OSDN Git Service

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