Debian Codegraph @

gcc/config/sh/sh.cc

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