gcc/ChangeLog:
[official-gcc.git] / gcc / config / sh / sh.c
blob010e5dcadc3498b89d44cc5faf92034463a78e36
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).
7 This file is part of GCC.
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.
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.
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. */
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 "tm_p.h"
44 #include "target.h"
45 #include "target-def.h"
46 #include "real.h"
47 #include "langhooks.h"
48 #include "basic-block.h"
49 #include "ra.h"
50 #include "cfglayout.h"
51 #include "intl.h"
52 #include "sched-int.h"
53 #include "ggc.h"
54 #include "tree-gimple.h"
57 int code_for_indirect_jump_scratch = CODE_FOR_indirect_jump_scratch;
59 #define MSW (TARGET_LITTLE_ENDIAN ? 1 : 0)
60 #define LSW (TARGET_LITTLE_ENDIAN ? 0 : 1)
62 /* These are some macros to abstract register modes. */
63 #define CONST_OK_FOR_ADD(size) \
64 (TARGET_SHMEDIA ? CONST_OK_FOR_I10 (size) : CONST_OK_FOR_I08 (size))
65 #define GEN_MOV (*(TARGET_SHMEDIA64 ? gen_movdi : gen_movsi))
66 #define GEN_ADD3 (*(TARGET_SHMEDIA64 ? gen_adddi3 : gen_addsi3))
67 #define GEN_SUB3 (*(TARGET_SHMEDIA64 ? gen_subdi3 : gen_subsi3))
69 /* Set to 1 by expand_prologue() when the function is an interrupt handler. */
70 int current_function_interrupt;
72 /* ??? The pragma interrupt support will not work for SH3. */
73 /* This is set by #pragma interrupt and #pragma trapa, and causes gcc to
74 output code for the next function appropriate for an interrupt handler. */
75 int pragma_interrupt;
77 /* This is set by the trap_exit attribute for functions. It specifies
78 a trap number to be used in a trapa instruction at function exit
79 (instead of an rte instruction). */
80 int trap_exit;
82 /* This is used by the sp_switch attribute for functions. It specifies
83 a variable holding the address of the stack the interrupt function
84 should switch to/from at entry/exit. */
85 rtx sp_switch;
87 /* This is set by #pragma trapa, and is similar to the above, except that
88 the compiler doesn't emit code to preserve all registers. */
89 static int pragma_trapa;
91 /* This is set by #pragma nosave_low_regs. This is useful on the SH3,
92 which has a separate set of low regs for User and Supervisor modes.
93 This should only be used for the lowest level of interrupts. Higher levels
94 of interrupts must save the registers in case they themselves are
95 interrupted. */
96 int pragma_nosave_low_regs;
98 /* This is used for communication between TARGET_SETUP_INCOMING_VARARGS and
99 sh_expand_prologue. */
100 int current_function_anonymous_args;
102 /* Global variables for machine-dependent things. */
104 /* Which cpu are we scheduling for. */
105 enum processor_type sh_cpu;
107 /* Definitions used in ready queue reordering for first scheduling pass. */
109 /* Reg weights arrays for modes SFmode and SImode, indexed by insn LUID. */
110 static short *regmode_weight[2];
112 /* Total SFmode and SImode weights of scheduled insns. */
113 static int curr_regmode_pressure[2];
115 /* If true, skip cycles for Q -> R movement. */
116 static int skip_cycles = 0;
118 /* Cached value of can_issue_more. This is cached in sh_variable_issue hook
119 and returned from sh_reorder2. */
120 static short cached_can_issue_more;
122 /* Saved operands from the last compare to use when we generate an scc
123 or bcc insn. */
125 rtx sh_compare_op0;
126 rtx sh_compare_op1;
128 /* Provides the class number of the smallest class containing
129 reg number. */
131 enum reg_class regno_reg_class[FIRST_PSEUDO_REGISTER] =
133 R0_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
134 GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
135 GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
136 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 FP0_REGS,FP_REGS, FP_REGS, FP_REGS,
150 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
151 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
152 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 TARGET_REGS, TARGET_REGS, TARGET_REGS, TARGET_REGS,
166 TARGET_REGS, TARGET_REGS, TARGET_REGS, TARGET_REGS,
167 DF_REGS, DF_REGS, DF_REGS, DF_REGS,
168 DF_REGS, DF_REGS, DF_REGS, DF_REGS,
169 NO_REGS, GENERAL_REGS, PR_REGS, T_REGS,
170 MAC_REGS, MAC_REGS, FPUL_REGS, FPSCR_REGS,
171 GENERAL_REGS,
174 char sh_register_names[FIRST_PSEUDO_REGISTER] \
175 [MAX_REGISTER_NAME_LENGTH + 1] = SH_REGISTER_NAMES_INITIALIZER;
177 char sh_additional_register_names[ADDREGNAMES_SIZE] \
178 [MAX_ADDITIONAL_REGISTER_NAME_LENGTH + 1]
179 = SH_ADDITIONAL_REGISTER_NAMES_INITIALIZER;
181 /* Provide reg_class from a letter such as appears in the machine
182 description. *: target independently reserved letter.
183 reg_class_from_letter['e' - 'a'] is set to NO_REGS for TARGET_FMOVD. */
185 enum reg_class reg_class_from_letter[] =
187 /* a */ ALL_REGS, /* b */ TARGET_REGS, /* c */ FPSCR_REGS, /* d */ DF_REGS,
188 /* e */ FP_REGS, /* f */ FP_REGS, /* g **/ NO_REGS, /* h */ NO_REGS,
189 /* i **/ NO_REGS, /* j */ NO_REGS, /* k */ SIBCALL_REGS, /* l */ PR_REGS,
190 /* m **/ NO_REGS, /* n **/ NO_REGS, /* o **/ NO_REGS, /* p **/ NO_REGS,
191 /* q */ NO_REGS, /* r **/ NO_REGS, /* s **/ NO_REGS, /* t */ T_REGS,
192 /* u */ NO_REGS, /* v */ NO_REGS, /* w */ FP0_REGS, /* x */ MAC_REGS,
193 /* y */ FPUL_REGS, /* z */ R0_REGS
196 int assembler_dialect;
198 static bool shmedia_space_reserved_for_target_registers;
200 static void split_branches (rtx);
201 static int branch_dest (rtx);
202 static void force_into (rtx, rtx);
203 static void print_slot (rtx);
204 static rtx add_constant (rtx, enum machine_mode, rtx);
205 static void dump_table (rtx, rtx);
206 static int hi_const (rtx);
207 static int broken_move (rtx);
208 static int mova_p (rtx);
209 static rtx find_barrier (int, rtx, rtx);
210 static int noncall_uses_reg (rtx, rtx, rtx *);
211 static rtx gen_block_redirect (rtx, int, int);
212 static void sh_reorg (void);
213 static void output_stack_adjust (int, rtx, int, HARD_REG_SET *);
214 static rtx frame_insn (rtx);
215 static rtx push (int);
216 static void pop (int);
217 static void push_regs (HARD_REG_SET *, int);
218 static int calc_live_regs (HARD_REG_SET *);
219 static void mark_use (rtx, rtx *);
220 static HOST_WIDE_INT rounded_frame_size (int);
221 static rtx mark_constant_pool_use (rtx);
222 const struct attribute_spec sh_attribute_table[];
223 static tree sh_handle_interrupt_handler_attribute (tree *, tree, tree, int, bool *);
224 static tree sh_handle_sp_switch_attribute (tree *, tree, tree, int, bool *);
225 static tree sh_handle_trap_exit_attribute (tree *, tree, tree, int, bool *);
226 static tree sh_handle_renesas_attribute (tree *, tree, tree, int, bool *);
227 static void sh_output_function_epilogue (FILE *, HOST_WIDE_INT);
228 static void sh_insert_attributes (tree, tree *);
229 static int sh_adjust_cost (rtx, rtx, rtx, int);
230 static int sh_issue_rate (void);
231 static int sh_dfa_new_cycle (FILE *, int, rtx, int, int, int *sort_p);
232 static short find_set_regmode_weight (rtx, enum machine_mode);
233 static short find_insn_regmode_weight (rtx, enum machine_mode);
234 static void find_regmode_weight (int, enum machine_mode);
235 static void sh_md_init_global (FILE *, int, int);
236 static void sh_md_finish_global (FILE *, int);
237 static int rank_for_reorder (const void *, const void *);
238 static void swap_reorder (rtx *, int);
239 static void ready_reorder (rtx *, int);
240 static short high_pressure (enum machine_mode);
241 static int sh_reorder (FILE *, int, rtx *, int *, int);
242 static int sh_reorder2 (FILE *, int, rtx *, int *, int);
243 static void sh_md_init (FILE *, int, int);
244 static int sh_variable_issue (FILE *, int, rtx, int);
246 static bool sh_function_ok_for_sibcall (tree, tree);
248 static bool sh_cannot_modify_jumps_p (void);
249 static int sh_target_reg_class (void);
250 static bool sh_optimize_target_register_callee_saved (bool);
251 static bool sh_ms_bitfield_layout_p (tree);
253 static void sh_init_builtins (void);
254 static void sh_media_init_builtins (void);
255 static rtx sh_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
256 static void sh_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT, tree);
257 static void sh_file_start (void);
258 static int flow_dependent_p (rtx, rtx);
259 static void flow_dependent_p_1 (rtx, rtx, void *);
260 static int shiftcosts (rtx);
261 static int andcosts (rtx);
262 static int addsubcosts (rtx);
263 static int multcosts (rtx);
264 static bool unspec_caller_rtx_p (rtx);
265 static bool sh_cannot_copy_insn_p (rtx);
266 static bool sh_rtx_costs (rtx, int, int, int *);
267 static int sh_address_cost (rtx);
268 static int shmedia_target_regs_stack_space (HARD_REG_SET *);
269 static int shmedia_reserve_space_for_target_registers_p (int, HARD_REG_SET *);
270 static int shmedia_target_regs_stack_adjust (HARD_REG_SET *);
271 static int scavenge_reg (HARD_REG_SET *s);
272 struct save_schedule_s;
273 static struct save_entry_s *sh5_schedule_saves (HARD_REG_SET *,
274 struct save_schedule_s *, int);
276 static rtx sh_struct_value_rtx (tree, int);
277 static bool sh_return_in_memory (tree, tree);
278 static rtx sh_builtin_saveregs (void);
279 static void sh_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode, tree, int *, int);
280 static bool sh_strict_argument_naming (CUMULATIVE_ARGS *);
281 static bool sh_pretend_outgoing_varargs_named (CUMULATIVE_ARGS *);
282 static tree sh_build_builtin_va_list (void);
283 static tree sh_gimplify_va_arg_expr (tree, tree, tree *, tree *);
284 static bool sh_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode,
285 tree, bool);
288 /* Initialize the GCC target structure. */
289 #undef TARGET_ATTRIBUTE_TABLE
290 #define TARGET_ATTRIBUTE_TABLE sh_attribute_table
292 /* The next two are used for debug info when compiling with -gdwarf. */
293 #undef TARGET_ASM_UNALIGNED_HI_OP
294 #define TARGET_ASM_UNALIGNED_HI_OP "\t.uaword\t"
295 #undef TARGET_ASM_UNALIGNED_SI_OP
296 #define TARGET_ASM_UNALIGNED_SI_OP "\t.ualong\t"
298 /* These are NULLed out on non-SH5 in OVERRIDE_OPTIONS. */
299 #undef TARGET_ASM_UNALIGNED_DI_OP
300 #define TARGET_ASM_UNALIGNED_DI_OP "\t.uaquad\t"
301 #undef TARGET_ASM_ALIGNED_DI_OP
302 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
304 #undef TARGET_ASM_FUNCTION_EPILOGUE
305 #define TARGET_ASM_FUNCTION_EPILOGUE sh_output_function_epilogue
307 #undef TARGET_ASM_OUTPUT_MI_THUNK
308 #define TARGET_ASM_OUTPUT_MI_THUNK sh_output_mi_thunk
310 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
311 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_tree_hwi_hwi_tree_true
313 #undef TARGET_ASM_FILE_START
314 #define TARGET_ASM_FILE_START sh_file_start
315 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
316 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
318 #undef TARGET_INSERT_ATTRIBUTES
319 #define TARGET_INSERT_ATTRIBUTES sh_insert_attributes
321 #undef TARGET_SCHED_ADJUST_COST
322 #define TARGET_SCHED_ADJUST_COST sh_adjust_cost
324 #undef TARGET_SCHED_ISSUE_RATE
325 #define TARGET_SCHED_ISSUE_RATE sh_issue_rate
327 /* The next 5 hooks have been implemented for reenabling sched1. With the
328 help of these macros we are limiting the movement of insns in sched1 to
329 reduce the register pressure. The overall idea is to keep count of SImode
330 and SFmode regs required by already scheduled insns. When these counts
331 cross some threshold values; give priority to insns that free registers.
332 The insn that frees registers is most likely to be the insn with lowest
333 LUID (original insn order); but such an insn might be there in the stalled
334 queue (Q) instead of the ready queue (R). To solve this, we skip cycles
335 upto a max of 8 cycles so that such insns may move from Q -> R.
337 The description of the hooks are as below:
339 TARGET_SCHED_INIT_GLOBAL: Added a new target hook in the generic
340 scheduler; it is called inside the sched_init function just after
341 find_insn_reg_weights function call. It is used to calculate the SImode
342 and SFmode weights of insns of basic blocks; much similar to what
343 find_insn_reg_weights does.
344 TARGET_SCHED_FINISH_GLOBAL: Corresponding cleanup hook.
346 TARGET_SCHED_DFA_NEW_CYCLE: Skip cycles if high register pressure is
347 indicated by TARGET_SCHED_REORDER2; doing this may move insns from
348 (Q)->(R).
350 TARGET_SCHED_REORDER: If the register pressure for SImode or SFmode is
351 high; reorder the ready queue so that the insn with lowest LUID will be
352 issued next.
354 TARGET_SCHED_REORDER2: If the register pressure is high, indicate to
355 TARGET_SCHED_DFA_NEW_CYCLE to skip cycles.
357 TARGET_SCHED_VARIABLE_ISSUE: Cache the value of can_issue_more so that it
358 can be returned from TARGET_SCHED_REORDER2.
360 TARGET_SCHED_INIT: Reset the register pressure counting variables. */
362 #undef TARGET_SCHED_DFA_NEW_CYCLE
363 #define TARGET_SCHED_DFA_NEW_CYCLE sh_dfa_new_cycle
365 #undef TARGET_SCHED_INIT_GLOBAL
366 #define TARGET_SCHED_INIT_GLOBAL sh_md_init_global
368 #undef TARGET_SCHED_FINISH_GLOBAL
369 #define TARGET_SCHED_FINISH_GLOBAL sh_md_finish_global
371 #undef TARGET_SCHED_VARIABLE_ISSUE
372 #define TARGET_SCHED_VARIABLE_ISSUE sh_variable_issue
374 #undef TARGET_SCHED_REORDER
375 #define TARGET_SCHED_REORDER sh_reorder
377 #undef TARGET_SCHED_REORDER2
378 #define TARGET_SCHED_REORDER2 sh_reorder2
380 #undef TARGET_SCHED_INIT
381 #define TARGET_SCHED_INIT sh_md_init
383 #undef TARGET_CANNOT_MODIFY_JUMPS_P
384 #define TARGET_CANNOT_MODIFY_JUMPS_P sh_cannot_modify_jumps_p
385 #undef TARGET_BRANCH_TARGET_REGISTER_CLASS
386 #define TARGET_BRANCH_TARGET_REGISTER_CLASS sh_target_reg_class
387 #undef TARGET_BRANCH_TARGET_REGISTER_CALLEE_SAVED
388 #define TARGET_BRANCH_TARGET_REGISTER_CALLEE_SAVED \
389 sh_optimize_target_register_callee_saved
391 #undef TARGET_MS_BITFIELD_LAYOUT_P
392 #define TARGET_MS_BITFIELD_LAYOUT_P sh_ms_bitfield_layout_p
394 #undef TARGET_INIT_BUILTINS
395 #define TARGET_INIT_BUILTINS sh_init_builtins
396 #undef TARGET_EXPAND_BUILTIN
397 #define TARGET_EXPAND_BUILTIN sh_expand_builtin
399 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
400 #define TARGET_FUNCTION_OK_FOR_SIBCALL sh_function_ok_for_sibcall
402 #undef TARGET_CANNOT_COPY_INSN_P
403 #define TARGET_CANNOT_COPY_INSN_P sh_cannot_copy_insn_p
404 #undef TARGET_RTX_COSTS
405 #define TARGET_RTX_COSTS sh_rtx_costs
406 #undef TARGET_ADDRESS_COST
407 #define TARGET_ADDRESS_COST sh_address_cost
409 #undef TARGET_MACHINE_DEPENDENT_REORG
410 #define TARGET_MACHINE_DEPENDENT_REORG sh_reorg
412 #ifdef HAVE_AS_TLS
413 #undef TARGET_HAVE_TLS
414 #define TARGET_HAVE_TLS true
415 #endif
417 #undef TARGET_PROMOTE_PROTOTYPES
418 #define TARGET_PROMOTE_PROTOTYPES sh_promote_prototypes
419 #undef TARGET_PROMOTE_FUNCTION_ARGS
420 #define TARGET_PROMOTE_FUNCTION_ARGS sh_promote_prototypes
421 #undef TARGET_PROMOTE_FUNCTION_RETURN
422 #define TARGET_PROMOTE_FUNCTION_RETURN sh_promote_prototypes
424 #undef TARGET_STRUCT_VALUE_RTX
425 #define TARGET_STRUCT_VALUE_RTX sh_struct_value_rtx
426 #undef TARGET_RETURN_IN_MEMORY
427 #define TARGET_RETURN_IN_MEMORY sh_return_in_memory
429 #undef TARGET_EXPAND_BUILTIN_SAVEREGS
430 #define TARGET_EXPAND_BUILTIN_SAVEREGS sh_builtin_saveregs
431 #undef TARGET_SETUP_INCOMING_VARARGS
432 #define TARGET_SETUP_INCOMING_VARARGS sh_setup_incoming_varargs
433 #undef TARGET_STRICT_ARGUMENT_NAMING
434 #define TARGET_STRICT_ARGUMENT_NAMING sh_strict_argument_naming
435 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
436 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED sh_pretend_outgoing_varargs_named
437 #undef TARGET_MUST_PASS_IN_STACK
438 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
439 #undef TARGET_PASS_BY_REFERENCE
440 #define TARGET_PASS_BY_REFERENCE sh_pass_by_reference
442 #undef TARGET_BUILD_BUILTIN_VA_LIST
443 #define TARGET_BUILD_BUILTIN_VA_LIST sh_build_builtin_va_list
444 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
445 #define TARGET_GIMPLIFY_VA_ARG_EXPR sh_gimplify_va_arg_expr
447 #undef TARGET_PCH_VALID_P
448 #define TARGET_PCH_VALID_P sh_pch_valid_p
450 /* Return regmode weight for insn. */
451 #define INSN_REGMODE_WEIGHT(INSN, MODE) regmode_weight[((MODE) == SImode) ? 0 : 1][INSN_UID (INSN)]
453 /* Return current register pressure for regmode. */
454 #define CURR_REGMODE_PRESSURE(MODE) curr_regmode_pressure[((MODE) == SImode) ? 0 : 1]
456 #ifdef SYMBIAN
458 #undef TARGET_ENCODE_SECTION_INFO
459 #define TARGET_ENCODE_SECTION_INFO sh_symbian_encode_section_info
460 #undef TARGET_STRIP_NAME_ENCODING
461 #define TARGET_STRIP_NAME_ENCODING sh_symbian_strip_name_encoding
462 #undef TARGET_CXX_IMPORT_EXPORT_CLASS
463 #define TARGET_CXX_IMPORT_EXPORT_CLASS symbian_import_export_class
465 #endif /* SYMBIAN */
467 struct gcc_target targetm = TARGET_INITIALIZER;
469 /* Print the operand address in x to the stream. */
471 void
472 print_operand_address (FILE *stream, rtx x)
474 switch (GET_CODE (x))
476 case REG:
477 case SUBREG:
478 fprintf (stream, "@%s", reg_names[true_regnum (x)]);
479 break;
481 case PLUS:
483 rtx base = XEXP (x, 0);
484 rtx index = XEXP (x, 1);
486 switch (GET_CODE (index))
488 case CONST_INT:
489 fprintf (stream, "@(%d,%s)", (int) INTVAL (index),
490 reg_names[true_regnum (base)]);
491 break;
493 case REG:
494 case SUBREG:
496 int base_num = true_regnum (base);
497 int index_num = true_regnum (index);
499 fprintf (stream, "@(r0,%s)",
500 reg_names[MAX (base_num, index_num)]);
501 break;
504 default:
505 debug_rtx (x);
506 abort ();
509 break;
511 case PRE_DEC:
512 fprintf (stream, "@-%s", reg_names[true_regnum (XEXP (x, 0))]);
513 break;
515 case POST_INC:
516 fprintf (stream, "@%s+", reg_names[true_regnum (XEXP (x, 0))]);
517 break;
519 default:
520 x = mark_constant_pool_use (x);
521 output_addr_const (stream, x);
522 break;
526 /* Print operand x (an rtx) in assembler syntax to file stream
527 according to modifier code.
529 '.' print a .s if insn needs delay slot
530 ',' print LOCAL_LABEL_PREFIX
531 '@' print trap, rte or rts depending upon pragma interruptness
532 '#' output a nop if there is nothing to put in the delay slot
533 ''' print likelihood suffix (/u for unlikely).
534 'O' print a constant without the #
535 'R' print the LSW of a dp value - changes if in little endian
536 'S' print the MSW of a dp value - changes if in little endian
537 'T' print the next word of a dp value - same as 'R' in big endian mode.
538 'M' print an `x' if `m' will print `base,index'.
539 'N' print 'r63' if the operand is (const_int 0).
540 'd' print a V2SF reg as dN instead of fpN.
541 'm' print a pair `base,offset' or `base,index', for LD and ST.
542 'u' prints the lowest 16 bits of CONST_INT, as an unsigned value.
543 'o' output an operator. */
545 void
546 print_operand (FILE *stream, rtx x, int code)
548 switch (code)
550 case '.':
551 if (final_sequence
552 && ! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
553 && get_attr_length (XVECEXP (final_sequence, 0, 1)))
554 fprintf (stream, ASSEMBLER_DIALECT ? "/s" : ".s");
555 break;
556 case ',':
557 fprintf (stream, "%s", LOCAL_LABEL_PREFIX);
558 break;
559 case '@':
560 if (trap_exit)
561 fprintf (stream, "trapa #%d", trap_exit);
562 else if (sh_cfun_interrupt_handler_p ())
563 fprintf (stream, "rte");
564 else
565 fprintf (stream, "rts");
566 break;
567 case '#':
568 /* Output a nop if there's nothing in the delay slot. */
569 if (dbr_sequence_length () == 0)
570 fprintf (stream, "\n\tnop");
571 break;
572 case '\'':
574 rtx note = find_reg_note (current_output_insn, REG_BR_PROB, 0);
576 if (note && INTVAL (XEXP (note, 0)) * 2 < REG_BR_PROB_BASE)
577 fputs ("/u", stream);
578 break;
580 case 'O':
581 x = mark_constant_pool_use (x);
582 output_addr_const (stream, x);
583 break;
584 case 'R':
585 fputs (reg_names[REGNO (x) + LSW], (stream));
586 break;
587 case 'S':
588 fputs (reg_names[REGNO (x) + MSW], (stream));
589 break;
590 case 'T':
591 /* Next word of a double. */
592 switch (GET_CODE (x))
594 case REG:
595 fputs (reg_names[REGNO (x) + 1], (stream));
596 break;
597 case MEM:
598 if (GET_CODE (XEXP (x, 0)) != PRE_DEC
599 && GET_CODE (XEXP (x, 0)) != POST_INC)
600 x = adjust_address (x, SImode, 4);
601 print_operand_address (stream, XEXP (x, 0));
602 break;
603 default:
604 break;
606 break;
607 case 'o':
608 switch (GET_CODE (x))
610 case PLUS: fputs ("add", stream); break;
611 case MINUS: fputs ("sub", stream); break;
612 case MULT: fputs ("mul", stream); break;
613 case DIV: fputs ("div", stream); break;
614 case EQ: fputs ("eq", stream); break;
615 case NE: fputs ("ne", stream); break;
616 case GT: case LT: fputs ("gt", stream); break;
617 case GE: case LE: fputs ("ge", stream); break;
618 case GTU: case LTU: fputs ("gtu", stream); break;
619 case GEU: case LEU: fputs ("geu", stream); break;
620 default:
621 break;
623 break;
624 case 'M':
625 if (GET_CODE (x) == MEM
626 && GET_CODE (XEXP (x, 0)) == PLUS
627 && (GET_CODE (XEXP (XEXP (x, 0), 1)) == REG
628 || GET_CODE (XEXP (XEXP (x, 0), 1)) == SUBREG))
629 fputc ('x', stream);
630 break;
632 case 'm':
633 if (GET_CODE (x) != MEM)
634 abort ();
635 x = XEXP (x, 0);
636 switch (GET_CODE (x))
638 case REG:
639 case SUBREG:
640 print_operand (stream, x, 0);
641 fputs (", 0", stream);
642 break;
644 case PLUS:
645 print_operand (stream, XEXP (x, 0), 0);
646 fputs (", ", stream);
647 print_operand (stream, XEXP (x, 1), 0);
648 break;
650 default:
651 abort ();
653 break;
655 case 'd':
656 if (GET_CODE (x) != REG || GET_MODE (x) != V2SFmode)
657 abort ();
659 fprintf ((stream), "d%s", reg_names[REGNO (x)] + 1);
660 break;
662 case 'N':
663 if (x == CONST0_RTX (GET_MODE (x)))
665 fprintf ((stream), "r63");
666 break;
668 goto default_output;
669 case 'u':
670 if (GET_CODE (x) == CONST_INT)
672 fprintf ((stream), "%u", (unsigned) INTVAL (x) & (0x10000 - 1));
673 break;
675 /* Fall through. */
677 default_output:
678 default:
679 switch (GET_CODE (x))
681 /* FIXME: We need this on SHmedia32 because reload generates
682 some sign-extended HI or QI loads into DImode registers
683 but, because Pmode is SImode, the address ends up with a
684 subreg:SI of the DImode register. Maybe reload should be
685 fixed so as to apply alter_subreg to such loads? */
686 case SUBREG:
687 if (SUBREG_BYTE (x) != 0
688 || GET_CODE (SUBREG_REG (x)) != REG)
689 abort ();
691 x = SUBREG_REG (x);
692 /* Fall through. */
694 case REG:
695 if (FP_REGISTER_P (REGNO (x))
696 && GET_MODE (x) == V16SFmode)
697 fprintf ((stream), "mtrx%s", reg_names[REGNO (x)] + 2);
698 else if (FP_REGISTER_P (REGNO (x))
699 && GET_MODE (x) == V4SFmode)
700 fprintf ((stream), "fv%s", reg_names[REGNO (x)] + 2);
701 else if (GET_CODE (x) == REG
702 && GET_MODE (x) == V2SFmode)
703 fprintf ((stream), "fp%s", reg_names[REGNO (x)] + 2);
704 else if (FP_REGISTER_P (REGNO (x))
705 && GET_MODE_SIZE (GET_MODE (x)) > 4)
706 fprintf ((stream), "d%s", reg_names[REGNO (x)] + 1);
707 else
708 fputs (reg_names[REGNO (x)], (stream));
709 break;
711 case MEM:
712 output_address (XEXP (x, 0));
713 break;
715 case CONST:
716 if (TARGET_SHMEDIA
717 && GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
718 && GET_MODE (XEXP (x, 0)) == DImode
719 && GET_CODE (XEXP (XEXP (x, 0), 0)) == TRUNCATE
720 && GET_MODE (XEXP (XEXP (x, 0), 0)) == HImode)
722 rtx val = XEXP (XEXP (XEXP (x, 0), 0), 0);
724 fputc ('(', stream);
725 if (GET_CODE (val) == ASHIFTRT)
727 fputc ('(', stream);
728 if (GET_CODE (XEXP (val, 0)) == CONST)
729 fputc ('(', stream);
730 output_addr_const (stream, XEXP (val, 0));
731 if (GET_CODE (XEXP (val, 0)) == CONST)
732 fputc (')', stream);
733 fputs (" >> ", stream);
734 output_addr_const (stream, XEXP (val, 1));
735 fputc (')', stream);
737 else
739 if (GET_CODE (val) == CONST)
740 fputc ('(', stream);
741 output_addr_const (stream, val);
742 if (GET_CODE (val) == CONST)
743 fputc (')', stream);
745 fputs (" & 65535)", stream);
746 break;
749 /* Fall through. */
750 default:
751 if (TARGET_SH1)
752 fputc ('#', stream);
753 output_addr_const (stream, x);
754 break;
756 break;
760 /* Like force_operand, but guarantees that VALUE ends up in TARGET. */
761 static void
762 force_into (rtx value, rtx target)
764 value = force_operand (value, target);
765 if (! rtx_equal_p (value, target))
766 emit_insn (gen_move_insn (target, value));
769 /* Emit code to perform a block move. Choose the best method.
771 OPERANDS[0] is the destination.
772 OPERANDS[1] is the source.
773 OPERANDS[2] is the size.
774 OPERANDS[3] is the alignment safe to use. */
777 expand_block_move (rtx *operands)
779 int align = INTVAL (operands[3]);
780 int constp = (GET_CODE (operands[2]) == CONST_INT);
781 int bytes = (constp ? INTVAL (operands[2]) : 0);
783 if (! constp)
784 return 0;
786 /* If we could use mov.l to move words and dest is word-aligned, we
787 can use movua.l for loads and still generate a relatively short
788 and efficient sequence. */
789 if (TARGET_SH4A_ARCH && align < 4
790 && MEM_ALIGN (operands[0]) >= 32
791 && can_move_by_pieces (bytes, 32))
793 rtx dest = copy_rtx (operands[0]);
794 rtx src = copy_rtx (operands[1]);
795 /* We could use different pseudos for each copied word, but
796 since movua can only load into r0, it's kind of
797 pointless. */
798 rtx temp = gen_reg_rtx (SImode);
799 rtx src_addr = copy_addr_to_reg (XEXP (src, 0));
800 int copied = 0;
802 while (copied + 4 <= bytes)
804 rtx to = adjust_address (dest, SImode, copied);
805 rtx from = adjust_automodify_address (src, SImode, src_addr, copied);
807 emit_insn (gen_movua (temp, from));
808 emit_move_insn (src_addr, plus_constant (src_addr, 4));
809 emit_move_insn (to, temp);
810 copied += 4;
813 if (copied < bytes)
814 move_by_pieces (adjust_address (dest, BLKmode, copied),
815 adjust_automodify_address (src, BLKmode,
816 src_addr, copied),
817 bytes - copied, align, 0);
819 return 1;
822 /* If it isn't a constant number of bytes, or if it doesn't have 4 byte
823 alignment, or if it isn't a multiple of 4 bytes, then fail. */
824 if (align < 4 || (bytes % 4 != 0))
825 return 0;
827 if (TARGET_HARD_SH4)
829 if (bytes < 12)
830 return 0;
831 else if (bytes == 12)
833 tree entry_name;
834 rtx sym;
835 rtx func_addr_rtx;
836 rtx r4 = gen_rtx_REG (SImode, 4);
837 rtx r5 = gen_rtx_REG (SImode, 5);
839 entry_name = get_identifier ("__movmemSI12_i4");
841 sym = function_symbol (IDENTIFIER_POINTER (entry_name));
842 func_addr_rtx = copy_to_mode_reg (Pmode, sym);
843 force_into (XEXP (operands[0], 0), r4);
844 force_into (XEXP (operands[1], 0), r5);
845 emit_insn (gen_block_move_real_i4 (func_addr_rtx));
846 return 1;
848 else if (! TARGET_SMALLCODE)
850 tree entry_name;
851 rtx sym;
852 rtx func_addr_rtx;
853 int dwords;
854 rtx r4 = gen_rtx_REG (SImode, 4);
855 rtx r5 = gen_rtx_REG (SImode, 5);
856 rtx r6 = gen_rtx_REG (SImode, 6);
858 entry_name = get_identifier (bytes & 4
859 ? "__movmem_i4_odd"
860 : "__movmem_i4_even");
861 sym = function_symbol (IDENTIFIER_POINTER (entry_name));
862 func_addr_rtx = copy_to_mode_reg (Pmode, sym);
863 force_into (XEXP (operands[0], 0), r4);
864 force_into (XEXP (operands[1], 0), r5);
866 dwords = bytes >> 3;
867 emit_insn (gen_move_insn (r6, GEN_INT (dwords - 1)));
868 emit_insn (gen_block_lump_real_i4 (func_addr_rtx));
869 return 1;
871 else
872 return 0;
874 if (bytes < 64)
876 char entry[30];
877 tree entry_name;
878 rtx sym;
879 rtx func_addr_rtx;
880 rtx r4 = gen_rtx_REG (SImode, 4);
881 rtx r5 = gen_rtx_REG (SImode, 5);
883 sprintf (entry, "__movmemSI%d", bytes);
884 entry_name = get_identifier (entry);
885 sym = function_symbol (IDENTIFIER_POINTER (entry_name));
886 func_addr_rtx = copy_to_mode_reg (Pmode, sym);
887 force_into (XEXP (operands[0], 0), r4);
888 force_into (XEXP (operands[1], 0), r5);
889 emit_insn (gen_block_move_real (func_addr_rtx));
890 return 1;
893 /* This is the same number of bytes as a memcpy call, but to a different
894 less common function name, so this will occasionally use more space. */
895 if (! TARGET_SMALLCODE)
897 tree entry_name;
898 rtx sym;
899 rtx func_addr_rtx;
900 int final_switch, while_loop;
901 rtx r4 = gen_rtx_REG (SImode, 4);
902 rtx r5 = gen_rtx_REG (SImode, 5);
903 rtx r6 = gen_rtx_REG (SImode, 6);
905 entry_name = get_identifier ("__movmem");
906 sym = function_symbol (IDENTIFIER_POINTER (entry_name));
907 func_addr_rtx = copy_to_mode_reg (Pmode, sym);
908 force_into (XEXP (operands[0], 0), r4);
909 force_into (XEXP (operands[1], 0), r5);
911 /* r6 controls the size of the move. 16 is decremented from it
912 for each 64 bytes moved. Then the negative bit left over is used
913 as an index into a list of move instructions. e.g., a 72 byte move
914 would be set up with size(r6) = 14, for one iteration through the
915 big while loop, and a switch of -2 for the last part. */
917 final_switch = 16 - ((bytes / 4) % 16);
918 while_loop = ((bytes / 4) / 16 - 1) * 16;
919 emit_insn (gen_move_insn (r6, GEN_INT (while_loop + final_switch)));
920 emit_insn (gen_block_lump_real (func_addr_rtx));
921 return 1;
924 return 0;
927 /* Prepare operands for a move define_expand; specifically, one of the
928 operands must be in a register. */
931 prepare_move_operands (rtx operands[], enum machine_mode mode)
933 if ((mode == SImode || mode == DImode)
934 && flag_pic
935 && ! ((mode == Pmode || mode == ptr_mode)
936 && tls_symbolic_operand (operands[1], Pmode) != 0))
938 rtx temp;
939 if (SYMBOLIC_CONST_P (operands[1]))
941 if (GET_CODE (operands[0]) == MEM)
942 operands[1] = force_reg (Pmode, operands[1]);
943 else if (TARGET_SHMEDIA
944 && GET_CODE (operands[1]) == LABEL_REF
945 && target_reg_operand (operands[0], mode))
946 /* It's ok. */;
947 else
949 temp = no_new_pseudos ? operands[0] : gen_reg_rtx (Pmode);
950 operands[1] = legitimize_pic_address (operands[1], mode, temp);
953 else if (GET_CODE (operands[1]) == CONST
954 && GET_CODE (XEXP (operands[1], 0)) == PLUS
955 && SYMBOLIC_CONST_P (XEXP (XEXP (operands[1], 0), 0)))
957 temp = no_new_pseudos ? operands[0] : gen_reg_rtx (Pmode);
958 temp = legitimize_pic_address (XEXP (XEXP (operands[1], 0), 0),
959 mode, temp);
960 operands[1] = expand_binop (mode, add_optab, temp,
961 XEXP (XEXP (operands[1], 0), 1),
962 no_new_pseudos ? temp
963 : gen_reg_rtx (Pmode),
964 0, OPTAB_LIB_WIDEN);
968 if (! reload_in_progress && ! reload_completed)
970 /* Copy the source to a register if both operands aren't registers. */
971 if (! register_operand (operands[0], mode)
972 && ! sh_register_operand (operands[1], mode))
973 operands[1] = copy_to_mode_reg (mode, operands[1]);
975 if (GET_CODE (operands[0]) == MEM && ! memory_operand (operands[0], mode))
977 /* This is like change_address_1 (operands[0], mode, 0, 1) ,
978 except that we can't use that function because it is static. */
979 rtx new = change_address (operands[0], mode, 0);
980 MEM_COPY_ATTRIBUTES (new, operands[0]);
981 operands[0] = new;
984 /* This case can happen while generating code to move the result
985 of a library call to the target. Reject `st r0,@(rX,rY)' because
986 reload will fail to find a spill register for rX, since r0 is already
987 being used for the source. */
988 else if (refers_to_regno_p (R0_REG, R0_REG + 1, operands[1], (rtx *)0)
989 && GET_CODE (operands[0]) == MEM
990 && GET_CODE (XEXP (operands[0], 0)) == PLUS
991 && GET_CODE (XEXP (XEXP (operands[0], 0), 1)) == REG)
992 operands[1] = copy_to_mode_reg (mode, operands[1]);
995 if (mode == Pmode || mode == ptr_mode)
997 rtx op0, op1;
998 enum tls_model tls_kind;
1000 op0 = operands[0];
1001 op1 = operands[1];
1002 if ((tls_kind = tls_symbolic_operand (op1, Pmode)))
1004 rtx tga_op1, tga_ret, tmp, tmp2;
1007 switch (tls_kind)
1009 case TLS_MODEL_GLOBAL_DYNAMIC:
1010 tga_ret = gen_rtx_REG (Pmode, R0_REG);
1011 emit_call_insn (gen_tls_global_dynamic (tga_ret, op1));
1012 op1 = tga_ret;
1013 break;
1015 case TLS_MODEL_LOCAL_DYNAMIC:
1016 tga_ret = gen_rtx_REG (Pmode, R0_REG);
1017 emit_call_insn (gen_tls_local_dynamic (tga_ret, op1));
1019 tmp = gen_reg_rtx (Pmode);
1020 emit_move_insn (tmp, tga_ret);
1022 if (register_operand (op0, Pmode))
1023 tmp2 = op0;
1024 else
1025 tmp2 = gen_reg_rtx (Pmode);
1027 emit_insn (gen_symDTPOFF2reg (tmp2, op1, tmp));
1028 op1 = tmp2;
1029 break;
1031 case TLS_MODEL_INITIAL_EXEC:
1032 if (! flag_pic)
1033 emit_insn (gen_GOTaddr2picreg ());
1034 tga_op1 = gen_reg_rtx (Pmode);
1035 tmp = gen_sym2GOTTPOFF (op1);
1036 emit_insn (gen_tls_initial_exec (tga_op1, tmp));
1037 op1 = tga_op1;
1038 break;
1040 case TLS_MODEL_LOCAL_EXEC:
1041 tmp2 = gen_reg_rtx (Pmode);
1042 emit_insn (gen_load_gbr (tmp2));
1043 tmp = gen_reg_rtx (Pmode);
1044 emit_insn (gen_symTPOFF2reg (tmp, op1));
1045 RTX_UNCHANGING_P (tmp) = 1;
1047 if (register_operand (op0, Pmode))
1048 op1 = op0;
1049 else
1050 op1 = gen_reg_rtx (Pmode);
1052 emit_insn (gen_addsi3 (op1, tmp, tmp2));
1053 break;
1055 default:
1056 abort ();
1058 operands[1] = op1;
1062 return 0;
1065 /* Prepare the operands for an scc instruction; make sure that the
1066 compare has been done. */
1068 prepare_scc_operands (enum rtx_code code)
1070 rtx t_reg = gen_rtx_REG (SImode, T_REG);
1071 enum rtx_code oldcode = code;
1072 enum machine_mode mode;
1074 /* First need a compare insn. */
1075 switch (code)
1077 case NE:
1078 /* It isn't possible to handle this case. */
1079 abort ();
1080 case LT:
1081 code = GT;
1082 break;
1083 case LE:
1084 code = GE;
1085 break;
1086 case LTU:
1087 code = GTU;
1088 break;
1089 case LEU:
1090 code = GEU;
1091 break;
1092 default:
1093 break;
1095 if (code != oldcode)
1097 rtx tmp = sh_compare_op0;
1098 sh_compare_op0 = sh_compare_op1;
1099 sh_compare_op1 = tmp;
1102 mode = GET_MODE (sh_compare_op0);
1103 if (mode == VOIDmode)
1104 mode = GET_MODE (sh_compare_op1);
1106 sh_compare_op0 = force_reg (mode, sh_compare_op0);
1107 if ((code != EQ && code != NE
1108 && (sh_compare_op1 != const0_rtx
1109 || code == GTU || code == GEU || code == LTU || code == LEU))
1110 || (mode == DImode && sh_compare_op1 != const0_rtx)
1111 || (TARGET_SH2E && GET_MODE_CLASS (mode) == MODE_FLOAT))
1112 sh_compare_op1 = force_reg (mode, sh_compare_op1);
1114 if (TARGET_SH4 && GET_MODE_CLASS (mode) == MODE_FLOAT)
1115 (mode == SFmode ? emit_sf_insn : emit_df_insn)
1116 (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2,
1117 gen_rtx_SET (VOIDmode, t_reg,
1118 gen_rtx_fmt_ee (code, SImode,
1119 sh_compare_op0, sh_compare_op1)),
1120 gen_rtx_USE (VOIDmode, get_fpscr_rtx ()))));
1121 else
1122 emit_insn (gen_rtx_SET (VOIDmode, t_reg,
1123 gen_rtx_fmt_ee (code, SImode,
1124 sh_compare_op0, sh_compare_op1)));
1126 return t_reg;
1129 /* Called from the md file, set up the operands of a compare instruction. */
1131 void
1132 from_compare (rtx *operands, int code)
1134 enum machine_mode mode = GET_MODE (sh_compare_op0);
1135 rtx insn;
1136 if (mode == VOIDmode)
1137 mode = GET_MODE (sh_compare_op1);
1138 if (code != EQ
1139 || mode == DImode
1140 || (TARGET_SH2E && GET_MODE_CLASS (mode) == MODE_FLOAT))
1142 /* Force args into regs, since we can't use constants here. */
1143 sh_compare_op0 = force_reg (mode, sh_compare_op0);
1144 if (sh_compare_op1 != const0_rtx
1145 || code == GTU || code == GEU
1146 || (TARGET_SH2E && GET_MODE_CLASS (mode) == MODE_FLOAT))
1147 sh_compare_op1 = force_reg (mode, sh_compare_op1);
1149 if (TARGET_SH2E && GET_MODE_CLASS (mode) == MODE_FLOAT && code == GE)
1151 from_compare (operands, GT);
1152 insn = gen_ieee_ccmpeqsf_t (sh_compare_op0, sh_compare_op1);
1154 else
1155 insn = gen_rtx_SET (VOIDmode,
1156 gen_rtx_REG (SImode, T_REG),
1157 gen_rtx_fmt_ee (code, SImode,
1158 sh_compare_op0, sh_compare_op1));
1159 if (TARGET_SH4 && GET_MODE_CLASS (mode) == MODE_FLOAT)
1161 insn = gen_rtx_PARALLEL (VOIDmode,
1162 gen_rtvec (2, insn,
1163 gen_rtx_USE (VOIDmode, get_fpscr_rtx ())));
1164 (mode == SFmode ? emit_sf_insn : emit_df_insn) (insn);
1166 else
1167 emit_insn (insn);
1170 /* Functions to output assembly code. */
1172 /* Return a sequence of instructions to perform DI or DF move.
1174 Since the SH cannot move a DI or DF in one instruction, we have
1175 to take care when we see overlapping source and dest registers. */
1177 const char *
1178 output_movedouble (rtx insn ATTRIBUTE_UNUSED, rtx operands[],
1179 enum machine_mode mode)
1181 rtx dst = operands[0];
1182 rtx src = operands[1];
1184 if (GET_CODE (dst) == MEM
1185 && GET_CODE (XEXP (dst, 0)) == PRE_DEC)
1186 return "mov.l %T1,%0\n\tmov.l %1,%0";
1188 if (register_operand (dst, mode)
1189 && register_operand (src, mode))
1191 if (REGNO (src) == MACH_REG)
1192 return "sts mach,%S0\n\tsts macl,%R0";
1194 /* When mov.d r1,r2 do r2->r3 then r1->r2;
1195 when mov.d r1,r0 do r1->r0 then r2->r1. */
1197 if (REGNO (src) + 1 == REGNO (dst))
1198 return "mov %T1,%T0\n\tmov %1,%0";
1199 else
1200 return "mov %1,%0\n\tmov %T1,%T0";
1202 else if (GET_CODE (src) == CONST_INT)
1204 if (INTVAL (src) < 0)
1205 output_asm_insn ("mov #-1,%S0", operands);
1206 else
1207 output_asm_insn ("mov #0,%S0", operands);
1209 return "mov %1,%R0";
1211 else if (GET_CODE (src) == MEM)
1213 int ptrreg = -1;
1214 int dreg = REGNO (dst);
1215 rtx inside = XEXP (src, 0);
1217 if (GET_CODE (inside) == REG)
1218 ptrreg = REGNO (inside);
1219 else if (GET_CODE (inside) == SUBREG)
1220 ptrreg = subreg_regno (inside);
1221 else if (GET_CODE (inside) == PLUS)
1223 ptrreg = REGNO (XEXP (inside, 0));
1224 /* ??? A r0+REG address shouldn't be possible here, because it isn't
1225 an offsettable address. Unfortunately, offsettable addresses use
1226 QImode to check the offset, and a QImode offsettable address
1227 requires r0 for the other operand, which is not currently
1228 supported, so we can't use the 'o' constraint.
1229 Thus we must check for and handle r0+REG addresses here.
1230 We punt for now, since this is likely very rare. */
1231 if (GET_CODE (XEXP (inside, 1)) == REG)
1232 abort ();
1234 else if (GET_CODE (inside) == LABEL_REF)
1235 return "mov.l %1,%0\n\tmov.l %1+4,%T0";
1236 else if (GET_CODE (inside) == POST_INC)
1237 return "mov.l %1,%0\n\tmov.l %1,%T0";
1238 else
1239 abort ();
1241 /* Work out the safe way to copy. Copy into the second half first. */
1242 if (dreg == ptrreg)
1243 return "mov.l %T1,%T0\n\tmov.l %1,%0";
1246 return "mov.l %1,%0\n\tmov.l %T1,%T0";
1249 /* Print an instruction which would have gone into a delay slot after
1250 another instruction, but couldn't because the other instruction expanded
1251 into a sequence where putting the slot insn at the end wouldn't work. */
1253 static void
1254 print_slot (rtx insn)
1256 final_scan_insn (XVECEXP (insn, 0, 1), asm_out_file, optimize, 0, 1, NULL);
1258 INSN_DELETED_P (XVECEXP (insn, 0, 1)) = 1;
1261 const char *
1262 output_far_jump (rtx insn, rtx op)
1264 struct { rtx lab, reg, op; } this;
1265 rtx braf_base_lab = NULL_RTX;
1266 const char *jump;
1267 int far;
1268 int offset = branch_dest (insn) - INSN_ADDRESSES (INSN_UID (insn));
1269 rtx prev;
1271 this.lab = gen_label_rtx ();
1273 if (TARGET_SH2
1274 && offset >= -32764
1275 && offset - get_attr_length (insn) <= 32766)
1277 far = 0;
1278 jump = "mov.w %O0,%1; braf %1";
1280 else
1282 far = 1;
1283 if (flag_pic)
1285 if (TARGET_SH2)
1286 jump = "mov.l %O0,%1; braf %1";
1287 else
1288 jump = "mov.l r0,@-r15; mova %O0,r0; mov.l @r0,%1; add r0,%1; mov.l @r15+,r0; jmp @%1";
1290 else
1291 jump = "mov.l %O0,%1; jmp @%1";
1293 /* If we have a scratch register available, use it. */
1294 if (GET_CODE ((prev = prev_nonnote_insn (insn))) == INSN
1295 && INSN_CODE (prev) == CODE_FOR_indirect_jump_scratch)
1297 this.reg = SET_DEST (XVECEXP (PATTERN (prev), 0, 0));
1298 if (REGNO (this.reg) == R0_REG && flag_pic && ! TARGET_SH2)
1299 jump = "mov.l r1,@-r15; mova %O0,r0; mov.l @r0,r1; add r1,r0; mov.l @r15+,r1; jmp @%1";
1300 output_asm_insn (jump, &this.lab);
1301 if (dbr_sequence_length ())
1302 print_slot (final_sequence);
1303 else
1304 output_asm_insn ("nop", 0);
1306 else
1308 /* Output the delay slot insn first if any. */
1309 if (dbr_sequence_length ())
1310 print_slot (final_sequence);
1312 this.reg = gen_rtx_REG (SImode, 13);
1313 /* We must keep the stack aligned to 8-byte boundaries on SH5.
1314 Fortunately, MACL is fixed and call-clobbered, and we never
1315 need its value across jumps, so save r13 in it instead of in
1316 the stack. */
1317 if (TARGET_SH5)
1318 output_asm_insn ("lds r13, macl", 0);
1319 else
1320 output_asm_insn ("mov.l r13,@-r15", 0);
1321 output_asm_insn (jump, &this.lab);
1322 if (TARGET_SH5)
1323 output_asm_insn ("sts macl, r13", 0);
1324 else
1325 output_asm_insn ("mov.l @r15+,r13", 0);
1327 if (far && flag_pic && TARGET_SH2)
1329 braf_base_lab = gen_label_rtx ();
1330 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1331 CODE_LABEL_NUMBER (braf_base_lab));
1333 if (far)
1334 output_asm_insn (".align 2", 0);
1335 (*targetm.asm_out.internal_label) (asm_out_file, "L", CODE_LABEL_NUMBER (this.lab));
1336 this.op = op;
1337 if (far && flag_pic)
1339 if (TARGET_SH2)
1340 this.lab = braf_base_lab;
1341 output_asm_insn (".long %O2-%O0", &this.lab);
1343 else
1344 output_asm_insn (far ? ".long %O2" : ".word %O2-%O0", &this.lab);
1345 return "";
1348 /* Local label counter, used for constants in the pool and inside
1349 pattern branches. */
1351 static int lf = 100;
1353 /* Output code for ordinary branches. */
1355 const char *
1356 output_branch (int logic, rtx insn, rtx *operands)
1358 switch (get_attr_length (insn))
1360 case 6:
1361 /* This can happen if filling the delay slot has caused a forward
1362 branch to exceed its range (we could reverse it, but only
1363 when we know we won't overextend other branches; this should
1364 best be handled by relaxation).
1365 It can also happen when other condbranches hoist delay slot insn
1366 from their destination, thus leading to code size increase.
1367 But the branch will still be in the range -4092..+4098 bytes. */
1369 if (! TARGET_RELAX)
1371 int label = lf++;
1372 /* The call to print_slot will clobber the operands. */
1373 rtx op0 = operands[0];
1375 /* If the instruction in the delay slot is annulled (true), then
1376 there is no delay slot where we can put it now. The only safe
1377 place for it is after the label. final will do that by default. */
1379 if (final_sequence
1380 && ! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
1382 asm_fprintf (asm_out_file, "\tb%s%ss\t%LLF%d\n", logic ? "f" : "t",
1383 ASSEMBLER_DIALECT ? "/" : ".", label);
1384 print_slot (final_sequence);
1386 else
1387 asm_fprintf (asm_out_file, "\tb%s\t%LLF%d\n", logic ? "f" : "t", label);
1389 output_asm_insn ("bra\t%l0", &op0);
1390 fprintf (asm_out_file, "\tnop\n");
1391 (*targetm.asm_out.internal_label) (asm_out_file, "LF", label);
1393 return "";
1395 /* When relaxing, handle this like a short branch. The linker
1396 will fix it up if it still doesn't fit after relaxation. */
1397 case 2:
1398 return logic ? "bt%.\t%l0" : "bf%.\t%l0";
1400 /* These are for SH2e, in which we have to account for the
1401 extra nop because of the hardware bug in annulled branches. */
1402 case 8:
1403 if (! TARGET_RELAX)
1405 int label = lf++;
1407 if (final_sequence
1408 && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
1409 abort ();
1410 asm_fprintf (asm_out_file, "b%s%ss\t%LLF%d\n",
1411 logic ? "f" : "t",
1412 ASSEMBLER_DIALECT ? "/" : ".", label);
1413 fprintf (asm_out_file, "\tnop\n");
1414 output_asm_insn ("bra\t%l0", operands);
1415 fprintf (asm_out_file, "\tnop\n");
1416 (*targetm.asm_out.internal_label) (asm_out_file, "LF", label);
1418 return "";
1420 /* When relaxing, fall through. */
1421 case 4:
1423 char buffer[10];
1425 sprintf (buffer, "b%s%ss\t%%l0",
1426 logic ? "t" : "f",
1427 ASSEMBLER_DIALECT ? "/" : ".");
1428 output_asm_insn (buffer, &operands[0]);
1429 return "nop";
1432 default:
1433 /* There should be no longer branches now - that would
1434 indicate that something has destroyed the branches set
1435 up in machine_dependent_reorg. */
1436 abort ();
1440 const char *
1441 output_branchy_insn (enum rtx_code code, const char *template,
1442 rtx insn, rtx *operands)
1444 rtx next_insn = NEXT_INSN (insn);
1446 if (next_insn && GET_CODE (next_insn) == JUMP_INSN && condjump_p (next_insn))
1448 rtx src = SET_SRC (PATTERN (next_insn));
1449 if (GET_CODE (src) == IF_THEN_ELSE && GET_CODE (XEXP (src, 0)) != code)
1451 /* Following branch not taken */
1452 operands[9] = gen_label_rtx ();
1453 emit_label_after (operands[9], next_insn);
1454 INSN_ADDRESSES_NEW (operands[9],
1455 INSN_ADDRESSES (INSN_UID (next_insn))
1456 + get_attr_length (next_insn));
1457 return template;
1459 else
1461 int offset = (branch_dest (next_insn)
1462 - INSN_ADDRESSES (INSN_UID (next_insn)) + 4);
1463 if (offset >= -252 && offset <= 258)
1465 if (GET_CODE (src) == IF_THEN_ELSE)
1466 /* branch_true */
1467 src = XEXP (src, 1);
1468 operands[9] = src;
1469 return template;
1473 operands[9] = gen_label_rtx ();
1474 emit_label_after (operands[9], insn);
1475 INSN_ADDRESSES_NEW (operands[9],
1476 INSN_ADDRESSES (INSN_UID (insn))
1477 + get_attr_length (insn));
1478 return template;
1481 const char *
1482 output_ieee_ccmpeq (rtx insn, rtx *operands)
1484 return output_branchy_insn (NE, "bt\t%l9\\;fcmp/eq\t%1,%0", insn, operands);
1487 /* Output the start of the assembler file. */
1489 static void
1490 sh_file_start (void)
1492 default_file_start ();
1494 #ifdef SYMBIAN
1495 /* Declare the .directive section before it is used. */
1496 fputs ("\t.section .directive, \"SM\", @progbits, 1\n", asm_out_file);
1497 fputs ("\t.asciz \"#<SYMEDIT>#\\n\"\n", asm_out_file);
1498 #endif
1500 if (TARGET_ELF)
1501 /* We need to show the text section with the proper
1502 attributes as in TEXT_SECTION_ASM_OP, before dwarf2out
1503 emits it without attributes in TEXT_SECTION_ASM_OP, else GAS
1504 will complain. We can teach GAS specifically about the
1505 default attributes for our choice of text section, but
1506 then we would have to change GAS again if/when we change
1507 the text section name. */
1508 fprintf (asm_out_file, "%s\n", TEXT_SECTION_ASM_OP);
1509 else
1510 /* Switch to the data section so that the coffsem symbol
1511 isn't in the text section. */
1512 data_section ();
1514 if (TARGET_LITTLE_ENDIAN)
1515 fputs ("\t.little\n", asm_out_file);
1517 if (!TARGET_ELF)
1519 if (TARGET_SHCOMPACT)
1520 fputs ("\t.mode\tSHcompact\n", asm_out_file);
1521 else if (TARGET_SHMEDIA)
1522 fprintf (asm_out_file, "\t.mode\tSHmedia\n\t.abi\t%i\n",
1523 TARGET_SHMEDIA64 ? 64 : 32);
1527 /* Check if PAT includes UNSPEC_CALLER unspec pattern. */
1529 static bool
1530 unspec_caller_rtx_p (rtx pat)
1532 switch (GET_CODE (pat))
1534 case CONST:
1535 return unspec_caller_rtx_p (XEXP (pat, 0));
1536 case PLUS:
1537 case MINUS:
1538 if (unspec_caller_rtx_p (XEXP (pat, 0)))
1539 return true;
1540 return unspec_caller_rtx_p (XEXP (pat, 1));
1541 case UNSPEC:
1542 if (XINT (pat, 1) == UNSPEC_CALLER)
1543 return true;
1544 default:
1545 break;
1548 return false;
1551 /* Indicate that INSN cannot be duplicated. This is true for insn
1552 that generates an unique label. */
1554 static bool
1555 sh_cannot_copy_insn_p (rtx insn)
1557 rtx pat;
1559 if (!reload_completed || !flag_pic)
1560 return false;
1562 if (GET_CODE (insn) != INSN)
1563 return false;
1564 if (asm_noperands (insn) >= 0)
1565 return false;
1567 pat = PATTERN (insn);
1568 if (GET_CODE (pat) != SET)
1569 return false;
1570 pat = SET_SRC (pat);
1572 if (unspec_caller_rtx_p (pat))
1573 return true;
1575 return false;
1578 /* Actual number of instructions used to make a shift by N. */
1579 static const char ashiftrt_insns[] =
1580 { 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};
1582 /* Left shift and logical right shift are the same. */
1583 static const char shift_insns[] =
1584 { 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};
1586 /* Individual shift amounts needed to get the above length sequences.
1587 One bit right shifts clobber the T bit, so when possible, put one bit
1588 shifts in the middle of the sequence, so the ends are eligible for
1589 branch delay slots. */
1590 static const short shift_amounts[32][5] = {
1591 {0}, {1}, {2}, {2, 1},
1592 {2, 2}, {2, 1, 2}, {2, 2, 2}, {2, 2, 1, 2},
1593 {8}, {8, 1}, {8, 2}, {8, 1, 2},
1594 {8, 2, 2}, {8, 2, 1, 2}, {8, -2, 8}, {8, -1, 8},
1595 {16}, {16, 1}, {16, 2}, {16, 1, 2},
1596 {16, 2, 2}, {16, 2, 1, 2}, {16, -2, 8}, {16, -1, 8},
1597 {16, 8}, {16, 1, 8}, {16, 8, 2}, {16, 8, 1, 2},
1598 {16, 8, 2, 2}, {16, -1, -2, 16}, {16, -2, 16}, {16, -1, 16}};
1600 /* Likewise, but for shift amounts < 16, up to three highmost bits
1601 might be clobbered. This is typically used when combined with some
1602 kind of sign or zero extension. */
1604 static const char ext_shift_insns[] =
1605 { 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};
1607 static const short ext_shift_amounts[32][4] = {
1608 {0}, {1}, {2}, {2, 1},
1609 {2, 2}, {2, 1, 2}, {8, -2}, {8, -1},
1610 {8}, {8, 1}, {8, 2}, {8, 1, 2},
1611 {8, 2, 2}, {16, -2, -1}, {16, -2}, {16, -1},
1612 {16}, {16, 1}, {16, 2}, {16, 1, 2},
1613 {16, 2, 2}, {16, 2, 1, 2}, {16, -2, 8}, {16, -1, 8},
1614 {16, 8}, {16, 1, 8}, {16, 8, 2}, {16, 8, 1, 2},
1615 {16, 8, 2, 2}, {16, -1, -2, 16}, {16, -2, 16}, {16, -1, 16}};
1617 /* Assuming we have a value that has been sign-extended by at least one bit,
1618 can we use the ext_shift_amounts with the last shift turned to an arithmetic shift
1619 to shift it by N without data loss, and quicker than by other means? */
1620 #define EXT_SHIFT_SIGNED(n) (((n) | 8) == 15)
1622 /* This is used in length attributes in sh.md to help compute the length
1623 of arbitrary constant shift instructions. */
1626 shift_insns_rtx (rtx insn)
1628 rtx set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
1629 int shift_count = INTVAL (XEXP (set_src, 1));
1630 enum rtx_code shift_code = GET_CODE (set_src);
1632 switch (shift_code)
1634 case ASHIFTRT:
1635 return ashiftrt_insns[shift_count];
1636 case LSHIFTRT:
1637 case ASHIFT:
1638 return shift_insns[shift_count];
1639 default:
1640 abort ();
1644 /* Return the cost of a shift. */
1646 static inline int
1647 shiftcosts (rtx x)
1649 int value;
1651 if (TARGET_SHMEDIA)
1652 return 1;
1654 if (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
1656 if (GET_MODE (x) == DImode
1657 && GET_CODE (XEXP (x, 1)) == CONST_INT
1658 && INTVAL (XEXP (x, 1)) == 1)
1659 return 2;
1661 /* Everything else is invalid, because there is no pattern for it. */
1662 return 10000;
1664 /* If shift by a non constant, then this will be expensive. */
1665 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
1666 return SH_DYNAMIC_SHIFT_COST;
1668 value = INTVAL (XEXP (x, 1));
1670 /* Otherwise, return the true cost in instructions. */
1671 if (GET_CODE (x) == ASHIFTRT)
1673 int cost = ashiftrt_insns[value];
1674 /* If SH3, then we put the constant in a reg and use shad. */
1675 if (cost > 1 + SH_DYNAMIC_SHIFT_COST)
1676 cost = 1 + SH_DYNAMIC_SHIFT_COST;
1677 return cost;
1679 else
1680 return shift_insns[value];
1683 /* Return the cost of an AND operation. */
1685 static inline int
1686 andcosts (rtx x)
1688 int i;
1690 /* Anding with a register is a single cycle and instruction. */
1691 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
1692 return 1;
1694 i = INTVAL (XEXP (x, 1));
1696 if (TARGET_SHMEDIA)
1698 if ((GET_CODE (XEXP (x, 1)) == CONST_INT
1699 && CONST_OK_FOR_I16 (INTVAL (XEXP (x, 1))))
1700 || EXTRA_CONSTRAINT_C16 (XEXP (x, 1)))
1701 return 1;
1702 else
1703 return 2;
1706 /* These constants are single cycle extu.[bw] instructions. */
1707 if (i == 0xff || i == 0xffff)
1708 return 1;
1709 /* Constants that can be used in an and immediate instruction in a single
1710 cycle, but this requires r0, so make it a little more expensive. */
1711 if (CONST_OK_FOR_K08 (i))
1712 return 2;
1713 /* Constants that can be loaded with a mov immediate and an and.
1714 This case is probably unnecessary. */
1715 if (CONST_OK_FOR_I08 (i))
1716 return 2;
1717 /* Any other constants requires a 2 cycle pc-relative load plus an and.
1718 This case is probably unnecessary. */
1719 return 3;
1722 /* Return the cost of an addition or a subtraction. */
1724 static inline int
1725 addsubcosts (rtx x)
1727 /* Adding a register is a single cycle insn. */
1728 if (GET_CODE (XEXP (x, 1)) == REG
1729 || GET_CODE (XEXP (x, 1)) == SUBREG)
1730 return 1;
1732 /* Likewise for small constants. */
1733 if (GET_CODE (XEXP (x, 1)) == CONST_INT
1734 && CONST_OK_FOR_ADD (INTVAL (XEXP (x, 1))))
1735 return 1;
1737 if (TARGET_SHMEDIA)
1738 switch (GET_CODE (XEXP (x, 1)))
1740 case CONST:
1741 case LABEL_REF:
1742 case SYMBOL_REF:
1743 return TARGET_SHMEDIA64 ? 5 : 3;
1745 case CONST_INT:
1746 if (CONST_OK_FOR_I16 (INTVAL (XEXP (x, 1))))
1747 return 2;
1748 else if (CONST_OK_FOR_I16 (INTVAL (XEXP (x, 1)) >> 16))
1749 return 3;
1750 else if (CONST_OK_FOR_I16 ((INTVAL (XEXP (x, 1)) >> 16) >> 16))
1751 return 4;
1753 /* Fall through. */
1754 default:
1755 return 5;
1758 /* Any other constant requires a 2 cycle pc-relative load plus an
1759 addition. */
1760 return 3;
1763 /* Return the cost of a multiply. */
1764 static inline int
1765 multcosts (rtx x ATTRIBUTE_UNUSED)
1767 if (TARGET_SHMEDIA)
1768 return 3;
1770 if (TARGET_SH2)
1772 /* We have a mul insn, so we can never take more than the mul and the
1773 read of the mac reg, but count more because of the latency and extra
1774 reg usage. */
1775 if (TARGET_SMALLCODE)
1776 return 2;
1777 return 3;
1780 /* If we're aiming at small code, then just count the number of
1781 insns in a multiply call sequence. */
1782 if (TARGET_SMALLCODE)
1783 return 5;
1785 /* Otherwise count all the insns in the routine we'd be calling too. */
1786 return 20;
1789 /* Compute a (partial) cost for rtx X. Return true if the complete
1790 cost has been computed, and false if subexpressions should be
1791 scanned. In either case, *TOTAL contains the cost result. */
1793 static bool
1794 sh_rtx_costs (rtx x, int code, int outer_code, int *total)
1796 switch (code)
1798 case CONST_INT:
1799 if (TARGET_SHMEDIA)
1801 if (INTVAL (x) == 0)
1802 *total = 0;
1803 else if (outer_code == AND && and_operand ((x), DImode))
1804 *total = 0;
1805 else if ((outer_code == IOR || outer_code == XOR
1806 || outer_code == PLUS)
1807 && CONST_OK_FOR_I10 (INTVAL (x)))
1808 *total = 0;
1809 else if (CONST_OK_FOR_I16 (INTVAL (x)))
1810 *total = COSTS_N_INSNS (outer_code != SET);
1811 else if (CONST_OK_FOR_I16 (INTVAL (x) >> 16))
1812 *total = COSTS_N_INSNS (2);
1813 else if (CONST_OK_FOR_I16 ((INTVAL (x) >> 16) >> 16))
1814 *total = COSTS_N_INSNS (3);
1815 else
1816 *total = COSTS_N_INSNS (4);
1817 return true;
1819 if (CONST_OK_FOR_I08 (INTVAL (x)))
1820 *total = 0;
1821 else if ((outer_code == AND || outer_code == IOR || outer_code == XOR)
1822 && CONST_OK_FOR_K08 (INTVAL (x)))
1823 *total = 1;
1824 else
1825 *total = 8;
1826 return true;
1828 case CONST:
1829 case LABEL_REF:
1830 case SYMBOL_REF:
1831 if (TARGET_SHMEDIA64)
1832 *total = COSTS_N_INSNS (4);
1833 else if (TARGET_SHMEDIA32)
1834 *total = COSTS_N_INSNS (2);
1835 else
1836 *total = 5;
1837 return true;
1839 case CONST_DOUBLE:
1840 if (TARGET_SHMEDIA)
1841 *total = COSTS_N_INSNS (4);
1842 else
1843 *total = 10;
1844 return true;
1846 case PLUS:
1847 *total = COSTS_N_INSNS (addsubcosts (x));
1848 return true;
1850 case AND:
1851 *total = COSTS_N_INSNS (andcosts (x));
1852 return true;
1854 case MULT:
1855 *total = COSTS_N_INSNS (multcosts (x));
1856 return true;
1858 case ASHIFT:
1859 case ASHIFTRT:
1860 case LSHIFTRT:
1861 *total = COSTS_N_INSNS (shiftcosts (x));
1862 return true;
1864 case DIV:
1865 case UDIV:
1866 case MOD:
1867 case UMOD:
1868 *total = COSTS_N_INSNS (20);
1869 return true;
1871 case FLOAT:
1872 case FIX:
1873 *total = 100;
1874 return true;
1876 default:
1877 return false;
1881 /* Compute the cost of an address. For the SH, all valid addresses are
1882 the same cost. Use a slightly higher cost for reg + reg addressing,
1883 since it increases pressure on r0. */
1885 static int
1886 sh_address_cost (rtx X)
1888 return (GET_CODE (X) == PLUS
1889 && ! CONSTANT_P (XEXP (X, 1))
1890 && ! TARGET_SHMEDIA ? 1 : 0);
1893 /* Code to expand a shift. */
1895 void
1896 gen_ashift (int type, int n, rtx reg)
1898 /* Negative values here come from the shift_amounts array. */
1899 if (n < 0)
1901 if (type == ASHIFT)
1902 type = LSHIFTRT;
1903 else
1904 type = ASHIFT;
1905 n = -n;
1908 switch (type)
1910 case ASHIFTRT:
1911 emit_insn (gen_ashrsi3_k (reg, reg, GEN_INT (n)));
1912 break;
1913 case LSHIFTRT:
1914 if (n == 1)
1915 emit_insn (gen_lshrsi3_m (reg, reg, GEN_INT (n)));
1916 else
1917 emit_insn (gen_lshrsi3_k (reg, reg, GEN_INT (n)));
1918 break;
1919 case ASHIFT:
1920 emit_insn (gen_ashlsi3_std (reg, reg, GEN_INT (n)));
1921 break;
1925 /* Same for HImode */
1927 void
1928 gen_ashift_hi (int type, int n, rtx reg)
1930 /* Negative values here come from the shift_amounts array. */
1931 if (n < 0)
1933 if (type == ASHIFT)
1934 type = LSHIFTRT;
1935 else
1936 type = ASHIFT;
1937 n = -n;
1940 switch (type)
1942 case ASHIFTRT:
1943 case LSHIFTRT:
1944 /* We don't have HImode right shift operations because using the
1945 ordinary 32 bit shift instructions for that doesn't generate proper
1946 zero/sign extension.
1947 gen_ashift_hi is only called in contexts where we know that the
1948 sign extension works out correctly. */
1950 int offset = 0;
1951 if (GET_CODE (reg) == SUBREG)
1953 offset = SUBREG_BYTE (reg);
1954 reg = SUBREG_REG (reg);
1956 gen_ashift (type, n, gen_rtx_SUBREG (SImode, reg, offset));
1957 break;
1959 case ASHIFT:
1960 emit_insn (gen_ashlhi3_k (reg, reg, GEN_INT (n)));
1961 break;
1965 /* Output RTL to split a constant shift into its component SH constant
1966 shift instructions. */
1968 void
1969 gen_shifty_op (int code, rtx *operands)
1971 int value = INTVAL (operands[2]);
1972 int max, i;
1974 /* Truncate the shift count in case it is out of bounds. */
1975 value = value & 0x1f;
1977 if (value == 31)
1979 if (code == LSHIFTRT)
1981 emit_insn (gen_rotlsi3_1 (operands[0], operands[0]));
1982 emit_insn (gen_movt (operands[0]));
1983 return;
1985 else if (code == ASHIFT)
1987 /* There is a two instruction sequence for 31 bit left shifts,
1988 but it requires r0. */
1989 if (GET_CODE (operands[0]) == REG && REGNO (operands[0]) == 0)
1991 emit_insn (gen_andsi3 (operands[0], operands[0], const1_rtx));
1992 emit_insn (gen_rotlsi3_31 (operands[0], operands[0]));
1993 return;
1997 else if (value == 0)
1999 /* This can happen when not optimizing. We must output something here
2000 to prevent the compiler from aborting in final.c after the try_split
2001 call. */
2002 emit_insn (gen_nop ());
2003 return;
2006 max = shift_insns[value];
2007 for (i = 0; i < max; i++)
2008 gen_ashift (code, shift_amounts[value][i], operands[0]);
2011 /* Same as above, but optimized for values where the topmost bits don't
2012 matter. */
2014 void
2015 gen_shifty_hi_op (int code, rtx *operands)
2017 int value = INTVAL (operands[2]);
2018 int max, i;
2019 void (*gen_fun) (int, int, rtx);
2021 /* This operation is used by and_shl for SImode values with a few
2022 high bits known to be cleared. */
2023 value &= 31;
2024 if (value == 0)
2026 emit_insn (gen_nop ());
2027 return;
2030 gen_fun = GET_MODE (operands[0]) == HImode ? gen_ashift_hi : gen_ashift;
2031 if (code == ASHIFT)
2033 max = ext_shift_insns[value];
2034 for (i = 0; i < max; i++)
2035 gen_fun (code, ext_shift_amounts[value][i], operands[0]);
2037 else
2038 /* When shifting right, emit the shifts in reverse order, so that
2039 solitary negative values come first. */
2040 for (i = ext_shift_insns[value] - 1; i >= 0; i--)
2041 gen_fun (code, ext_shift_amounts[value][i], operands[0]);
2044 /* Output RTL for an arithmetic right shift. */
2046 /* ??? Rewrite to use super-optimizer sequences. */
2049 expand_ashiftrt (rtx *operands)
2051 rtx sym;
2052 rtx wrk;
2053 char func[18];
2054 tree func_name;
2055 int value;
2057 if (TARGET_SH3)
2059 if (GET_CODE (operands[2]) != CONST_INT)
2061 rtx count = copy_to_mode_reg (SImode, operands[2]);
2062 emit_insn (gen_negsi2 (count, count));
2063 emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
2064 return 1;
2066 else if (ashiftrt_insns[INTVAL (operands[2]) & 31]
2067 > 1 + SH_DYNAMIC_SHIFT_COST)
2069 rtx count
2070 = force_reg (SImode, GEN_INT (- (INTVAL (operands[2]) & 31)));
2071 emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
2072 return 1;
2075 if (GET_CODE (operands[2]) != CONST_INT)
2076 return 0;
2078 value = INTVAL (operands[2]) & 31;
2080 if (value == 31)
2082 emit_insn (gen_ashrsi2_31 (operands[0], operands[1]));
2083 return 1;
2085 else if (value >= 16 && value <= 19)
2087 wrk = gen_reg_rtx (SImode);
2088 emit_insn (gen_ashrsi2_16 (wrk, operands[1]));
2089 value -= 16;
2090 while (value--)
2091 gen_ashift (ASHIFTRT, 1, wrk);
2092 emit_move_insn (operands[0], wrk);
2093 return 1;
2095 /* Expand a short sequence inline, longer call a magic routine. */
2096 else if (value <= 5)
2098 wrk = gen_reg_rtx (SImode);
2099 emit_move_insn (wrk, operands[1]);
2100 while (value--)
2101 gen_ashift (ASHIFTRT, 1, wrk);
2102 emit_move_insn (operands[0], wrk);
2103 return 1;
2106 wrk = gen_reg_rtx (Pmode);
2108 /* Load the value into an arg reg and call a helper. */
2109 emit_move_insn (gen_rtx_REG (SImode, 4), operands[1]);
2110 sprintf (func, "__ashiftrt_r4_%d", value);
2111 func_name = get_identifier (func);
2112 sym = function_symbol (IDENTIFIER_POINTER (func_name));
2113 emit_move_insn (wrk, sym);
2114 emit_insn (gen_ashrsi3_n (GEN_INT (value), wrk));
2115 emit_move_insn (operands[0], gen_rtx_REG (SImode, 4));
2116 return 1;
2120 sh_dynamicalize_shift_p (rtx count)
2122 return shift_insns[INTVAL (count)] > 1 + SH_DYNAMIC_SHIFT_COST;
2125 /* Try to find a good way to implement the combiner pattern
2126 [(set (match_operand:SI 0 "register_operand" "r")
2127 (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
2128 (match_operand:SI 2 "const_int_operand" "n"))
2129 (match_operand:SI 3 "const_int_operand" "n"))) .
2130 LEFT_RTX is operand 2 in the above pattern, and MASK_RTX is operand 3.
2131 return 0 for simple right / left or left/right shift combination.
2132 return 1 for a combination of shifts with zero_extend.
2133 return 2 for a combination of shifts with an AND that needs r0.
2134 return 3 for a combination of shifts with an AND that needs an extra
2135 scratch register, when the three highmost bits of the AND mask are clear.
2136 return 4 for a combination of shifts with an AND that needs an extra
2137 scratch register, when any of the three highmost bits of the AND mask
2138 is set.
2139 If ATTRP is set, store an initial right shift width in ATTRP[0],
2140 and the instruction length in ATTRP[1] . These values are not valid
2141 when returning 0.
2142 When ATTRP is set and returning 1, ATTRP[2] gets set to the index into
2143 shift_amounts for the last shift value that is to be used before the
2144 sign extend. */
2146 shl_and_kind (rtx left_rtx, rtx mask_rtx, int *attrp)
2148 unsigned HOST_WIDE_INT mask, lsb, mask2, lsb2;
2149 int left = INTVAL (left_rtx), right;
2150 int best = 0;
2151 int cost, best_cost = 10000;
2152 int best_right = 0, best_len = 0;
2153 int i;
2154 int can_ext;
2156 if (left < 0 || left > 31)
2157 return 0;
2158 if (GET_CODE (mask_rtx) == CONST_INT)
2159 mask = (unsigned HOST_WIDE_INT) INTVAL (mask_rtx) >> left;
2160 else
2161 mask = (unsigned HOST_WIDE_INT) GET_MODE_MASK (SImode) >> left;
2162 /* Can this be expressed as a right shift / left shift pair? */
2163 lsb = ((mask ^ (mask - 1)) >> 1) + 1;
2164 right = exact_log2 (lsb);
2165 mask2 = ~(mask + lsb - 1);
2166 lsb2 = ((mask2 ^ (mask2 - 1)) >> 1) + 1;
2167 /* mask has no zeroes but trailing zeroes <==> ! mask2 */
2168 if (! mask2)
2169 best_cost = shift_insns[right] + shift_insns[right + left];
2170 /* mask has no trailing zeroes <==> ! right */
2171 else if (! right && mask2 == ~(lsb2 - 1))
2173 int late_right = exact_log2 (lsb2);
2174 best_cost = shift_insns[left + late_right] + shift_insns[late_right];
2176 /* Try to use zero extend. */
2177 if (mask2 == ~(lsb2 - 1))
2179 int width, first;
2181 for (width = 8; width <= 16; width += 8)
2183 /* Can we zero-extend right away? */
2184 if (lsb2 == (unsigned HOST_WIDE_INT) 1 << width)
2186 cost
2187 = 1 + ext_shift_insns[right] + ext_shift_insns[left + right];
2188 if (cost < best_cost)
2190 best = 1;
2191 best_cost = cost;
2192 best_right = right;
2193 best_len = cost;
2194 if (attrp)
2195 attrp[2] = -1;
2197 continue;
2199 /* ??? Could try to put zero extend into initial right shift,
2200 or even shift a bit left before the right shift. */
2201 /* Determine value of first part of left shift, to get to the
2202 zero extend cut-off point. */
2203 first = width - exact_log2 (lsb2) + right;
2204 if (first >= 0 && right + left - first >= 0)
2206 cost = ext_shift_insns[right] + ext_shift_insns[first] + 1
2207 + ext_shift_insns[right + left - first];
2208 if (cost < best_cost)
2210 best = 1;
2211 best_cost = cost;
2212 best_right = right;
2213 best_len = cost;
2214 if (attrp)
2215 attrp[2] = first;
2220 /* Try to use r0 AND pattern */
2221 for (i = 0; i <= 2; i++)
2223 if (i > right)
2224 break;
2225 if (! CONST_OK_FOR_K08 (mask >> i))
2226 continue;
2227 cost = (i != 0) + 2 + ext_shift_insns[left + i];
2228 if (cost < best_cost)
2230 best = 2;
2231 best_cost = cost;
2232 best_right = i;
2233 best_len = cost - 1;
2236 /* Try to use a scratch register to hold the AND operand. */
2237 can_ext = ((mask << left) & ((unsigned HOST_WIDE_INT) 3 << 30)) == 0;
2238 for (i = 0; i <= 2; i++)
2240 if (i > right)
2241 break;
2242 cost = (i != 0) + (CONST_OK_FOR_I08 (mask >> i) ? 2 : 3)
2243 + (can_ext ? ext_shift_insns : shift_insns)[left + i];
2244 if (cost < best_cost)
2246 best = 4 - can_ext;
2247 best_cost = cost;
2248 best_right = i;
2249 best_len = cost - 1 - ! CONST_OK_FOR_I08 (mask >> i);
2253 if (attrp)
2255 attrp[0] = best_right;
2256 attrp[1] = best_len;
2258 return best;
2261 /* This is used in length attributes of the unnamed instructions
2262 corresponding to shl_and_kind return values of 1 and 2. */
2264 shl_and_length (rtx insn)
2266 rtx set_src, left_rtx, mask_rtx;
2267 int attributes[3];
2269 set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
2270 left_rtx = XEXP (XEXP (set_src, 0), 1);
2271 mask_rtx = XEXP (set_src, 1);
2272 shl_and_kind (left_rtx, mask_rtx, attributes);
2273 return attributes[1];
2276 /* This is used in length attribute of the and_shl_scratch instruction. */
2279 shl_and_scr_length (rtx insn)
2281 rtx set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
2282 int len = shift_insns[INTVAL (XEXP (set_src, 1))];
2283 rtx op = XEXP (set_src, 0);
2284 len += shift_insns[INTVAL (XEXP (op, 1))] + 1;
2285 op = XEXP (XEXP (op, 0), 0);
2286 return len + shift_insns[INTVAL (XEXP (op, 1))];
2289 /* Generating rtl? */
2290 extern int rtx_equal_function_value_matters;
2292 /* Generate rtl for instructions for which shl_and_kind advised a particular
2293 method of generating them, i.e. returned zero. */
2296 gen_shl_and (rtx dest, rtx left_rtx, rtx mask_rtx, rtx source)
2298 int attributes[3];
2299 unsigned HOST_WIDE_INT mask;
2300 int kind = shl_and_kind (left_rtx, mask_rtx, attributes);
2301 int right, total_shift;
2302 void (*shift_gen_fun) (int, rtx *) = gen_shifty_hi_op;
2304 right = attributes[0];
2305 total_shift = INTVAL (left_rtx) + right;
2306 mask = (unsigned HOST_WIDE_INT) INTVAL (mask_rtx) >> total_shift;
2307 switch (kind)
2309 default:
2310 return -1;
2311 case 1:
2313 int first = attributes[2];
2314 rtx operands[3];
2316 if (first < 0)
2318 emit_insn ((mask << right) <= 0xff
2319 ? gen_zero_extendqisi2 (dest,
2320 gen_lowpart (QImode, source))
2321 : gen_zero_extendhisi2 (dest,
2322 gen_lowpart (HImode, source)));
2323 source = dest;
2325 if (source != dest)
2326 emit_insn (gen_movsi (dest, source));
2327 operands[0] = dest;
2328 if (right)
2330 operands[2] = GEN_INT (right);
2331 gen_shifty_hi_op (LSHIFTRT, operands);
2333 if (first > 0)
2335 operands[2] = GEN_INT (first);
2336 gen_shifty_hi_op (ASHIFT, operands);
2337 total_shift -= first;
2338 mask <<= first;
2340 if (first >= 0)
2341 emit_insn (mask <= 0xff
2342 ? gen_zero_extendqisi2 (dest, gen_lowpart (QImode, dest))
2343 : gen_zero_extendhisi2 (dest, gen_lowpart (HImode, dest)));
2344 if (total_shift > 0)
2346 operands[2] = GEN_INT (total_shift);
2347 gen_shifty_hi_op (ASHIFT, operands);
2349 break;
2351 case 4:
2352 shift_gen_fun = gen_shifty_op;
2353 case 3:
2354 /* If the topmost bit that matters is set, set the topmost bits
2355 that don't matter. This way, we might be able to get a shorter
2356 signed constant. */
2357 if (mask & ((HOST_WIDE_INT) 1 << (31 - total_shift)))
2358 mask |= (HOST_WIDE_INT) ~0 << (31 - total_shift);
2359 case 2:
2360 /* Don't expand fine-grained when combining, because that will
2361 make the pattern fail. */
2362 if (rtx_equal_function_value_matters
2363 || reload_in_progress || reload_completed)
2365 rtx operands[3];
2367 /* Cases 3 and 4 should be handled by this split
2368 only while combining */
2369 if (kind > 2)
2370 abort ();
2371 if (right)
2373 emit_insn (gen_lshrsi3 (dest, source, GEN_INT (right)));
2374 source = dest;
2376 emit_insn (gen_andsi3 (dest, source, GEN_INT (mask)));
2377 if (total_shift)
2379 operands[0] = dest;
2380 operands[1] = dest;
2381 operands[2] = GEN_INT (total_shift);
2382 shift_gen_fun (ASHIFT, operands);
2384 break;
2386 else
2388 int neg = 0;
2389 if (kind != 4 && total_shift < 16)
2391 neg = -ext_shift_amounts[total_shift][1];
2392 if (neg > 0)
2393 neg -= ext_shift_amounts[total_shift][2];
2394 else
2395 neg = 0;
2397 emit_insn (gen_and_shl_scratch (dest, source,
2398 GEN_INT (right),
2399 GEN_INT (mask),
2400 GEN_INT (total_shift + neg),
2401 GEN_INT (neg)));
2402 emit_insn (gen_movsi (dest, dest));
2403 break;
2406 return 0;
2409 /* Try to find a good way to implement the combiner pattern
2410 [(set (match_operand:SI 0 "register_operand" "=r")
2411 (sign_extract:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
2412 (match_operand:SI 2 "const_int_operand" "n")
2413 (match_operand:SI 3 "const_int_operand" "n")
2414 (const_int 0)))
2415 (clobber (reg:SI T_REG))]
2416 LEFT_RTX is operand 2 in the above pattern, and SIZE_RTX is operand 3.
2417 return 0 for simple left / right shift combination.
2418 return 1 for left shift / 8 bit sign extend / left shift.
2419 return 2 for left shift / 16 bit sign extend / left shift.
2420 return 3 for left shift / 8 bit sign extend / shift / sign extend.
2421 return 4 for left shift / 16 bit sign extend / shift / sign extend.
2422 return 5 for left shift / 16 bit sign extend / right shift
2423 return 6 for < 8 bit sign extend / left shift.
2424 return 7 for < 8 bit sign extend / left shift / single right shift.
2425 If COSTP is nonzero, assign the calculated cost to *COSTP. */
2428 shl_sext_kind (rtx left_rtx, rtx size_rtx, int *costp)
2430 int left, size, insize, ext;
2431 int cost = 0, best_cost;
2432 int kind;
2434 left = INTVAL (left_rtx);
2435 size = INTVAL (size_rtx);
2436 insize = size - left;
2437 if (insize <= 0)
2438 abort ();
2439 /* Default to left / right shift. */
2440 kind = 0;
2441 best_cost = shift_insns[32 - insize] + ashiftrt_insns[32 - size];
2442 if (size <= 16)
2444 /* 16 bit shift / sign extend / 16 bit shift */
2445 cost = shift_insns[16 - insize] + 1 + ashiftrt_insns[16 - size];
2446 /* If ashiftrt_insns[16 - size] is 8, this choice will be overridden
2447 below, by alternative 3 or something even better. */
2448 if (cost < best_cost)
2450 kind = 5;
2451 best_cost = cost;
2454 /* Try a plain sign extend between two shifts. */
2455 for (ext = 16; ext >= insize; ext -= 8)
2457 if (ext <= size)
2459 cost = ext_shift_insns[ext - insize] + 1 + shift_insns[size - ext];
2460 if (cost < best_cost)
2462 kind = ext / (unsigned) 8;
2463 best_cost = cost;
2466 /* Check if we can do a sloppy shift with a final signed shift
2467 restoring the sign. */
2468 if (EXT_SHIFT_SIGNED (size - ext))
2469 cost = ext_shift_insns[ext - insize] + ext_shift_insns[size - ext] + 1;
2470 /* If not, maybe it's still cheaper to do the second shift sloppy,
2471 and do a final sign extend? */
2472 else if (size <= 16)
2473 cost = ext_shift_insns[ext - insize] + 1
2474 + ext_shift_insns[size > ext ? size - ext : ext - size] + 1;
2475 else
2476 continue;
2477 if (cost < best_cost)
2479 kind = ext / (unsigned) 8 + 2;
2480 best_cost = cost;
2483 /* Check if we can sign extend in r0 */
2484 if (insize < 8)
2486 cost = 3 + shift_insns[left];
2487 if (cost < best_cost)
2489 kind = 6;
2490 best_cost = cost;
2492 /* Try the same with a final signed shift. */
2493 if (left < 31)
2495 cost = 3 + ext_shift_insns[left + 1] + 1;
2496 if (cost < best_cost)
2498 kind = 7;
2499 best_cost = cost;
2503 if (TARGET_SH3)
2505 /* Try to use a dynamic shift. */
2506 cost = shift_insns[32 - insize] + 1 + SH_DYNAMIC_SHIFT_COST;
2507 if (cost < best_cost)
2509 kind = 0;
2510 best_cost = cost;
2513 if (costp)
2514 *costp = cost;
2515 return kind;
2518 /* Function to be used in the length attribute of the instructions
2519 implementing this pattern. */
2522 shl_sext_length (rtx insn)
2524 rtx set_src, left_rtx, size_rtx;
2525 int cost;
2527 set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
2528 left_rtx = XEXP (XEXP (set_src, 0), 1);
2529 size_rtx = XEXP (set_src, 1);
2530 shl_sext_kind (left_rtx, size_rtx, &cost);
2531 return cost;
2534 /* Generate rtl for this pattern */
2537 gen_shl_sext (rtx dest, rtx left_rtx, rtx size_rtx, rtx source)
2539 int kind;
2540 int left, size, insize, cost;
2541 rtx operands[3];
2543 kind = shl_sext_kind (left_rtx, size_rtx, &cost);
2544 left = INTVAL (left_rtx);
2545 size = INTVAL (size_rtx);
2546 insize = size - left;
2547 switch (kind)
2549 case 1:
2550 case 2:
2551 case 3:
2552 case 4:
2554 int ext = kind & 1 ? 8 : 16;
2555 int shift2 = size - ext;
2557 /* Don't expand fine-grained when combining, because that will
2558 make the pattern fail. */
2559 if (! rtx_equal_function_value_matters
2560 && ! reload_in_progress && ! reload_completed)
2562 emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
2563 emit_insn (gen_movsi (dest, source));
2564 break;
2566 if (dest != source)
2567 emit_insn (gen_movsi (dest, source));
2568 operands[0] = dest;
2569 if (ext - insize)
2571 operands[2] = GEN_INT (ext - insize);
2572 gen_shifty_hi_op (ASHIFT, operands);
2574 emit_insn (kind & 1
2575 ? gen_extendqisi2 (dest, gen_lowpart (QImode, dest))
2576 : gen_extendhisi2 (dest, gen_lowpart (HImode, dest)));
2577 if (kind <= 2)
2579 if (shift2)
2581 operands[2] = GEN_INT (shift2);
2582 gen_shifty_op (ASHIFT, operands);
2585 else
2587 if (shift2 > 0)
2589 if (EXT_SHIFT_SIGNED (shift2))
2591 operands[2] = GEN_INT (shift2 + 1);
2592 gen_shifty_op (ASHIFT, operands);
2593 operands[2] = const1_rtx;
2594 gen_shifty_op (ASHIFTRT, operands);
2595 break;
2597 operands[2] = GEN_INT (shift2);
2598 gen_shifty_hi_op (ASHIFT, operands);
2600 else if (shift2)
2602 operands[2] = GEN_INT (-shift2);
2603 gen_shifty_hi_op (LSHIFTRT, operands);
2605 emit_insn (size <= 8
2606 ? gen_extendqisi2 (dest, gen_lowpart (QImode, dest))
2607 : gen_extendhisi2 (dest, gen_lowpart (HImode, dest)));
2609 break;
2611 case 5:
2613 int i = 16 - size;
2614 if (! rtx_equal_function_value_matters
2615 && ! reload_in_progress && ! reload_completed)
2616 emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
2617 else
2619 operands[0] = dest;
2620 operands[2] = GEN_INT (16 - insize);
2621 gen_shifty_hi_op (ASHIFT, operands);
2622 emit_insn (gen_extendhisi2 (dest, gen_lowpart (HImode, dest)));
2624 /* Don't use gen_ashrsi3 because it generates new pseudos. */
2625 while (--i >= 0)
2626 gen_ashift (ASHIFTRT, 1, dest);
2627 break;
2629 case 6:
2630 case 7:
2631 /* Don't expand fine-grained when combining, because that will
2632 make the pattern fail. */
2633 if (! rtx_equal_function_value_matters
2634 && ! reload_in_progress && ! reload_completed)
2636 emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
2637 emit_insn (gen_movsi (dest, source));
2638 break;
2640 emit_insn (gen_andsi3 (dest, source, GEN_INT ((1 << insize) - 1)));
2641 emit_insn (gen_xorsi3 (dest, dest, GEN_INT (1 << (insize - 1))));
2642 emit_insn (gen_addsi3 (dest, dest, GEN_INT (-1 << (insize - 1))));
2643 operands[0] = dest;
2644 operands[2] = kind == 7 ? GEN_INT (left + 1) : left_rtx;
2645 gen_shifty_op (ASHIFT, operands);
2646 if (kind == 7)
2647 emit_insn (gen_ashrsi3_k (dest, dest, const1_rtx));
2648 break;
2649 default:
2650 return -1;
2652 return 0;
2655 /* Prefix a symbol_ref name with "datalabel". */
2658 gen_datalabel_ref (rtx sym)
2660 if (GET_CODE (sym) == LABEL_REF)
2661 return gen_rtx_CONST (GET_MODE (sym),
2662 gen_rtx_UNSPEC (GET_MODE (sym),
2663 gen_rtvec (1, sym),
2664 UNSPEC_DATALABEL));
2666 if (GET_CODE (sym) != SYMBOL_REF)
2667 abort ();
2669 return sym;
2673 /* The SH cannot load a large constant into a register, constants have to
2674 come from a pc relative load. The reference of a pc relative load
2675 instruction must be less than 1k infront of the instruction. This
2676 means that we often have to dump a constant inside a function, and
2677 generate code to branch around it.
2679 It is important to minimize this, since the branches will slow things
2680 down and make things bigger.
2682 Worst case code looks like:
2684 mov.l L1,rn
2685 bra L2
2687 align
2688 L1: .long value
2692 mov.l L3,rn
2693 bra L4
2695 align
2696 L3: .long value
2700 We fix this by performing a scan before scheduling, which notices which
2701 instructions need to have their operands fetched from the constant table
2702 and builds the table.
2704 The algorithm is:
2706 scan, find an instruction which needs a pcrel move. Look forward, find the
2707 last barrier which is within MAX_COUNT bytes of the requirement.
2708 If there isn't one, make one. Process all the instructions between
2709 the find and the barrier.
2711 In the above example, we can tell that L3 is within 1k of L1, so
2712 the first move can be shrunk from the 3 insn+constant sequence into
2713 just 1 insn, and the constant moved to L3 to make:
2715 mov.l L1,rn
2717 mov.l L3,rn
2718 bra L4
2720 align
2721 L3:.long value
2722 L4:.long value
2724 Then the second move becomes the target for the shortening process. */
2726 typedef struct
2728 rtx value; /* Value in table. */
2729 rtx label; /* Label of value. */
2730 rtx wend; /* End of window. */
2731 enum machine_mode mode; /* Mode of value. */
2733 /* True if this constant is accessed as part of a post-increment
2734 sequence. Note that HImode constants are never accessed in this way. */
2735 bool part_of_sequence_p;
2736 } pool_node;
2738 /* The maximum number of constants that can fit into one pool, since
2739 the pc relative range is 0...1020 bytes and constants are at least 4
2740 bytes long. */
2742 #define MAX_POOL_SIZE (1020/4)
2743 static pool_node pool_vector[MAX_POOL_SIZE];
2744 static int pool_size;
2745 static rtx pool_window_label;
2746 static int pool_window_last;
2748 /* ??? If we need a constant in HImode which is the truncated value of a
2749 constant we need in SImode, we could combine the two entries thus saving
2750 two bytes. Is this common enough to be worth the effort of implementing
2751 it? */
2753 /* ??? This stuff should be done at the same time that we shorten branches.
2754 As it is now, we must assume that all branches are the maximum size, and
2755 this causes us to almost always output constant pools sooner than
2756 necessary. */
2758 /* Add a constant to the pool and return its label. */
2760 static rtx
2761 add_constant (rtx x, enum machine_mode mode, rtx last_value)
2763 int i;
2764 rtx lab, new, ref, newref;
2766 /* First see if we've already got it. */
2767 for (i = 0; i < pool_size; i++)
2769 if (x->code == pool_vector[i].value->code
2770 && mode == pool_vector[i].mode)
2772 if (x->code == CODE_LABEL)
2774 if (XINT (x, 3) != XINT (pool_vector[i].value, 3))
2775 continue;
2777 if (rtx_equal_p (x, pool_vector[i].value))
2779 lab = new = 0;
2780 if (! last_value
2781 || ! i
2782 || ! rtx_equal_p (last_value, pool_vector[i-1].value))
2784 new = gen_label_rtx ();
2785 LABEL_REFS (new) = pool_vector[i].label;
2786 pool_vector[i].label = lab = new;
2788 if (lab && pool_window_label)
2790 newref = gen_rtx_LABEL_REF (VOIDmode, pool_window_label);
2791 ref = pool_vector[pool_window_last].wend;
2792 LABEL_NEXTREF (newref) = ref;
2793 pool_vector[pool_window_last].wend = newref;
2795 if (new)
2796 pool_window_label = new;
2797 pool_window_last = i;
2798 return lab;
2803 /* Need a new one. */
2804 pool_vector[pool_size].value = x;
2805 if (last_value && rtx_equal_p (last_value, pool_vector[pool_size - 1].value))
2807 lab = 0;
2808 pool_vector[pool_size - 1].part_of_sequence_p = true;
2810 else
2811 lab = gen_label_rtx ();
2812 pool_vector[pool_size].mode = mode;
2813 pool_vector[pool_size].label = lab;
2814 pool_vector[pool_size].wend = NULL_RTX;
2815 pool_vector[pool_size].part_of_sequence_p = (lab == 0);
2816 if (lab && pool_window_label)
2818 newref = gen_rtx_LABEL_REF (VOIDmode, pool_window_label);
2819 ref = pool_vector[pool_window_last].wend;
2820 LABEL_NEXTREF (newref) = ref;
2821 pool_vector[pool_window_last].wend = newref;
2823 if (lab)
2824 pool_window_label = lab;
2825 pool_window_last = pool_size;
2826 pool_size++;
2827 return lab;
2830 /* Output the literal table. START, if nonzero, is the first instruction
2831 this table is needed for, and also indicates that there is at least one
2832 casesi_worker_2 instruction; We have to emit the operand3 labels from
2833 these insns at a 4-byte aligned position. BARRIER is the barrier
2834 after which we are to place the table. */
2836 static void
2837 dump_table (rtx start, rtx barrier)
2839 rtx scan = barrier;
2840 int i;
2841 int need_align = 1;
2842 rtx lab, ref;
2843 int have_df = 0;
2845 /* Do two passes, first time dump out the HI sized constants. */
2847 for (i = 0; i < pool_size; i++)
2849 pool_node *p = &pool_vector[i];
2851 if (p->mode == HImode)
2853 if (need_align)
2855 scan = emit_insn_after (gen_align_2 (), scan);
2856 need_align = 0;
2858 for (lab = p->label; lab; lab = LABEL_REFS (lab))
2859 scan = emit_label_after (lab, scan);
2860 scan = emit_insn_after (gen_consttable_2 (p->value, const0_rtx),
2861 scan);
2862 for (ref = p->wend; ref; ref = LABEL_NEXTREF (ref))
2864 lab = XEXP (ref, 0);
2865 scan = emit_insn_after (gen_consttable_window_end (lab), scan);
2868 else if (p->mode == DFmode)
2869 have_df = 1;
2872 need_align = 1;
2874 if (start)
2876 scan = emit_insn_after (gen_align_4 (), scan);
2877 need_align = 0;
2878 for (; start != barrier; start = NEXT_INSN (start))
2879 if (GET_CODE (start) == INSN
2880 && recog_memoized (start) == CODE_FOR_casesi_worker_2)
2882 rtx src = SET_SRC (XVECEXP (PATTERN (start), 0, 0));
2883 rtx lab = XEXP (XVECEXP (src, 0, 3), 0);
2885 scan = emit_label_after (lab, scan);
2888 if (TARGET_FMOVD && TARGET_ALIGN_DOUBLE && have_df)
2890 rtx align_insn = NULL_RTX;
2892 scan = emit_label_after (gen_label_rtx (), scan);
2893 scan = emit_insn_after (gen_align_log (GEN_INT (3)), scan);
2894 need_align = 0;
2896 for (i = 0; i < pool_size; i++)
2898 pool_node *p = &pool_vector[i];
2900 switch (p->mode)
2902 case HImode:
2903 break;
2904 case SImode:
2905 case SFmode:
2906 if (align_insn && !p->part_of_sequence_p)
2908 for (lab = p->label; lab; lab = LABEL_REFS (lab))
2909 emit_label_before (lab, align_insn);
2910 emit_insn_before (gen_consttable_4 (p->value, const0_rtx),
2911 align_insn);
2912 for (ref = p->wend; ref; ref = LABEL_NEXTREF (ref))
2914 lab = XEXP (ref, 0);
2915 emit_insn_before (gen_consttable_window_end (lab),
2916 align_insn);
2918 delete_insn (align_insn);
2919 align_insn = NULL_RTX;
2920 continue;
2922 else
2924 for (lab = p->label; lab; lab = LABEL_REFS (lab))
2925 scan = emit_label_after (lab, scan);
2926 scan = emit_insn_after (gen_consttable_4 (p->value,
2927 const0_rtx), scan);
2928 need_align = ! need_align;
2930 break;
2931 case DFmode:
2932 if (need_align)
2934 scan = emit_insn_after (gen_align_log (GEN_INT (3)), scan);
2935 align_insn = scan;
2936 need_align = 0;
2938 case DImode:
2939 for (lab = p->label; lab; lab = LABEL_REFS (lab))
2940 scan = emit_label_after (lab, scan);
2941 scan = emit_insn_after (gen_consttable_8 (p->value, const0_rtx),
2942 scan);
2943 break;
2944 default:
2945 abort ();
2946 break;
2949 if (p->mode != HImode)
2951 for (ref = p->wend; ref; ref = LABEL_NEXTREF (ref))
2953 lab = XEXP (ref, 0);
2954 scan = emit_insn_after (gen_consttable_window_end (lab),
2955 scan);
2960 pool_size = 0;
2963 for (i = 0; i < pool_size; i++)
2965 pool_node *p = &pool_vector[i];
2967 switch (p->mode)
2969 case HImode:
2970 break;
2971 case SImode:
2972 case SFmode:
2973 if (need_align)
2975 need_align = 0;
2976 scan = emit_label_after (gen_label_rtx (), scan);
2977 scan = emit_insn_after (gen_align_4 (), scan);
2979 for (lab = p->label; lab; lab = LABEL_REFS (lab))
2980 scan = emit_label_after (lab, scan);
2981 scan = emit_insn_after (gen_consttable_4 (p->value, const0_rtx),
2982 scan);
2983 break;
2984 case DFmode:
2985 case DImode:
2986 if (need_align)
2988 need_align = 0;
2989 scan = emit_label_after (gen_label_rtx (), scan);
2990 scan = emit_insn_after (gen_align_4 (), scan);
2992 for (lab = p->label; lab; lab = LABEL_REFS (lab))
2993 scan = emit_label_after (lab, scan);
2994 scan = emit_insn_after (gen_consttable_8 (p->value, const0_rtx),
2995 scan);
2996 break;
2997 default:
2998 abort ();
2999 break;
3002 if (p->mode != HImode)
3004 for (ref = p->wend; ref; ref = LABEL_NEXTREF (ref))
3006 lab = XEXP (ref, 0);
3007 scan = emit_insn_after (gen_consttable_window_end (lab), scan);
3012 scan = emit_insn_after (gen_consttable_end (), scan);
3013 scan = emit_barrier_after (scan);
3014 pool_size = 0;
3015 pool_window_label = NULL_RTX;
3016 pool_window_last = 0;
3019 /* Return nonzero if constant would be an ok source for a
3020 mov.w instead of a mov.l. */
3022 static int
3023 hi_const (rtx src)
3025 return (GET_CODE (src) == CONST_INT
3026 && INTVAL (src) >= -32768
3027 && INTVAL (src) <= 32767);
3030 /* Nonzero if the insn is a move instruction which needs to be fixed. */
3032 /* ??? For a DImode/DFmode moves, we don't need to fix it if each half of the
3033 CONST_DOUBLE input value is CONST_OK_FOR_I08. For a SFmode move, we don't
3034 need to fix it if the input value is CONST_OK_FOR_I08. */
3036 static int
3037 broken_move (rtx insn)
3039 if (GET_CODE (insn) == INSN)
3041 rtx pat = PATTERN (insn);
3042 if (GET_CODE (pat) == PARALLEL)
3043 pat = XVECEXP (pat, 0, 0);
3044 if (GET_CODE (pat) == SET
3045 /* We can load any 8 bit value if we don't care what the high
3046 order bits end up as. */
3047 && GET_MODE (SET_DEST (pat)) != QImode
3048 && (CONSTANT_P (SET_SRC (pat))
3049 /* Match mova_const. */
3050 || (GET_CODE (SET_SRC (pat)) == UNSPEC
3051 && XINT (SET_SRC (pat), 1) == UNSPEC_MOVA
3052 && GET_CODE (XVECEXP (SET_SRC (pat), 0, 0)) == CONST))
3053 && ! (TARGET_SH2E
3054 && GET_CODE (SET_SRC (pat)) == CONST_DOUBLE
3055 && (fp_zero_operand (SET_SRC (pat))
3056 || fp_one_operand (SET_SRC (pat)))
3057 /* ??? If this is a -m4 or -m4-single compilation, in general
3058 we don't know the current setting of fpscr, so disable fldi.
3059 There is an exception if this was a register-register move
3060 before reload - and hence it was ascertained that we have
3061 single precision setting - and in a post-reload optimization
3062 we changed this to do a constant load. In that case
3063 we don't have an r0 clobber, hence we must use fldi. */
3064 && (! TARGET_SH4 || TARGET_FMOVD
3065 || (GET_CODE (XEXP (XVECEXP (PATTERN (insn), 0, 2), 0))
3066 == SCRATCH))
3067 && GET_CODE (SET_DEST (pat)) == REG
3068 && FP_REGISTER_P (REGNO (SET_DEST (pat))))
3069 && (GET_CODE (SET_SRC (pat)) != CONST_INT
3070 || ! CONST_OK_FOR_I08 (INTVAL (SET_SRC (pat)))))
3071 return 1;
3074 return 0;
3077 static int
3078 mova_p (rtx insn)
3080 return (GET_CODE (insn) == INSN
3081 && GET_CODE (PATTERN (insn)) == SET
3082 && GET_CODE (SET_SRC (PATTERN (insn))) == UNSPEC
3083 && XINT (SET_SRC (PATTERN (insn)), 1) == UNSPEC_MOVA
3084 /* Don't match mova_const. */
3085 && GET_CODE (XVECEXP (SET_SRC (PATTERN (insn)), 0, 0)) == LABEL_REF);
3088 /* Fix up a mova from a switch that went out of range. */
3089 static void
3090 fixup_mova (rtx mova)
3092 if (! flag_pic)
3094 SET_SRC (PATTERN (mova)) = XVECEXP (SET_SRC (PATTERN (mova)), 0, 0);
3095 INSN_CODE (mova) = -1;
3097 else
3099 rtx worker = mova;
3100 rtx lab = gen_label_rtx ();
3101 rtx wpat, wpat0, wpat1, wsrc, diff;
3105 worker = NEXT_INSN (worker);
3106 if (! worker
3107 || GET_CODE (worker) == CODE_LABEL
3108 || GET_CODE (worker) == JUMP_INSN)
3109 abort ();
3110 } while (recog_memoized (worker) != CODE_FOR_casesi_worker_1);
3111 wpat = PATTERN (worker);
3112 wpat0 = XVECEXP (wpat, 0, 0);
3113 wpat1 = XVECEXP (wpat, 0, 1);
3114 wsrc = SET_SRC (wpat0);
3115 PATTERN (worker) = (gen_casesi_worker_2
3116 (SET_DEST (wpat0), XVECEXP (wsrc, 0, 1),
3117 XEXP (XVECEXP (wsrc, 0, 2), 0), lab,
3118 XEXP (wpat1, 0)));
3119 INSN_CODE (worker) = -1;
3120 diff = gen_rtx_MINUS (Pmode, XVECEXP (SET_SRC (PATTERN (mova)), 0, 0),
3121 gen_rtx_LABEL_REF (Pmode, lab));
3122 diff = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, diff), UNSPEC_PIC);
3123 SET_SRC (PATTERN (mova)) = gen_rtx_CONST (Pmode, diff);
3124 INSN_CODE (mova) = -1;
3128 /* Find the last barrier from insn FROM which is close enough to hold the
3129 constant pool. If we can't find one, then create one near the end of
3130 the range. */
3132 static rtx
3133 find_barrier (int num_mova, rtx mova, rtx from)
3135 int count_si = 0;
3136 int count_hi = 0;
3137 int found_hi = 0;
3138 int found_si = 0;
3139 int found_di = 0;
3140 int hi_align = 2;
3141 int si_align = 2;
3142 int leading_mova = num_mova;
3143 rtx barrier_before_mova = 0, found_barrier = 0, good_barrier = 0;
3144 int si_limit;
3145 int hi_limit;
3147 /* For HImode: range is 510, add 4 because pc counts from address of
3148 second instruction after this one, subtract 2 for the jump instruction
3149 that we may need to emit before the table, subtract 2 for the instruction
3150 that fills the jump delay slot (in very rare cases, reorg will take an
3151 instruction from after the constant pool or will leave the delay slot
3152 empty). This gives 510.
3153 For SImode: range is 1020, add 4 because pc counts from address of
3154 second instruction after this one, subtract 2 in case pc is 2 byte
3155 aligned, subtract 2 for the jump instruction that we may need to emit
3156 before the table, subtract 2 for the instruction that fills the jump
3157 delay slot. This gives 1018. */
3159 /* The branch will always be shortened now that the reference address for
3160 forward branches is the successor address, thus we need no longer make
3161 adjustments to the [sh]i_limit for -O0. */
3163 si_limit = 1018;
3164 hi_limit = 510;
3166 while (from && count_si < si_limit && count_hi < hi_limit)
3168 int inc = get_attr_length (from);
3169 int new_align = 1;
3171 if (GET_CODE (from) == CODE_LABEL)
3173 if (optimize)
3174 new_align = 1 << label_to_alignment (from);
3175 else if (GET_CODE (prev_nonnote_insn (from)) == BARRIER)
3176 new_align = 1 << barrier_align (from);
3177 else
3178 new_align = 1;
3179 inc = 0;
3182 if (GET_CODE (from) == BARRIER)
3185 found_barrier = from;
3187 /* If we are at the end of the function, or in front of an alignment
3188 instruction, we need not insert an extra alignment. We prefer
3189 this kind of barrier. */
3190 if (barrier_align (from) > 2)
3191 good_barrier = from;
3194 if (broken_move (from))
3196 rtx pat, src, dst;
3197 enum machine_mode mode;
3199 pat = PATTERN (from);
3200 if (GET_CODE (pat) == PARALLEL)
3201 pat = XVECEXP (pat, 0, 0);
3202 src = SET_SRC (pat);
3203 dst = SET_DEST (pat);
3204 mode = GET_MODE (dst);
3206 /* We must explicitly check the mode, because sometimes the
3207 front end will generate code to load unsigned constants into
3208 HImode targets without properly sign extending them. */
3209 if (mode == HImode
3210 || (mode == SImode && hi_const (src) && REGNO (dst) != FPUL_REG))
3212 found_hi += 2;
3213 /* We put the short constants before the long constants, so
3214 we must count the length of short constants in the range
3215 for the long constants. */
3216 /* ??? This isn't optimal, but is easy to do. */
3217 si_limit -= 2;
3219 else
3221 /* We dump DF/DI constants before SF/SI ones, because
3222 the limit is the same, but the alignment requirements
3223 are higher. We may waste up to 4 additional bytes
3224 for alignment, and the DF/DI constant may have
3225 another SF/SI constant placed before it. */
3226 if (TARGET_SHCOMPACT
3227 && ! found_di
3228 && (mode == DFmode || mode == DImode))
3230 found_di = 1;
3231 si_limit -= 8;
3233 while (si_align > 2 && found_si + si_align - 2 > count_si)
3234 si_align >>= 1;
3235 if (found_si > count_si)
3236 count_si = found_si;
3237 found_si += GET_MODE_SIZE (mode);
3238 if (num_mova)
3239 si_limit -= GET_MODE_SIZE (mode);
3242 /* See the code in machine_dependent_reorg, which has a similar if
3243 statement that generates a new mova insn in many cases. */
3244 if (GET_CODE (dst) == REG && FP_ANY_REGISTER_P (REGNO (dst)))
3245 inc += 2;
3248 if (mova_p (from))
3250 if (! num_mova++)
3252 leading_mova = 0;
3253 mova = from;
3254 barrier_before_mova = good_barrier ? good_barrier : found_barrier;
3256 if (found_si > count_si)
3257 count_si = found_si;
3259 else if (GET_CODE (from) == JUMP_INSN
3260 && (GET_CODE (PATTERN (from)) == ADDR_VEC
3261 || GET_CODE (PATTERN (from)) == ADDR_DIFF_VEC))
3263 if (num_mova)
3264 num_mova--;
3265 if (barrier_align (next_real_insn (from)) == align_jumps_log)
3267 /* We have just passed the barrier in front of the
3268 ADDR_DIFF_VEC, which is stored in found_barrier. Since
3269 the ADDR_DIFF_VEC is accessed as data, just like our pool
3270 constants, this is a good opportunity to accommodate what
3271 we have gathered so far.
3272 If we waited any longer, we could end up at a barrier in
3273 front of code, which gives worse cache usage for separated
3274 instruction / data caches. */
3275 good_barrier = found_barrier;
3276 break;
3278 else
3280 rtx body = PATTERN (from);
3281 inc = XVECLEN (body, 1) * GET_MODE_SIZE (GET_MODE (body));
3284 /* For the SH1, we generate alignments even after jumps-around-jumps. */
3285 else if (GET_CODE (from) == JUMP_INSN
3286 && ! TARGET_SH2
3287 && ! TARGET_SMALLCODE)
3288 new_align = 4;
3290 if (found_si)
3292 count_si += inc;
3293 if (new_align > si_align)
3295 si_limit -= (count_si - 1) & (new_align - si_align);
3296 si_align = new_align;
3298 count_si = (count_si + new_align - 1) & -new_align;
3300 if (found_hi)
3302 count_hi += inc;
3303 if (new_align > hi_align)
3305 hi_limit -= (count_hi - 1) & (new_align - hi_align);
3306 hi_align = new_align;
3308 count_hi = (count_hi + new_align - 1) & -new_align;
3310 from = NEXT_INSN (from);
3313 if (num_mova)
3315 if (leading_mova)
3317 /* Try as we might, the leading mova is out of range. Change
3318 it into a load (which will become a pcload) and retry. */
3319 fixup_mova (mova);
3320 return find_barrier (0, 0, mova);
3322 else
3324 /* Insert the constant pool table before the mova instruction,
3325 to prevent the mova label reference from going out of range. */
3326 from = mova;
3327 good_barrier = found_barrier = barrier_before_mova;
3331 if (found_barrier)
3333 if (good_barrier && next_real_insn (found_barrier))
3334 found_barrier = good_barrier;
3336 else
3338 /* We didn't find a barrier in time to dump our stuff,
3339 so we'll make one. */
3340 rtx label = gen_label_rtx ();
3342 /* If we exceeded the range, then we must back up over the last
3343 instruction we looked at. Otherwise, we just need to undo the
3344 NEXT_INSN at the end of the loop. */
3345 if (count_hi > hi_limit || count_si > si_limit)
3346 from = PREV_INSN (PREV_INSN (from));
3347 else
3348 from = PREV_INSN (from);
3350 /* Walk back to be just before any jump or label.
3351 Putting it before a label reduces the number of times the branch
3352 around the constant pool table will be hit. Putting it before
3353 a jump makes it more likely that the bra delay slot will be
3354 filled. */
3355 while (GET_CODE (from) == JUMP_INSN || GET_CODE (from) == NOTE
3356 || GET_CODE (from) == CODE_LABEL)
3357 from = PREV_INSN (from);
3359 from = emit_jump_insn_after (gen_jump (label), from);
3360 JUMP_LABEL (from) = label;
3361 LABEL_NUSES (label) = 1;
3362 found_barrier = emit_barrier_after (from);
3363 emit_label_after (label, found_barrier);
3366 return found_barrier;
3369 /* If the instruction INSN is implemented by a special function, and we can
3370 positively find the register that is used to call the sfunc, and this
3371 register is not used anywhere else in this instruction - except as the
3372 destination of a set, return this register; else, return 0. */
3374 sfunc_uses_reg (rtx insn)
3376 int i;
3377 rtx pattern, part, reg_part, reg;
3379 if (GET_CODE (insn) != INSN)
3380 return 0;
3381 pattern = PATTERN (insn);
3382 if (GET_CODE (pattern) != PARALLEL || get_attr_type (insn) != TYPE_SFUNC)
3383 return 0;
3385 for (reg_part = 0, i = XVECLEN (pattern, 0) - 1; i >= 1; i--)
3387 part = XVECEXP (pattern, 0, i);
3388 if (GET_CODE (part) == USE && GET_MODE (XEXP (part, 0)) == SImode)
3389 reg_part = part;
3391 if (! reg_part)
3392 return 0;
3393 reg = XEXP (reg_part, 0);
3394 for (i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
3396 part = XVECEXP (pattern, 0, i);
3397 if (part == reg_part || GET_CODE (part) == CLOBBER)
3398 continue;
3399 if (reg_mentioned_p (reg, ((GET_CODE (part) == SET
3400 && GET_CODE (SET_DEST (part)) == REG)
3401 ? SET_SRC (part) : part)))
3402 return 0;
3404 return reg;
3407 /* See if the only way in which INSN uses REG is by calling it, or by
3408 setting it while calling it. Set *SET to a SET rtx if the register
3409 is set by INSN. */
3411 static int
3412 noncall_uses_reg (rtx reg, rtx insn, rtx *set)
3414 rtx pattern, reg2;
3416 *set = NULL_RTX;
3418 reg2 = sfunc_uses_reg (insn);
3419 if (reg2 && REGNO (reg2) == REGNO (reg))
3421 pattern = single_set (insn);
3422 if (pattern
3423 && GET_CODE (SET_DEST (pattern)) == REG
3424 && REGNO (reg) == REGNO (SET_DEST (pattern)))
3425 *set = pattern;
3426 return 0;
3428 if (GET_CODE (insn) != CALL_INSN)
3430 /* We don't use rtx_equal_p because we don't care if the mode is
3431 different. */
3432 pattern = single_set (insn);
3433 if (pattern
3434 && GET_CODE (SET_DEST (pattern)) == REG
3435 && REGNO (reg) == REGNO (SET_DEST (pattern)))
3437 rtx par, part;
3438 int i;
3440 *set = pattern;
3441 par = PATTERN (insn);
3442 if (GET_CODE (par) == PARALLEL)
3443 for (i = XVECLEN (par, 0) - 1; i >= 0; i--)
3445 part = XVECEXP (par, 0, i);
3446 if (GET_CODE (part) != SET && reg_mentioned_p (reg, part))
3447 return 1;
3449 return reg_mentioned_p (reg, SET_SRC (pattern));
3452 return 1;
3455 pattern = PATTERN (insn);
3457 if (GET_CODE (pattern) == PARALLEL)
3459 int i;
3461 for (i = XVECLEN (pattern, 0) - 1; i >= 1; i--)
3462 if (reg_mentioned_p (reg, XVECEXP (pattern, 0, i)))
3463 return 1;
3464 pattern = XVECEXP (pattern, 0, 0);
3467 if (GET_CODE (pattern) == SET)
3469 if (reg_mentioned_p (reg, SET_DEST (pattern)))
3471 /* We don't use rtx_equal_p, because we don't care if the
3472 mode is different. */
3473 if (GET_CODE (SET_DEST (pattern)) != REG
3474 || REGNO (reg) != REGNO (SET_DEST (pattern)))
3475 return 1;
3477 *set = pattern;
3480 pattern = SET_SRC (pattern);
3483 if (GET_CODE (pattern) != CALL
3484 || GET_CODE (XEXP (pattern, 0)) != MEM
3485 || ! rtx_equal_p (reg, XEXP (XEXP (pattern, 0), 0)))
3486 return 1;
3488 return 0;
3491 /* Given a X, a pattern of an insn or a part of it, return a mask of used
3492 general registers. Bits 0..15 mean that the respective registers
3493 are used as inputs in the instruction. Bits 16..31 mean that the
3494 registers 0..15, respectively, are used as outputs, or are clobbered.
3495 IS_DEST should be set to 16 if X is the destination of a SET, else to 0. */
3497 regs_used (rtx x, int is_dest)
3499 enum rtx_code code;
3500 const char *fmt;
3501 int i, used = 0;
3503 if (! x)
3504 return used;
3505 code = GET_CODE (x);
3506 switch (code)
3508 case REG:
3509 if (REGNO (x) < 16)
3510 return (((1 << HARD_REGNO_NREGS (0, GET_MODE (x))) - 1)
3511 << (REGNO (x) + is_dest));
3512 return 0;
3513 case SUBREG:
3515 rtx y = SUBREG_REG (x);
3517 if (GET_CODE (y) != REG)
3518 break;
3519 if (REGNO (y) < 16)
3520 return (((1 << HARD_REGNO_NREGS (0, GET_MODE (x))) - 1)
3521 << (REGNO (y) +
3522 subreg_regno_offset (REGNO (y),
3523 GET_MODE (y),
3524 SUBREG_BYTE (x),
3525 GET_MODE (x)) + is_dest));
3526 return 0;
3528 case SET:
3529 return regs_used (SET_SRC (x), 0) | regs_used (SET_DEST (x), 16);
3530 case RETURN:
3531 /* If there was a return value, it must have been indicated with USE. */
3532 return 0x00ffff00;
3533 case CLOBBER:
3534 is_dest = 1;
3535 break;
3536 case MEM:
3537 is_dest = 0;
3538 break;
3539 case CALL:
3540 used |= 0x00ff00f0;
3541 break;
3542 default:
3543 break;
3546 fmt = GET_RTX_FORMAT (code);
3548 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3550 if (fmt[i] == 'E')
3552 register int j;
3553 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3554 used |= regs_used (XVECEXP (x, i, j), is_dest);
3556 else if (fmt[i] == 'e')
3557 used |= regs_used (XEXP (x, i), is_dest);
3559 return used;
3562 /* Create an instruction that prevents redirection of a conditional branch
3563 to the destination of the JUMP with address ADDR.
3564 If the branch needs to be implemented as an indirect jump, try to find
3565 a scratch register for it.
3566 If NEED_BLOCK is 0, don't do anything unless we need a scratch register.
3567 If any preceding insn that doesn't fit into a delay slot is good enough,
3568 pass 1. Pass 2 if a definite blocking insn is needed.
3569 -1 is used internally to avoid deep recursion.
3570 If a blocking instruction is made or recognized, return it. */
3572 static rtx
3573 gen_block_redirect (rtx jump, int addr, int need_block)
3575 int dead = 0;
3576 rtx prev = prev_nonnote_insn (jump);
3577 rtx dest;
3579 /* First, check if we already have an instruction that satisfies our need. */
3580 if (prev && GET_CODE (prev) == INSN && ! INSN_DELETED_P (prev))
3582 if (INSN_CODE (prev) == CODE_FOR_indirect_jump_scratch)
3583 return prev;
3584 if (GET_CODE (PATTERN (prev)) == USE
3585 || GET_CODE (PATTERN (prev)) == CLOBBER
3586 || get_attr_in_delay_slot (prev) == IN_DELAY_SLOT_YES)
3587 prev = jump;
3588 else if ((need_block &= ~1) < 0)
3589 return prev;
3590 else if (recog_memoized (prev) == CODE_FOR_block_branch_redirect)
3591 need_block = 0;
3593 if (GET_CODE (PATTERN (jump)) == RETURN)
3595 if (! need_block)
3596 return prev;
3597 /* Reorg even does nasty things with return insns that cause branches
3598 to go out of range - see find_end_label and callers. */
3599 return emit_insn_before (gen_block_branch_redirect (const0_rtx) , jump);
3601 /* We can't use JUMP_LABEL here because it might be undefined
3602 when not optimizing. */
3603 dest = XEXP (SET_SRC (PATTERN (jump)), 0);
3604 /* If the branch is out of range, try to find a scratch register for it. */
3605 if (optimize
3606 && (INSN_ADDRESSES (INSN_UID (dest)) - addr + (unsigned) 4092
3607 > 4092 + 4098))
3609 rtx scan;
3610 /* Don't look for the stack pointer as a scratch register,
3611 it would cause trouble if an interrupt occurred. */
3612 unsigned try = 0x7fff, used;
3613 int jump_left = flag_expensive_optimizations + 1;
3615 /* It is likely that the most recent eligible instruction is wanted for
3616 the delay slot. Therefore, find out which registers it uses, and
3617 try to avoid using them. */
3619 for (scan = jump; (scan = PREV_INSN (scan)); )
3621 enum rtx_code code;
3623 if (INSN_DELETED_P (scan))
3624 continue;
3625 code = GET_CODE (scan);
3626 if (code == CODE_LABEL || code == JUMP_INSN)
3627 break;
3628 if (code == INSN
3629 && GET_CODE (PATTERN (scan)) != USE
3630 && GET_CODE (PATTERN (scan)) != CLOBBER
3631 && get_attr_in_delay_slot (scan) == IN_DELAY_SLOT_YES)
3633 try &= ~regs_used (PATTERN (scan), 0);
3634 break;
3637 for (used = dead = 0, scan = JUMP_LABEL (jump);
3638 (scan = NEXT_INSN (scan)); )
3640 enum rtx_code code;
3642 if (INSN_DELETED_P (scan))
3643 continue;
3644 code = GET_CODE (scan);
3645 if (INSN_P (scan))
3647 used |= regs_used (PATTERN (scan), 0);
3648 if (code == CALL_INSN)
3649 used |= regs_used (CALL_INSN_FUNCTION_USAGE (scan), 0);
3650 dead |= (used >> 16) & ~used;
3651 if (dead & try)
3653 dead &= try;
3654 break;
3656 if (code == JUMP_INSN)
3658 if (jump_left-- && simplejump_p (scan))
3659 scan = JUMP_LABEL (scan);
3660 else
3661 break;
3665 /* Mask out the stack pointer again, in case it was
3666 the only 'free' register we have found. */
3667 dead &= 0x7fff;
3669 /* If the immediate destination is still in range, check for possible
3670 threading with a jump beyond the delay slot insn.
3671 Don't check if we are called recursively; the jump has been or will be
3672 checked in a different invocation then. */
3674 else if (optimize && need_block >= 0)
3676 rtx next = next_active_insn (next_active_insn (dest));
3677 if (next && GET_CODE (next) == JUMP_INSN
3678 && GET_CODE (PATTERN (next)) == SET
3679 && recog_memoized (next) == CODE_FOR_jump_compact)
3681 dest = JUMP_LABEL (next);
3682 if (dest
3683 && (INSN_ADDRESSES (INSN_UID (dest)) - addr + (unsigned) 4092
3684 > 4092 + 4098))
3685 gen_block_redirect (next, INSN_ADDRESSES (INSN_UID (next)), -1);
3689 if (dead)
3691 rtx reg = gen_rtx_REG (SImode, exact_log2 (dead & -dead));
3693 /* It would be nice if we could convert the jump into an indirect
3694 jump / far branch right now, and thus exposing all constituent
3695 instructions to further optimization. However, reorg uses
3696 simplejump_p to determine if there is an unconditional jump where
3697 it should try to schedule instructions from the target of the
3698 branch; simplejump_p fails for indirect jumps even if they have
3699 a JUMP_LABEL. */
3700 rtx insn = emit_insn_before (gen_indirect_jump_scratch
3701 (reg, GEN_INT (INSN_UID (JUMP_LABEL (jump))))
3702 , jump);
3703 /* ??? We would like this to have the scope of the jump, but that
3704 scope will change when a delay slot insn of an inner scope is added.
3705 Hence, after delay slot scheduling, we'll have to expect
3706 NOTE_INSN_BLOCK_END notes between the indirect_jump_scratch and
3707 the jump. */
3709 INSN_LOCATOR (insn) = INSN_LOCATOR (jump);
3710 INSN_CODE (insn) = CODE_FOR_indirect_jump_scratch;
3711 return insn;
3713 else if (need_block)
3714 /* We can't use JUMP_LABEL here because it might be undefined
3715 when not optimizing. */
3716 return emit_insn_before (gen_block_branch_redirect
3717 (GEN_INT (INSN_UID (XEXP (SET_SRC (PATTERN (jump)), 0))))
3718 , jump);
3719 return prev;
3722 #define CONDJUMP_MIN -252
3723 #define CONDJUMP_MAX 262
3724 struct far_branch
3726 /* A label (to be placed) in front of the jump
3727 that jumps to our ultimate destination. */
3728 rtx near_label;
3729 /* Where we are going to insert it if we cannot move the jump any farther,
3730 or the jump itself if we have picked up an existing jump. */
3731 rtx insert_place;
3732 /* The ultimate destination. */
3733 rtx far_label;
3734 struct far_branch *prev;
3735 /* If the branch has already been created, its address;
3736 else the address of its first prospective user. */
3737 int address;
3740 static void gen_far_branch (struct far_branch *);
3741 enum mdep_reorg_phase_e mdep_reorg_phase;
3742 static void
3743 gen_far_branch (struct far_branch *bp)
3745 rtx insn = bp->insert_place;
3746 rtx jump;
3747 rtx label = gen_label_rtx ();
3749 emit_label_after (label, insn);
3750 if (bp->far_label)
3752 jump = emit_jump_insn_after (gen_jump (bp->far_label), insn);
3753 LABEL_NUSES (bp->far_label)++;
3755 else
3756 jump = emit_jump_insn_after (gen_return (), insn);
3757 /* Emit a barrier so that reorg knows that any following instructions
3758 are not reachable via a fall-through path.
3759 But don't do this when not optimizing, since we wouldn't suppress the
3760 alignment for the barrier then, and could end up with out-of-range
3761 pc-relative loads. */
3762 if (optimize)
3763 emit_barrier_after (jump);
3764 emit_label_after (bp->near_label, insn);
3765 JUMP_LABEL (jump) = bp->far_label;
3766 if (! invert_jump (insn, label, 1))
3767 abort ();
3768 /* If we are branching around a jump (rather than a return), prevent
3769 reorg from using an insn from the jump target as the delay slot insn -
3770 when reorg did this, it pessimized code (we rather hide the delay slot)
3771 and it could cause branches to go out of range. */
3772 if (bp->far_label)
3773 (emit_insn_after
3774 (gen_stuff_delay_slot
3775 (GEN_INT (INSN_UID (XEXP (SET_SRC (PATTERN (jump)), 0))),
3776 GEN_INT (recog_memoized (insn) == CODE_FOR_branch_false)),
3777 insn));
3778 /* Prevent reorg from undoing our splits. */
3779 gen_block_redirect (jump, bp->address += 2, 2);
3782 /* Fix up ADDR_DIFF_VECs. */
3783 void
3784 fixup_addr_diff_vecs (rtx first)
3786 rtx insn;
3788 for (insn = first; insn; insn = NEXT_INSN (insn))
3790 rtx vec_lab, pat, prev, prevpat, x, braf_label;
3792 if (GET_CODE (insn) != JUMP_INSN
3793 || GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
3794 continue;
3795 pat = PATTERN (insn);
3796 vec_lab = XEXP (XEXP (pat, 0), 0);
3798 /* Search the matching casesi_jump_2. */
3799 for (prev = vec_lab; ; prev = PREV_INSN (prev))
3801 if (GET_CODE (prev) != JUMP_INSN)
3802 continue;
3803 prevpat = PATTERN (prev);
3804 if (GET_CODE (prevpat) != PARALLEL || XVECLEN (prevpat, 0) != 2)
3805 continue;
3806 x = XVECEXP (prevpat, 0, 1);
3807 if (GET_CODE (x) != USE)
3808 continue;
3809 x = XEXP (x, 0);
3810 if (GET_CODE (x) == LABEL_REF && XEXP (x, 0) == vec_lab)
3811 break;
3814 /* Emit the reference label of the braf where it belongs, right after
3815 the casesi_jump_2 (i.e. braf). */
3816 braf_label = XEXP (XEXP (SET_SRC (XVECEXP (prevpat, 0, 0)), 1), 0);
3817 emit_label_after (braf_label, prev);
3819 /* Fix up the ADDR_DIF_VEC to be relative
3820 to the reference address of the braf. */
3821 XEXP (XEXP (pat, 0), 0) = braf_label;
3825 /* BARRIER_OR_LABEL is either a BARRIER or a CODE_LABEL immediately following
3826 a barrier. Return the base 2 logarithm of the desired alignment. */
3828 barrier_align (rtx barrier_or_label)
3830 rtx next = next_real_insn (barrier_or_label), pat, prev;
3831 int slot, credit, jump_to_next = 0;
3833 if (! next)
3834 return 0;
3836 pat = PATTERN (next);
3838 if (GET_CODE (pat) == ADDR_DIFF_VEC)
3839 return 2;
3841 if (GET_CODE (pat) == UNSPEC_VOLATILE && XINT (pat, 1) == UNSPECV_ALIGN)
3842 /* This is a barrier in front of a constant table. */
3843 return 0;
3845 prev = prev_real_insn (barrier_or_label);
3846 if (GET_CODE (PATTERN (prev)) == ADDR_DIFF_VEC)
3848 pat = PATTERN (prev);
3849 /* If this is a very small table, we want to keep the alignment after
3850 the table to the minimum for proper code alignment. */
3851 return ((TARGET_SMALLCODE
3852 || ((unsigned) XVECLEN (pat, 1) * GET_MODE_SIZE (GET_MODE (pat))
3853 <= (unsigned) 1 << (CACHE_LOG - 2)))
3854 ? 1 << TARGET_SHMEDIA : align_jumps_log);
3857 if (TARGET_SMALLCODE)
3858 return 0;
3860 if (! TARGET_SH2 || ! optimize)
3861 return align_jumps_log;
3863 /* When fixing up pcloads, a constant table might be inserted just before
3864 the basic block that ends with the barrier. Thus, we can't trust the
3865 instruction lengths before that. */
3866 if (mdep_reorg_phase > SH_FIXUP_PCLOAD)
3868 /* Check if there is an immediately preceding branch to the insn beyond
3869 the barrier. We must weight the cost of discarding useful information
3870 from the current cache line when executing this branch and there is
3871 an alignment, against that of fetching unneeded insn in front of the
3872 branch target when there is no alignment. */
3874 /* There are two delay_slot cases to consider. One is the simple case
3875 where the preceding branch is to the insn beyond the barrier (simple
3876 delay slot filling), and the other is where the preceding branch has
3877 a delay slot that is a duplicate of the insn after the barrier
3878 (fill_eager_delay_slots) and the branch is to the insn after the insn
3879 after the barrier. */
3881 /* PREV is presumed to be the JUMP_INSN for the barrier under
3882 investigation. Skip to the insn before it. */
3883 prev = prev_real_insn (prev);
3885 for (slot = 2, credit = (1 << (CACHE_LOG - 2)) + 2;
3886 credit >= 0 && prev && GET_CODE (prev) == INSN;
3887 prev = prev_real_insn (prev))
3889 jump_to_next = 0;
3890 if (GET_CODE (PATTERN (prev)) == USE
3891 || GET_CODE (PATTERN (prev)) == CLOBBER)
3892 continue;
3893 if (GET_CODE (PATTERN (prev)) == SEQUENCE)
3895 prev = XVECEXP (PATTERN (prev), 0, 1);
3896 if (INSN_UID (prev) == INSN_UID (next))
3898 /* Delay slot was filled with insn at jump target. */
3899 jump_to_next = 1;
3900 continue;
3904 if (slot &&
3905 get_attr_in_delay_slot (prev) == IN_DELAY_SLOT_YES)
3906 slot = 0;
3907 credit -= get_attr_length (prev);
3909 if (prev
3910 && GET_CODE (prev) == JUMP_INSN
3911 && JUMP_LABEL (prev))
3913 rtx x;
3914 if (jump_to_next
3915 || next_real_insn (JUMP_LABEL (prev)) == next
3916 /* If relax_delay_slots() decides NEXT was redundant
3917 with some previous instruction, it will have
3918 redirected PREV's jump to the following insn. */
3919 || JUMP_LABEL (prev) == next_nonnote_insn (next)
3920 /* There is no upper bound on redundant instructions
3921 that might have been skipped, but we must not put an
3922 alignment where none had been before. */
3923 || (x = (NEXT_INSN (NEXT_INSN (PREV_INSN (prev)))),
3924 (INSN_P (x)
3925 && (INSN_CODE (x) == CODE_FOR_block_branch_redirect
3926 || INSN_CODE (x) == CODE_FOR_indirect_jump_scratch
3927 || INSN_CODE (x) == CODE_FOR_stuff_delay_slot))))
3929 rtx pat = PATTERN (prev);
3930 if (GET_CODE (pat) == PARALLEL)
3931 pat = XVECEXP (pat, 0, 0);
3932 if (credit - slot >= (GET_CODE (SET_SRC (pat)) == PC ? 2 : 0))
3933 return 0;
3938 return align_jumps_log;
3941 /* If we are inside a phony loop, almost any kind of label can turn up as the
3942 first one in the loop. Aligning a braf label causes incorrect switch
3943 destination addresses; we can detect braf labels because they are
3944 followed by a BARRIER.
3945 Applying loop alignment to small constant or switch tables is a waste
3946 of space, so we suppress this too. */
3948 sh_loop_align (rtx label)
3950 rtx next = label;
3953 next = next_nonnote_insn (next);
3954 while (next && GET_CODE (next) == CODE_LABEL);
3956 if (! next
3957 || ! INSN_P (next)
3958 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC
3959 || recog_memoized (next) == CODE_FOR_consttable_2)
3960 return 0;
3962 return align_loops_log;
3965 /* Do a final pass over the function, just before delayed branch
3966 scheduling. */
3968 static void
3969 sh_reorg (void)
3971 rtx first, insn, mova = NULL_RTX;
3972 int num_mova;
3973 rtx r0_rtx = gen_rtx_REG (Pmode, 0);
3974 rtx r0_inc_rtx = gen_rtx_POST_INC (Pmode, r0_rtx);
3976 first = get_insns ();
3978 /* We must split call insns before introducing `mova's. If we're
3979 optimizing, they'll have already been split. Otherwise, make
3980 sure we don't split them too late. */
3981 if (! optimize)
3982 split_all_insns_noflow ();
3984 if (TARGET_SHMEDIA)
3985 return;
3987 /* If relaxing, generate pseudo-ops to associate function calls with
3988 the symbols they call. It does no harm to not generate these
3989 pseudo-ops. However, when we can generate them, it enables to
3990 linker to potentially relax the jsr to a bsr, and eliminate the
3991 register load and, possibly, the constant pool entry. */
3993 mdep_reorg_phase = SH_INSERT_USES_LABELS;
3994 if (TARGET_RELAX)
3996 /* Remove all REG_LABEL notes. We want to use them for our own
3997 purposes. This works because none of the remaining passes
3998 need to look at them.
4000 ??? But it may break in the future. We should use a machine
4001 dependent REG_NOTE, or some other approach entirely. */
4002 for (insn = first; insn; insn = NEXT_INSN (insn))
4004 if (INSN_P (insn))
4006 rtx note;
4008 while ((note = find_reg_note (insn, REG_LABEL, NULL_RTX)) != 0)
4009 remove_note (insn, note);
4013 for (insn = first; insn; insn = NEXT_INSN (insn))
4015 rtx pattern, reg, link, set, scan, dies, label;
4016 int rescan = 0, foundinsn = 0;
4018 if (GET_CODE (insn) == CALL_INSN)
4020 pattern = PATTERN (insn);
4022 if (GET_CODE (pattern) == PARALLEL)
4023 pattern = XVECEXP (pattern, 0, 0);
4024 if (GET_CODE (pattern) == SET)
4025 pattern = SET_SRC (pattern);
4027 if (GET_CODE (pattern) != CALL
4028 || GET_CODE (XEXP (pattern, 0)) != MEM)
4029 continue;
4031 reg = XEXP (XEXP (pattern, 0), 0);
4033 else
4035 reg = sfunc_uses_reg (insn);
4036 if (! reg)
4037 continue;
4040 if (GET_CODE (reg) != REG)
4041 continue;
4043 /* This is a function call via REG. If the only uses of REG
4044 between the time that it is set and the time that it dies
4045 are in function calls, then we can associate all the
4046 function calls with the setting of REG. */
4048 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
4050 if (REG_NOTE_KIND (link) != 0)
4051 continue;
4052 set = single_set (XEXP (link, 0));
4053 if (set && rtx_equal_p (reg, SET_DEST (set)))
4055 link = XEXP (link, 0);
4056 break;
4060 if (! link)
4062 /* ??? Sometimes global register allocation will have
4063 deleted the insn pointed to by LOG_LINKS. Try
4064 scanning backward to find where the register is set. */
4065 for (scan = PREV_INSN (insn);
4066 scan && GET_CODE (scan) != CODE_LABEL;
4067 scan = PREV_INSN (scan))
4069 if (! INSN_P (scan))
4070 continue;
4072 if (! reg_mentioned_p (reg, scan))
4073 continue;
4075 if (noncall_uses_reg (reg, scan, &set))
4076 break;
4078 if (set)
4080 link = scan;
4081 break;
4086 if (! link)
4087 continue;
4089 /* The register is set at LINK. */
4091 /* We can only optimize the function call if the register is
4092 being set to a symbol. In theory, we could sometimes
4093 optimize calls to a constant location, but the assembler
4094 and linker do not support that at present. */
4095 if (GET_CODE (SET_SRC (set)) != SYMBOL_REF
4096 && GET_CODE (SET_SRC (set)) != LABEL_REF)
4097 continue;
4099 /* Scan forward from LINK to the place where REG dies, and
4100 make sure that the only insns which use REG are
4101 themselves function calls. */
4103 /* ??? This doesn't work for call targets that were allocated
4104 by reload, since there may not be a REG_DEAD note for the
4105 register. */
4107 dies = NULL_RTX;
4108 for (scan = NEXT_INSN (link); scan; scan = NEXT_INSN (scan))
4110 rtx scanset;
4112 /* Don't try to trace forward past a CODE_LABEL if we haven't
4113 seen INSN yet. Ordinarily, we will only find the setting insn
4114 in LOG_LINKS if it is in the same basic block. However,
4115 cross-jumping can insert code labels in between the load and
4116 the call, and can result in situations where a single call
4117 insn may have two targets depending on where we came from. */
4119 if (GET_CODE (scan) == CODE_LABEL && ! foundinsn)
4120 break;
4122 if (! INSN_P (scan))
4123 continue;
4125 /* Don't try to trace forward past a JUMP. To optimize
4126 safely, we would have to check that all the
4127 instructions at the jump destination did not use REG. */
4129 if (GET_CODE (scan) == JUMP_INSN)
4130 break;
4132 if (! reg_mentioned_p (reg, scan))
4133 continue;
4135 if (noncall_uses_reg (reg, scan, &scanset))
4136 break;
4138 if (scan == insn)
4139 foundinsn = 1;
4141 if (scan != insn
4142 && (GET_CODE (scan) == CALL_INSN || sfunc_uses_reg (scan)))
4144 /* There is a function call to this register other
4145 than the one we are checking. If we optimize
4146 this call, we need to rescan again below. */
4147 rescan = 1;
4150 /* ??? We shouldn't have to worry about SCANSET here.
4151 We should just be able to check for a REG_DEAD note
4152 on a function call. However, the REG_DEAD notes are
4153 apparently not dependable around libcalls; c-torture
4154 execute/920501-2 is a test case. If SCANSET is set,
4155 then this insn sets the register, so it must have
4156 died earlier. Unfortunately, this will only handle
4157 the cases in which the register is, in fact, set in a
4158 later insn. */
4160 /* ??? We shouldn't have to use FOUNDINSN here.
4161 However, the LOG_LINKS fields are apparently not
4162 entirely reliable around libcalls;
4163 newlib/libm/math/e_pow.c is a test case. Sometimes
4164 an insn will appear in LOG_LINKS even though it is
4165 not the most recent insn which sets the register. */
4167 if (foundinsn
4168 && (scanset
4169 || find_reg_note (scan, REG_DEAD, reg)))
4171 dies = scan;
4172 break;
4176 if (! dies)
4178 /* Either there was a branch, or some insn used REG
4179 other than as a function call address. */
4180 continue;
4183 /* Create a code label, and put it in a REG_LABEL note on
4184 the insn which sets the register, and on each call insn
4185 which uses the register. In final_prescan_insn we look
4186 for the REG_LABEL notes, and output the appropriate label
4187 or pseudo-op. */
4189 label = gen_label_rtx ();
4190 REG_NOTES (link) = gen_rtx_INSN_LIST (REG_LABEL, label,
4191 REG_NOTES (link));
4192 REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, label,
4193 REG_NOTES (insn));
4194 if (rescan)
4196 scan = link;
4199 rtx reg2;
4201 scan = NEXT_INSN (scan);
4202 if (scan != insn
4203 && ((GET_CODE (scan) == CALL_INSN
4204 && reg_mentioned_p (reg, scan))
4205 || ((reg2 = sfunc_uses_reg (scan))
4206 && REGNO (reg2) == REGNO (reg))))
4207 REG_NOTES (scan)
4208 = gen_rtx_INSN_LIST (REG_LABEL, label, REG_NOTES (scan));
4210 while (scan != dies);
4215 if (TARGET_SH2)
4216 fixup_addr_diff_vecs (first);
4218 if (optimize)
4220 mdep_reorg_phase = SH_SHORTEN_BRANCHES0;
4221 shorten_branches (first);
4223 /* Scan the function looking for move instructions which have to be
4224 changed to pc-relative loads and insert the literal tables. */
4226 mdep_reorg_phase = SH_FIXUP_PCLOAD;
4227 for (insn = first, num_mova = 0; insn; insn = NEXT_INSN (insn))
4229 if (mova_p (insn))
4231 /* ??? basic block reordering can move a switch table dispatch
4232 below the switch table. Check if that has happened.
4233 We only have the addresses available when optimizing; but then,
4234 this check shouldn't be needed when not optimizing. */
4235 rtx label_ref = XVECEXP (SET_SRC (PATTERN (insn)), 0, 0);
4236 if (optimize
4237 && (INSN_ADDRESSES (INSN_UID (insn))
4238 > INSN_ADDRESSES (INSN_UID (XEXP (label_ref, 0)))))
4240 /* Change the mova into a load.
4241 broken_move will then return true for it. */
4242 fixup_mova (insn);
4244 else if (! num_mova++)
4245 mova = insn;
4247 else if (GET_CODE (insn) == JUMP_INSN
4248 && GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
4249 && num_mova)
4251 rtx scan;
4252 int total;
4254 num_mova--;
4256 /* Some code might have been inserted between the mova and
4257 its ADDR_DIFF_VEC. Check if the mova is still in range. */
4258 for (scan = mova, total = 0; scan != insn; scan = NEXT_INSN (scan))
4259 total += get_attr_length (scan);
4261 /* range of mova is 1020, add 4 because pc counts from address of
4262 second instruction after this one, subtract 2 in case pc is 2
4263 byte aligned. Possible alignment needed for the ADDR_DIFF_VEC
4264 cancels out with alignment effects of the mova itself. */
4265 if (total > 1022)
4267 /* Change the mova into a load, and restart scanning
4268 there. broken_move will then return true for mova. */
4269 fixup_mova (mova);
4270 insn = mova;
4273 if (broken_move (insn)
4274 || (GET_CODE (insn) == INSN
4275 && recog_memoized (insn) == CODE_FOR_casesi_worker_2))
4277 rtx scan;
4278 /* Scan ahead looking for a barrier to stick the constant table
4279 behind. */
4280 rtx barrier = find_barrier (num_mova, mova, insn);
4281 rtx last_float_move = NULL_RTX, last_float = 0, *last_float_addr = NULL;
4282 int need_aligned_label = 0;
4284 if (num_mova && ! mova_p (mova))
4286 /* find_barrier had to change the first mova into a
4287 pcload; thus, we have to start with this new pcload. */
4288 insn = mova;
4289 num_mova = 0;
4291 /* Now find all the moves between the points and modify them. */
4292 for (scan = insn; scan != barrier; scan = NEXT_INSN (scan))
4294 if (GET_CODE (scan) == CODE_LABEL)
4295 last_float = 0;
4296 if (GET_CODE (scan) == INSN
4297 && recog_memoized (scan) == CODE_FOR_casesi_worker_2)
4298 need_aligned_label = 1;
4299 if (broken_move (scan))
4301 rtx *patp = &PATTERN (scan), pat = *patp;
4302 rtx src, dst;
4303 rtx lab;
4304 rtx newsrc;
4305 enum machine_mode mode;
4307 if (GET_CODE (pat) == PARALLEL)
4308 patp = &XVECEXP (pat, 0, 0), pat = *patp;
4309 src = SET_SRC (pat);
4310 dst = SET_DEST (pat);
4311 mode = GET_MODE (dst);
4313 if (mode == SImode && hi_const (src)
4314 && REGNO (dst) != FPUL_REG)
4316 int offset = 0;
4318 mode = HImode;
4319 while (GET_CODE (dst) == SUBREG)
4321 offset += subreg_regno_offset (REGNO (SUBREG_REG (dst)),
4322 GET_MODE (SUBREG_REG (dst)),
4323 SUBREG_BYTE (dst),
4324 GET_MODE (dst));
4325 dst = SUBREG_REG (dst);
4327 dst = gen_rtx_REG (HImode, REGNO (dst) + offset);
4329 if (GET_CODE (dst) == REG && FP_ANY_REGISTER_P (REGNO (dst)))
4331 /* This must be an insn that clobbers r0. */
4332 rtx *clobberp = &XVECEXP (PATTERN (scan), 0,
4333 XVECLEN (PATTERN (scan), 0)
4334 - 1);
4335 rtx clobber = *clobberp;
4337 if (GET_CODE (clobber) != CLOBBER
4338 || ! rtx_equal_p (XEXP (clobber, 0), r0_rtx))
4339 abort ();
4341 if (last_float
4342 && reg_set_between_p (r0_rtx, last_float_move, scan))
4343 last_float = 0;
4344 if (last_float
4345 && TARGET_SHCOMPACT
4346 && GET_MODE_SIZE (mode) != 4
4347 && GET_MODE_SIZE (GET_MODE (last_float)) == 4)
4348 last_float = 0;
4349 lab = add_constant (src, mode, last_float);
4350 if (lab)
4351 emit_insn_before (gen_mova (lab), scan);
4352 else
4354 /* There will be a REG_UNUSED note for r0 on
4355 LAST_FLOAT_MOVE; we have to change it to REG_INC,
4356 lest reorg:mark_target_live_regs will not
4357 consider r0 to be used, and we end up with delay
4358 slot insn in front of SCAN that clobbers r0. */
4359 rtx note
4360 = find_regno_note (last_float_move, REG_UNUSED, 0);
4362 /* If we are not optimizing, then there may not be
4363 a note. */
4364 if (note)
4365 PUT_MODE (note, REG_INC);
4367 *last_float_addr = r0_inc_rtx;
4369 last_float_move = scan;
4370 last_float = src;
4371 newsrc = gen_rtx_MEM (mode,
4372 (((TARGET_SH4 && ! TARGET_FMOVD)
4373 || REGNO (dst) == FPUL_REG)
4374 ? r0_inc_rtx
4375 : r0_rtx));
4376 last_float_addr = &XEXP (newsrc, 0);
4378 /* Remove the clobber of r0. */
4379 *clobberp = gen_rtx_CLOBBER (GET_MODE (clobber),
4380 gen_rtx_SCRATCH (Pmode));
4381 RTX_UNCHANGING_P (newsrc) = 1;
4383 /* This is a mova needing a label. Create it. */
4384 else if (GET_CODE (src) == UNSPEC
4385 && XINT (src, 1) == UNSPEC_MOVA
4386 && GET_CODE (XVECEXP (src, 0, 0)) == CONST)
4388 lab = add_constant (XVECEXP (src, 0, 0), mode, 0);
4389 newsrc = gen_rtx_LABEL_REF (VOIDmode, lab);
4390 newsrc = gen_rtx_UNSPEC (SImode,
4391 gen_rtvec (1, newsrc),
4392 UNSPEC_MOVA);
4394 else
4396 lab = add_constant (src, mode, 0);
4397 newsrc = gen_rtx_MEM (mode,
4398 gen_rtx_LABEL_REF (VOIDmode, lab));
4399 RTX_UNCHANGING_P (newsrc) = 1;
4401 *patp = gen_rtx_SET (VOIDmode, dst, newsrc);
4402 INSN_CODE (scan) = -1;
4405 dump_table (need_aligned_label ? insn : 0, barrier);
4406 insn = barrier;
4410 mdep_reorg_phase = SH_SHORTEN_BRANCHES1;
4411 INSN_ADDRESSES_FREE ();
4412 split_branches (first);
4414 /* The INSN_REFERENCES_ARE_DELAYED in sh.h is problematic because it
4415 also has an effect on the register that holds the address of the sfunc.
4416 Insert an extra dummy insn in front of each sfunc that pretends to
4417 use this register. */
4418 if (flag_delayed_branch)
4420 for (insn = first; insn; insn = NEXT_INSN (insn))
4422 rtx reg = sfunc_uses_reg (insn);
4424 if (! reg)
4425 continue;
4426 emit_insn_before (gen_use_sfunc_addr (reg), insn);
4429 #if 0
4430 /* fpscr is not actually a user variable, but we pretend it is for the
4431 sake of the previous optimization passes, since we want it handled like
4432 one. However, we don't have any debugging information for it, so turn
4433 it into a non-user variable now. */
4434 if (TARGET_SH4)
4435 REG_USERVAR_P (get_fpscr_rtx ()) = 0;
4436 #endif
4437 mdep_reorg_phase = SH_AFTER_MDEP_REORG;
4441 get_dest_uid (rtx label, int max_uid)
4443 rtx dest = next_real_insn (label);
4444 int dest_uid;
4445 if (! dest)
4446 /* This can happen for an undefined label. */
4447 return 0;
4448 dest_uid = INSN_UID (dest);
4449 /* If this is a newly created branch redirection blocking instruction,
4450 we cannot index the branch_uid or insn_addresses arrays with its
4451 uid. But then, we won't need to, because the actual destination is
4452 the following branch. */
4453 while (dest_uid >= max_uid)
4455 dest = NEXT_INSN (dest);
4456 dest_uid = INSN_UID (dest);
4458 if (GET_CODE (dest) == JUMP_INSN && GET_CODE (PATTERN (dest)) == RETURN)
4459 return 0;
4460 return dest_uid;
4463 /* Split condbranches that are out of range. Also add clobbers for
4464 scratch registers that are needed in far jumps.
4465 We do this before delay slot scheduling, so that it can take our
4466 newly created instructions into account. It also allows us to
4467 find branches with common targets more easily. */
4469 static void
4470 split_branches (rtx first)
4472 rtx insn;
4473 struct far_branch **uid_branch, *far_branch_list = 0;
4474 int max_uid = get_max_uid ();
4476 /* Find out which branches are out of range. */
4477 shorten_branches (first);
4479 uid_branch = (struct far_branch **) alloca (max_uid * sizeof *uid_branch);
4480 memset ((char *) uid_branch, 0, max_uid * sizeof *uid_branch);
4482 for (insn = first; insn; insn = NEXT_INSN (insn))
4483 if (! INSN_P (insn))
4484 continue;
4485 else if (INSN_DELETED_P (insn))
4487 /* Shorten_branches would split this instruction again,
4488 so transform it into a note. */
4489 PUT_CODE (insn, NOTE);
4490 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
4491 NOTE_SOURCE_FILE (insn) = 0;
4493 else if (GET_CODE (insn) == JUMP_INSN
4494 /* Don't mess with ADDR_DIFF_VEC */
4495 && (GET_CODE (PATTERN (insn)) == SET
4496 || GET_CODE (PATTERN (insn)) == RETURN))
4498 enum attr_type type = get_attr_type (insn);
4499 if (type == TYPE_CBRANCH)
4501 rtx next, beyond;
4503 if (get_attr_length (insn) > 4)
4505 rtx src = SET_SRC (PATTERN (insn));
4506 rtx olabel = XEXP (XEXP (src, 1), 0);
4507 int addr = INSN_ADDRESSES (INSN_UID (insn));
4508 rtx label = 0;
4509 int dest_uid = get_dest_uid (olabel, max_uid);
4510 struct far_branch *bp = uid_branch[dest_uid];
4512 /* redirect_jump needs a valid JUMP_LABEL, and it might delete
4513 the label if the LABEL_NUSES count drops to zero. There is
4514 always a jump_optimize pass that sets these values, but it
4515 proceeds to delete unreferenced code, and then if not
4516 optimizing, to un-delete the deleted instructions, thus
4517 leaving labels with too low uses counts. */
4518 if (! optimize)
4520 JUMP_LABEL (insn) = olabel;
4521 LABEL_NUSES (olabel)++;
4523 if (! bp)
4525 bp = (struct far_branch *) alloca (sizeof *bp);
4526 uid_branch[dest_uid] = bp;
4527 bp->prev = far_branch_list;
4528 far_branch_list = bp;
4529 bp->far_label
4530 = XEXP (XEXP (SET_SRC (PATTERN (insn)), 1), 0);
4531 LABEL_NUSES (bp->far_label)++;
4533 else
4535 label = bp->near_label;
4536 if (! label && bp->address - addr >= CONDJUMP_MIN)
4538 rtx block = bp->insert_place;
4540 if (GET_CODE (PATTERN (block)) == RETURN)
4541 block = PREV_INSN (block);
4542 else
4543 block = gen_block_redirect (block,
4544 bp->address, 2);
4545 label = emit_label_after (gen_label_rtx (),
4546 PREV_INSN (block));
4547 bp->near_label = label;
4549 else if (label && ! NEXT_INSN (label))
4551 if (addr + 2 - bp->address <= CONDJUMP_MAX)
4552 bp->insert_place = insn;
4553 else
4554 gen_far_branch (bp);
4557 if (! label
4558 || (NEXT_INSN (label) && bp->address - addr < CONDJUMP_MIN))
4560 bp->near_label = label = gen_label_rtx ();
4561 bp->insert_place = insn;
4562 bp->address = addr;
4564 if (! redirect_jump (insn, label, 1))
4565 abort ();
4567 else
4569 /* get_attr_length (insn) == 2 */
4570 /* Check if we have a pattern where reorg wants to redirect
4571 the branch to a label from an unconditional branch that
4572 is too far away. */
4573 /* We can't use JUMP_LABEL here because it might be undefined
4574 when not optimizing. */
4575 /* A syntax error might cause beyond to be NULL_RTX. */
4576 beyond
4577 = next_active_insn (XEXP (XEXP (SET_SRC (PATTERN (insn)), 1),
4578 0));
4580 if (beyond
4581 && (GET_CODE (beyond) == JUMP_INSN
4582 || ((beyond = next_active_insn (beyond))
4583 && GET_CODE (beyond) == JUMP_INSN))
4584 && GET_CODE (PATTERN (beyond)) == SET
4585 && recog_memoized (beyond) == CODE_FOR_jump_compact
4586 && ((INSN_ADDRESSES
4587 (INSN_UID (XEXP (SET_SRC (PATTERN (beyond)), 0)))
4588 - INSN_ADDRESSES (INSN_UID (insn)) + (unsigned) 252)
4589 > 252 + 258 + 2))
4590 gen_block_redirect (beyond,
4591 INSN_ADDRESSES (INSN_UID (beyond)), 1);
4594 next = next_active_insn (insn);
4596 if ((GET_CODE (next) == JUMP_INSN
4597 || GET_CODE (next = next_active_insn (next)) == JUMP_INSN)
4598 && GET_CODE (PATTERN (next)) == SET
4599 && recog_memoized (next) == CODE_FOR_jump_compact
4600 && ((INSN_ADDRESSES
4601 (INSN_UID (XEXP (SET_SRC (PATTERN (next)), 0)))
4602 - INSN_ADDRESSES (INSN_UID (insn)) + (unsigned) 252)
4603 > 252 + 258 + 2))
4604 gen_block_redirect (next, INSN_ADDRESSES (INSN_UID (next)), 1);
4606 else if (type == TYPE_JUMP || type == TYPE_RETURN)
4608 int addr = INSN_ADDRESSES (INSN_UID (insn));
4609 rtx far_label = 0;
4610 int dest_uid = 0;
4611 struct far_branch *bp;
4613 if (type == TYPE_JUMP)
4615 far_label = XEXP (SET_SRC (PATTERN (insn)), 0);
4616 dest_uid = get_dest_uid (far_label, max_uid);
4617 if (! dest_uid)
4619 /* Parse errors can lead to labels outside
4620 the insn stream. */
4621 if (! NEXT_INSN (far_label))
4622 continue;
4624 if (! optimize)
4626 JUMP_LABEL (insn) = far_label;
4627 LABEL_NUSES (far_label)++;
4629 redirect_jump (insn, NULL_RTX, 1);
4630 far_label = 0;
4633 bp = uid_branch[dest_uid];
4634 if (! bp)
4636 bp = (struct far_branch *) alloca (sizeof *bp);
4637 uid_branch[dest_uid] = bp;
4638 bp->prev = far_branch_list;
4639 far_branch_list = bp;
4640 bp->near_label = 0;
4641 bp->far_label = far_label;
4642 if (far_label)
4643 LABEL_NUSES (far_label)++;
4645 else if (bp->near_label && ! NEXT_INSN (bp->near_label))
4646 if (addr - bp->address <= CONDJUMP_MAX)
4647 emit_label_after (bp->near_label, PREV_INSN (insn));
4648 else
4650 gen_far_branch (bp);
4651 bp->near_label = 0;
4653 else
4654 bp->near_label = 0;
4655 bp->address = addr;
4656 bp->insert_place = insn;
4657 if (! far_label)
4658 emit_insn_before (gen_block_branch_redirect (const0_rtx), insn);
4659 else
4660 gen_block_redirect (insn, addr, bp->near_label ? 2 : 0);
4663 /* Generate all pending far branches,
4664 and free our references to the far labels. */
4665 while (far_branch_list)
4667 if (far_branch_list->near_label
4668 && ! NEXT_INSN (far_branch_list->near_label))
4669 gen_far_branch (far_branch_list);
4670 if (optimize
4671 && far_branch_list->far_label
4672 && ! --LABEL_NUSES (far_branch_list->far_label))
4673 delete_insn (far_branch_list->far_label);
4674 far_branch_list = far_branch_list->prev;
4677 /* Instruction length information is no longer valid due to the new
4678 instructions that have been generated. */
4679 init_insn_lengths ();
4682 /* Dump out instruction addresses, which is useful for debugging the
4683 constant pool table stuff.
4685 If relaxing, output the label and pseudo-ops used to link together
4686 calls and the instruction which set the registers. */
4688 /* ??? The addresses printed by this routine for insns are nonsense for
4689 insns which are inside of a sequence where none of the inner insns have
4690 variable length. This is because the second pass of shorten_branches
4691 does not bother to update them. */
4693 void
4694 final_prescan_insn (rtx insn, rtx *opvec ATTRIBUTE_UNUSED,
4695 int noperands ATTRIBUTE_UNUSED)
4697 if (TARGET_DUMPISIZE)
4698 fprintf (asm_out_file, "\n! at %04x\n", INSN_ADDRESSES (INSN_UID (insn)));
4700 if (TARGET_RELAX)
4702 rtx note;
4704 note = find_reg_note (insn, REG_LABEL, NULL_RTX);
4705 if (note)
4707 rtx pattern;
4709 pattern = PATTERN (insn);
4710 if (GET_CODE (pattern) == PARALLEL)
4711 pattern = XVECEXP (pattern, 0, 0);
4712 if (GET_CODE (pattern) == CALL
4713 || (GET_CODE (pattern) == SET
4714 && (GET_CODE (SET_SRC (pattern)) == CALL
4715 || get_attr_type (insn) == TYPE_SFUNC)))
4716 asm_fprintf (asm_out_file, "\t.uses %LL%d\n",
4717 CODE_LABEL_NUMBER (XEXP (note, 0)));
4718 else if (GET_CODE (pattern) == SET)
4719 (*targetm.asm_out.internal_label) (asm_out_file, "L",
4720 CODE_LABEL_NUMBER (XEXP (note, 0)));
4721 else
4722 abort ();
4727 /* Dump out any constants accumulated in the final pass. These will
4728 only be labels. */
4730 const char *
4731 output_jump_label_table (void)
4733 int i;
4735 if (pool_size)
4737 fprintf (asm_out_file, "\t.align 2\n");
4738 for (i = 0; i < pool_size; i++)
4740 pool_node *p = &pool_vector[i];
4742 (*targetm.asm_out.internal_label) (asm_out_file, "L",
4743 CODE_LABEL_NUMBER (p->label));
4744 output_asm_insn (".long %O0", &p->value);
4746 pool_size = 0;
4749 return "";
4752 /* A full frame looks like:
4754 arg-5
4755 arg-4
4756 [ if current_function_anonymous_args
4757 arg-3
4758 arg-2
4759 arg-1
4760 arg-0 ]
4761 saved-fp
4762 saved-r10
4763 saved-r11
4764 saved-r12
4765 saved-pr
4766 local-n
4768 local-1
4769 local-0 <- fp points here. */
4771 /* Number of bytes pushed for anonymous args, used to pass information
4772 between expand_prologue and expand_epilogue. */
4774 /* Adjust the stack by SIZE bytes. REG holds the rtl of the register to be
4775 adjusted. If epilogue_p is zero, this is for a prologue; otherwise, it's
4776 for an epilogue and a negative value means that it's for a sibcall
4777 epilogue. If LIVE_REGS_MASK is nonzero, it points to a HARD_REG_SET of
4778 all the registers that are about to be restored, and hence dead. */
4780 static void
4781 output_stack_adjust (int size, rtx reg, int epilogue_p,
4782 HARD_REG_SET *live_regs_mask)
4784 rtx (*emit_fn) (rtx) = epilogue_p ? &emit_insn : &frame_insn;
4785 if (size)
4787 HOST_WIDE_INT align = STACK_BOUNDARY / BITS_PER_UNIT;
4789 if (size % align)
4790 abort ();
4792 if (CONST_OK_FOR_ADD (size))
4793 emit_fn (GEN_ADD3 (reg, reg, GEN_INT (size)));
4794 /* Try to do it with two partial adjustments; however, we must make
4795 sure that the stack is properly aligned at all times, in case
4796 an interrupt occurs between the two partial adjustments. */
4797 else if (CONST_OK_FOR_ADD (size / 2 & -align)
4798 && CONST_OK_FOR_ADD (size - (size / 2 & -align)))
4800 emit_fn (GEN_ADD3 (reg, reg, GEN_INT (size / 2 & -align)));
4801 emit_fn (GEN_ADD3 (reg, reg, GEN_INT (size - (size / 2 & -align))));
4803 else
4805 rtx const_reg;
4806 rtx insn;
4807 int temp = epilogue_p ? 7 : (TARGET_SH5 ? 0 : 1);
4808 int i;
4810 /* If TEMP is invalid, we could temporarily save a general
4811 register to MACL. However, there is currently no need
4812 to handle this case, so just abort when we see it. */
4813 if (epilogue_p < 0
4814 || current_function_interrupt
4815 || ! call_used_regs[temp] || fixed_regs[temp])
4816 temp = -1;
4817 if (temp < 0 && ! current_function_interrupt
4818 && (TARGET_SHMEDIA || epilogue_p >= 0))
4820 HARD_REG_SET temps;
4821 COPY_HARD_REG_SET (temps, call_used_reg_set);
4822 AND_COMPL_HARD_REG_SET (temps, call_fixed_reg_set);
4823 if (epilogue_p > 0)
4825 int nreg = 0;
4826 if (current_function_return_rtx)
4828 enum machine_mode mode;
4829 mode = GET_MODE (current_function_return_rtx);
4830 if (BASE_RETURN_VALUE_REG (mode) == FIRST_RET_REG)
4831 nreg = HARD_REGNO_NREGS (FIRST_RET_REG, mode);
4833 for (i = 0; i < nreg; i++)
4834 CLEAR_HARD_REG_BIT (temps, FIRST_RET_REG + i);
4835 if (current_function_calls_eh_return)
4837 CLEAR_HARD_REG_BIT (temps, EH_RETURN_STACKADJ_REGNO);
4838 for (i = 0; i <= 3; i++)
4839 CLEAR_HARD_REG_BIT (temps, EH_RETURN_DATA_REGNO (i));
4842 if (TARGET_SHMEDIA && epilogue_p < 0)
4843 for (i = FIRST_TARGET_REG; i <= LAST_TARGET_REG; i++)
4844 CLEAR_HARD_REG_BIT (temps, i);
4845 if (epilogue_p <= 0)
4847 for (i = FIRST_PARM_REG;
4848 i < FIRST_PARM_REG + NPARM_REGS (SImode); i++)
4849 CLEAR_HARD_REG_BIT (temps, i);
4850 if (cfun->static_chain_decl != NULL)
4851 CLEAR_HARD_REG_BIT (temps, STATIC_CHAIN_REGNUM);
4853 temp = scavenge_reg (&temps);
4855 if (temp < 0 && live_regs_mask)
4856 temp = scavenge_reg (live_regs_mask);
4857 if (temp < 0)
4859 /* If we reached here, the most likely case is the (sibcall)
4860 epilogue for non SHmedia. Put a special push/pop sequence
4861 for such case as the last resort. This looks lengthy but
4862 would not be problem because it seems to be very rare. */
4863 if (! TARGET_SHMEDIA && epilogue_p)
4865 rtx adj_reg, tmp_reg, mem;
4867 /* ??? There is still the slight possibility that r4 or r5
4868 have been reserved as fixed registers or assigned as
4869 global registers, and they change during an interrupt.
4870 There are possible ways to handle this:
4871 - If we are adjusting the frame pointer (r14), we can do
4872 with a single temp register and an ordinary push / pop
4873 on the stack.
4874 - Grab any call-used or call-saved registers (i.e. not
4875 fixed or globals) for the temps we need. We might
4876 also grab r14 if we are adjusting the stack pointer.
4877 If we can't find enough available registers, issue
4878 a diagnostic and abort - the user must have reserved
4879 way too many registers.
4880 But since all this is rather unlikely to happen and
4881 would require extra testing, we just abort if r4 / r5
4882 are not available. */
4883 if (fixed_regs[4] || fixed_regs[5]
4884 || global_regs[4] || global_regs[5])
4885 abort ();
4887 adj_reg = gen_rtx_REG (GET_MODE (reg), 4);
4888 tmp_reg = gen_rtx_REG (GET_MODE (reg), 5);
4889 emit_move_insn (gen_rtx_MEM (Pmode, reg), adj_reg);
4890 emit_insn (GEN_MOV (adj_reg, GEN_INT (size)));
4891 emit_insn (GEN_ADD3 (adj_reg, adj_reg, reg));
4892 mem = gen_rtx_MEM (Pmode, gen_rtx_PRE_DEC (Pmode, adj_reg));
4893 emit_move_insn (mem, tmp_reg);
4894 emit_move_insn (tmp_reg, gen_rtx_MEM (Pmode, reg));
4895 mem = gen_rtx_MEM (Pmode, gen_rtx_PRE_DEC (Pmode, adj_reg));
4896 emit_move_insn (mem, tmp_reg);
4897 emit_move_insn (reg, adj_reg);
4898 mem = gen_rtx_MEM (Pmode, gen_rtx_POST_INC (Pmode, reg));
4899 emit_move_insn (adj_reg, mem);
4900 mem = gen_rtx_MEM (Pmode, gen_rtx_POST_INC (Pmode, reg));
4901 emit_move_insn (tmp_reg, mem);
4902 return;
4904 else
4905 abort ();
4907 const_reg = gen_rtx_REG (GET_MODE (reg), temp);
4909 /* If SIZE is negative, subtract the positive value.
4910 This sometimes allows a constant pool entry to be shared
4911 between prologue and epilogue code. */
4912 if (size < 0)
4914 emit_insn (GEN_MOV (const_reg, GEN_INT (-size)));
4915 insn = emit_fn (GEN_SUB3 (reg, reg, const_reg));
4917 else
4919 emit_insn (GEN_MOV (const_reg, GEN_INT (size)));
4920 insn = emit_fn (GEN_ADD3 (reg, reg, const_reg));
4922 if (! epilogue_p)
4923 REG_NOTES (insn)
4924 = (gen_rtx_EXPR_LIST
4925 (REG_FRAME_RELATED_EXPR,
4926 gen_rtx_SET (VOIDmode, reg,
4927 gen_rtx_PLUS (SImode, reg, GEN_INT (size))),
4928 REG_NOTES (insn)));
4933 static rtx
4934 frame_insn (rtx x)
4936 x = emit_insn (x);
4937 RTX_FRAME_RELATED_P (x) = 1;
4938 return x;
4941 /* Output RTL to push register RN onto the stack. */
4943 static rtx
4944 push (int rn)
4946 rtx x;
4947 if (rn == FPUL_REG)
4948 x = gen_push_fpul ();
4949 else if (rn == FPSCR_REG)
4950 x = gen_push_fpscr ();
4951 else if (TARGET_SH4 && TARGET_FMOVD && ! TARGET_FPU_SINGLE
4952 && FP_OR_XD_REGISTER_P (rn))
4954 if (FP_REGISTER_P (rn) && (rn - FIRST_FP_REG) & 1)
4955 return NULL_RTX;
4956 x = gen_push_4 (gen_rtx_REG (DFmode, rn));
4958 else if (TARGET_SH2E && FP_REGISTER_P (rn))
4959 x = gen_push_e (gen_rtx_REG (SFmode, rn));
4960 else
4961 x = gen_push (gen_rtx_REG (SImode, rn));
4963 x = frame_insn (x);
4964 REG_NOTES (x)
4965 = gen_rtx_EXPR_LIST (REG_INC,
4966 gen_rtx_REG (SImode, STACK_POINTER_REGNUM), 0);
4967 return x;
4970 /* Output RTL to pop register RN from the stack. */
4972 static void
4973 pop (int rn)
4975 rtx x;
4976 if (rn == FPUL_REG)
4977 x = gen_pop_fpul ();
4978 else if (rn == FPSCR_REG)
4979 x = gen_pop_fpscr ();
4980 else if (TARGET_SH4 && TARGET_FMOVD && ! TARGET_FPU_SINGLE
4981 && FP_OR_XD_REGISTER_P (rn))
4983 if (FP_REGISTER_P (rn) && (rn - FIRST_FP_REG) & 1)
4984 return;
4985 x = gen_pop_4 (gen_rtx_REG (DFmode, rn));
4987 else if (TARGET_SH2E && FP_REGISTER_P (rn))
4988 x = gen_pop_e (gen_rtx_REG (SFmode, rn));
4989 else
4990 x = gen_pop (gen_rtx_REG (SImode, rn));
4992 x = emit_insn (x);
4993 REG_NOTES (x)
4994 = gen_rtx_EXPR_LIST (REG_INC,
4995 gen_rtx_REG (SImode, STACK_POINTER_REGNUM), 0);
4998 /* Generate code to push the regs specified in the mask. */
5000 static void
5001 push_regs (HARD_REG_SET *mask, int interrupt_handler)
5003 int i;
5004 int skip_fpscr = 0;
5006 /* Push PR last; this gives better latencies after the prologue, and
5007 candidates for the return delay slot when there are no general
5008 registers pushed. */
5009 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5011 /* If this is an interrupt handler, and the SZ bit varies,
5012 and we have to push any floating point register, we need
5013 to switch to the correct precision first. */
5014 if (i == FIRST_FP_REG && interrupt_handler && TARGET_FMOVD
5015 && hard_regs_intersect_p (mask, &reg_class_contents[DF_REGS]))
5017 HARD_REG_SET unsaved;
5019 push (FPSCR_REG);
5020 COMPL_HARD_REG_SET (unsaved, *mask);
5021 fpscr_set_from_mem (NORMAL_MODE (FP_MODE), unsaved);
5022 skip_fpscr = 1;
5024 if (i != PR_REG
5025 && (i != FPSCR_REG || ! skip_fpscr)
5026 && TEST_HARD_REG_BIT (*mask, i))
5027 push (i);
5029 if (TEST_HARD_REG_BIT (*mask, PR_REG))
5030 push (PR_REG);
5033 /* Calculate how much extra space is needed to save all callee-saved
5034 target registers.
5035 LIVE_REGS_MASK is the register mask calculated by calc_live_regs. */
5037 static int
5038 shmedia_target_regs_stack_space (HARD_REG_SET *live_regs_mask)
5040 int reg;
5041 int stack_space = 0;
5042 int interrupt_handler = sh_cfun_interrupt_handler_p ();
5044 for (reg = LAST_TARGET_REG; reg >= FIRST_TARGET_REG; reg--)
5045 if ((! call_used_regs[reg] || interrupt_handler)
5046 && ! TEST_HARD_REG_BIT (*live_regs_mask, reg))
5047 /* Leave space to save this target register on the stack,
5048 in case target register allocation wants to use it. */
5049 stack_space += GET_MODE_SIZE (REGISTER_NATURAL_MODE (reg));
5050 return stack_space;
5053 /* Decide whether we should reserve space for callee-save target registers,
5054 in case target register allocation wants to use them. REGS_SAVED is
5055 the space, in bytes, that is already required for register saves.
5056 LIVE_REGS_MASK is the register mask calculated by calc_live_regs. */
5058 static int
5059 shmedia_reserve_space_for_target_registers_p (int regs_saved,
5060 HARD_REG_SET *live_regs_mask)
5062 if (optimize_size)
5063 return 0;
5064 return shmedia_target_regs_stack_space (live_regs_mask) <= regs_saved;
5067 /* Decide how much space to reserve for callee-save target registers
5068 in case target register allocation wants to use them.
5069 LIVE_REGS_MASK is the register mask calculated by calc_live_regs. */
5071 static int
5072 shmedia_target_regs_stack_adjust (HARD_REG_SET *live_regs_mask)
5074 if (shmedia_space_reserved_for_target_registers)
5075 return shmedia_target_regs_stack_space (live_regs_mask);
5076 else
5077 return 0;
5080 /* Work out the registers which need to be saved, both as a mask and a
5081 count of saved words. Return the count.
5083 If doing a pragma interrupt function, then push all regs used by the
5084 function, and if we call another function (we can tell by looking at PR),
5085 make sure that all the regs it clobbers are safe too. */
5087 static int
5088 calc_live_regs (HARD_REG_SET *live_regs_mask)
5090 int reg;
5091 int count;
5092 int interrupt_handler;
5093 int pr_live, has_call;
5095 interrupt_handler = sh_cfun_interrupt_handler_p ();
5097 CLEAR_HARD_REG_SET (*live_regs_mask);
5098 if (TARGET_SH4 && TARGET_FMOVD && interrupt_handler
5099 && regs_ever_live[FPSCR_REG])
5100 target_flags &= ~FPU_SINGLE_BIT;
5101 /* If we can save a lot of saves by switching to double mode, do that. */
5102 else if (TARGET_SH4 && TARGET_FMOVD && TARGET_FPU_SINGLE)
5103 for (count = 0, reg = FIRST_FP_REG; reg <= LAST_FP_REG; reg += 2)
5104 if (regs_ever_live[reg] && regs_ever_live[reg+1]
5105 && (! call_used_regs[reg] || (interrupt_handler && ! pragma_trapa))
5106 && ++count > 2)
5108 target_flags &= ~FPU_SINGLE_BIT;
5109 break;
5111 /* PR_MEDIA_REG is a general purpose register, thus global_alloc already
5112 knows how to use it. That means the pseudo originally allocated for
5113 the initial value can become the PR_MEDIA_REG hard register, as seen for
5114 execute/20010122-1.c:test9. */
5115 if (TARGET_SHMEDIA)
5116 /* ??? this function is called from initial_elimination_offset, hence we
5117 can't use the result of sh_media_register_for_return here. */
5118 pr_live = sh_pr_n_sets ();
5119 else
5121 rtx pr_initial = has_hard_reg_initial_val (Pmode, PR_REG);
5122 pr_live = (pr_initial
5123 ? (GET_CODE (pr_initial) != REG
5124 || REGNO (pr_initial) != (PR_REG))
5125 : regs_ever_live[PR_REG]);
5126 /* For Shcompact, if not optimizing, we end up with a memory reference
5127 using the return address pointer for __builtin_return_address even
5128 though there is no actual need to put the PR register on the stack. */
5129 pr_live |= regs_ever_live[RETURN_ADDRESS_POINTER_REGNUM];
5131 /* Force PR to be live if the prologue has to call the SHmedia
5132 argument decoder or register saver. */
5133 if (TARGET_SHCOMPACT
5134 && ((current_function_args_info.call_cookie
5135 & ~ CALL_COOKIE_RET_TRAMP (1))
5136 || current_function_has_nonlocal_label))
5137 pr_live = 1;
5138 has_call = TARGET_SHMEDIA ? ! leaf_function_p () : pr_live;
5139 for (count = 0, reg = FIRST_PSEUDO_REGISTER - 1; reg >= 0; reg--)
5141 if (reg == (TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG)
5142 ? pr_live
5143 : (interrupt_handler && ! pragma_trapa)
5144 ? (/* Need to save all the regs ever live. */
5145 (regs_ever_live[reg]
5146 || (call_used_regs[reg]
5147 && (! fixed_regs[reg] || reg == MACH_REG || reg == MACL_REG)
5148 && has_call)
5149 || (has_call && REGISTER_NATURAL_MODE (reg) == SImode
5150 && (GENERAL_REGISTER_P (reg) || TARGET_REGISTER_P (reg))))
5151 && reg != STACK_POINTER_REGNUM && reg != ARG_POINTER_REGNUM
5152 && reg != RETURN_ADDRESS_POINTER_REGNUM
5153 && reg != T_REG && reg != GBR_REG
5154 /* Push fpscr only on targets which have FPU */
5155 && (reg != FPSCR_REG || TARGET_FPU_ANY))
5156 : (/* Only push those regs which are used and need to be saved. */
5157 (TARGET_SHCOMPACT
5158 && flag_pic
5159 && current_function_args_info.call_cookie
5160 && reg == (int) PIC_OFFSET_TABLE_REGNUM)
5161 || (regs_ever_live[reg] && ! call_used_regs[reg])
5162 || (current_function_calls_eh_return
5163 && (reg == (int) EH_RETURN_DATA_REGNO (0)
5164 || reg == (int) EH_RETURN_DATA_REGNO (1)
5165 || reg == (int) EH_RETURN_DATA_REGNO (2)
5166 || reg == (int) EH_RETURN_DATA_REGNO (3)))
5167 || ((reg == MACL_REG || reg == MACH_REG)
5168 && regs_ever_live[reg]
5169 && sh_cfun_attr_renesas_p ())
5172 SET_HARD_REG_BIT (*live_regs_mask, reg);
5173 count += GET_MODE_SIZE (REGISTER_NATURAL_MODE (reg));
5175 if ((TARGET_SH4 || TARGET_SH5) && TARGET_FMOVD
5176 && GET_MODE_CLASS (REGISTER_NATURAL_MODE (reg)) == MODE_FLOAT)
5178 if (FP_REGISTER_P (reg))
5180 if (! TARGET_FPU_SINGLE && ! regs_ever_live[reg ^ 1])
5182 SET_HARD_REG_BIT (*live_regs_mask, (reg ^ 1));
5183 count += GET_MODE_SIZE (REGISTER_NATURAL_MODE (reg ^ 1));
5186 else if (XD_REGISTER_P (reg))
5188 /* Must switch to double mode to access these registers. */
5189 target_flags &= ~FPU_SINGLE_BIT;
5194 /* If we have a target register optimization pass after prologue / epilogue
5195 threading, we need to assume all target registers will be live even if
5196 they aren't now. */
5197 if (flag_branch_target_load_optimize2
5198 && TARGET_SAVE_ALL_TARGET_REGS
5199 && shmedia_space_reserved_for_target_registers)
5200 for (reg = LAST_TARGET_REG; reg >= FIRST_TARGET_REG; reg--)
5201 if ((! call_used_regs[reg] || interrupt_handler)
5202 && ! TEST_HARD_REG_BIT (*live_regs_mask, reg))
5204 SET_HARD_REG_BIT (*live_regs_mask, reg);
5205 count += GET_MODE_SIZE (REGISTER_NATURAL_MODE (reg));
5207 /* If this is an interrupt handler, we don't have any call-clobbered
5208 registers we can conveniently use for target register save/restore.
5209 Make sure we save at least one general purpose register when we need
5210 to save target registers. */
5211 if (interrupt_handler
5212 && hard_regs_intersect_p (live_regs_mask,
5213 &reg_class_contents[TARGET_REGS])
5214 && ! hard_regs_intersect_p (live_regs_mask,
5215 &reg_class_contents[GENERAL_REGS]))
5217 SET_HARD_REG_BIT (*live_regs_mask, R0_REG);
5218 count += GET_MODE_SIZE (REGISTER_NATURAL_MODE (R0_REG));
5221 return count;
5224 /* Code to generate prologue and epilogue sequences */
5226 /* PUSHED is the number of bytes that are being pushed on the
5227 stack for register saves. Return the frame size, padded
5228 appropriately so that the stack stays properly aligned. */
5229 static HOST_WIDE_INT
5230 rounded_frame_size (int pushed)
5232 HOST_WIDE_INT size = get_frame_size ();
5233 HOST_WIDE_INT align = STACK_BOUNDARY / BITS_PER_UNIT;
5235 return ((size + pushed + align - 1) & -align) - pushed;
5238 /* Choose a call-clobbered target-branch register that remains
5239 unchanged along the whole function. We set it up as the return
5240 value in the prologue. */
5242 sh_media_register_for_return (void)
5244 int regno;
5245 int tr0_used;
5247 if (! current_function_is_leaf)
5248 return -1;
5249 if (lookup_attribute ("interrupt_handler",
5250 DECL_ATTRIBUTES (current_function_decl)))
5251 return -1;
5253 tr0_used = flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM];
5255 for (regno = FIRST_TARGET_REG + tr0_used; regno <= LAST_TARGET_REG; regno++)
5256 if (call_used_regs[regno] && ! regs_ever_live[regno])
5257 return regno;
5259 return -1;
5262 /* The maximum registers we need to save are:
5263 - 62 general purpose registers (r15 is stack pointer, r63 is zero)
5264 - 32 floating point registers (for each pair, we save none,
5265 one single precision value, or a double precision value).
5266 - 8 target registers
5267 - add 1 entry for a delimiter. */
5268 #define MAX_SAVED_REGS (62+32+8)
5270 typedef struct save_entry_s
5272 unsigned char reg;
5273 unsigned char mode;
5274 short offset;
5275 } save_entry;
5277 #define MAX_TEMPS 4
5279 /* There will be a delimiter entry with VOIDmode both at the start and the
5280 end of a filled in schedule. The end delimiter has the offset of the
5281 save with the smallest (i.e. most negative) offset. */
5282 typedef struct save_schedule_s
5284 save_entry entries[MAX_SAVED_REGS + 2];
5285 int temps[MAX_TEMPS+1];
5286 } save_schedule;
5288 /* Fill in SCHEDULE according to LIVE_REGS_MASK. If RESTORE is nonzero,
5289 use reverse order. Returns the last entry written to (not counting
5290 the delimiter). OFFSET_BASE is a number to be added to all offset
5291 entries. */
5293 static save_entry *
5294 sh5_schedule_saves (HARD_REG_SET *live_regs_mask, save_schedule *schedule,
5295 int offset_base)
5297 int align, i;
5298 save_entry *entry = schedule->entries;
5299 int tmpx = 0;
5300 int offset;
5302 if (! current_function_interrupt)
5303 for (i = FIRST_GENERAL_REG; tmpx < MAX_TEMPS && i <= LAST_GENERAL_REG; i++)
5304 if (call_used_regs[i] && ! fixed_regs[i] && i != PR_MEDIA_REG
5305 && ! FUNCTION_ARG_REGNO_P (i)
5306 && i != FIRST_RET_REG
5307 && ! (cfun->static_chain_decl != NULL && i == STATIC_CHAIN_REGNUM)
5308 && ! (current_function_calls_eh_return
5309 && (i == EH_RETURN_STACKADJ_REGNO
5310 || ((unsigned) i <= EH_RETURN_DATA_REGNO (0)
5311 && (unsigned) i >= EH_RETURN_DATA_REGNO (3)))))
5312 schedule->temps[tmpx++] = i;
5313 entry->reg = -1;
5314 entry->mode = VOIDmode;
5315 entry->offset = offset_base;
5316 entry++;
5317 /* We loop twice: first, we save 8-byte aligned registers in the
5318 higher addresses, that are known to be aligned. Then, we
5319 proceed to saving 32-bit registers that don't need 8-byte
5320 alignment.
5321 If this is an interrupt function, all registers that need saving
5322 need to be saved in full. moreover, we need to postpone saving
5323 target registers till we have saved some general purpose registers
5324 we can then use as scratch registers. */
5325 offset = offset_base;
5326 for (align = 1; align >= 0; align--)
5328 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
5329 if (TEST_HARD_REG_BIT (*live_regs_mask, i))
5331 enum machine_mode mode = REGISTER_NATURAL_MODE (i);
5332 int reg = i;
5334 if (current_function_interrupt)
5336 if (TARGET_REGISTER_P (i))
5337 continue;
5338 if (GENERAL_REGISTER_P (i))
5339 mode = DImode;
5341 if (mode == SFmode && (i % 2) == 1
5342 && ! TARGET_FPU_SINGLE && FP_REGISTER_P (i)
5343 && (TEST_HARD_REG_BIT (*live_regs_mask, (i ^ 1))))
5345 mode = DFmode;
5346 i--;
5347 reg--;
5350 /* If we're doing the aligned pass and this is not aligned,
5351 or we're doing the unaligned pass and this is aligned,
5352 skip it. */
5353 if ((GET_MODE_SIZE (mode) % (STACK_BOUNDARY / BITS_PER_UNIT) == 0)
5354 != align)
5355 continue;
5357 if (current_function_interrupt
5358 && GENERAL_REGISTER_P (i)
5359 && tmpx < MAX_TEMPS)
5360 schedule->temps[tmpx++] = i;
5362 offset -= GET_MODE_SIZE (mode);
5363 entry->reg = i;
5364 entry->mode = mode;
5365 entry->offset = offset;
5366 entry++;
5368 if (align && current_function_interrupt)
5369 for (i = LAST_TARGET_REG; i >= FIRST_TARGET_REG; i--)
5370 if (TEST_HARD_REG_BIT (*live_regs_mask, i))
5372 offset -= GET_MODE_SIZE (DImode);
5373 entry->reg = i;
5374 entry->mode = DImode;
5375 entry->offset = offset;
5376 entry++;
5379 entry->reg = -1;
5380 entry->mode = VOIDmode;
5381 entry->offset = offset;
5382 schedule->temps[tmpx] = -1;
5383 return entry - 1;
5386 void
5387 sh_expand_prologue (void)
5389 HARD_REG_SET live_regs_mask;
5390 int d, i;
5391 int d_rounding = 0;
5392 int save_flags = target_flags;
5393 int pretend_args;
5395 current_function_interrupt = sh_cfun_interrupt_handler_p ();
5397 /* We have pretend args if we had an object sent partially in registers
5398 and partially on the stack, e.g. a large structure. */
5399 pretend_args = current_function_pretend_args_size;
5400 if (TARGET_VARARGS_PRETEND_ARGS (current_function_decl)
5401 && (NPARM_REGS(SImode)
5402 > current_function_args_info.arg_count[(int) SH_ARG_INT]))
5403 pretend_args = 0;
5404 output_stack_adjust (-pretend_args
5405 - current_function_args_info.stack_regs * 8,
5406 stack_pointer_rtx, 0, NULL);
5408 if (TARGET_SHCOMPACT && flag_pic && current_function_args_info.call_cookie)
5409 /* We're going to use the PIC register to load the address of the
5410 incoming-argument decoder and/or of the return trampoline from
5411 the GOT, so make sure the PIC register is preserved and
5412 initialized. */
5413 regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
5415 if (TARGET_SHCOMPACT
5416 && (current_function_args_info.call_cookie & ~ CALL_COOKIE_RET_TRAMP(1)))
5418 int reg;
5420 /* First, make all registers with incoming arguments that will
5421 be pushed onto the stack live, so that register renaming
5422 doesn't overwrite them. */
5423 for (reg = 0; reg < NPARM_REGS (SImode); reg++)
5424 if (CALL_COOKIE_STACKSEQ_GET (current_function_args_info.call_cookie)
5425 >= NPARM_REGS (SImode) - reg)
5426 for (; reg < NPARM_REGS (SImode); reg++)
5427 emit_insn (gen_shcompact_preserve_incoming_args
5428 (gen_rtx_REG (SImode, FIRST_PARM_REG + reg)));
5429 else if (CALL_COOKIE_INT_REG_GET
5430 (current_function_args_info.call_cookie, reg) == 1)
5431 emit_insn (gen_shcompact_preserve_incoming_args
5432 (gen_rtx_REG (SImode, FIRST_PARM_REG + reg)));
5434 emit_move_insn (gen_rtx_REG (Pmode, MACL_REG),
5435 stack_pointer_rtx);
5436 emit_move_insn (gen_rtx_REG (SImode, R0_REG),
5437 GEN_INT (current_function_args_info.call_cookie));
5438 emit_move_insn (gen_rtx_REG (SImode, MACH_REG),
5439 gen_rtx_REG (SImode, R0_REG));
5441 else if (TARGET_SHMEDIA)
5443 int tr = sh_media_register_for_return ();
5445 if (tr >= 0)
5447 rtx insn = emit_move_insn (gen_rtx_REG (DImode, tr),
5448 gen_rtx_REG (DImode, PR_MEDIA_REG));
5450 /* ??? We should suppress saving pr when we don't need it, but this
5451 is tricky because of builtin_return_address. */
5453 /* If this function only exits with sibcalls, this copy
5454 will be flagged as dead. */
5455 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
5456 const0_rtx,
5457 REG_NOTES (insn));
5461 /* Emit the code for SETUP_VARARGS. */
5462 if (current_function_stdarg)
5464 if (TARGET_VARARGS_PRETEND_ARGS (current_function_decl))
5466 /* Push arg regs as if they'd been provided by caller in stack. */
5467 for (i = 0; i < NPARM_REGS(SImode); i++)
5469 int rn = NPARM_REGS(SImode) + FIRST_PARM_REG - i - 1;
5470 rtx insn;
5472 if (i >= (NPARM_REGS(SImode)
5473 - current_function_args_info.arg_count[(int) SH_ARG_INT]
5475 break;
5476 insn = push (rn);
5477 RTX_FRAME_RELATED_P (insn) = 0;
5482 /* If we're supposed to switch stacks at function entry, do so now. */
5483 if (sp_switch)
5484 emit_insn (gen_sp_switch_1 ());
5486 d = calc_live_regs (&live_regs_mask);
5487 /* ??? Maybe we could save some switching if we can move a mode switch
5488 that already happens to be at the function start into the prologue. */
5489 if (target_flags != save_flags && ! current_function_interrupt)
5490 emit_insn (gen_toggle_sz ());
5492 if (TARGET_SH5)
5494 int offset_base, offset;
5495 rtx r0 = NULL_RTX;
5496 int offset_in_r0 = -1;
5497 int sp_in_r0 = 0;
5498 int tregs_space = shmedia_target_regs_stack_adjust (&live_regs_mask);
5499 int total_size, save_size;
5500 save_schedule schedule;
5501 save_entry *entry;
5502 int *tmp_pnt;
5504 if (call_used_regs[R0_REG] && ! fixed_regs[R0_REG]
5505 && ! current_function_interrupt)
5506 r0 = gen_rtx_REG (Pmode, R0_REG);
5508 /* D is the actual number of bytes that we need for saving registers,
5509 however, in initial_elimination_offset we have committed to using
5510 an additional TREGS_SPACE amount of bytes - in order to keep both
5511 addresses to arguments supplied by the caller and local variables
5512 valid, we must keep this gap. Place it between the incoming
5513 arguments and the actually saved registers in a bid to optimize
5514 locality of reference. */
5515 total_size = d + tregs_space;
5516 total_size += rounded_frame_size (total_size);
5517 save_size = total_size - rounded_frame_size (d);
5518 if (save_size % (STACK_BOUNDARY / BITS_PER_UNIT))
5519 d_rounding = ((STACK_BOUNDARY / BITS_PER_UNIT)
5520 - save_size % (STACK_BOUNDARY / BITS_PER_UNIT));
5522 /* If adjusting the stack in a single step costs nothing extra, do so.
5523 I.e. either if a single addi is enough, or we need a movi anyway,
5524 and we don't exceed the maximum offset range (the test for the
5525 latter is conservative for simplicity). */
5526 if (TARGET_SHMEDIA
5527 && (CONST_OK_FOR_I10 (-total_size)
5528 || (! CONST_OK_FOR_I10 (-(save_size + d_rounding))
5529 && total_size <= 2044)))
5530 d_rounding = total_size - save_size;
5532 offset_base = d + d_rounding;
5534 output_stack_adjust (-(save_size + d_rounding), stack_pointer_rtx,
5535 0, NULL);
5537 sh5_schedule_saves (&live_regs_mask, &schedule, offset_base);
5538 tmp_pnt = schedule.temps;
5539 for (entry = &schedule.entries[1]; entry->mode != VOIDmode; entry++)
5541 enum machine_mode mode = entry->mode;
5542 int reg = entry->reg;
5543 rtx reg_rtx, mem_rtx, pre_dec = NULL_RTX;
5545 offset = entry->offset;
5547 reg_rtx = gen_rtx_REG (mode, reg);
5549 mem_rtx = gen_rtx_MEM (mode,
5550 gen_rtx_PLUS (Pmode,
5551 stack_pointer_rtx,
5552 GEN_INT (offset)));
5554 GO_IF_LEGITIMATE_ADDRESS (mode, XEXP (mem_rtx, 0), try_pre_dec);
5556 if (! r0)
5557 abort ();
5558 mem_rtx = NULL_RTX;
5560 try_pre_dec:
5562 if (HAVE_PRE_DECREMENT
5563 && (offset_in_r0 - offset == GET_MODE_SIZE (mode)
5564 || mem_rtx == NULL_RTX
5565 || reg == PR_REG || SPECIAL_REGISTER_P (reg)))
5567 pre_dec = gen_rtx_MEM (mode,
5568 gen_rtx_PRE_DEC (Pmode, r0));
5570 GO_IF_LEGITIMATE_ADDRESS (mode, XEXP (pre_dec, 0),
5571 pre_dec_ok);
5573 pre_dec = NULL_RTX;
5575 break;
5577 pre_dec_ok:
5578 mem_rtx = NULL_RTX;
5579 offset += GET_MODE_SIZE (mode);
5581 while (0);
5583 if (mem_rtx != NULL_RTX)
5584 goto addr_ok;
5586 if (offset_in_r0 == -1)
5588 emit_move_insn (r0, GEN_INT (offset));
5589 offset_in_r0 = offset;
5591 else if (offset != offset_in_r0)
5593 emit_move_insn (r0,
5594 gen_rtx_PLUS
5595 (Pmode, r0,
5596 GEN_INT (offset - offset_in_r0)));
5597 offset_in_r0 += offset - offset_in_r0;
5600 if (pre_dec != NULL_RTX)
5602 if (! sp_in_r0)
5604 emit_move_insn (r0,
5605 gen_rtx_PLUS
5606 (Pmode, r0, stack_pointer_rtx));
5607 sp_in_r0 = 1;
5610 offset -= GET_MODE_SIZE (mode);
5611 offset_in_r0 -= GET_MODE_SIZE (mode);
5613 mem_rtx = pre_dec;
5615 else if (sp_in_r0)
5616 mem_rtx = gen_rtx_MEM (mode, r0);
5617 else
5618 mem_rtx = gen_rtx_MEM (mode,
5619 gen_rtx_PLUS (Pmode,
5620 stack_pointer_rtx,
5621 r0));
5623 /* We must not use an r0-based address for target-branch
5624 registers or for special registers without pre-dec
5625 memory addresses, since we store their values in r0
5626 first. */
5627 if (TARGET_REGISTER_P (reg)
5628 || ((reg == PR_REG || SPECIAL_REGISTER_P (reg))
5629 && mem_rtx != pre_dec))
5630 abort ();
5632 addr_ok:
5633 if (TARGET_REGISTER_P (reg)
5634 || ((reg == PR_REG || SPECIAL_REGISTER_P (reg))
5635 && mem_rtx != pre_dec))
5637 rtx tmp_reg = gen_rtx_REG (GET_MODE (reg_rtx), *tmp_pnt);
5639 emit_move_insn (tmp_reg, reg_rtx);
5641 if (REGNO (tmp_reg) == R0_REG)
5643 offset_in_r0 = -1;
5644 sp_in_r0 = 0;
5645 if (refers_to_regno_p (R0_REG, R0_REG+1, mem_rtx, (rtx *) 0))
5646 abort ();
5649 if (*++tmp_pnt <= 0)
5650 tmp_pnt = schedule.temps;
5652 reg_rtx = tmp_reg;
5655 rtx insn;
5657 /* Mark as interesting for dwarf cfi generator */
5658 insn = emit_move_insn (mem_rtx, reg_rtx);
5659 RTX_FRAME_RELATED_P (insn) = 1;
5661 if (TARGET_SHCOMPACT && (offset_in_r0 != -1))
5663 rtx reg_rtx = gen_rtx_REG (mode, reg);
5664 rtx set, note_rtx;
5665 rtx mem_rtx = gen_rtx_MEM (mode,
5666 gen_rtx_PLUS (Pmode,
5667 stack_pointer_rtx,
5668 GEN_INT (offset)));
5670 set = gen_rtx_SET (VOIDmode, mem_rtx, reg_rtx);
5671 note_rtx = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, set,
5672 REG_NOTES (insn));
5673 REG_NOTES (insn) = note_rtx;
5678 if (entry->offset != d_rounding)
5679 abort ();
5681 else
5682 push_regs (&live_regs_mask, current_function_interrupt);
5684 if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
5686 rtx insn = get_last_insn ();
5687 rtx last = emit_insn (gen_GOTaddr2picreg ());
5689 /* Mark these insns as possibly dead. Sometimes, flow2 may
5690 delete all uses of the PIC register. In this case, let it
5691 delete the initialization too. */
5694 insn = NEXT_INSN (insn);
5696 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
5697 const0_rtx,
5698 REG_NOTES (insn));
5700 while (insn != last);
5703 if (SHMEDIA_REGS_STACK_ADJUST ())
5705 emit_move_insn (gen_rtx_REG (Pmode, R0_REG),
5706 function_symbol (TARGET_FPU_ANY
5707 ? "__GCC_push_shmedia_regs"
5708 : "__GCC_push_shmedia_regs_nofpu"));
5709 /* This must NOT go through the PLT, otherwise mach and macl
5710 may be clobbered. */
5711 emit_insn (gen_shmedia_save_restore_regs_compact
5712 (GEN_INT (-SHMEDIA_REGS_STACK_ADJUST ())));
5715 if (target_flags != save_flags && ! current_function_interrupt)
5717 rtx insn = emit_insn (gen_toggle_sz ());
5719 /* If we're lucky, a mode switch in the function body will
5720 overwrite fpscr, turning this insn dead. Tell flow this
5721 insn is ok to delete. */
5722 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
5723 const0_rtx,
5724 REG_NOTES (insn));
5727 target_flags = save_flags;
5729 output_stack_adjust (-rounded_frame_size (d) + d_rounding,
5730 stack_pointer_rtx, 0, NULL);
5732 if (frame_pointer_needed)
5733 frame_insn (GEN_MOV (frame_pointer_rtx, stack_pointer_rtx));
5735 if (TARGET_SHCOMPACT
5736 && (current_function_args_info.call_cookie & ~ CALL_COOKIE_RET_TRAMP(1)))
5738 /* This must NOT go through the PLT, otherwise mach and macl
5739 may be clobbered. */
5740 emit_move_insn (gen_rtx_REG (Pmode, R0_REG),
5741 function_symbol ("__GCC_shcompact_incoming_args"));
5742 emit_insn (gen_shcompact_incoming_args ());
5746 void
5747 sh_expand_epilogue (bool sibcall_p)
5749 HARD_REG_SET live_regs_mask;
5750 int d, i;
5751 int d_rounding = 0;
5753 int save_flags = target_flags;
5754 int frame_size, save_size;
5755 int fpscr_deferred = 0;
5756 int e = sibcall_p ? -1 : 1;
5758 d = calc_live_regs (&live_regs_mask);
5760 save_size = d;
5761 frame_size = rounded_frame_size (d);
5763 if (TARGET_SH5)
5765 int tregs_space = shmedia_target_regs_stack_adjust (&live_regs_mask);
5766 int total_size;
5767 if (d % (STACK_BOUNDARY / BITS_PER_UNIT))
5768 d_rounding = ((STACK_BOUNDARY / BITS_PER_UNIT)
5769 - d % (STACK_BOUNDARY / BITS_PER_UNIT));
5771 total_size = d + tregs_space;
5772 total_size += rounded_frame_size (total_size);
5773 save_size = total_size - frame_size;
5775 /* If adjusting the stack in a single step costs nothing extra, do so.
5776 I.e. either if a single addi is enough, or we need a movi anyway,
5777 and we don't exceed the maximum offset range (the test for the
5778 latter is conservative for simplicity). */
5779 if (TARGET_SHMEDIA
5780 && ! frame_pointer_needed
5781 && (CONST_OK_FOR_I10 (total_size)
5782 || (! CONST_OK_FOR_I10 (save_size + d_rounding)
5783 && total_size <= 2044)))
5784 d_rounding = frame_size;
5786 frame_size -= d_rounding;
5789 if (frame_pointer_needed)
5791 output_stack_adjust (frame_size, frame_pointer_rtx, e, &live_regs_mask);
5793 /* We must avoid moving the stack pointer adjustment past code
5794 which reads from the local frame, else an interrupt could
5795 occur after the SP adjustment and clobber data in the local
5796 frame. */
5797 emit_insn (gen_blockage ());
5798 emit_insn (GEN_MOV (stack_pointer_rtx, frame_pointer_rtx));
5800 else if (frame_size)
5802 /* We must avoid moving the stack pointer adjustment past code
5803 which reads from the local frame, else an interrupt could
5804 occur after the SP adjustment and clobber data in the local
5805 frame. */
5806 emit_insn (gen_blockage ());
5807 output_stack_adjust (frame_size, stack_pointer_rtx, e, &live_regs_mask);
5810 if (SHMEDIA_REGS_STACK_ADJUST ())
5812 emit_move_insn (gen_rtx_REG (Pmode, R0_REG),
5813 function_symbol (TARGET_FPU_ANY
5814 ? "__GCC_pop_shmedia_regs"
5815 : "__GCC_pop_shmedia_regs_nofpu"));
5816 /* This must NOT go through the PLT, otherwise mach and macl
5817 may be clobbered. */
5818 emit_insn (gen_shmedia_save_restore_regs_compact
5819 (GEN_INT (SHMEDIA_REGS_STACK_ADJUST ())));
5822 /* Pop all the registers. */
5824 if (target_flags != save_flags && ! current_function_interrupt)
5825 emit_insn (gen_toggle_sz ());
5826 if (TARGET_SH5)
5828 int offset_base, offset;
5829 int offset_in_r0 = -1;
5830 int sp_in_r0 = 0;
5831 rtx r0 = gen_rtx_REG (Pmode, R0_REG);
5832 save_schedule schedule;
5833 save_entry *entry;
5834 int *tmp_pnt;
5836 entry = sh5_schedule_saves (&live_regs_mask, &schedule, d_rounding);
5837 offset_base = -entry[1].offset + d_rounding;
5838 tmp_pnt = schedule.temps;
5839 for (; entry->mode != VOIDmode; entry--)
5841 enum machine_mode mode = entry->mode;
5842 int reg = entry->reg;
5843 rtx reg_rtx, mem_rtx, post_inc = NULL_RTX, insn;
5845 offset = offset_base + entry->offset;
5846 reg_rtx = gen_rtx_REG (mode, reg);
5848 mem_rtx = gen_rtx_MEM (mode,
5849 gen_rtx_PLUS (Pmode,
5850 stack_pointer_rtx,
5851 GEN_INT (offset)));
5853 GO_IF_LEGITIMATE_ADDRESS (mode, XEXP (mem_rtx, 0), try_post_inc);
5855 mem_rtx = NULL_RTX;
5857 try_post_inc:
5859 if (HAVE_POST_INCREMENT
5860 && (offset == offset_in_r0
5861 || (offset + GET_MODE_SIZE (mode) != d + d_rounding
5862 && mem_rtx == NULL_RTX)
5863 || reg == PR_REG || SPECIAL_REGISTER_P (reg)))
5865 post_inc = gen_rtx_MEM (mode,
5866 gen_rtx_POST_INC (Pmode, r0));
5868 GO_IF_LEGITIMATE_ADDRESS (mode, XEXP (post_inc, 0),
5869 post_inc_ok);
5871 post_inc = NULL_RTX;
5873 break;
5875 post_inc_ok:
5876 mem_rtx = NULL_RTX;
5878 while (0);
5880 if (mem_rtx != NULL_RTX)
5881 goto addr_ok;
5883 if (offset_in_r0 == -1)
5885 emit_move_insn (r0, GEN_INT (offset));
5886 offset_in_r0 = offset;
5888 else if (offset != offset_in_r0)
5890 emit_move_insn (r0,
5891 gen_rtx_PLUS
5892 (Pmode, r0,
5893 GEN_INT (offset - offset_in_r0)));
5894 offset_in_r0 += offset - offset_in_r0;
5897 if (post_inc != NULL_RTX)
5899 if (! sp_in_r0)
5901 emit_move_insn (r0,
5902 gen_rtx_PLUS
5903 (Pmode, r0, stack_pointer_rtx));
5904 sp_in_r0 = 1;
5907 mem_rtx = post_inc;
5909 offset_in_r0 += GET_MODE_SIZE (mode);
5911 else if (sp_in_r0)
5912 mem_rtx = gen_rtx_MEM (mode, r0);
5913 else
5914 mem_rtx = gen_rtx_MEM (mode,
5915 gen_rtx_PLUS (Pmode,
5916 stack_pointer_rtx,
5917 r0));
5919 if ((reg == PR_REG || SPECIAL_REGISTER_P (reg))
5920 && mem_rtx != post_inc)
5921 abort ();
5923 addr_ok:
5924 if ((reg == PR_REG || SPECIAL_REGISTER_P (reg))
5925 && mem_rtx != post_inc)
5927 insn = emit_move_insn (r0, mem_rtx);
5928 mem_rtx = r0;
5930 else if (TARGET_REGISTER_P (reg))
5932 rtx tmp_reg = gen_rtx_REG (mode, *tmp_pnt);
5934 /* Give the scheduler a bit of freedom by using up to
5935 MAX_TEMPS registers in a round-robin fashion. */
5936 insn = emit_move_insn (tmp_reg, mem_rtx);
5937 mem_rtx = tmp_reg;
5938 if (*++tmp_pnt < 0)
5939 tmp_pnt = schedule.temps;
5942 insn = emit_move_insn (reg_rtx, mem_rtx);
5943 if (reg == PR_MEDIA_REG && sh_media_register_for_return () >= 0)
5944 /* This is dead, unless we return with a sibcall. */
5945 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
5946 const0_rtx,
5947 REG_NOTES (insn));
5950 if (entry->offset + offset_base != d + d_rounding)
5951 abort ();
5953 else /* ! TARGET_SH5 */
5955 save_size = 0;
5956 if (TEST_HARD_REG_BIT (live_regs_mask, PR_REG))
5957 pop (PR_REG);
5958 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5960 int j = (FIRST_PSEUDO_REGISTER - 1) - i;
5962 if (j == FPSCR_REG && current_function_interrupt && TARGET_FMOVD
5963 && hard_regs_intersect_p (&live_regs_mask,
5964 &reg_class_contents[DF_REGS]))
5965 fpscr_deferred = 1;
5966 else if (j != PR_REG && TEST_HARD_REG_BIT (live_regs_mask, j))
5967 pop (j);
5968 if (j == FIRST_FP_REG && fpscr_deferred)
5969 pop (FPSCR_REG);
5973 if (target_flags != save_flags && ! current_function_interrupt)
5974 emit_insn (gen_toggle_sz ());
5975 target_flags = save_flags;
5977 output_stack_adjust (current_function_pretend_args_size
5978 + save_size + d_rounding
5979 + current_function_args_info.stack_regs * 8,
5980 stack_pointer_rtx, e, NULL);
5982 if (current_function_calls_eh_return)
5983 emit_insn (GEN_ADD3 (stack_pointer_rtx, stack_pointer_rtx,
5984 EH_RETURN_STACKADJ_RTX));
5986 /* Switch back to the normal stack if necessary. */
5987 if (sp_switch)
5988 emit_insn (gen_sp_switch_2 ());
5990 /* Tell flow the insn that pops PR isn't dead. */
5991 /* PR_REG will never be live in SHmedia mode, and we don't need to
5992 USE PR_MEDIA_REG, since it will be explicitly copied to TR0_REG
5993 by the return pattern. */
5994 if (TEST_HARD_REG_BIT (live_regs_mask, PR_REG))
5995 emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, PR_REG)));
5998 static int sh_need_epilogue_known = 0;
6001 sh_need_epilogue (void)
6003 if (! sh_need_epilogue_known)
6005 rtx epilogue;
6007 start_sequence ();
6008 sh_expand_epilogue (0);
6009 epilogue = get_insns ();
6010 end_sequence ();
6011 sh_need_epilogue_known = (epilogue == NULL ? -1 : 1);
6013 return sh_need_epilogue_known > 0;
6016 /* Emit code to change the current function's return address to RA.
6017 TEMP is available as a scratch register, if needed. */
6019 void
6020 sh_set_return_address (rtx ra, rtx tmp)
6022 HARD_REG_SET live_regs_mask;
6023 int d;
6024 int pr_reg = TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG;
6025 int pr_offset;
6027 d = calc_live_regs (&live_regs_mask);
6029 /* If pr_reg isn't life, we can set it (or the register given in
6030 sh_media_register_for_return) directly. */
6031 if (! TEST_HARD_REG_BIT (live_regs_mask, pr_reg))
6033 rtx rr;
6035 if (TARGET_SHMEDIA)
6037 int rr_regno = sh_media_register_for_return ();
6039 if (rr_regno < 0)
6040 rr_regno = pr_reg;
6042 rr = gen_rtx_REG (DImode, rr_regno);
6044 else
6045 rr = gen_rtx_REG (SImode, pr_reg);
6047 emit_insn (GEN_MOV (rr, ra));
6048 /* Tell flow the register for return isn't dead. */
6049 emit_insn (gen_rtx_USE (VOIDmode, rr));
6050 return;
6053 if (TARGET_SH5)
6055 int offset;
6056 save_schedule schedule;
6057 save_entry *entry;
6059 entry = sh5_schedule_saves (&live_regs_mask, &schedule, 0);
6060 offset = entry[1].offset;
6061 for (; entry->mode != VOIDmode; entry--)
6062 if (entry->reg == pr_reg)
6063 goto found;
6065 /* We can't find pr register. */
6066 abort ();
6068 found:
6069 offset = entry->offset - offset;
6070 pr_offset = (rounded_frame_size (d) + offset
6071 + SHMEDIA_REGS_STACK_ADJUST ());
6073 else
6074 pr_offset = rounded_frame_size (d);
6076 emit_insn (GEN_MOV (tmp, GEN_INT (pr_offset)));
6077 emit_insn (GEN_ADD3 (tmp, tmp, frame_pointer_rtx));
6079 tmp = gen_rtx_MEM (Pmode, tmp);
6080 emit_insn (GEN_MOV (tmp, ra));
6083 /* Clear variables at function end. */
6085 static void
6086 sh_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
6087 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6089 trap_exit = pragma_interrupt = pragma_trapa = pragma_nosave_low_regs = 0;
6090 sh_need_epilogue_known = 0;
6091 sp_switch = NULL_RTX;
6094 static rtx
6095 sh_builtin_saveregs (void)
6097 /* First unnamed integer register. */
6098 int first_intreg = current_function_args_info.arg_count[(int) SH_ARG_INT];
6099 /* Number of integer registers we need to save. */
6100 int n_intregs = MAX (0, NPARM_REGS (SImode) - first_intreg);
6101 /* First unnamed SFmode float reg */
6102 int first_floatreg = current_function_args_info.arg_count[(int) SH_ARG_FLOAT];
6103 /* Number of SFmode float regs to save. */
6104 int n_floatregs = MAX (0, NPARM_REGS (SFmode) - first_floatreg);
6105 rtx regbuf, fpregs;
6106 int bufsize, regno;
6107 HOST_WIDE_INT alias_set;
6109 if (TARGET_SH5)
6111 if (n_intregs)
6113 int pushregs = n_intregs;
6115 while (pushregs < NPARM_REGS (SImode) - 1
6116 && (CALL_COOKIE_INT_REG_GET
6117 (current_function_args_info.call_cookie,
6118 NPARM_REGS (SImode) - pushregs)
6119 == 1))
6121 current_function_args_info.call_cookie
6122 &= ~ CALL_COOKIE_INT_REG (NPARM_REGS (SImode)
6123 - pushregs, 1);
6124 pushregs++;
6127 if (pushregs == NPARM_REGS (SImode))
6128 current_function_args_info.call_cookie
6129 |= (CALL_COOKIE_INT_REG (0, 1)
6130 | CALL_COOKIE_STACKSEQ (pushregs - 1));
6131 else
6132 current_function_args_info.call_cookie
6133 |= CALL_COOKIE_STACKSEQ (pushregs);
6135 current_function_pretend_args_size += 8 * n_intregs;
6137 if (TARGET_SHCOMPACT)
6138 return const0_rtx;
6141 if (! TARGET_SH2E && ! TARGET_SH4 && ! TARGET_SH5)
6143 error ("__builtin_saveregs not supported by this subtarget");
6144 return const0_rtx;
6147 if (TARGET_SHMEDIA)
6148 n_floatregs = 0;
6150 /* Allocate block of memory for the regs. */
6151 /* ??? If n_intregs + n_floatregs == 0, should we allocate at least 1 byte?
6152 Or can assign_stack_local accept a 0 SIZE argument? */
6153 bufsize = (n_intregs * UNITS_PER_WORD) + (n_floatregs * UNITS_PER_WORD);
6155 if (TARGET_SHMEDIA)
6156 regbuf = gen_rtx_MEM (BLKmode,
6157 gen_rtx_REG (Pmode, ARG_POINTER_REGNUM));
6158 else if (n_floatregs & 1)
6160 rtx addr;
6162 regbuf = assign_stack_local (BLKmode, bufsize + UNITS_PER_WORD, 0);
6163 addr = copy_to_mode_reg (Pmode, XEXP (regbuf, 0));
6164 emit_insn (gen_iorsi3 (addr, addr, GEN_INT (UNITS_PER_WORD)));
6165 regbuf = change_address (regbuf, BLKmode, addr);
6167 else
6168 regbuf = assign_stack_local (BLKmode, bufsize, 0);
6169 alias_set = get_varargs_alias_set ();
6170 set_mem_alias_set (regbuf, alias_set);
6172 /* Save int args.
6173 This is optimized to only save the regs that are necessary. Explicitly
6174 named args need not be saved. */
6175 if (n_intregs > 0)
6176 move_block_from_reg (BASE_ARG_REG (SImode) + first_intreg,
6177 adjust_address (regbuf, BLKmode,
6178 n_floatregs * UNITS_PER_WORD),
6179 n_intregs);
6181 if (TARGET_SHMEDIA)
6182 /* Return the address of the regbuf. */
6183 return XEXP (regbuf, 0);
6185 /* Save float args.
6186 This is optimized to only save the regs that are necessary. Explicitly
6187 named args need not be saved.
6188 We explicitly build a pointer to the buffer because it halves the insn
6189 count when not optimizing (otherwise the pointer is built for each reg
6190 saved).
6191 We emit the moves in reverse order so that we can use predecrement. */
6193 fpregs = gen_reg_rtx (Pmode);
6194 emit_move_insn (fpregs, XEXP (regbuf, 0));
6195 emit_insn (gen_addsi3 (fpregs, fpregs,
6196 GEN_INT (n_floatregs * UNITS_PER_WORD)));
6197 if (TARGET_SH4)
6199 rtx mem;
6200 for (regno = NPARM_REGS (DFmode) - 2; regno >= first_floatreg; regno -= 2)
6202 emit_insn (gen_addsi3 (fpregs, fpregs,
6203 GEN_INT (-2 * UNITS_PER_WORD)));
6204 mem = gen_rtx_MEM (DFmode, fpregs);
6205 set_mem_alias_set (mem, alias_set);
6206 emit_move_insn (mem,
6207 gen_rtx_REG (DFmode, BASE_ARG_REG (DFmode) + regno));
6209 regno = first_floatreg;
6210 if (regno & 1)
6212 emit_insn (gen_addsi3 (fpregs, fpregs, GEN_INT (-UNITS_PER_WORD)));
6213 mem = gen_rtx_MEM (SFmode, fpregs);
6214 set_mem_alias_set (mem, alias_set);
6215 emit_move_insn (mem,
6216 gen_rtx_REG (SFmode, BASE_ARG_REG (SFmode) + regno
6217 - (TARGET_LITTLE_ENDIAN != 0)));
6220 else
6221 for (regno = NPARM_REGS (SFmode) - 1; regno >= first_floatreg; regno--)
6223 rtx mem;
6225 emit_insn (gen_addsi3 (fpregs, fpregs, GEN_INT (-UNITS_PER_WORD)));
6226 mem = gen_rtx_MEM (SFmode, fpregs);
6227 set_mem_alias_set (mem, alias_set);
6228 emit_move_insn (mem,
6229 gen_rtx_REG (SFmode, BASE_ARG_REG (SFmode) + regno));
6232 /* Return the address of the regbuf. */
6233 return XEXP (regbuf, 0);
6236 /* Define the `__builtin_va_list' type for the ABI. */
6238 static tree
6239 sh_build_builtin_va_list (void)
6241 tree f_next_o, f_next_o_limit, f_next_fp, f_next_fp_limit, f_next_stack;
6242 tree record;
6244 if (TARGET_SH5 || (! TARGET_SH2E && ! TARGET_SH4)
6245 || TARGET_HITACHI || sh_cfun_attr_renesas_p ())
6246 return ptr_type_node;
6248 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
6250 f_next_o = build_decl (FIELD_DECL, get_identifier ("__va_next_o"),
6251 ptr_type_node);
6252 f_next_o_limit = build_decl (FIELD_DECL,
6253 get_identifier ("__va_next_o_limit"),
6254 ptr_type_node);
6255 f_next_fp = build_decl (FIELD_DECL, get_identifier ("__va_next_fp"),
6256 ptr_type_node);
6257 f_next_fp_limit = build_decl (FIELD_DECL,
6258 get_identifier ("__va_next_fp_limit"),
6259 ptr_type_node);
6260 f_next_stack = build_decl (FIELD_DECL, get_identifier ("__va_next_stack"),
6261 ptr_type_node);
6263 DECL_FIELD_CONTEXT (f_next_o) = record;
6264 DECL_FIELD_CONTEXT (f_next_o_limit) = record;
6265 DECL_FIELD_CONTEXT (f_next_fp) = record;
6266 DECL_FIELD_CONTEXT (f_next_fp_limit) = record;
6267 DECL_FIELD_CONTEXT (f_next_stack) = record;
6269 TYPE_FIELDS (record) = f_next_o;
6270 TREE_CHAIN (f_next_o) = f_next_o_limit;
6271 TREE_CHAIN (f_next_o_limit) = f_next_fp;
6272 TREE_CHAIN (f_next_fp) = f_next_fp_limit;
6273 TREE_CHAIN (f_next_fp_limit) = f_next_stack;
6275 layout_type (record);
6277 return record;
6280 /* Implement `va_start' for varargs and stdarg. */
6282 void
6283 sh_va_start (tree valist, rtx nextarg)
6285 tree f_next_o, f_next_o_limit, f_next_fp, f_next_fp_limit, f_next_stack;
6286 tree next_o, next_o_limit, next_fp, next_fp_limit, next_stack;
6287 tree t, u;
6288 int nfp, nint;
6290 if (TARGET_SH5)
6292 expand_builtin_saveregs ();
6293 std_expand_builtin_va_start (valist, nextarg);
6294 return;
6297 if ((! TARGET_SH2E && ! TARGET_SH4)
6298 || TARGET_HITACHI || sh_cfun_attr_renesas_p ())
6300 std_expand_builtin_va_start (valist, nextarg);
6301 return;
6304 f_next_o = TYPE_FIELDS (va_list_type_node);
6305 f_next_o_limit = TREE_CHAIN (f_next_o);
6306 f_next_fp = TREE_CHAIN (f_next_o_limit);
6307 f_next_fp_limit = TREE_CHAIN (f_next_fp);
6308 f_next_stack = TREE_CHAIN (f_next_fp_limit);
6310 next_o = build (COMPONENT_REF, TREE_TYPE (f_next_o), valist, f_next_o,
6311 NULL_TREE);
6312 next_o_limit = build (COMPONENT_REF, TREE_TYPE (f_next_o_limit),
6313 valist, f_next_o_limit, NULL_TREE);
6314 next_fp = build (COMPONENT_REF, TREE_TYPE (f_next_fp), valist, f_next_fp,
6315 NULL_TREE);
6316 next_fp_limit = build (COMPONENT_REF, TREE_TYPE (f_next_fp_limit),
6317 valist, f_next_fp_limit, NULL_TREE);
6318 next_stack = build (COMPONENT_REF, TREE_TYPE (f_next_stack),
6319 valist, f_next_stack, NULL_TREE);
6321 /* Call __builtin_saveregs. */
6322 u = make_tree (ptr_type_node, expand_builtin_saveregs ());
6323 t = build (MODIFY_EXPR, ptr_type_node, next_fp, u);
6324 TREE_SIDE_EFFECTS (t) = 1;
6325 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6327 nfp = current_function_args_info.arg_count[SH_ARG_FLOAT];
6328 if (nfp < 8)
6329 nfp = 8 - nfp;
6330 else
6331 nfp = 0;
6332 u = fold (build (PLUS_EXPR, ptr_type_node, u,
6333 build_int_2 (UNITS_PER_WORD * nfp, 0)));
6334 t = build (MODIFY_EXPR, ptr_type_node, next_fp_limit, u);
6335 TREE_SIDE_EFFECTS (t) = 1;
6336 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6338 t = build (MODIFY_EXPR, ptr_type_node, next_o, u);
6339 TREE_SIDE_EFFECTS (t) = 1;
6340 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6342 nint = current_function_args_info.arg_count[SH_ARG_INT];
6343 if (nint < 4)
6344 nint = 4 - nint;
6345 else
6346 nint = 0;
6347 u = fold (build (PLUS_EXPR, ptr_type_node, u,
6348 build_int_2 (UNITS_PER_WORD * nint, 0)));
6349 t = build (MODIFY_EXPR, ptr_type_node, next_o_limit, u);
6350 TREE_SIDE_EFFECTS (t) = 1;
6351 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6353 u = make_tree (ptr_type_node, nextarg);
6354 t = build (MODIFY_EXPR, ptr_type_node, next_stack, u);
6355 TREE_SIDE_EFFECTS (t) = 1;
6356 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6359 /* Implement `va_arg'. */
6361 static tree
6362 sh_gimplify_va_arg_expr (tree valist, tree type, tree *pre_p,
6363 tree *post_p ATTRIBUTE_UNUSED)
6365 HOST_WIDE_INT size, rsize;
6366 tree tmp, pptr_type_node;
6367 tree addr, lab_over, result = NULL;
6368 int pass_by_ref = pass_by_reference (NULL, TYPE_MODE (type), type, false);
6370 if (pass_by_ref)
6371 type = build_pointer_type (type);
6373 size = int_size_in_bytes (type);
6374 rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
6375 pptr_type_node = build_pointer_type (ptr_type_node);
6377 if (! TARGET_SH5 && (TARGET_SH2E || TARGET_SH4)
6378 && ! (TARGET_HITACHI || sh_cfun_attr_renesas_p ()))
6380 tree f_next_o, f_next_o_limit, f_next_fp, f_next_fp_limit, f_next_stack;
6381 tree next_o, next_o_limit, next_fp, next_fp_limit, next_stack;
6382 int pass_as_float;
6383 tree lab_false;
6385 f_next_o = TYPE_FIELDS (va_list_type_node);
6386 f_next_o_limit = TREE_CHAIN (f_next_o);
6387 f_next_fp = TREE_CHAIN (f_next_o_limit);
6388 f_next_fp_limit = TREE_CHAIN (f_next_fp);
6389 f_next_stack = TREE_CHAIN (f_next_fp_limit);
6391 next_o = build (COMPONENT_REF, TREE_TYPE (f_next_o), valist, f_next_o,
6392 NULL_TREE);
6393 next_o_limit = build (COMPONENT_REF, TREE_TYPE (f_next_o_limit),
6394 valist, f_next_o_limit, NULL_TREE);
6395 next_fp = build (COMPONENT_REF, TREE_TYPE (f_next_fp),
6396 valist, f_next_fp, NULL_TREE);
6397 next_fp_limit = build (COMPONENT_REF, TREE_TYPE (f_next_fp_limit),
6398 valist, f_next_fp_limit, NULL_TREE);
6399 next_stack = build (COMPONENT_REF, TREE_TYPE (f_next_stack),
6400 valist, f_next_stack, NULL_TREE);
6402 /* Structures with a single member with a distinct mode are passed
6403 like their member. This is relevant if the latter has a REAL_TYPE
6404 or COMPLEX_TYPE type. */
6405 if (TREE_CODE (type) == RECORD_TYPE
6406 && TYPE_FIELDS (type)
6407 && TREE_CODE (TYPE_FIELDS (type)) == FIELD_DECL
6408 && (TREE_CODE (TREE_TYPE (TYPE_FIELDS (type))) == REAL_TYPE
6409 || TREE_CODE (TREE_TYPE (TYPE_FIELDS (type))) == COMPLEX_TYPE)
6410 && TREE_CHAIN (TYPE_FIELDS (type)) == NULL_TREE)
6411 type = TREE_TYPE (TYPE_FIELDS (type));
6413 if (TARGET_SH4)
6415 pass_as_float = ((TREE_CODE (type) == REAL_TYPE && size <= 8)
6416 || (TREE_CODE (type) == COMPLEX_TYPE
6417 && TREE_CODE (TREE_TYPE (type)) == REAL_TYPE
6418 && size <= 16));
6420 else
6422 pass_as_float = (TREE_CODE (type) == REAL_TYPE && size == 4);
6425 addr = create_tmp_var (pptr_type_node, NULL);
6426 lab_false = create_artificial_label ();
6427 lab_over = create_artificial_label ();
6429 valist = build1 (INDIRECT_REF, ptr_type_node, addr);
6431 if (pass_as_float)
6433 int first_floatreg
6434 = current_function_args_info.arg_count[(int) SH_ARG_FLOAT];
6435 int n_floatregs = MAX (0, NPARM_REGS (SFmode) - first_floatreg);
6437 tmp = build (GE_EXPR, boolean_type_node, next_fp, next_fp_limit);
6438 tmp = build (COND_EXPR, void_type_node, tmp,
6439 build (GOTO_EXPR, void_type_node, lab_false),
6440 NULL);
6441 gimplify_and_add (tmp, pre_p);
6443 if (TYPE_ALIGN (type) > BITS_PER_WORD
6444 || (((TREE_CODE (type) == REAL_TYPE && size == 8) || size == 16)
6445 && (n_floatregs & 1)))
6447 tmp = fold_convert (ptr_type_node, size_int (UNITS_PER_WORD));
6448 tmp = build (BIT_AND_EXPR, ptr_type_node, next_fp, tmp);
6449 tmp = build (PLUS_EXPR, ptr_type_node, next_fp, tmp);
6450 tmp = build (MODIFY_EXPR, ptr_type_node, next_fp, tmp);
6451 gimplify_and_add (tmp, pre_p);
6454 tmp = build1 (ADDR_EXPR, pptr_type_node, next_fp);
6455 tmp = build (MODIFY_EXPR, void_type_node, addr, tmp);
6456 gimplify_and_add (tmp, pre_p);
6458 #ifdef FUNCTION_ARG_SCmode_WART
6459 if (TYPE_MODE (type) == SCmode && TARGET_SH4 && TARGET_LITTLE_ENDIAN)
6461 tree subtype = TREE_TYPE (type);
6462 tree real, imag;
6464 imag = std_gimplify_va_arg_expr (valist, subtype, pre_p, NULL);
6465 imag = get_initialized_tmp_var (imag, pre_p, NULL);
6467 real = std_gimplify_va_arg_expr (valist, subtype, pre_p, NULL);
6468 real = get_initialized_tmp_var (real, pre_p, NULL);
6470 result = build (COMPLEX_EXPR, type, real, imag);
6471 result = get_initialized_tmp_var (result, pre_p, NULL);
6473 #endif /* FUNCTION_ARG_SCmode_WART */
6475 tmp = build (GOTO_EXPR, void_type_node, lab_over);
6476 gimplify_and_add (tmp, pre_p);
6478 tmp = build (LABEL_EXPR, void_type_node, lab_false);
6479 gimplify_and_add (tmp, pre_p);
6481 tmp = build1 (ADDR_EXPR, pptr_type_node, next_stack);
6482 tmp = build (MODIFY_EXPR, void_type_node, addr, tmp);
6483 gimplify_and_add (tmp, pre_p);
6485 else
6487 tmp = fold_convert (ptr_type_node, size_int (rsize));
6488 tmp = build (PLUS_EXPR, ptr_type_node, next_o, tmp);
6489 tmp = build (GT_EXPR, boolean_type_node, tmp, next_o_limit);
6490 tmp = build (COND_EXPR, void_type_node, tmp,
6491 build (GOTO_EXPR, void_type_node, lab_false),
6492 NULL);
6493 gimplify_and_add (tmp, pre_p);
6495 tmp = build1 (ADDR_EXPR, pptr_type_node, next_o);
6496 tmp = build (MODIFY_EXPR, void_type_node, addr, tmp);
6497 gimplify_and_add (tmp, pre_p);
6499 tmp = build (GOTO_EXPR, void_type_node, lab_over);
6500 gimplify_and_add (tmp, pre_p);
6502 tmp = build (LABEL_EXPR, void_type_node, lab_false);
6503 gimplify_and_add (tmp, pre_p);
6505 if (size > 4 && ! TARGET_SH4)
6507 tmp = build (MODIFY_EXPR, ptr_type_node, next_o, next_o_limit);
6508 gimplify_and_add (tmp, pre_p);
6511 tmp = build1 (ADDR_EXPR, pptr_type_node, next_stack);
6512 tmp = build (MODIFY_EXPR, void_type_node, addr, tmp);
6513 gimplify_and_add (tmp, pre_p);
6516 if (!result)
6518 tmp = build (LABEL_EXPR, void_type_node, lab_over);
6519 gimplify_and_add (tmp, pre_p);
6523 /* ??? In va-sh.h, there had been code to make values larger than
6524 size 8 indirect. This does not match the FUNCTION_ARG macros. */
6526 tmp = std_gimplify_va_arg_expr (valist, type, pre_p, NULL);
6527 if (result)
6529 tmp = build (MODIFY_EXPR, void_type_node, result, tmp);
6530 gimplify_and_add (tmp, pre_p);
6532 tmp = build (LABEL_EXPR, void_type_node, lab_over);
6533 gimplify_and_add (tmp, pre_p);
6535 else
6536 result = tmp;
6538 if (pass_by_ref)
6539 result = build_fold_indirect_ref (result);
6541 return result;
6544 bool
6545 sh_promote_prototypes (tree type)
6547 if (TARGET_HITACHI)
6548 return 0;
6549 if (! type)
6550 return 1;
6551 return ! sh_attr_renesas_p (type);
6554 /* Whether an argument must be passed by reference. On SHcompact, we
6555 pretend arguments wider than 32-bits that would have been passed in
6556 registers are passed by reference, so that an SHmedia trampoline
6557 loads them into the full 64-bits registers. */
6559 static int
6560 shcompact_byref (CUMULATIVE_ARGS *cum, enum machine_mode mode,
6561 tree type, bool named)
6563 unsigned HOST_WIDE_INT size;
6565 if (type)
6566 size = int_size_in_bytes (type);
6567 else
6568 size = GET_MODE_SIZE (mode);
6570 if (cum->arg_count[SH_ARG_INT] < NPARM_REGS (SImode)
6571 && (!named
6572 || GET_SH_ARG_CLASS (mode) == SH_ARG_INT
6573 || (GET_SH_ARG_CLASS (mode) == SH_ARG_FLOAT
6574 && cum->arg_count[SH_ARG_FLOAT] >= NPARM_REGS (SFmode)))
6575 && size > 4
6576 && !SHCOMPACT_FORCE_ON_STACK (mode, type)
6577 && !SH5_WOULD_BE_PARTIAL_NREGS (*cum, mode, type, named))
6578 return size;
6579 else
6580 return 0;
6583 static bool
6584 sh_pass_by_reference (CUMULATIVE_ARGS *cum, enum machine_mode mode,
6585 tree type, bool named)
6587 if (targetm.calls.must_pass_in_stack (mode, type))
6588 return true;
6590 if (TARGET_SHCOMPACT)
6592 cum->byref = shcompact_byref (cum, mode, type, named);
6593 return cum->byref != 0;
6596 return false;
6599 /* Define where to put the arguments to a function.
6600 Value is zero to push the argument on the stack,
6601 or a hard register in which to store the argument.
6603 MODE is the argument's machine mode.
6604 TYPE is the data type of the argument (as a tree).
6605 This is null for libcalls where that information may
6606 not be available.
6607 CUM is a variable of type CUMULATIVE_ARGS which gives info about
6608 the preceding args and about the function being called.
6609 NAMED is nonzero if this argument is a named parameter
6610 (otherwise it is an extra parameter matching an ellipsis).
6612 On SH the first args are normally in registers
6613 and the rest are pushed. Any arg that starts within the first
6614 NPARM_REGS words is at least partially passed in a register unless
6615 its data type forbids. */
6619 sh_function_arg (CUMULATIVE_ARGS *ca, enum machine_mode mode,
6620 tree type, int named)
6622 if (! TARGET_SH5 && mode == VOIDmode)
6623 return GEN_INT (ca->renesas_abi ? 1 : 0);
6625 if (! TARGET_SH5
6626 && PASS_IN_REG_P (*ca, mode, type)
6627 && (named || ! (TARGET_HITACHI || ca->renesas_abi)))
6629 int regno;
6631 if (mode == SCmode && TARGET_SH4 && TARGET_LITTLE_ENDIAN
6632 && (! FUNCTION_ARG_SCmode_WART || (ROUND_REG (*ca, mode) & 1)))
6634 rtx r1 = gen_rtx_EXPR_LIST (VOIDmode,
6635 gen_rtx_REG (SFmode,
6636 BASE_ARG_REG (mode)
6637 + (ROUND_REG (*ca, mode) ^ 1)),
6638 const0_rtx);
6639 rtx r2 = gen_rtx_EXPR_LIST (VOIDmode,
6640 gen_rtx_REG (SFmode,
6641 BASE_ARG_REG (mode)
6642 + ((ROUND_REG (*ca, mode) + 1) ^ 1)),
6643 GEN_INT (4));
6644 return gen_rtx_PARALLEL(SCmode, gen_rtvec(2, r1, r2));
6647 /* If the alignment of a DF value causes an SF register to be
6648 skipped, we will use that skipped register for the next SF
6649 value. */
6650 if ((TARGET_HITACHI || ca->renesas_abi)
6651 && ca->free_single_fp_reg
6652 && mode == SFmode)
6653 return gen_rtx_REG (mode, ca->free_single_fp_reg);
6655 regno = (BASE_ARG_REG (mode) + ROUND_REG (*ca, mode))
6656 ^ (mode == SFmode && TARGET_SH4
6657 && TARGET_LITTLE_ENDIAN != 0
6658 && ! TARGET_HITACHI && ! ca->renesas_abi);
6659 return gen_rtx_REG (mode, regno);
6663 if (TARGET_SH5)
6665 if (mode == VOIDmode && TARGET_SHCOMPACT)
6666 return GEN_INT (ca->call_cookie);
6668 /* The following test assumes unnamed arguments are promoted to
6669 DFmode. */
6670 if (mode == SFmode && ca->free_single_fp_reg)
6671 return SH5_PROTOTYPED_FLOAT_ARG (*ca, mode, ca->free_single_fp_reg);
6673 if ((GET_SH_ARG_CLASS (mode) == SH_ARG_FLOAT)
6674 && (named || ! ca->prototype_p)
6675 && ca->arg_count[(int) SH_ARG_FLOAT] < NPARM_REGS (SFmode))
6677 if (! ca->prototype_p && TARGET_SHMEDIA)
6678 return SH5_PROTOTYPELESS_FLOAT_ARG (*ca, mode);
6680 return SH5_PROTOTYPED_FLOAT_ARG (*ca, mode,
6681 FIRST_FP_PARM_REG
6682 + ca->arg_count[(int) SH_ARG_FLOAT]);
6685 if (ca->arg_count[(int) SH_ARG_INT] < NPARM_REGS (SImode)
6686 && (! TARGET_SHCOMPACT
6687 || (! SHCOMPACT_FORCE_ON_STACK (mode, type)
6688 && ! SH5_WOULD_BE_PARTIAL_NREGS (*ca, mode,
6689 type, named))))
6691 return gen_rtx_REG (mode, (FIRST_PARM_REG
6692 + ca->arg_count[(int) SH_ARG_INT]));
6695 return 0;
6698 return 0;
6701 /* Update the data in CUM to advance over an argument
6702 of mode MODE and data type TYPE.
6703 (TYPE is null for libcalls where that information may not be
6704 available.) */
6706 void
6707 sh_function_arg_advance (CUMULATIVE_ARGS *ca, enum machine_mode mode,
6708 tree type, int named)
6710 if (ca->force_mem)
6711 ca->force_mem = 0;
6712 else if (TARGET_SH5)
6714 tree type2 = (ca->byref && type
6715 ? TREE_TYPE (type)
6716 : type);
6717 enum machine_mode mode2 = (ca->byref && type
6718 ? TYPE_MODE (type2)
6719 : mode);
6720 int dwords = ((ca->byref
6721 ? ca->byref
6722 : mode2 == BLKmode
6723 ? int_size_in_bytes (type2)
6724 : GET_MODE_SIZE (mode2)) + 7) / 8;
6725 int numregs = MIN (dwords, NPARM_REGS (SImode)
6726 - ca->arg_count[(int) SH_ARG_INT]);
6728 if (numregs)
6730 ca->arg_count[(int) SH_ARG_INT] += numregs;
6731 if (TARGET_SHCOMPACT
6732 && SHCOMPACT_FORCE_ON_STACK (mode2, type2))
6734 ca->call_cookie
6735 |= CALL_COOKIE_INT_REG (ca->arg_count[(int) SH_ARG_INT]
6736 - numregs, 1);
6737 /* N.B. We want this also for outgoing. */
6738 ca->stack_regs += numregs;
6740 else if (ca->byref)
6742 if (! ca->outgoing)
6743 ca->stack_regs += numregs;
6744 ca->byref_regs += numregs;
6745 ca->byref = 0;
6747 ca->call_cookie
6748 |= CALL_COOKIE_INT_REG (ca->arg_count[(int) SH_ARG_INT]
6749 - numregs, 2);
6750 while (--numregs);
6751 ca->call_cookie
6752 |= CALL_COOKIE_INT_REG (ca->arg_count[(int) SH_ARG_INT]
6753 - 1, 1);
6755 else if (dwords > numregs)
6757 int pushregs = numregs;
6759 if (TARGET_SHCOMPACT)
6760 ca->stack_regs += numregs;
6761 while (pushregs < NPARM_REGS (SImode) - 1
6762 && (CALL_COOKIE_INT_REG_GET
6763 (ca->call_cookie,
6764 NPARM_REGS (SImode) - pushregs)
6765 == 1))
6767 ca->call_cookie
6768 &= ~ CALL_COOKIE_INT_REG (NPARM_REGS (SImode)
6769 - pushregs, 1);
6770 pushregs++;
6772 if (numregs == NPARM_REGS (SImode))
6773 ca->call_cookie
6774 |= CALL_COOKIE_INT_REG (0, 1)
6775 | CALL_COOKIE_STACKSEQ (numregs - 1);
6776 else
6777 ca->call_cookie
6778 |= CALL_COOKIE_STACKSEQ (numregs);
6781 if (GET_SH_ARG_CLASS (mode2) == SH_ARG_FLOAT
6782 && (named || ! ca->prototype_p))
6784 if (mode2 == SFmode && ca->free_single_fp_reg)
6785 ca->free_single_fp_reg = 0;
6786 else if (ca->arg_count[(int) SH_ARG_FLOAT]
6787 < NPARM_REGS (SFmode))
6789 int numfpregs
6790 = MIN ((GET_MODE_SIZE (mode2) + 7) / 8 * 2,
6791 NPARM_REGS (SFmode)
6792 - ca->arg_count[(int) SH_ARG_FLOAT]);
6794 ca->arg_count[(int) SH_ARG_FLOAT] += numfpregs;
6796 if (TARGET_SHCOMPACT && ! ca->prototype_p)
6798 if (ca->outgoing && numregs > 0)
6801 ca->call_cookie
6802 |= (CALL_COOKIE_INT_REG
6803 (ca->arg_count[(int) SH_ARG_INT]
6804 - numregs + ((numfpregs - 2) / 2),
6805 4 + (ca->arg_count[(int) SH_ARG_FLOAT]
6806 - numfpregs) / 2));
6808 while (numfpregs -= 2);
6810 else if (mode2 == SFmode && (named)
6811 && (ca->arg_count[(int) SH_ARG_FLOAT]
6812 < NPARM_REGS (SFmode)))
6813 ca->free_single_fp_reg
6814 = FIRST_FP_PARM_REG - numfpregs
6815 + ca->arg_count[(int) SH_ARG_FLOAT] + 1;
6818 return;
6821 if ((TARGET_HITACHI || ca->renesas_abi) && TARGET_FPU_DOUBLE)
6823 /* Note that we've used the skipped register. */
6824 if (mode == SFmode && ca->free_single_fp_reg)
6826 ca->free_single_fp_reg = 0;
6827 return;
6829 /* When we have a DF after an SF, there's an SF register that get
6830 skipped in order to align the DF value. We note this skipped
6831 register, because the next SF value will use it, and not the
6832 SF that follows the DF. */
6833 if (mode == DFmode
6834 && ROUND_REG (*ca, DFmode) != ROUND_REG (*ca, SFmode))
6836 ca->free_single_fp_reg = (ROUND_REG (*ca, SFmode)
6837 + BASE_ARG_REG (mode));
6841 if (! (TARGET_SH4 || ca->renesas_abi)
6842 || PASS_IN_REG_P (*ca, mode, type))
6843 (ca->arg_count[(int) GET_SH_ARG_CLASS (mode)]
6844 = (ROUND_REG (*ca, mode)
6845 + (mode == BLKmode
6846 ? ROUND_ADVANCE (int_size_in_bytes (type))
6847 : ROUND_ADVANCE (GET_MODE_SIZE (mode)))));
6850 /* The Renesas calling convention doesn't quite fit into this scheme since
6851 the address is passed like an invisible argument, but one that is always
6852 passed in memory. */
6853 static rtx
6854 sh_struct_value_rtx (tree fndecl, int incoming ATTRIBUTE_UNUSED)
6856 if (TARGET_HITACHI || sh_attr_renesas_p (fndecl))
6857 return 0;
6858 return gen_rtx_REG (Pmode, 2);
6861 /* Worker function for TARGET_RETURN_IN_MEMORY. */
6863 static bool
6864 sh_return_in_memory (tree type, tree fndecl)
6866 if (TARGET_SH5)
6868 if (TYPE_MODE (type) == BLKmode)
6869 return ((unsigned HOST_WIDE_INT) int_size_in_bytes (type)) > 8;
6870 else
6871 return GET_MODE_SIZE (TYPE_MODE (type)) > 8;
6873 else
6875 return (TYPE_MODE (type) == BLKmode
6876 || ((TARGET_HITACHI || sh_attr_renesas_p (fndecl))
6877 && TREE_CODE (type) == RECORD_TYPE));
6881 /* We actually emit the code in sh_expand_prologue. We used to use
6882 a static variable to flag that we need to emit this code, but that
6883 doesn't when inlining, when functions are deferred and then emitted
6884 later. Fortunately, we already have two flags that are part of struct
6885 function that tell if a function uses varargs or stdarg. */
6886 static void
6887 sh_setup_incoming_varargs (CUMULATIVE_ARGS *ca,
6888 enum machine_mode mode,
6889 tree type,
6890 int *pretend_arg_size,
6891 int second_time ATTRIBUTE_UNUSED)
6893 if (! current_function_stdarg)
6894 abort ();
6895 if (TARGET_VARARGS_PRETEND_ARGS (current_function_decl))
6897 int named_parm_regs, anon_parm_regs;
6899 named_parm_regs = (ROUND_REG (*ca, mode)
6900 + (mode == BLKmode
6901 ? ROUND_ADVANCE (int_size_in_bytes (type))
6902 : ROUND_ADVANCE (GET_MODE_SIZE (mode))));
6903 anon_parm_regs = NPARM_REGS (SImode) - named_parm_regs;
6904 if (anon_parm_regs > 0)
6905 *pretend_arg_size = anon_parm_regs * 4;
6909 static bool
6910 sh_strict_argument_naming (CUMULATIVE_ARGS *ca ATTRIBUTE_UNUSED)
6912 return TARGET_SH5;
6915 static bool
6916 sh_pretend_outgoing_varargs_named (CUMULATIVE_ARGS *ca)
6918 return ! (TARGET_HITACHI || ca->renesas_abi) && ! TARGET_SH5;
6922 /* Define the offset between two registers, one to be eliminated, and
6923 the other its replacement, at the start of a routine. */
6926 initial_elimination_offset (int from, int to)
6928 int regs_saved;
6929 int regs_saved_rounding = 0;
6930 int total_saved_regs_space;
6931 int total_auto_space;
6932 int save_flags = target_flags;
6933 int copy_flags;
6934 HARD_REG_SET live_regs_mask;
6936 shmedia_space_reserved_for_target_registers = false;
6937 regs_saved = calc_live_regs (&live_regs_mask);
6938 regs_saved += SHMEDIA_REGS_STACK_ADJUST ();
6940 if (shmedia_reserve_space_for_target_registers_p (regs_saved, &live_regs_mask))
6942 shmedia_space_reserved_for_target_registers = true;
6943 regs_saved += shmedia_target_regs_stack_adjust (&live_regs_mask);
6946 if (TARGET_SH5 && regs_saved % (STACK_BOUNDARY / BITS_PER_UNIT))
6947 regs_saved_rounding = ((STACK_BOUNDARY / BITS_PER_UNIT)
6948 - regs_saved % (STACK_BOUNDARY / BITS_PER_UNIT));
6950 total_auto_space = rounded_frame_size (regs_saved) - regs_saved_rounding;
6951 copy_flags = target_flags;
6952 target_flags = save_flags;
6954 total_saved_regs_space = regs_saved + regs_saved_rounding;
6956 if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
6957 return total_saved_regs_space + total_auto_space
6958 + current_function_args_info.byref_regs * 8;
6960 if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
6961 return total_saved_regs_space + total_auto_space
6962 + current_function_args_info.byref_regs * 8;
6964 /* Initial gap between fp and sp is 0. */
6965 if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
6966 return 0;
6968 if (from == RETURN_ADDRESS_POINTER_REGNUM
6969 && (to == FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM))
6971 if (TARGET_SH5)
6973 int n = total_saved_regs_space;
6974 int pr_reg = TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG;
6975 save_schedule schedule;
6976 save_entry *entry;
6978 n += total_auto_space;
6980 /* If it wasn't saved, there's not much we can do. */
6981 if (! TEST_HARD_REG_BIT (live_regs_mask, pr_reg))
6982 return n;
6984 target_flags = copy_flags;
6986 sh5_schedule_saves (&live_regs_mask, &schedule, n);
6987 for (entry = &schedule.entries[1]; entry->mode != VOIDmode; entry++)
6988 if (entry->reg == pr_reg)
6990 target_flags = save_flags;
6991 return entry->offset;
6993 abort ();
6995 else
6996 return total_auto_space;
6999 abort ();
7002 /* Handle machine specific pragmas to be semi-compatible with Renesas
7003 compiler. */
7005 void
7006 sh_pr_interrupt (struct cpp_reader *pfile ATTRIBUTE_UNUSED)
7008 pragma_interrupt = 1;
7011 void
7012 sh_pr_trapa (struct cpp_reader *pfile ATTRIBUTE_UNUSED)
7014 pragma_interrupt = pragma_trapa = 1;
7017 void
7018 sh_pr_nosave_low_regs (struct cpp_reader *pfile ATTRIBUTE_UNUSED)
7020 pragma_nosave_low_regs = 1;
7023 /* Generate 'handle_interrupt' attribute for decls */
7025 static void
7026 sh_insert_attributes (tree node, tree *attributes)
7028 if (! pragma_interrupt
7029 || TREE_CODE (node) != FUNCTION_DECL)
7030 return;
7032 /* We are only interested in fields. */
7033 if (TREE_CODE_CLASS (TREE_CODE (node)) != 'd')
7034 return;
7036 /* Add a 'handle_interrupt' attribute. */
7037 * attributes = tree_cons (get_identifier ("interrupt_handler"), NULL, * attributes);
7039 return;
7042 /* Supported attributes:
7044 interrupt_handler -- specifies this function is an interrupt handler.
7046 sp_switch -- specifies an alternate stack for an interrupt handler
7047 to run on.
7049 trap_exit -- use a trapa to exit an interrupt function instead of
7050 an rte instruction.
7052 renesas -- use Renesas calling/layout conventions (functions and
7053 structures).
7057 const struct attribute_spec sh_attribute_table[] =
7059 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
7060 { "interrupt_handler", 0, 0, true, false, false, sh_handle_interrupt_handler_attribute },
7061 { "sp_switch", 1, 1, true, false, false, sh_handle_sp_switch_attribute },
7062 { "trap_exit", 1, 1, true, false, false, sh_handle_trap_exit_attribute },
7063 { "renesas", 0, 0, false, true, false, sh_handle_renesas_attribute },
7064 #ifdef SYMBIAN
7065 /* Symbian support adds three new attributes:
7066 dllexport - for exporting a function/variable that will live in a dll
7067 dllimport - for importing a function/variable from a dll
7069 Microsoft allows multiple declspecs in one __declspec, separating
7070 them with spaces. We do NOT support this. Instead, use __declspec
7071 multiple times. */
7072 { "dllimport", 0, 0, true, false, false, sh_symbian_handle_dll_attribute },
7073 { "dllexport", 0, 0, true, false, false, sh_symbian_handle_dll_attribute },
7074 #endif
7075 { NULL, 0, 0, false, false, false, NULL }
7078 /* Handle an "interrupt_handler" attribute; arguments as in
7079 struct attribute_spec.handler. */
7080 static tree
7081 sh_handle_interrupt_handler_attribute (tree *node, tree name,
7082 tree args ATTRIBUTE_UNUSED,
7083 int flags ATTRIBUTE_UNUSED,
7084 bool *no_add_attrs)
7086 if (TREE_CODE (*node) != FUNCTION_DECL)
7088 warning ("`%s' attribute only applies to functions",
7089 IDENTIFIER_POINTER (name));
7090 *no_add_attrs = true;
7092 else if (TARGET_SHCOMPACT)
7094 error ("attribute interrupt_handler is not compatible with -m5-compact");
7095 *no_add_attrs = true;
7098 return NULL_TREE;
7101 /* Handle an "sp_switch" attribute; arguments as in
7102 struct attribute_spec.handler. */
7103 static tree
7104 sh_handle_sp_switch_attribute (tree *node, tree name, tree args,
7105 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
7107 if (TREE_CODE (*node) != FUNCTION_DECL)
7109 warning ("`%s' attribute only applies to functions",
7110 IDENTIFIER_POINTER (name));
7111 *no_add_attrs = true;
7113 else if (!pragma_interrupt)
7115 /* The sp_switch attribute only has meaning for interrupt functions. */
7116 warning ("`%s' attribute only applies to interrupt functions",
7117 IDENTIFIER_POINTER (name));
7118 *no_add_attrs = true;
7120 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
7122 /* The argument must be a constant string. */
7123 warning ("`%s' attribute argument not a string constant",
7124 IDENTIFIER_POINTER (name));
7125 *no_add_attrs = true;
7127 else
7129 sp_switch = gen_rtx_SYMBOL_REF (VOIDmode,
7130 TREE_STRING_POINTER (TREE_VALUE (args)));
7133 return NULL_TREE;
7136 /* Handle an "trap_exit" attribute; arguments as in
7137 struct attribute_spec.handler. */
7138 static tree
7139 sh_handle_trap_exit_attribute (tree *node, tree name, tree args,
7140 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
7142 if (TREE_CODE (*node) != FUNCTION_DECL)
7144 warning ("`%s' attribute only applies to functions",
7145 IDENTIFIER_POINTER (name));
7146 *no_add_attrs = true;
7148 else if (!pragma_interrupt)
7150 /* The trap_exit attribute only has meaning for interrupt functions. */
7151 warning ("`%s' attribute only applies to interrupt functions",
7152 IDENTIFIER_POINTER (name));
7153 *no_add_attrs = true;
7155 else if (TREE_CODE (TREE_VALUE (args)) != INTEGER_CST)
7157 /* The argument must be a constant integer. */
7158 warning ("`%s' attribute argument not an integer constant",
7159 IDENTIFIER_POINTER (name));
7160 *no_add_attrs = true;
7162 else
7164 trap_exit = TREE_INT_CST_LOW (TREE_VALUE (args));
7167 return NULL_TREE;
7170 static tree
7171 sh_handle_renesas_attribute (tree *node ATTRIBUTE_UNUSED,
7172 tree name ATTRIBUTE_UNUSED,
7173 tree args ATTRIBUTE_UNUSED,
7174 int flags ATTRIBUTE_UNUSED,
7175 bool *no_add_attrs ATTRIBUTE_UNUSED)
7177 return NULL_TREE;
7180 /* True if __attribute__((renesas)) or -mrenesas. */
7182 sh_attr_renesas_p (tree td)
7184 if (TARGET_HITACHI)
7185 return 1;
7186 if (td == 0)
7187 return 0;
7188 if (DECL_P (td))
7189 td = TREE_TYPE (td);
7190 return (lookup_attribute ("renesas", TYPE_ATTRIBUTES (td))
7191 != NULL_TREE);
7194 /* True if __attribute__((renesas)) or -mrenesas, for the current
7195 function. */
7197 sh_cfun_attr_renesas_p (void)
7199 return sh_attr_renesas_p (current_function_decl);
7203 sh_cfun_interrupt_handler_p (void)
7205 return (lookup_attribute ("interrupt_handler",
7206 DECL_ATTRIBUTES (current_function_decl))
7207 != NULL_TREE);
7210 /* ??? target_switches in toplev.c is static, hence we have to duplicate it. */
7211 static const struct
7213 const char *const name;
7214 const int value;
7215 const char *const description;
7217 sh_target_switches[] = TARGET_SWITCHES;
7218 #define target_switches sh_target_switches
7220 /* Like default_pch_valid_p, but take flag_mask into account. */
7221 const char *
7222 sh_pch_valid_p (const void *data_p, size_t len)
7224 const char *data = (const char *)data_p;
7225 const char *flag_that_differs = NULL;
7226 size_t i;
7227 int old_flags;
7228 int flag_mask
7229 = (SH1_BIT | SH2_BIT | SH3_BIT | SH_E_BIT | HARD_SH4_BIT | FPU_SINGLE_BIT
7230 | SH4_BIT | HITACHI_BIT | LITTLE_ENDIAN_BIT);
7232 /* -fpic and -fpie also usually make a PCH invalid. */
7233 if (data[0] != flag_pic)
7234 return _("created and used with different settings of -fpic");
7235 if (data[1] != flag_pie)
7236 return _("created and used with different settings of -fpie");
7237 data += 2;
7239 /* Check target_flags. */
7240 memcpy (&old_flags, data, sizeof (target_flags));
7241 if (((old_flags ^ target_flags) & flag_mask) != 0)
7243 for (i = 0; i < ARRAY_SIZE (target_switches); i++)
7245 int bits;
7247 bits = target_switches[i].value;
7248 if (bits < 0)
7249 bits = -bits;
7250 bits &= flag_mask;
7251 if ((target_flags & bits) != (old_flags & bits))
7253 flag_that_differs = target_switches[i].name;
7254 goto make_message;
7257 abort ();
7259 data += sizeof (target_flags);
7260 len -= sizeof (target_flags);
7262 /* Check string options. */
7263 #ifdef TARGET_OPTIONS
7264 for (i = 0; i < ARRAY_SIZE (target_options); i++)
7266 const char *str = *target_options[i].variable;
7267 size_t l;
7268 if (! str)
7269 str = "";
7270 l = strlen (str) + 1;
7271 if (len < l || memcmp (data, str, l) != 0)
7273 flag_that_differs = target_options[i].prefix;
7274 goto make_message;
7276 data += l;
7277 len -= l;
7279 #endif
7281 return NULL;
7283 make_message:
7285 char *r;
7286 asprintf (&r, _("created and used with differing settings of `-m%s'"),
7287 flag_that_differs);
7288 if (r == NULL)
7289 return _("out of memory");
7290 return r;
7294 /* Predicates used by the templates. */
7296 /* Returns 1 if OP is MACL, MACH or PR. The input must be a REG rtx.
7297 Used only in general_movsrc_operand. */
7300 system_reg_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7302 switch (REGNO (op))
7304 case PR_REG:
7305 case MACL_REG:
7306 case MACH_REG:
7307 return 1;
7309 return 0;
7312 /* Returns 1 if OP can be source of a simple move operation.
7313 Same as general_operand, but a LABEL_REF is valid, PRE_DEC is
7314 invalid as are subregs of system registers. */
7317 general_movsrc_operand (rtx op, enum machine_mode mode)
7319 if (GET_CODE (op) == MEM)
7321 rtx inside = XEXP (op, 0);
7322 if (GET_CODE (inside) == CONST)
7323 inside = XEXP (inside, 0);
7325 if (GET_CODE (inside) == LABEL_REF)
7326 return 1;
7328 if (GET_CODE (inside) == PLUS
7329 && GET_CODE (XEXP (inside, 0)) == LABEL_REF
7330 && GET_CODE (XEXP (inside, 1)) == CONST_INT)
7331 return 1;
7333 /* Only post inc allowed. */
7334 if (GET_CODE (inside) == PRE_DEC)
7335 return 0;
7338 if ((mode == QImode || mode == HImode)
7339 && (GET_CODE (op) == SUBREG
7340 && GET_CODE (XEXP (op, 0)) == REG
7341 && system_reg_operand (XEXP (op, 0), mode)))
7342 return 0;
7344 return general_operand (op, mode);
7347 /* Returns 1 if OP can be a destination of a move.
7348 Same as general_operand, but no preinc allowed. */
7351 general_movdst_operand (rtx op, enum machine_mode mode)
7353 /* Only pre dec allowed. */
7354 if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == POST_INC)
7355 return 0;
7357 return general_operand (op, mode);
7360 /* Returns 1 if OP is a normal arithmetic register. */
7363 arith_reg_operand (rtx op, enum machine_mode mode)
7365 if (register_operand (op, mode))
7367 int regno;
7369 if (GET_CODE (op) == REG)
7370 regno = REGNO (op);
7371 else if (GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == REG)
7372 regno = REGNO (SUBREG_REG (op));
7373 else
7374 return 1;
7376 return (regno != T_REG && regno != PR_REG
7377 && ! TARGET_REGISTER_P (regno)
7378 && (regno != FPUL_REG || TARGET_SH4)
7379 && regno != MACH_REG && regno != MACL_REG);
7381 return 0;
7384 /* Like above, but for DImode destinations: forbid paradoxical DImode subregs,
7385 because this would lead to missing sign extensions when truncating from
7386 DImode to SImode. */
7388 arith_reg_dest (rtx op, enum machine_mode mode)
7390 if (mode == DImode && GET_CODE (op) == SUBREG
7391 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (op))) < 8)
7392 return 0;
7393 return arith_reg_operand (op, mode);
7397 int_gpr_dest (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7399 enum machine_mode op_mode = GET_MODE (op);
7401 if (GET_MODE_CLASS (op_mode) != MODE_INT
7402 || GET_MODE_SIZE (op_mode) >= UNITS_PER_WORD)
7403 return 0;
7404 if (! reload_completed)
7405 return 0;
7406 return true_regnum (op) <= LAST_GENERAL_REG;
7410 fp_arith_reg_operand (rtx op, enum machine_mode mode)
7412 if (register_operand (op, mode))
7414 int regno;
7416 if (GET_CODE (op) == REG)
7417 regno = REGNO (op);
7418 else if (GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == REG)
7419 regno = REGNO (SUBREG_REG (op));
7420 else
7421 return 1;
7423 return (regno >= FIRST_PSEUDO_REGISTER
7424 || FP_REGISTER_P (regno));
7426 return 0;
7429 /* Returns 1 if OP is a valid source operand for an arithmetic insn. */
7432 arith_operand (rtx op, enum machine_mode mode)
7434 if (arith_reg_operand (op, mode))
7435 return 1;
7437 if (TARGET_SHMEDIA)
7439 /* FIXME: We should be checking whether the CONST_INT fits in a
7440 CONST_OK_FOR_I16 here, but this causes reload_cse to crash when
7441 attempting to transform a sequence of two 64-bit sets of the
7442 same register from literal constants into a set and an add,
7443 when the difference is too wide for an add. */
7444 if (GET_CODE (op) == CONST_INT
7445 || EXTRA_CONSTRAINT_C16 (op))
7446 return 1;
7447 else
7448 return 0;
7450 else if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_I08 (INTVAL (op)))
7451 return 1;
7453 return 0;
7456 /* Returns 1 if OP is a valid source operand for a compare insn. */
7459 arith_reg_or_0_operand (rtx op, enum machine_mode mode)
7461 if (arith_reg_operand (op, mode))
7462 return 1;
7464 if (EXTRA_CONSTRAINT_Z (op))
7465 return 1;
7467 return 0;
7470 /* Return 1 if OP is a valid source operand for an SHmedia operation
7471 that takes either a register or a 6-bit immediate. */
7474 shmedia_6bit_operand (rtx op, enum machine_mode mode)
7476 return (arith_reg_operand (op, mode)
7477 || (GET_CODE (op) == CONST_INT && CONST_OK_FOR_I06 (INTVAL (op))));
7480 /* Returns 1 if OP is a valid source operand for a logical operation. */
7483 logical_operand (rtx op, enum machine_mode mode)
7485 if (arith_reg_operand (op, mode))
7486 return 1;
7488 if (TARGET_SHMEDIA)
7490 if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_I10 (INTVAL (op)))
7491 return 1;
7492 else
7493 return 0;
7495 else if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_K08 (INTVAL (op)))
7496 return 1;
7498 return 0;
7502 and_operand (rtx op, enum machine_mode mode)
7504 if (logical_operand (op, mode))
7505 return 1;
7507 /* Check mshflo.l / mshflhi.l opportunities. */
7508 if (TARGET_SHMEDIA
7509 && mode == DImode
7510 && GET_CODE (op) == CONST_INT
7511 && CONST_OK_FOR_J16 (INTVAL (op)))
7512 return 1;
7514 return 0;
7517 /* Nonzero if OP is a floating point value with value 0.0. */
7520 fp_zero_operand (rtx op)
7522 REAL_VALUE_TYPE r;
7524 if (GET_MODE (op) != SFmode)
7525 return 0;
7527 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
7528 return REAL_VALUES_EQUAL (r, dconst0) && ! REAL_VALUE_MINUS_ZERO (r);
7531 /* Nonzero if OP is a floating point value with value 1.0. */
7534 fp_one_operand (rtx op)
7536 REAL_VALUE_TYPE r;
7538 if (GET_MODE (op) != SFmode)
7539 return 0;
7541 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
7542 return REAL_VALUES_EQUAL (r, dconst1);
7545 /* For -m4 and -m4-single-only, mode switching is used. If we are
7546 compiling without -mfmovd, movsf_ie isn't taken into account for
7547 mode switching. We could check in machine_dependent_reorg for
7548 cases where we know we are in single precision mode, but there is
7549 interface to find that out during reload, so we must avoid
7550 choosing an fldi alternative during reload and thus failing to
7551 allocate a scratch register for the constant loading. */
7553 fldi_ok (void)
7555 return ! TARGET_SH4 || TARGET_FMOVD || reload_completed;
7559 tertiary_reload_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7561 enum rtx_code code = GET_CODE (op);
7562 return code == MEM || (TARGET_SH4 && code == CONST_DOUBLE);
7566 fpscr_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7568 return (GET_CODE (op) == REG && REGNO (op) == FPSCR_REG
7569 && GET_MODE (op) == PSImode);
7573 fpul_operand (rtx op, enum machine_mode mode)
7575 if (TARGET_SHMEDIA)
7576 return fp_arith_reg_operand (op, mode);
7578 return (GET_CODE (op) == REG
7579 && (REGNO (op) == FPUL_REG || REGNO (op) >= FIRST_PSEUDO_REGISTER)
7580 && GET_MODE (op) == mode);
7584 symbol_ref_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7586 return (GET_CODE (op) == SYMBOL_REF);
7589 /* Return the TLS type for TLS symbols, 0 for otherwise. */
7591 tls_symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7593 if (GET_CODE (op) != SYMBOL_REF)
7594 return 0;
7595 return SYMBOL_REF_TLS_MODEL (op);
7599 commutative_float_operator (rtx op, enum machine_mode mode)
7601 if (GET_MODE (op) != mode)
7602 return 0;
7603 switch (GET_CODE (op))
7605 case PLUS:
7606 case MULT:
7607 return 1;
7608 default:
7609 break;
7611 return 0;
7615 noncommutative_float_operator (rtx op, enum machine_mode mode)
7617 if (GET_MODE (op) != mode)
7618 return 0;
7619 switch (GET_CODE (op))
7621 case MINUS:
7622 case DIV:
7623 return 1;
7624 default:
7625 break;
7627 return 0;
7631 unary_float_operator (rtx op, enum machine_mode mode)
7633 if (GET_MODE (op) != mode)
7634 return 0;
7635 switch (GET_CODE (op))
7637 case ABS:
7638 case NEG:
7639 case SQRT:
7640 return 1;
7641 default:
7642 break;
7644 return 0;
7648 binary_float_operator (rtx op, enum machine_mode mode)
7650 if (GET_MODE (op) != mode)
7651 return 0;
7652 switch (GET_CODE (op))
7654 case PLUS:
7655 case MINUS:
7656 case MULT:
7657 case DIV:
7658 return 1;
7659 default:
7660 break;
7662 return 0;
7666 binary_logical_operator (rtx op, enum machine_mode mode)
7668 if (GET_MODE (op) != mode)
7669 return 0;
7670 switch (GET_CODE (op))
7672 case IOR:
7673 case AND:
7674 case XOR:
7675 return 1;
7676 default:
7677 break;
7679 return 0;
7683 equality_comparison_operator (rtx op, enum machine_mode mode)
7685 return ((mode == VOIDmode || GET_MODE (op) == mode)
7686 && (GET_CODE (op) == EQ || GET_CODE (op) == NE));
7690 greater_comparison_operator (rtx op, enum machine_mode mode)
7692 if (mode != VOIDmode && GET_MODE (op) == mode)
7693 return 0;
7694 switch (GET_CODE (op))
7696 case GT:
7697 case GE:
7698 case GTU:
7699 case GEU:
7700 return 1;
7701 default:
7702 return 0;
7707 less_comparison_operator (rtx op, enum machine_mode mode)
7709 if (mode != VOIDmode && GET_MODE (op) == mode)
7710 return 0;
7711 switch (GET_CODE (op))
7713 case LT:
7714 case LE:
7715 case LTU:
7716 case LEU:
7717 return 1;
7718 default:
7719 return 0;
7723 /* Accept pseudos and branch target registers. */
7725 target_reg_operand (rtx op, enum machine_mode mode)
7727 if (mode != DImode
7728 || GET_MODE (op) != DImode)
7729 return 0;
7731 if (GET_CODE (op) == SUBREG)
7732 op = XEXP (op, 0);
7734 if (GET_CODE (op) != REG)
7735 return 0;
7737 /* We must protect ourselves from matching pseudos that are virtual
7738 register, because they will eventually be replaced with hardware
7739 registers that aren't branch-target registers. */
7740 if (REGNO (op) > LAST_VIRTUAL_REGISTER
7741 || TARGET_REGISTER_P (REGNO (op)))
7742 return 1;
7744 return 0;
7747 /* Same as target_reg_operand, except that label_refs and symbol_refs
7748 are accepted before reload. */
7750 target_operand (rtx op, enum machine_mode mode)
7752 if (mode != DImode)
7753 return 0;
7755 if ((GET_MODE (op) == DImode || GET_MODE (op) == VOIDmode)
7756 && EXTRA_CONSTRAINT_Csy (op))
7757 return ! reload_completed;
7759 return target_reg_operand (op, mode);
7763 mextr_bit_offset (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7765 HOST_WIDE_INT i;
7767 if (GET_CODE (op) != CONST_INT)
7768 return 0;
7769 i = INTVAL (op);
7770 return i >= 1 * 8 && i <= 7 * 8 && (i & 7) == 0;
7774 extend_reg_operand (rtx op, enum machine_mode mode)
7776 return (GET_CODE (op) == TRUNCATE
7777 ? arith_operand
7778 : arith_reg_operand) (op, mode);
7782 trunc_hi_operand (rtx op, enum machine_mode mode)
7784 enum machine_mode op_mode = GET_MODE (op);
7786 if (op_mode != SImode && op_mode != DImode
7787 && op_mode != V4HImode && op_mode != V2SImode)
7788 return 0;
7789 return extend_reg_operand (op, mode);
7793 extend_reg_or_0_operand (rtx op, enum machine_mode mode)
7795 return (GET_CODE (op) == TRUNCATE
7796 ? arith_operand
7797 : arith_reg_or_0_operand) (op, mode);
7801 general_extend_operand (rtx op, enum machine_mode mode)
7803 return (GET_CODE (op) == TRUNCATE
7804 ? arith_operand
7805 : nonimmediate_operand) (op, mode);
7809 inqhi_operand (rtx op, enum machine_mode mode)
7811 if (GET_CODE (op) != TRUNCATE || mode != GET_MODE (op))
7812 return 0;
7813 op = XEXP (op, 0);
7814 /* Can't use true_regnum here because copy_cost wants to know about
7815 SECONDARY_INPUT_RELOAD_CLASS. */
7816 return GET_CODE (op) == REG && FP_REGISTER_P (REGNO (op));
7820 sh_rep_vec (rtx v, enum machine_mode mode)
7822 int i;
7823 rtx x, y;
7825 if ((GET_CODE (v) != CONST_VECTOR && GET_CODE (v) != PARALLEL)
7826 || (GET_MODE (v) != mode && mode != VOIDmode))
7827 return 0;
7828 i = XVECLEN (v, 0) - 2;
7829 x = XVECEXP (v, 0, i + 1);
7830 if (GET_MODE_UNIT_SIZE (mode) == 1)
7832 y = XVECEXP (v, 0, i);
7833 for (i -= 2; i >= 0; i -= 2)
7834 if (! rtx_equal_p (XVECEXP (v, 0, i + 1), x)
7835 || ! rtx_equal_p (XVECEXP (v, 0, i), y))
7836 return 0;
7838 else
7839 for (; i >= 0; i--)
7840 if (XVECEXP (v, 0, i) != x)
7841 return 0;
7842 return 1;
7845 /* Determine if V is a constant vector matching MODE with only one element
7846 that is not a sign extension. Two byte-sized elements count as one. */
7848 sh_1el_vec (rtx v, enum machine_mode mode)
7850 int unit_size;
7851 int i, last, least, sign_ix;
7852 rtx sign;
7854 if (GET_CODE (v) != CONST_VECTOR
7855 || (GET_MODE (v) != mode && mode != VOIDmode))
7856 return 0;
7857 /* Determine numbers of last and of least significant elements. */
7858 last = XVECLEN (v, 0) - 1;
7859 least = TARGET_LITTLE_ENDIAN ? 0 : last;
7860 if (GET_CODE (XVECEXP (v, 0, least)) != CONST_INT)
7861 return 0;
7862 sign_ix = least;
7863 if (GET_MODE_UNIT_SIZE (mode) == 1)
7864 sign_ix = TARGET_LITTLE_ENDIAN ? 1 : last - 1;
7865 if (GET_CODE (XVECEXP (v, 0, sign_ix)) != CONST_INT)
7866 return 0;
7867 unit_size = GET_MODE_UNIT_SIZE (GET_MODE (v));
7868 sign = (INTVAL (XVECEXP (v, 0, sign_ix)) >> (unit_size * BITS_PER_UNIT - 1)
7869 ? constm1_rtx : const0_rtx);
7870 i = XVECLEN (v, 0) - 1;
7872 if (i != least && i != sign_ix && XVECEXP (v, 0, i) != sign)
7873 return 0;
7874 while (--i);
7875 return 1;
7879 sh_const_vec (rtx v, enum machine_mode mode)
7881 int i;
7883 if (GET_CODE (v) != CONST_VECTOR
7884 || (GET_MODE (v) != mode && mode != VOIDmode))
7885 return 0;
7886 i = XVECLEN (v, 0) - 1;
7887 for (; i >= 0; i--)
7888 if (GET_CODE (XVECEXP (v, 0, i)) != CONST_INT)
7889 return 0;
7890 return 1;
7893 /* Return the destination address of a branch. */
7895 static int
7896 branch_dest (rtx branch)
7898 rtx dest = SET_SRC (PATTERN (branch));
7899 int dest_uid;
7901 if (GET_CODE (dest) == IF_THEN_ELSE)
7902 dest = XEXP (dest, 1);
7903 dest = XEXP (dest, 0);
7904 dest_uid = INSN_UID (dest);
7905 return INSN_ADDRESSES (dest_uid);
7908 /* Return nonzero if REG is not used after INSN.
7909 We assume REG is a reload reg, and therefore does
7910 not live past labels. It may live past calls or jumps though. */
7912 reg_unused_after (rtx reg, rtx insn)
7914 enum rtx_code code;
7915 rtx set;
7917 /* If the reg is set by this instruction, then it is safe for our
7918 case. Disregard the case where this is a store to memory, since
7919 we are checking a register used in the store address. */
7920 set = single_set (insn);
7921 if (set && GET_CODE (SET_DEST (set)) != MEM
7922 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
7923 return 1;
7925 while ((insn = NEXT_INSN (insn)))
7927 rtx set;
7928 if (!INSN_P (insn))
7929 continue;
7931 code = GET_CODE (insn);
7933 #if 0
7934 /* If this is a label that existed before reload, then the register
7935 if dead here. However, if this is a label added by reorg, then
7936 the register may still be live here. We can't tell the difference,
7937 so we just ignore labels completely. */
7938 if (code == CODE_LABEL)
7939 return 1;
7940 /* else */
7941 #endif
7943 if (code == JUMP_INSN)
7944 return 0;
7946 /* If this is a sequence, we must handle them all at once.
7947 We could have for instance a call that sets the target register,
7948 and an insn in a delay slot that uses the register. In this case,
7949 we must return 0. */
7950 else if (code == INSN && GET_CODE (PATTERN (insn)) == SEQUENCE)
7952 int i;
7953 int retval = 0;
7955 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
7957 rtx this_insn = XVECEXP (PATTERN (insn), 0, i);
7958 rtx set = single_set (this_insn);
7960 if (GET_CODE (this_insn) == CALL_INSN)
7961 code = CALL_INSN;
7962 else if (GET_CODE (this_insn) == JUMP_INSN)
7964 if (INSN_ANNULLED_BRANCH_P (this_insn))
7965 return 0;
7966 code = JUMP_INSN;
7969 if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
7970 return 0;
7971 if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
7973 if (GET_CODE (SET_DEST (set)) != MEM)
7974 retval = 1;
7975 else
7976 return 0;
7978 if (set == 0
7979 && reg_overlap_mentioned_p (reg, PATTERN (this_insn)))
7980 return 0;
7982 if (retval == 1)
7983 return 1;
7984 else if (code == JUMP_INSN)
7985 return 0;
7988 set = single_set (insn);
7989 if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
7990 return 0;
7991 if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
7992 return GET_CODE (SET_DEST (set)) != MEM;
7993 if (set == 0 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
7994 return 0;
7996 if (code == CALL_INSN && call_used_regs[REGNO (reg)])
7997 return 1;
7999 return 1;
8002 #include "ggc.h"
8004 static GTY(()) rtx fpscr_rtx;
8006 get_fpscr_rtx (void)
8008 if (! fpscr_rtx)
8010 fpscr_rtx = gen_rtx_REG (PSImode, FPSCR_REG);
8011 REG_USERVAR_P (fpscr_rtx) = 1;
8012 mark_user_reg (fpscr_rtx);
8014 if (! reload_completed || mdep_reorg_phase != SH_AFTER_MDEP_REORG)
8015 mark_user_reg (fpscr_rtx);
8016 return fpscr_rtx;
8019 void
8020 emit_sf_insn (rtx pat)
8022 emit_insn (pat);
8025 void
8026 emit_df_insn (rtx pat)
8028 emit_insn (pat);
8031 void
8032 expand_sf_unop (rtx (*fun) (rtx, rtx, rtx), rtx *operands)
8034 emit_sf_insn ((*fun) (operands[0], operands[1], get_fpscr_rtx ()));
8037 void
8038 expand_sf_binop (rtx (*fun) (rtx, rtx, rtx, rtx), rtx *operands)
8040 emit_sf_insn ((*fun) (operands[0], operands[1], operands[2],
8041 get_fpscr_rtx ()));
8044 void
8045 expand_df_unop (rtx (*fun) (rtx, rtx, rtx), rtx *operands)
8047 emit_df_insn ((*fun) (operands[0], operands[1], get_fpscr_rtx ()));
8050 void
8051 expand_df_binop (rtx (*fun) (rtx, rtx, rtx, rtx), rtx *operands)
8053 emit_df_insn ((*fun) (operands[0], operands[1], operands[2],
8054 get_fpscr_rtx ()));
8057 /* ??? gcc does flow analysis strictly after common subexpression
8058 elimination. As a result, common subexpression elimination fails
8059 when there are some intervening statements setting the same register.
8060 If we did nothing about this, this would hurt the precision switching
8061 for SH4 badly. There is some cse after reload, but it is unable to
8062 undo the extra register pressure from the unused instructions, and
8063 it cannot remove auto-increment loads.
8065 A C code example that shows this flow/cse weakness for (at least) SH
8066 and sparc (as of gcc ss-970706) is this:
8068 double
8069 f(double a)
8071 double d;
8072 d = 0.1;
8073 a += d;
8074 d = 1.1;
8075 d = 0.1;
8076 a *= d;
8077 return a;
8080 So we add another pass before common subexpression elimination, to
8081 remove assignments that are dead due to a following assignment in the
8082 same basic block. */
8084 static void
8085 mark_use (rtx x, rtx *reg_set_block)
8087 enum rtx_code code;
8089 if (! x)
8090 return;
8091 code = GET_CODE (x);
8092 switch (code)
8094 case REG:
8096 int regno = REGNO (x);
8097 int nregs = (regno < FIRST_PSEUDO_REGISTER
8098 ? HARD_REGNO_NREGS (regno, GET_MODE (x))
8099 : 1);
8102 reg_set_block[regno + nregs - 1] = 0;
8104 while (--nregs);
8105 break;
8107 case SET:
8109 rtx dest = SET_DEST (x);
8111 if (GET_CODE (dest) == SUBREG)
8112 dest = SUBREG_REG (dest);
8113 if (GET_CODE (dest) != REG)
8114 mark_use (dest, reg_set_block);
8115 mark_use (SET_SRC (x), reg_set_block);
8116 break;
8118 case CLOBBER:
8119 break;
8120 default:
8122 const char *fmt = GET_RTX_FORMAT (code);
8123 int i, j;
8124 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8126 if (fmt[i] == 'e')
8127 mark_use (XEXP (x, i), reg_set_block);
8128 else if (fmt[i] == 'E')
8129 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8130 mark_use (XVECEXP (x, i, j), reg_set_block);
8132 break;
8137 static rtx get_free_reg (HARD_REG_SET);
8139 /* This function returns a register to use to load the address to load
8140 the fpscr from. Currently it always returns r1 or r7, but when we are
8141 able to use pseudo registers after combine, or have a better mechanism
8142 for choosing a register, it should be done here. */
8143 /* REGS_LIVE is the liveness information for the point for which we
8144 need this allocation. In some bare-bones exit blocks, r1 is live at the
8145 start. We can even have all of r0..r3 being live:
8146 __complex__ long long f (double d) { if (d == 0) return 2; else return 3; }
8147 INSN before which new insns are placed with will clobber the register
8148 we return. If a basic block consists only of setting the return value
8149 register to a pseudo and using that register, the return value is not
8150 live before or after this block, yet we we'll insert our insns right in
8151 the middle. */
8153 static rtx
8154 get_free_reg (HARD_REG_SET regs_live)
8156 if (! TEST_HARD_REG_BIT (regs_live, 1))
8157 return gen_rtx_REG (Pmode, 1);
8159 /* Hard reg 1 is live; since this is a SMALL_REGISTER_CLASSES target,
8160 there shouldn't be anything but a jump before the function end. */
8161 if (! TEST_HARD_REG_BIT (regs_live, 7))
8162 return gen_rtx_REG (Pmode, 7);
8164 abort ();
8167 /* This function will set the fpscr from memory.
8168 MODE is the mode we are setting it to. */
8169 void
8170 fpscr_set_from_mem (int mode, HARD_REG_SET regs_live)
8172 enum attr_fp_mode fp_mode = mode;
8173 rtx addr_reg = get_free_reg (regs_live);
8175 if (fp_mode == (enum attr_fp_mode) ACTUAL_NORMAL_MODE (FP_MODE))
8176 emit_insn (gen_fpu_switch1 (addr_reg));
8177 else
8178 emit_insn (gen_fpu_switch0 (addr_reg));
8181 /* Is the given character a logical line separator for the assembler? */
8182 #ifndef IS_ASM_LOGICAL_LINE_SEPARATOR
8183 #define IS_ASM_LOGICAL_LINE_SEPARATOR(C) ((C) == ';')
8184 #endif
8187 sh_insn_length_adjustment (rtx insn)
8189 /* Instructions with unfilled delay slots take up an extra two bytes for
8190 the nop in the delay slot. */
8191 if (((GET_CODE (insn) == INSN
8192 && GET_CODE (PATTERN (insn)) != USE
8193 && GET_CODE (PATTERN (insn)) != CLOBBER)
8194 || GET_CODE (insn) == CALL_INSN
8195 || (GET_CODE (insn) == JUMP_INSN
8196 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
8197 && GET_CODE (PATTERN (insn)) != ADDR_VEC))
8198 && GET_CODE (PATTERN (NEXT_INSN (PREV_INSN (insn)))) != SEQUENCE
8199 && get_attr_needs_delay_slot (insn) == NEEDS_DELAY_SLOT_YES)
8200 return 2;
8202 /* SH2e has a bug that prevents the use of annulled branches, so if
8203 the delay slot is not filled, we'll have to put a NOP in it. */
8204 if (sh_cpu == CPU_SH2E
8205 && GET_CODE (insn) == JUMP_INSN
8206 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
8207 && GET_CODE (PATTERN (insn)) != ADDR_VEC
8208 && get_attr_type (insn) == TYPE_CBRANCH
8209 && GET_CODE (PATTERN (NEXT_INSN (PREV_INSN (insn)))) != SEQUENCE)
8210 return 2;
8212 /* sh-dsp parallel processing insn take four bytes instead of two. */
8214 if (GET_CODE (insn) == INSN)
8216 int sum = 0;
8217 rtx body = PATTERN (insn);
8218 const char *template;
8219 char c;
8220 int maybe_label = 1;
8222 if (GET_CODE (body) == ASM_INPUT)
8223 template = XSTR (body, 0);
8224 else if (asm_noperands (body) >= 0)
8225 template
8226 = decode_asm_operands (body, NULL, NULL, NULL, NULL);
8227 else
8228 return 0;
8231 int ppi_adjust = 0;
8234 c = *template++;
8235 while (c == ' ' || c == '\t');
8236 /* all sh-dsp parallel-processing insns start with p.
8237 The only non-ppi sh insn starting with p is pref.
8238 The only ppi starting with pr is prnd. */
8239 if ((c == 'p' || c == 'P') && strncasecmp ("re", template, 2))
8240 ppi_adjust = 2;
8241 /* The repeat pseudo-insn expands two three insns, a total of
8242 six bytes in size. */
8243 else if ((c == 'r' || c == 'R')
8244 && ! strncasecmp ("epeat", template, 5))
8245 ppi_adjust = 4;
8246 while (c && c != '\n' && ! IS_ASM_LOGICAL_LINE_SEPARATOR (c))
8248 /* If this is a label, it is obviously not a ppi insn. */
8249 if (c == ':' && maybe_label)
8251 ppi_adjust = 0;
8252 break;
8254 else if (c == '\'' || c == '"')
8255 maybe_label = 0;
8256 c = *template++;
8258 sum += ppi_adjust;
8259 maybe_label = c != ':';
8261 while (c);
8262 return sum;
8264 return 0;
8267 /* Return TRUE if X references a SYMBOL_REF or LABEL_REF whose symbol
8268 isn't protected by a PIC unspec. */
8270 nonpic_symbol_mentioned_p (rtx x)
8272 register const char *fmt;
8273 register int i;
8275 if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF
8276 || GET_CODE (x) == PC)
8277 return 1;
8279 /* We don't want to look into the possible MEM location of a
8280 CONST_DOUBLE, since we're not going to use it, in general. */
8281 if (GET_CODE (x) == CONST_DOUBLE)
8282 return 0;
8284 if (GET_CODE (x) == UNSPEC
8285 && (XINT (x, 1) == UNSPEC_PIC
8286 || XINT (x, 1) == UNSPEC_GOT
8287 || XINT (x, 1) == UNSPEC_GOTOFF
8288 || XINT (x, 1) == UNSPEC_GOTPLT
8289 || XINT (x, 1) == UNSPEC_GOTTPOFF
8290 || XINT (x, 1) == UNSPEC_DTPOFF
8291 || XINT (x, 1) == UNSPEC_PLT))
8292 return 0;
8294 fmt = GET_RTX_FORMAT (GET_CODE (x));
8295 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
8297 if (fmt[i] == 'E')
8299 register int j;
8301 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8302 if (nonpic_symbol_mentioned_p (XVECEXP (x, i, j)))
8303 return 1;
8305 else if (fmt[i] == 'e' && nonpic_symbol_mentioned_p (XEXP (x, i)))
8306 return 1;
8309 return 0;
8312 /* Convert a non-PIC address in `orig' to a PIC address using @GOT or
8313 @GOTOFF in `reg'. */
8315 legitimize_pic_address (rtx orig, enum machine_mode mode ATTRIBUTE_UNUSED,
8316 rtx reg)
8318 if (tls_symbolic_operand (orig, Pmode))
8319 return orig;
8321 if (GET_CODE (orig) == LABEL_REF
8322 || (GET_CODE (orig) == SYMBOL_REF && SYMBOL_REF_LOCAL_P (orig)))
8324 if (reg == 0)
8325 reg = gen_reg_rtx (Pmode);
8327 emit_insn (gen_symGOTOFF2reg (reg, orig));
8328 return reg;
8330 else if (GET_CODE (orig) == SYMBOL_REF)
8332 if (reg == 0)
8333 reg = gen_reg_rtx (Pmode);
8335 emit_insn (gen_symGOT2reg (reg, orig));
8336 return reg;
8338 return orig;
8341 /* Mark the use of a constant in the literal table. If the constant
8342 has multiple labels, make it unique. */
8343 static rtx
8344 mark_constant_pool_use (rtx x)
8346 rtx insn, lab, pattern;
8348 if (x == NULL)
8349 return x;
8351 switch (GET_CODE (x))
8353 case LABEL_REF:
8354 x = XEXP (x, 0);
8355 case CODE_LABEL:
8356 break;
8357 default:
8358 return x;
8361 /* Get the first label in the list of labels for the same constant
8362 and delete another labels in the list. */
8363 lab = x;
8364 for (insn = PREV_INSN (x); insn; insn = PREV_INSN (insn))
8366 if (GET_CODE (insn) != CODE_LABEL
8367 || LABEL_REFS (insn) != NEXT_INSN (insn))
8368 break;
8369 lab = insn;
8372 for (insn = LABEL_REFS (lab); insn; insn = LABEL_REFS (insn))
8373 INSN_DELETED_P (insn) = 1;
8375 /* Mark constants in a window. */
8376 for (insn = NEXT_INSN (x); insn; insn = NEXT_INSN (insn))
8378 if (GET_CODE (insn) != INSN)
8379 continue;
8381 pattern = PATTERN (insn);
8382 if (GET_CODE (pattern) != UNSPEC_VOLATILE)
8383 continue;
8385 switch (XINT (pattern, 1))
8387 case UNSPECV_CONST2:
8388 case UNSPECV_CONST4:
8389 case UNSPECV_CONST8:
8390 XVECEXP (pattern, 0, 1) = const1_rtx;
8391 break;
8392 case UNSPECV_WINDOW_END:
8393 if (XVECEXP (pattern, 0, 0) == x)
8394 return lab;
8395 break;
8396 case UNSPECV_CONST_END:
8397 return lab;
8398 default:
8399 break;
8403 return lab;
8406 /* Return true if it's possible to redirect BRANCH1 to the destination
8407 of an unconditional jump BRANCH2. We only want to do this if the
8408 resulting branch will have a short displacement. */
8409 int
8410 sh_can_redirect_branch (rtx branch1, rtx branch2)
8412 if (flag_expensive_optimizations && simplejump_p (branch2))
8414 rtx dest = XEXP (SET_SRC (single_set (branch2)), 0);
8415 rtx insn;
8416 int distance;
8418 for (distance = 0, insn = NEXT_INSN (branch1);
8419 insn && distance < 256;
8420 insn = PREV_INSN (insn))
8422 if (insn == dest)
8423 return 1;
8424 else
8425 distance += get_attr_length (insn);
8427 for (distance = 0, insn = NEXT_INSN (branch1);
8428 insn && distance < 256;
8429 insn = NEXT_INSN (insn))
8431 if (insn == dest)
8432 return 1;
8433 else
8434 distance += get_attr_length (insn);
8437 return 0;
8440 /* Return nonzero if register old_reg can be renamed to register new_reg. */
8442 sh_hard_regno_rename_ok (unsigned int old_reg ATTRIBUTE_UNUSED,
8443 unsigned int new_reg)
8445 /* Interrupt functions can only use registers that have already been
8446 saved by the prologue, even if they would normally be
8447 call-clobbered. */
8449 if (sh_cfun_interrupt_handler_p () && !regs_ever_live[new_reg])
8450 return 0;
8452 return 1;
8455 /* Function to update the integer COST
8456 based on the relationship between INSN that is dependent on
8457 DEP_INSN through the dependence LINK. The default is to make no
8458 adjustment to COST. This can be used for example to specify to
8459 the scheduler that an output- or anti-dependence does not incur
8460 the same cost as a data-dependence. The return value should be
8461 the new value for COST. */
8462 static int
8463 sh_adjust_cost (rtx insn, rtx link ATTRIBUTE_UNUSED, rtx dep_insn, int cost)
8465 rtx reg, use_pat;
8467 if (TARGET_SHMEDIA)
8469 /* On SHmedia, if the dependence is an anti-dependence or
8470 output-dependence, there is no cost. */
8471 if (REG_NOTE_KIND (link) != 0)
8472 cost = 0;
8474 if (get_attr_is_mac_media (insn)
8475 && get_attr_is_mac_media (dep_insn))
8476 cost = 1;
8478 else if (REG_NOTE_KIND (link) == 0)
8480 enum attr_type dep_type, type;
8482 if (recog_memoized (insn) < 0
8483 || recog_memoized (dep_insn) < 0)
8484 return cost;
8486 dep_type = get_attr_type (dep_insn);
8487 if (dep_type == TYPE_FLOAD || dep_type == TYPE_PCFLOAD)
8488 cost--;
8489 if ((dep_type == TYPE_LOAD_SI || dep_type == TYPE_PCLOAD_SI)
8490 && (type = get_attr_type (insn)) != TYPE_CALL
8491 && type != TYPE_SFUNC)
8492 cost--;
8494 /* The only input for a call that is timing-critical is the
8495 function's address. */
8496 if (GET_CODE(insn) == CALL_INSN)
8498 rtx call = PATTERN (insn);
8500 if (GET_CODE (call) == PARALLEL)
8501 call = XVECEXP (call, 0 ,0);
8502 if (GET_CODE (call) == SET)
8503 call = SET_SRC (call);
8504 if (GET_CODE (call) == CALL && GET_CODE (XEXP (call, 0)) == MEM
8505 && ! reg_set_p (XEXP (XEXP (call, 0), 0), dep_insn))
8506 cost = 0;
8508 /* Likewise, the most timing critical input for an sfuncs call
8509 is the function address. However, sfuncs typically start
8510 using their arguments pretty quickly.
8511 Assume a four cycle delay before they are needed. */
8512 /* All sfunc calls are parallels with at least four components.
8513 Exploit this to avoid unnecessary calls to sfunc_uses_reg. */
8514 else if (GET_CODE (PATTERN (insn)) == PARALLEL
8515 && XVECLEN (PATTERN (insn), 0) >= 4
8516 && (reg = sfunc_uses_reg (insn)))
8518 if (! reg_set_p (reg, dep_insn))
8519 cost -= 4;
8521 /* When the preceding instruction loads the shift amount of
8522 the following SHAD/SHLD, the latency of the load is increased
8523 by 1 cycle. */
8524 else if (TARGET_SH4
8525 && get_attr_type (insn) == TYPE_DYN_SHIFT
8526 && get_attr_any_int_load (dep_insn) == ANY_INT_LOAD_YES
8527 && reg_overlap_mentioned_p (SET_DEST (PATTERN (dep_insn)),
8528 XEXP (SET_SRC (single_set (insn)),
8529 1)))
8530 cost++;
8531 /* When an LS group instruction with a latency of less than
8532 3 cycles is followed by a double-precision floating-point
8533 instruction, FIPR, or FTRV, the latency of the first
8534 instruction is increased to 3 cycles. */
8535 else if (cost < 3
8536 && get_attr_insn_class (dep_insn) == INSN_CLASS_LS_GROUP
8537 && get_attr_dfp_comp (insn) == DFP_COMP_YES)
8538 cost = 3;
8539 /* The lsw register of a double-precision computation is ready one
8540 cycle earlier. */
8541 else if (reload_completed
8542 && get_attr_dfp_comp (dep_insn) == DFP_COMP_YES
8543 && (use_pat = single_set (insn))
8544 && ! regno_use_in (REGNO (SET_DEST (single_set (dep_insn))),
8545 SET_SRC (use_pat)))
8546 cost -= 1;
8548 if (get_attr_any_fp_comp (dep_insn) == ANY_FP_COMP_YES
8549 && get_attr_late_fp_use (insn) == LATE_FP_USE_YES)
8550 cost -= 1;
8552 /* An anti-dependence penalty of two applies if the first insn is a double
8553 precision fadd / fsub / fmul. */
8554 else if (REG_NOTE_KIND (link) == REG_DEP_ANTI
8555 && recog_memoized (dep_insn) >= 0
8556 && get_attr_type (dep_insn) == TYPE_DFP_ARITH
8557 /* A lot of alleged anti-flow dependences are fake,
8558 so check this one is real. */
8559 && flow_dependent_p (dep_insn, insn))
8560 cost = 2;
8563 return cost;
8566 /* Check if INSN is flow-dependent on DEP_INSN. Can also be used to check
8567 if DEP_INSN is anti-flow dependent on INSN. */
8568 static int
8569 flow_dependent_p (rtx insn, rtx dep_insn)
8571 rtx tmp = PATTERN (insn);
8573 note_stores (PATTERN (dep_insn), flow_dependent_p_1, &tmp);
8574 return tmp == NULL_RTX;
8577 /* A helper function for flow_dependent_p called through note_stores. */
8578 static void
8579 flow_dependent_p_1 (rtx x, rtx pat ATTRIBUTE_UNUSED, void *data)
8581 rtx * pinsn = (rtx *) data;
8583 if (*pinsn && reg_referenced_p (x, *pinsn))
8584 *pinsn = NULL_RTX;
8587 /* For use by ALLOCATE_INITIAL_VALUE. Note that sh.md contains some
8588 'special function' patterns (type sfunc) that clobber pr, but that
8589 do not look like function calls to leaf_function_p. Hence we must
8590 do this extra check. */
8592 sh_pr_n_sets (void)
8594 return REG_N_SETS (TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG);
8597 /* This function returns "2" to indicate dual issue for the SH4
8598 processor. To be used by the DFA pipeline description. */
8599 static int
8600 sh_issue_rate (void)
8602 if (TARGET_SUPERSCALAR)
8603 return 2;
8604 else
8605 return 1;
8608 /* Functions for ready queue reordering for sched1. */
8610 /* Get weight for mode for a set x. */
8611 static short
8612 find_set_regmode_weight (rtx x, enum machine_mode mode)
8614 if (GET_CODE (x) == CLOBBER && register_operand (SET_DEST (x), mode))
8615 return 1;
8616 if (GET_CODE (x) == SET && register_operand (SET_DEST (x), mode))
8618 if (GET_CODE (SET_DEST (x)) == REG)
8620 if (!reg_mentioned_p (SET_DEST (x), SET_SRC (x)))
8621 return 1;
8622 else
8623 return 0;
8625 return 1;
8627 return 0;
8630 /* Get regmode weight for insn. */
8631 static short
8632 find_insn_regmode_weight (rtx insn, enum machine_mode mode)
8634 short reg_weight = 0;
8635 rtx x;
8637 /* Increment weight for each register born here. */
8638 x = PATTERN (insn);
8639 reg_weight += find_set_regmode_weight (x, mode);
8640 if (GET_CODE (x) == PARALLEL)
8642 int j;
8643 for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
8645 x = XVECEXP (PATTERN (insn), 0, j);
8646 reg_weight += find_set_regmode_weight (x, mode);
8649 /* Decrement weight for each register that dies here. */
8650 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
8652 if (REG_NOTE_KIND (x) == REG_DEAD || REG_NOTE_KIND (x) == REG_UNUSED)
8654 rtx note = XEXP (x, 0);
8655 if (GET_CODE (note) == REG && GET_MODE (note) == mode)
8656 reg_weight--;
8659 return reg_weight;
8662 /* Calculate regmode weights for all insns of a basic block. */
8663 static void
8664 find_regmode_weight (int b, enum machine_mode mode)
8666 rtx insn, next_tail, head, tail;
8668 get_block_head_tail (b, &head, &tail);
8669 next_tail = NEXT_INSN (tail);
8671 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
8673 /* Handle register life information. */
8674 if (!INSN_P (insn))
8675 continue;
8677 if (mode == SFmode)
8678 INSN_REGMODE_WEIGHT (insn, mode) =
8679 find_insn_regmode_weight (insn, mode) + 2 * find_insn_regmode_weight (insn, DFmode);
8680 else if (mode == SImode)
8681 INSN_REGMODE_WEIGHT (insn, mode) =
8682 find_insn_regmode_weight (insn, mode) + 2 * find_insn_regmode_weight (insn, DImode);
8686 /* Comparison function for ready queue sorting. */
8687 static int
8688 rank_for_reorder (const void *x, const void *y)
8690 rtx tmp = *(const rtx *) y;
8691 rtx tmp2 = *(const rtx *) x;
8693 /* The insn in a schedule group should be issued the first. */
8694 if (SCHED_GROUP_P (tmp) != SCHED_GROUP_P (tmp2))
8695 return SCHED_GROUP_P (tmp2) ? 1 : -1;
8697 /* If insns are equally good, sort by INSN_LUID (original insn order), This
8698 minimizes instruction movement, thus minimizing sched's effect on
8699 register pressure. */
8700 return INSN_LUID (tmp) - INSN_LUID (tmp2);
8703 /* Resort the array A in which only element at index N may be out of order. */
8704 static void
8705 swap_reorder (rtx *a, int n)
8707 rtx insn = a[n - 1];
8708 int i = n - 2;
8710 while (i >= 0 && rank_for_reorder (a + i, &insn) >= 0)
8712 a[i + 1] = a[i];
8713 i -= 1;
8715 a[i + 1] = insn;
8718 #define SCHED_REORDER(READY, N_READY) \
8719 do \
8721 if ((N_READY) == 2) \
8722 swap_reorder (READY, N_READY); \
8723 else if ((N_READY) > 2) \
8724 qsort (READY, N_READY, sizeof (rtx), rank_for_reorder); \
8726 while (0)
8728 /* Sort the ready list READY by ascending priority, using the SCHED_REORDER
8729 macro. */
8730 static void
8731 ready_reorder (rtx *ready, int nready)
8733 SCHED_REORDER (ready, nready);
8736 /* Calculate regmode weights for all insns of all basic block. */
8737 static void
8738 sh_md_init_global (FILE *dump ATTRIBUTE_UNUSED,
8739 int verbose ATTRIBUTE_UNUSED,
8740 int old_max_uid)
8742 basic_block b;
8744 regmode_weight[0] = (short *) xcalloc (old_max_uid, sizeof (short));
8745 regmode_weight[1] = (short *) xcalloc (old_max_uid, sizeof (short));
8747 FOR_EACH_BB_REVERSE (b)
8749 find_regmode_weight (b->index, SImode);
8750 find_regmode_weight (b->index, SFmode);
8753 CURR_REGMODE_PRESSURE (SImode) = 0;
8754 CURR_REGMODE_PRESSURE (SFmode) = 0;
8758 /* Cleanup. */
8759 static void
8760 sh_md_finish_global (FILE *dump ATTRIBUTE_UNUSED,
8761 int verbose ATTRIBUTE_UNUSED)
8763 if (regmode_weight[0])
8765 free (regmode_weight[0]);
8766 regmode_weight[0] = NULL;
8768 if (regmode_weight[1])
8770 free (regmode_weight[1]);
8771 regmode_weight[1] = NULL;
8775 /* Cache the can_issue_more so that we can return it from reorder2. Also,
8776 keep count of register pressures on SImode and SFmode. */
8777 static int
8778 sh_variable_issue (FILE *dump ATTRIBUTE_UNUSED,
8779 int sched_verbose ATTRIBUTE_UNUSED,
8780 rtx insn,
8781 int can_issue_more)
8783 if (GET_CODE (PATTERN (insn)) != USE
8784 && GET_CODE (PATTERN (insn)) != CLOBBER)
8785 cached_can_issue_more = can_issue_more - 1;
8786 else
8787 cached_can_issue_more = can_issue_more;
8789 if (reload_completed)
8790 return cached_can_issue_more;
8792 CURR_REGMODE_PRESSURE (SImode) += INSN_REGMODE_WEIGHT (insn, SImode);
8793 CURR_REGMODE_PRESSURE (SFmode) += INSN_REGMODE_WEIGHT (insn, SFmode);
8795 return cached_can_issue_more;
8798 static void
8799 sh_md_init (FILE *dump ATTRIBUTE_UNUSED,
8800 int verbose ATTRIBUTE_UNUSED,
8801 int veclen ATTRIBUTE_UNUSED)
8803 CURR_REGMODE_PRESSURE (SImode) = 0;
8804 CURR_REGMODE_PRESSURE (SFmode) = 0;
8807 /* Some magic numbers. */
8808 /* Pressure on register r0 can lead to spill failures. so avoid sched1 for
8809 functions that already have high pressure on r0. */
8810 #define R0_MAX_LIFE_REGIONS 2
8811 #define R0_MAX_LIVE_LENGTH 12
8812 /* Register Pressure thresholds for SImode and SFmode registers. */
8813 #define SIMODE_MAX_WEIGHT 5
8814 #define SFMODE_MAX_WEIGHT 10
8816 /* Return true if the pressure is high for MODE. */
8817 static short
8818 high_pressure (enum machine_mode mode)
8820 /* Pressure on register r0 can lead to spill failures. so avoid sched1 for
8821 functions that already have high pressure on r0. */
8822 if ((REG_N_SETS (0) - REG_N_DEATHS (0)) >= R0_MAX_LIFE_REGIONS
8823 && REG_LIVE_LENGTH (0) >= R0_MAX_LIVE_LENGTH)
8824 return 1;
8826 if (mode == SFmode)
8827 return (CURR_REGMODE_PRESSURE (SFmode) > SFMODE_MAX_WEIGHT);
8828 else
8829 return (CURR_REGMODE_PRESSURE (SImode) > SIMODE_MAX_WEIGHT);
8832 /* Reorder ready queue if register pressure is high. */
8833 static int
8834 sh_reorder (FILE *dump ATTRIBUTE_UNUSED,
8835 int sched_verbose ATTRIBUTE_UNUSED,
8836 rtx *ready,
8837 int *n_readyp,
8838 int clock_var ATTRIBUTE_UNUSED)
8840 if (reload_completed)
8841 return sh_issue_rate ();
8843 if (high_pressure (SFmode) || high_pressure (SImode))
8845 ready_reorder (ready, *n_readyp);
8848 return sh_issue_rate ();
8851 /* Skip cycles if the current register pressure is high. */
8852 static int
8853 sh_reorder2 (FILE *dump ATTRIBUTE_UNUSED,
8854 int sched_verbose ATTRIBUTE_UNUSED,
8855 rtx *ready ATTRIBUTE_UNUSED,
8856 int *n_readyp ATTRIBUTE_UNUSED,
8857 int clock_var ATTRIBUTE_UNUSED)
8859 if (reload_completed)
8860 return cached_can_issue_more;
8862 if (high_pressure(SFmode) || high_pressure (SImode))
8863 skip_cycles = 1;
8865 return cached_can_issue_more;
8868 /* Skip cycles without sorting the ready queue. This will move insn from
8869 Q->R. If this is the last cycle we are skipping; allow sorting of ready
8870 queue by sh_reorder. */
8872 /* Generally, skipping these many cycles are sufficient for all insns to move
8873 from Q -> R. */
8874 #define MAX_SKIPS 8
8876 static int
8877 sh_dfa_new_cycle (FILE *sched_dump ATTRIBUTE_UNUSED,
8878 int sched_verbose ATTRIBUTE_UNUSED,
8879 rtx insn ATTRIBUTE_UNUSED,
8880 int last_clock_var,
8881 int clock_var,
8882 int *sort_p)
8884 if (reload_completed)
8885 return 0;
8887 if (skip_cycles)
8889 if ((clock_var - last_clock_var) < MAX_SKIPS)
8891 *sort_p = 0;
8892 return 1;
8894 /* If this is the last cycle we are skipping, allow reordering of R. */
8895 if ((clock_var - last_clock_var) == MAX_SKIPS)
8897 *sort_p = 1;
8898 return 1;
8902 skip_cycles = 0;
8904 return 0;
8907 /* SHmedia requires registers for branches, so we can't generate new
8908 branches past reload. */
8909 static bool
8910 sh_cannot_modify_jumps_p (void)
8912 return (TARGET_SHMEDIA && (reload_in_progress || reload_completed));
8915 static int
8916 sh_target_reg_class (void)
8918 return TARGET_SHMEDIA ? TARGET_REGS : NO_REGS;
8921 static bool
8922 sh_optimize_target_register_callee_saved (bool after_prologue_epilogue_gen)
8924 return (shmedia_space_reserved_for_target_registers
8925 && (! after_prologue_epilogue_gen || TARGET_SAVE_ALL_TARGET_REGS));
8928 static bool
8929 sh_ms_bitfield_layout_p (tree record_type ATTRIBUTE_UNUSED)
8931 return (TARGET_SH5 || TARGET_HITACHI || sh_attr_renesas_p (record_type));
8935 On the SH1..SH4, the trampoline looks like
8936 2 0002 D202 mov.l l2,r2
8937 1 0000 D301 mov.l l1,r3
8938 3 0004 422B jmp @r2
8939 4 0006 0009 nop
8940 5 0008 00000000 l1: .long area
8941 6 000c 00000000 l2: .long function
8943 SH5 (compact) uses r1 instead of r3 for the static chain. */
8946 /* Emit RTL insns to initialize the variable parts of a trampoline.
8947 FNADDR is an RTX for the address of the function's pure code.
8948 CXT is an RTX for the static chain value for the function. */
8950 void
8951 sh_initialize_trampoline (rtx tramp, rtx fnaddr, rtx cxt)
8953 if (TARGET_SHMEDIA64)
8955 rtx tramp_templ;
8956 int fixed_len;
8958 rtx movi1 = GEN_INT (0xcc000010);
8959 rtx shori1 = GEN_INT (0xc8000010);
8960 rtx src, dst;
8962 /* The following trampoline works within a +- 128 KB range for cxt:
8963 ptb/u cxt,tr1; movi fnaddr >> 48,r0; shori fnaddr >> 32,r0;
8964 shori fnaddr >> 16,r0; shori fnaddr,r0; ptabs/l r0,tr0
8965 gettr tr1,r1; blink tr0,r63 */
8966 /* Address rounding makes it hard to compute the exact bounds of the
8967 offset for this trampoline, but we have a rather generous offset
8968 range, so frame_offset should do fine as an upper bound. */
8969 if (cxt == virtual_stack_vars_rtx && frame_offset < 0x20000)
8971 /* ??? could optimize this trampoline initialization
8972 by writing DImode words with two insns each. */
8973 rtx mask = force_reg (DImode, GEN_INT (0x3fffc00));
8974 rtx insn = gen_rtx_MINUS (DImode, cxt, tramp);
8975 insn = gen_rtx_ASHIFT (DImode, insn, GEN_INT (10-2));
8976 insn = gen_rtx_AND (DImode, insn, mask);
8977 /* Or in ptb/u .,tr1 pattern */
8978 insn = gen_rtx_IOR (DImode, insn, gen_int_mode (0xec000010, SImode));
8979 insn = force_operand (insn, NULL_RTX);
8980 insn = gen_lowpart (SImode, insn);
8981 emit_move_insn (gen_rtx_MEM (SImode, tramp), insn);
8982 insn = gen_rtx_LSHIFTRT (DImode, fnaddr, GEN_INT (38));
8983 insn = gen_rtx_AND (DImode, insn, mask);
8984 insn = force_operand (gen_rtx_IOR (DImode, movi1, insn), NULL_RTX);
8985 insn = gen_lowpart (SImode, insn);
8986 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 4)), insn);
8987 insn = gen_rtx_LSHIFTRT (DImode, fnaddr, GEN_INT (22));
8988 insn = gen_rtx_AND (DImode, insn, mask);
8989 insn = force_operand (gen_rtx_IOR (DImode, shori1, insn), NULL_RTX);
8990 insn = gen_lowpart (SImode, insn);
8991 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 8)), insn);
8992 insn = gen_rtx_LSHIFTRT (DImode, fnaddr, GEN_INT (6));
8993 insn = gen_rtx_AND (DImode, insn, mask);
8994 insn = force_operand (gen_rtx_IOR (DImode, shori1, insn), NULL_RTX);
8995 insn = gen_lowpart (SImode, insn);
8996 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 12)),
8997 insn);
8998 insn = gen_rtx_ASHIFT (DImode, fnaddr, GEN_INT (10));
8999 insn = gen_rtx_AND (DImode, insn, mask);
9000 insn = force_operand (gen_rtx_IOR (DImode, shori1, insn), NULL_RTX);
9001 insn = gen_lowpart (SImode, insn);
9002 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 16)),
9003 insn);
9004 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 20)),
9005 GEN_INT (0x6bf10600));
9006 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 24)),
9007 GEN_INT (0x4415fc10));
9008 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 28)),
9009 GEN_INT (0x4401fff0));
9010 emit_insn (gen_ic_invalidate_line (tramp));
9011 return;
9013 tramp_templ = gen_rtx_SYMBOL_REF (Pmode,"__GCC_nested_trampoline");
9014 fixed_len = TRAMPOLINE_SIZE - 2 * GET_MODE_SIZE (Pmode);
9016 tramp_templ = gen_datalabel_ref (tramp_templ);
9017 dst = gen_rtx_MEM (BLKmode, tramp);
9018 src = gen_rtx_MEM (BLKmode, tramp_templ);
9019 set_mem_align (dst, 256);
9020 set_mem_align (src, 64);
9021 emit_block_move (dst, src, GEN_INT (fixed_len), BLOCK_OP_NORMAL);
9023 emit_move_insn (gen_rtx_MEM (Pmode, plus_constant (tramp, fixed_len)),
9024 fnaddr);
9025 emit_move_insn (gen_rtx_MEM (Pmode,
9026 plus_constant (tramp,
9027 fixed_len
9028 + GET_MODE_SIZE (Pmode))),
9029 cxt);
9030 emit_insn (gen_ic_invalidate_line (tramp));
9031 return;
9033 else if (TARGET_SHMEDIA)
9035 /* movi fnaddr >> 16,r1; shori fnaddr,r1; ptabs/l r1,tr0
9036 movi cxt >> 16,r1; shori cxt,r1; blink tr0,r63 */
9037 rtx quad0 = gen_reg_rtx (DImode), cxtload = gen_reg_rtx (DImode);
9038 rtx quad1 = gen_reg_rtx (DImode), quad2 = gen_reg_rtx (DImode);
9039 /* movi 0,r1: 0xcc000010 shori 0,r1: c8000010 concatenated,
9040 rotated 10 right, and higher 16 bit of every 32 selected. */
9041 rtx movishori
9042 = force_reg (V2HImode, (simplify_gen_subreg
9043 (V2HImode, GEN_INT (0x4330432), SImode, 0)));
9044 rtx ptabs = force_reg (DImode, GEN_INT (0x6bf10600));
9045 rtx blink = force_reg (DImode, GEN_INT (0x4401fff0));
9047 tramp = force_reg (Pmode, tramp);
9048 fnaddr = force_reg (SImode, fnaddr);
9049 cxt = force_reg (SImode, cxt);
9050 emit_insn (gen_mshflo_w_x (gen_rtx_SUBREG (V4HImode, quad0, 0),
9051 gen_rtx_SUBREG (V2HImode, fnaddr, 0),
9052 movishori));
9053 emit_insn (gen_rotrdi3_mextr (quad0, quad0,
9054 GEN_INT (TARGET_LITTLE_ENDIAN ? 24 : 56)));
9055 emit_insn (gen_ashldi3_media (quad0, quad0, const2_rtx));
9056 emit_move_insn (gen_rtx_MEM (DImode, tramp), quad0);
9057 emit_insn (gen_mshflo_w_x (gen_rtx_SUBREG (V4HImode, cxtload, 0),
9058 gen_rtx_SUBREG (V2HImode, cxt, 0),
9059 movishori));
9060 emit_insn (gen_rotrdi3_mextr (cxtload, cxtload,
9061 GEN_INT (TARGET_LITTLE_ENDIAN ? 24 : 56)));
9062 emit_insn (gen_ashldi3_media (cxtload, cxtload, const2_rtx));
9063 if (TARGET_LITTLE_ENDIAN)
9065 emit_insn (gen_mshflo_l_di (quad1, ptabs, cxtload));
9066 emit_insn (gen_mextr4 (quad2, cxtload, blink));
9068 else
9070 emit_insn (gen_mextr4 (quad1, cxtload, ptabs));
9071 emit_insn (gen_mshflo_l_di (quad2, blink, cxtload));
9073 emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, 8)), quad1);
9074 emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, 16)), quad2);
9075 emit_insn (gen_ic_invalidate_line (tramp));
9076 return;
9078 else if (TARGET_SHCOMPACT)
9080 emit_insn (gen_initialize_trampoline (tramp, cxt, fnaddr));
9081 return;
9083 emit_move_insn (gen_rtx_MEM (SImode, tramp),
9084 gen_int_mode (TARGET_LITTLE_ENDIAN ? 0xd301d202 : 0xd202d301,
9085 SImode));
9086 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 4)),
9087 gen_int_mode (TARGET_LITTLE_ENDIAN ? 0x0009422b : 0x422b0009,
9088 SImode));
9089 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 8)),
9090 cxt);
9091 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 12)),
9092 fnaddr);
9093 if (TARGET_HARVARD)
9095 if (TARGET_USERMODE)
9096 emit_library_call (function_symbol ("__ic_invalidate"),
9097 0, VOIDmode, 1, tramp, SImode);
9098 else
9099 emit_insn (gen_ic_invalidate_line (tramp));
9103 /* FIXME: This is overly conservative. A SHcompact function that
9104 receives arguments ``by reference'' will have them stored in its
9105 own stack frame, so it must not pass pointers or references to
9106 these arguments to other functions by means of sibling calls. */
9107 static bool
9108 sh_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
9110 return (decl
9111 && (! TARGET_SHCOMPACT
9112 || current_function_args_info.stack_regs == 0)
9113 && ! sh_cfun_interrupt_handler_p ());
9116 /* Machine specific built-in functions. */
9118 struct builtin_description
9120 const enum insn_code icode;
9121 const char *const name;
9122 int signature;
9125 /* describe number and signedness of arguments; arg[0] == result
9126 (1: unsigned, 2: signed, 4: don't care, 8: pointer 0: no argument */
9127 static const char signature_args[][4] =
9129 #define SH_BLTIN_V2SI2 0
9130 { 4, 4 },
9131 #define SH_BLTIN_V4HI2 1
9132 { 4, 4 },
9133 #define SH_BLTIN_V2SI3 2
9134 { 4, 4, 4 },
9135 #define SH_BLTIN_V4HI3 3
9136 { 4, 4, 4 },
9137 #define SH_BLTIN_V8QI3 4
9138 { 4, 4, 4 },
9139 #define SH_BLTIN_MAC_HISI 5
9140 { 1, 4, 4, 1 },
9141 #define SH_BLTIN_SH_HI 6
9142 { 4, 4, 1 },
9143 #define SH_BLTIN_SH_SI 7
9144 { 4, 4, 1 },
9145 #define SH_BLTIN_V4HI2V2SI 8
9146 { 4, 4, 4 },
9147 #define SH_BLTIN_V4HI2V8QI 9
9148 { 4, 4, 4 },
9149 #define SH_BLTIN_SISF 10
9150 { 4, 2 },
9151 #define SH_BLTIN_LDUA_L 11
9152 { 2, 8 },
9153 #define SH_BLTIN_LDUA_Q 12
9154 { 1, 8 },
9155 #define SH_BLTIN_STUA_L 13
9156 { 0, 8, 2 },
9157 #define SH_BLTIN_STUA_Q 14
9158 { 0, 8, 1 },
9159 #define SH_BLTIN_UDI 15
9160 { 0, 8, 1 },
9161 #define SH_BLTIN_NUM_SHARED_SIGNATURES 16
9162 #define SH_BLTIN_2 16
9163 #define SH_BLTIN_SU 16
9164 { 1, 2 },
9165 #define SH_BLTIN_3 17
9166 #define SH_BLTIN_SUS 17
9167 { 2, 2, 1 },
9168 #define SH_BLTIN_PSSV 18
9169 { 0, 8, 2, 2 },
9170 #define SH_BLTIN_XXUU 19
9171 #define SH_BLTIN_UUUU 19
9172 { 1, 1, 1, 1 },
9173 #define SH_BLTIN_PV 20
9174 { 0, 8 },
9176 /* mcmv: operands considered unsigned. */
9177 /* mmulsum_wq, msad_ubq: result considered unsigned long long. */
9178 /* mperm: control value considered unsigned int. */
9179 /* mshalds, mshard, mshards, mshlld, mshlrd: shift count is unsigned int. */
9180 /* mshards_q: returns signed short. */
9181 /* nsb: takes long long arg, returns unsigned char. */
9182 static const struct builtin_description bdesc[] =
9184 { CODE_FOR_absv2si2, "__builtin_absv2si2", SH_BLTIN_V2SI2 },
9185 { CODE_FOR_absv4hi2, "__builtin_absv4hi2", SH_BLTIN_V4HI2 },
9186 { CODE_FOR_addv2si3, "__builtin_addv2si3", SH_BLTIN_V2SI3 },
9187 { CODE_FOR_addv4hi3, "__builtin_addv4hi3", SH_BLTIN_V4HI3 },
9188 { CODE_FOR_ssaddv2si3,"__builtin_ssaddv2si3", SH_BLTIN_V2SI3 },
9189 { CODE_FOR_usaddv8qi3,"__builtin_usaddv8qi3", SH_BLTIN_V8QI3 },
9190 { CODE_FOR_ssaddv4hi3,"__builtin_ssaddv4hi3", SH_BLTIN_V4HI3 },
9191 #if 0
9192 { CODE_FOR_alloco32, "__builtin_sh_media_ALLOCO", SH_BLTIN_PV },
9193 { CODE_FOR_alloco64, "__builtin_sh_media_ALLOCO", SH_BLTIN_PV },
9194 #endif
9195 { CODE_FOR_negcmpeqv8qi,"__builtin_sh_media_MCMPEQ_B", SH_BLTIN_V8QI3 },
9196 { CODE_FOR_negcmpeqv2si,"__builtin_sh_media_MCMPEQ_L", SH_BLTIN_V2SI3 },
9197 { CODE_FOR_negcmpeqv4hi,"__builtin_sh_media_MCMPEQ_W", SH_BLTIN_V4HI3 },
9198 { CODE_FOR_negcmpgtuv8qi,"__builtin_sh_media_MCMPGT_UB", SH_BLTIN_V8QI3 },
9199 { CODE_FOR_negcmpgtv2si,"__builtin_sh_media_MCMPGT_L", SH_BLTIN_V2SI3 },
9200 { CODE_FOR_negcmpgtv4hi,"__builtin_sh_media_MCMPGT_W", SH_BLTIN_V4HI3 },
9201 { CODE_FOR_mcmv, "__builtin_sh_media_MCMV", SH_BLTIN_UUUU },
9202 { CODE_FOR_mcnvs_lw, "__builtin_sh_media_MCNVS_LW", SH_BLTIN_3 },
9203 { CODE_FOR_mcnvs_wb, "__builtin_sh_media_MCNVS_WB", SH_BLTIN_V4HI2V8QI },
9204 { CODE_FOR_mcnvs_wub, "__builtin_sh_media_MCNVS_WUB", SH_BLTIN_V4HI2V8QI },
9205 { CODE_FOR_mextr1, "__builtin_sh_media_MEXTR1", SH_BLTIN_UDI },
9206 { CODE_FOR_mextr2, "__builtin_sh_media_MEXTR2", SH_BLTIN_UDI },
9207 { CODE_FOR_mextr3, "__builtin_sh_media_MEXTR3", SH_BLTIN_UDI },
9208 { CODE_FOR_mextr4, "__builtin_sh_media_MEXTR4", SH_BLTIN_UDI },
9209 { CODE_FOR_mextr5, "__builtin_sh_media_MEXTR5", SH_BLTIN_UDI },
9210 { CODE_FOR_mextr6, "__builtin_sh_media_MEXTR6", SH_BLTIN_UDI },
9211 { CODE_FOR_mextr7, "__builtin_sh_media_MEXTR7", SH_BLTIN_UDI },
9212 { CODE_FOR_mmacfx_wl, "__builtin_sh_media_MMACFX_WL", SH_BLTIN_MAC_HISI },
9213 { CODE_FOR_mmacnfx_wl,"__builtin_sh_media_MMACNFX_WL", SH_BLTIN_MAC_HISI },
9214 { CODE_FOR_mulv2si3, "__builtin_mulv2si3", SH_BLTIN_V2SI3, },
9215 { CODE_FOR_mulv4hi3, "__builtin_mulv4hi3", SH_BLTIN_V4HI3 },
9216 { CODE_FOR_mmulfx_l, "__builtin_sh_media_MMULFX_L", SH_BLTIN_V2SI3 },
9217 { CODE_FOR_mmulfx_w, "__builtin_sh_media_MMULFX_W", SH_BLTIN_V4HI3 },
9218 { CODE_FOR_mmulfxrp_w,"__builtin_sh_media_MMULFXRP_W", SH_BLTIN_V4HI3 },
9219 { CODE_FOR_mmulhi_wl, "__builtin_sh_media_MMULHI_WL", SH_BLTIN_V4HI2V2SI },
9220 { CODE_FOR_mmullo_wl, "__builtin_sh_media_MMULLO_WL", SH_BLTIN_V4HI2V2SI },
9221 { CODE_FOR_mmulsum_wq,"__builtin_sh_media_MMULSUM_WQ", SH_BLTIN_XXUU },
9222 { CODE_FOR_mperm_w, "__builtin_sh_media_MPERM_W", SH_BLTIN_SH_HI },
9223 { CODE_FOR_msad_ubq, "__builtin_sh_media_MSAD_UBQ", SH_BLTIN_XXUU },
9224 { CODE_FOR_mshalds_l, "__builtin_sh_media_MSHALDS_L", SH_BLTIN_SH_SI },
9225 { CODE_FOR_mshalds_w, "__builtin_sh_media_MSHALDS_W", SH_BLTIN_SH_HI },
9226 { CODE_FOR_ashrv2si3, "__builtin_ashrv2si3", SH_BLTIN_SH_SI },
9227 { CODE_FOR_ashrv4hi3, "__builtin_ashrv4hi3", SH_BLTIN_SH_HI },
9228 { CODE_FOR_mshards_q, "__builtin_sh_media_MSHARDS_Q", SH_BLTIN_SUS },
9229 { CODE_FOR_mshfhi_b, "__builtin_sh_media_MSHFHI_B", SH_BLTIN_V8QI3 },
9230 { CODE_FOR_mshfhi_l, "__builtin_sh_media_MSHFHI_L", SH_BLTIN_V2SI3 },
9231 { CODE_FOR_mshfhi_w, "__builtin_sh_media_MSHFHI_W", SH_BLTIN_V4HI3 },
9232 { CODE_FOR_mshflo_b, "__builtin_sh_media_MSHFLO_B", SH_BLTIN_V8QI3 },
9233 { CODE_FOR_mshflo_l, "__builtin_sh_media_MSHFLO_L", SH_BLTIN_V2SI3 },
9234 { CODE_FOR_mshflo_w, "__builtin_sh_media_MSHFLO_W", SH_BLTIN_V4HI3 },
9235 { CODE_FOR_ashlv2si3, "__builtin_ashlv2si3", SH_BLTIN_SH_SI },
9236 { CODE_FOR_ashlv4hi3, "__builtin_ashlv4hi3", SH_BLTIN_SH_HI },
9237 { CODE_FOR_lshrv2si3, "__builtin_lshrv2si3", SH_BLTIN_SH_SI },
9238 { CODE_FOR_lshrv4hi3, "__builtin_lshrv4hi3", SH_BLTIN_SH_HI },
9239 { CODE_FOR_subv2si3, "__builtin_subv2si3", SH_BLTIN_V2SI3 },
9240 { CODE_FOR_subv4hi3, "__builtin_subv4hi3", SH_BLTIN_V4HI3 },
9241 { CODE_FOR_sssubv2si3,"__builtin_sssubv2si3", SH_BLTIN_V2SI3 },
9242 { CODE_FOR_ussubv8qi3,"__builtin_ussubv8qi3", SH_BLTIN_V8QI3 },
9243 { CODE_FOR_sssubv4hi3,"__builtin_sssubv4hi3", SH_BLTIN_V4HI3 },
9244 { CODE_FOR_fcosa_s, "__builtin_sh_media_FCOSA_S", SH_BLTIN_SISF },
9245 { CODE_FOR_fsina_s, "__builtin_sh_media_FSINA_S", SH_BLTIN_SISF },
9246 { CODE_FOR_fipr, "__builtin_sh_media_FIPR_S", SH_BLTIN_3 },
9247 { CODE_FOR_ftrv, "__builtin_sh_media_FTRV_S", SH_BLTIN_3 },
9248 { CODE_FOR_fsrra_s, "__builtin_sh_media_FSRRA_S", SH_BLTIN_2 },
9249 #if 0
9250 { CODE_FOR_ldhi_l, "__builtin_sh_media_LDHI_L", SH_BLTIN_LDUA_L },
9251 { CODE_FOR_ldhi_q, "__builtin_sh_media_LDHI_Q", SH_BLTIN_LDUA_Q },
9252 { CODE_FOR_ldlo_l, "__builtin_sh_media_LDLO_L", SH_BLTIN_LDUA_L },
9253 { CODE_FOR_ldlo_q, "__builtin_sh_media_LDLO_Q", SH_BLTIN_LDUA_Q },
9254 { CODE_FOR_sthi_l, "__builtin_sh_media_STHI_L", SH_BLTIN_STUA_L },
9255 { CODE_FOR_sthi_q, "__builtin_sh_media_STHI_Q", SH_BLTIN_STUA_Q },
9256 { CODE_FOR_stlo_l, "__builtin_sh_media_STLO_L", SH_BLTIN_STUA_L },
9257 { CODE_FOR_stlo_q, "__builtin_sh_media_STLO_Q", SH_BLTIN_STUA_Q },
9258 { CODE_FOR_ldhi_l64, "__builtin_sh_media_LDHI_L", SH_BLTIN_LDUA_L },
9259 { CODE_FOR_ldhi_q64, "__builtin_sh_media_LDHI_Q", SH_BLTIN_LDUA_Q },
9260 { CODE_FOR_ldlo_l64, "__builtin_sh_media_LDLO_L", SH_BLTIN_LDUA_L },
9261 { CODE_FOR_ldlo_q64, "__builtin_sh_media_LDLO_Q", SH_BLTIN_LDUA_Q },
9262 { CODE_FOR_sthi_l64, "__builtin_sh_media_STHI_L", SH_BLTIN_STUA_L },
9263 { CODE_FOR_sthi_q64, "__builtin_sh_media_STHI_Q", SH_BLTIN_STUA_Q },
9264 { CODE_FOR_stlo_l64, "__builtin_sh_media_STLO_L", SH_BLTIN_STUA_L },
9265 { CODE_FOR_stlo_q64, "__builtin_sh_media_STLO_Q", SH_BLTIN_STUA_Q },
9266 #endif
9267 { CODE_FOR_nsb, "__builtin_sh_media_NSB", SH_BLTIN_SU },
9268 { CODE_FOR_byterev, "__builtin_sh_media_BYTEREV", SH_BLTIN_2 },
9269 #if 0
9270 { CODE_FOR_prefetch32,"__builtin_sh_media_PREFO", SH_BLTIN_PSSV },
9271 { CODE_FOR_prefetch64,"__builtin_sh_media_PREFO", SH_BLTIN_PSSV }
9272 #endif
9275 static void
9276 sh_media_init_builtins (void)
9278 tree shared[SH_BLTIN_NUM_SHARED_SIGNATURES];
9279 const struct builtin_description *d;
9281 memset (shared, 0, sizeof shared);
9282 for (d = bdesc; d - bdesc < (int) ARRAY_SIZE (bdesc); d++)
9284 tree type, arg_type;
9285 int signature = d->signature;
9286 int i;
9288 if (signature < SH_BLTIN_NUM_SHARED_SIGNATURES && shared[signature])
9289 type = shared[signature];
9290 else
9292 int has_result = signature_args[signature][0] != 0;
9294 if (signature_args[signature][1] == 8
9295 && (insn_data[d->icode].operand[has_result].mode != Pmode))
9296 continue;
9297 if (! TARGET_FPU_ANY
9298 && FLOAT_MODE_P (insn_data[d->icode].operand[0].mode))
9299 continue;
9300 type = void_list_node;
9301 for (i = 3; ; i--)
9303 int arg = signature_args[signature][i];
9304 int opno = i - 1 + has_result;
9306 if (arg == 8)
9307 arg_type = ptr_type_node;
9308 else if (arg)
9309 arg_type = ((*lang_hooks.types.type_for_mode)
9310 (insn_data[d->icode].operand[opno].mode,
9311 (arg & 1)));
9312 else if (i)
9313 continue;
9314 else
9315 arg_type = void_type_node;
9316 if (i == 0)
9317 break;
9318 type = tree_cons (NULL_TREE, arg_type, type);
9320 type = build_function_type (arg_type, type);
9321 if (signature < SH_BLTIN_NUM_SHARED_SIGNATURES)
9322 shared[signature] = type;
9324 lang_hooks.builtin_function (d->name, type, d - bdesc, BUILT_IN_MD,
9325 NULL, NULL_TREE);
9329 static void
9330 sh_init_builtins (void)
9332 if (TARGET_SHMEDIA)
9333 sh_media_init_builtins ();
9336 /* Expand an expression EXP that calls a built-in function,
9337 with result going to TARGET if that's convenient
9338 (and in mode MODE if that's convenient).
9339 SUBTARGET may be used as the target for computing one of EXP's operands.
9340 IGNORE is nonzero if the value is to be ignored. */
9342 static rtx
9343 sh_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
9344 enum machine_mode mode ATTRIBUTE_UNUSED, int ignore)
9346 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
9347 tree arglist = TREE_OPERAND (exp, 1);
9348 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
9349 const struct builtin_description *d = &bdesc[fcode];
9350 enum insn_code icode = d->icode;
9351 int signature = d->signature;
9352 enum machine_mode tmode = VOIDmode;
9353 int nop = 0, i;
9354 rtx op[4];
9355 rtx pat;
9357 if (signature_args[signature][0])
9359 if (ignore)
9360 return 0;
9362 tmode = insn_data[icode].operand[0].mode;
9363 if (! target
9364 || GET_MODE (target) != tmode
9365 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
9366 target = gen_reg_rtx (tmode);
9367 op[nop++] = target;
9369 else
9370 target = 0;
9372 for (i = 1; i <= 3; i++, nop++)
9374 tree arg;
9375 enum machine_mode opmode, argmode;
9377 if (! signature_args[signature][i])
9378 break;
9379 arg = TREE_VALUE (arglist);
9380 if (arg == error_mark_node)
9381 return const0_rtx;
9382 arglist = TREE_CHAIN (arglist);
9383 opmode = insn_data[icode].operand[nop].mode;
9384 argmode = TYPE_MODE (TREE_TYPE (arg));
9385 if (argmode != opmode)
9386 arg = build1 (NOP_EXPR,
9387 (*lang_hooks.types.type_for_mode) (opmode, 0), arg);
9388 op[nop] = expand_expr (arg, NULL_RTX, opmode, 0);
9389 if (! (*insn_data[icode].operand[nop].predicate) (op[nop], opmode))
9390 op[nop] = copy_to_mode_reg (opmode, op[nop]);
9393 switch (nop)
9395 case 1:
9396 pat = (*insn_data[d->icode].genfun) (op[0]);
9397 break;
9398 case 2:
9399 pat = (*insn_data[d->icode].genfun) (op[0], op[1]);
9400 break;
9401 case 3:
9402 pat = (*insn_data[d->icode].genfun) (op[0], op[1], op[2]);
9403 break;
9404 case 4:
9405 pat = (*insn_data[d->icode].genfun) (op[0], op[1], op[2], op[3]);
9406 break;
9407 default:
9408 abort ();
9410 if (! pat)
9411 return 0;
9412 emit_insn (pat);
9413 return target;
9416 void
9417 sh_expand_unop_v2sf (enum rtx_code code, rtx op0, rtx op1)
9419 rtx sel0 = const0_rtx;
9420 rtx sel1 = const1_rtx;
9421 rtx (*fn) (rtx, rtx, rtx, rtx, rtx) = gen_unary_sf_op;
9422 rtx op = gen_rtx_fmt_e (code, SFmode, op1);
9424 emit_insn ((*fn) (op0, op1, op, sel0, sel0));
9425 emit_insn ((*fn) (op0, op1, op, sel1, sel1));
9428 void
9429 sh_expand_binop_v2sf (enum rtx_code code, rtx op0, rtx op1, rtx op2)
9431 rtx sel0 = const0_rtx;
9432 rtx sel1 = const1_rtx;
9433 rtx (*fn) (rtx, rtx, rtx, rtx, rtx, rtx, rtx, rtx)
9434 = gen_binary_sf_op;
9435 rtx op = gen_rtx_fmt_ee (code, SFmode, op1, op2);
9437 emit_insn ((*fn) (op0, op1, op2, op, sel0, sel0, sel0, sel1));
9438 emit_insn ((*fn) (op0, op1, op2, op, sel1, sel1, sel1, sel0));
9441 /* Return the class of registers for which a mode change from FROM to TO
9442 is invalid. */
9443 bool
9444 sh_cannot_change_mode_class (enum machine_mode from, enum machine_mode to,
9445 enum reg_class class)
9447 /* We want to enable the use of SUBREGs as a means to
9448 VEC_SELECT a single element of a vector. */
9449 if (to == SFmode && VECTOR_MODE_P (from) && GET_MODE_INNER (from) == SFmode)
9450 return (reg_classes_intersect_p (GENERAL_REGS, class));
9452 if (GET_MODE_SIZE (from) != GET_MODE_SIZE (to))
9454 if (TARGET_LITTLE_ENDIAN)
9456 if (GET_MODE_SIZE (to) < 8 || GET_MODE_SIZE (from) < 8)
9457 return reg_classes_intersect_p (DF_REGS, class);
9459 else
9461 if (GET_MODE_SIZE (from) < 8)
9462 return reg_classes_intersect_p (DF_HI_REGS, class);
9465 return 0;
9469 /* If ADDRESS refers to a CODE_LABEL, add NUSES to the number of times
9470 that label is used. */
9472 void
9473 sh_mark_label (rtx address, int nuses)
9475 if (GOTOFF_P (address))
9477 /* Extract the label or symbol. */
9478 address = XEXP (address, 0);
9479 if (GET_CODE (address) == PLUS)
9480 address = XEXP (address, 0);
9481 address = XVECEXP (address, 0, 0);
9483 if (GET_CODE (address) == LABEL_REF
9484 && GET_CODE (XEXP (address, 0)) == CODE_LABEL)
9485 LABEL_NUSES (XEXP (address, 0)) += nuses;
9488 /* Compute extra cost of moving data between one register class
9489 and another. */
9491 /* If SECONDARY*_RELOAD_CLASS says something about the src/dst pair, regclass
9492 uses this information. Hence, the general register <-> floating point
9493 register information here is not used for SFmode. */
9496 sh_register_move_cost (enum machine_mode mode,
9497 enum reg_class srcclass, enum reg_class dstclass)
9499 if (dstclass == T_REGS || dstclass == PR_REGS)
9500 return 10;
9502 if (dstclass == MAC_REGS && srcclass == MAC_REGS)
9503 return 4;
9505 if (mode == SImode && ! TARGET_SHMEDIA && TARGET_FMOVD
9506 && REGCLASS_HAS_FP_REG (srcclass)
9507 && REGCLASS_HAS_FP_REG (dstclass))
9508 return 4;
9510 if ((REGCLASS_HAS_FP_REG (dstclass) && srcclass == MAC_REGS)
9511 || (dstclass == MAC_REGS && REGCLASS_HAS_FP_REG (srcclass)))
9512 return 9;
9514 if ((REGCLASS_HAS_FP_REG (dstclass)
9515 && REGCLASS_HAS_GENERAL_REG (srcclass))
9516 || (REGCLASS_HAS_GENERAL_REG (dstclass)
9517 && REGCLASS_HAS_FP_REG (srcclass)))
9518 return ((TARGET_SHMEDIA ? 4 : TARGET_FMOVD ? 8 : 12)
9519 * ((GET_MODE_SIZE (mode) + 7) / 8U));
9521 if ((dstclass == FPUL_REGS
9522 && REGCLASS_HAS_GENERAL_REG (srcclass))
9523 || (srcclass == FPUL_REGS
9524 && REGCLASS_HAS_GENERAL_REG (dstclass)))
9525 return 5;
9527 if ((dstclass == FPUL_REGS
9528 && (srcclass == PR_REGS || srcclass == MAC_REGS || srcclass == T_REGS))
9529 || (srcclass == FPUL_REGS
9530 && (dstclass == PR_REGS || dstclass == MAC_REGS)))
9531 return 7;
9533 if ((srcclass == TARGET_REGS && ! REGCLASS_HAS_GENERAL_REG (dstclass))
9534 || ((dstclass) == TARGET_REGS && ! REGCLASS_HAS_GENERAL_REG (srcclass)))
9535 return 20;
9537 if ((srcclass == FPSCR_REGS && ! REGCLASS_HAS_GENERAL_REG (dstclass))
9538 || (dstclass == FPSCR_REGS && ! REGCLASS_HAS_GENERAL_REG (srcclass)))
9539 return 4;
9541 if (TARGET_SHMEDIA
9542 || (TARGET_FMOVD
9543 && ! REGCLASS_HAS_GENERAL_REG (srcclass)
9544 && ! REGCLASS_HAS_GENERAL_REG (dstclass)))
9545 return 2 * ((GET_MODE_SIZE (mode) + 7) / 8U);
9547 return 2 * ((GET_MODE_SIZE (mode) + 3) / 4U);
9550 /* Like register_operand, but take into account that SHMEDIA can use
9551 the constant zero like a general register. */
9553 sh_register_operand (rtx op, enum machine_mode mode)
9555 if (op == CONST0_RTX (mode) && TARGET_SHMEDIA)
9556 return 1;
9557 return register_operand (op, mode);
9561 cmpsi_operand (rtx op, enum machine_mode mode)
9563 if (GET_CODE (op) == REG && REGNO (op) == T_REG
9564 && GET_MODE (op) == SImode)
9565 return 1;
9566 return arith_operand (op, mode);
9569 static rtx emit_load_ptr (rtx, rtx);
9571 static rtx
9572 emit_load_ptr (rtx reg, rtx addr)
9574 rtx mem = gen_rtx_MEM (ptr_mode, addr);
9576 if (Pmode != ptr_mode)
9577 mem = gen_rtx_SIGN_EXTEND (Pmode, mem);
9578 return emit_move_insn (reg, mem);
9581 void
9582 sh_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
9583 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
9584 tree function)
9586 CUMULATIVE_ARGS cum;
9587 int structure_value_byref = 0;
9588 rtx this, this_value, sibcall, insns, funexp;
9589 tree funtype = TREE_TYPE (function);
9590 int simple_add = CONST_OK_FOR_ADD (delta);
9591 int did_load = 0;
9592 rtx scratch0, scratch1, scratch2;
9594 reload_completed = 1;
9595 epilogue_completed = 1;
9596 no_new_pseudos = 1;
9597 current_function_uses_only_leaf_regs = 1;
9598 reset_block_changes ();
9600 emit_note (NOTE_INSN_PROLOGUE_END);
9602 /* Find the "this" pointer. We have such a wide range of ABIs for the
9603 SH that it's best to do this completely machine independently.
9604 "this" is passed as first argument, unless a structure return pointer
9605 comes first, in which case "this" comes second. */
9606 INIT_CUMULATIVE_ARGS (cum, funtype, NULL_RTX, 0, 1);
9607 #ifndef PCC_STATIC_STRUCT_RETURN
9608 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
9609 structure_value_byref = 1;
9610 #endif /* not PCC_STATIC_STRUCT_RETURN */
9611 if (structure_value_byref && sh_struct_value_rtx (function, 0) == 0)
9613 tree ptype = build_pointer_type (TREE_TYPE (funtype));
9615 FUNCTION_ARG_ADVANCE (cum, Pmode, ptype, 1);
9617 this = FUNCTION_ARG (cum, Pmode, ptr_type_node, 1);
9619 /* For SHcompact, we only have r0 for a scratch register: r1 is the
9620 static chain pointer (even if you can't have nested virtual functions
9621 right now, someone might implement them sometime), and the rest of the
9622 registers are used for argument passing, are callee-saved, or reserved. */
9623 scratch0 = scratch1 = scratch2 = gen_rtx_REG (Pmode, 0);
9624 if (! TARGET_SH5)
9626 scratch1 = gen_rtx_REG (ptr_mode, 1);
9627 /* N.B., if not TARGET_HITACHI, register 2 is used to pass the pointer
9628 pointing where to return struct values. */
9629 scratch2 = gen_rtx_REG (Pmode, 3);
9631 else if (TARGET_SHMEDIA)
9633 scratch1 = gen_rtx_REG (ptr_mode, 21);
9634 scratch2 = gen_rtx_REG (Pmode, TR0_REG);
9637 this_value = plus_constant (this, delta);
9638 if (vcall_offset
9639 && (simple_add || scratch0 != scratch1)
9640 && strict_memory_address_p (ptr_mode, this_value))
9642 emit_load_ptr (scratch0, this_value);
9643 did_load = 1;
9646 if (!delta)
9647 ; /* Do nothing. */
9648 else if (simple_add)
9649 emit_move_insn (this, this_value);
9650 else
9652 emit_move_insn (scratch1, GEN_INT (delta));
9653 emit_insn (gen_add2_insn (this, scratch1));
9656 if (vcall_offset)
9658 rtx offset_addr;
9660 if (!did_load)
9661 emit_load_ptr (scratch0, this);
9663 offset_addr = plus_constant (scratch0, vcall_offset);
9664 if (strict_memory_address_p (ptr_mode, offset_addr))
9665 ; /* Do nothing. */
9666 else if (! TARGET_SH5)
9668 /* scratch0 != scratch1, and we have indexed loads. Get better
9669 schedule by loading the offset into r1 and using an indexed
9670 load - then the load of r1 can issue before the load from
9671 (this + delta) finishes. */
9672 emit_move_insn (scratch1, GEN_INT (vcall_offset));
9673 offset_addr = gen_rtx_PLUS (Pmode, scratch0, scratch1);
9675 else if (CONST_OK_FOR_ADD (vcall_offset))
9677 emit_insn (gen_add2_insn (scratch0, GEN_INT (vcall_offset)));
9678 offset_addr = scratch0;
9680 else if (scratch0 != scratch1)
9682 emit_move_insn (scratch1, GEN_INT (vcall_offset));
9683 emit_insn (gen_add2_insn (scratch0, scratch1));
9684 offset_addr = scratch0;
9686 else
9687 abort (); /* FIXME */
9688 emit_load_ptr (scratch0, offset_addr);
9690 if (Pmode != ptr_mode)
9691 scratch0 = gen_rtx_TRUNCATE (ptr_mode, scratch0);
9692 emit_insn (gen_add2_insn (this, scratch0));
9695 /* Generate a tail call to the target function. */
9696 if (! TREE_USED (function))
9698 assemble_external (function);
9699 TREE_USED (function) = 1;
9701 funexp = XEXP (DECL_RTL (function), 0);
9702 emit_move_insn (scratch2, funexp);
9703 funexp = gen_rtx_MEM (FUNCTION_MODE, scratch2);
9704 sibcall = emit_call_insn (gen_sibcall (funexp, const0_rtx, NULL_RTX));
9705 SIBLING_CALL_P (sibcall) = 1;
9706 use_reg (&CALL_INSN_FUNCTION_USAGE (sibcall), this);
9707 emit_barrier ();
9709 /* Run just enough of rest_of_compilation to do scheduling and get
9710 the insns emitted. Note that use_thunk calls
9711 assemble_start_function and assemble_end_function. */
9713 insn_locators_initialize ();
9714 insns = get_insns ();
9716 if (optimize > 0 && flag_schedule_insns_after_reload)
9718 find_basic_blocks (insns, max_reg_num (), dump_file);
9719 life_analysis (dump_file, PROP_FINAL);
9721 split_all_insns (1);
9723 schedule_insns (dump_file);
9726 sh_reorg ();
9728 if (optimize > 0 && flag_delayed_branch)
9729 dbr_schedule (insns, dump_file);
9730 shorten_branches (insns);
9731 final_start_function (insns, file, 1);
9732 final (insns, file, 1, 0);
9733 final_end_function ();
9735 if (optimize > 0 && flag_schedule_insns_after_reload)
9737 /* Release all memory allocated by flow. */
9738 free_basic_block_vars ();
9740 /* Release all memory held by regsets now. */
9741 regset_release_memory ();
9744 reload_completed = 0;
9745 epilogue_completed = 0;
9746 no_new_pseudos = 0;
9750 function_symbol (const char *name)
9752 rtx sym = gen_rtx_SYMBOL_REF (Pmode, name);
9753 SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_FUNCTION;
9754 return sym;
9757 /* Find the number of a general purpose register in S. */
9758 static int
9759 scavenge_reg (HARD_REG_SET *s)
9761 int r;
9762 for (r = FIRST_GENERAL_REG; r <= LAST_GENERAL_REG; r++)
9763 if (TEST_HARD_REG_BIT (*s, r))
9764 return r;
9765 return -1;
9769 sh_get_pr_initial_val (void)
9771 rtx val;
9773 /* ??? Unfortunately, get_hard_reg_initial_val doesn't always work for the
9774 PR register on SHcompact, because it might be clobbered by the prologue.
9775 We check first if that is known to be the case. */
9776 if (TARGET_SHCOMPACT
9777 && ((current_function_args_info.call_cookie
9778 & ~ CALL_COOKIE_RET_TRAMP (1))
9779 || current_function_has_nonlocal_label))
9780 return gen_rtx_MEM (SImode, return_address_pointer_rtx);
9782 /* If we haven't finished rtl generation, there might be a nonlocal label
9783 that we haven't seen yet.
9784 ??? get_hard_reg_initial_val fails if it is called while no_new_pseudos
9785 is set, unless it has been called before for the same register. And even
9786 then, we end in trouble if we didn't use the register in the same
9787 basic block before. So call get_hard_reg_initial_val now and wrap it
9788 in an unspec if we might need to replace it. */
9789 /* ??? We also must do this for TARGET_SH1 in general, because otherwise
9790 combine can put the pseudo returned by get_hard_reg_initial_val into
9791 instructions that need a general purpose registers, which will fail to
9792 be recognized when the pseudo becomes allocated to PR. */
9794 = get_hard_reg_initial_val (Pmode, TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG);
9795 if (TARGET_SH1)
9796 return gen_rtx_UNSPEC (SImode, gen_rtvec (1, val), UNSPEC_RA);
9797 return val;
9801 sh_expand_t_scc (enum rtx_code code, rtx target)
9803 rtx result = target;
9804 HOST_WIDE_INT val;
9806 if (GET_CODE (sh_compare_op0) != REG || REGNO (sh_compare_op0) != T_REG
9807 || GET_CODE (sh_compare_op1) != CONST_INT)
9808 return 0;
9809 if (GET_CODE (result) != REG)
9810 result = gen_reg_rtx (SImode);
9811 val = INTVAL (sh_compare_op1);
9812 if ((code == EQ && val == 1) || (code == NE && val == 0))
9813 emit_insn (gen_movt (result));
9814 else if ((code == EQ && val == 0) || (code == NE && val == 1))
9816 emit_insn (gen_rtx_CLOBBER (VOIDmode, result));
9817 emit_insn (gen_subc (result, result, result));
9818 emit_insn (gen_addsi3 (result, result, const1_rtx));
9820 else if (code == EQ || code == NE)
9821 emit_insn (gen_move_insn (result, GEN_INT (code == NE)));
9822 else
9823 return 0;
9824 if (result != target)
9825 emit_move_insn (target, result);
9826 return 1;
9829 /* INSN is an sfunc; return the rtx that describes the address used. */
9830 static rtx
9831 extract_sfunc_addr (rtx insn)
9833 rtx pattern, part = NULL_RTX;
9834 int len, i;
9836 pattern = PATTERN (insn);
9837 len = XVECLEN (pattern, 0);
9838 for (i = 0; i < len; i++)
9840 part = XVECEXP (pattern, 0, i);
9841 if (GET_CODE (part) == USE && GET_MODE (XEXP (part, 0)) == Pmode
9842 && GENERAL_REGISTER_P (true_regnum (XEXP (part, 0))))
9843 return XEXP (part, 0);
9845 if (GET_CODE (XVECEXP (pattern, 0, 0)) == UNSPEC_VOLATILE)
9846 return XVECEXP (XVECEXP (pattern, 0, 0), 0, 1);
9847 abort ();
9850 /* Verify that the register in use_sfunc_addr still agrees with the address
9851 used in the sfunc. This prevents fill_slots_from_thread from changing
9852 use_sfunc_addr.
9853 INSN is the use_sfunc_addr instruction, and REG is the register it
9854 guards. */
9856 check_use_sfunc_addr (rtx insn, rtx reg)
9858 /* Search for the sfunc. It should really come right after INSN. */
9859 while ((insn = NEXT_INSN (insn)))
9861 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
9862 break;
9863 if (! INSN_P (insn))
9864 continue;
9866 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
9867 insn = XVECEXP (PATTERN (insn), 0, 0);
9868 if (GET_CODE (PATTERN (insn)) != PARALLEL
9869 || get_attr_type (insn) != TYPE_SFUNC)
9870 continue;
9871 return rtx_equal_p (extract_sfunc_addr (insn), reg);
9873 abort ();
9876 /* Returns 1 if OP is a MEM that can be source of a simple move operation. */
9879 unaligned_load_operand (rtx op, enum machine_mode mode)
9881 rtx inside;
9883 if (GET_CODE (op) != MEM || GET_MODE (op) != mode)
9884 return 0;
9886 inside = XEXP (op, 0);
9888 if (GET_CODE (inside) == POST_INC)
9889 inside = XEXP (inside, 0);
9891 if (GET_CODE (inside) == REG)
9892 return 1;
9894 return 0;
9897 /* This function returns a constant rtx that represents pi / 2**15 in
9898 SFmode. it's used to scale SFmode angles, in radians, to a
9899 fixed-point signed 16.16-bit fraction of a full circle, i.e., 2*pi
9900 maps to 0x10000). */
9902 static GTY(()) rtx sh_fsca_sf2int_rtx;
9905 sh_fsca_sf2int (void)
9907 if (! sh_fsca_sf2int_rtx)
9909 REAL_VALUE_TYPE rv;
9911 real_from_string (&rv, "10430.378350470453");
9912 sh_fsca_sf2int_rtx = const_double_from_real_value (rv, SFmode);
9915 return sh_fsca_sf2int_rtx;
9918 /* This function returns a constant rtx that represents pi / 2**15 in
9919 DFmode. it's used to scale DFmode angles, in radians, to a
9920 fixed-point signed 16.16-bit fraction of a full circle, i.e., 2*pi
9921 maps to 0x10000). */
9923 static GTY(()) rtx sh_fsca_df2int_rtx;
9926 sh_fsca_df2int (void)
9928 if (! sh_fsca_df2int_rtx)
9930 REAL_VALUE_TYPE rv;
9932 real_from_string (&rv, "10430.378350470453");
9933 sh_fsca_df2int_rtx = const_double_from_real_value (rv, DFmode);
9936 return sh_fsca_df2int_rtx;
9939 /* This function returns a constant rtx that represents 2**15 / pi in
9940 SFmode. it's used to scale a fixed-point signed 16.16-bit fraction
9941 of a full circle back to a SFmode value, i.e., 0x10000 maps to
9942 2*pi). */
9944 static GTY(()) rtx sh_fsca_int2sf_rtx;
9947 sh_fsca_int2sf (void)
9949 if (! sh_fsca_int2sf_rtx)
9951 REAL_VALUE_TYPE rv;
9953 real_from_string (&rv, "9.587379924285257e-5");
9954 sh_fsca_int2sf_rtx = const_double_from_real_value (rv, SFmode);
9957 return sh_fsca_int2sf_rtx;
9959 #include "gt-sh.h"