]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/arm/arm.c
target.def (legitimate_constant_p): New hook.
[thirdparty/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,
147a0bcf 3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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"
aec3cfba 44#include "recog.h"
390b17c2 45#include "cgraph.h"
92a432f4 46#include "ggc.h"
d5b7b3ae 47#include "except.h"
39dabefd 48#include "c-family/c-pragma.h" /* ??? */
7b8b8ade 49#include "integrate.h"
c27ba912 50#include "tm_p.h"
672a6f42
NB
51#include "target.h"
52#include "target-def.h"
980e61bb 53#include "debug.h"
6e34d3a3 54#include "langhooks.h"
6fb5fa3c 55#include "df.h"
0fd8c3ad 56#include "intl.h"
353a58f7 57#include "libfuncs.h"
ec3728ad 58#include "params.h"
96e45421 59#include "opts.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);
1a627b35
RS
147static bool arm_cannot_force_const_mem (enum machine_mode, rtx);
148static bool arm_legitimate_constant_p (enum machine_mode, rtx);
d5a0a47b
RE
149static bool arm_rtx_costs_1 (rtx, enum rtx_code, int*, bool);
150static bool arm_size_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *);
151static bool arm_slowmul_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
152static bool arm_fastmul_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
153static bool arm_xscale_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
154static bool arm_9e_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
f40751dd
JH
155static bool arm_rtx_costs (rtx, int, int, int *, bool);
156static int arm_address_cost (rtx, bool);
e32bac5b
RE
157static bool arm_memory_load_p (rtx);
158static bool arm_cirrus_insn_p (rtx);
159static void cirrus_reorg (rtx);
5a9335ef 160static void arm_init_builtins (void);
5a9335ef
NC
161static void arm_init_iwmmxt_builtins (void);
162static rtx safe_vector_operand (rtx, enum machine_mode);
163static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
164static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
165static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
229a1c59 166static tree arm_builtin_decl (unsigned, bool);
a406f566 167static void emit_constant_insn (rtx cond, rtx pattern);
d66437c5 168static rtx emit_set_insn (rtx, rtx);
78a52f11
RH
169static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
170 tree, bool);
9c6a2bee
NF
171static rtx arm_function_arg (CUMULATIVE_ARGS *, enum machine_mode,
172 const_tree, bool);
173static void arm_function_arg_advance (CUMULATIVE_ARGS *, enum machine_mode,
174 const_tree, bool);
c2ed6cf8 175static unsigned int arm_function_arg_boundary (enum machine_mode, const_tree);
390b17c2
RE
176static rtx aapcs_allocate_return_reg (enum machine_mode, const_tree,
177 const_tree);
178static int aapcs_select_return_coproc (const_tree, const_tree);
5a9335ef 179
7abc66b1 180#ifdef OBJECT_FORMAT_ELF
9403b7f7
RS
181static void arm_elf_asm_constructor (rtx, int) ATTRIBUTE_UNUSED;
182static void arm_elf_asm_destructor (rtx, int) ATTRIBUTE_UNUSED;
7abc66b1 183#endif
fb49053f 184#ifndef ARM_PE
e32bac5b 185static void arm_encode_section_info (tree, rtx, int);
fb49053f 186#endif
b12a00f1
RE
187
188static void arm_file_end (void);
6c6aa1af 189static void arm_file_start (void);
b12a00f1 190
1cc9f5f5
KH
191static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
192 tree, int *, int);
8cd5a4e0 193static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
586de218
KG
194 enum machine_mode, const_tree, bool);
195static bool arm_promote_prototypes (const_tree);
6b045785 196static bool arm_default_short_enums (void);
13c1cd82 197static bool arm_align_anon_bitfield (void);
586de218
KG
198static bool arm_return_in_msb (const_tree);
199static bool arm_must_pass_in_stack (enum machine_mode, const_tree);
23668cf7 200static bool arm_return_in_memory (const_tree, const_tree);
f0a0390e 201#if ARM_UNWIND_INFO
617a1b71
PB
202static void arm_unwind_emit (FILE *, rtx);
203static bool arm_output_ttype (rtx);
a68b5e52
RH
204static void arm_asm_emit_except_personality (rtx);
205static void arm_asm_init_sections (void);
617a1b71 206#endif
d5fabb58 207static enum unwind_info_type arm_except_unwind_info (struct gcc_options *);
5b3e6663 208static void arm_dwarf_handle_frame_unspec (const char *, rtx, int);
854b8a40 209static rtx arm_dwarf_register_span (rtx);
c237e94a 210
4185ae53
PB
211static tree arm_cxx_guard_type (void);
212static bool arm_cxx_guard_mask_bit (void);
46e995e0
PB
213static tree arm_get_cookie_size (tree);
214static bool arm_cookie_has_size (void);
44d10c10 215static bool arm_cxx_cdtor_returns_this (void);
505970fc 216static bool arm_cxx_key_method_may_be_inline (void);
1e731102
MM
217static void arm_cxx_determine_class_data_visibility (tree);
218static bool arm_cxx_class_data_always_comdat (void);
9f62c3e3 219static bool arm_cxx_use_aeabi_atexit (void);
b3f8d95d 220static void arm_init_libfuncs (void);
07d8efe3
MM
221static tree arm_build_builtin_va_list (void);
222static void arm_expand_builtin_va_start (tree, rtx);
ae46a823 223static tree arm_gimplify_va_arg_expr (tree, tree, gimple_seq *, gimple_seq *);
c5387660 224static void arm_option_override (void);
273a2526 225static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
d3585b76
DJ
226static bool arm_cannot_copy_insn_p (rtx);
227static bool arm_tls_symbol_p (rtx x);
bd4dc3cd 228static int arm_issue_rate (void);
afcc986d 229static void arm_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
ffda8a0d 230static bool arm_output_addr_const_extra (FILE *, rtx);
007e61c2 231static bool arm_allocate_stack_slots_for_args (void);
0fd8c3ad
SL
232static const char *arm_invalid_parameter_type (const_tree t);
233static const char *arm_invalid_return_type (const_tree t);
234static tree arm_promoted_type (const_tree t);
235static tree arm_convert_to_type (tree type, tree expr);
bdc4827b 236static bool arm_scalar_mode_supported_p (enum machine_mode);
b52b1749 237static bool arm_frame_pointer_required (void);
7b5cbb57 238static bool arm_can_eliminate (const int, const int);
0ef9304b
RH
239static void arm_asm_trampoline_template (FILE *);
240static void arm_trampoline_init (rtx, tree, rtx);
241static rtx arm_trampoline_adjust_address (rtx);
85c9bcd4 242static rtx arm_pic_static_addr (rtx orig, rtx reg);
b0c13111
RR
243static bool cortex_a9_sched_adjust_cost (rtx, rtx, rtx, int *);
244static bool xscale_sched_adjust_cost (rtx, rtx, rtx, int *);
c02a5ccb 245static bool fa726te_sched_adjust_cost (rtx, rtx, rtx, int *);
cc4b5170 246static enum machine_mode arm_preferred_simd_mode (enum machine_mode);
d163e655 247static bool arm_class_likely_spilled_p (reg_class_t);
c452684d
JB
248static bool arm_vector_alignment_reachable (const_tree type, bool is_packed);
249static bool arm_builtin_support_vector_misalignment (enum machine_mode mode,
250 const_tree type,
251 int misalignment,
252 bool is_packed);
5efd84c5 253static void arm_conditional_register_usage (void);
6d3fbe2f 254static reg_class_t arm_preferred_rename_class (reg_class_t rclass);
69d52339 255static unsigned int arm_autovectorize_vector_sizes (void);
b52b1749 256
5a82ecd9
ILT
257\f
258/* Table of machine attributes. */
259static const struct attribute_spec arm_attribute_table[] =
260{
62d784f7
KT
261 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
262 affects_type_identity } */
5a82ecd9
ILT
263 /* Function calls made to this symbol must be done indirectly, because
264 it may lie outside of the 26 bit addressing range of a normal function
265 call. */
62d784f7 266 { "long_call", 0, 0, false, true, true, NULL, false },
5a82ecd9
ILT
267 /* Whereas these functions are always known to reside within the 26 bit
268 addressing range. */
62d784f7 269 { "short_call", 0, 0, false, true, true, NULL, false },
390b17c2 270 /* Specify the procedure call conventions for a function. */
62d784f7
KT
271 { "pcs", 1, 1, false, true, true, arm_handle_pcs_attribute,
272 false },
5a82ecd9 273 /* Interrupt Service Routines have special prologue and epilogue requirements. */
62d784f7
KT
274 { "isr", 0, 1, false, false, false, arm_handle_isr_attribute,
275 false },
276 { "interrupt", 0, 1, false, false, false, arm_handle_isr_attribute,
277 false },
278 { "naked", 0, 0, true, false, false, arm_handle_fndecl_attribute,
279 false },
5a82ecd9
ILT
280#ifdef ARM_PE
281 /* ARM/PE has three new attributes:
282 interfacearm - ?
283 dllexport - for exporting a function/variable that will live in a dll
284 dllimport - for importing a function/variable from a dll
d3585b76 285
5a82ecd9
ILT
286 Microsoft allows multiple declspecs in one __declspec, separating
287 them with spaces. We do NOT support this. Instead, use __declspec
288 multiple times.
289 */
62d784f7
KT
290 { "dllimport", 0, 0, true, false, false, NULL, false },
291 { "dllexport", 0, 0, true, false, false, NULL, false },
292 { "interfacearm", 0, 0, true, false, false, arm_handle_fndecl_attribute,
293 false },
5a82ecd9 294#elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
62d784f7
KT
295 { "dllimport", 0, 0, false, false, false, handle_dll_attribute, false },
296 { "dllexport", 0, 0, false, false, false, handle_dll_attribute, false },
297 { "notshared", 0, 0, false, true, false, arm_handle_notshared_attribute,
298 false },
5a82ecd9 299#endif
62d784f7 300 { NULL, 0, 0, false, false, false, NULL, false }
5a82ecd9 301};
3020190e
JM
302
303/* Set default optimization options. */
304static const struct default_options arm_option_optimization_table[] =
305 {
306 /* Enable section anchors by default at -O1 or higher. */
307 { OPT_LEVELS_1_PLUS, OPT_fsection_anchors, NULL, 1 },
308 { OPT_LEVELS_1_PLUS, OPT_fomit_frame_pointer, NULL, 1 },
309 { OPT_LEVELS_NONE, 0, NULL, 0 }
310 };
672a6f42
NB
311\f
312/* Initialize the GCC target structure. */
b2ca3702 313#if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1d6e90ac 314#undef TARGET_MERGE_DECL_ATTRIBUTES
672a6f42
NB
315#define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
316#endif
f3bb6135 317
506d7b68
PB
318#undef TARGET_LEGITIMIZE_ADDRESS
319#define TARGET_LEGITIMIZE_ADDRESS arm_legitimize_address
320
1d6e90ac 321#undef TARGET_ATTRIBUTE_TABLE
91d231cb 322#define TARGET_ATTRIBUTE_TABLE arm_attribute_table
672a6f42 323
6c6aa1af
PB
324#undef TARGET_ASM_FILE_START
325#define TARGET_ASM_FILE_START arm_file_start
b12a00f1
RE
326#undef TARGET_ASM_FILE_END
327#define TARGET_ASM_FILE_END arm_file_end
328
1d6e90ac 329#undef TARGET_ASM_ALIGNED_SI_OP
301d03af 330#define TARGET_ASM_ALIGNED_SI_OP NULL
1d6e90ac 331#undef TARGET_ASM_INTEGER
301d03af 332#define TARGET_ASM_INTEGER arm_assemble_integer
301d03af 333
944442bb
NF
334#undef TARGET_PRINT_OPERAND
335#define TARGET_PRINT_OPERAND arm_print_operand
336#undef TARGET_PRINT_OPERAND_ADDRESS
337#define TARGET_PRINT_OPERAND_ADDRESS arm_print_operand_address
338#undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
339#define TARGET_PRINT_OPERAND_PUNCT_VALID_P arm_print_operand_punct_valid_p
340
ffda8a0d
AS
341#undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
342#define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA arm_output_addr_const_extra
343
1d6e90ac 344#undef TARGET_ASM_FUNCTION_PROLOGUE
08c148a8
NB
345#define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
346
1d6e90ac 347#undef TARGET_ASM_FUNCTION_EPILOGUE
08c148a8
NB
348#define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
349
c54c7322
RS
350#undef TARGET_DEFAULT_TARGET_FLAGS
351#define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
c5387660
JM
352#undef TARGET_OPTION_OVERRIDE
353#define TARGET_OPTION_OVERRIDE arm_option_override
3020190e
JM
354#undef TARGET_OPTION_OPTIMIZATION_TABLE
355#define TARGET_OPTION_OPTIMIZATION_TABLE arm_option_optimization_table
c54c7322 356
1d6e90ac 357#undef TARGET_COMP_TYPE_ATTRIBUTES
8d8e52be
JM
358#define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
359
1d6e90ac 360#undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
8d8e52be
JM
361#define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
362
1d6e90ac 363#undef TARGET_SCHED_ADJUST_COST
c237e94a
ZW
364#define TARGET_SCHED_ADJUST_COST arm_adjust_cost
365
fb49053f
RH
366#undef TARGET_ENCODE_SECTION_INFO
367#ifdef ARM_PE
368#define TARGET_ENCODE_SECTION_INFO arm_pe_encode_section_info
369#else
370#define TARGET_ENCODE_SECTION_INFO arm_encode_section_info
371#endif
372
5a9335ef 373#undef TARGET_STRIP_NAME_ENCODING
772c5265
RH
374#define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
375
5a9335ef 376#undef TARGET_ASM_INTERNAL_LABEL
4977bab6
ZW
377#define TARGET_ASM_INTERNAL_LABEL arm_internal_label
378
5a9335ef 379#undef TARGET_FUNCTION_OK_FOR_SIBCALL
4977bab6
ZW
380#define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
381
390b17c2
RE
382#undef TARGET_FUNCTION_VALUE
383#define TARGET_FUNCTION_VALUE arm_function_value
384
385#undef TARGET_LIBCALL_VALUE
386#define TARGET_LIBCALL_VALUE arm_libcall_value
387
5a9335ef 388#undef TARGET_ASM_OUTPUT_MI_THUNK
c590b625 389#define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
5a9335ef 390#undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
3961e8fe 391#define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
c590b625 392
5a9335ef 393#undef TARGET_RTX_COSTS
f40751dd 394#define TARGET_RTX_COSTS arm_rtx_costs
5a9335ef 395#undef TARGET_ADDRESS_COST
dcefdf67 396#define TARGET_ADDRESS_COST arm_address_cost
3c50106f 397
273a2526
RS
398#undef TARGET_SHIFT_TRUNCATION_MASK
399#define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
f676971a
EC
400#undef TARGET_VECTOR_MODE_SUPPORTED_P
401#define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
cc4b5170
RG
402#undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
403#define TARGET_VECTORIZE_PREFERRED_SIMD_MODE arm_preferred_simd_mode
69d52339
IR
404#undef TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_SIZES
405#define TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_SIZES \
406 arm_autovectorize_vector_sizes
f676971a 407
5a9335ef 408#undef TARGET_MACHINE_DEPENDENT_REORG
18dbd950
RS
409#define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
410
5a9335ef
NC
411#undef TARGET_INIT_BUILTINS
412#define TARGET_INIT_BUILTINS arm_init_builtins
413#undef TARGET_EXPAND_BUILTIN
414#define TARGET_EXPAND_BUILTIN arm_expand_builtin
229a1c59
JZ
415#undef TARGET_BUILTIN_DECL
416#define TARGET_BUILTIN_DECL arm_builtin_decl
5a9335ef 417
b3f8d95d
MM
418#undef TARGET_INIT_LIBFUNCS
419#define TARGET_INIT_LIBFUNCS arm_init_libfuncs
420
cde0f3fd
PB
421#undef TARGET_PROMOTE_FUNCTION_MODE
422#define TARGET_PROMOTE_FUNCTION_MODE arm_promote_function_mode
f9ba5949 423#undef TARGET_PROMOTE_PROTOTYPES
70301b45 424#define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
8cd5a4e0
RH
425#undef TARGET_PASS_BY_REFERENCE
426#define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
78a52f11
RH
427#undef TARGET_ARG_PARTIAL_BYTES
428#define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
9c6a2bee
NF
429#undef TARGET_FUNCTION_ARG
430#define TARGET_FUNCTION_ARG arm_function_arg
431#undef TARGET_FUNCTION_ARG_ADVANCE
432#define TARGET_FUNCTION_ARG_ADVANCE arm_function_arg_advance
c2ed6cf8
NF
433#undef TARGET_FUNCTION_ARG_BOUNDARY
434#define TARGET_FUNCTION_ARG_BOUNDARY arm_function_arg_boundary
f9ba5949 435
1cc9f5f5
KH
436#undef TARGET_SETUP_INCOMING_VARARGS
437#define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
438
007e61c2
PB
439#undef TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS
440#define TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS arm_allocate_stack_slots_for_args
441
0ef9304b
RH
442#undef TARGET_ASM_TRAMPOLINE_TEMPLATE
443#define TARGET_ASM_TRAMPOLINE_TEMPLATE arm_asm_trampoline_template
444#undef TARGET_TRAMPOLINE_INIT
445#define TARGET_TRAMPOLINE_INIT arm_trampoline_init
446#undef TARGET_TRAMPOLINE_ADJUST_ADDRESS
447#define TARGET_TRAMPOLINE_ADJUST_ADDRESS arm_trampoline_adjust_address
448
6b045785
PB
449#undef TARGET_DEFAULT_SHORT_ENUMS
450#define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
451
13c1cd82
PB
452#undef TARGET_ALIGN_ANON_BITFIELD
453#define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
454
c2a64439
PB
455#undef TARGET_NARROW_VOLATILE_BITFIELD
456#define TARGET_NARROW_VOLATILE_BITFIELD hook_bool_void_false
457
4185ae53
PB
458#undef TARGET_CXX_GUARD_TYPE
459#define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
460
461#undef TARGET_CXX_GUARD_MASK_BIT
462#define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
463
46e995e0
PB
464#undef TARGET_CXX_GET_COOKIE_SIZE
465#define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
466
467#undef TARGET_CXX_COOKIE_HAS_SIZE
468#define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
469
44d10c10
PB
470#undef TARGET_CXX_CDTOR_RETURNS_THIS
471#define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
472
505970fc
MM
473#undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
474#define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
475
9f62c3e3
PB
476#undef TARGET_CXX_USE_AEABI_ATEXIT
477#define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
478
1e731102
MM
479#undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
480#define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
481 arm_cxx_determine_class_data_visibility
482
483#undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
484#define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
505970fc 485
866af8a9
JB
486#undef TARGET_RETURN_IN_MSB
487#define TARGET_RETURN_IN_MSB arm_return_in_msb
488
23668cf7
CLT
489#undef TARGET_RETURN_IN_MEMORY
490#define TARGET_RETURN_IN_MEMORY arm_return_in_memory
491
866af8a9
JB
492#undef TARGET_MUST_PASS_IN_STACK
493#define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
494
f0a0390e 495#if ARM_UNWIND_INFO
38f8b050
JR
496#undef TARGET_ASM_UNWIND_EMIT
497#define TARGET_ASM_UNWIND_EMIT arm_unwind_emit
617a1b71
PB
498
499/* EABI unwinding tables use a different format for the typeinfo tables. */
500#undef TARGET_ASM_TTYPE
501#define TARGET_ASM_TTYPE arm_output_ttype
502
503#undef TARGET_ARM_EABI_UNWINDER
504#define TARGET_ARM_EABI_UNWINDER true
a68b5e52
RH
505
506#undef TARGET_ASM_EMIT_EXCEPT_PERSONALITY
507#define TARGET_ASM_EMIT_EXCEPT_PERSONALITY arm_asm_emit_except_personality
508
509#undef TARGET_ASM_INIT_SECTIONS
510#define TARGET_ASM_INIT_SECTIONS arm_asm_init_sections
f0a0390e
RH
511#endif /* ARM_UNWIND_INFO */
512
513#undef TARGET_EXCEPT_UNWIND_INFO
514#define TARGET_EXCEPT_UNWIND_INFO arm_except_unwind_info
617a1b71 515
5b3e6663
PB
516#undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
517#define TARGET_DWARF_HANDLE_FRAME_UNSPEC arm_dwarf_handle_frame_unspec
518
854b8a40
JB
519#undef TARGET_DWARF_REGISTER_SPAN
520#define TARGET_DWARF_REGISTER_SPAN arm_dwarf_register_span
521
d3585b76
DJ
522#undef TARGET_CANNOT_COPY_INSN_P
523#define TARGET_CANNOT_COPY_INSN_P arm_cannot_copy_insn_p
524
525#ifdef HAVE_AS_TLS
526#undef TARGET_HAVE_TLS
527#define TARGET_HAVE_TLS true
528#endif
529
2929029c
WG
530#undef TARGET_HAVE_CONDITIONAL_EXECUTION
531#define TARGET_HAVE_CONDITIONAL_EXECUTION arm_have_conditional_execution
532
1a627b35
RS
533#undef TARGET_LEGITIMATE_CONSTANT_P
534#define TARGET_LEGITIMATE_CONSTANT_P arm_legitimate_constant_p
535
d3585b76 536#undef TARGET_CANNOT_FORCE_CONST_MEM
8426b956 537#define TARGET_CANNOT_FORCE_CONST_MEM arm_cannot_force_const_mem
d3585b76 538
f67358da
PB
539#undef TARGET_MAX_ANCHOR_OFFSET
540#define TARGET_MAX_ANCHOR_OFFSET 4095
541
542/* The minimum is set such that the total size of the block
543 for a particular anchor is -4088 + 1 + 4095 bytes, which is
544 divisible by eight, ensuring natural spacing of anchors. */
545#undef TARGET_MIN_ANCHOR_OFFSET
546#define TARGET_MIN_ANCHOR_OFFSET -4088
547
bd4dc3cd
PB
548#undef TARGET_SCHED_ISSUE_RATE
549#define TARGET_SCHED_ISSUE_RATE arm_issue_rate
550
608063c3
JB
551#undef TARGET_MANGLE_TYPE
552#define TARGET_MANGLE_TYPE arm_mangle_type
553
07d8efe3
MM
554#undef TARGET_BUILD_BUILTIN_VA_LIST
555#define TARGET_BUILD_BUILTIN_VA_LIST arm_build_builtin_va_list
556#undef TARGET_EXPAND_BUILTIN_VA_START
557#define TARGET_EXPAND_BUILTIN_VA_START arm_expand_builtin_va_start
558#undef TARGET_GIMPLIFY_VA_ARG_EXPR
559#define TARGET_GIMPLIFY_VA_ARG_EXPR arm_gimplify_va_arg_expr
560
afcc986d
JM
561#ifdef HAVE_AS_TLS
562#undef TARGET_ASM_OUTPUT_DWARF_DTPREL
563#define TARGET_ASM_OUTPUT_DWARF_DTPREL arm_output_dwarf_dtprel
564#endif
565
c6c3dba9
PB
566#undef TARGET_LEGITIMATE_ADDRESS_P
567#define TARGET_LEGITIMATE_ADDRESS_P arm_legitimate_address_p
568
0fd8c3ad
SL
569#undef TARGET_INVALID_PARAMETER_TYPE
570#define TARGET_INVALID_PARAMETER_TYPE arm_invalid_parameter_type
571
572#undef TARGET_INVALID_RETURN_TYPE
573#define TARGET_INVALID_RETURN_TYPE arm_invalid_return_type
574
575#undef TARGET_PROMOTED_TYPE
576#define TARGET_PROMOTED_TYPE arm_promoted_type
577
578#undef TARGET_CONVERT_TO_TYPE
579#define TARGET_CONVERT_TO_TYPE arm_convert_to_type
580
bdc4827b
SL
581#undef TARGET_SCALAR_MODE_SUPPORTED_P
582#define TARGET_SCALAR_MODE_SUPPORTED_P arm_scalar_mode_supported_p
583
b52b1749
AS
584#undef TARGET_FRAME_POINTER_REQUIRED
585#define TARGET_FRAME_POINTER_REQUIRED arm_frame_pointer_required
586
7b5cbb57
AS
587#undef TARGET_CAN_ELIMINATE
588#define TARGET_CAN_ELIMINATE arm_can_eliminate
589
5efd84c5
NF
590#undef TARGET_CONDITIONAL_REGISTER_USAGE
591#define TARGET_CONDITIONAL_REGISTER_USAGE arm_conditional_register_usage
592
d163e655
AS
593#undef TARGET_CLASS_LIKELY_SPILLED_P
594#define TARGET_CLASS_LIKELY_SPILLED_P arm_class_likely_spilled_p
595
c452684d
JB
596#undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
597#define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE \
598 arm_vector_alignment_reachable
599
600#undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
601#define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT \
602 arm_builtin_support_vector_misalignment
603
74e32076
YQ
604#undef TARGET_PREFERRED_RENAME_CLASS
605#define TARGET_PREFERRED_RENAME_CLASS \
606 arm_preferred_rename_class
607
f6897b10 608struct gcc_target targetm = TARGET_INITIALIZER;
672a6f42 609\f
c7319d87
RE
610/* Obstack for minipool constant handling. */
611static struct obstack minipool_obstack;
1d6e90ac 612static char * minipool_startobj;
c7319d87 613
1d6e90ac
NC
614/* The maximum number of insns skipped which
615 will be conditionalised if possible. */
c27ba912
DM
616static int max_insns_skipped = 5;
617
618extern FILE * asm_out_file;
619
6354dc9b 620/* True if we are currently building a constant table. */
13bd191d
PB
621int making_const_table;
622
9b66ebb1
PB
623/* The processor for which instructions should be scheduled. */
624enum processor_type arm_tune = arm_none;
625
1b78f575
RE
626/* The current tuning set. */
627const struct tune_params *current_tune;
628
9b66ebb1 629/* Which floating point hardware to schedule for. */
d79f3032
PB
630int arm_fpu_attr;
631
632/* Which floating popint hardware to use. */
633const struct arm_fpu_desc *arm_fpu_desc;
9b66ebb1
PB
634
635/* Whether to use floating point hardware. */
636enum float_abi_type arm_float_abi;
637
0fd8c3ad
SL
638/* Which __fp16 format to use. */
639enum arm_fp16_format_type arm_fp16_format;
640
5848830f
PB
641/* Which ABI to use. */
642enum arm_abi_type arm_abi;
643
d3585b76
DJ
644/* Which thread pointer model to use. */
645enum arm_tp_type target_thread_pointer = TP_AUTO;
646
b355a481 647/* Used to parse -mstructure_size_boundary command line option. */
723ae7c1 648int arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
b355a481 649
b12a00f1 650/* Used for Thumb call_via trampolines. */
57ecec57 651rtx thumb_call_via_label[14];
b12a00f1
RE
652static int thumb_call_reg_needed;
653
aec3cfba 654/* Bit values used to identify processor capabilities. */
62b10bbc 655#define FL_CO_PROC (1 << 0) /* Has external co-processor bus */
9b66ebb1 656#define FL_ARCH3M (1 << 1) /* Extended multiply */
62b10bbc
NC
657#define FL_MODE26 (1 << 2) /* 26-bit mode support */
658#define FL_MODE32 (1 << 3) /* 32-bit mode support */
659#define FL_ARCH4 (1 << 4) /* Architecture rel 4 */
660#define FL_ARCH5 (1 << 5) /* Architecture rel 5 */
661#define FL_THUMB (1 << 6) /* Thumb aware */
662#define FL_LDSCHED (1 << 7) /* Load scheduling necessary */
663#define FL_STRONG (1 << 8) /* StrongARM */
6bc82793 664#define FL_ARCH5E (1 << 9) /* DSP extensions to v5 */
d19fb8e3 665#define FL_XSCALE (1 << 10) /* XScale */
9b6b54e2 666#define FL_CIRRUS (1 << 11) /* Cirrus/DSP. */
9b66ebb1 667#define FL_ARCH6 (1 << 12) /* Architecture rel 6. Adds
81f9037c
MM
668 media instructions. */
669#define FL_VFPV2 (1 << 13) /* Vector Floating Point V2. */
abac3b49
RE
670#define FL_WBUF (1 << 14) /* Schedule for write buffer ops.
671 Note: ARM6 & 7 derivatives only. */
d3585b76 672#define FL_ARCH6K (1 << 15) /* Architecture rel 6 K extensions. */
5b3e6663
PB
673#define FL_THUMB2 (1 << 16) /* Thumb-2. */
674#define FL_NOTM (1 << 17) /* Instructions not present in the 'M'
675 profile. */
7a085dce 676#define FL_DIV (1 << 18) /* Hardware divide. */
f1adb0a9 677#define FL_VFPV3 (1 << 19) /* Vector Floating Point V3. */
88f77cba 678#define FL_NEON (1 << 20) /* Neon instructions. */
60bd3528
PB
679#define FL_ARCH7EM (1 << 21) /* Instructions present in the ARMv7E-M
680 architecture. */
029e79eb 681#define FL_ARCH7 (1 << 22) /* Architecture 7. */
aec3cfba 682
9b66ebb1
PB
683#define FL_IWMMXT (1 << 29) /* XScale v2 or "Intel Wireless MMX technology". */
684
12a0a4d4
PB
685/* Flags that only effect tuning, not available instructions. */
686#define FL_TUNE (FL_WBUF | FL_VFPV2 | FL_STRONG | FL_LDSCHED \
687 | FL_CO_PROC)
688
5b3e6663
PB
689#define FL_FOR_ARCH2 FL_NOTM
690#define FL_FOR_ARCH3 (FL_FOR_ARCH2 | FL_MODE32)
78011587
PB
691#define FL_FOR_ARCH3M (FL_FOR_ARCH3 | FL_ARCH3M)
692#define FL_FOR_ARCH4 (FL_FOR_ARCH3M | FL_ARCH4)
693#define FL_FOR_ARCH4T (FL_FOR_ARCH4 | FL_THUMB)
694#define FL_FOR_ARCH5 (FL_FOR_ARCH4 | FL_ARCH5)
695#define FL_FOR_ARCH5T (FL_FOR_ARCH5 | FL_THUMB)
696#define FL_FOR_ARCH5E (FL_FOR_ARCH5 | FL_ARCH5E)
697#define FL_FOR_ARCH5TE (FL_FOR_ARCH5E | FL_THUMB)
698#define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
699#define FL_FOR_ARCH6 (FL_FOR_ARCH5TE | FL_ARCH6)
700#define FL_FOR_ARCH6J FL_FOR_ARCH6
d3585b76 701#define FL_FOR_ARCH6K (FL_FOR_ARCH6 | FL_ARCH6K)
fa91adc6 702#define FL_FOR_ARCH6Z FL_FOR_ARCH6
d3585b76 703#define FL_FOR_ARCH6ZK FL_FOR_ARCH6K
5b3e6663 704#define FL_FOR_ARCH6T2 (FL_FOR_ARCH6 | FL_THUMB2)
bf98ec6c 705#define FL_FOR_ARCH6M (FL_FOR_ARCH6 & ~FL_NOTM)
029e79eb 706#define FL_FOR_ARCH7 ((FL_FOR_ARCH6T2 & ~FL_NOTM) | FL_ARCH7)
87d05b44 707#define FL_FOR_ARCH7A (FL_FOR_ARCH7 | FL_NOTM | FL_ARCH6K)
5b3e6663
PB
708#define FL_FOR_ARCH7R (FL_FOR_ARCH7A | FL_DIV)
709#define FL_FOR_ARCH7M (FL_FOR_ARCH7 | FL_DIV)
60bd3528 710#define FL_FOR_ARCH7EM (FL_FOR_ARCH7M | FL_ARCH7EM)
78011587 711
1d6e90ac
NC
712/* The bits in this mask specify which
713 instructions we are allowed to generate. */
0977774b 714static unsigned long insn_flags = 0;
d5b7b3ae 715
aec3cfba 716/* The bits in this mask specify which instruction scheduling options should
9b66ebb1 717 be used. */
0977774b 718static unsigned long tune_flags = 0;
aec3cfba
NC
719
720/* The following are used in the arm.md file as equivalents to bits
721 in the above two flag variables. */
722
9b66ebb1
PB
723/* Nonzero if this chip supports the ARM Architecture 3M extensions. */
724int arm_arch3m = 0;
2b835d68 725
6354dc9b 726/* Nonzero if this chip supports the ARM Architecture 4 extensions. */
2b835d68
RE
727int arm_arch4 = 0;
728
68d560d4
RE
729/* Nonzero if this chip supports the ARM Architecture 4t extensions. */
730int arm_arch4t = 0;
731
6354dc9b 732/* Nonzero if this chip supports the ARM Architecture 5 extensions. */
62b10bbc
NC
733int arm_arch5 = 0;
734
b15bca31
RE
735/* Nonzero if this chip supports the ARM Architecture 5E extensions. */
736int arm_arch5e = 0;
737
9b66ebb1
PB
738/* Nonzero if this chip supports the ARM Architecture 6 extensions. */
739int arm_arch6 = 0;
740
d3585b76
DJ
741/* Nonzero if this chip supports the ARM 6K extensions. */
742int arm_arch6k = 0;
743
029e79eb
MS
744/* Nonzero if this chip supports the ARM 7 extensions. */
745int arm_arch7 = 0;
746
5b3e6663
PB
747/* Nonzero if instructions not present in the 'M' profile can be used. */
748int arm_arch_notm = 0;
749
60bd3528
PB
750/* Nonzero if instructions present in ARMv7E-M can be used. */
751int arm_arch7em = 0;
752
aec3cfba 753/* Nonzero if this chip can benefit from load scheduling. */
f5a1b0d2
NC
754int arm_ld_sched = 0;
755
756/* Nonzero if this chip is a StrongARM. */
abac3b49 757int arm_tune_strongarm = 0;
f5a1b0d2 758
78011587
PB
759/* Nonzero if this chip is a Cirrus variant. */
760int arm_arch_cirrus = 0;
761
5a9335ef
NC
762/* Nonzero if this chip supports Intel Wireless MMX technology. */
763int arm_arch_iwmmxt = 0;
764
d19fb8e3 765/* Nonzero if this chip is an XScale. */
4b3c2e48
PB
766int arm_arch_xscale = 0;
767
768/* Nonzero if tuning for XScale */
769int arm_tune_xscale = 0;
d19fb8e3 770
e0b92319 771/* Nonzero if we want to tune for stores that access the write-buffer.
c5d34bb2 772 This typically means an ARM6 or ARM7 with MMU or MPU. */
abac3b49 773int arm_tune_wbuf = 0;
b111229a 774
7612f14d
PB
775/* Nonzero if tuning for Cortex-A9. */
776int arm_tune_cortex_a9 = 0;
777
0616531f
RE
778/* Nonzero if generating Thumb instructions. */
779int thumb_code = 0;
780
906668bb
BS
781/* Nonzero if generating Thumb-1 instructions. */
782int thumb1_code = 0;
783
2ad4dcf9 784/* Nonzero if we should define __THUMB_INTERWORK__ in the
f676971a 785 preprocessor.
2ad4dcf9
RE
786 XXX This is a bit of a hack, it's intended to help work around
787 problems in GLD which doesn't understand that armv5t code is
788 interworking clean. */
789int arm_cpp_interwork = 0;
790
5b3e6663
PB
791/* Nonzero if chip supports Thumb 2. */
792int arm_arch_thumb2;
793
794/* Nonzero if chip supports integer division instruction. */
795int arm_arch_hwdiv;
796
944442bb
NF
797/* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference,
798 we must report the mode of the memory reference from
799 TARGET_PRINT_OPERAND to TARGET_PRINT_OPERAND_ADDRESS. */
f3bb6135 800enum machine_mode output_memory_reference_mode;
cce8749e 801
32de079a 802/* The register number to be used for the PIC offset register. */
020a4035 803unsigned arm_pic_register = INVALID_REGNUM;
32de079a 804
aec3cfba
NC
805/* Set to 1 after arm_reorg has started. Reset to start at the start of
806 the next function. */
4b632bf1
RE
807static int after_arm_reorg = 0;
808
12ffc7d5 809enum arm_pcs arm_pcs_default;
390b17c2 810
cce8749e
CH
811/* For an explanation of these variables, see final_prescan_insn below. */
812int arm_ccfsm_state;
5b3e6663 813/* arm_current_cc is also used for Thumb-2 cond_exec blocks. */
84ed5e79 814enum arm_cond_code arm_current_cc;
906668bb 815
cce8749e
CH
816rtx arm_target_insn;
817int arm_target_label;
5b3e6663
PB
818/* The number of conditionally executed insns, including the current insn. */
819int arm_condexec_count = 0;
820/* A bitmask specifying the patterns for the IT block.
821 Zero means do not output an IT block before this insn. */
822int arm_condexec_mask = 0;
823/* The number of bits used in arm_condexec_mask. */
824int arm_condexec_masklen = 0;
9997d19d
RE
825
826/* The condition codes of the ARM, and the inverse function. */
1d6e90ac 827static const char * const arm_condition_codes[] =
9997d19d
RE
828{
829 "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
830 "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
831};
832
37119410
BS
833/* The register numbers in sequence, for passing to arm_gen_load_multiple. */
834int arm_regs_in_sequence[] =
835{
836 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
837};
838
5b3e6663 839#define ARM_LSL_NAME (TARGET_UNIFIED_ASM ? "lsl" : "asl")
f5a1b0d2 840#define streq(string1, string2) (strcmp (string1, string2) == 0)
5b3e6663
PB
841
842#define THUMB2_WORK_REGS (0xff & ~( (1 << THUMB_HARD_FRAME_POINTER_REGNUM) \
843 | (1 << SP_REGNUM) | (1 << PC_REGNUM) \
844 | (1 << PIC_OFFSET_TABLE_REGNUM)))
2b835d68 845\f
6354dc9b 846/* Initialization code. */
2b835d68 847
2b835d68
RE
848struct processors
849{
8b60264b 850 const char *const name;
9b66ebb1 851 enum processor_type core;
78011587 852 const char *arch;
0977774b 853 const unsigned long flags;
1b78f575
RE
854 const struct tune_params *const tune;
855};
856
911de8a3
IB
857
858#define ARM_PREFETCH_NOT_BENEFICIAL 0, -1, -1
859#define ARM_PREFETCH_BENEFICIAL(prefetch_slots,l1_size,l1_line_size) \
860 prefetch_slots, \
861 l1_size, \
862 l1_line_size
863
1b78f575
RE
864const struct tune_params arm_slowmul_tune =
865{
866 arm_slowmul_rtx_costs,
b0c13111 867 NULL,
911de8a3
IB
868 3,
869 ARM_PREFETCH_NOT_BENEFICIAL
1b78f575
RE
870};
871
872const struct tune_params arm_fastmul_tune =
873{
874 arm_fastmul_rtx_costs,
b0c13111 875 NULL,
911de8a3
IB
876 1,
877 ARM_PREFETCH_NOT_BENEFICIAL
1b78f575
RE
878};
879
880const struct tune_params arm_xscale_tune =
881{
882 arm_xscale_rtx_costs,
b0c13111 883 xscale_sched_adjust_cost,
911de8a3
IB
884 2,
885 ARM_PREFETCH_NOT_BENEFICIAL
1b78f575
RE
886};
887
888const struct tune_params arm_9e_tune =
889{
890 arm_9e_rtx_costs,
b0c13111 891 NULL,
911de8a3
IB
892 1,
893 ARM_PREFETCH_NOT_BENEFICIAL
2b835d68
RE
894};
895
b0c13111
RR
896const struct tune_params arm_cortex_a9_tune =
897{
898 arm_9e_rtx_costs,
899 cortex_a9_sched_adjust_cost,
911de8a3
IB
900 1,
901 ARM_PREFETCH_BENEFICIAL(4,32,32)
b0c13111
RR
902};
903
c02a5ccb
SL
904const struct tune_params arm_fa726te_tune =
905{
906 arm_9e_rtx_costs,
907 fa726te_sched_adjust_cost,
908 1,
909 ARM_PREFETCH_NOT_BENEFICIAL
910};
911
b0c13111 912
2b835d68
RE
913/* Not all of these give usefully different compilation alternatives,
914 but there is no simple way of generalizing them. */
8b60264b 915static const struct processors all_cores[] =
f5a1b0d2
NC
916{
917 /* ARM Cores */
d98a72fd 918#define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
12a0a4d4 919 {NAME, IDENT, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, &arm_##COSTS##_tune},
9b66ebb1
PB
920#include "arm-cores.def"
921#undef ARM_CORE
78011587 922 {NULL, arm_none, NULL, 0, NULL}
f5a1b0d2
NC
923};
924
8b60264b 925static const struct processors all_architectures[] =
2b835d68 926{
f5a1b0d2 927 /* ARM Architectures */
1b78f575 928 /* We don't specify tuning costs here as it will be figured out
9b66ebb1 929 from the core. */
f676971a 930
ad7be009
JM
931#define ARM_ARCH(NAME, CORE, ARCH, FLAGS) \
932 {NAME, CORE, #ARCH, FLAGS, NULL},
933#include "arm-arches.def"
934#undef ARM_ARCH
78011587 935 {NULL, arm_none, NULL, 0 , NULL}
f5a1b0d2
NC
936};
937
f5a1b0d2 938
12a0a4d4
PB
939/* These are populated as commandline arguments are processed, or NULL
940 if not specified. */
941static const struct processors *arm_selected_arch;
942static const struct processors *arm_selected_cpu;
943static const struct processors *arm_selected_tune;
78011587 944
afc0a4ba 945/* The name of the preprocessor macro to define for this architecture. */
78011587
PB
946
947char arm_arch_name[] = "__ARM_ARCH_0UNK__";
948
56f42830 949/* Available values for -mfpu=. */
9b66ebb1 950
d79f3032
PB
951static const struct arm_fpu_desc all_fpus[] =
952{
70dd156a
RN
953 {"fpa", ARM_FP_MODEL_FPA, 0, VFP_NONE, false, false},
954 {"fpe2", ARM_FP_MODEL_FPA, 2, VFP_NONE, false, false},
955 {"fpe3", ARM_FP_MODEL_FPA, 3, VFP_NONE, false, false},
956 {"maverick", ARM_FP_MODEL_MAVERICK, 0, VFP_NONE, false, false},
d79f3032
PB
957 {"vfp", ARM_FP_MODEL_VFP, 2, VFP_REG_D16, false, false},
958 {"vfpv3", ARM_FP_MODEL_VFP, 3, VFP_REG_D32, false, false},
e0dc3601 959 {"vfpv3-fp16", ARM_FP_MODEL_VFP, 3, VFP_REG_D32, false, true},
d79f3032 960 {"vfpv3-d16", ARM_FP_MODEL_VFP, 3, VFP_REG_D16, false, false},
e0dc3601
PB
961 {"vfpv3-d16-fp16", ARM_FP_MODEL_VFP, 3, VFP_REG_D16, false, true},
962 {"vfpv3xd", ARM_FP_MODEL_VFP, 3, VFP_REG_SINGLE, false, false},
963 {"vfpv3xd-fp16", ARM_FP_MODEL_VFP, 3, VFP_REG_SINGLE, false, true},
d79f3032
PB
964 {"neon", ARM_FP_MODEL_VFP, 3, VFP_REG_D32, true , false},
965 {"neon-fp16", ARM_FP_MODEL_VFP, 3, VFP_REG_D32, true , true },
6ed126e6
PB
966 {"vfpv4", ARM_FP_MODEL_VFP, 4, VFP_REG_D32, false, true},
967 {"vfpv4-d16", ARM_FP_MODEL_VFP, 4, VFP_REG_D16, false, true},
1abed66b 968 {"fpv4-sp-d16", ARM_FP_MODEL_VFP, 4, VFP_REG_SINGLE, false, true},
6ed126e6 969 {"neon-vfpv4", ARM_FP_MODEL_VFP, 4, VFP_REG_D32, true, true},
d79f3032
PB
970 /* Compatibility aliases. */
971 {"vfp3", ARM_FP_MODEL_VFP, 3, VFP_REG_D32, false, false},
9b66ebb1
PB
972};
973
974
975struct float_abi
976{
977 const char * name;
978 enum float_abi_type abi_type;
979};
980
981
982/* Available values for -mfloat-abi=. */
983
984static const struct float_abi all_float_abis[] =
985{
986 {"soft", ARM_FLOAT_ABI_SOFT},
987 {"softfp", ARM_FLOAT_ABI_SOFTFP},
988 {"hard", ARM_FLOAT_ABI_HARD}
989};
990
991
0fd8c3ad
SL
992struct fp16_format
993{
994 const char *name;
995 enum arm_fp16_format_type fp16_format_type;
996};
997
998
999/* Available values for -mfp16-format=. */
1000
1001static const struct fp16_format all_fp16_formats[] =
1002{
1003 {"none", ARM_FP16_FORMAT_NONE},
1004 {"ieee", ARM_FP16_FORMAT_IEEE},
1005 {"alternative", ARM_FP16_FORMAT_ALTERNATIVE}
1006};
1007
1008
5848830f
PB
1009struct abi_name
1010{
1011 const char *name;
1012 enum arm_abi_type abi_type;
1013};
1014
1015
1016/* Available values for -mabi=. */
1017
1018static const struct abi_name arm_all_abis[] =
1019{
1020 {"apcs-gnu", ARM_ABI_APCS},
1021 {"atpcs", ARM_ABI_ATPCS},
1022 {"aapcs", ARM_ABI_AAPCS},
077fc835
KH
1023 {"iwmmxt", ARM_ABI_IWMMXT},
1024 {"aapcs-linux", ARM_ABI_AAPCS_LINUX}
5848830f
PB
1025};
1026
d3585b76
DJ
1027/* Supported TLS relocations. */
1028
1029enum tls_reloc {
1030 TLS_GD32,
1031 TLS_LDM32,
1032 TLS_LDO32,
1033 TLS_IE32,
1034 TLS_LE32
1035};
1036
1b78f575
RE
1037/* The maximum number of insns to be used when loading a constant. */
1038inline static int
1039arm_constant_limit (bool size_p)
1040{
1041 return size_p ? 1 : current_tune->constant_limit;
1042}
1043
d66437c5
RE
1044/* Emit an insn that's a simple single-set. Both the operands must be known
1045 to be valid. */
1046inline static rtx
1047emit_set_insn (rtx x, rtx y)
1048{
1049 return emit_insn (gen_rtx_SET (VOIDmode, x, y));
1050}
1051
0977774b
JT
1052/* Return the number of bits set in VALUE. */
1053static unsigned
e32bac5b 1054bit_count (unsigned long value)
aec3cfba 1055{
d5b7b3ae 1056 unsigned long count = 0;
f676971a 1057
aec3cfba
NC
1058 while (value)
1059 {
0977774b
JT
1060 count++;
1061 value &= value - 1; /* Clear the least-significant set bit. */
aec3cfba
NC
1062 }
1063
1064 return count;
1065}
1066
c112cf2b 1067/* Set up library functions unique to ARM. */
b3f8d95d
MM
1068
1069static void
1070arm_init_libfuncs (void)
1071{
1072 /* There are no special library functions unless we are using the
1073 ARM BPABI. */
1074 if (!TARGET_BPABI)
1075 return;
1076
1077 /* The functions below are described in Section 4 of the "Run-Time
1078 ABI for the ARM architecture", Version 1.0. */
1079
1080 /* Double-precision floating-point arithmetic. Table 2. */
1081 set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
1082 set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
1083 set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
1084 set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
1085 set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
1086
c112cf2b 1087 /* Double-precision comparisons. Table 3. */
b3f8d95d
MM
1088 set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
1089 set_optab_libfunc (ne_optab, DFmode, NULL);
1090 set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
1091 set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
1092 set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
1093 set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
1094 set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
1095
1096 /* Single-precision floating-point arithmetic. Table 4. */
1097 set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
1098 set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
1099 set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
1100 set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
1101 set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
f676971a 1102
c112cf2b 1103 /* Single-precision comparisons. Table 5. */
b3f8d95d
MM
1104 set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
1105 set_optab_libfunc (ne_optab, SFmode, NULL);
1106 set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
1107 set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
1108 set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
1109 set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
1110 set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
1111
1112 /* Floating-point to integer conversions. Table 6. */
1113 set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
1114 set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
1115 set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
1116 set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
1117 set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
1118 set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
1119 set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
1120 set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
1121
1122 /* Conversions between floating types. Table 7. */
1123 set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
1124 set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
1125
c112cf2b 1126 /* Integer to floating-point conversions. Table 8. */
b3f8d95d
MM
1127 set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
1128 set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
1129 set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
1130 set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
1131 set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
1132 set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
1133 set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
1134 set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
1135
1136 /* Long long. Table 9. */
1137 set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
1138 set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
1139 set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
1140 set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
1141 set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
1142 set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
1143 set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
1144 set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
1145
1146 /* Integer (32/32->32) division. \S 4.3.1. */
1147 set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
1148 set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
1149
1150 /* The divmod functions are designed so that they can be used for
1151 plain division, even though they return both the quotient and the
1152 remainder. The quotient is returned in the usual location (i.e.,
1153 r0 for SImode, {r0, r1} for DImode), just as would be expected
1154 for an ordinary division routine. Because the AAPCS calling
1155 conventions specify that all of { r0, r1, r2, r3 } are
1156 callee-saved registers, there is no need to tell the compiler
1157 explicitly that those registers are clobbered by these
1158 routines. */
1159 set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
1160 set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
e993ba8f
DJ
1161
1162 /* For SImode division the ABI provides div-without-mod routines,
1163 which are faster. */
1164 set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
1165 set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
01c19d47
PB
1166
1167 /* We don't have mod libcalls. Fortunately gcc knows how to use the
1168 divmod libcalls instead. */
1169 set_optab_libfunc (smod_optab, DImode, NULL);
1170 set_optab_libfunc (umod_optab, DImode, NULL);
1171 set_optab_libfunc (smod_optab, SImode, NULL);
1172 set_optab_libfunc (umod_optab, SImode, NULL);
0fd8c3ad
SL
1173
1174 /* Half-precision float operations. The compiler handles all operations
1175 with NULL libfuncs by converting the SFmode. */
1176 switch (arm_fp16_format)
1177 {
1178 case ARM_FP16_FORMAT_IEEE:
1179 case ARM_FP16_FORMAT_ALTERNATIVE:
1180
1181 /* Conversions. */
1182 set_conv_libfunc (trunc_optab, HFmode, SFmode,
1183 (arm_fp16_format == ARM_FP16_FORMAT_IEEE
1184 ? "__gnu_f2h_ieee"
1185 : "__gnu_f2h_alternative"));
1186 set_conv_libfunc (sext_optab, SFmode, HFmode,
1187 (arm_fp16_format == ARM_FP16_FORMAT_IEEE
1188 ? "__gnu_h2f_ieee"
1189 : "__gnu_h2f_alternative"));
1190
1191 /* Arithmetic. */
1192 set_optab_libfunc (add_optab, HFmode, NULL);
1193 set_optab_libfunc (sdiv_optab, HFmode, NULL);
1194 set_optab_libfunc (smul_optab, HFmode, NULL);
1195 set_optab_libfunc (neg_optab, HFmode, NULL);
1196 set_optab_libfunc (sub_optab, HFmode, NULL);
1197
1198 /* Comparisons. */
1199 set_optab_libfunc (eq_optab, HFmode, NULL);
1200 set_optab_libfunc (ne_optab, HFmode, NULL);
1201 set_optab_libfunc (lt_optab, HFmode, NULL);
1202 set_optab_libfunc (le_optab, HFmode, NULL);
1203 set_optab_libfunc (ge_optab, HFmode, NULL);
1204 set_optab_libfunc (gt_optab, HFmode, NULL);
1205 set_optab_libfunc (unord_optab, HFmode, NULL);
1206 break;
1207
1208 default:
1209 break;
1210 }
353a58f7
AH
1211
1212 if (TARGET_AAPCS_BASED)
1213 synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
b3f8d95d
MM
1214}
1215
07d8efe3
MM
1216/* On AAPCS systems, this is the "struct __va_list". */
1217static GTY(()) tree va_list_type;
1218
1219/* Return the type to use as __builtin_va_list. */
1220static tree
1221arm_build_builtin_va_list (void)
1222{
1223 tree va_list_name;
1224 tree ap_field;
1225
1226 if (!TARGET_AAPCS_BASED)
1227 return std_build_builtin_va_list ();
1228
1229 /* AAPCS \S 7.1.4 requires that va_list be a typedef for a type
1230 defined as:
1231
1232 struct __va_list
1233 {
1234 void *__ap;
1235 };
1236
1237 The C Library ABI further reinforces this definition in \S
1238 4.1.
1239
1240 We must follow this definition exactly. The structure tag
1241 name is visible in C++ mangled names, and thus forms a part
1242 of the ABI. The field name may be used by people who
1243 #include <stdarg.h>. */
1244 /* Create the type. */
1245 va_list_type = lang_hooks.types.make_type (RECORD_TYPE);
1246 /* Give it the required name. */
4c4bde29
AH
1247 va_list_name = build_decl (BUILTINS_LOCATION,
1248 TYPE_DECL,
07d8efe3
MM
1249 get_identifier ("__va_list"),
1250 va_list_type);
1251 DECL_ARTIFICIAL (va_list_name) = 1;
1252 TYPE_NAME (va_list_type) = va_list_name;
56f01f0b 1253 TYPE_STUB_DECL (va_list_type) = va_list_name;
07d8efe3 1254 /* Create the __ap field. */
4c4bde29
AH
1255 ap_field = build_decl (BUILTINS_LOCATION,
1256 FIELD_DECL,
07d8efe3
MM
1257 get_identifier ("__ap"),
1258 ptr_type_node);
1259 DECL_ARTIFICIAL (ap_field) = 1;
1260 DECL_FIELD_CONTEXT (ap_field) = va_list_type;
1261 TYPE_FIELDS (va_list_type) = ap_field;
1262 /* Compute its layout. */
1263 layout_type (va_list_type);
1264
1265 return va_list_type;
1266}
1267
1268/* Return an expression of type "void *" pointing to the next
1269 available argument in a variable-argument list. VALIST is the
1270 user-level va_list object, of type __builtin_va_list. */
1271static tree
1272arm_extract_valist_ptr (tree valist)
1273{
1274 if (TREE_TYPE (valist) == error_mark_node)
1275 return error_mark_node;
1276
1277 /* On an AAPCS target, the pointer is stored within "struct
1278 va_list". */
1279 if (TARGET_AAPCS_BASED)
1280 {
1281 tree ap_field = TYPE_FIELDS (TREE_TYPE (valist));
1282 valist = build3 (COMPONENT_REF, TREE_TYPE (ap_field),
1283 valist, ap_field, NULL_TREE);
1284 }
1285
1286 return valist;
1287}
1288
1289/* Implement TARGET_EXPAND_BUILTIN_VA_START. */
1290static void
1291arm_expand_builtin_va_start (tree valist, rtx nextarg)
1292{
1293 valist = arm_extract_valist_ptr (valist);
1294 std_expand_builtin_va_start (valist, nextarg);
1295}
1296
1297/* Implement TARGET_GIMPLIFY_VA_ARG_EXPR. */
1298static tree
ae46a823
JM
1299arm_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
1300 gimple_seq *post_p)
07d8efe3
MM
1301{
1302 valist = arm_extract_valist_ptr (valist);
1303 return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
1304}
1305
c5387660
JM
1306/* Fix up any incompatible options that the user has specified. */
1307static void
1308arm_option_override (void)
2b835d68 1309{
ed4c4348 1310 unsigned i;
9b66ebb1 1311
ad7be009
JM
1312 if (global_options_set.x_arm_arch_option)
1313 arm_selected_arch = &all_architectures[arm_arch_option];
1314
1315 if (global_options_set.x_arm_cpu_option)
1316 arm_selected_cpu = &all_cores[(int) arm_cpu_option];
1317
1318 if (global_options_set.x_arm_tune_option)
1319 arm_selected_tune = &all_cores[(int) arm_tune_option];
1320
c5387660
JM
1321#ifdef SUBTARGET_OVERRIDE_OPTIONS
1322 SUBTARGET_OVERRIDE_OPTIONS;
1323#endif
1324
12a0a4d4 1325 if (arm_selected_arch)
bd9c7e23 1326 {
12a0a4d4
PB
1327 if (arm_selected_cpu)
1328 {
1329 /* Check for conflict between mcpu and march. */
1330 if ((arm_selected_cpu->flags ^ arm_selected_arch->flags) & ~FL_TUNE)
1331 {
1332 warning (0, "switch -mcpu=%s conflicts with -march=%s switch",
1333 arm_selected_cpu->name, arm_selected_arch->name);
1334 /* -march wins for code generation.
1335 -mcpu wins for default tuning. */
1336 if (!arm_selected_tune)
1337 arm_selected_tune = arm_selected_cpu;
1338
1339 arm_selected_cpu = arm_selected_arch;
1340 }
1341 else
1342 /* -mcpu wins. */
1343 arm_selected_arch = NULL;
1344 }
1345 else
1346 /* Pick a CPU based on the architecture. */
1347 arm_selected_cpu = arm_selected_arch;
bd9c7e23 1348 }
f676971a 1349
f5a1b0d2 1350 /* If the user did not specify a processor, choose one for them. */
12a0a4d4 1351 if (!arm_selected_cpu)
f5a1b0d2 1352 {
8b60264b 1353 const struct processors * sel;
aec3cfba 1354 unsigned int sought;
aec3cfba 1355
12a0a4d4
PB
1356 arm_selected_cpu = &all_cores[TARGET_CPU_DEFAULT];
1357 if (!arm_selected_cpu->name)
78011587
PB
1358 {
1359#ifdef SUBTARGET_CPU_DEFAULT
1360 /* Use the subtarget default CPU if none was specified by
1361 configure. */
12a0a4d4 1362 arm_selected_cpu = &all_cores[SUBTARGET_CPU_DEFAULT];
78011587
PB
1363#endif
1364 /* Default to ARM6. */
fe7645b9 1365 if (!arm_selected_cpu->name)
12a0a4d4 1366 arm_selected_cpu = &all_cores[arm6];
78011587 1367 }
aec3cfba 1368
12a0a4d4 1369 sel = arm_selected_cpu;
aec3cfba 1370 insn_flags = sel->flags;
9b66ebb1 1371
aec3cfba
NC
1372 /* Now check to see if the user has specified some command line
1373 switch that require certain abilities from the cpu. */
1374 sought = 0;
f676971a 1375
d5b7b3ae 1376 if (TARGET_INTERWORK || TARGET_THUMB)
f5a1b0d2 1377 {
aec3cfba 1378 sought |= (FL_THUMB | FL_MODE32);
f676971a 1379
d5b7b3ae 1380 /* There are no ARM processors that support both APCS-26 and
aec3cfba
NC
1381 interworking. Therefore we force FL_MODE26 to be removed
1382 from insn_flags here (if it was set), so that the search
1383 below will always be able to find a compatible processor. */
5895f793 1384 insn_flags &= ~FL_MODE26;
f5a1b0d2 1385 }
f676971a 1386
aec3cfba 1387 if (sought != 0 && ((sought & insn_flags) != sought))
f5a1b0d2 1388 {
aec3cfba
NC
1389 /* Try to locate a CPU type that supports all of the abilities
1390 of the default CPU, plus the extra abilities requested by
1391 the user. */
5895f793 1392 for (sel = all_cores; sel->name != NULL; sel++)
aec3cfba 1393 if ((sel->flags & sought) == (sought | insn_flags))
f5a1b0d2
NC
1394 break;
1395
1396 if (sel->name == NULL)
aec3cfba 1397 {
0977774b 1398 unsigned current_bit_count = 0;
8b60264b 1399 const struct processors * best_fit = NULL;
f676971a 1400
aec3cfba
NC
1401 /* Ideally we would like to issue an error message here
1402 saying that it was not possible to find a CPU compatible
1403 with the default CPU, but which also supports the command
1404 line options specified by the programmer, and so they
1405 ought to use the -mcpu=<name> command line option to
1406 override the default CPU type.
1407
61f0ccff
RE
1408 If we cannot find a cpu that has both the
1409 characteristics of the default cpu and the given
1410 command line options we scan the array again looking
1411 for a best match. */
5895f793 1412 for (sel = all_cores; sel->name != NULL; sel++)
aec3cfba
NC
1413 if ((sel->flags & sought) == sought)
1414 {
0977774b 1415 unsigned count;
aec3cfba
NC
1416
1417 count = bit_count (sel->flags & insn_flags);
1418
1419 if (count >= current_bit_count)
1420 {
1421 best_fit = sel;
1422 current_bit_count = count;
1423 }
1424 }
f5a1b0d2 1425
e6d29d15
NS
1426 gcc_assert (best_fit);
1427 sel = best_fit;
aec3cfba
NC
1428 }
1429
12a0a4d4 1430 arm_selected_cpu = sel;
f5a1b0d2
NC
1431 }
1432 }
f676971a 1433
12a0a4d4
PB
1434 gcc_assert (arm_selected_cpu);
1435 /* The selected cpu may be an architecture, so lookup tuning by core ID. */
1436 if (!arm_selected_tune)
1437 arm_selected_tune = &all_cores[arm_selected_cpu->core];
1438
1439 sprintf (arm_arch_name, "__ARM_ARCH_%s__", arm_selected_cpu->arch);
1440 insn_flags = arm_selected_cpu->flags;
f676971a 1441
12a0a4d4
PB
1442 arm_tune = arm_selected_tune->core;
1443 tune_flags = arm_selected_tune->flags;
1444 current_tune = arm_selected_tune->tune;
e26053d1 1445
0fd8c3ad
SL
1446 if (target_fp16_format_name)
1447 {
1448 for (i = 0; i < ARRAY_SIZE (all_fp16_formats); i++)
1449 {
1450 if (streq (all_fp16_formats[i].name, target_fp16_format_name))
1451 {
1452 arm_fp16_format = all_fp16_formats[i].fp16_format_type;
1453 break;
1454 }
1455 }
1456 if (i == ARRAY_SIZE (all_fp16_formats))
1457 error ("invalid __fp16 format option: -mfp16-format=%s",
1458 target_fp16_format_name);
1459 }
1460 else
1461 arm_fp16_format = ARM_FP16_FORMAT_NONE;
1462
26272ba2
PB
1463 if (target_abi_name)
1464 {
1465 for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1466 {
1467 if (streq (arm_all_abis[i].name, target_abi_name))
1468 {
1469 arm_abi = arm_all_abis[i].abi_type;
1470 break;
1471 }
1472 }
1473 if (i == ARRAY_SIZE (arm_all_abis))
1474 error ("invalid ABI option: -mabi=%s", target_abi_name);
1475 }
1476 else
1477 arm_abi = ARM_DEFAULT_ABI;
1478
f5a1b0d2
NC
1479 /* Make sure that the processor choice does not conflict with any of the
1480 other command line choices. */
5b3e6663
PB
1481 if (TARGET_ARM && !(insn_flags & FL_NOTM))
1482 error ("target CPU does not support ARM mode");
1483
26272ba2
PB
1484 /* BPABI targets use linker tricks to allow interworking on cores
1485 without thumb support. */
1486 if (TARGET_INTERWORK && !((insn_flags & FL_THUMB) || TARGET_BPABI))
f5a1b0d2 1487 {
d4ee4d25 1488 warning (0, "target CPU does not support interworking" );
c54c7322 1489 target_flags &= ~MASK_INTERWORK;
f5a1b0d2 1490 }
f676971a 1491
d5b7b3ae
RE
1492 if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1493 {
d4ee4d25 1494 warning (0, "target CPU does not support THUMB instructions");
c54c7322 1495 target_flags &= ~MASK_THUMB;
d5b7b3ae
RE
1496 }
1497
1498 if (TARGET_APCS_FRAME && TARGET_THUMB)
1499 {
d4ee4d25 1500 /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
c54c7322 1501 target_flags &= ~MASK_APCS_FRAME;
d5b7b3ae 1502 }
d19fb8e3 1503
da8ce8be
RE
1504 /* Callee super interworking implies thumb interworking. Adding
1505 this to the flags here simplifies the logic elsewhere. */
1506 if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
4546a2ef 1507 target_flags |= MASK_INTERWORK;
da8ce8be 1508
d5b7b3ae
RE
1509 /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1510 from here where no function is being compiled currently. */
c54c7322 1511 if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
d4ee4d25 1512 warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
d5b7b3ae
RE
1513
1514 if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
d4ee4d25 1515 warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
d5b7b3ae 1516
5895f793 1517 if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
f5a1b0d2 1518 {
d4ee4d25 1519 warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
c54c7322 1520 target_flags |= MASK_APCS_FRAME;
f5a1b0d2 1521 }
f676971a 1522
2b835d68 1523 if (TARGET_POKE_FUNCTION_NAME)
c54c7322 1524 target_flags |= MASK_APCS_FRAME;
f676971a 1525
2b835d68 1526 if (TARGET_APCS_REENT && flag_pic)
400500c4 1527 error ("-fpic and -mapcs-reent are incompatible");
f676971a 1528
2b835d68 1529 if (TARGET_APCS_REENT)
d4ee4d25 1530 warning (0, "APCS reentrant code not supported. Ignored");
f676971a 1531
d5b7b3ae
RE
1532 /* If this target is normally configured to use APCS frames, warn if they
1533 are turned off and debugging is turned on. */
1534 if (TARGET_ARM
1535 && write_symbols != NO_DEBUG
5895f793 1536 && !TARGET_APCS_FRAME
c54c7322 1537 && (TARGET_DEFAULT & MASK_APCS_FRAME))
d4ee4d25 1538 warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
f676971a 1539
2b835d68 1540 if (TARGET_APCS_FLOAT)
d4ee4d25 1541 warning (0, "passing floating point arguments in fp regs not yet supported");
f676971a 1542
4912a07c 1543 /* Initialize boolean versions of the flags, for use in the arm.md file. */
9b66ebb1
PB
1544 arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1545 arm_arch4 = (insn_flags & FL_ARCH4) != 0;
68d560d4 1546 arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
9b66ebb1
PB
1547 arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1548 arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1549 arm_arch6 = (insn_flags & FL_ARCH6) != 0;
d3585b76 1550 arm_arch6k = (insn_flags & FL_ARCH6K) != 0;
5b3e6663 1551 arm_arch_notm = (insn_flags & FL_NOTM) != 0;
029e79eb 1552 arm_arch7 = (insn_flags & FL_ARCH7) != 0;
60bd3528 1553 arm_arch7em = (insn_flags & FL_ARCH7EM) != 0;
5b3e6663 1554 arm_arch_thumb2 = (insn_flags & FL_THUMB2) != 0;
9b66ebb1 1555 arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
78011587 1556 arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
9b66ebb1
PB
1557
1558 arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
abac3b49 1559 arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
906668bb
BS
1560 thumb_code = TARGET_ARM == 0;
1561 thumb1_code = TARGET_THUMB1 != 0;
abac3b49 1562 arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
9b66ebb1
PB
1563 arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1564 arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
5b3e6663 1565 arm_arch_hwdiv = (insn_flags & FL_DIV) != 0;
7612f14d 1566 arm_tune_cortex_a9 = (arm_tune == cortexa9) != 0;
5a9335ef 1567
f67358da
PB
1568 /* If we are not using the default (ARM mode) section anchor offset
1569 ranges, then set the correct ranges now. */
1570 if (TARGET_THUMB1)
1571 {
1572 /* Thumb-1 LDR instructions cannot have negative offsets.
1573 Permissible positive offset ranges are 5-bit (for byte loads),
1574 6-bit (for halfword loads), or 7-bit (for word loads).
1575 Empirical results suggest a 7-bit anchor range gives the best
1576 overall code size. */
1577 targetm.min_anchor_offset = 0;
1578 targetm.max_anchor_offset = 127;
1579 }
1580 else if (TARGET_THUMB2)
1581 {
1582 /* The minimum is set such that the total size of the block
1583 for a particular anchor is 248 + 1 + 4095 bytes, which is
1584 divisible by eight, ensuring natural spacing of anchors. */
1585 targetm.min_anchor_offset = -248;
1586 targetm.max_anchor_offset = 4095;
1587 }
1588
68d560d4
RE
1589 /* V5 code we generate is completely interworking capable, so we turn off
1590 TARGET_INTERWORK here to avoid many tests later on. */
2ad4dcf9
RE
1591
1592 /* XXX However, we must pass the right pre-processor defines to CPP
1593 or GLD can get confused. This is a hack. */
1594 if (TARGET_INTERWORK)
1595 arm_cpp_interwork = 1;
1596
68d560d4 1597 if (arm_arch5)
c54c7322 1598 target_flags &= ~MASK_INTERWORK;
68d560d4 1599
5848830f
PB
1600 if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1601 error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1602
1603 if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1604 error ("iwmmxt abi requires an iwmmxt capable cpu");
6f7ebcbb 1605
9b66ebb1 1606 if (target_fpu_name == NULL && target_fpe_name != NULL)
9b6b54e2 1607 {
9b66ebb1
PB
1608 if (streq (target_fpe_name, "2"))
1609 target_fpu_name = "fpe2";
1610 else if (streq (target_fpe_name, "3"))
1611 target_fpu_name = "fpe3";
1612 else
1613 error ("invalid floating point emulation option: -mfpe=%s",
1614 target_fpe_name);
1615 }
d79f3032
PB
1616
1617 if (target_fpu_name == NULL)
2b835d68 1618 {
9b66ebb1 1619#ifdef FPUTYPE_DEFAULT
d79f3032 1620 target_fpu_name = FPUTYPE_DEFAULT;
9b66ebb1 1621#else
78011587 1622 if (arm_arch_cirrus)
d79f3032 1623 target_fpu_name = "maverick";
9b66ebb1 1624 else
d79f3032 1625 target_fpu_name = "fpe2";
9b66ebb1 1626#endif
d79f3032
PB
1627 }
1628
1629 arm_fpu_desc = NULL;
1630 for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1631 {
1632 if (streq (all_fpus[i].name, target_fpu_name))
1633 {
1634 arm_fpu_desc = &all_fpus[i];
1635 break;
1636 }
1637 }
b761dbe6 1638
d79f3032 1639 if (!arm_fpu_desc)
b761dbe6
RE
1640 {
1641 error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1642 return;
1643 }
d79f3032
PB
1644
1645 switch (arm_fpu_desc->model)
1646 {
1647 case ARM_FP_MODEL_FPA:
1648 if (arm_fpu_desc->rev == 2)
1649 arm_fpu_attr = FPU_FPE2;
1650 else if (arm_fpu_desc->rev == 3)
1651 arm_fpu_attr = FPU_FPE3;
2b835d68 1652 else
d79f3032
PB
1653 arm_fpu_attr = FPU_FPA;
1654 break;
1655
1656 case ARM_FP_MODEL_MAVERICK:
1657 arm_fpu_attr = FPU_MAVERICK;
1658 break;
1659
1660 case ARM_FP_MODEL_VFP:
1661 arm_fpu_attr = FPU_VFP;
1662 break;
1663
1664 default:
1665 gcc_unreachable();
9b66ebb1
PB
1666 }
1667
1668 if (target_float_abi_name != NULL)
1669 {
1670 /* The user specified a FP ABI. */
1671 for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1672 {
1673 if (streq (all_float_abis[i].name, target_float_abi_name))
1674 {
1675 arm_float_abi = all_float_abis[i].abi_type;
1676 break;
1677 }
1678 }
1679 if (i == ARRAY_SIZE (all_float_abis))
1680 error ("invalid floating point abi: -mfloat-abi=%s",
1681 target_float_abi_name);
2b835d68 1682 }
3d8532aa
PB
1683 else
1684 arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
9b66ebb1 1685
0c48a567 1686 if (TARGET_AAPCS_BASED
d79f3032 1687 && (arm_fpu_desc->model == ARM_FP_MODEL_FPA))
0c48a567
RR
1688 error ("FPA is unsupported in the AAPCS");
1689
3ce14752 1690 if (TARGET_AAPCS_BASED)
9df5bfe4
RR
1691 {
1692 if (TARGET_CALLER_INTERWORKING)
1693 error ("AAPCS does not support -mcaller-super-interworking");
1694 else
1695 if (TARGET_CALLEE_INTERWORKING)
1696 error ("AAPCS does not support -mcallee-super-interworking");
1697 }
3ce14752 1698
87b24aaf
PB
1699 /* FPA and iWMMXt are incompatible because the insn encodings overlap.
1700 VFP and iWMMXt can theoretically coexist, but it's unlikely such silicon
1701 will ever exist. GCC makes no attempt to support this combination. */
1702 if (TARGET_IWMMXT && !TARGET_SOFT_FLOAT)
1703 sorry ("iWMMXt and hardware floating point");
1704
5b3e6663
PB
1705 /* ??? iWMMXt insn patterns need auditing for Thumb-2. */
1706 if (TARGET_THUMB2 && TARGET_IWMMXT)
1707 sorry ("Thumb-2 iWMMXt");
1708
0fd8c3ad
SL
1709 /* __fp16 support currently assumes the core has ldrh. */
1710 if (!arm_arch4 && arm_fp16_format != ARM_FP16_FORMAT_NONE)
1711 sorry ("__fp16 and no ldrh");
1712
9b66ebb1
PB
1713 /* If soft-float is specified then don't use FPU. */
1714 if (TARGET_SOFT_FLOAT)
d79f3032 1715 arm_fpu_attr = FPU_NONE;
f676971a 1716
390b17c2
RE
1717 if (TARGET_AAPCS_BASED)
1718 {
1719 if (arm_abi == ARM_ABI_IWMMXT)
1720 arm_pcs_default = ARM_PCS_AAPCS_IWMMXT;
1721 else if (arm_float_abi == ARM_FLOAT_ABI_HARD
1722 && TARGET_HARD_FLOAT
1723 && TARGET_VFP)
1724 arm_pcs_default = ARM_PCS_AAPCS_VFP;
1725 else
1726 arm_pcs_default = ARM_PCS_AAPCS;
1727 }
1728 else
1729 {
1730 if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1731 sorry ("-mfloat-abi=hard and VFP");
1732
1733 if (arm_abi == ARM_ABI_APCS)
1734 arm_pcs_default = ARM_PCS_APCS;
1735 else
1736 arm_pcs_default = ARM_PCS_ATPCS;
1737 }
1738
f5a1b0d2
NC
1739 /* For arm2/3 there is no need to do any scheduling if there is only
1740 a floating point emulator, or we are doing software floating-point. */
9b66ebb1 1741 if ((TARGET_SOFT_FLOAT
d79f3032 1742 || (TARGET_FPA && arm_fpu_desc->rev))
ed0e6530 1743 && (tune_flags & FL_MODE32) == 0)
f5a1b0d2 1744 flag_schedule_insns = flag_schedule_insns_after_reload = 0;
f676971a 1745
d3585b76
DJ
1746 if (target_thread_switch)
1747 {
1748 if (strcmp (target_thread_switch, "soft") == 0)
1749 target_thread_pointer = TP_SOFT;
1750 else if (strcmp (target_thread_switch, "auto") == 0)
1751 target_thread_pointer = TP_AUTO;
1752 else if (strcmp (target_thread_switch, "cp15") == 0)
1753 target_thread_pointer = TP_CP15;
1754 else
1755 error ("invalid thread pointer option: -mtp=%s", target_thread_switch);
1756 }
1757
1758 /* Use the cp15 method if it is available. */
1759 if (target_thread_pointer == TP_AUTO)
1760 {
87d05b44 1761 if (arm_arch6k && !TARGET_THUMB1)
d3585b76
DJ
1762 target_thread_pointer = TP_CP15;
1763 else
1764 target_thread_pointer = TP_SOFT;
1765 }
1766
5b3e6663
PB
1767 if (TARGET_HARD_TP && TARGET_THUMB1)
1768 error ("can not use -mtp=cp15 with 16-bit Thumb");
d3585b76 1769
5848830f 1770 /* Override the default structure alignment for AAPCS ABI. */
077fc835 1771 if (TARGET_AAPCS_BASED)
5848830f
PB
1772 arm_structure_size_boundary = 8;
1773
b355a481
NC
1774 if (structure_size_string != NULL)
1775 {
1776 int size = strtol (structure_size_string, NULL, 0);
5848830f
PB
1777
1778 if (size == 8 || size == 32
1779 || (ARM_DOUBLEWORD_ALIGN && size == 64))
b355a481
NC
1780 arm_structure_size_boundary = size;
1781 else
d4ee4d25 1782 warning (0, "structure size boundary can only be set to %s",
5848830f 1783 ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
b355a481 1784 }
ed0e6530 1785
9403b7f7
RS
1786 if (!TARGET_ARM && TARGET_VXWORKS_RTP && flag_pic)
1787 {
1788 error ("RTP PIC is incompatible with Thumb");
1789 flag_pic = 0;
1790 }
1791
c147eacb
PB
1792 /* If stack checking is disabled, we can use r10 as the PIC register,
1793 which keeps r9 available. The EABI specifies r9 as the PIC register. */
1794 if (flag_pic && TARGET_SINGLE_PIC_BASE)
9403b7f7
RS
1795 {
1796 if (TARGET_VXWORKS_RTP)
1797 warning (0, "RTP PIC is incompatible with -msingle-pic-base");
1798 arm_pic_register = (TARGET_APCS_STACK || TARGET_AAPCS_BASED) ? 9 : 10;
1799 }
1800
1801 if (flag_pic && TARGET_VXWORKS_RTP)
1802 arm_pic_register = 9;
c147eacb 1803
ed0e6530
PB
1804 if (arm_pic_register_string != NULL)
1805 {
5b43fed1 1806 int pic_register = decode_reg_name (arm_pic_register_string);
e26053d1 1807
5895f793 1808 if (!flag_pic)
d4ee4d25 1809 warning (0, "-mpic-register= is useless without -fpic");
ed0e6530 1810
ed0e6530 1811 /* Prevent the user from choosing an obviously stupid PIC register. */
5b43fed1
RH
1812 else if (pic_register < 0 || call_used_regs[pic_register]
1813 || pic_register == HARD_FRAME_POINTER_REGNUM
1814 || pic_register == STACK_POINTER_REGNUM
9403b7f7
RS
1815 || pic_register >= PC_REGNUM
1816 || (TARGET_VXWORKS_RTP
1817 && (unsigned int) pic_register != arm_pic_register))
c725bd79 1818 error ("unable to use '%s' for PIC register", arm_pic_register_string);
ed0e6530
PB
1819 else
1820 arm_pic_register = pic_register;
1821 }
d5b7b3ae 1822
5fd42423
PB
1823 /* Enable -mfix-cortex-m3-ldrd by default for Cortex-M3 cores. */
1824 if (fix_cm3_ldrd == 2)
1825 {
12a0a4d4 1826 if (arm_selected_cpu->core == cortexm3)
5fd42423
PB
1827 fix_cm3_ldrd = 1;
1828 else
1829 fix_cm3_ldrd = 0;
1830 }
1831
4aef21c8 1832 if (TARGET_THUMB1 && flag_schedule_insns)
d5b7b3ae
RE
1833 {
1834 /* Don't warn since it's on by default in -O2. */
1835 flag_schedule_insns = 0;
1836 }
1837
f5a1b0d2 1838 if (optimize_size)
be03ccc9 1839 {
be03ccc9 1840 /* If optimizing for size, bump the number of instructions that we
d6b4baa4 1841 are prepared to conditionally execute (even on a StrongARM). */
be03ccc9
NP
1842 max_insns_skipped = 6;
1843 }
1844 else
1845 {
be03ccc9
NP
1846 /* StrongARM has early execution of branches, so a sequence
1847 that is worth skipping is shorter. */
abac3b49 1848 if (arm_tune_strongarm)
be03ccc9
NP
1849 max_insns_skipped = 3;
1850 }
92a432f4 1851
70041f8a
RE
1852 /* Hot/Cold partitioning is not currently supported, since we can't
1853 handle literal pool placement in that case. */
1854 if (flag_reorder_blocks_and_partition)
1855 {
1856 inform (input_location,
1857 "-freorder-blocks-and-partition not supported on this architecture");
1858 flag_reorder_blocks_and_partition = 0;
1859 flag_reorder_blocks = 1;
1860 }
1861
128dc8e2 1862 if (flag_pic)
ec3728ad
MK
1863 /* Hoisting PIC address calculations more aggressively provides a small,
1864 but measurable, size reduction for PIC code. Therefore, we decrease
1865 the bar for unrestricted expression hoisting to the cost of PIC address
1866 calculation, which is 2 instructions. */
48476d13
JM
1867 maybe_set_param_value (PARAM_GCSE_UNRESTRICTED_COST, 2,
1868 global_options.x_param_values,
1869 global_options_set.x_param_values);
ec3728ad 1870
f5c88dbf
JZ
1871 /* ARM EABI defaults to strict volatile bitfields. */
1872 if (TARGET_AAPCS_BASED && flag_strict_volatile_bitfields < 0)
1873 flag_strict_volatile_bitfields = 1;
1874
911de8a3
IB
1875 /* Enable sw prefetching at -O3 for CPUS that have prefetch, and we have deemed
1876 it beneficial (signified by setting num_prefetch_slots to 1 or more.) */
1877 if (flag_prefetch_loop_arrays < 0
1878 && HAVE_prefetch
1879 && optimize >= 3
1880 && current_tune->num_prefetch_slots > 0)
1881 flag_prefetch_loop_arrays = 1;
1882
1883 /* Set up parameters to be used in prefetching algorithm. Do not override the
1884 defaults unless we are tuning for a core we have researched values for. */
1885 if (current_tune->num_prefetch_slots > 0)
1886 maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
1887 current_tune->num_prefetch_slots,
1888 global_options.x_param_values,
1889 global_options_set.x_param_values);
1890 if (current_tune->l1_cache_line_size >= 0)
1891 maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
1892 current_tune->l1_cache_line_size,
1893 global_options.x_param_values,
1894 global_options_set.x_param_values);
1895 if (current_tune->l1_cache_size >= 0)
1896 maybe_set_param_value (PARAM_L1_CACHE_SIZE,
1897 current_tune->l1_cache_size,
1898 global_options.x_param_values,
1899 global_options_set.x_param_values);
1900
92a432f4
RE
1901 /* Register global variables with the garbage collector. */
1902 arm_add_gc_roots ();
1903}
1904
1905static void
e32bac5b 1906arm_add_gc_roots (void)
92a432f4 1907{
c7319d87
RE
1908 gcc_obstack_init(&minipool_obstack);
1909 minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
2b835d68 1910}
cce8749e 1911\f
6d3d9133
NC
1912/* A table of known ARM exception types.
1913 For use with the interrupt function attribute. */
1914
1915typedef struct
1916{
8b60264b
KG
1917 const char *const arg;
1918 const unsigned long return_value;
6d3d9133
NC
1919}
1920isr_attribute_arg;
1921
8b60264b 1922static const isr_attribute_arg isr_attribute_args [] =
6d3d9133
NC
1923{
1924 { "IRQ", ARM_FT_ISR },
1925 { "irq", ARM_FT_ISR },
1926 { "FIQ", ARM_FT_FIQ },
1927 { "fiq", ARM_FT_FIQ },
1928 { "ABORT", ARM_FT_ISR },
1929 { "abort", ARM_FT_ISR },
1930 { "ABORT", ARM_FT_ISR },
1931 { "abort", ARM_FT_ISR },
1932 { "UNDEF", ARM_FT_EXCEPTION },
1933 { "undef", ARM_FT_EXCEPTION },
1934 { "SWI", ARM_FT_EXCEPTION },
1935 { "swi", ARM_FT_EXCEPTION },
1936 { NULL, ARM_FT_NORMAL }
1937};
1938
1939/* Returns the (interrupt) function type of the current
1940 function, or ARM_FT_UNKNOWN if the type cannot be determined. */
1941
1942static unsigned long
e32bac5b 1943arm_isr_value (tree argument)
6d3d9133 1944{
8b60264b 1945 const isr_attribute_arg * ptr;
1d6e90ac 1946 const char * arg;
6d3d9133 1947
5b3e6663
PB
1948 if (!arm_arch_notm)
1949 return ARM_FT_NORMAL | ARM_FT_STACKALIGN;
1950
6d3d9133
NC
1951 /* No argument - default to IRQ. */
1952 if (argument == NULL_TREE)
1953 return ARM_FT_ISR;
1954
1955 /* Get the value of the argument. */
1956 if (TREE_VALUE (argument) == NULL_TREE
1957 || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1958 return ARM_FT_UNKNOWN;
1959
1960 arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1961
1962 /* Check it against the list of known arguments. */
5a9335ef 1963 for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1d6e90ac
NC
1964 if (streq (arg, ptr->arg))
1965 return ptr->return_value;
6d3d9133 1966
05713b80 1967 /* An unrecognized interrupt type. */
6d3d9133
NC
1968 return ARM_FT_UNKNOWN;
1969}
1970
1971/* Computes the type of the current function. */
1972
1973static unsigned long
e32bac5b 1974arm_compute_func_type (void)
6d3d9133
NC
1975{
1976 unsigned long type = ARM_FT_UNKNOWN;
1977 tree a;
1978 tree attr;
f676971a 1979
e6d29d15 1980 gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
6d3d9133
NC
1981
1982 /* Decide if the current function is volatile. Such functions
1983 never return, and many memory cycles can be saved by not storing
1984 register values that will never be needed again. This optimization
1985 was added to speed up context switching in a kernel application. */
1986 if (optimize > 0
cf1955dc
PB
1987 && (TREE_NOTHROW (current_function_decl)
1988 || !(flag_unwind_tables
d5fabb58
JM
1989 || (flag_exceptions
1990 && arm_except_unwind_info (&global_options) != UI_SJLJ)))
6d3d9133
NC
1991 && TREE_THIS_VOLATILE (current_function_decl))
1992 type |= ARM_FT_VOLATILE;
f676971a 1993
6de9cd9a 1994 if (cfun->static_chain_decl != NULL)
6d3d9133
NC
1995 type |= ARM_FT_NESTED;
1996
91d231cb 1997 attr = DECL_ATTRIBUTES (current_function_decl);
f676971a 1998
6d3d9133
NC
1999 a = lookup_attribute ("naked", attr);
2000 if (a != NULL_TREE)
2001 type |= ARM_FT_NAKED;
2002
c9ca9b88
PB
2003 a = lookup_attribute ("isr", attr);
2004 if (a == NULL_TREE)
2005 a = lookup_attribute ("interrupt", attr);
f676971a 2006
c9ca9b88
PB
2007 if (a == NULL_TREE)
2008 type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
6d3d9133 2009 else
c9ca9b88 2010 type |= arm_isr_value (TREE_VALUE (a));
f676971a 2011
6d3d9133
NC
2012 return type;
2013}
2014
2015/* Returns the type of the current function. */
2016
2017unsigned long
e32bac5b 2018arm_current_func_type (void)
6d3d9133
NC
2019{
2020 if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
2021 cfun->machine->func_type = arm_compute_func_type ();
2022
2023 return cfun->machine->func_type;
2024}
007e61c2
PB
2025
2026bool
2027arm_allocate_stack_slots_for_args (void)
2028{
2029 /* Naked functions should not allocate stack slots for arguments. */
2030 return !IS_NAKED (arm_current_func_type ());
2031}
2032
0ef9304b
RH
2033\f
2034/* Output assembler code for a block containing the constant parts
2035 of a trampoline, leaving space for the variable parts.
2036
2037 On the ARM, (if r8 is the static chain regnum, and remembering that
2038 referencing pc adds an offset of 8) the trampoline looks like:
2039 ldr r8, [pc, #0]
2040 ldr pc, [pc]
2041 .word static chain value
2042 .word function's address
2043 XXX FIXME: When the trampoline returns, r8 will be clobbered. */
2044
2045static void
2046arm_asm_trampoline_template (FILE *f)
2047{
2048 if (TARGET_ARM)
2049 {
2050 asm_fprintf (f, "\tldr\t%r, [%r, #0]\n", STATIC_CHAIN_REGNUM, PC_REGNUM);
2051 asm_fprintf (f, "\tldr\t%r, [%r, #0]\n", PC_REGNUM, PC_REGNUM);
2052 }
2053 else if (TARGET_THUMB2)
2054 {
2055 /* The Thumb-2 trampoline is similar to the arm implementation.
2056 Unlike 16-bit Thumb, we enter the stub in thumb mode. */
2057 asm_fprintf (f, "\tldr.w\t%r, [%r, #4]\n",
2058 STATIC_CHAIN_REGNUM, PC_REGNUM);
2059 asm_fprintf (f, "\tldr.w\t%r, [%r, #4]\n", PC_REGNUM, PC_REGNUM);
2060 }
2061 else
2062 {
2063 ASM_OUTPUT_ALIGN (f, 2);
2064 fprintf (f, "\t.code\t16\n");
2065 fprintf (f, ".Ltrampoline_start:\n");
2066 asm_fprintf (f, "\tpush\t{r0, r1}\n");
2067 asm_fprintf (f, "\tldr\tr0, [%r, #8]\n", PC_REGNUM);
2068 asm_fprintf (f, "\tmov\t%r, r0\n", STATIC_CHAIN_REGNUM);
2069 asm_fprintf (f, "\tldr\tr0, [%r, #8]\n", PC_REGNUM);
2070 asm_fprintf (f, "\tstr\tr0, [%r, #4]\n", SP_REGNUM);
2071 asm_fprintf (f, "\tpop\t{r0, %r}\n", PC_REGNUM);
2072 }
2073 assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2074 assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2075}
2076
2077/* Emit RTL insns to initialize the variable parts of a trampoline. */
2078
2079static void
2080arm_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
2081{
2082 rtx fnaddr, mem, a_tramp;
2083
2084 emit_block_move (m_tramp, assemble_trampoline_template (),
2085 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
2086
2087 mem = adjust_address (m_tramp, SImode, TARGET_32BIT ? 8 : 12);
2088 emit_move_insn (mem, chain_value);
2089
2090 mem = adjust_address (m_tramp, SImode, TARGET_32BIT ? 12 : 16);
2091 fnaddr = XEXP (DECL_RTL (fndecl), 0);
2092 emit_move_insn (mem, fnaddr);
2093
2094 a_tramp = XEXP (m_tramp, 0);
2095 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__clear_cache"),
2096 LCT_NORMAL, VOIDmode, 2, a_tramp, Pmode,
2097 plus_constant (a_tramp, TRAMPOLINE_SIZE), Pmode);
2098}
2099
2100/* Thumb trampolines should be entered in thumb mode, so set
2101 the bottom bit of the address. */
2102
2103static rtx
2104arm_trampoline_adjust_address (rtx addr)
2105{
2106 if (TARGET_THUMB)
2107 addr = expand_simple_binop (Pmode, IOR, addr, const1_rtx,
2108 NULL, 0, OPTAB_LIB_WIDEN);
2109 return addr;
2110}
6d3d9133 2111\f
f676971a 2112/* Return 1 if it is possible to return using a single instruction.
a72d4945
RE
2113 If SIBLING is non-null, this is a test for a return before a sibling
2114 call. SIBLING is the call insn, so we can examine its register usage. */
6d3d9133 2115
ff9940b0 2116int
a72d4945 2117use_return_insn (int iscond, rtx sibling)
ff9940b0
RE
2118{
2119 int regno;
9b598fa0 2120 unsigned int func_type;
d5db54a1 2121 unsigned long saved_int_regs;
a72d4945 2122 unsigned HOST_WIDE_INT stack_adjust;
5848830f 2123 arm_stack_offsets *offsets;
ff9940b0 2124
d5b7b3ae 2125 /* Never use a return instruction before reload has run. */
6d3d9133
NC
2126 if (!reload_completed)
2127 return 0;
efc2515b 2128
9b598fa0
RE
2129 func_type = arm_current_func_type ();
2130
5b3e6663 2131 /* Naked, volatile and stack alignment functions need special
3a7731fd 2132 consideration. */
5b3e6663 2133 if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED | ARM_FT_STACKALIGN))
6d3d9133 2134 return 0;
06bea5aa 2135
a15908a4
PB
2136 /* So do interrupt functions that use the frame pointer and Thumb
2137 interrupt functions. */
2138 if (IS_INTERRUPT (func_type) && (frame_pointer_needed || TARGET_THUMB))
06bea5aa 2139 return 0;
a72d4945 2140
5848830f
PB
2141 offsets = arm_get_frame_offsets ();
2142 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
a72d4945 2143
6d3d9133 2144 /* As do variadic functions. */
38173d38 2145 if (crtl->args.pretend_args_size
3cb66fd7 2146 || cfun->machine->uses_anonymous_args
699a4925 2147 /* Or if the function calls __builtin_eh_return () */
e3b5732b 2148 || crtl->calls_eh_return
699a4925 2149 /* Or if the function calls alloca */
e3b5732b 2150 || cfun->calls_alloca
a72d4945
RE
2151 /* Or if there is a stack adjustment. However, if the stack pointer
2152 is saved on the stack, we can use a pre-incrementing stack load. */
ec6237e4
PB
2153 || !(stack_adjust == 0 || (TARGET_APCS_FRAME && frame_pointer_needed
2154 && stack_adjust == 4)))
ff9940b0
RE
2155 return 0;
2156
954954d1 2157 saved_int_regs = offsets->saved_regs_mask;
d5db54a1 2158
a72d4945
RE
2159 /* Unfortunately, the insn
2160
2161 ldmib sp, {..., sp, ...}
2162
2163 triggers a bug on most SA-110 based devices, such that the stack
2164 pointer won't be correctly restored if the instruction takes a
839a4992 2165 page fault. We work around this problem by popping r3 along with
a72d4945 2166 the other registers, since that is never slower than executing
f676971a 2167 another instruction.
a72d4945
RE
2168
2169 We test for !arm_arch5 here, because code for any architecture
2170 less than this could potentially be run on one of the buggy
2171 chips. */
5b3e6663 2172 if (stack_adjust == 4 && !arm_arch5 && TARGET_ARM)
a72d4945
RE
2173 {
2174 /* Validate that r3 is a call-clobbered register (always true in
d6b4baa4 2175 the default abi) ... */
a72d4945
RE
2176 if (!call_used_regs[3])
2177 return 0;
2178
4f5dfed0
JC
2179 /* ... that it isn't being used for a return value ... */
2180 if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
2181 return 0;
2182
2183 /* ... or for a tail-call argument ... */
a72d4945
RE
2184 if (sibling)
2185 {
e6d29d15 2186 gcc_assert (GET_CODE (sibling) == CALL_INSN);
a72d4945
RE
2187
2188 if (find_regno_fusage (sibling, USE, 3))
2189 return 0;
2190 }
2191
2192 /* ... and that there are no call-saved registers in r0-r2
2193 (always true in the default ABI). */
2194 if (saved_int_regs & 0x7)
2195 return 0;
2196 }
2197
b111229a 2198 /* Can't be done if interworking with Thumb, and any registers have been
d5db54a1 2199 stacked. */
a15908a4 2200 if (TARGET_INTERWORK && saved_int_regs != 0 && !IS_INTERRUPT(func_type))
b36ba79f 2201 return 0;
d5db54a1
RE
2202
2203 /* On StrongARM, conditional returns are expensive if they aren't
2204 taken and multiple registers have been stacked. */
abac3b49 2205 if (iscond && arm_tune_strongarm)
6ed30148 2206 {
f676971a 2207 /* Conditional return when just the LR is stored is a simple
d5db54a1
RE
2208 conditional-load instruction, that's not expensive. */
2209 if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
2210 return 0;
6ed30148 2211
020a4035
RE
2212 if (flag_pic
2213 && arm_pic_register != INVALID_REGNUM
6fb5fa3c 2214 && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
b111229a 2215 return 0;
6ed30148 2216 }
d5db54a1
RE
2217
2218 /* If there are saved registers but the LR isn't saved, then we need
2219 two instructions for the return. */
2220 if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
2221 return 0;
2222
3b684012 2223 /* Can't be done if any of the FPA regs are pushed,
6d3d9133 2224 since this also requires an insn. */
9b66ebb1
PB
2225 if (TARGET_HARD_FLOAT && TARGET_FPA)
2226 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
6fb5fa3c 2227 if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
9b66ebb1
PB
2228 return 0;
2229
2230 /* Likewise VFP regs. */
2231 if (TARGET_HARD_FLOAT && TARGET_VFP)
2232 for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
6fb5fa3c 2233 if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
d5b7b3ae 2234 return 0;
ff9940b0 2235
5a9335ef
NC
2236 if (TARGET_REALLY_IWMMXT)
2237 for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
6fb5fa3c 2238 if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
5a9335ef
NC
2239 return 0;
2240
ff9940b0
RE
2241 return 1;
2242}
2243
cce8749e
CH
2244/* Return TRUE if int I is a valid immediate ARM constant. */
2245
2246int
e32bac5b 2247const_ok_for_arm (HOST_WIDE_INT i)
cce8749e 2248{
4642ccb1 2249 int lowbit;
e0b92319 2250
f676971a 2251 /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
56636818 2252 be all zero, or all one. */
30cf4896
KG
2253 if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
2254 && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
2255 != ((~(unsigned HOST_WIDE_INT) 0)
2256 & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
56636818 2257 return FALSE;
f676971a 2258
4642ccb1 2259 i &= (unsigned HOST_WIDE_INT) 0xffffffff;
e0b92319 2260
4642ccb1
RE
2261 /* Fast return for 0 and small values. We must do this for zero, since
2262 the code below can't handle that one case. */
2263 if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
e2c671ba
RE
2264 return TRUE;
2265
5b3e6663
PB
2266 /* Get the number of trailing zeros. */
2267 lowbit = ffs((int) i) - 1;
2268
2269 /* Only even shifts are allowed in ARM mode so round down to the
2270 nearest even number. */
2271 if (TARGET_ARM)
2272 lowbit &= ~1;
4642ccb1
RE
2273
2274 if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
2275 return TRUE;
5b3e6663
PB
2276
2277 if (TARGET_ARM)
2278 {
2279 /* Allow rotated constants in ARM mode. */
2280 if (lowbit <= 4
4642ccb1
RE
2281 && ((i & ~0xc000003f) == 0
2282 || (i & ~0xf000000f) == 0
2283 || (i & ~0xfc000003) == 0))
5b3e6663
PB
2284 return TRUE;
2285 }
2286 else
2287 {
2288 HOST_WIDE_INT v;
2289
d724c8f0 2290 /* Allow repeated patterns 0x00XY00XY or 0xXYXYXYXY. */
5b3e6663
PB
2291 v = i & 0xff;
2292 v |= v << 16;
2293 if (i == v || i == (v | (v << 8)))
2294 return TRUE;
d724c8f0
AS
2295
2296 /* Allow repeated pattern 0xXY00XY00. */
2297 v = i & 0xff00;
2298 v |= v << 16;
2299 if (i == v)
2300 return TRUE;
5b3e6663 2301 }
cce8749e 2302
f3bb6135
RE
2303 return FALSE;
2304}
cce8749e 2305
6354dc9b 2306/* Return true if I is a valid constant for the operation CODE. */
74bbc178 2307static int
e32bac5b 2308const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
e2c671ba
RE
2309{
2310 if (const_ok_for_arm (i))
2311 return 1;
2312
2313 switch (code)
2314 {
bc2c1a60
AS
2315 case SET:
2316 /* See if we can use movw. */
2317 if (arm_arch_thumb2 && (i & 0xffff0000) == 0)
2318 return 1;
2319 else
2320 return 0;
2321
e2c671ba 2322 case PLUS:
d5a0a47b
RE
2323 case COMPARE:
2324 case EQ:
2325 case NE:
2326 case GT:
2327 case LE:
2328 case LT:
2329 case GE:
2330 case GEU:
2331 case LTU:
2332 case GTU:
2333 case LEU:
2334 case UNORDERED:
2335 case ORDERED:
2336 case UNEQ:
2337 case UNGE:
2338 case UNLT:
2339 case UNGT:
2340 case UNLE:
e2c671ba
RE
2341 return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
2342
2343 case MINUS: /* Should only occur with (MINUS I reg) => rsb */
2344 case XOR:
a7994a57
RR
2345 return 0;
2346
e2c671ba 2347 case IOR:
a7994a57
RR
2348 if (TARGET_THUMB2)
2349 return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
e2c671ba
RE
2350 return 0;
2351
2352 case AND:
2353 return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2354
2355 default:
e6d29d15 2356 gcc_unreachable ();
e2c671ba
RE
2357 }
2358}
2359
2360/* Emit a sequence of insns to handle a large constant.
2361 CODE is the code of the operation required, it can be any of SET, PLUS,
2362 IOR, AND, XOR, MINUS;
2363 MODE is the mode in which the operation is being performed;
2364 VAL is the integer to operate on;
2365 SOURCE is the other operand (a register, or a null-pointer for SET);
2366 SUBTARGETS means it is safe to create scratch registers if that will
2b835d68
RE
2367 either produce a simpler sequence, or we will want to cse the values.
2368 Return value is the number of insns emitted. */
e2c671ba 2369
5b3e6663 2370/* ??? Tweak this for thumb2. */
e2c671ba 2371int
a406f566 2372arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
e32bac5b 2373 HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
2b835d68 2374{
a406f566
MM
2375 rtx cond;
2376
2377 if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
2378 cond = COND_EXEC_TEST (PATTERN (insn));
2379 else
2380 cond = NULL_RTX;
2381
2b835d68
RE
2382 if (subtargets || code == SET
2383 || (GET_CODE (target) == REG && GET_CODE (source) == REG
2384 && REGNO (target) != REGNO (source)))
2385 {
4b632bf1 2386 /* After arm_reorg has been called, we can't fix up expensive
05713b80 2387 constants by pushing them into memory so we must synthesize
4b632bf1
RE
2388 them in-line, regardless of the cost. This is only likely to
2389 be more costly on chips that have load delay slots and we are
2390 compiling without running the scheduler (so no splitting
aec3cfba
NC
2391 occurred before the final instruction emission).
2392
2393 Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
aec3cfba 2394 */
5895f793 2395 if (!after_arm_reorg
a406f566 2396 && !cond
f676971a 2397 && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
a406f566 2398 1, 0)
1b78f575
RE
2399 > (arm_constant_limit (optimize_function_for_size_p (cfun))
2400 + (code != SET))))
2b835d68
RE
2401 {
2402 if (code == SET)
2403 {
2404 /* Currently SET is the only monadic value for CODE, all
2405 the rest are diadic. */
571191af
PB
2406 if (TARGET_USE_MOVT)
2407 arm_emit_movpair (target, GEN_INT (val));
2408 else
2409 emit_set_insn (target, GEN_INT (val));
2410
2b835d68
RE
2411 return 1;
2412 }
2413 else
2414 {
2415 rtx temp = subtargets ? gen_reg_rtx (mode) : target;
2416
571191af
PB
2417 if (TARGET_USE_MOVT)
2418 arm_emit_movpair (temp, GEN_INT (val));
2419 else
2420 emit_set_insn (temp, GEN_INT (val));
2421
2b835d68
RE
2422 /* For MINUS, the value is subtracted from, since we never
2423 have subtraction of a constant. */
2424 if (code == MINUS)
d66437c5 2425 emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
2b835d68 2426 else
d66437c5
RE
2427 emit_set_insn (target,
2428 gen_rtx_fmt_ee (code, mode, source, temp));
2b835d68
RE
2429 return 2;
2430 }
2431 }
2432 }
2433
f676971a 2434 return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
a406f566 2435 1);
2b835d68
RE
2436}
2437
162e4591
RE
2438/* Return the number of instructions required to synthesize the given
2439 constant, if we start emitting them from bit-position I. */
ceebdb09 2440static int
e32bac5b 2441count_insns_for_constant (HOST_WIDE_INT remainder, int i)
ceebdb09
PB
2442{
2443 HOST_WIDE_INT temp1;
162e4591 2444 int step_size = TARGET_ARM ? 2 : 1;
ceebdb09 2445 int num_insns = 0;
162e4591
RE
2446
2447 gcc_assert (TARGET_ARM || i == 0);
2448
ceebdb09
PB
2449 do
2450 {
2451 int end;
f676971a 2452
ceebdb09
PB
2453 if (i <= 0)
2454 i += 32;
162e4591 2455 if (remainder & (((1 << step_size) - 1) << (i - step_size)))
ceebdb09
PB
2456 {
2457 end = i - 8;
2458 if (end < 0)
2459 end += 32;
2460 temp1 = remainder & ((0x0ff << end)
2461 | ((i < end) ? (0xff >> (32 - end)) : 0));
2462 remainder &= ~temp1;
2463 num_insns++;
162e4591 2464 i -= 8 - step_size;
ceebdb09 2465 }
162e4591 2466 i -= step_size;
ceebdb09
PB
2467 } while (remainder);
2468 return num_insns;
2469}
2470
162e4591 2471static int
90e77553 2472find_best_start (unsigned HOST_WIDE_INT remainder)
162e4591
RE
2473{
2474 int best_consecutive_zeros = 0;
2475 int i;
2476 int best_start = 0;
2477
2478 /* If we aren't targetting ARM, the best place to start is always at
2479 the bottom. */
2480 if (! TARGET_ARM)
2481 return 0;
2482
2483 for (i = 0; i < 32; i += 2)
2484 {
2485 int consecutive_zeros = 0;
2486
2487 if (!(remainder & (3 << i)))
2488 {
2489 while ((i < 32) && !(remainder & (3 << i)))
2490 {
2491 consecutive_zeros += 2;
2492 i += 2;
2493 }
2494 if (consecutive_zeros > best_consecutive_zeros)
2495 {
2496 best_consecutive_zeros = consecutive_zeros;
2497 best_start = i - consecutive_zeros;
2498 }
2499 i -= 2;
2500 }
2501 }
2502
2503 /* So long as it won't require any more insns to do so, it's
2504 desirable to emit a small constant (in bits 0...9) in the last
2505 insn. This way there is more chance that it can be combined with
2506 a later addressing insn to form a pre-indexed load or store
2507 operation. Consider:
2508
2509 *((volatile int *)0xe0000100) = 1;
2510 *((volatile int *)0xe0000110) = 2;
2511
2512 We want this to wind up as:
2513
2514 mov rA, #0xe0000000
2515 mov rB, #1
2516 str rB, [rA, #0x100]
2517 mov rB, #2
2518 str rB, [rA, #0x110]
2519
2520 rather than having to synthesize both large constants from scratch.
2521
2522 Therefore, we calculate how many insns would be required to emit
2523 the constant starting from `best_start', and also starting from
2524 zero (i.e. with bit 31 first to be output). If `best_start' doesn't
2525 yield a shorter sequence, we may as well use zero. */
2526 if (best_start != 0
2527 && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2528 && (count_insns_for_constant (remainder, 0) <=
2529 count_insns_for_constant (remainder, best_start)))
2530 best_start = 0;
2531
2532 return best_start;
2533}
2534
a406f566
MM
2535/* Emit an instruction with the indicated PATTERN. If COND is
2536 non-NULL, conditionalize the execution of the instruction on COND
2537 being true. */
2538
2539static void
2540emit_constant_insn (rtx cond, rtx pattern)
2541{
2542 if (cond)
2543 pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
2544 emit_insn (pattern);
2545}
2546
2b835d68
RE
2547/* As above, but extra parameter GENERATE which, if clear, suppresses
2548 RTL generation. */
5b3e6663 2549/* ??? This needs more work for thumb2. */
1d6e90ac 2550
d5b7b3ae 2551static int
a406f566 2552arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
e32bac5b
RE
2553 HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
2554 int generate)
e2c671ba 2555{
e2c671ba
RE
2556 int can_invert = 0;
2557 int can_negate = 0;
162e4591 2558 int final_invert = 0;
e2c671ba 2559 int can_negate_initial = 0;
e2c671ba
RE
2560 int i;
2561 int num_bits_set = 0;
2562 int set_sign_bit_copies = 0;
2563 int clear_sign_bit_copies = 0;
2564 int clear_zero_bit_copies = 0;
2565 int set_zero_bit_copies = 0;
2566 int insns = 0;
e2c671ba 2567 unsigned HOST_WIDE_INT temp1, temp2;
30cf4896 2568 unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
162e4591 2569 int step_size = TARGET_ARM ? 2 : 1;
e2c671ba 2570
d5b7b3ae 2571 /* Find out which operations are safe for a given CODE. Also do a quick
e2c671ba
RE
2572 check for degenerate cases; these can occur when DImode operations
2573 are split. */
2574 switch (code)
2575 {
2576 case SET:
2577 can_invert = 1;
e2c671ba
RE
2578 can_negate = 1;
2579 break;
2580
2581 case PLUS:
2582 can_negate = 1;
2583 can_negate_initial = 1;
2584 break;
2585
2586 case IOR:
30cf4896 2587 if (remainder == 0xffffffff)
e2c671ba 2588 {
2b835d68 2589 if (generate)
a406f566
MM
2590 emit_constant_insn (cond,
2591 gen_rtx_SET (VOIDmode, target,
2592 GEN_INT (ARM_SIGN_EXTEND (val))));
e2c671ba
RE
2593 return 1;
2594 }
a7994a57 2595
e2c671ba
RE
2596 if (remainder == 0)
2597 {
2598 if (reload_completed && rtx_equal_p (target, source))
2599 return 0;
a7994a57 2600
2b835d68 2601 if (generate)
a406f566
MM
2602 emit_constant_insn (cond,
2603 gen_rtx_SET (VOIDmode, target, source));
e2c671ba
RE
2604 return 1;
2605 }
2606 break;
2607
2608 case AND:
2609 if (remainder == 0)
2610 {
2b835d68 2611 if (generate)
a406f566
MM
2612 emit_constant_insn (cond,
2613 gen_rtx_SET (VOIDmode, target, const0_rtx));
e2c671ba
RE
2614 return 1;
2615 }
30cf4896 2616 if (remainder == 0xffffffff)
e2c671ba
RE
2617 {
2618 if (reload_completed && rtx_equal_p (target, source))
2619 return 0;
2b835d68 2620 if (generate)
a406f566
MM
2621 emit_constant_insn (cond,
2622 gen_rtx_SET (VOIDmode, target, source));
e2c671ba
RE
2623 return 1;
2624 }
2625 can_invert = 1;
2626 break;
2627
2628 case XOR:
2629 if (remainder == 0)
2630 {
2631 if (reload_completed && rtx_equal_p (target, source))
2632 return 0;
2b835d68 2633 if (generate)
a406f566
MM
2634 emit_constant_insn (cond,
2635 gen_rtx_SET (VOIDmode, target, source));
e2c671ba
RE
2636 return 1;
2637 }
e0b92319 2638
162e4591
RE
2639 if (remainder == 0xffffffff)
2640 {
2641 if (generate)
2642 emit_constant_insn (cond,
2643 gen_rtx_SET (VOIDmode, target,
2644 gen_rtx_NOT (mode, source)));
2645 return 1;
2646 }
2647 break;
e2c671ba
RE
2648
2649 case MINUS:
2650 /* We treat MINUS as (val - source), since (source - val) is always
2651 passed as (source + (-val)). */
2652 if (remainder == 0)
2653 {
2b835d68 2654 if (generate)
a406f566
MM
2655 emit_constant_insn (cond,
2656 gen_rtx_SET (VOIDmode, target,
2657 gen_rtx_NEG (mode, source)));
e2c671ba
RE
2658 return 1;
2659 }
2660 if (const_ok_for_arm (val))
2661 {
2b835d68 2662 if (generate)
a406f566 2663 emit_constant_insn (cond,
f676971a 2664 gen_rtx_SET (VOIDmode, target,
a406f566
MM
2665 gen_rtx_MINUS (mode, GEN_INT (val),
2666 source)));
e2c671ba
RE
2667 return 1;
2668 }
2669 can_negate = 1;
2670
2671 break;
2672
2673 default:
e6d29d15 2674 gcc_unreachable ();
e2c671ba
RE
2675 }
2676
6354dc9b 2677 /* If we can do it in one insn get out quickly. */
bc2c1a60 2678 if (const_ok_for_op (val, code))
e2c671ba 2679 {
2b835d68 2680 if (generate)
a406f566
MM
2681 emit_constant_insn (cond,
2682 gen_rtx_SET (VOIDmode, target,
f676971a 2683 (source
a406f566
MM
2684 ? gen_rtx_fmt_ee (code, mode, source,
2685 GEN_INT (val))
2686 : GEN_INT (val))));
e2c671ba
RE
2687 return 1;
2688 }
2689
e2c671ba 2690 /* Calculate a few attributes that may be useful for specific
6354dc9b 2691 optimizations. */
a7994a57 2692 /* Count number of leading zeros. */
e2c671ba
RE
2693 for (i = 31; i >= 0; i--)
2694 {
2695 if ((remainder & (1 << i)) == 0)
2696 clear_sign_bit_copies++;
2697 else
2698 break;
2699 }
2700
a7994a57 2701 /* Count number of leading 1's. */
e2c671ba
RE
2702 for (i = 31; i >= 0; i--)
2703 {
2704 if ((remainder & (1 << i)) != 0)
2705 set_sign_bit_copies++;
2706 else
2707 break;
2708 }
2709
a7994a57 2710 /* Count number of trailing zero's. */
e2c671ba
RE
2711 for (i = 0; i <= 31; i++)
2712 {
2713 if ((remainder & (1 << i)) == 0)
2714 clear_zero_bit_copies++;
2715 else
2716 break;
2717 }
2718
a7994a57 2719 /* Count number of trailing 1's. */
e2c671ba
RE
2720 for (i = 0; i <= 31; i++)
2721 {
2722 if ((remainder & (1 << i)) != 0)
2723 set_zero_bit_copies++;
2724 else
2725 break;
2726 }
2727
2728 switch (code)
2729 {
2730 case SET:
2731 /* See if we can do this by sign_extending a constant that is known
2732 to be negative. This is a good, way of doing it, since the shift
2733 may well merge into a subsequent insn. */
2734 if (set_sign_bit_copies > 1)
2735 {
2736 if (const_ok_for_arm
f676971a 2737 (temp1 = ARM_SIGN_EXTEND (remainder
e2c671ba
RE
2738 << (set_sign_bit_copies - 1))))
2739 {
2b835d68
RE
2740 if (generate)
2741 {
d499463f 2742 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
a406f566 2743 emit_constant_insn (cond,
f676971a 2744 gen_rtx_SET (VOIDmode, new_src,
a406f566
MM
2745 GEN_INT (temp1)));
2746 emit_constant_insn (cond,
f676971a 2747 gen_ashrsi3 (target, new_src,
a406f566 2748 GEN_INT (set_sign_bit_copies - 1)));
2b835d68 2749 }
e2c671ba
RE
2750 return 2;
2751 }
2752 /* For an inverted constant, we will need to set the low bits,
2753 these will be shifted out of harm's way. */
2754 temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
2755 if (const_ok_for_arm (~temp1))
2756 {
2b835d68
RE
2757 if (generate)
2758 {
d499463f 2759 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
a406f566
MM
2760 emit_constant_insn (cond,
2761 gen_rtx_SET (VOIDmode, new_src,
2762 GEN_INT (temp1)));
2763 emit_constant_insn (cond,
f676971a 2764 gen_ashrsi3 (target, new_src,
a406f566 2765 GEN_INT (set_sign_bit_copies - 1)));
2b835d68 2766 }
e2c671ba
RE
2767 return 2;
2768 }
2769 }
2770
c87e6352
RE
2771 /* See if we can calculate the value as the difference between two
2772 valid immediates. */
2773 if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
2774 {
2775 int topshift = clear_sign_bit_copies & ~1;
2776
fa2c88a0
RE
2777 temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
2778 & (0xff000000 >> topshift));
c87e6352
RE
2779
2780 /* If temp1 is zero, then that means the 9 most significant
2781 bits of remainder were 1 and we've caused it to overflow.
2782 When topshift is 0 we don't need to do anything since we
2783 can borrow from 'bit 32'. */
2784 if (temp1 == 0 && topshift != 0)
2785 temp1 = 0x80000000 >> (topshift - 1);
2786
fa2c88a0 2787 temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
e0b92319 2788
c87e6352
RE
2789 if (const_ok_for_arm (temp2))
2790 {
2791 if (generate)
2792 {
2793 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2794 emit_constant_insn (cond,
2795 gen_rtx_SET (VOIDmode, new_src,
2796 GEN_INT (temp1)));
2797 emit_constant_insn (cond,
2798 gen_addsi3 (target, new_src,
2799 GEN_INT (-temp2)));
2800 }
2801
2802 return 2;
2803 }
2804 }
2805
e2c671ba
RE
2806 /* See if we can generate this by setting the bottom (or the top)
2807 16 bits, and then shifting these into the other half of the
2808 word. We only look for the simplest cases, to do more would cost
2809 too much. Be careful, however, not to generate this when the
2810 alternative would take fewer insns. */
30cf4896 2811 if (val & 0xffff0000)
e2c671ba 2812 {
30cf4896 2813 temp1 = remainder & 0xffff0000;
e2c671ba
RE
2814 temp2 = remainder & 0x0000ffff;
2815
6354dc9b 2816 /* Overlaps outside this range are best done using other methods. */
e2c671ba
RE
2817 for (i = 9; i < 24; i++)
2818 {
30cf4896 2819 if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
5895f793 2820 && !const_ok_for_arm (temp2))
e2c671ba 2821 {
d499463f
RE
2822 rtx new_src = (subtargets
2823 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2824 : target);
a406f566 2825 insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2b835d68 2826 source, subtargets, generate);
e2c671ba 2827 source = new_src;
2b835d68 2828 if (generate)
f676971a 2829 emit_constant_insn
a406f566
MM
2830 (cond,
2831 gen_rtx_SET
2832 (VOIDmode, target,
2833 gen_rtx_IOR (mode,
2834 gen_rtx_ASHIFT (mode, source,
2835 GEN_INT (i)),
2836 source)));
e2c671ba
RE
2837 return insns + 1;
2838 }
2839 }
2840
6354dc9b 2841 /* Don't duplicate cases already considered. */
e2c671ba
RE
2842 for (i = 17; i < 24; i++)
2843 {
2844 if (((temp1 | (temp1 >> i)) == remainder)
5895f793 2845 && !const_ok_for_arm (temp1))
e2c671ba 2846 {
d499463f
RE
2847 rtx new_src = (subtargets
2848 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2849 : target);
a406f566 2850 insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2b835d68 2851 source, subtargets, generate);
e2c671ba 2852 source = new_src;
2b835d68 2853 if (generate)
a406f566
MM
2854 emit_constant_insn
2855 (cond,
2856 gen_rtx_SET (VOIDmode, target,
43cffd11
RE
2857 gen_rtx_IOR
2858 (mode,
2859 gen_rtx_LSHIFTRT (mode, source,
2860 GEN_INT (i)),
2861 source)));
e2c671ba
RE
2862 return insns + 1;
2863 }
2864 }
2865 }
2866 break;
2867
2868 case IOR:
2869 case XOR:
7b64da89
RE
2870 /* If we have IOR or XOR, and the constant can be loaded in a
2871 single instruction, and we can find a temporary to put it in,
e2c671ba
RE
2872 then this can be done in two instructions instead of 3-4. */
2873 if (subtargets
d499463f 2874 /* TARGET can't be NULL if SUBTARGETS is 0 */
5895f793 2875 || (reload_completed && !reg_mentioned_p (target, source)))
e2c671ba 2876 {
5895f793 2877 if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
e2c671ba 2878 {
2b835d68
RE
2879 if (generate)
2880 {
2881 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
e2c671ba 2882
a406f566 2883 emit_constant_insn (cond,
f676971a 2884 gen_rtx_SET (VOIDmode, sub,
a406f566
MM
2885 GEN_INT (val)));
2886 emit_constant_insn (cond,
f676971a 2887 gen_rtx_SET (VOIDmode, target,
a406f566
MM
2888 gen_rtx_fmt_ee (code, mode,
2889 source, sub)));
2b835d68 2890 }
e2c671ba
RE
2891 return 2;
2892 }
2893 }
2894
2895 if (code == XOR)
2896 break;
2897
a7994a57
RR
2898 /* Convert.
2899 x = y | constant ( which is composed of set_sign_bit_copies of leading 1s
2900 and the remainder 0s for e.g. 0xfff00000)
2901 x = ~(~(y ashift set_sign_bit_copies) lshiftrt set_sign_bit_copies)
2902
2903 This can be done in 2 instructions by using shifts with mov or mvn.
2904 e.g. for
2905 x = x | 0xfff00000;
2906 we generate.
2907 mvn r0, r0, asl #12
2908 mvn r0, r0, lsr #12 */
e2c671ba
RE
2909 if (set_sign_bit_copies > 8
2910 && (val & (-1 << (32 - set_sign_bit_copies))) == val)
2911 {
2b835d68
RE
2912 if (generate)
2913 {
2914 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2915 rtx shift = GEN_INT (set_sign_bit_copies);
2916
f676971a 2917 emit_constant_insn
a406f566
MM
2918 (cond,
2919 gen_rtx_SET (VOIDmode, sub,
f676971a 2920 gen_rtx_NOT (mode,
a406f566 2921 gen_rtx_ASHIFT (mode,
f676971a 2922 source,
a406f566 2923 shift))));
f676971a 2924 emit_constant_insn
a406f566
MM
2925 (cond,
2926 gen_rtx_SET (VOIDmode, target,
2927 gen_rtx_NOT (mode,
2928 gen_rtx_LSHIFTRT (mode, sub,
2929 shift))));
2b835d68 2930 }
e2c671ba
RE
2931 return 2;
2932 }
2933
a7994a57
RR
2934 /* Convert
2935 x = y | constant (which has set_zero_bit_copies number of trailing ones).
2936 to
2937 x = ~((~y lshiftrt set_zero_bit_copies) ashift set_zero_bit_copies).
2938
2939 For eg. r0 = r0 | 0xfff
2940 mvn r0, r0, lsr #12
2941 mvn r0, r0, asl #12
2942
2943 */
e2c671ba
RE
2944 if (set_zero_bit_copies > 8
2945 && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2946 {
2b835d68
RE
2947 if (generate)
2948 {
2949 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2950 rtx shift = GEN_INT (set_zero_bit_copies);
2951
a406f566
MM
2952 emit_constant_insn
2953 (cond,
2954 gen_rtx_SET (VOIDmode, sub,
2955 gen_rtx_NOT (mode,
2956 gen_rtx_LSHIFTRT (mode,
2957 source,
2958 shift))));
f676971a 2959 emit_constant_insn
a406f566
MM
2960 (cond,
2961 gen_rtx_SET (VOIDmode, target,
2962 gen_rtx_NOT (mode,
2963 gen_rtx_ASHIFT (mode, sub,
2964 shift))));
2b835d68 2965 }
e2c671ba
RE
2966 return 2;
2967 }
2968
a7994a57
RR
2969 /* This will never be reached for Thumb2 because orn is a valid
2970 instruction. This is for Thumb1 and the ARM 32 bit cases.
2971
2972 x = y | constant (such that ~constant is a valid constant)
2973 Transform this to
2974 x = ~(~y & ~constant).
2975 */
5895f793 2976 if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
e2c671ba 2977 {
2b835d68
RE
2978 if (generate)
2979 {
2980 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
a406f566
MM
2981 emit_constant_insn (cond,
2982 gen_rtx_SET (VOIDmode, sub,
2983 gen_rtx_NOT (mode, source)));
2b835d68
RE
2984 source = sub;
2985 if (subtargets)
2986 sub = gen_reg_rtx (mode);
a406f566
MM
2987 emit_constant_insn (cond,
2988 gen_rtx_SET (VOIDmode, sub,
f676971a 2989 gen_rtx_AND (mode, source,
a406f566
MM
2990 GEN_INT (temp1))));
2991 emit_constant_insn (cond,
2992 gen_rtx_SET (VOIDmode, target,
2993 gen_rtx_NOT (mode, sub)));
2b835d68 2994 }
e2c671ba
RE
2995 return 3;
2996 }
2997 break;
2998
2999 case AND:
3000 /* See if two shifts will do 2 or more insn's worth of work. */
3001 if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
3002 {
30cf4896 3003 HOST_WIDE_INT shift_mask = ((0xffffffff
e2c671ba 3004 << (32 - clear_sign_bit_copies))
30cf4896 3005 & 0xffffffff);
e2c671ba 3006
30cf4896 3007 if ((remainder | shift_mask) != 0xffffffff)
e2c671ba 3008 {
2b835d68
RE
3009 if (generate)
3010 {
d499463f 3011 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
f676971a 3012 insns = arm_gen_constant (AND, mode, cond,
a406f566 3013 remainder | shift_mask,
d499463f
RE
3014 new_src, source, subtargets, 1);
3015 source = new_src;
2b835d68
RE
3016 }
3017 else
d499463f
RE
3018 {
3019 rtx targ = subtargets ? NULL_RTX : target;
a406f566
MM
3020 insns = arm_gen_constant (AND, mode, cond,
3021 remainder | shift_mask,
d499463f
RE
3022 targ, source, subtargets, 0);
3023 }
2b835d68
RE
3024 }
3025
3026 if (generate)
3027 {
d499463f
RE
3028 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3029 rtx shift = GEN_INT (clear_sign_bit_copies);
3030
3031 emit_insn (gen_ashlsi3 (new_src, source, shift));
3032 emit_insn (gen_lshrsi3 (target, new_src, shift));
e2c671ba
RE
3033 }
3034
e2c671ba
RE
3035 return insns + 2;
3036 }
3037
3038 if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
3039 {
3040 HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
f676971a 3041
30cf4896 3042 if ((remainder | shift_mask) != 0xffffffff)
e2c671ba 3043 {
2b835d68
RE
3044 if (generate)
3045 {
d499463f
RE
3046 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3047
a406f566
MM
3048 insns = arm_gen_constant (AND, mode, cond,
3049 remainder | shift_mask,
d499463f
RE
3050 new_src, source, subtargets, 1);
3051 source = new_src;
2b835d68
RE
3052 }
3053 else
d499463f
RE
3054 {
3055 rtx targ = subtargets ? NULL_RTX : target;
3056
a406f566
MM
3057 insns = arm_gen_constant (AND, mode, cond,
3058 remainder | shift_mask,
d499463f
RE
3059 targ, source, subtargets, 0);
3060 }
2b835d68
RE
3061 }
3062
3063 if (generate)
3064 {
d499463f
RE
3065 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3066 rtx shift = GEN_INT (clear_zero_bit_copies);
3067
3068 emit_insn (gen_lshrsi3 (new_src, source, shift));
3069 emit_insn (gen_ashlsi3 (target, new_src, shift));
e2c671ba
RE
3070 }
3071
e2c671ba
RE
3072 return insns + 2;
3073 }
3074
3075 break;
3076
3077 default:
3078 break;
3079 }
3080
3081 for (i = 0; i < 32; i++)
3082 if (remainder & (1 << i))
3083 num_bits_set++;
3084
422c0989 3085 if ((code == AND) || (can_invert && num_bits_set > 16))
162e4591 3086 remainder ^= 0xffffffff;
e2c671ba 3087 else if (code == PLUS && num_bits_set > 16)
30cf4896 3088 remainder = (-remainder) & 0xffffffff;
162e4591
RE
3089
3090 /* For XOR, if more than half the bits are set and there's a sequence
3091 of more than 8 consecutive ones in the pattern then we can XOR by the
3092 inverted constant and then invert the final result; this may save an
3093 instruction and might also lead to the final mvn being merged with
3094 some other operation. */
3095 else if (code == XOR && num_bits_set > 16
3096 && (count_insns_for_constant (remainder ^ 0xffffffff,
3097 find_best_start
3098 (remainder ^ 0xffffffff))
3099 < count_insns_for_constant (remainder,
3100 find_best_start (remainder))))
3101 {
3102 remainder ^= 0xffffffff;
3103 final_invert = 1;
3104 }
e2c671ba
RE
3105 else
3106 {
3107 can_invert = 0;
3108 can_negate = 0;
3109 }
3110
3111 /* Now try and find a way of doing the job in either two or three
3112 instructions.
3113 We start by looking for the largest block of zeros that are aligned on
3114 a 2-bit boundary, we then fill up the temps, wrapping around to the
3115 top of the word when we drop off the bottom.
5b3e6663
PB
3116 In the worst case this code should produce no more than four insns.
3117 Thumb-2 constants are shifted, not rotated, so the MSB is always the
3118 best place to start. */
3119
3120 /* ??? Use thumb2 replicated constants when the high and low halfwords are
3121 the same. */
e2c671ba 3122 {
ceebdb09 3123 /* Now start emitting the insns. */
162e4591 3124 i = find_best_start (remainder);
e2c671ba
RE
3125 do
3126 {
3127 int end;
3128
3129 if (i <= 0)
3130 i += 32;
3131 if (remainder & (3 << (i - 2)))
3132 {
3133 end = i - 8;
3134 if (end < 0)
3135 end += 32;
3136 temp1 = remainder & ((0x0ff << end)
3137 | ((i < end) ? (0xff >> (32 - end)) : 0));
3138 remainder &= ~temp1;
3139
d499463f 3140 if (generate)
e2c671ba 3141 {
9503f3d1
RH
3142 rtx new_src, temp1_rtx;
3143
3144 if (code == SET || code == MINUS)
3145 {
3146 new_src = (subtargets ? gen_reg_rtx (mode) : target);
96ae8197 3147 if (can_invert && code != MINUS)
9503f3d1
RH
3148 temp1 = ~temp1;
3149 }
3150 else
3151 {
162e4591 3152 if ((final_invert || remainder) && subtargets)
9503f3d1 3153 new_src = gen_reg_rtx (mode);
96ae8197
NC
3154 else
3155 new_src = target;
9503f3d1
RH
3156 if (can_invert)
3157 temp1 = ~temp1;
3158 else if (can_negate)
3159 temp1 = -temp1;
3160 }
3161
3162 temp1 = trunc_int_for_mode (temp1, mode);
3163 temp1_rtx = GEN_INT (temp1);
d499463f
RE
3164
3165 if (code == SET)
9503f3d1 3166 ;
d499463f 3167 else if (code == MINUS)
9503f3d1 3168 temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
d499463f 3169 else
9503f3d1
RH
3170 temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
3171
a406f566 3172 emit_constant_insn (cond,
f676971a 3173 gen_rtx_SET (VOIDmode, new_src,
a406f566 3174 temp1_rtx));
d499463f 3175 source = new_src;
e2c671ba
RE
3176 }
3177
d499463f
RE
3178 if (code == SET)
3179 {
3180 can_invert = 0;
3181 code = PLUS;
3182 }
3183 else if (code == MINUS)
3184 code = PLUS;
3185
e2c671ba 3186 insns++;
162e4591 3187 i -= 8 - step_size;
e2c671ba 3188 }
7a085dce 3189 /* Arm allows rotates by a multiple of two. Thumb-2 allows arbitrary
5b3e6663 3190 shifts. */
162e4591 3191 i -= step_size;
1d6e90ac
NC
3192 }
3193 while (remainder);
e2c671ba 3194 }
1d6e90ac 3195
162e4591
RE
3196 if (final_invert)
3197 {
3198 if (generate)
3199 emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
3200 gen_rtx_NOT (mode, source)));
3201 insns++;
3202 }
3203
e2c671ba
RE
3204 return insns;
3205}
3206
bd9c7e23
RE
3207/* Canonicalize a comparison so that we are more likely to recognize it.
3208 This can be done for a few constant compares, where we can make the
3209 immediate value easier to load. */
1d6e90ac 3210
bd9c7e23 3211enum rtx_code
73160ba9 3212arm_canonicalize_comparison (enum rtx_code code, rtx *op0, rtx *op1)
bd9c7e23 3213{
73160ba9
DJ
3214 enum machine_mode mode;
3215 unsigned HOST_WIDE_INT i, maxval;
3216
3217 mode = GET_MODE (*op0);
3218 if (mode == VOIDmode)
3219 mode = GET_MODE (*op1);
3220
a14b88bb 3221 maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
bd9c7e23 3222
73160ba9
DJ
3223 /* For DImode, we have GE/LT/GEU/LTU comparisons. In ARM mode
3224 we can also use cmp/cmpeq for GTU/LEU. GT/LE must be either
3225 reversed or (for constant OP1) adjusted to GE/LT. Similarly
3226 for GTU/LEU in Thumb mode. */
3227 if (mode == DImode)
3228 {
3229 rtx tem;
3230
3231 /* To keep things simple, always use the Cirrus cfcmp64 if it is
3232 available. */
3233 if (TARGET_ARM && TARGET_HARD_FLOAT && TARGET_MAVERICK)
3234 return code;
3235
3236 if (code == GT || code == LE
3237 || (!TARGET_ARM && (code == GTU || code == LEU)))
3238 {
3239 /* Missing comparison. First try to use an available
3240 comparison. */
3241 if (GET_CODE (*op1) == CONST_INT)
3242 {
3243 i = INTVAL (*op1);
3244 switch (code)
3245 {
3246 case GT:
3247 case LE:
3248 if (i != maxval
3249 && arm_const_double_by_immediates (GEN_INT (i + 1)))
3250 {
3251 *op1 = GEN_INT (i + 1);
3252 return code == GT ? GE : LT;
3253 }
3254 break;
3255 case GTU:
3256 case LEU:
3257 if (i != ~((unsigned HOST_WIDE_INT) 0)
3258 && arm_const_double_by_immediates (GEN_INT (i + 1)))
3259 {
3260 *op1 = GEN_INT (i + 1);
3261 return code == GTU ? GEU : LTU;
3262 }
3263 break;
3264 default:
3265 gcc_unreachable ();
3266 }
3267 }
3268
3269 /* If that did not work, reverse the condition. */
3270 tem = *op0;
3271 *op0 = *op1;
3272 *op1 = tem;
3273 return swap_condition (code);
3274 }
3275
3276 return code;
3277 }
3278
3279 /* Comparisons smaller than DImode. Only adjust comparisons against
3280 an out-of-range constant. */
3281 if (GET_CODE (*op1) != CONST_INT
3282 || const_ok_for_arm (INTVAL (*op1))
3283 || const_ok_for_arm (- INTVAL (*op1)))
3284 return code;
3285
3286 i = INTVAL (*op1);
3287
bd9c7e23
RE
3288 switch (code)
3289 {
3290 case EQ:
3291 case NE:
3292 return code;
3293
3294 case GT:
3295 case LE:
a14b88bb 3296 if (i != maxval
5895f793 3297 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
bd9c7e23 3298 {
5895f793 3299 *op1 = GEN_INT (i + 1);
bd9c7e23
RE
3300 return code == GT ? GE : LT;
3301 }
3302 break;
3303
3304 case GE:
3305 case LT:
a14b88bb 3306 if (i != ~maxval
5895f793 3307 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
bd9c7e23 3308 {
5895f793 3309 *op1 = GEN_INT (i - 1);
bd9c7e23
RE
3310 return code == GE ? GT : LE;
3311 }
3312 break;
3313
3314 case GTU:
3315 case LEU:
30cf4896 3316 if (i != ~((unsigned HOST_WIDE_INT) 0)
5895f793 3317 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
bd9c7e23
RE
3318 {
3319 *op1 = GEN_INT (i + 1);
3320 return code == GTU ? GEU : LTU;
3321 }
3322 break;
3323
3324 case GEU:
3325 case LTU:
3326 if (i != 0
5895f793 3327 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
bd9c7e23
RE
3328 {
3329 *op1 = GEN_INT (i - 1);
3330 return code == GEU ? GTU : LEU;
3331 }
3332 break;
3333
3334 default:
e6d29d15 3335 gcc_unreachable ();
bd9c7e23
RE
3336 }
3337
3338 return code;
3339}
bd9c7e23 3340
d4453b7a
PB
3341
3342/* Define how to find the value returned by a function. */
3343
390b17c2
RE
3344static rtx
3345arm_function_value(const_tree type, const_tree func,
3346 bool outgoing ATTRIBUTE_UNUSED)
d4453b7a
PB
3347{
3348 enum machine_mode mode;
3349 int unsignedp ATTRIBUTE_UNUSED;
3350 rtx r ATTRIBUTE_UNUSED;
3351
d4453b7a 3352 mode = TYPE_MODE (type);
390b17c2
RE
3353
3354 if (TARGET_AAPCS_BASED)
3355 return aapcs_allocate_return_reg (mode, type, func);
3356
d4453b7a
PB
3357 /* Promote integer types. */
3358 if (INTEGRAL_TYPE_P (type))
cde0f3fd 3359 mode = arm_promote_function_mode (type, mode, &unsignedp, func, 1);
866af8a9
JB
3360
3361 /* Promotes small structs returned in a register to full-word size
3362 for big-endian AAPCS. */
3363 if (arm_return_in_msb (type))
3364 {
3365 HOST_WIDE_INT size = int_size_in_bytes (type);
3366 if (size % UNITS_PER_WORD != 0)
3367 {
3368 size += UNITS_PER_WORD - size % UNITS_PER_WORD;
3369 mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
3370 }
3371 }
e0b92319 3372
390b17c2
RE
3373 return LIBCALL_VALUE (mode);
3374}
3375
3376static int
3377libcall_eq (const void *p1, const void *p2)
3378{
3379 return rtx_equal_p ((const_rtx) p1, (const_rtx) p2);
3380}
3381
3382static hashval_t
3383libcall_hash (const void *p1)
3384{
3385 return hash_rtx ((const_rtx) p1, VOIDmode, NULL, NULL, FALSE);
3386}
3387
3388static void
3389add_libcall (htab_t htab, rtx libcall)
3390{
3391 *htab_find_slot (htab, libcall, INSERT) = libcall;
3392}
3393
3394static bool
7fc6a96b 3395arm_libcall_uses_aapcs_base (const_rtx libcall)
390b17c2
RE
3396{
3397 static bool init_done = false;
3398 static htab_t libcall_htab;
3399
3400 if (!init_done)
3401 {
3402 init_done = true;
3403
3404 libcall_htab = htab_create (31, libcall_hash, libcall_eq,
3405 NULL);
3406 add_libcall (libcall_htab,
3407 convert_optab_libfunc (sfloat_optab, SFmode, SImode));
3408 add_libcall (libcall_htab,
3409 convert_optab_libfunc (sfloat_optab, DFmode, SImode));
3410 add_libcall (libcall_htab,
3411 convert_optab_libfunc (sfloat_optab, SFmode, DImode));
3412 add_libcall (libcall_htab,
3413 convert_optab_libfunc (sfloat_optab, DFmode, DImode));
3414
3415 add_libcall (libcall_htab,
3416 convert_optab_libfunc (ufloat_optab, SFmode, SImode));
3417 add_libcall (libcall_htab,
3418 convert_optab_libfunc (ufloat_optab, DFmode, SImode));
3419 add_libcall (libcall_htab,
3420 convert_optab_libfunc (ufloat_optab, SFmode, DImode));
3421 add_libcall (libcall_htab,
3422 convert_optab_libfunc (ufloat_optab, DFmode, DImode));
3423
3424 add_libcall (libcall_htab,
3425 convert_optab_libfunc (sext_optab, SFmode, HFmode));
3426 add_libcall (libcall_htab,
3427 convert_optab_libfunc (trunc_optab, HFmode, SFmode));
3428 add_libcall (libcall_htab,
3429 convert_optab_libfunc (sfix_optab, DImode, DFmode));
3430 add_libcall (libcall_htab,
3431 convert_optab_libfunc (ufix_optab, DImode, DFmode));
3432 add_libcall (libcall_htab,
3433 convert_optab_libfunc (sfix_optab, DImode, SFmode));
3434 add_libcall (libcall_htab,
3435 convert_optab_libfunc (ufix_optab, DImode, SFmode));
3436 }
3437
3438 return libcall && htab_find (libcall_htab, libcall) != NULL;
3439}
3440
3441rtx
7fc6a96b 3442arm_libcall_value (enum machine_mode mode, const_rtx libcall)
390b17c2
RE
3443{
3444 if (TARGET_AAPCS_BASED && arm_pcs_default != ARM_PCS_AAPCS
3445 && GET_MODE_CLASS (mode) == MODE_FLOAT)
3446 {
3447 /* The following libcalls return their result in integer registers,
3448 even though they return a floating point value. */
3449 if (arm_libcall_uses_aapcs_base (libcall))
3450 return gen_rtx_REG (mode, ARG_REGISTER(1));
3451
3452 }
3453
3454 return LIBCALL_VALUE (mode);
d4453b7a
PB
3455}
3456
e0b92319 3457/* Determine the amount of memory needed to store the possible return
9f7bf991
RE
3458 registers of an untyped call. */
3459int
3460arm_apply_result_size (void)
3461{
3462 int size = 16;
3463
390b17c2 3464 if (TARGET_32BIT)
9f7bf991
RE
3465 {
3466 if (TARGET_HARD_FLOAT_ABI)
3467 {
390b17c2
RE
3468 if (TARGET_VFP)
3469 size += 32;
9f7bf991
RE
3470 if (TARGET_FPA)
3471 size += 12;
3472 if (TARGET_MAVERICK)
3473 size += 8;
3474 }
3475 if (TARGET_IWMMXT_ABI)
3476 size += 8;
3477 }
3478
3479 return size;
3480}
d4453b7a 3481
390b17c2
RE
3482/* Decide whether TYPE should be returned in memory (true)
3483 or in a register (false). FNTYPE is the type of the function making
3484 the call. */
23668cf7 3485static bool
390b17c2 3486arm_return_in_memory (const_tree type, const_tree fntype)
2b835d68 3487{
dc0ba55a
JT
3488 HOST_WIDE_INT size;
3489
390b17c2
RE
3490 size = int_size_in_bytes (type); /* Negative if not fixed size. */
3491
3492 if (TARGET_AAPCS_BASED)
3493 {
3494 /* Simple, non-aggregate types (ie not including vectors and
3495 complex) are always returned in a register (or registers).
3496 We don't care about which register here, so we can short-cut
3497 some of the detail. */
3498 if (!AGGREGATE_TYPE_P (type)
3499 && TREE_CODE (type) != VECTOR_TYPE
3500 && TREE_CODE (type) != COMPLEX_TYPE)
3501 return false;
3502
3503 /* Any return value that is no larger than one word can be
3504 returned in r0. */
3505 if (((unsigned HOST_WIDE_INT) size) <= UNITS_PER_WORD)
3506 return false;
3507
3508 /* Check any available co-processors to see if they accept the
3509 type as a register candidate (VFP, for example, can return
3510 some aggregates in consecutive registers). These aren't
3511 available if the call is variadic. */
3512 if (aapcs_select_return_coproc (type, fntype) >= 0)
3513 return false;
3514
3515 /* Vector values should be returned using ARM registers, not
3516 memory (unless they're over 16 bytes, which will break since
3517 we only have four call-clobbered registers to play with). */
3518 if (TREE_CODE (type) == VECTOR_TYPE)
3519 return (size < 0 || size > (4 * UNITS_PER_WORD));
3520
3521 /* The rest go in memory. */
3522 return true;
3523 }
88f77cba 3524
88f77cba
JB
3525 if (TREE_CODE (type) == VECTOR_TYPE)
3526 return (size < 0 || size > (4 * UNITS_PER_WORD));
3527
3dd7ab65 3528 if (!AGGREGATE_TYPE_P (type) &&
390b17c2
RE
3529 (TREE_CODE (type) != VECTOR_TYPE))
3530 /* All simple types are returned in registers. */
3531 return false;
dc0ba55a 3532
5848830f 3533 if (arm_abi != ARM_ABI_APCS)
dc0ba55a 3534 {
5848830f 3535 /* ATPCS and later return aggregate types in memory only if they are
dc0ba55a
JT
3536 larger than a word (or are variable size). */
3537 return (size < 0 || size > UNITS_PER_WORD);
3538 }
f676971a 3539
6bc82793 3540 /* For the arm-wince targets we choose to be compatible with Microsoft's
d5b7b3ae
RE
3541 ARM and Thumb compilers, which always return aggregates in memory. */
3542#ifndef ARM_WINCE
e529bd42
NC
3543 /* All structures/unions bigger than one word are returned in memory.
3544 Also catch the case where int_size_in_bytes returns -1. In this case
6bc82793 3545 the aggregate is either huge or of variable size, and in either case
e529bd42 3546 we will want to return it via memory and not in a register. */
dc0ba55a 3547 if (size < 0 || size > UNITS_PER_WORD)
390b17c2 3548 return true;
f676971a 3549
d7d01975 3550 if (TREE_CODE (type) == RECORD_TYPE)
2b835d68
RE
3551 {
3552 tree field;
3553
3a2ea258
RE
3554 /* For a struct the APCS says that we only return in a register
3555 if the type is 'integer like' and every addressable element
3556 has an offset of zero. For practical purposes this means
3557 that the structure can have at most one non bit-field element
3558 and that this element must be the first one in the structure. */
f676971a 3559
f5a1b0d2
NC
3560 /* Find the first field, ignoring non FIELD_DECL things which will
3561 have been created by C++. */
3562 for (field = TYPE_FIELDS (type);
3563 field && TREE_CODE (field) != FIELD_DECL;
910ad8de 3564 field = DECL_CHAIN (field))
f5a1b0d2 3565 continue;
f676971a 3566
f5a1b0d2 3567 if (field == NULL)
390b17c2 3568 return false; /* An empty structure. Allowed by an extension to ANSI C. */
f5a1b0d2 3569
d5b7b3ae
RE
3570 /* Check that the first field is valid for returning in a register. */
3571
3572 /* ... Floats are not allowed */
9e291dbe 3573 if (FLOAT_TYPE_P (TREE_TYPE (field)))
390b17c2 3574 return true;
3a2ea258 3575
d5b7b3ae
RE
3576 /* ... Aggregates that are not themselves valid for returning in
3577 a register are not allowed. */
81464b2c 3578 if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
390b17c2 3579 return true;
6f7ebcbb 3580
3a2ea258
RE
3581 /* Now check the remaining fields, if any. Only bitfields are allowed,
3582 since they are not addressable. */
910ad8de 3583 for (field = DECL_CHAIN (field);
f5a1b0d2 3584 field;
910ad8de 3585 field = DECL_CHAIN (field))
f5a1b0d2
NC
3586 {
3587 if (TREE_CODE (field) != FIELD_DECL)
3588 continue;
f676971a 3589
5895f793 3590 if (!DECL_BIT_FIELD_TYPE (field))
390b17c2 3591 return true;
f5a1b0d2 3592 }
2b835d68 3593
390b17c2 3594 return false;
2b835d68 3595 }
f676971a 3596
d7d01975 3597 if (TREE_CODE (type) == UNION_TYPE)
2b835d68
RE
3598 {
3599 tree field;
3600
3601 /* Unions can be returned in registers if every element is
3602 integral, or can be returned in an integer register. */
f5a1b0d2
NC
3603 for (field = TYPE_FIELDS (type);
3604 field;
910ad8de 3605 field = DECL_CHAIN (field))
2b835d68 3606 {
f5a1b0d2
NC
3607 if (TREE_CODE (field) != FIELD_DECL)
3608 continue;
3609
6cc8c0b3 3610 if (FLOAT_TYPE_P (TREE_TYPE (field)))
390b17c2 3611 return true;
f676971a 3612
81464b2c 3613 if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
390b17c2 3614 return true;
2b835d68 3615 }
f676971a 3616
390b17c2 3617 return false;
2b835d68 3618 }
f676971a
EC
3619#endif /* not ARM_WINCE */
3620
d5b7b3ae 3621 /* Return all other types in memory. */
390b17c2 3622 return true;
2b835d68
RE
3623}
3624
d6b4baa4 3625/* Indicate whether or not words of a double are in big-endian order. */
3717da94
JT
3626
3627int
e32bac5b 3628arm_float_words_big_endian (void)
3717da94 3629{
9b66ebb1 3630 if (TARGET_MAVERICK)
9b6b54e2 3631 return 0;
3717da94
JT
3632
3633 /* For FPA, float words are always big-endian. For VFP, floats words
3634 follow the memory system mode. */
3635
9b66ebb1 3636 if (TARGET_FPA)
3717da94 3637 {
3717da94
JT
3638 return 1;
3639 }
3640
3641 if (TARGET_VFP)
3642 return (TARGET_BIG_END ? 1 : 0);
3643
3644 return 1;
3645}
3646
390b17c2
RE
3647const struct pcs_attribute_arg
3648{
3649 const char *arg;
3650 enum arm_pcs value;
3651} pcs_attribute_args[] =
3652 {
3653 {"aapcs", ARM_PCS_AAPCS},
3654 {"aapcs-vfp", ARM_PCS_AAPCS_VFP},
0f1a24df
RE
3655#if 0
3656 /* We could recognize these, but changes would be needed elsewhere
3657 * to implement them. */
390b17c2
RE
3658 {"aapcs-iwmmxt", ARM_PCS_AAPCS_IWMMXT},
3659 {"atpcs", ARM_PCS_ATPCS},
3660 {"apcs", ARM_PCS_APCS},
0f1a24df 3661#endif
390b17c2
RE
3662 {NULL, ARM_PCS_UNKNOWN}
3663 };
3664
3665static enum arm_pcs
3666arm_pcs_from_attribute (tree attr)
3667{
3668 const struct pcs_attribute_arg *ptr;
3669 const char *arg;
3670
3671 /* Get the value of the argument. */
3672 if (TREE_VALUE (attr) == NULL_TREE
3673 || TREE_CODE (TREE_VALUE (attr)) != STRING_CST)
3674 return ARM_PCS_UNKNOWN;
3675
3676 arg = TREE_STRING_POINTER (TREE_VALUE (attr));
3677
3678 /* Check it against the list of known arguments. */
3679 for (ptr = pcs_attribute_args; ptr->arg != NULL; ptr++)
3680 if (streq (arg, ptr->arg))
3681 return ptr->value;
3682
3683 /* An unrecognized interrupt type. */
3684 return ARM_PCS_UNKNOWN;
3685}
3686
3687/* Get the PCS variant to use for this call. TYPE is the function's type
3688 specification, DECL is the specific declartion. DECL may be null if
3689 the call could be indirect or if this is a library call. */
3690static enum arm_pcs
3691arm_get_pcs_model (const_tree type, const_tree decl)
3692{
3693 bool user_convention = false;
3694 enum arm_pcs user_pcs = arm_pcs_default;
3695 tree attr;
3696
3697 gcc_assert (type);
3698
3699 attr = lookup_attribute ("pcs", TYPE_ATTRIBUTES (type));
3700 if (attr)
3701 {
3702 user_pcs = arm_pcs_from_attribute (TREE_VALUE (attr));
3703 user_convention = true;
3704 }
3705
3706 if (TARGET_AAPCS_BASED)
3707 {
3708 /* Detect varargs functions. These always use the base rules
3709 (no argument is ever a candidate for a co-processor
3710 register). */
f38958e8 3711 bool base_rules = stdarg_p (type);
390b17c2
RE
3712
3713 if (user_convention)
3714 {
3715 if (user_pcs > ARM_PCS_AAPCS_LOCAL)
d8a07487 3716 sorry ("non-AAPCS derived PCS variant");
390b17c2 3717 else if (base_rules && user_pcs != ARM_PCS_AAPCS)
d8a07487 3718 error ("variadic functions must use the base AAPCS variant");
390b17c2
RE
3719 }
3720
3721 if (base_rules)
3722 return ARM_PCS_AAPCS;
3723 else if (user_convention)
3724 return user_pcs;
3725 else if (decl && flag_unit_at_a_time)
3726 {
3727 /* Local functions never leak outside this compilation unit,
3728 so we are free to use whatever conventions are
3729 appropriate. */
3730 /* FIXME: remove CONST_CAST_TREE when cgraph is constified. */
3731 struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE(decl));
3732 if (i && i->local)
3733 return ARM_PCS_AAPCS_LOCAL;
3734 }
3735 }
3736 else if (user_convention && user_pcs != arm_pcs_default)
3737 sorry ("PCS variant");
3738
3739 /* For everything else we use the target's default. */
3740 return arm_pcs_default;
3741}
3742
3743
3744static void
3745aapcs_vfp_cum_init (CUMULATIVE_ARGS *pcum ATTRIBUTE_UNUSED,
3746 const_tree fntype ATTRIBUTE_UNUSED,
3747 rtx libcall ATTRIBUTE_UNUSED,
3748 const_tree fndecl ATTRIBUTE_UNUSED)
3749{
3750 /* Record the unallocated VFP registers. */
3751 pcum->aapcs_vfp_regs_free = (1 << NUM_VFP_ARG_REGS) - 1;
3752 pcum->aapcs_vfp_reg_alloc = 0;
3753}
3754
3755/* Walk down the type tree of TYPE counting consecutive base elements.
3756 If *MODEP is VOIDmode, then set it to the first valid floating point
3757 type. If a non-floating point type is found, or if a floating point
3758 type that doesn't match a non-VOIDmode *MODEP is found, then return -1,
3759 otherwise return the count in the sub-tree. */
3760static int
3761aapcs_vfp_sub_candidate (const_tree type, enum machine_mode *modep)
3762{
3763 enum machine_mode mode;
3764 HOST_WIDE_INT size;
3765
3766 switch (TREE_CODE (type))
3767 {
3768 case REAL_TYPE:
3769 mode = TYPE_MODE (type);
3770 if (mode != DFmode && mode != SFmode)
3771 return -1;
3772
3773 if (*modep == VOIDmode)
3774 *modep = mode;
3775
3776 if (*modep == mode)
3777 return 1;
3778
3779 break;
3780
3781 case COMPLEX_TYPE:
3782 mode = TYPE_MODE (TREE_TYPE (type));
3783 if (mode != DFmode && mode != SFmode)
3784 return -1;
3785
3786 if (*modep == VOIDmode)
3787 *modep = mode;
3788
3789 if (*modep == mode)
3790 return 2;
3791
3792 break;
3793
3794 case VECTOR_TYPE:
3795 /* Use V2SImode and V4SImode as representatives of all 64-bit
3796 and 128-bit vector types, whether or not those modes are
3797 supported with the present options. */
3798 size = int_size_in_bytes (type);
3799 switch (size)
3800 {
3801 case 8:
3802 mode = V2SImode;
3803 break;
3804 case 16:
3805 mode = V4SImode;
3806 break;
3807 default:
3808 return -1;
3809 }
3810
3811 if (*modep == VOIDmode)
3812 *modep = mode;
3813
3814 /* Vector modes are considered to be opaque: two vectors are
3815 equivalent for the purposes of being homogeneous aggregates
3816 if they are the same size. */
3817 if (*modep == mode)
3818 return 1;
3819
3820 break;
3821
3822 case ARRAY_TYPE:
3823 {
3824 int count;
3825 tree index = TYPE_DOMAIN (type);
3826
3827 /* Can't handle incomplete types. */
3828 if (!COMPLETE_TYPE_P(type))
3829 return -1;
3830
3831 count = aapcs_vfp_sub_candidate (TREE_TYPE (type), modep);
3832 if (count == -1
3833 || !index
3834 || !TYPE_MAX_VALUE (index)
3835 || !host_integerp (TYPE_MAX_VALUE (index), 1)
3836 || !TYPE_MIN_VALUE (index)
3837 || !host_integerp (TYPE_MIN_VALUE (index), 1)
3838 || count < 0)
3839 return -1;
3840
3841 count *= (1 + tree_low_cst (TYPE_MAX_VALUE (index), 1)
3842 - tree_low_cst (TYPE_MIN_VALUE (index), 1));
3843
3844 /* There must be no padding. */
3845 if (!host_integerp (TYPE_SIZE (type), 1)
3846 || (tree_low_cst (TYPE_SIZE (type), 1)
3847 != count * GET_MODE_BITSIZE (*modep)))
3848 return -1;
3849
3850 return count;
3851 }
3852
3853 case RECORD_TYPE:
3854 {
3855 int count = 0;
3856 int sub_count;
3857 tree field;
3858
3859 /* Can't handle incomplete types. */
3860 if (!COMPLETE_TYPE_P(type))
3861 return -1;
3862
910ad8de 3863 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
390b17c2
RE
3864 {
3865 if (TREE_CODE (field) != FIELD_DECL)
3866 continue;
3867
3868 sub_count = aapcs_vfp_sub_candidate (TREE_TYPE (field), modep);
3869 if (sub_count < 0)
3870 return -1;
3871 count += sub_count;
3872 }
3873
3874 /* There must be no padding. */
3875 if (!host_integerp (TYPE_SIZE (type), 1)
3876 || (tree_low_cst (TYPE_SIZE (type), 1)
3877 != count * GET_MODE_BITSIZE (*modep)))
3878 return -1;
3879
3880 return count;
3881 }
3882
3883 case UNION_TYPE:
3884 case QUAL_UNION_TYPE:
3885 {
3886 /* These aren't very interesting except in a degenerate case. */
3887 int count = 0;
3888 int sub_count;
3889 tree field;
3890
3891 /* Can't handle incomplete types. */
3892 if (!COMPLETE_TYPE_P(type))
3893 return -1;
3894
910ad8de 3895 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
390b17c2
RE
3896 {
3897 if (TREE_CODE (field) != FIELD_DECL)
3898 continue;
3899
3900 sub_count = aapcs_vfp_sub_candidate (TREE_TYPE (field), modep);
3901 if (sub_count < 0)
3902 return -1;
3903 count = count > sub_count ? count : sub_count;
3904 }
3905
3906 /* There must be no padding. */
3907 if (!host_integerp (TYPE_SIZE (type), 1)
3908 || (tree_low_cst (TYPE_SIZE (type), 1)
3909 != count * GET_MODE_BITSIZE (*modep)))
3910 return -1;
3911
3912 return count;
3913 }
3914
3915 default:
3916 break;
3917 }
3918
3919 return -1;
3920}
3921
e0dc3601 3922/* Return true if PCS_VARIANT should use VFP registers. */
390b17c2 3923static bool
e0dc3601 3924use_vfp_abi (enum arm_pcs pcs_variant, bool is_double)
390b17c2 3925{
e0dc3601 3926 if (pcs_variant == ARM_PCS_AAPCS_VFP)
50416c61
PB
3927 {
3928 static bool seen_thumb1_vfp = false;
3929
3930 if (TARGET_THUMB1 && !seen_thumb1_vfp)
3931 {
3932 sorry ("Thumb-1 hard-float VFP ABI");
3933 /* sorry() is not immediately fatal, so only display this once. */
3934 seen_thumb1_vfp = true;
3935 }
3936
3937 return true;
3938 }
e0dc3601
PB
3939
3940 if (pcs_variant != ARM_PCS_AAPCS_LOCAL)
3941 return false;
3942
3943 return (TARGET_32BIT && TARGET_VFP && TARGET_HARD_FLOAT &&
3944 (TARGET_VFP_DOUBLE || !is_double));
3945}
3946
3947static bool
3948aapcs_vfp_is_call_or_return_candidate (enum arm_pcs pcs_variant,
3949 enum machine_mode mode, const_tree type,
70dd156a 3950 enum machine_mode *base_mode, int *count)
e0dc3601
PB
3951{
3952 enum machine_mode new_mode = VOIDmode;
3953
390b17c2
RE
3954 if (GET_MODE_CLASS (mode) == MODE_FLOAT
3955 || GET_MODE_CLASS (mode) == MODE_VECTOR_INT
3956 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
3957 {
3958 *count = 1;
e0dc3601 3959 new_mode = mode;
390b17c2
RE
3960 }
3961 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3962 {
3963 *count = 2;
e0dc3601 3964 new_mode = (mode == DCmode ? DFmode : SFmode);
390b17c2
RE
3965 }
3966 else if (type && (mode == BLKmode || TREE_CODE (type) == VECTOR_TYPE))
3967 {
e0dc3601 3968 int ag_count = aapcs_vfp_sub_candidate (type, &new_mode);
390b17c2
RE
3969
3970 if (ag_count > 0 && ag_count <= 4)
e0dc3601
PB
3971 *count = ag_count;
3972 else
3973 return false;
390b17c2 3974 }
e0dc3601
PB
3975 else
3976 return false;
3977
3978
3979 if (!use_vfp_abi (pcs_variant, ARM_NUM_REGS (new_mode) > 1))
3980 return false;
3981
3982 *base_mode = new_mode;
3983 return true;
390b17c2
RE
3984}
3985
3986static bool
3987aapcs_vfp_is_return_candidate (enum arm_pcs pcs_variant,
3988 enum machine_mode mode, const_tree type)
3989{
3990 int count ATTRIBUTE_UNUSED;
46107b99 3991 enum machine_mode ag_mode ATTRIBUTE_UNUSED;
390b17c2 3992
e0dc3601 3993 if (!use_vfp_abi (pcs_variant, false))
390b17c2 3994 return false;
e0dc3601
PB
3995 return aapcs_vfp_is_call_or_return_candidate (pcs_variant, mode, type,
3996 &ag_mode, &count);
390b17c2
RE
3997}
3998
3999static bool
4000aapcs_vfp_is_call_candidate (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4001 const_tree type)
4002{
e0dc3601 4003 if (!use_vfp_abi (pcum->pcs_variant, false))
390b17c2 4004 return false;
e0dc3601
PB
4005
4006 return aapcs_vfp_is_call_or_return_candidate (pcum->pcs_variant, mode, type,
390b17c2
RE
4007 &pcum->aapcs_vfp_rmode,
4008 &pcum->aapcs_vfp_rcount);
4009}
4010
4011static bool
4012aapcs_vfp_allocate (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4013 const_tree type ATTRIBUTE_UNUSED)
4014{
4015 int shift = GET_MODE_SIZE (pcum->aapcs_vfp_rmode) / GET_MODE_SIZE (SFmode);
4016 unsigned mask = (1 << (shift * pcum->aapcs_vfp_rcount)) - 1;
4017 int regno;
4018
4019 for (regno = 0; regno < NUM_VFP_ARG_REGS; regno += shift)
4020 if (((pcum->aapcs_vfp_regs_free >> regno) & mask) == mask)
4021 {
4022 pcum->aapcs_vfp_reg_alloc = mask << regno;
4023 if (mode == BLKmode || (mode == TImode && !TARGET_NEON))
4024 {
4025 int i;
4026 int rcount = pcum->aapcs_vfp_rcount;
4027 int rshift = shift;
4028 enum machine_mode rmode = pcum->aapcs_vfp_rmode;
4029 rtx par;
4030 if (!TARGET_NEON)
4031 {
4032 /* Avoid using unsupported vector modes. */
4033 if (rmode == V2SImode)
4034 rmode = DImode;
4035 else if (rmode == V4SImode)
4036 {
4037 rmode = DImode;
4038 rcount *= 2;
4039 rshift /= 2;
4040 }
4041 }
4042 par = gen_rtx_PARALLEL (mode, rtvec_alloc (rcount));
4043 for (i = 0; i < rcount; i++)
4044 {
4045 rtx tmp = gen_rtx_REG (rmode,
4046 FIRST_VFP_REGNUM + regno + i * rshift);
4047 tmp = gen_rtx_EXPR_LIST
4048 (VOIDmode, tmp,
4049 GEN_INT (i * GET_MODE_SIZE (rmode)));
4050 XVECEXP (par, 0, i) = tmp;
4051 }
4052
4053 pcum->aapcs_reg = par;
4054 }
4055 else
4056 pcum->aapcs_reg = gen_rtx_REG (mode, FIRST_VFP_REGNUM + regno);
4057 return true;
4058 }
4059 return false;
4060}
4061
4062static rtx
4063aapcs_vfp_allocate_return_reg (enum arm_pcs pcs_variant ATTRIBUTE_UNUSED,
4064 enum machine_mode mode,
4065 const_tree type ATTRIBUTE_UNUSED)
4066{
e0dc3601 4067 if (!use_vfp_abi (pcs_variant, false))
390b17c2 4068 return false;
e0dc3601 4069
390b17c2
RE
4070 if (mode == BLKmode || (mode == TImode && !TARGET_NEON))
4071 {
4072 int count;
46107b99 4073 enum machine_mode ag_mode;
390b17c2
RE
4074 int i;
4075 rtx par;
4076 int shift;
4077
e0dc3601
PB
4078 aapcs_vfp_is_call_or_return_candidate (pcs_variant, mode, type,
4079 &ag_mode, &count);
390b17c2
RE
4080
4081 if (!TARGET_NEON)
4082 {
4083 if (ag_mode == V2SImode)
4084 ag_mode = DImode;
4085 else if (ag_mode == V4SImode)
4086 {
4087 ag_mode = DImode;
4088 count *= 2;
4089 }
4090 }
4091 shift = GET_MODE_SIZE(ag_mode) / GET_MODE_SIZE(SFmode);
4092 par = gen_rtx_PARALLEL (mode, rtvec_alloc (count));
4093 for (i = 0; i < count; i++)
4094 {
4095 rtx tmp = gen_rtx_REG (ag_mode, FIRST_VFP_REGNUM + i * shift);
4096 tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp,
4097 GEN_INT (i * GET_MODE_SIZE (ag_mode)));
4098 XVECEXP (par, 0, i) = tmp;
4099 }
4100
4101 return par;
4102 }
4103
4104 return gen_rtx_REG (mode, FIRST_VFP_REGNUM);
4105}
4106
4107static void
4108aapcs_vfp_advance (CUMULATIVE_ARGS *pcum ATTRIBUTE_UNUSED,
4109 enum machine_mode mode ATTRIBUTE_UNUSED,
4110 const_tree type ATTRIBUTE_UNUSED)
4111{
4112 pcum->aapcs_vfp_regs_free &= ~pcum->aapcs_vfp_reg_alloc;
4113 pcum->aapcs_vfp_reg_alloc = 0;
4114 return;
4115}
4116
4117#define AAPCS_CP(X) \
4118 { \
4119 aapcs_ ## X ## _cum_init, \
4120 aapcs_ ## X ## _is_call_candidate, \
4121 aapcs_ ## X ## _allocate, \
4122 aapcs_ ## X ## _is_return_candidate, \
4123 aapcs_ ## X ## _allocate_return_reg, \
4124 aapcs_ ## X ## _advance \
4125 }
4126
4127/* Table of co-processors that can be used to pass arguments in
4128 registers. Idealy no arugment should be a candidate for more than
4129 one co-processor table entry, but the table is processed in order
4130 and stops after the first match. If that entry then fails to put
4131 the argument into a co-processor register, the argument will go on
4132 the stack. */
4133static struct
4134{
4135 /* Initialize co-processor related state in CUMULATIVE_ARGS structure. */
4136 void (*cum_init) (CUMULATIVE_ARGS *, const_tree, rtx, const_tree);
4137
4138 /* Return true if an argument of mode MODE (or type TYPE if MODE is
4139 BLKmode) is a candidate for this co-processor's registers; this
4140 function should ignore any position-dependent state in
4141 CUMULATIVE_ARGS and only use call-type dependent information. */
4142 bool (*is_call_candidate) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4143
4144 /* Return true if the argument does get a co-processor register; it
4145 should set aapcs_reg to an RTX of the register allocated as is
4146 required for a return from FUNCTION_ARG. */
4147 bool (*allocate) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4148
4149 /* Return true if a result of mode MODE (or type TYPE if MODE is
4150 BLKmode) is can be returned in this co-processor's registers. */
4151 bool (*is_return_candidate) (enum arm_pcs, enum machine_mode, const_tree);
4152
4153 /* Allocate and return an RTX element to hold the return type of a
4154 call, this routine must not fail and will only be called if
4155 is_return_candidate returned true with the same parameters. */
4156 rtx (*allocate_return_reg) (enum arm_pcs, enum machine_mode, const_tree);
4157
4158 /* Finish processing this argument and prepare to start processing
4159 the next one. */
4160 void (*advance) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4161} aapcs_cp_arg_layout[ARM_NUM_COPROC_SLOTS] =
4162 {
4163 AAPCS_CP(vfp)
4164 };
4165
4166#undef AAPCS_CP
4167
4168static int
4169aapcs_select_call_coproc (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
9c6a2bee 4170 const_tree type)
390b17c2
RE
4171{
4172 int i;
4173
4174 for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4175 if (aapcs_cp_arg_layout[i].is_call_candidate (pcum, mode, type))
4176 return i;
4177
4178 return -1;
4179}
4180
4181static int
4182aapcs_select_return_coproc (const_tree type, const_tree fntype)
4183{
4184 /* We aren't passed a decl, so we can't check that a call is local.
4185 However, it isn't clear that that would be a win anyway, since it
4186 might limit some tail-calling opportunities. */
4187 enum arm_pcs pcs_variant;
4188
4189 if (fntype)
4190 {
4191 const_tree fndecl = NULL_TREE;
4192
4193 if (TREE_CODE (fntype) == FUNCTION_DECL)
4194 {
4195 fndecl = fntype;
4196 fntype = TREE_TYPE (fntype);
4197 }
4198
4199 pcs_variant = arm_get_pcs_model (fntype, fndecl);
4200 }
4201 else
4202 pcs_variant = arm_pcs_default;
4203
4204 if (pcs_variant != ARM_PCS_AAPCS)
4205 {
4206 int i;
4207
4208 for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4209 if (aapcs_cp_arg_layout[i].is_return_candidate (pcs_variant,
4210 TYPE_MODE (type),
4211 type))
4212 return i;
4213 }
4214 return -1;
4215}
4216
4217static rtx
4218aapcs_allocate_return_reg (enum machine_mode mode, const_tree type,
4219 const_tree fntype)
4220{
4221 /* We aren't passed a decl, so we can't check that a call is local.
4222 However, it isn't clear that that would be a win anyway, since it
4223 might limit some tail-calling opportunities. */
4224 enum arm_pcs pcs_variant;
4225 int unsignedp ATTRIBUTE_UNUSED;
4226
4227 if (fntype)
4228 {
4229 const_tree fndecl = NULL_TREE;
4230
4231 if (TREE_CODE (fntype) == FUNCTION_DECL)
4232 {
4233 fndecl = fntype;
4234 fntype = TREE_TYPE (fntype);
4235 }
4236
4237 pcs_variant = arm_get_pcs_model (fntype, fndecl);
4238 }
4239 else
4240 pcs_variant = arm_pcs_default;
4241
4242 /* Promote integer types. */
4243 if (type && INTEGRAL_TYPE_P (type))
4244 mode = arm_promote_function_mode (type, mode, &unsignedp, fntype, 1);
4245
4246 if (pcs_variant != ARM_PCS_AAPCS)
4247 {
4248 int i;
4249
4250 for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4251 if (aapcs_cp_arg_layout[i].is_return_candidate (pcs_variant, mode,
4252 type))
4253 return aapcs_cp_arg_layout[i].allocate_return_reg (pcs_variant,
4254 mode, type);
4255 }
4256
4257 /* Promotes small structs returned in a register to full-word size
4258 for big-endian AAPCS. */
4259 if (type && arm_return_in_msb (type))
4260 {
4261 HOST_WIDE_INT size = int_size_in_bytes (type);
4262 if (size % UNITS_PER_WORD != 0)
4263 {
4264 size += UNITS_PER_WORD - size % UNITS_PER_WORD;
4265 mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
4266 }
4267 }
4268
4269 return gen_rtx_REG (mode, R0_REGNUM);
4270}
4271
4272rtx
4273aapcs_libcall_value (enum machine_mode mode)
4274{
4275 return aapcs_allocate_return_reg (mode, NULL_TREE, NULL_TREE);
4276}
4277
4278/* Lay out a function argument using the AAPCS rules. The rule
4279 numbers referred to here are those in the AAPCS. */
4280static void
4281aapcs_layout_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
9c6a2bee 4282 const_tree type, bool named)
390b17c2
RE
4283{
4284 int nregs, nregs2;
4285 int ncrn;
4286
4287 /* We only need to do this once per argument. */
4288 if (pcum->aapcs_arg_processed)
4289 return;
4290
4291 pcum->aapcs_arg_processed = true;
4292
4293 /* Special case: if named is false then we are handling an incoming
4294 anonymous argument which is on the stack. */
4295 if (!named)
4296 return;
4297
4298 /* Is this a potential co-processor register candidate? */
4299 if (pcum->pcs_variant != ARM_PCS_AAPCS)
4300 {
4301 int slot = aapcs_select_call_coproc (pcum, mode, type);
4302 pcum->aapcs_cprc_slot = slot;
4303
4304 /* We don't have to apply any of the rules from part B of the
4305 preparation phase, these are handled elsewhere in the
4306 compiler. */
4307
4308 if (slot >= 0)
4309 {
4310 /* A Co-processor register candidate goes either in its own
4311 class of registers or on the stack. */
4312 if (!pcum->aapcs_cprc_failed[slot])
4313 {
4314 /* C1.cp - Try to allocate the argument to co-processor
4315 registers. */
4316 if (aapcs_cp_arg_layout[slot].allocate (pcum, mode, type))
4317 return;
4318
4319 /* C2.cp - Put the argument on the stack and note that we
4320 can't assign any more candidates in this slot. We also
4321 need to note that we have allocated stack space, so that
4322 we won't later try to split a non-cprc candidate between
4323 core registers and the stack. */
4324 pcum->aapcs_cprc_failed[slot] = true;
4325 pcum->can_split = false;
4326 }
4327
4328 /* We didn't get a register, so this argument goes on the
4329 stack. */
4330 gcc_assert (pcum->can_split == false);
4331 return;
4332 }
4333 }
4334
4335 /* C3 - For double-word aligned arguments, round the NCRN up to the
4336 next even number. */
4337 ncrn = pcum->aapcs_ncrn;
4338 if ((ncrn & 1) && arm_needs_doubleword_align (mode, type))
4339 ncrn++;
4340
4341 nregs = ARM_NUM_REGS2(mode, type);
4342
4343 /* Sigh, this test should really assert that nregs > 0, but a GCC
4344 extension allows empty structs and then gives them empty size; it
4345 then allows such a structure to be passed by value. For some of
4346 the code below we have to pretend that such an argument has
4347 non-zero size so that we 'locate' it correctly either in
4348 registers or on the stack. */
4349 gcc_assert (nregs >= 0);
4350
4351 nregs2 = nregs ? nregs : 1;
4352
4353 /* C4 - Argument fits entirely in core registers. */
4354 if (ncrn + nregs2 <= NUM_ARG_REGS)
4355 {
4356 pcum->aapcs_reg = gen_rtx_REG (mode, ncrn);
4357 pcum->aapcs_next_ncrn = ncrn + nregs;
4358 return;
4359 }
4360
4361 /* C5 - Some core registers left and there are no arguments already
4362 on the stack: split this argument between the remaining core
4363 registers and the stack. */
4364 if (ncrn < NUM_ARG_REGS && pcum->can_split)
4365 {
4366 pcum->aapcs_reg = gen_rtx_REG (mode, ncrn);
4367 pcum->aapcs_next_ncrn = NUM_ARG_REGS;
4368 pcum->aapcs_partial = (NUM_ARG_REGS - ncrn) * UNITS_PER_WORD;
4369 return;
4370 }
4371
4372 /* C6 - NCRN is set to 4. */
4373 pcum->aapcs_next_ncrn = NUM_ARG_REGS;
4374
4375 /* C7,C8 - arugment goes on the stack. We have nothing to do here. */
4376 return;
4377}
4378
82e9d970
PB
4379/* Initialize a variable CUM of type CUMULATIVE_ARGS
4380 for a call to a function whose data type is FNTYPE.
4381 For a library call, FNTYPE is NULL. */
4382void
f676971a 4383arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
390b17c2 4384 rtx libname,
e32bac5b 4385 tree fndecl ATTRIBUTE_UNUSED)
82e9d970 4386{
390b17c2
RE
4387 /* Long call handling. */
4388 if (fntype)
4389 pcum->pcs_variant = arm_get_pcs_model (fntype, fndecl);
4390 else
4391 pcum->pcs_variant = arm_pcs_default;
4392
4393 if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4394 {
4395 if (arm_libcall_uses_aapcs_base (libname))
4396 pcum->pcs_variant = ARM_PCS_AAPCS;
4397
4398 pcum->aapcs_ncrn = pcum->aapcs_next_ncrn = 0;
4399 pcum->aapcs_reg = NULL_RTX;
4400 pcum->aapcs_partial = 0;
4401 pcum->aapcs_arg_processed = false;
4402 pcum->aapcs_cprc_slot = -1;
4403 pcum->can_split = true;
4404
4405 if (pcum->pcs_variant != ARM_PCS_AAPCS)
4406 {
4407 int i;
4408
4409 for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4410 {
4411 pcum->aapcs_cprc_failed[i] = false;
4412 aapcs_cp_arg_layout[i].cum_init (pcum, fntype, libname, fndecl);
4413 }
4414 }
4415 return;
4416 }
4417
4418 /* Legacy ABIs */
4419
82e9d970 4420 /* On the ARM, the offset starts at 0. */
29e339b9 4421 pcum->nregs = 0;
5a9335ef 4422 pcum->iwmmxt_nregs = 0;
5848830f 4423 pcum->can_split = true;
f676971a 4424
5a9335ef
NC
4425 /* Varargs vectors are treated the same as long long.
4426 named_count avoids having to change the way arm handles 'named' */
4427 pcum->named_count = 0;
4428 pcum->nargs = 0;
4429
4430 if (TARGET_REALLY_IWMMXT && fntype)
4431 {
4432 tree fn_arg;
4433
4434 for (fn_arg = TYPE_ARG_TYPES (fntype);
4435 fn_arg;
4436 fn_arg = TREE_CHAIN (fn_arg))
4437 pcum->named_count += 1;
4438
4439 if (! pcum->named_count)
4440 pcum->named_count = INT_MAX;
4441 }
82e9d970
PB
4442}
4443
5848830f
PB
4444
4445/* Return true if mode/type need doubleword alignment. */
c2ed6cf8 4446static bool
9c6a2bee 4447arm_needs_doubleword_align (enum machine_mode mode, const_tree type)
5848830f 4448{
65a939f7
PB
4449 return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
4450 || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
5848830f
PB
4451}
4452
4453
82e9d970
PB
4454/* Determine where to put an argument to a function.
4455 Value is zero to push the argument on the stack,
4456 or a hard register in which to store the argument.
4457
4458 MODE is the argument's machine mode.
4459 TYPE is the data type of the argument (as a tree).
4460 This is null for libcalls where that information may
4461 not be available.
4462 CUM is a variable of type CUMULATIVE_ARGS which gives info about
4463 the preceding args and about the function being called.
4464 NAMED is nonzero if this argument is a named parameter
9c6a2bee 4465 (otherwise it is an extra parameter matching an ellipsis).
1d6e90ac 4466
9c6a2bee
NF
4467 On the ARM, normally the first 16 bytes are passed in registers r0-r3; all
4468 other arguments are passed on the stack. If (NAMED == 0) (which happens
4469 only in assign_parms, since TARGET_SETUP_INCOMING_VARARGS is
4470 defined), say it is passed in the stack (function_prologue will
4471 indeed make it pass in the stack if necessary). */
4472
4473static rtx
e32bac5b 4474arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
9c6a2bee 4475 const_tree type, bool named)
82e9d970 4476{
5848830f
PB
4477 int nregs;
4478
390b17c2
RE
4479 /* Handle the special case quickly. Pick an arbitrary value for op2 of
4480 a call insn (op3 of a call_value insn). */
4481 if (mode == VOIDmode)
4482 return const0_rtx;
4483
4484 if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4485 {
4486 aapcs_layout_arg (pcum, mode, type, named);
4487 return pcum->aapcs_reg;
4488 }
4489
5848830f
PB
4490 /* Varargs vectors are treated the same as long long.
4491 named_count avoids having to change the way arm handles 'named' */
4492 if (TARGET_IWMMXT_ABI
f676971a 4493 && arm_vector_mode_supported_p (mode)
5848830f 4494 && pcum->named_count > pcum->nargs + 1)
5a9335ef 4495 {
5848830f
PB
4496 if (pcum->iwmmxt_nregs <= 9)
4497 return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
4498 else
5a9335ef 4499 {
5848830f
PB
4500 pcum->can_split = false;
4501 return NULL_RTX;
5a9335ef 4502 }
5a9335ef
NC
4503 }
4504
5848830f
PB
4505 /* Put doubleword aligned quantities in even register pairs. */
4506 if (pcum->nregs & 1
4507 && ARM_DOUBLEWORD_ALIGN
4508 && arm_needs_doubleword_align (mode, type))
4509 pcum->nregs++;
4510
666c27b9 4511 /* Only allow splitting an arg between regs and memory if all preceding
5848830f
PB
4512 args were allocated to regs. For args passed by reference we only count
4513 the reference pointer. */
4514 if (pcum->can_split)
4515 nregs = 1;
4516 else
4517 nregs = ARM_NUM_REGS2 (mode, type);
4518
4519 if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
82e9d970 4520 return NULL_RTX;
f676971a 4521
82e9d970
PB
4522 return gen_rtx_REG (mode, pcum->nregs);
4523}
1741620c 4524
c2ed6cf8
NF
4525static unsigned int
4526arm_function_arg_boundary (enum machine_mode mode, const_tree type)
4527{
4528 return (ARM_DOUBLEWORD_ALIGN && arm_needs_doubleword_align (mode, type)
4529 ? DOUBLEWORD_ALIGNMENT
4530 : PARM_BOUNDARY);
4531}
4532
78a52f11
RH
4533static int
4534arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
390b17c2 4535 tree type, bool named)
78a52f11
RH
4536{
4537 int nregs = pcum->nregs;
4538
390b17c2
RE
4539 if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4540 {
4541 aapcs_layout_arg (pcum, mode, type, named);
4542 return pcum->aapcs_partial;
4543 }
4544
88f77cba 4545 if (TARGET_IWMMXT_ABI && arm_vector_mode_supported_p (mode))
78a52f11
RH
4546 return 0;
4547
4548 if (NUM_ARG_REGS > nregs
4549 && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
4550 && pcum->can_split)
4551 return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
4552
4553 return 0;
4554}
4555
9c6a2bee
NF
4556/* Update the data in PCUM to advance over an argument
4557 of mode MODE and data type TYPE.
4558 (TYPE is null for libcalls where that information may not be available.) */
4559
4560static void
390b17c2 4561arm_function_arg_advance (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
9c6a2bee 4562 const_tree type, bool named)
390b17c2
RE
4563{
4564 if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4565 {
4566 aapcs_layout_arg (pcum, mode, type, named);
4567
4568 if (pcum->aapcs_cprc_slot >= 0)
4569 {
4570 aapcs_cp_arg_layout[pcum->aapcs_cprc_slot].advance (pcum, mode,
4571 type);
4572 pcum->aapcs_cprc_slot = -1;
4573 }
4574
4575 /* Generic stuff. */
4576 pcum->aapcs_arg_processed = false;
4577 pcum->aapcs_ncrn = pcum->aapcs_next_ncrn;
4578 pcum->aapcs_reg = NULL_RTX;
4579 pcum->aapcs_partial = 0;
4580 }
4581 else
4582 {
4583 pcum->nargs += 1;
4584 if (arm_vector_mode_supported_p (mode)
4585 && pcum->named_count > pcum->nargs
4586 && TARGET_IWMMXT_ABI)
4587 pcum->iwmmxt_nregs += 1;
4588 else
4589 pcum->nregs += ARM_NUM_REGS2 (mode, type);
4590 }
4591}
4592
1741620c
JD
4593/* Variable sized types are passed by reference. This is a GCC
4594 extension to the ARM ABI. */
4595
8cd5a4e0
RH
4596static bool
4597arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
4598 enum machine_mode mode ATTRIBUTE_UNUSED,
586de218 4599 const_tree type, bool named ATTRIBUTE_UNUSED)
1741620c
JD
4600{
4601 return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
4602}
82e9d970 4603\f
c27ba912
DM
4604/* Encode the current state of the #pragma [no_]long_calls. */
4605typedef enum
82e9d970 4606{
6fc0bb99 4607 OFF, /* No #pragma [no_]long_calls is in effect. */
c27ba912
DM
4608 LONG, /* #pragma long_calls is in effect. */
4609 SHORT /* #pragma no_long_calls is in effect. */
4610} arm_pragma_enum;
82e9d970 4611
c27ba912 4612static arm_pragma_enum arm_pragma_long_calls = OFF;
82e9d970 4613
8b97c5f8 4614void
e32bac5b 4615arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
82e9d970 4616{
8b97c5f8
ZW
4617 arm_pragma_long_calls = LONG;
4618}
4619
4620void
e32bac5b 4621arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
8b97c5f8
ZW
4622{
4623 arm_pragma_long_calls = SHORT;
4624}
4625
4626void
e32bac5b 4627arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
8b97c5f8
ZW
4628{
4629 arm_pragma_long_calls = OFF;
82e9d970
PB
4630}
4631\f
91d231cb
JM
4632/* Handle an attribute requiring a FUNCTION_DECL;
4633 arguments as in struct attribute_spec.handler. */
4634static tree
e32bac5b
RE
4635arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
4636 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
91d231cb
JM
4637{
4638 if (TREE_CODE (*node) != FUNCTION_DECL)
4639 {
29d08eba
JM
4640 warning (OPT_Wattributes, "%qE attribute only applies to functions",
4641 name);
91d231cb
JM
4642 *no_add_attrs = true;
4643 }
4644
4645 return NULL_TREE;
4646}
4647
4648/* Handle an "interrupt" or "isr" attribute;
4649 arguments as in struct attribute_spec.handler. */
4650static tree
e32bac5b
RE
4651arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
4652 bool *no_add_attrs)
91d231cb
JM
4653{
4654 if (DECL_P (*node))
4655 {
4656 if (TREE_CODE (*node) != FUNCTION_DECL)
4657 {
29d08eba
JM
4658 warning (OPT_Wattributes, "%qE attribute only applies to functions",
4659 name);
91d231cb
JM
4660 *no_add_attrs = true;
4661 }
4662 /* FIXME: the argument if any is checked for type attributes;
4663 should it be checked for decl ones? */
4664 }
4665 else
4666 {
4667 if (TREE_CODE (*node) == FUNCTION_TYPE
4668 || TREE_CODE (*node) == METHOD_TYPE)
4669 {
4670 if (arm_isr_value (args) == ARM_FT_UNKNOWN)
4671 {
29d08eba
JM
4672 warning (OPT_Wattributes, "%qE attribute ignored",
4673 name);
91d231cb
JM
4674 *no_add_attrs = true;
4675 }
4676 }
4677 else if (TREE_CODE (*node) == POINTER_TYPE
4678 && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
4679 || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
4680 && arm_isr_value (args) != ARM_FT_UNKNOWN)
4681 {
8dd16ecc 4682 *node = build_variant_type_copy (*node);
1d6e90ac
NC
4683 TREE_TYPE (*node) = build_type_attribute_variant
4684 (TREE_TYPE (*node),
4685 tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
91d231cb
JM
4686 *no_add_attrs = true;
4687 }
4688 else
4689 {
4690 /* Possibly pass this attribute on from the type to a decl. */
4691 if (flags & ((int) ATTR_FLAG_DECL_NEXT
4692 | (int) ATTR_FLAG_FUNCTION_NEXT
4693 | (int) ATTR_FLAG_ARRAY_NEXT))
4694 {
4695 *no_add_attrs = true;
4696 return tree_cons (name, args, NULL_TREE);
4697 }
4698 else
4699 {
29d08eba
JM
4700 warning (OPT_Wattributes, "%qE attribute ignored",
4701 name);
91d231cb
JM
4702 }
4703 }
4704 }
4705
4706 return NULL_TREE;
82e9d970
PB
4707}
4708
390b17c2
RE
4709/* Handle a "pcs" attribute; arguments as in struct
4710 attribute_spec.handler. */
4711static tree
4712arm_handle_pcs_attribute (tree *node ATTRIBUTE_UNUSED, tree name, tree args,
4713 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4714{
4715 if (arm_pcs_from_attribute (args) == ARM_PCS_UNKNOWN)
4716 {
a9717079 4717 warning (OPT_Wattributes, "%qE attribute ignored", name);
390b17c2
RE
4718 *no_add_attrs = true;
4719 }
4720 return NULL_TREE;
4721}
4722
7bff66a7 4723#if TARGET_DLLIMPORT_DECL_ATTRIBUTES
04fb56d5
MM
4724/* Handle the "notshared" attribute. This attribute is another way of
4725 requesting hidden visibility. ARM's compiler supports
4726 "__declspec(notshared)"; we support the same thing via an
4727 attribute. */
4728
4729static tree
e0b92319
NC
4730arm_handle_notshared_attribute (tree *node,
4731 tree name ATTRIBUTE_UNUSED,
4732 tree args ATTRIBUTE_UNUSED,
4733 int flags ATTRIBUTE_UNUSED,
04fb56d5
MM
4734 bool *no_add_attrs)
4735{
4736 tree decl = TYPE_NAME (*node);
4737
4738 if (decl)
4739 {
4740 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
4741 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4742 *no_add_attrs = false;
4743 }
4744 return NULL_TREE;
4745}
7bff66a7 4746#endif
04fb56d5 4747
82e9d970
PB
4748/* Return 0 if the attributes for two types are incompatible, 1 if they
4749 are compatible, and 2 if they are nearly compatible (which causes a
4750 warning to be generated). */
8d8e52be 4751static int
3101faab 4752arm_comp_type_attributes (const_tree type1, const_tree type2)
82e9d970 4753{
1cb8d58a 4754 int l1, l2, s1, s2;
f676971a 4755
82e9d970
PB
4756 /* Check for mismatch of non-default calling convention. */
4757 if (TREE_CODE (type1) != FUNCTION_TYPE)
4758 return 1;
4759
4760 /* Check for mismatched call attributes. */
1cb8d58a
NC
4761 l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
4762 l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
4763 s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
4764 s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
bd7fc26f
NC
4765
4766 /* Only bother to check if an attribute is defined. */
4767 if (l1 | l2 | s1 | s2)
4768 {
4769 /* If one type has an attribute, the other must have the same attribute. */
1cb8d58a 4770 if ((l1 != l2) || (s1 != s2))
bd7fc26f 4771 return 0;
82e9d970 4772
bd7fc26f
NC
4773 /* Disallow mixed attributes. */
4774 if ((l1 & s2) || (l2 & s1))
4775 return 0;
4776 }
f676971a 4777
6d3d9133
NC
4778 /* Check for mismatched ISR attribute. */
4779 l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
4780 if (! l1)
4781 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
4782 l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
4783 if (! l2)
4784 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
4785 if (l1 != l2)
4786 return 0;
4787
bd7fc26f 4788 return 1;
82e9d970
PB
4789}
4790
c27ba912
DM
4791/* Assigns default attributes to newly defined type. This is used to
4792 set short_call/long_call attributes for function types of
4793 functions defined inside corresponding #pragma scopes. */
8d8e52be 4794static void
e32bac5b 4795arm_set_default_type_attributes (tree type)
c27ba912
DM
4796{
4797 /* Add __attribute__ ((long_call)) to all functions, when
4798 inside #pragma long_calls or __attribute__ ((short_call)),
4799 when inside #pragma no_long_calls. */
4800 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
4801 {
4802 tree type_attr_list, attr_name;
4803 type_attr_list = TYPE_ATTRIBUTES (type);
4804
4805 if (arm_pragma_long_calls == LONG)
4806 attr_name = get_identifier ("long_call");
4807 else if (arm_pragma_long_calls == SHORT)
4808 attr_name = get_identifier ("short_call");
4809 else
4810 return;
4811
4812 type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
4813 TYPE_ATTRIBUTES (type) = type_attr_list;
4814 }
4815}
4816\f
25a65198
RS
4817/* Return true if DECL is known to be linked into section SECTION. */
4818
4819static bool
4820arm_function_in_section_p (tree decl, section *section)
c27ba912 4821{
25a65198
RS
4822 /* We can only be certain about functions defined in the same
4823 compilation unit. */
4824 if (!TREE_STATIC (decl))
4825 return false;
c27ba912 4826
25a65198
RS
4827 /* Make sure that SYMBOL always binds to the definition in this
4828 compilation unit. */
4829 if (!targetm.binds_local_p (decl))
4830 return false;
c27ba912 4831
25a65198
RS
4832 /* If DECL_SECTION_NAME is set, assume it is trustworthy. */
4833 if (!DECL_SECTION_NAME (decl))
4834 {
25a65198
RS
4835 /* Make sure that we will not create a unique section for DECL. */
4836 if (flag_function_sections || DECL_ONE_ONLY (decl))
4837 return false;
4838 }
4839
4840 return function_section (decl) == section;
c27ba912
DM
4841}
4842
a50aa827 4843/* Return nonzero if a 32-bit "long_call" should be generated for
25a65198
RS
4844 a call from the current function to DECL. We generate a long_call
4845 if the function:
c27ba912
DM
4846
4847 a. has an __attribute__((long call))
4848 or b. is within the scope of a #pragma long_calls
4849 or c. the -mlong-calls command line switch has been specified
4850
4851 However we do not generate a long call if the function:
f676971a 4852
c27ba912
DM
4853 d. has an __attribute__ ((short_call))
4854 or e. is inside the scope of a #pragma no_long_calls
25a65198 4855 or f. is defined in the same section as the current function. */
c27ba912 4856
25a65198
RS
4857bool
4858arm_is_long_call_p (tree decl)
4859{
4860 tree attrs;
c27ba912 4861
25a65198
RS
4862 if (!decl)
4863 return TARGET_LONG_CALLS;
c27ba912 4864
25a65198
RS
4865 attrs = TYPE_ATTRIBUTES (TREE_TYPE (decl));
4866 if (lookup_attribute ("short_call", attrs))
4867 return false;
c27ba912 4868
25a65198
RS
4869 /* For "f", be conservative, and only cater for cases in which the
4870 whole of the current function is placed in the same section. */
4871 if (!flag_reorder_blocks_and_partition
b3a796bc 4872 && TREE_CODE (decl) == FUNCTION_DECL
25a65198
RS
4873 && arm_function_in_section_p (decl, current_function_section ()))
4874 return false;
a77655b1 4875
25a65198
RS
4876 if (lookup_attribute ("long_call", attrs))
4877 return true;
f676971a 4878
25a65198 4879 return TARGET_LONG_CALLS;
c27ba912 4880}
f99fce0c 4881
825dda42 4882/* Return nonzero if it is ok to make a tail-call to DECL. */
4977bab6 4883static bool
390b17c2 4884arm_function_ok_for_sibcall (tree decl, tree exp)
f99fce0c 4885{
5b3e6663 4886 unsigned long func_type;
f99fce0c 4887
5a9335ef
NC
4888 if (cfun->machine->sibcall_blocked)
4889 return false;
4890
f99fce0c 4891 /* Never tailcall something for which we have no decl, or if we
7c19c715
JB
4892 are generating code for Thumb-1. */
4893 if (decl == NULL || TARGET_THUMB1)
4977bab6 4894 return false;
f99fce0c 4895
9403b7f7
RS
4896 /* The PIC register is live on entry to VxWorks PLT entries, so we
4897 must make the call before restoring the PIC register. */
4898 if (TARGET_VXWORKS_RTP && flag_pic && !targetm.binds_local_p (decl))
4899 return false;
4900
f99fce0c 4901 /* Cannot tail-call to long calls, since these are out of range of
25a65198
RS
4902 a branch instruction. */
4903 if (arm_is_long_call_p (decl))
4977bab6 4904 return false;
f99fce0c
RE
4905
4906 /* If we are interworking and the function is not declared static
f676971a 4907 then we can't tail-call it unless we know that it exists in this
f99fce0c 4908 compilation unit (since it might be a Thumb routine). */
5895f793 4909 if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
4977bab6 4910 return false;
f99fce0c 4911
5b3e6663 4912 func_type = arm_current_func_type ();
6d3d9133 4913 /* Never tailcall from an ISR routine - it needs a special exit sequence. */
5b3e6663
PB
4914 if (IS_INTERRUPT (func_type))
4915 return false;
4916
390b17c2
RE
4917 if (!VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
4918 {
4919 /* Check that the return value locations are the same. For
4920 example that we aren't returning a value from the sibling in
4921 a VFP register but then need to transfer it to a core
4922 register. */
4923 rtx a, b;
4924
4925 a = arm_function_value (TREE_TYPE (exp), decl, false);
4926 b = arm_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
4927 cfun->decl, false);
4928 if (!rtx_equal_p (a, b))
4929 return false;
4930 }
4931
5b3e6663
PB
4932 /* Never tailcall if function may be called with a misaligned SP. */
4933 if (IS_STACKALIGN (func_type))
4977bab6 4934 return false;
6d3d9133 4935
f99fce0c 4936 /* Everything else is ok. */
4977bab6 4937 return true;
f99fce0c
RE
4938}
4939
82e9d970 4940\f
6b990f6b
RE
4941/* Addressing mode support functions. */
4942
0b4be7de 4943/* Return nonzero if X is a legitimate immediate operand when compiling
020a4035 4944 for PIC. We know that X satisfies CONSTANT_P and flag_pic is true. */
32de079a 4945int
e32bac5b 4946legitimate_pic_operand_p (rtx x)
32de079a 4947{
020a4035
RE
4948 if (GET_CODE (x) == SYMBOL_REF
4949 || (GET_CODE (x) == CONST
4950 && GET_CODE (XEXP (x, 0)) == PLUS
4951 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
32de079a
RE
4952 return 0;
4953
4954 return 1;
4955}
4956
9403b7f7
RS
4957/* Record that the current function needs a PIC register. Initialize
4958 cfun->machine->pic_reg if we have not already done so. */
4959
4960static void
4961require_pic_register (void)
4962{
4963 /* A lot of the logic here is made obscure by the fact that this
4964 routine gets called as part of the rtx cost estimation process.
4965 We don't want those calls to affect any assumptions about the real
4966 function; and further, we can't call entry_of_function() until we
4967 start the real expansion process. */
e3b5732b 4968 if (!crtl->uses_pic_offset_table)
9403b7f7 4969 {
b3a13419 4970 gcc_assert (can_create_pseudo_p ());
9403b7f7
RS
4971 if (arm_pic_register != INVALID_REGNUM)
4972 {
6d2538f5
JB
4973 if (!cfun->machine->pic_reg)
4974 cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
9403b7f7
RS
4975
4976 /* Play games to avoid marking the function as needing pic
4977 if we are being called as part of the cost-estimation
4978 process. */
04ef80ce 4979 if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
e3b5732b 4980 crtl->uses_pic_offset_table = 1;
9403b7f7
RS
4981 }
4982 else
4983 {
cb227aa9 4984 rtx seq, insn;
9403b7f7 4985
6d2538f5
JB
4986 if (!cfun->machine->pic_reg)
4987 cfun->machine->pic_reg = gen_reg_rtx (Pmode);
9403b7f7
RS
4988
4989 /* Play games to avoid marking the function as needing pic
4990 if we are being called as part of the cost-estimation
4991 process. */
04ef80ce 4992 if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
9403b7f7 4993 {
e3b5732b 4994 crtl->uses_pic_offset_table = 1;
9403b7f7
RS
4995 start_sequence ();
4996
4997 arm_load_pic_register (0UL);
4998
4999 seq = get_insns ();
5000 end_sequence ();
cb227aa9
UW
5001
5002 for (insn = seq; insn; insn = NEXT_INSN (insn))
5003 if (INSN_P (insn))
5004 INSN_LOCATOR (insn) = prologue_locator;
5005
af618949
MM
5006 /* We can be called during expansion of PHI nodes, where
5007 we can't yet emit instructions directly in the final
5008 insn stream. Queue the insns on the entry edge, they will
5009 be committed after everything else is expanded. */
5010 insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
9403b7f7
RS
5011 }
5012 }
5013 }
5014}
5015
32de079a 5016rtx
e32bac5b 5017legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
32de079a 5018{
a3c48721
RE
5019 if (GET_CODE (orig) == SYMBOL_REF
5020 || GET_CODE (orig) == LABEL_REF)
32de079a 5021 {
32de079a 5022 rtx insn;
020a4035 5023
32de079a
RE
5024 if (reg == 0)
5025 {
b3a13419 5026 gcc_assert (can_create_pseudo_p ());
e6d29d15 5027 reg = gen_reg_rtx (Pmode);
32de079a 5028 }
32de079a 5029
9403b7f7
RS
5030 /* VxWorks does not impose a fixed gap between segments; the run-time
5031 gap can be different from the object-file gap. We therefore can't
5032 use GOTOFF unless we are absolutely sure that the symbol is in the
5033 same segment as the GOT. Unfortunately, the flexibility of linker
5034 scripts means that we can't be sure of that in general, so assume
5035 that GOTOFF is never valid on VxWorks. */
14f583b8 5036 if ((GET_CODE (orig) == LABEL_REF
f676971a 5037 || (GET_CODE (orig) == SYMBOL_REF &&
94428622 5038 SYMBOL_REF_LOCAL_P (orig)))
9403b7f7
RS
5039 && NEED_GOT_RELOC
5040 && !TARGET_VXWORKS_RTP)
85c9bcd4 5041 insn = arm_pic_static_addr (orig, reg);
a3c48721
RE
5042 else
5043 {
d37c3c62
MK
5044 rtx pat;
5045 rtx mem;
5046
85c9bcd4
WG
5047 /* If this function doesn't have a pic register, create one now. */
5048 require_pic_register ();
5049
d37c3c62 5050 pat = gen_calculate_pic_address (reg, cfun->machine->pic_reg, orig);
85c9bcd4 5051
d37c3c62
MK
5052 /* Make the MEM as close to a constant as possible. */
5053 mem = SET_SRC (pat);
5054 gcc_assert (MEM_P (mem) && !MEM_VOLATILE_P (mem));
5055 MEM_READONLY_P (mem) = 1;
5056 MEM_NOTRAP_P (mem) = 1;
5057
5058 insn = emit_insn (pat);
a3c48721
RE
5059 }
5060
32de079a
RE
5061 /* Put a REG_EQUAL note on this insn, so that it can be optimized
5062 by loop. */
bd94cb6e
SB
5063 set_unique_reg_note (insn, REG_EQUAL, orig);
5064
32de079a
RE
5065 return reg;
5066 }
5067 else if (GET_CODE (orig) == CONST)
5068 {
5069 rtx base, offset;
5070
5071 if (GET_CODE (XEXP (orig, 0)) == PLUS
020a4035 5072 && XEXP (XEXP (orig, 0), 0) == cfun->machine->pic_reg)
32de079a
RE
5073 return orig;
5074
f67358da 5075 /* Handle the case where we have: const (UNSPEC_TLS). */
d3585b76
DJ
5076 if (GET_CODE (XEXP (orig, 0)) == UNSPEC
5077 && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
5078 return orig;
5079
f67358da
PB
5080 /* Handle the case where we have:
5081 const (plus (UNSPEC_TLS) (ADDEND)). The ADDEND must be a
5082 CONST_INT. */
5083 if (GET_CODE (XEXP (orig, 0)) == PLUS
5084 && GET_CODE (XEXP (XEXP (orig, 0), 0)) == UNSPEC
5085 && XINT (XEXP (XEXP (orig, 0), 0), 1) == UNSPEC_TLS)
5086 {
5087 gcc_assert (GET_CODE (XEXP (XEXP (orig, 0), 1)) == CONST_INT);
5088 return orig;
5089 }
5090
32de079a
RE
5091 if (reg == 0)
5092 {
b3a13419 5093 gcc_assert (can_create_pseudo_p ());
e6d29d15 5094 reg = gen_reg_rtx (Pmode);
32de079a
RE
5095 }
5096
e6d29d15 5097 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
e0b92319 5098
e6d29d15
NS
5099 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
5100 offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
5101 base == reg ? 0 : reg);
32de079a
RE
5102
5103 if (GET_CODE (offset) == CONST_INT)
5104 {
5105 /* The base register doesn't really matter, we only want to
5106 test the index for the appropriate mode. */
1e1ab407 5107 if (!arm_legitimate_index_p (mode, offset, SET, 0))
6b990f6b 5108 {
b3a13419 5109 gcc_assert (can_create_pseudo_p ());
e6d29d15 5110 offset = force_reg (Pmode, offset);
6b990f6b 5111 }
32de079a 5112
32de079a 5113 if (GET_CODE (offset) == CONST_INT)
ed8908e7 5114 return plus_constant (base, INTVAL (offset));
32de079a
RE
5115 }
5116
5117 if (GET_MODE_SIZE (mode) > 4
5118 && (GET_MODE_CLASS (mode) == MODE_INT
5119 || TARGET_SOFT_FLOAT))
5120 {
5121 emit_insn (gen_addsi3 (reg, base, offset));
5122 return reg;
5123 }
5124
43cffd11 5125 return gen_rtx_PLUS (Pmode, base, offset);
32de079a 5126 }
32de079a
RE
5127
5128 return orig;
5129}
5130
57934c39 5131
5b3e6663 5132/* Find a spare register to use during the prolog of a function. */
57934c39
PB
5133
5134static int
b279b20a 5135thumb_find_work_register (unsigned long pushed_regs_mask)
57934c39
PB
5136{
5137 int reg;
5138
b279b20a
NC
5139 /* Check the argument registers first as these are call-used. The
5140 register allocation order means that sometimes r3 might be used
5141 but earlier argument registers might not, so check them all. */
5142 for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
6fb5fa3c 5143 if (!df_regs_ever_live_p (reg))
b279b20a
NC
5144 return reg;
5145
5146 /* Before going on to check the call-saved registers we can try a couple
5147 more ways of deducing that r3 is available. The first is when we are
5148 pushing anonymous arguments onto the stack and we have less than 4
5149 registers worth of fixed arguments(*). In this case r3 will be part of
5150 the variable argument list and so we can be sure that it will be
5151 pushed right at the start of the function. Hence it will be available
5152 for the rest of the prologue.
38173d38 5153 (*): ie crtl->args.pretend_args_size is greater than 0. */
b279b20a 5154 if (cfun->machine->uses_anonymous_args
38173d38 5155 && crtl->args.pretend_args_size > 0)
57934c39
PB
5156 return LAST_ARG_REGNUM;
5157
b279b20a
NC
5158 /* The other case is when we have fixed arguments but less than 4 registers
5159 worth. In this case r3 might be used in the body of the function, but
5160 it is not being used to convey an argument into the function. In theory
38173d38 5161 we could just check crtl->args.size to see how many bytes are
b279b20a
NC
5162 being passed in argument registers, but it seems that it is unreliable.
5163 Sometimes it will have the value 0 when in fact arguments are being
5164 passed. (See testcase execute/20021111-1.c for an example). So we also
5165 check the args_info.nregs field as well. The problem with this field is
5166 that it makes no allowances for arguments that are passed to the
5167 function but which are not used. Hence we could miss an opportunity
5168 when a function has an unused argument in r3. But it is better to be
5169 safe than to be sorry. */
5170 if (! cfun->machine->uses_anonymous_args
38173d38
JH
5171 && crtl->args.size >= 0
5172 && crtl->args.size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
f6d2671e 5173 && crtl->args.info.nregs < 4)
b279b20a 5174 return LAST_ARG_REGNUM;
e0b92319 5175
b279b20a
NC
5176 /* Otherwise look for a call-saved register that is going to be pushed. */
5177 for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
5178 if (pushed_regs_mask & (1 << reg))
57934c39
PB
5179 return reg;
5180
5b3e6663
PB
5181 if (TARGET_THUMB2)
5182 {
5183 /* Thumb-2 can use high regs. */
5184 for (reg = FIRST_HI_REGNUM; reg < 15; reg ++)
5185 if (pushed_regs_mask & (1 << reg))
5186 return reg;
5187 }
b279b20a
NC
5188 /* Something went wrong - thumb_compute_save_reg_mask()
5189 should have arranged for a suitable register to be pushed. */
e6d29d15 5190 gcc_unreachable ();
57934c39
PB
5191}
5192
f16fe45f 5193static GTY(()) int pic_labelno;
876f13b0 5194
fe013435
PB
5195/* Generate code to load the PIC register. In thumb mode SCRATCH is a
5196 low register. */
876f13b0 5197
32de079a 5198void
e55ef7f4 5199arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
32de079a 5200{
f9bd1a89 5201 rtx l1, labelno, pic_tmp, pic_rtx, pic_reg;
32de079a 5202
e3b5732b 5203 if (crtl->uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
32de079a
RE
5204 return;
5205
e6d29d15 5206 gcc_assert (flag_pic);
32de079a 5207
9403b7f7
RS
5208 pic_reg = cfun->machine->pic_reg;
5209 if (TARGET_VXWORKS_RTP)
5210 {
5211 pic_rtx = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE);
5212 pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
87d05b44 5213 emit_insn (gen_pic_load_addr_32bit (pic_reg, pic_rtx));
43cffd11 5214
9403b7f7 5215 emit_insn (gen_rtx_SET (Pmode, pic_reg, gen_rtx_MEM (Pmode, pic_reg)));
f676971a 5216
9403b7f7
RS
5217 pic_tmp = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
5218 emit_insn (gen_pic_offset_arm (pic_reg, pic_reg, pic_tmp));
4bec9f7d 5219 }
9403b7f7 5220 else
5b3e6663 5221 {
9403b7f7
RS
5222 /* We use an UNSPEC rather than a LABEL_REF because this label
5223 never appears in the code stream. */
5224
5225 labelno = GEN_INT (pic_labelno++);
5226 l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
5227 l1 = gen_rtx_CONST (VOIDmode, l1);
5228
9403b7f7
RS
5229 /* On the ARM the PC register contains 'dot + 8' at the time of the
5230 addition, on the Thumb it is 'dot + 4'. */
f9bd1a89
RS
5231 pic_rtx = plus_constant (l1, TARGET_ARM ? 8 : 4);
5232 pic_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, pic_rtx),
5233 UNSPEC_GOTSYM_OFF);
9403b7f7
RS
5234 pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
5235
87d05b44 5236 if (TARGET_32BIT)
9403b7f7 5237 {
87d05b44
RE
5238 emit_insn (gen_pic_load_addr_32bit (pic_reg, pic_rtx));
5239 if (TARGET_ARM)
5240 emit_insn (gen_pic_add_dot_plus_eight (pic_reg, pic_reg, labelno));
9403b7f7 5241 else
87d05b44 5242 emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
9403b7f7
RS
5243 }
5244 else /* TARGET_THUMB1 */
876f13b0 5245 {
9403b7f7
RS
5246 if (arm_pic_register != INVALID_REGNUM
5247 && REGNO (pic_reg) > LAST_LO_REGNUM)
5248 {
5249 /* We will have pushed the pic register, so we should always be
5250 able to find a work register. */
5251 pic_tmp = gen_rtx_REG (SImode,
5252 thumb_find_work_register (saved_regs));
5253 emit_insn (gen_pic_load_addr_thumb1 (pic_tmp, pic_rtx));
5254 emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
5255 }
5256 else
5257 emit_insn (gen_pic_load_addr_thumb1 (pic_reg, pic_rtx));
5258 emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
876f13b0 5259 }
4bec9f7d 5260 }
32de079a 5261
32de079a
RE
5262 /* Need to emit this whether or not we obey regdecls,
5263 since setjmp/longjmp can cause life info to screw up. */
c41c1387 5264 emit_use (pic_reg);
32de079a
RE
5265}
5266
85c9bcd4
WG
5267/* Generate code to load the address of a static var when flag_pic is set. */
5268static rtx
5269arm_pic_static_addr (rtx orig, rtx reg)
5270{
5271 rtx l1, labelno, offset_rtx, insn;
5272
5273 gcc_assert (flag_pic);
5274
5275 /* We use an UNSPEC rather than a LABEL_REF because this label
5276 never appears in the code stream. */
5277 labelno = GEN_INT (pic_labelno++);
5278 l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
5279 l1 = gen_rtx_CONST (VOIDmode, l1);
5280
5281 /* On the ARM the PC register contains 'dot + 8' at the time of the
5282 addition, on the Thumb it is 'dot + 4'. */
5283 offset_rtx = plus_constant (l1, TARGET_ARM ? 8 : 4);
5284 offset_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, orig, offset_rtx),
5285 UNSPEC_SYMBOL_OFFSET);
5286 offset_rtx = gen_rtx_CONST (Pmode, offset_rtx);
5287
5288 if (TARGET_32BIT)
5289 {
5290 emit_insn (gen_pic_load_addr_32bit (reg, offset_rtx));
5291 if (TARGET_ARM)
5292 insn = emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
5293 else
5294 insn = emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5295 }
5296 else /* TARGET_THUMB1 */
5297 {
5298 emit_insn (gen_pic_load_addr_thumb1 (reg, offset_rtx));
5299 insn = emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5300 }
5301
5302 return insn;
5303}
876f13b0 5304
6b990f6b
RE
5305/* Return nonzero if X is valid as an ARM state addressing register. */
5306static int
e32bac5b 5307arm_address_register_rtx_p (rtx x, int strict_p)
6b990f6b
RE
5308{
5309 int regno;
5310
5311 if (GET_CODE (x) != REG)
5312 return 0;
5313
5314 regno = REGNO (x);
5315
5316 if (strict_p)
5317 return ARM_REGNO_OK_FOR_BASE_P (regno);
5318
5319 return (regno <= LAST_ARM_REGNUM
5320 || regno >= FIRST_PSEUDO_REGISTER
5321 || regno == FRAME_POINTER_REGNUM
5322 || regno == ARG_POINTER_REGNUM);
5323}
5324
d3585b76
DJ
5325/* Return TRUE if this rtx is the difference of a symbol and a label,
5326 and will reduce to a PC-relative relocation in the object file.
5327 Expressions like this can be left alone when generating PIC, rather
5328 than forced through the GOT. */
5329static int
5330pcrel_constant_p (rtx x)
5331{
5332 if (GET_CODE (x) == MINUS)
5333 return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
5334
5335 return FALSE;
5336}
5337
d37c3c62
MK
5338/* Return true if X will surely end up in an index register after next
5339 splitting pass. */
5340static bool
5341will_be_in_index_register (const_rtx x)
5342{
5343 /* arm.md: calculate_pic_address will split this into a register. */
5344 return GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_SYM;
5345}
5346
6b990f6b
RE
5347/* Return nonzero if X is a valid ARM state address operand. */
5348int
c6c3dba9
PB
5349arm_legitimate_address_outer_p (enum machine_mode mode, rtx x, RTX_CODE outer,
5350 int strict_p)
6b990f6b 5351{
fdd695fd
PB
5352 bool use_ldrd;
5353 enum rtx_code code = GET_CODE (x);
f676971a 5354
6b990f6b
RE
5355 if (arm_address_register_rtx_p (x, strict_p))
5356 return 1;
5357
fdd695fd
PB
5358 use_ldrd = (TARGET_LDRD
5359 && (mode == DImode
5360 || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
5361
5362 if (code == POST_INC || code == PRE_DEC
5363 || ((code == PRE_INC || code == POST_DEC)
5364 && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
6b990f6b
RE
5365 return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
5366
fdd695fd 5367 else if ((code == POST_MODIFY || code == PRE_MODIFY)
6b990f6b
RE
5368 && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
5369 && GET_CODE (XEXP (x, 1)) == PLUS
386d3a16 5370 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
fdd695fd
PB
5371 {
5372 rtx addend = XEXP (XEXP (x, 1), 1);
5373
112cdef5 5374 /* Don't allow ldrd post increment by register because it's hard
fdd695fd
PB
5375 to fixup invalid register choices. */
5376 if (use_ldrd
5377 && GET_CODE (x) == POST_MODIFY
5378 && GET_CODE (addend) == REG)
5379 return 0;
5380
5381 return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
5382 && arm_legitimate_index_p (mode, addend, outer, strict_p));
5383 }
6b990f6b
RE
5384
5385 /* After reload constants split into minipools will have addresses
5386 from a LABEL_REF. */
0bfb39ef 5387 else if (reload_completed
fdd695fd
PB
5388 && (code == LABEL_REF
5389 || (code == CONST
6b990f6b
RE
5390 && GET_CODE (XEXP (x, 0)) == PLUS
5391 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5392 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5393 return 1;
5394
88f77cba 5395 else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
6b990f6b
RE
5396 return 0;
5397
fdd695fd 5398 else if (code == PLUS)
6b990f6b
RE
5399 {
5400 rtx xop0 = XEXP (x, 0);
5401 rtx xop1 = XEXP (x, 1);
5402
5403 return ((arm_address_register_rtx_p (xop0, strict_p)
d37c3c62
MK
5404 && ((GET_CODE(xop1) == CONST_INT
5405 && arm_legitimate_index_p (mode, xop1, outer, strict_p))
5406 || (!strict_p && will_be_in_index_register (xop1))))
6b990f6b 5407 || (arm_address_register_rtx_p (xop1, strict_p)
1e1ab407 5408 && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
6b990f6b
RE
5409 }
5410
5411#if 0
5412 /* Reload currently can't handle MINUS, so disable this for now */
5413 else if (GET_CODE (x) == MINUS)
5414 {
5415 rtx xop0 = XEXP (x, 0);
5416 rtx xop1 = XEXP (x, 1);
5417
5418 return (arm_address_register_rtx_p (xop0, strict_p)
1e1ab407 5419 && arm_legitimate_index_p (mode, xop1, outer, strict_p));
6b990f6b
RE
5420 }
5421#endif
5422
5423 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
fdd695fd 5424 && code == SYMBOL_REF
6b990f6b
RE
5425 && CONSTANT_POOL_ADDRESS_P (x)
5426 && ! (flag_pic
d3585b76
DJ
5427 && symbol_mentioned_p (get_pool_constant (x))
5428 && ! pcrel_constant_p (get_pool_constant (x))))
6b990f6b
RE
5429 return 1;
5430
6b990f6b
RE
5431 return 0;
5432}
5433
5b3e6663 5434/* Return nonzero if X is a valid Thumb-2 address operand. */
c6c3dba9 5435static int
5b3e6663
PB
5436thumb2_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
5437{
5438 bool use_ldrd;
5439 enum rtx_code code = GET_CODE (x);
5440
5441 if (arm_address_register_rtx_p (x, strict_p))
5442 return 1;
5443
5444 use_ldrd = (TARGET_LDRD
5445 && (mode == DImode
5446 || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
5447
5448 if (code == POST_INC || code == PRE_DEC
5449 || ((code == PRE_INC || code == POST_DEC)
5450 && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
5451 return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
5452
5453 else if ((code == POST_MODIFY || code == PRE_MODIFY)
5454 && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
5455 && GET_CODE (XEXP (x, 1)) == PLUS
5456 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5457 {
5458 /* Thumb-2 only has autoincrement by constant. */
5459 rtx addend = XEXP (XEXP (x, 1), 1);
5460 HOST_WIDE_INT offset;
5461
5462 if (GET_CODE (addend) != CONST_INT)
5463 return 0;
5464
5465 offset = INTVAL(addend);
5466 if (GET_MODE_SIZE (mode) <= 4)
5467 return (offset > -256 && offset < 256);
5468
5469 return (use_ldrd && offset > -1024 && offset < 1024
5470 && (offset & 3) == 0);
5471 }
5472
5473 /* After reload constants split into minipools will have addresses
5474 from a LABEL_REF. */
5475 else if (reload_completed
5476 && (code == LABEL_REF
5477 || (code == CONST
5478 && GET_CODE (XEXP (x, 0)) == PLUS
5479 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5480 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5481 return 1;
5482
88f77cba 5483 else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
5b3e6663
PB
5484 return 0;
5485
5486 else if (code == PLUS)
5487 {
5488 rtx xop0 = XEXP (x, 0);
5489 rtx xop1 = XEXP (x, 1);
5490
5491 return ((arm_address_register_rtx_p (xop0, strict_p)
d37c3c62
MK
5492 && (thumb2_legitimate_index_p (mode, xop1, strict_p)
5493 || (!strict_p && will_be_in_index_register (xop1))))
5b3e6663
PB
5494 || (arm_address_register_rtx_p (xop1, strict_p)
5495 && thumb2_legitimate_index_p (mode, xop0, strict_p)));
5496 }
5497
5498 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
5499 && code == SYMBOL_REF
5500 && CONSTANT_POOL_ADDRESS_P (x)
5501 && ! (flag_pic
5502 && symbol_mentioned_p (get_pool_constant (x))
5503 && ! pcrel_constant_p (get_pool_constant (x))))
5504 return 1;
5505
5506 return 0;
5507}
5508
6b990f6b
RE
5509/* Return nonzero if INDEX is valid for an address index operand in
5510 ARM state. */
5511static int
1e1ab407
RE
5512arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
5513 int strict_p)
6b990f6b
RE
5514{
5515 HOST_WIDE_INT range;
5516 enum rtx_code code = GET_CODE (index);
5517
778ebdd9
PB
5518 /* Standard coprocessor addressing modes. */
5519 if (TARGET_HARD_FLOAT
f3fb6590
CLT
5520 && (TARGET_VFP || TARGET_FPA || TARGET_MAVERICK)
5521 && (mode == SFmode || mode == DFmode
778ebdd9 5522 || (TARGET_MAVERICK && mode == DImode)))
6b990f6b
RE
5523 return (code == CONST_INT && INTVAL (index) < 1024
5524 && INTVAL (index) > -1024
5525 && (INTVAL (index) & 3) == 0);
5526
49460951
NF
5527 /* For quad modes, we restrict the constant offset to be slightly less
5528 than what the instruction format permits. We do this because for
5529 quad mode moves, we will actually decompose them into two separate
5530 double-mode reads or writes. INDEX must therefore be a valid
5531 (double-mode) offset and so should INDEX+8. */
5532 if (TARGET_NEON && VALID_NEON_QREG_MODE (mode))
88f77cba
JB
5533 return (code == CONST_INT
5534 && INTVAL (index) < 1016
5535 && INTVAL (index) > -1024
5536 && (INTVAL (index) & 3) == 0);
5537
49460951
NF
5538 /* We have no such constraint on double mode offsets, so we permit the
5539 full range of the instruction format. */
5540 if (TARGET_NEON && VALID_NEON_DREG_MODE (mode))
5541 return (code == CONST_INT
5542 && INTVAL (index) < 1024
5543 && INTVAL (index) > -1024
5544 && (INTVAL (index) & 3) == 0);
5545
5a9335ef
NC
5546 if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
5547 return (code == CONST_INT
3657dc3e
PB
5548 && INTVAL (index) < 1024
5549 && INTVAL (index) > -1024
5550 && (INTVAL (index) & 3) == 0);
5a9335ef 5551
fdd695fd
PB
5552 if (arm_address_register_rtx_p (index, strict_p)
5553 && (GET_MODE_SIZE (mode) <= 4))
5554 return 1;
5555
5556 if (mode == DImode || mode == DFmode)
5557 {
5558 if (code == CONST_INT)
5559 {
5560 HOST_WIDE_INT val = INTVAL (index);
5561
5562 if (TARGET_LDRD)
5563 return val > -256 && val < 256;
5564 else
f372c932 5565 return val > -4096 && val < 4092;
fdd695fd
PB
5566 }
5567
5568 return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
5569 }
5570
6b990f6b 5571 if (GET_MODE_SIZE (mode) <= 4
1e1ab407
RE
5572 && ! (arm_arch4
5573 && (mode == HImode
0fd8c3ad 5574 || mode == HFmode
1e1ab407 5575 || (mode == QImode && outer == SIGN_EXTEND))))
6b990f6b 5576 {
1e1ab407
RE
5577 if (code == MULT)
5578 {
5579 rtx xiop0 = XEXP (index, 0);
5580 rtx xiop1 = XEXP (index, 1);
5581
5582 return ((arm_address_register_rtx_p (xiop0, strict_p)
5583 && power_of_two_operand (xiop1, SImode))
5584 || (arm_address_register_rtx_p (xiop1, strict_p)
5585 && power_of_two_operand (xiop0, SImode)));
5586 }
5587 else if (code == LSHIFTRT || code == ASHIFTRT
5588 || code == ASHIFT || code == ROTATERT)
5589 {
5590 rtx op = XEXP (index, 1);
6b990f6b 5591
1e1ab407
RE
5592 return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
5593 && GET_CODE (op) == CONST_INT
5594 && INTVAL (op) > 0
5595 && INTVAL (op) <= 31);
5596 }
6b990f6b
RE
5597 }
5598
1e1ab407
RE
5599 /* For ARM v4 we may be doing a sign-extend operation during the
5600 load. */
e1471c91 5601 if (arm_arch4)
1e1ab407 5602 {
0fd8c3ad
SL
5603 if (mode == HImode
5604 || mode == HFmode
5605 || (outer == SIGN_EXTEND && mode == QImode))
1e1ab407
RE
5606 range = 256;
5607 else
5608 range = 4096;
5609 }
e1471c91 5610 else
0fd8c3ad 5611 range = (mode == HImode || mode == HFmode) ? 4095 : 4096;
6b990f6b
RE
5612
5613 return (code == CONST_INT
5614 && INTVAL (index) < range
5615 && INTVAL (index) > -range);
76a318e9
RE
5616}
5617
5b3e6663
PB
5618/* Return true if OP is a valid index scaling factor for Thumb-2 address
5619 index operand. i.e. 1, 2, 4 or 8. */
5620static bool
5621thumb2_index_mul_operand (rtx op)
5622{
5623 HOST_WIDE_INT val;
5624
5625 if (GET_CODE(op) != CONST_INT)
5626 return false;
5627
5628 val = INTVAL(op);
5629 return (val == 1 || val == 2 || val == 4 || val == 8);
5630}
5631
5632/* Return nonzero if INDEX is a valid Thumb-2 address index operand. */
5633static int
5634thumb2_legitimate_index_p (enum machine_mode mode, rtx index, int strict_p)
5635{
5636 enum rtx_code code = GET_CODE (index);
5637
5638 /* ??? Combine arm and thumb2 coprocessor addressing modes. */
5639 /* Standard coprocessor addressing modes. */
5640 if (TARGET_HARD_FLOAT
f3fb6590
CLT
5641 && (TARGET_VFP || TARGET_FPA || TARGET_MAVERICK)
5642 && (mode == SFmode || mode == DFmode
5b3e6663
PB
5643 || (TARGET_MAVERICK && mode == DImode)))
5644 return (code == CONST_INT && INTVAL (index) < 1024
bff99262
CLT
5645 /* Thumb-2 allows only > -256 index range for it's core register
5646 load/stores. Since we allow SF/DF in core registers, we have
5647 to use the intersection between -256~4096 (core) and -1024~1024
5648 (coprocessor). */
5649 && INTVAL (index) > -256
5b3e6663
PB
5650 && (INTVAL (index) & 3) == 0);
5651
5652 if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
fe2d934b
PB
5653 {
5654 /* For DImode assume values will usually live in core regs
5655 and only allow LDRD addressing modes. */
5656 if (!TARGET_LDRD || mode != DImode)
5657 return (code == CONST_INT
5658 && INTVAL (index) < 1024
5659 && INTVAL (index) > -1024
5660 && (INTVAL (index) & 3) == 0);
5661 }
5b3e6663 5662
49460951
NF
5663 /* For quad modes, we restrict the constant offset to be slightly less
5664 than what the instruction format permits. We do this because for
5665 quad mode moves, we will actually decompose them into two separate
5666 double-mode reads or writes. INDEX must therefore be a valid
5667 (double-mode) offset and so should INDEX+8. */
5668 if (TARGET_NEON && VALID_NEON_QREG_MODE (mode))
88f77cba
JB
5669 return (code == CONST_INT
5670 && INTVAL (index) < 1016
5671 && INTVAL (index) > -1024
5672 && (INTVAL (index) & 3) == 0);
5673
49460951
NF
5674 /* We have no such constraint on double mode offsets, so we permit the
5675 full range of the instruction format. */
5676 if (TARGET_NEON && VALID_NEON_DREG_MODE (mode))
5677 return (code == CONST_INT
5678 && INTVAL (index) < 1024
5679 && INTVAL (index) > -1024
5680 && (INTVAL (index) & 3) == 0);
5681
5b3e6663
PB
5682 if (arm_address_register_rtx_p (index, strict_p)
5683 && (GET_MODE_SIZE (mode) <= 4))
5684 return 1;
5685
5686 if (mode == DImode || mode == DFmode)
5687 {
e07e020b
RR
5688 if (code == CONST_INT)
5689 {
5690 HOST_WIDE_INT val = INTVAL (index);
5691 /* ??? Can we assume ldrd for thumb2? */
5692 /* Thumb-2 ldrd only has reg+const addressing modes. */
5693 /* ldrd supports offsets of +-1020.
5694 However the ldr fallback does not. */
5695 return val > -256 && val < 256 && (val & 3) == 0;
5696 }
5697 else
5b3e6663 5698 return 0;
5b3e6663
PB
5699 }
5700
5701 if (code == MULT)
5702 {
5703 rtx xiop0 = XEXP (index, 0);
5704 rtx xiop1 = XEXP (index, 1);
5705
5706 return ((arm_address_register_rtx_p (xiop0, strict_p)
5707 && thumb2_index_mul_operand (xiop1))
5708 || (arm_address_register_rtx_p (xiop1, strict_p)
5709 && thumb2_index_mul_operand (xiop0)));
5710 }
5711 else if (code == ASHIFT)
5712 {
5713 rtx op = XEXP (index, 1);
5714
5715 return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
5716 && GET_CODE (op) == CONST_INT
5717 && INTVAL (op) > 0
5718 && INTVAL (op) <= 3);
5719 }
5720
5721 return (code == CONST_INT
5722 && INTVAL (index) < 4096
5723 && INTVAL (index) > -256);
5724}
5725
5726/* Return nonzero if X is valid as a 16-bit Thumb state base register. */
76a318e9 5727static int
5b3e6663 5728thumb1_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
76a318e9
RE
5729{
5730 int regno;
5731
5732 if (GET_CODE (x) != REG)
5733 return 0;
5734
5735 regno = REGNO (x);
5736
5737 if (strict_p)
5b3e6663 5738 return THUMB1_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
76a318e9
RE
5739
5740 return (regno <= LAST_LO_REGNUM
07e58265 5741 || regno > LAST_VIRTUAL_REGISTER
76a318e9
RE
5742 || regno == FRAME_POINTER_REGNUM
5743 || (GET_MODE_SIZE (mode) >= 4
5744 && (regno == STACK_POINTER_REGNUM
edf7cee8 5745 || regno >= FIRST_PSEUDO_REGISTER
76a318e9
RE
5746 || x == hard_frame_pointer_rtx
5747 || x == arg_pointer_rtx)));
5748}
5749
5750/* Return nonzero if x is a legitimate index register. This is the case
5751 for any base register that can access a QImode object. */
5752inline static int
5b3e6663 5753thumb1_index_register_rtx_p (rtx x, int strict_p)
76a318e9 5754{
5b3e6663 5755 return thumb1_base_register_rtx_p (x, QImode, strict_p);
76a318e9
RE
5756}
5757
5b3e6663 5758/* Return nonzero if x is a legitimate 16-bit Thumb-state address.
f676971a 5759
76a318e9
RE
5760 The AP may be eliminated to either the SP or the FP, so we use the
5761 least common denominator, e.g. SImode, and offsets from 0 to 64.
5762
5763 ??? Verify whether the above is the right approach.
5764
5765 ??? Also, the FP may be eliminated to the SP, so perhaps that
5766 needs special handling also.
5767
5768 ??? Look at how the mips16 port solves this problem. It probably uses
5769 better ways to solve some of these problems.
5770
5771 Although it is not incorrect, we don't accept QImode and HImode
5772 addresses based on the frame pointer or arg pointer until the
5773 reload pass starts. This is so that eliminating such addresses
5774 into stack based ones won't produce impossible code. */
363ee90e 5775int
5b3e6663 5776thumb1_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
76a318e9
RE
5777{
5778 /* ??? Not clear if this is right. Experiment. */
5779 if (GET_MODE_SIZE (mode) < 4
5780 && !(reload_in_progress || reload_completed)
5781 && (reg_mentioned_p (frame_pointer_rtx, x)
5782 || reg_mentioned_p (arg_pointer_rtx, x)
5783 || reg_mentioned_p (virtual_incoming_args_rtx, x)
5784 || reg_mentioned_p (virtual_outgoing_args_rtx, x)
5785 || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
5786 || reg_mentioned_p (virtual_stack_vars_rtx, x)))
5787 return 0;
5788
5789 /* Accept any base register. SP only in SImode or larger. */
5b3e6663 5790 else if (thumb1_base_register_rtx_p (x, mode, strict_p))
76a318e9
RE
5791 return 1;
5792
18dbd950 5793 /* This is PC relative data before arm_reorg runs. */
76a318e9
RE
5794 else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
5795 && GET_CODE (x) == SYMBOL_REF
020a4035 5796 && CONSTANT_POOL_ADDRESS_P (x) && !flag_pic)
76a318e9
RE
5797 return 1;
5798
18dbd950 5799 /* This is PC relative data after arm_reorg runs. */
0fd8c3ad
SL
5800 else if ((GET_MODE_SIZE (mode) >= 4 || mode == HFmode)
5801 && reload_completed
76a318e9
RE
5802 && (GET_CODE (x) == LABEL_REF
5803 || (GET_CODE (x) == CONST
5804 && GET_CODE (XEXP (x, 0)) == PLUS
5805 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5806 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5807 return 1;
5808
5809 /* Post-inc indexing only supported for SImode and larger. */
5810 else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
5b3e6663 5811 && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p))
76a318e9
RE
5812 return 1;
5813
5814 else if (GET_CODE (x) == PLUS)
5815 {
5816 /* REG+REG address can be any two index registers. */
5817 /* We disallow FRAME+REG addressing since we know that FRAME
5818 will be replaced with STACK, and SP relative addressing only
5819 permits SP+OFFSET. */
5820 if (GET_MODE_SIZE (mode) <= 4
5821 && XEXP (x, 0) != frame_pointer_rtx
5822 && XEXP (x, 1) != frame_pointer_rtx
5b3e6663 5823 && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
d37c3c62
MK
5824 && (thumb1_index_register_rtx_p (XEXP (x, 1), strict_p)
5825 || (!strict_p && will_be_in_index_register (XEXP (x, 1)))))
76a318e9
RE
5826 return 1;
5827
5828 /* REG+const has 5-7 bit offset for non-SP registers. */
5b3e6663 5829 else if ((thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
76a318e9
RE
5830 || XEXP (x, 0) == arg_pointer_rtx)
5831 && GET_CODE (XEXP (x, 1)) == CONST_INT
5832 && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
5833 return 1;
5834
a50aa827 5835 /* REG+const has 10-bit offset for SP, but only SImode and
76a318e9
RE
5836 larger is supported. */
5837 /* ??? Should probably check for DI/DFmode overflow here
5838 just like GO_IF_LEGITIMATE_OFFSET does. */
5839 else if (GET_CODE (XEXP (x, 0)) == REG
5840 && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
5841 && GET_MODE_SIZE (mode) >= 4
5842 && GET_CODE (XEXP (x, 1)) == CONST_INT
5843 && INTVAL (XEXP (x, 1)) >= 0
5844 && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
5845 && (INTVAL (XEXP (x, 1)) & 3) == 0)
5846 return 1;
5847
5848 else if (GET_CODE (XEXP (x, 0)) == REG
c5289e45
RE
5849 && (REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
5850 || REGNO (XEXP (x, 0)) == ARG_POINTER_REGNUM
5851 || (REGNO (XEXP (x, 0)) >= FIRST_VIRTUAL_REGISTER
32990d5b
JJ
5852 && REGNO (XEXP (x, 0))
5853 <= LAST_VIRTUAL_POINTER_REGISTER))
76a318e9
RE
5854 && GET_MODE_SIZE (mode) >= 4
5855 && GET_CODE (XEXP (x, 1)) == CONST_INT
5856 && (INTVAL (XEXP (x, 1)) & 3) == 0)
5857 return 1;
5858 }
5859
5860 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
f954388e 5861 && GET_MODE_SIZE (mode) == 4
76a318e9
RE
5862 && GET_CODE (x) == SYMBOL_REF
5863 && CONSTANT_POOL_ADDRESS_P (x)
d3585b76
DJ
5864 && ! (flag_pic
5865 && symbol_mentioned_p (get_pool_constant (x))
5866 && ! pcrel_constant_p (get_pool_constant (x))))
76a318e9
RE
5867 return 1;
5868
5869 return 0;
5870}
5871
5872/* Return nonzero if VAL can be used as an offset in a Thumb-state address
5873 instruction of mode MODE. */
5874int
e32bac5b 5875thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
76a318e9
RE
5876{
5877 switch (GET_MODE_SIZE (mode))
5878 {
5879 case 1:
5880 return val >= 0 && val < 32;
5881
5882 case 2:
5883 return val >= 0 && val < 64 && (val & 1) == 0;
5884
5885 default:
5886 return (val >= 0
5887 && (val + GET_MODE_SIZE (mode)) <= 128
5888 && (val & 3) == 0);
5889 }
5890}
5891
c6c3dba9
PB
5892bool
5893arm_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
5894{
5895 if (TARGET_ARM)
5896 return arm_legitimate_address_outer_p (mode, x, SET, strict_p);
5897 else if (TARGET_THUMB2)
5898 return thumb2_legitimate_address_p (mode, x, strict_p);
5899 else /* if (TARGET_THUMB1) */
5900 return thumb1_legitimate_address_p (mode, x, strict_p);
5901}
5902
d3585b76
DJ
5903/* Build the SYMBOL_REF for __tls_get_addr. */
5904
5905static GTY(()) rtx tls_get_addr_libfunc;
5906
5907static rtx
5908get_tls_get_addr (void)
5909{
5910 if (!tls_get_addr_libfunc)
5911 tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
5912 return tls_get_addr_libfunc;
5913}
5914
5915static rtx
5916arm_load_tp (rtx target)
5917{
5918 if (!target)
5919 target = gen_reg_rtx (SImode);
5920
5921 if (TARGET_HARD_TP)
5922 {
5923 /* Can return in any reg. */
5924 emit_insn (gen_load_tp_hard (target));
5925 }
5926 else
5927 {
5928 /* Always returned in r0. Immediately copy the result into a pseudo,
5929 otherwise other uses of r0 (e.g. setting up function arguments) may
5930 clobber the value. */
5931
5932 rtx tmp;
5933
5934 emit_insn (gen_load_tp_soft ());
5935
5936 tmp = gen_rtx_REG (SImode, 0);
5937 emit_move_insn (target, tmp);
5938 }
5939 return target;
5940}
5941
5942static rtx
5943load_tls_operand (rtx x, rtx reg)
5944{
5945 rtx tmp;
5946
5947 if (reg == NULL_RTX)
5948 reg = gen_reg_rtx (SImode);
5949
5950 tmp = gen_rtx_CONST (SImode, x);
5951
5952 emit_move_insn (reg, tmp);
5953
5954 return reg;
5955}
5956
5957static rtx
5958arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
5959{
f16fe45f 5960 rtx insns, label, labelno, sum;
d3585b76
DJ
5961
5962 start_sequence ();
5963
f16fe45f
DJ
5964 labelno = GEN_INT (pic_labelno++);
5965 label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
5966 label = gen_rtx_CONST (VOIDmode, label);
5967
d3585b76 5968 sum = gen_rtx_UNSPEC (Pmode,
f16fe45f 5969 gen_rtvec (4, x, GEN_INT (reloc), label,
d3585b76
DJ
5970 GEN_INT (TARGET_ARM ? 8 : 4)),
5971 UNSPEC_TLS);
5972 reg = load_tls_operand (sum, reg);
5973
5974 if (TARGET_ARM)
f16fe45f 5975 emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
5b3e6663 5976 else if (TARGET_THUMB2)
87d05b44 5977 emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5b3e6663 5978 else /* TARGET_THUMB1 */
f16fe45f 5979 emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
d3585b76
DJ
5980
5981 *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX, LCT_PURE, /* LCT_CONST? */
5982 Pmode, 1, reg, Pmode);
5983
5984 insns = get_insns ();
5985 end_sequence ();
5986
5987 return insns;
5988}
5989
5990rtx
5991legitimize_tls_address (rtx x, rtx reg)
5992{
f16fe45f 5993 rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
d3585b76
DJ
5994 unsigned int model = SYMBOL_REF_TLS_MODEL (x);
5995
5996 switch (model)
5997 {
5998 case TLS_MODEL_GLOBAL_DYNAMIC:
5999 insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
6000 dest = gen_reg_rtx (Pmode);
6001 emit_libcall_block (insns, dest, ret, x);
6002 return dest;
6003
6004 case TLS_MODEL_LOCAL_DYNAMIC:
6005 insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
6006
6007 /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
6008 share the LDM result with other LD model accesses. */
6009 eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
6010 UNSPEC_TLS);
6011 dest = gen_reg_rtx (Pmode);
e66e1c68 6012 emit_libcall_block (insns, dest, ret, eqv);
d3585b76
DJ
6013
6014 /* Load the addend. */
6015 addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (TLS_LDO32)),
6016 UNSPEC_TLS);
6017 addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
6018 return gen_rtx_PLUS (Pmode, dest, addend);
6019
6020 case TLS_MODEL_INITIAL_EXEC:
f16fe45f
DJ
6021 labelno = GEN_INT (pic_labelno++);
6022 label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
6023 label = gen_rtx_CONST (VOIDmode, label);
d3585b76 6024 sum = gen_rtx_UNSPEC (Pmode,
f16fe45f 6025 gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
d3585b76
DJ
6026 GEN_INT (TARGET_ARM ? 8 : 4)),
6027 UNSPEC_TLS);
6028 reg = load_tls_operand (sum, reg);
6029
6030 if (TARGET_ARM)
f16fe45f 6031 emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
5b3e6663 6032 else if (TARGET_THUMB2)
2e5505a4 6033 emit_insn (gen_tls_load_dot_plus_four (reg, NULL, reg, labelno));
d3585b76
DJ
6034 else
6035 {
f16fe45f 6036 emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
d3585b76
DJ
6037 emit_move_insn (reg, gen_const_mem (SImode, reg));
6038 }
6039
6040 tp = arm_load_tp (NULL_RTX);
6041
6042 return gen_rtx_PLUS (Pmode, tp, reg);
6043
6044 case TLS_MODEL_LOCAL_EXEC:
6045 tp = arm_load_tp (NULL_RTX);
6046
6047 reg = gen_rtx_UNSPEC (Pmode,
6048 gen_rtvec (2, x, GEN_INT (TLS_LE32)),
6049 UNSPEC_TLS);
6050 reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
6051
6052 return gen_rtx_PLUS (Pmode, tp, reg);
6053
6054 default:
6055 abort ();
6056 }
6057}
6058
ccf4d512
RE
6059/* Try machine-dependent ways of modifying an illegitimate address
6060 to be legitimate. If we find one, return the new, valid address. */
ccf4d512 6061rtx
e32bac5b 6062arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
ccf4d512 6063{
506d7b68
PB
6064 if (!TARGET_ARM)
6065 {
6066 /* TODO: legitimize_address for Thumb2. */
6067 if (TARGET_THUMB2)
6068 return x;
6069 return thumb_legitimize_address (x, orig_x, mode);
6070 }
6071
d3585b76
DJ
6072 if (arm_tls_symbol_p (x))
6073 return legitimize_tls_address (x, NULL_RTX);
6074
ccf4d512
RE
6075 if (GET_CODE (x) == PLUS)
6076 {
6077 rtx xop0 = XEXP (x, 0);
6078 rtx xop1 = XEXP (x, 1);
6079
6080 if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
6081 xop0 = force_reg (SImode, xop0);
6082
6083 if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
6084 xop1 = force_reg (SImode, xop1);
6085
6086 if (ARM_BASE_REGISTER_RTX_P (xop0)
6087 && GET_CODE (xop1) == CONST_INT)
6088 {
6089 HOST_WIDE_INT n, low_n;
6090 rtx base_reg, val;
6091 n = INTVAL (xop1);
6092
9b66ebb1
PB
6093 /* VFP addressing modes actually allow greater offsets, but for
6094 now we just stick with the lowest common denominator. */
6095 if (mode == DImode
6096 || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
ccf4d512
RE
6097 {
6098 low_n = n & 0x0f;
6099 n &= ~0x0f;
6100 if (low_n > 4)
6101 {
6102 n += 16;
6103 low_n -= 16;
6104 }
6105 }
6106 else
6107 {
6108 low_n = ((mode) == TImode ? 0
6109 : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
6110 n -= low_n;
6111 }
6112
6113 base_reg = gen_reg_rtx (SImode);
d66437c5 6114 val = force_operand (plus_constant (xop0, n), NULL_RTX);
ccf4d512 6115 emit_move_insn (base_reg, val);
d66437c5 6116 x = plus_constant (base_reg, low_n);
ccf4d512
RE
6117 }
6118 else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
6119 x = gen_rtx_PLUS (SImode, xop0, xop1);
6120 }
6121
6122 /* XXX We don't allow MINUS any more -- see comment in
c6c3dba9 6123 arm_legitimate_address_outer_p (). */
ccf4d512
RE
6124 else if (GET_CODE (x) == MINUS)
6125 {
6126 rtx xop0 = XEXP (x, 0);
6127 rtx xop1 = XEXP (x, 1);
6128
6129 if (CONSTANT_P (xop0))
6130 xop0 = force_reg (SImode, xop0);
6131
6132 if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
6133 xop1 = force_reg (SImode, xop1);
6134
6135 if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
6136 x = gen_rtx_MINUS (SImode, xop0, xop1);
6137 }
6138
86805759
NP
6139 /* Make sure to take full advantage of the pre-indexed addressing mode
6140 with absolute addresses which often allows for the base register to
6141 be factorized for multiple adjacent memory references, and it might
6142 even allows for the mini pool to be avoided entirely. */
6143 else if (GET_CODE (x) == CONST_INT && optimize > 0)
6144 {
6145 unsigned int bits;
6146 HOST_WIDE_INT mask, base, index;
6147 rtx base_reg;
6148
a50aa827
KH
6149 /* ldr and ldrb can use a 12-bit index, ldrsb and the rest can only
6150 use a 8-bit index. So let's use a 12-bit index for SImode only and
86805759
NP
6151 hope that arm_gen_constant will enable ldrb to use more bits. */
6152 bits = (mode == SImode) ? 12 : 8;
6153 mask = (1 << bits) - 1;
6154 base = INTVAL (x) & ~mask;
6155 index = INTVAL (x) & mask;
b107d743 6156 if (bit_count (base & 0xffffffff) > (32 - bits)/2)
86805759
NP
6157 {
6158 /* It'll most probably be more efficient to generate the base
6159 with more bits set and use a negative index instead. */
6160 base |= mask;
6161 index -= mask;
6162 }
6163 base_reg = force_reg (SImode, GEN_INT (base));
d66437c5 6164 x = plus_constant (base_reg, index);
86805759
NP
6165 }
6166
ccf4d512
RE
6167 if (flag_pic)
6168 {
6169 /* We need to find and carefully transform any SYMBOL and LABEL
6170 references; so go back to the original address expression. */
6171 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
6172
6173 if (new_x != orig_x)
6174 x = new_x;
6175 }
6176
6177 return x;
6178}
6179
6f5b4f3e
RE
6180
6181/* Try machine-dependent ways of modifying an illegitimate Thumb address
6182 to be legitimate. If we find one, return the new, valid address. */
6183rtx
6184thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
6185{
d3585b76
DJ
6186 if (arm_tls_symbol_p (x))
6187 return legitimize_tls_address (x, NULL_RTX);
6188
6f5b4f3e
RE
6189 if (GET_CODE (x) == PLUS
6190 && GET_CODE (XEXP (x, 1)) == CONST_INT
6191 && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
6192 || INTVAL (XEXP (x, 1)) < 0))
6193 {
6194 rtx xop0 = XEXP (x, 0);
6195 rtx xop1 = XEXP (x, 1);
6196 HOST_WIDE_INT offset = INTVAL (xop1);
6197
6198 /* Try and fold the offset into a biasing of the base register and
6199 then offsetting that. Don't do this when optimizing for space
6200 since it can cause too many CSEs. */
6201 if (optimize_size && offset >= 0
6202 && offset < 256 + 31 * GET_MODE_SIZE (mode))
6203 {
6204 HOST_WIDE_INT delta;
6205
6206 if (offset >= 256)
6207 delta = offset - (256 - GET_MODE_SIZE (mode));
6208 else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
6209 delta = 31 * GET_MODE_SIZE (mode);
6210 else
6211 delta = offset & (~31 * GET_MODE_SIZE (mode));
6212
6213 xop0 = force_operand (plus_constant (xop0, offset - delta),
6214 NULL_RTX);
6215 x = plus_constant (xop0, delta);
6216 }
6217 else if (offset < 0 && offset > -256)
6218 /* Small negative offsets are best done with a subtract before the
6219 dereference, forcing these into a register normally takes two
6220 instructions. */
6221 x = force_operand (x, NULL_RTX);
6222 else
6223 {
6224 /* For the remaining cases, force the constant into a register. */
6225 xop1 = force_reg (SImode, xop1);
6226 x = gen_rtx_PLUS (SImode, xop0, xop1);
6227 }
6228 }
6229 else if (GET_CODE (x) == PLUS
6230 && s_register_operand (XEXP (x, 1), SImode)
6231 && !s_register_operand (XEXP (x, 0), SImode))
6232 {
6233 rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
6234
6235 x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
6236 }
6237
6238 if (flag_pic)
6239 {
6240 /* We need to find and carefully transform any SYMBOL and LABEL
6241 references; so go back to the original address expression. */
6242 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
6243
6244 if (new_x != orig_x)
6245 x = new_x;
6246 }
6247
6248 return x;
6249}
6250
0cd98787
JZ
6251bool
6252arm_legitimize_reload_address (rtx *p,
6253 enum machine_mode mode,
6254 int opnum, int type,
6255 int ind_levels ATTRIBUTE_UNUSED)
6256{
6257 if (GET_CODE (*p) == PLUS
6258 && GET_CODE (XEXP (*p, 0)) == REG
6259 && ARM_REGNO_OK_FOR_BASE_P (REGNO (XEXP (*p, 0)))
6260 && GET_CODE (XEXP (*p, 1)) == CONST_INT)
6261 {
6262 HOST_WIDE_INT val = INTVAL (XEXP (*p, 1));
6263 HOST_WIDE_INT low, high;
6264
06eb52ca
CLT
6265 /* Detect coprocessor load/stores. */
6266 bool coproc_p = ((TARGET_HARD_FLOAT
6267 && (TARGET_VFP || TARGET_FPA || TARGET_MAVERICK)
6268 && (mode == SFmode || mode == DFmode
6269 || (mode == DImode && TARGET_MAVERICK)))
6270 || (TARGET_REALLY_IWMMXT
6271 && VALID_IWMMXT_REG_MODE (mode))
6272 || (TARGET_NEON
6273 && (VALID_NEON_DREG_MODE (mode)
6274 || VALID_NEON_QREG_MODE (mode))));
6275
6276 /* For some conditions, bail out when lower two bits are unaligned. */
6277 if ((val & 0x3) != 0
6278 /* Coprocessor load/store indexes are 8-bits + '00' appended. */
6279 && (coproc_p
6280 /* For DI, and DF under soft-float: */
6281 || ((mode == DImode || mode == DFmode)
6282 /* Without ldrd, we use stm/ldm, which does not
6283 fair well with unaligned bits. */
6284 && (! TARGET_LDRD
6285 /* Thumb-2 ldrd/strd is [-1020,+1020] in steps of 4. */
6286 || TARGET_THUMB2))))
6287 return false;
6288
6289 /* When breaking down a [reg+index] reload address into [(reg+high)+low],
6290 of which the (reg+high) gets turned into a reload add insn,
6291 we try to decompose the index into high/low values that can often
6292 also lead to better reload CSE.
6293 For example:
6294 ldr r0, [r2, #4100] // Offset too large
6295 ldr r1, [r2, #4104] // Offset too large
6296
6297 is best reloaded as:
6298 add t1, r2, #4096
6299 ldr r0, [t1, #4]
6300 add t2, r2, #4096
6301 ldr r1, [t2, #8]
6302
6303 which post-reload CSE can simplify in most cases to eliminate the
6304 second add instruction:
6305 add t1, r2, #4096
6306 ldr r0, [t1, #4]
6307 ldr r1, [t1, #8]
6308
6309 The idea here is that we want to split out the bits of the constant
6310 as a mask, rather than as subtracting the maximum offset that the
6311 respective type of load/store used can handle.
6312
6313 When encountering negative offsets, we can still utilize it even if
6314 the overall offset is positive; sometimes this may lead to an immediate
6315 that can be constructed with fewer instructions.
6316 For example:
6317 ldr r0, [r2, #0x3FFFFC]
6318
6319 This is best reloaded as:
6320 add t1, r2, #0x400000
6321 ldr r0, [t1, #-4]
6322
6323 The trick for spotting this for a load insn with N bits of offset
6324 (i.e. bits N-1:0) is to look at bit N; if it is set, then chose a
6325 negative offset that is going to make bit N and all the bits below
6326 it become zero in the remainder part.
6327
6328 The SIGN_MAG_LOW_ADDR_BITS macro below implements this, with respect
6329 to sign-magnitude addressing (i.e. separate +- bit, or 1's complement),
6330 used in most cases of ARM load/store instructions. */
6331
6332#define SIGN_MAG_LOW_ADDR_BITS(VAL, N) \
6333 (((VAL) & ((1 << (N)) - 1)) \
6334 ? (((VAL) & ((1 << ((N) + 1)) - 1)) ^ (1 << (N))) - (1 << (N)) \
6335 : 0)
6336
6337 if (coproc_p)
01475747
CLT
6338 {
6339 low = SIGN_MAG_LOW_ADDR_BITS (val, 10);
6340
6341 /* NEON quad-word load/stores are made of two double-word accesses,
6342 so the valid index range is reduced by 8. Treat as 9-bit range if
6343 we go over it. */
6344 if (TARGET_NEON && VALID_NEON_QREG_MODE (mode) && low >= 1016)
6345 low = SIGN_MAG_LOW_ADDR_BITS (val, 9);
6346 }
06eb52ca
CLT
6347 else if (GET_MODE_SIZE (mode) == 8)
6348 {
6349 if (TARGET_LDRD)
6350 low = (TARGET_THUMB2
6351 ? SIGN_MAG_LOW_ADDR_BITS (val, 10)
6352 : SIGN_MAG_LOW_ADDR_BITS (val, 8));
6353 else
6354 /* For pre-ARMv5TE (without ldrd), we use ldm/stm(db/da/ib)
6355 to access doublewords. The supported load/store offsets are
6356 -8, -4, and 4, which we try to produce here. */
6357 low = ((val & 0xf) ^ 0x8) - 0x8;
6358 }
6359 else if (GET_MODE_SIZE (mode) < 8)
6360 {
6361 /* NEON element load/stores do not have an offset. */
6362 if (TARGET_NEON_FP16 && mode == HFmode)
6363 return false;
6364
6365 if (TARGET_THUMB2)
6366 {
6367 /* Thumb-2 has an asymmetrical index range of (-256,4096).
6368 Try the wider 12-bit range first, and re-try if the result
6369 is out of range. */
6370 low = SIGN_MAG_LOW_ADDR_BITS (val, 12);
6371 if (low < -255)
6372 low = SIGN_MAG_LOW_ADDR_BITS (val, 8);
6373 }
6374 else
6375 {
6376 if (mode == HImode || mode == HFmode)
6377 {
6378 if (arm_arch4)
6379 low = SIGN_MAG_LOW_ADDR_BITS (val, 8);
6380 else
6381 {
6382 /* The storehi/movhi_bytes fallbacks can use only
6383 [-4094,+4094] of the full ldrb/strb index range. */
6384 low = SIGN_MAG_LOW_ADDR_BITS (val, 12);
6385 if (low == 4095 || low == -4095)
6386 return false;
6387 }
6388 }
6389 else
6390 low = SIGN_MAG_LOW_ADDR_BITS (val, 12);
6391 }
6392 }
0cd98787
JZ
6393 else
6394 return false;
6395
6396 high = ((((val - low) & (unsigned HOST_WIDE_INT) 0xffffffff)
6397 ^ (unsigned HOST_WIDE_INT) 0x80000000)
6398 - (unsigned HOST_WIDE_INT) 0x80000000);
6399 /* Check for overflow or zero */
6400 if (low == 0 || high == 0 || (high + low != val))
6401 return false;
6402
6403 /* Reload the high part into a base reg; leave the low part
6404 in the mem. */
6405 *p = gen_rtx_PLUS (GET_MODE (*p),
6406 gen_rtx_PLUS (GET_MODE (*p), XEXP (*p, 0),
6407 GEN_INT (high)),
6408 GEN_INT (low));
6409 push_reload (XEXP (*p, 0), NULL_RTX, &XEXP (*p, 0), NULL,
6410 MODE_BASE_REG_CLASS (mode), GET_MODE (*p),
6411 VOIDmode, 0, 0, opnum, (enum reload_type) type);
6412 return true;
6413 }
6414
6415 return false;
6416}
6417
a132dad6 6418rtx
e0b92319
NC
6419thumb_legitimize_reload_address (rtx *x_p,
6420 enum machine_mode mode,
6421 int opnum, int type,
6422 int ind_levels ATTRIBUTE_UNUSED)
a132dad6
RE
6423{
6424 rtx x = *x_p;
e0b92319 6425
a132dad6
RE
6426 if (GET_CODE (x) == PLUS
6427 && GET_MODE_SIZE (mode) < 4
6428 && REG_P (XEXP (x, 0))
6429 && XEXP (x, 0) == stack_pointer_rtx
6430 && GET_CODE (XEXP (x, 1)) == CONST_INT
6431 && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
6432 {
6433 rtx orig_x = x;
6434
6435 x = copy_rtx (x);
6436 push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
bbbbb16a 6437 Pmode, VOIDmode, 0, 0, opnum, (enum reload_type) type);
a132dad6
RE
6438 return x;
6439 }
6440
6441 /* If both registers are hi-regs, then it's better to reload the
6442 entire expression rather than each register individually. That
6443 only requires one reload register rather than two. */
6444 if (GET_CODE (x) == PLUS
6445 && REG_P (XEXP (x, 0))
6446 && REG_P (XEXP (x, 1))
6447 && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
6448 && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
6449 {
6450 rtx orig_x = x;
6451
6452 x = copy_rtx (x);
6453 push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
bbbbb16a 6454 Pmode, VOIDmode, 0, 0, opnum, (enum reload_type) type);
a132dad6
RE
6455 return x;
6456 }
6457
6458 return NULL;
6459}
d3585b76
DJ
6460
6461/* Test for various thread-local symbols. */
6462
6463/* Return TRUE if X is a thread-local symbol. */
6464
6465static bool
6466arm_tls_symbol_p (rtx x)
6467{
6468 if (! TARGET_HAVE_TLS)
6469 return false;
6470
6471 if (GET_CODE (x) != SYMBOL_REF)
6472 return false;
6473
6474 return SYMBOL_REF_TLS_MODEL (x) != 0;
6475}
6476
6477/* Helper for arm_tls_referenced_p. */
6478
6479static int
6480arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
6481{
6482 if (GET_CODE (*x) == SYMBOL_REF)
6483 return SYMBOL_REF_TLS_MODEL (*x) != 0;
6484
6485 /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
6486 TLS offsets, not real symbol references. */
6487 if (GET_CODE (*x) == UNSPEC
6488 && XINT (*x, 1) == UNSPEC_TLS)
6489 return -1;
6490
6491 return 0;
6492}
6493
6494/* Return TRUE if X contains any TLS symbol references. */
6495
6496bool
6497arm_tls_referenced_p (rtx x)
6498{
6499 if (! TARGET_HAVE_TLS)
6500 return false;
6501
6502 return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
6503}
8426b956 6504
1a627b35
RS
6505/* Implement TARGET_LEGITIMATE_CONSTANT_P.
6506
6507 On the ARM, allow any integer (invalid ones are removed later by insn
6508 patterns), nice doubles and symbol_refs which refer to the function's
6509 constant pool XXX.
6510
6511 When generating pic allow anything. */
6512
6513static bool
6514arm_legitimate_constant_p_1 (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
6515{
6516 return flag_pic || !label_mentioned_p (x);
6517}
6518
6519static bool
6520thumb_legitimate_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
6521{
6522 return (GET_CODE (x) == CONST_INT
6523 || GET_CODE (x) == CONST_DOUBLE
6524 || CONSTANT_ADDRESS_P (x)
6525 || flag_pic);
6526}
6527
6528static bool
6529arm_legitimate_constant_p (enum machine_mode mode, rtx x)
6530{
6531 return (!arm_cannot_force_const_mem (mode, x)
6532 && (TARGET_32BIT
6533 ? arm_legitimate_constant_p_1 (mode, x)
6534 : thumb_legitimate_constant_p (mode, x)));
6535}
6536
8426b956
RS
6537/* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
6538
1a627b35 6539static bool
fbbf66e7 6540arm_cannot_force_const_mem (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
8426b956
RS
6541{
6542 rtx base, offset;
6543
6544 if (ARM_OFFSETS_MUST_BE_WITHIN_SECTIONS_P)
6545 {
6546 split_const (x, &base, &offset);
6547 if (GET_CODE (base) == SYMBOL_REF
6548 && !offset_within_block_p (base, INTVAL (offset)))
6549 return true;
6550 }
6551 return arm_tls_referenced_p (x);
6552}
6b990f6b 6553\f
e2c671ba
RE
6554#define REG_OR_SUBREG_REG(X) \
6555 (GET_CODE (X) == REG \
6556 || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
6557
6558#define REG_OR_SUBREG_RTX(X) \
6559 (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
6560
3c50106f 6561static inline int
5b3e6663 6562thumb1_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
e2c671ba
RE
6563{
6564 enum machine_mode mode = GET_MODE (x);
e4c6a07a 6565 int total;
e2c671ba 6566
9b66ebb1 6567 switch (code)
d5b7b3ae 6568 {
9b66ebb1
PB
6569 case ASHIFT:
6570 case ASHIFTRT:
6571 case LSHIFTRT:
f676971a 6572 case ROTATERT:
9b66ebb1
PB
6573 case PLUS:
6574 case MINUS:
6575 case COMPARE:
6576 case NEG:
f676971a 6577 case NOT:
9b66ebb1 6578 return COSTS_N_INSNS (1);
f676971a
EC
6579
6580 case MULT:
6581 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6582 {
6583 int cycles = 0;
9b66ebb1 6584 unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
f676971a
EC
6585
6586 while (i)
6587 {
6588 i >>= 2;
6589 cycles++;
6590 }
6591 return COSTS_N_INSNS (2) + cycles;
9b66ebb1
PB
6592 }
6593 return COSTS_N_INSNS (1) + 16;
f676971a
EC
6594
6595 case SET:
6596 return (COSTS_N_INSNS (1)
6597 + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
9b66ebb1 6598 + GET_CODE (SET_DEST (x)) == MEM));
f676971a
EC
6599
6600 case CONST_INT:
6601 if (outer == SET)
6602 {
6603 if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
6604 return 0;
6605 if (thumb_shiftable_const (INTVAL (x)))
6606 return COSTS_N_INSNS (2);
6607 return COSTS_N_INSNS (3);
6608 }
9b66ebb1 6609 else if ((outer == PLUS || outer == COMPARE)
f676971a 6610 && INTVAL (x) < 256 && INTVAL (x) > -256)
9b66ebb1 6611 return 0;
582021ba 6612 else if ((outer == IOR || outer == XOR || outer == AND)
9b66ebb1
PB
6613 && INTVAL (x) < 256 && INTVAL (x) >= -256)
6614 return COSTS_N_INSNS (1);
c99102b8
BS
6615 else if (outer == AND)
6616 {
6617 int i;
6618 /* This duplicates the tests in the andsi3 expander. */
6619 for (i = 9; i <= 31; i++)
6620 if ((((HOST_WIDE_INT) 1) << i) - 1 == INTVAL (x)
6621 || (((HOST_WIDE_INT) 1) << i) - 1 == ~INTVAL (x))
6622 return COSTS_N_INSNS (2);
6623 }
f676971a
EC
6624 else if (outer == ASHIFT || outer == ASHIFTRT
6625 || outer == LSHIFTRT)
6626 return 0;
9b66ebb1 6627 return COSTS_N_INSNS (2);
f676971a
EC
6628
6629 case CONST:
6630 case CONST_DOUBLE:
6631 case LABEL_REF:
6632 case SYMBOL_REF:
9b66ebb1 6633 return COSTS_N_INSNS (3);
f676971a 6634
9b66ebb1
PB
6635 case UDIV:
6636 case UMOD:
6637 case DIV:
6638 case MOD:
6639 return 100;
d5b7b3ae 6640
9b66ebb1
PB
6641 case TRUNCATE:
6642 return 99;
d5b7b3ae 6643
9b66ebb1
PB
6644 case AND:
6645 case XOR:
f676971a 6646 case IOR:
ff482c8d 6647 /* XXX guess. */
9b66ebb1 6648 return 8;
d5b7b3ae 6649
9b66ebb1
PB
6650 case MEM:
6651 /* XXX another guess. */
6652 /* Memory costs quite a lot for the first word, but subsequent words
6653 load at the equivalent of a single insn each. */
6654 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
6655 + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
6656 ? 4 : 0));
6657
6658 case IF_THEN_ELSE:
ff482c8d 6659 /* XXX a guess. */
9b66ebb1
PB
6660 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
6661 return 14;
6662 return 2;
6663
e4c6a07a 6664 case SIGN_EXTEND:
9b66ebb1 6665 case ZERO_EXTEND:
e4c6a07a
BS
6666 total = mode == DImode ? COSTS_N_INSNS (1) : 0;
6667 total += thumb1_rtx_costs (XEXP (x, 0), GET_CODE (XEXP (x, 0)), code);
f676971a 6668
e4c6a07a
BS
6669 if (mode == SImode)
6670 return total;
f676971a 6671
e4c6a07a
BS
6672 if (arm_arch6)
6673 return total + COSTS_N_INSNS (1);
f676971a 6674
e4c6a07a
BS
6675 /* Assume a two-shift sequence. Increase the cost slightly so
6676 we prefer actual shifts over an extend operation. */
6677 return total + 1 + COSTS_N_INSNS (2);
f676971a 6678
9b66ebb1
PB
6679 default:
6680 return 99;
d5b7b3ae 6681 }
9b66ebb1
PB
6682}
6683
d5a0a47b
RE
6684static inline bool
6685arm_rtx_costs_1 (rtx x, enum rtx_code outer, int* total, bool speed)
9b66ebb1
PB
6686{
6687 enum machine_mode mode = GET_MODE (x);
6688 enum rtx_code subcode;
d5a0a47b
RE
6689 rtx operand;
6690 enum rtx_code code = GET_CODE (x);
d5a0a47b 6691 *total = 0;
9b66ebb1 6692
e2c671ba
RE
6693 switch (code)
6694 {
6695 case MEM:
6696 /* Memory costs quite a lot for the first word, but subsequent words
6697 load at the equivalent of a single insn each. */
d5a0a47b
RE
6698 *total = COSTS_N_INSNS (2 + ARM_NUM_REGS (mode));
6699 return true;
e2c671ba
RE
6700
6701 case DIV:
6702 case MOD:
b9c53150
RS
6703 case UDIV:
6704 case UMOD:
d5a0a47b
RE
6705 if (TARGET_HARD_FLOAT && mode == SFmode)
6706 *total = COSTS_N_INSNS (2);
e0dc3601 6707 else if (TARGET_HARD_FLOAT && mode == DFmode && !TARGET_VFP_SINGLE)
d5a0a47b
RE
6708 *total = COSTS_N_INSNS (4);
6709 else
6710 *total = COSTS_N_INSNS (20);
6711 return false;
e2c671ba
RE
6712
6713 case ROTATE:
d5a0a47b
RE
6714 if (GET_CODE (XEXP (x, 1)) == REG)
6715 *total = COSTS_N_INSNS (1); /* Need to subtract from 32 */
6716 else if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6717 *total = rtx_cost (XEXP (x, 1), code, speed);
6718
e2c671ba
RE
6719 /* Fall through */
6720 case ROTATERT:
6721 if (mode != SImode)
d5a0a47b
RE
6722 {
6723 *total += COSTS_N_INSNS (4);
6724 return true;
6725 }
6726
e2c671ba
RE
6727 /* Fall through */
6728 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
d5a0a47b 6729 *total += rtx_cost (XEXP (x, 0), code, speed);
e2c671ba 6730 if (mode == DImode)
d5a0a47b
RE
6731 {
6732 *total += COSTS_N_INSNS (3);
6733 return true;
6734 }
7612f14d 6735
d5a0a47b 6736 *total += COSTS_N_INSNS (1);
7612f14d
PB
6737 /* Increase the cost of complex shifts because they aren't any faster,
6738 and reduce dual issue opportunities. */
6739 if (arm_tune_cortex_a9
6740 && outer != SET && GET_CODE (XEXP (x, 1)) != CONST_INT)
d5a0a47b
RE
6741 ++*total;
6742
6743 return true;
e2c671ba
RE
6744
6745 case MINUS:
6746 if (mode == DImode)
d5a0a47b
RE
6747 {
6748 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
6749 if (GET_CODE (XEXP (x, 0)) == CONST_INT
6750 && const_ok_for_arm (INTVAL (XEXP (x, 0))))
6751 {
6752 *total += rtx_cost (XEXP (x, 1), code, speed);
6753 return true;
6754 }
6755
6756 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6757 && const_ok_for_arm (INTVAL (XEXP (x, 1))))
6758 {
6759 *total += rtx_cost (XEXP (x, 0), code, speed);
6760 return true;
6761 }
6762
6763 return false;
6764 }
e2c671ba
RE
6765
6766 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
d5a0a47b 6767 {
e0dc3601
PB
6768 if (TARGET_HARD_FLOAT
6769 && (mode == SFmode
6770 || (mode == DFmode && !TARGET_VFP_SINGLE)))
d5a0a47b
RE
6771 {
6772 *total = COSTS_N_INSNS (1);
6773 if (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
6774 && arm_const_double_rtx (XEXP (x, 0)))
6775 {
6776 *total += rtx_cost (XEXP (x, 1), code, speed);
6777 return true;
6778 }
6779
6780 if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
6781 && arm_const_double_rtx (XEXP (x, 1)))
6782 {
6783 *total += rtx_cost (XEXP (x, 0), code, speed);
6784 return true;
6785 }
6786
6787 return false;
6788 }
6789 *total = COSTS_N_INSNS (20);
6790 return false;
6791 }
6792
6793 *total = COSTS_N_INSNS (1);
6794 if (GET_CODE (XEXP (x, 0)) == CONST_INT
6795 && const_ok_for_arm (INTVAL (XEXP (x, 0))))
6796 {
6797 *total += rtx_cost (XEXP (x, 1), code, speed);
6798 return true;
6799 }
6800
6801 subcode = GET_CODE (XEXP (x, 1));
6802 if (subcode == ASHIFT || subcode == ASHIFTRT
6803 || subcode == LSHIFTRT
6804 || subcode == ROTATE || subcode == ROTATERT)
6805 {
6806 *total += rtx_cost (XEXP (x, 0), code, speed);
6807 *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, speed);
6808 return true;
6809 }
6810
b32f6fff
KH
6811 /* A shift as a part of RSB costs no more than RSB itself. */
6812 if (GET_CODE (XEXP (x, 0)) == MULT
4c7c486a 6813 && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
b32f6fff
KH
6814 {
6815 *total += rtx_cost (XEXP (XEXP (x, 0), 0), code, speed);
6816 *total += rtx_cost (XEXP (x, 1), code, speed);
6817 return true;
6818 }
6819
d5a0a47b 6820 if (subcode == MULT
4c7c486a 6821 && power_of_two_operand (XEXP (XEXP (x, 1), 1), SImode))
d5a0a47b
RE
6822 {
6823 *total += rtx_cost (XEXP (x, 0), code, speed);
6824 *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, speed);
6825 return true;
6826 }
6827
6828 if (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMPARE
6829 || GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMM_COMPARE)
6830 {
6831 *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
6832 if (GET_CODE (XEXP (XEXP (x, 1), 0)) == REG
6833 && REGNO (XEXP (XEXP (x, 1), 0)) != CC_REGNUM)
6834 *total += COSTS_N_INSNS (1);
6835
6836 return true;
6837 }
6838
e2c671ba
RE
6839 /* Fall through */
6840
f676971a 6841 case PLUS:
d5a0a47b 6842 if (code == PLUS && arm_arch6 && mode == SImode
ff069900
PB
6843 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
6844 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
d5a0a47b
RE
6845 {
6846 *total = COSTS_N_INSNS (1);
6847 *total += rtx_cost (XEXP (XEXP (x, 0), 0), GET_CODE (XEXP (x, 0)),
6848 speed);
6849 *total += rtx_cost (XEXP (x, 1), code, speed);
6850 return true;
6851 }
ff069900 6852
d5a0a47b
RE
6853 /* MLA: All arguments must be registers. We filter out
6854 multiplication by a power of two, so that we fall down into
6855 the code below. */
6856 if (GET_CODE (XEXP (x, 0)) == MULT
4c7c486a 6857 && !power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
26da58dd 6858 {
d5a0a47b
RE
6859 /* The cost comes from the cost of the multiply. */
6860 return false;
26da58dd
PB
6861 }
6862
e2c671ba 6863 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
d5a0a47b 6864 {
e0dc3601
PB
6865 if (TARGET_HARD_FLOAT
6866 && (mode == SFmode
6867 || (mode == DFmode && !TARGET_VFP_SINGLE)))
d5a0a47b
RE
6868 {
6869 *total = COSTS_N_INSNS (1);
6870 if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
6871 && arm_const_double_rtx (XEXP (x, 1)))
6872 {
6873 *total += rtx_cost (XEXP (x, 0), code, speed);
6874 return true;
6875 }
6876
6877 return false;
6878 }
6879
6880 *total = COSTS_N_INSNS (20);
6881 return false;
6882 }
6883
6884 if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMPARE
6885 || GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMM_COMPARE)
6886 {
6887 *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 1), code, speed);
6888 if (GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
6889 && REGNO (XEXP (XEXP (x, 0), 0)) != CC_REGNUM)
6890 *total += COSTS_N_INSNS (1);
6891 return true;
6892 }
e2c671ba
RE
6893
6894 /* Fall through */
d5a0a47b 6895
f676971a 6896 case AND: case XOR: case IOR:
e2c671ba
RE
6897
6898 /* Normally the frame registers will be spilt into reg+const during
6899 reload, so it is a bad idea to combine them with other instructions,
6900 since then they might not be moved outside of loops. As a compromise
6901 we allow integration with ops that have a constant as their second
6902 operand. */
13cc4787
BS
6903 if (REG_OR_SUBREG_REG (XEXP (x, 0))
6904 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
6905 && GET_CODE (XEXP (x, 1)) != CONST_INT)
6906 *total = COSTS_N_INSNS (1);
e2c671ba
RE
6907
6908 if (mode == DImode)
d5a0a47b
RE
6909 {
6910 *total += COSTS_N_INSNS (2);
6911 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6912 && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
6913 {
6914 *total += rtx_cost (XEXP (x, 0), code, speed);
6915 return true;
6916 }
e2c671ba 6917
d5a0a47b
RE
6918 return false;
6919 }
6920
6921 *total += COSTS_N_INSNS (1);
6922 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6923 && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
6924 {
6925 *total += rtx_cost (XEXP (x, 0), code, speed);
6926 return true;
6927 }
6928 subcode = GET_CODE (XEXP (x, 0));
6929 if (subcode == ASHIFT || subcode == ASHIFTRT
6930 || subcode == LSHIFTRT
6931 || subcode == ROTATE || subcode == ROTATERT)
6932 {
6933 *total += rtx_cost (XEXP (x, 1), code, speed);
6934 *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6935 return true;
6936 }
6937
6938 if (subcode == MULT
4c7c486a 6939 && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
d5a0a47b
RE
6940 {
6941 *total += rtx_cost (XEXP (x, 1), code, speed);
6942 *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6943 return true;
6944 }
6945
6946 if (subcode == UMIN || subcode == UMAX
6947 || subcode == SMIN || subcode == SMAX)
6948 {
6949 *total = COSTS_N_INSNS (3);
6950 return true;
6951 }
6952
6953 return false;
e2c671ba
RE
6954
6955 case MULT:
9b66ebb1 6956 /* This should have been handled by the CPU specific routines. */
e6d29d15 6957 gcc_unreachable ();
e2c671ba 6958
56636818 6959 case TRUNCATE:
9b66ebb1 6960 if (arm_arch3m && mode == SImode
56636818
JL
6961 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
6962 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
6963 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
6964 == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
6965 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
6966 || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
d5a0a47b
RE
6967 {
6968 *total = rtx_cost (XEXP (XEXP (x, 0), 0), LSHIFTRT, speed);
6969 return true;
6970 }
6971 *total = COSTS_N_INSNS (2); /* Plus the cost of the MULT */
6972 return false;
56636818 6973
e2c671ba
RE
6974 case NEG:
6975 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
d5a0a47b 6976 {
e0dc3601
PB
6977 if (TARGET_HARD_FLOAT
6978 && (mode == SFmode
6979 || (mode == DFmode && !TARGET_VFP_SINGLE)))
d5a0a47b
RE
6980 {
6981 *total = COSTS_N_INSNS (1);
6982 return false;
6983 }
6984 *total = COSTS_N_INSNS (2);
6985 return false;
6986 }
6987
e2c671ba
RE
6988 /* Fall through */
6989 case NOT:
d5a0a47b
RE
6990 *total = COSTS_N_INSNS (ARM_NUM_REGS(mode));
6991 if (mode == SImode && code == NOT)
6992 {
6993 subcode = GET_CODE (XEXP (x, 0));
6994 if (subcode == ASHIFT || subcode == ASHIFTRT
6995 || subcode == LSHIFTRT
6996 || subcode == ROTATE || subcode == ROTATERT
6997 || (subcode == MULT
4c7c486a 6998 && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode)))
d5a0a47b
RE
6999 {
7000 *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
7001 /* Register shifts cost an extra cycle. */
7002 if (GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
7003 *total += COSTS_N_INSNS (1) + rtx_cost (XEXP (XEXP (x, 0), 1),
7004 subcode, speed);
7005 return true;
7006 }
7007 }
e2c671ba 7008
d5a0a47b 7009 return false;
e2c671ba
RE
7010
7011 case IF_THEN_ELSE:
7012 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
d5a0a47b
RE
7013 {
7014 *total = COSTS_N_INSNS (4);
7015 return true;
7016 }
7017
7018 operand = XEXP (x, 0);
7019
7020 if (!((GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMPARE
7021 || GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMM_COMPARE)
7022 && GET_CODE (XEXP (operand, 0)) == REG
7023 && REGNO (XEXP (operand, 0)) == CC_REGNUM))
7024 *total += COSTS_N_INSNS (1);
7025 *total += (rtx_cost (XEXP (x, 1), code, speed)
7026 + rtx_cost (XEXP (x, 2), code, speed));
7027 return true;
7028
7029 case NE:
7030 if (mode == SImode && XEXP (x, 1) == const0_rtx)
7031 {
7032 *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
7033 return true;
7034 }
7035 goto scc_insn;
7036
7037 case GE:
7038 if ((GET_CODE (XEXP (x, 0)) != REG || REGNO (XEXP (x, 0)) != CC_REGNUM)
7039 && mode == SImode && XEXP (x, 1) == const0_rtx)
7040 {
7041 *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
7042 return true;
7043 }
7044 goto scc_insn;
7045
7046 case LT:
7047 if ((GET_CODE (XEXP (x, 0)) != REG || REGNO (XEXP (x, 0)) != CC_REGNUM)
7048 && mode == SImode && XEXP (x, 1) == const0_rtx)
7049 {
7050 *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
7051 return true;
7052 }
7053 goto scc_insn;
7054
7055 case EQ:
7056 case GT:
7057 case LE:
7058 case GEU:
7059 case LTU:
7060 case GTU:
7061 case LEU:
7062 case UNORDERED:
7063 case ORDERED:
7064 case UNEQ:
7065 case UNGE:
7066 case UNLT:
7067 case UNGT:
7068 case UNLE:
7069 scc_insn:
7070 /* SCC insns. In the case where the comparison has already been
7071 performed, then they cost 2 instructions. Otherwise they need
7072 an additional comparison before them. */
7073 *total = COSTS_N_INSNS (2);
7074 if (GET_CODE (XEXP (x, 0)) == REG && REGNO (XEXP (x, 0)) == CC_REGNUM)
7075 {
7076 return true;
7077 }
e2c671ba 7078
d5a0a47b 7079 /* Fall through */
e2c671ba 7080 case COMPARE:
d5a0a47b
RE
7081 if (GET_CODE (XEXP (x, 0)) == REG && REGNO (XEXP (x, 0)) == CC_REGNUM)
7082 {
7083 *total = 0;
7084 return true;
7085 }
7086
7087 *total += COSTS_N_INSNS (1);
7088 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7089 && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
7090 {
7091 *total += rtx_cost (XEXP (x, 0), code, speed);
7092 return true;
7093 }
7094
7095 subcode = GET_CODE (XEXP (x, 0));
7096 if (subcode == ASHIFT || subcode == ASHIFTRT
7097 || subcode == LSHIFTRT
7098 || subcode == ROTATE || subcode == ROTATERT)
7099 {
7100 *total += rtx_cost (XEXP (x, 1), code, speed);
7101 *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
7102 return true;
7103 }
7104
7105 if (subcode == MULT
4c7c486a 7106 && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
d5a0a47b
RE
7107 {
7108 *total += rtx_cost (XEXP (x, 1), code, speed);
7109 *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
7110 return true;
7111 }
7112
7113 return false;
7114
7115 case UMIN:
7116 case UMAX:
7117 case SMIN:
7118 case SMAX:
7119 *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
7120 if (GET_CODE (XEXP (x, 1)) != CONST_INT
7121 || !const_ok_for_arm (INTVAL (XEXP (x, 1))))
7122 *total += rtx_cost (XEXP (x, 1), code, speed);
7123 return true;
e2c671ba
RE
7124
7125 case ABS:
bbbbb16a 7126 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
d5a0a47b 7127 {
e0dc3601
PB
7128 if (TARGET_HARD_FLOAT
7129 && (mode == SFmode
7130 || (mode == DFmode && !TARGET_VFP_SINGLE)))
d5a0a47b
RE
7131 {
7132 *total = COSTS_N_INSNS (1);
7133 return false;
7134 }
7135 *total = COSTS_N_INSNS (20);
7136 return false;
7137 }
7138 *total = COSTS_N_INSNS (1);
7139 if (mode == DImode)
7140 *total += COSTS_N_INSNS (3);
7141 return false;
e2c671ba
RE
7142
7143 case SIGN_EXTEND:
e2c671ba 7144 case ZERO_EXTEND:
d5a0a47b
RE
7145 *total = 0;
7146 if (GET_MODE_CLASS (mode) == MODE_INT)
e2c671ba 7147 {
e4c6a07a
BS
7148 rtx op = XEXP (x, 0);
7149 enum machine_mode opmode = GET_MODE (op);
7150
d5a0a47b
RE
7151 if (mode == DImode)
7152 *total += COSTS_N_INSNS (1);
e2c671ba 7153
e4c6a07a 7154 if (opmode != SImode)
d5a0a47b 7155 {
e4c6a07a 7156 if (MEM_P (op))
d5a0a47b 7157 {
e4c6a07a
BS
7158 /* If !arm_arch4, we use one of the extendhisi2_mem
7159 or movhi_bytes patterns for HImode. For a QImode
7160 sign extension, we first zero-extend from memory
7161 and then perform a shift sequence. */
7162 if (!arm_arch4 && (opmode != QImode || code == SIGN_EXTEND))
7163 *total += COSTS_N_INSNS (2);
d5a0a47b 7164 }
e4c6a07a
BS
7165 else if (arm_arch6)
7166 *total += COSTS_N_INSNS (1);
7167
7168 /* We don't have the necessary insn, so we need to perform some
7169 other operation. */
7170 else if (TARGET_ARM && code == ZERO_EXTEND && mode == QImode)
7171 /* An and with constant 255. */
7172 *total += COSTS_N_INSNS (1);
7173 else
7174 /* A shift sequence. Increase costs slightly to avoid
7175 combining two shifts into an extend operation. */
7176 *total += COSTS_N_INSNS (2) + 1;
d5a0a47b 7177 }
e2c671ba 7178
d5a0a47b
RE
7179 return false;
7180 }
ad076f4e 7181
d5a0a47b
RE
7182 switch (GET_MODE (XEXP (x, 0)))
7183 {
5a9335ef
NC
7184 case V8QImode:
7185 case V4HImode:
7186 case V2SImode:
7187 case V4QImode:
7188 case V2HImode:
d5a0a47b
RE
7189 *total = COSTS_N_INSNS (1);
7190 return false;
5a9335ef 7191
ad076f4e 7192 default:
e6d29d15 7193 gcc_unreachable ();
e2c671ba 7194 }
e6d29d15 7195 gcc_unreachable ();
e2c671ba 7196
d5a0a47b
RE
7197 case ZERO_EXTRACT:
7198 case SIGN_EXTRACT:
7199 *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
7200 return true;
7201
f676971a 7202 case CONST_INT:
d5a0a47b
RE
7203 if (const_ok_for_arm (INTVAL (x))
7204 || const_ok_for_arm (~INTVAL (x)))
7205 *total = COSTS_N_INSNS (1);
f676971a 7206 else
d5a0a47b
RE
7207 *total = COSTS_N_INSNS (arm_gen_constant (SET, mode, NULL_RTX,
7208 INTVAL (x), NULL_RTX,
7209 NULL_RTX, 0, 0));
7210 return true;
f676971a
EC
7211
7212 case CONST:
7213 case LABEL_REF:
7214 case SYMBOL_REF:
d5a0a47b
RE
7215 *total = COSTS_N_INSNS (3);
7216 return true;
f676971a 7217
571191af 7218 case HIGH:
d5a0a47b
RE
7219 *total = COSTS_N_INSNS (1);
7220 return true;
7221
571191af 7222 case LO_SUM:
d5a0a47b
RE
7223 *total = COSTS_N_INSNS (1);
7224 *total += rtx_cost (XEXP (x, 0), code, speed);
7225 return true;
571191af 7226
f676971a 7227 case CONST_DOUBLE:
e0dc3601
PB
7228 if (TARGET_HARD_FLOAT && vfp3_const_double_rtx (x)
7229 && (mode == SFmode || !TARGET_VFP_SINGLE))
d5a0a47b
RE
7230 *total = COSTS_N_INSNS (1);
7231 else
7232 *total = COSTS_N_INSNS (4);
7233 return true;
f676971a 7234
e2c671ba 7235 default:
d5a0a47b
RE
7236 *total = COSTS_N_INSNS (4);
7237 return false;
e2c671ba
RE
7238 }
7239}
32de079a 7240
7548c1be
WG
7241/* Estimates the size cost of thumb1 instructions.
7242 For now most of the code is copied from thumb1_rtx_costs. We need more
7243 fine grain tuning when we have more related test cases. */
7244static inline int
7245thumb1_size_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
7246{
7247 enum machine_mode mode = GET_MODE (x);
7248
7249 switch (code)
7250 {
7251 case ASHIFT:
7252 case ASHIFTRT:
7253 case LSHIFTRT:
7254 case ROTATERT:
7255 case PLUS:
7256 case MINUS:
7257 case COMPARE:
7258 case NEG:
7259 case NOT:
7260 return COSTS_N_INSNS (1);
7261
7262 case MULT:
7263 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7264 {
7265 /* Thumb1 mul instruction can't operate on const. We must Load it
7266 into a register first. */
7267 int const_size = thumb1_size_rtx_costs (XEXP (x, 1), CONST_INT, SET);
7268 return COSTS_N_INSNS (1) + const_size;
7269 }
7270 return COSTS_N_INSNS (1);
7271
7272 case SET:
7273 return (COSTS_N_INSNS (1)
7274 + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
7275 + GET_CODE (SET_DEST (x)) == MEM));
7276
7277 case CONST_INT:
7278 if (outer == SET)
7279 {
7280 if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
9b9ee6d3 7281 return COSTS_N_INSNS (1);
3393e880
MK
7282 /* See split "TARGET_THUMB1 && satisfies_constraint_J". */
7283 if (INTVAL (x) >= -255 && INTVAL (x) <= -1)
7284 return COSTS_N_INSNS (2);
7285 /* See split "TARGET_THUMB1 && satisfies_constraint_K". */
7548c1be
WG
7286 if (thumb_shiftable_const (INTVAL (x)))
7287 return COSTS_N_INSNS (2);
7288 return COSTS_N_INSNS (3);
7289 }
7290 else if ((outer == PLUS || outer == COMPARE)
7291 && INTVAL (x) < 256 && INTVAL (x) > -256)
7292 return 0;
7293 else if ((outer == IOR || outer == XOR || outer == AND)
7294 && INTVAL (x) < 256 && INTVAL (x) >= -256)
7295 return COSTS_N_INSNS (1);
7296 else if (outer == AND)
7297 {
7298 int i;
7299 /* This duplicates the tests in the andsi3 expander. */
7300 for (i = 9; i <= 31; i++)
7301 if ((((HOST_WIDE_INT) 1) << i) - 1 == INTVAL (x)
7302 || (((HOST_WIDE_INT) 1) << i) - 1 == ~INTVAL (x))
7303 return COSTS_N_INSNS (2);
7304 }
7305 else if (outer == ASHIFT || outer == ASHIFTRT
7306 || outer == LSHIFTRT)
7307 return 0;
7308 return COSTS_N_INSNS (2);
7309
7310 case CONST:
7311 case CONST_DOUBLE:
7312 case LABEL_REF:
7313 case SYMBOL_REF:
7314 return COSTS_N_INSNS (3);
7315
7316 case UDIV:
7317 case UMOD:
7318 case DIV:
7319 case MOD:
7320 return 100;
7321
7322 case TRUNCATE:
7323 return 99;
7324
7325 case AND:
7326 case XOR:
7327 case IOR:
7328 /* XXX guess. */
7329 return 8;
7330
7331 case MEM:
7332 /* XXX another guess. */
7333 /* Memory costs quite a lot for the first word, but subsequent words
7334 load at the equivalent of a single insn each. */
7335 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
7336 + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
7337 ? 4 : 0));
7338
7339 case IF_THEN_ELSE:
7340 /* XXX a guess. */
7341 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
7342 return 14;
7343 return 2;
7344
7345 case ZERO_EXTEND:
7346 /* XXX still guessing. */
7347 switch (GET_MODE (XEXP (x, 0)))
7348 {
7349 case QImode:
7350 return (1 + (mode == DImode ? 4 : 0)
7351 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
7352
7353 case HImode:
7354 return (4 + (mode == DImode ? 4 : 0)
7355 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
7356
7357 case SImode:
7358 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
7359
7360 default:
7361 return 99;
7362 }
7363
7364 default:
7365 return 99;
7366 }
7367}
7368
21b5653c
RE
7369/* RTX costs when optimizing for size. */
7370static bool
d5a0a47b
RE
7371arm_size_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7372 int *total)
21b5653c
RE
7373{
7374 enum machine_mode mode = GET_MODE (x);
09754904 7375 if (TARGET_THUMB1)
21b5653c 7376 {
7548c1be 7377 *total = thumb1_size_rtx_costs (x, code, outer_code);
21b5653c
RE
7378 return true;
7379 }
7380
09754904 7381 /* FIXME: This makes no attempt to prefer narrow Thumb-2 instructions. */
21b5653c
RE
7382 switch (code)
7383 {
7384 case MEM:
f676971a 7385 /* A memory access costs 1 insn if the mode is small, or the address is
21b5653c
RE
7386 a single register, otherwise it costs one insn per word. */
7387 if (REG_P (XEXP (x, 0)))
7388 *total = COSTS_N_INSNS (1);
d37c3c62
MK
7389 else if (flag_pic
7390 && GET_CODE (XEXP (x, 0)) == PLUS
7391 && will_be_in_index_register (XEXP (XEXP (x, 0), 1)))
7392 /* This will be split into two instructions.
7393 See arm.md:calculate_pic_address. */
7394 *total = COSTS_N_INSNS (2);
21b5653c
RE
7395 else
7396 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7397 return true;
7398
7399 case DIV:
7400 case MOD:
7401 case UDIV:
7402 case UMOD:
7403 /* Needs a libcall, so it costs about this. */
7404 *total = COSTS_N_INSNS (2);
7405 return false;
7406
7407 case ROTATE:
7408 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
7409 {
f40751dd 7410 *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, false);
21b5653c
RE
7411 return true;
7412 }
7413 /* Fall through */
7414 case ROTATERT:
7415 case ASHIFT:
7416 case LSHIFTRT:
7417 case ASHIFTRT:
7418 if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
7419 {
f40751dd 7420 *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code, false);
21b5653c
RE
7421 return true;
7422 }
7423 else if (mode == SImode)
7424 {
f40751dd 7425 *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, false);
21b5653c
RE
7426 /* Slightly disparage register shifts, but not by much. */
7427 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
f40751dd 7428 *total += 1 + rtx_cost (XEXP (x, 1), code, false);
21b5653c
RE
7429 return true;
7430 }
7431
7432 /* Needs a libcall. */
7433 *total = COSTS_N_INSNS (2);
7434 return false;
7435
7436 case MINUS:
e0dc3601
PB
7437 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7438 && (mode == SFmode || !TARGET_VFP_SINGLE))
21b5653c
RE
7439 {
7440 *total = COSTS_N_INSNS (1);
7441 return false;
7442 }
7443
7444 if (mode == SImode)
7445 {
7446 enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
7447 enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
7448
7449 if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
7450 || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
7451 || subcode1 == ROTATE || subcode1 == ROTATERT
7452 || subcode1 == ASHIFT || subcode1 == LSHIFTRT
7453 || subcode1 == ASHIFTRT)
7454 {
7455 /* It's just the cost of the two operands. */
7456 *total = 0;
7457 return false;
7458 }
7459
7460 *total = COSTS_N_INSNS (1);
7461 return false;
7462 }
7463
7464 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7465 return false;
7466
f676971a 7467 case PLUS:
e0dc3601
PB
7468 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7469 && (mode == SFmode || !TARGET_VFP_SINGLE))
21b5653c
RE
7470 {
7471 *total = COSTS_N_INSNS (1);
7472 return false;
7473 }
7474
6e782a29
KH
7475 /* A shift as a part of ADD costs nothing. */
7476 if (GET_CODE (XEXP (x, 0)) == MULT
7477 && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
7478 {
7479 *total = COSTS_N_INSNS (TARGET_THUMB2 ? 2 : 1);
7480 *total += rtx_cost (XEXP (XEXP (x, 0), 0), code, false);
7481 *total += rtx_cost (XEXP (x, 1), code, false);
7482 return true;
7483 }
7484
21b5653c
RE
7485 /* Fall through */
7486 case AND: case XOR: case IOR:
7487 if (mode == SImode)
7488 {
7489 enum rtx_code subcode = GET_CODE (XEXP (x, 0));
7490
7491 if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
7492 || subcode == LSHIFTRT || subcode == ASHIFTRT
7493 || (code == AND && subcode == NOT))
7494 {
7495 /* It's just the cost of the two operands. */
7496 *total = 0;
7497 return false;
7498 }
7499 }
7500
7501 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7502 return false;
7503
7504 case MULT:
7505 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7506 return false;
7507
7508 case NEG:
e0dc3601
PB
7509 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7510 && (mode == SFmode || !TARGET_VFP_SINGLE))
7ce8451d
MG
7511 {
7512 *total = COSTS_N_INSNS (1);
7513 return false;
7514 }
7515
21b5653c
RE
7516 /* Fall through */
7517 case NOT:
7518 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7519
7520 return false;
7521
7522 case IF_THEN_ELSE:
7523 *total = 0;
7524 return false;
7525
7526 case COMPARE:
7527 if (cc_register (XEXP (x, 0), VOIDmode))
7528 * total = 0;
7529 else
7530 *total = COSTS_N_INSNS (1);
7531 return false;
7532
7533 case ABS:
e0dc3601
PB
7534 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7535 && (mode == SFmode || !TARGET_VFP_SINGLE))
21b5653c
RE
7536 *total = COSTS_N_INSNS (1);
7537 else
7538 *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
7539 return false;
7540
7541 case SIGN_EXTEND:
21b5653c 7542 case ZERO_EXTEND:
e4c6a07a 7543 return arm_rtx_costs_1 (x, outer_code, total, 0);
21b5653c 7544
f676971a
EC
7545 case CONST_INT:
7546 if (const_ok_for_arm (INTVAL (x)))
6e782a29
KH
7547 /* A multiplication by a constant requires another instruction
7548 to load the constant to a register. */
7549 *total = COSTS_N_INSNS ((outer_code == SET || outer_code == MULT)
7550 ? 1 : 0);
21b5653c
RE
7551 else if (const_ok_for_arm (~INTVAL (x)))
7552 *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
7553 else if (const_ok_for_arm (-INTVAL (x)))
7554 {
7555 if (outer_code == COMPARE || outer_code == PLUS
7556 || outer_code == MINUS)
7557 *total = 0;
7558 else
7559 *total = COSTS_N_INSNS (1);
7560 }
7561 else
7562 *total = COSTS_N_INSNS (2);
7563 return true;
f676971a
EC
7564
7565 case CONST:
7566 case LABEL_REF:
7567 case SYMBOL_REF:
21b5653c
RE
7568 *total = COSTS_N_INSNS (2);
7569 return true;
f676971a 7570
21b5653c
RE
7571 case CONST_DOUBLE:
7572 *total = COSTS_N_INSNS (4);
7573 return true;
7574
571191af
PB
7575 case HIGH:
7576 case LO_SUM:
7577 /* We prefer constant pool entries to MOVW/MOVT pairs, so bump the
7578 cost of these slightly. */
7579 *total = COSTS_N_INSNS (1) + 1;
7580 return true;
7581
21b5653c
RE
7582 default:
7583 if (mode != VOIDmode)
7584 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7585 else
7586 *total = COSTS_N_INSNS (4); /* How knows? */
7587 return false;
7588 }
7589}
7590
f40751dd
JH
7591/* RTX costs when optimizing for size. */
7592static bool
d5a0a47b
RE
7593arm_rtx_costs (rtx x, int code, int outer_code, int *total,
7594 bool speed)
f40751dd
JH
7595{
7596 if (!speed)
bbbbb16a
ILT
7597 return arm_size_rtx_costs (x, (enum rtx_code) code,
7598 (enum rtx_code) outer_code, total);
f40751dd 7599 else
1b78f575
RE
7600 return current_tune->rtx_costs (x, (enum rtx_code) code,
7601 (enum rtx_code) outer_code,
7602 total, speed);
f40751dd
JH
7603}
7604
5b3e6663
PB
7605/* RTX costs for cores with a slow MUL implementation. Thumb-2 is not
7606 supported on any "slowmul" cores, so it can be ignored. */
9b66ebb1 7607
3c50106f 7608static bool
d5a0a47b
RE
7609arm_slowmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7610 int *total, bool speed)
3c50106f 7611{
9b66ebb1
PB
7612 enum machine_mode mode = GET_MODE (x);
7613
7614 if (TARGET_THUMB)
7615 {
5b3e6663 7616 *total = thumb1_rtx_costs (x, code, outer_code);
9b66ebb1
PB
7617 return true;
7618 }
f676971a 7619
9b66ebb1
PB
7620 switch (code)
7621 {
7622 case MULT:
7623 if (GET_MODE_CLASS (mode) == MODE_FLOAT
7624 || mode == DImode)
7625 {
d5a0a47b
RE
7626 *total = COSTS_N_INSNS (20);
7627 return false;
9b66ebb1
PB
7628 }
7629
7630 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7631 {
7632 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
7633 & (unsigned HOST_WIDE_INT) 0xffffffff);
7634 int cost, const_ok = const_ok_for_arm (i);
7635 int j, booth_unit_size;
7636
f676971a 7637 /* Tune as appropriate. */
9b66ebb1
PB
7638 cost = const_ok ? 4 : 8;
7639 booth_unit_size = 2;
7640 for (j = 0; i && j < 32; j += booth_unit_size)
7641 {
7642 i >>= booth_unit_size;
d5a0a47b 7643 cost++;
9b66ebb1
PB
7644 }
7645
d5a0a47b
RE
7646 *total = COSTS_N_INSNS (cost);
7647 *total += rtx_cost (XEXP (x, 0), code, speed);
9b66ebb1
PB
7648 return true;
7649 }
7650
d5a0a47b
RE
7651 *total = COSTS_N_INSNS (20);
7652 return false;
f676971a 7653
9b66ebb1 7654 default:
d5a0a47b 7655 return arm_rtx_costs_1 (x, outer_code, total, speed);;
9b66ebb1 7656 }
3c50106f
RH
7657}
7658
9b66ebb1
PB
7659
7660/* RTX cost for cores with a fast multiply unit (M variants). */
7661
7662static bool
d5a0a47b
RE
7663arm_fastmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7664 int *total, bool speed)
9b66ebb1
PB
7665{
7666 enum machine_mode mode = GET_MODE (x);
7667
5b3e6663 7668 if (TARGET_THUMB1)
9b66ebb1 7669 {
5b3e6663 7670 *total = thumb1_rtx_costs (x, code, outer_code);
9b66ebb1
PB
7671 return true;
7672 }
f676971a 7673
5b3e6663 7674 /* ??? should thumb2 use different costs? */
9b66ebb1
PB
7675 switch (code)
7676 {
7677 case MULT:
7678 /* There is no point basing this on the tuning, since it is always the
7679 fast variant if it exists at all. */
7680 if (mode == DImode
7681 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
7682 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7683 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7684 {
d5a0a47b
RE
7685 *total = COSTS_N_INSNS(2);
7686 return false;
9b66ebb1 7687 }
f676971a 7688
9b66ebb1 7689
d5a0a47b 7690 if (mode == DImode)
9b66ebb1 7691 {
d5a0a47b
RE
7692 *total = COSTS_N_INSNS (5);
7693 return false;
9b66ebb1
PB
7694 }
7695
7696 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7697 {
7698 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
7699 & (unsigned HOST_WIDE_INT) 0xffffffff);
7700 int cost, const_ok = const_ok_for_arm (i);
7701 int j, booth_unit_size;
7702
f676971a 7703 /* Tune as appropriate. */
9b66ebb1
PB
7704 cost = const_ok ? 4 : 8;
7705 booth_unit_size = 8;
7706 for (j = 0; i && j < 32; j += booth_unit_size)
7707 {
7708 i >>= booth_unit_size;
d5a0a47b 7709 cost++;
9b66ebb1
PB
7710 }
7711
d5a0a47b
RE
7712 *total = COSTS_N_INSNS(cost);
7713 return false;
9b66ebb1
PB
7714 }
7715
d5a0a47b
RE
7716 if (mode == SImode)
7717 {
7718 *total = COSTS_N_INSNS (4);
7719 return false;
7720 }
7721
7722 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7723 {
e0dc3601
PB
7724 if (TARGET_HARD_FLOAT
7725 && (mode == SFmode
7726 || (mode == DFmode && !TARGET_VFP_SINGLE)))
d5a0a47b
RE
7727 {
7728 *total = COSTS_N_INSNS (1);
7729 return false;
7730 }
7731 }
7732
7733 /* Requires a lib call */
7734 *total = COSTS_N_INSNS (20);
7735 return false;
f676971a 7736
9b66ebb1 7737 default:
d5a0a47b 7738 return arm_rtx_costs_1 (x, outer_code, total, speed);
9b66ebb1
PB
7739 }
7740}
7741
7742
5b3e6663
PB
7743/* RTX cost for XScale CPUs. Thumb-2 is not supported on any xscale cores,
7744 so it can be ignored. */
9b66ebb1
PB
7745
7746static bool
1b78f575
RE
7747arm_xscale_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7748 int *total, bool speed)
9b66ebb1
PB
7749{
7750 enum machine_mode mode = GET_MODE (x);
7751
7752 if (TARGET_THUMB)
7753 {
5b3e6663 7754 *total = thumb1_rtx_costs (x, code, outer_code);
9b66ebb1
PB
7755 return true;
7756 }
f676971a 7757
9b66ebb1
PB
7758 switch (code)
7759 {
d5a0a47b
RE
7760 case COMPARE:
7761 if (GET_CODE (XEXP (x, 0)) != MULT)
7762 return arm_rtx_costs_1 (x, outer_code, total, speed);
7763
7764 /* A COMPARE of a MULT is slow on XScale; the muls instruction
7765 will stall until the multiplication is complete. */
7766 *total = COSTS_N_INSNS (3);
7767 return false;
7768
9b66ebb1
PB
7769 case MULT:
7770 /* There is no point basing this on the tuning, since it is always the
7771 fast variant if it exists at all. */
7772 if (mode == DImode
7773 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
7774 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7775 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7776 {
d5a0a47b
RE
7777 *total = COSTS_N_INSNS (2);
7778 return false;
9b66ebb1 7779 }
f676971a 7780
9b66ebb1 7781
d5a0a47b 7782 if (mode == DImode)
9b66ebb1 7783 {
d5a0a47b
RE
7784 *total = COSTS_N_INSNS (5);
7785 return false;
9b66ebb1
PB
7786 }
7787
7788 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7789 {
d5a0a47b
RE
7790 /* If operand 1 is a constant we can more accurately
7791 calculate the cost of the multiply. The multiplier can
7792 retire 15 bits on the first cycle and a further 12 on the
7793 second. We do, of course, have to load the constant into
7794 a register first. */
7795 unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
7796 /* There's a general overhead of one cycle. */
7797 int cost = 1;
9b66ebb1
PB
7798 unsigned HOST_WIDE_INT masked_const;
7799
d5a0a47b
RE
7800 if (i & 0x80000000)
7801 i = ~i;
7802
7803 i &= (unsigned HOST_WIDE_INT) 0xffffffff;
7804
9b66ebb1 7805 masked_const = i & 0xffff8000;
d5a0a47b 7806 if (masked_const != 0)
9b66ebb1 7807 {
d5a0a47b 7808 cost++;
9b66ebb1 7809 masked_const = i & 0xf8000000;
d5a0a47b
RE
7810 if (masked_const != 0)
7811 cost++;
9b66ebb1 7812 }
d5a0a47b
RE
7813 *total = COSTS_N_INSNS (cost);
7814 return false;
9b66ebb1
PB
7815 }
7816
d5a0a47b
RE
7817 if (mode == SImode)
7818 {
7819 *total = COSTS_N_INSNS (3);
7820 return false;
7821 }
f676971a 7822
d5a0a47b
RE
7823 /* Requires a lib call */
7824 *total = COSTS_N_INSNS (20);
7825 return false;
06d5588c 7826
9b66ebb1 7827 default:
d5a0a47b 7828 return arm_rtx_costs_1 (x, outer_code, total, speed);
9b66ebb1
PB
7829 }
7830}
7831
7832
7833/* RTX costs for 9e (and later) cores. */
7834
7835static bool
d5a0a47b
RE
7836arm_9e_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7837 int *total, bool speed)
9b66ebb1
PB
7838{
7839 enum machine_mode mode = GET_MODE (x);
f676971a 7840
5b3e6663 7841 if (TARGET_THUMB1)
9b66ebb1
PB
7842 {
7843 switch (code)
7844 {
7845 case MULT:
7846 *total = COSTS_N_INSNS (3);
7847 return true;
f676971a 7848
9b66ebb1 7849 default:
5b3e6663 7850 *total = thumb1_rtx_costs (x, code, outer_code);
9b66ebb1
PB
7851 return true;
7852 }
7853 }
f676971a 7854
9b66ebb1
PB
7855 switch (code)
7856 {
7857 case MULT:
7858 /* There is no point basing this on the tuning, since it is always the
7859 fast variant if it exists at all. */
7860 if (mode == DImode
7861 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
7862 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7863 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7864 {
d5a0a47b
RE
7865 *total = COSTS_N_INSNS (2);
7866 return false;
9b66ebb1 7867 }
f676971a 7868
9b66ebb1 7869
9b66ebb1
PB
7870 if (mode == DImode)
7871 {
d5a0a47b
RE
7872 *total = COSTS_N_INSNS (5);
7873 return false;
9b66ebb1 7874 }
d5a0a47b
RE
7875
7876 if (mode == SImode)
9b66ebb1 7877 {
d5a0a47b
RE
7878 *total = COSTS_N_INSNS (2);
7879 return false;
9b66ebb1
PB
7880 }
7881
d5a0a47b
RE
7882 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7883 {
e0dc3601
PB
7884 if (TARGET_HARD_FLOAT
7885 && (mode == SFmode
7886 || (mode == DFmode && !TARGET_VFP_SINGLE)))
d5a0a47b
RE
7887 {
7888 *total = COSTS_N_INSNS (1);
7889 return false;
7890 }
7891 }
9b66ebb1 7892
d5a0a47b
RE
7893 *total = COSTS_N_INSNS (20);
7894 return false;
f676971a 7895
9b66ebb1 7896 default:
d5a0a47b 7897 return arm_rtx_costs_1 (x, outer_code, total, speed);
9b66ebb1
PB
7898 }
7899}
dcefdf67
RH
7900/* All address computations that can be done are free, but rtx cost returns
7901 the same for practically all of them. So we weight the different types
7902 of address here in the order (most pref first):
d6b4baa4 7903 PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL. */
d2b6eb76
ZW
7904static inline int
7905arm_arm_address_cost (rtx x)
7906{
7907 enum rtx_code c = GET_CODE (x);
7908
7909 if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
7910 return 0;
7911 if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
7912 return 10;
7913
17eb4921 7914 if (c == PLUS)
d2b6eb76 7915 {
17eb4921 7916 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
d2b6eb76
ZW
7917 return 2;
7918
ec8e098d 7919 if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
d2b6eb76
ZW
7920 return 3;
7921
7922 return 4;
7923 }
7924
7925 return 6;
7926}
7927
7928static inline int
7929arm_thumb_address_cost (rtx x)
7930{
7931 enum rtx_code c = GET_CODE (x);
7932
7933 if (c == REG)
7934 return 1;
7935 if (c == PLUS
7936 && GET_CODE (XEXP (x, 0)) == REG
7937 && GET_CODE (XEXP (x, 1)) == CONST_INT)
7938 return 1;
7939
7940 return 2;
7941}
7942
dcefdf67 7943static int
f40751dd 7944arm_address_cost (rtx x, bool speed ATTRIBUTE_UNUSED)
dcefdf67 7945{
5b3e6663 7946 return TARGET_32BIT ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
dcefdf67 7947}
906668bb 7948
b0c13111
RR
7949/* Adjust cost hook for XScale. */
7950static bool
7951xscale_sched_adjust_cost (rtx insn, rtx link, rtx dep, int * cost)
7952{
d19fb8e3
NC
7953 /* Some true dependencies can have a higher cost depending
7954 on precisely how certain input operands are used. */
b0c13111 7955 if (REG_NOTE_KIND(link) == 0
eda833e3
BE
7956 && recog_memoized (insn) >= 0
7957 && recog_memoized (dep) >= 0)
d19fb8e3
NC
7958 {
7959 int shift_opnum = get_attr_shift (insn);
7960 enum attr_type attr_type = get_attr_type (dep);
7961
7962 /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
7963 operand for INSN. If we have a shifted input operand and the
7964 instruction we depend on is another ALU instruction, then we may
7965 have to account for an additional stall. */
9b66ebb1
PB
7966 if (shift_opnum != 0
7967 && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
d19fb8e3
NC
7968 {
7969 rtx shifted_operand;
7970 int opno;
f676971a 7971
d19fb8e3
NC
7972 /* Get the shifted operand. */
7973 extract_insn (insn);
7974 shifted_operand = recog_data.operand[shift_opnum];
7975
7976 /* Iterate over all the operands in DEP. If we write an operand
7977 that overlaps with SHIFTED_OPERAND, then we have increase the
7978 cost of this dependency. */
7979 extract_insn (dep);
7980 preprocess_constraints ();
7981 for (opno = 0; opno < recog_data.n_operands; opno++)
7982 {
7983 /* We can ignore strict inputs. */
7984 if (recog_data.operand_type[opno] == OP_IN)
7985 continue;
7986
7987 if (reg_overlap_mentioned_p (recog_data.operand[opno],
7988 shifted_operand))
b0c13111
RR
7989 {
7990 *cost = 2;
7991 return false;
7992 }
d19fb8e3
NC
7993 }
7994 }
7995 }
b0c13111
RR
7996 return true;
7997}
7998
7999/* Adjust cost hook for Cortex A9. */
8000static bool
8001cortex_a9_sched_adjust_cost (rtx insn, rtx link, rtx dep, int * cost)
8002{
8003 switch (REG_NOTE_KIND (link))
8004 {
8005 case REG_DEP_ANTI:
8006 *cost = 0;
8007 return false;
8008
8009 case REG_DEP_TRUE:
8010 case REG_DEP_OUTPUT:
8011 if (recog_memoized (insn) >= 0
8012 && recog_memoized (dep) >= 0)
8013 {
8014 if (GET_CODE (PATTERN (insn)) == SET)
8015 {
8016 if (GET_MODE_CLASS
8017 (GET_MODE (SET_DEST (PATTERN (insn)))) == MODE_FLOAT
8018 || GET_MODE_CLASS
8019 (GET_MODE (SET_SRC (PATTERN (insn)))) == MODE_FLOAT)
8020 {
8021 enum attr_type attr_type_insn = get_attr_type (insn);
8022 enum attr_type attr_type_dep = get_attr_type (dep);
8023
8024 /* By default all dependencies of the form
8025 s0 = s0 <op> s1
8026 s0 = s0 <op> s2
8027 have an extra latency of 1 cycle because
8028 of the input and output dependency in this
8029 case. However this gets modeled as an true
8030 dependency and hence all these checks. */
8031 if (REG_P (SET_DEST (PATTERN (insn)))
8032 && REG_P (SET_DEST (PATTERN (dep)))
8033 && reg_overlap_mentioned_p (SET_DEST (PATTERN (insn)),
8034 SET_DEST (PATTERN (dep))))
8035 {
8036 /* FMACS is a special case where the dependant
8037 instruction can be issued 3 cycles before
8038 the normal latency in case of an output
8039 dependency. */
8040 if ((attr_type_insn == TYPE_FMACS
8041 || attr_type_insn == TYPE_FMACD)
8042 && (attr_type_dep == TYPE_FMACS
8043 || attr_type_dep == TYPE_FMACD))
8044 {
8045 if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
8046 *cost = insn_default_latency (dep) - 3;
8047 else
8048 *cost = insn_default_latency (dep);
8049 return false;
8050 }
8051 else
8052 {
8053 if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
8054 *cost = insn_default_latency (dep) + 1;
8055 else
8056 *cost = insn_default_latency (dep);
8057 }
8058 return false;
8059 }
8060 }
8061 }
8062 }
8063 break;
8064
8065 default:
8066 gcc_unreachable ();
8067 }
8068
8069 return true;
8070}
8071
c02a5ccb
SL
8072/* Adjust cost hook for FA726TE. */
8073static bool
8074fa726te_sched_adjust_cost (rtx insn, rtx link, rtx dep, int * cost)
8075{
8076 /* For FA726TE, true dependency on CPSR (i.e. set cond followed by predicated)
8077 have penalty of 3. */
8078 if (REG_NOTE_KIND (link) == REG_DEP_TRUE
8079 && recog_memoized (insn) >= 0
8080 && recog_memoized (dep) >= 0
8081 && get_attr_conds (dep) == CONDS_SET)
8082 {
8083 /* Use of carry (e.g. 64-bit arithmetic) in ALU: 3-cycle latency. */
8084 if (get_attr_conds (insn) == CONDS_USE
8085 && get_attr_type (insn) != TYPE_BRANCH)
8086 {
8087 *cost = 3;
8088 return false;
8089 }
8090
8091 if (GET_CODE (PATTERN (insn)) == COND_EXEC
8092 || get_attr_conds (insn) == CONDS_USE)
8093 {
8094 *cost = 0;
8095 return false;
8096 }
8097 }
8098
8099 return true;
8100}
8101
b0c13111
RR
8102/* This function implements the target macro TARGET_SCHED_ADJUST_COST.
8103 It corrects the value of COST based on the relationship between
8104 INSN and DEP through the dependence LINK. It returns the new
8105 value. There is a per-core adjust_cost hook to adjust scheduler costs
8106 and the per-core hook can choose to completely override the generic
8107 adjust_cost function. Only put bits of code into arm_adjust_cost that
8108 are common across all cores. */
8109static int
8110arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
8111{
8112 rtx i_pat, d_pat;
8113
8114 /* When generating Thumb-1 code, we want to place flag-setting operations
8115 close to a conditional branch which depends on them, so that we can
8116 omit the comparison. */
8117 if (TARGET_THUMB1
8118 && REG_NOTE_KIND (link) == 0
8119 && recog_memoized (insn) == CODE_FOR_cbranchsi4_insn
8120 && recog_memoized (dep) >= 0
8121 && get_attr_conds (dep) == CONDS_SET)
8122 return 0;
8123
8124 if (current_tune->sched_adjust_cost != NULL)
8125 {
8126 if (!current_tune->sched_adjust_cost (insn, link, dep, &cost))
8127 return cost;
8128 }
d19fb8e3 8129
6354dc9b 8130 /* XXX This is not strictly true for the FPA. */
d5b7b3ae
RE
8131 if (REG_NOTE_KIND (link) == REG_DEP_ANTI
8132 || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
b36ba79f
RE
8133 return 0;
8134
d5b7b3ae
RE
8135 /* Call insns don't incur a stall, even if they follow a load. */
8136 if (REG_NOTE_KIND (link) == 0
8137 && GET_CODE (insn) == CALL_INSN)
8138 return 1;
8139
32de079a
RE
8140 if ((i_pat = single_set (insn)) != NULL
8141 && GET_CODE (SET_SRC (i_pat)) == MEM
8142 && (d_pat = single_set (dep)) != NULL
8143 && GET_CODE (SET_DEST (d_pat)) == MEM)
8144 {
48f6efae 8145 rtx src_mem = XEXP (SET_SRC (i_pat), 0);
32de079a
RE
8146 /* This is a load after a store, there is no conflict if the load reads
8147 from a cached area. Assume that loads from the stack, and from the
f676971a 8148 constant pool are cached, and that others will miss. This is a
6354dc9b 8149 hack. */
f676971a 8150
b0c13111
RR
8151 if ((GET_CODE (src_mem) == SYMBOL_REF
8152 && CONSTANT_POOL_ADDRESS_P (src_mem))
48f6efae
NC
8153 || reg_mentioned_p (stack_pointer_rtx, src_mem)
8154 || reg_mentioned_p (frame_pointer_rtx, src_mem)
8155 || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
949d79eb 8156 return 1;
32de079a
RE
8157 }
8158
8159 return cost;
8160}
8161
9b66ebb1 8162static int fp_consts_inited = 0;
ff9940b0 8163
9b66ebb1
PB
8164/* Only zero is valid for VFP. Other values are also valid for FPA. */
8165static const char * const strings_fp[8] =
62b10bbc 8166{
2b835d68
RE
8167 "0", "1", "2", "3",
8168 "4", "5", "0.5", "10"
8169};
ff9940b0 8170
9b66ebb1 8171static REAL_VALUE_TYPE values_fp[8];
ff9940b0
RE
8172
8173static void
9b66ebb1 8174init_fp_table (void)
ff9940b0
RE
8175{
8176 int i;
8177 REAL_VALUE_TYPE r;
8178
9b66ebb1
PB
8179 if (TARGET_VFP)
8180 fp_consts_inited = 1;
8181 else
8182 fp_consts_inited = 8;
8183
8184 for (i = 0; i < fp_consts_inited; i++)
ff9940b0 8185 {
9b66ebb1
PB
8186 r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
8187 values_fp[i] = r;
ff9940b0 8188 }
ff9940b0
RE
8189}
8190
9b66ebb1 8191/* Return TRUE if rtx X is a valid immediate FP constant. */
cce8749e 8192int
9b66ebb1 8193arm_const_double_rtx (rtx x)
cce8749e 8194{
ff9940b0
RE
8195 REAL_VALUE_TYPE r;
8196 int i;
f676971a 8197
9b66ebb1
PB
8198 if (!fp_consts_inited)
8199 init_fp_table ();
f676971a 8200
ff9940b0
RE
8201 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8202 if (REAL_VALUE_MINUS_ZERO (r))
8203 return 0;
f3bb6135 8204
9b66ebb1
PB
8205 for (i = 0; i < fp_consts_inited; i++)
8206 if (REAL_VALUES_EQUAL (r, values_fp[i]))
ff9940b0 8207 return 1;
f3bb6135 8208
ff9940b0 8209 return 0;
f3bb6135 8210}
ff9940b0 8211
3b684012 8212/* Return TRUE if rtx X is a valid immediate FPA constant. */
ff9940b0 8213int
e32bac5b 8214neg_const_double_rtx_ok_for_fpa (rtx x)
ff9940b0
RE
8215{
8216 REAL_VALUE_TYPE r;
8217 int i;
f676971a 8218
9b66ebb1
PB
8219 if (!fp_consts_inited)
8220 init_fp_table ();
f676971a 8221
ff9940b0 8222 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
d49b6e1e 8223 r = real_value_negate (&r);
ff9940b0
RE
8224 if (REAL_VALUE_MINUS_ZERO (r))
8225 return 0;
f3bb6135 8226
ff9940b0 8227 for (i = 0; i < 8; i++)
9b66ebb1 8228 if (REAL_VALUES_EQUAL (r, values_fp[i]))
ff9940b0 8229 return 1;
f3bb6135 8230
ff9940b0 8231 return 0;
f3bb6135 8232}
f1adb0a9
JB
8233
8234
8235/* VFPv3 has a fairly wide range of representable immediates, formed from
8236 "quarter-precision" floating-point values. These can be evaluated using this
8237 formula (with ^ for exponentiation):
8238
8239 -1^s * n * 2^-r
8240
8241 Where 's' is a sign bit (0/1), 'n' and 'r' are integers such that
8242 16 <= n <= 31 and 0 <= r <= 7.
8243
8244 These values are mapped onto an 8-bit integer ABCDEFGH s.t.
8245
8246 - A (most-significant) is the sign bit.
8247 - BCD are the exponent (encoded as r XOR 3).
8248 - EFGH are the mantissa (encoded as n - 16).
8249*/
8250
8251/* Return an integer index for a VFPv3 immediate operand X suitable for the
8252 fconst[sd] instruction, or -1 if X isn't suitable. */
8253static int
8254vfp3_const_double_index (rtx x)
8255{
8256 REAL_VALUE_TYPE r, m;
8257 int sign, exponent;
8258 unsigned HOST_WIDE_INT mantissa, mant_hi;
8259 unsigned HOST_WIDE_INT mask;
8e39e9af 8260 HOST_WIDE_INT m1, m2;
f1adb0a9
JB
8261 int point_pos = 2 * HOST_BITS_PER_WIDE_INT - 1;
8262
8263 if (!TARGET_VFP3 || GET_CODE (x) != CONST_DOUBLE)
8264 return -1;
8265
8266 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8267
8268 /* We can't represent these things, so detect them first. */
8269 if (REAL_VALUE_ISINF (r) || REAL_VALUE_ISNAN (r) || REAL_VALUE_MINUS_ZERO (r))
8270 return -1;
8271
8272 /* Extract sign, exponent and mantissa. */
8273 sign = REAL_VALUE_NEGATIVE (r) ? 1 : 0;
d49b6e1e 8274 r = real_value_abs (&r);
f1adb0a9
JB
8275 exponent = REAL_EXP (&r);
8276 /* For the mantissa, we expand into two HOST_WIDE_INTS, apart from the
8277 highest (sign) bit, with a fixed binary point at bit point_pos.
8278 WARNING: If there's ever a VFP version which uses more than 2 * H_W_I - 1
8279 bits for the mantissa, this may fail (low bits would be lost). */
8280 real_ldexp (&m, &r, point_pos - exponent);
8e39e9af
RE
8281 REAL_VALUE_TO_INT (&m1, &m2, m);
8282 mantissa = m1;
8283 mant_hi = m2;
f1adb0a9
JB
8284
8285 /* If there are bits set in the low part of the mantissa, we can't
8286 represent this value. */
8287 if (mantissa != 0)
8288 return -1;
8289
8290 /* Now make it so that mantissa contains the most-significant bits, and move
8291 the point_pos to indicate that the least-significant bits have been
8292 discarded. */
8293 point_pos -= HOST_BITS_PER_WIDE_INT;
8294 mantissa = mant_hi;
8295
8296 /* We can permit four significant bits of mantissa only, plus a high bit
8297 which is always 1. */
8298 mask = ((unsigned HOST_WIDE_INT)1 << (point_pos - 5)) - 1;
8299 if ((mantissa & mask) != 0)
8300 return -1;
8301
8302 /* Now we know the mantissa is in range, chop off the unneeded bits. */
8303 mantissa >>= point_pos - 5;
8304
8305 /* The mantissa may be zero. Disallow that case. (It's possible to load the
8306 floating-point immediate zero with Neon using an integer-zero load, but
8307 that case is handled elsewhere.) */
8308 if (mantissa == 0)
8309 return -1;
8310
8311 gcc_assert (mantissa >= 16 && mantissa <= 31);
8312
8313 /* The value of 5 here would be 4 if GCC used IEEE754-like encoding (where
6ed3da00
KH
8314 normalized significands are in the range [1, 2). (Our mantissa is shifted
8315 left 4 places at this point relative to normalized IEEE754 values). GCC
f1adb0a9
JB
8316 internally uses [0.5, 1) (see real.c), so the exponent returned from
8317 REAL_EXP must be altered. */
8318 exponent = 5 - exponent;
8319
8320 if (exponent < 0 || exponent > 7)
8321 return -1;
8322
8323 /* Sign, mantissa and exponent are now in the correct form to plug into the
15dc95cb 8324 formula described in the comment above. */
f1adb0a9
JB
8325 return (sign << 7) | ((exponent ^ 3) << 4) | (mantissa - 16);
8326}
8327
8328/* Return TRUE if rtx X is a valid immediate VFPv3 constant. */
8329int
8330vfp3_const_double_rtx (rtx x)
8331{
8332 if (!TARGET_VFP3)
8333 return 0;
8334
8335 return vfp3_const_double_index (x) != -1;
8336}
8337
88f77cba
JB
8338/* Recognize immediates which can be used in various Neon instructions. Legal
8339 immediates are described by the following table (for VMVN variants, the
8340 bitwise inverse of the constant shown is recognized. In either case, VMOV
8341 is output and the correct instruction to use for a given constant is chosen
8342 by the assembler). The constant shown is replicated across all elements of
8343 the destination vector.
8344
8345 insn elems variant constant (binary)
8346 ---- ----- ------- -----------------
8347 vmov i32 0 00000000 00000000 00000000 abcdefgh
8348 vmov i32 1 00000000 00000000 abcdefgh 00000000
8349 vmov i32 2 00000000 abcdefgh 00000000 00000000
8350 vmov i32 3 abcdefgh 00000000 00000000 00000000
8351 vmov i16 4 00000000 abcdefgh
8352 vmov i16 5 abcdefgh 00000000
8353 vmvn i32 6 00000000 00000000 00000000 abcdefgh
8354 vmvn i32 7 00000000 00000000 abcdefgh 00000000
8355 vmvn i32 8 00000000 abcdefgh 00000000 00000000
8356 vmvn i32 9 abcdefgh 00000000 00000000 00000000
8357 vmvn i16 10 00000000 abcdefgh
8358 vmvn i16 11 abcdefgh 00000000
8359 vmov i32 12 00000000 00000000 abcdefgh 11111111
8360 vmvn i32 13 00000000 00000000 abcdefgh 11111111
8361 vmov i32 14 00000000 abcdefgh 11111111 11111111
8362 vmvn i32 15 00000000 abcdefgh 11111111 11111111
8363 vmov i8 16 abcdefgh
8364 vmov i64 17 aaaaaaaa bbbbbbbb cccccccc dddddddd
8365 eeeeeeee ffffffff gggggggg hhhhhhhh
8366 vmov f32 18 aBbbbbbc defgh000 00000000 00000000
8367
8368 For case 18, B = !b. Representable values are exactly those accepted by
8369 vfp3_const_double_index, but are output as floating-point numbers rather
8370 than indices.
8371
8372 Variants 0-5 (inclusive) may also be used as immediates for the second
8373 operand of VORR/VBIC instructions.
8374
8375 The INVERSE argument causes the bitwise inverse of the given operand to be
8376 recognized instead (used for recognizing legal immediates for the VAND/VORN
8377 pseudo-instructions). If INVERSE is true, the value placed in *MODCONST is
8378 *not* inverted (i.e. the pseudo-instruction forms vand/vorn should still be
8379 output, rather than the real insns vbic/vorr).
8380
8381 INVERSE makes no difference to the recognition of float vectors.
8382
8383 The return value is the variant of immediate as shown in the above table, or
8384 -1 if the given value doesn't match any of the listed patterns.
8385*/
8386static int
8387neon_valid_immediate (rtx op, enum machine_mode mode, int inverse,
8388 rtx *modconst, int *elementwidth)
8389{
8390#define CHECK(STRIDE, ELSIZE, CLASS, TEST) \
8391 matches = 1; \
8392 for (i = 0; i < idx; i += (STRIDE)) \
8393 if (!(TEST)) \
8394 matches = 0; \
8395 if (matches) \
8396 { \
8397 immtype = (CLASS); \
8398 elsize = (ELSIZE); \
8399 break; \
8400 }
8401
ff128632 8402 unsigned int i, elsize = 0, idx = 0, n_elts = CONST_VECTOR_NUNITS (op);
88f77cba
JB
8403 unsigned int innersize = GET_MODE_SIZE (GET_MODE_INNER (mode));
8404 unsigned char bytes[16];
8405 int immtype = -1, matches;
8406 unsigned int invmask = inverse ? 0xff : 0;
8407
8408 /* Vectors of float constants. */
8409 if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
8410 {
8411 rtx el0 = CONST_VECTOR_ELT (op, 0);
8412 REAL_VALUE_TYPE r0;
8413
8414 if (!vfp3_const_double_rtx (el0))
8415 return -1;
8416
8417 REAL_VALUE_FROM_CONST_DOUBLE (r0, el0);
8418
8419 for (i = 1; i < n_elts; i++)
8420 {
8421 rtx elt = CONST_VECTOR_ELT (op, i);
8422 REAL_VALUE_TYPE re;
8423
8424 REAL_VALUE_FROM_CONST_DOUBLE (re, elt);
8425
8426 if (!REAL_VALUES_EQUAL (r0, re))
8427 return -1;
8428 }
8429
8430 if (modconst)
8431 *modconst = CONST_VECTOR_ELT (op, 0);
8432
8433 if (elementwidth)
8434 *elementwidth = 0;
8435
8436 return 18;
8437 }
8438
8439 /* Splat vector constant out into a byte vector. */
8440 for (i = 0; i < n_elts; i++)
8441 {
8442 rtx el = CONST_VECTOR_ELT (op, i);
8443 unsigned HOST_WIDE_INT elpart;
8444 unsigned int part, parts;
8445
8446 if (GET_CODE (el) == CONST_INT)
8447 {
8448 elpart = INTVAL (el);
8449 parts = 1;
8450 }
8451 else if (GET_CODE (el) == CONST_DOUBLE)
8452 {
8453 elpart = CONST_DOUBLE_LOW (el);
8454 parts = 2;
8455 }
8456 else
8457 gcc_unreachable ();
8458
8459 for (part = 0; part < parts; part++)
8460 {
8461 unsigned int byte;
8462 for (byte = 0; byte < innersize; byte++)
8463 {
8464 bytes[idx++] = (elpart & 0xff) ^ invmask;
8465 elpart >>= BITS_PER_UNIT;
8466 }
8467 if (GET_CODE (el) == CONST_DOUBLE)
8468 elpart = CONST_DOUBLE_HIGH (el);
8469 }
8470 }
8471
8472 /* Sanity check. */
8473 gcc_assert (idx == GET_MODE_SIZE (mode));
8474
8475 do
8476 {
8477 CHECK (4, 32, 0, bytes[i] == bytes[0] && bytes[i + 1] == 0
8478 && bytes[i + 2] == 0 && bytes[i + 3] == 0);
8479
8480 CHECK (4, 32, 1, bytes[i] == 0 && bytes[i + 1] == bytes[1]
8481 && bytes[i + 2] == 0 && bytes[i + 3] == 0);
8482
8483 CHECK (4, 32, 2, bytes[i] == 0 && bytes[i + 1] == 0
8484 && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
8485
8486 CHECK (4, 32, 3, bytes[i] == 0 && bytes[i + 1] == 0
8487 && bytes[i + 2] == 0 && bytes[i + 3] == bytes[3]);
8488
8489 CHECK (2, 16, 4, bytes[i] == bytes[0] && bytes[i + 1] == 0);
8490
8491 CHECK (2, 16, 5, bytes[i] == 0 && bytes[i + 1] == bytes[1]);
8492
8493 CHECK (4, 32, 6, bytes[i] == bytes[0] && bytes[i + 1] == 0xff
8494 && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
8495
8496 CHECK (4, 32, 7, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
8497 && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
8498
8499 CHECK (4, 32, 8, bytes[i] == 0xff && bytes[i + 1] == 0xff
8500 && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
8501
8502 CHECK (4, 32, 9, bytes[i] == 0xff && bytes[i + 1] == 0xff
8503 && bytes[i + 2] == 0xff && bytes[i + 3] == bytes[3]);
8504
8505 CHECK (2, 16, 10, bytes[i] == bytes[0] && bytes[i + 1] == 0xff);
8506
8507 CHECK (2, 16, 11, bytes[i] == 0xff && bytes[i + 1] == bytes[1]);
8508
8509 CHECK (4, 32, 12, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
8510 && bytes[i + 2] == 0 && bytes[i + 3] == 0);
8511
8512 CHECK (4, 32, 13, bytes[i] == 0 && bytes[i + 1] == bytes[1]
8513 && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
8514
8515 CHECK (4, 32, 14, bytes[i] == 0xff && bytes[i + 1] == 0xff
8516 && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
8517
8518 CHECK (4, 32, 15, bytes[i] == 0 && bytes[i + 1] == 0
8519 && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
8520
8521 CHECK (1, 8, 16, bytes[i] == bytes[0]);
8522
8523 CHECK (1, 64, 17, (bytes[i] == 0 || bytes[i] == 0xff)
8524 && bytes[i] == bytes[(i + 8) % idx]);
8525 }
8526 while (0);
8527
8528 if (immtype == -1)
8529 return -1;
8530
8531 if (elementwidth)
8532 *elementwidth = elsize;
8533
8534 if (modconst)
8535 {
8536 unsigned HOST_WIDE_INT imm = 0;
8537
cea618ac 8538 /* Un-invert bytes of recognized vector, if necessary. */
88f77cba
JB
8539 if (invmask != 0)
8540 for (i = 0; i < idx; i++)
8541 bytes[i] ^= invmask;
8542
8543 if (immtype == 17)
8544 {
8545 /* FIXME: Broken on 32-bit H_W_I hosts. */
8546 gcc_assert (sizeof (HOST_WIDE_INT) == 8);
8547
8548 for (i = 0; i < 8; i++)
8549 imm |= (unsigned HOST_WIDE_INT) (bytes[i] ? 0xff : 0)
8550 << (i * BITS_PER_UNIT);
8551
8552 *modconst = GEN_INT (imm);
8553 }
8554 else
8555 {
8556 unsigned HOST_WIDE_INT imm = 0;
8557
8558 for (i = 0; i < elsize / BITS_PER_UNIT; i++)
8559 imm |= (unsigned HOST_WIDE_INT) bytes[i] << (i * BITS_PER_UNIT);
8560
8561 *modconst = GEN_INT (imm);
8562 }
8563 }
8564
8565 return immtype;
8566#undef CHECK
8567}
8568
8569/* Return TRUE if rtx X is legal for use as either a Neon VMOV (or, implicitly,
8570 VMVN) immediate. Write back width per element to *ELEMENTWIDTH (or zero for
8571 float elements), and a modified constant (whatever should be output for a
8572 VMOV) in *MODCONST. */
8573
8574int
8575neon_immediate_valid_for_move (rtx op, enum machine_mode mode,
8576 rtx *modconst, int *elementwidth)
8577{
8578 rtx tmpconst;
8579 int tmpwidth;
8580 int retval = neon_valid_immediate (op, mode, 0, &tmpconst, &tmpwidth);
8581
8582 if (retval == -1)
8583 return 0;
8584
8585 if (modconst)
8586 *modconst = tmpconst;
8587
8588 if (elementwidth)
8589 *elementwidth = tmpwidth;
8590
8591 return 1;
8592}
8593
8594/* Return TRUE if rtx X is legal for use in a VORR or VBIC instruction. If
8595 the immediate is valid, write a constant suitable for using as an operand
8596 to VORR/VBIC/VAND/VORN to *MODCONST and the corresponding element width to
8597 *ELEMENTWIDTH. See neon_valid_immediate for description of INVERSE. */
8598
8599int
8600neon_immediate_valid_for_logic (rtx op, enum machine_mode mode, int inverse,
8601 rtx *modconst, int *elementwidth)
8602{
8603 rtx tmpconst;
8604 int tmpwidth;
8605 int retval = neon_valid_immediate (op, mode, inverse, &tmpconst, &tmpwidth);
8606
8607 if (retval < 0 || retval > 5)
8608 return 0;
8609
8610 if (modconst)
8611 *modconst = tmpconst;
8612
8613 if (elementwidth)
8614 *elementwidth = tmpwidth;
8615
8616 return 1;
8617}
8618
8619/* Return a string suitable for output of Neon immediate logic operation
8620 MNEM. */
8621
8622char *
8623neon_output_logic_immediate (const char *mnem, rtx *op2, enum machine_mode mode,
8624 int inverse, int quad)
8625{
8626 int width, is_valid;
8627 static char templ[40];
8628
8629 is_valid = neon_immediate_valid_for_logic (*op2, mode, inverse, op2, &width);
8630
8631 gcc_assert (is_valid != 0);
8632
8633 if (quad)
8634 sprintf (templ, "%s.i%d\t%%q0, %%2", mnem, width);
8635 else
8636 sprintf (templ, "%s.i%d\t%%P0, %%2", mnem, width);
8637
8638 return templ;
8639}
8640
8641/* Output a sequence of pairwise operations to implement a reduction.
8642 NOTE: We do "too much work" here, because pairwise operations work on two
8643 registers-worth of operands in one go. Unfortunately we can't exploit those
8644 extra calculations to do the full operation in fewer steps, I don't think.
8645 Although all vector elements of the result but the first are ignored, we
8646 actually calculate the same result in each of the elements. An alternative
8647 such as initially loading a vector with zero to use as each of the second
8648 operands would use up an additional register and take an extra instruction,
8649 for no particular gain. */
8650
8651void
8652neon_pairwise_reduce (rtx op0, rtx op1, enum machine_mode mode,
8653 rtx (*reduc) (rtx, rtx, rtx))
8654{
8655 enum machine_mode inner = GET_MODE_INNER (mode);
8656 unsigned int i, parts = GET_MODE_SIZE (mode) / GET_MODE_SIZE (inner);
8657 rtx tmpsum = op1;
8658
8659 for (i = parts / 2; i >= 1; i /= 2)
8660 {
8661 rtx dest = (i == 1) ? op0 : gen_reg_rtx (mode);
8662 emit_insn (reduc (dest, tmpsum, tmpsum));
8663 tmpsum = dest;
8664 }
8665}
8666
814a4c3b
DJ
8667/* If VALS is a vector constant that can be loaded into a register
8668 using VDUP, generate instructions to do so and return an RTX to
8669 assign to the register. Otherwise return NULL_RTX. */
8670
8671static rtx
8672neon_vdup_constant (rtx vals)
8673{
8674 enum machine_mode mode = GET_MODE (vals);
8675 enum machine_mode inner_mode = GET_MODE_INNER (mode);
8676 int n_elts = GET_MODE_NUNITS (mode);
8677 bool all_same = true;
8678 rtx x;
8679 int i;
8680
8681 if (GET_CODE (vals) != CONST_VECTOR || GET_MODE_SIZE (inner_mode) > 4)
8682 return NULL_RTX;
8683
8684 for (i = 0; i < n_elts; ++i)
8685 {
8686 x = XVECEXP (vals, 0, i);
8687 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
8688 all_same = false;
8689 }
8690
8691 if (!all_same)
8692 /* The elements are not all the same. We could handle repeating
8693 patterns of a mode larger than INNER_MODE here (e.g. int8x8_t
8694 {0, C, 0, C, 0, C, 0, C} which can be loaded using
8695 vdup.i16). */
8696 return NULL_RTX;
8697
8698 /* We can load this constant by using VDUP and a constant in a
8699 single ARM register. This will be cheaper than a vector
8700 load. */
8701
8702 x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, 0));
a277dd9b 8703 return gen_rtx_VEC_DUPLICATE (mode, x);
814a4c3b
DJ
8704}
8705
8706/* Generate code to load VALS, which is a PARALLEL containing only
8707 constants (for vec_init) or CONST_VECTOR, efficiently into a
8708 register. Returns an RTX to copy into the register, or NULL_RTX
8709 for a PARALLEL that can not be converted into a CONST_VECTOR. */
8710
8711rtx
8712neon_make_constant (rtx vals)
8713{
8714 enum machine_mode mode = GET_MODE (vals);
8715 rtx target;
8716 rtx const_vec = NULL_RTX;
8717 int n_elts = GET_MODE_NUNITS (mode);
8718 int n_const = 0;
8719 int i;
8720
8721 if (GET_CODE (vals) == CONST_VECTOR)
8722 const_vec = vals;
8723 else if (GET_CODE (vals) == PARALLEL)
8724 {
8725 /* A CONST_VECTOR must contain only CONST_INTs and
8726 CONST_DOUBLEs, but CONSTANT_P allows more (e.g. SYMBOL_REF).
8727 Only store valid constants in a CONST_VECTOR. */
8728 for (i = 0; i < n_elts; ++i)
8729 {
8730 rtx x = XVECEXP (vals, 0, i);
8731 if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
8732 n_const++;
8733 }
8734 if (n_const == n_elts)
8735 const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
8736 }
8737 else
8738 gcc_unreachable ();
8739
8740 if (const_vec != NULL
8741 && neon_immediate_valid_for_move (const_vec, mode, NULL, NULL))
8742 /* Load using VMOV. On Cortex-A8 this takes one cycle. */
8743 return const_vec;
8744 else if ((target = neon_vdup_constant (vals)) != NULL_RTX)
8745 /* Loaded using VDUP. On Cortex-A8 the VDUP takes one NEON
8746 pipeline cycle; creating the constant takes one or two ARM
8747 pipeline cycles. */
8748 return target;
8749 else if (const_vec != NULL_RTX)
8750 /* Load from constant pool. On Cortex-A8 this takes two cycles
8751 (for either double or quad vectors). We can not take advantage
8752 of single-cycle VLD1 because we need a PC-relative addressing
8753 mode. */
8754 return const_vec;
8755 else
8756 /* A PARALLEL containing something not valid inside CONST_VECTOR.
8757 We can not construct an initializer. */
8758 return NULL_RTX;
8759}
8760
8761/* Initialize vector TARGET to VALS. */
88f77cba
JB
8762
8763void
8764neon_expand_vector_init (rtx target, rtx vals)
8765{
8766 enum machine_mode mode = GET_MODE (target);
814a4c3b
DJ
8767 enum machine_mode inner_mode = GET_MODE_INNER (mode);
8768 int n_elts = GET_MODE_NUNITS (mode);
8769 int n_var = 0, one_var = -1;
8770 bool all_same = true;
8771 rtx x, mem;
8772 int i;
88f77cba 8773
814a4c3b
DJ
8774 for (i = 0; i < n_elts; ++i)
8775 {
8776 x = XVECEXP (vals, 0, i);
8777 if (!CONSTANT_P (x))
8778 ++n_var, one_var = i;
8779
8780 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
8781 all_same = false;
8782 }
88f77cba 8783
814a4c3b
DJ
8784 if (n_var == 0)
8785 {
8786 rtx constant = neon_make_constant (vals);
8787 if (constant != NULL_RTX)
8788 {
8789 emit_move_insn (target, constant);
8790 return;
8791 }
8792 }
8793
8794 /* Splat a single non-constant element if we can. */
8795 if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
8796 {
8797 x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, 0));
8798 emit_insn (gen_rtx_SET (VOIDmode, target,
a277dd9b 8799 gen_rtx_VEC_DUPLICATE (mode, x)));
814a4c3b
DJ
8800 return;
8801 }
8802
8803 /* One field is non-constant. Load constant then overwrite varying
8804 field. This is more efficient than using the stack. */
8805 if (n_var == 1)
8806 {
8807 rtx copy = copy_rtx (vals);
a277dd9b 8808 rtx index = GEN_INT (one_var);
814a4c3b
DJ
8809
8810 /* Load constant part of vector, substitute neighboring value for
8811 varying element. */
8812 XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
8813 neon_expand_vector_init (target, copy);
8814
8815 /* Insert variable. */
8816 x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, one_var));
a277dd9b
SL
8817 switch (mode)
8818 {
8819 case V8QImode:
8820 emit_insn (gen_neon_vset_lanev8qi (target, x, target, index));
8821 break;
8822 case V16QImode:
8823 emit_insn (gen_neon_vset_lanev16qi (target, x, target, index));
8824 break;
8825 case V4HImode:
8826 emit_insn (gen_neon_vset_lanev4hi (target, x, target, index));
8827 break;
8828 case V8HImode:
8829 emit_insn (gen_neon_vset_lanev8hi (target, x, target, index));
8830 break;
8831 case V2SImode:
8832 emit_insn (gen_neon_vset_lanev2si (target, x, target, index));
8833 break;
8834 case V4SImode:
8835 emit_insn (gen_neon_vset_lanev4si (target, x, target, index));
8836 break;
8837 case V2SFmode:
8838 emit_insn (gen_neon_vset_lanev2sf (target, x, target, index));
8839 break;
8840 case V4SFmode:
8841 emit_insn (gen_neon_vset_lanev4sf (target, x, target, index));
8842 break;
8843 case V2DImode:
8844 emit_insn (gen_neon_vset_lanev2di (target, x, target, index));
8845 break;
8846 default:
8847 gcc_unreachable ();
8848 }
814a4c3b
DJ
8849 return;
8850 }
8851
8852 /* Construct the vector in memory one field at a time
8853 and load the whole vector. */
88f77cba
JB
8854 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), 0);
8855 for (i = 0; i < n_elts; i++)
814a4c3b
DJ
8856 emit_move_insn (adjust_address_nv (mem, inner_mode,
8857 i * GET_MODE_SIZE (inner_mode)),
8858 XVECEXP (vals, 0, i));
88f77cba
JB
8859 emit_move_insn (target, mem);
8860}
8861
b617fc71
JB
8862/* Ensure OPERAND lies between LOW (inclusive) and HIGH (exclusive). Raise
8863 ERR if it doesn't. FIXME: NEON bounds checks occur late in compilation, so
8864 reported source locations are bogus. */
8865
8866static void
8867bounds_check (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high,
8868 const char *err)
8869{
8870 HOST_WIDE_INT lane;
8871
8872 gcc_assert (GET_CODE (operand) == CONST_INT);
8873
8874 lane = INTVAL (operand);
8875
8876 if (lane < low || lane >= high)
8877 error (err);
8878}
8879
8880/* Bounds-check lanes. */
8881
8882void
8883neon_lane_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
8884{
8885 bounds_check (operand, low, high, "lane out of range");
8886}
8887
8888/* Bounds-check constants. */
8889
8890void
8891neon_const_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
8892{
8893 bounds_check (operand, low, high, "constant out of range");
8894}
8895
8896HOST_WIDE_INT
8897neon_element_bits (enum machine_mode mode)
8898{
8899 if (mode == DImode)
8900 return GET_MODE_BITSIZE (mode);
8901 else
8902 return GET_MODE_BITSIZE (GET_MODE_INNER (mode));
8903}
8904
cce8749e
CH
8905\f
8906/* Predicates for `match_operand' and `match_operator'. */
8907
9b6b54e2 8908/* Return nonzero if OP is a valid Cirrus memory address pattern. */
9b6b54e2 8909int
e32bac5b 8910cirrus_memory_offset (rtx op)
9b6b54e2
NC
8911{
8912 /* Reject eliminable registers. */
8913 if (! (reload_in_progress || reload_completed)
8914 && ( reg_mentioned_p (frame_pointer_rtx, op)
8915 || reg_mentioned_p (arg_pointer_rtx, op)
8916 || reg_mentioned_p (virtual_incoming_args_rtx, op)
8917 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8918 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8919 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8920 return 0;
8921
8922 if (GET_CODE (op) == MEM)
8923 {
8924 rtx ind;
8925
8926 ind = XEXP (op, 0);
8927
8928 /* Match: (mem (reg)). */
8929 if (GET_CODE (ind) == REG)
8930 return 1;
8931
8932 /* Match:
8933 (mem (plus (reg)
8934 (const))). */
8935 if (GET_CODE (ind) == PLUS
8936 && GET_CODE (XEXP (ind, 0)) == REG
8937 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
8938 && GET_CODE (XEXP (ind, 1)) == CONST_INT)
8939 return 1;
8940 }
8941
8942 return 0;
8943}
8944
f26b8ec9 8945/* Return TRUE if OP is a valid coprocessor memory address pattern.
5b3e6663
PB
8946 WB is true if full writeback address modes are allowed and is false
8947 if limited writeback address modes (POST_INC and PRE_DEC) are
8948 allowed. */
9b66ebb1
PB
8949
8950int
fdd695fd 8951arm_coproc_mem_operand (rtx op, bool wb)
9b66ebb1 8952{
fdd695fd 8953 rtx ind;
9b66ebb1 8954
fdd695fd 8955 /* Reject eliminable registers. */
9b66ebb1
PB
8956 if (! (reload_in_progress || reload_completed)
8957 && ( reg_mentioned_p (frame_pointer_rtx, op)
8958 || reg_mentioned_p (arg_pointer_rtx, op)
8959 || reg_mentioned_p (virtual_incoming_args_rtx, op)
8960 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8961 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8962 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8963 return FALSE;
8964
59b9a953 8965 /* Constants are converted into offsets from labels. */
fdd695fd
PB
8966 if (GET_CODE (op) != MEM)
8967 return FALSE;
9b66ebb1 8968
fdd695fd 8969 ind = XEXP (op, 0);
9b66ebb1 8970
fdd695fd
PB
8971 if (reload_completed
8972 && (GET_CODE (ind) == LABEL_REF
8973 || (GET_CODE (ind) == CONST
8974 && GET_CODE (XEXP (ind, 0)) == PLUS
8975 && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
8976 && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
8977 return TRUE;
9b66ebb1 8978
fdd695fd
PB
8979 /* Match: (mem (reg)). */
8980 if (GET_CODE (ind) == REG)
8981 return arm_address_register_rtx_p (ind, 0);
8982
5b3e6663
PB
8983 /* Autoincremment addressing modes. POST_INC and PRE_DEC are
8984 acceptable in any case (subject to verification by
8985 arm_address_register_rtx_p). We need WB to be true to accept
8986 PRE_INC and POST_DEC. */
8987 if (GET_CODE (ind) == POST_INC
8988 || GET_CODE (ind) == PRE_DEC
8989 || (wb
8990 && (GET_CODE (ind) == PRE_INC
8991 || GET_CODE (ind) == POST_DEC)))
fdd695fd
PB
8992 return arm_address_register_rtx_p (XEXP (ind, 0), 0);
8993
8994 if (wb
8995 && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
8996 && arm_address_register_rtx_p (XEXP (ind, 0), 0)
8997 && GET_CODE (XEXP (ind, 1)) == PLUS
8998 && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
8999 ind = XEXP (ind, 1);
9000
9001 /* Match:
9002 (plus (reg)
9003 (const)). */
9004 if (GET_CODE (ind) == PLUS
9005 && GET_CODE (XEXP (ind, 0)) == REG
9006 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
9007 && GET_CODE (XEXP (ind, 1)) == CONST_INT
9008 && INTVAL (XEXP (ind, 1)) > -1024
9009 && INTVAL (XEXP (ind, 1)) < 1024
9010 && (INTVAL (XEXP (ind, 1)) & 3) == 0)
9011 return TRUE;
9b66ebb1
PB
9012
9013 return FALSE;
9014}
9015
88f77cba 9016/* Return TRUE if OP is a memory operand which we can load or store a vector
dc34db56
PB
9017 to/from. TYPE is one of the following values:
9018 0 - Vector load/stor (vldr)
9019 1 - Core registers (ldm)
9020 2 - Element/structure loads (vld1)
9021 */
88f77cba 9022int
dc34db56 9023neon_vector_mem_operand (rtx op, int type)
88f77cba
JB
9024{
9025 rtx ind;
9026
9027 /* Reject eliminable registers. */
9028 if (! (reload_in_progress || reload_completed)
9029 && ( reg_mentioned_p (frame_pointer_rtx, op)
9030 || reg_mentioned_p (arg_pointer_rtx, op)
9031 || reg_mentioned_p (virtual_incoming_args_rtx, op)
9032 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
9033 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
9034 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
9035 return FALSE;
9036
9037 /* Constants are converted into offsets from labels. */
9038 if (GET_CODE (op) != MEM)
9039 return FALSE;
9040
9041 ind = XEXP (op, 0);
9042
9043 if (reload_completed
9044 && (GET_CODE (ind) == LABEL_REF
9045 || (GET_CODE (ind) == CONST
9046 && GET_CODE (XEXP (ind, 0)) == PLUS
9047 && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
9048 && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
9049 return TRUE;
9050
9051 /* Match: (mem (reg)). */
9052 if (GET_CODE (ind) == REG)
9053 return arm_address_register_rtx_p (ind, 0);
9054
9055 /* Allow post-increment with Neon registers. */
c452684d
JB
9056 if ((type != 1 && GET_CODE (ind) == POST_INC)
9057 || (type == 0 && GET_CODE (ind) == PRE_DEC))
88f77cba
JB
9058 return arm_address_register_rtx_p (XEXP (ind, 0), 0);
9059
dc34db56 9060 /* FIXME: vld1 allows register post-modify. */
88f77cba
JB
9061
9062 /* Match:
9063 (plus (reg)
9064 (const)). */
dc34db56 9065 if (type == 0
88f77cba
JB
9066 && GET_CODE (ind) == PLUS
9067 && GET_CODE (XEXP (ind, 0)) == REG
9068 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
9069 && GET_CODE (XEXP (ind, 1)) == CONST_INT
9070 && INTVAL (XEXP (ind, 1)) > -1024
9071 && INTVAL (XEXP (ind, 1)) < 1016
9072 && (INTVAL (XEXP (ind, 1)) & 3) == 0)
9073 return TRUE;
9074
9075 return FALSE;
9076}
9077
9078/* Return TRUE if OP is a mem suitable for loading/storing a Neon struct
9079 type. */
9080int
9081neon_struct_mem_operand (rtx op)
9082{
9083 rtx ind;
9084
9085 /* Reject eliminable registers. */
9086 if (! (reload_in_progress || reload_completed)
9087 && ( reg_mentioned_p (frame_pointer_rtx, op)
9088 || reg_mentioned_p (arg_pointer_rtx, op)
9089 || reg_mentioned_p (virtual_incoming_args_rtx, op)
9090 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
9091 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
9092 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
9093 return FALSE;
9094
9095 /* Constants are converted into offsets from labels. */
9096 if (GET_CODE (op) != MEM)
9097 return FALSE;
9098
9099 ind = XEXP (op, 0);
9100
9101 if (reload_completed
9102 && (GET_CODE (ind) == LABEL_REF
9103 || (GET_CODE (ind) == CONST
9104 && GET_CODE (XEXP (ind, 0)) == PLUS
9105 && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
9106 && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
9107 return TRUE;
9108
9109 /* Match: (mem (reg)). */
9110 if (GET_CODE (ind) == REG)
9111 return arm_address_register_rtx_p (ind, 0);
9112
e54170f4
CLT
9113 /* vldm/vstm allows POST_INC (ia) and PRE_DEC (db). */
9114 if (GET_CODE (ind) == POST_INC
9115 || GET_CODE (ind) == PRE_DEC)
9116 return arm_address_register_rtx_p (XEXP (ind, 0), 0);
9117
88f77cba
JB
9118 return FALSE;
9119}
9120
6555b6bd
RE
9121/* Return true if X is a register that will be eliminated later on. */
9122int
9123arm_eliminable_register (rtx x)
9124{
9125 return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
9126 || REGNO (x) == ARG_POINTER_REGNUM
9127 || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
9128 && REGNO (x) <= LAST_VIRTUAL_REGISTER));
9129}
9b66ebb1 9130
9b66ebb1 9131/* Return GENERAL_REGS if a scratch register required to reload x to/from
fe2d934b 9132 coprocessor registers. Otherwise return NO_REGS. */
9b66ebb1
PB
9133
9134enum reg_class
fe2d934b 9135coproc_secondary_reload_class (enum machine_mode mode, rtx x, bool wb)
9b66ebb1 9136{
0fd8c3ad
SL
9137 if (mode == HFmode)
9138 {
e0dc3601
PB
9139 if (!TARGET_NEON_FP16)
9140 return GENERAL_REGS;
0fd8c3ad
SL
9141 if (s_register_operand (x, mode) || neon_vector_mem_operand (x, 2))
9142 return NO_REGS;
9143 return GENERAL_REGS;
9144 }
9145
159b81b0
RS
9146 /* The neon move patterns handle all legitimate vector and struct
9147 addresses. */
88f77cba 9148 if (TARGET_NEON
159b81b0 9149 && MEM_P (x)
88f77cba 9150 && (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
159b81b0
RS
9151 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9152 || VALID_NEON_STRUCT_MODE (mode)))
9153 return NO_REGS;
88f77cba 9154
fe2d934b 9155 if (arm_coproc_mem_operand (x, wb) || s_register_operand (x, mode))
9b66ebb1
PB
9156 return NO_REGS;
9157
9158 return GENERAL_REGS;
9159}
9160
866af8a9
JB
9161/* Values which must be returned in the most-significant end of the return
9162 register. */
9163
9164static bool
586de218 9165arm_return_in_msb (const_tree valtype)
866af8a9
JB
9166{
9167 return (TARGET_AAPCS_BASED
9168 && BYTES_BIG_ENDIAN
9169 && (AGGREGATE_TYPE_P (valtype)
9170 || TREE_CODE (valtype) == COMPLEX_TYPE));
9171}
9b66ebb1 9172
f0375c66
NC
9173/* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
9174 Use by the Cirrus Maverick code which has to workaround
9175 a hardware bug triggered by such instructions. */
f0375c66 9176static bool
e32bac5b 9177arm_memory_load_p (rtx insn)
9b6b54e2
NC
9178{
9179 rtx body, lhs, rhs;;
9180
f0375c66
NC
9181 if (insn == NULL_RTX || GET_CODE (insn) != INSN)
9182 return false;
9b6b54e2
NC
9183
9184 body = PATTERN (insn);
9185
9186 if (GET_CODE (body) != SET)
f0375c66 9187 return false;
9b6b54e2
NC
9188
9189 lhs = XEXP (body, 0);
9190 rhs = XEXP (body, 1);
9191
f0375c66
NC
9192 lhs = REG_OR_SUBREG_RTX (lhs);
9193
9194 /* If the destination is not a general purpose
9195 register we do not have to worry. */
9196 if (GET_CODE (lhs) != REG
9197 || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
9198 return false;
9199
9200 /* As well as loads from memory we also have to react
9201 to loads of invalid constants which will be turned
9202 into loads from the minipool. */
9203 return (GET_CODE (rhs) == MEM
9204 || GET_CODE (rhs) == SYMBOL_REF
9205 || note_invalid_constants (insn, -1, false));
9b6b54e2
NC
9206}
9207
f0375c66 9208/* Return TRUE if INSN is a Cirrus instruction. */
f0375c66 9209static bool
e32bac5b 9210arm_cirrus_insn_p (rtx insn)
9b6b54e2
NC
9211{
9212 enum attr_cirrus attr;
9213
e6d29d15 9214 /* get_attr cannot accept USE or CLOBBER. */
9b6b54e2
NC
9215 if (!insn
9216 || GET_CODE (insn) != INSN
9217 || GET_CODE (PATTERN (insn)) == USE
9218 || GET_CODE (PATTERN (insn)) == CLOBBER)
9219 return 0;
9220
9221 attr = get_attr_cirrus (insn);
9222
f0375c66 9223 return attr != CIRRUS_NOT;
9b6b54e2
NC
9224}
9225
9226/* Cirrus reorg for invalid instruction combinations. */
9b6b54e2 9227static void
e32bac5b 9228cirrus_reorg (rtx first)
9b6b54e2
NC
9229{
9230 enum attr_cirrus attr;
9231 rtx body = PATTERN (first);
9232 rtx t;
9233 int nops;
9234
9235 /* Any branch must be followed by 2 non Cirrus instructions. */
9236 if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
9237 {
9238 nops = 0;
9239 t = next_nonnote_insn (first);
9240
f0375c66 9241 if (arm_cirrus_insn_p (t))
9b6b54e2
NC
9242 ++ nops;
9243
f0375c66 9244 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
9b6b54e2
NC
9245 ++ nops;
9246
9247 while (nops --)
9248 emit_insn_after (gen_nop (), first);
9249
9250 return;
9251 }
9252
9253 /* (float (blah)) is in parallel with a clobber. */
9254 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
9255 body = XVECEXP (body, 0, 0);
9256
9257 if (GET_CODE (body) == SET)
9258 {
9259 rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
9260
9261 /* cfldrd, cfldr64, cfstrd, cfstr64 must
9262 be followed by a non Cirrus insn. */
9263 if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
9264 {
f0375c66 9265 if (arm_cirrus_insn_p (next_nonnote_insn (first)))
9b6b54e2
NC
9266 emit_insn_after (gen_nop (), first);
9267
9268 return;
9269 }
f0375c66 9270 else if (arm_memory_load_p (first))
9b6b54e2
NC
9271 {
9272 unsigned int arm_regno;
9273
9274 /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
9275 ldr/cfmv64hr combination where the Rd field is the same
9276 in both instructions must be split with a non Cirrus
9277 insn. Example:
9278
9279 ldr r0, blah
9280 nop
9281 cfmvsr mvf0, r0. */
9282
9283 /* Get Arm register number for ldr insn. */
9284 if (GET_CODE (lhs) == REG)
9285 arm_regno = REGNO (lhs);
9b6b54e2 9286 else
e6d29d15
NS
9287 {
9288 gcc_assert (GET_CODE (rhs) == REG);
9289 arm_regno = REGNO (rhs);
9290 }
9b6b54e2
NC
9291
9292 /* Next insn. */
9293 first = next_nonnote_insn (first);
9294
f0375c66 9295 if (! arm_cirrus_insn_p (first))
9b6b54e2
NC
9296 return;
9297
9298 body = PATTERN (first);
9299
9300 /* (float (blah)) is in parallel with a clobber. */
9301 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
9302 body = XVECEXP (body, 0, 0);
9303
9304 if (GET_CODE (body) == FLOAT)
9305 body = XEXP (body, 0);
9306
9307 if (get_attr_cirrus (first) == CIRRUS_MOVE
9308 && GET_CODE (XEXP (body, 1)) == REG
9309 && arm_regno == REGNO (XEXP (body, 1)))
9310 emit_insn_after (gen_nop (), first);
9311
9312 return;
9313 }
9314 }
9315
e6d29d15 9316 /* get_attr cannot accept USE or CLOBBER. */
9b6b54e2
NC
9317 if (!first
9318 || GET_CODE (first) != INSN
9319 || GET_CODE (PATTERN (first)) == USE
9320 || GET_CODE (PATTERN (first)) == CLOBBER)
9321 return;
9322
9323 attr = get_attr_cirrus (first);
9324
9325 /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
9326 must be followed by a non-coprocessor instruction. */
9327 if (attr == CIRRUS_COMPARE)
9328 {
9329 nops = 0;
9330
9331 t = next_nonnote_insn (first);
9332
f0375c66 9333 if (arm_cirrus_insn_p (t))
9b6b54e2
NC
9334 ++ nops;
9335
f0375c66 9336 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
9b6b54e2
NC
9337 ++ nops;
9338
9339 while (nops --)
9340 emit_insn_after (gen_nop (), first);
9341
9342 return;
9343 }
9344}
9345
2b835d68
RE
9346/* Return TRUE if X references a SYMBOL_REF. */
9347int
e32bac5b 9348symbol_mentioned_p (rtx x)
2b835d68 9349{
1d6e90ac
NC
9350 const char * fmt;
9351 int i;
2b835d68
RE
9352
9353 if (GET_CODE (x) == SYMBOL_REF)
9354 return 1;
9355
d3585b76
DJ
9356 /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
9357 are constant offsets, not symbols. */
9358 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
9359 return 0;
9360
2b835d68 9361 fmt = GET_RTX_FORMAT (GET_CODE (x));
f676971a 9362
2b835d68
RE
9363 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
9364 {
9365 if (fmt[i] == 'E')
9366 {
1d6e90ac 9367 int j;
2b835d68
RE
9368
9369 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9370 if (symbol_mentioned_p (XVECEXP (x, i, j)))
9371 return 1;
9372 }
9373 else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
9374 return 1;
9375 }
9376
9377 return 0;
9378}
9379
9380/* Return TRUE if X references a LABEL_REF. */
9381int
e32bac5b 9382label_mentioned_p (rtx x)
2b835d68 9383{
1d6e90ac
NC
9384 const char * fmt;
9385 int i;
2b835d68
RE
9386
9387 if (GET_CODE (x) == LABEL_REF)
9388 return 1;
9389
d3585b76
DJ
9390 /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
9391 instruction, but they are constant offsets, not symbols. */
9392 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
9393 return 0;
9394
2b835d68
RE
9395 fmt = GET_RTX_FORMAT (GET_CODE (x));
9396 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
9397 {
9398 if (fmt[i] == 'E')
9399 {
1d6e90ac 9400 int j;
2b835d68
RE
9401
9402 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9403 if (label_mentioned_p (XVECEXP (x, i, j)))
9404 return 1;
9405 }
9406 else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
9407 return 1;
9408 }
9409
9410 return 0;
9411}
9412
d3585b76
DJ
9413int
9414tls_mentioned_p (rtx x)
9415{
9416 switch (GET_CODE (x))
9417 {
9418 case CONST:
9419 return tls_mentioned_p (XEXP (x, 0));
9420
9421 case UNSPEC:
9422 if (XINT (x, 1) == UNSPEC_TLS)
9423 return 1;
9424
9425 default:
9426 return 0;
9427 }
9428}
9429
2e5505a4
RE
9430/* Must not copy any rtx that uses a pc-relative address. */
9431
9432static int
9433arm_note_pic_base (rtx *x, void *date ATTRIBUTE_UNUSED)
9434{
9435 if (GET_CODE (*x) == UNSPEC
9436 && XINT (*x, 1) == UNSPEC_PIC_BASE)
9437 return 1;
9438 return 0;
9439}
d3585b76
DJ
9440
9441static bool
9442arm_cannot_copy_insn_p (rtx insn)
9443{
2e5505a4 9444 return for_each_rtx (&PATTERN (insn), arm_note_pic_base, NULL);
d3585b76
DJ
9445}
9446
ff9940b0 9447enum rtx_code
e32bac5b 9448minmax_code (rtx x)
ff9940b0
RE
9449{
9450 enum rtx_code code = GET_CODE (x);
9451
e6d29d15
NS
9452 switch (code)
9453 {
9454 case SMAX:
9455 return GE;
9456 case SMIN:
9457 return LE;
9458 case UMIN:
9459 return LEU;
9460 case UMAX:
9461 return GEU;
9462 default:
9463 gcc_unreachable ();
9464 }
ff9940b0
RE
9465}
9466
6354dc9b 9467/* Return 1 if memory locations are adjacent. */
f3bb6135 9468int
e32bac5b 9469adjacent_mem_locations (rtx a, rtx b)
ff9940b0 9470{
15b5c4c1
RE
9471 /* We don't guarantee to preserve the order of these memory refs. */
9472 if (volatile_refs_p (a) || volatile_refs_p (b))
9473 return 0;
9474
ff9940b0
RE
9475 if ((GET_CODE (XEXP (a, 0)) == REG
9476 || (GET_CODE (XEXP (a, 0)) == PLUS
9477 && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
9478 && (GET_CODE (XEXP (b, 0)) == REG
9479 || (GET_CODE (XEXP (b, 0)) == PLUS
9480 && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
9481 {
6555b6bd
RE
9482 HOST_WIDE_INT val0 = 0, val1 = 0;
9483 rtx reg0, reg1;
9484 int val_diff;
f676971a 9485
ff9940b0
RE
9486 if (GET_CODE (XEXP (a, 0)) == PLUS)
9487 {
6555b6bd 9488 reg0 = XEXP (XEXP (a, 0), 0);
ff9940b0
RE
9489 val0 = INTVAL (XEXP (XEXP (a, 0), 1));
9490 }
9491 else
6555b6bd 9492 reg0 = XEXP (a, 0);
1d6e90ac 9493
ff9940b0
RE
9494 if (GET_CODE (XEXP (b, 0)) == PLUS)
9495 {
6555b6bd 9496 reg1 = XEXP (XEXP (b, 0), 0);
ff9940b0
RE
9497 val1 = INTVAL (XEXP (XEXP (b, 0), 1));
9498 }
9499 else
6555b6bd 9500 reg1 = XEXP (b, 0);
1d6e90ac 9501
e32bac5b
RE
9502 /* Don't accept any offset that will require multiple
9503 instructions to handle, since this would cause the
9504 arith_adjacentmem pattern to output an overlong sequence. */
bbbbb16a 9505 if (!const_ok_for_op (val0, PLUS) || !const_ok_for_op (val1, PLUS))
c75a3ddc 9506 return 0;
f676971a 9507
6555b6bd
RE
9508 /* Don't allow an eliminable register: register elimination can make
9509 the offset too large. */
9510 if (arm_eliminable_register (reg0))
9511 return 0;
9512
9513 val_diff = val1 - val0;
15b5c4c1
RE
9514
9515 if (arm_ld_sched)
9516 {
9517 /* If the target has load delay slots, then there's no benefit
9518 to using an ldm instruction unless the offset is zero and
9519 we are optimizing for size. */
9520 return (optimize_size && (REGNO (reg0) == REGNO (reg1))
9521 && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
9522 && (val_diff == 4 || val_diff == -4));
9523 }
9524
6555b6bd
RE
9525 return ((REGNO (reg0) == REGNO (reg1))
9526 && (val_diff == 4 || val_diff == -4));
ff9940b0 9527 }
6555b6bd 9528
ff9940b0
RE
9529 return 0;
9530}
9531
93b338c3
BS
9532/* Return true iff it would be profitable to turn a sequence of NOPS loads
9533 or stores (depending on IS_STORE) into a load-multiple or store-multiple
9534 instruction. ADD_OFFSET is nonzero if the base address register needs
9535 to be modified with an add instruction before we can use it. */
9536
9537static bool
9538multiple_operation_profitable_p (bool is_store ATTRIBUTE_UNUSED,
9539 int nops, HOST_WIDE_INT add_offset)
9540 {
9541 /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
9542 if the offset isn't small enough. The reason 2 ldrs are faster
9543 is because these ARMs are able to do more than one cache access
9544 in a single cycle. The ARM9 and StrongARM have Harvard caches,
9545 whilst the ARM8 has a double bandwidth cache. This means that
9546 these cores can do both an instruction fetch and a data fetch in
9547 a single cycle, so the trick of calculating the address into a
9548 scratch register (one of the result regs) and then doing a load
9549 multiple actually becomes slower (and no smaller in code size).
9550 That is the transformation
9551
9552 ldr rd1, [rbase + offset]
9553 ldr rd2, [rbase + offset + 4]
9554
9555 to
9556
9557 add rd1, rbase, offset
9558 ldmia rd1, {rd1, rd2}
9559
9560 produces worse code -- '3 cycles + any stalls on rd2' instead of
9561 '2 cycles + any stalls on rd2'. On ARMs with only one cache
9562 access per cycle, the first sequence could never complete in less
9563 than 6 cycles, whereas the ldm sequence would only take 5 and
9564 would make better use of sequential accesses if not hitting the
9565 cache.
9566
9567 We cheat here and test 'arm_ld_sched' which we currently know to
9568 only be true for the ARM8, ARM9 and StrongARM. If this ever
9569 changes, then the test below needs to be reworked. */
9570 if (nops == 2 && arm_ld_sched && add_offset != 0)
9571 return false;
9572
8f4c6e28
BS
9573 /* XScale has load-store double instructions, but they have stricter
9574 alignment requirements than load-store multiple, so we cannot
9575 use them.
9576
9577 For XScale ldm requires 2 + NREGS cycles to complete and blocks
9578 the pipeline until completion.
9579
9580 NREGS CYCLES
9581 1 3
9582 2 4
9583 3 5
9584 4 6
9585
9586 An ldr instruction takes 1-3 cycles, but does not block the
9587 pipeline.
9588
9589 NREGS CYCLES
9590 1 1-3
9591 2 2-6
9592 3 3-9
9593 4 4-12
9594
9595 Best case ldr will always win. However, the more ldr instructions
9596 we issue, the less likely we are to be able to schedule them well.
9597 Using ldr instructions also increases code size.
9598
9599 As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
9600 for counts of 3 or 4 regs. */
9601 if (nops <= 2 && arm_tune_xscale && !optimize_size)
9602 return false;
93b338c3
BS
9603 return true;
9604}
9605
9606/* Subroutine of load_multiple_sequence and store_multiple_sequence.
9607 Given an array of UNSORTED_OFFSETS, of which there are NOPS, compute
9608 an array ORDER which describes the sequence to use when accessing the
9609 offsets that produces an ascending order. In this sequence, each
9610 offset must be larger by exactly 4 than the previous one. ORDER[0]
9611 must have been filled in with the lowest offset by the caller.
9612 If UNSORTED_REGS is nonnull, it is an array of register numbers that
9613 we use to verify that ORDER produces an ascending order of registers.
9614 Return true if it was possible to construct such an order, false if
9615 not. */
9616
9617static bool
9618compute_offset_order (int nops, HOST_WIDE_INT *unsorted_offsets, int *order,
9619 int *unsorted_regs)
9620{
9621 int i;
9622 for (i = 1; i < nops; i++)
9623 {
9624 int j;
9625
9626 order[i] = order[i - 1];
9627 for (j = 0; j < nops; j++)
9628 if (unsorted_offsets[j] == unsorted_offsets[order[i - 1]] + 4)
9629 {
9630 /* We must find exactly one offset that is higher than the
9631 previous one by 4. */
9632 if (order[i] != order[i - 1])
9633 return false;
9634 order[i] = j;
9635 }
9636 if (order[i] == order[i - 1])
9637 return false;
9638 /* The register numbers must be ascending. */
9639 if (unsorted_regs != NULL
9640 && unsorted_regs[order[i]] <= unsorted_regs[order[i - 1]])
9641 return false;
9642 }
9643 return true;
9644}
9645
37119410
BS
9646/* Used to determine in a peephole whether a sequence of load
9647 instructions can be changed into a load-multiple instruction.
9648 NOPS is the number of separate load instructions we are examining. The
9649 first NOPS entries in OPERANDS are the destination registers, the
9650 next NOPS entries are memory operands. If this function is
9651 successful, *BASE is set to the common base register of the memory
9652 accesses; *LOAD_OFFSET is set to the first memory location's offset
9653 from that base register.
9654 REGS is an array filled in with the destination register numbers.
9655 SAVED_ORDER (if nonnull), is an array filled in with an order that maps
dd5a833e 9656 insn numbers to an ascending order of stores. If CHECK_REGS is true,
37119410
BS
9657 the sequence of registers in REGS matches the loads from ascending memory
9658 locations, and the function verifies that the register numbers are
9659 themselves ascending. If CHECK_REGS is false, the register numbers
9660 are stored in the order they are found in the operands. */
9661static int
9662load_multiple_sequence (rtx *operands, int nops, int *regs, int *saved_order,
9663 int *base, HOST_WIDE_INT *load_offset, bool check_regs)
84ed5e79 9664{
93b338c3
BS
9665 int unsorted_regs[MAX_LDM_STM_OPS];
9666 HOST_WIDE_INT unsorted_offsets[MAX_LDM_STM_OPS];
9667 int order[MAX_LDM_STM_OPS];
37119410 9668 rtx base_reg_rtx = NULL;
ad076f4e 9669 int base_reg = -1;
93b338c3 9670 int i, ldm_case;
84ed5e79 9671
93b338c3
BS
9672 /* Can only handle up to MAX_LDM_STM_OPS insns at present, though could be
9673 easily extended if required. */
9674 gcc_assert (nops >= 2 && nops <= MAX_LDM_STM_OPS);
84ed5e79 9675
93b338c3 9676 memset (order, 0, MAX_LDM_STM_OPS * sizeof (int));
f0b4bdd5 9677
84ed5e79 9678 /* Loop over the operands and check that the memory references are
112cdef5 9679 suitable (i.e. immediate offsets from the same base register). At
84ed5e79
RE
9680 the same time, extract the target register, and the memory
9681 offsets. */
9682 for (i = 0; i < nops; i++)
9683 {
9684 rtx reg;
9685 rtx offset;
9686
56636818
JL
9687 /* Convert a subreg of a mem into the mem itself. */
9688 if (GET_CODE (operands[nops + i]) == SUBREG)
4e26a7af 9689 operands[nops + i] = alter_subreg (operands + (nops + i));
56636818 9690
e6d29d15 9691 gcc_assert (GET_CODE (operands[nops + i]) == MEM);
84ed5e79
RE
9692
9693 /* Don't reorder volatile memory references; it doesn't seem worth
9694 looking for the case where the order is ok anyway. */
9695 if (MEM_VOLATILE_P (operands[nops + i]))
9696 return 0;
9697
9698 offset = const0_rtx;
9699
9700 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
9701 || (GET_CODE (reg) == SUBREG
9702 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9703 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
9704 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
9705 == REG)
9706 || (GET_CODE (reg) == SUBREG
9707 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9708 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
9709 == CONST_INT)))
9710 {
9711 if (i == 0)
84ed5e79 9712 {
37119410
BS
9713 base_reg = REGNO (reg);
9714 base_reg_rtx = reg;
9715 if (TARGET_THUMB1 && base_reg > LAST_LO_REGNUM)
84ed5e79 9716 return 0;
84ed5e79 9717 }
37119410
BS
9718 else if (base_reg != (int) REGNO (reg))
9719 /* Not addressed from the same base register. */
9720 return 0;
9721
93b338c3
BS
9722 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
9723 ? REGNO (operands[i])
9724 : REGNO (SUBREG_REG (operands[i])));
84ed5e79
RE
9725
9726 /* If it isn't an integer register, or if it overwrites the
9727 base register but isn't the last insn in the list, then
9728 we can't do this. */
37119410
BS
9729 if (unsorted_regs[i] < 0
9730 || (TARGET_THUMB1 && unsorted_regs[i] > LAST_LO_REGNUM)
9731 || unsorted_regs[i] > 14
84ed5e79
RE
9732 || (i != nops - 1 && unsorted_regs[i] == base_reg))
9733 return 0;
9734
9735 unsorted_offsets[i] = INTVAL (offset);
93b338c3
BS
9736 if (i == 0 || unsorted_offsets[i] < unsorted_offsets[order[0]])
9737 order[0] = i;
84ed5e79
RE
9738 }
9739 else
9740 /* Not a suitable memory address. */
9741 return 0;
9742 }
9743
9744 /* All the useful information has now been extracted from the
9745 operands into unsorted_regs and unsorted_offsets; additionally,
93b338c3
BS
9746 order[0] has been set to the lowest offset in the list. Sort
9747 the offsets into order, verifying that they are adjacent, and
9748 check that the register numbers are ascending. */
37119410
BS
9749 if (!compute_offset_order (nops, unsorted_offsets, order,
9750 check_regs ? unsorted_regs : NULL))
93b338c3 9751 return 0;
84ed5e79 9752
37119410
BS
9753 if (saved_order)
9754 memcpy (saved_order, order, sizeof order);
9755
84ed5e79
RE
9756 if (base)
9757 {
9758 *base = base_reg;
9759
9760 for (i = 0; i < nops; i++)
37119410 9761 regs[i] = unsorted_regs[check_regs ? order[i] : i];
84ed5e79
RE
9762
9763 *load_offset = unsorted_offsets[order[0]];
9764 }
9765
37119410
BS
9766 if (TARGET_THUMB1
9767 && !peep2_reg_dead_p (nops, base_reg_rtx))
9768 return 0;
9769
84ed5e79 9770 if (unsorted_offsets[order[0]] == 0)
93b338c3
BS
9771 ldm_case = 1; /* ldmia */
9772 else if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
9773 ldm_case = 2; /* ldmib */
9774 else if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
9775 ldm_case = 3; /* ldmda */
37119410 9776 else if (TARGET_32BIT && unsorted_offsets[order[nops - 1]] == -4)
93b338c3
BS
9777 ldm_case = 4; /* ldmdb */
9778 else if (const_ok_for_arm (unsorted_offsets[order[0]])
9779 || const_ok_for_arm (-unsorted_offsets[order[0]]))
9780 ldm_case = 5;
9781 else
9782 return 0;
949d79eb 9783
93b338c3
BS
9784 if (!multiple_operation_profitable_p (false, nops,
9785 ldm_case == 5
9786 ? unsorted_offsets[order[0]] : 0))
b36ba79f
RE
9787 return 0;
9788
93b338c3 9789 return ldm_case;
84ed5e79
RE
9790}
9791
37119410
BS
9792/* Used to determine in a peephole whether a sequence of store instructions can
9793 be changed into a store-multiple instruction.
9794 NOPS is the number of separate store instructions we are examining.
9795 NOPS_TOTAL is the total number of instructions recognized by the peephole
9796 pattern.
9797 The first NOPS entries in OPERANDS are the source registers, the next
9798 NOPS entries are memory operands. If this function is successful, *BASE is
9799 set to the common base register of the memory accesses; *LOAD_OFFSET is set
9800 to the first memory location's offset from that base register. REGS is an
9801 array filled in with the source register numbers, REG_RTXS (if nonnull) is
9802 likewise filled with the corresponding rtx's.
9803 SAVED_ORDER (if nonnull), is an array filled in with an order that maps insn
dd5a833e 9804 numbers to an ascending order of stores.
37119410
BS
9805 If CHECK_REGS is true, the sequence of registers in *REGS matches the stores
9806 from ascending memory locations, and the function verifies that the register
9807 numbers are themselves ascending. If CHECK_REGS is false, the register
9808 numbers are stored in the order they are found in the operands. */
9809static int
9810store_multiple_sequence (rtx *operands, int nops, int nops_total,
9811 int *regs, rtx *reg_rtxs, int *saved_order, int *base,
9812 HOST_WIDE_INT *load_offset, bool check_regs)
84ed5e79 9813{
93b338c3 9814 int unsorted_regs[MAX_LDM_STM_OPS];
37119410 9815 rtx unsorted_reg_rtxs[MAX_LDM_STM_OPS];
93b338c3
BS
9816 HOST_WIDE_INT unsorted_offsets[MAX_LDM_STM_OPS];
9817 int order[MAX_LDM_STM_OPS];
ad076f4e 9818 int base_reg = -1;
37119410 9819 rtx base_reg_rtx = NULL;
93b338c3 9820 int i, stm_case;
84ed5e79 9821
93b338c3
BS
9822 /* Can only handle up to MAX_LDM_STM_OPS insns at present, though could be
9823 easily extended if required. */
9824 gcc_assert (nops >= 2 && nops <= MAX_LDM_STM_OPS);
84ed5e79 9825
93b338c3 9826 memset (order, 0, MAX_LDM_STM_OPS * sizeof (int));
f0b4bdd5 9827
84ed5e79 9828 /* Loop over the operands and check that the memory references are
112cdef5 9829 suitable (i.e. immediate offsets from the same base register). At
84ed5e79
RE
9830 the same time, extract the target register, and the memory
9831 offsets. */
9832 for (i = 0; i < nops; i++)
9833 {
9834 rtx reg;
9835 rtx offset;
9836
56636818
JL
9837 /* Convert a subreg of a mem into the mem itself. */
9838 if (GET_CODE (operands[nops + i]) == SUBREG)
4e26a7af 9839 operands[nops + i] = alter_subreg (operands + (nops + i));
56636818 9840
e6d29d15 9841 gcc_assert (GET_CODE (operands[nops + i]) == MEM);
84ed5e79
RE
9842
9843 /* Don't reorder volatile memory references; it doesn't seem worth
9844 looking for the case where the order is ok anyway. */
9845 if (MEM_VOLATILE_P (operands[nops + i]))
9846 return 0;
9847
9848 offset = const0_rtx;
9849
9850 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
9851 || (GET_CODE (reg) == SUBREG
9852 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9853 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
9854 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
9855 == REG)
9856 || (GET_CODE (reg) == SUBREG
9857 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9858 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
9859 == CONST_INT)))
9860 {
37119410
BS
9861 unsorted_reg_rtxs[i] = (GET_CODE (operands[i]) == REG
9862 ? operands[i] : SUBREG_REG (operands[i]));
9863 unsorted_regs[i] = REGNO (unsorted_reg_rtxs[i]);
9864
84ed5e79 9865 if (i == 0)
37119410
BS
9866 {
9867 base_reg = REGNO (reg);
9868 base_reg_rtx = reg;
9869 if (TARGET_THUMB1 && base_reg > LAST_LO_REGNUM)
9870 return 0;
9871 }
93b338c3
BS
9872 else if (base_reg != (int) REGNO (reg))
9873 /* Not addressed from the same base register. */
9874 return 0;
84ed5e79
RE
9875
9876 /* If it isn't an integer register, then we can't do this. */
37119410
BS
9877 if (unsorted_regs[i] < 0
9878 || (TARGET_THUMB1 && unsorted_regs[i] > LAST_LO_REGNUM)
9879 || (TARGET_THUMB2 && unsorted_regs[i] == base_reg)
9880 || (TARGET_THUMB2 && unsorted_regs[i] == SP_REGNUM)
9881 || unsorted_regs[i] > 14)
84ed5e79
RE
9882 return 0;
9883
9884 unsorted_offsets[i] = INTVAL (offset);
93b338c3
BS
9885 if (i == 0 || unsorted_offsets[i] < unsorted_offsets[order[0]])
9886 order[0] = i;
84ed5e79
RE
9887 }
9888 else
9889 /* Not a suitable memory address. */
9890 return 0;
9891 }
9892
9893 /* All the useful information has now been extracted from the
9894 operands into unsorted_regs and unsorted_offsets; additionally,
93b338c3
BS
9895 order[0] has been set to the lowest offset in the list. Sort
9896 the offsets into order, verifying that they are adjacent, and
9897 check that the register numbers are ascending. */
37119410
BS
9898 if (!compute_offset_order (nops, unsorted_offsets, order,
9899 check_regs ? unsorted_regs : NULL))
93b338c3 9900 return 0;
84ed5e79 9901
37119410
BS
9902 if (saved_order)
9903 memcpy (saved_order, order, sizeof order);
9904
84ed5e79
RE
9905 if (base)
9906 {
9907 *base = base_reg;
9908
9909 for (i = 0; i < nops; i++)
37119410
BS
9910 {
9911 regs[i] = unsorted_regs[check_regs ? order[i] : i];
9912 if (reg_rtxs)
9913 reg_rtxs[i] = unsorted_reg_rtxs[check_regs ? order[i] : i];
9914 }
84ed5e79
RE
9915
9916 *load_offset = unsorted_offsets[order[0]];
9917 }
9918
37119410
BS
9919 if (TARGET_THUMB1
9920 && !peep2_reg_dead_p (nops_total, base_reg_rtx))
9921 return 0;
9922
84ed5e79 9923 if (unsorted_offsets[order[0]] == 0)
93b338c3
BS
9924 stm_case = 1; /* stmia */
9925 else if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
9926 stm_case = 2; /* stmib */
9927 else if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
9928 stm_case = 3; /* stmda */
37119410 9929 else if (TARGET_32BIT && unsorted_offsets[order[nops - 1]] == -4)
93b338c3
BS
9930 stm_case = 4; /* stmdb */
9931 else
9932 return 0;
84ed5e79 9933
93b338c3
BS
9934 if (!multiple_operation_profitable_p (false, nops, 0))
9935 return 0;
84ed5e79 9936
93b338c3 9937 return stm_case;
84ed5e79 9938}
ff9940b0 9939\f
6354dc9b 9940/* Routines for use in generating RTL. */
1d6e90ac 9941
37119410
BS
9942/* Generate a load-multiple instruction. COUNT is the number of loads in
9943 the instruction; REGS and MEMS are arrays containing the operands.
9944 BASEREG is the base register to be used in addressing the memory operands.
9945 WBACK_OFFSET is nonzero if the instruction should update the base
9946 register. */
9947
9948static rtx
9949arm_gen_load_multiple_1 (int count, int *regs, rtx *mems, rtx basereg,
9950 HOST_WIDE_INT wback_offset)
ff9940b0
RE
9951{
9952 int i = 0, j;
9953 rtx result;
ff9940b0 9954
8f4c6e28 9955 if (!multiple_operation_profitable_p (false, count, 0))
d19fb8e3
NC
9956 {
9957 rtx seq;
f676971a 9958
d19fb8e3 9959 start_sequence ();
f676971a 9960
d19fb8e3 9961 for (i = 0; i < count; i++)
37119410 9962 emit_move_insn (gen_rtx_REG (SImode, regs[i]), mems[i]);
d19fb8e3 9963
37119410
BS
9964 if (wback_offset != 0)
9965 emit_move_insn (basereg, plus_constant (basereg, wback_offset));
d19fb8e3 9966
2f937369 9967 seq = get_insns ();
d19fb8e3 9968 end_sequence ();
f676971a 9969
d19fb8e3
NC
9970 return seq;
9971 }
9972
43cffd11 9973 result = gen_rtx_PARALLEL (VOIDmode,
37119410
BS
9974 rtvec_alloc (count + (wback_offset != 0 ? 1 : 0)));
9975 if (wback_offset != 0)
f3bb6135 9976 {
ff9940b0 9977 XVECEXP (result, 0, 0)
37119410
BS
9978 = gen_rtx_SET (VOIDmode, basereg,
9979 plus_constant (basereg, wback_offset));
ff9940b0
RE
9980 i = 1;
9981 count++;
f3bb6135
RE
9982 }
9983
ff9940b0 9984 for (j = 0; i < count; i++, j++)
37119410
BS
9985 XVECEXP (result, 0, i)
9986 = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, regs[j]), mems[j]);
50ed9cea 9987
ff9940b0
RE
9988 return result;
9989}
9990
37119410
BS
9991/* Generate a store-multiple instruction. COUNT is the number of stores in
9992 the instruction; REGS and MEMS are arrays containing the operands.
9993 BASEREG is the base register to be used in addressing the memory operands.
9994 WBACK_OFFSET is nonzero if the instruction should update the base
9995 register. */
9996
9997static rtx
9998arm_gen_store_multiple_1 (int count, int *regs, rtx *mems, rtx basereg,
9999 HOST_WIDE_INT wback_offset)
ff9940b0
RE
10000{
10001 int i = 0, j;
10002 rtx result;
ff9940b0 10003
37119410
BS
10004 if (GET_CODE (basereg) == PLUS)
10005 basereg = XEXP (basereg, 0);
10006
8f4c6e28 10007 if (!multiple_operation_profitable_p (false, count, 0))
d19fb8e3
NC
10008 {
10009 rtx seq;
f676971a 10010
d19fb8e3 10011 start_sequence ();
f676971a 10012
d19fb8e3 10013 for (i = 0; i < count; i++)
37119410 10014 emit_move_insn (mems[i], gen_rtx_REG (SImode, regs[i]));
d19fb8e3 10015
37119410
BS
10016 if (wback_offset != 0)
10017 emit_move_insn (basereg, plus_constant (basereg, wback_offset));
d19fb8e3 10018
2f937369 10019 seq = get_insns ();
d19fb8e3 10020 end_sequence ();
f676971a 10021
d19fb8e3
NC
10022 return seq;
10023 }
10024
43cffd11 10025 result = gen_rtx_PARALLEL (VOIDmode,
37119410
BS
10026 rtvec_alloc (count + (wback_offset != 0 ? 1 : 0)));
10027 if (wback_offset != 0)
f3bb6135 10028 {
ff9940b0 10029 XVECEXP (result, 0, 0)
37119410
BS
10030 = gen_rtx_SET (VOIDmode, basereg,
10031 plus_constant (basereg, wback_offset));
ff9940b0
RE
10032 i = 1;
10033 count++;
f3bb6135
RE
10034 }
10035
ff9940b0 10036 for (j = 0; i < count; i++, j++)
37119410
BS
10037 XVECEXP (result, 0, i)
10038 = gen_rtx_SET (VOIDmode, mems[j], gen_rtx_REG (SImode, regs[j]));
10039
10040 return result;
10041}
10042
10043/* Generate either a load-multiple or a store-multiple instruction. This
10044 function can be used in situations where we can start with a single MEM
10045 rtx and adjust its address upwards.
10046 COUNT is the number of operations in the instruction, not counting a
10047 possible update of the base register. REGS is an array containing the
10048 register operands.
10049 BASEREG is the base register to be used in addressing the memory operands,
10050 which are constructed from BASEMEM.
10051 WRITE_BACK specifies whether the generated instruction should include an
10052 update of the base register.
10053 OFFSETP is used to pass an offset to and from this function; this offset
10054 is not used when constructing the address (instead BASEMEM should have an
10055 appropriate offset in its address), it is used only for setting
10056 MEM_OFFSET. It is updated only if WRITE_BACK is true.*/
10057
10058static rtx
10059arm_gen_multiple_op (bool is_load, int *regs, int count, rtx basereg,
10060 bool write_back, rtx basemem, HOST_WIDE_INT *offsetp)
10061{
10062 rtx mems[MAX_LDM_STM_OPS];
10063 HOST_WIDE_INT offset = *offsetp;
10064 int i;
10065
10066 gcc_assert (count <= MAX_LDM_STM_OPS);
10067
10068 if (GET_CODE (basereg) == PLUS)
10069 basereg = XEXP (basereg, 0);
10070
10071 for (i = 0; i < count; i++)
f3bb6135 10072 {
37119410
BS
10073 rtx addr = plus_constant (basereg, i * 4);
10074 mems[i] = adjust_automodify_address_nv (basemem, SImode, addr, offset);
10075 offset += 4;
f3bb6135
RE
10076 }
10077
50ed9cea
RH
10078 if (write_back)
10079 *offsetp = offset;
10080
37119410
BS
10081 if (is_load)
10082 return arm_gen_load_multiple_1 (count, regs, mems, basereg,
10083 write_back ? 4 * count : 0);
10084 else
10085 return arm_gen_store_multiple_1 (count, regs, mems, basereg,
10086 write_back ? 4 * count : 0);
10087}
10088
10089rtx
10090arm_gen_load_multiple (int *regs, int count, rtx basereg, int write_back,
10091 rtx basemem, HOST_WIDE_INT *offsetp)
10092{
10093 return arm_gen_multiple_op (TRUE, regs, count, basereg, write_back, basemem,
10094 offsetp);
10095}
10096
10097rtx
10098arm_gen_store_multiple (int *regs, int count, rtx basereg, int write_back,
10099 rtx basemem, HOST_WIDE_INT *offsetp)
10100{
10101 return arm_gen_multiple_op (FALSE, regs, count, basereg, write_back, basemem,
10102 offsetp);
10103}
10104
10105/* Called from a peephole2 expander to turn a sequence of loads into an
10106 LDM instruction. OPERANDS are the operands found by the peephole matcher;
10107 NOPS indicates how many separate loads we are trying to combine. SORT_REGS
10108 is true if we can reorder the registers because they are used commutatively
10109 subsequently.
10110 Returns true iff we could generate a new instruction. */
10111
10112bool
10113gen_ldm_seq (rtx *operands, int nops, bool sort_regs)
10114{
10115 int regs[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
10116 rtx mems[MAX_LDM_STM_OPS];
10117 int i, j, base_reg;
10118 rtx base_reg_rtx;
10119 HOST_WIDE_INT offset;
10120 int write_back = FALSE;
10121 int ldm_case;
10122 rtx addr;
10123
10124 ldm_case = load_multiple_sequence (operands, nops, regs, mem_order,
10125 &base_reg, &offset, !sort_regs);
10126
10127 if (ldm_case == 0)
10128 return false;
10129
10130 if (sort_regs)
10131 for (i = 0; i < nops - 1; i++)
10132 for (j = i + 1; j < nops; j++)
10133 if (regs[i] > regs[j])
10134 {
10135 int t = regs[i];
10136 regs[i] = regs[j];
10137 regs[j] = t;
10138 }
10139 base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
10140
10141 if (TARGET_THUMB1)
10142 {
10143 gcc_assert (peep2_reg_dead_p (nops, base_reg_rtx));
10144 gcc_assert (ldm_case == 1 || ldm_case == 5);
10145 write_back = TRUE;
10146 }
10147
10148 if (ldm_case == 5)
10149 {
10150 rtx newbase = TARGET_THUMB1 ? base_reg_rtx : gen_rtx_REG (SImode, regs[0]);
10151 emit_insn (gen_addsi3 (newbase, base_reg_rtx, GEN_INT (offset)));
10152 offset = 0;
10153 if (!TARGET_THUMB1)
10154 {
10155 base_reg = regs[0];
10156 base_reg_rtx = newbase;
10157 }
10158 }
10159
10160 for (i = 0; i < nops; i++)
10161 {
10162 addr = plus_constant (base_reg_rtx, offset + i * 4);
10163 mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
10164 SImode, addr, 0);
10165 }
10166 emit_insn (arm_gen_load_multiple_1 (nops, regs, mems, base_reg_rtx,
10167 write_back ? offset + i * 4 : 0));
10168 return true;
10169}
10170
10171/* Called from a peephole2 expander to turn a sequence of stores into an
10172 STM instruction. OPERANDS are the operands found by the peephole matcher;
10173 NOPS indicates how many separate stores we are trying to combine.
10174 Returns true iff we could generate a new instruction. */
10175
10176bool
10177gen_stm_seq (rtx *operands, int nops)
10178{
10179 int i;
10180 int regs[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
10181 rtx mems[MAX_LDM_STM_OPS];
10182 int base_reg;
10183 rtx base_reg_rtx;
10184 HOST_WIDE_INT offset;
10185 int write_back = FALSE;
10186 int stm_case;
10187 rtx addr;
10188 bool base_reg_dies;
10189
10190 stm_case = store_multiple_sequence (operands, nops, nops, regs, NULL,
10191 mem_order, &base_reg, &offset, true);
10192
10193 if (stm_case == 0)
10194 return false;
10195
10196 base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
10197
10198 base_reg_dies = peep2_reg_dead_p (nops, base_reg_rtx);
10199 if (TARGET_THUMB1)
10200 {
10201 gcc_assert (base_reg_dies);
10202 write_back = TRUE;
10203 }
10204
10205 if (stm_case == 5)
10206 {
10207 gcc_assert (base_reg_dies);
10208 emit_insn (gen_addsi3 (base_reg_rtx, base_reg_rtx, GEN_INT (offset)));
10209 offset = 0;
10210 }
10211
10212 addr = plus_constant (base_reg_rtx, offset);
10213
10214 for (i = 0; i < nops; i++)
10215 {
10216 addr = plus_constant (base_reg_rtx, offset + i * 4);
10217 mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
10218 SImode, addr, 0);
10219 }
10220 emit_insn (arm_gen_store_multiple_1 (nops, regs, mems, base_reg_rtx,
10221 write_back ? offset + i * 4 : 0));
10222 return true;
10223}
10224
10225/* Called from a peephole2 expander to turn a sequence of stores that are
10226 preceded by constant loads into an STM instruction. OPERANDS are the
10227 operands found by the peephole matcher; NOPS indicates how many
10228 separate stores we are trying to combine; there are 2 * NOPS
10229 instructions in the peephole.
10230 Returns true iff we could generate a new instruction. */
10231
10232bool
10233gen_const_stm_seq (rtx *operands, int nops)
10234{
10235 int regs[MAX_LDM_STM_OPS], sorted_regs[MAX_LDM_STM_OPS];
10236 int reg_order[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
10237 rtx reg_rtxs[MAX_LDM_STM_OPS], orig_reg_rtxs[MAX_LDM_STM_OPS];
10238 rtx mems[MAX_LDM_STM_OPS];
10239 int base_reg;
10240 rtx base_reg_rtx;
10241 HOST_WIDE_INT offset;
10242 int write_back = FALSE;
10243 int stm_case;
10244 rtx addr;
10245 bool base_reg_dies;
10246 int i, j;
10247 HARD_REG_SET allocated;
10248
10249 stm_case = store_multiple_sequence (operands, nops, 2 * nops, regs, reg_rtxs,
10250 mem_order, &base_reg, &offset, false);
10251
10252 if (stm_case == 0)
10253 return false;
10254
10255 memcpy (orig_reg_rtxs, reg_rtxs, sizeof orig_reg_rtxs);
10256
10257 /* If the same register is used more than once, try to find a free
10258 register. */
10259 CLEAR_HARD_REG_SET (allocated);
10260 for (i = 0; i < nops; i++)
10261 {
10262 for (j = i + 1; j < nops; j++)
10263 if (regs[i] == regs[j])
10264 {
10265 rtx t = peep2_find_free_register (0, nops * 2,
10266 TARGET_THUMB1 ? "l" : "r",
10267 SImode, &allocated);
10268 if (t == NULL_RTX)
10269 return false;
10270 reg_rtxs[i] = t;
10271 regs[i] = REGNO (t);
10272 }
10273 }
10274
10275 /* Compute an ordering that maps the register numbers to an ascending
10276 sequence. */
10277 reg_order[0] = 0;
10278 for (i = 0; i < nops; i++)
10279 if (regs[i] < regs[reg_order[0]])
10280 reg_order[0] = i;
10281
10282 for (i = 1; i < nops; i++)
10283 {
10284 int this_order = reg_order[i - 1];
10285 for (j = 0; j < nops; j++)
10286 if (regs[j] > regs[reg_order[i - 1]]
10287 && (this_order == reg_order[i - 1]
10288 || regs[j] < regs[this_order]))
10289 this_order = j;
10290 reg_order[i] = this_order;
10291 }
10292
10293 /* Ensure that registers that must be live after the instruction end
10294 up with the correct value. */
10295 for (i = 0; i < nops; i++)
10296 {
10297 int this_order = reg_order[i];
10298 if ((this_order != mem_order[i]
10299 || orig_reg_rtxs[this_order] != reg_rtxs[this_order])
10300 && !peep2_reg_dead_p (nops * 2, orig_reg_rtxs[this_order]))
10301 return false;
10302 }
10303
10304 /* Load the constants. */
10305 for (i = 0; i < nops; i++)
10306 {
10307 rtx op = operands[2 * nops + mem_order[i]];
10308 sorted_regs[i] = regs[reg_order[i]];
10309 emit_move_insn (reg_rtxs[reg_order[i]], op);
10310 }
10311
10312 base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
10313
10314 base_reg_dies = peep2_reg_dead_p (nops * 2, base_reg_rtx);
10315 if (TARGET_THUMB1)
10316 {
10317 gcc_assert (base_reg_dies);
10318 write_back = TRUE;
10319 }
10320
10321 if (stm_case == 5)
10322 {
10323 gcc_assert (base_reg_dies);
10324 emit_insn (gen_addsi3 (base_reg_rtx, base_reg_rtx, GEN_INT (offset)));
10325 offset = 0;
10326 }
10327
10328 addr = plus_constant (base_reg_rtx, offset);
10329
10330 for (i = 0; i < nops; i++)
10331 {
10332 addr = plus_constant (base_reg_rtx, offset + i * 4);
10333 mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
10334 SImode, addr, 0);
10335 }
10336 emit_insn (arm_gen_store_multiple_1 (nops, sorted_regs, mems, base_reg_rtx,
10337 write_back ? offset + i * 4 : 0));
10338 return true;
ff9940b0
RE
10339}
10340
880e2516 10341int
70128ad9 10342arm_gen_movmemqi (rtx *operands)
880e2516
RE
10343{
10344 HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
50ed9cea 10345 HOST_WIDE_INT srcoffset, dstoffset;
ad076f4e 10346 int i;
50ed9cea 10347 rtx src, dst, srcbase, dstbase;
880e2516 10348 rtx part_bytes_reg = NULL;
56636818 10349 rtx mem;
880e2516
RE
10350
10351 if (GET_CODE (operands[2]) != CONST_INT
10352 || GET_CODE (operands[3]) != CONST_INT
10353 || INTVAL (operands[2]) > 64
10354 || INTVAL (operands[3]) & 3)
10355 return 0;
10356
50ed9cea
RH
10357 dstbase = operands[0];
10358 srcbase = operands[1];
56636818 10359
50ed9cea
RH
10360 dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
10361 src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
880e2516 10362
e9d7b180 10363 in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
880e2516
RE
10364 out_words_to_go = INTVAL (operands[2]) / 4;
10365 last_bytes = INTVAL (operands[2]) & 3;
50ed9cea 10366 dstoffset = srcoffset = 0;
880e2516
RE
10367
10368 if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
43cffd11 10369 part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
880e2516
RE
10370
10371 for (i = 0; in_words_to_go >= 2; i+=4)
10372 {
bd9c7e23 10373 if (in_words_to_go > 4)
37119410
BS
10374 emit_insn (arm_gen_load_multiple (arm_regs_in_sequence, 4, src,
10375 TRUE, srcbase, &srcoffset));
bd9c7e23 10376 else
37119410
BS
10377 emit_insn (arm_gen_load_multiple (arm_regs_in_sequence, in_words_to_go,
10378 src, FALSE, srcbase,
10379 &srcoffset));
bd9c7e23 10380
880e2516
RE
10381 if (out_words_to_go)
10382 {
bd9c7e23 10383 if (out_words_to_go > 4)
37119410
BS
10384 emit_insn (arm_gen_store_multiple (arm_regs_in_sequence, 4, dst,
10385 TRUE, dstbase, &dstoffset));
bd9c7e23 10386 else if (out_words_to_go != 1)
37119410
BS
10387 emit_insn (arm_gen_store_multiple (arm_regs_in_sequence,
10388 out_words_to_go, dst,
bd9c7e23 10389 (last_bytes == 0
56636818 10390 ? FALSE : TRUE),
50ed9cea 10391 dstbase, &dstoffset));
880e2516
RE
10392 else
10393 {
50ed9cea 10394 mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
43cffd11 10395 emit_move_insn (mem, gen_rtx_REG (SImode, 0));
bd9c7e23 10396 if (last_bytes != 0)
50ed9cea
RH
10397 {
10398 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
10399 dstoffset += 4;
10400 }
880e2516
RE
10401 }
10402 }
10403
10404 in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
10405 out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
10406 }
10407
10408 /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do. */
10409 if (out_words_to_go)
62b10bbc
NC
10410 {
10411 rtx sreg;
f676971a 10412
50ed9cea
RH
10413 mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
10414 sreg = copy_to_reg (mem);
10415
10416 mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
62b10bbc 10417 emit_move_insn (mem, sreg);
62b10bbc 10418 in_words_to_go--;
f676971a 10419
e6d29d15 10420 gcc_assert (!in_words_to_go); /* Sanity check */
62b10bbc 10421 }
880e2516
RE
10422
10423 if (in_words_to_go)
10424 {
e6d29d15 10425 gcc_assert (in_words_to_go > 0);
880e2516 10426
50ed9cea 10427 mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
56636818 10428 part_bytes_reg = copy_to_mode_reg (SImode, mem);
880e2516
RE
10429 }
10430
e6d29d15 10431 gcc_assert (!last_bytes || part_bytes_reg);
d5b7b3ae 10432
880e2516
RE
10433 if (BYTES_BIG_ENDIAN && last_bytes)
10434 {
10435 rtx tmp = gen_reg_rtx (SImode);
10436
6354dc9b 10437 /* The bytes we want are in the top end of the word. */
bee06f3d
RE
10438 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
10439 GEN_INT (8 * (4 - last_bytes))));
880e2516 10440 part_bytes_reg = tmp;
f676971a 10441
880e2516
RE
10442 while (last_bytes)
10443 {
50ed9cea
RH
10444 mem = adjust_automodify_address (dstbase, QImode,
10445 plus_constant (dst, last_bytes - 1),
10446 dstoffset + last_bytes - 1);
5d5603e2
BS
10447 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
10448
880e2516
RE
10449 if (--last_bytes)
10450 {
10451 tmp = gen_reg_rtx (SImode);
10452 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
10453 part_bytes_reg = tmp;
10454 }
10455 }
f676971a 10456
880e2516
RE
10457 }
10458 else
10459 {
d5b7b3ae 10460 if (last_bytes > 1)
880e2516 10461 {
50ed9cea 10462 mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
5d5603e2 10463 emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
d5b7b3ae
RE
10464 last_bytes -= 2;
10465 if (last_bytes)
880e2516
RE
10466 {
10467 rtx tmp = gen_reg_rtx (SImode);
a556fd39 10468 emit_insn (gen_addsi3 (dst, dst, const2_rtx));
d5b7b3ae 10469 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
880e2516 10470 part_bytes_reg = tmp;
50ed9cea 10471 dstoffset += 2;
880e2516
RE
10472 }
10473 }
f676971a 10474
d5b7b3ae
RE
10475 if (last_bytes)
10476 {
50ed9cea 10477 mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
5d5603e2 10478 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
d5b7b3ae 10479 }
880e2516
RE
10480 }
10481
10482 return 1;
10483}
10484
03f1640c
RE
10485/* Select a dominance comparison mode if possible for a test of the general
10486 form (OP (COND_OR (X) (Y)) (const_int 0)). We support three forms.
f676971a 10487 COND_OR == DOM_CC_X_AND_Y => (X && Y)
03f1640c 10488 COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
f676971a 10489 COND_OR == DOM_CC_X_OR_Y => (X || Y)
03f1640c 10490 In all cases OP will be either EQ or NE, but we don't need to know which
f676971a 10491 here. If we are unable to support a dominance comparison we return
03f1640c
RE
10492 CC mode. This will then fail to match for the RTL expressions that
10493 generate this call. */
03f1640c 10494enum machine_mode
e32bac5b 10495arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
84ed5e79
RE
10496{
10497 enum rtx_code cond1, cond2;
10498 int swapped = 0;
10499
10500 /* Currently we will probably get the wrong result if the individual
10501 comparisons are not simple. This also ensures that it is safe to
956d6950 10502 reverse a comparison if necessary. */
84ed5e79
RE
10503 if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
10504 != CCmode)
10505 || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
10506 != CCmode))
10507 return CCmode;
10508
1646cf41
RE
10509 /* The if_then_else variant of this tests the second condition if the
10510 first passes, but is true if the first fails. Reverse the first
10511 condition to get a true "inclusive-or" expression. */
03f1640c 10512 if (cond_or == DOM_CC_NX_OR_Y)
84ed5e79
RE
10513 cond1 = reverse_condition (cond1);
10514
10515 /* If the comparisons are not equal, and one doesn't dominate the other,
10516 then we can't do this. */
f676971a 10517 if (cond1 != cond2
5895f793
RE
10518 && !comparison_dominates_p (cond1, cond2)
10519 && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
84ed5e79
RE
10520 return CCmode;
10521
10522 if (swapped)
10523 {
10524 enum rtx_code temp = cond1;
10525 cond1 = cond2;
10526 cond2 = temp;
10527 }
10528
10529 switch (cond1)
10530 {
10531 case EQ:
e6d29d15 10532 if (cond_or == DOM_CC_X_AND_Y)
84ed5e79
RE
10533 return CC_DEQmode;
10534
10535 switch (cond2)
10536 {
e6d29d15 10537 case EQ: return CC_DEQmode;
84ed5e79
RE
10538 case LE: return CC_DLEmode;
10539 case LEU: return CC_DLEUmode;
10540 case GE: return CC_DGEmode;
10541 case GEU: return CC_DGEUmode;
e6d29d15 10542 default: gcc_unreachable ();
84ed5e79
RE
10543 }
10544
84ed5e79 10545 case LT:
e6d29d15 10546 if (cond_or == DOM_CC_X_AND_Y)
84ed5e79 10547 return CC_DLTmode;
e0b92319 10548
e6d29d15
NS
10549 switch (cond2)
10550 {
10551 case LT:
10552 return CC_DLTmode;
10553 case LE:
10554 return CC_DLEmode;
10555 case NE:
10556 return CC_DNEmode;
10557 default:
10558 gcc_unreachable ();
10559 }
84ed5e79
RE
10560
10561 case GT:
e6d29d15 10562 if (cond_or == DOM_CC_X_AND_Y)
84ed5e79 10563 return CC_DGTmode;
e6d29d15
NS
10564
10565 switch (cond2)
10566 {
10567 case GT:
10568 return CC_DGTmode;
10569 case GE:
10570 return CC_DGEmode;
10571 case NE:
10572 return CC_DNEmode;
10573 default:
10574 gcc_unreachable ();
10575 }
f676971a 10576
84ed5e79 10577 case LTU:
e6d29d15 10578 if (cond_or == DOM_CC_X_AND_Y)
84ed5e79 10579 return CC_DLTUmode;
e6d29d15
NS
10580
10581 switch (cond2)
10582 {
10583 case LTU:
10584 return CC_DLTUmode;
10585 case LEU:
10586 return CC_DLEUmode;
10587 case NE:
10588 return CC_DNEmode;
10589 default:
10590 gcc_unreachable ();
10591 }
84ed5e79
RE
10592
10593 case GTU:
e6d29d15 10594 if (cond_or == DOM_CC_X_AND_Y)
84ed5e79 10595 return CC_DGTUmode;
e0b92319 10596
e6d29d15
NS
10597 switch (cond2)
10598 {
10599 case GTU:
10600 return CC_DGTUmode;
10601 case GEU:
10602 return CC_DGEUmode;
10603 case NE:
10604 return CC_DNEmode;
10605 default:
10606 gcc_unreachable ();
10607 }
84ed5e79
RE
10608
10609 /* The remaining cases only occur when both comparisons are the
10610 same. */
10611 case NE:
e6d29d15 10612 gcc_assert (cond1 == cond2);
84ed5e79
RE
10613 return CC_DNEmode;
10614
10615 case LE:
e6d29d15 10616 gcc_assert (cond1 == cond2);
84ed5e79
RE
10617 return CC_DLEmode;
10618
10619 case GE:
e6d29d15 10620 gcc_assert (cond1 == cond2);
84ed5e79
RE
10621 return CC_DGEmode;
10622
10623 case LEU:
e6d29d15 10624 gcc_assert (cond1 == cond2);
84ed5e79
RE
10625 return CC_DLEUmode;
10626
10627 case GEU:
e6d29d15 10628 gcc_assert (cond1 == cond2);
84ed5e79 10629 return CC_DGEUmode;
ad076f4e
RE
10630
10631 default:
e6d29d15 10632 gcc_unreachable ();
84ed5e79 10633 }
84ed5e79
RE
10634}
10635
10636enum machine_mode
e32bac5b 10637arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
84ed5e79
RE
10638{
10639 /* All floating point compares return CCFP if it is an equality
10640 comparison, and CCFPE otherwise. */
10641 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
e45b72c4
RE
10642 {
10643 switch (op)
10644 {
10645 case EQ:
10646 case NE:
10647 case UNORDERED:
10648 case ORDERED:
10649 case UNLT:
10650 case UNLE:
10651 case UNGT:
10652 case UNGE:
10653 case UNEQ:
10654 case LTGT:
10655 return CCFPmode;
10656
10657 case LT:
10658 case LE:
10659 case GT:
10660 case GE:
9b66ebb1 10661 if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
9b6b54e2 10662 return CCFPmode;
e45b72c4
RE
10663 return CCFPEmode;
10664
10665 default:
e6d29d15 10666 gcc_unreachable ();
e45b72c4
RE
10667 }
10668 }
f676971a 10669
84ed5e79
RE
10670 /* A compare with a shifted operand. Because of canonicalization, the
10671 comparison will have to be swapped when we emit the assembler. */
3e2d9dcf
RR
10672 if (GET_MODE (y) == SImode
10673 && (REG_P (y) || (GET_CODE (y) == SUBREG))
84ed5e79
RE
10674 && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
10675 || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
10676 || GET_CODE (x) == ROTATERT))
10677 return CC_SWPmode;
10678
04d8b819
RE
10679 /* This operation is performed swapped, but since we only rely on the Z
10680 flag we don't need an additional mode. */
3e2d9dcf
RR
10681 if (GET_MODE (y) == SImode
10682 && (REG_P (y) || (GET_CODE (y) == SUBREG))
04d8b819
RE
10683 && GET_CODE (x) == NEG
10684 && (op == EQ || op == NE))
10685 return CC_Zmode;
10686
f676971a 10687 /* This is a special case that is used by combine to allow a
956d6950 10688 comparison of a shifted byte load to be split into a zero-extend
84ed5e79 10689 followed by a comparison of the shifted integer (only valid for
956d6950 10690 equalities and unsigned inequalities). */
84ed5e79
RE
10691 if (GET_MODE (x) == SImode
10692 && GET_CODE (x) == ASHIFT
10693 && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
10694 && GET_CODE (XEXP (x, 0)) == SUBREG
10695 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
10696 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
10697 && (op == EQ || op == NE
10698 || op == GEU || op == GTU || op == LTU || op == LEU)
10699 && GET_CODE (y) == CONST_INT)
10700 return CC_Zmode;
10701
1646cf41
RE
10702 /* A construct for a conditional compare, if the false arm contains
10703 0, then both conditions must be true, otherwise either condition
10704 must be true. Not all conditions are possible, so CCmode is
10705 returned if it can't be done. */
10706 if (GET_CODE (x) == IF_THEN_ELSE
10707 && (XEXP (x, 2) == const0_rtx
10708 || XEXP (x, 2) == const1_rtx)
ec8e098d
PB
10709 && COMPARISON_P (XEXP (x, 0))
10710 && COMPARISON_P (XEXP (x, 1)))
f676971a 10711 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
03f1640c 10712 INTVAL (XEXP (x, 2)));
1646cf41
RE
10713
10714 /* Alternate canonicalizations of the above. These are somewhat cleaner. */
10715 if (GET_CODE (x) == AND
4d5d6586 10716 && (op == EQ || op == NE)
ec8e098d
PB
10717 && COMPARISON_P (XEXP (x, 0))
10718 && COMPARISON_P (XEXP (x, 1)))
03f1640c
RE
10719 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
10720 DOM_CC_X_AND_Y);
1646cf41
RE
10721
10722 if (GET_CODE (x) == IOR
4d5d6586 10723 && (op == EQ || op == NE)
ec8e098d
PB
10724 && COMPARISON_P (XEXP (x, 0))
10725 && COMPARISON_P (XEXP (x, 1)))
03f1640c
RE
10726 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
10727 DOM_CC_X_OR_Y);
1646cf41 10728
defc0463
RE
10729 /* An operation (on Thumb) where we want to test for a single bit.
10730 This is done by shifting that bit up into the top bit of a
10731 scratch register; we can then branch on the sign bit. */
5b3e6663 10732 if (TARGET_THUMB1
defc0463
RE
10733 && GET_MODE (x) == SImode
10734 && (op == EQ || op == NE)
f9fa4363
RE
10735 && GET_CODE (x) == ZERO_EXTRACT
10736 && XEXP (x, 1) == const1_rtx)
defc0463
RE
10737 return CC_Nmode;
10738
84ed5e79
RE
10739 /* An operation that sets the condition codes as a side-effect, the
10740 V flag is not set correctly, so we can only use comparisons where
10741 this doesn't matter. (For LT and GE we can use "mi" and "pl"
defc0463 10742 instead.) */
5b3e6663 10743 /* ??? Does the ZERO_EXTRACT case really apply to thumb2? */
84ed5e79
RE
10744 if (GET_MODE (x) == SImode
10745 && y == const0_rtx
10746 && (op == EQ || op == NE || op == LT || op == GE)
10747 && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
10748 || GET_CODE (x) == AND || GET_CODE (x) == IOR
10749 || GET_CODE (x) == XOR || GET_CODE (x) == MULT
10750 || GET_CODE (x) == NOT || GET_CODE (x) == NEG
10751 || GET_CODE (x) == LSHIFTRT
10752 || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
defc0463 10753 || GET_CODE (x) == ROTATERT
5b3e6663 10754 || (TARGET_32BIT && GET_CODE (x) == ZERO_EXTRACT)))
84ed5e79
RE
10755 return CC_NOOVmode;
10756
84ed5e79
RE
10757 if (GET_MODE (x) == QImode && (op == EQ || op == NE))
10758 return CC_Zmode;
10759
bd9c7e23
RE
10760 if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
10761 && GET_CODE (x) == PLUS
10762 && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
10763 return CC_Cmode;
10764
73160ba9
DJ
10765 if (GET_MODE (x) == DImode || GET_MODE (y) == DImode)
10766 {
10767 /* To keep things simple, always use the Cirrus cfcmp64 if it is
10768 available. */
10769 if (TARGET_ARM && TARGET_HARD_FLOAT && TARGET_MAVERICK)
10770 return CCmode;
10771
10772 switch (op)
10773 {
10774 case EQ:
10775 case NE:
10776 /* A DImode comparison against zero can be implemented by
10777 or'ing the two halves together. */
10778 if (y == const0_rtx)
10779 return CC_Zmode;
10780
10781 /* We can do an equality test in three Thumb instructions. */
10782 if (!TARGET_ARM)
10783 return CC_Zmode;
10784
10785 /* FALLTHROUGH */
10786
10787 case LTU:
10788 case LEU:
10789 case GTU:
10790 case GEU:
10791 /* DImode unsigned comparisons can be implemented by cmp +
10792 cmpeq without a scratch register. Not worth doing in
10793 Thumb-2. */
10794 if (TARGET_ARM)
10795 return CC_CZmode;
10796
10797 /* FALLTHROUGH */
10798
10799 case LT:
10800 case LE:
10801 case GT:
10802 case GE:
10803 /* DImode signed and unsigned comparisons can be implemented
10804 by cmp + sbcs with a scratch register, but that does not
10805 set the Z flag - we must reverse GT/LE/GTU/LEU. */
10806 gcc_assert (op != EQ && op != NE);
10807 return CC_NCVmode;
10808
10809 default:
10810 gcc_unreachable ();
10811 }
10812 }
10813
84ed5e79
RE
10814 return CCmode;
10815}
10816
ff9940b0
RE
10817/* X and Y are two things to compare using CODE. Emit the compare insn and
10818 return the rtx for register 0 in the proper mode. FP means this is a
10819 floating point compare: I don't think that it is needed on the arm. */
ff9940b0 10820rtx
e32bac5b 10821arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
ff9940b0 10822{
73160ba9
DJ
10823 enum machine_mode mode;
10824 rtx cc_reg;
10825 int dimode_comparison = GET_MODE (x) == DImode || GET_MODE (y) == DImode;
ff9940b0 10826
73160ba9
DJ
10827 /* We might have X as a constant, Y as a register because of the predicates
10828 used for cmpdi. If so, force X to a register here. */
10829 if (dimode_comparison && !REG_P (x))
10830 x = force_reg (DImode, x);
10831
10832 mode = SELECT_CC_MODE (code, x, y);
10833 cc_reg = gen_rtx_REG (mode, CC_REGNUM);
10834
10835 if (dimode_comparison
10836 && !(TARGET_HARD_FLOAT && TARGET_MAVERICK)
10837 && mode != CC_CZmode)
10838 {
10839 rtx clobber, set;
10840
10841 /* To compare two non-zero values for equality, XOR them and
10842 then compare against zero. Not used for ARM mode; there
10843 CC_CZmode is cheaper. */
10844 if (mode == CC_Zmode && y != const0_rtx)
10845 {
10846 x = expand_binop (DImode, xor_optab, x, y, NULL_RTX, 0, OPTAB_WIDEN);
10847 y = const0_rtx;
10848 }
10849 /* A scratch register is required. */
10850 clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (SImode));
10851 set = gen_rtx_SET (VOIDmode, cc_reg, gen_rtx_COMPARE (mode, x, y));
10852 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
10853 }
10854 else
10855 emit_set_insn (cc_reg, gen_rtx_COMPARE (mode, x, y));
ff9940b0
RE
10856
10857 return cc_reg;
10858}
10859
fcd53748
JT
10860/* Generate a sequence of insns that will generate the correct return
10861 address mask depending on the physical architecture that the program
10862 is running on. */
fcd53748 10863rtx
e32bac5b 10864arm_gen_return_addr_mask (void)
fcd53748
JT
10865{
10866 rtx reg = gen_reg_rtx (Pmode);
10867
10868 emit_insn (gen_return_addr_mask (reg));
10869 return reg;
10870}
10871
0a81f500 10872void
e32bac5b 10873arm_reload_in_hi (rtx *operands)
0a81f500 10874{
f9cc092a
RE
10875 rtx ref = operands[1];
10876 rtx base, scratch;
10877 HOST_WIDE_INT offset = 0;
10878
10879 if (GET_CODE (ref) == SUBREG)
10880 {
ddef6bc7 10881 offset = SUBREG_BYTE (ref);
f9cc092a
RE
10882 ref = SUBREG_REG (ref);
10883 }
10884
10885 if (GET_CODE (ref) == REG)
10886 {
10887 /* We have a pseudo which has been spilt onto the stack; there
10888 are two cases here: the first where there is a simple
10889 stack-slot replacement and a second where the stack-slot is
10890 out of range, or is used as a subreg. */
f2034d06 10891 if (reg_equiv_mem (REGNO (ref)))
f9cc092a 10892 {
f2034d06 10893 ref = reg_equiv_mem (REGNO (ref));
f9cc092a
RE
10894 base = find_replacement (&XEXP (ref, 0));
10895 }
10896 else
6354dc9b 10897 /* The slot is out of range, or was dressed up in a SUBREG. */
f2034d06 10898 base = reg_equiv_address (REGNO (ref));
f9cc092a
RE
10899 }
10900 else
10901 base = find_replacement (&XEXP (ref, 0));
0a81f500 10902
e5e809f4
JL
10903 /* Handle the case where the address is too complex to be offset by 1. */
10904 if (GET_CODE (base) == MINUS
10905 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
10906 {
f9cc092a 10907 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
e5e809f4 10908
d66437c5 10909 emit_set_insn (base_plus, base);
e5e809f4
JL
10910 base = base_plus;
10911 }
f9cc092a
RE
10912 else if (GET_CODE (base) == PLUS)
10913 {
6354dc9b 10914 /* The addend must be CONST_INT, or we would have dealt with it above. */
f9cc092a
RE
10915 HOST_WIDE_INT hi, lo;
10916
10917 offset += INTVAL (XEXP (base, 1));
10918 base = XEXP (base, 0);
10919
6354dc9b 10920 /* Rework the address into a legal sequence of insns. */
f9cc092a
RE
10921 /* Valid range for lo is -4095 -> 4095 */
10922 lo = (offset >= 0
10923 ? (offset & 0xfff)
10924 : -((-offset) & 0xfff));
10925
10926 /* Corner case, if lo is the max offset then we would be out of range
10927 once we have added the additional 1 below, so bump the msb into the
10928 pre-loading insn(s). */
10929 if (lo == 4095)
10930 lo &= 0x7ff;
10931
30cf4896
KG
10932 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
10933 ^ (HOST_WIDE_INT) 0x80000000)
10934 - (HOST_WIDE_INT) 0x80000000);
f9cc092a 10935
e6d29d15 10936 gcc_assert (hi + lo == offset);
f9cc092a
RE
10937
10938 if (hi != 0)
10939 {
10940 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10941
10942 /* Get the base address; addsi3 knows how to handle constants
6354dc9b 10943 that require more than one insn. */
f9cc092a
RE
10944 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
10945 base = base_plus;
10946 offset = lo;
10947 }
10948 }
e5e809f4 10949
3a1944a6
RE
10950 /* Operands[2] may overlap operands[0] (though it won't overlap
10951 operands[1]), that's why we asked for a DImode reg -- so we can
10952 use the bit that does not overlap. */
10953 if (REGNO (operands[2]) == REGNO (operands[0]))
10954 scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10955 else
10956 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
10957
f9cc092a
RE
10958 emit_insn (gen_zero_extendqisi2 (scratch,
10959 gen_rtx_MEM (QImode,
10960 plus_constant (base,
10961 offset))));
43cffd11 10962 emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
f676971a 10963 gen_rtx_MEM (QImode,
f9cc092a
RE
10964 plus_constant (base,
10965 offset + 1))));
5895f793 10966 if (!BYTES_BIG_ENDIAN)
d66437c5
RE
10967 emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
10968 gen_rtx_IOR (SImode,
10969 gen_rtx_ASHIFT
10970 (SImode,
10971 gen_rtx_SUBREG (SImode, operands[0], 0),
10972 GEN_INT (8)),
10973 scratch));
0a81f500 10974 else
d66437c5
RE
10975 emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
10976 gen_rtx_IOR (SImode,
10977 gen_rtx_ASHIFT (SImode, scratch,
10978 GEN_INT (8)),
10979 gen_rtx_SUBREG (SImode, operands[0], 0)));
0a81f500
RE
10980}
10981
72ac76be 10982/* Handle storing a half-word to memory during reload by synthesizing as two
f9cc092a
RE
10983 byte stores. Take care not to clobber the input values until after we
10984 have moved them somewhere safe. This code assumes that if the DImode
10985 scratch in operands[2] overlaps either the input value or output address
10986 in some way, then that value must die in this insn (we absolutely need
10987 two scratch registers for some corner cases). */
f3bb6135 10988void
e32bac5b 10989arm_reload_out_hi (rtx *operands)
af48348a 10990{
f9cc092a
RE
10991 rtx ref = operands[0];
10992 rtx outval = operands[1];
10993 rtx base, scratch;
10994 HOST_WIDE_INT offset = 0;
10995
10996 if (GET_CODE (ref) == SUBREG)
10997 {
ddef6bc7 10998 offset = SUBREG_BYTE (ref);
f9cc092a
RE
10999 ref = SUBREG_REG (ref);
11000 }
11001
f9cc092a
RE
11002 if (GET_CODE (ref) == REG)
11003 {
11004 /* We have a pseudo which has been spilt onto the stack; there
11005 are two cases here: the first where there is a simple
11006 stack-slot replacement and a second where the stack-slot is
11007 out of range, or is used as a subreg. */
f2034d06 11008 if (reg_equiv_mem (REGNO (ref)))
f9cc092a 11009 {
f2034d06 11010 ref = reg_equiv_mem (REGNO (ref));
f9cc092a
RE
11011 base = find_replacement (&XEXP (ref, 0));
11012 }
11013 else
6354dc9b 11014 /* The slot is out of range, or was dressed up in a SUBREG. */
f2034d06 11015 base = reg_equiv_address (REGNO (ref));
f9cc092a
RE
11016 }
11017 else
11018 base = find_replacement (&XEXP (ref, 0));
11019
11020 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
11021
11022 /* Handle the case where the address is too complex to be offset by 1. */
11023 if (GET_CODE (base) == MINUS
11024 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
11025 {
11026 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
11027
11028 /* Be careful not to destroy OUTVAL. */
11029 if (reg_overlap_mentioned_p (base_plus, outval))
11030 {
11031 /* Updating base_plus might destroy outval, see if we can
11032 swap the scratch and base_plus. */
5895f793 11033 if (!reg_overlap_mentioned_p (scratch, outval))
f9cc092a
RE
11034 {
11035 rtx tmp = scratch;
11036 scratch = base_plus;
11037 base_plus = tmp;
11038 }
11039 else
11040 {
11041 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
11042
11043 /* Be conservative and copy OUTVAL into the scratch now,
11044 this should only be necessary if outval is a subreg
11045 of something larger than a word. */
11046 /* XXX Might this clobber base? I can't see how it can,
11047 since scratch is known to overlap with OUTVAL, and
11048 must be wider than a word. */
11049 emit_insn (gen_movhi (scratch_hi, outval));
11050 outval = scratch_hi;
11051 }
11052 }
11053
d66437c5 11054 emit_set_insn (base_plus, base);
f9cc092a
RE
11055 base = base_plus;
11056 }
11057 else if (GET_CODE (base) == PLUS)
11058 {
6354dc9b 11059 /* The addend must be CONST_INT, or we would have dealt with it above. */
f9cc092a
RE
11060 HOST_WIDE_INT hi, lo;
11061
11062 offset += INTVAL (XEXP (base, 1));
11063 base = XEXP (base, 0);
11064
6354dc9b 11065 /* Rework the address into a legal sequence of insns. */
f9cc092a
RE
11066 /* Valid range for lo is -4095 -> 4095 */
11067 lo = (offset >= 0
11068 ? (offset & 0xfff)
11069 : -((-offset) & 0xfff));
11070
11071 /* Corner case, if lo is the max offset then we would be out of range
11072 once we have added the additional 1 below, so bump the msb into the
11073 pre-loading insn(s). */
11074 if (lo == 4095)
11075 lo &= 0x7ff;
11076
30cf4896
KG
11077 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
11078 ^ (HOST_WIDE_INT) 0x80000000)
11079 - (HOST_WIDE_INT) 0x80000000);
f9cc092a 11080
e6d29d15 11081 gcc_assert (hi + lo == offset);
f9cc092a
RE
11082
11083 if (hi != 0)
11084 {
11085 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
11086
11087 /* Be careful not to destroy OUTVAL. */
11088 if (reg_overlap_mentioned_p (base_plus, outval))
11089 {
11090 /* Updating base_plus might destroy outval, see if we
11091 can swap the scratch and base_plus. */
5895f793 11092 if (!reg_overlap_mentioned_p (scratch, outval))
f9cc092a
RE
11093 {
11094 rtx tmp = scratch;
11095 scratch = base_plus;
11096 base_plus = tmp;
11097 }
11098 else
11099 {
11100 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
11101
11102 /* Be conservative and copy outval into scratch now,
11103 this should only be necessary if outval is a
11104 subreg of something larger than a word. */
11105 /* XXX Might this clobber base? I can't see how it
11106 can, since scratch is known to overlap with
11107 outval. */
11108 emit_insn (gen_movhi (scratch_hi, outval));
11109 outval = scratch_hi;
11110 }
11111 }
11112
11113 /* Get the base address; addsi3 knows how to handle constants
6354dc9b 11114 that require more than one insn. */
f9cc092a
RE
11115 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
11116 base = base_plus;
11117 offset = lo;
11118 }
11119 }
af48348a 11120
b5cc037f
RE
11121 if (BYTES_BIG_ENDIAN)
11122 {
f676971a 11123 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
f9cc092a 11124 plus_constant (base, offset + 1)),
5d5603e2 11125 gen_lowpart (QImode, outval)));
f9cc092a
RE
11126 emit_insn (gen_lshrsi3 (scratch,
11127 gen_rtx_SUBREG (SImode, outval, 0),
b5cc037f 11128 GEN_INT (8)));
f9cc092a 11129 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
5d5603e2 11130 gen_lowpart (QImode, scratch)));
b5cc037f
RE
11131 }
11132 else
11133 {
f9cc092a 11134 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
5d5603e2 11135 gen_lowpart (QImode, outval)));
f9cc092a
RE
11136 emit_insn (gen_lshrsi3 (scratch,
11137 gen_rtx_SUBREG (SImode, outval, 0),
b5cc037f 11138 GEN_INT (8)));
f9cc092a
RE
11139 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
11140 plus_constant (base, offset + 1)),
5d5603e2 11141 gen_lowpart (QImode, scratch)));
b5cc037f 11142 }
af48348a 11143}
866af8a9
JB
11144
11145/* Return true if a type must be passed in memory. For AAPCS, small aggregates
11146 (padded to the size of a word) should be passed in a register. */
11147
11148static bool
586de218 11149arm_must_pass_in_stack (enum machine_mode mode, const_tree type)
866af8a9
JB
11150{
11151 if (TARGET_AAPCS_BASED)
11152 return must_pass_in_stack_var_size (mode, type);
11153 else
11154 return must_pass_in_stack_var_size_or_pad (mode, type);
11155}
11156
11157
11158/* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
11159 Return true if an argument passed on the stack should be padded upwards,
5a29b385
PB
11160 i.e. if the least-significant byte has useful data.
11161 For legacy APCS ABIs we use the default. For AAPCS based ABIs small
11162 aggregate types are placed in the lowest memory address. */
866af8a9
JB
11163
11164bool
586de218 11165arm_pad_arg_upward (enum machine_mode mode, const_tree type)
866af8a9
JB
11166{
11167 if (!TARGET_AAPCS_BASED)
5a29b385 11168 return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
866af8a9
JB
11169
11170 if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
11171 return false;
11172
11173 return true;
11174}
11175
11176
11177/* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
11178 For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
11179 byte of the register has useful data, and return the opposite if the
11180 most significant byte does.
11181 For AAPCS, small aggregates and small complex types are always padded
11182 upwards. */
11183
11184bool
11185arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
11186 tree type, int first ATTRIBUTE_UNUSED)
11187{
11188 if (TARGET_AAPCS_BASED
11189 && BYTES_BIG_ENDIAN
11190 && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11191 && int_size_in_bytes (type) <= 4)
11192 return true;
11193
11194 /* Otherwise, use default padding. */
11195 return !BYTES_BIG_ENDIAN;
11196}
11197
2b835d68 11198\f
d5b7b3ae
RE
11199/* Print a symbolic form of X to the debug file, F. */
11200static void
e32bac5b 11201arm_print_value (FILE *f, rtx x)
d5b7b3ae
RE
11202{
11203 switch (GET_CODE (x))
11204 {
11205 case CONST_INT:
11206 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
11207 return;
11208
11209 case CONST_DOUBLE:
11210 fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
11211 return;
11212
5a9335ef
NC
11213 case CONST_VECTOR:
11214 {
11215 int i;
11216
11217 fprintf (f, "<");
11218 for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
11219 {
11220 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
11221 if (i < (CONST_VECTOR_NUNITS (x) - 1))
11222 fputc (',', f);
11223 }
11224 fprintf (f, ">");
11225 }
11226 return;
11227
d5b7b3ae
RE
11228 case CONST_STRING:
11229 fprintf (f, "\"%s\"", XSTR (x, 0));
11230 return;
11231
11232 case SYMBOL_REF:
11233 fprintf (f, "`%s'", XSTR (x, 0));
11234 return;
11235
11236 case LABEL_REF:
11237 fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
11238 return;
11239
11240 case CONST:
11241 arm_print_value (f, XEXP (x, 0));
11242 return;
11243
11244 case PLUS:
11245 arm_print_value (f, XEXP (x, 0));
11246 fprintf (f, "+");
11247 arm_print_value (f, XEXP (x, 1));
11248 return;
11249
11250 case PC:
11251 fprintf (f, "pc");
11252 return;
11253
11254 default:
11255 fprintf (f, "????");
11256 return;
11257 }
11258}
11259\f
2b835d68 11260/* Routines for manipulation of the constant pool. */
2b835d68 11261
949d79eb
RE
11262/* Arm instructions cannot load a large constant directly into a
11263 register; they have to come from a pc relative load. The constant
11264 must therefore be placed in the addressable range of the pc
11265 relative load. Depending on the precise pc relative load
11266 instruction the range is somewhere between 256 bytes and 4k. This
11267 means that we often have to dump a constant inside a function, and
2b835d68
RE
11268 generate code to branch around it.
11269
949d79eb
RE
11270 It is important to minimize this, since the branches will slow
11271 things down and make the code larger.
2b835d68 11272
949d79eb
RE
11273 Normally we can hide the table after an existing unconditional
11274 branch so that there is no interruption of the flow, but in the
11275 worst case the code looks like this:
2b835d68
RE
11276
11277 ldr rn, L1
949d79eb 11278 ...
2b835d68
RE
11279 b L2
11280 align
11281 L1: .long value
11282 L2:
949d79eb 11283 ...
2b835d68 11284
2b835d68 11285 ldr rn, L3
949d79eb 11286 ...
2b835d68
RE
11287 b L4
11288 align
2b835d68
RE
11289 L3: .long value
11290 L4:
949d79eb
RE
11291 ...
11292
11293 We fix this by performing a scan after scheduling, which notices
11294 which instructions need to have their operands fetched from the
11295 constant table and builds the table.
11296
11297 The algorithm starts by building a table of all the constants that
11298 need fixing up and all the natural barriers in the function (places
11299 where a constant table can be dropped without breaking the flow).
11300 For each fixup we note how far the pc-relative replacement will be
11301 able to reach and the offset of the instruction into the function.
11302
11303 Having built the table we then group the fixes together to form
11304 tables that are as large as possible (subject to addressing
11305 constraints) and emit each table of constants after the last
11306 barrier that is within range of all the instructions in the group.
11307 If a group does not contain a barrier, then we forcibly create one
11308 by inserting a jump instruction into the flow. Once the table has
11309 been inserted, the insns are then modified to reference the
11310 relevant entry in the pool.
11311
6354dc9b 11312 Possible enhancements to the algorithm (not implemented) are:
949d79eb 11313
d5b7b3ae 11314 1) For some processors and object formats, there may be benefit in
949d79eb
RE
11315 aligning the pools to the start of cache lines; this alignment
11316 would need to be taken into account when calculating addressability
6354dc9b 11317 of a pool. */
2b835d68 11318
d5b7b3ae
RE
11319/* These typedefs are located at the start of this file, so that
11320 they can be used in the prototypes there. This comment is to
11321 remind readers of that fact so that the following structures
11322 can be understood more easily.
11323
11324 typedef struct minipool_node Mnode;
11325 typedef struct minipool_fixup Mfix; */
11326
11327struct minipool_node
11328{
11329 /* Doubly linked chain of entries. */
11330 Mnode * next;
11331 Mnode * prev;
11332 /* The maximum offset into the code that this entry can be placed. While
11333 pushing fixes for forward references, all entries are sorted in order
11334 of increasing max_address. */
11335 HOST_WIDE_INT max_address;
5519a4f9 11336 /* Similarly for an entry inserted for a backwards ref. */
d5b7b3ae
RE
11337 HOST_WIDE_INT min_address;
11338 /* The number of fixes referencing this entry. This can become zero
11339 if we "unpush" an entry. In this case we ignore the entry when we
11340 come to emit the code. */
11341 int refcount;
11342 /* The offset from the start of the minipool. */
11343 HOST_WIDE_INT offset;
11344 /* The value in table. */
11345 rtx value;
11346 /* The mode of value. */
11347 enum machine_mode mode;
5a9335ef
NC
11348 /* The size of the value. With iWMMXt enabled
11349 sizes > 4 also imply an alignment of 8-bytes. */
d5b7b3ae
RE
11350 int fix_size;
11351};
11352
11353struct minipool_fixup
2b835d68 11354{
d5b7b3ae
RE
11355 Mfix * next;
11356 rtx insn;
11357 HOST_WIDE_INT address;
11358 rtx * loc;
11359 enum machine_mode mode;
11360 int fix_size;
11361 rtx value;
11362 Mnode * minipool;
11363 HOST_WIDE_INT forwards;
11364 HOST_WIDE_INT backwards;
11365};
2b835d68 11366
d5b7b3ae
RE
11367/* Fixes less than a word need padding out to a word boundary. */
11368#define MINIPOOL_FIX_SIZE(mode) \
11369 (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
2b835d68 11370
d5b7b3ae
RE
11371static Mnode * minipool_vector_head;
11372static Mnode * minipool_vector_tail;
11373static rtx minipool_vector_label;
34a9f549 11374static int minipool_pad;
332072db 11375
d5b7b3ae
RE
11376/* The linked list of all minipool fixes required for this function. */
11377Mfix * minipool_fix_head;
11378Mfix * minipool_fix_tail;
11379/* The fix entry for the current minipool, once it has been placed. */
11380Mfix * minipool_barrier;
11381
11382/* Determines if INSN is the start of a jump table. Returns the end
11383 of the TABLE or NULL_RTX. */
11384static rtx
e32bac5b 11385is_jump_table (rtx insn)
2b835d68 11386{
d5b7b3ae 11387 rtx table;
f676971a 11388
d5b7b3ae
RE
11389 if (GET_CODE (insn) == JUMP_INSN
11390 && JUMP_LABEL (insn) != NULL
11391 && ((table = next_real_insn (JUMP_LABEL (insn)))
11392 == next_real_insn (insn))
11393 && table != NULL
11394 && GET_CODE (table) == JUMP_INSN
11395 && (GET_CODE (PATTERN (table)) == ADDR_VEC
11396 || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
11397 return table;
11398
11399 return NULL_RTX;
2b835d68
RE
11400}
11401
657d9449
RE
11402#ifndef JUMP_TABLES_IN_TEXT_SECTION
11403#define JUMP_TABLES_IN_TEXT_SECTION 0
11404#endif
11405
d5b7b3ae 11406static HOST_WIDE_INT
e32bac5b 11407get_jump_table_size (rtx insn)
2b835d68 11408{
657d9449
RE
11409 /* ADDR_VECs only take room if read-only data does into the text
11410 section. */
d6b5193b 11411 if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
657d9449
RE
11412 {
11413 rtx body = PATTERN (insn);
11414 int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
5b3e6663
PB
11415 HOST_WIDE_INT size;
11416 HOST_WIDE_INT modesize;
2b835d68 11417
5b3e6663
PB
11418 modesize = GET_MODE_SIZE (GET_MODE (body));
11419 size = modesize * XVECLEN (body, elt);
11420 switch (modesize)
11421 {
11422 case 1:
88512ba0 11423 /* Round up size of TBB table to a halfword boundary. */
5b3e6663
PB
11424 size = (size + 1) & ~(HOST_WIDE_INT)1;
11425 break;
11426 case 2:
7a085dce 11427 /* No padding necessary for TBH. */
5b3e6663
PB
11428 break;
11429 case 4:
11430 /* Add two bytes for alignment on Thumb. */
11431 if (TARGET_THUMB)
11432 size += 2;
11433 break;
11434 default:
11435 gcc_unreachable ();
11436 }
11437 return size;
657d9449
RE
11438 }
11439
11440 return 0;
d5b7b3ae 11441}
2b835d68 11442
d5b7b3ae
RE
11443/* Move a minipool fix MP from its current location to before MAX_MP.
11444 If MAX_MP is NULL, then MP doesn't need moving, but the addressing
093354e0 11445 constraints may need updating. */
d5b7b3ae 11446static Mnode *
e32bac5b
RE
11447move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
11448 HOST_WIDE_INT max_address)
d5b7b3ae 11449{
e6d29d15
NS
11450 /* The code below assumes these are different. */
11451 gcc_assert (mp != max_mp);
d5b7b3ae
RE
11452
11453 if (max_mp == NULL)
11454 {
11455 if (max_address < mp->max_address)
11456 mp->max_address = max_address;
11457 }
11458 else
2b835d68 11459 {
d5b7b3ae
RE
11460 if (max_address > max_mp->max_address - mp->fix_size)
11461 mp->max_address = max_mp->max_address - mp->fix_size;
11462 else
11463 mp->max_address = max_address;
2b835d68 11464
d5b7b3ae
RE
11465 /* Unlink MP from its current position. Since max_mp is non-null,
11466 mp->prev must be non-null. */
11467 mp->prev->next = mp->next;
11468 if (mp->next != NULL)
11469 mp->next->prev = mp->prev;
11470 else
11471 minipool_vector_tail = mp->prev;
2b835d68 11472
d5b7b3ae
RE
11473 /* Re-insert it before MAX_MP. */
11474 mp->next = max_mp;
11475 mp->prev = max_mp->prev;
11476 max_mp->prev = mp;
f676971a 11477
d5b7b3ae
RE
11478 if (mp->prev != NULL)
11479 mp->prev->next = mp;
11480 else
11481 minipool_vector_head = mp;
11482 }
2b835d68 11483
d5b7b3ae
RE
11484 /* Save the new entry. */
11485 max_mp = mp;
11486
d6a7951f 11487 /* Scan over the preceding entries and adjust their addresses as
d5b7b3ae
RE
11488 required. */
11489 while (mp->prev != NULL
11490 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
11491 {
11492 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
11493 mp = mp->prev;
2b835d68
RE
11494 }
11495
d5b7b3ae 11496 return max_mp;
2b835d68
RE
11497}
11498
d5b7b3ae
RE
11499/* Add a constant to the minipool for a forward reference. Returns the
11500 node added or NULL if the constant will not fit in this pool. */
11501static Mnode *
e32bac5b 11502add_minipool_forward_ref (Mfix *fix)
d5b7b3ae
RE
11503{
11504 /* If set, max_mp is the first pool_entry that has a lower
11505 constraint than the one we are trying to add. */
11506 Mnode * max_mp = NULL;
34a9f549 11507 HOST_WIDE_INT max_address = fix->address + fix->forwards - minipool_pad;
d5b7b3ae 11508 Mnode * mp;
f676971a 11509
7a7017bc
PB
11510 /* If the minipool starts before the end of FIX->INSN then this FIX
11511 can not be placed into the current pool. Furthermore, adding the
11512 new constant pool entry may cause the pool to start FIX_SIZE bytes
11513 earlier. */
d5b7b3ae 11514 if (minipool_vector_head &&
7a7017bc
PB
11515 (fix->address + get_attr_length (fix->insn)
11516 >= minipool_vector_head->max_address - fix->fix_size))
d5b7b3ae 11517 return NULL;
2b835d68 11518
d5b7b3ae
RE
11519 /* Scan the pool to see if a constant with the same value has
11520 already been added. While we are doing this, also note the
11521 location where we must insert the constant if it doesn't already
11522 exist. */
11523 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11524 {
11525 if (GET_CODE (fix->value) == GET_CODE (mp->value)
11526 && fix->mode == mp->mode
11527 && (GET_CODE (fix->value) != CODE_LABEL
11528 || (CODE_LABEL_NUMBER (fix->value)
11529 == CODE_LABEL_NUMBER (mp->value)))
11530 && rtx_equal_p (fix->value, mp->value))
11531 {
11532 /* More than one fix references this entry. */
11533 mp->refcount++;
11534 return move_minipool_fix_forward_ref (mp, max_mp, max_address);
11535 }
11536
11537 /* Note the insertion point if necessary. */
11538 if (max_mp == NULL
11539 && mp->max_address > max_address)
11540 max_mp = mp;
5a9335ef
NC
11541
11542 /* If we are inserting an 8-bytes aligned quantity and
11543 we have not already found an insertion point, then
11544 make sure that all such 8-byte aligned quantities are
11545 placed at the start of the pool. */
5848830f 11546 if (ARM_DOUBLEWORD_ALIGN
5a9335ef 11547 && max_mp == NULL
88f77cba
JB
11548 && fix->fix_size >= 8
11549 && mp->fix_size < 8)
5a9335ef
NC
11550 {
11551 max_mp = mp;
11552 max_address = mp->max_address;
11553 }
d5b7b3ae
RE
11554 }
11555
11556 /* The value is not currently in the minipool, so we need to create
11557 a new entry for it. If MAX_MP is NULL, the entry will be put on
11558 the end of the list since the placement is less constrained than
11559 any existing entry. Otherwise, we insert the new fix before
6bc82793 11560 MAX_MP and, if necessary, adjust the constraints on the other
d5b7b3ae 11561 entries. */
5ed6ace5 11562 mp = XNEW (Mnode);
d5b7b3ae
RE
11563 mp->fix_size = fix->fix_size;
11564 mp->mode = fix->mode;
11565 mp->value = fix->value;
11566 mp->refcount = 1;
11567 /* Not yet required for a backwards ref. */
11568 mp->min_address = -65536;
11569
11570 if (max_mp == NULL)
11571 {
11572 mp->max_address = max_address;
11573 mp->next = NULL;
11574 mp->prev = minipool_vector_tail;
11575
11576 if (mp->prev == NULL)
11577 {
11578 minipool_vector_head = mp;
11579 minipool_vector_label = gen_label_rtx ();
7551cbc7 11580 }
2b835d68 11581 else
d5b7b3ae 11582 mp->prev->next = mp;
2b835d68 11583
d5b7b3ae
RE
11584 minipool_vector_tail = mp;
11585 }
11586 else
11587 {
11588 if (max_address > max_mp->max_address - mp->fix_size)
11589 mp->max_address = max_mp->max_address - mp->fix_size;
11590 else
11591 mp->max_address = max_address;
11592
11593 mp->next = max_mp;
11594 mp->prev = max_mp->prev;
11595 max_mp->prev = mp;
11596 if (mp->prev != NULL)
11597 mp->prev->next = mp;
11598 else
11599 minipool_vector_head = mp;
11600 }
11601
11602 /* Save the new entry. */
11603 max_mp = mp;
11604
d6a7951f 11605 /* Scan over the preceding entries and adjust their addresses as
d5b7b3ae
RE
11606 required. */
11607 while (mp->prev != NULL
11608 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
11609 {
11610 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
11611 mp = mp->prev;
2b835d68
RE
11612 }
11613
d5b7b3ae
RE
11614 return max_mp;
11615}
11616
11617static Mnode *
e32bac5b
RE
11618move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
11619 HOST_WIDE_INT min_address)
d5b7b3ae
RE
11620{
11621 HOST_WIDE_INT offset;
11622
e6d29d15
NS
11623 /* The code below assumes these are different. */
11624 gcc_assert (mp != min_mp);
d5b7b3ae
RE
11625
11626 if (min_mp == NULL)
2b835d68 11627 {
d5b7b3ae
RE
11628 if (min_address > mp->min_address)
11629 mp->min_address = min_address;
11630 }
11631 else
11632 {
11633 /* We will adjust this below if it is too loose. */
11634 mp->min_address = min_address;
11635
11636 /* Unlink MP from its current position. Since min_mp is non-null,
11637 mp->next must be non-null. */
11638 mp->next->prev = mp->prev;
11639 if (mp->prev != NULL)
11640 mp->prev->next = mp->next;
11641 else
11642 minipool_vector_head = mp->next;
11643
11644 /* Reinsert it after MIN_MP. */
11645 mp->prev = min_mp;
11646 mp->next = min_mp->next;
11647 min_mp->next = mp;
11648 if (mp->next != NULL)
11649 mp->next->prev = mp;
2b835d68 11650 else
d5b7b3ae
RE
11651 minipool_vector_tail = mp;
11652 }
11653
11654 min_mp = mp;
11655
11656 offset = 0;
11657 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11658 {
11659 mp->offset = offset;
11660 if (mp->refcount > 0)
11661 offset += mp->fix_size;
11662
11663 if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
11664 mp->next->min_address = mp->min_address + mp->fix_size;
11665 }
11666
11667 return min_mp;
f676971a 11668}
d5b7b3ae
RE
11669
11670/* Add a constant to the minipool for a backward reference. Returns the
f676971a 11671 node added or NULL if the constant will not fit in this pool.
d5b7b3ae
RE
11672
11673 Note that the code for insertion for a backwards reference can be
11674 somewhat confusing because the calculated offsets for each fix do
11675 not take into account the size of the pool (which is still under
11676 construction. */
11677static Mnode *
e32bac5b 11678add_minipool_backward_ref (Mfix *fix)
d5b7b3ae
RE
11679{
11680 /* If set, min_mp is the last pool_entry that has a lower constraint
11681 than the one we are trying to add. */
e32bac5b 11682 Mnode *min_mp = NULL;
d5b7b3ae
RE
11683 /* This can be negative, since it is only a constraint. */
11684 HOST_WIDE_INT min_address = fix->address - fix->backwards;
e32bac5b 11685 Mnode *mp;
d5b7b3ae
RE
11686
11687 /* If we can't reach the current pool from this insn, or if we can't
11688 insert this entry at the end of the pool without pushing other
11689 fixes out of range, then we don't try. This ensures that we
11690 can't fail later on. */
11691 if (min_address >= minipool_barrier->address
11692 || (minipool_vector_tail->min_address + fix->fix_size
11693 >= minipool_barrier->address))
11694 return NULL;
11695
11696 /* Scan the pool to see if a constant with the same value has
11697 already been added. While we are doing this, also note the
11698 location where we must insert the constant if it doesn't already
11699 exist. */
11700 for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
11701 {
11702 if (GET_CODE (fix->value) == GET_CODE (mp->value)
11703 && fix->mode == mp->mode
11704 && (GET_CODE (fix->value) != CODE_LABEL
11705 || (CODE_LABEL_NUMBER (fix->value)
11706 == CODE_LABEL_NUMBER (mp->value)))
11707 && rtx_equal_p (fix->value, mp->value)
11708 /* Check that there is enough slack to move this entry to the
11709 end of the table (this is conservative). */
f676971a
EC
11710 && (mp->max_address
11711 > (minipool_barrier->address
d5b7b3ae
RE
11712 + minipool_vector_tail->offset
11713 + minipool_vector_tail->fix_size)))
11714 {
11715 mp->refcount++;
11716 return move_minipool_fix_backward_ref (mp, min_mp, min_address);
11717 }
11718
11719 if (min_mp != NULL)
11720 mp->min_address += fix->fix_size;
11721 else
11722 {
11723 /* Note the insertion point if necessary. */
11724 if (mp->min_address < min_address)
5a9335ef
NC
11725 {
11726 /* For now, we do not allow the insertion of 8-byte alignment
11727 requiring nodes anywhere but at the start of the pool. */
5848830f 11728 if (ARM_DOUBLEWORD_ALIGN
88f77cba 11729 && fix->fix_size >= 8 && mp->fix_size < 8)
5a9335ef
NC
11730 return NULL;
11731 else
11732 min_mp = mp;
11733 }
d5b7b3ae
RE
11734 else if (mp->max_address
11735 < minipool_barrier->address + mp->offset + fix->fix_size)
11736 {
11737 /* Inserting before this entry would push the fix beyond
11738 its maximum address (which can happen if we have
11739 re-located a forwards fix); force the new fix to come
11740 after it. */
853ff9e2
JM
11741 if (ARM_DOUBLEWORD_ALIGN
11742 && fix->fix_size >= 8 && mp->fix_size < 8)
11743 return NULL;
11744 else
11745 {
11746 min_mp = mp;
11747 min_address = mp->min_address + fix->fix_size;
11748 }
d5b7b3ae 11749 }
853ff9e2
JM
11750 /* Do not insert a non-8-byte aligned quantity before 8-byte
11751 aligned quantities. */
5848830f 11752 else if (ARM_DOUBLEWORD_ALIGN
853ff9e2
JM
11753 && fix->fix_size < 8
11754 && mp->fix_size >= 8)
5a9335ef
NC
11755 {
11756 min_mp = mp;
11757 min_address = mp->min_address + fix->fix_size;
11758 }
d5b7b3ae
RE
11759 }
11760 }
11761
11762 /* We need to create a new entry. */
5ed6ace5 11763 mp = XNEW (Mnode);
d5b7b3ae
RE
11764 mp->fix_size = fix->fix_size;
11765 mp->mode = fix->mode;
11766 mp->value = fix->value;
11767 mp->refcount = 1;
11768 mp->max_address = minipool_barrier->address + 65536;
11769
11770 mp->min_address = min_address;
11771
11772 if (min_mp == NULL)
11773 {
11774 mp->prev = NULL;
11775 mp->next = minipool_vector_head;
11776
11777 if (mp->next == NULL)
11778 {
11779 minipool_vector_tail = mp;
11780 minipool_vector_label = gen_label_rtx ();
11781 }
11782 else
11783 mp->next->prev = mp;
11784
11785 minipool_vector_head = mp;
11786 }
11787 else
11788 {
11789 mp->next = min_mp->next;
11790 mp->prev = min_mp;
11791 min_mp->next = mp;
f676971a 11792
d5b7b3ae
RE
11793 if (mp->next != NULL)
11794 mp->next->prev = mp;
11795 else
11796 minipool_vector_tail = mp;
11797 }
11798
11799 /* Save the new entry. */
11800 min_mp = mp;
11801
11802 if (mp->prev)
11803 mp = mp->prev;
11804 else
11805 mp->offset = 0;
11806
11807 /* Scan over the following entries and adjust their offsets. */
11808 while (mp->next != NULL)
11809 {
11810 if (mp->next->min_address < mp->min_address + mp->fix_size)
11811 mp->next->min_address = mp->min_address + mp->fix_size;
11812
11813 if (mp->refcount)
11814 mp->next->offset = mp->offset + mp->fix_size;
11815 else
11816 mp->next->offset = mp->offset;
11817
11818 mp = mp->next;
11819 }
11820
11821 return min_mp;
11822}
11823
11824static void
e32bac5b 11825assign_minipool_offsets (Mfix *barrier)
d5b7b3ae
RE
11826{
11827 HOST_WIDE_INT offset = 0;
e32bac5b 11828 Mnode *mp;
d5b7b3ae
RE
11829
11830 minipool_barrier = barrier;
11831
11832 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11833 {
11834 mp->offset = offset;
f676971a 11835
d5b7b3ae
RE
11836 if (mp->refcount > 0)
11837 offset += mp->fix_size;
11838 }
11839}
11840
11841/* Output the literal table */
11842static void
e32bac5b 11843dump_minipool (rtx scan)
d5b7b3ae 11844{
5a9335ef
NC
11845 Mnode * mp;
11846 Mnode * nmp;
11847 int align64 = 0;
11848
5848830f 11849 if (ARM_DOUBLEWORD_ALIGN)
5a9335ef 11850 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
88f77cba 11851 if (mp->refcount > 0 && mp->fix_size >= 8)
5a9335ef
NC
11852 {
11853 align64 = 1;
11854 break;
11855 }
d5b7b3ae 11856
c263766c
RH
11857 if (dump_file)
11858 fprintf (dump_file,
5a9335ef
NC
11859 ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
11860 INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
d5b7b3ae
RE
11861
11862 scan = emit_label_after (gen_label_rtx (), scan);
5a9335ef 11863 scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
d5b7b3ae
RE
11864 scan = emit_label_after (minipool_vector_label, scan);
11865
11866 for (mp = minipool_vector_head; mp != NULL; mp = nmp)
11867 {
11868 if (mp->refcount > 0)
11869 {
c263766c 11870 if (dump_file)
d5b7b3ae 11871 {
f676971a 11872 fprintf (dump_file,
d5b7b3ae
RE
11873 ";; Offset %u, min %ld, max %ld ",
11874 (unsigned) mp->offset, (unsigned long) mp->min_address,
11875 (unsigned long) mp->max_address);
c263766c
RH
11876 arm_print_value (dump_file, mp->value);
11877 fputc ('\n', dump_file);
d5b7b3ae
RE
11878 }
11879
11880 switch (mp->fix_size)
11881 {
11882#ifdef HAVE_consttable_1
11883 case 1:
11884 scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
11885 break;
11886
11887#endif
11888#ifdef HAVE_consttable_2
11889 case 2:
11890 scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
11891 break;
11892
11893#endif
11894#ifdef HAVE_consttable_4
11895 case 4:
11896 scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
11897 break;
11898
11899#endif
11900#ifdef HAVE_consttable_8
11901 case 8:
11902 scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
11903 break;
11904
88f77cba
JB
11905#endif
11906#ifdef HAVE_consttable_16
11907 case 16:
11908 scan = emit_insn_after (gen_consttable_16 (mp->value), scan);
11909 break;
11910
d5b7b3ae
RE
11911#endif
11912 default:
e6d29d15 11913 gcc_unreachable ();
d5b7b3ae
RE
11914 }
11915 }
11916
11917 nmp = mp->next;
11918 free (mp);
2b835d68
RE
11919 }
11920
d5b7b3ae
RE
11921 minipool_vector_head = minipool_vector_tail = NULL;
11922 scan = emit_insn_after (gen_consttable_end (), scan);
11923 scan = emit_barrier_after (scan);
2b835d68
RE
11924}
11925
d5b7b3ae
RE
11926/* Return the cost of forcibly inserting a barrier after INSN. */
11927static int
e32bac5b 11928arm_barrier_cost (rtx insn)
949d79eb 11929{
d5b7b3ae
RE
11930 /* Basing the location of the pool on the loop depth is preferable,
11931 but at the moment, the basic block information seems to be
11932 corrupt by this stage of the compilation. */
11933 int base_cost = 50;
11934 rtx next = next_nonnote_insn (insn);
11935
11936 if (next != NULL && GET_CODE (next) == CODE_LABEL)
11937 base_cost -= 20;
11938
11939 switch (GET_CODE (insn))
11940 {
11941 case CODE_LABEL:
11942 /* It will always be better to place the table before the label, rather
11943 than after it. */
f676971a 11944 return 50;
949d79eb 11945
d5b7b3ae
RE
11946 case INSN:
11947 case CALL_INSN:
11948 return base_cost;
11949
11950 case JUMP_INSN:
11951 return base_cost - 10;
11952
11953 default:
11954 return base_cost + 10;
11955 }
11956}
11957
11958/* Find the best place in the insn stream in the range
11959 (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
11960 Create the barrier by inserting a jump and add a new fix entry for
11961 it. */
11962static Mfix *
e32bac5b 11963create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
d5b7b3ae
RE
11964{
11965 HOST_WIDE_INT count = 0;
11966 rtx barrier;
11967 rtx from = fix->insn;
7a7017bc
PB
11968 /* The instruction after which we will insert the jump. */
11969 rtx selected = NULL;
d5b7b3ae 11970 int selected_cost;
7a7017bc 11971 /* The address at which the jump instruction will be placed. */
d5b7b3ae
RE
11972 HOST_WIDE_INT selected_address;
11973 Mfix * new_fix;
11974 HOST_WIDE_INT max_count = max_address - fix->address;
11975 rtx label = gen_label_rtx ();
11976
11977 selected_cost = arm_barrier_cost (from);
11978 selected_address = fix->address;
11979
11980 while (from && count < max_count)
11981 {
11982 rtx tmp;
11983 int new_cost;
11984
11985 /* This code shouldn't have been called if there was a natural barrier
11986 within range. */
e6d29d15 11987 gcc_assert (GET_CODE (from) != BARRIER);
d5b7b3ae
RE
11988
11989 /* Count the length of this insn. */
11990 count += get_attr_length (from);
11991
11992 /* If there is a jump table, add its length. */
11993 tmp = is_jump_table (from);
11994 if (tmp != NULL)
11995 {
11996 count += get_jump_table_size (tmp);
11997
11998 /* Jump tables aren't in a basic block, so base the cost on
11999 the dispatch insn. If we select this location, we will
12000 still put the pool after the table. */
12001 new_cost = arm_barrier_cost (from);
12002
7a7017bc
PB
12003 if (count < max_count
12004 && (!selected || new_cost <= selected_cost))
d5b7b3ae
RE
12005 {
12006 selected = tmp;
12007 selected_cost = new_cost;
12008 selected_address = fix->address + count;
12009 }
12010
12011 /* Continue after the dispatch table. */
12012 from = NEXT_INSN (tmp);
12013 continue;
12014 }
12015
12016 new_cost = arm_barrier_cost (from);
f676971a 12017
7a7017bc
PB
12018 if (count < max_count
12019 && (!selected || new_cost <= selected_cost))
d5b7b3ae
RE
12020 {
12021 selected = from;
12022 selected_cost = new_cost;
12023 selected_address = fix->address + count;
12024 }
12025
12026 from = NEXT_INSN (from);
12027 }
12028
7a7017bc
PB
12029 /* Make sure that we found a place to insert the jump. */
12030 gcc_assert (selected);
12031
b6a75dda
RR
12032 /* Make sure we do not split a call and its corresponding
12033 CALL_ARG_LOCATION note. */
12034 if (CALL_P (selected))
12035 {
12036 rtx next = NEXT_INSN (selected);
12037 if (next && NOTE_P (next)
12038 && NOTE_KIND (next) == NOTE_INSN_CALL_ARG_LOCATION)
12039 selected = next;
12040 }
12041
d5b7b3ae
RE
12042 /* Create a new JUMP_INSN that branches around a barrier. */
12043 from = emit_jump_insn_after (gen_jump (label), selected);
12044 JUMP_LABEL (from) = label;
12045 barrier = emit_barrier_after (from);
12046 emit_label_after (label, barrier);
12047
12048 /* Create a minipool barrier entry for the new barrier. */
c7319d87 12049 new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
d5b7b3ae
RE
12050 new_fix->insn = barrier;
12051 new_fix->address = selected_address;
12052 new_fix->next = fix->next;
12053 fix->next = new_fix;
12054
12055 return new_fix;
12056}
12057
12058/* Record that there is a natural barrier in the insn stream at
12059 ADDRESS. */
949d79eb 12060static void
e32bac5b 12061push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
2b835d68 12062{
c7319d87 12063 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
ad076f4e 12064
949d79eb
RE
12065 fix->insn = insn;
12066 fix->address = address;
2b835d68 12067
949d79eb
RE
12068 fix->next = NULL;
12069 if (minipool_fix_head != NULL)
12070 minipool_fix_tail->next = fix;
12071 else
12072 minipool_fix_head = fix;
12073
12074 minipool_fix_tail = fix;
12075}
2b835d68 12076
d5b7b3ae
RE
12077/* Record INSN, which will need fixing up to load a value from the
12078 minipool. ADDRESS is the offset of the insn since the start of the
12079 function; LOC is a pointer to the part of the insn which requires
12080 fixing; VALUE is the constant that must be loaded, which is of type
12081 MODE. */
949d79eb 12082static void
e32bac5b
RE
12083push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
12084 enum machine_mode mode, rtx value)
949d79eb 12085{
c7319d87 12086 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
949d79eb 12087
949d79eb
RE
12088 fix->insn = insn;
12089 fix->address = address;
12090 fix->loc = loc;
12091 fix->mode = mode;
d5b7b3ae 12092 fix->fix_size = MINIPOOL_FIX_SIZE (mode);
949d79eb 12093 fix->value = value;
d5b7b3ae
RE
12094 fix->forwards = get_attr_pool_range (insn);
12095 fix->backwards = get_attr_neg_pool_range (insn);
12096 fix->minipool = NULL;
949d79eb
RE
12097
12098 /* If an insn doesn't have a range defined for it, then it isn't
e6d29d15 12099 expecting to be reworked by this code. Better to stop now than
949d79eb 12100 to generate duff assembly code. */
e6d29d15 12101 gcc_assert (fix->forwards || fix->backwards);
949d79eb 12102
34a9f549
PB
12103 /* If an entry requires 8-byte alignment then assume all constant pools
12104 require 4 bytes of padding. Trying to do this later on a per-pool
917f1b7e 12105 basis is awkward because existing pool entries have to be modified. */
88f77cba 12106 if (ARM_DOUBLEWORD_ALIGN && fix->fix_size >= 8)
34a9f549 12107 minipool_pad = 4;
5a9335ef 12108
c263766c 12109 if (dump_file)
d5b7b3ae 12110 {
c263766c 12111 fprintf (dump_file,
d5b7b3ae
RE
12112 ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
12113 GET_MODE_NAME (mode),
f676971a 12114 INSN_UID (insn), (unsigned long) address,
d5b7b3ae 12115 -1 * (long)fix->backwards, (long)fix->forwards);
c263766c
RH
12116 arm_print_value (dump_file, fix->value);
12117 fprintf (dump_file, "\n");
d5b7b3ae
RE
12118 }
12119
6354dc9b 12120 /* Add it to the chain of fixes. */
949d79eb 12121 fix->next = NULL;
f676971a 12122
949d79eb
RE
12123 if (minipool_fix_head != NULL)
12124 minipool_fix_tail->next = fix;
12125 else
12126 minipool_fix_head = fix;
12127
12128 minipool_fix_tail = fix;
12129}
12130
9b901d50 12131/* Return the cost of synthesizing a 64-bit constant VAL inline.
2075b05d
RE
12132 Returns the number of insns needed, or 99 if we don't know how to
12133 do it. */
12134int
12135arm_const_double_inline_cost (rtx val)
b9e8bfda 12136{
9b901d50
RE
12137 rtx lowpart, highpart;
12138 enum machine_mode mode;
e0b92319 12139
9b901d50 12140 mode = GET_MODE (val);
b9e8bfda 12141
9b901d50
RE
12142 if (mode == VOIDmode)
12143 mode = DImode;
12144
12145 gcc_assert (GET_MODE_SIZE (mode) == 8);
e0b92319 12146
9b901d50
RE
12147 lowpart = gen_lowpart (SImode, val);
12148 highpart = gen_highpart_mode (SImode, mode, val);
e0b92319 12149
9b901d50
RE
12150 gcc_assert (GET_CODE (lowpart) == CONST_INT);
12151 gcc_assert (GET_CODE (highpart) == CONST_INT);
12152
12153 return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
2075b05d 12154 NULL_RTX, NULL_RTX, 0, 0)
9b901d50 12155 + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
2075b05d
RE
12156 NULL_RTX, NULL_RTX, 0, 0));
12157}
12158
f3b569ca 12159/* Return true if it is worthwhile to split a 64-bit constant into two
b4a58f80
RE
12160 32-bit operations. This is the case if optimizing for size, or
12161 if we have load delay slots, or if one 32-bit part can be done with
12162 a single data operation. */
12163bool
12164arm_const_double_by_parts (rtx val)
12165{
12166 enum machine_mode mode = GET_MODE (val);
12167 rtx part;
12168
12169 if (optimize_size || arm_ld_sched)
12170 return true;
12171
12172 if (mode == VOIDmode)
12173 mode = DImode;
e0b92319 12174
b4a58f80 12175 part = gen_highpart_mode (SImode, mode, val);
e0b92319 12176
b4a58f80 12177 gcc_assert (GET_CODE (part) == CONST_INT);
e0b92319 12178
b4a58f80
RE
12179 if (const_ok_for_arm (INTVAL (part))
12180 || const_ok_for_arm (~INTVAL (part)))
12181 return true;
e0b92319 12182
b4a58f80 12183 part = gen_lowpart (SImode, val);
e0b92319 12184
b4a58f80 12185 gcc_assert (GET_CODE (part) == CONST_INT);
e0b92319 12186
b4a58f80
RE
12187 if (const_ok_for_arm (INTVAL (part))
12188 || const_ok_for_arm (~INTVAL (part)))
12189 return true;
e0b92319 12190
b4a58f80
RE
12191 return false;
12192}
12193
73160ba9
DJ
12194/* Return true if it is possible to inline both the high and low parts
12195 of a 64-bit constant into 32-bit data processing instructions. */
12196bool
12197arm_const_double_by_immediates (rtx val)
12198{
12199 enum machine_mode mode = GET_MODE (val);
12200 rtx part;
12201
12202 if (mode == VOIDmode)
12203 mode = DImode;
12204
12205 part = gen_highpart_mode (SImode, mode, val);
12206
12207 gcc_assert (GET_CODE (part) == CONST_INT);
12208
12209 if (!const_ok_for_arm (INTVAL (part)))
12210 return false;
12211
12212 part = gen_lowpart (SImode, val);
12213
12214 gcc_assert (GET_CODE (part) == CONST_INT);
12215
12216 if (!const_ok_for_arm (INTVAL (part)))
12217 return false;
12218
12219 return true;
12220}
12221
f0375c66
NC
12222/* Scan INSN and note any of its operands that need fixing.
12223 If DO_PUSHES is false we do not actually push any of the fixups
9b901d50 12224 needed. The function returns TRUE if any fixups were needed/pushed.
f0375c66
NC
12225 This is used by arm_memory_load_p() which needs to know about loads
12226 of constants that will be converted into minipool loads. */
f0375c66 12227static bool
e32bac5b 12228note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
949d79eb 12229{
f0375c66 12230 bool result = false;
949d79eb
RE
12231 int opno;
12232
d5b7b3ae 12233 extract_insn (insn);
949d79eb 12234
5895f793 12235 if (!constrain_operands (1))
949d79eb
RE
12236 fatal_insn_not_found (insn);
12237
8c2a5582
RE
12238 if (recog_data.n_alternatives == 0)
12239 return false;
12240
9b901d50
RE
12241 /* Fill in recog_op_alt with information about the constraints of
12242 this insn. */
949d79eb
RE
12243 preprocess_constraints ();
12244
1ccbefce 12245 for (opno = 0; opno < recog_data.n_operands; opno++)
949d79eb 12246 {
6354dc9b 12247 /* Things we need to fix can only occur in inputs. */
36ab44c7 12248 if (recog_data.operand_type[opno] != OP_IN)
949d79eb
RE
12249 continue;
12250
12251 /* If this alternative is a memory reference, then any mention
12252 of constants in this alternative is really to fool reload
12253 into allowing us to accept one there. We need to fix them up
12254 now so that we output the right code. */
12255 if (recog_op_alt[opno][which_alternative].memory_ok)
12256 {
1ccbefce 12257 rtx op = recog_data.operand[opno];
949d79eb 12258
9b901d50 12259 if (CONSTANT_P (op))
f0375c66
NC
12260 {
12261 if (do_pushes)
12262 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
12263 recog_data.operand_mode[opno], op);
12264 result = true;
12265 }
d5b7b3ae 12266 else if (GET_CODE (op) == MEM
949d79eb
RE
12267 && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
12268 && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
f0375c66
NC
12269 {
12270 if (do_pushes)
244b1afb
RE
12271 {
12272 rtx cop = avoid_constant_pool_reference (op);
12273
12274 /* Casting the address of something to a mode narrower
12275 than a word can cause avoid_constant_pool_reference()
12276 to return the pool reference itself. That's no good to
f676971a 12277 us here. Lets just hope that we can use the
244b1afb
RE
12278 constant pool value directly. */
12279 if (op == cop)
c769a35d 12280 cop = get_pool_constant (XEXP (op, 0));
244b1afb
RE
12281
12282 push_minipool_fix (insn, address,
12283 recog_data.operand_loc[opno],
c769a35d 12284 recog_data.operand_mode[opno], cop);
244b1afb 12285 }
f0375c66
NC
12286
12287 result = true;
12288 }
949d79eb 12289 }
2b835d68 12290 }
f0375c66
NC
12291
12292 return result;
2b835d68
RE
12293}
12294
eef5973d
BS
12295/* Convert instructions to their cc-clobbering variant if possible, since
12296 that allows us to use smaller encodings. */
12297
12298static void
12299thumb2_reorg (void)
12300{
12301 basic_block bb;
12302 regset_head live;
12303
12304 INIT_REG_SET (&live);
12305
12306 /* We are freeing block_for_insn in the toplev to keep compatibility
12307 with old MDEP_REORGS that are not CFG based. Recompute it now. */
12308 compute_bb_for_insn ();
12309 df_analyze ();
12310
12311 FOR_EACH_BB (bb)
12312 {
12313 rtx insn;
c994a0ce 12314
eef5973d
BS
12315 COPY_REG_SET (&live, DF_LR_OUT (bb));
12316 df_simulate_initialize_backwards (bb, &live);
12317 FOR_BB_INSNS_REVERSE (bb, insn)
12318 {
12319 if (NONJUMP_INSN_P (insn)
12320 && !REGNO_REG_SET_P (&live, CC_REGNUM))
12321 {
12322 rtx pat = PATTERN (insn);
12323 if (GET_CODE (pat) == SET
12324 && low_register_operand (XEXP (pat, 0), SImode)
12325 && thumb_16bit_operator (XEXP (pat, 1), SImode)
12326 && low_register_operand (XEXP (XEXP (pat, 1), 0), SImode)
12327 && low_register_operand (XEXP (XEXP (pat, 1), 1), SImode))
12328 {
12329 rtx dst = XEXP (pat, 0);
12330 rtx src = XEXP (pat, 1);
12331 rtx op0 = XEXP (src, 0);
c994a0ce
RE
12332 rtx op1 = (GET_RTX_CLASS (GET_CODE (src)) == RTX_COMM_ARITH
12333 ? XEXP (src, 1) : NULL);
12334
eef5973d
BS
12335 if (rtx_equal_p (dst, op0)
12336 || GET_CODE (src) == PLUS || GET_CODE (src) == MINUS)
12337 {
12338 rtx ccreg = gen_rtx_REG (CCmode, CC_REGNUM);
12339 rtx clobber = gen_rtx_CLOBBER (VOIDmode, ccreg);
f63a6726 12340 rtvec vec = gen_rtvec (2, pat, clobber);
c994a0ce
RE
12341
12342 PATTERN (insn) = gen_rtx_PARALLEL (VOIDmode, vec);
12343 INSN_CODE (insn) = -1;
12344 }
12345 /* We can also handle a commutative operation where the
12346 second operand matches the destination. */
12347 else if (op1 && rtx_equal_p (dst, op1))
12348 {
12349 rtx ccreg = gen_rtx_REG (CCmode, CC_REGNUM);
12350 rtx clobber = gen_rtx_CLOBBER (VOIDmode, ccreg);
12351 rtvec vec;
12352
12353 src = copy_rtx (src);
12354 XEXP (src, 0) = op1;
12355 XEXP (src, 1) = op0;
12356 pat = gen_rtx_SET (VOIDmode, dst, src);
12357 vec = gen_rtvec (2, pat, clobber);
eef5973d
BS
12358 PATTERN (insn) = gen_rtx_PARALLEL (VOIDmode, vec);
12359 INSN_CODE (insn) = -1;
12360 }
12361 }
12362 }
c994a0ce 12363
eef5973d
BS
12364 if (NONDEBUG_INSN_P (insn))
12365 df_simulate_one_insn_backwards (bb, insn, &live);
12366 }
12367 }
c994a0ce 12368
eef5973d
BS
12369 CLEAR_REG_SET (&live);
12370}
12371
18dbd950
RS
12372/* Gcc puts the pool in the wrong place for ARM, since we can only
12373 load addresses a limited distance around the pc. We do some
12374 special munging to move the constant pool values to the correct
12375 point in the code. */
18dbd950 12376static void
e32bac5b 12377arm_reorg (void)
2b835d68
RE
12378{
12379 rtx insn;
d5b7b3ae
RE
12380 HOST_WIDE_INT address = 0;
12381 Mfix * fix;
ad076f4e 12382
eef5973d
BS
12383 if (TARGET_THUMB2)
12384 thumb2_reorg ();
12385
949d79eb 12386 minipool_fix_head = minipool_fix_tail = NULL;
2b835d68 12387
949d79eb
RE
12388 /* The first insn must always be a note, or the code below won't
12389 scan it properly. */
18dbd950 12390 insn = get_insns ();
e6d29d15 12391 gcc_assert (GET_CODE (insn) == NOTE);
34a9f549 12392 minipool_pad = 0;
949d79eb
RE
12393
12394 /* Scan all the insns and record the operands that will need fixing. */
18dbd950 12395 for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
2b835d68 12396 {
9b6b54e2 12397 if (TARGET_CIRRUS_FIX_INVALID_INSNS
f0375c66 12398 && (arm_cirrus_insn_p (insn)
9b6b54e2 12399 || GET_CODE (insn) == JUMP_INSN
f0375c66 12400 || arm_memory_load_p (insn)))
9b6b54e2
NC
12401 cirrus_reorg (insn);
12402
949d79eb 12403 if (GET_CODE (insn) == BARRIER)
d5b7b3ae 12404 push_minipool_barrier (insn, address);
f0375c66 12405 else if (INSN_P (insn))
949d79eb
RE
12406 {
12407 rtx table;
12408
f0375c66 12409 note_invalid_constants (insn, address, true);
949d79eb 12410 address += get_attr_length (insn);
d5b7b3ae 12411
949d79eb
RE
12412 /* If the insn is a vector jump, add the size of the table
12413 and skip the table. */
d5b7b3ae 12414 if ((table = is_jump_table (insn)) != NULL)
2b835d68 12415 {
d5b7b3ae 12416 address += get_jump_table_size (table);
949d79eb
RE
12417 insn = table;
12418 }
12419 }
12420 }
332072db 12421
d5b7b3ae 12422 fix = minipool_fix_head;
f676971a 12423
949d79eb 12424 /* Now scan the fixups and perform the required changes. */
d5b7b3ae 12425 while (fix)
949d79eb 12426 {
d5b7b3ae
RE
12427 Mfix * ftmp;
12428 Mfix * fdel;
12429 Mfix * last_added_fix;
12430 Mfix * last_barrier = NULL;
12431 Mfix * this_fix;
949d79eb
RE
12432
12433 /* Skip any further barriers before the next fix. */
12434 while (fix && GET_CODE (fix->insn) == BARRIER)
12435 fix = fix->next;
12436
d5b7b3ae 12437 /* No more fixes. */
949d79eb
RE
12438 if (fix == NULL)
12439 break;
332072db 12440
d5b7b3ae 12441 last_added_fix = NULL;
2b835d68 12442
d5b7b3ae 12443 for (ftmp = fix; ftmp; ftmp = ftmp->next)
949d79eb 12444 {
949d79eb 12445 if (GET_CODE (ftmp->insn) == BARRIER)
949d79eb 12446 {
d5b7b3ae
RE
12447 if (ftmp->address >= minipool_vector_head->max_address)
12448 break;
2b835d68 12449
d5b7b3ae 12450 last_barrier = ftmp;
2b835d68 12451 }
d5b7b3ae
RE
12452 else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
12453 break;
12454
12455 last_added_fix = ftmp; /* Keep track of the last fix added. */
2b835d68 12456 }
949d79eb 12457
d5b7b3ae
RE
12458 /* If we found a barrier, drop back to that; any fixes that we
12459 could have reached but come after the barrier will now go in
12460 the next mini-pool. */
949d79eb
RE
12461 if (last_barrier != NULL)
12462 {
f676971a 12463 /* Reduce the refcount for those fixes that won't go into this
d5b7b3ae
RE
12464 pool after all. */
12465 for (fdel = last_barrier->next;
12466 fdel && fdel != ftmp;
12467 fdel = fdel->next)
12468 {
12469 fdel->minipool->refcount--;
12470 fdel->minipool = NULL;
12471 }
12472
949d79eb
RE
12473 ftmp = last_barrier;
12474 }
12475 else
2bfa88dc 12476 {
d5b7b3ae
RE
12477 /* ftmp is first fix that we can't fit into this pool and
12478 there no natural barriers that we could use. Insert a
12479 new barrier in the code somewhere between the previous
12480 fix and this one, and arrange to jump around it. */
12481 HOST_WIDE_INT max_address;
12482
12483 /* The last item on the list of fixes must be a barrier, so
12484 we can never run off the end of the list of fixes without
12485 last_barrier being set. */
e6d29d15 12486 gcc_assert (ftmp);
d5b7b3ae
RE
12487
12488 max_address = minipool_vector_head->max_address;
2bfa88dc
RE
12489 /* Check that there isn't another fix that is in range that
12490 we couldn't fit into this pool because the pool was
12491 already too large: we need to put the pool before such an
7a7017bc
PB
12492 instruction. The pool itself may come just after the
12493 fix because create_fix_barrier also allows space for a
12494 jump instruction. */
d5b7b3ae 12495 if (ftmp->address < max_address)
7a7017bc 12496 max_address = ftmp->address + 1;
d5b7b3ae
RE
12497
12498 last_barrier = create_fix_barrier (last_added_fix, max_address);
12499 }
12500
12501 assign_minipool_offsets (last_barrier);
12502
12503 while (ftmp)
12504 {
12505 if (GET_CODE (ftmp->insn) != BARRIER
12506 && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
12507 == NULL))
12508 break;
2bfa88dc 12509
d5b7b3ae 12510 ftmp = ftmp->next;
2bfa88dc 12511 }
949d79eb
RE
12512
12513 /* Scan over the fixes we have identified for this pool, fixing them
12514 up and adding the constants to the pool itself. */
d5b7b3ae 12515 for (this_fix = fix; this_fix && ftmp != this_fix;
949d79eb
RE
12516 this_fix = this_fix->next)
12517 if (GET_CODE (this_fix->insn) != BARRIER)
12518 {
949d79eb 12519 rtx addr
f676971a 12520 = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
949d79eb 12521 minipool_vector_label),
d5b7b3ae 12522 this_fix->minipool->offset);
949d79eb
RE
12523 *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
12524 }
12525
d5b7b3ae 12526 dump_minipool (last_barrier->insn);
949d79eb 12527 fix = ftmp;
2b835d68 12528 }
4b632bf1 12529
949d79eb
RE
12530 /* From now on we must synthesize any constants that we can't handle
12531 directly. This can happen if the RTL gets split during final
12532 instruction generation. */
4b632bf1 12533 after_arm_reorg = 1;
c7319d87
RE
12534
12535 /* Free the minipool memory. */
12536 obstack_free (&minipool_obstack, minipool_startobj);
2b835d68 12537}
cce8749e
CH
12538\f
12539/* Routines to output assembly language. */
12540
f3bb6135 12541/* If the rtx is the correct value then return the string of the number.
ff9940b0 12542 In this way we can ensure that valid double constants are generated even
6354dc9b 12543 when cross compiling. */
cd2b33d0 12544const char *
e32bac5b 12545fp_immediate_constant (rtx x)
ff9940b0
RE
12546{
12547 REAL_VALUE_TYPE r;
12548 int i;
f676971a 12549
9b66ebb1
PB
12550 if (!fp_consts_inited)
12551 init_fp_table ();
f676971a 12552
ff9940b0
RE
12553 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
12554 for (i = 0; i < 8; i++)
9b66ebb1
PB
12555 if (REAL_VALUES_EQUAL (r, values_fp[i]))
12556 return strings_fp[i];
f3bb6135 12557
e6d29d15 12558 gcc_unreachable ();
ff9940b0
RE
12559}
12560
9997d19d 12561/* As for fp_immediate_constant, but value is passed directly, not in rtx. */
cd2b33d0 12562static const char *
e32bac5b 12563fp_const_from_val (REAL_VALUE_TYPE *r)
9997d19d
RE
12564{
12565 int i;
12566
9b66ebb1
PB
12567 if (!fp_consts_inited)
12568 init_fp_table ();
9997d19d
RE
12569
12570 for (i = 0; i < 8; i++)
9b66ebb1
PB
12571 if (REAL_VALUES_EQUAL (*r, values_fp[i]))
12572 return strings_fp[i];
9997d19d 12573
e6d29d15 12574 gcc_unreachable ();
9997d19d 12575}
ff9940b0 12576
cce8749e
CH
12577/* Output the operands of a LDM/STM instruction to STREAM.
12578 MASK is the ARM register set mask of which only bits 0-15 are important.
6d3d9133 12579 REG is the base register, either the frame pointer or the stack pointer,
a15908a4
PB
12580 INSTR is the possibly suffixed load or store instruction.
12581 RFE is nonzero if the instruction should also copy spsr to cpsr. */
b279b20a 12582
d5b7b3ae 12583static void
b279b20a 12584print_multi_reg (FILE *stream, const char *instr, unsigned reg,
a15908a4 12585 unsigned long mask, int rfe)
cce8749e 12586{
b279b20a
NC
12587 unsigned i;
12588 bool not_first = FALSE;
cce8749e 12589
a15908a4 12590 gcc_assert (!rfe || (mask & (1 << PC_REGNUM)));
1d5473cb 12591 fputc ('\t', stream);
dd18ae56 12592 asm_fprintf (stream, instr, reg);
5b3e6663 12593 fputc ('{', stream);
f676971a 12594
d5b7b3ae 12595 for (i = 0; i <= LAST_ARM_REGNUM; i++)
cce8749e
CH
12596 if (mask & (1 << i))
12597 {
12598 if (not_first)
12599 fprintf (stream, ", ");
f676971a 12600
dd18ae56 12601 asm_fprintf (stream, "%r", i);
cce8749e
CH
12602 not_first = TRUE;
12603 }
f3bb6135 12604
a15908a4
PB
12605 if (rfe)
12606 fprintf (stream, "}^\n");
12607 else
12608 fprintf (stream, "}\n");
f3bb6135 12609}
cce8749e 12610
9b66ebb1 12611
8edfc4cc 12612/* Output a FLDMD instruction to STREAM.
9728c9d1
PB
12613 BASE if the register containing the address.
12614 REG and COUNT specify the register range.
8edfc4cc
MS
12615 Extra registers may be added to avoid hardware bugs.
12616
12617 We output FLDMD even for ARMv5 VFP implementations. Although
12618 FLDMD is technically not supported until ARMv6, it is believed
12619 that all VFP implementations support its use in this context. */
9b66ebb1
PB
12620
12621static void
8edfc4cc 12622vfp_output_fldmd (FILE * stream, unsigned int base, int reg, int count)
9b66ebb1
PB
12623{
12624 int i;
12625
9728c9d1
PB
12626 /* Workaround ARM10 VFPr1 bug. */
12627 if (count == 2 && !arm_arch6)
12628 {
12629 if (reg == 15)
12630 reg--;
12631 count++;
12632 }
12633
f1adb0a9
JB
12634 /* FLDMD may not load more than 16 doubleword registers at a time. Split the
12635 load into multiple parts if we have to handle more than 16 registers. */
12636 if (count > 16)
12637 {
12638 vfp_output_fldmd (stream, base, reg, 16);
12639 vfp_output_fldmd (stream, base, reg + 16, count - 16);
12640 return;
12641 }
12642
9b66ebb1 12643 fputc ('\t', stream);
8edfc4cc 12644 asm_fprintf (stream, "fldmfdd\t%r!, {", base);
9b66ebb1 12645
9728c9d1 12646 for (i = reg; i < reg + count; i++)
9b66ebb1 12647 {
9728c9d1 12648 if (i > reg)
9b66ebb1 12649 fputs (", ", stream);
9728c9d1 12650 asm_fprintf (stream, "d%d", i);
9b66ebb1
PB
12651 }
12652 fputs ("}\n", stream);
9728c9d1 12653
9b66ebb1
PB
12654}
12655
12656
12657/* Output the assembly for a store multiple. */
12658
12659const char *
8edfc4cc 12660vfp_output_fstmd (rtx * operands)
9b66ebb1
PB
12661{
12662 char pattern[100];
12663 int p;
12664 int base;
12665 int i;
12666
8edfc4cc 12667 strcpy (pattern, "fstmfdd\t%m0!, {%P1");
9b66ebb1
PB
12668 p = strlen (pattern);
12669
e6d29d15 12670 gcc_assert (GET_CODE (operands[1]) == REG);
9b66ebb1
PB
12671
12672 base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
12673 for (i = 1; i < XVECLEN (operands[2], 0); i++)
12674 {
12675 p += sprintf (&pattern[p], ", d%d", base + i);
12676 }
12677 strcpy (&pattern[p], "}");
12678
12679 output_asm_insn (pattern, operands);
12680 return "";
12681}
12682
12683
9728c9d1
PB
12684/* Emit RTL to save block of VFP register pairs to the stack. Returns the
12685 number of bytes pushed. */
9b66ebb1 12686
9728c9d1 12687static int
8edfc4cc 12688vfp_emit_fstmd (int base_reg, int count)
9b66ebb1
PB
12689{
12690 rtx par;
12691 rtx dwarf;
12692 rtx tmp, reg;
12693 int i;
12694
9728c9d1
PB
12695 /* Workaround ARM10 VFPr1 bug. Data corruption can occur when exactly two
12696 register pairs are stored by a store multiple insn. We avoid this
12697 by pushing an extra pair. */
12698 if (count == 2 && !arm_arch6)
12699 {
12700 if (base_reg == LAST_VFP_REGNUM - 3)
12701 base_reg -= 2;
12702 count++;
12703 }
12704
f1adb0a9
JB
12705 /* FSTMD may not store more than 16 doubleword registers at once. Split
12706 larger stores into multiple parts (up to a maximum of two, in
12707 practice). */
12708 if (count > 16)
12709 {
12710 int saved;
12711 /* NOTE: base_reg is an internal register number, so each D register
12712 counts as 2. */
12713 saved = vfp_emit_fstmd (base_reg + 32, count - 16);
12714 saved += vfp_emit_fstmd (base_reg, 16);
12715 return saved;
12716 }
12717
9b66ebb1
PB
12718 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
12719 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
12720
12721 reg = gen_rtx_REG (DFmode, base_reg);
12722 base_reg += 2;
12723
12724 XVECEXP (par, 0, 0)
12725 = gen_rtx_SET (VOIDmode,
9abf5d7b
RR
12726 gen_frame_mem
12727 (BLKmode,
12728 gen_rtx_PRE_MODIFY (Pmode,
12729 stack_pointer_rtx,
12730 plus_constant
12731 (stack_pointer_rtx,
12732 - (count * 8)))
12733 ),
9b66ebb1
PB
12734 gen_rtx_UNSPEC (BLKmode,
12735 gen_rtvec (1, reg),
12736 UNSPEC_PUSH_MULT));
12737
12738 tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8edfc4cc 12739 plus_constant (stack_pointer_rtx, -(count * 8)));
9b66ebb1
PB
12740 RTX_FRAME_RELATED_P (tmp) = 1;
12741 XVECEXP (dwarf, 0, 0) = tmp;
12742
12743 tmp = gen_rtx_SET (VOIDmode,
31fa16b6 12744 gen_frame_mem (DFmode, stack_pointer_rtx),
9b66ebb1
PB
12745 reg);
12746 RTX_FRAME_RELATED_P (tmp) = 1;
12747 XVECEXP (dwarf, 0, 1) = tmp;
12748
12749 for (i = 1; i < count; i++)
12750 {
12751 reg = gen_rtx_REG (DFmode, base_reg);
12752 base_reg += 2;
12753 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
12754
12755 tmp = gen_rtx_SET (VOIDmode,
31fa16b6 12756 gen_frame_mem (DFmode,
d66437c5
RE
12757 plus_constant (stack_pointer_rtx,
12758 i * 8)),
9b66ebb1
PB
12759 reg);
12760 RTX_FRAME_RELATED_P (tmp) = 1;
12761 XVECEXP (dwarf, 0, i + 1) = tmp;
12762 }
12763
12764 par = emit_insn (par);
bbbbb16a 12765 add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
9728c9d1
PB
12766 RTX_FRAME_RELATED_P (par) = 1;
12767
8edfc4cc 12768 return count * 8;
9b66ebb1
PB
12769}
12770
9403b7f7
RS
12771/* Emit a call instruction with pattern PAT. ADDR is the address of
12772 the call target. */
12773
12774void
12775arm_emit_call_insn (rtx pat, rtx addr)
12776{
12777 rtx insn;
12778
12779 insn = emit_call_insn (pat);
12780
12781 /* The PIC register is live on entry to VxWorks PIC PLT entries.
12782 If the call might use such an entry, add a use of the PIC register
12783 to the instruction's CALL_INSN_FUNCTION_USAGE. */
12784 if (TARGET_VXWORKS_RTP
12785 && flag_pic
12786 && GET_CODE (addr) == SYMBOL_REF
12787 && (SYMBOL_REF_DECL (addr)
12788 ? !targetm.binds_local_p (SYMBOL_REF_DECL (addr))
12789 : !SYMBOL_REF_LOCAL_P (addr)))
12790 {
12791 require_pic_register ();
12792 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), cfun->machine->pic_reg);
12793 }
12794}
9b66ebb1 12795
6354dc9b 12796/* Output a 'call' insn. */
cd2b33d0 12797const char *
e32bac5b 12798output_call (rtx *operands)
cce8749e 12799{
e6d29d15 12800 gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly. */
cce8749e 12801
68d560d4 12802 /* Handle calls to lr using ip (which may be clobbered in subr anyway). */
62b10bbc 12803 if (REGNO (operands[0]) == LR_REGNUM)
cce8749e 12804 {
62b10bbc 12805 operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
1d5473cb 12806 output_asm_insn ("mov%?\t%0, %|lr", operands);
cce8749e 12807 }
f676971a 12808
1d5473cb 12809 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
f676971a 12810
68d560d4 12811 if (TARGET_INTERWORK || arm_arch4t)
da6558fd
NC
12812 output_asm_insn ("bx%?\t%0", operands);
12813 else
12814 output_asm_insn ("mov%?\t%|pc, %0", operands);
f676971a 12815
f3bb6135
RE
12816 return "";
12817}
cce8749e 12818
0986ef45
JB
12819/* Output a 'call' insn that is a reference in memory. This is
12820 disabled for ARMv5 and we prefer a blx instead because otherwise
12821 there's a significant performance overhead. */
cd2b33d0 12822const char *
e32bac5b 12823output_call_mem (rtx *operands)
ff9940b0 12824{
0986ef45
JB
12825 gcc_assert (!arm_arch5);
12826 if (TARGET_INTERWORK)
da6558fd
NC
12827 {
12828 output_asm_insn ("ldr%?\t%|ip, %0", operands);
12829 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
12830 output_asm_insn ("bx%?\t%|ip", operands);
12831 }
6ab5da80
RE
12832 else if (regno_use_in (LR_REGNUM, operands[0]))
12833 {
12834 /* LR is used in the memory address. We load the address in the
12835 first instruction. It's safe to use IP as the target of the
12836 load since the call will kill it anyway. */
12837 output_asm_insn ("ldr%?\t%|ip, %0", operands);
0986ef45
JB
12838 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
12839 if (arm_arch4t)
12840 output_asm_insn ("bx%?\t%|ip", operands);
68d560d4 12841 else
0986ef45 12842 output_asm_insn ("mov%?\t%|pc, %|ip", operands);
6ab5da80 12843 }
da6558fd
NC
12844 else
12845 {
12846 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
12847 output_asm_insn ("ldr%?\t%|pc, %0", operands);
12848 }
12849
f3bb6135
RE
12850 return "";
12851}
ff9940b0
RE
12852
12853
3b684012
RE
12854/* Output a move from arm registers to an fpa registers.
12855 OPERANDS[0] is an fpa register.
ff9940b0 12856 OPERANDS[1] is the first registers of an arm register pair. */
cd2b33d0 12857const char *
e32bac5b 12858output_mov_long_double_fpa_from_arm (rtx *operands)
ff9940b0
RE
12859{
12860 int arm_reg0 = REGNO (operands[1]);
12861 rtx ops[3];
12862
e6d29d15 12863 gcc_assert (arm_reg0 != IP_REGNUM);
f3bb6135 12864
43cffd11
RE
12865 ops[0] = gen_rtx_REG (SImode, arm_reg0);
12866 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
12867 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
f676971a 12868
5b3e6663 12869 output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
1d5473cb 12870 output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
f676971a 12871
f3bb6135
RE
12872 return "";
12873}
ff9940b0 12874
3b684012 12875/* Output a move from an fpa register to arm registers.
ff9940b0 12876 OPERANDS[0] is the first registers of an arm register pair.
3b684012 12877 OPERANDS[1] is an fpa register. */
cd2b33d0 12878const char *
e32bac5b 12879output_mov_long_double_arm_from_fpa (rtx *operands)
ff9940b0
RE
12880{
12881 int arm_reg0 = REGNO (operands[0]);
12882 rtx ops[3];
12883
e6d29d15 12884 gcc_assert (arm_reg0 != IP_REGNUM);
f3bb6135 12885
43cffd11
RE
12886 ops[0] = gen_rtx_REG (SImode, arm_reg0);
12887 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
12888 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
ff9940b0 12889
1d5473cb 12890 output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
5b3e6663 12891 output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
f3bb6135
RE
12892 return "";
12893}
ff9940b0
RE
12894
12895/* Output a move from arm registers to arm registers of a long double
12896 OPERANDS[0] is the destination.
12897 OPERANDS[1] is the source. */
cd2b33d0 12898const char *
e32bac5b 12899output_mov_long_double_arm_from_arm (rtx *operands)
ff9940b0 12900{
6354dc9b 12901 /* We have to be careful here because the two might overlap. */
ff9940b0
RE
12902 int dest_start = REGNO (operands[0]);
12903 int src_start = REGNO (operands[1]);
12904 rtx ops[2];
12905 int i;
12906
12907 if (dest_start < src_start)
12908 {
12909 for (i = 0; i < 3; i++)
12910 {
43cffd11
RE
12911 ops[0] = gen_rtx_REG (SImode, dest_start + i);
12912 ops[1] = gen_rtx_REG (SImode, src_start + i);
9997d19d 12913 output_asm_insn ("mov%?\t%0, %1", ops);
ff9940b0
RE
12914 }
12915 }
12916 else
12917 {
12918 for (i = 2; i >= 0; i--)
12919 {
43cffd11
RE
12920 ops[0] = gen_rtx_REG (SImode, dest_start + i);
12921 ops[1] = gen_rtx_REG (SImode, src_start + i);
9997d19d 12922 output_asm_insn ("mov%?\t%0, %1", ops);
ff9940b0
RE
12923 }
12924 }
f3bb6135 12925
ff9940b0
RE
12926 return "";
12927}
12928
a552b644
RR
12929void
12930arm_emit_movpair (rtx dest, rtx src)
12931 {
12932 /* If the src is an immediate, simplify it. */
12933 if (CONST_INT_P (src))
12934 {
12935 HOST_WIDE_INT val = INTVAL (src);
12936 emit_set_insn (dest, GEN_INT (val & 0x0000ffff));
12937 if ((val >> 16) & 0x0000ffff)
12938 emit_set_insn (gen_rtx_ZERO_EXTRACT (SImode, dest, GEN_INT (16),
12939 GEN_INT (16)),
12940 GEN_INT ((val >> 16) & 0x0000ffff));
12941 return;
12942 }
12943 emit_set_insn (dest, gen_rtx_HIGH (SImode, src));
12944 emit_set_insn (dest, gen_rtx_LO_SUM (SImode, dest, src));
12945 }
571191af 12946
3b684012
RE
12947/* Output a move from arm registers to an fpa registers.
12948 OPERANDS[0] is an fpa register.
cce8749e 12949 OPERANDS[1] is the first registers of an arm register pair. */
cd2b33d0 12950const char *
e32bac5b 12951output_mov_double_fpa_from_arm (rtx *operands)
cce8749e
CH
12952{
12953 int arm_reg0 = REGNO (operands[1]);
12954 rtx ops[2];
12955
e6d29d15 12956 gcc_assert (arm_reg0 != IP_REGNUM);
f676971a 12957
43cffd11
RE
12958 ops[0] = gen_rtx_REG (SImode, arm_reg0);
12959 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
5b3e6663 12960 output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1}", ops);
1d5473cb 12961 output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
f3bb6135
RE
12962 return "";
12963}
cce8749e 12964
3b684012 12965/* Output a move from an fpa register to arm registers.
cce8749e 12966 OPERANDS[0] is the first registers of an arm register pair.
3b684012 12967 OPERANDS[1] is an fpa register. */
cd2b33d0 12968const char *
e32bac5b 12969output_mov_double_arm_from_fpa (rtx *operands)
cce8749e
CH
12970{
12971 int arm_reg0 = REGNO (operands[0]);
12972 rtx ops[2];
12973
e6d29d15 12974 gcc_assert (arm_reg0 != IP_REGNUM);
f3bb6135 12975
43cffd11
RE
12976 ops[0] = gen_rtx_REG (SImode, arm_reg0);
12977 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
1d5473cb 12978 output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
5b3e6663 12979 output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1}", ops);
f3bb6135
RE
12980 return "";
12981}
cce8749e 12982
16a9afdc
JZ
12983/* Output a move between double words. It must be REG<-MEM
12984 or MEM<-REG. */
cd2b33d0 12985const char *
e32bac5b 12986output_move_double (rtx *operands)
cce8749e
CH
12987{
12988 enum rtx_code code0 = GET_CODE (operands[0]);
12989 enum rtx_code code1 = GET_CODE (operands[1]);
56636818 12990 rtx otherops[3];
cce8749e
CH
12991
12992 if (code0 == REG)
12993 {
f0b4bdd5 12994 unsigned int reg0 = REGNO (operands[0]);
cce8749e 12995
43cffd11 12996 otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
f676971a 12997
e6d29d15
NS
12998 gcc_assert (code1 == MEM); /* Constraints should ensure this. */
12999
13000 switch (GET_CODE (XEXP (operands[1], 0)))
cce8749e 13001 {
e6d29d15 13002 case REG:
5fd42423
PB
13003 if (TARGET_LDRD
13004 && !(fix_cm3_ldrd && reg0 == REGNO(XEXP (operands[1], 0))))
5dea0c19
PB
13005 output_asm_insn ("ldr%(d%)\t%0, [%m1]", operands);
13006 else
13007 output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
e6d29d15 13008 break;
e0b92319 13009
e6d29d15
NS
13010 case PRE_INC:
13011 gcc_assert (TARGET_LDRD);
5b3e6663 13012 output_asm_insn ("ldr%(d%)\t%0, [%m1, #8]!", operands);
e6d29d15 13013 break;
e0b92319 13014
e6d29d15 13015 case PRE_DEC:
5b3e6663
PB
13016 if (TARGET_LDRD)
13017 output_asm_insn ("ldr%(d%)\t%0, [%m1, #-8]!", operands);
13018 else
13019 output_asm_insn ("ldm%(db%)\t%m1!, %M0", operands);
e6d29d15 13020 break;
e0b92319 13021
e6d29d15 13022 case POST_INC:
5dea0c19
PB
13023 if (TARGET_LDRD)
13024 output_asm_insn ("ldr%(d%)\t%0, [%m1], #8", operands);
13025 else
13026 output_asm_insn ("ldm%(ia%)\t%m1!, %M0", operands);
e6d29d15 13027 break;
e0b92319 13028
e6d29d15
NS
13029 case POST_DEC:
13030 gcc_assert (TARGET_LDRD);
5b3e6663 13031 output_asm_insn ("ldr%(d%)\t%0, [%m1], #-8", operands);
e6d29d15 13032 break;
e0b92319 13033
e6d29d15
NS
13034 case PRE_MODIFY:
13035 case POST_MODIFY:
5fd42423
PB
13036 /* Autoicrement addressing modes should never have overlapping
13037 base and destination registers, and overlapping index registers
13038 are already prohibited, so this doesn't need to worry about
13039 fix_cm3_ldrd. */
e6d29d15
NS
13040 otherops[0] = operands[0];
13041 otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
13042 otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
e0b92319 13043
e6d29d15 13044 if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
cce8749e 13045 {
e6d29d15 13046 if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
fdd695fd 13047 {
e6d29d15
NS
13048 /* Registers overlap so split out the increment. */
13049 output_asm_insn ("add%?\t%1, %1, %2", otherops);
5b3e6663 13050 output_asm_insn ("ldr%(d%)\t%0, [%1] @split", otherops);
fdd695fd
PB
13051 }
13052 else
fe2d934b 13053 {
ff128632
RE
13054 /* Use a single insn if we can.
13055 FIXME: IWMMXT allows offsets larger than ldrd can
13056 handle, fix these up with a pair of ldr. */
13057 if (TARGET_THUMB2
13058 || GET_CODE (otherops[2]) != CONST_INT
13059 || (INTVAL (otherops[2]) > -256
13060 && INTVAL (otherops[2]) < 256))
13061 output_asm_insn ("ldr%(d%)\t%0, [%1, %2]!", otherops);
13062 else
fe2d934b
PB
13063 {
13064 output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
ff128632 13065 output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
fe2d934b 13066 }
fe2d934b 13067 }
e6d29d15
NS
13068 }
13069 else
13070 {
ff128632
RE
13071 /* Use a single insn if we can.
13072 FIXME: IWMMXT allows offsets larger than ldrd can handle,
fe2d934b 13073 fix these up with a pair of ldr. */
ff128632
RE
13074 if (TARGET_THUMB2
13075 || GET_CODE (otherops[2]) != CONST_INT
13076 || (INTVAL (otherops[2]) > -256
13077 && INTVAL (otherops[2]) < 256))
13078 output_asm_insn ("ldr%(d%)\t%0, [%1], %2", otherops);
13079 else
fe2d934b 13080 {
ff128632 13081 output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
fe2d934b
PB
13082 output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
13083 }
e6d29d15
NS
13084 }
13085 break;
e0b92319 13086
e6d29d15
NS
13087 case LABEL_REF:
13088 case CONST:
5dea0c19
PB
13089 /* We might be able to use ldrd %0, %1 here. However the range is
13090 different to ldr/adr, and it is broken on some ARMv7-M
13091 implementations. */
5fd42423
PB
13092 /* Use the second register of the pair to avoid problematic
13093 overlap. */
13094 otherops[1] = operands[1];
13095 output_asm_insn ("adr%?\t%0, %1", otherops);
13096 operands[1] = otherops[0];
5dea0c19 13097 if (TARGET_LDRD)
5fd42423 13098 output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
5dea0c19 13099 else
5fd42423 13100 output_asm_insn ("ldm%(ia%)\t%1, %M0", operands);
e6d29d15 13101 break;
e0b92319 13102
5b3e6663 13103 /* ??? This needs checking for thumb2. */
e6d29d15
NS
13104 default:
13105 if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
13106 GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
13107 {
13108 otherops[0] = operands[0];
13109 otherops[1] = XEXP (XEXP (operands[1], 0), 0);
13110 otherops[2] = XEXP (XEXP (operands[1], 0), 1);
e0b92319 13111
e6d29d15 13112 if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
fdd695fd 13113 {
5dea0c19 13114 if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
2b835d68 13115 {
e6d29d15 13116 switch ((int) INTVAL (otherops[2]))
2b835d68 13117 {
e6d29d15 13118 case -8:
5b3e6663 13119 output_asm_insn ("ldm%(db%)\t%1, %M0", otherops);
e6d29d15
NS
13120 return "";
13121 case -4:
5b3e6663
PB
13122 if (TARGET_THUMB2)
13123 break;
13124 output_asm_insn ("ldm%(da%)\t%1, %M0", otherops);
e6d29d15
NS
13125 return "";
13126 case 4:
5b3e6663
PB
13127 if (TARGET_THUMB2)
13128 break;
13129 output_asm_insn ("ldm%(ib%)\t%1, %M0", otherops);
e6d29d15 13130 return "";
fdd695fd 13131 }
e6d29d15 13132 }
5fd42423
PB
13133 otherops[0] = gen_rtx_REG(SImode, REGNO(operands[0]) + 1);
13134 operands[1] = otherops[0];
e6d29d15
NS
13135 if (TARGET_LDRD
13136 && (GET_CODE (otherops[2]) == REG
ff128632 13137 || TARGET_THUMB2
e6d29d15
NS
13138 || (GET_CODE (otherops[2]) == CONST_INT
13139 && INTVAL (otherops[2]) > -256
13140 && INTVAL (otherops[2]) < 256)))
13141 {
5fd42423 13142 if (reg_overlap_mentioned_p (operands[0],
e6d29d15 13143 otherops[2]))
fdd695fd 13144 {
5fd42423 13145 rtx tmp;
e6d29d15
NS
13146 /* Swap base and index registers over to
13147 avoid a conflict. */
5fd42423
PB
13148 tmp = otherops[1];
13149 otherops[1] = otherops[2];
13150 otherops[2] = tmp;
fdd695fd 13151 }
e6d29d15
NS
13152 /* If both registers conflict, it will usually
13153 have been fixed by a splitter. */
5fd42423
PB
13154 if (reg_overlap_mentioned_p (operands[0], otherops[2])
13155 || (fix_cm3_ldrd && reg0 == REGNO (otherops[1])))
fdd695fd 13156 {
5fd42423
PB
13157 output_asm_insn ("add%?\t%0, %1, %2", otherops);
13158 output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
2b835d68
RE
13159 }
13160 else
5fd42423
PB
13161 {
13162 otherops[0] = operands[0];
13163 output_asm_insn ("ldr%(d%)\t%0, [%1, %2]", otherops);
13164 }
e6d29d15 13165 return "";
2b835d68 13166 }
e0b92319 13167
e6d29d15 13168 if (GET_CODE (otherops[2]) == CONST_INT)
2b835d68 13169 {
e6d29d15
NS
13170 if (!(const_ok_for_arm (INTVAL (otherops[2]))))
13171 output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
13172 else
13173 output_asm_insn ("add%?\t%0, %1, %2", otherops);
2b835d68
RE
13174 }
13175 else
e6d29d15
NS
13176 output_asm_insn ("add%?\t%0, %1, %2", otherops);
13177 }
13178 else
13179 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
13180
5dea0c19 13181 if (TARGET_LDRD)
5fd42423 13182 return "ldr%(d%)\t%0, [%1]";
5dea0c19 13183
5fd42423 13184 return "ldm%(ia%)\t%1, %M0";
e6d29d15
NS
13185 }
13186 else
13187 {
13188 otherops[1] = adjust_address (operands[1], SImode, 4);
13189 /* Take care of overlapping base/data reg. */
13190 if (reg_mentioned_p (operands[0], operands[1]))
13191 {
13192 output_asm_insn ("ldr%?\t%0, %1", otherops);
13193 output_asm_insn ("ldr%?\t%0, %1", operands);
13194 }
13195 else
13196 {
13197 output_asm_insn ("ldr%?\t%0, %1", operands);
13198 output_asm_insn ("ldr%?\t%0, %1", otherops);
cce8749e
CH
13199 }
13200 }
13201 }
cce8749e 13202 }
e6d29d15 13203 else
cce8749e 13204 {
e6d29d15
NS
13205 /* Constraints should ensure this. */
13206 gcc_assert (code0 == MEM && code1 == REG);
13207 gcc_assert (REGNO (operands[1]) != IP_REGNUM);
2b835d68 13208
ff9940b0
RE
13209 switch (GET_CODE (XEXP (operands[0], 0)))
13210 {
13211 case REG:
5dea0c19
PB
13212 if (TARGET_LDRD)
13213 output_asm_insn ("str%(d%)\t%1, [%m0]", operands);
13214 else
13215 output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
ff9940b0 13216 break;
2b835d68 13217
ff9940b0 13218 case PRE_INC:
e6d29d15 13219 gcc_assert (TARGET_LDRD);
5b3e6663 13220 output_asm_insn ("str%(d%)\t%1, [%m0, #8]!", operands);
ff9940b0 13221 break;
2b835d68 13222
ff9940b0 13223 case PRE_DEC:
5b3e6663
PB
13224 if (TARGET_LDRD)
13225 output_asm_insn ("str%(d%)\t%1, [%m0, #-8]!", operands);
13226 else
13227 output_asm_insn ("stm%(db%)\t%m0!, %M1", operands);
ff9940b0 13228 break;
2b835d68 13229
ff9940b0 13230 case POST_INC:
5dea0c19
PB
13231 if (TARGET_LDRD)
13232 output_asm_insn ("str%(d%)\t%1, [%m0], #8", operands);
13233 else
13234 output_asm_insn ("stm%(ia%)\t%m0!, %M1", operands);
ff9940b0 13235 break;
2b835d68 13236
ff9940b0 13237 case POST_DEC:
e6d29d15 13238 gcc_assert (TARGET_LDRD);
5b3e6663 13239 output_asm_insn ("str%(d%)\t%1, [%m0], #-8", operands);
fdd695fd
PB
13240 break;
13241
13242 case PRE_MODIFY:
13243 case POST_MODIFY:
13244 otherops[0] = operands[1];
13245 otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
13246 otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
13247
fe2d934b
PB
13248 /* IWMMXT allows offsets larger than ldrd can handle,
13249 fix these up with a pair of ldr. */
ff128632
RE
13250 if (!TARGET_THUMB2
13251 && GET_CODE (otherops[2]) == CONST_INT
fe2d934b
PB
13252 && (INTVAL(otherops[2]) <= -256
13253 || INTVAL(otherops[2]) >= 256))
13254 {
fe2d934b
PB
13255 if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
13256 {
8019fcfb
YQ
13257 output_asm_insn ("str%?\t%0, [%1, %2]!", otherops);
13258 output_asm_insn ("str%?\t%H0, [%1, #4]", otherops);
fe2d934b
PB
13259 }
13260 else
13261 {
8019fcfb
YQ
13262 output_asm_insn ("str%?\t%H0, [%1, #4]", otherops);
13263 output_asm_insn ("str%?\t%0, [%1], %2", otherops);
fe2d934b
PB
13264 }
13265 }
13266 else if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
5b3e6663 13267 output_asm_insn ("str%(d%)\t%0, [%1, %2]!", otherops);
fdd695fd 13268 else
5b3e6663 13269 output_asm_insn ("str%(d%)\t%0, [%1], %2", otherops);
ff9940b0 13270 break;
2b835d68
RE
13271
13272 case PLUS:
fdd695fd 13273 otherops[2] = XEXP (XEXP (operands[0], 0), 1);
5dea0c19 13274 if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
2b835d68 13275 {
06bea5aa 13276 switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
2b835d68
RE
13277 {
13278 case -8:
5b3e6663 13279 output_asm_insn ("stm%(db%)\t%m0, %M1", operands);
2b835d68
RE
13280 return "";
13281
13282 case -4:
5b3e6663
PB
13283 if (TARGET_THUMB2)
13284 break;
13285 output_asm_insn ("stm%(da%)\t%m0, %M1", operands);
2b835d68
RE
13286 return "";
13287
13288 case 4:
5b3e6663
PB
13289 if (TARGET_THUMB2)
13290 break;
13291 output_asm_insn ("stm%(ib%)\t%m0, %M1", operands);
2b835d68
RE
13292 return "";
13293 }
13294 }
fdd695fd
PB
13295 if (TARGET_LDRD
13296 && (GET_CODE (otherops[2]) == REG
ff128632 13297 || TARGET_THUMB2
fdd695fd
PB
13298 || (GET_CODE (otherops[2]) == CONST_INT
13299 && INTVAL (otherops[2]) > -256
13300 && INTVAL (otherops[2]) < 256)))
13301 {
13302 otherops[0] = operands[1];
13303 otherops[1] = XEXP (XEXP (operands[0], 0), 0);
5b3e6663 13304 output_asm_insn ("str%(d%)\t%0, [%1, %2]", otherops);
fdd695fd
PB
13305 return "";
13306 }
2b835d68
RE
13307 /* Fall through */
13308
ff9940b0 13309 default:
a4a37b30 13310 otherops[0] = adjust_address (operands[0], SImode, 4);
ff128632 13311 otherops[1] = operands[1];
9997d19d 13312 output_asm_insn ("str%?\t%1, %0", operands);
ff128632 13313 output_asm_insn ("str%?\t%H1, %0", otherops);
cce8749e
CH
13314 }
13315 }
cce8749e 13316
9997d19d
RE
13317 return "";
13318}
cce8749e 13319
88f77cba 13320/* Output a move, load or store for quad-word vectors in ARM registers. Only
dc34db56 13321 handles MEMs accepted by neon_vector_mem_operand with TYPE=1. */
5b3e6663
PB
13322
13323const char *
88f77cba 13324output_move_quad (rtx *operands)
5b3e6663 13325{
88f77cba
JB
13326 if (REG_P (operands[0]))
13327 {
13328 /* Load, or reg->reg move. */
5b3e6663 13329
88f77cba
JB
13330 if (MEM_P (operands[1]))
13331 {
13332 switch (GET_CODE (XEXP (operands[1], 0)))
13333 {
13334 case REG:
13335 output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
13336 break;
13337
13338 case LABEL_REF:
13339 case CONST:
13340 output_asm_insn ("adr%?\t%0, %1", operands);
13341 output_asm_insn ("ldm%(ia%)\t%0, %M0", operands);
13342 break;
13343
13344 default:
13345 gcc_unreachable ();
13346 }
13347 }
13348 else
13349 {
13350 rtx ops[2];
13351 int dest, src, i;
5b3e6663 13352
88f77cba 13353 gcc_assert (REG_P (operands[1]));
5b3e6663 13354
88f77cba
JB
13355 dest = REGNO (operands[0]);
13356 src = REGNO (operands[1]);
5b3e6663 13357
88f77cba
JB
13358 /* This seems pretty dumb, but hopefully GCC won't try to do it
13359 very often. */
13360 if (dest < src)
13361 for (i = 0; i < 4; i++)
13362 {
13363 ops[0] = gen_rtx_REG (SImode, dest + i);
13364 ops[1] = gen_rtx_REG (SImode, src + i);
13365 output_asm_insn ("mov%?\t%0, %1", ops);
13366 }
13367 else
13368 for (i = 3; i >= 0; i--)
13369 {
13370 ops[0] = gen_rtx_REG (SImode, dest + i);
13371 ops[1] = gen_rtx_REG (SImode, src + i);
13372 output_asm_insn ("mov%?\t%0, %1", ops);
13373 }
13374 }
13375 }
13376 else
13377 {
13378 gcc_assert (MEM_P (operands[0]));
13379 gcc_assert (REG_P (operands[1]));
13380 gcc_assert (!reg_overlap_mentioned_p (operands[1], operands[0]));
13381
13382 switch (GET_CODE (XEXP (operands[0], 0)))
13383 {
13384 case REG:
13385 output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
13386 break;
13387
13388 default:
13389 gcc_unreachable ();
13390 }
13391 }
13392
13393 return "";
13394}
13395
13396/* Output a VFP load or store instruction. */
13397
13398const char *
13399output_move_vfp (rtx *operands)
13400{
13401 rtx reg, mem, addr, ops[2];
13402 int load = REG_P (operands[0]);
13403 int dp = GET_MODE_SIZE (GET_MODE (operands[0])) == 8;
13404 int integer_p = GET_MODE_CLASS (GET_MODE (operands[0])) == MODE_INT;
0a2aaacc 13405 const char *templ;
88f77cba
JB
13406 char buff[50];
13407 enum machine_mode mode;
13408
13409 reg = operands[!load];
13410 mem = operands[load];
13411
13412 mode = GET_MODE (reg);
13413
13414 gcc_assert (REG_P (reg));
13415 gcc_assert (IS_VFP_REGNUM (REGNO (reg)));
13416 gcc_assert (mode == SFmode
13417 || mode == DFmode
13418 || mode == SImode
13419 || mode == DImode
13420 || (TARGET_NEON && VALID_NEON_DREG_MODE (mode)));
13421 gcc_assert (MEM_P (mem));
13422
13423 addr = XEXP (mem, 0);
13424
13425 switch (GET_CODE (addr))
13426 {
13427 case PRE_DEC:
0a2aaacc 13428 templ = "f%smdb%c%%?\t%%0!, {%%%s1}%s";
88f77cba
JB
13429 ops[0] = XEXP (addr, 0);
13430 ops[1] = reg;
5b3e6663
PB
13431 break;
13432
13433 case POST_INC:
0a2aaacc 13434 templ = "f%smia%c%%?\t%%0!, {%%%s1}%s";
5b3e6663
PB
13435 ops[0] = XEXP (addr, 0);
13436 ops[1] = reg;
13437 break;
13438
13439 default:
0a2aaacc 13440 templ = "f%s%c%%?\t%%%s0, %%1%s";
5b3e6663
PB
13441 ops[0] = reg;
13442 ops[1] = mem;
13443 break;
13444 }
13445
0a2aaacc 13446 sprintf (buff, templ,
5b3e6663
PB
13447 load ? "ld" : "st",
13448 dp ? 'd' : 's',
13449 dp ? "P" : "",
13450 integer_p ? "\t%@ int" : "");
13451 output_asm_insn (buff, ops);
13452
13453 return "";
13454}
13455
88f77cba 13456/* Output a Neon quad-word load or store, or a load or store for
874d42b9 13457 larger structure modes.
88f77cba 13458
874d42b9
JM
13459 WARNING: The ordering of elements is weird in big-endian mode,
13460 because we use VSTM, as required by the EABI. GCC RTL defines
13461 element ordering based on in-memory order. This can be differ
13462 from the architectural ordering of elements within a NEON register.
13463 The intrinsics defined in arm_neon.h use the NEON register element
13464 ordering, not the GCC RTL element ordering.
88f77cba 13465
874d42b9
JM
13466 For example, the in-memory ordering of a big-endian a quadword
13467 vector with 16-bit elements when stored from register pair {d0,d1}
13468 will be (lowest address first, d0[N] is NEON register element N):
88f77cba 13469
874d42b9 13470 [d0[3], d0[2], d0[1], d0[0], d1[7], d1[6], d1[5], d1[4]]
88f77cba 13471
874d42b9
JM
13472 When necessary, quadword registers (dN, dN+1) are moved to ARM
13473 registers from rN in the order:
88f77cba
JB
13474
13475 dN -> (rN+1, rN), dN+1 -> (rN+3, rN+2)
13476
874d42b9
JM
13477 So that STM/LDM can be used on vectors in ARM registers, and the
13478 same memory layout will result as if VSTM/VLDM were used. */
88f77cba
JB
13479
13480const char *
13481output_move_neon (rtx *operands)
13482{
13483 rtx reg, mem, addr, ops[2];
13484 int regno, load = REG_P (operands[0]);
0a2aaacc 13485 const char *templ;
88f77cba
JB
13486 char buff[50];
13487 enum machine_mode mode;
13488
13489 reg = operands[!load];
13490 mem = operands[load];
13491
13492 mode = GET_MODE (reg);
13493
13494 gcc_assert (REG_P (reg));
13495 regno = REGNO (reg);
13496 gcc_assert (VFP_REGNO_OK_FOR_DOUBLE (regno)
13497 || NEON_REGNO_OK_FOR_QUAD (regno));
13498 gcc_assert (VALID_NEON_DREG_MODE (mode)
13499 || VALID_NEON_QREG_MODE (mode)
13500 || VALID_NEON_STRUCT_MODE (mode));
13501 gcc_assert (MEM_P (mem));
13502
13503 addr = XEXP (mem, 0);
13504
13505 /* Strip off const from addresses like (const (plus (...))). */
13506 if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
13507 addr = XEXP (addr, 0);
13508
13509 switch (GET_CODE (addr))
13510 {
13511 case POST_INC:
0a2aaacc 13512 templ = "v%smia%%?\t%%0!, %%h1";
88f77cba
JB
13513 ops[0] = XEXP (addr, 0);
13514 ops[1] = reg;
13515 break;
13516
dc34db56
PB
13517 case PRE_DEC:
13518 /* FIXME: We should be using vld1/vst1 here in BE mode? */
13519 templ = "v%smdb%%?\t%%0!, %%h1";
13520 ops[0] = XEXP (addr, 0);
13521 ops[1] = reg;
13522 break;
13523
88f77cba
JB
13524 case POST_MODIFY:
13525 /* FIXME: Not currently enabled in neon_vector_mem_operand. */
13526 gcc_unreachable ();
13527
13528 case LABEL_REF:
13529 case PLUS:
13530 {
13531 int nregs = HARD_REGNO_NREGS (REGNO (reg), mode) / 2;
13532 int i;
13533 int overlap = -1;
13534 for (i = 0; i < nregs; i++)
13535 {
13536 /* We're only using DImode here because it's a convenient size. */
13537 ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * i);
5728868b 13538 ops[1] = adjust_address (mem, DImode, 8 * i);
88f77cba
JB
13539 if (reg_overlap_mentioned_p (ops[0], mem))
13540 {
13541 gcc_assert (overlap == -1);
13542 overlap = i;
13543 }
13544 else
13545 {
13546 sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
13547 output_asm_insn (buff, ops);
13548 }
13549 }
13550 if (overlap != -1)
13551 {
13552 ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * overlap);
13553 ops[1] = adjust_address (mem, SImode, 8 * overlap);
13554 sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
13555 output_asm_insn (buff, ops);
13556 }
13557
13558 return "";
13559 }
13560
13561 default:
0a2aaacc 13562 templ = "v%smia%%?\t%%m0, %%h1";
88f77cba
JB
13563 ops[0] = mem;
13564 ops[1] = reg;
13565 }
13566
0a2aaacc 13567 sprintf (buff, templ, load ? "ld" : "st");
88f77cba
JB
13568 output_asm_insn (buff, ops);
13569
13570 return "";
13571}
13572
7c4f0041
JZ
13573/* Compute and return the length of neon_mov<mode>, where <mode> is
13574 one of VSTRUCT modes: EI, OI, CI or XI. */
13575int
13576arm_attr_length_move_neon (rtx insn)
13577{
13578 rtx reg, mem, addr;
e4dde839 13579 int load;
7c4f0041
JZ
13580 enum machine_mode mode;
13581
13582 extract_insn_cached (insn);
13583
13584 if (REG_P (recog_data.operand[0]) && REG_P (recog_data.operand[1]))
13585 {
13586 mode = GET_MODE (recog_data.operand[0]);
13587 switch (mode)
13588 {
13589 case EImode:
13590 case OImode:
13591 return 8;
13592 case CImode:
13593 return 12;
13594 case XImode:
13595 return 16;
13596 default:
13597 gcc_unreachable ();
13598 }
13599 }
13600
13601 load = REG_P (recog_data.operand[0]);
13602 reg = recog_data.operand[!load];
13603 mem = recog_data.operand[load];
13604
13605 gcc_assert (MEM_P (mem));
13606
13607 mode = GET_MODE (reg);
7c4f0041
JZ
13608 addr = XEXP (mem, 0);
13609
13610 /* Strip off const from addresses like (const (plus (...))). */
13611 if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
13612 addr = XEXP (addr, 0);
13613
13614 if (GET_CODE (addr) == LABEL_REF || GET_CODE (addr) == PLUS)
13615 {
13616 int insns = HARD_REGNO_NREGS (REGNO (reg), mode) / 2;
13617 return insns * 4;
13618 }
13619 else
13620 return 4;
13621}
13622
47d8f18d
JZ
13623/* Return nonzero if the offset in the address is an immediate. Otherwise,
13624 return zero. */
13625
13626int
13627arm_address_offset_is_imm (rtx insn)
13628{
13629 rtx mem, addr;
13630
13631 extract_insn_cached (insn);
13632
13633 if (REG_P (recog_data.operand[0]))
13634 return 0;
13635
13636 mem = recog_data.operand[0];
13637
13638 gcc_assert (MEM_P (mem));
13639
13640 addr = XEXP (mem, 0);
13641
13642 if (GET_CODE (addr) == REG
13643 || (GET_CODE (addr) == PLUS
13644 && GET_CODE (XEXP (addr, 0)) == REG
13645 && GET_CODE (XEXP (addr, 1)) == CONST_INT))
13646 return 1;
13647 else
13648 return 0;
13649}
13650
1d6e90ac
NC
13651/* Output an ADD r, s, #n where n may be too big for one instruction.
13652 If adding zero to one register, output nothing. */
cd2b33d0 13653const char *
e32bac5b 13654output_add_immediate (rtx *operands)
cce8749e 13655{
f3bb6135 13656 HOST_WIDE_INT n = INTVAL (operands[2]);
cce8749e
CH
13657
13658 if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
13659 {
13660 if (n < 0)
13661 output_multi_immediate (operands,
9997d19d
RE
13662 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
13663 -n);
cce8749e
CH
13664 else
13665 output_multi_immediate (operands,
9997d19d
RE
13666 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
13667 n);
cce8749e 13668 }
f3bb6135
RE
13669
13670 return "";
13671}
cce8749e 13672
cce8749e
CH
13673/* Output a multiple immediate operation.
13674 OPERANDS is the vector of operands referred to in the output patterns.
13675 INSTR1 is the output pattern to use for the first constant.
13676 INSTR2 is the output pattern to use for subsequent constants.
13677 IMMED_OP is the index of the constant slot in OPERANDS.
13678 N is the constant value. */
cd2b33d0 13679static const char *
e32bac5b
RE
13680output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
13681 int immed_op, HOST_WIDE_INT n)
cce8749e 13682{
f3bb6135 13683#if HOST_BITS_PER_WIDE_INT > 32
30cf4896 13684 n &= 0xffffffff;
f3bb6135
RE
13685#endif
13686
cce8749e
CH
13687 if (n == 0)
13688 {
1d6e90ac 13689 /* Quick and easy output. */
cce8749e 13690 operands[immed_op] = const0_rtx;
1d6e90ac 13691 output_asm_insn (instr1, operands);
cce8749e
CH
13692 }
13693 else
13694 {
13695 int i;
cd2b33d0 13696 const char * instr = instr1;
cce8749e 13697
6354dc9b 13698 /* Note that n is never zero here (which would give no output). */
cce8749e
CH
13699 for (i = 0; i < 32; i += 2)
13700 {
13701 if (n & (3 << i))
13702 {
f3bb6135
RE
13703 operands[immed_op] = GEN_INT (n & (255 << i));
13704 output_asm_insn (instr, operands);
cce8749e
CH
13705 instr = instr2;
13706 i += 6;
13707 }
13708 }
13709 }
f676971a 13710
f3bb6135 13711 return "";
9997d19d 13712}
cce8749e 13713
5b3e6663
PB
13714/* Return the name of a shifter operation. */
13715static const char *
13716arm_shift_nmem(enum rtx_code code)
13717{
13718 switch (code)
13719 {
13720 case ASHIFT:
13721 return ARM_LSL_NAME;
13722
13723 case ASHIFTRT:
13724 return "asr";
13725
13726 case LSHIFTRT:
13727 return "lsr";
13728
13729 case ROTATERT:
13730 return "ror";
13731
13732 default:
13733 abort();
13734 }
13735}
13736
cce8749e
CH
13737/* Return the appropriate ARM instruction for the operation code.
13738 The returned result should not be overwritten. OP is the rtx of the
13739 operation. SHIFT_FIRST_ARG is TRUE if the first argument of the operator
13740 was shifted. */
cd2b33d0 13741const char *
e32bac5b 13742arithmetic_instr (rtx op, int shift_first_arg)
cce8749e 13743{
9997d19d 13744 switch (GET_CODE (op))
cce8749e
CH
13745 {
13746 case PLUS:
f3bb6135
RE
13747 return "add";
13748
cce8749e 13749 case MINUS:
f3bb6135
RE
13750 return shift_first_arg ? "rsb" : "sub";
13751
cce8749e 13752 case IOR:
f3bb6135
RE
13753 return "orr";
13754
cce8749e 13755 case XOR:
f3bb6135
RE
13756 return "eor";
13757
cce8749e 13758 case AND:
f3bb6135
RE
13759 return "and";
13760
5b3e6663
PB
13761 case ASHIFT:
13762 case ASHIFTRT:
13763 case LSHIFTRT:
13764 case ROTATERT:
13765 return arm_shift_nmem(GET_CODE(op));
13766
cce8749e 13767 default:
e6d29d15 13768 gcc_unreachable ();
cce8749e 13769 }
f3bb6135 13770}
cce8749e 13771
cce8749e
CH
13772/* Ensure valid constant shifts and return the appropriate shift mnemonic
13773 for the operation code. The returned result should not be overwritten.
13774 OP is the rtx code of the shift.
9997d19d 13775 On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
6354dc9b 13776 shift. */
cd2b33d0 13777static const char *
e32bac5b 13778shift_op (rtx op, HOST_WIDE_INT *amountp)
cce8749e 13779{
cd2b33d0 13780 const char * mnem;
e2c671ba 13781 enum rtx_code code = GET_CODE (op);
cce8749e 13782
e6d29d15
NS
13783 switch (GET_CODE (XEXP (op, 1)))
13784 {
13785 case REG:
13786 case SUBREG:
13787 *amountp = -1;
13788 break;
13789
13790 case CONST_INT:
13791 *amountp = INTVAL (XEXP (op, 1));
13792 break;
13793
13794 default:
13795 gcc_unreachable ();
13796 }
9997d19d 13797
e2c671ba 13798 switch (code)
cce8749e 13799 {
a657c98a 13800 case ROTATE:
e6d29d15 13801 gcc_assert (*amountp != -1);
a657c98a 13802 *amountp = 32 - *amountp;
5b3e6663 13803 code = ROTATERT;
a657c98a
RE
13804
13805 /* Fall through. */
13806
5b3e6663
PB
13807 case ASHIFT:
13808 case ASHIFTRT:
13809 case LSHIFTRT:
9997d19d 13810 case ROTATERT:
5b3e6663 13811 mnem = arm_shift_nmem(code);
9997d19d
RE
13812 break;
13813
ff9940b0 13814 case MULT:
e2c671ba
RE
13815 /* We never have to worry about the amount being other than a
13816 power of 2, since this case can never be reloaded from a reg. */
e6d29d15
NS
13817 gcc_assert (*amountp != -1);
13818 *amountp = int_log2 (*amountp);
5b3e6663 13819 return ARM_LSL_NAME;
f3bb6135 13820
cce8749e 13821 default:
e6d29d15 13822 gcc_unreachable ();
cce8749e
CH
13823 }
13824
e2c671ba
RE
13825 if (*amountp != -1)
13826 {
13827 /* This is not 100% correct, but follows from the desire to merge
13828 multiplication by a power of 2 with the recognizer for a
5b3e6663 13829 shift. >=32 is not a valid shift for "lsl", so we must try and
e2c671ba 13830 output a shift that produces the correct arithmetical result.
ddd5a7c1 13831 Using lsr #32 is identical except for the fact that the carry bit
f676971a 13832 is not set correctly if we set the flags; but we never use the
e2c671ba
RE
13833 carry bit from such an operation, so we can ignore that. */
13834 if (code == ROTATERT)
1d6e90ac
NC
13835 /* Rotate is just modulo 32. */
13836 *amountp &= 31;
e2c671ba
RE
13837 else if (*amountp != (*amountp & 31))
13838 {
13839 if (code == ASHIFT)
13840 mnem = "lsr";
13841 *amountp = 32;
13842 }
13843
13844 /* Shifts of 0 are no-ops. */
13845 if (*amountp == 0)
13846 return NULL;
f676971a 13847 }
e2c671ba 13848
9997d19d
RE
13849 return mnem;
13850}
cce8749e 13851
6354dc9b 13852/* Obtain the shift from the POWER of two. */
1d6e90ac 13853
18af7313 13854static HOST_WIDE_INT
e32bac5b 13855int_log2 (HOST_WIDE_INT power)
cce8749e 13856{
f3bb6135 13857 HOST_WIDE_INT shift = 0;
cce8749e 13858
30cf4896 13859 while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
cce8749e 13860 {
e6d29d15 13861 gcc_assert (shift <= 31);
e32bac5b 13862 shift++;
cce8749e 13863 }
f3bb6135
RE
13864
13865 return shift;
13866}
cce8749e 13867
c5ff069d
ZW
13868/* Output a .ascii pseudo-op, keeping track of lengths. This is
13869 because /bin/as is horribly restrictive. The judgement about
13870 whether or not each character is 'printable' (and can be output as
13871 is) or not (and must be printed with an octal escape) must be made
13872 with reference to the *host* character set -- the situation is
13873 similar to that discussed in the comments above pp_c_char in
13874 c-pretty-print.c. */
13875
6cfc7210 13876#define MAX_ASCII_LEN 51
cce8749e
CH
13877
13878void
e32bac5b 13879output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
cce8749e
CH
13880{
13881 int i;
6cfc7210 13882 int len_so_far = 0;
cce8749e 13883
6cfc7210 13884 fputs ("\t.ascii\t\"", stream);
f676971a 13885
cce8749e
CH
13886 for (i = 0; i < len; i++)
13887 {
1d6e90ac 13888 int c = p[i];
cce8749e 13889
6cfc7210 13890 if (len_so_far >= MAX_ASCII_LEN)
cce8749e 13891 {
6cfc7210 13892 fputs ("\"\n\t.ascii\t\"", stream);
cce8749e 13893 len_so_far = 0;
cce8749e
CH
13894 }
13895
c5ff069d 13896 if (ISPRINT (c))
cce8749e 13897 {
c5ff069d 13898 if (c == '\\' || c == '\"')
6cfc7210 13899 {
c5ff069d 13900 putc ('\\', stream);
5895f793 13901 len_so_far++;
6cfc7210 13902 }
c5ff069d
ZW
13903 putc (c, stream);
13904 len_so_far++;
13905 }
13906 else
13907 {
13908 fprintf (stream, "\\%03o", c);
13909 len_so_far += 4;
cce8749e 13910 }
cce8749e 13911 }
f3bb6135 13912
cce8749e 13913 fputs ("\"\n", stream);
f3bb6135 13914}
cce8749e 13915\f
c9ca9b88 13916/* Compute the register save mask for registers 0 through 12
5848830f 13917 inclusive. This code is used by arm_compute_save_reg_mask. */
b279b20a 13918
6d3d9133 13919static unsigned long
e32bac5b 13920arm_compute_save_reg0_reg12_mask (void)
6d3d9133 13921{
121308d4 13922 unsigned long func_type = arm_current_func_type ();
b279b20a 13923 unsigned long save_reg_mask = 0;
6d3d9133 13924 unsigned int reg;
6d3d9133 13925
7b8b8ade 13926 if (IS_INTERRUPT (func_type))
6d3d9133 13927 {
7b8b8ade 13928 unsigned int max_reg;
7b8b8ade
NC
13929 /* Interrupt functions must not corrupt any registers,
13930 even call clobbered ones. If this is a leaf function
13931 we can just examine the registers used by the RTL, but
13932 otherwise we have to assume that whatever function is
13933 called might clobber anything, and so we have to save
13934 all the call-clobbered registers as well. */
13935 if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
13936 /* FIQ handlers have registers r8 - r12 banked, so
13937 we only need to check r0 - r7, Normal ISRs only
121308d4 13938 bank r14 and r15, so we must check up to r12.
7b8b8ade
NC
13939 r13 is the stack pointer which is always preserved,
13940 so we do not need to consider it here. */
13941 max_reg = 7;
13942 else
13943 max_reg = 12;
f676971a 13944
7b8b8ade 13945 for (reg = 0; reg <= max_reg; reg++)
6fb5fa3c
DB
13946 if (df_regs_ever_live_p (reg)
13947 || (! current_function_is_leaf && call_used_regs[reg]))
6d3d9133 13948 save_reg_mask |= (1 << reg);
cfa01aab 13949
286d28c3 13950 /* Also save the pic base register if necessary. */
cfa01aab
PB
13951 if (flag_pic
13952 && !TARGET_SINGLE_PIC_BASE
020a4035 13953 && arm_pic_register != INVALID_REGNUM
e3b5732b 13954 && crtl->uses_pic_offset_table)
cfa01aab 13955 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
6d3d9133 13956 }
1586899e
PB
13957 else if (IS_VOLATILE(func_type))
13958 {
13959 /* For noreturn functions we historically omitted register saves
13960 altogether. However this really messes up debugging. As a
3ed04dbd 13961 compromise save just the frame pointers. Combined with the link
1586899e
PB
13962 register saved elsewhere this should be sufficient to get
13963 a backtrace. */
13964 if (frame_pointer_needed)
13965 save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
13966 if (df_regs_ever_live_p (ARM_HARD_FRAME_POINTER_REGNUM))
13967 save_reg_mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
13968 if (df_regs_ever_live_p (THUMB_HARD_FRAME_POINTER_REGNUM))
13969 save_reg_mask |= 1 << THUMB_HARD_FRAME_POINTER_REGNUM;
13970 }
6d3d9133
NC
13971 else
13972 {
13973 /* In the normal case we only need to save those registers
13974 which are call saved and which are used by this function. */
ec6237e4 13975 for (reg = 0; reg <= 11; reg++)
6fb5fa3c 13976 if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
6d3d9133
NC
13977 save_reg_mask |= (1 << reg);
13978
13979 /* Handle the frame pointer as a special case. */
ec6237e4 13980 if (frame_pointer_needed)
6d3d9133
NC
13981 save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
13982
13983 /* If we aren't loading the PIC register,
13984 don't stack it even though it may be live. */
13985 if (flag_pic
e0b92319 13986 && !TARGET_SINGLE_PIC_BASE
020a4035 13987 && arm_pic_register != INVALID_REGNUM
6fb5fa3c 13988 && (df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM)
e3b5732b 13989 || crtl->uses_pic_offset_table))
6d3d9133 13990 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
5b3e6663
PB
13991
13992 /* The prologue will copy SP into R0, so save it. */
13993 if (IS_STACKALIGN (func_type))
13994 save_reg_mask |= 1;
6d3d9133
NC
13995 }
13996
c9ca9b88 13997 /* Save registers so the exception handler can modify them. */
e3b5732b 13998 if (crtl->calls_eh_return)
c9ca9b88
PB
13999 {
14000 unsigned int i;
f676971a 14001
c9ca9b88
PB
14002 for (i = 0; ; i++)
14003 {
14004 reg = EH_RETURN_DATA_REGNO (i);
14005 if (reg == INVALID_REGNUM)
14006 break;
14007 save_reg_mask |= 1 << reg;
14008 }
14009 }
14010
121308d4
NC
14011 return save_reg_mask;
14012}
14013
5b3e6663 14014
35596784
AJ
14015/* Compute the number of bytes used to store the static chain register on the
14016 stack, above the stack frame. We need to know this accurately to get the
14017 alignment of the rest of the stack frame correct. */
14018
14019static int arm_compute_static_chain_stack_bytes (void)
14020{
14021 unsigned long func_type = arm_current_func_type ();
14022 int static_chain_stack_bytes = 0;
14023
14024 if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM &&
14025 IS_NESTED (func_type) &&
14026 df_regs_ever_live_p (3) && crtl->args.pretend_args_size == 0)
14027 static_chain_stack_bytes = 4;
14028
14029 return static_chain_stack_bytes;
14030}
14031
14032
121308d4 14033/* Compute a bit mask of which registers need to be
954954d1
PB
14034 saved on the stack for the current function.
14035 This is used by arm_get_frame_offsets, which may add extra registers. */
121308d4
NC
14036
14037static unsigned long
e32bac5b 14038arm_compute_save_reg_mask (void)
121308d4
NC
14039{
14040 unsigned int save_reg_mask = 0;
14041 unsigned long func_type = arm_current_func_type ();
5b3e6663 14042 unsigned int reg;
121308d4
NC
14043
14044 if (IS_NAKED (func_type))
14045 /* This should never really happen. */
14046 return 0;
14047
14048 /* If we are creating a stack frame, then we must save the frame pointer,
14049 IP (which will hold the old stack pointer), LR and the PC. */
ec6237e4 14050 if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
121308d4
NC
14051 save_reg_mask |=
14052 (1 << ARM_HARD_FRAME_POINTER_REGNUM)
14053 | (1 << IP_REGNUM)
14054 | (1 << LR_REGNUM)
14055 | (1 << PC_REGNUM);
14056
121308d4
NC
14057 save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
14058
6d3d9133
NC
14059 /* Decide if we need to save the link register.
14060 Interrupt routines have their own banked link register,
14061 so they never need to save it.
1768c26f 14062 Otherwise if we do not use the link register we do not need to save
6d3d9133
NC
14063 it. If we are pushing other registers onto the stack however, we
14064 can save an instruction in the epilogue by pushing the link register
14065 now and then popping it back into the PC. This incurs extra memory
72ac76be 14066 accesses though, so we only do it when optimizing for size, and only
6d3d9133 14067 if we know that we will not need a fancy return sequence. */
6fb5fa3c
DB
14068 if (df_regs_ever_live_p (LR_REGNUM)
14069 || (save_reg_mask
14070 && optimize_size
14071 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
e3b5732b 14072 && !crtl->calls_eh_return))
6d3d9133
NC
14073 save_reg_mask |= 1 << LR_REGNUM;
14074
6f7ebcbb
NC
14075 if (cfun->machine->lr_save_eliminated)
14076 save_reg_mask &= ~ (1 << LR_REGNUM);
14077
5a9335ef
NC
14078 if (TARGET_REALLY_IWMMXT
14079 && ((bit_count (save_reg_mask)
35596784
AJ
14080 + ARM_NUM_INTS (crtl->args.pretend_args_size +
14081 arm_compute_static_chain_stack_bytes())
14082 ) % 2) != 0)
5a9335ef 14083 {
5a9335ef
NC
14084 /* The total number of registers that are going to be pushed
14085 onto the stack is odd. We need to ensure that the stack
14086 is 64-bit aligned before we start to save iWMMXt registers,
14087 and also before we start to create locals. (A local variable
14088 might be a double or long long which we will load/store using
14089 an iWMMXt instruction). Therefore we need to push another
14090 ARM register, so that the stack will be 64-bit aligned. We
14091 try to avoid using the arg registers (r0 -r3) as they might be
14092 used to pass values in a tail call. */
14093 for (reg = 4; reg <= 12; reg++)
14094 if ((save_reg_mask & (1 << reg)) == 0)
14095 break;
14096
14097 if (reg <= 12)
14098 save_reg_mask |= (1 << reg);
14099 else
14100 {
14101 cfun->machine->sibcall_blocked = 1;
14102 save_reg_mask |= (1 << 3);
14103 }
14104 }
14105
5b3e6663
PB
14106 /* We may need to push an additional register for use initializing the
14107 PIC base register. */
14108 if (TARGET_THUMB2 && IS_NESTED (func_type) && flag_pic
14109 && (save_reg_mask & THUMB2_WORK_REGS) == 0)
14110 {
14111 reg = thumb_find_work_register (1 << 4);
14112 if (!call_used_regs[reg])
14113 save_reg_mask |= (1 << reg);
14114 }
14115
6d3d9133
NC
14116 return save_reg_mask;
14117}
14118
9728c9d1 14119
57934c39
PB
14120/* Compute a bit mask of which registers need to be
14121 saved on the stack for the current function. */
14122static unsigned long
5b3e6663 14123thumb1_compute_save_reg_mask (void)
57934c39
PB
14124{
14125 unsigned long mask;
b279b20a 14126 unsigned reg;
57934c39
PB
14127
14128 mask = 0;
14129 for (reg = 0; reg < 12; reg ++)
6fb5fa3c 14130 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
b279b20a 14131 mask |= 1 << reg;
57934c39 14132
39c39be0
RE
14133 if (flag_pic
14134 && !TARGET_SINGLE_PIC_BASE
020a4035 14135 && arm_pic_register != INVALID_REGNUM
e3b5732b 14136 && crtl->uses_pic_offset_table)
39c39be0 14137 mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
b279b20a 14138
a2503645
RS
14139 /* See if we might need r11 for calls to _interwork_r11_call_via_rN(). */
14140 if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
14141 mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
57934c39 14142
b279b20a 14143 /* LR will also be pushed if any lo regs are pushed. */
57934c39
PB
14144 if (mask & 0xff || thumb_force_lr_save ())
14145 mask |= (1 << LR_REGNUM);
14146
b279b20a
NC
14147 /* Make sure we have a low work register if we need one.
14148 We will need one if we are going to push a high register,
14149 but we are not currently intending to push a low register. */
14150 if ((mask & 0xff) == 0
57934c39 14151 && ((mask & 0x0f00) || TARGET_BACKTRACE))
b279b20a
NC
14152 {
14153 /* Use thumb_find_work_register to choose which register
14154 we will use. If the register is live then we will
14155 have to push it. Use LAST_LO_REGNUM as our fallback
14156 choice for the register to select. */
14157 reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
19e723f4
PB
14158 /* Make sure the register returned by thumb_find_work_register is
14159 not part of the return value. */
954954d1 14160 if (reg * UNITS_PER_WORD <= (unsigned) arm_size_return_regs ())
19e723f4 14161 reg = LAST_LO_REGNUM;
b279b20a
NC
14162
14163 if (! call_used_regs[reg])
14164 mask |= 1 << reg;
14165 }
57934c39 14166
35596784
AJ
14167 /* The 504 below is 8 bytes less than 512 because there are two possible
14168 alignment words. We can't tell here if they will be present or not so we
14169 have to play it safe and assume that they are. */
14170 if ((CALLER_INTERWORKING_SLOT_SIZE +
14171 ROUND_UP_WORD (get_frame_size ()) +
14172 crtl->outgoing_args_size) >= 504)
14173 {
14174 /* This is the same as the code in thumb1_expand_prologue() which
14175 determines which register to use for stack decrement. */
14176 for (reg = LAST_ARG_REGNUM + 1; reg <= LAST_LO_REGNUM; reg++)
14177 if (mask & (1 << reg))
14178 break;
14179
14180 if (reg > LAST_LO_REGNUM)
14181 {
14182 /* Make sure we have a register available for stack decrement. */
14183 mask |= 1 << LAST_LO_REGNUM;
14184 }
14185 }
14186
57934c39
PB
14187 return mask;
14188}
14189
14190
9728c9d1
PB
14191/* Return the number of bytes required to save VFP registers. */
14192static int
14193arm_get_vfp_saved_size (void)
14194{
14195 unsigned int regno;
14196 int count;
14197 int saved;
14198
14199 saved = 0;
14200 /* Space for saved VFP registers. */
14201 if (TARGET_HARD_FLOAT && TARGET_VFP)
14202 {
14203 count = 0;
14204 for (regno = FIRST_VFP_REGNUM;
14205 regno < LAST_VFP_REGNUM;
14206 regno += 2)
14207 {
6fb5fa3c
DB
14208 if ((!df_regs_ever_live_p (regno) || call_used_regs[regno])
14209 && (!df_regs_ever_live_p (regno + 1) || call_used_regs[regno + 1]))
9728c9d1
PB
14210 {
14211 if (count > 0)
14212 {
14213 /* Workaround ARM10 VFPr1 bug. */
14214 if (count == 2 && !arm_arch6)
14215 count++;
8edfc4cc 14216 saved += count * 8;
9728c9d1
PB
14217 }
14218 count = 0;
14219 }
14220 else
14221 count++;
14222 }
14223 if (count > 0)
14224 {
14225 if (count == 2 && !arm_arch6)
14226 count++;
8edfc4cc 14227 saved += count * 8;
9728c9d1
PB
14228 }
14229 }
14230 return saved;
14231}
14232
14233
699a4925 14234/* Generate a function exit sequence. If REALLY_RETURN is false, then do
6d3d9133 14235 everything bar the final return instruction. */
cd2b33d0 14236const char *
e32bac5b 14237output_return_instruction (rtx operand, int really_return, int reverse)
ff9940b0 14238{
6d3d9133 14239 char conditional[10];
ff9940b0 14240 char instr[100];
b279b20a 14241 unsigned reg;
6d3d9133
NC
14242 unsigned long live_regs_mask;
14243 unsigned long func_type;
5848830f 14244 arm_stack_offsets *offsets;
e26053d1 14245
6d3d9133 14246 func_type = arm_current_func_type ();
e2c671ba 14247
6d3d9133 14248 if (IS_NAKED (func_type))
d5b7b3ae 14249 return "";
6d3d9133
NC
14250
14251 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
e2c671ba 14252 {
699a4925
RE
14253 /* If this function was declared non-returning, and we have
14254 found a tail call, then we have to trust that the called
14255 function won't return. */
3a5a4282
PB
14256 if (really_return)
14257 {
14258 rtx ops[2];
f676971a 14259
3a5a4282
PB
14260 /* Otherwise, trap an attempted return by aborting. */
14261 ops[0] = operand;
f676971a 14262 ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
3a5a4282
PB
14263 : "abort");
14264 assemble_external_libcall (ops[1]);
14265 output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
14266 }
f676971a 14267
e2c671ba
RE
14268 return "";
14269 }
6d3d9133 14270
e3b5732b 14271 gcc_assert (!cfun->calls_alloca || really_return);
ff9940b0 14272
c414f8a9 14273 sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
d5b7b3ae 14274
934c2060 14275 cfun->machine->return_used_this_function = 1;
ff9940b0 14276
954954d1
PB
14277 offsets = arm_get_frame_offsets ();
14278 live_regs_mask = offsets->saved_regs_mask;
ff9940b0 14279
1768c26f 14280 if (live_regs_mask)
6d3d9133 14281 {
1768c26f
PB
14282 const char * return_reg;
14283
f676971a 14284 /* If we do not have any special requirements for function exit
a15908a4 14285 (e.g. interworking) then we can load the return address
1768c26f
PB
14286 directly into the PC. Otherwise we must load it into LR. */
14287 if (really_return
a15908a4 14288 && (IS_INTERRUPT (func_type) || !TARGET_INTERWORK))
1768c26f 14289 return_reg = reg_names[PC_REGNUM];
6d3d9133 14290 else
1768c26f
PB
14291 return_reg = reg_names[LR_REGNUM];
14292
6d3d9133 14293 if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
b034930f
ILT
14294 {
14295 /* There are three possible reasons for the IP register
14296 being saved. 1) a stack frame was created, in which case
14297 IP contains the old stack pointer, or 2) an ISR routine
14298 corrupted it, or 3) it was saved to align the stack on
14299 iWMMXt. In case 1, restore IP into SP, otherwise just
14300 restore IP. */
14301 if (frame_pointer_needed)
14302 {
14303 live_regs_mask &= ~ (1 << IP_REGNUM);
14304 live_regs_mask |= (1 << SP_REGNUM);
14305 }
14306 else
e6d29d15 14307 gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
b034930f 14308 }
f3bb6135 14309
3a7731fd
PB
14310 /* On some ARM architectures it is faster to use LDR rather than
14311 LDM to load a single register. On other architectures, the
14312 cost is the same. In 26 bit mode, or for exception handlers,
14313 we have to use LDM to load the PC so that the CPSR is also
14314 restored. */
14315 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
b279b20a
NC
14316 if (live_regs_mask == (1U << reg))
14317 break;
14318
3a7731fd
PB
14319 if (reg <= LAST_ARM_REGNUM
14320 && (reg != LR_REGNUM
f676971a 14321 || ! really_return
61f0ccff 14322 || ! IS_INTERRUPT (func_type)))
3a7731fd 14323 {
f676971a 14324 sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
3a7731fd 14325 (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
6d3d9133 14326 }
ff9940b0 14327 else
1d5473cb 14328 {
1768c26f
PB
14329 char *p;
14330 int first = 1;
6d3d9133 14331
699a4925
RE
14332 /* Generate the load multiple instruction to restore the
14333 registers. Note we can get here, even if
14334 frame_pointer_needed is true, but only if sp already
14335 points to the base of the saved core registers. */
14336 if (live_regs_mask & (1 << SP_REGNUM))
a72d4945 14337 {
5848830f
PB
14338 unsigned HOST_WIDE_INT stack_adjust;
14339
5848830f 14340 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
e6d29d15 14341 gcc_assert (stack_adjust == 0 || stack_adjust == 4);
a72d4945 14342
5b3e6663 14343 if (stack_adjust && arm_arch5 && TARGET_ARM)
c7e9ab97
RR
14344 if (TARGET_UNIFIED_ASM)
14345 sprintf (instr, "ldmib%s\t%%|sp, {", conditional);
14346 else
14347 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
a72d4945
RE
14348 else
14349 {
b279b20a
NC
14350 /* If we can't use ldmib (SA110 bug),
14351 then try to pop r3 instead. */
a72d4945
RE
14352 if (stack_adjust)
14353 live_regs_mask |= 1 << 3;
c7e9ab97
RR
14354
14355 if (TARGET_UNIFIED_ASM)
14356 sprintf (instr, "ldmfd%s\t%%|sp, {", conditional);
14357 else
14358 sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
a72d4945
RE
14359 }
14360 }
da6558fd 14361 else
c7e9ab97
RR
14362 if (TARGET_UNIFIED_ASM)
14363 sprintf (instr, "pop%s\t{", conditional);
14364 else
14365 sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
1768c26f
PB
14366
14367 p = instr + strlen (instr);
6d3d9133 14368
1768c26f
PB
14369 for (reg = 0; reg <= SP_REGNUM; reg++)
14370 if (live_regs_mask & (1 << reg))
14371 {
14372 int l = strlen (reg_names[reg]);
14373
14374 if (first)
14375 first = 0;
14376 else
14377 {
14378 memcpy (p, ", ", 2);
14379 p += 2;
14380 }
14381
14382 memcpy (p, "%|", 2);
14383 memcpy (p + 2, reg_names[reg], l);
14384 p += l + 2;
14385 }
f676971a 14386
1768c26f
PB
14387 if (live_regs_mask & (1 << LR_REGNUM))
14388 {
b17fe233 14389 sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
61f0ccff
RE
14390 /* If returning from an interrupt, restore the CPSR. */
14391 if (IS_INTERRUPT (func_type))
b17fe233 14392 strcat (p, "^");
1768c26f
PB
14393 }
14394 else
14395 strcpy (p, "}");
1d5473cb 14396 }
da6558fd 14397
1768c26f
PB
14398 output_asm_insn (instr, & operand);
14399
3a7731fd
PB
14400 /* See if we need to generate an extra instruction to
14401 perform the actual function return. */
14402 if (really_return
14403 && func_type != ARM_FT_INTERWORKED
14404 && (live_regs_mask & (1 << LR_REGNUM)) != 0)
da6558fd 14405 {
3a7731fd
PB
14406 /* The return has already been handled
14407 by loading the LR into the PC. */
14408 really_return = 0;
da6558fd 14409 }
ff9940b0 14410 }
e26053d1 14411
1768c26f 14412 if (really_return)
ff9940b0 14413 {
6d3d9133
NC
14414 switch ((int) ARM_FUNC_TYPE (func_type))
14415 {
14416 case ARM_FT_ISR:
14417 case ARM_FT_FIQ:
5b3e6663 14418 /* ??? This is wrong for unified assembly syntax. */
6d3d9133
NC
14419 sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
14420 break;
14421
14422 case ARM_FT_INTERWORKED:
14423 sprintf (instr, "bx%s\t%%|lr", conditional);
14424 break;
14425
14426 case ARM_FT_EXCEPTION:
5b3e6663 14427 /* ??? This is wrong for unified assembly syntax. */
6d3d9133
NC
14428 sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
14429 break;
14430
14431 default:
68d560d4
RE
14432 /* Use bx if it's available. */
14433 if (arm_arch5 || arm_arch4t)
f676971a 14434 sprintf (instr, "bx%s\t%%|lr", conditional);
1768c26f 14435 else
61f0ccff 14436 sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
6d3d9133
NC
14437 break;
14438 }
1768c26f
PB
14439
14440 output_asm_insn (instr, & operand);
ff9940b0 14441 }
f3bb6135 14442
ff9940b0
RE
14443 return "";
14444}
14445
ef179a26
NC
14446/* Write the function name into the code section, directly preceding
14447 the function prologue.
14448
14449 Code will be output similar to this:
14450 t0
14451 .ascii "arm_poke_function_name", 0
14452 .align
14453 t1
14454 .word 0xff000000 + (t1 - t0)
14455 arm_poke_function_name
14456 mov ip, sp
14457 stmfd sp!, {fp, ip, lr, pc}
14458 sub fp, ip, #4
14459
14460 When performing a stack backtrace, code can inspect the value
14461 of 'pc' stored at 'fp' + 0. If the trace function then looks
14462 at location pc - 12 and the top 8 bits are set, then we know
14463 that there is a function name embedded immediately preceding this
14464 location and has length ((pc[-3]) & 0xff000000).
14465
14466 We assume that pc is declared as a pointer to an unsigned long.
14467
14468 It is of no benefit to output the function name if we are assembling
14469 a leaf function. These function types will not contain a stack
14470 backtrace structure, therefore it is not possible to determine the
14471 function name. */
ef179a26 14472void
e32bac5b 14473arm_poke_function_name (FILE *stream, const char *name)
ef179a26
NC
14474{
14475 unsigned long alignlength;
14476 unsigned long length;
14477 rtx x;
14478
d5b7b3ae 14479 length = strlen (name) + 1;
0c2ca901 14480 alignlength = ROUND_UP_WORD (length);
f676971a 14481
949d79eb 14482 ASM_OUTPUT_ASCII (stream, name, length);
ef179a26 14483 ASM_OUTPUT_ALIGN (stream, 2);
30cf4896 14484 x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
301d03af 14485 assemble_aligned_integer (UNITS_PER_WORD, x);
ef179a26
NC
14486}
14487
6d3d9133
NC
14488/* Place some comments into the assembler stream
14489 describing the current function. */
08c148a8 14490static void
e32bac5b 14491arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
cce8749e 14492{
6d3d9133 14493 unsigned long func_type;
08c148a8 14494
5b3e6663 14495 if (TARGET_THUMB1)
08c148a8 14496 {
5b3e6663 14497 thumb1_output_function_prologue (f, frame_size);
08c148a8
NB
14498 return;
14499 }
f676971a 14500
6d3d9133 14501 /* Sanity check. */
e6d29d15 14502 gcc_assert (!arm_ccfsm_state && !arm_target_insn);
31fdb4d5 14503
6d3d9133 14504 func_type = arm_current_func_type ();
f676971a 14505
6d3d9133
NC
14506 switch ((int) ARM_FUNC_TYPE (func_type))
14507 {
14508 default:
14509 case ARM_FT_NORMAL:
14510 break;
14511 case ARM_FT_INTERWORKED:
14512 asm_fprintf (f, "\t%@ Function supports interworking.\n");
14513 break;
6d3d9133
NC
14514 case ARM_FT_ISR:
14515 asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
14516 break;
14517 case ARM_FT_FIQ:
14518 asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
14519 break;
14520 case ARM_FT_EXCEPTION:
14521 asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
14522 break;
14523 }
f676971a 14524
6d3d9133
NC
14525 if (IS_NAKED (func_type))
14526 asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
14527
14528 if (IS_VOLATILE (func_type))
14529 asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
14530
14531 if (IS_NESTED (func_type))
14532 asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
5b3e6663
PB
14533 if (IS_STACKALIGN (func_type))
14534 asm_fprintf (f, "\t%@ Stack Align: May be called with mis-aligned SP.\n");
f676971a 14535
c53dddc2 14536 asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
38173d38
JH
14537 crtl->args.size,
14538 crtl->args.pretend_args_size, frame_size);
6d3d9133 14539
3cb66fd7 14540 asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
dd18ae56 14541 frame_pointer_needed,
3cb66fd7 14542 cfun->machine->uses_anonymous_args);
cce8749e 14543
6f7ebcbb
NC
14544 if (cfun->machine->lr_save_eliminated)
14545 asm_fprintf (f, "\t%@ link register save eliminated.\n");
14546
e3b5732b 14547 if (crtl->calls_eh_return)
c9ca9b88
PB
14548 asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
14549
f3bb6135 14550}
cce8749e 14551
cd2b33d0 14552const char *
a72d4945 14553arm_output_epilogue (rtx sibling)
cce8749e 14554{
949d79eb 14555 int reg;
6f7ebcbb 14556 unsigned long saved_regs_mask;
6d3d9133 14557 unsigned long func_type;
f676971a 14558 /* Floats_offset is the offset from the "virtual" frame. In an APCS
c882c7ac
RE
14559 frame that is $fp + 4 for a non-variadic function. */
14560 int floats_offset = 0;
cce8749e 14561 rtx operands[3];
d5b7b3ae 14562 FILE * f = asm_out_file;
5a9335ef 14563 unsigned int lrm_count = 0;
a72d4945 14564 int really_return = (sibling == NULL);
9b66ebb1 14565 int start_reg;
5848830f 14566 arm_stack_offsets *offsets;
cce8749e 14567
6d3d9133
NC
14568 /* If we have already generated the return instruction
14569 then it is futile to generate anything else. */
934c2060
RR
14570 if (use_return_insn (FALSE, sibling) &&
14571 (cfun->machine->return_used_this_function != 0))
949d79eb 14572 return "";
cce8749e 14573
6d3d9133 14574 func_type = arm_current_func_type ();
d5b7b3ae 14575
6d3d9133
NC
14576 if (IS_NAKED (func_type))
14577 /* Naked functions don't have epilogues. */
14578 return "";
0616531f 14579
6d3d9133 14580 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
e2c671ba 14581 {
86efdc8e 14582 rtx op;
f676971a 14583
6d3d9133 14584 /* A volatile function should never return. Call abort. */
ed0e6530 14585 op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
2b835d68 14586 assemble_external_libcall (op);
e2c671ba 14587 output_asm_insn ("bl\t%a0", &op);
f676971a 14588
949d79eb 14589 return "";
e2c671ba
RE
14590 }
14591
e6d29d15
NS
14592 /* If we are throwing an exception, then we really must be doing a
14593 return, so we can't tail-call. */
e3b5732b 14594 gcc_assert (!crtl->calls_eh_return || really_return);
f676971a 14595
5848830f 14596 offsets = arm_get_frame_offsets ();
954954d1 14597 saved_regs_mask = offsets->saved_regs_mask;
5a9335ef
NC
14598
14599 if (TARGET_IWMMXT)
14600 lrm_count = bit_count (saved_regs_mask);
14601
5848830f 14602 floats_offset = offsets->saved_args;
6d3d9133 14603 /* Compute how far away the floats will be. */
5a9335ef 14604 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
6f7ebcbb 14605 if (saved_regs_mask & (1 << reg))
6ed30148 14606 floats_offset += 4;
f676971a 14607
ec6237e4 14608 if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
cce8749e 14609 {
9b66ebb1 14610 /* This variable is for the Virtual Frame Pointer, not VFP regs. */
5848830f 14611 int vfp_offset = offsets->frame;
c882c7ac 14612
d79f3032 14613 if (TARGET_FPA_EMU2)
b111229a 14614 {
9b66ebb1 14615 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
6fb5fa3c 14616 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
b111229a
RE
14617 {
14618 floats_offset += 12;
f676971a 14619 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
c882c7ac 14620 reg, FP_REGNUM, floats_offset - vfp_offset);
b111229a
RE
14621 }
14622 }
14623 else
14624 {
9b66ebb1 14625 start_reg = LAST_FPA_REGNUM;
b111229a 14626
9b66ebb1 14627 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
b111229a 14628 {
6fb5fa3c 14629 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
b111229a
RE
14630 {
14631 floats_offset += 12;
f676971a 14632
6354dc9b 14633 /* We can't unstack more than four registers at once. */
b111229a
RE
14634 if (start_reg - reg == 3)
14635 {
dd18ae56 14636 asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
c882c7ac 14637 reg, FP_REGNUM, floats_offset - vfp_offset);
b111229a
RE
14638 start_reg = reg - 1;
14639 }
14640 }
14641 else
14642 {
14643 if (reg != start_reg)
dd18ae56
NC
14644 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
14645 reg + 1, start_reg - reg,
c882c7ac 14646 FP_REGNUM, floats_offset - vfp_offset);
b111229a
RE
14647 start_reg = reg - 1;
14648 }
14649 }
14650
14651 /* Just in case the last register checked also needs unstacking. */
14652 if (reg != start_reg)
dd18ae56
NC
14653 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
14654 reg + 1, start_reg - reg,
c882c7ac 14655 FP_REGNUM, floats_offset - vfp_offset);
b111229a 14656 }
6d3d9133 14657
9b66ebb1
PB
14658 if (TARGET_HARD_FLOAT && TARGET_VFP)
14659 {
9728c9d1 14660 int saved_size;
9b66ebb1 14661
8edfc4cc
MS
14662 /* The fldmd insns do not have base+offset addressing
14663 modes, so we use IP to hold the address. */
9728c9d1 14664 saved_size = arm_get_vfp_saved_size ();
9b66ebb1 14665
9728c9d1 14666 if (saved_size > 0)
9b66ebb1 14667 {
9728c9d1 14668 floats_offset += saved_size;
9b66ebb1
PB
14669 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
14670 FP_REGNUM, floats_offset - vfp_offset);
14671 }
14672 start_reg = FIRST_VFP_REGNUM;
14673 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
14674 {
6fb5fa3c
DB
14675 if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
14676 && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
9b66ebb1
PB
14677 {
14678 if (start_reg != reg)
8edfc4cc 14679 vfp_output_fldmd (f, IP_REGNUM,
9728c9d1
PB
14680 (start_reg - FIRST_VFP_REGNUM) / 2,
14681 (reg - start_reg) / 2);
9b66ebb1
PB
14682 start_reg = reg + 2;
14683 }
14684 }
14685 if (start_reg != reg)
8edfc4cc 14686 vfp_output_fldmd (f, IP_REGNUM,
9728c9d1
PB
14687 (start_reg - FIRST_VFP_REGNUM) / 2,
14688 (reg - start_reg) / 2);
9b66ebb1
PB
14689 }
14690
5a9335ef
NC
14691 if (TARGET_IWMMXT)
14692 {
14693 /* The frame pointer is guaranteed to be non-double-word aligned.
14694 This is because it is set to (old_stack_pointer - 4) and the
14695 old_stack_pointer was double word aligned. Thus the offset to
14696 the iWMMXt registers to be loaded must also be non-double-word
14697 sized, so that the resultant address *is* double-word aligned.
14698 We can ignore floats_offset since that was already included in
14699 the live_regs_mask. */
14700 lrm_count += (lrm_count % 2 ? 2 : 1);
f676971a 14701
01d4c813 14702 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
6fb5fa3c 14703 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
5a9335ef 14704 {
f676971a 14705 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
5a9335ef 14706 reg, FP_REGNUM, lrm_count * 4);
f676971a 14707 lrm_count += 2;
5a9335ef
NC
14708 }
14709 }
14710
6f7ebcbb 14711 /* saved_regs_mask should contain the IP, which at the time of stack
6d3d9133
NC
14712 frame generation actually contains the old stack pointer. So a
14713 quick way to unwind the stack is just pop the IP register directly
14714 into the stack pointer. */
e6d29d15 14715 gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
6f7ebcbb
NC
14716 saved_regs_mask &= ~ (1 << IP_REGNUM);
14717 saved_regs_mask |= (1 << SP_REGNUM);
6d3d9133 14718
6f7ebcbb 14719 /* There are two registers left in saved_regs_mask - LR and PC. We
6d3d9133
NC
14720 only need to restore the LR register (the return address), but to
14721 save time we can load it directly into the PC, unless we need a
14722 special function exit sequence, or we are not really returning. */
c9ca9b88
PB
14723 if (really_return
14724 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
e3b5732b 14725 && !crtl->calls_eh_return)
6d3d9133
NC
14726 /* Delete the LR from the register mask, so that the LR on
14727 the stack is loaded into the PC in the register mask. */
6f7ebcbb 14728 saved_regs_mask &= ~ (1 << LR_REGNUM);
b111229a 14729 else
6f7ebcbb 14730 saved_regs_mask &= ~ (1 << PC_REGNUM);
efc2515b
RE
14731
14732 /* We must use SP as the base register, because SP is one of the
14733 registers being restored. If an interrupt or page fault
14734 happens in the ldm instruction, the SP might or might not
14735 have been restored. That would be bad, as then SP will no
14736 longer indicate the safe area of stack, and we can get stack
14737 corruption. Using SP as the base register means that it will
14738 be reset correctly to the original value, should an interrupt
699a4925
RE
14739 occur. If the stack pointer already points at the right
14740 place, then omit the subtraction. */
5848830f 14741 if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
e3b5732b 14742 || cfun->calls_alloca)
699a4925
RE
14743 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
14744 4 * bit_count (saved_regs_mask));
a15908a4 14745 print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask, 0);
7b8b8ade
NC
14746
14747 if (IS_INTERRUPT (func_type))
14748 /* Interrupt handlers will have pushed the
14749 IP onto the stack, so restore it now. */
a15908a4 14750 print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, 1 << IP_REGNUM, 0);
cce8749e
CH
14751 }
14752 else
14753 {
ec6237e4
PB
14754 /* This branch is executed for ARM mode (non-apcs frames) and
14755 Thumb-2 mode. Frame layout is essentially the same for those
14756 cases, except that in ARM mode frame pointer points to the
14757 first saved register, while in Thumb-2 mode the frame pointer points
14758 to the last saved register.
14759
14760 It is possible to make frame pointer point to last saved
14761 register in both cases, and remove some conditionals below.
14762 That means that fp setup in prologue would be just "mov fp, sp"
14763 and sp restore in epilogue would be just "mov sp, fp", whereas
14764 now we have to use add/sub in those cases. However, the value
14765 of that would be marginal, as both mov and add/sub are 32-bit
14766 in ARM mode, and it would require extra conditionals
14767 in arm_expand_prologue to distingish ARM-apcs-frame case
14768 (where frame pointer is required to point at first register)
14769 and ARM-non-apcs-frame. Therefore, such change is postponed
14770 until real need arise. */
f0b4bdd5 14771 unsigned HOST_WIDE_INT amount;
a15908a4 14772 int rfe;
d2288d8d 14773 /* Restore stack pointer if necessary. */
ec6237e4 14774 if (TARGET_ARM && frame_pointer_needed)
5b3e6663
PB
14775 {
14776 operands[0] = stack_pointer_rtx;
ec6237e4
PB
14777 operands[1] = hard_frame_pointer_rtx;
14778
14779 operands[2] = GEN_INT (offsets->frame - offsets->saved_regs);
14780 output_add_immediate (operands);
5b3e6663 14781 }
ec6237e4 14782 else
5b3e6663 14783 {
ec6237e4
PB
14784 if (frame_pointer_needed)
14785 {
14786 /* For Thumb-2 restore sp from the frame pointer.
14787 Operand restrictions mean we have to incrememnt FP, then copy
14788 to SP. */
14789 amount = offsets->locals_base - offsets->saved_regs;
14790 operands[0] = hard_frame_pointer_rtx;
14791 }
14792 else
14793 {
954954d1 14794 unsigned long count;
ec6237e4
PB
14795 operands[0] = stack_pointer_rtx;
14796 amount = offsets->outgoing_args - offsets->saved_regs;
954954d1
PB
14797 /* pop call clobbered registers if it avoids a
14798 separate stack adjustment. */
14799 count = offsets->saved_regs - offsets->saved_args;
14800 if (optimize_size
14801 && count != 0
e3b5732b 14802 && !crtl->calls_eh_return
954954d1
PB
14803 && bit_count(saved_regs_mask) * 4 == count
14804 && !IS_INTERRUPT (func_type)
e3b5732b 14805 && !crtl->tail_call_emit)
954954d1
PB
14806 {
14807 unsigned long mask;
c92f1823
IB
14808 /* Preserve return values, of any size. */
14809 mask = (1 << ((arm_size_return_regs() + 3) / 4)) - 1;
954954d1
PB
14810 mask ^= 0xf;
14811 mask &= ~saved_regs_mask;
14812 reg = 0;
14813 while (bit_count (mask) * 4 > amount)
14814 {
14815 while ((mask & (1 << reg)) == 0)
14816 reg++;
14817 mask &= ~(1 << reg);
14818 }
14819 if (bit_count (mask) * 4 == amount) {
14820 amount = 0;
14821 saved_regs_mask |= mask;
14822 }
14823 }
ec6237e4
PB
14824 }
14825
14826 if (amount)
14827 {
14828 operands[1] = operands[0];
14829 operands[2] = GEN_INT (amount);
14830 output_add_immediate (operands);
14831 }
14832 if (frame_pointer_needed)
14833 asm_fprintf (f, "\tmov\t%r, %r\n",
14834 SP_REGNUM, HARD_FRAME_POINTER_REGNUM);
d2288d8d
TG
14835 }
14836
d79f3032 14837 if (TARGET_FPA_EMU2)
b111229a 14838 {
9b66ebb1 14839 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
6fb5fa3c 14840 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
dd18ae56
NC
14841 asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
14842 reg, SP_REGNUM);
b111229a
RE
14843 }
14844 else
14845 {
9b66ebb1 14846 start_reg = FIRST_FPA_REGNUM;
b111229a 14847
9b66ebb1 14848 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
b111229a 14849 {
6fb5fa3c 14850 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
b111229a
RE
14851 {
14852 if (reg - start_reg == 3)
14853 {
dd18ae56
NC
14854 asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
14855 start_reg, SP_REGNUM);
b111229a
RE
14856 start_reg = reg + 1;
14857 }
14858 }
14859 else
14860 {
14861 if (reg != start_reg)
dd18ae56
NC
14862 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
14863 start_reg, reg - start_reg,
14864 SP_REGNUM);
f676971a 14865
b111229a
RE
14866 start_reg = reg + 1;
14867 }
14868 }
14869
14870 /* Just in case the last register checked also needs unstacking. */
14871 if (reg != start_reg)
dd18ae56
NC
14872 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
14873 start_reg, reg - start_reg, SP_REGNUM);
b111229a
RE
14874 }
14875
9b66ebb1
PB
14876 if (TARGET_HARD_FLOAT && TARGET_VFP)
14877 {
f8b68ed3
RE
14878 int end_reg = LAST_VFP_REGNUM + 1;
14879
14880 /* Scan the registers in reverse order. We need to match
14881 any groupings made in the prologue and generate matching
14882 pop operations. */
14883 for (reg = LAST_VFP_REGNUM - 1; reg >= FIRST_VFP_REGNUM; reg -= 2)
9b66ebb1 14884 {
6fb5fa3c 14885 if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
f8b68ed3
RE
14886 && (!df_regs_ever_live_p (reg + 1)
14887 || call_used_regs[reg + 1]))
9b66ebb1 14888 {
f8b68ed3 14889 if (end_reg > reg + 2)
8edfc4cc 14890 vfp_output_fldmd (f, SP_REGNUM,
f8b68ed3
RE
14891 (reg + 2 - FIRST_VFP_REGNUM) / 2,
14892 (end_reg - (reg + 2)) / 2);
14893 end_reg = reg;
9b66ebb1
PB
14894 }
14895 }
f8b68ed3
RE
14896 if (end_reg > reg + 2)
14897 vfp_output_fldmd (f, SP_REGNUM, 0,
14898 (end_reg - (reg + 2)) / 2);
9b66ebb1 14899 }
f8b68ed3 14900
5a9335ef
NC
14901 if (TARGET_IWMMXT)
14902 for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
6fb5fa3c 14903 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
01d4c813 14904 asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
5a9335ef 14905
6d3d9133 14906 /* If we can, restore the LR into the PC. */
a15908a4
PB
14907 if (ARM_FUNC_TYPE (func_type) != ARM_FT_INTERWORKED
14908 && (TARGET_ARM || ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
5b3e6663 14909 && !IS_STACKALIGN (func_type)
6d3d9133 14910 && really_return
38173d38 14911 && crtl->args.pretend_args_size == 0
c9ca9b88 14912 && saved_regs_mask & (1 << LR_REGNUM)
e3b5732b 14913 && !crtl->calls_eh_return)
cce8749e 14914 {
6f7ebcbb
NC
14915 saved_regs_mask &= ~ (1 << LR_REGNUM);
14916 saved_regs_mask |= (1 << PC_REGNUM);
a15908a4 14917 rfe = IS_INTERRUPT (func_type);
6d3d9133 14918 }
a15908a4
PB
14919 else
14920 rfe = 0;
d5b7b3ae 14921
6d3d9133 14922 /* Load the registers off the stack. If we only have one register
5b3e6663
PB
14923 to load use the LDR instruction - it is faster. For Thumb-2
14924 always use pop and the assembler will pick the best instruction.*/
a15908a4
PB
14925 if (TARGET_ARM && saved_regs_mask == (1 << LR_REGNUM)
14926 && !IS_INTERRUPT(func_type))
6d3d9133 14927 {
c9ca9b88 14928 asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
cce8749e 14929 }
6f7ebcbb 14930 else if (saved_regs_mask)
f1acdf8b
NC
14931 {
14932 if (saved_regs_mask & (1 << SP_REGNUM))
14933 /* Note - write back to the stack register is not enabled
112cdef5 14934 (i.e. "ldmfd sp!..."). We know that the stack pointer is
f1acdf8b
NC
14935 in the list of registers and if we add writeback the
14936 instruction becomes UNPREDICTABLE. */
a15908a4
PB
14937 print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask,
14938 rfe);
5b3e6663 14939 else if (TARGET_ARM)
a15908a4
PB
14940 print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, saved_regs_mask,
14941 rfe);
f1acdf8b 14942 else
a15908a4 14943 print_multi_reg (f, "pop\t", SP_REGNUM, saved_regs_mask, 0);
f1acdf8b 14944 }
6d3d9133 14945
38173d38 14946 if (crtl->args.pretend_args_size)
cce8749e 14947 {
6d3d9133
NC
14948 /* Unwind the pre-pushed regs. */
14949 operands[0] = operands[1] = stack_pointer_rtx;
38173d38 14950 operands[2] = GEN_INT (crtl->args.pretend_args_size);
6d3d9133
NC
14951 output_add_immediate (operands);
14952 }
14953 }
32de079a 14954
2966b00e 14955 /* We may have already restored PC directly from the stack. */
0cc3dda8 14956 if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
6d3d9133 14957 return "";
d5b7b3ae 14958
c9ca9b88 14959 /* Stack adjustment for exception handler. */
e3b5732b 14960 if (crtl->calls_eh_return)
f676971a 14961 asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
c9ca9b88
PB
14962 ARM_EH_STACKADJ_REGNUM);
14963
6d3d9133
NC
14964 /* Generate the return instruction. */
14965 switch ((int) ARM_FUNC_TYPE (func_type))
14966 {
6d3d9133
NC
14967 case ARM_FT_ISR:
14968 case ARM_FT_FIQ:
14969 asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
14970 break;
14971
14972 case ARM_FT_EXCEPTION:
14973 asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
14974 break;
14975
14976 case ARM_FT_INTERWORKED:
14977 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
14978 break;
14979
14980 default:
5b3e6663
PB
14981 if (IS_STACKALIGN (func_type))
14982 {
14983 /* See comment in arm_expand_prologue. */
14984 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, 0);
14985 }
68d560d4
RE
14986 if (arm_arch5 || arm_arch4t)
14987 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
14988 else
14989 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
6d3d9133 14990 break;
cce8749e 14991 }
f3bb6135 14992
949d79eb
RE
14993 return "";
14994}
14995
08c148a8 14996static void
e32bac5b 14997arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
5848830f 14998 HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
949d79eb 14999{
5848830f
PB
15000 arm_stack_offsets *offsets;
15001
5b3e6663 15002 if (TARGET_THUMB1)
d5b7b3ae 15003 {
b12a00f1
RE
15004 int regno;
15005
15006 /* Emit any call-via-reg trampolines that are needed for v4t support
15007 of call_reg and call_value_reg type insns. */
57ecec57 15008 for (regno = 0; regno < LR_REGNUM; regno++)
b12a00f1
RE
15009 {
15010 rtx label = cfun->machine->call_via[regno];
15011
15012 if (label != NULL)
15013 {
d6b5193b 15014 switch_to_section (function_section (current_function_decl));
b12a00f1
RE
15015 targetm.asm_out.internal_label (asm_out_file, "L",
15016 CODE_LABEL_NUMBER (label));
15017 asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
15018 }
15019 }
15020
d5b7b3ae
RE
15021 /* ??? Probably not safe to set this here, since it assumes that a
15022 function will be emitted as assembly immediately after we generate
15023 RTL for it. This does not happen for inline functions. */
934c2060 15024 cfun->machine->return_used_this_function = 0;
d5b7b3ae 15025 }
5b3e6663 15026 else /* TARGET_32BIT */
d5b7b3ae 15027 {
0977774b 15028 /* We need to take into account any stack-frame rounding. */
5848830f 15029 offsets = arm_get_frame_offsets ();
0977774b 15030
e6d29d15 15031 gcc_assert (!use_return_insn (FALSE, NULL)
934c2060 15032 || (cfun->machine->return_used_this_function != 0)
e6d29d15
NS
15033 || offsets->saved_regs == offsets->outgoing_args
15034 || frame_pointer_needed);
f3bb6135 15035
d5b7b3ae 15036 /* Reset the ARM-specific per-function variables. */
d5b7b3ae
RE
15037 after_arm_reorg = 0;
15038 }
f3bb6135 15039}
e2c671ba 15040
2c849145
JM
15041/* Generate and emit an insn that we will recognize as a push_multi.
15042 Unfortunately, since this insn does not reflect very well the actual
15043 semantics of the operation, we need to annotate the insn for the benefit
15044 of DWARF2 frame unwind information. */
2c849145 15045static rtx
b279b20a 15046emit_multi_reg_push (unsigned long mask)
e2c671ba
RE
15047{
15048 int num_regs = 0;
9b598fa0 15049 int num_dwarf_regs;
e2c671ba
RE
15050 int i, j;
15051 rtx par;
2c849145 15052 rtx dwarf;
87e27392 15053 int dwarf_par_index;
2c849145 15054 rtx tmp, reg;
e2c671ba 15055
d5b7b3ae 15056 for (i = 0; i <= LAST_ARM_REGNUM; i++)
e2c671ba 15057 if (mask & (1 << i))
5895f793 15058 num_regs++;
e2c671ba 15059
e6d29d15 15060 gcc_assert (num_regs && num_regs <= 16);
e2c671ba 15061
9b598fa0
RE
15062 /* We don't record the PC in the dwarf frame information. */
15063 num_dwarf_regs = num_regs;
15064 if (mask & (1 << PC_REGNUM))
15065 num_dwarf_regs--;
15066
87e27392 15067 /* For the body of the insn we are going to generate an UNSPEC in
05713b80 15068 parallel with several USEs. This allows the insn to be recognized
9abf5d7b
RR
15069 by the push_multi pattern in the arm.md file.
15070
15071 The body of the insn looks something like this:
87e27392 15072
f676971a 15073 (parallel [
9abf5d7b
RR
15074 (set (mem:BLK (pre_modify:SI (reg:SI sp)
15075 (const_int:SI <num>)))
b15bca31 15076 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
9abf5d7b
RR
15077 (use (reg:SI XX))
15078 (use (reg:SI YY))
15079 ...
87e27392
NC
15080 ])
15081
15082 For the frame note however, we try to be more explicit and actually
15083 show each register being stored into the stack frame, plus a (single)
15084 decrement of the stack pointer. We do it this way in order to be
15085 friendly to the stack unwinding code, which only wants to see a single
15086 stack decrement per instruction. The RTL we generate for the note looks
15087 something like this:
15088
f676971a 15089 (sequence [
87e27392
NC
15090 (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
15091 (set (mem:SI (reg:SI sp)) (reg:SI r4))
9abf5d7b
RR
15092 (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI XX))
15093 (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI YY))
15094 ...
87e27392
NC
15095 ])
15096
9abf5d7b
RR
15097 FIXME:: In an ideal world the PRE_MODIFY would not exist and
15098 instead we'd have a parallel expression detailing all
15099 the stores to the various memory addresses so that debug
15100 information is more up-to-date. Remember however while writing
15101 this to take care of the constraints with the push instruction.
15102
15103 Note also that this has to be taken care of for the VFP registers.
15104
15105 For more see PR43399. */
f676971a 15106
43cffd11 15107 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
9b598fa0 15108 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
87e27392 15109 dwarf_par_index = 1;
e2c671ba 15110
d5b7b3ae 15111 for (i = 0; i <= LAST_ARM_REGNUM; i++)
e2c671ba
RE
15112 {
15113 if (mask & (1 << i))
15114 {
2c849145
JM
15115 reg = gen_rtx_REG (SImode, i);
15116
e2c671ba 15117 XVECEXP (par, 0, 0)
43cffd11 15118 = gen_rtx_SET (VOIDmode,
9abf5d7b
RR
15119 gen_frame_mem
15120 (BLKmode,
15121 gen_rtx_PRE_MODIFY (Pmode,
15122 stack_pointer_rtx,
15123 plus_constant
15124 (stack_pointer_rtx,
15125 -4 * num_regs))
15126 ),
43cffd11 15127 gen_rtx_UNSPEC (BLKmode,
2c849145 15128 gen_rtvec (1, reg),
9b598fa0 15129 UNSPEC_PUSH_MULT));
2c849145 15130
9b598fa0
RE
15131 if (i != PC_REGNUM)
15132 {
15133 tmp = gen_rtx_SET (VOIDmode,
31fa16b6 15134 gen_frame_mem (SImode, stack_pointer_rtx),
9b598fa0
RE
15135 reg);
15136 RTX_FRAME_RELATED_P (tmp) = 1;
15137 XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
15138 dwarf_par_index++;
15139 }
2c849145 15140
e2c671ba
RE
15141 break;
15142 }
15143 }
15144
15145 for (j = 1, i++; j < num_regs; i++)
15146 {
15147 if (mask & (1 << i))
15148 {
2c849145
JM
15149 reg = gen_rtx_REG (SImode, i);
15150
15151 XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
15152
9b598fa0
RE
15153 if (i != PC_REGNUM)
15154 {
31fa16b6
RE
15155 tmp
15156 = gen_rtx_SET (VOIDmode,
9abf5d7b
RR
15157 gen_frame_mem
15158 (SImode,
15159 plus_constant (stack_pointer_rtx,
15160 4 * j)),
31fa16b6 15161 reg);
9b598fa0
RE
15162 RTX_FRAME_RELATED_P (tmp) = 1;
15163 XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
15164 }
15165
e2c671ba
RE
15166 j++;
15167 }
15168 }
b111229a 15169
2c849145 15170 par = emit_insn (par);
f676971a 15171
d66437c5 15172 tmp = gen_rtx_SET (VOIDmode,
87e27392 15173 stack_pointer_rtx,
d66437c5 15174 plus_constant (stack_pointer_rtx, -4 * num_regs));
87e27392
NC
15175 RTX_FRAME_RELATED_P (tmp) = 1;
15176 XVECEXP (dwarf, 0, 0) = tmp;
f676971a 15177
bbbbb16a
ILT
15178 add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
15179
2c849145 15180 return par;
b111229a
RE
15181}
15182
4f5dfed0 15183/* Calculate the size of the return value that is passed in registers. */
466e4b7a 15184static unsigned
4f5dfed0
JC
15185arm_size_return_regs (void)
15186{
15187 enum machine_mode mode;
15188
38173d38
JH
15189 if (crtl->return_rtx != 0)
15190 mode = GET_MODE (crtl->return_rtx);
4f5dfed0
JC
15191 else
15192 mode = DECL_MODE (DECL_RESULT (current_function_decl));
15193
15194 return GET_MODE_SIZE (mode);
15195}
15196
2c849145 15197static rtx
e32bac5b 15198emit_sfm (int base_reg, int count)
b111229a
RE
15199{
15200 rtx par;
2c849145
JM
15201 rtx dwarf;
15202 rtx tmp, reg;
b111229a
RE
15203 int i;
15204
43cffd11 15205 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
8ee6eb4e 15206 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
2c849145
JM
15207
15208 reg = gen_rtx_REG (XFmode, base_reg++);
43cffd11
RE
15209
15210 XVECEXP (par, 0, 0)
f676971a 15211 = gen_rtx_SET (VOIDmode,
9abf5d7b
RR
15212 gen_frame_mem
15213 (BLKmode,
15214 gen_rtx_PRE_MODIFY (Pmode,
15215 stack_pointer_rtx,
15216 plus_constant
15217 (stack_pointer_rtx,
15218 -12 * count))
15219 ),
43cffd11 15220 gen_rtx_UNSPEC (BLKmode,
2c849145 15221 gen_rtvec (1, reg),
b15bca31 15222 UNSPEC_PUSH_MULT));
f676971a 15223 tmp = gen_rtx_SET (VOIDmode,
31fa16b6 15224 gen_frame_mem (XFmode, stack_pointer_rtx), reg);
2c849145 15225 RTX_FRAME_RELATED_P (tmp) = 1;
f676971a
EC
15226 XVECEXP (dwarf, 0, 1) = tmp;
15227
b111229a 15228 for (i = 1; i < count; i++)
2c849145
JM
15229 {
15230 reg = gen_rtx_REG (XFmode, base_reg++);
15231 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
15232
f676971a 15233 tmp = gen_rtx_SET (VOIDmode,
31fa16b6
RE
15234 gen_frame_mem (XFmode,
15235 plus_constant (stack_pointer_rtx,
15236 i * 12)),
2c849145
JM
15237 reg);
15238 RTX_FRAME_RELATED_P (tmp) = 1;
f676971a 15239 XVECEXP (dwarf, 0, i + 1) = tmp;
2c849145 15240 }
b111229a 15241
8ee6eb4e
PB
15242 tmp = gen_rtx_SET (VOIDmode,
15243 stack_pointer_rtx,
d66437c5
RE
15244 plus_constant (stack_pointer_rtx, -12 * count));
15245
8ee6eb4e
PB
15246 RTX_FRAME_RELATED_P (tmp) = 1;
15247 XVECEXP (dwarf, 0, 0) = tmp;
15248
2c849145 15249 par = emit_insn (par);
bbbbb16a
ILT
15250 add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
15251
2c849145 15252 return par;
e2c671ba
RE
15253}
15254
9b66ebb1 15255
3c7ad43e
PB
15256/* Return true if the current function needs to save/restore LR. */
15257
15258static bool
15259thumb_force_lr_save (void)
15260{
15261 return !cfun->machine->lr_save_eliminated
15262 && (!leaf_function_p ()
15263 || thumb_far_jump_used_p ()
6fb5fa3c 15264 || df_regs_ever_live_p (LR_REGNUM));
3c7ad43e
PB
15265}
15266
15267
147a0bcf
JJ
15268/* Return true if r3 is used by any of the tail call insns in the
15269 current function. */
15270
15271static bool
15272any_sibcall_uses_r3 (void)
15273{
15274 edge_iterator ei;
15275 edge e;
15276
15277 if (!crtl->tail_call_emit)
15278 return false;
15279 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
15280 if (e->flags & EDGE_SIBCALL)
15281 {
15282 rtx call = BB_END (e->src);
15283 if (!CALL_P (call))
15284 call = prev_nonnote_nondebug_insn (call);
15285 gcc_assert (CALL_P (call) && SIBLING_CALL_P (call));
15286 if (find_regno_fusage (call, USE, 3))
15287 return true;
15288 }
15289 return false;
15290}
15291
15292
095bb276
NC
15293/* Compute the distance from register FROM to register TO.
15294 These can be the arg pointer (26), the soft frame pointer (25),
15295 the stack pointer (13) or the hard frame pointer (11).
c9ca9b88 15296 In thumb mode r7 is used as the soft frame pointer, if needed.
095bb276
NC
15297 Typical stack layout looks like this:
15298
15299 old stack pointer -> | |
15300 ----
15301 | | \
15302 | | saved arguments for
15303 | | vararg functions
15304 | | /
15305 --
15306 hard FP & arg pointer -> | | \
15307 | | stack
15308 | | frame
15309 | | /
15310 --
15311 | | \
15312 | | call saved
15313 | | registers
15314 soft frame pointer -> | | /
15315 --
15316 | | \
15317 | | local
15318 | | variables
2591db65 15319 locals base pointer -> | | /
095bb276
NC
15320 --
15321 | | \
15322 | | outgoing
15323 | | arguments
15324 current stack pointer -> | | /
15325 --
15326
43aa4e05 15327 For a given function some or all of these stack components
095bb276
NC
15328 may not be needed, giving rise to the possibility of
15329 eliminating some of the registers.
15330
825dda42 15331 The values returned by this function must reflect the behavior
095bb276
NC
15332 of arm_expand_prologue() and arm_compute_save_reg_mask().
15333
15334 The sign of the number returned reflects the direction of stack
15335 growth, so the values are positive for all eliminations except
5848830f
PB
15336 from the soft frame pointer to the hard frame pointer.
15337
15338 SFP may point just inside the local variables block to ensure correct
15339 alignment. */
15340
15341
15342/* Calculate stack offsets. These are used to calculate register elimination
954954d1
PB
15343 offsets and in prologue/epilogue code. Also calculates which registers
15344 should be saved. */
5848830f
PB
15345
15346static arm_stack_offsets *
15347arm_get_frame_offsets (void)
095bb276 15348{
5848830f 15349 struct arm_stack_offsets *offsets;
095bb276 15350 unsigned long func_type;
5848830f 15351 int leaf;
5848830f 15352 int saved;
954954d1 15353 int core_saved;
5848830f 15354 HOST_WIDE_INT frame_size;
954954d1 15355 int i;
5848830f
PB
15356
15357 offsets = &cfun->machine->stack_offsets;
f676971a 15358
5848830f
PB
15359 /* We need to know if we are a leaf function. Unfortunately, it
15360 is possible to be called after start_sequence has been called,
15361 which causes get_insns to return the insns for the sequence,
15362 not the function, which will cause leaf_function_p to return
15363 the incorrect result.
095bb276 15364
5848830f
PB
15365 to know about leaf functions once reload has completed, and the
15366 frame size cannot be changed after that time, so we can safely
15367 use the cached value. */
15368
15369 if (reload_completed)
15370 return offsets;
15371
666c27b9
KH
15372 /* Initially this is the size of the local variables. It will translated
15373 into an offset once we have determined the size of preceding data. */
5848830f
PB
15374 frame_size = ROUND_UP_WORD (get_frame_size ());
15375
15376 leaf = leaf_function_p ();
15377
15378 /* Space for variadic functions. */
38173d38 15379 offsets->saved_args = crtl->args.pretend_args_size;
5848830f 15380
5b3e6663 15381 /* In Thumb mode this is incorrect, but never used. */
35596784
AJ
15382 offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0) +
15383 arm_compute_static_chain_stack_bytes();
5848830f 15384
5b3e6663 15385 if (TARGET_32BIT)
095bb276 15386 {
5848830f 15387 unsigned int regno;
ef7112de 15388
954954d1
PB
15389 offsets->saved_regs_mask = arm_compute_save_reg_mask ();
15390 core_saved = bit_count (offsets->saved_regs_mask) * 4;
15391 saved = core_saved;
5a9335ef 15392
5848830f
PB
15393 /* We know that SP will be doubleword aligned on entry, and we must
15394 preserve that condition at any subroutine call. We also require the
15395 soft frame pointer to be doubleword aligned. */
15396
15397 if (TARGET_REALLY_IWMMXT)
9b66ebb1 15398 {
5848830f
PB
15399 /* Check for the call-saved iWMMXt registers. */
15400 for (regno = FIRST_IWMMXT_REGNUM;
15401 regno <= LAST_IWMMXT_REGNUM;
15402 regno++)
6fb5fa3c 15403 if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
5848830f
PB
15404 saved += 8;
15405 }
15406
15407 func_type = arm_current_func_type ();
15408 if (! IS_VOLATILE (func_type))
15409 {
15410 /* Space for saved FPA registers. */
15411 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
6fb5fa3c 15412 if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
5848830f
PB
15413 saved += 12;
15414
15415 /* Space for saved VFP registers. */
15416 if (TARGET_HARD_FLOAT && TARGET_VFP)
9728c9d1 15417 saved += arm_get_vfp_saved_size ();
9b66ebb1 15418 }
5848830f 15419 }
5b3e6663 15420 else /* TARGET_THUMB1 */
5848830f 15421 {
954954d1
PB
15422 offsets->saved_regs_mask = thumb1_compute_save_reg_mask ();
15423 core_saved = bit_count (offsets->saved_regs_mask) * 4;
15424 saved = core_saved;
5848830f 15425 if (TARGET_BACKTRACE)
57934c39 15426 saved += 16;
5848830f 15427 }
9b66ebb1 15428
5848830f 15429 /* Saved registers include the stack frame. */
35596784
AJ
15430 offsets->saved_regs = offsets->saved_args + saved +
15431 arm_compute_static_chain_stack_bytes();
a2503645 15432 offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
5848830f
PB
15433 /* A leaf function does not need any stack alignment if it has nothing
15434 on the stack. */
7c71147d
CLT
15435 if (leaf && frame_size == 0
15436 /* However if it calls alloca(), we have a dynamically allocated
15437 block of BIGGEST_ALIGNMENT on stack, so still do stack alignment. */
15438 && ! cfun->calls_alloca)
5848830f
PB
15439 {
15440 offsets->outgoing_args = offsets->soft_frame;
a3a531ec 15441 offsets->locals_base = offsets->soft_frame;
5848830f
PB
15442 return offsets;
15443 }
15444
15445 /* Ensure SFP has the correct alignment. */
15446 if (ARM_DOUBLEWORD_ALIGN
15447 && (offsets->soft_frame & 7))
954954d1
PB
15448 {
15449 offsets->soft_frame += 4;
15450 /* Try to align stack by pushing an extra reg. Don't bother doing this
15451 when there is a stack frame as the alignment will be rolled into
15452 the normal stack adjustment. */
38173d38 15453 if (frame_size + crtl->outgoing_args_size == 0)
954954d1
PB
15454 {
15455 int reg = -1;
15456
55b2829b
RE
15457 /* If it is safe to use r3, then do so. This sometimes
15458 generates better code on Thumb-2 by avoiding the need to
15459 use 32-bit push/pop instructions. */
147a0bcf 15460 if (! any_sibcall_uses_r3 ()
fb2f8cf8
JZ
15461 && arm_size_return_regs () <= 12
15462 && (offsets->saved_regs_mask & (1 << 3)) == 0)
954954d1 15463 {
954954d1
PB
15464 reg = 3;
15465 }
55b2829b
RE
15466 else
15467 for (i = 4; i <= (TARGET_THUMB1 ? LAST_LO_REGNUM : 11); i++)
15468 {
15469 if ((offsets->saved_regs_mask & (1 << i)) == 0)
15470 {
15471 reg = i;
15472 break;
15473 }
15474 }
954954d1
PB
15475
15476 if (reg != -1)
15477 {
15478 offsets->saved_regs += 4;
15479 offsets->saved_regs_mask |= (1 << reg);
15480 }
15481 }
15482 }
5848830f 15483
2591db65
RE
15484 offsets->locals_base = offsets->soft_frame + frame_size;
15485 offsets->outgoing_args = (offsets->locals_base
38173d38 15486 + crtl->outgoing_args_size);
5848830f
PB
15487
15488 if (ARM_DOUBLEWORD_ALIGN)
15489 {
15490 /* Ensure SP remains doubleword aligned. */
15491 if (offsets->outgoing_args & 7)
15492 offsets->outgoing_args += 4;
e6d29d15 15493 gcc_assert (!(offsets->outgoing_args & 7));
095bb276
NC
15494 }
15495
5848830f
PB
15496 return offsets;
15497}
15498
15499
666c27b9 15500/* Calculate the relative offsets for the different stack pointers. Positive
5848830f
PB
15501 offsets are in the direction of stack growth. */
15502
b3f8d95d 15503HOST_WIDE_INT
5848830f
PB
15504arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
15505{
15506 arm_stack_offsets *offsets;
15507
15508 offsets = arm_get_frame_offsets ();
095bb276 15509
095bb276
NC
15510 /* OK, now we have enough information to compute the distances.
15511 There must be an entry in these switch tables for each pair
15512 of registers in ELIMINABLE_REGS, even if some of the entries
15513 seem to be redundant or useless. */
15514 switch (from)
15515 {
15516 case ARG_POINTER_REGNUM:
15517 switch (to)
15518 {
15519 case THUMB_HARD_FRAME_POINTER_REGNUM:
15520 return 0;
15521
15522 case FRAME_POINTER_REGNUM:
15523 /* This is the reverse of the soft frame pointer
15524 to hard frame pointer elimination below. */
5848830f 15525 return offsets->soft_frame - offsets->saved_args;
095bb276
NC
15526
15527 case ARM_HARD_FRAME_POINTER_REGNUM:
35596784
AJ
15528 /* This is only non-zero in the case where the static chain register
15529 is stored above the frame. */
15530 return offsets->frame - offsets->saved_args - 4;
095bb276
NC
15531
15532 case STACK_POINTER_REGNUM:
15533 /* If nothing has been pushed on the stack at all
15534 then this will return -4. This *is* correct! */
5848830f 15535 return offsets->outgoing_args - (offsets->saved_args + 4);
095bb276
NC
15536
15537 default:
e6d29d15 15538 gcc_unreachable ();
095bb276 15539 }
e6d29d15 15540 gcc_unreachable ();
095bb276
NC
15541
15542 case FRAME_POINTER_REGNUM:
15543 switch (to)
15544 {
15545 case THUMB_HARD_FRAME_POINTER_REGNUM:
15546 return 0;
15547
15548 case ARM_HARD_FRAME_POINTER_REGNUM:
15549 /* The hard frame pointer points to the top entry in the
15550 stack frame. The soft frame pointer to the bottom entry
15551 in the stack frame. If there is no stack frame at all,
15552 then they are identical. */
5848830f
PB
15553
15554 return offsets->frame - offsets->soft_frame;
095bb276
NC
15555
15556 case STACK_POINTER_REGNUM:
5848830f 15557 return offsets->outgoing_args - offsets->soft_frame;
095bb276
NC
15558
15559 default:
e6d29d15 15560 gcc_unreachable ();
095bb276 15561 }
e6d29d15 15562 gcc_unreachable ();
095bb276
NC
15563
15564 default:
15565 /* You cannot eliminate from the stack pointer.
15566 In theory you could eliminate from the hard frame
15567 pointer to the stack pointer, but this will never
15568 happen, since if a stack frame is not needed the
15569 hard frame pointer will never be used. */
e6d29d15 15570 gcc_unreachable ();
095bb276
NC
15571 }
15572}
15573
7b5cbb57
AS
15574/* Given FROM and TO register numbers, say whether this elimination is
15575 allowed. Frame pointer elimination is automatically handled.
15576
15577 All eliminations are permissible. Note that ARG_POINTER_REGNUM and
15578 HARD_FRAME_POINTER_REGNUM are in fact the same thing. If we need a frame
15579 pointer, we must eliminate FRAME_POINTER_REGNUM into
15580 HARD_FRAME_POINTER_REGNUM and not into STACK_POINTER_REGNUM or
15581 ARG_POINTER_REGNUM. */
15582
15583bool
15584arm_can_eliminate (const int from, const int to)
15585{
15586 return ((to == FRAME_POINTER_REGNUM && from == ARG_POINTER_REGNUM) ? false :
15587 (to == STACK_POINTER_REGNUM && frame_pointer_needed) ? false :
15588 (to == ARM_HARD_FRAME_POINTER_REGNUM && TARGET_THUMB) ? false :
15589 (to == THUMB_HARD_FRAME_POINTER_REGNUM && TARGET_ARM) ? false :
15590 true);
15591}
0977774b 15592
7a085dce 15593/* Emit RTL to save coprocessor registers on function entry. Returns the
5b3e6663
PB
15594 number of bytes pushed. */
15595
15596static int
15597arm_save_coproc_regs(void)
15598{
15599 int saved_size = 0;
15600 unsigned reg;
15601 unsigned start_reg;
15602 rtx insn;
15603
15604 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
6fb5fa3c 15605 if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
5b3e6663 15606 {
d8d55ac0 15607 insn = gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx);
5b3e6663
PB
15608 insn = gen_rtx_MEM (V2SImode, insn);
15609 insn = emit_set_insn (insn, gen_rtx_REG (V2SImode, reg));
15610 RTX_FRAME_RELATED_P (insn) = 1;
15611 saved_size += 8;
15612 }
15613
15614 /* Save any floating point call-saved registers used by this
15615 function. */
d79f3032 15616 if (TARGET_FPA_EMU2)
5b3e6663
PB
15617 {
15618 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
6fb5fa3c 15619 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
5b3e6663 15620 {
d8d55ac0 15621 insn = gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx);
5b3e6663
PB
15622 insn = gen_rtx_MEM (XFmode, insn);
15623 insn = emit_set_insn (insn, gen_rtx_REG (XFmode, reg));
15624 RTX_FRAME_RELATED_P (insn) = 1;
15625 saved_size += 12;
15626 }
15627 }
15628 else
15629 {
15630 start_reg = LAST_FPA_REGNUM;
15631
15632 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
15633 {
6fb5fa3c 15634 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
5b3e6663
PB
15635 {
15636 if (start_reg - reg == 3)
15637 {
15638 insn = emit_sfm (reg, 4);
15639 RTX_FRAME_RELATED_P (insn) = 1;
15640 saved_size += 48;
15641 start_reg = reg - 1;
15642 }
15643 }
15644 else
15645 {
15646 if (start_reg != reg)
15647 {
15648 insn = emit_sfm (reg + 1, start_reg - reg);
15649 RTX_FRAME_RELATED_P (insn) = 1;
15650 saved_size += (start_reg - reg) * 12;
15651 }
15652 start_reg = reg - 1;
15653 }
15654 }
15655
15656 if (start_reg != reg)
15657 {
15658 insn = emit_sfm (reg + 1, start_reg - reg);
15659 saved_size += (start_reg - reg) * 12;
15660 RTX_FRAME_RELATED_P (insn) = 1;
15661 }
15662 }
15663 if (TARGET_HARD_FLOAT && TARGET_VFP)
15664 {
15665 start_reg = FIRST_VFP_REGNUM;
15666
15667 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
15668 {
6fb5fa3c
DB
15669 if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
15670 && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
5b3e6663
PB
15671 {
15672 if (start_reg != reg)
15673 saved_size += vfp_emit_fstmd (start_reg,
15674 (reg - start_reg) / 2);
15675 start_reg = reg + 2;
15676 }
15677 }
15678 if (start_reg != reg)
15679 saved_size += vfp_emit_fstmd (start_reg,
15680 (reg - start_reg) / 2);
15681 }
15682 return saved_size;
15683}
15684
15685
15686/* Set the Thumb frame pointer from the stack pointer. */
15687
15688static void
15689thumb_set_frame_pointer (arm_stack_offsets *offsets)
15690{
15691 HOST_WIDE_INT amount;
15692 rtx insn, dwarf;
15693
15694 amount = offsets->outgoing_args - offsets->locals_base;
15695 if (amount < 1024)
15696 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15697 stack_pointer_rtx, GEN_INT (amount)));
15698 else
15699 {
15700 emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
f5c630c3
PB
15701 /* Thumb-2 RTL patterns expect sp as the first input. Thumb-1
15702 expects the first two operands to be the same. */
15703 if (TARGET_THUMB2)
15704 {
15705 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15706 stack_pointer_rtx,
15707 hard_frame_pointer_rtx));
15708 }
15709 else
15710 {
15711 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15712 hard_frame_pointer_rtx,
15713 stack_pointer_rtx));
15714 }
5b3e6663
PB
15715 dwarf = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
15716 plus_constant (stack_pointer_rtx, amount));
15717 RTX_FRAME_RELATED_P (dwarf) = 1;
bbbbb16a 15718 add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
5b3e6663
PB
15719 }
15720
15721 RTX_FRAME_RELATED_P (insn) = 1;
15722}
15723
15724/* Generate the prologue instructions for entry into an ARM or Thumb-2
15725 function. */
e2c671ba 15726void
e32bac5b 15727arm_expand_prologue (void)
e2c671ba 15728{
6d3d9133 15729 rtx amount;
2c849145 15730 rtx insn;
68dfd979 15731 rtx ip_rtx;
6d3d9133
NC
15732 unsigned long live_regs_mask;
15733 unsigned long func_type;
68dfd979 15734 int fp_offset = 0;
095bb276 15735 int saved_pretend_args = 0;
5848830f 15736 int saved_regs = 0;
b9705a21 15737 unsigned HOST_WIDE_INT args_to_push;
5848830f 15738 arm_stack_offsets *offsets;
d3236b4d 15739
6d3d9133 15740 func_type = arm_current_func_type ();
e2c671ba 15741
31fdb4d5 15742 /* Naked functions don't have prologues. */
6d3d9133 15743 if (IS_NAKED (func_type))
31fdb4d5
DE
15744 return;
15745
095bb276 15746 /* Make a copy of c_f_p_a_s as we may need to modify it locally. */
38173d38 15747 args_to_push = crtl->args.pretend_args_size;
f676971a 15748
6d3d9133 15749 /* Compute which register we will have to save onto the stack. */
954954d1
PB
15750 offsets = arm_get_frame_offsets ();
15751 live_regs_mask = offsets->saved_regs_mask;
e2c671ba 15752
68dfd979 15753 ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
d3236b4d 15754
5b3e6663
PB
15755 if (IS_STACKALIGN (func_type))
15756 {
15757 rtx dwarf;
15758 rtx r0;
15759 rtx r1;
15760 /* Handle a word-aligned stack pointer. We generate the following:
15761
15762 mov r0, sp
15763 bic r1, r0, #7
15764 mov sp, r1
15765 <save and restore r0 in normal prologue/epilogue>
15766 mov sp, r0
15767 bx lr
15768
15769 The unwinder doesn't need to know about the stack realignment.
15770 Just tell it we saved SP in r0. */
15771 gcc_assert (TARGET_THUMB2 && !arm_arch_notm && args_to_push == 0);
15772
15773 r0 = gen_rtx_REG (SImode, 0);
15774 r1 = gen_rtx_REG (SImode, 1);
44bfa35b
NF
15775 /* Use a real rtvec rather than NULL_RTVEC so the rest of the
15776 compiler won't choke. */
15777 dwarf = gen_rtx_UNSPEC (SImode, rtvec_alloc (0), UNSPEC_STACK_ALIGN);
5b3e6663
PB
15778 dwarf = gen_rtx_SET (VOIDmode, r0, dwarf);
15779 insn = gen_movsi (r0, stack_pointer_rtx);
15780 RTX_FRAME_RELATED_P (insn) = 1;
bbbbb16a 15781 add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
5b3e6663
PB
15782 emit_insn (insn);
15783 emit_insn (gen_andsi3 (r1, r0, GEN_INT (~(HOST_WIDE_INT)7)));
15784 emit_insn (gen_movsi (stack_pointer_rtx, r1));
15785 }
15786
ec6237e4
PB
15787 /* For APCS frames, if IP register is clobbered
15788 when creating frame, save that register in a special
15789 way. */
15790 if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
e2c671ba 15791 {
7b8b8ade
NC
15792 if (IS_INTERRUPT (func_type))
15793 {
15794 /* Interrupt functions must not corrupt any registers.
15795 Creating a frame pointer however, corrupts the IP
15796 register, so we must push it first. */
15797 insn = emit_multi_reg_push (1 << IP_REGNUM);
121308d4
NC
15798
15799 /* Do not set RTX_FRAME_RELATED_P on this insn.
15800 The dwarf stack unwinding code only wants to see one
15801 stack decrement per function, and this is not it. If
15802 this instruction is labeled as being part of the frame
15803 creation sequence then dwarf2out_frame_debug_expr will
e6d29d15 15804 die when it encounters the assignment of IP to FP
121308d4
NC
15805 later on, since the use of SP here establishes SP as
15806 the CFA register and not IP.
15807
15808 Anyway this instruction is not really part of the stack
15809 frame creation although it is part of the prologue. */
7b8b8ade
NC
15810 }
15811 else if (IS_NESTED (func_type))
68dfd979
NC
15812 {
15813 /* The Static chain register is the same as the IP register
15814 used as a scratch register during stack frame creation.
15815 To get around this need to find somewhere to store IP
15816 whilst the frame is being created. We try the following
15817 places in order:
f676971a 15818
6d3d9133 15819 1. The last argument register.
68dfd979
NC
15820 2. A slot on the stack above the frame. (This only
15821 works if the function is not a varargs function).
095bb276
NC
15822 3. Register r3, after pushing the argument registers
15823 onto the stack.
6d3d9133 15824
34ce3d7b
JM
15825 Note - we only need to tell the dwarf2 backend about the SP
15826 adjustment in the second variant; the static chain register
15827 doesn't need to be unwound, as it doesn't contain a value
15828 inherited from the caller. */
d3236b4d 15829
6fb5fa3c 15830 if (df_regs_ever_live_p (3) == false)
d66437c5 15831 insn = emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
095bb276 15832 else if (args_to_push == 0)
68dfd979 15833 {
f0b4bdd5
RE
15834 rtx dwarf;
15835
35596784
AJ
15836 gcc_assert(arm_compute_static_chain_stack_bytes() == 4);
15837 saved_regs += 4;
15838
d66437c5
RE
15839 insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
15840 insn = emit_set_insn (gen_frame_mem (SImode, insn), ip_rtx);
68dfd979 15841 fp_offset = 4;
34ce3d7b
JM
15842
15843 /* Just tell the dwarf backend that we adjusted SP. */
15844 dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
d66437c5
RE
15845 plus_constant (stack_pointer_rtx,
15846 -fp_offset));
34ce3d7b 15847 RTX_FRAME_RELATED_P (insn) = 1;
bbbbb16a 15848 add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
68dfd979
NC
15849 }
15850 else
095bb276
NC
15851 {
15852 /* Store the args on the stack. */
3cb66fd7 15853 if (cfun->machine->uses_anonymous_args)
095bb276
NC
15854 insn = emit_multi_reg_push
15855 ((0xf0 >> (args_to_push / 4)) & 0xf);
15856 else
15857 insn = emit_insn
f676971a 15858 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
095bb276
NC
15859 GEN_INT (- args_to_push)));
15860
15861 RTX_FRAME_RELATED_P (insn) = 1;
15862
15863 saved_pretend_args = 1;
15864 fp_offset = args_to_push;
15865 args_to_push = 0;
15866
15867 /* Now reuse r3 to preserve IP. */
d66437c5 15868 emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
095bb276 15869 }
68dfd979
NC
15870 }
15871
d66437c5
RE
15872 insn = emit_set_insn (ip_rtx,
15873 plus_constant (stack_pointer_rtx, fp_offset));
8e56560e 15874 RTX_FRAME_RELATED_P (insn) = 1;
e2c671ba
RE
15875 }
15876
095bb276 15877 if (args_to_push)
e2c671ba 15878 {
6d3d9133 15879 /* Push the argument registers, or reserve space for them. */
3cb66fd7 15880 if (cfun->machine->uses_anonymous_args)
2c849145 15881 insn = emit_multi_reg_push
095bb276 15882 ((0xf0 >> (args_to_push / 4)) & 0xf);
e2c671ba 15883 else
2c849145 15884 insn = emit_insn
f676971a 15885 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
095bb276 15886 GEN_INT (- args_to_push)));
2c849145 15887 RTX_FRAME_RELATED_P (insn) = 1;
e2c671ba
RE
15888 }
15889
06bea5aa 15890 /* If this is an interrupt service routine, and the link register
ec6237e4
PB
15891 is going to be pushed, and we're not generating extra
15892 push of IP (needed when frame is needed and frame layout if apcs),
06bea5aa
NC
15893 subtracting four from LR now will mean that the function return
15894 can be done with a single instruction. */
3a7731fd 15895 if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
06bea5aa 15896 && (live_regs_mask & (1 << LR_REGNUM)) != 0
ec6237e4 15897 && !(frame_pointer_needed && TARGET_APCS_FRAME)
a15908a4 15898 && TARGET_ARM)
d66437c5
RE
15899 {
15900 rtx lr = gen_rtx_REG (SImode, LR_REGNUM);
15901
15902 emit_set_insn (lr, plus_constant (lr, -4));
15903 }
3a7731fd 15904
e2c671ba
RE
15905 if (live_regs_mask)
15906 {
5848830f 15907 saved_regs += bit_count (live_regs_mask) * 4;
954954d1
PB
15908 if (optimize_size && !frame_pointer_needed
15909 && saved_regs == offsets->saved_regs - offsets->saved_args)
15910 {
15911 /* If no coprocessor registers are being pushed and we don't have
15912 to worry about a frame pointer then push extra registers to
15913 create the stack frame. This is done is a way that does not
15914 alter the frame layout, so is independent of the epilogue. */
15915 int n;
15916 int frame;
15917 n = 0;
15918 while (n < 8 && (live_regs_mask & (1 << n)) == 0)
15919 n++;
15920 frame = offsets->outgoing_args - (offsets->saved_args + saved_regs);
15921 if (frame && n * 4 >= frame)
15922 {
15923 n = frame / 4;
15924 live_regs_mask |= (1 << n) - 1;
15925 saved_regs += frame;
15926 }
15927 }
15928 insn = emit_multi_reg_push (live_regs_mask);
2c849145 15929 RTX_FRAME_RELATED_P (insn) = 1;
e2c671ba 15930 }
d5b7b3ae 15931
6d3d9133 15932 if (! IS_VOLATILE (func_type))
5b3e6663 15933 saved_regs += arm_save_coproc_regs ();
b111229a 15934
5b3e6663
PB
15935 if (frame_pointer_needed && TARGET_ARM)
15936 {
15937 /* Create the new frame pointer. */
ec6237e4 15938 if (TARGET_APCS_FRAME)
9b66ebb1 15939 {
5b3e6663
PB
15940 insn = GEN_INT (-(4 + args_to_push + fp_offset));
15941 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
15942 RTX_FRAME_RELATED_P (insn) = 1;
9b66ebb1 15943
5b3e6663 15944 if (IS_NESTED (func_type))
9b66ebb1 15945 {
5b3e6663 15946 /* Recover the static chain register. */
6fb5fa3c 15947 if (!df_regs_ever_live_p (3)
5b3e6663
PB
15948 || saved_pretend_args)
15949 insn = gen_rtx_REG (SImode, 3);
38173d38 15950 else /* if (crtl->args.pretend_args_size == 0) */
9b66ebb1 15951 {
5b3e6663
PB
15952 insn = plus_constant (hard_frame_pointer_rtx, 4);
15953 insn = gen_frame_mem (SImode, insn);
9b66ebb1 15954 }
5b3e6663
PB
15955 emit_set_insn (ip_rtx, insn);
15956 /* Add a USE to stop propagate_one_insn() from barfing. */
15957 emit_insn (gen_prologue_use (ip_rtx));
9b66ebb1 15958 }
68dfd979 15959 }
ec6237e4
PB
15960 else
15961 {
15962 insn = GEN_INT (saved_regs - 4);
15963 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15964 stack_pointer_rtx, insn));
15965 RTX_FRAME_RELATED_P (insn) = 1;
15966 }
2c849145 15967 }
e2c671ba 15968
55256000
TK
15969 if (flag_stack_usage)
15970 current_function_static_stack_size
15971 = offsets->outgoing_args - offsets->saved_args;
15972
5848830f 15973 if (offsets->outgoing_args != offsets->saved_args + saved_regs)
e2c671ba 15974 {
745b9093
JM
15975 /* This add can produce multiple insns for a large constant, so we
15976 need to get tricky. */
15977 rtx last = get_last_insn ();
5848830f
PB
15978
15979 amount = GEN_INT (offsets->saved_args + saved_regs
15980 - offsets->outgoing_args);
15981
2c849145
JM
15982 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
15983 amount));
745b9093
JM
15984 do
15985 {
15986 last = last ? NEXT_INSN (last) : get_insns ();
15987 RTX_FRAME_RELATED_P (last) = 1;
15988 }
15989 while (last != insn);
e04c2d6c
RE
15990
15991 /* If the frame pointer is needed, emit a special barrier that
15992 will prevent the scheduler from moving stores to the frame
15993 before the stack adjustment. */
15994 if (frame_pointer_needed)
3894f59e
RE
15995 insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
15996 hard_frame_pointer_rtx));
e2c671ba
RE
15997 }
15998
876f13b0 15999
5b3e6663
PB
16000 if (frame_pointer_needed && TARGET_THUMB2)
16001 thumb_set_frame_pointer (offsets);
16002
020a4035 16003 if (flag_pic && arm_pic_register != INVALID_REGNUM)
5b3e6663
PB
16004 {
16005 unsigned long mask;
16006
16007 mask = live_regs_mask;
16008 mask &= THUMB2_WORK_REGS;
16009 if (!IS_NESTED (func_type))
16010 mask |= (1 << IP_REGNUM);
16011 arm_load_pic_register (mask);
16012 }
876f13b0 16013
e2c671ba 16014 /* If we are profiling, make sure no instructions are scheduled before
f5a1b0d2 16015 the call to mcount. Similarly if the user has requested no
74d9c39f
DJ
16016 scheduling in the prolog. Similarly if we want non-call exceptions
16017 using the EABI unwinder, to prevent faulting instructions from being
16018 swapped with a stack adjustment. */
e3b5732b 16019 if (crtl->profile || !TARGET_SCHED_PROLOG
d5fabb58 16020 || (arm_except_unwind_info (&global_options) == UI_TARGET
f0a0390e 16021 && cfun->can_throw_non_call_exceptions))
e2c671ba 16022 emit_insn (gen_blockage ());
6f7ebcbb
NC
16023
16024 /* If the link register is being kept alive, with the return address in it,
16025 then make sure that it does not get reused by the ce2 pass. */
16026 if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
6fb5fa3c 16027 cfun->machine->lr_save_eliminated = 1;
e2c671ba 16028}
cce8749e 16029\f
5b3e6663
PB
16030/* Print condition code to STREAM. Helper function for arm_print_operand. */
16031static void
16032arm_print_condition (FILE *stream)
16033{
16034 if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
16035 {
16036 /* Branch conversion is not implemented for Thumb-2. */
16037 if (TARGET_THUMB)
16038 {
16039 output_operand_lossage ("predicated Thumb instruction");
16040 return;
16041 }
16042 if (current_insn_predicate != NULL)
16043 {
16044 output_operand_lossage
16045 ("predicated instruction in conditional sequence");
16046 return;
16047 }
16048
16049 fputs (arm_condition_codes[arm_current_cc], stream);
16050 }
16051 else if (current_insn_predicate)
16052 {
16053 enum arm_cond_code code;
16054
16055 if (TARGET_THUMB1)
16056 {
16057 output_operand_lossage ("predicated Thumb instruction");
16058 return;
16059 }
16060
16061 code = get_arm_condition_code (current_insn_predicate);
16062 fputs (arm_condition_codes[code], stream);
16063 }
16064}
16065
16066
9997d19d
RE
16067/* If CODE is 'd', then the X is a condition operand and the instruction
16068 should only be executed if the condition is true.
ddd5a7c1 16069 if CODE is 'D', then the X is a condition operand and the instruction
9997d19d
RE
16070 should only be executed if the condition is false: however, if the mode
16071 of the comparison is CCFPEmode, then always execute the instruction -- we
16072 do this because in these circumstances !GE does not necessarily imply LT;
16073 in these cases the instruction pattern will take care to make sure that
16074 an instruction containing %d will follow, thereby undoing the effects of
ddd5a7c1 16075 doing this instruction unconditionally.
9997d19d
RE
16076 If CODE is 'N' then X is a floating point operand that must be negated
16077 before output.
16078 If CODE is 'B' then output a bitwise inverted value of X (a const int).
16079 If X is a REG and CODE is `M', output a ldm/stm style multi-reg. */
944442bb 16080static void
e32bac5b 16081arm_print_operand (FILE *stream, rtx x, int code)
9997d19d
RE
16082{
16083 switch (code)
16084 {
16085 case '@':
f3139301 16086 fputs (ASM_COMMENT_START, stream);
9997d19d
RE
16087 return;
16088
d5b7b3ae
RE
16089 case '_':
16090 fputs (user_label_prefix, stream);
16091 return;
f676971a 16092
9997d19d 16093 case '|':
f3139301 16094 fputs (REGISTER_PREFIX, stream);
9997d19d
RE
16095 return;
16096
16097 case '?':
5b3e6663
PB
16098 arm_print_condition (stream);
16099 return;
cca0a211 16100
5b3e6663
PB
16101 case '(':
16102 /* Nothing in unified syntax, otherwise the current condition code. */
16103 if (!TARGET_UNIFIED_ASM)
16104 arm_print_condition (stream);
16105 break;
16106
16107 case ')':
16108 /* The current condition code in unified syntax, otherwise nothing. */
16109 if (TARGET_UNIFIED_ASM)
16110 arm_print_condition (stream);
16111 break;
16112
16113 case '.':
16114 /* The current condition code for a condition code setting instruction.
7a085dce 16115 Preceded by 's' in unified syntax, otherwise followed by 's'. */
5b3e6663
PB
16116 if (TARGET_UNIFIED_ASM)
16117 {
16118 fputc('s', stream);
16119 arm_print_condition (stream);
cca0a211 16120 }
5b3e6663 16121 else
cca0a211 16122 {
5b3e6663
PB
16123 arm_print_condition (stream);
16124 fputc('s', stream);
cca0a211 16125 }
9997d19d
RE
16126 return;
16127
5b3e6663
PB
16128 case '!':
16129 /* If the instruction is conditionally executed then print
16130 the current condition code, otherwise print 's'. */
16131 gcc_assert (TARGET_THUMB2 && TARGET_UNIFIED_ASM);
16132 if (current_insn_predicate)
16133 arm_print_condition (stream);
16134 else
16135 fputc('s', stream);
16136 break;
16137
88f77cba 16138 /* %# is a "break" sequence. It doesn't output anything, but is used to
cea618ac 16139 separate e.g. operand numbers from following text, if that text consists
88f77cba
JB
16140 of further digits which we don't want to be part of the operand
16141 number. */
16142 case '#':
16143 return;
16144
9997d19d
RE
16145 case 'N':
16146 {
16147 REAL_VALUE_TYPE r;
16148 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
d49b6e1e 16149 r = real_value_negate (&r);
9997d19d
RE
16150 fprintf (stream, "%s", fp_const_from_val (&r));
16151 }
16152 return;
16153
571191af 16154 /* An integer or symbol address without a preceding # sign. */
88f77cba 16155 case 'c':
571191af
PB
16156 switch (GET_CODE (x))
16157 {
16158 case CONST_INT:
16159 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
16160 break;
16161
16162 case SYMBOL_REF:
16163 output_addr_const (stream, x);
16164 break;
16165
16166 default:
16167 gcc_unreachable ();
16168 }
88f77cba
JB
16169 return;
16170
9997d19d
RE
16171 case 'B':
16172 if (GET_CODE (x) == CONST_INT)
4bc74ece
NC
16173 {
16174 HOST_WIDE_INT val;
5895f793 16175 val = ARM_SIGN_EXTEND (~INTVAL (x));
36ba9cb8 16176 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
4bc74ece 16177 }
9997d19d
RE
16178 else
16179 {
16180 putc ('~', stream);
16181 output_addr_const (stream, x);
16182 }
16183 return;
16184
5b3e6663
PB
16185 case 'L':
16186 /* The low 16 bits of an immediate constant. */
16187 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL(x) & 0xffff);
16188 return;
16189
9997d19d
RE
16190 case 'i':
16191 fprintf (stream, "%s", arithmetic_instr (x, 1));
16192 return;
16193
9b6b54e2
NC
16194 /* Truncate Cirrus shift counts. */
16195 case 's':
16196 if (GET_CODE (x) == CONST_INT)
16197 {
16198 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
16199 return;
16200 }
16201 arm_print_operand (stream, x, 0);
16202 return;
16203
9997d19d
RE
16204 case 'I':
16205 fprintf (stream, "%s", arithmetic_instr (x, 0));
16206 return;
16207
16208 case 'S':
16209 {
16210 HOST_WIDE_INT val;
beed32b8
RE
16211 const char *shift;
16212
16213 if (!shift_operator (x, SImode))
16214 {
16215 output_operand_lossage ("invalid shift operand");
16216 break;
16217 }
16218
16219 shift = shift_op (x, &val);
9997d19d 16220
e2c671ba
RE
16221 if (shift)
16222 {
beed32b8 16223 fprintf (stream, ", %s ", shift);
e2c671ba
RE
16224 if (val == -1)
16225 arm_print_operand (stream, XEXP (x, 1), 0);
16226 else
4a0a75dd 16227 fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
e2c671ba 16228 }
9997d19d
RE
16229 }
16230 return;
16231
d5b7b3ae 16232 /* An explanation of the 'Q', 'R' and 'H' register operands:
f676971a 16233
d5b7b3ae
RE
16234 In a pair of registers containing a DI or DF value the 'Q'
16235 operand returns the register number of the register containing
093354e0 16236 the least significant part of the value. The 'R' operand returns
d5b7b3ae
RE
16237 the register number of the register containing the most
16238 significant part of the value.
f676971a 16239
d5b7b3ae
RE
16240 The 'H' operand returns the higher of the two register numbers.
16241 On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
093354e0 16242 same as the 'Q' operand, since the most significant part of the
d5b7b3ae
RE
16243 value is held in the lower number register. The reverse is true
16244 on systems where WORDS_BIG_ENDIAN is false.
f676971a 16245
d5b7b3ae
RE
16246 The purpose of these operands is to distinguish between cases
16247 where the endian-ness of the values is important (for example
16248 when they are added together), and cases where the endian-ness
16249 is irrelevant, but the order of register operations is important.
16250 For example when loading a value from memory into a register
16251 pair, the endian-ness does not matter. Provided that the value
16252 from the lower memory address is put into the lower numbered
16253 register, and the value from the higher address is put into the
16254 higher numbered register, the load will work regardless of whether
16255 the value being loaded is big-wordian or little-wordian. The
16256 order of the two register loads can matter however, if the address
16257 of the memory location is actually held in one of the registers
73160ba9
DJ
16258 being overwritten by the load.
16259
16260 The 'Q' and 'R' constraints are also available for 64-bit
16261 constants. */
c1c2bc04 16262 case 'Q':
73160ba9
DJ
16263 if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
16264 {
16265 rtx part = gen_lowpart (SImode, x);
16266 fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, INTVAL (part));
16267 return;
16268 }
16269
22de4c3d
RE
16270 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16271 {
16272 output_operand_lossage ("invalid operand for code '%c'", code);
16273 return;
16274 }
16275
d5b7b3ae 16276 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
c1c2bc04
RE
16277 return;
16278
9997d19d 16279 case 'R':
73160ba9
DJ
16280 if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
16281 {
16282 enum machine_mode mode = GET_MODE (x);
16283 rtx part;
16284
16285 if (mode == VOIDmode)
16286 mode = DImode;
16287 part = gen_highpart_mode (SImode, mode, x);
16288 fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, INTVAL (part));
16289 return;
16290 }
16291
22de4c3d
RE
16292 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16293 {
16294 output_operand_lossage ("invalid operand for code '%c'", code);
16295 return;
16296 }
16297
d5b7b3ae
RE
16298 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
16299 return;
16300
16301 case 'H':
22de4c3d
RE
16302 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16303 {
16304 output_operand_lossage ("invalid operand for code '%c'", code);
16305 return;
16306 }
16307
d5b7b3ae 16308 asm_fprintf (stream, "%r", REGNO (x) + 1);
9997d19d
RE
16309 return;
16310
88f77cba
JB
16311 case 'J':
16312 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16313 {
16314 output_operand_lossage ("invalid operand for code '%c'", code);
16315 return;
16316 }
16317
16318 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 3 : 2));
16319 return;
16320
16321 case 'K':
16322 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16323 {
16324 output_operand_lossage ("invalid operand for code '%c'", code);
16325 return;
16326 }
16327
16328 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 2 : 3));
16329 return;
16330
9997d19d 16331 case 'm':
f676971a 16332 asm_fprintf (stream, "%r",
d5b7b3ae
RE
16333 GET_CODE (XEXP (x, 0)) == REG
16334 ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
9997d19d
RE
16335 return;
16336
16337 case 'M':
dd18ae56 16338 asm_fprintf (stream, "{%r-%r}",
d5b7b3ae 16339 REGNO (x),
e9d7b180 16340 REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
9997d19d
RE
16341 return;
16342
88f77cba
JB
16343 /* Like 'M', but writing doubleword vector registers, for use by Neon
16344 insns. */
16345 case 'h':
16346 {
16347 int regno = (REGNO (x) - FIRST_VFP_REGNUM) / 2;
16348 int numregs = ARM_NUM_REGS (GET_MODE (x)) / 2;
16349 if (numregs == 1)
16350 asm_fprintf (stream, "{d%d}", regno);
16351 else
16352 asm_fprintf (stream, "{d%d-d%d}", regno, regno + numregs - 1);
16353 }
16354 return;
16355
9997d19d 16356 case 'd':
64e92a26
RE
16357 /* CONST_TRUE_RTX means always -- that's the default. */
16358 if (x == const_true_rtx)
d5b7b3ae 16359 return;
f676971a 16360
22de4c3d
RE
16361 if (!COMPARISON_P (x))
16362 {
16363 output_operand_lossage ("invalid operand for code '%c'", code);
16364 return;
16365 }
16366
defc0463
RE
16367 fputs (arm_condition_codes[get_arm_condition_code (x)],
16368 stream);
9997d19d
RE
16369 return;
16370
16371 case 'D':
112cdef5 16372 /* CONST_TRUE_RTX means not always -- i.e. never. We shouldn't ever
64e92a26
RE
16373 want to do that. */
16374 if (x == const_true_rtx)
22de4c3d 16375 {
4dad0aca 16376 output_operand_lossage ("instruction never executed");
22de4c3d
RE
16377 return;
16378 }
16379 if (!COMPARISON_P (x))
16380 {
16381 output_operand_lossage ("invalid operand for code '%c'", code);
16382 return;
16383 }
d5b7b3ae 16384
defc0463
RE
16385 fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
16386 (get_arm_condition_code (x))],
16387 stream);
9997d19d
RE
16388 return;
16389
9b6b54e2
NC
16390 /* Cirrus registers can be accessed in a variety of ways:
16391 single floating point (f)
16392 double floating point (d)
16393 32bit integer (fx)
16394 64bit integer (dx). */
16395 case 'W': /* Cirrus register in F mode. */
16396 case 'X': /* Cirrus register in D mode. */
16397 case 'Y': /* Cirrus register in FX mode. */
16398 case 'Z': /* Cirrus register in DX mode. */
e6d29d15
NS
16399 gcc_assert (GET_CODE (x) == REG
16400 && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
9b6b54e2
NC
16401
16402 fprintf (stream, "mv%s%s",
16403 code == 'W' ? "f"
16404 : code == 'X' ? "d"
16405 : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
16406
16407 return;
16408
16409 /* Print cirrus register in the mode specified by the register's mode. */
16410 case 'V':
16411 {
16412 int mode = GET_MODE (x);
16413
16414 if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
22de4c3d
RE
16415 {
16416 output_operand_lossage ("invalid operand for code '%c'", code);
16417 return;
16418 }
9b6b54e2
NC
16419
16420 fprintf (stream, "mv%s%s",
16421 mode == DFmode ? "d"
16422 : mode == SImode ? "fx"
16423 : mode == DImode ? "dx"
16424 : "f", reg_names[REGNO (x)] + 2);
16425
16426 return;
16427 }
16428
5a9335ef
NC
16429 case 'U':
16430 if (GET_CODE (x) != REG
16431 || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
16432 || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
16433 /* Bad value for wCG register number. */
22de4c3d
RE
16434 {
16435 output_operand_lossage ("invalid operand for code '%c'", code);
16436 return;
16437 }
16438
5a9335ef
NC
16439 else
16440 fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
16441 return;
16442
16443 /* Print an iWMMXt control register name. */
16444 case 'w':
16445 if (GET_CODE (x) != CONST_INT
16446 || INTVAL (x) < 0
16447 || INTVAL (x) >= 16)
16448 /* Bad value for wC register number. */
22de4c3d
RE
16449 {
16450 output_operand_lossage ("invalid operand for code '%c'", code);
16451 return;
16452 }
16453
5a9335ef
NC
16454 else
16455 {
16456 static const char * wc_reg_names [16] =
16457 {
16458 "wCID", "wCon", "wCSSF", "wCASF",
16459 "wC4", "wC5", "wC6", "wC7",
16460 "wCGR0", "wCGR1", "wCGR2", "wCGR3",
16461 "wC12", "wC13", "wC14", "wC15"
16462 };
f676971a 16463
5a9335ef
NC
16464 fprintf (stream, wc_reg_names [INTVAL (x)]);
16465 }
16466 return;
16467
e0dc3601
PB
16468 /* Print the high single-precision register of a VFP double-precision
16469 register. */
16470 case 'p':
16471 {
16472 int mode = GET_MODE (x);
16473 int regno;
16474
16475 if (GET_MODE_SIZE (mode) != 8 || GET_CODE (x) != REG)
16476 {
16477 output_operand_lossage ("invalid operand for code '%c'", code);
16478 return;
16479 }
16480
16481 regno = REGNO (x);
16482 if (!VFP_REGNO_OK_FOR_DOUBLE (regno))
16483 {
16484 output_operand_lossage ("invalid operand for code '%c'", code);
16485 return;
16486 }
16487
16488 fprintf (stream, "s%d", regno - FIRST_VFP_REGNUM + 1);
16489 }
16490 return;
16491
88f77cba 16492 /* Print a VFP/Neon double precision or quad precision register name. */
9b66ebb1 16493 case 'P':
88f77cba 16494 case 'q':
9b66ebb1
PB
16495 {
16496 int mode = GET_MODE (x);
88f77cba
JB
16497 int is_quad = (code == 'q');
16498 int regno;
9b66ebb1 16499
88f77cba 16500 if (GET_MODE_SIZE (mode) != (is_quad ? 16 : 8))
22de4c3d
RE
16501 {
16502 output_operand_lossage ("invalid operand for code '%c'", code);
16503 return;
16504 }
9b66ebb1
PB
16505
16506 if (GET_CODE (x) != REG
16507 || !IS_VFP_REGNUM (REGNO (x)))
22de4c3d
RE
16508 {
16509 output_operand_lossage ("invalid operand for code '%c'", code);
16510 return;
16511 }
9b66ebb1 16512
88f77cba
JB
16513 regno = REGNO (x);
16514 if ((is_quad && !NEON_REGNO_OK_FOR_QUAD (regno))
16515 || (!is_quad && !VFP_REGNO_OK_FOR_DOUBLE (regno)))
22de4c3d
RE
16516 {
16517 output_operand_lossage ("invalid operand for code '%c'", code);
16518 return;
16519 }
9b66ebb1 16520
88f77cba
JB
16521 fprintf (stream, "%c%d", is_quad ? 'q' : 'd',
16522 (regno - FIRST_VFP_REGNUM) >> (is_quad ? 2 : 1));
16523 }
16524 return;
16525
16526 /* These two codes print the low/high doubleword register of a Neon quad
16527 register, respectively. For pair-structure types, can also print
16528 low/high quadword registers. */
16529 case 'e':
16530 case 'f':
16531 {
16532 int mode = GET_MODE (x);
16533 int regno;
16534
16535 if ((GET_MODE_SIZE (mode) != 16
16536 && GET_MODE_SIZE (mode) != 32) || GET_CODE (x) != REG)
16537 {
16538 output_operand_lossage ("invalid operand for code '%c'", code);
16539 return;
16540 }
16541
16542 regno = REGNO (x);
16543 if (!NEON_REGNO_OK_FOR_QUAD (regno))
16544 {
16545 output_operand_lossage ("invalid operand for code '%c'", code);
16546 return;
16547 }
16548
16549 if (GET_MODE_SIZE (mode) == 16)
16550 fprintf (stream, "d%d", ((regno - FIRST_VFP_REGNUM) >> 1)
16551 + (code == 'f' ? 1 : 0));
16552 else
16553 fprintf (stream, "q%d", ((regno - FIRST_VFP_REGNUM) >> 2)
16554 + (code == 'f' ? 1 : 0));
9b66ebb1
PB
16555 }
16556 return;
16557
f1adb0a9
JB
16558 /* Print a VFPv3 floating-point constant, represented as an integer
16559 index. */
16560 case 'G':
16561 {
16562 int index = vfp3_const_double_index (x);
16563 gcc_assert (index != -1);
16564 fprintf (stream, "%d", index);
16565 }
16566 return;
16567
88f77cba
JB
16568 /* Print bits representing opcode features for Neon.
16569
16570 Bit 0 is 1 for signed, 0 for unsigned. Floats count as signed
16571 and polynomials as unsigned.
16572
16573 Bit 1 is 1 for floats and polynomials, 0 for ordinary integers.
16574
16575 Bit 2 is 1 for rounding functions, 0 otherwise. */
16576
16577 /* Identify the type as 's', 'u', 'p' or 'f'. */
16578 case 'T':
16579 {
16580 HOST_WIDE_INT bits = INTVAL (x);
16581 fputc ("uspf"[bits & 3], stream);
16582 }
16583 return;
16584
16585 /* Likewise, but signed and unsigned integers are both 'i'. */
16586 case 'F':
16587 {
16588 HOST_WIDE_INT bits = INTVAL (x);
16589 fputc ("iipf"[bits & 3], stream);
16590 }
16591 return;
16592
16593 /* As for 'T', but emit 'u' instead of 'p'. */
16594 case 't':
16595 {
16596 HOST_WIDE_INT bits = INTVAL (x);
16597 fputc ("usuf"[bits & 3], stream);
16598 }
16599 return;
16600
16601 /* Bit 2: rounding (vs none). */
16602 case 'O':
16603 {
16604 HOST_WIDE_INT bits = INTVAL (x);
16605 fputs ((bits & 4) != 0 ? "r" : "", stream);
16606 }
16607 return;
16608
dc34db56
PB
16609 /* Memory operand for vld1/vst1 instruction. */
16610 case 'A':
16611 {
16612 rtx addr;
16613 bool postinc = FALSE;
6308e208 16614 unsigned align, memsize, align_bits;
c452684d 16615
dc34db56
PB
16616 gcc_assert (GET_CODE (x) == MEM);
16617 addr = XEXP (x, 0);
16618 if (GET_CODE (addr) == POST_INC)
16619 {
16620 postinc = 1;
16621 addr = XEXP (addr, 0);
16622 }
c452684d
JB
16623 asm_fprintf (stream, "[%r", REGNO (addr));
16624
16625 /* We know the alignment of this access, so we can emit a hint in the
16626 instruction (for some alignments) as an aid to the memory subsystem
16627 of the target. */
16628 align = MEM_ALIGN (x) >> 3;
6308e208 16629 memsize = INTVAL (MEM_SIZE (x));
c452684d
JB
16630
16631 /* Only certain alignment specifiers are supported by the hardware. */
6308e208 16632 if (memsize == 16 && (align % 32) == 0)
c452684d 16633 align_bits = 256;
6308e208 16634 else if ((memsize == 8 || memsize == 16) && (align % 16) == 0)
c452684d
JB
16635 align_bits = 128;
16636 else if ((align % 8) == 0)
16637 align_bits = 64;
16638 else
16639 align_bits = 0;
16640
16641 if (align_bits != 0)
16642 asm_fprintf (stream, ":%d", align_bits);
16643
16644 asm_fprintf (stream, "]");
16645
dc34db56
PB
16646 if (postinc)
16647 fputs("!", stream);
16648 }
16649 return;
16650
029e79eb
MS
16651 case 'C':
16652 {
16653 rtx addr;
16654
16655 gcc_assert (GET_CODE (x) == MEM);
16656 addr = XEXP (x, 0);
16657 gcc_assert (GET_CODE (addr) == REG);
16658 asm_fprintf (stream, "[%r]", REGNO (addr));
16659 }
16660 return;
16661
814a4c3b
DJ
16662 /* Translate an S register number into a D register number and element index. */
16663 case 'y':
16664 {
16665 int mode = GET_MODE (x);
16666 int regno;
16667
16668 if (GET_MODE_SIZE (mode) != 4 || GET_CODE (x) != REG)
16669 {
16670 output_operand_lossage ("invalid operand for code '%c'", code);
16671 return;
16672 }
16673
16674 regno = REGNO (x);
16675 if (!VFP_REGNO_OK_FOR_SINGLE (regno))
16676 {
16677 output_operand_lossage ("invalid operand for code '%c'", code);
16678 return;
16679 }
16680
16681 regno = regno - FIRST_VFP_REGNUM;
16682 fprintf (stream, "d%d[%d]", regno / 2, regno % 2);
16683 }
16684 return;
16685
0fd8c3ad
SL
16686 /* Register specifier for vld1.16/vst1.16. Translate the S register
16687 number into a D register number and element index. */
16688 case 'z':
16689 {
16690 int mode = GET_MODE (x);
16691 int regno;
16692
16693 if (GET_MODE_SIZE (mode) != 2 || GET_CODE (x) != REG)
16694 {
16695 output_operand_lossage ("invalid operand for code '%c'", code);
16696 return;
16697 }
16698
16699 regno = REGNO (x);
16700 if (!VFP_REGNO_OK_FOR_SINGLE (regno))
16701 {
16702 output_operand_lossage ("invalid operand for code '%c'", code);
16703 return;
16704 }
16705
16706 regno = regno - FIRST_VFP_REGNUM;
16707 fprintf (stream, "d%d[%d]", regno/2, ((regno % 2) ? 2 : 0));
16708 }
16709 return;
16710
9997d19d
RE
16711 default:
16712 if (x == 0)
22de4c3d
RE
16713 {
16714 output_operand_lossage ("missing operand");
16715 return;
16716 }
9997d19d 16717
e6d29d15 16718 switch (GET_CODE (x))
9997d19d 16719 {
e6d29d15
NS
16720 case REG:
16721 asm_fprintf (stream, "%r", REGNO (x));
16722 break;
16723
16724 case MEM:
9997d19d
RE
16725 output_memory_reference_mode = GET_MODE (x);
16726 output_address (XEXP (x, 0));
e6d29d15
NS
16727 break;
16728
16729 case CONST_DOUBLE:
88f77cba
JB
16730 if (TARGET_NEON)
16731 {
16732 char fpstr[20];
16733 real_to_decimal (fpstr, CONST_DOUBLE_REAL_VALUE (x),
16734 sizeof (fpstr), 0, 1);
16735 fprintf (stream, "#%s", fpstr);
16736 }
16737 else
16738 fprintf (stream, "#%s", fp_immediate_constant (x));
e6d29d15
NS
16739 break;
16740
16741 default:
16742 gcc_assert (GET_CODE (x) != NEG);
9997d19d 16743 fputc ('#', stream);
d58bc084
NS
16744 if (GET_CODE (x) == HIGH)
16745 {
16746 fputs (":lower16:", stream);
16747 x = XEXP (x, 0);
16748 }
16749
9997d19d 16750 output_addr_const (stream, x);
e6d29d15 16751 break;
9997d19d
RE
16752 }
16753 }
16754}
cce8749e 16755\f
944442bb
NF
16756/* Target hook for printing a memory address. */
16757static void
16758arm_print_operand_address (FILE *stream, rtx x)
16759{
16760 if (TARGET_32BIT)
16761 {
16762 int is_minus = GET_CODE (x) == MINUS;
16763
16764 if (GET_CODE (x) == REG)
16765 asm_fprintf (stream, "[%r, #0]", REGNO (x));
16766 else if (GET_CODE (x) == PLUS || is_minus)
16767 {
16768 rtx base = XEXP (x, 0);
16769 rtx index = XEXP (x, 1);
16770 HOST_WIDE_INT offset = 0;
16771 if (GET_CODE (base) != REG
16772 || (GET_CODE (index) == REG && REGNO (index) == SP_REGNUM))
16773 {
16774 /* Ensure that BASE is a register. */
16775 /* (one of them must be). */
16776 /* Also ensure the SP is not used as in index register. */
16777 rtx temp = base;
16778 base = index;
16779 index = temp;
16780 }
16781 switch (GET_CODE (index))
16782 {
16783 case CONST_INT:
16784 offset = INTVAL (index);
16785 if (is_minus)
16786 offset = -offset;
16787 asm_fprintf (stream, "[%r, #%wd]",
16788 REGNO (base), offset);
16789 break;
16790
16791 case REG:
16792 asm_fprintf (stream, "[%r, %s%r]",
16793 REGNO (base), is_minus ? "-" : "",
16794 REGNO (index));
16795 break;
16796
16797 case MULT:
16798 case ASHIFTRT:
16799 case LSHIFTRT:
16800 case ASHIFT:
16801 case ROTATERT:
16802 {
16803 asm_fprintf (stream, "[%r, %s%r",
16804 REGNO (base), is_minus ? "-" : "",
16805 REGNO (XEXP (index, 0)));
16806 arm_print_operand (stream, index, 'S');
16807 fputs ("]", stream);
16808 break;
16809 }
16810
16811 default:
16812 gcc_unreachable ();
16813 }
16814 }
16815 else if (GET_CODE (x) == PRE_INC || GET_CODE (x) == POST_INC
16816 || GET_CODE (x) == PRE_DEC || GET_CODE (x) == POST_DEC)
16817 {
16818 extern enum machine_mode output_memory_reference_mode;
16819
16820 gcc_assert (GET_CODE (XEXP (x, 0)) == REG);
16821
16822 if (GET_CODE (x) == PRE_DEC || GET_CODE (x) == PRE_INC)
16823 asm_fprintf (stream, "[%r, #%s%d]!",
16824 REGNO (XEXP (x, 0)),
16825 GET_CODE (x) == PRE_DEC ? "-" : "",
16826 GET_MODE_SIZE (output_memory_reference_mode));
16827 else
16828 asm_fprintf (stream, "[%r], #%s%d",
16829 REGNO (XEXP (x, 0)),
16830 GET_CODE (x) == POST_DEC ? "-" : "",
16831 GET_MODE_SIZE (output_memory_reference_mode));
16832 }
16833 else if (GET_CODE (x) == PRE_MODIFY)
16834 {
16835 asm_fprintf (stream, "[%r, ", REGNO (XEXP (x, 0)));
16836 if (GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
16837 asm_fprintf (stream, "#%wd]!",
16838 INTVAL (XEXP (XEXP (x, 1), 1)));
16839 else
16840 asm_fprintf (stream, "%r]!",
16841 REGNO (XEXP (XEXP (x, 1), 1)));
16842 }
16843 else if (GET_CODE (x) == POST_MODIFY)
16844 {
16845 asm_fprintf (stream, "[%r], ", REGNO (XEXP (x, 0)));
16846 if (GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
16847 asm_fprintf (stream, "#%wd",
16848 INTVAL (XEXP (XEXP (x, 1), 1)));
16849 else
16850 asm_fprintf (stream, "%r",
16851 REGNO (XEXP (XEXP (x, 1), 1)));
16852 }
16853 else output_addr_const (stream, x);
16854 }
16855 else
16856 {
16857 if (GET_CODE (x) == REG)
16858 asm_fprintf (stream, "[%r]", REGNO (x));
16859 else if (GET_CODE (x) == POST_INC)
16860 asm_fprintf (stream, "%r!", REGNO (XEXP (x, 0)));
16861 else if (GET_CODE (x) == PLUS)
16862 {
16863 gcc_assert (GET_CODE (XEXP (x, 0)) == REG);
16864 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
16865 asm_fprintf (stream, "[%r, #%wd]",
16866 REGNO (XEXP (x, 0)),
16867 INTVAL (XEXP (x, 1)));
16868 else
16869 asm_fprintf (stream, "[%r, %r]",
16870 REGNO (XEXP (x, 0)),
16871 REGNO (XEXP (x, 1)));
16872 }
16873 else
16874 output_addr_const (stream, x);
16875 }
16876}
16877\f
16878/* Target hook for indicating whether a punctuation character for
16879 TARGET_PRINT_OPERAND is valid. */
16880static bool
16881arm_print_operand_punct_valid_p (unsigned char code)
16882{
16883 return (code == '@' || code == '|' || code == '.'
16884 || code == '(' || code == ')' || code == '#'
16885 || (TARGET_32BIT && (code == '?'))
16886 || (TARGET_THUMB2 && (code == '!'))
16887 || (TARGET_THUMB && (code == '_')));
16888}
16889\f
301d03af
RS
16890/* Target hook for assembling integer objects. The ARM version needs to
16891 handle word-sized values specially. */
301d03af 16892static bool
e32bac5b 16893arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
301d03af 16894{
88f77cba
JB
16895 enum machine_mode mode;
16896
301d03af
RS
16897 if (size == UNITS_PER_WORD && aligned_p)
16898 {
16899 fputs ("\t.word\t", asm_out_file);
16900 output_addr_const (asm_out_file, x);
16901
16902 /* Mark symbols as position independent. We only do this in the
d6b4baa4 16903 .text segment, not in the .data segment. */
301d03af
RS
16904 if (NEED_GOT_RELOC && flag_pic && making_const_table &&
16905 (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
16906 {
9403b7f7
RS
16907 /* See legitimize_pic_address for an explanation of the
16908 TARGET_VXWORKS_RTP check. */
16909 if (TARGET_VXWORKS_RTP
16910 || (GET_CODE (x) == SYMBOL_REF && !SYMBOL_REF_LOCAL_P (x)))
301d03af 16911 fputs ("(GOT)", asm_out_file);
9403b7f7
RS
16912 else
16913 fputs ("(GOTOFF)", asm_out_file);
301d03af
RS
16914 }
16915 fputc ('\n', asm_out_file);
16916 return true;
16917 }
1d6e90ac 16918
88f77cba
JB
16919 mode = GET_MODE (x);
16920
16921 if (arm_vector_mode_supported_p (mode))
5a9335ef
NC
16922 {
16923 int i, units;
16924
e6d29d15 16925 gcc_assert (GET_CODE (x) == CONST_VECTOR);
5a9335ef
NC
16926
16927 units = CONST_VECTOR_NUNITS (x);
88f77cba 16928 size = GET_MODE_SIZE (GET_MODE_INNER (mode));
5a9335ef 16929
88f77cba
JB
16930 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
16931 for (i = 0; i < units; i++)
16932 {
874d42b9 16933 rtx elt = CONST_VECTOR_ELT (x, i);
88f77cba
JB
16934 assemble_integer
16935 (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
16936 }
16937 else
16938 for (i = 0; i < units; i++)
16939 {
16940 rtx elt = CONST_VECTOR_ELT (x, i);
16941 REAL_VALUE_TYPE rval;
5a9335ef 16942
88f77cba
JB
16943 REAL_VALUE_FROM_CONST_DOUBLE (rval, elt);
16944
16945 assemble_real
16946 (rval, GET_MODE_INNER (mode),
16947 i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT);
16948 }
5a9335ef
NC
16949
16950 return true;
16951 }
16952
301d03af
RS
16953 return default_assemble_integer (x, size, aligned_p);
16954}
7abc66b1 16955
7abc66b1 16956static void
9f296620 16957arm_elf_asm_cdtor (rtx symbol, int priority, bool is_ctor)
7abc66b1 16958{
50603eed
PB
16959 section *s;
16960
7abc66b1
JB
16961 if (!TARGET_AAPCS_BASED)
16962 {
9f296620
MM
16963 (is_ctor ?
16964 default_named_section_asm_out_constructor
16965 : default_named_section_asm_out_destructor) (symbol, priority);
7abc66b1
JB
16966 return;
16967 }
16968
16969 /* Put these in the .init_array section, using a special relocation. */
50603eed
PB
16970 if (priority != DEFAULT_INIT_PRIORITY)
16971 {
16972 char buf[18];
9f296620
MM
16973 sprintf (buf, "%s.%.5u",
16974 is_ctor ? ".init_array" : ".fini_array",
16975 priority);
50603eed
PB
16976 s = get_section (buf, SECTION_WRITE, NULL_TREE);
16977 }
9f296620 16978 else if (is_ctor)
50603eed 16979 s = ctors_section;
9f296620
MM
16980 else
16981 s = dtors_section;
50603eed
PB
16982
16983 switch_to_section (s);
7abc66b1
JB
16984 assemble_align (POINTER_SIZE);
16985 fputs ("\t.word\t", asm_out_file);
16986 output_addr_const (asm_out_file, symbol);
16987 fputs ("(target1)\n", asm_out_file);
16988}
9f296620
MM
16989
16990/* Add a function to the list of static constructors. */
16991
16992static void
16993arm_elf_asm_constructor (rtx symbol, int priority)
16994{
16995 arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/true);
16996}
16997
16998/* Add a function to the list of static destructors. */
16999
17000static void
17001arm_elf_asm_destructor (rtx symbol, int priority)
17002{
17003 arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/false);
17004}
301d03af 17005\f
cce8749e
CH
17006/* A finite state machine takes care of noticing whether or not instructions
17007 can be conditionally executed, and thus decrease execution time and code
17008 size by deleting branch instructions. The fsm is controlled by
17009 final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE. */
17010
17011/* The state of the fsm controlling condition codes are:
17012 0: normal, do nothing special
17013 1: make ASM_OUTPUT_OPCODE not output this instruction
17014 2: make ASM_OUTPUT_OPCODE not output this instruction
17015 3: make instructions conditional
17016 4: make instructions conditional
17017
17018 State transitions (state->state by whom under condition):
17019 0 -> 1 final_prescan_insn if the `target' is a label
17020 0 -> 2 final_prescan_insn if the `target' is an unconditional branch
17021 1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
17022 2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
4977bab6 17023 3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
cce8749e
CH
17024 (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
17025 4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
17026 (the target insn is arm_target_insn).
17027
ff9940b0
RE
17028 If the jump clobbers the conditions then we use states 2 and 4.
17029
17030 A similar thing can be done with conditional return insns.
17031
cce8749e
CH
17032 XXX In case the `target' is an unconditional branch, this conditionalising
17033 of the instructions always reduces code size, but not always execution
17034 time. But then, I want to reduce the code size to somewhere near what
17035 /bin/cc produces. */
17036
5b3e6663
PB
17037/* In addition to this, state is maintained for Thumb-2 COND_EXEC
17038 instructions. When a COND_EXEC instruction is seen the subsequent
17039 instructions are scanned so that multiple conditional instructions can be
17040 combined into a single IT block. arm_condexec_count and arm_condexec_mask
17041 specify the length and true/false mask for the IT block. These will be
17042 decremented/zeroed by arm_asm_output_opcode as the insns are output. */
17043
cce8749e
CH
17044/* Returns the index of the ARM condition code string in
17045 `arm_condition_codes'. COMPARISON should be an rtx like
17046 `(eq (...) (...))'. */
84ed5e79 17047static enum arm_cond_code
e32bac5b 17048get_arm_condition_code (rtx comparison)
cce8749e 17049{
5165176d 17050 enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
81f40b79 17051 enum arm_cond_code code;
1d6e90ac 17052 enum rtx_code comp_code = GET_CODE (comparison);
5165176d
RE
17053
17054 if (GET_MODE_CLASS (mode) != MODE_CC)
84ed5e79 17055 mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
5165176d
RE
17056 XEXP (comparison, 1));
17057
17058 switch (mode)
cce8749e 17059 {
84ed5e79
RE
17060 case CC_DNEmode: code = ARM_NE; goto dominance;
17061 case CC_DEQmode: code = ARM_EQ; goto dominance;
17062 case CC_DGEmode: code = ARM_GE; goto dominance;
17063 case CC_DGTmode: code = ARM_GT; goto dominance;
17064 case CC_DLEmode: code = ARM_LE; goto dominance;
17065 case CC_DLTmode: code = ARM_LT; goto dominance;
17066 case CC_DGEUmode: code = ARM_CS; goto dominance;
17067 case CC_DGTUmode: code = ARM_HI; goto dominance;
17068 case CC_DLEUmode: code = ARM_LS; goto dominance;
17069 case CC_DLTUmode: code = ARM_CC;
17070
17071 dominance:
e6d29d15 17072 gcc_assert (comp_code == EQ || comp_code == NE);
84ed5e79
RE
17073
17074 if (comp_code == EQ)
17075 return ARM_INVERSE_CONDITION_CODE (code);
17076 return code;
17077
5165176d 17078 case CC_NOOVmode:
84ed5e79 17079 switch (comp_code)
5165176d 17080 {
84ed5e79
RE
17081 case NE: return ARM_NE;
17082 case EQ: return ARM_EQ;
17083 case GE: return ARM_PL;
17084 case LT: return ARM_MI;
e6d29d15 17085 default: gcc_unreachable ();
5165176d
RE
17086 }
17087
17088 case CC_Zmode:
84ed5e79 17089 switch (comp_code)
5165176d 17090 {
84ed5e79
RE
17091 case NE: return ARM_NE;
17092 case EQ: return ARM_EQ;
e6d29d15 17093 default: gcc_unreachable ();
5165176d
RE
17094 }
17095
defc0463
RE
17096 case CC_Nmode:
17097 switch (comp_code)
17098 {
17099 case NE: return ARM_MI;
17100 case EQ: return ARM_PL;
e6d29d15 17101 default: gcc_unreachable ();
defc0463
RE
17102 }
17103
5165176d 17104 case CCFPEmode:
e45b72c4
RE
17105 case CCFPmode:
17106 /* These encodings assume that AC=1 in the FPA system control
17107 byte. This allows us to handle all cases except UNEQ and
17108 LTGT. */
84ed5e79
RE
17109 switch (comp_code)
17110 {
17111 case GE: return ARM_GE;
17112 case GT: return ARM_GT;
17113 case LE: return ARM_LS;
17114 case LT: return ARM_MI;
e45b72c4
RE
17115 case NE: return ARM_NE;
17116 case EQ: return ARM_EQ;
17117 case ORDERED: return ARM_VC;
17118 case UNORDERED: return ARM_VS;
17119 case UNLT: return ARM_LT;
17120 case UNLE: return ARM_LE;
17121 case UNGT: return ARM_HI;
17122 case UNGE: return ARM_PL;
17123 /* UNEQ and LTGT do not have a representation. */
17124 case UNEQ: /* Fall through. */
17125 case LTGT: /* Fall through. */
e6d29d15 17126 default: gcc_unreachable ();
84ed5e79
RE
17127 }
17128
17129 case CC_SWPmode:
17130 switch (comp_code)
17131 {
17132 case NE: return ARM_NE;
17133 case EQ: return ARM_EQ;
17134 case GE: return ARM_LE;
17135 case GT: return ARM_LT;
17136 case LE: return ARM_GE;
17137 case LT: return ARM_GT;
17138 case GEU: return ARM_LS;
17139 case GTU: return ARM_CC;
17140 case LEU: return ARM_CS;
17141 case LTU: return ARM_HI;
e6d29d15 17142 default: gcc_unreachable ();
84ed5e79
RE
17143 }
17144
bd9c7e23
RE
17145 case CC_Cmode:
17146 switch (comp_code)
18e8200f
BS
17147 {
17148 case LTU: return ARM_CS;
17149 case GEU: return ARM_CC;
17150 default: gcc_unreachable ();
17151 }
17152
73160ba9
DJ
17153 case CC_CZmode:
17154 switch (comp_code)
17155 {
17156 case NE: return ARM_NE;
17157 case EQ: return ARM_EQ;
17158 case GEU: return ARM_CS;
17159 case GTU: return ARM_HI;
17160 case LEU: return ARM_LS;
17161 case LTU: return ARM_CC;
17162 default: gcc_unreachable ();
17163 }
17164
17165 case CC_NCVmode:
17166 switch (comp_code)
17167 {
17168 case GE: return ARM_GE;
17169 case LT: return ARM_LT;
17170 case GEU: return ARM_CS;
17171 case LTU: return ARM_CC;
17172 default: gcc_unreachable ();
17173 }
17174
5165176d 17175 case CCmode:
84ed5e79 17176 switch (comp_code)
5165176d 17177 {
84ed5e79
RE
17178 case NE: return ARM_NE;
17179 case EQ: return ARM_EQ;
17180 case GE: return ARM_GE;
17181 case GT: return ARM_GT;
17182 case LE: return ARM_LE;
17183 case LT: return ARM_LT;
17184 case GEU: return ARM_CS;
17185 case GTU: return ARM_HI;
17186 case LEU: return ARM_LS;
17187 case LTU: return ARM_CC;
e6d29d15 17188 default: gcc_unreachable ();
5165176d
RE
17189 }
17190
e6d29d15 17191 default: gcc_unreachable ();
cce8749e 17192 }
f3bb6135 17193}
cce8749e 17194
44c7bd63 17195/* Tell arm_asm_output_opcode to output IT blocks for conditionally executed
5b3e6663
PB
17196 instructions. */
17197void
17198thumb2_final_prescan_insn (rtx insn)
17199{
17200 rtx first_insn = insn;
17201 rtx body = PATTERN (insn);
17202 rtx predicate;
17203 enum arm_cond_code code;
17204 int n;
17205 int mask;
17206
17207 /* Remove the previous insn from the count of insns to be output. */
17208 if (arm_condexec_count)
17209 arm_condexec_count--;
17210
17211 /* Nothing to do if we are already inside a conditional block. */
17212 if (arm_condexec_count)
17213 return;
17214
17215 if (GET_CODE (body) != COND_EXEC)
17216 return;
17217
17218 /* Conditional jumps are implemented directly. */
17219 if (GET_CODE (insn) == JUMP_INSN)
17220 return;
17221
17222 predicate = COND_EXEC_TEST (body);
17223 arm_current_cc = get_arm_condition_code (predicate);
17224
17225 n = get_attr_ce_count (insn);
17226 arm_condexec_count = 1;
17227 arm_condexec_mask = (1 << n) - 1;
17228 arm_condexec_masklen = n;
17229 /* See if subsequent instructions can be combined into the same block. */
17230 for (;;)
17231 {
17232 insn = next_nonnote_insn (insn);
17233
17234 /* Jumping into the middle of an IT block is illegal, so a label or
17235 barrier terminates the block. */
17236 if (GET_CODE (insn) != INSN && GET_CODE(insn) != JUMP_INSN)
17237 break;
17238
17239 body = PATTERN (insn);
17240 /* USE and CLOBBER aren't really insns, so just skip them. */
17241 if (GET_CODE (body) == USE
17242 || GET_CODE (body) == CLOBBER)
5b0202af 17243 continue;
5b3e6663 17244
7a085dce 17245 /* ??? Recognize conditional jumps, and combine them with IT blocks. */
5b3e6663
PB
17246 if (GET_CODE (body) != COND_EXEC)
17247 break;
17248 /* Allow up to 4 conditionally executed instructions in a block. */
17249 n = get_attr_ce_count (insn);
17250 if (arm_condexec_masklen + n > 4)
17251 break;
17252
17253 predicate = COND_EXEC_TEST (body);
17254 code = get_arm_condition_code (predicate);
17255 mask = (1 << n) - 1;
17256 if (arm_current_cc == code)
17257 arm_condexec_mask |= (mask << arm_condexec_masklen);
17258 else if (arm_current_cc != ARM_INVERSE_CONDITION_CODE(code))
17259 break;
17260
17261 arm_condexec_count++;
17262 arm_condexec_masklen += n;
17263
17264 /* A jump must be the last instruction in a conditional block. */
17265 if (GET_CODE(insn) == JUMP_INSN)
17266 break;
17267 }
17268 /* Restore recog_data (getting the attributes of other insns can
17269 destroy this array, but final.c assumes that it remains intact
17270 across this call). */
17271 extract_constrain_insn_cached (first_insn);
17272}
17273
cce8749e 17274void
e32bac5b 17275arm_final_prescan_insn (rtx insn)
cce8749e
CH
17276{
17277 /* BODY will hold the body of INSN. */
1d6e90ac 17278 rtx body = PATTERN (insn);
cce8749e
CH
17279
17280 /* This will be 1 if trying to repeat the trick, and things need to be
17281 reversed if it appears to fail. */
17282 int reverse = 0;
17283
6354dc9b 17284 /* If we start with a return insn, we only succeed if we find another one. */
ff9940b0 17285 int seeking_return = 0;
f676971a 17286
cce8749e
CH
17287 /* START_INSN will hold the insn from where we start looking. This is the
17288 first insn after the following code_label if REVERSE is true. */
17289 rtx start_insn = insn;
17290
17291 /* If in state 4, check if the target branch is reached, in order to
17292 change back to state 0. */
17293 if (arm_ccfsm_state == 4)
17294 {
17295 if (insn == arm_target_insn)
f5a1b0d2
NC
17296 {
17297 arm_target_insn = NULL;
17298 arm_ccfsm_state = 0;
17299 }
cce8749e
CH
17300 return;
17301 }
17302
17303 /* If in state 3, it is possible to repeat the trick, if this insn is an
17304 unconditional branch to a label, and immediately following this branch
17305 is the previous target label which is only used once, and the label this
17306 branch jumps to is not too far off. */
17307 if (arm_ccfsm_state == 3)
17308 {
17309 if (simplejump_p (insn))
17310 {
17311 start_insn = next_nonnote_insn (start_insn);
17312 if (GET_CODE (start_insn) == BARRIER)
17313 {
17314 /* XXX Isn't this always a barrier? */
17315 start_insn = next_nonnote_insn (start_insn);
17316 }
17317 if (GET_CODE (start_insn) == CODE_LABEL
17318 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
17319 && LABEL_NUSES (start_insn) == 1)
17320 reverse = TRUE;
17321 else
17322 return;
17323 }
ff9940b0
RE
17324 else if (GET_CODE (body) == RETURN)
17325 {
17326 start_insn = next_nonnote_insn (start_insn);
17327 if (GET_CODE (start_insn) == BARRIER)
17328 start_insn = next_nonnote_insn (start_insn);
17329 if (GET_CODE (start_insn) == CODE_LABEL
17330 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
17331 && LABEL_NUSES (start_insn) == 1)
17332 {
17333 reverse = TRUE;
17334 seeking_return = 1;
17335 }
17336 else
17337 return;
17338 }
cce8749e
CH
17339 else
17340 return;
17341 }
17342
e6d29d15 17343 gcc_assert (!arm_ccfsm_state || reverse);
cce8749e
CH
17344 if (GET_CODE (insn) != JUMP_INSN)
17345 return;
17346
f676971a 17347 /* This jump might be paralleled with a clobber of the condition codes
ff9940b0
RE
17348 the jump should always come first */
17349 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
17350 body = XVECEXP (body, 0, 0);
17351
cce8749e
CH
17352 if (reverse
17353 || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
17354 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
17355 {
bd9c7e23
RE
17356 int insns_skipped;
17357 int fail = FALSE, succeed = FALSE;
cce8749e
CH
17358 /* Flag which part of the IF_THEN_ELSE is the LABEL_REF. */
17359 int then_not_else = TRUE;
ff9940b0 17360 rtx this_insn = start_insn, label = 0;
cce8749e
CH
17361
17362 /* Register the insn jumped to. */
17363 if (reverse)
ff9940b0
RE
17364 {
17365 if (!seeking_return)
17366 label = XEXP (SET_SRC (body), 0);
17367 }
cce8749e
CH
17368 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
17369 label = XEXP (XEXP (SET_SRC (body), 1), 0);
17370 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
17371 {
17372 label = XEXP (XEXP (SET_SRC (body), 2), 0);
17373 then_not_else = FALSE;
17374 }
ff9940b0
RE
17375 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
17376 seeking_return = 1;
17377 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
17378 {
17379 seeking_return = 1;
17380 then_not_else = FALSE;
17381 }
cce8749e 17382 else
e6d29d15 17383 gcc_unreachable ();
cce8749e
CH
17384
17385 /* See how many insns this branch skips, and what kind of insns. If all
17386 insns are okay, and the label or unconditional branch to the same
17387 label is not too far away, succeed. */
17388 for (insns_skipped = 0;
b36ba79f 17389 !fail && !succeed && insns_skipped++ < max_insns_skipped;)
cce8749e
CH
17390 {
17391 rtx scanbody;
17392
17393 this_insn = next_nonnote_insn (this_insn);
17394 if (!this_insn)
17395 break;
17396
cce8749e
CH
17397 switch (GET_CODE (this_insn))
17398 {
17399 case CODE_LABEL:
17400 /* Succeed if it is the target label, otherwise fail since
17401 control falls in from somewhere else. */
17402 if (this_insn == label)
17403 {
accbd151 17404 arm_ccfsm_state = 1;
cce8749e
CH
17405 succeed = TRUE;
17406 }
17407 else
17408 fail = TRUE;
17409 break;
17410
ff9940b0 17411 case BARRIER:
cce8749e 17412 /* Succeed if the following insn is the target label.
f676971a
EC
17413 Otherwise fail.
17414 If return insns are used then the last insn in a function
6354dc9b 17415 will be a barrier. */
cce8749e 17416 this_insn = next_nonnote_insn (this_insn);
ff9940b0 17417 if (this_insn && this_insn == label)
cce8749e 17418 {
accbd151 17419 arm_ccfsm_state = 1;
cce8749e
CH
17420 succeed = TRUE;
17421 }
17422 else
17423 fail = TRUE;
17424 break;
17425
ff9940b0 17426 case CALL_INSN:
68d560d4
RE
17427 /* The AAPCS says that conditional calls should not be
17428 used since they make interworking inefficient (the
17429 linker can't transform BL<cond> into BLX). That's
17430 only a problem if the machine has BLX. */
17431 if (arm_arch5)
17432 {
17433 fail = TRUE;
17434 break;
17435 }
17436
61f0ccff
RE
17437 /* Succeed if the following insn is the target label, or
17438 if the following two insns are a barrier and the
17439 target label. */
17440 this_insn = next_nonnote_insn (this_insn);
17441 if (this_insn && GET_CODE (this_insn) == BARRIER)
17442 this_insn = next_nonnote_insn (this_insn);
bd9c7e23 17443
61f0ccff
RE
17444 if (this_insn && this_insn == label
17445 && insns_skipped < max_insns_skipped)
17446 {
accbd151 17447 arm_ccfsm_state = 1;
61f0ccff 17448 succeed = TRUE;
bd9c7e23 17449 }
61f0ccff
RE
17450 else
17451 fail = TRUE;
ff9940b0 17452 break;
2b835d68 17453
cce8749e
CH
17454 case JUMP_INSN:
17455 /* If this is an unconditional branch to the same label, succeed.
17456 If it is to another label, do nothing. If it is conditional,
17457 fail. */
e32bac5b
RE
17458 /* XXX Probably, the tests for SET and the PC are
17459 unnecessary. */
cce8749e 17460
ed4c4348 17461 scanbody = PATTERN (this_insn);
ff9940b0
RE
17462 if (GET_CODE (scanbody) == SET
17463 && GET_CODE (SET_DEST (scanbody)) == PC)
cce8749e
CH
17464 {
17465 if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
17466 && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
17467 {
17468 arm_ccfsm_state = 2;
17469 succeed = TRUE;
17470 }
17471 else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
17472 fail = TRUE;
17473 }
112cdef5 17474 /* Fail if a conditional return is undesirable (e.g. on a
b36ba79f
RE
17475 StrongARM), but still allow this if optimizing for size. */
17476 else if (GET_CODE (scanbody) == RETURN
a72d4945 17477 && !use_return_insn (TRUE, NULL)
5895f793 17478 && !optimize_size)
b36ba79f 17479 fail = TRUE;
ff9940b0
RE
17480 else if (GET_CODE (scanbody) == RETURN
17481 && seeking_return)
17482 {
17483 arm_ccfsm_state = 2;
17484 succeed = TRUE;
17485 }
17486 else if (GET_CODE (scanbody) == PARALLEL)
17487 {
17488 switch (get_attr_conds (this_insn))
17489 {
17490 case CONDS_NOCOND:
17491 break;
17492 default:
17493 fail = TRUE;
17494 break;
17495 }
17496 }
4e67550b 17497 else
112cdef5 17498 fail = TRUE; /* Unrecognized jump (e.g. epilogue). */
4e67550b 17499
cce8749e
CH
17500 break;
17501
17502 case INSN:
ff9940b0
RE
17503 /* Instructions using or affecting the condition codes make it
17504 fail. */
ed4c4348 17505 scanbody = PATTERN (this_insn);
5895f793
RE
17506 if (!(GET_CODE (scanbody) == SET
17507 || GET_CODE (scanbody) == PARALLEL)
74641843 17508 || get_attr_conds (this_insn) != CONDS_NOCOND)
cce8749e 17509 fail = TRUE;
9b6b54e2
NC
17510
17511 /* A conditional cirrus instruction must be followed by
17512 a non Cirrus instruction. However, since we
17513 conditionalize instructions in this function and by
17514 the time we get here we can't add instructions
17515 (nops), because shorten_branches() has already been
17516 called, we will disable conditionalizing Cirrus
17517 instructions to be safe. */
17518 if (GET_CODE (scanbody) != USE
17519 && GET_CODE (scanbody) != CLOBBER
f0375c66 17520 && get_attr_cirrus (this_insn) != CIRRUS_NOT)
9b6b54e2 17521 fail = TRUE;
cce8749e
CH
17522 break;
17523
17524 default:
17525 break;
17526 }
17527 }
17528 if (succeed)
17529 {
ff9940b0 17530 if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
cce8749e 17531 arm_target_label = CODE_LABEL_NUMBER (label);
e6d29d15 17532 else
ff9940b0 17533 {
e6d29d15 17534 gcc_assert (seeking_return || arm_ccfsm_state == 2);
e0b92319 17535
ff9940b0
RE
17536 while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
17537 {
17538 this_insn = next_nonnote_insn (this_insn);
e6d29d15
NS
17539 gcc_assert (!this_insn
17540 || (GET_CODE (this_insn) != BARRIER
17541 && GET_CODE (this_insn) != CODE_LABEL));
ff9940b0
RE
17542 }
17543 if (!this_insn)
17544 {
d6b4baa4 17545 /* Oh, dear! we ran off the end.. give up. */
5b3e6663 17546 extract_constrain_insn_cached (insn);
ff9940b0 17547 arm_ccfsm_state = 0;
abaa26e5 17548 arm_target_insn = NULL;
ff9940b0
RE
17549 return;
17550 }
17551 arm_target_insn = this_insn;
17552 }
accbd151
PB
17553
17554 /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
17555 what it was. */
17556 if (!reverse)
17557 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body), 0));
cce8749e 17558
cce8749e
CH
17559 if (reverse || then_not_else)
17560 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
17561 }
f676971a 17562
1ccbefce 17563 /* Restore recog_data (getting the attributes of other insns can
ff9940b0 17564 destroy this array, but final.c assumes that it remains intact
5b3e6663
PB
17565 across this call. */
17566 extract_constrain_insn_cached (insn);
17567 }
17568}
17569
17570/* Output IT instructions. */
17571void
17572thumb2_asm_output_opcode (FILE * stream)
17573{
17574 char buff[5];
17575 int n;
17576
17577 if (arm_condexec_mask)
17578 {
17579 for (n = 0; n < arm_condexec_masklen; n++)
17580 buff[n] = (arm_condexec_mask & (1 << n)) ? 't' : 'e';
17581 buff[n] = 0;
17582 asm_fprintf(stream, "i%s\t%s\n\t", buff,
17583 arm_condition_codes[arm_current_cc]);
17584 arm_condexec_mask = 0;
cce8749e 17585 }
f3bb6135 17586}
cce8749e 17587
4b02997f 17588/* Returns true if REGNO is a valid register
21b5653c 17589 for holding a quantity of type MODE. */
4b02997f 17590int
e32bac5b 17591arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
4b02997f
NC
17592{
17593 if (GET_MODE_CLASS (mode) == MODE_CC)
a6a5de04
RE
17594 return (regno == CC_REGNUM
17595 || (TARGET_HARD_FLOAT && TARGET_VFP
17596 && regno == VFPCC_REGNUM));
f676971a 17597
5b3e6663 17598 if (TARGET_THUMB1)
4b02997f
NC
17599 /* For the Thumb we only allow values bigger than SImode in
17600 registers 0 - 6, so that there is always a second low
17601 register available to hold the upper part of the value.
17602 We probably we ought to ensure that the register is the
17603 start of an even numbered register pair. */
e9d7b180 17604 return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
4b02997f 17605
a6a5de04
RE
17606 if (TARGET_HARD_FLOAT && TARGET_MAVERICK
17607 && IS_CIRRUS_REGNUM (regno))
9b6b54e2
NC
17608 /* We have outlawed SI values in Cirrus registers because they
17609 reside in the lower 32 bits, but SF values reside in the
17610 upper 32 bits. This causes gcc all sorts of grief. We can't
17611 even split the registers into pairs because Cirrus SI values
17612 get sign extended to 64bits-- aldyh. */
17613 return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
17614
a6a5de04
RE
17615 if (TARGET_HARD_FLOAT && TARGET_VFP
17616 && IS_VFP_REGNUM (regno))
9b66ebb1
PB
17617 {
17618 if (mode == SFmode || mode == SImode)
f1adb0a9 17619 return VFP_REGNO_OK_FOR_SINGLE (regno);
9b66ebb1 17620
9b66ebb1 17621 if (mode == DFmode)
f1adb0a9 17622 return VFP_REGNO_OK_FOR_DOUBLE (regno);
88f77cba 17623
0fd8c3ad 17624 /* VFP registers can hold HFmode values, but there is no point in
e0dc3601 17625 putting them there unless we have hardware conversion insns. */
0fd8c3ad 17626 if (mode == HFmode)
e0dc3601 17627 return TARGET_FP16 && VFP_REGNO_OK_FOR_SINGLE (regno);
0fd8c3ad 17628
88f77cba
JB
17629 if (TARGET_NEON)
17630 return (VALID_NEON_DREG_MODE (mode) && VFP_REGNO_OK_FOR_DOUBLE (regno))
17631 || (VALID_NEON_QREG_MODE (mode)
17632 && NEON_REGNO_OK_FOR_QUAD (regno))
17633 || (mode == TImode && NEON_REGNO_OK_FOR_NREGS (regno, 2))
17634 || (mode == EImode && NEON_REGNO_OK_FOR_NREGS (regno, 3))
17635 || (mode == OImode && NEON_REGNO_OK_FOR_NREGS (regno, 4))
17636 || (mode == CImode && NEON_REGNO_OK_FOR_NREGS (regno, 6))
17637 || (mode == XImode && NEON_REGNO_OK_FOR_NREGS (regno, 8));
17638
9b66ebb1
PB
17639 return FALSE;
17640 }
17641
a6a5de04
RE
17642 if (TARGET_REALLY_IWMMXT)
17643 {
17644 if (IS_IWMMXT_GR_REGNUM (regno))
17645 return mode == SImode;
5a9335ef 17646
a6a5de04
RE
17647 if (IS_IWMMXT_REGNUM (regno))
17648 return VALID_IWMMXT_REG_MODE (mode);
17649 }
17650
2e94c12d 17651 /* We allow almost any value to be stored in the general registers.
fdd695fd 17652 Restrict doubleword quantities to even register pairs so that we can
2e94c12d
JB
17653 use ldrd. Do not allow very large Neon structure opaque modes in
17654 general registers; they would use too many. */
4b02997f 17655 if (regno <= LAST_ARM_REGNUM)
88f77cba 17656 return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0)
2e94c12d 17657 && ARM_NUM_REGS (mode) <= 4;
4b02997f 17658
a6a5de04 17659 if (regno == FRAME_POINTER_REGNUM
4b02997f
NC
17660 || regno == ARG_POINTER_REGNUM)
17661 /* We only allow integers in the fake hard registers. */
17662 return GET_MODE_CLASS (mode) == MODE_INT;
17663
3b684012 17664 /* The only registers left are the FPA registers
4b02997f 17665 which we only allow to hold FP values. */
a6a5de04
RE
17666 return (TARGET_HARD_FLOAT && TARGET_FPA
17667 && GET_MODE_CLASS (mode) == MODE_FLOAT
17668 && regno >= FIRST_FPA_REGNUM
17669 && regno <= LAST_FPA_REGNUM);
4b02997f
NC
17670}
17671
5b3e6663
PB
17672/* For efficiency and historical reasons LO_REGS, HI_REGS and CC_REGS are
17673 not used in arm mode. */
bbbbb16a
ILT
17674
17675enum reg_class
e32bac5b 17676arm_regno_class (int regno)
d5b7b3ae 17677{
5b3e6663 17678 if (TARGET_THUMB1)
d5b7b3ae
RE
17679 {
17680 if (regno == STACK_POINTER_REGNUM)
17681 return STACK_REG;
17682 if (regno == CC_REGNUM)
17683 return CC_REG;
17684 if (regno < 8)
17685 return LO_REGS;
17686 return HI_REGS;
17687 }
17688
5b3e6663
PB
17689 if (TARGET_THUMB2 && regno < 8)
17690 return LO_REGS;
17691
d5b7b3ae
RE
17692 if ( regno <= LAST_ARM_REGNUM
17693 || regno == FRAME_POINTER_REGNUM
17694 || regno == ARG_POINTER_REGNUM)
5b3e6663 17695 return TARGET_THUMB2 ? HI_REGS : GENERAL_REGS;
f676971a 17696
9b66ebb1 17697 if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
5b3e6663 17698 return TARGET_THUMB2 ? CC_REG : NO_REGS;
d5b7b3ae 17699
9b6b54e2
NC
17700 if (IS_CIRRUS_REGNUM (regno))
17701 return CIRRUS_REGS;
17702
9b66ebb1 17703 if (IS_VFP_REGNUM (regno))
f1adb0a9
JB
17704 {
17705 if (regno <= D7_VFP_REGNUM)
17706 return VFP_D0_D7_REGS;
17707 else if (regno <= LAST_LO_VFP_REGNUM)
17708 return VFP_LO_REGS;
17709 else
17710 return VFP_HI_REGS;
17711 }
9b66ebb1 17712
5a9335ef
NC
17713 if (IS_IWMMXT_REGNUM (regno))
17714 return IWMMXT_REGS;
17715
e99faaaa
ILT
17716 if (IS_IWMMXT_GR_REGNUM (regno))
17717 return IWMMXT_GR_REGS;
17718
3b684012 17719 return FPA_REGS;
d5b7b3ae
RE
17720}
17721
17722/* Handle a special case when computing the offset
17723 of an argument from the frame pointer. */
17724int
e32bac5b 17725arm_debugger_arg_offset (int value, rtx addr)
d5b7b3ae
RE
17726{
17727 rtx insn;
17728
17729 /* We are only interested if dbxout_parms() failed to compute the offset. */
17730 if (value != 0)
17731 return 0;
17732
17733 /* We can only cope with the case where the address is held in a register. */
17734 if (GET_CODE (addr) != REG)
17735 return 0;
17736
17737 /* If we are using the frame pointer to point at the argument, then
17738 an offset of 0 is correct. */
cd2b33d0 17739 if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
d5b7b3ae 17740 return 0;
f676971a 17741
d5b7b3ae
RE
17742 /* If we are using the stack pointer to point at the
17743 argument, then an offset of 0 is correct. */
5b3e6663 17744 /* ??? Check this is consistent with thumb2 frame layout. */
5895f793 17745 if ((TARGET_THUMB || !frame_pointer_needed)
d5b7b3ae
RE
17746 && REGNO (addr) == SP_REGNUM)
17747 return 0;
f676971a 17748
d5b7b3ae
RE
17749 /* Oh dear. The argument is pointed to by a register rather
17750 than being held in a register, or being stored at a known
17751 offset from the frame pointer. Since GDB only understands
17752 those two kinds of argument we must translate the address
17753 held in the register into an offset from the frame pointer.
17754 We do this by searching through the insns for the function
17755 looking to see where this register gets its value. If the
4912a07c 17756 register is initialized from the frame pointer plus an offset
d5b7b3ae 17757 then we are in luck and we can continue, otherwise we give up.
f676971a 17758
d5b7b3ae
RE
17759 This code is exercised by producing debugging information
17760 for a function with arguments like this:
f676971a 17761
d5b7b3ae 17762 double func (double a, double b, int c, double d) {return d;}
f676971a 17763
d5b7b3ae
RE
17764 Without this code the stab for parameter 'd' will be set to
17765 an offset of 0 from the frame pointer, rather than 8. */
17766
17767 /* The if() statement says:
17768
17769 If the insn is a normal instruction
17770 and if the insn is setting the value in a register
17771 and if the register being set is the register holding the address of the argument
17772 and if the address is computing by an addition
17773 that involves adding to a register
17774 which is the frame pointer
17775 a constant integer
17776
d6b4baa4 17777 then... */
f676971a 17778
d5b7b3ae
RE
17779 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
17780 {
f676971a 17781 if ( GET_CODE (insn) == INSN
d5b7b3ae
RE
17782 && GET_CODE (PATTERN (insn)) == SET
17783 && REGNO (XEXP (PATTERN (insn), 0)) == REGNO (addr)
17784 && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
17785 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
cd2b33d0 17786 && REGNO (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
d5b7b3ae
RE
17787 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
17788 )
17789 {
17790 value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
f676971a 17791
d5b7b3ae
RE
17792 break;
17793 }
17794 }
f676971a 17795
d5b7b3ae
RE
17796 if (value == 0)
17797 {
17798 debug_rtx (addr);
d4ee4d25 17799 warning (0, "unable to compute real location of stacked parameter");
d5b7b3ae
RE
17800 value = 8; /* XXX magic hack */
17801 }
17802
17803 return value;
17804}
d5b7b3ae 17805\f
229a1c59
JZ
17806typedef enum {
17807 T_V8QI,
17808 T_V4HI,
17809 T_V2SI,
17810 T_V2SF,
17811 T_DI,
17812 T_V16QI,
17813 T_V8HI,
17814 T_V4SI,
17815 T_V4SF,
17816 T_V2DI,
17817 T_TI,
17818 T_EI,
17819 T_OI,
17820 T_MAX /* Size of enum. Keep last. */
17821} neon_builtin_type_mode;
17822
17823#define TYPE_MODE_BIT(X) (1 << (X))
17824
17825#define TB_DREG (TYPE_MODE_BIT (T_V8QI) | TYPE_MODE_BIT (T_V4HI) \
17826 | TYPE_MODE_BIT (T_V2SI) | TYPE_MODE_BIT (T_V2SF) \
17827 | TYPE_MODE_BIT (T_DI))
17828#define TB_QREG (TYPE_MODE_BIT (T_V16QI) | TYPE_MODE_BIT (T_V8HI) \
17829 | TYPE_MODE_BIT (T_V4SI) | TYPE_MODE_BIT (T_V4SF) \
17830 | TYPE_MODE_BIT (T_V2DI) | TYPE_MODE_BIT (T_TI))
5a9335ef 17831
229a1c59
JZ
17832#define v8qi_UP T_V8QI
17833#define v4hi_UP T_V4HI
17834#define v2si_UP T_V2SI
17835#define v2sf_UP T_V2SF
17836#define di_UP T_DI
17837#define v16qi_UP T_V16QI
17838#define v8hi_UP T_V8HI
17839#define v4si_UP T_V4SI
17840#define v4sf_UP T_V4SF
17841#define v2di_UP T_V2DI
17842#define ti_UP T_TI
17843#define ei_UP T_EI
17844#define oi_UP T_OI
5a9335ef 17845
229a1c59 17846#define UP(X) X##_UP
5a9335ef 17847
229a1c59
JZ
17848typedef enum {
17849 NEON_BINOP,
17850 NEON_TERNOP,
17851 NEON_UNOP,
17852 NEON_GETLANE,
17853 NEON_SETLANE,
17854 NEON_CREATE,
17855 NEON_DUP,
17856 NEON_DUPLANE,
17857 NEON_COMBINE,
17858 NEON_SPLIT,
17859 NEON_LANEMUL,
17860 NEON_LANEMULL,
17861 NEON_LANEMULH,
17862 NEON_LANEMAC,
17863 NEON_SCALARMUL,
17864 NEON_SCALARMULL,
17865 NEON_SCALARMULH,
17866 NEON_SCALARMAC,
17867 NEON_CONVERT,
17868 NEON_FIXCONV,
17869 NEON_SELECT,
17870 NEON_RESULTPAIR,
17871 NEON_REINTERP,
17872 NEON_VTBL,
17873 NEON_VTBX,
17874 NEON_LOAD1,
17875 NEON_LOAD1LANE,
17876 NEON_STORE1,
17877 NEON_STORE1LANE,
17878 NEON_LOADSTRUCT,
17879 NEON_LOADSTRUCTLANE,
17880 NEON_STORESTRUCT,
17881 NEON_STORESTRUCTLANE,
17882 NEON_LOGICBINOP,
17883 NEON_SHIFTINSERT,
17884 NEON_SHIFTIMM,
17885 NEON_SHIFTACC
17886} neon_itype;
5a9335ef 17887
229a1c59
JZ
17888typedef struct {
17889 const char *name;
17890 const neon_itype itype;
17891 const neon_builtin_type_mode mode;
17892 const enum insn_code code;
17893 unsigned int fcode;
17894} neon_builtin_datum;
f676971a 17895
229a1c59 17896#define CF(N,X) CODE_FOR_neon_##N##X
5a9335ef 17897
229a1c59
JZ
17898#define VAR1(T, N, A) \
17899 {#N, NEON_##T, UP (A), CF (N, A), 0}
17900#define VAR2(T, N, A, B) \
17901 VAR1 (T, N, A), \
17902 {#N, NEON_##T, UP (B), CF (N, B), 0}
17903#define VAR3(T, N, A, B, C) \
17904 VAR2 (T, N, A, B), \
17905 {#N, NEON_##T, UP (C), CF (N, C), 0}
17906#define VAR4(T, N, A, B, C, D) \
17907 VAR3 (T, N, A, B, C), \
17908 {#N, NEON_##T, UP (D), CF (N, D), 0}
17909#define VAR5(T, N, A, B, C, D, E) \
17910 VAR4 (T, N, A, B, C, D), \
17911 {#N, NEON_##T, UP (E), CF (N, E), 0}
17912#define VAR6(T, N, A, B, C, D, E, F) \
17913 VAR5 (T, N, A, B, C, D, E), \
17914 {#N, NEON_##T, UP (F), CF (N, F), 0}
17915#define VAR7(T, N, A, B, C, D, E, F, G) \
17916 VAR6 (T, N, A, B, C, D, E, F), \
17917 {#N, NEON_##T, UP (G), CF (N, G), 0}
17918#define VAR8(T, N, A, B, C, D, E, F, G, H) \
17919 VAR7 (T, N, A, B, C, D, E, F, G), \
17920 {#N, NEON_##T, UP (H), CF (N, H), 0}
17921#define VAR9(T, N, A, B, C, D, E, F, G, H, I) \
17922 VAR8 (T, N, A, B, C, D, E, F, G, H), \
17923 {#N, NEON_##T, UP (I), CF (N, I), 0}
17924#define VAR10(T, N, A, B, C, D, E, F, G, H, I, J) \
17925 VAR9 (T, N, A, B, C, D, E, F, G, H, I), \
17926 {#N, NEON_##T, UP (J), CF (N, J), 0}
17927
17928/* The mode entries in the following table correspond to the "key" type of the
17929 instruction variant, i.e. equivalent to that which would be specified after
17930 the assembler mnemonic, which usually refers to the last vector operand.
17931 (Signed/unsigned/polynomial types are not differentiated between though, and
17932 are all mapped onto the same mode for a given element size.) The modes
17933 listed per instruction should be the same as those defined for that
17934 instruction's pattern in neon.md. */
17935
17936static neon_builtin_datum neon_builtin_data[] =
5a9335ef 17937{
229a1c59
JZ
17938 VAR10 (BINOP, vadd,
17939 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
17940 VAR3 (BINOP, vaddl, v8qi, v4hi, v2si),
17941 VAR3 (BINOP, vaddw, v8qi, v4hi, v2si),
17942 VAR6 (BINOP, vhadd, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
17943 VAR8 (BINOP, vqadd, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
17944 VAR3 (BINOP, vaddhn, v8hi, v4si, v2di),
17945 VAR8 (BINOP, vmul, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
17946 VAR8 (TERNOP, vmla, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
17947 VAR3 (TERNOP, vmlal, v8qi, v4hi, v2si),
17948 VAR8 (TERNOP, vmls, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
17949 VAR3 (TERNOP, vmlsl, v8qi, v4hi, v2si),
17950 VAR4 (BINOP, vqdmulh, v4hi, v2si, v8hi, v4si),
17951 VAR2 (TERNOP, vqdmlal, v4hi, v2si),
17952 VAR2 (TERNOP, vqdmlsl, v4hi, v2si),
17953 VAR3 (BINOP, vmull, v8qi, v4hi, v2si),
17954 VAR2 (SCALARMULL, vmull_n, v4hi, v2si),
17955 VAR2 (LANEMULL, vmull_lane, v4hi, v2si),
17956 VAR2 (SCALARMULL, vqdmull_n, v4hi, v2si),
17957 VAR2 (LANEMULL, vqdmull_lane, v4hi, v2si),
17958 VAR4 (SCALARMULH, vqdmulh_n, v4hi, v2si, v8hi, v4si),
17959 VAR4 (LANEMULH, vqdmulh_lane, v4hi, v2si, v8hi, v4si),
17960 VAR2 (BINOP, vqdmull, v4hi, v2si),
17961 VAR8 (BINOP, vshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
17962 VAR8 (BINOP, vqshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
17963 VAR8 (SHIFTIMM, vshr_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
17964 VAR3 (SHIFTIMM, vshrn_n, v8hi, v4si, v2di),
17965 VAR3 (SHIFTIMM, vqshrn_n, v8hi, v4si, v2di),
17966 VAR3 (SHIFTIMM, vqshrun_n, v8hi, v4si, v2di),
17967 VAR8 (SHIFTIMM, vshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
17968 VAR8 (SHIFTIMM, vqshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
17969 VAR8 (SHIFTIMM, vqshlu_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
17970 VAR3 (SHIFTIMM, vshll_n, v8qi, v4hi, v2si),
17971 VAR8 (SHIFTACC, vsra_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
17972 VAR10 (BINOP, vsub,
17973 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
17974 VAR3 (BINOP, vsubl, v8qi, v4hi, v2si),
17975 VAR3 (BINOP, vsubw, v8qi, v4hi, v2si),
17976 VAR8 (BINOP, vqsub, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
17977 VAR6 (BINOP, vhsub, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
17978 VAR3 (BINOP, vsubhn, v8hi, v4si, v2di),
17979 VAR8 (BINOP, vceq, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
17980 VAR8 (BINOP, vcge, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
17981 VAR8 (BINOP, vcgt, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
17982 VAR2 (BINOP, vcage, v2sf, v4sf),
17983 VAR2 (BINOP, vcagt, v2sf, v4sf),
17984 VAR6 (BINOP, vtst, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
17985 VAR8 (BINOP, vabd, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
17986 VAR3 (BINOP, vabdl, v8qi, v4hi, v2si),
17987 VAR6 (TERNOP, vaba, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
17988 VAR3 (TERNOP, vabal, v8qi, v4hi, v2si),
17989 VAR8 (BINOP, vmax, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
17990 VAR8 (BINOP, vmin, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
17991 VAR4 (BINOP, vpadd, v8qi, v4hi, v2si, v2sf),
17992 VAR6 (UNOP, vpaddl, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
17993 VAR6 (BINOP, vpadal, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
17994 VAR4 (BINOP, vpmax, v8qi, v4hi, v2si, v2sf),
17995 VAR4 (BINOP, vpmin, v8qi, v4hi, v2si, v2sf),
17996 VAR2 (BINOP, vrecps, v2sf, v4sf),
17997 VAR2 (BINOP, vrsqrts, v2sf, v4sf),
17998 VAR8 (SHIFTINSERT, vsri_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
17999 VAR8 (SHIFTINSERT, vsli_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
18000 VAR8 (UNOP, vabs, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
18001 VAR6 (UNOP, vqabs, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
18002 VAR8 (UNOP, vneg, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
18003 VAR6 (UNOP, vqneg, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
18004 VAR6 (UNOP, vcls, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
18005 VAR6 (UNOP, vclz, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
18006 VAR2 (UNOP, vcnt, v8qi, v16qi),
18007 VAR4 (UNOP, vrecpe, v2si, v2sf, v4si, v4sf),
18008 VAR4 (UNOP, vrsqrte, v2si, v2sf, v4si, v4sf),
18009 VAR6 (UNOP, vmvn, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
18010 /* FIXME: vget_lane supports more variants than this! */
18011 VAR10 (GETLANE, vget_lane,
18012 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18013 VAR10 (SETLANE, vset_lane,
18014 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18015 VAR5 (CREATE, vcreate, v8qi, v4hi, v2si, v2sf, di),
18016 VAR10 (DUP, vdup_n,
18017 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18018 VAR10 (DUPLANE, vdup_lane,
18019 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18020 VAR5 (COMBINE, vcombine, v8qi, v4hi, v2si, v2sf, di),
18021 VAR5 (SPLIT, vget_high, v16qi, v8hi, v4si, v4sf, v2di),
18022 VAR5 (SPLIT, vget_low, v16qi, v8hi, v4si, v4sf, v2di),
18023 VAR3 (UNOP, vmovn, v8hi, v4si, v2di),
18024 VAR3 (UNOP, vqmovn, v8hi, v4si, v2di),
18025 VAR3 (UNOP, vqmovun, v8hi, v4si, v2di),
18026 VAR3 (UNOP, vmovl, v8qi, v4hi, v2si),
18027 VAR6 (LANEMUL, vmul_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18028 VAR6 (LANEMAC, vmla_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18029 VAR2 (LANEMAC, vmlal_lane, v4hi, v2si),
18030 VAR2 (LANEMAC, vqdmlal_lane, v4hi, v2si),
18031 VAR6 (LANEMAC, vmls_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18032 VAR2 (LANEMAC, vmlsl_lane, v4hi, v2si),
18033 VAR2 (LANEMAC, vqdmlsl_lane, v4hi, v2si),
18034 VAR6 (SCALARMUL, vmul_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18035 VAR6 (SCALARMAC, vmla_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18036 VAR2 (SCALARMAC, vmlal_n, v4hi, v2si),
18037 VAR2 (SCALARMAC, vqdmlal_n, v4hi, v2si),
18038 VAR6 (SCALARMAC, vmls_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18039 VAR2 (SCALARMAC, vmlsl_n, v4hi, v2si),
18040 VAR2 (SCALARMAC, vqdmlsl_n, v4hi, v2si),
18041 VAR10 (BINOP, vext,
18042 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18043 VAR8 (UNOP, vrev64, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
18044 VAR4 (UNOP, vrev32, v8qi, v4hi, v16qi, v8hi),
18045 VAR2 (UNOP, vrev16, v8qi, v16qi),
18046 VAR4 (CONVERT, vcvt, v2si, v2sf, v4si, v4sf),
18047 VAR4 (FIXCONV, vcvt_n, v2si, v2sf, v4si, v4sf),
18048 VAR10 (SELECT, vbsl,
18049 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18050 VAR1 (VTBL, vtbl1, v8qi),
18051 VAR1 (VTBL, vtbl2, v8qi),
18052 VAR1 (VTBL, vtbl3, v8qi),
18053 VAR1 (VTBL, vtbl4, v8qi),
18054 VAR1 (VTBX, vtbx1, v8qi),
18055 VAR1 (VTBX, vtbx2, v8qi),
18056 VAR1 (VTBX, vtbx3, v8qi),
18057 VAR1 (VTBX, vtbx4, v8qi),
18058 VAR8 (RESULTPAIR, vtrn, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
18059 VAR8 (RESULTPAIR, vzip, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
18060 VAR8 (RESULTPAIR, vuzp, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
18061 VAR5 (REINTERP, vreinterpretv8qi, v8qi, v4hi, v2si, v2sf, di),
18062 VAR5 (REINTERP, vreinterpretv4hi, v8qi, v4hi, v2si, v2sf, di),
18063 VAR5 (REINTERP, vreinterpretv2si, v8qi, v4hi, v2si, v2sf, di),
18064 VAR5 (REINTERP, vreinterpretv2sf, v8qi, v4hi, v2si, v2sf, di),
18065 VAR5 (REINTERP, vreinterpretdi, v8qi, v4hi, v2si, v2sf, di),
18066 VAR5 (REINTERP, vreinterpretv16qi, v16qi, v8hi, v4si, v4sf, v2di),
18067 VAR5 (REINTERP, vreinterpretv8hi, v16qi, v8hi, v4si, v4sf, v2di),
18068 VAR5 (REINTERP, vreinterpretv4si, v16qi, v8hi, v4si, v4sf, v2di),
18069 VAR5 (REINTERP, vreinterpretv4sf, v16qi, v8hi, v4si, v4sf, v2di),
18070 VAR5 (REINTERP, vreinterpretv2di, v16qi, v8hi, v4si, v4sf, v2di),
18071 VAR10 (LOAD1, vld1,
18072 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18073 VAR10 (LOAD1LANE, vld1_lane,
18074 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18075 VAR10 (LOAD1, vld1_dup,
18076 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18077 VAR10 (STORE1, vst1,
18078 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18079 VAR10 (STORE1LANE, vst1_lane,
18080 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18081 VAR9 (LOADSTRUCT,
18082 vld2, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf),
18083 VAR7 (LOADSTRUCTLANE, vld2_lane,
18084 v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18085 VAR5 (LOADSTRUCT, vld2_dup, v8qi, v4hi, v2si, v2sf, di),
18086 VAR9 (STORESTRUCT, vst2,
18087 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf),
18088 VAR7 (STORESTRUCTLANE, vst2_lane,
18089 v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18090 VAR9 (LOADSTRUCT,
18091 vld3, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf),
18092 VAR7 (LOADSTRUCTLANE, vld3_lane,
18093 v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18094 VAR5 (LOADSTRUCT, vld3_dup, v8qi, v4hi, v2si, v2sf, di),
18095 VAR9 (STORESTRUCT, vst3,
18096 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf),
18097 VAR7 (STORESTRUCTLANE, vst3_lane,
18098 v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18099 VAR9 (LOADSTRUCT, vld4,
18100 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf),
18101 VAR7 (LOADSTRUCTLANE, vld4_lane,
18102 v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18103 VAR5 (LOADSTRUCT, vld4_dup, v8qi, v4hi, v2si, v2sf, di),
18104 VAR9 (STORESTRUCT, vst4,
18105 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf),
18106 VAR7 (STORESTRUCTLANE, vst4_lane,
18107 v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18108 VAR10 (LOGICBINOP, vand,
18109 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18110 VAR10 (LOGICBINOP, vorr,
18111 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18112 VAR10 (BINOP, veor,
18113 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18114 VAR10 (LOGICBINOP, vbic,
18115 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18116 VAR10 (LOGICBINOP, vorn,
18117 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di)
18118};
18119
18120#undef CF
18121#undef VAR1
18122#undef VAR2
18123#undef VAR3
18124#undef VAR4
18125#undef VAR5
18126#undef VAR6
18127#undef VAR7
18128#undef VAR8
18129#undef VAR9
18130#undef VAR10
18131
18132/* Neon defines builtins from ARM_BUILTIN_MAX upwards, though they don't have
18133 symbolic names defined here (which would require too much duplication).
18134 FIXME? */
18135enum arm_builtins
18136{
18137 ARM_BUILTIN_GETWCX,
18138 ARM_BUILTIN_SETWCX,
18139
18140 ARM_BUILTIN_WZERO,
18141
18142 ARM_BUILTIN_WAVG2BR,
18143 ARM_BUILTIN_WAVG2HR,
18144 ARM_BUILTIN_WAVG2B,
18145 ARM_BUILTIN_WAVG2H,
18146
18147 ARM_BUILTIN_WACCB,
18148 ARM_BUILTIN_WACCH,
18149 ARM_BUILTIN_WACCW,
18150
18151 ARM_BUILTIN_WMACS,
18152 ARM_BUILTIN_WMACSZ,
18153 ARM_BUILTIN_WMACU,
18154 ARM_BUILTIN_WMACUZ,
18155
18156 ARM_BUILTIN_WSADB,
18157 ARM_BUILTIN_WSADBZ,
18158 ARM_BUILTIN_WSADH,
18159 ARM_BUILTIN_WSADHZ,
18160
18161 ARM_BUILTIN_WALIGN,
18162
18163 ARM_BUILTIN_TMIA,
18164 ARM_BUILTIN_TMIAPH,
18165 ARM_BUILTIN_TMIABB,
18166 ARM_BUILTIN_TMIABT,
18167 ARM_BUILTIN_TMIATB,
18168 ARM_BUILTIN_TMIATT,
18169
18170 ARM_BUILTIN_TMOVMSKB,
18171 ARM_BUILTIN_TMOVMSKH,
18172 ARM_BUILTIN_TMOVMSKW,
18173
18174 ARM_BUILTIN_TBCSTB,
18175 ARM_BUILTIN_TBCSTH,
18176 ARM_BUILTIN_TBCSTW,
18177
18178 ARM_BUILTIN_WMADDS,
18179 ARM_BUILTIN_WMADDU,
18180
18181 ARM_BUILTIN_WPACKHSS,
18182 ARM_BUILTIN_WPACKWSS,
18183 ARM_BUILTIN_WPACKDSS,
18184 ARM_BUILTIN_WPACKHUS,
18185 ARM_BUILTIN_WPACKWUS,
18186 ARM_BUILTIN_WPACKDUS,
18187
18188 ARM_BUILTIN_WADDB,
18189 ARM_BUILTIN_WADDH,
18190 ARM_BUILTIN_WADDW,
18191 ARM_BUILTIN_WADDSSB,
18192 ARM_BUILTIN_WADDSSH,
18193 ARM_BUILTIN_WADDSSW,
18194 ARM_BUILTIN_WADDUSB,
18195 ARM_BUILTIN_WADDUSH,
18196 ARM_BUILTIN_WADDUSW,
18197 ARM_BUILTIN_WSUBB,
18198 ARM_BUILTIN_WSUBH,
18199 ARM_BUILTIN_WSUBW,
18200 ARM_BUILTIN_WSUBSSB,
18201 ARM_BUILTIN_WSUBSSH,
18202 ARM_BUILTIN_WSUBSSW,
18203 ARM_BUILTIN_WSUBUSB,
18204 ARM_BUILTIN_WSUBUSH,
18205 ARM_BUILTIN_WSUBUSW,
18206
18207 ARM_BUILTIN_WAND,
18208 ARM_BUILTIN_WANDN,
18209 ARM_BUILTIN_WOR,
18210 ARM_BUILTIN_WXOR,
18211
18212 ARM_BUILTIN_WCMPEQB,
18213 ARM_BUILTIN_WCMPEQH,
18214 ARM_BUILTIN_WCMPEQW,
18215 ARM_BUILTIN_WCMPGTUB,
18216 ARM_BUILTIN_WCMPGTUH,
18217 ARM_BUILTIN_WCMPGTUW,
18218 ARM_BUILTIN_WCMPGTSB,
18219 ARM_BUILTIN_WCMPGTSH,
18220 ARM_BUILTIN_WCMPGTSW,
18221
18222 ARM_BUILTIN_TEXTRMSB,
18223 ARM_BUILTIN_TEXTRMSH,
18224 ARM_BUILTIN_TEXTRMSW,
18225 ARM_BUILTIN_TEXTRMUB,
18226 ARM_BUILTIN_TEXTRMUH,
18227 ARM_BUILTIN_TEXTRMUW,
18228 ARM_BUILTIN_TINSRB,
18229 ARM_BUILTIN_TINSRH,
18230 ARM_BUILTIN_TINSRW,
18231
18232 ARM_BUILTIN_WMAXSW,
18233 ARM_BUILTIN_WMAXSH,
18234 ARM_BUILTIN_WMAXSB,
18235 ARM_BUILTIN_WMAXUW,
18236 ARM_BUILTIN_WMAXUH,
18237 ARM_BUILTIN_WMAXUB,
18238 ARM_BUILTIN_WMINSW,
18239 ARM_BUILTIN_WMINSH,
18240 ARM_BUILTIN_WMINSB,
18241 ARM_BUILTIN_WMINUW,
18242 ARM_BUILTIN_WMINUH,
18243 ARM_BUILTIN_WMINUB,
18244
18245 ARM_BUILTIN_WMULUM,
18246 ARM_BUILTIN_WMULSM,
18247 ARM_BUILTIN_WMULUL,
18248
18249 ARM_BUILTIN_PSADBH,
18250 ARM_BUILTIN_WSHUFH,
18251
18252 ARM_BUILTIN_WSLLH,
18253 ARM_BUILTIN_WSLLW,
18254 ARM_BUILTIN_WSLLD,
18255 ARM_BUILTIN_WSRAH,
18256 ARM_BUILTIN_WSRAW,
18257 ARM_BUILTIN_WSRAD,
18258 ARM_BUILTIN_WSRLH,
18259 ARM_BUILTIN_WSRLW,
18260 ARM_BUILTIN_WSRLD,
18261 ARM_BUILTIN_WRORH,
18262 ARM_BUILTIN_WRORW,
18263 ARM_BUILTIN_WRORD,
18264 ARM_BUILTIN_WSLLHI,
18265 ARM_BUILTIN_WSLLWI,
18266 ARM_BUILTIN_WSLLDI,
18267 ARM_BUILTIN_WSRAHI,
18268 ARM_BUILTIN_WSRAWI,
18269 ARM_BUILTIN_WSRADI,
18270 ARM_BUILTIN_WSRLHI,
18271 ARM_BUILTIN_WSRLWI,
18272 ARM_BUILTIN_WSRLDI,
18273 ARM_BUILTIN_WRORHI,
18274 ARM_BUILTIN_WRORWI,
18275 ARM_BUILTIN_WRORDI,
18276
18277 ARM_BUILTIN_WUNPCKIHB,
18278 ARM_BUILTIN_WUNPCKIHH,
18279 ARM_BUILTIN_WUNPCKIHW,
18280 ARM_BUILTIN_WUNPCKILB,
18281 ARM_BUILTIN_WUNPCKILH,
18282 ARM_BUILTIN_WUNPCKILW,
18283
18284 ARM_BUILTIN_WUNPCKEHSB,
18285 ARM_BUILTIN_WUNPCKEHSH,
18286 ARM_BUILTIN_WUNPCKEHSW,
18287 ARM_BUILTIN_WUNPCKEHUB,
18288 ARM_BUILTIN_WUNPCKEHUH,
18289 ARM_BUILTIN_WUNPCKEHUW,
18290 ARM_BUILTIN_WUNPCKELSB,
18291 ARM_BUILTIN_WUNPCKELSH,
18292 ARM_BUILTIN_WUNPCKELSW,
18293 ARM_BUILTIN_WUNPCKELUB,
18294 ARM_BUILTIN_WUNPCKELUH,
18295 ARM_BUILTIN_WUNPCKELUW,
18296
18297 ARM_BUILTIN_THREAD_POINTER,
18298
18299 ARM_BUILTIN_NEON_BASE,
18300
18301 ARM_BUILTIN_MAX = ARM_BUILTIN_NEON_BASE + ARRAY_SIZE (neon_builtin_data)
5a9335ef
NC
18302};
18303
229a1c59 18304static GTY(()) tree arm_builtin_decls[ARM_BUILTIN_MAX];
5a9335ef
NC
18305
18306static void
229a1c59 18307arm_init_neon_builtins (void)
5a9335ef 18308{
229a1c59
JZ
18309 unsigned int i, fcode;
18310 tree decl;
5a9335ef 18311
229a1c59
JZ
18312 tree neon_intQI_type_node;
18313 tree neon_intHI_type_node;
18314 tree neon_polyQI_type_node;
18315 tree neon_polyHI_type_node;
18316 tree neon_intSI_type_node;
18317 tree neon_intDI_type_node;
18318 tree neon_float_type_node;
4a5eab38 18319
229a1c59
JZ
18320 tree intQI_pointer_node;
18321 tree intHI_pointer_node;
18322 tree intSI_pointer_node;
18323 tree intDI_pointer_node;
18324 tree float_pointer_node;
5a9335ef 18325
229a1c59
JZ
18326 tree const_intQI_node;
18327 tree const_intHI_node;
18328 tree const_intSI_node;
18329 tree const_intDI_node;
18330 tree const_float_node;
5a9335ef 18331
229a1c59
JZ
18332 tree const_intQI_pointer_node;
18333 tree const_intHI_pointer_node;
18334 tree const_intSI_pointer_node;
18335 tree const_intDI_pointer_node;
18336 tree const_float_pointer_node;
5a9335ef 18337
229a1c59
JZ
18338 tree V8QI_type_node;
18339 tree V4HI_type_node;
18340 tree V2SI_type_node;
18341 tree V2SF_type_node;
18342 tree V16QI_type_node;
18343 tree V8HI_type_node;
18344 tree V4SI_type_node;
18345 tree V4SF_type_node;
18346 tree V2DI_type_node;
5a9335ef 18347
229a1c59
JZ
18348 tree intUQI_type_node;
18349 tree intUHI_type_node;
18350 tree intUSI_type_node;
18351 tree intUDI_type_node;
5a9335ef 18352
229a1c59
JZ
18353 tree intEI_type_node;
18354 tree intOI_type_node;
18355 tree intCI_type_node;
18356 tree intXI_type_node;
5a9335ef 18357
229a1c59
JZ
18358 tree V8QI_pointer_node;
18359 tree V4HI_pointer_node;
18360 tree V2SI_pointer_node;
18361 tree V2SF_pointer_node;
18362 tree V16QI_pointer_node;
18363 tree V8HI_pointer_node;
18364 tree V4SI_pointer_node;
18365 tree V4SF_pointer_node;
18366 tree V2DI_pointer_node;
5a9335ef 18367
229a1c59
JZ
18368 tree void_ftype_pv8qi_v8qi_v8qi;
18369 tree void_ftype_pv4hi_v4hi_v4hi;
18370 tree void_ftype_pv2si_v2si_v2si;
18371 tree void_ftype_pv2sf_v2sf_v2sf;
18372 tree void_ftype_pdi_di_di;
18373 tree void_ftype_pv16qi_v16qi_v16qi;
18374 tree void_ftype_pv8hi_v8hi_v8hi;
18375 tree void_ftype_pv4si_v4si_v4si;
18376 tree void_ftype_pv4sf_v4sf_v4sf;
18377 tree void_ftype_pv2di_v2di_v2di;
5a9335ef 18378
229a1c59
JZ
18379 tree reinterp_ftype_dreg[5][5];
18380 tree reinterp_ftype_qreg[5][5];
18381 tree dreg_types[5], qreg_types[5];
af06585a 18382
88f77cba
JB
18383 /* Create distinguished type nodes for NEON vector element types,
18384 and pointers to values of such types, so we can detect them later. */
af06585a
JM
18385 neon_intQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
18386 neon_intHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
18387 neon_polyQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
18388 neon_polyHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
18389 neon_intSI_type_node = make_signed_type (GET_MODE_PRECISION (SImode));
18390 neon_intDI_type_node = make_signed_type (GET_MODE_PRECISION (DImode));
18391 neon_float_type_node = make_node (REAL_TYPE);
18392 TYPE_PRECISION (neon_float_type_node) = FLOAT_TYPE_SIZE;
18393 layout_type (neon_float_type_node);
18394
bcbdbbb0
JM
18395 /* Define typedefs which exactly correspond to the modes we are basing vector
18396 types on. If you change these names you'll need to change
18397 the table used by arm_mangle_type too. */
18398 (*lang_hooks.types.register_builtin_type) (neon_intQI_type_node,
18399 "__builtin_neon_qi");
18400 (*lang_hooks.types.register_builtin_type) (neon_intHI_type_node,
18401 "__builtin_neon_hi");
18402 (*lang_hooks.types.register_builtin_type) (neon_intSI_type_node,
18403 "__builtin_neon_si");
18404 (*lang_hooks.types.register_builtin_type) (neon_float_type_node,
18405 "__builtin_neon_sf");
18406 (*lang_hooks.types.register_builtin_type) (neon_intDI_type_node,
18407 "__builtin_neon_di");
18408 (*lang_hooks.types.register_builtin_type) (neon_polyQI_type_node,
18409 "__builtin_neon_poly8");
18410 (*lang_hooks.types.register_builtin_type) (neon_polyHI_type_node,
18411 "__builtin_neon_poly16");
18412
af06585a
JM
18413 intQI_pointer_node = build_pointer_type (neon_intQI_type_node);
18414 intHI_pointer_node = build_pointer_type (neon_intHI_type_node);
18415 intSI_pointer_node = build_pointer_type (neon_intSI_type_node);
18416 intDI_pointer_node = build_pointer_type (neon_intDI_type_node);
18417 float_pointer_node = build_pointer_type (neon_float_type_node);
88f77cba
JB
18418
18419 /* Next create constant-qualified versions of the above types. */
af06585a
JM
18420 const_intQI_node = build_qualified_type (neon_intQI_type_node,
18421 TYPE_QUAL_CONST);
18422 const_intHI_node = build_qualified_type (neon_intHI_type_node,
18423 TYPE_QUAL_CONST);
18424 const_intSI_node = build_qualified_type (neon_intSI_type_node,
18425 TYPE_QUAL_CONST);
18426 const_intDI_node = build_qualified_type (neon_intDI_type_node,
18427 TYPE_QUAL_CONST);
18428 const_float_node = build_qualified_type (neon_float_type_node,
18429 TYPE_QUAL_CONST);
18430
18431 const_intQI_pointer_node = build_pointer_type (const_intQI_node);
18432 const_intHI_pointer_node = build_pointer_type (const_intHI_node);
18433 const_intSI_pointer_node = build_pointer_type (const_intSI_node);
18434 const_intDI_pointer_node = build_pointer_type (const_intDI_node);
18435 const_float_pointer_node = build_pointer_type (const_float_node);
88f77cba
JB
18436
18437 /* Now create vector types based on our NEON element types. */
18438 /* 64-bit vectors. */
af06585a 18439 V8QI_type_node =
88f77cba 18440 build_vector_type_for_mode (neon_intQI_type_node, V8QImode);
af06585a 18441 V4HI_type_node =
88f77cba 18442 build_vector_type_for_mode (neon_intHI_type_node, V4HImode);
af06585a 18443 V2SI_type_node =
88f77cba 18444 build_vector_type_for_mode (neon_intSI_type_node, V2SImode);
af06585a 18445 V2SF_type_node =
88f77cba
JB
18446 build_vector_type_for_mode (neon_float_type_node, V2SFmode);
18447 /* 128-bit vectors. */
af06585a 18448 V16QI_type_node =
88f77cba 18449 build_vector_type_for_mode (neon_intQI_type_node, V16QImode);
af06585a 18450 V8HI_type_node =
88f77cba 18451 build_vector_type_for_mode (neon_intHI_type_node, V8HImode);
af06585a 18452 V4SI_type_node =
88f77cba 18453 build_vector_type_for_mode (neon_intSI_type_node, V4SImode);
af06585a 18454 V4SF_type_node =
88f77cba 18455 build_vector_type_for_mode (neon_float_type_node, V4SFmode);
af06585a 18456 V2DI_type_node =
88f77cba
JB
18457 build_vector_type_for_mode (neon_intDI_type_node, V2DImode);
18458
18459 /* Unsigned integer types for various mode sizes. */
af06585a
JM
18460 intUQI_type_node = make_unsigned_type (GET_MODE_PRECISION (QImode));
18461 intUHI_type_node = make_unsigned_type (GET_MODE_PRECISION (HImode));
18462 intUSI_type_node = make_unsigned_type (GET_MODE_PRECISION (SImode));
18463 intUDI_type_node = make_unsigned_type (GET_MODE_PRECISION (DImode));
88f77cba 18464
bcbdbbb0
JM
18465 (*lang_hooks.types.register_builtin_type) (intUQI_type_node,
18466 "__builtin_neon_uqi");
18467 (*lang_hooks.types.register_builtin_type) (intUHI_type_node,
18468 "__builtin_neon_uhi");
18469 (*lang_hooks.types.register_builtin_type) (intUSI_type_node,
18470 "__builtin_neon_usi");
18471 (*lang_hooks.types.register_builtin_type) (intUDI_type_node,
18472 "__builtin_neon_udi");
18473
88f77cba 18474 /* Opaque integer types for structures of vectors. */
af06585a
JM
18475 intEI_type_node = make_signed_type (GET_MODE_PRECISION (EImode));
18476 intOI_type_node = make_signed_type (GET_MODE_PRECISION (OImode));
18477 intCI_type_node = make_signed_type (GET_MODE_PRECISION (CImode));
18478 intXI_type_node = make_signed_type (GET_MODE_PRECISION (XImode));
88f77cba 18479
bcbdbbb0
JM
18480 (*lang_hooks.types.register_builtin_type) (intTI_type_node,
18481 "__builtin_neon_ti");
18482 (*lang_hooks.types.register_builtin_type) (intEI_type_node,
18483 "__builtin_neon_ei");
18484 (*lang_hooks.types.register_builtin_type) (intOI_type_node,
18485 "__builtin_neon_oi");
18486 (*lang_hooks.types.register_builtin_type) (intCI_type_node,
18487 "__builtin_neon_ci");
18488 (*lang_hooks.types.register_builtin_type) (intXI_type_node,
18489 "__builtin_neon_xi");
18490
88f77cba 18491 /* Pointers to vector types. */
af06585a
JM
18492 V8QI_pointer_node = build_pointer_type (V8QI_type_node);
18493 V4HI_pointer_node = build_pointer_type (V4HI_type_node);
18494 V2SI_pointer_node = build_pointer_type (V2SI_type_node);
18495 V2SF_pointer_node = build_pointer_type (V2SF_type_node);
18496 V16QI_pointer_node = build_pointer_type (V16QI_type_node);
18497 V8HI_pointer_node = build_pointer_type (V8HI_type_node);
18498 V4SI_pointer_node = build_pointer_type (V4SI_type_node);
18499 V4SF_pointer_node = build_pointer_type (V4SF_type_node);
18500 V2DI_pointer_node = build_pointer_type (V2DI_type_node);
88f77cba
JB
18501
18502 /* Operations which return results as pairs. */
af06585a 18503 void_ftype_pv8qi_v8qi_v8qi =
88f77cba
JB
18504 build_function_type_list (void_type_node, V8QI_pointer_node, V8QI_type_node,
18505 V8QI_type_node, NULL);
af06585a 18506 void_ftype_pv4hi_v4hi_v4hi =
88f77cba
JB
18507 build_function_type_list (void_type_node, V4HI_pointer_node, V4HI_type_node,
18508 V4HI_type_node, NULL);
af06585a 18509 void_ftype_pv2si_v2si_v2si =
88f77cba
JB
18510 build_function_type_list (void_type_node, V2SI_pointer_node, V2SI_type_node,
18511 V2SI_type_node, NULL);
af06585a 18512 void_ftype_pv2sf_v2sf_v2sf =
88f77cba
JB
18513 build_function_type_list (void_type_node, V2SF_pointer_node, V2SF_type_node,
18514 V2SF_type_node, NULL);
af06585a 18515 void_ftype_pdi_di_di =
88f77cba
JB
18516 build_function_type_list (void_type_node, intDI_pointer_node,
18517 neon_intDI_type_node, neon_intDI_type_node, NULL);
af06585a 18518 void_ftype_pv16qi_v16qi_v16qi =
88f77cba
JB
18519 build_function_type_list (void_type_node, V16QI_pointer_node,
18520 V16QI_type_node, V16QI_type_node, NULL);
af06585a 18521 void_ftype_pv8hi_v8hi_v8hi =
88f77cba
JB
18522 build_function_type_list (void_type_node, V8HI_pointer_node, V8HI_type_node,
18523 V8HI_type_node, NULL);
af06585a 18524 void_ftype_pv4si_v4si_v4si =
88f77cba
JB
18525 build_function_type_list (void_type_node, V4SI_pointer_node, V4SI_type_node,
18526 V4SI_type_node, NULL);
af06585a 18527 void_ftype_pv4sf_v4sf_v4sf =
88f77cba
JB
18528 build_function_type_list (void_type_node, V4SF_pointer_node, V4SF_type_node,
18529 V4SF_type_node, NULL);
af06585a 18530 void_ftype_pv2di_v2di_v2di =
88f77cba
JB
18531 build_function_type_list (void_type_node, V2DI_pointer_node, V2DI_type_node,
18532 V2DI_type_node, NULL);
18533
88f77cba
JB
18534 dreg_types[0] = V8QI_type_node;
18535 dreg_types[1] = V4HI_type_node;
18536 dreg_types[2] = V2SI_type_node;
18537 dreg_types[3] = V2SF_type_node;
18538 dreg_types[4] = neon_intDI_type_node;
18539
229a1c59
JZ
18540 qreg_types[0] = V16QI_type_node;
18541 qreg_types[1] = V8HI_type_node;
18542 qreg_types[2] = V4SI_type_node;
18543 qreg_types[3] = V4SF_type_node;
18544 qreg_types[4] = V2DI_type_node;
18545
18546 for (i = 0; i < 5; i++)
18547 {
18548 int j;
18549 for (j = 0; j < 5; j++)
18550 {
18551 reinterp_ftype_dreg[i][j]
18552 = build_function_type_list (dreg_types[i], dreg_types[j], NULL);
18553 reinterp_ftype_qreg[i][j]
18554 = build_function_type_list (qreg_types[i], qreg_types[j], NULL);
18555 }
18556 }
18557
18558 for (i = 0, fcode = ARM_BUILTIN_NEON_BASE;
18559 i < ARRAY_SIZE (neon_builtin_data);
18560 i++, fcode++)
18561 {
18562 neon_builtin_datum *d = &neon_builtin_data[i];
18563
18564 const char* const modenames[] = {
18565 "v8qi", "v4hi", "v2si", "v2sf", "di",
18566 "v16qi", "v8hi", "v4si", "v4sf", "v2di",
18567 "ti", "ei", "oi"
18568 };
18569 char namebuf[60];
18570 tree ftype = NULL;
18571 int is_load = 0, is_store = 0;
18572
18573 gcc_assert (ARRAY_SIZE (modenames) == T_MAX);
18574
18575 d->fcode = fcode;
18576
18577 switch (d->itype)
18578 {
18579 case NEON_LOAD1:
18580 case NEON_LOAD1LANE:
18581 case NEON_LOADSTRUCT:
18582 case NEON_LOADSTRUCTLANE:
18583 is_load = 1;
18584 /* Fall through. */
18585 case NEON_STORE1:
18586 case NEON_STORE1LANE:
18587 case NEON_STORESTRUCT:
18588 case NEON_STORESTRUCTLANE:
18589 if (!is_load)
18590 is_store = 1;
18591 /* Fall through. */
18592 case NEON_UNOP:
18593 case NEON_BINOP:
18594 case NEON_LOGICBINOP:
18595 case NEON_SHIFTINSERT:
18596 case NEON_TERNOP:
18597 case NEON_GETLANE:
18598 case NEON_SETLANE:
18599 case NEON_CREATE:
18600 case NEON_DUP:
18601 case NEON_DUPLANE:
18602 case NEON_SHIFTIMM:
18603 case NEON_SHIFTACC:
18604 case NEON_COMBINE:
18605 case NEON_SPLIT:
18606 case NEON_CONVERT:
18607 case NEON_FIXCONV:
18608 case NEON_LANEMUL:
18609 case NEON_LANEMULL:
18610 case NEON_LANEMULH:
18611 case NEON_LANEMAC:
18612 case NEON_SCALARMUL:
18613 case NEON_SCALARMULL:
18614 case NEON_SCALARMULH:
18615 case NEON_SCALARMAC:
18616 case NEON_SELECT:
18617 case NEON_VTBL:
18618 case NEON_VTBX:
18619 {
18620 int k;
18621 tree return_type = void_type_node, args = void_list_node;
18622
18623 /* Build a function type directly from the insn_data for
18624 this builtin. The build_function_type() function takes
18625 care of removing duplicates for us. */
18626 for (k = insn_data[d->code].n_generator_args - 1; k >= 0; k--)
18627 {
18628 tree eltype;
18629
18630 if (is_load && k == 1)
18631 {
18632 /* Neon load patterns always have the memory
18633 operand in the operand 1 position. */
18634 gcc_assert (insn_data[d->code].operand[k].predicate
18635 == neon_struct_operand);
18636
18637 switch (d->mode)
18638 {
18639 case T_V8QI:
18640 case T_V16QI:
18641 eltype = const_intQI_pointer_node;
18642 break;
18643
18644 case T_V4HI:
18645 case T_V8HI:
18646 eltype = const_intHI_pointer_node;
18647 break;
18648
18649 case T_V2SI:
18650 case T_V4SI:
18651 eltype = const_intSI_pointer_node;
18652 break;
18653
18654 case T_V2SF:
18655 case T_V4SF:
18656 eltype = const_float_pointer_node;
18657 break;
18658
18659 case T_DI:
18660 case T_V2DI:
18661 eltype = const_intDI_pointer_node;
18662 break;
18663
18664 default: gcc_unreachable ();
18665 }
18666 }
18667 else if (is_store && k == 0)
18668 {
18669 /* Similarly, Neon store patterns use operand 0 as
18670 the memory location to store to. */
18671 gcc_assert (insn_data[d->code].operand[k].predicate
18672 == neon_struct_operand);
18673
18674 switch (d->mode)
18675 {
18676 case T_V8QI:
18677 case T_V16QI:
18678 eltype = intQI_pointer_node;
18679 break;
18680
18681 case T_V4HI:
18682 case T_V8HI:
18683 eltype = intHI_pointer_node;
18684 break;
18685
18686 case T_V2SI:
18687 case T_V4SI:
18688 eltype = intSI_pointer_node;
18689 break;
18690
18691 case T_V2SF:
18692 case T_V4SF:
18693 eltype = float_pointer_node;
18694 break;
18695
18696 case T_DI:
18697 case T_V2DI:
18698 eltype = intDI_pointer_node;
18699 break;
18700
18701 default: gcc_unreachable ();
18702 }
18703 }
18704 else
18705 {
18706 switch (insn_data[d->code].operand[k].mode)
18707 {
18708 case VOIDmode: eltype = void_type_node; break;
18709 /* Scalars. */
18710 case QImode: eltype = neon_intQI_type_node; break;
18711 case HImode: eltype = neon_intHI_type_node; break;
18712 case SImode: eltype = neon_intSI_type_node; break;
18713 case SFmode: eltype = neon_float_type_node; break;
18714 case DImode: eltype = neon_intDI_type_node; break;
18715 case TImode: eltype = intTI_type_node; break;
18716 case EImode: eltype = intEI_type_node; break;
18717 case OImode: eltype = intOI_type_node; break;
18718 case CImode: eltype = intCI_type_node; break;
18719 case XImode: eltype = intXI_type_node; break;
18720 /* 64-bit vectors. */
18721 case V8QImode: eltype = V8QI_type_node; break;
18722 case V4HImode: eltype = V4HI_type_node; break;
18723 case V2SImode: eltype = V2SI_type_node; break;
18724 case V2SFmode: eltype = V2SF_type_node; break;
18725 /* 128-bit vectors. */
18726 case V16QImode: eltype = V16QI_type_node; break;
18727 case V8HImode: eltype = V8HI_type_node; break;
18728 case V4SImode: eltype = V4SI_type_node; break;
18729 case V4SFmode: eltype = V4SF_type_node; break;
18730 case V2DImode: eltype = V2DI_type_node; break;
18731 default: gcc_unreachable ();
18732 }
18733 }
18734
18735 if (k == 0 && !is_store)
18736 return_type = eltype;
18737 else
18738 args = tree_cons (NULL_TREE, eltype, args);
18739 }
18740
18741 ftype = build_function_type (return_type, args);
18742 }
18743 break;
18744
18745 case NEON_RESULTPAIR:
18746 {
18747 switch (insn_data[d->code].operand[1].mode)
18748 {
18749 case V8QImode: ftype = void_ftype_pv8qi_v8qi_v8qi; break;
18750 case V4HImode: ftype = void_ftype_pv4hi_v4hi_v4hi; break;
18751 case V2SImode: ftype = void_ftype_pv2si_v2si_v2si; break;
18752 case V2SFmode: ftype = void_ftype_pv2sf_v2sf_v2sf; break;
18753 case DImode: ftype = void_ftype_pdi_di_di; break;
18754 case V16QImode: ftype = void_ftype_pv16qi_v16qi_v16qi; break;
18755 case V8HImode: ftype = void_ftype_pv8hi_v8hi_v8hi; break;
18756 case V4SImode: ftype = void_ftype_pv4si_v4si_v4si; break;
18757 case V4SFmode: ftype = void_ftype_pv4sf_v4sf_v4sf; break;
18758 case V2DImode: ftype = void_ftype_pv2di_v2di_v2di; break;
18759 default: gcc_unreachable ();
18760 }
18761 }
18762 break;
18763
18764 case NEON_REINTERP:
18765 {
18766 /* We iterate over 5 doubleword types, then 5 quadword
18767 types. */
18768 int rhs = d->mode % 5;
18769 switch (insn_data[d->code].operand[0].mode)
18770 {
18771 case V8QImode: ftype = reinterp_ftype_dreg[0][rhs]; break;
18772 case V4HImode: ftype = reinterp_ftype_dreg[1][rhs]; break;
18773 case V2SImode: ftype = reinterp_ftype_dreg[2][rhs]; break;
18774 case V2SFmode: ftype = reinterp_ftype_dreg[3][rhs]; break;
18775 case DImode: ftype = reinterp_ftype_dreg[4][rhs]; break;
18776 case V16QImode: ftype = reinterp_ftype_qreg[0][rhs]; break;
18777 case V8HImode: ftype = reinterp_ftype_qreg[1][rhs]; break;
18778 case V4SImode: ftype = reinterp_ftype_qreg[2][rhs]; break;
18779 case V4SFmode: ftype = reinterp_ftype_qreg[3][rhs]; break;
18780 case V2DImode: ftype = reinterp_ftype_qreg[4][rhs]; break;
18781 default: gcc_unreachable ();
18782 }
18783 }
18784 break;
18785
18786 default:
18787 gcc_unreachable ();
18788 }
18789
18790 gcc_assert (ftype != NULL);
18791
18792 sprintf (namebuf, "__builtin_neon_%s%s", d->name, modenames[d->mode]);
18793
18794 decl = add_builtin_function (namebuf, ftype, fcode, BUILT_IN_MD, NULL,
18795 NULL_TREE);
18796 arm_builtin_decls[fcode] = decl;
18797 }
18798}
18799
18800#define def_mbuiltin(MASK, NAME, TYPE, CODE) \
18801 do \
18802 { \
18803 if ((MASK) & insn_flags) \
18804 { \
18805 tree bdecl; \
18806 bdecl = add_builtin_function ((NAME), (TYPE), (CODE), \
18807 BUILT_IN_MD, NULL, NULL_TREE); \
18808 arm_builtin_decls[CODE] = bdecl; \
18809 } \
18810 } \
18811 while (0)
18812
18813struct builtin_description
18814{
18815 const unsigned int mask;
18816 const enum insn_code icode;
18817 const char * const name;
18818 const enum arm_builtins code;
18819 const enum rtx_code comparison;
18820 const unsigned int flag;
18821};
18822
18823static const struct builtin_description bdesc_2arg[] =
18824{
18825#define IWMMXT_BUILTIN(code, string, builtin) \
18826 { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
18827 ARM_BUILTIN_##builtin, UNKNOWN, 0 },
18828
18829 IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
18830 IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
18831 IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
18832 IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
18833 IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
18834 IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
18835 IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
18836 IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
18837 IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
18838 IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
18839 IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
18840 IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
18841 IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
18842 IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
18843 IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
18844 IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
18845 IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
18846 IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
18847 IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
18848 IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
18849 IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
18850 IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
18851 IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
18852 IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
18853 IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
18854 IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
18855 IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
18856 IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
18857 IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
18858 IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
18859 IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
18860 IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
18861 IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
18862 IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
18863 IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
18864 IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
18865 IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
18866 IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
18867 IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
18868 IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
18869 IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
18870 IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
18871 IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
18872 IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
18873 IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
18874 IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
18875 IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
18876 IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
18877 IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
18878 IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
18879 IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
18880 IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
18881 IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
18882 IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
18883 IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
18884 IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
18885 IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
18886 IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
18887
18888#define IWMMXT_BUILTIN2(code, builtin) \
18889 { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, UNKNOWN, 0 },
18890
18891 IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
18892 IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
18893 IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
18894 IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
18895 IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
18896 IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
18897 IWMMXT_BUILTIN2 (ashlv4hi3_di, WSLLH)
18898 IWMMXT_BUILTIN2 (ashlv4hi3_iwmmxt, WSLLHI)
18899 IWMMXT_BUILTIN2 (ashlv2si3_di, WSLLW)
18900 IWMMXT_BUILTIN2 (ashlv2si3_iwmmxt, WSLLWI)
18901 IWMMXT_BUILTIN2 (ashldi3_di, WSLLD)
18902 IWMMXT_BUILTIN2 (ashldi3_iwmmxt, WSLLDI)
18903 IWMMXT_BUILTIN2 (lshrv4hi3_di, WSRLH)
18904 IWMMXT_BUILTIN2 (lshrv4hi3_iwmmxt, WSRLHI)
18905 IWMMXT_BUILTIN2 (lshrv2si3_di, WSRLW)
18906 IWMMXT_BUILTIN2 (lshrv2si3_iwmmxt, WSRLWI)
18907 IWMMXT_BUILTIN2 (lshrdi3_di, WSRLD)
18908 IWMMXT_BUILTIN2 (lshrdi3_iwmmxt, WSRLDI)
18909 IWMMXT_BUILTIN2 (ashrv4hi3_di, WSRAH)
18910 IWMMXT_BUILTIN2 (ashrv4hi3_iwmmxt, WSRAHI)
18911 IWMMXT_BUILTIN2 (ashrv2si3_di, WSRAW)
18912 IWMMXT_BUILTIN2 (ashrv2si3_iwmmxt, WSRAWI)
18913 IWMMXT_BUILTIN2 (ashrdi3_di, WSRAD)
18914 IWMMXT_BUILTIN2 (ashrdi3_iwmmxt, WSRADI)
18915 IWMMXT_BUILTIN2 (rorv4hi3_di, WRORH)
18916 IWMMXT_BUILTIN2 (rorv4hi3, WRORHI)
18917 IWMMXT_BUILTIN2 (rorv2si3_di, WRORW)
18918 IWMMXT_BUILTIN2 (rorv2si3, WRORWI)
18919 IWMMXT_BUILTIN2 (rordi3_di, WRORD)
18920 IWMMXT_BUILTIN2 (rordi3, WRORDI)
18921 IWMMXT_BUILTIN2 (iwmmxt_wmacuz, WMACUZ)
18922 IWMMXT_BUILTIN2 (iwmmxt_wmacsz, WMACSZ)
18923};
18924
18925static const struct builtin_description bdesc_1arg[] =
18926{
18927 IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
18928 IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
18929 IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
18930 IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
18931 IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
18932 IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
18933 IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
18934 IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
18935 IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
18936 IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
18937 IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
18938 IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
18939 IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
18940 IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
18941 IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
18942 IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
18943 IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
18944 IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
18945};
18946
18947/* Set up all the iWMMXt builtins. This is not called if
18948 TARGET_IWMMXT is zero. */
18949
18950static void
18951arm_init_iwmmxt_builtins (void)
18952{
18953 const struct builtin_description * d;
18954 size_t i;
18955 tree endlink = void_list_node;
18956
18957 tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
18958 tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
18959 tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
18960
18961 tree int_ftype_int
18962 = build_function_type (integer_type_node,
18963 tree_cons (NULL_TREE, integer_type_node, endlink));
18964 tree v8qi_ftype_v8qi_v8qi_int
18965 = build_function_type (V8QI_type_node,
18966 tree_cons (NULL_TREE, V8QI_type_node,
18967 tree_cons (NULL_TREE, V8QI_type_node,
18968 tree_cons (NULL_TREE,
18969 integer_type_node,
18970 endlink))));
18971 tree v4hi_ftype_v4hi_int
18972 = build_function_type (V4HI_type_node,
18973 tree_cons (NULL_TREE, V4HI_type_node,
18974 tree_cons (NULL_TREE, integer_type_node,
18975 endlink)));
18976 tree v2si_ftype_v2si_int
18977 = build_function_type (V2SI_type_node,
18978 tree_cons (NULL_TREE, V2SI_type_node,
18979 tree_cons (NULL_TREE, integer_type_node,
18980 endlink)));
18981 tree v2si_ftype_di_di
18982 = build_function_type (V2SI_type_node,
18983 tree_cons (NULL_TREE, long_long_integer_type_node,
18984 tree_cons (NULL_TREE,
18985 long_long_integer_type_node,
18986 endlink)));
18987 tree di_ftype_di_int
18988 = build_function_type (long_long_integer_type_node,
18989 tree_cons (NULL_TREE, long_long_integer_type_node,
18990 tree_cons (NULL_TREE, integer_type_node,
18991 endlink)));
18992 tree di_ftype_di_int_int
18993 = build_function_type (long_long_integer_type_node,
18994 tree_cons (NULL_TREE, long_long_integer_type_node,
18995 tree_cons (NULL_TREE, integer_type_node,
18996 tree_cons (NULL_TREE,
18997 integer_type_node,
18998 endlink))));
18999 tree int_ftype_v8qi
19000 = build_function_type (integer_type_node,
19001 tree_cons (NULL_TREE, V8QI_type_node,
19002 endlink));
19003 tree int_ftype_v4hi
19004 = build_function_type (integer_type_node,
19005 tree_cons (NULL_TREE, V4HI_type_node,
19006 endlink));
19007 tree int_ftype_v2si
19008 = build_function_type (integer_type_node,
19009 tree_cons (NULL_TREE, V2SI_type_node,
19010 endlink));
19011 tree int_ftype_v8qi_int
19012 = build_function_type (integer_type_node,
19013 tree_cons (NULL_TREE, V8QI_type_node,
19014 tree_cons (NULL_TREE, integer_type_node,
19015 endlink)));
19016 tree int_ftype_v4hi_int
19017 = build_function_type (integer_type_node,
19018 tree_cons (NULL_TREE, V4HI_type_node,
19019 tree_cons (NULL_TREE, integer_type_node,
19020 endlink)));
19021 tree int_ftype_v2si_int
19022 = build_function_type (integer_type_node,
19023 tree_cons (NULL_TREE, V2SI_type_node,
19024 tree_cons (NULL_TREE, integer_type_node,
19025 endlink)));
19026 tree v8qi_ftype_v8qi_int_int
19027 = build_function_type (V8QI_type_node,
19028 tree_cons (NULL_TREE, V8QI_type_node,
19029 tree_cons (NULL_TREE, integer_type_node,
19030 tree_cons (NULL_TREE,
19031 integer_type_node,
19032 endlink))));
19033 tree v4hi_ftype_v4hi_int_int
19034 = build_function_type (V4HI_type_node,
19035 tree_cons (NULL_TREE, V4HI_type_node,
19036 tree_cons (NULL_TREE, integer_type_node,
19037 tree_cons (NULL_TREE,
19038 integer_type_node,
19039 endlink))));
19040 tree v2si_ftype_v2si_int_int
19041 = build_function_type (V2SI_type_node,
19042 tree_cons (NULL_TREE, V2SI_type_node,
19043 tree_cons (NULL_TREE, integer_type_node,
19044 tree_cons (NULL_TREE,
19045 integer_type_node,
19046 endlink))));
19047 /* Miscellaneous. */
19048 tree v8qi_ftype_v4hi_v4hi
19049 = build_function_type (V8QI_type_node,
19050 tree_cons (NULL_TREE, V4HI_type_node,
19051 tree_cons (NULL_TREE, V4HI_type_node,
19052 endlink)));
19053 tree v4hi_ftype_v2si_v2si
19054 = build_function_type (V4HI_type_node,
19055 tree_cons (NULL_TREE, V2SI_type_node,
19056 tree_cons (NULL_TREE, V2SI_type_node,
19057 endlink)));
19058 tree v2si_ftype_v4hi_v4hi
19059 = build_function_type (V2SI_type_node,
19060 tree_cons (NULL_TREE, V4HI_type_node,
19061 tree_cons (NULL_TREE, V4HI_type_node,
19062 endlink)));
19063 tree v2si_ftype_v8qi_v8qi
19064 = build_function_type (V2SI_type_node,
19065 tree_cons (NULL_TREE, V8QI_type_node,
19066 tree_cons (NULL_TREE, V8QI_type_node,
19067 endlink)));
19068 tree v4hi_ftype_v4hi_di
19069 = build_function_type (V4HI_type_node,
19070 tree_cons (NULL_TREE, V4HI_type_node,
19071 tree_cons (NULL_TREE,
19072 long_long_integer_type_node,
19073 endlink)));
19074 tree v2si_ftype_v2si_di
19075 = build_function_type (V2SI_type_node,
19076 tree_cons (NULL_TREE, V2SI_type_node,
19077 tree_cons (NULL_TREE,
19078 long_long_integer_type_node,
19079 endlink)));
19080 tree void_ftype_int_int
19081 = build_function_type (void_type_node,
19082 tree_cons (NULL_TREE, integer_type_node,
19083 tree_cons (NULL_TREE, integer_type_node,
19084 endlink)));
19085 tree di_ftype_void
19086 = build_function_type (long_long_unsigned_type_node, endlink);
19087 tree di_ftype_v8qi
19088 = build_function_type (long_long_integer_type_node,
19089 tree_cons (NULL_TREE, V8QI_type_node,
19090 endlink));
19091 tree di_ftype_v4hi
19092 = build_function_type (long_long_integer_type_node,
19093 tree_cons (NULL_TREE, V4HI_type_node,
19094 endlink));
19095 tree di_ftype_v2si
19096 = build_function_type (long_long_integer_type_node,
19097 tree_cons (NULL_TREE, V2SI_type_node,
19098 endlink));
19099 tree v2si_ftype_v4hi
19100 = build_function_type (V2SI_type_node,
19101 tree_cons (NULL_TREE, V4HI_type_node,
19102 endlink));
19103 tree v4hi_ftype_v8qi
19104 = build_function_type (V4HI_type_node,
19105 tree_cons (NULL_TREE, V8QI_type_node,
19106 endlink));
19107
19108 tree di_ftype_di_v4hi_v4hi
19109 = build_function_type (long_long_unsigned_type_node,
19110 tree_cons (NULL_TREE,
19111 long_long_unsigned_type_node,
19112 tree_cons (NULL_TREE, V4HI_type_node,
19113 tree_cons (NULL_TREE,
19114 V4HI_type_node,
19115 endlink))));
88f77cba 19116
229a1c59
JZ
19117 tree di_ftype_v4hi_v4hi
19118 = build_function_type (long_long_unsigned_type_node,
19119 tree_cons (NULL_TREE, V4HI_type_node,
19120 tree_cons (NULL_TREE, V4HI_type_node,
19121 endlink)));
19122
19123 /* Normal vector binops. */
19124 tree v8qi_ftype_v8qi_v8qi
19125 = build_function_type (V8QI_type_node,
19126 tree_cons (NULL_TREE, V8QI_type_node,
19127 tree_cons (NULL_TREE, V8QI_type_node,
19128 endlink)));
19129 tree v4hi_ftype_v4hi_v4hi
19130 = build_function_type (V4HI_type_node,
19131 tree_cons (NULL_TREE, V4HI_type_node,
19132 tree_cons (NULL_TREE, V4HI_type_node,
19133 endlink)));
19134 tree v2si_ftype_v2si_v2si
19135 = build_function_type (V2SI_type_node,
19136 tree_cons (NULL_TREE, V2SI_type_node,
19137 tree_cons (NULL_TREE, V2SI_type_node,
19138 endlink)));
19139 tree di_ftype_di_di
19140 = build_function_type (long_long_unsigned_type_node,
19141 tree_cons (NULL_TREE, long_long_unsigned_type_node,
19142 tree_cons (NULL_TREE,
19143 long_long_unsigned_type_node,
19144 endlink)));
19145
19146 /* Add all builtins that are more or less simple operations on two
19147 operands. */
19148 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
88f77cba 19149 {
229a1c59
JZ
19150 /* Use one of the operands; the target can have a different mode for
19151 mask-generating compares. */
19152 enum machine_mode mode;
19153 tree type;
88f77cba 19154
229a1c59
JZ
19155 if (d->name == 0)
19156 continue;
88f77cba 19157
229a1c59 19158 mode = insn_data[d->icode].operand[1].mode;
88f77cba 19159
229a1c59 19160 switch (mode)
88f77cba 19161 {
229a1c59
JZ
19162 case V8QImode:
19163 type = v8qi_ftype_v8qi_v8qi;
19164 break;
19165 case V4HImode:
19166 type = v4hi_ftype_v4hi_v4hi;
19167 break;
19168 case V2SImode:
19169 type = v2si_ftype_v2si_v2si;
19170 break;
19171 case DImode:
19172 type = di_ftype_di_di;
19173 break;
88f77cba 19174
229a1c59
JZ
19175 default:
19176 gcc_unreachable ();
19177 }
88f77cba 19178
229a1c59
JZ
19179 def_mbuiltin (d->mask, d->name, type, d->code);
19180 }
88f77cba 19181
229a1c59
JZ
19182 /* Add the remaining MMX insns with somewhat more complicated types. */
19183#define iwmmx_mbuiltin(NAME, TYPE, CODE) \
19184 def_mbuiltin (FL_IWMMXT, "__builtin_arm_" NAME, (TYPE), \
19185 ARM_BUILTIN_ ## CODE)
19186
19187 iwmmx_mbuiltin ("wzero", di_ftype_void, WZERO);
19188 iwmmx_mbuiltin ("setwcx", void_ftype_int_int, SETWCX);
19189 iwmmx_mbuiltin ("getwcx", int_ftype_int, GETWCX);
19190
19191 iwmmx_mbuiltin ("wsllh", v4hi_ftype_v4hi_di, WSLLH);
19192 iwmmx_mbuiltin ("wsllw", v2si_ftype_v2si_di, WSLLW);
19193 iwmmx_mbuiltin ("wslld", di_ftype_di_di, WSLLD);
19194 iwmmx_mbuiltin ("wsllhi", v4hi_ftype_v4hi_int, WSLLHI);
19195 iwmmx_mbuiltin ("wsllwi", v2si_ftype_v2si_int, WSLLWI);
19196 iwmmx_mbuiltin ("wslldi", di_ftype_di_int, WSLLDI);
19197
19198 iwmmx_mbuiltin ("wsrlh", v4hi_ftype_v4hi_di, WSRLH);
19199 iwmmx_mbuiltin ("wsrlw", v2si_ftype_v2si_di, WSRLW);
19200 iwmmx_mbuiltin ("wsrld", di_ftype_di_di, WSRLD);
19201 iwmmx_mbuiltin ("wsrlhi", v4hi_ftype_v4hi_int, WSRLHI);
19202 iwmmx_mbuiltin ("wsrlwi", v2si_ftype_v2si_int, WSRLWI);
19203 iwmmx_mbuiltin ("wsrldi", di_ftype_di_int, WSRLDI);
19204
19205 iwmmx_mbuiltin ("wsrah", v4hi_ftype_v4hi_di, WSRAH);
19206 iwmmx_mbuiltin ("wsraw", v2si_ftype_v2si_di, WSRAW);
19207 iwmmx_mbuiltin ("wsrad", di_ftype_di_di, WSRAD);
19208 iwmmx_mbuiltin ("wsrahi", v4hi_ftype_v4hi_int, WSRAHI);
19209 iwmmx_mbuiltin ("wsrawi", v2si_ftype_v2si_int, WSRAWI);
19210 iwmmx_mbuiltin ("wsradi", di_ftype_di_int, WSRADI);
19211
19212 iwmmx_mbuiltin ("wrorh", v4hi_ftype_v4hi_di, WRORH);
19213 iwmmx_mbuiltin ("wrorw", v2si_ftype_v2si_di, WRORW);
19214 iwmmx_mbuiltin ("wrord", di_ftype_di_di, WRORD);
19215 iwmmx_mbuiltin ("wrorhi", v4hi_ftype_v4hi_int, WRORHI);
19216 iwmmx_mbuiltin ("wrorwi", v2si_ftype_v2si_int, WRORWI);
19217 iwmmx_mbuiltin ("wrordi", di_ftype_di_int, WRORDI);
19218
19219 iwmmx_mbuiltin ("wshufh", v4hi_ftype_v4hi_int, WSHUFH);
19220
19221 iwmmx_mbuiltin ("wsadb", v2si_ftype_v8qi_v8qi, WSADB);
19222 iwmmx_mbuiltin ("wsadh", v2si_ftype_v4hi_v4hi, WSADH);
19223 iwmmx_mbuiltin ("wsadbz", v2si_ftype_v8qi_v8qi, WSADBZ);
19224 iwmmx_mbuiltin ("wsadhz", v2si_ftype_v4hi_v4hi, WSADHZ);
19225
19226 iwmmx_mbuiltin ("textrmsb", int_ftype_v8qi_int, TEXTRMSB);
19227 iwmmx_mbuiltin ("textrmsh", int_ftype_v4hi_int, TEXTRMSH);
19228 iwmmx_mbuiltin ("textrmsw", int_ftype_v2si_int, TEXTRMSW);
19229 iwmmx_mbuiltin ("textrmub", int_ftype_v8qi_int, TEXTRMUB);
19230 iwmmx_mbuiltin ("textrmuh", int_ftype_v4hi_int, TEXTRMUH);
19231 iwmmx_mbuiltin ("textrmuw", int_ftype_v2si_int, TEXTRMUW);
19232 iwmmx_mbuiltin ("tinsrb", v8qi_ftype_v8qi_int_int, TINSRB);
19233 iwmmx_mbuiltin ("tinsrh", v4hi_ftype_v4hi_int_int, TINSRH);
19234 iwmmx_mbuiltin ("tinsrw", v2si_ftype_v2si_int_int, TINSRW);
19235
19236 iwmmx_mbuiltin ("waccb", di_ftype_v8qi, WACCB);
19237 iwmmx_mbuiltin ("wacch", di_ftype_v4hi, WACCH);
19238 iwmmx_mbuiltin ("waccw", di_ftype_v2si, WACCW);
19239
19240 iwmmx_mbuiltin ("tmovmskb", int_ftype_v8qi, TMOVMSKB);
19241 iwmmx_mbuiltin ("tmovmskh", int_ftype_v4hi, TMOVMSKH);
19242 iwmmx_mbuiltin ("tmovmskw", int_ftype_v2si, TMOVMSKW);
19243
19244 iwmmx_mbuiltin ("wpackhss", v8qi_ftype_v4hi_v4hi, WPACKHSS);
19245 iwmmx_mbuiltin ("wpackhus", v8qi_ftype_v4hi_v4hi, WPACKHUS);
19246 iwmmx_mbuiltin ("wpackwus", v4hi_ftype_v2si_v2si, WPACKWUS);
19247 iwmmx_mbuiltin ("wpackwss", v4hi_ftype_v2si_v2si, WPACKWSS);
19248 iwmmx_mbuiltin ("wpackdus", v2si_ftype_di_di, WPACKDUS);
19249 iwmmx_mbuiltin ("wpackdss", v2si_ftype_di_di, WPACKDSS);
19250
19251 iwmmx_mbuiltin ("wunpckehub", v4hi_ftype_v8qi, WUNPCKEHUB);
19252 iwmmx_mbuiltin ("wunpckehuh", v2si_ftype_v4hi, WUNPCKEHUH);
19253 iwmmx_mbuiltin ("wunpckehuw", di_ftype_v2si, WUNPCKEHUW);
19254 iwmmx_mbuiltin ("wunpckehsb", v4hi_ftype_v8qi, WUNPCKEHSB);
19255 iwmmx_mbuiltin ("wunpckehsh", v2si_ftype_v4hi, WUNPCKEHSH);
19256 iwmmx_mbuiltin ("wunpckehsw", di_ftype_v2si, WUNPCKEHSW);
19257 iwmmx_mbuiltin ("wunpckelub", v4hi_ftype_v8qi, WUNPCKELUB);
19258 iwmmx_mbuiltin ("wunpckeluh", v2si_ftype_v4hi, WUNPCKELUH);
19259 iwmmx_mbuiltin ("wunpckeluw", di_ftype_v2si, WUNPCKELUW);
19260 iwmmx_mbuiltin ("wunpckelsb", v4hi_ftype_v8qi, WUNPCKELSB);
19261 iwmmx_mbuiltin ("wunpckelsh", v2si_ftype_v4hi, WUNPCKELSH);
19262 iwmmx_mbuiltin ("wunpckelsw", di_ftype_v2si, WUNPCKELSW);
19263
19264 iwmmx_mbuiltin ("wmacs", di_ftype_di_v4hi_v4hi, WMACS);
19265 iwmmx_mbuiltin ("wmacsz", di_ftype_v4hi_v4hi, WMACSZ);
19266 iwmmx_mbuiltin ("wmacu", di_ftype_di_v4hi_v4hi, WMACU);
19267 iwmmx_mbuiltin ("wmacuz", di_ftype_v4hi_v4hi, WMACUZ);
19268
19269 iwmmx_mbuiltin ("walign", v8qi_ftype_v8qi_v8qi_int, WALIGN);
19270 iwmmx_mbuiltin ("tmia", di_ftype_di_int_int, TMIA);
19271 iwmmx_mbuiltin ("tmiaph", di_ftype_di_int_int, TMIAPH);
19272 iwmmx_mbuiltin ("tmiabb", di_ftype_di_int_int, TMIABB);
19273 iwmmx_mbuiltin ("tmiabt", di_ftype_di_int_int, TMIABT);
19274 iwmmx_mbuiltin ("tmiatb", di_ftype_di_int_int, TMIATB);
19275 iwmmx_mbuiltin ("tmiatt", di_ftype_di_int_int, TMIATT);
19276
19277#undef iwmmx_mbuiltin
19278}
88f77cba 19279
229a1c59
JZ
19280static void
19281arm_init_tls_builtins (void)
19282{
19283 tree ftype, decl;
88f77cba 19284
229a1c59
JZ
19285 ftype = build_function_type (ptr_type_node, void_list_node);
19286 decl = add_builtin_function ("__builtin_thread_pointer", ftype,
19287 ARM_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
19288 NULL, NULL_TREE);
19289 TREE_NOTHROW (decl) = 1;
19290 TREE_READONLY (decl) = 1;
19291 arm_builtin_decls[ARM_BUILTIN_THREAD_POINTER] = decl;
88f77cba
JB
19292}
19293
0fd8c3ad
SL
19294static void
19295arm_init_fp16_builtins (void)
19296{
19297 tree fp16_type = make_node (REAL_TYPE);
19298 TYPE_PRECISION (fp16_type) = 16;
19299 layout_type (fp16_type);
19300 (*lang_hooks.types.register_builtin_type) (fp16_type, "__fp16");
19301}
19302
5a9335ef
NC
19303static void
19304arm_init_builtins (void)
19305{
d3585b76
DJ
19306 arm_init_tls_builtins ();
19307
5a9335ef
NC
19308 if (TARGET_REALLY_IWMMXT)
19309 arm_init_iwmmxt_builtins ();
88f77cba
JB
19310
19311 if (TARGET_NEON)
19312 arm_init_neon_builtins ();
0fd8c3ad
SL
19313
19314 if (arm_fp16_format)
19315 arm_init_fp16_builtins ();
19316}
19317
229a1c59
JZ
19318/* Return the ARM builtin for CODE. */
19319
19320static tree
19321arm_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
19322{
19323 if (code >= ARM_BUILTIN_MAX)
19324 return error_mark_node;
19325
19326 return arm_builtin_decls[code];
19327}
19328
0fd8c3ad
SL
19329/* Implement TARGET_INVALID_PARAMETER_TYPE. */
19330
19331static const char *
19332arm_invalid_parameter_type (const_tree t)
19333{
19334 if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
19335 return N_("function parameters cannot have __fp16 type");
19336 return NULL;
19337}
19338
19339/* Implement TARGET_INVALID_PARAMETER_TYPE. */
19340
19341static const char *
19342arm_invalid_return_type (const_tree t)
19343{
19344 if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
19345 return N_("functions cannot return __fp16 type");
19346 return NULL;
19347}
19348
19349/* Implement TARGET_PROMOTED_TYPE. */
19350
19351static tree
19352arm_promoted_type (const_tree t)
19353{
19354 if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
19355 return float_type_node;
19356 return NULL_TREE;
19357}
19358
19359/* Implement TARGET_CONVERT_TO_TYPE.
19360 Specifically, this hook implements the peculiarity of the ARM
19361 half-precision floating-point C semantics that requires conversions between
19362 __fp16 to or from double to do an intermediate conversion to float. */
19363
19364static tree
19365arm_convert_to_type (tree type, tree expr)
19366{
19367 tree fromtype = TREE_TYPE (expr);
19368 if (!SCALAR_FLOAT_TYPE_P (fromtype) || !SCALAR_FLOAT_TYPE_P (type))
19369 return NULL_TREE;
19370 if ((TYPE_PRECISION (fromtype) == 16 && TYPE_PRECISION (type) > 32)
19371 || (TYPE_PRECISION (type) == 16 && TYPE_PRECISION (fromtype) > 32))
19372 return convert (type, convert (float_type_node, expr));
19373 return NULL_TREE;
5a9335ef
NC
19374}
19375
bdc4827b
SL
19376/* Implement TARGET_SCALAR_MODE_SUPPORTED_P.
19377 This simply adds HFmode as a supported mode; even though we don't
19378 implement arithmetic on this type directly, it's supported by
19379 optabs conversions, much the way the double-word arithmetic is
19380 special-cased in the default hook. */
19381
19382static bool
19383arm_scalar_mode_supported_p (enum machine_mode mode)
19384{
19385 if (mode == HFmode)
19386 return (arm_fp16_format != ARM_FP16_FORMAT_NONE);
19387 else
19388 return default_scalar_mode_supported_p (mode);
19389}
19390
5a9335ef
NC
19391/* Errors in the source file can cause expand_expr to return const0_rtx
19392 where we expect a vector. To avoid crashing, use one of the vector
19393 clear instructions. */
19394
19395static rtx
19396safe_vector_operand (rtx x, enum machine_mode mode)
19397{
19398 if (x != const0_rtx)
19399 return x;
19400 x = gen_reg_rtx (mode);
19401
19402 emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
19403 : gen_rtx_SUBREG (DImode, x, 0)));
19404 return x;
19405}
19406
19407/* Subroutine of arm_expand_builtin to take care of binop insns. */
19408
19409static rtx
19410arm_expand_binop_builtin (enum insn_code icode,
5039610b 19411 tree exp, rtx target)
5a9335ef
NC
19412{
19413 rtx pat;
5039610b
SL
19414 tree arg0 = CALL_EXPR_ARG (exp, 0);
19415 tree arg1 = CALL_EXPR_ARG (exp, 1);
84217346
MD
19416 rtx op0 = expand_normal (arg0);
19417 rtx op1 = expand_normal (arg1);
5a9335ef
NC
19418 enum machine_mode tmode = insn_data[icode].operand[0].mode;
19419 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
19420 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
19421
19422 if (VECTOR_MODE_P (mode0))
19423 op0 = safe_vector_operand (op0, mode0);
19424 if (VECTOR_MODE_P (mode1))
19425 op1 = safe_vector_operand (op1, mode1);
19426
19427 if (! target
19428 || GET_MODE (target) != tmode
19429 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19430 target = gen_reg_rtx (tmode);
19431
e6d29d15 19432 gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
5a9335ef
NC
19433
19434 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19435 op0 = copy_to_mode_reg (mode0, op0);
19436 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
19437 op1 = copy_to_mode_reg (mode1, op1);
19438
19439 pat = GEN_FCN (icode) (target, op0, op1);
19440 if (! pat)
19441 return 0;
19442 emit_insn (pat);
19443 return target;
19444}
19445
19446/* Subroutine of arm_expand_builtin to take care of unop insns. */
19447
19448static rtx
19449arm_expand_unop_builtin (enum insn_code icode,
5039610b 19450 tree exp, rtx target, int do_load)
5a9335ef
NC
19451{
19452 rtx pat;
5039610b 19453 tree arg0 = CALL_EXPR_ARG (exp, 0);
84217346 19454 rtx op0 = expand_normal (arg0);
5a9335ef
NC
19455 enum machine_mode tmode = insn_data[icode].operand[0].mode;
19456 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
19457
19458 if (! target
19459 || GET_MODE (target) != tmode
19460 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19461 target = gen_reg_rtx (tmode);
19462 if (do_load)
19463 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
19464 else
19465 {
19466 if (VECTOR_MODE_P (mode0))
19467 op0 = safe_vector_operand (op0, mode0);
19468
19469 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19470 op0 = copy_to_mode_reg (mode0, op0);
19471 }
19472
19473 pat = GEN_FCN (icode) (target, op0);
19474 if (! pat)
19475 return 0;
19476 emit_insn (pat);
19477 return target;
19478}
19479
88f77cba
JB
19480typedef enum {
19481 NEON_ARG_COPY_TO_REG,
19482 NEON_ARG_CONSTANT,
6308e208 19483 NEON_ARG_MEMORY,
88f77cba
JB
19484 NEON_ARG_STOP
19485} builtin_arg;
19486
19487#define NEON_MAX_BUILTIN_ARGS 5
19488
6308e208
RS
19489/* EXP is a pointer argument to a Neon load or store intrinsic. Derive
19490 and return an expression for the accessed memory.
19491
19492 The intrinsic function operates on a block of registers that has
229a1c59 19493 mode REG_MODE. This block contains vectors of type TYPE_MODE.
6308e208
RS
19494 The function references the memory at EXP in mode MEM_MODE;
19495 this mode may be BLKmode if no more suitable mode is available. */
19496
19497static tree
19498neon_dereference_pointer (tree exp, enum machine_mode mem_mode,
19499 enum machine_mode reg_mode,
229a1c59 19500 neon_builtin_type_mode type_mode)
6308e208
RS
19501{
19502 HOST_WIDE_INT reg_size, vector_size, nvectors, nelems;
19503 tree elem_type, upper_bound, array_type;
19504
19505 /* Work out the size of the register block in bytes. */
19506 reg_size = GET_MODE_SIZE (reg_mode);
19507
19508 /* Work out the size of each vector in bytes. */
229a1c59
JZ
19509 gcc_assert (TYPE_MODE_BIT (type_mode) & (TB_DREG | TB_QREG));
19510 vector_size = (TYPE_MODE_BIT (type_mode) & TB_QREG ? 16 : 8);
6308e208
RS
19511
19512 /* Work out how many vectors there are. */
19513 gcc_assert (reg_size % vector_size == 0);
19514 nvectors = reg_size / vector_size;
19515
19516 /* Work out how many elements are being loaded or stored.
19517 MEM_MODE == REG_MODE implies a one-to-one mapping between register
19518 and memory elements; anything else implies a lane load or store. */
19519 if (mem_mode == reg_mode)
19520 nelems = vector_size * nvectors;
19521 else
19522 nelems = nvectors;
19523
19524 /* Work out the type of each element. */
19525 gcc_assert (POINTER_TYPE_P (TREE_TYPE (exp)));
19526 elem_type = TREE_TYPE (TREE_TYPE (exp));
19527
19528 /* Create a type that describes the full access. */
19529 upper_bound = build_int_cst (size_type_node, nelems - 1);
19530 array_type = build_array_type (elem_type, build_index_type (upper_bound));
19531
19532 /* Dereference EXP using that type. */
19533 exp = convert (build_pointer_type (array_type), exp);
19534 return fold_build2 (MEM_REF, array_type, exp,
19535 build_int_cst (TREE_TYPE (exp), 0));
19536}
19537
88f77cba
JB
19538/* Expand a Neon builtin. */
19539static rtx
19540arm_expand_neon_args (rtx target, int icode, int have_retval,
229a1c59 19541 neon_builtin_type_mode type_mode,
88f77cba
JB
19542 tree exp, ...)
19543{
19544 va_list ap;
19545 rtx pat;
19546 tree arg[NEON_MAX_BUILTIN_ARGS];
19547 rtx op[NEON_MAX_BUILTIN_ARGS];
19548 enum machine_mode tmode = insn_data[icode].operand[0].mode;
19549 enum machine_mode mode[NEON_MAX_BUILTIN_ARGS];
6308e208 19550 enum machine_mode other_mode;
88f77cba 19551 int argc = 0;
6308e208 19552 int opno;
88f77cba
JB
19553
19554 if (have_retval
19555 && (!target
19556 || GET_MODE (target) != tmode
19557 || !(*insn_data[icode].operand[0].predicate) (target, tmode)))
19558 target = gen_reg_rtx (tmode);
19559
19560 va_start (ap, exp);
19561
19562 for (;;)
19563 {
81f40b79 19564 builtin_arg thisarg = (builtin_arg) va_arg (ap, int);
88f77cba
JB
19565
19566 if (thisarg == NEON_ARG_STOP)
19567 break;
19568 else
19569 {
6308e208
RS
19570 opno = argc + have_retval;
19571 mode[argc] = insn_data[icode].operand[opno].mode;
88f77cba 19572 arg[argc] = CALL_EXPR_ARG (exp, argc);
6308e208
RS
19573 if (thisarg == NEON_ARG_MEMORY)
19574 {
19575 other_mode = insn_data[icode].operand[1 - opno].mode;
19576 arg[argc] = neon_dereference_pointer (arg[argc], mode[argc],
229a1c59 19577 other_mode, type_mode);
6308e208 19578 }
88f77cba 19579 op[argc] = expand_normal (arg[argc]);
88f77cba
JB
19580
19581 switch (thisarg)
19582 {
19583 case NEON_ARG_COPY_TO_REG:
19584 /*gcc_assert (GET_MODE (op[argc]) == mode[argc]);*/
6308e208 19585 if (!(*insn_data[icode].operand[opno].predicate)
88f77cba
JB
19586 (op[argc], mode[argc]))
19587 op[argc] = copy_to_mode_reg (mode[argc], op[argc]);
19588 break;
19589
19590 case NEON_ARG_CONSTANT:
19591 /* FIXME: This error message is somewhat unhelpful. */
6308e208 19592 if (!(*insn_data[icode].operand[opno].predicate)
88f77cba
JB
19593 (op[argc], mode[argc]))
19594 error ("argument must be a constant");
19595 break;
19596
6308e208
RS
19597 case NEON_ARG_MEMORY:
19598 gcc_assert (MEM_P (op[argc]));
19599 PUT_MODE (op[argc], mode[argc]);
19600 /* ??? arm_neon.h uses the same built-in functions for signed
19601 and unsigned accesses, casting where necessary. This isn't
19602 alias safe. */
19603 set_mem_alias_set (op[argc], 0);
19604 if (!(*insn_data[icode].operand[opno].predicate)
19605 (op[argc], mode[argc]))
19606 op[argc] = (replace_equiv_address
19607 (op[argc], force_reg (Pmode, XEXP (op[argc], 0))));
19608 break;
19609
88f77cba
JB
19610 case NEON_ARG_STOP:
19611 gcc_unreachable ();
19612 }
19613
19614 argc++;
19615 }
19616 }
19617
19618 va_end (ap);
19619
19620 if (have_retval)
19621 switch (argc)
19622 {
19623 case 1:
19624 pat = GEN_FCN (icode) (target, op[0]);
19625 break;
19626
19627 case 2:
19628 pat = GEN_FCN (icode) (target, op[0], op[1]);
19629 break;
19630
19631 case 3:
19632 pat = GEN_FCN (icode) (target, op[0], op[1], op[2]);
19633 break;
19634
19635 case 4:
19636 pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3]);
19637 break;
19638
19639 case 5:
19640 pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3], op[4]);
19641 break;
19642
19643 default:
19644 gcc_unreachable ();
19645 }
19646 else
19647 switch (argc)
19648 {
19649 case 1:
19650 pat = GEN_FCN (icode) (op[0]);
19651 break;
19652
19653 case 2:
19654 pat = GEN_FCN (icode) (op[0], op[1]);
19655 break;
19656
19657 case 3:
19658 pat = GEN_FCN (icode) (op[0], op[1], op[2]);
19659 break;
19660
19661 case 4:
19662 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
19663 break;
19664
19665 case 5:
19666 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4]);
19667 break;
19668
19669 default:
19670 gcc_unreachable ();
19671 }
19672
19673 if (!pat)
19674 return 0;
19675
19676 emit_insn (pat);
19677
19678 return target;
19679}
19680
19681/* Expand a Neon builtin. These are "special" because they don't have symbolic
19682 constants defined per-instruction or per instruction-variant. Instead, the
19683 required info is looked up in the table neon_builtin_data. */
19684static rtx
19685arm_expand_neon_builtin (int fcode, tree exp, rtx target)
19686{
229a1c59
JZ
19687 neon_builtin_datum *d = &neon_builtin_data[fcode - ARM_BUILTIN_NEON_BASE];
19688 neon_itype itype = d->itype;
19689 enum insn_code icode = d->code;
19690 neon_builtin_type_mode type_mode = d->mode;
88f77cba
JB
19691
19692 switch (itype)
19693 {
19694 case NEON_UNOP:
19695 case NEON_CONVERT:
19696 case NEON_DUPLANE:
229a1c59 19697 return arm_expand_neon_args (target, icode, 1, type_mode, exp,
88f77cba
JB
19698 NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_STOP);
19699
19700 case NEON_BINOP:
19701 case NEON_SETLANE:
19702 case NEON_SCALARMUL:
19703 case NEON_SCALARMULL:
19704 case NEON_SCALARMULH:
19705 case NEON_SHIFTINSERT:
19706 case NEON_LOGICBINOP:
229a1c59 19707 return arm_expand_neon_args (target, icode, 1, type_mode, exp,
88f77cba
JB
19708 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19709 NEON_ARG_STOP);
19710
19711 case NEON_TERNOP:
229a1c59 19712 return arm_expand_neon_args (target, icode, 1, type_mode, exp,
88f77cba
JB
19713 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19714 NEON_ARG_CONSTANT, NEON_ARG_STOP);
19715
19716 case NEON_GETLANE:
19717 case NEON_FIXCONV:
19718 case NEON_SHIFTIMM:
229a1c59 19719 return arm_expand_neon_args (target, icode, 1, type_mode, exp,
88f77cba
JB
19720 NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_CONSTANT,
19721 NEON_ARG_STOP);
19722
19723 case NEON_CREATE:
229a1c59 19724 return arm_expand_neon_args (target, icode, 1, type_mode, exp,
88f77cba
JB
19725 NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19726
19727 case NEON_DUP:
19728 case NEON_SPLIT:
19729 case NEON_REINTERP:
229a1c59 19730 return arm_expand_neon_args (target, icode, 1, type_mode, exp,
88f77cba
JB
19731 NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19732
19733 case NEON_COMBINE:
19734 case NEON_VTBL:
229a1c59 19735 return arm_expand_neon_args (target, icode, 1, type_mode, exp,
88f77cba
JB
19736 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19737
19738 case NEON_RESULTPAIR:
229a1c59 19739 return arm_expand_neon_args (target, icode, 0, type_mode, exp,
88f77cba
JB
19740 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19741 NEON_ARG_STOP);
19742
19743 case NEON_LANEMUL:
19744 case NEON_LANEMULL:
19745 case NEON_LANEMULH:
229a1c59 19746 return arm_expand_neon_args (target, icode, 1, type_mode, exp,
88f77cba
JB
19747 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19748 NEON_ARG_CONSTANT, NEON_ARG_STOP);
19749
19750 case NEON_LANEMAC:
229a1c59 19751 return arm_expand_neon_args (target, icode, 1, type_mode, exp,
88f77cba
JB
19752 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19753 NEON_ARG_CONSTANT, NEON_ARG_CONSTANT, NEON_ARG_STOP);
19754
19755 case NEON_SHIFTACC:
229a1c59 19756 return arm_expand_neon_args (target, icode, 1, type_mode, exp,
88f77cba
JB
19757 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19758 NEON_ARG_CONSTANT, NEON_ARG_STOP);
19759
19760 case NEON_SCALARMAC:
229a1c59 19761 return arm_expand_neon_args (target, icode, 1, type_mode, exp,
88f77cba
JB
19762 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19763 NEON_ARG_CONSTANT, NEON_ARG_STOP);
19764
19765 case NEON_SELECT:
19766 case NEON_VTBX:
229a1c59 19767 return arm_expand_neon_args (target, icode, 1, type_mode, exp,
88f77cba
JB
19768 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19769 NEON_ARG_STOP);
19770
19771 case NEON_LOAD1:
19772 case NEON_LOADSTRUCT:
229a1c59 19773 return arm_expand_neon_args (target, icode, 1, type_mode, exp,
6308e208 19774 NEON_ARG_MEMORY, NEON_ARG_STOP);
88f77cba
JB
19775
19776 case NEON_LOAD1LANE:
19777 case NEON_LOADSTRUCTLANE:
229a1c59 19778 return arm_expand_neon_args (target, icode, 1, type_mode, exp,
6308e208 19779 NEON_ARG_MEMORY, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
88f77cba
JB
19780 NEON_ARG_STOP);
19781
19782 case NEON_STORE1:
19783 case NEON_STORESTRUCT:
229a1c59 19784 return arm_expand_neon_args (target, icode, 0, type_mode, exp,
6308e208 19785 NEON_ARG_MEMORY, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
88f77cba
JB
19786
19787 case NEON_STORE1LANE:
19788 case NEON_STORESTRUCTLANE:
229a1c59 19789 return arm_expand_neon_args (target, icode, 0, type_mode, exp,
6308e208 19790 NEON_ARG_MEMORY, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
88f77cba
JB
19791 NEON_ARG_STOP);
19792 }
19793
19794 gcc_unreachable ();
19795}
19796
19797/* Emit code to reinterpret one Neon type as another, without altering bits. */
19798void
19799neon_reinterpret (rtx dest, rtx src)
19800{
19801 emit_move_insn (dest, gen_lowpart (GET_MODE (dest), src));
19802}
19803
19804/* Emit code to place a Neon pair result in memory locations (with equal
19805 registers). */
19806void
19807neon_emit_pair_result_insn (enum machine_mode mode,
19808 rtx (*intfn) (rtx, rtx, rtx, rtx), rtx destaddr,
19809 rtx op1, rtx op2)
19810{
19811 rtx mem = gen_rtx_MEM (mode, destaddr);
19812 rtx tmp1 = gen_reg_rtx (mode);
19813 rtx tmp2 = gen_reg_rtx (mode);
19814
7e7cfcf6 19815 emit_insn (intfn (tmp1, op1, op2, tmp2));
88f77cba
JB
19816
19817 emit_move_insn (mem, tmp1);
19818 mem = adjust_address (mem, mode, GET_MODE_SIZE (mode));
19819 emit_move_insn (mem, tmp2);
19820}
19821
19822/* Set up operands for a register copy from src to dest, taking care not to
19823 clobber registers in the process.
19824 FIXME: This has rather high polynomial complexity (O(n^3)?) but shouldn't
19825 be called with a large N, so that should be OK. */
19826
19827void
19828neon_disambiguate_copy (rtx *operands, rtx *dest, rtx *src, unsigned int count)
19829{
19830 unsigned int copied = 0, opctr = 0;
19831 unsigned int done = (1 << count) - 1;
19832 unsigned int i, j;
19833
19834 while (copied != done)
19835 {
19836 for (i = 0; i < count; i++)
19837 {
19838 int good = 1;
19839
19840 for (j = 0; good && j < count; j++)
19841 if (i != j && (copied & (1 << j)) == 0
19842 && reg_overlap_mentioned_p (src[j], dest[i]))
19843 good = 0;
19844
19845 if (good)
19846 {
19847 operands[opctr++] = dest[i];
19848 operands[opctr++] = src[i];
19849 copied |= 1 << i;
19850 }
19851 }
19852 }
19853
19854 gcc_assert (opctr == count * 2);
19855}
19856
5a9335ef
NC
19857/* Expand an expression EXP that calls a built-in function,
19858 with result going to TARGET if that's convenient
19859 (and in mode MODE if that's convenient).
19860 SUBTARGET may be used as the target for computing one of EXP's operands.
19861 IGNORE is nonzero if the value is to be ignored. */
19862
19863static rtx
19864arm_expand_builtin (tree exp,
19865 rtx target,
19866 rtx subtarget ATTRIBUTE_UNUSED,
19867 enum machine_mode mode ATTRIBUTE_UNUSED,
19868 int ignore ATTRIBUTE_UNUSED)
19869{
19870 const struct builtin_description * d;
19871 enum insn_code icode;
5039610b 19872 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
5a9335ef
NC
19873 tree arg0;
19874 tree arg1;
19875 tree arg2;
19876 rtx op0;
19877 rtx op1;
19878 rtx op2;
19879 rtx pat;
19880 int fcode = DECL_FUNCTION_CODE (fndecl);
19881 size_t i;
19882 enum machine_mode tmode;
19883 enum machine_mode mode0;
19884 enum machine_mode mode1;
19885 enum machine_mode mode2;
19886
88f77cba
JB
19887 if (fcode >= ARM_BUILTIN_NEON_BASE)
19888 return arm_expand_neon_builtin (fcode, exp, target);
19889
5a9335ef
NC
19890 switch (fcode)
19891 {
19892 case ARM_BUILTIN_TEXTRMSB:
19893 case ARM_BUILTIN_TEXTRMUB:
19894 case ARM_BUILTIN_TEXTRMSH:
19895 case ARM_BUILTIN_TEXTRMUH:
19896 case ARM_BUILTIN_TEXTRMSW:
19897 case ARM_BUILTIN_TEXTRMUW:
19898 icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
19899 : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
19900 : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
19901 : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
19902 : CODE_FOR_iwmmxt_textrmw);
19903
5039610b
SL
19904 arg0 = CALL_EXPR_ARG (exp, 0);
19905 arg1 = CALL_EXPR_ARG (exp, 1);
84217346
MD
19906 op0 = expand_normal (arg0);
19907 op1 = expand_normal (arg1);
5a9335ef
NC
19908 tmode = insn_data[icode].operand[0].mode;
19909 mode0 = insn_data[icode].operand[1].mode;
19910 mode1 = insn_data[icode].operand[2].mode;
19911
19912 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19913 op0 = copy_to_mode_reg (mode0, op0);
19914 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
19915 {
19916 /* @@@ better error message */
19917 error ("selector must be an immediate");
19918 return gen_reg_rtx (tmode);
19919 }
19920 if (target == 0
19921 || GET_MODE (target) != tmode
19922 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19923 target = gen_reg_rtx (tmode);
19924 pat = GEN_FCN (icode) (target, op0, op1);
19925 if (! pat)
19926 return 0;
19927 emit_insn (pat);
19928 return target;
19929
19930 case ARM_BUILTIN_TINSRB:
19931 case ARM_BUILTIN_TINSRH:
19932 case ARM_BUILTIN_TINSRW:
19933 icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
19934 : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
19935 : CODE_FOR_iwmmxt_tinsrw);
5039610b
SL
19936 arg0 = CALL_EXPR_ARG (exp, 0);
19937 arg1 = CALL_EXPR_ARG (exp, 1);
19938 arg2 = CALL_EXPR_ARG (exp, 2);
84217346
MD
19939 op0 = expand_normal (arg0);
19940 op1 = expand_normal (arg1);
19941 op2 = expand_normal (arg2);
5a9335ef
NC
19942 tmode = insn_data[icode].operand[0].mode;
19943 mode0 = insn_data[icode].operand[1].mode;
19944 mode1 = insn_data[icode].operand[2].mode;
19945 mode2 = insn_data[icode].operand[3].mode;
19946
19947 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19948 op0 = copy_to_mode_reg (mode0, op0);
19949 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
19950 op1 = copy_to_mode_reg (mode1, op1);
19951 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
19952 {
19953 /* @@@ better error message */
19954 error ("selector must be an immediate");
19955 return const0_rtx;
19956 }
19957 if (target == 0
19958 || GET_MODE (target) != tmode
19959 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19960 target = gen_reg_rtx (tmode);
19961 pat = GEN_FCN (icode) (target, op0, op1, op2);
19962 if (! pat)
19963 return 0;
19964 emit_insn (pat);
19965 return target;
19966
19967 case ARM_BUILTIN_SETWCX:
5039610b
SL
19968 arg0 = CALL_EXPR_ARG (exp, 0);
19969 arg1 = CALL_EXPR_ARG (exp, 1);
84217346
MD
19970 op0 = force_reg (SImode, expand_normal (arg0));
19971 op1 = expand_normal (arg1);
f07a6b21 19972 emit_insn (gen_iwmmxt_tmcr (op1, op0));
5a9335ef
NC
19973 return 0;
19974
19975 case ARM_BUILTIN_GETWCX:
5039610b 19976 arg0 = CALL_EXPR_ARG (exp, 0);
84217346 19977 op0 = expand_normal (arg0);
5a9335ef
NC
19978 target = gen_reg_rtx (SImode);
19979 emit_insn (gen_iwmmxt_tmrc (target, op0));
19980 return target;
19981
19982 case ARM_BUILTIN_WSHUFH:
19983 icode = CODE_FOR_iwmmxt_wshufh;
5039610b
SL
19984 arg0 = CALL_EXPR_ARG (exp, 0);
19985 arg1 = CALL_EXPR_ARG (exp, 1);
84217346
MD
19986 op0 = expand_normal (arg0);
19987 op1 = expand_normal (arg1);
5a9335ef
NC
19988 tmode = insn_data[icode].operand[0].mode;
19989 mode1 = insn_data[icode].operand[1].mode;
19990 mode2 = insn_data[icode].operand[2].mode;
19991
19992 if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
19993 op0 = copy_to_mode_reg (mode1, op0);
19994 if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
19995 {
19996 /* @@@ better error message */
19997 error ("mask must be an immediate");
19998 return const0_rtx;
19999 }
20000 if (target == 0
20001 || GET_MODE (target) != tmode
20002 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
20003 target = gen_reg_rtx (tmode);
20004 pat = GEN_FCN (icode) (target, op0, op1);
20005 if (! pat)
20006 return 0;
20007 emit_insn (pat);
20008 return target;
20009
20010 case ARM_BUILTIN_WSADB:
5039610b 20011 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, exp, target);
5a9335ef 20012 case ARM_BUILTIN_WSADH:
5039610b 20013 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, exp, target);
5a9335ef 20014 case ARM_BUILTIN_WSADBZ:
5039610b 20015 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, exp, target);
5a9335ef 20016 case ARM_BUILTIN_WSADHZ:
5039610b 20017 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, exp, target);
5a9335ef
NC
20018
20019 /* Several three-argument builtins. */
20020 case ARM_BUILTIN_WMACS:
20021 case ARM_BUILTIN_WMACU:
20022 case ARM_BUILTIN_WALIGN:
20023 case ARM_BUILTIN_TMIA:
20024 case ARM_BUILTIN_TMIAPH:
20025 case ARM_BUILTIN_TMIATT:
20026 case ARM_BUILTIN_TMIATB:
20027 case ARM_BUILTIN_TMIABT:
20028 case ARM_BUILTIN_TMIABB:
20029 icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
20030 : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
20031 : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
20032 : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
20033 : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
20034 : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
20035 : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
20036 : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
20037 : CODE_FOR_iwmmxt_walign);
5039610b
SL
20038 arg0 = CALL_EXPR_ARG (exp, 0);
20039 arg1 = CALL_EXPR_ARG (exp, 1);
20040 arg2 = CALL_EXPR_ARG (exp, 2);
84217346
MD
20041 op0 = expand_normal (arg0);
20042 op1 = expand_normal (arg1);
20043 op2 = expand_normal (arg2);
5a9335ef
NC
20044 tmode = insn_data[icode].operand[0].mode;
20045 mode0 = insn_data[icode].operand[1].mode;
20046 mode1 = insn_data[icode].operand[2].mode;
20047 mode2 = insn_data[icode].operand[3].mode;
20048
20049 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
20050 op0 = copy_to_mode_reg (mode0, op0);
20051 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
20052 op1 = copy_to_mode_reg (mode1, op1);
20053 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
20054 op2 = copy_to_mode_reg (mode2, op2);
20055 if (target == 0
20056 || GET_MODE (target) != tmode
20057 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
20058 target = gen_reg_rtx (tmode);
20059 pat = GEN_FCN (icode) (target, op0, op1, op2);
20060 if (! pat)
20061 return 0;
20062 emit_insn (pat);
20063 return target;
f676971a 20064
5a9335ef
NC
20065 case ARM_BUILTIN_WZERO:
20066 target = gen_reg_rtx (DImode);
20067 emit_insn (gen_iwmmxt_clrdi (target));
20068 return target;
20069
d3585b76
DJ
20070 case ARM_BUILTIN_THREAD_POINTER:
20071 return arm_load_tp (target);
20072
5a9335ef
NC
20073 default:
20074 break;
20075 }
20076
e97a46ce 20077 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
5a9335ef 20078 if (d->code == (const enum arm_builtins) fcode)
5039610b 20079 return arm_expand_binop_builtin (d->icode, exp, target);
5a9335ef 20080
e97a46ce 20081 for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
5a9335ef 20082 if (d->code == (const enum arm_builtins) fcode)
5039610b 20083 return arm_expand_unop_builtin (d->icode, exp, target, 0);
5a9335ef
NC
20084
20085 /* @@@ Should really do something sensible here. */
20086 return NULL_RTX;
20087}
20088\f
1d6e90ac
NC
20089/* Return the number (counting from 0) of
20090 the least significant set bit in MASK. */
20091
e32bac5b 20092inline static int
b279b20a 20093number_of_first_bit_set (unsigned mask)
d5b7b3ae
RE
20094{
20095 int bit;
20096
20097 for (bit = 0;
20098 (mask & (1 << bit)) == 0;
5895f793 20099 ++bit)
d5b7b3ae
RE
20100 continue;
20101
20102 return bit;
20103}
20104
b279b20a
NC
20105/* Emit code to push or pop registers to or from the stack. F is the
20106 assembly file. MASK is the registers to push or pop. PUSH is
20107 nonzero if we should push, and zero if we should pop. For debugging
20108 output, if pushing, adjust CFA_OFFSET by the amount of space added
20109 to the stack. REAL_REGS should have the same number of bits set as
20110 MASK, and will be used instead (in the same order) to describe which
20111 registers were saved - this is used to mark the save slots when we
20112 push high registers after moving them to low registers. */
20113static void
20114thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
20115 unsigned long real_regs)
20116{
20117 int regno;
20118 int lo_mask = mask & 0xFF;
20119 int pushed_words = 0;
20120
e6d29d15 20121 gcc_assert (mask);
b279b20a
NC
20122
20123 if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
20124 {
20125 /* Special case. Do not generate a POP PC statement here, do it in
20126 thumb_exit() */
20127 thumb_exit (f, -1);
20128 return;
20129 }
20130
d5fabb58 20131 if (push && arm_except_unwind_info (&global_options) == UI_TARGET)
617a1b71
PB
20132 {
20133 fprintf (f, "\t.save\t{");
20134 for (regno = 0; regno < 15; regno++)
20135 {
20136 if (real_regs & (1 << regno))
20137 {
20138 if (real_regs & ((1 << regno) -1))
20139 fprintf (f, ", ");
20140 asm_fprintf (f, "%r", regno);
20141 }
20142 }
20143 fprintf (f, "}\n");
20144 }
20145
b279b20a
NC
20146 fprintf (f, "\t%s\t{", push ? "push" : "pop");
20147
20148 /* Look at the low registers first. */
20149 for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
20150 {
20151 if (lo_mask & 1)
20152 {
20153 asm_fprintf (f, "%r", regno);
20154
20155 if ((lo_mask & ~1) != 0)
20156 fprintf (f, ", ");
20157
20158 pushed_words++;
20159 }
20160 }
20161
20162 if (push && (mask & (1 << LR_REGNUM)))
20163 {
20164 /* Catch pushing the LR. */
20165 if (mask & 0xFF)
20166 fprintf (f, ", ");
20167
20168 asm_fprintf (f, "%r", LR_REGNUM);
20169
20170 pushed_words++;
20171 }
20172 else if (!push && (mask & (1 << PC_REGNUM)))
20173 {
20174 /* Catch popping the PC. */
20175 if (TARGET_INTERWORK || TARGET_BACKTRACE
e3b5732b 20176 || crtl->calls_eh_return)
b279b20a
NC
20177 {
20178 /* The PC is never poped directly, instead
20179 it is popped into r3 and then BX is used. */
20180 fprintf (f, "}\n");
20181
20182 thumb_exit (f, -1);
20183
20184 return;
20185 }
20186 else
20187 {
20188 if (mask & 0xFF)
20189 fprintf (f, ", ");
20190
20191 asm_fprintf (f, "%r", PC_REGNUM);
20192 }
20193 }
20194
20195 fprintf (f, "}\n");
20196
20197 if (push && pushed_words && dwarf2out_do_frame ())
20198 {
d342c045 20199 char *l = dwarf2out_cfi_label (false);
b279b20a
NC
20200 int pushed_mask = real_regs;
20201
20202 *cfa_offset += pushed_words * 4;
20203 dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
20204
20205 pushed_words = 0;
20206 pushed_mask = real_regs;
20207 for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
20208 {
20209 if (pushed_mask & 1)
20210 dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
20211 }
20212 }
20213}
20214
d5b7b3ae
RE
20215/* Generate code to return from a thumb function.
20216 If 'reg_containing_return_addr' is -1, then the return address is
20217 actually on the stack, at the stack pointer. */
20218static void
c9ca9b88 20219thumb_exit (FILE *f, int reg_containing_return_addr)
d5b7b3ae
RE
20220{
20221 unsigned regs_available_for_popping;
20222 unsigned regs_to_pop;
20223 int pops_needed;
20224 unsigned available;
20225 unsigned required;
20226 int mode;
20227 int size;
20228 int restore_a4 = FALSE;
20229
20230 /* Compute the registers we need to pop. */
20231 regs_to_pop = 0;
20232 pops_needed = 0;
20233
c9ca9b88 20234 if (reg_containing_return_addr == -1)
d5b7b3ae 20235 {
d5b7b3ae 20236 regs_to_pop |= 1 << LR_REGNUM;
5895f793 20237 ++pops_needed;
d5b7b3ae
RE
20238 }
20239
20240 if (TARGET_BACKTRACE)
20241 {
20242 /* Restore the (ARM) frame pointer and stack pointer. */
20243 regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
20244 pops_needed += 2;
20245 }
20246
20247 /* If there is nothing to pop then just emit the BX instruction and
20248 return. */
20249 if (pops_needed == 0)
20250 {
e3b5732b 20251 if (crtl->calls_eh_return)
c9ca9b88 20252 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
d5b7b3ae
RE
20253
20254 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
20255 return;
20256 }
20257 /* Otherwise if we are not supporting interworking and we have not created
20258 a backtrace structure and the function was not entered in ARM mode then
20259 just pop the return address straight into the PC. */
5895f793
RE
20260 else if (!TARGET_INTERWORK
20261 && !TARGET_BACKTRACE
c9ca9b88 20262 && !is_called_in_ARM_mode (current_function_decl)
e3b5732b 20263 && !crtl->calls_eh_return)
d5b7b3ae 20264 {
c9ca9b88 20265 asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
d5b7b3ae
RE
20266 return;
20267 }
20268
20269 /* Find out how many of the (return) argument registers we can corrupt. */
20270 regs_available_for_popping = 0;
20271
20272 /* If returning via __builtin_eh_return, the bottom three registers
20273 all contain information needed for the return. */
e3b5732b 20274 if (crtl->calls_eh_return)
d5b7b3ae
RE
20275 size = 12;
20276 else
20277 {
d5b7b3ae
RE
20278 /* If we can deduce the registers used from the function's
20279 return value. This is more reliable that examining
6fb5fa3c 20280 df_regs_ever_live_p () because that will be set if the register is
d5b7b3ae
RE
20281 ever used in the function, not just if the register is used
20282 to hold a return value. */
20283
38173d38
JH
20284 if (crtl->return_rtx != 0)
20285 mode = GET_MODE (crtl->return_rtx);
d5b7b3ae 20286 else
d5b7b3ae
RE
20287 mode = DECL_MODE (DECL_RESULT (current_function_decl));
20288
20289 size = GET_MODE_SIZE (mode);
20290
20291 if (size == 0)
20292 {
20293 /* In a void function we can use any argument register.
20294 In a function that returns a structure on the stack
20295 we can use the second and third argument registers. */
20296 if (mode == VOIDmode)
20297 regs_available_for_popping =
20298 (1 << ARG_REGISTER (1))
20299 | (1 << ARG_REGISTER (2))
20300 | (1 << ARG_REGISTER (3));
20301 else
20302 regs_available_for_popping =
20303 (1 << ARG_REGISTER (2))
20304 | (1 << ARG_REGISTER (3));
20305 }
20306 else if (size <= 4)
20307 regs_available_for_popping =
20308 (1 << ARG_REGISTER (2))
20309 | (1 << ARG_REGISTER (3));
20310 else if (size <= 8)
20311 regs_available_for_popping =
20312 (1 << ARG_REGISTER (3));
20313 }
20314
20315 /* Match registers to be popped with registers into which we pop them. */
20316 for (available = regs_available_for_popping,
20317 required = regs_to_pop;
20318 required != 0 && available != 0;
20319 available &= ~(available & - available),
20320 required &= ~(required & - required))
20321 -- pops_needed;
20322
20323 /* If we have any popping registers left over, remove them. */
20324 if (available > 0)
5895f793 20325 regs_available_for_popping &= ~available;
f676971a 20326
d5b7b3ae
RE
20327 /* Otherwise if we need another popping register we can use
20328 the fourth argument register. */
20329 else if (pops_needed)
20330 {
20331 /* If we have not found any free argument registers and
20332 reg a4 contains the return address, we must move it. */
20333 if (regs_available_for_popping == 0
20334 && reg_containing_return_addr == LAST_ARG_REGNUM)
20335 {
20336 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
20337 reg_containing_return_addr = LR_REGNUM;
20338 }
20339 else if (size > 12)
20340 {
20341 /* Register a4 is being used to hold part of the return value,
20342 but we have dire need of a free, low register. */
20343 restore_a4 = TRUE;
f676971a 20344
d5b7b3ae
RE
20345 asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
20346 }
f676971a 20347
d5b7b3ae
RE
20348 if (reg_containing_return_addr != LAST_ARG_REGNUM)
20349 {
20350 /* The fourth argument register is available. */
20351 regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
f676971a 20352
5895f793 20353 --pops_needed;
d5b7b3ae
RE
20354 }
20355 }
20356
20357 /* Pop as many registers as we can. */
980e61bb
DJ
20358 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
20359 regs_available_for_popping);
d5b7b3ae
RE
20360
20361 /* Process the registers we popped. */
20362 if (reg_containing_return_addr == -1)
20363 {
20364 /* The return address was popped into the lowest numbered register. */
5895f793 20365 regs_to_pop &= ~(1 << LR_REGNUM);
f676971a 20366
d5b7b3ae
RE
20367 reg_containing_return_addr =
20368 number_of_first_bit_set (regs_available_for_popping);
20369
20370 /* Remove this register for the mask of available registers, so that
6bc82793 20371 the return address will not be corrupted by further pops. */
5895f793 20372 regs_available_for_popping &= ~(1 << reg_containing_return_addr);
d5b7b3ae
RE
20373 }
20374
20375 /* If we popped other registers then handle them here. */
20376 if (regs_available_for_popping)
20377 {
20378 int frame_pointer;
f676971a 20379
d5b7b3ae
RE
20380 /* Work out which register currently contains the frame pointer. */
20381 frame_pointer = number_of_first_bit_set (regs_available_for_popping);
20382
20383 /* Move it into the correct place. */
20384 asm_fprintf (f, "\tmov\t%r, %r\n",
20385 ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
20386
20387 /* (Temporarily) remove it from the mask of popped registers. */
5895f793
RE
20388 regs_available_for_popping &= ~(1 << frame_pointer);
20389 regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
f676971a 20390
d5b7b3ae
RE
20391 if (regs_available_for_popping)
20392 {
20393 int stack_pointer;
f676971a 20394
d5b7b3ae
RE
20395 /* We popped the stack pointer as well,
20396 find the register that contains it. */
20397 stack_pointer = number_of_first_bit_set (regs_available_for_popping);
20398
20399 /* Move it into the stack register. */
20400 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
f676971a 20401
d5b7b3ae
RE
20402 /* At this point we have popped all necessary registers, so
20403 do not worry about restoring regs_available_for_popping
20404 to its correct value:
20405
20406 assert (pops_needed == 0)
20407 assert (regs_available_for_popping == (1 << frame_pointer))
20408 assert (regs_to_pop == (1 << STACK_POINTER)) */
20409 }
20410 else
20411 {
20412 /* Since we have just move the popped value into the frame
20413 pointer, the popping register is available for reuse, and
20414 we know that we still have the stack pointer left to pop. */
20415 regs_available_for_popping |= (1 << frame_pointer);
20416 }
20417 }
f676971a 20418
d5b7b3ae
RE
20419 /* If we still have registers left on the stack, but we no longer have
20420 any registers into which we can pop them, then we must move the return
20421 address into the link register and make available the register that
20422 contained it. */
20423 if (regs_available_for_popping == 0 && pops_needed > 0)
20424 {
20425 regs_available_for_popping |= 1 << reg_containing_return_addr;
f676971a 20426
d5b7b3ae
RE
20427 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
20428 reg_containing_return_addr);
f676971a 20429
d5b7b3ae
RE
20430 reg_containing_return_addr = LR_REGNUM;
20431 }
20432
20433 /* If we have registers left on the stack then pop some more.
20434 We know that at most we will want to pop FP and SP. */
20435 if (pops_needed > 0)
20436 {
20437 int popped_into;
20438 int move_to;
f676971a 20439
980e61bb
DJ
20440 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
20441 regs_available_for_popping);
d5b7b3ae
RE
20442
20443 /* We have popped either FP or SP.
20444 Move whichever one it is into the correct register. */
20445 popped_into = number_of_first_bit_set (regs_available_for_popping);
20446 move_to = number_of_first_bit_set (regs_to_pop);
20447
20448 asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
20449
5895f793 20450 regs_to_pop &= ~(1 << move_to);
d5b7b3ae 20451
5895f793 20452 --pops_needed;
d5b7b3ae 20453 }
f676971a 20454
d5b7b3ae
RE
20455 /* If we still have not popped everything then we must have only
20456 had one register available to us and we are now popping the SP. */
20457 if (pops_needed > 0)
20458 {
20459 int popped_into;
f676971a 20460
980e61bb
DJ
20461 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
20462 regs_available_for_popping);
d5b7b3ae
RE
20463
20464 popped_into = number_of_first_bit_set (regs_available_for_popping);
20465
20466 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
20467 /*
20468 assert (regs_to_pop == (1 << STACK_POINTER))
20469 assert (pops_needed == 1)
20470 */
20471 }
20472
20473 /* If necessary restore the a4 register. */
20474 if (restore_a4)
20475 {
20476 if (reg_containing_return_addr != LR_REGNUM)
20477 {
20478 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
20479 reg_containing_return_addr = LR_REGNUM;
20480 }
f676971a 20481
d5b7b3ae
RE
20482 asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
20483 }
20484
e3b5732b 20485 if (crtl->calls_eh_return)
c9ca9b88 20486 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
d5b7b3ae
RE
20487
20488 /* Return to caller. */
20489 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
20490}
d5b7b3ae 20491\f
906668bb
BS
20492/* Scan INSN just before assembler is output for it.
20493 For Thumb-1, we track the status of the condition codes; this
20494 information is used in the cbranchsi4_insn pattern. */
d5b7b3ae 20495void
5b3e6663 20496thumb1_final_prescan_insn (rtx insn)
d5b7b3ae 20497{
d5b7b3ae 20498 if (flag_print_asm_name)
9d98a694
AO
20499 asm_fprintf (asm_out_file, "%@ 0x%04x\n",
20500 INSN_ADDRESSES (INSN_UID (insn)));
906668bb
BS
20501 /* Don't overwrite the previous setter when we get to a cbranch. */
20502 if (INSN_CODE (insn) != CODE_FOR_cbranchsi4_insn)
20503 {
20504 enum attr_conds conds;
20505
20506 if (cfun->machine->thumb1_cc_insn)
20507 {
20508 if (modified_in_p (cfun->machine->thumb1_cc_op0, insn)
20509 || modified_in_p (cfun->machine->thumb1_cc_op1, insn))
20510 CC_STATUS_INIT;
20511 }
20512 conds = get_attr_conds (insn);
20513 if (conds == CONDS_SET)
20514 {
20515 rtx set = single_set (insn);
20516 cfun->machine->thumb1_cc_insn = insn;
20517 cfun->machine->thumb1_cc_op0 = SET_DEST (set);
20518 cfun->machine->thumb1_cc_op1 = const0_rtx;
20519 cfun->machine->thumb1_cc_mode = CC_NOOVmode;
20520 if (INSN_CODE (insn) == CODE_FOR_thumb1_subsi3_insn)
20521 {
20522 rtx src1 = XEXP (SET_SRC (set), 1);
20523 if (src1 == const0_rtx)
20524 cfun->machine->thumb1_cc_mode = CCmode;
20525 }
20526 }
20527 else if (conds != CONDS_NOCOND)
20528 cfun->machine->thumb1_cc_insn = NULL_RTX;
20529 }
d5b7b3ae
RE
20530}
20531
20532int
e32bac5b 20533thumb_shiftable_const (unsigned HOST_WIDE_INT val)
d5b7b3ae
RE
20534{
20535 unsigned HOST_WIDE_INT mask = 0xff;
20536 int i;
20537
ce41c38b 20538 val = val & (unsigned HOST_WIDE_INT)0xffffffffu;
d5b7b3ae
RE
20539 if (val == 0) /* XXX */
20540 return 0;
f676971a 20541
d5b7b3ae
RE
20542 for (i = 0; i < 25; i++)
20543 if ((val & (mask << i)) == val)
20544 return 1;
20545
20546 return 0;
20547}
20548
825dda42 20549/* Returns nonzero if the current function contains,
d5b7b3ae 20550 or might contain a far jump. */
5848830f
PB
20551static int
20552thumb_far_jump_used_p (void)
d5b7b3ae
RE
20553{
20554 rtx insn;
20555
20556 /* This test is only important for leaf functions. */
5895f793 20557 /* assert (!leaf_function_p ()); */
f676971a 20558
d5b7b3ae
RE
20559 /* If we have already decided that far jumps may be used,
20560 do not bother checking again, and always return true even if
20561 it turns out that they are not being used. Once we have made
20562 the decision that far jumps are present (and that hence the link
20563 register will be pushed onto the stack) we cannot go back on it. */
20564 if (cfun->machine->far_jump_used)
20565 return 1;
20566
20567 /* If this function is not being called from the prologue/epilogue
20568 generation code then it must be being called from the
20569 INITIAL_ELIMINATION_OFFSET macro. */
5848830f 20570 if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
d5b7b3ae
RE
20571 {
20572 /* In this case we know that we are being asked about the elimination
20573 of the arg pointer register. If that register is not being used,
20574 then there are no arguments on the stack, and we do not have to
20575 worry that a far jump might force the prologue to push the link
20576 register, changing the stack offsets. In this case we can just
20577 return false, since the presence of far jumps in the function will
20578 not affect stack offsets.
20579
20580 If the arg pointer is live (or if it was live, but has now been
20581 eliminated and so set to dead) then we do have to test to see if
20582 the function might contain a far jump. This test can lead to some
20583 false negatives, since before reload is completed, then length of
20584 branch instructions is not known, so gcc defaults to returning their
20585 longest length, which in turn sets the far jump attribute to true.
20586
20587 A false negative will not result in bad code being generated, but it
20588 will result in a needless push and pop of the link register. We
5848830f
PB
20589 hope that this does not occur too often.
20590
20591 If we need doubleword stack alignment this could affect the other
20592 elimination offsets so we can't risk getting it wrong. */
6fb5fa3c 20593 if (df_regs_ever_live_p (ARG_POINTER_REGNUM))
d5b7b3ae 20594 cfun->machine->arg_pointer_live = 1;
5895f793 20595 else if (!cfun->machine->arg_pointer_live)
d5b7b3ae
RE
20596 return 0;
20597 }
20598
20599 /* Check to see if the function contains a branch
20600 insn with the far jump attribute set. */
20601 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
20602 {
20603 if (GET_CODE (insn) == JUMP_INSN
20604 /* Ignore tablejump patterns. */
20605 && GET_CODE (PATTERN (insn)) != ADDR_VEC
20606 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
20607 && get_attr_far_jump (insn) == FAR_JUMP_YES
20608 )
20609 {
9a9f7594 20610 /* Record the fact that we have decided that
d5b7b3ae
RE
20611 the function does use far jumps. */
20612 cfun->machine->far_jump_used = 1;
20613 return 1;
20614 }
20615 }
f676971a 20616
d5b7b3ae
RE
20617 return 0;
20618}
20619
825dda42 20620/* Return nonzero if FUNC must be entered in ARM mode. */
d5b7b3ae 20621int
e32bac5b 20622is_called_in_ARM_mode (tree func)
d5b7b3ae 20623{
e6d29d15 20624 gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
d5b7b3ae 20625
696e78bf 20626 /* Ignore the problem about functions whose address is taken. */
d5b7b3ae
RE
20627 if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
20628 return TRUE;
20629
f676971a 20630#ifdef ARM_PE
91d231cb 20631 return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
d5b7b3ae
RE
20632#else
20633 return FALSE;
20634#endif
20635}
20636
e784c52c
BS
20637/* Given the stack offsets and register mask in OFFSETS, decide how
20638 many additional registers to push instead of subtracting a constant
20639 from SP. For epilogues the principle is the same except we use pop.
20640 FOR_PROLOGUE indicates which we're generating. */
20641static int
20642thumb1_extra_regs_pushed (arm_stack_offsets *offsets, bool for_prologue)
20643{
20644 HOST_WIDE_INT amount;
20645 unsigned long live_regs_mask = offsets->saved_regs_mask;
20646 /* Extract a mask of the ones we can give to the Thumb's push/pop
20647 instruction. */
20648 unsigned long l_mask = live_regs_mask & (for_prologue ? 0x40ff : 0xff);
20649 /* Then count how many other high registers will need to be pushed. */
20650 unsigned long high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
20651 int n_free, reg_base;
20652
20653 if (!for_prologue && frame_pointer_needed)
20654 amount = offsets->locals_base - offsets->saved_regs;
20655 else
20656 amount = offsets->outgoing_args - offsets->saved_regs;
20657
20658 /* If the stack frame size is 512 exactly, we can save one load
20659 instruction, which should make this a win even when optimizing
20660 for speed. */
20661 if (!optimize_size && amount != 512)
20662 return 0;
20663
20664 /* Can't do this if there are high registers to push. */
20665 if (high_regs_pushed != 0)
20666 return 0;
20667
20668 /* Shouldn't do it in the prologue if no registers would normally
20669 be pushed at all. In the epilogue, also allow it if we'll have
20670 a pop insn for the PC. */
20671 if (l_mask == 0
20672 && (for_prologue
20673 || TARGET_BACKTRACE
20674 || (live_regs_mask & 1 << LR_REGNUM) == 0
20675 || TARGET_INTERWORK
20676 || crtl->args.pretend_args_size != 0))
20677 return 0;
20678
20679 /* Don't do this if thumb_expand_prologue wants to emit instructions
20680 between the push and the stack frame allocation. */
20681 if (for_prologue
20682 && ((flag_pic && arm_pic_register != INVALID_REGNUM)
20683 || (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)))
20684 return 0;
20685
20686 reg_base = 0;
20687 n_free = 0;
20688 if (!for_prologue)
20689 {
20690 reg_base = arm_size_return_regs () / UNITS_PER_WORD;
20691 live_regs_mask >>= reg_base;
20692 }
20693
20694 while (reg_base + n_free < 8 && !(live_regs_mask & 1)
20695 && (for_prologue || call_used_regs[reg_base + n_free]))
20696 {
20697 live_regs_mask >>= 1;
20698 n_free++;
20699 }
20700
20701 if (n_free == 0)
20702 return 0;
20703 gcc_assert (amount / 4 * 4 == amount);
20704
20705 if (amount >= 512 && (amount - n_free * 4) < 512)
20706 return (amount - 508) / 4;
20707 if (amount <= n_free * 4)
20708 return amount / 4;
20709 return 0;
20710}
20711
d6b4baa4 20712/* The bits which aren't usefully expanded as rtl. */
cd2b33d0 20713const char *
e32bac5b 20714thumb_unexpanded_epilogue (void)
d5b7b3ae 20715{
954954d1 20716 arm_stack_offsets *offsets;
d5b7b3ae 20717 int regno;
b279b20a 20718 unsigned long live_regs_mask = 0;
d5b7b3ae 20719 int high_regs_pushed = 0;
e784c52c 20720 int extra_pop;
d5b7b3ae 20721 int had_to_push_lr;
57934c39 20722 int size;
d5b7b3ae 20723
934c2060 20724 if (cfun->machine->return_used_this_function != 0)
d5b7b3ae
RE
20725 return "";
20726
58e60158
AN
20727 if (IS_NAKED (arm_current_func_type ()))
20728 return "";
20729
954954d1
PB
20730 offsets = arm_get_frame_offsets ();
20731 live_regs_mask = offsets->saved_regs_mask;
57934c39
PB
20732 high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
20733
20734 /* If we can deduce the registers used from the function's return value.
6fb5fa3c 20735 This is more reliable that examining df_regs_ever_live_p () because that
57934c39
PB
20736 will be set if the register is ever used in the function, not just if
20737 the register is used to hold a return value. */
4f5dfed0 20738 size = arm_size_return_regs ();
d5b7b3ae 20739
e784c52c
BS
20740 extra_pop = thumb1_extra_regs_pushed (offsets, false);
20741 if (extra_pop > 0)
20742 {
20743 unsigned long extra_mask = (1 << extra_pop) - 1;
20744 live_regs_mask |= extra_mask << (size / UNITS_PER_WORD);
20745 }
20746
d5b7b3ae 20747 /* The prolog may have pushed some high registers to use as
112cdef5 20748 work registers. e.g. the testsuite file:
d5b7b3ae
RE
20749 gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
20750 compiles to produce:
20751 push {r4, r5, r6, r7, lr}
20752 mov r7, r9
20753 mov r6, r8
20754 push {r6, r7}
20755 as part of the prolog. We have to undo that pushing here. */
f676971a 20756
d5b7b3ae
RE
20757 if (high_regs_pushed)
20758 {
b279b20a 20759 unsigned long mask = live_regs_mask & 0xff;
d5b7b3ae 20760 int next_hi_reg;
d5b7b3ae 20761
57934c39
PB
20762 /* The available low registers depend on the size of the value we are
20763 returning. */
20764 if (size <= 12)
d5b7b3ae 20765 mask |= 1 << 3;
57934c39
PB
20766 if (size <= 8)
20767 mask |= 1 << 2;
d5b7b3ae
RE
20768
20769 if (mask == 0)
20770 /* Oh dear! We have no low registers into which we can pop
20771 high registers! */
400500c4
RK
20772 internal_error
20773 ("no low registers available for popping high registers");
f676971a 20774
d5b7b3ae 20775 for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
57934c39 20776 if (live_regs_mask & (1 << next_hi_reg))
d5b7b3ae
RE
20777 break;
20778
20779 while (high_regs_pushed)
20780 {
20781 /* Find lo register(s) into which the high register(s) can
20782 be popped. */
20783 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
20784 {
20785 if (mask & (1 << regno))
20786 high_regs_pushed--;
20787 if (high_regs_pushed == 0)
20788 break;
20789 }
20790
20791 mask &= (2 << regno) - 1; /* A noop if regno == 8 */
20792
d6b4baa4 20793 /* Pop the values into the low register(s). */
980e61bb 20794 thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
d5b7b3ae
RE
20795
20796 /* Move the value(s) into the high registers. */
20797 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
20798 {
20799 if (mask & (1 << regno))
20800 {
20801 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
20802 regno);
f676971a 20803
d5b7b3ae 20804 for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
57934c39 20805 if (live_regs_mask & (1 << next_hi_reg))
d5b7b3ae
RE
20806 break;
20807 }
20808 }
20809 }
57934c39 20810 live_regs_mask &= ~0x0f00;
d5b7b3ae
RE
20811 }
20812
57934c39
PB
20813 had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
20814 live_regs_mask &= 0xff;
20815
38173d38 20816 if (crtl->args.pretend_args_size == 0 || TARGET_BACKTRACE)
d5b7b3ae 20817 {
f676971a 20818 /* Pop the return address into the PC. */
57934c39 20819 if (had_to_push_lr)
d5b7b3ae
RE
20820 live_regs_mask |= 1 << PC_REGNUM;
20821
20822 /* Either no argument registers were pushed or a backtrace
20823 structure was created which includes an adjusted stack
20824 pointer, so just pop everything. */
20825 if (live_regs_mask)
980e61bb
DJ
20826 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
20827 live_regs_mask);
57934c39 20828
d5b7b3ae 20829 /* We have either just popped the return address into the
e784c52c
BS
20830 PC or it is was kept in LR for the entire function.
20831 Note that thumb_pushpop has already called thumb_exit if the
20832 PC was in the list. */
57934c39
PB
20833 if (!had_to_push_lr)
20834 thumb_exit (asm_out_file, LR_REGNUM);
d5b7b3ae
RE
20835 }
20836 else
20837 {
20838 /* Pop everything but the return address. */
d5b7b3ae 20839 if (live_regs_mask)
980e61bb
DJ
20840 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
20841 live_regs_mask);
d5b7b3ae
RE
20842
20843 if (had_to_push_lr)
57934c39
PB
20844 {
20845 if (size > 12)
20846 {
20847 /* We have no free low regs, so save one. */
20848 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
20849 LAST_ARG_REGNUM);
20850 }
20851
20852 /* Get the return address into a temporary register. */
20853 thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
20854 1 << LAST_ARG_REGNUM);
20855
20856 if (size > 12)
20857 {
20858 /* Move the return address to lr. */
20859 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
20860 LAST_ARG_REGNUM);
20861 /* Restore the low register. */
20862 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
20863 IP_REGNUM);
20864 regno = LR_REGNUM;
20865 }
20866 else
20867 regno = LAST_ARG_REGNUM;
20868 }
20869 else
20870 regno = LR_REGNUM;
f676971a 20871
d5b7b3ae
RE
20872 /* Remove the argument registers that were pushed onto the stack. */
20873 asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
20874 SP_REGNUM, SP_REGNUM,
38173d38 20875 crtl->args.pretend_args_size);
f676971a 20876
57934c39 20877 thumb_exit (asm_out_file, regno);
d5b7b3ae
RE
20878 }
20879
20880 return "";
20881}
20882
20883/* Functions to save and restore machine-specific function data. */
e2500fed 20884static struct machine_function *
e32bac5b 20885arm_init_machine_status (void)
d5b7b3ae 20886{
e2500fed 20887 struct machine_function *machine;
a9429e29 20888 machine = ggc_alloc_cleared_machine_function ();
6d3d9133 20889
f676971a 20890#if ARM_FT_UNKNOWN != 0
e2500fed 20891 machine->func_type = ARM_FT_UNKNOWN;
6d3d9133 20892#endif
e2500fed 20893 return machine;
f7a80099
NC
20894}
20895
d5b7b3ae
RE
20896/* Return an RTX indicating where the return address to the
20897 calling function can be found. */
20898rtx
e32bac5b 20899arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
d5b7b3ae 20900{
d5b7b3ae
RE
20901 if (count != 0)
20902 return NULL_RTX;
20903
61f0ccff 20904 return get_hard_reg_initial_val (Pmode, LR_REGNUM);
d5b7b3ae
RE
20905}
20906
20907/* Do anything needed before RTL is emitted for each function. */
20908void
e32bac5b 20909arm_init_expanders (void)
d5b7b3ae
RE
20910{
20911 /* Arrange to initialize and mark the machine per-function status. */
20912 init_machine_status = arm_init_machine_status;
3ac5ea7c
RH
20913
20914 /* This is to stop the combine pass optimizing away the alignment
20915 adjustment of va_arg. */
20916 /* ??? It is claimed that this should not be necessary. */
20917 if (cfun)
20918 mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
d5b7b3ae
RE
20919}
20920
0977774b 20921
2591db65
RE
20922/* Like arm_compute_initial_elimination offset. Simpler because there
20923 isn't an ABI specified frame pointer for Thumb. Instead, we set it
20924 to point at the base of the local variables after static stack
20925 space for a function has been allocated. */
0977774b 20926
5848830f
PB
20927HOST_WIDE_INT
20928thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
20929{
20930 arm_stack_offsets *offsets;
0977774b 20931
5848830f 20932 offsets = arm_get_frame_offsets ();
0977774b 20933
5848830f 20934 switch (from)
0977774b 20935 {
5848830f
PB
20936 case ARG_POINTER_REGNUM:
20937 switch (to)
20938 {
20939 case STACK_POINTER_REGNUM:
20940 return offsets->outgoing_args - offsets->saved_args;
0977774b 20941
5848830f
PB
20942 case FRAME_POINTER_REGNUM:
20943 return offsets->soft_frame - offsets->saved_args;
0977774b 20944
5848830f
PB
20945 case ARM_HARD_FRAME_POINTER_REGNUM:
20946 return offsets->saved_regs - offsets->saved_args;
0977774b 20947
2591db65
RE
20948 case THUMB_HARD_FRAME_POINTER_REGNUM:
20949 return offsets->locals_base - offsets->saved_args;
20950
5848830f 20951 default:
e6d29d15 20952 gcc_unreachable ();
5848830f
PB
20953 }
20954 break;
0977774b 20955
5848830f
PB
20956 case FRAME_POINTER_REGNUM:
20957 switch (to)
20958 {
20959 case STACK_POINTER_REGNUM:
20960 return offsets->outgoing_args - offsets->soft_frame;
0977774b 20961
5848830f
PB
20962 case ARM_HARD_FRAME_POINTER_REGNUM:
20963 return offsets->saved_regs - offsets->soft_frame;
0977774b 20964
2591db65
RE
20965 case THUMB_HARD_FRAME_POINTER_REGNUM:
20966 return offsets->locals_base - offsets->soft_frame;
20967
5848830f 20968 default:
e6d29d15 20969 gcc_unreachable ();
5848830f
PB
20970 }
20971 break;
0977774b 20972
5848830f 20973 default:
e6d29d15 20974 gcc_unreachable ();
5848830f 20975 }
0977774b
JT
20976}
20977
d5b7b3ae
RE
20978/* Generate the rest of a function's prologue. */
20979void
5b3e6663 20980thumb1_expand_prologue (void)
d5b7b3ae 20981{
980e61bb
DJ
20982 rtx insn, dwarf;
20983
5848830f
PB
20984 HOST_WIDE_INT amount;
20985 arm_stack_offsets *offsets;
6d3d9133 20986 unsigned long func_type;
3c7ad43e 20987 int regno;
57934c39 20988 unsigned long live_regs_mask;
6d3d9133
NC
20989
20990 func_type = arm_current_func_type ();
f676971a 20991
d5b7b3ae 20992 /* Naked functions don't have prologues. */
6d3d9133 20993 if (IS_NAKED (func_type))
d5b7b3ae
RE
20994 return;
20995
6d3d9133
NC
20996 if (IS_INTERRUPT (func_type))
20997 {
c725bd79 20998 error ("interrupt Service Routines cannot be coded in Thumb mode");
6d3d9133
NC
20999 return;
21000 }
21001
954954d1
PB
21002 offsets = arm_get_frame_offsets ();
21003 live_regs_mask = offsets->saved_regs_mask;
b279b20a
NC
21004 /* Load the pic register before setting the frame pointer,
21005 so we can use r7 as a temporary work register. */
020a4035 21006 if (flag_pic && arm_pic_register != INVALID_REGNUM)
e55ef7f4 21007 arm_load_pic_register (live_regs_mask);
876f13b0 21008
2591db65 21009 if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
a2503645
RS
21010 emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
21011 stack_pointer_rtx);
d5b7b3ae 21012
55256000
TK
21013 if (flag_stack_usage)
21014 current_function_static_stack_size
21015 = offsets->outgoing_args - offsets->saved_args;
21016
5848830f 21017 amount = offsets->outgoing_args - offsets->saved_regs;
e784c52c 21018 amount -= 4 * thumb1_extra_regs_pushed (offsets, true);
d5b7b3ae
RE
21019 if (amount)
21020 {
d5b7b3ae 21021 if (amount < 512)
980e61bb
DJ
21022 {
21023 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
21024 GEN_INT (- amount)));
21025 RTX_FRAME_RELATED_P (insn) = 1;
21026 }
d5b7b3ae
RE
21027 else
21028 {
d5b7b3ae
RE
21029 rtx reg;
21030
21031 /* The stack decrement is too big for an immediate value in a single
21032 insn. In theory we could issue multiple subtracts, but after
21033 three of them it becomes more space efficient to place the full
21034 value in the constant pool and load into a register. (Also the
21035 ARM debugger really likes to see only one stack decrement per
21036 function). So instead we look for a scratch register into which
21037 we can load the decrement, and then we subtract this from the
21038 stack pointer. Unfortunately on the thumb the only available
21039 scratch registers are the argument registers, and we cannot use
21040 these as they may hold arguments to the function. Instead we
21041 attempt to locate a call preserved register which is used by this
21042 function. If we can find one, then we know that it will have
21043 been pushed at the start of the prologue and so we can corrupt
21044 it now. */
21045 for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
35596784 21046 if (live_regs_mask & (1 << regno))
d5b7b3ae
RE
21047 break;
21048
35596784 21049 gcc_assert(regno <= LAST_LO_REGNUM);
d5b7b3ae 21050
35596784 21051 reg = gen_rtx_REG (SImode, regno);
d5b7b3ae 21052
35596784 21053 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
980e61bb 21054
35596784
AJ
21055 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
21056 stack_pointer_rtx, reg));
21057 RTX_FRAME_RELATED_P (insn) = 1;
21058 dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
21059 plus_constant (stack_pointer_rtx,
21060 -amount));
21061 RTX_FRAME_RELATED_P (dwarf) = 1;
bbbbb16a 21062 add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
d5b7b3ae 21063 }
2591db65
RE
21064 }
21065
21066 if (frame_pointer_needed)
5b3e6663 21067 thumb_set_frame_pointer (offsets);
f676971a 21068
74d9c39f
DJ
21069 /* If we are profiling, make sure no instructions are scheduled before
21070 the call to mcount. Similarly if the user has requested no
21071 scheduling in the prolog. Similarly if we want non-call exceptions
21072 using the EABI unwinder, to prevent faulting instructions from being
21073 swapped with a stack adjustment. */
e3b5732b 21074 if (crtl->profile || !TARGET_SCHED_PROLOG
d5fabb58 21075 || (arm_except_unwind_info (&global_options) == UI_TARGET
f0a0390e 21076 && cfun->can_throw_non_call_exceptions))
d5b7b3ae 21077 emit_insn (gen_blockage ());
3c7ad43e
PB
21078
21079 cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
57934c39
PB
21080 if (live_regs_mask & 0xff)
21081 cfun->machine->lr_save_eliminated = 0;
d5b7b3ae
RE
21082}
21083
57934c39 21084
d5b7b3ae 21085void
5b3e6663 21086thumb1_expand_epilogue (void)
d5b7b3ae 21087{
5848830f
PB
21088 HOST_WIDE_INT amount;
21089 arm_stack_offsets *offsets;
defc0463
RE
21090 int regno;
21091
6d3d9133
NC
21092 /* Naked functions don't have prologues. */
21093 if (IS_NAKED (arm_current_func_type ()))
d5b7b3ae
RE
21094 return;
21095
5848830f
PB
21096 offsets = arm_get_frame_offsets ();
21097 amount = offsets->outgoing_args - offsets->saved_regs;
21098
d5b7b3ae 21099 if (frame_pointer_needed)
2591db65
RE
21100 {
21101 emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
21102 amount = offsets->locals_base - offsets->saved_regs;
21103 }
e784c52c 21104 amount -= 4 * thumb1_extra_regs_pushed (offsets, false);
e0b92319 21105
a3a531ec 21106 gcc_assert (amount >= 0);
2591db65 21107 if (amount)
d5b7b3ae 21108 {
d5b7b3ae
RE
21109 if (amount < 512)
21110 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
21111 GEN_INT (amount)));
21112 else
21113 {
21114 /* r3 is always free in the epilogue. */
f1c25d3b 21115 rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
d5b7b3ae
RE
21116
21117 emit_insn (gen_movsi (reg, GEN_INT (amount)));
21118 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
21119 }
21120 }
f676971a 21121
d5b7b3ae
RE
21122 /* Emit a USE (stack_pointer_rtx), so that
21123 the stack adjustment will not be deleted. */
6bacc7b0 21124 emit_insn (gen_prologue_use (stack_pointer_rtx));
d5b7b3ae 21125
e3b5732b 21126 if (crtl->profile || !TARGET_SCHED_PROLOG)
d5b7b3ae 21127 emit_insn (gen_blockage ());
defc0463
RE
21128
21129 /* Emit a clobber for each insn that will be restored in the epilogue,
21130 so that flow2 will get register lifetimes correct. */
21131 for (regno = 0; regno < 13; regno++)
6fb5fa3c 21132 if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
c41c1387 21133 emit_clobber (gen_rtx_REG (SImode, regno));
defc0463 21134
6fb5fa3c 21135 if (! df_regs_ever_live_p (LR_REGNUM))
c41c1387 21136 emit_use (gen_rtx_REG (SImode, LR_REGNUM));
d5b7b3ae
RE
21137}
21138
08c148a8 21139static void
5b3e6663 21140thumb1_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
d5b7b3ae 21141{
954954d1 21142 arm_stack_offsets *offsets;
b279b20a
NC
21143 unsigned long live_regs_mask = 0;
21144 unsigned long l_mask;
21145 unsigned high_regs_pushed = 0;
980e61bb 21146 int cfa_offset = 0;
d5b7b3ae
RE
21147 int regno;
21148
6d3d9133 21149 if (IS_NAKED (arm_current_func_type ()))
d5b7b3ae
RE
21150 return;
21151
21152 if (is_called_in_ARM_mode (current_function_decl))
21153 {
21154 const char * name;
21155
e6d29d15
NS
21156 gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
21157 gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
21158 == SYMBOL_REF);
d5b7b3ae 21159 name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
f676971a 21160
d5b7b3ae
RE
21161 /* Generate code sequence to switch us into Thumb mode. */
21162 /* The .code 32 directive has already been emitted by
6d77b53e 21163 ASM_DECLARE_FUNCTION_NAME. */
d5b7b3ae
RE
21164 asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
21165 asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
21166
21167 /* Generate a label, so that the debugger will notice the
21168 change in instruction sets. This label is also used by
21169 the assembler to bypass the ARM code when this function
21170 is called from a Thumb encoded function elsewhere in the
21171 same file. Hence the definition of STUB_NAME here must
d6b4baa4 21172 agree with the definition in gas/config/tc-arm.c. */
f676971a 21173
d5b7b3ae 21174#define STUB_NAME ".real_start_of"
f676971a 21175
761c70aa 21176 fprintf (f, "\t.code\t16\n");
d5b7b3ae
RE
21177#ifdef ARM_PE
21178 if (arm_dllexport_name_p (name))
e5951263 21179 name = arm_strip_name_encoding (name);
f676971a 21180#endif
d5b7b3ae 21181 asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
761c70aa 21182 fprintf (f, "\t.thumb_func\n");
d5b7b3ae
RE
21183 asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
21184 }
f676971a 21185
38173d38 21186 if (crtl->args.pretend_args_size)
d5b7b3ae 21187 {
617a1b71 21188 /* Output unwind directive for the stack adjustment. */
d5fabb58 21189 if (arm_except_unwind_info (&global_options) == UI_TARGET)
617a1b71 21190 fprintf (f, "\t.pad #%d\n",
38173d38 21191 crtl->args.pretend_args_size);
617a1b71 21192
3cb66fd7 21193 if (cfun->machine->uses_anonymous_args)
d5b7b3ae
RE
21194 {
21195 int num_pushes;
f676971a 21196
761c70aa 21197 fprintf (f, "\tpush\t{");
d5b7b3ae 21198
38173d38 21199 num_pushes = ARM_NUM_INTS (crtl->args.pretend_args_size);
f676971a 21200
d5b7b3ae
RE
21201 for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
21202 regno <= LAST_ARG_REGNUM;
5895f793 21203 regno++)
d5b7b3ae
RE
21204 asm_fprintf (f, "%r%s", regno,
21205 regno == LAST_ARG_REGNUM ? "" : ", ");
21206
761c70aa 21207 fprintf (f, "}\n");
d5b7b3ae
RE
21208 }
21209 else
f676971a 21210 asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
d5b7b3ae 21211 SP_REGNUM, SP_REGNUM,
38173d38 21212 crtl->args.pretend_args_size);
980e61bb
DJ
21213
21214 /* We don't need to record the stores for unwinding (would it
21215 help the debugger any if we did?), but record the change in
21216 the stack pointer. */
21217 if (dwarf2out_do_frame ())
21218 {
d342c045 21219 char *l = dwarf2out_cfi_label (false);
b279b20a 21220
38173d38 21221 cfa_offset = cfa_offset + crtl->args.pretend_args_size;
980e61bb
DJ
21222 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
21223 }
d5b7b3ae
RE
21224 }
21225
b279b20a 21226 /* Get the registers we are going to push. */
954954d1
PB
21227 offsets = arm_get_frame_offsets ();
21228 live_regs_mask = offsets->saved_regs_mask;
b279b20a 21229 /* Extract a mask of the ones we can give to the Thumb's push instruction. */
57934c39 21230 l_mask = live_regs_mask & 0x40ff;
b279b20a
NC
21231 /* Then count how many other high registers will need to be pushed. */
21232 high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
d5b7b3ae
RE
21233
21234 if (TARGET_BACKTRACE)
21235 {
b279b20a
NC
21236 unsigned offset;
21237 unsigned work_register;
f676971a 21238
d5b7b3ae
RE
21239 /* We have been asked to create a stack backtrace structure.
21240 The code looks like this:
f676971a 21241
d5b7b3ae
RE
21242 0 .align 2
21243 0 func:
21244 0 sub SP, #16 Reserve space for 4 registers.
57934c39 21245 2 push {R7} Push low registers.
d5b7b3ae
RE
21246 4 add R7, SP, #20 Get the stack pointer before the push.
21247 6 str R7, [SP, #8] Store the stack pointer (before reserving the space).
21248 8 mov R7, PC Get hold of the start of this code plus 12.
21249 10 str R7, [SP, #16] Store it.
21250 12 mov R7, FP Get hold of the current frame pointer.
21251 14 str R7, [SP, #4] Store it.
21252 16 mov R7, LR Get hold of the current return address.
21253 18 str R7, [SP, #12] Store it.
21254 20 add R7, SP, #16 Point at the start of the backtrace structure.
21255 22 mov FP, R7 Put this value into the frame pointer. */
21256
57934c39 21257 work_register = thumb_find_work_register (live_regs_mask);
f676971a 21258
d5fabb58 21259 if (arm_except_unwind_info (&global_options) == UI_TARGET)
617a1b71
PB
21260 asm_fprintf (f, "\t.pad #16\n");
21261
d5b7b3ae
RE
21262 asm_fprintf
21263 (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
21264 SP_REGNUM, SP_REGNUM);
980e61bb
DJ
21265
21266 if (dwarf2out_do_frame ())
21267 {
d342c045 21268 char *l = dwarf2out_cfi_label (false);
b279b20a 21269
980e61bb
DJ
21270 cfa_offset = cfa_offset + 16;
21271 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
21272 }
21273
57934c39
PB
21274 if (l_mask)
21275 {
21276 thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
1a59548b 21277 offset = bit_count (l_mask) * UNITS_PER_WORD;
57934c39
PB
21278 }
21279 else
21280 offset = 0;
f676971a 21281
d5b7b3ae 21282 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
38173d38 21283 offset + 16 + crtl->args.pretend_args_size);
f676971a 21284
d5b7b3ae
RE
21285 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
21286 offset + 4);
21287
21288 /* Make sure that the instruction fetching the PC is in the right place
21289 to calculate "start of backtrace creation code + 12". */
57934c39 21290 if (l_mask)
d5b7b3ae
RE
21291 {
21292 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
21293 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
21294 offset + 12);
21295 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
21296 ARM_HARD_FRAME_POINTER_REGNUM);
21297 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
21298 offset);
21299 }
21300 else
21301 {
21302 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
21303 ARM_HARD_FRAME_POINTER_REGNUM);
21304 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
21305 offset);
21306 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
21307 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
21308 offset + 12);
21309 }
f676971a 21310
d5b7b3ae
RE
21311 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
21312 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
21313 offset + 8);
21314 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
21315 offset + 12);
21316 asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
21317 ARM_HARD_FRAME_POINTER_REGNUM, work_register);
21318 }
0fa2e4df 21319 /* Optimization: If we are not pushing any low registers but we are going
b279b20a
NC
21320 to push some high registers then delay our first push. This will just
21321 be a push of LR and we can combine it with the push of the first high
21322 register. */
21323 else if ((l_mask & 0xff) != 0
21324 || (high_regs_pushed == 0 && l_mask))
cb751cbd
BS
21325 {
21326 unsigned long mask = l_mask;
e784c52c 21327 mask |= (1 << thumb1_extra_regs_pushed (offsets, true)) - 1;
cb751cbd
BS
21328 thumb_pushpop (f, mask, 1, &cfa_offset, mask);
21329 }
d5b7b3ae 21330
d5b7b3ae
RE
21331 if (high_regs_pushed)
21332 {
b279b20a
NC
21333 unsigned pushable_regs;
21334 unsigned next_hi_reg;
d5b7b3ae
RE
21335
21336 for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
57934c39 21337 if (live_regs_mask & (1 << next_hi_reg))
e26053d1 21338 break;
d5b7b3ae 21339
57934c39 21340 pushable_regs = l_mask & 0xff;
d5b7b3ae
RE
21341
21342 if (pushable_regs == 0)
57934c39 21343 pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
d5b7b3ae
RE
21344
21345 while (high_regs_pushed > 0)
21346 {
b279b20a 21347 unsigned long real_regs_mask = 0;
980e61bb 21348
b279b20a 21349 for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
d5b7b3ae 21350 {
57934c39 21351 if (pushable_regs & (1 << regno))
d5b7b3ae
RE
21352 {
21353 asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
f676971a 21354
b279b20a 21355 high_regs_pushed --;
980e61bb 21356 real_regs_mask |= (1 << next_hi_reg);
f676971a 21357
d5b7b3ae 21358 if (high_regs_pushed)
aeaf4d25 21359 {
b279b20a
NC
21360 for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
21361 next_hi_reg --)
57934c39 21362 if (live_regs_mask & (1 << next_hi_reg))
d5b7b3ae 21363 break;
aeaf4d25 21364 }
d5b7b3ae
RE
21365 else
21366 {
57934c39 21367 pushable_regs &= ~((1 << regno) - 1);
d5b7b3ae
RE
21368 break;
21369 }
21370 }
21371 }
980e61bb 21372
b279b20a
NC
21373 /* If we had to find a work register and we have not yet
21374 saved the LR then add it to the list of regs to push. */
21375 if (l_mask == (1 << LR_REGNUM))
21376 {
21377 thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
21378 1, &cfa_offset,
21379 real_regs_mask | (1 << LR_REGNUM));
21380 l_mask = 0;
21381 }
21382 else
21383 thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
d5b7b3ae 21384 }
d5b7b3ae
RE
21385 }
21386}
21387
21388/* Handle the case of a double word load into a low register from
21389 a computed memory address. The computed address may involve a
21390 register which is overwritten by the load. */
cd2b33d0 21391const char *
e32bac5b 21392thumb_load_double_from_address (rtx *operands)
d5b7b3ae
RE
21393{
21394 rtx addr;
21395 rtx base;
21396 rtx offset;
21397 rtx arg1;
21398 rtx arg2;
f676971a 21399
e6d29d15
NS
21400 gcc_assert (GET_CODE (operands[0]) == REG);
21401 gcc_assert (GET_CODE (operands[1]) == MEM);
d5b7b3ae
RE
21402
21403 /* Get the memory address. */
21404 addr = XEXP (operands[1], 0);
f676971a 21405
d5b7b3ae
RE
21406 /* Work out how the memory address is computed. */
21407 switch (GET_CODE (addr))
21408 {
21409 case REG:
31fa16b6 21410 operands[2] = adjust_address (operands[1], SImode, 4);
e0b92319 21411
d5b7b3ae
RE
21412 if (REGNO (operands[0]) == REGNO (addr))
21413 {
21414 output_asm_insn ("ldr\t%H0, %2", operands);
21415 output_asm_insn ("ldr\t%0, %1", operands);
21416 }
21417 else
21418 {
21419 output_asm_insn ("ldr\t%0, %1", operands);
21420 output_asm_insn ("ldr\t%H0, %2", operands);
21421 }
21422 break;
f676971a 21423
d5b7b3ae
RE
21424 case CONST:
21425 /* Compute <address> + 4 for the high order load. */
31fa16b6 21426 operands[2] = adjust_address (operands[1], SImode, 4);
e0b92319 21427
d5b7b3ae
RE
21428 output_asm_insn ("ldr\t%0, %1", operands);
21429 output_asm_insn ("ldr\t%H0, %2", operands);
21430 break;
f676971a 21431
d5b7b3ae
RE
21432 case PLUS:
21433 arg1 = XEXP (addr, 0);
21434 arg2 = XEXP (addr, 1);
f676971a 21435
d5b7b3ae
RE
21436 if (CONSTANT_P (arg1))
21437 base = arg2, offset = arg1;
21438 else
21439 base = arg1, offset = arg2;
f676971a 21440
e6d29d15 21441 gcc_assert (GET_CODE (base) == REG);
d5b7b3ae
RE
21442
21443 /* Catch the case of <address> = <reg> + <reg> */
21444 if (GET_CODE (offset) == REG)
21445 {
21446 int reg_offset = REGNO (offset);
21447 int reg_base = REGNO (base);
21448 int reg_dest = REGNO (operands[0]);
f676971a 21449
d5b7b3ae
RE
21450 /* Add the base and offset registers together into the
21451 higher destination register. */
21452 asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
21453 reg_dest + 1, reg_base, reg_offset);
f676971a 21454
d5b7b3ae
RE
21455 /* Load the lower destination register from the address in
21456 the higher destination register. */
21457 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
21458 reg_dest, reg_dest + 1);
f676971a 21459
d5b7b3ae
RE
21460 /* Load the higher destination register from its own address
21461 plus 4. */
21462 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
21463 reg_dest + 1, reg_dest + 1);
21464 }
21465 else
21466 {
21467 /* Compute <address> + 4 for the high order load. */
31fa16b6 21468 operands[2] = adjust_address (operands[1], SImode, 4);
f676971a 21469
d5b7b3ae
RE
21470 /* If the computed address is held in the low order register
21471 then load the high order register first, otherwise always
21472 load the low order register first. */
21473 if (REGNO (operands[0]) == REGNO (base))
21474 {
21475 output_asm_insn ("ldr\t%H0, %2", operands);
21476 output_asm_insn ("ldr\t%0, %1", operands);
21477 }
21478 else
21479 {
21480 output_asm_insn ("ldr\t%0, %1", operands);
21481 output_asm_insn ("ldr\t%H0, %2", operands);
21482 }
21483 }
21484 break;
21485
21486 case LABEL_REF:
21487 /* With no registers to worry about we can just load the value
21488 directly. */
31fa16b6 21489 operands[2] = adjust_address (operands[1], SImode, 4);
f676971a 21490
d5b7b3ae
RE
21491 output_asm_insn ("ldr\t%H0, %2", operands);
21492 output_asm_insn ("ldr\t%0, %1", operands);
21493 break;
f676971a 21494
d5b7b3ae 21495 default:
e6d29d15 21496 gcc_unreachable ();
d5b7b3ae 21497 }
f676971a 21498
d5b7b3ae
RE
21499 return "";
21500}
21501
cd2b33d0 21502const char *
e32bac5b 21503thumb_output_move_mem_multiple (int n, rtx *operands)
d5b7b3ae
RE
21504{
21505 rtx tmp;
21506
21507 switch (n)
21508 {
21509 case 2:
ca356f3a 21510 if (REGNO (operands[4]) > REGNO (operands[5]))
d5b7b3ae 21511 {
ca356f3a
RE
21512 tmp = operands[4];
21513 operands[4] = operands[5];
21514 operands[5] = tmp;
d5b7b3ae 21515 }
ca356f3a
RE
21516 output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
21517 output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
d5b7b3ae
RE
21518 break;
21519
21520 case 3:
ca356f3a 21521 if (REGNO (operands[4]) > REGNO (operands[5]))
d5b7b3ae 21522 {
ca356f3a
RE
21523 tmp = operands[4];
21524 operands[4] = operands[5];
21525 operands[5] = tmp;
d5b7b3ae 21526 }
ca356f3a 21527 if (REGNO (operands[5]) > REGNO (operands[6]))
d5b7b3ae 21528 {
ca356f3a
RE
21529 tmp = operands[5];
21530 operands[5] = operands[6];
21531 operands[6] = tmp;
d5b7b3ae 21532 }
ca356f3a 21533 if (REGNO (operands[4]) > REGNO (operands[5]))
d5b7b3ae 21534 {
ca356f3a
RE
21535 tmp = operands[4];
21536 operands[4] = operands[5];
21537 operands[5] = tmp;
d5b7b3ae 21538 }
f676971a 21539
ca356f3a
RE
21540 output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
21541 output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
d5b7b3ae
RE
21542 break;
21543
21544 default:
e6d29d15 21545 gcc_unreachable ();
d5b7b3ae
RE
21546 }
21547
21548 return "";
21549}
21550
b12a00f1
RE
21551/* Output a call-via instruction for thumb state. */
21552const char *
21553thumb_call_via_reg (rtx reg)
21554{
21555 int regno = REGNO (reg);
21556 rtx *labelp;
21557
57ecec57 21558 gcc_assert (regno < LR_REGNUM);
b12a00f1
RE
21559
21560 /* If we are in the normal text section we can use a single instance
21561 per compilation unit. If we are doing function sections, then we need
21562 an entry per section, since we can't rely on reachability. */
d6b5193b 21563 if (in_section == text_section)
b12a00f1
RE
21564 {
21565 thumb_call_reg_needed = 1;
21566
21567 if (thumb_call_via_label[regno] == NULL)
21568 thumb_call_via_label[regno] = gen_label_rtx ();
21569 labelp = thumb_call_via_label + regno;
21570 }
21571 else
21572 {
21573 if (cfun->machine->call_via[regno] == NULL)
21574 cfun->machine->call_via[regno] = gen_label_rtx ();
21575 labelp = cfun->machine->call_via + regno;
21576 }
21577
21578 output_asm_insn ("bl\t%a0", labelp);
21579 return "";
21580}
21581
1d6e90ac 21582/* Routines for generating rtl. */
d5b7b3ae 21583void
70128ad9 21584thumb_expand_movmemqi (rtx *operands)
d5b7b3ae
RE
21585{
21586 rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
21587 rtx in = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
21588 HOST_WIDE_INT len = INTVAL (operands[2]);
21589 HOST_WIDE_INT offset = 0;
21590
21591 while (len >= 12)
21592 {
ca356f3a 21593 emit_insn (gen_movmem12b (out, in, out, in));
d5b7b3ae
RE
21594 len -= 12;
21595 }
f676971a 21596
d5b7b3ae
RE
21597 if (len >= 8)
21598 {
ca356f3a 21599 emit_insn (gen_movmem8b (out, in, out, in));
d5b7b3ae
RE
21600 len -= 8;
21601 }
f676971a 21602
d5b7b3ae
RE
21603 if (len >= 4)
21604 {
21605 rtx reg = gen_reg_rtx (SImode);
f1c25d3b
KH
21606 emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
21607 emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
d5b7b3ae
RE
21608 len -= 4;
21609 offset += 4;
21610 }
f676971a 21611
d5b7b3ae
RE
21612 if (len >= 2)
21613 {
21614 rtx reg = gen_reg_rtx (HImode);
f676971a 21615 emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
f1c25d3b
KH
21616 plus_constant (in, offset))));
21617 emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
d5b7b3ae
RE
21618 reg));
21619 len -= 2;
21620 offset += 2;
21621 }
f676971a 21622
d5b7b3ae
RE
21623 if (len)
21624 {
21625 rtx reg = gen_reg_rtx (QImode);
f1c25d3b
KH
21626 emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
21627 plus_constant (in, offset))));
21628 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
d5b7b3ae
RE
21629 reg));
21630 }
21631}
21632
d5b7b3ae 21633void
e32bac5b 21634thumb_reload_out_hi (rtx *operands)
d5b7b3ae
RE
21635{
21636 emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
21637}
21638
f676971a 21639/* Handle reading a half-word from memory during reload. */
d5b7b3ae 21640void
e32bac5b 21641thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
d5b7b3ae 21642{
e6d29d15 21643 gcc_unreachable ();
d5b7b3ae
RE
21644}
21645
c27ba912
DM
21646/* Return the length of a function name prefix
21647 that starts with the character 'c'. */
21648static int
e32bac5b 21649arm_get_strip_length (int c)
c27ba912
DM
21650{
21651 switch (c)
21652 {
21653 ARM_NAME_ENCODING_LENGTHS
f676971a 21654 default: return 0;
c27ba912
DM
21655 }
21656}
21657
21658/* Return a pointer to a function's name with any
21659 and all prefix encodings stripped from it. */
21660const char *
e32bac5b 21661arm_strip_name_encoding (const char *name)
c27ba912
DM
21662{
21663 int skip;
f676971a 21664
c27ba912
DM
21665 while ((skip = arm_get_strip_length (* name)))
21666 name += skip;
21667
21668 return name;
21669}
21670
e1944073
KW
21671/* If there is a '*' anywhere in the name's prefix, then
21672 emit the stripped name verbatim, otherwise prepend an
21673 underscore if leading underscores are being used. */
e1944073 21674void
e32bac5b 21675arm_asm_output_labelref (FILE *stream, const char *name)
e1944073
KW
21676{
21677 int skip;
21678 int verbatim = 0;
21679
21680 while ((skip = arm_get_strip_length (* name)))
21681 {
21682 verbatim |= (*name == '*');
21683 name += skip;
21684 }
21685
21686 if (verbatim)
21687 fputs (name, stream);
21688 else
21689 asm_fprintf (stream, "%U%s", name);
21690}
21691
6c6aa1af
PB
21692static void
21693arm_file_start (void)
21694{
21695 int val;
21696
5b3e6663
PB
21697 if (TARGET_UNIFIED_ASM)
21698 asm_fprintf (asm_out_file, "\t.syntax unified\n");
21699
6c6aa1af
PB
21700 if (TARGET_BPABI)
21701 {
21702 const char *fpu_name;
12a0a4d4
PB
21703 if (arm_selected_arch)
21704 asm_fprintf (asm_out_file, "\t.arch %s\n", arm_selected_arch->name);
6c6aa1af 21705 else
12a0a4d4 21706 asm_fprintf (asm_out_file, "\t.cpu %s\n", arm_selected_cpu->name);
6c6aa1af
PB
21707
21708 if (TARGET_SOFT_FLOAT)
21709 {
21710 if (TARGET_VFP)
21711 fpu_name = "softvfp";
21712 else
21713 fpu_name = "softfpa";
21714 }
21715 else
21716 {
d79f3032
PB
21717 fpu_name = arm_fpu_desc->name;
21718 if (arm_fpu_desc->model == ARM_FP_MODEL_VFP)
f1adb0a9
JB
21719 {
21720 if (TARGET_HARD_FLOAT)
21721 asm_fprintf (asm_out_file, "\t.eabi_attribute 27, 3\n");
21722 if (TARGET_HARD_FLOAT_ABI)
21723 asm_fprintf (asm_out_file, "\t.eabi_attribute 28, 1\n");
21724 }
6c6aa1af
PB
21725 }
21726 asm_fprintf (asm_out_file, "\t.fpu %s\n", fpu_name);
21727
21728 /* Some of these attributes only apply when the corresponding features
21729 are used. However we don't have any easy way of figuring this out.
21730 Conservatively record the setting that would have been used. */
21731
6c6aa1af
PB
21732 /* Tag_ABI_FP_rounding. */
21733 if (flag_rounding_math)
21734 asm_fprintf (asm_out_file, "\t.eabi_attribute 19, 1\n");
21735 if (!flag_unsafe_math_optimizations)
21736 {
21737 /* Tag_ABI_FP_denomal. */
21738 asm_fprintf (asm_out_file, "\t.eabi_attribute 20, 1\n");
21739 /* Tag_ABI_FP_exceptions. */
21740 asm_fprintf (asm_out_file, "\t.eabi_attribute 21, 1\n");
21741 }
21742 /* Tag_ABI_FP_user_exceptions. */
21743 if (flag_signaling_nans)
21744 asm_fprintf (asm_out_file, "\t.eabi_attribute 22, 1\n");
21745 /* Tag_ABI_FP_number_model. */
21746 asm_fprintf (asm_out_file, "\t.eabi_attribute 23, %d\n",
21747 flag_finite_math_only ? 1 : 3);
21748
21749 /* Tag_ABI_align8_needed. */
21750 asm_fprintf (asm_out_file, "\t.eabi_attribute 24, 1\n");
21751 /* Tag_ABI_align8_preserved. */
21752 asm_fprintf (asm_out_file, "\t.eabi_attribute 25, 1\n");
21753 /* Tag_ABI_enum_size. */
21754 asm_fprintf (asm_out_file, "\t.eabi_attribute 26, %d\n",
21755 flag_short_enums ? 1 : 2);
21756
21757 /* Tag_ABI_optimization_goals. */
21758 if (optimize_size)
21759 val = 4;
21760 else if (optimize >= 2)
21761 val = 2;
21762 else if (optimize)
21763 val = 1;
21764 else
21765 val = 6;
21766 asm_fprintf (asm_out_file, "\t.eabi_attribute 30, %d\n", val);
b76c3c4b 21767
0fd8c3ad
SL
21768 /* Tag_ABI_FP_16bit_format. */
21769 if (arm_fp16_format)
21770 asm_fprintf (asm_out_file, "\t.eabi_attribute 38, %d\n",
21771 (int)arm_fp16_format);
21772
b76c3c4b
PB
21773 if (arm_lang_output_object_attributes_hook)
21774 arm_lang_output_object_attributes_hook();
6c6aa1af
PB
21775 }
21776 default_file_start();
21777}
21778
b12a00f1
RE
21779static void
21780arm_file_end (void)
21781{
21782 int regno;
21783
978e411f
CD
21784 if (NEED_INDICATE_EXEC_STACK)
21785 /* Add .note.GNU-stack. */
21786 file_end_indicate_exec_stack ();
21787
b12a00f1
RE
21788 if (! thumb_call_reg_needed)
21789 return;
21790
d6b5193b 21791 switch_to_section (text_section);
b12a00f1
RE
21792 asm_fprintf (asm_out_file, "\t.code 16\n");
21793 ASM_OUTPUT_ALIGN (asm_out_file, 1);
21794
57ecec57 21795 for (regno = 0; regno < LR_REGNUM; regno++)
b12a00f1
RE
21796 {
21797 rtx label = thumb_call_via_label[regno];
21798
21799 if (label != 0)
21800 {
21801 targetm.asm_out.internal_label (asm_out_file, "L",
21802 CODE_LABEL_NUMBER (label));
21803 asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
21804 }
21805 }
21806}
21807
fb49053f
RH
21808#ifndef ARM_PE
21809/* Symbols in the text segment can be accessed without indirecting via the
21810 constant pool; it may take an extra binary operation, but this is still
21811 faster than indirecting via memory. Don't do this when not optimizing,
21812 since we won't be calculating al of the offsets necessary to do this
21813 simplification. */
21814
21815static void
e32bac5b 21816arm_encode_section_info (tree decl, rtx rtl, int first)
fb49053f 21817{
3521b33c 21818 if (optimize > 0 && TREE_CONSTANT (decl))
c6a2438a 21819 SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
fb49053f 21820
d3585b76 21821 default_encode_section_info (decl, rtl, first);
fb49053f
RH
21822}
21823#endif /* !ARM_PE */
483ab821 21824
4977bab6 21825static void
e32bac5b 21826arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
4977bab6
ZW
21827{
21828 if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
21829 && !strcmp (prefix, "L"))
21830 {
21831 arm_ccfsm_state = 0;
21832 arm_target_insn = NULL;
21833 }
21834 default_internal_label (stream, prefix, labelno);
21835}
21836
c590b625
RH
21837/* Output code to add DELTA to the first argument, and then jump
21838 to FUNCTION. Used for C++ multiple inheritance. */
c590b625 21839static void
e32bac5b
RE
21840arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
21841 HOST_WIDE_INT delta,
21842 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
21843 tree function)
483ab821 21844{
9b66ebb1
PB
21845 static int thunk_label = 0;
21846 char label[256];
54b9e939 21847 char labelpc[256];
483ab821
MM
21848 int mi_delta = delta;
21849 const char *const mi_op = mi_delta < 0 ? "sub" : "add";
21850 int shift = 0;
61f71b34 21851 int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
483ab821
MM
21852 ? 1 : 0);
21853 if (mi_delta < 0)
21854 mi_delta = - mi_delta;
bf98ec6c 21855
5b3e6663 21856 if (TARGET_THUMB1)
9b66ebb1
PB
21857 {
21858 int labelno = thunk_label++;
21859 ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
bf98ec6c
PB
21860 /* Thunks are entered in arm mode when avaiable. */
21861 if (TARGET_THUMB1_ONLY)
21862 {
21863 /* push r3 so we can use it as a temporary. */
21864 /* TODO: Omit this save if r3 is not used. */
21865 fputs ("\tpush {r3}\n", file);
21866 fputs ("\tldr\tr3, ", file);
21867 }
21868 else
21869 {
21870 fputs ("\tldr\tr12, ", file);
21871 }
9b66ebb1
PB
21872 assemble_name (file, label);
21873 fputc ('\n', file);
54b9e939
KH
21874 if (flag_pic)
21875 {
21876 /* If we are generating PIC, the ldr instruction below loads
21877 "(target - 7) - .LTHUNKPCn" into r12. The pc reads as
21878 the address of the add + 8, so we have:
21879
21880 r12 = (target - 7) - .LTHUNKPCn + (.LTHUNKPCn + 8)
21881 = target + 1.
21882
21883 Note that we have "+ 1" because some versions of GNU ld
21884 don't set the low bit of the result for R_ARM_REL32
bf98ec6c
PB
21885 relocations against thumb function symbols.
21886 On ARMv6M this is +4, not +8. */
54b9e939
KH
21887 ASM_GENERATE_INTERNAL_LABEL (labelpc, "LTHUNKPC", labelno);
21888 assemble_name (file, labelpc);
21889 fputs (":\n", file);
bf98ec6c
PB
21890 if (TARGET_THUMB1_ONLY)
21891 {
21892 /* This is 2 insns after the start of the thunk, so we know it
21893 is 4-byte aligned. */
21894 fputs ("\tadd\tr3, pc, r3\n", file);
21895 fputs ("\tmov r12, r3\n", file);
21896 }
21897 else
21898 fputs ("\tadd\tr12, pc, r12\n", file);
54b9e939 21899 }
bf98ec6c
PB
21900 else if (TARGET_THUMB1_ONLY)
21901 fputs ("\tmov r12, r3\n", file);
9b66ebb1 21902 }
bf98ec6c 21903 if (TARGET_THUMB1_ONLY)
483ab821 21904 {
bf98ec6c
PB
21905 if (mi_delta > 255)
21906 {
21907 fputs ("\tldr\tr3, ", file);
21908 assemble_name (file, label);
21909 fputs ("+4\n", file);
21910 asm_fprintf (file, "\t%s\t%r, %r, r3\n",
21911 mi_op, this_regno, this_regno);
21912 }
21913 else if (mi_delta != 0)
21914 {
21915 asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
21916 mi_op, this_regno, this_regno,
21917 mi_delta);
21918 }
21919 }
21920 else
21921 {
21922 /* TODO: Use movw/movt for large constants when available. */
21923 while (mi_delta != 0)
21924 {
21925 if ((mi_delta & (3 << shift)) == 0)
21926 shift += 2;
21927 else
21928 {
21929 asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
21930 mi_op, this_regno, this_regno,
21931 mi_delta & (0xff << shift));
21932 mi_delta &= ~(0xff << shift);
21933 shift += 8;
21934 }
21935 }
483ab821 21936 }
5b3e6663 21937 if (TARGET_THUMB1)
9b66ebb1 21938 {
bf98ec6c
PB
21939 if (TARGET_THUMB1_ONLY)
21940 fputs ("\tpop\t{r3}\n", file);
21941
9b66ebb1
PB
21942 fprintf (file, "\tbx\tr12\n");
21943 ASM_OUTPUT_ALIGN (file, 2);
21944 assemble_name (file, label);
21945 fputs (":\n", file);
54b9e939
KH
21946 if (flag_pic)
21947 {
21948 /* Output ".word .LTHUNKn-7-.LTHUNKPCn". */
21949 rtx tem = XEXP (DECL_RTL (function), 0);
21950 tem = gen_rtx_PLUS (GET_MODE (tem), tem, GEN_INT (-7));
21951 tem = gen_rtx_MINUS (GET_MODE (tem),
21952 tem,
21953 gen_rtx_SYMBOL_REF (Pmode,
21954 ggc_strdup (labelpc)));
21955 assemble_integer (tem, 4, BITS_PER_WORD, 1);
21956 }
21957 else
21958 /* Output ".word .LTHUNKn". */
21959 assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
bf98ec6c
PB
21960
21961 if (TARGET_THUMB1_ONLY && mi_delta > 255)
21962 assemble_integer (GEN_INT(mi_delta), 4, BITS_PER_WORD, 1);
9b66ebb1
PB
21963 }
21964 else
21965 {
21966 fputs ("\tb\t", file);
21967 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
21968 if (NEED_PLT_RELOC)
21969 fputs ("(PLT)", file);
21970 fputc ('\n', file);
21971 }
483ab821 21972}
5a9335ef
NC
21973
21974int
6f5f2481 21975arm_emit_vector_const (FILE *file, rtx x)
5a9335ef
NC
21976{
21977 int i;
21978 const char * pattern;
21979
e6d29d15 21980 gcc_assert (GET_CODE (x) == CONST_VECTOR);
5a9335ef
NC
21981
21982 switch (GET_MODE (x))
21983 {
21984 case V2SImode: pattern = "%08x"; break;
21985 case V4HImode: pattern = "%04x"; break;
21986 case V8QImode: pattern = "%02x"; break;
e6d29d15 21987 default: gcc_unreachable ();
5a9335ef
NC
21988 }
21989
21990 fprintf (file, "0x");
21991 for (i = CONST_VECTOR_NUNITS (x); i--;)
21992 {
21993 rtx element;
21994
21995 element = CONST_VECTOR_ELT (x, i);
21996 fprintf (file, pattern, INTVAL (element));
21997 }
21998
21999 return 1;
22000}
22001
0fd8c3ad
SL
22002/* Emit a fp16 constant appropriately padded to occupy a 4-byte word.
22003 HFmode constant pool entries are actually loaded with ldr. */
22004void
22005arm_emit_fp16_const (rtx c)
22006{
22007 REAL_VALUE_TYPE r;
22008 long bits;
22009
22010 REAL_VALUE_FROM_CONST_DOUBLE (r, c);
22011 bits = real_to_target (NULL, &r, HFmode);
22012 if (WORDS_BIG_ENDIAN)
22013 assemble_zeros (2);
22014 assemble_integer (GEN_INT (bits), 2, BITS_PER_WORD, 1);
22015 if (!WORDS_BIG_ENDIAN)
22016 assemble_zeros (2);
22017}
22018
5a9335ef 22019const char *
6f5f2481 22020arm_output_load_gr (rtx *operands)
5a9335ef
NC
22021{
22022 rtx reg;
22023 rtx offset;
22024 rtx wcgr;
22025 rtx sum;
f676971a 22026
5a9335ef
NC
22027 if (GET_CODE (operands [1]) != MEM
22028 || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
22029 || GET_CODE (reg = XEXP (sum, 0)) != REG
22030 || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
22031 || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
22032 return "wldrw%?\t%0, %1";
f676971a
EC
22033
22034 /* Fix up an out-of-range load of a GR register. */
5a9335ef
NC
22035 output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
22036 wcgr = operands[0];
22037 operands[0] = reg;
22038 output_asm_insn ("ldr%?\t%0, %1", operands);
22039
22040 operands[0] = wcgr;
22041 operands[1] = reg;
22042 output_asm_insn ("tmcr%?\t%0, %1", operands);
22043 output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
22044
22045 return "";
22046}
f9ba5949 22047
1cc9f5f5
KH
22048/* Worker function for TARGET_SETUP_INCOMING_VARARGS.
22049
22050 On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
22051 named arg and all anonymous args onto the stack.
22052 XXX I know the prologue shouldn't be pushing registers, but it is faster
22053 that way. */
22054
22055static void
390b17c2 22056arm_setup_incoming_varargs (CUMULATIVE_ARGS *pcum,
22ccaaee
JJ
22057 enum machine_mode mode,
22058 tree type,
1cc9f5f5
KH
22059 int *pretend_size,
22060 int second_time ATTRIBUTE_UNUSED)
22061{
390b17c2
RE
22062 int nregs;
22063
1cc9f5f5 22064 cfun->machine->uses_anonymous_args = 1;
390b17c2
RE
22065 if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
22066 {
22067 nregs = pcum->aapcs_ncrn;
22068 if ((nregs & 1) && arm_needs_doubleword_align (mode, type))
22069 nregs++;
22070 }
22071 else
22072 nregs = pcum->nregs;
22073
22ccaaee
JJ
22074 if (nregs < NUM_ARG_REGS)
22075 *pretend_size = (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
1cc9f5f5 22076}
9b66ebb1 22077
59b9a953 22078/* Return nonzero if the CONSUMER instruction (a store) does not need
9b66ebb1
PB
22079 PRODUCER's value to calculate the address. */
22080
22081int
22082arm_no_early_store_addr_dep (rtx producer, rtx consumer)
22083{
22084 rtx value = PATTERN (producer);
22085 rtx addr = PATTERN (consumer);
22086
22087 if (GET_CODE (value) == COND_EXEC)
22088 value = COND_EXEC_CODE (value);
22089 if (GET_CODE (value) == PARALLEL)
22090 value = XVECEXP (value, 0, 0);
22091 value = XEXP (value, 0);
22092 if (GET_CODE (addr) == COND_EXEC)
22093 addr = COND_EXEC_CODE (addr);
22094 if (GET_CODE (addr) == PARALLEL)
22095 addr = XVECEXP (addr, 0, 0);
22096 addr = XEXP (addr, 0);
f676971a 22097
9b66ebb1
PB
22098 return !reg_overlap_mentioned_p (value, addr);
22099}
22100
47d8f18d
JZ
22101/* Return nonzero if the CONSUMER instruction (a store) does need
22102 PRODUCER's value to calculate the address. */
22103
22104int
22105arm_early_store_addr_dep (rtx producer, rtx consumer)
22106{
22107 return !arm_no_early_store_addr_dep (producer, consumer);
22108}
22109
22110/* Return nonzero if the CONSUMER instruction (a load) does need
22111 PRODUCER's value to calculate the address. */
22112
22113int
22114arm_early_load_addr_dep (rtx producer, rtx consumer)
22115{
22116 rtx value = PATTERN (producer);
22117 rtx addr = PATTERN (consumer);
22118
22119 if (GET_CODE (value) == COND_EXEC)
22120 value = COND_EXEC_CODE (value);
22121 if (GET_CODE (value) == PARALLEL)
22122 value = XVECEXP (value, 0, 0);
22123 value = XEXP (value, 0);
22124 if (GET_CODE (addr) == COND_EXEC)
22125 addr = COND_EXEC_CODE (addr);
22126 if (GET_CODE (addr) == PARALLEL)
22127 addr = XVECEXP (addr, 0, 0);
22128 addr = XEXP (addr, 1);
22129
22130 return reg_overlap_mentioned_p (value, addr);
22131}
22132
59b9a953 22133/* Return nonzero if the CONSUMER instruction (an ALU op) does not
9b66ebb1
PB
22134 have an early register shift value or amount dependency on the
22135 result of PRODUCER. */
22136
22137int
22138arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
22139{
22140 rtx value = PATTERN (producer);
22141 rtx op = PATTERN (consumer);
22142 rtx early_op;
22143
22144 if (GET_CODE (value) == COND_EXEC)
22145 value = COND_EXEC_CODE (value);
22146 if (GET_CODE (value) == PARALLEL)
22147 value = XVECEXP (value, 0, 0);
22148 value = XEXP (value, 0);
22149 if (GET_CODE (op) == COND_EXEC)
22150 op = COND_EXEC_CODE (op);
22151 if (GET_CODE (op) == PARALLEL)
22152 op = XVECEXP (op, 0, 0);
22153 op = XEXP (op, 1);
f676971a 22154
9b66ebb1
PB
22155 early_op = XEXP (op, 0);
22156 /* This is either an actual independent shift, or a shift applied to
22157 the first operand of another operation. We want the whole shift
22158 operation. */
22159 if (GET_CODE (early_op) == REG)
22160 early_op = op;
22161
22162 return !reg_overlap_mentioned_p (value, early_op);
22163}
22164
59b9a953 22165/* Return nonzero if the CONSUMER instruction (an ALU op) does not
9b66ebb1
PB
22166 have an early register shift value dependency on the result of
22167 PRODUCER. */
22168
22169int
22170arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
22171{
22172 rtx value = PATTERN (producer);
22173 rtx op = PATTERN (consumer);
22174 rtx early_op;
22175
22176 if (GET_CODE (value) == COND_EXEC)
22177 value = COND_EXEC_CODE (value);
22178 if (GET_CODE (value) == PARALLEL)
22179 value = XVECEXP (value, 0, 0);
22180 value = XEXP (value, 0);
22181 if (GET_CODE (op) == COND_EXEC)
22182 op = COND_EXEC_CODE (op);
22183 if (GET_CODE (op) == PARALLEL)
22184 op = XVECEXP (op, 0, 0);
22185 op = XEXP (op, 1);
f676971a 22186
9b66ebb1
PB
22187 early_op = XEXP (op, 0);
22188
22189 /* This is either an actual independent shift, or a shift applied to
22190 the first operand of another operation. We want the value being
22191 shifted, in either case. */
22192 if (GET_CODE (early_op) != REG)
22193 early_op = XEXP (early_op, 0);
f676971a 22194
9b66ebb1
PB
22195 return !reg_overlap_mentioned_p (value, early_op);
22196}
22197
59b9a953 22198/* Return nonzero if the CONSUMER (a mul or mac op) does not
9b66ebb1
PB
22199 have an early register mult dependency on the result of
22200 PRODUCER. */
22201
22202int
22203arm_no_early_mul_dep (rtx producer, rtx consumer)
22204{
22205 rtx value = PATTERN (producer);
22206 rtx op = PATTERN (consumer);
22207
22208 if (GET_CODE (value) == COND_EXEC)
22209 value = COND_EXEC_CODE (value);
22210 if (GET_CODE (value) == PARALLEL)
22211 value = XVECEXP (value, 0, 0);
22212 value = XEXP (value, 0);
22213 if (GET_CODE (op) == COND_EXEC)
22214 op = COND_EXEC_CODE (op);
22215 if (GET_CODE (op) == PARALLEL)
22216 op = XVECEXP (op, 0, 0);
22217 op = XEXP (op, 1);
f676971a 22218
756f763b
PB
22219 if (GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
22220 {
22221 if (GET_CODE (XEXP (op, 0)) == MULT)
22222 return !reg_overlap_mentioned_p (value, XEXP (op, 0));
22223 else
22224 return !reg_overlap_mentioned_p (value, XEXP (op, 1));
22225 }
22226
22227 return 0;
9b66ebb1
PB
22228}
22229
70301b45
PB
22230/* We can't rely on the caller doing the proper promotion when
22231 using APCS or ATPCS. */
22232
22233static bool
586de218 22234arm_promote_prototypes (const_tree t ATTRIBUTE_UNUSED)
70301b45 22235{
b6685939 22236 return !TARGET_AAPCS_BASED;
70301b45
PB
22237}
22238
cde0f3fd
PB
22239static enum machine_mode
22240arm_promote_function_mode (const_tree type ATTRIBUTE_UNUSED,
22241 enum machine_mode mode,
22242 int *punsignedp ATTRIBUTE_UNUSED,
22243 const_tree fntype ATTRIBUTE_UNUSED,
22244 int for_return ATTRIBUTE_UNUSED)
22245{
22246 if (GET_MODE_CLASS (mode) == MODE_INT
22247 && GET_MODE_SIZE (mode) < 4)
22248 return SImode;
22249
22250 return mode;
22251}
6b045785
PB
22252
22253/* AAPCS based ABIs use short enums by default. */
22254
22255static bool
22256arm_default_short_enums (void)
22257{
077fc835 22258 return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
6b045785 22259}
13c1cd82
PB
22260
22261
22262/* AAPCS requires that anonymous bitfields affect structure alignment. */
22263
22264static bool
22265arm_align_anon_bitfield (void)
22266{
22267 return TARGET_AAPCS_BASED;
22268}
4185ae53
PB
22269
22270
22271/* The generic C++ ABI says 64-bit (long long). The EABI says 32-bit. */
22272
22273static tree
22274arm_cxx_guard_type (void)
22275{
22276 return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
22277}
22278
c956e102
MS
22279/* Return non-zero if the consumer (a multiply-accumulate instruction)
22280 has an accumulator dependency on the result of the producer (a
22281 multiplication instruction) and no other dependency on that result. */
22282int
22283arm_mac_accumulator_is_mul_result (rtx producer, rtx consumer)
22284{
22285 rtx mul = PATTERN (producer);
22286 rtx mac = PATTERN (consumer);
22287 rtx mul_result;
22288 rtx mac_op0, mac_op1, mac_acc;
22289
22290 if (GET_CODE (mul) == COND_EXEC)
22291 mul = COND_EXEC_CODE (mul);
22292 if (GET_CODE (mac) == COND_EXEC)
22293 mac = COND_EXEC_CODE (mac);
22294
22295 /* Check that mul is of the form (set (...) (mult ...))
22296 and mla is of the form (set (...) (plus (mult ...) (...))). */
22297 if ((GET_CODE (mul) != SET || GET_CODE (XEXP (mul, 1)) != MULT)
22298 || (GET_CODE (mac) != SET || GET_CODE (XEXP (mac, 1)) != PLUS
22299 || GET_CODE (XEXP (XEXP (mac, 1), 0)) != MULT))
22300 return 0;
22301
22302 mul_result = XEXP (mul, 0);
22303 mac_op0 = XEXP (XEXP (XEXP (mac, 1), 0), 0);
22304 mac_op1 = XEXP (XEXP (XEXP (mac, 1), 0), 1);
22305 mac_acc = XEXP (XEXP (mac, 1), 1);
22306
22307 return (reg_overlap_mentioned_p (mul_result, mac_acc)
22308 && !reg_overlap_mentioned_p (mul_result, mac_op0)
22309 && !reg_overlap_mentioned_p (mul_result, mac_op1));
22310}
22311
4185ae53 22312
0fa2e4df 22313/* The EABI says test the least significant bit of a guard variable. */
4185ae53
PB
22314
22315static bool
22316arm_cxx_guard_mask_bit (void)
22317{
22318 return TARGET_AAPCS_BASED;
22319}
46e995e0
PB
22320
22321
22322/* The EABI specifies that all array cookies are 8 bytes long. */
22323
22324static tree
22325arm_get_cookie_size (tree type)
22326{
22327 tree size;
22328
22329 if (!TARGET_AAPCS_BASED)
22330 return default_cxx_get_cookie_size (type);
22331
7d60be94 22332 size = build_int_cst (sizetype, 8);
46e995e0
PB
22333 return size;
22334}
22335
22336
22337/* The EABI says that array cookies should also contain the element size. */
22338
22339static bool
22340arm_cookie_has_size (void)
22341{
22342 return TARGET_AAPCS_BASED;
22343}
44d10c10
PB
22344
22345
22346/* The EABI says constructors and destructors should return a pointer to
22347 the object constructed/destroyed. */
22348
22349static bool
22350arm_cxx_cdtor_returns_this (void)
22351{
22352 return TARGET_AAPCS_BASED;
22353}
c9ca9b88 22354
505970fc
MM
22355/* The EABI says that an inline function may never be the key
22356 method. */
22357
22358static bool
22359arm_cxx_key_method_may_be_inline (void)
22360{
22361 return !TARGET_AAPCS_BASED;
22362}
22363
1e731102
MM
22364static void
22365arm_cxx_determine_class_data_visibility (tree decl)
22366{
711b2998
JB
22367 if (!TARGET_AAPCS_BASED
22368 || !TARGET_DLLIMPORT_DECL_ATTRIBUTES)
1e731102 22369 return;
505970fc 22370
1e731102
MM
22371 /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
22372 is exported. However, on systems without dynamic vague linkage,
22373 \S 3.2.5.6 says that COMDAT class data has hidden linkage. */
22374 if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
22375 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
22376 else
22377 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
22378 DECL_VISIBILITY_SPECIFIED (decl) = 1;
22379}
e0b92319 22380
505970fc 22381static bool
1e731102 22382arm_cxx_class_data_always_comdat (void)
505970fc 22383{
1e731102
MM
22384 /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
22385 vague linkage if the class has no key function. */
22386 return !TARGET_AAPCS_BASED;
505970fc 22387}
c9ca9b88 22388
9f62c3e3
PB
22389
22390/* The EABI says __aeabi_atexit should be used to register static
22391 destructors. */
22392
22393static bool
22394arm_cxx_use_aeabi_atexit (void)
22395{
22396 return TARGET_AAPCS_BASED;
22397}
22398
22399
c9ca9b88
PB
22400void
22401arm_set_return_address (rtx source, rtx scratch)
22402{
22403 arm_stack_offsets *offsets;
22404 HOST_WIDE_INT delta;
22405 rtx addr;
22406 unsigned long saved_regs;
22407
954954d1
PB
22408 offsets = arm_get_frame_offsets ();
22409 saved_regs = offsets->saved_regs_mask;
c9ca9b88
PB
22410
22411 if ((saved_regs & (1 << LR_REGNUM)) == 0)
22412 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
22413 else
22414 {
22415 if (frame_pointer_needed)
22416 addr = plus_constant(hard_frame_pointer_rtx, -4);
22417 else
22418 {
22419 /* LR will be the first saved register. */
c9ca9b88
PB
22420 delta = offsets->outgoing_args - (offsets->frame + 4);
22421
f676971a 22422
c9ca9b88
PB
22423 if (delta >= 4096)
22424 {
22425 emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
22426 GEN_INT (delta & ~4095)));
22427 addr = scratch;
22428 delta &= 4095;
22429 }
22430 else
22431 addr = stack_pointer_rtx;
22432
22433 addr = plus_constant (addr, delta);
22434 }
31fa16b6 22435 emit_move_insn (gen_frame_mem (Pmode, addr), source);
c9ca9b88
PB
22436 }
22437}
22438
22439
22440void
22441thumb_set_return_address (rtx source, rtx scratch)
22442{
22443 arm_stack_offsets *offsets;
c9ca9b88 22444 HOST_WIDE_INT delta;
5b3e6663 22445 HOST_WIDE_INT limit;
c9ca9b88
PB
22446 int reg;
22447 rtx addr;
57934c39 22448 unsigned long mask;
c9ca9b88 22449
c41c1387 22450 emit_use (source);
c9ca9b88 22451
954954d1
PB
22452 offsets = arm_get_frame_offsets ();
22453 mask = offsets->saved_regs_mask;
57934c39 22454 if (mask & (1 << LR_REGNUM))
c9ca9b88 22455 {
5b3e6663 22456 limit = 1024;
c9ca9b88
PB
22457 /* Find the saved regs. */
22458 if (frame_pointer_needed)
22459 {
22460 delta = offsets->soft_frame - offsets->saved_args;
22461 reg = THUMB_HARD_FRAME_POINTER_REGNUM;
5b3e6663
PB
22462 if (TARGET_THUMB1)
22463 limit = 128;
c9ca9b88
PB
22464 }
22465 else
22466 {
22467 delta = offsets->outgoing_args - offsets->saved_args;
22468 reg = SP_REGNUM;
22469 }
22470 /* Allow for the stack frame. */
5b3e6663 22471 if (TARGET_THUMB1 && TARGET_BACKTRACE)
c9ca9b88
PB
22472 delta -= 16;
22473 /* The link register is always the first saved register. */
22474 delta -= 4;
f676971a 22475
c9ca9b88
PB
22476 /* Construct the address. */
22477 addr = gen_rtx_REG (SImode, reg);
5b3e6663 22478 if (delta > limit)
c9ca9b88
PB
22479 {
22480 emit_insn (gen_movsi (scratch, GEN_INT (delta)));
22481 emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
22482 addr = scratch;
22483 }
22484 else
22485 addr = plus_constant (addr, delta);
22486
31fa16b6 22487 emit_move_insn (gen_frame_mem (Pmode, addr), source);
c9ca9b88
PB
22488 }
22489 else
22490 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
22491}
22492
f676971a
EC
22493/* Implements target hook vector_mode_supported_p. */
22494bool
22495arm_vector_mode_supported_p (enum machine_mode mode)
22496{
88f77cba
JB
22497 /* Neon also supports V2SImode, etc. listed in the clause below. */
22498 if (TARGET_NEON && (mode == V2SFmode || mode == V4SImode || mode == V8HImode
22499 || mode == V16QImode || mode == V4SFmode || mode == V2DImode))
22500 return true;
22501
390b17c2
RE
22502 if ((TARGET_NEON || TARGET_IWMMXT)
22503 && ((mode == V2SImode)
22504 || (mode == V4HImode)
22505 || (mode == V8QImode)))
f676971a
EC
22506 return true;
22507
22508 return false;
22509}
273a2526 22510
26983c22
L
22511/* Use the option -mvectorize-with-neon-quad to override the use of doubleword
22512 registers when autovectorizing for Neon, at least until multiple vector
22513 widths are supported properly by the middle-end. */
22514
cc4b5170
RG
22515static enum machine_mode
22516arm_preferred_simd_mode (enum machine_mode mode)
26983c22 22517{
cc4b5170
RG
22518 if (TARGET_NEON)
22519 switch (mode)
22520 {
22521 case SFmode:
22522 return TARGET_NEON_VECTORIZE_QUAD ? V4SFmode : V2SFmode;
22523 case SImode:
22524 return TARGET_NEON_VECTORIZE_QUAD ? V4SImode : V2SImode;
22525 case HImode:
22526 return TARGET_NEON_VECTORIZE_QUAD ? V8HImode : V4HImode;
22527 case QImode:
22528 return TARGET_NEON_VECTORIZE_QUAD ? V16QImode : V8QImode;
22529 case DImode:
22530 if (TARGET_NEON_VECTORIZE_QUAD)
22531 return V2DImode;
22532 break;
22533
22534 default:;
22535 }
22536
22537 if (TARGET_REALLY_IWMMXT)
22538 switch (mode)
22539 {
22540 case SImode:
22541 return V2SImode;
22542 case HImode:
22543 return V4HImode;
22544 case QImode:
22545 return V8QImode;
22546
22547 default:;
22548 }
22549
22550 return word_mode;
26983c22
L
22551}
22552
d163e655
AS
22553/* Implement TARGET_CLASS_LIKELY_SPILLED_P.
22554
9adc580c
AS
22555 We need to define this for LO_REGS on Thumb-1. Otherwise we can end up
22556 using r0-r4 for function arguments, r7 for the stack frame and don't have
22557 enough left over to do doubleword arithmetic. For Thumb-2 all the
22558 potentially problematic instructions accept high registers so this is not
22559 necessary. Care needs to be taken to avoid adding new Thumb-2 patterns
22560 that require many low registers. */
d163e655
AS
22561static bool
22562arm_class_likely_spilled_p (reg_class_t rclass)
22563{
9adc580c 22564 if ((TARGET_THUMB1 && rclass == LO_REGS)
d163e655
AS
22565 || rclass == CC_REG)
22566 return true;
22567
22568 return false;
22569}
22570
42db504c
SB
22571/* Implements target hook small_register_classes_for_mode_p. */
22572bool
22573arm_small_register_classes_for_mode_p (enum machine_mode mode ATTRIBUTE_UNUSED)
22574{
22575 return TARGET_THUMB1;
22576}
22577
273a2526
RS
22578/* Implement TARGET_SHIFT_TRUNCATION_MASK. SImode shifts use normal
22579 ARM insns and therefore guarantee that the shift count is modulo 256.
22580 DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
22581 guarantee no particular behavior for out-of-range counts. */
22582
22583static unsigned HOST_WIDE_INT
22584arm_shift_truncation_mask (enum machine_mode mode)
22585{
22586 return mode == SImode ? 255 : 0;
22587}
2fa330b2
PB
22588
22589
22590/* Map internal gcc register numbers to DWARF2 register numbers. */
22591
22592unsigned int
22593arm_dbx_register_number (unsigned int regno)
22594{
22595 if (regno < 16)
22596 return regno;
22597
22598 /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
22599 compatibility. The EABI defines them as registers 96-103. */
22600 if (IS_FPA_REGNUM (regno))
22601 return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
22602
22603 if (IS_VFP_REGNUM (regno))
854b8a40
JB
22604 {
22605 /* See comment in arm_dwarf_register_span. */
22606 if (VFP_REGNO_OK_FOR_SINGLE (regno))
22607 return 64 + regno - FIRST_VFP_REGNUM;
22608 else
22609 return 256 + (regno - FIRST_VFP_REGNUM) / 2;
22610 }
2fa330b2
PB
22611
22612 if (IS_IWMMXT_GR_REGNUM (regno))
22613 return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
22614
22615 if (IS_IWMMXT_REGNUM (regno))
22616 return 112 + regno - FIRST_IWMMXT_REGNUM;
22617
e6d29d15 22618 gcc_unreachable ();
2fa330b2
PB
22619}
22620
854b8a40
JB
22621/* Dwarf models VFPv3 registers as 32 64-bit registers.
22622 GCC models tham as 64 32-bit registers, so we need to describe this to
22623 the DWARF generation code. Other registers can use the default. */
22624static rtx
22625arm_dwarf_register_span (rtx rtl)
22626{
22627 unsigned regno;
22628 int nregs;
22629 int i;
22630 rtx p;
22631
22632 regno = REGNO (rtl);
22633 if (!IS_VFP_REGNUM (regno))
22634 return NULL_RTX;
22635
22636 /* XXX FIXME: The EABI defines two VFP register ranges:
22637 64-95: Legacy VFPv2 numbering for S0-S31 (obsolescent)
22638 256-287: D0-D31
22639 The recommended encoding for S0-S31 is a DW_OP_bit_piece of the
22640 corresponding D register. Until GDB supports this, we shall use the
22641 legacy encodings. We also use these encodings for D0-D15 for
22642 compatibility with older debuggers. */
22643 if (VFP_REGNO_OK_FOR_SINGLE (regno))
22644 return NULL_RTX;
22645
22646 nregs = GET_MODE_SIZE (GET_MODE (rtl)) / 8;
22647 p = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nregs));
22648 regno = (regno - FIRST_VFP_REGNUM) / 2;
22649 for (i = 0; i < nregs; i++)
22650 XVECEXP (p, 0, i) = gen_rtx_REG (DImode, 256 + regno + i);
22651
22652 return p;
22653}
617a1b71 22654
f0a0390e 22655#if ARM_UNWIND_INFO
5b3e6663
PB
22656/* Emit unwind directives for a store-multiple instruction or stack pointer
22657 push during alignment.
22658 These should only ever be generated by the function prologue code, so
22659 expect them to have a particular form. */
617a1b71
PB
22660
22661static void
5b3e6663 22662arm_unwind_emit_sequence (FILE * asm_out_file, rtx p)
617a1b71
PB
22663{
22664 int i;
22665 HOST_WIDE_INT offset;
22666 HOST_WIDE_INT nregs;
22667 int reg_size;
22668 unsigned reg;
22669 unsigned lastreg;
22670 rtx e;
22671
617a1b71 22672 e = XVECEXP (p, 0, 0);
5b3e6663
PB
22673 if (GET_CODE (e) != SET)
22674 abort ();
22675
22676 /* First insn will adjust the stack pointer. */
617a1b71
PB
22677 if (GET_CODE (e) != SET
22678 || GET_CODE (XEXP (e, 0)) != REG
22679 || REGNO (XEXP (e, 0)) != SP_REGNUM
22680 || GET_CODE (XEXP (e, 1)) != PLUS)
22681 abort ();
22682
22683 offset = -INTVAL (XEXP (XEXP (e, 1), 1));
22684 nregs = XVECLEN (p, 0) - 1;
22685
22686 reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
22687 if (reg < 16)
22688 {
22689 /* The function prologue may also push pc, but not annotate it as it is
569b7f6a 22690 never restored. We turn this into a stack pointer adjustment. */
617a1b71
PB
22691 if (nregs * 4 == offset - 4)
22692 {
22693 fprintf (asm_out_file, "\t.pad #4\n");
22694 offset -= 4;
22695 }
22696 reg_size = 4;
8edfc4cc 22697 fprintf (asm_out_file, "\t.save {");
617a1b71
PB
22698 }
22699 else if (IS_VFP_REGNUM (reg))
22700 {
617a1b71 22701 reg_size = 8;
8edfc4cc 22702 fprintf (asm_out_file, "\t.vsave {");
617a1b71
PB
22703 }
22704 else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
22705 {
22706 /* FPA registers are done differently. */
ea40ba9c 22707 asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
617a1b71
PB
22708 return;
22709 }
22710 else
22711 /* Unknown register type. */
22712 abort ();
22713
22714 /* If the stack increment doesn't match the size of the saved registers,
22715 something has gone horribly wrong. */
22716 if (offset != nregs * reg_size)
22717 abort ();
22718
617a1b71
PB
22719 offset = 0;
22720 lastreg = 0;
22721 /* The remaining insns will describe the stores. */
22722 for (i = 1; i <= nregs; i++)
22723 {
22724 /* Expect (set (mem <addr>) (reg)).
22725 Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)). */
22726 e = XVECEXP (p, 0, i);
22727 if (GET_CODE (e) != SET
22728 || GET_CODE (XEXP (e, 0)) != MEM
22729 || GET_CODE (XEXP (e, 1)) != REG)
22730 abort ();
e0b92319 22731
617a1b71
PB
22732 reg = REGNO (XEXP (e, 1));
22733 if (reg < lastreg)
22734 abort ();
e0b92319 22735
617a1b71
PB
22736 if (i != 1)
22737 fprintf (asm_out_file, ", ");
22738 /* We can't use %r for vfp because we need to use the
22739 double precision register names. */
22740 if (IS_VFP_REGNUM (reg))
22741 asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
22742 else
22743 asm_fprintf (asm_out_file, "%r", reg);
22744
22745#ifdef ENABLE_CHECKING
22746 /* Check that the addresses are consecutive. */
22747 e = XEXP (XEXP (e, 0), 0);
22748 if (GET_CODE (e) == PLUS)
22749 {
22750 offset += reg_size;
22751 if (GET_CODE (XEXP (e, 0)) != REG
22752 || REGNO (XEXP (e, 0)) != SP_REGNUM
22753 || GET_CODE (XEXP (e, 1)) != CONST_INT
22754 || offset != INTVAL (XEXP (e, 1)))
22755 abort ();
22756 }
22757 else if (i != 1
22758 || GET_CODE (e) != REG
22759 || REGNO (e) != SP_REGNUM)
22760 abort ();
22761#endif
22762 }
22763 fprintf (asm_out_file, "}\n");
22764}
22765
22766/* Emit unwind directives for a SET. */
22767
22768static void
22769arm_unwind_emit_set (FILE * asm_out_file, rtx p)
22770{
22771 rtx e0;
22772 rtx e1;
5b3e6663 22773 unsigned reg;
617a1b71
PB
22774
22775 e0 = XEXP (p, 0);
22776 e1 = XEXP (p, 1);
22777 switch (GET_CODE (e0))
22778 {
22779 case MEM:
22780 /* Pushing a single register. */
22781 if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
22782 || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
22783 || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
22784 abort ();
22785
22786 asm_fprintf (asm_out_file, "\t.save ");
22787 if (IS_VFP_REGNUM (REGNO (e1)))
22788 asm_fprintf(asm_out_file, "{d%d}\n",
22789 (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
22790 else
22791 asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
22792 break;
22793
22794 case REG:
22795 if (REGNO (e0) == SP_REGNUM)
22796 {
22797 /* A stack increment. */
22798 if (GET_CODE (e1) != PLUS
22799 || GET_CODE (XEXP (e1, 0)) != REG
22800 || REGNO (XEXP (e1, 0)) != SP_REGNUM
22801 || GET_CODE (XEXP (e1, 1)) != CONST_INT)
22802 abort ();
22803
ea40ba9c 22804 asm_fprintf (asm_out_file, "\t.pad #%wd\n",
617a1b71
PB
22805 -INTVAL (XEXP (e1, 1)));
22806 }
22807 else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
22808 {
22809 HOST_WIDE_INT offset;
e0b92319 22810
617a1b71
PB
22811 if (GET_CODE (e1) == PLUS)
22812 {
22813 if (GET_CODE (XEXP (e1, 0)) != REG
22814 || GET_CODE (XEXP (e1, 1)) != CONST_INT)
22815 abort ();
22816 reg = REGNO (XEXP (e1, 0));
22817 offset = INTVAL (XEXP (e1, 1));
ea40ba9c 22818 asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
617a1b71 22819 HARD_FRAME_POINTER_REGNUM, reg,
80d56d04 22820 offset);
617a1b71
PB
22821 }
22822 else if (GET_CODE (e1) == REG)
22823 {
22824 reg = REGNO (e1);
22825 asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
22826 HARD_FRAME_POINTER_REGNUM, reg);
22827 }
22828 else
22829 abort ();
22830 }
22831 else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
22832 {
22833 /* Move from sp to reg. */
22834 asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
22835 }
758ed9b2
PB
22836 else if (GET_CODE (e1) == PLUS
22837 && GET_CODE (XEXP (e1, 0)) == REG
22838 && REGNO (XEXP (e1, 0)) == SP_REGNUM
22839 && GET_CODE (XEXP (e1, 1)) == CONST_INT)
22840 {
22841 /* Set reg to offset from sp. */
22842 asm_fprintf (asm_out_file, "\t.movsp %r, #%d\n",
22843 REGNO (e0), (int)INTVAL(XEXP (e1, 1)));
22844 }
5b3e6663
PB
22845 else if (GET_CODE (e1) == UNSPEC && XINT (e1, 1) == UNSPEC_STACK_ALIGN)
22846 {
22847 /* Stack pointer save before alignment. */
22848 reg = REGNO (e0);
22849 asm_fprintf (asm_out_file, "\t.unwind_raw 0, 0x%x @ vsp = r%d\n",
22850 reg + 0x90, reg);
22851 }
617a1b71
PB
22852 else
22853 abort ();
22854 break;
22855
22856 default:
22857 abort ();
22858 }
22859}
22860
22861
22862/* Emit unwind directives for the given insn. */
22863
22864static void
22865arm_unwind_emit (FILE * asm_out_file, rtx insn)
22866{
22867 rtx pat;
22868
d5fabb58 22869 if (arm_except_unwind_info (&global_options) != UI_TARGET)
617a1b71
PB
22870 return;
22871
e3b5732b 22872 if (!(flag_unwind_tables || crtl->uses_eh_lsda)
80efdb6a 22873 && (TREE_NOTHROW (current_function_decl)
ad516a74 22874 || crtl->all_throwers_are_sibcalls))
80efdb6a
PB
22875 return;
22876
617a1b71
PB
22877 if (GET_CODE (insn) == NOTE || !RTX_FRAME_RELATED_P (insn))
22878 return;
22879
22880 pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
22881 if (pat)
22882 pat = XEXP (pat, 0);
22883 else
22884 pat = PATTERN (insn);
22885
22886 switch (GET_CODE (pat))
22887 {
22888 case SET:
22889 arm_unwind_emit_set (asm_out_file, pat);
22890 break;
22891
22892 case SEQUENCE:
22893 /* Store multiple. */
5b3e6663 22894 arm_unwind_emit_sequence (asm_out_file, pat);
617a1b71
PB
22895 break;
22896
22897 default:
22898 abort();
22899 }
22900}
22901
22902
22903/* Output a reference from a function exception table to the type_info
22904 object X. The EABI specifies that the symbol should be relocated by
22905 an R_ARM_TARGET2 relocation. */
22906
22907static bool
22908arm_output_ttype (rtx x)
22909{
22910 fputs ("\t.word\t", asm_out_file);
22911 output_addr_const (asm_out_file, x);
22912 /* Use special relocations for symbol references. */
22913 if (GET_CODE (x) != CONST_INT)
22914 fputs ("(TARGET2)", asm_out_file);
22915 fputc ('\n', asm_out_file);
22916
22917 return TRUE;
22918}
a68b5e52
RH
22919
22920/* Implement TARGET_ASM_EMIT_EXCEPT_PERSONALITY. */
22921
22922static void
22923arm_asm_emit_except_personality (rtx personality)
22924{
22925 fputs ("\t.personality\t", asm_out_file);
22926 output_addr_const (asm_out_file, personality);
22927 fputc ('\n', asm_out_file);
22928}
22929
22930/* Implement TARGET_ASM_INITIALIZE_SECTIONS. */
22931
22932static void
22933arm_asm_init_sections (void)
22934{
22935 exception_section = get_unnamed_section (0, output_section_asm_op,
22936 "\t.handlerdata");
22937}
f0a0390e
RH
22938#endif /* ARM_UNWIND_INFO */
22939
22940/* Implement TARGET_EXCEPT_UNWIND_INFO. */
22941
22942static enum unwind_info_type
d5fabb58 22943arm_except_unwind_info (struct gcc_options *opts)
f0a0390e
RH
22944{
22945 /* Honor the --enable-sjlj-exceptions configure switch. */
22946#ifdef CONFIG_SJLJ_EXCEPTIONS
22947 if (CONFIG_SJLJ_EXCEPTIONS)
22948 return UI_SJLJ;
22949#endif
22950
22951 /* If not using ARM EABI unwind tables... */
22952 if (ARM_UNWIND_INFO)
22953 {
22954 /* For simplicity elsewhere in this file, indicate that all unwind
22955 info is disabled if we're not emitting unwind tables. */
d5fabb58 22956 if (!opts->x_flag_exceptions && !opts->x_flag_unwind_tables)
f0a0390e
RH
22957 return UI_NONE;
22958 else
22959 return UI_TARGET;
22960 }
22961
22962 /* ... we use sjlj exceptions for backwards compatibility. */
22963 return UI_SJLJ;
22964}
617a1b71
PB
22965
22966
5b3e6663
PB
22967/* Handle UNSPEC DWARF call frame instructions. These are needed for dynamic
22968 stack alignment. */
22969
22970static void
22971arm_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
22972{
22973 rtx unspec = SET_SRC (pattern);
22974 gcc_assert (GET_CODE (unspec) == UNSPEC);
22975
22976 switch (index)
22977 {
22978 case UNSPEC_STACK_ALIGN:
22979 /* ??? We should set the CFA = (SP & ~7). At this point we haven't
22980 put anything on the stack, so hopefully it won't matter.
22981 CFA = SP will be correct after alignment. */
22982 dwarf2out_reg_save_reg (label, stack_pointer_rtx,
22983 SET_DEST (pattern));
22984 break;
22985 default:
22986 gcc_unreachable ();
22987 }
22988}
22989
22990
617a1b71
PB
22991/* Output unwind directives for the start/end of a function. */
22992
22993void
22994arm_output_fn_unwind (FILE * f, bool prologue)
22995{
d5fabb58 22996 if (arm_except_unwind_info (&global_options) != UI_TARGET)
617a1b71
PB
22997 return;
22998
22999 if (prologue)
23000 fputs ("\t.fnstart\n", f);
23001 else
80efdb6a
PB
23002 {
23003 /* If this function will never be unwound, then mark it as such.
23004 The came condition is used in arm_unwind_emit to suppress
23005 the frame annotations. */
e3b5732b 23006 if (!(flag_unwind_tables || crtl->uses_eh_lsda)
80efdb6a 23007 && (TREE_NOTHROW (current_function_decl)
ad516a74 23008 || crtl->all_throwers_are_sibcalls))
80efdb6a
PB
23009 fputs("\t.cantunwind\n", f);
23010
23011 fputs ("\t.fnend\n", f);
23012 }
617a1b71 23013}
d3585b76
DJ
23014
23015static bool
23016arm_emit_tls_decoration (FILE *fp, rtx x)
23017{
23018 enum tls_reloc reloc;
23019 rtx val;
23020
23021 val = XVECEXP (x, 0, 0);
32e8bb8e 23022 reloc = (enum tls_reloc) INTVAL (XVECEXP (x, 0, 1));
d3585b76
DJ
23023
23024 output_addr_const (fp, val);
23025
23026 switch (reloc)
23027 {
23028 case TLS_GD32:
23029 fputs ("(tlsgd)", fp);
23030 break;
23031 case TLS_LDM32:
23032 fputs ("(tlsldm)", fp);
23033 break;
23034 case TLS_LDO32:
23035 fputs ("(tlsldo)", fp);
23036 break;
23037 case TLS_IE32:
23038 fputs ("(gottpoff)", fp);
23039 break;
23040 case TLS_LE32:
23041 fputs ("(tpoff)", fp);
23042 break;
23043 default:
23044 gcc_unreachable ();
23045 }
23046
23047 switch (reloc)
23048 {
23049 case TLS_GD32:
23050 case TLS_LDM32:
23051 case TLS_IE32:
23052 fputs (" + (. - ", fp);
23053 output_addr_const (fp, XVECEXP (x, 0, 2));
23054 fputs (" - ", fp);
23055 output_addr_const (fp, XVECEXP (x, 0, 3));
23056 fputc (')', fp);
23057 break;
23058 default:
23059 break;
23060 }
23061
23062 return TRUE;
23063}
23064
afcc986d
JM
23065/* ARM implementation of TARGET_ASM_OUTPUT_DWARF_DTPREL. */
23066
23067static void
23068arm_output_dwarf_dtprel (FILE *file, int size, rtx x)
23069{
23070 gcc_assert (size == 4);
23071 fputs ("\t.word\t", file);
23072 output_addr_const (file, x);
23073 fputs ("(tlsldo)", file);
23074}
23075
ffda8a0d
AS
23076/* Implement TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA. */
23077
23078static bool
d3585b76
DJ
23079arm_output_addr_const_extra (FILE *fp, rtx x)
23080{
23081 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
23082 return arm_emit_tls_decoration (fp, x);
f16fe45f
DJ
23083 else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
23084 {
23085 char label[256];
23086 int labelno = INTVAL (XVECEXP (x, 0, 0));
23087
23088 ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
23089 assemble_name_raw (fp, label);
23090
f9bd1a89
RS
23091 return TRUE;
23092 }
23093 else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_GOTSYM_OFF)
23094 {
23095 assemble_name (fp, "_GLOBAL_OFFSET_TABLE_");
23096 if (GOT_PCREL)
23097 fputs ("+.", fp);
23098 fputs ("-(", fp);
23099 output_addr_const (fp, XVECEXP (x, 0, 0));
23100 fputc (')', fp);
f16fe45f
DJ
23101 return TRUE;
23102 }
85c9bcd4
WG
23103 else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_SYMBOL_OFFSET)
23104 {
23105 output_addr_const (fp, XVECEXP (x, 0, 0));
23106 if (GOT_PCREL)
23107 fputs ("+.", fp);
23108 fputs ("-(", fp);
23109 output_addr_const (fp, XVECEXP (x, 0, 1));
23110 fputc (')', fp);
23111 return TRUE;
23112 }
d3585b76
DJ
23113 else if (GET_CODE (x) == CONST_VECTOR)
23114 return arm_emit_vector_const (fp, x);
23115
23116 return FALSE;
23117}
23118
5b3e6663
PB
23119/* Output assembly for a shift instruction.
23120 SET_FLAGS determines how the instruction modifies the condition codes.
7a085dce 23121 0 - Do not set condition codes.
5b3e6663
PB
23122 1 - Set condition codes.
23123 2 - Use smallest instruction. */
23124const char *
23125arm_output_shift(rtx * operands, int set_flags)
23126{
23127 char pattern[100];
23128 static const char flag_chars[3] = {'?', '.', '!'};
23129 const char *shift;
23130 HOST_WIDE_INT val;
23131 char c;
23132
23133 c = flag_chars[set_flags];
23134 if (TARGET_UNIFIED_ASM)
23135 {
23136 shift = shift_op(operands[3], &val);
23137 if (shift)
23138 {
23139 if (val != -1)
23140 operands[2] = GEN_INT(val);
23141 sprintf (pattern, "%s%%%c\t%%0, %%1, %%2", shift, c);
23142 }
23143 else
23144 sprintf (pattern, "mov%%%c\t%%0, %%1", c);
23145 }
23146 else
23147 sprintf (pattern, "mov%%%c\t%%0, %%1%%S3", c);
23148 output_asm_insn (pattern, operands);
23149 return "";
23150}
23151
907dd0c7
RE
23152/* Output a Thumb-1 casesi dispatch sequence. */
23153const char *
23154thumb1_output_casesi (rtx *operands)
23155{
23156 rtx diff_vec = PATTERN (next_real_insn (operands[0]));
907dd0c7
RE
23157
23158 gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
23159
907dd0c7
RE
23160 switch (GET_MODE(diff_vec))
23161 {
23162 case QImode:
23163 return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned ?
23164 "bl\t%___gnu_thumb1_case_uqi" : "bl\t%___gnu_thumb1_case_sqi");
23165 case HImode:
23166 return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned ?
23167 "bl\t%___gnu_thumb1_case_uhi" : "bl\t%___gnu_thumb1_case_shi");
23168 case SImode:
23169 return "bl\t%___gnu_thumb1_case_si";
23170 default:
23171 gcc_unreachable ();
23172 }
23173}
23174
5b3e6663
PB
23175/* Output a Thumb-2 casesi instruction. */
23176const char *
23177thumb2_output_casesi (rtx *operands)
23178{
23179 rtx diff_vec = PATTERN (next_real_insn (operands[2]));
23180
23181 gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
23182
23183 output_asm_insn ("cmp\t%0, %1", operands);
23184 output_asm_insn ("bhi\t%l3", operands);
23185 switch (GET_MODE(diff_vec))
23186 {
23187 case QImode:
23188 return "tbb\t[%|pc, %0]";
23189 case HImode:
23190 return "tbh\t[%|pc, %0, lsl #1]";
23191 case SImode:
23192 if (flag_pic)
23193 {
23194 output_asm_insn ("adr\t%4, %l2", operands);
23195 output_asm_insn ("ldr\t%5, [%4, %0, lsl #2]", operands);
23196 output_asm_insn ("add\t%4, %4, %5", operands);
23197 return "bx\t%4";
23198 }
23199 else
23200 {
23201 output_asm_insn ("adr\t%4, %l2", operands);
23202 return "ldr\t%|pc, [%4, %0, lsl #2]";
23203 }
23204 default:
23205 gcc_unreachable ();
23206 }
23207}
23208
bd4dc3cd
PB
23209/* Most ARM cores are single issue, but some newer ones can dual issue.
23210 The scheduler descriptions rely on this being correct. */
23211static int
23212arm_issue_rate (void)
23213{
23214 switch (arm_tune)
23215 {
23216 case cortexr4:
51c69ddb 23217 case cortexr4f:
d8099dd8 23218 case cortexa5:
bd4dc3cd 23219 case cortexa8:
7612f14d 23220 case cortexa9:
c02a5ccb 23221 case fa726te:
bd4dc3cd
PB
23222 return 2;
23223
23224 default:
23225 return 1;
23226 }
23227}
23228
608063c3
JB
23229/* A table and a function to perform ARM-specific name mangling for
23230 NEON vector types in order to conform to the AAPCS (see "Procedure
23231 Call Standard for the ARM Architecture", Appendix A). To qualify
23232 for emission with the mangled names defined in that document, a
23233 vector type must not only be of the correct mode but also be
23234 composed of NEON vector element types (e.g. __builtin_neon_qi). */
23235typedef struct
23236{
23237 enum machine_mode mode;
23238 const char *element_type_name;
23239 const char *aapcs_name;
23240} arm_mangle_map_entry;
23241
23242static arm_mangle_map_entry arm_mangle_map[] = {
23243 /* 64-bit containerized types. */
23244 { V8QImode, "__builtin_neon_qi", "15__simd64_int8_t" },
23245 { V8QImode, "__builtin_neon_uqi", "16__simd64_uint8_t" },
23246 { V4HImode, "__builtin_neon_hi", "16__simd64_int16_t" },
23247 { V4HImode, "__builtin_neon_uhi", "17__simd64_uint16_t" },
23248 { V2SImode, "__builtin_neon_si", "16__simd64_int32_t" },
23249 { V2SImode, "__builtin_neon_usi", "17__simd64_uint32_t" },
23250 { V2SFmode, "__builtin_neon_sf", "18__simd64_float32_t" },
23251 { V8QImode, "__builtin_neon_poly8", "16__simd64_poly8_t" },
23252 { V4HImode, "__builtin_neon_poly16", "17__simd64_poly16_t" },
23253 /* 128-bit containerized types. */
23254 { V16QImode, "__builtin_neon_qi", "16__simd128_int8_t" },
23255 { V16QImode, "__builtin_neon_uqi", "17__simd128_uint8_t" },
23256 { V8HImode, "__builtin_neon_hi", "17__simd128_int16_t" },
23257 { V8HImode, "__builtin_neon_uhi", "18__simd128_uint16_t" },
23258 { V4SImode, "__builtin_neon_si", "17__simd128_int32_t" },
23259 { V4SImode, "__builtin_neon_usi", "18__simd128_uint32_t" },
23260 { V4SFmode, "__builtin_neon_sf", "19__simd128_float32_t" },
23261 { V16QImode, "__builtin_neon_poly8", "17__simd128_poly8_t" },
23262 { V8HImode, "__builtin_neon_poly16", "18__simd128_poly16_t" },
23263 { VOIDmode, NULL, NULL }
23264};
23265
23266const char *
3101faab 23267arm_mangle_type (const_tree type)
608063c3
JB
23268{
23269 arm_mangle_map_entry *pos = arm_mangle_map;
23270
07d8efe3
MM
23271 /* The ARM ABI documents (10th October 2008) say that "__va_list"
23272 has to be managled as if it is in the "std" namespace. */
23273 if (TARGET_AAPCS_BASED
ae46a823 23274 && lang_hooks.types_compatible_p (CONST_CAST_TREE (type), va_list_type))
07d8efe3
MM
23275 {
23276 static bool warned;
d147cbd9 23277 if (!warned && warn_psabi && !in_system_header)
07d8efe3
MM
23278 {
23279 warned = true;
23280 inform (input_location,
23281 "the mangling of %<va_list%> has changed in GCC 4.4");
23282 }
23283 return "St9__va_list";
23284 }
23285
0fd8c3ad
SL
23286 /* Half-precision float. */
23287 if (TREE_CODE (type) == REAL_TYPE && TYPE_PRECISION (type) == 16)
23288 return "Dh";
23289
608063c3
JB
23290 if (TREE_CODE (type) != VECTOR_TYPE)
23291 return NULL;
23292
23293 /* Check the mode of the vector type, and the name of the vector
23294 element type, against the table. */
23295 while (pos->mode != VOIDmode)
23296 {
23297 tree elt_type = TREE_TYPE (type);
23298
23299 if (pos->mode == TYPE_MODE (type)
23300 && TREE_CODE (TYPE_NAME (elt_type)) == TYPE_DECL
23301 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (elt_type))),
23302 pos->element_type_name))
23303 return pos->aapcs_name;
23304
23305 pos++;
23306 }
23307
23308 /* Use the default mangling for unrecognized (possibly user-defined)
23309 vector types. */
23310 return NULL;
23311}
23312
795dc4fc
PB
23313/* Order of allocation of core registers for Thumb: this allocation is
23314 written over the corresponding initial entries of the array
23315 initialized with REG_ALLOC_ORDER. We allocate all low registers
23316 first. Saving and restoring a low register is usually cheaper than
23317 using a call-clobbered high register. */
23318
23319static const int thumb_core_reg_alloc_order[] =
23320{
23321 3, 2, 1, 0, 4, 5, 6, 7,
23322 14, 12, 8, 9, 10, 11, 13, 15
23323};
23324
23325/* Adjust register allocation order when compiling for Thumb. */
23326
23327void
23328arm_order_regs_for_local_alloc (void)
23329{
23330 const int arm_reg_alloc_order[] = REG_ALLOC_ORDER;
23331 memcpy(reg_alloc_order, arm_reg_alloc_order, sizeof (reg_alloc_order));
23332 if (TARGET_THUMB)
23333 memcpy (reg_alloc_order, thumb_core_reg_alloc_order,
23334 sizeof (thumb_core_reg_alloc_order));
23335}
23336
b52b1749
AS
23337/* Implement TARGET_FRAME_POINTER_REQUIRED. */
23338
23339bool
23340arm_frame_pointer_required (void)
23341{
23342 return (cfun->has_nonlocal_label
23343 || SUBTARGET_FRAME_POINTER_REQUIRED
23344 || (TARGET_ARM && TARGET_APCS_FRAME && ! leaf_function_p ()));
23345}
23346
2929029c
WG
23347/* Only thumb1 can't support conditional execution, so return true if
23348 the target is not thumb1. */
23349static bool
23350arm_have_conditional_execution (void)
23351{
23352 return !TARGET_THUMB1;
23353}
23354
029e79eb
MS
23355/* Legitimize a memory reference for sync primitive implemented using
23356 ldrex / strex. We currently force the form of the reference to be
23357 indirect without offset. We do not yet support the indirect offset
23358 addressing supported by some ARM targets for these
23359 instructions. */
23360static rtx
23361arm_legitimize_sync_memory (rtx memory)
23362{
23363 rtx addr = force_reg (Pmode, XEXP (memory, 0));
23364 rtx legitimate_memory = gen_rtx_MEM (GET_MODE (memory), addr);
23365
23366 set_mem_alias_set (legitimate_memory, ALIAS_SET_MEMORY_BARRIER);
23367 MEM_VOLATILE_P (legitimate_memory) = MEM_VOLATILE_P (memory);
23368 return legitimate_memory;
23369}
23370
23371/* An instruction emitter. */
23372typedef void (* emit_f) (int label, const char *, rtx *);
23373
23374/* An instruction emitter that emits via the conventional
23375 output_asm_insn. */
23376static void
23377arm_emit (int label ATTRIBUTE_UNUSED, const char *pattern, rtx *operands)
23378{
23379 output_asm_insn (pattern, operands);
23380}
23381
23382/* Count the number of emitted synchronization instructions. */
23383static unsigned arm_insn_count;
23384
23385/* An emitter that counts emitted instructions but does not actually
dd5a833e 23386 emit instruction into the instruction stream. */
029e79eb
MS
23387static void
23388arm_count (int label,
23389 const char *pattern ATTRIBUTE_UNUSED,
23390 rtx *operands ATTRIBUTE_UNUSED)
23391{
23392 if (! label)
23393 ++ arm_insn_count;
23394}
23395
23396/* Construct a pattern using conventional output formatting and feed
23397 it to output_asm_insn. Provides a mechanism to construct the
23398 output pattern on the fly. Note the hard limit on the pattern
23399 buffer size. */
21272a45 23400static void ATTRIBUTE_PRINTF_4
029e79eb
MS
23401arm_output_asm_insn (emit_f emit, int label, rtx *operands,
23402 const char *pattern, ...)
23403{
23404 va_list ap;
23405 char buffer[256];
23406
23407 va_start (ap, pattern);
23408 vsprintf (buffer, pattern, ap);
23409 va_end (ap);
23410 emit (label, buffer, operands);
23411}
23412
23413/* Emit the memory barrier instruction, if any, provided by this
23414 target to a specified emitter. */
23415static void
23416arm_process_output_memory_barrier (emit_f emit, rtx *operands)
23417{
23418 if (TARGET_HAVE_DMB)
23419 {
23420 /* Note we issue a system level barrier. We should consider
23421 issuing a inner shareabilty zone barrier here instead, ie.
23422 "DMB ISH". */
23423 emit (0, "dmb\tsy", operands);
23424 return;
23425 }
23426
23427 if (TARGET_HAVE_DMB_MCR)
23428 {
23429 emit (0, "mcr\tp15, 0, r0, c7, c10, 5", operands);
23430 return;
23431 }
23432
23433 gcc_unreachable ();
23434}
23435
23436/* Emit the memory barrier instruction, if any, provided by this
23437 target. */
23438const char *
23439arm_output_memory_barrier (rtx *operands)
23440{
23441 arm_process_output_memory_barrier (arm_emit, operands);
23442 return "";
23443}
23444
23445/* Helper to figure out the instruction suffix required on ldrex/strex
23446 for operations on an object of the specified mode. */
23447static const char *
23448arm_ldrex_suffix (enum machine_mode mode)
23449{
23450 switch (mode)
23451 {
23452 case QImode: return "b";
23453 case HImode: return "h";
23454 case SImode: return "";
23455 case DImode: return "d";
23456 default:
23457 gcc_unreachable ();
23458 }
23459 return "";
23460}
23461
23462/* Emit an ldrex{b,h,d, } instruction appropriate for the specified
23463 mode. */
23464static void
23465arm_output_ldrex (emit_f emit,
23466 enum machine_mode mode,
23467 rtx target,
23468 rtx memory)
23469{
23470 const char *suffix = arm_ldrex_suffix (mode);
23471 rtx operands[2];
23472
23473 operands[0] = target;
23474 operands[1] = memory;
23475 arm_output_asm_insn (emit, 0, operands, "ldrex%s\t%%0, %%C1", suffix);
23476}
23477
23478/* Emit a strex{b,h,d, } instruction appropriate for the specified
23479 mode. */
23480static void
23481arm_output_strex (emit_f emit,
23482 enum machine_mode mode,
23483 const char *cc,
23484 rtx result,
23485 rtx value,
23486 rtx memory)
23487{
23488 const char *suffix = arm_ldrex_suffix (mode);
23489 rtx operands[3];
23490
23491 operands[0] = result;
23492 operands[1] = value;
23493 operands[2] = memory;
23494 arm_output_asm_insn (emit, 0, operands, "strex%s%s\t%%0, %%1, %%C2", suffix,
23495 cc);
23496}
23497
23498/* Helper to emit a two operand instruction. */
23499static void
23500arm_output_op2 (emit_f emit, const char *mnemonic, rtx d, rtx s)
23501{
23502 rtx operands[2];
23503
23504 operands[0] = d;
23505 operands[1] = s;
23506 arm_output_asm_insn (emit, 0, operands, "%s\t%%0, %%1", mnemonic);
23507}
23508
23509/* Helper to emit a three operand instruction. */
23510static void
23511arm_output_op3 (emit_f emit, const char *mnemonic, rtx d, rtx a, rtx b)
23512{
23513 rtx operands[3];
23514
23515 operands[0] = d;
23516 operands[1] = a;
23517 operands[2] = b;
23518 arm_output_asm_insn (emit, 0, operands, "%s\t%%0, %%1, %%2", mnemonic);
23519}
23520
23521/* Emit a load store exclusive synchronization loop.
23522
23523 do
23524 old_value = [mem]
23525 if old_value != required_value
23526 break;
23527 t1 = sync_op (old_value, new_value)
23528 [mem] = t1, t2 = [0|1]
23529 while ! t2
23530
23531 Note:
23532 t1 == t2 is not permitted
23533 t1 == old_value is permitted
23534
23535 required_value:
23536
23537 RTX register or const_int representing the required old_value for
23538 the modify to continue, if NULL no comparsion is performed. */
23539static void
23540arm_output_sync_loop (emit_f emit,
23541 enum machine_mode mode,
23542 rtx old_value,
23543 rtx memory,
23544 rtx required_value,
23545 rtx new_value,
23546 rtx t1,
23547 rtx t2,
23548 enum attr_sync_op sync_op,
23549 int early_barrier_required)
23550{
23551 rtx operands[1];
23552
23553 gcc_assert (t1 != t2);
23554
23555 if (early_barrier_required)
23556 arm_process_output_memory_barrier (emit, NULL);
23557
23558 arm_output_asm_insn (emit, 1, operands, "%sLSYT%%=:", LOCAL_LABEL_PREFIX);
23559
23560 arm_output_ldrex (emit, mode, old_value, memory);
23561
23562 if (required_value)
23563 {
23564 rtx operands[2];
23565
23566 operands[0] = old_value;
23567 operands[1] = required_value;
23568 arm_output_asm_insn (emit, 0, operands, "cmp\t%%0, %%1");
23569 arm_output_asm_insn (emit, 0, operands, "bne\t%sLSYB%%=", LOCAL_LABEL_PREFIX);
23570 }
23571
23572 switch (sync_op)
23573 {
23574 case SYNC_OP_ADD:
23575 arm_output_op3 (emit, "add", t1, old_value, new_value);
23576 break;
23577
23578 case SYNC_OP_SUB:
23579 arm_output_op3 (emit, "sub", t1, old_value, new_value);
23580 break;
23581
23582 case SYNC_OP_IOR:
23583 arm_output_op3 (emit, "orr", t1, old_value, new_value);
23584 break;
23585
23586 case SYNC_OP_XOR:
23587 arm_output_op3 (emit, "eor", t1, old_value, new_value);
23588 break;
23589
23590 case SYNC_OP_AND:
23591 arm_output_op3 (emit,"and", t1, old_value, new_value);
23592 break;
23593
23594 case SYNC_OP_NAND:
23595 arm_output_op3 (emit, "and", t1, old_value, new_value);
23596 arm_output_op2 (emit, "mvn", t1, t1);
23597 break;
23598
23599 case SYNC_OP_NONE:
23600 t1 = new_value;
23601 break;
23602 }
23603
b7b79b54
KW
23604 if (t2)
23605 {
23606 arm_output_strex (emit, mode, "", t2, t1, memory);
23607 operands[0] = t2;
23608 arm_output_asm_insn (emit, 0, operands, "teq\t%%0, #0");
23609 arm_output_asm_insn (emit, 0, operands, "bne\t%sLSYT%%=",
23610 LOCAL_LABEL_PREFIX);
23611 }
23612 else
23613 {
23614 /* Use old_value for the return value because for some operations
23615 the old_value can easily be restored. This saves one register. */
23616 arm_output_strex (emit, mode, "", old_value, t1, memory);
23617 operands[0] = old_value;
23618 arm_output_asm_insn (emit, 0, operands, "teq\t%%0, #0");
23619 arm_output_asm_insn (emit, 0, operands, "bne\t%sLSYT%%=",
23620 LOCAL_LABEL_PREFIX);
23621
23622 switch (sync_op)
23623 {
23624 case SYNC_OP_ADD:
23625 arm_output_op3 (emit, "sub", old_value, t1, new_value);
23626 break;
23627
23628 case SYNC_OP_SUB:
23629 arm_output_op3 (emit, "add", old_value, t1, new_value);
23630 break;
23631
23632 case SYNC_OP_XOR:
23633 arm_output_op3 (emit, "eor", old_value, t1, new_value);
23634 break;
23635
23636 case SYNC_OP_NONE:
23637 arm_output_op2 (emit, "mov", old_value, required_value);
23638 break;
23639
23640 default:
23641 gcc_unreachable ();
23642 }
23643 }
029e79eb
MS
23644
23645 arm_process_output_memory_barrier (emit, NULL);
23646 arm_output_asm_insn (emit, 1, operands, "%sLSYB%%=:", LOCAL_LABEL_PREFIX);
23647}
23648
23649static rtx
23650arm_get_sync_operand (rtx *operands, int index, rtx default_value)
23651{
23652 if (index > 0)
23653 default_value = operands[index - 1];
23654
23655 return default_value;
23656}
23657
23658#define FETCH_SYNC_OPERAND(NAME, DEFAULT) \
23659 arm_get_sync_operand (operands, (int) get_attr_sync_##NAME (insn), DEFAULT);
23660
23661/* Extract the operands for a synchroniztion instruction from the
23662 instructions attributes and emit the instruction. */
23663static void
23664arm_process_output_sync_insn (emit_f emit, rtx insn, rtx *operands)
23665{
23666 rtx result, memory, required_value, new_value, t1, t2;
23667 int early_barrier;
23668 enum machine_mode mode;
23669 enum attr_sync_op sync_op;
23670
23671 result = FETCH_SYNC_OPERAND(result, 0);
23672 memory = FETCH_SYNC_OPERAND(memory, 0);
23673 required_value = FETCH_SYNC_OPERAND(required_value, 0);
23674 new_value = FETCH_SYNC_OPERAND(new_value, 0);
23675 t1 = FETCH_SYNC_OPERAND(t1, 0);
23676 t2 = FETCH_SYNC_OPERAND(t2, 0);
23677 early_barrier =
23678 get_attr_sync_release_barrier (insn) == SYNC_RELEASE_BARRIER_YES;
23679 sync_op = get_attr_sync_op (insn);
23680 mode = GET_MODE (memory);
23681
23682 arm_output_sync_loop (emit, mode, result, memory, required_value,
23683 new_value, t1, t2, sync_op, early_barrier);
23684}
23685
23686/* Emit a synchronization instruction loop. */
23687const char *
23688arm_output_sync_insn (rtx insn, rtx *operands)
23689{
23690 arm_process_output_sync_insn (arm_emit, insn, operands);
23691 return "";
23692}
23693
23694/* Count the number of machine instruction that will be emitted for a
23695 synchronization instruction. Note that the emitter used does not
23696 emit instructions, it just counts instructions being carefull not
23697 to count labels. */
23698unsigned int
23699arm_sync_loop_insns (rtx insn, rtx *operands)
23700{
23701 arm_insn_count = 0;
23702 arm_process_output_sync_insn (arm_count, insn, operands);
23703 return arm_insn_count;
23704}
23705
23706/* Helper to call a target sync instruction generator, dealing with
23707 the variation in operands required by the different generators. */
23708static rtx
23709arm_call_generator (struct arm_sync_generator *generator, rtx old_value,
23710 rtx memory, rtx required_value, rtx new_value)
23711{
23712 switch (generator->op)
23713 {
23714 case arm_sync_generator_omn:
23715 gcc_assert (! required_value);
23716 return generator->u.omn (old_value, memory, new_value);
23717
23718 case arm_sync_generator_omrn:
23719 gcc_assert (required_value);
23720 return generator->u.omrn (old_value, memory, required_value, new_value);
23721 }
23722
23723 return NULL;
23724}
23725
23726/* Expand a synchronization loop. The synchronization loop is expanded
23727 as an opaque block of instructions in order to ensure that we do
23728 not subsequently get extraneous memory accesses inserted within the
23729 critical region. The exclusive access property of ldrex/strex is
23730 only guaranteed in there are no intervening memory accesses. */
23731void
23732arm_expand_sync (enum machine_mode mode,
23733 struct arm_sync_generator *generator,
23734 rtx target, rtx memory, rtx required_value, rtx new_value)
23735{
23736 if (target == NULL)
23737 target = gen_reg_rtx (mode);
23738
23739 memory = arm_legitimize_sync_memory (memory);
23740 if (mode != SImode)
23741 {
23742 rtx load_temp = gen_reg_rtx (SImode);
23743
23744 if (required_value)
23745 required_value = convert_modes (SImode, mode, required_value, true);
23746
23747 new_value = convert_modes (SImode, mode, new_value, true);
23748 emit_insn (arm_call_generator (generator, load_temp, memory,
23749 required_value, new_value));
23750 emit_move_insn (target, gen_lowpart (mode, load_temp));
23751 }
23752 else
23753 {
23754 emit_insn (arm_call_generator (generator, target, memory, required_value,
23755 new_value));
23756 }
23757}
23758
69d52339
IR
23759static unsigned int
23760arm_autovectorize_vector_sizes (void)
23761{
23762 return TARGET_NEON_VECTORIZE_QUAD ? 16 | 8 : 0;
23763}
23764
c452684d
JB
23765static bool
23766arm_vector_alignment_reachable (const_tree type, bool is_packed)
23767{
23768 /* Vectors which aren't in packed structures will not be less aligned than
23769 the natural alignment of their element type, so this is safe. */
23770 if (TARGET_NEON && !BYTES_BIG_ENDIAN)
23771 return !is_packed;
23772
23773 return default_builtin_vector_alignment_reachable (type, is_packed);
23774}
23775
23776static bool
23777arm_builtin_support_vector_misalignment (enum machine_mode mode,
23778 const_tree type, int misalignment,
23779 bool is_packed)
23780{
23781 if (TARGET_NEON && !BYTES_BIG_ENDIAN)
23782 {
23783 HOST_WIDE_INT align = TYPE_ALIGN_UNIT (type);
23784
23785 if (is_packed)
23786 return align == 1;
23787
23788 /* If the misalignment is unknown, we should be able to handle the access
23789 so long as it is not to a member of a packed data structure. */
23790 if (misalignment == -1)
23791 return true;
23792
23793 /* Return true if the misalignment is a multiple of the natural alignment
23794 of the vector's element type. This is probably always going to be
23795 true in practice, since we've already established that this isn't a
23796 packed access. */
23797 return ((misalignment % align) == 0);
23798 }
23799
23800 return default_builtin_support_vector_misalignment (mode, type, misalignment,
23801 is_packed);
23802}
23803
5efd84c5
NF
23804static void
23805arm_conditional_register_usage (void)
23806{
23807 int regno;
23808
23809 if (TARGET_SOFT_FLOAT || TARGET_THUMB1 || !TARGET_FPA)
23810 {
23811 for (regno = FIRST_FPA_REGNUM;
23812 regno <= LAST_FPA_REGNUM; ++regno)
23813 fixed_regs[regno] = call_used_regs[regno] = 1;
23814 }
23815
23816 if (TARGET_THUMB1 && optimize_size)
23817 {
23818 /* When optimizing for size on Thumb-1, it's better not
23819 to use the HI regs, because of the overhead of
23820 stacking them. */
23821 for (regno = FIRST_HI_REGNUM;
23822 regno <= LAST_HI_REGNUM; ++regno)
23823 fixed_regs[regno] = call_used_regs[regno] = 1;
23824 }
23825
23826 /* The link register can be clobbered by any branch insn,
23827 but we have no way to track that at present, so mark
23828 it as unavailable. */
23829 if (TARGET_THUMB1)
23830 fixed_regs[LR_REGNUM] = call_used_regs[LR_REGNUM] = 1;
23831
23832 if (TARGET_32BIT && TARGET_HARD_FLOAT)
23833 {
23834 if (TARGET_MAVERICK)
23835 {
23836 for (regno = FIRST_FPA_REGNUM;
23837 regno <= LAST_FPA_REGNUM; ++ regno)
23838 fixed_regs[regno] = call_used_regs[regno] = 1;
23839 for (regno = FIRST_CIRRUS_FP_REGNUM;
23840 regno <= LAST_CIRRUS_FP_REGNUM; ++ regno)
23841 {
23842 fixed_regs[regno] = 0;
23843 call_used_regs[regno] = regno < FIRST_CIRRUS_FP_REGNUM + 4;
23844 }
23845 }
23846 if (TARGET_VFP)
23847 {
23848 /* VFPv3 registers are disabled when earlier VFP
23849 versions are selected due to the definition of
23850 LAST_VFP_REGNUM. */
23851 for (regno = FIRST_VFP_REGNUM;
23852 regno <= LAST_VFP_REGNUM; ++ regno)
23853 {
23854 fixed_regs[regno] = 0;
23855 call_used_regs[regno] = regno < FIRST_VFP_REGNUM + 16
23856 || regno >= FIRST_VFP_REGNUM + 32;
23857 }
23858 }
23859 }
23860
23861 if (TARGET_REALLY_IWMMXT)
23862 {
23863 regno = FIRST_IWMMXT_GR_REGNUM;
23864 /* The 2002/10/09 revision of the XScale ABI has wCG0
23865 and wCG1 as call-preserved registers. The 2002/11/21
23866 revision changed this so that all wCG registers are
23867 scratch registers. */
23868 for (regno = FIRST_IWMMXT_GR_REGNUM;
23869 regno <= LAST_IWMMXT_GR_REGNUM; ++ regno)
23870 fixed_regs[regno] = 0;
23871 /* The XScale ABI has wR0 - wR9 as scratch registers,
23872 the rest as call-preserved registers. */
23873 for (regno = FIRST_IWMMXT_REGNUM;
23874 regno <= LAST_IWMMXT_REGNUM; ++ regno)
23875 {
23876 fixed_regs[regno] = 0;
23877 call_used_regs[regno] = regno < FIRST_IWMMXT_REGNUM + 10;
23878 }
23879 }
23880
23881 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
23882 {
23883 fixed_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
23884 call_used_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
23885 }
23886 else if (TARGET_APCS_STACK)
23887 {
23888 fixed_regs[10] = 1;
23889 call_used_regs[10] = 1;
23890 }
23891 /* -mcaller-super-interworking reserves r11 for calls to
23892 _interwork_r11_call_via_rN(). Making the register global
23893 is an easy way of ensuring that it remains valid for all
23894 calls. */
23895 if (TARGET_APCS_FRAME || TARGET_CALLER_INTERWORKING
23896 || TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME)
23897 {
23898 fixed_regs[ARM_HARD_FRAME_POINTER_REGNUM] = 1;
23899 call_used_regs[ARM_HARD_FRAME_POINTER_REGNUM] = 1;
23900 if (TARGET_CALLER_INTERWORKING)
23901 global_regs[ARM_HARD_FRAME_POINTER_REGNUM] = 1;
23902 }
23903 SUBTARGET_CONDITIONAL_REGISTER_USAGE
23904}
23905
74e32076 23906static reg_class_t
6d3fbe2f 23907arm_preferred_rename_class (reg_class_t rclass)
74e32076
YQ
23908{
23909 /* Thumb-2 instructions using LO_REGS may be smaller than instructions
23910 using GENERIC_REGS. During register rename pass, we prefer LO_REGS,
23911 and code size can be reduced. */
6d3fbe2f 23912 if (TARGET_THUMB2 && rclass == GENERAL_REGS)
74e32076
YQ
23913 return LO_REGS;
23914 else
23915 return NO_REGS;
23916}
23917
0c27e2d8
WG
23918/* Compute the atrribute "length" of insn "*push_multi".
23919 So this function MUST be kept in sync with that insn pattern. */
23920int
23921arm_attr_length_push_multi(rtx parallel_op, rtx first_op)
23922{
23923 int i, regno, hi_reg;
23924 int num_saves = XVECLEN (parallel_op, 0);
23925
23926 /* ARM mode. */
23927 if (TARGET_ARM)
23928 return 4;
23929
23930 /* Thumb2 mode. */
23931 regno = REGNO (first_op);
23932 hi_reg = (REGNO_REG_CLASS (regno) == HI_REGS) && (regno != LR_REGNUM);
23933 for (i = 1; i < num_saves && !hi_reg; i++)
23934 {
23935 regno = REGNO (XEXP (XVECEXP (parallel_op, 0, i), 0));
23936 hi_reg |= (REGNO_REG_CLASS (regno) == HI_REGS) && (regno != LR_REGNUM);
23937 }
23938
23939 if (!hi_reg)
23940 return 2;
23941 return 4;
23942}
23943
d3585b76 23944#include "gt-arm.h"