]> gcc.gnu.org Git - gcc.git/blame - gcc/config/arm/arm.c
cgraphunit.c (process_function_and_variable_attributes): Mark dllexport-ed function...
[gcc.git] / gcc / config / arm / arm.c
CommitLineData
b36ba79f 1/* Output routines for GCC for ARM.
f954388e 2 Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
d8d55ac0 3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
b3a796bc 4 Free Software Foundation, Inc.
cce8749e 5 Contributed by Pieter `Tiggr' Schoenmakers (rcpieter@win.tue.nl)
956d6950 6 and Martin Simmons (@harleqn.co.uk).
b36ba79f 7 More major hacks by Richard Earnshaw (rearnsha@arm.com).
cce8749e 8
4f448245 9 This file is part of GCC.
cce8749e 10
4f448245
NC
11 GCC is free software; you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published
2f83c7d6 13 by the Free Software Foundation; either version 3, or (at your
4f448245 14 option) any later version.
cce8749e 15
4f448245
NC
16 GCC is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
19 License for more details.
cce8749e 20
4f448245 21 You should have received a copy of the GNU General Public License
2f83c7d6
NC
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
f676971a 24
56636818 25#include "config.h"
43cffd11 26#include "system.h"
4977bab6
ZW
27#include "coretypes.h"
28#include "tm.h"
cce8749e 29#include "rtl.h"
d5b7b3ae 30#include "tree.h"
c7319d87 31#include "obstack.h"
cce8749e
CH
32#include "regs.h"
33#include "hard-reg-set.h"
cce8749e
CH
34#include "insn-config.h"
35#include "conditions.h"
cce8749e
CH
36#include "output.h"
37#include "insn-attr.h"
38#include "flags.h"
af48348a 39#include "reload.h"
49ad7cfa 40#include "function.h"
bee06f3d 41#include "expr.h"
e78d8e51 42#include "optabs.h"
718f9c0f 43#include "diagnostic-core.h"
ad076f4e 44#include "toplev.h"
aec3cfba 45#include "recog.h"
390b17c2 46#include "cgraph.h"
92a432f4 47#include "ggc.h"
d5b7b3ae 48#include "except.h"
39dabefd 49#include "c-family/c-pragma.h" /* ??? */
7b8b8ade 50#include "integrate.h"
c27ba912 51#include "tm_p.h"
672a6f42
NB
52#include "target.h"
53#include "target-def.h"
980e61bb 54#include "debug.h"
6e34d3a3 55#include "langhooks.h"
6fb5fa3c 56#include "df.h"
0fd8c3ad 57#include "intl.h"
353a58f7 58#include "libfuncs.h"
ec3728ad 59#include "params.h"
cce8749e 60
d5b7b3ae
RE
61/* Forward definitions of types. */
62typedef struct minipool_node Mnode;
63typedef struct minipool_fixup Mfix;
64
b76c3c4b
PB
65void (*arm_lang_output_object_attributes_hook)(void);
66
d5b7b3ae 67/* Forward function declarations. */
c2ed6cf8 68static bool arm_needs_doubleword_align (enum machine_mode, const_tree);
35596784 69static int arm_compute_static_chain_stack_bytes (void);
5848830f 70static arm_stack_offsets *arm_get_frame_offsets (void);
e32bac5b 71static void arm_add_gc_roots (void);
a406f566
MM
72static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
73 HOST_WIDE_INT, rtx, rtx, int, int);
e32bac5b
RE
74static unsigned bit_count (unsigned long);
75static int arm_address_register_rtx_p (rtx, int);
1e1ab407 76static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
5b3e6663
PB
77static int thumb2_legitimate_index_p (enum machine_mode, rtx, int);
78static int thumb1_base_register_rtx_p (rtx, enum machine_mode, int);
506d7b68
PB
79static rtx arm_legitimize_address (rtx, rtx, enum machine_mode);
80static rtx thumb_legitimize_address (rtx, rtx, enum machine_mode);
5b3e6663 81inline static int thumb1_index_register_rtx_p (rtx, int);
c6c3dba9 82static bool arm_legitimate_address_p (enum machine_mode, rtx, bool);
5848830f 83static int thumb_far_jump_used_p (void);
57934c39 84static bool thumb_force_lr_save (void);
e32bac5b 85static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
e32bac5b 86static rtx emit_sfm (int, int);
466e4b7a 87static unsigned arm_size_return_regs (void);
e32bac5b 88static bool arm_assemble_integer (rtx, unsigned int, int);
944442bb
NF
89static void arm_print_operand (FILE *, rtx, int);
90static void arm_print_operand_address (FILE *, rtx);
91static bool arm_print_operand_punct_valid_p (unsigned char code);
e32bac5b
RE
92static const char *fp_const_from_val (REAL_VALUE_TYPE *);
93static arm_cc get_arm_condition_code (rtx);
e32bac5b
RE
94static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
95static rtx is_jump_table (rtx);
96static const char *output_multi_immediate (rtx *, const char *, const char *,
97 int, HOST_WIDE_INT);
e32bac5b
RE
98static const char *shift_op (rtx, HOST_WIDE_INT *);
99static struct machine_function *arm_init_machine_status (void);
c9ca9b88 100static void thumb_exit (FILE *, int);
e32bac5b
RE
101static rtx is_jump_table (rtx);
102static HOST_WIDE_INT get_jump_table_size (rtx);
103static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
104static Mnode *add_minipool_forward_ref (Mfix *);
105static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
106static Mnode *add_minipool_backward_ref (Mfix *);
107static void assign_minipool_offsets (Mfix *);
108static void arm_print_value (FILE *, rtx);
109static void dump_minipool (rtx);
110static int arm_barrier_cost (rtx);
111static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
112static void push_minipool_barrier (rtx, HOST_WIDE_INT);
113static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
114 rtx);
115static void arm_reorg (void);
116static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
e32bac5b
RE
117static unsigned long arm_compute_save_reg0_reg12_mask (void);
118static unsigned long arm_compute_save_reg_mask (void);
119static unsigned long arm_isr_value (tree);
120static unsigned long arm_compute_func_type (void);
121static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
390b17c2 122static tree arm_handle_pcs_attribute (tree *, tree, tree, int, bool *);
e32bac5b 123static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
7bff66a7 124#if TARGET_DLLIMPORT_DECL_ATTRIBUTES
04fb56d5 125static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
7bff66a7 126#endif
e32bac5b
RE
127static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
128static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
5b3e6663 129static void thumb1_output_function_prologue (FILE *, HOST_WIDE_INT);
3101faab 130static int arm_comp_type_attributes (const_tree, const_tree);
e32bac5b
RE
131static void arm_set_default_type_attributes (tree);
132static int arm_adjust_cost (rtx, rtx, rtx, int);
e32bac5b
RE
133static int count_insns_for_constant (HOST_WIDE_INT, int);
134static int arm_get_strip_length (int);
135static bool arm_function_ok_for_sibcall (tree, tree);
390b17c2
RE
136static enum machine_mode arm_promote_function_mode (const_tree,
137 enum machine_mode, int *,
138 const_tree, int);
139static bool arm_return_in_memory (const_tree, const_tree);
140static rtx arm_function_value (const_tree, const_tree, bool);
7fc6a96b 141static rtx arm_libcall_value (enum machine_mode, const_rtx);
390b17c2 142
e32bac5b
RE
143static void arm_internal_label (FILE *, const char *, unsigned long);
144static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
145 tree);
2929029c 146static bool arm_have_conditional_execution (void);
d5a0a47b
RE
147static bool arm_rtx_costs_1 (rtx, enum rtx_code, int*, bool);
148static bool arm_size_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *);
149static bool arm_slowmul_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
150static bool arm_fastmul_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
151static bool arm_xscale_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
152static bool arm_9e_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
f40751dd
JH
153static bool arm_rtx_costs (rtx, int, int, int *, bool);
154static int arm_address_cost (rtx, bool);
e32bac5b
RE
155static bool arm_memory_load_p (rtx);
156static bool arm_cirrus_insn_p (rtx);
157static void cirrus_reorg (rtx);
5a9335ef 158static void arm_init_builtins (void);
5a9335ef
NC
159static void arm_init_iwmmxt_builtins (void);
160static rtx safe_vector_operand (rtx, enum machine_mode);
161static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
162static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
163static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
a406f566 164static void emit_constant_insn (rtx cond, rtx pattern);
d66437c5 165static rtx emit_set_insn (rtx, rtx);
78a52f11
RH
166static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
167 tree, bool);
9c6a2bee
NF
168static rtx arm_function_arg (CUMULATIVE_ARGS *, enum machine_mode,
169 const_tree, bool);
170static void arm_function_arg_advance (CUMULATIVE_ARGS *, enum machine_mode,
171 const_tree, bool);
c2ed6cf8 172static unsigned int arm_function_arg_boundary (enum machine_mode, const_tree);
390b17c2
RE
173static rtx aapcs_allocate_return_reg (enum machine_mode, const_tree,
174 const_tree);
175static int aapcs_select_return_coproc (const_tree, const_tree);
5a9335ef 176
7abc66b1 177#ifdef OBJECT_FORMAT_ELF
9403b7f7
RS
178static void arm_elf_asm_constructor (rtx, int) ATTRIBUTE_UNUSED;
179static void arm_elf_asm_destructor (rtx, int) ATTRIBUTE_UNUSED;
7abc66b1 180#endif
fb49053f 181#ifndef ARM_PE
e32bac5b 182static void arm_encode_section_info (tree, rtx, int);
fb49053f 183#endif
b12a00f1
RE
184
185static void arm_file_end (void);
6c6aa1af 186static void arm_file_start (void);
b12a00f1 187
1cc9f5f5
KH
188static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
189 tree, int *, int);
8cd5a4e0 190static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
586de218
KG
191 enum machine_mode, const_tree, bool);
192static bool arm_promote_prototypes (const_tree);
6b045785 193static bool arm_default_short_enums (void);
13c1cd82 194static bool arm_align_anon_bitfield (void);
586de218
KG
195static bool arm_return_in_msb (const_tree);
196static bool arm_must_pass_in_stack (enum machine_mode, const_tree);
23668cf7 197static bool arm_return_in_memory (const_tree, const_tree);
f0a0390e 198#if ARM_UNWIND_INFO
617a1b71
PB
199static void arm_unwind_emit (FILE *, rtx);
200static bool arm_output_ttype (rtx);
a68b5e52
RH
201static void arm_asm_emit_except_personality (rtx);
202static void arm_asm_init_sections (void);
617a1b71 203#endif
f0a0390e 204static enum unwind_info_type arm_except_unwind_info (void);
5b3e6663 205static void arm_dwarf_handle_frame_unspec (const char *, rtx, int);
854b8a40 206static rtx arm_dwarf_register_span (rtx);
c237e94a 207
4185ae53
PB
208static tree arm_cxx_guard_type (void);
209static bool arm_cxx_guard_mask_bit (void);
46e995e0
PB
210static tree arm_get_cookie_size (tree);
211static bool arm_cookie_has_size (void);
44d10c10 212static bool arm_cxx_cdtor_returns_this (void);
505970fc 213static bool arm_cxx_key_method_may_be_inline (void);
1e731102
MM
214static void arm_cxx_determine_class_data_visibility (tree);
215static bool arm_cxx_class_data_always_comdat (void);
9f62c3e3 216static bool arm_cxx_use_aeabi_atexit (void);
b3f8d95d 217static void arm_init_libfuncs (void);
07d8efe3
MM
218static tree arm_build_builtin_va_list (void);
219static void arm_expand_builtin_va_start (tree, rtx);
ae46a823 220static tree arm_gimplify_va_arg_expr (tree, tree, gimple_seq *, gimple_seq *);
c5387660 221static void arm_option_override (void);
c54c7322 222static bool arm_handle_option (size_t, const char *, int);
67e6ba46 223static void arm_target_help (void);
273a2526 224static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
d3585b76
DJ
225static bool arm_cannot_copy_insn_p (rtx);
226static bool arm_tls_symbol_p (rtx x);
bd4dc3cd 227static int arm_issue_rate (void);
afcc986d 228static void arm_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
ffda8a0d 229static bool arm_output_addr_const_extra (FILE *, rtx);
007e61c2 230static bool arm_allocate_stack_slots_for_args (void);
0fd8c3ad
SL
231static const char *arm_invalid_parameter_type (const_tree t);
232static const char *arm_invalid_return_type (const_tree t);
233static tree arm_promoted_type (const_tree t);
234static tree arm_convert_to_type (tree type, tree expr);
bdc4827b 235static bool arm_scalar_mode_supported_p (enum machine_mode);
b52b1749 236static bool arm_frame_pointer_required (void);
7b5cbb57 237static bool arm_can_eliminate (const int, const int);
0ef9304b
RH
238static void arm_asm_trampoline_template (FILE *);
239static void arm_trampoline_init (rtx, tree, rtx);
240static rtx arm_trampoline_adjust_address (rtx);
85c9bcd4 241static rtx arm_pic_static_addr (rtx orig, rtx reg);
b0c13111
RR
242static bool cortex_a9_sched_adjust_cost (rtx, rtx, rtx, int *);
243static bool xscale_sched_adjust_cost (rtx, rtx, rtx, int *);
cc4b5170 244static enum machine_mode arm_preferred_simd_mode (enum machine_mode);
d163e655 245static bool arm_class_likely_spilled_p (reg_class_t);
c452684d
JB
246static bool arm_vector_alignment_reachable (const_tree type, bool is_packed);
247static bool arm_builtin_support_vector_misalignment (enum machine_mode mode,
248 const_tree type,
249 int misalignment,
250 bool is_packed);
5efd84c5 251static void arm_conditional_register_usage (void);
b52b1749 252
5a82ecd9
ILT
253\f
254/* Table of machine attributes. */
255static const struct attribute_spec arm_attribute_table[] =
256{
257 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
258 /* Function calls made to this symbol must be done indirectly, because
259 it may lie outside of the 26 bit addressing range of a normal function
260 call. */
261 { "long_call", 0, 0, false, true, true, NULL },
262 /* Whereas these functions are always known to reside within the 26 bit
263 addressing range. */
264 { "short_call", 0, 0, false, true, true, NULL },
390b17c2
RE
265 /* Specify the procedure call conventions for a function. */
266 { "pcs", 1, 1, false, true, true, arm_handle_pcs_attribute },
5a82ecd9
ILT
267 /* Interrupt Service Routines have special prologue and epilogue requirements. */
268 { "isr", 0, 1, false, false, false, arm_handle_isr_attribute },
269 { "interrupt", 0, 1, false, false, false, arm_handle_isr_attribute },
270 { "naked", 0, 0, true, false, false, arm_handle_fndecl_attribute },
271#ifdef ARM_PE
272 /* ARM/PE has three new attributes:
273 interfacearm - ?
274 dllexport - for exporting a function/variable that will live in a dll
275 dllimport - for importing a function/variable from a dll
d3585b76 276
5a82ecd9
ILT
277 Microsoft allows multiple declspecs in one __declspec, separating
278 them with spaces. We do NOT support this. Instead, use __declspec
279 multiple times.
280 */
281 { "dllimport", 0, 0, true, false, false, NULL },
282 { "dllexport", 0, 0, true, false, false, NULL },
283 { "interfacearm", 0, 0, true, false, false, arm_handle_fndecl_attribute },
284#elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
285 { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
286 { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
287 { "notshared", 0, 0, false, true, false, arm_handle_notshared_attribute },
288#endif
289 { NULL, 0, 0, false, false, false, NULL }
290};
3020190e
JM
291
292/* Set default optimization options. */
293static const struct default_options arm_option_optimization_table[] =
294 {
295 /* Enable section anchors by default at -O1 or higher. */
296 { OPT_LEVELS_1_PLUS, OPT_fsection_anchors, NULL, 1 },
297 { OPT_LEVELS_1_PLUS, OPT_fomit_frame_pointer, NULL, 1 },
298 { OPT_LEVELS_NONE, 0, NULL, 0 }
299 };
672a6f42
NB
300\f
301/* Initialize the GCC target structure. */
b2ca3702 302#if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1d6e90ac 303#undef TARGET_MERGE_DECL_ATTRIBUTES
672a6f42
NB
304#define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
305#endif
f3bb6135 306
506d7b68
PB
307#undef TARGET_LEGITIMIZE_ADDRESS
308#define TARGET_LEGITIMIZE_ADDRESS arm_legitimize_address
309
1d6e90ac 310#undef TARGET_ATTRIBUTE_TABLE
91d231cb 311#define TARGET_ATTRIBUTE_TABLE arm_attribute_table
672a6f42 312
6c6aa1af
PB
313#undef TARGET_ASM_FILE_START
314#define TARGET_ASM_FILE_START arm_file_start
b12a00f1
RE
315#undef TARGET_ASM_FILE_END
316#define TARGET_ASM_FILE_END arm_file_end
317
1d6e90ac 318#undef TARGET_ASM_ALIGNED_SI_OP
301d03af 319#define TARGET_ASM_ALIGNED_SI_OP NULL
1d6e90ac 320#undef TARGET_ASM_INTEGER
301d03af 321#define TARGET_ASM_INTEGER arm_assemble_integer
301d03af 322
944442bb
NF
323#undef TARGET_PRINT_OPERAND
324#define TARGET_PRINT_OPERAND arm_print_operand
325#undef TARGET_PRINT_OPERAND_ADDRESS
326#define TARGET_PRINT_OPERAND_ADDRESS arm_print_operand_address
327#undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
328#define TARGET_PRINT_OPERAND_PUNCT_VALID_P arm_print_operand_punct_valid_p
329
ffda8a0d
AS
330#undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
331#define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA arm_output_addr_const_extra
332
1d6e90ac 333#undef TARGET_ASM_FUNCTION_PROLOGUE
08c148a8
NB
334#define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
335
1d6e90ac 336#undef TARGET_ASM_FUNCTION_EPILOGUE
08c148a8
NB
337#define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
338
c54c7322
RS
339#undef TARGET_DEFAULT_TARGET_FLAGS
340#define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
341#undef TARGET_HANDLE_OPTION
342#define TARGET_HANDLE_OPTION arm_handle_option
67e6ba46
NC
343#undef TARGET_HELP
344#define TARGET_HELP arm_target_help
c5387660
JM
345#undef TARGET_OPTION_OVERRIDE
346#define TARGET_OPTION_OVERRIDE arm_option_override
3020190e
JM
347#undef TARGET_OPTION_OPTIMIZATION_TABLE
348#define TARGET_OPTION_OPTIMIZATION_TABLE arm_option_optimization_table
c54c7322 349
1d6e90ac 350#undef TARGET_COMP_TYPE_ATTRIBUTES
8d8e52be
JM
351#define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
352
1d6e90ac 353#undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
8d8e52be
JM
354#define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
355
1d6e90ac 356#undef TARGET_SCHED_ADJUST_COST
c237e94a
ZW
357#define TARGET_SCHED_ADJUST_COST arm_adjust_cost
358
fb49053f
RH
359#undef TARGET_ENCODE_SECTION_INFO
360#ifdef ARM_PE
361#define TARGET_ENCODE_SECTION_INFO arm_pe_encode_section_info
362#else
363#define TARGET_ENCODE_SECTION_INFO arm_encode_section_info
364#endif
365
5a9335ef 366#undef TARGET_STRIP_NAME_ENCODING
772c5265
RH
367#define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
368
5a9335ef 369#undef TARGET_ASM_INTERNAL_LABEL
4977bab6
ZW
370#define TARGET_ASM_INTERNAL_LABEL arm_internal_label
371
5a9335ef 372#undef TARGET_FUNCTION_OK_FOR_SIBCALL
4977bab6
ZW
373#define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
374
390b17c2
RE
375#undef TARGET_FUNCTION_VALUE
376#define TARGET_FUNCTION_VALUE arm_function_value
377
378#undef TARGET_LIBCALL_VALUE
379#define TARGET_LIBCALL_VALUE arm_libcall_value
380
5a9335ef 381#undef TARGET_ASM_OUTPUT_MI_THUNK
c590b625 382#define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
5a9335ef 383#undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
3961e8fe 384#define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
c590b625 385
5a9335ef 386#undef TARGET_RTX_COSTS
f40751dd 387#define TARGET_RTX_COSTS arm_rtx_costs
5a9335ef 388#undef TARGET_ADDRESS_COST
dcefdf67 389#define TARGET_ADDRESS_COST arm_address_cost
3c50106f 390
273a2526
RS
391#undef TARGET_SHIFT_TRUNCATION_MASK
392#define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
f676971a
EC
393#undef TARGET_VECTOR_MODE_SUPPORTED_P
394#define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
cc4b5170
RG
395#undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
396#define TARGET_VECTORIZE_PREFERRED_SIMD_MODE arm_preferred_simd_mode
f676971a 397
5a9335ef 398#undef TARGET_MACHINE_DEPENDENT_REORG
18dbd950
RS
399#define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
400
5a9335ef
NC
401#undef TARGET_INIT_BUILTINS
402#define TARGET_INIT_BUILTINS arm_init_builtins
403#undef TARGET_EXPAND_BUILTIN
404#define TARGET_EXPAND_BUILTIN arm_expand_builtin
405
b3f8d95d
MM
406#undef TARGET_INIT_LIBFUNCS
407#define TARGET_INIT_LIBFUNCS arm_init_libfuncs
408
cde0f3fd
PB
409#undef TARGET_PROMOTE_FUNCTION_MODE
410#define TARGET_PROMOTE_FUNCTION_MODE arm_promote_function_mode
f9ba5949 411#undef TARGET_PROMOTE_PROTOTYPES
70301b45 412#define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
8cd5a4e0
RH
413#undef TARGET_PASS_BY_REFERENCE
414#define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
78a52f11
RH
415#undef TARGET_ARG_PARTIAL_BYTES
416#define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
9c6a2bee
NF
417#undef TARGET_FUNCTION_ARG
418#define TARGET_FUNCTION_ARG arm_function_arg
419#undef TARGET_FUNCTION_ARG_ADVANCE
420#define TARGET_FUNCTION_ARG_ADVANCE arm_function_arg_advance
c2ed6cf8
NF
421#undef TARGET_FUNCTION_ARG_BOUNDARY
422#define TARGET_FUNCTION_ARG_BOUNDARY arm_function_arg_boundary
f9ba5949 423
1cc9f5f5
KH
424#undef TARGET_SETUP_INCOMING_VARARGS
425#define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
426
007e61c2
PB
427#undef TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS
428#define TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS arm_allocate_stack_slots_for_args
429
0ef9304b
RH
430#undef TARGET_ASM_TRAMPOLINE_TEMPLATE
431#define TARGET_ASM_TRAMPOLINE_TEMPLATE arm_asm_trampoline_template
432#undef TARGET_TRAMPOLINE_INIT
433#define TARGET_TRAMPOLINE_INIT arm_trampoline_init
434#undef TARGET_TRAMPOLINE_ADJUST_ADDRESS
435#define TARGET_TRAMPOLINE_ADJUST_ADDRESS arm_trampoline_adjust_address
436
6b045785
PB
437#undef TARGET_DEFAULT_SHORT_ENUMS
438#define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
439
13c1cd82
PB
440#undef TARGET_ALIGN_ANON_BITFIELD
441#define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
442
c2a64439
PB
443#undef TARGET_NARROW_VOLATILE_BITFIELD
444#define TARGET_NARROW_VOLATILE_BITFIELD hook_bool_void_false
445
4185ae53
PB
446#undef TARGET_CXX_GUARD_TYPE
447#define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
448
449#undef TARGET_CXX_GUARD_MASK_BIT
450#define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
451
46e995e0
PB
452#undef TARGET_CXX_GET_COOKIE_SIZE
453#define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
454
455#undef TARGET_CXX_COOKIE_HAS_SIZE
456#define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
457
44d10c10
PB
458#undef TARGET_CXX_CDTOR_RETURNS_THIS
459#define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
460
505970fc
MM
461#undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
462#define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
463
9f62c3e3
PB
464#undef TARGET_CXX_USE_AEABI_ATEXIT
465#define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
466
1e731102
MM
467#undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
468#define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
469 arm_cxx_determine_class_data_visibility
470
471#undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
472#define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
505970fc 473
866af8a9
JB
474#undef TARGET_RETURN_IN_MSB
475#define TARGET_RETURN_IN_MSB arm_return_in_msb
476
23668cf7
CLT
477#undef TARGET_RETURN_IN_MEMORY
478#define TARGET_RETURN_IN_MEMORY arm_return_in_memory
479
866af8a9
JB
480#undef TARGET_MUST_PASS_IN_STACK
481#define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
482
f0a0390e 483#if ARM_UNWIND_INFO
38f8b050
JR
484#undef TARGET_ASM_UNWIND_EMIT
485#define TARGET_ASM_UNWIND_EMIT arm_unwind_emit
617a1b71
PB
486
487/* EABI unwinding tables use a different format for the typeinfo tables. */
488#undef TARGET_ASM_TTYPE
489#define TARGET_ASM_TTYPE arm_output_ttype
490
491#undef TARGET_ARM_EABI_UNWINDER
492#define TARGET_ARM_EABI_UNWINDER true
a68b5e52
RH
493
494#undef TARGET_ASM_EMIT_EXCEPT_PERSONALITY
495#define TARGET_ASM_EMIT_EXCEPT_PERSONALITY arm_asm_emit_except_personality
496
497#undef TARGET_ASM_INIT_SECTIONS
498#define TARGET_ASM_INIT_SECTIONS arm_asm_init_sections
f0a0390e
RH
499#endif /* ARM_UNWIND_INFO */
500
501#undef TARGET_EXCEPT_UNWIND_INFO
502#define TARGET_EXCEPT_UNWIND_INFO arm_except_unwind_info
617a1b71 503
5b3e6663
PB
504#undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
505#define TARGET_DWARF_HANDLE_FRAME_UNSPEC arm_dwarf_handle_frame_unspec
506
854b8a40
JB
507#undef TARGET_DWARF_REGISTER_SPAN
508#define TARGET_DWARF_REGISTER_SPAN arm_dwarf_register_span
509
d3585b76
DJ
510#undef TARGET_CANNOT_COPY_INSN_P
511#define TARGET_CANNOT_COPY_INSN_P arm_cannot_copy_insn_p
512
513#ifdef HAVE_AS_TLS
514#undef TARGET_HAVE_TLS
515#define TARGET_HAVE_TLS true
516#endif
517
2929029c
WG
518#undef TARGET_HAVE_CONDITIONAL_EXECUTION
519#define TARGET_HAVE_CONDITIONAL_EXECUTION arm_have_conditional_execution
520
d3585b76 521#undef TARGET_CANNOT_FORCE_CONST_MEM
8426b956 522#define TARGET_CANNOT_FORCE_CONST_MEM arm_cannot_force_const_mem
d3585b76 523
f67358da
PB
524#undef TARGET_MAX_ANCHOR_OFFSET
525#define TARGET_MAX_ANCHOR_OFFSET 4095
526
527/* The minimum is set such that the total size of the block
528 for a particular anchor is -4088 + 1 + 4095 bytes, which is
529 divisible by eight, ensuring natural spacing of anchors. */
530#undef TARGET_MIN_ANCHOR_OFFSET
531#define TARGET_MIN_ANCHOR_OFFSET -4088
532
bd4dc3cd
PB
533#undef TARGET_SCHED_ISSUE_RATE
534#define TARGET_SCHED_ISSUE_RATE arm_issue_rate
535
608063c3
JB
536#undef TARGET_MANGLE_TYPE
537#define TARGET_MANGLE_TYPE arm_mangle_type
538
07d8efe3
MM
539#undef TARGET_BUILD_BUILTIN_VA_LIST
540#define TARGET_BUILD_BUILTIN_VA_LIST arm_build_builtin_va_list
541#undef TARGET_EXPAND_BUILTIN_VA_START
542#define TARGET_EXPAND_BUILTIN_VA_START arm_expand_builtin_va_start
543#undef TARGET_GIMPLIFY_VA_ARG_EXPR
544#define TARGET_GIMPLIFY_VA_ARG_EXPR arm_gimplify_va_arg_expr
545
afcc986d
JM
546#ifdef HAVE_AS_TLS
547#undef TARGET_ASM_OUTPUT_DWARF_DTPREL
548#define TARGET_ASM_OUTPUT_DWARF_DTPREL arm_output_dwarf_dtprel
549#endif
550
c6c3dba9
PB
551#undef TARGET_LEGITIMATE_ADDRESS_P
552#define TARGET_LEGITIMATE_ADDRESS_P arm_legitimate_address_p
553
0fd8c3ad
SL
554#undef TARGET_INVALID_PARAMETER_TYPE
555#define TARGET_INVALID_PARAMETER_TYPE arm_invalid_parameter_type
556
557#undef TARGET_INVALID_RETURN_TYPE
558#define TARGET_INVALID_RETURN_TYPE arm_invalid_return_type
559
560#undef TARGET_PROMOTED_TYPE
561#define TARGET_PROMOTED_TYPE arm_promoted_type
562
563#undef TARGET_CONVERT_TO_TYPE
564#define TARGET_CONVERT_TO_TYPE arm_convert_to_type
565
bdc4827b
SL
566#undef TARGET_SCALAR_MODE_SUPPORTED_P
567#define TARGET_SCALAR_MODE_SUPPORTED_P arm_scalar_mode_supported_p
568
b52b1749
AS
569#undef TARGET_FRAME_POINTER_REQUIRED
570#define TARGET_FRAME_POINTER_REQUIRED arm_frame_pointer_required
571
7b5cbb57
AS
572#undef TARGET_CAN_ELIMINATE
573#define TARGET_CAN_ELIMINATE arm_can_eliminate
574
5efd84c5
NF
575#undef TARGET_CONDITIONAL_REGISTER_USAGE
576#define TARGET_CONDITIONAL_REGISTER_USAGE arm_conditional_register_usage
577
d163e655
AS
578#undef TARGET_CLASS_LIKELY_SPILLED_P
579#define TARGET_CLASS_LIKELY_SPILLED_P arm_class_likely_spilled_p
580
c452684d
JB
581#undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
582#define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE \
583 arm_vector_alignment_reachable
584
585#undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
586#define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT \
587 arm_builtin_support_vector_misalignment
588
f6897b10 589struct gcc_target targetm = TARGET_INITIALIZER;
672a6f42 590\f
c7319d87
RE
591/* Obstack for minipool constant handling. */
592static struct obstack minipool_obstack;
1d6e90ac 593static char * minipool_startobj;
c7319d87 594
1d6e90ac
NC
595/* The maximum number of insns skipped which
596 will be conditionalised if possible. */
c27ba912
DM
597static int max_insns_skipped = 5;
598
599extern FILE * asm_out_file;
600
6354dc9b 601/* True if we are currently building a constant table. */
13bd191d
PB
602int making_const_table;
603
9b66ebb1
PB
604/* The processor for which instructions should be scheduled. */
605enum processor_type arm_tune = arm_none;
606
1b78f575
RE
607/* The current tuning set. */
608const struct tune_params *current_tune;
609
9b66ebb1 610/* Which floating point hardware to schedule for. */
d79f3032
PB
611int arm_fpu_attr;
612
613/* Which floating popint hardware to use. */
614const struct arm_fpu_desc *arm_fpu_desc;
9b66ebb1
PB
615
616/* Whether to use floating point hardware. */
617enum float_abi_type arm_float_abi;
618
0fd8c3ad
SL
619/* Which __fp16 format to use. */
620enum arm_fp16_format_type arm_fp16_format;
621
5848830f
PB
622/* Which ABI to use. */
623enum arm_abi_type arm_abi;
624
d3585b76
DJ
625/* Which thread pointer model to use. */
626enum arm_tp_type target_thread_pointer = TP_AUTO;
627
b355a481 628/* Used to parse -mstructure_size_boundary command line option. */
723ae7c1 629int arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
b355a481 630
b12a00f1 631/* Used for Thumb call_via trampolines. */
57ecec57 632rtx thumb_call_via_label[14];
b12a00f1
RE
633static int thumb_call_reg_needed;
634
aec3cfba 635/* Bit values used to identify processor capabilities. */
62b10bbc 636#define FL_CO_PROC (1 << 0) /* Has external co-processor bus */
9b66ebb1 637#define FL_ARCH3M (1 << 1) /* Extended multiply */
62b10bbc
NC
638#define FL_MODE26 (1 << 2) /* 26-bit mode support */
639#define FL_MODE32 (1 << 3) /* 32-bit mode support */
640#define FL_ARCH4 (1 << 4) /* Architecture rel 4 */
641#define FL_ARCH5 (1 << 5) /* Architecture rel 5 */
642#define FL_THUMB (1 << 6) /* Thumb aware */
643#define FL_LDSCHED (1 << 7) /* Load scheduling necessary */
644#define FL_STRONG (1 << 8) /* StrongARM */
6bc82793 645#define FL_ARCH5E (1 << 9) /* DSP extensions to v5 */
d19fb8e3 646#define FL_XSCALE (1 << 10) /* XScale */
9b6b54e2 647#define FL_CIRRUS (1 << 11) /* Cirrus/DSP. */
9b66ebb1 648#define FL_ARCH6 (1 << 12) /* Architecture rel 6. Adds
81f9037c
MM
649 media instructions. */
650#define FL_VFPV2 (1 << 13) /* Vector Floating Point V2. */
abac3b49
RE
651#define FL_WBUF (1 << 14) /* Schedule for write buffer ops.
652 Note: ARM6 & 7 derivatives only. */
d3585b76 653#define FL_ARCH6K (1 << 15) /* Architecture rel 6 K extensions. */
5b3e6663
PB
654#define FL_THUMB2 (1 << 16) /* Thumb-2. */
655#define FL_NOTM (1 << 17) /* Instructions not present in the 'M'
656 profile. */
7a085dce 657#define FL_DIV (1 << 18) /* Hardware divide. */
f1adb0a9 658#define FL_VFPV3 (1 << 19) /* Vector Floating Point V3. */
88f77cba 659#define FL_NEON (1 << 20) /* Neon instructions. */
60bd3528
PB
660#define FL_ARCH7EM (1 << 21) /* Instructions present in the ARMv7E-M
661 architecture. */
029e79eb 662#define FL_ARCH7 (1 << 22) /* Architecture 7. */
aec3cfba 663
9b66ebb1
PB
664#define FL_IWMMXT (1 << 29) /* XScale v2 or "Intel Wireless MMX technology". */
665
12a0a4d4
PB
666/* Flags that only effect tuning, not available instructions. */
667#define FL_TUNE (FL_WBUF | FL_VFPV2 | FL_STRONG | FL_LDSCHED \
668 | FL_CO_PROC)
669
5b3e6663
PB
670#define FL_FOR_ARCH2 FL_NOTM
671#define FL_FOR_ARCH3 (FL_FOR_ARCH2 | FL_MODE32)
78011587
PB
672#define FL_FOR_ARCH3M (FL_FOR_ARCH3 | FL_ARCH3M)
673#define FL_FOR_ARCH4 (FL_FOR_ARCH3M | FL_ARCH4)
674#define FL_FOR_ARCH4T (FL_FOR_ARCH4 | FL_THUMB)
675#define FL_FOR_ARCH5 (FL_FOR_ARCH4 | FL_ARCH5)
676#define FL_FOR_ARCH5T (FL_FOR_ARCH5 | FL_THUMB)
677#define FL_FOR_ARCH5E (FL_FOR_ARCH5 | FL_ARCH5E)
678#define FL_FOR_ARCH5TE (FL_FOR_ARCH5E | FL_THUMB)
679#define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
680#define FL_FOR_ARCH6 (FL_FOR_ARCH5TE | FL_ARCH6)
681#define FL_FOR_ARCH6J FL_FOR_ARCH6
d3585b76 682#define FL_FOR_ARCH6K (FL_FOR_ARCH6 | FL_ARCH6K)
fa91adc6 683#define FL_FOR_ARCH6Z FL_FOR_ARCH6
d3585b76 684#define FL_FOR_ARCH6ZK FL_FOR_ARCH6K
5b3e6663 685#define FL_FOR_ARCH6T2 (FL_FOR_ARCH6 | FL_THUMB2)
bf98ec6c 686#define FL_FOR_ARCH6M (FL_FOR_ARCH6 & ~FL_NOTM)
029e79eb 687#define FL_FOR_ARCH7 ((FL_FOR_ARCH6T2 & ~FL_NOTM) | FL_ARCH7)
87d05b44 688#define FL_FOR_ARCH7A (FL_FOR_ARCH7 | FL_NOTM | FL_ARCH6K)
5b3e6663
PB
689#define FL_FOR_ARCH7R (FL_FOR_ARCH7A | FL_DIV)
690#define FL_FOR_ARCH7M (FL_FOR_ARCH7 | FL_DIV)
60bd3528 691#define FL_FOR_ARCH7EM (FL_FOR_ARCH7M | FL_ARCH7EM)
78011587 692
1d6e90ac
NC
693/* The bits in this mask specify which
694 instructions we are allowed to generate. */
0977774b 695static unsigned long insn_flags = 0;
d5b7b3ae 696
aec3cfba 697/* The bits in this mask specify which instruction scheduling options should
9b66ebb1 698 be used. */
0977774b 699static unsigned long tune_flags = 0;
aec3cfba
NC
700
701/* The following are used in the arm.md file as equivalents to bits
702 in the above two flag variables. */
703
9b66ebb1
PB
704/* Nonzero if this chip supports the ARM Architecture 3M extensions. */
705int arm_arch3m = 0;
2b835d68 706
6354dc9b 707/* Nonzero if this chip supports the ARM Architecture 4 extensions. */
2b835d68
RE
708int arm_arch4 = 0;
709
68d560d4
RE
710/* Nonzero if this chip supports the ARM Architecture 4t extensions. */
711int arm_arch4t = 0;
712
6354dc9b 713/* Nonzero if this chip supports the ARM Architecture 5 extensions. */
62b10bbc
NC
714int arm_arch5 = 0;
715
b15bca31
RE
716/* Nonzero if this chip supports the ARM Architecture 5E extensions. */
717int arm_arch5e = 0;
718
9b66ebb1
PB
719/* Nonzero if this chip supports the ARM Architecture 6 extensions. */
720int arm_arch6 = 0;
721
d3585b76
DJ
722/* Nonzero if this chip supports the ARM 6K extensions. */
723int arm_arch6k = 0;
724
029e79eb
MS
725/* Nonzero if this chip supports the ARM 7 extensions. */
726int arm_arch7 = 0;
727
5b3e6663
PB
728/* Nonzero if instructions not present in the 'M' profile can be used. */
729int arm_arch_notm = 0;
730
60bd3528
PB
731/* Nonzero if instructions present in ARMv7E-M can be used. */
732int arm_arch7em = 0;
733
aec3cfba 734/* Nonzero if this chip can benefit from load scheduling. */
f5a1b0d2
NC
735int arm_ld_sched = 0;
736
737/* Nonzero if this chip is a StrongARM. */
abac3b49 738int arm_tune_strongarm = 0;
f5a1b0d2 739
78011587
PB
740/* Nonzero if this chip is a Cirrus variant. */
741int arm_arch_cirrus = 0;
742
5a9335ef
NC
743/* Nonzero if this chip supports Intel Wireless MMX technology. */
744int arm_arch_iwmmxt = 0;
745
d19fb8e3 746/* Nonzero if this chip is an XScale. */
4b3c2e48
PB
747int arm_arch_xscale = 0;
748
749/* Nonzero if tuning for XScale */
750int arm_tune_xscale = 0;
d19fb8e3 751
e0b92319 752/* Nonzero if we want to tune for stores that access the write-buffer.
c5d34bb2 753 This typically means an ARM6 or ARM7 with MMU or MPU. */
abac3b49 754int arm_tune_wbuf = 0;
b111229a 755
7612f14d
PB
756/* Nonzero if tuning for Cortex-A9. */
757int arm_tune_cortex_a9 = 0;
758
0616531f
RE
759/* Nonzero if generating Thumb instructions. */
760int thumb_code = 0;
761
906668bb
BS
762/* Nonzero if generating Thumb-1 instructions. */
763int thumb1_code = 0;
764
2ad4dcf9 765/* Nonzero if we should define __THUMB_INTERWORK__ in the
f676971a 766 preprocessor.
2ad4dcf9
RE
767 XXX This is a bit of a hack, it's intended to help work around
768 problems in GLD which doesn't understand that armv5t code is
769 interworking clean. */
770int arm_cpp_interwork = 0;
771
5b3e6663
PB
772/* Nonzero if chip supports Thumb 2. */
773int arm_arch_thumb2;
774
775/* Nonzero if chip supports integer division instruction. */
776int arm_arch_hwdiv;
777
944442bb
NF
778/* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference,
779 we must report the mode of the memory reference from
780 TARGET_PRINT_OPERAND to TARGET_PRINT_OPERAND_ADDRESS. */
f3bb6135 781enum machine_mode output_memory_reference_mode;
cce8749e 782
32de079a 783/* The register number to be used for the PIC offset register. */
020a4035 784unsigned arm_pic_register = INVALID_REGNUM;
32de079a 785
aec3cfba
NC
786/* Set to 1 after arm_reorg has started. Reset to start at the start of
787 the next function. */
4b632bf1
RE
788static int after_arm_reorg = 0;
789
12ffc7d5 790enum arm_pcs arm_pcs_default;
390b17c2 791
cce8749e
CH
792/* For an explanation of these variables, see final_prescan_insn below. */
793int arm_ccfsm_state;
5b3e6663 794/* arm_current_cc is also used for Thumb-2 cond_exec blocks. */
84ed5e79 795enum arm_cond_code arm_current_cc;
906668bb 796
cce8749e
CH
797rtx arm_target_insn;
798int arm_target_label;
5b3e6663
PB
799/* The number of conditionally executed insns, including the current insn. */
800int arm_condexec_count = 0;
801/* A bitmask specifying the patterns for the IT block.
802 Zero means do not output an IT block before this insn. */
803int arm_condexec_mask = 0;
804/* The number of bits used in arm_condexec_mask. */
805int arm_condexec_masklen = 0;
9997d19d
RE
806
807/* The condition codes of the ARM, and the inverse function. */
1d6e90ac 808static const char * const arm_condition_codes[] =
9997d19d
RE
809{
810 "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
811 "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
812};
813
37119410
BS
814/* The register numbers in sequence, for passing to arm_gen_load_multiple. */
815int arm_regs_in_sequence[] =
816{
817 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
818};
819
5b3e6663 820#define ARM_LSL_NAME (TARGET_UNIFIED_ASM ? "lsl" : "asl")
f5a1b0d2 821#define streq(string1, string2) (strcmp (string1, string2) == 0)
5b3e6663
PB
822
823#define THUMB2_WORK_REGS (0xff & ~( (1 << THUMB_HARD_FRAME_POINTER_REGNUM) \
824 | (1 << SP_REGNUM) | (1 << PC_REGNUM) \
825 | (1 << PIC_OFFSET_TABLE_REGNUM)))
2b835d68 826\f
6354dc9b 827/* Initialization code. */
2b835d68 828
2b835d68
RE
829struct processors
830{
8b60264b 831 const char *const name;
9b66ebb1 832 enum processor_type core;
78011587 833 const char *arch;
0977774b 834 const unsigned long flags;
1b78f575
RE
835 const struct tune_params *const tune;
836};
837
838const struct tune_params arm_slowmul_tune =
839{
840 arm_slowmul_rtx_costs,
b0c13111 841 NULL,
1b78f575
RE
842 3
843};
844
845const struct tune_params arm_fastmul_tune =
846{
847 arm_fastmul_rtx_costs,
b0c13111 848 NULL,
1b78f575
RE
849 1
850};
851
852const struct tune_params arm_xscale_tune =
853{
854 arm_xscale_rtx_costs,
b0c13111 855 xscale_sched_adjust_cost,
1b78f575
RE
856 2
857};
858
859const struct tune_params arm_9e_tune =
860{
861 arm_9e_rtx_costs,
b0c13111 862 NULL,
1b78f575 863 1
2b835d68
RE
864};
865
b0c13111
RR
866const struct tune_params arm_cortex_a9_tune =
867{
868 arm_9e_rtx_costs,
869 cortex_a9_sched_adjust_cost,
870 1
871};
872
873
2b835d68
RE
874/* Not all of these give usefully different compilation alternatives,
875 but there is no simple way of generalizing them. */
8b60264b 876static const struct processors all_cores[] =
f5a1b0d2
NC
877{
878 /* ARM Cores */
d98a72fd 879#define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
12a0a4d4 880 {NAME, IDENT, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, &arm_##COSTS##_tune},
9b66ebb1
PB
881#include "arm-cores.def"
882#undef ARM_CORE
78011587 883 {NULL, arm_none, NULL, 0, NULL}
f5a1b0d2
NC
884};
885
8b60264b 886static const struct processors all_architectures[] =
2b835d68 887{
f5a1b0d2 888 /* ARM Architectures */
1b78f575 889 /* We don't specify tuning costs here as it will be figured out
9b66ebb1 890 from the core. */
f676971a 891
78011587
PB
892 {"armv2", arm2, "2", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
893 {"armv2a", arm2, "2", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
894 {"armv3", arm6, "3", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
895 {"armv3m", arm7m, "3M", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
896 {"armv4", arm7tdmi, "4", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
b111229a
RE
897 /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
898 implementations that support it, so we will leave it out for now. */
78011587
PB
899 {"armv4t", arm7tdmi, "4T", FL_CO_PROC | FL_FOR_ARCH4T, NULL},
900 {"armv5", arm10tdmi, "5", FL_CO_PROC | FL_FOR_ARCH5, NULL},
901 {"armv5t", arm10tdmi, "5T", FL_CO_PROC | FL_FOR_ARCH5T, NULL},
902 {"armv5e", arm1026ejs, "5E", FL_CO_PROC | FL_FOR_ARCH5E, NULL},
903 {"armv5te", arm1026ejs, "5TE", FL_CO_PROC | FL_FOR_ARCH5TE, NULL},
904 {"armv6", arm1136js, "6", FL_CO_PROC | FL_FOR_ARCH6, NULL},
905 {"armv6j", arm1136js, "6J", FL_CO_PROC | FL_FOR_ARCH6J, NULL},
fa91adc6
PB
906 {"armv6k", mpcore, "6K", FL_CO_PROC | FL_FOR_ARCH6K, NULL},
907 {"armv6z", arm1176jzs, "6Z", FL_CO_PROC | FL_FOR_ARCH6Z, NULL},
908 {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC | FL_FOR_ARCH6ZK, NULL},
5b3e6663 909 {"armv6t2", arm1156t2s, "6T2", FL_CO_PROC | FL_FOR_ARCH6T2, NULL},
bf98ec6c 910 {"armv6-m", cortexm1, "6M", FL_FOR_ARCH6M, NULL},
5b3e6663
PB
911 {"armv7", cortexa8, "7", FL_CO_PROC | FL_FOR_ARCH7, NULL},
912 {"armv7-a", cortexa8, "7A", FL_CO_PROC | FL_FOR_ARCH7A, NULL},
913 {"armv7-r", cortexr4, "7R", FL_CO_PROC | FL_FOR_ARCH7R, NULL},
914 {"armv7-m", cortexm3, "7M", FL_CO_PROC | FL_FOR_ARCH7M, NULL},
f6e47b26 915 {"armv7e-m", cortexm4, "7EM", FL_CO_PROC | FL_FOR_ARCH7EM, NULL},
78011587
PB
916 {"ep9312", ep9312, "4T", FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
917 {"iwmmxt", iwmmxt, "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
442dc742 918 {"iwmmxt2", iwmmxt2, "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
78011587 919 {NULL, arm_none, NULL, 0 , NULL}
f5a1b0d2
NC
920};
921
f5a1b0d2 922
12a0a4d4
PB
923/* These are populated as commandline arguments are processed, or NULL
924 if not specified. */
925static const struct processors *arm_selected_arch;
926static const struct processors *arm_selected_cpu;
927static const struct processors *arm_selected_tune;
78011587 928
afc0a4ba 929/* The name of the preprocessor macro to define for this architecture. */
78011587
PB
930
931char arm_arch_name[] = "__ARM_ARCH_0UNK__";
932
56f42830 933/* Available values for -mfpu=. */
9b66ebb1 934
d79f3032
PB
935static const struct arm_fpu_desc all_fpus[] =
936{
70dd156a
RN
937 {"fpa", ARM_FP_MODEL_FPA, 0, VFP_NONE, false, false},
938 {"fpe2", ARM_FP_MODEL_FPA, 2, VFP_NONE, false, false},
939 {"fpe3", ARM_FP_MODEL_FPA, 3, VFP_NONE, false, false},
940 {"maverick", ARM_FP_MODEL_MAVERICK, 0, VFP_NONE, false, false},
d79f3032
PB
941 {"vfp", ARM_FP_MODEL_VFP, 2, VFP_REG_D16, false, false},
942 {"vfpv3", ARM_FP_MODEL_VFP, 3, VFP_REG_D32, false, false},
e0dc3601 943 {"vfpv3-fp16", ARM_FP_MODEL_VFP, 3, VFP_REG_D32, false, true},
d79f3032 944 {"vfpv3-d16", ARM_FP_MODEL_VFP, 3, VFP_REG_D16, false, false},
e0dc3601
PB
945 {"vfpv3-d16-fp16", ARM_FP_MODEL_VFP, 3, VFP_REG_D16, false, true},
946 {"vfpv3xd", ARM_FP_MODEL_VFP, 3, VFP_REG_SINGLE, false, false},
947 {"vfpv3xd-fp16", ARM_FP_MODEL_VFP, 3, VFP_REG_SINGLE, false, true},
d79f3032
PB
948 {"neon", ARM_FP_MODEL_VFP, 3, VFP_REG_D32, true , false},
949 {"neon-fp16", ARM_FP_MODEL_VFP, 3, VFP_REG_D32, true , true },
6ed126e6
PB
950 {"vfpv4", ARM_FP_MODEL_VFP, 4, VFP_REG_D32, false, true},
951 {"vfpv4-d16", ARM_FP_MODEL_VFP, 4, VFP_REG_D16, false, true},
1abed66b 952 {"fpv4-sp-d16", ARM_FP_MODEL_VFP, 4, VFP_REG_SINGLE, false, true},
6ed126e6 953 {"neon-vfpv4", ARM_FP_MODEL_VFP, 4, VFP_REG_D32, true, true},
d79f3032
PB
954 /* Compatibility aliases. */
955 {"vfp3", ARM_FP_MODEL_VFP, 3, VFP_REG_D32, false, false},
9b66ebb1
PB
956};
957
958
959struct float_abi
960{
961 const char * name;
962 enum float_abi_type abi_type;
963};
964
965
966/* Available values for -mfloat-abi=. */
967
968static const struct float_abi all_float_abis[] =
969{
970 {"soft", ARM_FLOAT_ABI_SOFT},
971 {"softfp", ARM_FLOAT_ABI_SOFTFP},
972 {"hard", ARM_FLOAT_ABI_HARD}
973};
974
975
0fd8c3ad
SL
976struct fp16_format
977{
978 const char *name;
979 enum arm_fp16_format_type fp16_format_type;
980};
981
982
983/* Available values for -mfp16-format=. */
984
985static const struct fp16_format all_fp16_formats[] =
986{
987 {"none", ARM_FP16_FORMAT_NONE},
988 {"ieee", ARM_FP16_FORMAT_IEEE},
989 {"alternative", ARM_FP16_FORMAT_ALTERNATIVE}
990};
991
992
5848830f
PB
993struct abi_name
994{
995 const char *name;
996 enum arm_abi_type abi_type;
997};
998
999
1000/* Available values for -mabi=. */
1001
1002static const struct abi_name arm_all_abis[] =
1003{
1004 {"apcs-gnu", ARM_ABI_APCS},
1005 {"atpcs", ARM_ABI_ATPCS},
1006 {"aapcs", ARM_ABI_AAPCS},
077fc835
KH
1007 {"iwmmxt", ARM_ABI_IWMMXT},
1008 {"aapcs-linux", ARM_ABI_AAPCS_LINUX}
5848830f
PB
1009};
1010
d3585b76
DJ
1011/* Supported TLS relocations. */
1012
1013enum tls_reloc {
1014 TLS_GD32,
1015 TLS_LDM32,
1016 TLS_LDO32,
1017 TLS_IE32,
1018 TLS_LE32
1019};
1020
1b78f575
RE
1021/* The maximum number of insns to be used when loading a constant. */
1022inline static int
1023arm_constant_limit (bool size_p)
1024{
1025 return size_p ? 1 : current_tune->constant_limit;
1026}
1027
d66437c5
RE
1028/* Emit an insn that's a simple single-set. Both the operands must be known
1029 to be valid. */
1030inline static rtx
1031emit_set_insn (rtx x, rtx y)
1032{
1033 return emit_insn (gen_rtx_SET (VOIDmode, x, y));
1034}
1035
0977774b
JT
1036/* Return the number of bits set in VALUE. */
1037static unsigned
e32bac5b 1038bit_count (unsigned long value)
aec3cfba 1039{
d5b7b3ae 1040 unsigned long count = 0;
f676971a 1041
aec3cfba
NC
1042 while (value)
1043 {
0977774b
JT
1044 count++;
1045 value &= value - 1; /* Clear the least-significant set bit. */
aec3cfba
NC
1046 }
1047
1048 return count;
1049}
1050
c112cf2b 1051/* Set up library functions unique to ARM. */
b3f8d95d
MM
1052
1053static void
1054arm_init_libfuncs (void)
1055{
1056 /* There are no special library functions unless we are using the
1057 ARM BPABI. */
1058 if (!TARGET_BPABI)
1059 return;
1060
1061 /* The functions below are described in Section 4 of the "Run-Time
1062 ABI for the ARM architecture", Version 1.0. */
1063
1064 /* Double-precision floating-point arithmetic. Table 2. */
1065 set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
1066 set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
1067 set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
1068 set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
1069 set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
1070
c112cf2b 1071 /* Double-precision comparisons. Table 3. */
b3f8d95d
MM
1072 set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
1073 set_optab_libfunc (ne_optab, DFmode, NULL);
1074 set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
1075 set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
1076 set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
1077 set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
1078 set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
1079
1080 /* Single-precision floating-point arithmetic. Table 4. */
1081 set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
1082 set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
1083 set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
1084 set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
1085 set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
f676971a 1086
c112cf2b 1087 /* Single-precision comparisons. Table 5. */
b3f8d95d
MM
1088 set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
1089 set_optab_libfunc (ne_optab, SFmode, NULL);
1090 set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
1091 set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
1092 set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
1093 set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
1094 set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
1095
1096 /* Floating-point to integer conversions. Table 6. */
1097 set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
1098 set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
1099 set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
1100 set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
1101 set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
1102 set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
1103 set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
1104 set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
1105
1106 /* Conversions between floating types. Table 7. */
1107 set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
1108 set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
1109
c112cf2b 1110 /* Integer to floating-point conversions. Table 8. */
b3f8d95d
MM
1111 set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
1112 set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
1113 set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
1114 set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
1115 set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
1116 set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
1117 set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
1118 set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
1119
1120 /* Long long. Table 9. */
1121 set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
1122 set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
1123 set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
1124 set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
1125 set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
1126 set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
1127 set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
1128 set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
1129
1130 /* Integer (32/32->32) division. \S 4.3.1. */
1131 set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
1132 set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
1133
1134 /* The divmod functions are designed so that they can be used for
1135 plain division, even though they return both the quotient and the
1136 remainder. The quotient is returned in the usual location (i.e.,
1137 r0 for SImode, {r0, r1} for DImode), just as would be expected
1138 for an ordinary division routine. Because the AAPCS calling
1139 conventions specify that all of { r0, r1, r2, r3 } are
1140 callee-saved registers, there is no need to tell the compiler
1141 explicitly that those registers are clobbered by these
1142 routines. */
1143 set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
1144 set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
e993ba8f
DJ
1145
1146 /* For SImode division the ABI provides div-without-mod routines,
1147 which are faster. */
1148 set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
1149 set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
01c19d47
PB
1150
1151 /* We don't have mod libcalls. Fortunately gcc knows how to use the
1152 divmod libcalls instead. */
1153 set_optab_libfunc (smod_optab, DImode, NULL);
1154 set_optab_libfunc (umod_optab, DImode, NULL);
1155 set_optab_libfunc (smod_optab, SImode, NULL);
1156 set_optab_libfunc (umod_optab, SImode, NULL);
0fd8c3ad
SL
1157
1158 /* Half-precision float operations. The compiler handles all operations
1159 with NULL libfuncs by converting the SFmode. */
1160 switch (arm_fp16_format)
1161 {
1162 case ARM_FP16_FORMAT_IEEE:
1163 case ARM_FP16_FORMAT_ALTERNATIVE:
1164
1165 /* Conversions. */
1166 set_conv_libfunc (trunc_optab, HFmode, SFmode,
1167 (arm_fp16_format == ARM_FP16_FORMAT_IEEE
1168 ? "__gnu_f2h_ieee"
1169 : "__gnu_f2h_alternative"));
1170 set_conv_libfunc (sext_optab, SFmode, HFmode,
1171 (arm_fp16_format == ARM_FP16_FORMAT_IEEE
1172 ? "__gnu_h2f_ieee"
1173 : "__gnu_h2f_alternative"));
1174
1175 /* Arithmetic. */
1176 set_optab_libfunc (add_optab, HFmode, NULL);
1177 set_optab_libfunc (sdiv_optab, HFmode, NULL);
1178 set_optab_libfunc (smul_optab, HFmode, NULL);
1179 set_optab_libfunc (neg_optab, HFmode, NULL);
1180 set_optab_libfunc (sub_optab, HFmode, NULL);
1181
1182 /* Comparisons. */
1183 set_optab_libfunc (eq_optab, HFmode, NULL);
1184 set_optab_libfunc (ne_optab, HFmode, NULL);
1185 set_optab_libfunc (lt_optab, HFmode, NULL);
1186 set_optab_libfunc (le_optab, HFmode, NULL);
1187 set_optab_libfunc (ge_optab, HFmode, NULL);
1188 set_optab_libfunc (gt_optab, HFmode, NULL);
1189 set_optab_libfunc (unord_optab, HFmode, NULL);
1190 break;
1191
1192 default:
1193 break;
1194 }
353a58f7
AH
1195
1196 if (TARGET_AAPCS_BASED)
1197 synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
b3f8d95d
MM
1198}
1199
07d8efe3
MM
1200/* On AAPCS systems, this is the "struct __va_list". */
1201static GTY(()) tree va_list_type;
1202
1203/* Return the type to use as __builtin_va_list. */
1204static tree
1205arm_build_builtin_va_list (void)
1206{
1207 tree va_list_name;
1208 tree ap_field;
1209
1210 if (!TARGET_AAPCS_BASED)
1211 return std_build_builtin_va_list ();
1212
1213 /* AAPCS \S 7.1.4 requires that va_list be a typedef for a type
1214 defined as:
1215
1216 struct __va_list
1217 {
1218 void *__ap;
1219 };
1220
1221 The C Library ABI further reinforces this definition in \S
1222 4.1.
1223
1224 We must follow this definition exactly. The structure tag
1225 name is visible in C++ mangled names, and thus forms a part
1226 of the ABI. The field name may be used by people who
1227 #include <stdarg.h>. */
1228 /* Create the type. */
1229 va_list_type = lang_hooks.types.make_type (RECORD_TYPE);
1230 /* Give it the required name. */
4c4bde29
AH
1231 va_list_name = build_decl (BUILTINS_LOCATION,
1232 TYPE_DECL,
07d8efe3
MM
1233 get_identifier ("__va_list"),
1234 va_list_type);
1235 DECL_ARTIFICIAL (va_list_name) = 1;
1236 TYPE_NAME (va_list_type) = va_list_name;
56f01f0b 1237 TYPE_STUB_DECL (va_list_type) = va_list_name;
07d8efe3 1238 /* Create the __ap field. */
4c4bde29
AH
1239 ap_field = build_decl (BUILTINS_LOCATION,
1240 FIELD_DECL,
07d8efe3
MM
1241 get_identifier ("__ap"),
1242 ptr_type_node);
1243 DECL_ARTIFICIAL (ap_field) = 1;
1244 DECL_FIELD_CONTEXT (ap_field) = va_list_type;
1245 TYPE_FIELDS (va_list_type) = ap_field;
1246 /* Compute its layout. */
1247 layout_type (va_list_type);
1248
1249 return va_list_type;
1250}
1251
1252/* Return an expression of type "void *" pointing to the next
1253 available argument in a variable-argument list. VALIST is the
1254 user-level va_list object, of type __builtin_va_list. */
1255static tree
1256arm_extract_valist_ptr (tree valist)
1257{
1258 if (TREE_TYPE (valist) == error_mark_node)
1259 return error_mark_node;
1260
1261 /* On an AAPCS target, the pointer is stored within "struct
1262 va_list". */
1263 if (TARGET_AAPCS_BASED)
1264 {
1265 tree ap_field = TYPE_FIELDS (TREE_TYPE (valist));
1266 valist = build3 (COMPONENT_REF, TREE_TYPE (ap_field),
1267 valist, ap_field, NULL_TREE);
1268 }
1269
1270 return valist;
1271}
1272
1273/* Implement TARGET_EXPAND_BUILTIN_VA_START. */
1274static void
1275arm_expand_builtin_va_start (tree valist, rtx nextarg)
1276{
1277 valist = arm_extract_valist_ptr (valist);
1278 std_expand_builtin_va_start (valist, nextarg);
1279}
1280
1281/* Implement TARGET_GIMPLIFY_VA_ARG_EXPR. */
1282static tree
ae46a823
JM
1283arm_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
1284 gimple_seq *post_p)
07d8efe3
MM
1285{
1286 valist = arm_extract_valist_ptr (valist);
1287 return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
1288}
1289
12a0a4d4
PB
1290/* Lookup NAME in SEL. */
1291
1292static const struct processors *
1293arm_find_cpu (const char *name, const struct processors *sel, const char *desc)
1294{
1295 if (!(name && *name))
1296 return NULL;
1297
1298 for (; sel->name != NULL; sel++)
1299 {
1300 if (streq (name, sel->name))
1301 return sel;
1302 }
1303
1304 error ("bad value (%s) for %s switch", name, desc);
1305 return NULL;
1306}
1307
c54c7322
RS
1308/* Implement TARGET_HANDLE_OPTION. */
1309
1310static bool
1311arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
1312{
1313 switch (code)
1314 {
c54c7322 1315 case OPT_march_:
12a0a4d4 1316 arm_selected_arch = arm_find_cpu(arg, all_architectures, "-march");
c54c7322
RS
1317 return true;
1318
1319 case OPT_mcpu_:
12a0a4d4 1320 arm_selected_cpu = arm_find_cpu(arg, all_cores, "-mcpu");
c54c7322
RS
1321 return true;
1322
c54c7322
RS
1323 case OPT_mhard_float:
1324 target_float_abi_name = "hard";
1325 return true;
1326
c54c7322
RS
1327 case OPT_msoft_float:
1328 target_float_abi_name = "soft";
1329 return true;
1330
c54c7322 1331 case OPT_mtune_:
12a0a4d4 1332 arm_selected_tune = arm_find_cpu(arg, all_cores, "-mtune");
c54c7322
RS
1333 return true;
1334
1335 default:
1336 return true;
1337 }
1338}
1339
67e6ba46
NC
1340static void
1341arm_target_help (void)
1342{
1343 int i;
1344 static int columns = 0;
1345 int remaining;
1346
1347 /* If we have not done so already, obtain the desired maximum width of
1348 the output. Note - this is a duplication of the code at the start of
1349 gcc/opts.c:print_specific_help() - the two copies should probably be
1350 replaced by a single function. */
1351 if (columns == 0)
1352 {
1353 const char *p;
1354
1355 GET_ENVIRONMENT (p, "COLUMNS");
1356 if (p != NULL)
1357 {
1358 int value = atoi (p);
1359
1360 if (value > 0)
1361 columns = value;
1362 }
1363
1364 if (columns == 0)
1365 /* Use a reasonable default. */
1366 columns = 80;
1367 }
1368
1369 printf (" Known ARM CPUs (for use with the -mcpu= and -mtune= options):\n");
1370
1371 /* The - 2 is because we know that the last entry in the array is NULL. */
1372 i = ARRAY_SIZE (all_cores) - 2;
1373 gcc_assert (i > 0);
1374 printf (" %s", all_cores[i].name);
1375 remaining = columns - (strlen (all_cores[i].name) + 4);
1376 gcc_assert (remaining >= 0);
1377
1378 while (i--)
1379 {
1380 int len = strlen (all_cores[i].name);
1381
1382 if (remaining > len + 2)
1383 {
1384 printf (", %s", all_cores[i].name);
1385 remaining -= len + 2;
1386 }
1387 else
1388 {
1389 if (remaining > 0)
1390 printf (",");
1391 printf ("\n %s", all_cores[i].name);
1392 remaining = columns - (len + 4);
1393 }
1394 }
1395
1396 printf ("\n\n Known ARM architectures (for use with the -march= option):\n");
1397
1398 i = ARRAY_SIZE (all_architectures) - 2;
1399 gcc_assert (i > 0);
1400
1401 printf (" %s", all_architectures[i].name);
1402 remaining = columns - (strlen (all_architectures[i].name) + 4);
1403 gcc_assert (remaining >= 0);
1404
1405 while (i--)
1406 {
1407 int len = strlen (all_architectures[i].name);
1408
1409 if (remaining > len + 2)
1410 {
1411 printf (", %s", all_architectures[i].name);
1412 remaining -= len + 2;
1413 }
1414 else
1415 {
1416 if (remaining > 0)
1417 printf (",");
1418 printf ("\n %s", all_architectures[i].name);
1419 remaining = columns - (len + 4);
1420 }
1421 }
1422 printf ("\n");
1423
1424}
1425
c5387660
JM
1426/* Fix up any incompatible options that the user has specified. */
1427static void
1428arm_option_override (void)
2b835d68 1429{
ed4c4348 1430 unsigned i;
9b66ebb1 1431
c5387660
JM
1432#ifdef SUBTARGET_OVERRIDE_OPTIONS
1433 SUBTARGET_OVERRIDE_OPTIONS;
1434#endif
1435
12a0a4d4 1436 if (arm_selected_arch)
bd9c7e23 1437 {
12a0a4d4
PB
1438 if (arm_selected_cpu)
1439 {
1440 /* Check for conflict between mcpu and march. */
1441 if ((arm_selected_cpu->flags ^ arm_selected_arch->flags) & ~FL_TUNE)
1442 {
1443 warning (0, "switch -mcpu=%s conflicts with -march=%s switch",
1444 arm_selected_cpu->name, arm_selected_arch->name);
1445 /* -march wins for code generation.
1446 -mcpu wins for default tuning. */
1447 if (!arm_selected_tune)
1448 arm_selected_tune = arm_selected_cpu;
1449
1450 arm_selected_cpu = arm_selected_arch;
1451 }
1452 else
1453 /* -mcpu wins. */
1454 arm_selected_arch = NULL;
1455 }
1456 else
1457 /* Pick a CPU based on the architecture. */
1458 arm_selected_cpu = arm_selected_arch;
bd9c7e23 1459 }
f676971a 1460
f5a1b0d2 1461 /* If the user did not specify a processor, choose one for them. */
12a0a4d4 1462 if (!arm_selected_cpu)
f5a1b0d2 1463 {
8b60264b 1464 const struct processors * sel;
aec3cfba 1465 unsigned int sought;
aec3cfba 1466
12a0a4d4
PB
1467 arm_selected_cpu = &all_cores[TARGET_CPU_DEFAULT];
1468 if (!arm_selected_cpu->name)
78011587
PB
1469 {
1470#ifdef SUBTARGET_CPU_DEFAULT
1471 /* Use the subtarget default CPU if none was specified by
1472 configure. */
12a0a4d4 1473 arm_selected_cpu = &all_cores[SUBTARGET_CPU_DEFAULT];
78011587
PB
1474#endif
1475 /* Default to ARM6. */
fe7645b9 1476 if (!arm_selected_cpu->name)
12a0a4d4 1477 arm_selected_cpu = &all_cores[arm6];
78011587 1478 }
aec3cfba 1479
12a0a4d4 1480 sel = arm_selected_cpu;
aec3cfba 1481 insn_flags = sel->flags;
9b66ebb1 1482
aec3cfba
NC
1483 /* Now check to see if the user has specified some command line
1484 switch that require certain abilities from the cpu. */
1485 sought = 0;
f676971a 1486
d5b7b3ae 1487 if (TARGET_INTERWORK || TARGET_THUMB)
f5a1b0d2 1488 {
aec3cfba 1489 sought |= (FL_THUMB | FL_MODE32);
f676971a 1490
d5b7b3ae 1491 /* There are no ARM processors that support both APCS-26 and
aec3cfba
NC
1492 interworking. Therefore we force FL_MODE26 to be removed
1493 from insn_flags here (if it was set), so that the search
1494 below will always be able to find a compatible processor. */
5895f793 1495 insn_flags &= ~FL_MODE26;
f5a1b0d2 1496 }
f676971a 1497
aec3cfba 1498 if (sought != 0 && ((sought & insn_flags) != sought))
f5a1b0d2 1499 {
aec3cfba
NC
1500 /* Try to locate a CPU type that supports all of the abilities
1501 of the default CPU, plus the extra abilities requested by
1502 the user. */
5895f793 1503 for (sel = all_cores; sel->name != NULL; sel++)
aec3cfba 1504 if ((sel->flags & sought) == (sought | insn_flags))
f5a1b0d2
NC
1505 break;
1506
1507 if (sel->name == NULL)
aec3cfba 1508 {
0977774b 1509 unsigned current_bit_count = 0;
8b60264b 1510 const struct processors * best_fit = NULL;
f676971a 1511
aec3cfba
NC
1512 /* Ideally we would like to issue an error message here
1513 saying that it was not possible to find a CPU compatible
1514 with the default CPU, but which also supports the command
1515 line options specified by the programmer, and so they
1516 ought to use the -mcpu=<name> command line option to
1517 override the default CPU type.
1518
61f0ccff
RE
1519 If we cannot find a cpu that has both the
1520 characteristics of the default cpu and the given
1521 command line options we scan the array again looking
1522 for a best match. */
5895f793 1523 for (sel = all_cores; sel->name != NULL; sel++)
aec3cfba
NC
1524 if ((sel->flags & sought) == sought)
1525 {
0977774b 1526 unsigned count;
aec3cfba
NC
1527
1528 count = bit_count (sel->flags & insn_flags);
1529
1530 if (count >= current_bit_count)
1531 {
1532 best_fit = sel;
1533 current_bit_count = count;
1534 }
1535 }
f5a1b0d2 1536
e6d29d15
NS
1537 gcc_assert (best_fit);
1538 sel = best_fit;
aec3cfba
NC
1539 }
1540
12a0a4d4 1541 arm_selected_cpu = sel;
f5a1b0d2
NC
1542 }
1543 }
f676971a 1544
12a0a4d4
PB
1545 gcc_assert (arm_selected_cpu);
1546 /* The selected cpu may be an architecture, so lookup tuning by core ID. */
1547 if (!arm_selected_tune)
1548 arm_selected_tune = &all_cores[arm_selected_cpu->core];
1549
1550 sprintf (arm_arch_name, "__ARM_ARCH_%s__", arm_selected_cpu->arch);
1551 insn_flags = arm_selected_cpu->flags;
f676971a 1552
12a0a4d4
PB
1553 arm_tune = arm_selected_tune->core;
1554 tune_flags = arm_selected_tune->flags;
1555 current_tune = arm_selected_tune->tune;
e26053d1 1556
0fd8c3ad
SL
1557 if (target_fp16_format_name)
1558 {
1559 for (i = 0; i < ARRAY_SIZE (all_fp16_formats); i++)
1560 {
1561 if (streq (all_fp16_formats[i].name, target_fp16_format_name))
1562 {
1563 arm_fp16_format = all_fp16_formats[i].fp16_format_type;
1564 break;
1565 }
1566 }
1567 if (i == ARRAY_SIZE (all_fp16_formats))
1568 error ("invalid __fp16 format option: -mfp16-format=%s",
1569 target_fp16_format_name);
1570 }
1571 else
1572 arm_fp16_format = ARM_FP16_FORMAT_NONE;
1573
26272ba2
PB
1574 if (target_abi_name)
1575 {
1576 for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1577 {
1578 if (streq (arm_all_abis[i].name, target_abi_name))
1579 {
1580 arm_abi = arm_all_abis[i].abi_type;
1581 break;
1582 }
1583 }
1584 if (i == ARRAY_SIZE (arm_all_abis))
1585 error ("invalid ABI option: -mabi=%s", target_abi_name);
1586 }
1587 else
1588 arm_abi = ARM_DEFAULT_ABI;
1589
f5a1b0d2
NC
1590 /* Make sure that the processor choice does not conflict with any of the
1591 other command line choices. */
5b3e6663
PB
1592 if (TARGET_ARM && !(insn_flags & FL_NOTM))
1593 error ("target CPU does not support ARM mode");
1594
26272ba2
PB
1595 /* BPABI targets use linker tricks to allow interworking on cores
1596 without thumb support. */
1597 if (TARGET_INTERWORK && !((insn_flags & FL_THUMB) || TARGET_BPABI))
f5a1b0d2 1598 {
d4ee4d25 1599 warning (0, "target CPU does not support interworking" );
c54c7322 1600 target_flags &= ~MASK_INTERWORK;
f5a1b0d2 1601 }
f676971a 1602
d5b7b3ae
RE
1603 if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1604 {
d4ee4d25 1605 warning (0, "target CPU does not support THUMB instructions");
c54c7322 1606 target_flags &= ~MASK_THUMB;
d5b7b3ae
RE
1607 }
1608
1609 if (TARGET_APCS_FRAME && TARGET_THUMB)
1610 {
d4ee4d25 1611 /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
c54c7322 1612 target_flags &= ~MASK_APCS_FRAME;
d5b7b3ae 1613 }
d19fb8e3 1614
da8ce8be
RE
1615 /* Callee super interworking implies thumb interworking. Adding
1616 this to the flags here simplifies the logic elsewhere. */
1617 if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
4546a2ef 1618 target_flags |= MASK_INTERWORK;
da8ce8be 1619
d5b7b3ae
RE
1620 /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1621 from here where no function is being compiled currently. */
c54c7322 1622 if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
d4ee4d25 1623 warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
d5b7b3ae
RE
1624
1625 if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
d4ee4d25 1626 warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
d5b7b3ae 1627
5895f793 1628 if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
f5a1b0d2 1629 {
d4ee4d25 1630 warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
c54c7322 1631 target_flags |= MASK_APCS_FRAME;
f5a1b0d2 1632 }
f676971a 1633
2b835d68 1634 if (TARGET_POKE_FUNCTION_NAME)
c54c7322 1635 target_flags |= MASK_APCS_FRAME;
f676971a 1636
2b835d68 1637 if (TARGET_APCS_REENT && flag_pic)
400500c4 1638 error ("-fpic and -mapcs-reent are incompatible");
f676971a 1639
2b835d68 1640 if (TARGET_APCS_REENT)
d4ee4d25 1641 warning (0, "APCS reentrant code not supported. Ignored");
f676971a 1642
d5b7b3ae
RE
1643 /* If this target is normally configured to use APCS frames, warn if they
1644 are turned off and debugging is turned on. */
1645 if (TARGET_ARM
1646 && write_symbols != NO_DEBUG
5895f793 1647 && !TARGET_APCS_FRAME
c54c7322 1648 && (TARGET_DEFAULT & MASK_APCS_FRAME))
d4ee4d25 1649 warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
f676971a 1650
2b835d68 1651 if (TARGET_APCS_FLOAT)
d4ee4d25 1652 warning (0, "passing floating point arguments in fp regs not yet supported");
f676971a 1653
4912a07c 1654 /* Initialize boolean versions of the flags, for use in the arm.md file. */
9b66ebb1
PB
1655 arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1656 arm_arch4 = (insn_flags & FL_ARCH4) != 0;
68d560d4 1657 arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
9b66ebb1
PB
1658 arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1659 arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1660 arm_arch6 = (insn_flags & FL_ARCH6) != 0;
d3585b76 1661 arm_arch6k = (insn_flags & FL_ARCH6K) != 0;
5b3e6663 1662 arm_arch_notm = (insn_flags & FL_NOTM) != 0;
029e79eb 1663 arm_arch7 = (insn_flags & FL_ARCH7) != 0;
60bd3528 1664 arm_arch7em = (insn_flags & FL_ARCH7EM) != 0;
5b3e6663 1665 arm_arch_thumb2 = (insn_flags & FL_THUMB2) != 0;
9b66ebb1 1666 arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
78011587 1667 arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
9b66ebb1
PB
1668
1669 arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
abac3b49 1670 arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
906668bb
BS
1671 thumb_code = TARGET_ARM == 0;
1672 thumb1_code = TARGET_THUMB1 != 0;
abac3b49 1673 arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
9b66ebb1
PB
1674 arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1675 arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
5b3e6663 1676 arm_arch_hwdiv = (insn_flags & FL_DIV) != 0;
7612f14d 1677 arm_tune_cortex_a9 = (arm_tune == cortexa9) != 0;
5a9335ef 1678
f67358da
PB
1679 /* If we are not using the default (ARM mode) section anchor offset
1680 ranges, then set the correct ranges now. */
1681 if (TARGET_THUMB1)
1682 {
1683 /* Thumb-1 LDR instructions cannot have negative offsets.
1684 Permissible positive offset ranges are 5-bit (for byte loads),
1685 6-bit (for halfword loads), or 7-bit (for word loads).
1686 Empirical results suggest a 7-bit anchor range gives the best
1687 overall code size. */
1688 targetm.min_anchor_offset = 0;
1689 targetm.max_anchor_offset = 127;
1690 }
1691 else if (TARGET_THUMB2)
1692 {
1693 /* The minimum is set such that the total size of the block
1694 for a particular anchor is 248 + 1 + 4095 bytes, which is
1695 divisible by eight, ensuring natural spacing of anchors. */
1696 targetm.min_anchor_offset = -248;
1697 targetm.max_anchor_offset = 4095;
1698 }
1699
68d560d4
RE
1700 /* V5 code we generate is completely interworking capable, so we turn off
1701 TARGET_INTERWORK here to avoid many tests later on. */
2ad4dcf9
RE
1702
1703 /* XXX However, we must pass the right pre-processor defines to CPP
1704 or GLD can get confused. This is a hack. */
1705 if (TARGET_INTERWORK)
1706 arm_cpp_interwork = 1;
1707
68d560d4 1708 if (arm_arch5)
c54c7322 1709 target_flags &= ~MASK_INTERWORK;
68d560d4 1710
5848830f
PB
1711 if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1712 error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1713
1714 if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1715 error ("iwmmxt abi requires an iwmmxt capable cpu");
6f7ebcbb 1716
9b66ebb1 1717 if (target_fpu_name == NULL && target_fpe_name != NULL)
9b6b54e2 1718 {
9b66ebb1
PB
1719 if (streq (target_fpe_name, "2"))
1720 target_fpu_name = "fpe2";
1721 else if (streq (target_fpe_name, "3"))
1722 target_fpu_name = "fpe3";
1723 else
1724 error ("invalid floating point emulation option: -mfpe=%s",
1725 target_fpe_name);
1726 }
d79f3032
PB
1727
1728 if (target_fpu_name == NULL)
2b835d68 1729 {
9b66ebb1 1730#ifdef FPUTYPE_DEFAULT
d79f3032 1731 target_fpu_name = FPUTYPE_DEFAULT;
9b66ebb1 1732#else
78011587 1733 if (arm_arch_cirrus)
d79f3032 1734 target_fpu_name = "maverick";
9b66ebb1 1735 else
d79f3032 1736 target_fpu_name = "fpe2";
9b66ebb1 1737#endif
d79f3032
PB
1738 }
1739
1740 arm_fpu_desc = NULL;
1741 for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1742 {
1743 if (streq (all_fpus[i].name, target_fpu_name))
1744 {
1745 arm_fpu_desc = &all_fpus[i];
1746 break;
1747 }
1748 }
b761dbe6 1749
d79f3032 1750 if (!arm_fpu_desc)
b761dbe6
RE
1751 {
1752 error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1753 return;
1754 }
d79f3032
PB
1755
1756 switch (arm_fpu_desc->model)
1757 {
1758 case ARM_FP_MODEL_FPA:
1759 if (arm_fpu_desc->rev == 2)
1760 arm_fpu_attr = FPU_FPE2;
1761 else if (arm_fpu_desc->rev == 3)
1762 arm_fpu_attr = FPU_FPE3;
2b835d68 1763 else
d79f3032
PB
1764 arm_fpu_attr = FPU_FPA;
1765 break;
1766
1767 case ARM_FP_MODEL_MAVERICK:
1768 arm_fpu_attr = FPU_MAVERICK;
1769 break;
1770
1771 case ARM_FP_MODEL_VFP:
1772 arm_fpu_attr = FPU_VFP;
1773 break;
1774
1775 default:
1776 gcc_unreachable();
9b66ebb1
PB
1777 }
1778
1779 if (target_float_abi_name != NULL)
1780 {
1781 /* The user specified a FP ABI. */
1782 for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1783 {
1784 if (streq (all_float_abis[i].name, target_float_abi_name))
1785 {
1786 arm_float_abi = all_float_abis[i].abi_type;
1787 break;
1788 }
1789 }
1790 if (i == ARRAY_SIZE (all_float_abis))
1791 error ("invalid floating point abi: -mfloat-abi=%s",
1792 target_float_abi_name);
2b835d68 1793 }
3d8532aa
PB
1794 else
1795 arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
9b66ebb1 1796
0c48a567 1797 if (TARGET_AAPCS_BASED
d79f3032 1798 && (arm_fpu_desc->model == ARM_FP_MODEL_FPA))
0c48a567
RR
1799 error ("FPA is unsupported in the AAPCS");
1800
3ce14752 1801 if (TARGET_AAPCS_BASED)
9df5bfe4
RR
1802 {
1803 if (TARGET_CALLER_INTERWORKING)
1804 error ("AAPCS does not support -mcaller-super-interworking");
1805 else
1806 if (TARGET_CALLEE_INTERWORKING)
1807 error ("AAPCS does not support -mcallee-super-interworking");
1808 }
3ce14752 1809
87b24aaf
PB
1810 /* FPA and iWMMXt are incompatible because the insn encodings overlap.
1811 VFP and iWMMXt can theoretically coexist, but it's unlikely such silicon
1812 will ever exist. GCC makes no attempt to support this combination. */
1813 if (TARGET_IWMMXT && !TARGET_SOFT_FLOAT)
1814 sorry ("iWMMXt and hardware floating point");
1815
5b3e6663
PB
1816 /* ??? iWMMXt insn patterns need auditing for Thumb-2. */
1817 if (TARGET_THUMB2 && TARGET_IWMMXT)
1818 sorry ("Thumb-2 iWMMXt");
1819
0fd8c3ad
SL
1820 /* __fp16 support currently assumes the core has ldrh. */
1821 if (!arm_arch4 && arm_fp16_format != ARM_FP16_FORMAT_NONE)
1822 sorry ("__fp16 and no ldrh");
1823
9b66ebb1
PB
1824 /* If soft-float is specified then don't use FPU. */
1825 if (TARGET_SOFT_FLOAT)
d79f3032 1826 arm_fpu_attr = FPU_NONE;
f676971a 1827
390b17c2
RE
1828 if (TARGET_AAPCS_BASED)
1829 {
1830 if (arm_abi == ARM_ABI_IWMMXT)
1831 arm_pcs_default = ARM_PCS_AAPCS_IWMMXT;
1832 else if (arm_float_abi == ARM_FLOAT_ABI_HARD
1833 && TARGET_HARD_FLOAT
1834 && TARGET_VFP)
1835 arm_pcs_default = ARM_PCS_AAPCS_VFP;
1836 else
1837 arm_pcs_default = ARM_PCS_AAPCS;
1838 }
1839 else
1840 {
1841 if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1842 sorry ("-mfloat-abi=hard and VFP");
1843
1844 if (arm_abi == ARM_ABI_APCS)
1845 arm_pcs_default = ARM_PCS_APCS;
1846 else
1847 arm_pcs_default = ARM_PCS_ATPCS;
1848 }
1849
f5a1b0d2
NC
1850 /* For arm2/3 there is no need to do any scheduling if there is only
1851 a floating point emulator, or we are doing software floating-point. */
9b66ebb1 1852 if ((TARGET_SOFT_FLOAT
d79f3032 1853 || (TARGET_FPA && arm_fpu_desc->rev))
ed0e6530 1854 && (tune_flags & FL_MODE32) == 0)
f5a1b0d2 1855 flag_schedule_insns = flag_schedule_insns_after_reload = 0;
f676971a 1856
d3585b76
DJ
1857 if (target_thread_switch)
1858 {
1859 if (strcmp (target_thread_switch, "soft") == 0)
1860 target_thread_pointer = TP_SOFT;
1861 else if (strcmp (target_thread_switch, "auto") == 0)
1862 target_thread_pointer = TP_AUTO;
1863 else if (strcmp (target_thread_switch, "cp15") == 0)
1864 target_thread_pointer = TP_CP15;
1865 else
1866 error ("invalid thread pointer option: -mtp=%s", target_thread_switch);
1867 }
1868
1869 /* Use the cp15 method if it is available. */
1870 if (target_thread_pointer == TP_AUTO)
1871 {
87d05b44 1872 if (arm_arch6k && !TARGET_THUMB1)
d3585b76
DJ
1873 target_thread_pointer = TP_CP15;
1874 else
1875 target_thread_pointer = TP_SOFT;
1876 }
1877
5b3e6663
PB
1878 if (TARGET_HARD_TP && TARGET_THUMB1)
1879 error ("can not use -mtp=cp15 with 16-bit Thumb");
d3585b76 1880
5848830f 1881 /* Override the default structure alignment for AAPCS ABI. */
077fc835 1882 if (TARGET_AAPCS_BASED)
5848830f
PB
1883 arm_structure_size_boundary = 8;
1884
b355a481
NC
1885 if (structure_size_string != NULL)
1886 {
1887 int size = strtol (structure_size_string, NULL, 0);
5848830f
PB
1888
1889 if (size == 8 || size == 32
1890 || (ARM_DOUBLEWORD_ALIGN && size == 64))
b355a481
NC
1891 arm_structure_size_boundary = size;
1892 else
d4ee4d25 1893 warning (0, "structure size boundary can only be set to %s",
5848830f 1894 ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
b355a481 1895 }
ed0e6530 1896
9403b7f7
RS
1897 if (!TARGET_ARM && TARGET_VXWORKS_RTP && flag_pic)
1898 {
1899 error ("RTP PIC is incompatible with Thumb");
1900 flag_pic = 0;
1901 }
1902
c147eacb
PB
1903 /* If stack checking is disabled, we can use r10 as the PIC register,
1904 which keeps r9 available. The EABI specifies r9 as the PIC register. */
1905 if (flag_pic && TARGET_SINGLE_PIC_BASE)
9403b7f7
RS
1906 {
1907 if (TARGET_VXWORKS_RTP)
1908 warning (0, "RTP PIC is incompatible with -msingle-pic-base");
1909 arm_pic_register = (TARGET_APCS_STACK || TARGET_AAPCS_BASED) ? 9 : 10;
1910 }
1911
1912 if (flag_pic && TARGET_VXWORKS_RTP)
1913 arm_pic_register = 9;
c147eacb 1914
ed0e6530
PB
1915 if (arm_pic_register_string != NULL)
1916 {
5b43fed1 1917 int pic_register = decode_reg_name (arm_pic_register_string);
e26053d1 1918
5895f793 1919 if (!flag_pic)
d4ee4d25 1920 warning (0, "-mpic-register= is useless without -fpic");
ed0e6530 1921
ed0e6530 1922 /* Prevent the user from choosing an obviously stupid PIC register. */
5b43fed1
RH
1923 else if (pic_register < 0 || call_used_regs[pic_register]
1924 || pic_register == HARD_FRAME_POINTER_REGNUM
1925 || pic_register == STACK_POINTER_REGNUM
9403b7f7
RS
1926 || pic_register >= PC_REGNUM
1927 || (TARGET_VXWORKS_RTP
1928 && (unsigned int) pic_register != arm_pic_register))
c725bd79 1929 error ("unable to use '%s' for PIC register", arm_pic_register_string);
ed0e6530
PB
1930 else
1931 arm_pic_register = pic_register;
1932 }
d5b7b3ae 1933
5fd42423
PB
1934 /* Enable -mfix-cortex-m3-ldrd by default for Cortex-M3 cores. */
1935 if (fix_cm3_ldrd == 2)
1936 {
12a0a4d4 1937 if (arm_selected_cpu->core == cortexm3)
5fd42423
PB
1938 fix_cm3_ldrd = 1;
1939 else
1940 fix_cm3_ldrd = 0;
1941 }
1942
4aef21c8 1943 if (TARGET_THUMB1 && flag_schedule_insns)
d5b7b3ae
RE
1944 {
1945 /* Don't warn since it's on by default in -O2. */
1946 flag_schedule_insns = 0;
1947 }
1948
f5a1b0d2 1949 if (optimize_size)
be03ccc9 1950 {
be03ccc9 1951 /* If optimizing for size, bump the number of instructions that we
d6b4baa4 1952 are prepared to conditionally execute (even on a StrongARM). */
be03ccc9
NP
1953 max_insns_skipped = 6;
1954 }
1955 else
1956 {
be03ccc9
NP
1957 /* StrongARM has early execution of branches, so a sequence
1958 that is worth skipping is shorter. */
abac3b49 1959 if (arm_tune_strongarm)
be03ccc9
NP
1960 max_insns_skipped = 3;
1961 }
92a432f4 1962
70041f8a
RE
1963 /* Hot/Cold partitioning is not currently supported, since we can't
1964 handle literal pool placement in that case. */
1965 if (flag_reorder_blocks_and_partition)
1966 {
1967 inform (input_location,
1968 "-freorder-blocks-and-partition not supported on this architecture");
1969 flag_reorder_blocks_and_partition = 0;
1970 flag_reorder_blocks = 1;
1971 }
1972
128dc8e2 1973 if (flag_pic)
ec3728ad
MK
1974 /* Hoisting PIC address calculations more aggressively provides a small,
1975 but measurable, size reduction for PIC code. Therefore, we decrease
1976 the bar for unrestricted expression hoisting to the cost of PIC address
1977 calculation, which is 2 instructions. */
48476d13
JM
1978 maybe_set_param_value (PARAM_GCSE_UNRESTRICTED_COST, 2,
1979 global_options.x_param_values,
1980 global_options_set.x_param_values);
ec3728ad 1981
f5c88dbf
JZ
1982 /* ARM EABI defaults to strict volatile bitfields. */
1983 if (TARGET_AAPCS_BASED && flag_strict_volatile_bitfields < 0)
1984 flag_strict_volatile_bitfields = 1;
1985
92a432f4
RE
1986 /* Register global variables with the garbage collector. */
1987 arm_add_gc_roots ();
1988}
1989
1990static void
e32bac5b 1991arm_add_gc_roots (void)
92a432f4 1992{
c7319d87
RE
1993 gcc_obstack_init(&minipool_obstack);
1994 minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
2b835d68 1995}
cce8749e 1996\f
6d3d9133
NC
1997/* A table of known ARM exception types.
1998 For use with the interrupt function attribute. */
1999
2000typedef struct
2001{
8b60264b
KG
2002 const char *const arg;
2003 const unsigned long return_value;
6d3d9133
NC
2004}
2005isr_attribute_arg;
2006
8b60264b 2007static const isr_attribute_arg isr_attribute_args [] =
6d3d9133
NC
2008{
2009 { "IRQ", ARM_FT_ISR },
2010 { "irq", ARM_FT_ISR },
2011 { "FIQ", ARM_FT_FIQ },
2012 { "fiq", ARM_FT_FIQ },
2013 { "ABORT", ARM_FT_ISR },
2014 { "abort", ARM_FT_ISR },
2015 { "ABORT", ARM_FT_ISR },
2016 { "abort", ARM_FT_ISR },
2017 { "UNDEF", ARM_FT_EXCEPTION },
2018 { "undef", ARM_FT_EXCEPTION },
2019 { "SWI", ARM_FT_EXCEPTION },
2020 { "swi", ARM_FT_EXCEPTION },
2021 { NULL, ARM_FT_NORMAL }
2022};
2023
2024/* Returns the (interrupt) function type of the current
2025 function, or ARM_FT_UNKNOWN if the type cannot be determined. */
2026
2027static unsigned long
e32bac5b 2028arm_isr_value (tree argument)
6d3d9133 2029{
8b60264b 2030 const isr_attribute_arg * ptr;
1d6e90ac 2031 const char * arg;
6d3d9133 2032
5b3e6663
PB
2033 if (!arm_arch_notm)
2034 return ARM_FT_NORMAL | ARM_FT_STACKALIGN;
2035
6d3d9133
NC
2036 /* No argument - default to IRQ. */
2037 if (argument == NULL_TREE)
2038 return ARM_FT_ISR;
2039
2040 /* Get the value of the argument. */
2041 if (TREE_VALUE (argument) == NULL_TREE
2042 || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
2043 return ARM_FT_UNKNOWN;
2044
2045 arg = TREE_STRING_POINTER (TREE_VALUE (argument));
2046
2047 /* Check it against the list of known arguments. */
5a9335ef 2048 for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1d6e90ac
NC
2049 if (streq (arg, ptr->arg))
2050 return ptr->return_value;
6d3d9133 2051
05713b80 2052 /* An unrecognized interrupt type. */
6d3d9133
NC
2053 return ARM_FT_UNKNOWN;
2054}
2055
2056/* Computes the type of the current function. */
2057
2058static unsigned long
e32bac5b 2059arm_compute_func_type (void)
6d3d9133
NC
2060{
2061 unsigned long type = ARM_FT_UNKNOWN;
2062 tree a;
2063 tree attr;
f676971a 2064
e6d29d15 2065 gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
6d3d9133
NC
2066
2067 /* Decide if the current function is volatile. Such functions
2068 never return, and many memory cycles can be saved by not storing
2069 register values that will never be needed again. This optimization
2070 was added to speed up context switching in a kernel application. */
2071 if (optimize > 0
cf1955dc
PB
2072 && (TREE_NOTHROW (current_function_decl)
2073 || !(flag_unwind_tables
f0a0390e 2074 || (flag_exceptions && arm_except_unwind_info () != UI_SJLJ)))
6d3d9133
NC
2075 && TREE_THIS_VOLATILE (current_function_decl))
2076 type |= ARM_FT_VOLATILE;
f676971a 2077
6de9cd9a 2078 if (cfun->static_chain_decl != NULL)
6d3d9133
NC
2079 type |= ARM_FT_NESTED;
2080
91d231cb 2081 attr = DECL_ATTRIBUTES (current_function_decl);
f676971a 2082
6d3d9133
NC
2083 a = lookup_attribute ("naked", attr);
2084 if (a != NULL_TREE)
2085 type |= ARM_FT_NAKED;
2086
c9ca9b88
PB
2087 a = lookup_attribute ("isr", attr);
2088 if (a == NULL_TREE)
2089 a = lookup_attribute ("interrupt", attr);
f676971a 2090
c9ca9b88
PB
2091 if (a == NULL_TREE)
2092 type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
6d3d9133 2093 else
c9ca9b88 2094 type |= arm_isr_value (TREE_VALUE (a));
f676971a 2095
6d3d9133
NC
2096 return type;
2097}
2098
2099/* Returns the type of the current function. */
2100
2101unsigned long
e32bac5b 2102arm_current_func_type (void)
6d3d9133
NC
2103{
2104 if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
2105 cfun->machine->func_type = arm_compute_func_type ();
2106
2107 return cfun->machine->func_type;
2108}
007e61c2
PB
2109
2110bool
2111arm_allocate_stack_slots_for_args (void)
2112{
2113 /* Naked functions should not allocate stack slots for arguments. */
2114 return !IS_NAKED (arm_current_func_type ());
2115}
2116
0ef9304b
RH
2117\f
2118/* Output assembler code for a block containing the constant parts
2119 of a trampoline, leaving space for the variable parts.
2120
2121 On the ARM, (if r8 is the static chain regnum, and remembering that
2122 referencing pc adds an offset of 8) the trampoline looks like:
2123 ldr r8, [pc, #0]
2124 ldr pc, [pc]
2125 .word static chain value
2126 .word function's address
2127 XXX FIXME: When the trampoline returns, r8 will be clobbered. */
2128
2129static void
2130arm_asm_trampoline_template (FILE *f)
2131{
2132 if (TARGET_ARM)
2133 {
2134 asm_fprintf (f, "\tldr\t%r, [%r, #0]\n", STATIC_CHAIN_REGNUM, PC_REGNUM);
2135 asm_fprintf (f, "\tldr\t%r, [%r, #0]\n", PC_REGNUM, PC_REGNUM);
2136 }
2137 else if (TARGET_THUMB2)
2138 {
2139 /* The Thumb-2 trampoline is similar to the arm implementation.
2140 Unlike 16-bit Thumb, we enter the stub in thumb mode. */
2141 asm_fprintf (f, "\tldr.w\t%r, [%r, #4]\n",
2142 STATIC_CHAIN_REGNUM, PC_REGNUM);
2143 asm_fprintf (f, "\tldr.w\t%r, [%r, #4]\n", PC_REGNUM, PC_REGNUM);
2144 }
2145 else
2146 {
2147 ASM_OUTPUT_ALIGN (f, 2);
2148 fprintf (f, "\t.code\t16\n");
2149 fprintf (f, ".Ltrampoline_start:\n");
2150 asm_fprintf (f, "\tpush\t{r0, r1}\n");
2151 asm_fprintf (f, "\tldr\tr0, [%r, #8]\n", PC_REGNUM);
2152 asm_fprintf (f, "\tmov\t%r, r0\n", STATIC_CHAIN_REGNUM);
2153 asm_fprintf (f, "\tldr\tr0, [%r, #8]\n", PC_REGNUM);
2154 asm_fprintf (f, "\tstr\tr0, [%r, #4]\n", SP_REGNUM);
2155 asm_fprintf (f, "\tpop\t{r0, %r}\n", PC_REGNUM);
2156 }
2157 assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2158 assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2159}
2160
2161/* Emit RTL insns to initialize the variable parts of a trampoline. */
2162
2163static void
2164arm_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
2165{
2166 rtx fnaddr, mem, a_tramp;
2167
2168 emit_block_move (m_tramp, assemble_trampoline_template (),
2169 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
2170
2171 mem = adjust_address (m_tramp, SImode, TARGET_32BIT ? 8 : 12);
2172 emit_move_insn (mem, chain_value);
2173
2174 mem = adjust_address (m_tramp, SImode, TARGET_32BIT ? 12 : 16);
2175 fnaddr = XEXP (DECL_RTL (fndecl), 0);
2176 emit_move_insn (mem, fnaddr);
2177
2178 a_tramp = XEXP (m_tramp, 0);
2179 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__clear_cache"),
2180 LCT_NORMAL, VOIDmode, 2, a_tramp, Pmode,
2181 plus_constant (a_tramp, TRAMPOLINE_SIZE), Pmode);
2182}
2183
2184/* Thumb trampolines should be entered in thumb mode, so set
2185 the bottom bit of the address. */
2186
2187static rtx
2188arm_trampoline_adjust_address (rtx addr)
2189{
2190 if (TARGET_THUMB)
2191 addr = expand_simple_binop (Pmode, IOR, addr, const1_rtx,
2192 NULL, 0, OPTAB_LIB_WIDEN);
2193 return addr;
2194}
6d3d9133 2195\f
f676971a 2196/* Return 1 if it is possible to return using a single instruction.
a72d4945
RE
2197 If SIBLING is non-null, this is a test for a return before a sibling
2198 call. SIBLING is the call insn, so we can examine its register usage. */
6d3d9133 2199
ff9940b0 2200int
a72d4945 2201use_return_insn (int iscond, rtx sibling)
ff9940b0
RE
2202{
2203 int regno;
9b598fa0 2204 unsigned int func_type;
d5db54a1 2205 unsigned long saved_int_regs;
a72d4945 2206 unsigned HOST_WIDE_INT stack_adjust;
5848830f 2207 arm_stack_offsets *offsets;
ff9940b0 2208
d5b7b3ae 2209 /* Never use a return instruction before reload has run. */
6d3d9133
NC
2210 if (!reload_completed)
2211 return 0;
efc2515b 2212
9b598fa0
RE
2213 func_type = arm_current_func_type ();
2214
5b3e6663 2215 /* Naked, volatile and stack alignment functions need special
3a7731fd 2216 consideration. */
5b3e6663 2217 if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED | ARM_FT_STACKALIGN))
6d3d9133 2218 return 0;
06bea5aa 2219
a15908a4
PB
2220 /* So do interrupt functions that use the frame pointer and Thumb
2221 interrupt functions. */
2222 if (IS_INTERRUPT (func_type) && (frame_pointer_needed || TARGET_THUMB))
06bea5aa 2223 return 0;
a72d4945 2224
5848830f
PB
2225 offsets = arm_get_frame_offsets ();
2226 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
a72d4945 2227
6d3d9133 2228 /* As do variadic functions. */
38173d38 2229 if (crtl->args.pretend_args_size
3cb66fd7 2230 || cfun->machine->uses_anonymous_args
699a4925 2231 /* Or if the function calls __builtin_eh_return () */
e3b5732b 2232 || crtl->calls_eh_return
699a4925 2233 /* Or if the function calls alloca */
e3b5732b 2234 || cfun->calls_alloca
a72d4945
RE
2235 /* Or if there is a stack adjustment. However, if the stack pointer
2236 is saved on the stack, we can use a pre-incrementing stack load. */
ec6237e4
PB
2237 || !(stack_adjust == 0 || (TARGET_APCS_FRAME && frame_pointer_needed
2238 && stack_adjust == 4)))
ff9940b0
RE
2239 return 0;
2240
954954d1 2241 saved_int_regs = offsets->saved_regs_mask;
d5db54a1 2242
a72d4945
RE
2243 /* Unfortunately, the insn
2244
2245 ldmib sp, {..., sp, ...}
2246
2247 triggers a bug on most SA-110 based devices, such that the stack
2248 pointer won't be correctly restored if the instruction takes a
839a4992 2249 page fault. We work around this problem by popping r3 along with
a72d4945 2250 the other registers, since that is never slower than executing
f676971a 2251 another instruction.
a72d4945
RE
2252
2253 We test for !arm_arch5 here, because code for any architecture
2254 less than this could potentially be run on one of the buggy
2255 chips. */
5b3e6663 2256 if (stack_adjust == 4 && !arm_arch5 && TARGET_ARM)
a72d4945
RE
2257 {
2258 /* Validate that r3 is a call-clobbered register (always true in
d6b4baa4 2259 the default abi) ... */
a72d4945
RE
2260 if (!call_used_regs[3])
2261 return 0;
2262
4f5dfed0
JC
2263 /* ... that it isn't being used for a return value ... */
2264 if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
2265 return 0;
2266
2267 /* ... or for a tail-call argument ... */
a72d4945
RE
2268 if (sibling)
2269 {
e6d29d15 2270 gcc_assert (GET_CODE (sibling) == CALL_INSN);
a72d4945
RE
2271
2272 if (find_regno_fusage (sibling, USE, 3))
2273 return 0;
2274 }
2275
2276 /* ... and that there are no call-saved registers in r0-r2
2277 (always true in the default ABI). */
2278 if (saved_int_regs & 0x7)
2279 return 0;
2280 }
2281
b111229a 2282 /* Can't be done if interworking with Thumb, and any registers have been
d5db54a1 2283 stacked. */
a15908a4 2284 if (TARGET_INTERWORK && saved_int_regs != 0 && !IS_INTERRUPT(func_type))
b36ba79f 2285 return 0;
d5db54a1
RE
2286
2287 /* On StrongARM, conditional returns are expensive if they aren't
2288 taken and multiple registers have been stacked. */
abac3b49 2289 if (iscond && arm_tune_strongarm)
6ed30148 2290 {
f676971a 2291 /* Conditional return when just the LR is stored is a simple
d5db54a1
RE
2292 conditional-load instruction, that's not expensive. */
2293 if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
2294 return 0;
6ed30148 2295
020a4035
RE
2296 if (flag_pic
2297 && arm_pic_register != INVALID_REGNUM
6fb5fa3c 2298 && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
b111229a 2299 return 0;
6ed30148 2300 }
d5db54a1
RE
2301
2302 /* If there are saved registers but the LR isn't saved, then we need
2303 two instructions for the return. */
2304 if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
2305 return 0;
2306
3b684012 2307 /* Can't be done if any of the FPA regs are pushed,
6d3d9133 2308 since this also requires an insn. */
9b66ebb1
PB
2309 if (TARGET_HARD_FLOAT && TARGET_FPA)
2310 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
6fb5fa3c 2311 if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
9b66ebb1
PB
2312 return 0;
2313
2314 /* Likewise VFP regs. */
2315 if (TARGET_HARD_FLOAT && TARGET_VFP)
2316 for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
6fb5fa3c 2317 if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
d5b7b3ae 2318 return 0;
ff9940b0 2319
5a9335ef
NC
2320 if (TARGET_REALLY_IWMMXT)
2321 for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
6fb5fa3c 2322 if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
5a9335ef
NC
2323 return 0;
2324
ff9940b0
RE
2325 return 1;
2326}
2327
cce8749e
CH
2328/* Return TRUE if int I is a valid immediate ARM constant. */
2329
2330int
e32bac5b 2331const_ok_for_arm (HOST_WIDE_INT i)
cce8749e 2332{
4642ccb1 2333 int lowbit;
e0b92319 2334
f676971a 2335 /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
56636818 2336 be all zero, or all one. */
30cf4896
KG
2337 if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
2338 && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
2339 != ((~(unsigned HOST_WIDE_INT) 0)
2340 & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
56636818 2341 return FALSE;
f676971a 2342
4642ccb1 2343 i &= (unsigned HOST_WIDE_INT) 0xffffffff;
e0b92319 2344
4642ccb1
RE
2345 /* Fast return for 0 and small values. We must do this for zero, since
2346 the code below can't handle that one case. */
2347 if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
e2c671ba
RE
2348 return TRUE;
2349
5b3e6663
PB
2350 /* Get the number of trailing zeros. */
2351 lowbit = ffs((int) i) - 1;
2352
2353 /* Only even shifts are allowed in ARM mode so round down to the
2354 nearest even number. */
2355 if (TARGET_ARM)
2356 lowbit &= ~1;
4642ccb1
RE
2357
2358 if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
2359 return TRUE;
5b3e6663
PB
2360
2361 if (TARGET_ARM)
2362 {
2363 /* Allow rotated constants in ARM mode. */
2364 if (lowbit <= 4
4642ccb1
RE
2365 && ((i & ~0xc000003f) == 0
2366 || (i & ~0xf000000f) == 0
2367 || (i & ~0xfc000003) == 0))
5b3e6663
PB
2368 return TRUE;
2369 }
2370 else
2371 {
2372 HOST_WIDE_INT v;
2373
d724c8f0 2374 /* Allow repeated patterns 0x00XY00XY or 0xXYXYXYXY. */
5b3e6663
PB
2375 v = i & 0xff;
2376 v |= v << 16;
2377 if (i == v || i == (v | (v << 8)))
2378 return TRUE;
d724c8f0
AS
2379
2380 /* Allow repeated pattern 0xXY00XY00. */
2381 v = i & 0xff00;
2382 v |= v << 16;
2383 if (i == v)
2384 return TRUE;
5b3e6663 2385 }
cce8749e 2386
f3bb6135
RE
2387 return FALSE;
2388}
cce8749e 2389
6354dc9b 2390/* Return true if I is a valid constant for the operation CODE. */
74bbc178 2391static int
e32bac5b 2392const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
e2c671ba
RE
2393{
2394 if (const_ok_for_arm (i))
2395 return 1;
2396
2397 switch (code)
2398 {
2399 case PLUS:
d5a0a47b
RE
2400 case COMPARE:
2401 case EQ:
2402 case NE:
2403 case GT:
2404 case LE:
2405 case LT:
2406 case GE:
2407 case GEU:
2408 case LTU:
2409 case GTU:
2410 case LEU:
2411 case UNORDERED:
2412 case ORDERED:
2413 case UNEQ:
2414 case UNGE:
2415 case UNLT:
2416 case UNGT:
2417 case UNLE:
e2c671ba
RE
2418 return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
2419
2420 case MINUS: /* Should only occur with (MINUS I reg) => rsb */
2421 case XOR:
a7994a57
RR
2422 return 0;
2423
e2c671ba 2424 case IOR:
a7994a57
RR
2425 if (TARGET_THUMB2)
2426 return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
e2c671ba
RE
2427 return 0;
2428
2429 case AND:
2430 return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2431
2432 default:
e6d29d15 2433 gcc_unreachable ();
e2c671ba
RE
2434 }
2435}
2436
2437/* Emit a sequence of insns to handle a large constant.
2438 CODE is the code of the operation required, it can be any of SET, PLUS,
2439 IOR, AND, XOR, MINUS;
2440 MODE is the mode in which the operation is being performed;
2441 VAL is the integer to operate on;
2442 SOURCE is the other operand (a register, or a null-pointer for SET);
2443 SUBTARGETS means it is safe to create scratch registers if that will
2b835d68
RE
2444 either produce a simpler sequence, or we will want to cse the values.
2445 Return value is the number of insns emitted. */
e2c671ba 2446
5b3e6663 2447/* ??? Tweak this for thumb2. */
e2c671ba 2448int
a406f566 2449arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
e32bac5b 2450 HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
2b835d68 2451{
a406f566
MM
2452 rtx cond;
2453
2454 if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
2455 cond = COND_EXEC_TEST (PATTERN (insn));
2456 else
2457 cond = NULL_RTX;
2458
2b835d68
RE
2459 if (subtargets || code == SET
2460 || (GET_CODE (target) == REG && GET_CODE (source) == REG
2461 && REGNO (target) != REGNO (source)))
2462 {
4b632bf1 2463 /* After arm_reorg has been called, we can't fix up expensive
05713b80 2464 constants by pushing them into memory so we must synthesize
4b632bf1
RE
2465 them in-line, regardless of the cost. This is only likely to
2466 be more costly on chips that have load delay slots and we are
2467 compiling without running the scheduler (so no splitting
aec3cfba
NC
2468 occurred before the final instruction emission).
2469
2470 Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
aec3cfba 2471 */
5895f793 2472 if (!after_arm_reorg
a406f566 2473 && !cond
f676971a 2474 && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
a406f566 2475 1, 0)
1b78f575
RE
2476 > (arm_constant_limit (optimize_function_for_size_p (cfun))
2477 + (code != SET))))
2b835d68
RE
2478 {
2479 if (code == SET)
2480 {
2481 /* Currently SET is the only monadic value for CODE, all
2482 the rest are diadic. */
571191af
PB
2483 if (TARGET_USE_MOVT)
2484 arm_emit_movpair (target, GEN_INT (val));
2485 else
2486 emit_set_insn (target, GEN_INT (val));
2487
2b835d68
RE
2488 return 1;
2489 }
2490 else
2491 {
2492 rtx temp = subtargets ? gen_reg_rtx (mode) : target;
2493
571191af
PB
2494 if (TARGET_USE_MOVT)
2495 arm_emit_movpair (temp, GEN_INT (val));
2496 else
2497 emit_set_insn (temp, GEN_INT (val));
2498
2b835d68
RE
2499 /* For MINUS, the value is subtracted from, since we never
2500 have subtraction of a constant. */
2501 if (code == MINUS)
d66437c5 2502 emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
2b835d68 2503 else
d66437c5
RE
2504 emit_set_insn (target,
2505 gen_rtx_fmt_ee (code, mode, source, temp));
2b835d68
RE
2506 return 2;
2507 }
2508 }
2509 }
2510
f676971a 2511 return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
a406f566 2512 1);
2b835d68
RE
2513}
2514
162e4591
RE
2515/* Return the number of instructions required to synthesize the given
2516 constant, if we start emitting them from bit-position I. */
ceebdb09 2517static int
e32bac5b 2518count_insns_for_constant (HOST_WIDE_INT remainder, int i)
ceebdb09
PB
2519{
2520 HOST_WIDE_INT temp1;
162e4591 2521 int step_size = TARGET_ARM ? 2 : 1;
ceebdb09 2522 int num_insns = 0;
162e4591
RE
2523
2524 gcc_assert (TARGET_ARM || i == 0);
2525
ceebdb09
PB
2526 do
2527 {
2528 int end;
f676971a 2529
ceebdb09
PB
2530 if (i <= 0)
2531 i += 32;
162e4591 2532 if (remainder & (((1 << step_size) - 1) << (i - step_size)))
ceebdb09
PB
2533 {
2534 end = i - 8;
2535 if (end < 0)
2536 end += 32;
2537 temp1 = remainder & ((0x0ff << end)
2538 | ((i < end) ? (0xff >> (32 - end)) : 0));
2539 remainder &= ~temp1;
2540 num_insns++;
162e4591 2541 i -= 8 - step_size;
ceebdb09 2542 }
162e4591 2543 i -= step_size;
ceebdb09
PB
2544 } while (remainder);
2545 return num_insns;
2546}
2547
162e4591 2548static int
90e77553 2549find_best_start (unsigned HOST_WIDE_INT remainder)
162e4591
RE
2550{
2551 int best_consecutive_zeros = 0;
2552 int i;
2553 int best_start = 0;
2554
2555 /* If we aren't targetting ARM, the best place to start is always at
2556 the bottom. */
2557 if (! TARGET_ARM)
2558 return 0;
2559
2560 for (i = 0; i < 32; i += 2)
2561 {
2562 int consecutive_zeros = 0;
2563
2564 if (!(remainder & (3 << i)))
2565 {
2566 while ((i < 32) && !(remainder & (3 << i)))
2567 {
2568 consecutive_zeros += 2;
2569 i += 2;
2570 }
2571 if (consecutive_zeros > best_consecutive_zeros)
2572 {
2573 best_consecutive_zeros = consecutive_zeros;
2574 best_start = i - consecutive_zeros;
2575 }
2576 i -= 2;
2577 }
2578 }
2579
2580 /* So long as it won't require any more insns to do so, it's
2581 desirable to emit a small constant (in bits 0...9) in the last
2582 insn. This way there is more chance that it can be combined with
2583 a later addressing insn to form a pre-indexed load or store
2584 operation. Consider:
2585
2586 *((volatile int *)0xe0000100) = 1;
2587 *((volatile int *)0xe0000110) = 2;
2588
2589 We want this to wind up as:
2590
2591 mov rA, #0xe0000000
2592 mov rB, #1
2593 str rB, [rA, #0x100]
2594 mov rB, #2
2595 str rB, [rA, #0x110]
2596
2597 rather than having to synthesize both large constants from scratch.
2598
2599 Therefore, we calculate how many insns would be required to emit
2600 the constant starting from `best_start', and also starting from
2601 zero (i.e. with bit 31 first to be output). If `best_start' doesn't
2602 yield a shorter sequence, we may as well use zero. */
2603 if (best_start != 0
2604 && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2605 && (count_insns_for_constant (remainder, 0) <=
2606 count_insns_for_constant (remainder, best_start)))
2607 best_start = 0;
2608
2609 return best_start;
2610}
2611
a406f566
MM
2612/* Emit an instruction with the indicated PATTERN. If COND is
2613 non-NULL, conditionalize the execution of the instruction on COND
2614 being true. */
2615
2616static void
2617emit_constant_insn (rtx cond, rtx pattern)
2618{
2619 if (cond)
2620 pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
2621 emit_insn (pattern);
2622}
2623
2b835d68
RE
2624/* As above, but extra parameter GENERATE which, if clear, suppresses
2625 RTL generation. */
5b3e6663 2626/* ??? This needs more work for thumb2. */
1d6e90ac 2627
d5b7b3ae 2628static int
a406f566 2629arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
e32bac5b
RE
2630 HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
2631 int generate)
e2c671ba 2632{
e2c671ba
RE
2633 int can_invert = 0;
2634 int can_negate = 0;
162e4591 2635 int final_invert = 0;
e2c671ba 2636 int can_negate_initial = 0;
e2c671ba
RE
2637 int i;
2638 int num_bits_set = 0;
2639 int set_sign_bit_copies = 0;
2640 int clear_sign_bit_copies = 0;
2641 int clear_zero_bit_copies = 0;
2642 int set_zero_bit_copies = 0;
2643 int insns = 0;
e2c671ba 2644 unsigned HOST_WIDE_INT temp1, temp2;
30cf4896 2645 unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
162e4591 2646 int step_size = TARGET_ARM ? 2 : 1;
e2c671ba 2647
d5b7b3ae 2648 /* Find out which operations are safe for a given CODE. Also do a quick
e2c671ba
RE
2649 check for degenerate cases; these can occur when DImode operations
2650 are split. */
2651 switch (code)
2652 {
2653 case SET:
2654 can_invert = 1;
e2c671ba
RE
2655 can_negate = 1;
2656 break;
2657
2658 case PLUS:
2659 can_negate = 1;
2660 can_negate_initial = 1;
2661 break;
2662
2663 case IOR:
30cf4896 2664 if (remainder == 0xffffffff)
e2c671ba 2665 {
2b835d68 2666 if (generate)
a406f566
MM
2667 emit_constant_insn (cond,
2668 gen_rtx_SET (VOIDmode, target,
2669 GEN_INT (ARM_SIGN_EXTEND (val))));
e2c671ba
RE
2670 return 1;
2671 }
a7994a57 2672
e2c671ba
RE
2673 if (remainder == 0)
2674 {
2675 if (reload_completed && rtx_equal_p (target, source))
2676 return 0;
a7994a57 2677
2b835d68 2678 if (generate)
a406f566
MM
2679 emit_constant_insn (cond,
2680 gen_rtx_SET (VOIDmode, target, source));
e2c671ba
RE
2681 return 1;
2682 }
a7994a57
RR
2683
2684 if (TARGET_THUMB2)
2685 can_invert = 1;
e2c671ba
RE
2686 break;
2687
2688 case AND:
2689 if (remainder == 0)
2690 {
2b835d68 2691 if (generate)
a406f566
MM
2692 emit_constant_insn (cond,
2693 gen_rtx_SET (VOIDmode, target, const0_rtx));
e2c671ba
RE
2694 return 1;
2695 }
30cf4896 2696 if (remainder == 0xffffffff)
e2c671ba
RE
2697 {
2698 if (reload_completed && rtx_equal_p (target, source))
2699 return 0;
2b835d68 2700 if (generate)
a406f566
MM
2701 emit_constant_insn (cond,
2702 gen_rtx_SET (VOIDmode, target, source));
e2c671ba
RE
2703 return 1;
2704 }
2705 can_invert = 1;
2706 break;
2707
2708 case XOR:
2709 if (remainder == 0)
2710 {
2711 if (reload_completed && rtx_equal_p (target, source))
2712 return 0;
2b835d68 2713 if (generate)
a406f566
MM
2714 emit_constant_insn (cond,
2715 gen_rtx_SET (VOIDmode, target, source));
e2c671ba
RE
2716 return 1;
2717 }
e0b92319 2718
162e4591
RE
2719 if (remainder == 0xffffffff)
2720 {
2721 if (generate)
2722 emit_constant_insn (cond,
2723 gen_rtx_SET (VOIDmode, target,
2724 gen_rtx_NOT (mode, source)));
2725 return 1;
2726 }
2727 break;
e2c671ba
RE
2728
2729 case MINUS:
2730 /* We treat MINUS as (val - source), since (source - val) is always
2731 passed as (source + (-val)). */
2732 if (remainder == 0)
2733 {
2b835d68 2734 if (generate)
a406f566
MM
2735 emit_constant_insn (cond,
2736 gen_rtx_SET (VOIDmode, target,
2737 gen_rtx_NEG (mode, source)));
e2c671ba
RE
2738 return 1;
2739 }
2740 if (const_ok_for_arm (val))
2741 {
2b835d68 2742 if (generate)
a406f566 2743 emit_constant_insn (cond,
f676971a 2744 gen_rtx_SET (VOIDmode, target,
a406f566
MM
2745 gen_rtx_MINUS (mode, GEN_INT (val),
2746 source)));
e2c671ba
RE
2747 return 1;
2748 }
2749 can_negate = 1;
2750
2751 break;
2752
2753 default:
e6d29d15 2754 gcc_unreachable ();
e2c671ba
RE
2755 }
2756
6354dc9b 2757 /* If we can do it in one insn get out quickly. */
e2c671ba
RE
2758 if (const_ok_for_arm (val)
2759 || (can_negate_initial && const_ok_for_arm (-val))
2760 || (can_invert && const_ok_for_arm (~val)))
2761 {
2b835d68 2762 if (generate)
a406f566
MM
2763 emit_constant_insn (cond,
2764 gen_rtx_SET (VOIDmode, target,
f676971a 2765 (source
a406f566
MM
2766 ? gen_rtx_fmt_ee (code, mode, source,
2767 GEN_INT (val))
2768 : GEN_INT (val))));
e2c671ba
RE
2769 return 1;
2770 }
2771
e2c671ba 2772 /* Calculate a few attributes that may be useful for specific
6354dc9b 2773 optimizations. */
a7994a57 2774 /* Count number of leading zeros. */
e2c671ba
RE
2775 for (i = 31; i >= 0; i--)
2776 {
2777 if ((remainder & (1 << i)) == 0)
2778 clear_sign_bit_copies++;
2779 else
2780 break;
2781 }
2782
a7994a57 2783 /* Count number of leading 1's. */
e2c671ba
RE
2784 for (i = 31; i >= 0; i--)
2785 {
2786 if ((remainder & (1 << i)) != 0)
2787 set_sign_bit_copies++;
2788 else
2789 break;
2790 }
2791
a7994a57 2792 /* Count number of trailing zero's. */
e2c671ba
RE
2793 for (i = 0; i <= 31; i++)
2794 {
2795 if ((remainder & (1 << i)) == 0)
2796 clear_zero_bit_copies++;
2797 else
2798 break;
2799 }
2800
a7994a57 2801 /* Count number of trailing 1's. */
e2c671ba
RE
2802 for (i = 0; i <= 31; i++)
2803 {
2804 if ((remainder & (1 << i)) != 0)
2805 set_zero_bit_copies++;
2806 else
2807 break;
2808 }
2809
2810 switch (code)
2811 {
2812 case SET:
5b3e6663
PB
2813 /* See if we can use movw. */
2814 if (arm_arch_thumb2 && (remainder & 0xffff0000) == 0)
2815 {
2816 if (generate)
2817 emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
2818 GEN_INT (val)));
2819 return 1;
2820 }
2821
e2c671ba
RE
2822 /* See if we can do this by sign_extending a constant that is known
2823 to be negative. This is a good, way of doing it, since the shift
2824 may well merge into a subsequent insn. */
2825 if (set_sign_bit_copies > 1)
2826 {
2827 if (const_ok_for_arm
f676971a 2828 (temp1 = ARM_SIGN_EXTEND (remainder
e2c671ba
RE
2829 << (set_sign_bit_copies - 1))))
2830 {
2b835d68
RE
2831 if (generate)
2832 {
d499463f 2833 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
a406f566 2834 emit_constant_insn (cond,
f676971a 2835 gen_rtx_SET (VOIDmode, new_src,
a406f566
MM
2836 GEN_INT (temp1)));
2837 emit_constant_insn (cond,
f676971a 2838 gen_ashrsi3 (target, new_src,
a406f566 2839 GEN_INT (set_sign_bit_copies - 1)));
2b835d68 2840 }
e2c671ba
RE
2841 return 2;
2842 }
2843 /* For an inverted constant, we will need to set the low bits,
2844 these will be shifted out of harm's way. */
2845 temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
2846 if (const_ok_for_arm (~temp1))
2847 {
2b835d68
RE
2848 if (generate)
2849 {
d499463f 2850 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
a406f566
MM
2851 emit_constant_insn (cond,
2852 gen_rtx_SET (VOIDmode, new_src,
2853 GEN_INT (temp1)));
2854 emit_constant_insn (cond,
f676971a 2855 gen_ashrsi3 (target, new_src,
a406f566 2856 GEN_INT (set_sign_bit_copies - 1)));
2b835d68 2857 }
e2c671ba
RE
2858 return 2;
2859 }
2860 }
2861
c87e6352
RE
2862 /* See if we can calculate the value as the difference between two
2863 valid immediates. */
2864 if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
2865 {
2866 int topshift = clear_sign_bit_copies & ~1;
2867
fa2c88a0
RE
2868 temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
2869 & (0xff000000 >> topshift));
c87e6352
RE
2870
2871 /* If temp1 is zero, then that means the 9 most significant
2872 bits of remainder were 1 and we've caused it to overflow.
2873 When topshift is 0 we don't need to do anything since we
2874 can borrow from 'bit 32'. */
2875 if (temp1 == 0 && topshift != 0)
2876 temp1 = 0x80000000 >> (topshift - 1);
2877
fa2c88a0 2878 temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
e0b92319 2879
c87e6352
RE
2880 if (const_ok_for_arm (temp2))
2881 {
2882 if (generate)
2883 {
2884 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2885 emit_constant_insn (cond,
2886 gen_rtx_SET (VOIDmode, new_src,
2887 GEN_INT (temp1)));
2888 emit_constant_insn (cond,
2889 gen_addsi3 (target, new_src,
2890 GEN_INT (-temp2)));
2891 }
2892
2893 return 2;
2894 }
2895 }
2896
e2c671ba
RE
2897 /* See if we can generate this by setting the bottom (or the top)
2898 16 bits, and then shifting these into the other half of the
2899 word. We only look for the simplest cases, to do more would cost
2900 too much. Be careful, however, not to generate this when the
2901 alternative would take fewer insns. */
30cf4896 2902 if (val & 0xffff0000)
e2c671ba 2903 {
30cf4896 2904 temp1 = remainder & 0xffff0000;
e2c671ba
RE
2905 temp2 = remainder & 0x0000ffff;
2906
6354dc9b 2907 /* Overlaps outside this range are best done using other methods. */
e2c671ba
RE
2908 for (i = 9; i < 24; i++)
2909 {
30cf4896 2910 if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
5895f793 2911 && !const_ok_for_arm (temp2))
e2c671ba 2912 {
d499463f
RE
2913 rtx new_src = (subtargets
2914 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2915 : target);
a406f566 2916 insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2b835d68 2917 source, subtargets, generate);
e2c671ba 2918 source = new_src;
2b835d68 2919 if (generate)
f676971a 2920 emit_constant_insn
a406f566
MM
2921 (cond,
2922 gen_rtx_SET
2923 (VOIDmode, target,
2924 gen_rtx_IOR (mode,
2925 gen_rtx_ASHIFT (mode, source,
2926 GEN_INT (i)),
2927 source)));
e2c671ba
RE
2928 return insns + 1;
2929 }
2930 }
2931
6354dc9b 2932 /* Don't duplicate cases already considered. */
e2c671ba
RE
2933 for (i = 17; i < 24; i++)
2934 {
2935 if (((temp1 | (temp1 >> i)) == remainder)
5895f793 2936 && !const_ok_for_arm (temp1))
e2c671ba 2937 {
d499463f
RE
2938 rtx new_src = (subtargets
2939 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2940 : target);
a406f566 2941 insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2b835d68 2942 source, subtargets, generate);
e2c671ba 2943 source = new_src;
2b835d68 2944 if (generate)
a406f566
MM
2945 emit_constant_insn
2946 (cond,
2947 gen_rtx_SET (VOIDmode, target,
43cffd11
RE
2948 gen_rtx_IOR
2949 (mode,
2950 gen_rtx_LSHIFTRT (mode, source,
2951 GEN_INT (i)),
2952 source)));
e2c671ba
RE
2953 return insns + 1;
2954 }
2955 }
2956 }
2957 break;
2958
2959 case IOR:
2960 case XOR:
7b64da89
RE
2961 /* If we have IOR or XOR, and the constant can be loaded in a
2962 single instruction, and we can find a temporary to put it in,
e2c671ba
RE
2963 then this can be done in two instructions instead of 3-4. */
2964 if (subtargets
d499463f 2965 /* TARGET can't be NULL if SUBTARGETS is 0 */
5895f793 2966 || (reload_completed && !reg_mentioned_p (target, source)))
e2c671ba 2967 {
5895f793 2968 if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
e2c671ba 2969 {
2b835d68
RE
2970 if (generate)
2971 {
2972 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
e2c671ba 2973
a406f566 2974 emit_constant_insn (cond,
f676971a 2975 gen_rtx_SET (VOIDmode, sub,
a406f566
MM
2976 GEN_INT (val)));
2977 emit_constant_insn (cond,
f676971a 2978 gen_rtx_SET (VOIDmode, target,
a406f566
MM
2979 gen_rtx_fmt_ee (code, mode,
2980 source, sub)));
2b835d68 2981 }
e2c671ba
RE
2982 return 2;
2983 }
2984 }
2985
2986 if (code == XOR)
2987 break;
2988
a7994a57
RR
2989 /* Convert.
2990 x = y | constant ( which is composed of set_sign_bit_copies of leading 1s
2991 and the remainder 0s for e.g. 0xfff00000)
2992 x = ~(~(y ashift set_sign_bit_copies) lshiftrt set_sign_bit_copies)
2993
2994 This can be done in 2 instructions by using shifts with mov or mvn.
2995 e.g. for
2996 x = x | 0xfff00000;
2997 we generate.
2998 mvn r0, r0, asl #12
2999 mvn r0, r0, lsr #12 */
e2c671ba
RE
3000 if (set_sign_bit_copies > 8
3001 && (val & (-1 << (32 - set_sign_bit_copies))) == val)
3002 {
2b835d68
RE
3003 if (generate)
3004 {
3005 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
3006 rtx shift = GEN_INT (set_sign_bit_copies);
3007
f676971a 3008 emit_constant_insn
a406f566
MM
3009 (cond,
3010 gen_rtx_SET (VOIDmode, sub,
f676971a 3011 gen_rtx_NOT (mode,
a406f566 3012 gen_rtx_ASHIFT (mode,
f676971a 3013 source,
a406f566 3014 shift))));
f676971a 3015 emit_constant_insn
a406f566
MM
3016 (cond,
3017 gen_rtx_SET (VOIDmode, target,
3018 gen_rtx_NOT (mode,
3019 gen_rtx_LSHIFTRT (mode, sub,
3020 shift))));
2b835d68 3021 }
e2c671ba
RE
3022 return 2;
3023 }
3024
a7994a57
RR
3025 /* Convert
3026 x = y | constant (which has set_zero_bit_copies number of trailing ones).
3027 to
3028 x = ~((~y lshiftrt set_zero_bit_copies) ashift set_zero_bit_copies).
3029
3030 For eg. r0 = r0 | 0xfff
3031 mvn r0, r0, lsr #12
3032 mvn r0, r0, asl #12
3033
3034 */
e2c671ba
RE
3035 if (set_zero_bit_copies > 8
3036 && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
3037 {
2b835d68
RE
3038 if (generate)
3039 {
3040 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
3041 rtx shift = GEN_INT (set_zero_bit_copies);
3042
a406f566
MM
3043 emit_constant_insn
3044 (cond,
3045 gen_rtx_SET (VOIDmode, sub,
3046 gen_rtx_NOT (mode,
3047 gen_rtx_LSHIFTRT (mode,
3048 source,
3049 shift))));
f676971a 3050 emit_constant_insn
a406f566
MM
3051 (cond,
3052 gen_rtx_SET (VOIDmode, target,
3053 gen_rtx_NOT (mode,
3054 gen_rtx_ASHIFT (mode, sub,
3055 shift))));
2b835d68 3056 }
e2c671ba
RE
3057 return 2;
3058 }
3059
a7994a57
RR
3060 /* This will never be reached for Thumb2 because orn is a valid
3061 instruction. This is for Thumb1 and the ARM 32 bit cases.
3062
3063 x = y | constant (such that ~constant is a valid constant)
3064 Transform this to
3065 x = ~(~y & ~constant).
3066 */
5895f793 3067 if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
e2c671ba 3068 {
2b835d68
RE
3069 if (generate)
3070 {
3071 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
a406f566
MM
3072 emit_constant_insn (cond,
3073 gen_rtx_SET (VOIDmode, sub,
3074 gen_rtx_NOT (mode, source)));
2b835d68
RE
3075 source = sub;
3076 if (subtargets)
3077 sub = gen_reg_rtx (mode);
a406f566
MM
3078 emit_constant_insn (cond,
3079 gen_rtx_SET (VOIDmode, sub,
f676971a 3080 gen_rtx_AND (mode, source,
a406f566
MM
3081 GEN_INT (temp1))));
3082 emit_constant_insn (cond,
3083 gen_rtx_SET (VOIDmode, target,
3084 gen_rtx_NOT (mode, sub)));
2b835d68 3085 }
e2c671ba
RE
3086 return 3;
3087 }
3088 break;
3089
3090 case AND:
3091 /* See if two shifts will do 2 or more insn's worth of work. */
3092 if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
3093 {
30cf4896 3094 HOST_WIDE_INT shift_mask = ((0xffffffff
e2c671ba 3095 << (32 - clear_sign_bit_copies))
30cf4896 3096 & 0xffffffff);
e2c671ba 3097
30cf4896 3098 if ((remainder | shift_mask) != 0xffffffff)
e2c671ba 3099 {
2b835d68
RE
3100 if (generate)
3101 {
d499463f 3102 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
f676971a 3103 insns = arm_gen_constant (AND, mode, cond,
a406f566 3104 remainder | shift_mask,
d499463f
RE
3105 new_src, source, subtargets, 1);
3106 source = new_src;
2b835d68
RE
3107 }
3108 else
d499463f
RE
3109 {
3110 rtx targ = subtargets ? NULL_RTX : target;
a406f566
MM
3111 insns = arm_gen_constant (AND, mode, cond,
3112 remainder | shift_mask,
d499463f
RE
3113 targ, source, subtargets, 0);
3114 }
2b835d68
RE
3115 }
3116
3117 if (generate)
3118 {
d499463f
RE
3119 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3120 rtx shift = GEN_INT (clear_sign_bit_copies);
3121
3122 emit_insn (gen_ashlsi3 (new_src, source, shift));
3123 emit_insn (gen_lshrsi3 (target, new_src, shift));
e2c671ba
RE
3124 }
3125
e2c671ba
RE
3126 return insns + 2;
3127 }
3128
3129 if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
3130 {
3131 HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
f676971a 3132
30cf4896 3133 if ((remainder | shift_mask) != 0xffffffff)
e2c671ba 3134 {
2b835d68
RE
3135 if (generate)
3136 {
d499463f
RE
3137 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3138
a406f566
MM
3139 insns = arm_gen_constant (AND, mode, cond,
3140 remainder | shift_mask,
d499463f
RE
3141 new_src, source, subtargets, 1);
3142 source = new_src;
2b835d68
RE
3143 }
3144 else
d499463f
RE
3145 {
3146 rtx targ = subtargets ? NULL_RTX : target;
3147
a406f566
MM
3148 insns = arm_gen_constant (AND, mode, cond,
3149 remainder | shift_mask,
d499463f
RE
3150 targ, source, subtargets, 0);
3151 }
2b835d68
RE
3152 }
3153
3154 if (generate)
3155 {
d499463f
RE
3156 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3157 rtx shift = GEN_INT (clear_zero_bit_copies);
3158
3159 emit_insn (gen_lshrsi3 (new_src, source, shift));
3160 emit_insn (gen_ashlsi3 (target, new_src, shift));
e2c671ba
RE
3161 }
3162
e2c671ba
RE
3163 return insns + 2;
3164 }
3165
3166 break;
3167
3168 default:
3169 break;
3170 }
3171
3172 for (i = 0; i < 32; i++)
3173 if (remainder & (1 << i))
3174 num_bits_set++;
3175
a7994a57
RR
3176 if ((code == AND)
3177 || (code != IOR && can_invert && num_bits_set > 16))
162e4591 3178 remainder ^= 0xffffffff;
e2c671ba 3179 else if (code == PLUS && num_bits_set > 16)
30cf4896 3180 remainder = (-remainder) & 0xffffffff;
162e4591
RE
3181
3182 /* For XOR, if more than half the bits are set and there's a sequence
3183 of more than 8 consecutive ones in the pattern then we can XOR by the
3184 inverted constant and then invert the final result; this may save an
3185 instruction and might also lead to the final mvn being merged with
3186 some other operation. */
3187 else if (code == XOR && num_bits_set > 16
3188 && (count_insns_for_constant (remainder ^ 0xffffffff,
3189 find_best_start
3190 (remainder ^ 0xffffffff))
3191 < count_insns_for_constant (remainder,
3192 find_best_start (remainder))))
3193 {
3194 remainder ^= 0xffffffff;
3195 final_invert = 1;
3196 }
e2c671ba
RE
3197 else
3198 {
3199 can_invert = 0;
3200 can_negate = 0;
3201 }
3202
3203 /* Now try and find a way of doing the job in either two or three
3204 instructions.
3205 We start by looking for the largest block of zeros that are aligned on
3206 a 2-bit boundary, we then fill up the temps, wrapping around to the
3207 top of the word when we drop off the bottom.
5b3e6663
PB
3208 In the worst case this code should produce no more than four insns.
3209 Thumb-2 constants are shifted, not rotated, so the MSB is always the
3210 best place to start. */
3211
3212 /* ??? Use thumb2 replicated constants when the high and low halfwords are
3213 the same. */
e2c671ba 3214 {
ceebdb09 3215 /* Now start emitting the insns. */
162e4591 3216 i = find_best_start (remainder);
e2c671ba
RE
3217 do
3218 {
3219 int end;
3220
3221 if (i <= 0)
3222 i += 32;
3223 if (remainder & (3 << (i - 2)))
3224 {
3225 end = i - 8;
3226 if (end < 0)
3227 end += 32;
3228 temp1 = remainder & ((0x0ff << end)
3229 | ((i < end) ? (0xff >> (32 - end)) : 0));
3230 remainder &= ~temp1;
3231
d499463f 3232 if (generate)
e2c671ba 3233 {
9503f3d1
RH
3234 rtx new_src, temp1_rtx;
3235
3236 if (code == SET || code == MINUS)
3237 {
3238 new_src = (subtargets ? gen_reg_rtx (mode) : target);
96ae8197 3239 if (can_invert && code != MINUS)
9503f3d1
RH
3240 temp1 = ~temp1;
3241 }
3242 else
3243 {
162e4591 3244 if ((final_invert || remainder) && subtargets)
9503f3d1 3245 new_src = gen_reg_rtx (mode);
96ae8197
NC
3246 else
3247 new_src = target;
9503f3d1
RH
3248 if (can_invert)
3249 temp1 = ~temp1;
3250 else if (can_negate)
3251 temp1 = -temp1;
3252 }
3253
3254 temp1 = trunc_int_for_mode (temp1, mode);
3255 temp1_rtx = GEN_INT (temp1);
d499463f
RE
3256
3257 if (code == SET)
9503f3d1 3258 ;
d499463f 3259 else if (code == MINUS)
9503f3d1 3260 temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
d499463f 3261 else
9503f3d1
RH
3262 temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
3263
a406f566 3264 emit_constant_insn (cond,
f676971a 3265 gen_rtx_SET (VOIDmode, new_src,
a406f566 3266 temp1_rtx));
d499463f 3267 source = new_src;
e2c671ba
RE
3268 }
3269
d499463f
RE
3270 if (code == SET)
3271 {
3272 can_invert = 0;
3273 code = PLUS;
3274 }
3275 else if (code == MINUS)
3276 code = PLUS;
3277
e2c671ba 3278 insns++;
162e4591 3279 i -= 8 - step_size;
e2c671ba 3280 }
7a085dce 3281 /* Arm allows rotates by a multiple of two. Thumb-2 allows arbitrary
5b3e6663 3282 shifts. */
162e4591 3283 i -= step_size;
1d6e90ac
NC
3284 }
3285 while (remainder);
e2c671ba 3286 }
1d6e90ac 3287
162e4591
RE
3288 if (final_invert)
3289 {
3290 if (generate)
3291 emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
3292 gen_rtx_NOT (mode, source)));
3293 insns++;
3294 }
3295
e2c671ba
RE
3296 return insns;
3297}
3298
bd9c7e23
RE
3299/* Canonicalize a comparison so that we are more likely to recognize it.
3300 This can be done for a few constant compares, where we can make the
3301 immediate value easier to load. */
1d6e90ac 3302
bd9c7e23 3303enum rtx_code
73160ba9 3304arm_canonicalize_comparison (enum rtx_code code, rtx *op0, rtx *op1)
bd9c7e23 3305{
73160ba9
DJ
3306 enum machine_mode mode;
3307 unsigned HOST_WIDE_INT i, maxval;
3308
3309 mode = GET_MODE (*op0);
3310 if (mode == VOIDmode)
3311 mode = GET_MODE (*op1);
3312
a14b88bb 3313 maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
bd9c7e23 3314
73160ba9
DJ
3315 /* For DImode, we have GE/LT/GEU/LTU comparisons. In ARM mode
3316 we can also use cmp/cmpeq for GTU/LEU. GT/LE must be either
3317 reversed or (for constant OP1) adjusted to GE/LT. Similarly
3318 for GTU/LEU in Thumb mode. */
3319 if (mode == DImode)
3320 {
3321 rtx tem;
3322
3323 /* To keep things simple, always use the Cirrus cfcmp64 if it is
3324 available. */
3325 if (TARGET_ARM && TARGET_HARD_FLOAT && TARGET_MAVERICK)
3326 return code;
3327
3328 if (code == GT || code == LE
3329 || (!TARGET_ARM && (code == GTU || code == LEU)))
3330 {
3331 /* Missing comparison. First try to use an available
3332 comparison. */
3333 if (GET_CODE (*op1) == CONST_INT)
3334 {
3335 i = INTVAL (*op1);
3336 switch (code)
3337 {
3338 case GT:
3339 case LE:
3340 if (i != maxval
3341 && arm_const_double_by_immediates (GEN_INT (i + 1)))
3342 {
3343 *op1 = GEN_INT (i + 1);
3344 return code == GT ? GE : LT;
3345 }
3346 break;
3347 case GTU:
3348 case LEU:
3349 if (i != ~((unsigned HOST_WIDE_INT) 0)
3350 && arm_const_double_by_immediates (GEN_INT (i + 1)))
3351 {
3352 *op1 = GEN_INT (i + 1);
3353 return code == GTU ? GEU : LTU;
3354 }
3355 break;
3356 default:
3357 gcc_unreachable ();
3358 }
3359 }
3360
3361 /* If that did not work, reverse the condition. */
3362 tem = *op0;
3363 *op0 = *op1;
3364 *op1 = tem;
3365 return swap_condition (code);
3366 }
3367
3368 return code;
3369 }
3370
3371 /* Comparisons smaller than DImode. Only adjust comparisons against
3372 an out-of-range constant. */
3373 if (GET_CODE (*op1) != CONST_INT
3374 || const_ok_for_arm (INTVAL (*op1))
3375 || const_ok_for_arm (- INTVAL (*op1)))
3376 return code;
3377
3378 i = INTVAL (*op1);
3379
bd9c7e23
RE
3380 switch (code)
3381 {
3382 case EQ:
3383 case NE:
3384 return code;
3385
3386 case GT:
3387 case LE:
a14b88bb 3388 if (i != maxval
5895f793 3389 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
bd9c7e23 3390 {
5895f793 3391 *op1 = GEN_INT (i + 1);
bd9c7e23
RE
3392 return code == GT ? GE : LT;
3393 }
3394 break;
3395
3396 case GE:
3397 case LT:
a14b88bb 3398 if (i != ~maxval
5895f793 3399 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
bd9c7e23 3400 {
5895f793 3401 *op1 = GEN_INT (i - 1);
bd9c7e23
RE
3402 return code == GE ? GT : LE;
3403 }
3404 break;
3405
3406 case GTU:
3407 case LEU:
30cf4896 3408 if (i != ~((unsigned HOST_WIDE_INT) 0)
5895f793 3409 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
bd9c7e23
RE
3410 {
3411 *op1 = GEN_INT (i + 1);
3412 return code == GTU ? GEU : LTU;
3413 }
3414 break;
3415
3416 case GEU:
3417 case LTU:
3418 if (i != 0
5895f793 3419 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
bd9c7e23
RE
3420 {
3421 *op1 = GEN_INT (i - 1);
3422 return code == GEU ? GTU : LEU;
3423 }
3424 break;
3425
3426 default:
e6d29d15 3427 gcc_unreachable ();
bd9c7e23
RE
3428 }
3429
3430 return code;
3431}
bd9c7e23 3432
d4453b7a
PB
3433
3434/* Define how to find the value returned by a function. */
3435
390b17c2
RE
3436static rtx
3437arm_function_value(const_tree type, const_tree func,
3438 bool outgoing ATTRIBUTE_UNUSED)
d4453b7a
PB
3439{
3440 enum machine_mode mode;
3441 int unsignedp ATTRIBUTE_UNUSED;
3442 rtx r ATTRIBUTE_UNUSED;
3443
d4453b7a 3444 mode = TYPE_MODE (type);
390b17c2
RE
3445
3446 if (TARGET_AAPCS_BASED)
3447 return aapcs_allocate_return_reg (mode, type, func);
3448
d4453b7a
PB
3449 /* Promote integer types. */
3450 if (INTEGRAL_TYPE_P (type))
cde0f3fd 3451 mode = arm_promote_function_mode (type, mode, &unsignedp, func, 1);
866af8a9
JB
3452
3453 /* Promotes small structs returned in a register to full-word size
3454 for big-endian AAPCS. */
3455 if (arm_return_in_msb (type))
3456 {
3457 HOST_WIDE_INT size = int_size_in_bytes (type);
3458 if (size % UNITS_PER_WORD != 0)
3459 {
3460 size += UNITS_PER_WORD - size % UNITS_PER_WORD;
3461 mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
3462 }
3463 }
e0b92319 3464
390b17c2
RE
3465 return LIBCALL_VALUE (mode);
3466}
3467
3468static int
3469libcall_eq (const void *p1, const void *p2)
3470{
3471 return rtx_equal_p ((const_rtx) p1, (const_rtx) p2);
3472}
3473
3474static hashval_t
3475libcall_hash (const void *p1)
3476{
3477 return hash_rtx ((const_rtx) p1, VOIDmode, NULL, NULL, FALSE);
3478}
3479
3480static void
3481add_libcall (htab_t htab, rtx libcall)
3482{
3483 *htab_find_slot (htab, libcall, INSERT) = libcall;
3484}
3485
3486static bool
7fc6a96b 3487arm_libcall_uses_aapcs_base (const_rtx libcall)
390b17c2
RE
3488{
3489 static bool init_done = false;
3490 static htab_t libcall_htab;
3491
3492 if (!init_done)
3493 {
3494 init_done = true;
3495
3496 libcall_htab = htab_create (31, libcall_hash, libcall_eq,
3497 NULL);
3498 add_libcall (libcall_htab,
3499 convert_optab_libfunc (sfloat_optab, SFmode, SImode));
3500 add_libcall (libcall_htab,
3501 convert_optab_libfunc (sfloat_optab, DFmode, SImode));
3502 add_libcall (libcall_htab,
3503 convert_optab_libfunc (sfloat_optab, SFmode, DImode));
3504 add_libcall (libcall_htab,
3505 convert_optab_libfunc (sfloat_optab, DFmode, DImode));
3506
3507 add_libcall (libcall_htab,
3508 convert_optab_libfunc (ufloat_optab, SFmode, SImode));
3509 add_libcall (libcall_htab,
3510 convert_optab_libfunc (ufloat_optab, DFmode, SImode));
3511 add_libcall (libcall_htab,
3512 convert_optab_libfunc (ufloat_optab, SFmode, DImode));
3513 add_libcall (libcall_htab,
3514 convert_optab_libfunc (ufloat_optab, DFmode, DImode));
3515
3516 add_libcall (libcall_htab,
3517 convert_optab_libfunc (sext_optab, SFmode, HFmode));
3518 add_libcall (libcall_htab,
3519 convert_optab_libfunc (trunc_optab, HFmode, SFmode));
3520 add_libcall (libcall_htab,
3521 convert_optab_libfunc (sfix_optab, DImode, DFmode));
3522 add_libcall (libcall_htab,
3523 convert_optab_libfunc (ufix_optab, DImode, DFmode));
3524 add_libcall (libcall_htab,
3525 convert_optab_libfunc (sfix_optab, DImode, SFmode));
3526 add_libcall (libcall_htab,
3527 convert_optab_libfunc (ufix_optab, DImode, SFmode));
3528 }
3529
3530 return libcall && htab_find (libcall_htab, libcall) != NULL;
3531}
3532
3533rtx
7fc6a96b 3534arm_libcall_value (enum machine_mode mode, const_rtx libcall)
390b17c2
RE
3535{
3536 if (TARGET_AAPCS_BASED && arm_pcs_default != ARM_PCS_AAPCS
3537 && GET_MODE_CLASS (mode) == MODE_FLOAT)
3538 {
3539 /* The following libcalls return their result in integer registers,
3540 even though they return a floating point value. */
3541 if (arm_libcall_uses_aapcs_base (libcall))
3542 return gen_rtx_REG (mode, ARG_REGISTER(1));
3543
3544 }
3545
3546 return LIBCALL_VALUE (mode);
d4453b7a
PB
3547}
3548
e0b92319 3549/* Determine the amount of memory needed to store the possible return
9f7bf991
RE
3550 registers of an untyped call. */
3551int
3552arm_apply_result_size (void)
3553{
3554 int size = 16;
3555
390b17c2 3556 if (TARGET_32BIT)
9f7bf991
RE
3557 {
3558 if (TARGET_HARD_FLOAT_ABI)
3559 {
390b17c2
RE
3560 if (TARGET_VFP)
3561 size += 32;
9f7bf991
RE
3562 if (TARGET_FPA)
3563 size += 12;
3564 if (TARGET_MAVERICK)
3565 size += 8;
3566 }
3567 if (TARGET_IWMMXT_ABI)
3568 size += 8;
3569 }
3570
3571 return size;
3572}
d4453b7a 3573
390b17c2
RE
3574/* Decide whether TYPE should be returned in memory (true)
3575 or in a register (false). FNTYPE is the type of the function making
3576 the call. */
23668cf7 3577static bool
390b17c2 3578arm_return_in_memory (const_tree type, const_tree fntype)
2b835d68 3579{
dc0ba55a
JT
3580 HOST_WIDE_INT size;
3581
390b17c2
RE
3582 size = int_size_in_bytes (type); /* Negative if not fixed size. */
3583
3584 if (TARGET_AAPCS_BASED)
3585 {
3586 /* Simple, non-aggregate types (ie not including vectors and
3587 complex) are always returned in a register (or registers).
3588 We don't care about which register here, so we can short-cut
3589 some of the detail. */
3590 if (!AGGREGATE_TYPE_P (type)
3591 && TREE_CODE (type) != VECTOR_TYPE
3592 && TREE_CODE (type) != COMPLEX_TYPE)
3593 return false;
3594
3595 /* Any return value that is no larger than one word can be
3596 returned in r0. */
3597 if (((unsigned HOST_WIDE_INT) size) <= UNITS_PER_WORD)
3598 return false;
3599
3600 /* Check any available co-processors to see if they accept the
3601 type as a register candidate (VFP, for example, can return
3602 some aggregates in consecutive registers). These aren't
3603 available if the call is variadic. */
3604 if (aapcs_select_return_coproc (type, fntype) >= 0)
3605 return false;
3606
3607 /* Vector values should be returned using ARM registers, not
3608 memory (unless they're over 16 bytes, which will break since
3609 we only have four call-clobbered registers to play with). */
3610 if (TREE_CODE (type) == VECTOR_TYPE)
3611 return (size < 0 || size > (4 * UNITS_PER_WORD));
3612
3613 /* The rest go in memory. */
3614 return true;
3615 }
88f77cba 3616
88f77cba
JB
3617 if (TREE_CODE (type) == VECTOR_TYPE)
3618 return (size < 0 || size > (4 * UNITS_PER_WORD));
3619
3dd7ab65 3620 if (!AGGREGATE_TYPE_P (type) &&
390b17c2
RE
3621 (TREE_CODE (type) != VECTOR_TYPE))
3622 /* All simple types are returned in registers. */
3623 return false;
dc0ba55a 3624
5848830f 3625 if (arm_abi != ARM_ABI_APCS)
dc0ba55a 3626 {
5848830f 3627 /* ATPCS and later return aggregate types in memory only if they are
dc0ba55a
JT
3628 larger than a word (or are variable size). */
3629 return (size < 0 || size > UNITS_PER_WORD);
3630 }
f676971a 3631
6bc82793 3632 /* For the arm-wince targets we choose to be compatible with Microsoft's
d5b7b3ae
RE
3633 ARM and Thumb compilers, which always return aggregates in memory. */
3634#ifndef ARM_WINCE
e529bd42
NC
3635 /* All structures/unions bigger than one word are returned in memory.
3636 Also catch the case where int_size_in_bytes returns -1. In this case
6bc82793 3637 the aggregate is either huge or of variable size, and in either case
e529bd42 3638 we will want to return it via memory and not in a register. */
dc0ba55a 3639 if (size < 0 || size > UNITS_PER_WORD)
390b17c2 3640 return true;
f676971a 3641
d7d01975 3642 if (TREE_CODE (type) == RECORD_TYPE)
2b835d68
RE
3643 {
3644 tree field;
3645
3a2ea258
RE
3646 /* For a struct the APCS says that we only return in a register
3647 if the type is 'integer like' and every addressable element
3648 has an offset of zero. For practical purposes this means
3649 that the structure can have at most one non bit-field element
3650 and that this element must be the first one in the structure. */
f676971a 3651
f5a1b0d2
NC
3652 /* Find the first field, ignoring non FIELD_DECL things which will
3653 have been created by C++. */
3654 for (field = TYPE_FIELDS (type);
3655 field && TREE_CODE (field) != FIELD_DECL;
910ad8de 3656 field = DECL_CHAIN (field))
f5a1b0d2 3657 continue;
f676971a 3658
f5a1b0d2 3659 if (field == NULL)
390b17c2 3660 return false; /* An empty structure. Allowed by an extension to ANSI C. */
f5a1b0d2 3661
d5b7b3ae
RE
3662 /* Check that the first field is valid for returning in a register. */
3663
3664 /* ... Floats are not allowed */
9e291dbe 3665 if (FLOAT_TYPE_P (TREE_TYPE (field)))
390b17c2 3666 return true;
3a2ea258 3667
d5b7b3ae
RE
3668 /* ... Aggregates that are not themselves valid for returning in
3669 a register are not allowed. */
81464b2c 3670 if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
390b17c2 3671 return true;
6f7ebcbb 3672
3a2ea258
RE
3673 /* Now check the remaining fields, if any. Only bitfields are allowed,
3674 since they are not addressable. */
910ad8de 3675 for (field = DECL_CHAIN (field);
f5a1b0d2 3676 field;
910ad8de 3677 field = DECL_CHAIN (field))
f5a1b0d2
NC
3678 {
3679 if (TREE_CODE (field) != FIELD_DECL)
3680 continue;
f676971a 3681
5895f793 3682 if (!DECL_BIT_FIELD_TYPE (field))
390b17c2 3683 return true;
f5a1b0d2 3684 }
2b835d68 3685
390b17c2 3686 return false;
2b835d68 3687 }
f676971a 3688
d7d01975 3689 if (TREE_CODE (type) == UNION_TYPE)
2b835d68
RE
3690 {
3691 tree field;
3692
3693 /* Unions can be returned in registers if every element is
3694 integral, or can be returned in an integer register. */
f5a1b0d2
NC
3695 for (field = TYPE_FIELDS (type);
3696 field;
910ad8de 3697 field = DECL_CHAIN (field))
2b835d68 3698 {
f5a1b0d2
NC
3699 if (TREE_CODE (field) != FIELD_DECL)
3700 continue;
3701
6cc8c0b3 3702 if (FLOAT_TYPE_P (TREE_TYPE (field)))
390b17c2 3703 return true;
f676971a 3704
81464b2c 3705 if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
390b17c2 3706 return true;
2b835d68 3707 }
f676971a 3708
390b17c2 3709 return false;
2b835d68 3710 }
f676971a
EC
3711#endif /* not ARM_WINCE */
3712
d5b7b3ae 3713 /* Return all other types in memory. */
390b17c2 3714 return true;
2b835d68
RE
3715}
3716
d6b4baa4 3717/* Indicate whether or not words of a double are in big-endian order. */
3717da94
JT
3718
3719int
e32bac5b 3720arm_float_words_big_endian (void)
3717da94 3721{
9b66ebb1 3722 if (TARGET_MAVERICK)
9b6b54e2 3723 return 0;
3717da94
JT
3724
3725 /* For FPA, float words are always big-endian. For VFP, floats words
3726 follow the memory system mode. */
3727
9b66ebb1 3728 if (TARGET_FPA)
3717da94 3729 {
3717da94
JT
3730 return 1;
3731 }
3732
3733 if (TARGET_VFP)
3734 return (TARGET_BIG_END ? 1 : 0);
3735
3736 return 1;
3737}
3738
390b17c2
RE
3739const struct pcs_attribute_arg
3740{
3741 const char *arg;
3742 enum arm_pcs value;
3743} pcs_attribute_args[] =
3744 {
3745 {"aapcs", ARM_PCS_AAPCS},
3746 {"aapcs-vfp", ARM_PCS_AAPCS_VFP},
0f1a24df
RE
3747#if 0
3748 /* We could recognize these, but changes would be needed elsewhere
3749 * to implement them. */
390b17c2
RE
3750 {"aapcs-iwmmxt", ARM_PCS_AAPCS_IWMMXT},
3751 {"atpcs", ARM_PCS_ATPCS},
3752 {"apcs", ARM_PCS_APCS},
0f1a24df 3753#endif
390b17c2
RE
3754 {NULL, ARM_PCS_UNKNOWN}
3755 };
3756
3757static enum arm_pcs
3758arm_pcs_from_attribute (tree attr)
3759{
3760 const struct pcs_attribute_arg *ptr;
3761 const char *arg;
3762
3763 /* Get the value of the argument. */
3764 if (TREE_VALUE (attr) == NULL_TREE
3765 || TREE_CODE (TREE_VALUE (attr)) != STRING_CST)
3766 return ARM_PCS_UNKNOWN;
3767
3768 arg = TREE_STRING_POINTER (TREE_VALUE (attr));
3769
3770 /* Check it against the list of known arguments. */
3771 for (ptr = pcs_attribute_args; ptr->arg != NULL; ptr++)
3772 if (streq (arg, ptr->arg))
3773 return ptr->value;
3774
3775 /* An unrecognized interrupt type. */
3776 return ARM_PCS_UNKNOWN;
3777}
3778
3779/* Get the PCS variant to use for this call. TYPE is the function's type
3780 specification, DECL is the specific declartion. DECL may be null if
3781 the call could be indirect or if this is a library call. */
3782static enum arm_pcs
3783arm_get_pcs_model (const_tree type, const_tree decl)
3784{
3785 bool user_convention = false;
3786 enum arm_pcs user_pcs = arm_pcs_default;
3787 tree attr;
3788
3789 gcc_assert (type);
3790
3791 attr = lookup_attribute ("pcs", TYPE_ATTRIBUTES (type));
3792 if (attr)
3793 {
3794 user_pcs = arm_pcs_from_attribute (TREE_VALUE (attr));
3795 user_convention = true;
3796 }
3797
3798 if (TARGET_AAPCS_BASED)
3799 {
3800 /* Detect varargs functions. These always use the base rules
3801 (no argument is ever a candidate for a co-processor
3802 register). */
f38958e8 3803 bool base_rules = stdarg_p (type);
390b17c2
RE
3804
3805 if (user_convention)
3806 {
3807 if (user_pcs > ARM_PCS_AAPCS_LOCAL)
d8a07487 3808 sorry ("non-AAPCS derived PCS variant");
390b17c2 3809 else if (base_rules && user_pcs != ARM_PCS_AAPCS)
d8a07487 3810 error ("variadic functions must use the base AAPCS variant");
390b17c2
RE
3811 }
3812
3813 if (base_rules)
3814 return ARM_PCS_AAPCS;
3815 else if (user_convention)
3816 return user_pcs;
3817 else if (decl && flag_unit_at_a_time)
3818 {
3819 /* Local functions never leak outside this compilation unit,
3820 so we are free to use whatever conventions are
3821 appropriate. */
3822 /* FIXME: remove CONST_CAST_TREE when cgraph is constified. */
3823 struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE(decl));
3824 if (i && i->local)
3825 return ARM_PCS_AAPCS_LOCAL;
3826 }
3827 }
3828 else if (user_convention && user_pcs != arm_pcs_default)
3829 sorry ("PCS variant");
3830
3831 /* For everything else we use the target's default. */
3832 return arm_pcs_default;
3833}
3834
3835
3836static void
3837aapcs_vfp_cum_init (CUMULATIVE_ARGS *pcum ATTRIBUTE_UNUSED,
3838 const_tree fntype ATTRIBUTE_UNUSED,
3839 rtx libcall ATTRIBUTE_UNUSED,
3840 const_tree fndecl ATTRIBUTE_UNUSED)
3841{
3842 /* Record the unallocated VFP registers. */
3843 pcum->aapcs_vfp_regs_free = (1 << NUM_VFP_ARG_REGS) - 1;
3844 pcum->aapcs_vfp_reg_alloc = 0;
3845}
3846
3847/* Walk down the type tree of TYPE counting consecutive base elements.
3848 If *MODEP is VOIDmode, then set it to the first valid floating point
3849 type. If a non-floating point type is found, or if a floating point
3850 type that doesn't match a non-VOIDmode *MODEP is found, then return -1,
3851 otherwise return the count in the sub-tree. */
3852static int
3853aapcs_vfp_sub_candidate (const_tree type, enum machine_mode *modep)
3854{
3855 enum machine_mode mode;
3856 HOST_WIDE_INT size;
3857
3858 switch (TREE_CODE (type))
3859 {
3860 case REAL_TYPE:
3861 mode = TYPE_MODE (type);
3862 if (mode != DFmode && mode != SFmode)
3863 return -1;
3864
3865 if (*modep == VOIDmode)
3866 *modep = mode;
3867
3868 if (*modep == mode)
3869 return 1;
3870
3871 break;
3872
3873 case COMPLEX_TYPE:
3874 mode = TYPE_MODE (TREE_TYPE (type));
3875 if (mode != DFmode && mode != SFmode)
3876 return -1;
3877
3878 if (*modep == VOIDmode)
3879 *modep = mode;
3880
3881 if (*modep == mode)
3882 return 2;
3883
3884 break;
3885
3886 case VECTOR_TYPE:
3887 /* Use V2SImode and V4SImode as representatives of all 64-bit
3888 and 128-bit vector types, whether or not those modes are
3889 supported with the present options. */
3890 size = int_size_in_bytes (type);
3891 switch (size)
3892 {
3893 case 8:
3894 mode = V2SImode;
3895 break;
3896 case 16:
3897 mode = V4SImode;
3898 break;
3899 default:
3900 return -1;
3901 }
3902
3903 if (*modep == VOIDmode)
3904 *modep = mode;
3905
3906 /* Vector modes are considered to be opaque: two vectors are
3907 equivalent for the purposes of being homogeneous aggregates
3908 if they are the same size. */
3909 if (*modep == mode)
3910 return 1;
3911
3912 break;
3913
3914 case ARRAY_TYPE:
3915 {
3916 int count;
3917 tree index = TYPE_DOMAIN (type);
3918
3919 /* Can't handle incomplete types. */
3920 if (!COMPLETE_TYPE_P(type))
3921 return -1;
3922
3923 count = aapcs_vfp_sub_candidate (TREE_TYPE (type), modep);
3924 if (count == -1
3925 || !index
3926 || !TYPE_MAX_VALUE (index)
3927 || !host_integerp (TYPE_MAX_VALUE (index), 1)
3928 || !TYPE_MIN_VALUE (index)
3929 || !host_integerp (TYPE_MIN_VALUE (index), 1)
3930 || count < 0)
3931 return -1;
3932
3933 count *= (1 + tree_low_cst (TYPE_MAX_VALUE (index), 1)
3934 - tree_low_cst (TYPE_MIN_VALUE (index), 1));
3935
3936 /* There must be no padding. */
3937 if (!host_integerp (TYPE_SIZE (type), 1)
3938 || (tree_low_cst (TYPE_SIZE (type), 1)
3939 != count * GET_MODE_BITSIZE (*modep)))
3940 return -1;
3941
3942 return count;
3943 }
3944
3945 case RECORD_TYPE:
3946 {
3947 int count = 0;
3948 int sub_count;
3949 tree field;
3950
3951 /* Can't handle incomplete types. */
3952 if (!COMPLETE_TYPE_P(type))
3953 return -1;
3954
910ad8de 3955 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
390b17c2
RE
3956 {
3957 if (TREE_CODE (field) != FIELD_DECL)
3958 continue;
3959
3960 sub_count = aapcs_vfp_sub_candidate (TREE_TYPE (field), modep);
3961 if (sub_count < 0)
3962 return -1;
3963 count += sub_count;
3964 }
3965
3966 /* There must be no padding. */
3967 if (!host_integerp (TYPE_SIZE (type), 1)
3968 || (tree_low_cst (TYPE_SIZE (type), 1)
3969 != count * GET_MODE_BITSIZE (*modep)))
3970 return -1;
3971
3972 return count;
3973 }
3974
3975 case UNION_TYPE:
3976 case QUAL_UNION_TYPE:
3977 {
3978 /* These aren't very interesting except in a degenerate case. */
3979 int count = 0;
3980 int sub_count;
3981 tree field;
3982
3983 /* Can't handle incomplete types. */
3984 if (!COMPLETE_TYPE_P(type))
3985 return -1;
3986
910ad8de 3987 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
390b17c2
RE
3988 {
3989 if (TREE_CODE (field) != FIELD_DECL)
3990 continue;
3991
3992 sub_count = aapcs_vfp_sub_candidate (TREE_TYPE (field), modep);
3993 if (sub_count < 0)
3994 return -1;
3995 count = count > sub_count ? count : sub_count;
3996 }
3997
3998 /* There must be no padding. */
3999 if (!host_integerp (TYPE_SIZE (type), 1)
4000 || (tree_low_cst (TYPE_SIZE (type), 1)
4001 != count * GET_MODE_BITSIZE (*modep)))
4002 return -1;
4003
4004 return count;
4005 }
4006
4007 default:
4008 break;
4009 }
4010
4011 return -1;
4012}
4013
e0dc3601 4014/* Return true if PCS_VARIANT should use VFP registers. */
390b17c2 4015static bool
e0dc3601 4016use_vfp_abi (enum arm_pcs pcs_variant, bool is_double)
390b17c2 4017{
e0dc3601 4018 if (pcs_variant == ARM_PCS_AAPCS_VFP)
50416c61
PB
4019 {
4020 static bool seen_thumb1_vfp = false;
4021
4022 if (TARGET_THUMB1 && !seen_thumb1_vfp)
4023 {
4024 sorry ("Thumb-1 hard-float VFP ABI");
4025 /* sorry() is not immediately fatal, so only display this once. */
4026 seen_thumb1_vfp = true;
4027 }
4028
4029 return true;
4030 }
e0dc3601
PB
4031
4032 if (pcs_variant != ARM_PCS_AAPCS_LOCAL)
4033 return false;
4034
4035 return (TARGET_32BIT && TARGET_VFP && TARGET_HARD_FLOAT &&
4036 (TARGET_VFP_DOUBLE || !is_double));
4037}
4038
4039static bool
4040aapcs_vfp_is_call_or_return_candidate (enum arm_pcs pcs_variant,
4041 enum machine_mode mode, const_tree type,
70dd156a 4042 enum machine_mode *base_mode, int *count)
e0dc3601
PB
4043{
4044 enum machine_mode new_mode = VOIDmode;
4045
390b17c2
RE
4046 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4047 || GET_MODE_CLASS (mode) == MODE_VECTOR_INT
4048 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
4049 {
4050 *count = 1;
e0dc3601 4051 new_mode = mode;
390b17c2
RE
4052 }
4053 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
4054 {
4055 *count = 2;
e0dc3601 4056 new_mode = (mode == DCmode ? DFmode : SFmode);
390b17c2
RE
4057 }
4058 else if (type && (mode == BLKmode || TREE_CODE (type) == VECTOR_TYPE))
4059 {
e0dc3601 4060 int ag_count = aapcs_vfp_sub_candidate (type, &new_mode);
390b17c2
RE
4061
4062 if (ag_count > 0 && ag_count <= 4)
e0dc3601
PB
4063 *count = ag_count;
4064 else
4065 return false;
390b17c2 4066 }
e0dc3601
PB
4067 else
4068 return false;
4069
4070
4071 if (!use_vfp_abi (pcs_variant, ARM_NUM_REGS (new_mode) > 1))
4072 return false;
4073
4074 *base_mode = new_mode;
4075 return true;
390b17c2
RE
4076}
4077
4078static bool
4079aapcs_vfp_is_return_candidate (enum arm_pcs pcs_variant,
4080 enum machine_mode mode, const_tree type)
4081{
4082 int count ATTRIBUTE_UNUSED;
46107b99 4083 enum machine_mode ag_mode ATTRIBUTE_UNUSED;
390b17c2 4084
e0dc3601 4085 if (!use_vfp_abi (pcs_variant, false))
390b17c2 4086 return false;
e0dc3601
PB
4087 return aapcs_vfp_is_call_or_return_candidate (pcs_variant, mode, type,
4088 &ag_mode, &count);
390b17c2
RE
4089}
4090
4091static bool
4092aapcs_vfp_is_call_candidate (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4093 const_tree type)
4094{
e0dc3601 4095 if (!use_vfp_abi (pcum->pcs_variant, false))
390b17c2 4096 return false;
e0dc3601
PB
4097
4098 return aapcs_vfp_is_call_or_return_candidate (pcum->pcs_variant, mode, type,
390b17c2
RE
4099 &pcum->aapcs_vfp_rmode,
4100 &pcum->aapcs_vfp_rcount);
4101}
4102
4103static bool
4104aapcs_vfp_allocate (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4105 const_tree type ATTRIBUTE_UNUSED)
4106{
4107 int shift = GET_MODE_SIZE (pcum->aapcs_vfp_rmode) / GET_MODE_SIZE (SFmode);
4108 unsigned mask = (1 << (shift * pcum->aapcs_vfp_rcount)) - 1;
4109 int regno;
4110
4111 for (regno = 0; regno < NUM_VFP_ARG_REGS; regno += shift)
4112 if (((pcum->aapcs_vfp_regs_free >> regno) & mask) == mask)
4113 {
4114 pcum->aapcs_vfp_reg_alloc = mask << regno;
4115 if (mode == BLKmode || (mode == TImode && !TARGET_NEON))
4116 {
4117 int i;
4118 int rcount = pcum->aapcs_vfp_rcount;
4119 int rshift = shift;
4120 enum machine_mode rmode = pcum->aapcs_vfp_rmode;
4121 rtx par;
4122 if (!TARGET_NEON)
4123 {
4124 /* Avoid using unsupported vector modes. */
4125 if (rmode == V2SImode)
4126 rmode = DImode;
4127 else if (rmode == V4SImode)
4128 {
4129 rmode = DImode;
4130 rcount *= 2;
4131 rshift /= 2;
4132 }
4133 }
4134 par = gen_rtx_PARALLEL (mode, rtvec_alloc (rcount));
4135 for (i = 0; i < rcount; i++)
4136 {
4137 rtx tmp = gen_rtx_REG (rmode,
4138 FIRST_VFP_REGNUM + regno + i * rshift);
4139 tmp = gen_rtx_EXPR_LIST
4140 (VOIDmode, tmp,
4141 GEN_INT (i * GET_MODE_SIZE (rmode)));
4142 XVECEXP (par, 0, i) = tmp;
4143 }
4144
4145 pcum->aapcs_reg = par;
4146 }
4147 else
4148 pcum->aapcs_reg = gen_rtx_REG (mode, FIRST_VFP_REGNUM + regno);
4149 return true;
4150 }
4151 return false;
4152}
4153
4154static rtx
4155aapcs_vfp_allocate_return_reg (enum arm_pcs pcs_variant ATTRIBUTE_UNUSED,
4156 enum machine_mode mode,
4157 const_tree type ATTRIBUTE_UNUSED)
4158{
e0dc3601 4159 if (!use_vfp_abi (pcs_variant, false))
390b17c2 4160 return false;
e0dc3601 4161
390b17c2
RE
4162 if (mode == BLKmode || (mode == TImode && !TARGET_NEON))
4163 {
4164 int count;
46107b99 4165 enum machine_mode ag_mode;
390b17c2
RE
4166 int i;
4167 rtx par;
4168 int shift;
4169
e0dc3601
PB
4170 aapcs_vfp_is_call_or_return_candidate (pcs_variant, mode, type,
4171 &ag_mode, &count);
390b17c2
RE
4172
4173 if (!TARGET_NEON)
4174 {
4175 if (ag_mode == V2SImode)
4176 ag_mode = DImode;
4177 else if (ag_mode == V4SImode)
4178 {
4179 ag_mode = DImode;
4180 count *= 2;
4181 }
4182 }
4183 shift = GET_MODE_SIZE(ag_mode) / GET_MODE_SIZE(SFmode);
4184 par = gen_rtx_PARALLEL (mode, rtvec_alloc (count));
4185 for (i = 0; i < count; i++)
4186 {
4187 rtx tmp = gen_rtx_REG (ag_mode, FIRST_VFP_REGNUM + i * shift);
4188 tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp,
4189 GEN_INT (i * GET_MODE_SIZE (ag_mode)));
4190 XVECEXP (par, 0, i) = tmp;
4191 }
4192
4193 return par;
4194 }
4195
4196 return gen_rtx_REG (mode, FIRST_VFP_REGNUM);
4197}
4198
4199static void
4200aapcs_vfp_advance (CUMULATIVE_ARGS *pcum ATTRIBUTE_UNUSED,
4201 enum machine_mode mode ATTRIBUTE_UNUSED,
4202 const_tree type ATTRIBUTE_UNUSED)
4203{
4204 pcum->aapcs_vfp_regs_free &= ~pcum->aapcs_vfp_reg_alloc;
4205 pcum->aapcs_vfp_reg_alloc = 0;
4206 return;
4207}
4208
4209#define AAPCS_CP(X) \
4210 { \
4211 aapcs_ ## X ## _cum_init, \
4212 aapcs_ ## X ## _is_call_candidate, \
4213 aapcs_ ## X ## _allocate, \
4214 aapcs_ ## X ## _is_return_candidate, \
4215 aapcs_ ## X ## _allocate_return_reg, \
4216 aapcs_ ## X ## _advance \
4217 }
4218
4219/* Table of co-processors that can be used to pass arguments in
4220 registers. Idealy no arugment should be a candidate for more than
4221 one co-processor table entry, but the table is processed in order
4222 and stops after the first match. If that entry then fails to put
4223 the argument into a co-processor register, the argument will go on
4224 the stack. */
4225static struct
4226{
4227 /* Initialize co-processor related state in CUMULATIVE_ARGS structure. */
4228 void (*cum_init) (CUMULATIVE_ARGS *, const_tree, rtx, const_tree);
4229
4230 /* Return true if an argument of mode MODE (or type TYPE if MODE is
4231 BLKmode) is a candidate for this co-processor's registers; this
4232 function should ignore any position-dependent state in
4233 CUMULATIVE_ARGS and only use call-type dependent information. */
4234 bool (*is_call_candidate) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4235
4236 /* Return true if the argument does get a co-processor register; it
4237 should set aapcs_reg to an RTX of the register allocated as is
4238 required for a return from FUNCTION_ARG. */
4239 bool (*allocate) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4240
4241 /* Return true if a result of mode MODE (or type TYPE if MODE is
4242 BLKmode) is can be returned in this co-processor's registers. */
4243 bool (*is_return_candidate) (enum arm_pcs, enum machine_mode, const_tree);
4244
4245 /* Allocate and return an RTX element to hold the return type of a
4246 call, this routine must not fail and will only be called if
4247 is_return_candidate returned true with the same parameters. */
4248 rtx (*allocate_return_reg) (enum arm_pcs, enum machine_mode, const_tree);
4249
4250 /* Finish processing this argument and prepare to start processing
4251 the next one. */
4252 void (*advance) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4253} aapcs_cp_arg_layout[ARM_NUM_COPROC_SLOTS] =
4254 {
4255 AAPCS_CP(vfp)
4256 };
4257
4258#undef AAPCS_CP
4259
4260static int
4261aapcs_select_call_coproc (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
9c6a2bee 4262 const_tree type)
390b17c2
RE
4263{
4264 int i;
4265
4266 for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4267 if (aapcs_cp_arg_layout[i].is_call_candidate (pcum, mode, type))
4268 return i;
4269
4270 return -1;
4271}
4272
4273static int
4274aapcs_select_return_coproc (const_tree type, const_tree fntype)
4275{
4276 /* We aren't passed a decl, so we can't check that a call is local.
4277 However, it isn't clear that that would be a win anyway, since it
4278 might limit some tail-calling opportunities. */
4279 enum arm_pcs pcs_variant;
4280
4281 if (fntype)
4282 {
4283 const_tree fndecl = NULL_TREE;
4284
4285 if (TREE_CODE (fntype) == FUNCTION_DECL)
4286 {
4287 fndecl = fntype;
4288 fntype = TREE_TYPE (fntype);
4289 }
4290
4291 pcs_variant = arm_get_pcs_model (fntype, fndecl);
4292 }
4293 else
4294 pcs_variant = arm_pcs_default;
4295
4296 if (pcs_variant != ARM_PCS_AAPCS)
4297 {
4298 int i;
4299
4300 for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4301 if (aapcs_cp_arg_layout[i].is_return_candidate (pcs_variant,
4302 TYPE_MODE (type),
4303 type))
4304 return i;
4305 }
4306 return -1;
4307}
4308
4309static rtx
4310aapcs_allocate_return_reg (enum machine_mode mode, const_tree type,
4311 const_tree fntype)
4312{
4313 /* We aren't passed a decl, so we can't check that a call is local.
4314 However, it isn't clear that that would be a win anyway, since it
4315 might limit some tail-calling opportunities. */
4316 enum arm_pcs pcs_variant;
4317 int unsignedp ATTRIBUTE_UNUSED;
4318
4319 if (fntype)
4320 {
4321 const_tree fndecl = NULL_TREE;
4322
4323 if (TREE_CODE (fntype) == FUNCTION_DECL)
4324 {
4325 fndecl = fntype;
4326 fntype = TREE_TYPE (fntype);
4327 }
4328
4329 pcs_variant = arm_get_pcs_model (fntype, fndecl);
4330 }
4331 else
4332 pcs_variant = arm_pcs_default;
4333
4334 /* Promote integer types. */
4335 if (type && INTEGRAL_TYPE_P (type))
4336 mode = arm_promote_function_mode (type, mode, &unsignedp, fntype, 1);
4337
4338 if (pcs_variant != ARM_PCS_AAPCS)
4339 {
4340 int i;
4341
4342 for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4343 if (aapcs_cp_arg_layout[i].is_return_candidate (pcs_variant, mode,
4344 type))
4345 return aapcs_cp_arg_layout[i].allocate_return_reg (pcs_variant,
4346 mode, type);
4347 }
4348
4349 /* Promotes small structs returned in a register to full-word size
4350 for big-endian AAPCS. */
4351 if (type && arm_return_in_msb (type))
4352 {
4353 HOST_WIDE_INT size = int_size_in_bytes (type);
4354 if (size % UNITS_PER_WORD != 0)
4355 {
4356 size += UNITS_PER_WORD - size % UNITS_PER_WORD;
4357 mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
4358 }
4359 }
4360
4361 return gen_rtx_REG (mode, R0_REGNUM);
4362}
4363
4364rtx
4365aapcs_libcall_value (enum machine_mode mode)
4366{
4367 return aapcs_allocate_return_reg (mode, NULL_TREE, NULL_TREE);
4368}
4369
4370/* Lay out a function argument using the AAPCS rules. The rule
4371 numbers referred to here are those in the AAPCS. */
4372static void
4373aapcs_layout_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
9c6a2bee 4374 const_tree type, bool named)
390b17c2
RE
4375{
4376 int nregs, nregs2;
4377 int ncrn;
4378
4379 /* We only need to do this once per argument. */
4380 if (pcum->aapcs_arg_processed)
4381 return;
4382
4383 pcum->aapcs_arg_processed = true;
4384
4385 /* Special case: if named is false then we are handling an incoming
4386 anonymous argument which is on the stack. */
4387 if (!named)
4388 return;
4389
4390 /* Is this a potential co-processor register candidate? */
4391 if (pcum->pcs_variant != ARM_PCS_AAPCS)
4392 {
4393 int slot = aapcs_select_call_coproc (pcum, mode, type);
4394 pcum->aapcs_cprc_slot = slot;
4395
4396 /* We don't have to apply any of the rules from part B of the
4397 preparation phase, these are handled elsewhere in the
4398 compiler. */
4399
4400 if (slot >= 0)
4401 {
4402 /* A Co-processor register candidate goes either in its own
4403 class of registers or on the stack. */
4404 if (!pcum->aapcs_cprc_failed[slot])
4405 {
4406 /* C1.cp - Try to allocate the argument to co-processor
4407 registers. */
4408 if (aapcs_cp_arg_layout[slot].allocate (pcum, mode, type))
4409 return;
4410
4411 /* C2.cp - Put the argument on the stack and note that we
4412 can't assign any more candidates in this slot. We also
4413 need to note that we have allocated stack space, so that
4414 we won't later try to split a non-cprc candidate between
4415 core registers and the stack. */
4416 pcum->aapcs_cprc_failed[slot] = true;
4417 pcum->can_split = false;
4418 }
4419
4420 /* We didn't get a register, so this argument goes on the
4421 stack. */
4422 gcc_assert (pcum->can_split == false);
4423 return;
4424 }
4425 }
4426
4427 /* C3 - For double-word aligned arguments, round the NCRN up to the
4428 next even number. */
4429 ncrn = pcum->aapcs_ncrn;
4430 if ((ncrn & 1) && arm_needs_doubleword_align (mode, type))
4431 ncrn++;
4432
4433 nregs = ARM_NUM_REGS2(mode, type);
4434
4435 /* Sigh, this test should really assert that nregs > 0, but a GCC
4436 extension allows empty structs and then gives them empty size; it
4437 then allows such a structure to be passed by value. For some of
4438 the code below we have to pretend that such an argument has
4439 non-zero size so that we 'locate' it correctly either in
4440 registers or on the stack. */
4441 gcc_assert (nregs >= 0);
4442
4443 nregs2 = nregs ? nregs : 1;
4444
4445 /* C4 - Argument fits entirely in core registers. */
4446 if (ncrn + nregs2 <= NUM_ARG_REGS)
4447 {
4448 pcum->aapcs_reg = gen_rtx_REG (mode, ncrn);
4449 pcum->aapcs_next_ncrn = ncrn + nregs;
4450 return;
4451 }
4452
4453 /* C5 - Some core registers left and there are no arguments already
4454 on the stack: split this argument between the remaining core
4455 registers and the stack. */
4456 if (ncrn < NUM_ARG_REGS && pcum->can_split)
4457 {
4458 pcum->aapcs_reg = gen_rtx_REG (mode, ncrn);
4459 pcum->aapcs_next_ncrn = NUM_ARG_REGS;
4460 pcum->aapcs_partial = (NUM_ARG_REGS - ncrn) * UNITS_PER_WORD;
4461 return;
4462 }
4463
4464 /* C6 - NCRN is set to 4. */
4465 pcum->aapcs_next_ncrn = NUM_ARG_REGS;
4466
4467 /* C7,C8 - arugment goes on the stack. We have nothing to do here. */
4468 return;
4469}
4470
82e9d970
PB
4471/* Initialize a variable CUM of type CUMULATIVE_ARGS
4472 for a call to a function whose data type is FNTYPE.
4473 For a library call, FNTYPE is NULL. */
4474void
f676971a 4475arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
390b17c2 4476 rtx libname,
e32bac5b 4477 tree fndecl ATTRIBUTE_UNUSED)
82e9d970 4478{
390b17c2
RE
4479 /* Long call handling. */
4480 if (fntype)
4481 pcum->pcs_variant = arm_get_pcs_model (fntype, fndecl);
4482 else
4483 pcum->pcs_variant = arm_pcs_default;
4484
4485 if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4486 {
4487 if (arm_libcall_uses_aapcs_base (libname))
4488 pcum->pcs_variant = ARM_PCS_AAPCS;
4489
4490 pcum->aapcs_ncrn = pcum->aapcs_next_ncrn = 0;
4491 pcum->aapcs_reg = NULL_RTX;
4492 pcum->aapcs_partial = 0;
4493 pcum->aapcs_arg_processed = false;
4494 pcum->aapcs_cprc_slot = -1;
4495 pcum->can_split = true;
4496
4497 if (pcum->pcs_variant != ARM_PCS_AAPCS)
4498 {
4499 int i;
4500
4501 for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4502 {
4503 pcum->aapcs_cprc_failed[i] = false;
4504 aapcs_cp_arg_layout[i].cum_init (pcum, fntype, libname, fndecl);
4505 }
4506 }
4507 return;
4508 }
4509
4510 /* Legacy ABIs */
4511
82e9d970 4512 /* On the ARM, the offset starts at 0. */
29e339b9 4513 pcum->nregs = 0;
5a9335ef 4514 pcum->iwmmxt_nregs = 0;
5848830f 4515 pcum->can_split = true;
f676971a 4516
5a9335ef
NC
4517 /* Varargs vectors are treated the same as long long.
4518 named_count avoids having to change the way arm handles 'named' */
4519 pcum->named_count = 0;
4520 pcum->nargs = 0;
4521
4522 if (TARGET_REALLY_IWMMXT && fntype)
4523 {
4524 tree fn_arg;
4525
4526 for (fn_arg = TYPE_ARG_TYPES (fntype);
4527 fn_arg;
4528 fn_arg = TREE_CHAIN (fn_arg))
4529 pcum->named_count += 1;
4530
4531 if (! pcum->named_count)
4532 pcum->named_count = INT_MAX;
4533 }
82e9d970
PB
4534}
4535
5848830f
PB
4536
4537/* Return true if mode/type need doubleword alignment. */
c2ed6cf8 4538static bool
9c6a2bee 4539arm_needs_doubleword_align (enum machine_mode mode, const_tree type)
5848830f 4540{
65a939f7
PB
4541 return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
4542 || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
5848830f
PB
4543}
4544
4545
82e9d970
PB
4546/* Determine where to put an argument to a function.
4547 Value is zero to push the argument on the stack,
4548 or a hard register in which to store the argument.
4549
4550 MODE is the argument's machine mode.
4551 TYPE is the data type of the argument (as a tree).
4552 This is null for libcalls where that information may
4553 not be available.
4554 CUM is a variable of type CUMULATIVE_ARGS which gives info about
4555 the preceding args and about the function being called.
4556 NAMED is nonzero if this argument is a named parameter
9c6a2bee 4557 (otherwise it is an extra parameter matching an ellipsis).
1d6e90ac 4558
9c6a2bee
NF
4559 On the ARM, normally the first 16 bytes are passed in registers r0-r3; all
4560 other arguments are passed on the stack. If (NAMED == 0) (which happens
4561 only in assign_parms, since TARGET_SETUP_INCOMING_VARARGS is
4562 defined), say it is passed in the stack (function_prologue will
4563 indeed make it pass in the stack if necessary). */
4564
4565static rtx
e32bac5b 4566arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
9c6a2bee 4567 const_tree type, bool named)
82e9d970 4568{
5848830f
PB
4569 int nregs;
4570
390b17c2
RE
4571 /* Handle the special case quickly. Pick an arbitrary value for op2 of
4572 a call insn (op3 of a call_value insn). */
4573 if (mode == VOIDmode)
4574 return const0_rtx;
4575
4576 if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4577 {
4578 aapcs_layout_arg (pcum, mode, type, named);
4579 return pcum->aapcs_reg;
4580 }
4581
5848830f
PB
4582 /* Varargs vectors are treated the same as long long.
4583 named_count avoids having to change the way arm handles 'named' */
4584 if (TARGET_IWMMXT_ABI
f676971a 4585 && arm_vector_mode_supported_p (mode)
5848830f 4586 && pcum->named_count > pcum->nargs + 1)
5a9335ef 4587 {
5848830f
PB
4588 if (pcum->iwmmxt_nregs <= 9)
4589 return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
4590 else
5a9335ef 4591 {
5848830f
PB
4592 pcum->can_split = false;
4593 return NULL_RTX;
5a9335ef 4594 }
5a9335ef
NC
4595 }
4596
5848830f
PB
4597 /* Put doubleword aligned quantities in even register pairs. */
4598 if (pcum->nregs & 1
4599 && ARM_DOUBLEWORD_ALIGN
4600 && arm_needs_doubleword_align (mode, type))
4601 pcum->nregs++;
4602
666c27b9 4603 /* Only allow splitting an arg between regs and memory if all preceding
5848830f
PB
4604 args were allocated to regs. For args passed by reference we only count
4605 the reference pointer. */
4606 if (pcum->can_split)
4607 nregs = 1;
4608 else
4609 nregs = ARM_NUM_REGS2 (mode, type);
4610
4611 if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
82e9d970 4612 return NULL_RTX;
f676971a 4613
82e9d970
PB
4614 return gen_rtx_REG (mode, pcum->nregs);
4615}
1741620c 4616
c2ed6cf8
NF
4617static unsigned int
4618arm_function_arg_boundary (enum machine_mode mode, const_tree type)
4619{
4620 return (ARM_DOUBLEWORD_ALIGN && arm_needs_doubleword_align (mode, type)
4621 ? DOUBLEWORD_ALIGNMENT
4622 : PARM_BOUNDARY);
4623}
4624
78a52f11
RH
4625static int
4626arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
390b17c2 4627 tree type, bool named)
78a52f11
RH
4628{
4629 int nregs = pcum->nregs;
4630
390b17c2
RE
4631 if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4632 {
4633 aapcs_layout_arg (pcum, mode, type, named);
4634 return pcum->aapcs_partial;
4635 }
4636
88f77cba 4637 if (TARGET_IWMMXT_ABI && arm_vector_mode_supported_p (mode))
78a52f11
RH
4638 return 0;
4639
4640 if (NUM_ARG_REGS > nregs
4641 && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
4642 && pcum->can_split)
4643 return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
4644
4645 return 0;
4646}
4647
9c6a2bee
NF
4648/* Update the data in PCUM to advance over an argument
4649 of mode MODE and data type TYPE.
4650 (TYPE is null for libcalls where that information may not be available.) */
4651
4652static void
390b17c2 4653arm_function_arg_advance (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
9c6a2bee 4654 const_tree type, bool named)
390b17c2
RE
4655{
4656 if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4657 {
4658 aapcs_layout_arg (pcum, mode, type, named);
4659
4660 if (pcum->aapcs_cprc_slot >= 0)
4661 {
4662 aapcs_cp_arg_layout[pcum->aapcs_cprc_slot].advance (pcum, mode,
4663 type);
4664 pcum->aapcs_cprc_slot = -1;
4665 }
4666
4667 /* Generic stuff. */
4668 pcum->aapcs_arg_processed = false;
4669 pcum->aapcs_ncrn = pcum->aapcs_next_ncrn;
4670 pcum->aapcs_reg = NULL_RTX;
4671 pcum->aapcs_partial = 0;
4672 }
4673 else
4674 {
4675 pcum->nargs += 1;
4676 if (arm_vector_mode_supported_p (mode)
4677 && pcum->named_count > pcum->nargs
4678 && TARGET_IWMMXT_ABI)
4679 pcum->iwmmxt_nregs += 1;
4680 else
4681 pcum->nregs += ARM_NUM_REGS2 (mode, type);
4682 }
4683}
4684
1741620c
JD
4685/* Variable sized types are passed by reference. This is a GCC
4686 extension to the ARM ABI. */
4687
8cd5a4e0
RH
4688static bool
4689arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
4690 enum machine_mode mode ATTRIBUTE_UNUSED,
586de218 4691 const_tree type, bool named ATTRIBUTE_UNUSED)
1741620c
JD
4692{
4693 return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
4694}
82e9d970 4695\f
c27ba912
DM
4696/* Encode the current state of the #pragma [no_]long_calls. */
4697typedef enum
82e9d970 4698{
6fc0bb99 4699 OFF, /* No #pragma [no_]long_calls is in effect. */
c27ba912
DM
4700 LONG, /* #pragma long_calls is in effect. */
4701 SHORT /* #pragma no_long_calls is in effect. */
4702} arm_pragma_enum;
82e9d970 4703
c27ba912 4704static arm_pragma_enum arm_pragma_long_calls = OFF;
82e9d970 4705
8b97c5f8 4706void
e32bac5b 4707arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
82e9d970 4708{
8b97c5f8
ZW
4709 arm_pragma_long_calls = LONG;
4710}
4711
4712void
e32bac5b 4713arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
8b97c5f8
ZW
4714{
4715 arm_pragma_long_calls = SHORT;
4716}
4717
4718void
e32bac5b 4719arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
8b97c5f8
ZW
4720{
4721 arm_pragma_long_calls = OFF;
82e9d970
PB
4722}
4723\f
91d231cb
JM
4724/* Handle an attribute requiring a FUNCTION_DECL;
4725 arguments as in struct attribute_spec.handler. */
4726static tree
e32bac5b
RE
4727arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
4728 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
91d231cb
JM
4729{
4730 if (TREE_CODE (*node) != FUNCTION_DECL)
4731 {
29d08eba
JM
4732 warning (OPT_Wattributes, "%qE attribute only applies to functions",
4733 name);
91d231cb
JM
4734 *no_add_attrs = true;
4735 }
4736
4737 return NULL_TREE;
4738}
4739
4740/* Handle an "interrupt" or "isr" attribute;
4741 arguments as in struct attribute_spec.handler. */
4742static tree
e32bac5b
RE
4743arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
4744 bool *no_add_attrs)
91d231cb
JM
4745{
4746 if (DECL_P (*node))
4747 {
4748 if (TREE_CODE (*node) != FUNCTION_DECL)
4749 {
29d08eba
JM
4750 warning (OPT_Wattributes, "%qE attribute only applies to functions",
4751 name);
91d231cb
JM
4752 *no_add_attrs = true;
4753 }
4754 /* FIXME: the argument if any is checked for type attributes;
4755 should it be checked for decl ones? */
4756 }
4757 else
4758 {
4759 if (TREE_CODE (*node) == FUNCTION_TYPE
4760 || TREE_CODE (*node) == METHOD_TYPE)
4761 {
4762 if (arm_isr_value (args) == ARM_FT_UNKNOWN)
4763 {
29d08eba
JM
4764 warning (OPT_Wattributes, "%qE attribute ignored",
4765 name);
91d231cb
JM
4766 *no_add_attrs = true;
4767 }
4768 }
4769 else if (TREE_CODE (*node) == POINTER_TYPE
4770 && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
4771 || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
4772 && arm_isr_value (args) != ARM_FT_UNKNOWN)
4773 {
8dd16ecc 4774 *node = build_variant_type_copy (*node);
1d6e90ac
NC
4775 TREE_TYPE (*node) = build_type_attribute_variant
4776 (TREE_TYPE (*node),
4777 tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
91d231cb
JM
4778 *no_add_attrs = true;
4779 }
4780 else
4781 {
4782 /* Possibly pass this attribute on from the type to a decl. */
4783 if (flags & ((int) ATTR_FLAG_DECL_NEXT
4784 | (int) ATTR_FLAG_FUNCTION_NEXT
4785 | (int) ATTR_FLAG_ARRAY_NEXT))
4786 {
4787 *no_add_attrs = true;
4788 return tree_cons (name, args, NULL_TREE);
4789 }
4790 else
4791 {
29d08eba
JM
4792 warning (OPT_Wattributes, "%qE attribute ignored",
4793 name);
91d231cb
JM
4794 }
4795 }
4796 }
4797
4798 return NULL_TREE;
82e9d970
PB
4799}
4800
390b17c2
RE
4801/* Handle a "pcs" attribute; arguments as in struct
4802 attribute_spec.handler. */
4803static tree
4804arm_handle_pcs_attribute (tree *node ATTRIBUTE_UNUSED, tree name, tree args,
4805 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4806{
4807 if (arm_pcs_from_attribute (args) == ARM_PCS_UNKNOWN)
4808 {
a9717079 4809 warning (OPT_Wattributes, "%qE attribute ignored", name);
390b17c2
RE
4810 *no_add_attrs = true;
4811 }
4812 return NULL_TREE;
4813}
4814
7bff66a7 4815#if TARGET_DLLIMPORT_DECL_ATTRIBUTES
04fb56d5
MM
4816/* Handle the "notshared" attribute. This attribute is another way of
4817 requesting hidden visibility. ARM's compiler supports
4818 "__declspec(notshared)"; we support the same thing via an
4819 attribute. */
4820
4821static tree
e0b92319
NC
4822arm_handle_notshared_attribute (tree *node,
4823 tree name ATTRIBUTE_UNUSED,
4824 tree args ATTRIBUTE_UNUSED,
4825 int flags ATTRIBUTE_UNUSED,
04fb56d5
MM
4826 bool *no_add_attrs)
4827{
4828 tree decl = TYPE_NAME (*node);
4829
4830 if (decl)
4831 {
4832 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
4833 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4834 *no_add_attrs = false;
4835 }
4836 return NULL_TREE;
4837}
7bff66a7 4838#endif
04fb56d5 4839
82e9d970
PB
4840/* Return 0 if the attributes for two types are incompatible, 1 if they
4841 are compatible, and 2 if they are nearly compatible (which causes a
4842 warning to be generated). */
8d8e52be 4843static int
3101faab 4844arm_comp_type_attributes (const_tree type1, const_tree type2)
82e9d970 4845{
1cb8d58a 4846 int l1, l2, s1, s2;
f676971a 4847
82e9d970
PB
4848 /* Check for mismatch of non-default calling convention. */
4849 if (TREE_CODE (type1) != FUNCTION_TYPE)
4850 return 1;
4851
4852 /* Check for mismatched call attributes. */
1cb8d58a
NC
4853 l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
4854 l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
4855 s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
4856 s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
bd7fc26f
NC
4857
4858 /* Only bother to check if an attribute is defined. */
4859 if (l1 | l2 | s1 | s2)
4860 {
4861 /* If one type has an attribute, the other must have the same attribute. */
1cb8d58a 4862 if ((l1 != l2) || (s1 != s2))
bd7fc26f 4863 return 0;
82e9d970 4864
bd7fc26f
NC
4865 /* Disallow mixed attributes. */
4866 if ((l1 & s2) || (l2 & s1))
4867 return 0;
4868 }
f676971a 4869
6d3d9133
NC
4870 /* Check for mismatched ISR attribute. */
4871 l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
4872 if (! l1)
4873 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
4874 l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
4875 if (! l2)
4876 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
4877 if (l1 != l2)
4878 return 0;
4879
bd7fc26f 4880 return 1;
82e9d970
PB
4881}
4882
c27ba912
DM
4883/* Assigns default attributes to newly defined type. This is used to
4884 set short_call/long_call attributes for function types of
4885 functions defined inside corresponding #pragma scopes. */
8d8e52be 4886static void
e32bac5b 4887arm_set_default_type_attributes (tree type)
c27ba912
DM
4888{
4889 /* Add __attribute__ ((long_call)) to all functions, when
4890 inside #pragma long_calls or __attribute__ ((short_call)),
4891 when inside #pragma no_long_calls. */
4892 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
4893 {
4894 tree type_attr_list, attr_name;
4895 type_attr_list = TYPE_ATTRIBUTES (type);
4896
4897 if (arm_pragma_long_calls == LONG)
4898 attr_name = get_identifier ("long_call");
4899 else if (arm_pragma_long_calls == SHORT)
4900 attr_name = get_identifier ("short_call");
4901 else
4902 return;
4903
4904 type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
4905 TYPE_ATTRIBUTES (type) = type_attr_list;
4906 }
4907}
4908\f
25a65198
RS
4909/* Return true if DECL is known to be linked into section SECTION. */
4910
4911static bool
4912arm_function_in_section_p (tree decl, section *section)
c27ba912 4913{
25a65198
RS
4914 /* We can only be certain about functions defined in the same
4915 compilation unit. */
4916 if (!TREE_STATIC (decl))
4917 return false;
c27ba912 4918
25a65198
RS
4919 /* Make sure that SYMBOL always binds to the definition in this
4920 compilation unit. */
4921 if (!targetm.binds_local_p (decl))
4922 return false;
c27ba912 4923
25a65198
RS
4924 /* If DECL_SECTION_NAME is set, assume it is trustworthy. */
4925 if (!DECL_SECTION_NAME (decl))
4926 {
25a65198
RS
4927 /* Make sure that we will not create a unique section for DECL. */
4928 if (flag_function_sections || DECL_ONE_ONLY (decl))
4929 return false;
4930 }
4931
4932 return function_section (decl) == section;
c27ba912
DM
4933}
4934
a50aa827 4935/* Return nonzero if a 32-bit "long_call" should be generated for
25a65198
RS
4936 a call from the current function to DECL. We generate a long_call
4937 if the function:
c27ba912
DM
4938
4939 a. has an __attribute__((long call))
4940 or b. is within the scope of a #pragma long_calls
4941 or c. the -mlong-calls command line switch has been specified
4942
4943 However we do not generate a long call if the function:
f676971a 4944
c27ba912
DM
4945 d. has an __attribute__ ((short_call))
4946 or e. is inside the scope of a #pragma no_long_calls
25a65198 4947 or f. is defined in the same section as the current function. */
c27ba912 4948
25a65198
RS
4949bool
4950arm_is_long_call_p (tree decl)
4951{
4952 tree attrs;
c27ba912 4953
25a65198
RS
4954 if (!decl)
4955 return TARGET_LONG_CALLS;
c27ba912 4956
25a65198
RS
4957 attrs = TYPE_ATTRIBUTES (TREE_TYPE (decl));
4958 if (lookup_attribute ("short_call", attrs))
4959 return false;
c27ba912 4960
25a65198
RS
4961 /* For "f", be conservative, and only cater for cases in which the
4962 whole of the current function is placed in the same section. */
4963 if (!flag_reorder_blocks_and_partition
b3a796bc 4964 && TREE_CODE (decl) == FUNCTION_DECL
25a65198
RS
4965 && arm_function_in_section_p (decl, current_function_section ()))
4966 return false;
a77655b1 4967
25a65198
RS
4968 if (lookup_attribute ("long_call", attrs))
4969 return true;
f676971a 4970
25a65198 4971 return TARGET_LONG_CALLS;
c27ba912 4972}
f99fce0c 4973
825dda42 4974/* Return nonzero if it is ok to make a tail-call to DECL. */
4977bab6 4975static bool
390b17c2 4976arm_function_ok_for_sibcall (tree decl, tree exp)
f99fce0c 4977{
5b3e6663 4978 unsigned long func_type;
f99fce0c 4979
5a9335ef
NC
4980 if (cfun->machine->sibcall_blocked)
4981 return false;
4982
f99fce0c 4983 /* Never tailcall something for which we have no decl, or if we
7c19c715
JB
4984 are generating code for Thumb-1. */
4985 if (decl == NULL || TARGET_THUMB1)
4977bab6 4986 return false;
f99fce0c 4987
9403b7f7
RS
4988 /* The PIC register is live on entry to VxWorks PLT entries, so we
4989 must make the call before restoring the PIC register. */
4990 if (TARGET_VXWORKS_RTP && flag_pic && !targetm.binds_local_p (decl))
4991 return false;
4992
f99fce0c 4993 /* Cannot tail-call to long calls, since these are out of range of
25a65198
RS
4994 a branch instruction. */
4995 if (arm_is_long_call_p (decl))
4977bab6 4996 return false;
f99fce0c
RE
4997
4998 /* If we are interworking and the function is not declared static
f676971a 4999 then we can't tail-call it unless we know that it exists in this
f99fce0c 5000 compilation unit (since it might be a Thumb routine). */
5895f793 5001 if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
4977bab6 5002 return false;
f99fce0c 5003
5b3e6663 5004 func_type = arm_current_func_type ();
6d3d9133 5005 /* Never tailcall from an ISR routine - it needs a special exit sequence. */
5b3e6663
PB
5006 if (IS_INTERRUPT (func_type))
5007 return false;
5008
390b17c2
RE
5009 if (!VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
5010 {
5011 /* Check that the return value locations are the same. For
5012 example that we aren't returning a value from the sibling in
5013 a VFP register but then need to transfer it to a core
5014 register. */
5015 rtx a, b;
5016
5017 a = arm_function_value (TREE_TYPE (exp), decl, false);
5018 b = arm_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
5019 cfun->decl, false);
5020 if (!rtx_equal_p (a, b))
5021 return false;
5022 }
5023
5b3e6663
PB
5024 /* Never tailcall if function may be called with a misaligned SP. */
5025 if (IS_STACKALIGN (func_type))
4977bab6 5026 return false;
6d3d9133 5027
f99fce0c 5028 /* Everything else is ok. */
4977bab6 5029 return true;
f99fce0c
RE
5030}
5031
82e9d970 5032\f
6b990f6b
RE
5033/* Addressing mode support functions. */
5034
0b4be7de 5035/* Return nonzero if X is a legitimate immediate operand when compiling
020a4035 5036 for PIC. We know that X satisfies CONSTANT_P and flag_pic is true. */
32de079a 5037int
e32bac5b 5038legitimate_pic_operand_p (rtx x)
32de079a 5039{
020a4035
RE
5040 if (GET_CODE (x) == SYMBOL_REF
5041 || (GET_CODE (x) == CONST
5042 && GET_CODE (XEXP (x, 0)) == PLUS
5043 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
32de079a
RE
5044 return 0;
5045
5046 return 1;
5047}
5048
9403b7f7
RS
5049/* Record that the current function needs a PIC register. Initialize
5050 cfun->machine->pic_reg if we have not already done so. */
5051
5052static void
5053require_pic_register (void)
5054{
5055 /* A lot of the logic here is made obscure by the fact that this
5056 routine gets called as part of the rtx cost estimation process.
5057 We don't want those calls to affect any assumptions about the real
5058 function; and further, we can't call entry_of_function() until we
5059 start the real expansion process. */
e3b5732b 5060 if (!crtl->uses_pic_offset_table)
9403b7f7 5061 {
b3a13419 5062 gcc_assert (can_create_pseudo_p ());
9403b7f7
RS
5063 if (arm_pic_register != INVALID_REGNUM)
5064 {
6d2538f5
JB
5065 if (!cfun->machine->pic_reg)
5066 cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
9403b7f7
RS
5067
5068 /* Play games to avoid marking the function as needing pic
5069 if we are being called as part of the cost-estimation
5070 process. */
04ef80ce 5071 if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
e3b5732b 5072 crtl->uses_pic_offset_table = 1;
9403b7f7
RS
5073 }
5074 else
5075 {
5076 rtx seq;
5077
6d2538f5
JB
5078 if (!cfun->machine->pic_reg)
5079 cfun->machine->pic_reg = gen_reg_rtx (Pmode);
9403b7f7
RS
5080
5081 /* Play games to avoid marking the function as needing pic
5082 if we are being called as part of the cost-estimation
5083 process. */
04ef80ce 5084 if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
9403b7f7 5085 {
e3b5732b 5086 crtl->uses_pic_offset_table = 1;
9403b7f7
RS
5087 start_sequence ();
5088
5089 arm_load_pic_register (0UL);
5090
5091 seq = get_insns ();
5092 end_sequence ();
af618949
MM
5093 /* We can be called during expansion of PHI nodes, where
5094 we can't yet emit instructions directly in the final
5095 insn stream. Queue the insns on the entry edge, they will
5096 be committed after everything else is expanded. */
5097 insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
9403b7f7
RS
5098 }
5099 }
5100 }
5101}
5102
32de079a 5103rtx
e32bac5b 5104legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
32de079a 5105{
a3c48721
RE
5106 if (GET_CODE (orig) == SYMBOL_REF
5107 || GET_CODE (orig) == LABEL_REF)
32de079a 5108 {
32de079a 5109 rtx insn;
020a4035 5110
32de079a
RE
5111 if (reg == 0)
5112 {
b3a13419 5113 gcc_assert (can_create_pseudo_p ());
e6d29d15 5114 reg = gen_reg_rtx (Pmode);
32de079a 5115 }
32de079a 5116
9403b7f7
RS
5117 /* VxWorks does not impose a fixed gap between segments; the run-time
5118 gap can be different from the object-file gap. We therefore can't
5119 use GOTOFF unless we are absolutely sure that the symbol is in the
5120 same segment as the GOT. Unfortunately, the flexibility of linker
5121 scripts means that we can't be sure of that in general, so assume
5122 that GOTOFF is never valid on VxWorks. */
14f583b8 5123 if ((GET_CODE (orig) == LABEL_REF
f676971a 5124 || (GET_CODE (orig) == SYMBOL_REF &&
94428622 5125 SYMBOL_REF_LOCAL_P (orig)))
9403b7f7
RS
5126 && NEED_GOT_RELOC
5127 && !TARGET_VXWORKS_RTP)
85c9bcd4 5128 insn = arm_pic_static_addr (orig, reg);
a3c48721
RE
5129 else
5130 {
d37c3c62
MK
5131 rtx pat;
5132 rtx mem;
5133
85c9bcd4
WG
5134 /* If this function doesn't have a pic register, create one now. */
5135 require_pic_register ();
5136
d37c3c62 5137 pat = gen_calculate_pic_address (reg, cfun->machine->pic_reg, orig);
85c9bcd4 5138
d37c3c62
MK
5139 /* Make the MEM as close to a constant as possible. */
5140 mem = SET_SRC (pat);
5141 gcc_assert (MEM_P (mem) && !MEM_VOLATILE_P (mem));
5142 MEM_READONLY_P (mem) = 1;
5143 MEM_NOTRAP_P (mem) = 1;
5144
5145 insn = emit_insn (pat);
a3c48721
RE
5146 }
5147
32de079a
RE
5148 /* Put a REG_EQUAL note on this insn, so that it can be optimized
5149 by loop. */
bd94cb6e
SB
5150 set_unique_reg_note (insn, REG_EQUAL, orig);
5151
32de079a
RE
5152 return reg;
5153 }
5154 else if (GET_CODE (orig) == CONST)
5155 {
5156 rtx base, offset;
5157
5158 if (GET_CODE (XEXP (orig, 0)) == PLUS
020a4035 5159 && XEXP (XEXP (orig, 0), 0) == cfun->machine->pic_reg)
32de079a
RE
5160 return orig;
5161
f67358da 5162 /* Handle the case where we have: const (UNSPEC_TLS). */
d3585b76
DJ
5163 if (GET_CODE (XEXP (orig, 0)) == UNSPEC
5164 && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
5165 return orig;
5166
f67358da
PB
5167 /* Handle the case where we have:
5168 const (plus (UNSPEC_TLS) (ADDEND)). The ADDEND must be a
5169 CONST_INT. */
5170 if (GET_CODE (XEXP (orig, 0)) == PLUS
5171 && GET_CODE (XEXP (XEXP (orig, 0), 0)) == UNSPEC
5172 && XINT (XEXP (XEXP (orig, 0), 0), 1) == UNSPEC_TLS)
5173 {
5174 gcc_assert (GET_CODE (XEXP (XEXP (orig, 0), 1)) == CONST_INT);
5175 return orig;
5176 }
5177
32de079a
RE
5178 if (reg == 0)
5179 {
b3a13419 5180 gcc_assert (can_create_pseudo_p ());
e6d29d15 5181 reg = gen_reg_rtx (Pmode);
32de079a
RE
5182 }
5183
e6d29d15 5184 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
e0b92319 5185
e6d29d15
NS
5186 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
5187 offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
5188 base == reg ? 0 : reg);
32de079a
RE
5189
5190 if (GET_CODE (offset) == CONST_INT)
5191 {
5192 /* The base register doesn't really matter, we only want to
5193 test the index for the appropriate mode. */
1e1ab407 5194 if (!arm_legitimate_index_p (mode, offset, SET, 0))
6b990f6b 5195 {
b3a13419 5196 gcc_assert (can_create_pseudo_p ());
e6d29d15 5197 offset = force_reg (Pmode, offset);
6b990f6b 5198 }
32de079a 5199
32de079a 5200 if (GET_CODE (offset) == CONST_INT)
ed8908e7 5201 return plus_constant (base, INTVAL (offset));
32de079a
RE
5202 }
5203
5204 if (GET_MODE_SIZE (mode) > 4
5205 && (GET_MODE_CLASS (mode) == MODE_INT
5206 || TARGET_SOFT_FLOAT))
5207 {
5208 emit_insn (gen_addsi3 (reg, base, offset));
5209 return reg;
5210 }
5211
43cffd11 5212 return gen_rtx_PLUS (Pmode, base, offset);
32de079a 5213 }
32de079a
RE
5214
5215 return orig;
5216}
5217
57934c39 5218
5b3e6663 5219/* Find a spare register to use during the prolog of a function. */
57934c39
PB
5220
5221static int
b279b20a 5222thumb_find_work_register (unsigned long pushed_regs_mask)
57934c39
PB
5223{
5224 int reg;
5225
b279b20a
NC
5226 /* Check the argument registers first as these are call-used. The
5227 register allocation order means that sometimes r3 might be used
5228 but earlier argument registers might not, so check them all. */
5229 for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
6fb5fa3c 5230 if (!df_regs_ever_live_p (reg))
b279b20a
NC
5231 return reg;
5232
5233 /* Before going on to check the call-saved registers we can try a couple
5234 more ways of deducing that r3 is available. The first is when we are
5235 pushing anonymous arguments onto the stack and we have less than 4
5236 registers worth of fixed arguments(*). In this case r3 will be part of
5237 the variable argument list and so we can be sure that it will be
5238 pushed right at the start of the function. Hence it will be available
5239 for the rest of the prologue.
38173d38 5240 (*): ie crtl->args.pretend_args_size is greater than 0. */
b279b20a 5241 if (cfun->machine->uses_anonymous_args
38173d38 5242 && crtl->args.pretend_args_size > 0)
57934c39
PB
5243 return LAST_ARG_REGNUM;
5244
b279b20a
NC
5245 /* The other case is when we have fixed arguments but less than 4 registers
5246 worth. In this case r3 might be used in the body of the function, but
5247 it is not being used to convey an argument into the function. In theory
38173d38 5248 we could just check crtl->args.size to see how many bytes are
b279b20a
NC
5249 being passed in argument registers, but it seems that it is unreliable.
5250 Sometimes it will have the value 0 when in fact arguments are being
5251 passed. (See testcase execute/20021111-1.c for an example). So we also
5252 check the args_info.nregs field as well. The problem with this field is
5253 that it makes no allowances for arguments that are passed to the
5254 function but which are not used. Hence we could miss an opportunity
5255 when a function has an unused argument in r3. But it is better to be
5256 safe than to be sorry. */
5257 if (! cfun->machine->uses_anonymous_args
38173d38
JH
5258 && crtl->args.size >= 0
5259 && crtl->args.size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
f6d2671e 5260 && crtl->args.info.nregs < 4)
b279b20a 5261 return LAST_ARG_REGNUM;
e0b92319 5262
b279b20a
NC
5263 /* Otherwise look for a call-saved register that is going to be pushed. */
5264 for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
5265 if (pushed_regs_mask & (1 << reg))
57934c39
PB
5266 return reg;
5267
5b3e6663
PB
5268 if (TARGET_THUMB2)
5269 {
5270 /* Thumb-2 can use high regs. */
5271 for (reg = FIRST_HI_REGNUM; reg < 15; reg ++)
5272 if (pushed_regs_mask & (1 << reg))
5273 return reg;
5274 }
b279b20a
NC
5275 /* Something went wrong - thumb_compute_save_reg_mask()
5276 should have arranged for a suitable register to be pushed. */
e6d29d15 5277 gcc_unreachable ();
57934c39
PB
5278}
5279
f16fe45f 5280static GTY(()) int pic_labelno;
876f13b0 5281
fe013435
PB
5282/* Generate code to load the PIC register. In thumb mode SCRATCH is a
5283 low register. */
876f13b0 5284
32de079a 5285void
e55ef7f4 5286arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
32de079a 5287{
f9bd1a89 5288 rtx l1, labelno, pic_tmp, pic_rtx, pic_reg;
32de079a 5289
e3b5732b 5290 if (crtl->uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
32de079a
RE
5291 return;
5292
e6d29d15 5293 gcc_assert (flag_pic);
32de079a 5294
9403b7f7
RS
5295 pic_reg = cfun->machine->pic_reg;
5296 if (TARGET_VXWORKS_RTP)
5297 {
5298 pic_rtx = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE);
5299 pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
87d05b44 5300 emit_insn (gen_pic_load_addr_32bit (pic_reg, pic_rtx));
43cffd11 5301
9403b7f7 5302 emit_insn (gen_rtx_SET (Pmode, pic_reg, gen_rtx_MEM (Pmode, pic_reg)));
f676971a 5303
9403b7f7
RS
5304 pic_tmp = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
5305 emit_insn (gen_pic_offset_arm (pic_reg, pic_reg, pic_tmp));
4bec9f7d 5306 }
9403b7f7 5307 else
5b3e6663 5308 {
9403b7f7
RS
5309 /* We use an UNSPEC rather than a LABEL_REF because this label
5310 never appears in the code stream. */
5311
5312 labelno = GEN_INT (pic_labelno++);
5313 l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
5314 l1 = gen_rtx_CONST (VOIDmode, l1);
5315
9403b7f7
RS
5316 /* On the ARM the PC register contains 'dot + 8' at the time of the
5317 addition, on the Thumb it is 'dot + 4'. */
f9bd1a89
RS
5318 pic_rtx = plus_constant (l1, TARGET_ARM ? 8 : 4);
5319 pic_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, pic_rtx),
5320 UNSPEC_GOTSYM_OFF);
9403b7f7
RS
5321 pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
5322
87d05b44 5323 if (TARGET_32BIT)
9403b7f7 5324 {
87d05b44
RE
5325 emit_insn (gen_pic_load_addr_32bit (pic_reg, pic_rtx));
5326 if (TARGET_ARM)
5327 emit_insn (gen_pic_add_dot_plus_eight (pic_reg, pic_reg, labelno));
9403b7f7 5328 else
87d05b44 5329 emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
9403b7f7
RS
5330 }
5331 else /* TARGET_THUMB1 */
876f13b0 5332 {
9403b7f7
RS
5333 if (arm_pic_register != INVALID_REGNUM
5334 && REGNO (pic_reg) > LAST_LO_REGNUM)
5335 {
5336 /* We will have pushed the pic register, so we should always be
5337 able to find a work register. */
5338 pic_tmp = gen_rtx_REG (SImode,
5339 thumb_find_work_register (saved_regs));
5340 emit_insn (gen_pic_load_addr_thumb1 (pic_tmp, pic_rtx));
5341 emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
5342 }
5343 else
5344 emit_insn (gen_pic_load_addr_thumb1 (pic_reg, pic_rtx));
5345 emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
876f13b0 5346 }
4bec9f7d 5347 }
32de079a 5348
32de079a
RE
5349 /* Need to emit this whether or not we obey regdecls,
5350 since setjmp/longjmp can cause life info to screw up. */
c41c1387 5351 emit_use (pic_reg);
32de079a
RE
5352}
5353
85c9bcd4
WG
5354/* Generate code to load the address of a static var when flag_pic is set. */
5355static rtx
5356arm_pic_static_addr (rtx orig, rtx reg)
5357{
5358 rtx l1, labelno, offset_rtx, insn;
5359
5360 gcc_assert (flag_pic);
5361
5362 /* We use an UNSPEC rather than a LABEL_REF because this label
5363 never appears in the code stream. */
5364 labelno = GEN_INT (pic_labelno++);
5365 l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
5366 l1 = gen_rtx_CONST (VOIDmode, l1);
5367
5368 /* On the ARM the PC register contains 'dot + 8' at the time of the
5369 addition, on the Thumb it is 'dot + 4'. */
5370 offset_rtx = plus_constant (l1, TARGET_ARM ? 8 : 4);
5371 offset_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, orig, offset_rtx),
5372 UNSPEC_SYMBOL_OFFSET);
5373 offset_rtx = gen_rtx_CONST (Pmode, offset_rtx);
5374
5375 if (TARGET_32BIT)
5376 {
5377 emit_insn (gen_pic_load_addr_32bit (reg, offset_rtx));
5378 if (TARGET_ARM)
5379 insn = emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
5380 else
5381 insn = emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5382 }
5383 else /* TARGET_THUMB1 */
5384 {
5385 emit_insn (gen_pic_load_addr_thumb1 (reg, offset_rtx));
5386 insn = emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5387 }
5388
5389 return insn;
5390}
876f13b0 5391
6b990f6b
RE
5392/* Return nonzero if X is valid as an ARM state addressing register. */
5393static int
e32bac5b 5394arm_address_register_rtx_p (rtx x, int strict_p)
6b990f6b
RE
5395{
5396 int regno;
5397
5398 if (GET_CODE (x) != REG)
5399 return 0;
5400
5401 regno = REGNO (x);
5402
5403 if (strict_p)
5404 return ARM_REGNO_OK_FOR_BASE_P (regno);
5405
5406 return (regno <= LAST_ARM_REGNUM
5407 || regno >= FIRST_PSEUDO_REGISTER
5408 || regno == FRAME_POINTER_REGNUM
5409 || regno == ARG_POINTER_REGNUM);
5410}
5411
d3585b76
DJ
5412/* Return TRUE if this rtx is the difference of a symbol and a label,
5413 and will reduce to a PC-relative relocation in the object file.
5414 Expressions like this can be left alone when generating PIC, rather
5415 than forced through the GOT. */
5416static int
5417pcrel_constant_p (rtx x)
5418{
5419 if (GET_CODE (x) == MINUS)
5420 return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
5421
5422 return FALSE;
5423}
5424
d37c3c62
MK
5425/* Return true if X will surely end up in an index register after next
5426 splitting pass. */
5427static bool
5428will_be_in_index_register (const_rtx x)
5429{
5430 /* arm.md: calculate_pic_address will split this into a register. */
5431 return GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_SYM;
5432}
5433
6b990f6b
RE
5434/* Return nonzero if X is a valid ARM state address operand. */
5435int
c6c3dba9
PB
5436arm_legitimate_address_outer_p (enum machine_mode mode, rtx x, RTX_CODE outer,
5437 int strict_p)
6b990f6b 5438{
fdd695fd
PB
5439 bool use_ldrd;
5440 enum rtx_code code = GET_CODE (x);
f676971a 5441
6b990f6b
RE
5442 if (arm_address_register_rtx_p (x, strict_p))
5443 return 1;
5444
fdd695fd
PB
5445 use_ldrd = (TARGET_LDRD
5446 && (mode == DImode
5447 || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
5448
5449 if (code == POST_INC || code == PRE_DEC
5450 || ((code == PRE_INC || code == POST_DEC)
5451 && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
6b990f6b
RE
5452 return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
5453
fdd695fd 5454 else if ((code == POST_MODIFY || code == PRE_MODIFY)
6b990f6b
RE
5455 && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
5456 && GET_CODE (XEXP (x, 1)) == PLUS
386d3a16 5457 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
fdd695fd
PB
5458 {
5459 rtx addend = XEXP (XEXP (x, 1), 1);
5460
112cdef5 5461 /* Don't allow ldrd post increment by register because it's hard
fdd695fd
PB
5462 to fixup invalid register choices. */
5463 if (use_ldrd
5464 && GET_CODE (x) == POST_MODIFY
5465 && GET_CODE (addend) == REG)
5466 return 0;
5467
5468 return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
5469 && arm_legitimate_index_p (mode, addend, outer, strict_p));
5470 }
6b990f6b
RE
5471
5472 /* After reload constants split into minipools will have addresses
5473 from a LABEL_REF. */
0bfb39ef 5474 else if (reload_completed
fdd695fd
PB
5475 && (code == LABEL_REF
5476 || (code == CONST
6b990f6b
RE
5477 && GET_CODE (XEXP (x, 0)) == PLUS
5478 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5479 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5480 return 1;
5481
88f77cba 5482 else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
6b990f6b
RE
5483 return 0;
5484
fdd695fd 5485 else if (code == PLUS)
6b990f6b
RE
5486 {
5487 rtx xop0 = XEXP (x, 0);
5488 rtx xop1 = XEXP (x, 1);
5489
5490 return ((arm_address_register_rtx_p (xop0, strict_p)
d37c3c62
MK
5491 && ((GET_CODE(xop1) == CONST_INT
5492 && arm_legitimate_index_p (mode, xop1, outer, strict_p))
5493 || (!strict_p && will_be_in_index_register (xop1))))
6b990f6b 5494 || (arm_address_register_rtx_p (xop1, strict_p)
1e1ab407 5495 && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
6b990f6b
RE
5496 }
5497
5498#if 0
5499 /* Reload currently can't handle MINUS, so disable this for now */
5500 else if (GET_CODE (x) == MINUS)
5501 {
5502 rtx xop0 = XEXP (x, 0);
5503 rtx xop1 = XEXP (x, 1);
5504
5505 return (arm_address_register_rtx_p (xop0, strict_p)
1e1ab407 5506 && arm_legitimate_index_p (mode, xop1, outer, strict_p));
6b990f6b
RE
5507 }
5508#endif
5509
5510 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
fdd695fd 5511 && code == SYMBOL_REF
6b990f6b
RE
5512 && CONSTANT_POOL_ADDRESS_P (x)
5513 && ! (flag_pic
d3585b76
DJ
5514 && symbol_mentioned_p (get_pool_constant (x))
5515 && ! pcrel_constant_p (get_pool_constant (x))))
6b990f6b
RE
5516 return 1;
5517
6b990f6b
RE
5518 return 0;
5519}
5520
5b3e6663 5521/* Return nonzero if X is a valid Thumb-2 address operand. */
c6c3dba9 5522static int
5b3e6663
PB
5523thumb2_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
5524{
5525 bool use_ldrd;
5526 enum rtx_code code = GET_CODE (x);
5527
5528 if (arm_address_register_rtx_p (x, strict_p))
5529 return 1;
5530
5531 use_ldrd = (TARGET_LDRD
5532 && (mode == DImode
5533 || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
5534
5535 if (code == POST_INC || code == PRE_DEC
5536 || ((code == PRE_INC || code == POST_DEC)
5537 && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
5538 return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
5539
5540 else if ((code == POST_MODIFY || code == PRE_MODIFY)
5541 && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
5542 && GET_CODE (XEXP (x, 1)) == PLUS
5543 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5544 {
5545 /* Thumb-2 only has autoincrement by constant. */
5546 rtx addend = XEXP (XEXP (x, 1), 1);
5547 HOST_WIDE_INT offset;
5548
5549 if (GET_CODE (addend) != CONST_INT)
5550 return 0;
5551
5552 offset = INTVAL(addend);
5553 if (GET_MODE_SIZE (mode) <= 4)
5554 return (offset > -256 && offset < 256);
5555
5556 return (use_ldrd && offset > -1024 && offset < 1024
5557 && (offset & 3) == 0);
5558 }
5559
5560 /* After reload constants split into minipools will have addresses
5561 from a LABEL_REF. */
5562 else if (reload_completed
5563 && (code == LABEL_REF
5564 || (code == CONST
5565 && GET_CODE (XEXP (x, 0)) == PLUS
5566 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5567 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5568 return 1;
5569
88f77cba 5570 else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
5b3e6663
PB
5571 return 0;
5572
5573 else if (code == PLUS)
5574 {
5575 rtx xop0 = XEXP (x, 0);
5576 rtx xop1 = XEXP (x, 1);
5577
5578 return ((arm_address_register_rtx_p (xop0, strict_p)
d37c3c62
MK
5579 && (thumb2_legitimate_index_p (mode, xop1, strict_p)
5580 || (!strict_p && will_be_in_index_register (xop1))))
5b3e6663
PB
5581 || (arm_address_register_rtx_p (xop1, strict_p)
5582 && thumb2_legitimate_index_p (mode, xop0, strict_p)));
5583 }
5584
5585 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
5586 && code == SYMBOL_REF
5587 && CONSTANT_POOL_ADDRESS_P (x)
5588 && ! (flag_pic
5589 && symbol_mentioned_p (get_pool_constant (x))
5590 && ! pcrel_constant_p (get_pool_constant (x))))
5591 return 1;
5592
5593 return 0;
5594}
5595
6b990f6b
RE
5596/* Return nonzero if INDEX is valid for an address index operand in
5597 ARM state. */
5598static int
1e1ab407
RE
5599arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
5600 int strict_p)
6b990f6b
RE
5601{
5602 HOST_WIDE_INT range;
5603 enum rtx_code code = GET_CODE (index);
5604
778ebdd9
PB
5605 /* Standard coprocessor addressing modes. */
5606 if (TARGET_HARD_FLOAT
5607 && (TARGET_FPA || TARGET_MAVERICK)
5608 && (GET_MODE_CLASS (mode) == MODE_FLOAT
5609 || (TARGET_MAVERICK && mode == DImode)))
6b990f6b
RE
5610 return (code == CONST_INT && INTVAL (index) < 1024
5611 && INTVAL (index) > -1024
5612 && (INTVAL (index) & 3) == 0);
5613
88f77cba
JB
5614 if (TARGET_NEON
5615 && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode)))
5616 return (code == CONST_INT
5617 && INTVAL (index) < 1016
5618 && INTVAL (index) > -1024
5619 && (INTVAL (index) & 3) == 0);
5620
5a9335ef
NC
5621 if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
5622 return (code == CONST_INT
3657dc3e
PB
5623 && INTVAL (index) < 1024
5624 && INTVAL (index) > -1024
5625 && (INTVAL (index) & 3) == 0);
5a9335ef 5626
fdd695fd
PB
5627 if (arm_address_register_rtx_p (index, strict_p)
5628 && (GET_MODE_SIZE (mode) <= 4))
5629 return 1;
5630
5631 if (mode == DImode || mode == DFmode)
5632 {
5633 if (code == CONST_INT)
5634 {
5635 HOST_WIDE_INT val = INTVAL (index);
5636
5637 if (TARGET_LDRD)
5638 return val > -256 && val < 256;
5639 else
f372c932 5640 return val > -4096 && val < 4092;
fdd695fd
PB
5641 }
5642
5643 return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
5644 }
5645
6b990f6b 5646 if (GET_MODE_SIZE (mode) <= 4
1e1ab407
RE
5647 && ! (arm_arch4
5648 && (mode == HImode
0fd8c3ad 5649 || mode == HFmode
1e1ab407 5650 || (mode == QImode && outer == SIGN_EXTEND))))
6b990f6b 5651 {
1e1ab407
RE
5652 if (code == MULT)
5653 {
5654 rtx xiop0 = XEXP (index, 0);
5655 rtx xiop1 = XEXP (index, 1);
5656
5657 return ((arm_address_register_rtx_p (xiop0, strict_p)
5658 && power_of_two_operand (xiop1, SImode))
5659 || (arm_address_register_rtx_p (xiop1, strict_p)
5660 && power_of_two_operand (xiop0, SImode)));
5661 }
5662 else if (code == LSHIFTRT || code == ASHIFTRT
5663 || code == ASHIFT || code == ROTATERT)
5664 {
5665 rtx op = XEXP (index, 1);
6b990f6b 5666
1e1ab407
RE
5667 return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
5668 && GET_CODE (op) == CONST_INT
5669 && INTVAL (op) > 0
5670 && INTVAL (op) <= 31);
5671 }
6b990f6b
RE
5672 }
5673
1e1ab407
RE
5674 /* For ARM v4 we may be doing a sign-extend operation during the
5675 load. */
e1471c91 5676 if (arm_arch4)
1e1ab407 5677 {
0fd8c3ad
SL
5678 if (mode == HImode
5679 || mode == HFmode
5680 || (outer == SIGN_EXTEND && mode == QImode))
1e1ab407
RE
5681 range = 256;
5682 else
5683 range = 4096;
5684 }
e1471c91 5685 else
0fd8c3ad 5686 range = (mode == HImode || mode == HFmode) ? 4095 : 4096;
6b990f6b
RE
5687
5688 return (code == CONST_INT
5689 && INTVAL (index) < range
5690 && INTVAL (index) > -range);
76a318e9
RE
5691}
5692
5b3e6663
PB
5693/* Return true if OP is a valid index scaling factor for Thumb-2 address
5694 index operand. i.e. 1, 2, 4 or 8. */
5695static bool
5696thumb2_index_mul_operand (rtx op)
5697{
5698 HOST_WIDE_INT val;
5699
5700 if (GET_CODE(op) != CONST_INT)
5701 return false;
5702
5703 val = INTVAL(op);
5704 return (val == 1 || val == 2 || val == 4 || val == 8);
5705}
5706
5707/* Return nonzero if INDEX is a valid Thumb-2 address index operand. */
5708static int
5709thumb2_legitimate_index_p (enum machine_mode mode, rtx index, int strict_p)
5710{
5711 enum rtx_code code = GET_CODE (index);
5712
5713 /* ??? Combine arm and thumb2 coprocessor addressing modes. */
5714 /* Standard coprocessor addressing modes. */
5715 if (TARGET_HARD_FLOAT
5716 && (TARGET_FPA || TARGET_MAVERICK)
5717 && (GET_MODE_CLASS (mode) == MODE_FLOAT
5718 || (TARGET_MAVERICK && mode == DImode)))
5719 return (code == CONST_INT && INTVAL (index) < 1024
5720 && INTVAL (index) > -1024
5721 && (INTVAL (index) & 3) == 0);
5722
5723 if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
fe2d934b
PB
5724 {
5725 /* For DImode assume values will usually live in core regs
5726 and only allow LDRD addressing modes. */
5727 if (!TARGET_LDRD || mode != DImode)
5728 return (code == CONST_INT
5729 && INTVAL (index) < 1024
5730 && INTVAL (index) > -1024
5731 && (INTVAL (index) & 3) == 0);
5732 }
5b3e6663 5733
88f77cba
JB
5734 if (TARGET_NEON
5735 && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode)))
5736 return (code == CONST_INT
5737 && INTVAL (index) < 1016
5738 && INTVAL (index) > -1024
5739 && (INTVAL (index) & 3) == 0);
5740
5b3e6663
PB
5741 if (arm_address_register_rtx_p (index, strict_p)
5742 && (GET_MODE_SIZE (mode) <= 4))
5743 return 1;
5744
5745 if (mode == DImode || mode == DFmode)
5746 {
e07e020b
RR
5747 if (code == CONST_INT)
5748 {
5749 HOST_WIDE_INT val = INTVAL (index);
5750 /* ??? Can we assume ldrd for thumb2? */
5751 /* Thumb-2 ldrd only has reg+const addressing modes. */
5752 /* ldrd supports offsets of +-1020.
5753 However the ldr fallback does not. */
5754 return val > -256 && val < 256 && (val & 3) == 0;
5755 }
5756 else
5b3e6663 5757 return 0;
5b3e6663
PB
5758 }
5759
5760 if (code == MULT)
5761 {
5762 rtx xiop0 = XEXP (index, 0);
5763 rtx xiop1 = XEXP (index, 1);
5764
5765 return ((arm_address_register_rtx_p (xiop0, strict_p)
5766 && thumb2_index_mul_operand (xiop1))
5767 || (arm_address_register_rtx_p (xiop1, strict_p)
5768 && thumb2_index_mul_operand (xiop0)));
5769 }
5770 else if (code == ASHIFT)
5771 {
5772 rtx op = XEXP (index, 1);
5773
5774 return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
5775 && GET_CODE (op) == CONST_INT
5776 && INTVAL (op) > 0
5777 && INTVAL (op) <= 3);
5778 }
5779
5780 return (code == CONST_INT
5781 && INTVAL (index) < 4096
5782 && INTVAL (index) > -256);
5783}
5784
5785/* Return nonzero if X is valid as a 16-bit Thumb state base register. */
76a318e9 5786static int
5b3e6663 5787thumb1_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
76a318e9
RE
5788{
5789 int regno;
5790
5791 if (GET_CODE (x) != REG)
5792 return 0;
5793
5794 regno = REGNO (x);
5795
5796 if (strict_p)
5b3e6663 5797 return THUMB1_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
76a318e9
RE
5798
5799 return (regno <= LAST_LO_REGNUM
07e58265 5800 || regno > LAST_VIRTUAL_REGISTER
76a318e9
RE
5801 || regno == FRAME_POINTER_REGNUM
5802 || (GET_MODE_SIZE (mode) >= 4
5803 && (regno == STACK_POINTER_REGNUM
edf7cee8 5804 || regno >= FIRST_PSEUDO_REGISTER
76a318e9
RE
5805 || x == hard_frame_pointer_rtx
5806 || x == arg_pointer_rtx)));
5807}
5808
5809/* Return nonzero if x is a legitimate index register. This is the case
5810 for any base register that can access a QImode object. */
5811inline static int
5b3e6663 5812thumb1_index_register_rtx_p (rtx x, int strict_p)
76a318e9 5813{
5b3e6663 5814 return thumb1_base_register_rtx_p (x, QImode, strict_p);
76a318e9
RE
5815}
5816
5b3e6663 5817/* Return nonzero if x is a legitimate 16-bit Thumb-state address.
f676971a 5818
76a318e9
RE
5819 The AP may be eliminated to either the SP or the FP, so we use the
5820 least common denominator, e.g. SImode, and offsets from 0 to 64.
5821
5822 ??? Verify whether the above is the right approach.
5823
5824 ??? Also, the FP may be eliminated to the SP, so perhaps that
5825 needs special handling also.
5826
5827 ??? Look at how the mips16 port solves this problem. It probably uses
5828 better ways to solve some of these problems.
5829
5830 Although it is not incorrect, we don't accept QImode and HImode
5831 addresses based on the frame pointer or arg pointer until the
5832 reload pass starts. This is so that eliminating such addresses
5833 into stack based ones won't produce impossible code. */
c6c3dba9 5834static int
5b3e6663 5835thumb1_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
76a318e9
RE
5836{
5837 /* ??? Not clear if this is right. Experiment. */
5838 if (GET_MODE_SIZE (mode) < 4
5839 && !(reload_in_progress || reload_completed)
5840 && (reg_mentioned_p (frame_pointer_rtx, x)
5841 || reg_mentioned_p (arg_pointer_rtx, x)
5842 || reg_mentioned_p (virtual_incoming_args_rtx, x)
5843 || reg_mentioned_p (virtual_outgoing_args_rtx, x)
5844 || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
5845 || reg_mentioned_p (virtual_stack_vars_rtx, x)))
5846 return 0;
5847
5848 /* Accept any base register. SP only in SImode or larger. */
5b3e6663 5849 else if (thumb1_base_register_rtx_p (x, mode, strict_p))
76a318e9
RE
5850 return 1;
5851
18dbd950 5852 /* This is PC relative data before arm_reorg runs. */
76a318e9
RE
5853 else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
5854 && GET_CODE (x) == SYMBOL_REF
020a4035 5855 && CONSTANT_POOL_ADDRESS_P (x) && !flag_pic)
76a318e9
RE
5856 return 1;
5857
18dbd950 5858 /* This is PC relative data after arm_reorg runs. */
0fd8c3ad
SL
5859 else if ((GET_MODE_SIZE (mode) >= 4 || mode == HFmode)
5860 && reload_completed
76a318e9
RE
5861 && (GET_CODE (x) == LABEL_REF
5862 || (GET_CODE (x) == CONST
5863 && GET_CODE (XEXP (x, 0)) == PLUS
5864 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5865 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5866 return 1;
5867
5868 /* Post-inc indexing only supported for SImode and larger. */
5869 else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
5b3e6663 5870 && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p))
76a318e9
RE
5871 return 1;
5872
5873 else if (GET_CODE (x) == PLUS)
5874 {
5875 /* REG+REG address can be any two index registers. */
5876 /* We disallow FRAME+REG addressing since we know that FRAME
5877 will be replaced with STACK, and SP relative addressing only
5878 permits SP+OFFSET. */
5879 if (GET_MODE_SIZE (mode) <= 4
5880 && XEXP (x, 0) != frame_pointer_rtx
5881 && XEXP (x, 1) != frame_pointer_rtx
5b3e6663 5882 && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
d37c3c62
MK
5883 && (thumb1_index_register_rtx_p (XEXP (x, 1), strict_p)
5884 || (!strict_p && will_be_in_index_register (XEXP (x, 1)))))
76a318e9
RE
5885 return 1;
5886
5887 /* REG+const has 5-7 bit offset for non-SP registers. */
5b3e6663 5888 else if ((thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
76a318e9
RE
5889 || XEXP (x, 0) == arg_pointer_rtx)
5890 && GET_CODE (XEXP (x, 1)) == CONST_INT
5891 && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
5892 return 1;
5893
a50aa827 5894 /* REG+const has 10-bit offset for SP, but only SImode and
76a318e9
RE
5895 larger is supported. */
5896 /* ??? Should probably check for DI/DFmode overflow here
5897 just like GO_IF_LEGITIMATE_OFFSET does. */
5898 else if (GET_CODE (XEXP (x, 0)) == REG
5899 && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
5900 && GET_MODE_SIZE (mode) >= 4
5901 && GET_CODE (XEXP (x, 1)) == CONST_INT
5902 && INTVAL (XEXP (x, 1)) >= 0
5903 && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
5904 && (INTVAL (XEXP (x, 1)) & 3) == 0)
5905 return 1;
5906
5907 else if (GET_CODE (XEXP (x, 0)) == REG
c5289e45
RE
5908 && (REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
5909 || REGNO (XEXP (x, 0)) == ARG_POINTER_REGNUM
5910 || (REGNO (XEXP (x, 0)) >= FIRST_VIRTUAL_REGISTER
32990d5b
JJ
5911 && REGNO (XEXP (x, 0))
5912 <= LAST_VIRTUAL_POINTER_REGISTER))
76a318e9
RE
5913 && GET_MODE_SIZE (mode) >= 4
5914 && GET_CODE (XEXP (x, 1)) == CONST_INT
5915 && (INTVAL (XEXP (x, 1)) & 3) == 0)
5916 return 1;
5917 }
5918
5919 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
f954388e 5920 && GET_MODE_SIZE (mode) == 4
76a318e9
RE
5921 && GET_CODE (x) == SYMBOL_REF
5922 && CONSTANT_POOL_ADDRESS_P (x)
d3585b76
DJ
5923 && ! (flag_pic
5924 && symbol_mentioned_p (get_pool_constant (x))
5925 && ! pcrel_constant_p (get_pool_constant (x))))
76a318e9
RE
5926 return 1;
5927
5928 return 0;
5929}
5930
5931/* Return nonzero if VAL can be used as an offset in a Thumb-state address
5932 instruction of mode MODE. */
5933int
e32bac5b 5934thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
76a318e9
RE
5935{
5936 switch (GET_MODE_SIZE (mode))
5937 {
5938 case 1:
5939 return val >= 0 && val < 32;
5940
5941 case 2:
5942 return val >= 0 && val < 64 && (val & 1) == 0;
5943
5944 default:
5945 return (val >= 0
5946 && (val + GET_MODE_SIZE (mode)) <= 128
5947 && (val & 3) == 0);
5948 }
5949}
5950
c6c3dba9
PB
5951bool
5952arm_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
5953{
5954 if (TARGET_ARM)
5955 return arm_legitimate_address_outer_p (mode, x, SET, strict_p);
5956 else if (TARGET_THUMB2)
5957 return thumb2_legitimate_address_p (mode, x, strict_p);
5958 else /* if (TARGET_THUMB1) */
5959 return thumb1_legitimate_address_p (mode, x, strict_p);
5960}
5961
d3585b76
DJ
5962/* Build the SYMBOL_REF for __tls_get_addr. */
5963
5964static GTY(()) rtx tls_get_addr_libfunc;
5965
5966static rtx
5967get_tls_get_addr (void)
5968{
5969 if (!tls_get_addr_libfunc)
5970 tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
5971 return tls_get_addr_libfunc;
5972}
5973
5974static rtx
5975arm_load_tp (rtx target)
5976{
5977 if (!target)
5978 target = gen_reg_rtx (SImode);
5979
5980 if (TARGET_HARD_TP)
5981 {
5982 /* Can return in any reg. */
5983 emit_insn (gen_load_tp_hard (target));
5984 }
5985 else
5986 {
5987 /* Always returned in r0. Immediately copy the result into a pseudo,
5988 otherwise other uses of r0 (e.g. setting up function arguments) may
5989 clobber the value. */
5990
5991 rtx tmp;
5992
5993 emit_insn (gen_load_tp_soft ());
5994
5995 tmp = gen_rtx_REG (SImode, 0);
5996 emit_move_insn (target, tmp);
5997 }
5998 return target;
5999}
6000
6001static rtx
6002load_tls_operand (rtx x, rtx reg)
6003{
6004 rtx tmp;
6005
6006 if (reg == NULL_RTX)
6007 reg = gen_reg_rtx (SImode);
6008
6009 tmp = gen_rtx_CONST (SImode, x);
6010
6011 emit_move_insn (reg, tmp);
6012
6013 return reg;
6014}
6015
6016static rtx
6017arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
6018{
f16fe45f 6019 rtx insns, label, labelno, sum;
d3585b76
DJ
6020
6021 start_sequence ();
6022
f16fe45f
DJ
6023 labelno = GEN_INT (pic_labelno++);
6024 label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
6025 label = gen_rtx_CONST (VOIDmode, label);
6026
d3585b76 6027 sum = gen_rtx_UNSPEC (Pmode,
f16fe45f 6028 gen_rtvec (4, x, GEN_INT (reloc), label,
d3585b76
DJ
6029 GEN_INT (TARGET_ARM ? 8 : 4)),
6030 UNSPEC_TLS);
6031 reg = load_tls_operand (sum, reg);
6032
6033 if (TARGET_ARM)
f16fe45f 6034 emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
5b3e6663 6035 else if (TARGET_THUMB2)
87d05b44 6036 emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5b3e6663 6037 else /* TARGET_THUMB1 */
f16fe45f 6038 emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
d3585b76
DJ
6039
6040 *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX, LCT_PURE, /* LCT_CONST? */
6041 Pmode, 1, reg, Pmode);
6042
6043 insns = get_insns ();
6044 end_sequence ();
6045
6046 return insns;
6047}
6048
6049rtx
6050legitimize_tls_address (rtx x, rtx reg)
6051{
f16fe45f 6052 rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
d3585b76
DJ
6053 unsigned int model = SYMBOL_REF_TLS_MODEL (x);
6054
6055 switch (model)
6056 {
6057 case TLS_MODEL_GLOBAL_DYNAMIC:
6058 insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
6059 dest = gen_reg_rtx (Pmode);
6060 emit_libcall_block (insns, dest, ret, x);
6061 return dest;
6062
6063 case TLS_MODEL_LOCAL_DYNAMIC:
6064 insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
6065
6066 /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
6067 share the LDM result with other LD model accesses. */
6068 eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
6069 UNSPEC_TLS);
6070 dest = gen_reg_rtx (Pmode);
e66e1c68 6071 emit_libcall_block (insns, dest, ret, eqv);
d3585b76
DJ
6072
6073 /* Load the addend. */
6074 addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (TLS_LDO32)),
6075 UNSPEC_TLS);
6076 addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
6077 return gen_rtx_PLUS (Pmode, dest, addend);
6078
6079 case TLS_MODEL_INITIAL_EXEC:
f16fe45f
DJ
6080 labelno = GEN_INT (pic_labelno++);
6081 label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
6082 label = gen_rtx_CONST (VOIDmode, label);
d3585b76 6083 sum = gen_rtx_UNSPEC (Pmode,
f16fe45f 6084 gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
d3585b76
DJ
6085 GEN_INT (TARGET_ARM ? 8 : 4)),
6086 UNSPEC_TLS);
6087 reg = load_tls_operand (sum, reg);
6088
6089 if (TARGET_ARM)
f16fe45f 6090 emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
5b3e6663 6091 else if (TARGET_THUMB2)
2e5505a4 6092 emit_insn (gen_tls_load_dot_plus_four (reg, NULL, reg, labelno));
d3585b76
DJ
6093 else
6094 {
f16fe45f 6095 emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
d3585b76
DJ
6096 emit_move_insn (reg, gen_const_mem (SImode, reg));
6097 }
6098
6099 tp = arm_load_tp (NULL_RTX);
6100
6101 return gen_rtx_PLUS (Pmode, tp, reg);
6102
6103 case TLS_MODEL_LOCAL_EXEC:
6104 tp = arm_load_tp (NULL_RTX);
6105
6106 reg = gen_rtx_UNSPEC (Pmode,
6107 gen_rtvec (2, x, GEN_INT (TLS_LE32)),
6108 UNSPEC_TLS);
6109 reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
6110
6111 return gen_rtx_PLUS (Pmode, tp, reg);
6112
6113 default:
6114 abort ();
6115 }
6116}
6117
ccf4d512
RE
6118/* Try machine-dependent ways of modifying an illegitimate address
6119 to be legitimate. If we find one, return the new, valid address. */
ccf4d512 6120rtx
e32bac5b 6121arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
ccf4d512 6122{
506d7b68
PB
6123 if (!TARGET_ARM)
6124 {
6125 /* TODO: legitimize_address for Thumb2. */
6126 if (TARGET_THUMB2)
6127 return x;
6128 return thumb_legitimize_address (x, orig_x, mode);
6129 }
6130
d3585b76
DJ
6131 if (arm_tls_symbol_p (x))
6132 return legitimize_tls_address (x, NULL_RTX);
6133
ccf4d512
RE
6134 if (GET_CODE (x) == PLUS)
6135 {
6136 rtx xop0 = XEXP (x, 0);
6137 rtx xop1 = XEXP (x, 1);
6138
6139 if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
6140 xop0 = force_reg (SImode, xop0);
6141
6142 if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
6143 xop1 = force_reg (SImode, xop1);
6144
6145 if (ARM_BASE_REGISTER_RTX_P (xop0)
6146 && GET_CODE (xop1) == CONST_INT)
6147 {
6148 HOST_WIDE_INT n, low_n;
6149 rtx base_reg, val;
6150 n = INTVAL (xop1);
6151
9b66ebb1
PB
6152 /* VFP addressing modes actually allow greater offsets, but for
6153 now we just stick with the lowest common denominator. */
6154 if (mode == DImode
6155 || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
ccf4d512
RE
6156 {
6157 low_n = n & 0x0f;
6158 n &= ~0x0f;
6159 if (low_n > 4)
6160 {
6161 n += 16;
6162 low_n -= 16;
6163 }
6164 }
6165 else
6166 {
6167 low_n = ((mode) == TImode ? 0
6168 : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
6169 n -= low_n;
6170 }
6171
6172 base_reg = gen_reg_rtx (SImode);
d66437c5 6173 val = force_operand (plus_constant (xop0, n), NULL_RTX);
ccf4d512 6174 emit_move_insn (base_reg, val);
d66437c5 6175 x = plus_constant (base_reg, low_n);
ccf4d512
RE
6176 }
6177 else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
6178 x = gen_rtx_PLUS (SImode, xop0, xop1);
6179 }
6180
6181 /* XXX We don't allow MINUS any more -- see comment in
c6c3dba9 6182 arm_legitimate_address_outer_p (). */
ccf4d512
RE
6183 else if (GET_CODE (x) == MINUS)
6184 {
6185 rtx xop0 = XEXP (x, 0);
6186 rtx xop1 = XEXP (x, 1);
6187
6188 if (CONSTANT_P (xop0))
6189 xop0 = force_reg (SImode, xop0);
6190
6191 if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
6192 xop1 = force_reg (SImode, xop1);
6193
6194 if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
6195 x = gen_rtx_MINUS (SImode, xop0, xop1);
6196 }
6197
86805759
NP
6198 /* Make sure to take full advantage of the pre-indexed addressing mode
6199 with absolute addresses which often allows for the base register to
6200 be factorized for multiple adjacent memory references, and it might
6201 even allows for the mini pool to be avoided entirely. */
6202 else if (GET_CODE (x) == CONST_INT && optimize > 0)
6203 {
6204 unsigned int bits;
6205 HOST_WIDE_INT mask, base, index;
6206 rtx base_reg;
6207
a50aa827
KH
6208 /* ldr and ldrb can use a 12-bit index, ldrsb and the rest can only
6209 use a 8-bit index. So let's use a 12-bit index for SImode only and
86805759
NP
6210 hope that arm_gen_constant will enable ldrb to use more bits. */
6211 bits = (mode == SImode) ? 12 : 8;
6212 mask = (1 << bits) - 1;
6213 base = INTVAL (x) & ~mask;
6214 index = INTVAL (x) & mask;
b107d743 6215 if (bit_count (base & 0xffffffff) > (32 - bits)/2)
86805759
NP
6216 {
6217 /* It'll most probably be more efficient to generate the base
6218 with more bits set and use a negative index instead. */
6219 base |= mask;
6220 index -= mask;
6221 }
6222 base_reg = force_reg (SImode, GEN_INT (base));
d66437c5 6223 x = plus_constant (base_reg, index);
86805759
NP
6224 }
6225
ccf4d512
RE
6226 if (flag_pic)
6227 {
6228 /* We need to find and carefully transform any SYMBOL and LABEL
6229 references; so go back to the original address expression. */
6230 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
6231
6232 if (new_x != orig_x)
6233 x = new_x;
6234 }
6235
6236 return x;
6237}
6238
6f5b4f3e
RE
6239
6240/* Try machine-dependent ways of modifying an illegitimate Thumb address
6241 to be legitimate. If we find one, return the new, valid address. */
6242rtx
6243thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
6244{
d3585b76
DJ
6245 if (arm_tls_symbol_p (x))
6246 return legitimize_tls_address (x, NULL_RTX);
6247
6f5b4f3e
RE
6248 if (GET_CODE (x) == PLUS
6249 && GET_CODE (XEXP (x, 1)) == CONST_INT
6250 && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
6251 || INTVAL (XEXP (x, 1)) < 0))
6252 {
6253 rtx xop0 = XEXP (x, 0);
6254 rtx xop1 = XEXP (x, 1);
6255 HOST_WIDE_INT offset = INTVAL (xop1);
6256
6257 /* Try and fold the offset into a biasing of the base register and
6258 then offsetting that. Don't do this when optimizing for space
6259 since it can cause too many CSEs. */
6260 if (optimize_size && offset >= 0
6261 && offset < 256 + 31 * GET_MODE_SIZE (mode))
6262 {
6263 HOST_WIDE_INT delta;
6264
6265 if (offset >= 256)
6266 delta = offset - (256 - GET_MODE_SIZE (mode));
6267 else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
6268 delta = 31 * GET_MODE_SIZE (mode);
6269 else
6270 delta = offset & (~31 * GET_MODE_SIZE (mode));
6271
6272 xop0 = force_operand (plus_constant (xop0, offset - delta),
6273 NULL_RTX);
6274 x = plus_constant (xop0, delta);
6275 }
6276 else if (offset < 0 && offset > -256)
6277 /* Small negative offsets are best done with a subtract before the
6278 dereference, forcing these into a register normally takes two
6279 instructions. */
6280 x = force_operand (x, NULL_RTX);
6281 else
6282 {
6283 /* For the remaining cases, force the constant into a register. */
6284 xop1 = force_reg (SImode, xop1);
6285 x = gen_rtx_PLUS (SImode, xop0, xop1);
6286 }
6287 }
6288 else if (GET_CODE (x) == PLUS
6289 && s_register_operand (XEXP (x, 1), SImode)
6290 && !s_register_operand (XEXP (x, 0), SImode))
6291 {
6292 rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
6293
6294 x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
6295 }
6296
6297 if (flag_pic)
6298 {
6299 /* We need to find and carefully transform any SYMBOL and LABEL
6300 references; so go back to the original address expression. */
6301 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
6302
6303 if (new_x != orig_x)
6304 x = new_x;
6305 }
6306
6307 return x;
6308}
6309
a132dad6 6310rtx
e0b92319
NC
6311thumb_legitimize_reload_address (rtx *x_p,
6312 enum machine_mode mode,
6313 int opnum, int type,
6314 int ind_levels ATTRIBUTE_UNUSED)
a132dad6
RE
6315{
6316 rtx x = *x_p;
e0b92319 6317
a132dad6
RE
6318 if (GET_CODE (x) == PLUS
6319 && GET_MODE_SIZE (mode) < 4
6320 && REG_P (XEXP (x, 0))
6321 && XEXP (x, 0) == stack_pointer_rtx
6322 && GET_CODE (XEXP (x, 1)) == CONST_INT
6323 && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
6324 {
6325 rtx orig_x = x;
6326
6327 x = copy_rtx (x);
6328 push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
bbbbb16a 6329 Pmode, VOIDmode, 0, 0, opnum, (enum reload_type) type);
a132dad6
RE
6330 return x;
6331 }
6332
6333 /* If both registers are hi-regs, then it's better to reload the
6334 entire expression rather than each register individually. That
6335 only requires one reload register rather than two. */
6336 if (GET_CODE (x) == PLUS
6337 && REG_P (XEXP (x, 0))
6338 && REG_P (XEXP (x, 1))
6339 && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
6340 && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
6341 {
6342 rtx orig_x = x;
6343
6344 x = copy_rtx (x);
6345 push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
bbbbb16a 6346 Pmode, VOIDmode, 0, 0, opnum, (enum reload_type) type);
a132dad6
RE
6347 return x;
6348 }
6349
6350 return NULL;
6351}
d3585b76
DJ
6352
6353/* Test for various thread-local symbols. */
6354
6355/* Return TRUE if X is a thread-local symbol. */
6356
6357static bool
6358arm_tls_symbol_p (rtx x)
6359{
6360 if (! TARGET_HAVE_TLS)
6361 return false;
6362
6363 if (GET_CODE (x) != SYMBOL_REF)
6364 return false;
6365
6366 return SYMBOL_REF_TLS_MODEL (x) != 0;
6367}
6368
6369/* Helper for arm_tls_referenced_p. */
6370
6371static int
6372arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
6373{
6374 if (GET_CODE (*x) == SYMBOL_REF)
6375 return SYMBOL_REF_TLS_MODEL (*x) != 0;
6376
6377 /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
6378 TLS offsets, not real symbol references. */
6379 if (GET_CODE (*x) == UNSPEC
6380 && XINT (*x, 1) == UNSPEC_TLS)
6381 return -1;
6382
6383 return 0;
6384}
6385
6386/* Return TRUE if X contains any TLS symbol references. */
6387
6388bool
6389arm_tls_referenced_p (rtx x)
6390{
6391 if (! TARGET_HAVE_TLS)
6392 return false;
6393
6394 return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
6395}
8426b956
RS
6396
6397/* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
6398
6399bool
6400arm_cannot_force_const_mem (rtx x)
6401{
6402 rtx base, offset;
6403
6404 if (ARM_OFFSETS_MUST_BE_WITHIN_SECTIONS_P)
6405 {
6406 split_const (x, &base, &offset);
6407 if (GET_CODE (base) == SYMBOL_REF
6408 && !offset_within_block_p (base, INTVAL (offset)))
6409 return true;
6410 }
6411 return arm_tls_referenced_p (x);
6412}
6b990f6b 6413\f
e2c671ba
RE
6414#define REG_OR_SUBREG_REG(X) \
6415 (GET_CODE (X) == REG \
6416 || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
6417
6418#define REG_OR_SUBREG_RTX(X) \
6419 (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
6420
3c50106f 6421static inline int
5b3e6663 6422thumb1_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
e2c671ba
RE
6423{
6424 enum machine_mode mode = GET_MODE (x);
e4c6a07a 6425 int total;
e2c671ba 6426
9b66ebb1 6427 switch (code)
d5b7b3ae 6428 {
9b66ebb1
PB
6429 case ASHIFT:
6430 case ASHIFTRT:
6431 case LSHIFTRT:
f676971a 6432 case ROTATERT:
9b66ebb1
PB
6433 case PLUS:
6434 case MINUS:
6435 case COMPARE:
6436 case NEG:
f676971a 6437 case NOT:
9b66ebb1 6438 return COSTS_N_INSNS (1);
f676971a
EC
6439
6440 case MULT:
6441 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6442 {
6443 int cycles = 0;
9b66ebb1 6444 unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
f676971a
EC
6445
6446 while (i)
6447 {
6448 i >>= 2;
6449 cycles++;
6450 }
6451 return COSTS_N_INSNS (2) + cycles;
9b66ebb1
PB
6452 }
6453 return COSTS_N_INSNS (1) + 16;
f676971a
EC
6454
6455 case SET:
6456 return (COSTS_N_INSNS (1)
6457 + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
9b66ebb1 6458 + GET_CODE (SET_DEST (x)) == MEM));
f676971a
EC
6459
6460 case CONST_INT:
6461 if (outer == SET)
6462 {
6463 if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
6464 return 0;
6465 if (thumb_shiftable_const (INTVAL (x)))
6466 return COSTS_N_INSNS (2);
6467 return COSTS_N_INSNS (3);
6468 }
9b66ebb1 6469 else if ((outer == PLUS || outer == COMPARE)
f676971a 6470 && INTVAL (x) < 256 && INTVAL (x) > -256)
9b66ebb1 6471 return 0;
582021ba 6472 else if ((outer == IOR || outer == XOR || outer == AND)
9b66ebb1
PB
6473 && INTVAL (x) < 256 && INTVAL (x) >= -256)
6474 return COSTS_N_INSNS (1);
c99102b8
BS
6475 else if (outer == AND)
6476 {
6477 int i;
6478 /* This duplicates the tests in the andsi3 expander. */
6479 for (i = 9; i <= 31; i++)
6480 if ((((HOST_WIDE_INT) 1) << i) - 1 == INTVAL (x)
6481 || (((HOST_WIDE_INT) 1) << i) - 1 == ~INTVAL (x))
6482 return COSTS_N_INSNS (2);
6483 }
f676971a
EC
6484 else if (outer == ASHIFT || outer == ASHIFTRT
6485 || outer == LSHIFTRT)
6486 return 0;
9b66ebb1 6487 return COSTS_N_INSNS (2);
f676971a
EC
6488
6489 case CONST:
6490 case CONST_DOUBLE:
6491 case LABEL_REF:
6492 case SYMBOL_REF:
9b66ebb1 6493 return COSTS_N_INSNS (3);
f676971a 6494
9b66ebb1
PB
6495 case UDIV:
6496 case UMOD:
6497 case DIV:
6498 case MOD:
6499 return 100;
d5b7b3ae 6500
9b66ebb1
PB
6501 case TRUNCATE:
6502 return 99;
d5b7b3ae 6503
9b66ebb1
PB
6504 case AND:
6505 case XOR:
f676971a 6506 case IOR:
ff482c8d 6507 /* XXX guess. */
9b66ebb1 6508 return 8;
d5b7b3ae 6509
9b66ebb1
PB
6510 case MEM:
6511 /* XXX another guess. */
6512 /* Memory costs quite a lot for the first word, but subsequent words
6513 load at the equivalent of a single insn each. */
6514 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
6515 + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
6516 ? 4 : 0));
6517
6518 case IF_THEN_ELSE:
ff482c8d 6519 /* XXX a guess. */
9b66ebb1
PB
6520 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
6521 return 14;
6522 return 2;
6523
e4c6a07a 6524 case SIGN_EXTEND:
9b66ebb1 6525 case ZERO_EXTEND:
e4c6a07a
BS
6526 total = mode == DImode ? COSTS_N_INSNS (1) : 0;
6527 total += thumb1_rtx_costs (XEXP (x, 0), GET_CODE (XEXP (x, 0)), code);
f676971a 6528
e4c6a07a
BS
6529 if (mode == SImode)
6530 return total;
f676971a 6531
e4c6a07a
BS
6532 if (arm_arch6)
6533 return total + COSTS_N_INSNS (1);
f676971a 6534
e4c6a07a
BS
6535 /* Assume a two-shift sequence. Increase the cost slightly so
6536 we prefer actual shifts over an extend operation. */
6537 return total + 1 + COSTS_N_INSNS (2);
f676971a 6538
9b66ebb1
PB
6539 default:
6540 return 99;
d5b7b3ae 6541 }
9b66ebb1
PB
6542}
6543
d5a0a47b
RE
6544static inline bool
6545arm_rtx_costs_1 (rtx x, enum rtx_code outer, int* total, bool speed)
9b66ebb1
PB
6546{
6547 enum machine_mode mode = GET_MODE (x);
6548 enum rtx_code subcode;
d5a0a47b
RE
6549 rtx operand;
6550 enum rtx_code code = GET_CODE (x);
d5a0a47b 6551 *total = 0;
9b66ebb1 6552
e2c671ba
RE
6553 switch (code)
6554 {
6555 case MEM:
6556 /* Memory costs quite a lot for the first word, but subsequent words
6557 load at the equivalent of a single insn each. */
d5a0a47b
RE
6558 *total = COSTS_N_INSNS (2 + ARM_NUM_REGS (mode));
6559 return true;
e2c671ba
RE
6560
6561 case DIV:
6562 case MOD:
b9c53150
RS
6563 case UDIV:
6564 case UMOD:
d5a0a47b
RE
6565 if (TARGET_HARD_FLOAT && mode == SFmode)
6566 *total = COSTS_N_INSNS (2);
e0dc3601 6567 else if (TARGET_HARD_FLOAT && mode == DFmode && !TARGET_VFP_SINGLE)
d5a0a47b
RE
6568 *total = COSTS_N_INSNS (4);
6569 else
6570 *total = COSTS_N_INSNS (20);
6571 return false;
e2c671ba
RE
6572
6573 case ROTATE:
d5a0a47b
RE
6574 if (GET_CODE (XEXP (x, 1)) == REG)
6575 *total = COSTS_N_INSNS (1); /* Need to subtract from 32 */
6576 else if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6577 *total = rtx_cost (XEXP (x, 1), code, speed);
6578
e2c671ba
RE
6579 /* Fall through */
6580 case ROTATERT:
6581 if (mode != SImode)
d5a0a47b
RE
6582 {
6583 *total += COSTS_N_INSNS (4);
6584 return true;
6585 }
6586
e2c671ba
RE
6587 /* Fall through */
6588 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
d5a0a47b 6589 *total += rtx_cost (XEXP (x, 0), code, speed);
e2c671ba 6590 if (mode == DImode)
d5a0a47b
RE
6591 {
6592 *total += COSTS_N_INSNS (3);
6593 return true;
6594 }
7612f14d 6595
d5a0a47b 6596 *total += COSTS_N_INSNS (1);
7612f14d
PB
6597 /* Increase the cost of complex shifts because they aren't any faster,
6598 and reduce dual issue opportunities. */
6599 if (arm_tune_cortex_a9
6600 && outer != SET && GET_CODE (XEXP (x, 1)) != CONST_INT)
d5a0a47b
RE
6601 ++*total;
6602
6603 return true;
e2c671ba
RE
6604
6605 case MINUS:
6606 if (mode == DImode)
d5a0a47b
RE
6607 {
6608 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
6609 if (GET_CODE (XEXP (x, 0)) == CONST_INT
6610 && const_ok_for_arm (INTVAL (XEXP (x, 0))))
6611 {
6612 *total += rtx_cost (XEXP (x, 1), code, speed);
6613 return true;
6614 }
6615
6616 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6617 && const_ok_for_arm (INTVAL (XEXP (x, 1))))
6618 {
6619 *total += rtx_cost (XEXP (x, 0), code, speed);
6620 return true;
6621 }
6622
6623 return false;
6624 }
e2c671ba
RE
6625
6626 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
d5a0a47b 6627 {
e0dc3601
PB
6628 if (TARGET_HARD_FLOAT
6629 && (mode == SFmode
6630 || (mode == DFmode && !TARGET_VFP_SINGLE)))
d5a0a47b
RE
6631 {
6632 *total = COSTS_N_INSNS (1);
6633 if (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
6634 && arm_const_double_rtx (XEXP (x, 0)))
6635 {
6636 *total += rtx_cost (XEXP (x, 1), code, speed);
6637 return true;
6638 }
6639
6640 if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
6641 && arm_const_double_rtx (XEXP (x, 1)))
6642 {
6643 *total += rtx_cost (XEXP (x, 0), code, speed);
6644 return true;
6645 }
6646
6647 return false;
6648 }
6649 *total = COSTS_N_INSNS (20);
6650 return false;
6651 }
6652
6653 *total = COSTS_N_INSNS (1);
6654 if (GET_CODE (XEXP (x, 0)) == CONST_INT
6655 && const_ok_for_arm (INTVAL (XEXP (x, 0))))
6656 {
6657 *total += rtx_cost (XEXP (x, 1), code, speed);
6658 return true;
6659 }
6660
6661 subcode = GET_CODE (XEXP (x, 1));
6662 if (subcode == ASHIFT || subcode == ASHIFTRT
6663 || subcode == LSHIFTRT
6664 || subcode == ROTATE || subcode == ROTATERT)
6665 {
6666 *total += rtx_cost (XEXP (x, 0), code, speed);
6667 *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, speed);
6668 return true;
6669 }
6670
b32f6fff
KH
6671 /* A shift as a part of RSB costs no more than RSB itself. */
6672 if (GET_CODE (XEXP (x, 0)) == MULT
4c7c486a 6673 && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
b32f6fff
KH
6674 {
6675 *total += rtx_cost (XEXP (XEXP (x, 0), 0), code, speed);
6676 *total += rtx_cost (XEXP (x, 1), code, speed);
6677 return true;
6678 }
6679
d5a0a47b 6680 if (subcode == MULT
4c7c486a 6681 && power_of_two_operand (XEXP (XEXP (x, 1), 1), SImode))
d5a0a47b
RE
6682 {
6683 *total += rtx_cost (XEXP (x, 0), code, speed);
6684 *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, speed);
6685 return true;
6686 }
6687
6688 if (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMPARE
6689 || GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMM_COMPARE)
6690 {
6691 *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
6692 if (GET_CODE (XEXP (XEXP (x, 1), 0)) == REG
6693 && REGNO (XEXP (XEXP (x, 1), 0)) != CC_REGNUM)
6694 *total += COSTS_N_INSNS (1);
6695
6696 return true;
6697 }
6698
e2c671ba
RE
6699 /* Fall through */
6700
f676971a 6701 case PLUS:
d5a0a47b 6702 if (code == PLUS && arm_arch6 && mode == SImode
ff069900
PB
6703 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
6704 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
d5a0a47b
RE
6705 {
6706 *total = COSTS_N_INSNS (1);
6707 *total += rtx_cost (XEXP (XEXP (x, 0), 0), GET_CODE (XEXP (x, 0)),
6708 speed);
6709 *total += rtx_cost (XEXP (x, 1), code, speed);
6710 return true;
6711 }
ff069900 6712
d5a0a47b
RE
6713 /* MLA: All arguments must be registers. We filter out
6714 multiplication by a power of two, so that we fall down into
6715 the code below. */
6716 if (GET_CODE (XEXP (x, 0)) == MULT
4c7c486a 6717 && !power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
26da58dd 6718 {
d5a0a47b
RE
6719 /* The cost comes from the cost of the multiply. */
6720 return false;
26da58dd
PB
6721 }
6722
e2c671ba 6723 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
d5a0a47b 6724 {
e0dc3601
PB
6725 if (TARGET_HARD_FLOAT
6726 && (mode == SFmode
6727 || (mode == DFmode && !TARGET_VFP_SINGLE)))
d5a0a47b
RE
6728 {
6729 *total = COSTS_N_INSNS (1);
6730 if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
6731 && arm_const_double_rtx (XEXP (x, 1)))
6732 {
6733 *total += rtx_cost (XEXP (x, 0), code, speed);
6734 return true;
6735 }
6736
6737 return false;
6738 }
6739
6740 *total = COSTS_N_INSNS (20);
6741 return false;
6742 }
6743
6744 if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMPARE
6745 || GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMM_COMPARE)
6746 {
6747 *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 1), code, speed);
6748 if (GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
6749 && REGNO (XEXP (XEXP (x, 0), 0)) != CC_REGNUM)
6750 *total += COSTS_N_INSNS (1);
6751 return true;
6752 }
e2c671ba
RE
6753
6754 /* Fall through */
d5a0a47b 6755
f676971a 6756 case AND: case XOR: case IOR:
e2c671ba
RE
6757
6758 /* Normally the frame registers will be spilt into reg+const during
6759 reload, so it is a bad idea to combine them with other instructions,
6760 since then they might not be moved outside of loops. As a compromise
6761 we allow integration with ops that have a constant as their second
6762 operand. */
13cc4787
BS
6763 if (REG_OR_SUBREG_REG (XEXP (x, 0))
6764 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
6765 && GET_CODE (XEXP (x, 1)) != CONST_INT)
6766 *total = COSTS_N_INSNS (1);
e2c671ba
RE
6767
6768 if (mode == DImode)
d5a0a47b
RE
6769 {
6770 *total += COSTS_N_INSNS (2);
6771 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6772 && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
6773 {
6774 *total += rtx_cost (XEXP (x, 0), code, speed);
6775 return true;
6776 }
e2c671ba 6777
d5a0a47b
RE
6778 return false;
6779 }
6780
6781 *total += COSTS_N_INSNS (1);
6782 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6783 && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
6784 {
6785 *total += rtx_cost (XEXP (x, 0), code, speed);
6786 return true;
6787 }
6788 subcode = GET_CODE (XEXP (x, 0));
6789 if (subcode == ASHIFT || subcode == ASHIFTRT
6790 || subcode == LSHIFTRT
6791 || subcode == ROTATE || subcode == ROTATERT)
6792 {
6793 *total += rtx_cost (XEXP (x, 1), code, speed);
6794 *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6795 return true;
6796 }
6797
6798 if (subcode == MULT
4c7c486a 6799 && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
d5a0a47b
RE
6800 {
6801 *total += rtx_cost (XEXP (x, 1), code, speed);
6802 *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6803 return true;
6804 }
6805
6806 if (subcode == UMIN || subcode == UMAX
6807 || subcode == SMIN || subcode == SMAX)
6808 {
6809 *total = COSTS_N_INSNS (3);
6810 return true;
6811 }
6812
6813 return false;
e2c671ba
RE
6814
6815 case MULT:
9b66ebb1 6816 /* This should have been handled by the CPU specific routines. */
e6d29d15 6817 gcc_unreachable ();
e2c671ba 6818
56636818 6819 case TRUNCATE:
9b66ebb1 6820 if (arm_arch3m && mode == SImode
56636818
JL
6821 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
6822 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
6823 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
6824 == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
6825 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
6826 || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
d5a0a47b
RE
6827 {
6828 *total = rtx_cost (XEXP (XEXP (x, 0), 0), LSHIFTRT, speed);
6829 return true;
6830 }
6831 *total = COSTS_N_INSNS (2); /* Plus the cost of the MULT */
6832 return false;
56636818 6833
e2c671ba
RE
6834 case NEG:
6835 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
d5a0a47b 6836 {
e0dc3601
PB
6837 if (TARGET_HARD_FLOAT
6838 && (mode == SFmode
6839 || (mode == DFmode && !TARGET_VFP_SINGLE)))
d5a0a47b
RE
6840 {
6841 *total = COSTS_N_INSNS (1);
6842 return false;
6843 }
6844 *total = COSTS_N_INSNS (2);
6845 return false;
6846 }
6847
e2c671ba
RE
6848 /* Fall through */
6849 case NOT:
d5a0a47b
RE
6850 *total = COSTS_N_INSNS (ARM_NUM_REGS(mode));
6851 if (mode == SImode && code == NOT)
6852 {
6853 subcode = GET_CODE (XEXP (x, 0));
6854 if (subcode == ASHIFT || subcode == ASHIFTRT
6855 || subcode == LSHIFTRT
6856 || subcode == ROTATE || subcode == ROTATERT
6857 || (subcode == MULT
4c7c486a 6858 && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode)))
d5a0a47b
RE
6859 {
6860 *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6861 /* Register shifts cost an extra cycle. */
6862 if (GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
6863 *total += COSTS_N_INSNS (1) + rtx_cost (XEXP (XEXP (x, 0), 1),
6864 subcode, speed);
6865 return true;
6866 }
6867 }
e2c671ba 6868
d5a0a47b 6869 return false;
e2c671ba
RE
6870
6871 case IF_THEN_ELSE:
6872 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
d5a0a47b
RE
6873 {
6874 *total = COSTS_N_INSNS (4);
6875 return true;
6876 }
6877
6878 operand = XEXP (x, 0);
6879
6880 if (!((GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMPARE
6881 || GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMM_COMPARE)
6882 && GET_CODE (XEXP (operand, 0)) == REG
6883 && REGNO (XEXP (operand, 0)) == CC_REGNUM))
6884 *total += COSTS_N_INSNS (1);
6885 *total += (rtx_cost (XEXP (x, 1), code, speed)
6886 + rtx_cost (XEXP (x, 2), code, speed));
6887 return true;
6888
6889 case NE:
6890 if (mode == SImode && XEXP (x, 1) == const0_rtx)
6891 {
6892 *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
6893 return true;
6894 }
6895 goto scc_insn;
6896
6897 case GE:
6898 if ((GET_CODE (XEXP (x, 0)) != REG || REGNO (XEXP (x, 0)) != CC_REGNUM)
6899 && mode == SImode && XEXP (x, 1) == const0_rtx)
6900 {
6901 *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
6902 return true;
6903 }
6904 goto scc_insn;
6905
6906 case LT:
6907 if ((GET_CODE (XEXP (x, 0)) != REG || REGNO (XEXP (x, 0)) != CC_REGNUM)
6908 && mode == SImode && XEXP (x, 1) == const0_rtx)
6909 {
6910 *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
6911 return true;
6912 }
6913 goto scc_insn;
6914
6915 case EQ:
6916 case GT:
6917 case LE:
6918 case GEU:
6919 case LTU:
6920 case GTU:
6921 case LEU:
6922 case UNORDERED:
6923 case ORDERED:
6924 case UNEQ:
6925 case UNGE:
6926 case UNLT:
6927 case UNGT:
6928 case UNLE:
6929 scc_insn:
6930 /* SCC insns. In the case where the comparison has already been
6931 performed, then they cost 2 instructions. Otherwise they need
6932 an additional comparison before them. */
6933 *total = COSTS_N_INSNS (2);
6934 if (GET_CODE (XEXP (x, 0)) == REG && REGNO (XEXP (x, 0)) == CC_REGNUM)
6935 {
6936 return true;
6937 }
e2c671ba 6938
d5a0a47b 6939 /* Fall through */
e2c671ba 6940 case COMPARE:
d5a0a47b
RE
6941 if (GET_CODE (XEXP (x, 0)) == REG && REGNO (XEXP (x, 0)) == CC_REGNUM)
6942 {
6943 *total = 0;
6944 return true;
6945 }
6946
6947 *total += COSTS_N_INSNS (1);
6948 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6949 && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
6950 {
6951 *total += rtx_cost (XEXP (x, 0), code, speed);
6952 return true;
6953 }
6954
6955 subcode = GET_CODE (XEXP (x, 0));
6956 if (subcode == ASHIFT || subcode == ASHIFTRT
6957 || subcode == LSHIFTRT
6958 || subcode == ROTATE || subcode == ROTATERT)
6959 {
6960 *total += rtx_cost (XEXP (x, 1), code, speed);
6961 *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6962 return true;
6963 }
6964
6965 if (subcode == MULT
4c7c486a 6966 && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
d5a0a47b
RE
6967 {
6968 *total += rtx_cost (XEXP (x, 1), code, speed);
6969 *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6970 return true;
6971 }
6972
6973 return false;
6974
6975 case UMIN:
6976 case UMAX:
6977 case SMIN:
6978 case SMAX:
6979 *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
6980 if (GET_CODE (XEXP (x, 1)) != CONST_INT
6981 || !const_ok_for_arm (INTVAL (XEXP (x, 1))))
6982 *total += rtx_cost (XEXP (x, 1), code, speed);
6983 return true;
e2c671ba
RE
6984
6985 case ABS:
bbbbb16a 6986 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
d5a0a47b 6987 {
e0dc3601
PB
6988 if (TARGET_HARD_FLOAT
6989 && (mode == SFmode
6990 || (mode == DFmode && !TARGET_VFP_SINGLE)))
d5a0a47b
RE
6991 {
6992 *total = COSTS_N_INSNS (1);
6993 return false;
6994 }
6995 *total = COSTS_N_INSNS (20);
6996 return false;
6997 }
6998 *total = COSTS_N_INSNS (1);
6999 if (mode == DImode)
7000 *total += COSTS_N_INSNS (3);
7001 return false;
e2c671ba
RE
7002
7003 case SIGN_EXTEND:
e2c671ba 7004 case ZERO_EXTEND:
d5a0a47b
RE
7005 *total = 0;
7006 if (GET_MODE_CLASS (mode) == MODE_INT)
e2c671ba 7007 {
e4c6a07a
BS
7008 rtx op = XEXP (x, 0);
7009 enum machine_mode opmode = GET_MODE (op);
7010
d5a0a47b
RE
7011 if (mode == DImode)
7012 *total += COSTS_N_INSNS (1);
e2c671ba 7013
e4c6a07a 7014 if (opmode != SImode)
d5a0a47b 7015 {
e4c6a07a 7016 if (MEM_P (op))
d5a0a47b 7017 {
e4c6a07a
BS
7018 /* If !arm_arch4, we use one of the extendhisi2_mem
7019 or movhi_bytes patterns for HImode. For a QImode
7020 sign extension, we first zero-extend from memory
7021 and then perform a shift sequence. */
7022 if (!arm_arch4 && (opmode != QImode || code == SIGN_EXTEND))
7023 *total += COSTS_N_INSNS (2);
d5a0a47b 7024 }
e4c6a07a
BS
7025 else if (arm_arch6)
7026 *total += COSTS_N_INSNS (1);
7027
7028 /* We don't have the necessary insn, so we need to perform some
7029 other operation. */
7030 else if (TARGET_ARM && code == ZERO_EXTEND && mode == QImode)
7031 /* An and with constant 255. */
7032 *total += COSTS_N_INSNS (1);
7033 else
7034 /* A shift sequence. Increase costs slightly to avoid
7035 combining two shifts into an extend operation. */
7036 *total += COSTS_N_INSNS (2) + 1;
d5a0a47b 7037 }
e2c671ba 7038
d5a0a47b
RE
7039 return false;
7040 }
ad076f4e 7041
d5a0a47b
RE
7042 switch (GET_MODE (XEXP (x, 0)))
7043 {
5a9335ef
NC
7044 case V8QImode:
7045 case V4HImode:
7046 case V2SImode:
7047 case V4QImode:
7048 case V2HImode:
d5a0a47b
RE
7049 *total = COSTS_N_INSNS (1);
7050 return false;
5a9335ef 7051
ad076f4e 7052 default:
e6d29d15 7053 gcc_unreachable ();
e2c671ba 7054 }
e6d29d15 7055 gcc_unreachable ();
e2c671ba 7056
d5a0a47b
RE
7057 case ZERO_EXTRACT:
7058 case SIGN_EXTRACT:
7059 *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
7060 return true;
7061
f676971a 7062 case CONST_INT:
d5a0a47b
RE
7063 if (const_ok_for_arm (INTVAL (x))
7064 || const_ok_for_arm (~INTVAL (x)))
7065 *total = COSTS_N_INSNS (1);
f676971a 7066 else
d5a0a47b
RE
7067 *total = COSTS_N_INSNS (arm_gen_constant (SET, mode, NULL_RTX,
7068 INTVAL (x), NULL_RTX,
7069 NULL_RTX, 0, 0));
7070 return true;
f676971a
EC
7071
7072 case CONST:
7073 case LABEL_REF:
7074 case SYMBOL_REF:
d5a0a47b
RE
7075 *total = COSTS_N_INSNS (3);
7076 return true;
f676971a 7077
571191af 7078 case HIGH:
d5a0a47b
RE
7079 *total = COSTS_N_INSNS (1);
7080 return true;
7081
571191af 7082 case LO_SUM:
d5a0a47b
RE
7083 *total = COSTS_N_INSNS (1);
7084 *total += rtx_cost (XEXP (x, 0), code, speed);
7085 return true;
571191af 7086
f676971a 7087 case CONST_DOUBLE:
e0dc3601
PB
7088 if (TARGET_HARD_FLOAT && vfp3_const_double_rtx (x)
7089 && (mode == SFmode || !TARGET_VFP_SINGLE))
d5a0a47b
RE
7090 *total = COSTS_N_INSNS (1);
7091 else
7092 *total = COSTS_N_INSNS (4);
7093 return true;
f676971a 7094
e2c671ba 7095 default:
d5a0a47b
RE
7096 *total = COSTS_N_INSNS (4);
7097 return false;
e2c671ba
RE
7098 }
7099}
32de079a 7100
7548c1be
WG
7101/* Estimates the size cost of thumb1 instructions.
7102 For now most of the code is copied from thumb1_rtx_costs. We need more
7103 fine grain tuning when we have more related test cases. */
7104static inline int
7105thumb1_size_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
7106{
7107 enum machine_mode mode = GET_MODE (x);
7108
7109 switch (code)
7110 {
7111 case ASHIFT:
7112 case ASHIFTRT:
7113 case LSHIFTRT:
7114 case ROTATERT:
7115 case PLUS:
7116 case MINUS:
7117 case COMPARE:
7118 case NEG:
7119 case NOT:
7120 return COSTS_N_INSNS (1);
7121
7122 case MULT:
7123 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7124 {
7125 /* Thumb1 mul instruction can't operate on const. We must Load it
7126 into a register first. */
7127 int const_size = thumb1_size_rtx_costs (XEXP (x, 1), CONST_INT, SET);
7128 return COSTS_N_INSNS (1) + const_size;
7129 }
7130 return COSTS_N_INSNS (1);
7131
7132 case SET:
7133 return (COSTS_N_INSNS (1)
7134 + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
7135 + GET_CODE (SET_DEST (x)) == MEM));
7136
7137 case CONST_INT:
7138 if (outer == SET)
7139 {
7140 if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
9b9ee6d3 7141 return COSTS_N_INSNS (1);
3393e880
MK
7142 /* See split "TARGET_THUMB1 && satisfies_constraint_J". */
7143 if (INTVAL (x) >= -255 && INTVAL (x) <= -1)
7144 return COSTS_N_INSNS (2);
7145 /* See split "TARGET_THUMB1 && satisfies_constraint_K". */
7548c1be
WG
7146 if (thumb_shiftable_const (INTVAL (x)))
7147 return COSTS_N_INSNS (2);
7148 return COSTS_N_INSNS (3);
7149 }
7150 else if ((outer == PLUS || outer == COMPARE)
7151 && INTVAL (x) < 256 && INTVAL (x) > -256)
7152 return 0;
7153 else if ((outer == IOR || outer == XOR || outer == AND)
7154 && INTVAL (x) < 256 && INTVAL (x) >= -256)
7155 return COSTS_N_INSNS (1);
7156 else if (outer == AND)
7157 {
7158 int i;
7159 /* This duplicates the tests in the andsi3 expander. */
7160 for (i = 9; i <= 31; i++)
7161 if ((((HOST_WIDE_INT) 1) << i) - 1 == INTVAL (x)
7162 || (((HOST_WIDE_INT) 1) << i) - 1 == ~INTVAL (x))
7163 return COSTS_N_INSNS (2);
7164 }
7165 else if (outer == ASHIFT || outer == ASHIFTRT
7166 || outer == LSHIFTRT)
7167 return 0;
7168 return COSTS_N_INSNS (2);
7169
7170 case CONST:
7171 case CONST_DOUBLE:
7172 case LABEL_REF:
7173 case SYMBOL_REF:
7174 return COSTS_N_INSNS (3);
7175
7176 case UDIV:
7177 case UMOD:
7178 case DIV:
7179 case MOD:
7180 return 100;
7181
7182 case TRUNCATE:
7183 return 99;
7184
7185 case AND:
7186 case XOR:
7187 case IOR:
7188 /* XXX guess. */
7189 return 8;
7190
7191 case MEM:
7192 /* XXX another guess. */
7193 /* Memory costs quite a lot for the first word, but subsequent words
7194 load at the equivalent of a single insn each. */
7195 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
7196 + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
7197 ? 4 : 0));
7198
7199 case IF_THEN_ELSE:
7200 /* XXX a guess. */
7201 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
7202 return 14;
7203 return 2;
7204
7205 case ZERO_EXTEND:
7206 /* XXX still guessing. */
7207 switch (GET_MODE (XEXP (x, 0)))
7208 {
7209 case QImode:
7210 return (1 + (mode == DImode ? 4 : 0)
7211 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
7212
7213 case HImode:
7214 return (4 + (mode == DImode ? 4 : 0)
7215 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
7216
7217 case SImode:
7218 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
7219
7220 default:
7221 return 99;
7222 }
7223
7224 default:
7225 return 99;
7226 }
7227}
7228
21b5653c
RE
7229/* RTX costs when optimizing for size. */
7230static bool
d5a0a47b
RE
7231arm_size_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7232 int *total)
21b5653c
RE
7233{
7234 enum machine_mode mode = GET_MODE (x);
09754904 7235 if (TARGET_THUMB1)
21b5653c 7236 {
7548c1be 7237 *total = thumb1_size_rtx_costs (x, code, outer_code);
21b5653c
RE
7238 return true;
7239 }
7240
09754904 7241 /* FIXME: This makes no attempt to prefer narrow Thumb-2 instructions. */
21b5653c
RE
7242 switch (code)
7243 {
7244 case MEM:
f676971a 7245 /* A memory access costs 1 insn if the mode is small, or the address is
21b5653c
RE
7246 a single register, otherwise it costs one insn per word. */
7247 if (REG_P (XEXP (x, 0)))
7248 *total = COSTS_N_INSNS (1);
d37c3c62
MK
7249 else if (flag_pic
7250 && GET_CODE (XEXP (x, 0)) == PLUS
7251 && will_be_in_index_register (XEXP (XEXP (x, 0), 1)))
7252 /* This will be split into two instructions.
7253 See arm.md:calculate_pic_address. */
7254 *total = COSTS_N_INSNS (2);
21b5653c
RE
7255 else
7256 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7257 return true;
7258
7259 case DIV:
7260 case MOD:
7261 case UDIV:
7262 case UMOD:
7263 /* Needs a libcall, so it costs about this. */
7264 *total = COSTS_N_INSNS (2);
7265 return false;
7266
7267 case ROTATE:
7268 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
7269 {
f40751dd 7270 *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, false);
21b5653c
RE
7271 return true;
7272 }
7273 /* Fall through */
7274 case ROTATERT:
7275 case ASHIFT:
7276 case LSHIFTRT:
7277 case ASHIFTRT:
7278 if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
7279 {
f40751dd 7280 *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code, false);
21b5653c
RE
7281 return true;
7282 }
7283 else if (mode == SImode)
7284 {
f40751dd 7285 *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, false);
21b5653c
RE
7286 /* Slightly disparage register shifts, but not by much. */
7287 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
f40751dd 7288 *total += 1 + rtx_cost (XEXP (x, 1), code, false);
21b5653c
RE
7289 return true;
7290 }
7291
7292 /* Needs a libcall. */
7293 *total = COSTS_N_INSNS (2);
7294 return false;
7295
7296 case MINUS:
e0dc3601
PB
7297 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7298 && (mode == SFmode || !TARGET_VFP_SINGLE))
21b5653c
RE
7299 {
7300 *total = COSTS_N_INSNS (1);
7301 return false;
7302 }
7303
7304 if (mode == SImode)
7305 {
7306 enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
7307 enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
7308
7309 if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
7310 || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
7311 || subcode1 == ROTATE || subcode1 == ROTATERT
7312 || subcode1 == ASHIFT || subcode1 == LSHIFTRT
7313 || subcode1 == ASHIFTRT)
7314 {
7315 /* It's just the cost of the two operands. */
7316 *total = 0;
7317 return false;
7318 }
7319
7320 *total = COSTS_N_INSNS (1);
7321 return false;
7322 }
7323
7324 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7325 return false;
7326
f676971a 7327 case PLUS:
e0dc3601
PB
7328 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7329 && (mode == SFmode || !TARGET_VFP_SINGLE))
21b5653c
RE
7330 {
7331 *total = COSTS_N_INSNS (1);
7332 return false;
7333 }
7334
6e782a29
KH
7335 /* A shift as a part of ADD costs nothing. */
7336 if (GET_CODE (XEXP (x, 0)) == MULT
7337 && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
7338 {
7339 *total = COSTS_N_INSNS (TARGET_THUMB2 ? 2 : 1);
7340 *total += rtx_cost (XEXP (XEXP (x, 0), 0), code, false);
7341 *total += rtx_cost (XEXP (x, 1), code, false);
7342 return true;
7343 }
7344
21b5653c
RE
7345 /* Fall through */
7346 case AND: case XOR: case IOR:
7347 if (mode == SImode)
7348 {
7349 enum rtx_code subcode = GET_CODE (XEXP (x, 0));
7350
7351 if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
7352 || subcode == LSHIFTRT || subcode == ASHIFTRT
7353 || (code == AND && subcode == NOT))
7354 {
7355 /* It's just the cost of the two operands. */
7356 *total = 0;
7357 return false;
7358 }
7359 }
7360
7361 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7362 return false;
7363
7364 case MULT:
7365 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7366 return false;
7367
7368 case NEG:
e0dc3601
PB
7369 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7370 && (mode == SFmode || !TARGET_VFP_SINGLE))
7ce8451d
MG
7371 {
7372 *total = COSTS_N_INSNS (1);
7373 return false;
7374 }
7375
21b5653c
RE
7376 /* Fall through */
7377 case NOT:
7378 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7379
7380 return false;
7381
7382 case IF_THEN_ELSE:
7383 *total = 0;
7384 return false;
7385
7386 case COMPARE:
7387 if (cc_register (XEXP (x, 0), VOIDmode))
7388 * total = 0;
7389 else
7390 *total = COSTS_N_INSNS (1);
7391 return false;
7392
7393 case ABS:
e0dc3601
PB
7394 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7395 && (mode == SFmode || !TARGET_VFP_SINGLE))
21b5653c
RE
7396 *total = COSTS_N_INSNS (1);
7397 else
7398 *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
7399 return false;
7400
7401 case SIGN_EXTEND:
21b5653c 7402 case ZERO_EXTEND:
e4c6a07a 7403 return arm_rtx_costs_1 (x, outer_code, total, 0);
21b5653c 7404
f676971a
EC
7405 case CONST_INT:
7406 if (const_ok_for_arm (INTVAL (x)))
6e782a29
KH
7407 /* A multiplication by a constant requires another instruction
7408 to load the constant to a register. */
7409 *total = COSTS_N_INSNS ((outer_code == SET || outer_code == MULT)
7410 ? 1 : 0);
21b5653c
RE
7411 else if (const_ok_for_arm (~INTVAL (x)))
7412 *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
7413 else if (const_ok_for_arm (-INTVAL (x)))
7414 {
7415 if (outer_code == COMPARE || outer_code == PLUS
7416 || outer_code == MINUS)
7417 *total = 0;
7418 else
7419 *total = COSTS_N_INSNS (1);
7420 }
7421 else
7422 *total = COSTS_N_INSNS (2);
7423 return true;
f676971a
EC
7424
7425 case CONST:
7426 case LABEL_REF:
7427 case SYMBOL_REF:
21b5653c
RE
7428 *total = COSTS_N_INSNS (2);
7429 return true;
f676971a 7430
21b5653c
RE
7431 case CONST_DOUBLE:
7432 *total = COSTS_N_INSNS (4);
7433 return true;
7434
571191af
PB
7435 case HIGH:
7436 case LO_SUM:
7437 /* We prefer constant pool entries to MOVW/MOVT pairs, so bump the
7438 cost of these slightly. */
7439 *total = COSTS_N_INSNS (1) + 1;
7440 return true;
7441
21b5653c
RE
7442 default:
7443 if (mode != VOIDmode)
7444 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7445 else
7446 *total = COSTS_N_INSNS (4); /* How knows? */
7447 return false;
7448 }
7449}
7450
f40751dd
JH
7451/* RTX costs when optimizing for size. */
7452static bool
d5a0a47b
RE
7453arm_rtx_costs (rtx x, int code, int outer_code, int *total,
7454 bool speed)
f40751dd
JH
7455{
7456 if (!speed)
bbbbb16a
ILT
7457 return arm_size_rtx_costs (x, (enum rtx_code) code,
7458 (enum rtx_code) outer_code, total);
f40751dd 7459 else
1b78f575
RE
7460 return current_tune->rtx_costs (x, (enum rtx_code) code,
7461 (enum rtx_code) outer_code,
7462 total, speed);
f40751dd
JH
7463}
7464
5b3e6663
PB
7465/* RTX costs for cores with a slow MUL implementation. Thumb-2 is not
7466 supported on any "slowmul" cores, so it can be ignored. */
9b66ebb1 7467
3c50106f 7468static bool
d5a0a47b
RE
7469arm_slowmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7470 int *total, bool speed)
3c50106f 7471{
9b66ebb1
PB
7472 enum machine_mode mode = GET_MODE (x);
7473
7474 if (TARGET_THUMB)
7475 {
5b3e6663 7476 *total = thumb1_rtx_costs (x, code, outer_code);
9b66ebb1
PB
7477 return true;
7478 }
f676971a 7479
9b66ebb1
PB
7480 switch (code)
7481 {
7482 case MULT:
7483 if (GET_MODE_CLASS (mode) == MODE_FLOAT
7484 || mode == DImode)
7485 {
d5a0a47b
RE
7486 *total = COSTS_N_INSNS (20);
7487 return false;
9b66ebb1
PB
7488 }
7489
7490 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7491 {
7492 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
7493 & (unsigned HOST_WIDE_INT) 0xffffffff);
7494 int cost, const_ok = const_ok_for_arm (i);
7495 int j, booth_unit_size;
7496
f676971a 7497 /* Tune as appropriate. */
9b66ebb1
PB
7498 cost = const_ok ? 4 : 8;
7499 booth_unit_size = 2;
7500 for (j = 0; i && j < 32; j += booth_unit_size)
7501 {
7502 i >>= booth_unit_size;
d5a0a47b 7503 cost++;
9b66ebb1
PB
7504 }
7505
d5a0a47b
RE
7506 *total = COSTS_N_INSNS (cost);
7507 *total += rtx_cost (XEXP (x, 0), code, speed);
9b66ebb1
PB
7508 return true;
7509 }
7510
d5a0a47b
RE
7511 *total = COSTS_N_INSNS (20);
7512 return false;
f676971a 7513
9b66ebb1 7514 default:
d5a0a47b 7515 return arm_rtx_costs_1 (x, outer_code, total, speed);;
9b66ebb1 7516 }
3c50106f
RH
7517}
7518
9b66ebb1
PB
7519
7520/* RTX cost for cores with a fast multiply unit (M variants). */
7521
7522static bool
d5a0a47b
RE
7523arm_fastmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7524 int *total, bool speed)
9b66ebb1
PB
7525{
7526 enum machine_mode mode = GET_MODE (x);
7527
5b3e6663 7528 if (TARGET_THUMB1)
9b66ebb1 7529 {
5b3e6663 7530 *total = thumb1_rtx_costs (x, code, outer_code);
9b66ebb1
PB
7531 return true;
7532 }
f676971a 7533
5b3e6663 7534 /* ??? should thumb2 use different costs? */
9b66ebb1
PB
7535 switch (code)
7536 {
7537 case MULT:
7538 /* There is no point basing this on the tuning, since it is always the
7539 fast variant if it exists at all. */
7540 if (mode == DImode
7541 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
7542 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7543 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7544 {
d5a0a47b
RE
7545 *total = COSTS_N_INSNS(2);
7546 return false;
9b66ebb1 7547 }
f676971a 7548
9b66ebb1 7549
d5a0a47b 7550 if (mode == DImode)
9b66ebb1 7551 {
d5a0a47b
RE
7552 *total = COSTS_N_INSNS (5);
7553 return false;
9b66ebb1
PB
7554 }
7555
7556 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7557 {
7558 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
7559 & (unsigned HOST_WIDE_INT) 0xffffffff);
7560 int cost, const_ok = const_ok_for_arm (i);
7561 int j, booth_unit_size;
7562
f676971a 7563 /* Tune as appropriate. */
9b66ebb1
PB
7564 cost = const_ok ? 4 : 8;
7565 booth_unit_size = 8;
7566 for (j = 0; i && j < 32; j += booth_unit_size)
7567 {
7568 i >>= booth_unit_size;
d5a0a47b 7569 cost++;
9b66ebb1
PB
7570 }
7571
d5a0a47b
RE
7572 *total = COSTS_N_INSNS(cost);
7573 return false;
9b66ebb1
PB
7574 }
7575
d5a0a47b
RE
7576 if (mode == SImode)
7577 {
7578 *total = COSTS_N_INSNS (4);
7579 return false;
7580 }
7581
7582 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7583 {
e0dc3601
PB
7584 if (TARGET_HARD_FLOAT
7585 && (mode == SFmode
7586 || (mode == DFmode && !TARGET_VFP_SINGLE)))
d5a0a47b
RE
7587 {
7588 *total = COSTS_N_INSNS (1);
7589 return false;
7590 }
7591 }
7592
7593 /* Requires a lib call */
7594 *total = COSTS_N_INSNS (20);
7595 return false;
f676971a 7596
9b66ebb1 7597 default:
d5a0a47b 7598 return arm_rtx_costs_1 (x, outer_code, total, speed);
9b66ebb1
PB
7599 }
7600}
7601
7602
5b3e6663
PB
7603/* RTX cost for XScale CPUs. Thumb-2 is not supported on any xscale cores,
7604 so it can be ignored. */
9b66ebb1
PB
7605
7606static bool
1b78f575
RE
7607arm_xscale_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7608 int *total, bool speed)
9b66ebb1
PB
7609{
7610 enum machine_mode mode = GET_MODE (x);
7611
7612 if (TARGET_THUMB)
7613 {
5b3e6663 7614 *total = thumb1_rtx_costs (x, code, outer_code);
9b66ebb1
PB
7615 return true;
7616 }
f676971a 7617
9b66ebb1
PB
7618 switch (code)
7619 {
d5a0a47b
RE
7620 case COMPARE:
7621 if (GET_CODE (XEXP (x, 0)) != MULT)
7622 return arm_rtx_costs_1 (x, outer_code, total, speed);
7623
7624 /* A COMPARE of a MULT is slow on XScale; the muls instruction
7625 will stall until the multiplication is complete. */
7626 *total = COSTS_N_INSNS (3);
7627 return false;
7628
9b66ebb1
PB
7629 case MULT:
7630 /* There is no point basing this on the tuning, since it is always the
7631 fast variant if it exists at all. */
7632 if (mode == DImode
7633 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
7634 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7635 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7636 {
d5a0a47b
RE
7637 *total = COSTS_N_INSNS (2);
7638 return false;
9b66ebb1 7639 }
f676971a 7640
9b66ebb1 7641
d5a0a47b 7642 if (mode == DImode)
9b66ebb1 7643 {
d5a0a47b
RE
7644 *total = COSTS_N_INSNS (5);
7645 return false;
9b66ebb1
PB
7646 }
7647
7648 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7649 {
d5a0a47b
RE
7650 /* If operand 1 is a constant we can more accurately
7651 calculate the cost of the multiply. The multiplier can
7652 retire 15 bits on the first cycle and a further 12 on the
7653 second. We do, of course, have to load the constant into
7654 a register first. */
7655 unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
7656 /* There's a general overhead of one cycle. */
7657 int cost = 1;
9b66ebb1
PB
7658 unsigned HOST_WIDE_INT masked_const;
7659
d5a0a47b
RE
7660 if (i & 0x80000000)
7661 i = ~i;
7662
7663 i &= (unsigned HOST_WIDE_INT) 0xffffffff;
7664
9b66ebb1 7665 masked_const = i & 0xffff8000;
d5a0a47b 7666 if (masked_const != 0)
9b66ebb1 7667 {
d5a0a47b 7668 cost++;
9b66ebb1 7669 masked_const = i & 0xf8000000;
d5a0a47b
RE
7670 if (masked_const != 0)
7671 cost++;
9b66ebb1 7672 }
d5a0a47b
RE
7673 *total = COSTS_N_INSNS (cost);
7674 return false;
9b66ebb1
PB
7675 }
7676
d5a0a47b
RE
7677 if (mode == SImode)
7678 {
7679 *total = COSTS_N_INSNS (3);
7680 return false;
7681 }
f676971a 7682
d5a0a47b
RE
7683 /* Requires a lib call */
7684 *total = COSTS_N_INSNS (20);
7685 return false;
06d5588c 7686
9b66ebb1 7687 default:
d5a0a47b 7688 return arm_rtx_costs_1 (x, outer_code, total, speed);
9b66ebb1
PB
7689 }
7690}
7691
7692
7693/* RTX costs for 9e (and later) cores. */
7694
7695static bool
d5a0a47b
RE
7696arm_9e_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7697 int *total, bool speed)
9b66ebb1
PB
7698{
7699 enum machine_mode mode = GET_MODE (x);
f676971a 7700
5b3e6663 7701 if (TARGET_THUMB1)
9b66ebb1
PB
7702 {
7703 switch (code)
7704 {
7705 case MULT:
7706 *total = COSTS_N_INSNS (3);
7707 return true;
f676971a 7708
9b66ebb1 7709 default:
5b3e6663 7710 *total = thumb1_rtx_costs (x, code, outer_code);
9b66ebb1
PB
7711 return true;
7712 }
7713 }
f676971a 7714
9b66ebb1
PB
7715 switch (code)
7716 {
7717 case MULT:
7718 /* There is no point basing this on the tuning, since it is always the
7719 fast variant if it exists at all. */
7720 if (mode == DImode
7721 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
7722 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7723 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7724 {
d5a0a47b
RE
7725 *total = COSTS_N_INSNS (2);
7726 return false;
9b66ebb1 7727 }
f676971a 7728
9b66ebb1 7729
9b66ebb1
PB
7730 if (mode == DImode)
7731 {
d5a0a47b
RE
7732 *total = COSTS_N_INSNS (5);
7733 return false;
9b66ebb1 7734 }
d5a0a47b
RE
7735
7736 if (mode == SImode)
9b66ebb1 7737 {
d5a0a47b
RE
7738 *total = COSTS_N_INSNS (2);
7739 return false;
9b66ebb1
PB
7740 }
7741
d5a0a47b
RE
7742 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7743 {
e0dc3601
PB
7744 if (TARGET_HARD_FLOAT
7745 && (mode == SFmode
7746 || (mode == DFmode && !TARGET_VFP_SINGLE)))
d5a0a47b
RE
7747 {
7748 *total = COSTS_N_INSNS (1);
7749 return false;
7750 }
7751 }
9b66ebb1 7752
d5a0a47b
RE
7753 *total = COSTS_N_INSNS (20);
7754 return false;
f676971a 7755
9b66ebb1 7756 default:
d5a0a47b 7757 return arm_rtx_costs_1 (x, outer_code, total, speed);
9b66ebb1
PB
7758 }
7759}
dcefdf67
RH
7760/* All address computations that can be done are free, but rtx cost returns
7761 the same for practically all of them. So we weight the different types
7762 of address here in the order (most pref first):
d6b4baa4 7763 PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL. */
d2b6eb76
ZW
7764static inline int
7765arm_arm_address_cost (rtx x)
7766{
7767 enum rtx_code c = GET_CODE (x);
7768
7769 if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
7770 return 0;
7771 if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
7772 return 10;
7773
17eb4921 7774 if (c == PLUS)
d2b6eb76 7775 {
17eb4921 7776 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
d2b6eb76
ZW
7777 return 2;
7778
ec8e098d 7779 if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
d2b6eb76
ZW
7780 return 3;
7781
7782 return 4;
7783 }
7784
7785 return 6;
7786}
7787
7788static inline int
7789arm_thumb_address_cost (rtx x)
7790{
7791 enum rtx_code c = GET_CODE (x);
7792
7793 if (c == REG)
7794 return 1;
7795 if (c == PLUS
7796 && GET_CODE (XEXP (x, 0)) == REG
7797 && GET_CODE (XEXP (x, 1)) == CONST_INT)
7798 return 1;
7799
7800 return 2;
7801}
7802
dcefdf67 7803static int
f40751dd 7804arm_address_cost (rtx x, bool speed ATTRIBUTE_UNUSED)
dcefdf67 7805{
5b3e6663 7806 return TARGET_32BIT ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
dcefdf67 7807}
906668bb 7808
b0c13111
RR
7809/* Adjust cost hook for XScale. */
7810static bool
7811xscale_sched_adjust_cost (rtx insn, rtx link, rtx dep, int * cost)
7812{
d19fb8e3
NC
7813 /* Some true dependencies can have a higher cost depending
7814 on precisely how certain input operands are used. */
b0c13111 7815 if (REG_NOTE_KIND(link) == 0
eda833e3
BE
7816 && recog_memoized (insn) >= 0
7817 && recog_memoized (dep) >= 0)
d19fb8e3
NC
7818 {
7819 int shift_opnum = get_attr_shift (insn);
7820 enum attr_type attr_type = get_attr_type (dep);
7821
7822 /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
7823 operand for INSN. If we have a shifted input operand and the
7824 instruction we depend on is another ALU instruction, then we may
7825 have to account for an additional stall. */
9b66ebb1
PB
7826 if (shift_opnum != 0
7827 && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
d19fb8e3
NC
7828 {
7829 rtx shifted_operand;
7830 int opno;
f676971a 7831
d19fb8e3
NC
7832 /* Get the shifted operand. */
7833 extract_insn (insn);
7834 shifted_operand = recog_data.operand[shift_opnum];
7835
7836 /* Iterate over all the operands in DEP. If we write an operand
7837 that overlaps with SHIFTED_OPERAND, then we have increase the
7838 cost of this dependency. */
7839 extract_insn (dep);
7840 preprocess_constraints ();
7841 for (opno = 0; opno < recog_data.n_operands; opno++)
7842 {
7843 /* We can ignore strict inputs. */
7844 if (recog_data.operand_type[opno] == OP_IN)
7845 continue;
7846
7847 if (reg_overlap_mentioned_p (recog_data.operand[opno],
7848 shifted_operand))
b0c13111
RR
7849 {
7850 *cost = 2;
7851 return false;
7852 }
d19fb8e3
NC
7853 }
7854 }
7855 }
b0c13111
RR
7856 return true;
7857}
7858
7859/* Adjust cost hook for Cortex A9. */
7860static bool
7861cortex_a9_sched_adjust_cost (rtx insn, rtx link, rtx dep, int * cost)
7862{
7863 switch (REG_NOTE_KIND (link))
7864 {
7865 case REG_DEP_ANTI:
7866 *cost = 0;
7867 return false;
7868
7869 case REG_DEP_TRUE:
7870 case REG_DEP_OUTPUT:
7871 if (recog_memoized (insn) >= 0
7872 && recog_memoized (dep) >= 0)
7873 {
7874 if (GET_CODE (PATTERN (insn)) == SET)
7875 {
7876 if (GET_MODE_CLASS
7877 (GET_MODE (SET_DEST (PATTERN (insn)))) == MODE_FLOAT
7878 || GET_MODE_CLASS
7879 (GET_MODE (SET_SRC (PATTERN (insn)))) == MODE_FLOAT)
7880 {
7881 enum attr_type attr_type_insn = get_attr_type (insn);
7882 enum attr_type attr_type_dep = get_attr_type (dep);
7883
7884 /* By default all dependencies of the form
7885 s0 = s0 <op> s1
7886 s0 = s0 <op> s2
7887 have an extra latency of 1 cycle because
7888 of the input and output dependency in this
7889 case. However this gets modeled as an true
7890 dependency and hence all these checks. */
7891 if (REG_P (SET_DEST (PATTERN (insn)))
7892 && REG_P (SET_DEST (PATTERN (dep)))
7893 && reg_overlap_mentioned_p (SET_DEST (PATTERN (insn)),
7894 SET_DEST (PATTERN (dep))))
7895 {
7896 /* FMACS is a special case where the dependant
7897 instruction can be issued 3 cycles before
7898 the normal latency in case of an output
7899 dependency. */
7900 if ((attr_type_insn == TYPE_FMACS
7901 || attr_type_insn == TYPE_FMACD)
7902 && (attr_type_dep == TYPE_FMACS
7903 || attr_type_dep == TYPE_FMACD))
7904 {
7905 if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
7906 *cost = insn_default_latency (dep) - 3;
7907 else
7908 *cost = insn_default_latency (dep);
7909 return false;
7910 }
7911 else
7912 {
7913 if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
7914 *cost = insn_default_latency (dep) + 1;
7915 else
7916 *cost = insn_default_latency (dep);
7917 }
7918 return false;
7919 }
7920 }
7921 }
7922 }
7923 break;
7924
7925 default:
7926 gcc_unreachable ();
7927 }
7928
7929 return true;
7930}
7931
7932/* This function implements the target macro TARGET_SCHED_ADJUST_COST.
7933 It corrects the value of COST based on the relationship between
7934 INSN and DEP through the dependence LINK. It returns the new
7935 value. There is a per-core adjust_cost hook to adjust scheduler costs
7936 and the per-core hook can choose to completely override the generic
7937 adjust_cost function. Only put bits of code into arm_adjust_cost that
7938 are common across all cores. */
7939static int
7940arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
7941{
7942 rtx i_pat, d_pat;
7943
7944 /* When generating Thumb-1 code, we want to place flag-setting operations
7945 close to a conditional branch which depends on them, so that we can
7946 omit the comparison. */
7947 if (TARGET_THUMB1
7948 && REG_NOTE_KIND (link) == 0
7949 && recog_memoized (insn) == CODE_FOR_cbranchsi4_insn
7950 && recog_memoized (dep) >= 0
7951 && get_attr_conds (dep) == CONDS_SET)
7952 return 0;
7953
7954 if (current_tune->sched_adjust_cost != NULL)
7955 {
7956 if (!current_tune->sched_adjust_cost (insn, link, dep, &cost))
7957 return cost;
7958 }
d19fb8e3 7959
6354dc9b 7960 /* XXX This is not strictly true for the FPA. */
d5b7b3ae
RE
7961 if (REG_NOTE_KIND (link) == REG_DEP_ANTI
7962 || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
b36ba79f
RE
7963 return 0;
7964
d5b7b3ae
RE
7965 /* Call insns don't incur a stall, even if they follow a load. */
7966 if (REG_NOTE_KIND (link) == 0
7967 && GET_CODE (insn) == CALL_INSN)
7968 return 1;
7969
32de079a
RE
7970 if ((i_pat = single_set (insn)) != NULL
7971 && GET_CODE (SET_SRC (i_pat)) == MEM
7972 && (d_pat = single_set (dep)) != NULL
7973 && GET_CODE (SET_DEST (d_pat)) == MEM)
7974 {
48f6efae 7975 rtx src_mem = XEXP (SET_SRC (i_pat), 0);
32de079a
RE
7976 /* This is a load after a store, there is no conflict if the load reads
7977 from a cached area. Assume that loads from the stack, and from the
f676971a 7978 constant pool are cached, and that others will miss. This is a
6354dc9b 7979 hack. */
f676971a 7980
b0c13111
RR
7981 if ((GET_CODE (src_mem) == SYMBOL_REF
7982 && CONSTANT_POOL_ADDRESS_P (src_mem))
48f6efae
NC
7983 || reg_mentioned_p (stack_pointer_rtx, src_mem)
7984 || reg_mentioned_p (frame_pointer_rtx, src_mem)
7985 || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
949d79eb 7986 return 1;
32de079a
RE
7987 }
7988
7989 return cost;
7990}
7991
9b66ebb1 7992static int fp_consts_inited = 0;
ff9940b0 7993
9b66ebb1
PB
7994/* Only zero is valid for VFP. Other values are also valid for FPA. */
7995static const char * const strings_fp[8] =
62b10bbc 7996{
2b835d68
RE
7997 "0", "1", "2", "3",
7998 "4", "5", "0.5", "10"
7999};
ff9940b0 8000
9b66ebb1 8001static REAL_VALUE_TYPE values_fp[8];
ff9940b0
RE
8002
8003static void
9b66ebb1 8004init_fp_table (void)
ff9940b0
RE
8005{
8006 int i;
8007 REAL_VALUE_TYPE r;
8008
9b66ebb1
PB
8009 if (TARGET_VFP)
8010 fp_consts_inited = 1;
8011 else
8012 fp_consts_inited = 8;
8013
8014 for (i = 0; i < fp_consts_inited; i++)
ff9940b0 8015 {
9b66ebb1
PB
8016 r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
8017 values_fp[i] = r;
ff9940b0 8018 }
ff9940b0
RE
8019}
8020
9b66ebb1 8021/* Return TRUE if rtx X is a valid immediate FP constant. */
cce8749e 8022int
9b66ebb1 8023arm_const_double_rtx (rtx x)
cce8749e 8024{
ff9940b0
RE
8025 REAL_VALUE_TYPE r;
8026 int i;
f676971a 8027
9b66ebb1
PB
8028 if (!fp_consts_inited)
8029 init_fp_table ();
f676971a 8030
ff9940b0
RE
8031 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8032 if (REAL_VALUE_MINUS_ZERO (r))
8033 return 0;
f3bb6135 8034
9b66ebb1
PB
8035 for (i = 0; i < fp_consts_inited; i++)
8036 if (REAL_VALUES_EQUAL (r, values_fp[i]))
ff9940b0 8037 return 1;
f3bb6135 8038
ff9940b0 8039 return 0;
f3bb6135 8040}
ff9940b0 8041
3b684012 8042/* Return TRUE if rtx X is a valid immediate FPA constant. */
ff9940b0 8043int
e32bac5b 8044neg_const_double_rtx_ok_for_fpa (rtx x)
ff9940b0
RE
8045{
8046 REAL_VALUE_TYPE r;
8047 int i;
f676971a 8048
9b66ebb1
PB
8049 if (!fp_consts_inited)
8050 init_fp_table ();
f676971a 8051
ff9940b0 8052 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
d49b6e1e 8053 r = real_value_negate (&r);
ff9940b0
RE
8054 if (REAL_VALUE_MINUS_ZERO (r))
8055 return 0;
f3bb6135 8056
ff9940b0 8057 for (i = 0; i < 8; i++)
9b66ebb1 8058 if (REAL_VALUES_EQUAL (r, values_fp[i]))
ff9940b0 8059 return 1;
f3bb6135 8060
ff9940b0 8061 return 0;
f3bb6135 8062}
f1adb0a9
JB
8063
8064
8065/* VFPv3 has a fairly wide range of representable immediates, formed from
8066 "quarter-precision" floating-point values. These can be evaluated using this
8067 formula (with ^ for exponentiation):
8068
8069 -1^s * n * 2^-r
8070
8071 Where 's' is a sign bit (0/1), 'n' and 'r' are integers such that
8072 16 <= n <= 31 and 0 <= r <= 7.
8073
8074 These values are mapped onto an 8-bit integer ABCDEFGH s.t.
8075
8076 - A (most-significant) is the sign bit.
8077 - BCD are the exponent (encoded as r XOR 3).
8078 - EFGH are the mantissa (encoded as n - 16).
8079*/
8080
8081/* Return an integer index for a VFPv3 immediate operand X suitable for the
8082 fconst[sd] instruction, or -1 if X isn't suitable. */
8083static int
8084vfp3_const_double_index (rtx x)
8085{
8086 REAL_VALUE_TYPE r, m;
8087 int sign, exponent;
8088 unsigned HOST_WIDE_INT mantissa, mant_hi;
8089 unsigned HOST_WIDE_INT mask;
8e39e9af 8090 HOST_WIDE_INT m1, m2;
f1adb0a9
JB
8091 int point_pos = 2 * HOST_BITS_PER_WIDE_INT - 1;
8092
8093 if (!TARGET_VFP3 || GET_CODE (x) != CONST_DOUBLE)
8094 return -1;
8095
8096 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8097
8098 /* We can't represent these things, so detect them first. */
8099 if (REAL_VALUE_ISINF (r) || REAL_VALUE_ISNAN (r) || REAL_VALUE_MINUS_ZERO (r))
8100 return -1;
8101
8102 /* Extract sign, exponent and mantissa. */
8103 sign = REAL_VALUE_NEGATIVE (r) ? 1 : 0;
d49b6e1e 8104 r = real_value_abs (&r);
f1adb0a9
JB
8105 exponent = REAL_EXP (&r);
8106 /* For the mantissa, we expand into two HOST_WIDE_INTS, apart from the
8107 highest (sign) bit, with a fixed binary point at bit point_pos.
8108 WARNING: If there's ever a VFP version which uses more than 2 * H_W_I - 1
8109 bits for the mantissa, this may fail (low bits would be lost). */
8110 real_ldexp (&m, &r, point_pos - exponent);
8e39e9af
RE
8111 REAL_VALUE_TO_INT (&m1, &m2, m);
8112 mantissa = m1;
8113 mant_hi = m2;
f1adb0a9
JB
8114
8115 /* If there are bits set in the low part of the mantissa, we can't
8116 represent this value. */
8117 if (mantissa != 0)
8118 return -1;
8119
8120 /* Now make it so that mantissa contains the most-significant bits, and move
8121 the point_pos to indicate that the least-significant bits have been
8122 discarded. */
8123 point_pos -= HOST_BITS_PER_WIDE_INT;
8124 mantissa = mant_hi;
8125
8126 /* We can permit four significant bits of mantissa only, plus a high bit
8127 which is always 1. */
8128 mask = ((unsigned HOST_WIDE_INT)1 << (point_pos - 5)) - 1;
8129 if ((mantissa & mask) != 0)
8130 return -1;
8131
8132 /* Now we know the mantissa is in range, chop off the unneeded bits. */
8133 mantissa >>= point_pos - 5;
8134
8135 /* The mantissa may be zero. Disallow that case. (It's possible to load the
8136 floating-point immediate zero with Neon using an integer-zero load, but
8137 that case is handled elsewhere.) */
8138 if (mantissa == 0)
8139 return -1;
8140
8141 gcc_assert (mantissa >= 16 && mantissa <= 31);
8142
8143 /* The value of 5 here would be 4 if GCC used IEEE754-like encoding (where
6ed3da00
KH
8144 normalized significands are in the range [1, 2). (Our mantissa is shifted
8145 left 4 places at this point relative to normalized IEEE754 values). GCC
f1adb0a9
JB
8146 internally uses [0.5, 1) (see real.c), so the exponent returned from
8147 REAL_EXP must be altered. */
8148 exponent = 5 - exponent;
8149
8150 if (exponent < 0 || exponent > 7)
8151 return -1;
8152
8153 /* Sign, mantissa and exponent are now in the correct form to plug into the
15dc95cb 8154 formula described in the comment above. */
f1adb0a9
JB
8155 return (sign << 7) | ((exponent ^ 3) << 4) | (mantissa - 16);
8156}
8157
8158/* Return TRUE if rtx X is a valid immediate VFPv3 constant. */
8159int
8160vfp3_const_double_rtx (rtx x)
8161{
8162 if (!TARGET_VFP3)
8163 return 0;
8164
8165 return vfp3_const_double_index (x) != -1;
8166}
8167
88f77cba
JB
8168/* Recognize immediates which can be used in various Neon instructions. Legal
8169 immediates are described by the following table (for VMVN variants, the
8170 bitwise inverse of the constant shown is recognized. In either case, VMOV
8171 is output and the correct instruction to use for a given constant is chosen
8172 by the assembler). The constant shown is replicated across all elements of
8173 the destination vector.
8174
8175 insn elems variant constant (binary)
8176 ---- ----- ------- -----------------
8177 vmov i32 0 00000000 00000000 00000000 abcdefgh
8178 vmov i32 1 00000000 00000000 abcdefgh 00000000
8179 vmov i32 2 00000000 abcdefgh 00000000 00000000
8180 vmov i32 3 abcdefgh 00000000 00000000 00000000
8181 vmov i16 4 00000000 abcdefgh
8182 vmov i16 5 abcdefgh 00000000
8183 vmvn i32 6 00000000 00000000 00000000 abcdefgh
8184 vmvn i32 7 00000000 00000000 abcdefgh 00000000
8185 vmvn i32 8 00000000 abcdefgh 00000000 00000000
8186 vmvn i32 9 abcdefgh 00000000 00000000 00000000
8187 vmvn i16 10 00000000 abcdefgh
8188 vmvn i16 11 abcdefgh 00000000
8189 vmov i32 12 00000000 00000000 abcdefgh 11111111
8190 vmvn i32 13 00000000 00000000 abcdefgh 11111111
8191 vmov i32 14 00000000 abcdefgh 11111111 11111111
8192 vmvn i32 15 00000000 abcdefgh 11111111 11111111
8193 vmov i8 16 abcdefgh
8194 vmov i64 17 aaaaaaaa bbbbbbbb cccccccc dddddddd
8195 eeeeeeee ffffffff gggggggg hhhhhhhh
8196 vmov f32 18 aBbbbbbc defgh000 00000000 00000000
8197
8198 For case 18, B = !b. Representable values are exactly those accepted by
8199 vfp3_const_double_index, but are output as floating-point numbers rather
8200 than indices.
8201
8202 Variants 0-5 (inclusive) may also be used as immediates for the second
8203 operand of VORR/VBIC instructions.
8204
8205 The INVERSE argument causes the bitwise inverse of the given operand to be
8206 recognized instead (used for recognizing legal immediates for the VAND/VORN
8207 pseudo-instructions). If INVERSE is true, the value placed in *MODCONST is
8208 *not* inverted (i.e. the pseudo-instruction forms vand/vorn should still be
8209 output, rather than the real insns vbic/vorr).
8210
8211 INVERSE makes no difference to the recognition of float vectors.
8212
8213 The return value is the variant of immediate as shown in the above table, or
8214 -1 if the given value doesn't match any of the listed patterns.
8215*/
8216static int
8217neon_valid_immediate (rtx op, enum machine_mode mode, int inverse,
8218 rtx *modconst, int *elementwidth)
8219{
8220#define CHECK(STRIDE, ELSIZE, CLASS, TEST) \
8221 matches = 1; \
8222 for (i = 0; i < idx; i += (STRIDE)) \
8223 if (!(TEST)) \
8224 matches = 0; \
8225 if (matches) \
8226 { \
8227 immtype = (CLASS); \
8228 elsize = (ELSIZE); \
8229 break; \
8230 }
8231
ff128632 8232 unsigned int i, elsize = 0, idx = 0, n_elts = CONST_VECTOR_NUNITS (op);
88f77cba
JB
8233 unsigned int innersize = GET_MODE_SIZE (GET_MODE_INNER (mode));
8234 unsigned char bytes[16];
8235 int immtype = -1, matches;
8236 unsigned int invmask = inverse ? 0xff : 0;
8237
8238 /* Vectors of float constants. */
8239 if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
8240 {
8241 rtx el0 = CONST_VECTOR_ELT (op, 0);
8242 REAL_VALUE_TYPE r0;
8243
8244 if (!vfp3_const_double_rtx (el0))
8245 return -1;
8246
8247 REAL_VALUE_FROM_CONST_DOUBLE (r0, el0);
8248
8249 for (i = 1; i < n_elts; i++)
8250 {
8251 rtx elt = CONST_VECTOR_ELT (op, i);
8252 REAL_VALUE_TYPE re;
8253
8254 REAL_VALUE_FROM_CONST_DOUBLE (re, elt);
8255
8256 if (!REAL_VALUES_EQUAL (r0, re))
8257 return -1;
8258 }
8259
8260 if (modconst)
8261 *modconst = CONST_VECTOR_ELT (op, 0);
8262
8263 if (elementwidth)
8264 *elementwidth = 0;
8265
8266 return 18;
8267 }
8268
8269 /* Splat vector constant out into a byte vector. */
8270 for (i = 0; i < n_elts; i++)
8271 {
8272 rtx el = CONST_VECTOR_ELT (op, i);
8273 unsigned HOST_WIDE_INT elpart;
8274 unsigned int part, parts;
8275
8276 if (GET_CODE (el) == CONST_INT)
8277 {
8278 elpart = INTVAL (el);
8279 parts = 1;
8280 }
8281 else if (GET_CODE (el) == CONST_DOUBLE)
8282 {
8283 elpart = CONST_DOUBLE_LOW (el);
8284 parts = 2;
8285 }
8286 else
8287 gcc_unreachable ();
8288
8289 for (part = 0; part < parts; part++)
8290 {
8291 unsigned int byte;
8292 for (byte = 0; byte < innersize; byte++)
8293 {
8294 bytes[idx++] = (elpart & 0xff) ^ invmask;
8295 elpart >>= BITS_PER_UNIT;
8296 }
8297 if (GET_CODE (el) == CONST_DOUBLE)
8298 elpart = CONST_DOUBLE_HIGH (el);
8299 }
8300 }
8301
8302 /* Sanity check. */
8303 gcc_assert (idx == GET_MODE_SIZE (mode));
8304
8305 do
8306 {
8307 CHECK (4, 32, 0, bytes[i] == bytes[0] && bytes[i + 1] == 0
8308 && bytes[i + 2] == 0 && bytes[i + 3] == 0);
8309
8310 CHECK (4, 32, 1, bytes[i] == 0 && bytes[i + 1] == bytes[1]
8311 && bytes[i + 2] == 0 && bytes[i + 3] == 0);
8312
8313 CHECK (4, 32, 2, bytes[i] == 0 && bytes[i + 1] == 0
8314 && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
8315
8316 CHECK (4, 32, 3, bytes[i] == 0 && bytes[i + 1] == 0
8317 && bytes[i + 2] == 0 && bytes[i + 3] == bytes[3]);
8318
8319 CHECK (2, 16, 4, bytes[i] == bytes[0] && bytes[i + 1] == 0);
8320
8321 CHECK (2, 16, 5, bytes[i] == 0 && bytes[i + 1] == bytes[1]);
8322
8323 CHECK (4, 32, 6, bytes[i] == bytes[0] && bytes[i + 1] == 0xff
8324 && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
8325
8326 CHECK (4, 32, 7, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
8327 && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
8328
8329 CHECK (4, 32, 8, bytes[i] == 0xff && bytes[i + 1] == 0xff
8330 && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
8331
8332 CHECK (4, 32, 9, bytes[i] == 0xff && bytes[i + 1] == 0xff
8333 && bytes[i + 2] == 0xff && bytes[i + 3] == bytes[3]);
8334
8335 CHECK (2, 16, 10, bytes[i] == bytes[0] && bytes[i + 1] == 0xff);
8336
8337 CHECK (2, 16, 11, bytes[i] == 0xff && bytes[i + 1] == bytes[1]);
8338
8339 CHECK (4, 32, 12, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
8340 && bytes[i + 2] == 0 && bytes[i + 3] == 0);
8341
8342 CHECK (4, 32, 13, bytes[i] == 0 && bytes[i + 1] == bytes[1]
8343 && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
8344
8345 CHECK (4, 32, 14, bytes[i] == 0xff && bytes[i + 1] == 0xff
8346 && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
8347
8348 CHECK (4, 32, 15, bytes[i] == 0 && bytes[i + 1] == 0
8349 && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
8350
8351 CHECK (1, 8, 16, bytes[i] == bytes[0]);
8352
8353 CHECK (1, 64, 17, (bytes[i] == 0 || bytes[i] == 0xff)
8354 && bytes[i] == bytes[(i + 8) % idx]);
8355 }
8356 while (0);
8357
8358 if (immtype == -1)
8359 return -1;
8360
8361 if (elementwidth)
8362 *elementwidth = elsize;
8363
8364 if (modconst)
8365 {
8366 unsigned HOST_WIDE_INT imm = 0;
8367
cea618ac 8368 /* Un-invert bytes of recognized vector, if necessary. */
88f77cba
JB
8369 if (invmask != 0)
8370 for (i = 0; i < idx; i++)
8371 bytes[i] ^= invmask;
8372
8373 if (immtype == 17)
8374 {
8375 /* FIXME: Broken on 32-bit H_W_I hosts. */
8376 gcc_assert (sizeof (HOST_WIDE_INT) == 8);
8377
8378 for (i = 0; i < 8; i++)
8379 imm |= (unsigned HOST_WIDE_INT) (bytes[i] ? 0xff : 0)
8380 << (i * BITS_PER_UNIT);
8381
8382 *modconst = GEN_INT (imm);
8383 }
8384 else
8385 {
8386 unsigned HOST_WIDE_INT imm = 0;
8387
8388 for (i = 0; i < elsize / BITS_PER_UNIT; i++)
8389 imm |= (unsigned HOST_WIDE_INT) bytes[i] << (i * BITS_PER_UNIT);
8390
8391 *modconst = GEN_INT (imm);
8392 }
8393 }
8394
8395 return immtype;
8396#undef CHECK
8397}
8398
8399/* Return TRUE if rtx X is legal for use as either a Neon VMOV (or, implicitly,
8400 VMVN) immediate. Write back width per element to *ELEMENTWIDTH (or zero for
8401 float elements), and a modified constant (whatever should be output for a
8402 VMOV) in *MODCONST. */
8403
8404int
8405neon_immediate_valid_for_move (rtx op, enum machine_mode mode,
8406 rtx *modconst, int *elementwidth)
8407{
8408 rtx tmpconst;
8409 int tmpwidth;
8410 int retval = neon_valid_immediate (op, mode, 0, &tmpconst, &tmpwidth);
8411
8412 if (retval == -1)
8413 return 0;
8414
8415 if (modconst)
8416 *modconst = tmpconst;
8417
8418 if (elementwidth)
8419 *elementwidth = tmpwidth;
8420
8421 return 1;
8422}
8423
8424/* Return TRUE if rtx X is legal for use in a VORR or VBIC instruction. If
8425 the immediate is valid, write a constant suitable for using as an operand
8426 to VORR/VBIC/VAND/VORN to *MODCONST and the corresponding element width to
8427 *ELEMENTWIDTH. See neon_valid_immediate for description of INVERSE. */
8428
8429int
8430neon_immediate_valid_for_logic (rtx op, enum machine_mode mode, int inverse,
8431 rtx *modconst, int *elementwidth)
8432{
8433 rtx tmpconst;
8434 int tmpwidth;
8435 int retval = neon_valid_immediate (op, mode, inverse, &tmpconst, &tmpwidth);
8436
8437 if (retval < 0 || retval > 5)
8438 return 0;
8439
8440 if (modconst)
8441 *modconst = tmpconst;
8442
8443 if (elementwidth)
8444 *elementwidth = tmpwidth;
8445
8446 return 1;
8447}
8448
8449/* Return a string suitable for output of Neon immediate logic operation
8450 MNEM. */
8451
8452char *
8453neon_output_logic_immediate (const char *mnem, rtx *op2, enum machine_mode mode,
8454 int inverse, int quad)
8455{
8456 int width, is_valid;
8457 static char templ[40];
8458
8459 is_valid = neon_immediate_valid_for_logic (*op2, mode, inverse, op2, &width);
8460
8461 gcc_assert (is_valid != 0);
8462
8463 if (quad)
8464 sprintf (templ, "%s.i%d\t%%q0, %%2", mnem, width);
8465 else
8466 sprintf (templ, "%s.i%d\t%%P0, %%2", mnem, width);
8467
8468 return templ;
8469}
8470
8471/* Output a sequence of pairwise operations to implement a reduction.
8472 NOTE: We do "too much work" here, because pairwise operations work on two
8473 registers-worth of operands in one go. Unfortunately we can't exploit those
8474 extra calculations to do the full operation in fewer steps, I don't think.
8475 Although all vector elements of the result but the first are ignored, we
8476 actually calculate the same result in each of the elements. An alternative
8477 such as initially loading a vector with zero to use as each of the second
8478 operands would use up an additional register and take an extra instruction,
8479 for no particular gain. */
8480
8481void
8482neon_pairwise_reduce (rtx op0, rtx op1, enum machine_mode mode,
8483 rtx (*reduc) (rtx, rtx, rtx))
8484{
8485 enum machine_mode inner = GET_MODE_INNER (mode);
8486 unsigned int i, parts = GET_MODE_SIZE (mode) / GET_MODE_SIZE (inner);
8487 rtx tmpsum = op1;
8488
8489 for (i = parts / 2; i >= 1; i /= 2)
8490 {
8491 rtx dest = (i == 1) ? op0 : gen_reg_rtx (mode);
8492 emit_insn (reduc (dest, tmpsum, tmpsum));
8493 tmpsum = dest;
8494 }
8495}
8496
814a4c3b
DJ
8497/* If VALS is a vector constant that can be loaded into a register
8498 using VDUP, generate instructions to do so and return an RTX to
8499 assign to the register. Otherwise return NULL_RTX. */
8500
8501static rtx
8502neon_vdup_constant (rtx vals)
8503{
8504 enum machine_mode mode = GET_MODE (vals);
8505 enum machine_mode inner_mode = GET_MODE_INNER (mode);
8506 int n_elts = GET_MODE_NUNITS (mode);
8507 bool all_same = true;
8508 rtx x;
8509 int i;
8510
8511 if (GET_CODE (vals) != CONST_VECTOR || GET_MODE_SIZE (inner_mode) > 4)
8512 return NULL_RTX;
8513
8514 for (i = 0; i < n_elts; ++i)
8515 {
8516 x = XVECEXP (vals, 0, i);
8517 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
8518 all_same = false;
8519 }
8520
8521 if (!all_same)
8522 /* The elements are not all the same. We could handle repeating
8523 patterns of a mode larger than INNER_MODE here (e.g. int8x8_t
8524 {0, C, 0, C, 0, C, 0, C} which can be loaded using
8525 vdup.i16). */
8526 return NULL_RTX;
8527
8528 /* We can load this constant by using VDUP and a constant in a
8529 single ARM register. This will be cheaper than a vector
8530 load. */
8531
8532 x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, 0));
a277dd9b 8533 return gen_rtx_VEC_DUPLICATE (mode, x);
814a4c3b
DJ
8534}
8535
8536/* Generate code to load VALS, which is a PARALLEL containing only
8537 constants (for vec_init) or CONST_VECTOR, efficiently into a
8538 register. Returns an RTX to copy into the register, or NULL_RTX
8539 for a PARALLEL that can not be converted into a CONST_VECTOR. */
8540
8541rtx
8542neon_make_constant (rtx vals)
8543{
8544 enum machine_mode mode = GET_MODE (vals);
8545 rtx target;
8546 rtx const_vec = NULL_RTX;
8547 int n_elts = GET_MODE_NUNITS (mode);
8548 int n_const = 0;
8549 int i;
8550
8551 if (GET_CODE (vals) == CONST_VECTOR)
8552 const_vec = vals;
8553 else if (GET_CODE (vals) == PARALLEL)
8554 {
8555 /* A CONST_VECTOR must contain only CONST_INTs and
8556 CONST_DOUBLEs, but CONSTANT_P allows more (e.g. SYMBOL_REF).
8557 Only store valid constants in a CONST_VECTOR. */
8558 for (i = 0; i < n_elts; ++i)
8559 {
8560 rtx x = XVECEXP (vals, 0, i);
8561 if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
8562 n_const++;
8563 }
8564 if (n_const == n_elts)
8565 const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
8566 }
8567 else
8568 gcc_unreachable ();
8569
8570 if (const_vec != NULL
8571 && neon_immediate_valid_for_move (const_vec, mode, NULL, NULL))
8572 /* Load using VMOV. On Cortex-A8 this takes one cycle. */
8573 return const_vec;
8574 else if ((target = neon_vdup_constant (vals)) != NULL_RTX)
8575 /* Loaded using VDUP. On Cortex-A8 the VDUP takes one NEON
8576 pipeline cycle; creating the constant takes one or two ARM
8577 pipeline cycles. */
8578 return target;
8579 else if (const_vec != NULL_RTX)
8580 /* Load from constant pool. On Cortex-A8 this takes two cycles
8581 (for either double or quad vectors). We can not take advantage
8582 of single-cycle VLD1 because we need a PC-relative addressing
8583 mode. */
8584 return const_vec;
8585 else
8586 /* A PARALLEL containing something not valid inside CONST_VECTOR.
8587 We can not construct an initializer. */
8588 return NULL_RTX;
8589}
8590
8591/* Initialize vector TARGET to VALS. */
88f77cba
JB
8592
8593void
8594neon_expand_vector_init (rtx target, rtx vals)
8595{
8596 enum machine_mode mode = GET_MODE (target);
814a4c3b
DJ
8597 enum machine_mode inner_mode = GET_MODE_INNER (mode);
8598 int n_elts = GET_MODE_NUNITS (mode);
8599 int n_var = 0, one_var = -1;
8600 bool all_same = true;
8601 rtx x, mem;
8602 int i;
88f77cba 8603
814a4c3b
DJ
8604 for (i = 0; i < n_elts; ++i)
8605 {
8606 x = XVECEXP (vals, 0, i);
8607 if (!CONSTANT_P (x))
8608 ++n_var, one_var = i;
8609
8610 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
8611 all_same = false;
8612 }
88f77cba 8613
814a4c3b
DJ
8614 if (n_var == 0)
8615 {
8616 rtx constant = neon_make_constant (vals);
8617 if (constant != NULL_RTX)
8618 {
8619 emit_move_insn (target, constant);
8620 return;
8621 }
8622 }
8623
8624 /* Splat a single non-constant element if we can. */
8625 if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
8626 {
8627 x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, 0));
8628 emit_insn (gen_rtx_SET (VOIDmode, target,
a277dd9b 8629 gen_rtx_VEC_DUPLICATE (mode, x)));
814a4c3b
DJ
8630 return;
8631 }
8632
8633 /* One field is non-constant. Load constant then overwrite varying
8634 field. This is more efficient than using the stack. */
8635 if (n_var == 1)
8636 {
8637 rtx copy = copy_rtx (vals);
a277dd9b 8638 rtx index = GEN_INT (one_var);
814a4c3b
DJ
8639
8640 /* Load constant part of vector, substitute neighboring value for
8641 varying element. */
8642 XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
8643 neon_expand_vector_init (target, copy);
8644
8645 /* Insert variable. */
8646 x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, one_var));
a277dd9b
SL
8647 switch (mode)
8648 {
8649 case V8QImode:
8650 emit_insn (gen_neon_vset_lanev8qi (target, x, target, index));
8651 break;
8652 case V16QImode:
8653 emit_insn (gen_neon_vset_lanev16qi (target, x, target, index));
8654 break;
8655 case V4HImode:
8656 emit_insn (gen_neon_vset_lanev4hi (target, x, target, index));
8657 break;
8658 case V8HImode:
8659 emit_insn (gen_neon_vset_lanev8hi (target, x, target, index));
8660 break;
8661 case V2SImode:
8662 emit_insn (gen_neon_vset_lanev2si (target, x, target, index));
8663 break;
8664 case V4SImode:
8665 emit_insn (gen_neon_vset_lanev4si (target, x, target, index));
8666 break;
8667 case V2SFmode:
8668 emit_insn (gen_neon_vset_lanev2sf (target, x, target, index));
8669 break;
8670 case V4SFmode:
8671 emit_insn (gen_neon_vset_lanev4sf (target, x, target, index));
8672 break;
8673 case V2DImode:
8674 emit_insn (gen_neon_vset_lanev2di (target, x, target, index));
8675 break;
8676 default:
8677 gcc_unreachable ();
8678 }
814a4c3b
DJ
8679 return;
8680 }
8681
8682 /* Construct the vector in memory one field at a time
8683 and load the whole vector. */
88f77cba
JB
8684 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), 0);
8685 for (i = 0; i < n_elts; i++)
814a4c3b
DJ
8686 emit_move_insn (adjust_address_nv (mem, inner_mode,
8687 i * GET_MODE_SIZE (inner_mode)),
8688 XVECEXP (vals, 0, i));
88f77cba
JB
8689 emit_move_insn (target, mem);
8690}
8691
b617fc71
JB
8692/* Ensure OPERAND lies between LOW (inclusive) and HIGH (exclusive). Raise
8693 ERR if it doesn't. FIXME: NEON bounds checks occur late in compilation, so
8694 reported source locations are bogus. */
8695
8696static void
8697bounds_check (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high,
8698 const char *err)
8699{
8700 HOST_WIDE_INT lane;
8701
8702 gcc_assert (GET_CODE (operand) == CONST_INT);
8703
8704 lane = INTVAL (operand);
8705
8706 if (lane < low || lane >= high)
8707 error (err);
8708}
8709
8710/* Bounds-check lanes. */
8711
8712void
8713neon_lane_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
8714{
8715 bounds_check (operand, low, high, "lane out of range");
8716}
8717
8718/* Bounds-check constants. */
8719
8720void
8721neon_const_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
8722{
8723 bounds_check (operand, low, high, "constant out of range");
8724}
8725
8726HOST_WIDE_INT
8727neon_element_bits (enum machine_mode mode)
8728{
8729 if (mode == DImode)
8730 return GET_MODE_BITSIZE (mode);
8731 else
8732 return GET_MODE_BITSIZE (GET_MODE_INNER (mode));
8733}
8734
cce8749e
CH
8735\f
8736/* Predicates for `match_operand' and `match_operator'. */
8737
9b6b54e2 8738/* Return nonzero if OP is a valid Cirrus memory address pattern. */
9b6b54e2 8739int
e32bac5b 8740cirrus_memory_offset (rtx op)
9b6b54e2
NC
8741{
8742 /* Reject eliminable registers. */
8743 if (! (reload_in_progress || reload_completed)
8744 && ( reg_mentioned_p (frame_pointer_rtx, op)
8745 || reg_mentioned_p (arg_pointer_rtx, op)
8746 || reg_mentioned_p (virtual_incoming_args_rtx, op)
8747 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8748 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8749 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8750 return 0;
8751
8752 if (GET_CODE (op) == MEM)
8753 {
8754 rtx ind;
8755
8756 ind = XEXP (op, 0);
8757
8758 /* Match: (mem (reg)). */
8759 if (GET_CODE (ind) == REG)
8760 return 1;
8761
8762 /* Match:
8763 (mem (plus (reg)
8764 (const))). */
8765 if (GET_CODE (ind) == PLUS
8766 && GET_CODE (XEXP (ind, 0)) == REG
8767 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
8768 && GET_CODE (XEXP (ind, 1)) == CONST_INT)
8769 return 1;
8770 }
8771
8772 return 0;
8773}
8774
f26b8ec9 8775/* Return TRUE if OP is a valid coprocessor memory address pattern.
5b3e6663
PB
8776 WB is true if full writeback address modes are allowed and is false
8777 if limited writeback address modes (POST_INC and PRE_DEC) are
8778 allowed. */
9b66ebb1
PB
8779
8780int
fdd695fd 8781arm_coproc_mem_operand (rtx op, bool wb)
9b66ebb1 8782{
fdd695fd 8783 rtx ind;
9b66ebb1 8784
fdd695fd 8785 /* Reject eliminable registers. */
9b66ebb1
PB
8786 if (! (reload_in_progress || reload_completed)
8787 && ( reg_mentioned_p (frame_pointer_rtx, op)
8788 || reg_mentioned_p (arg_pointer_rtx, op)
8789 || reg_mentioned_p (virtual_incoming_args_rtx, op)
8790 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8791 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8792 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8793 return FALSE;
8794
59b9a953 8795 /* Constants are converted into offsets from labels. */
fdd695fd
PB
8796 if (GET_CODE (op) != MEM)
8797 return FALSE;
9b66ebb1 8798
fdd695fd 8799 ind = XEXP (op, 0);
9b66ebb1 8800
fdd695fd
PB
8801 if (reload_completed
8802 && (GET_CODE (ind) == LABEL_REF
8803 || (GET_CODE (ind) == CONST
8804 && GET_CODE (XEXP (ind, 0)) == PLUS
8805 && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
8806 && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
8807 return TRUE;
9b66ebb1 8808
fdd695fd
PB
8809 /* Match: (mem (reg)). */
8810 if (GET_CODE (ind) == REG)
8811 return arm_address_register_rtx_p (ind, 0);
8812
5b3e6663
PB
8813 /* Autoincremment addressing modes. POST_INC and PRE_DEC are
8814 acceptable in any case (subject to verification by
8815 arm_address_register_rtx_p). We need WB to be true to accept
8816 PRE_INC and POST_DEC. */
8817 if (GET_CODE (ind) == POST_INC
8818 || GET_CODE (ind) == PRE_DEC
8819 || (wb
8820 && (GET_CODE (ind) == PRE_INC
8821 || GET_CODE (ind) == POST_DEC)))
fdd695fd
PB
8822 return arm_address_register_rtx_p (XEXP (ind, 0), 0);
8823
8824 if (wb
8825 && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
8826 && arm_address_register_rtx_p (XEXP (ind, 0), 0)
8827 && GET_CODE (XEXP (ind, 1)) == PLUS
8828 && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
8829 ind = XEXP (ind, 1);
8830
8831 /* Match:
8832 (plus (reg)
8833 (const)). */
8834 if (GET_CODE (ind) == PLUS
8835 && GET_CODE (XEXP (ind, 0)) == REG
8836 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
8837 && GET_CODE (XEXP (ind, 1)) == CONST_INT
8838 && INTVAL (XEXP (ind, 1)) > -1024
8839 && INTVAL (XEXP (ind, 1)) < 1024
8840 && (INTVAL (XEXP (ind, 1)) & 3) == 0)
8841 return TRUE;
9b66ebb1
PB
8842
8843 return FALSE;
8844}
8845
88f77cba 8846/* Return TRUE if OP is a memory operand which we can load or store a vector
dc34db56
PB
8847 to/from. TYPE is one of the following values:
8848 0 - Vector load/stor (vldr)
8849 1 - Core registers (ldm)
8850 2 - Element/structure loads (vld1)
8851 */
88f77cba 8852int
dc34db56 8853neon_vector_mem_operand (rtx op, int type)
88f77cba
JB
8854{
8855 rtx ind;
8856
8857 /* Reject eliminable registers. */
8858 if (! (reload_in_progress || reload_completed)
8859 && ( reg_mentioned_p (frame_pointer_rtx, op)
8860 || reg_mentioned_p (arg_pointer_rtx, op)
8861 || reg_mentioned_p (virtual_incoming_args_rtx, op)
8862 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8863 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8864 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8865 return FALSE;
8866
8867 /* Constants are converted into offsets from labels. */
8868 if (GET_CODE (op) != MEM)
8869 return FALSE;
8870
8871 ind = XEXP (op, 0);
8872
8873 if (reload_completed
8874 && (GET_CODE (ind) == LABEL_REF
8875 || (GET_CODE (ind) == CONST
8876 && GET_CODE (XEXP (ind, 0)) == PLUS
8877 && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
8878 && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
8879 return TRUE;
8880
8881 /* Match: (mem (reg)). */
8882 if (GET_CODE (ind) == REG)
8883 return arm_address_register_rtx_p (ind, 0);
8884
8885 /* Allow post-increment with Neon registers. */
c452684d
JB
8886 if ((type != 1 && GET_CODE (ind) == POST_INC)
8887 || (type == 0 && GET_CODE (ind) == PRE_DEC))
88f77cba
JB
8888 return arm_address_register_rtx_p (XEXP (ind, 0), 0);
8889
dc34db56 8890 /* FIXME: vld1 allows register post-modify. */
88f77cba
JB
8891
8892 /* Match:
8893 (plus (reg)
8894 (const)). */
dc34db56 8895 if (type == 0
88f77cba
JB
8896 && GET_CODE (ind) == PLUS
8897 && GET_CODE (XEXP (ind, 0)) == REG
8898 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
8899 && GET_CODE (XEXP (ind, 1)) == CONST_INT
8900 && INTVAL (XEXP (ind, 1)) > -1024
8901 && INTVAL (XEXP (ind, 1)) < 1016
8902 && (INTVAL (XEXP (ind, 1)) & 3) == 0)
8903 return TRUE;
8904
8905 return FALSE;
8906}
8907
8908/* Return TRUE if OP is a mem suitable for loading/storing a Neon struct
8909 type. */
8910int
8911neon_struct_mem_operand (rtx op)
8912{
8913 rtx ind;
8914
8915 /* Reject eliminable registers. */
8916 if (! (reload_in_progress || reload_completed)
8917 && ( reg_mentioned_p (frame_pointer_rtx, op)
8918 || reg_mentioned_p (arg_pointer_rtx, op)
8919 || reg_mentioned_p (virtual_incoming_args_rtx, op)
8920 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8921 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8922 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8923 return FALSE;
8924
8925 /* Constants are converted into offsets from labels. */
8926 if (GET_CODE (op) != MEM)
8927 return FALSE;
8928
8929 ind = XEXP (op, 0);
8930
8931 if (reload_completed
8932 && (GET_CODE (ind) == LABEL_REF
8933 || (GET_CODE (ind) == CONST
8934 && GET_CODE (XEXP (ind, 0)) == PLUS
8935 && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
8936 && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
8937 return TRUE;
8938
8939 /* Match: (mem (reg)). */
8940 if (GET_CODE (ind) == REG)
8941 return arm_address_register_rtx_p (ind, 0);
8942
8943 return FALSE;
8944}
8945
6555b6bd
RE
8946/* Return true if X is a register that will be eliminated later on. */
8947int
8948arm_eliminable_register (rtx x)
8949{
8950 return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
8951 || REGNO (x) == ARG_POINTER_REGNUM
8952 || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
8953 && REGNO (x) <= LAST_VIRTUAL_REGISTER));
8954}
9b66ebb1 8955
9b66ebb1 8956/* Return GENERAL_REGS if a scratch register required to reload x to/from
fe2d934b 8957 coprocessor registers. Otherwise return NO_REGS. */
9b66ebb1
PB
8958
8959enum reg_class
fe2d934b 8960coproc_secondary_reload_class (enum machine_mode mode, rtx x, bool wb)
9b66ebb1 8961{
0fd8c3ad
SL
8962 if (mode == HFmode)
8963 {
e0dc3601
PB
8964 if (!TARGET_NEON_FP16)
8965 return GENERAL_REGS;
0fd8c3ad
SL
8966 if (s_register_operand (x, mode) || neon_vector_mem_operand (x, 2))
8967 return NO_REGS;
8968 return GENERAL_REGS;
8969 }
8970
88f77cba
JB
8971 if (TARGET_NEON
8972 && (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
8973 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
dc34db56 8974 && neon_vector_mem_operand (x, 0))
88f77cba
JB
8975 return NO_REGS;
8976
fe2d934b 8977 if (arm_coproc_mem_operand (x, wb) || s_register_operand (x, mode))
9b66ebb1
PB
8978 return NO_REGS;
8979
8980 return GENERAL_REGS;
8981}
8982
866af8a9
JB
8983/* Values which must be returned in the most-significant end of the return
8984 register. */
8985
8986static bool
586de218 8987arm_return_in_msb (const_tree valtype)
866af8a9
JB
8988{
8989 return (TARGET_AAPCS_BASED
8990 && BYTES_BIG_ENDIAN
8991 && (AGGREGATE_TYPE_P (valtype)
8992 || TREE_CODE (valtype) == COMPLEX_TYPE));
8993}
9b66ebb1 8994
f0375c66
NC
8995/* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
8996 Use by the Cirrus Maverick code which has to workaround
8997 a hardware bug triggered by such instructions. */
f0375c66 8998static bool
e32bac5b 8999arm_memory_load_p (rtx insn)
9b6b54e2
NC
9000{
9001 rtx body, lhs, rhs;;
9002
f0375c66
NC
9003 if (insn == NULL_RTX || GET_CODE (insn) != INSN)
9004 return false;
9b6b54e2
NC
9005
9006 body = PATTERN (insn);
9007
9008 if (GET_CODE (body) != SET)
f0375c66 9009 return false;
9b6b54e2
NC
9010
9011 lhs = XEXP (body, 0);
9012 rhs = XEXP (body, 1);
9013
f0375c66
NC
9014 lhs = REG_OR_SUBREG_RTX (lhs);
9015
9016 /* If the destination is not a general purpose
9017 register we do not have to worry. */
9018 if (GET_CODE (lhs) != REG
9019 || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
9020 return false;
9021
9022 /* As well as loads from memory we also have to react
9023 to loads of invalid constants which will be turned
9024 into loads from the minipool. */
9025 return (GET_CODE (rhs) == MEM
9026 || GET_CODE (rhs) == SYMBOL_REF
9027 || note_invalid_constants (insn, -1, false));
9b6b54e2
NC
9028}
9029
f0375c66 9030/* Return TRUE if INSN is a Cirrus instruction. */
f0375c66 9031static bool
e32bac5b 9032arm_cirrus_insn_p (rtx insn)
9b6b54e2
NC
9033{
9034 enum attr_cirrus attr;
9035
e6d29d15 9036 /* get_attr cannot accept USE or CLOBBER. */
9b6b54e2
NC
9037 if (!insn
9038 || GET_CODE (insn) != INSN
9039 || GET_CODE (PATTERN (insn)) == USE
9040 || GET_CODE (PATTERN (insn)) == CLOBBER)
9041 return 0;
9042
9043 attr = get_attr_cirrus (insn);
9044
f0375c66 9045 return attr != CIRRUS_NOT;
9b6b54e2
NC
9046}
9047
9048/* Cirrus reorg for invalid instruction combinations. */
9b6b54e2 9049static void
e32bac5b 9050cirrus_reorg (rtx first)
9b6b54e2
NC
9051{
9052 enum attr_cirrus attr;
9053 rtx body = PATTERN (first);
9054 rtx t;
9055 int nops;
9056
9057 /* Any branch must be followed by 2 non Cirrus instructions. */
9058 if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
9059 {
9060 nops = 0;
9061 t = next_nonnote_insn (first);
9062
f0375c66 9063 if (arm_cirrus_insn_p (t))
9b6b54e2
NC
9064 ++ nops;
9065
f0375c66 9066 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
9b6b54e2
NC
9067 ++ nops;
9068
9069 while (nops --)
9070 emit_insn_after (gen_nop (), first);
9071
9072 return;
9073 }
9074
9075 /* (float (blah)) is in parallel with a clobber. */
9076 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
9077 body = XVECEXP (body, 0, 0);
9078
9079 if (GET_CODE (body) == SET)
9080 {
9081 rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
9082
9083 /* cfldrd, cfldr64, cfstrd, cfstr64 must
9084 be followed by a non Cirrus insn. */
9085 if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
9086 {
f0375c66 9087 if (arm_cirrus_insn_p (next_nonnote_insn (first)))
9b6b54e2
NC
9088 emit_insn_after (gen_nop (), first);
9089
9090 return;
9091 }
f0375c66 9092 else if (arm_memory_load_p (first))
9b6b54e2
NC
9093 {
9094 unsigned int arm_regno;
9095
9096 /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
9097 ldr/cfmv64hr combination where the Rd field is the same
9098 in both instructions must be split with a non Cirrus
9099 insn. Example:
9100
9101 ldr r0, blah
9102 nop
9103 cfmvsr mvf0, r0. */
9104
9105 /* Get Arm register number for ldr insn. */
9106 if (GET_CODE (lhs) == REG)
9107 arm_regno = REGNO (lhs);
9b6b54e2 9108 else
e6d29d15
NS
9109 {
9110 gcc_assert (GET_CODE (rhs) == REG);
9111 arm_regno = REGNO (rhs);
9112 }
9b6b54e2
NC
9113
9114 /* Next insn. */
9115 first = next_nonnote_insn (first);
9116
f0375c66 9117 if (! arm_cirrus_insn_p (first))
9b6b54e2
NC
9118 return;
9119
9120 body = PATTERN (first);
9121
9122 /* (float (blah)) is in parallel with a clobber. */
9123 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
9124 body = XVECEXP (body, 0, 0);
9125
9126 if (GET_CODE (body) == FLOAT)
9127 body = XEXP (body, 0);
9128
9129 if (get_attr_cirrus (first) == CIRRUS_MOVE
9130 && GET_CODE (XEXP (body, 1)) == REG
9131 && arm_regno == REGNO (XEXP (body, 1)))
9132 emit_insn_after (gen_nop (), first);
9133
9134 return;
9135 }
9136 }
9137
e6d29d15 9138 /* get_attr cannot accept USE or CLOBBER. */
9b6b54e2
NC
9139 if (!first
9140 || GET_CODE (first) != INSN
9141 || GET_CODE (PATTERN (first)) == USE
9142 || GET_CODE (PATTERN (first)) == CLOBBER)
9143 return;
9144
9145 attr = get_attr_cirrus (first);
9146
9147 /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
9148 must be followed by a non-coprocessor instruction. */
9149 if (attr == CIRRUS_COMPARE)
9150 {
9151 nops = 0;
9152
9153 t = next_nonnote_insn (first);
9154
f0375c66 9155 if (arm_cirrus_insn_p (t))
9b6b54e2
NC
9156 ++ nops;
9157
f0375c66 9158 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
9b6b54e2
NC
9159 ++ nops;
9160
9161 while (nops --)
9162 emit_insn_after (gen_nop (), first);
9163
9164 return;
9165 }
9166}
9167
2b835d68
RE
9168/* Return TRUE if X references a SYMBOL_REF. */
9169int
e32bac5b 9170symbol_mentioned_p (rtx x)
2b835d68 9171{
1d6e90ac
NC
9172 const char * fmt;
9173 int i;
2b835d68
RE
9174
9175 if (GET_CODE (x) == SYMBOL_REF)
9176 return 1;
9177
d3585b76
DJ
9178 /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
9179 are constant offsets, not symbols. */
9180 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
9181 return 0;
9182
2b835d68 9183 fmt = GET_RTX_FORMAT (GET_CODE (x));
f676971a 9184
2b835d68
RE
9185 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
9186 {
9187 if (fmt[i] == 'E')
9188 {
1d6e90ac 9189 int j;
2b835d68
RE
9190
9191 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9192 if (symbol_mentioned_p (XVECEXP (x, i, j)))
9193 return 1;
9194 }
9195 else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
9196 return 1;
9197 }
9198
9199 return 0;
9200}
9201
9202/* Return TRUE if X references a LABEL_REF. */
9203int
e32bac5b 9204label_mentioned_p (rtx x)
2b835d68 9205{
1d6e90ac
NC
9206 const char * fmt;
9207 int i;
2b835d68
RE
9208
9209 if (GET_CODE (x) == LABEL_REF)
9210 return 1;
9211
d3585b76
DJ
9212 /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
9213 instruction, but they are constant offsets, not symbols. */
9214 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
9215 return 0;
9216
2b835d68
RE
9217 fmt = GET_RTX_FORMAT (GET_CODE (x));
9218 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
9219 {
9220 if (fmt[i] == 'E')
9221 {
1d6e90ac 9222 int j;
2b835d68
RE
9223
9224 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9225 if (label_mentioned_p (XVECEXP (x, i, j)))
9226 return 1;
9227 }
9228 else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
9229 return 1;
9230 }
9231
9232 return 0;
9233}
9234
d3585b76
DJ
9235int
9236tls_mentioned_p (rtx x)
9237{
9238 switch (GET_CODE (x))
9239 {
9240 case CONST:
9241 return tls_mentioned_p (XEXP (x, 0));
9242
9243 case UNSPEC:
9244 if (XINT (x, 1) == UNSPEC_TLS)
9245 return 1;
9246
9247 default:
9248 return 0;
9249 }
9250}
9251
2e5505a4
RE
9252/* Must not copy any rtx that uses a pc-relative address. */
9253
9254static int
9255arm_note_pic_base (rtx *x, void *date ATTRIBUTE_UNUSED)
9256{
9257 if (GET_CODE (*x) == UNSPEC
9258 && XINT (*x, 1) == UNSPEC_PIC_BASE)
9259 return 1;
9260 return 0;
9261}
d3585b76
DJ
9262
9263static bool
9264arm_cannot_copy_insn_p (rtx insn)
9265{
2e5505a4 9266 return for_each_rtx (&PATTERN (insn), arm_note_pic_base, NULL);
d3585b76
DJ
9267}
9268
ff9940b0 9269enum rtx_code
e32bac5b 9270minmax_code (rtx x)
ff9940b0
RE
9271{
9272 enum rtx_code code = GET_CODE (x);
9273
e6d29d15
NS
9274 switch (code)
9275 {
9276 case SMAX:
9277 return GE;
9278 case SMIN:
9279 return LE;
9280 case UMIN:
9281 return LEU;
9282 case UMAX:
9283 return GEU;
9284 default:
9285 gcc_unreachable ();
9286 }
ff9940b0
RE
9287}
9288
6354dc9b 9289/* Return 1 if memory locations are adjacent. */
f3bb6135 9290int
e32bac5b 9291adjacent_mem_locations (rtx a, rtx b)
ff9940b0 9292{
15b5c4c1
RE
9293 /* We don't guarantee to preserve the order of these memory refs. */
9294 if (volatile_refs_p (a) || volatile_refs_p (b))
9295 return 0;
9296
ff9940b0
RE
9297 if ((GET_CODE (XEXP (a, 0)) == REG
9298 || (GET_CODE (XEXP (a, 0)) == PLUS
9299 && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
9300 && (GET_CODE (XEXP (b, 0)) == REG
9301 || (GET_CODE (XEXP (b, 0)) == PLUS
9302 && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
9303 {
6555b6bd
RE
9304 HOST_WIDE_INT val0 = 0, val1 = 0;
9305 rtx reg0, reg1;
9306 int val_diff;
f676971a 9307
ff9940b0
RE
9308 if (GET_CODE (XEXP (a, 0)) == PLUS)
9309 {
6555b6bd 9310 reg0 = XEXP (XEXP (a, 0), 0);
ff9940b0
RE
9311 val0 = INTVAL (XEXP (XEXP (a, 0), 1));
9312 }
9313 else
6555b6bd 9314 reg0 = XEXP (a, 0);
1d6e90ac 9315
ff9940b0
RE
9316 if (GET_CODE (XEXP (b, 0)) == PLUS)
9317 {
6555b6bd 9318 reg1 = XEXP (XEXP (b, 0), 0);
ff9940b0
RE
9319 val1 = INTVAL (XEXP (XEXP (b, 0), 1));
9320 }
9321 else
6555b6bd 9322 reg1 = XEXP (b, 0);
1d6e90ac 9323
e32bac5b
RE
9324 /* Don't accept any offset that will require multiple
9325 instructions to handle, since this would cause the
9326 arith_adjacentmem pattern to output an overlong sequence. */
bbbbb16a 9327 if (!const_ok_for_op (val0, PLUS) || !const_ok_for_op (val1, PLUS))
c75a3ddc 9328 return 0;
f676971a 9329
6555b6bd
RE
9330 /* Don't allow an eliminable register: register elimination can make
9331 the offset too large. */
9332 if (arm_eliminable_register (reg0))
9333 return 0;
9334
9335 val_diff = val1 - val0;
15b5c4c1
RE
9336
9337 if (arm_ld_sched)
9338 {
9339 /* If the target has load delay slots, then there's no benefit
9340 to using an ldm instruction unless the offset is zero and
9341 we are optimizing for size. */
9342 return (optimize_size && (REGNO (reg0) == REGNO (reg1))
9343 && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
9344 && (val_diff == 4 || val_diff == -4));
9345 }
9346
6555b6bd
RE
9347 return ((REGNO (reg0) == REGNO (reg1))
9348 && (val_diff == 4 || val_diff == -4));
ff9940b0 9349 }
6555b6bd 9350
ff9940b0
RE
9351 return 0;
9352}
9353
93b338c3
BS
9354/* Return true iff it would be profitable to turn a sequence of NOPS loads
9355 or stores (depending on IS_STORE) into a load-multiple or store-multiple
9356 instruction. ADD_OFFSET is nonzero if the base address register needs
9357 to be modified with an add instruction before we can use it. */
9358
9359static bool
9360multiple_operation_profitable_p (bool is_store ATTRIBUTE_UNUSED,
9361 int nops, HOST_WIDE_INT add_offset)
9362 {
9363 /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
9364 if the offset isn't small enough. The reason 2 ldrs are faster
9365 is because these ARMs are able to do more than one cache access
9366 in a single cycle. The ARM9 and StrongARM have Harvard caches,
9367 whilst the ARM8 has a double bandwidth cache. This means that
9368 these cores can do both an instruction fetch and a data fetch in
9369 a single cycle, so the trick of calculating the address into a
9370 scratch register (one of the result regs) and then doing a load
9371 multiple actually becomes slower (and no smaller in code size).
9372 That is the transformation
9373
9374 ldr rd1, [rbase + offset]
9375 ldr rd2, [rbase + offset + 4]
9376
9377 to
9378
9379 add rd1, rbase, offset
9380 ldmia rd1, {rd1, rd2}
9381
9382 produces worse code -- '3 cycles + any stalls on rd2' instead of
9383 '2 cycles + any stalls on rd2'. On ARMs with only one cache
9384 access per cycle, the first sequence could never complete in less
9385 than 6 cycles, whereas the ldm sequence would only take 5 and
9386 would make better use of sequential accesses if not hitting the
9387 cache.
9388
9389 We cheat here and test 'arm_ld_sched' which we currently know to
9390 only be true for the ARM8, ARM9 and StrongARM. If this ever
9391 changes, then the test below needs to be reworked. */
9392 if (nops == 2 && arm_ld_sched && add_offset != 0)
9393 return false;
9394
8f4c6e28
BS
9395 /* XScale has load-store double instructions, but they have stricter
9396 alignment requirements than load-store multiple, so we cannot
9397 use them.
9398
9399 For XScale ldm requires 2 + NREGS cycles to complete and blocks
9400 the pipeline until completion.
9401
9402 NREGS CYCLES
9403 1 3
9404 2 4
9405 3 5
9406 4 6
9407
9408 An ldr instruction takes 1-3 cycles, but does not block the
9409 pipeline.
9410
9411 NREGS CYCLES
9412 1 1-3
9413 2 2-6
9414 3 3-9
9415 4 4-12
9416
9417 Best case ldr will always win. However, the more ldr instructions
9418 we issue, the less likely we are to be able to schedule them well.
9419 Using ldr instructions also increases code size.
9420
9421 As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
9422 for counts of 3 or 4 regs. */
9423 if (nops <= 2 && arm_tune_xscale && !optimize_size)
9424 return false;
93b338c3
BS
9425 return true;
9426}
9427
9428/* Subroutine of load_multiple_sequence and store_multiple_sequence.
9429 Given an array of UNSORTED_OFFSETS, of which there are NOPS, compute
9430 an array ORDER which describes the sequence to use when accessing the
9431 offsets that produces an ascending order. In this sequence, each
9432 offset must be larger by exactly 4 than the previous one. ORDER[0]
9433 must have been filled in with the lowest offset by the caller.
9434 If UNSORTED_REGS is nonnull, it is an array of register numbers that
9435 we use to verify that ORDER produces an ascending order of registers.
9436 Return true if it was possible to construct such an order, false if
9437 not. */
9438
9439static bool
9440compute_offset_order (int nops, HOST_WIDE_INT *unsorted_offsets, int *order,
9441 int *unsorted_regs)
9442{
9443 int i;
9444 for (i = 1; i < nops; i++)
9445 {
9446 int j;
9447
9448 order[i] = order[i - 1];
9449 for (j = 0; j < nops; j++)
9450 if (unsorted_offsets[j] == unsorted_offsets[order[i - 1]] + 4)
9451 {
9452 /* We must find exactly one offset that is higher than the
9453 previous one by 4. */
9454 if (order[i] != order[i - 1])
9455 return false;
9456 order[i] = j;
9457 }
9458 if (order[i] == order[i - 1])
9459 return false;
9460 /* The register numbers must be ascending. */
9461 if (unsorted_regs != NULL
9462 && unsorted_regs[order[i]] <= unsorted_regs[order[i - 1]])
9463 return false;
9464 }
9465 return true;
9466}
9467
37119410
BS
9468/* Used to determine in a peephole whether a sequence of load
9469 instructions can be changed into a load-multiple instruction.
9470 NOPS is the number of separate load instructions we are examining. The
9471 first NOPS entries in OPERANDS are the destination registers, the
9472 next NOPS entries are memory operands. If this function is
9473 successful, *BASE is set to the common base register of the memory
9474 accesses; *LOAD_OFFSET is set to the first memory location's offset
9475 from that base register.
9476 REGS is an array filled in with the destination register numbers.
9477 SAVED_ORDER (if nonnull), is an array filled in with an order that maps
9478 insn numbers to to an ascending order of stores. If CHECK_REGS is true,
9479 the sequence of registers in REGS matches the loads from ascending memory
9480 locations, and the function verifies that the register numbers are
9481 themselves ascending. If CHECK_REGS is false, the register numbers
9482 are stored in the order they are found in the operands. */
9483static int
9484load_multiple_sequence (rtx *operands, int nops, int *regs, int *saved_order,
9485 int *base, HOST_WIDE_INT *load_offset, bool check_regs)
84ed5e79 9486{
93b338c3
BS
9487 int unsorted_regs[MAX_LDM_STM_OPS];
9488 HOST_WIDE_INT unsorted_offsets[MAX_LDM_STM_OPS];
9489 int order[MAX_LDM_STM_OPS];
37119410 9490 rtx base_reg_rtx = NULL;
ad076f4e 9491 int base_reg = -1;
93b338c3 9492 int i, ldm_case;
84ed5e79 9493
93b338c3
BS
9494 /* Can only handle up to MAX_LDM_STM_OPS insns at present, though could be
9495 easily extended if required. */
9496 gcc_assert (nops >= 2 && nops <= MAX_LDM_STM_OPS);
84ed5e79 9497
93b338c3 9498 memset (order, 0, MAX_LDM_STM_OPS * sizeof (int));
f0b4bdd5 9499
84ed5e79 9500 /* Loop over the operands and check that the memory references are
112cdef5 9501 suitable (i.e. immediate offsets from the same base register). At
84ed5e79
RE
9502 the same time, extract the target register, and the memory
9503 offsets. */
9504 for (i = 0; i < nops; i++)
9505 {
9506 rtx reg;
9507 rtx offset;
9508
56636818
JL
9509 /* Convert a subreg of a mem into the mem itself. */
9510 if (GET_CODE (operands[nops + i]) == SUBREG)
4e26a7af 9511 operands[nops + i] = alter_subreg (operands + (nops + i));
56636818 9512
e6d29d15 9513 gcc_assert (GET_CODE (operands[nops + i]) == MEM);
84ed5e79
RE
9514
9515 /* Don't reorder volatile memory references; it doesn't seem worth
9516 looking for the case where the order is ok anyway. */
9517 if (MEM_VOLATILE_P (operands[nops + i]))
9518 return 0;
9519
9520 offset = const0_rtx;
9521
9522 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
9523 || (GET_CODE (reg) == SUBREG
9524 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9525 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
9526 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
9527 == REG)
9528 || (GET_CODE (reg) == SUBREG
9529 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9530 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
9531 == CONST_INT)))
9532 {
9533 if (i == 0)
84ed5e79 9534 {
37119410
BS
9535 base_reg = REGNO (reg);
9536 base_reg_rtx = reg;
9537 if (TARGET_THUMB1 && base_reg > LAST_LO_REGNUM)
84ed5e79 9538 return 0;
84ed5e79 9539 }
37119410
BS
9540 else if (base_reg != (int) REGNO (reg))
9541 /* Not addressed from the same base register. */
9542 return 0;
9543
93b338c3
BS
9544 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
9545 ? REGNO (operands[i])
9546 : REGNO (SUBREG_REG (operands[i])));
84ed5e79
RE
9547
9548 /* If it isn't an integer register, or if it overwrites the
9549 base register but isn't the last insn in the list, then
9550 we can't do this. */
37119410
BS
9551 if (unsorted_regs[i] < 0
9552 || (TARGET_THUMB1 && unsorted_regs[i] > LAST_LO_REGNUM)
9553 || unsorted_regs[i] > 14
84ed5e79
RE
9554 || (i != nops - 1 && unsorted_regs[i] == base_reg))
9555 return 0;
9556
9557 unsorted_offsets[i] = INTVAL (offset);
93b338c3
BS
9558 if (i == 0 || unsorted_offsets[i] < unsorted_offsets[order[0]])
9559 order[0] = i;
84ed5e79
RE
9560 }
9561 else
9562 /* Not a suitable memory address. */
9563 return 0;
9564 }
9565
9566 /* All the useful information has now been extracted from the
9567 operands into unsorted_regs and unsorted_offsets; additionally,
93b338c3
BS
9568 order[0] has been set to the lowest offset in the list. Sort
9569 the offsets into order, verifying that they are adjacent, and
9570 check that the register numbers are ascending. */
37119410
BS
9571 if (!compute_offset_order (nops, unsorted_offsets, order,
9572 check_regs ? unsorted_regs : NULL))
93b338c3 9573 return 0;
84ed5e79 9574
37119410
BS
9575 if (saved_order)
9576 memcpy (saved_order, order, sizeof order);
9577
84ed5e79
RE
9578 if (base)
9579 {
9580 *base = base_reg;
9581
9582 for (i = 0; i < nops; i++)
37119410 9583 regs[i] = unsorted_regs[check_regs ? order[i] : i];
84ed5e79
RE
9584
9585 *load_offset = unsorted_offsets[order[0]];
9586 }
9587
37119410
BS
9588 if (TARGET_THUMB1
9589 && !peep2_reg_dead_p (nops, base_reg_rtx))
9590 return 0;
9591
84ed5e79 9592 if (unsorted_offsets[order[0]] == 0)
93b338c3
BS
9593 ldm_case = 1; /* ldmia */
9594 else if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
9595 ldm_case = 2; /* ldmib */
9596 else if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
9597 ldm_case = 3; /* ldmda */
37119410 9598 else if (TARGET_32BIT && unsorted_offsets[order[nops - 1]] == -4)
93b338c3
BS
9599 ldm_case = 4; /* ldmdb */
9600 else if (const_ok_for_arm (unsorted_offsets[order[0]])
9601 || const_ok_for_arm (-unsorted_offsets[order[0]]))
9602 ldm_case = 5;
9603 else
9604 return 0;
949d79eb 9605
93b338c3
BS
9606 if (!multiple_operation_profitable_p (false, nops,
9607 ldm_case == 5
9608 ? unsorted_offsets[order[0]] : 0))
b36ba79f
RE
9609 return 0;
9610
93b338c3 9611 return ldm_case;
84ed5e79
RE
9612}
9613
37119410
BS
9614/* Used to determine in a peephole whether a sequence of store instructions can
9615 be changed into a store-multiple instruction.
9616 NOPS is the number of separate store instructions we are examining.
9617 NOPS_TOTAL is the total number of instructions recognized by the peephole
9618 pattern.
9619 The first NOPS entries in OPERANDS are the source registers, the next
9620 NOPS entries are memory operands. If this function is successful, *BASE is
9621 set to the common base register of the memory accesses; *LOAD_OFFSET is set
9622 to the first memory location's offset from that base register. REGS is an
9623 array filled in with the source register numbers, REG_RTXS (if nonnull) is
9624 likewise filled with the corresponding rtx's.
9625 SAVED_ORDER (if nonnull), is an array filled in with an order that maps insn
9626 numbers to to an ascending order of stores.
9627 If CHECK_REGS is true, the sequence of registers in *REGS matches the stores
9628 from ascending memory locations, and the function verifies that the register
9629 numbers are themselves ascending. If CHECK_REGS is false, the register
9630 numbers are stored in the order they are found in the operands. */
9631static int
9632store_multiple_sequence (rtx *operands, int nops, int nops_total,
9633 int *regs, rtx *reg_rtxs, int *saved_order, int *base,
9634 HOST_WIDE_INT *load_offset, bool check_regs)
84ed5e79 9635{
93b338c3 9636 int unsorted_regs[MAX_LDM_STM_OPS];
37119410 9637 rtx unsorted_reg_rtxs[MAX_LDM_STM_OPS];
93b338c3
BS
9638 HOST_WIDE_INT unsorted_offsets[MAX_LDM_STM_OPS];
9639 int order[MAX_LDM_STM_OPS];
ad076f4e 9640 int base_reg = -1;
37119410 9641 rtx base_reg_rtx = NULL;
93b338c3 9642 int i, stm_case;
84ed5e79 9643
93b338c3
BS
9644 /* Can only handle up to MAX_LDM_STM_OPS insns at present, though could be
9645 easily extended if required. */
9646 gcc_assert (nops >= 2 && nops <= MAX_LDM_STM_OPS);
84ed5e79 9647
93b338c3 9648 memset (order, 0, MAX_LDM_STM_OPS * sizeof (int));
f0b4bdd5 9649
84ed5e79 9650 /* Loop over the operands and check that the memory references are
112cdef5 9651 suitable (i.e. immediate offsets from the same base register). At
84ed5e79
RE
9652 the same time, extract the target register, and the memory
9653 offsets. */
9654 for (i = 0; i < nops; i++)
9655 {
9656 rtx reg;
9657 rtx offset;
9658
56636818
JL
9659 /* Convert a subreg of a mem into the mem itself. */
9660 if (GET_CODE (operands[nops + i]) == SUBREG)
4e26a7af 9661 operands[nops + i] = alter_subreg (operands + (nops + i));
56636818 9662
e6d29d15 9663 gcc_assert (GET_CODE (operands[nops + i]) == MEM);
84ed5e79
RE
9664
9665 /* Don't reorder volatile memory references; it doesn't seem worth
9666 looking for the case where the order is ok anyway. */
9667 if (MEM_VOLATILE_P (operands[nops + i]))
9668 return 0;
9669
9670 offset = const0_rtx;
9671
9672 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
9673 || (GET_CODE (reg) == SUBREG
9674 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9675 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
9676 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
9677 == REG)
9678 || (GET_CODE (reg) == SUBREG
9679 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9680 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
9681 == CONST_INT)))
9682 {
37119410
BS
9683 unsorted_reg_rtxs[i] = (GET_CODE (operands[i]) == REG
9684 ? operands[i] : SUBREG_REG (operands[i]));
9685 unsorted_regs[i] = REGNO (unsorted_reg_rtxs[i]);
9686
84ed5e79 9687 if (i == 0)
37119410
BS
9688 {
9689 base_reg = REGNO (reg);
9690 base_reg_rtx = reg;
9691 if (TARGET_THUMB1 && base_reg > LAST_LO_REGNUM)
9692 return 0;
9693 }
93b338c3
BS
9694 else if (base_reg != (int) REGNO (reg))
9695 /* Not addressed from the same base register. */
9696 return 0;
84ed5e79
RE
9697
9698 /* If it isn't an integer register, then we can't do this. */
37119410
BS
9699 if (unsorted_regs[i] < 0
9700 || (TARGET_THUMB1 && unsorted_regs[i] > LAST_LO_REGNUM)
9701 || (TARGET_THUMB2 && unsorted_regs[i] == base_reg)
9702 || (TARGET_THUMB2 && unsorted_regs[i] == SP_REGNUM)
9703 || unsorted_regs[i] > 14)
84ed5e79
RE
9704 return 0;
9705
9706 unsorted_offsets[i] = INTVAL (offset);
93b338c3
BS
9707 if (i == 0 || unsorted_offsets[i] < unsorted_offsets[order[0]])
9708 order[0] = i;
84ed5e79
RE
9709 }
9710 else
9711 /* Not a suitable memory address. */
9712 return 0;
9713 }
9714
9715 /* All the useful information has now been extracted from the
9716 operands into unsorted_regs and unsorted_offsets; additionally,
93b338c3
BS
9717 order[0] has been set to the lowest offset in the list. Sort
9718 the offsets into order, verifying that they are adjacent, and
9719 check that the register numbers are ascending. */
37119410
BS
9720 if (!compute_offset_order (nops, unsorted_offsets, order,
9721 check_regs ? unsorted_regs : NULL))
93b338c3 9722 return 0;
84ed5e79 9723
37119410
BS
9724 if (saved_order)
9725 memcpy (saved_order, order, sizeof order);
9726
84ed5e79
RE
9727 if (base)
9728 {
9729 *base = base_reg;
9730
9731 for (i = 0; i < nops; i++)
37119410
BS
9732 {
9733 regs[i] = unsorted_regs[check_regs ? order[i] : i];
9734 if (reg_rtxs)
9735 reg_rtxs[i] = unsorted_reg_rtxs[check_regs ? order[i] : i];
9736 }
84ed5e79
RE
9737
9738 *load_offset = unsorted_offsets[order[0]];
9739 }
9740
37119410
BS
9741 if (TARGET_THUMB1
9742 && !peep2_reg_dead_p (nops_total, base_reg_rtx))
9743 return 0;
9744
84ed5e79 9745 if (unsorted_offsets[order[0]] == 0)
93b338c3
BS
9746 stm_case = 1; /* stmia */
9747 else if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
9748 stm_case = 2; /* stmib */
9749 else if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
9750 stm_case = 3; /* stmda */
37119410 9751 else if (TARGET_32BIT && unsorted_offsets[order[nops - 1]] == -4)
93b338c3
BS
9752 stm_case = 4; /* stmdb */
9753 else
9754 return 0;
84ed5e79 9755
93b338c3
BS
9756 if (!multiple_operation_profitable_p (false, nops, 0))
9757 return 0;
84ed5e79 9758
93b338c3 9759 return stm_case;
84ed5e79 9760}
ff9940b0 9761\f
6354dc9b 9762/* Routines for use in generating RTL. */
1d6e90ac 9763
37119410
BS
9764/* Generate a load-multiple instruction. COUNT is the number of loads in
9765 the instruction; REGS and MEMS are arrays containing the operands.
9766 BASEREG is the base register to be used in addressing the memory operands.
9767 WBACK_OFFSET is nonzero if the instruction should update the base
9768 register. */
9769
9770static rtx
9771arm_gen_load_multiple_1 (int count, int *regs, rtx *mems, rtx basereg,
9772 HOST_WIDE_INT wback_offset)
ff9940b0
RE
9773{
9774 int i = 0, j;
9775 rtx result;
ff9940b0 9776
8f4c6e28 9777 if (!multiple_operation_profitable_p (false, count, 0))
d19fb8e3
NC
9778 {
9779 rtx seq;
f676971a 9780
d19fb8e3 9781 start_sequence ();
f676971a 9782
d19fb8e3 9783 for (i = 0; i < count; i++)
37119410 9784 emit_move_insn (gen_rtx_REG (SImode, regs[i]), mems[i]);
d19fb8e3 9785
37119410
BS
9786 if (wback_offset != 0)
9787 emit_move_insn (basereg, plus_constant (basereg, wback_offset));
d19fb8e3 9788
2f937369 9789 seq = get_insns ();
d19fb8e3 9790 end_sequence ();
f676971a 9791
d19fb8e3
NC
9792 return seq;
9793 }
9794
43cffd11 9795 result = gen_rtx_PARALLEL (VOIDmode,
37119410
BS
9796 rtvec_alloc (count + (wback_offset != 0 ? 1 : 0)));
9797 if (wback_offset != 0)
f3bb6135 9798 {
ff9940b0 9799 XVECEXP (result, 0, 0)
37119410
BS
9800 = gen_rtx_SET (VOIDmode, basereg,
9801 plus_constant (basereg, wback_offset));
ff9940b0
RE
9802 i = 1;
9803 count++;
f3bb6135
RE
9804 }
9805
ff9940b0 9806 for (j = 0; i < count; i++, j++)
37119410
BS
9807 XVECEXP (result, 0, i)
9808 = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, regs[j]), mems[j]);
50ed9cea 9809
ff9940b0
RE
9810 return result;
9811}
9812
37119410
BS
9813/* Generate a store-multiple instruction. COUNT is the number of stores in
9814 the instruction; REGS and MEMS are arrays containing the operands.
9815 BASEREG is the base register to be used in addressing the memory operands.
9816 WBACK_OFFSET is nonzero if the instruction should update the base
9817 register. */
9818
9819static rtx
9820arm_gen_store_multiple_1 (int count, int *regs, rtx *mems, rtx basereg,
9821 HOST_WIDE_INT wback_offset)
ff9940b0
RE
9822{
9823 int i = 0, j;
9824 rtx result;
ff9940b0 9825
37119410
BS
9826 if (GET_CODE (basereg) == PLUS)
9827 basereg = XEXP (basereg, 0);
9828
8f4c6e28 9829 if (!multiple_operation_profitable_p (false, count, 0))
d19fb8e3
NC
9830 {
9831 rtx seq;
f676971a 9832
d19fb8e3 9833 start_sequence ();
f676971a 9834
d19fb8e3 9835 for (i = 0; i < count; i++)
37119410 9836 emit_move_insn (mems[i], gen_rtx_REG (SImode, regs[i]));
d19fb8e3 9837
37119410
BS
9838 if (wback_offset != 0)
9839 emit_move_insn (basereg, plus_constant (basereg, wback_offset));
d19fb8e3 9840
2f937369 9841 seq = get_insns ();
d19fb8e3 9842 end_sequence ();
f676971a 9843
d19fb8e3
NC
9844 return seq;
9845 }
9846
43cffd11 9847 result = gen_rtx_PARALLEL (VOIDmode,
37119410
BS
9848 rtvec_alloc (count + (wback_offset != 0 ? 1 : 0)));
9849 if (wback_offset != 0)
f3bb6135 9850 {
ff9940b0 9851 XVECEXP (result, 0, 0)
37119410
BS
9852 = gen_rtx_SET (VOIDmode, basereg,
9853 plus_constant (basereg, wback_offset));
ff9940b0
RE
9854 i = 1;
9855 count++;
f3bb6135
RE
9856 }
9857
ff9940b0 9858 for (j = 0; i < count; i++, j++)
37119410
BS
9859 XVECEXP (result, 0, i)
9860 = gen_rtx_SET (VOIDmode, mems[j], gen_rtx_REG (SImode, regs[j]));
9861
9862 return result;
9863}
9864
9865/* Generate either a load-multiple or a store-multiple instruction. This
9866 function can be used in situations where we can start with a single MEM
9867 rtx and adjust its address upwards.
9868 COUNT is the number of operations in the instruction, not counting a
9869 possible update of the base register. REGS is an array containing the
9870 register operands.
9871 BASEREG is the base register to be used in addressing the memory operands,
9872 which are constructed from BASEMEM.
9873 WRITE_BACK specifies whether the generated instruction should include an
9874 update of the base register.
9875 OFFSETP is used to pass an offset to and from this function; this offset
9876 is not used when constructing the address (instead BASEMEM should have an
9877 appropriate offset in its address), it is used only for setting
9878 MEM_OFFSET. It is updated only if WRITE_BACK is true.*/
9879
9880static rtx
9881arm_gen_multiple_op (bool is_load, int *regs, int count, rtx basereg,
9882 bool write_back, rtx basemem, HOST_WIDE_INT *offsetp)
9883{
9884 rtx mems[MAX_LDM_STM_OPS];
9885 HOST_WIDE_INT offset = *offsetp;
9886 int i;
9887
9888 gcc_assert (count <= MAX_LDM_STM_OPS);
9889
9890 if (GET_CODE (basereg) == PLUS)
9891 basereg = XEXP (basereg, 0);
9892
9893 for (i = 0; i < count; i++)
f3bb6135 9894 {
37119410
BS
9895 rtx addr = plus_constant (basereg, i * 4);
9896 mems[i] = adjust_automodify_address_nv (basemem, SImode, addr, offset);
9897 offset += 4;
f3bb6135
RE
9898 }
9899
50ed9cea
RH
9900 if (write_back)
9901 *offsetp = offset;
9902
37119410
BS
9903 if (is_load)
9904 return arm_gen_load_multiple_1 (count, regs, mems, basereg,
9905 write_back ? 4 * count : 0);
9906 else
9907 return arm_gen_store_multiple_1 (count, regs, mems, basereg,
9908 write_back ? 4 * count : 0);
9909}
9910
9911rtx
9912arm_gen_load_multiple (int *regs, int count, rtx basereg, int write_back,
9913 rtx basemem, HOST_WIDE_INT *offsetp)
9914{
9915 return arm_gen_multiple_op (TRUE, regs, count, basereg, write_back, basemem,
9916 offsetp);
9917}
9918
9919rtx
9920arm_gen_store_multiple (int *regs, int count, rtx basereg, int write_back,
9921 rtx basemem, HOST_WIDE_INT *offsetp)
9922{
9923 return arm_gen_multiple_op (FALSE, regs, count, basereg, write_back, basemem,
9924 offsetp);
9925}
9926
9927/* Called from a peephole2 expander to turn a sequence of loads into an
9928 LDM instruction. OPERANDS are the operands found by the peephole matcher;
9929 NOPS indicates how many separate loads we are trying to combine. SORT_REGS
9930 is true if we can reorder the registers because they are used commutatively
9931 subsequently.
9932 Returns true iff we could generate a new instruction. */
9933
9934bool
9935gen_ldm_seq (rtx *operands, int nops, bool sort_regs)
9936{
9937 int regs[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
9938 rtx mems[MAX_LDM_STM_OPS];
9939 int i, j, base_reg;
9940 rtx base_reg_rtx;
9941 HOST_WIDE_INT offset;
9942 int write_back = FALSE;
9943 int ldm_case;
9944 rtx addr;
9945
9946 ldm_case = load_multiple_sequence (operands, nops, regs, mem_order,
9947 &base_reg, &offset, !sort_regs);
9948
9949 if (ldm_case == 0)
9950 return false;
9951
9952 if (sort_regs)
9953 for (i = 0; i < nops - 1; i++)
9954 for (j = i + 1; j < nops; j++)
9955 if (regs[i] > regs[j])
9956 {
9957 int t = regs[i];
9958 regs[i] = regs[j];
9959 regs[j] = t;
9960 }
9961 base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
9962
9963 if (TARGET_THUMB1)
9964 {
9965 gcc_assert (peep2_reg_dead_p (nops, base_reg_rtx));
9966 gcc_assert (ldm_case == 1 || ldm_case == 5);
9967 write_back = TRUE;
9968 }
9969
9970 if (ldm_case == 5)
9971 {
9972 rtx newbase = TARGET_THUMB1 ? base_reg_rtx : gen_rtx_REG (SImode, regs[0]);
9973 emit_insn (gen_addsi3 (newbase, base_reg_rtx, GEN_INT (offset)));
9974 offset = 0;
9975 if (!TARGET_THUMB1)
9976 {
9977 base_reg = regs[0];
9978 base_reg_rtx = newbase;
9979 }
9980 }
9981
9982 for (i = 0; i < nops; i++)
9983 {
9984 addr = plus_constant (base_reg_rtx, offset + i * 4);
9985 mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
9986 SImode, addr, 0);
9987 }
9988 emit_insn (arm_gen_load_multiple_1 (nops, regs, mems, base_reg_rtx,
9989 write_back ? offset + i * 4 : 0));
9990 return true;
9991}
9992
9993/* Called from a peephole2 expander to turn a sequence of stores into an
9994 STM instruction. OPERANDS are the operands found by the peephole matcher;
9995 NOPS indicates how many separate stores we are trying to combine.
9996 Returns true iff we could generate a new instruction. */
9997
9998bool
9999gen_stm_seq (rtx *operands, int nops)
10000{
10001 int i;
10002 int regs[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
10003 rtx mems[MAX_LDM_STM_OPS];
10004 int base_reg;
10005 rtx base_reg_rtx;
10006 HOST_WIDE_INT offset;
10007 int write_back = FALSE;
10008 int stm_case;
10009 rtx addr;
10010 bool base_reg_dies;
10011
10012 stm_case = store_multiple_sequence (operands, nops, nops, regs, NULL,
10013 mem_order, &base_reg, &offset, true);
10014
10015 if (stm_case == 0)
10016 return false;
10017
10018 base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
10019
10020 base_reg_dies = peep2_reg_dead_p (nops, base_reg_rtx);
10021 if (TARGET_THUMB1)
10022 {
10023 gcc_assert (base_reg_dies);
10024 write_back = TRUE;
10025 }
10026
10027 if (stm_case == 5)
10028 {
10029 gcc_assert (base_reg_dies);
10030 emit_insn (gen_addsi3 (base_reg_rtx, base_reg_rtx, GEN_INT (offset)));
10031 offset = 0;
10032 }
10033
10034 addr = plus_constant (base_reg_rtx, offset);
10035
10036 for (i = 0; i < nops; i++)
10037 {
10038 addr = plus_constant (base_reg_rtx, offset + i * 4);
10039 mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
10040 SImode, addr, 0);
10041 }
10042 emit_insn (arm_gen_store_multiple_1 (nops, regs, mems, base_reg_rtx,
10043 write_back ? offset + i * 4 : 0));
10044 return true;
10045}
10046
10047/* Called from a peephole2 expander to turn a sequence of stores that are
10048 preceded by constant loads into an STM instruction. OPERANDS are the
10049 operands found by the peephole matcher; NOPS indicates how many
10050 separate stores we are trying to combine; there are 2 * NOPS
10051 instructions in the peephole.
10052 Returns true iff we could generate a new instruction. */
10053
10054bool
10055gen_const_stm_seq (rtx *operands, int nops)
10056{
10057 int regs[MAX_LDM_STM_OPS], sorted_regs[MAX_LDM_STM_OPS];
10058 int reg_order[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
10059 rtx reg_rtxs[MAX_LDM_STM_OPS], orig_reg_rtxs[MAX_LDM_STM_OPS];
10060 rtx mems[MAX_LDM_STM_OPS];
10061 int base_reg;
10062 rtx base_reg_rtx;
10063 HOST_WIDE_INT offset;
10064 int write_back = FALSE;
10065 int stm_case;
10066 rtx addr;
10067 bool base_reg_dies;
10068 int i, j;
10069 HARD_REG_SET allocated;
10070
10071 stm_case = store_multiple_sequence (operands, nops, 2 * nops, regs, reg_rtxs,
10072 mem_order, &base_reg, &offset, false);
10073
10074 if (stm_case == 0)
10075 return false;
10076
10077 memcpy (orig_reg_rtxs, reg_rtxs, sizeof orig_reg_rtxs);
10078
10079 /* If the same register is used more than once, try to find a free
10080 register. */
10081 CLEAR_HARD_REG_SET (allocated);
10082 for (i = 0; i < nops; i++)
10083 {
10084 for (j = i + 1; j < nops; j++)
10085 if (regs[i] == regs[j])
10086 {
10087 rtx t = peep2_find_free_register (0, nops * 2,
10088 TARGET_THUMB1 ? "l" : "r",
10089 SImode, &allocated);
10090 if (t == NULL_RTX)
10091 return false;
10092 reg_rtxs[i] = t;
10093 regs[i] = REGNO (t);
10094 }
10095 }
10096
10097 /* Compute an ordering that maps the register numbers to an ascending
10098 sequence. */
10099 reg_order[0] = 0;
10100 for (i = 0; i < nops; i++)
10101 if (regs[i] < regs[reg_order[0]])
10102 reg_order[0] = i;
10103
10104 for (i = 1; i < nops; i++)
10105 {
10106 int this_order = reg_order[i - 1];
10107 for (j = 0; j < nops; j++)
10108 if (regs[j] > regs[reg_order[i - 1]]
10109 && (this_order == reg_order[i - 1]
10110 || regs[j] < regs[this_order]))
10111 this_order = j;
10112 reg_order[i] = this_order;
10113 }
10114
10115 /* Ensure that registers that must be live after the instruction end
10116 up with the correct value. */
10117 for (i = 0; i < nops; i++)
10118 {
10119 int this_order = reg_order[i];
10120 if ((this_order != mem_order[i]
10121 || orig_reg_rtxs[this_order] != reg_rtxs[this_order])
10122 && !peep2_reg_dead_p (nops * 2, orig_reg_rtxs[this_order]))
10123 return false;
10124 }
10125
10126 /* Load the constants. */
10127 for (i = 0; i < nops; i++)
10128 {
10129 rtx op = operands[2 * nops + mem_order[i]];
10130 sorted_regs[i] = regs[reg_order[i]];
10131 emit_move_insn (reg_rtxs[reg_order[i]], op);
10132 }
10133
10134 base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
10135
10136 base_reg_dies = peep2_reg_dead_p (nops * 2, base_reg_rtx);
10137 if (TARGET_THUMB1)
10138 {
10139 gcc_assert (base_reg_dies);
10140 write_back = TRUE;
10141 }
10142
10143 if (stm_case == 5)
10144 {
10145 gcc_assert (base_reg_dies);
10146 emit_insn (gen_addsi3 (base_reg_rtx, base_reg_rtx, GEN_INT (offset)));
10147 offset = 0;
10148 }
10149
10150 addr = plus_constant (base_reg_rtx, offset);
10151
10152 for (i = 0; i < nops; i++)
10153 {
10154 addr = plus_constant (base_reg_rtx, offset + i * 4);
10155 mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
10156 SImode, addr, 0);
10157 }
10158 emit_insn (arm_gen_store_multiple_1 (nops, sorted_regs, mems, base_reg_rtx,
10159 write_back ? offset + i * 4 : 0));
10160 return true;
ff9940b0
RE
10161}
10162
880e2516 10163int
70128ad9 10164arm_gen_movmemqi (rtx *operands)
880e2516
RE
10165{
10166 HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
50ed9cea 10167 HOST_WIDE_INT srcoffset, dstoffset;
ad076f4e 10168 int i;
50ed9cea 10169 rtx src, dst, srcbase, dstbase;
880e2516 10170 rtx part_bytes_reg = NULL;
56636818 10171 rtx mem;
880e2516
RE
10172
10173 if (GET_CODE (operands[2]) != CONST_INT
10174 || GET_CODE (operands[3]) != CONST_INT
10175 || INTVAL (operands[2]) > 64
10176 || INTVAL (operands[3]) & 3)
10177 return 0;
10178
50ed9cea
RH
10179 dstbase = operands[0];
10180 srcbase = operands[1];
56636818 10181
50ed9cea
RH
10182 dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
10183 src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
880e2516 10184
e9d7b180 10185 in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
880e2516
RE
10186 out_words_to_go = INTVAL (operands[2]) / 4;
10187 last_bytes = INTVAL (operands[2]) & 3;
50ed9cea 10188 dstoffset = srcoffset = 0;
880e2516
RE
10189
10190 if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
43cffd11 10191 part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
880e2516
RE
10192
10193 for (i = 0; in_words_to_go >= 2; i+=4)
10194 {
bd9c7e23 10195 if (in_words_to_go > 4)
37119410
BS
10196 emit_insn (arm_gen_load_multiple (arm_regs_in_sequence, 4, src,
10197 TRUE, srcbase, &srcoffset));
bd9c7e23 10198 else
37119410
BS
10199 emit_insn (arm_gen_load_multiple (arm_regs_in_sequence, in_words_to_go,
10200 src, FALSE, srcbase,
10201 &srcoffset));
bd9c7e23 10202
880e2516
RE
10203 if (out_words_to_go)
10204 {
bd9c7e23 10205 if (out_words_to_go > 4)
37119410
BS
10206 emit_insn (arm_gen_store_multiple (arm_regs_in_sequence, 4, dst,
10207 TRUE, dstbase, &dstoffset));
bd9c7e23 10208 else if (out_words_to_go != 1)
37119410
BS
10209 emit_insn (arm_gen_store_multiple (arm_regs_in_sequence,
10210 out_words_to_go, dst,
bd9c7e23 10211 (last_bytes == 0
56636818 10212 ? FALSE : TRUE),
50ed9cea 10213 dstbase, &dstoffset));
880e2516
RE
10214 else
10215 {
50ed9cea 10216 mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
43cffd11 10217 emit_move_insn (mem, gen_rtx_REG (SImode, 0));
bd9c7e23 10218 if (last_bytes != 0)
50ed9cea
RH
10219 {
10220 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
10221 dstoffset += 4;
10222 }
880e2516
RE
10223 }
10224 }
10225
10226 in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
10227 out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
10228 }
10229
10230 /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do. */
10231 if (out_words_to_go)
62b10bbc
NC
10232 {
10233 rtx sreg;
f676971a 10234
50ed9cea
RH
10235 mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
10236 sreg = copy_to_reg (mem);
10237
10238 mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
62b10bbc 10239 emit_move_insn (mem, sreg);
62b10bbc 10240 in_words_to_go--;
f676971a 10241
e6d29d15 10242 gcc_assert (!in_words_to_go); /* Sanity check */
62b10bbc 10243 }
880e2516
RE
10244
10245 if (in_words_to_go)
10246 {
e6d29d15 10247 gcc_assert (in_words_to_go > 0);
880e2516 10248
50ed9cea 10249 mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
56636818 10250 part_bytes_reg = copy_to_mode_reg (SImode, mem);
880e2516
RE
10251 }
10252
e6d29d15 10253 gcc_assert (!last_bytes || part_bytes_reg);
d5b7b3ae 10254
880e2516
RE
10255 if (BYTES_BIG_ENDIAN && last_bytes)
10256 {
10257 rtx tmp = gen_reg_rtx (SImode);
10258
6354dc9b 10259 /* The bytes we want are in the top end of the word. */
bee06f3d
RE
10260 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
10261 GEN_INT (8 * (4 - last_bytes))));
880e2516 10262 part_bytes_reg = tmp;
f676971a 10263
880e2516
RE
10264 while (last_bytes)
10265 {
50ed9cea
RH
10266 mem = adjust_automodify_address (dstbase, QImode,
10267 plus_constant (dst, last_bytes - 1),
10268 dstoffset + last_bytes - 1);
5d5603e2
BS
10269 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
10270
880e2516
RE
10271 if (--last_bytes)
10272 {
10273 tmp = gen_reg_rtx (SImode);
10274 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
10275 part_bytes_reg = tmp;
10276 }
10277 }
f676971a 10278
880e2516
RE
10279 }
10280 else
10281 {
d5b7b3ae 10282 if (last_bytes > 1)
880e2516 10283 {
50ed9cea 10284 mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
5d5603e2 10285 emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
d5b7b3ae
RE
10286 last_bytes -= 2;
10287 if (last_bytes)
880e2516
RE
10288 {
10289 rtx tmp = gen_reg_rtx (SImode);
a556fd39 10290 emit_insn (gen_addsi3 (dst, dst, const2_rtx));
d5b7b3ae 10291 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
880e2516 10292 part_bytes_reg = tmp;
50ed9cea 10293 dstoffset += 2;
880e2516
RE
10294 }
10295 }
f676971a 10296
d5b7b3ae
RE
10297 if (last_bytes)
10298 {
50ed9cea 10299 mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
5d5603e2 10300 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
d5b7b3ae 10301 }
880e2516
RE
10302 }
10303
10304 return 1;
10305}
10306
03f1640c
RE
10307/* Select a dominance comparison mode if possible for a test of the general
10308 form (OP (COND_OR (X) (Y)) (const_int 0)). We support three forms.
f676971a 10309 COND_OR == DOM_CC_X_AND_Y => (X && Y)
03f1640c 10310 COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
f676971a 10311 COND_OR == DOM_CC_X_OR_Y => (X || Y)
03f1640c 10312 In all cases OP will be either EQ or NE, but we don't need to know which
f676971a 10313 here. If we are unable to support a dominance comparison we return
03f1640c
RE
10314 CC mode. This will then fail to match for the RTL expressions that
10315 generate this call. */
03f1640c 10316enum machine_mode
e32bac5b 10317arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
84ed5e79
RE
10318{
10319 enum rtx_code cond1, cond2;
10320 int swapped = 0;
10321
10322 /* Currently we will probably get the wrong result if the individual
10323 comparisons are not simple. This also ensures that it is safe to
956d6950 10324 reverse a comparison if necessary. */
84ed5e79
RE
10325 if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
10326 != CCmode)
10327 || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
10328 != CCmode))
10329 return CCmode;
10330
1646cf41
RE
10331 /* The if_then_else variant of this tests the second condition if the
10332 first passes, but is true if the first fails. Reverse the first
10333 condition to get a true "inclusive-or" expression. */
03f1640c 10334 if (cond_or == DOM_CC_NX_OR_Y)
84ed5e79
RE
10335 cond1 = reverse_condition (cond1);
10336
10337 /* If the comparisons are not equal, and one doesn't dominate the other,
10338 then we can't do this. */
f676971a 10339 if (cond1 != cond2
5895f793
RE
10340 && !comparison_dominates_p (cond1, cond2)
10341 && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
84ed5e79
RE
10342 return CCmode;
10343
10344 if (swapped)
10345 {
10346 enum rtx_code temp = cond1;
10347 cond1 = cond2;
10348 cond2 = temp;
10349 }
10350
10351 switch (cond1)
10352 {
10353 case EQ:
e6d29d15 10354 if (cond_or == DOM_CC_X_AND_Y)
84ed5e79
RE
10355 return CC_DEQmode;
10356
10357 switch (cond2)
10358 {
e6d29d15 10359 case EQ: return CC_DEQmode;
84ed5e79
RE
10360 case LE: return CC_DLEmode;
10361 case LEU: return CC_DLEUmode;
10362 case GE: return CC_DGEmode;
10363 case GEU: return CC_DGEUmode;
e6d29d15 10364 default: gcc_unreachable ();
84ed5e79
RE
10365 }
10366
84ed5e79 10367 case LT:
e6d29d15 10368 if (cond_or == DOM_CC_X_AND_Y)
84ed5e79 10369 return CC_DLTmode;
e0b92319 10370
e6d29d15
NS
10371 switch (cond2)
10372 {
10373 case LT:
10374 return CC_DLTmode;
10375 case LE:
10376 return CC_DLEmode;
10377 case NE:
10378 return CC_DNEmode;
10379 default:
10380 gcc_unreachable ();
10381 }
84ed5e79
RE
10382
10383 case GT:
e6d29d15 10384 if (cond_or == DOM_CC_X_AND_Y)
84ed5e79 10385 return CC_DGTmode;
e6d29d15
NS
10386
10387 switch (cond2)
10388 {
10389 case GT:
10390 return CC_DGTmode;
10391 case GE:
10392 return CC_DGEmode;
10393 case NE:
10394 return CC_DNEmode;
10395 default:
10396 gcc_unreachable ();
10397 }
f676971a 10398
84ed5e79 10399 case LTU:
e6d29d15 10400 if (cond_or == DOM_CC_X_AND_Y)
84ed5e79 10401 return CC_DLTUmode;
e6d29d15
NS
10402
10403 switch (cond2)
10404 {
10405 case LTU:
10406 return CC_DLTUmode;
10407 case LEU:
10408 return CC_DLEUmode;
10409 case NE:
10410 return CC_DNEmode;
10411 default:
10412 gcc_unreachable ();
10413 }
84ed5e79
RE
10414
10415 case GTU:
e6d29d15 10416 if (cond_or == DOM_CC_X_AND_Y)
84ed5e79 10417 return CC_DGTUmode;
e0b92319 10418
e6d29d15
NS
10419 switch (cond2)
10420 {
10421 case GTU:
10422 return CC_DGTUmode;
10423 case GEU:
10424 return CC_DGEUmode;
10425 case NE:
10426 return CC_DNEmode;
10427 default:
10428 gcc_unreachable ();
10429 }
84ed5e79
RE
10430
10431 /* The remaining cases only occur when both comparisons are the
10432 same. */
10433 case NE:
e6d29d15 10434 gcc_assert (cond1 == cond2);
84ed5e79
RE
10435 return CC_DNEmode;
10436
10437 case LE:
e6d29d15 10438 gcc_assert (cond1 == cond2);
84ed5e79
RE
10439 return CC_DLEmode;
10440
10441 case GE:
e6d29d15 10442 gcc_assert (cond1 == cond2);
84ed5e79
RE
10443 return CC_DGEmode;
10444
10445 case LEU:
e6d29d15 10446 gcc_assert (cond1 == cond2);
84ed5e79
RE
10447 return CC_DLEUmode;
10448
10449 case GEU:
e6d29d15 10450 gcc_assert (cond1 == cond2);
84ed5e79 10451 return CC_DGEUmode;
ad076f4e
RE
10452
10453 default:
e6d29d15 10454 gcc_unreachable ();
84ed5e79 10455 }
84ed5e79
RE
10456}
10457
10458enum machine_mode
e32bac5b 10459arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
84ed5e79
RE
10460{
10461 /* All floating point compares return CCFP if it is an equality
10462 comparison, and CCFPE otherwise. */
10463 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
e45b72c4
RE
10464 {
10465 switch (op)
10466 {
10467 case EQ:
10468 case NE:
10469 case UNORDERED:
10470 case ORDERED:
10471 case UNLT:
10472 case UNLE:
10473 case UNGT:
10474 case UNGE:
10475 case UNEQ:
10476 case LTGT:
10477 return CCFPmode;
10478
10479 case LT:
10480 case LE:
10481 case GT:
10482 case GE:
9b66ebb1 10483 if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
9b6b54e2 10484 return CCFPmode;
e45b72c4
RE
10485 return CCFPEmode;
10486
10487 default:
e6d29d15 10488 gcc_unreachable ();
e45b72c4
RE
10489 }
10490 }
f676971a 10491
84ed5e79
RE
10492 /* A compare with a shifted operand. Because of canonicalization, the
10493 comparison will have to be swapped when we emit the assembler. */
3e2d9dcf
RR
10494 if (GET_MODE (y) == SImode
10495 && (REG_P (y) || (GET_CODE (y) == SUBREG))
84ed5e79
RE
10496 && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
10497 || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
10498 || GET_CODE (x) == ROTATERT))
10499 return CC_SWPmode;
10500
04d8b819
RE
10501 /* This operation is performed swapped, but since we only rely on the Z
10502 flag we don't need an additional mode. */
3e2d9dcf
RR
10503 if (GET_MODE (y) == SImode
10504 && (REG_P (y) || (GET_CODE (y) == SUBREG))
04d8b819
RE
10505 && GET_CODE (x) == NEG
10506 && (op == EQ || op == NE))
10507 return CC_Zmode;
10508
f676971a 10509 /* This is a special case that is used by combine to allow a
956d6950 10510 comparison of a shifted byte load to be split into a zero-extend
84ed5e79 10511 followed by a comparison of the shifted integer (only valid for
956d6950 10512 equalities and unsigned inequalities). */
84ed5e79
RE
10513 if (GET_MODE (x) == SImode
10514 && GET_CODE (x) == ASHIFT
10515 && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
10516 && GET_CODE (XEXP (x, 0)) == SUBREG
10517 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
10518 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
10519 && (op == EQ || op == NE
10520 || op == GEU || op == GTU || op == LTU || op == LEU)
10521 && GET_CODE (y) == CONST_INT)
10522 return CC_Zmode;
10523
1646cf41
RE
10524 /* A construct for a conditional compare, if the false arm contains
10525 0, then both conditions must be true, otherwise either condition
10526 must be true. Not all conditions are possible, so CCmode is
10527 returned if it can't be done. */
10528 if (GET_CODE (x) == IF_THEN_ELSE
10529 && (XEXP (x, 2) == const0_rtx
10530 || XEXP (x, 2) == const1_rtx)
ec8e098d
PB
10531 && COMPARISON_P (XEXP (x, 0))
10532 && COMPARISON_P (XEXP (x, 1)))
f676971a 10533 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
03f1640c 10534 INTVAL (XEXP (x, 2)));
1646cf41
RE
10535
10536 /* Alternate canonicalizations of the above. These are somewhat cleaner. */
10537 if (GET_CODE (x) == AND
ec8e098d
PB
10538 && COMPARISON_P (XEXP (x, 0))
10539 && COMPARISON_P (XEXP (x, 1)))
03f1640c
RE
10540 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
10541 DOM_CC_X_AND_Y);
1646cf41
RE
10542
10543 if (GET_CODE (x) == IOR
ec8e098d
PB
10544 && COMPARISON_P (XEXP (x, 0))
10545 && COMPARISON_P (XEXP (x, 1)))
03f1640c
RE
10546 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
10547 DOM_CC_X_OR_Y);
1646cf41 10548
defc0463
RE
10549 /* An operation (on Thumb) where we want to test for a single bit.
10550 This is done by shifting that bit up into the top bit of a
10551 scratch register; we can then branch on the sign bit. */
5b3e6663 10552 if (TARGET_THUMB1
defc0463
RE
10553 && GET_MODE (x) == SImode
10554 && (op == EQ || op == NE)
f9fa4363
RE
10555 && GET_CODE (x) == ZERO_EXTRACT
10556 && XEXP (x, 1) == const1_rtx)
defc0463
RE
10557 return CC_Nmode;
10558
84ed5e79
RE
10559 /* An operation that sets the condition codes as a side-effect, the
10560 V flag is not set correctly, so we can only use comparisons where
10561 this doesn't matter. (For LT and GE we can use "mi" and "pl"
defc0463 10562 instead.) */
5b3e6663 10563 /* ??? Does the ZERO_EXTRACT case really apply to thumb2? */
84ed5e79
RE
10564 if (GET_MODE (x) == SImode
10565 && y == const0_rtx
10566 && (op == EQ || op == NE || op == LT || op == GE)
10567 && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
10568 || GET_CODE (x) == AND || GET_CODE (x) == IOR
10569 || GET_CODE (x) == XOR || GET_CODE (x) == MULT
10570 || GET_CODE (x) == NOT || GET_CODE (x) == NEG
10571 || GET_CODE (x) == LSHIFTRT
10572 || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
defc0463 10573 || GET_CODE (x) == ROTATERT
5b3e6663 10574 || (TARGET_32BIT && GET_CODE (x) == ZERO_EXTRACT)))
84ed5e79
RE
10575 return CC_NOOVmode;
10576
84ed5e79
RE
10577 if (GET_MODE (x) == QImode && (op == EQ || op == NE))
10578 return CC_Zmode;
10579
bd9c7e23
RE
10580 if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
10581 && GET_CODE (x) == PLUS
10582 && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
10583 return CC_Cmode;
10584
73160ba9
DJ
10585 if (GET_MODE (x) == DImode || GET_MODE (y) == DImode)
10586 {
10587 /* To keep things simple, always use the Cirrus cfcmp64 if it is
10588 available. */
10589 if (TARGET_ARM && TARGET_HARD_FLOAT && TARGET_MAVERICK)
10590 return CCmode;
10591
10592 switch (op)
10593 {
10594 case EQ:
10595 case NE:
10596 /* A DImode comparison against zero can be implemented by
10597 or'ing the two halves together. */
10598 if (y == const0_rtx)
10599 return CC_Zmode;
10600
10601 /* We can do an equality test in three Thumb instructions. */
10602 if (!TARGET_ARM)
10603 return CC_Zmode;
10604
10605 /* FALLTHROUGH */
10606
10607 case LTU:
10608 case LEU:
10609 case GTU:
10610 case GEU:
10611 /* DImode unsigned comparisons can be implemented by cmp +
10612 cmpeq without a scratch register. Not worth doing in
10613 Thumb-2. */
10614 if (TARGET_ARM)
10615 return CC_CZmode;
10616
10617 /* FALLTHROUGH */
10618
10619 case LT:
10620 case LE:
10621 case GT:
10622 case GE:
10623 /* DImode signed and unsigned comparisons can be implemented
10624 by cmp + sbcs with a scratch register, but that does not
10625 set the Z flag - we must reverse GT/LE/GTU/LEU. */
10626 gcc_assert (op != EQ && op != NE);
10627 return CC_NCVmode;
10628
10629 default:
10630 gcc_unreachable ();
10631 }
10632 }
10633
84ed5e79
RE
10634 return CCmode;
10635}
10636
ff9940b0
RE
10637/* X and Y are two things to compare using CODE. Emit the compare insn and
10638 return the rtx for register 0 in the proper mode. FP means this is a
10639 floating point compare: I don't think that it is needed on the arm. */
ff9940b0 10640rtx
e32bac5b 10641arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
ff9940b0 10642{
73160ba9
DJ
10643 enum machine_mode mode;
10644 rtx cc_reg;
10645 int dimode_comparison = GET_MODE (x) == DImode || GET_MODE (y) == DImode;
ff9940b0 10646
73160ba9
DJ
10647 /* We might have X as a constant, Y as a register because of the predicates
10648 used for cmpdi. If so, force X to a register here. */
10649 if (dimode_comparison && !REG_P (x))
10650 x = force_reg (DImode, x);
10651
10652 mode = SELECT_CC_MODE (code, x, y);
10653 cc_reg = gen_rtx_REG (mode, CC_REGNUM);
10654
10655 if (dimode_comparison
10656 && !(TARGET_HARD_FLOAT && TARGET_MAVERICK)
10657 && mode != CC_CZmode)
10658 {
10659 rtx clobber, set;
10660
10661 /* To compare two non-zero values for equality, XOR them and
10662 then compare against zero. Not used for ARM mode; there
10663 CC_CZmode is cheaper. */
10664 if (mode == CC_Zmode && y != const0_rtx)
10665 {
10666 x = expand_binop (DImode, xor_optab, x, y, NULL_RTX, 0, OPTAB_WIDEN);
10667 y = const0_rtx;
10668 }
10669 /* A scratch register is required. */
10670 clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (SImode));
10671 set = gen_rtx_SET (VOIDmode, cc_reg, gen_rtx_COMPARE (mode, x, y));
10672 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
10673 }
10674 else
10675 emit_set_insn (cc_reg, gen_rtx_COMPARE (mode, x, y));
ff9940b0
RE
10676
10677 return cc_reg;
10678}
10679
fcd53748
JT
10680/* Generate a sequence of insns that will generate the correct return
10681 address mask depending on the physical architecture that the program
10682 is running on. */
fcd53748 10683rtx
e32bac5b 10684arm_gen_return_addr_mask (void)
fcd53748
JT
10685{
10686 rtx reg = gen_reg_rtx (Pmode);
10687
10688 emit_insn (gen_return_addr_mask (reg));
10689 return reg;
10690}
10691
0a81f500 10692void
e32bac5b 10693arm_reload_in_hi (rtx *operands)
0a81f500 10694{
f9cc092a
RE
10695 rtx ref = operands[1];
10696 rtx base, scratch;
10697 HOST_WIDE_INT offset = 0;
10698
10699 if (GET_CODE (ref) == SUBREG)
10700 {
ddef6bc7 10701 offset = SUBREG_BYTE (ref);
f9cc092a
RE
10702 ref = SUBREG_REG (ref);
10703 }
10704
10705 if (GET_CODE (ref) == REG)
10706 {
10707 /* We have a pseudo which has been spilt onto the stack; there
10708 are two cases here: the first where there is a simple
10709 stack-slot replacement and a second where the stack-slot is
10710 out of range, or is used as a subreg. */
10711 if (reg_equiv_mem[REGNO (ref)])
10712 {
10713 ref = reg_equiv_mem[REGNO (ref)];
10714 base = find_replacement (&XEXP (ref, 0));
10715 }
10716 else
6354dc9b 10717 /* The slot is out of range, or was dressed up in a SUBREG. */
f9cc092a
RE
10718 base = reg_equiv_address[REGNO (ref)];
10719 }
10720 else
10721 base = find_replacement (&XEXP (ref, 0));
0a81f500 10722
e5e809f4
JL
10723 /* Handle the case where the address is too complex to be offset by 1. */
10724 if (GET_CODE (base) == MINUS
10725 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
10726 {
f9cc092a 10727 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
e5e809f4 10728
d66437c5 10729 emit_set_insn (base_plus, base);
e5e809f4
JL
10730 base = base_plus;
10731 }
f9cc092a
RE
10732 else if (GET_CODE (base) == PLUS)
10733 {
6354dc9b 10734 /* The addend must be CONST_INT, or we would have dealt with it above. */
f9cc092a
RE
10735 HOST_WIDE_INT hi, lo;
10736
10737 offset += INTVAL (XEXP (base, 1));
10738 base = XEXP (base, 0);
10739
6354dc9b 10740 /* Rework the address into a legal sequence of insns. */
f9cc092a
RE
10741 /* Valid range for lo is -4095 -> 4095 */
10742 lo = (offset >= 0
10743 ? (offset & 0xfff)
10744 : -((-offset) & 0xfff));
10745
10746 /* Corner case, if lo is the max offset then we would be out of range
10747 once we have added the additional 1 below, so bump the msb into the
10748 pre-loading insn(s). */
10749 if (lo == 4095)
10750 lo &= 0x7ff;
10751
30cf4896
KG
10752 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
10753 ^ (HOST_WIDE_INT) 0x80000000)
10754 - (HOST_WIDE_INT) 0x80000000);
f9cc092a 10755
e6d29d15 10756 gcc_assert (hi + lo == offset);
f9cc092a
RE
10757
10758 if (hi != 0)
10759 {
10760 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10761
10762 /* Get the base address; addsi3 knows how to handle constants
6354dc9b 10763 that require more than one insn. */
f9cc092a
RE
10764 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
10765 base = base_plus;
10766 offset = lo;
10767 }
10768 }
e5e809f4 10769
3a1944a6
RE
10770 /* Operands[2] may overlap operands[0] (though it won't overlap
10771 operands[1]), that's why we asked for a DImode reg -- so we can
10772 use the bit that does not overlap. */
10773 if (REGNO (operands[2]) == REGNO (operands[0]))
10774 scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10775 else
10776 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
10777
f9cc092a
RE
10778 emit_insn (gen_zero_extendqisi2 (scratch,
10779 gen_rtx_MEM (QImode,
10780 plus_constant (base,
10781 offset))));
43cffd11 10782 emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
f676971a 10783 gen_rtx_MEM (QImode,
f9cc092a
RE
10784 plus_constant (base,
10785 offset + 1))));
5895f793 10786 if (!BYTES_BIG_ENDIAN)
d66437c5
RE
10787 emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
10788 gen_rtx_IOR (SImode,
10789 gen_rtx_ASHIFT
10790 (SImode,
10791 gen_rtx_SUBREG (SImode, operands[0], 0),
10792 GEN_INT (8)),
10793 scratch));
0a81f500 10794 else
d66437c5
RE
10795 emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
10796 gen_rtx_IOR (SImode,
10797 gen_rtx_ASHIFT (SImode, scratch,
10798 GEN_INT (8)),
10799 gen_rtx_SUBREG (SImode, operands[0], 0)));
0a81f500
RE
10800}
10801
72ac76be 10802/* Handle storing a half-word to memory during reload by synthesizing as two
f9cc092a
RE
10803 byte stores. Take care not to clobber the input values until after we
10804 have moved them somewhere safe. This code assumes that if the DImode
10805 scratch in operands[2] overlaps either the input value or output address
10806 in some way, then that value must die in this insn (we absolutely need
10807 two scratch registers for some corner cases). */
f3bb6135 10808void
e32bac5b 10809arm_reload_out_hi (rtx *operands)
af48348a 10810{
f9cc092a
RE
10811 rtx ref = operands[0];
10812 rtx outval = operands[1];
10813 rtx base, scratch;
10814 HOST_WIDE_INT offset = 0;
10815
10816 if (GET_CODE (ref) == SUBREG)
10817 {
ddef6bc7 10818 offset = SUBREG_BYTE (ref);
f9cc092a
RE
10819 ref = SUBREG_REG (ref);
10820 }
10821
f9cc092a
RE
10822 if (GET_CODE (ref) == REG)
10823 {
10824 /* We have a pseudo which has been spilt onto the stack; there
10825 are two cases here: the first where there is a simple
10826 stack-slot replacement and a second where the stack-slot is
10827 out of range, or is used as a subreg. */
10828 if (reg_equiv_mem[REGNO (ref)])
10829 {
10830 ref = reg_equiv_mem[REGNO (ref)];
10831 base = find_replacement (&XEXP (ref, 0));
10832 }
10833 else
6354dc9b 10834 /* The slot is out of range, or was dressed up in a SUBREG. */
f9cc092a
RE
10835 base = reg_equiv_address[REGNO (ref)];
10836 }
10837 else
10838 base = find_replacement (&XEXP (ref, 0));
10839
10840 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
10841
10842 /* Handle the case where the address is too complex to be offset by 1. */
10843 if (GET_CODE (base) == MINUS
10844 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
10845 {
10846 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10847
10848 /* Be careful not to destroy OUTVAL. */
10849 if (reg_overlap_mentioned_p (base_plus, outval))
10850 {
10851 /* Updating base_plus might destroy outval, see if we can
10852 swap the scratch and base_plus. */
5895f793 10853 if (!reg_overlap_mentioned_p (scratch, outval))
f9cc092a
RE
10854 {
10855 rtx tmp = scratch;
10856 scratch = base_plus;
10857 base_plus = tmp;
10858 }
10859 else
10860 {
10861 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
10862
10863 /* Be conservative and copy OUTVAL into the scratch now,
10864 this should only be necessary if outval is a subreg
10865 of something larger than a word. */
10866 /* XXX Might this clobber base? I can't see how it can,
10867 since scratch is known to overlap with OUTVAL, and
10868 must be wider than a word. */
10869 emit_insn (gen_movhi (scratch_hi, outval));
10870 outval = scratch_hi;
10871 }
10872 }
10873
d66437c5 10874 emit_set_insn (base_plus, base);
f9cc092a
RE
10875 base = base_plus;
10876 }
10877 else if (GET_CODE (base) == PLUS)
10878 {
6354dc9b 10879 /* The addend must be CONST_INT, or we would have dealt with it above. */
f9cc092a
RE
10880 HOST_WIDE_INT hi, lo;
10881
10882 offset += INTVAL (XEXP (base, 1));
10883 base = XEXP (base, 0);
10884
6354dc9b 10885 /* Rework the address into a legal sequence of insns. */
f9cc092a
RE
10886 /* Valid range for lo is -4095 -> 4095 */
10887 lo = (offset >= 0
10888 ? (offset & 0xfff)
10889 : -((-offset) & 0xfff));
10890
10891 /* Corner case, if lo is the max offset then we would be out of range
10892 once we have added the additional 1 below, so bump the msb into the
10893 pre-loading insn(s). */
10894 if (lo == 4095)
10895 lo &= 0x7ff;
10896
30cf4896
KG
10897 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
10898 ^ (HOST_WIDE_INT) 0x80000000)
10899 - (HOST_WIDE_INT) 0x80000000);
f9cc092a 10900
e6d29d15 10901 gcc_assert (hi + lo == offset);
f9cc092a
RE
10902
10903 if (hi != 0)
10904 {
10905 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10906
10907 /* Be careful not to destroy OUTVAL. */
10908 if (reg_overlap_mentioned_p (base_plus, outval))
10909 {
10910 /* Updating base_plus might destroy outval, see if we
10911 can swap the scratch and base_plus. */
5895f793 10912 if (!reg_overlap_mentioned_p (scratch, outval))
f9cc092a
RE
10913 {
10914 rtx tmp = scratch;
10915 scratch = base_plus;
10916 base_plus = tmp;
10917 }
10918 else
10919 {
10920 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
10921
10922 /* Be conservative and copy outval into scratch now,
10923 this should only be necessary if outval is a
10924 subreg of something larger than a word. */
10925 /* XXX Might this clobber base? I can't see how it
10926 can, since scratch is known to overlap with
10927 outval. */
10928 emit_insn (gen_movhi (scratch_hi, outval));
10929 outval = scratch_hi;
10930 }
10931 }
10932
10933 /* Get the base address; addsi3 knows how to handle constants
6354dc9b 10934 that require more than one insn. */
f9cc092a
RE
10935 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
10936 base = base_plus;
10937 offset = lo;
10938 }
10939 }
af48348a 10940
b5cc037f
RE
10941 if (BYTES_BIG_ENDIAN)
10942 {
f676971a 10943 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
f9cc092a 10944 plus_constant (base, offset + 1)),
5d5603e2 10945 gen_lowpart (QImode, outval)));
f9cc092a
RE
10946 emit_insn (gen_lshrsi3 (scratch,
10947 gen_rtx_SUBREG (SImode, outval, 0),
b5cc037f 10948 GEN_INT (8)));
f9cc092a 10949 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
5d5603e2 10950 gen_lowpart (QImode, scratch)));
b5cc037f
RE
10951 }
10952 else
10953 {
f9cc092a 10954 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
5d5603e2 10955 gen_lowpart (QImode, outval)));
f9cc092a
RE
10956 emit_insn (gen_lshrsi3 (scratch,
10957 gen_rtx_SUBREG (SImode, outval, 0),
b5cc037f 10958 GEN_INT (8)));
f9cc092a
RE
10959 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
10960 plus_constant (base, offset + 1)),
5d5603e2 10961 gen_lowpart (QImode, scratch)));
b5cc037f 10962 }
af48348a 10963}
866af8a9
JB
10964
10965/* Return true if a type must be passed in memory. For AAPCS, small aggregates
10966 (padded to the size of a word) should be passed in a register. */
10967
10968static bool
586de218 10969arm_must_pass_in_stack (enum machine_mode mode, const_tree type)
866af8a9
JB
10970{
10971 if (TARGET_AAPCS_BASED)
10972 return must_pass_in_stack_var_size (mode, type);
10973 else
10974 return must_pass_in_stack_var_size_or_pad (mode, type);
10975}
10976
10977
10978/* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
10979 Return true if an argument passed on the stack should be padded upwards,
5a29b385
PB
10980 i.e. if the least-significant byte has useful data.
10981 For legacy APCS ABIs we use the default. For AAPCS based ABIs small
10982 aggregate types are placed in the lowest memory address. */
866af8a9
JB
10983
10984bool
586de218 10985arm_pad_arg_upward (enum machine_mode mode, const_tree type)
866af8a9
JB
10986{
10987 if (!TARGET_AAPCS_BASED)
5a29b385 10988 return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
866af8a9
JB
10989
10990 if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
10991 return false;
10992
10993 return true;
10994}
10995
10996
10997/* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
10998 For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
10999 byte of the register has useful data, and return the opposite if the
11000 most significant byte does.
11001 For AAPCS, small aggregates and small complex types are always padded
11002 upwards. */
11003
11004bool
11005arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
11006 tree type, int first ATTRIBUTE_UNUSED)
11007{
11008 if (TARGET_AAPCS_BASED
11009 && BYTES_BIG_ENDIAN
11010 && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11011 && int_size_in_bytes (type) <= 4)
11012 return true;
11013
11014 /* Otherwise, use default padding. */
11015 return !BYTES_BIG_ENDIAN;
11016}
11017
2b835d68 11018\f
d5b7b3ae
RE
11019/* Print a symbolic form of X to the debug file, F. */
11020static void
e32bac5b 11021arm_print_value (FILE *f, rtx x)
d5b7b3ae
RE
11022{
11023 switch (GET_CODE (x))
11024 {
11025 case CONST_INT:
11026 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
11027 return;
11028
11029 case CONST_DOUBLE:
11030 fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
11031 return;
11032
5a9335ef
NC
11033 case CONST_VECTOR:
11034 {
11035 int i;
11036
11037 fprintf (f, "<");
11038 for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
11039 {
11040 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
11041 if (i < (CONST_VECTOR_NUNITS (x) - 1))
11042 fputc (',', f);
11043 }
11044 fprintf (f, ">");
11045 }
11046 return;
11047
d5b7b3ae
RE
11048 case CONST_STRING:
11049 fprintf (f, "\"%s\"", XSTR (x, 0));
11050 return;
11051
11052 case SYMBOL_REF:
11053 fprintf (f, "`%s'", XSTR (x, 0));
11054 return;
11055
11056 case LABEL_REF:
11057 fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
11058 return;
11059
11060 case CONST:
11061 arm_print_value (f, XEXP (x, 0));
11062 return;
11063
11064 case PLUS:
11065 arm_print_value (f, XEXP (x, 0));
11066 fprintf (f, "+");
11067 arm_print_value (f, XEXP (x, 1));
11068 return;
11069
11070 case PC:
11071 fprintf (f, "pc");
11072 return;
11073
11074 default:
11075 fprintf (f, "????");
11076 return;
11077 }
11078}
11079\f
2b835d68 11080/* Routines for manipulation of the constant pool. */
2b835d68 11081
949d79eb
RE
11082/* Arm instructions cannot load a large constant directly into a
11083 register; they have to come from a pc relative load. The constant
11084 must therefore be placed in the addressable range of the pc
11085 relative load. Depending on the precise pc relative load
11086 instruction the range is somewhere between 256 bytes and 4k. This
11087 means that we often have to dump a constant inside a function, and
2b835d68
RE
11088 generate code to branch around it.
11089
949d79eb
RE
11090 It is important to minimize this, since the branches will slow
11091 things down and make the code larger.
2b835d68 11092
949d79eb
RE
11093 Normally we can hide the table after an existing unconditional
11094 branch so that there is no interruption of the flow, but in the
11095 worst case the code looks like this:
2b835d68
RE
11096
11097 ldr rn, L1
949d79eb 11098 ...
2b835d68
RE
11099 b L2
11100 align
11101 L1: .long value
11102 L2:
949d79eb 11103 ...
2b835d68 11104
2b835d68 11105 ldr rn, L3
949d79eb 11106 ...
2b835d68
RE
11107 b L4
11108 align
2b835d68
RE
11109 L3: .long value
11110 L4:
949d79eb
RE
11111 ...
11112
11113 We fix this by performing a scan after scheduling, which notices
11114 which instructions need to have their operands fetched from the
11115 constant table and builds the table.
11116
11117 The algorithm starts by building a table of all the constants that
11118 need fixing up and all the natural barriers in the function (places
11119 where a constant table can be dropped without breaking the flow).
11120 For each fixup we note how far the pc-relative replacement will be
11121 able to reach and the offset of the instruction into the function.
11122
11123 Having built the table we then group the fixes together to form
11124 tables that are as large as possible (subject to addressing
11125 constraints) and emit each table of constants after the last
11126 barrier that is within range of all the instructions in the group.
11127 If a group does not contain a barrier, then we forcibly create one
11128 by inserting a jump instruction into the flow. Once the table has
11129 been inserted, the insns are then modified to reference the
11130 relevant entry in the pool.
11131
6354dc9b 11132 Possible enhancements to the algorithm (not implemented) are:
949d79eb 11133
d5b7b3ae 11134 1) For some processors and object formats, there may be benefit in
949d79eb
RE
11135 aligning the pools to the start of cache lines; this alignment
11136 would need to be taken into account when calculating addressability
6354dc9b 11137 of a pool. */
2b835d68 11138
d5b7b3ae
RE
11139/* These typedefs are located at the start of this file, so that
11140 they can be used in the prototypes there. This comment is to
11141 remind readers of that fact so that the following structures
11142 can be understood more easily.
11143
11144 typedef struct minipool_node Mnode;
11145 typedef struct minipool_fixup Mfix; */
11146
11147struct minipool_node
11148{
11149 /* Doubly linked chain of entries. */
11150 Mnode * next;
11151 Mnode * prev;
11152 /* The maximum offset into the code that this entry can be placed. While
11153 pushing fixes for forward references, all entries are sorted in order
11154 of increasing max_address. */
11155 HOST_WIDE_INT max_address;
5519a4f9 11156 /* Similarly for an entry inserted for a backwards ref. */
d5b7b3ae
RE
11157 HOST_WIDE_INT min_address;
11158 /* The number of fixes referencing this entry. This can become zero
11159 if we "unpush" an entry. In this case we ignore the entry when we
11160 come to emit the code. */
11161 int refcount;
11162 /* The offset from the start of the minipool. */
11163 HOST_WIDE_INT offset;
11164 /* The value in table. */
11165 rtx value;
11166 /* The mode of value. */
11167 enum machine_mode mode;
5a9335ef
NC
11168 /* The size of the value. With iWMMXt enabled
11169 sizes > 4 also imply an alignment of 8-bytes. */
d5b7b3ae
RE
11170 int fix_size;
11171};
11172
11173struct minipool_fixup
2b835d68 11174{
d5b7b3ae
RE
11175 Mfix * next;
11176 rtx insn;
11177 HOST_WIDE_INT address;
11178 rtx * loc;
11179 enum machine_mode mode;
11180 int fix_size;
11181 rtx value;
11182 Mnode * minipool;
11183 HOST_WIDE_INT forwards;
11184 HOST_WIDE_INT backwards;
11185};
2b835d68 11186
d5b7b3ae
RE
11187/* Fixes less than a word need padding out to a word boundary. */
11188#define MINIPOOL_FIX_SIZE(mode) \
11189 (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
2b835d68 11190
d5b7b3ae
RE
11191static Mnode * minipool_vector_head;
11192static Mnode * minipool_vector_tail;
11193static rtx minipool_vector_label;
34a9f549 11194static int minipool_pad;
332072db 11195
d5b7b3ae
RE
11196/* The linked list of all minipool fixes required for this function. */
11197Mfix * minipool_fix_head;
11198Mfix * minipool_fix_tail;
11199/* The fix entry for the current minipool, once it has been placed. */
11200Mfix * minipool_barrier;
11201
11202/* Determines if INSN is the start of a jump table. Returns the end
11203 of the TABLE or NULL_RTX. */
11204static rtx
e32bac5b 11205is_jump_table (rtx insn)
2b835d68 11206{
d5b7b3ae 11207 rtx table;
f676971a 11208
d5b7b3ae
RE
11209 if (GET_CODE (insn) == JUMP_INSN
11210 && JUMP_LABEL (insn) != NULL
11211 && ((table = next_real_insn (JUMP_LABEL (insn)))
11212 == next_real_insn (insn))
11213 && table != NULL
11214 && GET_CODE (table) == JUMP_INSN
11215 && (GET_CODE (PATTERN (table)) == ADDR_VEC
11216 || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
11217 return table;
11218
11219 return NULL_RTX;
2b835d68
RE
11220}
11221
657d9449
RE
11222#ifndef JUMP_TABLES_IN_TEXT_SECTION
11223#define JUMP_TABLES_IN_TEXT_SECTION 0
11224#endif
11225
d5b7b3ae 11226static HOST_WIDE_INT
e32bac5b 11227get_jump_table_size (rtx insn)
2b835d68 11228{
657d9449
RE
11229 /* ADDR_VECs only take room if read-only data does into the text
11230 section. */
d6b5193b 11231 if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
657d9449
RE
11232 {
11233 rtx body = PATTERN (insn);
11234 int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
5b3e6663
PB
11235 HOST_WIDE_INT size;
11236 HOST_WIDE_INT modesize;
2b835d68 11237
5b3e6663
PB
11238 modesize = GET_MODE_SIZE (GET_MODE (body));
11239 size = modesize * XVECLEN (body, elt);
11240 switch (modesize)
11241 {
11242 case 1:
88512ba0 11243 /* Round up size of TBB table to a halfword boundary. */
5b3e6663
PB
11244 size = (size + 1) & ~(HOST_WIDE_INT)1;
11245 break;
11246 case 2:
7a085dce 11247 /* No padding necessary for TBH. */
5b3e6663
PB
11248 break;
11249 case 4:
11250 /* Add two bytes for alignment on Thumb. */
11251 if (TARGET_THUMB)
11252 size += 2;
11253 break;
11254 default:
11255 gcc_unreachable ();
11256 }
11257 return size;
657d9449
RE
11258 }
11259
11260 return 0;
d5b7b3ae 11261}
2b835d68 11262
d5b7b3ae
RE
11263/* Move a minipool fix MP from its current location to before MAX_MP.
11264 If MAX_MP is NULL, then MP doesn't need moving, but the addressing
093354e0 11265 constraints may need updating. */
d5b7b3ae 11266static Mnode *
e32bac5b
RE
11267move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
11268 HOST_WIDE_INT max_address)
d5b7b3ae 11269{
e6d29d15
NS
11270 /* The code below assumes these are different. */
11271 gcc_assert (mp != max_mp);
d5b7b3ae
RE
11272
11273 if (max_mp == NULL)
11274 {
11275 if (max_address < mp->max_address)
11276 mp->max_address = max_address;
11277 }
11278 else
2b835d68 11279 {
d5b7b3ae
RE
11280 if (max_address > max_mp->max_address - mp->fix_size)
11281 mp->max_address = max_mp->max_address - mp->fix_size;
11282 else
11283 mp->max_address = max_address;
2b835d68 11284
d5b7b3ae
RE
11285 /* Unlink MP from its current position. Since max_mp is non-null,
11286 mp->prev must be non-null. */
11287 mp->prev->next = mp->next;
11288 if (mp->next != NULL)
11289 mp->next->prev = mp->prev;
11290 else
11291 minipool_vector_tail = mp->prev;
2b835d68 11292
d5b7b3ae
RE
11293 /* Re-insert it before MAX_MP. */
11294 mp->next = max_mp;
11295 mp->prev = max_mp->prev;
11296 max_mp->prev = mp;
f676971a 11297
d5b7b3ae
RE
11298 if (mp->prev != NULL)
11299 mp->prev->next = mp;
11300 else
11301 minipool_vector_head = mp;
11302 }
2b835d68 11303
d5b7b3ae
RE
11304 /* Save the new entry. */
11305 max_mp = mp;
11306
d6a7951f 11307 /* Scan over the preceding entries and adjust their addresses as
d5b7b3ae
RE
11308 required. */
11309 while (mp->prev != NULL
11310 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
11311 {
11312 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
11313 mp = mp->prev;
2b835d68
RE
11314 }
11315
d5b7b3ae 11316 return max_mp;
2b835d68
RE
11317}
11318
d5b7b3ae
RE
11319/* Add a constant to the minipool for a forward reference. Returns the
11320 node added or NULL if the constant will not fit in this pool. */
11321static Mnode *
e32bac5b 11322add_minipool_forward_ref (Mfix *fix)
d5b7b3ae
RE
11323{
11324 /* If set, max_mp is the first pool_entry that has a lower
11325 constraint than the one we are trying to add. */
11326 Mnode * max_mp = NULL;
34a9f549 11327 HOST_WIDE_INT max_address = fix->address + fix->forwards - minipool_pad;
d5b7b3ae 11328 Mnode * mp;
f676971a 11329
7a7017bc
PB
11330 /* If the minipool starts before the end of FIX->INSN then this FIX
11331 can not be placed into the current pool. Furthermore, adding the
11332 new constant pool entry may cause the pool to start FIX_SIZE bytes
11333 earlier. */
d5b7b3ae 11334 if (minipool_vector_head &&
7a7017bc
PB
11335 (fix->address + get_attr_length (fix->insn)
11336 >= minipool_vector_head->max_address - fix->fix_size))
d5b7b3ae 11337 return NULL;
2b835d68 11338
d5b7b3ae
RE
11339 /* Scan the pool to see if a constant with the same value has
11340 already been added. While we are doing this, also note the
11341 location where we must insert the constant if it doesn't already
11342 exist. */
11343 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11344 {
11345 if (GET_CODE (fix->value) == GET_CODE (mp->value)
11346 && fix->mode == mp->mode
11347 && (GET_CODE (fix->value) != CODE_LABEL
11348 || (CODE_LABEL_NUMBER (fix->value)
11349 == CODE_LABEL_NUMBER (mp->value)))
11350 && rtx_equal_p (fix->value, mp->value))
11351 {
11352 /* More than one fix references this entry. */
11353 mp->refcount++;
11354 return move_minipool_fix_forward_ref (mp, max_mp, max_address);
11355 }
11356
11357 /* Note the insertion point if necessary. */
11358 if (max_mp == NULL
11359 && mp->max_address > max_address)
11360 max_mp = mp;
5a9335ef
NC
11361
11362 /* If we are inserting an 8-bytes aligned quantity and
11363 we have not already found an insertion point, then
11364 make sure that all such 8-byte aligned quantities are
11365 placed at the start of the pool. */
5848830f 11366 if (ARM_DOUBLEWORD_ALIGN
5a9335ef 11367 && max_mp == NULL
88f77cba
JB
11368 && fix->fix_size >= 8
11369 && mp->fix_size < 8)
5a9335ef
NC
11370 {
11371 max_mp = mp;
11372 max_address = mp->max_address;
11373 }
d5b7b3ae
RE
11374 }
11375
11376 /* The value is not currently in the minipool, so we need to create
11377 a new entry for it. If MAX_MP is NULL, the entry will be put on
11378 the end of the list since the placement is less constrained than
11379 any existing entry. Otherwise, we insert the new fix before
6bc82793 11380 MAX_MP and, if necessary, adjust the constraints on the other
d5b7b3ae 11381 entries. */
5ed6ace5 11382 mp = XNEW (Mnode);
d5b7b3ae
RE
11383 mp->fix_size = fix->fix_size;
11384 mp->mode = fix->mode;
11385 mp->value = fix->value;
11386 mp->refcount = 1;
11387 /* Not yet required for a backwards ref. */
11388 mp->min_address = -65536;
11389
11390 if (max_mp == NULL)
11391 {
11392 mp->max_address = max_address;
11393 mp->next = NULL;
11394 mp->prev = minipool_vector_tail;
11395
11396 if (mp->prev == NULL)
11397 {
11398 minipool_vector_head = mp;
11399 minipool_vector_label = gen_label_rtx ();
7551cbc7 11400 }
2b835d68 11401 else
d5b7b3ae 11402 mp->prev->next = mp;
2b835d68 11403
d5b7b3ae
RE
11404 minipool_vector_tail = mp;
11405 }
11406 else
11407 {
11408 if (max_address > max_mp->max_address - mp->fix_size)
11409 mp->max_address = max_mp->max_address - mp->fix_size;
11410 else
11411 mp->max_address = max_address;
11412
11413 mp->next = max_mp;
11414 mp->prev = max_mp->prev;
11415 max_mp->prev = mp;
11416 if (mp->prev != NULL)
11417 mp->prev->next = mp;
11418 else
11419 minipool_vector_head = mp;
11420 }
11421
11422 /* Save the new entry. */
11423 max_mp = mp;
11424
d6a7951f 11425 /* Scan over the preceding entries and adjust their addresses as
d5b7b3ae
RE
11426 required. */
11427 while (mp->prev != NULL
11428 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
11429 {
11430 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
11431 mp = mp->prev;
2b835d68
RE
11432 }
11433
d5b7b3ae
RE
11434 return max_mp;
11435}
11436
11437static Mnode *
e32bac5b
RE
11438move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
11439 HOST_WIDE_INT min_address)
d5b7b3ae
RE
11440{
11441 HOST_WIDE_INT offset;
11442
e6d29d15
NS
11443 /* The code below assumes these are different. */
11444 gcc_assert (mp != min_mp);
d5b7b3ae
RE
11445
11446 if (min_mp == NULL)
2b835d68 11447 {
d5b7b3ae
RE
11448 if (min_address > mp->min_address)
11449 mp->min_address = min_address;
11450 }
11451 else
11452 {
11453 /* We will adjust this below if it is too loose. */
11454 mp->min_address = min_address;
11455
11456 /* Unlink MP from its current position. Since min_mp is non-null,
11457 mp->next must be non-null. */
11458 mp->next->prev = mp->prev;
11459 if (mp->prev != NULL)
11460 mp->prev->next = mp->next;
11461 else
11462 minipool_vector_head = mp->next;
11463
11464 /* Reinsert it after MIN_MP. */
11465 mp->prev = min_mp;
11466 mp->next = min_mp->next;
11467 min_mp->next = mp;
11468 if (mp->next != NULL)
11469 mp->next->prev = mp;
2b835d68 11470 else
d5b7b3ae
RE
11471 minipool_vector_tail = mp;
11472 }
11473
11474 min_mp = mp;
11475
11476 offset = 0;
11477 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11478 {
11479 mp->offset = offset;
11480 if (mp->refcount > 0)
11481 offset += mp->fix_size;
11482
11483 if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
11484 mp->next->min_address = mp->min_address + mp->fix_size;
11485 }
11486
11487 return min_mp;
f676971a 11488}
d5b7b3ae
RE
11489
11490/* Add a constant to the minipool for a backward reference. Returns the
f676971a 11491 node added or NULL if the constant will not fit in this pool.
d5b7b3ae
RE
11492
11493 Note that the code for insertion for a backwards reference can be
11494 somewhat confusing because the calculated offsets for each fix do
11495 not take into account the size of the pool (which is still under
11496 construction. */
11497static Mnode *
e32bac5b 11498add_minipool_backward_ref (Mfix *fix)
d5b7b3ae
RE
11499{
11500 /* If set, min_mp is the last pool_entry that has a lower constraint
11501 than the one we are trying to add. */
e32bac5b 11502 Mnode *min_mp = NULL;
d5b7b3ae
RE
11503 /* This can be negative, since it is only a constraint. */
11504 HOST_WIDE_INT min_address = fix->address - fix->backwards;
e32bac5b 11505 Mnode *mp;
d5b7b3ae
RE
11506
11507 /* If we can't reach the current pool from this insn, or if we can't
11508 insert this entry at the end of the pool without pushing other
11509 fixes out of range, then we don't try. This ensures that we
11510 can't fail later on. */
11511 if (min_address >= minipool_barrier->address
11512 || (minipool_vector_tail->min_address + fix->fix_size
11513 >= minipool_barrier->address))
11514 return NULL;
11515
11516 /* Scan the pool to see if a constant with the same value has
11517 already been added. While we are doing this, also note the
11518 location where we must insert the constant if it doesn't already
11519 exist. */
11520 for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
11521 {
11522 if (GET_CODE (fix->value) == GET_CODE (mp->value)
11523 && fix->mode == mp->mode
11524 && (GET_CODE (fix->value) != CODE_LABEL
11525 || (CODE_LABEL_NUMBER (fix->value)
11526 == CODE_LABEL_NUMBER (mp->value)))
11527 && rtx_equal_p (fix->value, mp->value)
11528 /* Check that there is enough slack to move this entry to the
11529 end of the table (this is conservative). */
f676971a
EC
11530 && (mp->max_address
11531 > (minipool_barrier->address
d5b7b3ae
RE
11532 + minipool_vector_tail->offset
11533 + minipool_vector_tail->fix_size)))
11534 {
11535 mp->refcount++;
11536 return move_minipool_fix_backward_ref (mp, min_mp, min_address);
11537 }
11538
11539 if (min_mp != NULL)
11540 mp->min_address += fix->fix_size;
11541 else
11542 {
11543 /* Note the insertion point if necessary. */
11544 if (mp->min_address < min_address)
5a9335ef
NC
11545 {
11546 /* For now, we do not allow the insertion of 8-byte alignment
11547 requiring nodes anywhere but at the start of the pool. */
5848830f 11548 if (ARM_DOUBLEWORD_ALIGN
88f77cba 11549 && fix->fix_size >= 8 && mp->fix_size < 8)
5a9335ef
NC
11550 return NULL;
11551 else
11552 min_mp = mp;
11553 }
d5b7b3ae
RE
11554 else if (mp->max_address
11555 < minipool_barrier->address + mp->offset + fix->fix_size)
11556 {
11557 /* Inserting before this entry would push the fix beyond
11558 its maximum address (which can happen if we have
11559 re-located a forwards fix); force the new fix to come
11560 after it. */
853ff9e2
JM
11561 if (ARM_DOUBLEWORD_ALIGN
11562 && fix->fix_size >= 8 && mp->fix_size < 8)
11563 return NULL;
11564 else
11565 {
11566 min_mp = mp;
11567 min_address = mp->min_address + fix->fix_size;
11568 }
d5b7b3ae 11569 }
853ff9e2
JM
11570 /* Do not insert a non-8-byte aligned quantity before 8-byte
11571 aligned quantities. */
5848830f 11572 else if (ARM_DOUBLEWORD_ALIGN
853ff9e2
JM
11573 && fix->fix_size < 8
11574 && mp->fix_size >= 8)
5a9335ef
NC
11575 {
11576 min_mp = mp;
11577 min_address = mp->min_address + fix->fix_size;
11578 }
d5b7b3ae
RE
11579 }
11580 }
11581
11582 /* We need to create a new entry. */
5ed6ace5 11583 mp = XNEW (Mnode);
d5b7b3ae
RE
11584 mp->fix_size = fix->fix_size;
11585 mp->mode = fix->mode;
11586 mp->value = fix->value;
11587 mp->refcount = 1;
11588 mp->max_address = minipool_barrier->address + 65536;
11589
11590 mp->min_address = min_address;
11591
11592 if (min_mp == NULL)
11593 {
11594 mp->prev = NULL;
11595 mp->next = minipool_vector_head;
11596
11597 if (mp->next == NULL)
11598 {
11599 minipool_vector_tail = mp;
11600 minipool_vector_label = gen_label_rtx ();
11601 }
11602 else
11603 mp->next->prev = mp;
11604
11605 minipool_vector_head = mp;
11606 }
11607 else
11608 {
11609 mp->next = min_mp->next;
11610 mp->prev = min_mp;
11611 min_mp->next = mp;
f676971a 11612
d5b7b3ae
RE
11613 if (mp->next != NULL)
11614 mp->next->prev = mp;
11615 else
11616 minipool_vector_tail = mp;
11617 }
11618
11619 /* Save the new entry. */
11620 min_mp = mp;
11621
11622 if (mp->prev)
11623 mp = mp->prev;
11624 else
11625 mp->offset = 0;
11626
11627 /* Scan over the following entries and adjust their offsets. */
11628 while (mp->next != NULL)
11629 {
11630 if (mp->next->min_address < mp->min_address + mp->fix_size)
11631 mp->next->min_address = mp->min_address + mp->fix_size;
11632
11633 if (mp->refcount)
11634 mp->next->offset = mp->offset + mp->fix_size;
11635 else
11636 mp->next->offset = mp->offset;
11637
11638 mp = mp->next;
11639 }
11640
11641 return min_mp;
11642}
11643
11644static void
e32bac5b 11645assign_minipool_offsets (Mfix *barrier)
d5b7b3ae
RE
11646{
11647 HOST_WIDE_INT offset = 0;
e32bac5b 11648 Mnode *mp;
d5b7b3ae
RE
11649
11650 minipool_barrier = barrier;
11651
11652 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11653 {
11654 mp->offset = offset;
f676971a 11655
d5b7b3ae
RE
11656 if (mp->refcount > 0)
11657 offset += mp->fix_size;
11658 }
11659}
11660
11661/* Output the literal table */
11662static void
e32bac5b 11663dump_minipool (rtx scan)
d5b7b3ae 11664{
5a9335ef
NC
11665 Mnode * mp;
11666 Mnode * nmp;
11667 int align64 = 0;
11668
5848830f 11669 if (ARM_DOUBLEWORD_ALIGN)
5a9335ef 11670 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
88f77cba 11671 if (mp->refcount > 0 && mp->fix_size >= 8)
5a9335ef
NC
11672 {
11673 align64 = 1;
11674 break;
11675 }
d5b7b3ae 11676
c263766c
RH
11677 if (dump_file)
11678 fprintf (dump_file,
5a9335ef
NC
11679 ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
11680 INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
d5b7b3ae
RE
11681
11682 scan = emit_label_after (gen_label_rtx (), scan);
5a9335ef 11683 scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
d5b7b3ae
RE
11684 scan = emit_label_after (minipool_vector_label, scan);
11685
11686 for (mp = minipool_vector_head; mp != NULL; mp = nmp)
11687 {
11688 if (mp->refcount > 0)
11689 {
c263766c 11690 if (dump_file)
d5b7b3ae 11691 {
f676971a 11692 fprintf (dump_file,
d5b7b3ae
RE
11693 ";; Offset %u, min %ld, max %ld ",
11694 (unsigned) mp->offset, (unsigned long) mp->min_address,
11695 (unsigned long) mp->max_address);
c263766c
RH
11696 arm_print_value (dump_file, mp->value);
11697 fputc ('\n', dump_file);
d5b7b3ae
RE
11698 }
11699
11700 switch (mp->fix_size)
11701 {
11702#ifdef HAVE_consttable_1
11703 case 1:
11704 scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
11705 break;
11706
11707#endif
11708#ifdef HAVE_consttable_2
11709 case 2:
11710 scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
11711 break;
11712
11713#endif
11714#ifdef HAVE_consttable_4
11715 case 4:
11716 scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
11717 break;
11718
11719#endif
11720#ifdef HAVE_consttable_8
11721 case 8:
11722 scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
11723 break;
11724
88f77cba
JB
11725#endif
11726#ifdef HAVE_consttable_16
11727 case 16:
11728 scan = emit_insn_after (gen_consttable_16 (mp->value), scan);
11729 break;
11730
d5b7b3ae
RE
11731#endif
11732 default:
e6d29d15 11733 gcc_unreachable ();
d5b7b3ae
RE
11734 }
11735 }
11736
11737 nmp = mp->next;
11738 free (mp);
2b835d68
RE
11739 }
11740
d5b7b3ae
RE
11741 minipool_vector_head = minipool_vector_tail = NULL;
11742 scan = emit_insn_after (gen_consttable_end (), scan);
11743 scan = emit_barrier_after (scan);
2b835d68
RE
11744}
11745
d5b7b3ae
RE
11746/* Return the cost of forcibly inserting a barrier after INSN. */
11747static int
e32bac5b 11748arm_barrier_cost (rtx insn)
949d79eb 11749{
d5b7b3ae
RE
11750 /* Basing the location of the pool on the loop depth is preferable,
11751 but at the moment, the basic block information seems to be
11752 corrupt by this stage of the compilation. */
11753 int base_cost = 50;
11754 rtx next = next_nonnote_insn (insn);
11755
11756 if (next != NULL && GET_CODE (next) == CODE_LABEL)
11757 base_cost -= 20;
11758
11759 switch (GET_CODE (insn))
11760 {
11761 case CODE_LABEL:
11762 /* It will always be better to place the table before the label, rather
11763 than after it. */
f676971a 11764 return 50;
949d79eb 11765
d5b7b3ae
RE
11766 case INSN:
11767 case CALL_INSN:
11768 return base_cost;
11769
11770 case JUMP_INSN:
11771 return base_cost - 10;
11772
11773 default:
11774 return base_cost + 10;
11775 }
11776}
11777
11778/* Find the best place in the insn stream in the range
11779 (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
11780 Create the barrier by inserting a jump and add a new fix entry for
11781 it. */
11782static Mfix *
e32bac5b 11783create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
d5b7b3ae
RE
11784{
11785 HOST_WIDE_INT count = 0;
11786 rtx barrier;
11787 rtx from = fix->insn;
7a7017bc
PB
11788 /* The instruction after which we will insert the jump. */
11789 rtx selected = NULL;
d5b7b3ae 11790 int selected_cost;
7a7017bc 11791 /* The address at which the jump instruction will be placed. */
d5b7b3ae
RE
11792 HOST_WIDE_INT selected_address;
11793 Mfix * new_fix;
11794 HOST_WIDE_INT max_count = max_address - fix->address;
11795 rtx label = gen_label_rtx ();
11796
11797 selected_cost = arm_barrier_cost (from);
11798 selected_address = fix->address;
11799
11800 while (from && count < max_count)
11801 {
11802 rtx tmp;
11803 int new_cost;
11804
11805 /* This code shouldn't have been called if there was a natural barrier
11806 within range. */
e6d29d15 11807 gcc_assert (GET_CODE (from) != BARRIER);
d5b7b3ae
RE
11808
11809 /* Count the length of this insn. */
11810 count += get_attr_length (from);
11811
11812 /* If there is a jump table, add its length. */
11813 tmp = is_jump_table (from);
11814 if (tmp != NULL)
11815 {
11816 count += get_jump_table_size (tmp);
11817
11818 /* Jump tables aren't in a basic block, so base the cost on
11819 the dispatch insn. If we select this location, we will
11820 still put the pool after the table. */
11821 new_cost = arm_barrier_cost (from);
11822
7a7017bc
PB
11823 if (count < max_count
11824 && (!selected || new_cost <= selected_cost))
d5b7b3ae
RE
11825 {
11826 selected = tmp;
11827 selected_cost = new_cost;
11828 selected_address = fix->address + count;
11829 }
11830
11831 /* Continue after the dispatch table. */
11832 from = NEXT_INSN (tmp);
11833 continue;
11834 }
11835
11836 new_cost = arm_barrier_cost (from);
f676971a 11837
7a7017bc
PB
11838 if (count < max_count
11839 && (!selected || new_cost <= selected_cost))
d5b7b3ae
RE
11840 {
11841 selected = from;
11842 selected_cost = new_cost;
11843 selected_address = fix->address + count;
11844 }
11845
11846 from = NEXT_INSN (from);
11847 }
11848
7a7017bc
PB
11849 /* Make sure that we found a place to insert the jump. */
11850 gcc_assert (selected);
11851
d5b7b3ae
RE
11852 /* Create a new JUMP_INSN that branches around a barrier. */
11853 from = emit_jump_insn_after (gen_jump (label), selected);
11854 JUMP_LABEL (from) = label;
11855 barrier = emit_barrier_after (from);
11856 emit_label_after (label, barrier);
11857
11858 /* Create a minipool barrier entry for the new barrier. */
c7319d87 11859 new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
d5b7b3ae
RE
11860 new_fix->insn = barrier;
11861 new_fix->address = selected_address;
11862 new_fix->next = fix->next;
11863 fix->next = new_fix;
11864
11865 return new_fix;
11866}
11867
11868/* Record that there is a natural barrier in the insn stream at
11869 ADDRESS. */
949d79eb 11870static void
e32bac5b 11871push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
2b835d68 11872{
c7319d87 11873 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
ad076f4e 11874
949d79eb
RE
11875 fix->insn = insn;
11876 fix->address = address;
2b835d68 11877
949d79eb
RE
11878 fix->next = NULL;
11879 if (minipool_fix_head != NULL)
11880 minipool_fix_tail->next = fix;
11881 else
11882 minipool_fix_head = fix;
11883
11884 minipool_fix_tail = fix;
11885}
2b835d68 11886
d5b7b3ae
RE
11887/* Record INSN, which will need fixing up to load a value from the
11888 minipool. ADDRESS is the offset of the insn since the start of the
11889 function; LOC is a pointer to the part of the insn which requires
11890 fixing; VALUE is the constant that must be loaded, which is of type
11891 MODE. */
949d79eb 11892static void
e32bac5b
RE
11893push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
11894 enum machine_mode mode, rtx value)
949d79eb 11895{
c7319d87 11896 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
949d79eb 11897
949d79eb
RE
11898 fix->insn = insn;
11899 fix->address = address;
11900 fix->loc = loc;
11901 fix->mode = mode;
d5b7b3ae 11902 fix->fix_size = MINIPOOL_FIX_SIZE (mode);
949d79eb 11903 fix->value = value;
d5b7b3ae
RE
11904 fix->forwards = get_attr_pool_range (insn);
11905 fix->backwards = get_attr_neg_pool_range (insn);
11906 fix->minipool = NULL;
949d79eb
RE
11907
11908 /* If an insn doesn't have a range defined for it, then it isn't
e6d29d15 11909 expecting to be reworked by this code. Better to stop now than
949d79eb 11910 to generate duff assembly code. */
e6d29d15 11911 gcc_assert (fix->forwards || fix->backwards);
949d79eb 11912
34a9f549
PB
11913 /* If an entry requires 8-byte alignment then assume all constant pools
11914 require 4 bytes of padding. Trying to do this later on a per-pool
917f1b7e 11915 basis is awkward because existing pool entries have to be modified. */
88f77cba 11916 if (ARM_DOUBLEWORD_ALIGN && fix->fix_size >= 8)
34a9f549 11917 minipool_pad = 4;
5a9335ef 11918
c263766c 11919 if (dump_file)
d5b7b3ae 11920 {
c263766c 11921 fprintf (dump_file,
d5b7b3ae
RE
11922 ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
11923 GET_MODE_NAME (mode),
f676971a 11924 INSN_UID (insn), (unsigned long) address,
d5b7b3ae 11925 -1 * (long)fix->backwards, (long)fix->forwards);
c263766c
RH
11926 arm_print_value (dump_file, fix->value);
11927 fprintf (dump_file, "\n");
d5b7b3ae
RE
11928 }
11929
6354dc9b 11930 /* Add it to the chain of fixes. */
949d79eb 11931 fix->next = NULL;
f676971a 11932
949d79eb
RE
11933 if (minipool_fix_head != NULL)
11934 minipool_fix_tail->next = fix;
11935 else
11936 minipool_fix_head = fix;
11937
11938 minipool_fix_tail = fix;
11939}
11940
9b901d50 11941/* Return the cost of synthesizing a 64-bit constant VAL inline.
2075b05d
RE
11942 Returns the number of insns needed, or 99 if we don't know how to
11943 do it. */
11944int
11945arm_const_double_inline_cost (rtx val)
b9e8bfda 11946{
9b901d50
RE
11947 rtx lowpart, highpart;
11948 enum machine_mode mode;
e0b92319 11949
9b901d50 11950 mode = GET_MODE (val);
b9e8bfda 11951
9b901d50
RE
11952 if (mode == VOIDmode)
11953 mode = DImode;
11954
11955 gcc_assert (GET_MODE_SIZE (mode) == 8);
e0b92319 11956
9b901d50
RE
11957 lowpart = gen_lowpart (SImode, val);
11958 highpart = gen_highpart_mode (SImode, mode, val);
e0b92319 11959
9b901d50
RE
11960 gcc_assert (GET_CODE (lowpart) == CONST_INT);
11961 gcc_assert (GET_CODE (highpart) == CONST_INT);
11962
11963 return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
2075b05d 11964 NULL_RTX, NULL_RTX, 0, 0)
9b901d50 11965 + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
2075b05d
RE
11966 NULL_RTX, NULL_RTX, 0, 0));
11967}
11968
f3b569ca 11969/* Return true if it is worthwhile to split a 64-bit constant into two
b4a58f80
RE
11970 32-bit operations. This is the case if optimizing for size, or
11971 if we have load delay slots, or if one 32-bit part can be done with
11972 a single data operation. */
11973bool
11974arm_const_double_by_parts (rtx val)
11975{
11976 enum machine_mode mode = GET_MODE (val);
11977 rtx part;
11978
11979 if (optimize_size || arm_ld_sched)
11980 return true;
11981
11982 if (mode == VOIDmode)
11983 mode = DImode;
e0b92319 11984
b4a58f80 11985 part = gen_highpart_mode (SImode, mode, val);
e0b92319 11986
b4a58f80 11987 gcc_assert (GET_CODE (part) == CONST_INT);
e0b92319 11988
b4a58f80
RE
11989 if (const_ok_for_arm (INTVAL (part))
11990 || const_ok_for_arm (~INTVAL (part)))
11991 return true;
e0b92319 11992
b4a58f80 11993 part = gen_lowpart (SImode, val);
e0b92319 11994
b4a58f80 11995 gcc_assert (GET_CODE (part) == CONST_INT);
e0b92319 11996
b4a58f80
RE
11997 if (const_ok_for_arm (INTVAL (part))
11998 || const_ok_for_arm (~INTVAL (part)))
11999 return true;
e0b92319 12000
b4a58f80
RE
12001 return false;
12002}
12003
73160ba9
DJ
12004/* Return true if it is possible to inline both the high and low parts
12005 of a 64-bit constant into 32-bit data processing instructions. */
12006bool
12007arm_const_double_by_immediates (rtx val)
12008{
12009 enum machine_mode mode = GET_MODE (val);
12010 rtx part;
12011
12012 if (mode == VOIDmode)
12013 mode = DImode;
12014
12015 part = gen_highpart_mode (SImode, mode, val);
12016
12017 gcc_assert (GET_CODE (part) == CONST_INT);
12018
12019 if (!const_ok_for_arm (INTVAL (part)))
12020 return false;
12021
12022 part = gen_lowpart (SImode, val);
12023
12024 gcc_assert (GET_CODE (part) == CONST_INT);
12025
12026 if (!const_ok_for_arm (INTVAL (part)))
12027 return false;
12028
12029 return true;
12030}
12031
f0375c66
NC
12032/* Scan INSN and note any of its operands that need fixing.
12033 If DO_PUSHES is false we do not actually push any of the fixups
9b901d50 12034 needed. The function returns TRUE if any fixups were needed/pushed.
f0375c66
NC
12035 This is used by arm_memory_load_p() which needs to know about loads
12036 of constants that will be converted into minipool loads. */
f0375c66 12037static bool
e32bac5b 12038note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
949d79eb 12039{
f0375c66 12040 bool result = false;
949d79eb
RE
12041 int opno;
12042
d5b7b3ae 12043 extract_insn (insn);
949d79eb 12044
5895f793 12045 if (!constrain_operands (1))
949d79eb
RE
12046 fatal_insn_not_found (insn);
12047
8c2a5582
RE
12048 if (recog_data.n_alternatives == 0)
12049 return false;
12050
9b901d50
RE
12051 /* Fill in recog_op_alt with information about the constraints of
12052 this insn. */
949d79eb
RE
12053 preprocess_constraints ();
12054
1ccbefce 12055 for (opno = 0; opno < recog_data.n_operands; opno++)
949d79eb 12056 {
6354dc9b 12057 /* Things we need to fix can only occur in inputs. */
36ab44c7 12058 if (recog_data.operand_type[opno] != OP_IN)
949d79eb
RE
12059 continue;
12060
12061 /* If this alternative is a memory reference, then any mention
12062 of constants in this alternative is really to fool reload
12063 into allowing us to accept one there. We need to fix them up
12064 now so that we output the right code. */
12065 if (recog_op_alt[opno][which_alternative].memory_ok)
12066 {
1ccbefce 12067 rtx op = recog_data.operand[opno];
949d79eb 12068
9b901d50 12069 if (CONSTANT_P (op))
f0375c66
NC
12070 {
12071 if (do_pushes)
12072 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
12073 recog_data.operand_mode[opno], op);
12074 result = true;
12075 }
d5b7b3ae 12076 else if (GET_CODE (op) == MEM
949d79eb
RE
12077 && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
12078 && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
f0375c66
NC
12079 {
12080 if (do_pushes)
244b1afb
RE
12081 {
12082 rtx cop = avoid_constant_pool_reference (op);
12083
12084 /* Casting the address of something to a mode narrower
12085 than a word can cause avoid_constant_pool_reference()
12086 to return the pool reference itself. That's no good to
f676971a 12087 us here. Lets just hope that we can use the
244b1afb
RE
12088 constant pool value directly. */
12089 if (op == cop)
c769a35d 12090 cop = get_pool_constant (XEXP (op, 0));
244b1afb
RE
12091
12092 push_minipool_fix (insn, address,
12093 recog_data.operand_loc[opno],
c769a35d 12094 recog_data.operand_mode[opno], cop);
244b1afb 12095 }
f0375c66
NC
12096
12097 result = true;
12098 }
949d79eb 12099 }
2b835d68 12100 }
f0375c66
NC
12101
12102 return result;
2b835d68
RE
12103}
12104
eef5973d
BS
12105/* Convert instructions to their cc-clobbering variant if possible, since
12106 that allows us to use smaller encodings. */
12107
12108static void
12109thumb2_reorg (void)
12110{
12111 basic_block bb;
12112 regset_head live;
12113
12114 INIT_REG_SET (&live);
12115
12116 /* We are freeing block_for_insn in the toplev to keep compatibility
12117 with old MDEP_REORGS that are not CFG based. Recompute it now. */
12118 compute_bb_for_insn ();
12119 df_analyze ();
12120
12121 FOR_EACH_BB (bb)
12122 {
12123 rtx insn;
12124 COPY_REG_SET (&live, DF_LR_OUT (bb));
12125 df_simulate_initialize_backwards (bb, &live);
12126 FOR_BB_INSNS_REVERSE (bb, insn)
12127 {
12128 if (NONJUMP_INSN_P (insn)
12129 && !REGNO_REG_SET_P (&live, CC_REGNUM))
12130 {
12131 rtx pat = PATTERN (insn);
12132 if (GET_CODE (pat) == SET
12133 && low_register_operand (XEXP (pat, 0), SImode)
12134 && thumb_16bit_operator (XEXP (pat, 1), SImode)
12135 && low_register_operand (XEXP (XEXP (pat, 1), 0), SImode)
12136 && low_register_operand (XEXP (XEXP (pat, 1), 1), SImode))
12137 {
12138 rtx dst = XEXP (pat, 0);
12139 rtx src = XEXP (pat, 1);
12140 rtx op0 = XEXP (src, 0);
eef5973d
BS
12141 if (rtx_equal_p (dst, op0)
12142 || GET_CODE (src) == PLUS || GET_CODE (src) == MINUS)
12143 {
12144 rtx ccreg = gen_rtx_REG (CCmode, CC_REGNUM);
12145 rtx clobber = gen_rtx_CLOBBER (VOIDmode, ccreg);
f63a6726 12146 rtvec vec = gen_rtvec (2, pat, clobber);
eef5973d
BS
12147 PATTERN (insn) = gen_rtx_PARALLEL (VOIDmode, vec);
12148 INSN_CODE (insn) = -1;
12149 }
12150 }
12151 }
12152 if (NONDEBUG_INSN_P (insn))
12153 df_simulate_one_insn_backwards (bb, insn, &live);
12154 }
12155 }
12156 CLEAR_REG_SET (&live);
12157}
12158
18dbd950
RS
12159/* Gcc puts the pool in the wrong place for ARM, since we can only
12160 load addresses a limited distance around the pc. We do some
12161 special munging to move the constant pool values to the correct
12162 point in the code. */
18dbd950 12163static void
e32bac5b 12164arm_reorg (void)
2b835d68
RE
12165{
12166 rtx insn;
d5b7b3ae
RE
12167 HOST_WIDE_INT address = 0;
12168 Mfix * fix;
ad076f4e 12169
eef5973d
BS
12170 if (TARGET_THUMB2)
12171 thumb2_reorg ();
12172
949d79eb 12173 minipool_fix_head = minipool_fix_tail = NULL;
2b835d68 12174
949d79eb
RE
12175 /* The first insn must always be a note, or the code below won't
12176 scan it properly. */
18dbd950 12177 insn = get_insns ();
e6d29d15 12178 gcc_assert (GET_CODE (insn) == NOTE);
34a9f549 12179 minipool_pad = 0;
949d79eb
RE
12180
12181 /* Scan all the insns and record the operands that will need fixing. */
18dbd950 12182 for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
2b835d68 12183 {
9b6b54e2 12184 if (TARGET_CIRRUS_FIX_INVALID_INSNS
f0375c66 12185 && (arm_cirrus_insn_p (insn)
9b6b54e2 12186 || GET_CODE (insn) == JUMP_INSN
f0375c66 12187 || arm_memory_load_p (insn)))
9b6b54e2
NC
12188 cirrus_reorg (insn);
12189
949d79eb 12190 if (GET_CODE (insn) == BARRIER)
d5b7b3ae 12191 push_minipool_barrier (insn, address);
f0375c66 12192 else if (INSN_P (insn))
949d79eb
RE
12193 {
12194 rtx table;
12195
f0375c66 12196 note_invalid_constants (insn, address, true);
949d79eb 12197 address += get_attr_length (insn);
d5b7b3ae 12198
949d79eb
RE
12199 /* If the insn is a vector jump, add the size of the table
12200 and skip the table. */
d5b7b3ae 12201 if ((table = is_jump_table (insn)) != NULL)
2b835d68 12202 {
d5b7b3ae 12203 address += get_jump_table_size (table);
949d79eb
RE
12204 insn = table;
12205 }
12206 }
12207 }
332072db 12208
d5b7b3ae 12209 fix = minipool_fix_head;
f676971a 12210
949d79eb 12211 /* Now scan the fixups and perform the required changes. */
d5b7b3ae 12212 while (fix)
949d79eb 12213 {
d5b7b3ae
RE
12214 Mfix * ftmp;
12215 Mfix * fdel;
12216 Mfix * last_added_fix;
12217 Mfix * last_barrier = NULL;
12218 Mfix * this_fix;
949d79eb
RE
12219
12220 /* Skip any further barriers before the next fix. */
12221 while (fix && GET_CODE (fix->insn) == BARRIER)
12222 fix = fix->next;
12223
d5b7b3ae 12224 /* No more fixes. */
949d79eb
RE
12225 if (fix == NULL)
12226 break;
332072db 12227
d5b7b3ae 12228 last_added_fix = NULL;
2b835d68 12229
d5b7b3ae 12230 for (ftmp = fix; ftmp; ftmp = ftmp->next)
949d79eb 12231 {
949d79eb 12232 if (GET_CODE (ftmp->insn) == BARRIER)
949d79eb 12233 {
d5b7b3ae
RE
12234 if (ftmp->address >= minipool_vector_head->max_address)
12235 break;
2b835d68 12236
d5b7b3ae 12237 last_barrier = ftmp;
2b835d68 12238 }
d5b7b3ae
RE
12239 else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
12240 break;
12241
12242 last_added_fix = ftmp; /* Keep track of the last fix added. */
2b835d68 12243 }
949d79eb 12244
d5b7b3ae
RE
12245 /* If we found a barrier, drop back to that; any fixes that we
12246 could have reached but come after the barrier will now go in
12247 the next mini-pool. */
949d79eb
RE
12248 if (last_barrier != NULL)
12249 {
f676971a 12250 /* Reduce the refcount for those fixes that won't go into this
d5b7b3ae
RE
12251 pool after all. */
12252 for (fdel = last_barrier->next;
12253 fdel && fdel != ftmp;
12254 fdel = fdel->next)
12255 {
12256 fdel->minipool->refcount--;
12257 fdel->minipool = NULL;
12258 }
12259
949d79eb
RE
12260 ftmp = last_barrier;
12261 }
12262 else
2bfa88dc 12263 {
d5b7b3ae
RE
12264 /* ftmp is first fix that we can't fit into this pool and
12265 there no natural barriers that we could use. Insert a
12266 new barrier in the code somewhere between the previous
12267 fix and this one, and arrange to jump around it. */
12268 HOST_WIDE_INT max_address;
12269
12270 /* The last item on the list of fixes must be a barrier, so
12271 we can never run off the end of the list of fixes without
12272 last_barrier being set. */
e6d29d15 12273 gcc_assert (ftmp);
d5b7b3ae
RE
12274
12275 max_address = minipool_vector_head->max_address;
2bfa88dc
RE
12276 /* Check that there isn't another fix that is in range that
12277 we couldn't fit into this pool because the pool was
12278 already too large: we need to put the pool before such an
7a7017bc
PB
12279 instruction. The pool itself may come just after the
12280 fix because create_fix_barrier also allows space for a
12281 jump instruction. */
d5b7b3ae 12282 if (ftmp->address < max_address)
7a7017bc 12283 max_address = ftmp->address + 1;
d5b7b3ae
RE
12284
12285 last_barrier = create_fix_barrier (last_added_fix, max_address);
12286 }
12287
12288 assign_minipool_offsets (last_barrier);
12289
12290 while (ftmp)
12291 {
12292 if (GET_CODE (ftmp->insn) != BARRIER
12293 && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
12294 == NULL))
12295 break;
2bfa88dc 12296
d5b7b3ae 12297 ftmp = ftmp->next;
2bfa88dc 12298 }
949d79eb
RE
12299
12300 /* Scan over the fixes we have identified for this pool, fixing them
12301 up and adding the constants to the pool itself. */
d5b7b3ae 12302 for (this_fix = fix; this_fix && ftmp != this_fix;
949d79eb
RE
12303 this_fix = this_fix->next)
12304 if (GET_CODE (this_fix->insn) != BARRIER)
12305 {
949d79eb 12306 rtx addr
f676971a 12307 = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
949d79eb 12308 minipool_vector_label),
d5b7b3ae 12309 this_fix->minipool->offset);
949d79eb
RE
12310 *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
12311 }
12312
d5b7b3ae 12313 dump_minipool (last_barrier->insn);
949d79eb 12314 fix = ftmp;
2b835d68 12315 }
4b632bf1 12316
949d79eb
RE
12317 /* From now on we must synthesize any constants that we can't handle
12318 directly. This can happen if the RTL gets split during final
12319 instruction generation. */
4b632bf1 12320 after_arm_reorg = 1;
c7319d87
RE
12321
12322 /* Free the minipool memory. */
12323 obstack_free (&minipool_obstack, minipool_startobj);
2b835d68 12324}
cce8749e
CH
12325\f
12326/* Routines to output assembly language. */
12327
f3bb6135 12328/* If the rtx is the correct value then return the string of the number.
ff9940b0 12329 In this way we can ensure that valid double constants are generated even
6354dc9b 12330 when cross compiling. */
cd2b33d0 12331const char *
e32bac5b 12332fp_immediate_constant (rtx x)
ff9940b0
RE
12333{
12334 REAL_VALUE_TYPE r;
12335 int i;
f676971a 12336
9b66ebb1
PB
12337 if (!fp_consts_inited)
12338 init_fp_table ();
f676971a 12339
ff9940b0
RE
12340 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
12341 for (i = 0; i < 8; i++)
9b66ebb1
PB
12342 if (REAL_VALUES_EQUAL (r, values_fp[i]))
12343 return strings_fp[i];
f3bb6135 12344
e6d29d15 12345 gcc_unreachable ();
ff9940b0
RE
12346}
12347
9997d19d 12348/* As for fp_immediate_constant, but value is passed directly, not in rtx. */
cd2b33d0 12349static const char *
e32bac5b 12350fp_const_from_val (REAL_VALUE_TYPE *r)
9997d19d
RE
12351{
12352 int i;
12353
9b66ebb1
PB
12354 if (!fp_consts_inited)
12355 init_fp_table ();
9997d19d
RE
12356
12357 for (i = 0; i < 8; i++)
9b66ebb1
PB
12358 if (REAL_VALUES_EQUAL (*r, values_fp[i]))
12359 return strings_fp[i];
9997d19d 12360
e6d29d15 12361 gcc_unreachable ();
9997d19d 12362}
ff9940b0 12363
cce8749e
CH
12364/* Output the operands of a LDM/STM instruction to STREAM.
12365 MASK is the ARM register set mask of which only bits 0-15 are important.
6d3d9133 12366 REG is the base register, either the frame pointer or the stack pointer,
a15908a4
PB
12367 INSTR is the possibly suffixed load or store instruction.
12368 RFE is nonzero if the instruction should also copy spsr to cpsr. */
b279b20a 12369
d5b7b3ae 12370static void
b279b20a 12371print_multi_reg (FILE *stream, const char *instr, unsigned reg,
a15908a4 12372 unsigned long mask, int rfe)
cce8749e 12373{
b279b20a
NC
12374 unsigned i;
12375 bool not_first = FALSE;
cce8749e 12376
a15908a4 12377 gcc_assert (!rfe || (mask & (1 << PC_REGNUM)));
1d5473cb 12378 fputc ('\t', stream);
dd18ae56 12379 asm_fprintf (stream, instr, reg);
5b3e6663 12380 fputc ('{', stream);
f676971a 12381
d5b7b3ae 12382 for (i = 0; i <= LAST_ARM_REGNUM; i++)
cce8749e
CH
12383 if (mask & (1 << i))
12384 {
12385 if (not_first)
12386 fprintf (stream, ", ");
f676971a 12387
dd18ae56 12388 asm_fprintf (stream, "%r", i);
cce8749e
CH
12389 not_first = TRUE;
12390 }
f3bb6135 12391
a15908a4
PB
12392 if (rfe)
12393 fprintf (stream, "}^\n");
12394 else
12395 fprintf (stream, "}\n");
f3bb6135 12396}
cce8749e 12397
9b66ebb1 12398
8edfc4cc 12399/* Output a FLDMD instruction to STREAM.
9728c9d1
PB
12400 BASE if the register containing the address.
12401 REG and COUNT specify the register range.
8edfc4cc
MS
12402 Extra registers may be added to avoid hardware bugs.
12403
12404 We output FLDMD even for ARMv5 VFP implementations. Although
12405 FLDMD is technically not supported until ARMv6, it is believed
12406 that all VFP implementations support its use in this context. */
9b66ebb1
PB
12407
12408static void
8edfc4cc 12409vfp_output_fldmd (FILE * stream, unsigned int base, int reg, int count)
9b66ebb1
PB
12410{
12411 int i;
12412
9728c9d1
PB
12413 /* Workaround ARM10 VFPr1 bug. */
12414 if (count == 2 && !arm_arch6)
12415 {
12416 if (reg == 15)
12417 reg--;
12418 count++;
12419 }
12420
f1adb0a9
JB
12421 /* FLDMD may not load more than 16 doubleword registers at a time. Split the
12422 load into multiple parts if we have to handle more than 16 registers. */
12423 if (count > 16)
12424 {
12425 vfp_output_fldmd (stream, base, reg, 16);
12426 vfp_output_fldmd (stream, base, reg + 16, count - 16);
12427 return;
12428 }
12429
9b66ebb1 12430 fputc ('\t', stream);
8edfc4cc 12431 asm_fprintf (stream, "fldmfdd\t%r!, {", base);
9b66ebb1 12432
9728c9d1 12433 for (i = reg; i < reg + count; i++)
9b66ebb1 12434 {
9728c9d1 12435 if (i > reg)
9b66ebb1 12436 fputs (", ", stream);
9728c9d1 12437 asm_fprintf (stream, "d%d", i);
9b66ebb1
PB
12438 }
12439 fputs ("}\n", stream);
9728c9d1 12440
9b66ebb1
PB
12441}
12442
12443
12444/* Output the assembly for a store multiple. */
12445
12446const char *
8edfc4cc 12447vfp_output_fstmd (rtx * operands)
9b66ebb1
PB
12448{
12449 char pattern[100];
12450 int p;
12451 int base;
12452 int i;
12453
8edfc4cc 12454 strcpy (pattern, "fstmfdd\t%m0!, {%P1");
9b66ebb1
PB
12455 p = strlen (pattern);
12456
e6d29d15 12457 gcc_assert (GET_CODE (operands[1]) == REG);
9b66ebb1
PB
12458
12459 base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
12460 for (i = 1; i < XVECLEN (operands[2], 0); i++)
12461 {
12462 p += sprintf (&pattern[p], ", d%d", base + i);
12463 }
12464 strcpy (&pattern[p], "}");
12465
12466 output_asm_insn (pattern, operands);
12467 return "";
12468}
12469
12470
9728c9d1
PB
12471/* Emit RTL to save block of VFP register pairs to the stack. Returns the
12472 number of bytes pushed. */
9b66ebb1 12473
9728c9d1 12474static int
8edfc4cc 12475vfp_emit_fstmd (int base_reg, int count)
9b66ebb1
PB
12476{
12477 rtx par;
12478 rtx dwarf;
12479 rtx tmp, reg;
12480 int i;
12481
9728c9d1
PB
12482 /* Workaround ARM10 VFPr1 bug. Data corruption can occur when exactly two
12483 register pairs are stored by a store multiple insn. We avoid this
12484 by pushing an extra pair. */
12485 if (count == 2 && !arm_arch6)
12486 {
12487 if (base_reg == LAST_VFP_REGNUM - 3)
12488 base_reg -= 2;
12489 count++;
12490 }
12491
f1adb0a9
JB
12492 /* FSTMD may not store more than 16 doubleword registers at once. Split
12493 larger stores into multiple parts (up to a maximum of two, in
12494 practice). */
12495 if (count > 16)
12496 {
12497 int saved;
12498 /* NOTE: base_reg is an internal register number, so each D register
12499 counts as 2. */
12500 saved = vfp_emit_fstmd (base_reg + 32, count - 16);
12501 saved += vfp_emit_fstmd (base_reg, 16);
12502 return saved;
12503 }
12504
9b66ebb1
PB
12505 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
12506 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
12507
12508 reg = gen_rtx_REG (DFmode, base_reg);
12509 base_reg += 2;
12510
12511 XVECEXP (par, 0, 0)
12512 = gen_rtx_SET (VOIDmode,
9abf5d7b
RR
12513 gen_frame_mem
12514 (BLKmode,
12515 gen_rtx_PRE_MODIFY (Pmode,
12516 stack_pointer_rtx,
12517 plus_constant
12518 (stack_pointer_rtx,
12519 - (count * 8)))
12520 ),
9b66ebb1
PB
12521 gen_rtx_UNSPEC (BLKmode,
12522 gen_rtvec (1, reg),
12523 UNSPEC_PUSH_MULT));
12524
12525 tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8edfc4cc 12526 plus_constant (stack_pointer_rtx, -(count * 8)));
9b66ebb1
PB
12527 RTX_FRAME_RELATED_P (tmp) = 1;
12528 XVECEXP (dwarf, 0, 0) = tmp;
12529
12530 tmp = gen_rtx_SET (VOIDmode,
31fa16b6 12531 gen_frame_mem (DFmode, stack_pointer_rtx),
9b66ebb1
PB
12532 reg);
12533 RTX_FRAME_RELATED_P (tmp) = 1;
12534 XVECEXP (dwarf, 0, 1) = tmp;
12535
12536 for (i = 1; i < count; i++)
12537 {
12538 reg = gen_rtx_REG (DFmode, base_reg);
12539 base_reg += 2;
12540 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
12541
12542 tmp = gen_rtx_SET (VOIDmode,
31fa16b6 12543 gen_frame_mem (DFmode,
d66437c5
RE
12544 plus_constant (stack_pointer_rtx,
12545 i * 8)),
9b66ebb1
PB
12546 reg);
12547 RTX_FRAME_RELATED_P (tmp) = 1;
12548 XVECEXP (dwarf, 0, i + 1) = tmp;
12549 }
12550
12551 par = emit_insn (par);
bbbbb16a 12552 add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
9728c9d1
PB
12553 RTX_FRAME_RELATED_P (par) = 1;
12554
8edfc4cc 12555 return count * 8;
9b66ebb1
PB
12556}
12557
9403b7f7
RS
12558/* Emit a call instruction with pattern PAT. ADDR is the address of
12559 the call target. */
12560
12561void
12562arm_emit_call_insn (rtx pat, rtx addr)
12563{
12564 rtx insn;
12565
12566 insn = emit_call_insn (pat);
12567
12568 /* The PIC register is live on entry to VxWorks PIC PLT entries.
12569 If the call might use such an entry, add a use of the PIC register
12570 to the instruction's CALL_INSN_FUNCTION_USAGE. */
12571 if (TARGET_VXWORKS_RTP
12572 && flag_pic
12573 && GET_CODE (addr) == SYMBOL_REF
12574 && (SYMBOL_REF_DECL (addr)
12575 ? !targetm.binds_local_p (SYMBOL_REF_DECL (addr))
12576 : !SYMBOL_REF_LOCAL_P (addr)))
12577 {
12578 require_pic_register ();
12579 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), cfun->machine->pic_reg);
12580 }
12581}
9b66ebb1 12582
6354dc9b 12583/* Output a 'call' insn. */
cd2b33d0 12584const char *
e32bac5b 12585output_call (rtx *operands)
cce8749e 12586{
e6d29d15 12587 gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly. */
cce8749e 12588
68d560d4 12589 /* Handle calls to lr using ip (which may be clobbered in subr anyway). */
62b10bbc 12590 if (REGNO (operands[0]) == LR_REGNUM)
cce8749e 12591 {
62b10bbc 12592 operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
1d5473cb 12593 output_asm_insn ("mov%?\t%0, %|lr", operands);
cce8749e 12594 }
f676971a 12595
1d5473cb 12596 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
f676971a 12597
68d560d4 12598 if (TARGET_INTERWORK || arm_arch4t)
da6558fd
NC
12599 output_asm_insn ("bx%?\t%0", operands);
12600 else
12601 output_asm_insn ("mov%?\t%|pc, %0", operands);
f676971a 12602
f3bb6135
RE
12603 return "";
12604}
cce8749e 12605
0986ef45
JB
12606/* Output a 'call' insn that is a reference in memory. This is
12607 disabled for ARMv5 and we prefer a blx instead because otherwise
12608 there's a significant performance overhead. */
cd2b33d0 12609const char *
e32bac5b 12610output_call_mem (rtx *operands)
ff9940b0 12611{
0986ef45
JB
12612 gcc_assert (!arm_arch5);
12613 if (TARGET_INTERWORK)
da6558fd
NC
12614 {
12615 output_asm_insn ("ldr%?\t%|ip, %0", operands);
12616 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
12617 output_asm_insn ("bx%?\t%|ip", operands);
12618 }
6ab5da80
RE
12619 else if (regno_use_in (LR_REGNUM, operands[0]))
12620 {
12621 /* LR is used in the memory address. We load the address in the
12622 first instruction. It's safe to use IP as the target of the
12623 load since the call will kill it anyway. */
12624 output_asm_insn ("ldr%?\t%|ip, %0", operands);
0986ef45
JB
12625 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
12626 if (arm_arch4t)
12627 output_asm_insn ("bx%?\t%|ip", operands);
68d560d4 12628 else
0986ef45 12629 output_asm_insn ("mov%?\t%|pc, %|ip", operands);
6ab5da80 12630 }
da6558fd
NC
12631 else
12632 {
12633 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
12634 output_asm_insn ("ldr%?\t%|pc, %0", operands);
12635 }
12636
f3bb6135
RE
12637 return "";
12638}
ff9940b0
RE
12639
12640
3b684012
RE
12641/* Output a move from arm registers to an fpa registers.
12642 OPERANDS[0] is an fpa register.
ff9940b0 12643 OPERANDS[1] is the first registers of an arm register pair. */
cd2b33d0 12644const char *
e32bac5b 12645output_mov_long_double_fpa_from_arm (rtx *operands)
ff9940b0
RE
12646{
12647 int arm_reg0 = REGNO (operands[1]);
12648 rtx ops[3];
12649
e6d29d15 12650 gcc_assert (arm_reg0 != IP_REGNUM);
f3bb6135 12651
43cffd11
RE
12652 ops[0] = gen_rtx_REG (SImode, arm_reg0);
12653 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
12654 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
f676971a 12655
5b3e6663 12656 output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
1d5473cb 12657 output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
f676971a 12658
f3bb6135
RE
12659 return "";
12660}
ff9940b0 12661
3b684012 12662/* Output a move from an fpa register to arm registers.
ff9940b0 12663 OPERANDS[0] is the first registers of an arm register pair.
3b684012 12664 OPERANDS[1] is an fpa register. */
cd2b33d0 12665const char *
e32bac5b 12666output_mov_long_double_arm_from_fpa (rtx *operands)
ff9940b0
RE
12667{
12668 int arm_reg0 = REGNO (operands[0]);
12669 rtx ops[3];
12670
e6d29d15 12671 gcc_assert (arm_reg0 != IP_REGNUM);
f3bb6135 12672
43cffd11
RE
12673 ops[0] = gen_rtx_REG (SImode, arm_reg0);
12674 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
12675 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
ff9940b0 12676
1d5473cb 12677 output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
5b3e6663 12678 output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
f3bb6135
RE
12679 return "";
12680}
ff9940b0
RE
12681
12682/* Output a move from arm registers to arm registers of a long double
12683 OPERANDS[0] is the destination.
12684 OPERANDS[1] is the source. */
cd2b33d0 12685const char *
e32bac5b 12686output_mov_long_double_arm_from_arm (rtx *operands)
ff9940b0 12687{
6354dc9b 12688 /* We have to be careful here because the two might overlap. */
ff9940b0
RE
12689 int dest_start = REGNO (operands[0]);
12690 int src_start = REGNO (operands[1]);
12691 rtx ops[2];
12692 int i;
12693
12694 if (dest_start < src_start)
12695 {
12696 for (i = 0; i < 3; i++)
12697 {
43cffd11
RE
12698 ops[0] = gen_rtx_REG (SImode, dest_start + i);
12699 ops[1] = gen_rtx_REG (SImode, src_start + i);
9997d19d 12700 output_asm_insn ("mov%?\t%0, %1", ops);
ff9940b0
RE
12701 }
12702 }
12703 else
12704 {
12705 for (i = 2; i >= 0; i--)
12706 {
43cffd11
RE
12707 ops[0] = gen_rtx_REG (SImode, dest_start + i);
12708 ops[1] = gen_rtx_REG (SImode, src_start + i);
9997d19d 12709 output_asm_insn ("mov%?\t%0, %1", ops);
ff9940b0
RE
12710 }
12711 }
f3bb6135 12712
ff9940b0
RE
12713 return "";
12714}
12715
a552b644
RR
12716void
12717arm_emit_movpair (rtx dest, rtx src)
12718 {
12719 /* If the src is an immediate, simplify it. */
12720 if (CONST_INT_P (src))
12721 {
12722 HOST_WIDE_INT val = INTVAL (src);
12723 emit_set_insn (dest, GEN_INT (val & 0x0000ffff));
12724 if ((val >> 16) & 0x0000ffff)
12725 emit_set_insn (gen_rtx_ZERO_EXTRACT (SImode, dest, GEN_INT (16),
12726 GEN_INT (16)),
12727 GEN_INT ((val >> 16) & 0x0000ffff));
12728 return;
12729 }
12730 emit_set_insn (dest, gen_rtx_HIGH (SImode, src));
12731 emit_set_insn (dest, gen_rtx_LO_SUM (SImode, dest, src));
12732 }
571191af 12733
3b684012
RE
12734/* Output a move from arm registers to an fpa registers.
12735 OPERANDS[0] is an fpa register.
cce8749e 12736 OPERANDS[1] is the first registers of an arm register pair. */
cd2b33d0 12737const char *
e32bac5b 12738output_mov_double_fpa_from_arm (rtx *operands)
cce8749e
CH
12739{
12740 int arm_reg0 = REGNO (operands[1]);
12741 rtx ops[2];
12742
e6d29d15 12743 gcc_assert (arm_reg0 != IP_REGNUM);
f676971a 12744
43cffd11
RE
12745 ops[0] = gen_rtx_REG (SImode, arm_reg0);
12746 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
5b3e6663 12747 output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1}", ops);
1d5473cb 12748 output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
f3bb6135
RE
12749 return "";
12750}
cce8749e 12751
3b684012 12752/* Output a move from an fpa register to arm registers.
cce8749e 12753 OPERANDS[0] is the first registers of an arm register pair.
3b684012 12754 OPERANDS[1] is an fpa register. */
cd2b33d0 12755const char *
e32bac5b 12756output_mov_double_arm_from_fpa (rtx *operands)
cce8749e
CH
12757{
12758 int arm_reg0 = REGNO (operands[0]);
12759 rtx ops[2];
12760
e6d29d15 12761 gcc_assert (arm_reg0 != IP_REGNUM);
f3bb6135 12762
43cffd11
RE
12763 ops[0] = gen_rtx_REG (SImode, arm_reg0);
12764 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
1d5473cb 12765 output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
5b3e6663 12766 output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1}", ops);
f3bb6135
RE
12767 return "";
12768}
cce8749e
CH
12769
12770/* Output a move between double words.
12771 It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
12772 or MEM<-REG and all MEMs must be offsettable addresses. */
cd2b33d0 12773const char *
e32bac5b 12774output_move_double (rtx *operands)
cce8749e
CH
12775{
12776 enum rtx_code code0 = GET_CODE (operands[0]);
12777 enum rtx_code code1 = GET_CODE (operands[1]);
56636818 12778 rtx otherops[3];
cce8749e
CH
12779
12780 if (code0 == REG)
12781 {
f0b4bdd5 12782 unsigned int reg0 = REGNO (operands[0]);
cce8749e 12783
43cffd11 12784 otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
f676971a 12785
e6d29d15
NS
12786 gcc_assert (code1 == MEM); /* Constraints should ensure this. */
12787
12788 switch (GET_CODE (XEXP (operands[1], 0)))
cce8749e 12789 {
e6d29d15 12790 case REG:
5fd42423
PB
12791 if (TARGET_LDRD
12792 && !(fix_cm3_ldrd && reg0 == REGNO(XEXP (operands[1], 0))))
5dea0c19
PB
12793 output_asm_insn ("ldr%(d%)\t%0, [%m1]", operands);
12794 else
12795 output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
e6d29d15 12796 break;
e0b92319 12797
e6d29d15
NS
12798 case PRE_INC:
12799 gcc_assert (TARGET_LDRD);
5b3e6663 12800 output_asm_insn ("ldr%(d%)\t%0, [%m1, #8]!", operands);
e6d29d15 12801 break;
e0b92319 12802
e6d29d15 12803 case PRE_DEC:
5b3e6663
PB
12804 if (TARGET_LDRD)
12805 output_asm_insn ("ldr%(d%)\t%0, [%m1, #-8]!", operands);
12806 else
12807 output_asm_insn ("ldm%(db%)\t%m1!, %M0", operands);
e6d29d15 12808 break;
e0b92319 12809
e6d29d15 12810 case POST_INC:
5dea0c19
PB
12811 if (TARGET_LDRD)
12812 output_asm_insn ("ldr%(d%)\t%0, [%m1], #8", operands);
12813 else
12814 output_asm_insn ("ldm%(ia%)\t%m1!, %M0", operands);
e6d29d15 12815 break;
e0b92319 12816
e6d29d15
NS
12817 case POST_DEC:
12818 gcc_assert (TARGET_LDRD);
5b3e6663 12819 output_asm_insn ("ldr%(d%)\t%0, [%m1], #-8", operands);
e6d29d15 12820 break;
e0b92319 12821
e6d29d15
NS
12822 case PRE_MODIFY:
12823 case POST_MODIFY:
5fd42423
PB
12824 /* Autoicrement addressing modes should never have overlapping
12825 base and destination registers, and overlapping index registers
12826 are already prohibited, so this doesn't need to worry about
12827 fix_cm3_ldrd. */
e6d29d15
NS
12828 otherops[0] = operands[0];
12829 otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
12830 otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
e0b92319 12831
e6d29d15 12832 if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
cce8749e 12833 {
e6d29d15 12834 if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
fdd695fd 12835 {
e6d29d15
NS
12836 /* Registers overlap so split out the increment. */
12837 output_asm_insn ("add%?\t%1, %1, %2", otherops);
5b3e6663 12838 output_asm_insn ("ldr%(d%)\t%0, [%1] @split", otherops);
fdd695fd
PB
12839 }
12840 else
fe2d934b 12841 {
ff128632
RE
12842 /* Use a single insn if we can.
12843 FIXME: IWMMXT allows offsets larger than ldrd can
12844 handle, fix these up with a pair of ldr. */
12845 if (TARGET_THUMB2
12846 || GET_CODE (otherops[2]) != CONST_INT
12847 || (INTVAL (otherops[2]) > -256
12848 && INTVAL (otherops[2]) < 256))
12849 output_asm_insn ("ldr%(d%)\t%0, [%1, %2]!", otherops);
12850 else
fe2d934b
PB
12851 {
12852 output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
ff128632 12853 output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
fe2d934b 12854 }
fe2d934b 12855 }
e6d29d15
NS
12856 }
12857 else
12858 {
ff128632
RE
12859 /* Use a single insn if we can.
12860 FIXME: IWMMXT allows offsets larger than ldrd can handle,
fe2d934b 12861 fix these up with a pair of ldr. */
ff128632
RE
12862 if (TARGET_THUMB2
12863 || GET_CODE (otherops[2]) != CONST_INT
12864 || (INTVAL (otherops[2]) > -256
12865 && INTVAL (otherops[2]) < 256))
12866 output_asm_insn ("ldr%(d%)\t%0, [%1], %2", otherops);
12867 else
fe2d934b 12868 {
ff128632 12869 output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
fe2d934b
PB
12870 output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
12871 }
e6d29d15
NS
12872 }
12873 break;
e0b92319 12874
e6d29d15
NS
12875 case LABEL_REF:
12876 case CONST:
5dea0c19
PB
12877 /* We might be able to use ldrd %0, %1 here. However the range is
12878 different to ldr/adr, and it is broken on some ARMv7-M
12879 implementations. */
5fd42423
PB
12880 /* Use the second register of the pair to avoid problematic
12881 overlap. */
12882 otherops[1] = operands[1];
12883 output_asm_insn ("adr%?\t%0, %1", otherops);
12884 operands[1] = otherops[0];
5dea0c19 12885 if (TARGET_LDRD)
5fd42423 12886 output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
5dea0c19 12887 else
5fd42423 12888 output_asm_insn ("ldm%(ia%)\t%1, %M0", operands);
e6d29d15 12889 break;
e0b92319 12890
5b3e6663 12891 /* ??? This needs checking for thumb2. */
e6d29d15
NS
12892 default:
12893 if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
12894 GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
12895 {
12896 otherops[0] = operands[0];
12897 otherops[1] = XEXP (XEXP (operands[1], 0), 0);
12898 otherops[2] = XEXP (XEXP (operands[1], 0), 1);
e0b92319 12899
e6d29d15 12900 if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
fdd695fd 12901 {
5dea0c19 12902 if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
2b835d68 12903 {
e6d29d15 12904 switch ((int) INTVAL (otherops[2]))
2b835d68 12905 {
e6d29d15 12906 case -8:
5b3e6663 12907 output_asm_insn ("ldm%(db%)\t%1, %M0", otherops);
e6d29d15
NS
12908 return "";
12909 case -4:
5b3e6663
PB
12910 if (TARGET_THUMB2)
12911 break;
12912 output_asm_insn ("ldm%(da%)\t%1, %M0", otherops);
e6d29d15
NS
12913 return "";
12914 case 4:
5b3e6663
PB
12915 if (TARGET_THUMB2)
12916 break;
12917 output_asm_insn ("ldm%(ib%)\t%1, %M0", otherops);
e6d29d15 12918 return "";
fdd695fd 12919 }
e6d29d15 12920 }
5fd42423
PB
12921 otherops[0] = gen_rtx_REG(SImode, REGNO(operands[0]) + 1);
12922 operands[1] = otherops[0];
e6d29d15
NS
12923 if (TARGET_LDRD
12924 && (GET_CODE (otherops[2]) == REG
ff128632 12925 || TARGET_THUMB2
e6d29d15
NS
12926 || (GET_CODE (otherops[2]) == CONST_INT
12927 && INTVAL (otherops[2]) > -256
12928 && INTVAL (otherops[2]) < 256)))
12929 {
5fd42423 12930 if (reg_overlap_mentioned_p (operands[0],
e6d29d15 12931 otherops[2]))
fdd695fd 12932 {
5fd42423 12933 rtx tmp;
e6d29d15
NS
12934 /* Swap base and index registers over to
12935 avoid a conflict. */
5fd42423
PB
12936 tmp = otherops[1];
12937 otherops[1] = otherops[2];
12938 otherops[2] = tmp;
fdd695fd 12939 }
e6d29d15
NS
12940 /* If both registers conflict, it will usually
12941 have been fixed by a splitter. */
5fd42423
PB
12942 if (reg_overlap_mentioned_p (operands[0], otherops[2])
12943 || (fix_cm3_ldrd && reg0 == REGNO (otherops[1])))
fdd695fd 12944 {
5fd42423
PB
12945 output_asm_insn ("add%?\t%0, %1, %2", otherops);
12946 output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
2b835d68
RE
12947 }
12948 else
5fd42423
PB
12949 {
12950 otherops[0] = operands[0];
12951 output_asm_insn ("ldr%(d%)\t%0, [%1, %2]", otherops);
12952 }
e6d29d15 12953 return "";
2b835d68 12954 }
e0b92319 12955
e6d29d15 12956 if (GET_CODE (otherops[2]) == CONST_INT)
2b835d68 12957 {
e6d29d15
NS
12958 if (!(const_ok_for_arm (INTVAL (otherops[2]))))
12959 output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
12960 else
12961 output_asm_insn ("add%?\t%0, %1, %2", otherops);
2b835d68
RE
12962 }
12963 else
e6d29d15
NS
12964 output_asm_insn ("add%?\t%0, %1, %2", otherops);
12965 }
12966 else
12967 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
12968
5dea0c19 12969 if (TARGET_LDRD)
5fd42423 12970 return "ldr%(d%)\t%0, [%1]";
5dea0c19 12971
5fd42423 12972 return "ldm%(ia%)\t%1, %M0";
e6d29d15
NS
12973 }
12974 else
12975 {
12976 otherops[1] = adjust_address (operands[1], SImode, 4);
12977 /* Take care of overlapping base/data reg. */
12978 if (reg_mentioned_p (operands[0], operands[1]))
12979 {
12980 output_asm_insn ("ldr%?\t%0, %1", otherops);
12981 output_asm_insn ("ldr%?\t%0, %1", operands);
12982 }
12983 else
12984 {
12985 output_asm_insn ("ldr%?\t%0, %1", operands);
12986 output_asm_insn ("ldr%?\t%0, %1", otherops);
cce8749e
CH
12987 }
12988 }
12989 }
cce8749e 12990 }
e6d29d15 12991 else
cce8749e 12992 {
e6d29d15
NS
12993 /* Constraints should ensure this. */
12994 gcc_assert (code0 == MEM && code1 == REG);
12995 gcc_assert (REGNO (operands[1]) != IP_REGNUM);
2b835d68 12996
ff9940b0
RE
12997 switch (GET_CODE (XEXP (operands[0], 0)))
12998 {
12999 case REG:
5dea0c19
PB
13000 if (TARGET_LDRD)
13001 output_asm_insn ("str%(d%)\t%1, [%m0]", operands);
13002 else
13003 output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
ff9940b0 13004 break;
2b835d68 13005
ff9940b0 13006 case PRE_INC:
e6d29d15 13007 gcc_assert (TARGET_LDRD);
5b3e6663 13008 output_asm_insn ("str%(d%)\t%1, [%m0, #8]!", operands);
ff9940b0 13009 break;
2b835d68 13010
ff9940b0 13011 case PRE_DEC:
5b3e6663
PB
13012 if (TARGET_LDRD)
13013 output_asm_insn ("str%(d%)\t%1, [%m0, #-8]!", operands);
13014 else
13015 output_asm_insn ("stm%(db%)\t%m0!, %M1", operands);
ff9940b0 13016 break;
2b835d68 13017
ff9940b0 13018 case POST_INC:
5dea0c19
PB
13019 if (TARGET_LDRD)
13020 output_asm_insn ("str%(d%)\t%1, [%m0], #8", operands);
13021 else
13022 output_asm_insn ("stm%(ia%)\t%m0!, %M1", operands);
ff9940b0 13023 break;
2b835d68 13024
ff9940b0 13025 case POST_DEC:
e6d29d15 13026 gcc_assert (TARGET_LDRD);
5b3e6663 13027 output_asm_insn ("str%(d%)\t%1, [%m0], #-8", operands);
fdd695fd
PB
13028 break;
13029
13030 case PRE_MODIFY:
13031 case POST_MODIFY:
13032 otherops[0] = operands[1];
13033 otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
13034 otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
13035
fe2d934b
PB
13036 /* IWMMXT allows offsets larger than ldrd can handle,
13037 fix these up with a pair of ldr. */
ff128632
RE
13038 if (!TARGET_THUMB2
13039 && GET_CODE (otherops[2]) == CONST_INT
fe2d934b
PB
13040 && (INTVAL(otherops[2]) <= -256
13041 || INTVAL(otherops[2]) >= 256))
13042 {
fe2d934b
PB
13043 if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
13044 {
8019fcfb
YQ
13045 output_asm_insn ("str%?\t%0, [%1, %2]!", otherops);
13046 output_asm_insn ("str%?\t%H0, [%1, #4]", otherops);
fe2d934b
PB
13047 }
13048 else
13049 {
8019fcfb
YQ
13050 output_asm_insn ("str%?\t%H0, [%1, #4]", otherops);
13051 output_asm_insn ("str%?\t%0, [%1], %2", otherops);
fe2d934b
PB
13052 }
13053 }
13054 else if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
5b3e6663 13055 output_asm_insn ("str%(d%)\t%0, [%1, %2]!", otherops);
fdd695fd 13056 else
5b3e6663 13057 output_asm_insn ("str%(d%)\t%0, [%1], %2", otherops);
ff9940b0 13058 break;
2b835d68
RE
13059
13060 case PLUS:
fdd695fd 13061 otherops[2] = XEXP (XEXP (operands[0], 0), 1);
5dea0c19 13062 if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
2b835d68 13063 {
06bea5aa 13064 switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
2b835d68
RE
13065 {
13066 case -8:
5b3e6663 13067 output_asm_insn ("stm%(db%)\t%m0, %M1", operands);
2b835d68
RE
13068 return "";
13069
13070 case -4:
5b3e6663
PB
13071 if (TARGET_THUMB2)
13072 break;
13073 output_asm_insn ("stm%(da%)\t%m0, %M1", operands);
2b835d68
RE
13074 return "";
13075
13076 case 4:
5b3e6663
PB
13077 if (TARGET_THUMB2)
13078 break;
13079 output_asm_insn ("stm%(ib%)\t%m0, %M1", operands);
2b835d68
RE
13080 return "";
13081 }
13082 }
fdd695fd
PB
13083 if (TARGET_LDRD
13084 && (GET_CODE (otherops[2]) == REG
ff128632 13085 || TARGET_THUMB2
fdd695fd
PB
13086 || (GET_CODE (otherops[2]) == CONST_INT
13087 && INTVAL (otherops[2]) > -256
13088 && INTVAL (otherops[2]) < 256)))
13089 {
13090 otherops[0] = operands[1];
13091 otherops[1] = XEXP (XEXP (operands[0], 0), 0);
5b3e6663 13092 output_asm_insn ("str%(d%)\t%0, [%1, %2]", otherops);
fdd695fd
PB
13093 return "";
13094 }
2b835d68
RE
13095 /* Fall through */
13096
ff9940b0 13097 default:
a4a37b30 13098 otherops[0] = adjust_address (operands[0], SImode, 4);
ff128632 13099 otherops[1] = operands[1];
9997d19d 13100 output_asm_insn ("str%?\t%1, %0", operands);
ff128632 13101 output_asm_insn ("str%?\t%H1, %0", otherops);
cce8749e
CH
13102 }
13103 }
cce8749e 13104
9997d19d
RE
13105 return "";
13106}
cce8749e 13107
88f77cba 13108/* Output a move, load or store for quad-word vectors in ARM registers. Only
dc34db56 13109 handles MEMs accepted by neon_vector_mem_operand with TYPE=1. */
5b3e6663
PB
13110
13111const char *
88f77cba 13112output_move_quad (rtx *operands)
5b3e6663 13113{
88f77cba
JB
13114 if (REG_P (operands[0]))
13115 {
13116 /* Load, or reg->reg move. */
5b3e6663 13117
88f77cba
JB
13118 if (MEM_P (operands[1]))
13119 {
13120 switch (GET_CODE (XEXP (operands[1], 0)))
13121 {
13122 case REG:
13123 output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
13124 break;
13125
13126 case LABEL_REF:
13127 case CONST:
13128 output_asm_insn ("adr%?\t%0, %1", operands);
13129 output_asm_insn ("ldm%(ia%)\t%0, %M0", operands);
13130 break;
13131
13132 default:
13133 gcc_unreachable ();
13134 }
13135 }
13136 else
13137 {
13138 rtx ops[2];
13139 int dest, src, i;
5b3e6663 13140
88f77cba 13141 gcc_assert (REG_P (operands[1]));
5b3e6663 13142
88f77cba
JB
13143 dest = REGNO (operands[0]);
13144 src = REGNO (operands[1]);
5b3e6663 13145
88f77cba
JB
13146 /* This seems pretty dumb, but hopefully GCC won't try to do it
13147 very often. */
13148 if (dest < src)
13149 for (i = 0; i < 4; i++)
13150 {
13151 ops[0] = gen_rtx_REG (SImode, dest + i);
13152 ops[1] = gen_rtx_REG (SImode, src + i);
13153 output_asm_insn ("mov%?\t%0, %1", ops);
13154 }
13155 else
13156 for (i = 3; i >= 0; i--)
13157 {
13158 ops[0] = gen_rtx_REG (SImode, dest + i);
13159 ops[1] = gen_rtx_REG (SImode, src + i);
13160 output_asm_insn ("mov%?\t%0, %1", ops);
13161 }
13162 }
13163 }
13164 else
13165 {
13166 gcc_assert (MEM_P (operands[0]));
13167 gcc_assert (REG_P (operands[1]));
13168 gcc_assert (!reg_overlap_mentioned_p (operands[1], operands[0]));
13169
13170 switch (GET_CODE (XEXP (operands[0], 0)))
13171 {
13172 case REG:
13173 output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
13174 break;
13175
13176 default:
13177 gcc_unreachable ();
13178 }
13179 }
13180
13181 return "";
13182}
13183
13184/* Output a VFP load or store instruction. */
13185
13186const char *
13187output_move_vfp (rtx *operands)
13188{
13189 rtx reg, mem, addr, ops[2];
13190 int load = REG_P (operands[0]);
13191 int dp = GET_MODE_SIZE (GET_MODE (operands[0])) == 8;
13192 int integer_p = GET_MODE_CLASS (GET_MODE (operands[0])) == MODE_INT;
0a2aaacc 13193 const char *templ;
88f77cba
JB
13194 char buff[50];
13195 enum machine_mode mode;
13196
13197 reg = operands[!load];
13198 mem = operands[load];
13199
13200 mode = GET_MODE (reg);
13201
13202 gcc_assert (REG_P (reg));
13203 gcc_assert (IS_VFP_REGNUM (REGNO (reg)));
13204 gcc_assert (mode == SFmode
13205 || mode == DFmode
13206 || mode == SImode
13207 || mode == DImode
13208 || (TARGET_NEON && VALID_NEON_DREG_MODE (mode)));
13209 gcc_assert (MEM_P (mem));
13210
13211 addr = XEXP (mem, 0);
13212
13213 switch (GET_CODE (addr))
13214 {
13215 case PRE_DEC:
0a2aaacc 13216 templ = "f%smdb%c%%?\t%%0!, {%%%s1}%s";
88f77cba
JB
13217 ops[0] = XEXP (addr, 0);
13218 ops[1] = reg;
5b3e6663
PB
13219 break;
13220
13221 case POST_INC:
0a2aaacc 13222 templ = "f%smia%c%%?\t%%0!, {%%%s1}%s";
5b3e6663
PB
13223 ops[0] = XEXP (addr, 0);
13224 ops[1] = reg;
13225 break;
13226
13227 default:
0a2aaacc 13228 templ = "f%s%c%%?\t%%%s0, %%1%s";
5b3e6663
PB
13229 ops[0] = reg;
13230 ops[1] = mem;
13231 break;
13232 }
13233
0a2aaacc 13234 sprintf (buff, templ,
5b3e6663
PB
13235 load ? "ld" : "st",
13236 dp ? 'd' : 's',
13237 dp ? "P" : "",
13238 integer_p ? "\t%@ int" : "");
13239 output_asm_insn (buff, ops);
13240
13241 return "";
13242}
13243
88f77cba 13244/* Output a Neon quad-word load or store, or a load or store for
874d42b9 13245 larger structure modes.
88f77cba 13246
874d42b9
JM
13247 WARNING: The ordering of elements is weird in big-endian mode,
13248 because we use VSTM, as required by the EABI. GCC RTL defines
13249 element ordering based on in-memory order. This can be differ
13250 from the architectural ordering of elements within a NEON register.
13251 The intrinsics defined in arm_neon.h use the NEON register element
13252 ordering, not the GCC RTL element ordering.
88f77cba 13253
874d42b9
JM
13254 For example, the in-memory ordering of a big-endian a quadword
13255 vector with 16-bit elements when stored from register pair {d0,d1}
13256 will be (lowest address first, d0[N] is NEON register element N):
88f77cba 13257
874d42b9 13258 [d0[3], d0[2], d0[1], d0[0], d1[7], d1[6], d1[5], d1[4]]
88f77cba 13259
874d42b9
JM
13260 When necessary, quadword registers (dN, dN+1) are moved to ARM
13261 registers from rN in the order:
88f77cba
JB
13262
13263 dN -> (rN+1, rN), dN+1 -> (rN+3, rN+2)
13264
874d42b9
JM
13265 So that STM/LDM can be used on vectors in ARM registers, and the
13266 same memory layout will result as if VSTM/VLDM were used. */
88f77cba
JB
13267
13268const char *
13269output_move_neon (rtx *operands)
13270{
13271 rtx reg, mem, addr, ops[2];
13272 int regno, load = REG_P (operands[0]);
0a2aaacc 13273 const char *templ;
88f77cba
JB
13274 char buff[50];
13275 enum machine_mode mode;
13276
13277 reg = operands[!load];
13278 mem = operands[load];
13279
13280 mode = GET_MODE (reg);
13281
13282 gcc_assert (REG_P (reg));
13283 regno = REGNO (reg);
13284 gcc_assert (VFP_REGNO_OK_FOR_DOUBLE (regno)
13285 || NEON_REGNO_OK_FOR_QUAD (regno));
13286 gcc_assert (VALID_NEON_DREG_MODE (mode)
13287 || VALID_NEON_QREG_MODE (mode)
13288 || VALID_NEON_STRUCT_MODE (mode));
13289 gcc_assert (MEM_P (mem));
13290
13291 addr = XEXP (mem, 0);
13292
13293 /* Strip off const from addresses like (const (plus (...))). */
13294 if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
13295 addr = XEXP (addr, 0);
13296
13297 switch (GET_CODE (addr))
13298 {
13299 case POST_INC:
0a2aaacc 13300 templ = "v%smia%%?\t%%0!, %%h1";
88f77cba
JB
13301 ops[0] = XEXP (addr, 0);
13302 ops[1] = reg;
13303 break;
13304
dc34db56
PB
13305 case PRE_DEC:
13306 /* FIXME: We should be using vld1/vst1 here in BE mode? */
13307 templ = "v%smdb%%?\t%%0!, %%h1";
13308 ops[0] = XEXP (addr, 0);
13309 ops[1] = reg;
13310 break;
13311
88f77cba
JB
13312 case POST_MODIFY:
13313 /* FIXME: Not currently enabled in neon_vector_mem_operand. */
13314 gcc_unreachable ();
13315
13316 case LABEL_REF:
13317 case PLUS:
13318 {
13319 int nregs = HARD_REGNO_NREGS (REGNO (reg), mode) / 2;
13320 int i;
13321 int overlap = -1;
13322 for (i = 0; i < nregs; i++)
13323 {
13324 /* We're only using DImode here because it's a convenient size. */
13325 ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * i);
5728868b 13326 ops[1] = adjust_address (mem, DImode, 8 * i);
88f77cba
JB
13327 if (reg_overlap_mentioned_p (ops[0], mem))
13328 {
13329 gcc_assert (overlap == -1);
13330 overlap = i;
13331 }
13332 else
13333 {
13334 sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
13335 output_asm_insn (buff, ops);
13336 }
13337 }
13338 if (overlap != -1)
13339 {
13340 ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * overlap);
13341 ops[1] = adjust_address (mem, SImode, 8 * overlap);
13342 sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
13343 output_asm_insn (buff, ops);
13344 }
13345
13346 return "";
13347 }
13348
13349 default:
0a2aaacc 13350 templ = "v%smia%%?\t%%m0, %%h1";
88f77cba
JB
13351 ops[0] = mem;
13352 ops[1] = reg;
13353 }
13354
0a2aaacc 13355 sprintf (buff, templ, load ? "ld" : "st");
88f77cba
JB
13356 output_asm_insn (buff, ops);
13357
13358 return "";
13359}
13360
7c4f0041
JZ
13361/* Compute and return the length of neon_mov<mode>, where <mode> is
13362 one of VSTRUCT modes: EI, OI, CI or XI. */
13363int
13364arm_attr_length_move_neon (rtx insn)
13365{
13366 rtx reg, mem, addr;
e4dde839 13367 int load;
7c4f0041
JZ
13368 enum machine_mode mode;
13369
13370 extract_insn_cached (insn);
13371
13372 if (REG_P (recog_data.operand[0]) && REG_P (recog_data.operand[1]))
13373 {
13374 mode = GET_MODE (recog_data.operand[0]);
13375 switch (mode)
13376 {
13377 case EImode:
13378 case OImode:
13379 return 8;
13380 case CImode:
13381 return 12;
13382 case XImode:
13383 return 16;
13384 default:
13385 gcc_unreachable ();
13386 }
13387 }
13388
13389 load = REG_P (recog_data.operand[0]);
13390 reg = recog_data.operand[!load];
13391 mem = recog_data.operand[load];
13392
13393 gcc_assert (MEM_P (mem));
13394
13395 mode = GET_MODE (reg);
7c4f0041
JZ
13396 addr = XEXP (mem, 0);
13397
13398 /* Strip off const from addresses like (const (plus (...))). */
13399 if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
13400 addr = XEXP (addr, 0);
13401
13402 if (GET_CODE (addr) == LABEL_REF || GET_CODE (addr) == PLUS)
13403 {
13404 int insns = HARD_REGNO_NREGS (REGNO (reg), mode) / 2;
13405 return insns * 4;
13406 }
13407 else
13408 return 4;
13409}
13410
47d8f18d
JZ
13411/* Return nonzero if the offset in the address is an immediate. Otherwise,
13412 return zero. */
13413
13414int
13415arm_address_offset_is_imm (rtx insn)
13416{
13417 rtx mem, addr;
13418
13419 extract_insn_cached (insn);
13420
13421 if (REG_P (recog_data.operand[0]))
13422 return 0;
13423
13424 mem = recog_data.operand[0];
13425
13426 gcc_assert (MEM_P (mem));
13427
13428 addr = XEXP (mem, 0);
13429
13430 if (GET_CODE (addr) == REG
13431 || (GET_CODE (addr) == PLUS
13432 && GET_CODE (XEXP (addr, 0)) == REG
13433 && GET_CODE (XEXP (addr, 1)) == CONST_INT))
13434 return 1;
13435 else
13436 return 0;
13437}
13438
1d6e90ac
NC
13439/* Output an ADD r, s, #n where n may be too big for one instruction.
13440 If adding zero to one register, output nothing. */
cd2b33d0 13441const char *
e32bac5b 13442output_add_immediate (rtx *operands)
cce8749e 13443{
f3bb6135 13444 HOST_WIDE_INT n = INTVAL (operands[2]);
cce8749e
CH
13445
13446 if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
13447 {
13448 if (n < 0)
13449 output_multi_immediate (operands,
9997d19d
RE
13450 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
13451 -n);
cce8749e
CH
13452 else
13453 output_multi_immediate (operands,
9997d19d
RE
13454 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
13455 n);
cce8749e 13456 }
f3bb6135
RE
13457
13458 return "";
13459}
cce8749e 13460
cce8749e
CH
13461/* Output a multiple immediate operation.
13462 OPERANDS is the vector of operands referred to in the output patterns.
13463 INSTR1 is the output pattern to use for the first constant.
13464 INSTR2 is the output pattern to use for subsequent constants.
13465 IMMED_OP is the index of the constant slot in OPERANDS.
13466 N is the constant value. */
cd2b33d0 13467static const char *
e32bac5b
RE
13468output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
13469 int immed_op, HOST_WIDE_INT n)
cce8749e 13470{
f3bb6135 13471#if HOST_BITS_PER_WIDE_INT > 32
30cf4896 13472 n &= 0xffffffff;
f3bb6135
RE
13473#endif
13474
cce8749e
CH
13475 if (n == 0)
13476 {
1d6e90ac 13477 /* Quick and easy output. */
cce8749e 13478 operands[immed_op] = const0_rtx;
1d6e90ac 13479 output_asm_insn (instr1, operands);
cce8749e
CH
13480 }
13481 else
13482 {
13483 int i;
cd2b33d0 13484 const char * instr = instr1;
cce8749e 13485
6354dc9b 13486 /* Note that n is never zero here (which would give no output). */
cce8749e
CH
13487 for (i = 0; i < 32; i += 2)
13488 {
13489 if (n & (3 << i))
13490 {
f3bb6135
RE
13491 operands[immed_op] = GEN_INT (n & (255 << i));
13492 output_asm_insn (instr, operands);
cce8749e
CH
13493 instr = instr2;
13494 i += 6;
13495 }
13496 }
13497 }
f676971a 13498
f3bb6135 13499 return "";
9997d19d 13500}
cce8749e 13501
5b3e6663
PB
13502/* Return the name of a shifter operation. */
13503static const char *
13504arm_shift_nmem(enum rtx_code code)
13505{
13506 switch (code)
13507 {
13508 case ASHIFT:
13509 return ARM_LSL_NAME;
13510
13511 case ASHIFTRT:
13512 return "asr";
13513
13514 case LSHIFTRT:
13515 return "lsr";
13516
13517 case ROTATERT:
13518 return "ror";
13519
13520 default:
13521 abort();
13522 }
13523}
13524
cce8749e
CH
13525/* Return the appropriate ARM instruction for the operation code.
13526 The returned result should not be overwritten. OP is the rtx of the
13527 operation. SHIFT_FIRST_ARG is TRUE if the first argument of the operator
13528 was shifted. */
cd2b33d0 13529const char *
e32bac5b 13530arithmetic_instr (rtx op, int shift_first_arg)
cce8749e 13531{
9997d19d 13532 switch (GET_CODE (op))
cce8749e
CH
13533 {
13534 case PLUS:
f3bb6135
RE
13535 return "add";
13536
cce8749e 13537 case MINUS:
f3bb6135
RE
13538 return shift_first_arg ? "rsb" : "sub";
13539
cce8749e 13540 case IOR:
f3bb6135
RE
13541 return "orr";
13542
cce8749e 13543 case XOR:
f3bb6135
RE
13544 return "eor";
13545
cce8749e 13546 case AND:
f3bb6135
RE
13547 return "and";
13548
5b3e6663
PB
13549 case ASHIFT:
13550 case ASHIFTRT:
13551 case LSHIFTRT:
13552 case ROTATERT:
13553 return arm_shift_nmem(GET_CODE(op));
13554
cce8749e 13555 default:
e6d29d15 13556 gcc_unreachable ();
cce8749e 13557 }
f3bb6135 13558}
cce8749e 13559
cce8749e
CH
13560/* Ensure valid constant shifts and return the appropriate shift mnemonic
13561 for the operation code. The returned result should not be overwritten.
13562 OP is the rtx code of the shift.
9997d19d 13563 On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
6354dc9b 13564 shift. */
cd2b33d0 13565static const char *
e32bac5b 13566shift_op (rtx op, HOST_WIDE_INT *amountp)
cce8749e 13567{
cd2b33d0 13568 const char * mnem;
e2c671ba 13569 enum rtx_code code = GET_CODE (op);
cce8749e 13570
e6d29d15
NS
13571 switch (GET_CODE (XEXP (op, 1)))
13572 {
13573 case REG:
13574 case SUBREG:
13575 *amountp = -1;
13576 break;
13577
13578 case CONST_INT:
13579 *amountp = INTVAL (XEXP (op, 1));
13580 break;
13581
13582 default:
13583 gcc_unreachable ();
13584 }
9997d19d 13585
e2c671ba 13586 switch (code)
cce8749e 13587 {
a657c98a 13588 case ROTATE:
e6d29d15 13589 gcc_assert (*amountp != -1);
a657c98a 13590 *amountp = 32 - *amountp;
5b3e6663 13591 code = ROTATERT;
a657c98a
RE
13592
13593 /* Fall through. */
13594
5b3e6663
PB
13595 case ASHIFT:
13596 case ASHIFTRT:
13597 case LSHIFTRT:
9997d19d 13598 case ROTATERT:
5b3e6663 13599 mnem = arm_shift_nmem(code);
9997d19d
RE
13600 break;
13601
ff9940b0 13602 case MULT:
e2c671ba
RE
13603 /* We never have to worry about the amount being other than a
13604 power of 2, since this case can never be reloaded from a reg. */
e6d29d15
NS
13605 gcc_assert (*amountp != -1);
13606 *amountp = int_log2 (*amountp);
5b3e6663 13607 return ARM_LSL_NAME;
f3bb6135 13608
cce8749e 13609 default:
e6d29d15 13610 gcc_unreachable ();
cce8749e
CH
13611 }
13612
e2c671ba
RE
13613 if (*amountp != -1)
13614 {
13615 /* This is not 100% correct, but follows from the desire to merge
13616 multiplication by a power of 2 with the recognizer for a
5b3e6663 13617 shift. >=32 is not a valid shift for "lsl", so we must try and
e2c671ba 13618 output a shift that produces the correct arithmetical result.
ddd5a7c1 13619 Using lsr #32 is identical except for the fact that the carry bit
f676971a 13620 is not set correctly if we set the flags; but we never use the
e2c671ba
RE
13621 carry bit from such an operation, so we can ignore that. */
13622 if (code == ROTATERT)
1d6e90ac
NC
13623 /* Rotate is just modulo 32. */
13624 *amountp &= 31;
e2c671ba
RE
13625 else if (*amountp != (*amountp & 31))
13626 {
13627 if (code == ASHIFT)
13628 mnem = "lsr";
13629 *amountp = 32;
13630 }
13631
13632 /* Shifts of 0 are no-ops. */
13633 if (*amountp == 0)
13634 return NULL;
f676971a 13635 }
e2c671ba 13636
9997d19d
RE
13637 return mnem;
13638}
cce8749e 13639
6354dc9b 13640/* Obtain the shift from the POWER of two. */
1d6e90ac 13641
18af7313 13642static HOST_WIDE_INT
e32bac5b 13643int_log2 (HOST_WIDE_INT power)
cce8749e 13644{
f3bb6135 13645 HOST_WIDE_INT shift = 0;
cce8749e 13646
30cf4896 13647 while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
cce8749e 13648 {
e6d29d15 13649 gcc_assert (shift <= 31);
e32bac5b 13650 shift++;
cce8749e 13651 }
f3bb6135
RE
13652
13653 return shift;
13654}
cce8749e 13655
c5ff069d
ZW
13656/* Output a .ascii pseudo-op, keeping track of lengths. This is
13657 because /bin/as is horribly restrictive. The judgement about
13658 whether or not each character is 'printable' (and can be output as
13659 is) or not (and must be printed with an octal escape) must be made
13660 with reference to the *host* character set -- the situation is
13661 similar to that discussed in the comments above pp_c_char in
13662 c-pretty-print.c. */
13663
6cfc7210 13664#define MAX_ASCII_LEN 51
cce8749e
CH
13665
13666void
e32bac5b 13667output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
cce8749e
CH
13668{
13669 int i;
6cfc7210 13670 int len_so_far = 0;
cce8749e 13671
6cfc7210 13672 fputs ("\t.ascii\t\"", stream);
f676971a 13673
cce8749e
CH
13674 for (i = 0; i < len; i++)
13675 {
1d6e90ac 13676 int c = p[i];
cce8749e 13677
6cfc7210 13678 if (len_so_far >= MAX_ASCII_LEN)
cce8749e 13679 {
6cfc7210 13680 fputs ("\"\n\t.ascii\t\"", stream);
cce8749e 13681 len_so_far = 0;
cce8749e
CH
13682 }
13683
c5ff069d 13684 if (ISPRINT (c))
cce8749e 13685 {
c5ff069d 13686 if (c == '\\' || c == '\"')
6cfc7210 13687 {
c5ff069d 13688 putc ('\\', stream);
5895f793 13689 len_so_far++;
6cfc7210 13690 }
c5ff069d
ZW
13691 putc (c, stream);
13692 len_so_far++;
13693 }
13694 else
13695 {
13696 fprintf (stream, "\\%03o", c);
13697 len_so_far += 4;
cce8749e 13698 }
cce8749e 13699 }
f3bb6135 13700
cce8749e 13701 fputs ("\"\n", stream);
f3bb6135 13702}
cce8749e 13703\f
c9ca9b88 13704/* Compute the register save mask for registers 0 through 12
5848830f 13705 inclusive. This code is used by arm_compute_save_reg_mask. */
b279b20a 13706
6d3d9133 13707static unsigned long
e32bac5b 13708arm_compute_save_reg0_reg12_mask (void)
6d3d9133 13709{
121308d4 13710 unsigned long func_type = arm_current_func_type ();
b279b20a 13711 unsigned long save_reg_mask = 0;
6d3d9133 13712 unsigned int reg;
6d3d9133 13713
7b8b8ade 13714 if (IS_INTERRUPT (func_type))
6d3d9133 13715 {
7b8b8ade 13716 unsigned int max_reg;
7b8b8ade
NC
13717 /* Interrupt functions must not corrupt any registers,
13718 even call clobbered ones. If this is a leaf function
13719 we can just examine the registers used by the RTL, but
13720 otherwise we have to assume that whatever function is
13721 called might clobber anything, and so we have to save
13722 all the call-clobbered registers as well. */
13723 if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
13724 /* FIQ handlers have registers r8 - r12 banked, so
13725 we only need to check r0 - r7, Normal ISRs only
121308d4 13726 bank r14 and r15, so we must check up to r12.
7b8b8ade
NC
13727 r13 is the stack pointer which is always preserved,
13728 so we do not need to consider it here. */
13729 max_reg = 7;
13730 else
13731 max_reg = 12;
f676971a 13732
7b8b8ade 13733 for (reg = 0; reg <= max_reg; reg++)
6fb5fa3c
DB
13734 if (df_regs_ever_live_p (reg)
13735 || (! current_function_is_leaf && call_used_regs[reg]))
6d3d9133 13736 save_reg_mask |= (1 << reg);
cfa01aab 13737
286d28c3 13738 /* Also save the pic base register if necessary. */
cfa01aab
PB
13739 if (flag_pic
13740 && !TARGET_SINGLE_PIC_BASE
020a4035 13741 && arm_pic_register != INVALID_REGNUM
e3b5732b 13742 && crtl->uses_pic_offset_table)
cfa01aab 13743 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
6d3d9133 13744 }
1586899e
PB
13745 else if (IS_VOLATILE(func_type))
13746 {
13747 /* For noreturn functions we historically omitted register saves
13748 altogether. However this really messes up debugging. As a
3ed04dbd 13749 compromise save just the frame pointers. Combined with the link
1586899e
PB
13750 register saved elsewhere this should be sufficient to get
13751 a backtrace. */
13752 if (frame_pointer_needed)
13753 save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
13754 if (df_regs_ever_live_p (ARM_HARD_FRAME_POINTER_REGNUM))
13755 save_reg_mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
13756 if (df_regs_ever_live_p (THUMB_HARD_FRAME_POINTER_REGNUM))
13757 save_reg_mask |= 1 << THUMB_HARD_FRAME_POINTER_REGNUM;
13758 }
6d3d9133
NC
13759 else
13760 {
13761 /* In the normal case we only need to save those registers
13762 which are call saved and which are used by this function. */
ec6237e4 13763 for (reg = 0; reg <= 11; reg++)
6fb5fa3c 13764 if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
6d3d9133
NC
13765 save_reg_mask |= (1 << reg);
13766
13767 /* Handle the frame pointer as a special case. */
ec6237e4 13768 if (frame_pointer_needed)
6d3d9133
NC
13769 save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
13770
13771 /* If we aren't loading the PIC register,
13772 don't stack it even though it may be live. */
13773 if (flag_pic
e0b92319 13774 && !TARGET_SINGLE_PIC_BASE
020a4035 13775 && arm_pic_register != INVALID_REGNUM
6fb5fa3c 13776 && (df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM)
e3b5732b 13777 || crtl->uses_pic_offset_table))
6d3d9133 13778 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
5b3e6663
PB
13779
13780 /* The prologue will copy SP into R0, so save it. */
13781 if (IS_STACKALIGN (func_type))
13782 save_reg_mask |= 1;
6d3d9133
NC
13783 }
13784
c9ca9b88 13785 /* Save registers so the exception handler can modify them. */
e3b5732b 13786 if (crtl->calls_eh_return)
c9ca9b88
PB
13787 {
13788 unsigned int i;
f676971a 13789
c9ca9b88
PB
13790 for (i = 0; ; i++)
13791 {
13792 reg = EH_RETURN_DATA_REGNO (i);
13793 if (reg == INVALID_REGNUM)
13794 break;
13795 save_reg_mask |= 1 << reg;
13796 }
13797 }
13798
121308d4
NC
13799 return save_reg_mask;
13800}
13801
5b3e6663 13802
35596784
AJ
13803/* Compute the number of bytes used to store the static chain register on the
13804 stack, above the stack frame. We need to know this accurately to get the
13805 alignment of the rest of the stack frame correct. */
13806
13807static int arm_compute_static_chain_stack_bytes (void)
13808{
13809 unsigned long func_type = arm_current_func_type ();
13810 int static_chain_stack_bytes = 0;
13811
13812 if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM &&
13813 IS_NESTED (func_type) &&
13814 df_regs_ever_live_p (3) && crtl->args.pretend_args_size == 0)
13815 static_chain_stack_bytes = 4;
13816
13817 return static_chain_stack_bytes;
13818}
13819
13820
121308d4 13821/* Compute a bit mask of which registers need to be
954954d1
PB
13822 saved on the stack for the current function.
13823 This is used by arm_get_frame_offsets, which may add extra registers. */
121308d4
NC
13824
13825static unsigned long
e32bac5b 13826arm_compute_save_reg_mask (void)
121308d4
NC
13827{
13828 unsigned int save_reg_mask = 0;
13829 unsigned long func_type = arm_current_func_type ();
5b3e6663 13830 unsigned int reg;
121308d4
NC
13831
13832 if (IS_NAKED (func_type))
13833 /* This should never really happen. */
13834 return 0;
13835
13836 /* If we are creating a stack frame, then we must save the frame pointer,
13837 IP (which will hold the old stack pointer), LR and the PC. */
ec6237e4 13838 if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
121308d4
NC
13839 save_reg_mask |=
13840 (1 << ARM_HARD_FRAME_POINTER_REGNUM)
13841 | (1 << IP_REGNUM)
13842 | (1 << LR_REGNUM)
13843 | (1 << PC_REGNUM);
13844
121308d4
NC
13845 save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
13846
6d3d9133
NC
13847 /* Decide if we need to save the link register.
13848 Interrupt routines have their own banked link register,
13849 so they never need to save it.
1768c26f 13850 Otherwise if we do not use the link register we do not need to save
6d3d9133
NC
13851 it. If we are pushing other registers onto the stack however, we
13852 can save an instruction in the epilogue by pushing the link register
13853 now and then popping it back into the PC. This incurs extra memory
72ac76be 13854 accesses though, so we only do it when optimizing for size, and only
6d3d9133 13855 if we know that we will not need a fancy return sequence. */
6fb5fa3c
DB
13856 if (df_regs_ever_live_p (LR_REGNUM)
13857 || (save_reg_mask
13858 && optimize_size
13859 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
e3b5732b 13860 && !crtl->calls_eh_return))
6d3d9133
NC
13861 save_reg_mask |= 1 << LR_REGNUM;
13862
6f7ebcbb
NC
13863 if (cfun->machine->lr_save_eliminated)
13864 save_reg_mask &= ~ (1 << LR_REGNUM);
13865
5a9335ef
NC
13866 if (TARGET_REALLY_IWMMXT
13867 && ((bit_count (save_reg_mask)
35596784
AJ
13868 + ARM_NUM_INTS (crtl->args.pretend_args_size +
13869 arm_compute_static_chain_stack_bytes())
13870 ) % 2) != 0)
5a9335ef 13871 {
5a9335ef
NC
13872 /* The total number of registers that are going to be pushed
13873 onto the stack is odd. We need to ensure that the stack
13874 is 64-bit aligned before we start to save iWMMXt registers,
13875 and also before we start to create locals. (A local variable
13876 might be a double or long long which we will load/store using
13877 an iWMMXt instruction). Therefore we need to push another
13878 ARM register, so that the stack will be 64-bit aligned. We
13879 try to avoid using the arg registers (r0 -r3) as they might be
13880 used to pass values in a tail call. */
13881 for (reg = 4; reg <= 12; reg++)
13882 if ((save_reg_mask & (1 << reg)) == 0)
13883 break;
13884
13885 if (reg <= 12)
13886 save_reg_mask |= (1 << reg);
13887 else
13888 {
13889 cfun->machine->sibcall_blocked = 1;
13890 save_reg_mask |= (1 << 3);
13891 }
13892 }
13893
5b3e6663
PB
13894 /* We may need to push an additional register for use initializing the
13895 PIC base register. */
13896 if (TARGET_THUMB2 && IS_NESTED (func_type) && flag_pic
13897 && (save_reg_mask & THUMB2_WORK_REGS) == 0)
13898 {
13899 reg = thumb_find_work_register (1 << 4);
13900 if (!call_used_regs[reg])
13901 save_reg_mask |= (1 << reg);
13902 }
13903
6d3d9133
NC
13904 return save_reg_mask;
13905}
13906
9728c9d1 13907
57934c39
PB
13908/* Compute a bit mask of which registers need to be
13909 saved on the stack for the current function. */
13910static unsigned long
5b3e6663 13911thumb1_compute_save_reg_mask (void)
57934c39
PB
13912{
13913 unsigned long mask;
b279b20a 13914 unsigned reg;
57934c39
PB
13915
13916 mask = 0;
13917 for (reg = 0; reg < 12; reg ++)
6fb5fa3c 13918 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
b279b20a 13919 mask |= 1 << reg;
57934c39 13920
39c39be0
RE
13921 if (flag_pic
13922 && !TARGET_SINGLE_PIC_BASE
020a4035 13923 && arm_pic_register != INVALID_REGNUM
e3b5732b 13924 && crtl->uses_pic_offset_table)
39c39be0 13925 mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
b279b20a 13926
a2503645
RS
13927 /* See if we might need r11 for calls to _interwork_r11_call_via_rN(). */
13928 if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
13929 mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
57934c39 13930
b279b20a 13931 /* LR will also be pushed if any lo regs are pushed. */
57934c39
PB
13932 if (mask & 0xff || thumb_force_lr_save ())
13933 mask |= (1 << LR_REGNUM);
13934
b279b20a
NC
13935 /* Make sure we have a low work register if we need one.
13936 We will need one if we are going to push a high register,
13937 but we are not currently intending to push a low register. */
13938 if ((mask & 0xff) == 0
57934c39 13939 && ((mask & 0x0f00) || TARGET_BACKTRACE))
b279b20a
NC
13940 {
13941 /* Use thumb_find_work_register to choose which register
13942 we will use. If the register is live then we will
13943 have to push it. Use LAST_LO_REGNUM as our fallback
13944 choice for the register to select. */
13945 reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
19e723f4
PB
13946 /* Make sure the register returned by thumb_find_work_register is
13947 not part of the return value. */
954954d1 13948 if (reg * UNITS_PER_WORD <= (unsigned) arm_size_return_regs ())
19e723f4 13949 reg = LAST_LO_REGNUM;
b279b20a
NC
13950
13951 if (! call_used_regs[reg])
13952 mask |= 1 << reg;
13953 }
57934c39 13954
35596784
AJ
13955 /* The 504 below is 8 bytes less than 512 because there are two possible
13956 alignment words. We can't tell here if they will be present or not so we
13957 have to play it safe and assume that they are. */
13958 if ((CALLER_INTERWORKING_SLOT_SIZE +
13959 ROUND_UP_WORD (get_frame_size ()) +
13960 crtl->outgoing_args_size) >= 504)
13961 {
13962 /* This is the same as the code in thumb1_expand_prologue() which
13963 determines which register to use for stack decrement. */
13964 for (reg = LAST_ARG_REGNUM + 1; reg <= LAST_LO_REGNUM; reg++)
13965 if (mask & (1 << reg))
13966 break;
13967
13968 if (reg > LAST_LO_REGNUM)
13969 {
13970 /* Make sure we have a register available for stack decrement. */
13971 mask |= 1 << LAST_LO_REGNUM;
13972 }
13973 }
13974
57934c39
PB
13975 return mask;
13976}
13977
13978
9728c9d1
PB
13979/* Return the number of bytes required to save VFP registers. */
13980static int
13981arm_get_vfp_saved_size (void)
13982{
13983 unsigned int regno;
13984 int count;
13985 int saved;
13986
13987 saved = 0;
13988 /* Space for saved VFP registers. */
13989 if (TARGET_HARD_FLOAT && TARGET_VFP)
13990 {
13991 count = 0;
13992 for (regno = FIRST_VFP_REGNUM;
13993 regno < LAST_VFP_REGNUM;
13994 regno += 2)
13995 {
6fb5fa3c
DB
13996 if ((!df_regs_ever_live_p (regno) || call_used_regs[regno])
13997 && (!df_regs_ever_live_p (regno + 1) || call_used_regs[regno + 1]))
9728c9d1
PB
13998 {
13999 if (count > 0)
14000 {
14001 /* Workaround ARM10 VFPr1 bug. */
14002 if (count == 2 && !arm_arch6)
14003 count++;
8edfc4cc 14004 saved += count * 8;
9728c9d1
PB
14005 }
14006 count = 0;
14007 }
14008 else
14009 count++;
14010 }
14011 if (count > 0)
14012 {
14013 if (count == 2 && !arm_arch6)
14014 count++;
8edfc4cc 14015 saved += count * 8;
9728c9d1
PB
14016 }
14017 }
14018 return saved;
14019}
14020
14021
699a4925 14022/* Generate a function exit sequence. If REALLY_RETURN is false, then do
6d3d9133 14023 everything bar the final return instruction. */
cd2b33d0 14024const char *
e32bac5b 14025output_return_instruction (rtx operand, int really_return, int reverse)
ff9940b0 14026{
6d3d9133 14027 char conditional[10];
ff9940b0 14028 char instr[100];
b279b20a 14029 unsigned reg;
6d3d9133
NC
14030 unsigned long live_regs_mask;
14031 unsigned long func_type;
5848830f 14032 arm_stack_offsets *offsets;
e26053d1 14033
6d3d9133 14034 func_type = arm_current_func_type ();
e2c671ba 14035
6d3d9133 14036 if (IS_NAKED (func_type))
d5b7b3ae 14037 return "";
6d3d9133
NC
14038
14039 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
e2c671ba 14040 {
699a4925
RE
14041 /* If this function was declared non-returning, and we have
14042 found a tail call, then we have to trust that the called
14043 function won't return. */
3a5a4282
PB
14044 if (really_return)
14045 {
14046 rtx ops[2];
f676971a 14047
3a5a4282
PB
14048 /* Otherwise, trap an attempted return by aborting. */
14049 ops[0] = operand;
f676971a 14050 ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
3a5a4282
PB
14051 : "abort");
14052 assemble_external_libcall (ops[1]);
14053 output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
14054 }
f676971a 14055
e2c671ba
RE
14056 return "";
14057 }
6d3d9133 14058
e3b5732b 14059 gcc_assert (!cfun->calls_alloca || really_return);
ff9940b0 14060
c414f8a9 14061 sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
d5b7b3ae 14062
934c2060 14063 cfun->machine->return_used_this_function = 1;
ff9940b0 14064
954954d1
PB
14065 offsets = arm_get_frame_offsets ();
14066 live_regs_mask = offsets->saved_regs_mask;
ff9940b0 14067
1768c26f 14068 if (live_regs_mask)
6d3d9133 14069 {
1768c26f
PB
14070 const char * return_reg;
14071
f676971a 14072 /* If we do not have any special requirements for function exit
a15908a4 14073 (e.g. interworking) then we can load the return address
1768c26f
PB
14074 directly into the PC. Otherwise we must load it into LR. */
14075 if (really_return
a15908a4 14076 && (IS_INTERRUPT (func_type) || !TARGET_INTERWORK))
1768c26f 14077 return_reg = reg_names[PC_REGNUM];
6d3d9133 14078 else
1768c26f
PB
14079 return_reg = reg_names[LR_REGNUM];
14080
6d3d9133 14081 if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
b034930f
ILT
14082 {
14083 /* There are three possible reasons for the IP register
14084 being saved. 1) a stack frame was created, in which case
14085 IP contains the old stack pointer, or 2) an ISR routine
14086 corrupted it, or 3) it was saved to align the stack on
14087 iWMMXt. In case 1, restore IP into SP, otherwise just
14088 restore IP. */
14089 if (frame_pointer_needed)
14090 {
14091 live_regs_mask &= ~ (1 << IP_REGNUM);
14092 live_regs_mask |= (1 << SP_REGNUM);
14093 }
14094 else
e6d29d15 14095 gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
b034930f 14096 }
f3bb6135 14097
3a7731fd
PB
14098 /* On some ARM architectures it is faster to use LDR rather than
14099 LDM to load a single register. On other architectures, the
14100 cost is the same. In 26 bit mode, or for exception handlers,
14101 we have to use LDM to load the PC so that the CPSR is also
14102 restored. */
14103 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
b279b20a
NC
14104 if (live_regs_mask == (1U << reg))
14105 break;
14106
3a7731fd
PB
14107 if (reg <= LAST_ARM_REGNUM
14108 && (reg != LR_REGNUM
f676971a 14109 || ! really_return
61f0ccff 14110 || ! IS_INTERRUPT (func_type)))
3a7731fd 14111 {
f676971a 14112 sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
3a7731fd 14113 (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
6d3d9133 14114 }
ff9940b0 14115 else
1d5473cb 14116 {
1768c26f
PB
14117 char *p;
14118 int first = 1;
6d3d9133 14119
699a4925
RE
14120 /* Generate the load multiple instruction to restore the
14121 registers. Note we can get here, even if
14122 frame_pointer_needed is true, but only if sp already
14123 points to the base of the saved core registers. */
14124 if (live_regs_mask & (1 << SP_REGNUM))
a72d4945 14125 {
5848830f
PB
14126 unsigned HOST_WIDE_INT stack_adjust;
14127
5848830f 14128 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
e6d29d15 14129 gcc_assert (stack_adjust == 0 || stack_adjust == 4);
a72d4945 14130
5b3e6663 14131 if (stack_adjust && arm_arch5 && TARGET_ARM)
c7e9ab97
RR
14132 if (TARGET_UNIFIED_ASM)
14133 sprintf (instr, "ldmib%s\t%%|sp, {", conditional);
14134 else
14135 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
a72d4945
RE
14136 else
14137 {
b279b20a
NC
14138 /* If we can't use ldmib (SA110 bug),
14139 then try to pop r3 instead. */
a72d4945
RE
14140 if (stack_adjust)
14141 live_regs_mask |= 1 << 3;
c7e9ab97
RR
14142
14143 if (TARGET_UNIFIED_ASM)
14144 sprintf (instr, "ldmfd%s\t%%|sp, {", conditional);
14145 else
14146 sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
a72d4945
RE
14147 }
14148 }
da6558fd 14149 else
c7e9ab97
RR
14150 if (TARGET_UNIFIED_ASM)
14151 sprintf (instr, "pop%s\t{", conditional);
14152 else
14153 sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
1768c26f
PB
14154
14155 p = instr + strlen (instr);
6d3d9133 14156
1768c26f
PB
14157 for (reg = 0; reg <= SP_REGNUM; reg++)
14158 if (live_regs_mask & (1 << reg))
14159 {
14160 int l = strlen (reg_names[reg]);
14161
14162 if (first)
14163 first = 0;
14164 else
14165 {
14166 memcpy (p, ", ", 2);
14167 p += 2;
14168 }
14169
14170 memcpy (p, "%|", 2);
14171 memcpy (p + 2, reg_names[reg], l);
14172 p += l + 2;
14173 }
f676971a 14174
1768c26f
PB
14175 if (live_regs_mask & (1 << LR_REGNUM))
14176 {
b17fe233 14177 sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
61f0ccff
RE
14178 /* If returning from an interrupt, restore the CPSR. */
14179 if (IS_INTERRUPT (func_type))
b17fe233 14180 strcat (p, "^");
1768c26f
PB
14181 }
14182 else
14183 strcpy (p, "}");
1d5473cb 14184 }
da6558fd 14185
1768c26f
PB
14186 output_asm_insn (instr, & operand);
14187
3a7731fd
PB
14188 /* See if we need to generate an extra instruction to
14189 perform the actual function return. */
14190 if (really_return
14191 && func_type != ARM_FT_INTERWORKED
14192 && (live_regs_mask & (1 << LR_REGNUM)) != 0)
da6558fd 14193 {
3a7731fd
PB
14194 /* The return has already been handled
14195 by loading the LR into the PC. */
14196 really_return = 0;
da6558fd 14197 }
ff9940b0 14198 }
e26053d1 14199
1768c26f 14200 if (really_return)
ff9940b0 14201 {
6d3d9133
NC
14202 switch ((int) ARM_FUNC_TYPE (func_type))
14203 {
14204 case ARM_FT_ISR:
14205 case ARM_FT_FIQ:
5b3e6663 14206 /* ??? This is wrong for unified assembly syntax. */
6d3d9133
NC
14207 sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
14208 break;
14209
14210 case ARM_FT_INTERWORKED:
14211 sprintf (instr, "bx%s\t%%|lr", conditional);
14212 break;
14213
14214 case ARM_FT_EXCEPTION:
5b3e6663 14215 /* ??? This is wrong for unified assembly syntax. */
6d3d9133
NC
14216 sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
14217 break;
14218
14219 default:
68d560d4
RE
14220 /* Use bx if it's available. */
14221 if (arm_arch5 || arm_arch4t)
f676971a 14222 sprintf (instr, "bx%s\t%%|lr", conditional);
1768c26f 14223 else
61f0ccff 14224 sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
6d3d9133
NC
14225 break;
14226 }
1768c26f
PB
14227
14228 output_asm_insn (instr, & operand);
ff9940b0 14229 }
f3bb6135 14230
ff9940b0
RE
14231 return "";
14232}
14233
ef179a26
NC
14234/* Write the function name into the code section, directly preceding
14235 the function prologue.
14236
14237 Code will be output similar to this:
14238 t0
14239 .ascii "arm_poke_function_name", 0
14240 .align
14241 t1
14242 .word 0xff000000 + (t1 - t0)
14243 arm_poke_function_name
14244 mov ip, sp
14245 stmfd sp!, {fp, ip, lr, pc}
14246 sub fp, ip, #4
14247
14248 When performing a stack backtrace, code can inspect the value
14249 of 'pc' stored at 'fp' + 0. If the trace function then looks
14250 at location pc - 12 and the top 8 bits are set, then we know
14251 that there is a function name embedded immediately preceding this
14252 location and has length ((pc[-3]) & 0xff000000).
14253
14254 We assume that pc is declared as a pointer to an unsigned long.
14255
14256 It is of no benefit to output the function name if we are assembling
14257 a leaf function. These function types will not contain a stack
14258 backtrace structure, therefore it is not possible to determine the
14259 function name. */
ef179a26 14260void
e32bac5b 14261arm_poke_function_name (FILE *stream, const char *name)
ef179a26
NC
14262{
14263 unsigned long alignlength;
14264 unsigned long length;
14265 rtx x;
14266
d5b7b3ae 14267 length = strlen (name) + 1;
0c2ca901 14268 alignlength = ROUND_UP_WORD (length);
f676971a 14269
949d79eb 14270 ASM_OUTPUT_ASCII (stream, name, length);
ef179a26 14271 ASM_OUTPUT_ALIGN (stream, 2);
30cf4896 14272 x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
301d03af 14273 assemble_aligned_integer (UNITS_PER_WORD, x);
ef179a26
NC
14274}
14275
6d3d9133
NC
14276/* Place some comments into the assembler stream
14277 describing the current function. */
08c148a8 14278static void
e32bac5b 14279arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
cce8749e 14280{
6d3d9133 14281 unsigned long func_type;
08c148a8 14282
5b3e6663 14283 if (TARGET_THUMB1)
08c148a8 14284 {
5b3e6663 14285 thumb1_output_function_prologue (f, frame_size);
08c148a8
NB
14286 return;
14287 }
f676971a 14288
6d3d9133 14289 /* Sanity check. */
e6d29d15 14290 gcc_assert (!arm_ccfsm_state && !arm_target_insn);
31fdb4d5 14291
6d3d9133 14292 func_type = arm_current_func_type ();
f676971a 14293
6d3d9133
NC
14294 switch ((int) ARM_FUNC_TYPE (func_type))
14295 {
14296 default:
14297 case ARM_FT_NORMAL:
14298 break;
14299 case ARM_FT_INTERWORKED:
14300 asm_fprintf (f, "\t%@ Function supports interworking.\n");
14301 break;
6d3d9133
NC
14302 case ARM_FT_ISR:
14303 asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
14304 break;
14305 case ARM_FT_FIQ:
14306 asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
14307 break;
14308 case ARM_FT_EXCEPTION:
14309 asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
14310 break;
14311 }
f676971a 14312
6d3d9133
NC
14313 if (IS_NAKED (func_type))
14314 asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
14315
14316 if (IS_VOLATILE (func_type))
14317 asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
14318
14319 if (IS_NESTED (func_type))
14320 asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
5b3e6663
PB
14321 if (IS_STACKALIGN (func_type))
14322 asm_fprintf (f, "\t%@ Stack Align: May be called with mis-aligned SP.\n");
f676971a 14323
c53dddc2 14324 asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
38173d38
JH
14325 crtl->args.size,
14326 crtl->args.pretend_args_size, frame_size);
6d3d9133 14327
3cb66fd7 14328 asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
dd18ae56 14329 frame_pointer_needed,
3cb66fd7 14330 cfun->machine->uses_anonymous_args);
cce8749e 14331
6f7ebcbb
NC
14332 if (cfun->machine->lr_save_eliminated)
14333 asm_fprintf (f, "\t%@ link register save eliminated.\n");
14334
e3b5732b 14335 if (crtl->calls_eh_return)
c9ca9b88
PB
14336 asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
14337
f3bb6135 14338}
cce8749e 14339
cd2b33d0 14340const char *
a72d4945 14341arm_output_epilogue (rtx sibling)
cce8749e 14342{
949d79eb 14343 int reg;
6f7ebcbb 14344 unsigned long saved_regs_mask;
6d3d9133 14345 unsigned long func_type;
f676971a 14346 /* Floats_offset is the offset from the "virtual" frame. In an APCS
c882c7ac
RE
14347 frame that is $fp + 4 for a non-variadic function. */
14348 int floats_offset = 0;
cce8749e 14349 rtx operands[3];
d5b7b3ae 14350 FILE * f = asm_out_file;
5a9335ef 14351 unsigned int lrm_count = 0;
a72d4945 14352 int really_return = (sibling == NULL);
9b66ebb1 14353 int start_reg;
5848830f 14354 arm_stack_offsets *offsets;
cce8749e 14355
6d3d9133
NC
14356 /* If we have already generated the return instruction
14357 then it is futile to generate anything else. */
934c2060
RR
14358 if (use_return_insn (FALSE, sibling) &&
14359 (cfun->machine->return_used_this_function != 0))
949d79eb 14360 return "";
cce8749e 14361
6d3d9133 14362 func_type = arm_current_func_type ();
d5b7b3ae 14363
6d3d9133
NC
14364 if (IS_NAKED (func_type))
14365 /* Naked functions don't have epilogues. */
14366 return "";
0616531f 14367
6d3d9133 14368 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
e2c671ba 14369 {
86efdc8e 14370 rtx op;
f676971a 14371
6d3d9133 14372 /* A volatile function should never return. Call abort. */
ed0e6530 14373 op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
2b835d68 14374 assemble_external_libcall (op);
e2c671ba 14375 output_asm_insn ("bl\t%a0", &op);
f676971a 14376
949d79eb 14377 return "";
e2c671ba
RE
14378 }
14379
e6d29d15
NS
14380 /* If we are throwing an exception, then we really must be doing a
14381 return, so we can't tail-call. */
e3b5732b 14382 gcc_assert (!crtl->calls_eh_return || really_return);
f676971a 14383
5848830f 14384 offsets = arm_get_frame_offsets ();
954954d1 14385 saved_regs_mask = offsets->saved_regs_mask;
5a9335ef
NC
14386
14387 if (TARGET_IWMMXT)
14388 lrm_count = bit_count (saved_regs_mask);
14389
5848830f 14390 floats_offset = offsets->saved_args;
6d3d9133 14391 /* Compute how far away the floats will be. */
5a9335ef 14392 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
6f7ebcbb 14393 if (saved_regs_mask & (1 << reg))
6ed30148 14394 floats_offset += 4;
f676971a 14395
ec6237e4 14396 if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
cce8749e 14397 {
9b66ebb1 14398 /* This variable is for the Virtual Frame Pointer, not VFP regs. */
5848830f 14399 int vfp_offset = offsets->frame;
c882c7ac 14400
d79f3032 14401 if (TARGET_FPA_EMU2)
b111229a 14402 {
9b66ebb1 14403 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
6fb5fa3c 14404 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
b111229a
RE
14405 {
14406 floats_offset += 12;
f676971a 14407 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
c882c7ac 14408 reg, FP_REGNUM, floats_offset - vfp_offset);
b111229a
RE
14409 }
14410 }
14411 else
14412 {
9b66ebb1 14413 start_reg = LAST_FPA_REGNUM;
b111229a 14414
9b66ebb1 14415 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
b111229a 14416 {
6fb5fa3c 14417 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
b111229a
RE
14418 {
14419 floats_offset += 12;
f676971a 14420
6354dc9b 14421 /* We can't unstack more than four registers at once. */
b111229a
RE
14422 if (start_reg - reg == 3)
14423 {
dd18ae56 14424 asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
c882c7ac 14425 reg, FP_REGNUM, floats_offset - vfp_offset);
b111229a
RE
14426 start_reg = reg - 1;
14427 }
14428 }
14429 else
14430 {
14431 if (reg != start_reg)
dd18ae56
NC
14432 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
14433 reg + 1, start_reg - reg,
c882c7ac 14434 FP_REGNUM, floats_offset - vfp_offset);
b111229a
RE
14435 start_reg = reg - 1;
14436 }
14437 }
14438
14439 /* Just in case the last register checked also needs unstacking. */
14440 if (reg != start_reg)
dd18ae56
NC
14441 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
14442 reg + 1, start_reg - reg,
c882c7ac 14443 FP_REGNUM, floats_offset - vfp_offset);
b111229a 14444 }
6d3d9133 14445
9b66ebb1
PB
14446 if (TARGET_HARD_FLOAT && TARGET_VFP)
14447 {
9728c9d1 14448 int saved_size;
9b66ebb1 14449
8edfc4cc
MS
14450 /* The fldmd insns do not have base+offset addressing
14451 modes, so we use IP to hold the address. */
9728c9d1 14452 saved_size = arm_get_vfp_saved_size ();
9b66ebb1 14453
9728c9d1 14454 if (saved_size > 0)
9b66ebb1 14455 {
9728c9d1 14456 floats_offset += saved_size;
9b66ebb1
PB
14457 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
14458 FP_REGNUM, floats_offset - vfp_offset);
14459 }
14460 start_reg = FIRST_VFP_REGNUM;
14461 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
14462 {
6fb5fa3c
DB
14463 if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
14464 && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
9b66ebb1
PB
14465 {
14466 if (start_reg != reg)
8edfc4cc 14467 vfp_output_fldmd (f, IP_REGNUM,
9728c9d1
PB
14468 (start_reg - FIRST_VFP_REGNUM) / 2,
14469 (reg - start_reg) / 2);
9b66ebb1
PB
14470 start_reg = reg + 2;
14471 }
14472 }
14473 if (start_reg != reg)
8edfc4cc 14474 vfp_output_fldmd (f, IP_REGNUM,
9728c9d1
PB
14475 (start_reg - FIRST_VFP_REGNUM) / 2,
14476 (reg - start_reg) / 2);
9b66ebb1
PB
14477 }
14478
5a9335ef
NC
14479 if (TARGET_IWMMXT)
14480 {
14481 /* The frame pointer is guaranteed to be non-double-word aligned.
14482 This is because it is set to (old_stack_pointer - 4) and the
14483 old_stack_pointer was double word aligned. Thus the offset to
14484 the iWMMXt registers to be loaded must also be non-double-word
14485 sized, so that the resultant address *is* double-word aligned.
14486 We can ignore floats_offset since that was already included in
14487 the live_regs_mask. */
14488 lrm_count += (lrm_count % 2 ? 2 : 1);
f676971a 14489
01d4c813 14490 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
6fb5fa3c 14491 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
5a9335ef 14492 {
f676971a 14493 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
5a9335ef 14494 reg, FP_REGNUM, lrm_count * 4);
f676971a 14495 lrm_count += 2;
5a9335ef
NC
14496 }
14497 }
14498
6f7ebcbb 14499 /* saved_regs_mask should contain the IP, which at the time of stack
6d3d9133
NC
14500 frame generation actually contains the old stack pointer. So a
14501 quick way to unwind the stack is just pop the IP register directly
14502 into the stack pointer. */
e6d29d15 14503 gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
6f7ebcbb
NC
14504 saved_regs_mask &= ~ (1 << IP_REGNUM);
14505 saved_regs_mask |= (1 << SP_REGNUM);
6d3d9133 14506
6f7ebcbb 14507 /* There are two registers left in saved_regs_mask - LR and PC. We
6d3d9133
NC
14508 only need to restore the LR register (the return address), but to
14509 save time we can load it directly into the PC, unless we need a
14510 special function exit sequence, or we are not really returning. */
c9ca9b88
PB
14511 if (really_return
14512 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
e3b5732b 14513 && !crtl->calls_eh_return)
6d3d9133
NC
14514 /* Delete the LR from the register mask, so that the LR on
14515 the stack is loaded into the PC in the register mask. */
6f7ebcbb 14516 saved_regs_mask &= ~ (1 << LR_REGNUM);
b111229a 14517 else
6f7ebcbb 14518 saved_regs_mask &= ~ (1 << PC_REGNUM);
efc2515b
RE
14519
14520 /* We must use SP as the base register, because SP is one of the
14521 registers being restored. If an interrupt or page fault
14522 happens in the ldm instruction, the SP might or might not
14523 have been restored. That would be bad, as then SP will no
14524 longer indicate the safe area of stack, and we can get stack
14525 corruption. Using SP as the base register means that it will
14526 be reset correctly to the original value, should an interrupt
699a4925
RE
14527 occur. If the stack pointer already points at the right
14528 place, then omit the subtraction. */
5848830f 14529 if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
e3b5732b 14530 || cfun->calls_alloca)
699a4925
RE
14531 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
14532 4 * bit_count (saved_regs_mask));
a15908a4 14533 print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask, 0);
7b8b8ade
NC
14534
14535 if (IS_INTERRUPT (func_type))
14536 /* Interrupt handlers will have pushed the
14537 IP onto the stack, so restore it now. */
a15908a4 14538 print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, 1 << IP_REGNUM, 0);
cce8749e
CH
14539 }
14540 else
14541 {
ec6237e4
PB
14542 /* This branch is executed for ARM mode (non-apcs frames) and
14543 Thumb-2 mode. Frame layout is essentially the same for those
14544 cases, except that in ARM mode frame pointer points to the
14545 first saved register, while in Thumb-2 mode the frame pointer points
14546 to the last saved register.
14547
14548 It is possible to make frame pointer point to last saved
14549 register in both cases, and remove some conditionals below.
14550 That means that fp setup in prologue would be just "mov fp, sp"
14551 and sp restore in epilogue would be just "mov sp, fp", whereas
14552 now we have to use add/sub in those cases. However, the value
14553 of that would be marginal, as both mov and add/sub are 32-bit
14554 in ARM mode, and it would require extra conditionals
14555 in arm_expand_prologue to distingish ARM-apcs-frame case
14556 (where frame pointer is required to point at first register)
14557 and ARM-non-apcs-frame. Therefore, such change is postponed
14558 until real need arise. */
f0b4bdd5 14559 unsigned HOST_WIDE_INT amount;
a15908a4 14560 int rfe;
d2288d8d 14561 /* Restore stack pointer if necessary. */
ec6237e4 14562 if (TARGET_ARM && frame_pointer_needed)
5b3e6663
PB
14563 {
14564 operands[0] = stack_pointer_rtx;
ec6237e4
PB
14565 operands[1] = hard_frame_pointer_rtx;
14566
14567 operands[2] = GEN_INT (offsets->frame - offsets->saved_regs);
14568 output_add_immediate (operands);
5b3e6663 14569 }
ec6237e4 14570 else
5b3e6663 14571 {
ec6237e4
PB
14572 if (frame_pointer_needed)
14573 {
14574 /* For Thumb-2 restore sp from the frame pointer.
14575 Operand restrictions mean we have to incrememnt FP, then copy
14576 to SP. */
14577 amount = offsets->locals_base - offsets->saved_regs;
14578 operands[0] = hard_frame_pointer_rtx;
14579 }
14580 else
14581 {
954954d1 14582 unsigned long count;
ec6237e4
PB
14583 operands[0] = stack_pointer_rtx;
14584 amount = offsets->outgoing_args - offsets->saved_regs;
954954d1
PB
14585 /* pop call clobbered registers if it avoids a
14586 separate stack adjustment. */
14587 count = offsets->saved_regs - offsets->saved_args;
14588 if (optimize_size
14589 && count != 0
e3b5732b 14590 && !crtl->calls_eh_return
954954d1
PB
14591 && bit_count(saved_regs_mask) * 4 == count
14592 && !IS_INTERRUPT (func_type)
e3b5732b 14593 && !crtl->tail_call_emit)
954954d1
PB
14594 {
14595 unsigned long mask;
c92f1823
IB
14596 /* Preserve return values, of any size. */
14597 mask = (1 << ((arm_size_return_regs() + 3) / 4)) - 1;
954954d1
PB
14598 mask ^= 0xf;
14599 mask &= ~saved_regs_mask;
14600 reg = 0;
14601 while (bit_count (mask) * 4 > amount)
14602 {
14603 while ((mask & (1 << reg)) == 0)
14604 reg++;
14605 mask &= ~(1 << reg);
14606 }
14607 if (bit_count (mask) * 4 == amount) {
14608 amount = 0;
14609 saved_regs_mask |= mask;
14610 }
14611 }
ec6237e4
PB
14612 }
14613
14614 if (amount)
14615 {
14616 operands[1] = operands[0];
14617 operands[2] = GEN_INT (amount);
14618 output_add_immediate (operands);
14619 }
14620 if (frame_pointer_needed)
14621 asm_fprintf (f, "\tmov\t%r, %r\n",
14622 SP_REGNUM, HARD_FRAME_POINTER_REGNUM);
d2288d8d
TG
14623 }
14624
d79f3032 14625 if (TARGET_FPA_EMU2)
b111229a 14626 {
9b66ebb1 14627 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
6fb5fa3c 14628 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
dd18ae56
NC
14629 asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
14630 reg, SP_REGNUM);
b111229a
RE
14631 }
14632 else
14633 {
9b66ebb1 14634 start_reg = FIRST_FPA_REGNUM;
b111229a 14635
9b66ebb1 14636 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
b111229a 14637 {
6fb5fa3c 14638 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
b111229a
RE
14639 {
14640 if (reg - start_reg == 3)
14641 {
dd18ae56
NC
14642 asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
14643 start_reg, SP_REGNUM);
b111229a
RE
14644 start_reg = reg + 1;
14645 }
14646 }
14647 else
14648 {
14649 if (reg != start_reg)
dd18ae56
NC
14650 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
14651 start_reg, reg - start_reg,
14652 SP_REGNUM);
f676971a 14653
b111229a
RE
14654 start_reg = reg + 1;
14655 }
14656 }
14657
14658 /* Just in case the last register checked also needs unstacking. */
14659 if (reg != start_reg)
dd18ae56
NC
14660 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
14661 start_reg, reg - start_reg, SP_REGNUM);
b111229a
RE
14662 }
14663
9b66ebb1
PB
14664 if (TARGET_HARD_FLOAT && TARGET_VFP)
14665 {
f8b68ed3
RE
14666 int end_reg = LAST_VFP_REGNUM + 1;
14667
14668 /* Scan the registers in reverse order. We need to match
14669 any groupings made in the prologue and generate matching
14670 pop operations. */
14671 for (reg = LAST_VFP_REGNUM - 1; reg >= FIRST_VFP_REGNUM; reg -= 2)
9b66ebb1 14672 {
6fb5fa3c 14673 if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
f8b68ed3
RE
14674 && (!df_regs_ever_live_p (reg + 1)
14675 || call_used_regs[reg + 1]))
9b66ebb1 14676 {
f8b68ed3 14677 if (end_reg > reg + 2)
8edfc4cc 14678 vfp_output_fldmd (f, SP_REGNUM,
f8b68ed3
RE
14679 (reg + 2 - FIRST_VFP_REGNUM) / 2,
14680 (end_reg - (reg + 2)) / 2);
14681 end_reg = reg;
9b66ebb1
PB
14682 }
14683 }
f8b68ed3
RE
14684 if (end_reg > reg + 2)
14685 vfp_output_fldmd (f, SP_REGNUM, 0,
14686 (end_reg - (reg + 2)) / 2);
9b66ebb1 14687 }
f8b68ed3 14688
5a9335ef
NC
14689 if (TARGET_IWMMXT)
14690 for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
6fb5fa3c 14691 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
01d4c813 14692 asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
5a9335ef 14693
6d3d9133 14694 /* If we can, restore the LR into the PC. */
a15908a4
PB
14695 if (ARM_FUNC_TYPE (func_type) != ARM_FT_INTERWORKED
14696 && (TARGET_ARM || ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
5b3e6663 14697 && !IS_STACKALIGN (func_type)
6d3d9133 14698 && really_return
38173d38 14699 && crtl->args.pretend_args_size == 0
c9ca9b88 14700 && saved_regs_mask & (1 << LR_REGNUM)
e3b5732b 14701 && !crtl->calls_eh_return)
cce8749e 14702 {
6f7ebcbb
NC
14703 saved_regs_mask &= ~ (1 << LR_REGNUM);
14704 saved_regs_mask |= (1 << PC_REGNUM);
a15908a4 14705 rfe = IS_INTERRUPT (func_type);
6d3d9133 14706 }
a15908a4
PB
14707 else
14708 rfe = 0;
d5b7b3ae 14709
6d3d9133 14710 /* Load the registers off the stack. If we only have one register
5b3e6663
PB
14711 to load use the LDR instruction - it is faster. For Thumb-2
14712 always use pop and the assembler will pick the best instruction.*/
a15908a4
PB
14713 if (TARGET_ARM && saved_regs_mask == (1 << LR_REGNUM)
14714 && !IS_INTERRUPT(func_type))
6d3d9133 14715 {
c9ca9b88 14716 asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
cce8749e 14717 }
6f7ebcbb 14718 else if (saved_regs_mask)
f1acdf8b
NC
14719 {
14720 if (saved_regs_mask & (1 << SP_REGNUM))
14721 /* Note - write back to the stack register is not enabled
112cdef5 14722 (i.e. "ldmfd sp!..."). We know that the stack pointer is
f1acdf8b
NC
14723 in the list of registers and if we add writeback the
14724 instruction becomes UNPREDICTABLE. */
a15908a4
PB
14725 print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask,
14726 rfe);
5b3e6663 14727 else if (TARGET_ARM)
a15908a4
PB
14728 print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, saved_regs_mask,
14729 rfe);
f1acdf8b 14730 else
a15908a4 14731 print_multi_reg (f, "pop\t", SP_REGNUM, saved_regs_mask, 0);
f1acdf8b 14732 }
6d3d9133 14733
38173d38 14734 if (crtl->args.pretend_args_size)
cce8749e 14735 {
6d3d9133
NC
14736 /* Unwind the pre-pushed regs. */
14737 operands[0] = operands[1] = stack_pointer_rtx;
38173d38 14738 operands[2] = GEN_INT (crtl->args.pretend_args_size);
6d3d9133
NC
14739 output_add_immediate (operands);
14740 }
14741 }
32de079a 14742
2966b00e 14743 /* We may have already restored PC directly from the stack. */
0cc3dda8 14744 if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
6d3d9133 14745 return "";
d5b7b3ae 14746
c9ca9b88 14747 /* Stack adjustment for exception handler. */
e3b5732b 14748 if (crtl->calls_eh_return)
f676971a 14749 asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
c9ca9b88
PB
14750 ARM_EH_STACKADJ_REGNUM);
14751
6d3d9133
NC
14752 /* Generate the return instruction. */
14753 switch ((int) ARM_FUNC_TYPE (func_type))
14754 {
6d3d9133
NC
14755 case ARM_FT_ISR:
14756 case ARM_FT_FIQ:
14757 asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
14758 break;
14759
14760 case ARM_FT_EXCEPTION:
14761 asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
14762 break;
14763
14764 case ARM_FT_INTERWORKED:
14765 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
14766 break;
14767
14768 default:
5b3e6663
PB
14769 if (IS_STACKALIGN (func_type))
14770 {
14771 /* See comment in arm_expand_prologue. */
14772 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, 0);
14773 }
68d560d4
RE
14774 if (arm_arch5 || arm_arch4t)
14775 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
14776 else
14777 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
6d3d9133 14778 break;
cce8749e 14779 }
f3bb6135 14780
949d79eb
RE
14781 return "";
14782}
14783
08c148a8 14784static void
e32bac5b 14785arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
5848830f 14786 HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
949d79eb 14787{
5848830f
PB
14788 arm_stack_offsets *offsets;
14789
5b3e6663 14790 if (TARGET_THUMB1)
d5b7b3ae 14791 {
b12a00f1
RE
14792 int regno;
14793
14794 /* Emit any call-via-reg trampolines that are needed for v4t support
14795 of call_reg and call_value_reg type insns. */
57ecec57 14796 for (regno = 0; regno < LR_REGNUM; regno++)
b12a00f1
RE
14797 {
14798 rtx label = cfun->machine->call_via[regno];
14799
14800 if (label != NULL)
14801 {
d6b5193b 14802 switch_to_section (function_section (current_function_decl));
b12a00f1
RE
14803 targetm.asm_out.internal_label (asm_out_file, "L",
14804 CODE_LABEL_NUMBER (label));
14805 asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
14806 }
14807 }
14808
d5b7b3ae
RE
14809 /* ??? Probably not safe to set this here, since it assumes that a
14810 function will be emitted as assembly immediately after we generate
14811 RTL for it. This does not happen for inline functions. */
934c2060 14812 cfun->machine->return_used_this_function = 0;
d5b7b3ae 14813 }
5b3e6663 14814 else /* TARGET_32BIT */
d5b7b3ae 14815 {
0977774b 14816 /* We need to take into account any stack-frame rounding. */
5848830f 14817 offsets = arm_get_frame_offsets ();
0977774b 14818
e6d29d15 14819 gcc_assert (!use_return_insn (FALSE, NULL)
934c2060 14820 || (cfun->machine->return_used_this_function != 0)
e6d29d15
NS
14821 || offsets->saved_regs == offsets->outgoing_args
14822 || frame_pointer_needed);
f3bb6135 14823
d5b7b3ae 14824 /* Reset the ARM-specific per-function variables. */
d5b7b3ae
RE
14825 after_arm_reorg = 0;
14826 }
f3bb6135 14827}
e2c671ba 14828
2c849145
JM
14829/* Generate and emit an insn that we will recognize as a push_multi.
14830 Unfortunately, since this insn does not reflect very well the actual
14831 semantics of the operation, we need to annotate the insn for the benefit
14832 of DWARF2 frame unwind information. */
2c849145 14833static rtx
b279b20a 14834emit_multi_reg_push (unsigned long mask)
e2c671ba
RE
14835{
14836 int num_regs = 0;
9b598fa0 14837 int num_dwarf_regs;
e2c671ba
RE
14838 int i, j;
14839 rtx par;
2c849145 14840 rtx dwarf;
87e27392 14841 int dwarf_par_index;
2c849145 14842 rtx tmp, reg;
e2c671ba 14843
d5b7b3ae 14844 for (i = 0; i <= LAST_ARM_REGNUM; i++)
e2c671ba 14845 if (mask & (1 << i))
5895f793 14846 num_regs++;
e2c671ba 14847
e6d29d15 14848 gcc_assert (num_regs && num_regs <= 16);
e2c671ba 14849
9b598fa0
RE
14850 /* We don't record the PC in the dwarf frame information. */
14851 num_dwarf_regs = num_regs;
14852 if (mask & (1 << PC_REGNUM))
14853 num_dwarf_regs--;
14854
87e27392 14855 /* For the body of the insn we are going to generate an UNSPEC in
05713b80 14856 parallel with several USEs. This allows the insn to be recognized
9abf5d7b
RR
14857 by the push_multi pattern in the arm.md file.
14858
14859 The body of the insn looks something like this:
87e27392 14860
f676971a 14861 (parallel [
9abf5d7b
RR
14862 (set (mem:BLK (pre_modify:SI (reg:SI sp)
14863 (const_int:SI <num>)))
b15bca31 14864 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
9abf5d7b
RR
14865 (use (reg:SI XX))
14866 (use (reg:SI YY))
14867 ...
87e27392
NC
14868 ])
14869
14870 For the frame note however, we try to be more explicit and actually
14871 show each register being stored into the stack frame, plus a (single)
14872 decrement of the stack pointer. We do it this way in order to be
14873 friendly to the stack unwinding code, which only wants to see a single
14874 stack decrement per instruction. The RTL we generate for the note looks
14875 something like this:
14876
f676971a 14877 (sequence [
87e27392
NC
14878 (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
14879 (set (mem:SI (reg:SI sp)) (reg:SI r4))
9abf5d7b
RR
14880 (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI XX))
14881 (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI YY))
14882 ...
87e27392
NC
14883 ])
14884
9abf5d7b
RR
14885 FIXME:: In an ideal world the PRE_MODIFY would not exist and
14886 instead we'd have a parallel expression detailing all
14887 the stores to the various memory addresses so that debug
14888 information is more up-to-date. Remember however while writing
14889 this to take care of the constraints with the push instruction.
14890
14891 Note also that this has to be taken care of for the VFP registers.
14892
14893 For more see PR43399. */
f676971a 14894
43cffd11 14895 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
9b598fa0 14896 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
87e27392 14897 dwarf_par_index = 1;
e2c671ba 14898
d5b7b3ae 14899 for (i = 0; i <= LAST_ARM_REGNUM; i++)
e2c671ba
RE
14900 {
14901 if (mask & (1 << i))
14902 {
2c849145
JM
14903 reg = gen_rtx_REG (SImode, i);
14904
e2c671ba 14905 XVECEXP (par, 0, 0)
43cffd11 14906 = gen_rtx_SET (VOIDmode,
9abf5d7b
RR
14907 gen_frame_mem
14908 (BLKmode,
14909 gen_rtx_PRE_MODIFY (Pmode,
14910 stack_pointer_rtx,
14911 plus_constant
14912 (stack_pointer_rtx,
14913 -4 * num_regs))
14914 ),
43cffd11 14915 gen_rtx_UNSPEC (BLKmode,
2c849145 14916 gen_rtvec (1, reg),
9b598fa0 14917 UNSPEC_PUSH_MULT));
2c849145 14918
9b598fa0
RE
14919 if (i != PC_REGNUM)
14920 {
14921 tmp = gen_rtx_SET (VOIDmode,
31fa16b6 14922 gen_frame_mem (SImode, stack_pointer_rtx),
9b598fa0
RE
14923 reg);
14924 RTX_FRAME_RELATED_P (tmp) = 1;
14925 XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
14926 dwarf_par_index++;
14927 }
2c849145 14928
e2c671ba
RE
14929 break;
14930 }
14931 }
14932
14933 for (j = 1, i++; j < num_regs; i++)
14934 {
14935 if (mask & (1 << i))
14936 {
2c849145
JM
14937 reg = gen_rtx_REG (SImode, i);
14938
14939 XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
14940
9b598fa0
RE
14941 if (i != PC_REGNUM)
14942 {
31fa16b6
RE
14943 tmp
14944 = gen_rtx_SET (VOIDmode,
9abf5d7b
RR
14945 gen_frame_mem
14946 (SImode,
14947 plus_constant (stack_pointer_rtx,
14948 4 * j)),
31fa16b6 14949 reg);
9b598fa0
RE
14950 RTX_FRAME_RELATED_P (tmp) = 1;
14951 XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
14952 }
14953
e2c671ba
RE
14954 j++;
14955 }
14956 }
b111229a 14957
2c849145 14958 par = emit_insn (par);
f676971a 14959
d66437c5 14960 tmp = gen_rtx_SET (VOIDmode,
87e27392 14961 stack_pointer_rtx,
d66437c5 14962 plus_constant (stack_pointer_rtx, -4 * num_regs));
87e27392
NC
14963 RTX_FRAME_RELATED_P (tmp) = 1;
14964 XVECEXP (dwarf, 0, 0) = tmp;
f676971a 14965
bbbbb16a
ILT
14966 add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
14967
2c849145 14968 return par;
b111229a
RE
14969}
14970
4f5dfed0 14971/* Calculate the size of the return value that is passed in registers. */
466e4b7a 14972static unsigned
4f5dfed0
JC
14973arm_size_return_regs (void)
14974{
14975 enum machine_mode mode;
14976
38173d38
JH
14977 if (crtl->return_rtx != 0)
14978 mode = GET_MODE (crtl->return_rtx);
4f5dfed0
JC
14979 else
14980 mode = DECL_MODE (DECL_RESULT (current_function_decl));
14981
14982 return GET_MODE_SIZE (mode);
14983}
14984
2c849145 14985static rtx
e32bac5b 14986emit_sfm (int base_reg, int count)
b111229a
RE
14987{
14988 rtx par;
2c849145
JM
14989 rtx dwarf;
14990 rtx tmp, reg;
b111229a
RE
14991 int i;
14992
43cffd11 14993 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
8ee6eb4e 14994 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
2c849145
JM
14995
14996 reg = gen_rtx_REG (XFmode, base_reg++);
43cffd11
RE
14997
14998 XVECEXP (par, 0, 0)
f676971a 14999 = gen_rtx_SET (VOIDmode,
9abf5d7b
RR
15000 gen_frame_mem
15001 (BLKmode,
15002 gen_rtx_PRE_MODIFY (Pmode,
15003 stack_pointer_rtx,
15004 plus_constant
15005 (stack_pointer_rtx,
15006 -12 * count))
15007 ),
43cffd11 15008 gen_rtx_UNSPEC (BLKmode,
2c849145 15009 gen_rtvec (1, reg),
b15bca31 15010 UNSPEC_PUSH_MULT));
f676971a 15011 tmp = gen_rtx_SET (VOIDmode,
31fa16b6 15012 gen_frame_mem (XFmode, stack_pointer_rtx), reg);
2c849145 15013 RTX_FRAME_RELATED_P (tmp) = 1;
f676971a
EC
15014 XVECEXP (dwarf, 0, 1) = tmp;
15015
b111229a 15016 for (i = 1; i < count; i++)
2c849145
JM
15017 {
15018 reg = gen_rtx_REG (XFmode, base_reg++);
15019 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
15020
f676971a 15021 tmp = gen_rtx_SET (VOIDmode,
31fa16b6
RE
15022 gen_frame_mem (XFmode,
15023 plus_constant (stack_pointer_rtx,
15024 i * 12)),
2c849145
JM
15025 reg);
15026 RTX_FRAME_RELATED_P (tmp) = 1;
f676971a 15027 XVECEXP (dwarf, 0, i + 1) = tmp;
2c849145 15028 }
b111229a 15029
8ee6eb4e
PB
15030 tmp = gen_rtx_SET (VOIDmode,
15031 stack_pointer_rtx,
d66437c5
RE
15032 plus_constant (stack_pointer_rtx, -12 * count));
15033
8ee6eb4e
PB
15034 RTX_FRAME_RELATED_P (tmp) = 1;
15035 XVECEXP (dwarf, 0, 0) = tmp;
15036
2c849145 15037 par = emit_insn (par);
bbbbb16a
ILT
15038 add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
15039
2c849145 15040 return par;
e2c671ba
RE
15041}
15042
9b66ebb1 15043
3c7ad43e
PB
15044/* Return true if the current function needs to save/restore LR. */
15045
15046static bool
15047thumb_force_lr_save (void)
15048{
15049 return !cfun->machine->lr_save_eliminated
15050 && (!leaf_function_p ()
15051 || thumb_far_jump_used_p ()
6fb5fa3c 15052 || df_regs_ever_live_p (LR_REGNUM));
3c7ad43e
PB
15053}
15054
15055
095bb276
NC
15056/* Compute the distance from register FROM to register TO.
15057 These can be the arg pointer (26), the soft frame pointer (25),
15058 the stack pointer (13) or the hard frame pointer (11).
c9ca9b88 15059 In thumb mode r7 is used as the soft frame pointer, if needed.
095bb276
NC
15060 Typical stack layout looks like this:
15061
15062 old stack pointer -> | |
15063 ----
15064 | | \
15065 | | saved arguments for
15066 | | vararg functions
15067 | | /
15068 --
15069 hard FP & arg pointer -> | | \
15070 | | stack
15071 | | frame
15072 | | /
15073 --
15074 | | \
15075 | | call saved
15076 | | registers
15077 soft frame pointer -> | | /
15078 --
15079 | | \
15080 | | local
15081 | | variables
2591db65 15082 locals base pointer -> | | /
095bb276
NC
15083 --
15084 | | \
15085 | | outgoing
15086 | | arguments
15087 current stack pointer -> | | /
15088 --
15089
43aa4e05 15090 For a given function some or all of these stack components
095bb276
NC
15091 may not be needed, giving rise to the possibility of
15092 eliminating some of the registers.
15093
825dda42 15094 The values returned by this function must reflect the behavior
095bb276
NC
15095 of arm_expand_prologue() and arm_compute_save_reg_mask().
15096
15097 The sign of the number returned reflects the direction of stack
15098 growth, so the values are positive for all eliminations except
5848830f
PB
15099 from the soft frame pointer to the hard frame pointer.
15100
15101 SFP may point just inside the local variables block to ensure correct
15102 alignment. */
15103
15104
15105/* Calculate stack offsets. These are used to calculate register elimination
954954d1
PB
15106 offsets and in prologue/epilogue code. Also calculates which registers
15107 should be saved. */
5848830f
PB
15108
15109static arm_stack_offsets *
15110arm_get_frame_offsets (void)
095bb276 15111{
5848830f 15112 struct arm_stack_offsets *offsets;
095bb276 15113 unsigned long func_type;
5848830f 15114 int leaf;
5848830f 15115 int saved;
954954d1 15116 int core_saved;
5848830f 15117 HOST_WIDE_INT frame_size;
954954d1 15118 int i;
5848830f
PB
15119
15120 offsets = &cfun->machine->stack_offsets;
f676971a 15121
5848830f
PB
15122 /* We need to know if we are a leaf function. Unfortunately, it
15123 is possible to be called after start_sequence has been called,
15124 which causes get_insns to return the insns for the sequence,
15125 not the function, which will cause leaf_function_p to return
15126 the incorrect result.
095bb276 15127
5848830f
PB
15128 to know about leaf functions once reload has completed, and the
15129 frame size cannot be changed after that time, so we can safely
15130 use the cached value. */
15131
15132 if (reload_completed)
15133 return offsets;
15134
666c27b9
KH
15135 /* Initially this is the size of the local variables. It will translated
15136 into an offset once we have determined the size of preceding data. */
5848830f
PB
15137 frame_size = ROUND_UP_WORD (get_frame_size ());
15138
15139 leaf = leaf_function_p ();
15140
15141 /* Space for variadic functions. */
38173d38 15142 offsets->saved_args = crtl->args.pretend_args_size;
5848830f 15143
5b3e6663 15144 /* In Thumb mode this is incorrect, but never used. */
35596784
AJ
15145 offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0) +
15146 arm_compute_static_chain_stack_bytes();
5848830f 15147
5b3e6663 15148 if (TARGET_32BIT)
095bb276 15149 {
5848830f 15150 unsigned int regno;
ef7112de 15151
954954d1
PB
15152 offsets->saved_regs_mask = arm_compute_save_reg_mask ();
15153 core_saved = bit_count (offsets->saved_regs_mask) * 4;
15154 saved = core_saved;
5a9335ef 15155
5848830f
PB
15156 /* We know that SP will be doubleword aligned on entry, and we must
15157 preserve that condition at any subroutine call. We also require the
15158 soft frame pointer to be doubleword aligned. */
15159
15160 if (TARGET_REALLY_IWMMXT)
9b66ebb1 15161 {
5848830f
PB
15162 /* Check for the call-saved iWMMXt registers. */
15163 for (regno = FIRST_IWMMXT_REGNUM;
15164 regno <= LAST_IWMMXT_REGNUM;
15165 regno++)
6fb5fa3c 15166 if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
5848830f
PB
15167 saved += 8;
15168 }
15169
15170 func_type = arm_current_func_type ();
15171 if (! IS_VOLATILE (func_type))
15172 {
15173 /* Space for saved FPA registers. */
15174 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
6fb5fa3c 15175 if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
5848830f
PB
15176 saved += 12;
15177
15178 /* Space for saved VFP registers. */
15179 if (TARGET_HARD_FLOAT && TARGET_VFP)
9728c9d1 15180 saved += arm_get_vfp_saved_size ();
9b66ebb1 15181 }
5848830f 15182 }
5b3e6663 15183 else /* TARGET_THUMB1 */
5848830f 15184 {
954954d1
PB
15185 offsets->saved_regs_mask = thumb1_compute_save_reg_mask ();
15186 core_saved = bit_count (offsets->saved_regs_mask) * 4;
15187 saved = core_saved;
5848830f 15188 if (TARGET_BACKTRACE)
57934c39 15189 saved += 16;
5848830f 15190 }
9b66ebb1 15191
5848830f 15192 /* Saved registers include the stack frame. */
35596784
AJ
15193 offsets->saved_regs = offsets->saved_args + saved +
15194 arm_compute_static_chain_stack_bytes();
a2503645 15195 offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
5848830f
PB
15196 /* A leaf function does not need any stack alignment if it has nothing
15197 on the stack. */
15198 if (leaf && frame_size == 0)
15199 {
15200 offsets->outgoing_args = offsets->soft_frame;
a3a531ec 15201 offsets->locals_base = offsets->soft_frame;
5848830f
PB
15202 return offsets;
15203 }
15204
15205 /* Ensure SFP has the correct alignment. */
15206 if (ARM_DOUBLEWORD_ALIGN
15207 && (offsets->soft_frame & 7))
954954d1
PB
15208 {
15209 offsets->soft_frame += 4;
15210 /* Try to align stack by pushing an extra reg. Don't bother doing this
15211 when there is a stack frame as the alignment will be rolled into
15212 the normal stack adjustment. */
38173d38 15213 if (frame_size + crtl->outgoing_args_size == 0)
954954d1
PB
15214 {
15215 int reg = -1;
15216
55b2829b
RE
15217 /* If it is safe to use r3, then do so. This sometimes
15218 generates better code on Thumb-2 by avoiding the need to
15219 use 32-bit push/pop instructions. */
15220 if (!crtl->tail_call_emit
fb2f8cf8
JZ
15221 && arm_size_return_regs () <= 12
15222 && (offsets->saved_regs_mask & (1 << 3)) == 0)
954954d1 15223 {
954954d1
PB
15224 reg = 3;
15225 }
55b2829b
RE
15226 else
15227 for (i = 4; i <= (TARGET_THUMB1 ? LAST_LO_REGNUM : 11); i++)
15228 {
15229 if ((offsets->saved_regs_mask & (1 << i)) == 0)
15230 {
15231 reg = i;
15232 break;
15233 }
15234 }
954954d1
PB
15235
15236 if (reg != -1)
15237 {
15238 offsets->saved_regs += 4;
15239 offsets->saved_regs_mask |= (1 << reg);
15240 }
15241 }
15242 }
5848830f 15243
2591db65
RE
15244 offsets->locals_base = offsets->soft_frame + frame_size;
15245 offsets->outgoing_args = (offsets->locals_base
38173d38 15246 + crtl->outgoing_args_size);
5848830f
PB
15247
15248 if (ARM_DOUBLEWORD_ALIGN)
15249 {
15250 /* Ensure SP remains doubleword aligned. */
15251 if (offsets->outgoing_args & 7)
15252 offsets->outgoing_args += 4;
e6d29d15 15253 gcc_assert (!(offsets->outgoing_args & 7));
095bb276
NC
15254 }
15255
5848830f
PB
15256 return offsets;
15257}
15258
15259
666c27b9 15260/* Calculate the relative offsets for the different stack pointers. Positive
5848830f
PB
15261 offsets are in the direction of stack growth. */
15262
b3f8d95d 15263HOST_WIDE_INT
5848830f
PB
15264arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
15265{
15266 arm_stack_offsets *offsets;
15267
15268 offsets = arm_get_frame_offsets ();
095bb276 15269
095bb276
NC
15270 /* OK, now we have enough information to compute the distances.
15271 There must be an entry in these switch tables for each pair
15272 of registers in ELIMINABLE_REGS, even if some of the entries
15273 seem to be redundant or useless. */
15274 switch (from)
15275 {
15276 case ARG_POINTER_REGNUM:
15277 switch (to)
15278 {
15279 case THUMB_HARD_FRAME_POINTER_REGNUM:
15280 return 0;
15281
15282 case FRAME_POINTER_REGNUM:
15283 /* This is the reverse of the soft frame pointer
15284 to hard frame pointer elimination below. */
5848830f 15285 return offsets->soft_frame - offsets->saved_args;
095bb276
NC
15286
15287 case ARM_HARD_FRAME_POINTER_REGNUM:
35596784
AJ
15288 /* This is only non-zero in the case where the static chain register
15289 is stored above the frame. */
15290 return offsets->frame - offsets->saved_args - 4;
095bb276
NC
15291
15292 case STACK_POINTER_REGNUM:
15293 /* If nothing has been pushed on the stack at all
15294 then this will return -4. This *is* correct! */
5848830f 15295 return offsets->outgoing_args - (offsets->saved_args + 4);
095bb276
NC
15296
15297 default:
e6d29d15 15298 gcc_unreachable ();
095bb276 15299 }
e6d29d15 15300 gcc_unreachable ();
095bb276
NC
15301
15302 case FRAME_POINTER_REGNUM:
15303 switch (to)
15304 {
15305 case THUMB_HARD_FRAME_POINTER_REGNUM:
15306 return 0;
15307
15308 case ARM_HARD_FRAME_POINTER_REGNUM:
15309 /* The hard frame pointer points to the top entry in the
15310 stack frame. The soft frame pointer to the bottom entry
15311 in the stack frame. If there is no stack frame at all,
15312 then they are identical. */
5848830f
PB
15313
15314 return offsets->frame - offsets->soft_frame;
095bb276
NC
15315
15316 case STACK_POINTER_REGNUM:
5848830f 15317 return offsets->outgoing_args - offsets->soft_frame;
095bb276
NC
15318
15319 default:
e6d29d15 15320 gcc_unreachable ();
095bb276 15321 }
e6d29d15 15322 gcc_unreachable ();
095bb276
NC
15323
15324 default:
15325 /* You cannot eliminate from the stack pointer.
15326 In theory you could eliminate from the hard frame
15327 pointer to the stack pointer, but this will never
15328 happen, since if a stack frame is not needed the
15329 hard frame pointer will never be used. */
e6d29d15 15330 gcc_unreachable ();
095bb276
NC
15331 }
15332}
15333
7b5cbb57
AS
15334/* Given FROM and TO register numbers, say whether this elimination is
15335 allowed. Frame pointer elimination is automatically handled.
15336
15337 All eliminations are permissible. Note that ARG_POINTER_REGNUM and
15338 HARD_FRAME_POINTER_REGNUM are in fact the same thing. If we need a frame
15339 pointer, we must eliminate FRAME_POINTER_REGNUM into
15340 HARD_FRAME_POINTER_REGNUM and not into STACK_POINTER_REGNUM or
15341 ARG_POINTER_REGNUM. */
15342
15343bool
15344arm_can_eliminate (const int from, const int to)
15345{
15346 return ((to == FRAME_POINTER_REGNUM && from == ARG_POINTER_REGNUM) ? false :
15347 (to == STACK_POINTER_REGNUM && frame_pointer_needed) ? false :
15348 (to == ARM_HARD_FRAME_POINTER_REGNUM && TARGET_THUMB) ? false :
15349 (to == THUMB_HARD_FRAME_POINTER_REGNUM && TARGET_ARM) ? false :
15350 true);
15351}
0977774b 15352
7a085dce 15353/* Emit RTL to save coprocessor registers on function entry. Returns the
5b3e6663
PB
15354 number of bytes pushed. */
15355
15356static int
15357arm_save_coproc_regs(void)
15358{
15359 int saved_size = 0;
15360 unsigned reg;
15361 unsigned start_reg;
15362 rtx insn;
15363
15364 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
6fb5fa3c 15365 if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
5b3e6663 15366 {
d8d55ac0 15367 insn = gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx);
5b3e6663
PB
15368 insn = gen_rtx_MEM (V2SImode, insn);
15369 insn = emit_set_insn (insn, gen_rtx_REG (V2SImode, reg));
15370 RTX_FRAME_RELATED_P (insn) = 1;
15371 saved_size += 8;
15372 }
15373
15374 /* Save any floating point call-saved registers used by this
15375 function. */
d79f3032 15376 if (TARGET_FPA_EMU2)
5b3e6663
PB
15377 {
15378 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
6fb5fa3c 15379 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
5b3e6663 15380 {
d8d55ac0 15381 insn = gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx);
5b3e6663
PB
15382 insn = gen_rtx_MEM (XFmode, insn);
15383 insn = emit_set_insn (insn, gen_rtx_REG (XFmode, reg));
15384 RTX_FRAME_RELATED_P (insn) = 1;
15385 saved_size += 12;
15386 }
15387 }
15388 else
15389 {
15390 start_reg = LAST_FPA_REGNUM;
15391
15392 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
15393 {
6fb5fa3c 15394 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
5b3e6663
PB
15395 {
15396 if (start_reg - reg == 3)
15397 {
15398 insn = emit_sfm (reg, 4);
15399 RTX_FRAME_RELATED_P (insn) = 1;
15400 saved_size += 48;
15401 start_reg = reg - 1;
15402 }
15403 }
15404 else
15405 {
15406 if (start_reg != reg)
15407 {
15408 insn = emit_sfm (reg + 1, start_reg - reg);
15409 RTX_FRAME_RELATED_P (insn) = 1;
15410 saved_size += (start_reg - reg) * 12;
15411 }
15412 start_reg = reg - 1;
15413 }
15414 }
15415
15416 if (start_reg != reg)
15417 {
15418 insn = emit_sfm (reg + 1, start_reg - reg);
15419 saved_size += (start_reg - reg) * 12;
15420 RTX_FRAME_RELATED_P (insn) = 1;
15421 }
15422 }
15423 if (TARGET_HARD_FLOAT && TARGET_VFP)
15424 {
15425 start_reg = FIRST_VFP_REGNUM;
15426
15427 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
15428 {
6fb5fa3c
DB
15429 if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
15430 && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
5b3e6663
PB
15431 {
15432 if (start_reg != reg)
15433 saved_size += vfp_emit_fstmd (start_reg,
15434 (reg - start_reg) / 2);
15435 start_reg = reg + 2;
15436 }
15437 }
15438 if (start_reg != reg)
15439 saved_size += vfp_emit_fstmd (start_reg,
15440 (reg - start_reg) / 2);
15441 }
15442 return saved_size;
15443}
15444
15445
15446/* Set the Thumb frame pointer from the stack pointer. */
15447
15448static void
15449thumb_set_frame_pointer (arm_stack_offsets *offsets)
15450{
15451 HOST_WIDE_INT amount;
15452 rtx insn, dwarf;
15453
15454 amount = offsets->outgoing_args - offsets->locals_base;
15455 if (amount < 1024)
15456 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15457 stack_pointer_rtx, GEN_INT (amount)));
15458 else
15459 {
15460 emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
f5c630c3
PB
15461 /* Thumb-2 RTL patterns expect sp as the first input. Thumb-1
15462 expects the first two operands to be the same. */
15463 if (TARGET_THUMB2)
15464 {
15465 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15466 stack_pointer_rtx,
15467 hard_frame_pointer_rtx));
15468 }
15469 else
15470 {
15471 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15472 hard_frame_pointer_rtx,
15473 stack_pointer_rtx));
15474 }
5b3e6663
PB
15475 dwarf = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
15476 plus_constant (stack_pointer_rtx, amount));
15477 RTX_FRAME_RELATED_P (dwarf) = 1;
bbbbb16a 15478 add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
5b3e6663
PB
15479 }
15480
15481 RTX_FRAME_RELATED_P (insn) = 1;
15482}
15483
15484/* Generate the prologue instructions for entry into an ARM or Thumb-2
15485 function. */
e2c671ba 15486void
e32bac5b 15487arm_expand_prologue (void)
e2c671ba 15488{
6d3d9133 15489 rtx amount;
2c849145 15490 rtx insn;
68dfd979 15491 rtx ip_rtx;
6d3d9133
NC
15492 unsigned long live_regs_mask;
15493 unsigned long func_type;
68dfd979 15494 int fp_offset = 0;
095bb276 15495 int saved_pretend_args = 0;
5848830f 15496 int saved_regs = 0;
b9705a21 15497 unsigned HOST_WIDE_INT args_to_push;
5848830f 15498 arm_stack_offsets *offsets;
d3236b4d 15499
6d3d9133 15500 func_type = arm_current_func_type ();
e2c671ba 15501
31fdb4d5 15502 /* Naked functions don't have prologues. */
6d3d9133 15503 if (IS_NAKED (func_type))
31fdb4d5
DE
15504 return;
15505
095bb276 15506 /* Make a copy of c_f_p_a_s as we may need to modify it locally. */
38173d38 15507 args_to_push = crtl->args.pretend_args_size;
f676971a 15508
6d3d9133 15509 /* Compute which register we will have to save onto the stack. */
954954d1
PB
15510 offsets = arm_get_frame_offsets ();
15511 live_regs_mask = offsets->saved_regs_mask;
e2c671ba 15512
68dfd979 15513 ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
d3236b4d 15514
5b3e6663
PB
15515 if (IS_STACKALIGN (func_type))
15516 {
15517 rtx dwarf;
15518 rtx r0;
15519 rtx r1;
15520 /* Handle a word-aligned stack pointer. We generate the following:
15521
15522 mov r0, sp
15523 bic r1, r0, #7
15524 mov sp, r1
15525 <save and restore r0 in normal prologue/epilogue>
15526 mov sp, r0
15527 bx lr
15528
15529 The unwinder doesn't need to know about the stack realignment.
15530 Just tell it we saved SP in r0. */
15531 gcc_assert (TARGET_THUMB2 && !arm_arch_notm && args_to_push == 0);
15532
15533 r0 = gen_rtx_REG (SImode, 0);
15534 r1 = gen_rtx_REG (SImode, 1);
44bfa35b
NF
15535 /* Use a real rtvec rather than NULL_RTVEC so the rest of the
15536 compiler won't choke. */
15537 dwarf = gen_rtx_UNSPEC (SImode, rtvec_alloc (0), UNSPEC_STACK_ALIGN);
5b3e6663
PB
15538 dwarf = gen_rtx_SET (VOIDmode, r0, dwarf);
15539 insn = gen_movsi (r0, stack_pointer_rtx);
15540 RTX_FRAME_RELATED_P (insn) = 1;
bbbbb16a 15541 add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
5b3e6663
PB
15542 emit_insn (insn);
15543 emit_insn (gen_andsi3 (r1, r0, GEN_INT (~(HOST_WIDE_INT)7)));
15544 emit_insn (gen_movsi (stack_pointer_rtx, r1));
15545 }
15546
ec6237e4
PB
15547 /* For APCS frames, if IP register is clobbered
15548 when creating frame, save that register in a special
15549 way. */
15550 if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
e2c671ba 15551 {
7b8b8ade
NC
15552 if (IS_INTERRUPT (func_type))
15553 {
15554 /* Interrupt functions must not corrupt any registers.
15555 Creating a frame pointer however, corrupts the IP
15556 register, so we must push it first. */
15557 insn = emit_multi_reg_push (1 << IP_REGNUM);
121308d4
NC
15558
15559 /* Do not set RTX_FRAME_RELATED_P on this insn.
15560 The dwarf stack unwinding code only wants to see one
15561 stack decrement per function, and this is not it. If
15562 this instruction is labeled as being part of the frame
15563 creation sequence then dwarf2out_frame_debug_expr will
e6d29d15 15564 die when it encounters the assignment of IP to FP
121308d4
NC
15565 later on, since the use of SP here establishes SP as
15566 the CFA register and not IP.
15567
15568 Anyway this instruction is not really part of the stack
15569 frame creation although it is part of the prologue. */
7b8b8ade
NC
15570 }
15571 else if (IS_NESTED (func_type))
68dfd979
NC
15572 {
15573 /* The Static chain register is the same as the IP register
15574 used as a scratch register during stack frame creation.
15575 To get around this need to find somewhere to store IP
15576 whilst the frame is being created. We try the following
15577 places in order:
f676971a 15578
6d3d9133 15579 1. The last argument register.
68dfd979
NC
15580 2. A slot on the stack above the frame. (This only
15581 works if the function is not a varargs function).
095bb276
NC
15582 3. Register r3, after pushing the argument registers
15583 onto the stack.
6d3d9133 15584
34ce3d7b
JM
15585 Note - we only need to tell the dwarf2 backend about the SP
15586 adjustment in the second variant; the static chain register
15587 doesn't need to be unwound, as it doesn't contain a value
15588 inherited from the caller. */
d3236b4d 15589
6fb5fa3c 15590 if (df_regs_ever_live_p (3) == false)
d66437c5 15591 insn = emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
095bb276 15592 else if (args_to_push == 0)
68dfd979 15593 {
f0b4bdd5
RE
15594 rtx dwarf;
15595
35596784
AJ
15596 gcc_assert(arm_compute_static_chain_stack_bytes() == 4);
15597 saved_regs += 4;
15598
d66437c5
RE
15599 insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
15600 insn = emit_set_insn (gen_frame_mem (SImode, insn), ip_rtx);
68dfd979 15601 fp_offset = 4;
34ce3d7b
JM
15602
15603 /* Just tell the dwarf backend that we adjusted SP. */
15604 dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
d66437c5
RE
15605 plus_constant (stack_pointer_rtx,
15606 -fp_offset));
34ce3d7b 15607 RTX_FRAME_RELATED_P (insn) = 1;
bbbbb16a 15608 add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
68dfd979
NC
15609 }
15610 else
095bb276
NC
15611 {
15612 /* Store the args on the stack. */
3cb66fd7 15613 if (cfun->machine->uses_anonymous_args)
095bb276
NC
15614 insn = emit_multi_reg_push
15615 ((0xf0 >> (args_to_push / 4)) & 0xf);
15616 else
15617 insn = emit_insn
f676971a 15618 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
095bb276
NC
15619 GEN_INT (- args_to_push)));
15620
15621 RTX_FRAME_RELATED_P (insn) = 1;
15622
15623 saved_pretend_args = 1;
15624 fp_offset = args_to_push;
15625 args_to_push = 0;
15626
15627 /* Now reuse r3 to preserve IP. */
d66437c5 15628 emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
095bb276 15629 }
68dfd979
NC
15630 }
15631
d66437c5
RE
15632 insn = emit_set_insn (ip_rtx,
15633 plus_constant (stack_pointer_rtx, fp_offset));
8e56560e 15634 RTX_FRAME_RELATED_P (insn) = 1;
e2c671ba
RE
15635 }
15636
095bb276 15637 if (args_to_push)
e2c671ba 15638 {
6d3d9133 15639 /* Push the argument registers, or reserve space for them. */
3cb66fd7 15640 if (cfun->machine->uses_anonymous_args)
2c849145 15641 insn = emit_multi_reg_push
095bb276 15642 ((0xf0 >> (args_to_push / 4)) & 0xf);
e2c671ba 15643 else
2c849145 15644 insn = emit_insn
f676971a 15645 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
095bb276 15646 GEN_INT (- args_to_push)));
2c849145 15647 RTX_FRAME_RELATED_P (insn) = 1;
e2c671ba
RE
15648 }
15649
06bea5aa 15650 /* If this is an interrupt service routine, and the link register
ec6237e4
PB
15651 is going to be pushed, and we're not generating extra
15652 push of IP (needed when frame is needed and frame layout if apcs),
06bea5aa
NC
15653 subtracting four from LR now will mean that the function return
15654 can be done with a single instruction. */
3a7731fd 15655 if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
06bea5aa 15656 && (live_regs_mask & (1 << LR_REGNUM)) != 0
ec6237e4 15657 && !(frame_pointer_needed && TARGET_APCS_FRAME)
a15908a4 15658 && TARGET_ARM)
d66437c5
RE
15659 {
15660 rtx lr = gen_rtx_REG (SImode, LR_REGNUM);
15661
15662 emit_set_insn (lr, plus_constant (lr, -4));
15663 }
3a7731fd 15664
e2c671ba
RE
15665 if (live_regs_mask)
15666 {
5848830f 15667 saved_regs += bit_count (live_regs_mask) * 4;
954954d1
PB
15668 if (optimize_size && !frame_pointer_needed
15669 && saved_regs == offsets->saved_regs - offsets->saved_args)
15670 {
15671 /* If no coprocessor registers are being pushed and we don't have
15672 to worry about a frame pointer then push extra registers to
15673 create the stack frame. This is done is a way that does not
15674 alter the frame layout, so is independent of the epilogue. */
15675 int n;
15676 int frame;
15677 n = 0;
15678 while (n < 8 && (live_regs_mask & (1 << n)) == 0)
15679 n++;
15680 frame = offsets->outgoing_args - (offsets->saved_args + saved_regs);
15681 if (frame && n * 4 >= frame)
15682 {
15683 n = frame / 4;
15684 live_regs_mask |= (1 << n) - 1;
15685 saved_regs += frame;
15686 }
15687 }
15688 insn = emit_multi_reg_push (live_regs_mask);
2c849145 15689 RTX_FRAME_RELATED_P (insn) = 1;
e2c671ba 15690 }
d5b7b3ae 15691
6d3d9133 15692 if (! IS_VOLATILE (func_type))
5b3e6663 15693 saved_regs += arm_save_coproc_regs ();
b111229a 15694
5b3e6663
PB
15695 if (frame_pointer_needed && TARGET_ARM)
15696 {
15697 /* Create the new frame pointer. */
ec6237e4 15698 if (TARGET_APCS_FRAME)
9b66ebb1 15699 {
5b3e6663
PB
15700 insn = GEN_INT (-(4 + args_to_push + fp_offset));
15701 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
15702 RTX_FRAME_RELATED_P (insn) = 1;
9b66ebb1 15703
5b3e6663 15704 if (IS_NESTED (func_type))
9b66ebb1 15705 {
5b3e6663 15706 /* Recover the static chain register. */
6fb5fa3c 15707 if (!df_regs_ever_live_p (3)
5b3e6663
PB
15708 || saved_pretend_args)
15709 insn = gen_rtx_REG (SImode, 3);
38173d38 15710 else /* if (crtl->args.pretend_args_size == 0) */
9b66ebb1 15711 {
5b3e6663
PB
15712 insn = plus_constant (hard_frame_pointer_rtx, 4);
15713 insn = gen_frame_mem (SImode, insn);
9b66ebb1 15714 }
5b3e6663
PB
15715 emit_set_insn (ip_rtx, insn);
15716 /* Add a USE to stop propagate_one_insn() from barfing. */
15717 emit_insn (gen_prologue_use (ip_rtx));
9b66ebb1 15718 }
68dfd979 15719 }
ec6237e4
PB
15720 else
15721 {
15722 insn = GEN_INT (saved_regs - 4);
15723 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15724 stack_pointer_rtx, insn));
15725 RTX_FRAME_RELATED_P (insn) = 1;
15726 }
2c849145 15727 }
e2c671ba 15728
5848830f 15729 if (offsets->outgoing_args != offsets->saved_args + saved_regs)
e2c671ba 15730 {
745b9093
JM
15731 /* This add can produce multiple insns for a large constant, so we
15732 need to get tricky. */
15733 rtx last = get_last_insn ();
5848830f
PB
15734
15735 amount = GEN_INT (offsets->saved_args + saved_regs
15736 - offsets->outgoing_args);
15737
2c849145
JM
15738 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
15739 amount));
745b9093
JM
15740 do
15741 {
15742 last = last ? NEXT_INSN (last) : get_insns ();
15743 RTX_FRAME_RELATED_P (last) = 1;
15744 }
15745 while (last != insn);
e04c2d6c
RE
15746
15747 /* If the frame pointer is needed, emit a special barrier that
15748 will prevent the scheduler from moving stores to the frame
15749 before the stack adjustment. */
15750 if (frame_pointer_needed)
3894f59e
RE
15751 insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
15752 hard_frame_pointer_rtx));
e2c671ba
RE
15753 }
15754
876f13b0 15755
5b3e6663
PB
15756 if (frame_pointer_needed && TARGET_THUMB2)
15757 thumb_set_frame_pointer (offsets);
15758
020a4035 15759 if (flag_pic && arm_pic_register != INVALID_REGNUM)
5b3e6663
PB
15760 {
15761 unsigned long mask;
15762
15763 mask = live_regs_mask;
15764 mask &= THUMB2_WORK_REGS;
15765 if (!IS_NESTED (func_type))
15766 mask |= (1 << IP_REGNUM);
15767 arm_load_pic_register (mask);
15768 }
876f13b0 15769
e2c671ba 15770 /* If we are profiling, make sure no instructions are scheduled before
f5a1b0d2 15771 the call to mcount. Similarly if the user has requested no
74d9c39f
DJ
15772 scheduling in the prolog. Similarly if we want non-call exceptions
15773 using the EABI unwinder, to prevent faulting instructions from being
15774 swapped with a stack adjustment. */
e3b5732b 15775 if (crtl->profile || !TARGET_SCHED_PROLOG
f0a0390e
RH
15776 || (arm_except_unwind_info () == UI_TARGET
15777 && cfun->can_throw_non_call_exceptions))
e2c671ba 15778 emit_insn (gen_blockage ());
6f7ebcbb
NC
15779
15780 /* If the link register is being kept alive, with the return address in it,
15781 then make sure that it does not get reused by the ce2 pass. */
15782 if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
6fb5fa3c 15783 cfun->machine->lr_save_eliminated = 1;
e2c671ba 15784}
cce8749e 15785\f
5b3e6663
PB
15786/* Print condition code to STREAM. Helper function for arm_print_operand. */
15787static void
15788arm_print_condition (FILE *stream)
15789{
15790 if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
15791 {
15792 /* Branch conversion is not implemented for Thumb-2. */
15793 if (TARGET_THUMB)
15794 {
15795 output_operand_lossage ("predicated Thumb instruction");
15796 return;
15797 }
15798 if (current_insn_predicate != NULL)
15799 {
15800 output_operand_lossage
15801 ("predicated instruction in conditional sequence");
15802 return;
15803 }
15804
15805 fputs (arm_condition_codes[arm_current_cc], stream);
15806 }
15807 else if (current_insn_predicate)
15808 {
15809 enum arm_cond_code code;
15810
15811 if (TARGET_THUMB1)
15812 {
15813 output_operand_lossage ("predicated Thumb instruction");
15814 return;
15815 }
15816
15817 code = get_arm_condition_code (current_insn_predicate);
15818 fputs (arm_condition_codes[code], stream);
15819 }
15820}
15821
15822
9997d19d
RE
15823/* If CODE is 'd', then the X is a condition operand and the instruction
15824 should only be executed if the condition is true.
ddd5a7c1 15825 if CODE is 'D', then the X is a condition operand and the instruction
9997d19d
RE
15826 should only be executed if the condition is false: however, if the mode
15827 of the comparison is CCFPEmode, then always execute the instruction -- we
15828 do this because in these circumstances !GE does not necessarily imply LT;
15829 in these cases the instruction pattern will take care to make sure that
15830 an instruction containing %d will follow, thereby undoing the effects of
ddd5a7c1 15831 doing this instruction unconditionally.
9997d19d
RE
15832 If CODE is 'N' then X is a floating point operand that must be negated
15833 before output.
15834 If CODE is 'B' then output a bitwise inverted value of X (a const int).
15835 If X is a REG and CODE is `M', output a ldm/stm style multi-reg. */
944442bb 15836static void
e32bac5b 15837arm_print_operand (FILE *stream, rtx x, int code)
9997d19d
RE
15838{
15839 switch (code)
15840 {
15841 case '@':
f3139301 15842 fputs (ASM_COMMENT_START, stream);
9997d19d
RE
15843 return;
15844
d5b7b3ae
RE
15845 case '_':
15846 fputs (user_label_prefix, stream);
15847 return;
f676971a 15848
9997d19d 15849 case '|':
f3139301 15850 fputs (REGISTER_PREFIX, stream);
9997d19d
RE
15851 return;
15852
15853 case '?':
5b3e6663
PB
15854 arm_print_condition (stream);
15855 return;
cca0a211 15856
5b3e6663
PB
15857 case '(':
15858 /* Nothing in unified syntax, otherwise the current condition code. */
15859 if (!TARGET_UNIFIED_ASM)
15860 arm_print_condition (stream);
15861 break;
15862
15863 case ')':
15864 /* The current condition code in unified syntax, otherwise nothing. */
15865 if (TARGET_UNIFIED_ASM)
15866 arm_print_condition (stream);
15867 break;
15868
15869 case '.':
15870 /* The current condition code for a condition code setting instruction.
7a085dce 15871 Preceded by 's' in unified syntax, otherwise followed by 's'. */
5b3e6663
PB
15872 if (TARGET_UNIFIED_ASM)
15873 {
15874 fputc('s', stream);
15875 arm_print_condition (stream);
cca0a211 15876 }
5b3e6663 15877 else
cca0a211 15878 {
5b3e6663
PB
15879 arm_print_condition (stream);
15880 fputc('s', stream);
cca0a211 15881 }
9997d19d
RE
15882 return;
15883
5b3e6663
PB
15884 case '!':
15885 /* If the instruction is conditionally executed then print
15886 the current condition code, otherwise print 's'. */
15887 gcc_assert (TARGET_THUMB2 && TARGET_UNIFIED_ASM);
15888 if (current_insn_predicate)
15889 arm_print_condition (stream);
15890 else
15891 fputc('s', stream);
15892 break;
15893
88f77cba 15894 /* %# is a "break" sequence. It doesn't output anything, but is used to
cea618ac 15895 separate e.g. operand numbers from following text, if that text consists
88f77cba
JB
15896 of further digits which we don't want to be part of the operand
15897 number. */
15898 case '#':
15899 return;
15900
9997d19d
RE
15901 case 'N':
15902 {
15903 REAL_VALUE_TYPE r;
15904 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
d49b6e1e 15905 r = real_value_negate (&r);
9997d19d
RE
15906 fprintf (stream, "%s", fp_const_from_val (&r));
15907 }
15908 return;
15909
571191af 15910 /* An integer or symbol address without a preceding # sign. */
88f77cba 15911 case 'c':
571191af
PB
15912 switch (GET_CODE (x))
15913 {
15914 case CONST_INT:
15915 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
15916 break;
15917
15918 case SYMBOL_REF:
15919 output_addr_const (stream, x);
15920 break;
15921
15922 default:
15923 gcc_unreachable ();
15924 }
88f77cba
JB
15925 return;
15926
9997d19d
RE
15927 case 'B':
15928 if (GET_CODE (x) == CONST_INT)
4bc74ece
NC
15929 {
15930 HOST_WIDE_INT val;
5895f793 15931 val = ARM_SIGN_EXTEND (~INTVAL (x));
36ba9cb8 15932 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
4bc74ece 15933 }
9997d19d
RE
15934 else
15935 {
15936 putc ('~', stream);
15937 output_addr_const (stream, x);
15938 }
15939 return;
15940
5b3e6663
PB
15941 case 'L':
15942 /* The low 16 bits of an immediate constant. */
15943 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL(x) & 0xffff);
15944 return;
15945
9997d19d
RE
15946 case 'i':
15947 fprintf (stream, "%s", arithmetic_instr (x, 1));
15948 return;
15949
9b6b54e2
NC
15950 /* Truncate Cirrus shift counts. */
15951 case 's':
15952 if (GET_CODE (x) == CONST_INT)
15953 {
15954 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
15955 return;
15956 }
15957 arm_print_operand (stream, x, 0);
15958 return;
15959
9997d19d
RE
15960 case 'I':
15961 fprintf (stream, "%s", arithmetic_instr (x, 0));
15962 return;
15963
15964 case 'S':
15965 {
15966 HOST_WIDE_INT val;
beed32b8
RE
15967 const char *shift;
15968
15969 if (!shift_operator (x, SImode))
15970 {
15971 output_operand_lossage ("invalid shift operand");
15972 break;
15973 }
15974
15975 shift = shift_op (x, &val);
9997d19d 15976
e2c671ba
RE
15977 if (shift)
15978 {
beed32b8 15979 fprintf (stream, ", %s ", shift);
e2c671ba
RE
15980 if (val == -1)
15981 arm_print_operand (stream, XEXP (x, 1), 0);
15982 else
4a0a75dd 15983 fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
e2c671ba 15984 }
9997d19d
RE
15985 }
15986 return;
15987
d5b7b3ae 15988 /* An explanation of the 'Q', 'R' and 'H' register operands:
f676971a 15989
d5b7b3ae
RE
15990 In a pair of registers containing a DI or DF value the 'Q'
15991 operand returns the register number of the register containing
093354e0 15992 the least significant part of the value. The 'R' operand returns
d5b7b3ae
RE
15993 the register number of the register containing the most
15994 significant part of the value.
f676971a 15995
d5b7b3ae
RE
15996 The 'H' operand returns the higher of the two register numbers.
15997 On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
093354e0 15998 same as the 'Q' operand, since the most significant part of the
d5b7b3ae
RE
15999 value is held in the lower number register. The reverse is true
16000 on systems where WORDS_BIG_ENDIAN is false.
f676971a 16001
d5b7b3ae
RE
16002 The purpose of these operands is to distinguish between cases
16003 where the endian-ness of the values is important (for example
16004 when they are added together), and cases where the endian-ness
16005 is irrelevant, but the order of register operations is important.
16006 For example when loading a value from memory into a register
16007 pair, the endian-ness does not matter. Provided that the value
16008 from the lower memory address is put into the lower numbered
16009 register, and the value from the higher address is put into the
16010 higher numbered register, the load will work regardless of whether
16011 the value being loaded is big-wordian or little-wordian. The
16012 order of the two register loads can matter however, if the address
16013 of the memory location is actually held in one of the registers
73160ba9
DJ
16014 being overwritten by the load.
16015
16016 The 'Q' and 'R' constraints are also available for 64-bit
16017 constants. */
c1c2bc04 16018 case 'Q':
73160ba9
DJ
16019 if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
16020 {
16021 rtx part = gen_lowpart (SImode, x);
16022 fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, INTVAL (part));
16023 return;
16024 }
16025
22de4c3d
RE
16026 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16027 {
16028 output_operand_lossage ("invalid operand for code '%c'", code);
16029 return;
16030 }
16031
d5b7b3ae 16032 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
c1c2bc04
RE
16033 return;
16034
9997d19d 16035 case 'R':
73160ba9
DJ
16036 if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
16037 {
16038 enum machine_mode mode = GET_MODE (x);
16039 rtx part;
16040
16041 if (mode == VOIDmode)
16042 mode = DImode;
16043 part = gen_highpart_mode (SImode, mode, x);
16044 fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, INTVAL (part));
16045 return;
16046 }
16047
22de4c3d
RE
16048 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16049 {
16050 output_operand_lossage ("invalid operand for code '%c'", code);
16051 return;
16052 }
16053
d5b7b3ae
RE
16054 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
16055 return;
16056
16057 case 'H':
22de4c3d
RE
16058 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16059 {
16060 output_operand_lossage ("invalid operand for code '%c'", code);
16061 return;
16062 }
16063
d5b7b3ae 16064 asm_fprintf (stream, "%r", REGNO (x) + 1);
9997d19d
RE
16065 return;
16066
88f77cba
JB
16067 case 'J':
16068 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16069 {
16070 output_operand_lossage ("invalid operand for code '%c'", code);
16071 return;
16072 }
16073
16074 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 3 : 2));
16075 return;
16076
16077 case 'K':
16078 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16079 {
16080 output_operand_lossage ("invalid operand for code '%c'", code);
16081 return;
16082 }
16083
16084 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 2 : 3));
16085 return;
16086
9997d19d 16087 case 'm':
f676971a 16088 asm_fprintf (stream, "%r",
d5b7b3ae
RE
16089 GET_CODE (XEXP (x, 0)) == REG
16090 ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
9997d19d
RE
16091 return;
16092
16093 case 'M':
dd18ae56 16094 asm_fprintf (stream, "{%r-%r}",
d5b7b3ae 16095 REGNO (x),
e9d7b180 16096 REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
9997d19d
RE
16097 return;
16098
88f77cba
JB
16099 /* Like 'M', but writing doubleword vector registers, for use by Neon
16100 insns. */
16101 case 'h':
16102 {
16103 int regno = (REGNO (x) - FIRST_VFP_REGNUM) / 2;
16104 int numregs = ARM_NUM_REGS (GET_MODE (x)) / 2;
16105 if (numregs == 1)
16106 asm_fprintf (stream, "{d%d}", regno);
16107 else
16108 asm_fprintf (stream, "{d%d-d%d}", regno, regno + numregs - 1);
16109 }
16110 return;
16111
9997d19d 16112 case 'd':
64e92a26
RE
16113 /* CONST_TRUE_RTX means always -- that's the default. */
16114 if (x == const_true_rtx)
d5b7b3ae 16115 return;
f676971a 16116
22de4c3d
RE
16117 if (!COMPARISON_P (x))
16118 {
16119 output_operand_lossage ("invalid operand for code '%c'", code);
16120 return;
16121 }
16122
defc0463
RE
16123 fputs (arm_condition_codes[get_arm_condition_code (x)],
16124 stream);
9997d19d
RE
16125 return;
16126
16127 case 'D':
112cdef5 16128 /* CONST_TRUE_RTX means not always -- i.e. never. We shouldn't ever
64e92a26
RE
16129 want to do that. */
16130 if (x == const_true_rtx)
22de4c3d 16131 {
4dad0aca 16132 output_operand_lossage ("instruction never executed");
22de4c3d
RE
16133 return;
16134 }
16135 if (!COMPARISON_P (x))
16136 {
16137 output_operand_lossage ("invalid operand for code '%c'", code);
16138 return;
16139 }
d5b7b3ae 16140
defc0463
RE
16141 fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
16142 (get_arm_condition_code (x))],
16143 stream);
9997d19d
RE
16144 return;
16145
9b6b54e2
NC
16146 /* Cirrus registers can be accessed in a variety of ways:
16147 single floating point (f)
16148 double floating point (d)
16149 32bit integer (fx)
16150 64bit integer (dx). */
16151 case 'W': /* Cirrus register in F mode. */
16152 case 'X': /* Cirrus register in D mode. */
16153 case 'Y': /* Cirrus register in FX mode. */
16154 case 'Z': /* Cirrus register in DX mode. */
e6d29d15
NS
16155 gcc_assert (GET_CODE (x) == REG
16156 && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
9b6b54e2
NC
16157
16158 fprintf (stream, "mv%s%s",
16159 code == 'W' ? "f"
16160 : code == 'X' ? "d"
16161 : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
16162
16163 return;
16164
16165 /* Print cirrus register in the mode specified by the register's mode. */
16166 case 'V':
16167 {
16168 int mode = GET_MODE (x);
16169
16170 if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
22de4c3d
RE
16171 {
16172 output_operand_lossage ("invalid operand for code '%c'", code);
16173 return;
16174 }
9b6b54e2
NC
16175
16176 fprintf (stream, "mv%s%s",
16177 mode == DFmode ? "d"
16178 : mode == SImode ? "fx"
16179 : mode == DImode ? "dx"
16180 : "f", reg_names[REGNO (x)] + 2);
16181
16182 return;
16183 }
16184
5a9335ef
NC
16185 case 'U':
16186 if (GET_CODE (x) != REG
16187 || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
16188 || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
16189 /* Bad value for wCG register number. */
22de4c3d
RE
16190 {
16191 output_operand_lossage ("invalid operand for code '%c'", code);
16192 return;
16193 }
16194
5a9335ef
NC
16195 else
16196 fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
16197 return;
16198
16199 /* Print an iWMMXt control register name. */
16200 case 'w':
16201 if (GET_CODE (x) != CONST_INT
16202 || INTVAL (x) < 0
16203 || INTVAL (x) >= 16)
16204 /* Bad value for wC register number. */
22de4c3d
RE
16205 {
16206 output_operand_lossage ("invalid operand for code '%c'", code);
16207 return;
16208 }
16209
5a9335ef
NC
16210 else
16211 {
16212 static const char * wc_reg_names [16] =
16213 {
16214 "wCID", "wCon", "wCSSF", "wCASF",
16215 "wC4", "wC5", "wC6", "wC7",
16216 "wCGR0", "wCGR1", "wCGR2", "wCGR3",
16217 "wC12", "wC13", "wC14", "wC15"
16218 };
f676971a 16219
5a9335ef
NC
16220 fprintf (stream, wc_reg_names [INTVAL (x)]);
16221 }
16222 return;
16223
e0dc3601
PB
16224 /* Print the high single-precision register of a VFP double-precision
16225 register. */
16226 case 'p':
16227 {
16228 int mode = GET_MODE (x);
16229 int regno;
16230
16231 if (GET_MODE_SIZE (mode) != 8 || GET_CODE (x) != REG)
16232 {
16233 output_operand_lossage ("invalid operand for code '%c'", code);
16234 return;
16235 }
16236
16237 regno = REGNO (x);
16238 if (!VFP_REGNO_OK_FOR_DOUBLE (regno))
16239 {
16240 output_operand_lossage ("invalid operand for code '%c'", code);
16241 return;
16242 }
16243
16244 fprintf (stream, "s%d", regno - FIRST_VFP_REGNUM + 1);
16245 }
16246 return;
16247
88f77cba 16248 /* Print a VFP/Neon double precision or quad precision register name. */
9b66ebb1 16249 case 'P':
88f77cba 16250 case 'q':
9b66ebb1
PB
16251 {
16252 int mode = GET_MODE (x);
88f77cba
JB
16253 int is_quad = (code == 'q');
16254 int regno;
9b66ebb1 16255
88f77cba 16256 if (GET_MODE_SIZE (mode) != (is_quad ? 16 : 8))
22de4c3d
RE
16257 {
16258 output_operand_lossage ("invalid operand for code '%c'", code);
16259 return;
16260 }
9b66ebb1
PB
16261
16262 if (GET_CODE (x) != REG
16263 || !IS_VFP_REGNUM (REGNO (x)))
22de4c3d
RE
16264 {
16265 output_operand_lossage ("invalid operand for code '%c'", code);
16266 return;
16267 }
9b66ebb1 16268
88f77cba
JB
16269 regno = REGNO (x);
16270 if ((is_quad && !NEON_REGNO_OK_FOR_QUAD (regno))
16271 || (!is_quad && !VFP_REGNO_OK_FOR_DOUBLE (regno)))
22de4c3d
RE
16272 {
16273 output_operand_lossage ("invalid operand for code '%c'", code);
16274 return;
16275 }
9b66ebb1 16276
88f77cba
JB
16277 fprintf (stream, "%c%d", is_quad ? 'q' : 'd',
16278 (regno - FIRST_VFP_REGNUM) >> (is_quad ? 2 : 1));
16279 }
16280 return;
16281
16282 /* These two codes print the low/high doubleword register of a Neon quad
16283 register, respectively. For pair-structure types, can also print
16284 low/high quadword registers. */
16285 case 'e':
16286 case 'f':
16287 {
16288 int mode = GET_MODE (x);
16289 int regno;
16290
16291 if ((GET_MODE_SIZE (mode) != 16
16292 && GET_MODE_SIZE (mode) != 32) || GET_CODE (x) != REG)
16293 {
16294 output_operand_lossage ("invalid operand for code '%c'", code);
16295 return;
16296 }
16297
16298 regno = REGNO (x);
16299 if (!NEON_REGNO_OK_FOR_QUAD (regno))
16300 {
16301 output_operand_lossage ("invalid operand for code '%c'", code);
16302 return;
16303 }
16304
16305 if (GET_MODE_SIZE (mode) == 16)
16306 fprintf (stream, "d%d", ((regno - FIRST_VFP_REGNUM) >> 1)
16307 + (code == 'f' ? 1 : 0));
16308 else
16309 fprintf (stream, "q%d", ((regno - FIRST_VFP_REGNUM) >> 2)
16310 + (code == 'f' ? 1 : 0));
9b66ebb1
PB
16311 }
16312 return;
16313
f1adb0a9
JB
16314 /* Print a VFPv3 floating-point constant, represented as an integer
16315 index. */
16316 case 'G':
16317 {
16318 int index = vfp3_const_double_index (x);
16319 gcc_assert (index != -1);
16320 fprintf (stream, "%d", index);
16321 }
16322 return;
16323
88f77cba
JB
16324 /* Print bits representing opcode features for Neon.
16325
16326 Bit 0 is 1 for signed, 0 for unsigned. Floats count as signed
16327 and polynomials as unsigned.
16328
16329 Bit 1 is 1 for floats and polynomials, 0 for ordinary integers.
16330
16331 Bit 2 is 1 for rounding functions, 0 otherwise. */
16332
16333 /* Identify the type as 's', 'u', 'p' or 'f'. */
16334 case 'T':
16335 {
16336 HOST_WIDE_INT bits = INTVAL (x);
16337 fputc ("uspf"[bits & 3], stream);
16338 }
16339 return;
16340
16341 /* Likewise, but signed and unsigned integers are both 'i'. */
16342 case 'F':
16343 {
16344 HOST_WIDE_INT bits = INTVAL (x);
16345 fputc ("iipf"[bits & 3], stream);
16346 }
16347 return;
16348
16349 /* As for 'T', but emit 'u' instead of 'p'. */
16350 case 't':
16351 {
16352 HOST_WIDE_INT bits = INTVAL (x);
16353 fputc ("usuf"[bits & 3], stream);
16354 }
16355 return;
16356
16357 /* Bit 2: rounding (vs none). */
16358 case 'O':
16359 {
16360 HOST_WIDE_INT bits = INTVAL (x);
16361 fputs ((bits & 4) != 0 ? "r" : "", stream);
16362 }
16363 return;
16364
dc34db56
PB
16365 /* Memory operand for vld1/vst1 instruction. */
16366 case 'A':
16367 {
16368 rtx addr;
16369 bool postinc = FALSE;
c452684d
JB
16370 unsigned align, modesize, align_bits;
16371
dc34db56
PB
16372 gcc_assert (GET_CODE (x) == MEM);
16373 addr = XEXP (x, 0);
16374 if (GET_CODE (addr) == POST_INC)
16375 {
16376 postinc = 1;
16377 addr = XEXP (addr, 0);
16378 }
c452684d
JB
16379 asm_fprintf (stream, "[%r", REGNO (addr));
16380
16381 /* We know the alignment of this access, so we can emit a hint in the
16382 instruction (for some alignments) as an aid to the memory subsystem
16383 of the target. */
16384 align = MEM_ALIGN (x) >> 3;
16385 modesize = GET_MODE_SIZE (GET_MODE (x));
16386
16387 /* Only certain alignment specifiers are supported by the hardware. */
16388 if (modesize == 16 && (align % 32) == 0)
16389 align_bits = 256;
16390 else if ((modesize == 8 || modesize == 16) && (align % 16) == 0)
16391 align_bits = 128;
16392 else if ((align % 8) == 0)
16393 align_bits = 64;
16394 else
16395 align_bits = 0;
16396
16397 if (align_bits != 0)
16398 asm_fprintf (stream, ":%d", align_bits);
16399
16400 asm_fprintf (stream, "]");
16401
dc34db56
PB
16402 if (postinc)
16403 fputs("!", stream);
16404 }
16405 return;
16406
029e79eb
MS
16407 case 'C':
16408 {
16409 rtx addr;
16410
16411 gcc_assert (GET_CODE (x) == MEM);
16412 addr = XEXP (x, 0);
16413 gcc_assert (GET_CODE (addr) == REG);
16414 asm_fprintf (stream, "[%r]", REGNO (addr));
16415 }
16416 return;
16417
814a4c3b
DJ
16418 /* Translate an S register number into a D register number and element index. */
16419 case 'y':
16420 {
16421 int mode = GET_MODE (x);
16422 int regno;
16423
16424 if (GET_MODE_SIZE (mode) != 4 || GET_CODE (x) != REG)
16425 {
16426 output_operand_lossage ("invalid operand for code '%c'", code);
16427 return;
16428 }
16429
16430 regno = REGNO (x);
16431 if (!VFP_REGNO_OK_FOR_SINGLE (regno))
16432 {
16433 output_operand_lossage ("invalid operand for code '%c'", code);
16434 return;
16435 }
16436
16437 regno = regno - FIRST_VFP_REGNUM;
16438 fprintf (stream, "d%d[%d]", regno / 2, regno % 2);
16439 }
16440 return;
16441
0fd8c3ad
SL
16442 /* Register specifier for vld1.16/vst1.16. Translate the S register
16443 number into a D register number and element index. */
16444 case 'z':
16445 {
16446 int mode = GET_MODE (x);
16447 int regno;
16448
16449 if (GET_MODE_SIZE (mode) != 2 || GET_CODE (x) != REG)
16450 {
16451 output_operand_lossage ("invalid operand for code '%c'", code);
16452 return;
16453 }
16454
16455 regno = REGNO (x);
16456 if (!VFP_REGNO_OK_FOR_SINGLE (regno))
16457 {
16458 output_operand_lossage ("invalid operand for code '%c'", code);
16459 return;
16460 }
16461
16462 regno = regno - FIRST_VFP_REGNUM;
16463 fprintf (stream, "d%d[%d]", regno/2, ((regno % 2) ? 2 : 0));
16464 }
16465 return;
16466
9997d19d
RE
16467 default:
16468 if (x == 0)
22de4c3d
RE
16469 {
16470 output_operand_lossage ("missing operand");
16471 return;
16472 }
9997d19d 16473
e6d29d15 16474 switch (GET_CODE (x))
9997d19d 16475 {
e6d29d15
NS
16476 case REG:
16477 asm_fprintf (stream, "%r", REGNO (x));
16478 break;
16479
16480 case MEM:
9997d19d
RE
16481 output_memory_reference_mode = GET_MODE (x);
16482 output_address (XEXP (x, 0));
e6d29d15
NS
16483 break;
16484
16485 case CONST_DOUBLE:
88f77cba
JB
16486 if (TARGET_NEON)
16487 {
16488 char fpstr[20];
16489 real_to_decimal (fpstr, CONST_DOUBLE_REAL_VALUE (x),
16490 sizeof (fpstr), 0, 1);
16491 fprintf (stream, "#%s", fpstr);
16492 }
16493 else
16494 fprintf (stream, "#%s", fp_immediate_constant (x));
e6d29d15
NS
16495 break;
16496
16497 default:
16498 gcc_assert (GET_CODE (x) != NEG);
9997d19d 16499 fputc ('#', stream);
d58bc084
NS
16500 if (GET_CODE (x) == HIGH)
16501 {
16502 fputs (":lower16:", stream);
16503 x = XEXP (x, 0);
16504 }
16505
9997d19d 16506 output_addr_const (stream, x);
e6d29d15 16507 break;
9997d19d
RE
16508 }
16509 }
16510}
cce8749e 16511\f
944442bb
NF
16512/* Target hook for printing a memory address. */
16513static void
16514arm_print_operand_address (FILE *stream, rtx x)
16515{
16516 if (TARGET_32BIT)
16517 {
16518 int is_minus = GET_CODE (x) == MINUS;
16519
16520 if (GET_CODE (x) == REG)
16521 asm_fprintf (stream, "[%r, #0]", REGNO (x));
16522 else if (GET_CODE (x) == PLUS || is_minus)
16523 {
16524 rtx base = XEXP (x, 0);
16525 rtx index = XEXP (x, 1);
16526 HOST_WIDE_INT offset = 0;
16527 if (GET_CODE (base) != REG
16528 || (GET_CODE (index) == REG && REGNO (index) == SP_REGNUM))
16529 {
16530 /* Ensure that BASE is a register. */
16531 /* (one of them must be). */
16532 /* Also ensure the SP is not used as in index register. */
16533 rtx temp = base;
16534 base = index;
16535 index = temp;
16536 }
16537 switch (GET_CODE (index))
16538 {
16539 case CONST_INT:
16540 offset = INTVAL (index);
16541 if (is_minus)
16542 offset = -offset;
16543 asm_fprintf (stream, "[%r, #%wd]",
16544 REGNO (base), offset);
16545 break;
16546
16547 case REG:
16548 asm_fprintf (stream, "[%r, %s%r]",
16549 REGNO (base), is_minus ? "-" : "",
16550 REGNO (index));
16551 break;
16552
16553 case MULT:
16554 case ASHIFTRT:
16555 case LSHIFTRT:
16556 case ASHIFT:
16557 case ROTATERT:
16558 {
16559 asm_fprintf (stream, "[%r, %s%r",
16560 REGNO (base), is_minus ? "-" : "",
16561 REGNO (XEXP (index, 0)));
16562 arm_print_operand (stream, index, 'S');
16563 fputs ("]", stream);
16564 break;
16565 }
16566
16567 default:
16568 gcc_unreachable ();
16569 }
16570 }
16571 else if (GET_CODE (x) == PRE_INC || GET_CODE (x) == POST_INC
16572 || GET_CODE (x) == PRE_DEC || GET_CODE (x) == POST_DEC)
16573 {
16574 extern enum machine_mode output_memory_reference_mode;
16575
16576 gcc_assert (GET_CODE (XEXP (x, 0)) == REG);
16577
16578 if (GET_CODE (x) == PRE_DEC || GET_CODE (x) == PRE_INC)
16579 asm_fprintf (stream, "[%r, #%s%d]!",
16580 REGNO (XEXP (x, 0)),
16581 GET_CODE (x) == PRE_DEC ? "-" : "",
16582 GET_MODE_SIZE (output_memory_reference_mode));
16583 else
16584 asm_fprintf (stream, "[%r], #%s%d",
16585 REGNO (XEXP (x, 0)),
16586 GET_CODE (x) == POST_DEC ? "-" : "",
16587 GET_MODE_SIZE (output_memory_reference_mode));
16588 }
16589 else if (GET_CODE (x) == PRE_MODIFY)
16590 {
16591 asm_fprintf (stream, "[%r, ", REGNO (XEXP (x, 0)));
16592 if (GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
16593 asm_fprintf (stream, "#%wd]!",
16594 INTVAL (XEXP (XEXP (x, 1), 1)));
16595 else
16596 asm_fprintf (stream, "%r]!",
16597 REGNO (XEXP (XEXP (x, 1), 1)));
16598 }
16599 else if (GET_CODE (x) == POST_MODIFY)
16600 {
16601 asm_fprintf (stream, "[%r], ", REGNO (XEXP (x, 0)));
16602 if (GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
16603 asm_fprintf (stream, "#%wd",
16604 INTVAL (XEXP (XEXP (x, 1), 1)));
16605 else
16606 asm_fprintf (stream, "%r",
16607 REGNO (XEXP (XEXP (x, 1), 1)));
16608 }
16609 else output_addr_const (stream, x);
16610 }
16611 else
16612 {
16613 if (GET_CODE (x) == REG)
16614 asm_fprintf (stream, "[%r]", REGNO (x));
16615 else if (GET_CODE (x) == POST_INC)
16616 asm_fprintf (stream, "%r!", REGNO (XEXP (x, 0)));
16617 else if (GET_CODE (x) == PLUS)
16618 {
16619 gcc_assert (GET_CODE (XEXP (x, 0)) == REG);
16620 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
16621 asm_fprintf (stream, "[%r, #%wd]",
16622 REGNO (XEXP (x, 0)),
16623 INTVAL (XEXP (x, 1)));
16624 else
16625 asm_fprintf (stream, "[%r, %r]",
16626 REGNO (XEXP (x, 0)),
16627 REGNO (XEXP (x, 1)));
16628 }
16629 else
16630 output_addr_const (stream, x);
16631 }
16632}
16633\f
16634/* Target hook for indicating whether a punctuation character for
16635 TARGET_PRINT_OPERAND is valid. */
16636static bool
16637arm_print_operand_punct_valid_p (unsigned char code)
16638{
16639 return (code == '@' || code == '|' || code == '.'
16640 || code == '(' || code == ')' || code == '#'
16641 || (TARGET_32BIT && (code == '?'))
16642 || (TARGET_THUMB2 && (code == '!'))
16643 || (TARGET_THUMB && (code == '_')));
16644}
16645\f
301d03af
RS
16646/* Target hook for assembling integer objects. The ARM version needs to
16647 handle word-sized values specially. */
301d03af 16648static bool
e32bac5b 16649arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
301d03af 16650{
88f77cba
JB
16651 enum machine_mode mode;
16652
301d03af
RS
16653 if (size == UNITS_PER_WORD && aligned_p)
16654 {
16655 fputs ("\t.word\t", asm_out_file);
16656 output_addr_const (asm_out_file, x);
16657
16658 /* Mark symbols as position independent. We only do this in the
d6b4baa4 16659 .text segment, not in the .data segment. */
301d03af
RS
16660 if (NEED_GOT_RELOC && flag_pic && making_const_table &&
16661 (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
16662 {
9403b7f7
RS
16663 /* See legitimize_pic_address for an explanation of the
16664 TARGET_VXWORKS_RTP check. */
16665 if (TARGET_VXWORKS_RTP
16666 || (GET_CODE (x) == SYMBOL_REF && !SYMBOL_REF_LOCAL_P (x)))
301d03af 16667 fputs ("(GOT)", asm_out_file);
9403b7f7
RS
16668 else
16669 fputs ("(GOTOFF)", asm_out_file);
301d03af
RS
16670 }
16671 fputc ('\n', asm_out_file);
16672 return true;
16673 }
1d6e90ac 16674
88f77cba
JB
16675 mode = GET_MODE (x);
16676
16677 if (arm_vector_mode_supported_p (mode))
5a9335ef
NC
16678 {
16679 int i, units;
16680
e6d29d15 16681 gcc_assert (GET_CODE (x) == CONST_VECTOR);
5a9335ef
NC
16682
16683 units = CONST_VECTOR_NUNITS (x);
88f77cba 16684 size = GET_MODE_SIZE (GET_MODE_INNER (mode));
5a9335ef 16685
88f77cba
JB
16686 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
16687 for (i = 0; i < units; i++)
16688 {
874d42b9 16689 rtx elt = CONST_VECTOR_ELT (x, i);
88f77cba
JB
16690 assemble_integer
16691 (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
16692 }
16693 else
16694 for (i = 0; i < units; i++)
16695 {
16696 rtx elt = CONST_VECTOR_ELT (x, i);
16697 REAL_VALUE_TYPE rval;
5a9335ef 16698
88f77cba
JB
16699 REAL_VALUE_FROM_CONST_DOUBLE (rval, elt);
16700
16701 assemble_real
16702 (rval, GET_MODE_INNER (mode),
16703 i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT);
16704 }
5a9335ef
NC
16705
16706 return true;
16707 }
16708
301d03af
RS
16709 return default_assemble_integer (x, size, aligned_p);
16710}
7abc66b1 16711
7abc66b1 16712static void
9f296620 16713arm_elf_asm_cdtor (rtx symbol, int priority, bool is_ctor)
7abc66b1 16714{
50603eed
PB
16715 section *s;
16716
7abc66b1
JB
16717 if (!TARGET_AAPCS_BASED)
16718 {
9f296620
MM
16719 (is_ctor ?
16720 default_named_section_asm_out_constructor
16721 : default_named_section_asm_out_destructor) (symbol, priority);
7abc66b1
JB
16722 return;
16723 }
16724
16725 /* Put these in the .init_array section, using a special relocation. */
50603eed
PB
16726 if (priority != DEFAULT_INIT_PRIORITY)
16727 {
16728 char buf[18];
9f296620
MM
16729 sprintf (buf, "%s.%.5u",
16730 is_ctor ? ".init_array" : ".fini_array",
16731 priority);
50603eed
PB
16732 s = get_section (buf, SECTION_WRITE, NULL_TREE);
16733 }
9f296620 16734 else if (is_ctor)
50603eed 16735 s = ctors_section;
9f296620
MM
16736 else
16737 s = dtors_section;
50603eed
PB
16738
16739 switch_to_section (s);
7abc66b1
JB
16740 assemble_align (POINTER_SIZE);
16741 fputs ("\t.word\t", asm_out_file);
16742 output_addr_const (asm_out_file, symbol);
16743 fputs ("(target1)\n", asm_out_file);
16744}
9f296620
MM
16745
16746/* Add a function to the list of static constructors. */
16747
16748static void
16749arm_elf_asm_constructor (rtx symbol, int priority)
16750{
16751 arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/true);
16752}
16753
16754/* Add a function to the list of static destructors. */
16755
16756static void
16757arm_elf_asm_destructor (rtx symbol, int priority)
16758{
16759 arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/false);
16760}
301d03af 16761\f
cce8749e
CH
16762/* A finite state machine takes care of noticing whether or not instructions
16763 can be conditionally executed, and thus decrease execution time and code
16764 size by deleting branch instructions. The fsm is controlled by
16765 final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE. */
16766
16767/* The state of the fsm controlling condition codes are:
16768 0: normal, do nothing special
16769 1: make ASM_OUTPUT_OPCODE not output this instruction
16770 2: make ASM_OUTPUT_OPCODE not output this instruction
16771 3: make instructions conditional
16772 4: make instructions conditional
16773
16774 State transitions (state->state by whom under condition):
16775 0 -> 1 final_prescan_insn if the `target' is a label
16776 0 -> 2 final_prescan_insn if the `target' is an unconditional branch
16777 1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
16778 2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
4977bab6 16779 3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
cce8749e
CH
16780 (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
16781 4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
16782 (the target insn is arm_target_insn).
16783
ff9940b0
RE
16784 If the jump clobbers the conditions then we use states 2 and 4.
16785
16786 A similar thing can be done with conditional return insns.
16787
cce8749e
CH
16788 XXX In case the `target' is an unconditional branch, this conditionalising
16789 of the instructions always reduces code size, but not always execution
16790 time. But then, I want to reduce the code size to somewhere near what
16791 /bin/cc produces. */
16792
5b3e6663
PB
16793/* In addition to this, state is maintained for Thumb-2 COND_EXEC
16794 instructions. When a COND_EXEC instruction is seen the subsequent
16795 instructions are scanned so that multiple conditional instructions can be
16796 combined into a single IT block. arm_condexec_count and arm_condexec_mask
16797 specify the length and true/false mask for the IT block. These will be
16798 decremented/zeroed by arm_asm_output_opcode as the insns are output. */
16799
cce8749e
CH
16800/* Returns the index of the ARM condition code string in
16801 `arm_condition_codes'. COMPARISON should be an rtx like
16802 `(eq (...) (...))'. */
84ed5e79 16803static enum arm_cond_code
e32bac5b 16804get_arm_condition_code (rtx comparison)
cce8749e 16805{
5165176d 16806 enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
81f40b79 16807 enum arm_cond_code code;
1d6e90ac 16808 enum rtx_code comp_code = GET_CODE (comparison);
5165176d
RE
16809
16810 if (GET_MODE_CLASS (mode) != MODE_CC)
84ed5e79 16811 mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
5165176d
RE
16812 XEXP (comparison, 1));
16813
16814 switch (mode)
cce8749e 16815 {
84ed5e79
RE
16816 case CC_DNEmode: code = ARM_NE; goto dominance;
16817 case CC_DEQmode: code = ARM_EQ; goto dominance;
16818 case CC_DGEmode: code = ARM_GE; goto dominance;
16819 case CC_DGTmode: code = ARM_GT; goto dominance;
16820 case CC_DLEmode: code = ARM_LE; goto dominance;
16821 case CC_DLTmode: code = ARM_LT; goto dominance;
16822 case CC_DGEUmode: code = ARM_CS; goto dominance;
16823 case CC_DGTUmode: code = ARM_HI; goto dominance;
16824 case CC_DLEUmode: code = ARM_LS; goto dominance;
16825 case CC_DLTUmode: code = ARM_CC;
16826
16827 dominance:
e6d29d15 16828 gcc_assert (comp_code == EQ || comp_code == NE);
84ed5e79
RE
16829
16830 if (comp_code == EQ)
16831 return ARM_INVERSE_CONDITION_CODE (code);
16832 return code;
16833
5165176d 16834 case CC_NOOVmode:
84ed5e79 16835 switch (comp_code)
5165176d 16836 {
84ed5e79
RE
16837 case NE: return ARM_NE;
16838 case EQ: return ARM_EQ;
16839 case GE: return ARM_PL;
16840 case LT: return ARM_MI;
e6d29d15 16841 default: gcc_unreachable ();
5165176d
RE
16842 }
16843
16844 case CC_Zmode:
84ed5e79 16845 switch (comp_code)
5165176d 16846 {
84ed5e79
RE
16847 case NE: return ARM_NE;
16848 case EQ: return ARM_EQ;
e6d29d15 16849 default: gcc_unreachable ();
5165176d
RE
16850 }
16851
defc0463
RE
16852 case CC_Nmode:
16853 switch (comp_code)
16854 {
16855 case NE: return ARM_MI;
16856 case EQ: return ARM_PL;
e6d29d15 16857 default: gcc_unreachable ();
defc0463
RE
16858 }
16859
5165176d 16860 case CCFPEmode:
e45b72c4
RE
16861 case CCFPmode:
16862 /* These encodings assume that AC=1 in the FPA system control
16863 byte. This allows us to handle all cases except UNEQ and
16864 LTGT. */
84ed5e79
RE
16865 switch (comp_code)
16866 {
16867 case GE: return ARM_GE;
16868 case GT: return ARM_GT;
16869 case LE: return ARM_LS;
16870 case LT: return ARM_MI;
e45b72c4
RE
16871 case NE: return ARM_NE;
16872 case EQ: return ARM_EQ;
16873 case ORDERED: return ARM_VC;
16874 case UNORDERED: return ARM_VS;
16875 case UNLT: return ARM_LT;
16876 case UNLE: return ARM_LE;
16877 case UNGT: return ARM_HI;
16878 case UNGE: return ARM_PL;
16879 /* UNEQ and LTGT do not have a representation. */
16880 case UNEQ: /* Fall through. */
16881 case LTGT: /* Fall through. */
e6d29d15 16882 default: gcc_unreachable ();
84ed5e79
RE
16883 }
16884
16885 case CC_SWPmode:
16886 switch (comp_code)
16887 {
16888 case NE: return ARM_NE;
16889 case EQ: return ARM_EQ;
16890 case GE: return ARM_LE;
16891 case GT: return ARM_LT;
16892 case LE: return ARM_GE;
16893 case LT: return ARM_GT;
16894 case GEU: return ARM_LS;
16895 case GTU: return ARM_CC;
16896 case LEU: return ARM_CS;
16897 case LTU: return ARM_HI;
e6d29d15 16898 default: gcc_unreachable ();
84ed5e79
RE
16899 }
16900
bd9c7e23
RE
16901 case CC_Cmode:
16902 switch (comp_code)
18e8200f
BS
16903 {
16904 case LTU: return ARM_CS;
16905 case GEU: return ARM_CC;
16906 default: gcc_unreachable ();
16907 }
16908
73160ba9
DJ
16909 case CC_CZmode:
16910 switch (comp_code)
16911 {
16912 case NE: return ARM_NE;
16913 case EQ: return ARM_EQ;
16914 case GEU: return ARM_CS;
16915 case GTU: return ARM_HI;
16916 case LEU: return ARM_LS;
16917 case LTU: return ARM_CC;
16918 default: gcc_unreachable ();
16919 }
16920
16921 case CC_NCVmode:
16922 switch (comp_code)
16923 {
16924 case GE: return ARM_GE;
16925 case LT: return ARM_LT;
16926 case GEU: return ARM_CS;
16927 case LTU: return ARM_CC;
16928 default: gcc_unreachable ();
16929 }
16930
5165176d 16931 case CCmode:
84ed5e79 16932 switch (comp_code)
5165176d 16933 {
84ed5e79
RE
16934 case NE: return ARM_NE;
16935 case EQ: return ARM_EQ;
16936 case GE: return ARM_GE;
16937 case GT: return ARM_GT;
16938 case LE: return ARM_LE;
16939 case LT: return ARM_LT;
16940 case GEU: return ARM_CS;
16941 case GTU: return ARM_HI;
16942 case LEU: return ARM_LS;
16943 case LTU: return ARM_CC;
e6d29d15 16944 default: gcc_unreachable ();
5165176d
RE
16945 }
16946
e6d29d15 16947 default: gcc_unreachable ();
cce8749e 16948 }
f3bb6135 16949}
cce8749e 16950
44c7bd63 16951/* Tell arm_asm_output_opcode to output IT blocks for conditionally executed
5b3e6663
PB
16952 instructions. */
16953void
16954thumb2_final_prescan_insn (rtx insn)
16955{
16956 rtx first_insn = insn;
16957 rtx body = PATTERN (insn);
16958 rtx predicate;
16959 enum arm_cond_code code;
16960 int n;
16961 int mask;
16962
16963 /* Remove the previous insn from the count of insns to be output. */
16964 if (arm_condexec_count)
16965 arm_condexec_count--;
16966
16967 /* Nothing to do if we are already inside a conditional block. */
16968 if (arm_condexec_count)
16969 return;
16970
16971 if (GET_CODE (body) != COND_EXEC)
16972 return;
16973
16974 /* Conditional jumps are implemented directly. */
16975 if (GET_CODE (insn) == JUMP_INSN)
16976 return;
16977
16978 predicate = COND_EXEC_TEST (body);
16979 arm_current_cc = get_arm_condition_code (predicate);
16980
16981 n = get_attr_ce_count (insn);
16982 arm_condexec_count = 1;
16983 arm_condexec_mask = (1 << n) - 1;
16984 arm_condexec_masklen = n;
16985 /* See if subsequent instructions can be combined into the same block. */
16986 for (;;)
16987 {
16988 insn = next_nonnote_insn (insn);
16989
16990 /* Jumping into the middle of an IT block is illegal, so a label or
16991 barrier terminates the block. */
16992 if (GET_CODE (insn) != INSN && GET_CODE(insn) != JUMP_INSN)
16993 break;
16994
16995 body = PATTERN (insn);
16996 /* USE and CLOBBER aren't really insns, so just skip them. */
16997 if (GET_CODE (body) == USE
16998 || GET_CODE (body) == CLOBBER)
5b0202af 16999 continue;
5b3e6663 17000
7a085dce 17001 /* ??? Recognize conditional jumps, and combine them with IT blocks. */
5b3e6663
PB
17002 if (GET_CODE (body) != COND_EXEC)
17003 break;
17004 /* Allow up to 4 conditionally executed instructions in a block. */
17005 n = get_attr_ce_count (insn);
17006 if (arm_condexec_masklen + n > 4)
17007 break;
17008
17009 predicate = COND_EXEC_TEST (body);
17010 code = get_arm_condition_code (predicate);
17011 mask = (1 << n) - 1;
17012 if (arm_current_cc == code)
17013 arm_condexec_mask |= (mask << arm_condexec_masklen);
17014 else if (arm_current_cc != ARM_INVERSE_CONDITION_CODE(code))
17015 break;
17016
17017 arm_condexec_count++;
17018 arm_condexec_masklen += n;
17019
17020 /* A jump must be the last instruction in a conditional block. */
17021 if (GET_CODE(insn) == JUMP_INSN)
17022 break;
17023 }
17024 /* Restore recog_data (getting the attributes of other insns can
17025 destroy this array, but final.c assumes that it remains intact
17026 across this call). */
17027 extract_constrain_insn_cached (first_insn);
17028}
17029
cce8749e 17030void
e32bac5b 17031arm_final_prescan_insn (rtx insn)
cce8749e
CH
17032{
17033 /* BODY will hold the body of INSN. */
1d6e90ac 17034 rtx body = PATTERN (insn);
cce8749e
CH
17035
17036 /* This will be 1 if trying to repeat the trick, and things need to be
17037 reversed if it appears to fail. */
17038 int reverse = 0;
17039
6354dc9b 17040 /* If we start with a return insn, we only succeed if we find another one. */
ff9940b0 17041 int seeking_return = 0;
f676971a 17042
cce8749e
CH
17043 /* START_INSN will hold the insn from where we start looking. This is the
17044 first insn after the following code_label if REVERSE is true. */
17045 rtx start_insn = insn;
17046
17047 /* If in state 4, check if the target branch is reached, in order to
17048 change back to state 0. */
17049 if (arm_ccfsm_state == 4)
17050 {
17051 if (insn == arm_target_insn)
f5a1b0d2
NC
17052 {
17053 arm_target_insn = NULL;
17054 arm_ccfsm_state = 0;
17055 }
cce8749e
CH
17056 return;
17057 }
17058
17059 /* If in state 3, it is possible to repeat the trick, if this insn is an
17060 unconditional branch to a label, and immediately following this branch
17061 is the previous target label which is only used once, and the label this
17062 branch jumps to is not too far off. */
17063 if (arm_ccfsm_state == 3)
17064 {
17065 if (simplejump_p (insn))
17066 {
17067 start_insn = next_nonnote_insn (start_insn);
17068 if (GET_CODE (start_insn) == BARRIER)
17069 {
17070 /* XXX Isn't this always a barrier? */
17071 start_insn = next_nonnote_insn (start_insn);
17072 }
17073 if (GET_CODE (start_insn) == CODE_LABEL
17074 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
17075 && LABEL_NUSES (start_insn) == 1)
17076 reverse = TRUE;
17077 else
17078 return;
17079 }
ff9940b0
RE
17080 else if (GET_CODE (body) == RETURN)
17081 {
17082 start_insn = next_nonnote_insn (start_insn);
17083 if (GET_CODE (start_insn) == BARRIER)
17084 start_insn = next_nonnote_insn (start_insn);
17085 if (GET_CODE (start_insn) == CODE_LABEL
17086 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
17087 && LABEL_NUSES (start_insn) == 1)
17088 {
17089 reverse = TRUE;
17090 seeking_return = 1;
17091 }
17092 else
17093 return;
17094 }
cce8749e
CH
17095 else
17096 return;
17097 }
17098
e6d29d15 17099 gcc_assert (!arm_ccfsm_state || reverse);
cce8749e
CH
17100 if (GET_CODE (insn) != JUMP_INSN)
17101 return;
17102
f676971a 17103 /* This jump might be paralleled with a clobber of the condition codes
ff9940b0
RE
17104 the jump should always come first */
17105 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
17106 body = XVECEXP (body, 0, 0);
17107
cce8749e
CH
17108 if (reverse
17109 || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
17110 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
17111 {
bd9c7e23
RE
17112 int insns_skipped;
17113 int fail = FALSE, succeed = FALSE;
cce8749e
CH
17114 /* Flag which part of the IF_THEN_ELSE is the LABEL_REF. */
17115 int then_not_else = TRUE;
ff9940b0 17116 rtx this_insn = start_insn, label = 0;
cce8749e
CH
17117
17118 /* Register the insn jumped to. */
17119 if (reverse)
ff9940b0
RE
17120 {
17121 if (!seeking_return)
17122 label = XEXP (SET_SRC (body), 0);
17123 }
cce8749e
CH
17124 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
17125 label = XEXP (XEXP (SET_SRC (body), 1), 0);
17126 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
17127 {
17128 label = XEXP (XEXP (SET_SRC (body), 2), 0);
17129 then_not_else = FALSE;
17130 }
ff9940b0
RE
17131 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
17132 seeking_return = 1;
17133 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
17134 {
17135 seeking_return = 1;
17136 then_not_else = FALSE;
17137 }
cce8749e 17138 else
e6d29d15 17139 gcc_unreachable ();
cce8749e
CH
17140
17141 /* See how many insns this branch skips, and what kind of insns. If all
17142 insns are okay, and the label or unconditional branch to the same
17143 label is not too far away, succeed. */
17144 for (insns_skipped = 0;
b36ba79f 17145 !fail && !succeed && insns_skipped++ < max_insns_skipped;)
cce8749e
CH
17146 {
17147 rtx scanbody;
17148
17149 this_insn = next_nonnote_insn (this_insn);
17150 if (!this_insn)
17151 break;
17152
cce8749e
CH
17153 switch (GET_CODE (this_insn))
17154 {
17155 case CODE_LABEL:
17156 /* Succeed if it is the target label, otherwise fail since
17157 control falls in from somewhere else. */
17158 if (this_insn == label)
17159 {
accbd151 17160 arm_ccfsm_state = 1;
cce8749e
CH
17161 succeed = TRUE;
17162 }
17163 else
17164 fail = TRUE;
17165 break;
17166
ff9940b0 17167 case BARRIER:
cce8749e 17168 /* Succeed if the following insn is the target label.
f676971a
EC
17169 Otherwise fail.
17170 If return insns are used then the last insn in a function
6354dc9b 17171 will be a barrier. */
cce8749e 17172 this_insn = next_nonnote_insn (this_insn);
ff9940b0 17173 if (this_insn && this_insn == label)
cce8749e 17174 {
accbd151 17175 arm_ccfsm_state = 1;
cce8749e
CH
17176 succeed = TRUE;
17177 }
17178 else
17179 fail = TRUE;
17180 break;
17181
ff9940b0 17182 case CALL_INSN:
68d560d4
RE
17183 /* The AAPCS says that conditional calls should not be
17184 used since they make interworking inefficient (the
17185 linker can't transform BL<cond> into BLX). That's
17186 only a problem if the machine has BLX. */
17187 if (arm_arch5)
17188 {
17189 fail = TRUE;
17190 break;
17191 }
17192
61f0ccff
RE
17193 /* Succeed if the following insn is the target label, or
17194 if the following two insns are a barrier and the
17195 target label. */
17196 this_insn = next_nonnote_insn (this_insn);
17197 if (this_insn && GET_CODE (this_insn) == BARRIER)
17198 this_insn = next_nonnote_insn (this_insn);
bd9c7e23 17199
61f0ccff
RE
17200 if (this_insn && this_insn == label
17201 && insns_skipped < max_insns_skipped)
17202 {
accbd151 17203 arm_ccfsm_state = 1;
61f0ccff 17204 succeed = TRUE;
bd9c7e23 17205 }
61f0ccff
RE
17206 else
17207 fail = TRUE;
ff9940b0 17208 break;
2b835d68 17209
cce8749e
CH
17210 case JUMP_INSN:
17211 /* If this is an unconditional branch to the same label, succeed.
17212 If it is to another label, do nothing. If it is conditional,
17213 fail. */
e32bac5b
RE
17214 /* XXX Probably, the tests for SET and the PC are
17215 unnecessary. */
cce8749e 17216
ed4c4348 17217 scanbody = PATTERN (this_insn);
ff9940b0
RE
17218 if (GET_CODE (scanbody) == SET
17219 && GET_CODE (SET_DEST (scanbody)) == PC)
cce8749e
CH
17220 {
17221 if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
17222 && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
17223 {
17224 arm_ccfsm_state = 2;
17225 succeed = TRUE;
17226 }
17227 else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
17228 fail = TRUE;
17229 }
112cdef5 17230 /* Fail if a conditional return is undesirable (e.g. on a
b36ba79f
RE
17231 StrongARM), but still allow this if optimizing for size. */
17232 else if (GET_CODE (scanbody) == RETURN
a72d4945 17233 && !use_return_insn (TRUE, NULL)
5895f793 17234 && !optimize_size)
b36ba79f 17235 fail = TRUE;
ff9940b0
RE
17236 else if (GET_CODE (scanbody) == RETURN
17237 && seeking_return)
17238 {
17239 arm_ccfsm_state = 2;
17240 succeed = TRUE;
17241 }
17242 else if (GET_CODE (scanbody) == PARALLEL)
17243 {
17244 switch (get_attr_conds (this_insn))
17245 {
17246 case CONDS_NOCOND:
17247 break;
17248 default:
17249 fail = TRUE;
17250 break;
17251 }
17252 }
4e67550b 17253 else
112cdef5 17254 fail = TRUE; /* Unrecognized jump (e.g. epilogue). */
4e67550b 17255
cce8749e
CH
17256 break;
17257
17258 case INSN:
ff9940b0
RE
17259 /* Instructions using or affecting the condition codes make it
17260 fail. */
ed4c4348 17261 scanbody = PATTERN (this_insn);
5895f793
RE
17262 if (!(GET_CODE (scanbody) == SET
17263 || GET_CODE (scanbody) == PARALLEL)
74641843 17264 || get_attr_conds (this_insn) != CONDS_NOCOND)
cce8749e 17265 fail = TRUE;
9b6b54e2
NC
17266
17267 /* A conditional cirrus instruction must be followed by
17268 a non Cirrus instruction. However, since we
17269 conditionalize instructions in this function and by
17270 the time we get here we can't add instructions
17271 (nops), because shorten_branches() has already been
17272 called, we will disable conditionalizing Cirrus
17273 instructions to be safe. */
17274 if (GET_CODE (scanbody) != USE
17275 && GET_CODE (scanbody) != CLOBBER
f0375c66 17276 && get_attr_cirrus (this_insn) != CIRRUS_NOT)
9b6b54e2 17277 fail = TRUE;
cce8749e
CH
17278 break;
17279
17280 default:
17281 break;
17282 }
17283 }
17284 if (succeed)
17285 {
ff9940b0 17286 if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
cce8749e 17287 arm_target_label = CODE_LABEL_NUMBER (label);
e6d29d15 17288 else
ff9940b0 17289 {
e6d29d15 17290 gcc_assert (seeking_return || arm_ccfsm_state == 2);
e0b92319 17291
ff9940b0
RE
17292 while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
17293 {
17294 this_insn = next_nonnote_insn (this_insn);
e6d29d15
NS
17295 gcc_assert (!this_insn
17296 || (GET_CODE (this_insn) != BARRIER
17297 && GET_CODE (this_insn) != CODE_LABEL));
ff9940b0
RE
17298 }
17299 if (!this_insn)
17300 {
d6b4baa4 17301 /* Oh, dear! we ran off the end.. give up. */
5b3e6663 17302 extract_constrain_insn_cached (insn);
ff9940b0 17303 arm_ccfsm_state = 0;
abaa26e5 17304 arm_target_insn = NULL;
ff9940b0
RE
17305 return;
17306 }
17307 arm_target_insn = this_insn;
17308 }
accbd151
PB
17309
17310 /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
17311 what it was. */
17312 if (!reverse)
17313 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body), 0));
cce8749e 17314
cce8749e
CH
17315 if (reverse || then_not_else)
17316 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
17317 }
f676971a 17318
1ccbefce 17319 /* Restore recog_data (getting the attributes of other insns can
ff9940b0 17320 destroy this array, but final.c assumes that it remains intact
5b3e6663
PB
17321 across this call. */
17322 extract_constrain_insn_cached (insn);
17323 }
17324}
17325
17326/* Output IT instructions. */
17327void
17328thumb2_asm_output_opcode (FILE * stream)
17329{
17330 char buff[5];
17331 int n;
17332
17333 if (arm_condexec_mask)
17334 {
17335 for (n = 0; n < arm_condexec_masklen; n++)
17336 buff[n] = (arm_condexec_mask & (1 << n)) ? 't' : 'e';
17337 buff[n] = 0;
17338 asm_fprintf(stream, "i%s\t%s\n\t", buff,
17339 arm_condition_codes[arm_current_cc]);
17340 arm_condexec_mask = 0;
cce8749e 17341 }
f3bb6135 17342}
cce8749e 17343
4b02997f 17344/* Returns true if REGNO is a valid register
21b5653c 17345 for holding a quantity of type MODE. */
4b02997f 17346int
e32bac5b 17347arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
4b02997f
NC
17348{
17349 if (GET_MODE_CLASS (mode) == MODE_CC)
a6a5de04
RE
17350 return (regno == CC_REGNUM
17351 || (TARGET_HARD_FLOAT && TARGET_VFP
17352 && regno == VFPCC_REGNUM));
f676971a 17353
5b3e6663 17354 if (TARGET_THUMB1)
4b02997f
NC
17355 /* For the Thumb we only allow values bigger than SImode in
17356 registers 0 - 6, so that there is always a second low
17357 register available to hold the upper part of the value.
17358 We probably we ought to ensure that the register is the
17359 start of an even numbered register pair. */
e9d7b180 17360 return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
4b02997f 17361
a6a5de04
RE
17362 if (TARGET_HARD_FLOAT && TARGET_MAVERICK
17363 && IS_CIRRUS_REGNUM (regno))
9b6b54e2
NC
17364 /* We have outlawed SI values in Cirrus registers because they
17365 reside in the lower 32 bits, but SF values reside in the
17366 upper 32 bits. This causes gcc all sorts of grief. We can't
17367 even split the registers into pairs because Cirrus SI values
17368 get sign extended to 64bits-- aldyh. */
17369 return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
17370
a6a5de04
RE
17371 if (TARGET_HARD_FLOAT && TARGET_VFP
17372 && IS_VFP_REGNUM (regno))
9b66ebb1
PB
17373 {
17374 if (mode == SFmode || mode == SImode)
f1adb0a9 17375 return VFP_REGNO_OK_FOR_SINGLE (regno);
9b66ebb1 17376
9b66ebb1 17377 if (mode == DFmode)
f1adb0a9 17378 return VFP_REGNO_OK_FOR_DOUBLE (regno);
88f77cba 17379
0fd8c3ad 17380 /* VFP registers can hold HFmode values, but there is no point in
e0dc3601 17381 putting them there unless we have hardware conversion insns. */
0fd8c3ad 17382 if (mode == HFmode)
e0dc3601 17383 return TARGET_FP16 && VFP_REGNO_OK_FOR_SINGLE (regno);
0fd8c3ad 17384
88f77cba
JB
17385 if (TARGET_NEON)
17386 return (VALID_NEON_DREG_MODE (mode) && VFP_REGNO_OK_FOR_DOUBLE (regno))
17387 || (VALID_NEON_QREG_MODE (mode)
17388 && NEON_REGNO_OK_FOR_QUAD (regno))
17389 || (mode == TImode && NEON_REGNO_OK_FOR_NREGS (regno, 2))
17390 || (mode == EImode && NEON_REGNO_OK_FOR_NREGS (regno, 3))
17391 || (mode == OImode && NEON_REGNO_OK_FOR_NREGS (regno, 4))
17392 || (mode == CImode && NEON_REGNO_OK_FOR_NREGS (regno, 6))
17393 || (mode == XImode && NEON_REGNO_OK_FOR_NREGS (regno, 8));
17394
9b66ebb1
PB
17395 return FALSE;
17396 }
17397
a6a5de04
RE
17398 if (TARGET_REALLY_IWMMXT)
17399 {
17400 if (IS_IWMMXT_GR_REGNUM (regno))
17401 return mode == SImode;
5a9335ef 17402
a6a5de04
RE
17403 if (IS_IWMMXT_REGNUM (regno))
17404 return VALID_IWMMXT_REG_MODE (mode);
17405 }
17406
2e94c12d 17407 /* We allow almost any value to be stored in the general registers.
fdd695fd 17408 Restrict doubleword quantities to even register pairs so that we can
2e94c12d
JB
17409 use ldrd. Do not allow very large Neon structure opaque modes in
17410 general registers; they would use too many. */
4b02997f 17411 if (regno <= LAST_ARM_REGNUM)
88f77cba 17412 return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0)
2e94c12d 17413 && ARM_NUM_REGS (mode) <= 4;
4b02997f 17414
a6a5de04 17415 if (regno == FRAME_POINTER_REGNUM
4b02997f
NC
17416 || regno == ARG_POINTER_REGNUM)
17417 /* We only allow integers in the fake hard registers. */
17418 return GET_MODE_CLASS (mode) == MODE_INT;
17419
3b684012 17420 /* The only registers left are the FPA registers
4b02997f 17421 which we only allow to hold FP values. */
a6a5de04
RE
17422 return (TARGET_HARD_FLOAT && TARGET_FPA
17423 && GET_MODE_CLASS (mode) == MODE_FLOAT
17424 && regno >= FIRST_FPA_REGNUM
17425 && regno <= LAST_FPA_REGNUM);
4b02997f
NC
17426}
17427
5b3e6663
PB
17428/* For efficiency and historical reasons LO_REGS, HI_REGS and CC_REGS are
17429 not used in arm mode. */
bbbbb16a
ILT
17430
17431enum reg_class
e32bac5b 17432arm_regno_class (int regno)
d5b7b3ae 17433{
5b3e6663 17434 if (TARGET_THUMB1)
d5b7b3ae
RE
17435 {
17436 if (regno == STACK_POINTER_REGNUM)
17437 return STACK_REG;
17438 if (regno == CC_REGNUM)
17439 return CC_REG;
17440 if (regno < 8)
17441 return LO_REGS;
17442 return HI_REGS;
17443 }
17444
5b3e6663
PB
17445 if (TARGET_THUMB2 && regno < 8)
17446 return LO_REGS;
17447
d5b7b3ae
RE
17448 if ( regno <= LAST_ARM_REGNUM
17449 || regno == FRAME_POINTER_REGNUM
17450 || regno == ARG_POINTER_REGNUM)
5b3e6663 17451 return TARGET_THUMB2 ? HI_REGS : GENERAL_REGS;
f676971a 17452
9b66ebb1 17453 if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
5b3e6663 17454 return TARGET_THUMB2 ? CC_REG : NO_REGS;
d5b7b3ae 17455
9b6b54e2
NC
17456 if (IS_CIRRUS_REGNUM (regno))
17457 return CIRRUS_REGS;
17458
9b66ebb1 17459 if (IS_VFP_REGNUM (regno))
f1adb0a9
JB
17460 {
17461 if (regno <= D7_VFP_REGNUM)
17462 return VFP_D0_D7_REGS;
17463 else if (regno <= LAST_LO_VFP_REGNUM)
17464 return VFP_LO_REGS;
17465 else
17466 return VFP_HI_REGS;
17467 }
9b66ebb1 17468
5a9335ef
NC
17469 if (IS_IWMMXT_REGNUM (regno))
17470 return IWMMXT_REGS;
17471
e99faaaa
ILT
17472 if (IS_IWMMXT_GR_REGNUM (regno))
17473 return IWMMXT_GR_REGS;
17474
3b684012 17475 return FPA_REGS;
d5b7b3ae
RE
17476}
17477
17478/* Handle a special case when computing the offset
17479 of an argument from the frame pointer. */
17480int
e32bac5b 17481arm_debugger_arg_offset (int value, rtx addr)
d5b7b3ae
RE
17482{
17483 rtx insn;
17484
17485 /* We are only interested if dbxout_parms() failed to compute the offset. */
17486 if (value != 0)
17487 return 0;
17488
17489 /* We can only cope with the case where the address is held in a register. */
17490 if (GET_CODE (addr) != REG)
17491 return 0;
17492
17493 /* If we are using the frame pointer to point at the argument, then
17494 an offset of 0 is correct. */
cd2b33d0 17495 if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
d5b7b3ae 17496 return 0;
f676971a 17497
d5b7b3ae
RE
17498 /* If we are using the stack pointer to point at the
17499 argument, then an offset of 0 is correct. */
5b3e6663 17500 /* ??? Check this is consistent with thumb2 frame layout. */
5895f793 17501 if ((TARGET_THUMB || !frame_pointer_needed)
d5b7b3ae
RE
17502 && REGNO (addr) == SP_REGNUM)
17503 return 0;
f676971a 17504
d5b7b3ae
RE
17505 /* Oh dear. The argument is pointed to by a register rather
17506 than being held in a register, or being stored at a known
17507 offset from the frame pointer. Since GDB only understands
17508 those two kinds of argument we must translate the address
17509 held in the register into an offset from the frame pointer.
17510 We do this by searching through the insns for the function
17511 looking to see where this register gets its value. If the
4912a07c 17512 register is initialized from the frame pointer plus an offset
d5b7b3ae 17513 then we are in luck and we can continue, otherwise we give up.
f676971a 17514
d5b7b3ae
RE
17515 This code is exercised by producing debugging information
17516 for a function with arguments like this:
f676971a 17517
d5b7b3ae 17518 double func (double a, double b, int c, double d) {return d;}
f676971a 17519
d5b7b3ae
RE
17520 Without this code the stab for parameter 'd' will be set to
17521 an offset of 0 from the frame pointer, rather than 8. */
17522
17523 /* The if() statement says:
17524
17525 If the insn is a normal instruction
17526 and if the insn is setting the value in a register
17527 and if the register being set is the register holding the address of the argument
17528 and if the address is computing by an addition
17529 that involves adding to a register
17530 which is the frame pointer
17531 a constant integer
17532
d6b4baa4 17533 then... */
f676971a 17534
d5b7b3ae
RE
17535 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
17536 {
f676971a 17537 if ( GET_CODE (insn) == INSN
d5b7b3ae
RE
17538 && GET_CODE (PATTERN (insn)) == SET
17539 && REGNO (XEXP (PATTERN (insn), 0)) == REGNO (addr)
17540 && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
17541 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
cd2b33d0 17542 && REGNO (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
d5b7b3ae
RE
17543 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
17544 )
17545 {
17546 value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
f676971a 17547
d5b7b3ae
RE
17548 break;
17549 }
17550 }
f676971a 17551
d5b7b3ae
RE
17552 if (value == 0)
17553 {
17554 debug_rtx (addr);
d4ee4d25 17555 warning (0, "unable to compute real location of stacked parameter");
d5b7b3ae
RE
17556 value = 8; /* XXX magic hack */
17557 }
17558
17559 return value;
17560}
d5b7b3ae 17561\f
5a9335ef
NC
17562#define def_mbuiltin(MASK, NAME, TYPE, CODE) \
17563 do \
17564 { \
17565 if ((MASK) & insn_flags) \
c79efc4d
RÁE
17566 add_builtin_function ((NAME), (TYPE), (CODE), \
17567 BUILT_IN_MD, NULL, NULL_TREE); \
5a9335ef
NC
17568 } \
17569 while (0)
17570
17571struct builtin_description
17572{
17573 const unsigned int mask;
17574 const enum insn_code icode;
17575 const char * const name;
17576 const enum arm_builtins code;
17577 const enum rtx_code comparison;
17578 const unsigned int flag;
17579};
17580
17581static const struct builtin_description bdesc_2arg[] =
17582{
17583#define IWMMXT_BUILTIN(code, string, builtin) \
17584 { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
81f40b79 17585 ARM_BUILTIN_##builtin, UNKNOWN, 0 },
5a9335ef
NC
17586
17587 IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
17588 IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
17589 IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
17590 IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
17591 IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
17592 IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
17593 IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
17594 IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
17595 IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
17596 IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
17597 IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
17598 IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
17599 IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
17600 IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
17601 IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
17602 IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
17603 IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
17604 IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
17605 IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
f07a6b21
BE
17606 IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
17607 IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
5a9335ef
NC
17608 IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
17609 IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
17610 IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
17611 IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
17612 IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
17613 IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
17614 IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
17615 IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
17616 IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
17617 IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
17618 IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
17619 IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
17620 IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
17621 IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
17622 IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
17623 IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
17624 IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
17625 IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
17626 IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
17627 IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
17628 IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
17629 IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
17630 IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
17631 IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
17632 IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
17633 IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
17634 IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
17635 IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
17636 IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
17637 IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
17638 IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
17639 IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
17640 IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
17641 IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
17642 IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
17643 IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
17644 IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
17645
17646#define IWMMXT_BUILTIN2(code, builtin) \
81f40b79 17647 { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, UNKNOWN, 0 },
f676971a 17648
5a9335ef
NC
17649 IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
17650 IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
17651 IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
17652 IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
17653 IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
17654 IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
17655 IWMMXT_BUILTIN2 (ashlv4hi3_di, WSLLH)
88f77cba 17656 IWMMXT_BUILTIN2 (ashlv4hi3_iwmmxt, WSLLHI)
5a9335ef 17657 IWMMXT_BUILTIN2 (ashlv2si3_di, WSLLW)
88f77cba 17658 IWMMXT_BUILTIN2 (ashlv2si3_iwmmxt, WSLLWI)
5a9335ef
NC
17659 IWMMXT_BUILTIN2 (ashldi3_di, WSLLD)
17660 IWMMXT_BUILTIN2 (ashldi3_iwmmxt, WSLLDI)
17661 IWMMXT_BUILTIN2 (lshrv4hi3_di, WSRLH)
88f77cba 17662 IWMMXT_BUILTIN2 (lshrv4hi3_iwmmxt, WSRLHI)
5a9335ef 17663 IWMMXT_BUILTIN2 (lshrv2si3_di, WSRLW)
88f77cba 17664 IWMMXT_BUILTIN2 (lshrv2si3_iwmmxt, WSRLWI)
5a9335ef 17665 IWMMXT_BUILTIN2 (lshrdi3_di, WSRLD)
9b66ebb1 17666 IWMMXT_BUILTIN2 (lshrdi3_iwmmxt, WSRLDI)
5a9335ef 17667 IWMMXT_BUILTIN2 (ashrv4hi3_di, WSRAH)
88f77cba 17668 IWMMXT_BUILTIN2 (ashrv4hi3_iwmmxt, WSRAHI)
5a9335ef 17669 IWMMXT_BUILTIN2 (ashrv2si3_di, WSRAW)
88f77cba 17670 IWMMXT_BUILTIN2 (ashrv2si3_iwmmxt, WSRAWI)
5a9335ef 17671 IWMMXT_BUILTIN2 (ashrdi3_di, WSRAD)
9b66ebb1 17672 IWMMXT_BUILTIN2 (ashrdi3_iwmmxt, WSRADI)
5a9335ef
NC
17673 IWMMXT_BUILTIN2 (rorv4hi3_di, WRORH)
17674 IWMMXT_BUILTIN2 (rorv4hi3, WRORHI)
17675 IWMMXT_BUILTIN2 (rorv2si3_di, WRORW)
17676 IWMMXT_BUILTIN2 (rorv2si3, WRORWI)
17677 IWMMXT_BUILTIN2 (rordi3_di, WRORD)
17678 IWMMXT_BUILTIN2 (rordi3, WRORDI)
17679 IWMMXT_BUILTIN2 (iwmmxt_wmacuz, WMACUZ)
17680 IWMMXT_BUILTIN2 (iwmmxt_wmacsz, WMACSZ)
17681};
17682
17683static const struct builtin_description bdesc_1arg[] =
17684{
17685 IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
17686 IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
17687 IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
17688 IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
17689 IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
17690 IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
17691 IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
17692 IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
17693 IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
17694 IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
17695 IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
17696 IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
17697 IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
17698 IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
17699 IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
17700 IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
17701 IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
17702 IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
17703};
17704
17705/* Set up all the iWMMXt builtins. This is
17706 not called if TARGET_IWMMXT is zero. */
17707
17708static void
17709arm_init_iwmmxt_builtins (void)
17710{
17711 const struct builtin_description * d;
17712 size_t i;
17713 tree endlink = void_list_node;
17714
4a5eab38
PB
17715 tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
17716 tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
17717 tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
17718
5a9335ef
NC
17719 tree int_ftype_int
17720 = build_function_type (integer_type_node,
17721 tree_cons (NULL_TREE, integer_type_node, endlink));
17722 tree v8qi_ftype_v8qi_v8qi_int
17723 = build_function_type (V8QI_type_node,
17724 tree_cons (NULL_TREE, V8QI_type_node,
17725 tree_cons (NULL_TREE, V8QI_type_node,
17726 tree_cons (NULL_TREE,
17727 integer_type_node,
17728 endlink))));
17729 tree v4hi_ftype_v4hi_int
17730 = build_function_type (V4HI_type_node,
17731 tree_cons (NULL_TREE, V4HI_type_node,
17732 tree_cons (NULL_TREE, integer_type_node,
17733 endlink)));
17734 tree v2si_ftype_v2si_int
17735 = build_function_type (V2SI_type_node,
17736 tree_cons (NULL_TREE, V2SI_type_node,
17737 tree_cons (NULL_TREE, integer_type_node,
17738 endlink)));
17739 tree v2si_ftype_di_di
17740 = build_function_type (V2SI_type_node,
17741 tree_cons (NULL_TREE, long_long_integer_type_node,
17742 tree_cons (NULL_TREE, long_long_integer_type_node,
17743 endlink)));
17744 tree di_ftype_di_int
17745 = build_function_type (long_long_integer_type_node,
17746 tree_cons (NULL_TREE, long_long_integer_type_node,
17747 tree_cons (NULL_TREE, integer_type_node,
17748 endlink)));
17749 tree di_ftype_di_int_int
17750 = build_function_type (long_long_integer_type_node,
17751 tree_cons (NULL_TREE, long_long_integer_type_node,
17752 tree_cons (NULL_TREE, integer_type_node,
17753 tree_cons (NULL_TREE,
17754 integer_type_node,
17755 endlink))));
17756 tree int_ftype_v8qi
17757 = build_function_type (integer_type_node,
17758 tree_cons (NULL_TREE, V8QI_type_node,
17759 endlink));
17760 tree int_ftype_v4hi
17761 = build_function_type (integer_type_node,
17762 tree_cons (NULL_TREE, V4HI_type_node,
17763 endlink));
17764 tree int_ftype_v2si
17765 = build_function_type (integer_type_node,
17766 tree_cons (NULL_TREE, V2SI_type_node,
17767 endlink));
17768 tree int_ftype_v8qi_int
17769 = build_function_type (integer_type_node,
17770 tree_cons (NULL_TREE, V8QI_type_node,
17771 tree_cons (NULL_TREE, integer_type_node,
17772 endlink)));
17773 tree int_ftype_v4hi_int
17774 = build_function_type (integer_type_node,
17775 tree_cons (NULL_TREE, V4HI_type_node,
17776 tree_cons (NULL_TREE, integer_type_node,
17777 endlink)));
17778 tree int_ftype_v2si_int
17779 = build_function_type (integer_type_node,
17780 tree_cons (NULL_TREE, V2SI_type_node,
17781 tree_cons (NULL_TREE, integer_type_node,
17782 endlink)));
17783 tree v8qi_ftype_v8qi_int_int
17784 = build_function_type (V8QI_type_node,
17785 tree_cons (NULL_TREE, V8QI_type_node,
17786 tree_cons (NULL_TREE, integer_type_node,
17787 tree_cons (NULL_TREE,
17788 integer_type_node,
17789 endlink))));
17790 tree v4hi_ftype_v4hi_int_int
17791 = build_function_type (V4HI_type_node,
17792 tree_cons (NULL_TREE, V4HI_type_node,
17793 tree_cons (NULL_TREE, integer_type_node,
17794 tree_cons (NULL_TREE,
17795 integer_type_node,
17796 endlink))));
17797 tree v2si_ftype_v2si_int_int
17798 = build_function_type (V2SI_type_node,
17799 tree_cons (NULL_TREE, V2SI_type_node,
17800 tree_cons (NULL_TREE, integer_type_node,
17801 tree_cons (NULL_TREE,
17802 integer_type_node,
17803 endlink))));
17804 /* Miscellaneous. */
17805 tree v8qi_ftype_v4hi_v4hi
17806 = build_function_type (V8QI_type_node,
17807 tree_cons (NULL_TREE, V4HI_type_node,
17808 tree_cons (NULL_TREE, V4HI_type_node,
17809 endlink)));
17810 tree v4hi_ftype_v2si_v2si
17811 = build_function_type (V4HI_type_node,
17812 tree_cons (NULL_TREE, V2SI_type_node,
17813 tree_cons (NULL_TREE, V2SI_type_node,
17814 endlink)));
17815 tree v2si_ftype_v4hi_v4hi
17816 = build_function_type (V2SI_type_node,
17817 tree_cons (NULL_TREE, V4HI_type_node,
17818 tree_cons (NULL_TREE, V4HI_type_node,
17819 endlink)));
17820 tree v2si_ftype_v8qi_v8qi
17821 = build_function_type (V2SI_type_node,
17822 tree_cons (NULL_TREE, V8QI_type_node,
17823 tree_cons (NULL_TREE, V8QI_type_node,
17824 endlink)));
17825 tree v4hi_ftype_v4hi_di
17826 = build_function_type (V4HI_type_node,
17827 tree_cons (NULL_TREE, V4HI_type_node,
17828 tree_cons (NULL_TREE,
17829 long_long_integer_type_node,
17830 endlink)));
17831 tree v2si_ftype_v2si_di
17832 = build_function_type (V2SI_type_node,
17833 tree_cons (NULL_TREE, V2SI_type_node,
17834 tree_cons (NULL_TREE,
17835 long_long_integer_type_node,
17836 endlink)));
17837 tree void_ftype_int_int
17838 = build_function_type (void_type_node,
17839 tree_cons (NULL_TREE, integer_type_node,
17840 tree_cons (NULL_TREE, integer_type_node,
17841 endlink)));
17842 tree di_ftype_void
17843 = build_function_type (long_long_unsigned_type_node, endlink);
17844 tree di_ftype_v8qi
17845 = build_function_type (long_long_integer_type_node,
17846 tree_cons (NULL_TREE, V8QI_type_node,
17847 endlink));
17848 tree di_ftype_v4hi
17849 = build_function_type (long_long_integer_type_node,
17850 tree_cons (NULL_TREE, V4HI_type_node,
17851 endlink));
17852 tree di_ftype_v2si
17853 = build_function_type (long_long_integer_type_node,
17854 tree_cons (NULL_TREE, V2SI_type_node,
17855 endlink));
17856 tree v2si_ftype_v4hi
17857 = build_function_type (V2SI_type_node,
17858 tree_cons (NULL_TREE, V4HI_type_node,
17859 endlink));
17860 tree v4hi_ftype_v8qi
17861 = build_function_type (V4HI_type_node,
17862 tree_cons (NULL_TREE, V8QI_type_node,
17863 endlink));
17864
17865 tree di_ftype_di_v4hi_v4hi
17866 = build_function_type (long_long_unsigned_type_node,
17867 tree_cons (NULL_TREE,
17868 long_long_unsigned_type_node,
17869 tree_cons (NULL_TREE, V4HI_type_node,
17870 tree_cons (NULL_TREE,
17871 V4HI_type_node,
17872 endlink))));
17873
17874 tree di_ftype_v4hi_v4hi
17875 = build_function_type (long_long_unsigned_type_node,
17876 tree_cons (NULL_TREE, V4HI_type_node,
17877 tree_cons (NULL_TREE, V4HI_type_node,
17878 endlink)));
17879
17880 /* Normal vector binops. */
17881 tree v8qi_ftype_v8qi_v8qi
17882 = build_function_type (V8QI_type_node,
17883 tree_cons (NULL_TREE, V8QI_type_node,
17884 tree_cons (NULL_TREE, V8QI_type_node,
17885 endlink)));
17886 tree v4hi_ftype_v4hi_v4hi
17887 = build_function_type (V4HI_type_node,
17888 tree_cons (NULL_TREE, V4HI_type_node,
17889 tree_cons (NULL_TREE, V4HI_type_node,
17890 endlink)));
17891 tree v2si_ftype_v2si_v2si
17892 = build_function_type (V2SI_type_node,
17893 tree_cons (NULL_TREE, V2SI_type_node,
17894 tree_cons (NULL_TREE, V2SI_type_node,
17895 endlink)));
17896 tree di_ftype_di_di
17897 = build_function_type (long_long_unsigned_type_node,
17898 tree_cons (NULL_TREE, long_long_unsigned_type_node,
17899 tree_cons (NULL_TREE,
17900 long_long_unsigned_type_node,
17901 endlink)));
17902
17903 /* Add all builtins that are more or less simple operations on two
17904 operands. */
e97a46ce 17905 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
5a9335ef
NC
17906 {
17907 /* Use one of the operands; the target can have a different mode for
17908 mask-generating compares. */
17909 enum machine_mode mode;
17910 tree type;
17911
17912 if (d->name == 0)
17913 continue;
17914
17915 mode = insn_data[d->icode].operand[1].mode;
17916
17917 switch (mode)
17918 {
17919 case V8QImode:
17920 type = v8qi_ftype_v8qi_v8qi;
17921 break;
17922 case V4HImode:
17923 type = v4hi_ftype_v4hi_v4hi;
17924 break;
17925 case V2SImode:
17926 type = v2si_ftype_v2si_v2si;
17927 break;
17928 case DImode:
17929 type = di_ftype_di_di;
17930 break;
17931
17932 default:
e6d29d15 17933 gcc_unreachable ();
5a9335ef
NC
17934 }
17935
17936 def_mbuiltin (d->mask, d->name, type, d->code);
17937 }
17938
17939 /* Add the remaining MMX insns with somewhat more complicated types. */
17940 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
17941 def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
17942 def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
17943
17944 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
17945 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
17946 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
17947 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
17948 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
17949 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
17950
17951 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
17952 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
17953 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
17954 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
17955 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
17956 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
17957
17958 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
17959 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
17960 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
17961 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
17962 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
17963 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
17964
17965 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
17966 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
17967 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
17968 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
17969 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
17970 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
17971
17972 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
17973
17974 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
17975 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
17976 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
17977 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
17978
17979 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
17980 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
17981 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
17982 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
17983 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
17984 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
17985 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
17986 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
17987 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
17988
17989 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
17990 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
17991 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
17992
17993 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
17994 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
17995 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
17996
17997 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
17998 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
17999 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
18000 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
18001 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
18002 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
18003
18004 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
18005 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
18006 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
18007 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
18008 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
18009 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
18010 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
18011 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
18012 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
18013 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
18014 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
18015 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
18016
18017 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
18018 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
18019 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
18020 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
18021
18022 def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
18023 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
18024 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
18025 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
18026 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
18027 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
18028 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
18029}
18030
d3585b76
DJ
18031static void
18032arm_init_tls_builtins (void)
18033{
ebfe65a3 18034 tree ftype, decl;
d3585b76
DJ
18035
18036 ftype = build_function_type (ptr_type_node, void_list_node);
ebfe65a3
JJ
18037 decl = add_builtin_function ("__builtin_thread_pointer", ftype,
18038 ARM_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
18039 NULL, NULL_TREE);
18040 TREE_NOTHROW (decl) = 1;
18041 TREE_READONLY (decl) = 1;
d3585b76
DJ
18042}
18043
81f40b79 18044enum neon_builtin_type_bits {
88f77cba
JB
18045 T_V8QI = 0x0001,
18046 T_V4HI = 0x0002,
18047 T_V2SI = 0x0004,
18048 T_V2SF = 0x0008,
18049 T_DI = 0x0010,
18050 T_V16QI = 0x0020,
18051 T_V8HI = 0x0040,
18052 T_V4SI = 0x0080,
18053 T_V4SF = 0x0100,
18054 T_V2DI = 0x0200,
18055 T_TI = 0x0400,
18056 T_EI = 0x0800,
18057 T_OI = 0x1000
81f40b79 18058};
88f77cba
JB
18059
18060#define v8qi_UP T_V8QI
18061#define v4hi_UP T_V4HI
18062#define v2si_UP T_V2SI
18063#define v2sf_UP T_V2SF
18064#define di_UP T_DI
18065#define v16qi_UP T_V16QI
18066#define v8hi_UP T_V8HI
18067#define v4si_UP T_V4SI
18068#define v4sf_UP T_V4SF
18069#define v2di_UP T_V2DI
18070#define ti_UP T_TI
18071#define ei_UP T_EI
18072#define oi_UP T_OI
18073
18074#define UP(X) X##_UP
18075
18076#define T_MAX 13
18077
18078typedef enum {
18079 NEON_BINOP,
18080 NEON_TERNOP,
18081 NEON_UNOP,
18082 NEON_GETLANE,
18083 NEON_SETLANE,
18084 NEON_CREATE,
18085 NEON_DUP,
18086 NEON_DUPLANE,
18087 NEON_COMBINE,
18088 NEON_SPLIT,
18089 NEON_LANEMUL,
18090 NEON_LANEMULL,
18091 NEON_LANEMULH,
18092 NEON_LANEMAC,
18093 NEON_SCALARMUL,
18094 NEON_SCALARMULL,
18095 NEON_SCALARMULH,
18096 NEON_SCALARMAC,
18097 NEON_CONVERT,
18098 NEON_FIXCONV,
18099 NEON_SELECT,
18100 NEON_RESULTPAIR,
18101 NEON_REINTERP,
18102 NEON_VTBL,
18103 NEON_VTBX,
18104 NEON_LOAD1,
18105 NEON_LOAD1LANE,
18106 NEON_STORE1,
18107 NEON_STORE1LANE,
18108 NEON_LOADSTRUCT,
18109 NEON_LOADSTRUCTLANE,
18110 NEON_STORESTRUCT,
18111 NEON_STORESTRUCTLANE,
18112 NEON_LOGICBINOP,
18113 NEON_SHIFTINSERT,
18114 NEON_SHIFTIMM,
18115 NEON_SHIFTACC
18116} neon_itype;
18117
18118typedef struct {
18119 const char *name;
18120 const neon_itype itype;
81f40b79 18121 const int bits;
88f77cba
JB
18122 const enum insn_code codes[T_MAX];
18123 const unsigned int num_vars;
18124 unsigned int base_fcode;
18125} neon_builtin_datum;
18126
18127#define CF(N,X) CODE_FOR_neon_##N##X
18128
18129#define VAR1(T, N, A) \
18130 #N, NEON_##T, UP (A), { CF (N, A) }, 1, 0
18131#define VAR2(T, N, A, B) \
18132 #N, NEON_##T, UP (A) | UP (B), { CF (N, A), CF (N, B) }, 2, 0
18133#define VAR3(T, N, A, B, C) \
18134 #N, NEON_##T, UP (A) | UP (B) | UP (C), \
18135 { CF (N, A), CF (N, B), CF (N, C) }, 3, 0
18136#define VAR4(T, N, A, B, C, D) \
18137 #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D), \
18138 { CF (N, A), CF (N, B), CF (N, C), CF (N, D) }, 4, 0
18139#define VAR5(T, N, A, B, C, D, E) \
18140 #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E), \
18141 { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E) }, 5, 0
18142#define VAR6(T, N, A, B, C, D, E, F) \
18143 #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F), \
18144 { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F) }, 6, 0
18145#define VAR7(T, N, A, B, C, D, E, F, G) \
18146 #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G), \
18147 { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
18148 CF (N, G) }, 7, 0
18149#define VAR8(T, N, A, B, C, D, E, F, G, H) \
18150 #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
18151 | UP (H), \
18152 { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
18153 CF (N, G), CF (N, H) }, 8, 0
18154#define VAR9(T, N, A, B, C, D, E, F, G, H, I) \
18155 #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
18156 | UP (H) | UP (I), \
18157 { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
18158 CF (N, G), CF (N, H), CF (N, I) }, 9, 0
18159#define VAR10(T, N, A, B, C, D, E, F, G, H, I, J) \
18160 #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
18161 | UP (H) | UP (I) | UP (J), \
18162 { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
18163 CF (N, G), CF (N, H), CF (N, I), CF (N, J) }, 10, 0
18164
18165/* The mode entries in the following table correspond to the "key" type of the
18166 instruction variant, i.e. equivalent to that which would be specified after
18167 the assembler mnemonic, which usually refers to the last vector operand.
18168 (Signed/unsigned/polynomial types are not differentiated between though, and
18169 are all mapped onto the same mode for a given element size.) The modes
18170 listed per instruction should be the same as those defined for that
18171 instruction's pattern in neon.md.
18172 WARNING: Variants should be listed in the same increasing order as
18173 neon_builtin_type_bits. */
18174
18175static neon_builtin_datum neon_builtin_data[] =
18176{
18177 { VAR10 (BINOP, vadd,
18178 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18179 { VAR3 (BINOP, vaddl, v8qi, v4hi, v2si) },
18180 { VAR3 (BINOP, vaddw, v8qi, v4hi, v2si) },
18181 { VAR6 (BINOP, vhadd, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18182 { VAR8 (BINOP, vqadd, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18183 { VAR3 (BINOP, vaddhn, v8hi, v4si, v2di) },
18184 { VAR8 (BINOP, vmul, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18185 { VAR8 (TERNOP, vmla, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18186 { VAR3 (TERNOP, vmlal, v8qi, v4hi, v2si) },
18187 { VAR8 (TERNOP, vmls, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18188 { VAR3 (TERNOP, vmlsl, v8qi, v4hi, v2si) },
18189 { VAR4 (BINOP, vqdmulh, v4hi, v2si, v8hi, v4si) },
18190 { VAR2 (TERNOP, vqdmlal, v4hi, v2si) },
18191 { VAR2 (TERNOP, vqdmlsl, v4hi, v2si) },
18192 { VAR3 (BINOP, vmull, v8qi, v4hi, v2si) },
18193 { VAR2 (SCALARMULL, vmull_n, v4hi, v2si) },
18194 { VAR2 (LANEMULL, vmull_lane, v4hi, v2si) },
18195 { VAR2 (SCALARMULL, vqdmull_n, v4hi, v2si) },
18196 { VAR2 (LANEMULL, vqdmull_lane, v4hi, v2si) },
18197 { VAR4 (SCALARMULH, vqdmulh_n, v4hi, v2si, v8hi, v4si) },
18198 { VAR4 (LANEMULH, vqdmulh_lane, v4hi, v2si, v8hi, v4si) },
18199 { VAR2 (BINOP, vqdmull, v4hi, v2si) },
18200 { VAR8 (BINOP, vshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18201 { VAR8 (BINOP, vqshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18202 { VAR8 (SHIFTIMM, vshr_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18203 { VAR3 (SHIFTIMM, vshrn_n, v8hi, v4si, v2di) },
18204 { VAR3 (SHIFTIMM, vqshrn_n, v8hi, v4si, v2di) },
18205 { VAR3 (SHIFTIMM, vqshrun_n, v8hi, v4si, v2di) },
18206 { VAR8 (SHIFTIMM, vshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18207 { VAR8 (SHIFTIMM, vqshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18208 { VAR8 (SHIFTIMM, vqshlu_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18209 { VAR3 (SHIFTIMM, vshll_n, v8qi, v4hi, v2si) },
18210 { VAR8 (SHIFTACC, vsra_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18211 { VAR10 (BINOP, vsub,
18212 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18213 { VAR3 (BINOP, vsubl, v8qi, v4hi, v2si) },
18214 { VAR3 (BINOP, vsubw, v8qi, v4hi, v2si) },
18215 { VAR8 (BINOP, vqsub, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18216 { VAR6 (BINOP, vhsub, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18217 { VAR3 (BINOP, vsubhn, v8hi, v4si, v2di) },
18218 { VAR8 (BINOP, vceq, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18219 { VAR8 (BINOP, vcge, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18220 { VAR8 (BINOP, vcgt, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18221 { VAR2 (BINOP, vcage, v2sf, v4sf) },
18222 { VAR2 (BINOP, vcagt, v2sf, v4sf) },
18223 { VAR6 (BINOP, vtst, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18224 { VAR8 (BINOP, vabd, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18225 { VAR3 (BINOP, vabdl, v8qi, v4hi, v2si) },
18226 { VAR6 (TERNOP, vaba, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18227 { VAR3 (TERNOP, vabal, v8qi, v4hi, v2si) },
18228 { VAR8 (BINOP, vmax, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18229 { VAR8 (BINOP, vmin, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18230 { VAR4 (BINOP, vpadd, v8qi, v4hi, v2si, v2sf) },
18231 { VAR6 (UNOP, vpaddl, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18232 { VAR6 (BINOP, vpadal, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18233 { VAR4 (BINOP, vpmax, v8qi, v4hi, v2si, v2sf) },
18234 { VAR4 (BINOP, vpmin, v8qi, v4hi, v2si, v2sf) },
18235 { VAR2 (BINOP, vrecps, v2sf, v4sf) },
18236 { VAR2 (BINOP, vrsqrts, v2sf, v4sf) },
18237 { VAR8 (SHIFTINSERT, vsri_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18238 { VAR8 (SHIFTINSERT, vsli_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18239 { VAR8 (UNOP, vabs, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18240 { VAR6 (UNOP, vqabs, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18241 { VAR8 (UNOP, vneg, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18242 { VAR6 (UNOP, vqneg, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18243 { VAR6 (UNOP, vcls, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18244 { VAR6 (UNOP, vclz, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18245 { VAR2 (UNOP, vcnt, v8qi, v16qi) },
18246 { VAR4 (UNOP, vrecpe, v2si, v2sf, v4si, v4sf) },
18247 { VAR4 (UNOP, vrsqrte, v2si, v2sf, v4si, v4sf) },
18248 { VAR6 (UNOP, vmvn, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18249 /* FIXME: vget_lane supports more variants than this! */
18250 { VAR10 (GETLANE, vget_lane,
18251 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18252 { VAR10 (SETLANE, vset_lane,
18253 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18254 { VAR5 (CREATE, vcreate, v8qi, v4hi, v2si, v2sf, di) },
18255 { VAR10 (DUP, vdup_n,
18256 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18257 { VAR10 (DUPLANE, vdup_lane,
18258 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18259 { VAR5 (COMBINE, vcombine, v8qi, v4hi, v2si, v2sf, di) },
18260 { VAR5 (SPLIT, vget_high, v16qi, v8hi, v4si, v4sf, v2di) },
18261 { VAR5 (SPLIT, vget_low, v16qi, v8hi, v4si, v4sf, v2di) },
18262 { VAR3 (UNOP, vmovn, v8hi, v4si, v2di) },
18263 { VAR3 (UNOP, vqmovn, v8hi, v4si, v2di) },
18264 { VAR3 (UNOP, vqmovun, v8hi, v4si, v2di) },
18265 { VAR3 (UNOP, vmovl, v8qi, v4hi, v2si) },
18266 { VAR6 (LANEMUL, vmul_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18267 { VAR6 (LANEMAC, vmla_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18268 { VAR2 (LANEMAC, vmlal_lane, v4hi, v2si) },
18269 { VAR2 (LANEMAC, vqdmlal_lane, v4hi, v2si) },
18270 { VAR6 (LANEMAC, vmls_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18271 { VAR2 (LANEMAC, vmlsl_lane, v4hi, v2si) },
18272 { VAR2 (LANEMAC, vqdmlsl_lane, v4hi, v2si) },
18273 { VAR6 (SCALARMUL, vmul_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18274 { VAR6 (SCALARMAC, vmla_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18275 { VAR2 (SCALARMAC, vmlal_n, v4hi, v2si) },
18276 { VAR2 (SCALARMAC, vqdmlal_n, v4hi, v2si) },
18277 { VAR6 (SCALARMAC, vmls_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18278 { VAR2 (SCALARMAC, vmlsl_n, v4hi, v2si) },
18279 { VAR2 (SCALARMAC, vqdmlsl_n, v4hi, v2si) },
18280 { VAR10 (BINOP, vext,
18281 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18282 { VAR8 (UNOP, vrev64, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18283 { VAR4 (UNOP, vrev32, v8qi, v4hi, v16qi, v8hi) },
18284 { VAR2 (UNOP, vrev16, v8qi, v16qi) },
18285 { VAR4 (CONVERT, vcvt, v2si, v2sf, v4si, v4sf) },
18286 { VAR4 (FIXCONV, vcvt_n, v2si, v2sf, v4si, v4sf) },
18287 { VAR10 (SELECT, vbsl,
18288 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18289 { VAR1 (VTBL, vtbl1, v8qi) },
18290 { VAR1 (VTBL, vtbl2, v8qi) },
18291 { VAR1 (VTBL, vtbl3, v8qi) },
18292 { VAR1 (VTBL, vtbl4, v8qi) },
18293 { VAR1 (VTBX, vtbx1, v8qi) },
18294 { VAR1 (VTBX, vtbx2, v8qi) },
18295 { VAR1 (VTBX, vtbx3, v8qi) },
18296 { VAR1 (VTBX, vtbx4, v8qi) },
18297 { VAR8 (RESULTPAIR, vtrn, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18298 { VAR8 (RESULTPAIR, vzip, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18299 { VAR8 (RESULTPAIR, vuzp, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18300 { VAR5 (REINTERP, vreinterpretv8qi, v8qi, v4hi, v2si, v2sf, di) },
18301 { VAR5 (REINTERP, vreinterpretv4hi, v8qi, v4hi, v2si, v2sf, di) },
18302 { VAR5 (REINTERP, vreinterpretv2si, v8qi, v4hi, v2si, v2sf, di) },
18303 { VAR5 (REINTERP, vreinterpretv2sf, v8qi, v4hi, v2si, v2sf, di) },
18304 { VAR5 (REINTERP, vreinterpretdi, v8qi, v4hi, v2si, v2sf, di) },
18305 { VAR5 (REINTERP, vreinterpretv16qi, v16qi, v8hi, v4si, v4sf, v2di) },
18306 { VAR5 (REINTERP, vreinterpretv8hi, v16qi, v8hi, v4si, v4sf, v2di) },
18307 { VAR5 (REINTERP, vreinterpretv4si, v16qi, v8hi, v4si, v4sf, v2di) },
18308 { VAR5 (REINTERP, vreinterpretv4sf, v16qi, v8hi, v4si, v4sf, v2di) },
18309 { VAR5 (REINTERP, vreinterpretv2di, v16qi, v8hi, v4si, v4sf, v2di) },
18310 { VAR10 (LOAD1, vld1,
18311 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18312 { VAR10 (LOAD1LANE, vld1_lane,
18313 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18314 { VAR10 (LOAD1, vld1_dup,
18315 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18316 { VAR10 (STORE1, vst1,
18317 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18318 { VAR10 (STORE1LANE, vst1_lane,
18319 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18320 { VAR9 (LOADSTRUCT,
18321 vld2, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18322 { VAR7 (LOADSTRUCTLANE, vld2_lane,
18323 v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18324 { VAR5 (LOADSTRUCT, vld2_dup, v8qi, v4hi, v2si, v2sf, di) },
18325 { VAR9 (STORESTRUCT, vst2,
18326 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18327 { VAR7 (STORESTRUCTLANE, vst2_lane,
18328 v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18329 { VAR9 (LOADSTRUCT,
18330 vld3, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18331 { VAR7 (LOADSTRUCTLANE, vld3_lane,
18332 v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18333 { VAR5 (LOADSTRUCT, vld3_dup, v8qi, v4hi, v2si, v2sf, di) },
18334 { VAR9 (STORESTRUCT, vst3,
18335 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18336 { VAR7 (STORESTRUCTLANE, vst3_lane,
18337 v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18338 { VAR9 (LOADSTRUCT, vld4,
18339 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18340 { VAR7 (LOADSTRUCTLANE, vld4_lane,
18341 v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18342 { VAR5 (LOADSTRUCT, vld4_dup, v8qi, v4hi, v2si, v2sf, di) },
18343 { VAR9 (STORESTRUCT, vst4,
18344 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18345 { VAR7 (STORESTRUCTLANE, vst4_lane,
18346 v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18347 { VAR10 (LOGICBINOP, vand,
18348 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18349 { VAR10 (LOGICBINOP, vorr,
18350 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18351 { VAR10 (BINOP, veor,
18352 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18353 { VAR10 (LOGICBINOP, vbic,
18354 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18355 { VAR10 (LOGICBINOP, vorn,
18356 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) }
18357};
18358
18359#undef CF
18360#undef VAR1
18361#undef VAR2
18362#undef VAR3
18363#undef VAR4
18364#undef VAR5
18365#undef VAR6
18366#undef VAR7
18367#undef VAR8
18368#undef VAR9
18369#undef VAR10
18370
18371static void
18372arm_init_neon_builtins (void)
18373{
18374 unsigned int i, fcode = ARM_BUILTIN_NEON_BASE;
18375
af06585a
JM
18376 tree neon_intQI_type_node;
18377 tree neon_intHI_type_node;
18378 tree neon_polyQI_type_node;
18379 tree neon_polyHI_type_node;
18380 tree neon_intSI_type_node;
18381 tree neon_intDI_type_node;
18382 tree neon_float_type_node;
18383
18384 tree intQI_pointer_node;
18385 tree intHI_pointer_node;
18386 tree intSI_pointer_node;
18387 tree intDI_pointer_node;
18388 tree float_pointer_node;
18389
18390 tree const_intQI_node;
18391 tree const_intHI_node;
18392 tree const_intSI_node;
18393 tree const_intDI_node;
18394 tree const_float_node;
18395
18396 tree const_intQI_pointer_node;
18397 tree const_intHI_pointer_node;
18398 tree const_intSI_pointer_node;
18399 tree const_intDI_pointer_node;
18400 tree const_float_pointer_node;
18401
18402 tree V8QI_type_node;
18403 tree V4HI_type_node;
18404 tree V2SI_type_node;
18405 tree V2SF_type_node;
18406 tree V16QI_type_node;
18407 tree V8HI_type_node;
18408 tree V4SI_type_node;
18409 tree V4SF_type_node;
18410 tree V2DI_type_node;
18411
18412 tree intUQI_type_node;
18413 tree intUHI_type_node;
18414 tree intUSI_type_node;
18415 tree intUDI_type_node;
18416
18417 tree intEI_type_node;
18418 tree intOI_type_node;
18419 tree intCI_type_node;
18420 tree intXI_type_node;
18421
18422 tree V8QI_pointer_node;
18423 tree V4HI_pointer_node;
18424 tree V2SI_pointer_node;
18425 tree V2SF_pointer_node;
18426 tree V16QI_pointer_node;
18427 tree V8HI_pointer_node;
18428 tree V4SI_pointer_node;
18429 tree V4SF_pointer_node;
18430 tree V2DI_pointer_node;
18431
18432 tree void_ftype_pv8qi_v8qi_v8qi;
18433 tree void_ftype_pv4hi_v4hi_v4hi;
18434 tree void_ftype_pv2si_v2si_v2si;
18435 tree void_ftype_pv2sf_v2sf_v2sf;
18436 tree void_ftype_pdi_di_di;
18437 tree void_ftype_pv16qi_v16qi_v16qi;
18438 tree void_ftype_pv8hi_v8hi_v8hi;
18439 tree void_ftype_pv4si_v4si_v4si;
18440 tree void_ftype_pv4sf_v4sf_v4sf;
18441 tree void_ftype_pv2di_v2di_v2di;
18442
18443 tree reinterp_ftype_dreg[5][5];
18444 tree reinterp_ftype_qreg[5][5];
18445 tree dreg_types[5], qreg_types[5];
18446
88f77cba
JB
18447 /* Create distinguished type nodes for NEON vector element types,
18448 and pointers to values of such types, so we can detect them later. */
af06585a
JM
18449 neon_intQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
18450 neon_intHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
18451 neon_polyQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
18452 neon_polyHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
18453 neon_intSI_type_node = make_signed_type (GET_MODE_PRECISION (SImode));
18454 neon_intDI_type_node = make_signed_type (GET_MODE_PRECISION (DImode));
18455 neon_float_type_node = make_node (REAL_TYPE);
18456 TYPE_PRECISION (neon_float_type_node) = FLOAT_TYPE_SIZE;
18457 layout_type (neon_float_type_node);
18458
bcbdbbb0
JM
18459 /* Define typedefs which exactly correspond to the modes we are basing vector
18460 types on. If you change these names you'll need to change
18461 the table used by arm_mangle_type too. */
18462 (*lang_hooks.types.register_builtin_type) (neon_intQI_type_node,
18463 "__builtin_neon_qi");
18464 (*lang_hooks.types.register_builtin_type) (neon_intHI_type_node,
18465 "__builtin_neon_hi");
18466 (*lang_hooks.types.register_builtin_type) (neon_intSI_type_node,
18467 "__builtin_neon_si");
18468 (*lang_hooks.types.register_builtin_type) (neon_float_type_node,
18469 "__builtin_neon_sf");
18470 (*lang_hooks.types.register_builtin_type) (neon_intDI_type_node,
18471 "__builtin_neon_di");
18472 (*lang_hooks.types.register_builtin_type) (neon_polyQI_type_node,
18473 "__builtin_neon_poly8");
18474 (*lang_hooks.types.register_builtin_type) (neon_polyHI_type_node,
18475 "__builtin_neon_poly16");
18476
af06585a
JM
18477 intQI_pointer_node = build_pointer_type (neon_intQI_type_node);
18478 intHI_pointer_node = build_pointer_type (neon_intHI_type_node);
18479 intSI_pointer_node = build_pointer_type (neon_intSI_type_node);
18480 intDI_pointer_node = build_pointer_type (neon_intDI_type_node);
18481 float_pointer_node = build_pointer_type (neon_float_type_node);
88f77cba
JB
18482
18483 /* Next create constant-qualified versions of the above types. */
af06585a
JM
18484 const_intQI_node = build_qualified_type (neon_intQI_type_node,
18485 TYPE_QUAL_CONST);
18486 const_intHI_node = build_qualified_type (neon_intHI_type_node,
18487 TYPE_QUAL_CONST);
18488 const_intSI_node = build_qualified_type (neon_intSI_type_node,
18489 TYPE_QUAL_CONST);
18490 const_intDI_node = build_qualified_type (neon_intDI_type_node,
18491 TYPE_QUAL_CONST);
18492 const_float_node = build_qualified_type (neon_float_type_node,
18493 TYPE_QUAL_CONST);
18494
18495 const_intQI_pointer_node = build_pointer_type (const_intQI_node);
18496 const_intHI_pointer_node = build_pointer_type (const_intHI_node);
18497 const_intSI_pointer_node = build_pointer_type (const_intSI_node);
18498 const_intDI_pointer_node = build_pointer_type (const_intDI_node);
18499 const_float_pointer_node = build_pointer_type (const_float_node);
88f77cba
JB
18500
18501 /* Now create vector types based on our NEON element types. */
18502 /* 64-bit vectors. */
af06585a 18503 V8QI_type_node =
88f77cba 18504 build_vector_type_for_mode (neon_intQI_type_node, V8QImode);
af06585a 18505 V4HI_type_node =
88f77cba 18506 build_vector_type_for_mode (neon_intHI_type_node, V4HImode);
af06585a 18507 V2SI_type_node =
88f77cba 18508 build_vector_type_for_mode (neon_intSI_type_node, V2SImode);
af06585a 18509 V2SF_type_node =
88f77cba
JB
18510 build_vector_type_for_mode (neon_float_type_node, V2SFmode);
18511 /* 128-bit vectors. */
af06585a 18512 V16QI_type_node =
88f77cba 18513 build_vector_type_for_mode (neon_intQI_type_node, V16QImode);
af06585a 18514 V8HI_type_node =
88f77cba 18515 build_vector_type_for_mode (neon_intHI_type_node, V8HImode);
af06585a 18516 V4SI_type_node =
88f77cba 18517 build_vector_type_for_mode (neon_intSI_type_node, V4SImode);
af06585a 18518 V4SF_type_node =
88f77cba 18519 build_vector_type_for_mode (neon_float_type_node, V4SFmode);
af06585a 18520 V2DI_type_node =
88f77cba
JB
18521 build_vector_type_for_mode (neon_intDI_type_node, V2DImode);
18522
18523 /* Unsigned integer types for various mode sizes. */
af06585a
JM
18524 intUQI_type_node = make_unsigned_type (GET_MODE_PRECISION (QImode));
18525 intUHI_type_node = make_unsigned_type (GET_MODE_PRECISION (HImode));
18526 intUSI_type_node = make_unsigned_type (GET_MODE_PRECISION (SImode));
18527 intUDI_type_node = make_unsigned_type (GET_MODE_PRECISION (DImode));
88f77cba 18528
bcbdbbb0
JM
18529 (*lang_hooks.types.register_builtin_type) (intUQI_type_node,
18530 "__builtin_neon_uqi");
18531 (*lang_hooks.types.register_builtin_type) (intUHI_type_node,
18532 "__builtin_neon_uhi");
18533 (*lang_hooks.types.register_builtin_type) (intUSI_type_node,
18534 "__builtin_neon_usi");
18535 (*lang_hooks.types.register_builtin_type) (intUDI_type_node,
18536 "__builtin_neon_udi");
18537
88f77cba 18538 /* Opaque integer types for structures of vectors. */
af06585a
JM
18539 intEI_type_node = make_signed_type (GET_MODE_PRECISION (EImode));
18540 intOI_type_node = make_signed_type (GET_MODE_PRECISION (OImode));
18541 intCI_type_node = make_signed_type (GET_MODE_PRECISION (CImode));
18542 intXI_type_node = make_signed_type (GET_MODE_PRECISION (XImode));
88f77cba 18543
bcbdbbb0
JM
18544 (*lang_hooks.types.register_builtin_type) (intTI_type_node,
18545 "__builtin_neon_ti");
18546 (*lang_hooks.types.register_builtin_type) (intEI_type_node,
18547 "__builtin_neon_ei");
18548 (*lang_hooks.types.register_builtin_type) (intOI_type_node,
18549 "__builtin_neon_oi");
18550 (*lang_hooks.types.register_builtin_type) (intCI_type_node,
18551 "__builtin_neon_ci");
18552 (*lang_hooks.types.register_builtin_type) (intXI_type_node,
18553 "__builtin_neon_xi");
18554
88f77cba 18555 /* Pointers to vector types. */
af06585a
JM
18556 V8QI_pointer_node = build_pointer_type (V8QI_type_node);
18557 V4HI_pointer_node = build_pointer_type (V4HI_type_node);
18558 V2SI_pointer_node = build_pointer_type (V2SI_type_node);
18559 V2SF_pointer_node = build_pointer_type (V2SF_type_node);
18560 V16QI_pointer_node = build_pointer_type (V16QI_type_node);
18561 V8HI_pointer_node = build_pointer_type (V8HI_type_node);
18562 V4SI_pointer_node = build_pointer_type (V4SI_type_node);
18563 V4SF_pointer_node = build_pointer_type (V4SF_type_node);
18564 V2DI_pointer_node = build_pointer_type (V2DI_type_node);
88f77cba
JB
18565
18566 /* Operations which return results as pairs. */
af06585a 18567 void_ftype_pv8qi_v8qi_v8qi =
88f77cba
JB
18568 build_function_type_list (void_type_node, V8QI_pointer_node, V8QI_type_node,
18569 V8QI_type_node, NULL);
af06585a 18570 void_ftype_pv4hi_v4hi_v4hi =
88f77cba
JB
18571 build_function_type_list (void_type_node, V4HI_pointer_node, V4HI_type_node,
18572 V4HI_type_node, NULL);
af06585a 18573 void_ftype_pv2si_v2si_v2si =
88f77cba
JB
18574 build_function_type_list (void_type_node, V2SI_pointer_node, V2SI_type_node,
18575 V2SI_type_node, NULL);
af06585a 18576 void_ftype_pv2sf_v2sf_v2sf =
88f77cba
JB
18577 build_function_type_list (void_type_node, V2SF_pointer_node, V2SF_type_node,
18578 V2SF_type_node, NULL);
af06585a 18579 void_ftype_pdi_di_di =
88f77cba
JB
18580 build_function_type_list (void_type_node, intDI_pointer_node,
18581 neon_intDI_type_node, neon_intDI_type_node, NULL);
af06585a 18582 void_ftype_pv16qi_v16qi_v16qi =
88f77cba
JB
18583 build_function_type_list (void_type_node, V16QI_pointer_node,
18584 V16QI_type_node, V16QI_type_node, NULL);
af06585a 18585 void_ftype_pv8hi_v8hi_v8hi =
88f77cba
JB
18586 build_function_type_list (void_type_node, V8HI_pointer_node, V8HI_type_node,
18587 V8HI_type_node, NULL);
af06585a 18588 void_ftype_pv4si_v4si_v4si =
88f77cba
JB
18589 build_function_type_list (void_type_node, V4SI_pointer_node, V4SI_type_node,
18590 V4SI_type_node, NULL);
af06585a 18591 void_ftype_pv4sf_v4sf_v4sf =
88f77cba
JB
18592 build_function_type_list (void_type_node, V4SF_pointer_node, V4SF_type_node,
18593 V4SF_type_node, NULL);
af06585a 18594 void_ftype_pv2di_v2di_v2di =
88f77cba
JB
18595 build_function_type_list (void_type_node, V2DI_pointer_node, V2DI_type_node,
18596 V2DI_type_node, NULL);
18597
88f77cba
JB
18598 dreg_types[0] = V8QI_type_node;
18599 dreg_types[1] = V4HI_type_node;
18600 dreg_types[2] = V2SI_type_node;
18601 dreg_types[3] = V2SF_type_node;
18602 dreg_types[4] = neon_intDI_type_node;
18603
18604 qreg_types[0] = V16QI_type_node;
18605 qreg_types[1] = V8HI_type_node;
18606 qreg_types[2] = V4SI_type_node;
18607 qreg_types[3] = V4SF_type_node;
18608 qreg_types[4] = V2DI_type_node;
18609
18610 for (i = 0; i < 5; i++)
18611 {
18612 int j;
18613 for (j = 0; j < 5; j++)
18614 {
18615 reinterp_ftype_dreg[i][j]
18616 = build_function_type_list (dreg_types[i], dreg_types[j], NULL);
18617 reinterp_ftype_qreg[i][j]
18618 = build_function_type_list (qreg_types[i], qreg_types[j], NULL);
18619 }
18620 }
18621
18622 for (i = 0; i < ARRAY_SIZE (neon_builtin_data); i++)
18623 {
18624 neon_builtin_datum *d = &neon_builtin_data[i];
18625 unsigned int j, codeidx = 0;
18626
18627 d->base_fcode = fcode;
18628
18629 for (j = 0; j < T_MAX; j++)
18630 {
18631 const char* const modenames[] = {
18632 "v8qi", "v4hi", "v2si", "v2sf", "di",
18633 "v16qi", "v8hi", "v4si", "v4sf", "v2di"
18634 };
18635 char namebuf[60];
18636 tree ftype = NULL;
18637 enum insn_code icode;
18638 int is_load = 0, is_store = 0;
18639
18640 if ((d->bits & (1 << j)) == 0)
18641 continue;
18642
18643 icode = d->codes[codeidx++];
18644
18645 switch (d->itype)
18646 {
18647 case NEON_LOAD1:
18648 case NEON_LOAD1LANE:
18649 case NEON_LOADSTRUCT:
18650 case NEON_LOADSTRUCTLANE:
18651 is_load = 1;
18652 /* Fall through. */
18653 case NEON_STORE1:
18654 case NEON_STORE1LANE:
18655 case NEON_STORESTRUCT:
18656 case NEON_STORESTRUCTLANE:
18657 if (!is_load)
18658 is_store = 1;
18659 /* Fall through. */
18660 case NEON_UNOP:
18661 case NEON_BINOP:
18662 case NEON_LOGICBINOP:
18663 case NEON_SHIFTINSERT:
18664 case NEON_TERNOP:
18665 case NEON_GETLANE:
18666 case NEON_SETLANE:
18667 case NEON_CREATE:
18668 case NEON_DUP:
18669 case NEON_DUPLANE:
18670 case NEON_SHIFTIMM:
18671 case NEON_SHIFTACC:
18672 case NEON_COMBINE:
18673 case NEON_SPLIT:
18674 case NEON_CONVERT:
18675 case NEON_FIXCONV:
18676 case NEON_LANEMUL:
18677 case NEON_LANEMULL:
18678 case NEON_LANEMULH:
18679 case NEON_LANEMAC:
18680 case NEON_SCALARMUL:
18681 case NEON_SCALARMULL:
18682 case NEON_SCALARMULH:
18683 case NEON_SCALARMAC:
18684 case NEON_SELECT:
18685 case NEON_VTBL:
18686 case NEON_VTBX:
18687 {
18688 int k;
18689 tree return_type = void_type_node, args = void_list_node;
18690
18691 /* Build a function type directly from the insn_data for this
18692 builtin. The build_function_type() function takes care of
18693 removing duplicates for us. */
18694 for (k = insn_data[icode].n_operands - 1; k >= 0; k--)
18695 {
18696 tree eltype;
18697
18698 if (is_load && k == 1)
18699 {
18700 /* Neon load patterns always have the memory operand
18701 (a SImode pointer) in the operand 1 position. We
18702 want a const pointer to the element type in that
18703 position. */
18704 gcc_assert (insn_data[icode].operand[k].mode == SImode);
18705
18706 switch (1 << j)
18707 {
18708 case T_V8QI:
18709 case T_V16QI:
18710 eltype = const_intQI_pointer_node;
18711 break;
18712
18713 case T_V4HI:
18714 case T_V8HI:
18715 eltype = const_intHI_pointer_node;
18716 break;
18717
18718 case T_V2SI:
18719 case T_V4SI:
18720 eltype = const_intSI_pointer_node;
18721 break;
18722
18723 case T_V2SF:
18724 case T_V4SF:
18725 eltype = const_float_pointer_node;
18726 break;
18727
18728 case T_DI:
18729 case T_V2DI:
18730 eltype = const_intDI_pointer_node;
18731 break;
18732
18733 default: gcc_unreachable ();
18734 }
18735 }
18736 else if (is_store && k == 0)
18737 {
18738 /* Similarly, Neon store patterns use operand 0 as
18739 the memory location to store to (a SImode pointer).
18740 Use a pointer to the element type of the store in
18741 that position. */
18742 gcc_assert (insn_data[icode].operand[k].mode == SImode);
18743
18744 switch (1 << j)
18745 {
18746 case T_V8QI:
18747 case T_V16QI:
18748 eltype = intQI_pointer_node;
18749 break;
18750
18751 case T_V4HI:
18752 case T_V8HI:
18753 eltype = intHI_pointer_node;
18754 break;
18755
18756 case T_V2SI:
18757 case T_V4SI:
18758 eltype = intSI_pointer_node;
18759 break;
18760
18761 case T_V2SF:
18762 case T_V4SF:
18763 eltype = float_pointer_node;
18764 break;
18765
18766 case T_DI:
18767 case T_V2DI:
18768 eltype = intDI_pointer_node;
18769 break;
18770
18771 default: gcc_unreachable ();
18772 }
18773 }
18774 else
18775 {
18776 switch (insn_data[icode].operand[k].mode)
18777 {
18778 case VOIDmode: eltype = void_type_node; break;
18779 /* Scalars. */
18780 case QImode: eltype = neon_intQI_type_node; break;
18781 case HImode: eltype = neon_intHI_type_node; break;
18782 case SImode: eltype = neon_intSI_type_node; break;
18783 case SFmode: eltype = neon_float_type_node; break;
18784 case DImode: eltype = neon_intDI_type_node; break;
18785 case TImode: eltype = intTI_type_node; break;
18786 case EImode: eltype = intEI_type_node; break;
18787 case OImode: eltype = intOI_type_node; break;
18788 case CImode: eltype = intCI_type_node; break;
18789 case XImode: eltype = intXI_type_node; break;
18790 /* 64-bit vectors. */
18791 case V8QImode: eltype = V8QI_type_node; break;
18792 case V4HImode: eltype = V4HI_type_node; break;
18793 case V2SImode: eltype = V2SI_type_node; break;
18794 case V2SFmode: eltype = V2SF_type_node; break;
18795 /* 128-bit vectors. */
18796 case V16QImode: eltype = V16QI_type_node; break;
18797 case V8HImode: eltype = V8HI_type_node; break;
18798 case V4SImode: eltype = V4SI_type_node; break;
18799 case V4SFmode: eltype = V4SF_type_node; break;
18800 case V2DImode: eltype = V2DI_type_node; break;
18801 default: gcc_unreachable ();
18802 }
18803 }
18804
18805 if (k == 0 && !is_store)
18806 return_type = eltype;
18807 else
18808 args = tree_cons (NULL_TREE, eltype, args);
18809 }
18810
18811 ftype = build_function_type (return_type, args);
18812 }
18813 break;
18814
18815 case NEON_RESULTPAIR:
18816 {
18817 switch (insn_data[icode].operand[1].mode)
18818 {
18819 case V8QImode: ftype = void_ftype_pv8qi_v8qi_v8qi; break;
18820 case V4HImode: ftype = void_ftype_pv4hi_v4hi_v4hi; break;
18821 case V2SImode: ftype = void_ftype_pv2si_v2si_v2si; break;
18822 case V2SFmode: ftype = void_ftype_pv2sf_v2sf_v2sf; break;
18823 case DImode: ftype = void_ftype_pdi_di_di; break;
18824 case V16QImode: ftype = void_ftype_pv16qi_v16qi_v16qi; break;
18825 case V8HImode: ftype = void_ftype_pv8hi_v8hi_v8hi; break;
18826 case V4SImode: ftype = void_ftype_pv4si_v4si_v4si; break;
18827 case V4SFmode: ftype = void_ftype_pv4sf_v4sf_v4sf; break;
18828 case V2DImode: ftype = void_ftype_pv2di_v2di_v2di; break;
18829 default: gcc_unreachable ();
18830 }
18831 }
18832 break;
18833
18834 case NEON_REINTERP:
18835 {
18836 /* We iterate over 5 doubleword types, then 5 quadword
18837 types. */
18838 int rhs = j % 5;
18839 switch (insn_data[icode].operand[0].mode)
18840 {
18841 case V8QImode: ftype = reinterp_ftype_dreg[0][rhs]; break;
18842 case V4HImode: ftype = reinterp_ftype_dreg[1][rhs]; break;
18843 case V2SImode: ftype = reinterp_ftype_dreg[2][rhs]; break;
18844 case V2SFmode: ftype = reinterp_ftype_dreg[3][rhs]; break;
18845 case DImode: ftype = reinterp_ftype_dreg[4][rhs]; break;
18846 case V16QImode: ftype = reinterp_ftype_qreg[0][rhs]; break;
18847 case V8HImode: ftype = reinterp_ftype_qreg[1][rhs]; break;
18848 case V4SImode: ftype = reinterp_ftype_qreg[2][rhs]; break;
18849 case V4SFmode: ftype = reinterp_ftype_qreg[3][rhs]; break;
18850 case V2DImode: ftype = reinterp_ftype_qreg[4][rhs]; break;
18851 default: gcc_unreachable ();
18852 }
18853 }
18854 break;
18855
18856 default:
18857 gcc_unreachable ();
18858 }
18859
18860 gcc_assert (ftype != NULL);
18861
18862 sprintf (namebuf, "__builtin_neon_%s%s", d->name, modenames[j]);
18863
18864 add_builtin_function (namebuf, ftype, fcode++, BUILT_IN_MD, NULL,
18865 NULL_TREE);
18866 }
18867 }
18868}
18869
0fd8c3ad
SL
18870static void
18871arm_init_fp16_builtins (void)
18872{
18873 tree fp16_type = make_node (REAL_TYPE);
18874 TYPE_PRECISION (fp16_type) = 16;
18875 layout_type (fp16_type);
18876 (*lang_hooks.types.register_builtin_type) (fp16_type, "__fp16");
18877}
18878
5a9335ef
NC
18879static void
18880arm_init_builtins (void)
18881{
d3585b76
DJ
18882 arm_init_tls_builtins ();
18883
5a9335ef
NC
18884 if (TARGET_REALLY_IWMMXT)
18885 arm_init_iwmmxt_builtins ();
88f77cba
JB
18886
18887 if (TARGET_NEON)
18888 arm_init_neon_builtins ();
0fd8c3ad
SL
18889
18890 if (arm_fp16_format)
18891 arm_init_fp16_builtins ();
18892}
18893
18894/* Implement TARGET_INVALID_PARAMETER_TYPE. */
18895
18896static const char *
18897arm_invalid_parameter_type (const_tree t)
18898{
18899 if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
18900 return N_("function parameters cannot have __fp16 type");
18901 return NULL;
18902}
18903
18904/* Implement TARGET_INVALID_PARAMETER_TYPE. */
18905
18906static const char *
18907arm_invalid_return_type (const_tree t)
18908{
18909 if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
18910 return N_("functions cannot return __fp16 type");
18911 return NULL;
18912}
18913
18914/* Implement TARGET_PROMOTED_TYPE. */
18915
18916static tree
18917arm_promoted_type (const_tree t)
18918{
18919 if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
18920 return float_type_node;
18921 return NULL_TREE;
18922}
18923
18924/* Implement TARGET_CONVERT_TO_TYPE.
18925 Specifically, this hook implements the peculiarity of the ARM
18926 half-precision floating-point C semantics that requires conversions between
18927 __fp16 to or from double to do an intermediate conversion to float. */
18928
18929static tree
18930arm_convert_to_type (tree type, tree expr)
18931{
18932 tree fromtype = TREE_TYPE (expr);
18933 if (!SCALAR_FLOAT_TYPE_P (fromtype) || !SCALAR_FLOAT_TYPE_P (type))
18934 return NULL_TREE;
18935 if ((TYPE_PRECISION (fromtype) == 16 && TYPE_PRECISION (type) > 32)
18936 || (TYPE_PRECISION (type) == 16 && TYPE_PRECISION (fromtype) > 32))
18937 return convert (type, convert (float_type_node, expr));
18938 return NULL_TREE;
5a9335ef
NC
18939}
18940
bdc4827b
SL
18941/* Implement TARGET_SCALAR_MODE_SUPPORTED_P.
18942 This simply adds HFmode as a supported mode; even though we don't
18943 implement arithmetic on this type directly, it's supported by
18944 optabs conversions, much the way the double-word arithmetic is
18945 special-cased in the default hook. */
18946
18947static bool
18948arm_scalar_mode_supported_p (enum machine_mode mode)
18949{
18950 if (mode == HFmode)
18951 return (arm_fp16_format != ARM_FP16_FORMAT_NONE);
18952 else
18953 return default_scalar_mode_supported_p (mode);
18954}
18955
5a9335ef
NC
18956/* Errors in the source file can cause expand_expr to return const0_rtx
18957 where we expect a vector. To avoid crashing, use one of the vector
18958 clear instructions. */
18959
18960static rtx
18961safe_vector_operand (rtx x, enum machine_mode mode)
18962{
18963 if (x != const0_rtx)
18964 return x;
18965 x = gen_reg_rtx (mode);
18966
18967 emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
18968 : gen_rtx_SUBREG (DImode, x, 0)));
18969 return x;
18970}
18971
18972/* Subroutine of arm_expand_builtin to take care of binop insns. */
18973
18974static rtx
18975arm_expand_binop_builtin (enum insn_code icode,
5039610b 18976 tree exp, rtx target)
5a9335ef
NC
18977{
18978 rtx pat;
5039610b
SL
18979 tree arg0 = CALL_EXPR_ARG (exp, 0);
18980 tree arg1 = CALL_EXPR_ARG (exp, 1);
84217346
MD
18981 rtx op0 = expand_normal (arg0);
18982 rtx op1 = expand_normal (arg1);
5a9335ef
NC
18983 enum machine_mode tmode = insn_data[icode].operand[0].mode;
18984 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
18985 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
18986
18987 if (VECTOR_MODE_P (mode0))
18988 op0 = safe_vector_operand (op0, mode0);
18989 if (VECTOR_MODE_P (mode1))
18990 op1 = safe_vector_operand (op1, mode1);
18991
18992 if (! target
18993 || GET_MODE (target) != tmode
18994 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
18995 target = gen_reg_rtx (tmode);
18996
e6d29d15 18997 gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
5a9335ef
NC
18998
18999 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19000 op0 = copy_to_mode_reg (mode0, op0);
19001 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
19002 op1 = copy_to_mode_reg (mode1, op1);
19003
19004 pat = GEN_FCN (icode) (target, op0, op1);
19005 if (! pat)
19006 return 0;
19007 emit_insn (pat);
19008 return target;
19009}
19010
19011/* Subroutine of arm_expand_builtin to take care of unop insns. */
19012
19013static rtx
19014arm_expand_unop_builtin (enum insn_code icode,
5039610b 19015 tree exp, rtx target, int do_load)
5a9335ef
NC
19016{
19017 rtx pat;
5039610b 19018 tree arg0 = CALL_EXPR_ARG (exp, 0);
84217346 19019 rtx op0 = expand_normal (arg0);
5a9335ef
NC
19020 enum machine_mode tmode = insn_data[icode].operand[0].mode;
19021 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
19022
19023 if (! target
19024 || GET_MODE (target) != tmode
19025 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19026 target = gen_reg_rtx (tmode);
19027 if (do_load)
19028 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
19029 else
19030 {
19031 if (VECTOR_MODE_P (mode0))
19032 op0 = safe_vector_operand (op0, mode0);
19033
19034 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19035 op0 = copy_to_mode_reg (mode0, op0);
19036 }
19037
19038 pat = GEN_FCN (icode) (target, op0);
19039 if (! pat)
19040 return 0;
19041 emit_insn (pat);
19042 return target;
19043}
19044
88f77cba
JB
19045static int
19046neon_builtin_compare (const void *a, const void *b)
19047{
5ead67f6
KG
19048 const neon_builtin_datum *const key = (const neon_builtin_datum *) a;
19049 const neon_builtin_datum *const memb = (const neon_builtin_datum *) b;
88f77cba
JB
19050 unsigned int soughtcode = key->base_fcode;
19051
19052 if (soughtcode >= memb->base_fcode
19053 && soughtcode < memb->base_fcode + memb->num_vars)
19054 return 0;
19055 else if (soughtcode < memb->base_fcode)
19056 return -1;
19057 else
19058 return 1;
19059}
19060
19061static enum insn_code
19062locate_neon_builtin_icode (int fcode, neon_itype *itype)
19063{
21272a45
JR
19064 neon_builtin_datum key
19065 = { NULL, (neon_itype) 0, 0, { CODE_FOR_nothing }, 0, 0 };
19066 neon_builtin_datum *found;
88f77cba
JB
19067 int idx;
19068
19069 key.base_fcode = fcode;
5ead67f6
KG
19070 found = (neon_builtin_datum *)
19071 bsearch (&key, &neon_builtin_data[0], ARRAY_SIZE (neon_builtin_data),
88f77cba
JB
19072 sizeof (neon_builtin_data[0]), neon_builtin_compare);
19073 gcc_assert (found);
19074 idx = fcode - (int) found->base_fcode;
19075 gcc_assert (idx >= 0 && idx < T_MAX && idx < (int)found->num_vars);
19076
19077 if (itype)
19078 *itype = found->itype;
19079
19080 return found->codes[idx];
19081}
19082
19083typedef enum {
19084 NEON_ARG_COPY_TO_REG,
19085 NEON_ARG_CONSTANT,
19086 NEON_ARG_STOP
19087} builtin_arg;
19088
19089#define NEON_MAX_BUILTIN_ARGS 5
19090
19091/* Expand a Neon builtin. */
19092static rtx
19093arm_expand_neon_args (rtx target, int icode, int have_retval,
19094 tree exp, ...)
19095{
19096 va_list ap;
19097 rtx pat;
19098 tree arg[NEON_MAX_BUILTIN_ARGS];
19099 rtx op[NEON_MAX_BUILTIN_ARGS];
19100 enum machine_mode tmode = insn_data[icode].operand[0].mode;
19101 enum machine_mode mode[NEON_MAX_BUILTIN_ARGS];
19102 int argc = 0;
19103
19104 if (have_retval
19105 && (!target
19106 || GET_MODE (target) != tmode
19107 || !(*insn_data[icode].operand[0].predicate) (target, tmode)))
19108 target = gen_reg_rtx (tmode);
19109
19110 va_start (ap, exp);
19111
19112 for (;;)
19113 {
81f40b79 19114 builtin_arg thisarg = (builtin_arg) va_arg (ap, int);
88f77cba
JB
19115
19116 if (thisarg == NEON_ARG_STOP)
19117 break;
19118 else
19119 {
19120 arg[argc] = CALL_EXPR_ARG (exp, argc);
19121 op[argc] = expand_normal (arg[argc]);
19122 mode[argc] = insn_data[icode].operand[argc + have_retval].mode;
19123
19124 switch (thisarg)
19125 {
19126 case NEON_ARG_COPY_TO_REG:
19127 /*gcc_assert (GET_MODE (op[argc]) == mode[argc]);*/
19128 if (!(*insn_data[icode].operand[argc + have_retval].predicate)
19129 (op[argc], mode[argc]))
19130 op[argc] = copy_to_mode_reg (mode[argc], op[argc]);
19131 break;
19132
19133 case NEON_ARG_CONSTANT:
19134 /* FIXME: This error message is somewhat unhelpful. */
19135 if (!(*insn_data[icode].operand[argc + have_retval].predicate)
19136 (op[argc], mode[argc]))
19137 error ("argument must be a constant");
19138 break;
19139
19140 case NEON_ARG_STOP:
19141 gcc_unreachable ();
19142 }
19143
19144 argc++;
19145 }
19146 }
19147
19148 va_end (ap);
19149
19150 if (have_retval)
19151 switch (argc)
19152 {
19153 case 1:
19154 pat = GEN_FCN (icode) (target, op[0]);
19155 break;
19156
19157 case 2:
19158 pat = GEN_FCN (icode) (target, op[0], op[1]);
19159 break;
19160
19161 case 3:
19162 pat = GEN_FCN (icode) (target, op[0], op[1], op[2]);
19163 break;
19164
19165 case 4:
19166 pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3]);
19167 break;
19168
19169 case 5:
19170 pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3], op[4]);
19171 break;
19172
19173 default:
19174 gcc_unreachable ();
19175 }
19176 else
19177 switch (argc)
19178 {
19179 case 1:
19180 pat = GEN_FCN (icode) (op[0]);
19181 break;
19182
19183 case 2:
19184 pat = GEN_FCN (icode) (op[0], op[1]);
19185 break;
19186
19187 case 3:
19188 pat = GEN_FCN (icode) (op[0], op[1], op[2]);
19189 break;
19190
19191 case 4:
19192 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
19193 break;
19194
19195 case 5:
19196 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4]);
19197 break;
19198
19199 default:
19200 gcc_unreachable ();
19201 }
19202
19203 if (!pat)
19204 return 0;
19205
19206 emit_insn (pat);
19207
19208 return target;
19209}
19210
19211/* Expand a Neon builtin. These are "special" because they don't have symbolic
19212 constants defined per-instruction or per instruction-variant. Instead, the
19213 required info is looked up in the table neon_builtin_data. */
19214static rtx
19215arm_expand_neon_builtin (int fcode, tree exp, rtx target)
19216{
19217 neon_itype itype;
19218 enum insn_code icode = locate_neon_builtin_icode (fcode, &itype);
19219
19220 switch (itype)
19221 {
19222 case NEON_UNOP:
19223 case NEON_CONVERT:
19224 case NEON_DUPLANE:
19225 return arm_expand_neon_args (target, icode, 1, exp,
19226 NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_STOP);
19227
19228 case NEON_BINOP:
19229 case NEON_SETLANE:
19230 case NEON_SCALARMUL:
19231 case NEON_SCALARMULL:
19232 case NEON_SCALARMULH:
19233 case NEON_SHIFTINSERT:
19234 case NEON_LOGICBINOP:
19235 return arm_expand_neon_args (target, icode, 1, exp,
19236 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19237 NEON_ARG_STOP);
19238
19239 case NEON_TERNOP:
19240 return arm_expand_neon_args (target, icode, 1, exp,
19241 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19242 NEON_ARG_CONSTANT, NEON_ARG_STOP);
19243
19244 case NEON_GETLANE:
19245 case NEON_FIXCONV:
19246 case NEON_SHIFTIMM:
19247 return arm_expand_neon_args (target, icode, 1, exp,
19248 NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_CONSTANT,
19249 NEON_ARG_STOP);
19250
19251 case NEON_CREATE:
19252 return arm_expand_neon_args (target, icode, 1, exp,
19253 NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19254
19255 case NEON_DUP:
19256 case NEON_SPLIT:
19257 case NEON_REINTERP:
19258 return arm_expand_neon_args (target, icode, 1, exp,
19259 NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19260
19261 case NEON_COMBINE:
19262 case NEON_VTBL:
19263 return arm_expand_neon_args (target, icode, 1, exp,
19264 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19265
19266 case NEON_RESULTPAIR:
19267 return arm_expand_neon_args (target, icode, 0, exp,
19268 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19269 NEON_ARG_STOP);
19270
19271 case NEON_LANEMUL:
19272 case NEON_LANEMULL:
19273 case NEON_LANEMULH:
19274 return arm_expand_neon_args (target, icode, 1, exp,
19275 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19276 NEON_ARG_CONSTANT, NEON_ARG_STOP);
19277
19278 case NEON_LANEMAC:
19279 return arm_expand_neon_args (target, icode, 1, exp,
19280 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19281 NEON_ARG_CONSTANT, NEON_ARG_CONSTANT, NEON_ARG_STOP);
19282
19283 case NEON_SHIFTACC:
19284 return arm_expand_neon_args (target, icode, 1, exp,
19285 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19286 NEON_ARG_CONSTANT, NEON_ARG_STOP);
19287
19288 case NEON_SCALARMAC:
19289 return arm_expand_neon_args (target, icode, 1, exp,
19290 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19291 NEON_ARG_CONSTANT, NEON_ARG_STOP);
19292
19293 case NEON_SELECT:
19294 case NEON_VTBX:
19295 return arm_expand_neon_args (target, icode, 1, exp,
19296 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19297 NEON_ARG_STOP);
19298
19299 case NEON_LOAD1:
19300 case NEON_LOADSTRUCT:
19301 return arm_expand_neon_args (target, icode, 1, exp,
19302 NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19303
19304 case NEON_LOAD1LANE:
19305 case NEON_LOADSTRUCTLANE:
19306 return arm_expand_neon_args (target, icode, 1, exp,
19307 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19308 NEON_ARG_STOP);
19309
19310 case NEON_STORE1:
19311 case NEON_STORESTRUCT:
19312 return arm_expand_neon_args (target, icode, 0, exp,
19313 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19314
19315 case NEON_STORE1LANE:
19316 case NEON_STORESTRUCTLANE:
19317 return arm_expand_neon_args (target, icode, 0, exp,
19318 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19319 NEON_ARG_STOP);
19320 }
19321
19322 gcc_unreachable ();
19323}
19324
19325/* Emit code to reinterpret one Neon type as another, without altering bits. */
19326void
19327neon_reinterpret (rtx dest, rtx src)
19328{
19329 emit_move_insn (dest, gen_lowpart (GET_MODE (dest), src));
19330}
19331
19332/* Emit code to place a Neon pair result in memory locations (with equal
19333 registers). */
19334void
19335neon_emit_pair_result_insn (enum machine_mode mode,
19336 rtx (*intfn) (rtx, rtx, rtx, rtx), rtx destaddr,
19337 rtx op1, rtx op2)
19338{
19339 rtx mem = gen_rtx_MEM (mode, destaddr);
19340 rtx tmp1 = gen_reg_rtx (mode);
19341 rtx tmp2 = gen_reg_rtx (mode);
19342
19343 emit_insn (intfn (tmp1, op1, tmp2, op2));
19344
19345 emit_move_insn (mem, tmp1);
19346 mem = adjust_address (mem, mode, GET_MODE_SIZE (mode));
19347 emit_move_insn (mem, tmp2);
19348}
19349
19350/* Set up operands for a register copy from src to dest, taking care not to
19351 clobber registers in the process.
19352 FIXME: This has rather high polynomial complexity (O(n^3)?) but shouldn't
19353 be called with a large N, so that should be OK. */
19354
19355void
19356neon_disambiguate_copy (rtx *operands, rtx *dest, rtx *src, unsigned int count)
19357{
19358 unsigned int copied = 0, opctr = 0;
19359 unsigned int done = (1 << count) - 1;
19360 unsigned int i, j;
19361
19362 while (copied != done)
19363 {
19364 for (i = 0; i < count; i++)
19365 {
19366 int good = 1;
19367
19368 for (j = 0; good && j < count; j++)
19369 if (i != j && (copied & (1 << j)) == 0
19370 && reg_overlap_mentioned_p (src[j], dest[i]))
19371 good = 0;
19372
19373 if (good)
19374 {
19375 operands[opctr++] = dest[i];
19376 operands[opctr++] = src[i];
19377 copied |= 1 << i;
19378 }
19379 }
19380 }
19381
19382 gcc_assert (opctr == count * 2);
19383}
19384
5a9335ef
NC
19385/* Expand an expression EXP that calls a built-in function,
19386 with result going to TARGET if that's convenient
19387 (and in mode MODE if that's convenient).
19388 SUBTARGET may be used as the target for computing one of EXP's operands.
19389 IGNORE is nonzero if the value is to be ignored. */
19390
19391static rtx
19392arm_expand_builtin (tree exp,
19393 rtx target,
19394 rtx subtarget ATTRIBUTE_UNUSED,
19395 enum machine_mode mode ATTRIBUTE_UNUSED,
19396 int ignore ATTRIBUTE_UNUSED)
19397{
19398 const struct builtin_description * d;
19399 enum insn_code icode;
5039610b 19400 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
5a9335ef
NC
19401 tree arg0;
19402 tree arg1;
19403 tree arg2;
19404 rtx op0;
19405 rtx op1;
19406 rtx op2;
19407 rtx pat;
19408 int fcode = DECL_FUNCTION_CODE (fndecl);
19409 size_t i;
19410 enum machine_mode tmode;
19411 enum machine_mode mode0;
19412 enum machine_mode mode1;
19413 enum machine_mode mode2;
19414
88f77cba
JB
19415 if (fcode >= ARM_BUILTIN_NEON_BASE)
19416 return arm_expand_neon_builtin (fcode, exp, target);
19417
5a9335ef
NC
19418 switch (fcode)
19419 {
19420 case ARM_BUILTIN_TEXTRMSB:
19421 case ARM_BUILTIN_TEXTRMUB:
19422 case ARM_BUILTIN_TEXTRMSH:
19423 case ARM_BUILTIN_TEXTRMUH:
19424 case ARM_BUILTIN_TEXTRMSW:
19425 case ARM_BUILTIN_TEXTRMUW:
19426 icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
19427 : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
19428 : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
19429 : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
19430 : CODE_FOR_iwmmxt_textrmw);
19431
5039610b
SL
19432 arg0 = CALL_EXPR_ARG (exp, 0);
19433 arg1 = CALL_EXPR_ARG (exp, 1);
84217346
MD
19434 op0 = expand_normal (arg0);
19435 op1 = expand_normal (arg1);
5a9335ef
NC
19436 tmode = insn_data[icode].operand[0].mode;
19437 mode0 = insn_data[icode].operand[1].mode;
19438 mode1 = insn_data[icode].operand[2].mode;
19439
19440 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19441 op0 = copy_to_mode_reg (mode0, op0);
19442 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
19443 {
19444 /* @@@ better error message */
19445 error ("selector must be an immediate");
19446 return gen_reg_rtx (tmode);
19447 }
19448 if (target == 0
19449 || GET_MODE (target) != tmode
19450 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19451 target = gen_reg_rtx (tmode);
19452 pat = GEN_FCN (icode) (target, op0, op1);
19453 if (! pat)
19454 return 0;
19455 emit_insn (pat);
19456 return target;
19457
19458 case ARM_BUILTIN_TINSRB:
19459 case ARM_BUILTIN_TINSRH:
19460 case ARM_BUILTIN_TINSRW:
19461 icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
19462 : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
19463 : CODE_FOR_iwmmxt_tinsrw);
5039610b
SL
19464 arg0 = CALL_EXPR_ARG (exp, 0);
19465 arg1 = CALL_EXPR_ARG (exp, 1);
19466 arg2 = CALL_EXPR_ARG (exp, 2);
84217346
MD
19467 op0 = expand_normal (arg0);
19468 op1 = expand_normal (arg1);
19469 op2 = expand_normal (arg2);
5a9335ef
NC
19470 tmode = insn_data[icode].operand[0].mode;
19471 mode0 = insn_data[icode].operand[1].mode;
19472 mode1 = insn_data[icode].operand[2].mode;
19473 mode2 = insn_data[icode].operand[3].mode;
19474
19475 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19476 op0 = copy_to_mode_reg (mode0, op0);
19477 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
19478 op1 = copy_to_mode_reg (mode1, op1);
19479 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
19480 {
19481 /* @@@ better error message */
19482 error ("selector must be an immediate");
19483 return const0_rtx;
19484 }
19485 if (target == 0
19486 || GET_MODE (target) != tmode
19487 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19488 target = gen_reg_rtx (tmode);
19489 pat = GEN_FCN (icode) (target, op0, op1, op2);
19490 if (! pat)
19491 return 0;
19492 emit_insn (pat);
19493 return target;
19494
19495 case ARM_BUILTIN_SETWCX:
5039610b
SL
19496 arg0 = CALL_EXPR_ARG (exp, 0);
19497 arg1 = CALL_EXPR_ARG (exp, 1);
84217346
MD
19498 op0 = force_reg (SImode, expand_normal (arg0));
19499 op1 = expand_normal (arg1);
f07a6b21 19500 emit_insn (gen_iwmmxt_tmcr (op1, op0));
5a9335ef
NC
19501 return 0;
19502
19503 case ARM_BUILTIN_GETWCX:
5039610b 19504 arg0 = CALL_EXPR_ARG (exp, 0);
84217346 19505 op0 = expand_normal (arg0);
5a9335ef
NC
19506 target = gen_reg_rtx (SImode);
19507 emit_insn (gen_iwmmxt_tmrc (target, op0));
19508 return target;
19509
19510 case ARM_BUILTIN_WSHUFH:
19511 icode = CODE_FOR_iwmmxt_wshufh;
5039610b
SL
19512 arg0 = CALL_EXPR_ARG (exp, 0);
19513 arg1 = CALL_EXPR_ARG (exp, 1);
84217346
MD
19514 op0 = expand_normal (arg0);
19515 op1 = expand_normal (arg1);
5a9335ef
NC
19516 tmode = insn_data[icode].operand[0].mode;
19517 mode1 = insn_data[icode].operand[1].mode;
19518 mode2 = insn_data[icode].operand[2].mode;
19519
19520 if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
19521 op0 = copy_to_mode_reg (mode1, op0);
19522 if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
19523 {
19524 /* @@@ better error message */
19525 error ("mask must be an immediate");
19526 return const0_rtx;
19527 }
19528 if (target == 0
19529 || GET_MODE (target) != tmode
19530 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19531 target = gen_reg_rtx (tmode);
19532 pat = GEN_FCN (icode) (target, op0, op1);
19533 if (! pat)
19534 return 0;
19535 emit_insn (pat);
19536 return target;
19537
19538 case ARM_BUILTIN_WSADB:
5039610b 19539 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, exp, target);
5a9335ef 19540 case ARM_BUILTIN_WSADH:
5039610b 19541 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, exp, target);
5a9335ef 19542 case ARM_BUILTIN_WSADBZ:
5039610b 19543 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, exp, target);
5a9335ef 19544 case ARM_BUILTIN_WSADHZ:
5039610b 19545 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, exp, target);
5a9335ef
NC
19546
19547 /* Several three-argument builtins. */
19548 case ARM_BUILTIN_WMACS:
19549 case ARM_BUILTIN_WMACU:
19550 case ARM_BUILTIN_WALIGN:
19551 case ARM_BUILTIN_TMIA:
19552 case ARM_BUILTIN_TMIAPH:
19553 case ARM_BUILTIN_TMIATT:
19554 case ARM_BUILTIN_TMIATB:
19555 case ARM_BUILTIN_TMIABT:
19556 case ARM_BUILTIN_TMIABB:
19557 icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
19558 : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
19559 : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
19560 : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
19561 : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
19562 : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
19563 : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
19564 : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
19565 : CODE_FOR_iwmmxt_walign);
5039610b
SL
19566 arg0 = CALL_EXPR_ARG (exp, 0);
19567 arg1 = CALL_EXPR_ARG (exp, 1);
19568 arg2 = CALL_EXPR_ARG (exp, 2);
84217346
MD
19569 op0 = expand_normal (arg0);
19570 op1 = expand_normal (arg1);
19571 op2 = expand_normal (arg2);
5a9335ef
NC
19572 tmode = insn_data[icode].operand[0].mode;
19573 mode0 = insn_data[icode].operand[1].mode;
19574 mode1 = insn_data[icode].operand[2].mode;
19575 mode2 = insn_data[icode].operand[3].mode;
19576
19577 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19578 op0 = copy_to_mode_reg (mode0, op0);
19579 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
19580 op1 = copy_to_mode_reg (mode1, op1);
19581 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
19582 op2 = copy_to_mode_reg (mode2, op2);
19583 if (target == 0
19584 || GET_MODE (target) != tmode
19585 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19586 target = gen_reg_rtx (tmode);
19587 pat = GEN_FCN (icode) (target, op0, op1, op2);
19588 if (! pat)
19589 return 0;
19590 emit_insn (pat);
19591 return target;
f676971a 19592
5a9335ef
NC
19593 case ARM_BUILTIN_WZERO:
19594 target = gen_reg_rtx (DImode);
19595 emit_insn (gen_iwmmxt_clrdi (target));
19596 return target;
19597
d3585b76
DJ
19598 case ARM_BUILTIN_THREAD_POINTER:
19599 return arm_load_tp (target);
19600
5a9335ef
NC
19601 default:
19602 break;
19603 }
19604
e97a46ce 19605 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
5a9335ef 19606 if (d->code == (const enum arm_builtins) fcode)
5039610b 19607 return arm_expand_binop_builtin (d->icode, exp, target);
5a9335ef 19608
e97a46ce 19609 for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
5a9335ef 19610 if (d->code == (const enum arm_builtins) fcode)
5039610b 19611 return arm_expand_unop_builtin (d->icode, exp, target, 0);
5a9335ef
NC
19612
19613 /* @@@ Should really do something sensible here. */
19614 return NULL_RTX;
19615}
19616\f
1d6e90ac
NC
19617/* Return the number (counting from 0) of
19618 the least significant set bit in MASK. */
19619
e32bac5b 19620inline static int
b279b20a 19621number_of_first_bit_set (unsigned mask)
d5b7b3ae
RE
19622{
19623 int bit;
19624
19625 for (bit = 0;
19626 (mask & (1 << bit)) == 0;
5895f793 19627 ++bit)
d5b7b3ae
RE
19628 continue;
19629
19630 return bit;
19631}
19632
b279b20a
NC
19633/* Emit code to push or pop registers to or from the stack. F is the
19634 assembly file. MASK is the registers to push or pop. PUSH is
19635 nonzero if we should push, and zero if we should pop. For debugging
19636 output, if pushing, adjust CFA_OFFSET by the amount of space added
19637 to the stack. REAL_REGS should have the same number of bits set as
19638 MASK, and will be used instead (in the same order) to describe which
19639 registers were saved - this is used to mark the save slots when we
19640 push high registers after moving them to low registers. */
19641static void
19642thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
19643 unsigned long real_regs)
19644{
19645 int regno;
19646 int lo_mask = mask & 0xFF;
19647 int pushed_words = 0;
19648
e6d29d15 19649 gcc_assert (mask);
b279b20a
NC
19650
19651 if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
19652 {
19653 /* Special case. Do not generate a POP PC statement here, do it in
19654 thumb_exit() */
19655 thumb_exit (f, -1);
19656 return;
19657 }
19658
f0a0390e 19659 if (push && arm_except_unwind_info () == UI_TARGET)
617a1b71
PB
19660 {
19661 fprintf (f, "\t.save\t{");
19662 for (regno = 0; regno < 15; regno++)
19663 {
19664 if (real_regs & (1 << regno))
19665 {
19666 if (real_regs & ((1 << regno) -1))
19667 fprintf (f, ", ");
19668 asm_fprintf (f, "%r", regno);
19669 }
19670 }
19671 fprintf (f, "}\n");
19672 }
19673
b279b20a
NC
19674 fprintf (f, "\t%s\t{", push ? "push" : "pop");
19675
19676 /* Look at the low registers first. */
19677 for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
19678 {
19679 if (lo_mask & 1)
19680 {
19681 asm_fprintf (f, "%r", regno);
19682
19683 if ((lo_mask & ~1) != 0)
19684 fprintf (f, ", ");
19685
19686 pushed_words++;
19687 }
19688 }
19689
19690 if (push && (mask & (1 << LR_REGNUM)))
19691 {
19692 /* Catch pushing the LR. */
19693 if (mask & 0xFF)
19694 fprintf (f, ", ");
19695
19696 asm_fprintf (f, "%r", LR_REGNUM);
19697
19698 pushed_words++;
19699 }
19700 else if (!push && (mask & (1 << PC_REGNUM)))
19701 {
19702 /* Catch popping the PC. */
19703 if (TARGET_INTERWORK || TARGET_BACKTRACE
e3b5732b 19704 || crtl->calls_eh_return)
b279b20a
NC
19705 {
19706 /* The PC is never poped directly, instead
19707 it is popped into r3 and then BX is used. */
19708 fprintf (f, "}\n");
19709
19710 thumb_exit (f, -1);
19711
19712 return;
19713 }
19714 else
19715 {
19716 if (mask & 0xFF)
19717 fprintf (f, ", ");
19718
19719 asm_fprintf (f, "%r", PC_REGNUM);
19720 }
19721 }
19722
19723 fprintf (f, "}\n");
19724
19725 if (push && pushed_words && dwarf2out_do_frame ())
19726 {
d342c045 19727 char *l = dwarf2out_cfi_label (false);
b279b20a
NC
19728 int pushed_mask = real_regs;
19729
19730 *cfa_offset += pushed_words * 4;
19731 dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
19732
19733 pushed_words = 0;
19734 pushed_mask = real_regs;
19735 for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
19736 {
19737 if (pushed_mask & 1)
19738 dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
19739 }
19740 }
19741}
19742
d5b7b3ae
RE
19743/* Generate code to return from a thumb function.
19744 If 'reg_containing_return_addr' is -1, then the return address is
19745 actually on the stack, at the stack pointer. */
19746static void
c9ca9b88 19747thumb_exit (FILE *f, int reg_containing_return_addr)
d5b7b3ae
RE
19748{
19749 unsigned regs_available_for_popping;
19750 unsigned regs_to_pop;
19751 int pops_needed;
19752 unsigned available;
19753 unsigned required;
19754 int mode;
19755 int size;
19756 int restore_a4 = FALSE;
19757
19758 /* Compute the registers we need to pop. */
19759 regs_to_pop = 0;
19760 pops_needed = 0;
19761
c9ca9b88 19762 if (reg_containing_return_addr == -1)
d5b7b3ae 19763 {
d5b7b3ae 19764 regs_to_pop |= 1 << LR_REGNUM;
5895f793 19765 ++pops_needed;
d5b7b3ae
RE
19766 }
19767
19768 if (TARGET_BACKTRACE)
19769 {
19770 /* Restore the (ARM) frame pointer and stack pointer. */
19771 regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
19772 pops_needed += 2;
19773 }
19774
19775 /* If there is nothing to pop then just emit the BX instruction and
19776 return. */
19777 if (pops_needed == 0)
19778 {
e3b5732b 19779 if (crtl->calls_eh_return)
c9ca9b88 19780 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
d5b7b3ae
RE
19781
19782 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
19783 return;
19784 }
19785 /* Otherwise if we are not supporting interworking and we have not created
19786 a backtrace structure and the function was not entered in ARM mode then
19787 just pop the return address straight into the PC. */
5895f793
RE
19788 else if (!TARGET_INTERWORK
19789 && !TARGET_BACKTRACE
c9ca9b88 19790 && !is_called_in_ARM_mode (current_function_decl)
e3b5732b 19791 && !crtl->calls_eh_return)
d5b7b3ae 19792 {
c9ca9b88 19793 asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
d5b7b3ae
RE
19794 return;
19795 }
19796
19797 /* Find out how many of the (return) argument registers we can corrupt. */
19798 regs_available_for_popping = 0;
19799
19800 /* If returning via __builtin_eh_return, the bottom three registers
19801 all contain information needed for the return. */
e3b5732b 19802 if (crtl->calls_eh_return)
d5b7b3ae
RE
19803 size = 12;
19804 else
19805 {
d5b7b3ae
RE
19806 /* If we can deduce the registers used from the function's
19807 return value. This is more reliable that examining
6fb5fa3c 19808 df_regs_ever_live_p () because that will be set if the register is
d5b7b3ae
RE
19809 ever used in the function, not just if the register is used
19810 to hold a return value. */
19811
38173d38
JH
19812 if (crtl->return_rtx != 0)
19813 mode = GET_MODE (crtl->return_rtx);
d5b7b3ae 19814 else
d5b7b3ae
RE
19815 mode = DECL_MODE (DECL_RESULT (current_function_decl));
19816
19817 size = GET_MODE_SIZE (mode);
19818
19819 if (size == 0)
19820 {
19821 /* In a void function we can use any argument register.
19822 In a function that returns a structure on the stack
19823 we can use the second and third argument registers. */
19824 if (mode == VOIDmode)
19825 regs_available_for_popping =
19826 (1 << ARG_REGISTER (1))
19827 | (1 << ARG_REGISTER (2))
19828 | (1 << ARG_REGISTER (3));
19829 else
19830 regs_available_for_popping =
19831 (1 << ARG_REGISTER (2))
19832 | (1 << ARG_REGISTER (3));
19833 }
19834 else if (size <= 4)
19835 regs_available_for_popping =
19836 (1 << ARG_REGISTER (2))
19837 | (1 << ARG_REGISTER (3));
19838 else if (size <= 8)
19839 regs_available_for_popping =
19840 (1 << ARG_REGISTER (3));
19841 }
19842
19843 /* Match registers to be popped with registers into which we pop them. */
19844 for (available = regs_available_for_popping,
19845 required = regs_to_pop;
19846 required != 0 && available != 0;
19847 available &= ~(available & - available),
19848 required &= ~(required & - required))
19849 -- pops_needed;
19850
19851 /* If we have any popping registers left over, remove them. */
19852 if (available > 0)
5895f793 19853 regs_available_for_popping &= ~available;
f676971a 19854
d5b7b3ae
RE
19855 /* Otherwise if we need another popping register we can use
19856 the fourth argument register. */
19857 else if (pops_needed)
19858 {
19859 /* If we have not found any free argument registers and
19860 reg a4 contains the return address, we must move it. */
19861 if (regs_available_for_popping == 0
19862 && reg_containing_return_addr == LAST_ARG_REGNUM)
19863 {
19864 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
19865 reg_containing_return_addr = LR_REGNUM;
19866 }
19867 else if (size > 12)
19868 {
19869 /* Register a4 is being used to hold part of the return value,
19870 but we have dire need of a free, low register. */
19871 restore_a4 = TRUE;
f676971a 19872
d5b7b3ae
RE
19873 asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
19874 }
f676971a 19875
d5b7b3ae
RE
19876 if (reg_containing_return_addr != LAST_ARG_REGNUM)
19877 {
19878 /* The fourth argument register is available. */
19879 regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
f676971a 19880
5895f793 19881 --pops_needed;
d5b7b3ae
RE
19882 }
19883 }
19884
19885 /* Pop as many registers as we can. */
980e61bb
DJ
19886 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
19887 regs_available_for_popping);
d5b7b3ae
RE
19888
19889 /* Process the registers we popped. */
19890 if (reg_containing_return_addr == -1)
19891 {
19892 /* The return address was popped into the lowest numbered register. */
5895f793 19893 regs_to_pop &= ~(1 << LR_REGNUM);
f676971a 19894
d5b7b3ae
RE
19895 reg_containing_return_addr =
19896 number_of_first_bit_set (regs_available_for_popping);
19897
19898 /* Remove this register for the mask of available registers, so that
6bc82793 19899 the return address will not be corrupted by further pops. */
5895f793 19900 regs_available_for_popping &= ~(1 << reg_containing_return_addr);
d5b7b3ae
RE
19901 }
19902
19903 /* If we popped other registers then handle them here. */
19904 if (regs_available_for_popping)
19905 {
19906 int frame_pointer;
f676971a 19907
d5b7b3ae
RE
19908 /* Work out which register currently contains the frame pointer. */
19909 frame_pointer = number_of_first_bit_set (regs_available_for_popping);
19910
19911 /* Move it into the correct place. */
19912 asm_fprintf (f, "\tmov\t%r, %r\n",
19913 ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
19914
19915 /* (Temporarily) remove it from the mask of popped registers. */
5895f793
RE
19916 regs_available_for_popping &= ~(1 << frame_pointer);
19917 regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
f676971a 19918
d5b7b3ae
RE
19919 if (regs_available_for_popping)
19920 {
19921 int stack_pointer;
f676971a 19922
d5b7b3ae
RE
19923 /* We popped the stack pointer as well,
19924 find the register that contains it. */
19925 stack_pointer = number_of_first_bit_set (regs_available_for_popping);
19926
19927 /* Move it into the stack register. */
19928 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
f676971a 19929
d5b7b3ae
RE
19930 /* At this point we have popped all necessary registers, so
19931 do not worry about restoring regs_available_for_popping
19932 to its correct value:
19933
19934 assert (pops_needed == 0)
19935 assert (regs_available_for_popping == (1 << frame_pointer))
19936 assert (regs_to_pop == (1 << STACK_POINTER)) */
19937 }
19938 else
19939 {
19940 /* Since we have just move the popped value into the frame
19941 pointer, the popping register is available for reuse, and
19942 we know that we still have the stack pointer left to pop. */
19943 regs_available_for_popping |= (1 << frame_pointer);
19944 }
19945 }
f676971a 19946
d5b7b3ae
RE
19947 /* If we still have registers left on the stack, but we no longer have
19948 any registers into which we can pop them, then we must move the return
19949 address into the link register and make available the register that
19950 contained it. */
19951 if (regs_available_for_popping == 0 && pops_needed > 0)
19952 {
19953 regs_available_for_popping |= 1 << reg_containing_return_addr;
f676971a 19954
d5b7b3ae
RE
19955 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
19956 reg_containing_return_addr);
f676971a 19957
d5b7b3ae
RE
19958 reg_containing_return_addr = LR_REGNUM;
19959 }
19960
19961 /* If we have registers left on the stack then pop some more.
19962 We know that at most we will want to pop FP and SP. */
19963 if (pops_needed > 0)
19964 {
19965 int popped_into;
19966 int move_to;
f676971a 19967
980e61bb
DJ
19968 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
19969 regs_available_for_popping);
d5b7b3ae
RE
19970
19971 /* We have popped either FP or SP.
19972 Move whichever one it is into the correct register. */
19973 popped_into = number_of_first_bit_set (regs_available_for_popping);
19974 move_to = number_of_first_bit_set (regs_to_pop);
19975
19976 asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
19977
5895f793 19978 regs_to_pop &= ~(1 << move_to);
d5b7b3ae 19979
5895f793 19980 --pops_needed;
d5b7b3ae 19981 }
f676971a 19982
d5b7b3ae
RE
19983 /* If we still have not popped everything then we must have only
19984 had one register available to us and we are now popping the SP. */
19985 if (pops_needed > 0)
19986 {
19987 int popped_into;
f676971a 19988
980e61bb
DJ
19989 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
19990 regs_available_for_popping);
d5b7b3ae
RE
19991
19992 popped_into = number_of_first_bit_set (regs_available_for_popping);
19993
19994 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
19995 /*
19996 assert (regs_to_pop == (1 << STACK_POINTER))
19997 assert (pops_needed == 1)
19998 */
19999 }
20000
20001 /* If necessary restore the a4 register. */
20002 if (restore_a4)
20003 {
20004 if (reg_containing_return_addr != LR_REGNUM)
20005 {
20006 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
20007 reg_containing_return_addr = LR_REGNUM;
20008 }
f676971a 20009
d5b7b3ae
RE
20010 asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
20011 }
20012
e3b5732b 20013 if (crtl->calls_eh_return)
c9ca9b88 20014 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
d5b7b3ae
RE
20015
20016 /* Return to caller. */
20017 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
20018}
d5b7b3ae 20019\f
906668bb
BS
20020/* Scan INSN just before assembler is output for it.
20021 For Thumb-1, we track the status of the condition codes; this
20022 information is used in the cbranchsi4_insn pattern. */
d5b7b3ae 20023void
5b3e6663 20024thumb1_final_prescan_insn (rtx insn)
d5b7b3ae 20025{
d5b7b3ae 20026 if (flag_print_asm_name)
9d98a694
AO
20027 asm_fprintf (asm_out_file, "%@ 0x%04x\n",
20028 INSN_ADDRESSES (INSN_UID (insn)));
906668bb
BS
20029 /* Don't overwrite the previous setter when we get to a cbranch. */
20030 if (INSN_CODE (insn) != CODE_FOR_cbranchsi4_insn)
20031 {
20032 enum attr_conds conds;
20033
20034 if (cfun->machine->thumb1_cc_insn)
20035 {
20036 if (modified_in_p (cfun->machine->thumb1_cc_op0, insn)
20037 || modified_in_p (cfun->machine->thumb1_cc_op1, insn))
20038 CC_STATUS_INIT;
20039 }
20040 conds = get_attr_conds (insn);
20041 if (conds == CONDS_SET)
20042 {
20043 rtx set = single_set (insn);
20044 cfun->machine->thumb1_cc_insn = insn;
20045 cfun->machine->thumb1_cc_op0 = SET_DEST (set);
20046 cfun->machine->thumb1_cc_op1 = const0_rtx;
20047 cfun->machine->thumb1_cc_mode = CC_NOOVmode;
20048 if (INSN_CODE (insn) == CODE_FOR_thumb1_subsi3_insn)
20049 {
20050 rtx src1 = XEXP (SET_SRC (set), 1);
20051 if (src1 == const0_rtx)
20052 cfun->machine->thumb1_cc_mode = CCmode;
20053 }
20054 }
20055 else if (conds != CONDS_NOCOND)
20056 cfun->machine->thumb1_cc_insn = NULL_RTX;
20057 }
d5b7b3ae
RE
20058}
20059
20060int
e32bac5b 20061thumb_shiftable_const (unsigned HOST_WIDE_INT val)
d5b7b3ae
RE
20062{
20063 unsigned HOST_WIDE_INT mask = 0xff;
20064 int i;
20065
ce41c38b 20066 val = val & (unsigned HOST_WIDE_INT)0xffffffffu;
d5b7b3ae
RE
20067 if (val == 0) /* XXX */
20068 return 0;
f676971a 20069
d5b7b3ae
RE
20070 for (i = 0; i < 25; i++)
20071 if ((val & (mask << i)) == val)
20072 return 1;
20073
20074 return 0;
20075}
20076
825dda42 20077/* Returns nonzero if the current function contains,
d5b7b3ae 20078 or might contain a far jump. */
5848830f
PB
20079static int
20080thumb_far_jump_used_p (void)
d5b7b3ae
RE
20081{
20082 rtx insn;
20083
20084 /* This test is only important for leaf functions. */
5895f793 20085 /* assert (!leaf_function_p ()); */
f676971a 20086
d5b7b3ae
RE
20087 /* If we have already decided that far jumps may be used,
20088 do not bother checking again, and always return true even if
20089 it turns out that they are not being used. Once we have made
20090 the decision that far jumps are present (and that hence the link
20091 register will be pushed onto the stack) we cannot go back on it. */
20092 if (cfun->machine->far_jump_used)
20093 return 1;
20094
20095 /* If this function is not being called from the prologue/epilogue
20096 generation code then it must be being called from the
20097 INITIAL_ELIMINATION_OFFSET macro. */
5848830f 20098 if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
d5b7b3ae
RE
20099 {
20100 /* In this case we know that we are being asked about the elimination
20101 of the arg pointer register. If that register is not being used,
20102 then there are no arguments on the stack, and we do not have to
20103 worry that a far jump might force the prologue to push the link
20104 register, changing the stack offsets. In this case we can just
20105 return false, since the presence of far jumps in the function will
20106 not affect stack offsets.
20107
20108 If the arg pointer is live (or if it was live, but has now been
20109 eliminated and so set to dead) then we do have to test to see if
20110 the function might contain a far jump. This test can lead to some
20111 false negatives, since before reload is completed, then length of
20112 branch instructions is not known, so gcc defaults to returning their
20113 longest length, which in turn sets the far jump attribute to true.
20114
20115 A false negative will not result in bad code being generated, but it
20116 will result in a needless push and pop of the link register. We
5848830f
PB
20117 hope that this does not occur too often.
20118
20119 If we need doubleword stack alignment this could affect the other
20120 elimination offsets so we can't risk getting it wrong. */
6fb5fa3c 20121 if (df_regs_ever_live_p (ARG_POINTER_REGNUM))
d5b7b3ae 20122 cfun->machine->arg_pointer_live = 1;
5895f793 20123 else if (!cfun->machine->arg_pointer_live)
d5b7b3ae
RE
20124 return 0;
20125 }
20126
20127 /* Check to see if the function contains a branch
20128 insn with the far jump attribute set. */
20129 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
20130 {
20131 if (GET_CODE (insn) == JUMP_INSN
20132 /* Ignore tablejump patterns. */
20133 && GET_CODE (PATTERN (insn)) != ADDR_VEC
20134 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
20135 && get_attr_far_jump (insn) == FAR_JUMP_YES
20136 )
20137 {
9a9f7594 20138 /* Record the fact that we have decided that
d5b7b3ae
RE
20139 the function does use far jumps. */
20140 cfun->machine->far_jump_used = 1;
20141 return 1;
20142 }
20143 }
f676971a 20144
d5b7b3ae
RE
20145 return 0;
20146}
20147
825dda42 20148/* Return nonzero if FUNC must be entered in ARM mode. */
d5b7b3ae 20149int
e32bac5b 20150is_called_in_ARM_mode (tree func)
d5b7b3ae 20151{
e6d29d15 20152 gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
d5b7b3ae 20153
696e78bf 20154 /* Ignore the problem about functions whose address is taken. */
d5b7b3ae
RE
20155 if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
20156 return TRUE;
20157
f676971a 20158#ifdef ARM_PE
91d231cb 20159 return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
d5b7b3ae
RE
20160#else
20161 return FALSE;
20162#endif
20163}
20164
e784c52c
BS
20165/* Given the stack offsets and register mask in OFFSETS, decide how
20166 many additional registers to push instead of subtracting a constant
20167 from SP. For epilogues the principle is the same except we use pop.
20168 FOR_PROLOGUE indicates which we're generating. */
20169static int
20170thumb1_extra_regs_pushed (arm_stack_offsets *offsets, bool for_prologue)
20171{
20172 HOST_WIDE_INT amount;
20173 unsigned long live_regs_mask = offsets->saved_regs_mask;
20174 /* Extract a mask of the ones we can give to the Thumb's push/pop
20175 instruction. */
20176 unsigned long l_mask = live_regs_mask & (for_prologue ? 0x40ff : 0xff);
20177 /* Then count how many other high registers will need to be pushed. */
20178 unsigned long high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
20179 int n_free, reg_base;
20180
20181 if (!for_prologue && frame_pointer_needed)
20182 amount = offsets->locals_base - offsets->saved_regs;
20183 else
20184 amount = offsets->outgoing_args - offsets->saved_regs;
20185
20186 /* If the stack frame size is 512 exactly, we can save one load
20187 instruction, which should make this a win even when optimizing
20188 for speed. */
20189 if (!optimize_size && amount != 512)
20190 return 0;
20191
20192 /* Can't do this if there are high registers to push. */
20193 if (high_regs_pushed != 0)
20194 return 0;
20195
20196 /* Shouldn't do it in the prologue if no registers would normally
20197 be pushed at all. In the epilogue, also allow it if we'll have
20198 a pop insn for the PC. */
20199 if (l_mask == 0
20200 && (for_prologue
20201 || TARGET_BACKTRACE
20202 || (live_regs_mask & 1 << LR_REGNUM) == 0
20203 || TARGET_INTERWORK
20204 || crtl->args.pretend_args_size != 0))
20205 return 0;
20206
20207 /* Don't do this if thumb_expand_prologue wants to emit instructions
20208 between the push and the stack frame allocation. */
20209 if (for_prologue
20210 && ((flag_pic && arm_pic_register != INVALID_REGNUM)
20211 || (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)))
20212 return 0;
20213
20214 reg_base = 0;
20215 n_free = 0;
20216 if (!for_prologue)
20217 {
20218 reg_base = arm_size_return_regs () / UNITS_PER_WORD;
20219 live_regs_mask >>= reg_base;
20220 }
20221
20222 while (reg_base + n_free < 8 && !(live_regs_mask & 1)
20223 && (for_prologue || call_used_regs[reg_base + n_free]))
20224 {
20225 live_regs_mask >>= 1;
20226 n_free++;
20227 }
20228
20229 if (n_free == 0)
20230 return 0;
20231 gcc_assert (amount / 4 * 4 == amount);
20232
20233 if (amount >= 512 && (amount - n_free * 4) < 512)
20234 return (amount - 508) / 4;
20235 if (amount <= n_free * 4)
20236 return amount / 4;
20237 return 0;
20238}
20239
d6b4baa4 20240/* The bits which aren't usefully expanded as rtl. */
cd2b33d0 20241const char *
e32bac5b 20242thumb_unexpanded_epilogue (void)
d5b7b3ae 20243{
954954d1 20244 arm_stack_offsets *offsets;
d5b7b3ae 20245 int regno;
b279b20a 20246 unsigned long live_regs_mask = 0;
d5b7b3ae 20247 int high_regs_pushed = 0;
e784c52c 20248 int extra_pop;
d5b7b3ae 20249 int had_to_push_lr;
57934c39 20250 int size;
d5b7b3ae 20251
934c2060 20252 if (cfun->machine->return_used_this_function != 0)
d5b7b3ae
RE
20253 return "";
20254
58e60158
AN
20255 if (IS_NAKED (arm_current_func_type ()))
20256 return "";
20257
954954d1
PB
20258 offsets = arm_get_frame_offsets ();
20259 live_regs_mask = offsets->saved_regs_mask;
57934c39
PB
20260 high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
20261
20262 /* If we can deduce the registers used from the function's return value.
6fb5fa3c 20263 This is more reliable that examining df_regs_ever_live_p () because that
57934c39
PB
20264 will be set if the register is ever used in the function, not just if
20265 the register is used to hold a return value. */
4f5dfed0 20266 size = arm_size_return_regs ();
d5b7b3ae 20267
e784c52c
BS
20268 extra_pop = thumb1_extra_regs_pushed (offsets, false);
20269 if (extra_pop > 0)
20270 {
20271 unsigned long extra_mask = (1 << extra_pop) - 1;
20272 live_regs_mask |= extra_mask << (size / UNITS_PER_WORD);
20273 }
20274
d5b7b3ae 20275 /* The prolog may have pushed some high registers to use as
112cdef5 20276 work registers. e.g. the testsuite file:
d5b7b3ae
RE
20277 gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
20278 compiles to produce:
20279 push {r4, r5, r6, r7, lr}
20280 mov r7, r9
20281 mov r6, r8
20282 push {r6, r7}
20283 as part of the prolog. We have to undo that pushing here. */
f676971a 20284
d5b7b3ae
RE
20285 if (high_regs_pushed)
20286 {
b279b20a 20287 unsigned long mask = live_regs_mask & 0xff;
d5b7b3ae 20288 int next_hi_reg;
d5b7b3ae 20289
57934c39
PB
20290 /* The available low registers depend on the size of the value we are
20291 returning. */
20292 if (size <= 12)
d5b7b3ae 20293 mask |= 1 << 3;
57934c39
PB
20294 if (size <= 8)
20295 mask |= 1 << 2;
d5b7b3ae
RE
20296
20297 if (mask == 0)
20298 /* Oh dear! We have no low registers into which we can pop
20299 high registers! */
400500c4
RK
20300 internal_error
20301 ("no low registers available for popping high registers");
f676971a 20302
d5b7b3ae 20303 for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
57934c39 20304 if (live_regs_mask & (1 << next_hi_reg))
d5b7b3ae
RE
20305 break;
20306
20307 while (high_regs_pushed)
20308 {
20309 /* Find lo register(s) into which the high register(s) can
20310 be popped. */
20311 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
20312 {
20313 if (mask & (1 << regno))
20314 high_regs_pushed--;
20315 if (high_regs_pushed == 0)
20316 break;
20317 }
20318
20319 mask &= (2 << regno) - 1; /* A noop if regno == 8 */
20320
d6b4baa4 20321 /* Pop the values into the low register(s). */
980e61bb 20322 thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
d5b7b3ae
RE
20323
20324 /* Move the value(s) into the high registers. */
20325 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
20326 {
20327 if (mask & (1 << regno))
20328 {
20329 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
20330 regno);
f676971a 20331
d5b7b3ae 20332 for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
57934c39 20333 if (live_regs_mask & (1 << next_hi_reg))
d5b7b3ae
RE
20334 break;
20335 }
20336 }
20337 }
57934c39 20338 live_regs_mask &= ~0x0f00;
d5b7b3ae
RE
20339 }
20340
57934c39
PB
20341 had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
20342 live_regs_mask &= 0xff;
20343
38173d38 20344 if (crtl->args.pretend_args_size == 0 || TARGET_BACKTRACE)
d5b7b3ae 20345 {
f676971a 20346 /* Pop the return address into the PC. */
57934c39 20347 if (had_to_push_lr)
d5b7b3ae
RE
20348 live_regs_mask |= 1 << PC_REGNUM;
20349
20350 /* Either no argument registers were pushed or a backtrace
20351 structure was created which includes an adjusted stack
20352 pointer, so just pop everything. */
20353 if (live_regs_mask)
980e61bb
DJ
20354 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
20355 live_regs_mask);
57934c39 20356
d5b7b3ae 20357 /* We have either just popped the return address into the
e784c52c
BS
20358 PC or it is was kept in LR for the entire function.
20359 Note that thumb_pushpop has already called thumb_exit if the
20360 PC was in the list. */
57934c39
PB
20361 if (!had_to_push_lr)
20362 thumb_exit (asm_out_file, LR_REGNUM);
d5b7b3ae
RE
20363 }
20364 else
20365 {
20366 /* Pop everything but the return address. */
d5b7b3ae 20367 if (live_regs_mask)
980e61bb
DJ
20368 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
20369 live_regs_mask);
d5b7b3ae
RE
20370
20371 if (had_to_push_lr)
57934c39
PB
20372 {
20373 if (size > 12)
20374 {
20375 /* We have no free low regs, so save one. */
20376 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
20377 LAST_ARG_REGNUM);
20378 }
20379
20380 /* Get the return address into a temporary register. */
20381 thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
20382 1 << LAST_ARG_REGNUM);
20383
20384 if (size > 12)
20385 {
20386 /* Move the return address to lr. */
20387 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
20388 LAST_ARG_REGNUM);
20389 /* Restore the low register. */
20390 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
20391 IP_REGNUM);
20392 regno = LR_REGNUM;
20393 }
20394 else
20395 regno = LAST_ARG_REGNUM;
20396 }
20397 else
20398 regno = LR_REGNUM;
f676971a 20399
d5b7b3ae
RE
20400 /* Remove the argument registers that were pushed onto the stack. */
20401 asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
20402 SP_REGNUM, SP_REGNUM,
38173d38 20403 crtl->args.pretend_args_size);
f676971a 20404
57934c39 20405 thumb_exit (asm_out_file, regno);
d5b7b3ae
RE
20406 }
20407
20408 return "";
20409}
20410
20411/* Functions to save and restore machine-specific function data. */
e2500fed 20412static struct machine_function *
e32bac5b 20413arm_init_machine_status (void)
d5b7b3ae 20414{
e2500fed 20415 struct machine_function *machine;
a9429e29 20416 machine = ggc_alloc_cleared_machine_function ();
6d3d9133 20417
f676971a 20418#if ARM_FT_UNKNOWN != 0
e2500fed 20419 machine->func_type = ARM_FT_UNKNOWN;
6d3d9133 20420#endif
e2500fed 20421 return machine;
f7a80099
NC
20422}
20423
d5b7b3ae
RE
20424/* Return an RTX indicating where the return address to the
20425 calling function can be found. */
20426rtx
e32bac5b 20427arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
d5b7b3ae 20428{
d5b7b3ae
RE
20429 if (count != 0)
20430 return NULL_RTX;
20431
61f0ccff 20432 return get_hard_reg_initial_val (Pmode, LR_REGNUM);
d5b7b3ae
RE
20433}
20434
20435/* Do anything needed before RTL is emitted for each function. */
20436void
e32bac5b 20437arm_init_expanders (void)
d5b7b3ae
RE
20438{
20439 /* Arrange to initialize and mark the machine per-function status. */
20440 init_machine_status = arm_init_machine_status;
3ac5ea7c
RH
20441
20442 /* This is to stop the combine pass optimizing away the alignment
20443 adjustment of va_arg. */
20444 /* ??? It is claimed that this should not be necessary. */
20445 if (cfun)
20446 mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
d5b7b3ae
RE
20447}
20448
0977774b 20449
2591db65
RE
20450/* Like arm_compute_initial_elimination offset. Simpler because there
20451 isn't an ABI specified frame pointer for Thumb. Instead, we set it
20452 to point at the base of the local variables after static stack
20453 space for a function has been allocated. */
0977774b 20454
5848830f
PB
20455HOST_WIDE_INT
20456thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
20457{
20458 arm_stack_offsets *offsets;
0977774b 20459
5848830f 20460 offsets = arm_get_frame_offsets ();
0977774b 20461
5848830f 20462 switch (from)
0977774b 20463 {
5848830f
PB
20464 case ARG_POINTER_REGNUM:
20465 switch (to)
20466 {
20467 case STACK_POINTER_REGNUM:
20468 return offsets->outgoing_args - offsets->saved_args;
0977774b 20469
5848830f
PB
20470 case FRAME_POINTER_REGNUM:
20471 return offsets->soft_frame - offsets->saved_args;
0977774b 20472
5848830f
PB
20473 case ARM_HARD_FRAME_POINTER_REGNUM:
20474 return offsets->saved_regs - offsets->saved_args;
0977774b 20475
2591db65
RE
20476 case THUMB_HARD_FRAME_POINTER_REGNUM:
20477 return offsets->locals_base - offsets->saved_args;
20478
5848830f 20479 default:
e6d29d15 20480 gcc_unreachable ();
5848830f
PB
20481 }
20482 break;
0977774b 20483
5848830f
PB
20484 case FRAME_POINTER_REGNUM:
20485 switch (to)
20486 {
20487 case STACK_POINTER_REGNUM:
20488 return offsets->outgoing_args - offsets->soft_frame;
0977774b 20489
5848830f
PB
20490 case ARM_HARD_FRAME_POINTER_REGNUM:
20491 return offsets->saved_regs - offsets->soft_frame;
0977774b 20492
2591db65
RE
20493 case THUMB_HARD_FRAME_POINTER_REGNUM:
20494 return offsets->locals_base - offsets->soft_frame;
20495
5848830f 20496 default:
e6d29d15 20497 gcc_unreachable ();
5848830f
PB
20498 }
20499 break;
0977774b 20500
5848830f 20501 default:
e6d29d15 20502 gcc_unreachable ();
5848830f 20503 }
0977774b
JT
20504}
20505
d5b7b3ae
RE
20506/* Generate the rest of a function's prologue. */
20507void
5b3e6663 20508thumb1_expand_prologue (void)
d5b7b3ae 20509{
980e61bb
DJ
20510 rtx insn, dwarf;
20511
5848830f
PB
20512 HOST_WIDE_INT amount;
20513 arm_stack_offsets *offsets;
6d3d9133 20514 unsigned long func_type;
3c7ad43e 20515 int regno;
57934c39 20516 unsigned long live_regs_mask;
6d3d9133
NC
20517
20518 func_type = arm_current_func_type ();
f676971a 20519
d5b7b3ae 20520 /* Naked functions don't have prologues. */
6d3d9133 20521 if (IS_NAKED (func_type))
d5b7b3ae
RE
20522 return;
20523
6d3d9133
NC
20524 if (IS_INTERRUPT (func_type))
20525 {
c725bd79 20526 error ("interrupt Service Routines cannot be coded in Thumb mode");
6d3d9133
NC
20527 return;
20528 }
20529
954954d1
PB
20530 offsets = arm_get_frame_offsets ();
20531 live_regs_mask = offsets->saved_regs_mask;
b279b20a
NC
20532 /* Load the pic register before setting the frame pointer,
20533 so we can use r7 as a temporary work register. */
020a4035 20534 if (flag_pic && arm_pic_register != INVALID_REGNUM)
e55ef7f4 20535 arm_load_pic_register (live_regs_mask);
876f13b0 20536
2591db65 20537 if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
a2503645
RS
20538 emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
20539 stack_pointer_rtx);
d5b7b3ae 20540
5848830f 20541 amount = offsets->outgoing_args - offsets->saved_regs;
e784c52c 20542 amount -= 4 * thumb1_extra_regs_pushed (offsets, true);
d5b7b3ae
RE
20543 if (amount)
20544 {
d5b7b3ae 20545 if (amount < 512)
980e61bb
DJ
20546 {
20547 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
20548 GEN_INT (- amount)));
20549 RTX_FRAME_RELATED_P (insn) = 1;
20550 }
d5b7b3ae
RE
20551 else
20552 {
d5b7b3ae
RE
20553 rtx reg;
20554
20555 /* The stack decrement is too big for an immediate value in a single
20556 insn. In theory we could issue multiple subtracts, but after
20557 three of them it becomes more space efficient to place the full
20558 value in the constant pool and load into a register. (Also the
20559 ARM debugger really likes to see only one stack decrement per
20560 function). So instead we look for a scratch register into which
20561 we can load the decrement, and then we subtract this from the
20562 stack pointer. Unfortunately on the thumb the only available
20563 scratch registers are the argument registers, and we cannot use
20564 these as they may hold arguments to the function. Instead we
20565 attempt to locate a call preserved register which is used by this
20566 function. If we can find one, then we know that it will have
20567 been pushed at the start of the prologue and so we can corrupt
20568 it now. */
20569 for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
35596784 20570 if (live_regs_mask & (1 << regno))
d5b7b3ae
RE
20571 break;
20572
35596784 20573 gcc_assert(regno <= LAST_LO_REGNUM);
d5b7b3ae 20574
35596784 20575 reg = gen_rtx_REG (SImode, regno);
d5b7b3ae 20576
35596784 20577 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
980e61bb 20578
35596784
AJ
20579 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
20580 stack_pointer_rtx, reg));
20581 RTX_FRAME_RELATED_P (insn) = 1;
20582 dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
20583 plus_constant (stack_pointer_rtx,
20584 -amount));
20585 RTX_FRAME_RELATED_P (dwarf) = 1;
bbbbb16a 20586 add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
d5b7b3ae 20587 }
2591db65
RE
20588 }
20589
20590 if (frame_pointer_needed)
5b3e6663 20591 thumb_set_frame_pointer (offsets);
f676971a 20592
74d9c39f
DJ
20593 /* If we are profiling, make sure no instructions are scheduled before
20594 the call to mcount. Similarly if the user has requested no
20595 scheduling in the prolog. Similarly if we want non-call exceptions
20596 using the EABI unwinder, to prevent faulting instructions from being
20597 swapped with a stack adjustment. */
e3b5732b 20598 if (crtl->profile || !TARGET_SCHED_PROLOG
f0a0390e
RH
20599 || (arm_except_unwind_info () == UI_TARGET
20600 && cfun->can_throw_non_call_exceptions))
d5b7b3ae 20601 emit_insn (gen_blockage ());
3c7ad43e
PB
20602
20603 cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
57934c39
PB
20604 if (live_regs_mask & 0xff)
20605 cfun->machine->lr_save_eliminated = 0;
d5b7b3ae
RE
20606}
20607
57934c39 20608
d5b7b3ae 20609void
5b3e6663 20610thumb1_expand_epilogue (void)
d5b7b3ae 20611{
5848830f
PB
20612 HOST_WIDE_INT amount;
20613 arm_stack_offsets *offsets;
defc0463
RE
20614 int regno;
20615
6d3d9133
NC
20616 /* Naked functions don't have prologues. */
20617 if (IS_NAKED (arm_current_func_type ()))
d5b7b3ae
RE
20618 return;
20619
5848830f
PB
20620 offsets = arm_get_frame_offsets ();
20621 amount = offsets->outgoing_args - offsets->saved_regs;
20622
d5b7b3ae 20623 if (frame_pointer_needed)
2591db65
RE
20624 {
20625 emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
20626 amount = offsets->locals_base - offsets->saved_regs;
20627 }
e784c52c 20628 amount -= 4 * thumb1_extra_regs_pushed (offsets, false);
e0b92319 20629
a3a531ec 20630 gcc_assert (amount >= 0);
2591db65 20631 if (amount)
d5b7b3ae 20632 {
d5b7b3ae
RE
20633 if (amount < 512)
20634 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
20635 GEN_INT (amount)));
20636 else
20637 {
20638 /* r3 is always free in the epilogue. */
f1c25d3b 20639 rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
d5b7b3ae
RE
20640
20641 emit_insn (gen_movsi (reg, GEN_INT (amount)));
20642 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
20643 }
20644 }
f676971a 20645
d5b7b3ae
RE
20646 /* Emit a USE (stack_pointer_rtx), so that
20647 the stack adjustment will not be deleted. */
6bacc7b0 20648 emit_insn (gen_prologue_use (stack_pointer_rtx));
d5b7b3ae 20649
e3b5732b 20650 if (crtl->profile || !TARGET_SCHED_PROLOG)
d5b7b3ae 20651 emit_insn (gen_blockage ());
defc0463
RE
20652
20653 /* Emit a clobber for each insn that will be restored in the epilogue,
20654 so that flow2 will get register lifetimes correct. */
20655 for (regno = 0; regno < 13; regno++)
6fb5fa3c 20656 if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
c41c1387 20657 emit_clobber (gen_rtx_REG (SImode, regno));
defc0463 20658
6fb5fa3c 20659 if (! df_regs_ever_live_p (LR_REGNUM))
c41c1387 20660 emit_use (gen_rtx_REG (SImode, LR_REGNUM));
d5b7b3ae
RE
20661}
20662
08c148a8 20663static void
5b3e6663 20664thumb1_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
d5b7b3ae 20665{
954954d1 20666 arm_stack_offsets *offsets;
b279b20a
NC
20667 unsigned long live_regs_mask = 0;
20668 unsigned long l_mask;
20669 unsigned high_regs_pushed = 0;
980e61bb 20670 int cfa_offset = 0;
d5b7b3ae
RE
20671 int regno;
20672
6d3d9133 20673 if (IS_NAKED (arm_current_func_type ()))
d5b7b3ae
RE
20674 return;
20675
20676 if (is_called_in_ARM_mode (current_function_decl))
20677 {
20678 const char * name;
20679
e6d29d15
NS
20680 gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
20681 gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
20682 == SYMBOL_REF);
d5b7b3ae 20683 name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
f676971a 20684
d5b7b3ae
RE
20685 /* Generate code sequence to switch us into Thumb mode. */
20686 /* The .code 32 directive has already been emitted by
6d77b53e 20687 ASM_DECLARE_FUNCTION_NAME. */
d5b7b3ae
RE
20688 asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
20689 asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
20690
20691 /* Generate a label, so that the debugger will notice the
20692 change in instruction sets. This label is also used by
20693 the assembler to bypass the ARM code when this function
20694 is called from a Thumb encoded function elsewhere in the
20695 same file. Hence the definition of STUB_NAME here must
d6b4baa4 20696 agree with the definition in gas/config/tc-arm.c. */
f676971a 20697
d5b7b3ae 20698#define STUB_NAME ".real_start_of"
f676971a 20699
761c70aa 20700 fprintf (f, "\t.code\t16\n");
d5b7b3ae
RE
20701#ifdef ARM_PE
20702 if (arm_dllexport_name_p (name))
e5951263 20703 name = arm_strip_name_encoding (name);
f676971a 20704#endif
d5b7b3ae 20705 asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
761c70aa 20706 fprintf (f, "\t.thumb_func\n");
d5b7b3ae
RE
20707 asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
20708 }
f676971a 20709
38173d38 20710 if (crtl->args.pretend_args_size)
d5b7b3ae 20711 {
617a1b71 20712 /* Output unwind directive for the stack adjustment. */
f0a0390e 20713 if (arm_except_unwind_info () == UI_TARGET)
617a1b71 20714 fprintf (f, "\t.pad #%d\n",
38173d38 20715 crtl->args.pretend_args_size);
617a1b71 20716
3cb66fd7 20717 if (cfun->machine->uses_anonymous_args)
d5b7b3ae
RE
20718 {
20719 int num_pushes;
f676971a 20720
761c70aa 20721 fprintf (f, "\tpush\t{");
d5b7b3ae 20722
38173d38 20723 num_pushes = ARM_NUM_INTS (crtl->args.pretend_args_size);
f676971a 20724
d5b7b3ae
RE
20725 for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
20726 regno <= LAST_ARG_REGNUM;
5895f793 20727 regno++)
d5b7b3ae
RE
20728 asm_fprintf (f, "%r%s", regno,
20729 regno == LAST_ARG_REGNUM ? "" : ", ");
20730
761c70aa 20731 fprintf (f, "}\n");
d5b7b3ae
RE
20732 }
20733 else
f676971a 20734 asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
d5b7b3ae 20735 SP_REGNUM, SP_REGNUM,
38173d38 20736 crtl->args.pretend_args_size);
980e61bb
DJ
20737
20738 /* We don't need to record the stores for unwinding (would it
20739 help the debugger any if we did?), but record the change in
20740 the stack pointer. */
20741 if (dwarf2out_do_frame ())
20742 {
d342c045 20743 char *l = dwarf2out_cfi_label (false);
b279b20a 20744
38173d38 20745 cfa_offset = cfa_offset + crtl->args.pretend_args_size;
980e61bb
DJ
20746 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
20747 }
d5b7b3ae
RE
20748 }
20749
b279b20a 20750 /* Get the registers we are going to push. */
954954d1
PB
20751 offsets = arm_get_frame_offsets ();
20752 live_regs_mask = offsets->saved_regs_mask;
b279b20a 20753 /* Extract a mask of the ones we can give to the Thumb's push instruction. */
57934c39 20754 l_mask = live_regs_mask & 0x40ff;
b279b20a
NC
20755 /* Then count how many other high registers will need to be pushed. */
20756 high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
d5b7b3ae
RE
20757
20758 if (TARGET_BACKTRACE)
20759 {
b279b20a
NC
20760 unsigned offset;
20761 unsigned work_register;
f676971a 20762
d5b7b3ae
RE
20763 /* We have been asked to create a stack backtrace structure.
20764 The code looks like this:
f676971a 20765
d5b7b3ae
RE
20766 0 .align 2
20767 0 func:
20768 0 sub SP, #16 Reserve space for 4 registers.
57934c39 20769 2 push {R7} Push low registers.
d5b7b3ae
RE
20770 4 add R7, SP, #20 Get the stack pointer before the push.
20771 6 str R7, [SP, #8] Store the stack pointer (before reserving the space).
20772 8 mov R7, PC Get hold of the start of this code plus 12.
20773 10 str R7, [SP, #16] Store it.
20774 12 mov R7, FP Get hold of the current frame pointer.
20775 14 str R7, [SP, #4] Store it.
20776 16 mov R7, LR Get hold of the current return address.
20777 18 str R7, [SP, #12] Store it.
20778 20 add R7, SP, #16 Point at the start of the backtrace structure.
20779 22 mov FP, R7 Put this value into the frame pointer. */
20780
57934c39 20781 work_register = thumb_find_work_register (live_regs_mask);
f676971a 20782
f0a0390e 20783 if (arm_except_unwind_info () == UI_TARGET)
617a1b71
PB
20784 asm_fprintf (f, "\t.pad #16\n");
20785
d5b7b3ae
RE
20786 asm_fprintf
20787 (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
20788 SP_REGNUM, SP_REGNUM);
980e61bb
DJ
20789
20790 if (dwarf2out_do_frame ())
20791 {
d342c045 20792 char *l = dwarf2out_cfi_label (false);
b279b20a 20793
980e61bb
DJ
20794 cfa_offset = cfa_offset + 16;
20795 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
20796 }
20797
57934c39
PB
20798 if (l_mask)
20799 {
20800 thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
1a59548b 20801 offset = bit_count (l_mask) * UNITS_PER_WORD;
57934c39
PB
20802 }
20803 else
20804 offset = 0;
f676971a 20805
d5b7b3ae 20806 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
38173d38 20807 offset + 16 + crtl->args.pretend_args_size);
f676971a 20808
d5b7b3ae
RE
20809 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20810 offset + 4);
20811
20812 /* Make sure that the instruction fetching the PC is in the right place
20813 to calculate "start of backtrace creation code + 12". */
57934c39 20814 if (l_mask)
d5b7b3ae
RE
20815 {
20816 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
20817 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20818 offset + 12);
20819 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
20820 ARM_HARD_FRAME_POINTER_REGNUM);
20821 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20822 offset);
20823 }
20824 else
20825 {
20826 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
20827 ARM_HARD_FRAME_POINTER_REGNUM);
20828 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20829 offset);
20830 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
20831 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20832 offset + 12);
20833 }
f676971a 20834
d5b7b3ae
RE
20835 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
20836 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20837 offset + 8);
20838 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
20839 offset + 12);
20840 asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
20841 ARM_HARD_FRAME_POINTER_REGNUM, work_register);
20842 }
0fa2e4df 20843 /* Optimization: If we are not pushing any low registers but we are going
b279b20a
NC
20844 to push some high registers then delay our first push. This will just
20845 be a push of LR and we can combine it with the push of the first high
20846 register. */
20847 else if ((l_mask & 0xff) != 0
20848 || (high_regs_pushed == 0 && l_mask))
cb751cbd
BS
20849 {
20850 unsigned long mask = l_mask;
e784c52c 20851 mask |= (1 << thumb1_extra_regs_pushed (offsets, true)) - 1;
cb751cbd
BS
20852 thumb_pushpop (f, mask, 1, &cfa_offset, mask);
20853 }
d5b7b3ae 20854
d5b7b3ae
RE
20855 if (high_regs_pushed)
20856 {
b279b20a
NC
20857 unsigned pushable_regs;
20858 unsigned next_hi_reg;
d5b7b3ae
RE
20859
20860 for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
57934c39 20861 if (live_regs_mask & (1 << next_hi_reg))
e26053d1 20862 break;
d5b7b3ae 20863
57934c39 20864 pushable_regs = l_mask & 0xff;
d5b7b3ae
RE
20865
20866 if (pushable_regs == 0)
57934c39 20867 pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
d5b7b3ae
RE
20868
20869 while (high_regs_pushed > 0)
20870 {
b279b20a 20871 unsigned long real_regs_mask = 0;
980e61bb 20872
b279b20a 20873 for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
d5b7b3ae 20874 {
57934c39 20875 if (pushable_regs & (1 << regno))
d5b7b3ae
RE
20876 {
20877 asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
f676971a 20878
b279b20a 20879 high_regs_pushed --;
980e61bb 20880 real_regs_mask |= (1 << next_hi_reg);
f676971a 20881
d5b7b3ae 20882 if (high_regs_pushed)
aeaf4d25 20883 {
b279b20a
NC
20884 for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
20885 next_hi_reg --)
57934c39 20886 if (live_regs_mask & (1 << next_hi_reg))
d5b7b3ae 20887 break;
aeaf4d25 20888 }
d5b7b3ae
RE
20889 else
20890 {
57934c39 20891 pushable_regs &= ~((1 << regno) - 1);
d5b7b3ae
RE
20892 break;
20893 }
20894 }
20895 }
980e61bb 20896
b279b20a
NC
20897 /* If we had to find a work register and we have not yet
20898 saved the LR then add it to the list of regs to push. */
20899 if (l_mask == (1 << LR_REGNUM))
20900 {
20901 thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
20902 1, &cfa_offset,
20903 real_regs_mask | (1 << LR_REGNUM));
20904 l_mask = 0;
20905 }
20906 else
20907 thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
d5b7b3ae 20908 }
d5b7b3ae
RE
20909 }
20910}
20911
20912/* Handle the case of a double word load into a low register from
20913 a computed memory address. The computed address may involve a
20914 register which is overwritten by the load. */
cd2b33d0 20915const char *
e32bac5b 20916thumb_load_double_from_address (rtx *operands)
d5b7b3ae
RE
20917{
20918 rtx addr;
20919 rtx base;
20920 rtx offset;
20921 rtx arg1;
20922 rtx arg2;
f676971a 20923
e6d29d15
NS
20924 gcc_assert (GET_CODE (operands[0]) == REG);
20925 gcc_assert (GET_CODE (operands[1]) == MEM);
d5b7b3ae
RE
20926
20927 /* Get the memory address. */
20928 addr = XEXP (operands[1], 0);
f676971a 20929
d5b7b3ae
RE
20930 /* Work out how the memory address is computed. */
20931 switch (GET_CODE (addr))
20932 {
20933 case REG:
31fa16b6 20934 operands[2] = adjust_address (operands[1], SImode, 4);
e0b92319 20935
d5b7b3ae
RE
20936 if (REGNO (operands[0]) == REGNO (addr))
20937 {
20938 output_asm_insn ("ldr\t%H0, %2", operands);
20939 output_asm_insn ("ldr\t%0, %1", operands);
20940 }
20941 else
20942 {
20943 output_asm_insn ("ldr\t%0, %1", operands);
20944 output_asm_insn ("ldr\t%H0, %2", operands);
20945 }
20946 break;
f676971a 20947
d5b7b3ae
RE
20948 case CONST:
20949 /* Compute <address> + 4 for the high order load. */
31fa16b6 20950 operands[2] = adjust_address (operands[1], SImode, 4);
e0b92319 20951
d5b7b3ae
RE
20952 output_asm_insn ("ldr\t%0, %1", operands);
20953 output_asm_insn ("ldr\t%H0, %2", operands);
20954 break;
f676971a 20955
d5b7b3ae
RE
20956 case PLUS:
20957 arg1 = XEXP (addr, 0);
20958 arg2 = XEXP (addr, 1);
f676971a 20959
d5b7b3ae
RE
20960 if (CONSTANT_P (arg1))
20961 base = arg2, offset = arg1;
20962 else
20963 base = arg1, offset = arg2;
f676971a 20964
e6d29d15 20965 gcc_assert (GET_CODE (base) == REG);
d5b7b3ae
RE
20966
20967 /* Catch the case of <address> = <reg> + <reg> */
20968 if (GET_CODE (offset) == REG)
20969 {
20970 int reg_offset = REGNO (offset);
20971 int reg_base = REGNO (base);
20972 int reg_dest = REGNO (operands[0]);
f676971a 20973
d5b7b3ae
RE
20974 /* Add the base and offset registers together into the
20975 higher destination register. */
20976 asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
20977 reg_dest + 1, reg_base, reg_offset);
f676971a 20978
d5b7b3ae
RE
20979 /* Load the lower destination register from the address in
20980 the higher destination register. */
20981 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
20982 reg_dest, reg_dest + 1);
f676971a 20983
d5b7b3ae
RE
20984 /* Load the higher destination register from its own address
20985 plus 4. */
20986 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
20987 reg_dest + 1, reg_dest + 1);
20988 }
20989 else
20990 {
20991 /* Compute <address> + 4 for the high order load. */
31fa16b6 20992 operands[2] = adjust_address (operands[1], SImode, 4);
f676971a 20993
d5b7b3ae
RE
20994 /* If the computed address is held in the low order register
20995 then load the high order register first, otherwise always
20996 load the low order register first. */
20997 if (REGNO (operands[0]) == REGNO (base))
20998 {
20999 output_asm_insn ("ldr\t%H0, %2", operands);
21000 output_asm_insn ("ldr\t%0, %1", operands);
21001 }
21002 else
21003 {
21004 output_asm_insn ("ldr\t%0, %1", operands);
21005 output_asm_insn ("ldr\t%H0, %2", operands);
21006 }
21007 }
21008 break;
21009
21010 case LABEL_REF:
21011 /* With no registers to worry about we can just load the value
21012 directly. */
31fa16b6 21013 operands[2] = adjust_address (operands[1], SImode, 4);
f676971a 21014
d5b7b3ae
RE
21015 output_asm_insn ("ldr\t%H0, %2", operands);
21016 output_asm_insn ("ldr\t%0, %1", operands);
21017 break;
f676971a 21018
d5b7b3ae 21019 default:
e6d29d15 21020 gcc_unreachable ();
d5b7b3ae 21021 }
f676971a 21022
d5b7b3ae
RE
21023 return "";
21024}
21025
cd2b33d0 21026const char *
e32bac5b 21027thumb_output_move_mem_multiple (int n, rtx *operands)
d5b7b3ae
RE
21028{
21029 rtx tmp;
21030
21031 switch (n)
21032 {
21033 case 2:
ca356f3a 21034 if (REGNO (operands[4]) > REGNO (operands[5]))
d5b7b3ae 21035 {
ca356f3a
RE
21036 tmp = operands[4];
21037 operands[4] = operands[5];
21038 operands[5] = tmp;
d5b7b3ae 21039 }
ca356f3a
RE
21040 output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
21041 output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
d5b7b3ae
RE
21042 break;
21043
21044 case 3:
ca356f3a 21045 if (REGNO (operands[4]) > REGNO (operands[5]))
d5b7b3ae 21046 {
ca356f3a
RE
21047 tmp = operands[4];
21048 operands[4] = operands[5];
21049 operands[5] = tmp;
d5b7b3ae 21050 }
ca356f3a 21051 if (REGNO (operands[5]) > REGNO (operands[6]))
d5b7b3ae 21052 {
ca356f3a
RE
21053 tmp = operands[5];
21054 operands[5] = operands[6];
21055 operands[6] = tmp;
d5b7b3ae 21056 }
ca356f3a 21057 if (REGNO (operands[4]) > REGNO (operands[5]))
d5b7b3ae 21058 {
ca356f3a
RE
21059 tmp = operands[4];
21060 operands[4] = operands[5];
21061 operands[5] = tmp;
d5b7b3ae 21062 }
f676971a 21063
ca356f3a
RE
21064 output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
21065 output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
d5b7b3ae
RE
21066 break;
21067
21068 default:
e6d29d15 21069 gcc_unreachable ();
d5b7b3ae
RE
21070 }
21071
21072 return "";
21073}
21074
b12a00f1
RE
21075/* Output a call-via instruction for thumb state. */
21076const char *
21077thumb_call_via_reg (rtx reg)
21078{
21079 int regno = REGNO (reg);
21080 rtx *labelp;
21081
57ecec57 21082 gcc_assert (regno < LR_REGNUM);
b12a00f1
RE
21083
21084 /* If we are in the normal text section we can use a single instance
21085 per compilation unit. If we are doing function sections, then we need
21086 an entry per section, since we can't rely on reachability. */
d6b5193b 21087 if (in_section == text_section)
b12a00f1
RE
21088 {
21089 thumb_call_reg_needed = 1;
21090
21091 if (thumb_call_via_label[regno] == NULL)
21092 thumb_call_via_label[regno] = gen_label_rtx ();
21093 labelp = thumb_call_via_label + regno;
21094 }
21095 else
21096 {
21097 if (cfun->machine->call_via[regno] == NULL)
21098 cfun->machine->call_via[regno] = gen_label_rtx ();
21099 labelp = cfun->machine->call_via + regno;
21100 }
21101
21102 output_asm_insn ("bl\t%a0", labelp);
21103 return "";
21104}
21105
1d6e90ac 21106/* Routines for generating rtl. */
d5b7b3ae 21107void
70128ad9 21108thumb_expand_movmemqi (rtx *operands)
d5b7b3ae
RE
21109{
21110 rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
21111 rtx in = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
21112 HOST_WIDE_INT len = INTVAL (operands[2]);
21113 HOST_WIDE_INT offset = 0;
21114
21115 while (len >= 12)
21116 {
ca356f3a 21117 emit_insn (gen_movmem12b (out, in, out, in));
d5b7b3ae
RE
21118 len -= 12;
21119 }
f676971a 21120
d5b7b3ae
RE
21121 if (len >= 8)
21122 {
ca356f3a 21123 emit_insn (gen_movmem8b (out, in, out, in));
d5b7b3ae
RE
21124 len -= 8;
21125 }
f676971a 21126
d5b7b3ae
RE
21127 if (len >= 4)
21128 {
21129 rtx reg = gen_reg_rtx (SImode);
f1c25d3b
KH
21130 emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
21131 emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
d5b7b3ae
RE
21132 len -= 4;
21133 offset += 4;
21134 }
f676971a 21135
d5b7b3ae
RE
21136 if (len >= 2)
21137 {
21138 rtx reg = gen_reg_rtx (HImode);
f676971a 21139 emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
f1c25d3b
KH
21140 plus_constant (in, offset))));
21141 emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
d5b7b3ae
RE
21142 reg));
21143 len -= 2;
21144 offset += 2;
21145 }
f676971a 21146
d5b7b3ae
RE
21147 if (len)
21148 {
21149 rtx reg = gen_reg_rtx (QImode);
f1c25d3b
KH
21150 emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
21151 plus_constant (in, offset))));
21152 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
d5b7b3ae
RE
21153 reg));
21154 }
21155}
21156
d5b7b3ae 21157void
e32bac5b 21158thumb_reload_out_hi (rtx *operands)
d5b7b3ae
RE
21159{
21160 emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
21161}
21162
f676971a 21163/* Handle reading a half-word from memory during reload. */
d5b7b3ae 21164void
e32bac5b 21165thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
d5b7b3ae 21166{
e6d29d15 21167 gcc_unreachable ();
d5b7b3ae
RE
21168}
21169
c27ba912
DM
21170/* Return the length of a function name prefix
21171 that starts with the character 'c'. */
21172static int
e32bac5b 21173arm_get_strip_length (int c)
c27ba912
DM
21174{
21175 switch (c)
21176 {
21177 ARM_NAME_ENCODING_LENGTHS
f676971a 21178 default: return 0;
c27ba912
DM
21179 }
21180}
21181
21182/* Return a pointer to a function's name with any
21183 and all prefix encodings stripped from it. */
21184const char *
e32bac5b 21185arm_strip_name_encoding (const char *name)
c27ba912
DM
21186{
21187 int skip;
f676971a 21188
c27ba912
DM
21189 while ((skip = arm_get_strip_length (* name)))
21190 name += skip;
21191
21192 return name;
21193}
21194
e1944073
KW
21195/* If there is a '*' anywhere in the name's prefix, then
21196 emit the stripped name verbatim, otherwise prepend an
21197 underscore if leading underscores are being used. */
e1944073 21198void
e32bac5b 21199arm_asm_output_labelref (FILE *stream, const char *name)
e1944073
KW
21200{
21201 int skip;
21202 int verbatim = 0;
21203
21204 while ((skip = arm_get_strip_length (* name)))
21205 {
21206 verbatim |= (*name == '*');
21207 name += skip;
21208 }
21209
21210 if (verbatim)
21211 fputs (name, stream);
21212 else
21213 asm_fprintf (stream, "%U%s", name);
21214}
21215
6c6aa1af
PB
21216static void
21217arm_file_start (void)
21218{
21219 int val;
21220
5b3e6663
PB
21221 if (TARGET_UNIFIED_ASM)
21222 asm_fprintf (asm_out_file, "\t.syntax unified\n");
21223
6c6aa1af
PB
21224 if (TARGET_BPABI)
21225 {
21226 const char *fpu_name;
12a0a4d4
PB
21227 if (arm_selected_arch)
21228 asm_fprintf (asm_out_file, "\t.arch %s\n", arm_selected_arch->name);
6c6aa1af 21229 else
12a0a4d4 21230 asm_fprintf (asm_out_file, "\t.cpu %s\n", arm_selected_cpu->name);
6c6aa1af
PB
21231
21232 if (TARGET_SOFT_FLOAT)
21233 {
21234 if (TARGET_VFP)
21235 fpu_name = "softvfp";
21236 else
21237 fpu_name = "softfpa";
21238 }
21239 else
21240 {
d79f3032
PB
21241 fpu_name = arm_fpu_desc->name;
21242 if (arm_fpu_desc->model == ARM_FP_MODEL_VFP)
f1adb0a9
JB
21243 {
21244 if (TARGET_HARD_FLOAT)
21245 asm_fprintf (asm_out_file, "\t.eabi_attribute 27, 3\n");
21246 if (TARGET_HARD_FLOAT_ABI)
21247 asm_fprintf (asm_out_file, "\t.eabi_attribute 28, 1\n");
21248 }
6c6aa1af
PB
21249 }
21250 asm_fprintf (asm_out_file, "\t.fpu %s\n", fpu_name);
21251
21252 /* Some of these attributes only apply when the corresponding features
21253 are used. However we don't have any easy way of figuring this out.
21254 Conservatively record the setting that would have been used. */
21255
6c6aa1af
PB
21256 /* Tag_ABI_FP_rounding. */
21257 if (flag_rounding_math)
21258 asm_fprintf (asm_out_file, "\t.eabi_attribute 19, 1\n");
21259 if (!flag_unsafe_math_optimizations)
21260 {
21261 /* Tag_ABI_FP_denomal. */
21262 asm_fprintf (asm_out_file, "\t.eabi_attribute 20, 1\n");
21263 /* Tag_ABI_FP_exceptions. */
21264 asm_fprintf (asm_out_file, "\t.eabi_attribute 21, 1\n");
21265 }
21266 /* Tag_ABI_FP_user_exceptions. */
21267 if (flag_signaling_nans)
21268 asm_fprintf (asm_out_file, "\t.eabi_attribute 22, 1\n");
21269 /* Tag_ABI_FP_number_model. */
21270 asm_fprintf (asm_out_file, "\t.eabi_attribute 23, %d\n",
21271 flag_finite_math_only ? 1 : 3);
21272
21273 /* Tag_ABI_align8_needed. */
21274 asm_fprintf (asm_out_file, "\t.eabi_attribute 24, 1\n");
21275 /* Tag_ABI_align8_preserved. */
21276 asm_fprintf (asm_out_file, "\t.eabi_attribute 25, 1\n");
21277 /* Tag_ABI_enum_size. */
21278 asm_fprintf (asm_out_file, "\t.eabi_attribute 26, %d\n",
21279 flag_short_enums ? 1 : 2);
21280
21281 /* Tag_ABI_optimization_goals. */
21282 if (optimize_size)
21283 val = 4;
21284 else if (optimize >= 2)
21285 val = 2;
21286 else if (optimize)
21287 val = 1;
21288 else
21289 val = 6;
21290 asm_fprintf (asm_out_file, "\t.eabi_attribute 30, %d\n", val);
b76c3c4b 21291
0fd8c3ad
SL
21292 /* Tag_ABI_FP_16bit_format. */
21293 if (arm_fp16_format)
21294 asm_fprintf (asm_out_file, "\t.eabi_attribute 38, %d\n",
21295 (int)arm_fp16_format);
21296
b76c3c4b
PB
21297 if (arm_lang_output_object_attributes_hook)
21298 arm_lang_output_object_attributes_hook();
6c6aa1af
PB
21299 }
21300 default_file_start();
21301}
21302
b12a00f1
RE
21303static void
21304arm_file_end (void)
21305{
21306 int regno;
21307
978e411f
CD
21308 if (NEED_INDICATE_EXEC_STACK)
21309 /* Add .note.GNU-stack. */
21310 file_end_indicate_exec_stack ();
21311
b12a00f1
RE
21312 if (! thumb_call_reg_needed)
21313 return;
21314
d6b5193b 21315 switch_to_section (text_section);
b12a00f1
RE
21316 asm_fprintf (asm_out_file, "\t.code 16\n");
21317 ASM_OUTPUT_ALIGN (asm_out_file, 1);
21318
57ecec57 21319 for (regno = 0; regno < LR_REGNUM; regno++)
b12a00f1
RE
21320 {
21321 rtx label = thumb_call_via_label[regno];
21322
21323 if (label != 0)
21324 {
21325 targetm.asm_out.internal_label (asm_out_file, "L",
21326 CODE_LABEL_NUMBER (label));
21327 asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
21328 }
21329 }
21330}
21331
fb49053f
RH
21332#ifndef ARM_PE
21333/* Symbols in the text segment can be accessed without indirecting via the
21334 constant pool; it may take an extra binary operation, but this is still
21335 faster than indirecting via memory. Don't do this when not optimizing,
21336 since we won't be calculating al of the offsets necessary to do this
21337 simplification. */
21338
21339static void
e32bac5b 21340arm_encode_section_info (tree decl, rtx rtl, int first)
fb49053f 21341{
3521b33c 21342 if (optimize > 0 && TREE_CONSTANT (decl))
c6a2438a 21343 SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
fb49053f 21344
d3585b76 21345 default_encode_section_info (decl, rtl, first);
fb49053f
RH
21346}
21347#endif /* !ARM_PE */
483ab821 21348
4977bab6 21349static void
e32bac5b 21350arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
4977bab6
ZW
21351{
21352 if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
21353 && !strcmp (prefix, "L"))
21354 {
21355 arm_ccfsm_state = 0;
21356 arm_target_insn = NULL;
21357 }
21358 default_internal_label (stream, prefix, labelno);
21359}
21360
c590b625
RH
21361/* Output code to add DELTA to the first argument, and then jump
21362 to FUNCTION. Used for C++ multiple inheritance. */
c590b625 21363static void
e32bac5b
RE
21364arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
21365 HOST_WIDE_INT delta,
21366 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
21367 tree function)
483ab821 21368{
9b66ebb1
PB
21369 static int thunk_label = 0;
21370 char label[256];
54b9e939 21371 char labelpc[256];
483ab821
MM
21372 int mi_delta = delta;
21373 const char *const mi_op = mi_delta < 0 ? "sub" : "add";
21374 int shift = 0;
61f71b34 21375 int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
483ab821
MM
21376 ? 1 : 0);
21377 if (mi_delta < 0)
21378 mi_delta = - mi_delta;
bf98ec6c 21379
5b3e6663 21380 if (TARGET_THUMB1)
9b66ebb1
PB
21381 {
21382 int labelno = thunk_label++;
21383 ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
bf98ec6c
PB
21384 /* Thunks are entered in arm mode when avaiable. */
21385 if (TARGET_THUMB1_ONLY)
21386 {
21387 /* push r3 so we can use it as a temporary. */
21388 /* TODO: Omit this save if r3 is not used. */
21389 fputs ("\tpush {r3}\n", file);
21390 fputs ("\tldr\tr3, ", file);
21391 }
21392 else
21393 {
21394 fputs ("\tldr\tr12, ", file);
21395 }
9b66ebb1
PB
21396 assemble_name (file, label);
21397 fputc ('\n', file);
54b9e939
KH
21398 if (flag_pic)
21399 {
21400 /* If we are generating PIC, the ldr instruction below loads
21401 "(target - 7) - .LTHUNKPCn" into r12. The pc reads as
21402 the address of the add + 8, so we have:
21403
21404 r12 = (target - 7) - .LTHUNKPCn + (.LTHUNKPCn + 8)
21405 = target + 1.
21406
21407 Note that we have "+ 1" because some versions of GNU ld
21408 don't set the low bit of the result for R_ARM_REL32
bf98ec6c
PB
21409 relocations against thumb function symbols.
21410 On ARMv6M this is +4, not +8. */
54b9e939
KH
21411 ASM_GENERATE_INTERNAL_LABEL (labelpc, "LTHUNKPC", labelno);
21412 assemble_name (file, labelpc);
21413 fputs (":\n", file);
bf98ec6c
PB
21414 if (TARGET_THUMB1_ONLY)
21415 {
21416 /* This is 2 insns after the start of the thunk, so we know it
21417 is 4-byte aligned. */
21418 fputs ("\tadd\tr3, pc, r3\n", file);
21419 fputs ("\tmov r12, r3\n", file);
21420 }
21421 else
21422 fputs ("\tadd\tr12, pc, r12\n", file);
54b9e939 21423 }
bf98ec6c
PB
21424 else if (TARGET_THUMB1_ONLY)
21425 fputs ("\tmov r12, r3\n", file);
9b66ebb1 21426 }
bf98ec6c 21427 if (TARGET_THUMB1_ONLY)
483ab821 21428 {
bf98ec6c
PB
21429 if (mi_delta > 255)
21430 {
21431 fputs ("\tldr\tr3, ", file);
21432 assemble_name (file, label);
21433 fputs ("+4\n", file);
21434 asm_fprintf (file, "\t%s\t%r, %r, r3\n",
21435 mi_op, this_regno, this_regno);
21436 }
21437 else if (mi_delta != 0)
21438 {
21439 asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
21440 mi_op, this_regno, this_regno,
21441 mi_delta);
21442 }
21443 }
21444 else
21445 {
21446 /* TODO: Use movw/movt for large constants when available. */
21447 while (mi_delta != 0)
21448 {
21449 if ((mi_delta & (3 << shift)) == 0)
21450 shift += 2;
21451 else
21452 {
21453 asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
21454 mi_op, this_regno, this_regno,
21455 mi_delta & (0xff << shift));
21456 mi_delta &= ~(0xff << shift);
21457 shift += 8;
21458 }
21459 }
483ab821 21460 }
5b3e6663 21461 if (TARGET_THUMB1)
9b66ebb1 21462 {
bf98ec6c
PB
21463 if (TARGET_THUMB1_ONLY)
21464 fputs ("\tpop\t{r3}\n", file);
21465
9b66ebb1
PB
21466 fprintf (file, "\tbx\tr12\n");
21467 ASM_OUTPUT_ALIGN (file, 2);
21468 assemble_name (file, label);
21469 fputs (":\n", file);
54b9e939
KH
21470 if (flag_pic)
21471 {
21472 /* Output ".word .LTHUNKn-7-.LTHUNKPCn". */
21473 rtx tem = XEXP (DECL_RTL (function), 0);
21474 tem = gen_rtx_PLUS (GET_MODE (tem), tem, GEN_INT (-7));
21475 tem = gen_rtx_MINUS (GET_MODE (tem),
21476 tem,
21477 gen_rtx_SYMBOL_REF (Pmode,
21478 ggc_strdup (labelpc)));
21479 assemble_integer (tem, 4, BITS_PER_WORD, 1);
21480 }
21481 else
21482 /* Output ".word .LTHUNKn". */
21483 assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
bf98ec6c
PB
21484
21485 if (TARGET_THUMB1_ONLY && mi_delta > 255)
21486 assemble_integer (GEN_INT(mi_delta), 4, BITS_PER_WORD, 1);
9b66ebb1
PB
21487 }
21488 else
21489 {
21490 fputs ("\tb\t", file);
21491 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
21492 if (NEED_PLT_RELOC)
21493 fputs ("(PLT)", file);
21494 fputc ('\n', file);
21495 }
483ab821 21496}
5a9335ef
NC
21497
21498int
6f5f2481 21499arm_emit_vector_const (FILE *file, rtx x)
5a9335ef
NC
21500{
21501 int i;
21502 const char * pattern;
21503
e6d29d15 21504 gcc_assert (GET_CODE (x) == CONST_VECTOR);
5a9335ef
NC
21505
21506 switch (GET_MODE (x))
21507 {
21508 case V2SImode: pattern = "%08x"; break;
21509 case V4HImode: pattern = "%04x"; break;
21510 case V8QImode: pattern = "%02x"; break;
e6d29d15 21511 default: gcc_unreachable ();
5a9335ef
NC
21512 }
21513
21514 fprintf (file, "0x");
21515 for (i = CONST_VECTOR_NUNITS (x); i--;)
21516 {
21517 rtx element;
21518
21519 element = CONST_VECTOR_ELT (x, i);
21520 fprintf (file, pattern, INTVAL (element));
21521 }
21522
21523 return 1;
21524}
21525
0fd8c3ad
SL
21526/* Emit a fp16 constant appropriately padded to occupy a 4-byte word.
21527 HFmode constant pool entries are actually loaded with ldr. */
21528void
21529arm_emit_fp16_const (rtx c)
21530{
21531 REAL_VALUE_TYPE r;
21532 long bits;
21533
21534 REAL_VALUE_FROM_CONST_DOUBLE (r, c);
21535 bits = real_to_target (NULL, &r, HFmode);
21536 if (WORDS_BIG_ENDIAN)
21537 assemble_zeros (2);
21538 assemble_integer (GEN_INT (bits), 2, BITS_PER_WORD, 1);
21539 if (!WORDS_BIG_ENDIAN)
21540 assemble_zeros (2);
21541}
21542
5a9335ef 21543const char *
6f5f2481 21544arm_output_load_gr (rtx *operands)
5a9335ef
NC
21545{
21546 rtx reg;
21547 rtx offset;
21548 rtx wcgr;
21549 rtx sum;
f676971a 21550
5a9335ef
NC
21551 if (GET_CODE (operands [1]) != MEM
21552 || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
21553 || GET_CODE (reg = XEXP (sum, 0)) != REG
21554 || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
21555 || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
21556 return "wldrw%?\t%0, %1";
f676971a
EC
21557
21558 /* Fix up an out-of-range load of a GR register. */
5a9335ef
NC
21559 output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
21560 wcgr = operands[0];
21561 operands[0] = reg;
21562 output_asm_insn ("ldr%?\t%0, %1", operands);
21563
21564 operands[0] = wcgr;
21565 operands[1] = reg;
21566 output_asm_insn ("tmcr%?\t%0, %1", operands);
21567 output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
21568
21569 return "";
21570}
f9ba5949 21571
1cc9f5f5
KH
21572/* Worker function for TARGET_SETUP_INCOMING_VARARGS.
21573
21574 On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
21575 named arg and all anonymous args onto the stack.
21576 XXX I know the prologue shouldn't be pushing registers, but it is faster
21577 that way. */
21578
21579static void
390b17c2 21580arm_setup_incoming_varargs (CUMULATIVE_ARGS *pcum,
22ccaaee
JJ
21581 enum machine_mode mode,
21582 tree type,
1cc9f5f5
KH
21583 int *pretend_size,
21584 int second_time ATTRIBUTE_UNUSED)
21585{
390b17c2
RE
21586 int nregs;
21587
1cc9f5f5 21588 cfun->machine->uses_anonymous_args = 1;
390b17c2
RE
21589 if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
21590 {
21591 nregs = pcum->aapcs_ncrn;
21592 if ((nregs & 1) && arm_needs_doubleword_align (mode, type))
21593 nregs++;
21594 }
21595 else
21596 nregs = pcum->nregs;
21597
22ccaaee
JJ
21598 if (nregs < NUM_ARG_REGS)
21599 *pretend_size = (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
1cc9f5f5 21600}
9b66ebb1 21601
59b9a953 21602/* Return nonzero if the CONSUMER instruction (a store) does not need
9b66ebb1
PB
21603 PRODUCER's value to calculate the address. */
21604
21605int
21606arm_no_early_store_addr_dep (rtx producer, rtx consumer)
21607{
21608 rtx value = PATTERN (producer);
21609 rtx addr = PATTERN (consumer);
21610
21611 if (GET_CODE (value) == COND_EXEC)
21612 value = COND_EXEC_CODE (value);
21613 if (GET_CODE (value) == PARALLEL)
21614 value = XVECEXP (value, 0, 0);
21615 value = XEXP (value, 0);
21616 if (GET_CODE (addr) == COND_EXEC)
21617 addr = COND_EXEC_CODE (addr);
21618 if (GET_CODE (addr) == PARALLEL)
21619 addr = XVECEXP (addr, 0, 0);
21620 addr = XEXP (addr, 0);
f676971a 21621
9b66ebb1
PB
21622 return !reg_overlap_mentioned_p (value, addr);
21623}
21624
47d8f18d
JZ
21625/* Return nonzero if the CONSUMER instruction (a store) does need
21626 PRODUCER's value to calculate the address. */
21627
21628int
21629arm_early_store_addr_dep (rtx producer, rtx consumer)
21630{
21631 return !arm_no_early_store_addr_dep (producer, consumer);
21632}
21633
21634/* Return nonzero if the CONSUMER instruction (a load) does need
21635 PRODUCER's value to calculate the address. */
21636
21637int
21638arm_early_load_addr_dep (rtx producer, rtx consumer)
21639{
21640 rtx value = PATTERN (producer);
21641 rtx addr = PATTERN (consumer);
21642
21643 if (GET_CODE (value) == COND_EXEC)
21644 value = COND_EXEC_CODE (value);
21645 if (GET_CODE (value) == PARALLEL)
21646 value = XVECEXP (value, 0, 0);
21647 value = XEXP (value, 0);
21648 if (GET_CODE (addr) == COND_EXEC)
21649 addr = COND_EXEC_CODE (addr);
21650 if (GET_CODE (addr) == PARALLEL)
21651 addr = XVECEXP (addr, 0, 0);
21652 addr = XEXP (addr, 1);
21653
21654 return reg_overlap_mentioned_p (value, addr);
21655}
21656
59b9a953 21657/* Return nonzero if the CONSUMER instruction (an ALU op) does not
9b66ebb1
PB
21658 have an early register shift value or amount dependency on the
21659 result of PRODUCER. */
21660
21661int
21662arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
21663{
21664 rtx value = PATTERN (producer);
21665 rtx op = PATTERN (consumer);
21666 rtx early_op;
21667
21668 if (GET_CODE (value) == COND_EXEC)
21669 value = COND_EXEC_CODE (value);
21670 if (GET_CODE (value) == PARALLEL)
21671 value = XVECEXP (value, 0, 0);
21672 value = XEXP (value, 0);
21673 if (GET_CODE (op) == COND_EXEC)
21674 op = COND_EXEC_CODE (op);
21675 if (GET_CODE (op) == PARALLEL)
21676 op = XVECEXP (op, 0, 0);
21677 op = XEXP (op, 1);
f676971a 21678
9b66ebb1
PB
21679 early_op = XEXP (op, 0);
21680 /* This is either an actual independent shift, or a shift applied to
21681 the first operand of another operation. We want the whole shift
21682 operation. */
21683 if (GET_CODE (early_op) == REG)
21684 early_op = op;
21685
21686 return !reg_overlap_mentioned_p (value, early_op);
21687}
21688
59b9a953 21689/* Return nonzero if the CONSUMER instruction (an ALU op) does not
9b66ebb1
PB
21690 have an early register shift value dependency on the result of
21691 PRODUCER. */
21692
21693int
21694arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
21695{
21696 rtx value = PATTERN (producer);
21697 rtx op = PATTERN (consumer);
21698 rtx early_op;
21699
21700 if (GET_CODE (value) == COND_EXEC)
21701 value = COND_EXEC_CODE (value);
21702 if (GET_CODE (value) == PARALLEL)
21703 value = XVECEXP (value, 0, 0);
21704 value = XEXP (value, 0);
21705 if (GET_CODE (op) == COND_EXEC)
21706 op = COND_EXEC_CODE (op);
21707 if (GET_CODE (op) == PARALLEL)
21708 op = XVECEXP (op, 0, 0);
21709 op = XEXP (op, 1);
f676971a 21710
9b66ebb1
PB
21711 early_op = XEXP (op, 0);
21712
21713 /* This is either an actual independent shift, or a shift applied to
21714 the first operand of another operation. We want the value being
21715 shifted, in either case. */
21716 if (GET_CODE (early_op) != REG)
21717 early_op = XEXP (early_op, 0);
f676971a 21718
9b66ebb1
PB
21719 return !reg_overlap_mentioned_p (value, early_op);
21720}
21721
59b9a953 21722/* Return nonzero if the CONSUMER (a mul or mac op) does not
9b66ebb1
PB
21723 have an early register mult dependency on the result of
21724 PRODUCER. */
21725
21726int
21727arm_no_early_mul_dep (rtx producer, rtx consumer)
21728{
21729 rtx value = PATTERN (producer);
21730 rtx op = PATTERN (consumer);
21731
21732 if (GET_CODE (value) == COND_EXEC)
21733 value = COND_EXEC_CODE (value);
21734 if (GET_CODE (value) == PARALLEL)
21735 value = XVECEXP (value, 0, 0);
21736 value = XEXP (value, 0);
21737 if (GET_CODE (op) == COND_EXEC)
21738 op = COND_EXEC_CODE (op);
21739 if (GET_CODE (op) == PARALLEL)
21740 op = XVECEXP (op, 0, 0);
21741 op = XEXP (op, 1);
f676971a 21742
756f763b
PB
21743 if (GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
21744 {
21745 if (GET_CODE (XEXP (op, 0)) == MULT)
21746 return !reg_overlap_mentioned_p (value, XEXP (op, 0));
21747 else
21748 return !reg_overlap_mentioned_p (value, XEXP (op, 1));
21749 }
21750
21751 return 0;
9b66ebb1
PB
21752}
21753
70301b45
PB
21754/* We can't rely on the caller doing the proper promotion when
21755 using APCS or ATPCS. */
21756
21757static bool
586de218 21758arm_promote_prototypes (const_tree t ATTRIBUTE_UNUSED)
70301b45 21759{
b6685939 21760 return !TARGET_AAPCS_BASED;
70301b45
PB
21761}
21762
cde0f3fd
PB
21763static enum machine_mode
21764arm_promote_function_mode (const_tree type ATTRIBUTE_UNUSED,
21765 enum machine_mode mode,
21766 int *punsignedp ATTRIBUTE_UNUSED,
21767 const_tree fntype ATTRIBUTE_UNUSED,
21768 int for_return ATTRIBUTE_UNUSED)
21769{
21770 if (GET_MODE_CLASS (mode) == MODE_INT
21771 && GET_MODE_SIZE (mode) < 4)
21772 return SImode;
21773
21774 return mode;
21775}
6b045785
PB
21776
21777/* AAPCS based ABIs use short enums by default. */
21778
21779static bool
21780arm_default_short_enums (void)
21781{
077fc835 21782 return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
6b045785 21783}
13c1cd82
PB
21784
21785
21786/* AAPCS requires that anonymous bitfields affect structure alignment. */
21787
21788static bool
21789arm_align_anon_bitfield (void)
21790{
21791 return TARGET_AAPCS_BASED;
21792}
4185ae53
PB
21793
21794
21795/* The generic C++ ABI says 64-bit (long long). The EABI says 32-bit. */
21796
21797static tree
21798arm_cxx_guard_type (void)
21799{
21800 return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
21801}
21802
c956e102
MS
21803/* Return non-zero if the consumer (a multiply-accumulate instruction)
21804 has an accumulator dependency on the result of the producer (a
21805 multiplication instruction) and no other dependency on that result. */
21806int
21807arm_mac_accumulator_is_mul_result (rtx producer, rtx consumer)
21808{
21809 rtx mul = PATTERN (producer);
21810 rtx mac = PATTERN (consumer);
21811 rtx mul_result;
21812 rtx mac_op0, mac_op1, mac_acc;
21813
21814 if (GET_CODE (mul) == COND_EXEC)
21815 mul = COND_EXEC_CODE (mul);
21816 if (GET_CODE (mac) == COND_EXEC)
21817 mac = COND_EXEC_CODE (mac);
21818
21819 /* Check that mul is of the form (set (...) (mult ...))
21820 and mla is of the form (set (...) (plus (mult ...) (...))). */
21821 if ((GET_CODE (mul) != SET || GET_CODE (XEXP (mul, 1)) != MULT)
21822 || (GET_CODE (mac) != SET || GET_CODE (XEXP (mac, 1)) != PLUS
21823 || GET_CODE (XEXP (XEXP (mac, 1), 0)) != MULT))
21824 return 0;
21825
21826 mul_result = XEXP (mul, 0);
21827 mac_op0 = XEXP (XEXP (XEXP (mac, 1), 0), 0);
21828 mac_op1 = XEXP (XEXP (XEXP (mac, 1), 0), 1);
21829 mac_acc = XEXP (XEXP (mac, 1), 1);
21830
21831 return (reg_overlap_mentioned_p (mul_result, mac_acc)
21832 && !reg_overlap_mentioned_p (mul_result, mac_op0)
21833 && !reg_overlap_mentioned_p (mul_result, mac_op1));
21834}
21835
4185ae53 21836
0fa2e4df 21837/* The EABI says test the least significant bit of a guard variable. */
4185ae53
PB
21838
21839static bool
21840arm_cxx_guard_mask_bit (void)
21841{
21842 return TARGET_AAPCS_BASED;
21843}
46e995e0
PB
21844
21845
21846/* The EABI specifies that all array cookies are 8 bytes long. */
21847
21848static tree
21849arm_get_cookie_size (tree type)
21850{
21851 tree size;
21852
21853 if (!TARGET_AAPCS_BASED)
21854 return default_cxx_get_cookie_size (type);
21855
7d60be94 21856 size = build_int_cst (sizetype, 8);
46e995e0
PB
21857 return size;
21858}
21859
21860
21861/* The EABI says that array cookies should also contain the element size. */
21862
21863static bool
21864arm_cookie_has_size (void)
21865{
21866 return TARGET_AAPCS_BASED;
21867}
44d10c10
PB
21868
21869
21870/* The EABI says constructors and destructors should return a pointer to
21871 the object constructed/destroyed. */
21872
21873static bool
21874arm_cxx_cdtor_returns_this (void)
21875{
21876 return TARGET_AAPCS_BASED;
21877}
c9ca9b88 21878
505970fc
MM
21879/* The EABI says that an inline function may never be the key
21880 method. */
21881
21882static bool
21883arm_cxx_key_method_may_be_inline (void)
21884{
21885 return !TARGET_AAPCS_BASED;
21886}
21887
1e731102
MM
21888static void
21889arm_cxx_determine_class_data_visibility (tree decl)
21890{
711b2998
JB
21891 if (!TARGET_AAPCS_BASED
21892 || !TARGET_DLLIMPORT_DECL_ATTRIBUTES)
1e731102 21893 return;
505970fc 21894
1e731102
MM
21895 /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
21896 is exported. However, on systems without dynamic vague linkage,
21897 \S 3.2.5.6 says that COMDAT class data has hidden linkage. */
21898 if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
21899 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
21900 else
21901 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
21902 DECL_VISIBILITY_SPECIFIED (decl) = 1;
21903}
e0b92319 21904
505970fc 21905static bool
1e731102 21906arm_cxx_class_data_always_comdat (void)
505970fc 21907{
1e731102
MM
21908 /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
21909 vague linkage if the class has no key function. */
21910 return !TARGET_AAPCS_BASED;
505970fc 21911}
c9ca9b88 21912
9f62c3e3
PB
21913
21914/* The EABI says __aeabi_atexit should be used to register static
21915 destructors. */
21916
21917static bool
21918arm_cxx_use_aeabi_atexit (void)
21919{
21920 return TARGET_AAPCS_BASED;
21921}
21922
21923
c9ca9b88
PB
21924void
21925arm_set_return_address (rtx source, rtx scratch)
21926{
21927 arm_stack_offsets *offsets;
21928 HOST_WIDE_INT delta;
21929 rtx addr;
21930 unsigned long saved_regs;
21931
954954d1
PB
21932 offsets = arm_get_frame_offsets ();
21933 saved_regs = offsets->saved_regs_mask;
c9ca9b88
PB
21934
21935 if ((saved_regs & (1 << LR_REGNUM)) == 0)
21936 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
21937 else
21938 {
21939 if (frame_pointer_needed)
21940 addr = plus_constant(hard_frame_pointer_rtx, -4);
21941 else
21942 {
21943 /* LR will be the first saved register. */
c9ca9b88
PB
21944 delta = offsets->outgoing_args - (offsets->frame + 4);
21945
f676971a 21946
c9ca9b88
PB
21947 if (delta >= 4096)
21948 {
21949 emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
21950 GEN_INT (delta & ~4095)));
21951 addr = scratch;
21952 delta &= 4095;
21953 }
21954 else
21955 addr = stack_pointer_rtx;
21956
21957 addr = plus_constant (addr, delta);
21958 }
31fa16b6 21959 emit_move_insn (gen_frame_mem (Pmode, addr), source);
c9ca9b88
PB
21960 }
21961}
21962
21963
21964void
21965thumb_set_return_address (rtx source, rtx scratch)
21966{
21967 arm_stack_offsets *offsets;
c9ca9b88 21968 HOST_WIDE_INT delta;
5b3e6663 21969 HOST_WIDE_INT limit;
c9ca9b88
PB
21970 int reg;
21971 rtx addr;
57934c39 21972 unsigned long mask;
c9ca9b88 21973
c41c1387 21974 emit_use (source);
c9ca9b88 21975
954954d1
PB
21976 offsets = arm_get_frame_offsets ();
21977 mask = offsets->saved_regs_mask;
57934c39 21978 if (mask & (1 << LR_REGNUM))
c9ca9b88 21979 {
5b3e6663 21980 limit = 1024;
c9ca9b88
PB
21981 /* Find the saved regs. */
21982 if (frame_pointer_needed)
21983 {
21984 delta = offsets->soft_frame - offsets->saved_args;
21985 reg = THUMB_HARD_FRAME_POINTER_REGNUM;
5b3e6663
PB
21986 if (TARGET_THUMB1)
21987 limit = 128;
c9ca9b88
PB
21988 }
21989 else
21990 {
21991 delta = offsets->outgoing_args - offsets->saved_args;
21992 reg = SP_REGNUM;
21993 }
21994 /* Allow for the stack frame. */
5b3e6663 21995 if (TARGET_THUMB1 && TARGET_BACKTRACE)
c9ca9b88
PB
21996 delta -= 16;
21997 /* The link register is always the first saved register. */
21998 delta -= 4;
f676971a 21999
c9ca9b88
PB
22000 /* Construct the address. */
22001 addr = gen_rtx_REG (SImode, reg);
5b3e6663 22002 if (delta > limit)
c9ca9b88
PB
22003 {
22004 emit_insn (gen_movsi (scratch, GEN_INT (delta)));
22005 emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
22006 addr = scratch;
22007 }
22008 else
22009 addr = plus_constant (addr, delta);
22010
31fa16b6 22011 emit_move_insn (gen_frame_mem (Pmode, addr), source);
c9ca9b88
PB
22012 }
22013 else
22014 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
22015}
22016
f676971a
EC
22017/* Implements target hook vector_mode_supported_p. */
22018bool
22019arm_vector_mode_supported_p (enum machine_mode mode)
22020{
88f77cba
JB
22021 /* Neon also supports V2SImode, etc. listed in the clause below. */
22022 if (TARGET_NEON && (mode == V2SFmode || mode == V4SImode || mode == V8HImode
22023 || mode == V16QImode || mode == V4SFmode || mode == V2DImode))
22024 return true;
22025
390b17c2
RE
22026 if ((TARGET_NEON || TARGET_IWMMXT)
22027 && ((mode == V2SImode)
22028 || (mode == V4HImode)
22029 || (mode == V8QImode)))
f676971a
EC
22030 return true;
22031
22032 return false;
22033}
273a2526 22034
26983c22
L
22035/* Use the option -mvectorize-with-neon-quad to override the use of doubleword
22036 registers when autovectorizing for Neon, at least until multiple vector
22037 widths are supported properly by the middle-end. */
22038
cc4b5170
RG
22039static enum machine_mode
22040arm_preferred_simd_mode (enum machine_mode mode)
26983c22 22041{
cc4b5170
RG
22042 if (TARGET_NEON)
22043 switch (mode)
22044 {
22045 case SFmode:
22046 return TARGET_NEON_VECTORIZE_QUAD ? V4SFmode : V2SFmode;
22047 case SImode:
22048 return TARGET_NEON_VECTORIZE_QUAD ? V4SImode : V2SImode;
22049 case HImode:
22050 return TARGET_NEON_VECTORIZE_QUAD ? V8HImode : V4HImode;
22051 case QImode:
22052 return TARGET_NEON_VECTORIZE_QUAD ? V16QImode : V8QImode;
22053 case DImode:
22054 if (TARGET_NEON_VECTORIZE_QUAD)
22055 return V2DImode;
22056 break;
22057
22058 default:;
22059 }
22060
22061 if (TARGET_REALLY_IWMMXT)
22062 switch (mode)
22063 {
22064 case SImode:
22065 return V2SImode;
22066 case HImode:
22067 return V4HImode;
22068 case QImode:
22069 return V8QImode;
22070
22071 default:;
22072 }
22073
22074 return word_mode;
26983c22
L
22075}
22076
d163e655
AS
22077/* Implement TARGET_CLASS_LIKELY_SPILLED_P.
22078
22079 We need to define this for LO_REGS on thumb. Otherwise we can end up
22080 using r0-r4 for function arguments, r7 for the stack frame and don't
22081 have enough left over to do doubleword arithmetic. */
22082
22083static bool
22084arm_class_likely_spilled_p (reg_class_t rclass)
22085{
22086 if ((TARGET_THUMB && rclass == LO_REGS)
22087 || rclass == CC_REG)
22088 return true;
22089
22090 return false;
22091}
22092
42db504c
SB
22093/* Implements target hook small_register_classes_for_mode_p. */
22094bool
22095arm_small_register_classes_for_mode_p (enum machine_mode mode ATTRIBUTE_UNUSED)
22096{
22097 return TARGET_THUMB1;
22098}
22099
273a2526
RS
22100/* Implement TARGET_SHIFT_TRUNCATION_MASK. SImode shifts use normal
22101 ARM insns and therefore guarantee that the shift count is modulo 256.
22102 DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
22103 guarantee no particular behavior for out-of-range counts. */
22104
22105static unsigned HOST_WIDE_INT
22106arm_shift_truncation_mask (enum machine_mode mode)
22107{
22108 return mode == SImode ? 255 : 0;
22109}
2fa330b2
PB
22110
22111
22112/* Map internal gcc register numbers to DWARF2 register numbers. */
22113
22114unsigned int
22115arm_dbx_register_number (unsigned int regno)
22116{
22117 if (regno < 16)
22118 return regno;
22119
22120 /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
22121 compatibility. The EABI defines them as registers 96-103. */
22122 if (IS_FPA_REGNUM (regno))
22123 return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
22124
22125 if (IS_VFP_REGNUM (regno))
854b8a40
JB
22126 {
22127 /* See comment in arm_dwarf_register_span. */
22128 if (VFP_REGNO_OK_FOR_SINGLE (regno))
22129 return 64 + regno - FIRST_VFP_REGNUM;
22130 else
22131 return 256 + (regno - FIRST_VFP_REGNUM) / 2;
22132 }
2fa330b2
PB
22133
22134 if (IS_IWMMXT_GR_REGNUM (regno))
22135 return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
22136
22137 if (IS_IWMMXT_REGNUM (regno))
22138 return 112 + regno - FIRST_IWMMXT_REGNUM;
22139
e6d29d15 22140 gcc_unreachable ();
2fa330b2
PB
22141}
22142
854b8a40
JB
22143/* Dwarf models VFPv3 registers as 32 64-bit registers.
22144 GCC models tham as 64 32-bit registers, so we need to describe this to
22145 the DWARF generation code. Other registers can use the default. */
22146static rtx
22147arm_dwarf_register_span (rtx rtl)
22148{
22149 unsigned regno;
22150 int nregs;
22151 int i;
22152 rtx p;
22153
22154 regno = REGNO (rtl);
22155 if (!IS_VFP_REGNUM (regno))
22156 return NULL_RTX;
22157
22158 /* XXX FIXME: The EABI defines two VFP register ranges:
22159 64-95: Legacy VFPv2 numbering for S0-S31 (obsolescent)
22160 256-287: D0-D31
22161 The recommended encoding for S0-S31 is a DW_OP_bit_piece of the
22162 corresponding D register. Until GDB supports this, we shall use the
22163 legacy encodings. We also use these encodings for D0-D15 for
22164 compatibility with older debuggers. */
22165 if (VFP_REGNO_OK_FOR_SINGLE (regno))
22166 return NULL_RTX;
22167
22168 nregs = GET_MODE_SIZE (GET_MODE (rtl)) / 8;
22169 p = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nregs));
22170 regno = (regno - FIRST_VFP_REGNUM) / 2;
22171 for (i = 0; i < nregs; i++)
22172 XVECEXP (p, 0, i) = gen_rtx_REG (DImode, 256 + regno + i);
22173
22174 return p;
22175}
617a1b71 22176
f0a0390e 22177#if ARM_UNWIND_INFO
5b3e6663
PB
22178/* Emit unwind directives for a store-multiple instruction or stack pointer
22179 push during alignment.
22180 These should only ever be generated by the function prologue code, so
22181 expect them to have a particular form. */
617a1b71
PB
22182
22183static void
5b3e6663 22184arm_unwind_emit_sequence (FILE * asm_out_file, rtx p)
617a1b71
PB
22185{
22186 int i;
22187 HOST_WIDE_INT offset;
22188 HOST_WIDE_INT nregs;
22189 int reg_size;
22190 unsigned reg;
22191 unsigned lastreg;
22192 rtx e;
22193
617a1b71 22194 e = XVECEXP (p, 0, 0);
5b3e6663
PB
22195 if (GET_CODE (e) != SET)
22196 abort ();
22197
22198 /* First insn will adjust the stack pointer. */
617a1b71
PB
22199 if (GET_CODE (e) != SET
22200 || GET_CODE (XEXP (e, 0)) != REG
22201 || REGNO (XEXP (e, 0)) != SP_REGNUM
22202 || GET_CODE (XEXP (e, 1)) != PLUS)
22203 abort ();
22204
22205 offset = -INTVAL (XEXP (XEXP (e, 1), 1));
22206 nregs = XVECLEN (p, 0) - 1;
22207
22208 reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
22209 if (reg < 16)
22210 {
22211 /* The function prologue may also push pc, but not annotate it as it is
569b7f6a 22212 never restored. We turn this into a stack pointer adjustment. */
617a1b71
PB
22213 if (nregs * 4 == offset - 4)
22214 {
22215 fprintf (asm_out_file, "\t.pad #4\n");
22216 offset -= 4;
22217 }
22218 reg_size = 4;
8edfc4cc 22219 fprintf (asm_out_file, "\t.save {");
617a1b71
PB
22220 }
22221 else if (IS_VFP_REGNUM (reg))
22222 {
617a1b71 22223 reg_size = 8;
8edfc4cc 22224 fprintf (asm_out_file, "\t.vsave {");
617a1b71
PB
22225 }
22226 else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
22227 {
22228 /* FPA registers are done differently. */
ea40ba9c 22229 asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
617a1b71
PB
22230 return;
22231 }
22232 else
22233 /* Unknown register type. */
22234 abort ();
22235
22236 /* If the stack increment doesn't match the size of the saved registers,
22237 something has gone horribly wrong. */
22238 if (offset != nregs * reg_size)
22239 abort ();
22240
617a1b71
PB
22241 offset = 0;
22242 lastreg = 0;
22243 /* The remaining insns will describe the stores. */
22244 for (i = 1; i <= nregs; i++)
22245 {
22246 /* Expect (set (mem <addr>) (reg)).
22247 Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)). */
22248 e = XVECEXP (p, 0, i);
22249 if (GET_CODE (e) != SET
22250 || GET_CODE (XEXP (e, 0)) != MEM
22251 || GET_CODE (XEXP (e, 1)) != REG)
22252 abort ();
e0b92319 22253
617a1b71
PB
22254 reg = REGNO (XEXP (e, 1));
22255 if (reg < lastreg)
22256 abort ();
e0b92319 22257
617a1b71
PB
22258 if (i != 1)
22259 fprintf (asm_out_file, ", ");
22260 /* We can't use %r for vfp because we need to use the
22261 double precision register names. */
22262 if (IS_VFP_REGNUM (reg))
22263 asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
22264 else
22265 asm_fprintf (asm_out_file, "%r", reg);
22266
22267#ifdef ENABLE_CHECKING
22268 /* Check that the addresses are consecutive. */
22269 e = XEXP (XEXP (e, 0), 0);
22270 if (GET_CODE (e) == PLUS)
22271 {
22272 offset += reg_size;
22273 if (GET_CODE (XEXP (e, 0)) != REG
22274 || REGNO (XEXP (e, 0)) != SP_REGNUM
22275 || GET_CODE (XEXP (e, 1)) != CONST_INT
22276 || offset != INTVAL (XEXP (e, 1)))
22277 abort ();
22278 }
22279 else if (i != 1
22280 || GET_CODE (e) != REG
22281 || REGNO (e) != SP_REGNUM)
22282 abort ();
22283#endif
22284 }
22285 fprintf (asm_out_file, "}\n");
22286}
22287
22288/* Emit unwind directives for a SET. */
22289
22290static void
22291arm_unwind_emit_set (FILE * asm_out_file, rtx p)
22292{
22293 rtx e0;
22294 rtx e1;
5b3e6663 22295 unsigned reg;
617a1b71
PB
22296
22297 e0 = XEXP (p, 0);
22298 e1 = XEXP (p, 1);
22299 switch (GET_CODE (e0))
22300 {
22301 case MEM:
22302 /* Pushing a single register. */
22303 if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
22304 || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
22305 || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
22306 abort ();
22307
22308 asm_fprintf (asm_out_file, "\t.save ");
22309 if (IS_VFP_REGNUM (REGNO (e1)))
22310 asm_fprintf(asm_out_file, "{d%d}\n",
22311 (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
22312 else
22313 asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
22314 break;
22315
22316 case REG:
22317 if (REGNO (e0) == SP_REGNUM)
22318 {
22319 /* A stack increment. */
22320 if (GET_CODE (e1) != PLUS
22321 || GET_CODE (XEXP (e1, 0)) != REG
22322 || REGNO (XEXP (e1, 0)) != SP_REGNUM
22323 || GET_CODE (XEXP (e1, 1)) != CONST_INT)
22324 abort ();
22325
ea40ba9c 22326 asm_fprintf (asm_out_file, "\t.pad #%wd\n",
617a1b71
PB
22327 -INTVAL (XEXP (e1, 1)));
22328 }
22329 else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
22330 {
22331 HOST_WIDE_INT offset;
e0b92319 22332
617a1b71
PB
22333 if (GET_CODE (e1) == PLUS)
22334 {
22335 if (GET_CODE (XEXP (e1, 0)) != REG
22336 || GET_CODE (XEXP (e1, 1)) != CONST_INT)
22337 abort ();
22338 reg = REGNO (XEXP (e1, 0));
22339 offset = INTVAL (XEXP (e1, 1));
ea40ba9c 22340 asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
617a1b71 22341 HARD_FRAME_POINTER_REGNUM, reg,
80d56d04 22342 offset);
617a1b71
PB
22343 }
22344 else if (GET_CODE (e1) == REG)
22345 {
22346 reg = REGNO (e1);
22347 asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
22348 HARD_FRAME_POINTER_REGNUM, reg);
22349 }
22350 else
22351 abort ();
22352 }
22353 else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
22354 {
22355 /* Move from sp to reg. */
22356 asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
22357 }
758ed9b2
PB
22358 else if (GET_CODE (e1) == PLUS
22359 && GET_CODE (XEXP (e1, 0)) == REG
22360 && REGNO (XEXP (e1, 0)) == SP_REGNUM
22361 && GET_CODE (XEXP (e1, 1)) == CONST_INT)
22362 {
22363 /* Set reg to offset from sp. */
22364 asm_fprintf (asm_out_file, "\t.movsp %r, #%d\n",
22365 REGNO (e0), (int)INTVAL(XEXP (e1, 1)));
22366 }
5b3e6663
PB
22367 else if (GET_CODE (e1) == UNSPEC && XINT (e1, 1) == UNSPEC_STACK_ALIGN)
22368 {
22369 /* Stack pointer save before alignment. */
22370 reg = REGNO (e0);
22371 asm_fprintf (asm_out_file, "\t.unwind_raw 0, 0x%x @ vsp = r%d\n",
22372 reg + 0x90, reg);
22373 }
617a1b71
PB
22374 else
22375 abort ();
22376 break;
22377
22378 default:
22379 abort ();
22380 }
22381}
22382
22383
22384/* Emit unwind directives for the given insn. */
22385
22386static void
22387arm_unwind_emit (FILE * asm_out_file, rtx insn)
22388{
22389 rtx pat;
22390
f0a0390e 22391 if (arm_except_unwind_info () != UI_TARGET)
617a1b71
PB
22392 return;
22393
e3b5732b 22394 if (!(flag_unwind_tables || crtl->uses_eh_lsda)
80efdb6a 22395 && (TREE_NOTHROW (current_function_decl)
ad516a74 22396 || crtl->all_throwers_are_sibcalls))
80efdb6a
PB
22397 return;
22398
617a1b71
PB
22399 if (GET_CODE (insn) == NOTE || !RTX_FRAME_RELATED_P (insn))
22400 return;
22401
22402 pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
22403 if (pat)
22404 pat = XEXP (pat, 0);
22405 else
22406 pat = PATTERN (insn);
22407
22408 switch (GET_CODE (pat))
22409 {
22410 case SET:
22411 arm_unwind_emit_set (asm_out_file, pat);
22412 break;
22413
22414 case SEQUENCE:
22415 /* Store multiple. */
5b3e6663 22416 arm_unwind_emit_sequence (asm_out_file, pat);
617a1b71
PB
22417 break;
22418
22419 default:
22420 abort();
22421 }
22422}
22423
22424
22425/* Output a reference from a function exception table to the type_info
22426 object X. The EABI specifies that the symbol should be relocated by
22427 an R_ARM_TARGET2 relocation. */
22428
22429static bool
22430arm_output_ttype (rtx x)
22431{
22432 fputs ("\t.word\t", asm_out_file);
22433 output_addr_const (asm_out_file, x);
22434 /* Use special relocations for symbol references. */
22435 if (GET_CODE (x) != CONST_INT)
22436 fputs ("(TARGET2)", asm_out_file);
22437 fputc ('\n', asm_out_file);
22438
22439 return TRUE;
22440}
a68b5e52
RH
22441
22442/* Implement TARGET_ASM_EMIT_EXCEPT_PERSONALITY. */
22443
22444static void
22445arm_asm_emit_except_personality (rtx personality)
22446{
22447 fputs ("\t.personality\t", asm_out_file);
22448 output_addr_const (asm_out_file, personality);
22449 fputc ('\n', asm_out_file);
22450}
22451
22452/* Implement TARGET_ASM_INITIALIZE_SECTIONS. */
22453
22454static void
22455arm_asm_init_sections (void)
22456{
22457 exception_section = get_unnamed_section (0, output_section_asm_op,
22458 "\t.handlerdata");
22459}
f0a0390e
RH
22460#endif /* ARM_UNWIND_INFO */
22461
22462/* Implement TARGET_EXCEPT_UNWIND_INFO. */
22463
22464static enum unwind_info_type
22465arm_except_unwind_info (void)
22466{
22467 /* Honor the --enable-sjlj-exceptions configure switch. */
22468#ifdef CONFIG_SJLJ_EXCEPTIONS
22469 if (CONFIG_SJLJ_EXCEPTIONS)
22470 return UI_SJLJ;
22471#endif
22472
22473 /* If not using ARM EABI unwind tables... */
22474 if (ARM_UNWIND_INFO)
22475 {
22476 /* For simplicity elsewhere in this file, indicate that all unwind
22477 info is disabled if we're not emitting unwind tables. */
22478 if (!flag_exceptions && !flag_unwind_tables)
22479 return UI_NONE;
22480 else
22481 return UI_TARGET;
22482 }
22483
22484 /* ... we use sjlj exceptions for backwards compatibility. */
22485 return UI_SJLJ;
22486}
617a1b71
PB
22487
22488
5b3e6663
PB
22489/* Handle UNSPEC DWARF call frame instructions. These are needed for dynamic
22490 stack alignment. */
22491
22492static void
22493arm_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
22494{
22495 rtx unspec = SET_SRC (pattern);
22496 gcc_assert (GET_CODE (unspec) == UNSPEC);
22497
22498 switch (index)
22499 {
22500 case UNSPEC_STACK_ALIGN:
22501 /* ??? We should set the CFA = (SP & ~7). At this point we haven't
22502 put anything on the stack, so hopefully it won't matter.
22503 CFA = SP will be correct after alignment. */
22504 dwarf2out_reg_save_reg (label, stack_pointer_rtx,
22505 SET_DEST (pattern));
22506 break;
22507 default:
22508 gcc_unreachable ();
22509 }
22510}
22511
22512
617a1b71
PB
22513/* Output unwind directives for the start/end of a function. */
22514
22515void
22516arm_output_fn_unwind (FILE * f, bool prologue)
22517{
f0a0390e 22518 if (arm_except_unwind_info () != UI_TARGET)
617a1b71
PB
22519 return;
22520
22521 if (prologue)
22522 fputs ("\t.fnstart\n", f);
22523 else
80efdb6a
PB
22524 {
22525 /* If this function will never be unwound, then mark it as such.
22526 The came condition is used in arm_unwind_emit to suppress
22527 the frame annotations. */
e3b5732b 22528 if (!(flag_unwind_tables || crtl->uses_eh_lsda)
80efdb6a 22529 && (TREE_NOTHROW (current_function_decl)
ad516a74 22530 || crtl->all_throwers_are_sibcalls))
80efdb6a
PB
22531 fputs("\t.cantunwind\n", f);
22532
22533 fputs ("\t.fnend\n", f);
22534 }
617a1b71 22535}
d3585b76
DJ
22536
22537static bool
22538arm_emit_tls_decoration (FILE *fp, rtx x)
22539{
22540 enum tls_reloc reloc;
22541 rtx val;
22542
22543 val = XVECEXP (x, 0, 0);
32e8bb8e 22544 reloc = (enum tls_reloc) INTVAL (XVECEXP (x, 0, 1));
d3585b76
DJ
22545
22546 output_addr_const (fp, val);
22547
22548 switch (reloc)
22549 {
22550 case TLS_GD32:
22551 fputs ("(tlsgd)", fp);
22552 break;
22553 case TLS_LDM32:
22554 fputs ("(tlsldm)", fp);
22555 break;
22556 case TLS_LDO32:
22557 fputs ("(tlsldo)", fp);
22558 break;
22559 case TLS_IE32:
22560 fputs ("(gottpoff)", fp);
22561 break;
22562 case TLS_LE32:
22563 fputs ("(tpoff)", fp);
22564 break;
22565 default:
22566 gcc_unreachable ();
22567 }
22568
22569 switch (reloc)
22570 {
22571 case TLS_GD32:
22572 case TLS_LDM32:
22573 case TLS_IE32:
22574 fputs (" + (. - ", fp);
22575 output_addr_const (fp, XVECEXP (x, 0, 2));
22576 fputs (" - ", fp);
22577 output_addr_const (fp, XVECEXP (x, 0, 3));
22578 fputc (')', fp);
22579 break;
22580 default:
22581 break;
22582 }
22583
22584 return TRUE;
22585}
22586
afcc986d
JM
22587/* ARM implementation of TARGET_ASM_OUTPUT_DWARF_DTPREL. */
22588
22589static void
22590arm_output_dwarf_dtprel (FILE *file, int size, rtx x)
22591{
22592 gcc_assert (size == 4);
22593 fputs ("\t.word\t", file);
22594 output_addr_const (file, x);
22595 fputs ("(tlsldo)", file);
22596}
22597
ffda8a0d
AS
22598/* Implement TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA. */
22599
22600static bool
d3585b76
DJ
22601arm_output_addr_const_extra (FILE *fp, rtx x)
22602{
22603 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
22604 return arm_emit_tls_decoration (fp, x);
f16fe45f
DJ
22605 else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
22606 {
22607 char label[256];
22608 int labelno = INTVAL (XVECEXP (x, 0, 0));
22609
22610 ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
22611 assemble_name_raw (fp, label);
22612
f9bd1a89
RS
22613 return TRUE;
22614 }
22615 else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_GOTSYM_OFF)
22616 {
22617 assemble_name (fp, "_GLOBAL_OFFSET_TABLE_");
22618 if (GOT_PCREL)
22619 fputs ("+.", fp);
22620 fputs ("-(", fp);
22621 output_addr_const (fp, XVECEXP (x, 0, 0));
22622 fputc (')', fp);
f16fe45f
DJ
22623 return TRUE;
22624 }
85c9bcd4
WG
22625 else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_SYMBOL_OFFSET)
22626 {
22627 output_addr_const (fp, XVECEXP (x, 0, 0));
22628 if (GOT_PCREL)
22629 fputs ("+.", fp);
22630 fputs ("-(", fp);
22631 output_addr_const (fp, XVECEXP (x, 0, 1));
22632 fputc (')', fp);
22633 return TRUE;
22634 }
d3585b76
DJ
22635 else if (GET_CODE (x) == CONST_VECTOR)
22636 return arm_emit_vector_const (fp, x);
22637
22638 return FALSE;
22639}
22640
5b3e6663
PB
22641/* Output assembly for a shift instruction.
22642 SET_FLAGS determines how the instruction modifies the condition codes.
7a085dce 22643 0 - Do not set condition codes.
5b3e6663
PB
22644 1 - Set condition codes.
22645 2 - Use smallest instruction. */
22646const char *
22647arm_output_shift(rtx * operands, int set_flags)
22648{
22649 char pattern[100];
22650 static const char flag_chars[3] = {'?', '.', '!'};
22651 const char *shift;
22652 HOST_WIDE_INT val;
22653 char c;
22654
22655 c = flag_chars[set_flags];
22656 if (TARGET_UNIFIED_ASM)
22657 {
22658 shift = shift_op(operands[3], &val);
22659 if (shift)
22660 {
22661 if (val != -1)
22662 operands[2] = GEN_INT(val);
22663 sprintf (pattern, "%s%%%c\t%%0, %%1, %%2", shift, c);
22664 }
22665 else
22666 sprintf (pattern, "mov%%%c\t%%0, %%1", c);
22667 }
22668 else
22669 sprintf (pattern, "mov%%%c\t%%0, %%1%%S3", c);
22670 output_asm_insn (pattern, operands);
22671 return "";
22672}
22673
907dd0c7
RE
22674/* Output a Thumb-1 casesi dispatch sequence. */
22675const char *
22676thumb1_output_casesi (rtx *operands)
22677{
22678 rtx diff_vec = PATTERN (next_real_insn (operands[0]));
907dd0c7
RE
22679
22680 gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
22681
907dd0c7
RE
22682 switch (GET_MODE(diff_vec))
22683 {
22684 case QImode:
22685 return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned ?
22686 "bl\t%___gnu_thumb1_case_uqi" : "bl\t%___gnu_thumb1_case_sqi");
22687 case HImode:
22688 return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned ?
22689 "bl\t%___gnu_thumb1_case_uhi" : "bl\t%___gnu_thumb1_case_shi");
22690 case SImode:
22691 return "bl\t%___gnu_thumb1_case_si";
22692 default:
22693 gcc_unreachable ();
22694 }
22695}
22696
5b3e6663
PB
22697/* Output a Thumb-2 casesi instruction. */
22698const char *
22699thumb2_output_casesi (rtx *operands)
22700{
22701 rtx diff_vec = PATTERN (next_real_insn (operands[2]));
22702
22703 gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
22704
22705 output_asm_insn ("cmp\t%0, %1", operands);
22706 output_asm_insn ("bhi\t%l3", operands);
22707 switch (GET_MODE(diff_vec))
22708 {
22709 case QImode:
22710 return "tbb\t[%|pc, %0]";
22711 case HImode:
22712 return "tbh\t[%|pc, %0, lsl #1]";
22713 case SImode:
22714 if (flag_pic)
22715 {
22716 output_asm_insn ("adr\t%4, %l2", operands);
22717 output_asm_insn ("ldr\t%5, [%4, %0, lsl #2]", operands);
22718 output_asm_insn ("add\t%4, %4, %5", operands);
22719 return "bx\t%4";
22720 }
22721 else
22722 {
22723 output_asm_insn ("adr\t%4, %l2", operands);
22724 return "ldr\t%|pc, [%4, %0, lsl #2]";
22725 }
22726 default:
22727 gcc_unreachable ();
22728 }
22729}
22730
bd4dc3cd
PB
22731/* Most ARM cores are single issue, but some newer ones can dual issue.
22732 The scheduler descriptions rely on this being correct. */
22733static int
22734arm_issue_rate (void)
22735{
22736 switch (arm_tune)
22737 {
22738 case cortexr4:
51c69ddb 22739 case cortexr4f:
d8099dd8 22740 case cortexa5:
bd4dc3cd 22741 case cortexa8:
7612f14d 22742 case cortexa9:
bd4dc3cd
PB
22743 return 2;
22744
22745 default:
22746 return 1;
22747 }
22748}
22749
608063c3
JB
22750/* A table and a function to perform ARM-specific name mangling for
22751 NEON vector types in order to conform to the AAPCS (see "Procedure
22752 Call Standard for the ARM Architecture", Appendix A). To qualify
22753 for emission with the mangled names defined in that document, a
22754 vector type must not only be of the correct mode but also be
22755 composed of NEON vector element types (e.g. __builtin_neon_qi). */
22756typedef struct
22757{
22758 enum machine_mode mode;
22759 const char *element_type_name;
22760 const char *aapcs_name;
22761} arm_mangle_map_entry;
22762
22763static arm_mangle_map_entry arm_mangle_map[] = {
22764 /* 64-bit containerized types. */
22765 { V8QImode, "__builtin_neon_qi", "15__simd64_int8_t" },
22766 { V8QImode, "__builtin_neon_uqi", "16__simd64_uint8_t" },
22767 { V4HImode, "__builtin_neon_hi", "16__simd64_int16_t" },
22768 { V4HImode, "__builtin_neon_uhi", "17__simd64_uint16_t" },
22769 { V2SImode, "__builtin_neon_si", "16__simd64_int32_t" },
22770 { V2SImode, "__builtin_neon_usi", "17__simd64_uint32_t" },
22771 { V2SFmode, "__builtin_neon_sf", "18__simd64_float32_t" },
22772 { V8QImode, "__builtin_neon_poly8", "16__simd64_poly8_t" },
22773 { V4HImode, "__builtin_neon_poly16", "17__simd64_poly16_t" },
22774 /* 128-bit containerized types. */
22775 { V16QImode, "__builtin_neon_qi", "16__simd128_int8_t" },
22776 { V16QImode, "__builtin_neon_uqi", "17__simd128_uint8_t" },
22777 { V8HImode, "__builtin_neon_hi", "17__simd128_int16_t" },
22778 { V8HImode, "__builtin_neon_uhi", "18__simd128_uint16_t" },
22779 { V4SImode, "__builtin_neon_si", "17__simd128_int32_t" },
22780 { V4SImode, "__builtin_neon_usi", "18__simd128_uint32_t" },
22781 { V4SFmode, "__builtin_neon_sf", "19__simd128_float32_t" },
22782 { V16QImode, "__builtin_neon_poly8", "17__simd128_poly8_t" },
22783 { V8HImode, "__builtin_neon_poly16", "18__simd128_poly16_t" },
22784 { VOIDmode, NULL, NULL }
22785};
22786
22787const char *
3101faab 22788arm_mangle_type (const_tree type)
608063c3
JB
22789{
22790 arm_mangle_map_entry *pos = arm_mangle_map;
22791
07d8efe3
MM
22792 /* The ARM ABI documents (10th October 2008) say that "__va_list"
22793 has to be managled as if it is in the "std" namespace. */
22794 if (TARGET_AAPCS_BASED
ae46a823 22795 && lang_hooks.types_compatible_p (CONST_CAST_TREE (type), va_list_type))
07d8efe3
MM
22796 {
22797 static bool warned;
d147cbd9 22798 if (!warned && warn_psabi && !in_system_header)
07d8efe3
MM
22799 {
22800 warned = true;
22801 inform (input_location,
22802 "the mangling of %<va_list%> has changed in GCC 4.4");
22803 }
22804 return "St9__va_list";
22805 }
22806
0fd8c3ad
SL
22807 /* Half-precision float. */
22808 if (TREE_CODE (type) == REAL_TYPE && TYPE_PRECISION (type) == 16)
22809 return "Dh";
22810
608063c3
JB
22811 if (TREE_CODE (type) != VECTOR_TYPE)
22812 return NULL;
22813
22814 /* Check the mode of the vector type, and the name of the vector
22815 element type, against the table. */
22816 while (pos->mode != VOIDmode)
22817 {
22818 tree elt_type = TREE_TYPE (type);
22819
22820 if (pos->mode == TYPE_MODE (type)
22821 && TREE_CODE (TYPE_NAME (elt_type)) == TYPE_DECL
22822 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (elt_type))),
22823 pos->element_type_name))
22824 return pos->aapcs_name;
22825
22826 pos++;
22827 }
22828
22829 /* Use the default mangling for unrecognized (possibly user-defined)
22830 vector types. */
22831 return NULL;
22832}
22833
795dc4fc
PB
22834/* Order of allocation of core registers for Thumb: this allocation is
22835 written over the corresponding initial entries of the array
22836 initialized with REG_ALLOC_ORDER. We allocate all low registers
22837 first. Saving and restoring a low register is usually cheaper than
22838 using a call-clobbered high register. */
22839
22840static const int thumb_core_reg_alloc_order[] =
22841{
22842 3, 2, 1, 0, 4, 5, 6, 7,
22843 14, 12, 8, 9, 10, 11, 13, 15
22844};
22845
22846/* Adjust register allocation order when compiling for Thumb. */
22847
22848void
22849arm_order_regs_for_local_alloc (void)
22850{
22851 const int arm_reg_alloc_order[] = REG_ALLOC_ORDER;
22852 memcpy(reg_alloc_order, arm_reg_alloc_order, sizeof (reg_alloc_order));
22853 if (TARGET_THUMB)
22854 memcpy (reg_alloc_order, thumb_core_reg_alloc_order,
22855 sizeof (thumb_core_reg_alloc_order));
22856}
22857
b52b1749
AS
22858/* Implement TARGET_FRAME_POINTER_REQUIRED. */
22859
22860bool
22861arm_frame_pointer_required (void)
22862{
22863 return (cfun->has_nonlocal_label
22864 || SUBTARGET_FRAME_POINTER_REQUIRED
22865 || (TARGET_ARM && TARGET_APCS_FRAME && ! leaf_function_p ()));
22866}
22867
2929029c
WG
22868/* Only thumb1 can't support conditional execution, so return true if
22869 the target is not thumb1. */
22870static bool
22871arm_have_conditional_execution (void)
22872{
22873 return !TARGET_THUMB1;
22874}
22875
029e79eb
MS
22876/* Legitimize a memory reference for sync primitive implemented using
22877 ldrex / strex. We currently force the form of the reference to be
22878 indirect without offset. We do not yet support the indirect offset
22879 addressing supported by some ARM targets for these
22880 instructions. */
22881static rtx
22882arm_legitimize_sync_memory (rtx memory)
22883{
22884 rtx addr = force_reg (Pmode, XEXP (memory, 0));
22885 rtx legitimate_memory = gen_rtx_MEM (GET_MODE (memory), addr);
22886
22887 set_mem_alias_set (legitimate_memory, ALIAS_SET_MEMORY_BARRIER);
22888 MEM_VOLATILE_P (legitimate_memory) = MEM_VOLATILE_P (memory);
22889 return legitimate_memory;
22890}
22891
22892/* An instruction emitter. */
22893typedef void (* emit_f) (int label, const char *, rtx *);
22894
22895/* An instruction emitter that emits via the conventional
22896 output_asm_insn. */
22897static void
22898arm_emit (int label ATTRIBUTE_UNUSED, const char *pattern, rtx *operands)
22899{
22900 output_asm_insn (pattern, operands);
22901}
22902
22903/* Count the number of emitted synchronization instructions. */
22904static unsigned arm_insn_count;
22905
22906/* An emitter that counts emitted instructions but does not actually
22907 emit instruction into the the instruction stream. */
22908static void
22909arm_count (int label,
22910 const char *pattern ATTRIBUTE_UNUSED,
22911 rtx *operands ATTRIBUTE_UNUSED)
22912{
22913 if (! label)
22914 ++ arm_insn_count;
22915}
22916
22917/* Construct a pattern using conventional output formatting and feed
22918 it to output_asm_insn. Provides a mechanism to construct the
22919 output pattern on the fly. Note the hard limit on the pattern
22920 buffer size. */
21272a45 22921static void ATTRIBUTE_PRINTF_4
029e79eb
MS
22922arm_output_asm_insn (emit_f emit, int label, rtx *operands,
22923 const char *pattern, ...)
22924{
22925 va_list ap;
22926 char buffer[256];
22927
22928 va_start (ap, pattern);
22929 vsprintf (buffer, pattern, ap);
22930 va_end (ap);
22931 emit (label, buffer, operands);
22932}
22933
22934/* Emit the memory barrier instruction, if any, provided by this
22935 target to a specified emitter. */
22936static void
22937arm_process_output_memory_barrier (emit_f emit, rtx *operands)
22938{
22939 if (TARGET_HAVE_DMB)
22940 {
22941 /* Note we issue a system level barrier. We should consider
22942 issuing a inner shareabilty zone barrier here instead, ie.
22943 "DMB ISH". */
22944 emit (0, "dmb\tsy", operands);
22945 return;
22946 }
22947
22948 if (TARGET_HAVE_DMB_MCR)
22949 {
22950 emit (0, "mcr\tp15, 0, r0, c7, c10, 5", operands);
22951 return;
22952 }
22953
22954 gcc_unreachable ();
22955}
22956
22957/* Emit the memory barrier instruction, if any, provided by this
22958 target. */
22959const char *
22960arm_output_memory_barrier (rtx *operands)
22961{
22962 arm_process_output_memory_barrier (arm_emit, operands);
22963 return "";
22964}
22965
22966/* Helper to figure out the instruction suffix required on ldrex/strex
22967 for operations on an object of the specified mode. */
22968static const char *
22969arm_ldrex_suffix (enum machine_mode mode)
22970{
22971 switch (mode)
22972 {
22973 case QImode: return "b";
22974 case HImode: return "h";
22975 case SImode: return "";
22976 case DImode: return "d";
22977 default:
22978 gcc_unreachable ();
22979 }
22980 return "";
22981}
22982
22983/* Emit an ldrex{b,h,d, } instruction appropriate for the specified
22984 mode. */
22985static void
22986arm_output_ldrex (emit_f emit,
22987 enum machine_mode mode,
22988 rtx target,
22989 rtx memory)
22990{
22991 const char *suffix = arm_ldrex_suffix (mode);
22992 rtx operands[2];
22993
22994 operands[0] = target;
22995 operands[1] = memory;
22996 arm_output_asm_insn (emit, 0, operands, "ldrex%s\t%%0, %%C1", suffix);
22997}
22998
22999/* Emit a strex{b,h,d, } instruction appropriate for the specified
23000 mode. */
23001static void
23002arm_output_strex (emit_f emit,
23003 enum machine_mode mode,
23004 const char *cc,
23005 rtx result,
23006 rtx value,
23007 rtx memory)
23008{
23009 const char *suffix = arm_ldrex_suffix (mode);
23010 rtx operands[3];
23011
23012 operands[0] = result;
23013 operands[1] = value;
23014 operands[2] = memory;
23015 arm_output_asm_insn (emit, 0, operands, "strex%s%s\t%%0, %%1, %%C2", suffix,
23016 cc);
23017}
23018
23019/* Helper to emit a two operand instruction. */
23020static void
23021arm_output_op2 (emit_f emit, const char *mnemonic, rtx d, rtx s)
23022{
23023 rtx operands[2];
23024
23025 operands[0] = d;
23026 operands[1] = s;
23027 arm_output_asm_insn (emit, 0, operands, "%s\t%%0, %%1", mnemonic);
23028}
23029
23030/* Helper to emit a three operand instruction. */
23031static void
23032arm_output_op3 (emit_f emit, const char *mnemonic, rtx d, rtx a, rtx b)
23033{
23034 rtx operands[3];
23035
23036 operands[0] = d;
23037 operands[1] = a;
23038 operands[2] = b;
23039 arm_output_asm_insn (emit, 0, operands, "%s\t%%0, %%1, %%2", mnemonic);
23040}
23041
23042/* Emit a load store exclusive synchronization loop.
23043
23044 do
23045 old_value = [mem]
23046 if old_value != required_value
23047 break;
23048 t1 = sync_op (old_value, new_value)
23049 [mem] = t1, t2 = [0|1]
23050 while ! t2
23051
23052 Note:
23053 t1 == t2 is not permitted
23054 t1 == old_value is permitted
23055
23056 required_value:
23057
23058 RTX register or const_int representing the required old_value for
23059 the modify to continue, if NULL no comparsion is performed. */
23060static void
23061arm_output_sync_loop (emit_f emit,
23062 enum machine_mode mode,
23063 rtx old_value,
23064 rtx memory,
23065 rtx required_value,
23066 rtx new_value,
23067 rtx t1,
23068 rtx t2,
23069 enum attr_sync_op sync_op,
23070 int early_barrier_required)
23071{
23072 rtx operands[1];
23073
23074 gcc_assert (t1 != t2);
23075
23076 if (early_barrier_required)
23077 arm_process_output_memory_barrier (emit, NULL);
23078
23079 arm_output_asm_insn (emit, 1, operands, "%sLSYT%%=:", LOCAL_LABEL_PREFIX);
23080
23081 arm_output_ldrex (emit, mode, old_value, memory);
23082
23083 if (required_value)
23084 {
23085 rtx operands[2];
23086
23087 operands[0] = old_value;
23088 operands[1] = required_value;
23089 arm_output_asm_insn (emit, 0, operands, "cmp\t%%0, %%1");
23090 arm_output_asm_insn (emit, 0, operands, "bne\t%sLSYB%%=", LOCAL_LABEL_PREFIX);
23091 }
23092
23093 switch (sync_op)
23094 {
23095 case SYNC_OP_ADD:
23096 arm_output_op3 (emit, "add", t1, old_value, new_value);
23097 break;
23098
23099 case SYNC_OP_SUB:
23100 arm_output_op3 (emit, "sub", t1, old_value, new_value);
23101 break;
23102
23103 case SYNC_OP_IOR:
23104 arm_output_op3 (emit, "orr", t1, old_value, new_value);
23105 break;
23106
23107 case SYNC_OP_XOR:
23108 arm_output_op3 (emit, "eor", t1, old_value, new_value);
23109 break;
23110
23111 case SYNC_OP_AND:
23112 arm_output_op3 (emit,"and", t1, old_value, new_value);
23113 break;
23114
23115 case SYNC_OP_NAND:
23116 arm_output_op3 (emit, "and", t1, old_value, new_value);
23117 arm_output_op2 (emit, "mvn", t1, t1);
23118 break;
23119
23120 case SYNC_OP_NONE:
23121 t1 = new_value;
23122 break;
23123 }
23124
23125 arm_output_strex (emit, mode, "", t2, t1, memory);
23126 operands[0] = t2;
23127 arm_output_asm_insn (emit, 0, operands, "teq\t%%0, #0");
23128 arm_output_asm_insn (emit, 0, operands, "bne\t%sLSYT%%=", LOCAL_LABEL_PREFIX);
23129
23130 arm_process_output_memory_barrier (emit, NULL);
23131 arm_output_asm_insn (emit, 1, operands, "%sLSYB%%=:", LOCAL_LABEL_PREFIX);
23132}
23133
23134static rtx
23135arm_get_sync_operand (rtx *operands, int index, rtx default_value)
23136{
23137 if (index > 0)
23138 default_value = operands[index - 1];
23139
23140 return default_value;
23141}
23142
23143#define FETCH_SYNC_OPERAND(NAME, DEFAULT) \
23144 arm_get_sync_operand (operands, (int) get_attr_sync_##NAME (insn), DEFAULT);
23145
23146/* Extract the operands for a synchroniztion instruction from the
23147 instructions attributes and emit the instruction. */
23148static void
23149arm_process_output_sync_insn (emit_f emit, rtx insn, rtx *operands)
23150{
23151 rtx result, memory, required_value, new_value, t1, t2;
23152 int early_barrier;
23153 enum machine_mode mode;
23154 enum attr_sync_op sync_op;
23155
23156 result = FETCH_SYNC_OPERAND(result, 0);
23157 memory = FETCH_SYNC_OPERAND(memory, 0);
23158 required_value = FETCH_SYNC_OPERAND(required_value, 0);
23159 new_value = FETCH_SYNC_OPERAND(new_value, 0);
23160 t1 = FETCH_SYNC_OPERAND(t1, 0);
23161 t2 = FETCH_SYNC_OPERAND(t2, 0);
23162 early_barrier =
23163 get_attr_sync_release_barrier (insn) == SYNC_RELEASE_BARRIER_YES;
23164 sync_op = get_attr_sync_op (insn);
23165 mode = GET_MODE (memory);
23166
23167 arm_output_sync_loop (emit, mode, result, memory, required_value,
23168 new_value, t1, t2, sync_op, early_barrier);
23169}
23170
23171/* Emit a synchronization instruction loop. */
23172const char *
23173arm_output_sync_insn (rtx insn, rtx *operands)
23174{
23175 arm_process_output_sync_insn (arm_emit, insn, operands);
23176 return "";
23177}
23178
23179/* Count the number of machine instruction that will be emitted for a
23180 synchronization instruction. Note that the emitter used does not
23181 emit instructions, it just counts instructions being carefull not
23182 to count labels. */
23183unsigned int
23184arm_sync_loop_insns (rtx insn, rtx *operands)
23185{
23186 arm_insn_count = 0;
23187 arm_process_output_sync_insn (arm_count, insn, operands);
23188 return arm_insn_count;
23189}
23190
23191/* Helper to call a target sync instruction generator, dealing with
23192 the variation in operands required by the different generators. */
23193static rtx
23194arm_call_generator (struct arm_sync_generator *generator, rtx old_value,
23195 rtx memory, rtx required_value, rtx new_value)
23196{
23197 switch (generator->op)
23198 {
23199 case arm_sync_generator_omn:
23200 gcc_assert (! required_value);
23201 return generator->u.omn (old_value, memory, new_value);
23202
23203 case arm_sync_generator_omrn:
23204 gcc_assert (required_value);
23205 return generator->u.omrn (old_value, memory, required_value, new_value);
23206 }
23207
23208 return NULL;
23209}
23210
23211/* Expand a synchronization loop. The synchronization loop is expanded
23212 as an opaque block of instructions in order to ensure that we do
23213 not subsequently get extraneous memory accesses inserted within the
23214 critical region. The exclusive access property of ldrex/strex is
23215 only guaranteed in there are no intervening memory accesses. */
23216void
23217arm_expand_sync (enum machine_mode mode,
23218 struct arm_sync_generator *generator,
23219 rtx target, rtx memory, rtx required_value, rtx new_value)
23220{
23221 if (target == NULL)
23222 target = gen_reg_rtx (mode);
23223
23224 memory = arm_legitimize_sync_memory (memory);
23225 if (mode != SImode)
23226 {
23227 rtx load_temp = gen_reg_rtx (SImode);
23228
23229 if (required_value)
23230 required_value = convert_modes (SImode, mode, required_value, true);
23231
23232 new_value = convert_modes (SImode, mode, new_value, true);
23233 emit_insn (arm_call_generator (generator, load_temp, memory,
23234 required_value, new_value));
23235 emit_move_insn (target, gen_lowpart (mode, load_temp));
23236 }
23237 else
23238 {
23239 emit_insn (arm_call_generator (generator, target, memory, required_value,
23240 new_value));
23241 }
23242}
23243
c452684d
JB
23244static bool
23245arm_vector_alignment_reachable (const_tree type, bool is_packed)
23246{
23247 /* Vectors which aren't in packed structures will not be less aligned than
23248 the natural alignment of their element type, so this is safe. */
23249 if (TARGET_NEON && !BYTES_BIG_ENDIAN)
23250 return !is_packed;
23251
23252 return default_builtin_vector_alignment_reachable (type, is_packed);
23253}
23254
23255static bool
23256arm_builtin_support_vector_misalignment (enum machine_mode mode,
23257 const_tree type, int misalignment,
23258 bool is_packed)
23259{
23260 if (TARGET_NEON && !BYTES_BIG_ENDIAN)
23261 {
23262 HOST_WIDE_INT align = TYPE_ALIGN_UNIT (type);
23263
23264 if (is_packed)
23265 return align == 1;
23266
23267 /* If the misalignment is unknown, we should be able to handle the access
23268 so long as it is not to a member of a packed data structure. */
23269 if (misalignment == -1)
23270 return true;
23271
23272 /* Return true if the misalignment is a multiple of the natural alignment
23273 of the vector's element type. This is probably always going to be
23274 true in practice, since we've already established that this isn't a
23275 packed access. */
23276 return ((misalignment % align) == 0);
23277 }
23278
23279 return default_builtin_support_vector_misalignment (mode, type, misalignment,
23280 is_packed);
23281}
23282
5efd84c5
NF
23283static void
23284arm_conditional_register_usage (void)
23285{
23286 int regno;
23287
23288 if (TARGET_SOFT_FLOAT || TARGET_THUMB1 || !TARGET_FPA)
23289 {
23290 for (regno = FIRST_FPA_REGNUM;
23291 regno <= LAST_FPA_REGNUM; ++regno)
23292 fixed_regs[regno] = call_used_regs[regno] = 1;
23293 }
23294
23295 if (TARGET_THUMB1 && optimize_size)
23296 {
23297 /* When optimizing for size on Thumb-1, it's better not
23298 to use the HI regs, because of the overhead of
23299 stacking them. */
23300 for (regno = FIRST_HI_REGNUM;
23301 regno <= LAST_HI_REGNUM; ++regno)
23302 fixed_regs[regno] = call_used_regs[regno] = 1;
23303 }
23304
23305 /* The link register can be clobbered by any branch insn,
23306 but we have no way to track that at present, so mark
23307 it as unavailable. */
23308 if (TARGET_THUMB1)
23309 fixed_regs[LR_REGNUM] = call_used_regs[LR_REGNUM] = 1;
23310
23311 if (TARGET_32BIT && TARGET_HARD_FLOAT)
23312 {
23313 if (TARGET_MAVERICK)
23314 {
23315 for (regno = FIRST_FPA_REGNUM;
23316 regno <= LAST_FPA_REGNUM; ++ regno)
23317 fixed_regs[regno] = call_used_regs[regno] = 1;
23318 for (regno = FIRST_CIRRUS_FP_REGNUM;
23319 regno <= LAST_CIRRUS_FP_REGNUM; ++ regno)
23320 {
23321 fixed_regs[regno] = 0;
23322 call_used_regs[regno] = regno < FIRST_CIRRUS_FP_REGNUM + 4;
23323 }
23324 }
23325 if (TARGET_VFP)
23326 {
23327 /* VFPv3 registers are disabled when earlier VFP
23328 versions are selected due to the definition of
23329 LAST_VFP_REGNUM. */
23330 for (regno = FIRST_VFP_REGNUM;
23331 regno <= LAST_VFP_REGNUM; ++ regno)
23332 {
23333 fixed_regs[regno] = 0;
23334 call_used_regs[regno] = regno < FIRST_VFP_REGNUM + 16
23335 || regno >= FIRST_VFP_REGNUM + 32;
23336 }
23337 }
23338 }
23339
23340 if (TARGET_REALLY_IWMMXT)
23341 {
23342 regno = FIRST_IWMMXT_GR_REGNUM;
23343 /* The 2002/10/09 revision of the XScale ABI has wCG0
23344 and wCG1 as call-preserved registers. The 2002/11/21
23345 revision changed this so that all wCG registers are
23346 scratch registers. */
23347 for (regno = FIRST_IWMMXT_GR_REGNUM;
23348 regno <= LAST_IWMMXT_GR_REGNUM; ++ regno)
23349 fixed_regs[regno] = 0;
23350 /* The XScale ABI has wR0 - wR9 as scratch registers,
23351 the rest as call-preserved registers. */
23352 for (regno = FIRST_IWMMXT_REGNUM;
23353 regno <= LAST_IWMMXT_REGNUM; ++ regno)
23354 {
23355 fixed_regs[regno] = 0;
23356 call_used_regs[regno] = regno < FIRST_IWMMXT_REGNUM + 10;
23357 }
23358 }
23359
23360 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
23361 {
23362 fixed_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
23363 call_used_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
23364 }
23365 else if (TARGET_APCS_STACK)
23366 {
23367 fixed_regs[10] = 1;
23368 call_used_regs[10] = 1;
23369 }
23370 /* -mcaller-super-interworking reserves r11 for calls to
23371 _interwork_r11_call_via_rN(). Making the register global
23372 is an easy way of ensuring that it remains valid for all
23373 calls. */
23374 if (TARGET_APCS_FRAME || TARGET_CALLER_INTERWORKING
23375 || TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME)
23376 {
23377 fixed_regs[ARM_HARD_FRAME_POINTER_REGNUM] = 1;
23378 call_used_regs[ARM_HARD_FRAME_POINTER_REGNUM] = 1;
23379 if (TARGET_CALLER_INTERWORKING)
23380 global_regs[ARM_HARD_FRAME_POINTER_REGNUM] = 1;
23381 }
23382 SUBTARGET_CONDITIONAL_REGISTER_USAGE
23383}
23384
d3585b76 23385#include "gt-arm.h"
This page took 8.305572 seconds and 5 git commands to generate.