973d25ae38bb4bc569bc4822d1ae3f7fbb525bbe
[platform/upstream/gcc.git] / gcc / config / sh / sh.c
1 /* Output routines for GCC for Renesas / SuperH SH.
2    Copyright (C) 1993-2013 Free Software Foundation, Inc.
3    Contributed by Steve Chamberlain (sac@cygnus.com).
4    Improved by Jim Wilson (wilson@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "insn-config.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "flags.h"
30 #include "expr.h"
31 #include "optabs.h"
32 #include "reload.h"
33 #include "function.h"
34 #include "regs.h"
35 #include "hard-reg-set.h"
36 #include "output.h"
37 #include "insn-attr.h"
38 #include "diagnostic-core.h"
39 #include "recog.h"
40 #include "dwarf2.h"
41 #include "tm_p.h"
42 #include "target.h"
43 #include "target-def.h"
44 #include "langhooks.h"
45 #include "basic-block.h"
46 #include "df.h"
47 #include "intl.h"
48 #include "sched-int.h"
49 #include "params.h"
50 #include "ggc.h"
51 #include "gimplify.h"
52 #include "cfgloop.h"
53 #include "alloc-pool.h"
54 #include "tm-constrs.h"
55 #include "opts.h"
56 #include "tree-pass.h"
57 #include "pass_manager.h"
58 #include "context.h"
59
60 #include <sstream>
61 #include <vector>
62 #include <algorithm>
63
64 int code_for_indirect_jump_scratch = CODE_FOR_indirect_jump_scratch;
65
66 /* These are some macros to abstract register modes.  */
67 #define CONST_OK_FOR_I10(VALUE) (((HOST_WIDE_INT)(VALUE)) >= -512 \
68                                  && ((HOST_WIDE_INT)(VALUE)) <= 511)
69
70 #define CONST_OK_FOR_ADD(size) \
71   (TARGET_SHMEDIA ? CONST_OK_FOR_I10 (size) : CONST_OK_FOR_I08 (size))
72 #define GEN_MOV (*(TARGET_SHMEDIA64 ? gen_movdi : gen_movsi))
73 #define GEN_ADD3 (*(TARGET_SHMEDIA64 ? gen_adddi3 : gen_addsi3))
74 #define GEN_SUB3 (*(TARGET_SHMEDIA64 ? gen_subdi3 : gen_subsi3))
75
76 /* Used to simplify the logic below.  Find the attributes wherever
77    they may be.  */
78 #define SH_ATTRIBUTES(decl) \
79   (TYPE_P (decl)) ? TYPE_ATTRIBUTES (decl) \
80                   : DECL_ATTRIBUTES (decl) \
81                   ? (DECL_ATTRIBUTES (decl)) \
82                   : TYPE_ATTRIBUTES (TREE_TYPE (decl))
83
84 /* Set to 1 by expand_prologue() when the function is an interrupt handler.  */
85 int current_function_interrupt;
86
87 tree sh_deferred_function_attributes;
88 tree *sh_deferred_function_attributes_tail = &sh_deferred_function_attributes;
89
90 /* Global variables for machine-dependent things.  */
91
92 /* Which cpu are we scheduling for.  */
93 enum processor_type sh_cpu;
94
95 /* Definitions used in ready queue reordering for first scheduling pass.  */
96
97 /* Reg weights arrays for modes SFmode and SImode, indexed by insn LUID.  */
98 static short *regmode_weight[2];
99
100 /* Total SFmode and SImode weights of scheduled insns.  */
101 static int curr_regmode_pressure[2];
102
103 /* Number of r0 life regions.  */
104 static int r0_life_regions;
105
106 /* If true, skip cycles for Q -> R movement.  */
107 static int skip_cycles = 0;
108
109 /* Cached value of can_issue_more.  This is cached in sh_variable_issue hook
110    and returned from sh_reorder2.  */
111 static short cached_can_issue_more;
112
113 /* Unique number for UNSPEC_BBR pattern.  */
114 static unsigned int unspec_bbr_uid = 1;
115
116 /* Provides the class number of the smallest class containing
117    reg number.  */
118 enum reg_class regno_reg_class[FIRST_PSEUDO_REGISTER] =
119 {
120   R0_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   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
136   FP0_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   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
152   TARGET_REGS, TARGET_REGS, TARGET_REGS, TARGET_REGS,
153   TARGET_REGS, TARGET_REGS, TARGET_REGS, TARGET_REGS,
154   DF_REGS, DF_REGS, DF_REGS, DF_REGS,
155   DF_REGS, DF_REGS, DF_REGS, DF_REGS,
156   NO_REGS, GENERAL_REGS, PR_REGS, T_REGS,
157   MAC_REGS, MAC_REGS, FPUL_REGS, FPSCR_REGS,
158   GENERAL_REGS, GENERAL_REGS,
159 };
160
161 char sh_register_names[FIRST_PSEUDO_REGISTER] \
162   [MAX_REGISTER_NAME_LENGTH + 1] = SH_REGISTER_NAMES_INITIALIZER;
163
164 char sh_additional_register_names[ADDREGNAMES_SIZE] \
165   [MAX_ADDITIONAL_REGISTER_NAME_LENGTH + 1]
166   = SH_ADDITIONAL_REGISTER_NAMES_INITIALIZER;
167
168 int assembler_dialect;
169
170 static bool shmedia_space_reserved_for_target_registers;
171
172 static void split_branches (rtx);
173 static int branch_dest (rtx);
174 static void print_slot (rtx);
175 static rtx add_constant (rtx, enum machine_mode, rtx);
176 static void dump_table (rtx, rtx);
177 static bool broken_move (rtx);
178 static bool mova_p (rtx);
179 static rtx find_barrier (int, rtx, rtx);
180 static bool noncall_uses_reg (rtx, rtx, rtx *);
181 static rtx gen_block_redirect (rtx, int, int);
182 static void sh_reorg (void);
183 static void sh_option_override (void);
184 static void output_stack_adjust (int, rtx, int, HARD_REG_SET *, bool);
185 static rtx frame_insn (rtx);
186 static rtx push (int);
187 static void pop (int);
188 static void push_regs (HARD_REG_SET *, int);
189 static int calc_live_regs (HARD_REG_SET *);
190 static HOST_WIDE_INT rounded_frame_size (int);
191 static bool sh_frame_pointer_required (void);
192 static rtx mark_constant_pool_use (rtx);
193 static tree sh_handle_interrupt_handler_attribute (tree *, tree, tree,
194                                                    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_print_operand (FILE *, rtx, int);
203 static void sh_print_operand_address (FILE *, rtx);
204 static bool sh_print_operand_punct_valid_p (unsigned char code);
205 static bool sh_asm_output_addr_const_extra (FILE *file, rtx x);
206 static void sh_output_function_epilogue (FILE *, HOST_WIDE_INT);
207 static void sh_insert_attributes (tree, tree *);
208 static const char *sh_check_pch_target_flags (int);
209 static int sh_register_move_cost (enum machine_mode, reg_class_t, reg_class_t);
210 static int sh_adjust_cost (rtx, rtx, rtx, int);
211 static int sh_issue_rate (void);
212 static int sh_dfa_new_cycle (FILE *, int, rtx, int, int, int *sort_p);
213 static short find_set_regmode_weight (rtx, enum machine_mode);
214 static short find_insn_regmode_weight (rtx, enum machine_mode);
215 static void find_regmode_weight (basic_block, enum machine_mode);
216 static int find_r0_life_regions (basic_block);
217 static void  sh_md_init_global (FILE *, int, int);
218 static void  sh_md_finish_global (FILE *, int);
219 static int rank_for_reorder (const void *, const void *);
220 static void swap_reorder (rtx *, int);
221 static void ready_reorder (rtx *, int);
222 static bool high_pressure (enum machine_mode);
223 static int sh_reorder (FILE *, int, rtx *, int *, int);
224 static int sh_reorder2 (FILE *, int, rtx *, int *, int);
225 static void sh_md_init (FILE *, int, int);
226 static int sh_variable_issue (FILE *, int, rtx, int);
227
228 static bool sh_function_ok_for_sibcall (tree, tree);
229
230 static bool sh_cannot_modify_jumps_p (void);
231 static reg_class_t sh_target_reg_class (void);
232 static bool sh_optimize_target_register_callee_saved (bool);
233 static bool sh_ms_bitfield_layout_p (const_tree);
234
235 static void sh_init_builtins (void);
236 static tree sh_builtin_decl (unsigned, bool);
237 static rtx sh_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
238 static void sh_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
239                                 HOST_WIDE_INT, tree);
240 static void sh_file_start (void);
241 static bool flow_dependent_p (rtx, rtx);
242 static void flow_dependent_p_1 (rtx, const_rtx, void *);
243 static int shiftcosts (rtx);
244 static int and_xor_ior_costs (rtx, int);
245 static int addsubcosts (rtx);
246 static int multcosts (rtx);
247 static bool unspec_caller_rtx_p (rtx);
248 static bool sh_cannot_copy_insn_p (rtx);
249 static bool sh_rtx_costs (rtx, int, int, int, int *, bool);
250 static int sh_address_cost (rtx, enum machine_mode, addr_space_t, bool);
251 static int sh_pr_n_sets (void);
252 static rtx sh_allocate_initial_value (rtx);
253 static reg_class_t sh_preferred_reload_class (rtx, reg_class_t);
254 static reg_class_t sh_secondary_reload (bool, rtx, reg_class_t,
255                                         enum machine_mode,
256                                         struct secondary_reload_info *);
257 static bool sh_legitimate_address_p (enum machine_mode, rtx, bool);
258 static rtx sh_legitimize_address (rtx, rtx, enum machine_mode);
259 static rtx sh_delegitimize_address (rtx);
260 static int shmedia_target_regs_stack_space (HARD_REG_SET *);
261 static int shmedia_reserve_space_for_target_registers_p (int, HARD_REG_SET *);
262 static int shmedia_target_regs_stack_adjust (HARD_REG_SET *);
263 static int scavenge_reg (HARD_REG_SET *s);
264 struct save_schedule_s;
265 static struct save_entry_s *sh5_schedule_saves (HARD_REG_SET *,
266                                                 struct save_schedule_s *, int);
267
268 static rtx sh_struct_value_rtx (tree, int);
269 static rtx sh_function_value (const_tree, const_tree, bool);
270 static bool sh_function_value_regno_p (const unsigned int);
271 static rtx sh_libcall_value (enum machine_mode, const_rtx);
272 static bool sh_return_in_memory (const_tree, const_tree);
273 static rtx sh_builtin_saveregs (void);
274 static void sh_setup_incoming_varargs (cumulative_args_t, enum machine_mode,
275                                        tree, int *, int);
276 static bool sh_strict_argument_naming (cumulative_args_t);
277 static bool sh_pretend_outgoing_varargs_named (cumulative_args_t);
278 static tree sh_build_builtin_va_list (void);
279 static void sh_va_start (tree, rtx);
280 static tree sh_gimplify_va_arg_expr (tree, tree, gimple_seq *, gimple_seq *);
281 static bool sh_promote_prototypes (const_tree);
282 static enum machine_mode sh_promote_function_mode (const_tree type,
283                                                    enum machine_mode,
284                                                    int *punsignedp,
285                                                    const_tree funtype,
286                                                    int for_return);
287 static bool sh_pass_by_reference (cumulative_args_t, enum machine_mode,
288                                   const_tree, bool);
289 static bool sh_callee_copies (cumulative_args_t, enum machine_mode,
290                               const_tree, bool);
291 static int sh_arg_partial_bytes (cumulative_args_t, enum machine_mode,
292                                  tree, bool);
293 static void sh_function_arg_advance (cumulative_args_t, enum machine_mode,
294                                      const_tree, bool);
295 static rtx sh_function_arg (cumulative_args_t, enum machine_mode,
296                             const_tree, bool);
297 static bool sh_scalar_mode_supported_p (enum machine_mode);
298 static int sh_dwarf_calling_convention (const_tree);
299 static void sh_encode_section_info (tree, rtx, int);
300 static bool sh2a_function_vector_p (tree);
301 static void sh_trampoline_init (rtx, tree, rtx);
302 static rtx sh_trampoline_adjust_address (rtx);
303 static void sh_conditional_register_usage (void);
304 static bool sh_legitimate_constant_p (enum machine_mode, rtx);
305 static int mov_insn_size (enum machine_mode, bool);
306 static int max_mov_insn_displacement (enum machine_mode, bool);
307 static int mov_insn_alignment_mask (enum machine_mode, bool);
308 static HOST_WIDE_INT disp_addr_displacement (rtx);
309 static bool sequence_insn_p (rtx);
310 static void sh_canonicalize_comparison (int *, rtx *, rtx *, bool);
311 static void sh_canonicalize_comparison (enum rtx_code&, rtx&, rtx&,
312                                         enum machine_mode, bool);
313 static bool sh_fixed_condition_code_regs (unsigned int* p1, unsigned int* p2);
314
315 static void sh_init_sync_libfuncs (void) ATTRIBUTE_UNUSED;
316 \f
317 static const struct attribute_spec sh_attribute_table[] =
318 {
319   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
320        affects_type_identity } */
321   { "interrupt_handler", 0, 0, true,  false, false,
322     sh_handle_interrupt_handler_attribute, false },
323   { "sp_switch",         1, 1, true,  false, false,
324      sh_handle_sp_switch_attribute, false },
325   { "trap_exit",         1, 1, true,  false, false,
326     sh_handle_trap_exit_attribute, false },
327   { "renesas",           0, 0, false, true, false,
328     sh_handle_renesas_attribute, false },
329   { "trapa_handler",     0, 0, true,  false, false,
330     sh_handle_interrupt_handler_attribute, false },
331   { "nosave_low_regs",   0, 0, true,  false, false,
332     sh_handle_interrupt_handler_attribute, false },
333   { "resbank",           0, 0, true,  false, false,
334     sh_handle_resbank_handler_attribute, false },
335   { "function_vector",   1, 1, true,  false, false,
336     sh2a_handle_function_vector_handler_attribute, false },
337   { NULL,                0, 0, false, false, false, NULL, false }
338 };
339 \f
340 /* Initialize the GCC target structure.  */
341 #undef TARGET_ATTRIBUTE_TABLE
342 #define TARGET_ATTRIBUTE_TABLE sh_attribute_table
343
344 /* The next two are used for debug info when compiling with -gdwarf.  */
345 #undef TARGET_ASM_UNALIGNED_HI_OP
346 #define TARGET_ASM_UNALIGNED_HI_OP "\t.uaword\t"
347 #undef TARGET_ASM_UNALIGNED_SI_OP
348 #define TARGET_ASM_UNALIGNED_SI_OP "\t.ualong\t"
349
350 /* These are NULLed out on non-SH5 in TARGET_OPTION_OVERRIDE.  */
351 #undef TARGET_ASM_UNALIGNED_DI_OP
352 #define TARGET_ASM_UNALIGNED_DI_OP "\t.uaquad\t"
353 #undef TARGET_ASM_ALIGNED_DI_OP
354 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
355
356 #undef TARGET_OPTION_OVERRIDE
357 #define TARGET_OPTION_OVERRIDE sh_option_override
358
359 #undef TARGET_PRINT_OPERAND
360 #define TARGET_PRINT_OPERAND sh_print_operand
361 #undef TARGET_PRINT_OPERAND_ADDRESS
362 #define TARGET_PRINT_OPERAND_ADDRESS sh_print_operand_address
363 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
364 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P sh_print_operand_punct_valid_p
365 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
366 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA sh_asm_output_addr_const_extra
367  
368 #undef TARGET_ASM_FUNCTION_EPILOGUE
369 #define TARGET_ASM_FUNCTION_EPILOGUE sh_output_function_epilogue
370
371 #undef TARGET_ASM_OUTPUT_MI_THUNK
372 #define TARGET_ASM_OUTPUT_MI_THUNK sh_output_mi_thunk
373
374 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
375 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK \
376   hook_bool_const_tree_hwi_hwi_const_tree_true
377
378 #undef TARGET_ASM_FILE_START
379 #define TARGET_ASM_FILE_START sh_file_start
380 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
381 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
382
383 #undef TARGET_REGISTER_MOVE_COST
384 #define TARGET_REGISTER_MOVE_COST sh_register_move_cost
385
386 #undef TARGET_INSERT_ATTRIBUTES
387 #define TARGET_INSERT_ATTRIBUTES sh_insert_attributes
388
389 #undef TARGET_SCHED_ADJUST_COST
390 #define TARGET_SCHED_ADJUST_COST sh_adjust_cost
391
392 #undef TARGET_SCHED_ISSUE_RATE
393 #define TARGET_SCHED_ISSUE_RATE sh_issue_rate
394
395 /* The next 5 hooks have been implemented for reenabling sched1.  With the
396    help of these macros we are limiting the movement of insns in sched1 to
397    reduce the register pressure.  The overall idea is to keep count of SImode
398    and SFmode regs required by already scheduled insns. When these counts
399    cross some threshold values; give priority to insns that free registers.
400    The insn that frees registers is most likely to be the insn with lowest
401    LUID (original insn order); but such an insn might be there in the stalled
402    queue (Q) instead of the ready queue (R).  To solve this, we skip cycles
403    up to a max of 8 cycles so that such insns may move from Q -> R.
404
405    The description of the hooks are as below:
406
407    TARGET_SCHED_INIT_GLOBAL: Added a new target hook in the generic
408    scheduler; it is called inside the sched_init function just after
409    find_insn_reg_weights function call. It is used to calculate the SImode
410    and SFmode weights of insns of basic blocks; much similar to what
411    find_insn_reg_weights does.
412    TARGET_SCHED_FINISH_GLOBAL: Corresponding cleanup hook.
413
414    TARGET_SCHED_DFA_NEW_CYCLE: Skip cycles if high register pressure is
415    indicated by TARGET_SCHED_REORDER2; doing this may move insns from
416    (Q)->(R).
417
418    TARGET_SCHED_REORDER: If the register pressure for SImode or SFmode is
419    high; reorder the ready queue so that the insn with lowest LUID will be
420    issued next.
421
422    TARGET_SCHED_REORDER2: If the register pressure is high, indicate to
423    TARGET_SCHED_DFA_NEW_CYCLE to skip cycles.
424
425    TARGET_SCHED_VARIABLE_ISSUE: Cache the value of can_issue_more so that it
426    can be returned from TARGET_SCHED_REORDER2.
427
428    TARGET_SCHED_INIT: Reset the register pressure counting variables.  */
429
430 #undef TARGET_SCHED_DFA_NEW_CYCLE
431 #define TARGET_SCHED_DFA_NEW_CYCLE sh_dfa_new_cycle
432
433 #undef TARGET_SCHED_INIT_GLOBAL
434 #define TARGET_SCHED_INIT_GLOBAL sh_md_init_global
435
436 #undef TARGET_SCHED_FINISH_GLOBAL
437 #define TARGET_SCHED_FINISH_GLOBAL sh_md_finish_global
438
439 #undef TARGET_SCHED_VARIABLE_ISSUE
440 #define TARGET_SCHED_VARIABLE_ISSUE sh_variable_issue
441
442 #undef TARGET_SCHED_REORDER
443 #define TARGET_SCHED_REORDER sh_reorder
444
445 #undef TARGET_SCHED_REORDER2
446 #define TARGET_SCHED_REORDER2 sh_reorder2
447
448 #undef TARGET_SCHED_INIT
449 #define TARGET_SCHED_INIT sh_md_init
450
451 #undef TARGET_DELEGITIMIZE_ADDRESS
452 #define TARGET_DELEGITIMIZE_ADDRESS sh_delegitimize_address
453
454 #undef TARGET_LEGITIMIZE_ADDRESS
455 #define TARGET_LEGITIMIZE_ADDRESS sh_legitimize_address
456
457 #undef TARGET_CANNOT_MODIFY_JUMPS_P
458 #define TARGET_CANNOT_MODIFY_JUMPS_P sh_cannot_modify_jumps_p
459 #undef TARGET_BRANCH_TARGET_REGISTER_CLASS
460 #define TARGET_BRANCH_TARGET_REGISTER_CLASS sh_target_reg_class
461 #undef TARGET_BRANCH_TARGET_REGISTER_CALLEE_SAVED
462 #define TARGET_BRANCH_TARGET_REGISTER_CALLEE_SAVED \
463   sh_optimize_target_register_callee_saved
464
465 #undef TARGET_MS_BITFIELD_LAYOUT_P
466 #define TARGET_MS_BITFIELD_LAYOUT_P sh_ms_bitfield_layout_p
467
468 #undef TARGET_INIT_BUILTINS
469 #define TARGET_INIT_BUILTINS sh_init_builtins
470 #undef TARGET_BUILTIN_DECL
471 #define TARGET_BUILTIN_DECL sh_builtin_decl
472 #undef TARGET_EXPAND_BUILTIN
473 #define TARGET_EXPAND_BUILTIN sh_expand_builtin
474
475 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
476 #define TARGET_FUNCTION_OK_FOR_SIBCALL sh_function_ok_for_sibcall
477
478 #undef TARGET_CANNOT_COPY_INSN_P
479 #define TARGET_CANNOT_COPY_INSN_P sh_cannot_copy_insn_p
480 #undef TARGET_RTX_COSTS
481 #define TARGET_RTX_COSTS sh_rtx_costs
482 #undef TARGET_ADDRESS_COST
483 #define TARGET_ADDRESS_COST sh_address_cost
484 #undef TARGET_ALLOCATE_INITIAL_VALUE
485 #define TARGET_ALLOCATE_INITIAL_VALUE sh_allocate_initial_value
486
487 #undef TARGET_MACHINE_DEPENDENT_REORG
488 #define TARGET_MACHINE_DEPENDENT_REORG sh_reorg
489
490 #undef TARGET_DWARF_REGISTER_SPAN
491 #define TARGET_DWARF_REGISTER_SPAN sh_dwarf_register_span
492
493 #ifdef HAVE_AS_TLS
494 #undef TARGET_HAVE_TLS
495 #define TARGET_HAVE_TLS true
496 #endif
497
498 #undef TARGET_PROMOTE_PROTOTYPES
499 #define TARGET_PROMOTE_PROTOTYPES sh_promote_prototypes
500 #undef TARGET_PROMOTE_FUNCTION_MODE
501 #define TARGET_PROMOTE_FUNCTION_MODE sh_promote_function_mode
502
503 #undef TARGET_FUNCTION_VALUE
504 #define TARGET_FUNCTION_VALUE sh_function_value
505 #undef TARGET_FUNCTION_VALUE_REGNO_P
506 #define TARGET_FUNCTION_VALUE_REGNO_P sh_function_value_regno_p
507 #undef TARGET_LIBCALL_VALUE
508 #define TARGET_LIBCALL_VALUE sh_libcall_value
509 #undef TARGET_STRUCT_VALUE_RTX
510 #define TARGET_STRUCT_VALUE_RTX sh_struct_value_rtx
511 #undef TARGET_RETURN_IN_MEMORY
512 #define TARGET_RETURN_IN_MEMORY sh_return_in_memory
513
514 #undef TARGET_EXPAND_BUILTIN_SAVEREGS
515 #define TARGET_EXPAND_BUILTIN_SAVEREGS sh_builtin_saveregs
516 #undef TARGET_SETUP_INCOMING_VARARGS
517 #define TARGET_SETUP_INCOMING_VARARGS sh_setup_incoming_varargs
518 #undef TARGET_STRICT_ARGUMENT_NAMING
519 #define TARGET_STRICT_ARGUMENT_NAMING sh_strict_argument_naming
520 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
521 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED sh_pretend_outgoing_varargs_named
522 #undef TARGET_MUST_PASS_IN_STACK
523 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
524 #undef TARGET_PASS_BY_REFERENCE
525 #define TARGET_PASS_BY_REFERENCE sh_pass_by_reference
526 #undef TARGET_CALLEE_COPIES
527 #define TARGET_CALLEE_COPIES sh_callee_copies
528 #undef TARGET_ARG_PARTIAL_BYTES
529 #define TARGET_ARG_PARTIAL_BYTES sh_arg_partial_bytes
530 #undef TARGET_FUNCTION_ARG
531 #define TARGET_FUNCTION_ARG sh_function_arg
532 #undef TARGET_FUNCTION_ARG_ADVANCE
533 #define TARGET_FUNCTION_ARG_ADVANCE sh_function_arg_advance
534
535 #undef TARGET_BUILD_BUILTIN_VA_LIST
536 #define TARGET_BUILD_BUILTIN_VA_LIST sh_build_builtin_va_list
537 #undef TARGET_EXPAND_BUILTIN_VA_START
538 #define TARGET_EXPAND_BUILTIN_VA_START sh_va_start
539 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
540 #define TARGET_GIMPLIFY_VA_ARG_EXPR sh_gimplify_va_arg_expr
541
542 #undef TARGET_SCALAR_MODE_SUPPORTED_P
543 #define TARGET_SCALAR_MODE_SUPPORTED_P sh_scalar_mode_supported_p
544 #undef TARGET_VECTOR_MODE_SUPPORTED_P
545 #define TARGET_VECTOR_MODE_SUPPORTED_P sh_vector_mode_supported_p
546
547 #undef TARGET_CHECK_PCH_TARGET_FLAGS
548 #define TARGET_CHECK_PCH_TARGET_FLAGS sh_check_pch_target_flags
549
550 #undef TARGET_DWARF_CALLING_CONVENTION
551 #define TARGET_DWARF_CALLING_CONVENTION sh_dwarf_calling_convention
552
553 #undef TARGET_FRAME_POINTER_REQUIRED
554 #define TARGET_FRAME_POINTER_REQUIRED sh_frame_pointer_required
555
556 /* Return regmode weight for insn.  */
557 #define INSN_REGMODE_WEIGHT(INSN, MODE)\
558   regmode_weight[((MODE) == SImode) ? 0 : 1][INSN_UID (INSN)]
559
560 /* Return current register pressure for regmode.  */
561 #define CURR_REGMODE_PRESSURE(MODE)\
562   curr_regmode_pressure[((MODE) == SImode) ? 0 : 1]
563
564 #undef  TARGET_ENCODE_SECTION_INFO
565 #define TARGET_ENCODE_SECTION_INFO      sh_encode_section_info
566
567 #undef TARGET_SECONDARY_RELOAD
568 #define TARGET_SECONDARY_RELOAD sh_secondary_reload
569
570 #undef  TARGET_PREFERRED_RELOAD_CLASS
571 #define TARGET_PREFERRED_RELOAD_CLASS sh_preferred_reload_class
572
573 #undef TARGET_CONDITIONAL_REGISTER_USAGE
574 #define TARGET_CONDITIONAL_REGISTER_USAGE sh_conditional_register_usage
575
576 #undef TARGET_LEGITIMATE_ADDRESS_P
577 #define TARGET_LEGITIMATE_ADDRESS_P     sh_legitimate_address_p
578
579 #undef TARGET_TRAMPOLINE_INIT
580 #define TARGET_TRAMPOLINE_INIT          sh_trampoline_init
581 #undef TARGET_TRAMPOLINE_ADJUST_ADDRESS
582 #define TARGET_TRAMPOLINE_ADJUST_ADDRESS sh_trampoline_adjust_address
583
584 #undef TARGET_LEGITIMATE_CONSTANT_P
585 #define TARGET_LEGITIMATE_CONSTANT_P    sh_legitimate_constant_p
586
587 #undef TARGET_CANONICALIZE_COMPARISON
588 #define TARGET_CANONICALIZE_COMPARISON  sh_canonicalize_comparison
589
590 #undef TARGET_FIXED_CONDITION_CODE_REGS
591 #define TARGET_FIXED_CONDITION_CODE_REGS sh_fixed_condition_code_regs
592
593 /* Machine-specific symbol_ref flags.  */
594 #define SYMBOL_FLAG_FUNCVEC_FUNCTION    (SYMBOL_FLAG_MACH_DEP << 0)
595
596 /* The tas.b instruction sets the 7th bit in the byte, i.e. 0x80.  This value
597    is used by optabs.c atomic op expansion code as well as in sync.md.  */
598 #undef TARGET_ATOMIC_TEST_AND_SET_TRUEVAL
599 #define TARGET_ATOMIC_TEST_AND_SET_TRUEVAL 0x80
600
601 struct gcc_target targetm = TARGET_INITIALIZER;
602 \f
603
604 /* Information on the currently selected atomic model.
605    This is initialized in sh_option_override.  */
606 static sh_atomic_model selected_atomic_model_;
607
608 const sh_atomic_model&
609 selected_atomic_model (void)
610 {
611   return selected_atomic_model_;
612 }
613
614 static sh_atomic_model
615 parse_validate_atomic_model_option (const char* str)
616 {
617   const char* model_names[sh_atomic_model::num_models];
618   model_names[sh_atomic_model::none] = "none";
619   model_names[sh_atomic_model::soft_gusa] = "soft-gusa";
620   model_names[sh_atomic_model::hard_llcs] = "hard-llcs";
621   model_names[sh_atomic_model::soft_tcb] = "soft-tcb";
622   model_names[sh_atomic_model::soft_imask] = "soft-imask";
623
624   const char* model_cdef_names[sh_atomic_model::num_models];
625   model_cdef_names[sh_atomic_model::none] = "NONE";
626   model_cdef_names[sh_atomic_model::soft_gusa] = "SOFT_GUSA";
627   model_cdef_names[sh_atomic_model::hard_llcs] = "HARD_LLCS";
628   model_cdef_names[sh_atomic_model::soft_tcb] = "SOFT_TCB";
629   model_cdef_names[sh_atomic_model::soft_imask] = "SOFT_IMASK";
630
631   sh_atomic_model ret;
632   ret.type = sh_atomic_model::none;
633   ret.name = model_names[sh_atomic_model::none];
634   ret.cdef_name = model_cdef_names[sh_atomic_model::none];
635   ret.strict = false;
636   ret.tcb_gbr_offset = -1;
637
638   /* Handle empty string as 'none'.  */
639   if (str == NULL || *str == '\0')
640     return ret;
641
642 #define err_ret(...) do { error (__VA_ARGS__); return ret; } while (0)
643
644   std::vector<std::string> tokens;
645   for (std::stringstream ss (str); ss.good (); )
646   {
647     tokens.push_back (std::string ());
648     std::getline (ss, tokens.back (), ',');
649   }
650
651   if (tokens.empty ())
652     err_ret ("invalid atomic model option");
653
654   /* The first token must be the atomic model name.  */
655   {
656     for (size_t i = 0; i < sh_atomic_model::num_models; ++i)
657       if (tokens.front () == model_names[i])
658         {
659           ret.type = (sh_atomic_model::enum_type)i;
660           ret.name = model_names[i];
661           ret.cdef_name = model_cdef_names[i];
662           goto got_mode_name;
663         }
664
665     err_ret ("invalid atomic model name \"%s\"", tokens.front ().c_str ());
666 got_mode_name:;
667   }
668
669   /* Go through the remaining tokens.  */
670   for (size_t i = 1; i < tokens.size (); ++i)
671     {
672       if (tokens[i] == "strict")
673         ret.strict = true;
674       else if (tokens[i].find ("gbr-offset=") == 0)
675         {
676           std::string offset_str = tokens[i].substr (strlen ("gbr-offset="));
677           ret.tcb_gbr_offset = integral_argument (offset_str.c_str ());
678           if (offset_str.empty () || ret.tcb_gbr_offset == -1)
679             err_ret ("could not parse gbr-offset value \"%s\" in atomic model "
680                      "option", offset_str.c_str ());
681         }
682       else
683         err_ret ("unknown parameter \"%s\" in atomic model option",
684                  tokens[i].c_str ());
685     }
686
687   /* Check that the selection makes sense.  */
688   if (TARGET_SHMEDIA && ret.type != sh_atomic_model::none)
689     err_ret ("atomic operations are not supported on SHmedia");
690
691   if (ret.type == sh_atomic_model::soft_gusa && !TARGET_SH3)
692     err_ret ("atomic model %s is only available on SH3 and SH4 targets",
693              ret.name);
694
695   if (ret.type == sh_atomic_model::hard_llcs && !TARGET_SH4A)
696     err_ret ("atomic model %s is only available on SH4A targets", ret.name);
697
698   if (ret.type == sh_atomic_model::soft_tcb && ret.tcb_gbr_offset == -1)
699     err_ret ("atomic model %s requires gbr-offset parameter", ret.name);
700
701   if (ret.type == sh_atomic_model::soft_tcb
702       && (ret.tcb_gbr_offset < 0 || ret.tcb_gbr_offset > 1020
703           || (ret.tcb_gbr_offset & 3) != 0))
704     err_ret ("invalid gbr-offset value \"%d\" for atomic model %s; it must be "
705              "a multiple of 4 in the range 0-1020", ret.tcb_gbr_offset,
706              ret.name);
707
708   if (ret.type == sh_atomic_model::soft_imask && TARGET_USERMODE)
709     err_ret ("cannot use atomic model %s in user mode", ret.name);
710
711   return ret;
712
713 #undef err_ret
714 }
715
716 /* Register SH specific RTL passes.  */
717 extern opt_pass* make_pass_sh_treg_combine (gcc::context* ctx, bool split_insns,
718                                      const char* name);
719 static void
720 register_sh_passes (void)
721 {
722   if (!TARGET_SH1)
723     return;
724
725 /* Running the sh_treg_combine pass after ce1 generates better code when
726    comparisons are combined and reg-reg moves are introduced, because
727    reg-reg moves will be eliminated afterwards.  However, there are quite
728    some cases where combine will be unable to fold comparison related insns,
729    thus for now don't do it.
730   register_pass (make_pass_sh_treg_combine (g, false, "sh_treg_combine1"),
731                  PASS_POS_INSERT_AFTER, "ce1", 1);
732 */
733
734   /* Run sh_treg_combine pass after combine but before register allocation.  */
735   register_pass (make_pass_sh_treg_combine (g, true, "sh_treg_combine2"),
736                  PASS_POS_INSERT_AFTER, "split1", 1);
737
738   /* Run sh_treg_combine pass after register allocation and basic block
739      reordering as this sometimes creates new opportunities.  */
740   register_pass (make_pass_sh_treg_combine (g, true, "sh_treg_combine3"),
741                  PASS_POS_INSERT_AFTER, "split4", 1);
742 }
743
744 /* Implement TARGET_OPTION_OVERRIDE macro.  Validate and override 
745    various options, and do some machine dependent initialization.  */
746 static void
747 sh_option_override (void)
748 {
749   int regno;
750
751   SUBTARGET_OVERRIDE_OPTIONS;
752   if (optimize > 1 && !optimize_size)
753     target_flags |= MASK_SAVE_ALL_TARGET_REGS;
754   sh_cpu = PROCESSOR_SH1;
755   assembler_dialect = 0;
756   if (TARGET_SH2)
757     sh_cpu = PROCESSOR_SH2;
758   if (TARGET_SH2E)
759     sh_cpu = PROCESSOR_SH2E;
760   if (TARGET_SH2A)
761     sh_cpu = PROCESSOR_SH2A;
762   if (TARGET_SH3)
763     sh_cpu = PROCESSOR_SH3;
764   if (TARGET_SH3E)
765     sh_cpu = PROCESSOR_SH3E;
766   if (TARGET_SH4)
767     {
768       assembler_dialect = 1;
769       sh_cpu = PROCESSOR_SH4;
770     }
771   if (TARGET_SH4A_ARCH)
772     {
773       assembler_dialect = 1;
774       sh_cpu = PROCESSOR_SH4A;
775     }
776   if (TARGET_SH5)
777     {
778       sh_cpu = PROCESSOR_SH5;
779       target_flags |= MASK_ALIGN_DOUBLE;
780       if (TARGET_SHMEDIA_FPU)
781         target_flags |= MASK_FMOVD;
782       if (TARGET_SHMEDIA)
783         {
784           /* There are no delay slots on SHmedia.  */
785           flag_delayed_branch = 0;
786           /* Relaxation isn't yet supported for SHmedia */
787           target_flags &= ~MASK_RELAX;
788           /* After reload, if conversion does little good but can cause
789              ICEs:
790              - find_if_block doesn't do anything for SH because we don't
791                have conditional execution patterns.  (We use conditional
792                move patterns, which are handled differently, and only
793                before reload).
794              - find_cond_trap doesn't do anything for the SH because we
795                don't have conditional traps.
796              - find_if_case_1 uses redirect_edge_and_branch_force in
797                the only path that does an optimization, and this causes
798                an ICE when branch targets are in registers.
799              - find_if_case_2 doesn't do anything for the SHmedia after
800                reload except when it can redirect a tablejump - and
801                that's rather rare.  */
802           flag_if_conversion2 = 0;
803           if (! strcmp (sh_div_str, "call"))
804             sh_div_strategy = SH_DIV_CALL;
805           else if (! strcmp (sh_div_str, "call2"))
806             sh_div_strategy = SH_DIV_CALL2;
807           if (! strcmp (sh_div_str, "fp") && TARGET_FPU_ANY)
808             sh_div_strategy = SH_DIV_FP;
809           else if (! strcmp (sh_div_str, "inv"))
810             sh_div_strategy = SH_DIV_INV;
811           else if (! strcmp (sh_div_str, "inv:minlat"))
812             sh_div_strategy = SH_DIV_INV_MINLAT;
813           else if (! strcmp (sh_div_str, "inv20u"))
814             sh_div_strategy = SH_DIV_INV20U;
815           else if (! strcmp (sh_div_str, "inv20l"))
816             sh_div_strategy = SH_DIV_INV20L;
817           else if (! strcmp (sh_div_str, "inv:call2"))
818             sh_div_strategy = SH_DIV_INV_CALL2;
819           else if (! strcmp (sh_div_str, "inv:call"))
820             sh_div_strategy = SH_DIV_INV_CALL;
821           else if (! strcmp (sh_div_str, "inv:fp"))
822             {
823               if (TARGET_FPU_ANY)
824                 sh_div_strategy = SH_DIV_INV_FP;
825               else
826                 sh_div_strategy = SH_DIV_INV;
827             }
828           TARGET_CBRANCHDI4 = 0;
829           /* Assembler CFI isn't yet fully supported for SHmedia.  */
830           flag_dwarf2_cfi_asm = 0;
831         }
832     }
833   else
834     {
835        /* Only the sh64-elf assembler fully supports .quad properly.  */
836        targetm.asm_out.aligned_op.di = NULL;
837        targetm.asm_out.unaligned_op.di = NULL;
838     }
839   if (TARGET_SH1)
840     {
841       if (! strcmp (sh_div_str, "call-div1"))
842         sh_div_strategy = SH_DIV_CALL_DIV1;
843       else if (! strcmp (sh_div_str, "call-fp")
844                && (TARGET_FPU_DOUBLE || TARGET_FPU_SINGLE_ONLY
845                    || (TARGET_SHCOMPACT && TARGET_FPU_ANY)))
846         sh_div_strategy = SH_DIV_CALL_FP;
847       else if (! strcmp (sh_div_str, "call-table") && TARGET_DYNSHIFT)
848         sh_div_strategy = SH_DIV_CALL_TABLE;
849       else
850         /* Pick one that makes most sense for the target in general.
851            It is not much good to use different functions depending
852            on -Os, since then we'll end up with two different functions
853            when some of the code is compiled for size, and some for
854            speed.  */
855
856         /* SH4 tends to emphasize speed.  */
857         if (TARGET_HARD_SH4)
858           sh_div_strategy = SH_DIV_CALL_TABLE;
859         /* These have their own way of doing things.  */
860         else if (TARGET_SH2A)
861           sh_div_strategy = SH_DIV_INTRINSIC;
862         /* ??? Should we use the integer SHmedia function instead?  */
863         else if (TARGET_SHCOMPACT && TARGET_FPU_ANY)
864           sh_div_strategy = SH_DIV_CALL_FP;
865         /* SH1 .. SH3 cores often go into small-footprint systems, so
866            default to the smallest implementation available.  */
867         else
868           sh_div_strategy = SH_DIV_CALL_DIV1;
869     }
870   if (!TARGET_SH1)
871     TARGET_PRETEND_CMOVE = 0;
872   if (sh_divsi3_libfunc[0])
873     ; /* User supplied - leave it alone.  */
874   else if (TARGET_DIVIDE_CALL_FP)
875     sh_divsi3_libfunc = "__sdivsi3_i4";
876   else if (TARGET_DIVIDE_CALL_TABLE)
877     sh_divsi3_libfunc = "__sdivsi3_i4i";
878   else if (TARGET_SH5)
879     sh_divsi3_libfunc = "__sdivsi3_1";
880   else
881     sh_divsi3_libfunc = "__sdivsi3";
882   if (sh_branch_cost == -1)
883     {
884       sh_branch_cost = 1;
885
886       /*  The SH1 does not have delay slots, hence we get a pipeline stall
887           at every branch.  The SH4 is superscalar, so the single delay slot
888           is not sufficient to keep both pipelines filled.  */
889       if (! TARGET_SH2 || TARGET_HARD_SH4)
890         sh_branch_cost = 2;
891     }
892
893   /* Set -mzdcbranch for SH4 / SH4A if not otherwise specified by the user.  */
894   if (! global_options_set.x_TARGET_ZDCBRANCH && TARGET_HARD_SH4)
895     TARGET_ZDCBRANCH = 1;
896
897   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
898     if (! VALID_REGISTER_P (regno))
899       sh_register_names[regno][0] = '\0';
900
901   for (regno = 0; regno < ADDREGNAMES_SIZE; regno++)
902     if (! VALID_REGISTER_P (ADDREGNAMES_REGNO (regno)))
903       sh_additional_register_names[regno][0] = '\0';
904
905   if ((flag_pic && ! TARGET_PREFERGOT)
906       || (TARGET_SHMEDIA && !TARGET_PT_FIXED))
907     flag_no_function_cse = 1;
908
909   if (targetm.small_register_classes_for_mode_p (VOIDmode))
910     {
911       /* Never run scheduling before reload, since that can
912          break global alloc, and generates slower code anyway due
913          to the pressure on R0.  */
914       /* Enable sched1 for SH4 if the user explicitly requests.
915          When sched1 is enabled, the ready queue will be reordered by
916          the target hooks if pressure is high.  We can not do this for
917          PIC, SH3 and lower as they give spill failures for R0.  */
918       if (!TARGET_HARD_SH4 || flag_pic)
919         flag_schedule_insns = 0;
920       /* ??? Current exception handling places basic block boundaries
921          after call_insns.  It causes the high pressure on R0 and gives
922          spill failures for R0 in reload.  See PR 22553 and the thread
923          on gcc-patches
924          <http://gcc.gnu.org/ml/gcc-patches/2005-10/msg00816.html>.  */
925       else if (flag_exceptions)
926         {
927           if (flag_schedule_insns && global_options_set.x_flag_schedule_insns)
928             warning (0, "ignoring -fschedule-insns because of exception "
929                         "handling bug");
930           flag_schedule_insns = 0;
931         }
932       else if (flag_schedule_insns
933                && !global_options_set.x_flag_schedule_insns)
934         flag_schedule_insns = 0;
935     }
936
937   /* Unwind info is not correct around the CFG unless either a frame
938      pointer is present or M_A_O_A is set.  Fixing this requires rewriting
939      unwind info generation to be aware of the CFG and propagating states
940      around edges.  */
941   if ((flag_unwind_tables || flag_asynchronous_unwind_tables
942        || flag_exceptions || flag_non_call_exceptions)
943       && flag_omit_frame_pointer && !TARGET_ACCUMULATE_OUTGOING_ARGS)
944     {
945       warning (0, "unwind tables currently require either a frame pointer "
946                "or -maccumulate-outgoing-args for correctness");
947       TARGET_ACCUMULATE_OUTGOING_ARGS = 1;
948     }
949
950   /* Unwinding with -freorder-blocks-and-partition does not work on this
951      architecture, because it requires far jumps to label crossing between
952      hot/cold sections which are rejected on this architecture.  */
953   if (flag_reorder_blocks_and_partition)
954     {
955       if (flag_exceptions)
956         {
957           inform (input_location, 
958                   "-freorder-blocks-and-partition does not work with "
959                   "exceptions on this architecture");
960           flag_reorder_blocks_and_partition = 0;
961           flag_reorder_blocks = 1;
962         }
963       else if (flag_unwind_tables)
964         {
965           inform (input_location,
966                   "-freorder-blocks-and-partition does not support unwind "
967                   "info on this architecture");
968           flag_reorder_blocks_and_partition = 0;
969           flag_reorder_blocks = 1;
970         }
971     }
972
973   /*  Adjust loop, jump and function alignment values (in bytes), if those
974       were not specified by the user using -falign-loops, -falign-jumps
975       and -falign-functions options.
976       32 bit alignment is better for speed, because instructions can be
977       fetched as a pair from a longword boundary.  For size use 16 bit
978       alignment to get more compact code.
979       Aligning all jumps increases the code size, even if it might
980       result in slightly faster code.  Thus, it is set to the smallest 
981       alignment possible if not specified by the user.  */
982   if (align_loops == 0)
983     {
984       if (TARGET_SH5)
985         align_loops = 8;
986       else
987         align_loops = optimize_size ? 2 : 4;
988     }
989
990   if (align_jumps == 0)
991     {
992       if (TARGET_SHMEDIA)
993         align_jumps = 1 << CACHE_LOG;
994       else
995         align_jumps = 2;
996     }
997   else if (align_jumps < (TARGET_SHMEDIA ? 4 : 2))
998     align_jumps = TARGET_SHMEDIA ? 4 : 2;
999
1000   if (align_functions == 0)
1001     {
1002       if (TARGET_SHMEDIA)
1003         align_functions = optimize_size
1004                           ? FUNCTION_BOUNDARY/8 : (1 << CACHE_LOG);
1005       else
1006         align_functions = optimize_size ? 2 : 4;
1007     }
1008
1009   /* The linker relaxation code breaks when a function contains
1010      alignments that are larger than that at the start of a
1011      compilation unit.  */
1012   if (TARGET_RELAX)
1013     {
1014       int min_align = align_loops > align_jumps ? align_loops : align_jumps;
1015
1016       /* Also take possible .long constants / mova tables into account. */
1017       if (min_align < 4)
1018         min_align = 4;
1019       if (align_functions < min_align)
1020         align_functions = min_align;
1021     }
1022
1023   if (flag_unsafe_math_optimizations)
1024     {
1025       /* Enable fsca insn for SH4A if not otherwise specified by the user.  */
1026       if (global_options_set.x_TARGET_FSCA == 0 && TARGET_SH4A_FP)
1027         TARGET_FSCA = 1;
1028
1029       /* Enable fsrra insn for SH4A if not otherwise specified by the user.  */
1030       if (global_options_set.x_TARGET_FSRRA == 0 && TARGET_SH4A_FP)
1031         TARGET_FSRRA = 1;
1032     }
1033
1034   /*  Allow fsrra insn only if -funsafe-math-optimizations and
1035       -ffinite-math-only is enabled.  */
1036   TARGET_FSRRA = TARGET_FSRRA
1037                  && flag_unsafe_math_optimizations
1038                  && flag_finite_math_only;
1039
1040   /* If the -mieee option was not explicitly set by the user, turn it on
1041      unless -ffinite-math-only was specified.  See also PR 33135.  */
1042   if (! global_options_set.x_TARGET_IEEE)
1043     TARGET_IEEE = ! flag_finite_math_only;
1044
1045   if (sh_fixed_range_str)
1046     sh_fix_range (sh_fixed_range_str);
1047
1048   /* This target defaults to strict volatile bitfields.  */
1049   if (flag_strict_volatile_bitfields < 0 && abi_version_at_least(2))
1050     flag_strict_volatile_bitfields = 1;
1051
1052   /* Parse atomic model option and make sure it is valid for the current
1053      target CPU.  */
1054   selected_atomic_model_
1055     = parse_validate_atomic_model_option (sh_atomic_model_str);
1056
1057   register_sh_passes ();
1058 }
1059 \f
1060 /* Print the operand address in x to the stream.  */
1061 static void
1062 sh_print_operand_address (FILE *stream, rtx x)
1063 {
1064   switch (GET_CODE (x))
1065     {
1066     case REG:
1067     case SUBREG:
1068       fprintf (stream, "@%s", reg_names[true_regnum (x)]);
1069       break;
1070
1071     case PLUS:
1072       {
1073         rtx base = XEXP (x, 0);
1074         rtx index = XEXP (x, 1);
1075
1076         switch (GET_CODE (index))
1077           {
1078           case CONST_INT:
1079             fprintf (stream, "@(%d,%s)", (int) INTVAL (index),
1080                      reg_names[true_regnum (base)]);
1081             break;
1082
1083           case REG:
1084           case SUBREG:
1085             {
1086               int base_num = true_regnum (base);
1087               int index_num = true_regnum (index);
1088
1089               fprintf (stream, "@(r0,%s)",
1090                        reg_names[MAX (base_num, index_num)]);
1091               break;
1092             }
1093
1094           default:
1095             gcc_unreachable ();
1096           }
1097       }
1098       break;
1099
1100     case PRE_DEC:
1101       fprintf (stream, "@-%s", reg_names[true_regnum (XEXP (x, 0))]);
1102       break;
1103
1104     case POST_INC:
1105       fprintf (stream, "@%s+", reg_names[true_regnum (XEXP (x, 0))]);
1106       break;
1107
1108     default:
1109       x = mark_constant_pool_use (x);
1110       output_addr_const (stream, x);
1111       break;
1112     }
1113 }
1114
1115 /* Print operand x (an rtx) in assembler syntax to file stream
1116    according to modifier code.
1117
1118    '.'  print a .s if insn needs delay slot
1119    ','  print LOCAL_LABEL_PREFIX
1120    '@'  print trap, rte or rts depending upon pragma interruptness
1121    '#'  output a nop if there is nothing to put in the delay slot
1122    '''  print likelihood suffix (/u for unlikely).
1123    '>'  print branch target if -fverbose-asm
1124    'O'  print a constant without the #
1125    'R'  print the LSW of a dp value - changes if in little endian
1126    'S'  print the MSW of a dp value - changes if in little endian
1127    'T'  print the next word of a dp value - same as 'R' in big endian mode.
1128    'M'  SHMEDIA: print an `x' if `m' will print `base,index'.
1129         otherwise: print .b / .w / .l / .s / .d suffix if operand is a MEM.
1130    'N'  print 'r63' if the operand is (const_int 0).
1131    'd'  print a V2SF reg as dN instead of fpN.
1132    'm'  print a pair `base,offset' or `base,index', for LD and ST.
1133    'U'  Likewise for {LD,ST}{HI,LO}.
1134    'V'  print the position of a single bit set.
1135    'W'  print the position of a single bit cleared.
1136    't'  print a memory address which is a register.
1137    'u'  prints the lowest 16 bits of CONST_INT, as an unsigned value.
1138    'o'  output an operator.  */
1139 static void
1140 sh_print_operand (FILE *stream, rtx x, int code)
1141 {
1142   int regno;
1143   enum machine_mode mode;
1144
1145   switch (code)
1146     {
1147       tree trapa_attr;
1148
1149     case '.':
1150       if (final_sequence
1151           && ! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1152           && get_attr_length (XVECEXP (final_sequence, 0, 1)))
1153         fprintf (stream, ASSEMBLER_DIALECT ? "/s" : ".s");
1154       break;
1155     case ',':
1156       fprintf (stream, "%s", LOCAL_LABEL_PREFIX);
1157       break;
1158     case '@':
1159       trapa_attr = lookup_attribute ("trap_exit",
1160                                       DECL_ATTRIBUTES (current_function_decl));
1161       if (trapa_attr)
1162         fprintf (stream, "trapa #%ld",
1163                  (long) TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (trapa_attr))));
1164       else if (sh_cfun_interrupt_handler_p ())
1165         {
1166           if (sh_cfun_resbank_handler_p ())
1167             fprintf (stream, "resbank\n");
1168           fprintf (stream, "rte");
1169         }
1170       else
1171         fprintf (stream, "rts");
1172       break;
1173     case '#':
1174       /* Output a nop if there's nothing in the delay slot.  */
1175       if (dbr_sequence_length () == 0)
1176         fprintf (stream, "\n\tnop");
1177       break;
1178     case '\'':
1179       {
1180         rtx note = find_reg_note (current_output_insn, REG_BR_PROB, 0);
1181
1182         if (note && XINT (note, 0) * 2 < REG_BR_PROB_BASE)
1183           fputs ("/u", stream);
1184         break;
1185       }
1186     case '>':
1187       if (flag_verbose_asm && JUMP_LABEL (current_output_insn))
1188         {
1189           fputs ("\t! target: ", stream);
1190           output_addr_const (stream, JUMP_LABEL (current_output_insn));
1191         }
1192       break;
1193     case 'O':
1194       x = mark_constant_pool_use (x);
1195       output_addr_const (stream, x);
1196       break;
1197     /* N.B.: %R / %S / %T adjust memory addresses by four.
1198        For SHMEDIA, that means they can be used to access the first and
1199        second 32 bit part of a 64 bit (or larger) value that
1200        might be held in floating point registers or memory.
1201        While they can be used to access 64 bit parts of a larger value
1202        held in general purpose registers, that won't work with memory -
1203        neither for fp registers, since the frxx names are used.  */
1204     case 'R':
1205       if (REG_P (x) || GET_CODE (x) == SUBREG)
1206         {
1207           regno = true_regnum (x);
1208           regno += FP_REGISTER_P (regno) ? 1 : SH_REG_LSW_OFFSET;
1209           fputs (reg_names[regno], (stream));
1210         }
1211       else if (MEM_P (x))
1212         {
1213           x = adjust_address (x, SImode, 4 * SH_REG_LSW_OFFSET);
1214           sh_print_operand_address (stream, XEXP (x, 0));
1215         }
1216       else
1217         {
1218           rtx sub = NULL_RTX;
1219
1220           mode = GET_MODE (x);
1221           if (mode == VOIDmode)
1222             mode = DImode;
1223           if (GET_MODE_SIZE (mode) >= 8)
1224             sub = simplify_subreg (SImode, x, mode, 4 * SH_REG_LSW_OFFSET);
1225           if (sub)
1226             sh_print_operand (stream, sub, 0);
1227           else
1228             output_operand_lossage ("invalid operand to %%R");
1229         }
1230       break;
1231     case 'S':
1232       if (REG_P (x) || GET_CODE (x) == SUBREG)
1233         {
1234           regno = true_regnum (x);
1235           regno += FP_REGISTER_P (regno) ? 0 : SH_REG_MSW_OFFSET;
1236           fputs (reg_names[regno], (stream));
1237         }
1238       else if (MEM_P (x))
1239         {
1240           x = adjust_address (x, SImode, 4 * SH_REG_MSW_OFFSET);
1241           sh_print_operand_address (stream, XEXP (x, 0));
1242         }
1243       else
1244         {
1245           rtx sub = NULL_RTX;
1246
1247           mode = GET_MODE (x);
1248           if (mode == VOIDmode)
1249             mode = DImode;
1250           if (GET_MODE_SIZE (mode) >= 8)
1251             sub = simplify_subreg (SImode, x, mode, 4 * SH_REG_MSW_OFFSET);
1252           if (sub)
1253             sh_print_operand (stream, sub, 0);
1254           else
1255             output_operand_lossage ("invalid operand to %%S");
1256         }
1257       break;
1258     case 'T':
1259       /* Next word of a double.  */
1260       switch (GET_CODE (x))
1261         {
1262         case REG:
1263           fputs (reg_names[REGNO (x) + 1], (stream));
1264           break;
1265         case MEM:
1266           if (GET_CODE (XEXP (x, 0)) != PRE_DEC
1267               && GET_CODE (XEXP (x, 0)) != POST_INC)
1268             x = adjust_address (x, SImode, 4);
1269           sh_print_operand_address (stream, XEXP (x, 0));
1270           break;
1271         default:
1272           break;
1273         }
1274       break;
1275
1276     case 't':
1277       gcc_assert (MEM_P (x));
1278       x = XEXP (x, 0);
1279       switch (GET_CODE (x))
1280         {
1281         case REG:
1282         case SUBREG:
1283           sh_print_operand (stream, x, 0);
1284           break;
1285         default:
1286           break;
1287         }
1288       break;
1289
1290     case 'o':
1291       switch (GET_CODE (x))
1292         {
1293         case PLUS:  fputs ("add", stream); break;
1294         case MINUS: fputs ("sub", stream); break;
1295         case MULT:  fputs ("mul", stream); break;
1296         case DIV:   fputs ("div", stream); break;
1297         case EQ:    fputs ("eq",  stream); break;
1298         case NE:    fputs ("ne",  stream); break;
1299         case GT:  case LT:  fputs ("gt",  stream); break;
1300         case GE:  case LE:  fputs ("ge",  stream); break;
1301         case GTU: case LTU: fputs ("gtu", stream); break;
1302         case GEU: case LEU: fputs ("geu", stream); break;
1303         default:
1304           break;
1305         }
1306       break;
1307     case 'M':
1308       if (TARGET_SHMEDIA)
1309         {
1310           if (MEM_P (x)
1311               && GET_CODE (XEXP (x, 0)) == PLUS
1312               && (REG_P (XEXP (XEXP (x, 0), 1))
1313                   || GET_CODE (XEXP (XEXP (x, 0), 1)) == SUBREG))
1314             fputc ('x', stream);
1315         }
1316       else
1317         {
1318           if (MEM_P (x))
1319             {
1320               switch (GET_MODE (x))
1321                 {
1322                 case QImode: fputs (".b", stream); break;
1323                 case HImode: fputs (".w", stream); break;
1324                 case SImode: fputs (".l", stream); break;
1325                 case SFmode: fputs (".s", stream); break;
1326                 case DFmode: fputs (".d", stream); break;
1327                 default: gcc_unreachable ();
1328                 }
1329             }
1330         }
1331       break;
1332
1333     case 'm':
1334       gcc_assert (MEM_P (x));
1335       x = XEXP (x, 0);
1336       /* Fall through.  */
1337     case 'U':
1338       switch (GET_CODE (x))
1339         {
1340         case REG:
1341         case SUBREG:
1342           sh_print_operand (stream, x, 0);
1343           fputs (", 0", stream);
1344           break;
1345
1346         case PLUS:
1347           sh_print_operand (stream, XEXP (x, 0), 0);
1348           fputs (", ", stream);
1349           sh_print_operand (stream, XEXP (x, 1), 0);
1350           break;
1351
1352         default:
1353           gcc_unreachable ();
1354         }
1355       break;
1356
1357     case 'V':
1358       {
1359         int num = exact_log2 (INTVAL (x));
1360         gcc_assert (num >= 0);
1361         fprintf (stream, "#%d", num);
1362       }
1363       break;
1364
1365     case 'W':
1366       {
1367         int num = exact_log2 (~INTVAL (x));
1368         gcc_assert (num >= 0);
1369         fprintf (stream, "#%d", num);
1370       }
1371       break;
1372
1373     case 'd':
1374       gcc_assert (REG_P (x) && GET_MODE (x) == V2SFmode);
1375
1376       fprintf ((stream), "d%s", reg_names[REGNO (x)] + 1);
1377       break;
1378
1379     case 'N':
1380       if (x == CONST0_RTX (GET_MODE (x)))
1381         {
1382           fprintf ((stream), "r63");
1383           break;
1384         }
1385       goto default_output;
1386     case 'u':
1387       if (CONST_INT_P (x))
1388         {
1389           fprintf ((stream), "%u", (unsigned) INTVAL (x) & (0x10000 - 1));
1390           break;
1391         }
1392       /* Fall through.  */
1393
1394     default_output:
1395     default:
1396       regno = 0;
1397       mode = GET_MODE (x);
1398
1399       switch (GET_CODE (x))
1400         {
1401         case TRUNCATE:
1402           {
1403             rtx inner = XEXP (x, 0);
1404             int offset = 0;
1405             enum machine_mode inner_mode;
1406
1407             /* We might see SUBREGs with vector mode registers inside.  */
1408             if (GET_CODE (inner) == SUBREG
1409                 && (GET_MODE_SIZE (GET_MODE (inner))
1410                     == GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner))))
1411                 && subreg_lowpart_p (inner))
1412               inner = SUBREG_REG (inner);
1413             if (CONST_INT_P (inner))
1414               {
1415                 x = GEN_INT (trunc_int_for_mode (INTVAL (inner), GET_MODE (x)));
1416                 goto default_output;
1417               }
1418             inner_mode = GET_MODE (inner);
1419             if (GET_CODE (inner) == SUBREG
1420                 && (GET_MODE_SIZE (GET_MODE (inner))
1421                     < GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner))))
1422                 && REG_P (SUBREG_REG (inner)))
1423               {
1424                 offset = subreg_regno_offset (REGNO (SUBREG_REG (inner)),
1425                                               GET_MODE (SUBREG_REG (inner)),
1426                                               SUBREG_BYTE (inner),
1427                                               GET_MODE (inner));
1428                 inner = SUBREG_REG (inner);
1429               }
1430             if (!REG_P (inner) || GET_MODE_SIZE (inner_mode) > 8)
1431               abort ();
1432             /* Floating point register pairs are always big endian;
1433                general purpose registers are 64 bit wide.  */
1434             regno = REGNO (inner);
1435             regno = (HARD_REGNO_NREGS (regno, inner_mode)
1436                      - HARD_REGNO_NREGS (regno, mode))
1437                      + offset;
1438             x = inner;
1439             goto reg;
1440           }
1441         case SIGN_EXTEND:
1442           x = XEXP (x, 0);
1443           goto reg;
1444           /* FIXME: We need this on SHmedia32 because reload generates
1445              some sign-extended HI or QI loads into DImode registers
1446              but, because Pmode is SImode, the address ends up with a
1447              subreg:SI of the DImode register.  Maybe reload should be
1448              fixed so as to apply alter_subreg to such loads?  */
1449         case IF_THEN_ELSE:
1450           gcc_assert (trapping_target_operand (x, VOIDmode));
1451           x = XEXP (XEXP (x, 2), 0);
1452           goto default_output;
1453         case SUBREG:
1454           gcc_assert (SUBREG_BYTE (x) == 0
1455                       && REG_P (SUBREG_REG (x)));
1456
1457           x = SUBREG_REG (x);
1458           /* Fall through.  */
1459
1460         reg:
1461         case REG:
1462           regno += REGNO (x);
1463           if (FP_REGISTER_P (regno)
1464               && mode == V16SFmode)
1465             fprintf ((stream), "mtrx%s", reg_names[regno] + 2);
1466           else if (FP_REGISTER_P (REGNO (x))
1467                    && mode == V4SFmode)
1468             fprintf ((stream), "fv%s", reg_names[regno] + 2);
1469           else if (REG_P (x)
1470                    && mode == V2SFmode)
1471             fprintf ((stream), "fp%s", reg_names[regno] + 2);
1472           else if (FP_REGISTER_P (REGNO (x))
1473                    && GET_MODE_SIZE (mode) > 4)
1474             fprintf ((stream), "d%s", reg_names[regno] + 1);
1475           else
1476             fputs (reg_names[regno], (stream));
1477           break;
1478
1479         case MEM:
1480           output_address (XEXP (x, 0));
1481           break;
1482
1483         default:
1484           if (TARGET_SH1)
1485             fputc ('#', stream);
1486           output_addr_const (stream, x);
1487           break;
1488         }
1489       break;
1490     }
1491 }
1492
1493 static bool
1494 sh_print_operand_punct_valid_p (unsigned char code)
1495 {
1496   return (code == '.' || code == '#' || code == '@' || code == ','
1497           || code == '$' || code == '\'' || code == '>');
1498 }
1499
1500 /* Implement TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA.  */
1501 static bool
1502 sh_asm_output_addr_const_extra (FILE *file, rtx x)
1503 {
1504   if (GET_CODE (x) == UNSPEC)
1505     {
1506       switch (XINT (x, 1))
1507         {
1508         case UNSPEC_DATALABEL:
1509           fputs ("datalabel ", file);
1510           output_addr_const (file, XVECEXP (x, 0, 0));
1511           break;
1512         case UNSPEC_PIC:
1513           /* GLOBAL_OFFSET_TABLE or local symbols, no suffix.  */
1514           output_addr_const (file, XVECEXP (x, 0, 0));
1515           break;
1516         case UNSPEC_GOT:
1517           output_addr_const (file, XVECEXP (x, 0, 0));
1518           fputs ("@GOT", file);
1519           break;
1520         case UNSPEC_GOTOFF:
1521           output_addr_const (file, XVECEXP (x, 0, 0));
1522           fputs ("@GOTOFF", file);
1523           break;
1524         case UNSPEC_PLT:
1525           output_addr_const (file, XVECEXP (x, 0, 0));
1526           fputs ("@PLT", file);
1527           break;
1528         case UNSPEC_GOTPLT:
1529           output_addr_const (file, XVECEXP (x, 0, 0));
1530           fputs ("@GOTPLT", file);
1531           break;
1532         case UNSPEC_DTPOFF:
1533           output_addr_const (file, XVECEXP (x, 0, 0));
1534           fputs ("@DTPOFF", file);
1535           break;
1536         case UNSPEC_GOTTPOFF:
1537           output_addr_const (file, XVECEXP (x, 0, 0));
1538           fputs ("@GOTTPOFF", file);
1539           break;
1540         case UNSPEC_TPOFF:
1541           output_addr_const (file, XVECEXP (x, 0, 0));
1542           fputs ("@TPOFF", file);
1543           break;
1544         case UNSPEC_CALLER:
1545           {
1546             char name[32];
1547             /* LPCS stands for Label for PIC Call Site.  */
1548             targetm.asm_out.generate_internal_label (name, "LPCS",
1549                                                      INTVAL (XVECEXP (x, 0, 0)));
1550             assemble_name (file, name);
1551           }
1552           break;
1553         case UNSPEC_EXTRACT_S16:
1554         case UNSPEC_EXTRACT_U16:
1555           {
1556             rtx val, shift;
1557
1558             val = XVECEXP (x, 0, 0);
1559             shift = XVECEXP (x, 0, 1);
1560             fputc ('(', file);
1561             if (shift != const0_rtx)
1562                 fputc ('(', file);
1563             if (GET_CODE (val) == CONST
1564                 || GET_RTX_CLASS (GET_CODE (val)) != RTX_OBJ)
1565               {
1566                 fputc ('(', file);
1567                 output_addr_const (file, val);
1568                 fputc (')', file);
1569               }
1570             else
1571               output_addr_const (file, val);
1572             if (shift != const0_rtx)
1573               {
1574                 fputs (" >> ", file);
1575                 output_addr_const (file, shift);
1576                 fputc (')', file);
1577               }
1578             fputs (" & 65535)", file);
1579           }
1580           break;
1581         case UNSPEC_SYMOFF:
1582           output_addr_const (file, XVECEXP (x, 0, 0));
1583           fputc ('-', file);
1584           if (GET_CODE (XVECEXP (x, 0, 1)) == CONST)
1585             {
1586               fputc ('(', file);
1587               output_addr_const (file, XVECEXP (x, 0, 1));
1588               fputc (')', file);
1589             }
1590           else
1591             output_addr_const (file, XVECEXP (x, 0, 1));
1592           break;
1593         case UNSPEC_PCREL_SYMOFF:
1594           output_addr_const (file, XVECEXP (x, 0, 0));
1595           fputs ("-(", file);
1596           output_addr_const (file, XVECEXP (x, 0, 1));
1597           fputs ("-.)", file);
1598           break;
1599         default:
1600           return false;
1601         }
1602       return true;
1603     }
1604   else
1605     return false;
1606 }
1607 \f
1608 /* Encode symbol attributes of a SYMBOL_REF into its
1609    SYMBOL_REF_FLAGS.  */
1610 static void
1611 sh_encode_section_info (tree decl, rtx rtl, int first)
1612 {
1613   default_encode_section_info (decl, rtl, first);
1614
1615   if (TREE_CODE (decl) == FUNCTION_DECL
1616       && sh2a_function_vector_p (decl) && TARGET_SH2A)
1617     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_FUNCVEC_FUNCTION;
1618 }
1619
1620 /* Prepare operands for a move define_expand; specifically, one of the
1621    operands must be in a register.  */
1622 void
1623 prepare_move_operands (rtx operands[], enum machine_mode mode)
1624 {
1625   if ((mode == SImode || mode == DImode)
1626       && flag_pic
1627       && ! ((mode == Pmode || mode == ptr_mode)
1628             && tls_symbolic_operand (operands[1], Pmode) != TLS_MODEL_NONE))
1629     {
1630       rtx temp;
1631       if (SYMBOLIC_CONST_P (operands[1]))
1632         {
1633           if (MEM_P (operands[0]))
1634             operands[1] = force_reg (Pmode, operands[1]);
1635           else if (TARGET_SHMEDIA
1636                    && GET_CODE (operands[1]) == LABEL_REF
1637                    && target_reg_operand (operands[0], mode))
1638             /* It's ok.  */;
1639           else
1640             {
1641               temp = (!can_create_pseudo_p ()
1642                       ? operands[0]
1643                       : gen_reg_rtx (Pmode));
1644               operands[1] = legitimize_pic_address (operands[1], mode, temp);
1645             }
1646         }
1647       else if (GET_CODE (operands[1]) == CONST
1648                && GET_CODE (XEXP (operands[1], 0)) == PLUS
1649                && SYMBOLIC_CONST_P (XEXP (XEXP (operands[1], 0), 0)))
1650         {
1651           temp = !can_create_pseudo_p () ? operands[0] : gen_reg_rtx (Pmode);
1652           temp = legitimize_pic_address (XEXP (XEXP (operands[1], 0), 0),
1653                                          mode, temp);
1654           operands[1] = expand_binop (mode, add_optab, temp,
1655                                       XEXP (XEXP (operands[1], 0), 1),
1656                                       (!can_create_pseudo_p ()
1657                                        ? temp
1658                                        : gen_reg_rtx (Pmode)),
1659                                       0, OPTAB_LIB_WIDEN);
1660         }
1661     }
1662
1663   if (! reload_in_progress && ! reload_completed)
1664     {
1665       /* Copy the source to a register if both operands aren't registers.  */
1666       if (! register_operand (operands[0], mode)
1667           && ! sh_register_operand (operands[1], mode))
1668         operands[1] = copy_to_mode_reg (mode, operands[1]);
1669
1670       if (MEM_P (operands[0]) && ! memory_operand (operands[0], mode))
1671         {
1672           /* This is like change_address_1 (operands[0], mode, 0, 1) ,
1673              except that we can't use that function because it is static.  */
1674           rtx new_rtx = change_address (operands[0], mode, 0);
1675           MEM_COPY_ATTRIBUTES (new_rtx, operands[0]);
1676           operands[0] = new_rtx;
1677         }
1678
1679       /* This case can happen while generating code to move the result
1680          of a library call to the target.  Reject `st r0,@(rX,rY)' because
1681          reload will fail to find a spill register for rX, since r0 is already
1682          being used for the source.  */
1683       else if (TARGET_SH1
1684                && refers_to_regno_p (R0_REG, R0_REG + 1, operands[1], (rtx *)0)
1685                && MEM_P (operands[0])
1686                && GET_CODE (XEXP (operands[0], 0)) == PLUS
1687                && REG_P (XEXP (XEXP (operands[0], 0), 1)))
1688         operands[1] = copy_to_mode_reg (mode, operands[1]);
1689     }
1690
1691   if (mode == Pmode || mode == ptr_mode)
1692     {
1693       rtx op0, op1, opc;
1694       enum tls_model tls_kind;
1695
1696       op0 = operands[0];
1697       op1 = operands[1];
1698       if (GET_CODE (op1) == CONST
1699           && GET_CODE (XEXP (op1, 0)) == PLUS
1700           && (tls_symbolic_operand (XEXP (XEXP (op1, 0), 0), Pmode)
1701               != TLS_MODEL_NONE))
1702         {
1703           opc = XEXP (XEXP (op1, 0), 1);
1704           op1 = XEXP (XEXP (op1, 0), 0);
1705         }
1706       else
1707         opc = NULL_RTX;
1708
1709       if ((tls_kind = tls_symbolic_operand (op1, Pmode)) != TLS_MODEL_NONE)
1710         {
1711           rtx tga_op1, tga_ret, tmp, tmp2;
1712
1713           if (! flag_pic
1714               && (tls_kind == TLS_MODEL_GLOBAL_DYNAMIC
1715                   || tls_kind == TLS_MODEL_LOCAL_DYNAMIC
1716                   || tls_kind == TLS_MODEL_INITIAL_EXEC))
1717             {
1718               /* Don't schedule insns for getting GOT address when
1719                  the first scheduling is enabled, to avoid spill
1720                  failures for R0.  */
1721               if (flag_schedule_insns)
1722                 emit_insn (gen_blockage ());
1723               emit_insn (gen_GOTaddr2picreg ());
1724               emit_use (gen_rtx_REG (SImode, PIC_REG));
1725               if (flag_schedule_insns)
1726                 emit_insn (gen_blockage ());
1727         }
1728
1729           switch (tls_kind)
1730             {
1731             case TLS_MODEL_GLOBAL_DYNAMIC:
1732               tga_ret = gen_rtx_REG (Pmode, R0_REG);
1733               emit_call_insn (gen_tls_global_dynamic (tga_ret, op1));
1734               tmp = gen_reg_rtx (Pmode);
1735               emit_move_insn (tmp, tga_ret);
1736               op1 = tmp;
1737               break;
1738
1739             case TLS_MODEL_LOCAL_DYNAMIC:
1740               tga_ret = gen_rtx_REG (Pmode, R0_REG);
1741               emit_call_insn (gen_tls_local_dynamic (tga_ret, op1));
1742
1743               tmp = gen_reg_rtx (Pmode);
1744               emit_move_insn (tmp, tga_ret);
1745
1746               if (register_operand (op0, Pmode))
1747                 tmp2 = op0;
1748               else
1749                 tmp2 = gen_reg_rtx (Pmode);
1750
1751               emit_insn (gen_symDTPOFF2reg (tmp2, op1, tmp));
1752               op1 = tmp2;
1753               break;
1754
1755             case TLS_MODEL_INITIAL_EXEC:
1756               tga_op1 = !can_create_pseudo_p () ? op0 : gen_reg_rtx (Pmode);
1757               tmp = gen_sym2GOTTPOFF (op1);
1758               emit_insn (gen_tls_initial_exec (tga_op1, tmp));
1759               op1 = tga_op1;
1760               break;
1761
1762             case TLS_MODEL_LOCAL_EXEC:
1763               tmp2 = gen_reg_rtx (Pmode);
1764               emit_insn (gen_store_gbr (tmp2));
1765               tmp = gen_reg_rtx (Pmode);
1766               emit_insn (gen_symTPOFF2reg (tmp, op1));
1767
1768               if (register_operand (op0, Pmode))
1769                 op1 = op0;
1770               else
1771                 op1 = gen_reg_rtx (Pmode);
1772
1773               emit_insn (gen_addsi3 (op1, tmp, tmp2));
1774               break;
1775
1776             default:
1777               gcc_unreachable ();
1778             }
1779           if (opc)
1780             emit_insn (gen_addsi3 (op1, op1, force_reg (SImode, opc)));
1781           operands[1] = op1;
1782         }
1783     }
1784 }
1785
1786 /* Implement the canonicalize_comparison target hook for the combine
1787    pass.  For the target hook this function is invoked via
1788    sh_canonicalize_comparison.  This function is also re-used to
1789    canonicalize comparisons in cbranch pattern expanders.  */
1790 static void
1791 sh_canonicalize_comparison (enum rtx_code& cmp, rtx& op0, rtx& op1,
1792                             enum machine_mode mode,
1793                             bool op0_preserve_value)
1794 {
1795   /* When invoked from within the combine pass the mode is not specified,
1796      so try to get it from one of the operands.  */
1797   if (mode == VOIDmode)
1798     mode = GET_MODE (op0);
1799   if (mode == VOIDmode)
1800     mode = GET_MODE (op1);
1801
1802   // We need to have a mode to do something useful here.
1803   if (mode == VOIDmode)
1804     return;
1805
1806   // Currently, we don't deal with floats here.
1807   if (GET_MODE_CLASS (mode) == MODE_FLOAT)
1808     return;
1809
1810   // Make sure that the constant operand is the second operand.
1811   if (CONST_INT_P (op0) && !CONST_INT_P (op1))
1812     {
1813       if (op0_preserve_value)
1814         return;
1815
1816       std::swap (op0, op1);
1817       cmp = swap_condition (cmp);
1818     }
1819
1820   if (CONST_INT_P (op1))
1821     {
1822       /* Try to adjust the constant operand in such a way that available
1823          comparison insns can be utilized better and the constant can be
1824          loaded with a 'mov #imm,Rm' insn.  This avoids a load from the
1825          constant pool.  */
1826       const HOST_WIDE_INT val = INTVAL (op1);
1827
1828       /* x > -1           --> x >= 0
1829          x > 0xFFFFFF7F   --> x >= 0xFFFFFF80
1830          x <= -1          --> x < 0
1831          x <= 0xFFFFFF7F  --> x < 0xFFFFFF80  */
1832       if ((val == -1 || val == -0x81) && (cmp == GT || cmp == LE))
1833         {
1834           cmp = cmp == GT ? GE : LT;
1835           op1 = gen_int_mode (val + 1, mode);
1836         }
1837
1838       /* x >= 1     --> x > 0
1839          x >= 0x80  --> x > 0x7F
1840          x < 1      --> x <= 0
1841          x < 0x80   --> x <= 0x7F  */
1842       else if ((val == 1 || val == 0x80) && (cmp == GE || cmp == LT))
1843         {
1844           cmp = cmp == GE ? GT : LE;
1845           op1 = gen_int_mode (val - 1, mode);
1846         }
1847
1848       /* unsigned x >= 1  --> x != 0
1849          unsigned x < 1   --> x == 0  */
1850       else if (val == 1 && (cmp == GEU || cmp == LTU))
1851         {
1852           cmp = cmp == GEU ? NE : EQ;
1853           op1 = CONST0_RTX (mode);
1854         }
1855
1856       /* unsigned x >= 0x80  --> unsigned x > 0x7F
1857          unsigned x < 0x80   --> unsigned x < 0x7F  */
1858       else if (val == 0x80 && (cmp == GEU || cmp == LTU))
1859         {
1860           cmp = cmp == GEU ? GTU : LEU;
1861           op1 = gen_int_mode (val - 1, mode);
1862         }
1863
1864       /* unsigned x > 0   --> x != 0
1865          unsigned x <= 0  --> x == 0  */
1866       else if (val == 0 && (cmp == GTU || cmp == LEU))
1867         cmp = cmp == GTU ? NE : EQ;
1868
1869       /* unsigned x > 0x7FFFFFFF   --> signed x < 0
1870          unsigned x <= 0x7FFFFFFF  --> signed x >= 0  */
1871       else if (mode == SImode && (cmp == GTU || cmp == LEU)
1872                && val == 0x7FFFFFFF)
1873         {
1874           cmp = cmp == GTU ? LT : GE;
1875           op1 = const0_rtx;
1876         }
1877
1878       /* unsigned x >= 0x80000000  --> signed x < 0
1879          unsigned x < 0x80000000   --> signed x >= 0  */
1880       else if (mode == SImode && (cmp == GEU || cmp == LTU)
1881                && (unsigned HOST_WIDE_INT)val
1882                    == ((unsigned HOST_WIDE_INT)0x7FFFFFFF + 1))
1883         {
1884           cmp = cmp == GEU ? LT : GE;
1885           op1 = const0_rtx;
1886         }
1887     }
1888 }
1889
1890 /* This function implements the canonicalize_comparison target hook.
1891    This wrapper around the internally used sh_canonicalize_comparison
1892    function is needed to do the enum rtx_code <-> int conversion.
1893    Target hooks cannot use enum rtx_code in its definition.  */
1894 static void
1895 sh_canonicalize_comparison (int *code, rtx *op0, rtx *op1,
1896                             bool op0_preserve_value)
1897 {
1898   enum rtx_code tmp_code = (enum rtx_code)*code;
1899   sh_canonicalize_comparison (tmp_code, *op0, *op1,
1900                               VOIDmode, op0_preserve_value);
1901   *code = (int)tmp_code;
1902 }
1903
1904 bool
1905 sh_fixed_condition_code_regs (unsigned int* p1, unsigned int* p2)
1906 {
1907   *p1 = T_REG;
1908   *p2 = INVALID_REGNUM;
1909   return true;
1910 }
1911
1912 enum rtx_code
1913 prepare_cbranch_operands (rtx *operands, enum machine_mode mode,
1914                           enum rtx_code comparison)
1915 {
1916   /* The scratch reg is only available when this is invoked from within
1917      the cbranchdi4_i splitter, through expand_cbranchdi4.  */
1918   rtx scratch = NULL_RTX;
1919
1920   if (comparison == LAST_AND_UNUSED_RTX_CODE)
1921     comparison = GET_CODE (operands[0]);
1922   else
1923     scratch = operands[4];
1924
1925   sh_canonicalize_comparison (comparison, operands[1], operands[2],
1926                               mode, false);
1927
1928   /* Notice that this function is also invoked after reload by
1929      the cbranchdi4_i pattern, through expand_cbranchdi4.  */
1930   rtx op1 = operands[1];
1931
1932   if (can_create_pseudo_p ())
1933     operands[1] = force_reg (mode, op1);
1934   /* When we are handling DImode comparisons, we want to keep constants so
1935      that we can optimize the component comparisons; however, memory loads
1936      are better issued as a whole so that they can be scheduled well.
1937      SImode equality comparisons allow I08 constants, but only when they
1938      compare r0.  Hence, if operands[1] has to be loaded from somewhere else
1939      into a register, that register might as well be r0, and we allow the
1940      constant.  If it is already in a register, this is likely to be
1941      allocated to a different hard register, thus we load the constant into
1942      a register unless it is zero.  */
1943   if (!REG_P (operands[2])
1944       && (!CONST_INT_P (operands[2])
1945           || (mode == SImode && operands[2] != CONST0_RTX (SImode)
1946               && ((comparison != EQ && comparison != NE)
1947                   || (REG_P (op1) && REGNO (op1) != R0_REG)
1948                   || !satisfies_constraint_I08 (operands[2])))))
1949     {
1950       if (scratch && GET_MODE (scratch) == mode)
1951         {
1952           emit_move_insn (scratch, operands[2]);
1953           operands[2] = scratch;
1954         }
1955       else if (can_create_pseudo_p ())
1956         operands[2] = force_reg (mode, operands[2]);
1957     }
1958   return comparison;
1959 }
1960
1961 void
1962 expand_cbranchsi4 (rtx *operands, enum rtx_code comparison, int probability)
1963 {
1964   rtx (*branch_expander) (rtx) = gen_branch_true;
1965   comparison = prepare_cbranch_operands (operands, SImode, comparison);
1966   switch (comparison)
1967     {
1968     case NE: case LT: case LE: case LTU: case LEU:
1969       comparison = reverse_condition (comparison);
1970       branch_expander = gen_branch_false;
1971     default: ;
1972     }
1973   emit_insn (gen_rtx_SET (VOIDmode, get_t_reg_rtx (),
1974                           gen_rtx_fmt_ee (comparison, SImode,
1975                                           operands[1], operands[2])));
1976   rtx jump = emit_jump_insn (branch_expander (operands[3]));
1977   if (probability >= 0)
1978     add_int_reg_note (jump, REG_BR_PROB, probability);
1979 }
1980
1981 /* ??? How should we distribute probabilities when more than one branch
1982    is generated.  So far we only have some ad-hoc observations:
1983    - If the operands are random, they are likely to differ in both parts.
1984    - If comparing items in a hash chain, the operands are random or equal;
1985      operation should be EQ or NE.
1986    - If items are searched in an ordered tree from the root, we can expect
1987      the highpart to be unequal about half of the time; operation should be
1988      an inequality comparison, operands non-constant, and overall probability
1989      about 50%.  Likewise for quicksort.
1990    - Range checks will be often made against constants.  Even if we assume for
1991      simplicity an even distribution of the non-constant operand over a
1992      sub-range here, the same probability could be generated with differently
1993      wide sub-ranges - as long as the ratio of the part of the subrange that
1994      is before the threshold to the part that comes after the threshold stays
1995      the same.  Thus, we can't really tell anything here;
1996      assuming random distribution is at least simple.
1997  */
1998 bool
1999 expand_cbranchdi4 (rtx *operands, enum rtx_code comparison)
2000 {
2001   enum rtx_code msw_taken, msw_skip, lsw_taken;
2002   rtx skip_label = NULL_RTX;
2003   rtx op1h, op1l, op2h, op2l;
2004   int num_branches;
2005   int prob, rev_prob;
2006   int msw_taken_prob = -1, msw_skip_prob = -1, lsw_taken_prob = -1;
2007   rtx scratch = operands[4];
2008
2009   comparison = prepare_cbranch_operands (operands, DImode, comparison);
2010   op1h = gen_highpart_mode (SImode, DImode, operands[1]);
2011   op2h = gen_highpart_mode (SImode, DImode, operands[2]);
2012   op1l = gen_lowpart (SImode, operands[1]);
2013   op2l = gen_lowpart (SImode, operands[2]);
2014   msw_taken = msw_skip = lsw_taken = LAST_AND_UNUSED_RTX_CODE;
2015   prob = split_branch_probability;
2016   rev_prob = REG_BR_PROB_BASE - prob;
2017   switch (comparison)
2018     {
2019     /* ??? Should we use the cmpeqdi_t pattern for equality comparisons?
2020        That costs 1 cycle more when the first branch can be predicted taken,
2021        but saves us mispredicts because only one branch needs prediction.
2022        It also enables generating the cmpeqdi_t-1 pattern.  */
2023     case EQ:
2024       if (TARGET_CMPEQDI_T)
2025         {
2026           emit_insn (gen_cmpeqdi_t (operands[1], operands[2]));
2027           emit_jump_insn (gen_branch_true (operands[3]));
2028           return true;
2029         }
2030       msw_skip = NE;
2031       lsw_taken = EQ;
2032       if (prob >= 0)
2033         {
2034           // If we had more precision, we'd use rev_prob - (rev_prob >> 32) .
2035           msw_skip_prob = rev_prob;
2036           if (REG_BR_PROB_BASE <= 65535)
2037             lsw_taken_prob = prob ? REG_BR_PROB_BASE : 0;
2038           else
2039             {
2040               gcc_assert (HOST_BITS_PER_WIDEST_INT >= 64);
2041               lsw_taken_prob
2042                 = (prob
2043                    ? (REG_BR_PROB_BASE
2044                       - ((HOST_WIDEST_INT) REG_BR_PROB_BASE * rev_prob
2045                          / ((HOST_WIDEST_INT) prob << 32)))
2046                    : 0);
2047             }
2048         }
2049       break;
2050     case NE:
2051       if (TARGET_CMPEQDI_T)
2052         {
2053           emit_insn (gen_cmpeqdi_t (operands[1], operands[2]));
2054           emit_jump_insn (gen_branch_false (operands[3]));
2055           return true;
2056         }
2057       msw_taken = NE;
2058       msw_taken_prob = prob;
2059       lsw_taken = NE;
2060       lsw_taken_prob = 0;
2061       break;
2062     case GTU: case GT:
2063       msw_taken = comparison;
2064       if (CONST_INT_P (op2l) && INTVAL (op2l) == -1)
2065         break;
2066       if (comparison != GTU || op2h != CONST0_RTX (SImode))
2067         msw_skip = swap_condition (msw_taken);
2068       lsw_taken = GTU;
2069       break;
2070     case GEU: case GE:
2071       if (op2l == CONST0_RTX (SImode))
2072         msw_taken = comparison;
2073       else
2074         {
2075           msw_taken = comparison == GE ? GT : GTU;
2076           msw_skip = swap_condition (msw_taken);
2077           lsw_taken = GEU;
2078         }
2079       break;
2080     case LTU: case LT:
2081       msw_taken = comparison;
2082       if (op2l == CONST0_RTX (SImode))
2083         break;
2084       msw_skip = swap_condition (msw_taken);
2085       lsw_taken = LTU;
2086       break;
2087     case LEU: case LE:
2088       if (CONST_INT_P (op2l) && INTVAL (op2l) == -1)
2089         msw_taken = comparison;
2090       else
2091         {
2092           lsw_taken = LEU;
2093           if (comparison == LE)
2094             msw_taken = LT;
2095           else if (op2h != CONST0_RTX (SImode))
2096             msw_taken = LTU;
2097           else
2098             {
2099               msw_skip = swap_condition (LTU);
2100               break;
2101             }
2102           msw_skip = swap_condition (msw_taken);
2103         }
2104       break;
2105     default: return false;
2106     }
2107   num_branches = ((msw_taken != LAST_AND_UNUSED_RTX_CODE)
2108                   + (msw_skip != LAST_AND_UNUSED_RTX_CODE)
2109                   + (lsw_taken != LAST_AND_UNUSED_RTX_CODE));
2110   if (comparison != EQ && comparison != NE && num_branches > 1)
2111     {
2112       if (!CONSTANT_P (operands[2])
2113           && prob >= (int) (REG_BR_PROB_BASE * 3 / 8U)
2114           && prob <= (int) (REG_BR_PROB_BASE * 5 / 8U))
2115         {
2116           msw_taken_prob = prob / 2U;
2117           msw_skip_prob
2118             = REG_BR_PROB_BASE * rev_prob / (REG_BR_PROB_BASE + rev_prob);
2119           lsw_taken_prob = prob;
2120         }
2121       else
2122         {
2123           msw_taken_prob = prob;
2124           msw_skip_prob = REG_BR_PROB_BASE;
2125           /* ??? If we have a constant op2h, should we use that when
2126              calculating lsw_taken_prob?  */
2127           lsw_taken_prob = prob;
2128         }
2129     }
2130   operands[1] = op1h;
2131   operands[2] = op2h;
2132   operands[4] = NULL_RTX;
2133   if (reload_completed
2134       && ! arith_reg_or_0_operand (op2h, SImode)
2135       && (true_regnum (op1h) || (comparison != EQ && comparison != NE))
2136       && (msw_taken != LAST_AND_UNUSED_RTX_CODE
2137           || msw_skip != LAST_AND_UNUSED_RTX_CODE))
2138     {
2139       emit_move_insn (scratch, operands[2]);
2140       operands[2] = scratch;
2141     }
2142   if (msw_taken != LAST_AND_UNUSED_RTX_CODE)
2143     expand_cbranchsi4 (operands, msw_taken, msw_taken_prob);
2144   if (msw_skip != LAST_AND_UNUSED_RTX_CODE)
2145     {
2146       rtx taken_label = operands[3];
2147
2148       /* Operands were possibly modified, but msw_skip doesn't expect this.
2149          Always use the original ones.  */
2150       if (msw_taken != LAST_AND_UNUSED_RTX_CODE)
2151         {
2152           operands[1] = op1h;
2153           operands[2] = op2h;
2154           if (reload_completed
2155               && ! arith_reg_or_0_operand (op2h, SImode)
2156               && (true_regnum (op1h) || (comparison != EQ && comparison != NE)))
2157             {
2158               emit_move_insn (scratch, operands[2]);
2159               operands[2] = scratch;
2160             }
2161         }
2162
2163       operands[3] = skip_label = gen_label_rtx ();
2164       expand_cbranchsi4 (operands, msw_skip, msw_skip_prob);
2165       operands[3] = taken_label;
2166     }
2167   operands[1] = op1l;
2168   operands[2] = op2l;
2169   if (lsw_taken != LAST_AND_UNUSED_RTX_CODE)
2170     {
2171       if (reload_completed
2172           && ! arith_reg_or_0_operand (op2l, SImode)
2173           && (true_regnum (op1l) || (lsw_taken != EQ && lsw_taken != NE)))
2174         {
2175           emit_move_insn (scratch, operands[2]);
2176           operands[2] = scratch;
2177         }
2178       expand_cbranchsi4 (operands, lsw_taken, lsw_taken_prob);
2179     }
2180   if (msw_skip != LAST_AND_UNUSED_RTX_CODE)
2181     emit_label (skip_label);
2182   return true;
2183 }
2184
2185 /* Given an operand, return 1 if the evaluated operand plugged into an
2186    if_then_else will result in a branch_true, 0 if branch_false, or
2187    -1 if neither nor applies.  The truth table goes like this:
2188
2189        op   | cmpval |   code  | result
2190    ---------+--------+---------+--------------------
2191       T (0) |   0    |  EQ (1) |  0 = 0 ^ (0 == 1)
2192       T (0) |   1    |  EQ (1) |  1 = 0 ^ (1 == 1)
2193       T (0) |   0    |  NE (0) |  1 = 0 ^ (0 == 0)
2194       T (0) |   1    |  NE (0) |  0 = 0 ^ (1 == 0)
2195      !T (1) |   0    |  EQ (1) |  1 = 1 ^ (0 == 1)
2196      !T (1) |   1    |  EQ (1) |  0 = 1 ^ (1 == 1)
2197      !T (1) |   0    |  NE (0) |  0 = 1 ^ (0 == 0)
2198      !T (1) |   1    |  NE (0) |  1 = 1 ^ (1 == 0)  */
2199 int
2200 sh_eval_treg_value (rtx op)
2201 {
2202   enum rtx_code code = GET_CODE (op);
2203   if ((code != EQ && code != NE) || !CONST_INT_P (XEXP (op, 1)))
2204     return -1;
2205
2206   int cmpop = code == EQ ? 1 : 0;
2207   int cmpval = INTVAL (XEXP (op, 1));
2208   if (cmpval != 0 && cmpval != 1)
2209     return -1;
2210
2211   int t;
2212   if (t_reg_operand (XEXP (op, 0), GET_MODE (XEXP (op, 0))))
2213     t = 0;
2214   else if (negt_reg_operand (XEXP (op, 0), GET_MODE (XEXP (op, 0))))
2215     t = 1;
2216   else
2217     return -1;
2218   
2219   return t ^ (cmpval == cmpop);
2220 }
2221
2222 /* Emit INSN, possibly in a PARALLEL with an USE of fpscr for SH4.  */
2223
2224 static void
2225 sh_emit_set_t_insn (rtx insn, enum machine_mode mode)
2226 {
2227   if ((TARGET_SH4 || TARGET_SH2A) && GET_MODE_CLASS (mode) == MODE_FLOAT)
2228     {
2229       insn = gen_rtx_PARALLEL (VOIDmode,
2230                        gen_rtvec (2, insn,
2231                                   gen_rtx_USE (VOIDmode, get_fpscr_rtx ())));
2232       (mode == SFmode ? emit_sf_insn : emit_df_insn) (insn);
2233     }
2234   else
2235     emit_insn (insn);
2236 }
2237
2238 /* Prepare the operands for an scc instruction; make sure that the
2239    compare has been done and the result is in T_REG.  */
2240 void
2241 sh_emit_scc_to_t (enum rtx_code code, rtx op0, rtx op1)
2242 {
2243   rtx t_reg = get_t_reg_rtx ();
2244   enum rtx_code oldcode = code;
2245   enum machine_mode mode;
2246
2247   /* First need a compare insn.  */
2248   switch (code)
2249     {
2250     case NE:
2251       /* It isn't possible to handle this case.  */
2252       gcc_unreachable ();
2253     case LT:
2254       code = GT;
2255       break;
2256     case LE:
2257       code = GE;
2258       break;
2259     case LTU:
2260       code = GTU;
2261       break;
2262     case LEU:
2263       code = GEU;
2264       break;
2265     default:
2266       break;
2267     }
2268   if (code != oldcode)
2269     {
2270       rtx tmp = op0;
2271       op0 = op1;
2272       op1 = tmp;
2273     }
2274
2275   mode = GET_MODE (op0);
2276   if (mode == VOIDmode)
2277     mode = GET_MODE (op1);
2278
2279   op0 = force_reg (mode, op0);
2280   if ((code != EQ && code != NE
2281        && (op1 != const0_rtx
2282            || code == GTU  || code == GEU || code == LTU || code == LEU))
2283       || (mode == DImode && op1 != const0_rtx)
2284       || (TARGET_SH2E && GET_MODE_CLASS (mode) == MODE_FLOAT))
2285     op1 = force_reg (mode, op1);
2286
2287   sh_emit_set_t_insn (gen_rtx_SET (VOIDmode, t_reg,
2288                                    gen_rtx_fmt_ee (code, SImode, op0, op1)),
2289                       mode);
2290 }
2291
2292 rtx
2293 sh_emit_cheap_store_flag (enum machine_mode mode, enum rtx_code code,
2294                           rtx op0, rtx op1)
2295 {
2296   rtx target = gen_reg_rtx (SImode);
2297   rtx tmp;
2298
2299   gcc_assert (TARGET_SHMEDIA);
2300   switch (code)
2301     {
2302     case EQ:
2303     case GT:
2304     case LT:
2305     case UNORDERED:
2306     case GTU:
2307     case LTU:
2308       tmp = gen_rtx_fmt_ee (code, SImode, op0, op1);
2309       emit_insn (gen_cstore4_media (target, tmp, op0, op1));
2310       code = NE;
2311       break;
2312
2313     case NE:
2314     case GE:
2315     case LE:
2316     case ORDERED:
2317     case GEU:
2318     case LEU:
2319       tmp = gen_rtx_fmt_ee (reverse_condition (code), mode, op0, op1);
2320       emit_insn (gen_cstore4_media (target, tmp, op0, op1));
2321       code = EQ;
2322       break;
2323
2324     case UNEQ:
2325     case UNGE:
2326     case UNGT:
2327     case UNLE:
2328     case UNLT:
2329     case LTGT:
2330       return NULL_RTX;
2331
2332     default:
2333       gcc_unreachable ();
2334     }
2335
2336   if (mode == DImode)
2337     {
2338       rtx t2 = gen_reg_rtx (DImode);
2339       emit_insn (gen_extendsidi2 (t2, target));
2340       target = t2;
2341     }
2342
2343   return gen_rtx_fmt_ee (code, VOIDmode, target, const0_rtx);
2344 }
2345
2346 /* Called from the md file, set up the operands of a compare instruction.  */
2347 void
2348 sh_emit_compare_and_branch (rtx *operands, enum machine_mode mode)
2349 {
2350   enum rtx_code code = GET_CODE (operands[0]);
2351   enum rtx_code branch_code;
2352   rtx op0 = operands[1];
2353   rtx op1 = operands[2];
2354   rtx insn, tem;
2355   bool need_ccmpeq = false;
2356
2357   if (TARGET_SH2E && GET_MODE_CLASS (mode) == MODE_FLOAT)
2358     {
2359       op0 = force_reg (mode, op0);
2360       op1 = force_reg (mode, op1);
2361     }
2362   else
2363     {
2364       if (code != EQ || mode == DImode)
2365         {
2366           /* Force args into regs, since we can't use constants here.  */
2367           op0 = force_reg (mode, op0);
2368           if (op1 != const0_rtx || code == GTU  || code == GEU)
2369             op1 = force_reg (mode, op1);
2370         }
2371     }
2372
2373   if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2374     {
2375       if (code == LT
2376           || (code == LE && TARGET_IEEE && TARGET_SH2E)
2377           || (code == GE && !(TARGET_IEEE && TARGET_SH2E)))
2378         {
2379           tem = op0, op0 = op1, op1 = tem;
2380           code = swap_condition (code);
2381         }
2382
2383       /* GE becomes fcmp/gt+fcmp/eq, for SH2E and TARGET_IEEE only.  */
2384       if (code == GE)
2385         {
2386           gcc_assert (TARGET_IEEE && TARGET_SH2E);
2387           need_ccmpeq = true;
2388           code = GT;
2389         }
2390
2391       /* Now we can have EQ, NE, GT, LE.  NE and LE are then transformed
2392          to EQ/GT respectively.  */
2393       gcc_assert (code == EQ || code == GT || code == NE || code == LE);
2394     }
2395
2396   switch (code)
2397     {
2398     case EQ:
2399     case GT:
2400     case GE:
2401     case GTU:
2402     case GEU:
2403       branch_code = code;
2404       break;
2405     case NE:
2406     case LT:
2407     case LE:
2408     case LTU:
2409     case LEU:
2410       branch_code = reverse_condition (code);
2411       break;
2412     default:
2413       gcc_unreachable ();
2414     }
2415
2416   insn = gen_rtx_SET (VOIDmode,
2417                       get_t_reg_rtx (),
2418                       gen_rtx_fmt_ee (branch_code, SImode, op0, op1));
2419
2420   sh_emit_set_t_insn (insn, mode);
2421   if (need_ccmpeq)
2422     sh_emit_set_t_insn (gen_ieee_ccmpeqsf_t (op0, op1), mode);
2423
2424   if (branch_code == code)
2425     emit_jump_insn (gen_branch_true (operands[3]));
2426   else
2427     emit_jump_insn (gen_branch_false (operands[3]));
2428 }
2429
2430 void
2431 sh_emit_compare_and_set (rtx *operands, enum machine_mode mode)
2432 {
2433   enum rtx_code code = GET_CODE (operands[1]);
2434   rtx op0 = operands[2];
2435   rtx op1 = operands[3];
2436   rtx lab = NULL_RTX;
2437   bool invert = false;
2438   rtx tem;
2439
2440   op0 = force_reg (mode, op0);
2441   if ((code != EQ && code != NE
2442        && (op1 != const0_rtx
2443            || code == GTU  || code == GEU || code == LTU || code == LEU))
2444       || (mode == DImode && op1 != const0_rtx)
2445       || (TARGET_SH2E && GET_MODE_CLASS (mode) == MODE_FLOAT))
2446     op1 = force_reg (mode, op1);
2447
2448   if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2449     {
2450       if (code == LT || code == LE)
2451         {
2452           code = swap_condition (code);
2453           tem = op0, op0 = op1, op1 = tem;
2454         }
2455       if (code == GE)
2456         {
2457           if (TARGET_IEEE)
2458             {
2459               lab = gen_label_rtx ();
2460               sh_emit_scc_to_t (EQ, op0, op1);
2461               emit_jump_insn (gen_branch_true (lab));
2462               code = GT;
2463            }
2464           else
2465             {
2466               code = LT;
2467               invert = true;
2468             }
2469         }
2470     }
2471
2472   if (code == NE)
2473     {
2474       code = EQ;
2475       invert = true;
2476     }
2477
2478   sh_emit_scc_to_t (code, op0, op1);
2479   if (lab)
2480     emit_label (lab);
2481   if (invert)
2482     emit_insn (gen_movnegt (operands[0], get_t_reg_rtx ()));
2483   else
2484     emit_move_insn (operands[0], get_t_reg_rtx ());
2485 }
2486 \f
2487 /* Functions to output assembly code.  */
2488
2489 /* Return a sequence of instructions to perform DI or DF move.
2490
2491    Since the SH cannot move a DI or DF in one instruction, we have
2492    to take care when we see overlapping source and dest registers.  */
2493 const char *
2494 output_movedouble (rtx insn ATTRIBUTE_UNUSED, rtx operands[],
2495                    enum machine_mode mode)
2496 {
2497   rtx dst = operands[0];
2498   rtx src = operands[1];
2499
2500   if (MEM_P (dst)
2501       && GET_CODE (XEXP (dst, 0)) == PRE_DEC)
2502     return     "mov.l   %T1,%0" "\n"
2503            "    mov.l   %1,%0";
2504
2505   if (register_operand (dst, mode)
2506       && register_operand (src, mode))
2507     {
2508       if (REGNO (src) == MACH_REG)
2509         return         "sts     mach,%S0" "\n"
2510                "        sts     macl,%R0";
2511
2512       /* When mov.d r1,r2 do r2->r3 then r1->r2;
2513          when mov.d r1,r0 do r1->r0 then r2->r1.  */
2514       if (REGNO (src) + 1 == REGNO (dst))
2515         return         "mov     %T1,%T0" "\n"
2516                "        mov     %1,%0";
2517       else
2518         return         "mov     %1,%0" "\n"
2519                "        mov     %T1,%T0";
2520     }
2521   else if (CONST_INT_P (src))
2522     {
2523       if (INTVAL (src) < 0)
2524         output_asm_insn ("mov   #-1,%S0", operands);
2525       else
2526         output_asm_insn ("mov   #0,%S0", operands);
2527
2528       return "mov       %1,%R0";
2529     }
2530   else if (MEM_P (src))
2531     {
2532       int ptrreg = -1;
2533       int dreg = REGNO (dst);
2534       rtx inside = XEXP (src, 0);
2535
2536       switch (GET_CODE (inside))
2537         {
2538         case REG:
2539           ptrreg = REGNO (inside);
2540           break;
2541
2542         case SUBREG:
2543           ptrreg = subreg_regno (inside);
2544           break;
2545
2546         case PLUS:
2547           ptrreg = REGNO (XEXP (inside, 0));
2548           /* ??? A r0+REG address shouldn't be possible here, because it isn't
2549              an offsettable address.  Unfortunately, offsettable addresses use
2550              QImode to check the offset, and a QImode offsettable address
2551              requires r0 for the other operand, which is not currently
2552              supported, so we can't use the 'o' constraint.
2553              Thus we must check for and handle r0+REG addresses here.
2554              We punt for now, since this is likely very rare.  */
2555           gcc_assert (!REG_P (XEXP (inside, 1)));
2556           break;
2557           
2558         case LABEL_REF:
2559           return       "mov.l   %1,%0" "\n"
2560                  "      mov.l   %1+4,%T0";
2561         case POST_INC:
2562           return       "mov.l   %1,%0" "\n"
2563                  "      mov.l   %1,%T0";
2564         default:
2565           gcc_unreachable ();
2566         }
2567
2568       /* Work out the safe way to copy.  Copy into the second half first.  */
2569       if (dreg == ptrreg)
2570         return         "mov.l   %T1,%T0" "\n"
2571                "        mov.l   %1,%0";
2572     }
2573
2574   return       "mov.l   %1,%0" "\n"
2575          "      mov.l   %T1,%T0";
2576 }
2577
2578 /* Print an instruction which would have gone into a delay slot after
2579    another instruction, but couldn't because the other instruction expanded
2580    into a sequence where putting the slot insn at the end wouldn't work.  */
2581 static void
2582 print_slot (rtx insn)
2583 {
2584   final_scan_insn (XVECEXP (insn, 0, 1), asm_out_file, optimize, 1, NULL);
2585
2586   INSN_DELETED_P (XVECEXP (insn, 0, 1)) = 1;
2587 }
2588
2589 const char *
2590 output_far_jump (rtx insn, rtx op)
2591 {
2592   struct { rtx lab, reg, op; } this_jmp;
2593   rtx braf_base_lab = NULL_RTX;
2594   const char *jump;
2595   int far;
2596   int offset = branch_dest (insn) - INSN_ADDRESSES (INSN_UID (insn));
2597   rtx prev;
2598
2599   this_jmp.lab = gen_label_rtx ();
2600
2601   if (TARGET_SH2
2602       && offset >= -32764
2603       && offset - get_attr_length (insn) <= 32766)
2604     {
2605       far = 0;
2606       jump =   "mov.w   %O0,%1" "\n"
2607              "  braf    %1";
2608     }
2609   else
2610     {
2611       far = 1;
2612       if (flag_pic)
2613         {
2614           if (TARGET_SH2)
2615             jump =     "mov.l   %O0,%1" "\n"
2616                    "    braf    %1";
2617           else
2618             jump =     "mov.l   r0,@-r15"       "\n"
2619                    "    mova    %O0,r0"         "\n"
2620                    "    mov.l   @r0,%1"         "\n"
2621                    "    add     r0,%1"          "\n"
2622                    "    mov.l   @r15+,r0"       "\n"
2623                    "    jmp     @%1";
2624         }
2625       else
2626         jump =         "mov.l   %O0,%1" "\n"
2627                "        jmp     @%1";
2628     }
2629   /* If we have a scratch register available, use it.  */
2630   if (NONJUMP_INSN_P ((prev = prev_nonnote_insn (insn)))
2631       && INSN_CODE (prev) == CODE_FOR_indirect_jump_scratch)
2632     {
2633       this_jmp.reg = SET_DEST (XVECEXP (PATTERN (prev), 0, 0));
2634       if (REGNO (this_jmp.reg) == R0_REG && flag_pic && ! TARGET_SH2)
2635         jump =         "mov.l   r1,@-r15"       "\n"
2636                "        mova    %O0,r0"         "\n"
2637                "        mov.l   @r0,r1"         "\n"
2638                "        add     r1,r0"          "\n"
2639                "        mov.l   @r15+,r1"       "\n"
2640                "        jmp     @%1";
2641       output_asm_insn (jump, &this_jmp.lab);
2642       if (dbr_sequence_length ())
2643         print_slot (final_sequence);
2644       else
2645         output_asm_insn ("nop", 0);
2646     }
2647   else
2648     {
2649       /* Output the delay slot insn first if any.  */
2650       if (dbr_sequence_length ())
2651         print_slot (final_sequence);
2652
2653       this_jmp.reg = gen_rtx_REG (SImode, 13);
2654       /* We must keep the stack aligned to 8-byte boundaries on SH5.
2655          Fortunately, MACL is fixed and call-clobbered, and we never
2656          need its value across jumps, so save r13 in it instead of in
2657          the stack.  */
2658       if (TARGET_SH5)
2659         output_asm_insn ("lds   r13,macl", 0);
2660       else
2661         output_asm_insn ("mov.l r13,@-r15", 0);
2662       output_asm_insn (jump, &this_jmp.lab);
2663       if (TARGET_SH5)
2664         output_asm_insn ("sts   macl,r13", 0);
2665       else
2666         output_asm_insn ("mov.l @r15+,r13", 0);
2667     }
2668   if (far && flag_pic && TARGET_SH2)
2669     {
2670       braf_base_lab = gen_label_rtx ();
2671       (*targetm.asm_out.internal_label) (asm_out_file, "L",
2672                                  CODE_LABEL_NUMBER (braf_base_lab));
2673     }
2674   if (far)
2675     output_asm_insn (".align    2", 0);
2676   (*targetm.asm_out.internal_label) (asm_out_file, "L", CODE_LABEL_NUMBER (this_jmp.lab));
2677   this_jmp.op = op;
2678   if (far && flag_pic)
2679     {
2680       if (TARGET_SH2)
2681         this_jmp.lab = braf_base_lab;
2682       output_asm_insn (".long   %O2-%O0", &this_jmp.lab);
2683     }
2684   else
2685     output_asm_insn (far ? ".long       %O2" : ".word %O2-%O0", &this_jmp.lab);
2686   return "";
2687 }
2688
2689 /* Local label counter, used for constants in the pool and inside
2690    pattern branches.  */
2691 static int lf = 100;
2692
2693 /* Output code for ordinary branches.  */
2694 const char *
2695 output_branch (int logic, rtx insn, rtx *operands)
2696 {
2697   switch (get_attr_length (insn))
2698     {
2699     case 6:
2700       /* This can happen if filling the delay slot has caused a forward
2701          branch to exceed its range (we could reverse it, but only
2702          when we know we won't overextend other branches; this should
2703          best be handled by relaxation).
2704          It can also happen when other condbranches hoist delay slot insn
2705          from their destination, thus leading to code size increase.
2706          But the branch will still be in the range -4092..+4098 bytes.  */
2707       if (! TARGET_RELAX)
2708         {
2709           int label = lf++;
2710           /* The call to print_slot will clobber the operands.  */
2711           rtx op0 = operands[0];
2712
2713           /* If the instruction in the delay slot is annulled (true), then
2714              there is no delay slot where we can put it now.  The only safe
2715              place for it is after the label.  final will do that by default.  */
2716
2717           if (final_sequence
2718               && ! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
2719               && get_attr_length (XVECEXP (final_sequence, 0, 1)))
2720             {
2721               asm_fprintf (asm_out_file, "\tb%s%ss\t%LLF%d\n", logic ? "f" : "t",
2722                            ASSEMBLER_DIALECT ? "/" : ".", label);
2723               print_slot (final_sequence);
2724             }
2725           else
2726             asm_fprintf (asm_out_file, "\tb%s\t%LLF%d\n", logic ? "f" : "t", label);
2727
2728           output_asm_insn ("bra\t%l0", &op0);
2729           fprintf (asm_out_file, "\tnop\n");
2730           (*targetm.asm_out.internal_label) (asm_out_file, "LF", label);
2731
2732           return "";
2733         }
2734       /* When relaxing, handle this like a short branch.  The linker
2735          will fix it up if it still doesn't fit after relaxation.  */
2736     case 2:
2737       return logic ? "bt%.\t%l0" : "bf%.\t%l0";
2738
2739       /* These are for SH2e, in which we have to account for the
2740          extra nop because of the hardware bug in annulled branches.  */
2741     case 8:
2742       if (! TARGET_RELAX)
2743         {
2744           int label = lf++;
2745
2746           gcc_assert (!final_sequence
2747                       || !(INSN_ANNULLED_BRANCH_P
2748                            (XVECEXP (final_sequence, 0, 0))));
2749           asm_fprintf (asm_out_file, "b%s%ss\t%LLF%d\n",
2750                        logic ? "f" : "t",
2751                        ASSEMBLER_DIALECT ? "/" : ".", label);
2752           fprintf (asm_out_file, "\tnop\n");
2753           output_asm_insn ("bra\t%l0", operands);
2754           fprintf (asm_out_file, "\tnop\n");
2755           (*targetm.asm_out.internal_label) (asm_out_file, "LF", label);
2756
2757           return "";
2758         }
2759       /* When relaxing, fall through.  */
2760     case 4:
2761       {
2762         char buffer[10];
2763
2764         sprintf (buffer, "b%s%ss\t%%l0",
2765                  logic ? "t" : "f",
2766                  ASSEMBLER_DIALECT ? "/" : ".");
2767         output_asm_insn (buffer, &operands[0]);
2768         return "nop";
2769       }
2770
2771     default:
2772       /* There should be no longer branches now - that would
2773          indicate that something has destroyed the branches set
2774          up in machine_dependent_reorg.  */
2775       gcc_unreachable ();
2776     }
2777 }
2778
2779 /* Output a code sequence for INSN using TEMPL with OPERANDS; but before,
2780    fill in operands 9 as a label to the successor insn.
2781    We try to use jump threading where possible.
2782    IF CODE matches the comparison in the IF_THEN_ELSE of a following jump,
2783    we assume the jump is taken.  I.e. EQ means follow jmp and bf, NE means
2784    follow jmp and bt, if the address is in range.  */
2785 const char *
2786 output_branchy_insn (enum rtx_code code, const char *templ,
2787                      rtx insn, rtx *operands)
2788 {
2789   rtx next_insn = NEXT_INSN (insn);
2790
2791   if (next_insn && JUMP_P (next_insn) && condjump_p (next_insn))
2792     {
2793       rtx src = SET_SRC (PATTERN (next_insn));
2794       if (GET_CODE (src) == IF_THEN_ELSE && GET_CODE (XEXP (src, 0)) != code)
2795         {
2796           /* Following branch not taken */
2797           operands[9] = gen_label_rtx ();
2798           emit_label_after (operands[9], next_insn);
2799           INSN_ADDRESSES_NEW (operands[9],
2800                               INSN_ADDRESSES (INSN_UID (next_insn))
2801                               + get_attr_length (next_insn));
2802           return templ;
2803         }
2804       else
2805         {
2806           int offset = (branch_dest (next_insn)
2807                         - INSN_ADDRESSES (INSN_UID (next_insn)) + 4);
2808           if (offset >= -252 && offset <= 258)
2809             {
2810               if (GET_CODE (src) == IF_THEN_ELSE)
2811                 /* branch_true */
2812                 src = XEXP (src, 1);
2813               operands[9] = src;
2814               return templ;
2815             }
2816         }
2817     }
2818   operands[9] = gen_label_rtx ();
2819   emit_label_after (operands[9], insn);
2820   INSN_ADDRESSES_NEW (operands[9],
2821                       INSN_ADDRESSES (INSN_UID (insn))
2822                       + get_attr_length (insn));
2823   return templ;
2824 }
2825
2826 const char *
2827 output_ieee_ccmpeq (rtx insn, rtx *operands)
2828 {
2829   return output_branchy_insn (NE,      "bt      %l9" "\n"
2830                                   "     fcmp/eq %1,%0",
2831                               insn, operands);
2832 }
2833 \f
2834 /* Output the start of the assembler file.  */
2835 static void
2836 sh_file_start (void)
2837 {
2838   default_file_start ();
2839
2840   if (TARGET_ELF)
2841     /* We need to show the text section with the proper
2842        attributes as in TEXT_SECTION_ASM_OP, before dwarf2out
2843        emits it without attributes in TEXT_SECTION_ASM_OP, else GAS
2844        will complain.  We can teach GAS specifically about the
2845        default attributes for our choice of text section, but
2846        then we would have to change GAS again if/when we change
2847        the text section name.  */
2848     fprintf (asm_out_file, "%s\n", TEXT_SECTION_ASM_OP);
2849   else
2850     /* Switch to the data section so that the coffsem symbol
2851        isn't in the text section.  */
2852     switch_to_section (data_section);
2853
2854   if (TARGET_LITTLE_ENDIAN)
2855     fputs ("\t.little\n", asm_out_file);
2856
2857   if (!TARGET_ELF)
2858     {
2859       if (TARGET_SHCOMPACT)
2860         fputs ("\t.mode\tSHcompact\n", asm_out_file);
2861       else if (TARGET_SHMEDIA)
2862         fprintf (asm_out_file, "\t.mode\tSHmedia\n\t.abi\t%i\n",
2863                  TARGET_SHMEDIA64 ? 64 : 32);
2864     }
2865 }
2866 \f
2867 /* Check if PAT includes UNSPEC_CALLER unspec pattern.  */
2868 static bool
2869 unspec_caller_rtx_p (rtx pat)
2870 {
2871   rtx base, offset;
2872   int i;
2873
2874   split_const (pat, &base, &offset);
2875   if (GET_CODE (base) == UNSPEC)
2876     {
2877       if (XINT (base, 1) == UNSPEC_CALLER)
2878         return true;
2879       for (i = 0; i < XVECLEN (base, 0); i++)
2880         if (unspec_caller_rtx_p (XVECEXP (base, 0, i)))
2881           return true;
2882     }
2883   return false;
2884 }
2885
2886 /* Indicate that INSN cannot be duplicated.  This is true for insn
2887    that generates a unique label.  */
2888 static bool
2889 sh_cannot_copy_insn_p (rtx insn)
2890 {
2891   rtx pat;
2892
2893   if (!reload_completed || !flag_pic)
2894     return false;
2895
2896   if (!NONJUMP_INSN_P (insn))
2897     return false;
2898   if (asm_noperands (insn) >= 0)
2899     return false;
2900
2901   pat = PATTERN (insn);
2902   if (GET_CODE (pat) != SET)
2903     return false;
2904   pat = SET_SRC (pat);
2905
2906   if (unspec_caller_rtx_p (pat))
2907     return true;
2908
2909   return false;
2910 }
2911 \f
2912 /* Number of instructions used to make an arithmetic right shift by N.  */
2913 static const char ashiftrt_insns[] =
2914   { 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};
2915
2916 /* Description of a logical left or right shift, when expanded to a sequence
2917    of 1/2/8/16 shifts.
2918    Notice that one bit right shifts clobber the T bit.  One bit left shifts
2919    are done with an 'add Rn,Rm' insn and thus do not clobber the T bit.  */
2920 enum
2921 {
2922   ASHL_CLOBBERS_T = 1 << 0,
2923   LSHR_CLOBBERS_T = 1 << 1
2924 };
2925
2926 struct ashl_lshr_sequence
2927 {
2928   char insn_count;
2929   char amount[6];
2930   char clobbers_t;
2931 };
2932
2933 static const struct ashl_lshr_sequence ashl_lshr_seq[32] =
2934 {
2935   { 0, { 0 },               0 },                // 0
2936   { 1, { 1 },               LSHR_CLOBBERS_T },
2937   { 1, { 2 },               0 },
2938   { 2, { 2, 1 },            LSHR_CLOBBERS_T },
2939   { 2, { 2, 2 },            0 },                // 4
2940   { 3, { 2, 1, 2 },         LSHR_CLOBBERS_T },
2941   { 3, { 2, 2, 2 },         0 },
2942   { 4, { 2, 2, 1, 2 },      LSHR_CLOBBERS_T },
2943   { 1, { 8 },               0 },                // 8
2944   { 2, { 8, 1 },            LSHR_CLOBBERS_T },
2945   { 2, { 8, 2 },            0 },
2946   { 3, { 8, 1, 2 },         LSHR_CLOBBERS_T },
2947   { 3, { 8, 2, 2 },         0 },                // 12
2948   { 4, { 8, 2, 1, 2 },      LSHR_CLOBBERS_T },
2949   { 3, { 8, -2, 8 },        0 },
2950   { 3, { 8, -1, 8 },        ASHL_CLOBBERS_T },
2951   { 1, { 16 },              0 },                // 16
2952   { 2, { 16, 1 },           LSHR_CLOBBERS_T },
2953   { 2, { 16, 2 },           0 },
2954   { 3, { 16, 1, 2 },        LSHR_CLOBBERS_T },
2955   { 3, { 16, 2, 2 },        0 },                // 20
2956   { 4, { 16, 2, 1, 2 },     LSHR_CLOBBERS_T },
2957   { 3, { 16, -2, 8 },       0 },
2958   { 3, { 16, -1, 8 },       ASHL_CLOBBERS_T },
2959   { 2, { 16, 8 },           0 },                // 24
2960   { 3, { 16, 1, 8 },        LSHR_CLOBBERS_T },
2961   { 3, { 16, 8, 2 },        0 },
2962   { 4, { 16, 8, 1, 2 },     LSHR_CLOBBERS_T },
2963   { 4, { 16, 8, 2, 2 },     0 },                // 28
2964   { 4, { 16, -1, -2, 16 },  ASHL_CLOBBERS_T },
2965   { 3, { 16, -2, 16 },      0 },
2966
2967   /* For a right shift by 31 a 2 insn shll-movt sequence can be used.
2968      For a left shift by 31 a 2 insn and-rotl sequences can be used.
2969      However, the shift-and combiner code needs this entry here to be in
2970      terms of real shift insns.  */
2971   { 3, { 16, -1, 16 },      ASHL_CLOBBERS_T }
2972 };
2973
2974 /* Individual shift amounts for shift amounts < 16, up to three highmost
2975    bits might be clobbered.  This is typically used when combined with some
2976    kind of sign or zero extension.  */
2977 static const struct ashl_lshr_sequence ext_ashl_lshr_seq[32] =
2978 {
2979   { 0, { 0 },               0 },                // 0
2980   { 1, { 1 },               LSHR_CLOBBERS_T },
2981   { 1, { 2 },               0 },
2982   { 2, { 2, 1 },            LSHR_CLOBBERS_T },
2983   { 2, { 2, 2 },            0 },                // 4
2984   { 3, { 2, 1, 2 },         LSHR_CLOBBERS_T },
2985   { 2, { 8, -2 },           0 },
2986   { 2, { 8, -1 },           ASHL_CLOBBERS_T },
2987   { 1, { 8 },               0 },                // 8
2988   { 2, { 8, 1 },            LSHR_CLOBBERS_T },
2989   { 2, { 8, 2 },            0 },
2990   { 3, { 8, 1, 2 },         LSHR_CLOBBERS_T },
2991   { 3, { 8, 2, 2 },         0 },                // 12
2992   { 3, { 16, -2, -1 },      ASHL_CLOBBERS_T },
2993   { 2, { 16, -2 },          0 },
2994   { 2, { 16, -1 },          ASHL_CLOBBERS_T },
2995   { 1, { 16 },              0 },                // 16
2996   { 2, { 16, 1 },           LSHR_CLOBBERS_T },
2997   { 2, { 16, 2 },           0 },
2998   { 3, { 16, 1, 2 },        LSHR_CLOBBERS_T },
2999   { 3, { 16, 2, 2 },        0 },                // 20
3000   { 4, { 16, 2, 1, 2 },     LSHR_CLOBBERS_T },
3001   { 3, { 16, -2, 8 },       0 },
3002   { 3, { 16, -1, 8 },       ASHL_CLOBBERS_T },
3003   { 2, { 16, 8 },           0 },                // 24
3004   { 3, { 16, 1, 8 },        LSHR_CLOBBERS_T },
3005   { 3, { 16, 8, 2 },        0 },
3006   { 4, { 16, 8, 1, 2 },     LSHR_CLOBBERS_T },
3007   { 4, { 16, 8, 2, 2 },     0 },                // 28
3008   { 4, { 16, -1, -2, 16 },  ASHL_CLOBBERS_T },
3009   { 3, { 16, -2, 16 },      0 },
3010   { 3, { 16, -1, 16 },      ASHL_CLOBBERS_T }
3011 };
3012
3013 /* Return true if a shift left consisting of 1/2/8/16 shift instructions
3014    will clobber the T bit.  */
3015 bool
3016 sh_ashlsi_clobbers_t_reg_p (rtx shift_amount)
3017 {
3018   gcc_assert (CONST_INT_P (shift_amount));
3019   
3020   const int shift_amount_i = INTVAL (shift_amount) & 31;
3021
3022   /* Special case for shift count of 31: use and-rotl sequence.  */
3023   if (shift_amount_i == 31)
3024     return true;
3025
3026   return (ashl_lshr_seq[shift_amount_i].clobbers_t
3027           & ASHL_CLOBBERS_T) != 0;
3028 }
3029
3030 /* Return true if a logical right shift consisting of 1/2/8/16 shift
3031    instructions will clobber the T bit.  */
3032 bool
3033 sh_lshrsi_clobbers_t_reg_p (rtx shift_amount)
3034 {
3035   gcc_assert (CONST_INT_P (shift_amount));
3036
3037   const int shift_amount_i = INTVAL (shift_amount) & 31;
3038  
3039   /* Special case for shift count of 31: use shll-movt sequence.  */
3040   if (shift_amount_i == 31)
3041     return true;
3042
3043   return (ashl_lshr_seq[shift_amount_i].clobbers_t
3044           & LSHR_CLOBBERS_T) != 0;
3045 }
3046
3047 /* Return true if it is potentially beneficial to use a dynamic shift
3048    instruction (shad / shar) instead of a combination of 1/2/8/16 
3049    shift instructions for the specified shift count.
3050    If dynamic shifts are not available, always return false.  */
3051 bool
3052 sh_dynamicalize_shift_p (rtx count)
3053 {
3054   gcc_assert (CONST_INT_P (count));
3055
3056   const int shift_amount_i = INTVAL (count) & 31;
3057   int insn_count;
3058
3059   /* For left and right shifts, there are shorter 2 insn sequences for
3060      shift amounts of 31.  */
3061   if (shift_amount_i == 31)
3062     insn_count = 2;
3063   else
3064     insn_count = ashl_lshr_seq[shift_amount_i].insn_count;
3065
3066   return TARGET_DYNSHIFT && (insn_count > 1 + SH_DYNAMIC_SHIFT_COST);
3067 }
3068
3069 /* Assuming we have a value that has been sign-extended by at least one bit,
3070    can we use the ext_shift_amounts with the last shift turned to an
3071    arithmetic shift to shift it by N without data loss, and quicker than by
3072    other means?  */
3073 #define EXT_SHIFT_SIGNED(n) (((n) | 8) == 15)
3074
3075 /* Return the cost of a shift.  */
3076 static inline int
3077 shiftcosts (rtx x)
3078 {
3079   int value;
3080
3081   if (TARGET_SHMEDIA)
3082     return 1;
3083
3084   if (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
3085     {
3086       if (GET_MODE (x) == DImode
3087           && CONST_INT_P (XEXP (x, 1))
3088           && INTVAL (XEXP (x, 1)) == 1)
3089         return 2;
3090
3091       /* Everything else is invalid, because there is no pattern for it.  */
3092       return -1;
3093     }
3094   /* If shift by a non constant, then this will be expensive.  */
3095   if (!CONST_INT_P (XEXP (x, 1)))
3096     return SH_DYNAMIC_SHIFT_COST;
3097
3098   /* Otherwise, return the true cost in instructions.  Cope with out of range
3099      shift counts more or less arbitrarily.  */
3100   value = INTVAL (XEXP (x, 1)) & 31;
3101
3102   if (GET_CODE (x) == ASHIFTRT)
3103     {
3104       int cost = ashiftrt_insns[value];
3105       /* If dynamic shifts are available and profitable in this case, then we
3106          put the constant in a reg and use shad.  */
3107       if (cost > 1 + SH_DYNAMIC_SHIFT_COST)
3108         cost = 1 + SH_DYNAMIC_SHIFT_COST;
3109       return cost;
3110     }
3111   else
3112     return ashl_lshr_seq[value].insn_count;
3113 }
3114
3115 /* Return the cost of an AND/XOR/IOR operation.  */
3116 static inline int
3117 and_xor_ior_costs (rtx x, int code)
3118 {
3119   /* On SH1-4 we have only max. SImode operations.
3120      Double the cost for modes > SImode.  */
3121   const int cost_scale = !TARGET_SHMEDIA
3122                          && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD
3123                          ? 2 : 1;
3124
3125   /* A logical operation with two registers is a single cycle
3126      instruction.  */
3127   if (!CONST_INT_P (XEXP (x, 1)))
3128     return 1 * cost_scale;
3129
3130   int i = INTVAL (XEXP (x, 1));
3131
3132   if (TARGET_SHMEDIA)
3133     {
3134       if (satisfies_constraint_I10 (XEXP (x, 1))
3135           || satisfies_constraint_J16 (XEXP (x, 1)))
3136         return 1;
3137       else
3138         return 1 + rtx_cost (XEXP (x, 1), AND, 1, !optimize_size);
3139     }
3140
3141   /* These constants are single cycle extu.[bw] instructions.  */
3142   if ((i == 0xff || i == 0xffff) && code == AND)
3143     return 1 * cost_scale;
3144   /* Constants that can be used in an instruction as an immediate are
3145      a single cycle, but this requires r0, so make it a little more
3146      expensive.  */
3147   if (CONST_OK_FOR_K08 (i))
3148     return 2 * cost_scale;
3149   /* Constants that can be loaded with a mov immediate need one more cycle.
3150      This case is probably unnecessary.  */
3151   if (CONST_OK_FOR_I08 (i))
3152     return 2 * cost_scale;
3153   /* Any other constant requires an additional 2 cycle pc-relative load.
3154      This case is probably unnecessary.  */
3155   return 3 * cost_scale;
3156 }
3157
3158 /* Return the cost of an addition or a subtraction.  */
3159 static inline int
3160 addsubcosts (rtx x)
3161 {
3162   if (GET_MODE (x) == SImode)
3163     {
3164       /* The addc or subc patterns will eventually become one or two
3165          instructions.  Below are some costs for some of the patterns
3166          which combine would reject because the costs of the individual
3167          insns in the patterns are lower.
3168
3169          FIXME: It would be much easier if we had something like insn cost
3170          attributes and the cost calculation machinery used those attributes
3171          in the first place.  This would eliminate redundant recog-like C
3172          code to calculate costs of complex patterns.  */
3173       rtx op0 = XEXP (x, 0);
3174       rtx op1 = XEXP (x, 1);
3175
3176       if (GET_CODE (x) == PLUS)
3177         {
3178           if (GET_CODE (op0) == AND
3179               && XEXP (op0, 1) == const1_rtx
3180               && (GET_CODE (op1) == PLUS
3181                   || (GET_CODE (op1) == MULT && XEXP (op1, 1) == const2_rtx)))
3182             return 1;
3183
3184           if (GET_CODE (op0) == MULT && XEXP (op0, 1) == const2_rtx
3185               && GET_CODE (op1) == LSHIFTRT
3186               && CONST_INT_P (XEXP (op1, 1)) && INTVAL (XEXP (op1, 1)) == 31)
3187             return 1;
3188         }
3189     }
3190
3191   /* On SH1-4 we have only max. SImode operations.
3192      Double the cost for modes > SImode.  */
3193   const int cost_scale = !TARGET_SHMEDIA
3194                          && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD
3195                          ? 2 : 1;
3196
3197   /* Adding a register is a single cycle insn.  */
3198   if (REG_P (XEXP (x, 1))
3199       || GET_CODE (XEXP (x, 1)) == SUBREG)
3200     return 1 * cost_scale;
3201
3202   /* Likewise for small constants.  */
3203   if (CONST_INT_P (XEXP (x, 1))
3204       && CONST_OK_FOR_ADD (INTVAL (XEXP (x, 1))))
3205     return 1 * cost_scale;
3206
3207   if (TARGET_SHMEDIA)
3208     switch (GET_CODE (XEXP (x, 1)))
3209       {
3210       case CONST:
3211       case LABEL_REF:
3212       case SYMBOL_REF:
3213         return TARGET_SHMEDIA64 ? 5 : 3;
3214
3215       case CONST_INT:
3216         if (CONST_OK_FOR_I16 (INTVAL (XEXP (x, 1))))
3217           return 2;
3218         else if (CONST_OK_FOR_I16 (INTVAL (XEXP (x, 1)) >> 16))
3219           return 3;
3220         else if (CONST_OK_FOR_I16 ((INTVAL (XEXP (x, 1)) >> 16) >> 16))
3221           return 4;
3222
3223         /* Fall through.  */
3224       default:
3225         return 5;
3226       }
3227
3228   /* Any other constant requires a 2 cycle pc-relative load plus an
3229      addition.  */
3230   return 3 * cost_scale;
3231 }
3232
3233 /* Return the cost of a multiply.  */
3234 static inline int
3235 multcosts (rtx x ATTRIBUTE_UNUSED)
3236 {
3237   if (sh_multcost >= 0)
3238     return sh_multcost;
3239   if (TARGET_SHMEDIA)
3240     /* ??? We have a mul insn, but it has a latency of three, and doesn't
3241        accept constants.  Ideally, we would use a cost of one or two and
3242        add the cost of the operand, but disregard the latter when inside loops
3243        and loop invariant code motion is still to follow.
3244        Using a multiply first and splitting it later if it's a loss
3245        doesn't work because of different sign / zero extension semantics
3246        of multiplies vs. shifts.  */
3247     return optimize_size ? 2 : 3;
3248
3249   if (TARGET_SH2)
3250     {
3251       /* We have a mul insn, so we can never take more than the mul and the
3252          read of the mac reg, but count more because of the latency and extra
3253          reg usage.  */
3254       if (optimize_size)
3255         return 2;
3256       return 3;
3257     }
3258
3259   /* If we're aiming at small code, then just count the number of
3260      insns in a multiply call sequence.  */
3261   if (optimize_size)
3262     return 5;
3263
3264   /* Otherwise count all the insns in the routine we'd be calling too.  */
3265   return 20;
3266 }
3267
3268 /* Compute a (partial) cost for rtx X.  Return true if the complete
3269    cost has been computed, and false if subexpressions should be
3270    scanned.  In either case, *TOTAL contains the cost result.  */
3271 static bool
3272 sh_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
3273               int *total, bool speed ATTRIBUTE_UNUSED)
3274 {
3275   switch (code)
3276     {
3277       /* The lower-subreg pass decides whether to split multi-word regs
3278          into individual regs by looking at the cost for a SET of certain
3279          modes with the following patterns:
3280            (set (reg) (reg)) 
3281            (set (reg) (const_int 0))
3282          On machines that support vector-move operations a multi-word move
3283          is the same cost as individual reg move.  On SH there is no
3284          vector-move, so we have to provide the correct cost in the number
3285          of move insns to load/store the reg of the mode in question.  */
3286     case SET:
3287       if (register_operand (SET_DEST (x), VOIDmode)
3288             && (register_operand (SET_SRC (x), VOIDmode)
3289                 || satisfies_constraint_Z (SET_SRC (x))))
3290         {
3291           const enum machine_mode mode = GET_MODE (SET_DEST (x));
3292           *total = COSTS_N_INSNS (GET_MODE_SIZE (mode)
3293                                   / mov_insn_size (mode, TARGET_SH2A));
3294           return true;
3295         }
3296       return false;
3297
3298     /* The cost of a mem access is mainly the cost of the address mode.  */
3299     case MEM:
3300       *total = sh_address_cost (XEXP (x, 0), GET_MODE (x), MEM_ADDR_SPACE (x),
3301                                 true);
3302       return true;
3303
3304     /* The cost of a sign or zero extend depends on whether the source is a
3305        reg or a mem.  In case of a mem take the address into acount.  */
3306     case SIGN_EXTEND:
3307       if (REG_P (XEXP (x, 0)))
3308         {
3309           *total = COSTS_N_INSNS (1);
3310           return true;
3311         }
3312       if (MEM_P (XEXP (x, 0)))
3313         {
3314           *total = sh_address_cost (XEXP (XEXP (x, 0), 0),
3315                                     GET_MODE (XEXP (x, 0)),
3316                                     MEM_ADDR_SPACE (XEXP (x, 0)), true);
3317           return true;
3318         }
3319       return false;
3320
3321     case ZERO_EXTEND:
3322       if (REG_P (XEXP (x, 0)))
3323         {
3324           *total = COSTS_N_INSNS (1);
3325           return true;
3326         }
3327       else if (TARGET_SH2A && MEM_P (XEXP (x, 0))
3328                && (GET_MODE (XEXP (x, 0)) == QImode
3329                    || GET_MODE (XEXP (x, 0)) == HImode))
3330         {
3331           /* Handle SH2A's movu.b and movu.w insn.  */
3332           *total = sh_address_cost (XEXP (XEXP (x, 0), 0), 
3333                                     GET_MODE (XEXP (x, 0)), 
3334                                     MEM_ADDR_SPACE (XEXP (x, 0)), true);
3335           return true;
3336         }
3337       return false;
3338
3339     /* mems for SFmode and DFmode can be inside a parallel due to
3340        the way the fpscr is handled.  */
3341     case PARALLEL:
3342       for (int i = 0; i < XVECLEN (x, 0); i++)
3343         {
3344           rtx xx = XVECEXP (x, 0, i);
3345           if (GET_CODE (xx) == SET && MEM_P (XEXP (xx, 0)))
3346             {
3347               *total = sh_address_cost (XEXP (XEXP (xx, 0), 0), 
3348                                         GET_MODE (XEXP (xx, 0)),
3349                                         MEM_ADDR_SPACE (XEXP (xx, 0)), true);
3350               return true;
3351             }
3352           if (GET_CODE (xx) == SET && MEM_P (XEXP (xx, 1)))
3353             {
3354               *total = sh_address_cost (XEXP (XEXP (xx, 1), 0),
3355                                         GET_MODE (XEXP (xx, 1)),
3356                                         MEM_ADDR_SPACE (XEXP (xx, 1)), true);
3357               return true;
3358             }
3359         }
3360
3361       if (sh_1el_vec (x, VOIDmode))
3362         *total = outer_code != SET;
3363       else if (sh_rep_vec (x, VOIDmode))
3364         *total = ((GET_MODE_UNIT_SIZE (GET_MODE (x)) + 3) / 4
3365                   + (outer_code != SET));
3366       else
3367         *total = COSTS_N_INSNS (3) + (outer_code != SET);
3368       return true;
3369
3370     case CONST_INT:
3371       if (TARGET_SHMEDIA)
3372         {
3373           if (INTVAL (x) == 0)
3374             *total = 0;
3375           else if (outer_code == AND && and_operand ((x), DImode))
3376             *total = 0;
3377           else if ((outer_code == IOR || outer_code == XOR
3378                     || outer_code == PLUS)
3379                    && CONST_OK_FOR_I10 (INTVAL (x)))
3380             *total = 0;
3381           else if (CONST_OK_FOR_I16 (INTVAL (x)))
3382             *total = COSTS_N_INSNS (outer_code != SET);
3383           else if (CONST_OK_FOR_I16 (INTVAL (x) >> 16))
3384             *total = COSTS_N_INSNS ((outer_code != SET) + 1);
3385           else if (CONST_OK_FOR_I16 ((INTVAL (x) >> 16) >> 16))
3386             *total = COSTS_N_INSNS ((outer_code != SET) + 2);
3387           else
3388             *total = COSTS_N_INSNS ((outer_code != SET) + 3);
3389           return true;
3390         }
3391       if (CONST_OK_FOR_I08 (INTVAL (x)))
3392         *total = 0;
3393       else if ((outer_code == AND || outer_code == IOR || outer_code == XOR)
3394                && CONST_OK_FOR_K08 (INTVAL (x)))
3395         *total = 1;
3396       /* prepare_cmp_insn will force costly constants int registers before
3397          the cbranch[sd]i4 patterns can see them, so preserve potentially
3398          interesting ones not covered by I08 above.  */
3399       else if (outer_code == COMPARE
3400                && ((unsigned HOST_WIDE_INT) INTVAL (x)
3401                     == (unsigned HOST_WIDE_INT) 0x7fffffff + 1
3402                     || INTVAL (x) == 0x7fffffff
3403                    || INTVAL (x) == 0x80 || INTVAL (x) == -0x81))
3404         *total = 1;
3405       else
3406         *total = 8;
3407       return true;
3408
3409     case EQ:
3410       /* An and with a constant compared against zero is
3411          most likely going to be a TST #imm, R0 instruction.
3412          Notice that this does not catch the zero_extract variants from
3413          the md file.  */
3414       if (GET_CODE (XEXP (x, 0)) == AND
3415           && CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) == 0)
3416         {
3417           *total = 1;
3418           return true;
3419         }
3420       else
3421         return false;
3422
3423     case SMIN:
3424     case SMAX:
3425       /* This is most likely a clips.b or clips.w insn that is being made up
3426          by combine.  */
3427       if (TARGET_SH2A
3428           && (GET_CODE (XEXP (x, 0)) == SMAX || GET_CODE (XEXP (x, 0)) == SMIN)
3429           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
3430           && REG_P (XEXP (XEXP (x, 0), 0))
3431           && CONST_INT_P (XEXP (x, 1)))
3432         {
3433           *total = COSTS_N_INSNS (1);
3434           return true;
3435         }
3436       else
3437         return false;
3438
3439     case CONST:
3440     case LABEL_REF:
3441     case SYMBOL_REF:
3442       if (TARGET_SHMEDIA64)
3443         *total = COSTS_N_INSNS (4);
3444       else if (TARGET_SHMEDIA32)
3445         *total = COSTS_N_INSNS (2);
3446       else
3447         *total = 5;
3448       return true;
3449
3450     case CONST_DOUBLE:
3451       if (TARGET_SHMEDIA)
3452         *total = COSTS_N_INSNS (4);
3453       /* prepare_cmp_insn will force costly constants int registers before
3454          the cbranchdi4 pattern can see them, so preserve potentially
3455          interesting ones.  */
3456       else if (outer_code == COMPARE && GET_MODE (x) == DImode)
3457         *total = 1;
3458       else
3459         *total = 10;
3460       return true;
3461
3462     case CONST_VECTOR:
3463     /* FIXME: This looks broken.  Only the last statement has any effect.
3464        Probably this could be folded with the PARALLEL case?  */
3465       if (x == CONST0_RTX (GET_MODE (x)))
3466         *total = 0;
3467       else if (sh_1el_vec (x, VOIDmode))
3468         *total = outer_code != SET;
3469       if (sh_rep_vec (x, VOIDmode))
3470         *total = ((GET_MODE_UNIT_SIZE (GET_MODE (x)) + 3) / 4
3471                   + (outer_code != SET));
3472       *total = COSTS_N_INSNS (3) + (outer_code != SET);
3473       return true;
3474
3475     case PLUS:
3476     case MINUS:
3477       *total = COSTS_N_INSNS (addsubcosts (x));
3478       return true;
3479
3480     case AND:
3481     case XOR:
3482     case IOR:
3483       *total = COSTS_N_INSNS (and_xor_ior_costs (x, code));
3484       return true;
3485
3486     case MULT:
3487       *total = COSTS_N_INSNS (multcosts (x));
3488       return true;
3489
3490     case LT:
3491     case GE:
3492       /* div0s sign comparison.  */
3493       if (GET_CODE (XEXP (x, 0)) == XOR
3494           && REG_P ((XEXP (XEXP (x, 0), 0)))
3495           && REG_P ((XEXP (XEXP (x, 0), 1)))
3496           && satisfies_constraint_Z (XEXP (x, 1)))
3497         {
3498           *total = COSTS_N_INSNS (1);
3499           return true;
3500         }
3501       else
3502         return false;
3503
3504     case LSHIFTRT:
3505       /* div0s sign comparison.  */
3506       if (GET_CODE (XEXP (x, 0)) == XOR
3507           && REG_P ((XEXP (XEXP (x, 0), 0)))
3508           && REG_P ((XEXP (XEXP (x, 0), 1)))
3509           && CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) == 31)
3510         {
3511           *total = COSTS_N_INSNS (1);
3512           return true;
3513         }
3514       /* Fall through to shiftcosts.  */
3515     case ASHIFT:
3516     case ASHIFTRT:
3517       {
3518         int cost = shiftcosts (x);
3519         if (cost < 0)
3520           return false;
3521         *total = COSTS_N_INSNS (cost);
3522         return true;
3523       }
3524
3525     case DIV:
3526     case UDIV:
3527     case MOD:
3528     case UMOD:
3529       *total = COSTS_N_INSNS (20);
3530       return true;
3531
3532     case FLOAT:
3533     case FIX:
3534       *total = 100;
3535       return true;
3536
3537     default:
3538       return false;
3539     }
3540 }
3541
3542 /* Determine the size of the fundamental move insn that will be used
3543    for the specified mode.  */
3544 static inline int
3545 mov_insn_size (enum machine_mode mode, bool consider_sh2a)
3546 {
3547   const int mode_sz = GET_MODE_SIZE (mode);
3548
3549   if ((consider_sh2a && TARGET_SH2A_DOUBLE && mode == DFmode)
3550       || (TARGET_FMOVD && mode == DFmode))
3551     return mode_sz;
3552   else
3553     {
3554       /* The max. available mode for actual move insns is SImode.
3555          Larger accesses will be split into multiple loads/stores.  */
3556       const int max_mov_sz = GET_MODE_SIZE (SImode);
3557       return mode_sz >= max_mov_sz ? max_mov_sz : mode_sz;
3558     }
3559 }
3560
3561 /* Determine the maximum possible displacement for a move insn for the
3562    specified mode.  */
3563 static int
3564 max_mov_insn_displacement (enum machine_mode mode, bool consider_sh2a)
3565 {
3566   /* The 4 byte displacement move insns are the same as the 2 byte
3567      versions but take a 12 bit displacement.  All we need to do is to
3568      scale the max. displacement value accordingly.  */
3569   const int disp_scale = consider_sh2a ? (4095 / 15) : 1;
3570
3571   /* SH2A supports FPU move insns with 12 bit displacements.
3572      Other variants to do not support any kind of displacements for
3573      FPU move insns.  */
3574   if (! consider_sh2a && TARGET_FPU_ANY && GET_MODE_CLASS (mode) == MODE_FLOAT)
3575     return 0;
3576   else
3577     {
3578       const int mov_insn_sz = mov_insn_size (mode, consider_sh2a);
3579       const int mode_sz = GET_MODE_SIZE (mode);
3580       int r = 15 * mov_insn_sz * disp_scale;
3581     
3582       /* If the mov insn will be split into multiple loads/stores, the
3583          maximum possible displacement is a bit smaller.  */
3584       if (mode_sz > mov_insn_sz)
3585         r -= mode_sz - mov_insn_sz;
3586       return r;
3587     }
3588 }
3589
3590 /* Determine the alignment mask for a move insn of the
3591    specified mode.  */
3592 static inline int
3593 mov_insn_alignment_mask (enum machine_mode mode, bool consider_sh2a)
3594 {
3595   const int mov_insn_sz = mov_insn_size (mode, consider_sh2a);
3596   return mov_insn_sz > 0 ? (mov_insn_sz - 1) : 0;
3597 }
3598
3599 /* Return the displacement value of a displacement address.  */
3600 static inline HOST_WIDE_INT
3601 disp_addr_displacement (rtx x)
3602 {
3603   gcc_assert (satisfies_constraint_Sdd (x));
3604   return INTVAL (XEXP (XEXP (x, 0), 1));
3605 }
3606
3607 /* Compute the cost of an address.  */
3608 static int
3609 sh_address_cost (rtx x, enum machine_mode mode,
3610                  addr_space_t as ATTRIBUTE_UNUSED, bool speed ATTRIBUTE_UNUSED)
3611 {
3612   /* 'GBR + 0'.  Account one more because of R0 restriction.  */
3613   if (REG_P (x) && REGNO (x) == GBR_REG)
3614     return 2;
3615
3616   /* Simple reg, post-inc, pre-dec addressing.  */
3617   if (REG_P (x) || GET_CODE (x) == POST_INC || GET_CODE (x) == PRE_DEC)
3618     return 1;
3619
3620   /* 'reg + disp' addressing.  */
3621   if (GET_CODE (x) == PLUS
3622       && REG_P (XEXP (x, 0)) && CONST_INT_P (XEXP (x, 1)))
3623     {
3624       /* 'GBR + disp'.  Account one more because of R0 restriction.  */
3625       if (REGNO (XEXP (x, 0)) == GBR_REG
3626           && gbr_displacement (XEXP (x, 1), mode))
3627         return 2;
3628
3629       const HOST_WIDE_INT offset = INTVAL (XEXP (x, 1));
3630
3631       if (offset == 0)
3632         return 1;
3633
3634       /* The displacement would fit into a 2 byte move insn.
3635          HImode and QImode loads/stores with displacement put pressure on
3636          R0 which will most likely require another reg copy.  Thus account
3637          a higher cost for that.  */
3638       if (offset > 0 && offset <= max_mov_insn_displacement (mode, false))
3639         return (mode == HImode || mode == QImode) ? 2 : 1;
3640
3641       /* The displacement would fit into a 4 byte move insn (SH2A).  */
3642       if (TARGET_SH2A
3643           && offset > 0 && offset <= max_mov_insn_displacement (mode, true))
3644         return 2;
3645
3646       /* The displacement is probably out of range and will require extra
3647          calculations.  */
3648       return 3;
3649     }
3650
3651   /* 'reg + reg' addressing.  Account a slightly higher cost because of 
3652      increased pressure on R0.  */
3653   if (GET_CODE (x) == PLUS && ! CONSTANT_P (XEXP (x, 1))
3654       && ! TARGET_SHMEDIA)
3655     return 3;
3656
3657   /* Not sure what it is - probably expensive.  */
3658   return 10;
3659 }
3660
3661 /* Code to expand a shift.  */
3662 static void
3663 gen_ashift (int type, int n, rtx reg)
3664 {
3665   rtx n_rtx;
3666
3667   /* Negative values here come from the shift_amounts array.  */
3668   if (n < 0)
3669     {
3670       if (type == ASHIFT)
3671         type = LSHIFTRT;
3672       else
3673         type = ASHIFT;
3674       n = -n;
3675     }
3676
3677   n_rtx = GEN_INT (n);
3678   gcc_assert (satisfies_constraint_P27 (n_rtx));
3679
3680   switch (type)
3681     {
3682     case ASHIFTRT:
3683       emit_insn (gen_ashrsi3_k (reg, reg, n_rtx));
3684       break;
3685     case LSHIFTRT:
3686       if (n == 1)
3687         emit_insn (gen_shlr (reg, reg));
3688       else
3689         emit_insn (gen_lshrsi3_k (reg, reg, n_rtx));
3690       break;
3691     case ASHIFT:
3692       emit_insn (gen_ashlsi3_k (reg, reg, n_rtx));
3693       break;
3694     default:
3695       gcc_unreachable ();
3696     }
3697 }
3698
3699 /* Code to expand a HImode shift.  */
3700 static void
3701 gen_ashift_hi (int type, int n, rtx reg)
3702 {
3703   /* Negative values here come from the shift_amounts array.  */
3704   if (n < 0)
3705     {
3706       if (type == ASHIFT)
3707         type = LSHIFTRT;
3708       else
3709         type = ASHIFT;
3710       n = -n;
3711     }
3712
3713   switch (type)
3714     {
3715     case ASHIFTRT:
3716     case LSHIFTRT:
3717       /* We don't have HImode right shift operations because using the
3718          ordinary 32 bit shift instructions for that doesn't generate proper
3719          zero/sign extension.
3720          gen_ashift_hi is only called in contexts where we know that the
3721          sign extension works out correctly.  */
3722       {
3723         int offset = 0;
3724         if (GET_CODE (reg) == SUBREG)
3725           {
3726             offset = SUBREG_BYTE (reg);
3727             reg = SUBREG_REG (reg);
3728           }
3729         gen_ashift (type, n, gen_rtx_SUBREG (SImode, reg, offset));
3730         break;
3731       }
3732     case ASHIFT:
3733       emit_insn (gen_ashlhi3_k (reg, reg, GEN_INT (n)));
3734       break;
3735     }
3736 }
3737
3738 /* Output RTL to split a constant shift into its component SH constant
3739    shift instructions.  */
3740 void
3741 gen_shifty_op (int code, rtx *operands)
3742 {
3743   int value = INTVAL (operands[2]);
3744   int max, i;
3745
3746   /* Truncate the shift count in case it is out of bounds.  */
3747   value = value & 31;
3748
3749   if (value == 31)
3750     {
3751       if (code == LSHIFTRT)
3752         {
3753           emit_insn (gen_rotlsi3_1 (operands[0], operands[0]));
3754           emit_insn (gen_movt (operands[0], get_t_reg_rtx ()));
3755           return;
3756         }
3757       else if (code == ASHIFT)
3758         {
3759           /* There is a two instruction sequence for 31 bit left shifts,
3760              but it requires r0.  */
3761           if (REG_P (operands[0]) && REGNO (operands[0]) == 0)
3762             {
3763               emit_insn (gen_andsi3 (operands[0], operands[0], const1_rtx));
3764               emit_insn (gen_rotlsi3_31 (operands[0], operands[0]));
3765               return;
3766             }
3767         }
3768     }
3769   else if (value == 0)
3770     {
3771       /* This can happen even when optimizing, if there were subregs before
3772          reload.  Don't output a nop here, as this is never optimized away;
3773          use a no-op move instead.  */
3774       emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[0]));
3775       return;
3776     }
3777
3778   max = ashl_lshr_seq[value].insn_count;
3779   for (i = 0; i < max; i++)
3780     gen_ashift (code, ashl_lshr_seq[value].amount[i], operands[0]);
3781 }
3782
3783 /* Same as gen_shifty_op, but optimized for values where the topmost bits
3784    don't matter.  */
3785 void
3786 gen_shifty_hi_op (int code, rtx *operands)
3787 {
3788   int value = INTVAL (operands[2]);
3789   int max, i;
3790   void (*gen_fun) (int, int, rtx);
3791
3792   /* This operation is used by and_shl for SImode values with a few
3793      high bits known to be cleared.  */
3794   value &= 31;
3795   if (value == 0)
3796     {
3797       emit_insn (gen_nop ());
3798       return;
3799     }
3800
3801   gen_fun = GET_MODE (operands[0]) == HImode ? gen_ashift_hi : gen_ashift;
3802   if (code == ASHIFT)
3803     {
3804       max = ext_ashl_lshr_seq[value].insn_count;
3805       for (i = 0; i < max; i++)
3806         gen_fun (code, ext_ashl_lshr_seq[value].amount[i], operands[0]);
3807     }
3808   else
3809     /* When shifting right, emit the shifts in reverse order, so that
3810        solitary negative values come first.  */
3811     for (i = ext_ashl_lshr_seq[value].insn_count - 1; i >= 0; i--)
3812       gen_fun (code, ext_ashl_lshr_seq[value].amount[i], operands[0]);
3813 }
3814
3815 /* Output RTL for an arithmetic right shift.
3816    ??? Rewrite to use super-optimizer sequences.  */
3817 bool
3818 expand_ashiftrt (rtx *operands)
3819 {
3820   rtx wrk;
3821   char func[18];
3822   int value;
3823
3824   if (TARGET_DYNSHIFT)
3825     {
3826       if (!CONST_INT_P (operands[2]))
3827         {
3828           rtx count = copy_to_mode_reg (SImode, operands[2]);
3829           emit_insn (gen_negsi2 (count, count));
3830           emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
3831           return true;
3832         }
3833       else if (ashiftrt_insns[INTVAL (operands[2]) & 31]
3834                > 1 + SH_DYNAMIC_SHIFT_COST)
3835         {
3836           rtx count
3837             = force_reg (SImode, GEN_INT (- (INTVAL (operands[2]) & 31)));
3838           emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
3839           return true;
3840         }
3841     }
3842   if (!CONST_INT_P (operands[2]))
3843     return false;
3844
3845   value = INTVAL (operands[2]) & 31;
3846
3847   if (value == 31)
3848     {
3849       /* If we are called from abs expansion, arrange things so that we
3850          we can use a single MT instruction that doesn't clobber the source,
3851          if LICM can hoist out the load of the constant zero.  */
3852       if (currently_expanding_to_rtl)
3853         {
3854           emit_insn (gen_cmpgtsi_t (force_reg (SImode, CONST0_RTX (SImode)),
3855                                     operands[1]));
3856           emit_insn (gen_mov_neg_si_t (operands[0], get_t_reg_rtx ()));
3857           return true;
3858         }
3859       emit_insn (gen_ashrsi2_31 (operands[0], operands[1]));
3860       return true;
3861     }
3862   else if (value >= 16 && value <= 19)
3863     {
3864       wrk = gen_reg_rtx (SImode);
3865       emit_insn (gen_ashrsi2_16 (wrk, operands[1]));
3866       value -= 16;
3867       while (value--)
3868         gen_ashift (ASHIFTRT, 1, wrk);
3869       emit_move_insn (operands[0], wrk);
3870       return true;
3871     }
3872   /* Expand a short sequence inline, longer call a magic routine.  */
3873   else if (value <= 5)
3874     {
3875       wrk = gen_reg_rtx (SImode);
3876       emit_move_insn (wrk, operands[1]);
3877       while (value--)
3878         gen_ashift (ASHIFTRT, 1, wrk);
3879       emit_move_insn (operands[0], wrk);
3880       return true;
3881     }
3882
3883   wrk = gen_reg_rtx (Pmode);
3884
3885   /* Load the value into an arg reg and call a helper.  */
3886   emit_move_insn (gen_rtx_REG (SImode, 4), operands[1]);
3887   sprintf (func, "__ashiftrt_r4_%d", value);
3888   function_symbol (wrk, func, SFUNC_STATIC);
3889   emit_insn (gen_ashrsi3_n (GEN_INT (value), wrk));
3890   emit_move_insn (operands[0], gen_rtx_REG (SImode, 4));
3891   return true;
3892 }
3893
3894 /* Try to find a good way to implement the combiner pattern
3895   [(set (match_operand:SI 0 "register_operand" "r")
3896         (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
3897                            (match_operand:SI 2 "const_int_operand" "n"))
3898                 (match_operand:SI 3 "const_int_operand" "n"))) .
3899   LEFT_RTX is operand 2 in the above pattern, and MASK_RTX is operand 3.
3900   return 0 for simple right / left or left/right shift combination.
3901   return 1 for a combination of shifts with zero_extend.
3902   return 2 for a combination of shifts with an AND that needs r0.
3903   return 3 for a combination of shifts with an AND that needs an extra
3904     scratch register, when the three highmost bits of the AND mask are clear.
3905   return 4 for a combination of shifts with an AND that needs an extra
3906     scratch register, when any of the three highmost bits of the AND mask
3907     is set.
3908   If ATTRP is set, store an initial right shift width in ATTRP[0],
3909   and the instruction length in ATTRP[1] .  These values are not valid
3910   when returning 0.
3911   When ATTRP is set and returning 1, ATTRP[2] gets set to the index into
3912   shift_amounts for the last shift value that is to be used before the
3913   sign extend.  */
3914 int
3915 shl_and_kind (rtx left_rtx, rtx mask_rtx, int *attrp)
3916 {
3917   unsigned HOST_WIDE_INT mask, lsb, mask2, lsb2;
3918   int left = INTVAL (left_rtx), right;
3919   int best = 0;
3920   int cost, best_cost = 10000;
3921   int best_right = 0, best_len = 0;
3922   int i;
3923   int can_ext;
3924
3925   if (left < 0 || left > 31)
3926     return 0;
3927   if (CONST_INT_P (mask_rtx))
3928     mask = (unsigned HOST_WIDE_INT) INTVAL (mask_rtx) >> left;
3929   else
3930     mask = (unsigned HOST_WIDE_INT) GET_MODE_MASK (SImode) >> left;
3931   /* Can this be expressed as a right shift / left shift pair?  */
3932   lsb = ((mask ^ (mask - 1)) >> 1) + 1;
3933   right = exact_log2 (lsb);
3934   mask2 = ~(mask + lsb - 1);
3935   lsb2 = ((mask2 ^ (mask2 - 1)) >> 1) + 1;
3936   /* mask has no zeroes but trailing zeroes <==> ! mask2 */
3937   if (! mask2)
3938     best_cost = ashl_lshr_seq[right].insn_count
3939                 + ashl_lshr_seq[right + left].insn_count;
3940   /* mask has no trailing zeroes <==> ! right */
3941   else if (! right && mask2 == ~(lsb2 - 1))
3942     {
3943       int late_right = exact_log2 (lsb2);
3944       best_cost = ashl_lshr_seq[left + late_right].insn_count
3945                   + ashl_lshr_seq[late_right].insn_count;
3946     }
3947   /* Try to use zero extend.  */
3948   if (mask2 == ~(lsb2 - 1))
3949     {
3950       int width, first;
3951
3952       for (width = 8; width <= 16; width += 8)
3953         {
3954           /* Can we zero-extend right away?  */
3955           if (lsb2 == (unsigned HOST_WIDE_INT) 1 << width)
3956             {
3957               cost = 1 + ext_ashl_lshr_seq[right].insn_count
3958                        + ext_ashl_lshr_seq[left + right].insn_count;
3959               if (cost < best_cost)
3960                 {
3961                   best = 1;
3962                   best_cost = cost;
3963                   best_right = right;
3964                   best_len = cost;
3965                   if (attrp)
3966                     attrp[2] = -1;
3967                 }
3968               continue;
3969             }
3970           /* ??? Could try to put zero extend into initial right shift,
3971              or even shift a bit left before the right shift.  */
3972           /* Determine value of first part of left shift, to get to the
3973              zero extend cut-off point.  */
3974           first = width - exact_log2 (lsb2) + right;
3975           if (first >= 0 && right + left - first >= 0)
3976             {
3977               cost = ext_ashl_lshr_seq[right].insn_count
3978                      + ext_ashl_lshr_seq[first].insn_count + 1
3979                      + ext_ashl_lshr_seq[right + left - first].insn_count;
3980
3981               if (cost < best_cost)
3982                 {
3983                   best = 1;
3984                   best_cost = cost;
3985                   best_right = right;
3986                   best_len = cost;
3987                   if (attrp)
3988                     attrp[2] = first;
3989                 }
3990             }
3991         }
3992     }
3993   /* Try to use r0 AND pattern */
3994   for (i = 0; i <= 2; i++)
3995     {
3996       if (i > right)
3997         break;
3998       if (! CONST_OK_FOR_K08 (mask >> i))
3999         continue;
4000       cost = (i != 0) + 2 + ext_ashl_lshr_seq[left + i].insn_count;
4001       if (cost < best_cost)
4002         {
4003           best = 2;
4004           best_cost = cost;
4005           best_right = i;
4006           best_len = cost - 1;
4007         }
4008     }
4009   /* Try to use a scratch register to hold the AND operand.  */
4010   can_ext = ((mask << left) & ((unsigned HOST_WIDE_INT) 3 << 30)) == 0;
4011   for (i = 0; i <= 2; i++)
4012     {
4013       if (i > right)
4014         break;
4015       cost = (i != 0) + (CONST_OK_FOR_I08 (mask >> i) ? 2 : 3)
4016              + (can_ext
4017                 ? ext_ashl_lshr_seq
4018                 : ashl_lshr_seq)[left + i].insn_count;
4019       if (cost < best_cost)
4020         {
4021           best = 4 - can_ext;
4022           best_cost = cost;
4023           best_right = i;
4024           best_len = cost - 1 - ! CONST_OK_FOR_I08 (mask >> i);
4025         }
4026     }
4027
4028   if (attrp)
4029     {
4030       attrp[0] = best_right;
4031       attrp[1] = best_len;
4032     }
4033   return best;
4034 }
4035
4036 /* This is used in length attributes of the unnamed instructions
4037    corresponding to shl_and_kind return values of 1 and 2.  */
4038 int
4039 shl_and_length (rtx insn)
4040 {
4041   rtx set_src, left_rtx, mask_rtx;
4042   int attributes[3];
4043
4044   set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
4045   left_rtx = XEXP (XEXP (set_src, 0), 1);
4046   mask_rtx = XEXP (set_src, 1);
4047   shl_and_kind (left_rtx, mask_rtx, attributes);
4048   return attributes[1];
4049 }
4050
4051 /* This is used in length attribute of the and_shl_scratch instruction.  */
4052 int
4053 shl_and_scr_length (rtx insn)
4054 {
4055   rtx set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
4056   int len = ashl_lshr_seq[INTVAL (XEXP (set_src, 1)) & 31].insn_count;
4057   rtx op = XEXP (set_src, 0);
4058   len += ashl_lshr_seq[INTVAL (XEXP (op, 1)) & 31].insn_count + 1;
4059   op = XEXP (XEXP (op, 0), 0);
4060   return len + ashl_lshr_seq[INTVAL (XEXP (op, 1)) & 31].insn_count;
4061 }
4062
4063 /* Generate rtl for instructions for which shl_and_kind advised a particular
4064    method of generating them, i.e. returned zero.  */
4065 bool
4066 gen_shl_and (rtx dest, rtx left_rtx, rtx mask_rtx, rtx source)
4067 {
4068   int attributes[3];
4069   unsigned HOST_WIDE_INT mask;
4070   int kind = shl_and_kind (left_rtx, mask_rtx, attributes);
4071   int right, total_shift;
4072   void (*shift_gen_fun) (int, rtx *) = gen_shifty_hi_op;
4073
4074   right = attributes[0];
4075   total_shift = INTVAL (left_rtx) + right;
4076   mask = (unsigned HOST_WIDE_INT) INTVAL (mask_rtx) >> total_shift;
4077   switch (kind)
4078     {
4079     default:
4080       return true;
4081     case 1:
4082       {
4083         int first = attributes[2];
4084         rtx operands[3];
4085
4086         if (first < 0)
4087           {
4088             emit_insn ((mask << right) <= 0xff
4089                        ? gen_zero_extendqisi2 (dest,
4090                                                gen_lowpart (QImode, source))
4091                        : gen_zero_extendhisi2 (dest,
4092                                                gen_lowpart (HImode, source)));
4093             source = dest;
4094           }
4095         if (source != dest)
4096           emit_insn (gen_movsi (dest, source));
4097         operands[0] = dest;
4098         if (right)
4099           {
4100             operands[2] = GEN_INT (right);
4101             gen_shifty_hi_op (LSHIFTRT, operands);
4102           }
4103         if (first > 0)
4104           {
4105             operands[2] = GEN_INT (first);
4106             gen_shifty_hi_op (ASHIFT, operands);
4107             total_shift -= first;
4108             mask <<= first;
4109           }
4110         if (first >= 0)
4111           emit_insn (mask <= 0xff
4112                      ? gen_zero_extendqisi2 (dest, gen_lowpart (QImode, dest))
4113                      : gen_zero_extendhisi2 (dest, gen_lowpart (HImode, dest)));
4114         if (total_shift > 0)
4115           {
4116             operands[2] = GEN_INT (total_shift);
4117             gen_shifty_hi_op (ASHIFT, operands);
4118           }
4119         break;
4120       }
4121     case 4:
4122       shift_gen_fun = gen_shifty_op;
4123     case 3:
4124       /* If the topmost bit that matters is set, set the topmost bits
4125          that don't matter.  This way, we might be able to get a shorter
4126          signed constant.  */
4127       if (mask & ((HOST_WIDE_INT) 1 << (31 - total_shift)))
4128         mask |= (HOST_WIDE_INT) ~0 << (31 - total_shift);
4129     case 2:
4130       /* Don't expand fine-grained when combining, because that will
4131          make the pattern fail.  */
4132       if (currently_expanding_to_rtl
4133           || reload_in_progress || reload_completed)
4134         {
4135           rtx operands[3];
4136
4137           /* Cases 3 and 4 should be handled by this split
4138              only while combining  */
4139           gcc_assert (kind <= 2);
4140           if (right)
4141             {
4142               emit_insn (gen_lshrsi3 (dest, source, GEN_INT (right)));
4143               source = dest;
4144             }
4145           emit_insn (gen_andsi3 (dest, source, GEN_INT (mask)));
4146           if (total_shift)
4147             {
4148               operands[0] = dest;
4149               operands[1] = dest;
4150               operands[2] = GEN_INT (total_shift);
4151               shift_gen_fun (ASHIFT, operands);
4152             }
4153           break;
4154         }
4155       else
4156         {
4157           int neg = 0;
4158           if (kind != 4 && total_shift < 16)
4159             {
4160               neg = -ext_ashl_lshr_seq[total_shift].amount[1];
4161               if (neg > 0)
4162                 neg -= ext_ashl_lshr_seq[total_shift].amount[2];
4163               else
4164                 neg = 0;
4165             }
4166           emit_insn (gen_and_shl_scratch (dest, source,
4167                                           GEN_INT (right),
4168                                           GEN_INT (mask),
4169                                           GEN_INT (total_shift + neg),
4170                                           GEN_INT (neg)));
4171           emit_insn (gen_movsi (dest, dest));
4172           break;
4173         }
4174     }
4175   return false;
4176 }
4177
4178 /* Try to find a good way to implement the combiner pattern
4179   [(set (match_operand:SI 0 "register_operand" "=r")
4180         (sign_extract:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
4181                                     (match_operand:SI 2 "const_int_operand" "n")
4182                          (match_operand:SI 3 "const_int_operand" "n")
4183                          (const_int 0)))
4184    (clobber (reg:SI T_REG))]
4185   LEFT_RTX is operand 2 in the above pattern, and SIZE_RTX is operand 3.
4186   return 0 for simple left / right shift combination.
4187   return 1 for left shift / 8 bit sign extend / left shift.
4188   return 2 for left shift / 16 bit sign extend / left shift.
4189   return 3 for left shift / 8 bit sign extend / shift / sign extend.
4190   return 4 for left shift / 16 bit sign extend / shift / sign extend.
4191   return 5 for left shift / 16 bit sign extend / right shift
4192   return 6 for < 8 bit sign extend / left shift.
4193   return 7 for < 8 bit sign extend / left shift / single right shift.
4194   If COSTP is nonzero, assign the calculated cost to *COSTP.  */
4195 int
4196 shl_sext_kind (rtx left_rtx, rtx size_rtx, int *costp)
4197 {
4198   int left, size, insize, ext;
4199   int cost = 0, best_cost;
4200   int kind;
4201
4202   left = INTVAL (left_rtx);
4203   size = INTVAL (size_rtx);
4204   insize = size - left;
4205   gcc_assert (insize > 0);
4206   /* Default to left / right shift.  */
4207   kind = 0;
4208   best_cost = ashl_lshr_seq[32 - insize].insn_count
4209               + ashl_lshr_seq[32 - size].insn_count;
4210   if (size <= 16)
4211     {
4212       /* 16 bit shift / sign extend / 16 bit shift */
4213       cost = ashl_lshr_seq[16 - insize].insn_count + 1
4214              + ashl_lshr_seq[16 - size].insn_count;
4215       /* If ashiftrt_insns[16 - size] is 8, this choice will be overridden
4216          below, by alternative 3 or something even better.  */
4217       if (cost < best_cost)
4218         {
4219           kind = 5;
4220           best_cost = cost;
4221         }
4222     }
4223   /* Try a plain sign extend between two shifts.  */
4224   for (ext = 16; ext >= insize; ext -= 8)
4225     {
4226       if (ext <= size)
4227         {
4228           cost = ext_ashl_lshr_seq[ext - insize].insn_count + 1
4229                  + ashl_lshr_seq[size - ext].insn_count;
4230           if (cost < best_cost)
4231             {
4232               kind = ext / (unsigned) 8;
4233               best_cost = cost;
4234             }
4235         }
4236       /* Check if we can do a sloppy shift with a final signed shift
4237          restoring the sign.  */
4238       if (EXT_SHIFT_SIGNED (size - ext))
4239         cost = ext_ashl_lshr_seq[ext - insize].insn_count
4240                + ext_ashl_lshr_seq[size - ext].insn_count + 1;
4241       /* If not, maybe it's still cheaper to do the second shift sloppy,
4242          and do a final sign extend?  */
4243       else if (size <= 16)
4244         cost = ext_ashl_lshr_seq[ext - insize].insn_count + 1
4245           + ext_ashl_lshr_seq[size > ext ? size - ext : ext - size].insn_count
4246           + 1;
4247       else
4248         continue;
4249       if (cost < best_cost)
4250         {
4251           kind = ext / (unsigned) 8 + 2;
4252           best_cost = cost;
4253         }
4254     }
4255   /* Check if we can sign extend in r0 */
4256   if (insize < 8)
4257     {
4258       cost = 3 + ashl_lshr_seq[left].insn_count;
4259       if (cost < best_cost)
4260         {
4261           kind = 6;
4262           best_cost = cost;
4263         }
4264       /* Try the same with a final signed shift.  */
4265       if (left < 31)
4266         {
4267           cost = 3 + ext_ashl_lshr_seq[left + 1].insn_count + 1;
4268           if (cost < best_cost)
4269             {
4270               kind = 7;
4271               best_cost = cost;
4272             }
4273         }
4274     }
4275   if (TARGET_DYNSHIFT)
4276     {
4277       /* Try to use a dynamic shift.  */
4278       cost = ashl_lshr_seq[32 - insize].insn_count + 1 + SH_DYNAMIC_SHIFT_COST;
4279       if (cost < best_cost)
4280         {
4281           kind = 0;
4282           best_cost = cost;
4283         }
4284     }
4285   if (costp)
4286     *costp = cost;
4287   return kind;
4288 }
4289
4290 /* Function to be used in the length attribute of the instructions
4291    implementing this pattern.  */
4292 int
4293 shl_sext_length (rtx insn)
4294 {
4295   rtx set_src, left_rtx, size_rtx;
4296   int cost;
4297
4298   set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
4299   left_rtx = XEXP (XEXP (set_src, 0), 1);
4300   size_rtx = XEXP (set_src, 1);
4301   shl_sext_kind (left_rtx, size_rtx, &cost);
4302   return cost;
4303 }
4304
4305 /* Generate rtl for this pattern */
4306 bool
4307 gen_shl_sext (rtx dest, rtx left_rtx, rtx size_rtx, rtx source)
4308 {
4309   int kind;
4310   int left, size, insize, cost;
4311   rtx operands[3];
4312
4313   kind = shl_sext_kind (left_rtx, size_rtx, &cost);
4314   left = INTVAL (left_rtx);
4315   size = INTVAL (size_rtx);
4316   insize = size - left;
4317   switch (kind)
4318     {
4319     case 1:
4320     case 2:
4321     case 3:
4322     case 4:
4323       {
4324         int ext = kind & 1 ? 8 : 16;
4325         int shift2 = size - ext;
4326
4327         /* Don't expand fine-grained when combining, because that will
4328            make the pattern fail.  */
4329         if (! currently_expanding_to_rtl
4330             && ! reload_in_progress && ! reload_completed)
4331           {
4332             emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
4333             emit_insn (gen_movsi (dest, source));
4334             break;
4335           }
4336         if (dest != source)
4337           emit_insn (gen_movsi (dest, source));
4338         operands[0] = dest;
4339         if (ext - insize)
4340           {
4341             operands[2] = GEN_INT (ext - insize);
4342             gen_shifty_hi_op (ASHIFT, operands);
4343           }
4344         emit_insn (kind & 1
4345                    ? gen_extendqisi2 (dest, gen_lowpart (QImode, dest))
4346                    : gen_extendhisi2 (dest, gen_lowpart (HImode, dest)));
4347         if (kind <= 2)
4348           {
4349             if (shift2)
4350               {
4351                 operands[2] = GEN_INT (shift2);
4352                 gen_shifty_op (ASHIFT, operands);
4353               }
4354           }
4355         else
4356           {
4357             if (shift2 > 0)
4358               {
4359                 if (EXT_SHIFT_SIGNED (shift2))
4360                   {
4361                     operands[2] = GEN_INT (shift2 + 1);
4362                     gen_shifty_op (ASHIFT, operands);
4363                     operands[2] = const1_rtx;
4364                     gen_shifty_op (ASHIFTRT, operands);
4365                     break;
4366                   }
4367                 operands[2] = GEN_INT (shift2);
4368                 gen_shifty_hi_op (ASHIFT, operands);
4369               }
4370             else if (shift2)
4371               {
4372                 operands[2] = GEN_INT (-shift2);
4373                 gen_shifty_hi_op (LSHIFTRT, operands);
4374               }
4375             emit_insn (size <= 8
4376                        ? gen_extendqisi2 (dest, gen_lowpart (QImode, dest))
4377                        : gen_extendhisi2 (dest, gen_lowpart (HImode, dest)));
4378           }
4379         break;
4380       }
4381     case 5:
4382       {
4383         int i = 16 - size;
4384         if (! currently_expanding_to_rtl
4385             && ! reload_in_progress && ! reload_completed)
4386           emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
4387         else
4388           {
4389             operands[0] = dest;
4390             operands[2] = GEN_INT (16 - insize);
4391             gen_shifty_hi_op (ASHIFT, operands);
4392             emit_insn (gen_extendhisi2 (dest, gen_lowpart (HImode, dest)));
4393           }
4394         /* Don't use gen_ashrsi3 because it generates new pseudos.  */
4395         while (--i >= 0)
4396           gen_ashift (ASHIFTRT, 1, dest);
4397         break;
4398       }
4399     case 6:
4400     case 7:
4401       /* Don't expand fine-grained when combining, because that will
4402          make the pattern fail.  */
4403       if (! currently_expanding_to_rtl
4404           && ! reload_in_progress && ! reload_completed)
4405         {
4406           emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
4407           emit_insn (gen_movsi (dest, source));
4408           break;
4409         }
4410       emit_insn (gen_andsi3 (dest, source, GEN_INT ((1 << insize) - 1)));
4411       emit_insn (gen_xorsi3 (dest, dest, GEN_INT (1 << (insize - 1))));
4412       emit_insn (gen_addsi3 (dest, dest, GEN_INT (-1 << (insize - 1))));
4413       operands[0] = dest;
4414       operands[2] = kind == 7 ? GEN_INT (left + 1) : left_rtx;
4415       gen_shifty_op (ASHIFT, operands);
4416       if (kind == 7)
4417         emit_insn (gen_ashrsi3_k (dest, dest, const1_rtx));
4418       break;
4419     default:
4420       return true;
4421     }
4422   return false;
4423 }
4424
4425 /* Prefix a symbol_ref name with "datalabel".  */
4426 rtx
4427 gen_datalabel_ref (rtx sym)
4428 {
4429   const char *str;
4430
4431   if (GET_CODE (sym) == LABEL_REF)
4432     return gen_rtx_CONST (GET_MODE (sym),
4433                           gen_rtx_UNSPEC (GET_MODE (sym),
4434                                           gen_rtvec (1, sym),
4435                                           UNSPEC_DATALABEL));
4436
4437   gcc_assert (GET_CODE (sym) == SYMBOL_REF);
4438
4439   str = XSTR (sym, 0);
4440   /* Share all SYMBOL_REF strings with the same value - that is important
4441      for cse.  */
4442   str = IDENTIFIER_POINTER (get_identifier (str));
4443   XSTR (sym, 0) = str;
4444
4445   return sym;
4446 }
4447
4448 \f
4449 static alloc_pool label_ref_list_pool;
4450
4451 typedef struct label_ref_list_d
4452 {
4453   rtx label;
4454   struct label_ref_list_d *next;
4455 } *label_ref_list_t;
4456
4457 /* The SH cannot load a large constant into a register, constants have to
4458    come from a pc relative load.  The reference of a pc relative load
4459    instruction must be less than 1k in front of the instruction.  This
4460    means that we often have to dump a constant inside a function, and
4461    generate code to branch around it.
4462
4463    It is important to minimize this, since the branches will slow things
4464    down and make things bigger.
4465
4466    Worst case code looks like:
4467
4468    mov.l L1,rn
4469    bra   L2
4470    nop
4471    align
4472    L1:   .long value
4473    L2:
4474    ..
4475
4476    mov.l L3,rn
4477    bra   L4
4478    nop
4479    align
4480    L3:   .long value
4481    L4:
4482    ..
4483
4484    We fix this by performing a scan before scheduling, which notices which
4485    instructions need to have their operands fetched from the constant table
4486    and builds the table.
4487
4488    The algorithm is:
4489
4490    scan, find an instruction which needs a pcrel move.  Look forward, find the
4491    last barrier which is within MAX_COUNT bytes of the requirement.
4492    If there isn't one, make one.  Process all the instructions between
4493    the find and the barrier.
4494
4495    In the above example, we can tell that L3 is within 1k of L1, so
4496    the first move can be shrunk from the 3 insn+constant sequence into
4497    just 1 insn, and the constant moved to L3 to make:
4498
4499    mov.l        L1,rn
4500    ..
4501    mov.l        L3,rn
4502    bra          L4
4503    nop
4504    align
4505    L3:.long value
4506    L4:.long value
4507
4508    Then the second move becomes the target for the shortening process.  */
4509
4510 typedef struct
4511 {
4512   rtx value;                    /* Value in table.  */
4513   rtx label;                    /* Label of value.  */
4514   label_ref_list_t wend;        /* End of window.  */
4515   enum machine_mode mode;       /* Mode of value.  */
4516
4517   /* True if this constant is accessed as part of a post-increment
4518      sequence.  Note that HImode constants are never accessed in this way.  */
4519   bool part_of_sequence_p;
4520 } pool_node;
4521
4522 /* The maximum number of constants that can fit into one pool, since
4523    constants in the range 0..510 are at least 2 bytes long, and in the
4524    range from there to 1018 at least 4 bytes.  */
4525
4526 #define MAX_POOL_SIZE 372
4527 static pool_node pool_vector[MAX_POOL_SIZE];
4528 static int pool_size;
4529 static rtx pool_window_label;
4530 static int pool_window_last;
4531
4532 static int max_labelno_before_reorg;
4533
4534 /* ??? If we need a constant in HImode which is the truncated value of a
4535    constant we need in SImode, we could combine the two entries thus saving
4536    two bytes.  Is this common enough to be worth the effort of implementing
4537    it?  */
4538
4539 /* ??? This stuff should be done at the same time that we shorten branches.
4540    As it is now, we must assume that all branches are the maximum size, and
4541    this causes us to almost always output constant pools sooner than
4542    necessary.  */
4543
4544 /* Add a constant to the pool and return its label.  */
4545 static rtx
4546 add_constant (rtx x, enum machine_mode mode, rtx last_value)
4547 {
4548   int i;
4549   rtx lab, new_rtx;
4550   label_ref_list_t ref, newref;
4551
4552   /* First see if we've already got it.  */
4553   for (i = 0; i < pool_size; i++)
4554     {
4555       if (x->code == pool_vector[i].value->code
4556           && mode == pool_vector[i].mode)
4557         {
4558           if (x->code == CODE_LABEL)
4559             {
4560               if (XINT (x, 3) != XINT (pool_vector[i].value, 3))
4561                 continue;
4562             }
4563           if (rtx_equal_p (x, pool_vector[i].value))
4564             {
4565               lab = new_rtx = 0;
4566               if (! last_value
4567                   || ! i
4568                   || ! rtx_equal_p (last_value, pool_vector[i-1].value))
4569                 {
4570                   new_rtx = gen_label_rtx ();
4571                   LABEL_REFS (new_rtx) = pool_vector[i].label;
4572                   pool_vector[i].label = lab = new_rtx;
4573                 }
4574               if (lab && pool_window_label)
4575                 {
4576                   newref = (label_ref_list_t) pool_alloc (label_ref_list_pool);
4577                   newref->label = pool_window_label;
4578                   ref = pool_vector[pool_window_last].wend;
4579                   newref->next = ref;
4580                   pool_vector[pool_window_last].wend = newref;
4581                 }
4582               if (new_rtx)
4583                 pool_window_label = new_rtx;
4584               pool_window_last = i;
4585               return lab;
4586             }
4587         }
4588     }
4589
4590   /* Need a new one.  */
4591   pool_vector[pool_size].value = x;
4592   if (last_value && rtx_equal_p (last_value, pool_vector[pool_size - 1].value))
4593     {
4594       lab = 0;
4595       pool_vector[pool_size - 1].part_of_sequence_p = true;
4596     }
4597   else
4598     lab = gen_label_rtx ();
4599   pool_vector[pool_size].mode = mode;
4600   pool_vector[pool_size].label = lab;
4601   pool_vector[pool_size].wend = NULL;
4602   pool_vector[pool_size].part_of_sequence_p = (lab == 0);
4603   if (lab && pool_window_label)
4604     {
4605       newref = (label_ref_list_t) pool_alloc (label_ref_list_pool);
4606       newref->label = pool_window_label;
4607       ref = pool_vector[pool_window_last].wend;
4608       newref->next = ref;
4609       pool_vector[pool_window_last].wend = newref;
4610     }
4611   if (lab)
4612     pool_window_label = lab;
4613   pool_window_last = pool_size;
4614   pool_size++;
4615   return lab;
4616 }
4617
4618 /* Output the literal table.  START, if nonzero, is the first instruction
4619    this table is needed for, and also indicates that there is at least one
4620    casesi_worker_2 instruction; We have to emit the operand3 labels from
4621    these insns at a 4-byte  aligned position.  BARRIER is the barrier
4622    after which we are to place the table.  */
4623 static void
4624 dump_table (rtx start, rtx barrier)
4625 {
4626   rtx scan = barrier;
4627   int i;
4628   bool need_align = true;
4629   rtx lab;
4630   label_ref_list_t ref;
4631   bool have_df = false;
4632
4633   /* Do two passes, first time dump out the HI sized constants.  */
4634
4635   for (i = 0; i < pool_size; i++)
4636     {
4637       pool_node *p = &pool_vector[i];
4638
4639       if (p->mode == HImode)
4640         {
4641           if (need_align)
4642             {
4643               scan = emit_insn_after (gen_align_2 (), scan);
4644               need_align = false;
4645             }
4646           for (lab = p->label; lab; lab = LABEL_REFS (lab))
4647             scan = emit_label_after (lab, scan);
4648           scan = emit_insn_after (gen_consttable_2 (p->value, const0_rtx),
4649                                   scan);
4650           for (ref = p->wend; ref; ref = ref->next)
4651             {
4652               lab = ref->label;
4653               scan = emit_insn_after (gen_consttable_window_end (lab), scan);
4654             }
4655         }
4656       else if (p->mode == DFmode)
4657         have_df = true;
4658     }
4659
4660   need_align = true;
4661
4662   if (start)
4663     {
4664       scan = emit_insn_after (gen_align_4 (), scan);
4665       need_align = false;
4666       for (; start != barrier; start = NEXT_INSN (start))
4667         if (NONJUMP_INSN_P (start)
4668             && recog_memoized (start) == CODE_FOR_casesi_worker_2)
4669           {
4670             rtx src = SET_SRC (XVECEXP (PATTERN (start), 0, 0));
4671             rtx lab = XEXP (XVECEXP (src, 0, 3), 0);
4672
4673             scan = emit_label_after (lab, scan);
4674           }
4675     }
4676   if (TARGET_FMOVD && TARGET_ALIGN_DOUBLE && have_df)
4677     {
4678       rtx align_insn = NULL_RTX;
4679
4680       scan = emit_label_after (gen_label_rtx (), scan);
4681       scan = emit_insn_after (gen_align_log (GEN_INT (3)), scan);
4682       need_align = false;
4683
4684       for (i = 0; i < pool_size; i++)
4685         {
4686           pool_node *p = &pool_vector[i];
4687
4688           switch (p->mode)
4689             {
4690             case HImode:
4691               break;
4692             case SImode:
4693             case SFmode:
4694               if (align_insn && !p->part_of_sequence_p)
4695                 {
4696                   for (lab = p->label; lab; lab = LABEL_REFS (lab))
4697                     emit_label_before (lab, align_insn);
4698                   emit_insn_before (gen_consttable_4 (p->value, const0_rtx),
4699                                     align_insn);
4700                   for (ref = p->wend; ref; ref = ref->next)
4701                     {
4702                       lab = ref->label;
4703                       emit_insn_before (gen_consttable_window_end (lab),
4704                                         align_insn);
4705                     }
4706                   delete_insn (align_insn);
4707                   align_insn = NULL_RTX;
4708                   continue;
4709                 }
4710               else
4711                 {
4712                   for (lab = p->label; lab; lab = LABEL_REFS (lab))
4713                     scan = emit_label_after (lab, scan);
4714                   scan = emit_insn_after (gen_consttable_4 (p->value,
4715                                                             const0_rtx), scan);
4716                   need_align = ! need_align;
4717                 }
4718               break;
4719             case DFmode:
4720               if (need_align)
4721                 {
4722                   scan = emit_insn_after (gen_align_log (GEN_INT (3)), scan);
4723                   align_insn = scan;
4724                   need_align = false;
4725                 }
4726             case DImode:
4727               for (lab = p->label; lab; lab = LABEL_REFS (lab))
4728                 scan = emit_label_after (lab, scan);
4729               scan = emit_insn_after (gen_consttable_8 (p->value, const0_rtx),
4730                                       scan);
4731               break;
4732             default:
4733               gcc_unreachable ();
4734             }
4735
4736           if (p->mode != HImode)
4737             {
4738               for (ref = p->wend; ref; ref = ref->next)
4739                 {
4740                   lab = ref->label;
4741                   scan = emit_insn_after (gen_consttable_window_end (lab),
4742                                           scan);
4743                 }
4744             }
4745         }
4746
4747       pool_size = 0;
4748     }
4749
4750   for (i = 0; i < pool_size; i++)
4751     {
4752       pool_node *p = &pool_vector[i];
4753
4754       switch (p->mode)
4755         {
4756         case HImode:
4757           break;
4758         case SImode:
4759         case SFmode:
4760           if (need_align)
4761             {
4762               need_align = false;
4763               scan = emit_label_after (gen_label_rtx (), scan);
4764               scan = emit_insn_after (gen_align_4 (), scan);
4765             }
4766           for (lab = p->label; lab; lab = LABEL_REFS (lab))
4767             scan = emit_label_after (lab, scan);
4768           scan = emit_insn_after (gen_consttable_4 (p->value, const0_rtx),
4769                                   scan);
4770           break;
4771         case DFmode:
4772         case DImode:
4773           if (need_align)
4774             {
4775               need_align = false;
4776               scan = emit_label_after (gen_label_rtx (), scan);
4777               scan = emit_insn_after (gen_align_4 (), scan);
4778             }
4779           for (lab = p->label; lab; lab = LABEL_REFS (lab))
4780             scan = emit_label_after (lab, scan);
4781           scan = emit_insn_after (gen_consttable_8 (p->value, const0_rtx),
4782                                   scan);
4783           break;
4784         default:
4785           gcc_unreachable ();
4786         }
4787
4788       if (p->mode != HImode)
4789         {
4790           for (ref = p->wend; ref; ref = ref->next)
4791             {
4792               lab = ref->label;
4793               scan = emit_insn_after (gen_consttable_window_end (lab), scan);
4794             }
4795         }
4796     }
4797
4798   scan = emit_insn_after (gen_consttable_end (), scan);
4799   scan = emit_barrier_after (scan);
4800   pool_size = 0;
4801   pool_window_label = NULL_RTX;
4802   pool_window_last = 0;
4803 }
4804
4805 #define MOVA_LABELREF(mova) XVECEXP (SET_SRC (PATTERN (mova)), 0, 0)
4806
4807 /* Nonzero if the insn is a move instruction which needs to be fixed.  */
4808
4809 /* ??? For a DImode/DFmode moves, we don't need to fix it if each half of the
4810    CONST_DOUBLE input value is CONST_OK_FOR_I08.  For a SFmode move, we don't
4811    need to fix it if the input value is CONST_OK_FOR_I08.  */
4812 static bool
4813 broken_move (rtx insn)
4814 {
4815   if (NONJUMP_INSN_P (insn))
4816     {
4817       rtx pat = PATTERN (insn);
4818       if (GET_CODE (pat) == PARALLEL)
4819         pat = XVECEXP (pat, 0, 0);
4820       if (GET_CODE (pat) == SET
4821           /* We can load any 8-bit value if we don't care what the high
4822              order bits end up as.  */
4823           && GET_MODE (SET_DEST (pat)) != QImode
4824           && (CONSTANT_P (SET_SRC (pat))
4825               || (GET_CODE (SET_SRC (pat)) == UNSPEC_VOLATILE
4826                   && XINT (SET_SRC (pat), 1) ==  UNSPECV_SP_SWITCH_B)
4827               /* Match mova_const.  */
4828               || (GET_CODE (SET_SRC (pat)) == UNSPEC
4829                   && XINT (SET_SRC (pat), 1) == UNSPEC_MOVA
4830                   && GET_CODE (XVECEXP (SET_SRC (pat), 0, 0)) == CONST))
4831           && ! (TARGET_SH2E
4832                 && GET_CODE (SET_SRC (pat)) == CONST_DOUBLE
4833                 && (fp_zero_operand (SET_SRC (pat))
4834                     || fp_one_operand (SET_SRC (pat)))
4835                 /* In general we don't know the current setting of fpscr, so
4836                    disable fldi.
4837                    There is an exception if this was a register-register move
4838                    before reload - and hence it was ascertained that we have
4839                    single precision setting - and in a post-reload optimization
4840                    we changed this to do a constant load.  In that case
4841                    we don't have an r0 clobber, hence we must use fldi.  */
4842                 && (TARGET_FMOVD
4843                     || (GET_CODE (XEXP (XVECEXP (PATTERN (insn), 0, 2), 0))
4844                         == SCRATCH))
4845                 && REG_P (SET_DEST (pat))
4846                 && FP_REGISTER_P (REGNO (SET_DEST (pat))))
4847           && ! (TARGET_SH2A
4848                 && GET_MODE (SET_DEST (pat)) == SImode
4849                 && (satisfies_constraint_I20 (SET_SRC (pat))
4850                    || satisfies_constraint_I28 (SET_SRC (pat))))
4851           && ! satisfies_constraint_I08 (SET_SRC (pat)))
4852         return true;
4853     }
4854
4855   return false;
4856 }
4857
4858 /* Return true if the specified insn is a mova insn.  */
4859 static bool
4860 mova_p (rtx insn)
4861 {
4862   return (NONJUMP_INSN_P (insn)
4863           && GET_CODE (PATTERN (insn)) == SET
4864           && GET_CODE (SET_SRC (PATTERN (insn))) == UNSPEC
4865           && XINT (SET_SRC (PATTERN (insn)), 1) == UNSPEC_MOVA
4866           /* Don't match mova_const.  */
4867           && GET_CODE (MOVA_LABELREF (insn)) == LABEL_REF);
4868 }
4869
4870 /* Fix up a mova from a switch that went out of range.  */
4871 static void
4872 fixup_mova (rtx mova)
4873 {
4874   PUT_MODE (XEXP (MOVA_LABELREF (mova), 0), QImode);
4875   if (! flag_pic)
4876     {
4877       SET_SRC (PATTERN (mova)) = MOVA_LABELREF (mova);
4878       INSN_CODE (mova) = -1;
4879     }
4880   else
4881     {
4882       rtx worker = mova;
4883       rtx lab = gen_label_rtx ();
4884       rtx wpat, wpat0, wpat1, wsrc, target, base, diff;
4885
4886       do
4887         {
4888           worker = NEXT_INSN (worker);
4889           gcc_assert (worker
4890                       && !LABEL_P (worker)
4891                       && !JUMP_P (worker));
4892         } while (NOTE_P (worker)
4893                  || recog_memoized (worker) != CODE_FOR_casesi_worker_1);
4894       wpat = PATTERN (worker);
4895       wpat0 = XVECEXP (wpat, 0, 0);
4896       wpat1 = XVECEXP (wpat, 0, 1);
4897       wsrc = SET_SRC (wpat0);
4898       PATTERN (worker) = (gen_casesi_worker_2
4899                           (SET_DEST (wpat0), XVECEXP (wsrc, 0, 1),
4900                            XEXP (XVECEXP (wsrc, 0, 2), 0), lab,
4901                            XEXP (wpat1, 0)));
4902       INSN_CODE (worker) = -1;
4903       target = XVECEXP (SET_SRC (PATTERN (mova)), 0, 0);
4904       base = gen_rtx_LABEL_REF (Pmode, lab);
4905       diff = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, target, base), UNSPEC_SYMOFF);
4906       SET_SRC (PATTERN (mova)) = gen_rtx_CONST (Pmode, diff);
4907       INSN_CODE (mova) = -1;
4908     }
4909 }
4910
4911 /* NEW_MOVA is a mova we've just encountered while scanning forward.  Update
4912    *num_mova, and check if the new mova is not nested within the first one.
4913    return 0 if *first_mova was replaced, 1 if new_mova was replaced,
4914    2 if new_mova has been assigned to *first_mova, -1 otherwise..  */
4915 static int
4916 untangle_mova (int *num_mova, rtx *first_mova, rtx new_mova)
4917 {
4918   int n_addr = 0; /* Initialization to shut up spurious warning.  */
4919   int f_target, n_target = 0; /* Likewise.  */
4920
4921   if (optimize)
4922     {
4923       /* If NEW_MOVA has no address yet, it will be handled later.  */
4924       if (INSN_ADDRESSES_SIZE() <= (unsigned) INSN_UID (new_mova))
4925         return -1;
4926
4927       n_addr = INSN_ADDRESSES (INSN_UID (new_mova));
4928       n_target = INSN_ADDRESSES (INSN_UID (XEXP (MOVA_LABELREF (new_mova), 0)));
4929       if (n_addr > n_target || n_addr + 1022 < n_target)
4930         {
4931           /* Change the mova into a load.
4932              broken_move will then return true for it.  */
4933           fixup_mova (new_mova);
4934           return 1;
4935         }
4936     }
4937   if (!(*num_mova)++)
4938     {
4939       *first_mova = new_mova;
4940       return 2;
4941     }
4942   if (!optimize
4943       || ((f_target
4944            = INSN_ADDRESSES (INSN_UID (XEXP (MOVA_LABELREF (*first_mova), 0))))
4945           >= n_target))
4946     return -1;
4947
4948   (*num_mova)--;
4949   if (f_target - INSN_ADDRESSES (INSN_UID (*first_mova))
4950       > n_target - n_addr)
4951     {
4952       fixup_mova (*first_mova);
4953       return 0;
4954     }
4955   else
4956     {
4957       fixup_mova (new_mova);
4958       return 1;
4959     }
4960 }
4961
4962 /* Find the last barrier from insn FROM which is close enough to hold the
4963    constant pool.  If we can't find one, then create one near the end of
4964    the range.  */
4965 static rtx
4966 find_barrier (int num_mova, rtx mova, rtx from)
4967 {
4968   int count_si = 0;
4969   int count_hi = 0;
4970   int found_hi = 0;
4971   int found_si = 0;
4972   int found_di = 0;
4973   int hi_align = 2;
4974   int si_align = 2;
4975   int leading_mova = num_mova;
4976   rtx barrier_before_mova = NULL_RTX;
4977   rtx found_barrier = NULL_RTX;
4978   rtx good_barrier = NULL_RTX;
4979   int si_limit;
4980   int hi_limit;
4981   rtx orig = from;
4982   rtx last_got = NULL_RTX;
4983   rtx last_symoff = NULL_RTX;
4984
4985   /* For HImode: range is 510, add 4 because pc counts from address of
4986      second instruction after this one, subtract 2 for the jump instruction
4987      that we may need to emit before the table, subtract 2 for the instruction
4988      that fills the jump delay slot (in very rare cases, reorg will take an
4989      instruction from after the constant pool or will leave the delay slot
4990      empty).  This gives 510.
4991      For SImode: range is 1020, add 4 because pc counts from address of
4992      second instruction after this one, subtract 2 in case pc is 2 byte
4993      aligned, subtract 2 for the jump instruction that we may need to emit
4994      before the table, subtract 2 for the instruction that fills the jump
4995      delay slot.  This gives 1018.  */
4996
4997   /* The branch will always be shortened now that the reference address for
4998      forward branches is the successor address, thus we need no longer make
4999      adjustments to the [sh]i_limit for -O0.  */
5000
5001   si_limit = 1018;
5002   hi_limit = 510;
5003
5004   while (from && count_si < si_limit && count_hi < hi_limit)
5005     {
5006       int inc = get_attr_length (from);
5007       int new_align = 1;
5008
5009       /* If this is a label that existed at the time of the compute_alignments
5010          call, determine the alignment.  N.B.  When find_barrier recurses for
5011          an out-of-reach mova, we might see labels at the start of previously
5012          inserted constant tables.  */
5013       if (LABEL_P (from)
5014           && CODE_LABEL_NUMBER (from) <= max_labelno_before_reorg)
5015         {
5016           if (optimize)
5017             new_align = 1 << label_to_alignment (from);
5018           else if (BARRIER_P (prev_nonnote_insn (from)))
5019             new_align = 1 << barrier_align (from);
5020           else
5021             new_align = 1;
5022           inc = 0;
5023         }
5024       /* In case we are scanning a constant table because of recursion, check
5025          for explicit alignments.  If the table is long, we might be forced
5026          to emit the new table in front of it; the length of the alignment
5027          might be the last straw.  */
5028       else if (NONJUMP_INSN_P (from)
5029                && GET_CODE (PATTERN (from)) == UNSPEC_VOLATILE
5030                && XINT (PATTERN (from), 1) == UNSPECV_ALIGN)
5031         new_align = INTVAL (XVECEXP (PATTERN (from), 0, 0));
5032       /* When we find the end of a constant table, paste the new constant
5033          at the end.  That is better than putting it in front because
5034          this way, we don't need extra alignment for adding a 4-byte-aligned
5035          mov(a) label to a 2/4 or 8/4 byte aligned table.  */
5036       else if (NONJUMP_INSN_P (from)
5037                && GET_CODE (PATTERN (from)) == UNSPEC_VOLATILE
5038                && XINT (PATTERN (from), 1) == UNSPECV_CONST_END)
5039         return from;
5040
5041       if (BARRIER_P (from))
5042         {
5043           rtx next;
5044
5045           found_barrier = from;
5046
5047           /* If we are at the end of the function, or in front of an alignment
5048              instruction, we need not insert an extra alignment.  We prefer
5049              this kind of barrier.  */
5050           if (barrier_align (from) > 2)
5051             good_barrier = from;
5052
5053           /* If we are at the end of a hot/cold block, dump the constants
5054              here.  */
5055           next = NEXT_INSN (from);
5056           if (next
5057               && NOTE_P (next)
5058               && NOTE_KIND (next) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
5059             break;
5060         }
5061
5062       if (broken_move (from))
5063         {
5064           rtx pat, src, dst;
5065           enum machine_mode mode;
5066
5067           pat = PATTERN (from);
5068           if (GET_CODE (pat) == PARALLEL)
5069             pat = XVECEXP (pat, 0, 0);
5070           src = SET_SRC (pat);
5071           dst = SET_DEST (pat);
5072           mode = GET_MODE (dst);
5073
5074           /* GOT pcrelat setting comes in pair of
5075              mova       .L8,r0
5076              mov.l      .L8,r12
5077              instructions.  (plus add r0,r12).
5078              Remember if we see one without the other.  */
5079           if (GET_CODE (src) == UNSPEC && PIC_ADDR_P (XVECEXP (src, 0, 0)))
5080             last_got = last_got ? NULL_RTX : from;
5081           else if (PIC_ADDR_P (src))
5082             last_got = last_got ? NULL_RTX : from;
5083
5084           /* We must explicitly check the mode, because sometimes the
5085              front end will generate code to load unsigned constants into
5086              HImode targets without properly sign extending them.  */
5087           if (mode == HImode
5088               || (mode == SImode && satisfies_constraint_I16 (src)
5089                   && REGNO (dst) != FPUL_REG))
5090             {
5091               found_hi += 2;
5092               /* We put the short constants before the long constants, so
5093                  we must count the length of short constants in the range
5094                  for the long constants.  */
5095               /* ??? This isn't optimal, but is easy to do.  */
5096               si_limit -= 2;
5097             }
5098           else
5099             {
5100               /* We dump DF/DI constants before SF/SI ones, because
5101                  the limit is the same, but the alignment requirements
5102                  are higher.  We may waste up to 4 additional bytes
5103                  for alignment, and the DF/DI constant may have
5104                  another SF/SI constant placed before it.  */
5105               if (TARGET_SHCOMPACT
5106                   && ! found_di
5107                   && (mode == DFmode || mode == DImode))
5108                 {
5109                   found_di = 1;
5110                   si_limit -= 8;
5111                 }
5112               while (si_align > 2 && found_si + si_align - 2 > count_si)
5113                 si_align >>= 1;
5114               if (found_si > count_si)
5115                 count_si = found_si;
5116               found_si += GET_MODE_SIZE (mode);
5117               if (num_mova)
5118                 si_limit -= GET_MODE_SIZE (mode);
5119             }
5120         }
5121
5122       if (mova_p (from))
5123         {
5124           switch (untangle_mova (&num_mova, &mova, from))
5125             {
5126               case 1:
5127                 if (flag_pic)
5128                   {
5129                     rtx src = SET_SRC (PATTERN (from));
5130                     if (GET_CODE (src) == CONST
5131                         && GET_CODE (XEXP (src, 0)) == UNSPEC
5132                         && XINT (XEXP (src, 0), 1) == UNSPEC_SYMOFF)
5133                       last_symoff = from;
5134                   }
5135                 break;
5136               case 0:   return find_barrier (0, 0, mova);
5137               case 2:
5138                 {
5139                   leading_mova = 0;
5140                   barrier_before_mova
5141                     = good_barrier ? good_barrier : found_barrier;
5142                 }
5143               default:  break;
5144             }
5145           if (found_si > count_si)
5146             count_si = found_si;
5147         }
5148       else if (JUMP_TABLE_DATA_P (from)
5149                && GET_CODE (PATTERN (from)) == ADDR_DIFF_VEC)
5150         {
5151           if ((num_mova > 1 && GET_MODE (prev_nonnote_insn (from)) == VOIDmode)
5152               || (num_mova
5153                   && (prev_nonnote_insn (from)
5154                       == XEXP (MOVA_LABELREF (mova), 0))))
5155             num_mova--;
5156           if (barrier_align (next_real_insn (from)) == align_jumps_log)
5157             {
5158               /* We have just passed the barrier in front of the
5159                  ADDR_DIFF_VEC, which is stored in found_barrier.  Since
5160                  the ADDR_DIFF_VEC is accessed as data, just like our pool
5161                  constants, this is a good opportunity to accommodate what
5162                  we have gathered so far.
5163                  If we waited any longer, we could end up at a barrier in
5164                  front of code, which gives worse cache usage for separated
5165                  instruction / data caches.  */
5166               good_barrier = found_barrier;
5167               break;
5168             }
5169           else
5170             {
5171               rtx body = PATTERN (from);
5172               inc = XVECLEN (body, 1) * GET_MODE_SIZE (GET_MODE (body));
5173             }
5174         }
5175       /* For the SH1, we generate alignments even after jumps-around-jumps.  */
5176       else if (JUMP_P (from)
5177                && ! TARGET_SH2
5178                && ! optimize_size)
5179         new_align = 4;
5180
5181       /* There is a possibility that a bf is transformed into a bf/s by the
5182          delay slot scheduler.  */
5183       if (JUMP_P (from)
5184           && get_attr_type (from) == TYPE_CBRANCH
5185           && ! sequence_insn_p (from))
5186         inc += 2;
5187
5188       if (found_si)
5189         {
5190           count_si += inc;
5191           if (new_align > si_align)
5192             {
5193               si_limit -= (count_si - 1) & (new_align - si_align);
5194               si_align = new_align;
5195             }
5196           count_si = (count_si + new_align - 1) & -new_align;
5197         }
5198       if (found_hi)
5199         {
5200           count_hi += inc;
5201           if (new_align > hi_align)
5202             {
5203               hi_limit -= (count_hi - 1) & (new_align - hi_align);
5204               hi_align = new_align;
5205             }
5206           count_hi = (count_hi + new_align - 1) & -new_align;
5207         }
5208       from = NEXT_INSN (from);
5209     }
5210
5211   if (num_mova)
5212     {
5213       if (leading_mova)
5214         {
5215           /* Try as we might, the leading mova is out of range.  Change
5216              it into a load (which will become a pcload) and retry.  */
5217           fixup_mova (mova);
5218           return find_barrier (0, 0, mova);
5219         }
5220       else
5221         {
5222           /* Insert the constant pool table before the mova instruction,
5223              to prevent the mova label reference from going out of range.  */
5224           from = mova;
5225           good_barrier = found_barrier = barrier_before_mova;
5226         }
5227     }
5228
5229   if (found_barrier)
5230     {
5231       if (good_barrier && next_real_insn (found_barrier))
5232         found_barrier = good_barrier;
5233     }
5234   else
5235     {
5236       /* We didn't find a barrier in time to dump our stuff,
5237          so we'll make one.  */
5238       rtx label = gen_label_rtx ();
5239
5240       /* Don't emit a constant table in the middle of insns for
5241          casesi_worker_2.  This is a bit overkill but is enough
5242          because casesi_worker_2 wouldn't appear so frequently.  */
5243       if (last_symoff)
5244         from = last_symoff;
5245
5246       /* If we exceeded the range, then we must back up over the last
5247          instruction we looked at.  Otherwise, we just need to undo the
5248          NEXT_INSN at the end of the loop.  */
5249       if (PREV_INSN (from) != orig
5250           && (count_hi > hi_limit || count_si > si_limit))
5251         from = PREV_INSN (PREV_INSN (from));
5252       else
5253         from = PREV_INSN (from);
5254
5255       /* Don't emit a constant table int the middle of global pointer setting,
5256          since that that would move the addressing base GOT into another table. 
5257          We need the first mov instruction before the _GLOBAL_OFFSET_TABLE_
5258          in the pool anyway, so just move up the whole constant pool.
5259
5260          However, avoid doing so when the last single GOT mov is the starting
5261          insn itself. Going past above the start insn would create a negative
5262          offset, causing errors.  */
5263       if (last_got && last_got != orig)
5264         from = PREV_INSN (last_got);
5265
5266       /* Don't insert the constant pool table at the position which
5267          may be the landing pad.  */
5268       if (flag_exceptions
5269           && CALL_P (from)
5270           && find_reg_note (from, REG_EH_REGION, NULL_RTX))
5271         from = PREV_INSN (from);
5272
5273       /* Walk back to be just before any jump or label.
5274          Putting it before a label reduces the number of times the branch
5275          around the constant pool table will be hit.  Putting it before
5276          a jump makes it more likely that the bra delay slot will be
5277          filled.  */
5278       while (NOTE_P (from) || JUMP_P (from)
5279              || LABEL_P (from))
5280         from = PREV_INSN (from);
5281
5282       /* Make sure we do not split between a call and its corresponding
5283          CALL_ARG_LOCATION note.  */
5284       if (CALL_P (from))
5285         {
5286           rtx next = NEXT_INSN (from);
5287           if (next && NOTE_P (next)
5288               && NOTE_KIND (next) == NOTE_INSN_CALL_ARG_LOCATION)
5289             from = next;
5290         }
5291
5292       from = emit_jump_insn_after (gen_jump (label), from);
5293       JUMP_LABEL (from) = label;
5294       LABEL_NUSES (label) = 1;
5295       found_barrier = emit_barrier_after (from);
5296       emit_label_after (label, found_barrier);
5297     }
5298
5299   return found_barrier;
5300 }
5301
5302 /* If the instruction INSN is implemented by a special function, and we can
5303    positively find the register that is used to call the sfunc, and this
5304    register is not used anywhere else in this instruction - except as the
5305    destination of a set, return this register; else, return 0.  */
5306 rtx
5307 sfunc_uses_reg (rtx insn)
5308 {
5309   int i;
5310   rtx pattern, part, reg_part, reg;
5311
5312   if (!NONJUMP_INSN_P (insn))
5313     return NULL_RTX;
5314   pattern = PATTERN (insn);
5315   if (GET_CODE (pattern) != PARALLEL || get_attr_type (insn) != TYPE_SFUNC)
5316     return NULL_RTX;
5317
5318   for (reg_part = NULL_RTX, i = XVECLEN (pattern, 0) - 1; i >= 1; i--)
5319     {
5320       part = XVECEXP (pattern, 0, i);
5321       if (GET_CODE (part) == USE && GET_MODE (XEXP (part, 0)) == SImode)
5322         reg_part = part;
5323     }
5324   if (! reg_part)
5325     return NULL_RTX;
5326   reg = XEXP (reg_part, 0);
5327   for (i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
5328     {
5329       part = XVECEXP (pattern, 0, i);
5330       if (part == reg_part || GET_CODE (part) == CLOBBER)
5331         continue;
5332       if (reg_mentioned_p (reg, ((GET_CODE (part) == SET
5333                                   && REG_P (SET_DEST (part)))
5334                                  ? SET_SRC (part) : part)))
5335         return NULL_RTX;
5336     }
5337   return reg;
5338 }
5339
5340 /* See if the only way in which INSN uses REG is by calling it, or by
5341    setting it while calling it.  Set *SET to a SET rtx if the register
5342    is set by INSN.  */
5343 static bool
5344 noncall_uses_reg (rtx reg, rtx insn, rtx *set)
5345 {
5346   rtx pattern, reg2;
5347
5348   *set = NULL_RTX;
5349
5350   reg2 = sfunc_uses_reg (insn);
5351   if (reg2 && REGNO (reg2) == REGNO (reg))
5352     {
5353       pattern = single_set (insn);
5354       if (pattern
5355           && REG_P (SET_DEST (pattern))
5356           && REGNO (reg) == REGNO (SET_DEST (pattern)))
5357         *set = pattern;
5358       return false;
5359     }
5360   if (!CALL_P (insn))
5361     {
5362       /* We don't use rtx_equal_p because we don't care if the mode is
5363          different.  */
5364       pattern = single_set (insn);
5365       if (pattern
5366           && REG_P (SET_DEST (pattern))
5367           && REGNO (reg) == REGNO (SET_DEST (pattern)))
5368         {
5369           rtx par, part;
5370           int i;
5371
5372           *set = pattern;
5373           par = PATTERN (insn);
5374           if (GET_CODE (par) == PARALLEL)
5375             for (i = XVECLEN (par, 0) - 1; i >= 0; i--)
5376               {
5377                 part = XVECEXP (par, 0, i);
5378                 if (GET_CODE (part) != SET && reg_mentioned_p (reg, part))
5379                   return true;
5380               }
5381           return reg_mentioned_p (reg, SET_SRC (pattern));
5382         }
5383
5384       return true;
5385     }
5386
5387   pattern = PATTERN (insn);
5388
5389   if (GET_CODE (pattern) == PARALLEL)
5390     {
5391       int i;
5392
5393       for (i = XVECLEN (pattern, 0) - 1; i >= 1; i--)
5394         if (reg_mentioned_p (reg, XVECEXP (pattern, 0, i)))
5395           return true;
5396       pattern = XVECEXP (pattern, 0, 0);
5397     }
5398
5399   if (GET_CODE (pattern) == SET)
5400     {
5401       if (reg_mentioned_p (reg, SET_DEST (pattern)))
5402         {
5403           /* We don't use rtx_equal_p, because we don't care if the
5404              mode is different.  */
5405           if (!REG_P (SET_DEST (pattern))
5406               || REGNO (reg) != REGNO (SET_DEST (pattern)))
5407             return true;
5408
5409           *set = pattern;
5410         }
5411
5412       pattern = SET_SRC (pattern);
5413     }
5414
5415   if (GET_CODE (pattern) != CALL
5416       || !MEM_P (XEXP (pattern, 0))
5417       || ! rtx_equal_p (reg, XEXP (XEXP (pattern, 0), 0)))
5418     return true;
5419
5420   return false;
5421 }
5422
5423 /* Given a X, a pattern of an insn or a part of it, return a mask of used
5424    general registers.  Bits 0..15 mean that the respective registers
5425    are used as inputs in the instruction.  Bits 16..31 mean that the
5426    registers 0..15, respectively, are used as outputs, or are clobbered.
5427    IS_DEST should be set to 16 if X is the destination of a SET, else to 0.  */
5428 int
5429 regs_used (rtx x, int is_dest)
5430 {
5431   enum rtx_code code;
5432   const char *fmt;
5433   int i, used = 0;
5434
5435   if (! x)
5436     return used;
5437   code = GET_CODE (x);
5438   switch (code)
5439     {
5440     case REG:
5441       if (REGNO (x) < 16)
5442         return (((1 << HARD_REGNO_NREGS (0, GET_MODE (x))) - 1)
5443                 << (REGNO (x) + is_dest));
5444       return 0;
5445     case SUBREG:
5446       {
5447         rtx y = SUBREG_REG (x);
5448
5449         if (!REG_P (y))
5450           break;
5451         if (REGNO (y) < 16)
5452           return (((1 << HARD_REGNO_NREGS (0, GET_MODE (x))) - 1)
5453                   << (REGNO (y) +
5454                       subreg_regno_offset (REGNO (y),
5455                                            GET_MODE (y),
5456                                            SUBREG_BYTE (x),
5457                                            GET_MODE (x)) + is_dest));
5458         return 0;
5459       }
5460     case SET:
5461       return regs_used (SET_SRC (x), 0) | regs_used (SET_DEST (x), 16);
5462     case RETURN:
5463       /* If there was a return value, it must have been indicated with USE.  */
5464       return 0x00ffff00;
5465     case CLOBBER:
5466       is_dest = 1;
5467       break;
5468     case MEM:
5469       is_dest = 0;
5470       break;
5471     case CALL:
5472       used |= 0x00ff00f0;
5473       break;
5474     default:
5475       break;
5476     }
5477
5478   fmt = GET_RTX_FORMAT (code);
5479
5480   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5481     {
5482       if (fmt[i] == 'E')
5483         {
5484           int j;
5485           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5486             used |= regs_used (XVECEXP (x, i, j), is_dest);
5487         }
5488       else if (fmt[i] == 'e')
5489         used |= regs_used (XEXP (x, i), is_dest);
5490     }
5491   return used;
5492 }
5493
5494 /* Create an instruction that prevents redirection of a conditional branch
5495    to the destination of the JUMP with address ADDR.
5496    If the branch needs to be implemented as an indirect jump, try to find
5497    a scratch register for it.
5498    If NEED_BLOCK is 0, don't do anything unless we need a scratch register.
5499    If any preceding insn that doesn't fit into a delay slot is good enough,
5500    pass 1.  Pass 2 if a definite blocking insn is needed.
5501    -1 is used internally to avoid deep recursion.
5502    If a blocking instruction is made or recognized, return it.  */
5503 static rtx
5504 gen_block_redirect (rtx jump, int addr, int need_block)
5505 {
5506   int dead = 0;
5507   rtx prev = prev_nonnote_insn (jump);
5508   rtx dest;
5509
5510   /* First, check if we already have an instruction that satisfies our need.  */
5511   if (prev && NONJUMP_INSN_P (prev) && ! INSN_DELETED_P (prev))
5512     {
5513       if (INSN_CODE (prev) == CODE_FOR_indirect_jump_scratch)
5514         return prev;
5515       if (GET_CODE (PATTERN (prev)) == USE
5516           || GET_CODE (PATTERN (prev)) == CLOBBER
5517           || get_attr_in_delay_slot (prev) == IN_DELAY_SLOT_YES)
5518         prev = jump;
5519       else if ((need_block &= ~1) < 0)
5520         return prev;
5521       else if (recog_memoized (prev) == CODE_FOR_block_branch_redirect)
5522         need_block = 0;
5523     }
5524   if (GET_CODE (PATTERN (jump)) == RETURN)
5525     {
5526       if (! need_block)
5527         return prev;
5528       /* Reorg even does nasty things with return insns that cause branches
5529          to go out of range - see find_end_label and callers.  */
5530       return emit_insn_before (gen_block_branch_redirect (const0_rtx) , jump);
5531     }
5532   /* We can't use JUMP_LABEL here because it might be undefined
5533      when not optimizing.  */
5534   dest = XEXP (SET_SRC (PATTERN (jump)), 0);
5535   /* If the branch is out of range, try to find a scratch register for it.  */
5536   if (optimize
5537       && (INSN_ADDRESSES (INSN_UID (dest)) - addr + (unsigned) 4092
5538           > 4092 + 4098))
5539     {
5540       rtx scan;
5541       /* Don't look for the stack pointer as a scratch register,
5542          it would cause trouble if an interrupt occurred.  */
5543       unsigned attempt = 0x7fff, used;
5544       int jump_left = flag_expensive_optimizations + 1;
5545
5546       /* It is likely that the most recent eligible instruction is wanted for
5547          the delay slot.  Therefore, find out which registers it uses, and
5548          try to avoid using them.  */
5549
5550       for (scan = jump; (scan = PREV_INSN (scan)); )
5551         {
5552           enum rtx_code code;
5553
5554           if (INSN_DELETED_P (scan))
5555             continue;
5556           code = GET_CODE (scan);
5557           if (code == CODE_LABEL || code == JUMP_INSN)
5558             break;
5559           if (code == INSN
5560               && GET_CODE (PATTERN (scan)) != USE
5561               && GET_CODE (PATTERN (scan)) != CLOBBER
5562               && get_attr_in_delay_slot (scan) == IN_DELAY_SLOT_YES)
5563             {
5564               attempt &= ~regs_used (PATTERN (scan), 0);
5565               break;
5566             }
5567         }
5568       for (used = dead = 0, scan = JUMP_LABEL (jump);
5569            (scan = NEXT_INSN (scan)); )
5570         {
5571           enum rtx_code code;
5572
5573           if (INSN_DELETED_P (scan))
5574             continue;
5575           code = GET_CODE (scan);
5576           if (INSN_P (scan))
5577             {
5578               used |= regs_used (PATTERN (scan), 0);
5579               if (code == CALL_INSN)
5580                 used |= regs_used (CALL_INSN_FUNCTION_USAGE (scan), 0);
5581               dead |= (used >> 16) & ~used;
5582               if (dead & attempt)
5583                 {
5584                   dead &= attempt;
5585                   break;
5586                 }
5587               if (code == JUMP_INSN)
5588                 {
5589                   if (jump_left-- && simplejump_p (scan))
5590                     scan = JUMP_LABEL (scan);
5591                   else
5592                     break;
5593                 }
5594             }
5595         }
5596       /* Mask out the stack pointer again, in case it was
5597          the only 'free' register we have found.  */
5598       dead &= 0x7fff;
5599     }
5600   /* If the immediate destination is still in range, check for possible
5601      threading with a jump beyond the delay slot insn.
5602      Don't check if we are called recursively; the jump has been or will be
5603      checked in a different invocation then.  */
5604
5605   else if (optimize && need_block >= 0)
5606     {
5607       rtx next = next_active_insn (next_active_insn (dest));
5608       if (next && JUMP_P (next)
5609           && GET_CODE (PATTERN (next)) == SET
5610           && recog_memoized (next) == CODE_FOR_jump_compact)
5611         {
5612           dest = JUMP_LABEL (next);
5613           if (dest
5614               && (INSN_ADDRESSES (INSN_UID (dest)) - addr + (unsigned) 4092
5615                   > 4092 + 4098))
5616             gen_block_redirect (next, INSN_ADDRESSES (INSN_UID (next)), -1);
5617         }
5618     }
5619
5620   if (dead)
5621     {
5622       rtx reg = gen_rtx_REG (SImode, exact_log2 (dead & -dead));
5623
5624       /* It would be nice if we could convert the jump into an indirect
5625          jump / far branch right now, and thus exposing all constituent
5626          instructions to further optimization.  However, reorg uses
5627          simplejump_p to determine if there is an unconditional jump where
5628          it should try to schedule instructions from the target of the
5629          branch; simplejump_p fails for indirect jumps even if they have
5630          a JUMP_LABEL.  */
5631       rtx insn = emit_insn_before (gen_indirect_jump_scratch
5632                                    (reg, GEN_INT (unspec_bbr_uid++)),
5633                                    jump);
5634       /* ??? We would like this to have the scope of the jump, but that
5635          scope will change when a delay slot insn of an inner scope is added.
5636          Hence, after delay slot scheduling, we'll have to expect
5637          NOTE_INSN_BLOCK_END notes between the indirect_jump_scratch and
5638          the jump.  */
5639
5640       INSN_LOCATION (insn) = INSN_LOCATION (jump);
5641       INSN_CODE (insn) = CODE_FOR_indirect_jump_scratch;
5642       return insn;
5643     }
5644   else if (need_block)
5645     /* We can't use JUMP_LABEL here because it might be undefined
5646        when not optimizing.  */
5647     return emit_insn_before (gen_block_branch_redirect
5648                              (GEN_INT (unspec_bbr_uid++)),
5649                              jump);
5650   return prev;
5651 }
5652
5653 #define CONDJUMP_MIN -252
5654 #define CONDJUMP_MAX 262
5655 struct far_branch
5656 {
5657   /* A label (to be placed) in front of the jump
5658      that jumps to our ultimate destination.  */
5659   rtx near_label;
5660   /* Where we are going to insert it if we cannot move the jump any farther,
5661      or the jump itself if we have picked up an existing jump.  */
5662   rtx insert_place;
5663   /* The ultimate destination.  */
5664   rtx far_label;
5665   struct far_branch *prev;
5666   /* If the branch has already been created, its address;
5667      else the address of its first prospective user.  */
5668   int address;
5669 };
5670
5671 static void gen_far_branch (struct far_branch *);
5672 enum mdep_reorg_phase_e mdep_reorg_phase;
5673 static void
5674 gen_far_branch (struct far_branch *bp)
5675 {
5676   rtx insn = bp->insert_place;
5677   rtx jump;
5678   rtx label = gen_label_rtx ();
5679   int ok;
5680
5681   emit_label_after (label, insn);
5682   if (bp->far_label)
5683     {
5684       jump = emit_jump_insn_after (gen_jump (bp->far_label), insn);
5685       LABEL_NUSES (bp->far_label)++;
5686     }
5687   else
5688     jump = emit_jump_insn_after (gen_return (), insn);
5689
5690   /* Emit a barrier so that reorg knows that any following instructions
5691      are not reachable via a fall-through path.
5692      But don't do this when not optimizing, since we wouldn't suppress the
5693      alignment for the barrier then, and could end up with out-of-range
5694      pc-relative loads.  */
5695   if (optimize)
5696     emit_barrier_after (jump);
5697   emit_label_after (bp->near_label, insn);
5698
5699   if (bp->far_label)
5700     JUMP_LABEL (jump) = bp->far_label;
5701   else
5702     {
5703       rtx pat = PATTERN (jump);
5704       gcc_assert (ANY_RETURN_P (pat));
5705       JUMP_LABEL (jump) = pat;
5706     }
5707
5708   ok = invert_jump (insn, label, 1);
5709   gcc_assert (ok);
5710
5711   /* If we are branching around a jump (rather than a return), prevent
5712      reorg from using an insn from the jump target as the delay slot insn -
5713      when reorg did this, it pessimized code (we rather hide the delay slot)
5714      and it could cause branches to go out of range.  */
5715   if (bp->far_label)
5716     (emit_insn_after
5717      (gen_stuff_delay_slot
5718       (GEN_INT (unspec_bbr_uid++),
5719        GEN_INT (recog_memoized (insn) == CODE_FOR_branch_false)),
5720       insn));
5721   /* Prevent reorg from undoing our splits.  */
5722   gen_block_redirect (jump, bp->address += 2, 2);
5723 }
5724
5725 /* Fix up ADDR_DIFF_VECs.  */
5726 void
5727 fixup_addr_diff_vecs (rtx first)
5728 {
5729   rtx insn;
5730
5731   for (insn = first; insn; insn = NEXT_INSN (insn))
5732     {
5733       rtx vec_lab, pat, prev, prevpat, x, braf_label;
5734
5735       if (! JUMP_TABLE_DATA_P (insn)
5736           || GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
5737         continue;
5738       pat = PATTERN (insn);
5739       vec_lab = XEXP (XEXP (pat, 0), 0);
5740
5741       /* Search the matching casesi_jump_2.  */
5742       for (prev = vec_lab; ; prev = PREV_INSN (prev))
5743         {
5744           if (!JUMP_P (prev))
5745             continue;
5746           prevpat = PATTERN (prev);
5747           if (GET_CODE (prevpat) != PARALLEL || XVECLEN (prevpat, 0) != 2)
5748             continue;
5749           x = XVECEXP (prevpat, 0, 1);
5750           if (GET_CODE (x) != USE)
5751             continue;
5752           x = XEXP (x, 0);
5753           if (GET_CODE (x) == LABEL_REF && XEXP (x, 0) == vec_lab)
5754             break;
5755         }
5756       /* FIXME: This is a bug in the optimizer, but it seems harmless
5757          to just avoid panicing.  */
5758       if (!prev)
5759         continue;
5760
5761       /* Emit the reference label of the braf where it belongs, right after
5762          the casesi_jump_2 (i.e. braf).  */
5763       braf_label = XEXP (XEXP (SET_SRC (XVECEXP (prevpat, 0, 0)), 1), 0);
5764       emit_label_after (braf_label, prev);
5765
5766       /* Fix up the ADDR_DIF_VEC to be relative
5767          to the reference address of the braf.  */
5768       XEXP (XEXP (pat, 0), 0) = braf_label;
5769     }
5770 }
5771
5772 /* BARRIER_OR_LABEL is either a BARRIER or a CODE_LABEL immediately following
5773    a barrier.  Return the base 2 logarithm of the desired alignment.  */
5774 int
5775 barrier_align (rtx barrier_or_label)
5776 {
5777   rtx next, pat;
5778
5779   if (LABEL_P (barrier_or_label)
5780       && NEXT_INSN (barrier_or_label)
5781       && JUMP_TABLE_DATA_P (NEXT_INSN (barrier_or_label)))
5782     return 2;
5783
5784   if (BARRIER_P (barrier_or_label)
5785       && PREV_INSN (barrier_or_label)
5786       && JUMP_TABLE_DATA_P (PREV_INSN (barrier_or_label)))
5787     {
5788       pat = PATTERN (PREV_INSN (barrier_or_label));
5789       /* If this is a very small table, we want to keep the alignment after
5790          the table to the minimum for proper code alignment.  */
5791       return ((optimize_size
5792                || ((unsigned) XVECLEN (pat, 1) * GET_MODE_SIZE (GET_MODE (pat))
5793                    <= (unsigned) 1 << (CACHE_LOG - 2)))
5794               ? 1 << TARGET_SHMEDIA : align_jumps_log);
5795     }
5796
5797   next = next_active_insn (barrier_or_label);
5798
5799   if (! next)
5800     return 0;
5801
5802   pat = PATTERN (next);
5803
5804   if (GET_CODE (pat) == UNSPEC_VOLATILE && XINT (pat, 1) == UNSPECV_ALIGN)
5805     /* This is a barrier in front of a constant table.  */
5806     return 0;
5807
5808   if (optimize_size)
5809     return 0;
5810
5811   if (! TARGET_SH2 || ! optimize)
5812     return align_jumps_log;
5813
5814   /* When fixing up pcloads, a constant table might be inserted just before
5815      the basic block that ends with the barrier.  Thus, we can't trust the
5816      instruction lengths before that.  */
5817   if (mdep_reorg_phase > SH_FIXUP_PCLOAD)
5818     {
5819       /* Check if there is an immediately preceding branch to the insn beyond
5820          the barrier.  We must weight the cost of discarding useful information
5821          from the current cache line when executing this branch and there is
5822          an alignment, against that of fetching unneeded insn in front of the
5823          branch target when there is no alignment.  */
5824
5825       /* There are two delay_slot cases to consider.  One is the simple case
5826          where the preceding branch is to the insn beyond the barrier (simple
5827          delay slot filling), and the other is where the preceding branch has
5828          a delay slot that is a duplicate of the insn after the barrier
5829          (fill_eager_delay_slots) and the branch is to the insn after the insn
5830          after the barrier.  */
5831
5832       int slot, credit;
5833       bool jump_to_next = false;
5834
5835       /* Skip to the insn before the JUMP_INSN before the barrier under
5836          investigation.  */
5837       rtx prev = prev_real_insn (prev_active_insn (barrier_or_label));
5838
5839       for (slot = 2, credit = (1 << (CACHE_LOG - 2)) + 2;
5840            credit >= 0 && prev && NONJUMP_INSN_P (prev);
5841            prev = prev_real_insn (prev))
5842         {
5843           jump_to_next = false;
5844           if (GET_CODE (PATTERN (prev)) == USE
5845               || GET_CODE (PATTERN (prev)) == CLOBBER)
5846             continue;
5847           if (GET_CODE (PATTERN (prev)) == SEQUENCE)
5848             {
5849               prev = XVECEXP (PATTERN (prev), 0, 1);
5850               if (INSN_UID (prev) == INSN_UID (next))
5851                 {
5852                   /* Delay slot was filled with insn at jump target.  */
5853                   jump_to_next = true;
5854                   continue;
5855                 }
5856             }
5857
5858           if (slot &&
5859               get_attr_in_delay_slot (prev) == IN_DELAY_SLOT_YES)
5860             slot = 0;
5861           credit -= get_attr_length (prev);
5862         }
5863       if (prev && jump_to_label_p (prev))
5864         {
5865           rtx x;
5866           if (jump_to_next
5867               || next_real_insn (JUMP_LABEL (prev)) == next
5868               /* If relax_delay_slots() decides NEXT was redundant
5869                  with some previous instruction, it will have
5870                  redirected PREV's jump to the following insn.  */
5871               || JUMP_LABEL (prev) == next_nonnote_insn (next)
5872               /* There is no upper bound on redundant instructions
5873                  that might have been skipped, but we must not put an
5874                  alignment where none had been before.  */
5875               || (x = (NEXT_INSN (NEXT_INSN (PREV_INSN (prev)))),
5876                   (INSN_P (x)
5877                    && (INSN_CODE (x) == CODE_FOR_block_branch_redirect
5878                        || INSN_CODE (x) == CODE_FOR_indirect_jump_scratch
5879                        || INSN_CODE (x) == CODE_FOR_stuff_delay_slot))))
5880             {
5881               rtx pat = PATTERN (prev);
5882               if (GET_CODE (pat) == PARALLEL)
5883                 pat = XVECEXP (pat, 0, 0);
5884               if (credit - slot >= (GET_CODE (SET_SRC (pat)) == PC ? 2 : 0))
5885                 return 0;
5886             }
5887         }
5888     }
5889
5890   return align_jumps_log;
5891 }
5892
5893 /* If we are inside a phony loop, almost any kind of label can turn up as the
5894    first one in the loop.  Aligning a braf label causes incorrect switch
5895    destination addresses; we can detect braf labels because they are
5896    followed by a BARRIER.
5897    Applying loop alignment to small constant or switch tables is a waste
5898    of space, so we suppress this too.  */
5899 int
5900 sh_loop_align (rtx label)
5901 {
5902   rtx next = label;
5903
5904   if (! optimize || optimize_size)
5905     return 0;
5906
5907   do
5908     next = next_nonnote_insn (next);
5909   while (next && LABEL_P (next));
5910
5911   if (! next
5912       || ! INSN_P (next)
5913       || recog_memoized (next) == CODE_FOR_consttable_2)
5914     return 0;
5915
5916   return align_loops_log;
5917 }
5918
5919 /* Do a final pass over the function, just before delayed branch
5920    scheduling.  */
5921 static void
5922 sh_reorg (void)
5923 {
5924   rtx first, insn, mova = NULL_RTX;
5925   int num_mova;
5926   rtx r0_rtx = gen_rtx_REG (Pmode, 0);
5927   rtx r0_inc_rtx = gen_rtx_POST_INC (Pmode, r0_rtx);
5928
5929   first = get_insns ();
5930   max_labelno_before_reorg = max_label_num ();
5931
5932   /* We must split call insns before introducing `mova's.  If we're
5933      optimizing, they'll have already been split.  Otherwise, make
5934      sure we don't split them too late.  */
5935   if (! optimize)
5936     split_all_insns_noflow ();
5937
5938   if (TARGET_SHMEDIA)
5939     return;
5940
5941   /* If relaxing, generate pseudo-ops to associate function calls with
5942      the symbols they call.  It does no harm to not generate these
5943      pseudo-ops.  However, when we can generate them, it enables the
5944      linker to potentially relax the jsr to a bsr, and eliminate the
5945      register load and, possibly, the constant pool entry.  */
5946
5947   mdep_reorg_phase = SH_INSERT_USES_LABELS;
5948   if (TARGET_RELAX)
5949     {
5950       /* Remove all REG_LABEL_OPERAND notes.  We want to use them for our
5951          own purposes.  This works because none of the remaining passes
5952          need to look at them.
5953
5954          ??? But it may break in the future.  We should use a machine
5955          dependent REG_NOTE, or some other approach entirely.  */
5956       for (insn = first; insn; insn = NEXT_INSN (insn))
5957         {
5958           if (INSN_P (insn))
5959             {
5960               rtx note;
5961
5962               while ((note = find_reg_note (insn, REG_LABEL_OPERAND,
5963                                             NULL_RTX)) != 0)
5964                 remove_note (insn, note);
5965             }
5966         }
5967
5968       for (insn = first; insn; insn = NEXT_INSN (insn))
5969         {
5970           rtx pattern, reg, link, set, scan, dies, label;
5971           int rescan = 0, foundinsn = 0;
5972
5973           if (CALL_P (insn))
5974             {
5975               pattern = PATTERN (insn);
5976
5977               if (GET_CODE (pattern) == PARALLEL)
5978                 pattern = XVECEXP (pattern, 0, 0);
5979               if (GET_CODE (pattern) == SET)
5980                 pattern = SET_SRC (pattern);
5981
5982               if (GET_CODE (pattern) != CALL
5983                   || !MEM_P (XEXP (pattern, 0)))
5984                 continue;
5985
5986               reg = XEXP (XEXP (pattern, 0), 0);
5987             }
5988           else
5989             {
5990               reg = sfunc_uses_reg (insn);
5991               if (! reg)
5992                 continue;
5993             }
5994
5995           if (!REG_P (reg))
5996             continue;
5997
5998           /* Try scanning backward to find where the register is set.  */
5999           link = NULL;
6000           for (scan = PREV_INSN (insn);
6001                scan && !LABEL_P (scan);
6002                scan = PREV_INSN (scan))
6003             {
6004               if (! INSN_P (scan))
6005                 continue;
6006
6007               if (! reg_mentioned_p (reg, scan))
6008                 continue;
6009
6010               if (noncall_uses_reg (reg, scan, &set))
6011                 break;
6012
6013               if (set)
6014                 {
6015                   link = scan;
6016                   break;
6017                 }
6018             }
6019
6020           if (! link)
6021             continue;
6022
6023           /* The register is set at LINK.  */
6024
6025           /* We can only optimize the function call if the register is
6026              being set to a symbol.  In theory, we could sometimes
6027              optimize calls to a constant location, but the assembler
6028              and linker do not support that at present.  */
6029           if (GET_CODE (SET_SRC (set)) != SYMBOL_REF
6030               && GET_CODE (SET_SRC (set)) != LABEL_REF)
6031             continue;
6032
6033           /* Scan forward from LINK to the place where REG dies, and
6034              make sure that the only insns which use REG are
6035              themselves function calls.  */
6036
6037           /* ??? This doesn't work for call targets that were allocated
6038              by reload, since there may not be a REG_DEAD note for the
6039              register.  */
6040
6041           dies = NULL_RTX;
6042           for (scan = NEXT_INSN (link); scan; scan = NEXT_INSN (scan))
6043             {
6044               rtx scanset;
6045
6046               /* Don't try to trace forward past a CODE_LABEL if we haven't
6047                  seen INSN yet.  Ordinarily, we will only find the setting insn
6048                  if it is in the same basic block.  However,
6049                  cross-jumping can insert code labels in between the load and
6050                  the call, and can result in situations where a single call
6051                  insn may have two targets depending on where we came from.  */
6052
6053               if (LABEL_P (scan) && ! foundinsn)
6054                 break;
6055
6056               if (! INSN_P (scan))
6057                 continue;
6058
6059               /* Don't try to trace forward past a JUMP.  To optimize
6060                  safely, we would have to check that all the
6061                  instructions at the jump destination did not use REG.  */
6062
6063               if (JUMP_P (scan))
6064                 break;
6065
6066               if (! reg_mentioned_p (reg, scan))
6067                 continue;
6068
6069               if (noncall_uses_reg (reg, scan, &scanset))
6070                 break;
6071
6072               if (scan == insn)
6073                 foundinsn = 1;
6074
6075               if (scan != insn
6076                   && (CALL_P (scan) || sfunc_uses_reg (scan)))
6077                 {
6078                   /* There is a function call to this register other
6079                      than the one we are checking.  If we optimize
6080                      this call, we need to rescan again below.  */
6081                   rescan = 1;
6082                 }
6083
6084               /* ??? We shouldn't have to worry about SCANSET here.
6085                  We should just be able to check for a REG_DEAD note
6086                  on a function call.  However, the REG_DEAD notes are
6087                  apparently not dependable around libcalls; c-torture
6088                  execute/920501-2 is a test case.  If SCANSET is set,
6089                  then this insn sets the register, so it must have
6090                  died earlier.  Unfortunately, this will only handle
6091                  the cases in which the register is, in fact, set in a
6092                  later insn.  */
6093
6094               /* ??? We shouldn't have to use FOUNDINSN here.
6095                  This dates back to when we used LOG_LINKS to find 
6096                  the most recent insn which sets the register.  */
6097
6098               if (foundinsn
6099                   && (scanset
6100                       || find_reg_note (scan, REG_DEAD, reg)))
6101                 {
6102                   dies = scan;
6103                   break;
6104                 }
6105             }
6106
6107           if (! dies)
6108             {
6109               /* Either there was a branch, or some insn used REG
6110                  other than as a function call address.  */
6111               continue;
6112             }
6113
6114           /* Create a code label, and put it in a REG_LABEL_OPERAND note
6115              on the insn which sets the register, and on each call insn
6116              which uses the register.  In final_prescan_insn we look for
6117              the REG_LABEL_OPERAND notes, and output the appropriate label
6118              or pseudo-op.  */
6119
6120           label = gen_label_rtx ();
6121           add_reg_note (link, REG_LABEL_OPERAND, label);
6122           add_reg_note (insn, REG_LABEL_OPERAND, label);
6123           if (rescan)
6124             {
6125               scan = link;
6126               do
6127                 {
6128                   rtx reg2;
6129
6130                   scan = NEXT_INSN (scan);
6131                   if (scan != insn
6132                       && ((CALL_P (scan)
6133                            && reg_mentioned_p (reg, scan))
6134                           || ((reg2 = sfunc_uses_reg (scan))
6135                               && REGNO (reg2) == REGNO (reg))))
6136                     add_reg_note (scan, REG_LABEL_OPERAND, label);
6137                 }
6138               while (scan != dies);
6139             }
6140         }
6141     }
6142
6143   if (TARGET_SH2)
6144     fixup_addr_diff_vecs (first);
6145
6146   if (optimize)
6147     {
6148       mdep_reorg_phase = SH_SHORTEN_BRANCHES0;
6149       shorten_branches (first);
6150     }
6151
6152   /* Scan the function looking for move instructions which have to be
6153      changed to pc-relative loads and insert the literal tables.  */
6154   label_ref_list_pool = create_alloc_pool ("label references list",
6155                                            sizeof (struct label_ref_list_d),
6156                                            30);
6157   mdep_reorg_phase = SH_FIXUP_PCLOAD;
6158   for (insn = first, num_mova = 0; insn; insn = NEXT_INSN (insn))
6159     {
6160       if (mova_p (insn))
6161         {
6162           /* ??? basic block reordering can move a switch table dispatch
6163              below the switch table.  Check if that has happened.
6164              We only have the addresses available when optimizing; but then,
6165              this check shouldn't be needed when not optimizing.  */
6166           if (!untangle_mova (&num_mova, &mova, insn))
6167             {
6168               insn = mova;
6169               num_mova = 0;
6170             }
6171         }
6172       else if (JUMP_TABLE_DATA_P (insn)
6173                && GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
6174                && num_mova
6175                /* ??? loop invariant motion can also move a mova out of a
6176                   loop.  Since loop does this code motion anyway, maybe we
6177                   should wrap UNSPEC_MOVA into a CONST, so that reload can
6178                   move it back.  */
6179                && ((num_mova > 1
6180                     && GET_MODE (prev_nonnote_insn (insn)) == VOIDmode)
6181                    || (prev_nonnote_insn (insn)
6182                        == XEXP (MOVA_LABELREF (mova), 0))))
6183         {
6184           rtx scan;
6185           int total;
6186
6187           num_mova--;
6188
6189           /* Some code might have been inserted between the mova and
6190              its ADDR_DIFF_VEC.  Check if the mova is still in range.  */
6191           for (scan = mova, total = 0; scan != insn; scan = NEXT_INSN (scan))
6192             total += get_attr_length (scan);
6193
6194           /* range of mova is 1020, add 4 because pc counts from address of
6195              second instruction after this one, subtract 2 in case pc is 2
6196              byte aligned.  Possible alignment needed for the ADDR_DIFF_VEC
6197              cancels out with alignment effects of the mova itself.  */
6198           if (total > 1022)
6199             {
6200               /* Change the mova into a load, and restart scanning
6201                  there.  broken_move will then return true for mova.  */
6202               fixup_mova (mova);
6203               insn = mova;
6204             }
6205         }
6206       if (broken_move (insn)
6207           || (NONJUMP_INSN_P (insn)
6208               && recog_memoized (insn) == CODE_FOR_casesi_worker_2))
6209         {
6210           rtx scan;
6211           /* Scan ahead looking for a barrier to stick the constant table
6212              behind.  */
6213           rtx barrier = find_barrier (num_mova, mova, insn);
6214           rtx last_float_move = NULL_RTX, last_float = 0, *last_float_addr = NULL;
6215           int need_aligned_label = 0;
6216
6217           if (num_mova && ! mova_p (mova))
6218             {
6219               /* find_barrier had to change the first mova into a
6220                  pcload; thus, we have to start with this new pcload.  */
6221               insn = mova;
6222               num_mova = 0;
6223             }
6224           /* Now find all the moves between the points and modify them.  */
6225           for (scan = insn; scan != barrier; scan = NEXT_INSN (scan))
6226             {
6227               if (LABEL_P (scan))
6228                 last_float = 0;
6229               if (NONJUMP_INSN_P (scan)
6230                   && recog_memoized (scan) == CODE_FOR_casesi_worker_2)
6231                 need_aligned_label = 1;
6232               if (broken_move (scan))
6233                 {
6234                   rtx *patp = &PATTERN (scan), pat = *patp;
6235                   rtx src, dst;
6236                   rtx lab;
6237                   rtx newsrc;
6238                   enum machine_mode mode;
6239
6240                   if (GET_CODE (pat) == PARALLEL)
6241                     patp = &XVECEXP (pat, 0, 0), pat = *patp;
6242                   src = SET_SRC (pat);
6243                   dst = SET_DEST (pat);
6244                   mode = GET_MODE (dst);
6245
6246                   if (mode == SImode && satisfies_constraint_I16 (src)
6247                       && REGNO (dst) != FPUL_REG)
6248                     {
6249                       int offset = 0;
6250
6251                       mode = HImode;
6252                       while (GET_CODE (dst) == SUBREG)
6253                         {
6254                           offset += subreg_regno_offset (REGNO (SUBREG_REG (dst)),
6255                                                          GET_MODE (SUBREG_REG (dst)),
6256                                                          SUBREG_BYTE (dst),
6257                                                          GET_MODE (dst));
6258                           dst = SUBREG_REG (dst);
6259                         }
6260                       dst = gen_rtx_REG (HImode, REGNO (dst) + offset);
6261                     }
6262                   if (REG_P (dst) && FP_ANY_REGISTER_P (REGNO (dst)))
6263                     {
6264                       /* This must be an insn that clobbers r0.  */
6265                       rtx *clobberp = &XVECEXP (PATTERN (scan), 0,
6266                                                 XVECLEN (PATTERN (scan), 0)
6267                                                 - 1);
6268                       rtx clobber = *clobberp;
6269
6270                       gcc_assert (GET_CODE (clobber) == CLOBBER
6271                                   && rtx_equal_p (XEXP (clobber, 0), r0_rtx));
6272
6273                       if (last_float
6274                           && reg_set_between_p (r0_rtx, last_float_move, scan))
6275                         last_float = 0;
6276                       if (last_float
6277                           && TARGET_SHCOMPACT
6278                           && GET_MODE_SIZE (mode) != 4
6279                           && GET_MODE_SIZE (GET_MODE (last_float)) == 4)
6280                         last_float = 0;
6281                       lab = add_constant (src, mode, last_float);
6282                       if (lab)
6283                         emit_insn_before (gen_mova (lab), scan);
6284                       else
6285                         {
6286                           /* There will be a REG_UNUSED note for r0 on
6287                              LAST_FLOAT_MOVE; we have to change it to REG_INC,
6288                              lest reorg:mark_target_live_regs will not
6289                              consider r0 to be used, and we end up with delay
6290                              slot insn in front of SCAN that clobbers r0.  */
6291                           rtx note
6292                             = find_regno_note (last_float_move, REG_UNUSED, 0);
6293
6294                           /* If we are not optimizing, then there may not be
6295                              a note.  */
6296                           if (note)
6297                             PUT_REG_NOTE_KIND (note, REG_INC);
6298
6299                           *last_float_addr = r0_inc_rtx;
6300                         }
6301                       last_float_move = scan;
6302                       last_float = src;
6303                       newsrc = gen_const_mem (mode,
6304                                         (((TARGET_SH4 && ! TARGET_FMOVD)
6305                                           || REGNO (dst) == FPUL_REG)
6306                                          ? r0_inc_rtx
6307                                          : r0_rtx));
6308                       last_float_addr = &XEXP (newsrc, 0);
6309
6310                       /* Remove the clobber of r0.  */
6311                       *clobberp = gen_rtx_CLOBBER (GET_MODE (clobber),
6312                                                    gen_rtx_SCRATCH (Pmode));
6313                     }
6314                   /* This is a mova needing a label.  Create it.  */
6315                   else if (GET_CODE (src) == UNSPEC
6316                            && XINT (src, 1) == UNSPEC_MOVA
6317                            && GET_CODE (XVECEXP (src, 0, 0)) == CONST)
6318                     {
6319                       lab = add_constant (XVECEXP (src, 0, 0), mode, 0);
6320                       newsrc = gen_rtx_LABEL_REF (VOIDmode, lab);
6321                       newsrc = gen_rtx_UNSPEC (SImode,
6322                                                gen_rtvec (1, newsrc),
6323                                                UNSPEC_MOVA);
6324                     }
6325                   else if (GET_CODE (src) == UNSPEC_VOLATILE
6326                            && XINT (src, 1) == UNSPECV_SP_SWITCH_B)
6327                     {
6328                       newsrc = XVECEXP (src, 0, 0);
6329                       XVECEXP (src, 0, 0) = gen_const_mem (mode, newsrc);
6330                       INSN_CODE (scan) = -1;
6331                       continue;
6332                     }
6333                   else
6334                     {
6335                       lab = add_constant (src, mode, 0);
6336                       newsrc = gen_rtx_LABEL_REF (VOIDmode, lab);
6337                       newsrc = gen_const_mem (mode, newsrc);
6338                     }
6339                   *patp = gen_rtx_SET (VOIDmode, dst, newsrc);
6340                   INSN_CODE (scan) = -1;
6341                 }
6342             }
6343           dump_table (need_aligned_label ? insn : 0, barrier);
6344           insn = barrier;
6345         }
6346     }
6347   free_alloc_pool (label_ref_list_pool);
6348   for (insn = first; insn; insn = NEXT_INSN (insn))
6349     PUT_MODE (insn, VOIDmode);
6350
6351   mdep_reorg_phase = SH_SHORTEN_BRANCHES1;
6352   INSN_ADDRESSES_FREE ();
6353   split_branches (first);
6354
6355   /* The INSN_REFERENCES_ARE_DELAYED in sh.h is problematic because it
6356      also has an effect on the register that holds the address of the sfunc.
6357      Insert an extra dummy insn in front of each sfunc that pretends to
6358      use this register.  */
6359   if (flag_delayed_branch)
6360     {
6361       for (insn = first; insn; insn = NEXT_INSN (insn))
6362         {
6363           rtx reg = sfunc_uses_reg (insn);
6364
6365           if (! reg)
6366             continue;
6367           emit_insn_before (gen_use_sfunc_addr (reg), insn);
6368         }
6369     }
6370 #if 0
6371   /* fpscr is not actually a user variable, but we pretend it is for the
6372      sake of the previous optimization passes, since we want it handled like
6373      one.  However, we don't have any debugging information for it, so turn
6374      it into a non-user variable now.  */
6375   if (TARGET_SH4)
6376     REG_USERVAR_P (get_fpscr_rtx ()) = 0;
6377 #endif
6378   mdep_reorg_phase = SH_AFTER_MDEP_REORG;
6379 }
6380
6381 /* Return the UID of the insn that follows the specified label.  */
6382 int
6383 get_dest_uid (rtx label, int max_uid)
6384 {
6385   rtx dest = next_real_insn (label);
6386   int dest_uid;
6387   if (! dest)
6388     /* This can happen for an undefined label.  */
6389     return 0;
6390   dest_uid = INSN_UID (dest);
6391   /* If this is a newly created branch redirection blocking instruction,
6392      we cannot index the branch_uid or insn_addresses arrays with its
6393      uid.  But then, we won't need to, because the actual destination is
6394      the following branch.  */
6395   while (dest_uid >= max_uid)
6396     {
6397       dest = NEXT_INSN (dest);
6398       dest_uid = INSN_UID (dest);
6399     }
6400   if (JUMP_P (dest) && GET_CODE (PATTERN (dest)) == RETURN)
6401     return 0;
6402   return dest_uid;
6403 }
6404
6405 /* Split condbranches that are out of range.  Also add clobbers for
6406    scratch registers that are needed in far jumps.
6407    We do this before delay slot scheduling, so that it can take our
6408    newly created instructions into account.  It also allows us to
6409    find branches with common targets more easily.  */
6410 static void
6411 split_branches (rtx first)
6412 {
6413   rtx insn;
6414   struct far_branch **uid_branch, *far_branch_list = 0;
6415   int max_uid = get_max_uid ();
6416   int ok;
6417
6418   /* Find out which branches are out of range.  */
6419   shorten_branches (first);
6420
6421   uid_branch = (struct far_branch **) alloca (max_uid * sizeof *uid_branch);
6422   memset ((char *) uid_branch, 0, max_uid * sizeof *uid_branch);
6423
6424   for (insn = first; insn; insn = NEXT_INSN (insn))
6425     if (! INSN_P (insn))
6426       continue;
6427     else if (INSN_DELETED_P (insn))
6428       {
6429         /* Shorten_branches would split this instruction again,
6430            so transform it into a note.  */
6431         SET_INSN_DELETED (insn);
6432       }
6433     else if (JUMP_P (insn))
6434       {
6435         enum attr_type type = get_attr_type (insn);
6436         if (type == TYPE_CBRANCH)
6437           {
6438             rtx next, beyond;
6439
6440             if (get_attr_length (insn) > 4)
6441               {
6442                 rtx src = SET_SRC (PATTERN (insn));
6443                 rtx olabel = XEXP (XEXP (src, 1), 0);
6444                 int addr = INSN_ADDRESSES (INSN_UID (insn));
6445                 rtx label = 0;
6446                 int dest_uid = get_dest_uid (olabel, max_uid);
6447                 struct far_branch *bp = uid_branch[dest_uid];
6448
6449                 /* redirect_jump needs a valid JUMP_LABEL, and it might delete
6450                    the label if the LABEL_NUSES count drops to zero.  There is
6451                    always a jump_optimize pass that sets these values, but it
6452                    proceeds to delete unreferenced code, and then if not
6453                    optimizing, to un-delete the deleted instructions, thus
6454                    leaving labels with too low uses counts.  */
6455                 if (! optimize)
6456                   {
6457                     JUMP_LABEL (insn) = olabel;
6458                     LABEL_NUSES (olabel)++;
6459                   }
6460                 if (! bp)
6461                   {
6462                     bp = (struct far_branch *) alloca (sizeof *bp);
6463                     uid_branch[dest_uid] = bp;
6464                     bp->prev = far_branch_list;
6465                     far_branch_list = bp;
6466                     bp->far_label
6467                       = XEXP (XEXP (SET_SRC (PATTERN (insn)), 1), 0);
6468                     LABEL_NUSES (bp->far_label)++;
6469                   }
6470                 else
6471                   {
6472                     label = bp->near_label;
6473                     if (! label && bp->address - addr >= CONDJUMP_MIN)
6474                       {
6475                         rtx block = bp->insert_place;
6476
6477                         if (GET_CODE (PATTERN (block)) == RETURN)
6478                           block = PREV_INSN (block);
6479                         else
6480                           block = gen_block_redirect (block,
6481                                                       bp->address, 2);
6482                         label = emit_label_after (gen_label_rtx (),
6483                                                   PREV_INSN (block));
6484                         bp->near_label = label;
6485                       }
6486                     else if (label && ! NEXT_INSN (label))
6487                       {
6488                         if (addr + 2 - bp->address <= CONDJUMP_MAX)
6489                           bp->insert_place = insn;
6490                         else
6491                           gen_far_branch (bp);
6492                       }
6493                   }
6494                 if (! label
6495                     || (NEXT_INSN (label) && bp->address - addr < CONDJUMP_MIN))
6496                   {
6497                     bp->near_label = label = gen_label_rtx ();
6498                     bp->insert_place = insn;
6499                     bp->address = addr;
6500                   }
6501                 ok = redirect_jump (insn, label, 0);
6502                 gcc_assert (ok);
6503               }
6504             else
6505               {
6506                 /* get_attr_length (insn) == 2 */
6507                 /* Check if we have a pattern where reorg wants to redirect
6508                    the branch to a label from an unconditional branch that
6509                    is too far away.  */
6510                 /* We can't use JUMP_LABEL here because it might be undefined
6511                    when not optimizing.  */
6512                 /* A syntax error might cause beyond to be NULL_RTX.  */
6513                 beyond
6514                   = next_active_insn (XEXP (XEXP (SET_SRC (PATTERN (insn)), 1),
6515                                             0));
6516
6517                 if (beyond
6518                     && (JUMP_P (beyond)
6519                         || ((beyond = next_active_insn (beyond))
6520                             && JUMP_P (beyond)))
6521                     && GET_CODE (PATTERN (beyond)) == SET
6522                     && recog_memoized (beyond) == CODE_FOR_jump_compact
6523                     && ((INSN_ADDRESSES
6524                          (INSN_UID (XEXP (SET_SRC (PATTERN (beyond)), 0)))
6525                          - INSN_ADDRESSES (INSN_UID (insn)) + (unsigned) 252)
6526                         > 252 + 258 + 2))
6527                   gen_block_redirect (beyond,
6528                                       INSN_ADDRESSES (INSN_UID (beyond)), 1);
6529               }
6530
6531             next = next_active_insn (insn);
6532
6533             if (next
6534                 && (JUMP_P (next)
6535                     || ((next = next_active_insn (next))
6536                         && JUMP_P (next)))
6537                 && GET_CODE (PATTERN (next)) == SET
6538                 && recog_memoized (next) == CODE_FOR_jump_compact
6539                 && ((INSN_ADDRESSES
6540                      (INSN_UID (XEXP (SET_SRC (PATTERN (next)), 0)))
6541                      - INSN_ADDRESSES (INSN_UID (insn)) + (unsigned) 252)
6542                     > 252 + 258 + 2))
6543               gen_block_redirect (next, INSN_ADDRESSES (INSN_UID (next)), 1);
6544           }
6545         else if (type == TYPE_JUMP || type == TYPE_RETURN)
6546           {
6547             int addr = INSN_ADDRESSES (INSN_UID (insn));
6548             rtx far_label = 0;
6549             int dest_uid = 0;
6550             struct far_branch *bp;
6551
6552             if (type == TYPE_JUMP)
6553               {
6554                 far_label = XEXP (SET_SRC (PATTERN (insn)), 0);
6555                 dest_uid = get_dest_uid (far_label, max_uid);
6556                 if (! dest_uid)
6557                   {
6558                     /* Parse errors can lead to labels outside
6559                       the insn stream.  */
6560                     if (! NEXT_INSN (far_label))
6561                       continue;
6562
6563                     if (! optimize)
6564                       {
6565                         JUMP_LABEL (insn) = far_label;
6566                         LABEL_NUSES (far_label)++;
6567                       }
6568                     redirect_jump (insn, ret_rtx, 1);
6569                     far_label = 0;
6570                   }
6571               }
6572             bp = uid_branch[dest_uid];
6573             if (! bp)
6574               {
6575                 bp = (struct far_branch *) alloca (sizeof *bp);
6576                 uid_branch[dest_uid] = bp;
6577                 bp->prev = far_branch_list;
6578                 far_branch_list = bp;
6579                 bp->near_label = 0;
6580                 bp->far_label = far_label;
6581                 if (far_label)
6582                   LABEL_NUSES (far_label)++;
6583               }
6584             else if (bp->near_label && ! NEXT_INSN (bp->near_label))
6585               if (addr - bp->address <= CONDJUMP_MAX)
6586                 emit_label_after (bp->near_label, PREV_INSN (insn));
6587               else
6588                 {
6589                   gen_far_branch (bp);
6590                   bp->near_label = 0;
6591                 }
6592             else
6593               bp->near_label = 0;
6594             bp->address = addr;
6595             bp->insert_place = insn;
6596             if (! far_label)
6597               emit_insn_before (gen_block_branch_redirect (const0_rtx), insn);
6598             else
6599               gen_block_redirect (insn, addr, bp->near_label ? 2 : 0);
6600           }
6601       }
6602   /* Generate all pending far branches,
6603      and free our references to the far labels.  */
6604   while (far_branch_list)
6605     {
6606       if (far_branch_list->near_label
6607           && ! NEXT_INSN (far_branch_list->near_label))
6608         gen_far_branch (far_branch_list);
6609       if (optimize
6610           && far_branch_list->far_label
6611           && ! --LABEL_NUSES (far_branch_list->far_label))
6612         delete_insn (far_branch_list->far_label);
6613       far_branch_list = far_branch_list->prev;
6614     }
6615
6616   /* Instruction length information is no longer valid due to the new
6617      instructions that have been generated.  */
6618   init_insn_lengths ();
6619 }
6620
6621 /* Dump out instruction addresses, which is useful for debugging the
6622    constant pool table stuff.
6623
6624    If relaxing, output the label and pseudo-ops used to link together
6625    calls and the instruction which set the registers.
6626
6627    ??? The addresses printed by this routine for insns are nonsense for
6628    insns which are inside of a sequence where none of the inner insns have
6629    variable length.  This is because the second pass of shorten_branches
6630    does not bother to update them.  */
6631 void
6632 final_prescan_insn (rtx insn, rtx *opvec ATTRIBUTE_UNUSED,
6633                     int noperands ATTRIBUTE_UNUSED)
6634 {
6635   if (TARGET_DUMPISIZE)
6636     fprintf (asm_out_file, "\n! at %04x\n", INSN_ADDRESSES (INSN_UID (insn)));
6637
6638   if (TARGET_RELAX)
6639     {
6640       rtx note;
6641
6642       note = find_reg_note (insn, REG_LABEL_OPERAND, NULL_RTX);
6643       if (note)
6644         {
6645           rtx pattern;
6646
6647           pattern = PATTERN (insn);
6648           if (GET_CODE (pattern) == PARALLEL)
6649             pattern = XVECEXP (pattern, 0, 0);
6650           switch (GET_CODE (pattern))
6651             {
6652             case SET:
6653               if (GET_CODE (SET_SRC (pattern)) != CALL
6654                   && get_attr_type (insn) != TYPE_SFUNC)
6655                 {
6656                   targetm.asm_out.internal_label
6657                     (asm_out_file, "L", CODE_LABEL_NUMBER (XEXP (note, 0)));
6658                   break;
6659                 }
6660               /* else FALLTHROUGH */
6661             case CALL:
6662               asm_fprintf (asm_out_file, "\t.uses %LL%d\n",
6663                            CODE_LABEL_NUMBER (XEXP (note, 0)));
6664               break;
6665
6666             default:
6667               gcc_unreachable ();
6668             }
6669         }
6670     }
6671 }
6672
6673 /* Dump out any constants accumulated in the final pass.  These will
6674    only be labels.  */
6675 const char *
6676 output_jump_label_table (void)
6677 {
6678   int i;
6679
6680   if (pool_size)
6681     {
6682       fprintf (asm_out_file, "\t.align 2\n");
6683       for (i = 0; i < pool_size; i++)
6684         {
6685           pool_node *p = &pool_vector[i];
6686
6687           (*targetm.asm_out.internal_label) (asm_out_file, "L",
6688                                      CODE_LABEL_NUMBER (p->label));
6689           output_asm_insn (".long       %O0", &p->value);
6690         }
6691       pool_size = 0;
6692     }
6693
6694   return "";
6695 }
6696 \f
6697 /* A full frame looks like:
6698
6699    arg-5
6700    arg-4
6701    [ if current_function_anonymous_args
6702    arg-3
6703    arg-2
6704    arg-1
6705    arg-0 ]
6706    saved-fp
6707    saved-r10
6708    saved-r11
6709    saved-r12
6710    saved-pr
6711    local-n
6712    ..
6713    local-1
6714    local-0        <- fp points here.
6715
6716    Number of bytes pushed for anonymous args, used to pass information
6717    between expand_prologue and expand_epilogue.
6718
6719    Adjust the stack by SIZE bytes.  REG holds the rtl of the register to be
6720    adjusted.  If epilogue_p is zero, this is for a prologue; otherwise, it's
6721    for an epilogue and a negative value means that it's for a sibcall
6722    epilogue.  If LIVE_REGS_MASK is nonzero, it points to a HARD_REG_SET of
6723    all the registers that are about to be restored, and hence dead.  */
6724 static void
6725 output_stack_adjust (int size, rtx reg, int epilogue_p,
6726                      HARD_REG_SET *live_regs_mask, bool frame_p)
6727 {
6728   rtx (*emit_fn) (rtx) = frame_p ? &frame_insn : &emit_insn;
6729   if (size)
6730     {
6731       HOST_WIDE_INT align = STACK_BOUNDARY / BITS_PER_UNIT;
6732
6733 /* This test is bogus, as output_stack_adjust is used to re-align the
6734    stack.  */
6735 #if 0
6736       gcc_assert (!(size % align));
6737 #endif
6738
6739       if (CONST_OK_FOR_ADD (size))
6740         emit_fn (GEN_ADD3 (reg, reg, GEN_INT (size)));
6741       /* Try to do it with two partial adjustments; however, we must make
6742          sure that the stack is properly aligned at all times, in case
6743          an interrupt occurs between the two partial adjustments.  */
6744       else if (CONST_OK_FOR_ADD (size / 2 & -align)
6745                && CONST_OK_FOR_ADD (size - (size / 2 & -align)))
6746         {
6747           emit_fn (GEN_ADD3 (reg, reg, GEN_INT (size / 2 & -align)));
6748           emit_fn (GEN_ADD3 (reg, reg, GEN_INT (size - (size / 2 & -align))));
6749         }
6750       else
6751         {
6752           rtx const_reg;
6753           rtx insn;
6754           int temp = epilogue_p ? 7 : (TARGET_SH5 ? 0 : 1);
6755           int i;
6756
6757           /* If TEMP is invalid, we could temporarily save a general
6758              register to MACL.  However, there is currently no need
6759              to handle this case, so just die when we see it.  */
6760           if (epilogue_p < 0
6761               || current_function_interrupt
6762               || ! call_really_used_regs[temp] || fixed_regs[temp])
6763             temp = -1;
6764           if (temp < 0 && ! current_function_interrupt
6765               && (TARGET_SHMEDIA || epilogue_p >= 0))
6766             {
6767               HARD_REG_SET temps;
6768               COPY_HARD_REG_SET (temps, call_used_reg_set);
6769               AND_COMPL_HARD_REG_SET (temps, call_fixed_reg_set);
6770               if (epilogue_p > 0)
6771                 {
6772                   int nreg = 0;
6773                   if (crtl->return_rtx)
6774                     {
6775                       enum machine_mode mode;
6776                       mode = GET_MODE (crtl->return_rtx);
6777                       if (BASE_RETURN_VALUE_REG (mode) == FIRST_RET_REG)
6778                         nreg = HARD_REGNO_NREGS (FIRST_RET_REG, mode);
6779                     }
6780                   for (i = 0; i < nreg; i++)
6781                     CLEAR_HARD_REG_BIT (temps, FIRST_RET_REG + i);
6782                   if (crtl->calls_eh_return)
6783                     {
6784                       CLEAR_HARD_REG_BIT (temps, EH_RETURN_STACKADJ_REGNO);
6785                       for (i = 0; i <= 3; i++)
6786                         CLEAR_HARD_REG_BIT (temps, EH_RETURN_DATA_REGNO (i));
6787                     }
6788                 }
6789               if (TARGET_SHMEDIA && epilogue_p < 0)
6790                 for (i = FIRST_TARGET_REG; i <= LAST_TARGET_REG; i++)
6791                   CLEAR_HARD_REG_BIT (temps, i);
6792               if (epilogue_p <= 0)
6793                 {
6794                   for (i = FIRST_PARM_REG;
6795                        i < FIRST_PARM_REG + NPARM_REGS (SImode); i++)
6796                     CLEAR_HARD_REG_BIT (temps, i);
6797                   if (cfun->static_chain_decl != NULL)
6798                     CLEAR_HARD_REG_BIT (temps, STATIC_CHAIN_REGNUM);
6799                 }
6800               temp = scavenge_reg (&temps);
6801             }
6802           if (temp < 0 && live_regs_mask)
6803             {
6804               HARD_REG_SET temps;
6805
6806               COPY_HARD_REG_SET (temps, *live_regs_mask);
6807               CLEAR_HARD_REG_BIT (temps, REGNO (reg));
6808               temp = scavenge_reg (&temps);
6809             }
6810           if (temp < 0)
6811             {
6812               rtx adj_reg, tmp_reg, mem;
6813               
6814               /* If we reached here, the most likely case is the (sibcall)
6815                  epilogue for non SHmedia.  Put a special push/pop sequence
6816                  for such case as the last resort.  This looks lengthy but
6817                  would not be problem because it seems to be very
6818                  rare.  */
6819               
6820               gcc_assert (!TARGET_SHMEDIA && epilogue_p);
6821               
6822
6823                /* ??? There is still the slight possibility that r4 or
6824                   r5 have been reserved as fixed registers or assigned
6825                   as global registers, and they change during an
6826                   interrupt.  There are possible ways to handle this:
6827                      
6828                   - If we are adjusting the frame pointer (r14), we can do
6829                     with a single temp register and an ordinary push / pop
6830                     on the stack.
6831                   - Grab any call-used or call-saved registers (i.e. not
6832                     fixed or globals) for the temps we need.  We might
6833                     also grab r14 if we are adjusting the stack pointer.
6834                     If we can't find enough available registers, issue
6835                     a diagnostic and die - the user must have reserved
6836                     way too many registers.
6837                  But since all this is rather unlikely to happen and
6838                  would require extra testing, we just die if r4 / r5
6839                  are not available.  */
6840               gcc_assert (!fixed_regs[4] && !fixed_regs[5]
6841                           && !global_regs[4] && !global_regs[5]);
6842
6843               adj_reg = gen_rtx_REG (GET_MODE (reg), 4);
6844               tmp_reg = gen_rtx_REG (GET_MODE (reg), 5);
6845               emit_move_insn (gen_tmp_stack_mem (Pmode, reg), adj_reg);
6846               emit_insn (GEN_MOV (adj_reg, GEN_INT (size)));
6847               emit_insn (GEN_ADD3 (adj_reg, adj_reg, reg));
6848               mem = gen_tmp_stack_mem (Pmode, gen_rtx_PRE_DEC (Pmode, adj_reg));
6849               emit_move_insn (mem, tmp_reg);
6850               emit_move_insn (tmp_reg, gen_tmp_stack_mem (Pmode, reg));
6851               mem = gen_tmp_stack_mem (Pmode, gen_rtx_PRE_DEC (Pmode, adj_reg));
6852               emit_move_insn (mem, tmp_reg);
6853               emit_move_insn (reg, adj_reg);
6854               mem = gen_tmp_stack_mem (Pmode, gen_rtx_POST_INC (Pmode, reg));
6855               emit_move_insn (adj_reg, mem);
6856               mem = gen_tmp_stack_mem (Pmode, gen_rtx_POST_INC (Pmode, reg));
6857               emit_move_insn (tmp_reg, mem);
6858               /* Tell flow the insns that pop r4/r5 aren't dead.  */
6859               emit_use (tmp_reg);
6860               emit_use (adj_reg);
6861               return;
6862             }
6863           const_reg = gen_rtx_REG (GET_MODE (reg), temp);
6864
6865           /* If SIZE is negative, subtract the positive value.
6866              This sometimes allows a constant pool entry to be shared
6867              between prologue and epilogue code.  */
6868           if (size < 0)
6869             {
6870               emit_insn (GEN_MOV (const_reg, GEN_INT (-size)));
6871               insn = emit_fn (GEN_SUB3 (reg, reg, const_reg));
6872             }
6873           else
6874             {
6875               emit_insn (GEN_MOV (const_reg, GEN_INT (size)));
6876               insn = emit_fn (GEN_ADD3 (reg, reg, const_reg));
6877             }
6878           add_reg_note (insn, REG_FRAME_RELATED_EXPR,
6879                         gen_rtx_SET (VOIDmode, reg,
6880                                      gen_rtx_PLUS (SImode, reg,
6881                                                    GEN_INT (size))));
6882         }
6883     }
6884 }
6885
6886 /* Emit the specified insn and mark it as frame related.
6887    FIXME: Rename this to emit_frame_insn.  */
6888 static rtx
6889 frame_insn (rtx x)
6890 {
6891   x = emit_insn (x);
6892   RTX_FRAME_RELATED_P (x) = 1;
6893   return x;
6894 }
6895
6896 /* Output RTL to push register RN onto the stack.  */
6897 static rtx
6898 push (int rn)
6899 {
6900   rtx x;
6901   if (rn == FPUL_REG)
6902     x = gen_push_fpul ();
6903   else if (rn == FPSCR_REG)
6904     x = gen_push_fpscr ();
6905   else if ((TARGET_SH4 || TARGET_SH2A_DOUBLE) && TARGET_FMOVD
6906            && ! TARGET_FPU_SINGLE && FP_OR_XD_REGISTER_P (rn))
6907     {
6908       if (FP_REGISTER_P (rn) && (rn - FIRST_FP_REG) & 1)
6909         return NULL_RTX;
6910       x = gen_push_4 (gen_rtx_REG (DFmode, rn));
6911     }
6912   else if (TARGET_SH2E && FP_REGISTER_P (rn))
6913     x = gen_push_e (gen_rtx_REG (SFmode, rn));
6914   else
6915     x = gen_push (gen_rtx_REG (SImode, rn));
6916
6917   x = frame_insn (x);
6918   add_reg_note (x, REG_INC, gen_rtx_REG (SImode, STACK_POINTER_REGNUM));
6919   return x;
6920 }
6921
6922 /* Output RTL to pop register RN from the stack.  */
6923 static void
6924 pop (int rn)
6925 {
6926   rtx x, sp_reg, reg;
6927   if (rn == FPUL_REG)
6928     x = gen_pop_fpul ();
6929   else if (rn == FPSCR_REG)
6930     x = gen_pop_fpscr ();
6931   else if ((TARGET_SH4 || TARGET_SH2A_DOUBLE) && TARGET_FMOVD
6932            && ! TARGET_FPU_SINGLE && FP_OR_XD_REGISTER_P (rn))
6933     {
6934       if (FP_REGISTER_P (rn) && (rn - FIRST_FP_REG) & 1)
6935         return;
6936       x = gen_pop_4 (gen_rtx_REG (DFmode, rn));
6937     }
6938   else if (TARGET_SH2E && FP_REGISTER_P (rn))
6939     x = gen_pop_e (gen_rtx_REG (SFmode, rn));
6940   else
6941     x = gen_pop (gen_rtx_REG (SImode, rn));
6942
6943   x = emit_insn (x);
6944
6945   sp_reg = gen_rtx_REG (SImode, STACK_POINTER_REGNUM);
6946   reg = copy_rtx (GET_CODE (PATTERN (x)) == PARALLEL
6947                   ? SET_DEST (XVECEXP (PATTERN (x), 0, 0))
6948                   : SET_DEST (PATTERN (x)));
6949   add_reg_note (x, REG_CFA_RESTORE, reg);
6950   add_reg_note (x, REG_CFA_ADJUST_CFA,
6951                 gen_rtx_SET (SImode, sp_reg,
6952                              plus_constant (SImode, sp_reg,
6953                                             GET_MODE_SIZE (GET_MODE (reg)))));
6954   add_reg_note (x, REG_INC, gen_rtx_REG (SImode, STACK_POINTER_REGNUM));
6955   RTX_FRAME_RELATED_P (x) = 1;
6956 }
6957
6958 /* Generate code to push the regs specified in the mask.  */
6959 static void
6960 push_regs (HARD_REG_SET *mask, int interrupt_handler)
6961 {
6962   int i = interrupt_handler ? LAST_BANKED_REG + 1 : 0;
6963   int skip_fpscr = 0;
6964
6965   /* Push PR last; this gives better latencies after the prologue, and
6966      candidates for the return delay slot when there are no general
6967      registers pushed.  */
6968   for (; i < FIRST_PSEUDO_REGISTER; i++)
6969     {
6970       /* If this is an interrupt handler, and the SZ bit varies,
6971          and we have to push any floating point register, we need
6972          to switch to the correct precision first.  */
6973       if (i == FIRST_FP_REG && interrupt_handler && TARGET_FMOVD
6974           && hard_reg_set_intersect_p (*mask, reg_class_contents[DF_REGS]))
6975         {
6976           HARD_REG_SET unsaved;
6977
6978           push (FPSCR_REG);
6979           COMPL_HARD_REG_SET (unsaved, *mask);
6980           fpscr_set_from_mem (NORMAL_MODE (FP_MODE), unsaved);
6981           skip_fpscr = 1;
6982         }
6983       if (i != PR_REG
6984           && (i != FPSCR_REG || ! skip_fpscr)
6985           && TEST_HARD_REG_BIT (*mask, i))
6986         {
6987         /* If the ISR has RESBANK attribute assigned, don't push any of
6988            the following registers - R0-R14, MACH, MACL and GBR.  */
6989       if (! (sh_cfun_resbank_handler_p ()
6990              && ((i >= FIRST_GENERAL_REG && i < LAST_GENERAL_REG)
6991                  || i == MACH_REG
6992                  || i == MACL_REG
6993                  || i == GBR_REG)))
6994           push (i);
6995         }
6996     }
6997
6998   /* Push banked registers last to improve delay slot opportunities.  */
6999   if (interrupt_handler)
7000     {
7001       bool use_movml = false;
7002
7003       if (TARGET_SH2A)
7004         {
7005           unsigned int count = 0;
7006
7007           for (i = FIRST_BANKED_REG; i <= LAST_BANKED_REG; i++)
7008             if (TEST_HARD_REG_BIT (*mask, i))
7009               count++;
7010             else
7011               break;
7012
7013           /* Use movml when all banked registers are pushed.  */
7014           if (count == LAST_BANKED_REG - FIRST_BANKED_REG + 1)
7015             use_movml = true;
7016         }
7017
7018       if (sh_cfun_resbank_handler_p ())
7019         ; /* Do nothing.  */
7020       else if (use_movml)
7021         {
7022           rtx x, mem, reg, set;
7023           rtx sp_reg = gen_rtx_REG (SImode, STACK_POINTER_REGNUM);
7024
7025           /* We must avoid scheduling multiple store insn with another
7026              insns.  */
7027           emit_insn (gen_blockage ());
7028           x = gen_movml_push_banked (sp_reg);
7029           x = frame_insn (x);
7030           for (i = FIRST_BANKED_REG; i <= LAST_BANKED_REG; i++)
7031             {
7032               mem = gen_rtx_MEM (SImode, plus_constant (Pmode, sp_reg, i * 4));
7033               reg = gen_rtx_REG (SImode, i);
7034               add_reg_note (x, REG_CFA_OFFSET, gen_rtx_SET (SImode, mem, reg));
7035             }
7036
7037           set = gen_rtx_SET (SImode, sp_reg,
7038                              plus_constant (Pmode, sp_reg, - 32));
7039           add_reg_note (x, REG_CFA_ADJUST_CFA, set);
7040           emit_insn (gen_blockage ());
7041         }
7042       else
7043         for (i = FIRST_BANKED_REG; i <= LAST_BANKED_REG; i++)
7044           if (TEST_HARD_REG_BIT (*mask, i))
7045             push (i);
7046     }
7047
7048   /* Don't push PR register for an ISR with RESBANK attribute assigned.  */
7049   if (TEST_HARD_REG_BIT (*mask, PR_REG) && !sh_cfun_resbank_handler_p ())
7050     push (PR_REG);
7051 }
7052
7053 /* Calculate how much extra space is needed to save all callee-saved
7054    target registers.
7055    LIVE_REGS_MASK is the register mask calculated by calc_live_regs.  */
7056 static int
7057 shmedia_target_regs_stack_space (HARD_REG_SET *live_regs_mask)
7058 {
7059   int reg;
7060   int stack_space = 0;
7061   int interrupt_handler = sh_cfun_interrupt_handler_p ();
7062
7063   for (reg = LAST_TARGET_REG; reg >= FIRST_TARGET_REG; reg--)
7064     if ((! call_really_used_regs[reg] || interrupt_handler)
7065         && ! TEST_HARD_REG_BIT (*live_regs_mask, reg))
7066       /* Leave space to save this target register on the stack,
7067          in case target register allocation wants to use it.  */
7068       stack_space += GET_MODE_SIZE (REGISTER_NATURAL_MODE (reg));
7069   return stack_space;
7070 }
7071
7072 /* Decide whether we should reserve space for callee-save target registers,
7073    in case target register allocation wants to use them.  REGS_SAVED is
7074    the space, in bytes, that is already required for register saves.
7075    LIVE_REGS_MASK is the register mask calculated by calc_live_regs.  */
7076 static int
7077 shmedia_reserve_space_for_target_registers_p (int regs_saved,
7078                                               HARD_REG_SET *live_regs_mask)
7079 {
7080   if (optimize_size)
7081     return 0;
7082   return shmedia_target_regs_stack_space (live_regs_mask) <= regs_saved;
7083 }
7084
7085 /* Decide how much space to reserve for callee-save target registers
7086    in case target register allocation wants to use them.
7087    LIVE_REGS_MASK is the register mask calculated by calc_live_regs.  */
7088 static int
7089 shmedia_target_regs_stack_adjust (HARD_REG_SET *live_regs_mask)
7090 {
7091   if (shmedia_space_reserved_for_target_registers)
7092     return shmedia_target_regs_stack_space (live_regs_mask);
7093   else
7094     return 0;
7095 }
7096
7097 /* Work out the registers which need to be saved, both as a mask and a
7098    count of saved words.  Return the count.
7099
7100    If doing a pragma interrupt function, then push all regs used by the
7101    function, and if we call another function (we can tell by looking at PR),
7102    make sure that all the regs it clobbers are safe too.  */
7103 static int
7104 calc_live_regs (HARD_REG_SET *live_regs_mask)
7105 {
7106   unsigned int reg;
7107   int count;
7108   tree attrs;
7109   bool interrupt_or_trapa_handler, trapa_handler, interrupt_handler;
7110   bool nosave_low_regs;
7111   int pr_live, has_call;
7112
7113   attrs = DECL_ATTRIBUTES (current_function_decl);
7114   interrupt_or_trapa_handler = sh_cfun_interrupt_handler_p ();
7115   trapa_handler = lookup_attribute ("trapa_handler", attrs) != NULL_TREE;
7116   interrupt_handler = interrupt_or_trapa_handler && ! trapa_handler;
7117   nosave_low_regs = lookup_attribute ("nosave_low_regs", attrs) != NULL_TREE;
7118
7119   CLEAR_HARD_REG_SET (*live_regs_mask);
7120   if ((TARGET_SH4 || TARGET_SH2A_DOUBLE) && TARGET_FMOVD && interrupt_handler
7121       && df_regs_ever_live_p (FPSCR_REG))
7122     target_flags &= ~MASK_FPU_SINGLE;
7123   /* If we can save a lot of saves by switching to double mode, do that.  */
7124   else if ((TARGET_SH4 || TARGET_SH2A_DOUBLE) && TARGET_FMOVD
7125            && TARGET_FPU_SINGLE)
7126     for (count = 0, reg = FIRST_FP_REG; reg <= LAST_FP_REG; reg += 2)
7127       if (df_regs_ever_live_p (reg) && df_regs_ever_live_p (reg+1)
7128           && (! call_really_used_regs[reg]
7129               || interrupt_handler)
7130           && ++count > 2)
7131         {
7132           target_flags &= ~MASK_FPU_SINGLE;
7133           break;
7134         }
7135   /* PR_MEDIA_REG is a general purpose register, thus global_alloc already
7136      knows how to use it.  That means the pseudo originally allocated for
7137      the initial value can become the PR_MEDIA_REG hard register, as seen for
7138      execute/20010122-1.c:test9.  */
7139   if (TARGET_SHMEDIA)
7140     /* ??? this function is called from initial_elimination_offset, hence we
7141        can't use the result of sh_media_register_for_return here.  */
7142     pr_live = sh_pr_n_sets ();
7143   else
7144     {
7145       rtx pr_initial = has_hard_reg_initial_val (Pmode, PR_REG);
7146       pr_live = (pr_initial
7147                  ? (!REG_P (pr_initial)
7148                     || REGNO (pr_initial) != (PR_REG))
7149                  : df_regs_ever_live_p (PR_REG));
7150       /* For Shcompact, if not optimizing, we end up with a memory reference
7151          using the return address pointer for __builtin_return_address even
7152          though there is no actual need to put the PR register on the stack.  */
7153       pr_live |= df_regs_ever_live_p (RETURN_ADDRESS_POINTER_REGNUM);
7154     }
7155   /* Force PR to be live if the prologue has to call the SHmedia
7156      argument decoder or register saver.  */
7157   if (TARGET_SHCOMPACT
7158       && ((crtl->args.info.call_cookie
7159            & ~ CALL_COOKIE_RET_TRAMP (1))
7160           || crtl->saves_all_registers))
7161     pr_live = 1;
7162   has_call = TARGET_SHMEDIA ? ! leaf_function_p () : pr_live;
7163   for (count = 0, reg = FIRST_PSEUDO_REGISTER; reg-- != 0; )
7164     {
7165       if (reg == (TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG)
7166           ? pr_live
7167           : interrupt_handler
7168           ? (/* Need to save all the regs ever live.  */
7169              (df_regs_ever_live_p (reg)
7170               || (call_really_used_regs[reg]
7171                   && (! fixed_regs[reg] || reg == MACH_REG || reg == MACL_REG
7172                       || reg == PIC_OFFSET_TABLE_REGNUM)
7173                   && has_call)
7174               || (TARGET_SHMEDIA && has_call
7175                   && REGISTER_NATURAL_MODE (reg) == SImode
7176                   && (GENERAL_REGISTER_P (reg) || TARGET_REGISTER_P (reg))))
7177              && reg != STACK_POINTER_REGNUM && reg != ARG_POINTER_REGNUM
7178              && reg != RETURN_ADDRESS_POINTER_REGNUM
7179              && reg != T_REG && reg != GBR_REG
7180              /* Push fpscr only on targets which have FPU */
7181              && (reg != FPSCR_REG || TARGET_FPU_ANY))
7182           : (/* Only push those regs which are used and need to be saved.  */
7183              (TARGET_SHCOMPACT
7184               && flag_pic
7185               && crtl->args.info.call_cookie
7186               && reg == PIC_OFFSET_TABLE_REGNUM)
7187              || (df_regs_ever_live_p (reg)
7188                  && ((!call_really_used_regs[reg]
7189                       && !(reg != PIC_OFFSET_TABLE_REGNUM
7190                            && fixed_regs[reg] && call_used_regs[reg]))
7191                      || (trapa_handler && reg == FPSCR_REG && TARGET_FPU_ANY)))
7192              || (crtl->calls_eh_return
7193                  && (reg == EH_RETURN_DATA_REGNO (0)
7194                      || reg == EH_RETURN_DATA_REGNO (1)
7195                      || reg == EH_RETURN_DATA_REGNO (2)
7196                      || reg == EH_RETURN_DATA_REGNO (3)))
7197              || ((reg == MACL_REG || reg == MACH_REG)
7198                  && df_regs_ever_live_p (reg)
7199                  && sh_cfun_attr_renesas_p ())
7200              ))
7201         {
7202           SET_HARD_REG_BIT (*live_regs_mask, reg);
7203           count += GET_MODE_SIZE (REGISTER_NATURAL_MODE (reg));
7204
7205           if ((TARGET_SH4 || TARGET_SH2A_DOUBLE || TARGET_SH5) && TARGET_FMOVD
7206               && GET_MODE_CLASS (REGISTER_NATURAL_MODE (reg)) == MODE_FLOAT)
7207             {
7208               if (FP_REGISTER_P (reg))
7209                 {
7210                   if (! TARGET_FPU_SINGLE && ! df_regs_ever_live_p (reg ^ 1))
7211                     {
7212                       SET_HARD_REG_BIT (*live_regs_mask, (reg ^ 1));
7213                       count += GET_MODE_SIZE (REGISTER_NATURAL_MODE (reg ^ 1));
7214                     }
7215                 }
7216               else if (XD_REGISTER_P (reg))
7217                 {
7218                   /* Must switch to double mode to access these registers.  */
7219                   target_flags &= ~MASK_FPU_SINGLE;
7220                 }
7221             }
7222         }
7223       if (nosave_low_regs && reg == R8_REG)
7224         break;
7225     }
7226   /* If we have a target register optimization pass after prologue / epilogue
7227      threading, we need to assume all target registers will be live even if
7228      they aren't now.  */
7229   if (flag_branch_target_load_optimize2
7230       && TARGET_SAVE_ALL_TARGET_REGS
7231       && shmedia_space_reserved_for_target_registers)
7232     for (reg = LAST_TARGET_REG; reg >= FIRST_TARGET_REG; reg--)
7233       if ((! call_really_used_regs[reg] || interrupt_handler)
7234           && ! TEST_HARD_REG_BIT (*live_regs_mask, reg))
7235         {
7236           SET_HARD_REG_BIT (*live_regs_mask, reg);
7237           count += GET_MODE_SIZE (REGISTER_NATURAL_MODE (reg));
7238         }
7239   /* If this is an interrupt handler, we don't have any call-clobbered
7240      registers we can conveniently use for target register save/restore.
7241      Make sure we save at least one general purpose register when we need
7242      to save target registers.  */
7243   if (interrupt_handler
7244       && hard_reg_set_intersect_p (*live_regs_mask,
7245                                    reg_class_contents[TARGET_REGS])
7246       && ! hard_reg_set_intersect_p (*live_regs_mask,
7247                                      reg_class_contents[GENERAL_REGS]))
7248     {
7249       SET_HARD_REG_BIT (*live_regs_mask, R0_REG);
7250       count += GET_MODE_SIZE (REGISTER_NATURAL_MODE (R0_REG));
7251     }
7252
7253   return count;
7254 }
7255
7256 /* Code to generate prologue and epilogue sequences */
7257
7258 /* PUSHED is the number of bytes that are being pushed on the
7259    stack for register saves.  Return the frame size, padded
7260    appropriately so that the stack stays properly aligned.  */
7261 static HOST_WIDE_INT
7262 rounded_frame_size (int pushed)
7263 {
7264   HOST_WIDE_INT size = get_frame_size ();
7265   HOST_WIDE_INT align = STACK_BOUNDARY / BITS_PER_UNIT;
7266
7267   if (ACCUMULATE_OUTGOING_ARGS)
7268     size += crtl->outgoing_args_size;
7269
7270   return ((size + pushed + align - 1) & -align) - pushed;
7271 }
7272
7273 /* Choose a call-clobbered target-branch register that remains
7274    unchanged along the whole function.  We set it up as the return
7275    value in the prologue.  */
7276 int
7277 sh_media_register_for_return (void)
7278 {
7279   int regno;
7280   int tr0_used;
7281
7282   if (! crtl->is_leaf)
7283     return -1;
7284   if (lookup_attribute ("interrupt_handler",
7285                         DECL_ATTRIBUTES (current_function_decl)))
7286     return -1;
7287   if (sh_cfun_interrupt_handler_p ())
7288     return -1;
7289
7290   tr0_used = flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM);
7291
7292   for (regno = FIRST_TARGET_REG + tr0_used; regno <= LAST_TARGET_REG; regno++)
7293     if (call_really_used_regs[regno] && ! df_regs_ever_live_p (regno))
7294       return regno;
7295
7296   return -1;
7297 }
7298
7299 /* The maximum registers we need to save are:
7300    - 62 general purpose registers (r15 is stack pointer, r63 is zero)
7301    - 32 floating point registers (for each pair, we save none,
7302          one single precision value, or a double precision value).
7303    -  8 target registers
7304    -  add 1 entry for a delimiter.  */
7305 #define MAX_SAVED_REGS (62+32+8)
7306
7307 typedef struct save_entry_s
7308 {
7309   unsigned char reg;
7310   unsigned char mode;
7311   short offset;
7312 } save_entry;
7313
7314 #define MAX_TEMPS 4
7315
7316 /* There will be a delimiter entry with VOIDmode both at the start and the
7317    end of a filled in schedule.  The end delimiter has the offset of the
7318    save with the smallest (i.e. most negative) offset.  */
7319 typedef struct save_schedule_s
7320 {
7321   save_entry entries[MAX_SAVED_REGS + 2];
7322   int temps[MAX_TEMPS+1];
7323 } save_schedule;
7324
7325 /* Fill in SCHEDULE according to LIVE_REGS_MASK.  If RESTORE is nonzero,
7326    use reverse order.  Returns the last entry written to (not counting
7327    the delimiter).  OFFSET_BASE is a number to be added to all offset
7328    entries.  */
7329 static save_entry *
7330 sh5_schedule_saves (HARD_REG_SET *live_regs_mask, save_schedule *schedule,
7331                     int offset_base)
7332 {
7333   int align, i;
7334   save_entry *entry = schedule->entries;
7335   int tmpx = 0;
7336   int offset;
7337
7338   if (! current_function_interrupt)
7339     for (i = FIRST_GENERAL_REG; tmpx < MAX_TEMPS && i <= LAST_GENERAL_REG; i++)
7340       if (call_really_used_regs[i] && ! fixed_regs[i] && i != PR_MEDIA_REG
7341           && ! FUNCTION_ARG_REGNO_P (i)
7342           && i != FIRST_RET_REG
7343           && ! (cfun->static_chain_decl != NULL && i == STATIC_CHAIN_REGNUM)
7344           && ! (crtl->calls_eh_return
7345                 && (i == EH_RETURN_STACKADJ_REGNO
7346                     || ((unsigned) i >= EH_RETURN_DATA_REGNO (0)
7347                         && (unsigned) i <= EH_RETURN_DATA_REGNO (3)))))
7348         schedule->temps[tmpx++] = i;
7349   entry->reg = -1;
7350   entry->mode = VOIDmode;
7351   entry->offset = offset_base;
7352   entry++;
7353   /* We loop twice: first, we save 8-byte aligned registers in the
7354      higher addresses, that are known to be aligned.  Then, we
7355      proceed to saving 32-bit registers that don't need 8-byte
7356      alignment.
7357      If this is an interrupt function, all registers that need saving
7358      need to be saved in full.  moreover, we need to postpone saving
7359      target registers till we have saved some general purpose registers
7360      we can then use as scratch registers.  */
7361   offset = offset_base;
7362   for (align = 1; align >= 0; align--)
7363     {
7364       for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
7365         if (TEST_HARD_REG_BIT (*live_regs_mask, i))
7366           {
7367             enum machine_mode mode = REGISTER_NATURAL_MODE (i);
7368             int reg = i;
7369
7370             if (current_function_interrupt)
7371               {
7372                 if (TARGET_REGISTER_P (i))
7373                   continue;
7374                 if (GENERAL_REGISTER_P (i))
7375                   mode = DImode;
7376               }
7377             if (mode == SFmode && (i % 2) == 1
7378                 && ! TARGET_FPU_SINGLE && FP_REGISTER_P (i)
7379                 && (TEST_HARD_REG_BIT (*live_regs_mask, (i ^ 1))))
7380               {
7381                 mode = DFmode;
7382                 i--;
7383                 reg--;
7384               }
7385
7386             /* If we're doing the aligned pass and this is not aligned,
7387                or we're doing the unaligned pass and this is aligned,
7388                skip it.  */
7389             if ((GET_MODE_SIZE (mode) % (STACK_BOUNDARY / BITS_PER_UNIT) == 0)
7390                 != align)
7391               continue;
7392
7393             if (current_function_interrupt
7394                 && GENERAL_REGISTER_P (i)
7395                 && tmpx < MAX_TEMPS)
7396               schedule->temps[tmpx++] = i;
7397
7398             offset -= GET_MODE_SIZE (mode);
7399             entry->reg = i;
7400             entry->mode = mode;
7401             entry->offset = offset;
7402             entry++;
7403           }
7404       if (align && current_function_interrupt)
7405         for (i = LAST_TARGET_REG; i >= FIRST_TARGET_REG; i--)
7406           if (TEST_HARD_REG_BIT (*live_regs_mask, i))
7407             {
7408               offset -= GET_MODE_SIZE (DImode);
7409               entry->reg = i;
7410               entry->mode = DImode;
7411               entry->offset = offset;
7412               entry++;
7413             }
7414     }
7415   entry->reg = -1;
7416   entry->mode = VOIDmode;
7417   entry->offset = offset;
7418   schedule->temps[tmpx] = -1;
7419   return entry - 1;
7420 }
7421
7422 /* Expand code for the function prologue.  */
7423 void
7424 sh_expand_prologue (void)
7425 {
7426   HARD_REG_SET live_regs_mask;
7427   int d, i;
7428   int d_rounding = 0;
7429   int save_flags = target_flags;
7430   int pretend_args;
7431   int stack_usage;
7432   tree sp_switch_attr
7433     = lookup_attribute ("sp_switch", DECL_ATTRIBUTES (current_function_decl));
7434
7435   current_function_interrupt = sh_cfun_interrupt_handler_p ();
7436
7437   /* We have pretend args if we had an object sent partially in registers
7438      and partially on the stack, e.g. a large structure.  */
7439   pretend_args = crtl->args.pretend_args_size;
7440   if (TARGET_VARARGS_PRETEND_ARGS (current_function_decl)
7441       && (NPARM_REGS(SImode)
7442           > crtl->args.info.arg_count[(int) SH_ARG_INT]))
7443     pretend_args = 0;
7444
7445   output_stack_adjust (-pretend_args
7446                        - crtl->args.info.stack_regs * 8,
7447                        stack_pointer_rtx, 0, NULL, true);
7448   stack_usage = pretend_args + crtl->args.info.stack_regs * 8;
7449
7450   if (TARGET_SHCOMPACT && flag_pic && crtl->args.info.call_cookie)
7451     /* We're going to use the PIC register to load the address of the
7452        incoming-argument decoder and/or of the return trampoline from
7453        the GOT, so make sure the PIC register is preserved and
7454        initialized.  */
7455     df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
7456
7457   if (TARGET_SHCOMPACT
7458       && (crtl->args.info.call_cookie & ~ CALL_COOKIE_RET_TRAMP(1)))
7459     {
7460       int reg;
7461
7462       /* First, make all registers with incoming arguments that will
7463          be pushed onto the stack live, so that register renaming
7464          doesn't overwrite them.  */
7465       for (reg = 0; reg < NPARM_REGS (SImode); reg++)
7466         if (CALL_COOKIE_STACKSEQ_GET (crtl->args.info.call_cookie)
7467             >= NPARM_REGS (SImode) - reg)
7468           for (; reg < NPARM_REGS (SImode); reg++)
7469             emit_insn (gen_shcompact_preserve_incoming_args
7470                        (gen_rtx_REG (SImode, FIRST_PARM_REG + reg)));
7471         else if (CALL_COOKIE_INT_REG_GET
7472                  (crtl->args.info.call_cookie, reg) == 1)
7473           emit_insn (gen_shcompact_preserve_incoming_args
7474                      (gen_rtx_REG (SImode, FIRST_PARM_REG + reg)));
7475
7476       emit_move_insn (gen_rtx_REG (Pmode, MACL_REG),
7477                       stack_pointer_rtx);
7478       emit_move_insn (gen_rtx_REG (SImode, R0_REG),
7479                       GEN_INT (crtl->args.info.call_cookie));
7480       emit_move_insn (gen_rtx_REG (SImode, MACH_REG),
7481                       gen_rtx_REG (SImode, R0_REG));
7482     }
7483   else if (TARGET_SHMEDIA)
7484     {
7485       int tr = sh_media_register_for_return ();
7486
7487       if (tr >= 0)
7488         emit_move_insn (gen_rtx_REG (DImode, tr),
7489                         gen_rtx_REG (DImode, PR_MEDIA_REG));
7490     }
7491
7492   /* Emit the code for SETUP_VARARGS.  */
7493   if (cfun->stdarg)
7494     {
7495       if (TARGET_VARARGS_PRETEND_ARGS (current_function_decl))
7496         {
7497           /* Push arg regs as if they'd been provided by caller in stack.  */
7498           for (i = 0; i < NPARM_REGS(SImode); i++)
7499             {
7500               int rn = NPARM_REGS(SImode) + FIRST_PARM_REG - i - 1;
7501
7502               if (i >= (NPARM_REGS(SImode)
7503                         - crtl->args.info.arg_count[(int) SH_ARG_INT]
7504                         ))
7505                 break;
7506               push (rn);
7507               stack_usage += GET_MODE_SIZE (SImode);
7508             }
7509         }
7510     }
7511
7512   /* If we're supposed to switch stacks at function entry, do so now.  */
7513   if (sp_switch_attr)
7514     {
7515       rtx lab, newsrc;
7516       /* The argument specifies a variable holding the address of the
7517          stack the interrupt function should switch to/from at entry/exit.  */
7518       tree arg = TREE_VALUE ( TREE_VALUE (sp_switch_attr));
7519       const char *s
7520         = ggc_strdup (TREE_STRING_POINTER (arg));
7521       rtx sp_switch = gen_rtx_SYMBOL_REF (Pmode, s);
7522
7523       lab = add_constant (sp_switch, SImode, 0);
7524       newsrc = gen_rtx_LABEL_REF (VOIDmode, lab);
7525
7526       emit_insn (gen_sp_switch_1 (newsrc));
7527     }
7528
7529   d = calc_live_regs (&live_regs_mask);
7530   /* ??? Maybe we could save some switching if we can move a mode switch
7531      that already happens to be at the function start into the prologue.  */
7532   if (target_flags != save_flags && ! current_function_interrupt)
7533     emit_insn (gen_toggle_sz ());
7534
7535   if (TARGET_SH5)
7536     {
7537       int offset_base, offset;
7538       rtx r0 = NULL_RTX;
7539       int offset_in_r0 = -1;
7540       int sp_in_r0 = 0;
7541       int tregs_space = shmedia_target_regs_stack_adjust (&live_regs_mask);
7542       int total_size, save_size;
7543       save_schedule schedule;
7544       save_entry *entry;
7545       int *tmp_pnt;
7546
7547       if (call_really_used_regs[R0_REG] && ! fixed_regs[R0_REG]
7548           && ! current_function_interrupt)
7549         r0 = gen_rtx_REG (Pmode, R0_REG);
7550
7551       /* D is the actual number of bytes that we need for saving registers,
7552          however, in initial_elimination_offset we have committed to using
7553          an additional TREGS_SPACE amount of bytes - in order to keep both
7554          addresses to arguments supplied by the caller and local variables
7555          valid, we must keep this gap.  Place it between the incoming
7556          arguments and the actually saved registers in a bid to optimize
7557          locality of reference.  */
7558       total_size = d + tregs_space;
7559       total_size += rounded_frame_size (total_size);
7560       save_size = total_size - rounded_frame_size (d);
7561       if (save_size % (STACK_BOUNDARY / BITS_PER_UNIT))
7562         d_rounding = ((STACK_BOUNDARY / BITS_PER_UNIT)
7563                         - save_size % (STACK_BOUNDARY / BITS_PER_UNIT));
7564
7565       /* If adjusting the stack in a single step costs nothing extra, do so.
7566          I.e. either if a single addi is enough, or we need a movi anyway,
7567          and we don't exceed the maximum offset range (the test for the
7568          latter is conservative for simplicity).  */
7569       if (TARGET_SHMEDIA
7570           && (CONST_OK_FOR_I10 (-total_size)
7571               || (! CONST_OK_FOR_I10 (-(save_size + d_rounding))
7572                   && total_size <= 2044)))
7573         d_rounding = total_size - save_size;
7574
7575       offset_base = d + d_rounding;
7576
7577       output_stack_adjust (-(save_size + d_rounding), stack_pointer_rtx,
7578                            0, NULL, true);
7579       stack_usage += save_size + d_rounding;
7580
7581       sh5_schedule_saves (&live_regs_mask, &schedule, offset_base);
7582       tmp_pnt = schedule.temps;
7583       for (entry = &schedule.entries[1]; entry->mode != VOIDmode; entry++)
7584         {
7585           enum machine_mode mode = (enum machine_mode) entry->mode;
7586           unsigned int reg = entry->reg;
7587           rtx reg_rtx, mem_rtx, pre_dec = NULL_RTX;
7588           rtx orig_reg_rtx;
7589
7590           offset = entry->offset;
7591
7592           reg_rtx = gen_rtx_REG (mode, reg);
7593
7594           mem_rtx = gen_frame_mem (mode,
7595                                    gen_rtx_PLUS (Pmode,
7596                                                  stack_pointer_rtx,
7597                                                  GEN_INT (offset)));
7598
7599           if (!memory_address_p (mode, XEXP (mem_rtx, 0)))
7600             {
7601               gcc_assert (r0);
7602               mem_rtx = NULL_RTX;
7603             }
7604
7605           if (HAVE_PRE_DECREMENT
7606               && (offset_in_r0 - offset == GET_MODE_SIZE (mode)
7607                   || mem_rtx == NULL_RTX
7608                   || reg == PR_REG || SPECIAL_REGISTER_P (reg)))
7609             {
7610               pre_dec = gen_frame_mem (mode, gen_rtx_PRE_DEC (Pmode, r0));
7611
7612               if (!memory_address_p (mode, XEXP (pre_dec, 0)))
7613                 pre_dec = NULL_RTX;
7614               else
7615                 {
7616                   mem_rtx = NULL_RTX;
7617                   offset += GET_MODE_SIZE (mode);
7618                 }
7619             }
7620
7621           if (mem_rtx != NULL_RTX)
7622             goto addr_ok;
7623
7624           if (offset_in_r0 == -1)
7625             {
7626               emit_move_insn (r0, GEN_INT (offset));
7627               offset_in_r0 = offset;
7628             }
7629           else if (offset != offset_in_r0)
7630             {
7631               emit_move_insn (r0,
7632                               gen_rtx_PLUS
7633                               (Pmode, r0,
7634                                GEN_INT (offset - offset_in_r0)));
7635               offset_in_r0 += offset - offset_in_r0;
7636             }
7637
7638           if (pre_dec != NULL_RTX)
7639             {
7640               if (! sp_in_r0)
7641                 {
7642                   emit_move_insn (r0,
7643                                   gen_rtx_PLUS
7644                                   (Pmode, r0, stack_pointer_rtx));
7645                   sp_in_r0 = 1;
7646                 }
7647
7648               offset -= GET_MODE_SIZE (mode);
7649               offset_in_r0 -= GET_MODE_SIZE (mode);
7650
7651               mem_rtx = pre_dec;
7652             }
7653           else if (sp_in_r0)
7654             mem_rtx = gen_frame_mem (mode, r0);
7655           else
7656             mem_rtx = gen_frame_mem (mode,
7657                                      gen_rtx_PLUS (Pmode,
7658                                                    stack_pointer_rtx,
7659                                                    r0));
7660
7661           /* We must not use an r0-based address for target-branch
7662              registers or for special registers without pre-dec
7663              memory addresses, since we store their values in r0
7664              first.  */
7665           gcc_assert (!TARGET_REGISTER_P (reg)
7666                       && ((reg != PR_REG && !SPECIAL_REGISTER_P (reg))
7667                           || mem_rtx == pre_dec));
7668           
7669         addr_ok:
7670           orig_reg_rtx = reg_rtx;
7671           if (TARGET_REGISTER_P (reg)
7672               || ((reg == PR_REG || SPECIAL_REGISTER_P (reg))
7673                   && mem_rtx != pre_dec))
7674             {
7675               rtx tmp_reg = gen_rtx_REG (GET_MODE (reg_rtx), *tmp_pnt);
7676
7677               emit_move_insn (tmp_reg, reg_rtx);
7678
7679               if (REGNO (tmp_reg) == R0_REG)
7680                 {
7681                   offset_in_r0 = -1;
7682                   sp_in_r0 = 0;
7683                   gcc_assert (!refers_to_regno_p
7684                               (R0_REG, R0_REG+1, mem_rtx, (rtx *) 0));
7685                 }
7686
7687               if (*++tmp_pnt <= 0)
7688                 tmp_pnt = schedule.temps;
7689
7690               reg_rtx = tmp_reg;
7691             }
7692           {
7693             rtx insn;
7694
7695             /* Mark as interesting for dwarf cfi generator */
7696             insn = emit_move_insn (mem_rtx, reg_rtx);
7697             RTX_FRAME_RELATED_P (insn) = 1;
7698             /* If we use an intermediate register for the save, we can't
7699                describe this exactly in cfi as a copy of the to-be-saved
7700                register into the temporary register and then the temporary
7701                register on the stack, because the temporary register can
7702                have a different natural size than the to-be-saved register.
7703                Thus, we gloss over the intermediate copy and pretend we do
7704                a direct save from the to-be-saved register.  */
7705             if (REGNO (reg_rtx) != reg)
7706               {
7707                 rtx set;
7708
7709                 set = gen_rtx_SET (VOIDmode, mem_rtx, orig_reg_rtx);
7710                 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
7711               }
7712
7713             if (TARGET_SHCOMPACT && (offset_in_r0 != -1))
7714               {
7715                 rtx reg_rtx = gen_rtx_REG (mode, reg);
7716                 rtx set;
7717                 rtx mem_rtx = gen_frame_mem (mode,
7718                                              gen_rtx_PLUS (Pmode,
7719                                                            stack_pointer_rtx,
7720                                                            GEN_INT (offset)));
7721
7722                 set = gen_rtx_SET (VOIDmode, mem_rtx, reg_rtx);
7723                 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
7724               }
7725           }
7726         }
7727
7728       gcc_assert (entry->offset == d_rounding);
7729     }
7730   else
7731     {
7732       push_regs (&live_regs_mask, current_function_interrupt);
7733       stack_usage += d;
7734     }
7735
7736   if (flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
7737     emit_insn (gen_GOTaddr2picreg ());
7738
7739   if (SHMEDIA_REGS_STACK_ADJUST ())
7740     {
7741       /* This must NOT go through the PLT, otherwise mach and macl
7742          may be clobbered.  */
7743       function_symbol (gen_rtx_REG (Pmode, R0_REG),
7744                        (TARGET_FPU_ANY
7745                         ? "__GCC_push_shmedia_regs"
7746                         : "__GCC_push_shmedia_regs_nofpu"), SFUNC_GOT);
7747       emit_insn (gen_shmedia_save_restore_regs_compact
7748                  (GEN_INT (-SHMEDIA_REGS_STACK_ADJUST ())));
7749     }
7750
7751   if (target_flags != save_flags && ! current_function_interrupt)
7752     emit_insn (gen_toggle_sz ());
7753
7754   target_flags = save_flags;
7755
7756   output_stack_adjust (-rounded_frame_size (d) + d_rounding,
7757                        stack_pointer_rtx, 0, NULL, true);
7758   stack_usage += rounded_frame_size (d) - d_rounding;
7759
7760   if (frame_pointer_needed)
7761     frame_insn (GEN_MOV (hard_frame_pointer_rtx, stack_pointer_rtx));
7762
7763   if (TARGET_SHCOMPACT
7764       && (crtl->args.info.call_cookie & ~ CALL_COOKIE_RET_TRAMP(1)))
7765     {
7766       /* This must NOT go through the PLT, otherwise mach and macl
7767          may be clobbered.  */
7768       function_symbol (gen_rtx_REG (Pmode, R0_REG),
7769                       "__GCC_shcompact_incoming_args", SFUNC_GOT);
7770       emit_insn (gen_shcompact_incoming_args ());
7771     }
7772
7773   /* If we are profiling, make sure no instructions are scheduled before
7774      the call to mcount.  Similarly if some call instructions are swapped
7775      before frame related insns, it'll confuse the unwinder because
7776      currently SH has no unwind info for function epilogues.  */
7777   if (crtl->profile || flag_exceptions || flag_unwind_tables)
7778     emit_insn (gen_blockage ());
7779
7780   if (flag_stack_usage_info)
7781     current_function_static_stack_size = stack_usage;
7782 }
7783
7784 /* Expand code for the function epilogue.  */
7785 void
7786 sh_expand_epilogue (bool sibcall_p)
7787 {
7788   HARD_REG_SET live_regs_mask;
7789   int d, i;
7790   int d_rounding = 0;
7791
7792   int save_flags = target_flags;
7793   int frame_size, save_size;
7794   int fpscr_deferred = 0;
7795   int e = sibcall_p ? -1 : 1;
7796
7797   d = calc_live_regs (&live_regs_mask);
7798
7799   save_size = d;
7800   frame_size = rounded_frame_size (d);
7801
7802   if (TARGET_SH5)
7803     {
7804       int tregs_space = shmedia_target_regs_stack_adjust (&live_regs_mask);
7805       int total_size;
7806       if (d % (STACK_BOUNDARY / BITS_PER_UNIT))
7807         d_rounding = ((STACK_BOUNDARY / BITS_PER_UNIT)
7808                       - d % (STACK_BOUNDARY / BITS_PER_UNIT));
7809
7810       total_size = d + tregs_space;
7811       total_size += rounded_frame_size (total_size);
7812       save_size = total_size - frame_size;
7813
7814       /* If adjusting the stack in a single step costs nothing extra, do so.
7815          I.e. either if a single addi is enough, or we need a movi anyway,
7816          and we don't exceed the maximum offset range (the test for the
7817          latter is conservative for simplicity).  */
7818       if (TARGET_SHMEDIA
7819           && ! frame_pointer_needed
7820           && (CONST_OK_FOR_I10 (total_size)
7821               || (! CONST_OK_FOR_I10 (save_size + d_rounding)
7822                   && total_size <= 2044)))
7823         d_rounding = frame_size;
7824
7825       frame_size -= d_rounding;
7826     }
7827
7828   if (frame_pointer_needed)
7829     {
7830       /* We must avoid scheduling the epilogue with previous basic blocks.
7831          See PR/18032 and PR/40313.  */
7832       emit_insn (gen_blockage ());
7833       output_stack_adjust (frame_size, hard_frame_pointer_rtx, e,
7834                            &live_regs_mask, true);
7835
7836       /* We must avoid moving the stack pointer adjustment past code
7837          which reads from the local frame, else an interrupt could
7838          occur after the SP adjustment and clobber data in the local
7839          frame.  */
7840       emit_insn (gen_blockage ());
7841       frame_insn (GEN_MOV (stack_pointer_rtx, hard_frame_pointer_rtx));
7842     }
7843   else if (frame_size)
7844     {
7845       /* We must avoid moving the stack pointer adjustment past code
7846          which reads from the local frame, else an interrupt could
7847          occur after the SP adjustment and clobber data in the local
7848          frame.  */
7849       emit_insn (gen_blockage ());
7850       output_stack_adjust (frame_size, stack_pointer_rtx, e,
7851                            &live_regs_mask, true);
7852     }
7853
7854   if (SHMEDIA_REGS_STACK_ADJUST ())
7855     {
7856       function_symbol (gen_rtx_REG (Pmode, R0_REG),
7857                        (TARGET_FPU_ANY
7858                         ? "__GCC_pop_shmedia_regs"
7859                         : "__GCC_pop_shmedia_regs_nofpu"), SFUNC_GOT);
7860       /* This must NOT go through the PLT, otherwise mach and macl
7861          may be clobbered.  */
7862       emit_insn (gen_shmedia_save_restore_regs_compact
7863                  (GEN_INT (SHMEDIA_REGS_STACK_ADJUST ())));
7864     }
7865
7866   /* Pop all the registers.  */
7867
7868   if (target_flags != save_flags && ! current_function_interrupt)
7869     emit_insn (gen_toggle_sz ());
7870   if (TARGET_SH5)
7871     {
7872       int offset_base, offset;
7873       int offset_in_r0 = -1;
7874       int sp_in_r0 = 0;
7875       rtx r0 = gen_rtx_REG (Pmode, R0_REG);
7876       save_schedule schedule;
7877       save_entry *entry;
7878       int *tmp_pnt;
7879
7880       entry = sh5_schedule_saves (&live_regs_mask, &schedule, d_rounding);
7881       offset_base = -entry[1].offset + d_rounding;
7882       tmp_pnt = schedule.temps;
7883       for (; entry->mode != VOIDmode; entry--)
7884         {
7885           enum machine_mode mode = (enum machine_mode) entry->mode;
7886           int reg = entry->reg;
7887           rtx reg_rtx, mem_rtx, post_inc = NULL_RTX;
7888
7889           offset = offset_base + entry->offset;
7890           reg_rtx = gen_rtx_REG (mode, reg);
7891
7892           mem_rtx = gen_frame_mem (mode,
7893                                    gen_rtx_PLUS (Pmode,
7894                                                  stack_pointer_rtx,
7895                                                  GEN_INT (offset)));
7896
7897           if (!memory_address_p (mode, XEXP (mem_rtx, 0)))
7898             mem_rtx = NULL_RTX;
7899
7900           if (HAVE_POST_INCREMENT
7901               && (offset == offset_in_r0
7902                   || (offset + GET_MODE_SIZE (mode) != d + d_rounding
7903                       && mem_rtx == NULL_RTX)
7904                   || reg == PR_REG || SPECIAL_REGISTER_P (reg)))
7905             {
7906               post_inc = gen_frame_mem (mode, gen_rtx_POST_INC (Pmode, r0));
7907
7908               if (!memory_address_p (mode, XEXP (post_inc, 0)))
7909                 post_inc = NULL_RTX;
7910               else
7911                 mem_rtx = NULL_RTX;
7912             }
7913
7914           if (mem_rtx != NULL_RTX)
7915             goto addr_ok;
7916
7917           if (offset_in_r0 == -1)
7918             {
7919               emit_move_insn (r0, GEN_INT (offset));
7920               offset_in_r0 = offset;
7921             }
7922           else if (offset != offset_in_r0)
7923             {
7924               emit_move_insn (r0,
7925                               gen_rtx_PLUS
7926                               (Pmode, r0,
7927                                GEN_INT (offset - offset_in_r0)));
7928               offset_in_r0 += offset - offset_in_r0;
7929             }
7930
7931           if (post_inc != NULL_RTX)
7932             {
7933               if (! sp_in_r0)
7934                 {
7935                   emit_move_insn (r0,
7936                                   gen_rtx_PLUS
7937                                   (Pmode, r0, stack_pointer_rtx));
7938                   sp_in_r0 = 1;
7939                 }
7940
7941               mem_rtx = post_inc;
7942
7943               offset_in_r0 += GET_MODE_SIZE (mode);
7944             }
7945           else if (sp_in_r0)
7946             mem_rtx = gen_frame_mem (mode, r0);
7947           else
7948             mem_rtx = gen_frame_mem (mode,
7949                                      gen_rtx_PLUS (Pmode,
7950                                                    stack_pointer_rtx,
7951                                                    r0));
7952
7953           gcc_assert ((reg != PR_REG && !SPECIAL_REGISTER_P (reg))
7954                       || mem_rtx == post_inc);
7955
7956         addr_ok:
7957           if ((reg == PR_REG || SPECIAL_REGISTER_P (reg))
7958               && mem_rtx != post_inc)
7959             {
7960               emit_move_insn (r0, mem_rtx);
7961               mem_rtx = r0;
7962             }
7963           else if (TARGET_REGISTER_P (reg))
7964             {
7965               rtx tmp_reg = gen_rtx_REG (mode, *tmp_pnt);
7966
7967               /* Give the scheduler a bit of freedom by using up to
7968                  MAX_TEMPS registers in a round-robin fashion.  */
7969               emit_move_insn (tmp_reg, mem_rtx);
7970               mem_rtx = tmp_reg;
7971               if (*++tmp_pnt < 0)
7972                 tmp_pnt = schedule.temps;
7973             }
7974
7975           emit_move_insn (reg_rtx, mem_rtx);
7976         }
7977
7978       gcc_assert (entry->offset + offset_base == d + d_rounding);
7979     }
7980   else /* ! TARGET_SH5 */
7981     {
7982       int last_reg;
7983
7984       save_size = 0;
7985         /* For an ISR with RESBANK attribute assigned, don't pop PR
7986            register.  */
7987       if (TEST_HARD_REG_BIT (live_regs_mask, PR_REG)
7988           && !sh_cfun_resbank_handler_p ())     
7989         {
7990           if (!frame_pointer_needed)
7991             emit_insn (gen_blockage ());
7992           pop (PR_REG);
7993         }
7994
7995       /* Banked registers are popped first to avoid being scheduled in the
7996          delay slot. RTE switches banks before the ds instruction.  */
7997       if (current_function_interrupt)
7998         {
7999           bool use_movml = false;
8000
8001           if (TARGET_SH2A)
8002             {
8003               unsigned int count = 0;
8004
8005               for (i = FIRST_BANKED_REG; i <= LAST_BANKED_REG; i++)
8006                 if (TEST_HARD_REG_BIT (live_regs_mask, i))
8007                   count++;
8008                 else
8009                   break;
8010
8011               /* Use movml when all banked register are poped.  */
8012               if (count == LAST_BANKED_REG - FIRST_BANKED_REG + 1)
8013                 use_movml = true;
8014             }
8015
8016           if (sh_cfun_resbank_handler_p ())
8017             ; /* Do nothing.  */
8018           else if (use_movml)
8019             {
8020               rtx sp_reg = gen_rtx_REG (SImode, STACK_POINTER_REGNUM);
8021
8022               /* We must avoid scheduling multiple load insn with another
8023                  insns.  */
8024               emit_insn (gen_blockage ());
8025               emit_insn (gen_movml_pop_banked (sp_reg));
8026               emit_insn (gen_blockage ());
8027             }
8028           else
8029             for (i = LAST_BANKED_REG; i >= FIRST_BANKED_REG; i--)
8030               if (TEST_HARD_REG_BIT (live_regs_mask, i))
8031                 pop (i);
8032
8033           last_reg = FIRST_PSEUDO_REGISTER - LAST_BANKED_REG - 1;
8034         }
8035       else
8036         last_reg = FIRST_PSEUDO_REGISTER;
8037
8038       for (i = 0; i < last_reg; i++)
8039         {
8040           int j = (FIRST_PSEUDO_REGISTER - 1) - i;
8041
8042           if (j == FPSCR_REG && current_function_interrupt && TARGET_FMOVD
8043               && hard_reg_set_intersect_p (live_regs_mask,
8044                                           reg_class_contents[DF_REGS]))
8045             fpscr_deferred = 1;
8046           /* For an ISR with RESBANK attribute assigned, don't pop
8047              following registers, R0-R14, MACH, MACL and GBR.  */
8048           else if (j != PR_REG && TEST_HARD_REG_BIT (live_regs_mask, j) 
8049                    && ! (sh_cfun_resbank_handler_p ()
8050                          && ((j >= FIRST_GENERAL_REG
8051                               && j < LAST_GENERAL_REG)
8052                               || j == MACH_REG
8053                               || j == MACL_REG
8054                               || j == GBR_REG)))
8055             pop (j);
8056
8057           if (j == FIRST_FP_REG && fpscr_deferred)
8058             pop (FPSCR_REG);
8059         }
8060     }
8061   if (target_flags != save_flags && ! current_function_interrupt)
8062     emit_insn (gen_toggle_sz ());
8063   target_flags = save_flags;
8064
8065   output_stack_adjust (crtl->args.pretend_args_size
8066                        + save_size + d_rounding
8067                        + crtl->args.info.stack_regs * 8,
8068                        stack_pointer_rtx, e, NULL, true);
8069
8070   if (crtl->calls_eh_return)
8071     emit_insn (GEN_ADD3 (stack_pointer_rtx, stack_pointer_rtx,
8072                          EH_RETURN_STACKADJ_RTX));
8073
8074   /* Switch back to the normal stack if necessary.  */
8075   if (lookup_attribute ("sp_switch", DECL_ATTRIBUTES (current_function_decl)))
8076     emit_insn (gen_sp_switch_2 ());
8077
8078   /* Tell flow the insn that pops PR isn't dead.  */
8079   /* PR_REG will never be live in SHmedia mode, and we don't need to
8080      USE PR_MEDIA_REG, since it will be explicitly copied to TR0_REG
8081      by the return pattern.  */
8082   if (TEST_HARD_REG_BIT (live_regs_mask, PR_REG))
8083     emit_use (gen_rtx_REG (SImode, PR_REG));
8084 }
8085
8086 /* Emit code to change the current function's return address to RA.
8087    TEMP is available as a scratch register, if needed.  */
8088 void
8089 sh_set_return_address (rtx ra, rtx tmp)
8090 {
8091   HARD_REG_SET live_regs_mask;
8092   int d;
8093   int pr_reg = TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG;
8094   int pr_offset;
8095
8096   d = calc_live_regs (&live_regs_mask);
8097
8098   /* If pr_reg isn't life, we can set it (or the register given in
8099      sh_media_register_for_return) directly.  */
8100   if (! TEST_HARD_REG_BIT (live_regs_mask, pr_reg))
8101     {
8102       rtx rr;
8103
8104       if (TARGET_SHMEDIA)
8105         {
8106           int rr_regno = sh_media_register_for_return ();
8107
8108           if (rr_regno < 0)
8109             rr_regno = pr_reg;
8110
8111           rr = gen_rtx_REG (DImode, rr_regno);
8112         }
8113       else
8114         rr = gen_rtx_REG (SImode, pr_reg);
8115
8116       emit_insn (GEN_MOV (rr, ra));
8117       /* Tell flow the register for return isn't dead.  */
8118       emit_use (rr);
8119       return;
8120     }
8121
8122   if (TARGET_SH5)
8123     {
8124       int offset;
8125       save_schedule schedule;
8126       save_entry *entry;
8127
8128       entry = sh5_schedule_saves (&live_regs_mask, &schedule, 0);
8129       offset = entry[1].offset;
8130       for (; entry->mode != VOIDmode; entry--)
8131         if (entry->reg == pr_reg)
8132           goto found;
8133
8134       /* We can't find pr register.  */
8135       gcc_unreachable ();
8136
8137     found:
8138       offset = entry->offset - offset;
8139       pr_offset = (rounded_frame_size (d) + offset
8140                    + SHMEDIA_REGS_STACK_ADJUST ());
8141     }
8142   else
8143     pr_offset = rounded_frame_size (d);
8144
8145   emit_insn (GEN_MOV (tmp, GEN_INT (pr_offset)));
8146
8147   if (frame_pointer_needed)
8148     emit_insn (GEN_ADD3 (tmp, tmp, hard_frame_pointer_rtx));
8149   else
8150     emit_insn (GEN_ADD3 (tmp, tmp, stack_pointer_rtx));
8151
8152   tmp = gen_frame_mem (Pmode, tmp);
8153   emit_insn (GEN_MOV (tmp, ra));
8154   /* Tell this store isn't dead.  */
8155   emit_use (tmp);
8156 }
8157
8158 /* Clear variables at function end.  */
8159 static void
8160 sh_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
8161                              HOST_WIDE_INT size ATTRIBUTE_UNUSED)
8162 {
8163 }
8164
8165 static rtx
8166 sh_builtin_saveregs (void)
8167 {
8168   /* First unnamed integer register.  */
8169   int first_intreg = crtl->args.info.arg_count[(int) SH_ARG_INT];
8170   /* Number of integer registers we need to save.  */
8171   int n_intregs = MAX (0, NPARM_REGS (SImode) - first_intreg);
8172   /* First unnamed SFmode float reg */
8173   int first_floatreg = crtl->args.info.arg_count[(int) SH_ARG_FLOAT];
8174   /* Number of SFmode float regs to save.  */
8175   int n_floatregs = MAX (0, NPARM_REGS (SFmode) - first_floatreg);
8176   rtx regbuf, fpregs;
8177   int bufsize, regno;
8178   alias_set_type alias_set;
8179
8180   if (TARGET_SH5)
8181     {
8182       if (n_intregs)
8183         {
8184           int pushregs = n_intregs;
8185
8186           while (pushregs < NPARM_REGS (SImode) - 1
8187                  && (CALL_COOKIE_INT_REG_GET
8188                         (crtl->args.info.call_cookie,
8189                          NPARM_REGS (SImode) - pushregs)
8190                      == 1))
8191             {
8192               crtl->args.info.call_cookie
8193                 &= ~ CALL_COOKIE_INT_REG (NPARM_REGS (SImode)
8194                                           - pushregs, 1);
8195               pushregs++;
8196             }
8197
8198           if (pushregs == NPARM_REGS (SImode))
8199             crtl->args.info.call_cookie
8200               |= (CALL_COOKIE_INT_REG (0, 1)
8201                   | CALL_COOKIE_STACKSEQ (pushregs - 1));
8202           else
8203             crtl->args.info.call_cookie
8204               |= CALL_COOKIE_STACKSEQ (pushregs);
8205
8206           crtl->args.pretend_args_size += 8 * n_intregs;
8207         }
8208       if (TARGET_SHCOMPACT)
8209         return const0_rtx;
8210     }
8211
8212   if (! TARGET_SH2E && ! TARGET_SH4 && ! TARGET_SH5)
8213     {
8214       error ("__builtin_saveregs not supported by this subtarget");
8215       return const0_rtx;
8216     }
8217
8218   if (TARGET_SHMEDIA)
8219     n_floatregs = 0;
8220
8221   /* Allocate block of memory for the regs.  */
8222   /* ??? If n_intregs + n_floatregs == 0, should we allocate at least 1 byte?
8223      Or can assign_stack_local accept a 0 SIZE argument?  */
8224   bufsize = (n_intregs * UNITS_PER_WORD) + (n_floatregs * UNITS_PER_WORD);
8225
8226   if (TARGET_SHMEDIA)
8227     regbuf = gen_frame_mem (BLKmode, gen_rtx_REG (Pmode, ARG_POINTER_REGNUM));
8228   else if (n_floatregs & 1)
8229     {
8230       rtx addr;
8231
8232       regbuf = assign_stack_local (BLKmode, bufsize + UNITS_PER_WORD, 0);
8233       addr = copy_to_mode_reg (Pmode, XEXP (regbuf, 0));
8234       emit_insn (gen_iorsi3 (addr, addr, GEN_INT (UNITS_PER_WORD)));
8235       regbuf = change_address (regbuf, BLKmode, addr);
8236     }
8237   else if (STACK_BOUNDARY < 64 && TARGET_FPU_DOUBLE && n_floatregs)
8238     {
8239       rtx addr, mask;
8240
8241       regbuf = assign_stack_local (BLKmode, bufsize + UNITS_PER_WORD, 0);
8242       addr = copy_to_mode_reg (Pmode, plus_constant (Pmode,
8243                                                      XEXP (regbuf, 0), 4));
8244       mask = copy_to_mode_reg (Pmode, GEN_INT (-8));
8245       emit_insn (gen_andsi3 (addr, addr, mask));
8246       regbuf = change_address (regbuf, BLKmode, addr);
8247     }
8248   else
8249     regbuf = assign_stack_local (BLKmode, bufsize, TARGET_FPU_DOUBLE ? 64 : 0);
8250   alias_set = get_varargs_alias_set ();
8251   set_mem_alias_set (regbuf, alias_set);
8252
8253   /* Save int args.
8254      This is optimized to only save the regs that are necessary.  Explicitly
8255      named args need not be saved.  */
8256   if (n_intregs > 0)
8257     move_block_from_reg (BASE_ARG_REG (SImode) + first_intreg,
8258                          adjust_address (regbuf, BLKmode,
8259                                          n_floatregs * UNITS_PER_WORD),
8260                          n_intregs);
8261
8262   if (TARGET_SHMEDIA)
8263     /* Return the address of the regbuf.  */
8264     return XEXP (regbuf, 0);
8265
8266   /* Save float args.
8267      This is optimized to only save the regs that are necessary.  Explicitly
8268      named args need not be saved.
8269      We explicitly build a pointer to the buffer because it halves the insn
8270      count when not optimizing (otherwise the pointer is built for each reg
8271      saved).
8272      We emit the moves in reverse order so that we can use predecrement.  */
8273
8274   fpregs = copy_to_mode_reg (Pmode,
8275                              plus_constant (Pmode, XEXP (regbuf, 0),
8276                                             n_floatregs * UNITS_PER_WORD));
8277   if (TARGET_SH4 || TARGET_SH2A_DOUBLE)
8278     {
8279       rtx mem;
8280       for (regno = NPARM_REGS (DFmode) - 2; regno >= first_floatreg; regno -= 2)
8281         {
8282           emit_insn (gen_addsi3 (fpregs, fpregs,
8283                                  GEN_INT (-2 * UNITS_PER_WORD)));
8284           mem = change_address (regbuf, DFmode, fpregs);
8285           emit_move_insn (mem,
8286                           gen_rtx_REG (DFmode, BASE_ARG_REG (DFmode) + regno));
8287         }
8288       regno = first_floatreg;
8289       if (regno & 1)
8290         {
8291           emit_insn (gen_addsi3 (fpregs, fpregs, GEN_INT (-UNITS_PER_WORD)));
8292           mem = change_address (regbuf, SFmode, fpregs);
8293           emit_move_insn (mem,
8294                           gen_rtx_REG (SFmode, BASE_ARG_REG (SFmode)
8295                                                + regno - SH_REG_MSW_OFFSET));
8296         }
8297     }
8298   else
8299     for (regno = NPARM_REGS (SFmode) - 1; regno >= first_floatreg; regno--)
8300       {
8301         rtx mem;
8302
8303         emit_insn (gen_addsi3 (fpregs, fpregs, GEN_INT (-UNITS_PER_WORD)));
8304         mem = change_address (regbuf, SFmode, fpregs);
8305         emit_move_insn (mem,
8306                         gen_rtx_REG (SFmode, BASE_ARG_REG (SFmode) + regno));
8307       }
8308
8309   /* Return the address of the regbuf.  */
8310   return XEXP (regbuf, 0);
8311 }
8312
8313 /* Define the `__builtin_va_list' type for the ABI.  */
8314 static tree
8315 sh_build_builtin_va_list (void)
8316 {
8317   tree f_next_o, f_next_o_limit, f_next_fp, f_next_fp_limit, f_next_stack;
8318   tree record, type_decl;
8319
8320   if (TARGET_SH5 || (! TARGET_SH2E && ! TARGET_SH4)
8321       || TARGET_HITACHI || sh_cfun_attr_renesas_p ())
8322     return ptr_type_node;
8323
8324   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
8325   type_decl = build_decl (BUILTINS_LOCATION,
8326                           TYPE_DECL, get_identifier ("__va_list_tag"), record);
8327
8328   f_next_o = build_decl (BUILTINS_LOCATION,
8329                          FIELD_DECL, get_identifier ("__va_next_o"),
8330                          ptr_type_node);
8331   f_next_o_limit = build_decl (BUILTINS_LOCATION,
8332                                FIELD_DECL,
8333                                get_identifier ("__va_next_o_limit"),
8334                                ptr_type_node);
8335   f_next_fp = build_decl (BUILTINS_LOCATION,
8336                           FIELD_DECL, get_identifier ("__va_next_fp"),
8337                           ptr_type_node);
8338   f_next_fp_limit = build_decl (BUILTINS_LOCATION,
8339                                 FIELD_DECL,
8340                                 get_identifier ("__va_next_fp_limit"),
8341                                 ptr_type_node);
8342   f_next_stack = build_decl (BUILTINS_LOCATION,
8343                              FIELD_DECL, get_identifier ("__va_next_stack"),
8344                              ptr_type_node);
8345
8346   DECL_FIELD_CONTEXT (f_next_o) = record;
8347   DECL_FIELD_CONTEXT (f_next_o_limit) = record;
8348   DECL_FIELD_CONTEXT (f_next_fp) = record;
8349   DECL_FIELD_CONTEXT (f_next_fp_limit) = record;
8350   DECL_FIELD_CONTEXT (f_next_stack) = record;
8351
8352   TYPE_STUB_DECL (record) = type_decl;
8353   TYPE_NAME (record) = type_decl;
8354   TYPE_FIELDS (record) = f_next_o;
8355   DECL_CHAIN (f_next_o) = f_next_o_limit;
8356   DECL_CHAIN (f_next_o_limit) = f_next_fp;
8357   DECL_CHAIN (f_next_fp) = f_next_fp_limit;
8358   DECL_CHAIN (f_next_fp_limit) = f_next_stack;
8359
8360   layout_type (record);
8361
8362   return record;
8363 }
8364
8365 /* Implement `va_start' for varargs and stdarg.  */
8366 static void
8367 sh_va_start (tree valist, rtx nextarg)
8368 {
8369   tree f_next_o, f_next_o_limit, f_next_fp, f_next_fp_limit, f_next_stack;
8370   tree next_o, next_o_limit, next_fp, next_fp_limit, next_stack;
8371   tree t, u;
8372   int nfp, nint;
8373
8374   if (TARGET_SH5)
8375     {
8376       expand_builtin_saveregs ();
8377       std_expand_builtin_va_start (valist, nextarg);
8378       return;
8379     }
8380
8381   if ((! TARGET_SH2E && ! TARGET_SH4)
8382       || TARGET_HITACHI || sh_cfun_attr_renesas_p ())
8383     {
8384       std_expand_builtin_va_start (valist, nextarg);
8385       return;
8386     }
8387
8388   f_next_o = TYPE_FIELDS (va_list_type_node);
8389   f_next_o_limit = DECL_CHAIN (f_next_o);
8390   f_next_fp = DECL_CHAIN (f_next_o_limit);
8391   f_next_fp_limit = DECL_CHAIN (f_next_fp);
8392   f_next_stack = DECL_CHAIN (f_next_fp_limit);
8393
8394   next_o = build3 (COMPONENT_REF, TREE_TYPE (f_next_o), valist, f_next_o,
8395                    NULL_TREE);
8396   next_o_limit = build3 (COMPONENT_REF, TREE_TYPE (f_next_o_limit),
8397                          valist, f_next_o_limit, NULL_TREE);
8398   next_fp = build3 (COMPONENT_REF, TREE_TYPE (f_next_fp), valist, f_next_fp,
8399                     NULL_TREE);
8400   next_fp_limit = build3 (COMPONENT_REF, TREE_TYPE (f_next_fp_limit),
8401                           valist, f_next_fp_limit, NULL_TREE);
8402   next_stack = build3 (COMPONENT_REF, TREE_TYPE (f_next_stack),
8403                        valist, f_next_stack, NULL_TREE);
8404
8405   /* Call __builtin_saveregs.  */
8406   u = make_tree (sizetype, expand_builtin_saveregs ());
8407   u = fold_convert (ptr_type_node, u);
8408   t = build2 (MODIFY_EXPR, ptr_type_node, next_fp, u);
8409   TREE_SIDE_EFFECTS (t) = 1;
8410   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
8411
8412   nfp = crtl->args.info.arg_count[SH_ARG_FLOAT];
8413   if (nfp < 8)
8414     nfp = 8 - nfp;
8415   else
8416     nfp = 0;
8417   u = fold_build_pointer_plus_hwi (u, UNITS_PER_WORD * nfp);
8418   t = build2 (MODIFY_EXPR, ptr_type_node, next_fp_limit, u);
8419   TREE_SIDE_EFFECTS (t) = 1;
8420   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
8421
8422   t = build2 (MODIFY_EXPR, ptr_type_node, next_o, u);
8423   TREE_SIDE_EFFECTS (t) = 1;
8424   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
8425
8426   nint = crtl->args.info.arg_count[SH_ARG_INT];
8427   if (nint < 4)
8428     nint = 4 - nint;
8429   else
8430     nint = 0;
8431   u = fold_build_pointer_plus_hwi (u, UNITS_PER_WORD * nint);
8432   t = build2 (MODIFY_EXPR, ptr_type_node, next_o_limit, u);
8433   TREE_SIDE_EFFECTS (t) = 1;
8434   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
8435
8436   u = make_tree (ptr_type_node, nextarg);
8437   t = build2 (MODIFY_EXPR, ptr_type_node, next_stack, u);
8438   TREE_SIDE_EFFECTS (t) = 1;
8439   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
8440 }
8441
8442 /* TYPE is a RECORD_TYPE.  If there is only a single nonzero-sized
8443    member, return it.  */
8444 static tree
8445 find_sole_member (tree type)
8446 {
8447   tree field, member = NULL_TREE;
8448
8449   for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8450     {
8451       if (TREE_CODE (field) != FIELD_DECL)
8452         continue;
8453       if (!DECL_SIZE (field))
8454         return NULL_TREE;
8455       if (integer_zerop (DECL_SIZE (field)))
8456         continue;
8457       if (member)
8458         return NULL_TREE;
8459       member = field;
8460     }
8461   return member;
8462 }
8463
8464 /* Implement `va_arg'.  */
8465 static tree
8466 sh_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
8467                          gimple_seq *post_p ATTRIBUTE_UNUSED)
8468 {
8469   HOST_WIDE_INT size, rsize;
8470   tree tmp, pptr_type_node;
8471   tree addr, lab_over = NULL, result = NULL;
8472   bool pass_by_ref;
8473   tree eff_type;
8474
8475   if (!VOID_TYPE_P (type))
8476     pass_by_ref = targetm.calls.must_pass_in_stack (TYPE_MODE (type), type);
8477   else
8478     pass_by_ref = false;
8479
8480   if (pass_by_ref)
8481     type = build_pointer_type (type);
8482
8483   size = int_size_in_bytes (type);
8484   rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
8485   pptr_type_node = build_pointer_type (ptr_type_node);
8486
8487   if (! TARGET_SH5 && (TARGET_SH2E || TARGET_SH4)
8488       && ! (TARGET_HITACHI || sh_cfun_attr_renesas_p ()))
8489     {
8490       tree f_next_o, f_next_o_limit, f_next_fp, f_next_fp_limit, f_next_stack;
8491       tree next_o, next_o_limit, next_fp, next_fp_limit, next_stack;
8492       int pass_as_float;
8493       tree lab_false;
8494       tree member;
8495
8496       f_next_o = TYPE_FIELDS (va_list_type_node);
8497       f_next_o_limit = DECL_CHAIN (f_next_o);
8498       f_next_fp = DECL_CHAIN (f_next_o_limit);
8499       f_next_fp_limit = DECL_CHAIN (f_next_fp);
8500       f_next_stack = DECL_CHAIN (f_next_fp_limit);
8501
8502       next_o = build3 (COMPONENT_REF, TREE_TYPE (f_next_o), valist, f_next_o,
8503                        NULL_TREE);
8504       next_o_limit = build3 (COMPONENT_REF, TREE_TYPE (f_next_o_limit),
8505                              valist, f_next_o_limit, NULL_TREE);
8506       next_fp = build3 (COMPONENT_REF, TREE_TYPE (f_next_fp),
8507                         valist, f_next_fp, NULL_TREE);
8508       next_fp_limit = build3 (COMPONENT_REF, TREE_TYPE (f_next_fp_limit),
8509                               valist, f_next_fp_limit, NULL_TREE);
8510       next_stack = build3 (COMPONENT_REF, TREE_TYPE (f_next_stack),
8511                            valist, f_next_stack, NULL_TREE);
8512
8513       /* Structures with a single member with a distinct mode are passed
8514          like their member.  This is relevant if the latter has a REAL_TYPE
8515          or COMPLEX_TYPE type.  */
8516       eff_type = type;
8517       while (TREE_CODE (eff_type) == RECORD_TYPE
8518              && (member = find_sole_member (eff_type))
8519              && (TREE_CODE (TREE_TYPE (member)) == REAL_TYPE
8520                  || TREE_CODE (TREE_TYPE (member)) == COMPLEX_TYPE
8521                  || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE))
8522         {
8523           tree field_type = TREE_TYPE (member);
8524
8525           if (TYPE_MODE (eff_type) == TYPE_MODE (field_type))
8526             eff_type = field_type;
8527           else
8528             {
8529               gcc_assert ((TYPE_ALIGN (eff_type)
8530                            < GET_MODE_ALIGNMENT (TYPE_MODE (field_type)))
8531                           || (TYPE_ALIGN (eff_type)
8532                               > GET_MODE_BITSIZE (TYPE_MODE (field_type))));
8533               break;
8534             }
8535         }
8536
8537       if (TARGET_SH4 || TARGET_SH2A_DOUBLE)
8538         {
8539           pass_as_float = ((TREE_CODE (eff_type) == REAL_TYPE && size <= 8)
8540                            || (TREE_CODE (eff_type) == COMPLEX_TYPE
8541                                && TREE_CODE (TREE_TYPE (eff_type)) == REAL_TYPE
8542                                && size <= 16));
8543         }
8544       else
8545         {
8546           pass_as_float = (TREE_CODE (eff_type) == REAL_TYPE && size == 4);
8547         }
8548
8549       addr = create_tmp_var (pptr_type_node, NULL);
8550       lab_false = create_artificial_label (UNKNOWN_LOCATION);
8551       lab_over = create_artificial_label (UNKNOWN_LOCATION);
8552
8553       valist = build_simple_mem_ref (addr);
8554
8555       if (pass_as_float)
8556         {
8557           tree next_fp_tmp = create_tmp_var (TREE_TYPE (f_next_fp), NULL);
8558           tree cmp;
8559           bool is_double = size == 8 && TREE_CODE (eff_type) == REAL_TYPE;
8560
8561           tmp = build1 (ADDR_EXPR, pptr_type_node, unshare_expr (next_fp));
8562           gimplify_assign (unshare_expr (addr), tmp, pre_p);
8563
8564           gimplify_assign (unshare_expr (next_fp_tmp), valist, pre_p);
8565           tmp = next_fp_limit;
8566           if (size > 4 && !is_double)
8567             tmp = fold_build_pointer_plus_hwi (unshare_expr (tmp), 4 - size);
8568           tmp = build2 (GE_EXPR, boolean_type_node,
8569                         unshare_expr (next_fp_tmp), unshare_expr (tmp));
8570           cmp = build3 (COND_EXPR, void_type_node, tmp,
8571                         build1 (GOTO_EXPR, void_type_node,
8572                                 unshare_expr (lab_false)), NULL_TREE);
8573           if (!is_double)
8574             gimplify_and_add (cmp, pre_p);
8575
8576           if (TYPE_ALIGN (eff_type) > BITS_PER_WORD
8577               || (is_double || size == 16))
8578             {
8579               tmp = fold_convert (sizetype, next_fp_tmp);
8580               tmp = build2 (BIT_AND_EXPR, sizetype, tmp,
8581                             size_int (UNITS_PER_WORD));
8582               tmp = fold_build_pointer_plus (unshare_expr (next_fp_tmp), tmp);
8583               gimplify_assign (unshare_expr (next_fp_tmp), tmp, pre_p);
8584             }
8585           if (is_double)
8586             gimplify_and_add (cmp, pre_p);
8587
8588 #ifdef FUNCTION_ARG_SCmode_WART
8589           if (TYPE_MODE (eff_type) == SCmode
8590               && TARGET_SH4 && TARGET_LITTLE_ENDIAN)
8591             {
8592               tree subtype = TREE_TYPE (eff_type);
8593               tree real, imag;
8594
8595               imag
8596                 = std_gimplify_va_arg_expr (next_fp_tmp, subtype, pre_p, NULL);
8597               imag = get_initialized_tmp_var (imag, pre_p, NULL);
8598
8599               real
8600                 = std_gimplify_va_arg_expr (next_fp_tmp, subtype, pre_p, NULL);
8601               real = get_initialized_tmp_var (real, pre_p, NULL);
8602
8603               result = build2 (COMPLEX_EXPR, eff_type, real, imag);
8604               if (type != eff_type)
8605                 result = build1 (VIEW_CONVERT_EXPR, type, result);
8606               result = get_initialized_tmp_var (result, pre_p, NULL);
8607             }
8608 #endif /* FUNCTION_ARG_SCmode_WART */
8609
8610           tmp = build1 (GOTO_EXPR, void_type_node, unshare_expr (lab_over));
8611           gimplify_and_add (tmp, pre_p);
8612
8613           tmp = build1 (LABEL_EXPR, void_type_node, unshare_expr (lab_false));
8614           gimplify_and_add (tmp, pre_p);
8615
8616           tmp = build1 (ADDR_EXPR, pptr_type_node, unshare_expr (next_stack));
8617           gimplify_assign (unshare_expr (addr), tmp, pre_p);
8618           gimplify_assign (unshare_expr (next_fp_tmp),
8619                            unshare_expr (valist), pre_p);
8620
8621           gimplify_assign (unshare_expr (valist),
8622                            unshare_expr (next_fp_tmp), post_p);
8623           valist = next_fp_tmp;
8624         }
8625       else
8626         {
8627           tmp = fold_build_pointer_plus_hwi (unshare_expr (next_o), rsize);
8628           tmp = build2 (GT_EXPR, boolean_type_node, tmp,
8629                         unshare_expr (next_o_limit));
8630           tmp = build3 (COND_EXPR, void_type_node, tmp,
8631                         build1 (GOTO_EXPR, void_type_node,
8632                                 unshare_expr (lab_false)),
8633                         NULL_TREE);
8634           gimplify_and_add (tmp, pre_p);
8635
8636           tmp = build1 (ADDR_EXPR, pptr_type_node, unshare_expr (next_o));
8637           gimplify_assign (unshare_expr (addr), tmp, pre_p);
8638
8639           tmp = build1 (GOTO_EXPR, void_type_node, unshare_expr (lab_over));
8640           gimplify_and_add (tmp, pre_p);
8641
8642           tmp = build1 (LABEL_EXPR, void_type_node, unshare_expr (lab_false));
8643           gimplify_and_add (tmp, pre_p);
8644
8645           if (size > 4 && ! (TARGET_SH4 || TARGET_SH2A))
8646             gimplify_assign (unshare_expr (next_o),
8647                              unshare_expr (next_o_limit), pre_p);
8648
8649           tmp = build1 (ADDR_EXPR, pptr_type_node, unshare_expr (next_stack));
8650           gimplify_assign (unshare_expr (addr), tmp, pre_p);
8651         }
8652
8653       if (!result)
8654         {
8655           tmp = build1 (LABEL_EXPR, void_type_node, unshare_expr (lab_over));
8656           gimplify_and_add (tmp, pre_p);
8657         }
8658     }
8659
8660   /* ??? In va-sh.h, there had been code to make values larger than
8661      size 8 indirect.  This does not match the FUNCTION_ARG macros.  */
8662
8663   tmp = std_gimplify_va_arg_expr (valist, type, pre_p, NULL);
8664   if (result)
8665     {
8666       gimplify_assign (result, tmp, pre_p);
8667       result = build1 (NOP_EXPR, TREE_TYPE (result), result);
8668       tmp = build1 (LABEL_EXPR, void_type_node, unshare_expr (lab_over));
8669       gimplify_and_add (tmp, pre_p);
8670     }
8671   else
8672     result = tmp;
8673
8674   if (pass_by_ref)
8675     result = build_va_arg_indirect_ref (result);
8676
8677   return result;
8678 }
8679
8680 /* 64 bit floating points memory transfers are paired single precision loads
8681    or store.  So DWARF information needs fixing in little endian (unless
8682    PR=SZ=1 in FPSCR).  */
8683 rtx
8684 sh_dwarf_register_span (rtx reg)
8685 {
8686   unsigned regno = REGNO (reg);
8687
8688   if (WORDS_BIG_ENDIAN || GET_MODE (reg) != DFmode)
8689     return NULL_RTX;
8690
8691   return
8692     gen_rtx_PARALLEL (VOIDmode,
8693                       gen_rtvec (2,
8694                                  gen_rtx_REG (SFmode, regno + 1),
8695                                  gen_rtx_REG (SFmode, regno)));
8696 }
8697
8698 static enum machine_mode
8699 sh_promote_function_mode (const_tree type, enum machine_mode mode,
8700                           int *punsignedp, const_tree funtype,
8701                           int for_return)
8702 {
8703   if (sh_promote_prototypes (funtype))
8704     return promote_mode (type, mode, punsignedp);
8705   else
8706     return default_promote_function_mode (type, mode, punsignedp, funtype,
8707                                           for_return);
8708 }
8709
8710 static bool
8711 sh_promote_prototypes (const_tree type)
8712 {
8713   if (TARGET_HITACHI)
8714     return false;
8715   if (! type)
8716     return true;
8717   return ! sh_attr_renesas_p (type);
8718 }
8719
8720 /* Whether an argument must be passed by reference.  On SHcompact, we
8721    pretend arguments wider than 32-bits that would have been passed in
8722    registers are passed by reference, so that an SHmedia trampoline
8723    loads them into the full 64-bits registers.  */
8724 static int
8725 shcompact_byref (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
8726                  const_tree type, bool named)
8727 {
8728   unsigned HOST_WIDE_INT size;
8729
8730   if (type)
8731     size = int_size_in_bytes (type);
8732   else
8733     size = GET_MODE_SIZE (mode);
8734
8735   if (cum->arg_count[SH_ARG_INT] < NPARM_REGS (SImode)
8736       && (!named
8737           || GET_SH_ARG_CLASS (mode) == SH_ARG_INT
8738           || (GET_SH_ARG_CLASS (mode) == SH_ARG_FLOAT
8739               && cum->arg_count[SH_ARG_FLOAT] >= NPARM_REGS (SFmode)))
8740       && size > 4
8741       && !SHCOMPACT_FORCE_ON_STACK (mode, type)
8742       && !SH5_WOULD_BE_PARTIAL_NREGS (*cum, mode, type, named))
8743     return size;
8744   else
8745     return 0;
8746 }
8747
8748 static bool
8749 sh_pass_by_reference (cumulative_args_t cum_v, enum machine_mode mode,
8750                       const_tree type, bool named)
8751 {
8752   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
8753
8754   if (targetm.calls.must_pass_in_stack (mode, type))
8755     return true;
8756
8757   /* ??? std_gimplify_va_arg_expr passes NULL for cum.  That function
8758      wants to know about pass-by-reference semantics for incoming
8759      arguments.  */
8760   if (! cum)
8761     return false;
8762
8763   if (TARGET_SHCOMPACT)
8764     {
8765       cum->byref = shcompact_byref (cum, mode, type, named);
8766       return cum->byref != 0;
8767     }
8768
8769   return false;
8770 }
8771
8772 static bool
8773 sh_callee_copies (cumulative_args_t cum, enum machine_mode mode,
8774                   const_tree type, bool named ATTRIBUTE_UNUSED)
8775 {
8776   /* ??? How can it possibly be correct to return true only on the
8777      caller side of the equation?  Is there someplace else in the
8778      sh backend that's magically producing the copies?  */
8779   return (get_cumulative_args (cum)->outgoing
8780           && ((mode == BLKmode ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode))
8781               % SH_MIN_ALIGN_FOR_CALLEE_COPY == 0));
8782 }
8783
8784 static int
8785 sh_arg_partial_bytes (cumulative_args_t cum_v, enum machine_mode mode,
8786                       tree type, bool named ATTRIBUTE_UNUSED)
8787 {
8788   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
8789   int words = 0;
8790
8791   if (!TARGET_SH5
8792       && PASS_IN_REG_P (*cum, mode, type)
8793       && !(TARGET_SH4 || TARGET_SH2A_DOUBLE)
8794       && (ROUND_REG (*cum, mode)
8795           + (mode != BLKmode
8796              ? ROUND_ADVANCE (GET_MODE_SIZE (mode))
8797              : ROUND_ADVANCE (int_size_in_bytes (type)))
8798           > NPARM_REGS (mode)))
8799     words = NPARM_REGS (mode) - ROUND_REG (*cum, mode);
8800
8801   else if (!TARGET_SHCOMPACT
8802            && SH5_WOULD_BE_PARTIAL_NREGS (*cum, mode, type, named))
8803     words = NPARM_REGS (SImode) - cum->arg_count[SH_ARG_INT];
8804
8805   return words * UNITS_PER_WORD;
8806 }
8807
8808
8809 /* Define where to put the arguments to a function.
8810    Value is zero to push the argument on the stack,
8811    or a hard register in which to store the argument.
8812
8813    MODE is the argument's machine mode.
8814    TYPE is the data type of the argument (as a tree).
8815     This is null for libcalls where that information may
8816     not be available.
8817    CUM is a variable of type CUMULATIVE_ARGS which gives info about
8818     the preceding args and about the function being called.
8819    NAMED is nonzero if this argument is a named parameter
8820     (otherwise it is an extra parameter matching an ellipsis).
8821
8822    On SH the first args are normally in registers
8823    and the rest are pushed.  Any arg that starts within the first
8824    NPARM_REGS words is at least partially passed in a register unless
8825    its data type forbids.  */
8826 static rtx
8827 sh_function_arg (cumulative_args_t ca_v, enum machine_mode mode,
8828                  const_tree type, bool named)
8829 {
8830   CUMULATIVE_ARGS *ca = get_cumulative_args (ca_v);
8831
8832   if (! TARGET_SH5 && mode == VOIDmode)
8833     return GEN_INT (ca->renesas_abi ? 1 : 0);
8834
8835   if (! TARGET_SH5
8836       && PASS_IN_REG_P (*ca, mode, type)
8837       && (named || ! (TARGET_HITACHI || ca->renesas_abi)))
8838     {
8839       int regno;
8840
8841       if (mode == SCmode && TARGET_SH4 && TARGET_LITTLE_ENDIAN
8842           && (! FUNCTION_ARG_SCmode_WART || (ROUND_REG (*ca, mode) & 1)))
8843         {
8844           rtx r1 = gen_rtx_EXPR_LIST (VOIDmode,
8845                                       gen_rtx_REG (SFmode,
8846                                                    BASE_ARG_REG (mode)
8847                                                    + (ROUND_REG (*ca, mode) ^ 1)),
8848                                       const0_rtx);
8849           rtx r2 = gen_rtx_EXPR_LIST (VOIDmode,
8850                                       gen_rtx_REG (SFmode,
8851                                                    BASE_ARG_REG (mode)
8852                                                    + ((ROUND_REG (*ca, mode) + 1) ^ 1)),
8853                                       GEN_INT (4));
8854           return gen_rtx_PARALLEL(SCmode, gen_rtvec(2, r1, r2));
8855         }
8856
8857      /* If the alignment of a DF value causes an SF register to be
8858         skipped, we will use that skipped register for the next SF
8859         value.  */
8860       if ((TARGET_HITACHI || ca->renesas_abi)
8861           && ca->free_single_fp_reg
8862           && mode == SFmode)
8863         return gen_rtx_REG (mode, ca->free_single_fp_reg);
8864
8865       regno = (BASE_ARG_REG (mode) + ROUND_REG (*ca, mode))
8866                ^ (mode == SFmode && TARGET_SH4
8867                   && TARGET_LITTLE_ENDIAN
8868                   && ! TARGET_HITACHI && ! ca->renesas_abi);
8869       return gen_rtx_REG (mode, regno);
8870
8871     }
8872
8873   if (TARGET_SH5)
8874     {
8875       if (mode == VOIDmode && TARGET_SHCOMPACT)
8876         return GEN_INT (ca->call_cookie);
8877
8878       /* The following test assumes unnamed arguments are promoted to
8879          DFmode.  */
8880       if (mode == SFmode && ca->free_single_fp_reg)
8881         return SH5_PROTOTYPED_FLOAT_ARG (*ca, mode, ca->free_single_fp_reg);
8882
8883       if ((GET_SH_ARG_CLASS (mode) == SH_ARG_FLOAT)
8884           && (named || ! ca->prototype_p)
8885           && ca->arg_count[(int) SH_ARG_FLOAT] < NPARM_REGS (SFmode))
8886         {
8887           if (! ca->prototype_p && TARGET_SHMEDIA)
8888             return SH5_PROTOTYPELESS_FLOAT_ARG (*ca, mode);
8889
8890           return SH5_PROTOTYPED_FLOAT_ARG (*ca, mode,
8891                                            FIRST_FP_PARM_REG
8892                                            + ca->arg_count[(int) SH_ARG_FLOAT]);
8893         }
8894
8895       if (ca->arg_count[(int) SH_ARG_INT] < NPARM_REGS (SImode)
8896           && (! TARGET_SHCOMPACT
8897               || (! SHCOMPACT_FORCE_ON_STACK (mode, type)
8898                   && ! SH5_WOULD_BE_PARTIAL_NREGS (*ca, mode,
8899                                                    type, named))))
8900         {
8901           return gen_rtx_REG (mode, (FIRST_PARM_REG
8902                                        + ca->arg_count[(int) SH_ARG_INT]));
8903         }
8904
8905       return NULL_RTX;
8906     }
8907
8908   return NULL_RTX;
8909 }
8910
8911 /* Update the data in CUM to advance over an argument
8912    of mode MODE and data type TYPE.
8913    (TYPE is null for libcalls where that information may not be
8914    available.)  */
8915 static void
8916 sh_function_arg_advance (cumulative_args_t ca_v, enum machine_mode mode,
8917                          const_tree type, bool named)
8918 {
8919   CUMULATIVE_ARGS *ca = get_cumulative_args (ca_v);
8920
8921   if (ca->force_mem)
8922     ca->force_mem = 0;
8923   else if (TARGET_SH5)
8924     {
8925       const_tree type2 = (ca->byref && type
8926                           ? TREE_TYPE (type)
8927                           : type);
8928       enum machine_mode mode2 = (ca->byref && type
8929                                  ? TYPE_MODE (type2)
8930                                  : mode);
8931       int dwords = ((ca->byref
8932                      ? ca->byref
8933                      : mode2 == BLKmode
8934                      ? int_size_in_bytes (type2)
8935                      : GET_MODE_SIZE (mode2)) + 7) / 8;
8936       int numregs = MIN (dwords, NPARM_REGS (SImode)
8937                          - ca->arg_count[(int) SH_ARG_INT]);
8938
8939       if (numregs)
8940         {
8941           ca->arg_count[(int) SH_ARG_INT] += numregs;
8942           if (TARGET_SHCOMPACT
8943               && SHCOMPACT_FORCE_ON_STACK (mode2, type2))
8944             {
8945               ca->call_cookie
8946                 |= CALL_COOKIE_INT_REG (ca->arg_count[(int) SH_ARG_INT]
8947                                         - numregs, 1);
8948               /* N.B. We want this also for outgoing.  */
8949               ca->stack_regs += numregs;
8950             }
8951           else if (ca->byref)
8952             {
8953               if (! ca->outgoing)
8954                 ca->stack_regs += numregs;
8955               ca->byref_regs += numregs;
8956               ca->byref = 0;
8957               do
8958                 ca->call_cookie
8959                   |= CALL_COOKIE_INT_REG (ca->arg_count[(int) SH_ARG_INT]
8960                                           - numregs, 2);
8961               while (--numregs);
8962               ca->call_cookie
8963                 |= CALL_COOKIE_INT_REG (ca->arg_count[(int) SH_ARG_INT]
8964                                         - 1, 1);
8965             }
8966           else if (dwords > numregs)
8967             {
8968               int pushregs = numregs;
8969
8970               if (TARGET_SHCOMPACT)
8971                 ca->stack_regs += numregs;
8972               while (pushregs < NPARM_REGS (SImode) - 1
8973                      && (CALL_COOKIE_INT_REG_GET
8974                          (ca->call_cookie,
8975                           NPARM_REGS (SImode) - pushregs)
8976                          == 1))
8977                 {
8978                   ca->call_cookie
8979                     &= ~ CALL_COOKIE_INT_REG (NPARM_REGS (SImode)
8980                                               - pushregs, 1);
8981                   pushregs++;
8982                 }
8983               if (numregs == NPARM_REGS (SImode))
8984                 ca->call_cookie
8985                   |= CALL_COOKIE_INT_REG (0, 1)
8986                   | CALL_COOKIE_STACKSEQ (numregs - 1);
8987               else
8988                 ca->call_cookie
8989                   |= CALL_COOKIE_STACKSEQ (numregs);
8990             }
8991         }
8992       if (GET_SH_ARG_CLASS (mode2) == SH_ARG_FLOAT
8993           && (named || ! ca->prototype_p))
8994         {
8995           if (mode2 == SFmode && ca->free_single_fp_reg)
8996             ca->free_single_fp_reg = 0;
8997           else if (ca->arg_count[(int) SH_ARG_FLOAT]
8998                    < NPARM_REGS (SFmode))
8999             {
9000               int numfpregs
9001                 = MIN ((GET_MODE_SIZE (mode2) + 7) / 8 * 2,
9002                        NPARM_REGS (SFmode)
9003                        - ca->arg_count[(int) SH_ARG_FLOAT]);
9004
9005               ca->arg_count[(int) SH_ARG_FLOAT] += numfpregs;
9006
9007               if (TARGET_SHCOMPACT && ! ca->prototype_p)
9008                 {
9009                   if (ca->outgoing && numregs > 0)
9010                     do
9011                       {
9012                         ca->call_cookie
9013                           |= (CALL_COOKIE_INT_REG
9014                               (ca->arg_count[(int) SH_ARG_INT]
9015                                - numregs + ((numfpregs - 2) / 2),
9016                                4 + (ca->arg_count[(int) SH_ARG_FLOAT]
9017                                     - numfpregs) / 2));
9018                       }
9019                     while (numfpregs -= 2);
9020                 }
9021               else if (mode2 == SFmode && (named)
9022                        && (ca->arg_count[(int) SH_ARG_FLOAT]
9023                            < NPARM_REGS (SFmode)))
9024                 ca->free_single_fp_reg
9025                   = FIRST_FP_PARM_REG - numfpregs
9026                   + ca->arg_count[(int) SH_ARG_FLOAT] + 1;
9027             }
9028         }
9029       return;
9030     }
9031
9032   if ((TARGET_HITACHI || ca->renesas_abi) && TARGET_FPU_DOUBLE)
9033     {
9034       /* Note that we've used the skipped register.  */
9035       if (mode == SFmode && ca->free_single_fp_reg)
9036         {
9037           ca->free_single_fp_reg = 0;
9038           return;
9039         }
9040       /* When we have a DF after an SF, there's an SF register that get
9041          skipped in order to align the DF value.  We note this skipped
9042          register, because the next SF value will use it, and not the
9043          SF that follows the DF.  */
9044       if (mode == DFmode
9045           && ROUND_REG (*ca, DFmode) != ROUND_REG (*ca, SFmode))
9046         {
9047           ca->free_single_fp_reg = (ROUND_REG (*ca, SFmode)
9048                                     + BASE_ARG_REG (mode));
9049         }
9050     }
9051
9052   if (! ((TARGET_SH4 || TARGET_SH2A) || ca->renesas_abi)
9053       || PASS_IN_REG_P (*ca, mode, type))
9054     (ca->arg_count[(int) GET_SH_ARG_CLASS (mode)]
9055      = (ROUND_REG (*ca, mode)
9056         + (mode == BLKmode
9057            ? ROUND_ADVANCE (int_size_in_bytes (type))
9058            : ROUND_ADVANCE (GET_MODE_SIZE (mode)))));
9059 }
9060
9061 /* The Renesas calling convention doesn't quite fit into this scheme since
9062    the address is passed like an invisible argument, but one that is always
9063    passed in memory.  */
9064 static rtx
9065 sh_struct_value_rtx (tree fndecl, int incoming ATTRIBUTE_UNUSED)
9066 {
9067   if (TARGET_HITACHI || sh_attr_renesas_p (fndecl))
9068     return NULL_RTX;
9069   return gen_rtx_REG (Pmode, 2);
9070 }
9071
9072 /* Worker function for TARGET_FUNCTION_VALUE.
9073
9074    For the SH, this is like LIBCALL_VALUE, except that we must change the
9075    mode like PROMOTE_MODE does.
9076    ??? PROMOTE_MODE is ignored for non-scalar types.  The set of types
9077    tested here has to be kept in sync with the one in
9078    explow.c:promote_mode.  */
9079 static rtx
9080 sh_function_value (const_tree valtype,
9081                    const_tree fn_decl_or_type,
9082                    bool outgoing ATTRIBUTE_UNUSED)
9083 {
9084   if (fn_decl_or_type
9085       && !DECL_P (fn_decl_or_type))
9086     fn_decl_or_type = NULL;
9087
9088   return gen_rtx_REG (
9089            ((GET_MODE_CLASS (TYPE_MODE (valtype)) == MODE_INT
9090              && GET_MODE_SIZE (TYPE_MODE (valtype)) < 4
9091              && (TREE_CODE (valtype) == INTEGER_TYPE
9092                  || TREE_CODE (valtype) == ENUMERAL_TYPE
9093                  || TREE_CODE (valtype) == BOOLEAN_TYPE
9094                  || TREE_CODE (valtype) == REAL_TYPE
9095                  || TREE_CODE (valtype) == OFFSET_TYPE))
9096             && sh_promote_prototypes (fn_decl_or_type)
9097             ? (TARGET_SHMEDIA64 ? DImode : SImode) : TYPE_MODE (valtype)),
9098            BASE_RETURN_VALUE_REG (TYPE_MODE (valtype)));
9099 }
9100
9101 /* Worker function for TARGET_LIBCALL_VALUE.  */
9102 static rtx
9103 sh_libcall_value (enum machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
9104 {
9105   return gen_rtx_REG (mode, BASE_RETURN_VALUE_REG (mode));
9106 }
9107
9108 /* Return true if N is a possible register number of function value.  */
9109 static bool
9110 sh_function_value_regno_p (const unsigned int regno)
9111 {
9112   return ((regno) == FIRST_RET_REG 
9113           || (TARGET_SH2E && (regno) == FIRST_FP_RET_REG)
9114           || (TARGET_SHMEDIA_FPU && (regno) == FIRST_FP_RET_REG));
9115 }
9116
9117 /* Worker function for TARGET_RETURN_IN_MEMORY.  */
9118 static bool
9119 sh_return_in_memory (const_tree type, const_tree fndecl)
9120 {
9121   if (TARGET_SH5)
9122     {
9123       if (TYPE_MODE (type) == BLKmode)
9124         return ((unsigned HOST_WIDE_INT) int_size_in_bytes (type)) > 8;
9125       else
9126         return GET_MODE_SIZE (TYPE_MODE (type)) > 8;
9127     }
9128   else
9129     {
9130       return (TYPE_MODE (type) == BLKmode
9131               || ((TARGET_HITACHI || sh_attr_renesas_p (fndecl))
9132                   && TREE_CODE (type) == RECORD_TYPE));
9133     }
9134 }
9135
9136 /* We actually emit the code in sh_expand_prologue.  We used to use
9137    a static variable to flag that we need to emit this code, but that
9138    doesn't when inlining, when functions are deferred and then emitted
9139    later.  Fortunately, we already have two flags that are part of struct
9140    function that tell if a function uses varargs or stdarg.  */
9141 static void
9142 sh_setup_incoming_varargs (cumulative_args_t ca,
9143                            enum machine_mode mode,
9144                            tree type,
9145                            int *pretend_arg_size,
9146                            int second_time ATTRIBUTE_UNUSED)
9147 {
9148   gcc_assert (cfun->stdarg);
9149   if (TARGET_VARARGS_PRETEND_ARGS (current_function_decl))
9150     {
9151       int named_parm_regs, anon_parm_regs;
9152
9153       named_parm_regs = (ROUND_REG (*get_cumulative_args (ca), mode)
9154                          + (mode == BLKmode
9155                             ? ROUND_ADVANCE (int_size_in_bytes (type))
9156                             : ROUND_ADVANCE (GET_MODE_SIZE (mode))));
9157       anon_parm_regs = NPARM_REGS (SImode) - named_parm_regs;
9158       if (anon_parm_regs > 0)
9159         *pretend_arg_size = anon_parm_regs * 4;
9160     }
9161 }
9162
9163 static bool
9164 sh_strict_argument_naming (cumulative_args_t ca ATTRIBUTE_UNUSED)
9165 {
9166   return TARGET_SH5;
9167 }
9168
9169 static bool
9170 sh_pretend_outgoing_varargs_named (cumulative_args_t ca_v)
9171 {
9172   CUMULATIVE_ARGS *ca = get_cumulative_args (ca_v);
9173
9174   return ! (TARGET_HITACHI || ca->renesas_abi) && ! TARGET_SH5;
9175 }
9176
9177
9178 /* Define the offset between two registers, one to be eliminated, and
9179    the other its replacement, at the start of a routine.  */
9180 int
9181 initial_elimination_offset (int from, int to)
9182 {
9183   int regs_saved;
9184   int regs_saved_rounding = 0;
9185   int total_saved_regs_space;
9186   int total_auto_space;
9187   int save_flags = target_flags;
9188   int copy_flags;
9189   HARD_REG_SET live_regs_mask;
9190
9191   shmedia_space_reserved_for_target_registers = false;
9192   regs_saved = calc_live_regs (&live_regs_mask);
9193   regs_saved += SHMEDIA_REGS_STACK_ADJUST ();
9194
9195   if (shmedia_reserve_space_for_target_registers_p (regs_saved, &live_regs_mask))
9196     {
9197       shmedia_space_reserved_for_target_registers = true;
9198       regs_saved += shmedia_target_regs_stack_adjust (&live_regs_mask);
9199     }
9200
9201   if (TARGET_SH5 && regs_saved % (STACK_BOUNDARY / BITS_PER_UNIT))
9202     regs_saved_rounding = ((STACK_BOUNDARY / BITS_PER_UNIT)
9203                            - regs_saved % (STACK_BOUNDARY / BITS_PER_UNIT));
9204
9205   total_auto_space = rounded_frame_size (regs_saved) - regs_saved_rounding;
9206   copy_flags = target_flags;
9207   target_flags = save_flags;
9208
9209   total_saved_regs_space = regs_saved + regs_saved_rounding;
9210
9211   if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
9212     return total_saved_regs_space + total_auto_space
9213            + crtl->args.info.byref_regs * 8;
9214
9215   if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
9216     return total_saved_regs_space + total_auto_space
9217            + crtl->args.info.byref_regs * 8;
9218
9219   /* Initial gap between fp and sp is 0.  */
9220   if (from == HARD_FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
9221     return 0;
9222
9223   if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
9224     return rounded_frame_size (0);
9225
9226   if (from == FRAME_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
9227     return rounded_frame_size (0);
9228
9229   gcc_assert (from == RETURN_ADDRESS_POINTER_REGNUM
9230               && (to == HARD_FRAME_POINTER_REGNUM
9231                   || to == STACK_POINTER_REGNUM));
9232   if (TARGET_SH5)
9233     {
9234       int n = total_saved_regs_space;
9235       int pr_reg = TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG;
9236       save_schedule schedule;
9237       save_entry *entry;
9238
9239       n += total_auto_space;
9240
9241       /* If it wasn't saved, there's not much we can do.  */
9242       if (! TEST_HARD_REG_BIT (live_regs_mask, pr_reg))
9243         return n;
9244
9245       target_flags = copy_flags;
9246
9247       sh5_schedule_saves (&live_regs_mask, &schedule, n);
9248       for (entry = &schedule.entries[1]; entry->mode != VOIDmode; entry++)
9249         if (entry->reg == pr_reg)
9250           {
9251             target_flags = save_flags;
9252             return entry->offset;
9253           }
9254       gcc_unreachable ();
9255     }
9256   else
9257     return total_auto_space;
9258 }
9259
9260 /* Parse the -mfixed-range= option string.  */
9261 void
9262 sh_fix_range (const char *const_str)
9263 {
9264   int i, first, last;
9265   char *str, *dash, *comma;
9266
9267   /* str must be of the form REG1'-'REG2{,REG1'-'REG} where REG1 and
9268      REG2 are either register names or register numbers.  The effect
9269      of this option is to mark the registers in the range from REG1 to
9270      REG2 as ``fixed'' so they won't be used by the compiler.  */
9271
9272   i = strlen (const_str);
9273   str = (char *) alloca (i + 1);
9274   memcpy (str, const_str, i + 1);
9275
9276   while (1)
9277     {
9278       dash = strchr (str, '-');
9279       if (!dash)
9280         {
9281           warning (0, "value of -mfixed-range must have form REG1-REG2");
9282           return;
9283         }
9284       *dash = '\0';
9285       comma = strchr (dash + 1, ',');
9286       if (comma)
9287         *comma = '\0';
9288
9289       first = decode_reg_name (str);
9290       if (first < 0)
9291         {
9292           warning (0, "unknown register name: %s", str);
9293           return;
9294         }
9295
9296       last = decode_reg_name (dash + 1);
9297       if (last < 0)
9298         {
9299           warning (0, "unknown register name: %s", dash + 1);
9300           return;
9301         }
9302
9303       *dash = '-';
9304
9305       if (first > last)
9306         {
9307           warning (0, "%s-%s is an empty range", str, dash + 1);
9308           return;
9309         }
9310
9311       for (i = first; i <= last; ++i)
9312         fixed_regs[i] = call_used_regs[i] = 1;
9313
9314       if (!comma)
9315         break;
9316
9317       *comma = ',';
9318       str = comma + 1;
9319     }
9320 }
9321 \f
9322 /* Insert any deferred function attributes from earlier pragmas.  */
9323 static void
9324 sh_insert_attributes (tree node, tree *attributes)
9325 {
9326   tree attrs;
9327
9328   if (TREE_CODE (node) != FUNCTION_DECL)
9329     return;
9330
9331   /* We are only interested in fields.  */
9332   if (!DECL_P (node))
9333     return;
9334
9335   /* Append the attributes to the deferred attributes.  */
9336   *sh_deferred_function_attributes_tail = *attributes;
9337   attrs = sh_deferred_function_attributes;
9338   if (!attrs)
9339     return;
9340
9341   /* Some attributes imply or require the interrupt attribute.  */
9342   if (!lookup_attribute ("interrupt_handler", attrs)
9343       && !lookup_attribute ("interrupt_handler", DECL_ATTRIBUTES (node)))
9344     {
9345       /* If we have a trapa_handler, but no interrupt_handler attribute,
9346          insert an interrupt_handler attribute.  */
9347       if (lookup_attribute ("trapa_handler", attrs) != NULL_TREE)
9348         /* We can't use sh_pr_interrupt here because that's not in the
9349            java frontend.  */
9350         attrs
9351           = tree_cons (get_identifier("interrupt_handler"), NULL_TREE, attrs);
9352       /* However, for sp_switch, trap_exit, nosave_low_regs and resbank,
9353          if the interrupt attribute is missing, we ignore the attribute
9354          and warn.  */
9355       else if (lookup_attribute ("sp_switch", attrs)
9356                || lookup_attribute ("trap_exit", attrs)
9357                || lookup_attribute ("nosave_low_regs", attrs)
9358                || lookup_attribute ("resbank", attrs))
9359         {
9360           tree *tail;
9361
9362           for (tail = attributes; attrs; attrs = TREE_CHAIN (attrs))
9363             {
9364               if (is_attribute_p ("sp_switch", TREE_PURPOSE (attrs))
9365                   || is_attribute_p ("trap_exit", TREE_PURPOSE (attrs))
9366                   || is_attribute_p ("nosave_low_regs", TREE_PURPOSE (attrs))
9367                   || is_attribute_p ("resbank", TREE_PURPOSE (attrs)))
9368                 warning (OPT_Wattributes,
9369                          "%qE attribute only applies to interrupt functions",
9370                          TREE_PURPOSE (attrs));
9371               else
9372                 {
9373                   *tail = tree_cons (TREE_PURPOSE (attrs), NULL_TREE,
9374                                      NULL_TREE);
9375                   tail = &TREE_CHAIN (*tail);
9376                 }
9377             }
9378           attrs = *attributes;
9379         }
9380     }
9381
9382   /* Install the processed list.  */
9383   *attributes = attrs;
9384
9385   /* Clear deferred attributes.  */
9386   sh_deferred_function_attributes = NULL_TREE;
9387   sh_deferred_function_attributes_tail = &sh_deferred_function_attributes;
9388
9389   return;
9390 }
9391
9392 /*------------------------------------------------------------------------------
9393   Target specific attributes
9394   Supported attributes are:
9395
9396    * interrupt_handler
9397         Specifies this function is an interrupt handler.
9398
9399    * trapa_handler
9400         Like interrupt_handler, but don't save all registers.
9401
9402    * sp_switch
9403         Specifies an alternate stack for an interrupt handler to run on.
9404
9405    * trap_exit
9406         Use a trapa to exit an interrupt function instead of rte.
9407
9408    * nosave_low_regs
9409         Don't save r0..r7 in an interrupt handler function.
9410         This is useful on SH3* and SH4*, which have a separate set of low
9411         regs for user and privileged modes.
9412         This is mainly to be used for non-reentrant interrupt handlers (i.e.
9413         those that run with interrupts disabled and thus can't be
9414         interrupted thenselves).
9415
9416    * renesas
9417         Use Renesas calling/layout conventions (functions and structures).
9418
9419    * resbank
9420         In case of an interrupt handler function, use a register bank to
9421         save registers R0-R14, MACH, MACL, GBR and PR.
9422         This is available only on SH2A targets.
9423
9424    * function_vector
9425         Declares a function to be called using the TBR relative addressing
9426         mode.  Takes an argument that specifies the slot number in the table
9427         where this function can be looked up by the JSR/N @@(disp8,TBR) insn.
9428 */
9429
9430 /* Handle a 'resbank' attribute.  */
9431 static tree
9432 sh_handle_resbank_handler_attribute (tree * node, tree name,
9433                                      tree args ATTRIBUTE_UNUSED,
9434                                      int flags ATTRIBUTE_UNUSED,
9435                                      bool * no_add_attrs)
9436 {
9437   if (!TARGET_SH2A)
9438     {
9439       warning (OPT_Wattributes, "%qE attribute is supported only for SH2A",
9440                name);
9441       *no_add_attrs = true;
9442     }
9443   if (TREE_CODE (*node) != FUNCTION_DECL)
9444     {
9445       warning (OPT_Wattributes, "%qE attribute only applies to functions",
9446                name);
9447       *no_add_attrs = true;
9448     }
9449
9450   return NULL_TREE;
9451 }
9452
9453 /* Handle an "interrupt_handler" attribute; arguments as in
9454    struct attribute_spec.handler.  */
9455 static tree
9456 sh_handle_interrupt_handler_attribute (tree *node, tree name,
9457                                        tree args ATTRIBUTE_UNUSED,
9458                                        int flags ATTRIBUTE_UNUSED,
9459                                        bool *no_add_attrs)
9460 {
9461   if (TREE_CODE (*node) != FUNCTION_DECL)
9462     {
9463       warning (OPT_Wattributes, "%qE attribute only applies to functions",
9464                name);
9465       *no_add_attrs = true;
9466     }
9467   else if (TARGET_SHCOMPACT)
9468     {
9469       error ("attribute interrupt_handler is not compatible with -m5-compact");
9470       *no_add_attrs = true;
9471     }
9472
9473   return NULL_TREE;
9474 }
9475
9476 /* Handle an 'function_vector' attribute; arguments as in
9477    struct attribute_spec.handler.  */
9478 static tree
9479 sh2a_handle_function_vector_handler_attribute (tree * node, tree name,
9480                                                tree args ATTRIBUTE_UNUSED,
9481                                                int flags ATTRIBUTE_UNUSED,
9482                                                bool * no_add_attrs)
9483 {
9484   if (!TARGET_SH2A)
9485     {
9486       warning (OPT_Wattributes, "%qE attribute only applies to SH2A",
9487                name);
9488       *no_add_attrs = true;
9489     }
9490   else if (TREE_CODE (*node) != FUNCTION_DECL)
9491     {
9492       warning (OPT_Wattributes, "%qE attribute only applies to functions",
9493                name);
9494       *no_add_attrs = true;
9495     }
9496   else if (TREE_CODE (TREE_VALUE (args)) != INTEGER_CST)
9497     {
9498       /* The argument must be a constant integer.  */
9499       warning (OPT_Wattributes,
9500                "%qE attribute argument not an integer constant",
9501                name);
9502       *no_add_attrs = true;
9503     }
9504   else if (TREE_INT_CST_LOW (TREE_VALUE (args)) > 255)
9505     {
9506       /* The argument value must be between 0 to 255.  */
9507       warning (OPT_Wattributes,
9508                "%qE attribute argument should be between 0 to 255",
9509                name);
9510       *no_add_attrs = true;
9511     }
9512   return NULL_TREE;
9513 }
9514
9515 /* Returns true if current function has been assigned the attribute
9516    'function_vector'.  */
9517 bool
9518 sh2a_is_function_vector_call (rtx x)
9519 {
9520   if (GET_CODE (x) == SYMBOL_REF
9521       && (SYMBOL_REF_FLAGS (x) & SYMBOL_FLAG_FUNCVEC_FUNCTION))
9522     {
9523       tree tr = SYMBOL_REF_DECL (x);
9524
9525       if (sh2a_function_vector_p (tr))
9526         return true;
9527     }
9528
9529   return false;
9530 }
9531
9532 /* Returns the function vector number, if the attribute
9533    'function_vector' is assigned, otherwise returns zero.  */
9534 int
9535 sh2a_get_function_vector_number (rtx x)
9536 {
9537   int num;
9538   tree list, t;
9539
9540   if ((GET_CODE (x) == SYMBOL_REF)
9541       && (SYMBOL_REF_FLAGS (x) & SYMBOL_FLAG_FUNCVEC_FUNCTION))
9542     {
9543       t = SYMBOL_REF_DECL (x);
9544
9545       if (TREE_CODE (t) != FUNCTION_DECL)
9546         return 0;
9547
9548       list = SH_ATTRIBUTES (t);
9549       while (list)
9550         {
9551           if (is_attribute_p ("function_vector", TREE_PURPOSE (list)))
9552             {
9553               num = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (list)));
9554               return num;
9555             }
9556
9557           list = TREE_CHAIN (list);
9558         }
9559
9560       return 0;
9561     }
9562   else
9563     return 0;
9564 }
9565
9566 /* Handle an "sp_switch" attribute; arguments as in
9567    struct attribute_spec.handler.  */
9568 static tree
9569 sh_handle_sp_switch_attribute (tree *node, tree name, tree args,
9570                                int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
9571 {
9572   if (TREE_CODE (*node) != FUNCTION_DECL)
9573     {
9574       warning (OPT_Wattributes, "%qE attribute only applies to functions",
9575                name);
9576       *no_add_attrs = true;
9577     }
9578   else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
9579     {
9580       /* The argument must be a constant string.  */
9581       warning (OPT_Wattributes, "%qE attribute argument not a string constant",
9582                name);
9583       *no_add_attrs = true;
9584     }
9585
9586   return NULL_TREE;
9587 }
9588
9589 /* Handle an "trap_exit" attribute; arguments as in
9590    struct attribute_spec.handler.  */
9591 static tree
9592 sh_handle_trap_exit_attribute (tree *node, tree name, tree args,
9593                                int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
9594 {
9595   if (TREE_CODE (*node) != FUNCTION_DECL)
9596     {
9597       warning (OPT_Wattributes, "%qE attribute only applies to functions",
9598                name);
9599       *no_add_attrs = true;
9600     }
9601   /* The argument specifies a trap number to be used in a trapa instruction
9602      at function exit (instead of an rte instruction).  */
9603   else if (TREE_CODE (TREE_VALUE (args)) != INTEGER_CST)
9604     {
9605       /* The argument must be a constant integer.  */
9606       warning (OPT_Wattributes, "%qE attribute argument not an "
9607                "integer constant", name);
9608       *no_add_attrs = true;
9609     }
9610
9611   return NULL_TREE;
9612 }
9613
9614 static tree
9615 sh_handle_renesas_attribute (tree *node ATTRIBUTE_UNUSED,
9616                              tree name ATTRIBUTE_UNUSED,
9617                              tree args ATTRIBUTE_UNUSED,
9618                              int flags ATTRIBUTE_UNUSED,
9619                              bool *no_add_attrs ATTRIBUTE_UNUSED)
9620 {
9621   return NULL_TREE;
9622 }
9623
9624 /* True if __attribute__((renesas)) or -mrenesas.  */
9625 bool
9626 sh_attr_renesas_p (const_tree td)
9627 {
9628   if (TARGET_HITACHI)
9629     return true;
9630   if (td == NULL_TREE)
9631     return false;
9632   if (DECL_P (td))
9633     td = TREE_TYPE (td);
9634   if (td == error_mark_node)
9635     return false;
9636   return (lookup_attribute ("renesas", TYPE_ATTRIBUTES (td))
9637           != NULL_TREE);
9638 }
9639
9640 /* True if __attribute__((renesas)) or -mrenesas, for the current
9641    function.  */
9642 bool
9643 sh_cfun_attr_renesas_p (void)
9644 {
9645   return sh_attr_renesas_p (current_function_decl);
9646 }
9647
9648 /* Returns true if the current function has the "interrupt_handler"
9649    attribute set.  */
9650 bool
9651 sh_cfun_interrupt_handler_p (void)
9652 {
9653   return (lookup_attribute ("interrupt_handler",
9654                             DECL_ATTRIBUTES (current_function_decl))
9655           != NULL_TREE);
9656 }
9657
9658 /* Returns true if FUNC has been assigned the attribute
9659    "function_vector".  */
9660 bool
9661 sh2a_function_vector_p (tree func)
9662 {
9663   tree list;
9664   if (TREE_CODE (func) != FUNCTION_DECL)
9665     return false;
9666
9667   list = SH_ATTRIBUTES (func);
9668   while (list)
9669     {
9670       if (is_attribute_p ("function_vector", TREE_PURPOSE (list)))
9671         return true;
9672
9673       list = TREE_CHAIN (list);
9674     }
9675   return false;
9676 }
9677
9678 /* Returns true if given tree has the "resbank" attribute set.  */
9679 bool
9680 sh_cfun_resbank_handler_p (void)
9681 {
9682   return ((lookup_attribute ("resbank",
9683                              DECL_ATTRIBUTES (current_function_decl))
9684           != NULL_TREE)
9685           && (lookup_attribute ("interrupt_handler",
9686                                 DECL_ATTRIBUTES (current_function_decl))
9687               != NULL_TREE) && TARGET_SH2A);
9688 }
9689
9690 /* Returns true if the current function has a "trap_exit" attribute set.  */
9691 bool
9692 sh_cfun_trap_exit_p (void)
9693 {
9694   return lookup_attribute ("trap_exit", DECL_ATTRIBUTES (current_function_decl))
9695          != NULL_TREE;
9696 }
9697
9698 /* Implement TARGET_CHECK_PCH_TARGET_FLAGS.  */
9699 static const char *
9700 sh_check_pch_target_flags (int old_flags)
9701 {
9702   if ((old_flags ^ target_flags) & (MASK_SH1 | MASK_SH2 | MASK_SH3
9703                                     | MASK_SH_E | MASK_HARD_SH4
9704                                     | MASK_FPU_SINGLE | MASK_SH4))
9705     return _("created and used with different architectures / ABIs");
9706   if ((old_flags ^ target_flags) & MASK_HITACHI)
9707     return _("created and used with different ABIs");
9708   if ((old_flags ^ target_flags) & MASK_LITTLE_ENDIAN)
9709     return _("created and used with different endianness");
9710   return NULL;
9711 }
9712 \f
9713 /* Predicates used by the templates.  */
9714
9715 /* Returns true if OP is MACL, MACH or PR.  The input must be a REG rtx.
9716    Used only in general_movsrc_operand.  */
9717 bool
9718 system_reg_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
9719 {
9720   switch (REGNO (op))
9721     {
9722     case PR_REG:
9723     case MACL_REG:
9724     case MACH_REG:
9725       return true;
9726     }
9727   return false;
9728 }
9729
9730 /* Returns true if OP is a floating point value with value 0.0.  */
9731 bool
9732 fp_zero_operand (rtx op)
9733 {
9734   REAL_VALUE_TYPE r;
9735
9736   if (GET_MODE (op) != SFmode)
9737     return false;
9738
9739   REAL_VALUE_FROM_CONST_DOUBLE (r, op);
9740   return REAL_VALUES_EQUAL (r, dconst0) && ! REAL_VALUE_MINUS_ZERO (r);
9741 }
9742
9743 /* Returns true if OP is a floating point value with value 1.0.  */
9744 bool
9745 fp_one_operand (rtx op)
9746 {
9747   REAL_VALUE_TYPE r;
9748
9749   if (GET_MODE (op) != SFmode)
9750     return false;
9751
9752   REAL_VALUE_FROM_CONST_DOUBLE (r, op);
9753   return REAL_VALUES_EQUAL (r, dconst1);
9754 }
9755
9756 /* In general mode switching is used.  If we are
9757    compiling without -mfmovd, movsf_ie isn't taken into account for
9758    mode switching.  We could check in machine_dependent_reorg for
9759    cases where we know we are in single precision mode, but there is
9760    interface to find that out during reload, so we must avoid
9761    choosing an fldi alternative during reload and thus failing to
9762    allocate a scratch register for the constant loading.  */
9763 bool
9764 fldi_ok (void)
9765 {
9766   return true;
9767 }
9768
9769 /* Return the TLS type for TLS symbols.  */
9770 enum tls_model
9771 tls_symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
9772 {
9773   if (GET_CODE (op) != SYMBOL_REF)
9774     return TLS_MODEL_NONE;
9775   return SYMBOL_REF_TLS_MODEL (op);
9776 }
9777 \f
9778 /* Return the destination address of a branch.  */
9779 static int
9780 branch_dest (rtx branch)
9781 {
9782   rtx dest = SET_SRC (PATTERN (branch));
9783   int dest_uid;
9784
9785   if (GET_CODE (dest) == IF_THEN_ELSE)
9786     dest = XEXP (dest, 1);
9787   dest = XEXP (dest, 0);
9788   dest_uid = INSN_UID (dest);
9789   return INSN_ADDRESSES (dest_uid);
9790 }
9791 \f
9792 /* Return nonzero if REG is not used after INSN.
9793    We assume REG is a reload reg, and therefore does
9794    not live past labels.  It may live past calls or jumps though.  */
9795 bool
9796 reg_unused_after (rtx reg, rtx insn)
9797 {
9798   enum rtx_code code;
9799   rtx set;
9800
9801   /* If the reg is set by this instruction, then it is safe for our
9802      case.  Disregard the case where this is a store to memory, since
9803      we are checking a register used in the store address.  */
9804   set = single_set (insn);
9805   if (set && !MEM_P (SET_DEST (set))
9806       && reg_overlap_mentioned_p (reg, SET_DEST (set)))
9807     return true;
9808
9809   while ((insn = NEXT_INSN (insn)))
9810     {
9811       rtx set;
9812       if (!INSN_P (insn))
9813         continue;
9814
9815       code = GET_CODE (insn);
9816
9817 #if 0
9818       /* If this is a label that existed before reload, then the register
9819          is dead here.  However, if this is a label added by reorg, then
9820          the register may still be live here.  We can't tell the difference,
9821          so we just ignore labels completely.  */
9822       if (code == CODE_LABEL)
9823         return 1;
9824       /* else */
9825 #endif
9826
9827       if (code == JUMP_INSN)
9828         return false;
9829
9830       /* If this is a sequence, we must handle them all at once.
9831          We could have for instance a call that sets the target register,
9832          and an insn in a delay slot that uses the register.  In this case,
9833          we must return 0.  */
9834       else if (code == INSN && GET_CODE (PATTERN (insn)) == SEQUENCE)
9835         {
9836           int i;
9837           int retval = 0;
9838
9839           for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
9840             {
9841               rtx this_insn = XVECEXP (PATTERN (insn), 0, i);
9842               rtx set = single_set (this_insn);
9843
9844               if (CALL_P (this_insn))
9845                 code = CALL_INSN;
9846               else if (JUMP_P (this_insn))
9847                 {
9848                   if (INSN_ANNULLED_BRANCH_P (this_insn))
9849                     return false;
9850                   code = JUMP_INSN;
9851                 }
9852
9853               if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
9854                 return false;
9855               if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
9856                 {
9857                   if (!MEM_P (SET_DEST (set)))
9858                     retval = true;
9859                   else
9860                     return false;
9861                 }
9862               if (set == NULL_RTX
9863                   && reg_overlap_mentioned_p (reg, PATTERN (this_insn)))
9864                 return false;
9865             }
9866           if (retval == 1)
9867             return true;
9868           else if (code == JUMP_INSN)
9869             return false;
9870         }
9871
9872       set = single_set (insn);
9873       if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
9874         return false;
9875       if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
9876         return !MEM_P (SET_DEST (set));
9877       if (set == 0 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
9878         return false;
9879
9880       if (code == CALL_INSN && call_really_used_regs[REGNO (reg)])
9881         return true;
9882     }
9883   return true;
9884 }
9885 \f
9886 #include "ggc.h"
9887
9888 static GTY(()) rtx t_reg_rtx;
9889 rtx
9890 get_t_reg_rtx (void)
9891 {
9892   if (! t_reg_rtx)
9893     t_reg_rtx = gen_rtx_REG (SImode, T_REG);
9894   return t_reg_rtx;
9895 }
9896
9897 static GTY(()) rtx fpscr_rtx;
9898 rtx
9899 get_fpscr_rtx (void)
9900 {
9901   if (! fpscr_rtx)
9902     {
9903       fpscr_rtx = gen_rtx_REG (PSImode, FPSCR_REG);
9904       REG_USERVAR_P (fpscr_rtx) = 1;
9905       mark_user_reg (fpscr_rtx);
9906     }
9907   if (! reload_completed || mdep_reorg_phase != SH_AFTER_MDEP_REORG)
9908     mark_user_reg (fpscr_rtx);
9909   return fpscr_rtx;
9910 }
9911
9912 static GTY(()) tree fpscr_values;
9913
9914 static void
9915 emit_fpu_switch (rtx scratch, int index)
9916 {
9917   rtx dst, src;
9918
9919   if (fpscr_values == NULL)
9920     {
9921       tree t;
9922
9923       t = build_index_type (integer_one_node);
9924       t = build_array_type (integer_type_node, t);
9925       t = build_decl (BUILTINS_LOCATION,
9926                       VAR_DECL, get_identifier ("__fpscr_values"), t);
9927       DECL_ARTIFICIAL (t) = 1;
9928       DECL_IGNORED_P (t) = 1;
9929       DECL_EXTERNAL (t) = 1;
9930       TREE_STATIC (t) = 1;
9931       TREE_PUBLIC (t) = 1;
9932       TREE_USED (t) = 1;
9933
9934       fpscr_values = t;
9935     }
9936
9937   src = DECL_RTL (fpscr_values);
9938   if (!can_create_pseudo_p ())
9939     {
9940       emit_move_insn (scratch, XEXP (src, 0));
9941       if (index != 0)
9942         emit_insn (gen_addsi3 (scratch, scratch, GEN_INT (index * 4)));
9943       src = adjust_automodify_address (src, PSImode, scratch, index * 4);
9944     }
9945   else
9946     src = adjust_address (src, PSImode, index * 4);
9947
9948   dst = get_fpscr_rtx ();
9949   emit_move_insn (dst, src);
9950 }
9951
9952 void
9953 emit_sf_insn (rtx pat)
9954 {
9955   emit_insn (pat);
9956 }
9957
9958 void
9959 emit_df_insn (rtx pat)
9960 {
9961   emit_insn (pat);
9962 }
9963
9964 void
9965 expand_sf_unop (rtx (*fun) (rtx, rtx, rtx), rtx *operands)
9966 {
9967   emit_sf_insn ((*fun) (operands[0], operands[1], get_fpscr_rtx ()));
9968 }
9969
9970 void
9971 expand_sf_binop (rtx (*fun) (rtx, rtx, rtx, rtx), rtx *operands)
9972 {
9973   emit_sf_insn ((*fun) (operands[0], operands[1], operands[2],
9974                          get_fpscr_rtx ()));
9975 }
9976
9977 void
9978 expand_df_unop (rtx (*fun) (rtx, rtx, rtx), rtx *operands)
9979 {
9980   emit_df_insn ((*fun) (operands[0], operands[1], get_fpscr_rtx ()));
9981 }
9982
9983 void
9984 expand_df_binop (rtx (*fun) (rtx, rtx, rtx, rtx), rtx *operands)
9985 {
9986   emit_df_insn ((*fun) (operands[0], operands[1], operands[2],
9987                         get_fpscr_rtx ()));
9988 }
9989 \f
9990 static rtx get_free_reg (HARD_REG_SET);
9991
9992 /* This function returns a register to use to load the address to load
9993    the fpscr from.  Currently it always returns r1 or r7, but when we are
9994    able to use pseudo registers after combine, or have a better mechanism
9995    for choosing a register, it should be done here.  */
9996 /* REGS_LIVE is the liveness information for the point for which we
9997    need this allocation.  In some bare-bones exit blocks, r1 is live at the
9998    start.  We can even have all of r0..r3 being live:
9999 __complex__ long long f (double d) { if (d == 0) return 2; else return 3; }
10000    INSN before which new insns are placed with will clobber the register
10001    we return.  If a basic block consists only of setting the return value
10002    register to a pseudo and using that register, the return value is not
10003    live before or after this block, yet we we'll insert our insns right in
10004    the middle.  */
10005 static rtx
10006 get_free_reg (HARD_REG_SET regs_live)
10007 {
10008   if (! TEST_HARD_REG_BIT (regs_live, 1))
10009     return gen_rtx_REG (Pmode, 1);
10010
10011   /* Hard reg 1 is live; since this is a small register classes target,
10012      there shouldn't be anything but a jump before the function end.  */
10013   gcc_assert (!TEST_HARD_REG_BIT (regs_live, 7));
10014   return gen_rtx_REG (Pmode, 7);
10015 }
10016
10017 /* This function will set the fpscr from memory.
10018    MODE is the mode we are setting it to.  */
10019 void
10020 fpscr_set_from_mem (int mode, HARD_REG_SET regs_live)
10021 {
10022   enum attr_fp_mode fp_mode = (enum attr_fp_mode) mode;
10023   enum attr_fp_mode norm_mode = ACTUAL_NORMAL_MODE (FP_MODE);
10024   rtx addr_reg;
10025
10026   addr_reg = !can_create_pseudo_p () ? get_free_reg (regs_live) : NULL_RTX;
10027   emit_fpu_switch (addr_reg, fp_mode == norm_mode);
10028 }
10029
10030 /* Is the given character a logical line separator for the assembler?  */
10031 #ifndef IS_ASM_LOGICAL_LINE_SEPARATOR
10032 #define IS_ASM_LOGICAL_LINE_SEPARATOR(C, STR) ((C) == ';')
10033 #endif
10034
10035 static bool
10036 sequence_insn_p (rtx insn)
10037 {
10038   rtx prev, next;
10039
10040   prev = PREV_INSN (insn);
10041   if (prev == NULL)
10042     return false;
10043
10044   next = NEXT_INSN (prev);
10045   if (next == NULL)
10046     return false;
10047
10048   return INSN_P (next) && GET_CODE (PATTERN (next)) == SEQUENCE;
10049 }
10050
10051 int
10052 sh_insn_length_adjustment (rtx insn)
10053 {
10054   /* Instructions with unfilled delay slots take up an extra two bytes for
10055      the nop in the delay slot.  */
10056   if (((NONJUMP_INSN_P (insn)
10057         && GET_CODE (PATTERN (insn)) != USE
10058         && GET_CODE (PATTERN (insn)) != CLOBBER)
10059        || CALL_P (insn) || JUMP_P (insn))
10060       && ! sequence_insn_p (insn)
10061       && get_attr_needs_delay_slot (insn) == NEEDS_DELAY_SLOT_YES)
10062     return 2;
10063
10064   /* SH2e has a bug that prevents the use of annulled branches, so if
10065      the delay slot is not filled, we'll have to put a NOP in it.  */
10066   if (sh_cpu_attr == CPU_SH2E
10067       && JUMP_P (insn)
10068       && get_attr_type (insn) == TYPE_CBRANCH
10069       && ! sequence_insn_p (insn))
10070     return 2;
10071
10072   /* sh-dsp parallel processing insn take four bytes instead of two.  */
10073
10074   if (NONJUMP_INSN_P (insn))
10075     {
10076       int sum = 0;
10077       rtx body = PATTERN (insn);
10078       const char *templ;
10079       char c;
10080       bool maybe_label = true;
10081
10082       if (GET_CODE (body) == ASM_INPUT)
10083         templ = XSTR (body, 0);
10084       else if (asm_noperands (body) >= 0)
10085         templ
10086           = decode_asm_operands (body, NULL, NULL, NULL, NULL, NULL);
10087       else
10088         return 0;
10089       do
10090         {
10091           int ppi_adjust = 0;
10092
10093           do
10094             c = *templ++;
10095           while (c == ' ' || c == '\t');
10096           /* all sh-dsp parallel-processing insns start with p.
10097              The only non-ppi sh insn starting with p is pref.
10098              The only ppi starting with pr is prnd.  */
10099           if ((c == 'p' || c == 'P') && strncasecmp ("re", templ, 2))
10100             ppi_adjust = 2;
10101           /* The repeat pseudo-insn expands two three insns, a total of
10102              six bytes in size.  */
10103           else if ((c == 'r' || c == 'R')
10104                    && ! strncasecmp ("epeat", templ, 5))
10105             ppi_adjust = 4;
10106           while (c && c != '\n'
10107                  && ! IS_ASM_LOGICAL_LINE_SEPARATOR (c, templ))
10108             {
10109               /* If this is a label, it is obviously not a ppi insn.  */
10110               if (c == ':' && maybe_label)
10111                 {
10112                   ppi_adjust = 0;
10113                   break;
10114                 }
10115               else if (c == '\'' || c == '"')
10116                 maybe_label = false;
10117               c = *templ++;
10118             }
10119           sum += ppi_adjust;
10120           maybe_label = c != ':';
10121         }
10122       while (c);
10123       return sum;
10124     }
10125   return 0;
10126 }
10127 \f
10128 /* Return TRUE for a valid displacement for the REG+disp addressing
10129    with MODE.  */
10130 bool
10131 sh_legitimate_index_p (enum machine_mode mode, rtx op, bool consider_sh2a,
10132                        bool allow_zero)
10133 {
10134   if (! CONST_INT_P (op))
10135     return false;
10136
10137   if (TARGET_SHMEDIA)
10138     {
10139       int size;
10140
10141       /* Check if this is the address of an unaligned load / store.  */
10142       if (mode == VOIDmode)
10143         return satisfies_constraint_I06 (op);
10144
10145       size = GET_MODE_SIZE (mode);
10146       return (!(INTVAL (op) & (size - 1))
10147               && INTVAL (op) >= -512 * size
10148               && INTVAL (op) < 512 * size);
10149     }
10150   else
10151     {
10152       const HOST_WIDE_INT offset = INTVAL (op);
10153       const int max_disp = max_mov_insn_displacement (mode, consider_sh2a);
10154       const int align_mask = mov_insn_alignment_mask (mode, consider_sh2a);
10155
10156       /* If the mode does not support any displacement always return false.
10157          Even though an index of '0' is actually always valid, it will cause
10158          troubles when e.g. a DFmode move is split into two SFmode moves,
10159          where one SFmode move will have index '0' and the other move will
10160          have index '4'.  */
10161        if (!allow_zero && max_disp < 1)
10162         return false;
10163
10164       return offset >= 0 && offset <= max_disp && (offset & align_mask) == 0;
10165     }
10166 }
10167
10168 /* Recognize an RTL expression that is a valid memory address for
10169    an instruction.
10170    The MODE argument is the machine mode for the MEM expression
10171    that wants to use this address.
10172    Allow  REG
10173           REG+disp
10174           REG+r0
10175           REG++
10176           --REG
10177           GBR
10178           GBR+disp  */
10179 static bool
10180 sh_legitimate_address_p (enum machine_mode mode, rtx x, bool strict)
10181 {
10182   if (REG_P (x) && REGNO (x) == GBR_REG)
10183     return true;
10184
10185   if (MAYBE_BASE_REGISTER_RTX_P (x, strict))
10186     return true;
10187   else if ((GET_CODE (x) == POST_INC || GET_CODE (x) == PRE_DEC)
10188            && ! TARGET_SHMEDIA
10189            && MAYBE_BASE_REGISTER_RTX_P (XEXP (x, 0), strict))
10190     return true;
10191   else if (GET_CODE (x) == PLUS
10192            && (mode != PSImode || reload_completed))
10193     {
10194       rtx xop0 = XEXP (x, 0);
10195       rtx xop1 = XEXP (x, 1);
10196
10197       if (REG_P (xop0) && REGNO (xop0) == GBR_REG)
10198         return gbr_displacement (xop1, mode);
10199
10200       if (GET_MODE_SIZE (mode) <= 8
10201           && MAYBE_BASE_REGISTER_RTX_P (xop0, strict)
10202           && sh_legitimate_index_p (mode, xop1, TARGET_SH2A, false))
10203         return true;
10204
10205       if ((ALLOW_INDEXED_ADDRESS || GET_MODE (x) == DImode
10206            || ((xop0 == stack_pointer_rtx
10207                 || xop0 == hard_frame_pointer_rtx)
10208                && REG_P (xop1) && REGNO (xop1) == R0_REG)
10209            || ((xop1 == stack_pointer_rtx
10210                 || xop1 == hard_frame_pointer_rtx)
10211                && REG_P (xop0) && REGNO (xop0) == R0_REG))
10212           && ((!TARGET_SHMEDIA && GET_MODE_SIZE (mode) <= 4)
10213               || (TARGET_SHMEDIA && GET_MODE_SIZE (mode) <= 8)
10214               || ((TARGET_SH4 || TARGET_SH2A_DOUBLE)
10215                   && TARGET_FMOVD && mode == DFmode)))
10216         {
10217           if (MAYBE_BASE_REGISTER_RTX_P (xop1, strict)
10218               && MAYBE_INDEX_REGISTER_RTX_P (xop0, strict))
10219             return true;
10220           if (MAYBE_INDEX_REGISTER_RTX_P (xop1, strict)
10221               && MAYBE_BASE_REGISTER_RTX_P (xop0, strict))
10222             return true;
10223         }
10224     }
10225
10226   return false;
10227 }
10228 \f
10229 /* Return TRUE if X references a SYMBOL_REF or LABEL_REF whose symbol
10230    isn't protected by a PIC unspec.  */
10231 bool
10232 nonpic_symbol_mentioned_p (rtx x)
10233 {
10234   const char *fmt;
10235   int i;
10236
10237   if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF
10238       || GET_CODE (x) == PC)
10239     return true;
10240
10241   /* We don't want to look into the possible MEM location of a
10242      CONST_DOUBLE, since we're not going to use it, in general.  */
10243   if (GET_CODE (x) == CONST_DOUBLE)
10244     return false;
10245
10246   if (GET_CODE (x) == UNSPEC
10247       && (XINT (x, 1) == UNSPEC_PIC
10248           || XINT (x, 1) == UNSPEC_GOT
10249           || XINT (x, 1) == UNSPEC_GOTOFF
10250           || XINT (x, 1) == UNSPEC_GOTPLT
10251           || XINT (x, 1) == UNSPEC_GOTTPOFF
10252           || XINT (x, 1) == UNSPEC_DTPOFF
10253           || XINT (x, 1) == UNSPEC_TPOFF
10254           || XINT (x, 1) == UNSPEC_PLT
10255           || XINT (x, 1) == UNSPEC_SYMOFF
10256           || XINT (x, 1) == UNSPEC_PCREL_SYMOFF))
10257     return false;
10258
10259   fmt = GET_RTX_FORMAT (GET_CODE (x));
10260   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
10261     {
10262       if (fmt[i] == 'E')
10263         {
10264           int j;
10265           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10266             if (nonpic_symbol_mentioned_p (XVECEXP (x, i, j)))
10267               return true;
10268         }
10269       else if (fmt[i] == 'e' && nonpic_symbol_mentioned_p (XEXP (x, i)))
10270         return true;
10271     }
10272
10273   return false;
10274 }
10275
10276 /* Convert a non-PIC address in `orig' to a PIC address using @GOT or
10277    @GOTOFF in `reg'.  */
10278 rtx
10279 legitimize_pic_address (rtx orig, enum machine_mode mode ATTRIBUTE_UNUSED,
10280                         rtx reg)
10281 {
10282   if (tls_symbolic_operand (orig, Pmode) != TLS_MODEL_NONE)
10283     return orig;
10284
10285   if (GET_CODE (orig) == LABEL_REF
10286       || (GET_CODE (orig) == SYMBOL_REF && SYMBOL_REF_LOCAL_P (orig)))
10287     {
10288       if (reg == NULL_RTX)
10289         reg = gen_reg_rtx (Pmode);
10290
10291       emit_insn (gen_symGOTOFF2reg (reg, orig));
10292       return reg;
10293     }
10294   else if (GET_CODE (orig) == SYMBOL_REF)
10295     {
10296       if (reg == NULL_RTX)
10297         reg = gen_reg_rtx (Pmode);
10298
10299       emit_insn (gen_symGOT2reg (reg, orig));
10300       return reg;
10301     }
10302   return orig;
10303 }
10304
10305 /* Given a (logical) mode size and an offset in bytes, try to find a the
10306    appropriate displacement value for a mov insn.  On SH the displacements
10307    are limited to max. 60 bytes for SImode, max. 30 bytes in HImode and max.
10308    15 bytes in QImode.  To compensate this we create a new base address by
10309    adding an adjustment value to it.
10310
10311    If the originally requested offset is greater than 127 we prefer using
10312    values 124..127 over 128..131 to increase opportunities to use the
10313    add #imm, Rn insn.
10314
10315    In some cases it is possible that a requested offset might seem unaligned
10316    or inappropriate for the mode size, like offset = 2 and mode size = 4.
10317    This is compensated by adjusting the base address so that the effective
10318    address of the displacement move insn will be aligned. 
10319
10320    This is not the best possible way of rebasing the base address, as it
10321    does not look at other present displacement addressings around it.
10322    In some cases this can create more base address adjustments than would
10323    actually be necessary.  */
10324 struct disp_adjust
10325 {
10326   rtx offset_adjust;
10327   rtx mov_disp;
10328 };
10329
10330 static struct disp_adjust
10331 sh_find_mov_disp_adjust (enum machine_mode mode, HOST_WIDE_INT offset)
10332 {
10333   struct disp_adjust res = { NULL_RTX, NULL_RTX };
10334
10335   /* Do not try to use SH2A's large displacements here, because this would
10336      effectively disable the small displacement insns.  */
10337   const int mode_sz = GET_MODE_SIZE (mode);
10338   const int mov_insn_sz = mov_insn_size (mode, false);
10339   const int max_disp = max_mov_insn_displacement (mode, false);
10340   const int max_disp_next = max_disp + mov_insn_sz;
10341   HOST_WIDE_INT align_modifier = offset > 127 ? mov_insn_sz : 0;
10342   HOST_WIDE_INT offset_adjust;
10343
10344   /* In some cases this actually does happen and we must check for it.  */
10345   if (mode_sz < 1 || mode_sz > 8 || max_disp < 1)
10346     return res;
10347
10348   /* Keeps the previous behavior for QImode displacement addressing.
10349      This just decides how the offset is re-based.  Removing this special
10350      case will result in slightly bigger code on average, but it's not that
10351      bad actually.  */
10352   if (mov_insn_sz == 1)
10353     align_modifier = 0;
10354
10355   offset_adjust = ((offset + align_modifier) & ~max_disp) - align_modifier;
10356
10357   if (mode_sz + offset - offset_adjust <= max_disp_next)
10358     {
10359       res.offset_adjust = GEN_INT (offset_adjust);
10360       res.mov_disp = GEN_INT (offset - offset_adjust);
10361     }
10362
10363   return res;
10364 }
10365
10366 /* Try to modify an illegitimate address and make it legitimate.
10367    If we find one, return the new, valid address.
10368    Otherwise, return the original address.  */
10369 static rtx
10370 sh_legitimize_address (rtx x, rtx oldx, enum machine_mode mode)
10371 {
10372   if (flag_pic)
10373     x = legitimize_pic_address (oldx, mode, NULL_RTX);
10374
10375   if (TARGET_SHMEDIA)
10376     return x;
10377
10378   if (((TARGET_SH4 || TARGET_SH2A_DOUBLE) && mode == DFmode)
10379       || (TARGET_SH2E && mode == SFmode))
10380     return x;
10381
10382   if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1))
10383       && BASE_REGISTER_RTX_P (XEXP (x, 0)))
10384     {
10385       struct disp_adjust adj = sh_find_mov_disp_adjust (mode,
10386                                                         INTVAL (XEXP (x, 1)));
10387
10388       if (adj.offset_adjust != NULL_RTX && adj.mov_disp != NULL_RTX)
10389         {
10390           rtx sum = expand_binop (Pmode, add_optab, XEXP (x, 0),
10391                                   adj.offset_adjust, NULL_RTX, 0,
10392                                   OPTAB_LIB_WIDEN);
10393           return gen_rtx_PLUS (Pmode, sum, adj.mov_disp);
10394         }
10395     }
10396
10397   return x;
10398 }
10399
10400 /* Attempt to replace *p, which is an address that needs reloading, with
10401    a valid memory address for an operand of mode MODE.
10402    Like for sh_legitimize_address, for the SH we try to get a normal form
10403    of the address.  That will allow inheritance of the address reloads.  */
10404 bool
10405 sh_legitimize_reload_address (rtx *p, enum machine_mode mode, int opnum,
10406                               int itype)
10407 {
10408   enum reload_type type = (enum reload_type) itype;
10409   const int mode_sz = GET_MODE_SIZE (mode);
10410
10411   if (TARGET_SHMEDIA)
10412     return false;
10413
10414   if (GET_CODE (*p) == PLUS && CONST_INT_P (XEXP (*p, 1))
10415       && MAYBE_BASE_REGISTER_RTX_P (XEXP (*p, 0), true)
10416       && ! (mode == PSImode && type == RELOAD_FOR_INPUT_ADDRESS)
10417       && (ALLOW_INDEXED_ADDRESS
10418           || XEXP (*p, 0) == stack_pointer_rtx
10419           || XEXP (*p, 0) == hard_frame_pointer_rtx))
10420     {
10421       const HOST_WIDE_INT offset = INTVAL (XEXP (*p, 1));
10422       struct disp_adjust adj = sh_find_mov_disp_adjust (mode, offset);
10423
10424       if (TARGET_SH2A && mode == DFmode && (offset & 0x7))
10425         {
10426           push_reload (*p, NULL_RTX, p, NULL,
10427                        BASE_REG_CLASS, Pmode, VOIDmode, 0, 0, opnum, type);
10428           return true;
10429         }
10430
10431       if (TARGET_SH2E && mode == SFmode)
10432         {
10433           *p = copy_rtx (*p);
10434           push_reload (*p, NULL_RTX, p, NULL,
10435                        BASE_REG_CLASS, Pmode, VOIDmode, 0, 0, opnum, type);
10436           return true;
10437         }
10438
10439       /* FIXME: Do not allow to legitimize QImode and HImode displacement
10440          moves because then reload has a problem figuring the constraint
10441          that the move insn target/source reg must be R0.
10442          Or maybe some handling is wrong in sh_secondary_reload for this
10443          to work properly? */
10444       if ((mode_sz == 4 || mode_sz == 8)
10445           && ! (TARGET_SH4 && mode == DFmode)
10446           && adj.offset_adjust != NULL_RTX && adj.mov_disp != NULL_RTX)
10447         {
10448           rtx sum = gen_rtx_PLUS (Pmode, XEXP (*p, 0), adj.offset_adjust);
10449           *p = gen_rtx_PLUS (Pmode, sum, adj.mov_disp);
10450           push_reload (sum, NULL_RTX, &XEXP (*p, 0), NULL,
10451                        BASE_REG_CLASS, Pmode, VOIDmode, 0, 0, opnum, type);
10452           return true;
10453         }
10454     }
10455
10456   /* We must re-recognize what we created before.  */
10457   if (GET_CODE (*p) == PLUS
10458       && (mode_sz == 4 || mode_sz == 8)
10459       && GET_CODE (XEXP (*p, 0)) == PLUS
10460       && CONST_INT_P (XEXP (XEXP (*p, 0), 1))
10461       && MAYBE_BASE_REGISTER_RTX_P (XEXP (XEXP (*p, 0), 0), true)
10462       && CONST_INT_P (XEXP (*p, 1))
10463       && ! (TARGET_SH2E && mode == SFmode))
10464     {
10465       /* Because this address is so complex, we know it must have
10466          been created by LEGITIMIZE_RELOAD_ADDRESS before; thus,
10467          it is already unshared, and needs no further unsharing.  */
10468       push_reload (XEXP (*p, 0), NULL_RTX, &XEXP (*p, 0), NULL,
10469                    BASE_REG_CLASS, Pmode, VOIDmode, 0, 0, opnum, type);
10470       return true;
10471     }
10472
10473   return false;
10474 }
10475
10476 /* In the name of slightly smaller debug output, and to cater to
10477    general assembler lossage, recognize various UNSPEC sequences
10478    and turn them back into a direct symbol reference.  */
10479 static rtx
10480 sh_delegitimize_address (rtx orig_x)
10481 {
10482   rtx x, y;
10483
10484   orig_x = delegitimize_mem_from_attrs (orig_x);
10485
10486   x = orig_x;
10487   if (MEM_P (x))
10488     x = XEXP (x, 0);
10489   if (GET_CODE (x) == CONST)
10490     {
10491       y = XEXP (x, 0);
10492       if (GET_CODE (y) == UNSPEC)
10493         {
10494           if (XINT (y, 1) == UNSPEC_GOT
10495               || XINT (y, 1) == UNSPEC_GOTOFF
10496               || XINT (y, 1) == UNSPEC_SYMOFF)
10497             return XVECEXP (y, 0, 0);
10498           else if (XINT (y, 1) == UNSPEC_PCREL_SYMOFF)
10499             {
10500               if (GET_CODE (XVECEXP (y, 0, 0)) == CONST)
10501                 {
10502                   rtx symplt = XEXP (XVECEXP (y, 0, 0), 0);
10503
10504                   if (GET_CODE (symplt) == UNSPEC
10505                       && XINT (symplt, 1) == UNSPEC_PLT)
10506                     return XVECEXP (symplt, 0, 0);
10507                 }
10508             }
10509           else if (TARGET_SHMEDIA
10510                    && (XINT (y, 1) == UNSPEC_EXTRACT_S16
10511                        || XINT (y, 1) == UNSPEC_EXTRACT_U16))
10512             {
10513               rtx offset = XVECEXP (y, 0, 1);
10514
10515               x = gen_rtx_PLUS (Pmode, XVECEXP (y, 0, 0), offset);
10516               if (MEM_P (orig_x))
10517                 x = replace_equiv_address_nv (orig_x, x);
10518               return x;
10519             }
10520         }
10521     }
10522
10523   return orig_x;
10524 }
10525
10526 /* Mark the use of a constant in the literal table. If the constant
10527    has multiple labels, make it unique.  */
10528 static rtx
10529 mark_constant_pool_use (rtx x)
10530 {
10531   rtx insn, lab, pattern;
10532
10533   if (x == NULL_RTX)
10534     return x;
10535
10536   switch (GET_CODE (x))
10537     {
10538     case LABEL_REF:
10539       x = XEXP (x, 0);
10540     case CODE_LABEL:
10541       break;
10542     default:
10543       return x;
10544     }
10545
10546   /* Get the first label in the list of labels for the same constant
10547      and delete another labels in the list.  */
10548   lab = x;
10549   for (insn = PREV_INSN (x); insn; insn = PREV_INSN (insn))
10550     {
10551       if (!LABEL_P (insn)
10552           || LABEL_REFS (insn) != NEXT_INSN (insn))
10553         break;
10554       lab = insn;
10555     }
10556
10557   for (insn = LABEL_REFS (lab); insn; insn = LABEL_REFS (insn))
10558     INSN_DELETED_P (insn) = 1;
10559
10560   /* Mark constants in a window.  */
10561   for (insn = NEXT_INSN (x); insn; insn = NEXT_INSN (insn))
10562     {
10563       if (!NONJUMP_INSN_P (insn))
10564         continue;
10565
10566       pattern = PATTERN (insn);
10567       if (GET_CODE (pattern) != UNSPEC_VOLATILE)
10568         continue;
10569
10570       switch (XINT (pattern, 1))
10571         {
10572         case UNSPECV_CONST2:
10573         case UNSPECV_CONST4:
10574         case UNSPECV_CONST8:
10575           XVECEXP (pattern, 0, 1) = const1_rtx;
10576           break;
10577         case UNSPECV_WINDOW_END:
10578           if (XVECEXP (pattern, 0, 0) == x)
10579             return lab;
10580           break;
10581         case UNSPECV_CONST_END:
10582           return lab;
10583         default:
10584           break;
10585         }
10586     }
10587
10588   return lab;
10589 }
10590 \f
10591 /* Return true if it's possible to redirect BRANCH1 to the destination
10592    of an unconditional jump BRANCH2.  We only want to do this if the
10593    resulting branch will have a short displacement.  */
10594 bool
10595 sh_can_redirect_branch (rtx branch1, rtx branch2)
10596 {
10597   if (flag_expensive_optimizations && simplejump_p (branch2))
10598     {
10599       rtx dest = XEXP (SET_SRC (single_set (branch2)), 0);
10600       rtx insn;
10601       int distance;
10602
10603       for (distance = 0, insn = NEXT_INSN (branch1);
10604            insn && distance < 256;
10605            insn = PREV_INSN (insn))
10606         {
10607           if (insn == dest)
10608             return true;
10609           else
10610             distance += get_attr_length (insn);
10611         }
10612       for (distance = 0, insn = NEXT_INSN (branch1);
10613            insn && distance < 256;
10614            insn = NEXT_INSN (insn))
10615         {
10616           if (insn == dest)
10617             return true;
10618           else
10619             distance += get_attr_length (insn);
10620         }
10621     }
10622   return false;
10623 }
10624
10625 /* Return nonzero if register old_reg can be renamed to register new_reg.  */
10626 bool
10627 sh_hard_regno_rename_ok (unsigned int old_reg ATTRIBUTE_UNUSED,
10628                          unsigned int new_reg)
10629 {
10630   /* Interrupt functions can only use registers that have already been
10631      saved by the prologue, even if they would normally be
10632      call-clobbered.  */
10633   if (sh_cfun_interrupt_handler_p () && !df_regs_ever_live_p (new_reg))
10634     return false;
10635
10636   return true;
10637 }
10638
10639 /* Function to update the integer COST
10640    based on the relationship between INSN that is dependent on
10641    DEP_INSN through the dependence LINK.  The default is to make no
10642    adjustment to COST.  This can be used for example to specify to
10643    the scheduler that an output- or anti-dependence does not incur
10644    the same cost as a data-dependence.  The return value should be
10645    the new value for COST.  */
10646 static int
10647 sh_adjust_cost (rtx insn, rtx link ATTRIBUTE_UNUSED, rtx dep_insn, int cost)
10648 {
10649   rtx reg, use_pat;
10650
10651   if (TARGET_SHMEDIA)
10652     {
10653       /* On SHmedia, if the dependence is an anti-dependence or
10654          output-dependence, there is no cost.  */
10655       if (REG_NOTE_KIND (link) != 0)
10656         {
10657           /* However, dependencies between target register loads and
10658              uses of the register in a subsequent block that are separated
10659              by a conditional branch are not modelled - we have to do with
10660              the anti-dependency between the target register load and the
10661              conditional branch that ends the current block.  */
10662           if (REG_NOTE_KIND (link) == REG_DEP_ANTI
10663               && GET_CODE (PATTERN (dep_insn)) == SET
10664               && (get_attr_type (dep_insn) == TYPE_PT_MEDIA
10665                   || get_attr_type (dep_insn) == TYPE_PTABS_MEDIA)
10666               && get_attr_type (insn) == TYPE_CBRANCH_MEDIA)
10667             {
10668               int orig_cost = cost;
10669               rtx note = find_reg_note (insn, REG_BR_PROB, 0);
10670               rtx target = ((!note || XINT (note, 0) * 2 < REG_BR_PROB_BASE)
10671                             ? insn : JUMP_LABEL (insn));
10672               /* On the likely path, the branch costs 1, on the unlikely path,
10673                  it costs 3.  */
10674               cost--;
10675               do
10676                 target = next_active_insn (target);
10677               while (target && ! flow_dependent_p (target, dep_insn)
10678                      && --cost > 0);
10679               /* If two branches are executed in immediate succession, with the
10680                  first branch properly predicted, this causes a stall at the
10681                  second branch, hence we won't need the target for the
10682                  second branch for two cycles after the launch of the first
10683                  branch.  */
10684               if (cost > orig_cost - 2)
10685                 cost = orig_cost - 2;
10686             }
10687           else
10688             cost = 0;
10689         }
10690
10691       else if (get_attr_is_mac_media (insn)
10692                && get_attr_is_mac_media (dep_insn))
10693         cost = 1;
10694
10695       else if (! reload_completed
10696                && GET_CODE (PATTERN (insn)) == SET
10697                && GET_CODE (SET_SRC (PATTERN (insn))) == FLOAT
10698                && GET_CODE (PATTERN (dep_insn)) == SET
10699                && fp_arith_reg_operand (SET_SRC (PATTERN (dep_insn)), VOIDmode)
10700                && cost < 4)
10701         cost = 4;
10702       /* Schedule the ptabs for a casesi_jump_media in preference to stuff
10703          that is needed at the target.  */
10704       else if (get_attr_type (insn) == TYPE_JUMP_MEDIA
10705                && ! flow_dependent_p (insn, dep_insn))
10706         cost--;
10707     }
10708   else if (REG_NOTE_KIND (link) == 0)
10709     {
10710       enum attr_type type;
10711       rtx dep_set;
10712
10713       if (recog_memoized (insn) < 0
10714           || recog_memoized (dep_insn) < 0)
10715         return cost;
10716
10717       dep_set = single_set (dep_insn);
10718
10719       /* The latency that we specify in the scheduling description refers
10720          to the actual output, not to an auto-increment register; for that,
10721          the latency is one.  */
10722       if (dep_set && MEM_P (SET_SRC (dep_set)) && cost > 1)
10723         {
10724           rtx set = single_set (insn);
10725
10726           if (set
10727               && !reg_mentioned_p (SET_DEST (dep_set), SET_SRC (set))
10728               && (!MEM_P (SET_DEST (set))
10729                   || !reg_mentioned_p (SET_DEST (dep_set),
10730                                        XEXP (SET_DEST (set), 0))))
10731             cost = 1;
10732         }
10733       /* The only input for a call that is timing-critical is the
10734          function's address.  */
10735       if (CALL_P (insn))
10736         {
10737           rtx call = get_call_rtx_from (insn);
10738           if (call
10739                   /* sibcalli_thunk uses a symbol_ref in an unspec.  */
10740               && (GET_CODE (XEXP (XEXP (call, 0), 0)) == UNSPEC
10741                   || ! reg_set_p (XEXP (XEXP (call, 0), 0), dep_insn)))
10742             cost -= TARGET_SH4_300 ? 3 : 6;
10743         }
10744       /* Likewise, the most timing critical input for an sfuncs call
10745          is the function address.  However, sfuncs typically start
10746          using their arguments pretty quickly.
10747          Assume a four cycle delay for SH4 before they are needed.
10748          Cached ST40-300 calls are quicker, so assume only a one
10749          cycle delay there.
10750          ??? Maybe we should encode the delays till input registers
10751          are needed by sfuncs into the sfunc call insn.  */
10752       /* All sfunc calls are parallels with at least four components.
10753          Exploit this to avoid unnecessary calls to sfunc_uses_reg.  */
10754       else if (GET_CODE (PATTERN (insn)) == PARALLEL
10755                && XVECLEN (PATTERN (insn), 0) >= 4
10756                && (reg = sfunc_uses_reg (insn)))
10757         {
10758           if (! reg_set_p (reg, dep_insn))
10759             cost -= TARGET_SH4_300 ? 1 : 4;
10760         }
10761       if (TARGET_HARD_SH4 && !TARGET_SH4_300)
10762         {
10763           enum attr_type dep_type = get_attr_type (dep_insn);
10764
10765           if (dep_type == TYPE_FLOAD || dep_type == TYPE_PCFLOAD)
10766             cost--;
10767           else if ((dep_type == TYPE_LOAD_SI || dep_type == TYPE_PCLOAD_SI)
10768                    && (type = get_attr_type (insn)) != TYPE_CALL
10769                    && type != TYPE_SFUNC)
10770             cost--;
10771           /* When the preceding instruction loads the shift amount of
10772              the following SHAD/SHLD, the latency of the load is increased
10773              by 1 cycle.  */
10774           if (get_attr_type (insn) == TYPE_DYN_SHIFT
10775               && get_attr_any_int_load (dep_insn) == ANY_INT_LOAD_YES
10776               && reg_overlap_mentioned_p (SET_DEST (dep_set),
10777                                           XEXP (SET_SRC (single_set (insn)),
10778                                                 1)))
10779             cost++;
10780           /* When an LS group instruction with a latency of less than
10781              3 cycles is followed by a double-precision floating-point
10782              instruction, FIPR, or FTRV, the latency of the first
10783              instruction is increased to 3 cycles.  */
10784           else if (cost < 3
10785                    && get_attr_insn_class (dep_insn) == INSN_CLASS_LS_GROUP
10786                    && get_attr_dfp_comp (insn) == DFP_COMP_YES)
10787             cost = 3;
10788           /* The lsw register of a double-precision computation is ready one
10789              cycle earlier.  */
10790           else if (reload_completed
10791                    && get_attr_dfp_comp (dep_insn) == DFP_COMP_YES
10792                    && (use_pat = single_set (insn))
10793                    && ! regno_use_in (REGNO (SET_DEST (single_set (dep_insn))),
10794                                       SET_SRC (use_pat)))
10795             cost -= 1;
10796
10797           if (get_attr_any_fp_comp (dep_insn) == ANY_FP_COMP_YES
10798               && get_attr_late_fp_use (insn) == LATE_FP_USE_YES)
10799             cost -= 1;
10800         }
10801       else if (TARGET_SH4_300)
10802         {
10803           /* Stores need their input register two cycles later.  */
10804           if (dep_set && cost >= 1
10805               && ((type = get_attr_type (insn)) == TYPE_STORE
10806                   || type == TYPE_PSTORE
10807                   || type == TYPE_FSTORE || type == TYPE_MAC_MEM))
10808             {
10809               rtx set = single_set (insn);
10810
10811               if (!reg_mentioned_p (SET_SRC (set), XEXP (SET_DEST (set), 0))
10812                   && rtx_equal_p (SET_SRC (set), SET_DEST (dep_set)))
10813                 {
10814                   cost -= 2;
10815                   /* But don't reduce the cost below 1 if the address depends
10816                      on a side effect of dep_insn.  */
10817                   if (cost < 1
10818                       && modified_in_p (XEXP (SET_DEST (set), 0), dep_insn))
10819                     cost = 1;
10820                 }
10821             }
10822         }
10823     }
10824   /* An anti-dependence penalty of two applies if the first insn is a double
10825      precision fadd / fsub / fmul.  */
10826   else if (!TARGET_SH4_300
10827            && REG_NOTE_KIND (link) == REG_DEP_ANTI
10828            && recog_memoized (dep_insn) >= 0
10829            && (get_attr_type (dep_insn) == TYPE_DFP_ARITH
10830                || get_attr_type (dep_insn) == TYPE_DFP_MUL)
10831            /* A lot of alleged anti-flow dependences are fake,
10832               so check this one is real.  */
10833            && flow_dependent_p (dep_insn, insn))
10834     cost = 2;
10835
10836   return cost;
10837 }
10838
10839 /* Check if INSN is flow-dependent on DEP_INSN.  Can also be used to check
10840    if DEP_INSN is anti-flow dependent on INSN.  */
10841 static bool
10842 flow_dependent_p (rtx insn, rtx dep_insn)
10843 {
10844   rtx tmp = PATTERN (insn);
10845
10846   note_stores (PATTERN (dep_insn), flow_dependent_p_1, &tmp);
10847   return tmp == NULL_RTX;
10848 }
10849
10850 /* A helper function for flow_dependent_p called through note_stores.  */
10851 static void
10852 flow_dependent_p_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
10853 {
10854   rtx * pinsn = (rtx *) data;
10855
10856   if (*pinsn && reg_referenced_p (x, *pinsn))
10857     *pinsn = NULL_RTX;
10858 }
10859
10860 /* For use by sh_allocate_initial_value.  Note that sh.md contains some
10861    'special function' patterns (type sfunc) that clobber pr, but that
10862    do not look like function calls to leaf_function_p.  Hence we must
10863    do this extra check.  */
10864 static int
10865 sh_pr_n_sets (void)
10866 {
10867   return DF_REG_DEF_COUNT (TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG);
10868 }
10869
10870 /* Return where to allocate pseudo for a given hard register initial
10871    value.  */
10872 static rtx
10873 sh_allocate_initial_value (rtx hard_reg)
10874 {
10875   rtx x;
10876
10877   if (REGNO (hard_reg) == (TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG))
10878     {
10879       if (crtl->is_leaf
10880           && ! sh_pr_n_sets ()
10881           && ! (TARGET_SHCOMPACT
10882                 && ((crtl->args.info.call_cookie
10883                      & ~ CALL_COOKIE_RET_TRAMP (1))
10884                     || crtl->saves_all_registers)))
10885         x = hard_reg;
10886       else
10887         x = gen_frame_mem (Pmode, return_address_pointer_rtx);
10888     }
10889   else
10890     x = NULL_RTX;
10891
10892   return x;
10893 }
10894
10895 /* This function returns "2" to indicate dual issue for the SH4
10896    processor.  To be used by the DFA pipeline description.  */
10897 static int
10898 sh_issue_rate (void)
10899 {
10900   if (TARGET_SUPERSCALAR)
10901     return 2;
10902   else
10903     return 1;
10904 }
10905
10906 /* Functions for ready queue reordering for sched1.  */
10907
10908 /* Get weight for mode for a set x.  */
10909 static short
10910 find_set_regmode_weight (rtx x, enum machine_mode mode)
10911 {
10912   if (GET_CODE (x) == CLOBBER && register_operand (SET_DEST (x), mode))
10913     return 1;
10914   if (GET_CODE (x) == SET && register_operand (SET_DEST (x), mode))
10915     {
10916       if (REG_P (SET_DEST (x)))
10917         {
10918           if (!reg_mentioned_p (SET_DEST (x), SET_SRC (x)))
10919             return 1;
10920           else
10921             return 0;
10922         }
10923       return 1;
10924     }
10925   return 0;
10926 }
10927
10928 /* Get regmode weight for insn.  */
10929 static short
10930 find_insn_regmode_weight (rtx insn, enum machine_mode mode)
10931 {
10932   short reg_weight = 0;
10933   rtx x;
10934
10935   /* Increment weight for each register born here.  */
10936   x = PATTERN (insn);
10937   reg_weight += find_set_regmode_weight (x, mode);
10938   if (GET_CODE (x) == PARALLEL)
10939     {
10940       int j;
10941       for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
10942         {
10943           x = XVECEXP (PATTERN (insn), 0, j);
10944           reg_weight += find_set_regmode_weight (x, mode);
10945         }
10946     }
10947   /* Decrement weight for each register that dies here.  */
10948   for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
10949     {
10950       if (REG_NOTE_KIND (x) == REG_DEAD || REG_NOTE_KIND (x) == REG_UNUSED)
10951         {
10952           rtx note = XEXP (x, 0);
10953           if (REG_P (note) && GET_MODE (note) == mode)
10954             reg_weight--;
10955         }
10956     }
10957   return reg_weight;
10958 }
10959
10960 /* Calculate regmode weights for all insns of a basic block.  */
10961 static void
10962 find_regmode_weight (basic_block b, enum machine_mode mode)
10963 {
10964   rtx insn, next_tail, head, tail;
10965
10966   get_ebb_head_tail (b, b, &head, &tail);
10967   next_tail = NEXT_INSN (tail);
10968
10969   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
10970     {
10971       /* Handle register life information.  */
10972       if (!INSN_P (insn))
10973         continue;
10974
10975       if (mode == SFmode)
10976         INSN_REGMODE_WEIGHT (insn, mode) =
10977           find_insn_regmode_weight (insn, mode)
10978           + 2 * find_insn_regmode_weight (insn, DFmode);
10979       else if (mode == SImode)
10980         INSN_REGMODE_WEIGHT (insn, mode) =
10981           find_insn_regmode_weight (insn, mode)
10982           + 2 * find_insn_regmode_weight (insn, DImode);
10983     }
10984 }
10985
10986 /* Comparison function for ready queue sorting.  */
10987 static int
10988 rank_for_reorder (const void *x, const void *y)
10989 {
10990   rtx tmp = *(const rtx *) y;
10991   rtx tmp2 = *(const rtx *) x;
10992
10993   /* The insn in a schedule group should be issued the first.  */
10994   if (SCHED_GROUP_P (tmp) != SCHED_GROUP_P (tmp2))
10995     return SCHED_GROUP_P (tmp2) ? 1 : -1;
10996
10997   /* If insns are equally good, sort by INSN_LUID (original insn order), This
10998      minimizes instruction movement, thus minimizing sched's effect on
10999      register pressure.  */
11000   return INSN_LUID (tmp) - INSN_LUID (tmp2);
11001 }
11002
11003 /* Resort the array A in which only element at index N may be out of order.  */
11004 static void
11005 swap_reorder (rtx *a, int n)
11006 {
11007   rtx insn = a[n - 1];
11008   int i = n - 2;
11009
11010   while (i >= 0 && rank_for_reorder (a + i, &insn) >= 0)
11011     {
11012       a[i + 1] = a[i];
11013       i -= 1;
11014     }
11015   a[i + 1] = insn;
11016 }
11017
11018 /* Sort the ready list by ascending priority.  */
11019 static void
11020 ready_reorder (rtx *ready, int nready)
11021 {
11022   if (nready == 2)
11023     swap_reorder (ready, nready);
11024   else if (nready > 2)
11025      qsort (ready, nready, sizeof (rtx), rank_for_reorder);
11026 }
11027
11028 /* Count life regions of r0 for a block.  */
11029 static int
11030 find_r0_life_regions (basic_block b)
11031 {
11032   rtx end, insn;
11033   rtx pset;
11034   rtx r0_reg;
11035   int live;
11036   int set;
11037   int death = 0;
11038
11039   if (REGNO_REG_SET_P (df_get_live_in (b), R0_REG))
11040     {
11041       set = 1;
11042       live = 1;
11043     }
11044   else
11045     {
11046       set = 0;
11047       live = 0;
11048     }
11049
11050   insn = BB_HEAD (b);
11051   end = BB_END (b);
11052   r0_reg = gen_rtx_REG (SImode, R0_REG);
11053   while (1)
11054     {
11055       if (INSN_P (insn))
11056         {
11057           if (find_regno_note (insn, REG_DEAD, R0_REG))
11058             {
11059               death++;
11060               live = 0;
11061             }
11062           if (!live
11063               && (pset = single_set (insn))
11064               && reg_overlap_mentioned_p (r0_reg, SET_DEST (pset))
11065               && !find_regno_note (insn, REG_UNUSED, R0_REG))
11066             {
11067               set++;
11068               live = 1;
11069             }
11070         }
11071       if (insn == end)
11072         break;
11073       insn = NEXT_INSN (insn);
11074     }
11075   return set - death;
11076 }
11077
11078 /* Calculate regmode weights for all insns of all basic block.  */
11079 static void
11080 sh_md_init_global (FILE *dump ATTRIBUTE_UNUSED,
11081                    int verbose ATTRIBUTE_UNUSED,
11082                    int old_max_uid)
11083 {
11084   basic_block b;
11085
11086   regmode_weight[0] = (short *) xcalloc (old_max_uid, sizeof (short));
11087   regmode_weight[1] = (short *) xcalloc (old_max_uid, sizeof (short));
11088   r0_life_regions = 0;
11089
11090   FOR_EACH_BB_REVERSE (b)
11091   {
11092     find_regmode_weight (b, SImode);
11093     find_regmode_weight (b, SFmode);
11094     if (!reload_completed)
11095       r0_life_regions += find_r0_life_regions (b);
11096   }
11097
11098   CURR_REGMODE_PRESSURE (SImode) = 0;
11099   CURR_REGMODE_PRESSURE (SFmode) = 0;
11100 }
11101
11102 /* Cleanup.  */
11103 static void
11104 sh_md_finish_global (FILE *dump ATTRIBUTE_UNUSED,
11105                      int verbose ATTRIBUTE_UNUSED)
11106 {
11107   if (regmode_weight[0])
11108     {
11109       free (regmode_weight[0]);
11110       regmode_weight[0] = NULL;
11111     }
11112   if (regmode_weight[1])
11113     {
11114       free (regmode_weight[1]);
11115       regmode_weight[1] = NULL;
11116     }
11117 }
11118
11119 /* The scalar modes supported differs from the default version in TImode
11120    for 32-bit SHMEDIA.  */
11121 static bool
11122 sh_scalar_mode_supported_p (enum machine_mode mode)
11123 {
11124   if (TARGET_SHMEDIA32 && mode == TImode)
11125     return false;
11126
11127   return default_scalar_mode_supported_p (mode);
11128 }
11129
11130 /* Cache the can_issue_more so that we can return it from reorder2. Also,
11131    keep count of register pressures on SImode and SFmode. */
11132 static int
11133 sh_variable_issue (FILE *dump ATTRIBUTE_UNUSED,
11134                    int sched_verbose ATTRIBUTE_UNUSED,
11135                    rtx insn,
11136                    int can_issue_more)
11137 {
11138   if (GET_CODE (PATTERN (insn)) != USE
11139       && GET_CODE (PATTERN (insn)) != CLOBBER)
11140     cached_can_issue_more = can_issue_more - 1;
11141   else
11142     cached_can_issue_more = can_issue_more;
11143
11144   if (reload_completed)
11145     return cached_can_issue_more;
11146
11147   CURR_REGMODE_PRESSURE (SImode) += INSN_REGMODE_WEIGHT (insn, SImode);
11148   CURR_REGMODE_PRESSURE (SFmode) += INSN_REGMODE_WEIGHT (insn, SFmode);
11149
11150   return cached_can_issue_more;
11151 }
11152
11153 static void
11154 sh_md_init (FILE *dump ATTRIBUTE_UNUSED,
11155             int verbose ATTRIBUTE_UNUSED,
11156             int veclen ATTRIBUTE_UNUSED)
11157 {
11158   CURR_REGMODE_PRESSURE (SImode) = 0;
11159   CURR_REGMODE_PRESSURE (SFmode) = 0;
11160 }
11161
11162 /* Some magic numbers.  */
11163 /* Pressure on register r0 can lead to spill failures. so avoid sched1 for
11164    functions that already have high pressure on r0. */
11165 #define R0_MAX_LIFE_REGIONS 2
11166 /* Register Pressure thresholds for SImode and SFmode registers.  */
11167 #define SIMODE_MAX_WEIGHT 5
11168 #define SFMODE_MAX_WEIGHT 10
11169
11170 /* Return true if the pressure is high for MODE.  */
11171 static bool
11172 high_pressure (enum machine_mode mode)
11173 {
11174   /* Pressure on register r0 can lead to spill failures. so avoid sched1 for
11175      functions that already have high pressure on r0. */
11176    if (r0_life_regions >= R0_MAX_LIFE_REGIONS)
11177      return true;
11178
11179   if (mode == SFmode)
11180     return (CURR_REGMODE_PRESSURE (SFmode) > SFMODE_MAX_WEIGHT);
11181   else
11182     return (CURR_REGMODE_PRESSURE (SImode) > SIMODE_MAX_WEIGHT);
11183 }
11184
11185 /* Reorder ready queue if register pressure is high.  */
11186 static int
11187 sh_reorder (FILE *dump ATTRIBUTE_UNUSED,
11188             int sched_verbose ATTRIBUTE_UNUSED,
11189             rtx *ready,
11190             int *n_readyp,
11191             int clock_var ATTRIBUTE_UNUSED)
11192 {
11193   if (reload_completed)
11194     return sh_issue_rate ();
11195
11196   if (high_pressure (SFmode) || high_pressure (SImode))
11197     {
11198       ready_reorder (ready, *n_readyp);
11199     }
11200
11201   return sh_issue_rate ();
11202 }
11203
11204 /* Skip cycles if the current register pressure is high.  */
11205 static int
11206 sh_reorder2 (FILE *dump ATTRIBUTE_UNUSED,
11207              int sched_verbose ATTRIBUTE_UNUSED,
11208              rtx *ready ATTRIBUTE_UNUSED,
11209              int *n_readyp ATTRIBUTE_UNUSED,
11210              int clock_var ATTRIBUTE_UNUSED)
11211 {
11212   if (reload_completed)
11213     return cached_can_issue_more;
11214
11215   if (high_pressure(SFmode) || high_pressure (SImode))
11216     skip_cycles = 1;
11217
11218   return cached_can_issue_more;
11219 }
11220
11221 /* Skip cycles without sorting the ready queue. This will move insn from
11222    Q->R. If this is the last cycle we are skipping; allow sorting of ready
11223    queue by sh_reorder.  */
11224
11225 /* Generally, skipping these many cycles are sufficient for all insns to move
11226    from Q -> R.  */
11227 #define MAX_SKIPS 8
11228
11229 static int
11230 sh_dfa_new_cycle (FILE *sched_dump ATTRIBUTE_UNUSED,
11231                   int sched_verbose ATTRIBUTE_UNUSED,
11232                   rtx insn ATTRIBUTE_UNUSED,
11233                   int last_clock_var,
11234                   int clock_var,
11235                   int *sort_p)
11236 {
11237   if (reload_completed)
11238     return 0;
11239
11240   if (skip_cycles)
11241     {
11242       if ((clock_var - last_clock_var) < MAX_SKIPS)
11243         {
11244           *sort_p = 0;
11245           return 1;
11246         }
11247       /* If this is the last cycle we are skipping, allow reordering of R.  */
11248       if ((clock_var - last_clock_var) == MAX_SKIPS)
11249         {
11250           *sort_p = 1;
11251           return 1;
11252         }
11253     }
11254
11255   skip_cycles = 0;
11256
11257   return 0;
11258 }
11259
11260 /* SHmedia requires registers for branches, so we can't generate new
11261    branches past reload.  */
11262 static bool
11263 sh_cannot_modify_jumps_p (void)
11264 {
11265   return (TARGET_SHMEDIA && (reload_in_progress || reload_completed));
11266 }
11267
11268 static reg_class_t
11269 sh_target_reg_class (void)
11270 {
11271   return TARGET_SHMEDIA ? TARGET_REGS : NO_REGS;
11272 }
11273
11274 static bool
11275 sh_optimize_target_register_callee_saved (bool after_prologue_epilogue_gen)
11276 {
11277   if (! shmedia_space_reserved_for_target_registers)
11278     return 0;
11279   if (after_prologue_epilogue_gen && ! TARGET_SAVE_ALL_TARGET_REGS)
11280     return 0;
11281
11282   HARD_REG_SET dummy;
11283   if (calc_live_regs (&dummy) >= 6 * 8)
11284     return 1;
11285   return 0;
11286 }
11287
11288 static bool
11289 sh_ms_bitfield_layout_p (const_tree record_type ATTRIBUTE_UNUSED)
11290 {
11291   return (TARGET_SH5 || TARGET_HITACHI || sh_attr_renesas_p (record_type));
11292 }
11293 \f
11294 /*
11295    On the SH1..SH4, the trampoline looks like
11296    2 0002 D202                  mov.l   l2,r2
11297    1 0000 D301                  mov.l   l1,r3
11298    3 0004 422B                  jmp     @r2
11299    4 0006 0009                  nop
11300    5 0008 00000000      l1:     .long   area
11301    6 000c 00000000      l2:     .long   function
11302
11303    SH5 (compact) uses r1 instead of r3 for the static chain.  */
11304
11305
11306 /* Emit RTL insns to initialize the variable parts of a trampoline.
11307    FNADDR is an RTX for the address of the function's pure code.
11308    CXT is an RTX for the static chain value for the function.  */
11309 static void
11310 sh_trampoline_init (rtx tramp_mem, tree fndecl, rtx cxt)
11311 {
11312   rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
11313   rtx tramp = force_reg (Pmode, XEXP (tramp_mem, 0));
11314
11315   if (TARGET_SHMEDIA64)
11316     {
11317       rtx tramp_templ;
11318       int fixed_len;
11319
11320       rtx movi1 = GEN_INT (0xcc000010);
11321       rtx shori1 = GEN_INT (0xc8000010);
11322       rtx src, dst;
11323
11324       /* The following trampoline works within a +- 128 KB range for cxt:
11325          ptb/u cxt,tr1; movi fnaddr >> 48,r0; shori fnaddr >> 32,r0;
11326          shori fnaddr >> 16,r0; shori fnaddr,r0; ptabs/l r0,tr0
11327          gettr tr1,r1; blink tr0,r63  */
11328       /* Address rounding makes it hard to compute the exact bounds of the
11329          offset for this trampoline, but we have a rather generous offset
11330          range, so frame_offset should do fine as an upper bound.  */
11331       if (cxt == virtual_stack_vars_rtx && frame_offset < 0x20000)
11332         {
11333           /* ??? could optimize this trampoline initialization
11334              by writing DImode words with two insns each.  */
11335           rtx mask = force_reg (DImode, GEN_INT (0x3fffc00));
11336           rtx insn = gen_rtx_MINUS (DImode, cxt, tramp);
11337           insn = gen_rtx_ASHIFT (DImode, insn, GEN_INT (10-2));
11338           insn = gen_rtx_AND (DImode, insn, mask);
11339           /* Or in ptb/u .,tr1 pattern */
11340           insn = gen_rtx_IOR (DImode, insn, gen_int_mode (0xec000010, SImode));
11341           insn = force_operand (insn, NULL_RTX);
11342           insn = gen_lowpart (SImode, insn);
11343           emit_move_insn (change_address (tramp_mem, SImode, NULL_RTX), insn);
11344           insn = gen_rtx_LSHIFTRT (DImode, fnaddr, GEN_INT (38));
11345           insn = gen_rtx_AND (DImode, insn, mask);
11346           insn = force_operand (gen_rtx_IOR (DImode, movi1, insn), NULL_RTX);
11347           insn = gen_lowpart (SImode, insn);
11348           emit_move_insn (adjust_address (tramp_mem, SImode, 4), insn);
11349           insn = gen_rtx_LSHIFTRT (DImode, fnaddr, GEN_INT (22));
11350           insn = gen_rtx_AND (DImode, insn, mask);
11351           insn = force_operand (gen_rtx_IOR (DImode, shori1, insn), NULL_RTX);
11352           insn = gen_lowpart (SImode, insn);
11353           emit_move_insn (adjust_address (tramp_mem, SImode, 8), insn);
11354           insn = gen_rtx_LSHIFTRT (DImode, fnaddr, GEN_INT (6));
11355           insn = gen_rtx_AND (DImode, insn, mask);
11356           insn = force_operand (gen_rtx_IOR (DImode, shori1, insn), NULL_RTX);
11357           insn = gen_lowpart (SImode, insn);
11358           emit_move_insn (adjust_address (tramp_mem, SImode, 12), insn);
11359           insn = gen_rtx_ASHIFT (DImode, fnaddr, GEN_INT (10));
11360           insn = gen_rtx_AND (DImode, insn, mask);
11361           insn = force_operand (gen_rtx_IOR (DImode, shori1, insn), NULL_RTX);
11362           insn = gen_lowpart (SImode, insn);
11363           emit_move_insn (adjust_address (tramp_mem, SImode, 16), insn);
11364           emit_move_insn (adjust_address (tramp_mem, SImode, 20),
11365                           GEN_INT (0x6bf10600));
11366           emit_move_insn (adjust_address (tramp_mem, SImode, 24),
11367                           GEN_INT (0x4415fc10));
11368           emit_move_insn (adjust_address (tramp_mem, SImode, 28),
11369                           GEN_INT (0x4401fff0));
11370           emit_insn (gen_ic_invalidate_line (tramp));
11371           return;
11372         }
11373       tramp_templ = gen_rtx_SYMBOL_REF (Pmode,"__GCC_nested_trampoline");
11374       fixed_len = TRAMPOLINE_SIZE - 2 * GET_MODE_SIZE (Pmode);
11375
11376       tramp_templ = gen_datalabel_ref (tramp_templ);
11377       dst = tramp_mem;
11378       src = gen_const_mem (BLKmode, tramp_templ);
11379       set_mem_align (dst, 256);
11380       set_mem_align (src, 64);
11381       emit_block_move (dst, src, GEN_INT (fixed_len), BLOCK_OP_NORMAL);
11382
11383       emit_move_insn (adjust_address (tramp_mem, Pmode, fixed_len), fnaddr);
11384       emit_move_insn (adjust_address (tramp_mem, Pmode,
11385                                       fixed_len + GET_MODE_SIZE (Pmode)),
11386                       cxt);
11387       emit_insn (gen_ic_invalidate_line (tramp));
11388       return;
11389     }
11390   else if (TARGET_SHMEDIA)
11391     {
11392       /* movi fnaddr >> 16,r1; shori fnaddr,r1; ptabs/l r1,tr0
11393          movi cxt >> 16,r1; shori cxt,r1; blink tr0,r63  */
11394       rtx quad0 = gen_reg_rtx (DImode), cxtload = gen_reg_rtx (DImode);
11395       rtx quad1 = gen_reg_rtx (DImode), quad2 = gen_reg_rtx (DImode);
11396       /* movi 0,r1: 0xcc000010 shori 0,r1: c8000010  concatenated,
11397          rotated 10 right, and higher 16 bit of every 32 selected.  */
11398       rtx movishori
11399         = force_reg (V2HImode, (simplify_gen_subreg
11400                                 (V2HImode, GEN_INT (0x4330432), SImode, 0)));
11401       rtx ptabs = force_reg (DImode, GEN_INT (0x6bf10600));
11402       rtx blink = force_reg (DImode, GEN_INT (0x4401fff0));
11403
11404       fnaddr = force_reg (SImode, fnaddr);
11405       cxt = force_reg (SImode, cxt);
11406       emit_insn (gen_mshflo_w_x (gen_rtx_SUBREG (V4HImode, quad0, 0),
11407                                  gen_rtx_SUBREG (V2HImode, fnaddr, 0),
11408                                  movishori));
11409       emit_insn (gen_rotrdi3_mextr (quad0, quad0,
11410                                     GEN_INT (TARGET_LITTLE_ENDIAN ? 24 : 56)));
11411       emit_insn (gen_ashldi3_media (quad0, quad0, const2_rtx));
11412       emit_move_insn (change_address (tramp_mem, DImode, NULL_RTX), quad0);
11413       emit_insn (gen_mshflo_w_x (gen_rtx_SUBREG (V4HImode, cxtload, 0),
11414                                  gen_rtx_SUBREG (V2HImode, cxt, 0),
11415                                  movishori));
11416       emit_insn (gen_rotrdi3_mextr (cxtload, cxtload,
11417                                     GEN_INT (TARGET_LITTLE_ENDIAN ? 24 : 56)));
11418       emit_insn (gen_ashldi3_media (cxtload, cxtload, const2_rtx));
11419       if (TARGET_LITTLE_ENDIAN)
11420         {
11421           emit_insn (gen_mshflo_l_di (quad1, ptabs, cxtload));
11422           emit_insn (gen_mextr4 (quad2, cxtload, blink));
11423         }
11424       else
11425         {
11426           emit_insn (gen_mextr4 (quad1, cxtload, ptabs));
11427           emit_insn (gen_mshflo_l_di (quad2, blink, cxtload));
11428         }
11429       emit_move_insn (adjust_address (tramp_mem, DImode, 8), quad1);
11430       emit_move_insn (adjust_address (tramp_mem, DImode, 16), quad2);
11431       emit_insn (gen_ic_invalidate_line (tramp));
11432       return;
11433     }
11434   else if (TARGET_SHCOMPACT)
11435     {
11436       emit_insn (gen_initialize_trampoline (tramp, cxt, fnaddr));
11437       return;
11438     }
11439   emit_move_insn (change_address (tramp_mem, SImode, NULL_RTX),
11440                   gen_int_mode (TARGET_LITTLE_ENDIAN ? 0xd301d202 : 0xd202d301,
11441                                 SImode));
11442   emit_move_insn (adjust_address (tramp_mem, SImode, 4),
11443                   gen_int_mode (TARGET_LITTLE_ENDIAN ? 0x0009422b : 0x422b0009,
11444                                 SImode));
11445   emit_move_insn (adjust_address (tramp_mem, SImode, 8), cxt);
11446   emit_move_insn (adjust_address (tramp_mem, SImode, 12), fnaddr);
11447   if (TARGET_HARD_SH4 || TARGET_SH5)
11448     {
11449       if (!TARGET_INLINE_IC_INVALIDATE
11450           || (!(TARGET_SH4A_ARCH || TARGET_SH4_300) && TARGET_USERMODE))
11451         emit_library_call (function_symbol (NULL, "__ic_invalidate",
11452                                             FUNCTION_ORDINARY),
11453                            LCT_NORMAL, VOIDmode, 1, tramp, SImode);
11454       else
11455         emit_insn (gen_ic_invalidate_line (tramp));
11456     }
11457 }
11458
11459 /* On SH5, trampolines are SHmedia code, so add 1 to the address.  */
11460 static rtx
11461 sh_trampoline_adjust_address (rtx tramp)
11462 {
11463   if (TARGET_SHMEDIA)
11464     tramp = expand_simple_binop (Pmode, PLUS, tramp, const1_rtx,
11465                                  gen_reg_rtx (Pmode), 0, OPTAB_LIB_WIDEN);
11466   return tramp;
11467 }
11468
11469 /* FIXME: This is overly conservative.  A SHcompact function that
11470    receives arguments ``by reference'' will have them stored in its
11471    own stack frame, so it must not pass pointers or references to
11472    these arguments to other functions by means of sibling calls.  */
11473 /* If PIC, we cannot make sibling calls to global functions
11474    because the PLT requires r12 to be live.  */
11475 static bool
11476 sh_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
11477 {
11478   return (1
11479           && (! TARGET_SHCOMPACT
11480               || crtl->args.info.stack_regs == 0)
11481           && ! sh_cfun_interrupt_handler_p ()
11482           && (! flag_pic
11483               || (decl && ! TREE_PUBLIC (decl))
11484               || (decl && DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT)));
11485 }
11486 \f
11487 /* Machine specific built-in functions.  */
11488
11489 struct builtin_description
11490 {
11491   bool (* const is_enabled) (void);
11492   const enum insn_code icode;
11493   const char *const name;
11494   int signature;
11495   tree fndecl;
11496 };
11497
11498 static bool
11499 shmedia_builtin_p (void)
11500 {
11501   return TARGET_SHMEDIA;
11502 }
11503
11504 /* This function can be used if there are any built-ins that are not for
11505    SHmedia.  It's commented out to avoid the defined-but-unused warning.
11506 static bool
11507 sh1_builtin_p (void)
11508 {
11509   return TARGET_SH1;
11510 }
11511 */
11512
11513 /* describe number and signedness of arguments; arg[0] == result
11514    (1: unsigned, 2: signed, 4: don't care, 8: pointer 0: no argument */
11515 /* 9: 64-bit pointer, 10: 32-bit pointer */
11516 static const char signature_args[][4] =
11517 {
11518 #define SH_BLTIN_V2SI2 0
11519   { 4, 4 },
11520 #define SH_BLTIN_V4HI2 1
11521   { 4, 4 },
11522 #define SH_BLTIN_V2SI3 2
11523   { 4, 4, 4 },
11524 #define SH_BLTIN_V4HI3 3
11525   { 4, 4, 4 },
11526 #define SH_BLTIN_V8QI3 4
11527   { 4, 4, 4 },
11528 #define SH_BLTIN_MAC_HISI 5
11529   { 1, 4, 4, 1 },
11530 #define SH_BLTIN_SH_HI 6
11531   { 4, 4, 1 },
11532 #define SH_BLTIN_SH_SI 7
11533   { 4, 4, 1 },
11534 #define SH_BLTIN_V4HI2V2SI 8
11535   { 4, 4, 4 },
11536 #define SH_BLTIN_V4HI2V8QI 9
11537   { 4, 4, 4 },
11538 #define SH_BLTIN_SISF 10
11539   { 4, 2 },
11540 #define SH_BLTIN_LDUA_L 11
11541   { 2, 10 },
11542 #define SH_BLTIN_LDUA_Q 12
11543   { 1, 10 },
11544 #define SH_BLTIN_STUA_L 13
11545   { 0, 10, 2 },
11546 #define SH_BLTIN_STUA_Q 14
11547   { 0, 10, 1 },
11548 #define SH_BLTIN_LDUA_L64 15
11549   { 2, 9 },
11550 #define SH_BLTIN_LDUA_Q64 16
11551   { 1, 9 },
11552 #define SH_BLTIN_STUA_L64 17
11553   { 0, 9, 2 },
11554 #define SH_BLTIN_STUA_Q64 18
11555   { 0, 9, 1 },
11556 #define SH_BLTIN_NUM_SHARED_SIGNATURES 19
11557 #define SH_BLTIN_2 19
11558 #define SH_BLTIN_SU 19
11559   { 1, 2 },
11560 #define SH_BLTIN_3 20
11561 #define SH_BLTIN_SUS 20
11562   { 2, 2, 1 },
11563 #define SH_BLTIN_PSSV 21
11564   { 0, 8, 2, 2 },
11565 #define SH_BLTIN_XXUU 22
11566 #define SH_BLTIN_UUUU 22
11567   { 1, 1, 1, 1 },
11568 #define SH_BLTIN_PV 23
11569   { 0, 8 },
11570 #define SH_BLTIN_VP 24
11571   { 8, 0 },
11572 };
11573 /* mcmv: operands considered unsigned.  */
11574 /* mmulsum_wq, msad_ubq: result considered unsigned long long.  */
11575 /* mperm: control value considered unsigned int.  */
11576 /* mshalds, mshard, mshards, mshlld, mshlrd: shift count is unsigned int.  */
11577 /* mshards_q: returns signed short.  */
11578 /* nsb: takes long long arg, returns unsigned char.  */
11579 static struct builtin_description bdesc[] =
11580 {
11581   { shmedia_builtin_p,
11582     CODE_FOR_absv2si2,  "__builtin_absv2si2", SH_BLTIN_V2SI2, 0 },
11583   { shmedia_builtin_p,
11584     CODE_FOR_absv4hi2,  "__builtin_absv4hi2", SH_BLTIN_V4HI2, 0 },
11585   { shmedia_builtin_p,
11586     CODE_FOR_addv2si3,  "__builtin_addv2si3", SH_BLTIN_V2SI3, 0 },
11587   { shmedia_builtin_p,
11588     CODE_FOR_addv4hi3,  "__builtin_addv4hi3", SH_BLTIN_V4HI3, 0 },
11589   { shmedia_builtin_p,
11590     CODE_FOR_ssaddv2si3,"__builtin_ssaddv2si3", SH_BLTIN_V2SI3, 0 },
11591   { shmedia_builtin_p,
11592     CODE_FOR_usaddv8qi3,"__builtin_usaddv8qi3", SH_BLTIN_V8QI3, 0 },
11593   { shmedia_builtin_p,
11594     CODE_FOR_ssaddv4hi3,"__builtin_ssaddv4hi3", SH_BLTIN_V4HI3, 0 },
11595   { shmedia_builtin_p,
11596     CODE_FOR_alloco_i,  "__builtin_sh_media_ALLOCO", SH_BLTIN_PV, 0 },
11597   { shmedia_builtin_p,
11598     CODE_FOR_negcmpeqv8qi,"__builtin_sh_media_MCMPEQ_B", SH_BLTIN_V8QI3, 0 },
11599   { shmedia_builtin_p,
11600     CODE_FOR_negcmpeqv2si,"__builtin_sh_media_MCMPEQ_L", SH_BLTIN_V2SI3, 0 },
11601   { shmedia_builtin_p,
11602     CODE_FOR_negcmpeqv4hi,"__builtin_sh_media_MCMPEQ_W", SH_BLTIN_V4HI3, 0 },
11603   { shmedia_builtin_p,
11604     CODE_FOR_negcmpgtuv8qi,"__builtin_sh_media_MCMPGT_UB", SH_BLTIN_V8QI3, 0 },
11605   { shmedia_builtin_p,
11606     CODE_FOR_negcmpgtv2si,"__builtin_sh_media_MCMPGT_L", SH_BLTIN_V2SI3, 0 },
11607   { shmedia_builtin_p,
11608     CODE_FOR_negcmpgtv4hi,"__builtin_sh_media_MCMPGT_W", SH_BLTIN_V4HI3, 0 },
11609   { shmedia_builtin_p,
11610     CODE_FOR_mcmv,      "__builtin_sh_media_MCMV", SH_BLTIN_UUUU, 0 },
11611   { shmedia_builtin_p,
11612     CODE_FOR_mcnvs_lw,  "__builtin_sh_media_MCNVS_LW", SH_BLTIN_3, 0 },
11613   { shmedia_builtin_p,
11614     CODE_FOR_mcnvs_wb,  "__builtin_sh_media_MCNVS_WB", SH_BLTIN_V4HI2V8QI, 0 },
11615   { shmedia_builtin_p,
11616     CODE_FOR_mcnvs_wub, "__builtin_sh_media_MCNVS_WUB", SH_BLTIN_V4HI2V8QI, 0 },
11617   { shmedia_builtin_p,
11618     CODE_FOR_mextr1,    "__builtin_sh_media_MEXTR1", SH_BLTIN_V8QI3, 0 },
11619   { shmedia_builtin_p,
11620     CODE_FOR_mextr2,    "__builtin_sh_media_MEXTR2", SH_BLTIN_V8QI3, 0 },
11621   { shmedia_builtin_p,
11622     CODE_FOR_mextr3,    "__builtin_sh_media_MEXTR3", SH_BLTIN_V8QI3, 0 },
11623   { shmedia_builtin_p,
11624     CODE_FOR_mextr4,    "__builtin_sh_media_MEXTR4", SH_BLTIN_V8QI3, 0 },
11625   { shmedia_builtin_p,
11626     CODE_FOR_mextr5,    "__builtin_sh_media_MEXTR5", SH_BLTIN_V8QI3, 0 },
11627   { shmedia_builtin_p,
11628     CODE_FOR_mextr6,    "__builtin_sh_media_MEXTR6", SH_BLTIN_V8QI3, 0 },
11629   { shmedia_builtin_p,
11630     CODE_FOR_mextr7,    "__builtin_sh_media_MEXTR7", SH_BLTIN_V8QI3, 0 },
11631   { shmedia_builtin_p,
11632     CODE_FOR_mmacfx_wl, "__builtin_sh_media_MMACFX_WL", SH_BLTIN_MAC_HISI, 0 },
11633   { shmedia_builtin_p,
11634     CODE_FOR_mmacnfx_wl,"__builtin_sh_media_MMACNFX_WL", SH_BLTIN_MAC_HISI, 0 },
11635   { shmedia_builtin_p,
11636     CODE_FOR_mulv2si3,  "__builtin_mulv2si3", SH_BLTIN_V2SI3, 0 },
11637   { shmedia_builtin_p,
11638     CODE_FOR_mulv4hi3,  "__builtin_mulv4hi3", SH_BLTIN_V4HI3, 0 },
11639   { shmedia_builtin_p,
11640     CODE_FOR_mmulfx_l,  "__builtin_sh_media_MMULFX_L", SH_BLTIN_V2SI3, 0 },
11641   { shmedia_builtin_p,
11642     CODE_FOR_mmulfx_w,  "__builtin_sh_media_MMULFX_W", SH_BLTIN_V4HI3, 0 },
11643   { shmedia_builtin_p,
11644     CODE_FOR_mmulfxrp_w,"__builtin_sh_media_MMULFXRP_W", SH_BLTIN_V4HI3, 0 },
11645   { shmedia_builtin_p,
11646     CODE_FOR_mmulhi_wl, "__builtin_sh_media_MMULHI_WL", SH_BLTIN_V4HI2V2SI, 0 },
11647   { shmedia_builtin_p,
11648     CODE_FOR_mmullo_wl, "__builtin_sh_media_MMULLO_WL", SH_BLTIN_V4HI2V2SI, 0 },
11649   { shmedia_builtin_p,
11650     CODE_FOR_mmulsum_wq,"__builtin_sh_media_MMULSUM_WQ", SH_BLTIN_XXUU, 0 },
11651   { shmedia_builtin_p,
11652     CODE_FOR_mperm_w,   "__builtin_sh_media_MPERM_W", SH_BLTIN_SH_HI, 0 },
11653   { shmedia_builtin_p,
11654     CODE_FOR_msad_ubq,  "__builtin_sh_media_MSAD_UBQ", SH_BLTIN_XXUU, 0 },
11655   { shmedia_builtin_p,
11656     CODE_FOR_mshalds_l, "__builtin_sh_media_MSHALDS_L", SH_BLTIN_SH_SI, 0 },
11657   { shmedia_builtin_p,
11658     CODE_FOR_mshalds_w, "__builtin_sh_media_MSHALDS_W", SH_BLTIN_SH_HI, 0 },
11659   { shmedia_builtin_p,
11660     CODE_FOR_ashrv2si3, "__builtin_ashrv2si3", SH_BLTIN_SH_SI, 0 },
11661   { shmedia_builtin_p,
11662     CODE_FOR_ashrv4hi3, "__builtin_ashrv4hi3", SH_BLTIN_SH_HI, 0 },
11663   { shmedia_builtin_p,
11664     CODE_FOR_mshards_q, "__builtin_sh_media_MSHARDS_Q", SH_BLTIN_SUS, 0 },
11665   { shmedia_builtin_p,
11666     CODE_FOR_mshfhi_b,  "__builtin_sh_media_MSHFHI_B", SH_BLTIN_V8QI3, 0 },
11667   { shmedia_builtin_p,
11668     CODE_FOR_mshfhi_l,  "__builtin_sh_media_MSHFHI_L", SH_BLTIN_V2SI3, 0 },
11669   { shmedia_builtin_p,
11670     CODE_FOR_mshfhi_w,  "__builtin_sh_media_MSHFHI_W", SH_BLTIN_V4HI3, 0 },
11671   { shmedia_builtin_p,
11672     CODE_FOR_mshflo_b,  "__builtin_sh_media_MSHFLO_B", SH_BLTIN_V8QI3, 0 },
11673   { shmedia_builtin_p,
11674     CODE_FOR_mshflo_l,  "__builtin_sh_media_MSHFLO_L", SH_BLTIN_V2SI3, 0 },
11675   { shmedia_builtin_p,
11676     CODE_FOR_mshflo_w,  "__builtin_sh_media_MSHFLO_W", SH_BLTIN_V4HI3, 0 },
11677   { shmedia_builtin_p,
11678     CODE_FOR_ashlv2si3, "__builtin_ashlv2si3", SH_BLTIN_SH_SI, 0 },
11679   { shmedia_builtin_p,
11680     CODE_FOR_ashlv4hi3, "__builtin_ashlv4hi3", SH_BLTIN_SH_HI, 0 },
11681   { shmedia_builtin_p,
11682     CODE_FOR_lshrv2si3, "__builtin_lshrv2si3", SH_BLTIN_SH_SI, 0 },
11683   { shmedia_builtin_p,
11684     CODE_FOR_lshrv4hi3, "__builtin_lshrv4hi3", SH_BLTIN_SH_HI, 0 },
11685   { shmedia_builtin_p,
11686     CODE_FOR_subv2si3,  "__builtin_subv2si3", SH_BLTIN_V2SI3, 0 },
11687   { shmedia_builtin_p,
11688     CODE_FOR_subv4hi3,  "__builtin_subv4hi3", SH_BLTIN_V4HI3, 0 },
11689   { shmedia_builtin_p,
11690     CODE_FOR_sssubv2si3,"__builtin_sssubv2si3", SH_BLTIN_V2SI3, 0 },
11691   { shmedia_builtin_p,
11692     CODE_FOR_ussubv8qi3,"__builtin_ussubv8qi3", SH_BLTIN_V8QI3, 0 },
11693   { shmedia_builtin_p,
11694     CODE_FOR_sssubv4hi3,"__builtin_sssubv4hi3", SH_BLTIN_V4HI3, 0 },
11695   { shmedia_builtin_p,
11696     CODE_FOR_fcosa_s,   "__builtin_sh_media_FCOSA_S", SH_BLTIN_SISF, 0 },
11697   { shmedia_builtin_p,
11698     CODE_FOR_fsina_s,   "__builtin_sh_media_FSINA_S", SH_BLTIN_SISF, 0 },
11699   { shmedia_builtin_p,
11700     CODE_FOR_fipr,      "__builtin_sh_media_FIPR_S", SH_BLTIN_3, 0 },
11701   { shmedia_builtin_p,
11702     CODE_FOR_ftrv,      "__builtin_sh_media_FTRV_S", SH_BLTIN_3, 0 },
11703   { shmedia_builtin_p,
11704     CODE_FOR_sqrtdf2,   "__builtin_sh_media_FSQRT_D", SH_BLTIN_2, 0 },
11705   { shmedia_builtin_p,
11706     CODE_FOR_sqrtsf2,   "__builtin_sh_media_FSQRT_S", SH_BLTIN_2, 0 },
11707   { shmedia_builtin_p,
11708     CODE_FOR_fsrra_s,   "__builtin_sh_media_FSRRA_S", SH_BLTIN_2, 0 },
11709   { shmedia_builtin_p,
11710     CODE_FOR_ldhi_l,    "__builtin_sh_media_LDHI_L", SH_BLTIN_LDUA_L, 0 },
11711   { shmedia_builtin_p,
11712     CODE_FOR_ldhi_q,    "__builtin_sh_media_LDHI_Q", SH_BLTIN_LDUA_Q, 0 },
11713   { shmedia_builtin_p,
11714     CODE_FOR_ldlo_l,    "__builtin_sh_media_LDLO_L", SH_BLTIN_LDUA_L, 0 },
11715   { shmedia_builtin_p,
11716     CODE_FOR_ldlo_q,    "__builtin_sh_media_LDLO_Q", SH_BLTIN_LDUA_Q, 0 },
11717   { shmedia_builtin_p,
11718     CODE_FOR_sthi_l,    "__builtin_sh_media_STHI_L", SH_BLTIN_STUA_L, 0 },
11719   { shmedia_builtin_p,
11720     CODE_FOR_sthi_q,    "__builtin_sh_media_STHI_Q", SH_BLTIN_STUA_Q, 0 },
11721   { shmedia_builtin_p,
11722     CODE_FOR_stlo_l,    "__builtin_sh_media_STLO_L", SH_BLTIN_STUA_L, 0 },
11723   { shmedia_builtin_p,
11724     CODE_FOR_stlo_q,    "__builtin_sh_media_STLO_Q", SH_BLTIN_STUA_Q, 0 },
11725   { shmedia_builtin_p,
11726     CODE_FOR_ldhi_l64,  "__builtin_sh_media_LDHI_L", SH_BLTIN_LDUA_L64, 0 },
11727   { shmedia_builtin_p,
11728     CODE_FOR_ldhi_q64,  "__builtin_sh_media_LDHI_Q", SH_BLTIN_LDUA_Q64, 0 },
11729   { shmedia_builtin_p,
11730     CODE_FOR_ldlo_l64,  "__builtin_sh_media_LDLO_L", SH_BLTIN_LDUA_L64, 0 },
11731   { shmedia_builtin_p,
11732     CODE_FOR_ldlo_q64,  "__builtin_sh_media_LDLO_Q", SH_BLTIN_LDUA_Q64, 0 },
11733   { shmedia_builtin_p,
11734     CODE_FOR_sthi_l64,  "__builtin_sh_media_STHI_L", SH_BLTIN_STUA_L64, 0 },
11735   { shmedia_builtin_p,
11736     CODE_FOR_sthi_q64,  "__builtin_sh_media_STHI_Q", SH_BLTIN_STUA_Q64, 0 },
11737   { shmedia_builtin_p,
11738     CODE_FOR_stlo_l64,  "__builtin_sh_media_STLO_L", SH_BLTIN_STUA_L64, 0 },
11739   { shmedia_builtin_p,
11740     CODE_FOR_stlo_q64,  "__builtin_sh_media_STLO_Q", SH_BLTIN_STUA_Q64, 0 },
11741   { shmedia_builtin_p,
11742     CODE_FOR_nsb,       "__builtin_sh_media_NSB", SH_BLTIN_SU, 0 },
11743   { shmedia_builtin_p,
11744     CODE_FOR_byterev,   "__builtin_sh_media_BYTEREV", SH_BLTIN_2, 0 },
11745   { shmedia_builtin_p,
11746     CODE_FOR_prefetch,  "__builtin_sh_media_PREFO", SH_BLTIN_PSSV, 0 },
11747 };
11748
11749 static void
11750 sh_init_builtins (void)
11751 {
11752   tree shared[SH_BLTIN_NUM_SHARED_SIGNATURES];
11753   memset (shared, 0, sizeof shared);
11754
11755   for (unsigned int di = 0; di < ARRAY_SIZE (bdesc); ++di)
11756     {
11757       builtin_description* d = &bdesc[di];
11758
11759       if (!d->is_enabled ())
11760         continue;
11761
11762       tree type, arg_type = NULL_TREE;
11763       int signature = d->signature;
11764
11765       if (signature < SH_BLTIN_NUM_SHARED_SIGNATURES && shared[signature])
11766         type = shared[signature];
11767       else
11768         {
11769           int has_result = signature_args[signature][0] != 0;
11770           tree args[3];
11771
11772           if ((signature_args[signature][1] & 8)
11773               && (((signature_args[signature][1] & 1) && TARGET_SHMEDIA32)
11774                   || ((signature_args[signature][1] & 2) && TARGET_SHMEDIA64)))
11775             continue;
11776           if (! TARGET_FPU_ANY
11777               && FLOAT_MODE_P (insn_data[d->icode].operand[0].mode))
11778             continue;
11779           for (unsigned int i = 0; i < ARRAY_SIZE (args); i++)
11780             args[i] = NULL_TREE;
11781           for (int i = 3; ; i--)
11782             {
11783               int arg = signature_args[signature][i];
11784               int opno = i - 1 + has_result;
11785
11786               if (arg & 8)
11787                 arg_type = ptr_type_node;
11788               else if (arg)
11789                 arg_type = (*lang_hooks.types.type_for_mode)
11790                   (insn_data[d->icode].operand[opno].mode, (arg & 1));
11791               else if (i)
11792                 continue;
11793               else
11794                 arg_type = void_type_node;
11795               if (i == 0)
11796                 break;
11797               args[i-1] = arg_type;
11798             }
11799           type = build_function_type_list (arg_type, args[0], args[1],
11800                                            args[2], NULL_TREE);
11801           if (signature < SH_BLTIN_NUM_SHARED_SIGNATURES)
11802             shared[signature] = type;
11803         }
11804       d->fndecl =
11805         add_builtin_function (d->name, type, d - bdesc, BUILT_IN_MD,
11806                               NULL, NULL_TREE);
11807     }
11808 }
11809
11810 /* Implements target hook vector_mode_supported_p.  */
11811 bool
11812 sh_vector_mode_supported_p (enum machine_mode mode)
11813 {
11814   if (TARGET_FPU_ANY
11815       && ((mode == V2SFmode)
11816           || (mode == V4SFmode)
11817           || (mode == V16SFmode)))
11818     return true;
11819
11820   else if (TARGET_SHMEDIA
11821            && ((mode == V8QImode)
11822                || (mode == V2HImode)
11823                || (mode == V4HImode)
11824                || (mode == V2SImode)))
11825     return true;
11826
11827   return false;
11828 }
11829
11830 bool
11831 sh_frame_pointer_required (void)
11832 {
11833 /* If needed override this in other tm.h files to cope with various OS 
11834    lossage requiring a frame pointer.  */
11835   if (SUBTARGET_FRAME_POINTER_REQUIRED)
11836     return true;
11837
11838   if (crtl->profile)
11839     return true;
11840
11841   return false;
11842 }
11843
11844 /* Implements target hook dwarf_calling_convention.  Return an enum
11845    of dwarf_calling_convention.  */
11846 int
11847 sh_dwarf_calling_convention (const_tree func)
11848 {
11849   if (sh_attr_renesas_p (func))
11850     return DW_CC_GNU_renesas_sh;
11851
11852   return DW_CC_normal;
11853 }
11854
11855 /* Returns the sh builtin decl for CODE.  */
11856 static tree
11857 sh_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
11858 {
11859   if (code >= ARRAY_SIZE (bdesc))
11860     return error_mark_node;
11861
11862   if (!bdesc[code].is_enabled ())
11863     return error_mark_node;
11864
11865   return bdesc[code].fndecl;
11866 }
11867
11868 /* Expand an expression EXP that calls a built-in function,
11869    with result going to TARGET if that's convenient
11870    (and in mode MODE if that's convenient).
11871    SUBTARGET may be used as the target for computing one of EXP's operands.
11872    IGNORE is nonzero if the value is to be ignored.  */
11873 static rtx
11874 sh_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
11875                    enum machine_mode mode ATTRIBUTE_UNUSED, int ignore)
11876 {
11877   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11878   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
11879   const struct builtin_description *d = &bdesc[fcode];
11880   enum insn_code icode = d->icode;
11881   int signature = d->signature;
11882   int nop = 0;
11883   rtx op[4];
11884
11885   if (signature_args[signature][0])
11886     {
11887       if (ignore)
11888         return NULL_RTX;
11889
11890       enum machine_mode tmode = insn_data[icode].operand[0].mode;
11891       if (! target || GET_MODE (target) != tmode
11892           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11893         target = gen_reg_rtx (tmode);
11894       op[nop++] = target;
11895     }
11896   else
11897     target = NULL_RTX;
11898
11899   for (int i = 1; i <= 3; i++, nop++)
11900     {
11901       tree arg;
11902       enum machine_mode opmode, argmode;
11903       tree optype;
11904
11905       if (! signature_args[signature][i])
11906         break;
11907       arg = CALL_EXPR_ARG (exp, i - 1);
11908       if (arg == error_mark_node)
11909         return const0_rtx;
11910       if (signature_args[signature][i] & 8)
11911         {
11912           opmode = ptr_mode;
11913           optype = ptr_type_node;
11914         }
11915       else
11916         {
11917           opmode = insn_data[icode].operand[nop].mode;
11918           optype = (*lang_hooks.types.type_for_mode) (opmode, 0);
11919         }
11920       argmode = TYPE_MODE (TREE_TYPE (arg));
11921       if (argmode != opmode)
11922         arg = build1 (NOP_EXPR, optype, arg);
11923       op[nop] = expand_expr (arg, NULL_RTX, opmode, EXPAND_NORMAL);
11924       if (! (*insn_data[icode].operand[nop].predicate) (op[nop], opmode))
11925         op[nop] = copy_to_mode_reg (opmode, op[nop]);
11926     }
11927
11928   rtx pat = NULL_RTX;
11929
11930   switch (nop)
11931     {
11932     case 1:
11933       pat = (*insn_data[d->icode].genfun) (op[0]);
11934       break;
11935     case 2:
11936       pat = (*insn_data[d->icode].genfun) (op[0], op[1]);
11937       break;
11938     case 3:
11939       pat = (*insn_data[d->icode].genfun) (op[0], op[1], op[2]);
11940       break;
11941     case 4:
11942       pat = (*insn_data[d->icode].genfun) (op[0], op[1], op[2], op[3]);
11943       break;
11944     default:
11945       gcc_unreachable ();
11946     }
11947   if (! pat)
11948     return NULL_RTX;
11949   emit_insn (pat);
11950   return target;
11951 }
11952
11953 void
11954 sh_expand_unop_v2sf (enum rtx_code code, rtx op0, rtx op1)
11955 {
11956   rtx sel0 = const0_rtx;
11957   rtx sel1 = const1_rtx;
11958   rtx (*fn) (rtx, rtx, rtx, rtx, rtx) = gen_unary_sf_op;
11959   rtx op = gen_rtx_fmt_e (code, SFmode, op1);
11960
11961   emit_insn ((*fn) (op0, op1, op, sel0, sel0));
11962   emit_insn ((*fn) (op0, op1, op, sel1, sel1));
11963 }
11964
11965 void
11966 sh_expand_binop_v2sf (enum rtx_code code, rtx op0, rtx op1, rtx op2)
11967 {
11968   rtx op = gen_rtx_fmt_ee (code, SFmode, op1, op2);
11969
11970   emit_insn (gen_binary_sf_op0 (op0, op1, op2, op));
11971   emit_insn (gen_binary_sf_op1 (op0, op1, op2, op));
11972 }
11973
11974 /* Return true if hard register REGNO can hold a value of machine-mode MODE.
11975    We can allow any mode in any general register.  The special registers
11976    only allow SImode.  Don't allow any mode in the PR.
11977
11978    We cannot hold DCmode values in the XD registers because alter_reg
11979    handles subregs of them incorrectly.  We could work around this by
11980    spacing the XD registers like the DR registers, but this would require
11981    additional memory in every compilation to hold larger register vectors.
11982    We could hold SFmode / SCmode values in XD registers, but that
11983    would require a tertiary reload when reloading from / to memory,
11984    and a secondary reload to reload from / to general regs; that
11985    seems to be a losing proposition.
11986
11987    We want to allow TImode FP regs so that when V4SFmode is loaded as TImode,
11988    it won't be ferried through GP registers first.  */
11989 bool
11990 sh_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
11991 {
11992   if (SPECIAL_REGISTER_P (regno))
11993     return mode == SImode;
11994
11995   if (regno == FPUL_REG)
11996     return (mode == SImode || mode == SFmode);
11997
11998   if (FP_REGISTER_P (regno) && mode == SFmode)
11999     return true;
12000
12001   if (mode == V2SFmode)
12002     {
12003       if (((FP_REGISTER_P (regno) && (regno - FIRST_FP_REG) % 2 == 0)
12004            || GENERAL_REGISTER_P (regno)))
12005         return true;
12006       else
12007         return false;
12008     }
12009
12010   if (mode == V4SFmode)
12011     {
12012       if ((FP_REGISTER_P (regno) && (regno - FIRST_FP_REG) % 4 == 0)
12013           || GENERAL_REGISTER_P (regno))
12014         return true;
12015       else
12016         return false;
12017     }
12018
12019   if (mode == V16SFmode)
12020     {
12021       if (TARGET_SHMEDIA)
12022         {
12023           if (FP_REGISTER_P (regno) && (regno - FIRST_FP_REG) % 16 == 0)
12024             return true;
12025           else
12026             return false;
12027         }
12028       else
12029         return regno == FIRST_XD_REG;
12030     }
12031
12032   if (FP_REGISTER_P (regno))
12033     {
12034       if (mode == SFmode
12035           || mode == SImode
12036           || ((TARGET_SH2E || TARGET_SHMEDIA) && mode == SCmode)
12037           || ((((TARGET_SH4 || TARGET_SH2A_DOUBLE) && mode == DFmode)
12038                || mode == DCmode
12039                || (TARGET_SHMEDIA
12040                    && (mode == DFmode || mode == DImode
12041                        || mode == V2SFmode || mode == TImode)))
12042               && ((regno - FIRST_FP_REG) & 1) == 0)
12043           || ((TARGET_SH4 || TARGET_SHMEDIA) && mode == TImode
12044               && ((regno - FIRST_FP_REG) & 3) == 0))
12045         return true;
12046       else
12047         return false;
12048     }
12049
12050   if (XD_REGISTER_P (regno))
12051     return mode == DFmode;
12052
12053   if (TARGET_REGISTER_P (regno))
12054     return (mode == DImode || mode == SImode || mode == PDImode);
12055
12056   if (regno == PR_REG)
12057     return mode == SImode;
12058
12059   if (regno == FPSCR_REG)
12060     return mode == PSImode;
12061
12062   /* FIXME.  This works around PR target/37633 for -O0.  */
12063   if (!optimize && TARGET_SHMEDIA32 && GET_MODE_SIZE (mode) > 4)
12064     {
12065       unsigned int n = GET_MODE_SIZE (mode) / 8;
12066
12067       if (regno >= FIRST_GENERAL_REG + 10 - n + 1
12068           && regno <= FIRST_GENERAL_REG + 14)
12069         return false;
12070     }
12071
12072   return true;
12073 }
12074
12075 /* Return the class of registers for which a mode change from FROM to TO
12076    is invalid.  */
12077 bool
12078 sh_cannot_change_mode_class (enum machine_mode from, enum machine_mode to,
12079                              enum reg_class rclass)
12080 {
12081   /* We want to enable the use of SUBREGs as a means to
12082      VEC_SELECT a single element of a vector.  */
12083
12084   /* This effectively disallows using GENERAL_REGS for SFmode vector subregs.
12085      This can be problematic when SFmode vector subregs need to be accessed
12086      on the stack with displacement addressing, as it happens with -O0.
12087      Thus we disallow the mode change for -O0.  */
12088   if (to == SFmode && VECTOR_MODE_P (from) && GET_MODE_INNER (from) == SFmode)
12089     return optimize ? (reg_classes_intersect_p (GENERAL_REGS, rclass)) : false;
12090
12091   if (GET_MODE_SIZE (from) != GET_MODE_SIZE (to))
12092     {
12093       if (TARGET_LITTLE_ENDIAN)
12094         {
12095           if (GET_MODE_SIZE (to) < 8 || GET_MODE_SIZE (from) < 8)
12096             return reg_classes_intersect_p (DF_REGS, rclass);
12097         }
12098       else
12099         {
12100           if (GET_MODE_SIZE (from) < 8)
12101             return reg_classes_intersect_p (DF_REGS, rclass);
12102         }
12103     }
12104   return false;
12105 }
12106
12107 /* Return true if registers in machine mode MODE will likely be
12108    allocated to registers in small register classes.  */
12109 bool
12110 sh_small_register_classes_for_mode_p (enum machine_mode mode ATTRIBUTE_UNUSED)
12111 {
12112   return (! TARGET_SHMEDIA);
12113 }
12114
12115 /* If ADDRESS refers to a CODE_LABEL, add NUSES to the number of times
12116    that label is used.  */
12117 void
12118 sh_mark_label (rtx address, int nuses)
12119 {
12120   if (GOTOFF_P (address))
12121     {
12122       /* Extract the label or symbol.  */
12123       address = XEXP (address, 0);
12124       if (GET_CODE (address) == PLUS)
12125         address = XEXP (address, 0);
12126       address = XVECEXP (address, 0, 0);
12127     }
12128   if (GET_CODE (address) == LABEL_REF
12129       && LABEL_P (XEXP (address, 0)))
12130     LABEL_NUSES (XEXP (address, 0)) += nuses;
12131 }
12132
12133 /* Compute extra cost of moving data between one register class
12134    and another.
12135
12136    If SECONDARY*_RELOAD_CLASS says something about the src/dst pair, regclass
12137    uses this information.  Hence, the general register <-> floating point
12138    register information here is not used for SFmode.  */
12139 static int
12140 sh_register_move_cost (enum machine_mode mode,
12141                        reg_class_t srcclass, reg_class_t dstclass)
12142 {
12143   if (dstclass == T_REGS || dstclass == PR_REGS)
12144     return 10;
12145
12146   if (dstclass == MAC_REGS && srcclass == MAC_REGS)
12147     return 4;
12148
12149   if (mode == SImode && ! TARGET_SHMEDIA && TARGET_FMOVD
12150       && REGCLASS_HAS_FP_REG (srcclass)
12151       && REGCLASS_HAS_FP_REG (dstclass))
12152     return 4;
12153
12154   if (REGCLASS_HAS_FP_REG (dstclass) && srcclass == T_REGS)
12155     return ((TARGET_HARD_SH4 && !optimize_size) ? 10 : 7);
12156
12157   if ((REGCLASS_HAS_FP_REG (dstclass) && srcclass == MAC_REGS)
12158       || (dstclass == MAC_REGS && REGCLASS_HAS_FP_REG (srcclass)))
12159     return 9;
12160
12161   if ((REGCLASS_HAS_FP_REG (dstclass)
12162        && REGCLASS_HAS_GENERAL_REG (srcclass))
12163       || (REGCLASS_HAS_GENERAL_REG (dstclass)
12164           && REGCLASS_HAS_FP_REG (srcclass)))
12165     {
12166       /* Discourage trying to use fp regs for a pointer.  This also
12167          discourages fp regs with SImode because Pmode is an alias
12168          of SImode on this target.  See PR target/48596.  */
12169       int addend = (mode == Pmode) ? 40 : 0;
12170
12171       return (((TARGET_SHMEDIA ? 4 : TARGET_FMOVD ? 8 : 12) + addend)
12172               * ((GET_MODE_SIZE (mode) + 7) / 8U));
12173     }
12174
12175   if ((dstclass == FPUL_REGS
12176        && REGCLASS_HAS_GENERAL_REG (srcclass))
12177       || (srcclass == FPUL_REGS
12178           && REGCLASS_HAS_GENERAL_REG (dstclass)))
12179     return 5;
12180
12181   if ((dstclass == FPUL_REGS
12182        && (srcclass == PR_REGS || srcclass == MAC_REGS || srcclass == T_REGS))
12183       || (srcclass == FPUL_REGS
12184           && (dstclass == PR_REGS || dstclass == MAC_REGS)))
12185     return 7;
12186
12187   if ((srcclass == TARGET_REGS && ! REGCLASS_HAS_GENERAL_REG (dstclass))
12188       || ((dstclass) == TARGET_REGS && ! REGCLASS_HAS_GENERAL_REG (srcclass)))
12189     return 20;
12190
12191   /* ??? ptabs faults on (value & 0x3) == 0x3  */
12192   if (TARGET_SHMEDIA
12193       && ((srcclass) == TARGET_REGS || (srcclass) == SIBCALL_REGS))
12194     {
12195       if (sh_gettrcost >= 0)
12196         return sh_gettrcost;
12197       else if (!TARGET_PT_FIXED)
12198         return 100;
12199     }
12200
12201   if ((srcclass == FPSCR_REGS && ! REGCLASS_HAS_GENERAL_REG (dstclass))
12202       || (dstclass == FPSCR_REGS && ! REGCLASS_HAS_GENERAL_REG (srcclass)))
12203   return 4;
12204
12205   if (TARGET_SHMEDIA
12206       || (TARGET_FMOVD
12207           && ! REGCLASS_HAS_GENERAL_REG (srcclass)
12208           && ! REGCLASS_HAS_GENERAL_REG (dstclass)))
12209     return 2 * ((GET_MODE_SIZE (mode) + 7) / 8U);
12210
12211   return 2 * ((GET_MODE_SIZE (mode) + 3) / 4U);
12212 }
12213
12214 static rtx
12215 emit_load_ptr (rtx reg, rtx addr)
12216 {
12217   rtx mem = gen_const_mem (ptr_mode, addr);
12218
12219   if (Pmode != ptr_mode)
12220     mem = gen_rtx_SIGN_EXTEND (Pmode, mem);
12221   return emit_move_insn (reg, mem);
12222 }
12223
12224 static void
12225 sh_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
12226                     HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
12227                     tree function)
12228 {
12229   CUMULATIVE_ARGS cum;
12230   int structure_value_byref = 0;
12231   rtx this_rtx, this_value, sibcall, insns, funexp;
12232   tree funtype = TREE_TYPE (function);
12233   int simple_add = CONST_OK_FOR_ADD (delta);
12234   int did_load = 0;
12235   rtx scratch0, scratch1, scratch2;
12236   unsigned i;
12237
12238   reload_completed = 1;
12239   epilogue_completed = 1;
12240   crtl->uses_only_leaf_regs = 1;
12241
12242   emit_note (NOTE_INSN_PROLOGUE_END);
12243
12244   /* Find the "this" pointer.  We have such a wide range of ABIs for the
12245      SH that it's best to do this completely machine independently.
12246      "this" is passed as first argument, unless a structure return pointer
12247      comes first, in which case "this" comes second.  */
12248   INIT_CUMULATIVE_ARGS (cum, funtype, NULL_RTX, 0, 1);
12249 #ifndef PCC_STATIC_STRUCT_RETURN
12250   if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
12251     structure_value_byref = 1;
12252 #endif /* not PCC_STATIC_STRUCT_RETURN */
12253   if (structure_value_byref && sh_struct_value_rtx (function, 0) == 0)
12254     {
12255       tree ptype = build_pointer_type (TREE_TYPE (funtype));
12256
12257       sh_function_arg_advance (pack_cumulative_args (&cum), Pmode, ptype, true);
12258     }
12259   this_rtx
12260     = sh_function_arg (pack_cumulative_args (&cum), Pmode, ptr_type_node, true);
12261
12262   /* For SHcompact, we only have r0 for a scratch register: r1 is the
12263      static chain pointer (even if you can't have nested virtual functions
12264      right now, someone might implement them sometime), and the rest of the
12265      registers are used for argument passing, are callee-saved, or reserved.  */
12266   /* We need to check call_used_regs / fixed_regs in case -fcall_saved-reg /
12267      -ffixed-reg has been used.  */
12268   if (! call_used_regs[0] || fixed_regs[0])
12269     error ("r0 needs to be available as a call-clobbered register");
12270   scratch0 = scratch1 = scratch2 = gen_rtx_REG (Pmode, 0);
12271   if (! TARGET_SH5)
12272     {
12273       if (call_used_regs[1] && ! fixed_regs[1])
12274         scratch1 = gen_rtx_REG (ptr_mode, 1);
12275       /* N.B., if not TARGET_HITACHI, register 2 is used to pass the pointer
12276          pointing where to return struct values.  */
12277       if (call_used_regs[3] && ! fixed_regs[3])
12278         scratch2 = gen_rtx_REG (Pmode, 3);
12279     }
12280   else if (TARGET_SHMEDIA)
12281     {
12282       for (i = FIRST_GENERAL_REG; i <= LAST_GENERAL_REG; i++)
12283         if (i != REGNO (scratch0) &&
12284             call_used_regs[i] && ! fixed_regs[i] && ! FUNCTION_ARG_REGNO_P (i))
12285           {
12286             scratch1 = gen_rtx_REG (ptr_mode, i);
12287             break;
12288           }
12289       if (scratch1 == scratch0)
12290         error ("need a second call-clobbered general purpose register");
12291       for (i = FIRST_TARGET_REG; i <= LAST_TARGET_REG; i++)
12292         if (call_used_regs[i] && ! fixed_regs[i])
12293           {
12294             scratch2 = gen_rtx_REG (Pmode, i);
12295             break;
12296           }
12297       if (scratch2 == scratch0)
12298         error ("need a call-clobbered target register");
12299     }
12300
12301   this_value = plus_constant (Pmode, this_rtx, delta);
12302   if (vcall_offset
12303       && (simple_add || scratch0 != scratch1)
12304       && strict_memory_address_p (ptr_mode, this_value))
12305     {
12306       emit_load_ptr (scratch0, this_value);
12307       did_load = 1;
12308     }
12309
12310   if (!delta)
12311     ; /* Do nothing.  */
12312   else if (simple_add)
12313     emit_move_insn (this_rtx, this_value);
12314   else
12315     {
12316       emit_move_insn (scratch1, GEN_INT (delta));
12317       emit_insn (gen_add2_insn (this_rtx, scratch1));
12318     }
12319
12320   if (vcall_offset)
12321     {
12322       rtx offset_addr;
12323
12324       if (!did_load)
12325         emit_load_ptr (scratch0, this_rtx);
12326
12327       offset_addr = plus_constant (Pmode, scratch0, vcall_offset);
12328       if (strict_memory_address_p (ptr_mode, offset_addr))
12329         ; /* Do nothing.  */
12330       else if (! TARGET_SH5 && scratch0 != scratch1)
12331         {
12332           /* scratch0 != scratch1, and we have indexed loads.  Get better
12333              schedule by loading the offset into r1 and using an indexed
12334              load - then the load of r1 can issue before the load from
12335              (this_rtx + delta) finishes.  */
12336           emit_move_insn (scratch1, GEN_INT (vcall_offset));
12337           offset_addr = gen_rtx_PLUS (Pmode, scratch0, scratch1);
12338         }
12339       else if (CONST_OK_FOR_ADD (vcall_offset))
12340         {
12341           emit_insn (gen_add2_insn (scratch0, GEN_INT (vcall_offset)));
12342           offset_addr = scratch0;
12343         }
12344       else if (scratch0 != scratch1)
12345         {
12346           emit_move_insn (scratch1, GEN_INT (vcall_offset));
12347           emit_insn (gen_add2_insn (scratch0, scratch1));
12348           offset_addr = scratch0;
12349         }
12350       else
12351         gcc_unreachable (); /* FIXME */
12352       emit_load_ptr (scratch0, offset_addr);
12353
12354       if (Pmode != ptr_mode)
12355         scratch0 = gen_rtx_TRUNCATE (ptr_mode, scratch0);
12356       emit_insn (gen_add2_insn (this_rtx, scratch0));
12357     }
12358
12359   /* Generate a tail call to the target function.  */
12360   if (! TREE_USED (function))
12361     {
12362       assemble_external (function);
12363       TREE_USED (function) = 1;
12364     }
12365   funexp = XEXP (DECL_RTL (function), 0);
12366   /* If the function is overridden, so is the thunk, hence we don't
12367      need GOT addressing even if this is a public symbol.  */
12368 #if 0
12369   if (TARGET_SH1 && ! flag_weak)
12370     sibcall = gen_sibcalli_thunk (funexp, const0_rtx);
12371   else
12372 #endif
12373   if (TARGET_SH2 && flag_pic)
12374     {
12375       sibcall = gen_sibcall_pcrel (funexp, const0_rtx);
12376       XEXP (XVECEXP (sibcall, 0, 2), 0) = scratch2;
12377     }
12378   else
12379     {
12380       if (TARGET_SHMEDIA && flag_pic)
12381         {
12382           funexp = gen_sym2PIC (funexp);
12383           PUT_MODE (funexp, Pmode);
12384         }
12385       emit_move_insn (scratch2, funexp);
12386       funexp = gen_rtx_MEM (FUNCTION_MODE, scratch2);
12387       sibcall = gen_sibcall (funexp, const0_rtx, NULL_RTX);
12388     }
12389   sibcall = emit_call_insn (sibcall);
12390   SIBLING_CALL_P (sibcall) = 1;
12391   use_reg (&CALL_INSN_FUNCTION_USAGE (sibcall), this_rtx);
12392   emit_barrier ();
12393
12394   /* Run just enough of rest_of_compilation to do scheduling and get
12395      the insns emitted.  Note that use_thunk calls
12396      assemble_start_function and assemble_end_function.  */
12397
12398   insns = get_insns ();
12399
12400   if (optimize > 0)
12401     {
12402       if (! cfun->cfg)
12403         init_flow (cfun);
12404       split_all_insns_noflow ();
12405     }
12406
12407   sh_reorg ();
12408   shorten_branches (insns);
12409   final_start_function (insns, file, 1);
12410   final (insns, file, 1);
12411   final_end_function ();
12412
12413   reload_completed = 0;
12414   epilogue_completed = 0;
12415 }
12416
12417 rtx
12418 function_symbol (rtx target, const char *name, enum sh_function_kind kind)
12419 {
12420   rtx sym;
12421
12422   /* If this is not an ordinary function, the name usually comes from a
12423      string literal or an sprintf buffer.  Make sure we use the same
12424      string consistently, so that cse will be able to unify address loads.  */
12425   if (kind != FUNCTION_ORDINARY)
12426     name = IDENTIFIER_POINTER (get_identifier (name));
12427   sym = gen_rtx_SYMBOL_REF (Pmode, name);
12428   SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_FUNCTION;
12429   if (flag_pic)
12430     switch (kind)
12431       {
12432       case FUNCTION_ORDINARY:
12433         break;
12434       case SFUNC_GOT:
12435         {
12436           rtx reg = target ? target : gen_reg_rtx (Pmode);
12437
12438           emit_insn (gen_symGOT2reg (reg, sym));
12439           sym = reg;
12440           break;
12441         }
12442       case SFUNC_STATIC:
12443         {
12444           /* ??? To allow cse to work, we use GOTOFF relocations.
12445              We could add combiner patterns to transform this into
12446              straight pc-relative calls with sym2PIC / bsrf when
12447              label load and function call are still 1:1 and in the
12448              same basic block during combine.  */
12449           rtx reg = target ? target : gen_reg_rtx (Pmode);
12450
12451           emit_insn (gen_symGOTOFF2reg (reg, sym));
12452           sym = reg;
12453           break;
12454         }
12455       }
12456   if (target && sym != target)
12457     {
12458       emit_move_insn (target, sym);
12459       return target;
12460     }
12461   return sym;
12462 }
12463
12464 /* Find the number of a general purpose register in S.  */
12465 static int
12466 scavenge_reg (HARD_REG_SET *s)
12467 {
12468   int r;
12469   for (r = FIRST_GENERAL_REG; r <= LAST_GENERAL_REG; r++)
12470     if (TEST_HARD_REG_BIT (*s, r))
12471       return r;
12472   return -1;
12473 }
12474
12475 rtx
12476 sh_get_pr_initial_val (void)
12477 {
12478   rtx val;
12479
12480   /* ??? Unfortunately, get_hard_reg_initial_val doesn't always work for the
12481      PR register on SHcompact, because it might be clobbered by the prologue.
12482      We check first if that is known to be the case.  */
12483   if (TARGET_SHCOMPACT
12484       && ((crtl->args.info.call_cookie
12485            & ~ CALL_COOKIE_RET_TRAMP (1))
12486           || crtl->saves_all_registers))
12487     return gen_frame_mem (SImode, return_address_pointer_rtx);
12488
12489   /* If we haven't finished rtl generation, there might be a nonlocal label
12490      that we haven't seen yet.
12491      ??? get_hard_reg_initial_val fails if it is called after register
12492      allocation has started, unless it has been called before for the
12493      same register.  And even then, we end in trouble if we didn't use
12494      the register in the same basic block before.  So call
12495      get_hard_reg_initial_val now and wrap it in an unspec if we might
12496      need to replace it.  */
12497   /* ??? We also must do this for TARGET_SH1 in general, because otherwise
12498      combine can put the pseudo returned by get_hard_reg_initial_val into
12499      instructions that need a general purpose registers, which will fail to
12500      be recognized when the pseudo becomes allocated to PR.  */
12501   val
12502     = get_hard_reg_initial_val (Pmode, TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG);
12503   if (TARGET_SH1)
12504     return gen_rtx_UNSPEC (SImode, gen_rtvec (1, val), UNSPEC_RA);
12505   return val;
12506 }
12507
12508 bool
12509 sh_expand_t_scc (rtx operands[])
12510 {
12511   enum rtx_code code = GET_CODE (operands[1]);
12512   rtx target = operands[0];
12513   rtx op0 = operands[2];
12514   rtx op1 = operands[3];
12515   rtx result = target;
12516   HOST_WIDE_INT val;
12517
12518   if (!REG_P (op0) || REGNO (op0) != T_REG
12519       || !CONST_INT_P (op1))
12520     return false;
12521   if (!REG_P (result))
12522     result = gen_reg_rtx (SImode);
12523   val = INTVAL (op1);
12524   if ((code == EQ && val == 1) || (code == NE && val == 0))
12525     emit_insn (gen_movt (result, get_t_reg_rtx ()));
12526   else if ((code == EQ && val == 0) || (code == NE && val == 1))
12527     emit_insn (gen_movnegt (result, get_t_reg_rtx ()));
12528   else if (code == EQ || code == NE)
12529     emit_insn (gen_move_insn (result, GEN_INT (code == NE)));
12530   else
12531     return false;
12532   if (result != target)
12533     emit_move_insn (target, result);
12534   return true;
12535 }
12536
12537 /* INSN is an sfunc; return the rtx that describes the address used.  */
12538 static rtx
12539 extract_sfunc_addr (rtx insn)
12540 {
12541   rtx pattern, part = NULL_RTX;
12542   int len, i;
12543
12544   pattern = PATTERN (insn);
12545   len = XVECLEN (pattern, 0);
12546   for (i = 0; i < len; i++)
12547     {
12548       part = XVECEXP (pattern, 0, i);
12549       if (GET_CODE (part) == USE && GET_MODE (XEXP (part, 0)) == Pmode
12550           && GENERAL_REGISTER_P (true_regnum (XEXP (part, 0))))
12551         return XEXP (part, 0);
12552     }
12553   gcc_assert (GET_CODE (XVECEXP (pattern, 0, 0)) == UNSPEC_VOLATILE);
12554   return XVECEXP (XVECEXP (pattern, 0, 0), 0, 1);
12555 }
12556
12557 /* Verify that the register in use_sfunc_addr still agrees with the address
12558    used in the sfunc.  This prevents fill_slots_from_thread from changing
12559    use_sfunc_addr.
12560    INSN is the use_sfunc_addr instruction, and REG is the register it
12561    guards.  */
12562 bool
12563 check_use_sfunc_addr (rtx insn, rtx reg)
12564 {
12565   /* Search for the sfunc.  It should really come right after INSN.  */
12566   while ((insn = NEXT_INSN (insn)))
12567     {
12568       if (LABEL_P (insn) || JUMP_P (insn))
12569         break;
12570       if (! INSN_P (insn))
12571         continue;
12572
12573       if (GET_CODE (PATTERN (insn)) == SEQUENCE)
12574         insn = XVECEXP (PATTERN (insn), 0, 0);
12575       if (GET_CODE (PATTERN (insn)) != PARALLEL
12576           || get_attr_type (insn) != TYPE_SFUNC)
12577         continue;
12578       return rtx_equal_p (extract_sfunc_addr (insn), reg);
12579     }
12580   gcc_unreachable ();
12581 }
12582
12583 /* This function returns a constant rtx that represents 2**15 / pi in
12584    SFmode.  It's used to scale a fixed-point signed 16.16-bit fraction
12585    of a full circle back to an SFmode value, i.e. 0x10000 maps to 2*pi.  */
12586 static GTY(()) rtx sh_fsca_sf2int_rtx;
12587
12588 rtx
12589 sh_fsca_sf2int (void)
12590 {
12591   if (! sh_fsca_sf2int_rtx)
12592     {
12593       REAL_VALUE_TYPE rv;
12594
12595       real_from_string (&rv, "10430.378350470453");
12596       sh_fsca_sf2int_rtx = const_double_from_real_value (rv, SFmode);
12597     }
12598
12599   return sh_fsca_sf2int_rtx;
12600 }
12601
12602 /* This function returns a constant rtx that represents pi / 2**15 in
12603    SFmode.  It's used to scale SFmode angles, in radians, to a
12604    fixed-point signed 16.16-bit fraction of a full circle, i.e. 2*pi
12605    maps to 0x10000.  */
12606 static GTY(()) rtx sh_fsca_int2sf_rtx;
12607
12608 rtx
12609 sh_fsca_int2sf (void)
12610 {
12611   if (! sh_fsca_int2sf_rtx)
12612     {
12613       REAL_VALUE_TYPE rv;
12614
12615       real_from_string (&rv, "9.587379924285257e-5");
12616       sh_fsca_int2sf_rtx = const_double_from_real_value (rv, SFmode);
12617     }
12618
12619   return sh_fsca_int2sf_rtx;
12620 }
12621
12622 /* Initialize the CUMULATIVE_ARGS structure.  */
12623 void
12624 sh_init_cumulative_args (CUMULATIVE_ARGS *  pcum,
12625                          tree               fntype,
12626                          rtx                libname ATTRIBUTE_UNUSED,
12627                          tree               fndecl,
12628                          signed int         n_named_args,
12629                          enum machine_mode  mode)
12630 {
12631   pcum->arg_count [(int) SH_ARG_FLOAT] = 0;
12632   pcum->free_single_fp_reg = 0;
12633   pcum->stack_regs = 0;
12634   pcum->byref_regs = 0;
12635   pcum->byref = 0;
12636   pcum->outgoing = (n_named_args == -1) ? 0 : 1;
12637
12638   /* XXX - Should we check TARGET_HITACHI here ???  */
12639   pcum->renesas_abi = sh_attr_renesas_p (fntype) ? 1 : 0;
12640
12641   if (fntype)
12642     {
12643       pcum->force_mem = ((TARGET_HITACHI || pcum->renesas_abi)
12644                          && aggregate_value_p (TREE_TYPE (fntype), fndecl));
12645       pcum->prototype_p = prototype_p (fntype);
12646       pcum->arg_count [(int) SH_ARG_INT]
12647         = TARGET_SH5 && aggregate_value_p (TREE_TYPE (fntype), fndecl);
12648
12649       pcum->call_cookie
12650         = CALL_COOKIE_RET_TRAMP (TARGET_SHCOMPACT
12651                                  && pcum->arg_count [(int) SH_ARG_INT] == 0
12652                                  && (TYPE_MODE (TREE_TYPE (fntype)) == BLKmode
12653                                      ? int_size_in_bytes (TREE_TYPE (fntype))
12654                                      : GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (fntype)))) > 4
12655                                  && (BASE_RETURN_VALUE_REG (TYPE_MODE (TREE_TYPE (fntype)))
12656                                      == FIRST_RET_REG));
12657     }
12658   else
12659     {
12660       pcum->arg_count [(int) SH_ARG_INT] = 0;
12661       pcum->prototype_p = FALSE;
12662       if (mode != VOIDmode)
12663         {
12664           pcum->call_cookie =
12665             CALL_COOKIE_RET_TRAMP (TARGET_SHCOMPACT
12666                                    && GET_MODE_SIZE (mode) > 4
12667                                    && BASE_RETURN_VALUE_REG (mode) == FIRST_RET_REG);
12668
12669           /* If the default ABI is the Renesas ABI then all library
12670              calls must assume that the library will be using the
12671              Renesas ABI.  So if the function would return its result
12672              in memory then we must force the address of this memory
12673              block onto the stack.  Ideally we would like to call
12674              targetm.calls.return_in_memory() here but we do not have
12675              the TYPE or the FNDECL available so we synthesize the
12676              contents of that function as best we can.  */
12677           pcum->force_mem =
12678             (TARGET_DEFAULT & MASK_HITACHI)
12679             && (mode == BLKmode
12680                 || (GET_MODE_SIZE (mode) > 4
12681                     && !(mode == DFmode
12682                          && TARGET_FPU_DOUBLE)));
12683         }
12684       else
12685         {
12686           pcum->call_cookie = 0;
12687           pcum->force_mem = FALSE;
12688         }
12689     }
12690 }
12691
12692 /* Replace any occurrence of FROM(n) in X with TO(n).  The function does
12693    not enter into CONST_DOUBLE for the replace.
12694
12695    Note that copying is not done so X must not be shared unless all copies
12696    are to be modified.
12697
12698    This is like replace_rtx, except that we operate on N_REPLACEMENTS
12699    replacements simultaneously - FROM(n) is replacements[n*2] and to(n) is
12700    replacements[n*2+1] - and that we take mode changes into account.
12701
12702    If a replacement is ambiguous, return NULL_RTX.
12703
12704    If MODIFY is zero, don't modify any rtl in place,
12705    just return zero or nonzero for failure / success.  */
12706 rtx
12707 replace_n_hard_rtx (rtx x, rtx *replacements, int n_replacements, int modify)
12708 {
12709   int i, j;
12710   const char *fmt;
12711
12712   /* The following prevents loops occurrence when we change MEM in
12713      CONST_DOUBLE onto the same CONST_DOUBLE.  */
12714   if (x != NULL_RTX && GET_CODE (x) == CONST_DOUBLE)
12715     return x;
12716
12717   for (i = n_replacements - 1; i >= 0 ; i--)
12718   if (x == replacements[i*2] && GET_MODE (x) == GET_MODE (replacements[i*2+1]))
12719     return replacements[i*2+1];
12720
12721   /* Allow this function to make replacements in EXPR_LISTs.  */
12722   if (x == NULL_RTX)
12723     return NULL_RTX;
12724
12725   if (GET_CODE (x) == SUBREG)
12726     {
12727       rtx new_rtx = replace_n_hard_rtx (SUBREG_REG (x), replacements,
12728                                     n_replacements, modify);
12729
12730       if (CONST_INT_P (new_rtx))
12731         {
12732           x = simplify_subreg (GET_MODE (x), new_rtx,
12733                                GET_MODE (SUBREG_REG (x)),
12734                                SUBREG_BYTE (x));
12735           if (! x)
12736             abort ();
12737         }
12738       else if (modify)
12739         SUBREG_REG (x) = new_rtx;
12740
12741       return x;
12742     }
12743   else if (REG_P (x))
12744     {
12745       unsigned regno = REGNO (x);
12746       unsigned nregs = (regno < FIRST_PSEUDO_REGISTER
12747                         ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
12748       rtx result = NULL_RTX;
12749
12750       for (i = n_replacements - 1; i >= 0; i--)
12751         {
12752           rtx from = replacements[i*2];
12753           rtx to = replacements[i*2+1];
12754           unsigned from_regno, from_nregs, to_regno, new_regno;
12755
12756           if (!REG_P (from))
12757             continue;
12758           from_regno = REGNO (from);
12759           from_nregs = (from_regno < FIRST_PSEUDO_REGISTER
12760                         ? HARD_REGNO_NREGS (from_regno, GET_MODE (from)) : 1);
12761           if (regno < from_regno + from_nregs && regno + nregs > from_regno)
12762             {
12763               if (regno < from_regno
12764                   || regno + nregs > from_regno + nregs
12765                   || !REG_P (to)
12766                   || result)
12767                 return NULL_RTX;
12768               to_regno = REGNO (to);
12769               if (to_regno < FIRST_PSEUDO_REGISTER)
12770                 {
12771                   new_regno = regno + to_regno - from_regno;
12772                   if ((unsigned) HARD_REGNO_NREGS (new_regno, GET_MODE (x))
12773                       != nregs)
12774                     return NULL_RTX;
12775                   result = gen_rtx_REG (GET_MODE (x), new_regno);
12776                 }
12777               else if (GET_MODE (x) <= GET_MODE (to))
12778                 result = gen_lowpart_common (GET_MODE (x), to);
12779               else
12780                 result = gen_lowpart_SUBREG (GET_MODE (x), to);
12781             }
12782         }
12783       return result ? result : x;
12784     }
12785   else if (GET_CODE (x) == ZERO_EXTEND)
12786     {
12787       rtx new_rtx = replace_n_hard_rtx (XEXP (x, 0), replacements,
12788                                     n_replacements, modify);
12789
12790       if (CONST_INT_P (new_rtx))
12791         {
12792           x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
12793                                         new_rtx, GET_MODE (XEXP (x, 0)));
12794           if (! x)
12795             abort ();
12796         }
12797       else if (modify)
12798         XEXP (x, 0) = new_rtx;
12799
12800       return x;
12801     }
12802
12803   fmt = GET_RTX_FORMAT (GET_CODE (x));
12804   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
12805     {
12806       rtx new_rtx;
12807
12808       if (fmt[i] == 'e')
12809         {
12810           new_rtx = replace_n_hard_rtx (XEXP (x, i), replacements,
12811                                     n_replacements, modify);
12812           if (!new_rtx)
12813             return NULL_RTX;
12814           if (modify)
12815             XEXP (x, i) = new_rtx;
12816         }
12817       else if (fmt[i] == 'E')
12818         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12819           {
12820             new_rtx = replace_n_hard_rtx (XVECEXP (x, i, j), replacements,
12821                                       n_replacements, modify);
12822           if (!new_rtx)
12823             return NULL_RTX;
12824             if (modify)
12825               XVECEXP (x, i, j) = new_rtx;
12826           }
12827     }
12828
12829   return x;
12830 }
12831
12832 rtx
12833 sh_gen_truncate (enum machine_mode mode, rtx x, int need_sign_ext)
12834 {
12835   enum rtx_code code = TRUNCATE;
12836
12837   if (GET_CODE (x) == ZERO_EXTEND || GET_CODE (x) == SIGN_EXTEND)
12838     {
12839       rtx inner = XEXP (x, 0);
12840       enum machine_mode inner_mode = GET_MODE (inner);
12841
12842       if (inner_mode == mode)
12843         return inner;
12844       else if (GET_MODE_SIZE (inner_mode) >= GET_MODE_SIZE (mode))
12845         x = inner;
12846       else if (GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (mode)
12847                && (! need_sign_ext || GET_CODE (x) == SIGN_EXTEND))
12848         {
12849           code = GET_CODE (x);
12850           x = inner;
12851         }
12852     }
12853   return gen_rtx_fmt_e (code, mode, x);
12854 }
12855
12856 /* Called via for_each_rtx after reload, to clean up truncates of
12857    registers that span multiple actual hard registers.  */
12858 int
12859 shmedia_cleanup_truncate (rtx *p, void *n_changes)
12860 {
12861   rtx x = *p, reg;
12862
12863   if (GET_CODE (x) != TRUNCATE)
12864     return 0;
12865   reg = XEXP (x, 0);
12866   if (GET_MODE_SIZE (GET_MODE (reg)) > 8 && REG_P (reg))
12867     {
12868       enum machine_mode reg_mode = GET_MODE (reg);
12869       XEXP (x, 0) = simplify_subreg (DImode, reg, reg_mode,
12870                                      subreg_lowpart_offset (DImode, reg_mode));
12871       *(int*) n_changes += 1;
12872       return -1;
12873     }
12874   return 0;
12875 }
12876
12877 /* Load and store depend on the highpart of the address.  However,
12878    set_attr_alternative does not give well-defined results before reload,
12879    so we must look at the rtl ourselves to see if any of the feeding
12880    registers is used in a memref.
12881
12882    Called by sh_contains_memref_p via for_each_rtx.  */
12883 static int
12884 sh_contains_memref_p_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
12885 {
12886   return (MEM_P (*loc));
12887 }
12888
12889 /* Return true iff INSN contains a MEM.  */
12890 bool
12891 sh_contains_memref_p (rtx insn)
12892 {
12893   return for_each_rtx (&PATTERN (insn), &sh_contains_memref_p_1, NULL);
12894 }
12895
12896 /* Return true iff INSN loads a banked register.  */
12897 bool
12898 sh_loads_bankedreg_p (rtx insn)
12899 {
12900   if (GET_CODE (PATTERN (insn)) == SET)
12901     {
12902       rtx op = SET_DEST (PATTERN(insn));
12903       if (REG_P (op) && BANKED_REGISTER_P (REGNO (op)))
12904         return true;
12905     }
12906
12907   return false;
12908 }
12909
12910 /* FNADDR is the MEM expression from a call expander.  Return an address
12911    to use in an SHmedia insn pattern.  */
12912 rtx
12913 shmedia_prepare_call_address (rtx fnaddr, int is_sibcall)
12914 {
12915   int is_sym;
12916
12917   fnaddr = XEXP (fnaddr, 0);
12918   is_sym = GET_CODE (fnaddr) == SYMBOL_REF;
12919   if (flag_pic && is_sym)
12920     {
12921       if (! SYMBOL_REF_LOCAL_P (fnaddr))
12922         {
12923           rtx reg = gen_reg_rtx (Pmode);
12924
12925           /* We must not use GOTPLT for sibcalls, because PIC_REG
12926              must be restored before the PLT code gets to run.  */
12927           if (is_sibcall)
12928             emit_insn (gen_symGOT2reg (reg, fnaddr));
12929           else
12930             emit_insn (gen_symGOTPLT2reg (reg, fnaddr));
12931           fnaddr = reg;
12932         }
12933       else
12934         {
12935           fnaddr = gen_sym2PIC (fnaddr);
12936           PUT_MODE (fnaddr, Pmode);
12937         }
12938     }
12939   /* If ptabs might trap, make this visible to the rest of the compiler.
12940      We generally assume that symbols pertain to valid locations, but
12941      it is possible to generate invalid symbols with asm or linker tricks.
12942      In a list of functions where each returns its successor, an invalid
12943      symbol might denote an empty list.  */
12944   if (!TARGET_PT_FIXED
12945       && (!is_sym || TARGET_INVALID_SYMBOLS)
12946       && (!REG_P (fnaddr) || ! TARGET_REGISTER_P (REGNO (fnaddr))))
12947     {
12948       rtx tr = gen_reg_rtx (PDImode);
12949
12950       emit_insn (gen_ptabs (tr, fnaddr));
12951       fnaddr = tr;
12952     }
12953   else if (! target_reg_operand (fnaddr, Pmode))
12954     fnaddr = copy_to_mode_reg (Pmode, fnaddr);
12955   return fnaddr;
12956 }
12957
12958 /* Implement TARGET_PREFERRED_RELOAD_CLASS.  */
12959 static reg_class_t
12960 sh_preferred_reload_class (rtx x, reg_class_t rclass)
12961 {
12962   if (rclass == NO_REGS
12963       && TARGET_SHMEDIA
12964       && (CONST_DOUBLE_P (x)
12965           || GET_CODE (x) == SYMBOL_REF
12966           || PIC_ADDR_P (x)))
12967     return GENERAL_REGS;
12968
12969   return rclass;
12970 }
12971
12972 /* Implement TARGET_SECONDARY_RELOAD.  */
12973 static reg_class_t
12974 sh_secondary_reload (bool in_p, rtx x, reg_class_t rclass_i,
12975                      enum machine_mode mode, secondary_reload_info *sri)
12976 {
12977   enum reg_class rclass = (enum reg_class) rclass_i;
12978
12979   if (MEM_P (x) && GET_CODE (XEXP (x, 0)) == PLUS
12980       && REG_P (XEXP (XEXP (x, 0), 0))
12981       && REGNO (XEXP (XEXP (x, 0), 0)) == GBR_REG)
12982     return rclass == R0_REGS ? NO_REGS : R0_REGS;
12983
12984   if (MEM_P (x) && REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) == GBR_REG)
12985     return rclass == R0_REGS ? NO_REGS : R0_REGS;
12986
12987   if (REG_P (x) && REGNO (x) == GBR_REG)
12988     return NO_REGS;
12989
12990   if (in_p)
12991     {
12992       if (REGCLASS_HAS_FP_REG (rclass)
12993           && ! TARGET_SHMEDIA
12994           && immediate_operand ((x), mode)
12995           && ! ((fp_zero_operand (x) || fp_one_operand (x))
12996                 && mode == SFmode && fldi_ok ()))
12997         switch (mode)
12998           {
12999           case SFmode:
13000             sri->icode = CODE_FOR_reload_insf__frn;
13001             return NO_REGS;
13002           case DFmode:
13003             sri->icode = CODE_FOR_reload_indf__frn;
13004             return NO_REGS;
13005           case SImode:
13006             /* ??? If we knew that we are in the appropriate mode -
13007                single precision - we could use a reload pattern directly.  */
13008             return FPUL_REGS;
13009           default:
13010             abort ();
13011           }
13012       if (rclass == FPUL_REGS
13013           && ((REG_P (x) && (REGNO (x) == MACL_REG || REGNO (x) == MACH_REG
13014                              || REGNO (x) == T_REG))
13015               || GET_CODE (x) == PLUS))
13016         return GENERAL_REGS;
13017       if (rclass == FPUL_REGS && immediate_operand (x, mode))
13018         {
13019           if (satisfies_constraint_I08 (x) || fp_zero_operand (x))
13020             return GENERAL_REGS;
13021           else if (mode == SFmode)
13022             return FP_REGS;
13023           sri->icode = CODE_FOR_reload_insi__i_fpul;
13024           return NO_REGS;
13025         }
13026       if (rclass == FPSCR_REGS
13027           && ((REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER)
13028               || (MEM_P (x) && GET_CODE (XEXP (x, 0)) == PLUS)))
13029         return GENERAL_REGS;
13030       if (REGCLASS_HAS_FP_REG (rclass)
13031           && TARGET_SHMEDIA
13032           && immediate_operand (x, mode)
13033           && x != CONST0_RTX (GET_MODE (x))
13034           && GET_MODE (x) != V4SFmode)
13035         return GENERAL_REGS;
13036       if ((mode == QImode || mode == HImode)
13037           && TARGET_SHMEDIA && inqhi_operand (x, mode))
13038         {
13039           sri->icode = ((mode == QImode)
13040                         ? CODE_FOR_reload_inqi : CODE_FOR_reload_inhi);
13041           return NO_REGS;
13042         }
13043       if (TARGET_SHMEDIA && rclass == GENERAL_REGS
13044           && (GET_CODE (x) == LABEL_REF || PIC_ADDR_P (x)))
13045         return TARGET_REGS;
13046     } /* end of input-only processing.  */
13047
13048   if (((REGCLASS_HAS_FP_REG (rclass)
13049         && (REG_P (x)
13050             && (GENERAL_OR_AP_REGISTER_P (REGNO (x))
13051                 || (FP_REGISTER_P (REGNO (x)) && mode == SImode
13052                     && TARGET_FMOVD))))
13053        || (REGCLASS_HAS_GENERAL_REG (rclass)
13054            && REG_P (x)
13055            && FP_REGISTER_P (REGNO (x))))
13056       && ! TARGET_SHMEDIA
13057       && (mode == SFmode || mode == SImode))
13058     return FPUL_REGS;
13059   if ((rclass == FPUL_REGS
13060        || (REGCLASS_HAS_FP_REG (rclass)
13061            && ! TARGET_SHMEDIA && mode == SImode))
13062       && (MEM_P (x)
13063           || (REG_P (x)
13064               && (REGNO (x) >= FIRST_PSEUDO_REGISTER
13065                   || REGNO (x) == T_REG
13066                   || system_reg_operand (x, VOIDmode)))))
13067     {
13068       if (rclass == FPUL_REGS)
13069         return GENERAL_REGS;
13070       return FPUL_REGS;
13071     }
13072   if ((rclass == TARGET_REGS
13073        || (TARGET_SHMEDIA && rclass == SIBCALL_REGS))
13074       && !satisfies_constraint_Csy (x)
13075       && (!REG_P (x) || ! GENERAL_REGISTER_P (REGNO (x))))
13076     return GENERAL_REGS;
13077   if ((rclass == MAC_REGS || rclass == PR_REGS)
13078       && REG_P (x) && ! GENERAL_REGISTER_P (REGNO (x))
13079       && rclass != REGNO_REG_CLASS (REGNO (x)))
13080     return GENERAL_REGS;
13081   if (rclass != GENERAL_REGS && REG_P (x)
13082       && TARGET_REGISTER_P (REGNO (x)))
13083     return GENERAL_REGS;
13084
13085  /* If here fall back to loading FPUL register through general registers.
13086     This case can happen when movsi_ie insn is picked initially to
13087     load/store the FPUL register from/to another register, and then the
13088     other register is allocated on the stack.  */
13089   if (rclass == FPUL_REGS && true_regnum (x) == -1)
13090     return GENERAL_REGS;
13091
13092   /* Force mov.b / mov.w displacement addressing insn to use R0 as
13093      the other operand.
13094      On SH2A could also just leave it alone here, which would result in a
13095      4 byte move insn being generated instead.  However, for this to work
13096      the insns must have the appropriate alternatives.  */
13097   if ((mode == QImode || mode == HImode) && rclass != R0_REGS
13098       && satisfies_constraint_Sdd (x)
13099       && disp_addr_displacement (x) <= max_mov_insn_displacement (mode, false))
13100     return R0_REGS;
13101
13102   /* When reload is trying to address a QImode or HImode subreg on the stack, 
13103      force any subreg byte into R0_REGS, as this is going to become a
13104      displacement address.
13105      We could restrict this to SUBREG_BYTE (x) > 0, but if the actual reg
13106      is on the stack, the memref to it might already require a displacement
13107      and that has to be added to the final address.  At this point we don't
13108      know the cumulative displacement so we assume the worst case.  */
13109   if ((mode == QImode || mode == HImode) && rclass != R0_REGS 
13110       && GET_CODE (x) == SUBREG && true_regnum (x) == -1)
13111     return R0_REGS;
13112
13113   return NO_REGS;
13114 }
13115
13116 static void
13117 sh_conditional_register_usage (void)
13118 {
13119   int regno;
13120   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno ++)
13121     if (! VALID_REGISTER_P (regno))
13122       fixed_regs[regno] = call_used_regs[regno] = 1;
13123   /* R8 and R9 are call-clobbered on SH5, but not on earlier SH ABIs.  */
13124   if (TARGET_SH5)
13125     {
13126       call_used_regs[FIRST_GENERAL_REG + 8]
13127         = call_used_regs[FIRST_GENERAL_REG + 9] = 1;
13128       call_really_used_regs[FIRST_GENERAL_REG + 8]
13129         = call_really_used_regs[FIRST_GENERAL_REG + 9] = 1;
13130     }
13131   if (TARGET_SHMEDIA)
13132     {
13133       regno_reg_class[FIRST_GENERAL_REG] = GENERAL_REGS;
13134       CLEAR_HARD_REG_SET (reg_class_contents[FP0_REGS]);
13135       regno_reg_class[FIRST_FP_REG] = FP_REGS;
13136     }
13137   if (flag_pic)
13138     {
13139       fixed_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
13140       call_used_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
13141     }
13142   /* Renesas saves and restores mac registers on call.  */
13143   if (TARGET_HITACHI && ! TARGET_NOMACSAVE)
13144     {
13145       call_really_used_regs[MACH_REG] = 0;
13146       call_really_used_regs[MACL_REG] = 0;
13147     }
13148
13149   if (TARGET_SHMEDIA)
13150     {
13151       for (regno = FIRST_TARGET_REG; regno <= LAST_TARGET_REG; regno ++)
13152         if (! fixed_regs[regno] && call_really_used_regs[regno])
13153           SET_HARD_REG_BIT (reg_class_contents[SIBCALL_REGS], regno);
13154     }
13155   else
13156     for (regno = FIRST_GENERAL_REG; regno <= LAST_GENERAL_REG; regno++)
13157       if (! fixed_regs[regno] && call_really_used_regs[regno])
13158         SET_HARD_REG_BIT (reg_class_contents[SIBCALL_REGS], regno);
13159 }
13160
13161 /* Implement TARGET_LEGITIMATE_CONSTANT_P
13162
13163    can_store_by_pieces constructs VOIDmode CONST_DOUBLEs.  */
13164 static bool
13165 sh_legitimate_constant_p (enum machine_mode mode, rtx x)
13166 {
13167   return (TARGET_SHMEDIA
13168           ? ((mode != DFmode && GET_MODE_CLASS (mode) != MODE_VECTOR_FLOAT)
13169              || x == CONST0_RTX (mode)
13170              || !TARGET_SHMEDIA_FPU
13171              || TARGET_SHMEDIA64)
13172           : (GET_CODE (x) != CONST_DOUBLE
13173              || mode == DFmode || mode == SFmode
13174              || mode == DImode || GET_MODE (x) == VOIDmode));
13175 }
13176
13177 enum sh_divide_strategy_e sh_div_strategy = SH_DIV_STRATEGY_DEFAULT;
13178
13179 static void
13180 sh_init_sync_libfuncs (void)
13181 {
13182   init_sync_libfuncs (UNITS_PER_WORD);
13183 }
13184
13185 /* Return true if it is appropriate to emit `ret' instructions in the
13186    body of a function.  */
13187 bool
13188 sh_can_use_simple_return_p (void)
13189 {
13190   HARD_REG_SET live_regs_mask;
13191   int d;
13192
13193   /* Some targets require special return insns.  */
13194   if (TARGET_SHMEDIA
13195       || (TARGET_SHCOMPACT
13196           && (crtl->args.info.call_cookie & CALL_COOKIE_RET_TRAMP (1))))
13197     return false;
13198
13199   if (! reload_completed || frame_pointer_needed)
13200     return false;
13201
13202   /* Moving prologue around does't reduce the size.  */
13203   if (optimize_function_for_size_p (cfun))
13204     return false;
13205
13206   /* Finally, allow for pr save.  */
13207   d = calc_live_regs (&live_regs_mask);
13208
13209   if (rounded_frame_size (d) > 4)
13210    return false;
13211
13212   return true;
13213 }
13214
13215 /*------------------------------------------------------------------------------
13216   Address mode optimization support code
13217 */
13218
13219 typedef HOST_WIDE_INT disp_t;
13220 static const disp_t MIN_DISP = HOST_WIDE_INT_MIN;
13221 static const disp_t MAX_DISP = HOST_WIDE_INT_MAX;
13222 static const disp_t INVALID_DISP = MAX_DISP;
13223
13224 /* A memory reference which is described by a base register and a
13225    displacement.  */
13226 class base_reg_disp
13227 {
13228 public:
13229   base_reg_disp (rtx br, disp_t d);
13230
13231   bool is_reg (void) const;
13232   bool is_disp (void) const;
13233   rtx reg (void) const;
13234   disp_t disp (void) const;
13235
13236 private:
13237   rtx reg_;
13238   disp_t disp_;
13239 };
13240
13241 inline
13242 base_reg_disp::base_reg_disp (rtx br, disp_t d)
13243 : reg_ (br), disp_ (d)
13244 {
13245 }
13246  
13247 inline bool
13248 base_reg_disp::is_reg (void) const
13249 {
13250   return reg_ != NULL_RTX && disp_ != INVALID_DISP;
13251 }
13252
13253 inline bool
13254 base_reg_disp::is_disp (void) const
13255 {
13256   return reg_ == NULL_RTX && disp_ != INVALID_DISP;
13257 }
13258
13259 inline rtx
13260 base_reg_disp::reg (void) const
13261 {
13262   return reg_;
13263 }
13264
13265 inline disp_t
13266 base_reg_disp::disp (void) const
13267 {
13268   return disp_;
13269 }
13270
13271 /* Find the base register and calculate the displacement for a given
13272    address rtx 'x'.
13273    This is done by walking the insn list backwards and following SET insns
13274    that set the value of the specified reg 'x'.  */
13275 static base_reg_disp
13276 sh_find_base_reg_disp (rtx insn, rtx x, disp_t disp = 0, rtx base_reg = NULL)
13277 {
13278   if (REG_P (x))
13279     {
13280       if (REGNO (x) == GBR_REG)
13281         return base_reg_disp (x, disp);
13282
13283       /* We've reached a hard-reg.  This is probably the point where
13284          function args are copied to pseudos.  Do not go any further and
13285          stick to the pseudo.  If the original mem addr was in a hard reg
13286          from the beginning, it will become the base reg.  */
13287       if (REGNO (x) < FIRST_PSEUDO_REGISTER)
13288         return base_reg_disp (base_reg != NULL ? base_reg : x, disp);
13289
13290       /* Try to find the previous insn that sets the reg.  */
13291       for (rtx i = prev_nonnote_insn (insn); i != NULL;
13292            i = prev_nonnote_insn (i))
13293         {
13294           if (REGNO_REG_SET_P (regs_invalidated_by_call_regset, GBR_REG)
13295               && CALL_P (i))
13296             break;
13297
13298           if (!NONJUMP_INSN_P (i))
13299             continue;
13300
13301           rtx p = PATTERN (i);
13302           if (p != NULL && GET_CODE (p) == SET && REG_P (XEXP (p, 0))
13303               && REGNO (XEXP (p, 0)) == REGNO (x))
13304             {
13305               /* If the recursion can't find out any more details about the
13306                  source of the set, then this reg becomes our new base reg.  */
13307               return sh_find_base_reg_disp (i, XEXP (p, 1), disp, XEXP (p, 0));
13308             }
13309         }
13310
13311     /* When here, no previous insn was found that sets the reg.
13312        The input reg is already the base reg.  */
13313     return base_reg_disp (x, disp);
13314   }
13315
13316   else if (GET_CODE (x) == PLUS)
13317     {
13318       base_reg_disp left_val = sh_find_base_reg_disp (insn, XEXP (x, 0));
13319       base_reg_disp right_val = sh_find_base_reg_disp (insn, XEXP (x, 1));
13320
13321       /* Either left or right val must be a reg.
13322          We don't handle the case of 'reg + reg' here.  */
13323       if (left_val.is_reg () && right_val.is_disp ())
13324         return base_reg_disp (left_val.reg (), left_val.disp ()
13325                                                + right_val.disp () + disp);
13326       else if (right_val.is_reg () && left_val.is_disp ())
13327         return base_reg_disp (right_val.reg (), right_val.disp ()
13328                                                 + left_val.disp () + disp);
13329       else
13330         return base_reg_disp (base_reg, disp);
13331     }
13332
13333   else if (CONST_INT_P (x))
13334     return base_reg_disp (NULL, disp + INTVAL (x));
13335
13336   /* Didn't find anything useful.  */
13337   return base_reg_disp (base_reg, disp);
13338 }
13339
13340 /* Given an insn and a memory operand, try to find an equivalent GBR
13341    based memory address and return the corresponding new memory address.
13342    Return NULL_RTX if not found.  */
13343 rtx
13344 sh_find_equiv_gbr_addr (rtx insn, rtx mem)
13345 {
13346   if (!MEM_P (mem))
13347     return NULL_RTX;
13348
13349   /* Leave post/pre inc/dec or any other side effect addresses alone.  */
13350   if (side_effects_p (XEXP (mem, 0)))
13351     return NULL_RTX;
13352
13353   base_reg_disp gbr_disp = sh_find_base_reg_disp (insn, XEXP (mem, 0));
13354
13355   if (gbr_disp.is_reg () && REGNO (gbr_disp.reg ()) == GBR_REG)
13356     {
13357       rtx disp = GEN_INT (gbr_disp.disp ());
13358       if (gbr_displacement (disp, GET_MODE (mem)))
13359         return gen_rtx_PLUS (SImode, gen_rtx_REG (SImode, GBR_REG), disp);
13360     }
13361
13362   return NULL_RTX;
13363 }
13364
13365 /*------------------------------------------------------------------------------
13366   Manual insn combine support code.
13367 */
13368
13369 /* Given a reg rtx and a start insn, try to find the insn that sets the
13370    specified reg by using the specified insn stepping function, such as 
13371    'prev_nonnote_insn_bb'.  When the insn is found, try to extract the rtx
13372    of the reg set.  */
13373 set_of_reg
13374 sh_find_set_of_reg (rtx reg, rtx insn, rtx(*stepfunc)(rtx))
13375 {
13376   set_of_reg result;
13377   result.insn = insn;
13378   result.set_rtx = NULL_RTX;
13379   result.set_src = NULL_RTX;
13380
13381   if (!REG_P (reg) || insn == NULL_RTX)
13382     return result;
13383
13384   for (result.insn = stepfunc (insn); result.insn != NULL_RTX;
13385        result.insn = stepfunc (result.insn))
13386     {
13387       if (BARRIER_P (result.insn))
13388         return result;
13389       if (!NONJUMP_INSN_P (result.insn))
13390         continue;
13391       if (reg_set_p (reg, result.insn))
13392         {
13393           result.set_rtx = set_of (reg, result.insn);
13394
13395           if (result.set_rtx == NULL_RTX || GET_CODE (result.set_rtx) != SET)
13396             return result;
13397
13398           result.set_src = XEXP (result.set_rtx, 1);
13399           return result;
13400         }
13401     }
13402
13403   return result;
13404 }
13405
13406 /* Given an op rtx and an insn, try to find out whether the result of the
13407    specified op consists only of logical operations on T bit stores.  */
13408 bool
13409 sh_is_logical_t_store_expr (rtx op, rtx insn)
13410 {
13411   if (!logical_operator (op, SImode))
13412     return false;
13413
13414   rtx ops[2] = { XEXP (op, 0), XEXP (op, 1) };
13415   int op_is_t_count = 0;
13416
13417   for (int i = 0; i < 2; ++i)
13418     {
13419       if (t_reg_operand (ops[i], VOIDmode)
13420           || negt_reg_operand (ops[i], VOIDmode))
13421         op_is_t_count++;
13422
13423       else
13424         {
13425           set_of_reg op_set = sh_find_set_of_reg (ops[i], insn,
13426                                                   prev_nonnote_insn_bb);
13427           if (op_set.set_src == NULL_RTX)
13428             continue;
13429
13430           if (t_reg_operand (op_set.set_src, VOIDmode)
13431               || negt_reg_operand (op_set.set_src, VOIDmode)
13432               || sh_is_logical_t_store_expr (op_set.set_src, op_set.insn))
13433               op_is_t_count++;
13434         }
13435     }
13436   
13437   return op_is_t_count == 2;
13438 }
13439
13440 /* Given the operand that is extended in a sign/zero extend insn, and the
13441    insn, try to figure out whether the sign/zero extension can be replaced
13442    by a simple reg-reg copy.  If so, the replacement reg rtx is returned,
13443    NULL_RTX otherwise.  */
13444 rtx
13445 sh_try_omit_signzero_extend (rtx extended_op, rtx insn)
13446 {
13447   if (REG_P (extended_op))
13448     extended_op = extended_op;
13449   else if (GET_CODE (extended_op) == SUBREG && REG_P (SUBREG_REG (extended_op)))
13450     extended_op = SUBREG_REG (extended_op);
13451   else
13452     return NULL_RTX;
13453
13454   /* Reg moves must be of the same mode.  */
13455   if (GET_MODE (extended_op) != SImode)
13456     return NULL_RTX;
13457
13458   set_of_reg s = sh_find_set_of_reg (extended_op, insn, prev_nonnote_insn_bb);
13459   if (s.set_src == NULL_RTX)
13460     return NULL_RTX;
13461
13462   if (t_reg_operand (s.set_src, VOIDmode)
13463       || negt_reg_operand (s.set_src, VOIDmode))
13464     return extended_op;
13465
13466   /* If the zero extended reg was formed by a logical operation, check the
13467      operands of the logical operation.  If both originated from T bit
13468      stores the zero extension can be eliminated.  */
13469   else if (sh_is_logical_t_store_expr (s.set_src, s.insn))
13470     return extended_op;
13471
13472   return NULL_RTX;
13473 }
13474
13475 #include "gt-sh.h"