]> gcc.gnu.org Git - gcc.git/blame - gcc/config/arm/arm.c
recog.c (split_insn): Consider attaching a REG_EQUAL note to the final insn of a...
[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,
bf98ec6c 3 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
cce8749e 4 Contributed by Pieter `Tiggr' Schoenmakers (rcpieter@win.tue.nl)
956d6950 5 and Martin Simmons (@harleqn.co.uk).
b36ba79f 6 More major hacks by Richard Earnshaw (rearnsha@arm.com).
cce8749e 7
4f448245 8 This file is part of GCC.
cce8749e 9
4f448245
NC
10 GCC is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published
2f83c7d6 12 by the Free Software Foundation; either version 3, or (at your
4f448245 13 option) any later version.
cce8749e 14
4f448245
NC
15 GCC is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
18 License for more details.
cce8749e 19
4f448245 20 You should have received a copy of the GNU General Public License
2f83c7d6
NC
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
f676971a 23
56636818 24#include "config.h"
43cffd11 25#include "system.h"
4977bab6
ZW
26#include "coretypes.h"
27#include "tm.h"
cce8749e 28#include "rtl.h"
d5b7b3ae 29#include "tree.h"
c7319d87 30#include "obstack.h"
cce8749e
CH
31#include "regs.h"
32#include "hard-reg-set.h"
33#include "real.h"
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"
ad076f4e 43#include "toplev.h"
aec3cfba 44#include "recog.h"
92a432f4 45#include "ggc.h"
d5b7b3ae 46#include "except.h"
8b97c5f8 47#include "c-pragma.h"
7b8b8ade 48#include "integrate.h"
c27ba912 49#include "tm_p.h"
672a6f42
NB
50#include "target.h"
51#include "target-def.h"
980e61bb 52#include "debug.h"
6e34d3a3 53#include "langhooks.h"
6fb5fa3c 54#include "df.h"
cce8749e 55
d5b7b3ae
RE
56/* Forward definitions of types. */
57typedef struct minipool_node Mnode;
58typedef struct minipool_fixup Mfix;
59
1d6e90ac
NC
60const struct attribute_spec arm_attribute_table[];
61
b76c3c4b
PB
62void (*arm_lang_output_object_attributes_hook)(void);
63
d5b7b3ae 64/* Forward function declarations. */
35596784 65static int arm_compute_static_chain_stack_bytes (void);
5848830f 66static arm_stack_offsets *arm_get_frame_offsets (void);
e32bac5b 67static void arm_add_gc_roots (void);
a406f566
MM
68static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
69 HOST_WIDE_INT, rtx, rtx, int, int);
e32bac5b
RE
70static unsigned bit_count (unsigned long);
71static int arm_address_register_rtx_p (rtx, int);
1e1ab407 72static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
5b3e6663
PB
73static int thumb2_legitimate_index_p (enum machine_mode, rtx, int);
74static int thumb1_base_register_rtx_p (rtx, enum machine_mode, int);
75inline static int thumb1_index_register_rtx_p (rtx, int);
5848830f 76static int thumb_far_jump_used_p (void);
57934c39 77static bool thumb_force_lr_save (void);
e32bac5b 78static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
e32bac5b 79static rtx emit_sfm (int, int);
466e4b7a 80static unsigned arm_size_return_regs (void);
e32bac5b 81static bool arm_assemble_integer (rtx, unsigned int, int);
e32bac5b
RE
82static const char *fp_const_from_val (REAL_VALUE_TYPE *);
83static arm_cc get_arm_condition_code (rtx);
e32bac5b
RE
84static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
85static rtx is_jump_table (rtx);
86static const char *output_multi_immediate (rtx *, const char *, const char *,
87 int, HOST_WIDE_INT);
e32bac5b
RE
88static const char *shift_op (rtx, HOST_WIDE_INT *);
89static struct machine_function *arm_init_machine_status (void);
c9ca9b88 90static void thumb_exit (FILE *, int);
e32bac5b
RE
91static rtx is_jump_table (rtx);
92static HOST_WIDE_INT get_jump_table_size (rtx);
93static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
94static Mnode *add_minipool_forward_ref (Mfix *);
95static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
96static Mnode *add_minipool_backward_ref (Mfix *);
97static void assign_minipool_offsets (Mfix *);
98static void arm_print_value (FILE *, rtx);
99static void dump_minipool (rtx);
100static int arm_barrier_cost (rtx);
101static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
102static void push_minipool_barrier (rtx, HOST_WIDE_INT);
103static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
104 rtx);
105static void arm_reorg (void);
106static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
e32bac5b
RE
107static unsigned long arm_compute_save_reg0_reg12_mask (void);
108static unsigned long arm_compute_save_reg_mask (void);
109static unsigned long arm_isr_value (tree);
110static unsigned long arm_compute_func_type (void);
111static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
112static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
7bff66a7 113#if TARGET_DLLIMPORT_DECL_ATTRIBUTES
04fb56d5 114static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
7bff66a7 115#endif
e32bac5b
RE
116static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
117static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
5b3e6663 118static void thumb1_output_function_prologue (FILE *, HOST_WIDE_INT);
3101faab 119static int arm_comp_type_attributes (const_tree, const_tree);
e32bac5b
RE
120static void arm_set_default_type_attributes (tree);
121static int arm_adjust_cost (rtx, rtx, rtx, int);
e32bac5b
RE
122static int count_insns_for_constant (HOST_WIDE_INT, int);
123static int arm_get_strip_length (int);
124static bool arm_function_ok_for_sibcall (tree, tree);
125static void arm_internal_label (FILE *, const char *, unsigned long);
126static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
127 tree);
128static int arm_rtx_costs_1 (rtx, enum rtx_code, enum rtx_code);
21b5653c 129static bool arm_size_rtx_costs (rtx, int, int, int *);
9b66ebb1
PB
130static bool arm_slowmul_rtx_costs (rtx, int, int, int *);
131static bool arm_fastmul_rtx_costs (rtx, int, int, int *);
132static bool arm_xscale_rtx_costs (rtx, int, int, int *);
133static bool arm_9e_rtx_costs (rtx, int, int, int *);
e32bac5b
RE
134static int arm_address_cost (rtx);
135static bool arm_memory_load_p (rtx);
136static bool arm_cirrus_insn_p (rtx);
137static void cirrus_reorg (rtx);
5a9335ef
NC
138static void arm_init_builtins (void);
139static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
140static void arm_init_iwmmxt_builtins (void);
141static rtx safe_vector_operand (rtx, enum machine_mode);
142static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
143static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
144static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
a406f566 145static void emit_constant_insn (rtx cond, rtx pattern);
d66437c5 146static rtx emit_set_insn (rtx, rtx);
78a52f11
RH
147static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
148 tree, bool);
5a9335ef 149
7abc66b1 150#ifdef OBJECT_FORMAT_ELF
9403b7f7
RS
151static void arm_elf_asm_constructor (rtx, int) ATTRIBUTE_UNUSED;
152static void arm_elf_asm_destructor (rtx, int) ATTRIBUTE_UNUSED;
7abc66b1 153#endif
fb49053f 154#ifndef ARM_PE
e32bac5b 155static void arm_encode_section_info (tree, rtx, int);
fb49053f 156#endif
b12a00f1
RE
157
158static void arm_file_end (void);
6c6aa1af 159static void arm_file_start (void);
b12a00f1 160
1cc9f5f5
KH
161static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
162 tree, int *, int);
8cd5a4e0 163static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
586de218
KG
164 enum machine_mode, const_tree, bool);
165static bool arm_promote_prototypes (const_tree);
6b045785 166static bool arm_default_short_enums (void);
13c1cd82 167static bool arm_align_anon_bitfield (void);
586de218
KG
168static bool arm_return_in_msb (const_tree);
169static bool arm_must_pass_in_stack (enum machine_mode, const_tree);
23668cf7 170static bool arm_return_in_memory (const_tree, const_tree);
617a1b71
PB
171#ifdef TARGET_UNWIND_INFO
172static void arm_unwind_emit (FILE *, rtx);
173static bool arm_output_ttype (rtx);
174#endif
5b3e6663 175static void arm_dwarf_handle_frame_unspec (const char *, rtx, int);
c237e94a 176
4185ae53
PB
177static tree arm_cxx_guard_type (void);
178static bool arm_cxx_guard_mask_bit (void);
46e995e0
PB
179static tree arm_get_cookie_size (tree);
180static bool arm_cookie_has_size (void);
44d10c10 181static bool arm_cxx_cdtor_returns_this (void);
505970fc 182static bool arm_cxx_key_method_may_be_inline (void);
1e731102
MM
183static void arm_cxx_determine_class_data_visibility (tree);
184static bool arm_cxx_class_data_always_comdat (void);
9f62c3e3 185static bool arm_cxx_use_aeabi_atexit (void);
b3f8d95d 186static void arm_init_libfuncs (void);
c54c7322 187static bool arm_handle_option (size_t, const char *, int);
67e6ba46 188static void arm_target_help (void);
273a2526 189static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
d3585b76
DJ
190static bool arm_cannot_copy_insn_p (rtx);
191static bool arm_tls_symbol_p (rtx x);
bd4dc3cd 192static int arm_issue_rate (void);
afcc986d 193static void arm_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
007e61c2 194static bool arm_allocate_stack_slots_for_args (void);
d3585b76 195
672a6f42
NB
196\f
197/* Initialize the GCC target structure. */
b2ca3702 198#if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1d6e90ac 199#undef TARGET_MERGE_DECL_ATTRIBUTES
672a6f42
NB
200#define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
201#endif
f3bb6135 202
1d6e90ac 203#undef TARGET_ATTRIBUTE_TABLE
91d231cb 204#define TARGET_ATTRIBUTE_TABLE arm_attribute_table
672a6f42 205
6c6aa1af
PB
206#undef TARGET_ASM_FILE_START
207#define TARGET_ASM_FILE_START arm_file_start
b12a00f1
RE
208#undef TARGET_ASM_FILE_END
209#define TARGET_ASM_FILE_END arm_file_end
210
1d6e90ac 211#undef TARGET_ASM_ALIGNED_SI_OP
301d03af 212#define TARGET_ASM_ALIGNED_SI_OP NULL
1d6e90ac 213#undef TARGET_ASM_INTEGER
301d03af 214#define TARGET_ASM_INTEGER arm_assemble_integer
301d03af 215
1d6e90ac 216#undef TARGET_ASM_FUNCTION_PROLOGUE
08c148a8
NB
217#define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
218
1d6e90ac 219#undef TARGET_ASM_FUNCTION_EPILOGUE
08c148a8
NB
220#define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
221
c54c7322
RS
222#undef TARGET_DEFAULT_TARGET_FLAGS
223#define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
224#undef TARGET_HANDLE_OPTION
225#define TARGET_HANDLE_OPTION arm_handle_option
67e6ba46
NC
226#undef TARGET_HELP
227#define TARGET_HELP arm_target_help
c54c7322 228
1d6e90ac 229#undef TARGET_COMP_TYPE_ATTRIBUTES
8d8e52be
JM
230#define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
231
1d6e90ac 232#undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
8d8e52be
JM
233#define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
234
1d6e90ac 235#undef TARGET_SCHED_ADJUST_COST
c237e94a
ZW
236#define TARGET_SCHED_ADJUST_COST arm_adjust_cost
237
fb49053f
RH
238#undef TARGET_ENCODE_SECTION_INFO
239#ifdef ARM_PE
240#define TARGET_ENCODE_SECTION_INFO arm_pe_encode_section_info
241#else
242#define TARGET_ENCODE_SECTION_INFO arm_encode_section_info
243#endif
244
5a9335ef 245#undef TARGET_STRIP_NAME_ENCODING
772c5265
RH
246#define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
247
5a9335ef 248#undef TARGET_ASM_INTERNAL_LABEL
4977bab6
ZW
249#define TARGET_ASM_INTERNAL_LABEL arm_internal_label
250
5a9335ef 251#undef TARGET_FUNCTION_OK_FOR_SIBCALL
4977bab6
ZW
252#define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
253
5a9335ef 254#undef TARGET_ASM_OUTPUT_MI_THUNK
c590b625 255#define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
5a9335ef 256#undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
3961e8fe 257#define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
c590b625 258
9b66ebb1 259/* This will be overridden in arm_override_options. */
5a9335ef 260#undef TARGET_RTX_COSTS
9b66ebb1 261#define TARGET_RTX_COSTS arm_slowmul_rtx_costs
5a9335ef 262#undef TARGET_ADDRESS_COST
dcefdf67 263#define TARGET_ADDRESS_COST arm_address_cost
3c50106f 264
273a2526
RS
265#undef TARGET_SHIFT_TRUNCATION_MASK
266#define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
f676971a
EC
267#undef TARGET_VECTOR_MODE_SUPPORTED_P
268#define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
269
5a9335ef 270#undef TARGET_MACHINE_DEPENDENT_REORG
18dbd950
RS
271#define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
272
5a9335ef
NC
273#undef TARGET_INIT_BUILTINS
274#define TARGET_INIT_BUILTINS arm_init_builtins
275#undef TARGET_EXPAND_BUILTIN
276#define TARGET_EXPAND_BUILTIN arm_expand_builtin
277
b3f8d95d
MM
278#undef TARGET_INIT_LIBFUNCS
279#define TARGET_INIT_LIBFUNCS arm_init_libfuncs
280
f9ba5949 281#undef TARGET_PROMOTE_FUNCTION_ARGS
586de218 282#define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_const_tree_true
d4453b7a 283#undef TARGET_PROMOTE_FUNCTION_RETURN
586de218 284#define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_const_tree_true
f9ba5949 285#undef TARGET_PROMOTE_PROTOTYPES
70301b45 286#define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
8cd5a4e0
RH
287#undef TARGET_PASS_BY_REFERENCE
288#define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
78a52f11
RH
289#undef TARGET_ARG_PARTIAL_BYTES
290#define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
f9ba5949 291
1cc9f5f5
KH
292#undef TARGET_SETUP_INCOMING_VARARGS
293#define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
294
007e61c2
PB
295#undef TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS
296#define TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS arm_allocate_stack_slots_for_args
297
6b045785
PB
298#undef TARGET_DEFAULT_SHORT_ENUMS
299#define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
300
13c1cd82
PB
301#undef TARGET_ALIGN_ANON_BITFIELD
302#define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
303
c2a64439
PB
304#undef TARGET_NARROW_VOLATILE_BITFIELD
305#define TARGET_NARROW_VOLATILE_BITFIELD hook_bool_void_false
306
4185ae53
PB
307#undef TARGET_CXX_GUARD_TYPE
308#define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
309
310#undef TARGET_CXX_GUARD_MASK_BIT
311#define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
312
46e995e0
PB
313#undef TARGET_CXX_GET_COOKIE_SIZE
314#define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
315
316#undef TARGET_CXX_COOKIE_HAS_SIZE
317#define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
318
44d10c10
PB
319#undef TARGET_CXX_CDTOR_RETURNS_THIS
320#define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
321
505970fc
MM
322#undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
323#define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
324
9f62c3e3
PB
325#undef TARGET_CXX_USE_AEABI_ATEXIT
326#define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
327
1e731102
MM
328#undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
329#define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
330 arm_cxx_determine_class_data_visibility
331
332#undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
333#define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
505970fc 334
866af8a9
JB
335#undef TARGET_RETURN_IN_MSB
336#define TARGET_RETURN_IN_MSB arm_return_in_msb
337
23668cf7
CLT
338#undef TARGET_RETURN_IN_MEMORY
339#define TARGET_RETURN_IN_MEMORY arm_return_in_memory
340
866af8a9
JB
341#undef TARGET_MUST_PASS_IN_STACK
342#define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
343
617a1b71
PB
344#ifdef TARGET_UNWIND_INFO
345#undef TARGET_UNWIND_EMIT
346#define TARGET_UNWIND_EMIT arm_unwind_emit
347
348/* EABI unwinding tables use a different format for the typeinfo tables. */
349#undef TARGET_ASM_TTYPE
350#define TARGET_ASM_TTYPE arm_output_ttype
351
352#undef TARGET_ARM_EABI_UNWINDER
353#define TARGET_ARM_EABI_UNWINDER true
354#endif /* TARGET_UNWIND_INFO */
355
5b3e6663
PB
356#undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
357#define TARGET_DWARF_HANDLE_FRAME_UNSPEC arm_dwarf_handle_frame_unspec
358
d3585b76
DJ
359#undef TARGET_CANNOT_COPY_INSN_P
360#define TARGET_CANNOT_COPY_INSN_P arm_cannot_copy_insn_p
361
362#ifdef HAVE_AS_TLS
363#undef TARGET_HAVE_TLS
364#define TARGET_HAVE_TLS true
365#endif
366
367#undef TARGET_CANNOT_FORCE_CONST_MEM
8426b956 368#define TARGET_CANNOT_FORCE_CONST_MEM arm_cannot_force_const_mem
d3585b76 369
f67358da
PB
370#undef TARGET_MAX_ANCHOR_OFFSET
371#define TARGET_MAX_ANCHOR_OFFSET 4095
372
373/* The minimum is set such that the total size of the block
374 for a particular anchor is -4088 + 1 + 4095 bytes, which is
375 divisible by eight, ensuring natural spacing of anchors. */
376#undef TARGET_MIN_ANCHOR_OFFSET
377#define TARGET_MIN_ANCHOR_OFFSET -4088
378
bd4dc3cd
PB
379#undef TARGET_SCHED_ISSUE_RATE
380#define TARGET_SCHED_ISSUE_RATE arm_issue_rate
381
608063c3
JB
382#undef TARGET_MANGLE_TYPE
383#define TARGET_MANGLE_TYPE arm_mangle_type
384
afcc986d
JM
385#ifdef HAVE_AS_TLS
386#undef TARGET_ASM_OUTPUT_DWARF_DTPREL
387#define TARGET_ASM_OUTPUT_DWARF_DTPREL arm_output_dwarf_dtprel
388#endif
389
f6897b10 390struct gcc_target targetm = TARGET_INITIALIZER;
672a6f42 391\f
c7319d87
RE
392/* Obstack for minipool constant handling. */
393static struct obstack minipool_obstack;
1d6e90ac 394static char * minipool_startobj;
c7319d87 395
1d6e90ac
NC
396/* The maximum number of insns skipped which
397 will be conditionalised if possible. */
c27ba912
DM
398static int max_insns_skipped = 5;
399
400extern FILE * asm_out_file;
401
6354dc9b 402/* True if we are currently building a constant table. */
13bd191d
PB
403int making_const_table;
404
60d0536b 405/* Define the information needed to generate branch insns. This is
6354dc9b 406 stored from the compare operation. */
ff9940b0 407rtx arm_compare_op0, arm_compare_op1;
ff9940b0 408
9b66ebb1
PB
409/* The processor for which instructions should be scheduled. */
410enum processor_type arm_tune = arm_none;
411
2f8e468b 412/* The default processor used if not overridden by commandline. */
6c6aa1af
PB
413static enum processor_type arm_default_cpu = arm_none;
414
9b66ebb1
PB
415/* Which floating point model to use. */
416enum arm_fp_model arm_fp_model;
bee06f3d 417
9b66ebb1 418/* Which floating point hardware is available. */
29ad9694 419enum fputype arm_fpu_arch;
b111229a 420
9b66ebb1
PB
421/* Which floating point hardware to schedule for. */
422enum fputype arm_fpu_tune;
423
424/* Whether to use floating point hardware. */
425enum float_abi_type arm_float_abi;
426
5848830f
PB
427/* Which ABI to use. */
428enum arm_abi_type arm_abi;
429
d3585b76
DJ
430/* Which thread pointer model to use. */
431enum arm_tp_type target_thread_pointer = TP_AUTO;
432
b355a481 433/* Used to parse -mstructure_size_boundary command line option. */
723ae7c1 434int arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
b355a481 435
b12a00f1 436/* Used for Thumb call_via trampolines. */
57ecec57 437rtx thumb_call_via_label[14];
b12a00f1
RE
438static int thumb_call_reg_needed;
439
aec3cfba 440/* Bit values used to identify processor capabilities. */
62b10bbc 441#define FL_CO_PROC (1 << 0) /* Has external co-processor bus */
9b66ebb1 442#define FL_ARCH3M (1 << 1) /* Extended multiply */
62b10bbc
NC
443#define FL_MODE26 (1 << 2) /* 26-bit mode support */
444#define FL_MODE32 (1 << 3) /* 32-bit mode support */
445#define FL_ARCH4 (1 << 4) /* Architecture rel 4 */
446#define FL_ARCH5 (1 << 5) /* Architecture rel 5 */
447#define FL_THUMB (1 << 6) /* Thumb aware */
448#define FL_LDSCHED (1 << 7) /* Load scheduling necessary */
449#define FL_STRONG (1 << 8) /* StrongARM */
6bc82793 450#define FL_ARCH5E (1 << 9) /* DSP extensions to v5 */
d19fb8e3 451#define FL_XSCALE (1 << 10) /* XScale */
9b6b54e2 452#define FL_CIRRUS (1 << 11) /* Cirrus/DSP. */
9b66ebb1 453#define FL_ARCH6 (1 << 12) /* Architecture rel 6. Adds
81f9037c
MM
454 media instructions. */
455#define FL_VFPV2 (1 << 13) /* Vector Floating Point V2. */
abac3b49
RE
456#define FL_WBUF (1 << 14) /* Schedule for write buffer ops.
457 Note: ARM6 & 7 derivatives only. */
d3585b76 458#define FL_ARCH6K (1 << 15) /* Architecture rel 6 K extensions. */
5b3e6663
PB
459#define FL_THUMB2 (1 << 16) /* Thumb-2. */
460#define FL_NOTM (1 << 17) /* Instructions not present in the 'M'
461 profile. */
7a085dce 462#define FL_DIV (1 << 18) /* Hardware divide. */
f1adb0a9 463#define FL_VFPV3 (1 << 19) /* Vector Floating Point V3. */
88f77cba 464#define FL_NEON (1 << 20) /* Neon instructions. */
aec3cfba 465
9b66ebb1
PB
466#define FL_IWMMXT (1 << 29) /* XScale v2 or "Intel Wireless MMX technology". */
467
5b3e6663
PB
468#define FL_FOR_ARCH2 FL_NOTM
469#define FL_FOR_ARCH3 (FL_FOR_ARCH2 | FL_MODE32)
78011587
PB
470#define FL_FOR_ARCH3M (FL_FOR_ARCH3 | FL_ARCH3M)
471#define FL_FOR_ARCH4 (FL_FOR_ARCH3M | FL_ARCH4)
472#define FL_FOR_ARCH4T (FL_FOR_ARCH4 | FL_THUMB)
473#define FL_FOR_ARCH5 (FL_FOR_ARCH4 | FL_ARCH5)
474#define FL_FOR_ARCH5T (FL_FOR_ARCH5 | FL_THUMB)
475#define FL_FOR_ARCH5E (FL_FOR_ARCH5 | FL_ARCH5E)
476#define FL_FOR_ARCH5TE (FL_FOR_ARCH5E | FL_THUMB)
477#define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
478#define FL_FOR_ARCH6 (FL_FOR_ARCH5TE | FL_ARCH6)
479#define FL_FOR_ARCH6J FL_FOR_ARCH6
d3585b76 480#define FL_FOR_ARCH6K (FL_FOR_ARCH6 | FL_ARCH6K)
fa91adc6 481#define FL_FOR_ARCH6Z FL_FOR_ARCH6
d3585b76 482#define FL_FOR_ARCH6ZK FL_FOR_ARCH6K
5b3e6663 483#define FL_FOR_ARCH6T2 (FL_FOR_ARCH6 | FL_THUMB2)
bf98ec6c 484#define FL_FOR_ARCH6M (FL_FOR_ARCH6 & ~FL_NOTM)
5b3e6663
PB
485#define FL_FOR_ARCH7 (FL_FOR_ARCH6T2 &~ FL_NOTM)
486#define FL_FOR_ARCH7A (FL_FOR_ARCH7 | FL_NOTM)
487#define FL_FOR_ARCH7R (FL_FOR_ARCH7A | FL_DIV)
488#define FL_FOR_ARCH7M (FL_FOR_ARCH7 | FL_DIV)
78011587 489
1d6e90ac
NC
490/* The bits in this mask specify which
491 instructions we are allowed to generate. */
0977774b 492static unsigned long insn_flags = 0;
d5b7b3ae 493
aec3cfba 494/* The bits in this mask specify which instruction scheduling options should
9b66ebb1 495 be used. */
0977774b 496static unsigned long tune_flags = 0;
aec3cfba
NC
497
498/* The following are used in the arm.md file as equivalents to bits
499 in the above two flag variables. */
500
9b66ebb1
PB
501/* Nonzero if this chip supports the ARM Architecture 3M extensions. */
502int arm_arch3m = 0;
2b835d68 503
6354dc9b 504/* Nonzero if this chip supports the ARM Architecture 4 extensions. */
2b835d68
RE
505int arm_arch4 = 0;
506
68d560d4
RE
507/* Nonzero if this chip supports the ARM Architecture 4t extensions. */
508int arm_arch4t = 0;
509
6354dc9b 510/* Nonzero if this chip supports the ARM Architecture 5 extensions. */
62b10bbc
NC
511int arm_arch5 = 0;
512
b15bca31
RE
513/* Nonzero if this chip supports the ARM Architecture 5E extensions. */
514int arm_arch5e = 0;
515
9b66ebb1
PB
516/* Nonzero if this chip supports the ARM Architecture 6 extensions. */
517int arm_arch6 = 0;
518
d3585b76
DJ
519/* Nonzero if this chip supports the ARM 6K extensions. */
520int arm_arch6k = 0;
521
5b3e6663
PB
522/* Nonzero if instructions not present in the 'M' profile can be used. */
523int arm_arch_notm = 0;
524
aec3cfba 525/* Nonzero if this chip can benefit from load scheduling. */
f5a1b0d2
NC
526int arm_ld_sched = 0;
527
528/* Nonzero if this chip is a StrongARM. */
abac3b49 529int arm_tune_strongarm = 0;
f5a1b0d2 530
78011587
PB
531/* Nonzero if this chip is a Cirrus variant. */
532int arm_arch_cirrus = 0;
533
5a9335ef
NC
534/* Nonzero if this chip supports Intel Wireless MMX technology. */
535int arm_arch_iwmmxt = 0;
536
d19fb8e3 537/* Nonzero if this chip is an XScale. */
4b3c2e48
PB
538int arm_arch_xscale = 0;
539
540/* Nonzero if tuning for XScale */
541int arm_tune_xscale = 0;
d19fb8e3 542
e0b92319 543/* Nonzero if we want to tune for stores that access the write-buffer.
c5d34bb2 544 This typically means an ARM6 or ARM7 with MMU or MPU. */
abac3b49 545int arm_tune_wbuf = 0;
b111229a 546
0616531f
RE
547/* Nonzero if generating Thumb instructions. */
548int thumb_code = 0;
549
2ad4dcf9 550/* Nonzero if we should define __THUMB_INTERWORK__ in the
f676971a 551 preprocessor.
2ad4dcf9
RE
552 XXX This is a bit of a hack, it's intended to help work around
553 problems in GLD which doesn't understand that armv5t code is
554 interworking clean. */
555int arm_cpp_interwork = 0;
556
5b3e6663
PB
557/* Nonzero if chip supports Thumb 2. */
558int arm_arch_thumb2;
559
560/* Nonzero if chip supports integer division instruction. */
561int arm_arch_hwdiv;
562
cce8749e
CH
563/* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
564 must report the mode of the memory reference from PRINT_OPERAND to
565 PRINT_OPERAND_ADDRESS. */
f3bb6135 566enum machine_mode output_memory_reference_mode;
cce8749e 567
32de079a 568/* The register number to be used for the PIC offset register. */
020a4035 569unsigned arm_pic_register = INVALID_REGNUM;
32de079a 570
ff9940b0 571/* Set to 1 when a return insn is output, this means that the epilogue
6354dc9b 572 is not needed. */
d5b7b3ae 573int return_used_this_function;
ff9940b0 574
aec3cfba
NC
575/* Set to 1 after arm_reorg has started. Reset to start at the start of
576 the next function. */
4b632bf1
RE
577static int after_arm_reorg = 0;
578
aec3cfba 579/* The maximum number of insns to be used when loading a constant. */
2b835d68
RE
580static int arm_constant_limit = 3;
581
cce8749e
CH
582/* For an explanation of these variables, see final_prescan_insn below. */
583int arm_ccfsm_state;
5b3e6663 584/* arm_current_cc is also used for Thumb-2 cond_exec blocks. */
84ed5e79 585enum arm_cond_code arm_current_cc;
cce8749e
CH
586rtx arm_target_insn;
587int arm_target_label;
5b3e6663
PB
588/* The number of conditionally executed insns, including the current insn. */
589int arm_condexec_count = 0;
590/* A bitmask specifying the patterns for the IT block.
591 Zero means do not output an IT block before this insn. */
592int arm_condexec_mask = 0;
593/* The number of bits used in arm_condexec_mask. */
594int arm_condexec_masklen = 0;
9997d19d
RE
595
596/* The condition codes of the ARM, and the inverse function. */
1d6e90ac 597static const char * const arm_condition_codes[] =
9997d19d
RE
598{
599 "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
600 "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
601};
602
5b3e6663 603#define ARM_LSL_NAME (TARGET_UNIFIED_ASM ? "lsl" : "asl")
f5a1b0d2 604#define streq(string1, string2) (strcmp (string1, string2) == 0)
5b3e6663
PB
605
606#define THUMB2_WORK_REGS (0xff & ~( (1 << THUMB_HARD_FRAME_POINTER_REGNUM) \
607 | (1 << SP_REGNUM) | (1 << PC_REGNUM) \
608 | (1 << PIC_OFFSET_TABLE_REGNUM)))
2b835d68 609\f
6354dc9b 610/* Initialization code. */
2b835d68 611
2b835d68
RE
612struct processors
613{
8b60264b 614 const char *const name;
9b66ebb1 615 enum processor_type core;
78011587 616 const char *arch;
0977774b 617 const unsigned long flags;
9b66ebb1 618 bool (* rtx_costs) (rtx, int, int, int *);
2b835d68
RE
619};
620
621/* Not all of these give usefully different compilation alternatives,
622 but there is no simple way of generalizing them. */
8b60264b 623static const struct processors all_cores[] =
f5a1b0d2
NC
624{
625 /* ARM Cores */
d98a72fd
RE
626#define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
627 {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
9b66ebb1
PB
628#include "arm-cores.def"
629#undef ARM_CORE
78011587 630 {NULL, arm_none, NULL, 0, NULL}
f5a1b0d2
NC
631};
632
8b60264b 633static const struct processors all_architectures[] =
2b835d68 634{
f5a1b0d2 635 /* ARM Architectures */
9b66ebb1
PB
636 /* We don't specify rtx_costs here as it will be figured out
637 from the core. */
f676971a 638
78011587
PB
639 {"armv2", arm2, "2", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
640 {"armv2a", arm2, "2", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
641 {"armv3", arm6, "3", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
642 {"armv3m", arm7m, "3M", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
643 {"armv4", arm7tdmi, "4", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
b111229a
RE
644 /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
645 implementations that support it, so we will leave it out for now. */
78011587
PB
646 {"armv4t", arm7tdmi, "4T", FL_CO_PROC | FL_FOR_ARCH4T, NULL},
647 {"armv5", arm10tdmi, "5", FL_CO_PROC | FL_FOR_ARCH5, NULL},
648 {"armv5t", arm10tdmi, "5T", FL_CO_PROC | FL_FOR_ARCH5T, NULL},
649 {"armv5e", arm1026ejs, "5E", FL_CO_PROC | FL_FOR_ARCH5E, NULL},
650 {"armv5te", arm1026ejs, "5TE", FL_CO_PROC | FL_FOR_ARCH5TE, NULL},
651 {"armv6", arm1136js, "6", FL_CO_PROC | FL_FOR_ARCH6, NULL},
652 {"armv6j", arm1136js, "6J", FL_CO_PROC | FL_FOR_ARCH6J, NULL},
fa91adc6
PB
653 {"armv6k", mpcore, "6K", FL_CO_PROC | FL_FOR_ARCH6K, NULL},
654 {"armv6z", arm1176jzs, "6Z", FL_CO_PROC | FL_FOR_ARCH6Z, NULL},
655 {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC | FL_FOR_ARCH6ZK, NULL},
5b3e6663 656 {"armv6t2", arm1156t2s, "6T2", FL_CO_PROC | FL_FOR_ARCH6T2, NULL},
bf98ec6c 657 {"armv6-m", cortexm1, "6M", FL_FOR_ARCH6M, NULL},
5b3e6663
PB
658 {"armv7", cortexa8, "7", FL_CO_PROC | FL_FOR_ARCH7, NULL},
659 {"armv7-a", cortexa8, "7A", FL_CO_PROC | FL_FOR_ARCH7A, NULL},
660 {"armv7-r", cortexr4, "7R", FL_CO_PROC | FL_FOR_ARCH7R, NULL},
661 {"armv7-m", cortexm3, "7M", FL_CO_PROC | FL_FOR_ARCH7M, NULL},
78011587
PB
662 {"ep9312", ep9312, "4T", FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
663 {"iwmmxt", iwmmxt, "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
664 {NULL, arm_none, NULL, 0 , NULL}
f5a1b0d2
NC
665};
666
c54c7322
RS
667struct arm_cpu_select
668{
669 const char * string;
670 const char * name;
671 const struct processors * processors;
672};
673
9a9f7594 674/* This is a magic structure. The 'string' field is magically filled in
f5a1b0d2
NC
675 with a pointer to the value specified by the user on the command line
676 assuming that the user has specified such a value. */
677
c54c7322 678static struct arm_cpu_select arm_select[] =
f5a1b0d2 679{
f676971a 680 /* string name processors */
f5a1b0d2
NC
681 { NULL, "-mcpu=", all_cores },
682 { NULL, "-march=", all_architectures },
683 { NULL, "-mtune=", all_cores }
2b835d68
RE
684};
685
e6fffade
RE
686/* Defines representing the indexes into the above table. */
687#define ARM_OPT_SET_CPU 0
688#define ARM_OPT_SET_ARCH 1
689#define ARM_OPT_SET_TUNE 2
78011587 690
afc0a4ba 691/* The name of the preprocessor macro to define for this architecture. */
78011587
PB
692
693char arm_arch_name[] = "__ARM_ARCH_0UNK__";
694
9b66ebb1
PB
695struct fpu_desc
696{
697 const char * name;
698 enum fputype fpu;
699};
700
701
56f42830 702/* Available values for -mfpu=. */
9b66ebb1
PB
703
704static const struct fpu_desc all_fpus[] =
705{
706 {"fpa", FPUTYPE_FPA},
707 {"fpe2", FPUTYPE_FPA_EMU2},
708 {"fpe3", FPUTYPE_FPA_EMU2},
709 {"maverick", FPUTYPE_MAVERICK},
f1adb0a9
JB
710 {"vfp", FPUTYPE_VFP},
711 {"vfp3", FPUTYPE_VFP3},
302c3d8e
PB
712 {"vfpv3", FPUTYPE_VFP3},
713 {"vfpv3-d16", FPUTYPE_VFP3D16},
88f77cba 714 {"neon", FPUTYPE_NEON}
9b66ebb1
PB
715};
716
717
718/* Floating point models used by the different hardware.
719 See fputype in arm.h. */
720
721static const enum fputype fp_model_for_fpu[] =
722{
723 /* No FP hardware. */
724 ARM_FP_MODEL_UNKNOWN, /* FPUTYPE_NONE */
725 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA */
726 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA_EMU2 */
727 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA_EMU3 */
728 ARM_FP_MODEL_MAVERICK, /* FPUTYPE_MAVERICK */
f1adb0a9 729 ARM_FP_MODEL_VFP, /* FPUTYPE_VFP */
302c3d8e 730 ARM_FP_MODEL_VFP, /* FPUTYPE_VFP3D16 */
88f77cba
JB
731 ARM_FP_MODEL_VFP, /* FPUTYPE_VFP3 */
732 ARM_FP_MODEL_VFP /* FPUTYPE_NEON */
9b66ebb1
PB
733};
734
735
736struct float_abi
737{
738 const char * name;
739 enum float_abi_type abi_type;
740};
741
742
743/* Available values for -mfloat-abi=. */
744
745static const struct float_abi all_float_abis[] =
746{
747 {"soft", ARM_FLOAT_ABI_SOFT},
748 {"softfp", ARM_FLOAT_ABI_SOFTFP},
749 {"hard", ARM_FLOAT_ABI_HARD}
750};
751
752
5848830f
PB
753struct abi_name
754{
755 const char *name;
756 enum arm_abi_type abi_type;
757};
758
759
760/* Available values for -mabi=. */
761
762static const struct abi_name arm_all_abis[] =
763{
764 {"apcs-gnu", ARM_ABI_APCS},
765 {"atpcs", ARM_ABI_ATPCS},
766 {"aapcs", ARM_ABI_AAPCS},
077fc835
KH
767 {"iwmmxt", ARM_ABI_IWMMXT},
768 {"aapcs-linux", ARM_ABI_AAPCS_LINUX}
5848830f
PB
769};
770
d3585b76
DJ
771/* Supported TLS relocations. */
772
773enum tls_reloc {
774 TLS_GD32,
775 TLS_LDM32,
776 TLS_LDO32,
777 TLS_IE32,
778 TLS_LE32
779};
780
d66437c5
RE
781/* Emit an insn that's a simple single-set. Both the operands must be known
782 to be valid. */
783inline static rtx
784emit_set_insn (rtx x, rtx y)
785{
786 return emit_insn (gen_rtx_SET (VOIDmode, x, y));
787}
788
0977774b
JT
789/* Return the number of bits set in VALUE. */
790static unsigned
e32bac5b 791bit_count (unsigned long value)
aec3cfba 792{
d5b7b3ae 793 unsigned long count = 0;
f676971a 794
aec3cfba
NC
795 while (value)
796 {
0977774b
JT
797 count++;
798 value &= value - 1; /* Clear the least-significant set bit. */
aec3cfba
NC
799 }
800
801 return count;
802}
803
c112cf2b 804/* Set up library functions unique to ARM. */
b3f8d95d
MM
805
806static void
807arm_init_libfuncs (void)
808{
809 /* There are no special library functions unless we are using the
810 ARM BPABI. */
811 if (!TARGET_BPABI)
812 return;
813
814 /* The functions below are described in Section 4 of the "Run-Time
815 ABI for the ARM architecture", Version 1.0. */
816
817 /* Double-precision floating-point arithmetic. Table 2. */
818 set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
819 set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
820 set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
821 set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
822 set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
823
c112cf2b 824 /* Double-precision comparisons. Table 3. */
b3f8d95d
MM
825 set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
826 set_optab_libfunc (ne_optab, DFmode, NULL);
827 set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
828 set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
829 set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
830 set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
831 set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
832
833 /* Single-precision floating-point arithmetic. Table 4. */
834 set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
835 set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
836 set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
837 set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
838 set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
f676971a 839
c112cf2b 840 /* Single-precision comparisons. Table 5. */
b3f8d95d
MM
841 set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
842 set_optab_libfunc (ne_optab, SFmode, NULL);
843 set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
844 set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
845 set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
846 set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
847 set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
848
849 /* Floating-point to integer conversions. Table 6. */
850 set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
851 set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
852 set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
853 set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
854 set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
855 set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
856 set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
857 set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
858
859 /* Conversions between floating types. Table 7. */
860 set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
861 set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
862
c112cf2b 863 /* Integer to floating-point conversions. Table 8. */
b3f8d95d
MM
864 set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
865 set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
866 set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
867 set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
868 set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
869 set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
870 set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
871 set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
872
873 /* Long long. Table 9. */
874 set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
875 set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
876 set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
877 set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
878 set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
879 set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
880 set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
881 set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
882
883 /* Integer (32/32->32) division. \S 4.3.1. */
884 set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
885 set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
886
887 /* The divmod functions are designed so that they can be used for
888 plain division, even though they return both the quotient and the
889 remainder. The quotient is returned in the usual location (i.e.,
890 r0 for SImode, {r0, r1} for DImode), just as would be expected
891 for an ordinary division routine. Because the AAPCS calling
892 conventions specify that all of { r0, r1, r2, r3 } are
893 callee-saved registers, there is no need to tell the compiler
894 explicitly that those registers are clobbered by these
895 routines. */
896 set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
897 set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
e993ba8f
DJ
898
899 /* For SImode division the ABI provides div-without-mod routines,
900 which are faster. */
901 set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
902 set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
01c19d47
PB
903
904 /* We don't have mod libcalls. Fortunately gcc knows how to use the
905 divmod libcalls instead. */
906 set_optab_libfunc (smod_optab, DImode, NULL);
907 set_optab_libfunc (umod_optab, DImode, NULL);
908 set_optab_libfunc (smod_optab, SImode, NULL);
909 set_optab_libfunc (umod_optab, SImode, NULL);
b3f8d95d
MM
910}
911
c54c7322
RS
912/* Implement TARGET_HANDLE_OPTION. */
913
914static bool
915arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
916{
917 switch (code)
918 {
c54c7322
RS
919 case OPT_march_:
920 arm_select[1].string = arg;
921 return true;
922
923 case OPT_mcpu_:
924 arm_select[0].string = arg;
925 return true;
926
c54c7322
RS
927 case OPT_mhard_float:
928 target_float_abi_name = "hard";
929 return true;
930
c54c7322
RS
931 case OPT_msoft_float:
932 target_float_abi_name = "soft";
933 return true;
934
c54c7322
RS
935 case OPT_mtune_:
936 arm_select[2].string = arg;
937 return true;
938
939 default:
940 return true;
941 }
942}
943
67e6ba46
NC
944static void
945arm_target_help (void)
946{
947 int i;
948 static int columns = 0;
949 int remaining;
950
951 /* If we have not done so already, obtain the desired maximum width of
952 the output. Note - this is a duplication of the code at the start of
953 gcc/opts.c:print_specific_help() - the two copies should probably be
954 replaced by a single function. */
955 if (columns == 0)
956 {
957 const char *p;
958
959 GET_ENVIRONMENT (p, "COLUMNS");
960 if (p != NULL)
961 {
962 int value = atoi (p);
963
964 if (value > 0)
965 columns = value;
966 }
967
968 if (columns == 0)
969 /* Use a reasonable default. */
970 columns = 80;
971 }
972
973 printf (" Known ARM CPUs (for use with the -mcpu= and -mtune= options):\n");
974
975 /* The - 2 is because we know that the last entry in the array is NULL. */
976 i = ARRAY_SIZE (all_cores) - 2;
977 gcc_assert (i > 0);
978 printf (" %s", all_cores[i].name);
979 remaining = columns - (strlen (all_cores[i].name) + 4);
980 gcc_assert (remaining >= 0);
981
982 while (i--)
983 {
984 int len = strlen (all_cores[i].name);
985
986 if (remaining > len + 2)
987 {
988 printf (", %s", all_cores[i].name);
989 remaining -= len + 2;
990 }
991 else
992 {
993 if (remaining > 0)
994 printf (",");
995 printf ("\n %s", all_cores[i].name);
996 remaining = columns - (len + 4);
997 }
998 }
999
1000 printf ("\n\n Known ARM architectures (for use with the -march= option):\n");
1001
1002 i = ARRAY_SIZE (all_architectures) - 2;
1003 gcc_assert (i > 0);
1004
1005 printf (" %s", all_architectures[i].name);
1006 remaining = columns - (strlen (all_architectures[i].name) + 4);
1007 gcc_assert (remaining >= 0);
1008
1009 while (i--)
1010 {
1011 int len = strlen (all_architectures[i].name);
1012
1013 if (remaining > len + 2)
1014 {
1015 printf (", %s", all_architectures[i].name);
1016 remaining -= len + 2;
1017 }
1018 else
1019 {
1020 if (remaining > 0)
1021 printf (",");
1022 printf ("\n %s", all_architectures[i].name);
1023 remaining = columns - (len + 4);
1024 }
1025 }
1026 printf ("\n");
1027
1028}
1029
2b835d68
RE
1030/* Fix up any incompatible options that the user has specified.
1031 This has now turned into a maze. */
1032void
e32bac5b 1033arm_override_options (void)
2b835d68 1034{
ed4c4348 1035 unsigned i;
e6fffade 1036 enum processor_type target_arch_cpu = arm_none;
9b66ebb1 1037
f5a1b0d2 1038 /* Set up the flags based on the cpu/architecture selected by the user. */
b6a1cbae 1039 for (i = ARRAY_SIZE (arm_select); i--;)
bd9c7e23 1040 {
f5a1b0d2 1041 struct arm_cpu_select * ptr = arm_select + i;
f676971a 1042
f5a1b0d2 1043 if (ptr->string != NULL && ptr->string[0] != '\0')
bd9c7e23 1044 {
13bd191d 1045 const struct processors * sel;
bd9c7e23 1046
5895f793 1047 for (sel = ptr->processors; sel->name != NULL; sel++)
f5a1b0d2 1048 if (streq (ptr->string, sel->name))
bd9c7e23 1049 {
78011587 1050 /* Set the architecture define. */
e6fffade 1051 if (i != ARM_OPT_SET_TUNE)
78011587
PB
1052 sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
1053
9b66ebb1
PB
1054 /* Determine the processor core for which we should
1055 tune code-generation. */
1056 if (/* -mcpu= is a sensible default. */
e6fffade 1057 i == ARM_OPT_SET_CPU
9b66ebb1 1058 /* -mtune= overrides -mcpu= and -march=. */
e6fffade 1059 || i == ARM_OPT_SET_TUNE)
9b66ebb1
PB
1060 arm_tune = (enum processor_type) (sel - ptr->processors);
1061
e6fffade
RE
1062 /* Remember the CPU associated with this architecture.
1063 If no other option is used to set the CPU type,
1064 we'll use this to guess the most suitable tuning
1065 options. */
1066 if (i == ARM_OPT_SET_ARCH)
1067 target_arch_cpu = sel->core;
e0b92319 1068
e6fffade 1069 if (i != ARM_OPT_SET_TUNE)
b111229a 1070 {
aec3cfba
NC
1071 /* If we have been given an architecture and a processor
1072 make sure that they are compatible. We only generate
1073 a warning though, and we prefer the CPU over the
6354dc9b 1074 architecture. */
aec3cfba 1075 if (insn_flags != 0 && (insn_flags ^ sel->flags))
d4ee4d25 1076 warning (0, "switch -mcpu=%s conflicts with -march= switch",
aec3cfba 1077 ptr->string);
f676971a 1078
aec3cfba 1079 insn_flags = sel->flags;
b111229a 1080 }
f676971a 1081
bd9c7e23
RE
1082 break;
1083 }
1084
1085 if (sel->name == NULL)
1086 error ("bad value (%s) for %s switch", ptr->string, ptr->name);
1087 }
1088 }
f676971a 1089
e6fffade
RE
1090 /* Guess the tuning options from the architecture if necessary. */
1091 if (arm_tune == arm_none)
1092 arm_tune = target_arch_cpu;
1093
f5a1b0d2 1094 /* If the user did not specify a processor, choose one for them. */
aec3cfba 1095 if (insn_flags == 0)
f5a1b0d2 1096 {
8b60264b 1097 const struct processors * sel;
aec3cfba 1098 unsigned int sought;
78011587 1099 enum processor_type cpu;
aec3cfba 1100
78011587
PB
1101 cpu = TARGET_CPU_DEFAULT;
1102 if (cpu == arm_none)
1103 {
1104#ifdef SUBTARGET_CPU_DEFAULT
1105 /* Use the subtarget default CPU if none was specified by
1106 configure. */
1107 cpu = SUBTARGET_CPU_DEFAULT;
1108#endif
1109 /* Default to ARM6. */
1110 if (cpu == arm_none)
1111 cpu = arm6;
1112 }
1113 sel = &all_cores[cpu];
aec3cfba
NC
1114
1115 insn_flags = sel->flags;
9b66ebb1 1116
aec3cfba
NC
1117 /* Now check to see if the user has specified some command line
1118 switch that require certain abilities from the cpu. */
1119 sought = 0;
f676971a 1120
d5b7b3ae 1121 if (TARGET_INTERWORK || TARGET_THUMB)
f5a1b0d2 1122 {
aec3cfba 1123 sought |= (FL_THUMB | FL_MODE32);
f676971a 1124
d5b7b3ae 1125 /* There are no ARM processors that support both APCS-26 and
aec3cfba
NC
1126 interworking. Therefore we force FL_MODE26 to be removed
1127 from insn_flags here (if it was set), so that the search
1128 below will always be able to find a compatible processor. */
5895f793 1129 insn_flags &= ~FL_MODE26;
f5a1b0d2 1130 }
f676971a 1131
aec3cfba 1132 if (sought != 0 && ((sought & insn_flags) != sought))
f5a1b0d2 1133 {
aec3cfba
NC
1134 /* Try to locate a CPU type that supports all of the abilities
1135 of the default CPU, plus the extra abilities requested by
1136 the user. */
5895f793 1137 for (sel = all_cores; sel->name != NULL; sel++)
aec3cfba 1138 if ((sel->flags & sought) == (sought | insn_flags))
f5a1b0d2
NC
1139 break;
1140
1141 if (sel->name == NULL)
aec3cfba 1142 {
0977774b 1143 unsigned current_bit_count = 0;
8b60264b 1144 const struct processors * best_fit = NULL;
f676971a 1145
aec3cfba
NC
1146 /* Ideally we would like to issue an error message here
1147 saying that it was not possible to find a CPU compatible
1148 with the default CPU, but which also supports the command
1149 line options specified by the programmer, and so they
1150 ought to use the -mcpu=<name> command line option to
1151 override the default CPU type.
1152
61f0ccff
RE
1153 If we cannot find a cpu that has both the
1154 characteristics of the default cpu and the given
1155 command line options we scan the array again looking
1156 for a best match. */
5895f793 1157 for (sel = all_cores; sel->name != NULL; sel++)
aec3cfba
NC
1158 if ((sel->flags & sought) == sought)
1159 {
0977774b 1160 unsigned count;
aec3cfba
NC
1161
1162 count = bit_count (sel->flags & insn_flags);
1163
1164 if (count >= current_bit_count)
1165 {
1166 best_fit = sel;
1167 current_bit_count = count;
1168 }
1169 }
f5a1b0d2 1170
e6d29d15
NS
1171 gcc_assert (best_fit);
1172 sel = best_fit;
aec3cfba
NC
1173 }
1174
1175 insn_flags = sel->flags;
f5a1b0d2 1176 }
78011587 1177 sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
6c6aa1af 1178 arm_default_cpu = (enum processor_type) (sel - all_cores);
9b66ebb1 1179 if (arm_tune == arm_none)
6c6aa1af 1180 arm_tune = arm_default_cpu;
f5a1b0d2 1181 }
f676971a 1182
9b66ebb1
PB
1183 /* The processor for which we should tune should now have been
1184 chosen. */
e6d29d15 1185 gcc_assert (arm_tune != arm_none);
f676971a 1186
9b66ebb1 1187 tune_flags = all_cores[(int)arm_tune].flags;
21b5653c
RE
1188 if (optimize_size)
1189 targetm.rtx_costs = arm_size_rtx_costs;
1190 else
1191 targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
e26053d1 1192
f5a1b0d2
NC
1193 /* Make sure that the processor choice does not conflict with any of the
1194 other command line choices. */
5b3e6663
PB
1195 if (TARGET_ARM && !(insn_flags & FL_NOTM))
1196 error ("target CPU does not support ARM mode");
1197
6cfc7210 1198 if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
f5a1b0d2 1199 {
d4ee4d25 1200 warning (0, "target CPU does not support interworking" );
c54c7322 1201 target_flags &= ~MASK_INTERWORK;
f5a1b0d2 1202 }
f676971a 1203
d5b7b3ae
RE
1204 if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1205 {
d4ee4d25 1206 warning (0, "target CPU does not support THUMB instructions");
c54c7322 1207 target_flags &= ~MASK_THUMB;
d5b7b3ae
RE
1208 }
1209
1210 if (TARGET_APCS_FRAME && TARGET_THUMB)
1211 {
d4ee4d25 1212 /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
c54c7322 1213 target_flags &= ~MASK_APCS_FRAME;
d5b7b3ae 1214 }
d19fb8e3 1215
da8ce8be
RE
1216 /* Callee super interworking implies thumb interworking. Adding
1217 this to the flags here simplifies the logic elsewhere. */
1218 if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
1219 target_flags |= MASK_INTERWORK;
1220
d5b7b3ae
RE
1221 /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1222 from here where no function is being compiled currently. */
c54c7322 1223 if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
d4ee4d25 1224 warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
d5b7b3ae
RE
1225
1226 if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
d4ee4d25 1227 warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
d5b7b3ae
RE
1228
1229 if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
d4ee4d25 1230 warning (0, "enabling caller interworking support is only meaningful when compiling for the Thumb");
d5b7b3ae 1231
5895f793 1232 if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
f5a1b0d2 1233 {
d4ee4d25 1234 warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
c54c7322 1235 target_flags |= MASK_APCS_FRAME;
f5a1b0d2 1236 }
f676971a 1237
2b835d68 1238 if (TARGET_POKE_FUNCTION_NAME)
c54c7322 1239 target_flags |= MASK_APCS_FRAME;
f676971a 1240
2b835d68 1241 if (TARGET_APCS_REENT && flag_pic)
400500c4 1242 error ("-fpic and -mapcs-reent are incompatible");
f676971a 1243
2b835d68 1244 if (TARGET_APCS_REENT)
d4ee4d25 1245 warning (0, "APCS reentrant code not supported. Ignored");
f676971a 1246
d5b7b3ae
RE
1247 /* If this target is normally configured to use APCS frames, warn if they
1248 are turned off and debugging is turned on. */
1249 if (TARGET_ARM
1250 && write_symbols != NO_DEBUG
5895f793 1251 && !TARGET_APCS_FRAME
c54c7322 1252 && (TARGET_DEFAULT & MASK_APCS_FRAME))
d4ee4d25 1253 warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
f676971a 1254
2b835d68 1255 if (TARGET_APCS_FLOAT)
d4ee4d25 1256 warning (0, "passing floating point arguments in fp regs not yet supported");
f676971a 1257
4912a07c 1258 /* Initialize boolean versions of the flags, for use in the arm.md file. */
9b66ebb1
PB
1259 arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1260 arm_arch4 = (insn_flags & FL_ARCH4) != 0;
68d560d4 1261 arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
9b66ebb1
PB
1262 arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1263 arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1264 arm_arch6 = (insn_flags & FL_ARCH6) != 0;
d3585b76 1265 arm_arch6k = (insn_flags & FL_ARCH6K) != 0;
5b3e6663
PB
1266 arm_arch_notm = (insn_flags & FL_NOTM) != 0;
1267 arm_arch_thumb2 = (insn_flags & FL_THUMB2) != 0;
9b66ebb1 1268 arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
78011587 1269 arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
9b66ebb1
PB
1270
1271 arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
abac3b49 1272 arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
9b66ebb1 1273 thumb_code = (TARGET_ARM == 0);
abac3b49 1274 arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
9b66ebb1
PB
1275 arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1276 arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
5b3e6663 1277 arm_arch_hwdiv = (insn_flags & FL_DIV) != 0;
5a9335ef 1278
f67358da
PB
1279 /* If we are not using the default (ARM mode) section anchor offset
1280 ranges, then set the correct ranges now. */
1281 if (TARGET_THUMB1)
1282 {
1283 /* Thumb-1 LDR instructions cannot have negative offsets.
1284 Permissible positive offset ranges are 5-bit (for byte loads),
1285 6-bit (for halfword loads), or 7-bit (for word loads).
1286 Empirical results suggest a 7-bit anchor range gives the best
1287 overall code size. */
1288 targetm.min_anchor_offset = 0;
1289 targetm.max_anchor_offset = 127;
1290 }
1291 else if (TARGET_THUMB2)
1292 {
1293 /* The minimum is set such that the total size of the block
1294 for a particular anchor is 248 + 1 + 4095 bytes, which is
1295 divisible by eight, ensuring natural spacing of anchors. */
1296 targetm.min_anchor_offset = -248;
1297 targetm.max_anchor_offset = 4095;
1298 }
1299
68d560d4
RE
1300 /* V5 code we generate is completely interworking capable, so we turn off
1301 TARGET_INTERWORK here to avoid many tests later on. */
2ad4dcf9
RE
1302
1303 /* XXX However, we must pass the right pre-processor defines to CPP
1304 or GLD can get confused. This is a hack. */
1305 if (TARGET_INTERWORK)
1306 arm_cpp_interwork = 1;
1307
68d560d4 1308 if (arm_arch5)
c54c7322 1309 target_flags &= ~MASK_INTERWORK;
68d560d4 1310
5848830f
PB
1311 if (target_abi_name)
1312 {
1313 for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1314 {
1315 if (streq (arm_all_abis[i].name, target_abi_name))
1316 {
1317 arm_abi = arm_all_abis[i].abi_type;
1318 break;
1319 }
1320 }
1321 if (i == ARRAY_SIZE (arm_all_abis))
1322 error ("invalid ABI option: -mabi=%s", target_abi_name);
1323 }
1324 else
c805f22e 1325 arm_abi = ARM_DEFAULT_ABI;
5848830f
PB
1326
1327 if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1328 error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1329
1330 if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1331 error ("iwmmxt abi requires an iwmmxt capable cpu");
6f7ebcbb 1332
9b66ebb1
PB
1333 arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1334 if (target_fpu_name == NULL && target_fpe_name != NULL)
9b6b54e2 1335 {
9b66ebb1
PB
1336 if (streq (target_fpe_name, "2"))
1337 target_fpu_name = "fpe2";
1338 else if (streq (target_fpe_name, "3"))
1339 target_fpu_name = "fpe3";
1340 else
1341 error ("invalid floating point emulation option: -mfpe=%s",
1342 target_fpe_name);
1343 }
1344 if (target_fpu_name != NULL)
1345 {
1346 /* The user specified a FPU. */
1347 for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1348 {
1349 if (streq (all_fpus[i].name, target_fpu_name))
1350 {
1351 arm_fpu_arch = all_fpus[i].fpu;
1352 arm_fpu_tune = arm_fpu_arch;
1353 arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1354 break;
1355 }
1356 }
1357 if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1358 error ("invalid floating point option: -mfpu=%s", target_fpu_name);
9b6b54e2
NC
1359 }
1360 else
2b835d68 1361 {
9b66ebb1 1362#ifdef FPUTYPE_DEFAULT
78011587 1363 /* Use the default if it is specified for this platform. */
9b66ebb1
PB
1364 arm_fpu_arch = FPUTYPE_DEFAULT;
1365 arm_fpu_tune = FPUTYPE_DEFAULT;
1366#else
1367 /* Pick one based on CPU type. */
78011587 1368 /* ??? Some targets assume FPA is the default.
9b66ebb1
PB
1369 if ((insn_flags & FL_VFP) != 0)
1370 arm_fpu_arch = FPUTYPE_VFP;
78011587
PB
1371 else
1372 */
1373 if (arm_arch_cirrus)
9b66ebb1
PB
1374 arm_fpu_arch = FPUTYPE_MAVERICK;
1375 else
29ad9694 1376 arm_fpu_arch = FPUTYPE_FPA_EMU2;
9b66ebb1
PB
1377#endif
1378 if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1379 arm_fpu_tune = FPUTYPE_FPA;
2b835d68 1380 else
9b66ebb1
PB
1381 arm_fpu_tune = arm_fpu_arch;
1382 arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
e6d29d15 1383 gcc_assert (arm_fp_model != ARM_FP_MODEL_UNKNOWN);
9b66ebb1
PB
1384 }
1385
1386 if (target_float_abi_name != NULL)
1387 {
1388 /* The user specified a FP ABI. */
1389 for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1390 {
1391 if (streq (all_float_abis[i].name, target_float_abi_name))
1392 {
1393 arm_float_abi = all_float_abis[i].abi_type;
1394 break;
1395 }
1396 }
1397 if (i == ARRAY_SIZE (all_float_abis))
1398 error ("invalid floating point abi: -mfloat-abi=%s",
1399 target_float_abi_name);
2b835d68 1400 }
3d8532aa
PB
1401 else
1402 arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
9b66ebb1 1403
72cdc543
PB
1404 if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1405 sorry ("-mfloat-abi=hard and VFP");
1406
87b24aaf
PB
1407 /* FPA and iWMMXt are incompatible because the insn encodings overlap.
1408 VFP and iWMMXt can theoretically coexist, but it's unlikely such silicon
1409 will ever exist. GCC makes no attempt to support this combination. */
1410 if (TARGET_IWMMXT && !TARGET_SOFT_FLOAT)
1411 sorry ("iWMMXt and hardware floating point");
1412
5b3e6663
PB
1413 /* ??? iWMMXt insn patterns need auditing for Thumb-2. */
1414 if (TARGET_THUMB2 && TARGET_IWMMXT)
1415 sorry ("Thumb-2 iWMMXt");
1416
9b66ebb1
PB
1417 /* If soft-float is specified then don't use FPU. */
1418 if (TARGET_SOFT_FLOAT)
1419 arm_fpu_arch = FPUTYPE_NONE;
f676971a 1420
f5a1b0d2
NC
1421 /* For arm2/3 there is no need to do any scheduling if there is only
1422 a floating point emulator, or we are doing software floating-point. */
9b66ebb1
PB
1423 if ((TARGET_SOFT_FLOAT
1424 || arm_fpu_tune == FPUTYPE_FPA_EMU2
1425 || arm_fpu_tune == FPUTYPE_FPA_EMU3)
ed0e6530 1426 && (tune_flags & FL_MODE32) == 0)
f5a1b0d2 1427 flag_schedule_insns = flag_schedule_insns_after_reload = 0;
f676971a 1428
d3585b76
DJ
1429 if (target_thread_switch)
1430 {
1431 if (strcmp (target_thread_switch, "soft") == 0)
1432 target_thread_pointer = TP_SOFT;
1433 else if (strcmp (target_thread_switch, "auto") == 0)
1434 target_thread_pointer = TP_AUTO;
1435 else if (strcmp (target_thread_switch, "cp15") == 0)
1436 target_thread_pointer = TP_CP15;
1437 else
1438 error ("invalid thread pointer option: -mtp=%s", target_thread_switch);
1439 }
1440
1441 /* Use the cp15 method if it is available. */
1442 if (target_thread_pointer == TP_AUTO)
1443 {
1444 if (arm_arch6k && !TARGET_THUMB)
1445 target_thread_pointer = TP_CP15;
1446 else
1447 target_thread_pointer = TP_SOFT;
1448 }
1449
5b3e6663
PB
1450 if (TARGET_HARD_TP && TARGET_THUMB1)
1451 error ("can not use -mtp=cp15 with 16-bit Thumb");
d3585b76 1452
5848830f 1453 /* Override the default structure alignment for AAPCS ABI. */
077fc835 1454 if (TARGET_AAPCS_BASED)
5848830f
PB
1455 arm_structure_size_boundary = 8;
1456
b355a481
NC
1457 if (structure_size_string != NULL)
1458 {
1459 int size = strtol (structure_size_string, NULL, 0);
5848830f
PB
1460
1461 if (size == 8 || size == 32
1462 || (ARM_DOUBLEWORD_ALIGN && size == 64))
b355a481
NC
1463 arm_structure_size_boundary = size;
1464 else
d4ee4d25 1465 warning (0, "structure size boundary can only be set to %s",
5848830f 1466 ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
b355a481 1467 }
ed0e6530 1468
9403b7f7
RS
1469 if (!TARGET_ARM && TARGET_VXWORKS_RTP && flag_pic)
1470 {
1471 error ("RTP PIC is incompatible with Thumb");
1472 flag_pic = 0;
1473 }
1474
c147eacb
PB
1475 /* If stack checking is disabled, we can use r10 as the PIC register,
1476 which keeps r9 available. The EABI specifies r9 as the PIC register. */
1477 if (flag_pic && TARGET_SINGLE_PIC_BASE)
9403b7f7
RS
1478 {
1479 if (TARGET_VXWORKS_RTP)
1480 warning (0, "RTP PIC is incompatible with -msingle-pic-base");
1481 arm_pic_register = (TARGET_APCS_STACK || TARGET_AAPCS_BASED) ? 9 : 10;
1482 }
1483
1484 if (flag_pic && TARGET_VXWORKS_RTP)
1485 arm_pic_register = 9;
c147eacb 1486
ed0e6530
PB
1487 if (arm_pic_register_string != NULL)
1488 {
5b43fed1 1489 int pic_register = decode_reg_name (arm_pic_register_string);
e26053d1 1490
5895f793 1491 if (!flag_pic)
d4ee4d25 1492 warning (0, "-mpic-register= is useless without -fpic");
ed0e6530 1493
ed0e6530 1494 /* Prevent the user from choosing an obviously stupid PIC register. */
5b43fed1
RH
1495 else if (pic_register < 0 || call_used_regs[pic_register]
1496 || pic_register == HARD_FRAME_POINTER_REGNUM
1497 || pic_register == STACK_POINTER_REGNUM
9403b7f7
RS
1498 || pic_register >= PC_REGNUM
1499 || (TARGET_VXWORKS_RTP
1500 && (unsigned int) pic_register != arm_pic_register))
c725bd79 1501 error ("unable to use '%s' for PIC register", arm_pic_register_string);
ed0e6530
PB
1502 else
1503 arm_pic_register = pic_register;
1504 }
d5b7b3ae 1505
5b3e6663 1506 /* ??? We might want scheduling for thumb2. */
d5b7b3ae
RE
1507 if (TARGET_THUMB && flag_schedule_insns)
1508 {
1509 /* Don't warn since it's on by default in -O2. */
1510 flag_schedule_insns = 0;
1511 }
1512
f5a1b0d2 1513 if (optimize_size)
be03ccc9 1514 {
577d6328 1515 arm_constant_limit = 1;
be03ccc9
NP
1516
1517 /* If optimizing for size, bump the number of instructions that we
d6b4baa4 1518 are prepared to conditionally execute (even on a StrongARM). */
be03ccc9
NP
1519 max_insns_skipped = 6;
1520 }
1521 else
1522 {
1523 /* For processors with load scheduling, it never costs more than
1524 2 cycles to load a constant, and the load scheduler may well
1525 reduce that to 1. */
2075b05d 1526 if (arm_ld_sched)
be03ccc9
NP
1527 arm_constant_limit = 1;
1528
1529 /* On XScale the longer latency of a load makes it more difficult
1530 to achieve a good schedule, so it's faster to synthesize
d6b4baa4 1531 constants that can be done in two insns. */
be03ccc9
NP
1532 if (arm_tune_xscale)
1533 arm_constant_limit = 2;
1534
1535 /* StrongARM has early execution of branches, so a sequence
1536 that is worth skipping is shorter. */
abac3b49 1537 if (arm_tune_strongarm)
be03ccc9
NP
1538 max_insns_skipped = 3;
1539 }
92a432f4
RE
1540
1541 /* Register global variables with the garbage collector. */
1542 arm_add_gc_roots ();
1543}
1544
1545static void
e32bac5b 1546arm_add_gc_roots (void)
92a432f4 1547{
c7319d87
RE
1548 gcc_obstack_init(&minipool_obstack);
1549 minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
2b835d68 1550}
cce8749e 1551\f
6d3d9133
NC
1552/* A table of known ARM exception types.
1553 For use with the interrupt function attribute. */
1554
1555typedef struct
1556{
8b60264b
KG
1557 const char *const arg;
1558 const unsigned long return_value;
6d3d9133
NC
1559}
1560isr_attribute_arg;
1561
8b60264b 1562static const isr_attribute_arg isr_attribute_args [] =
6d3d9133
NC
1563{
1564 { "IRQ", ARM_FT_ISR },
1565 { "irq", ARM_FT_ISR },
1566 { "FIQ", ARM_FT_FIQ },
1567 { "fiq", ARM_FT_FIQ },
1568 { "ABORT", ARM_FT_ISR },
1569 { "abort", ARM_FT_ISR },
1570 { "ABORT", ARM_FT_ISR },
1571 { "abort", ARM_FT_ISR },
1572 { "UNDEF", ARM_FT_EXCEPTION },
1573 { "undef", ARM_FT_EXCEPTION },
1574 { "SWI", ARM_FT_EXCEPTION },
1575 { "swi", ARM_FT_EXCEPTION },
1576 { NULL, ARM_FT_NORMAL }
1577};
1578
1579/* Returns the (interrupt) function type of the current
1580 function, or ARM_FT_UNKNOWN if the type cannot be determined. */
1581
1582static unsigned long
e32bac5b 1583arm_isr_value (tree argument)
6d3d9133 1584{
8b60264b 1585 const isr_attribute_arg * ptr;
1d6e90ac 1586 const char * arg;
6d3d9133 1587
5b3e6663
PB
1588 if (!arm_arch_notm)
1589 return ARM_FT_NORMAL | ARM_FT_STACKALIGN;
1590
6d3d9133
NC
1591 /* No argument - default to IRQ. */
1592 if (argument == NULL_TREE)
1593 return ARM_FT_ISR;
1594
1595 /* Get the value of the argument. */
1596 if (TREE_VALUE (argument) == NULL_TREE
1597 || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1598 return ARM_FT_UNKNOWN;
1599
1600 arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1601
1602 /* Check it against the list of known arguments. */
5a9335ef 1603 for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1d6e90ac
NC
1604 if (streq (arg, ptr->arg))
1605 return ptr->return_value;
6d3d9133 1606
05713b80 1607 /* An unrecognized interrupt type. */
6d3d9133
NC
1608 return ARM_FT_UNKNOWN;
1609}
1610
1611/* Computes the type of the current function. */
1612
1613static unsigned long
e32bac5b 1614arm_compute_func_type (void)
6d3d9133
NC
1615{
1616 unsigned long type = ARM_FT_UNKNOWN;
1617 tree a;
1618 tree attr;
f676971a 1619
e6d29d15 1620 gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
6d3d9133
NC
1621
1622 /* Decide if the current function is volatile. Such functions
1623 never return, and many memory cycles can be saved by not storing
1624 register values that will never be needed again. This optimization
1625 was added to speed up context switching in a kernel application. */
1626 if (optimize > 0
cf1955dc
PB
1627 && (TREE_NOTHROW (current_function_decl)
1628 || !(flag_unwind_tables
1629 || (flag_exceptions && !USING_SJLJ_EXCEPTIONS)))
6d3d9133
NC
1630 && TREE_THIS_VOLATILE (current_function_decl))
1631 type |= ARM_FT_VOLATILE;
f676971a 1632
6de9cd9a 1633 if (cfun->static_chain_decl != NULL)
6d3d9133
NC
1634 type |= ARM_FT_NESTED;
1635
91d231cb 1636 attr = DECL_ATTRIBUTES (current_function_decl);
f676971a 1637
6d3d9133
NC
1638 a = lookup_attribute ("naked", attr);
1639 if (a != NULL_TREE)
1640 type |= ARM_FT_NAKED;
1641
c9ca9b88
PB
1642 a = lookup_attribute ("isr", attr);
1643 if (a == NULL_TREE)
1644 a = lookup_attribute ("interrupt", attr);
f676971a 1645
c9ca9b88
PB
1646 if (a == NULL_TREE)
1647 type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
6d3d9133 1648 else
c9ca9b88 1649 type |= arm_isr_value (TREE_VALUE (a));
f676971a 1650
6d3d9133
NC
1651 return type;
1652}
1653
1654/* Returns the type of the current function. */
1655
1656unsigned long
e32bac5b 1657arm_current_func_type (void)
6d3d9133
NC
1658{
1659 if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1660 cfun->machine->func_type = arm_compute_func_type ();
1661
1662 return cfun->machine->func_type;
1663}
007e61c2
PB
1664
1665bool
1666arm_allocate_stack_slots_for_args (void)
1667{
1668 /* Naked functions should not allocate stack slots for arguments. */
1669 return !IS_NAKED (arm_current_func_type ());
1670}
1671
6d3d9133 1672\f
f676971a 1673/* Return 1 if it is possible to return using a single instruction.
a72d4945
RE
1674 If SIBLING is non-null, this is a test for a return before a sibling
1675 call. SIBLING is the call insn, so we can examine its register usage. */
6d3d9133 1676
ff9940b0 1677int
a72d4945 1678use_return_insn (int iscond, rtx sibling)
ff9940b0
RE
1679{
1680 int regno;
9b598fa0 1681 unsigned int func_type;
d5db54a1 1682 unsigned long saved_int_regs;
a72d4945 1683 unsigned HOST_WIDE_INT stack_adjust;
5848830f 1684 arm_stack_offsets *offsets;
ff9940b0 1685
d5b7b3ae 1686 /* Never use a return instruction before reload has run. */
6d3d9133
NC
1687 if (!reload_completed)
1688 return 0;
efc2515b 1689
9b598fa0
RE
1690 func_type = arm_current_func_type ();
1691
5b3e6663 1692 /* Naked, volatile and stack alignment functions need special
3a7731fd 1693 consideration. */
5b3e6663 1694 if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED | ARM_FT_STACKALIGN))
6d3d9133 1695 return 0;
06bea5aa 1696
a15908a4
PB
1697 /* So do interrupt functions that use the frame pointer and Thumb
1698 interrupt functions. */
1699 if (IS_INTERRUPT (func_type) && (frame_pointer_needed || TARGET_THUMB))
06bea5aa 1700 return 0;
a72d4945 1701
5848830f
PB
1702 offsets = arm_get_frame_offsets ();
1703 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
a72d4945 1704
6d3d9133 1705 /* As do variadic functions. */
38173d38 1706 if (crtl->args.pretend_args_size
3cb66fd7 1707 || cfun->machine->uses_anonymous_args
699a4925 1708 /* Or if the function calls __builtin_eh_return () */
e3b5732b 1709 || crtl->calls_eh_return
699a4925 1710 /* Or if the function calls alloca */
e3b5732b 1711 || cfun->calls_alloca
a72d4945
RE
1712 /* Or if there is a stack adjustment. However, if the stack pointer
1713 is saved on the stack, we can use a pre-incrementing stack load. */
ec6237e4
PB
1714 || !(stack_adjust == 0 || (TARGET_APCS_FRAME && frame_pointer_needed
1715 && stack_adjust == 4)))
ff9940b0
RE
1716 return 0;
1717
954954d1 1718 saved_int_regs = offsets->saved_regs_mask;
d5db54a1 1719
a72d4945
RE
1720 /* Unfortunately, the insn
1721
1722 ldmib sp, {..., sp, ...}
1723
1724 triggers a bug on most SA-110 based devices, such that the stack
1725 pointer won't be correctly restored if the instruction takes a
839a4992 1726 page fault. We work around this problem by popping r3 along with
a72d4945 1727 the other registers, since that is never slower than executing
f676971a 1728 another instruction.
a72d4945
RE
1729
1730 We test for !arm_arch5 here, because code for any architecture
1731 less than this could potentially be run on one of the buggy
1732 chips. */
5b3e6663 1733 if (stack_adjust == 4 && !arm_arch5 && TARGET_ARM)
a72d4945
RE
1734 {
1735 /* Validate that r3 is a call-clobbered register (always true in
d6b4baa4 1736 the default abi) ... */
a72d4945
RE
1737 if (!call_used_regs[3])
1738 return 0;
1739
4f5dfed0
JC
1740 /* ... that it isn't being used for a return value ... */
1741 if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
1742 return 0;
1743
1744 /* ... or for a tail-call argument ... */
a72d4945
RE
1745 if (sibling)
1746 {
e6d29d15 1747 gcc_assert (GET_CODE (sibling) == CALL_INSN);
a72d4945
RE
1748
1749 if (find_regno_fusage (sibling, USE, 3))
1750 return 0;
1751 }
1752
1753 /* ... and that there are no call-saved registers in r0-r2
1754 (always true in the default ABI). */
1755 if (saved_int_regs & 0x7)
1756 return 0;
1757 }
1758
b111229a 1759 /* Can't be done if interworking with Thumb, and any registers have been
d5db54a1 1760 stacked. */
a15908a4 1761 if (TARGET_INTERWORK && saved_int_regs != 0 && !IS_INTERRUPT(func_type))
b36ba79f 1762 return 0;
d5db54a1
RE
1763
1764 /* On StrongARM, conditional returns are expensive if they aren't
1765 taken and multiple registers have been stacked. */
abac3b49 1766 if (iscond && arm_tune_strongarm)
6ed30148 1767 {
f676971a 1768 /* Conditional return when just the LR is stored is a simple
d5db54a1
RE
1769 conditional-load instruction, that's not expensive. */
1770 if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1771 return 0;
6ed30148 1772
020a4035
RE
1773 if (flag_pic
1774 && arm_pic_register != INVALID_REGNUM
6fb5fa3c 1775 && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
b111229a 1776 return 0;
6ed30148 1777 }
d5db54a1
RE
1778
1779 /* If there are saved registers but the LR isn't saved, then we need
1780 two instructions for the return. */
1781 if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1782 return 0;
1783
3b684012 1784 /* Can't be done if any of the FPA regs are pushed,
6d3d9133 1785 since this also requires an insn. */
9b66ebb1
PB
1786 if (TARGET_HARD_FLOAT && TARGET_FPA)
1787 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
6fb5fa3c 1788 if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
9b66ebb1
PB
1789 return 0;
1790
1791 /* Likewise VFP regs. */
1792 if (TARGET_HARD_FLOAT && TARGET_VFP)
1793 for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
6fb5fa3c 1794 if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
d5b7b3ae 1795 return 0;
ff9940b0 1796
5a9335ef
NC
1797 if (TARGET_REALLY_IWMMXT)
1798 for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
6fb5fa3c 1799 if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
5a9335ef
NC
1800 return 0;
1801
ff9940b0
RE
1802 return 1;
1803}
1804
cce8749e
CH
1805/* Return TRUE if int I is a valid immediate ARM constant. */
1806
1807int
e32bac5b 1808const_ok_for_arm (HOST_WIDE_INT i)
cce8749e 1809{
4642ccb1 1810 int lowbit;
e0b92319 1811
f676971a 1812 /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
56636818 1813 be all zero, or all one. */
30cf4896
KG
1814 if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1815 && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1816 != ((~(unsigned HOST_WIDE_INT) 0)
1817 & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
56636818 1818 return FALSE;
f676971a 1819
4642ccb1 1820 i &= (unsigned HOST_WIDE_INT) 0xffffffff;
e0b92319 1821
4642ccb1
RE
1822 /* Fast return for 0 and small values. We must do this for zero, since
1823 the code below can't handle that one case. */
1824 if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
e2c671ba
RE
1825 return TRUE;
1826
5b3e6663
PB
1827 /* Get the number of trailing zeros. */
1828 lowbit = ffs((int) i) - 1;
1829
1830 /* Only even shifts are allowed in ARM mode so round down to the
1831 nearest even number. */
1832 if (TARGET_ARM)
1833 lowbit &= ~1;
4642ccb1
RE
1834
1835 if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
1836 return TRUE;
5b3e6663
PB
1837
1838 if (TARGET_ARM)
1839 {
1840 /* Allow rotated constants in ARM mode. */
1841 if (lowbit <= 4
4642ccb1
RE
1842 && ((i & ~0xc000003f) == 0
1843 || (i & ~0xf000000f) == 0
1844 || (i & ~0xfc000003) == 0))
5b3e6663
PB
1845 return TRUE;
1846 }
1847 else
1848 {
1849 HOST_WIDE_INT v;
1850
1851 /* Allow repeated pattern. */
1852 v = i & 0xff;
1853 v |= v << 16;
1854 if (i == v || i == (v | (v << 8)))
1855 return TRUE;
1856 }
cce8749e 1857
f3bb6135
RE
1858 return FALSE;
1859}
cce8749e 1860
6354dc9b 1861/* Return true if I is a valid constant for the operation CODE. */
74bbc178 1862static int
e32bac5b 1863const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
e2c671ba
RE
1864{
1865 if (const_ok_for_arm (i))
1866 return 1;
1867
1868 switch (code)
1869 {
1870 case PLUS:
1871 return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1872
1873 case MINUS: /* Should only occur with (MINUS I reg) => rsb */
1874 case XOR:
1875 case IOR:
1876 return 0;
1877
1878 case AND:
1879 return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1880
1881 default:
e6d29d15 1882 gcc_unreachable ();
e2c671ba
RE
1883 }
1884}
1885
1886/* Emit a sequence of insns to handle a large constant.
1887 CODE is the code of the operation required, it can be any of SET, PLUS,
1888 IOR, AND, XOR, MINUS;
1889 MODE is the mode in which the operation is being performed;
1890 VAL is the integer to operate on;
1891 SOURCE is the other operand (a register, or a null-pointer for SET);
1892 SUBTARGETS means it is safe to create scratch registers if that will
2b835d68
RE
1893 either produce a simpler sequence, or we will want to cse the values.
1894 Return value is the number of insns emitted. */
e2c671ba 1895
5b3e6663 1896/* ??? Tweak this for thumb2. */
e2c671ba 1897int
a406f566 1898arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
e32bac5b 1899 HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
2b835d68 1900{
a406f566
MM
1901 rtx cond;
1902
1903 if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1904 cond = COND_EXEC_TEST (PATTERN (insn));
1905 else
1906 cond = NULL_RTX;
1907
2b835d68
RE
1908 if (subtargets || code == SET
1909 || (GET_CODE (target) == REG && GET_CODE (source) == REG
1910 && REGNO (target) != REGNO (source)))
1911 {
4b632bf1 1912 /* After arm_reorg has been called, we can't fix up expensive
05713b80 1913 constants by pushing them into memory so we must synthesize
4b632bf1
RE
1914 them in-line, regardless of the cost. This is only likely to
1915 be more costly on chips that have load delay slots and we are
1916 compiling without running the scheduler (so no splitting
aec3cfba
NC
1917 occurred before the final instruction emission).
1918
1919 Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
aec3cfba 1920 */
5895f793 1921 if (!after_arm_reorg
a406f566 1922 && !cond
f676971a 1923 && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
a406f566 1924 1, 0)
4b632bf1 1925 > arm_constant_limit + (code != SET)))
2b835d68
RE
1926 {
1927 if (code == SET)
1928 {
1929 /* Currently SET is the only monadic value for CODE, all
1930 the rest are diadic. */
d66437c5 1931 emit_set_insn (target, GEN_INT (val));
2b835d68
RE
1932 return 1;
1933 }
1934 else
1935 {
1936 rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1937
d66437c5 1938 emit_set_insn (temp, GEN_INT (val));
2b835d68
RE
1939 /* For MINUS, the value is subtracted from, since we never
1940 have subtraction of a constant. */
1941 if (code == MINUS)
d66437c5 1942 emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
2b835d68 1943 else
d66437c5
RE
1944 emit_set_insn (target,
1945 gen_rtx_fmt_ee (code, mode, source, temp));
2b835d68
RE
1946 return 2;
1947 }
1948 }
1949 }
1950
f676971a 1951 return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
a406f566 1952 1);
2b835d68
RE
1953}
1954
5b3e6663
PB
1955/* Return the number of ARM instructions required to synthesize the given
1956 constant. */
ceebdb09 1957static int
e32bac5b 1958count_insns_for_constant (HOST_WIDE_INT remainder, int i)
ceebdb09
PB
1959{
1960 HOST_WIDE_INT temp1;
1961 int num_insns = 0;
1962 do
1963 {
1964 int end;
f676971a 1965
ceebdb09
PB
1966 if (i <= 0)
1967 i += 32;
1968 if (remainder & (3 << (i - 2)))
1969 {
1970 end = i - 8;
1971 if (end < 0)
1972 end += 32;
1973 temp1 = remainder & ((0x0ff << end)
1974 | ((i < end) ? (0xff >> (32 - end)) : 0));
1975 remainder &= ~temp1;
1976 num_insns++;
1977 i -= 6;
1978 }
1979 i -= 2;
1980 } while (remainder);
1981 return num_insns;
1982}
1983
a406f566
MM
1984/* Emit an instruction with the indicated PATTERN. If COND is
1985 non-NULL, conditionalize the execution of the instruction on COND
1986 being true. */
1987
1988static void
1989emit_constant_insn (rtx cond, rtx pattern)
1990{
1991 if (cond)
1992 pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1993 emit_insn (pattern);
1994}
1995
2b835d68
RE
1996/* As above, but extra parameter GENERATE which, if clear, suppresses
1997 RTL generation. */
5b3e6663 1998/* ??? This needs more work for thumb2. */
1d6e90ac 1999
d5b7b3ae 2000static int
a406f566 2001arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
e32bac5b
RE
2002 HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
2003 int generate)
e2c671ba 2004{
e2c671ba
RE
2005 int can_invert = 0;
2006 int can_negate = 0;
2007 int can_negate_initial = 0;
2008 int can_shift = 0;
2009 int i;
2010 int num_bits_set = 0;
2011 int set_sign_bit_copies = 0;
2012 int clear_sign_bit_copies = 0;
2013 int clear_zero_bit_copies = 0;
2014 int set_zero_bit_copies = 0;
2015 int insns = 0;
e2c671ba 2016 unsigned HOST_WIDE_INT temp1, temp2;
30cf4896 2017 unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
e2c671ba 2018
d5b7b3ae 2019 /* Find out which operations are safe for a given CODE. Also do a quick
e2c671ba
RE
2020 check for degenerate cases; these can occur when DImode operations
2021 are split. */
2022 switch (code)
2023 {
2024 case SET:
2025 can_invert = 1;
2026 can_shift = 1;
2027 can_negate = 1;
2028 break;
2029
2030 case PLUS:
2031 can_negate = 1;
2032 can_negate_initial = 1;
2033 break;
2034
2035 case IOR:
30cf4896 2036 if (remainder == 0xffffffff)
e2c671ba 2037 {
2b835d68 2038 if (generate)
a406f566
MM
2039 emit_constant_insn (cond,
2040 gen_rtx_SET (VOIDmode, target,
2041 GEN_INT (ARM_SIGN_EXTEND (val))));
e2c671ba
RE
2042 return 1;
2043 }
2044 if (remainder == 0)
2045 {
2046 if (reload_completed && rtx_equal_p (target, source))
2047 return 0;
2b835d68 2048 if (generate)
a406f566
MM
2049 emit_constant_insn (cond,
2050 gen_rtx_SET (VOIDmode, target, source));
e2c671ba
RE
2051 return 1;
2052 }
2053 break;
2054
2055 case AND:
2056 if (remainder == 0)
2057 {
2b835d68 2058 if (generate)
a406f566
MM
2059 emit_constant_insn (cond,
2060 gen_rtx_SET (VOIDmode, target, const0_rtx));
e2c671ba
RE
2061 return 1;
2062 }
30cf4896 2063 if (remainder == 0xffffffff)
e2c671ba
RE
2064 {
2065 if (reload_completed && rtx_equal_p (target, source))
2066 return 0;
2b835d68 2067 if (generate)
a406f566
MM
2068 emit_constant_insn (cond,
2069 gen_rtx_SET (VOIDmode, target, source));
e2c671ba
RE
2070 return 1;
2071 }
2072 can_invert = 1;
2073 break;
2074
2075 case XOR:
2076 if (remainder == 0)
2077 {
2078 if (reload_completed && rtx_equal_p (target, source))
2079 return 0;
2b835d68 2080 if (generate)
a406f566
MM
2081 emit_constant_insn (cond,
2082 gen_rtx_SET (VOIDmode, target, source));
e2c671ba
RE
2083 return 1;
2084 }
e0b92319 2085
e6d29d15
NS
2086 /* We don't know how to handle other cases yet. */
2087 gcc_assert (remainder == 0xffffffff);
e0b92319 2088
e6d29d15
NS
2089 if (generate)
2090 emit_constant_insn (cond,
2091 gen_rtx_SET (VOIDmode, target,
2092 gen_rtx_NOT (mode, source)));
2093 return 1;
e2c671ba
RE
2094
2095 case MINUS:
2096 /* We treat MINUS as (val - source), since (source - val) is always
2097 passed as (source + (-val)). */
2098 if (remainder == 0)
2099 {
2b835d68 2100 if (generate)
a406f566
MM
2101 emit_constant_insn (cond,
2102 gen_rtx_SET (VOIDmode, target,
2103 gen_rtx_NEG (mode, source)));
e2c671ba
RE
2104 return 1;
2105 }
2106 if (const_ok_for_arm (val))
2107 {
2b835d68 2108 if (generate)
a406f566 2109 emit_constant_insn (cond,
f676971a 2110 gen_rtx_SET (VOIDmode, target,
a406f566
MM
2111 gen_rtx_MINUS (mode, GEN_INT (val),
2112 source)));
e2c671ba
RE
2113 return 1;
2114 }
2115 can_negate = 1;
2116
2117 break;
2118
2119 default:
e6d29d15 2120 gcc_unreachable ();
e2c671ba
RE
2121 }
2122
6354dc9b 2123 /* If we can do it in one insn get out quickly. */
e2c671ba
RE
2124 if (const_ok_for_arm (val)
2125 || (can_negate_initial && const_ok_for_arm (-val))
2126 || (can_invert && const_ok_for_arm (~val)))
2127 {
2b835d68 2128 if (generate)
a406f566
MM
2129 emit_constant_insn (cond,
2130 gen_rtx_SET (VOIDmode, target,
f676971a 2131 (source
a406f566
MM
2132 ? gen_rtx_fmt_ee (code, mode, source,
2133 GEN_INT (val))
2134 : GEN_INT (val))));
e2c671ba
RE
2135 return 1;
2136 }
2137
e2c671ba 2138 /* Calculate a few attributes that may be useful for specific
6354dc9b 2139 optimizations. */
e2c671ba
RE
2140 for (i = 31; i >= 0; i--)
2141 {
2142 if ((remainder & (1 << i)) == 0)
2143 clear_sign_bit_copies++;
2144 else
2145 break;
2146 }
2147
2148 for (i = 31; i >= 0; i--)
2149 {
2150 if ((remainder & (1 << i)) != 0)
2151 set_sign_bit_copies++;
2152 else
2153 break;
2154 }
2155
2156 for (i = 0; i <= 31; i++)
2157 {
2158 if ((remainder & (1 << i)) == 0)
2159 clear_zero_bit_copies++;
2160 else
2161 break;
2162 }
2163
2164 for (i = 0; i <= 31; i++)
2165 {
2166 if ((remainder & (1 << i)) != 0)
2167 set_zero_bit_copies++;
2168 else
2169 break;
2170 }
2171
2172 switch (code)
2173 {
2174 case SET:
5b3e6663
PB
2175 /* See if we can use movw. */
2176 if (arm_arch_thumb2 && (remainder & 0xffff0000) == 0)
2177 {
2178 if (generate)
2179 emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
2180 GEN_INT (val)));
2181 return 1;
2182 }
2183
e2c671ba
RE
2184 /* See if we can do this by sign_extending a constant that is known
2185 to be negative. This is a good, way of doing it, since the shift
2186 may well merge into a subsequent insn. */
2187 if (set_sign_bit_copies > 1)
2188 {
2189 if (const_ok_for_arm
f676971a 2190 (temp1 = ARM_SIGN_EXTEND (remainder
e2c671ba
RE
2191 << (set_sign_bit_copies - 1))))
2192 {
2b835d68
RE
2193 if (generate)
2194 {
d499463f 2195 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
a406f566 2196 emit_constant_insn (cond,
f676971a 2197 gen_rtx_SET (VOIDmode, new_src,
a406f566
MM
2198 GEN_INT (temp1)));
2199 emit_constant_insn (cond,
f676971a 2200 gen_ashrsi3 (target, new_src,
a406f566 2201 GEN_INT (set_sign_bit_copies - 1)));
2b835d68 2202 }
e2c671ba
RE
2203 return 2;
2204 }
2205 /* For an inverted constant, we will need to set the low bits,
2206 these will be shifted out of harm's way. */
2207 temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
2208 if (const_ok_for_arm (~temp1))
2209 {
2b835d68
RE
2210 if (generate)
2211 {
d499463f 2212 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
a406f566
MM
2213 emit_constant_insn (cond,
2214 gen_rtx_SET (VOIDmode, new_src,
2215 GEN_INT (temp1)));
2216 emit_constant_insn (cond,
f676971a 2217 gen_ashrsi3 (target, new_src,
a406f566 2218 GEN_INT (set_sign_bit_copies - 1)));
2b835d68 2219 }
e2c671ba
RE
2220 return 2;
2221 }
2222 }
2223
c87e6352
RE
2224 /* See if we can calculate the value as the difference between two
2225 valid immediates. */
2226 if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
2227 {
2228 int topshift = clear_sign_bit_copies & ~1;
2229
fa2c88a0
RE
2230 temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
2231 & (0xff000000 >> topshift));
c87e6352
RE
2232
2233 /* If temp1 is zero, then that means the 9 most significant
2234 bits of remainder were 1 and we've caused it to overflow.
2235 When topshift is 0 we don't need to do anything since we
2236 can borrow from 'bit 32'. */
2237 if (temp1 == 0 && topshift != 0)
2238 temp1 = 0x80000000 >> (topshift - 1);
2239
fa2c88a0 2240 temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
e0b92319 2241
c87e6352
RE
2242 if (const_ok_for_arm (temp2))
2243 {
2244 if (generate)
2245 {
2246 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2247 emit_constant_insn (cond,
2248 gen_rtx_SET (VOIDmode, new_src,
2249 GEN_INT (temp1)));
2250 emit_constant_insn (cond,
2251 gen_addsi3 (target, new_src,
2252 GEN_INT (-temp2)));
2253 }
2254
2255 return 2;
2256 }
2257 }
2258
e2c671ba
RE
2259 /* See if we can generate this by setting the bottom (or the top)
2260 16 bits, and then shifting these into the other half of the
2261 word. We only look for the simplest cases, to do more would cost
2262 too much. Be careful, however, not to generate this when the
2263 alternative would take fewer insns. */
30cf4896 2264 if (val & 0xffff0000)
e2c671ba 2265 {
30cf4896 2266 temp1 = remainder & 0xffff0000;
e2c671ba
RE
2267 temp2 = remainder & 0x0000ffff;
2268
6354dc9b 2269 /* Overlaps outside this range are best done using other methods. */
e2c671ba
RE
2270 for (i = 9; i < 24; i++)
2271 {
30cf4896 2272 if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
5895f793 2273 && !const_ok_for_arm (temp2))
e2c671ba 2274 {
d499463f
RE
2275 rtx new_src = (subtargets
2276 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2277 : target);
a406f566 2278 insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2b835d68 2279 source, subtargets, generate);
e2c671ba 2280 source = new_src;
2b835d68 2281 if (generate)
f676971a 2282 emit_constant_insn
a406f566
MM
2283 (cond,
2284 gen_rtx_SET
2285 (VOIDmode, target,
2286 gen_rtx_IOR (mode,
2287 gen_rtx_ASHIFT (mode, source,
2288 GEN_INT (i)),
2289 source)));
e2c671ba
RE
2290 return insns + 1;
2291 }
2292 }
2293
6354dc9b 2294 /* Don't duplicate cases already considered. */
e2c671ba
RE
2295 for (i = 17; i < 24; i++)
2296 {
2297 if (((temp1 | (temp1 >> i)) == remainder)
5895f793 2298 && !const_ok_for_arm (temp1))
e2c671ba 2299 {
d499463f
RE
2300 rtx new_src = (subtargets
2301 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2302 : target);
a406f566 2303 insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2b835d68 2304 source, subtargets, generate);
e2c671ba 2305 source = new_src;
2b835d68 2306 if (generate)
a406f566
MM
2307 emit_constant_insn
2308 (cond,
2309 gen_rtx_SET (VOIDmode, target,
43cffd11
RE
2310 gen_rtx_IOR
2311 (mode,
2312 gen_rtx_LSHIFTRT (mode, source,
2313 GEN_INT (i)),
2314 source)));
e2c671ba
RE
2315 return insns + 1;
2316 }
2317 }
2318 }
2319 break;
2320
2321 case IOR:
2322 case XOR:
7b64da89
RE
2323 /* If we have IOR or XOR, and the constant can be loaded in a
2324 single instruction, and we can find a temporary to put it in,
e2c671ba
RE
2325 then this can be done in two instructions instead of 3-4. */
2326 if (subtargets
d499463f 2327 /* TARGET can't be NULL if SUBTARGETS is 0 */
5895f793 2328 || (reload_completed && !reg_mentioned_p (target, source)))
e2c671ba 2329 {
5895f793 2330 if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
e2c671ba 2331 {
2b835d68
RE
2332 if (generate)
2333 {
2334 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
e2c671ba 2335
a406f566 2336 emit_constant_insn (cond,
f676971a 2337 gen_rtx_SET (VOIDmode, sub,
a406f566
MM
2338 GEN_INT (val)));
2339 emit_constant_insn (cond,
f676971a 2340 gen_rtx_SET (VOIDmode, target,
a406f566
MM
2341 gen_rtx_fmt_ee (code, mode,
2342 source, sub)));
2b835d68 2343 }
e2c671ba
RE
2344 return 2;
2345 }
2346 }
2347
2348 if (code == XOR)
2349 break;
2350
2351 if (set_sign_bit_copies > 8
2352 && (val & (-1 << (32 - set_sign_bit_copies))) == val)
2353 {
2b835d68
RE
2354 if (generate)
2355 {
2356 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2357 rtx shift = GEN_INT (set_sign_bit_copies);
2358
f676971a 2359 emit_constant_insn
a406f566
MM
2360 (cond,
2361 gen_rtx_SET (VOIDmode, sub,
f676971a 2362 gen_rtx_NOT (mode,
a406f566 2363 gen_rtx_ASHIFT (mode,
f676971a 2364 source,
a406f566 2365 shift))));
f676971a 2366 emit_constant_insn
a406f566
MM
2367 (cond,
2368 gen_rtx_SET (VOIDmode, target,
2369 gen_rtx_NOT (mode,
2370 gen_rtx_LSHIFTRT (mode, sub,
2371 shift))));
2b835d68 2372 }
e2c671ba
RE
2373 return 2;
2374 }
2375
2376 if (set_zero_bit_copies > 8
2377 && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2378 {
2b835d68
RE
2379 if (generate)
2380 {
2381 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2382 rtx shift = GEN_INT (set_zero_bit_copies);
2383
a406f566
MM
2384 emit_constant_insn
2385 (cond,
2386 gen_rtx_SET (VOIDmode, sub,
2387 gen_rtx_NOT (mode,
2388 gen_rtx_LSHIFTRT (mode,
2389 source,
2390 shift))));
f676971a 2391 emit_constant_insn
a406f566
MM
2392 (cond,
2393 gen_rtx_SET (VOIDmode, target,
2394 gen_rtx_NOT (mode,
2395 gen_rtx_ASHIFT (mode, sub,
2396 shift))));
2b835d68 2397 }
e2c671ba
RE
2398 return 2;
2399 }
2400
5895f793 2401 if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
e2c671ba 2402 {
2b835d68
RE
2403 if (generate)
2404 {
2405 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
a406f566
MM
2406 emit_constant_insn (cond,
2407 gen_rtx_SET (VOIDmode, sub,
2408 gen_rtx_NOT (mode, source)));
2b835d68
RE
2409 source = sub;
2410 if (subtargets)
2411 sub = gen_reg_rtx (mode);
a406f566
MM
2412 emit_constant_insn (cond,
2413 gen_rtx_SET (VOIDmode, sub,
f676971a 2414 gen_rtx_AND (mode, source,
a406f566
MM
2415 GEN_INT (temp1))));
2416 emit_constant_insn (cond,
2417 gen_rtx_SET (VOIDmode, target,
2418 gen_rtx_NOT (mode, sub)));
2b835d68 2419 }
e2c671ba
RE
2420 return 3;
2421 }
2422 break;
2423
2424 case AND:
2425 /* See if two shifts will do 2 or more insn's worth of work. */
2426 if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
2427 {
30cf4896 2428 HOST_WIDE_INT shift_mask = ((0xffffffff
e2c671ba 2429 << (32 - clear_sign_bit_copies))
30cf4896 2430 & 0xffffffff);
e2c671ba 2431
30cf4896 2432 if ((remainder | shift_mask) != 0xffffffff)
e2c671ba 2433 {
2b835d68
RE
2434 if (generate)
2435 {
d499463f 2436 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
f676971a 2437 insns = arm_gen_constant (AND, mode, cond,
a406f566 2438 remainder | shift_mask,
d499463f
RE
2439 new_src, source, subtargets, 1);
2440 source = new_src;
2b835d68
RE
2441 }
2442 else
d499463f
RE
2443 {
2444 rtx targ = subtargets ? NULL_RTX : target;
a406f566
MM
2445 insns = arm_gen_constant (AND, mode, cond,
2446 remainder | shift_mask,
d499463f
RE
2447 targ, source, subtargets, 0);
2448 }
2b835d68
RE
2449 }
2450
2451 if (generate)
2452 {
d499463f
RE
2453 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2454 rtx shift = GEN_INT (clear_sign_bit_copies);
2455
2456 emit_insn (gen_ashlsi3 (new_src, source, shift));
2457 emit_insn (gen_lshrsi3 (target, new_src, shift));
e2c671ba
RE
2458 }
2459
e2c671ba
RE
2460 return insns + 2;
2461 }
2462
2463 if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2464 {
2465 HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
f676971a 2466
30cf4896 2467 if ((remainder | shift_mask) != 0xffffffff)
e2c671ba 2468 {
2b835d68
RE
2469 if (generate)
2470 {
d499463f
RE
2471 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2472
a406f566
MM
2473 insns = arm_gen_constant (AND, mode, cond,
2474 remainder | shift_mask,
d499463f
RE
2475 new_src, source, subtargets, 1);
2476 source = new_src;
2b835d68
RE
2477 }
2478 else
d499463f
RE
2479 {
2480 rtx targ = subtargets ? NULL_RTX : target;
2481
a406f566
MM
2482 insns = arm_gen_constant (AND, mode, cond,
2483 remainder | shift_mask,
d499463f
RE
2484 targ, source, subtargets, 0);
2485 }
2b835d68
RE
2486 }
2487
2488 if (generate)
2489 {
d499463f
RE
2490 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2491 rtx shift = GEN_INT (clear_zero_bit_copies);
2492
2493 emit_insn (gen_lshrsi3 (new_src, source, shift));
2494 emit_insn (gen_ashlsi3 (target, new_src, shift));
e2c671ba
RE
2495 }
2496
e2c671ba
RE
2497 return insns + 2;
2498 }
2499
2500 break;
2501
2502 default:
2503 break;
2504 }
2505
2506 for (i = 0; i < 32; i++)
2507 if (remainder & (1 << i))
2508 num_bits_set++;
2509
2510 if (code == AND || (can_invert && num_bits_set > 16))
30cf4896 2511 remainder = (~remainder) & 0xffffffff;
e2c671ba 2512 else if (code == PLUS && num_bits_set > 16)
30cf4896 2513 remainder = (-remainder) & 0xffffffff;
e2c671ba
RE
2514 else
2515 {
2516 can_invert = 0;
2517 can_negate = 0;
2518 }
2519
2520 /* Now try and find a way of doing the job in either two or three
2521 instructions.
2522 We start by looking for the largest block of zeros that are aligned on
2523 a 2-bit boundary, we then fill up the temps, wrapping around to the
2524 top of the word when we drop off the bottom.
5b3e6663
PB
2525 In the worst case this code should produce no more than four insns.
2526 Thumb-2 constants are shifted, not rotated, so the MSB is always the
2527 best place to start. */
2528
2529 /* ??? Use thumb2 replicated constants when the high and low halfwords are
2530 the same. */
e2c671ba
RE
2531 {
2532 int best_start = 0;
5b3e6663 2533 if (!TARGET_THUMB2)
e2c671ba 2534 {
5b3e6663 2535 int best_consecutive_zeros = 0;
e2c671ba 2536
5b3e6663 2537 for (i = 0; i < 32; i += 2)
e2c671ba 2538 {
5b3e6663
PB
2539 int consecutive_zeros = 0;
2540
2541 if (!(remainder & (3 << i)))
e2c671ba 2542 {
5b3e6663
PB
2543 while ((i < 32) && !(remainder & (3 << i)))
2544 {
2545 consecutive_zeros += 2;
2546 i += 2;
2547 }
2548 if (consecutive_zeros > best_consecutive_zeros)
2549 {
2550 best_consecutive_zeros = consecutive_zeros;
2551 best_start = i - consecutive_zeros;
2552 }
2553 i -= 2;
e2c671ba 2554 }
e2c671ba 2555 }
ceebdb09 2556
5b3e6663
PB
2557 /* So long as it won't require any more insns to do so, it's
2558 desirable to emit a small constant (in bits 0...9) in the last
2559 insn. This way there is more chance that it can be combined with
2560 a later addressing insn to form a pre-indexed load or store
2561 operation. Consider:
2562
2563 *((volatile int *)0xe0000100) = 1;
2564 *((volatile int *)0xe0000110) = 2;
2565
2566 We want this to wind up as:
2567
2568 mov rA, #0xe0000000
2569 mov rB, #1
2570 str rB, [rA, #0x100]
2571 mov rB, #2
2572 str rB, [rA, #0x110]
2573
2574 rather than having to synthesize both large constants from scratch.
2575
2576 Therefore, we calculate how many insns would be required to emit
2577 the constant starting from `best_start', and also starting from
2578 zero (i.e. with bit 31 first to be output). If `best_start' doesn't
2579 yield a shorter sequence, we may as well use zero. */
2580 if (best_start != 0
2581 && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2582 && (count_insns_for_constant (remainder, 0) <=
2583 count_insns_for_constant (remainder, best_start)))
2584 best_start = 0;
2585 }
ceebdb09
PB
2586
2587 /* Now start emitting the insns. */
e2c671ba
RE
2588 i = best_start;
2589 do
2590 {
2591 int end;
2592
2593 if (i <= 0)
2594 i += 32;
2595 if (remainder & (3 << (i - 2)))
2596 {
2597 end = i - 8;
2598 if (end < 0)
2599 end += 32;
2600 temp1 = remainder & ((0x0ff << end)
2601 | ((i < end) ? (0xff >> (32 - end)) : 0));
2602 remainder &= ~temp1;
2603
d499463f 2604 if (generate)
e2c671ba 2605 {
9503f3d1
RH
2606 rtx new_src, temp1_rtx;
2607
2608 if (code == SET || code == MINUS)
2609 {
2610 new_src = (subtargets ? gen_reg_rtx (mode) : target);
96ae8197 2611 if (can_invert && code != MINUS)
9503f3d1
RH
2612 temp1 = ~temp1;
2613 }
2614 else
2615 {
96ae8197 2616 if (remainder && subtargets)
9503f3d1 2617 new_src = gen_reg_rtx (mode);
96ae8197
NC
2618 else
2619 new_src = target;
9503f3d1
RH
2620 if (can_invert)
2621 temp1 = ~temp1;
2622 else if (can_negate)
2623 temp1 = -temp1;
2624 }
2625
2626 temp1 = trunc_int_for_mode (temp1, mode);
2627 temp1_rtx = GEN_INT (temp1);
d499463f
RE
2628
2629 if (code == SET)
9503f3d1 2630 ;
d499463f 2631 else if (code == MINUS)
9503f3d1 2632 temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
d499463f 2633 else
9503f3d1
RH
2634 temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2635
a406f566 2636 emit_constant_insn (cond,
f676971a 2637 gen_rtx_SET (VOIDmode, new_src,
a406f566 2638 temp1_rtx));
d499463f 2639 source = new_src;
e2c671ba
RE
2640 }
2641
d499463f
RE
2642 if (code == SET)
2643 {
2644 can_invert = 0;
2645 code = PLUS;
2646 }
2647 else if (code == MINUS)
2648 code = PLUS;
2649
e2c671ba 2650 insns++;
5b3e6663
PB
2651 if (TARGET_ARM)
2652 i -= 6;
2653 else
2654 i -= 7;
e2c671ba 2655 }
7a085dce 2656 /* Arm allows rotates by a multiple of two. Thumb-2 allows arbitrary
5b3e6663
PB
2657 shifts. */
2658 if (TARGET_ARM)
2659 i -= 2;
2660 else
2661 i--;
1d6e90ac
NC
2662 }
2663 while (remainder);
e2c671ba 2664 }
1d6e90ac 2665
e2c671ba
RE
2666 return insns;
2667}
2668
bd9c7e23
RE
2669/* Canonicalize a comparison so that we are more likely to recognize it.
2670 This can be done for a few constant compares, where we can make the
2671 immediate value easier to load. */
1d6e90ac 2672
bd9c7e23 2673enum rtx_code
a14b88bb
PB
2674arm_canonicalize_comparison (enum rtx_code code, enum machine_mode mode,
2675 rtx * op1)
bd9c7e23 2676{
ad076f4e 2677 unsigned HOST_WIDE_INT i = INTVAL (*op1);
a14b88bb
PB
2678 unsigned HOST_WIDE_INT maxval;
2679 maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
bd9c7e23
RE
2680
2681 switch (code)
2682 {
2683 case EQ:
2684 case NE:
2685 return code;
2686
2687 case GT:
2688 case LE:
a14b88bb 2689 if (i != maxval
5895f793 2690 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
bd9c7e23 2691 {
5895f793 2692 *op1 = GEN_INT (i + 1);
bd9c7e23
RE
2693 return code == GT ? GE : LT;
2694 }
2695 break;
2696
2697 case GE:
2698 case LT:
a14b88bb 2699 if (i != ~maxval
5895f793 2700 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
bd9c7e23 2701 {
5895f793 2702 *op1 = GEN_INT (i - 1);
bd9c7e23
RE
2703 return code == GE ? GT : LE;
2704 }
2705 break;
2706
2707 case GTU:
2708 case LEU:
30cf4896 2709 if (i != ~((unsigned HOST_WIDE_INT) 0)
5895f793 2710 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
bd9c7e23
RE
2711 {
2712 *op1 = GEN_INT (i + 1);
2713 return code == GTU ? GEU : LTU;
2714 }
2715 break;
2716
2717 case GEU:
2718 case LTU:
2719 if (i != 0
5895f793 2720 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
bd9c7e23
RE
2721 {
2722 *op1 = GEN_INT (i - 1);
2723 return code == GEU ? GTU : LEU;
2724 }
2725 break;
2726
2727 default:
e6d29d15 2728 gcc_unreachable ();
bd9c7e23
RE
2729 }
2730
2731 return code;
2732}
bd9c7e23 2733
d4453b7a
PB
2734
2735/* Define how to find the value returned by a function. */
2736
9f7bf991 2737rtx
586de218 2738arm_function_value(const_tree type, const_tree func ATTRIBUTE_UNUSED)
d4453b7a
PB
2739{
2740 enum machine_mode mode;
2741 int unsignedp ATTRIBUTE_UNUSED;
2742 rtx r ATTRIBUTE_UNUSED;
2743
d4453b7a
PB
2744 mode = TYPE_MODE (type);
2745 /* Promote integer types. */
2746 if (INTEGRAL_TYPE_P (type))
2747 PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
866af8a9
JB
2748
2749 /* Promotes small structs returned in a register to full-word size
2750 for big-endian AAPCS. */
2751 if (arm_return_in_msb (type))
2752 {
2753 HOST_WIDE_INT size = int_size_in_bytes (type);
2754 if (size % UNITS_PER_WORD != 0)
2755 {
2756 size += UNITS_PER_WORD - size % UNITS_PER_WORD;
2757 mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2758 }
2759 }
e0b92319 2760
d4453b7a
PB
2761 return LIBCALL_VALUE(mode);
2762}
2763
e0b92319 2764/* Determine the amount of memory needed to store the possible return
9f7bf991
RE
2765 registers of an untyped call. */
2766int
2767arm_apply_result_size (void)
2768{
2769 int size = 16;
2770
2771 if (TARGET_ARM)
2772 {
2773 if (TARGET_HARD_FLOAT_ABI)
2774 {
2775 if (TARGET_FPA)
2776 size += 12;
2777 if (TARGET_MAVERICK)
2778 size += 8;
2779 }
2780 if (TARGET_IWMMXT_ABI)
2781 size += 8;
2782 }
2783
2784 return size;
2785}
d4453b7a 2786
f5a1b0d2 2787/* Decide whether a type should be returned in memory (true)
23668cf7 2788 or in a register (false). This is called as the target hook
81464b2c 2789 TARGET_RETURN_IN_MEMORY. */
23668cf7 2790static bool
81464b2c 2791arm_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
2b835d68 2792{
dc0ba55a
JT
2793 HOST_WIDE_INT size;
2794
88f77cba
JB
2795 size = int_size_in_bytes (type);
2796
2797 /* Vector values should be returned using ARM registers, not memory (unless
2798 they're over 16 bytes, which will break since we only have four
2799 call-clobbered registers to play with). */
2800 if (TREE_CODE (type) == VECTOR_TYPE)
2801 return (size < 0 || size > (4 * UNITS_PER_WORD));
2802
3dd7ab65
JB
2803 if (!AGGREGATE_TYPE_P (type) &&
2804 !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2805 /* All simple types are returned in registers.
2806 For AAPCS, complex types are treated the same as aggregates. */
d7d01975 2807 return 0;
dc0ba55a 2808
5848830f 2809 if (arm_abi != ARM_ABI_APCS)
dc0ba55a 2810 {
5848830f 2811 /* ATPCS and later return aggregate types in memory only if they are
dc0ba55a
JT
2812 larger than a word (or are variable size). */
2813 return (size < 0 || size > UNITS_PER_WORD);
2814 }
f676971a 2815
6bc82793 2816 /* For the arm-wince targets we choose to be compatible with Microsoft's
d5b7b3ae
RE
2817 ARM and Thumb compilers, which always return aggregates in memory. */
2818#ifndef ARM_WINCE
e529bd42
NC
2819 /* All structures/unions bigger than one word are returned in memory.
2820 Also catch the case where int_size_in_bytes returns -1. In this case
6bc82793 2821 the aggregate is either huge or of variable size, and in either case
e529bd42 2822 we will want to return it via memory and not in a register. */
dc0ba55a 2823 if (size < 0 || size > UNITS_PER_WORD)
d7d01975 2824 return 1;
f676971a 2825
d7d01975 2826 if (TREE_CODE (type) == RECORD_TYPE)
2b835d68
RE
2827 {
2828 tree field;
2829
3a2ea258
RE
2830 /* For a struct the APCS says that we only return in a register
2831 if the type is 'integer like' and every addressable element
2832 has an offset of zero. For practical purposes this means
2833 that the structure can have at most one non bit-field element
2834 and that this element must be the first one in the structure. */
f676971a 2835
f5a1b0d2
NC
2836 /* Find the first field, ignoring non FIELD_DECL things which will
2837 have been created by C++. */
2838 for (field = TYPE_FIELDS (type);
2839 field && TREE_CODE (field) != FIELD_DECL;
2840 field = TREE_CHAIN (field))
2841 continue;
f676971a 2842
f5a1b0d2 2843 if (field == NULL)
9e291dbe 2844 return 0; /* An empty structure. Allowed by an extension to ANSI C. */
f5a1b0d2 2845
d5b7b3ae
RE
2846 /* Check that the first field is valid for returning in a register. */
2847
2848 /* ... Floats are not allowed */
9e291dbe 2849 if (FLOAT_TYPE_P (TREE_TYPE (field)))
3a2ea258
RE
2850 return 1;
2851
d5b7b3ae
RE
2852 /* ... Aggregates that are not themselves valid for returning in
2853 a register are not allowed. */
81464b2c 2854 if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
3a2ea258 2855 return 1;
6f7ebcbb 2856
3a2ea258
RE
2857 /* Now check the remaining fields, if any. Only bitfields are allowed,
2858 since they are not addressable. */
f5a1b0d2
NC
2859 for (field = TREE_CHAIN (field);
2860 field;
2861 field = TREE_CHAIN (field))
2862 {
2863 if (TREE_CODE (field) != FIELD_DECL)
2864 continue;
f676971a 2865
5895f793 2866 if (!DECL_BIT_FIELD_TYPE (field))
f5a1b0d2
NC
2867 return 1;
2868 }
2b835d68
RE
2869
2870 return 0;
2871 }
f676971a 2872
d7d01975 2873 if (TREE_CODE (type) == UNION_TYPE)
2b835d68
RE
2874 {
2875 tree field;
2876
2877 /* Unions can be returned in registers if every element is
2878 integral, or can be returned in an integer register. */
f5a1b0d2
NC
2879 for (field = TYPE_FIELDS (type);
2880 field;
2881 field = TREE_CHAIN (field))
2b835d68 2882 {
f5a1b0d2
NC
2883 if (TREE_CODE (field) != FIELD_DECL)
2884 continue;
2885
6cc8c0b3
NC
2886 if (FLOAT_TYPE_P (TREE_TYPE (field)))
2887 return 1;
f676971a 2888
81464b2c 2889 if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
2b835d68
RE
2890 return 1;
2891 }
f676971a 2892
2b835d68
RE
2893 return 0;
2894 }
f676971a
EC
2895#endif /* not ARM_WINCE */
2896
d5b7b3ae 2897 /* Return all other types in memory. */
2b835d68
RE
2898 return 1;
2899}
2900
d6b4baa4 2901/* Indicate whether or not words of a double are in big-endian order. */
3717da94
JT
2902
2903int
e32bac5b 2904arm_float_words_big_endian (void)
3717da94 2905{
9b66ebb1 2906 if (TARGET_MAVERICK)
9b6b54e2 2907 return 0;
3717da94
JT
2908
2909 /* For FPA, float words are always big-endian. For VFP, floats words
2910 follow the memory system mode. */
2911
9b66ebb1 2912 if (TARGET_FPA)
3717da94 2913 {
3717da94
JT
2914 return 1;
2915 }
2916
2917 if (TARGET_VFP)
2918 return (TARGET_BIG_END ? 1 : 0);
2919
2920 return 1;
2921}
2922
82e9d970
PB
2923/* Initialize a variable CUM of type CUMULATIVE_ARGS
2924 for a call to a function whose data type is FNTYPE.
2925 For a library call, FNTYPE is NULL. */
2926void
f676971a 2927arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
e32bac5b
RE
2928 rtx libname ATTRIBUTE_UNUSED,
2929 tree fndecl ATTRIBUTE_UNUSED)
82e9d970
PB
2930{
2931 /* On the ARM, the offset starts at 0. */
29e339b9 2932 pcum->nregs = 0;
5a9335ef 2933 pcum->iwmmxt_nregs = 0;
5848830f 2934 pcum->can_split = true;
f676971a 2935
5a9335ef
NC
2936 /* Varargs vectors are treated the same as long long.
2937 named_count avoids having to change the way arm handles 'named' */
2938 pcum->named_count = 0;
2939 pcum->nargs = 0;
2940
2941 if (TARGET_REALLY_IWMMXT && fntype)
2942 {
2943 tree fn_arg;
2944
2945 for (fn_arg = TYPE_ARG_TYPES (fntype);
2946 fn_arg;
2947 fn_arg = TREE_CHAIN (fn_arg))
2948 pcum->named_count += 1;
2949
2950 if (! pcum->named_count)
2951 pcum->named_count = INT_MAX;
2952 }
82e9d970
PB
2953}
2954
5848830f
PB
2955
2956/* Return true if mode/type need doubleword alignment. */
2957bool
2958arm_needs_doubleword_align (enum machine_mode mode, tree type)
2959{
65a939f7
PB
2960 return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2961 || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
5848830f
PB
2962}
2963
2964
82e9d970
PB
2965/* Determine where to put an argument to a function.
2966 Value is zero to push the argument on the stack,
2967 or a hard register in which to store the argument.
2968
2969 MODE is the argument's machine mode.
2970 TYPE is the data type of the argument (as a tree).
2971 This is null for libcalls where that information may
2972 not be available.
2973 CUM is a variable of type CUMULATIVE_ARGS which gives info about
2974 the preceding args and about the function being called.
2975 NAMED is nonzero if this argument is a named parameter
2976 (otherwise it is an extra parameter matching an ellipsis). */
1d6e90ac 2977
82e9d970 2978rtx
e32bac5b 2979arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
5848830f 2980 tree type, int named)
82e9d970 2981{
5848830f
PB
2982 int nregs;
2983
2984 /* Varargs vectors are treated the same as long long.
2985 named_count avoids having to change the way arm handles 'named' */
2986 if (TARGET_IWMMXT_ABI
f676971a 2987 && arm_vector_mode_supported_p (mode)
5848830f 2988 && pcum->named_count > pcum->nargs + 1)
5a9335ef 2989 {
5848830f
PB
2990 if (pcum->iwmmxt_nregs <= 9)
2991 return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2992 else
5a9335ef 2993 {
5848830f
PB
2994 pcum->can_split = false;
2995 return NULL_RTX;
5a9335ef 2996 }
5a9335ef
NC
2997 }
2998
5848830f
PB
2999 /* Put doubleword aligned quantities in even register pairs. */
3000 if (pcum->nregs & 1
3001 && ARM_DOUBLEWORD_ALIGN
3002 && arm_needs_doubleword_align (mode, type))
3003 pcum->nregs++;
3004
82e9d970 3005 if (mode == VOIDmode)
25a65198
RS
3006 /* Pick an arbitrary value for operand 2 of the call insn. */
3007 return const0_rtx;
5848830f 3008
666c27b9 3009 /* Only allow splitting an arg between regs and memory if all preceding
5848830f
PB
3010 args were allocated to regs. For args passed by reference we only count
3011 the reference pointer. */
3012 if (pcum->can_split)
3013 nregs = 1;
3014 else
3015 nregs = ARM_NUM_REGS2 (mode, type);
3016
3017 if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
82e9d970 3018 return NULL_RTX;
f676971a 3019
82e9d970
PB
3020 return gen_rtx_REG (mode, pcum->nregs);
3021}
1741620c 3022
78a52f11
RH
3023static int
3024arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
3025 tree type, bool named ATTRIBUTE_UNUSED)
3026{
3027 int nregs = pcum->nregs;
3028
88f77cba 3029 if (TARGET_IWMMXT_ABI && arm_vector_mode_supported_p (mode))
78a52f11
RH
3030 return 0;
3031
3032 if (NUM_ARG_REGS > nregs
3033 && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
3034 && pcum->can_split)
3035 return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
3036
3037 return 0;
3038}
3039
1741620c
JD
3040/* Variable sized types are passed by reference. This is a GCC
3041 extension to the ARM ABI. */
3042
8cd5a4e0
RH
3043static bool
3044arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
3045 enum machine_mode mode ATTRIBUTE_UNUSED,
586de218 3046 const_tree type, bool named ATTRIBUTE_UNUSED)
1741620c
JD
3047{
3048 return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
3049}
82e9d970 3050\f
c27ba912
DM
3051/* Encode the current state of the #pragma [no_]long_calls. */
3052typedef enum
82e9d970 3053{
6fc0bb99 3054 OFF, /* No #pragma [no_]long_calls is in effect. */
c27ba912
DM
3055 LONG, /* #pragma long_calls is in effect. */
3056 SHORT /* #pragma no_long_calls is in effect. */
3057} arm_pragma_enum;
82e9d970 3058
c27ba912 3059static arm_pragma_enum arm_pragma_long_calls = OFF;
82e9d970 3060
8b97c5f8 3061void
e32bac5b 3062arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
82e9d970 3063{
8b97c5f8
ZW
3064 arm_pragma_long_calls = LONG;
3065}
3066
3067void
e32bac5b 3068arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
8b97c5f8
ZW
3069{
3070 arm_pragma_long_calls = SHORT;
3071}
3072
3073void
e32bac5b 3074arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
8b97c5f8
ZW
3075{
3076 arm_pragma_long_calls = OFF;
82e9d970
PB
3077}
3078\f
91d231cb
JM
3079/* Table of machine attributes. */
3080const struct attribute_spec arm_attribute_table[] =
82e9d970 3081{
91d231cb 3082 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
82e9d970
PB
3083 /* Function calls made to this symbol must be done indirectly, because
3084 it may lie outside of the 26 bit addressing range of a normal function
3085 call. */
91d231cb 3086 { "long_call", 0, 0, false, true, true, NULL },
82e9d970
PB
3087 /* Whereas these functions are always known to reside within the 26 bit
3088 addressing range. */
91d231cb 3089 { "short_call", 0, 0, false, true, true, NULL },
f676971a 3090 /* Interrupt Service Routines have special prologue and epilogue requirements. */
91d231cb
JM
3091 { "isr", 0, 1, false, false, false, arm_handle_isr_attribute },
3092 { "interrupt", 0, 1, false, false, false, arm_handle_isr_attribute },
3093 { "naked", 0, 0, true, false, false, arm_handle_fndecl_attribute },
3094#ifdef ARM_PE
3095 /* ARM/PE has three new attributes:
3096 interfacearm - ?
3097 dllexport - for exporting a function/variable that will live in a dll
3098 dllimport - for importing a function/variable from a dll
3099
3100 Microsoft allows multiple declspecs in one __declspec, separating
3101 them with spaces. We do NOT support this. Instead, use __declspec
3102 multiple times.
3103 */
3104 { "dllimport", 0, 0, true, false, false, NULL },
3105 { "dllexport", 0, 0, true, false, false, NULL },
3106 { "interfacearm", 0, 0, true, false, false, arm_handle_fndecl_attribute },
b2ca3702
MM
3107#elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
3108 { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
3109 { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
04fb56d5 3110 { "notshared", 0, 0, false, true, false, arm_handle_notshared_attribute },
91d231cb
JM
3111#endif
3112 { NULL, 0, 0, false, false, false, NULL }
3113};
6d3d9133 3114
91d231cb
JM
3115/* Handle an attribute requiring a FUNCTION_DECL;
3116 arguments as in struct attribute_spec.handler. */
3117static tree
e32bac5b
RE
3118arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
3119 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
91d231cb
JM
3120{
3121 if (TREE_CODE (*node) != FUNCTION_DECL)
3122 {
5c498b10 3123 warning (OPT_Wattributes, "%qs attribute only applies to functions",
91d231cb
JM
3124 IDENTIFIER_POINTER (name));
3125 *no_add_attrs = true;
3126 }
3127
3128 return NULL_TREE;
3129}
3130
3131/* Handle an "interrupt" or "isr" attribute;
3132 arguments as in struct attribute_spec.handler. */
3133static tree
e32bac5b
RE
3134arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
3135 bool *no_add_attrs)
91d231cb
JM
3136{
3137 if (DECL_P (*node))
3138 {
3139 if (TREE_CODE (*node) != FUNCTION_DECL)
3140 {
5c498b10 3141 warning (OPT_Wattributes, "%qs attribute only applies to functions",
91d231cb
JM
3142 IDENTIFIER_POINTER (name));
3143 *no_add_attrs = true;
3144 }
3145 /* FIXME: the argument if any is checked for type attributes;
3146 should it be checked for decl ones? */
3147 }
3148 else
3149 {
3150 if (TREE_CODE (*node) == FUNCTION_TYPE
3151 || TREE_CODE (*node) == METHOD_TYPE)
3152 {
3153 if (arm_isr_value (args) == ARM_FT_UNKNOWN)
3154 {
5c498b10
DD
3155 warning (OPT_Wattributes, "%qs attribute ignored",
3156 IDENTIFIER_POINTER (name));
91d231cb
JM
3157 *no_add_attrs = true;
3158 }
3159 }
3160 else if (TREE_CODE (*node) == POINTER_TYPE
3161 && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
3162 || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
3163 && arm_isr_value (args) != ARM_FT_UNKNOWN)
3164 {
8dd16ecc 3165 *node = build_variant_type_copy (*node);
1d6e90ac
NC
3166 TREE_TYPE (*node) = build_type_attribute_variant
3167 (TREE_TYPE (*node),
3168 tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
91d231cb
JM
3169 *no_add_attrs = true;
3170 }
3171 else
3172 {
3173 /* Possibly pass this attribute on from the type to a decl. */
3174 if (flags & ((int) ATTR_FLAG_DECL_NEXT
3175 | (int) ATTR_FLAG_FUNCTION_NEXT
3176 | (int) ATTR_FLAG_ARRAY_NEXT))
3177 {
3178 *no_add_attrs = true;
3179 return tree_cons (name, args, NULL_TREE);
3180 }
3181 else
3182 {
5c498b10
DD
3183 warning (OPT_Wattributes, "%qs attribute ignored",
3184 IDENTIFIER_POINTER (name));
91d231cb
JM
3185 }
3186 }
3187 }
3188
3189 return NULL_TREE;
82e9d970
PB
3190}
3191
7bff66a7 3192#if TARGET_DLLIMPORT_DECL_ATTRIBUTES
04fb56d5
MM
3193/* Handle the "notshared" attribute. This attribute is another way of
3194 requesting hidden visibility. ARM's compiler supports
3195 "__declspec(notshared)"; we support the same thing via an
3196 attribute. */
3197
3198static tree
e0b92319
NC
3199arm_handle_notshared_attribute (tree *node,
3200 tree name ATTRIBUTE_UNUSED,
3201 tree args ATTRIBUTE_UNUSED,
3202 int flags ATTRIBUTE_UNUSED,
04fb56d5
MM
3203 bool *no_add_attrs)
3204{
3205 tree decl = TYPE_NAME (*node);
3206
3207 if (decl)
3208 {
3209 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
3210 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3211 *no_add_attrs = false;
3212 }
3213 return NULL_TREE;
3214}
7bff66a7 3215#endif
04fb56d5 3216
82e9d970
PB
3217/* Return 0 if the attributes for two types are incompatible, 1 if they
3218 are compatible, and 2 if they are nearly compatible (which causes a
3219 warning to be generated). */
8d8e52be 3220static int
3101faab 3221arm_comp_type_attributes (const_tree type1, const_tree type2)
82e9d970 3222{
1cb8d58a 3223 int l1, l2, s1, s2;
f676971a 3224
82e9d970
PB
3225 /* Check for mismatch of non-default calling convention. */
3226 if (TREE_CODE (type1) != FUNCTION_TYPE)
3227 return 1;
3228
3229 /* Check for mismatched call attributes. */
1cb8d58a
NC
3230 l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
3231 l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
3232 s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
3233 s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
bd7fc26f
NC
3234
3235 /* Only bother to check if an attribute is defined. */
3236 if (l1 | l2 | s1 | s2)
3237 {
3238 /* If one type has an attribute, the other must have the same attribute. */
1cb8d58a 3239 if ((l1 != l2) || (s1 != s2))
bd7fc26f 3240 return 0;
82e9d970 3241
bd7fc26f
NC
3242 /* Disallow mixed attributes. */
3243 if ((l1 & s2) || (l2 & s1))
3244 return 0;
3245 }
f676971a 3246
6d3d9133
NC
3247 /* Check for mismatched ISR attribute. */
3248 l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
3249 if (! l1)
3250 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
3251 l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
3252 if (! l2)
3253 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
3254 if (l1 != l2)
3255 return 0;
3256
bd7fc26f 3257 return 1;
82e9d970
PB
3258}
3259
c27ba912
DM
3260/* Assigns default attributes to newly defined type. This is used to
3261 set short_call/long_call attributes for function types of
3262 functions defined inside corresponding #pragma scopes. */
8d8e52be 3263static void
e32bac5b 3264arm_set_default_type_attributes (tree type)
c27ba912
DM
3265{
3266 /* Add __attribute__ ((long_call)) to all functions, when
3267 inside #pragma long_calls or __attribute__ ((short_call)),
3268 when inside #pragma no_long_calls. */
3269 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
3270 {
3271 tree type_attr_list, attr_name;
3272 type_attr_list = TYPE_ATTRIBUTES (type);
3273
3274 if (arm_pragma_long_calls == LONG)
3275 attr_name = get_identifier ("long_call");
3276 else if (arm_pragma_long_calls == SHORT)
3277 attr_name = get_identifier ("short_call");
3278 else
3279 return;
3280
3281 type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
3282 TYPE_ATTRIBUTES (type) = type_attr_list;
3283 }
3284}
3285\f
25a65198
RS
3286/* Return true if DECL is known to be linked into section SECTION. */
3287
3288static bool
3289arm_function_in_section_p (tree decl, section *section)
c27ba912 3290{
25a65198
RS
3291 /* We can only be certain about functions defined in the same
3292 compilation unit. */
3293 if (!TREE_STATIC (decl))
3294 return false;
c27ba912 3295
25a65198
RS
3296 /* Make sure that SYMBOL always binds to the definition in this
3297 compilation unit. */
3298 if (!targetm.binds_local_p (decl))
3299 return false;
c27ba912 3300
25a65198
RS
3301 /* If DECL_SECTION_NAME is set, assume it is trustworthy. */
3302 if (!DECL_SECTION_NAME (decl))
3303 {
25a65198
RS
3304 /* Make sure that we will not create a unique section for DECL. */
3305 if (flag_function_sections || DECL_ONE_ONLY (decl))
3306 return false;
3307 }
3308
3309 return function_section (decl) == section;
c27ba912
DM
3310}
3311
a50aa827 3312/* Return nonzero if a 32-bit "long_call" should be generated for
25a65198
RS
3313 a call from the current function to DECL. We generate a long_call
3314 if the function:
c27ba912
DM
3315
3316 a. has an __attribute__((long call))
3317 or b. is within the scope of a #pragma long_calls
3318 or c. the -mlong-calls command line switch has been specified
3319
3320 However we do not generate a long call if the function:
f676971a 3321
c27ba912
DM
3322 d. has an __attribute__ ((short_call))
3323 or e. is inside the scope of a #pragma no_long_calls
25a65198 3324 or f. is defined in the same section as the current function. */
c27ba912 3325
25a65198
RS
3326bool
3327arm_is_long_call_p (tree decl)
3328{
3329 tree attrs;
c27ba912 3330
25a65198
RS
3331 if (!decl)
3332 return TARGET_LONG_CALLS;
c27ba912 3333
25a65198
RS
3334 attrs = TYPE_ATTRIBUTES (TREE_TYPE (decl));
3335 if (lookup_attribute ("short_call", attrs))
3336 return false;
c27ba912 3337
25a65198
RS
3338 /* For "f", be conservative, and only cater for cases in which the
3339 whole of the current function is placed in the same section. */
3340 if (!flag_reorder_blocks_and_partition
3341 && arm_function_in_section_p (decl, current_function_section ()))
3342 return false;
a77655b1 3343
25a65198
RS
3344 if (lookup_attribute ("long_call", attrs))
3345 return true;
f676971a 3346
25a65198 3347 return TARGET_LONG_CALLS;
c27ba912 3348}
f99fce0c 3349
825dda42 3350/* Return nonzero if it is ok to make a tail-call to DECL. */
4977bab6 3351static bool
e32bac5b 3352arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
f99fce0c 3353{
5b3e6663 3354 unsigned long func_type;
f99fce0c 3355
5a9335ef
NC
3356 if (cfun->machine->sibcall_blocked)
3357 return false;
3358
f99fce0c
RE
3359 /* Never tailcall something for which we have no decl, or if we
3360 are in Thumb mode. */
3361 if (decl == NULL || TARGET_THUMB)
4977bab6 3362 return false;
f99fce0c 3363
9403b7f7
RS
3364 /* The PIC register is live on entry to VxWorks PLT entries, so we
3365 must make the call before restoring the PIC register. */
3366 if (TARGET_VXWORKS_RTP && flag_pic && !targetm.binds_local_p (decl))
3367 return false;
3368
f99fce0c 3369 /* Cannot tail-call to long calls, since these are out of range of
25a65198
RS
3370 a branch instruction. */
3371 if (arm_is_long_call_p (decl))
4977bab6 3372 return false;
f99fce0c
RE
3373
3374 /* If we are interworking and the function is not declared static
f676971a 3375 then we can't tail-call it unless we know that it exists in this
f99fce0c 3376 compilation unit (since it might be a Thumb routine). */
5895f793 3377 if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
4977bab6 3378 return false;
f99fce0c 3379
5b3e6663 3380 func_type = arm_current_func_type ();
6d3d9133 3381 /* Never tailcall from an ISR routine - it needs a special exit sequence. */
5b3e6663
PB
3382 if (IS_INTERRUPT (func_type))
3383 return false;
3384
3385 /* Never tailcall if function may be called with a misaligned SP. */
3386 if (IS_STACKALIGN (func_type))
4977bab6 3387 return false;
6d3d9133 3388
f99fce0c 3389 /* Everything else is ok. */
4977bab6 3390 return true;
f99fce0c
RE
3391}
3392
82e9d970 3393\f
6b990f6b
RE
3394/* Addressing mode support functions. */
3395
0b4be7de 3396/* Return nonzero if X is a legitimate immediate operand when compiling
020a4035 3397 for PIC. We know that X satisfies CONSTANT_P and flag_pic is true. */
32de079a 3398int
e32bac5b 3399legitimate_pic_operand_p (rtx x)
32de079a 3400{
020a4035
RE
3401 if (GET_CODE (x) == SYMBOL_REF
3402 || (GET_CODE (x) == CONST
3403 && GET_CODE (XEXP (x, 0)) == PLUS
3404 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
32de079a
RE
3405 return 0;
3406
3407 return 1;
3408}
3409
9403b7f7
RS
3410/* Record that the current function needs a PIC register. Initialize
3411 cfun->machine->pic_reg if we have not already done so. */
3412
3413static void
3414require_pic_register (void)
3415{
3416 /* A lot of the logic here is made obscure by the fact that this
3417 routine gets called as part of the rtx cost estimation process.
3418 We don't want those calls to affect any assumptions about the real
3419 function; and further, we can't call entry_of_function() until we
3420 start the real expansion process. */
e3b5732b 3421 if (!crtl->uses_pic_offset_table)
9403b7f7 3422 {
b3a13419 3423 gcc_assert (can_create_pseudo_p ());
9403b7f7
RS
3424 if (arm_pic_register != INVALID_REGNUM)
3425 {
3426 cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
3427
3428 /* Play games to avoid marking the function as needing pic
3429 if we are being called as part of the cost-estimation
3430 process. */
3431 if (current_ir_type () != IR_GIMPLE)
e3b5732b 3432 crtl->uses_pic_offset_table = 1;
9403b7f7
RS
3433 }
3434 else
3435 {
3436 rtx seq;
3437
3438 cfun->machine->pic_reg = gen_reg_rtx (Pmode);
3439
3440 /* Play games to avoid marking the function as needing pic
3441 if we are being called as part of the cost-estimation
3442 process. */
3443 if (current_ir_type () != IR_GIMPLE)
3444 {
e3b5732b 3445 crtl->uses_pic_offset_table = 1;
9403b7f7
RS
3446 start_sequence ();
3447
3448 arm_load_pic_register (0UL);
3449
3450 seq = get_insns ();
3451 end_sequence ();
3452 emit_insn_after (seq, entry_of_function ());
3453 }
3454 }
3455 }
3456}
3457
32de079a 3458rtx
e32bac5b 3459legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
32de079a 3460{
a3c48721
RE
3461 if (GET_CODE (orig) == SYMBOL_REF
3462 || GET_CODE (orig) == LABEL_REF)
32de079a
RE
3463 {
3464 rtx pic_ref, address;
3465 rtx insn;
3466 int subregs = 0;
3467
9403b7f7
RS
3468 /* If this function doesn't have a pic register, create one now. */
3469 require_pic_register ();
020a4035 3470
32de079a
RE
3471 if (reg == 0)
3472 {
b3a13419 3473 gcc_assert (can_create_pseudo_p ());
e6d29d15 3474 reg = gen_reg_rtx (Pmode);
32de079a
RE
3475
3476 subregs = 1;
3477 }
3478
32de079a
RE
3479 if (subregs)
3480 address = gen_reg_rtx (Pmode);
3481 else
3482 address = reg;
3483
4bec9f7d
NC
3484 if (TARGET_ARM)
3485 emit_insn (gen_pic_load_addr_arm (address, orig));
5b3e6663
PB
3486 else if (TARGET_THUMB2)
3487 emit_insn (gen_pic_load_addr_thumb2 (address, orig));
3488 else /* TARGET_THUMB1 */
3489 emit_insn (gen_pic_load_addr_thumb1 (address, orig));
32de079a 3490
9403b7f7
RS
3491 /* VxWorks does not impose a fixed gap between segments; the run-time
3492 gap can be different from the object-file gap. We therefore can't
3493 use GOTOFF unless we are absolutely sure that the symbol is in the
3494 same segment as the GOT. Unfortunately, the flexibility of linker
3495 scripts means that we can't be sure of that in general, so assume
3496 that GOTOFF is never valid on VxWorks. */
14f583b8 3497 if ((GET_CODE (orig) == LABEL_REF
f676971a 3498 || (GET_CODE (orig) == SYMBOL_REF &&
94428622 3499 SYMBOL_REF_LOCAL_P (orig)))
9403b7f7
RS
3500 && NEED_GOT_RELOC
3501 && !TARGET_VXWORKS_RTP)
020a4035 3502 pic_ref = gen_rtx_PLUS (Pmode, cfun->machine->pic_reg, address);
a3c48721
RE
3503 else
3504 {
542a8afa 3505 pic_ref = gen_const_mem (Pmode,
020a4035 3506 gen_rtx_PLUS (Pmode, cfun->machine->pic_reg,
542a8afa 3507 address));
a3c48721
RE
3508 }
3509
32de079a 3510 insn = emit_move_insn (reg, pic_ref);
96a3900d 3511
32de079a
RE
3512 /* Put a REG_EQUAL note on this insn, so that it can be optimized
3513 by loop. */
bd94cb6e
SB
3514 set_unique_reg_note (insn, REG_EQUAL, orig);
3515
32de079a
RE
3516 return reg;
3517 }
3518 else if (GET_CODE (orig) == CONST)
3519 {
3520 rtx base, offset;
3521
3522 if (GET_CODE (XEXP (orig, 0)) == PLUS
020a4035 3523 && XEXP (XEXP (orig, 0), 0) == cfun->machine->pic_reg)
32de079a
RE
3524 return orig;
3525
f67358da 3526 /* Handle the case where we have: const (UNSPEC_TLS). */
d3585b76
DJ
3527 if (GET_CODE (XEXP (orig, 0)) == UNSPEC
3528 && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
3529 return orig;
3530
f67358da
PB
3531 /* Handle the case where we have:
3532 const (plus (UNSPEC_TLS) (ADDEND)). The ADDEND must be a
3533 CONST_INT. */
3534 if (GET_CODE (XEXP (orig, 0)) == PLUS
3535 && GET_CODE (XEXP (XEXP (orig, 0), 0)) == UNSPEC
3536 && XINT (XEXP (XEXP (orig, 0), 0), 1) == UNSPEC_TLS)
3537 {
3538 gcc_assert (GET_CODE (XEXP (XEXP (orig, 0), 1)) == CONST_INT);
3539 return orig;
3540 }
3541
32de079a
RE
3542 if (reg == 0)
3543 {
b3a13419 3544 gcc_assert (can_create_pseudo_p ());
e6d29d15 3545 reg = gen_reg_rtx (Pmode);
32de079a
RE
3546 }
3547
e6d29d15 3548 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
e0b92319 3549
e6d29d15
NS
3550 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3551 offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3552 base == reg ? 0 : reg);
32de079a
RE
3553
3554 if (GET_CODE (offset) == CONST_INT)
3555 {
3556 /* The base register doesn't really matter, we only want to
3557 test the index for the appropriate mode. */
1e1ab407 3558 if (!arm_legitimate_index_p (mode, offset, SET, 0))
6b990f6b 3559 {
b3a13419 3560 gcc_assert (can_create_pseudo_p ());
e6d29d15 3561 offset = force_reg (Pmode, offset);
6b990f6b 3562 }
32de079a 3563
32de079a 3564 if (GET_CODE (offset) == CONST_INT)
ed8908e7 3565 return plus_constant (base, INTVAL (offset));
32de079a
RE
3566 }
3567
3568 if (GET_MODE_SIZE (mode) > 4
3569 && (GET_MODE_CLASS (mode) == MODE_INT
3570 || TARGET_SOFT_FLOAT))
3571 {
3572 emit_insn (gen_addsi3 (reg, base, offset));
3573 return reg;
3574 }
3575
43cffd11 3576 return gen_rtx_PLUS (Pmode, base, offset);
32de079a 3577 }
32de079a
RE
3578
3579 return orig;
3580}
3581
57934c39 3582
5b3e6663 3583/* Find a spare register to use during the prolog of a function. */
57934c39
PB
3584
3585static int
b279b20a 3586thumb_find_work_register (unsigned long pushed_regs_mask)
57934c39
PB
3587{
3588 int reg;
3589
b279b20a
NC
3590 /* Check the argument registers first as these are call-used. The
3591 register allocation order means that sometimes r3 might be used
3592 but earlier argument registers might not, so check them all. */
3593 for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
6fb5fa3c 3594 if (!df_regs_ever_live_p (reg))
b279b20a
NC
3595 return reg;
3596
3597 /* Before going on to check the call-saved registers we can try a couple
3598 more ways of deducing that r3 is available. The first is when we are
3599 pushing anonymous arguments onto the stack and we have less than 4
3600 registers worth of fixed arguments(*). In this case r3 will be part of
3601 the variable argument list and so we can be sure that it will be
3602 pushed right at the start of the function. Hence it will be available
3603 for the rest of the prologue.
38173d38 3604 (*): ie crtl->args.pretend_args_size is greater than 0. */
b279b20a 3605 if (cfun->machine->uses_anonymous_args
38173d38 3606 && crtl->args.pretend_args_size > 0)
57934c39
PB
3607 return LAST_ARG_REGNUM;
3608
b279b20a
NC
3609 /* The other case is when we have fixed arguments but less than 4 registers
3610 worth. In this case r3 might be used in the body of the function, but
3611 it is not being used to convey an argument into the function. In theory
38173d38 3612 we could just check crtl->args.size to see how many bytes are
b279b20a
NC
3613 being passed in argument registers, but it seems that it is unreliable.
3614 Sometimes it will have the value 0 when in fact arguments are being
3615 passed. (See testcase execute/20021111-1.c for an example). So we also
3616 check the args_info.nregs field as well. The problem with this field is
3617 that it makes no allowances for arguments that are passed to the
3618 function but which are not used. Hence we could miss an opportunity
3619 when a function has an unused argument in r3. But it is better to be
3620 safe than to be sorry. */
3621 if (! cfun->machine->uses_anonymous_args
38173d38
JH
3622 && crtl->args.size >= 0
3623 && crtl->args.size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
f6d2671e 3624 && crtl->args.info.nregs < 4)
b279b20a 3625 return LAST_ARG_REGNUM;
e0b92319 3626
b279b20a
NC
3627 /* Otherwise look for a call-saved register that is going to be pushed. */
3628 for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3629 if (pushed_regs_mask & (1 << reg))
57934c39
PB
3630 return reg;
3631
5b3e6663
PB
3632 if (TARGET_THUMB2)
3633 {
3634 /* Thumb-2 can use high regs. */
3635 for (reg = FIRST_HI_REGNUM; reg < 15; reg ++)
3636 if (pushed_regs_mask & (1 << reg))
3637 return reg;
3638 }
b279b20a
NC
3639 /* Something went wrong - thumb_compute_save_reg_mask()
3640 should have arranged for a suitable register to be pushed. */
e6d29d15 3641 gcc_unreachable ();
57934c39
PB
3642}
3643
f16fe45f 3644static GTY(()) int pic_labelno;
876f13b0 3645
fe013435
PB
3646/* Generate code to load the PIC register. In thumb mode SCRATCH is a
3647 low register. */
876f13b0 3648
32de079a 3649void
e55ef7f4 3650arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
32de079a 3651{
9403b7f7 3652 rtx l1, labelno, pic_tmp, pic_tmp2, pic_rtx, pic_reg;
32de079a
RE
3653 rtx global_offset_table;
3654
e3b5732b 3655 if (crtl->uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
32de079a
RE
3656 return;
3657
e6d29d15 3658 gcc_assert (flag_pic);
32de079a 3659
9403b7f7
RS
3660 pic_reg = cfun->machine->pic_reg;
3661 if (TARGET_VXWORKS_RTP)
3662 {
3663 pic_rtx = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE);
3664 pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
3665 emit_insn (gen_pic_load_addr_arm (pic_reg, pic_rtx));
43cffd11 3666
9403b7f7 3667 emit_insn (gen_rtx_SET (Pmode, pic_reg, gen_rtx_MEM (Pmode, pic_reg)));
f676971a 3668
9403b7f7
RS
3669 pic_tmp = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
3670 emit_insn (gen_pic_offset_arm (pic_reg, pic_reg, pic_tmp));
4bec9f7d 3671 }
9403b7f7 3672 else
5b3e6663 3673 {
9403b7f7
RS
3674 /* We use an UNSPEC rather than a LABEL_REF because this label
3675 never appears in the code stream. */
3676
3677 labelno = GEN_INT (pic_labelno++);
3678 l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3679 l1 = gen_rtx_CONST (VOIDmode, l1);
3680
3681 global_offset_table
3682 = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3683 /* On the ARM the PC register contains 'dot + 8' at the time of the
3684 addition, on the Thumb it is 'dot + 4'. */
3685 pic_tmp = plus_constant (l1, TARGET_ARM ? 8 : 4);
3686 if (GOT_PCREL)
5b3e6663 3687 {
9403b7f7
RS
3688 pic_tmp2 = gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx);
3689 pic_tmp2 = gen_rtx_CONST (VOIDmode, pic_tmp2);
5b3e6663
PB
3690 }
3691 else
9403b7f7
RS
3692 pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3693
3694 pic_rtx = gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp);
3695 pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
3696
3697 if (TARGET_ARM)
5b3e6663 3698 {
9403b7f7
RS
3699 emit_insn (gen_pic_load_addr_arm (pic_reg, pic_rtx));
3700 emit_insn (gen_pic_add_dot_plus_eight (pic_reg, pic_reg, labelno));
5b3e6663 3701 }
9403b7f7
RS
3702 else if (TARGET_THUMB2)
3703 {
3704 /* Thumb-2 only allows very limited access to the PC. Calculate the
3705 address in a temporary register. */
3706 if (arm_pic_register != INVALID_REGNUM)
3707 {
3708 pic_tmp = gen_rtx_REG (SImode,
3709 thumb_find_work_register (saved_regs));
3710 }
3711 else
3712 {
b3a13419 3713 gcc_assert (can_create_pseudo_p ());
9403b7f7
RS
3714 pic_tmp = gen_reg_rtx (Pmode);
3715 }
5b3e6663 3716
9403b7f7
RS
3717 emit_insn (gen_pic_load_addr_thumb2 (pic_reg, pic_rtx));
3718 emit_insn (gen_pic_load_dot_plus_four (pic_tmp, labelno));
3719 emit_insn (gen_addsi3 (pic_reg, pic_reg, pic_tmp));
3720 }
3721 else /* TARGET_THUMB1 */
876f13b0 3722 {
9403b7f7
RS
3723 if (arm_pic_register != INVALID_REGNUM
3724 && REGNO (pic_reg) > LAST_LO_REGNUM)
3725 {
3726 /* We will have pushed the pic register, so we should always be
3727 able to find a work register. */
3728 pic_tmp = gen_rtx_REG (SImode,
3729 thumb_find_work_register (saved_regs));
3730 emit_insn (gen_pic_load_addr_thumb1 (pic_tmp, pic_rtx));
3731 emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3732 }
3733 else
3734 emit_insn (gen_pic_load_addr_thumb1 (pic_reg, pic_rtx));
3735 emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
876f13b0 3736 }
4bec9f7d 3737 }
32de079a 3738
32de079a
RE
3739 /* Need to emit this whether or not we obey regdecls,
3740 since setjmp/longjmp can cause life info to screw up. */
c41c1387 3741 emit_use (pic_reg);
32de079a
RE
3742}
3743
876f13b0 3744
6b990f6b
RE
3745/* Return nonzero if X is valid as an ARM state addressing register. */
3746static int
e32bac5b 3747arm_address_register_rtx_p (rtx x, int strict_p)
6b990f6b
RE
3748{
3749 int regno;
3750
3751 if (GET_CODE (x) != REG)
3752 return 0;
3753
3754 regno = REGNO (x);
3755
3756 if (strict_p)
3757 return ARM_REGNO_OK_FOR_BASE_P (regno);
3758
3759 return (regno <= LAST_ARM_REGNUM
3760 || regno >= FIRST_PSEUDO_REGISTER
3761 || regno == FRAME_POINTER_REGNUM
3762 || regno == ARG_POINTER_REGNUM);
3763}
3764
d3585b76
DJ
3765/* Return TRUE if this rtx is the difference of a symbol and a label,
3766 and will reduce to a PC-relative relocation in the object file.
3767 Expressions like this can be left alone when generating PIC, rather
3768 than forced through the GOT. */
3769static int
3770pcrel_constant_p (rtx x)
3771{
3772 if (GET_CODE (x) == MINUS)
3773 return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
3774
3775 return FALSE;
3776}
3777
6b990f6b
RE
3778/* Return nonzero if X is a valid ARM state address operand. */
3779int
1e1ab407
RE
3780arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3781 int strict_p)
6b990f6b 3782{
fdd695fd
PB
3783 bool use_ldrd;
3784 enum rtx_code code = GET_CODE (x);
f676971a 3785
6b990f6b
RE
3786 if (arm_address_register_rtx_p (x, strict_p))
3787 return 1;
3788
fdd695fd
PB
3789 use_ldrd = (TARGET_LDRD
3790 && (mode == DImode
3791 || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3792
3793 if (code == POST_INC || code == PRE_DEC
3794 || ((code == PRE_INC || code == POST_DEC)
3795 && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
6b990f6b
RE
3796 return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3797
fdd695fd 3798 else if ((code == POST_MODIFY || code == PRE_MODIFY)
6b990f6b
RE
3799 && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3800 && GET_CODE (XEXP (x, 1)) == PLUS
386d3a16 3801 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
fdd695fd
PB
3802 {
3803 rtx addend = XEXP (XEXP (x, 1), 1);
3804
112cdef5 3805 /* Don't allow ldrd post increment by register because it's hard
fdd695fd
PB
3806 to fixup invalid register choices. */
3807 if (use_ldrd
3808 && GET_CODE (x) == POST_MODIFY
3809 && GET_CODE (addend) == REG)
3810 return 0;
3811
3812 return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3813 && arm_legitimate_index_p (mode, addend, outer, strict_p));
3814 }
6b990f6b
RE
3815
3816 /* After reload constants split into minipools will have addresses
3817 from a LABEL_REF. */
0bfb39ef 3818 else if (reload_completed
fdd695fd
PB
3819 && (code == LABEL_REF
3820 || (code == CONST
6b990f6b
RE
3821 && GET_CODE (XEXP (x, 0)) == PLUS
3822 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3823 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3824 return 1;
3825
88f77cba 3826 else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
6b990f6b
RE
3827 return 0;
3828
fdd695fd 3829 else if (code == PLUS)
6b990f6b
RE
3830 {
3831 rtx xop0 = XEXP (x, 0);
3832 rtx xop1 = XEXP (x, 1);
3833
3834 return ((arm_address_register_rtx_p (xop0, strict_p)
1e1ab407 3835 && arm_legitimate_index_p (mode, xop1, outer, strict_p))
6b990f6b 3836 || (arm_address_register_rtx_p (xop1, strict_p)
1e1ab407 3837 && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
6b990f6b
RE
3838 }
3839
3840#if 0
3841 /* Reload currently can't handle MINUS, so disable this for now */
3842 else if (GET_CODE (x) == MINUS)
3843 {
3844 rtx xop0 = XEXP (x, 0);
3845 rtx xop1 = XEXP (x, 1);
3846
3847 return (arm_address_register_rtx_p (xop0, strict_p)
1e1ab407 3848 && arm_legitimate_index_p (mode, xop1, outer, strict_p));
6b990f6b
RE
3849 }
3850#endif
3851
3852 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
fdd695fd 3853 && code == SYMBOL_REF
6b990f6b
RE
3854 && CONSTANT_POOL_ADDRESS_P (x)
3855 && ! (flag_pic
d3585b76
DJ
3856 && symbol_mentioned_p (get_pool_constant (x))
3857 && ! pcrel_constant_p (get_pool_constant (x))))
6b990f6b
RE
3858 return 1;
3859
6b990f6b
RE
3860 return 0;
3861}
3862
5b3e6663
PB
3863/* Return nonzero if X is a valid Thumb-2 address operand. */
3864int
3865thumb2_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3866{
3867 bool use_ldrd;
3868 enum rtx_code code = GET_CODE (x);
3869
3870 if (arm_address_register_rtx_p (x, strict_p))
3871 return 1;
3872
3873 use_ldrd = (TARGET_LDRD
3874 && (mode == DImode
3875 || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3876
3877 if (code == POST_INC || code == PRE_DEC
3878 || ((code == PRE_INC || code == POST_DEC)
3879 && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3880 return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3881
3882 else if ((code == POST_MODIFY || code == PRE_MODIFY)
3883 && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3884 && GET_CODE (XEXP (x, 1)) == PLUS
3885 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3886 {
3887 /* Thumb-2 only has autoincrement by constant. */
3888 rtx addend = XEXP (XEXP (x, 1), 1);
3889 HOST_WIDE_INT offset;
3890
3891 if (GET_CODE (addend) != CONST_INT)
3892 return 0;
3893
3894 offset = INTVAL(addend);
3895 if (GET_MODE_SIZE (mode) <= 4)
3896 return (offset > -256 && offset < 256);
3897
3898 return (use_ldrd && offset > -1024 && offset < 1024
3899 && (offset & 3) == 0);
3900 }
3901
3902 /* After reload constants split into minipools will have addresses
3903 from a LABEL_REF. */
3904 else if (reload_completed
3905 && (code == LABEL_REF
3906 || (code == CONST
3907 && GET_CODE (XEXP (x, 0)) == PLUS
3908 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3909 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3910 return 1;
3911
88f77cba 3912 else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
5b3e6663
PB
3913 return 0;
3914
3915 else if (code == PLUS)
3916 {
3917 rtx xop0 = XEXP (x, 0);
3918 rtx xop1 = XEXP (x, 1);
3919
3920 return ((arm_address_register_rtx_p (xop0, strict_p)
3921 && thumb2_legitimate_index_p (mode, xop1, strict_p))
3922 || (arm_address_register_rtx_p (xop1, strict_p)
3923 && thumb2_legitimate_index_p (mode, xop0, strict_p)));
3924 }
3925
3926 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3927 && code == SYMBOL_REF
3928 && CONSTANT_POOL_ADDRESS_P (x)
3929 && ! (flag_pic
3930 && symbol_mentioned_p (get_pool_constant (x))
3931 && ! pcrel_constant_p (get_pool_constant (x))))
3932 return 1;
3933
3934 return 0;
3935}
3936
6b990f6b
RE
3937/* Return nonzero if INDEX is valid for an address index operand in
3938 ARM state. */
3939static int
1e1ab407
RE
3940arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3941 int strict_p)
6b990f6b
RE
3942{
3943 HOST_WIDE_INT range;
3944 enum rtx_code code = GET_CODE (index);
3945
778ebdd9
PB
3946 /* Standard coprocessor addressing modes. */
3947 if (TARGET_HARD_FLOAT
3948 && (TARGET_FPA || TARGET_MAVERICK)
3949 && (GET_MODE_CLASS (mode) == MODE_FLOAT
3950 || (TARGET_MAVERICK && mode == DImode)))
6b990f6b
RE
3951 return (code == CONST_INT && INTVAL (index) < 1024
3952 && INTVAL (index) > -1024
3953 && (INTVAL (index) & 3) == 0);
3954
88f77cba
JB
3955 if (TARGET_NEON
3956 && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode)))
3957 return (code == CONST_INT
3958 && INTVAL (index) < 1016
3959 && INTVAL (index) > -1024
3960 && (INTVAL (index) & 3) == 0);
3961
5a9335ef
NC
3962 if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3963 return (code == CONST_INT
3657dc3e
PB
3964 && INTVAL (index) < 1024
3965 && INTVAL (index) > -1024
3966 && (INTVAL (index) & 3) == 0);
5a9335ef 3967
fdd695fd
PB
3968 if (arm_address_register_rtx_p (index, strict_p)
3969 && (GET_MODE_SIZE (mode) <= 4))
3970 return 1;
3971
3972 if (mode == DImode || mode == DFmode)
3973 {
3974 if (code == CONST_INT)
3975 {
3976 HOST_WIDE_INT val = INTVAL (index);
3977
3978 if (TARGET_LDRD)
3979 return val > -256 && val < 256;
3980 else
f372c932 3981 return val > -4096 && val < 4092;
fdd695fd
PB
3982 }
3983
3984 return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3985 }
3986
6b990f6b 3987 if (GET_MODE_SIZE (mode) <= 4
1e1ab407
RE
3988 && ! (arm_arch4
3989 && (mode == HImode
3990 || (mode == QImode && outer == SIGN_EXTEND))))
6b990f6b 3991 {
1e1ab407
RE
3992 if (code == MULT)
3993 {
3994 rtx xiop0 = XEXP (index, 0);
3995 rtx xiop1 = XEXP (index, 1);
3996
3997 return ((arm_address_register_rtx_p (xiop0, strict_p)
3998 && power_of_two_operand (xiop1, SImode))
3999 || (arm_address_register_rtx_p (xiop1, strict_p)
4000 && power_of_two_operand (xiop0, SImode)));
4001 }
4002 else if (code == LSHIFTRT || code == ASHIFTRT
4003 || code == ASHIFT || code == ROTATERT)
4004 {
4005 rtx op = XEXP (index, 1);
6b990f6b 4006
1e1ab407
RE
4007 return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
4008 && GET_CODE (op) == CONST_INT
4009 && INTVAL (op) > 0
4010 && INTVAL (op) <= 31);
4011 }
6b990f6b
RE
4012 }
4013
1e1ab407
RE
4014 /* For ARM v4 we may be doing a sign-extend operation during the
4015 load. */
e1471c91 4016 if (arm_arch4)
1e1ab407
RE
4017 {
4018 if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
4019 range = 256;
4020 else
4021 range = 4096;
4022 }
e1471c91
RE
4023 else
4024 range = (mode == HImode) ? 4095 : 4096;
6b990f6b
RE
4025
4026 return (code == CONST_INT
4027 && INTVAL (index) < range
4028 && INTVAL (index) > -range);
76a318e9
RE
4029}
4030
5b3e6663
PB
4031/* Return true if OP is a valid index scaling factor for Thumb-2 address
4032 index operand. i.e. 1, 2, 4 or 8. */
4033static bool
4034thumb2_index_mul_operand (rtx op)
4035{
4036 HOST_WIDE_INT val;
4037
4038 if (GET_CODE(op) != CONST_INT)
4039 return false;
4040
4041 val = INTVAL(op);
4042 return (val == 1 || val == 2 || val == 4 || val == 8);
4043}
4044
4045/* Return nonzero if INDEX is a valid Thumb-2 address index operand. */
4046static int
4047thumb2_legitimate_index_p (enum machine_mode mode, rtx index, int strict_p)
4048{
4049 enum rtx_code code = GET_CODE (index);
4050
4051 /* ??? Combine arm and thumb2 coprocessor addressing modes. */
4052 /* Standard coprocessor addressing modes. */
4053 if (TARGET_HARD_FLOAT
4054 && (TARGET_FPA || TARGET_MAVERICK)
4055 && (GET_MODE_CLASS (mode) == MODE_FLOAT
4056 || (TARGET_MAVERICK && mode == DImode)))
4057 return (code == CONST_INT && INTVAL (index) < 1024
4058 && INTVAL (index) > -1024
4059 && (INTVAL (index) & 3) == 0);
4060
4061 if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
fe2d934b
PB
4062 {
4063 /* For DImode assume values will usually live in core regs
4064 and only allow LDRD addressing modes. */
4065 if (!TARGET_LDRD || mode != DImode)
4066 return (code == CONST_INT
4067 && INTVAL (index) < 1024
4068 && INTVAL (index) > -1024
4069 && (INTVAL (index) & 3) == 0);
4070 }
5b3e6663 4071
88f77cba
JB
4072 if (TARGET_NEON
4073 && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode)))
4074 return (code == CONST_INT
4075 && INTVAL (index) < 1016
4076 && INTVAL (index) > -1024
4077 && (INTVAL (index) & 3) == 0);
4078
5b3e6663
PB
4079 if (arm_address_register_rtx_p (index, strict_p)
4080 && (GET_MODE_SIZE (mode) <= 4))
4081 return 1;
4082
4083 if (mode == DImode || mode == DFmode)
4084 {
4085 HOST_WIDE_INT val = INTVAL (index);
4086 /* ??? Can we assume ldrd for thumb2? */
4087 /* Thumb-2 ldrd only has reg+const addressing modes. */
4088 if (code != CONST_INT)
4089 return 0;
4090
4091 /* ldrd supports offsets of +-1020.
4092 However the ldr fallback does not. */
4093 return val > -256 && val < 256 && (val & 3) == 0;
4094 }
4095
4096 if (code == MULT)
4097 {
4098 rtx xiop0 = XEXP (index, 0);
4099 rtx xiop1 = XEXP (index, 1);
4100
4101 return ((arm_address_register_rtx_p (xiop0, strict_p)
4102 && thumb2_index_mul_operand (xiop1))
4103 || (arm_address_register_rtx_p (xiop1, strict_p)
4104 && thumb2_index_mul_operand (xiop0)));
4105 }
4106 else if (code == ASHIFT)
4107 {
4108 rtx op = XEXP (index, 1);
4109
4110 return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
4111 && GET_CODE (op) == CONST_INT
4112 && INTVAL (op) > 0
4113 && INTVAL (op) <= 3);
4114 }
4115
4116 return (code == CONST_INT
4117 && INTVAL (index) < 4096
4118 && INTVAL (index) > -256);
4119}
4120
4121/* Return nonzero if X is valid as a 16-bit Thumb state base register. */
76a318e9 4122static int
5b3e6663 4123thumb1_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
76a318e9
RE
4124{
4125 int regno;
4126
4127 if (GET_CODE (x) != REG)
4128 return 0;
4129
4130 regno = REGNO (x);
4131
4132 if (strict_p)
5b3e6663 4133 return THUMB1_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
76a318e9
RE
4134
4135 return (regno <= LAST_LO_REGNUM
07e58265 4136 || regno > LAST_VIRTUAL_REGISTER
76a318e9
RE
4137 || regno == FRAME_POINTER_REGNUM
4138 || (GET_MODE_SIZE (mode) >= 4
4139 && (regno == STACK_POINTER_REGNUM
edf7cee8 4140 || regno >= FIRST_PSEUDO_REGISTER
76a318e9
RE
4141 || x == hard_frame_pointer_rtx
4142 || x == arg_pointer_rtx)));
4143}
4144
4145/* Return nonzero if x is a legitimate index register. This is the case
4146 for any base register that can access a QImode object. */
4147inline static int
5b3e6663 4148thumb1_index_register_rtx_p (rtx x, int strict_p)
76a318e9 4149{
5b3e6663 4150 return thumb1_base_register_rtx_p (x, QImode, strict_p);
76a318e9
RE
4151}
4152
5b3e6663 4153/* Return nonzero if x is a legitimate 16-bit Thumb-state address.
f676971a 4154
76a318e9
RE
4155 The AP may be eliminated to either the SP or the FP, so we use the
4156 least common denominator, e.g. SImode, and offsets from 0 to 64.
4157
4158 ??? Verify whether the above is the right approach.
4159
4160 ??? Also, the FP may be eliminated to the SP, so perhaps that
4161 needs special handling also.
4162
4163 ??? Look at how the mips16 port solves this problem. It probably uses
4164 better ways to solve some of these problems.
4165
4166 Although it is not incorrect, we don't accept QImode and HImode
4167 addresses based on the frame pointer or arg pointer until the
4168 reload pass starts. This is so that eliminating such addresses
4169 into stack based ones won't produce impossible code. */
4170int
5b3e6663 4171thumb1_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
76a318e9
RE
4172{
4173 /* ??? Not clear if this is right. Experiment. */
4174 if (GET_MODE_SIZE (mode) < 4
4175 && !(reload_in_progress || reload_completed)
4176 && (reg_mentioned_p (frame_pointer_rtx, x)
4177 || reg_mentioned_p (arg_pointer_rtx, x)
4178 || reg_mentioned_p (virtual_incoming_args_rtx, x)
4179 || reg_mentioned_p (virtual_outgoing_args_rtx, x)
4180 || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
4181 || reg_mentioned_p (virtual_stack_vars_rtx, x)))
4182 return 0;
4183
4184 /* Accept any base register. SP only in SImode or larger. */
5b3e6663 4185 else if (thumb1_base_register_rtx_p (x, mode, strict_p))
76a318e9
RE
4186 return 1;
4187
18dbd950 4188 /* This is PC relative data before arm_reorg runs. */
76a318e9
RE
4189 else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
4190 && GET_CODE (x) == SYMBOL_REF
020a4035 4191 && CONSTANT_POOL_ADDRESS_P (x) && !flag_pic)
76a318e9
RE
4192 return 1;
4193
18dbd950 4194 /* This is PC relative data after arm_reorg runs. */
76a318e9
RE
4195 else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
4196 && (GET_CODE (x) == LABEL_REF
4197 || (GET_CODE (x) == CONST
4198 && GET_CODE (XEXP (x, 0)) == PLUS
4199 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
4200 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
4201 return 1;
4202
4203 /* Post-inc indexing only supported for SImode and larger. */
4204 else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
5b3e6663 4205 && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p))
76a318e9
RE
4206 return 1;
4207
4208 else if (GET_CODE (x) == PLUS)
4209 {
4210 /* REG+REG address can be any two index registers. */
4211 /* We disallow FRAME+REG addressing since we know that FRAME
4212 will be replaced with STACK, and SP relative addressing only
4213 permits SP+OFFSET. */
4214 if (GET_MODE_SIZE (mode) <= 4
4215 && XEXP (x, 0) != frame_pointer_rtx
4216 && XEXP (x, 1) != frame_pointer_rtx
5b3e6663
PB
4217 && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
4218 && thumb1_index_register_rtx_p (XEXP (x, 1), strict_p))
76a318e9
RE
4219 return 1;
4220
4221 /* REG+const has 5-7 bit offset for non-SP registers. */
5b3e6663 4222 else if ((thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
76a318e9
RE
4223 || XEXP (x, 0) == arg_pointer_rtx)
4224 && GET_CODE (XEXP (x, 1)) == CONST_INT
4225 && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4226 return 1;
4227
a50aa827 4228 /* REG+const has 10-bit offset for SP, but only SImode and
76a318e9
RE
4229 larger is supported. */
4230 /* ??? Should probably check for DI/DFmode overflow here
4231 just like GO_IF_LEGITIMATE_OFFSET does. */
4232 else if (GET_CODE (XEXP (x, 0)) == REG
4233 && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
4234 && GET_MODE_SIZE (mode) >= 4
4235 && GET_CODE (XEXP (x, 1)) == CONST_INT
4236 && INTVAL (XEXP (x, 1)) >= 0
4237 && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
4238 && (INTVAL (XEXP (x, 1)) & 3) == 0)
4239 return 1;
4240
4241 else if (GET_CODE (XEXP (x, 0)) == REG
c5289e45
RE
4242 && (REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
4243 || REGNO (XEXP (x, 0)) == ARG_POINTER_REGNUM
4244 || (REGNO (XEXP (x, 0)) >= FIRST_VIRTUAL_REGISTER
4245 && REGNO (XEXP (x, 0)) <= LAST_VIRTUAL_REGISTER))
76a318e9
RE
4246 && GET_MODE_SIZE (mode) >= 4
4247 && GET_CODE (XEXP (x, 1)) == CONST_INT
4248 && (INTVAL (XEXP (x, 1)) & 3) == 0)
4249 return 1;
4250 }
4251
4252 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
f954388e 4253 && GET_MODE_SIZE (mode) == 4
76a318e9
RE
4254 && GET_CODE (x) == SYMBOL_REF
4255 && CONSTANT_POOL_ADDRESS_P (x)
d3585b76
DJ
4256 && ! (flag_pic
4257 && symbol_mentioned_p (get_pool_constant (x))
4258 && ! pcrel_constant_p (get_pool_constant (x))))
76a318e9
RE
4259 return 1;
4260
4261 return 0;
4262}
4263
4264/* Return nonzero if VAL can be used as an offset in a Thumb-state address
4265 instruction of mode MODE. */
4266int
e32bac5b 4267thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
76a318e9
RE
4268{
4269 switch (GET_MODE_SIZE (mode))
4270 {
4271 case 1:
4272 return val >= 0 && val < 32;
4273
4274 case 2:
4275 return val >= 0 && val < 64 && (val & 1) == 0;
4276
4277 default:
4278 return (val >= 0
4279 && (val + GET_MODE_SIZE (mode)) <= 128
4280 && (val & 3) == 0);
4281 }
4282}
4283
d3585b76
DJ
4284/* Build the SYMBOL_REF for __tls_get_addr. */
4285
4286static GTY(()) rtx tls_get_addr_libfunc;
4287
4288static rtx
4289get_tls_get_addr (void)
4290{
4291 if (!tls_get_addr_libfunc)
4292 tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
4293 return tls_get_addr_libfunc;
4294}
4295
4296static rtx
4297arm_load_tp (rtx target)
4298{
4299 if (!target)
4300 target = gen_reg_rtx (SImode);
4301
4302 if (TARGET_HARD_TP)
4303 {
4304 /* Can return in any reg. */
4305 emit_insn (gen_load_tp_hard (target));
4306 }
4307 else
4308 {
4309 /* Always returned in r0. Immediately copy the result into a pseudo,
4310 otherwise other uses of r0 (e.g. setting up function arguments) may
4311 clobber the value. */
4312
4313 rtx tmp;
4314
4315 emit_insn (gen_load_tp_soft ());
4316
4317 tmp = gen_rtx_REG (SImode, 0);
4318 emit_move_insn (target, tmp);
4319 }
4320 return target;
4321}
4322
4323static rtx
4324load_tls_operand (rtx x, rtx reg)
4325{
4326 rtx tmp;
4327
4328 if (reg == NULL_RTX)
4329 reg = gen_reg_rtx (SImode);
4330
4331 tmp = gen_rtx_CONST (SImode, x);
4332
4333 emit_move_insn (reg, tmp);
4334
4335 return reg;
4336}
4337
4338static rtx
4339arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
4340{
f16fe45f 4341 rtx insns, label, labelno, sum;
d3585b76
DJ
4342
4343 start_sequence ();
4344
f16fe45f
DJ
4345 labelno = GEN_INT (pic_labelno++);
4346 label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
4347 label = gen_rtx_CONST (VOIDmode, label);
4348
d3585b76 4349 sum = gen_rtx_UNSPEC (Pmode,
f16fe45f 4350 gen_rtvec (4, x, GEN_INT (reloc), label,
d3585b76
DJ
4351 GEN_INT (TARGET_ARM ? 8 : 4)),
4352 UNSPEC_TLS);
4353 reg = load_tls_operand (sum, reg);
4354
4355 if (TARGET_ARM)
f16fe45f 4356 emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
5b3e6663
PB
4357 else if (TARGET_THUMB2)
4358 {
4359 rtx tmp;
4360 /* Thumb-2 only allows very limited access to the PC. Calculate
4361 the address in a temporary register. */
4362 tmp = gen_reg_rtx (SImode);
4363 emit_insn (gen_pic_load_dot_plus_four (tmp, labelno));
4364 emit_insn (gen_addsi3(reg, reg, tmp));
4365 }
4366 else /* TARGET_THUMB1 */
f16fe45f 4367 emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
d3585b76
DJ
4368
4369 *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX, LCT_PURE, /* LCT_CONST? */
4370 Pmode, 1, reg, Pmode);
4371
4372 insns = get_insns ();
4373 end_sequence ();
4374
4375 return insns;
4376}
4377
4378rtx
4379legitimize_tls_address (rtx x, rtx reg)
4380{
f16fe45f 4381 rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
d3585b76
DJ
4382 unsigned int model = SYMBOL_REF_TLS_MODEL (x);
4383
4384 switch (model)
4385 {
4386 case TLS_MODEL_GLOBAL_DYNAMIC:
4387 insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
4388 dest = gen_reg_rtx (Pmode);
4389 emit_libcall_block (insns, dest, ret, x);
4390 return dest;
4391
4392 case TLS_MODEL_LOCAL_DYNAMIC:
4393 insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
4394
4395 /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
4396 share the LDM result with other LD model accesses. */
4397 eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
4398 UNSPEC_TLS);
4399 dest = gen_reg_rtx (Pmode);
e66e1c68 4400 emit_libcall_block (insns, dest, ret, eqv);
d3585b76
DJ
4401
4402 /* Load the addend. */
4403 addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (TLS_LDO32)),
4404 UNSPEC_TLS);
4405 addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
4406 return gen_rtx_PLUS (Pmode, dest, addend);
4407
4408 case TLS_MODEL_INITIAL_EXEC:
f16fe45f
DJ
4409 labelno = GEN_INT (pic_labelno++);
4410 label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
4411 label = gen_rtx_CONST (VOIDmode, label);
d3585b76 4412 sum = gen_rtx_UNSPEC (Pmode,
f16fe45f 4413 gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
d3585b76
DJ
4414 GEN_INT (TARGET_ARM ? 8 : 4)),
4415 UNSPEC_TLS);
4416 reg = load_tls_operand (sum, reg);
4417
4418 if (TARGET_ARM)
f16fe45f 4419 emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
5b3e6663
PB
4420 else if (TARGET_THUMB2)
4421 {
4422 rtx tmp;
4423 /* Thumb-2 only allows very limited access to the PC. Calculate
4424 the address in a temporary register. */
4425 tmp = gen_reg_rtx (SImode);
4426 emit_insn (gen_pic_load_dot_plus_four (tmp, labelno));
4427 emit_insn (gen_addsi3(reg, reg, tmp));
4428 emit_move_insn (reg, gen_const_mem (SImode, reg));
4429 }
d3585b76
DJ
4430 else
4431 {
f16fe45f 4432 emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
d3585b76
DJ
4433 emit_move_insn (reg, gen_const_mem (SImode, reg));
4434 }
4435
4436 tp = arm_load_tp (NULL_RTX);
4437
4438 return gen_rtx_PLUS (Pmode, tp, reg);
4439
4440 case TLS_MODEL_LOCAL_EXEC:
4441 tp = arm_load_tp (NULL_RTX);
4442
4443 reg = gen_rtx_UNSPEC (Pmode,
4444 gen_rtvec (2, x, GEN_INT (TLS_LE32)),
4445 UNSPEC_TLS);
4446 reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
4447
4448 return gen_rtx_PLUS (Pmode, tp, reg);
4449
4450 default:
4451 abort ();
4452 }
4453}
4454
ccf4d512
RE
4455/* Try machine-dependent ways of modifying an illegitimate address
4456 to be legitimate. If we find one, return the new, valid address. */
ccf4d512 4457rtx
e32bac5b 4458arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
ccf4d512 4459{
d3585b76
DJ
4460 if (arm_tls_symbol_p (x))
4461 return legitimize_tls_address (x, NULL_RTX);
4462
ccf4d512
RE
4463 if (GET_CODE (x) == PLUS)
4464 {
4465 rtx xop0 = XEXP (x, 0);
4466 rtx xop1 = XEXP (x, 1);
4467
4468 if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
4469 xop0 = force_reg (SImode, xop0);
4470
4471 if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
4472 xop1 = force_reg (SImode, xop1);
4473
4474 if (ARM_BASE_REGISTER_RTX_P (xop0)
4475 && GET_CODE (xop1) == CONST_INT)
4476 {
4477 HOST_WIDE_INT n, low_n;
4478 rtx base_reg, val;
4479 n = INTVAL (xop1);
4480
9b66ebb1
PB
4481 /* VFP addressing modes actually allow greater offsets, but for
4482 now we just stick with the lowest common denominator. */
4483 if (mode == DImode
4484 || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
ccf4d512
RE
4485 {
4486 low_n = n & 0x0f;
4487 n &= ~0x0f;
4488 if (low_n > 4)
4489 {
4490 n += 16;
4491 low_n -= 16;
4492 }
4493 }
4494 else
4495 {
4496 low_n = ((mode) == TImode ? 0
4497 : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
4498 n -= low_n;
4499 }
4500
4501 base_reg = gen_reg_rtx (SImode);
d66437c5 4502 val = force_operand (plus_constant (xop0, n), NULL_RTX);
ccf4d512 4503 emit_move_insn (base_reg, val);
d66437c5 4504 x = plus_constant (base_reg, low_n);
ccf4d512
RE
4505 }
4506 else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4507 x = gen_rtx_PLUS (SImode, xop0, xop1);
4508 }
4509
4510 /* XXX We don't allow MINUS any more -- see comment in
4511 arm_legitimate_address_p (). */
4512 else if (GET_CODE (x) == MINUS)
4513 {
4514 rtx xop0 = XEXP (x, 0);
4515 rtx xop1 = XEXP (x, 1);
4516
4517 if (CONSTANT_P (xop0))
4518 xop0 = force_reg (SImode, xop0);
4519
4520 if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
4521 xop1 = force_reg (SImode, xop1);
4522
4523 if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4524 x = gen_rtx_MINUS (SImode, xop0, xop1);
4525 }
4526
86805759
NP
4527 /* Make sure to take full advantage of the pre-indexed addressing mode
4528 with absolute addresses which often allows for the base register to
4529 be factorized for multiple adjacent memory references, and it might
4530 even allows for the mini pool to be avoided entirely. */
4531 else if (GET_CODE (x) == CONST_INT && optimize > 0)
4532 {
4533 unsigned int bits;
4534 HOST_WIDE_INT mask, base, index;
4535 rtx base_reg;
4536
a50aa827
KH
4537 /* ldr and ldrb can use a 12-bit index, ldrsb and the rest can only
4538 use a 8-bit index. So let's use a 12-bit index for SImode only and
86805759
NP
4539 hope that arm_gen_constant will enable ldrb to use more bits. */
4540 bits = (mode == SImode) ? 12 : 8;
4541 mask = (1 << bits) - 1;
4542 base = INTVAL (x) & ~mask;
4543 index = INTVAL (x) & mask;
b107d743 4544 if (bit_count (base & 0xffffffff) > (32 - bits)/2)
86805759
NP
4545 {
4546 /* It'll most probably be more efficient to generate the base
4547 with more bits set and use a negative index instead. */
4548 base |= mask;
4549 index -= mask;
4550 }
4551 base_reg = force_reg (SImode, GEN_INT (base));
d66437c5 4552 x = plus_constant (base_reg, index);
86805759
NP
4553 }
4554
ccf4d512
RE
4555 if (flag_pic)
4556 {
4557 /* We need to find and carefully transform any SYMBOL and LABEL
4558 references; so go back to the original address expression. */
4559 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4560
4561 if (new_x != orig_x)
4562 x = new_x;
4563 }
4564
4565 return x;
4566}
4567
6f5b4f3e
RE
4568
4569/* Try machine-dependent ways of modifying an illegitimate Thumb address
4570 to be legitimate. If we find one, return the new, valid address. */
4571rtx
4572thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
4573{
d3585b76
DJ
4574 if (arm_tls_symbol_p (x))
4575 return legitimize_tls_address (x, NULL_RTX);
4576
6f5b4f3e
RE
4577 if (GET_CODE (x) == PLUS
4578 && GET_CODE (XEXP (x, 1)) == CONST_INT
4579 && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
4580 || INTVAL (XEXP (x, 1)) < 0))
4581 {
4582 rtx xop0 = XEXP (x, 0);
4583 rtx xop1 = XEXP (x, 1);
4584 HOST_WIDE_INT offset = INTVAL (xop1);
4585
4586 /* Try and fold the offset into a biasing of the base register and
4587 then offsetting that. Don't do this when optimizing for space
4588 since it can cause too many CSEs. */
4589 if (optimize_size && offset >= 0
4590 && offset < 256 + 31 * GET_MODE_SIZE (mode))
4591 {
4592 HOST_WIDE_INT delta;
4593
4594 if (offset >= 256)
4595 delta = offset - (256 - GET_MODE_SIZE (mode));
4596 else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
4597 delta = 31 * GET_MODE_SIZE (mode);
4598 else
4599 delta = offset & (~31 * GET_MODE_SIZE (mode));
4600
4601 xop0 = force_operand (plus_constant (xop0, offset - delta),
4602 NULL_RTX);
4603 x = plus_constant (xop0, delta);
4604 }
4605 else if (offset < 0 && offset > -256)
4606 /* Small negative offsets are best done with a subtract before the
4607 dereference, forcing these into a register normally takes two
4608 instructions. */
4609 x = force_operand (x, NULL_RTX);
4610 else
4611 {
4612 /* For the remaining cases, force the constant into a register. */
4613 xop1 = force_reg (SImode, xop1);
4614 x = gen_rtx_PLUS (SImode, xop0, xop1);
4615 }
4616 }
4617 else if (GET_CODE (x) == PLUS
4618 && s_register_operand (XEXP (x, 1), SImode)
4619 && !s_register_operand (XEXP (x, 0), SImode))
4620 {
4621 rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
4622
4623 x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
4624 }
4625
4626 if (flag_pic)
4627 {
4628 /* We need to find and carefully transform any SYMBOL and LABEL
4629 references; so go back to the original address expression. */
4630 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4631
4632 if (new_x != orig_x)
4633 x = new_x;
4634 }
4635
4636 return x;
4637}
4638
a132dad6 4639rtx
e0b92319
NC
4640thumb_legitimize_reload_address (rtx *x_p,
4641 enum machine_mode mode,
4642 int opnum, int type,
4643 int ind_levels ATTRIBUTE_UNUSED)
a132dad6
RE
4644{
4645 rtx x = *x_p;
e0b92319 4646
a132dad6
RE
4647 if (GET_CODE (x) == PLUS
4648 && GET_MODE_SIZE (mode) < 4
4649 && REG_P (XEXP (x, 0))
4650 && XEXP (x, 0) == stack_pointer_rtx
4651 && GET_CODE (XEXP (x, 1)) == CONST_INT
4652 && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4653 {
4654 rtx orig_x = x;
4655
4656 x = copy_rtx (x);
4657 push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4658 Pmode, VOIDmode, 0, 0, opnum, type);
4659 return x;
4660 }
4661
4662 /* If both registers are hi-regs, then it's better to reload the
4663 entire expression rather than each register individually. That
4664 only requires one reload register rather than two. */
4665 if (GET_CODE (x) == PLUS
4666 && REG_P (XEXP (x, 0))
4667 && REG_P (XEXP (x, 1))
4668 && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
4669 && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
4670 {
4671 rtx orig_x = x;
4672
4673 x = copy_rtx (x);
4674 push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4675 Pmode, VOIDmode, 0, 0, opnum, type);
4676 return x;
4677 }
4678
4679 return NULL;
4680}
d3585b76
DJ
4681
4682/* Test for various thread-local symbols. */
4683
4684/* Return TRUE if X is a thread-local symbol. */
4685
4686static bool
4687arm_tls_symbol_p (rtx x)
4688{
4689 if (! TARGET_HAVE_TLS)
4690 return false;
4691
4692 if (GET_CODE (x) != SYMBOL_REF)
4693 return false;
4694
4695 return SYMBOL_REF_TLS_MODEL (x) != 0;
4696}
4697
4698/* Helper for arm_tls_referenced_p. */
4699
4700static int
4701arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
4702{
4703 if (GET_CODE (*x) == SYMBOL_REF)
4704 return SYMBOL_REF_TLS_MODEL (*x) != 0;
4705
4706 /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
4707 TLS offsets, not real symbol references. */
4708 if (GET_CODE (*x) == UNSPEC
4709 && XINT (*x, 1) == UNSPEC_TLS)
4710 return -1;
4711
4712 return 0;
4713}
4714
4715/* Return TRUE if X contains any TLS symbol references. */
4716
4717bool
4718arm_tls_referenced_p (rtx x)
4719{
4720 if (! TARGET_HAVE_TLS)
4721 return false;
4722
4723 return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
4724}
8426b956
RS
4725
4726/* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
4727
4728bool
4729arm_cannot_force_const_mem (rtx x)
4730{
4731 rtx base, offset;
4732
4733 if (ARM_OFFSETS_MUST_BE_WITHIN_SECTIONS_P)
4734 {
4735 split_const (x, &base, &offset);
4736 if (GET_CODE (base) == SYMBOL_REF
4737 && !offset_within_block_p (base, INTVAL (offset)))
4738 return true;
4739 }
4740 return arm_tls_referenced_p (x);
4741}
6b990f6b 4742\f
e2c671ba
RE
4743#define REG_OR_SUBREG_REG(X) \
4744 (GET_CODE (X) == REG \
4745 || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
4746
4747#define REG_OR_SUBREG_RTX(X) \
4748 (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
4749
d5b7b3ae
RE
4750#ifndef COSTS_N_INSNS
4751#define COSTS_N_INSNS(N) ((N) * 4 - 2)
4752#endif
3c50106f 4753static inline int
5b3e6663 4754thumb1_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
e2c671ba
RE
4755{
4756 enum machine_mode mode = GET_MODE (x);
e2c671ba 4757
9b66ebb1 4758 switch (code)
d5b7b3ae 4759 {
9b66ebb1
PB
4760 case ASHIFT:
4761 case ASHIFTRT:
4762 case LSHIFTRT:
f676971a 4763 case ROTATERT:
9b66ebb1
PB
4764 case PLUS:
4765 case MINUS:
4766 case COMPARE:
4767 case NEG:
f676971a 4768 case NOT:
9b66ebb1 4769 return COSTS_N_INSNS (1);
f676971a
EC
4770
4771 case MULT:
4772 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4773 {
4774 int cycles = 0;
9b66ebb1 4775 unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
f676971a
EC
4776
4777 while (i)
4778 {
4779 i >>= 2;
4780 cycles++;
4781 }
4782 return COSTS_N_INSNS (2) + cycles;
9b66ebb1
PB
4783 }
4784 return COSTS_N_INSNS (1) + 16;
f676971a
EC
4785
4786 case SET:
4787 return (COSTS_N_INSNS (1)
4788 + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
9b66ebb1 4789 + GET_CODE (SET_DEST (x)) == MEM));
f676971a
EC
4790
4791 case CONST_INT:
4792 if (outer == SET)
4793 {
4794 if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
4795 return 0;
4796 if (thumb_shiftable_const (INTVAL (x)))
4797 return COSTS_N_INSNS (2);
4798 return COSTS_N_INSNS (3);
4799 }
9b66ebb1 4800 else if ((outer == PLUS || outer == COMPARE)
f676971a 4801 && INTVAL (x) < 256 && INTVAL (x) > -256)
9b66ebb1
PB
4802 return 0;
4803 else if (outer == AND
4804 && INTVAL (x) < 256 && INTVAL (x) >= -256)
4805 return COSTS_N_INSNS (1);
f676971a
EC
4806 else if (outer == ASHIFT || outer == ASHIFTRT
4807 || outer == LSHIFTRT)
4808 return 0;
9b66ebb1 4809 return COSTS_N_INSNS (2);
f676971a
EC
4810
4811 case CONST:
4812 case CONST_DOUBLE:
4813 case LABEL_REF:
4814 case SYMBOL_REF:
9b66ebb1 4815 return COSTS_N_INSNS (3);
f676971a 4816
9b66ebb1
PB
4817 case UDIV:
4818 case UMOD:
4819 case DIV:
4820 case MOD:
4821 return 100;
d5b7b3ae 4822
9b66ebb1
PB
4823 case TRUNCATE:
4824 return 99;
d5b7b3ae 4825
9b66ebb1
PB
4826 case AND:
4827 case XOR:
f676971a 4828 case IOR:
ff482c8d 4829 /* XXX guess. */
9b66ebb1 4830 return 8;
d5b7b3ae 4831
9b66ebb1
PB
4832 case MEM:
4833 /* XXX another guess. */
4834 /* Memory costs quite a lot for the first word, but subsequent words
4835 load at the equivalent of a single insn each. */
4836 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4837 + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
4838 ? 4 : 0));
4839
4840 case IF_THEN_ELSE:
ff482c8d 4841 /* XXX a guess. */
9b66ebb1
PB
4842 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4843 return 14;
4844 return 2;
4845
4846 case ZERO_EXTEND:
4847 /* XXX still guessing. */
4848 switch (GET_MODE (XEXP (x, 0)))
4849 {
4850 case QImode:
4851 return (1 + (mode == DImode ? 4 : 0)
4852 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
f676971a 4853
9b66ebb1
PB
4854 case HImode:
4855 return (4 + (mode == DImode ? 4 : 0)
4856 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
f676971a 4857
9b66ebb1
PB
4858 case SImode:
4859 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
f676971a 4860
d5b7b3ae
RE
4861 default:
4862 return 99;
d5b7b3ae 4863 }
f676971a 4864
9b66ebb1
PB
4865 default:
4866 return 99;
d5b7b3ae 4867 }
9b66ebb1
PB
4868}
4869
4870
4871/* Worker routine for arm_rtx_costs. */
5b3e6663 4872/* ??? This needs updating for thumb2. */
9b66ebb1
PB
4873static inline int
4874arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
4875{
4876 enum machine_mode mode = GET_MODE (x);
4877 enum rtx_code subcode;
4878 int extra_cost;
4879
e2c671ba
RE
4880 switch (code)
4881 {
4882 case MEM:
4883 /* Memory costs quite a lot for the first word, but subsequent words
4884 load at the equivalent of a single insn each. */
4885 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
48f6efae
NC
4886 + (GET_CODE (x) == SYMBOL_REF
4887 && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
e2c671ba
RE
4888
4889 case DIV:
4890 case MOD:
b9c53150
RS
4891 case UDIV:
4892 case UMOD:
4893 return optimize_size ? COSTS_N_INSNS (2) : 100;
e2c671ba
RE
4894
4895 case ROTATE:
4896 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4897 return 4;
4898 /* Fall through */
4899 case ROTATERT:
4900 if (mode != SImode)
4901 return 8;
4902 /* Fall through */
4903 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
4904 if (mode == DImode)
4905 return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
f676971a 4906 + ((GET_CODE (XEXP (x, 0)) == REG
e2c671ba
RE
4907 || (GET_CODE (XEXP (x, 0)) == SUBREG
4908 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4909 ? 0 : 8));
4910 return (1 + ((GET_CODE (XEXP (x, 0)) == REG
4911 || (GET_CODE (XEXP (x, 0)) == SUBREG
4912 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4913 ? 0 : 4)
4914 + ((GET_CODE (XEXP (x, 1)) == REG
4915 || (GET_CODE (XEXP (x, 1)) == SUBREG
4916 && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
4917 || (GET_CODE (XEXP (x, 1)) == CONST_INT))
4918 ? 0 : 4));
4919
4920 case MINUS:
26b53f80
PB
4921 if (GET_CODE (XEXP (x, 1)) == MULT && mode == SImode && arm_arch_thumb2)
4922 {
4923 extra_cost = rtx_cost (XEXP (x, 1), code);
4924 if (!REG_OR_SUBREG_REG (XEXP (x, 0)))
4925 extra_cost += 4 * ARM_NUM_REGS (mode);
4926 return extra_cost;
4927 }
4928
e2c671ba
RE
4929 if (mode == DImode)
4930 return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
4931 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4932 || (GET_CODE (XEXP (x, 0)) == CONST_INT
4933 && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
4934 ? 0 : 8));
4935
4936 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4937 return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4938 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
9b66ebb1 4939 && arm_const_double_rtx (XEXP (x, 1))))
e2c671ba
RE
4940 ? 0 : 8)
4941 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4942 || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
9b66ebb1 4943 && arm_const_double_rtx (XEXP (x, 0))))
e2c671ba
RE
4944 ? 0 : 8));
4945
4946 if (((GET_CODE (XEXP (x, 0)) == CONST_INT
4947 && const_ok_for_arm (INTVAL (XEXP (x, 0)))
4948 && REG_OR_SUBREG_REG (XEXP (x, 1))))
4949 || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
4950 || subcode == ASHIFTRT || subcode == LSHIFTRT
4951 || subcode == ROTATE || subcode == ROTATERT
4952 || (subcode == MULT
4953 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4954 && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
4955 (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
4956 && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
4957 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
4958 || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
4959 && REG_OR_SUBREG_REG (XEXP (x, 0))))
4960 return 1;
4961 /* Fall through */
4962
f676971a 4963 case PLUS:
ff069900
PB
4964 if (arm_arch6 && mode == SImode
4965 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4966 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4967 return 1 + (GET_CODE (XEXP (XEXP (x, 0), 0)) == MEM ? 10 : 0)
4968 + (GET_CODE (XEXP (x, 1)) == MEM ? 10 : 0);
4969
26da58dd
PB
4970 if (GET_CODE (XEXP (x, 0)) == MULT)
4971 {
4972 extra_cost = rtx_cost (XEXP (x, 0), code);
4973 if (!REG_OR_SUBREG_REG (XEXP (x, 1)))
4974 extra_cost += 4 * ARM_NUM_REGS (mode);
4975 return extra_cost;
4976 }
4977
e2c671ba
RE
4978 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4979 return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4980 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4981 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
9b66ebb1 4982 && arm_const_double_rtx (XEXP (x, 1))))
e2c671ba
RE
4983 ? 0 : 8));
4984
4985 /* Fall through */
f676971a 4986 case AND: case XOR: case IOR:
e2c671ba
RE
4987 extra_cost = 0;
4988
4989 /* Normally the frame registers will be spilt into reg+const during
4990 reload, so it is a bad idea to combine them with other instructions,
4991 since then they might not be moved outside of loops. As a compromise
4992 we allow integration with ops that have a constant as their second
4993 operand. */
4994 if ((REG_OR_SUBREG_REG (XEXP (x, 0))
4995 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
4996 && GET_CODE (XEXP (x, 1)) != CONST_INT)
4997 || (REG_OR_SUBREG_REG (XEXP (x, 0))
4998 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
4999 extra_cost = 4;
5000
5001 if (mode == DImode)
5002 return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
5003 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
5004 || (GET_CODE (XEXP (x, 1)) == CONST_INT
74bbc178 5005 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
e2c671ba
RE
5006 ? 0 : 8));
5007
5008 if (REG_OR_SUBREG_REG (XEXP (x, 0)))
5009 return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
5010 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
5011 || (GET_CODE (XEXP (x, 1)) == CONST_INT
74bbc178 5012 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
e2c671ba
RE
5013 ? 0 : 4));
5014
5015 else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
5016 return (1 + extra_cost
5017 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
5018 || subcode == LSHIFTRT || subcode == ASHIFTRT
5019 || subcode == ROTATE || subcode == ROTATERT
5020 || (subcode == MULT
5021 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5022 && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
ad076f4e 5023 (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
e2c671ba
RE
5024 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
5025 && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
ad076f4e 5026 || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
e2c671ba
RE
5027 ? 0 : 4));
5028
5029 return 8;
5030
5031 case MULT:
9b66ebb1 5032 /* This should have been handled by the CPU specific routines. */
e6d29d15 5033 gcc_unreachable ();
e2c671ba 5034
56636818 5035 case TRUNCATE:
9b66ebb1 5036 if (arm_arch3m && mode == SImode
56636818
JL
5037 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
5038 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
5039 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
5040 == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
5041 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
5042 || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
5043 return 8;
5044 return 99;
5045
e2c671ba
RE
5046 case NEG:
5047 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5048 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
5049 /* Fall through */
5050 case NOT:
5051 if (mode == DImode)
5052 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
5053
5054 return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
5055
5056 case IF_THEN_ELSE:
5057 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
5058 return 14;
5059 return 2;
5060
5061 case COMPARE:
5062 return 1;
5063
5064 case ABS:
5065 return 4 + (mode == DImode ? 4 : 0);
5066
5067 case SIGN_EXTEND:
ff069900
PB
5068 if (arm_arch_thumb2 && mode == SImode)
5069 return 1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0);
5070
e2c671ba
RE
5071 if (GET_MODE (XEXP (x, 0)) == QImode)
5072 return (4 + (mode == DImode ? 4 : 0)
5073 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
5074 /* Fall through */
5075 case ZERO_EXTEND:
ff069900
PB
5076 if (arm_arch6 && mode == SImode)
5077 return 1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0);
5078
e2c671ba
RE
5079 switch (GET_MODE (XEXP (x, 0)))
5080 {
5081 case QImode:
5082 return (1 + (mode == DImode ? 4 : 0)
5083 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
5084
5085 case HImode:
5086 return (4 + (mode == DImode ? 4 : 0)
5087 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
5088
5089 case SImode:
5090 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
ad076f4e 5091
5a9335ef
NC
5092 case V8QImode:
5093 case V4HImode:
5094 case V2SImode:
5095 case V4QImode:
5096 case V2HImode:
5097 return 1;
5098
ad076f4e 5099 default:
e6d29d15 5100 gcc_unreachable ();
e2c671ba 5101 }
e6d29d15 5102 gcc_unreachable ();
e2c671ba 5103
f676971a
EC
5104 case CONST_INT:
5105 if (const_ok_for_arm (INTVAL (x)))
5106 return outer == SET ? 2 : -1;
5107 else if (outer == AND
5108 && const_ok_for_arm (~INTVAL (x)))
5109 return -1;
5110 else if ((outer == COMPARE
5111 || outer == PLUS || outer == MINUS)
5112 && const_ok_for_arm (-INTVAL (x)))
5113 return -1;
5114 else
d5b7b3ae 5115 return 5;
f676971a
EC
5116
5117 case CONST:
5118 case LABEL_REF:
5119 case SYMBOL_REF:
d5b7b3ae 5120 return 6;
f676971a
EC
5121
5122 case CONST_DOUBLE:
f1adb0a9 5123 if (arm_const_double_rtx (x) || vfp3_const_double_rtx (x))
f676971a
EC
5124 return outer == SET ? 2 : -1;
5125 else if ((outer == COMPARE || outer == PLUS)
5126 && neg_const_double_rtx_ok_for_fpa (x))
5127 return -1;
d5b7b3ae 5128 return 7;
f676971a 5129
e2c671ba
RE
5130 default:
5131 return 99;
5132 }
5133}
32de079a 5134
21b5653c
RE
5135/* RTX costs when optimizing for size. */
5136static bool
5137arm_size_rtx_costs (rtx x, int code, int outer_code, int *total)
5138{
5139 enum machine_mode mode = GET_MODE (x);
5140
09754904 5141 if (TARGET_THUMB1)
21b5653c
RE
5142 {
5143 /* XXX TBD. For now, use the standard costs. */
5b3e6663 5144 *total = thumb1_rtx_costs (x, code, outer_code);
21b5653c
RE
5145 return true;
5146 }
5147
09754904 5148 /* FIXME: This makes no attempt to prefer narrow Thumb-2 instructions. */
21b5653c
RE
5149 switch (code)
5150 {
5151 case MEM:
f676971a 5152 /* A memory access costs 1 insn if the mode is small, or the address is
21b5653c
RE
5153 a single register, otherwise it costs one insn per word. */
5154 if (REG_P (XEXP (x, 0)))
5155 *total = COSTS_N_INSNS (1);
5156 else
5157 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5158 return true;
5159
5160 case DIV:
5161 case MOD:
5162 case UDIV:
5163 case UMOD:
5164 /* Needs a libcall, so it costs about this. */
5165 *total = COSTS_N_INSNS (2);
5166 return false;
5167
5168 case ROTATE:
5169 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
5170 {
5171 *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code);
5172 return true;
5173 }
5174 /* Fall through */
5175 case ROTATERT:
5176 case ASHIFT:
5177 case LSHIFTRT:
5178 case ASHIFTRT:
5179 if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
5180 {
5181 *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code);
5182 return true;
5183 }
5184 else if (mode == SImode)
5185 {
5186 *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code);
5187 /* Slightly disparage register shifts, but not by much. */
5188 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5189 *total += 1 + rtx_cost (XEXP (x, 1), code);
5190 return true;
5191 }
5192
5193 /* Needs a libcall. */
5194 *total = COSTS_N_INSNS (2);
5195 return false;
5196
5197 case MINUS:
5198 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5199 {
5200 *total = COSTS_N_INSNS (1);
5201 return false;
5202 }
5203
5204 if (mode == SImode)
5205 {
5206 enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
5207 enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
5208
5209 if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
5210 || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
5211 || subcode1 == ROTATE || subcode1 == ROTATERT
5212 || subcode1 == ASHIFT || subcode1 == LSHIFTRT
5213 || subcode1 == ASHIFTRT)
5214 {
5215 /* It's just the cost of the two operands. */
5216 *total = 0;
5217 return false;
5218 }
5219
5220 *total = COSTS_N_INSNS (1);
5221 return false;
5222 }
5223
5224 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5225 return false;
5226
f676971a 5227 case PLUS:
21b5653c
RE
5228 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5229 {
5230 *total = COSTS_N_INSNS (1);
5231 return false;
5232 }
5233
5234 /* Fall through */
5235 case AND: case XOR: case IOR:
5236 if (mode == SImode)
5237 {
5238 enum rtx_code subcode = GET_CODE (XEXP (x, 0));
5239
5240 if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
5241 || subcode == LSHIFTRT || subcode == ASHIFTRT
5242 || (code == AND && subcode == NOT))
5243 {
5244 /* It's just the cost of the two operands. */
5245 *total = 0;
5246 return false;
5247 }
5248 }
5249
5250 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5251 return false;
5252
5253 case MULT:
5254 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5255 return false;
5256
5257 case NEG:
5258 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5259 *total = COSTS_N_INSNS (1);
5260 /* Fall through */
5261 case NOT:
5262 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5263
5264 return false;
5265
5266 case IF_THEN_ELSE:
5267 *total = 0;
5268 return false;
5269
5270 case COMPARE:
5271 if (cc_register (XEXP (x, 0), VOIDmode))
5272 * total = 0;
5273 else
5274 *total = COSTS_N_INSNS (1);
5275 return false;
5276
5277 case ABS:
5278 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5279 *total = COSTS_N_INSNS (1);
5280 else
5281 *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
5282 return false;
5283
5284 case SIGN_EXTEND:
5285 *total = 0;
5286 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
5287 {
5288 if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
5289 *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
5290 }
5291 if (mode == DImode)
5292 *total += COSTS_N_INSNS (1);
5293 return false;
5294
5295 case ZERO_EXTEND:
5296 *total = 0;
5297 if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
5298 {
5299 switch (GET_MODE (XEXP (x, 0)))
5300 {
5301 case QImode:
5302 *total += COSTS_N_INSNS (1);
5303 break;
5304
5305 case HImode:
5306 *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
f676971a 5307
21b5653c
RE
5308 case SImode:
5309 break;
5310
5311 default:
5312 *total += COSTS_N_INSNS (2);
5313 }
5314 }
5315
5316 if (mode == DImode)
5317 *total += COSTS_N_INSNS (1);
5318
5319 return false;
5320
f676971a
EC
5321 case CONST_INT:
5322 if (const_ok_for_arm (INTVAL (x)))
21b5653c
RE
5323 *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
5324 else if (const_ok_for_arm (~INTVAL (x)))
5325 *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
5326 else if (const_ok_for_arm (-INTVAL (x)))
5327 {
5328 if (outer_code == COMPARE || outer_code == PLUS
5329 || outer_code == MINUS)
5330 *total = 0;
5331 else
5332 *total = COSTS_N_INSNS (1);
5333 }
5334 else
5335 *total = COSTS_N_INSNS (2);
5336 return true;
f676971a
EC
5337
5338 case CONST:
5339 case LABEL_REF:
5340 case SYMBOL_REF:
21b5653c
RE
5341 *total = COSTS_N_INSNS (2);
5342 return true;
f676971a 5343
21b5653c
RE
5344 case CONST_DOUBLE:
5345 *total = COSTS_N_INSNS (4);
5346 return true;
5347
5348 default:
5349 if (mode != VOIDmode)
5350 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5351 else
5352 *total = COSTS_N_INSNS (4); /* How knows? */
5353 return false;
5354 }
5355}
5356
5b3e6663
PB
5357/* RTX costs for cores with a slow MUL implementation. Thumb-2 is not
5358 supported on any "slowmul" cores, so it can be ignored. */
9b66ebb1 5359
3c50106f 5360static bool
9b66ebb1 5361arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
3c50106f 5362{
9b66ebb1
PB
5363 enum machine_mode mode = GET_MODE (x);
5364
5365 if (TARGET_THUMB)
5366 {
5b3e6663 5367 *total = thumb1_rtx_costs (x, code, outer_code);
9b66ebb1
PB
5368 return true;
5369 }
f676971a 5370
9b66ebb1
PB
5371 switch (code)
5372 {
5373 case MULT:
5374 if (GET_MODE_CLASS (mode) == MODE_FLOAT
5375 || mode == DImode)
5376 {
5377 *total = 30;
5378 return true;
5379 }
5380
5381 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5382 {
5383 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
5384 & (unsigned HOST_WIDE_INT) 0xffffffff);
5385 int cost, const_ok = const_ok_for_arm (i);
5386 int j, booth_unit_size;
5387
f676971a 5388 /* Tune as appropriate. */
9b66ebb1
PB
5389 cost = const_ok ? 4 : 8;
5390 booth_unit_size = 2;
5391 for (j = 0; i && j < 32; j += booth_unit_size)
5392 {
5393 i >>= booth_unit_size;
5394 cost += 2;
5395 }
5396
5397 *total = cost;
5398 return true;
5399 }
5400
5401 *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
5402 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
5403 return true;
f676971a 5404
9b66ebb1
PB
5405 default:
5406 *total = arm_rtx_costs_1 (x, code, outer_code);
5407 return true;
5408 }
3c50106f
RH
5409}
5410
9b66ebb1
PB
5411
5412/* RTX cost for cores with a fast multiply unit (M variants). */
5413
5414static bool
5415arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
5416{
5417 enum machine_mode mode = GET_MODE (x);
5418
5b3e6663 5419 if (TARGET_THUMB1)
9b66ebb1 5420 {
5b3e6663 5421 *total = thumb1_rtx_costs (x, code, outer_code);
9b66ebb1
PB
5422 return true;
5423 }
f676971a 5424
5b3e6663 5425 /* ??? should thumb2 use different costs? */
9b66ebb1
PB
5426 switch (code)
5427 {
5428 case MULT:
5429 /* There is no point basing this on the tuning, since it is always the
5430 fast variant if it exists at all. */
5431 if (mode == DImode
5432 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5433 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5434 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5435 {
5436 *total = 8;
5437 return true;
5438 }
f676971a 5439
9b66ebb1
PB
5440
5441 if (GET_MODE_CLASS (mode) == MODE_FLOAT
5442 || mode == DImode)
5443 {
5444 *total = 30;
5445 return true;
5446 }
5447
5448 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5449 {
5450 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
5451 & (unsigned HOST_WIDE_INT) 0xffffffff);
5452 int cost, const_ok = const_ok_for_arm (i);
5453 int j, booth_unit_size;
5454
f676971a 5455 /* Tune as appropriate. */
9b66ebb1
PB
5456 cost = const_ok ? 4 : 8;
5457 booth_unit_size = 8;
5458 for (j = 0; i && j < 32; j += booth_unit_size)
5459 {
5460 i >>= booth_unit_size;
5461 cost += 2;
5462 }
5463
5464 *total = cost;
5465 return true;
5466 }
5467
5468 *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
5469 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
5470 return true;
f676971a 5471
9b66ebb1
PB
5472 default:
5473 *total = arm_rtx_costs_1 (x, code, outer_code);
5474 return true;
5475 }
5476}
5477
5478
5b3e6663
PB
5479/* RTX cost for XScale CPUs. Thumb-2 is not supported on any xscale cores,
5480 so it can be ignored. */
9b66ebb1
PB
5481
5482static bool
5483arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
5484{
5485 enum machine_mode mode = GET_MODE (x);
5486
5487 if (TARGET_THUMB)
5488 {
5b3e6663 5489 *total = thumb1_rtx_costs (x, code, outer_code);
9b66ebb1
PB
5490 return true;
5491 }
f676971a 5492
9b66ebb1
PB
5493 switch (code)
5494 {
5495 case MULT:
5496 /* There is no point basing this on the tuning, since it is always the
5497 fast variant if it exists at all. */
5498 if (mode == DImode
5499 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5500 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5501 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5502 {
5503 *total = 8;
5504 return true;
5505 }
f676971a 5506
9b66ebb1
PB
5507
5508 if (GET_MODE_CLASS (mode) == MODE_FLOAT
5509 || mode == DImode)
5510 {
5511 *total = 30;
5512 return true;
5513 }
5514
5515 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5516 {
5517 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
5518 & (unsigned HOST_WIDE_INT) 0xffffffff);
5519 int cost, const_ok = const_ok_for_arm (i);
5520 unsigned HOST_WIDE_INT masked_const;
5521
5522 /* The cost will be related to two insns.
ff482c8d 5523 First a load of the constant (MOV or LDR), then a multiply. */
9b66ebb1
PB
5524 cost = 2;
5525 if (! const_ok)
5526 cost += 1; /* LDR is probably more expensive because
ff482c8d 5527 of longer result latency. */
9b66ebb1
PB
5528 masked_const = i & 0xffff8000;
5529 if (masked_const != 0 && masked_const != 0xffff8000)
5530 {
5531 masked_const = i & 0xf8000000;
5532 if (masked_const == 0 || masked_const == 0xf8000000)
5533 cost += 1;
5534 else
5535 cost += 2;
5536 }
5537 *total = cost;
5538 return true;
5539 }
5540
5541 *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
5542 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
5543 return true;
f676971a 5544
06d5588c
ILT
5545 case COMPARE:
5546 /* A COMPARE of a MULT is slow on XScale; the muls instruction
5547 will stall until the multiplication is complete. */
5548 if (GET_CODE (XEXP (x, 0)) == MULT)
5549 *total = 4 + rtx_cost (XEXP (x, 0), code);
5550 else
5551 *total = arm_rtx_costs_1 (x, code, outer_code);
5552 return true;
5553
9b66ebb1
PB
5554 default:
5555 *total = arm_rtx_costs_1 (x, code, outer_code);
5556 return true;
5557 }
5558}
5559
5560
5561/* RTX costs for 9e (and later) cores. */
5562
5563static bool
5564arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
5565{
5566 enum machine_mode mode = GET_MODE (x);
5567 int nonreg_cost;
5568 int cost;
f676971a 5569
5b3e6663 5570 if (TARGET_THUMB1)
9b66ebb1
PB
5571 {
5572 switch (code)
5573 {
5574 case MULT:
5575 *total = COSTS_N_INSNS (3);
5576 return true;
f676971a 5577
9b66ebb1 5578 default:
5b3e6663 5579 *total = thumb1_rtx_costs (x, code, outer_code);
9b66ebb1
PB
5580 return true;
5581 }
5582 }
f676971a 5583
9b66ebb1
PB
5584 switch (code)
5585 {
5586 case MULT:
5587 /* There is no point basing this on the tuning, since it is always the
5588 fast variant if it exists at all. */
5589 if (mode == DImode
5590 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5591 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5592 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5593 {
5594 *total = 3;
5595 return true;
5596 }
f676971a 5597
9b66ebb1
PB
5598
5599 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5600 {
5601 *total = 30;
5602 return true;
5603 }
5604 if (mode == DImode)
5605 {
5606 cost = 7;
5607 nonreg_cost = 8;
5608 }
5609 else
5610 {
5611 cost = 2;
5612 nonreg_cost = 4;
5613 }
5614
5615
5616 *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
5617 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
5618 return true;
f676971a 5619
9b66ebb1
PB
5620 default:
5621 *total = arm_rtx_costs_1 (x, code, outer_code);
5622 return true;
5623 }
5624}
dcefdf67
RH
5625/* All address computations that can be done are free, but rtx cost returns
5626 the same for practically all of them. So we weight the different types
5627 of address here in the order (most pref first):
d6b4baa4 5628 PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL. */
d2b6eb76
ZW
5629static inline int
5630arm_arm_address_cost (rtx x)
5631{
5632 enum rtx_code c = GET_CODE (x);
5633
5634 if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
5635 return 0;
5636 if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
5637 return 10;
5638
5639 if (c == PLUS || c == MINUS)
5640 {
d2b6eb76
ZW
5641 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5642 return 2;
5643
ec8e098d 5644 if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
d2b6eb76
ZW
5645 return 3;
5646
5647 return 4;
5648 }
5649
5650 return 6;
5651}
5652
5653static inline int
5654arm_thumb_address_cost (rtx x)
5655{
5656 enum rtx_code c = GET_CODE (x);
5657
5658 if (c == REG)
5659 return 1;
5660 if (c == PLUS
5661 && GET_CODE (XEXP (x, 0)) == REG
5662 && GET_CODE (XEXP (x, 1)) == CONST_INT)
5663 return 1;
5664
5665 return 2;
5666}
5667
dcefdf67 5668static int
e32bac5b 5669arm_address_cost (rtx x)
dcefdf67 5670{
5b3e6663 5671 return TARGET_32BIT ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
dcefdf67
RH
5672}
5673
c237e94a 5674static int
e32bac5b 5675arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
32de079a
RE
5676{
5677 rtx i_pat, d_pat;
5678
d19fb8e3
NC
5679 /* Some true dependencies can have a higher cost depending
5680 on precisely how certain input operands are used. */
4b3c2e48 5681 if (arm_tune_xscale
d19fb8e3 5682 && REG_NOTE_KIND (link) == 0
eda833e3
BE
5683 && recog_memoized (insn) >= 0
5684 && recog_memoized (dep) >= 0)
d19fb8e3
NC
5685 {
5686 int shift_opnum = get_attr_shift (insn);
5687 enum attr_type attr_type = get_attr_type (dep);
5688
5689 /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
5690 operand for INSN. If we have a shifted input operand and the
5691 instruction we depend on is another ALU instruction, then we may
5692 have to account for an additional stall. */
9b66ebb1
PB
5693 if (shift_opnum != 0
5694 && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
d19fb8e3
NC
5695 {
5696 rtx shifted_operand;
5697 int opno;
f676971a 5698
d19fb8e3
NC
5699 /* Get the shifted operand. */
5700 extract_insn (insn);
5701 shifted_operand = recog_data.operand[shift_opnum];
5702
5703 /* Iterate over all the operands in DEP. If we write an operand
5704 that overlaps with SHIFTED_OPERAND, then we have increase the
5705 cost of this dependency. */
5706 extract_insn (dep);
5707 preprocess_constraints ();
5708 for (opno = 0; opno < recog_data.n_operands; opno++)
5709 {
5710 /* We can ignore strict inputs. */
5711 if (recog_data.operand_type[opno] == OP_IN)
5712 continue;
5713
5714 if (reg_overlap_mentioned_p (recog_data.operand[opno],
5715 shifted_operand))
5716 return 2;
5717 }
5718 }
5719 }
5720
6354dc9b 5721 /* XXX This is not strictly true for the FPA. */
d5b7b3ae
RE
5722 if (REG_NOTE_KIND (link) == REG_DEP_ANTI
5723 || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
b36ba79f
RE
5724 return 0;
5725
d5b7b3ae
RE
5726 /* Call insns don't incur a stall, even if they follow a load. */
5727 if (REG_NOTE_KIND (link) == 0
5728 && GET_CODE (insn) == CALL_INSN)
5729 return 1;
5730
32de079a
RE
5731 if ((i_pat = single_set (insn)) != NULL
5732 && GET_CODE (SET_SRC (i_pat)) == MEM
5733 && (d_pat = single_set (dep)) != NULL
5734 && GET_CODE (SET_DEST (d_pat)) == MEM)
5735 {
48f6efae 5736 rtx src_mem = XEXP (SET_SRC (i_pat), 0);
32de079a
RE
5737 /* This is a load after a store, there is no conflict if the load reads
5738 from a cached area. Assume that loads from the stack, and from the
f676971a 5739 constant pool are cached, and that others will miss. This is a
6354dc9b 5740 hack. */
f676971a 5741
48f6efae
NC
5742 if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
5743 || reg_mentioned_p (stack_pointer_rtx, src_mem)
5744 || reg_mentioned_p (frame_pointer_rtx, src_mem)
5745 || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
949d79eb 5746 return 1;
32de079a
RE
5747 }
5748
5749 return cost;
5750}
5751
9b66ebb1 5752static int fp_consts_inited = 0;
ff9940b0 5753
9b66ebb1
PB
5754/* Only zero is valid for VFP. Other values are also valid for FPA. */
5755static const char * const strings_fp[8] =
62b10bbc 5756{
2b835d68
RE
5757 "0", "1", "2", "3",
5758 "4", "5", "0.5", "10"
5759};
ff9940b0 5760
9b66ebb1 5761static REAL_VALUE_TYPE values_fp[8];
ff9940b0
RE
5762
5763static void
9b66ebb1 5764init_fp_table (void)
ff9940b0
RE
5765{
5766 int i;
5767 REAL_VALUE_TYPE r;
5768
9b66ebb1
PB
5769 if (TARGET_VFP)
5770 fp_consts_inited = 1;
5771 else
5772 fp_consts_inited = 8;
5773
5774 for (i = 0; i < fp_consts_inited; i++)
ff9940b0 5775 {
9b66ebb1
PB
5776 r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
5777 values_fp[i] = r;
ff9940b0 5778 }
ff9940b0
RE
5779}
5780
9b66ebb1 5781/* Return TRUE if rtx X is a valid immediate FP constant. */
cce8749e 5782int
9b66ebb1 5783arm_const_double_rtx (rtx x)
cce8749e 5784{
ff9940b0
RE
5785 REAL_VALUE_TYPE r;
5786 int i;
f676971a 5787
9b66ebb1
PB
5788 if (!fp_consts_inited)
5789 init_fp_table ();
f676971a 5790
ff9940b0
RE
5791 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5792 if (REAL_VALUE_MINUS_ZERO (r))
5793 return 0;
f3bb6135 5794
9b66ebb1
PB
5795 for (i = 0; i < fp_consts_inited; i++)
5796 if (REAL_VALUES_EQUAL (r, values_fp[i]))
ff9940b0 5797 return 1;
f3bb6135 5798
ff9940b0 5799 return 0;
f3bb6135 5800}
ff9940b0 5801
3b684012 5802/* Return TRUE if rtx X is a valid immediate FPA constant. */
ff9940b0 5803int
e32bac5b 5804neg_const_double_rtx_ok_for_fpa (rtx x)
ff9940b0
RE
5805{
5806 REAL_VALUE_TYPE r;
5807 int i;
f676971a 5808
9b66ebb1
PB
5809 if (!fp_consts_inited)
5810 init_fp_table ();
f676971a 5811
ff9940b0
RE
5812 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5813 r = REAL_VALUE_NEGATE (r);
5814 if (REAL_VALUE_MINUS_ZERO (r))
5815 return 0;
f3bb6135 5816
ff9940b0 5817 for (i = 0; i < 8; i++)
9b66ebb1 5818 if (REAL_VALUES_EQUAL (r, values_fp[i]))
ff9940b0 5819 return 1;
f3bb6135 5820
ff9940b0 5821 return 0;
f3bb6135 5822}
f1adb0a9
JB
5823
5824
5825/* VFPv3 has a fairly wide range of representable immediates, formed from
5826 "quarter-precision" floating-point values. These can be evaluated using this
5827 formula (with ^ for exponentiation):
5828
5829 -1^s * n * 2^-r
5830
5831 Where 's' is a sign bit (0/1), 'n' and 'r' are integers such that
5832 16 <= n <= 31 and 0 <= r <= 7.
5833
5834 These values are mapped onto an 8-bit integer ABCDEFGH s.t.
5835
5836 - A (most-significant) is the sign bit.
5837 - BCD are the exponent (encoded as r XOR 3).
5838 - EFGH are the mantissa (encoded as n - 16).
5839*/
5840
5841/* Return an integer index for a VFPv3 immediate operand X suitable for the
5842 fconst[sd] instruction, or -1 if X isn't suitable. */
5843static int
5844vfp3_const_double_index (rtx x)
5845{
5846 REAL_VALUE_TYPE r, m;
5847 int sign, exponent;
5848 unsigned HOST_WIDE_INT mantissa, mant_hi;
5849 unsigned HOST_WIDE_INT mask;
8e39e9af 5850 HOST_WIDE_INT m1, m2;
f1adb0a9
JB
5851 int point_pos = 2 * HOST_BITS_PER_WIDE_INT - 1;
5852
5853 if (!TARGET_VFP3 || GET_CODE (x) != CONST_DOUBLE)
5854 return -1;
5855
5856 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5857
5858 /* We can't represent these things, so detect them first. */
5859 if (REAL_VALUE_ISINF (r) || REAL_VALUE_ISNAN (r) || REAL_VALUE_MINUS_ZERO (r))
5860 return -1;
5861
5862 /* Extract sign, exponent and mantissa. */
5863 sign = REAL_VALUE_NEGATIVE (r) ? 1 : 0;
5864 r = REAL_VALUE_ABS (r);
5865 exponent = REAL_EXP (&r);
5866 /* For the mantissa, we expand into two HOST_WIDE_INTS, apart from the
5867 highest (sign) bit, with a fixed binary point at bit point_pos.
5868 WARNING: If there's ever a VFP version which uses more than 2 * H_W_I - 1
5869 bits for the mantissa, this may fail (low bits would be lost). */
5870 real_ldexp (&m, &r, point_pos - exponent);
8e39e9af
RE
5871 REAL_VALUE_TO_INT (&m1, &m2, m);
5872 mantissa = m1;
5873 mant_hi = m2;
f1adb0a9
JB
5874
5875 /* If there are bits set in the low part of the mantissa, we can't
5876 represent this value. */
5877 if (mantissa != 0)
5878 return -1;
5879
5880 /* Now make it so that mantissa contains the most-significant bits, and move
5881 the point_pos to indicate that the least-significant bits have been
5882 discarded. */
5883 point_pos -= HOST_BITS_PER_WIDE_INT;
5884 mantissa = mant_hi;
5885
5886 /* We can permit four significant bits of mantissa only, plus a high bit
5887 which is always 1. */
5888 mask = ((unsigned HOST_WIDE_INT)1 << (point_pos - 5)) - 1;
5889 if ((mantissa & mask) != 0)
5890 return -1;
5891
5892 /* Now we know the mantissa is in range, chop off the unneeded bits. */
5893 mantissa >>= point_pos - 5;
5894
5895 /* The mantissa may be zero. Disallow that case. (It's possible to load the
5896 floating-point immediate zero with Neon using an integer-zero load, but
5897 that case is handled elsewhere.) */
5898 if (mantissa == 0)
5899 return -1;
5900
5901 gcc_assert (mantissa >= 16 && mantissa <= 31);
5902
5903 /* The value of 5 here would be 4 if GCC used IEEE754-like encoding (where
6ed3da00
KH
5904 normalized significands are in the range [1, 2). (Our mantissa is shifted
5905 left 4 places at this point relative to normalized IEEE754 values). GCC
f1adb0a9
JB
5906 internally uses [0.5, 1) (see real.c), so the exponent returned from
5907 REAL_EXP must be altered. */
5908 exponent = 5 - exponent;
5909
5910 if (exponent < 0 || exponent > 7)
5911 return -1;
5912
5913 /* Sign, mantissa and exponent are now in the correct form to plug into the
15dc95cb 5914 formula described in the comment above. */
f1adb0a9
JB
5915 return (sign << 7) | ((exponent ^ 3) << 4) | (mantissa - 16);
5916}
5917
5918/* Return TRUE if rtx X is a valid immediate VFPv3 constant. */
5919int
5920vfp3_const_double_rtx (rtx x)
5921{
5922 if (!TARGET_VFP3)
5923 return 0;
5924
5925 return vfp3_const_double_index (x) != -1;
5926}
5927
88f77cba
JB
5928/* Recognize immediates which can be used in various Neon instructions. Legal
5929 immediates are described by the following table (for VMVN variants, the
5930 bitwise inverse of the constant shown is recognized. In either case, VMOV
5931 is output and the correct instruction to use for a given constant is chosen
5932 by the assembler). The constant shown is replicated across all elements of
5933 the destination vector.
5934
5935 insn elems variant constant (binary)
5936 ---- ----- ------- -----------------
5937 vmov i32 0 00000000 00000000 00000000 abcdefgh
5938 vmov i32 1 00000000 00000000 abcdefgh 00000000
5939 vmov i32 2 00000000 abcdefgh 00000000 00000000
5940 vmov i32 3 abcdefgh 00000000 00000000 00000000
5941 vmov i16 4 00000000 abcdefgh
5942 vmov i16 5 abcdefgh 00000000
5943 vmvn i32 6 00000000 00000000 00000000 abcdefgh
5944 vmvn i32 7 00000000 00000000 abcdefgh 00000000
5945 vmvn i32 8 00000000 abcdefgh 00000000 00000000
5946 vmvn i32 9 abcdefgh 00000000 00000000 00000000
5947 vmvn i16 10 00000000 abcdefgh
5948 vmvn i16 11 abcdefgh 00000000
5949 vmov i32 12 00000000 00000000 abcdefgh 11111111
5950 vmvn i32 13 00000000 00000000 abcdefgh 11111111
5951 vmov i32 14 00000000 abcdefgh 11111111 11111111
5952 vmvn i32 15 00000000 abcdefgh 11111111 11111111
5953 vmov i8 16 abcdefgh
5954 vmov i64 17 aaaaaaaa bbbbbbbb cccccccc dddddddd
5955 eeeeeeee ffffffff gggggggg hhhhhhhh
5956 vmov f32 18 aBbbbbbc defgh000 00000000 00000000
5957
5958 For case 18, B = !b. Representable values are exactly those accepted by
5959 vfp3_const_double_index, but are output as floating-point numbers rather
5960 than indices.
5961
5962 Variants 0-5 (inclusive) may also be used as immediates for the second
5963 operand of VORR/VBIC instructions.
5964
5965 The INVERSE argument causes the bitwise inverse of the given operand to be
5966 recognized instead (used for recognizing legal immediates for the VAND/VORN
5967 pseudo-instructions). If INVERSE is true, the value placed in *MODCONST is
5968 *not* inverted (i.e. the pseudo-instruction forms vand/vorn should still be
5969 output, rather than the real insns vbic/vorr).
5970
5971 INVERSE makes no difference to the recognition of float vectors.
5972
5973 The return value is the variant of immediate as shown in the above table, or
5974 -1 if the given value doesn't match any of the listed patterns.
5975*/
5976static int
5977neon_valid_immediate (rtx op, enum machine_mode mode, int inverse,
5978 rtx *modconst, int *elementwidth)
5979{
5980#define CHECK(STRIDE, ELSIZE, CLASS, TEST) \
5981 matches = 1; \
5982 for (i = 0; i < idx; i += (STRIDE)) \
5983 if (!(TEST)) \
5984 matches = 0; \
5985 if (matches) \
5986 { \
5987 immtype = (CLASS); \
5988 elsize = (ELSIZE); \
5989 break; \
5990 }
5991
5992 unsigned int i, elsize, idx = 0, n_elts = CONST_VECTOR_NUNITS (op);
5993 unsigned int innersize = GET_MODE_SIZE (GET_MODE_INNER (mode));
5994 unsigned char bytes[16];
5995 int immtype = -1, matches;
5996 unsigned int invmask = inverse ? 0xff : 0;
5997
5998 /* Vectors of float constants. */
5999 if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
6000 {
6001 rtx el0 = CONST_VECTOR_ELT (op, 0);
6002 REAL_VALUE_TYPE r0;
6003
6004 if (!vfp3_const_double_rtx (el0))
6005 return -1;
6006
6007 REAL_VALUE_FROM_CONST_DOUBLE (r0, el0);
6008
6009 for (i = 1; i < n_elts; i++)
6010 {
6011 rtx elt = CONST_VECTOR_ELT (op, i);
6012 REAL_VALUE_TYPE re;
6013
6014 REAL_VALUE_FROM_CONST_DOUBLE (re, elt);
6015
6016 if (!REAL_VALUES_EQUAL (r0, re))
6017 return -1;
6018 }
6019
6020 if (modconst)
6021 *modconst = CONST_VECTOR_ELT (op, 0);
6022
6023 if (elementwidth)
6024 *elementwidth = 0;
6025
6026 return 18;
6027 }
6028
6029 /* Splat vector constant out into a byte vector. */
6030 for (i = 0; i < n_elts; i++)
6031 {
6032 rtx el = CONST_VECTOR_ELT (op, i);
6033 unsigned HOST_WIDE_INT elpart;
6034 unsigned int part, parts;
6035
6036 if (GET_CODE (el) == CONST_INT)
6037 {
6038 elpart = INTVAL (el);
6039 parts = 1;
6040 }
6041 else if (GET_CODE (el) == CONST_DOUBLE)
6042 {
6043 elpart = CONST_DOUBLE_LOW (el);
6044 parts = 2;
6045 }
6046 else
6047 gcc_unreachable ();
6048
6049 for (part = 0; part < parts; part++)
6050 {
6051 unsigned int byte;
6052 for (byte = 0; byte < innersize; byte++)
6053 {
6054 bytes[idx++] = (elpart & 0xff) ^ invmask;
6055 elpart >>= BITS_PER_UNIT;
6056 }
6057 if (GET_CODE (el) == CONST_DOUBLE)
6058 elpart = CONST_DOUBLE_HIGH (el);
6059 }
6060 }
6061
6062 /* Sanity check. */
6063 gcc_assert (idx == GET_MODE_SIZE (mode));
6064
6065 do
6066 {
6067 CHECK (4, 32, 0, bytes[i] == bytes[0] && bytes[i + 1] == 0
6068 && bytes[i + 2] == 0 && bytes[i + 3] == 0);
6069
6070 CHECK (4, 32, 1, bytes[i] == 0 && bytes[i + 1] == bytes[1]
6071 && bytes[i + 2] == 0 && bytes[i + 3] == 0);
6072
6073 CHECK (4, 32, 2, bytes[i] == 0 && bytes[i + 1] == 0
6074 && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
6075
6076 CHECK (4, 32, 3, bytes[i] == 0 && bytes[i + 1] == 0
6077 && bytes[i + 2] == 0 && bytes[i + 3] == bytes[3]);
6078
6079 CHECK (2, 16, 4, bytes[i] == bytes[0] && bytes[i + 1] == 0);
6080
6081 CHECK (2, 16, 5, bytes[i] == 0 && bytes[i + 1] == bytes[1]);
6082
6083 CHECK (4, 32, 6, bytes[i] == bytes[0] && bytes[i + 1] == 0xff
6084 && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
6085
6086 CHECK (4, 32, 7, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
6087 && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
6088
6089 CHECK (4, 32, 8, bytes[i] == 0xff && bytes[i + 1] == 0xff
6090 && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
6091
6092 CHECK (4, 32, 9, bytes[i] == 0xff && bytes[i + 1] == 0xff
6093 && bytes[i + 2] == 0xff && bytes[i + 3] == bytes[3]);
6094
6095 CHECK (2, 16, 10, bytes[i] == bytes[0] && bytes[i + 1] == 0xff);
6096
6097 CHECK (2, 16, 11, bytes[i] == 0xff && bytes[i + 1] == bytes[1]);
6098
6099 CHECK (4, 32, 12, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
6100 && bytes[i + 2] == 0 && bytes[i + 3] == 0);
6101
6102 CHECK (4, 32, 13, bytes[i] == 0 && bytes[i + 1] == bytes[1]
6103 && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
6104
6105 CHECK (4, 32, 14, bytes[i] == 0xff && bytes[i + 1] == 0xff
6106 && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
6107
6108 CHECK (4, 32, 15, bytes[i] == 0 && bytes[i + 1] == 0
6109 && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
6110
6111 CHECK (1, 8, 16, bytes[i] == bytes[0]);
6112
6113 CHECK (1, 64, 17, (bytes[i] == 0 || bytes[i] == 0xff)
6114 && bytes[i] == bytes[(i + 8) % idx]);
6115 }
6116 while (0);
6117
6118 if (immtype == -1)
6119 return -1;
6120
6121 if (elementwidth)
6122 *elementwidth = elsize;
6123
6124 if (modconst)
6125 {
6126 unsigned HOST_WIDE_INT imm = 0;
6127
cea618ac 6128 /* Un-invert bytes of recognized vector, if necessary. */
88f77cba
JB
6129 if (invmask != 0)
6130 for (i = 0; i < idx; i++)
6131 bytes[i] ^= invmask;
6132
6133 if (immtype == 17)
6134 {
6135 /* FIXME: Broken on 32-bit H_W_I hosts. */
6136 gcc_assert (sizeof (HOST_WIDE_INT) == 8);
6137
6138 for (i = 0; i < 8; i++)
6139 imm |= (unsigned HOST_WIDE_INT) (bytes[i] ? 0xff : 0)
6140 << (i * BITS_PER_UNIT);
6141
6142 *modconst = GEN_INT (imm);
6143 }
6144 else
6145 {
6146 unsigned HOST_WIDE_INT imm = 0;
6147
6148 for (i = 0; i < elsize / BITS_PER_UNIT; i++)
6149 imm |= (unsigned HOST_WIDE_INT) bytes[i] << (i * BITS_PER_UNIT);
6150
6151 *modconst = GEN_INT (imm);
6152 }
6153 }
6154
6155 return immtype;
6156#undef CHECK
6157}
6158
6159/* Return TRUE if rtx X is legal for use as either a Neon VMOV (or, implicitly,
6160 VMVN) immediate. Write back width per element to *ELEMENTWIDTH (or zero for
6161 float elements), and a modified constant (whatever should be output for a
6162 VMOV) in *MODCONST. */
6163
6164int
6165neon_immediate_valid_for_move (rtx op, enum machine_mode mode,
6166 rtx *modconst, int *elementwidth)
6167{
6168 rtx tmpconst;
6169 int tmpwidth;
6170 int retval = neon_valid_immediate (op, mode, 0, &tmpconst, &tmpwidth);
6171
6172 if (retval == -1)
6173 return 0;
6174
6175 if (modconst)
6176 *modconst = tmpconst;
6177
6178 if (elementwidth)
6179 *elementwidth = tmpwidth;
6180
6181 return 1;
6182}
6183
6184/* Return TRUE if rtx X is legal for use in a VORR or VBIC instruction. If
6185 the immediate is valid, write a constant suitable for using as an operand
6186 to VORR/VBIC/VAND/VORN to *MODCONST and the corresponding element width to
6187 *ELEMENTWIDTH. See neon_valid_immediate for description of INVERSE. */
6188
6189int
6190neon_immediate_valid_for_logic (rtx op, enum machine_mode mode, int inverse,
6191 rtx *modconst, int *elementwidth)
6192{
6193 rtx tmpconst;
6194 int tmpwidth;
6195 int retval = neon_valid_immediate (op, mode, inverse, &tmpconst, &tmpwidth);
6196
6197 if (retval < 0 || retval > 5)
6198 return 0;
6199
6200 if (modconst)
6201 *modconst = tmpconst;
6202
6203 if (elementwidth)
6204 *elementwidth = tmpwidth;
6205
6206 return 1;
6207}
6208
6209/* Return a string suitable for output of Neon immediate logic operation
6210 MNEM. */
6211
6212char *
6213neon_output_logic_immediate (const char *mnem, rtx *op2, enum machine_mode mode,
6214 int inverse, int quad)
6215{
6216 int width, is_valid;
6217 static char templ[40];
6218
6219 is_valid = neon_immediate_valid_for_logic (*op2, mode, inverse, op2, &width);
6220
6221 gcc_assert (is_valid != 0);
6222
6223 if (quad)
6224 sprintf (templ, "%s.i%d\t%%q0, %%2", mnem, width);
6225 else
6226 sprintf (templ, "%s.i%d\t%%P0, %%2", mnem, width);
6227
6228 return templ;
6229}
6230
6231/* Output a sequence of pairwise operations to implement a reduction.
6232 NOTE: We do "too much work" here, because pairwise operations work on two
6233 registers-worth of operands in one go. Unfortunately we can't exploit those
6234 extra calculations to do the full operation in fewer steps, I don't think.
6235 Although all vector elements of the result but the first are ignored, we
6236 actually calculate the same result in each of the elements. An alternative
6237 such as initially loading a vector with zero to use as each of the second
6238 operands would use up an additional register and take an extra instruction,
6239 for no particular gain. */
6240
6241void
6242neon_pairwise_reduce (rtx op0, rtx op1, enum machine_mode mode,
6243 rtx (*reduc) (rtx, rtx, rtx))
6244{
6245 enum machine_mode inner = GET_MODE_INNER (mode);
6246 unsigned int i, parts = GET_MODE_SIZE (mode) / GET_MODE_SIZE (inner);
6247 rtx tmpsum = op1;
6248
6249 for (i = parts / 2; i >= 1; i /= 2)
6250 {
6251 rtx dest = (i == 1) ? op0 : gen_reg_rtx (mode);
6252 emit_insn (reduc (dest, tmpsum, tmpsum));
6253 tmpsum = dest;
6254 }
6255}
6256
cea618ac 6257/* Initialize a vector with non-constant elements. FIXME: We can do better
88f77cba
JB
6258 than the current implementation (building a vector on the stack and then
6259 loading it) in many cases. See rs6000.c. */
6260
6261void
6262neon_expand_vector_init (rtx target, rtx vals)
6263{
6264 enum machine_mode mode = GET_MODE (target);
6265 enum machine_mode inner = GET_MODE_INNER (mode);
6266 unsigned int i, n_elts = GET_MODE_NUNITS (mode);
6267 rtx mem;
6268
6269 gcc_assert (VECTOR_MODE_P (mode));
6270
6271 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), 0);
6272 for (i = 0; i < n_elts; i++)
6273 emit_move_insn (adjust_address_nv (mem, inner, i * GET_MODE_SIZE (inner)),
6274 XVECEXP (vals, 0, i));
6275
6276 emit_move_insn (target, mem);
6277}
6278
b617fc71
JB
6279/* Ensure OPERAND lies between LOW (inclusive) and HIGH (exclusive). Raise
6280 ERR if it doesn't. FIXME: NEON bounds checks occur late in compilation, so
6281 reported source locations are bogus. */
6282
6283static void
6284bounds_check (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high,
6285 const char *err)
6286{
6287 HOST_WIDE_INT lane;
6288
6289 gcc_assert (GET_CODE (operand) == CONST_INT);
6290
6291 lane = INTVAL (operand);
6292
6293 if (lane < low || lane >= high)
6294 error (err);
6295}
6296
6297/* Bounds-check lanes. */
6298
6299void
6300neon_lane_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
6301{
6302 bounds_check (operand, low, high, "lane out of range");
6303}
6304
6305/* Bounds-check constants. */
6306
6307void
6308neon_const_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
6309{
6310 bounds_check (operand, low, high, "constant out of range");
6311}
6312
6313HOST_WIDE_INT
6314neon_element_bits (enum machine_mode mode)
6315{
6316 if (mode == DImode)
6317 return GET_MODE_BITSIZE (mode);
6318 else
6319 return GET_MODE_BITSIZE (GET_MODE_INNER (mode));
6320}
6321
cce8749e
CH
6322\f
6323/* Predicates for `match_operand' and `match_operator'. */
6324
9b6b54e2 6325/* Return nonzero if OP is a valid Cirrus memory address pattern. */
9b6b54e2 6326int
e32bac5b 6327cirrus_memory_offset (rtx op)
9b6b54e2
NC
6328{
6329 /* Reject eliminable registers. */
6330 if (! (reload_in_progress || reload_completed)
6331 && ( reg_mentioned_p (frame_pointer_rtx, op)
6332 || reg_mentioned_p (arg_pointer_rtx, op)
6333 || reg_mentioned_p (virtual_incoming_args_rtx, op)
6334 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
6335 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
6336 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
6337 return 0;
6338
6339 if (GET_CODE (op) == MEM)
6340 {
6341 rtx ind;
6342
6343 ind = XEXP (op, 0);
6344
6345 /* Match: (mem (reg)). */
6346 if (GET_CODE (ind) == REG)
6347 return 1;
6348
6349 /* Match:
6350 (mem (plus (reg)
6351 (const))). */
6352 if (GET_CODE (ind) == PLUS
6353 && GET_CODE (XEXP (ind, 0)) == REG
6354 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
6355 && GET_CODE (XEXP (ind, 1)) == CONST_INT)
6356 return 1;
6357 }
6358
6359 return 0;
6360}
6361
f26b8ec9 6362/* Return TRUE if OP is a valid coprocessor memory address pattern.
5b3e6663
PB
6363 WB is true if full writeback address modes are allowed and is false
6364 if limited writeback address modes (POST_INC and PRE_DEC) are
6365 allowed. */
9b66ebb1
PB
6366
6367int
fdd695fd 6368arm_coproc_mem_operand (rtx op, bool wb)
9b66ebb1 6369{
fdd695fd 6370 rtx ind;
9b66ebb1 6371
fdd695fd 6372 /* Reject eliminable registers. */
9b66ebb1
PB
6373 if (! (reload_in_progress || reload_completed)
6374 && ( reg_mentioned_p (frame_pointer_rtx, op)
6375 || reg_mentioned_p (arg_pointer_rtx, op)
6376 || reg_mentioned_p (virtual_incoming_args_rtx, op)
6377 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
6378 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
6379 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
6380 return FALSE;
6381
59b9a953 6382 /* Constants are converted into offsets from labels. */
fdd695fd
PB
6383 if (GET_CODE (op) != MEM)
6384 return FALSE;
9b66ebb1 6385
fdd695fd 6386 ind = XEXP (op, 0);
9b66ebb1 6387
fdd695fd
PB
6388 if (reload_completed
6389 && (GET_CODE (ind) == LABEL_REF
6390 || (GET_CODE (ind) == CONST
6391 && GET_CODE (XEXP (ind, 0)) == PLUS
6392 && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
6393 && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
6394 return TRUE;
9b66ebb1 6395
fdd695fd
PB
6396 /* Match: (mem (reg)). */
6397 if (GET_CODE (ind) == REG)
6398 return arm_address_register_rtx_p (ind, 0);
6399
5b3e6663
PB
6400 /* Autoincremment addressing modes. POST_INC and PRE_DEC are
6401 acceptable in any case (subject to verification by
6402 arm_address_register_rtx_p). We need WB to be true to accept
6403 PRE_INC and POST_DEC. */
6404 if (GET_CODE (ind) == POST_INC
6405 || GET_CODE (ind) == PRE_DEC
6406 || (wb
6407 && (GET_CODE (ind) == PRE_INC
6408 || GET_CODE (ind) == POST_DEC)))
fdd695fd
PB
6409 return arm_address_register_rtx_p (XEXP (ind, 0), 0);
6410
6411 if (wb
6412 && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
6413 && arm_address_register_rtx_p (XEXP (ind, 0), 0)
6414 && GET_CODE (XEXP (ind, 1)) == PLUS
6415 && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
6416 ind = XEXP (ind, 1);
6417
6418 /* Match:
6419 (plus (reg)
6420 (const)). */
6421 if (GET_CODE (ind) == PLUS
6422 && GET_CODE (XEXP (ind, 0)) == REG
6423 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
6424 && GET_CODE (XEXP (ind, 1)) == CONST_INT
6425 && INTVAL (XEXP (ind, 1)) > -1024
6426 && INTVAL (XEXP (ind, 1)) < 1024
6427 && (INTVAL (XEXP (ind, 1)) & 3) == 0)
6428 return TRUE;
9b66ebb1
PB
6429
6430 return FALSE;
6431}
6432
88f77cba
JB
6433/* Return TRUE if OP is a memory operand which we can load or store a vector
6434 to/from. If CORE is true, we're moving from ARM registers not Neon
6435 registers. */
6436int
6437neon_vector_mem_operand (rtx op, bool core)
6438{
6439 rtx ind;
6440
6441 /* Reject eliminable registers. */
6442 if (! (reload_in_progress || reload_completed)
6443 && ( reg_mentioned_p (frame_pointer_rtx, op)
6444 || reg_mentioned_p (arg_pointer_rtx, op)
6445 || reg_mentioned_p (virtual_incoming_args_rtx, op)
6446 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
6447 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
6448 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
6449 return FALSE;
6450
6451 /* Constants are converted into offsets from labels. */
6452 if (GET_CODE (op) != MEM)
6453 return FALSE;
6454
6455 ind = XEXP (op, 0);
6456
6457 if (reload_completed
6458 && (GET_CODE (ind) == LABEL_REF
6459 || (GET_CODE (ind) == CONST
6460 && GET_CODE (XEXP (ind, 0)) == PLUS
6461 && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
6462 && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
6463 return TRUE;
6464
6465 /* Match: (mem (reg)). */
6466 if (GET_CODE (ind) == REG)
6467 return arm_address_register_rtx_p (ind, 0);
6468
6469 /* Allow post-increment with Neon registers. */
6470 if (!core && GET_CODE (ind) == POST_INC)
6471 return arm_address_register_rtx_p (XEXP (ind, 0), 0);
6472
6473#if 0
6474 /* FIXME: We can support this too if we use VLD1/VST1. */
6475 if (!core
6476 && GET_CODE (ind) == POST_MODIFY
6477 && arm_address_register_rtx_p (XEXP (ind, 0), 0)
6478 && GET_CODE (XEXP (ind, 1)) == PLUS
6479 && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
6480 ind = XEXP (ind, 1);
6481#endif
6482
6483 /* Match:
6484 (plus (reg)
6485 (const)). */
6486 if (!core
6487 && GET_CODE (ind) == PLUS
6488 && GET_CODE (XEXP (ind, 0)) == REG
6489 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
6490 && GET_CODE (XEXP (ind, 1)) == CONST_INT
6491 && INTVAL (XEXP (ind, 1)) > -1024
6492 && INTVAL (XEXP (ind, 1)) < 1016
6493 && (INTVAL (XEXP (ind, 1)) & 3) == 0)
6494 return TRUE;
6495
6496 return FALSE;
6497}
6498
6499/* Return TRUE if OP is a mem suitable for loading/storing a Neon struct
6500 type. */
6501int
6502neon_struct_mem_operand (rtx op)
6503{
6504 rtx ind;
6505
6506 /* Reject eliminable registers. */
6507 if (! (reload_in_progress || reload_completed)
6508 && ( reg_mentioned_p (frame_pointer_rtx, op)
6509 || reg_mentioned_p (arg_pointer_rtx, op)
6510 || reg_mentioned_p (virtual_incoming_args_rtx, op)
6511 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
6512 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
6513 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
6514 return FALSE;
6515
6516 /* Constants are converted into offsets from labels. */
6517 if (GET_CODE (op) != MEM)
6518 return FALSE;
6519
6520 ind = XEXP (op, 0);
6521
6522 if (reload_completed
6523 && (GET_CODE (ind) == LABEL_REF
6524 || (GET_CODE (ind) == CONST
6525 && GET_CODE (XEXP (ind, 0)) == PLUS
6526 && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
6527 && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
6528 return TRUE;
6529
6530 /* Match: (mem (reg)). */
6531 if (GET_CODE (ind) == REG)
6532 return arm_address_register_rtx_p (ind, 0);
6533
6534 return FALSE;
6535}
6536
6555b6bd
RE
6537/* Return true if X is a register that will be eliminated later on. */
6538int
6539arm_eliminable_register (rtx x)
6540{
6541 return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
6542 || REGNO (x) == ARG_POINTER_REGNUM
6543 || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
6544 && REGNO (x) <= LAST_VIRTUAL_REGISTER));
6545}
9b66ebb1 6546
9b66ebb1 6547/* Return GENERAL_REGS if a scratch register required to reload x to/from
fe2d934b 6548 coprocessor registers. Otherwise return NO_REGS. */
9b66ebb1
PB
6549
6550enum reg_class
fe2d934b 6551coproc_secondary_reload_class (enum machine_mode mode, rtx x, bool wb)
9b66ebb1 6552{
88f77cba
JB
6553 if (TARGET_NEON
6554 && (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
6555 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
6556 && neon_vector_mem_operand (x, FALSE))
6557 return NO_REGS;
6558
fe2d934b 6559 if (arm_coproc_mem_operand (x, wb) || s_register_operand (x, mode))
9b66ebb1
PB
6560 return NO_REGS;
6561
6562 return GENERAL_REGS;
6563}
6564
866af8a9
JB
6565/* Values which must be returned in the most-significant end of the return
6566 register. */
6567
6568static bool
586de218 6569arm_return_in_msb (const_tree valtype)
866af8a9
JB
6570{
6571 return (TARGET_AAPCS_BASED
6572 && BYTES_BIG_ENDIAN
6573 && (AGGREGATE_TYPE_P (valtype)
6574 || TREE_CODE (valtype) == COMPLEX_TYPE));
6575}
9b66ebb1 6576
f0375c66
NC
6577/* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
6578 Use by the Cirrus Maverick code which has to workaround
6579 a hardware bug triggered by such instructions. */
f0375c66 6580static bool
e32bac5b 6581arm_memory_load_p (rtx insn)
9b6b54e2
NC
6582{
6583 rtx body, lhs, rhs;;
6584
f0375c66
NC
6585 if (insn == NULL_RTX || GET_CODE (insn) != INSN)
6586 return false;
9b6b54e2
NC
6587
6588 body = PATTERN (insn);
6589
6590 if (GET_CODE (body) != SET)
f0375c66 6591 return false;
9b6b54e2
NC
6592
6593 lhs = XEXP (body, 0);
6594 rhs = XEXP (body, 1);
6595
f0375c66
NC
6596 lhs = REG_OR_SUBREG_RTX (lhs);
6597
6598 /* If the destination is not a general purpose
6599 register we do not have to worry. */
6600 if (GET_CODE (lhs) != REG
6601 || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
6602 return false;
6603
6604 /* As well as loads from memory we also have to react
6605 to loads of invalid constants which will be turned
6606 into loads from the minipool. */
6607 return (GET_CODE (rhs) == MEM
6608 || GET_CODE (rhs) == SYMBOL_REF
6609 || note_invalid_constants (insn, -1, false));
9b6b54e2
NC
6610}
6611
f0375c66 6612/* Return TRUE if INSN is a Cirrus instruction. */
f0375c66 6613static bool
e32bac5b 6614arm_cirrus_insn_p (rtx insn)
9b6b54e2
NC
6615{
6616 enum attr_cirrus attr;
6617
e6d29d15 6618 /* get_attr cannot accept USE or CLOBBER. */
9b6b54e2
NC
6619 if (!insn
6620 || GET_CODE (insn) != INSN
6621 || GET_CODE (PATTERN (insn)) == USE
6622 || GET_CODE (PATTERN (insn)) == CLOBBER)
6623 return 0;
6624
6625 attr = get_attr_cirrus (insn);
6626
f0375c66 6627 return attr != CIRRUS_NOT;
9b6b54e2
NC
6628}
6629
6630/* Cirrus reorg for invalid instruction combinations. */
9b6b54e2 6631static void
e32bac5b 6632cirrus_reorg (rtx first)
9b6b54e2
NC
6633{
6634 enum attr_cirrus attr;
6635 rtx body = PATTERN (first);
6636 rtx t;
6637 int nops;
6638
6639 /* Any branch must be followed by 2 non Cirrus instructions. */
6640 if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
6641 {
6642 nops = 0;
6643 t = next_nonnote_insn (first);
6644
f0375c66 6645 if (arm_cirrus_insn_p (t))
9b6b54e2
NC
6646 ++ nops;
6647
f0375c66 6648 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
9b6b54e2
NC
6649 ++ nops;
6650
6651 while (nops --)
6652 emit_insn_after (gen_nop (), first);
6653
6654 return;
6655 }
6656
6657 /* (float (blah)) is in parallel with a clobber. */
6658 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
6659 body = XVECEXP (body, 0, 0);
6660
6661 if (GET_CODE (body) == SET)
6662 {
6663 rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
6664
6665 /* cfldrd, cfldr64, cfstrd, cfstr64 must
6666 be followed by a non Cirrus insn. */
6667 if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
6668 {
f0375c66 6669 if (arm_cirrus_insn_p (next_nonnote_insn (first)))
9b6b54e2
NC
6670 emit_insn_after (gen_nop (), first);
6671
6672 return;
6673 }
f0375c66 6674 else if (arm_memory_load_p (first))
9b6b54e2
NC
6675 {
6676 unsigned int arm_regno;
6677
6678 /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
6679 ldr/cfmv64hr combination where the Rd field is the same
6680 in both instructions must be split with a non Cirrus
6681 insn. Example:
6682
6683 ldr r0, blah
6684 nop
6685 cfmvsr mvf0, r0. */
6686
6687 /* Get Arm register number for ldr insn. */
6688 if (GET_CODE (lhs) == REG)
6689 arm_regno = REGNO (lhs);
9b6b54e2 6690 else
e6d29d15
NS
6691 {
6692 gcc_assert (GET_CODE (rhs) == REG);
6693 arm_regno = REGNO (rhs);
6694 }
9b6b54e2
NC
6695
6696 /* Next insn. */
6697 first = next_nonnote_insn (first);
6698
f0375c66 6699 if (! arm_cirrus_insn_p (first))
9b6b54e2
NC
6700 return;
6701
6702 body = PATTERN (first);
6703
6704 /* (float (blah)) is in parallel with a clobber. */
6705 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
6706 body = XVECEXP (body, 0, 0);
6707
6708 if (GET_CODE (body) == FLOAT)
6709 body = XEXP (body, 0);
6710
6711 if (get_attr_cirrus (first) == CIRRUS_MOVE
6712 && GET_CODE (XEXP (body, 1)) == REG
6713 && arm_regno == REGNO (XEXP (body, 1)))
6714 emit_insn_after (gen_nop (), first);
6715
6716 return;
6717 }
6718 }
6719
e6d29d15 6720 /* get_attr cannot accept USE or CLOBBER. */
9b6b54e2
NC
6721 if (!first
6722 || GET_CODE (first) != INSN
6723 || GET_CODE (PATTERN (first)) == USE
6724 || GET_CODE (PATTERN (first)) == CLOBBER)
6725 return;
6726
6727 attr = get_attr_cirrus (first);
6728
6729 /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
6730 must be followed by a non-coprocessor instruction. */
6731 if (attr == CIRRUS_COMPARE)
6732 {
6733 nops = 0;
6734
6735 t = next_nonnote_insn (first);
6736
f0375c66 6737 if (arm_cirrus_insn_p (t))
9b6b54e2
NC
6738 ++ nops;
6739
f0375c66 6740 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
9b6b54e2
NC
6741 ++ nops;
6742
6743 while (nops --)
6744 emit_insn_after (gen_nop (), first);
6745
6746 return;
6747 }
6748}
6749
2b835d68
RE
6750/* Return TRUE if X references a SYMBOL_REF. */
6751int
e32bac5b 6752symbol_mentioned_p (rtx x)
2b835d68 6753{
1d6e90ac
NC
6754 const char * fmt;
6755 int i;
2b835d68
RE
6756
6757 if (GET_CODE (x) == SYMBOL_REF)
6758 return 1;
6759
d3585b76
DJ
6760 /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
6761 are constant offsets, not symbols. */
6762 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
6763 return 0;
6764
2b835d68 6765 fmt = GET_RTX_FORMAT (GET_CODE (x));
f676971a 6766
2b835d68
RE
6767 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6768 {
6769 if (fmt[i] == 'E')
6770 {
1d6e90ac 6771 int j;
2b835d68
RE
6772
6773 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6774 if (symbol_mentioned_p (XVECEXP (x, i, j)))
6775 return 1;
6776 }
6777 else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
6778 return 1;
6779 }
6780
6781 return 0;
6782}
6783
6784/* Return TRUE if X references a LABEL_REF. */
6785int
e32bac5b 6786label_mentioned_p (rtx x)
2b835d68 6787{
1d6e90ac
NC
6788 const char * fmt;
6789 int i;
2b835d68
RE
6790
6791 if (GET_CODE (x) == LABEL_REF)
6792 return 1;
6793
d3585b76
DJ
6794 /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
6795 instruction, but they are constant offsets, not symbols. */
6796 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
6797 return 0;
6798
2b835d68
RE
6799 fmt = GET_RTX_FORMAT (GET_CODE (x));
6800 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6801 {
6802 if (fmt[i] == 'E')
6803 {
1d6e90ac 6804 int j;
2b835d68
RE
6805
6806 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6807 if (label_mentioned_p (XVECEXP (x, i, j)))
6808 return 1;
6809 }
6810 else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
6811 return 1;
6812 }
6813
6814 return 0;
6815}
6816
d3585b76
DJ
6817int
6818tls_mentioned_p (rtx x)
6819{
6820 switch (GET_CODE (x))
6821 {
6822 case CONST:
6823 return tls_mentioned_p (XEXP (x, 0));
6824
6825 case UNSPEC:
6826 if (XINT (x, 1) == UNSPEC_TLS)
6827 return 1;
6828
6829 default:
6830 return 0;
6831 }
6832}
6833
6834/* Must not copy a SET whose source operand is PC-relative. */
6835
6836static bool
6837arm_cannot_copy_insn_p (rtx insn)
6838{
6839 rtx pat = PATTERN (insn);
6840
10ae7c7b 6841 if (GET_CODE (pat) == SET)
d3585b76 6842 {
10ae7c7b 6843 rtx rhs = SET_SRC (pat);
d3585b76
DJ
6844
6845 if (GET_CODE (rhs) == UNSPEC
6846 && XINT (rhs, 1) == UNSPEC_PIC_BASE)
6847 return TRUE;
6848
6849 if (GET_CODE (rhs) == MEM
6850 && GET_CODE (XEXP (rhs, 0)) == UNSPEC
6851 && XINT (XEXP (rhs, 0), 1) == UNSPEC_PIC_BASE)
6852 return TRUE;
6853 }
6854
6855 return FALSE;
6856}
6857
ff9940b0 6858enum rtx_code
e32bac5b 6859minmax_code (rtx x)
ff9940b0
RE
6860{
6861 enum rtx_code code = GET_CODE (x);
6862
e6d29d15
NS
6863 switch (code)
6864 {
6865 case SMAX:
6866 return GE;
6867 case SMIN:
6868 return LE;
6869 case UMIN:
6870 return LEU;
6871 case UMAX:
6872 return GEU;
6873 default:
6874 gcc_unreachable ();
6875 }
ff9940b0
RE
6876}
6877
6354dc9b 6878/* Return 1 if memory locations are adjacent. */
f3bb6135 6879int
e32bac5b 6880adjacent_mem_locations (rtx a, rtx b)
ff9940b0 6881{
15b5c4c1
RE
6882 /* We don't guarantee to preserve the order of these memory refs. */
6883 if (volatile_refs_p (a) || volatile_refs_p (b))
6884 return 0;
6885
ff9940b0
RE
6886 if ((GET_CODE (XEXP (a, 0)) == REG
6887 || (GET_CODE (XEXP (a, 0)) == PLUS
6888 && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
6889 && (GET_CODE (XEXP (b, 0)) == REG
6890 || (GET_CODE (XEXP (b, 0)) == PLUS
6891 && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
6892 {
6555b6bd
RE
6893 HOST_WIDE_INT val0 = 0, val1 = 0;
6894 rtx reg0, reg1;
6895 int val_diff;
f676971a 6896
ff9940b0
RE
6897 if (GET_CODE (XEXP (a, 0)) == PLUS)
6898 {
6555b6bd 6899 reg0 = XEXP (XEXP (a, 0), 0);
ff9940b0
RE
6900 val0 = INTVAL (XEXP (XEXP (a, 0), 1));
6901 }
6902 else
6555b6bd 6903 reg0 = XEXP (a, 0);
1d6e90ac 6904
ff9940b0
RE
6905 if (GET_CODE (XEXP (b, 0)) == PLUS)
6906 {
6555b6bd 6907 reg1 = XEXP (XEXP (b, 0), 0);
ff9940b0
RE
6908 val1 = INTVAL (XEXP (XEXP (b, 0), 1));
6909 }
6910 else
6555b6bd 6911 reg1 = XEXP (b, 0);
1d6e90ac 6912
e32bac5b
RE
6913 /* Don't accept any offset that will require multiple
6914 instructions to handle, since this would cause the
6915 arith_adjacentmem pattern to output an overlong sequence. */
c75a3ddc
PB
6916 if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
6917 return 0;
f676971a 6918
6555b6bd
RE
6919 /* Don't allow an eliminable register: register elimination can make
6920 the offset too large. */
6921 if (arm_eliminable_register (reg0))
6922 return 0;
6923
6924 val_diff = val1 - val0;
15b5c4c1
RE
6925
6926 if (arm_ld_sched)
6927 {
6928 /* If the target has load delay slots, then there's no benefit
6929 to using an ldm instruction unless the offset is zero and
6930 we are optimizing for size. */
6931 return (optimize_size && (REGNO (reg0) == REGNO (reg1))
6932 && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
6933 && (val_diff == 4 || val_diff == -4));
6934 }
6935
6555b6bd
RE
6936 return ((REGNO (reg0) == REGNO (reg1))
6937 && (val_diff == 4 || val_diff == -4));
ff9940b0 6938 }
6555b6bd 6939
ff9940b0
RE
6940 return 0;
6941}
6942
84ed5e79 6943int
e32bac5b
RE
6944load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
6945 HOST_WIDE_INT *load_offset)
84ed5e79
RE
6946{
6947 int unsorted_regs[4];
6948 HOST_WIDE_INT unsorted_offsets[4];
6949 int order[4];
ad076f4e 6950 int base_reg = -1;
84ed5e79
RE
6951 int i;
6952
1d6e90ac
NC
6953 /* Can only handle 2, 3, or 4 insns at present,
6954 though could be easily extended if required. */
e6d29d15 6955 gcc_assert (nops >= 2 && nops <= 4);
84ed5e79
RE
6956
6957 /* Loop over the operands and check that the memory references are
112cdef5 6958 suitable (i.e. immediate offsets from the same base register). At
84ed5e79
RE
6959 the same time, extract the target register, and the memory
6960 offsets. */
6961 for (i = 0; i < nops; i++)
6962 {
6963 rtx reg;
6964 rtx offset;
6965
56636818
JL
6966 /* Convert a subreg of a mem into the mem itself. */
6967 if (GET_CODE (operands[nops + i]) == SUBREG)
4e26a7af 6968 operands[nops + i] = alter_subreg (operands + (nops + i));
56636818 6969
e6d29d15 6970 gcc_assert (GET_CODE (operands[nops + i]) == MEM);
84ed5e79
RE
6971
6972 /* Don't reorder volatile memory references; it doesn't seem worth
6973 looking for the case where the order is ok anyway. */
6974 if (MEM_VOLATILE_P (operands[nops + i]))
6975 return 0;
6976
6977 offset = const0_rtx;
6978
6979 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
6980 || (GET_CODE (reg) == SUBREG
6981 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6982 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
6983 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
6984 == REG)
6985 || (GET_CODE (reg) == SUBREG
6986 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6987 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
6988 == CONST_INT)))
6989 {
6990 if (i == 0)
6991 {
d5b7b3ae 6992 base_reg = REGNO (reg);
84ed5e79
RE
6993 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
6994 ? REGNO (operands[i])
6995 : REGNO (SUBREG_REG (operands[i])));
6996 order[0] = 0;
6997 }
f676971a 6998 else
84ed5e79 6999 {
6354dc9b 7000 if (base_reg != (int) REGNO (reg))
84ed5e79
RE
7001 /* Not addressed from the same base register. */
7002 return 0;
7003
7004 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
7005 ? REGNO (operands[i])
7006 : REGNO (SUBREG_REG (operands[i])));
7007 if (unsorted_regs[i] < unsorted_regs[order[0]])
7008 order[0] = i;
7009 }
7010
7011 /* If it isn't an integer register, or if it overwrites the
7012 base register but isn't the last insn in the list, then
7013 we can't do this. */
7014 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
7015 || (i != nops - 1 && unsorted_regs[i] == base_reg))
7016 return 0;
7017
7018 unsorted_offsets[i] = INTVAL (offset);
7019 }
7020 else
7021 /* Not a suitable memory address. */
7022 return 0;
7023 }
7024
7025 /* All the useful information has now been extracted from the
7026 operands into unsorted_regs and unsorted_offsets; additionally,
7027 order[0] has been set to the lowest numbered register in the
7028 list. Sort the registers into order, and check that the memory
7029 offsets are ascending and adjacent. */
7030
7031 for (i = 1; i < nops; i++)
7032 {
7033 int j;
7034
7035 order[i] = order[i - 1];
7036 for (j = 0; j < nops; j++)
7037 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
7038 && (order[i] == order[i - 1]
7039 || unsorted_regs[j] < unsorted_regs[order[i]]))
7040 order[i] = j;
7041
7042 /* Have we found a suitable register? if not, one must be used more
7043 than once. */
7044 if (order[i] == order[i - 1])
7045 return 0;
7046
7047 /* Is the memory address adjacent and ascending? */
7048 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
7049 return 0;
7050 }
7051
7052 if (base)
7053 {
7054 *base = base_reg;
7055
7056 for (i = 0; i < nops; i++)
7057 regs[i] = unsorted_regs[order[i]];
7058
7059 *load_offset = unsorted_offsets[order[0]];
7060 }
7061
7062 if (unsorted_offsets[order[0]] == 0)
7063 return 1; /* ldmia */
7064
5b3e6663 7065 if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
84ed5e79
RE
7066 return 2; /* ldmib */
7067
5b3e6663 7068 if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
84ed5e79
RE
7069 return 3; /* ldmda */
7070
7071 if (unsorted_offsets[order[nops - 1]] == -4)
7072 return 4; /* ldmdb */
7073
949d79eb
RE
7074 /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
7075 if the offset isn't small enough. The reason 2 ldrs are faster
7076 is because these ARMs are able to do more than one cache access
7077 in a single cycle. The ARM9 and StrongARM have Harvard caches,
7078 whilst the ARM8 has a double bandwidth cache. This means that
7079 these cores can do both an instruction fetch and a data fetch in
7080 a single cycle, so the trick of calculating the address into a
7081 scratch register (one of the result regs) and then doing a load
7082 multiple actually becomes slower (and no smaller in code size).
7083 That is the transformation
f676971a 7084
6cc8c0b3
NC
7085 ldr rd1, [rbase + offset]
7086 ldr rd2, [rbase + offset + 4]
f676971a 7087
6cc8c0b3 7088 to
f676971a 7089
6cc8c0b3
NC
7090 add rd1, rbase, offset
7091 ldmia rd1, {rd1, rd2}
f676971a 7092
949d79eb
RE
7093 produces worse code -- '3 cycles + any stalls on rd2' instead of
7094 '2 cycles + any stalls on rd2'. On ARMs with only one cache
7095 access per cycle, the first sequence could never complete in less
7096 than 6 cycles, whereas the ldm sequence would only take 5 and
7097 would make better use of sequential accesses if not hitting the
7098 cache.
7099
7100 We cheat here and test 'arm_ld_sched' which we currently know to
7101 only be true for the ARM8, ARM9 and StrongARM. If this ever
7102 changes, then the test below needs to be reworked. */
f5a1b0d2 7103 if (nops == 2 && arm_ld_sched)
b36ba79f
RE
7104 return 0;
7105
84ed5e79
RE
7106 /* Can't do it without setting up the offset, only do this if it takes
7107 no more than one insn. */
f676971a 7108 return (const_ok_for_arm (unsorted_offsets[order[0]])
84ed5e79
RE
7109 || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
7110}
7111
cd2b33d0 7112const char *
e32bac5b 7113emit_ldm_seq (rtx *operands, int nops)
84ed5e79
RE
7114{
7115 int regs[4];
7116 int base_reg;
7117 HOST_WIDE_INT offset;
7118 char buf[100];
7119 int i;
7120
7121 switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
7122 {
7123 case 1:
5b3e6663 7124 strcpy (buf, "ldm%(ia%)\t");
84ed5e79
RE
7125 break;
7126
7127 case 2:
5b3e6663 7128 strcpy (buf, "ldm%(ib%)\t");
84ed5e79
RE
7129 break;
7130
7131 case 3:
5b3e6663 7132 strcpy (buf, "ldm%(da%)\t");
84ed5e79
RE
7133 break;
7134
7135 case 4:
5b3e6663 7136 strcpy (buf, "ldm%(db%)\t");
84ed5e79
RE
7137 break;
7138
7139 case 5:
7140 if (offset >= 0)
7141 sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
7142 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
7143 (long) offset);
7144 else
7145 sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
7146 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
7147 (long) -offset);
7148 output_asm_insn (buf, operands);
7149 base_reg = regs[0];
5b3e6663 7150 strcpy (buf, "ldm%(ia%)\t");
84ed5e79
RE
7151 break;
7152
7153 default:
e6d29d15 7154 gcc_unreachable ();
84ed5e79
RE
7155 }
7156
f676971a 7157 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
84ed5e79
RE
7158 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
7159
7160 for (i = 1; i < nops; i++)
7161 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
7162 reg_names[regs[i]]);
7163
7164 strcat (buf, "}\t%@ phole ldm");
7165
7166 output_asm_insn (buf, operands);
7167 return "";
7168}
7169
7170int
e32bac5b
RE
7171store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
7172 HOST_WIDE_INT * load_offset)
84ed5e79
RE
7173{
7174 int unsorted_regs[4];
7175 HOST_WIDE_INT unsorted_offsets[4];
7176 int order[4];
ad076f4e 7177 int base_reg = -1;
84ed5e79
RE
7178 int i;
7179
7180 /* Can only handle 2, 3, or 4 insns at present, though could be easily
7181 extended if required. */
e6d29d15 7182 gcc_assert (nops >= 2 && nops <= 4);
84ed5e79
RE
7183
7184 /* Loop over the operands and check that the memory references are
112cdef5 7185 suitable (i.e. immediate offsets from the same base register). At
84ed5e79
RE
7186 the same time, extract the target register, and the memory
7187 offsets. */
7188 for (i = 0; i < nops; i++)
7189 {
7190 rtx reg;
7191 rtx offset;
7192
56636818
JL
7193 /* Convert a subreg of a mem into the mem itself. */
7194 if (GET_CODE (operands[nops + i]) == SUBREG)
4e26a7af 7195 operands[nops + i] = alter_subreg (operands + (nops + i));
56636818 7196
e6d29d15 7197 gcc_assert (GET_CODE (operands[nops + i]) == MEM);
84ed5e79
RE
7198
7199 /* Don't reorder volatile memory references; it doesn't seem worth
7200 looking for the case where the order is ok anyway. */
7201 if (MEM_VOLATILE_P (operands[nops + i]))
7202 return 0;
7203
7204 offset = const0_rtx;
7205
7206 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
7207 || (GET_CODE (reg) == SUBREG
7208 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
7209 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
7210 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
7211 == REG)
7212 || (GET_CODE (reg) == SUBREG
7213 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
7214 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
7215 == CONST_INT)))
7216 {
7217 if (i == 0)
7218 {
62b10bbc 7219 base_reg = REGNO (reg);
84ed5e79
RE
7220 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
7221 ? REGNO (operands[i])
7222 : REGNO (SUBREG_REG (operands[i])));
7223 order[0] = 0;
7224 }
f676971a 7225 else
84ed5e79 7226 {
6354dc9b 7227 if (base_reg != (int) REGNO (reg))
84ed5e79
RE
7228 /* Not addressed from the same base register. */
7229 return 0;
7230
7231 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
7232 ? REGNO (operands[i])
7233 : REGNO (SUBREG_REG (operands[i])));
7234 if (unsorted_regs[i] < unsorted_regs[order[0]])
7235 order[0] = i;
7236 }
7237
7238 /* If it isn't an integer register, then we can't do this. */
7239 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
7240 return 0;
7241
7242 unsorted_offsets[i] = INTVAL (offset);
7243 }
7244 else
7245 /* Not a suitable memory address. */
7246 return 0;
7247 }
7248
7249 /* All the useful information has now been extracted from the
7250 operands into unsorted_regs and unsorted_offsets; additionally,
7251 order[0] has been set to the lowest numbered register in the
7252 list. Sort the registers into order, and check that the memory
7253 offsets are ascending and adjacent. */
7254
7255 for (i = 1; i < nops; i++)
7256 {
7257 int j;
7258
7259 order[i] = order[i - 1];
7260 for (j = 0; j < nops; j++)
7261 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
7262 && (order[i] == order[i - 1]
7263 || unsorted_regs[j] < unsorted_regs[order[i]]))
7264 order[i] = j;
7265
7266 /* Have we found a suitable register? if not, one must be used more
7267 than once. */
7268 if (order[i] == order[i - 1])
7269 return 0;
7270
7271 /* Is the memory address adjacent and ascending? */
7272 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
7273 return 0;
7274 }
7275
7276 if (base)
7277 {
7278 *base = base_reg;
7279
7280 for (i = 0; i < nops; i++)
7281 regs[i] = unsorted_regs[order[i]];
7282
7283 *load_offset = unsorted_offsets[order[0]];
7284 }
7285
7286 if (unsorted_offsets[order[0]] == 0)
7287 return 1; /* stmia */
7288
7289 if (unsorted_offsets[order[0]] == 4)
7290 return 2; /* stmib */
7291
7292 if (unsorted_offsets[order[nops - 1]] == 0)
7293 return 3; /* stmda */
7294
7295 if (unsorted_offsets[order[nops - 1]] == -4)
7296 return 4; /* stmdb */
7297
7298 return 0;
7299}
7300
cd2b33d0 7301const char *
e32bac5b 7302emit_stm_seq (rtx *operands, int nops)
84ed5e79
RE
7303{
7304 int regs[4];
7305 int base_reg;
7306 HOST_WIDE_INT offset;
7307 char buf[100];
7308 int i;
7309
7310 switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
7311 {
7312 case 1:
5b3e6663 7313 strcpy (buf, "stm%(ia%)\t");
84ed5e79
RE
7314 break;
7315
7316 case 2:
5b3e6663 7317 strcpy (buf, "stm%(ib%)\t");
84ed5e79
RE
7318 break;
7319
7320 case 3:
5b3e6663 7321 strcpy (buf, "stm%(da%)\t");
84ed5e79
RE
7322 break;
7323
7324 case 4:
5b3e6663 7325 strcpy (buf, "stm%(db%)\t");
84ed5e79
RE
7326 break;
7327
7328 default:
e6d29d15 7329 gcc_unreachable ();
84ed5e79
RE
7330 }
7331
f676971a 7332 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
84ed5e79
RE
7333 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
7334
7335 for (i = 1; i < nops; i++)
7336 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
7337 reg_names[regs[i]]);
7338
7339 strcat (buf, "}\t%@ phole stm");
7340
7341 output_asm_insn (buf, operands);
7342 return "";
7343}
ff9940b0 7344\f
6354dc9b 7345/* Routines for use in generating RTL. */
1d6e90ac 7346
f3bb6135 7347rtx
e32bac5b 7348arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
50ed9cea 7349 int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
ff9940b0 7350{
50ed9cea 7351 HOST_WIDE_INT offset = *offsetp;
ff9940b0
RE
7352 int i = 0, j;
7353 rtx result;
7354 int sign = up ? 1 : -1;
50ed9cea 7355 rtx mem, addr;
ff9940b0 7356
d19fb8e3 7357 /* XScale has load-store double instructions, but they have stricter
1e5f1716 7358 alignment requirements than load-store multiple, so we cannot
d19fb8e3
NC
7359 use them.
7360
7361 For XScale ldm requires 2 + NREGS cycles to complete and blocks
7362 the pipeline until completion.
7363
7364 NREGS CYCLES
7365 1 3
7366 2 4
7367 3 5
7368 4 6
7369
7370 An ldr instruction takes 1-3 cycles, but does not block the
7371 pipeline.
7372
7373 NREGS CYCLES
7374 1 1-3
7375 2 2-6
7376 3 3-9
7377 4 4-12
7378
7379 Best case ldr will always win. However, the more ldr instructions
7380 we issue, the less likely we are to be able to schedule them well.
7381 Using ldr instructions also increases code size.
7382
7383 As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
7384 for counts of 3 or 4 regs. */
4b3c2e48 7385 if (arm_tune_xscale && count <= 2 && ! optimize_size)
d19fb8e3
NC
7386 {
7387 rtx seq;
f676971a 7388
d19fb8e3 7389 start_sequence ();
f676971a 7390
d19fb8e3
NC
7391 for (i = 0; i < count; i++)
7392 {
50ed9cea
RH
7393 addr = plus_constant (from, i * 4 * sign);
7394 mem = adjust_automodify_address (basemem, SImode, addr, offset);
d19fb8e3 7395 emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
50ed9cea 7396 offset += 4 * sign;
d19fb8e3
NC
7397 }
7398
7399 if (write_back)
50ed9cea
RH
7400 {
7401 emit_move_insn (from, plus_constant (from, count * 4 * sign));
7402 *offsetp = offset;
7403 }
d19fb8e3 7404
2f937369 7405 seq = get_insns ();
d19fb8e3 7406 end_sequence ();
f676971a 7407
d19fb8e3
NC
7408 return seq;
7409 }
7410
43cffd11 7411 result = gen_rtx_PARALLEL (VOIDmode,
41e3f998 7412 rtvec_alloc (count + (write_back ? 1 : 0)));
ff9940b0 7413 if (write_back)
f3bb6135 7414 {
ff9940b0 7415 XVECEXP (result, 0, 0)
d66437c5 7416 = gen_rtx_SET (VOIDmode, from, plus_constant (from, count * 4 * sign));
ff9940b0
RE
7417 i = 1;
7418 count++;
f3bb6135
RE
7419 }
7420
ff9940b0 7421 for (j = 0; i < count; i++, j++)
f3bb6135 7422 {
50ed9cea
RH
7423 addr = plus_constant (from, j * 4 * sign);
7424 mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
43cffd11
RE
7425 XVECEXP (result, 0, i)
7426 = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
50ed9cea 7427 offset += 4 * sign;
f3bb6135
RE
7428 }
7429
50ed9cea
RH
7430 if (write_back)
7431 *offsetp = offset;
7432
ff9940b0
RE
7433 return result;
7434}
7435
f3bb6135 7436rtx
e32bac5b 7437arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
50ed9cea 7438 int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
ff9940b0 7439{
50ed9cea 7440 HOST_WIDE_INT offset = *offsetp;
ff9940b0
RE
7441 int i = 0, j;
7442 rtx result;
7443 int sign = up ? 1 : -1;
50ed9cea 7444 rtx mem, addr;
ff9940b0 7445
d19fb8e3
NC
7446 /* See arm_gen_load_multiple for discussion of
7447 the pros/cons of ldm/stm usage for XScale. */
4b3c2e48 7448 if (arm_tune_xscale && count <= 2 && ! optimize_size)
d19fb8e3
NC
7449 {
7450 rtx seq;
f676971a 7451
d19fb8e3 7452 start_sequence ();
f676971a 7453
d19fb8e3
NC
7454 for (i = 0; i < count; i++)
7455 {
50ed9cea
RH
7456 addr = plus_constant (to, i * 4 * sign);
7457 mem = adjust_automodify_address (basemem, SImode, addr, offset);
d19fb8e3 7458 emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
50ed9cea 7459 offset += 4 * sign;
d19fb8e3
NC
7460 }
7461
7462 if (write_back)
50ed9cea
RH
7463 {
7464 emit_move_insn (to, plus_constant (to, count * 4 * sign));
7465 *offsetp = offset;
7466 }
d19fb8e3 7467
2f937369 7468 seq = get_insns ();
d19fb8e3 7469 end_sequence ();
f676971a 7470
d19fb8e3
NC
7471 return seq;
7472 }
7473
43cffd11 7474 result = gen_rtx_PARALLEL (VOIDmode,
41e3f998 7475 rtvec_alloc (count + (write_back ? 1 : 0)));
ff9940b0 7476 if (write_back)
f3bb6135 7477 {
ff9940b0 7478 XVECEXP (result, 0, 0)
d66437c5 7479 = gen_rtx_SET (VOIDmode, to,
43cffd11 7480 plus_constant (to, count * 4 * sign));
ff9940b0
RE
7481 i = 1;
7482 count++;
f3bb6135
RE
7483 }
7484
ff9940b0 7485 for (j = 0; i < count; i++, j++)
f3bb6135 7486 {
50ed9cea
RH
7487 addr = plus_constant (to, j * 4 * sign);
7488 mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
43cffd11
RE
7489 XVECEXP (result, 0, i)
7490 = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
50ed9cea 7491 offset += 4 * sign;
f3bb6135
RE
7492 }
7493
50ed9cea
RH
7494 if (write_back)
7495 *offsetp = offset;
7496
ff9940b0
RE
7497 return result;
7498}
7499
880e2516 7500int
70128ad9 7501arm_gen_movmemqi (rtx *operands)
880e2516
RE
7502{
7503 HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
50ed9cea 7504 HOST_WIDE_INT srcoffset, dstoffset;
ad076f4e 7505 int i;
50ed9cea 7506 rtx src, dst, srcbase, dstbase;
880e2516 7507 rtx part_bytes_reg = NULL;
56636818 7508 rtx mem;
880e2516
RE
7509
7510 if (GET_CODE (operands[2]) != CONST_INT
7511 || GET_CODE (operands[3]) != CONST_INT
7512 || INTVAL (operands[2]) > 64
7513 || INTVAL (operands[3]) & 3)
7514 return 0;
7515
50ed9cea
RH
7516 dstbase = operands[0];
7517 srcbase = operands[1];
56636818 7518
50ed9cea
RH
7519 dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
7520 src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
880e2516 7521
e9d7b180 7522 in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
880e2516
RE
7523 out_words_to_go = INTVAL (operands[2]) / 4;
7524 last_bytes = INTVAL (operands[2]) & 3;
50ed9cea 7525 dstoffset = srcoffset = 0;
880e2516
RE
7526
7527 if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
43cffd11 7528 part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
880e2516
RE
7529
7530 for (i = 0; in_words_to_go >= 2; i+=4)
7531 {
bd9c7e23 7532 if (in_words_to_go > 4)
56636818 7533 emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
50ed9cea 7534 srcbase, &srcoffset));
bd9c7e23 7535 else
f676971a 7536 emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
50ed9cea 7537 FALSE, srcbase, &srcoffset));
bd9c7e23 7538
880e2516
RE
7539 if (out_words_to_go)
7540 {
bd9c7e23 7541 if (out_words_to_go > 4)
56636818 7542 emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
50ed9cea 7543 dstbase, &dstoffset));
bd9c7e23
RE
7544 else if (out_words_to_go != 1)
7545 emit_insn (arm_gen_store_multiple (0, out_words_to_go,
f676971a 7546 dst, TRUE,
bd9c7e23 7547 (last_bytes == 0
56636818 7548 ? FALSE : TRUE),
50ed9cea 7549 dstbase, &dstoffset));
880e2516
RE
7550 else
7551 {
50ed9cea 7552 mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
43cffd11 7553 emit_move_insn (mem, gen_rtx_REG (SImode, 0));
bd9c7e23 7554 if (last_bytes != 0)
50ed9cea
RH
7555 {
7556 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
7557 dstoffset += 4;
7558 }
880e2516
RE
7559 }
7560 }
7561
7562 in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
7563 out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
7564 }
7565
7566 /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do. */
7567 if (out_words_to_go)
62b10bbc
NC
7568 {
7569 rtx sreg;
f676971a 7570
50ed9cea
RH
7571 mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
7572 sreg = copy_to_reg (mem);
7573
7574 mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
62b10bbc 7575 emit_move_insn (mem, sreg);
62b10bbc 7576 in_words_to_go--;
f676971a 7577
e6d29d15 7578 gcc_assert (!in_words_to_go); /* Sanity check */
62b10bbc 7579 }
880e2516
RE
7580
7581 if (in_words_to_go)
7582 {
e6d29d15 7583 gcc_assert (in_words_to_go > 0);
880e2516 7584
50ed9cea 7585 mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
56636818 7586 part_bytes_reg = copy_to_mode_reg (SImode, mem);
880e2516
RE
7587 }
7588
e6d29d15 7589 gcc_assert (!last_bytes || part_bytes_reg);
d5b7b3ae 7590
880e2516
RE
7591 if (BYTES_BIG_ENDIAN && last_bytes)
7592 {
7593 rtx tmp = gen_reg_rtx (SImode);
7594
6354dc9b 7595 /* The bytes we want are in the top end of the word. */
bee06f3d
RE
7596 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
7597 GEN_INT (8 * (4 - last_bytes))));
880e2516 7598 part_bytes_reg = tmp;
f676971a 7599
880e2516
RE
7600 while (last_bytes)
7601 {
50ed9cea
RH
7602 mem = adjust_automodify_address (dstbase, QImode,
7603 plus_constant (dst, last_bytes - 1),
7604 dstoffset + last_bytes - 1);
5d5603e2
BS
7605 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
7606
880e2516
RE
7607 if (--last_bytes)
7608 {
7609 tmp = gen_reg_rtx (SImode);
7610 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
7611 part_bytes_reg = tmp;
7612 }
7613 }
f676971a 7614
880e2516
RE
7615 }
7616 else
7617 {
d5b7b3ae 7618 if (last_bytes > 1)
880e2516 7619 {
50ed9cea 7620 mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
5d5603e2 7621 emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
d5b7b3ae
RE
7622 last_bytes -= 2;
7623 if (last_bytes)
880e2516
RE
7624 {
7625 rtx tmp = gen_reg_rtx (SImode);
a556fd39 7626 emit_insn (gen_addsi3 (dst, dst, const2_rtx));
d5b7b3ae 7627 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
880e2516 7628 part_bytes_reg = tmp;
50ed9cea 7629 dstoffset += 2;
880e2516
RE
7630 }
7631 }
f676971a 7632
d5b7b3ae
RE
7633 if (last_bytes)
7634 {
50ed9cea 7635 mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
5d5603e2 7636 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
d5b7b3ae 7637 }
880e2516
RE
7638 }
7639
7640 return 1;
7641}
7642
03f1640c
RE
7643/* Select a dominance comparison mode if possible for a test of the general
7644 form (OP (COND_OR (X) (Y)) (const_int 0)). We support three forms.
f676971a 7645 COND_OR == DOM_CC_X_AND_Y => (X && Y)
03f1640c 7646 COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
f676971a 7647 COND_OR == DOM_CC_X_OR_Y => (X || Y)
03f1640c 7648 In all cases OP will be either EQ or NE, but we don't need to know which
f676971a 7649 here. If we are unable to support a dominance comparison we return
03f1640c
RE
7650 CC mode. This will then fail to match for the RTL expressions that
7651 generate this call. */
03f1640c 7652enum machine_mode
e32bac5b 7653arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
84ed5e79
RE
7654{
7655 enum rtx_code cond1, cond2;
7656 int swapped = 0;
7657
7658 /* Currently we will probably get the wrong result if the individual
7659 comparisons are not simple. This also ensures that it is safe to
956d6950 7660 reverse a comparison if necessary. */
84ed5e79
RE
7661 if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
7662 != CCmode)
7663 || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
7664 != CCmode))
7665 return CCmode;
7666
1646cf41
RE
7667 /* The if_then_else variant of this tests the second condition if the
7668 first passes, but is true if the first fails. Reverse the first
7669 condition to get a true "inclusive-or" expression. */
03f1640c 7670 if (cond_or == DOM_CC_NX_OR_Y)
84ed5e79
RE
7671 cond1 = reverse_condition (cond1);
7672
7673 /* If the comparisons are not equal, and one doesn't dominate the other,
7674 then we can't do this. */
f676971a 7675 if (cond1 != cond2
5895f793
RE
7676 && !comparison_dominates_p (cond1, cond2)
7677 && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
84ed5e79
RE
7678 return CCmode;
7679
7680 if (swapped)
7681 {
7682 enum rtx_code temp = cond1;
7683 cond1 = cond2;
7684 cond2 = temp;
7685 }
7686
7687 switch (cond1)
7688 {
7689 case EQ:
e6d29d15 7690 if (cond_or == DOM_CC_X_AND_Y)
84ed5e79
RE
7691 return CC_DEQmode;
7692
7693 switch (cond2)
7694 {
e6d29d15 7695 case EQ: return CC_DEQmode;
84ed5e79
RE
7696 case LE: return CC_DLEmode;
7697 case LEU: return CC_DLEUmode;
7698 case GE: return CC_DGEmode;
7699 case GEU: return CC_DGEUmode;
e6d29d15 7700 default: gcc_unreachable ();
84ed5e79
RE
7701 }
7702
84ed5e79 7703 case LT:
e6d29d15 7704 if (cond_or == DOM_CC_X_AND_Y)
84ed5e79 7705 return CC_DLTmode;
e0b92319 7706
e6d29d15
NS
7707 switch (cond2)
7708 {
7709 case LT:
7710 return CC_DLTmode;
7711 case LE:
7712 return CC_DLEmode;
7713 case NE:
7714 return CC_DNEmode;
7715 default:
7716 gcc_unreachable ();
7717 }
84ed5e79
RE
7718
7719 case GT:
e6d29d15 7720 if (cond_or == DOM_CC_X_AND_Y)
84ed5e79 7721 return CC_DGTmode;
e6d29d15
NS
7722
7723 switch (cond2)
7724 {
7725 case GT:
7726 return CC_DGTmode;
7727 case GE:
7728 return CC_DGEmode;
7729 case NE:
7730 return CC_DNEmode;
7731 default:
7732 gcc_unreachable ();
7733 }
f676971a 7734
84ed5e79 7735 case LTU:
e6d29d15 7736 if (cond_or == DOM_CC_X_AND_Y)
84ed5e79 7737 return CC_DLTUmode;
e6d29d15
NS
7738
7739 switch (cond2)
7740 {
7741 case LTU:
7742 return CC_DLTUmode;
7743 case LEU:
7744 return CC_DLEUmode;
7745 case NE:
7746 return CC_DNEmode;
7747 default:
7748 gcc_unreachable ();
7749 }
84ed5e79
RE
7750
7751 case GTU:
e6d29d15 7752 if (cond_or == DOM_CC_X_AND_Y)
84ed5e79 7753 return CC_DGTUmode;
e0b92319 7754
e6d29d15
NS
7755 switch (cond2)
7756 {
7757 case GTU:
7758 return CC_DGTUmode;
7759 case GEU:
7760 return CC_DGEUmode;
7761 case NE:
7762 return CC_DNEmode;
7763 default:
7764 gcc_unreachable ();
7765 }
84ed5e79
RE
7766
7767 /* The remaining cases only occur when both comparisons are the
7768 same. */
7769 case NE:
e6d29d15 7770 gcc_assert (cond1 == cond2);
84ed5e79
RE
7771 return CC_DNEmode;
7772
7773 case LE:
e6d29d15 7774 gcc_assert (cond1 == cond2);
84ed5e79
RE
7775 return CC_DLEmode;
7776
7777 case GE:
e6d29d15 7778 gcc_assert (cond1 == cond2);
84ed5e79
RE
7779 return CC_DGEmode;
7780
7781 case LEU:
e6d29d15 7782 gcc_assert (cond1 == cond2);
84ed5e79
RE
7783 return CC_DLEUmode;
7784
7785 case GEU:
e6d29d15 7786 gcc_assert (cond1 == cond2);
84ed5e79 7787 return CC_DGEUmode;
ad076f4e
RE
7788
7789 default:
e6d29d15 7790 gcc_unreachable ();
84ed5e79 7791 }
84ed5e79
RE
7792}
7793
7794enum machine_mode
e32bac5b 7795arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
84ed5e79
RE
7796{
7797 /* All floating point compares return CCFP if it is an equality
7798 comparison, and CCFPE otherwise. */
7799 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
e45b72c4
RE
7800 {
7801 switch (op)
7802 {
7803 case EQ:
7804 case NE:
7805 case UNORDERED:
7806 case ORDERED:
7807 case UNLT:
7808 case UNLE:
7809 case UNGT:
7810 case UNGE:
7811 case UNEQ:
7812 case LTGT:
7813 return CCFPmode;
7814
7815 case LT:
7816 case LE:
7817 case GT:
7818 case GE:
9b66ebb1 7819 if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
9b6b54e2 7820 return CCFPmode;
e45b72c4
RE
7821 return CCFPEmode;
7822
7823 default:
e6d29d15 7824 gcc_unreachable ();
e45b72c4
RE
7825 }
7826 }
f676971a 7827
84ed5e79
RE
7828 /* A compare with a shifted operand. Because of canonicalization, the
7829 comparison will have to be swapped when we emit the assembler. */
7830 if (GET_MODE (y) == SImode && GET_CODE (y) == REG
7831 && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
7832 || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
7833 || GET_CODE (x) == ROTATERT))
7834 return CC_SWPmode;
7835
04d8b819
RE
7836 /* This operation is performed swapped, but since we only rely on the Z
7837 flag we don't need an additional mode. */
7838 if (GET_MODE (y) == SImode && REG_P (y)
7839 && GET_CODE (x) == NEG
7840 && (op == EQ || op == NE))
7841 return CC_Zmode;
7842
f676971a 7843 /* This is a special case that is used by combine to allow a
956d6950 7844 comparison of a shifted byte load to be split into a zero-extend
84ed5e79 7845 followed by a comparison of the shifted integer (only valid for
956d6950 7846 equalities and unsigned inequalities). */
84ed5e79
RE
7847 if (GET_MODE (x) == SImode
7848 && GET_CODE (x) == ASHIFT
7849 && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
7850 && GET_CODE (XEXP (x, 0)) == SUBREG
7851 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
7852 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
7853 && (op == EQ || op == NE
7854 || op == GEU || op == GTU || op == LTU || op == LEU)
7855 && GET_CODE (y) == CONST_INT)
7856 return CC_Zmode;
7857
1646cf41
RE
7858 /* A construct for a conditional compare, if the false arm contains
7859 0, then both conditions must be true, otherwise either condition
7860 must be true. Not all conditions are possible, so CCmode is
7861 returned if it can't be done. */
7862 if (GET_CODE (x) == IF_THEN_ELSE
7863 && (XEXP (x, 2) == const0_rtx
7864 || XEXP (x, 2) == const1_rtx)
ec8e098d
PB
7865 && COMPARISON_P (XEXP (x, 0))
7866 && COMPARISON_P (XEXP (x, 1)))
f676971a 7867 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
03f1640c 7868 INTVAL (XEXP (x, 2)));
1646cf41
RE
7869
7870 /* Alternate canonicalizations of the above. These are somewhat cleaner. */
7871 if (GET_CODE (x) == AND
ec8e098d
PB
7872 && COMPARISON_P (XEXP (x, 0))
7873 && COMPARISON_P (XEXP (x, 1)))
03f1640c
RE
7874 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
7875 DOM_CC_X_AND_Y);
1646cf41
RE
7876
7877 if (GET_CODE (x) == IOR
ec8e098d
PB
7878 && COMPARISON_P (XEXP (x, 0))
7879 && COMPARISON_P (XEXP (x, 1)))
03f1640c
RE
7880 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
7881 DOM_CC_X_OR_Y);
1646cf41 7882
defc0463
RE
7883 /* An operation (on Thumb) where we want to test for a single bit.
7884 This is done by shifting that bit up into the top bit of a
7885 scratch register; we can then branch on the sign bit. */
5b3e6663 7886 if (TARGET_THUMB1
defc0463
RE
7887 && GET_MODE (x) == SImode
7888 && (op == EQ || op == NE)
f9fa4363
RE
7889 && GET_CODE (x) == ZERO_EXTRACT
7890 && XEXP (x, 1) == const1_rtx)
defc0463
RE
7891 return CC_Nmode;
7892
84ed5e79
RE
7893 /* An operation that sets the condition codes as a side-effect, the
7894 V flag is not set correctly, so we can only use comparisons where
7895 this doesn't matter. (For LT and GE we can use "mi" and "pl"
defc0463 7896 instead.) */
5b3e6663 7897 /* ??? Does the ZERO_EXTRACT case really apply to thumb2? */
84ed5e79
RE
7898 if (GET_MODE (x) == SImode
7899 && y == const0_rtx
7900 && (op == EQ || op == NE || op == LT || op == GE)
7901 && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
7902 || GET_CODE (x) == AND || GET_CODE (x) == IOR
7903 || GET_CODE (x) == XOR || GET_CODE (x) == MULT
7904 || GET_CODE (x) == NOT || GET_CODE (x) == NEG
7905 || GET_CODE (x) == LSHIFTRT
7906 || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
defc0463 7907 || GET_CODE (x) == ROTATERT
5b3e6663 7908 || (TARGET_32BIT && GET_CODE (x) == ZERO_EXTRACT)))
84ed5e79
RE
7909 return CC_NOOVmode;
7910
84ed5e79
RE
7911 if (GET_MODE (x) == QImode && (op == EQ || op == NE))
7912 return CC_Zmode;
7913
bd9c7e23
RE
7914 if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
7915 && GET_CODE (x) == PLUS
7916 && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
7917 return CC_Cmode;
7918
84ed5e79
RE
7919 return CCmode;
7920}
7921
ff9940b0
RE
7922/* X and Y are two things to compare using CODE. Emit the compare insn and
7923 return the rtx for register 0 in the proper mode. FP means this is a
7924 floating point compare: I don't think that it is needed on the arm. */
ff9940b0 7925rtx
e32bac5b 7926arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
ff9940b0
RE
7927{
7928 enum machine_mode mode = SELECT_CC_MODE (code, x, y);
d5b7b3ae 7929 rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
ff9940b0 7930
d66437c5 7931 emit_set_insn (cc_reg, gen_rtx_COMPARE (mode, x, y));
ff9940b0
RE
7932
7933 return cc_reg;
7934}
7935
fcd53748
JT
7936/* Generate a sequence of insns that will generate the correct return
7937 address mask depending on the physical architecture that the program
7938 is running on. */
fcd53748 7939rtx
e32bac5b 7940arm_gen_return_addr_mask (void)
fcd53748
JT
7941{
7942 rtx reg = gen_reg_rtx (Pmode);
7943
7944 emit_insn (gen_return_addr_mask (reg));
7945 return reg;
7946}
7947
0a81f500 7948void
e32bac5b 7949arm_reload_in_hi (rtx *operands)
0a81f500 7950{
f9cc092a
RE
7951 rtx ref = operands[1];
7952 rtx base, scratch;
7953 HOST_WIDE_INT offset = 0;
7954
7955 if (GET_CODE (ref) == SUBREG)
7956 {
ddef6bc7 7957 offset = SUBREG_BYTE (ref);
f9cc092a
RE
7958 ref = SUBREG_REG (ref);
7959 }
7960
7961 if (GET_CODE (ref) == REG)
7962 {
7963 /* We have a pseudo which has been spilt onto the stack; there
7964 are two cases here: the first where there is a simple
7965 stack-slot replacement and a second where the stack-slot is
7966 out of range, or is used as a subreg. */
7967 if (reg_equiv_mem[REGNO (ref)])
7968 {
7969 ref = reg_equiv_mem[REGNO (ref)];
7970 base = find_replacement (&XEXP (ref, 0));
7971 }
7972 else
6354dc9b 7973 /* The slot is out of range, or was dressed up in a SUBREG. */
f9cc092a
RE
7974 base = reg_equiv_address[REGNO (ref)];
7975 }
7976 else
7977 base = find_replacement (&XEXP (ref, 0));
0a81f500 7978
e5e809f4
JL
7979 /* Handle the case where the address is too complex to be offset by 1. */
7980 if (GET_CODE (base) == MINUS
7981 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
7982 {
f9cc092a 7983 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
e5e809f4 7984
d66437c5 7985 emit_set_insn (base_plus, base);
e5e809f4
JL
7986 base = base_plus;
7987 }
f9cc092a
RE
7988 else if (GET_CODE (base) == PLUS)
7989 {
6354dc9b 7990 /* The addend must be CONST_INT, or we would have dealt with it above. */
f9cc092a
RE
7991 HOST_WIDE_INT hi, lo;
7992
7993 offset += INTVAL (XEXP (base, 1));
7994 base = XEXP (base, 0);
7995
6354dc9b 7996 /* Rework the address into a legal sequence of insns. */
f9cc092a
RE
7997 /* Valid range for lo is -4095 -> 4095 */
7998 lo = (offset >= 0
7999 ? (offset & 0xfff)
8000 : -((-offset) & 0xfff));
8001
8002 /* Corner case, if lo is the max offset then we would be out of range
8003 once we have added the additional 1 below, so bump the msb into the
8004 pre-loading insn(s). */
8005 if (lo == 4095)
8006 lo &= 0x7ff;
8007
30cf4896
KG
8008 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
8009 ^ (HOST_WIDE_INT) 0x80000000)
8010 - (HOST_WIDE_INT) 0x80000000);
f9cc092a 8011
e6d29d15 8012 gcc_assert (hi + lo == offset);
f9cc092a
RE
8013
8014 if (hi != 0)
8015 {
8016 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8017
8018 /* Get the base address; addsi3 knows how to handle constants
6354dc9b 8019 that require more than one insn. */
f9cc092a
RE
8020 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
8021 base = base_plus;
8022 offset = lo;
8023 }
8024 }
e5e809f4 8025
3a1944a6
RE
8026 /* Operands[2] may overlap operands[0] (though it won't overlap
8027 operands[1]), that's why we asked for a DImode reg -- so we can
8028 use the bit that does not overlap. */
8029 if (REGNO (operands[2]) == REGNO (operands[0]))
8030 scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8031 else
8032 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
8033
f9cc092a
RE
8034 emit_insn (gen_zero_extendqisi2 (scratch,
8035 gen_rtx_MEM (QImode,
8036 plus_constant (base,
8037 offset))));
43cffd11 8038 emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
f676971a 8039 gen_rtx_MEM (QImode,
f9cc092a
RE
8040 plus_constant (base,
8041 offset + 1))));
5895f793 8042 if (!BYTES_BIG_ENDIAN)
d66437c5
RE
8043 emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
8044 gen_rtx_IOR (SImode,
8045 gen_rtx_ASHIFT
8046 (SImode,
8047 gen_rtx_SUBREG (SImode, operands[0], 0),
8048 GEN_INT (8)),
8049 scratch));
0a81f500 8050 else
d66437c5
RE
8051 emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
8052 gen_rtx_IOR (SImode,
8053 gen_rtx_ASHIFT (SImode, scratch,
8054 GEN_INT (8)),
8055 gen_rtx_SUBREG (SImode, operands[0], 0)));
0a81f500
RE
8056}
8057
72ac76be 8058/* Handle storing a half-word to memory during reload by synthesizing as two
f9cc092a
RE
8059 byte stores. Take care not to clobber the input values until after we
8060 have moved them somewhere safe. This code assumes that if the DImode
8061 scratch in operands[2] overlaps either the input value or output address
8062 in some way, then that value must die in this insn (we absolutely need
8063 two scratch registers for some corner cases). */
f3bb6135 8064void
e32bac5b 8065arm_reload_out_hi (rtx *operands)
af48348a 8066{
f9cc092a
RE
8067 rtx ref = operands[0];
8068 rtx outval = operands[1];
8069 rtx base, scratch;
8070 HOST_WIDE_INT offset = 0;
8071
8072 if (GET_CODE (ref) == SUBREG)
8073 {
ddef6bc7 8074 offset = SUBREG_BYTE (ref);
f9cc092a
RE
8075 ref = SUBREG_REG (ref);
8076 }
8077
f9cc092a
RE
8078 if (GET_CODE (ref) == REG)
8079 {
8080 /* We have a pseudo which has been spilt onto the stack; there
8081 are two cases here: the first where there is a simple
8082 stack-slot replacement and a second where the stack-slot is
8083 out of range, or is used as a subreg. */
8084 if (reg_equiv_mem[REGNO (ref)])
8085 {
8086 ref = reg_equiv_mem[REGNO (ref)];
8087 base = find_replacement (&XEXP (ref, 0));
8088 }
8089 else
6354dc9b 8090 /* The slot is out of range, or was dressed up in a SUBREG. */
f9cc092a
RE
8091 base = reg_equiv_address[REGNO (ref)];
8092 }
8093 else
8094 base = find_replacement (&XEXP (ref, 0));
8095
8096 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
8097
8098 /* Handle the case where the address is too complex to be offset by 1. */
8099 if (GET_CODE (base) == MINUS
8100 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
8101 {
8102 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8103
8104 /* Be careful not to destroy OUTVAL. */
8105 if (reg_overlap_mentioned_p (base_plus, outval))
8106 {
8107 /* Updating base_plus might destroy outval, see if we can
8108 swap the scratch and base_plus. */
5895f793 8109 if (!reg_overlap_mentioned_p (scratch, outval))
f9cc092a
RE
8110 {
8111 rtx tmp = scratch;
8112 scratch = base_plus;
8113 base_plus = tmp;
8114 }
8115 else
8116 {
8117 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
8118
8119 /* Be conservative and copy OUTVAL into the scratch now,
8120 this should only be necessary if outval is a subreg
8121 of something larger than a word. */
8122 /* XXX Might this clobber base? I can't see how it can,
8123 since scratch is known to overlap with OUTVAL, and
8124 must be wider than a word. */
8125 emit_insn (gen_movhi (scratch_hi, outval));
8126 outval = scratch_hi;
8127 }
8128 }
8129
d66437c5 8130 emit_set_insn (base_plus, base);
f9cc092a
RE
8131 base = base_plus;
8132 }
8133 else if (GET_CODE (base) == PLUS)
8134 {
6354dc9b 8135 /* The addend must be CONST_INT, or we would have dealt with it above. */
f9cc092a
RE
8136 HOST_WIDE_INT hi, lo;
8137
8138 offset += INTVAL (XEXP (base, 1));
8139 base = XEXP (base, 0);
8140
6354dc9b 8141 /* Rework the address into a legal sequence of insns. */
f9cc092a
RE
8142 /* Valid range for lo is -4095 -> 4095 */
8143 lo = (offset >= 0
8144 ? (offset & 0xfff)
8145 : -((-offset) & 0xfff));
8146
8147 /* Corner case, if lo is the max offset then we would be out of range
8148 once we have added the additional 1 below, so bump the msb into the
8149 pre-loading insn(s). */
8150 if (lo == 4095)
8151 lo &= 0x7ff;
8152
30cf4896
KG
8153 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
8154 ^ (HOST_WIDE_INT) 0x80000000)
8155 - (HOST_WIDE_INT) 0x80000000);
f9cc092a 8156
e6d29d15 8157 gcc_assert (hi + lo == offset);
f9cc092a
RE
8158
8159 if (hi != 0)
8160 {
8161 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8162
8163 /* Be careful not to destroy OUTVAL. */
8164 if (reg_overlap_mentioned_p (base_plus, outval))
8165 {
8166 /* Updating base_plus might destroy outval, see if we
8167 can swap the scratch and base_plus. */
5895f793 8168 if (!reg_overlap_mentioned_p (scratch, outval))
f9cc092a
RE
8169 {
8170 rtx tmp = scratch;
8171 scratch = base_plus;
8172 base_plus = tmp;
8173 }
8174 else
8175 {
8176 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
8177
8178 /* Be conservative and copy outval into scratch now,
8179 this should only be necessary if outval is a
8180 subreg of something larger than a word. */
8181 /* XXX Might this clobber base? I can't see how it
8182 can, since scratch is known to overlap with
8183 outval. */
8184 emit_insn (gen_movhi (scratch_hi, outval));
8185 outval = scratch_hi;
8186 }
8187 }
8188
8189 /* Get the base address; addsi3 knows how to handle constants
6354dc9b 8190 that require more than one insn. */
f9cc092a
RE
8191 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
8192 base = base_plus;
8193 offset = lo;
8194 }
8195 }
af48348a 8196
b5cc037f
RE
8197 if (BYTES_BIG_ENDIAN)
8198 {
f676971a 8199 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
f9cc092a 8200 plus_constant (base, offset + 1)),
5d5603e2 8201 gen_lowpart (QImode, outval)));
f9cc092a
RE
8202 emit_insn (gen_lshrsi3 (scratch,
8203 gen_rtx_SUBREG (SImode, outval, 0),
b5cc037f 8204 GEN_INT (8)));
f9cc092a 8205 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
5d5603e2 8206 gen_lowpart (QImode, scratch)));
b5cc037f
RE
8207 }
8208 else
8209 {
f9cc092a 8210 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
5d5603e2 8211 gen_lowpart (QImode, outval)));
f9cc092a
RE
8212 emit_insn (gen_lshrsi3 (scratch,
8213 gen_rtx_SUBREG (SImode, outval, 0),
b5cc037f 8214 GEN_INT (8)));
f9cc092a
RE
8215 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
8216 plus_constant (base, offset + 1)),
5d5603e2 8217 gen_lowpart (QImode, scratch)));
b5cc037f 8218 }
af48348a 8219}
866af8a9
JB
8220
8221/* Return true if a type must be passed in memory. For AAPCS, small aggregates
8222 (padded to the size of a word) should be passed in a register. */
8223
8224static bool
586de218 8225arm_must_pass_in_stack (enum machine_mode mode, const_tree type)
866af8a9
JB
8226{
8227 if (TARGET_AAPCS_BASED)
8228 return must_pass_in_stack_var_size (mode, type);
8229 else
8230 return must_pass_in_stack_var_size_or_pad (mode, type);
8231}
8232
8233
8234/* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
8235 Return true if an argument passed on the stack should be padded upwards,
5a29b385
PB
8236 i.e. if the least-significant byte has useful data.
8237 For legacy APCS ABIs we use the default. For AAPCS based ABIs small
8238 aggregate types are placed in the lowest memory address. */
866af8a9
JB
8239
8240bool
586de218 8241arm_pad_arg_upward (enum machine_mode mode, const_tree type)
866af8a9
JB
8242{
8243 if (!TARGET_AAPCS_BASED)
5a29b385 8244 return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
866af8a9
JB
8245
8246 if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
8247 return false;
8248
8249 return true;
8250}
8251
8252
8253/* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
8254 For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
8255 byte of the register has useful data, and return the opposite if the
8256 most significant byte does.
8257 For AAPCS, small aggregates and small complex types are always padded
8258 upwards. */
8259
8260bool
8261arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
8262 tree type, int first ATTRIBUTE_UNUSED)
8263{
8264 if (TARGET_AAPCS_BASED
8265 && BYTES_BIG_ENDIAN
8266 && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
8267 && int_size_in_bytes (type) <= 4)
8268 return true;
8269
8270 /* Otherwise, use default padding. */
8271 return !BYTES_BIG_ENDIAN;
8272}
8273
2b835d68 8274\f
d5b7b3ae
RE
8275/* Print a symbolic form of X to the debug file, F. */
8276static void
e32bac5b 8277arm_print_value (FILE *f, rtx x)
d5b7b3ae
RE
8278{
8279 switch (GET_CODE (x))
8280 {
8281 case CONST_INT:
8282 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
8283 return;
8284
8285 case CONST_DOUBLE:
8286 fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
8287 return;
8288
5a9335ef
NC
8289 case CONST_VECTOR:
8290 {
8291 int i;
8292
8293 fprintf (f, "<");
8294 for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
8295 {
8296 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
8297 if (i < (CONST_VECTOR_NUNITS (x) - 1))
8298 fputc (',', f);
8299 }
8300 fprintf (f, ">");
8301 }
8302 return;
8303
d5b7b3ae
RE
8304 case CONST_STRING:
8305 fprintf (f, "\"%s\"", XSTR (x, 0));
8306 return;
8307
8308 case SYMBOL_REF:
8309 fprintf (f, "`%s'", XSTR (x, 0));
8310 return;
8311
8312 case LABEL_REF:
8313 fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
8314 return;
8315
8316 case CONST:
8317 arm_print_value (f, XEXP (x, 0));
8318 return;
8319
8320 case PLUS:
8321 arm_print_value (f, XEXP (x, 0));
8322 fprintf (f, "+");
8323 arm_print_value (f, XEXP (x, 1));
8324 return;
8325
8326 case PC:
8327 fprintf (f, "pc");
8328 return;
8329
8330 default:
8331 fprintf (f, "????");
8332 return;
8333 }
8334}
8335\f
2b835d68 8336/* Routines for manipulation of the constant pool. */
2b835d68 8337
949d79eb
RE
8338/* Arm instructions cannot load a large constant directly into a
8339 register; they have to come from a pc relative load. The constant
8340 must therefore be placed in the addressable range of the pc
8341 relative load. Depending on the precise pc relative load
8342 instruction the range is somewhere between 256 bytes and 4k. This
8343 means that we often have to dump a constant inside a function, and
2b835d68
RE
8344 generate code to branch around it.
8345
949d79eb
RE
8346 It is important to minimize this, since the branches will slow
8347 things down and make the code larger.
2b835d68 8348
949d79eb
RE
8349 Normally we can hide the table after an existing unconditional
8350 branch so that there is no interruption of the flow, but in the
8351 worst case the code looks like this:
2b835d68
RE
8352
8353 ldr rn, L1
949d79eb 8354 ...
2b835d68
RE
8355 b L2
8356 align
8357 L1: .long value
8358 L2:
949d79eb 8359 ...
2b835d68 8360
2b835d68 8361 ldr rn, L3
949d79eb 8362 ...
2b835d68
RE
8363 b L4
8364 align
2b835d68
RE
8365 L3: .long value
8366 L4:
949d79eb
RE
8367 ...
8368
8369 We fix this by performing a scan after scheduling, which notices
8370 which instructions need to have their operands fetched from the
8371 constant table and builds the table.
8372
8373 The algorithm starts by building a table of all the constants that
8374 need fixing up and all the natural barriers in the function (places
8375 where a constant table can be dropped without breaking the flow).
8376 For each fixup we note how far the pc-relative replacement will be
8377 able to reach and the offset of the instruction into the function.
8378
8379 Having built the table we then group the fixes together to form
8380 tables that are as large as possible (subject to addressing
8381 constraints) and emit each table of constants after the last
8382 barrier that is within range of all the instructions in the group.
8383 If a group does not contain a barrier, then we forcibly create one
8384 by inserting a jump instruction into the flow. Once the table has
8385 been inserted, the insns are then modified to reference the
8386 relevant entry in the pool.
8387
6354dc9b 8388 Possible enhancements to the algorithm (not implemented) are:
949d79eb 8389
d5b7b3ae 8390 1) For some processors and object formats, there may be benefit in
949d79eb
RE
8391 aligning the pools to the start of cache lines; this alignment
8392 would need to be taken into account when calculating addressability
6354dc9b 8393 of a pool. */
2b835d68 8394
d5b7b3ae
RE
8395/* These typedefs are located at the start of this file, so that
8396 they can be used in the prototypes there. This comment is to
8397 remind readers of that fact so that the following structures
8398 can be understood more easily.
8399
8400 typedef struct minipool_node Mnode;
8401 typedef struct minipool_fixup Mfix; */
8402
8403struct minipool_node
8404{
8405 /* Doubly linked chain of entries. */
8406 Mnode * next;
8407 Mnode * prev;
8408 /* The maximum offset into the code that this entry can be placed. While
8409 pushing fixes for forward references, all entries are sorted in order
8410 of increasing max_address. */
8411 HOST_WIDE_INT max_address;
5519a4f9 8412 /* Similarly for an entry inserted for a backwards ref. */
d5b7b3ae
RE
8413 HOST_WIDE_INT min_address;
8414 /* The number of fixes referencing this entry. This can become zero
8415 if we "unpush" an entry. In this case we ignore the entry when we
8416 come to emit the code. */
8417 int refcount;
8418 /* The offset from the start of the minipool. */
8419 HOST_WIDE_INT offset;
8420 /* The value in table. */
8421 rtx value;
8422 /* The mode of value. */
8423 enum machine_mode mode;
5a9335ef
NC
8424 /* The size of the value. With iWMMXt enabled
8425 sizes > 4 also imply an alignment of 8-bytes. */
d5b7b3ae
RE
8426 int fix_size;
8427};
8428
8429struct minipool_fixup
2b835d68 8430{
d5b7b3ae
RE
8431 Mfix * next;
8432 rtx insn;
8433 HOST_WIDE_INT address;
8434 rtx * loc;
8435 enum machine_mode mode;
8436 int fix_size;
8437 rtx value;
8438 Mnode * minipool;
8439 HOST_WIDE_INT forwards;
8440 HOST_WIDE_INT backwards;
8441};
2b835d68 8442
d5b7b3ae
RE
8443/* Fixes less than a word need padding out to a word boundary. */
8444#define MINIPOOL_FIX_SIZE(mode) \
8445 (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
2b835d68 8446
d5b7b3ae
RE
8447static Mnode * minipool_vector_head;
8448static Mnode * minipool_vector_tail;
8449static rtx minipool_vector_label;
34a9f549 8450static int minipool_pad;
332072db 8451
d5b7b3ae
RE
8452/* The linked list of all minipool fixes required for this function. */
8453Mfix * minipool_fix_head;
8454Mfix * minipool_fix_tail;
8455/* The fix entry for the current minipool, once it has been placed. */
8456Mfix * minipool_barrier;
8457
8458/* Determines if INSN is the start of a jump table. Returns the end
8459 of the TABLE or NULL_RTX. */
8460static rtx
e32bac5b 8461is_jump_table (rtx insn)
2b835d68 8462{
d5b7b3ae 8463 rtx table;
f676971a 8464
d5b7b3ae
RE
8465 if (GET_CODE (insn) == JUMP_INSN
8466 && JUMP_LABEL (insn) != NULL
8467 && ((table = next_real_insn (JUMP_LABEL (insn)))
8468 == next_real_insn (insn))
8469 && table != NULL
8470 && GET_CODE (table) == JUMP_INSN
8471 && (GET_CODE (PATTERN (table)) == ADDR_VEC
8472 || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
8473 return table;
8474
8475 return NULL_RTX;
2b835d68
RE
8476}
8477
657d9449
RE
8478#ifndef JUMP_TABLES_IN_TEXT_SECTION
8479#define JUMP_TABLES_IN_TEXT_SECTION 0
8480#endif
8481
d5b7b3ae 8482static HOST_WIDE_INT
e32bac5b 8483get_jump_table_size (rtx insn)
2b835d68 8484{
657d9449
RE
8485 /* ADDR_VECs only take room if read-only data does into the text
8486 section. */
d6b5193b 8487 if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
657d9449
RE
8488 {
8489 rtx body = PATTERN (insn);
8490 int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
5b3e6663
PB
8491 HOST_WIDE_INT size;
8492 HOST_WIDE_INT modesize;
2b835d68 8493
5b3e6663
PB
8494 modesize = GET_MODE_SIZE (GET_MODE (body));
8495 size = modesize * XVECLEN (body, elt);
8496 switch (modesize)
8497 {
8498 case 1:
88512ba0 8499 /* Round up size of TBB table to a halfword boundary. */
5b3e6663
PB
8500 size = (size + 1) & ~(HOST_WIDE_INT)1;
8501 break;
8502 case 2:
7a085dce 8503 /* No padding necessary for TBH. */
5b3e6663
PB
8504 break;
8505 case 4:
8506 /* Add two bytes for alignment on Thumb. */
8507 if (TARGET_THUMB)
8508 size += 2;
8509 break;
8510 default:
8511 gcc_unreachable ();
8512 }
8513 return size;
657d9449
RE
8514 }
8515
8516 return 0;
d5b7b3ae 8517}
2b835d68 8518
d5b7b3ae
RE
8519/* Move a minipool fix MP from its current location to before MAX_MP.
8520 If MAX_MP is NULL, then MP doesn't need moving, but the addressing
093354e0 8521 constraints may need updating. */
d5b7b3ae 8522static Mnode *
e32bac5b
RE
8523move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
8524 HOST_WIDE_INT max_address)
d5b7b3ae 8525{
e6d29d15
NS
8526 /* The code below assumes these are different. */
8527 gcc_assert (mp != max_mp);
d5b7b3ae
RE
8528
8529 if (max_mp == NULL)
8530 {
8531 if (max_address < mp->max_address)
8532 mp->max_address = max_address;
8533 }
8534 else
2b835d68 8535 {
d5b7b3ae
RE
8536 if (max_address > max_mp->max_address - mp->fix_size)
8537 mp->max_address = max_mp->max_address - mp->fix_size;
8538 else
8539 mp->max_address = max_address;
2b835d68 8540
d5b7b3ae
RE
8541 /* Unlink MP from its current position. Since max_mp is non-null,
8542 mp->prev must be non-null. */
8543 mp->prev->next = mp->next;
8544 if (mp->next != NULL)
8545 mp->next->prev = mp->prev;
8546 else
8547 minipool_vector_tail = mp->prev;
2b835d68 8548
d5b7b3ae
RE
8549 /* Re-insert it before MAX_MP. */
8550 mp->next = max_mp;
8551 mp->prev = max_mp->prev;
8552 max_mp->prev = mp;
f676971a 8553
d5b7b3ae
RE
8554 if (mp->prev != NULL)
8555 mp->prev->next = mp;
8556 else
8557 minipool_vector_head = mp;
8558 }
2b835d68 8559
d5b7b3ae
RE
8560 /* Save the new entry. */
8561 max_mp = mp;
8562
d6a7951f 8563 /* Scan over the preceding entries and adjust their addresses as
d5b7b3ae
RE
8564 required. */
8565 while (mp->prev != NULL
8566 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
8567 {
8568 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
8569 mp = mp->prev;
2b835d68
RE
8570 }
8571
d5b7b3ae 8572 return max_mp;
2b835d68
RE
8573}
8574
d5b7b3ae
RE
8575/* Add a constant to the minipool for a forward reference. Returns the
8576 node added or NULL if the constant will not fit in this pool. */
8577static Mnode *
e32bac5b 8578add_minipool_forward_ref (Mfix *fix)
d5b7b3ae
RE
8579{
8580 /* If set, max_mp is the first pool_entry that has a lower
8581 constraint than the one we are trying to add. */
8582 Mnode * max_mp = NULL;
34a9f549 8583 HOST_WIDE_INT max_address = fix->address + fix->forwards - minipool_pad;
d5b7b3ae 8584 Mnode * mp;
f676971a 8585
7a7017bc
PB
8586 /* If the minipool starts before the end of FIX->INSN then this FIX
8587 can not be placed into the current pool. Furthermore, adding the
8588 new constant pool entry may cause the pool to start FIX_SIZE bytes
8589 earlier. */
d5b7b3ae 8590 if (minipool_vector_head &&
7a7017bc
PB
8591 (fix->address + get_attr_length (fix->insn)
8592 >= minipool_vector_head->max_address - fix->fix_size))
d5b7b3ae 8593 return NULL;
2b835d68 8594
d5b7b3ae
RE
8595 /* Scan the pool to see if a constant with the same value has
8596 already been added. While we are doing this, also note the
8597 location where we must insert the constant if it doesn't already
8598 exist. */
8599 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
8600 {
8601 if (GET_CODE (fix->value) == GET_CODE (mp->value)
8602 && fix->mode == mp->mode
8603 && (GET_CODE (fix->value) != CODE_LABEL
8604 || (CODE_LABEL_NUMBER (fix->value)
8605 == CODE_LABEL_NUMBER (mp->value)))
8606 && rtx_equal_p (fix->value, mp->value))
8607 {
8608 /* More than one fix references this entry. */
8609 mp->refcount++;
8610 return move_minipool_fix_forward_ref (mp, max_mp, max_address);
8611 }
8612
8613 /* Note the insertion point if necessary. */
8614 if (max_mp == NULL
8615 && mp->max_address > max_address)
8616 max_mp = mp;
5a9335ef
NC
8617
8618 /* If we are inserting an 8-bytes aligned quantity and
8619 we have not already found an insertion point, then
8620 make sure that all such 8-byte aligned quantities are
8621 placed at the start of the pool. */
5848830f 8622 if (ARM_DOUBLEWORD_ALIGN
5a9335ef 8623 && max_mp == NULL
88f77cba
JB
8624 && fix->fix_size >= 8
8625 && mp->fix_size < 8)
5a9335ef
NC
8626 {
8627 max_mp = mp;
8628 max_address = mp->max_address;
8629 }
d5b7b3ae
RE
8630 }
8631
8632 /* The value is not currently in the minipool, so we need to create
8633 a new entry for it. If MAX_MP is NULL, the entry will be put on
8634 the end of the list since the placement is less constrained than
8635 any existing entry. Otherwise, we insert the new fix before
6bc82793 8636 MAX_MP and, if necessary, adjust the constraints on the other
d5b7b3ae 8637 entries. */
5ed6ace5 8638 mp = XNEW (Mnode);
d5b7b3ae
RE
8639 mp->fix_size = fix->fix_size;
8640 mp->mode = fix->mode;
8641 mp->value = fix->value;
8642 mp->refcount = 1;
8643 /* Not yet required for a backwards ref. */
8644 mp->min_address = -65536;
8645
8646 if (max_mp == NULL)
8647 {
8648 mp->max_address = max_address;
8649 mp->next = NULL;
8650 mp->prev = minipool_vector_tail;
8651
8652 if (mp->prev == NULL)
8653 {
8654 minipool_vector_head = mp;
8655 minipool_vector_label = gen_label_rtx ();
7551cbc7 8656 }
2b835d68 8657 else
d5b7b3ae 8658 mp->prev->next = mp;
2b835d68 8659
d5b7b3ae
RE
8660 minipool_vector_tail = mp;
8661 }
8662 else
8663 {
8664 if (max_address > max_mp->max_address - mp->fix_size)
8665 mp->max_address = max_mp->max_address - mp->fix_size;
8666 else
8667 mp->max_address = max_address;
8668
8669 mp->next = max_mp;
8670 mp->prev = max_mp->prev;
8671 max_mp->prev = mp;
8672 if (mp->prev != NULL)
8673 mp->prev->next = mp;
8674 else
8675 minipool_vector_head = mp;
8676 }
8677
8678 /* Save the new entry. */
8679 max_mp = mp;
8680
d6a7951f 8681 /* Scan over the preceding entries and adjust their addresses as
d5b7b3ae
RE
8682 required. */
8683 while (mp->prev != NULL
8684 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
8685 {
8686 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
8687 mp = mp->prev;
2b835d68
RE
8688 }
8689
d5b7b3ae
RE
8690 return max_mp;
8691}
8692
8693static Mnode *
e32bac5b
RE
8694move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
8695 HOST_WIDE_INT min_address)
d5b7b3ae
RE
8696{
8697 HOST_WIDE_INT offset;
8698
e6d29d15
NS
8699 /* The code below assumes these are different. */
8700 gcc_assert (mp != min_mp);
d5b7b3ae
RE
8701
8702 if (min_mp == NULL)
2b835d68 8703 {
d5b7b3ae
RE
8704 if (min_address > mp->min_address)
8705 mp->min_address = min_address;
8706 }
8707 else
8708 {
8709 /* We will adjust this below if it is too loose. */
8710 mp->min_address = min_address;
8711
8712 /* Unlink MP from its current position. Since min_mp is non-null,
8713 mp->next must be non-null. */
8714 mp->next->prev = mp->prev;
8715 if (mp->prev != NULL)
8716 mp->prev->next = mp->next;
8717 else
8718 minipool_vector_head = mp->next;
8719
8720 /* Reinsert it after MIN_MP. */
8721 mp->prev = min_mp;
8722 mp->next = min_mp->next;
8723 min_mp->next = mp;
8724 if (mp->next != NULL)
8725 mp->next->prev = mp;
2b835d68 8726 else
d5b7b3ae
RE
8727 minipool_vector_tail = mp;
8728 }
8729
8730 min_mp = mp;
8731
8732 offset = 0;
8733 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
8734 {
8735 mp->offset = offset;
8736 if (mp->refcount > 0)
8737 offset += mp->fix_size;
8738
8739 if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
8740 mp->next->min_address = mp->min_address + mp->fix_size;
8741 }
8742
8743 return min_mp;
f676971a 8744}
d5b7b3ae
RE
8745
8746/* Add a constant to the minipool for a backward reference. Returns the
f676971a 8747 node added or NULL if the constant will not fit in this pool.
d5b7b3ae
RE
8748
8749 Note that the code for insertion for a backwards reference can be
8750 somewhat confusing because the calculated offsets for each fix do
8751 not take into account the size of the pool (which is still under
8752 construction. */
8753static Mnode *
e32bac5b 8754add_minipool_backward_ref (Mfix *fix)
d5b7b3ae
RE
8755{
8756 /* If set, min_mp is the last pool_entry that has a lower constraint
8757 than the one we are trying to add. */
e32bac5b 8758 Mnode *min_mp = NULL;
d5b7b3ae
RE
8759 /* This can be negative, since it is only a constraint. */
8760 HOST_WIDE_INT min_address = fix->address - fix->backwards;
e32bac5b 8761 Mnode *mp;
d5b7b3ae
RE
8762
8763 /* If we can't reach the current pool from this insn, or if we can't
8764 insert this entry at the end of the pool without pushing other
8765 fixes out of range, then we don't try. This ensures that we
8766 can't fail later on. */
8767 if (min_address >= minipool_barrier->address
8768 || (minipool_vector_tail->min_address + fix->fix_size
8769 >= minipool_barrier->address))
8770 return NULL;
8771
8772 /* Scan the pool to see if a constant with the same value has
8773 already been added. While we are doing this, also note the
8774 location where we must insert the constant if it doesn't already
8775 exist. */
8776 for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
8777 {
8778 if (GET_CODE (fix->value) == GET_CODE (mp->value)
8779 && fix->mode == mp->mode
8780 && (GET_CODE (fix->value) != CODE_LABEL
8781 || (CODE_LABEL_NUMBER (fix->value)
8782 == CODE_LABEL_NUMBER (mp->value)))
8783 && rtx_equal_p (fix->value, mp->value)
8784 /* Check that there is enough slack to move this entry to the
8785 end of the table (this is conservative). */
f676971a
EC
8786 && (mp->max_address
8787 > (minipool_barrier->address
d5b7b3ae
RE
8788 + minipool_vector_tail->offset
8789 + minipool_vector_tail->fix_size)))
8790 {
8791 mp->refcount++;
8792 return move_minipool_fix_backward_ref (mp, min_mp, min_address);
8793 }
8794
8795 if (min_mp != NULL)
8796 mp->min_address += fix->fix_size;
8797 else
8798 {
8799 /* Note the insertion point if necessary. */
8800 if (mp->min_address < min_address)
5a9335ef
NC
8801 {
8802 /* For now, we do not allow the insertion of 8-byte alignment
8803 requiring nodes anywhere but at the start of the pool. */
5848830f 8804 if (ARM_DOUBLEWORD_ALIGN
88f77cba 8805 && fix->fix_size >= 8 && mp->fix_size < 8)
5a9335ef
NC
8806 return NULL;
8807 else
8808 min_mp = mp;
8809 }
d5b7b3ae
RE
8810 else if (mp->max_address
8811 < minipool_barrier->address + mp->offset + fix->fix_size)
8812 {
8813 /* Inserting before this entry would push the fix beyond
8814 its maximum address (which can happen if we have
8815 re-located a forwards fix); force the new fix to come
8816 after it. */
853ff9e2
JM
8817 if (ARM_DOUBLEWORD_ALIGN
8818 && fix->fix_size >= 8 && mp->fix_size < 8)
8819 return NULL;
8820 else
8821 {
8822 min_mp = mp;
8823 min_address = mp->min_address + fix->fix_size;
8824 }
d5b7b3ae 8825 }
853ff9e2
JM
8826 /* Do not insert a non-8-byte aligned quantity before 8-byte
8827 aligned quantities. */
5848830f 8828 else if (ARM_DOUBLEWORD_ALIGN
853ff9e2
JM
8829 && fix->fix_size < 8
8830 && mp->fix_size >= 8)
5a9335ef
NC
8831 {
8832 min_mp = mp;
8833 min_address = mp->min_address + fix->fix_size;
8834 }
d5b7b3ae
RE
8835 }
8836 }
8837
8838 /* We need to create a new entry. */
5ed6ace5 8839 mp = XNEW (Mnode);
d5b7b3ae
RE
8840 mp->fix_size = fix->fix_size;
8841 mp->mode = fix->mode;
8842 mp->value = fix->value;
8843 mp->refcount = 1;
8844 mp->max_address = minipool_barrier->address + 65536;
8845
8846 mp->min_address = min_address;
8847
8848 if (min_mp == NULL)
8849 {
8850 mp->prev = NULL;
8851 mp->next = minipool_vector_head;
8852
8853 if (mp->next == NULL)
8854 {
8855 minipool_vector_tail = mp;
8856 minipool_vector_label = gen_label_rtx ();
8857 }
8858 else
8859 mp->next->prev = mp;
8860
8861 minipool_vector_head = mp;
8862 }
8863 else
8864 {
8865 mp->next = min_mp->next;
8866 mp->prev = min_mp;
8867 min_mp->next = mp;
f676971a 8868
d5b7b3ae
RE
8869 if (mp->next != NULL)
8870 mp->next->prev = mp;
8871 else
8872 minipool_vector_tail = mp;
8873 }
8874
8875 /* Save the new entry. */
8876 min_mp = mp;
8877
8878 if (mp->prev)
8879 mp = mp->prev;
8880 else
8881 mp->offset = 0;
8882
8883 /* Scan over the following entries and adjust their offsets. */
8884 while (mp->next != NULL)
8885 {
8886 if (mp->next->min_address < mp->min_address + mp->fix_size)
8887 mp->next->min_address = mp->min_address + mp->fix_size;
8888
8889 if (mp->refcount)
8890 mp->next->offset = mp->offset + mp->fix_size;
8891 else
8892 mp->next->offset = mp->offset;
8893
8894 mp = mp->next;
8895 }
8896
8897 return min_mp;
8898}
8899
8900static void
e32bac5b 8901assign_minipool_offsets (Mfix *barrier)
d5b7b3ae
RE
8902{
8903 HOST_WIDE_INT offset = 0;
e32bac5b 8904 Mnode *mp;
d5b7b3ae
RE
8905
8906 minipool_barrier = barrier;
8907
8908 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
8909 {
8910 mp->offset = offset;
f676971a 8911
d5b7b3ae
RE
8912 if (mp->refcount > 0)
8913 offset += mp->fix_size;
8914 }
8915}
8916
8917/* Output the literal table */
8918static void
e32bac5b 8919dump_minipool (rtx scan)
d5b7b3ae 8920{
5a9335ef
NC
8921 Mnode * mp;
8922 Mnode * nmp;
8923 int align64 = 0;
8924
5848830f 8925 if (ARM_DOUBLEWORD_ALIGN)
5a9335ef 8926 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
88f77cba 8927 if (mp->refcount > 0 && mp->fix_size >= 8)
5a9335ef
NC
8928 {
8929 align64 = 1;
8930 break;
8931 }
d5b7b3ae 8932
c263766c
RH
8933 if (dump_file)
8934 fprintf (dump_file,
5a9335ef
NC
8935 ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
8936 INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
d5b7b3ae
RE
8937
8938 scan = emit_label_after (gen_label_rtx (), scan);
5a9335ef 8939 scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
d5b7b3ae
RE
8940 scan = emit_label_after (minipool_vector_label, scan);
8941
8942 for (mp = minipool_vector_head; mp != NULL; mp = nmp)
8943 {
8944 if (mp->refcount > 0)
8945 {
c263766c 8946 if (dump_file)
d5b7b3ae 8947 {
f676971a 8948 fprintf (dump_file,
d5b7b3ae
RE
8949 ";; Offset %u, min %ld, max %ld ",
8950 (unsigned) mp->offset, (unsigned long) mp->min_address,
8951 (unsigned long) mp->max_address);
c263766c
RH
8952 arm_print_value (dump_file, mp->value);
8953 fputc ('\n', dump_file);
d5b7b3ae
RE
8954 }
8955
8956 switch (mp->fix_size)
8957 {
8958#ifdef HAVE_consttable_1
8959 case 1:
8960 scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
8961 break;
8962
8963#endif
8964#ifdef HAVE_consttable_2
8965 case 2:
8966 scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
8967 break;
8968
8969#endif
8970#ifdef HAVE_consttable_4
8971 case 4:
8972 scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
8973 break;
8974
8975#endif
8976#ifdef HAVE_consttable_8
8977 case 8:
8978 scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
8979 break;
8980
88f77cba
JB
8981#endif
8982#ifdef HAVE_consttable_16
8983 case 16:
8984 scan = emit_insn_after (gen_consttable_16 (mp->value), scan);
8985 break;
8986
d5b7b3ae
RE
8987#endif
8988 default:
e6d29d15 8989 gcc_unreachable ();
d5b7b3ae
RE
8990 }
8991 }
8992
8993 nmp = mp->next;
8994 free (mp);
2b835d68
RE
8995 }
8996
d5b7b3ae
RE
8997 minipool_vector_head = minipool_vector_tail = NULL;
8998 scan = emit_insn_after (gen_consttable_end (), scan);
8999 scan = emit_barrier_after (scan);
2b835d68
RE
9000}
9001
d5b7b3ae
RE
9002/* Return the cost of forcibly inserting a barrier after INSN. */
9003static int
e32bac5b 9004arm_barrier_cost (rtx insn)
949d79eb 9005{
d5b7b3ae
RE
9006 /* Basing the location of the pool on the loop depth is preferable,
9007 but at the moment, the basic block information seems to be
9008 corrupt by this stage of the compilation. */
9009 int base_cost = 50;
9010 rtx next = next_nonnote_insn (insn);
9011
9012 if (next != NULL && GET_CODE (next) == CODE_LABEL)
9013 base_cost -= 20;
9014
9015 switch (GET_CODE (insn))
9016 {
9017 case CODE_LABEL:
9018 /* It will always be better to place the table before the label, rather
9019 than after it. */
f676971a 9020 return 50;
949d79eb 9021
d5b7b3ae
RE
9022 case INSN:
9023 case CALL_INSN:
9024 return base_cost;
9025
9026 case JUMP_INSN:
9027 return base_cost - 10;
9028
9029 default:
9030 return base_cost + 10;
9031 }
9032}
9033
9034/* Find the best place in the insn stream in the range
9035 (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
9036 Create the barrier by inserting a jump and add a new fix entry for
9037 it. */
9038static Mfix *
e32bac5b 9039create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
d5b7b3ae
RE
9040{
9041 HOST_WIDE_INT count = 0;
9042 rtx barrier;
9043 rtx from = fix->insn;
7a7017bc
PB
9044 /* The instruction after which we will insert the jump. */
9045 rtx selected = NULL;
d5b7b3ae 9046 int selected_cost;
7a7017bc 9047 /* The address at which the jump instruction will be placed. */
d5b7b3ae
RE
9048 HOST_WIDE_INT selected_address;
9049 Mfix * new_fix;
9050 HOST_WIDE_INT max_count = max_address - fix->address;
9051 rtx label = gen_label_rtx ();
9052
9053 selected_cost = arm_barrier_cost (from);
9054 selected_address = fix->address;
9055
9056 while (from && count < max_count)
9057 {
9058 rtx tmp;
9059 int new_cost;
9060
9061 /* This code shouldn't have been called if there was a natural barrier
9062 within range. */
e6d29d15 9063 gcc_assert (GET_CODE (from) != BARRIER);
d5b7b3ae
RE
9064
9065 /* Count the length of this insn. */
9066 count += get_attr_length (from);
9067
9068 /* If there is a jump table, add its length. */
9069 tmp = is_jump_table (from);
9070 if (tmp != NULL)
9071 {
9072 count += get_jump_table_size (tmp);
9073
9074 /* Jump tables aren't in a basic block, so base the cost on
9075 the dispatch insn. If we select this location, we will
9076 still put the pool after the table. */
9077 new_cost = arm_barrier_cost (from);
9078
7a7017bc
PB
9079 if (count < max_count
9080 && (!selected || new_cost <= selected_cost))
d5b7b3ae
RE
9081 {
9082 selected = tmp;
9083 selected_cost = new_cost;
9084 selected_address = fix->address + count;
9085 }
9086
9087 /* Continue after the dispatch table. */
9088 from = NEXT_INSN (tmp);
9089 continue;
9090 }
9091
9092 new_cost = arm_barrier_cost (from);
f676971a 9093
7a7017bc
PB
9094 if (count < max_count
9095 && (!selected || new_cost <= selected_cost))
d5b7b3ae
RE
9096 {
9097 selected = from;
9098 selected_cost = new_cost;
9099 selected_address = fix->address + count;
9100 }
9101
9102 from = NEXT_INSN (from);
9103 }
9104
7a7017bc
PB
9105 /* Make sure that we found a place to insert the jump. */
9106 gcc_assert (selected);
9107
d5b7b3ae
RE
9108 /* Create a new JUMP_INSN that branches around a barrier. */
9109 from = emit_jump_insn_after (gen_jump (label), selected);
9110 JUMP_LABEL (from) = label;
9111 barrier = emit_barrier_after (from);
9112 emit_label_after (label, barrier);
9113
9114 /* Create a minipool barrier entry for the new barrier. */
c7319d87 9115 new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
d5b7b3ae
RE
9116 new_fix->insn = barrier;
9117 new_fix->address = selected_address;
9118 new_fix->next = fix->next;
9119 fix->next = new_fix;
9120
9121 return new_fix;
9122}
9123
9124/* Record that there is a natural barrier in the insn stream at
9125 ADDRESS. */
949d79eb 9126static void
e32bac5b 9127push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
2b835d68 9128{
c7319d87 9129 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
ad076f4e 9130
949d79eb
RE
9131 fix->insn = insn;
9132 fix->address = address;
2b835d68 9133
949d79eb
RE
9134 fix->next = NULL;
9135 if (minipool_fix_head != NULL)
9136 minipool_fix_tail->next = fix;
9137 else
9138 minipool_fix_head = fix;
9139
9140 minipool_fix_tail = fix;
9141}
2b835d68 9142
d5b7b3ae
RE
9143/* Record INSN, which will need fixing up to load a value from the
9144 minipool. ADDRESS is the offset of the insn since the start of the
9145 function; LOC is a pointer to the part of the insn which requires
9146 fixing; VALUE is the constant that must be loaded, which is of type
9147 MODE. */
949d79eb 9148static void
e32bac5b
RE
9149push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
9150 enum machine_mode mode, rtx value)
949d79eb 9151{
c7319d87 9152 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
949d79eb 9153
949d79eb
RE
9154 fix->insn = insn;
9155 fix->address = address;
9156 fix->loc = loc;
9157 fix->mode = mode;
d5b7b3ae 9158 fix->fix_size = MINIPOOL_FIX_SIZE (mode);
949d79eb 9159 fix->value = value;
d5b7b3ae
RE
9160 fix->forwards = get_attr_pool_range (insn);
9161 fix->backwards = get_attr_neg_pool_range (insn);
9162 fix->minipool = NULL;
949d79eb
RE
9163
9164 /* If an insn doesn't have a range defined for it, then it isn't
e6d29d15 9165 expecting to be reworked by this code. Better to stop now than
949d79eb 9166 to generate duff assembly code. */
e6d29d15 9167 gcc_assert (fix->forwards || fix->backwards);
949d79eb 9168
34a9f549
PB
9169 /* If an entry requires 8-byte alignment then assume all constant pools
9170 require 4 bytes of padding. Trying to do this later on a per-pool
917f1b7e 9171 basis is awkward because existing pool entries have to be modified. */
88f77cba 9172 if (ARM_DOUBLEWORD_ALIGN && fix->fix_size >= 8)
34a9f549 9173 minipool_pad = 4;
5a9335ef 9174
c263766c 9175 if (dump_file)
d5b7b3ae 9176 {
c263766c 9177 fprintf (dump_file,
d5b7b3ae
RE
9178 ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
9179 GET_MODE_NAME (mode),
f676971a 9180 INSN_UID (insn), (unsigned long) address,
d5b7b3ae 9181 -1 * (long)fix->backwards, (long)fix->forwards);
c263766c
RH
9182 arm_print_value (dump_file, fix->value);
9183 fprintf (dump_file, "\n");
d5b7b3ae
RE
9184 }
9185
6354dc9b 9186 /* Add it to the chain of fixes. */
949d79eb 9187 fix->next = NULL;
f676971a 9188
949d79eb
RE
9189 if (minipool_fix_head != NULL)
9190 minipool_fix_tail->next = fix;
9191 else
9192 minipool_fix_head = fix;
9193
9194 minipool_fix_tail = fix;
9195}
9196
9b901d50 9197/* Return the cost of synthesizing a 64-bit constant VAL inline.
2075b05d
RE
9198 Returns the number of insns needed, or 99 if we don't know how to
9199 do it. */
9200int
9201arm_const_double_inline_cost (rtx val)
b9e8bfda 9202{
9b901d50
RE
9203 rtx lowpart, highpart;
9204 enum machine_mode mode;
e0b92319 9205
9b901d50 9206 mode = GET_MODE (val);
b9e8bfda 9207
9b901d50
RE
9208 if (mode == VOIDmode)
9209 mode = DImode;
9210
9211 gcc_assert (GET_MODE_SIZE (mode) == 8);
e0b92319 9212
9b901d50
RE
9213 lowpart = gen_lowpart (SImode, val);
9214 highpart = gen_highpart_mode (SImode, mode, val);
e0b92319 9215
9b901d50
RE
9216 gcc_assert (GET_CODE (lowpart) == CONST_INT);
9217 gcc_assert (GET_CODE (highpart) == CONST_INT);
9218
9219 return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
2075b05d 9220 NULL_RTX, NULL_RTX, 0, 0)
9b901d50 9221 + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
2075b05d
RE
9222 NULL_RTX, NULL_RTX, 0, 0));
9223}
9224
f3b569ca 9225/* Return true if it is worthwhile to split a 64-bit constant into two
b4a58f80
RE
9226 32-bit operations. This is the case if optimizing for size, or
9227 if we have load delay slots, or if one 32-bit part can be done with
9228 a single data operation. */
9229bool
9230arm_const_double_by_parts (rtx val)
9231{
9232 enum machine_mode mode = GET_MODE (val);
9233 rtx part;
9234
9235 if (optimize_size || arm_ld_sched)
9236 return true;
9237
9238 if (mode == VOIDmode)
9239 mode = DImode;
e0b92319 9240
b4a58f80 9241 part = gen_highpart_mode (SImode, mode, val);
e0b92319 9242
b4a58f80 9243 gcc_assert (GET_CODE (part) == CONST_INT);
e0b92319 9244
b4a58f80
RE
9245 if (const_ok_for_arm (INTVAL (part))
9246 || const_ok_for_arm (~INTVAL (part)))
9247 return true;
e0b92319 9248
b4a58f80 9249 part = gen_lowpart (SImode, val);
e0b92319 9250
b4a58f80 9251 gcc_assert (GET_CODE (part) == CONST_INT);
e0b92319 9252
b4a58f80
RE
9253 if (const_ok_for_arm (INTVAL (part))
9254 || const_ok_for_arm (~INTVAL (part)))
9255 return true;
e0b92319 9256
b4a58f80
RE
9257 return false;
9258}
9259
f0375c66
NC
9260/* Scan INSN and note any of its operands that need fixing.
9261 If DO_PUSHES is false we do not actually push any of the fixups
9b901d50 9262 needed. The function returns TRUE if any fixups were needed/pushed.
f0375c66
NC
9263 This is used by arm_memory_load_p() which needs to know about loads
9264 of constants that will be converted into minipool loads. */
f0375c66 9265static bool
e32bac5b 9266note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
949d79eb 9267{
f0375c66 9268 bool result = false;
949d79eb
RE
9269 int opno;
9270
d5b7b3ae 9271 extract_insn (insn);
949d79eb 9272
5895f793 9273 if (!constrain_operands (1))
949d79eb
RE
9274 fatal_insn_not_found (insn);
9275
8c2a5582
RE
9276 if (recog_data.n_alternatives == 0)
9277 return false;
9278
9b901d50
RE
9279 /* Fill in recog_op_alt with information about the constraints of
9280 this insn. */
949d79eb
RE
9281 preprocess_constraints ();
9282
1ccbefce 9283 for (opno = 0; opno < recog_data.n_operands; opno++)
949d79eb 9284 {
6354dc9b 9285 /* Things we need to fix can only occur in inputs. */
36ab44c7 9286 if (recog_data.operand_type[opno] != OP_IN)
949d79eb
RE
9287 continue;
9288
9289 /* If this alternative is a memory reference, then any mention
9290 of constants in this alternative is really to fool reload
9291 into allowing us to accept one there. We need to fix them up
9292 now so that we output the right code. */
9293 if (recog_op_alt[opno][which_alternative].memory_ok)
9294 {
1ccbefce 9295 rtx op = recog_data.operand[opno];
949d79eb 9296
9b901d50 9297 if (CONSTANT_P (op))
f0375c66
NC
9298 {
9299 if (do_pushes)
9300 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
9301 recog_data.operand_mode[opno], op);
9302 result = true;
9303 }
d5b7b3ae 9304 else if (GET_CODE (op) == MEM
949d79eb
RE
9305 && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
9306 && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
f0375c66
NC
9307 {
9308 if (do_pushes)
244b1afb
RE
9309 {
9310 rtx cop = avoid_constant_pool_reference (op);
9311
9312 /* Casting the address of something to a mode narrower
9313 than a word can cause avoid_constant_pool_reference()
9314 to return the pool reference itself. That's no good to
f676971a 9315 us here. Lets just hope that we can use the
244b1afb
RE
9316 constant pool value directly. */
9317 if (op == cop)
c769a35d 9318 cop = get_pool_constant (XEXP (op, 0));
244b1afb
RE
9319
9320 push_minipool_fix (insn, address,
9321 recog_data.operand_loc[opno],
c769a35d 9322 recog_data.operand_mode[opno], cop);
244b1afb 9323 }
f0375c66
NC
9324
9325 result = true;
9326 }
949d79eb 9327 }
2b835d68 9328 }
f0375c66
NC
9329
9330 return result;
2b835d68
RE
9331}
9332
18dbd950
RS
9333/* Gcc puts the pool in the wrong place for ARM, since we can only
9334 load addresses a limited distance around the pc. We do some
9335 special munging to move the constant pool values to the correct
9336 point in the code. */
18dbd950 9337static void
e32bac5b 9338arm_reorg (void)
2b835d68
RE
9339{
9340 rtx insn;
d5b7b3ae
RE
9341 HOST_WIDE_INT address = 0;
9342 Mfix * fix;
ad076f4e 9343
949d79eb 9344 minipool_fix_head = minipool_fix_tail = NULL;
2b835d68 9345
949d79eb
RE
9346 /* The first insn must always be a note, or the code below won't
9347 scan it properly. */
18dbd950 9348 insn = get_insns ();
e6d29d15 9349 gcc_assert (GET_CODE (insn) == NOTE);
34a9f549 9350 minipool_pad = 0;
949d79eb
RE
9351
9352 /* Scan all the insns and record the operands that will need fixing. */
18dbd950 9353 for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
2b835d68 9354 {
9b6b54e2 9355 if (TARGET_CIRRUS_FIX_INVALID_INSNS
f0375c66 9356 && (arm_cirrus_insn_p (insn)
9b6b54e2 9357 || GET_CODE (insn) == JUMP_INSN
f0375c66 9358 || arm_memory_load_p (insn)))
9b6b54e2
NC
9359 cirrus_reorg (insn);
9360
949d79eb 9361 if (GET_CODE (insn) == BARRIER)
d5b7b3ae 9362 push_minipool_barrier (insn, address);
f0375c66 9363 else if (INSN_P (insn))
949d79eb
RE
9364 {
9365 rtx table;
9366
f0375c66 9367 note_invalid_constants (insn, address, true);
949d79eb 9368 address += get_attr_length (insn);
d5b7b3ae 9369
949d79eb
RE
9370 /* If the insn is a vector jump, add the size of the table
9371 and skip the table. */
d5b7b3ae 9372 if ((table = is_jump_table (insn)) != NULL)
2b835d68 9373 {
d5b7b3ae 9374 address += get_jump_table_size (table);
949d79eb
RE
9375 insn = table;
9376 }
9377 }
9378 }
332072db 9379
d5b7b3ae 9380 fix = minipool_fix_head;
f676971a 9381
949d79eb 9382 /* Now scan the fixups and perform the required changes. */
d5b7b3ae 9383 while (fix)
949d79eb 9384 {
d5b7b3ae
RE
9385 Mfix * ftmp;
9386 Mfix * fdel;
9387 Mfix * last_added_fix;
9388 Mfix * last_barrier = NULL;
9389 Mfix * this_fix;
949d79eb
RE
9390
9391 /* Skip any further barriers before the next fix. */
9392 while (fix && GET_CODE (fix->insn) == BARRIER)
9393 fix = fix->next;
9394
d5b7b3ae 9395 /* No more fixes. */
949d79eb
RE
9396 if (fix == NULL)
9397 break;
332072db 9398
d5b7b3ae 9399 last_added_fix = NULL;
2b835d68 9400
d5b7b3ae 9401 for (ftmp = fix; ftmp; ftmp = ftmp->next)
949d79eb 9402 {
949d79eb 9403 if (GET_CODE (ftmp->insn) == BARRIER)
949d79eb 9404 {
d5b7b3ae
RE
9405 if (ftmp->address >= minipool_vector_head->max_address)
9406 break;
2b835d68 9407
d5b7b3ae 9408 last_barrier = ftmp;
2b835d68 9409 }
d5b7b3ae
RE
9410 else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
9411 break;
9412
9413 last_added_fix = ftmp; /* Keep track of the last fix added. */
2b835d68 9414 }
949d79eb 9415
d5b7b3ae
RE
9416 /* If we found a barrier, drop back to that; any fixes that we
9417 could have reached but come after the barrier will now go in
9418 the next mini-pool. */
949d79eb
RE
9419 if (last_barrier != NULL)
9420 {
f676971a 9421 /* Reduce the refcount for those fixes that won't go into this
d5b7b3ae
RE
9422 pool after all. */
9423 for (fdel = last_barrier->next;
9424 fdel && fdel != ftmp;
9425 fdel = fdel->next)
9426 {
9427 fdel->minipool->refcount--;
9428 fdel->minipool = NULL;
9429 }
9430
949d79eb
RE
9431 ftmp = last_barrier;
9432 }
9433 else
2bfa88dc 9434 {
d5b7b3ae
RE
9435 /* ftmp is first fix that we can't fit into this pool and
9436 there no natural barriers that we could use. Insert a
9437 new barrier in the code somewhere between the previous
9438 fix and this one, and arrange to jump around it. */
9439 HOST_WIDE_INT max_address;
9440
9441 /* The last item on the list of fixes must be a barrier, so
9442 we can never run off the end of the list of fixes without
9443 last_barrier being set. */
e6d29d15 9444 gcc_assert (ftmp);
d5b7b3ae
RE
9445
9446 max_address = minipool_vector_head->max_address;
2bfa88dc
RE
9447 /* Check that there isn't another fix that is in range that
9448 we couldn't fit into this pool because the pool was
9449 already too large: we need to put the pool before such an
7a7017bc
PB
9450 instruction. The pool itself may come just after the
9451 fix because create_fix_barrier also allows space for a
9452 jump instruction. */
d5b7b3ae 9453 if (ftmp->address < max_address)
7a7017bc 9454 max_address = ftmp->address + 1;
d5b7b3ae
RE
9455
9456 last_barrier = create_fix_barrier (last_added_fix, max_address);
9457 }
9458
9459 assign_minipool_offsets (last_barrier);
9460
9461 while (ftmp)
9462 {
9463 if (GET_CODE (ftmp->insn) != BARRIER
9464 && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
9465 == NULL))
9466 break;
2bfa88dc 9467
d5b7b3ae 9468 ftmp = ftmp->next;
2bfa88dc 9469 }
949d79eb
RE
9470
9471 /* Scan over the fixes we have identified for this pool, fixing them
9472 up and adding the constants to the pool itself. */
d5b7b3ae 9473 for (this_fix = fix; this_fix && ftmp != this_fix;
949d79eb
RE
9474 this_fix = this_fix->next)
9475 if (GET_CODE (this_fix->insn) != BARRIER)
9476 {
949d79eb 9477 rtx addr
f676971a 9478 = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
949d79eb 9479 minipool_vector_label),
d5b7b3ae 9480 this_fix->minipool->offset);
949d79eb
RE
9481 *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
9482 }
9483
d5b7b3ae 9484 dump_minipool (last_barrier->insn);
949d79eb 9485 fix = ftmp;
2b835d68 9486 }
4b632bf1 9487
949d79eb
RE
9488 /* From now on we must synthesize any constants that we can't handle
9489 directly. This can happen if the RTL gets split during final
9490 instruction generation. */
4b632bf1 9491 after_arm_reorg = 1;
c7319d87
RE
9492
9493 /* Free the minipool memory. */
9494 obstack_free (&minipool_obstack, minipool_startobj);
2b835d68 9495}
cce8749e
CH
9496\f
9497/* Routines to output assembly language. */
9498
f3bb6135 9499/* If the rtx is the correct value then return the string of the number.
ff9940b0 9500 In this way we can ensure that valid double constants are generated even
6354dc9b 9501 when cross compiling. */
cd2b33d0 9502const char *
e32bac5b 9503fp_immediate_constant (rtx x)
ff9940b0
RE
9504{
9505 REAL_VALUE_TYPE r;
9506 int i;
f676971a 9507
9b66ebb1
PB
9508 if (!fp_consts_inited)
9509 init_fp_table ();
f676971a 9510
ff9940b0
RE
9511 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
9512 for (i = 0; i < 8; i++)
9b66ebb1
PB
9513 if (REAL_VALUES_EQUAL (r, values_fp[i]))
9514 return strings_fp[i];
f3bb6135 9515
e6d29d15 9516 gcc_unreachable ();
ff9940b0
RE
9517}
9518
9997d19d 9519/* As for fp_immediate_constant, but value is passed directly, not in rtx. */
cd2b33d0 9520static const char *
e32bac5b 9521fp_const_from_val (REAL_VALUE_TYPE *r)
9997d19d
RE
9522{
9523 int i;
9524
9b66ebb1
PB
9525 if (!fp_consts_inited)
9526 init_fp_table ();
9997d19d
RE
9527
9528 for (i = 0; i < 8; i++)
9b66ebb1
PB
9529 if (REAL_VALUES_EQUAL (*r, values_fp[i]))
9530 return strings_fp[i];
9997d19d 9531
e6d29d15 9532 gcc_unreachable ();
9997d19d 9533}
ff9940b0 9534
cce8749e
CH
9535/* Output the operands of a LDM/STM instruction to STREAM.
9536 MASK is the ARM register set mask of which only bits 0-15 are important.
6d3d9133 9537 REG is the base register, either the frame pointer or the stack pointer,
a15908a4
PB
9538 INSTR is the possibly suffixed load or store instruction.
9539 RFE is nonzero if the instruction should also copy spsr to cpsr. */
b279b20a 9540
d5b7b3ae 9541static void
b279b20a 9542print_multi_reg (FILE *stream, const char *instr, unsigned reg,
a15908a4 9543 unsigned long mask, int rfe)
cce8749e 9544{
b279b20a
NC
9545 unsigned i;
9546 bool not_first = FALSE;
cce8749e 9547
a15908a4 9548 gcc_assert (!rfe || (mask & (1 << PC_REGNUM)));
1d5473cb 9549 fputc ('\t', stream);
dd18ae56 9550 asm_fprintf (stream, instr, reg);
5b3e6663 9551 fputc ('{', stream);
f676971a 9552
d5b7b3ae 9553 for (i = 0; i <= LAST_ARM_REGNUM; i++)
cce8749e
CH
9554 if (mask & (1 << i))
9555 {
9556 if (not_first)
9557 fprintf (stream, ", ");
f676971a 9558
dd18ae56 9559 asm_fprintf (stream, "%r", i);
cce8749e
CH
9560 not_first = TRUE;
9561 }
f3bb6135 9562
a15908a4
PB
9563 if (rfe)
9564 fprintf (stream, "}^\n");
9565 else
9566 fprintf (stream, "}\n");
f3bb6135 9567}
cce8749e 9568
9b66ebb1 9569
8edfc4cc 9570/* Output a FLDMD instruction to STREAM.
9728c9d1
PB
9571 BASE if the register containing the address.
9572 REG and COUNT specify the register range.
8edfc4cc
MS
9573 Extra registers may be added to avoid hardware bugs.
9574
9575 We output FLDMD even for ARMv5 VFP implementations. Although
9576 FLDMD is technically not supported until ARMv6, it is believed
9577 that all VFP implementations support its use in this context. */
9b66ebb1
PB
9578
9579static void
8edfc4cc 9580vfp_output_fldmd (FILE * stream, unsigned int base, int reg, int count)
9b66ebb1
PB
9581{
9582 int i;
9583
9728c9d1
PB
9584 /* Workaround ARM10 VFPr1 bug. */
9585 if (count == 2 && !arm_arch6)
9586 {
9587 if (reg == 15)
9588 reg--;
9589 count++;
9590 }
9591
f1adb0a9
JB
9592 /* FLDMD may not load more than 16 doubleword registers at a time. Split the
9593 load into multiple parts if we have to handle more than 16 registers. */
9594 if (count > 16)
9595 {
9596 vfp_output_fldmd (stream, base, reg, 16);
9597 vfp_output_fldmd (stream, base, reg + 16, count - 16);
9598 return;
9599 }
9600
9b66ebb1 9601 fputc ('\t', stream);
8edfc4cc 9602 asm_fprintf (stream, "fldmfdd\t%r!, {", base);
9b66ebb1 9603
9728c9d1 9604 for (i = reg; i < reg + count; i++)
9b66ebb1 9605 {
9728c9d1 9606 if (i > reg)
9b66ebb1 9607 fputs (", ", stream);
9728c9d1 9608 asm_fprintf (stream, "d%d", i);
9b66ebb1
PB
9609 }
9610 fputs ("}\n", stream);
9728c9d1 9611
9b66ebb1
PB
9612}
9613
9614
9615/* Output the assembly for a store multiple. */
9616
9617const char *
8edfc4cc 9618vfp_output_fstmd (rtx * operands)
9b66ebb1
PB
9619{
9620 char pattern[100];
9621 int p;
9622 int base;
9623 int i;
9624
8edfc4cc 9625 strcpy (pattern, "fstmfdd\t%m0!, {%P1");
9b66ebb1
PB
9626 p = strlen (pattern);
9627
e6d29d15 9628 gcc_assert (GET_CODE (operands[1]) == REG);
9b66ebb1
PB
9629
9630 base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
9631 for (i = 1; i < XVECLEN (operands[2], 0); i++)
9632 {
9633 p += sprintf (&pattern[p], ", d%d", base + i);
9634 }
9635 strcpy (&pattern[p], "}");
9636
9637 output_asm_insn (pattern, operands);
9638 return "";
9639}
9640
9641
9728c9d1
PB
9642/* Emit RTL to save block of VFP register pairs to the stack. Returns the
9643 number of bytes pushed. */
9b66ebb1 9644
9728c9d1 9645static int
8edfc4cc 9646vfp_emit_fstmd (int base_reg, int count)
9b66ebb1
PB
9647{
9648 rtx par;
9649 rtx dwarf;
9650 rtx tmp, reg;
9651 int i;
9652
9728c9d1
PB
9653 /* Workaround ARM10 VFPr1 bug. Data corruption can occur when exactly two
9654 register pairs are stored by a store multiple insn. We avoid this
9655 by pushing an extra pair. */
9656 if (count == 2 && !arm_arch6)
9657 {
9658 if (base_reg == LAST_VFP_REGNUM - 3)
9659 base_reg -= 2;
9660 count++;
9661 }
9662
f1adb0a9
JB
9663 /* FSTMD may not store more than 16 doubleword registers at once. Split
9664 larger stores into multiple parts (up to a maximum of two, in
9665 practice). */
9666 if (count > 16)
9667 {
9668 int saved;
9669 /* NOTE: base_reg is an internal register number, so each D register
9670 counts as 2. */
9671 saved = vfp_emit_fstmd (base_reg + 32, count - 16);
9672 saved += vfp_emit_fstmd (base_reg, 16);
9673 return saved;
9674 }
9675
9b66ebb1
PB
9676 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9677 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
9678
9679 reg = gen_rtx_REG (DFmode, base_reg);
9680 base_reg += 2;
9681
9682 XVECEXP (par, 0, 0)
9683 = gen_rtx_SET (VOIDmode,
31fa16b6
RE
9684 gen_frame_mem (BLKmode,
9685 gen_rtx_PRE_DEC (BLKmode,
9686 stack_pointer_rtx)),
9b66ebb1
PB
9687 gen_rtx_UNSPEC (BLKmode,
9688 gen_rtvec (1, reg),
9689 UNSPEC_PUSH_MULT));
9690
9691 tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8edfc4cc 9692 plus_constant (stack_pointer_rtx, -(count * 8)));
9b66ebb1
PB
9693 RTX_FRAME_RELATED_P (tmp) = 1;
9694 XVECEXP (dwarf, 0, 0) = tmp;
9695
9696 tmp = gen_rtx_SET (VOIDmode,
31fa16b6 9697 gen_frame_mem (DFmode, stack_pointer_rtx),
9b66ebb1
PB
9698 reg);
9699 RTX_FRAME_RELATED_P (tmp) = 1;
9700 XVECEXP (dwarf, 0, 1) = tmp;
9701
9702 for (i = 1; i < count; i++)
9703 {
9704 reg = gen_rtx_REG (DFmode, base_reg);
9705 base_reg += 2;
9706 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
9707
9708 tmp = gen_rtx_SET (VOIDmode,
31fa16b6 9709 gen_frame_mem (DFmode,
d66437c5
RE
9710 plus_constant (stack_pointer_rtx,
9711 i * 8)),
9b66ebb1
PB
9712 reg);
9713 RTX_FRAME_RELATED_P (tmp) = 1;
9714 XVECEXP (dwarf, 0, i + 1) = tmp;
9715 }
9716
9717 par = emit_insn (par);
9718 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9719 REG_NOTES (par));
9728c9d1
PB
9720 RTX_FRAME_RELATED_P (par) = 1;
9721
8edfc4cc 9722 return count * 8;
9b66ebb1
PB
9723}
9724
9403b7f7
RS
9725/* Emit a call instruction with pattern PAT. ADDR is the address of
9726 the call target. */
9727
9728void
9729arm_emit_call_insn (rtx pat, rtx addr)
9730{
9731 rtx insn;
9732
9733 insn = emit_call_insn (pat);
9734
9735 /* The PIC register is live on entry to VxWorks PIC PLT entries.
9736 If the call might use such an entry, add a use of the PIC register
9737 to the instruction's CALL_INSN_FUNCTION_USAGE. */
9738 if (TARGET_VXWORKS_RTP
9739 && flag_pic
9740 && GET_CODE (addr) == SYMBOL_REF
9741 && (SYMBOL_REF_DECL (addr)
9742 ? !targetm.binds_local_p (SYMBOL_REF_DECL (addr))
9743 : !SYMBOL_REF_LOCAL_P (addr)))
9744 {
9745 require_pic_register ();
9746 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), cfun->machine->pic_reg);
9747 }
9748}
9b66ebb1 9749
6354dc9b 9750/* Output a 'call' insn. */
cd2b33d0 9751const char *
e32bac5b 9752output_call (rtx *operands)
cce8749e 9753{
e6d29d15 9754 gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly. */
cce8749e 9755
68d560d4 9756 /* Handle calls to lr using ip (which may be clobbered in subr anyway). */
62b10bbc 9757 if (REGNO (operands[0]) == LR_REGNUM)
cce8749e 9758 {
62b10bbc 9759 operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
1d5473cb 9760 output_asm_insn ("mov%?\t%0, %|lr", operands);
cce8749e 9761 }
f676971a 9762
1d5473cb 9763 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
f676971a 9764
68d560d4 9765 if (TARGET_INTERWORK || arm_arch4t)
da6558fd
NC
9766 output_asm_insn ("bx%?\t%0", operands);
9767 else
9768 output_asm_insn ("mov%?\t%|pc, %0", operands);
f676971a 9769
f3bb6135
RE
9770 return "";
9771}
cce8749e 9772
6354dc9b 9773/* Output a 'call' insn that is a reference in memory. */
cd2b33d0 9774const char *
e32bac5b 9775output_call_mem (rtx *operands)
ff9940b0 9776{
68d560d4 9777 if (TARGET_INTERWORK && !arm_arch5)
da6558fd
NC
9778 {
9779 output_asm_insn ("ldr%?\t%|ip, %0", operands);
9780 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
9781 output_asm_insn ("bx%?\t%|ip", operands);
9782 }
6ab5da80
RE
9783 else if (regno_use_in (LR_REGNUM, operands[0]))
9784 {
9785 /* LR is used in the memory address. We load the address in the
9786 first instruction. It's safe to use IP as the target of the
9787 load since the call will kill it anyway. */
9788 output_asm_insn ("ldr%?\t%|ip, %0", operands);
68d560d4 9789 if (arm_arch5)
07ec3810 9790 output_asm_insn ("blx%?\t%|ip", operands);
68d560d4
RE
9791 else
9792 {
9793 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
9794 if (arm_arch4t)
9795 output_asm_insn ("bx%?\t%|ip", operands);
9796 else
9797 output_asm_insn ("mov%?\t%|pc, %|ip", operands);
9798 }
6ab5da80 9799 }
da6558fd
NC
9800 else
9801 {
9802 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
9803 output_asm_insn ("ldr%?\t%|pc, %0", operands);
9804 }
9805
f3bb6135
RE
9806 return "";
9807}
ff9940b0
RE
9808
9809
3b684012
RE
9810/* Output a move from arm registers to an fpa registers.
9811 OPERANDS[0] is an fpa register.
ff9940b0 9812 OPERANDS[1] is the first registers of an arm register pair. */
cd2b33d0 9813const char *
e32bac5b 9814output_mov_long_double_fpa_from_arm (rtx *operands)
ff9940b0
RE
9815{
9816 int arm_reg0 = REGNO (operands[1]);
9817 rtx ops[3];
9818
e6d29d15 9819 gcc_assert (arm_reg0 != IP_REGNUM);
f3bb6135 9820
43cffd11
RE
9821 ops[0] = gen_rtx_REG (SImode, arm_reg0);
9822 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
9823 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
f676971a 9824
5b3e6663 9825 output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
1d5473cb 9826 output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
f676971a 9827
f3bb6135
RE
9828 return "";
9829}
ff9940b0 9830
3b684012 9831/* Output a move from an fpa register to arm registers.
ff9940b0 9832 OPERANDS[0] is the first registers of an arm register pair.
3b684012 9833 OPERANDS[1] is an fpa register. */
cd2b33d0 9834const char *
e32bac5b 9835output_mov_long_double_arm_from_fpa (rtx *operands)
ff9940b0
RE
9836{
9837 int arm_reg0 = REGNO (operands[0]);
9838 rtx ops[3];
9839
e6d29d15 9840 gcc_assert (arm_reg0 != IP_REGNUM);
f3bb6135 9841
43cffd11
RE
9842 ops[0] = gen_rtx_REG (SImode, arm_reg0);
9843 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
9844 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
ff9940b0 9845
1d5473cb 9846 output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
5b3e6663 9847 output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
f3bb6135
RE
9848 return "";
9849}
ff9940b0
RE
9850
9851/* Output a move from arm registers to arm registers of a long double
9852 OPERANDS[0] is the destination.
9853 OPERANDS[1] is the source. */
cd2b33d0 9854const char *
e32bac5b 9855output_mov_long_double_arm_from_arm (rtx *operands)
ff9940b0 9856{
6354dc9b 9857 /* We have to be careful here because the two might overlap. */
ff9940b0
RE
9858 int dest_start = REGNO (operands[0]);
9859 int src_start = REGNO (operands[1]);
9860 rtx ops[2];
9861 int i;
9862
9863 if (dest_start < src_start)
9864 {
9865 for (i = 0; i < 3; i++)
9866 {
43cffd11
RE
9867 ops[0] = gen_rtx_REG (SImode, dest_start + i);
9868 ops[1] = gen_rtx_REG (SImode, src_start + i);
9997d19d 9869 output_asm_insn ("mov%?\t%0, %1", ops);
ff9940b0
RE
9870 }
9871 }
9872 else
9873 {
9874 for (i = 2; i >= 0; i--)
9875 {
43cffd11
RE
9876 ops[0] = gen_rtx_REG (SImode, dest_start + i);
9877 ops[1] = gen_rtx_REG (SImode, src_start + i);
9997d19d 9878 output_asm_insn ("mov%?\t%0, %1", ops);
ff9940b0
RE
9879 }
9880 }
f3bb6135 9881
ff9940b0
RE
9882 return "";
9883}
9884
9885
3b684012
RE
9886/* Output a move from arm registers to an fpa registers.
9887 OPERANDS[0] is an fpa register.
cce8749e 9888 OPERANDS[1] is the first registers of an arm register pair. */
cd2b33d0 9889const char *
e32bac5b 9890output_mov_double_fpa_from_arm (rtx *operands)
cce8749e
CH
9891{
9892 int arm_reg0 = REGNO (operands[1]);
9893 rtx ops[2];
9894
e6d29d15 9895 gcc_assert (arm_reg0 != IP_REGNUM);
f676971a 9896
43cffd11
RE
9897 ops[0] = gen_rtx_REG (SImode, arm_reg0);
9898 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
5b3e6663 9899 output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1}", ops);
1d5473cb 9900 output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
f3bb6135
RE
9901 return "";
9902}
cce8749e 9903
3b684012 9904/* Output a move from an fpa register to arm registers.
cce8749e 9905 OPERANDS[0] is the first registers of an arm register pair.
3b684012 9906 OPERANDS[1] is an fpa register. */
cd2b33d0 9907const char *
e32bac5b 9908output_mov_double_arm_from_fpa (rtx *operands)
cce8749e
CH
9909{
9910 int arm_reg0 = REGNO (operands[0]);
9911 rtx ops[2];
9912
e6d29d15 9913 gcc_assert (arm_reg0 != IP_REGNUM);
f3bb6135 9914
43cffd11
RE
9915 ops[0] = gen_rtx_REG (SImode, arm_reg0);
9916 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
1d5473cb 9917 output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
5b3e6663 9918 output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1}", ops);
f3bb6135
RE
9919 return "";
9920}
cce8749e
CH
9921
9922/* Output a move between double words.
9923 It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
9924 or MEM<-REG and all MEMs must be offsettable addresses. */
cd2b33d0 9925const char *
e32bac5b 9926output_move_double (rtx *operands)
cce8749e
CH
9927{
9928 enum rtx_code code0 = GET_CODE (operands[0]);
9929 enum rtx_code code1 = GET_CODE (operands[1]);
56636818 9930 rtx otherops[3];
cce8749e
CH
9931
9932 if (code0 == REG)
9933 {
9934 int reg0 = REGNO (operands[0]);
9935
43cffd11 9936 otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
f676971a 9937
e6d29d15
NS
9938 gcc_assert (code1 == MEM); /* Constraints should ensure this. */
9939
9940 switch (GET_CODE (XEXP (operands[1], 0)))
cce8749e 9941 {
e6d29d15 9942 case REG:
5dea0c19
PB
9943 if (TARGET_LDRD)
9944 output_asm_insn ("ldr%(d%)\t%0, [%m1]", operands);
9945 else
9946 output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
e6d29d15 9947 break;
e0b92319 9948
e6d29d15
NS
9949 case PRE_INC:
9950 gcc_assert (TARGET_LDRD);
5b3e6663 9951 output_asm_insn ("ldr%(d%)\t%0, [%m1, #8]!", operands);
e6d29d15 9952 break;
e0b92319 9953
e6d29d15 9954 case PRE_DEC:
5b3e6663
PB
9955 if (TARGET_LDRD)
9956 output_asm_insn ("ldr%(d%)\t%0, [%m1, #-8]!", operands);
9957 else
9958 output_asm_insn ("ldm%(db%)\t%m1!, %M0", operands);
e6d29d15 9959 break;
e0b92319 9960
e6d29d15 9961 case POST_INC:
5dea0c19
PB
9962 if (TARGET_LDRD)
9963 output_asm_insn ("ldr%(d%)\t%0, [%m1], #8", operands);
9964 else
9965 output_asm_insn ("ldm%(ia%)\t%m1!, %M0", operands);
e6d29d15 9966 break;
e0b92319 9967
e6d29d15
NS
9968 case POST_DEC:
9969 gcc_assert (TARGET_LDRD);
5b3e6663 9970 output_asm_insn ("ldr%(d%)\t%0, [%m1], #-8", operands);
e6d29d15 9971 break;
e0b92319 9972
e6d29d15
NS
9973 case PRE_MODIFY:
9974 case POST_MODIFY:
9975 otherops[0] = operands[0];
9976 otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
9977 otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
e0b92319 9978
e6d29d15 9979 if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
cce8749e 9980 {
e6d29d15 9981 if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
fdd695fd 9982 {
e6d29d15
NS
9983 /* Registers overlap so split out the increment. */
9984 output_asm_insn ("add%?\t%1, %1, %2", otherops);
5b3e6663 9985 output_asm_insn ("ldr%(d%)\t%0, [%1] @split", otherops);
fdd695fd
PB
9986 }
9987 else
fe2d934b
PB
9988 {
9989 /* IWMMXT allows offsets larger than ldrd can handle,
9990 fix these up with a pair of ldr. */
9991 if (GET_CODE (otherops[2]) == CONST_INT
9992 && (INTVAL(otherops[2]) <= -256
9993 || INTVAL(otherops[2]) >= 256))
9994 {
9995 output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
9996 otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
9997 output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
9998 }
9999 else
10000 output_asm_insn ("ldr%(d%)\t%0, [%1, %2]!", otherops);
10001 }
e6d29d15
NS
10002 }
10003 else
10004 {
fe2d934b
PB
10005 /* IWMMXT allows offsets larger than ldrd can handle,
10006 fix these up with a pair of ldr. */
10007 if (GET_CODE (otherops[2]) == CONST_INT
10008 && (INTVAL(otherops[2]) <= -256
10009 || INTVAL(otherops[2]) >= 256))
10010 {
10011 otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
10012 output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
10013 otherops[0] = operands[0];
10014 output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
10015 }
10016 else
10017 /* We only allow constant increments, so this is safe. */
10018 output_asm_insn ("ldr%(d%)\t%0, [%1], %2", otherops);
e6d29d15
NS
10019 }
10020 break;
e0b92319 10021
e6d29d15
NS
10022 case LABEL_REF:
10023 case CONST:
5dea0c19
PB
10024 /* We might be able to use ldrd %0, %1 here. However the range is
10025 different to ldr/adr, and it is broken on some ARMv7-M
10026 implementations. */
e6d29d15 10027 output_asm_insn ("adr%?\t%0, %1", operands);
5dea0c19
PB
10028 if (TARGET_LDRD)
10029 output_asm_insn ("ldr%(d%)\t%0, [%0]", operands);
10030 else
10031 output_asm_insn ("ldm%(ia%)\t%0, %M0", operands);
e6d29d15 10032 break;
e0b92319 10033
5b3e6663 10034 /* ??? This needs checking for thumb2. */
e6d29d15
NS
10035 default:
10036 if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
10037 GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
10038 {
10039 otherops[0] = operands[0];
10040 otherops[1] = XEXP (XEXP (operands[1], 0), 0);
10041 otherops[2] = XEXP (XEXP (operands[1], 0), 1);
e0b92319 10042
e6d29d15 10043 if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
fdd695fd 10044 {
5dea0c19 10045 if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
2b835d68 10046 {
e6d29d15 10047 switch ((int) INTVAL (otherops[2]))
2b835d68 10048 {
e6d29d15 10049 case -8:
5b3e6663 10050 output_asm_insn ("ldm%(db%)\t%1, %M0", otherops);
e6d29d15
NS
10051 return "";
10052 case -4:
5b3e6663
PB
10053 if (TARGET_THUMB2)
10054 break;
10055 output_asm_insn ("ldm%(da%)\t%1, %M0", otherops);
e6d29d15
NS
10056 return "";
10057 case 4:
5b3e6663
PB
10058 if (TARGET_THUMB2)
10059 break;
10060 output_asm_insn ("ldm%(ib%)\t%1, %M0", otherops);
e6d29d15 10061 return "";
fdd695fd 10062 }
e6d29d15
NS
10063 }
10064 if (TARGET_LDRD
10065 && (GET_CODE (otherops[2]) == REG
10066 || (GET_CODE (otherops[2]) == CONST_INT
10067 && INTVAL (otherops[2]) > -256
10068 && INTVAL (otherops[2]) < 256)))
10069 {
10070 if (reg_overlap_mentioned_p (otherops[0],
10071 otherops[2]))
fdd695fd 10072 {
e6d29d15
NS
10073 /* Swap base and index registers over to
10074 avoid a conflict. */
10075 otherops[1] = XEXP (XEXP (operands[1], 0), 1);
10076 otherops[2] = XEXP (XEXP (operands[1], 0), 0);
fdd695fd 10077 }
e6d29d15
NS
10078 /* If both registers conflict, it will usually
10079 have been fixed by a splitter. */
10080 if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
fdd695fd 10081 {
e6d29d15 10082 output_asm_insn ("add%?\t%1, %1, %2", otherops);
5b3e6663 10083 output_asm_insn ("ldr%(d%)\t%0, [%1]",
e6d29d15 10084 otherops);
2b835d68
RE
10085 }
10086 else
5b3e6663 10087 output_asm_insn ("ldr%(d%)\t%0, [%1, %2]", otherops);
e6d29d15 10088 return "";
2b835d68 10089 }
e0b92319 10090
e6d29d15 10091 if (GET_CODE (otherops[2]) == CONST_INT)
2b835d68 10092 {
e6d29d15
NS
10093 if (!(const_ok_for_arm (INTVAL (otherops[2]))))
10094 output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
10095 else
10096 output_asm_insn ("add%?\t%0, %1, %2", otherops);
2b835d68
RE
10097 }
10098 else
e6d29d15
NS
10099 output_asm_insn ("add%?\t%0, %1, %2", otherops);
10100 }
10101 else
10102 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
10103
5dea0c19
PB
10104 if (TARGET_LDRD)
10105 return "ldr%(d%)\t%0, [%0]";
10106
5b3e6663 10107 return "ldm%(ia%)\t%0, %M0";
e6d29d15
NS
10108 }
10109 else
10110 {
10111 otherops[1] = adjust_address (operands[1], SImode, 4);
10112 /* Take care of overlapping base/data reg. */
10113 if (reg_mentioned_p (operands[0], operands[1]))
10114 {
10115 output_asm_insn ("ldr%?\t%0, %1", otherops);
10116 output_asm_insn ("ldr%?\t%0, %1", operands);
10117 }
10118 else
10119 {
10120 output_asm_insn ("ldr%?\t%0, %1", operands);
10121 output_asm_insn ("ldr%?\t%0, %1", otherops);
cce8749e
CH
10122 }
10123 }
10124 }
cce8749e 10125 }
e6d29d15 10126 else
cce8749e 10127 {
e6d29d15
NS
10128 /* Constraints should ensure this. */
10129 gcc_assert (code0 == MEM && code1 == REG);
10130 gcc_assert (REGNO (operands[1]) != IP_REGNUM);
2b835d68 10131
ff9940b0
RE
10132 switch (GET_CODE (XEXP (operands[0], 0)))
10133 {
10134 case REG:
5dea0c19
PB
10135 if (TARGET_LDRD)
10136 output_asm_insn ("str%(d%)\t%1, [%m0]", operands);
10137 else
10138 output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
ff9940b0 10139 break;
2b835d68 10140
ff9940b0 10141 case PRE_INC:
e6d29d15 10142 gcc_assert (TARGET_LDRD);
5b3e6663 10143 output_asm_insn ("str%(d%)\t%1, [%m0, #8]!", operands);
ff9940b0 10144 break;
2b835d68 10145
ff9940b0 10146 case PRE_DEC:
5b3e6663
PB
10147 if (TARGET_LDRD)
10148 output_asm_insn ("str%(d%)\t%1, [%m0, #-8]!", operands);
10149 else
10150 output_asm_insn ("stm%(db%)\t%m0!, %M1", operands);
ff9940b0 10151 break;
2b835d68 10152
ff9940b0 10153 case POST_INC:
5dea0c19
PB
10154 if (TARGET_LDRD)
10155 output_asm_insn ("str%(d%)\t%1, [%m0], #8", operands);
10156 else
10157 output_asm_insn ("stm%(ia%)\t%m0!, %M1", operands);
ff9940b0 10158 break;
2b835d68 10159
ff9940b0 10160 case POST_DEC:
e6d29d15 10161 gcc_assert (TARGET_LDRD);
5b3e6663 10162 output_asm_insn ("str%(d%)\t%1, [%m0], #-8", operands);
fdd695fd
PB
10163 break;
10164
10165 case PRE_MODIFY:
10166 case POST_MODIFY:
10167 otherops[0] = operands[1];
10168 otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
10169 otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
10170
fe2d934b
PB
10171 /* IWMMXT allows offsets larger than ldrd can handle,
10172 fix these up with a pair of ldr. */
10173 if (GET_CODE (otherops[2]) == CONST_INT
10174 && (INTVAL(otherops[2]) <= -256
10175 || INTVAL(otherops[2]) >= 256))
10176 {
10177 rtx reg1;
10178 reg1 = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
10179 if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
10180 {
10181 output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
10182 otherops[0] = reg1;
10183 output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
10184 }
10185 else
10186 {
10187 otherops[0] = reg1;
10188 output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
10189 otherops[0] = operands[1];
10190 output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
10191 }
10192 }
10193 else if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
5b3e6663 10194 output_asm_insn ("str%(d%)\t%0, [%1, %2]!", otherops);
fdd695fd 10195 else
5b3e6663 10196 output_asm_insn ("str%(d%)\t%0, [%1], %2", otherops);
ff9940b0 10197 break;
2b835d68
RE
10198
10199 case PLUS:
fdd695fd 10200 otherops[2] = XEXP (XEXP (operands[0], 0), 1);
5dea0c19 10201 if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
2b835d68 10202 {
06bea5aa 10203 switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
2b835d68
RE
10204 {
10205 case -8:
5b3e6663 10206 output_asm_insn ("stm%(db%)\t%m0, %M1", operands);
2b835d68
RE
10207 return "";
10208
10209 case -4:
5b3e6663
PB
10210 if (TARGET_THUMB2)
10211 break;
10212 output_asm_insn ("stm%(da%)\t%m0, %M1", operands);
2b835d68
RE
10213 return "";
10214
10215 case 4:
5b3e6663
PB
10216 if (TARGET_THUMB2)
10217 break;
10218 output_asm_insn ("stm%(ib%)\t%m0, %M1", operands);
2b835d68
RE
10219 return "";
10220 }
10221 }
fdd695fd
PB
10222 if (TARGET_LDRD
10223 && (GET_CODE (otherops[2]) == REG
10224 || (GET_CODE (otherops[2]) == CONST_INT
10225 && INTVAL (otherops[2]) > -256
10226 && INTVAL (otherops[2]) < 256)))
10227 {
10228 otherops[0] = operands[1];
10229 otherops[1] = XEXP (XEXP (operands[0], 0), 0);
5b3e6663 10230 output_asm_insn ("str%(d%)\t%0, [%1, %2]", otherops);
fdd695fd
PB
10231 return "";
10232 }
2b835d68
RE
10233 /* Fall through */
10234
ff9940b0 10235 default:
a4a37b30 10236 otherops[0] = adjust_address (operands[0], SImode, 4);
43cffd11 10237 otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
9997d19d
RE
10238 output_asm_insn ("str%?\t%1, %0", operands);
10239 output_asm_insn ("str%?\t%1, %0", otherops);
cce8749e
CH
10240 }
10241 }
cce8749e 10242
9997d19d
RE
10243 return "";
10244}
cce8749e 10245
88f77cba
JB
10246/* Output a move, load or store for quad-word vectors in ARM registers. Only
10247 handles MEMs accepted by neon_vector_mem_operand with CORE=true. */
5b3e6663
PB
10248
10249const char *
88f77cba 10250output_move_quad (rtx *operands)
5b3e6663 10251{
88f77cba
JB
10252 if (REG_P (operands[0]))
10253 {
10254 /* Load, or reg->reg move. */
5b3e6663 10255
88f77cba
JB
10256 if (MEM_P (operands[1]))
10257 {
10258 switch (GET_CODE (XEXP (operands[1], 0)))
10259 {
10260 case REG:
10261 output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
10262 break;
10263
10264 case LABEL_REF:
10265 case CONST:
10266 output_asm_insn ("adr%?\t%0, %1", operands);
10267 output_asm_insn ("ldm%(ia%)\t%0, %M0", operands);
10268 break;
10269
10270 default:
10271 gcc_unreachable ();
10272 }
10273 }
10274 else
10275 {
10276 rtx ops[2];
10277 int dest, src, i;
5b3e6663 10278
88f77cba 10279 gcc_assert (REG_P (operands[1]));
5b3e6663 10280
88f77cba
JB
10281 dest = REGNO (operands[0]);
10282 src = REGNO (operands[1]);
5b3e6663 10283
88f77cba
JB
10284 /* This seems pretty dumb, but hopefully GCC won't try to do it
10285 very often. */
10286 if (dest < src)
10287 for (i = 0; i < 4; i++)
10288 {
10289 ops[0] = gen_rtx_REG (SImode, dest + i);
10290 ops[1] = gen_rtx_REG (SImode, src + i);
10291 output_asm_insn ("mov%?\t%0, %1", ops);
10292 }
10293 else
10294 for (i = 3; i >= 0; i--)
10295 {
10296 ops[0] = gen_rtx_REG (SImode, dest + i);
10297 ops[1] = gen_rtx_REG (SImode, src + i);
10298 output_asm_insn ("mov%?\t%0, %1", ops);
10299 }
10300 }
10301 }
10302 else
10303 {
10304 gcc_assert (MEM_P (operands[0]));
10305 gcc_assert (REG_P (operands[1]));
10306 gcc_assert (!reg_overlap_mentioned_p (operands[1], operands[0]));
10307
10308 switch (GET_CODE (XEXP (operands[0], 0)))
10309 {
10310 case REG:
10311 output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
10312 break;
10313
10314 default:
10315 gcc_unreachable ();
10316 }
10317 }
10318
10319 return "";
10320}
10321
10322/* Output a VFP load or store instruction. */
10323
10324const char *
10325output_move_vfp (rtx *operands)
10326{
10327 rtx reg, mem, addr, ops[2];
10328 int load = REG_P (operands[0]);
10329 int dp = GET_MODE_SIZE (GET_MODE (operands[0])) == 8;
10330 int integer_p = GET_MODE_CLASS (GET_MODE (operands[0])) == MODE_INT;
0a2aaacc 10331 const char *templ;
88f77cba
JB
10332 char buff[50];
10333 enum machine_mode mode;
10334
10335 reg = operands[!load];
10336 mem = operands[load];
10337
10338 mode = GET_MODE (reg);
10339
10340 gcc_assert (REG_P (reg));
10341 gcc_assert (IS_VFP_REGNUM (REGNO (reg)));
10342 gcc_assert (mode == SFmode
10343 || mode == DFmode
10344 || mode == SImode
10345 || mode == DImode
10346 || (TARGET_NEON && VALID_NEON_DREG_MODE (mode)));
10347 gcc_assert (MEM_P (mem));
10348
10349 addr = XEXP (mem, 0);
10350
10351 switch (GET_CODE (addr))
10352 {
10353 case PRE_DEC:
0a2aaacc 10354 templ = "f%smdb%c%%?\t%%0!, {%%%s1}%s";
88f77cba
JB
10355 ops[0] = XEXP (addr, 0);
10356 ops[1] = reg;
5b3e6663
PB
10357 break;
10358
10359 case POST_INC:
0a2aaacc 10360 templ = "f%smia%c%%?\t%%0!, {%%%s1}%s";
5b3e6663
PB
10361 ops[0] = XEXP (addr, 0);
10362 ops[1] = reg;
10363 break;
10364
10365 default:
0a2aaacc 10366 templ = "f%s%c%%?\t%%%s0, %%1%s";
5b3e6663
PB
10367 ops[0] = reg;
10368 ops[1] = mem;
10369 break;
10370 }
10371
0a2aaacc 10372 sprintf (buff, templ,
5b3e6663
PB
10373 load ? "ld" : "st",
10374 dp ? 'd' : 's',
10375 dp ? "P" : "",
10376 integer_p ? "\t%@ int" : "");
10377 output_asm_insn (buff, ops);
10378
10379 return "";
10380}
10381
88f77cba 10382/* Output a Neon quad-word load or store, or a load or store for
874d42b9 10383 larger structure modes.
88f77cba 10384
874d42b9
JM
10385 WARNING: The ordering of elements is weird in big-endian mode,
10386 because we use VSTM, as required by the EABI. GCC RTL defines
10387 element ordering based on in-memory order. This can be differ
10388 from the architectural ordering of elements within a NEON register.
10389 The intrinsics defined in arm_neon.h use the NEON register element
10390 ordering, not the GCC RTL element ordering.
88f77cba 10391
874d42b9
JM
10392 For example, the in-memory ordering of a big-endian a quadword
10393 vector with 16-bit elements when stored from register pair {d0,d1}
10394 will be (lowest address first, d0[N] is NEON register element N):
88f77cba 10395
874d42b9 10396 [d0[3], d0[2], d0[1], d0[0], d1[7], d1[6], d1[5], d1[4]]
88f77cba 10397
874d42b9
JM
10398 When necessary, quadword registers (dN, dN+1) are moved to ARM
10399 registers from rN in the order:
88f77cba
JB
10400
10401 dN -> (rN+1, rN), dN+1 -> (rN+3, rN+2)
10402
874d42b9
JM
10403 So that STM/LDM can be used on vectors in ARM registers, and the
10404 same memory layout will result as if VSTM/VLDM were used. */
88f77cba
JB
10405
10406const char *
10407output_move_neon (rtx *operands)
10408{
10409 rtx reg, mem, addr, ops[2];
10410 int regno, load = REG_P (operands[0]);
0a2aaacc 10411 const char *templ;
88f77cba
JB
10412 char buff[50];
10413 enum machine_mode mode;
10414
10415 reg = operands[!load];
10416 mem = operands[load];
10417
10418 mode = GET_MODE (reg);
10419
10420 gcc_assert (REG_P (reg));
10421 regno = REGNO (reg);
10422 gcc_assert (VFP_REGNO_OK_FOR_DOUBLE (regno)
10423 || NEON_REGNO_OK_FOR_QUAD (regno));
10424 gcc_assert (VALID_NEON_DREG_MODE (mode)
10425 || VALID_NEON_QREG_MODE (mode)
10426 || VALID_NEON_STRUCT_MODE (mode));
10427 gcc_assert (MEM_P (mem));
10428
10429 addr = XEXP (mem, 0);
10430
10431 /* Strip off const from addresses like (const (plus (...))). */
10432 if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
10433 addr = XEXP (addr, 0);
10434
10435 switch (GET_CODE (addr))
10436 {
10437 case POST_INC:
0a2aaacc 10438 templ = "v%smia%%?\t%%0!, %%h1";
88f77cba
JB
10439 ops[0] = XEXP (addr, 0);
10440 ops[1] = reg;
10441 break;
10442
10443 case POST_MODIFY:
10444 /* FIXME: Not currently enabled in neon_vector_mem_operand. */
10445 gcc_unreachable ();
10446
10447 case LABEL_REF:
10448 case PLUS:
10449 {
10450 int nregs = HARD_REGNO_NREGS (REGNO (reg), mode) / 2;
10451 int i;
10452 int overlap = -1;
10453 for (i = 0; i < nregs; i++)
10454 {
10455 /* We're only using DImode here because it's a convenient size. */
10456 ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * i);
10457 ops[1] = adjust_address (mem, SImode, 8 * i);
10458 if (reg_overlap_mentioned_p (ops[0], mem))
10459 {
10460 gcc_assert (overlap == -1);
10461 overlap = i;
10462 }
10463 else
10464 {
10465 sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
10466 output_asm_insn (buff, ops);
10467 }
10468 }
10469 if (overlap != -1)
10470 {
10471 ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * overlap);
10472 ops[1] = adjust_address (mem, SImode, 8 * overlap);
10473 sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
10474 output_asm_insn (buff, ops);
10475 }
10476
10477 return "";
10478 }
10479
10480 default:
0a2aaacc 10481 templ = "v%smia%%?\t%%m0, %%h1";
88f77cba
JB
10482 ops[0] = mem;
10483 ops[1] = reg;
10484 }
10485
0a2aaacc 10486 sprintf (buff, templ, load ? "ld" : "st");
88f77cba
JB
10487 output_asm_insn (buff, ops);
10488
10489 return "";
10490}
10491
1d6e90ac
NC
10492/* Output an ADD r, s, #n where n may be too big for one instruction.
10493 If adding zero to one register, output nothing. */
cd2b33d0 10494const char *
e32bac5b 10495output_add_immediate (rtx *operands)
cce8749e 10496{
f3bb6135 10497 HOST_WIDE_INT n = INTVAL (operands[2]);
cce8749e
CH
10498
10499 if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
10500 {
10501 if (n < 0)
10502 output_multi_immediate (operands,
9997d19d
RE
10503 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
10504 -n);
cce8749e
CH
10505 else
10506 output_multi_immediate (operands,
9997d19d
RE
10507 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
10508 n);
cce8749e 10509 }
f3bb6135
RE
10510
10511 return "";
10512}
cce8749e 10513
cce8749e
CH
10514/* Output a multiple immediate operation.
10515 OPERANDS is the vector of operands referred to in the output patterns.
10516 INSTR1 is the output pattern to use for the first constant.
10517 INSTR2 is the output pattern to use for subsequent constants.
10518 IMMED_OP is the index of the constant slot in OPERANDS.
10519 N is the constant value. */
cd2b33d0 10520static const char *
e32bac5b
RE
10521output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
10522 int immed_op, HOST_WIDE_INT n)
cce8749e 10523{
f3bb6135 10524#if HOST_BITS_PER_WIDE_INT > 32
30cf4896 10525 n &= 0xffffffff;
f3bb6135
RE
10526#endif
10527
cce8749e
CH
10528 if (n == 0)
10529 {
1d6e90ac 10530 /* Quick and easy output. */
cce8749e 10531 operands[immed_op] = const0_rtx;
1d6e90ac 10532 output_asm_insn (instr1, operands);
cce8749e
CH
10533 }
10534 else
10535 {
10536 int i;
cd2b33d0 10537 const char * instr = instr1;
cce8749e 10538
6354dc9b 10539 /* Note that n is never zero here (which would give no output). */
cce8749e
CH
10540 for (i = 0; i < 32; i += 2)
10541 {
10542 if (n & (3 << i))
10543 {
f3bb6135
RE
10544 operands[immed_op] = GEN_INT (n & (255 << i));
10545 output_asm_insn (instr, operands);
cce8749e
CH
10546 instr = instr2;
10547 i += 6;
10548 }
10549 }
10550 }
f676971a 10551
f3bb6135 10552 return "";
9997d19d 10553}
cce8749e 10554
5b3e6663
PB
10555/* Return the name of a shifter operation. */
10556static const char *
10557arm_shift_nmem(enum rtx_code code)
10558{
10559 switch (code)
10560 {
10561 case ASHIFT:
10562 return ARM_LSL_NAME;
10563
10564 case ASHIFTRT:
10565 return "asr";
10566
10567 case LSHIFTRT:
10568 return "lsr";
10569
10570 case ROTATERT:
10571 return "ror";
10572
10573 default:
10574 abort();
10575 }
10576}
10577
cce8749e
CH
10578/* Return the appropriate ARM instruction for the operation code.
10579 The returned result should not be overwritten. OP is the rtx of the
10580 operation. SHIFT_FIRST_ARG is TRUE if the first argument of the operator
10581 was shifted. */
cd2b33d0 10582const char *
e32bac5b 10583arithmetic_instr (rtx op, int shift_first_arg)
cce8749e 10584{
9997d19d 10585 switch (GET_CODE (op))
cce8749e
CH
10586 {
10587 case PLUS:
f3bb6135
RE
10588 return "add";
10589
cce8749e 10590 case MINUS:
f3bb6135
RE
10591 return shift_first_arg ? "rsb" : "sub";
10592
cce8749e 10593 case IOR:
f3bb6135
RE
10594 return "orr";
10595
cce8749e 10596 case XOR:
f3bb6135
RE
10597 return "eor";
10598
cce8749e 10599 case AND:
f3bb6135
RE
10600 return "and";
10601
5b3e6663
PB
10602 case ASHIFT:
10603 case ASHIFTRT:
10604 case LSHIFTRT:
10605 case ROTATERT:
10606 return arm_shift_nmem(GET_CODE(op));
10607
cce8749e 10608 default:
e6d29d15 10609 gcc_unreachable ();
cce8749e 10610 }
f3bb6135 10611}
cce8749e 10612
cce8749e
CH
10613/* Ensure valid constant shifts and return the appropriate shift mnemonic
10614 for the operation code. The returned result should not be overwritten.
10615 OP is the rtx code of the shift.
9997d19d 10616 On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
6354dc9b 10617 shift. */
cd2b33d0 10618static const char *
e32bac5b 10619shift_op (rtx op, HOST_WIDE_INT *amountp)
cce8749e 10620{
cd2b33d0 10621 const char * mnem;
e2c671ba 10622 enum rtx_code code = GET_CODE (op);
cce8749e 10623
e6d29d15
NS
10624 switch (GET_CODE (XEXP (op, 1)))
10625 {
10626 case REG:
10627 case SUBREG:
10628 *amountp = -1;
10629 break;
10630
10631 case CONST_INT:
10632 *amountp = INTVAL (XEXP (op, 1));
10633 break;
10634
10635 default:
10636 gcc_unreachable ();
10637 }
9997d19d 10638
e2c671ba 10639 switch (code)
cce8749e 10640 {
a657c98a 10641 case ROTATE:
e6d29d15 10642 gcc_assert (*amountp != -1);
a657c98a 10643 *amountp = 32 - *amountp;
5b3e6663 10644 code = ROTATERT;
a657c98a
RE
10645
10646 /* Fall through. */
10647
5b3e6663
PB
10648 case ASHIFT:
10649 case ASHIFTRT:
10650 case LSHIFTRT:
9997d19d 10651 case ROTATERT:
5b3e6663 10652 mnem = arm_shift_nmem(code);
9997d19d
RE
10653 break;
10654
ff9940b0 10655 case MULT:
e2c671ba
RE
10656 /* We never have to worry about the amount being other than a
10657 power of 2, since this case can never be reloaded from a reg. */
e6d29d15
NS
10658 gcc_assert (*amountp != -1);
10659 *amountp = int_log2 (*amountp);
5b3e6663 10660 return ARM_LSL_NAME;
f3bb6135 10661
cce8749e 10662 default:
e6d29d15 10663 gcc_unreachable ();
cce8749e
CH
10664 }
10665
e2c671ba
RE
10666 if (*amountp != -1)
10667 {
10668 /* This is not 100% correct, but follows from the desire to merge
10669 multiplication by a power of 2 with the recognizer for a
5b3e6663 10670 shift. >=32 is not a valid shift for "lsl", so we must try and
e2c671ba 10671 output a shift that produces the correct arithmetical result.
ddd5a7c1 10672 Using lsr #32 is identical except for the fact that the carry bit
f676971a 10673 is not set correctly if we set the flags; but we never use the
e2c671ba
RE
10674 carry bit from such an operation, so we can ignore that. */
10675 if (code == ROTATERT)
1d6e90ac
NC
10676 /* Rotate is just modulo 32. */
10677 *amountp &= 31;
e2c671ba
RE
10678 else if (*amountp != (*amountp & 31))
10679 {
10680 if (code == ASHIFT)
10681 mnem = "lsr";
10682 *amountp = 32;
10683 }
10684
10685 /* Shifts of 0 are no-ops. */
10686 if (*amountp == 0)
10687 return NULL;
f676971a 10688 }
e2c671ba 10689
9997d19d
RE
10690 return mnem;
10691}
cce8749e 10692
6354dc9b 10693/* Obtain the shift from the POWER of two. */
1d6e90ac 10694
18af7313 10695static HOST_WIDE_INT
e32bac5b 10696int_log2 (HOST_WIDE_INT power)
cce8749e 10697{
f3bb6135 10698 HOST_WIDE_INT shift = 0;
cce8749e 10699
30cf4896 10700 while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
cce8749e 10701 {
e6d29d15 10702 gcc_assert (shift <= 31);
e32bac5b 10703 shift++;
cce8749e 10704 }
f3bb6135
RE
10705
10706 return shift;
10707}
cce8749e 10708
c5ff069d
ZW
10709/* Output a .ascii pseudo-op, keeping track of lengths. This is
10710 because /bin/as is horribly restrictive. The judgement about
10711 whether or not each character is 'printable' (and can be output as
10712 is) or not (and must be printed with an octal escape) must be made
10713 with reference to the *host* character set -- the situation is
10714 similar to that discussed in the comments above pp_c_char in
10715 c-pretty-print.c. */
10716
6cfc7210 10717#define MAX_ASCII_LEN 51
cce8749e
CH
10718
10719void
e32bac5b 10720output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
cce8749e
CH
10721{
10722 int i;
6cfc7210 10723 int len_so_far = 0;
cce8749e 10724
6cfc7210 10725 fputs ("\t.ascii\t\"", stream);
f676971a 10726
cce8749e
CH
10727 for (i = 0; i < len; i++)
10728 {
1d6e90ac 10729 int c = p[i];
cce8749e 10730
6cfc7210 10731 if (len_so_far >= MAX_ASCII_LEN)
cce8749e 10732 {
6cfc7210 10733 fputs ("\"\n\t.ascii\t\"", stream);
cce8749e 10734 len_so_far = 0;
cce8749e
CH
10735 }
10736
c5ff069d 10737 if (ISPRINT (c))
cce8749e 10738 {
c5ff069d 10739 if (c == '\\' || c == '\"')
6cfc7210 10740 {
c5ff069d 10741 putc ('\\', stream);
5895f793 10742 len_so_far++;
6cfc7210 10743 }
c5ff069d
ZW
10744 putc (c, stream);
10745 len_so_far++;
10746 }
10747 else
10748 {
10749 fprintf (stream, "\\%03o", c);
10750 len_so_far += 4;
cce8749e 10751 }
cce8749e 10752 }
f3bb6135 10753
cce8749e 10754 fputs ("\"\n", stream);
f3bb6135 10755}
cce8749e 10756\f
c9ca9b88 10757/* Compute the register save mask for registers 0 through 12
5848830f 10758 inclusive. This code is used by arm_compute_save_reg_mask. */
b279b20a 10759
6d3d9133 10760static unsigned long
e32bac5b 10761arm_compute_save_reg0_reg12_mask (void)
6d3d9133 10762{
121308d4 10763 unsigned long func_type = arm_current_func_type ();
b279b20a 10764 unsigned long save_reg_mask = 0;
6d3d9133 10765 unsigned int reg;
6d3d9133 10766
7b8b8ade 10767 if (IS_INTERRUPT (func_type))
6d3d9133 10768 {
7b8b8ade 10769 unsigned int max_reg;
7b8b8ade
NC
10770 /* Interrupt functions must not corrupt any registers,
10771 even call clobbered ones. If this is a leaf function
10772 we can just examine the registers used by the RTL, but
10773 otherwise we have to assume that whatever function is
10774 called might clobber anything, and so we have to save
10775 all the call-clobbered registers as well. */
10776 if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
10777 /* FIQ handlers have registers r8 - r12 banked, so
10778 we only need to check r0 - r7, Normal ISRs only
121308d4 10779 bank r14 and r15, so we must check up to r12.
7b8b8ade
NC
10780 r13 is the stack pointer which is always preserved,
10781 so we do not need to consider it here. */
10782 max_reg = 7;
10783 else
10784 max_reg = 12;
f676971a 10785
7b8b8ade 10786 for (reg = 0; reg <= max_reg; reg++)
6fb5fa3c
DB
10787 if (df_regs_ever_live_p (reg)
10788 || (! current_function_is_leaf && call_used_regs[reg]))
6d3d9133 10789 save_reg_mask |= (1 << reg);
cfa01aab 10790
286d28c3 10791 /* Also save the pic base register if necessary. */
cfa01aab
PB
10792 if (flag_pic
10793 && !TARGET_SINGLE_PIC_BASE
020a4035 10794 && arm_pic_register != INVALID_REGNUM
e3b5732b 10795 && crtl->uses_pic_offset_table)
cfa01aab 10796 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
6d3d9133
NC
10797 }
10798 else
10799 {
10800 /* In the normal case we only need to save those registers
10801 which are call saved and which are used by this function. */
ec6237e4 10802 for (reg = 0; reg <= 11; reg++)
6fb5fa3c 10803 if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
6d3d9133
NC
10804 save_reg_mask |= (1 << reg);
10805
10806 /* Handle the frame pointer as a special case. */
ec6237e4 10807 if (frame_pointer_needed)
6d3d9133
NC
10808 save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
10809
10810 /* If we aren't loading the PIC register,
10811 don't stack it even though it may be live. */
10812 if (flag_pic
e0b92319 10813 && !TARGET_SINGLE_PIC_BASE
020a4035 10814 && arm_pic_register != INVALID_REGNUM
6fb5fa3c 10815 && (df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM)
e3b5732b 10816 || crtl->uses_pic_offset_table))
6d3d9133 10817 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
5b3e6663
PB
10818
10819 /* The prologue will copy SP into R0, so save it. */
10820 if (IS_STACKALIGN (func_type))
10821 save_reg_mask |= 1;
6d3d9133
NC
10822 }
10823
c9ca9b88 10824 /* Save registers so the exception handler can modify them. */
e3b5732b 10825 if (crtl->calls_eh_return)
c9ca9b88
PB
10826 {
10827 unsigned int i;
f676971a 10828
c9ca9b88
PB
10829 for (i = 0; ; i++)
10830 {
10831 reg = EH_RETURN_DATA_REGNO (i);
10832 if (reg == INVALID_REGNUM)
10833 break;
10834 save_reg_mask |= 1 << reg;
10835 }
10836 }
10837
121308d4
NC
10838 return save_reg_mask;
10839}
10840
5b3e6663 10841
35596784
AJ
10842/* Compute the number of bytes used to store the static chain register on the
10843 stack, above the stack frame. We need to know this accurately to get the
10844 alignment of the rest of the stack frame correct. */
10845
10846static int arm_compute_static_chain_stack_bytes (void)
10847{
10848 unsigned long func_type = arm_current_func_type ();
10849 int static_chain_stack_bytes = 0;
10850
10851 if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM &&
10852 IS_NESTED (func_type) &&
10853 df_regs_ever_live_p (3) && crtl->args.pretend_args_size == 0)
10854 static_chain_stack_bytes = 4;
10855
10856 return static_chain_stack_bytes;
10857}
10858
10859
121308d4 10860/* Compute a bit mask of which registers need to be
954954d1
PB
10861 saved on the stack for the current function.
10862 This is used by arm_get_frame_offsets, which may add extra registers. */
121308d4
NC
10863
10864static unsigned long
e32bac5b 10865arm_compute_save_reg_mask (void)
121308d4
NC
10866{
10867 unsigned int save_reg_mask = 0;
10868 unsigned long func_type = arm_current_func_type ();
5b3e6663 10869 unsigned int reg;
121308d4
NC
10870
10871 if (IS_NAKED (func_type))
10872 /* This should never really happen. */
10873 return 0;
10874
10875 /* If we are creating a stack frame, then we must save the frame pointer,
10876 IP (which will hold the old stack pointer), LR and the PC. */
ec6237e4 10877 if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
121308d4
NC
10878 save_reg_mask |=
10879 (1 << ARM_HARD_FRAME_POINTER_REGNUM)
10880 | (1 << IP_REGNUM)
10881 | (1 << LR_REGNUM)
10882 | (1 << PC_REGNUM);
10883
10884 /* Volatile functions do not return, so there
10885 is no need to save any other registers. */
10886 if (IS_VOLATILE (func_type))
10887 return save_reg_mask;
10888
10889 save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
10890
6d3d9133
NC
10891 /* Decide if we need to save the link register.
10892 Interrupt routines have their own banked link register,
10893 so they never need to save it.
1768c26f 10894 Otherwise if we do not use the link register we do not need to save
6d3d9133
NC
10895 it. If we are pushing other registers onto the stack however, we
10896 can save an instruction in the epilogue by pushing the link register
10897 now and then popping it back into the PC. This incurs extra memory
72ac76be 10898 accesses though, so we only do it when optimizing for size, and only
6d3d9133 10899 if we know that we will not need a fancy return sequence. */
6fb5fa3c
DB
10900 if (df_regs_ever_live_p (LR_REGNUM)
10901 || (save_reg_mask
10902 && optimize_size
10903 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
e3b5732b 10904 && !crtl->calls_eh_return))
6d3d9133
NC
10905 save_reg_mask |= 1 << LR_REGNUM;
10906
6f7ebcbb
NC
10907 if (cfun->machine->lr_save_eliminated)
10908 save_reg_mask &= ~ (1 << LR_REGNUM);
10909
5a9335ef
NC
10910 if (TARGET_REALLY_IWMMXT
10911 && ((bit_count (save_reg_mask)
35596784
AJ
10912 + ARM_NUM_INTS (crtl->args.pretend_args_size +
10913 arm_compute_static_chain_stack_bytes())
10914 ) % 2) != 0)
5a9335ef 10915 {
5a9335ef
NC
10916 /* The total number of registers that are going to be pushed
10917 onto the stack is odd. We need to ensure that the stack
10918 is 64-bit aligned before we start to save iWMMXt registers,
10919 and also before we start to create locals. (A local variable
10920 might be a double or long long which we will load/store using
10921 an iWMMXt instruction). Therefore we need to push another
10922 ARM register, so that the stack will be 64-bit aligned. We
10923 try to avoid using the arg registers (r0 -r3) as they might be
10924 used to pass values in a tail call. */
10925 for (reg = 4; reg <= 12; reg++)
10926 if ((save_reg_mask & (1 << reg)) == 0)
10927 break;
10928
10929 if (reg <= 12)
10930 save_reg_mask |= (1 << reg);
10931 else
10932 {
10933 cfun->machine->sibcall_blocked = 1;
10934 save_reg_mask |= (1 << 3);
10935 }
10936 }
10937
5b3e6663
PB
10938 /* We may need to push an additional register for use initializing the
10939 PIC base register. */
10940 if (TARGET_THUMB2 && IS_NESTED (func_type) && flag_pic
10941 && (save_reg_mask & THUMB2_WORK_REGS) == 0)
10942 {
10943 reg = thumb_find_work_register (1 << 4);
10944 if (!call_used_regs[reg])
10945 save_reg_mask |= (1 << reg);
10946 }
10947
6d3d9133
NC
10948 return save_reg_mask;
10949}
10950
9728c9d1 10951
57934c39
PB
10952/* Compute a bit mask of which registers need to be
10953 saved on the stack for the current function. */
10954static unsigned long
5b3e6663 10955thumb1_compute_save_reg_mask (void)
57934c39
PB
10956{
10957 unsigned long mask;
b279b20a 10958 unsigned reg;
57934c39
PB
10959
10960 mask = 0;
10961 for (reg = 0; reg < 12; reg ++)
6fb5fa3c 10962 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
b279b20a 10963 mask |= 1 << reg;
57934c39 10964
39c39be0
RE
10965 if (flag_pic
10966 && !TARGET_SINGLE_PIC_BASE
020a4035 10967 && arm_pic_register != INVALID_REGNUM
e3b5732b 10968 && crtl->uses_pic_offset_table)
39c39be0 10969 mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
b279b20a 10970
a2503645
RS
10971 /* See if we might need r11 for calls to _interwork_r11_call_via_rN(). */
10972 if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
10973 mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
57934c39 10974
b279b20a 10975 /* LR will also be pushed if any lo regs are pushed. */
57934c39
PB
10976 if (mask & 0xff || thumb_force_lr_save ())
10977 mask |= (1 << LR_REGNUM);
10978
b279b20a
NC
10979 /* Make sure we have a low work register if we need one.
10980 We will need one if we are going to push a high register,
10981 but we are not currently intending to push a low register. */
10982 if ((mask & 0xff) == 0
57934c39 10983 && ((mask & 0x0f00) || TARGET_BACKTRACE))
b279b20a
NC
10984 {
10985 /* Use thumb_find_work_register to choose which register
10986 we will use. If the register is live then we will
10987 have to push it. Use LAST_LO_REGNUM as our fallback
10988 choice for the register to select. */
10989 reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
19e723f4
PB
10990 /* Make sure the register returned by thumb_find_work_register is
10991 not part of the return value. */
954954d1 10992 if (reg * UNITS_PER_WORD <= (unsigned) arm_size_return_regs ())
19e723f4 10993 reg = LAST_LO_REGNUM;
b279b20a
NC
10994
10995 if (! call_used_regs[reg])
10996 mask |= 1 << reg;
10997 }
57934c39 10998
35596784
AJ
10999 /* The 504 below is 8 bytes less than 512 because there are two possible
11000 alignment words. We can't tell here if they will be present or not so we
11001 have to play it safe and assume that they are. */
11002 if ((CALLER_INTERWORKING_SLOT_SIZE +
11003 ROUND_UP_WORD (get_frame_size ()) +
11004 crtl->outgoing_args_size) >= 504)
11005 {
11006 /* This is the same as the code in thumb1_expand_prologue() which
11007 determines which register to use for stack decrement. */
11008 for (reg = LAST_ARG_REGNUM + 1; reg <= LAST_LO_REGNUM; reg++)
11009 if (mask & (1 << reg))
11010 break;
11011
11012 if (reg > LAST_LO_REGNUM)
11013 {
11014 /* Make sure we have a register available for stack decrement. */
11015 mask |= 1 << LAST_LO_REGNUM;
11016 }
11017 }
11018
57934c39
PB
11019 return mask;
11020}
11021
11022
9728c9d1
PB
11023/* Return the number of bytes required to save VFP registers. */
11024static int
11025arm_get_vfp_saved_size (void)
11026{
11027 unsigned int regno;
11028 int count;
11029 int saved;
11030
11031 saved = 0;
11032 /* Space for saved VFP registers. */
11033 if (TARGET_HARD_FLOAT && TARGET_VFP)
11034 {
11035 count = 0;
11036 for (regno = FIRST_VFP_REGNUM;
11037 regno < LAST_VFP_REGNUM;
11038 regno += 2)
11039 {
6fb5fa3c
DB
11040 if ((!df_regs_ever_live_p (regno) || call_used_regs[regno])
11041 && (!df_regs_ever_live_p (regno + 1) || call_used_regs[regno + 1]))
9728c9d1
PB
11042 {
11043 if (count > 0)
11044 {
11045 /* Workaround ARM10 VFPr1 bug. */
11046 if (count == 2 && !arm_arch6)
11047 count++;
8edfc4cc 11048 saved += count * 8;
9728c9d1
PB
11049 }
11050 count = 0;
11051 }
11052 else
11053 count++;
11054 }
11055 if (count > 0)
11056 {
11057 if (count == 2 && !arm_arch6)
11058 count++;
8edfc4cc 11059 saved += count * 8;
9728c9d1
PB
11060 }
11061 }
11062 return saved;
11063}
11064
11065
699a4925 11066/* Generate a function exit sequence. If REALLY_RETURN is false, then do
6d3d9133 11067 everything bar the final return instruction. */
cd2b33d0 11068const char *
e32bac5b 11069output_return_instruction (rtx operand, int really_return, int reverse)
ff9940b0 11070{
6d3d9133 11071 char conditional[10];
ff9940b0 11072 char instr[100];
b279b20a 11073 unsigned reg;
6d3d9133
NC
11074 unsigned long live_regs_mask;
11075 unsigned long func_type;
5848830f 11076 arm_stack_offsets *offsets;
e26053d1 11077
6d3d9133 11078 func_type = arm_current_func_type ();
e2c671ba 11079
6d3d9133 11080 if (IS_NAKED (func_type))
d5b7b3ae 11081 return "";
6d3d9133
NC
11082
11083 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
e2c671ba 11084 {
699a4925
RE
11085 /* If this function was declared non-returning, and we have
11086 found a tail call, then we have to trust that the called
11087 function won't return. */
3a5a4282
PB
11088 if (really_return)
11089 {
11090 rtx ops[2];
f676971a 11091
3a5a4282
PB
11092 /* Otherwise, trap an attempted return by aborting. */
11093 ops[0] = operand;
f676971a 11094 ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
3a5a4282
PB
11095 : "abort");
11096 assemble_external_libcall (ops[1]);
11097 output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
11098 }
f676971a 11099
e2c671ba
RE
11100 return "";
11101 }
6d3d9133 11102
e3b5732b 11103 gcc_assert (!cfun->calls_alloca || really_return);
ff9940b0 11104
c414f8a9 11105 sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
d5b7b3ae 11106
6d3d9133 11107 return_used_this_function = 1;
ff9940b0 11108
954954d1
PB
11109 offsets = arm_get_frame_offsets ();
11110 live_regs_mask = offsets->saved_regs_mask;
ff9940b0 11111
1768c26f 11112 if (live_regs_mask)
6d3d9133 11113 {
1768c26f
PB
11114 const char * return_reg;
11115
f676971a 11116 /* If we do not have any special requirements for function exit
a15908a4 11117 (e.g. interworking) then we can load the return address
1768c26f
PB
11118 directly into the PC. Otherwise we must load it into LR. */
11119 if (really_return
a15908a4 11120 && (IS_INTERRUPT (func_type) || !TARGET_INTERWORK))
1768c26f 11121 return_reg = reg_names[PC_REGNUM];
6d3d9133 11122 else
1768c26f
PB
11123 return_reg = reg_names[LR_REGNUM];
11124
6d3d9133 11125 if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
b034930f
ILT
11126 {
11127 /* There are three possible reasons for the IP register
11128 being saved. 1) a stack frame was created, in which case
11129 IP contains the old stack pointer, or 2) an ISR routine
11130 corrupted it, or 3) it was saved to align the stack on
11131 iWMMXt. In case 1, restore IP into SP, otherwise just
11132 restore IP. */
11133 if (frame_pointer_needed)
11134 {
11135 live_regs_mask &= ~ (1 << IP_REGNUM);
11136 live_regs_mask |= (1 << SP_REGNUM);
11137 }
11138 else
e6d29d15 11139 gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
b034930f 11140 }
f3bb6135 11141
3a7731fd
PB
11142 /* On some ARM architectures it is faster to use LDR rather than
11143 LDM to load a single register. On other architectures, the
11144 cost is the same. In 26 bit mode, or for exception handlers,
11145 we have to use LDM to load the PC so that the CPSR is also
11146 restored. */
11147 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
b279b20a
NC
11148 if (live_regs_mask == (1U << reg))
11149 break;
11150
3a7731fd
PB
11151 if (reg <= LAST_ARM_REGNUM
11152 && (reg != LR_REGNUM
f676971a 11153 || ! really_return
61f0ccff 11154 || ! IS_INTERRUPT (func_type)))
3a7731fd 11155 {
f676971a 11156 sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
3a7731fd 11157 (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
6d3d9133 11158 }
ff9940b0 11159 else
1d5473cb 11160 {
1768c26f
PB
11161 char *p;
11162 int first = 1;
6d3d9133 11163
699a4925
RE
11164 /* Generate the load multiple instruction to restore the
11165 registers. Note we can get here, even if
11166 frame_pointer_needed is true, but only if sp already
11167 points to the base of the saved core registers. */
11168 if (live_regs_mask & (1 << SP_REGNUM))
a72d4945 11169 {
5848830f
PB
11170 unsigned HOST_WIDE_INT stack_adjust;
11171
5848830f 11172 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
e6d29d15 11173 gcc_assert (stack_adjust == 0 || stack_adjust == 4);
a72d4945 11174
5b3e6663 11175 if (stack_adjust && arm_arch5 && TARGET_ARM)
a72d4945
RE
11176 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
11177 else
11178 {
b279b20a
NC
11179 /* If we can't use ldmib (SA110 bug),
11180 then try to pop r3 instead. */
a72d4945
RE
11181 if (stack_adjust)
11182 live_regs_mask |= 1 << 3;
11183 sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
11184 }
11185 }
da6558fd 11186 else
1768c26f
PB
11187 sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
11188
11189 p = instr + strlen (instr);
6d3d9133 11190
1768c26f
PB
11191 for (reg = 0; reg <= SP_REGNUM; reg++)
11192 if (live_regs_mask & (1 << reg))
11193 {
11194 int l = strlen (reg_names[reg]);
11195
11196 if (first)
11197 first = 0;
11198 else
11199 {
11200 memcpy (p, ", ", 2);
11201 p += 2;
11202 }
11203
11204 memcpy (p, "%|", 2);
11205 memcpy (p + 2, reg_names[reg], l);
11206 p += l + 2;
11207 }
f676971a 11208
1768c26f
PB
11209 if (live_regs_mask & (1 << LR_REGNUM))
11210 {
b17fe233 11211 sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
61f0ccff
RE
11212 /* If returning from an interrupt, restore the CPSR. */
11213 if (IS_INTERRUPT (func_type))
b17fe233 11214 strcat (p, "^");
1768c26f
PB
11215 }
11216 else
11217 strcpy (p, "}");
1d5473cb 11218 }
da6558fd 11219
1768c26f
PB
11220 output_asm_insn (instr, & operand);
11221
3a7731fd
PB
11222 /* See if we need to generate an extra instruction to
11223 perform the actual function return. */
11224 if (really_return
11225 && func_type != ARM_FT_INTERWORKED
11226 && (live_regs_mask & (1 << LR_REGNUM)) != 0)
da6558fd 11227 {
3a7731fd
PB
11228 /* The return has already been handled
11229 by loading the LR into the PC. */
11230 really_return = 0;
da6558fd 11231 }
ff9940b0 11232 }
e26053d1 11233
1768c26f 11234 if (really_return)
ff9940b0 11235 {
6d3d9133
NC
11236 switch ((int) ARM_FUNC_TYPE (func_type))
11237 {
11238 case ARM_FT_ISR:
11239 case ARM_FT_FIQ:
5b3e6663 11240 /* ??? This is wrong for unified assembly syntax. */
6d3d9133
NC
11241 sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
11242 break;
11243
11244 case ARM_FT_INTERWORKED:
11245 sprintf (instr, "bx%s\t%%|lr", conditional);
11246 break;
11247
11248 case ARM_FT_EXCEPTION:
5b3e6663 11249 /* ??? This is wrong for unified assembly syntax. */
6d3d9133
NC
11250 sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
11251 break;
11252
11253 default:
68d560d4
RE
11254 /* Use bx if it's available. */
11255 if (arm_arch5 || arm_arch4t)
f676971a 11256 sprintf (instr, "bx%s\t%%|lr", conditional);
1768c26f 11257 else
61f0ccff 11258 sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
6d3d9133
NC
11259 break;
11260 }
1768c26f
PB
11261
11262 output_asm_insn (instr, & operand);
ff9940b0 11263 }
f3bb6135 11264
ff9940b0
RE
11265 return "";
11266}
11267
ef179a26
NC
11268/* Write the function name into the code section, directly preceding
11269 the function prologue.
11270
11271 Code will be output similar to this:
11272 t0
11273 .ascii "arm_poke_function_name", 0
11274 .align
11275 t1
11276 .word 0xff000000 + (t1 - t0)
11277 arm_poke_function_name
11278 mov ip, sp
11279 stmfd sp!, {fp, ip, lr, pc}
11280 sub fp, ip, #4
11281
11282 When performing a stack backtrace, code can inspect the value
11283 of 'pc' stored at 'fp' + 0. If the trace function then looks
11284 at location pc - 12 and the top 8 bits are set, then we know
11285 that there is a function name embedded immediately preceding this
11286 location and has length ((pc[-3]) & 0xff000000).
11287
11288 We assume that pc is declared as a pointer to an unsigned long.
11289
11290 It is of no benefit to output the function name if we are assembling
11291 a leaf function. These function types will not contain a stack
11292 backtrace structure, therefore it is not possible to determine the
11293 function name. */
ef179a26 11294void
e32bac5b 11295arm_poke_function_name (FILE *stream, const char *name)
ef179a26
NC
11296{
11297 unsigned long alignlength;
11298 unsigned long length;
11299 rtx x;
11300
d5b7b3ae 11301 length = strlen (name) + 1;
0c2ca901 11302 alignlength = ROUND_UP_WORD (length);
f676971a 11303
949d79eb 11304 ASM_OUTPUT_ASCII (stream, name, length);
ef179a26 11305 ASM_OUTPUT_ALIGN (stream, 2);
30cf4896 11306 x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
301d03af 11307 assemble_aligned_integer (UNITS_PER_WORD, x);
ef179a26
NC
11308}
11309
6d3d9133
NC
11310/* Place some comments into the assembler stream
11311 describing the current function. */
08c148a8 11312static void
e32bac5b 11313arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
cce8749e 11314{
6d3d9133 11315 unsigned long func_type;
08c148a8 11316
5b3e6663 11317 if (TARGET_THUMB1)
08c148a8 11318 {
5b3e6663 11319 thumb1_output_function_prologue (f, frame_size);
08c148a8
NB
11320 return;
11321 }
f676971a 11322
6d3d9133 11323 /* Sanity check. */
e6d29d15 11324 gcc_assert (!arm_ccfsm_state && !arm_target_insn);
31fdb4d5 11325
6d3d9133 11326 func_type = arm_current_func_type ();
f676971a 11327
6d3d9133
NC
11328 switch ((int) ARM_FUNC_TYPE (func_type))
11329 {
11330 default:
11331 case ARM_FT_NORMAL:
11332 break;
11333 case ARM_FT_INTERWORKED:
11334 asm_fprintf (f, "\t%@ Function supports interworking.\n");
11335 break;
6d3d9133
NC
11336 case ARM_FT_ISR:
11337 asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
11338 break;
11339 case ARM_FT_FIQ:
11340 asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
11341 break;
11342 case ARM_FT_EXCEPTION:
11343 asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
11344 break;
11345 }
f676971a 11346
6d3d9133
NC
11347 if (IS_NAKED (func_type))
11348 asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
11349
11350 if (IS_VOLATILE (func_type))
11351 asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
11352
11353 if (IS_NESTED (func_type))
11354 asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
5b3e6663
PB
11355 if (IS_STACKALIGN (func_type))
11356 asm_fprintf (f, "\t%@ Stack Align: May be called with mis-aligned SP.\n");
f676971a 11357
c53dddc2 11358 asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
38173d38
JH
11359 crtl->args.size,
11360 crtl->args.pretend_args_size, frame_size);
6d3d9133 11361
3cb66fd7 11362 asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
dd18ae56 11363 frame_pointer_needed,
3cb66fd7 11364 cfun->machine->uses_anonymous_args);
cce8749e 11365
6f7ebcbb
NC
11366 if (cfun->machine->lr_save_eliminated)
11367 asm_fprintf (f, "\t%@ link register save eliminated.\n");
11368
e3b5732b 11369 if (crtl->calls_eh_return)
c9ca9b88
PB
11370 asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
11371
f676971a 11372 return_used_this_function = 0;
f3bb6135 11373}
cce8749e 11374
cd2b33d0 11375const char *
a72d4945 11376arm_output_epilogue (rtx sibling)
cce8749e 11377{
949d79eb 11378 int reg;
6f7ebcbb 11379 unsigned long saved_regs_mask;
6d3d9133 11380 unsigned long func_type;
f676971a 11381 /* Floats_offset is the offset from the "virtual" frame. In an APCS
c882c7ac
RE
11382 frame that is $fp + 4 for a non-variadic function. */
11383 int floats_offset = 0;
cce8749e 11384 rtx operands[3];
d5b7b3ae 11385 FILE * f = asm_out_file;
5a9335ef 11386 unsigned int lrm_count = 0;
a72d4945 11387 int really_return = (sibling == NULL);
9b66ebb1 11388 int start_reg;
5848830f 11389 arm_stack_offsets *offsets;
cce8749e 11390
6d3d9133
NC
11391 /* If we have already generated the return instruction
11392 then it is futile to generate anything else. */
a72d4945 11393 if (use_return_insn (FALSE, sibling) && return_used_this_function)
949d79eb 11394 return "";
cce8749e 11395
6d3d9133 11396 func_type = arm_current_func_type ();
d5b7b3ae 11397
6d3d9133
NC
11398 if (IS_NAKED (func_type))
11399 /* Naked functions don't have epilogues. */
11400 return "";
0616531f 11401
6d3d9133 11402 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
e2c671ba 11403 {
86efdc8e 11404 rtx op;
f676971a 11405
6d3d9133 11406 /* A volatile function should never return. Call abort. */
ed0e6530 11407 op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
2b835d68 11408 assemble_external_libcall (op);
e2c671ba 11409 output_asm_insn ("bl\t%a0", &op);
f676971a 11410
949d79eb 11411 return "";
e2c671ba
RE
11412 }
11413
e6d29d15
NS
11414 /* If we are throwing an exception, then we really must be doing a
11415 return, so we can't tail-call. */
e3b5732b 11416 gcc_assert (!crtl->calls_eh_return || really_return);
f676971a 11417
5848830f 11418 offsets = arm_get_frame_offsets ();
954954d1 11419 saved_regs_mask = offsets->saved_regs_mask;
5a9335ef
NC
11420
11421 if (TARGET_IWMMXT)
11422 lrm_count = bit_count (saved_regs_mask);
11423
5848830f 11424 floats_offset = offsets->saved_args;
6d3d9133 11425 /* Compute how far away the floats will be. */
5a9335ef 11426 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
6f7ebcbb 11427 if (saved_regs_mask & (1 << reg))
6ed30148 11428 floats_offset += 4;
f676971a 11429
ec6237e4 11430 if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
cce8749e 11431 {
9b66ebb1 11432 /* This variable is for the Virtual Frame Pointer, not VFP regs. */
5848830f 11433 int vfp_offset = offsets->frame;
c882c7ac 11434
29ad9694 11435 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
b111229a 11436 {
9b66ebb1 11437 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
6fb5fa3c 11438 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
b111229a
RE
11439 {
11440 floats_offset += 12;
f676971a 11441 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
c882c7ac 11442 reg, FP_REGNUM, floats_offset - vfp_offset);
b111229a
RE
11443 }
11444 }
11445 else
11446 {
9b66ebb1 11447 start_reg = LAST_FPA_REGNUM;
b111229a 11448
9b66ebb1 11449 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
b111229a 11450 {
6fb5fa3c 11451 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
b111229a
RE
11452 {
11453 floats_offset += 12;
f676971a 11454
6354dc9b 11455 /* We can't unstack more than four registers at once. */
b111229a
RE
11456 if (start_reg - reg == 3)
11457 {
dd18ae56 11458 asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
c882c7ac 11459 reg, FP_REGNUM, floats_offset - vfp_offset);
b111229a
RE
11460 start_reg = reg - 1;
11461 }
11462 }
11463 else
11464 {
11465 if (reg != start_reg)
dd18ae56
NC
11466 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
11467 reg + 1, start_reg - reg,
c882c7ac 11468 FP_REGNUM, floats_offset - vfp_offset);
b111229a
RE
11469 start_reg = reg - 1;
11470 }
11471 }
11472
11473 /* Just in case the last register checked also needs unstacking. */
11474 if (reg != start_reg)
dd18ae56
NC
11475 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
11476 reg + 1, start_reg - reg,
c882c7ac 11477 FP_REGNUM, floats_offset - vfp_offset);
b111229a 11478 }
6d3d9133 11479
9b66ebb1
PB
11480 if (TARGET_HARD_FLOAT && TARGET_VFP)
11481 {
9728c9d1 11482 int saved_size;
9b66ebb1 11483
8edfc4cc
MS
11484 /* The fldmd insns do not have base+offset addressing
11485 modes, so we use IP to hold the address. */
9728c9d1 11486 saved_size = arm_get_vfp_saved_size ();
9b66ebb1 11487
9728c9d1 11488 if (saved_size > 0)
9b66ebb1 11489 {
9728c9d1 11490 floats_offset += saved_size;
9b66ebb1
PB
11491 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
11492 FP_REGNUM, floats_offset - vfp_offset);
11493 }
11494 start_reg = FIRST_VFP_REGNUM;
11495 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
11496 {
6fb5fa3c
DB
11497 if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
11498 && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
9b66ebb1
PB
11499 {
11500 if (start_reg != reg)
8edfc4cc 11501 vfp_output_fldmd (f, IP_REGNUM,
9728c9d1
PB
11502 (start_reg - FIRST_VFP_REGNUM) / 2,
11503 (reg - start_reg) / 2);
9b66ebb1
PB
11504 start_reg = reg + 2;
11505 }
11506 }
11507 if (start_reg != reg)
8edfc4cc 11508 vfp_output_fldmd (f, IP_REGNUM,
9728c9d1
PB
11509 (start_reg - FIRST_VFP_REGNUM) / 2,
11510 (reg - start_reg) / 2);
9b66ebb1
PB
11511 }
11512
5a9335ef
NC
11513 if (TARGET_IWMMXT)
11514 {
11515 /* The frame pointer is guaranteed to be non-double-word aligned.
11516 This is because it is set to (old_stack_pointer - 4) and the
11517 old_stack_pointer was double word aligned. Thus the offset to
11518 the iWMMXt registers to be loaded must also be non-double-word
11519 sized, so that the resultant address *is* double-word aligned.
11520 We can ignore floats_offset since that was already included in
11521 the live_regs_mask. */
11522 lrm_count += (lrm_count % 2 ? 2 : 1);
f676971a 11523
01d4c813 11524 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
6fb5fa3c 11525 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
5a9335ef 11526 {
f676971a 11527 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
5a9335ef 11528 reg, FP_REGNUM, lrm_count * 4);
f676971a 11529 lrm_count += 2;
5a9335ef
NC
11530 }
11531 }
11532
6f7ebcbb 11533 /* saved_regs_mask should contain the IP, which at the time of stack
6d3d9133
NC
11534 frame generation actually contains the old stack pointer. So a
11535 quick way to unwind the stack is just pop the IP register directly
11536 into the stack pointer. */
e6d29d15 11537 gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
6f7ebcbb
NC
11538 saved_regs_mask &= ~ (1 << IP_REGNUM);
11539 saved_regs_mask |= (1 << SP_REGNUM);
6d3d9133 11540
6f7ebcbb 11541 /* There are two registers left in saved_regs_mask - LR and PC. We
6d3d9133
NC
11542 only need to restore the LR register (the return address), but to
11543 save time we can load it directly into the PC, unless we need a
11544 special function exit sequence, or we are not really returning. */
c9ca9b88
PB
11545 if (really_return
11546 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
e3b5732b 11547 && !crtl->calls_eh_return)
6d3d9133
NC
11548 /* Delete the LR from the register mask, so that the LR on
11549 the stack is loaded into the PC in the register mask. */
6f7ebcbb 11550 saved_regs_mask &= ~ (1 << LR_REGNUM);
b111229a 11551 else
6f7ebcbb 11552 saved_regs_mask &= ~ (1 << PC_REGNUM);
efc2515b
RE
11553
11554 /* We must use SP as the base register, because SP is one of the
11555 registers being restored. If an interrupt or page fault
11556 happens in the ldm instruction, the SP might or might not
11557 have been restored. That would be bad, as then SP will no
11558 longer indicate the safe area of stack, and we can get stack
11559 corruption. Using SP as the base register means that it will
11560 be reset correctly to the original value, should an interrupt
699a4925
RE
11561 occur. If the stack pointer already points at the right
11562 place, then omit the subtraction. */
5848830f 11563 if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
e3b5732b 11564 || cfun->calls_alloca)
699a4925
RE
11565 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
11566 4 * bit_count (saved_regs_mask));
a15908a4 11567 print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask, 0);
7b8b8ade
NC
11568
11569 if (IS_INTERRUPT (func_type))
11570 /* Interrupt handlers will have pushed the
11571 IP onto the stack, so restore it now. */
a15908a4 11572 print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, 1 << IP_REGNUM, 0);
cce8749e
CH
11573 }
11574 else
11575 {
ec6237e4
PB
11576 /* This branch is executed for ARM mode (non-apcs frames) and
11577 Thumb-2 mode. Frame layout is essentially the same for those
11578 cases, except that in ARM mode frame pointer points to the
11579 first saved register, while in Thumb-2 mode the frame pointer points
11580 to the last saved register.
11581
11582 It is possible to make frame pointer point to last saved
11583 register in both cases, and remove some conditionals below.
11584 That means that fp setup in prologue would be just "mov fp, sp"
11585 and sp restore in epilogue would be just "mov sp, fp", whereas
11586 now we have to use add/sub in those cases. However, the value
11587 of that would be marginal, as both mov and add/sub are 32-bit
11588 in ARM mode, and it would require extra conditionals
11589 in arm_expand_prologue to distingish ARM-apcs-frame case
11590 (where frame pointer is required to point at first register)
11591 and ARM-non-apcs-frame. Therefore, such change is postponed
11592 until real need arise. */
5b3e6663 11593 HOST_WIDE_INT amount;
a15908a4 11594 int rfe;
d2288d8d 11595 /* Restore stack pointer if necessary. */
ec6237e4 11596 if (TARGET_ARM && frame_pointer_needed)
5b3e6663
PB
11597 {
11598 operands[0] = stack_pointer_rtx;
ec6237e4
PB
11599 operands[1] = hard_frame_pointer_rtx;
11600
11601 operands[2] = GEN_INT (offsets->frame - offsets->saved_regs);
11602 output_add_immediate (operands);
5b3e6663 11603 }
ec6237e4 11604 else
5b3e6663 11605 {
ec6237e4
PB
11606 if (frame_pointer_needed)
11607 {
11608 /* For Thumb-2 restore sp from the frame pointer.
11609 Operand restrictions mean we have to incrememnt FP, then copy
11610 to SP. */
11611 amount = offsets->locals_base - offsets->saved_regs;
11612 operands[0] = hard_frame_pointer_rtx;
11613 }
11614 else
11615 {
954954d1 11616 unsigned long count;
ec6237e4
PB
11617 operands[0] = stack_pointer_rtx;
11618 amount = offsets->outgoing_args - offsets->saved_regs;
954954d1
PB
11619 /* pop call clobbered registers if it avoids a
11620 separate stack adjustment. */
11621 count = offsets->saved_regs - offsets->saved_args;
11622 if (optimize_size
11623 && count != 0
e3b5732b 11624 && !crtl->calls_eh_return
954954d1
PB
11625 && bit_count(saved_regs_mask) * 4 == count
11626 && !IS_INTERRUPT (func_type)
e3b5732b 11627 && !crtl->tail_call_emit)
954954d1
PB
11628 {
11629 unsigned long mask;
11630 mask = (1 << (arm_size_return_regs() / 4)) - 1;
11631 mask ^= 0xf;
11632 mask &= ~saved_regs_mask;
11633 reg = 0;
11634 while (bit_count (mask) * 4 > amount)
11635 {
11636 while ((mask & (1 << reg)) == 0)
11637 reg++;
11638 mask &= ~(1 << reg);
11639 }
11640 if (bit_count (mask) * 4 == amount) {
11641 amount = 0;
11642 saved_regs_mask |= mask;
11643 }
11644 }
ec6237e4
PB
11645 }
11646
11647 if (amount)
11648 {
11649 operands[1] = operands[0];
11650 operands[2] = GEN_INT (amount);
11651 output_add_immediate (operands);
11652 }
11653 if (frame_pointer_needed)
11654 asm_fprintf (f, "\tmov\t%r, %r\n",
11655 SP_REGNUM, HARD_FRAME_POINTER_REGNUM);
d2288d8d
TG
11656 }
11657
29ad9694 11658 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
b111229a 11659 {
9b66ebb1 11660 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
6fb5fa3c 11661 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
dd18ae56
NC
11662 asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
11663 reg, SP_REGNUM);
b111229a
RE
11664 }
11665 else
11666 {
9b66ebb1 11667 start_reg = FIRST_FPA_REGNUM;
b111229a 11668
9b66ebb1 11669 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
b111229a 11670 {
6fb5fa3c 11671 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
b111229a
RE
11672 {
11673 if (reg - start_reg == 3)
11674 {
dd18ae56
NC
11675 asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
11676 start_reg, SP_REGNUM);
b111229a
RE
11677 start_reg = reg + 1;
11678 }
11679 }
11680 else
11681 {
11682 if (reg != start_reg)
dd18ae56
NC
11683 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
11684 start_reg, reg - start_reg,
11685 SP_REGNUM);
f676971a 11686
b111229a
RE
11687 start_reg = reg + 1;
11688 }
11689 }
11690
11691 /* Just in case the last register checked also needs unstacking. */
11692 if (reg != start_reg)
dd18ae56
NC
11693 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
11694 start_reg, reg - start_reg, SP_REGNUM);
b111229a
RE
11695 }
11696
9b66ebb1
PB
11697 if (TARGET_HARD_FLOAT && TARGET_VFP)
11698 {
11699 start_reg = FIRST_VFP_REGNUM;
11700 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
11701 {
6fb5fa3c
DB
11702 if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
11703 && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
9b66ebb1
PB
11704 {
11705 if (start_reg != reg)
8edfc4cc 11706 vfp_output_fldmd (f, SP_REGNUM,
9728c9d1
PB
11707 (start_reg - FIRST_VFP_REGNUM) / 2,
11708 (reg - start_reg) / 2);
9b66ebb1
PB
11709 start_reg = reg + 2;
11710 }
11711 }
11712 if (start_reg != reg)
8edfc4cc 11713 vfp_output_fldmd (f, SP_REGNUM,
9728c9d1
PB
11714 (start_reg - FIRST_VFP_REGNUM) / 2,
11715 (reg - start_reg) / 2);
9b66ebb1 11716 }
5a9335ef
NC
11717 if (TARGET_IWMMXT)
11718 for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
6fb5fa3c 11719 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
01d4c813 11720 asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
5a9335ef 11721
6d3d9133 11722 /* If we can, restore the LR into the PC. */
a15908a4
PB
11723 if (ARM_FUNC_TYPE (func_type) != ARM_FT_INTERWORKED
11724 && (TARGET_ARM || ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
5b3e6663 11725 && !IS_STACKALIGN (func_type)
6d3d9133 11726 && really_return
38173d38 11727 && crtl->args.pretend_args_size == 0
c9ca9b88 11728 && saved_regs_mask & (1 << LR_REGNUM)
e3b5732b 11729 && !crtl->calls_eh_return)
cce8749e 11730 {
6f7ebcbb
NC
11731 saved_regs_mask &= ~ (1 << LR_REGNUM);
11732 saved_regs_mask |= (1 << PC_REGNUM);
a15908a4 11733 rfe = IS_INTERRUPT (func_type);
6d3d9133 11734 }
a15908a4
PB
11735 else
11736 rfe = 0;
d5b7b3ae 11737
6d3d9133 11738 /* Load the registers off the stack. If we only have one register
5b3e6663
PB
11739 to load use the LDR instruction - it is faster. For Thumb-2
11740 always use pop and the assembler will pick the best instruction.*/
a15908a4
PB
11741 if (TARGET_ARM && saved_regs_mask == (1 << LR_REGNUM)
11742 && !IS_INTERRUPT(func_type))
6d3d9133 11743 {
c9ca9b88 11744 asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
cce8749e 11745 }
6f7ebcbb 11746 else if (saved_regs_mask)
f1acdf8b
NC
11747 {
11748 if (saved_regs_mask & (1 << SP_REGNUM))
11749 /* Note - write back to the stack register is not enabled
112cdef5 11750 (i.e. "ldmfd sp!..."). We know that the stack pointer is
f1acdf8b
NC
11751 in the list of registers and if we add writeback the
11752 instruction becomes UNPREDICTABLE. */
a15908a4
PB
11753 print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask,
11754 rfe);
5b3e6663 11755 else if (TARGET_ARM)
a15908a4
PB
11756 print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, saved_regs_mask,
11757 rfe);
f1acdf8b 11758 else
a15908a4 11759 print_multi_reg (f, "pop\t", SP_REGNUM, saved_regs_mask, 0);
f1acdf8b 11760 }
6d3d9133 11761
38173d38 11762 if (crtl->args.pretend_args_size)
cce8749e 11763 {
6d3d9133
NC
11764 /* Unwind the pre-pushed regs. */
11765 operands[0] = operands[1] = stack_pointer_rtx;
38173d38 11766 operands[2] = GEN_INT (crtl->args.pretend_args_size);
6d3d9133
NC
11767 output_add_immediate (operands);
11768 }
11769 }
32de079a 11770
2966b00e 11771 /* We may have already restored PC directly from the stack. */
0cc3dda8 11772 if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
6d3d9133 11773 return "";
d5b7b3ae 11774
c9ca9b88 11775 /* Stack adjustment for exception handler. */
e3b5732b 11776 if (crtl->calls_eh_return)
f676971a 11777 asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
c9ca9b88
PB
11778 ARM_EH_STACKADJ_REGNUM);
11779
6d3d9133
NC
11780 /* Generate the return instruction. */
11781 switch ((int) ARM_FUNC_TYPE (func_type))
11782 {
6d3d9133
NC
11783 case ARM_FT_ISR:
11784 case ARM_FT_FIQ:
11785 asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
11786 break;
11787
11788 case ARM_FT_EXCEPTION:
11789 asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
11790 break;
11791
11792 case ARM_FT_INTERWORKED:
11793 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
11794 break;
11795
11796 default:
5b3e6663
PB
11797 if (IS_STACKALIGN (func_type))
11798 {
11799 /* See comment in arm_expand_prologue. */
11800 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, 0);
11801 }
68d560d4
RE
11802 if (arm_arch5 || arm_arch4t)
11803 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
11804 else
11805 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
6d3d9133 11806 break;
cce8749e 11807 }
f3bb6135 11808
949d79eb
RE
11809 return "";
11810}
11811
08c148a8 11812static void
e32bac5b 11813arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
5848830f 11814 HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
949d79eb 11815{
5848830f
PB
11816 arm_stack_offsets *offsets;
11817
5b3e6663 11818 if (TARGET_THUMB1)
d5b7b3ae 11819 {
b12a00f1
RE
11820 int regno;
11821
11822 /* Emit any call-via-reg trampolines that are needed for v4t support
11823 of call_reg and call_value_reg type insns. */
57ecec57 11824 for (regno = 0; regno < LR_REGNUM; regno++)
b12a00f1
RE
11825 {
11826 rtx label = cfun->machine->call_via[regno];
11827
11828 if (label != NULL)
11829 {
d6b5193b 11830 switch_to_section (function_section (current_function_decl));
b12a00f1
RE
11831 targetm.asm_out.internal_label (asm_out_file, "L",
11832 CODE_LABEL_NUMBER (label));
11833 asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
11834 }
11835 }
11836
d5b7b3ae
RE
11837 /* ??? Probably not safe to set this here, since it assumes that a
11838 function will be emitted as assembly immediately after we generate
11839 RTL for it. This does not happen for inline functions. */
11840 return_used_this_function = 0;
11841 }
5b3e6663 11842 else /* TARGET_32BIT */
d5b7b3ae 11843 {
0977774b 11844 /* We need to take into account any stack-frame rounding. */
5848830f 11845 offsets = arm_get_frame_offsets ();
0977774b 11846
e6d29d15
NS
11847 gcc_assert (!use_return_insn (FALSE, NULL)
11848 || !return_used_this_function
11849 || offsets->saved_regs == offsets->outgoing_args
11850 || frame_pointer_needed);
f3bb6135 11851
d5b7b3ae 11852 /* Reset the ARM-specific per-function variables. */
d5b7b3ae
RE
11853 after_arm_reorg = 0;
11854 }
f3bb6135 11855}
e2c671ba 11856
2c849145
JM
11857/* Generate and emit an insn that we will recognize as a push_multi.
11858 Unfortunately, since this insn does not reflect very well the actual
11859 semantics of the operation, we need to annotate the insn for the benefit
11860 of DWARF2 frame unwind information. */
2c849145 11861static rtx
b279b20a 11862emit_multi_reg_push (unsigned long mask)
e2c671ba
RE
11863{
11864 int num_regs = 0;
9b598fa0 11865 int num_dwarf_regs;
e2c671ba
RE
11866 int i, j;
11867 rtx par;
2c849145 11868 rtx dwarf;
87e27392 11869 int dwarf_par_index;
2c849145 11870 rtx tmp, reg;
e2c671ba 11871
d5b7b3ae 11872 for (i = 0; i <= LAST_ARM_REGNUM; i++)
e2c671ba 11873 if (mask & (1 << i))
5895f793 11874 num_regs++;
e2c671ba 11875
e6d29d15 11876 gcc_assert (num_regs && num_regs <= 16);
e2c671ba 11877
9b598fa0
RE
11878 /* We don't record the PC in the dwarf frame information. */
11879 num_dwarf_regs = num_regs;
11880 if (mask & (1 << PC_REGNUM))
11881 num_dwarf_regs--;
11882
87e27392 11883 /* For the body of the insn we are going to generate an UNSPEC in
05713b80 11884 parallel with several USEs. This allows the insn to be recognized
87e27392
NC
11885 by the push_multi pattern in the arm.md file. The insn looks
11886 something like this:
11887
f676971a 11888 (parallel [
b15bca31
RE
11889 (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
11890 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
87e27392
NC
11891 (use (reg:SI 11 fp))
11892 (use (reg:SI 12 ip))
11893 (use (reg:SI 14 lr))
11894 (use (reg:SI 15 pc))
11895 ])
11896
11897 For the frame note however, we try to be more explicit and actually
11898 show each register being stored into the stack frame, plus a (single)
11899 decrement of the stack pointer. We do it this way in order to be
11900 friendly to the stack unwinding code, which only wants to see a single
11901 stack decrement per instruction. The RTL we generate for the note looks
11902 something like this:
11903
f676971a 11904 (sequence [
87e27392
NC
11905 (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
11906 (set (mem:SI (reg:SI sp)) (reg:SI r4))
11907 (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
11908 (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
11909 (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
87e27392
NC
11910 ])
11911
11912 This sequence is used both by the code to support stack unwinding for
11913 exceptions handlers and the code to generate dwarf2 frame debugging. */
f676971a 11914
43cffd11 11915 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
9b598fa0 11916 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
87e27392 11917 dwarf_par_index = 1;
e2c671ba 11918
d5b7b3ae 11919 for (i = 0; i <= LAST_ARM_REGNUM; i++)
e2c671ba
RE
11920 {
11921 if (mask & (1 << i))
11922 {
2c849145
JM
11923 reg = gen_rtx_REG (SImode, i);
11924
e2c671ba 11925 XVECEXP (par, 0, 0)
43cffd11 11926 = gen_rtx_SET (VOIDmode,
31fa16b6
RE
11927 gen_frame_mem (BLKmode,
11928 gen_rtx_PRE_DEC (BLKmode,
11929 stack_pointer_rtx)),
43cffd11 11930 gen_rtx_UNSPEC (BLKmode,
2c849145 11931 gen_rtvec (1, reg),
9b598fa0 11932 UNSPEC_PUSH_MULT));
2c849145 11933
9b598fa0
RE
11934 if (i != PC_REGNUM)
11935 {
11936 tmp = gen_rtx_SET (VOIDmode,
31fa16b6 11937 gen_frame_mem (SImode, stack_pointer_rtx),
9b598fa0
RE
11938 reg);
11939 RTX_FRAME_RELATED_P (tmp) = 1;
11940 XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
11941 dwarf_par_index++;
11942 }
2c849145 11943
e2c671ba
RE
11944 break;
11945 }
11946 }
11947
11948 for (j = 1, i++; j < num_regs; i++)
11949 {
11950 if (mask & (1 << i))
11951 {
2c849145
JM
11952 reg = gen_rtx_REG (SImode, i);
11953
11954 XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
11955
9b598fa0
RE
11956 if (i != PC_REGNUM)
11957 {
31fa16b6
RE
11958 tmp
11959 = gen_rtx_SET (VOIDmode,
11960 gen_frame_mem (SImode,
9b598fa0
RE
11961 plus_constant (stack_pointer_rtx,
11962 4 * j)),
31fa16b6 11963 reg);
9b598fa0
RE
11964 RTX_FRAME_RELATED_P (tmp) = 1;
11965 XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
11966 }
11967
e2c671ba
RE
11968 j++;
11969 }
11970 }
b111229a 11971
2c849145 11972 par = emit_insn (par);
f676971a 11973
d66437c5 11974 tmp = gen_rtx_SET (VOIDmode,
87e27392 11975 stack_pointer_rtx,
d66437c5 11976 plus_constant (stack_pointer_rtx, -4 * num_regs));
87e27392
NC
11977 RTX_FRAME_RELATED_P (tmp) = 1;
11978 XVECEXP (dwarf, 0, 0) = tmp;
f676971a 11979
2c849145
JM
11980 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
11981 REG_NOTES (par));
11982 return par;
b111229a
RE
11983}
11984
4f5dfed0 11985/* Calculate the size of the return value that is passed in registers. */
466e4b7a 11986static unsigned
4f5dfed0
JC
11987arm_size_return_regs (void)
11988{
11989 enum machine_mode mode;
11990
38173d38
JH
11991 if (crtl->return_rtx != 0)
11992 mode = GET_MODE (crtl->return_rtx);
4f5dfed0
JC
11993 else
11994 mode = DECL_MODE (DECL_RESULT (current_function_decl));
11995
11996 return GET_MODE_SIZE (mode);
11997}
11998
2c849145 11999static rtx
e32bac5b 12000emit_sfm (int base_reg, int count)
b111229a
RE
12001{
12002 rtx par;
2c849145
JM
12003 rtx dwarf;
12004 rtx tmp, reg;
b111229a
RE
12005 int i;
12006
43cffd11 12007 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
8ee6eb4e 12008 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
2c849145
JM
12009
12010 reg = gen_rtx_REG (XFmode, base_reg++);
43cffd11
RE
12011
12012 XVECEXP (par, 0, 0)
f676971a 12013 = gen_rtx_SET (VOIDmode,
31fa16b6
RE
12014 gen_frame_mem (BLKmode,
12015 gen_rtx_PRE_DEC (BLKmode,
12016 stack_pointer_rtx)),
43cffd11 12017 gen_rtx_UNSPEC (BLKmode,
2c849145 12018 gen_rtvec (1, reg),
b15bca31 12019 UNSPEC_PUSH_MULT));
f676971a 12020 tmp = gen_rtx_SET (VOIDmode,
31fa16b6 12021 gen_frame_mem (XFmode, stack_pointer_rtx), reg);
2c849145 12022 RTX_FRAME_RELATED_P (tmp) = 1;
f676971a
EC
12023 XVECEXP (dwarf, 0, 1) = tmp;
12024
b111229a 12025 for (i = 1; i < count; i++)
2c849145
JM
12026 {
12027 reg = gen_rtx_REG (XFmode, base_reg++);
12028 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
12029
f676971a 12030 tmp = gen_rtx_SET (VOIDmode,
31fa16b6
RE
12031 gen_frame_mem (XFmode,
12032 plus_constant (stack_pointer_rtx,
12033 i * 12)),
2c849145
JM
12034 reg);
12035 RTX_FRAME_RELATED_P (tmp) = 1;
f676971a 12036 XVECEXP (dwarf, 0, i + 1) = tmp;
2c849145 12037 }
b111229a 12038
8ee6eb4e
PB
12039 tmp = gen_rtx_SET (VOIDmode,
12040 stack_pointer_rtx,
d66437c5
RE
12041 plus_constant (stack_pointer_rtx, -12 * count));
12042
8ee6eb4e
PB
12043 RTX_FRAME_RELATED_P (tmp) = 1;
12044 XVECEXP (dwarf, 0, 0) = tmp;
12045
2c849145
JM
12046 par = emit_insn (par);
12047 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
12048 REG_NOTES (par));
12049 return par;
e2c671ba
RE
12050}
12051
9b66ebb1 12052
3c7ad43e
PB
12053/* Return true if the current function needs to save/restore LR. */
12054
12055static bool
12056thumb_force_lr_save (void)
12057{
12058 return !cfun->machine->lr_save_eliminated
12059 && (!leaf_function_p ()
12060 || thumb_far_jump_used_p ()
6fb5fa3c 12061 || df_regs_ever_live_p (LR_REGNUM));
3c7ad43e
PB
12062}
12063
12064
095bb276
NC
12065/* Compute the distance from register FROM to register TO.
12066 These can be the arg pointer (26), the soft frame pointer (25),
12067 the stack pointer (13) or the hard frame pointer (11).
c9ca9b88 12068 In thumb mode r7 is used as the soft frame pointer, if needed.
095bb276
NC
12069 Typical stack layout looks like this:
12070
12071 old stack pointer -> | |
12072 ----
12073 | | \
12074 | | saved arguments for
12075 | | vararg functions
12076 | | /
12077 --
12078 hard FP & arg pointer -> | | \
12079 | | stack
12080 | | frame
12081 | | /
12082 --
12083 | | \
12084 | | call saved
12085 | | registers
12086 soft frame pointer -> | | /
12087 --
12088 | | \
12089 | | local
12090 | | variables
2591db65 12091 locals base pointer -> | | /
095bb276
NC
12092 --
12093 | | \
12094 | | outgoing
12095 | | arguments
12096 current stack pointer -> | | /
12097 --
12098
43aa4e05 12099 For a given function some or all of these stack components
095bb276
NC
12100 may not be needed, giving rise to the possibility of
12101 eliminating some of the registers.
12102
825dda42 12103 The values returned by this function must reflect the behavior
095bb276
NC
12104 of arm_expand_prologue() and arm_compute_save_reg_mask().
12105
12106 The sign of the number returned reflects the direction of stack
12107 growth, so the values are positive for all eliminations except
5848830f
PB
12108 from the soft frame pointer to the hard frame pointer.
12109
12110 SFP may point just inside the local variables block to ensure correct
12111 alignment. */
12112
12113
12114/* Calculate stack offsets. These are used to calculate register elimination
954954d1
PB
12115 offsets and in prologue/epilogue code. Also calculates which registers
12116 should be saved. */
5848830f
PB
12117
12118static arm_stack_offsets *
12119arm_get_frame_offsets (void)
095bb276 12120{
5848830f 12121 struct arm_stack_offsets *offsets;
095bb276 12122 unsigned long func_type;
5848830f 12123 int leaf;
5848830f 12124 int saved;
954954d1 12125 int core_saved;
5848830f 12126 HOST_WIDE_INT frame_size;
954954d1 12127 int i;
5848830f
PB
12128
12129 offsets = &cfun->machine->stack_offsets;
f676971a 12130
5848830f
PB
12131 /* We need to know if we are a leaf function. Unfortunately, it
12132 is possible to be called after start_sequence has been called,
12133 which causes get_insns to return the insns for the sequence,
12134 not the function, which will cause leaf_function_p to return
12135 the incorrect result.
095bb276 12136
5848830f
PB
12137 to know about leaf functions once reload has completed, and the
12138 frame size cannot be changed after that time, so we can safely
12139 use the cached value. */
12140
12141 if (reload_completed)
12142 return offsets;
12143
666c27b9
KH
12144 /* Initially this is the size of the local variables. It will translated
12145 into an offset once we have determined the size of preceding data. */
5848830f
PB
12146 frame_size = ROUND_UP_WORD (get_frame_size ());
12147
12148 leaf = leaf_function_p ();
12149
12150 /* Space for variadic functions. */
38173d38 12151 offsets->saved_args = crtl->args.pretend_args_size;
5848830f 12152
5b3e6663 12153 /* In Thumb mode this is incorrect, but never used. */
35596784
AJ
12154 offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0) +
12155 arm_compute_static_chain_stack_bytes();
5848830f 12156
5b3e6663 12157 if (TARGET_32BIT)
095bb276 12158 {
5848830f 12159 unsigned int regno;
ef7112de 12160
954954d1
PB
12161 offsets->saved_regs_mask = arm_compute_save_reg_mask ();
12162 core_saved = bit_count (offsets->saved_regs_mask) * 4;
12163 saved = core_saved;
5a9335ef 12164
5848830f
PB
12165 /* We know that SP will be doubleword aligned on entry, and we must
12166 preserve that condition at any subroutine call. We also require the
12167 soft frame pointer to be doubleword aligned. */
12168
12169 if (TARGET_REALLY_IWMMXT)
9b66ebb1 12170 {
5848830f
PB
12171 /* Check for the call-saved iWMMXt registers. */
12172 for (regno = FIRST_IWMMXT_REGNUM;
12173 regno <= LAST_IWMMXT_REGNUM;
12174 regno++)
6fb5fa3c 12175 if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
5848830f
PB
12176 saved += 8;
12177 }
12178
12179 func_type = arm_current_func_type ();
12180 if (! IS_VOLATILE (func_type))
12181 {
12182 /* Space for saved FPA registers. */
12183 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
6fb5fa3c 12184 if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
5848830f
PB
12185 saved += 12;
12186
12187 /* Space for saved VFP registers. */
12188 if (TARGET_HARD_FLOAT && TARGET_VFP)
9728c9d1 12189 saved += arm_get_vfp_saved_size ();
9b66ebb1 12190 }
5848830f 12191 }
5b3e6663 12192 else /* TARGET_THUMB1 */
5848830f 12193 {
954954d1
PB
12194 offsets->saved_regs_mask = thumb1_compute_save_reg_mask ();
12195 core_saved = bit_count (offsets->saved_regs_mask) * 4;
12196 saved = core_saved;
5848830f 12197 if (TARGET_BACKTRACE)
57934c39 12198 saved += 16;
5848830f 12199 }
9b66ebb1 12200
5848830f 12201 /* Saved registers include the stack frame. */
35596784
AJ
12202 offsets->saved_regs = offsets->saved_args + saved +
12203 arm_compute_static_chain_stack_bytes();
a2503645 12204 offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
5848830f
PB
12205 /* A leaf function does not need any stack alignment if it has nothing
12206 on the stack. */
12207 if (leaf && frame_size == 0)
12208 {
12209 offsets->outgoing_args = offsets->soft_frame;
a3a531ec 12210 offsets->locals_base = offsets->soft_frame;
5848830f
PB
12211 return offsets;
12212 }
12213
12214 /* Ensure SFP has the correct alignment. */
12215 if (ARM_DOUBLEWORD_ALIGN
12216 && (offsets->soft_frame & 7))
954954d1
PB
12217 {
12218 offsets->soft_frame += 4;
12219 /* Try to align stack by pushing an extra reg. Don't bother doing this
12220 when there is a stack frame as the alignment will be rolled into
12221 the normal stack adjustment. */
38173d38 12222 if (frame_size + crtl->outgoing_args_size == 0)
954954d1
PB
12223 {
12224 int reg = -1;
12225
12226 for (i = 4; i <= (TARGET_THUMB1 ? LAST_LO_REGNUM : 11); i++)
12227 {
12228 if ((offsets->saved_regs_mask & (1 << i)) == 0)
12229 {
12230 reg = i;
12231 break;
12232 }
12233 }
12234
12235 if (reg == -1 && arm_size_return_regs () <= 12
e3b5732b 12236 && !crtl->tail_call_emit)
954954d1
PB
12237 {
12238 /* Push/pop an argument register (r3) if all callee saved
12239 registers are already being pushed. */
12240 reg = 3;
12241 }
12242
12243 if (reg != -1)
12244 {
12245 offsets->saved_regs += 4;
12246 offsets->saved_regs_mask |= (1 << reg);
12247 }
12248 }
12249 }
5848830f 12250
2591db65
RE
12251 offsets->locals_base = offsets->soft_frame + frame_size;
12252 offsets->outgoing_args = (offsets->locals_base
38173d38 12253 + crtl->outgoing_args_size);
5848830f
PB
12254
12255 if (ARM_DOUBLEWORD_ALIGN)
12256 {
12257 /* Ensure SP remains doubleword aligned. */
12258 if (offsets->outgoing_args & 7)
12259 offsets->outgoing_args += 4;
e6d29d15 12260 gcc_assert (!(offsets->outgoing_args & 7));
095bb276
NC
12261 }
12262
5848830f
PB
12263 return offsets;
12264}
12265
12266
666c27b9 12267/* Calculate the relative offsets for the different stack pointers. Positive
5848830f
PB
12268 offsets are in the direction of stack growth. */
12269
b3f8d95d 12270HOST_WIDE_INT
5848830f
PB
12271arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
12272{
12273 arm_stack_offsets *offsets;
12274
12275 offsets = arm_get_frame_offsets ();
095bb276 12276
095bb276
NC
12277 /* OK, now we have enough information to compute the distances.
12278 There must be an entry in these switch tables for each pair
12279 of registers in ELIMINABLE_REGS, even if some of the entries
12280 seem to be redundant or useless. */
12281 switch (from)
12282 {
12283 case ARG_POINTER_REGNUM:
12284 switch (to)
12285 {
12286 case THUMB_HARD_FRAME_POINTER_REGNUM:
12287 return 0;
12288
12289 case FRAME_POINTER_REGNUM:
12290 /* This is the reverse of the soft frame pointer
12291 to hard frame pointer elimination below. */
5848830f 12292 return offsets->soft_frame - offsets->saved_args;
095bb276
NC
12293
12294 case ARM_HARD_FRAME_POINTER_REGNUM:
35596784
AJ
12295 /* This is only non-zero in the case where the static chain register
12296 is stored above the frame. */
12297 return offsets->frame - offsets->saved_args - 4;
095bb276
NC
12298
12299 case STACK_POINTER_REGNUM:
12300 /* If nothing has been pushed on the stack at all
12301 then this will return -4. This *is* correct! */
5848830f 12302 return offsets->outgoing_args - (offsets->saved_args + 4);
095bb276
NC
12303
12304 default:
e6d29d15 12305 gcc_unreachable ();
095bb276 12306 }
e6d29d15 12307 gcc_unreachable ();
095bb276
NC
12308
12309 case FRAME_POINTER_REGNUM:
12310 switch (to)
12311 {
12312 case THUMB_HARD_FRAME_POINTER_REGNUM:
12313 return 0;
12314
12315 case ARM_HARD_FRAME_POINTER_REGNUM:
12316 /* The hard frame pointer points to the top entry in the
12317 stack frame. The soft frame pointer to the bottom entry
12318 in the stack frame. If there is no stack frame at all,
12319 then they are identical. */
5848830f
PB
12320
12321 return offsets->frame - offsets->soft_frame;
095bb276
NC
12322
12323 case STACK_POINTER_REGNUM:
5848830f 12324 return offsets->outgoing_args - offsets->soft_frame;
095bb276
NC
12325
12326 default:
e6d29d15 12327 gcc_unreachable ();
095bb276 12328 }
e6d29d15 12329 gcc_unreachable ();
095bb276
NC
12330
12331 default:
12332 /* You cannot eliminate from the stack pointer.
12333 In theory you could eliminate from the hard frame
12334 pointer to the stack pointer, but this will never
12335 happen, since if a stack frame is not needed the
12336 hard frame pointer will never be used. */
e6d29d15 12337 gcc_unreachable ();
095bb276
NC
12338 }
12339}
12340
0977774b 12341
7a085dce 12342/* Emit RTL to save coprocessor registers on function entry. Returns the
5b3e6663
PB
12343 number of bytes pushed. */
12344
12345static int
12346arm_save_coproc_regs(void)
12347{
12348 int saved_size = 0;
12349 unsigned reg;
12350 unsigned start_reg;
12351 rtx insn;
12352
12353 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
6fb5fa3c 12354 if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
5b3e6663
PB
12355 {
12356 insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
12357 insn = gen_rtx_MEM (V2SImode, insn);
12358 insn = emit_set_insn (insn, gen_rtx_REG (V2SImode, reg));
12359 RTX_FRAME_RELATED_P (insn) = 1;
12360 saved_size += 8;
12361 }
12362
12363 /* Save any floating point call-saved registers used by this
12364 function. */
12365 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
12366 {
12367 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
6fb5fa3c 12368 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
5b3e6663
PB
12369 {
12370 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
12371 insn = gen_rtx_MEM (XFmode, insn);
12372 insn = emit_set_insn (insn, gen_rtx_REG (XFmode, reg));
12373 RTX_FRAME_RELATED_P (insn) = 1;
12374 saved_size += 12;
12375 }
12376 }
12377 else
12378 {
12379 start_reg = LAST_FPA_REGNUM;
12380
12381 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
12382 {
6fb5fa3c 12383 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
5b3e6663
PB
12384 {
12385 if (start_reg - reg == 3)
12386 {
12387 insn = emit_sfm (reg, 4);
12388 RTX_FRAME_RELATED_P (insn) = 1;
12389 saved_size += 48;
12390 start_reg = reg - 1;
12391 }
12392 }
12393 else
12394 {
12395 if (start_reg != reg)
12396 {
12397 insn = emit_sfm (reg + 1, start_reg - reg);
12398 RTX_FRAME_RELATED_P (insn) = 1;
12399 saved_size += (start_reg - reg) * 12;
12400 }
12401 start_reg = reg - 1;
12402 }
12403 }
12404
12405 if (start_reg != reg)
12406 {
12407 insn = emit_sfm (reg + 1, start_reg - reg);
12408 saved_size += (start_reg - reg) * 12;
12409 RTX_FRAME_RELATED_P (insn) = 1;
12410 }
12411 }
12412 if (TARGET_HARD_FLOAT && TARGET_VFP)
12413 {
12414 start_reg = FIRST_VFP_REGNUM;
12415
12416 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
12417 {
6fb5fa3c
DB
12418 if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
12419 && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
5b3e6663
PB
12420 {
12421 if (start_reg != reg)
12422 saved_size += vfp_emit_fstmd (start_reg,
12423 (reg - start_reg) / 2);
12424 start_reg = reg + 2;
12425 }
12426 }
12427 if (start_reg != reg)
12428 saved_size += vfp_emit_fstmd (start_reg,
12429 (reg - start_reg) / 2);
12430 }
12431 return saved_size;
12432}
12433
12434
12435/* Set the Thumb frame pointer from the stack pointer. */
12436
12437static void
12438thumb_set_frame_pointer (arm_stack_offsets *offsets)
12439{
12440 HOST_WIDE_INT amount;
12441 rtx insn, dwarf;
12442
12443 amount = offsets->outgoing_args - offsets->locals_base;
12444 if (amount < 1024)
12445 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
12446 stack_pointer_rtx, GEN_INT (amount)));
12447 else
12448 {
12449 emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
f5c630c3
PB
12450 /* Thumb-2 RTL patterns expect sp as the first input. Thumb-1
12451 expects the first two operands to be the same. */
12452 if (TARGET_THUMB2)
12453 {
12454 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
12455 stack_pointer_rtx,
12456 hard_frame_pointer_rtx));
12457 }
12458 else
12459 {
12460 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
12461 hard_frame_pointer_rtx,
12462 stack_pointer_rtx));
12463 }
5b3e6663
PB
12464 dwarf = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
12465 plus_constant (stack_pointer_rtx, amount));
12466 RTX_FRAME_RELATED_P (dwarf) = 1;
12467 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
12468 REG_NOTES (insn));
12469 }
12470
12471 RTX_FRAME_RELATED_P (insn) = 1;
12472}
12473
12474/* Generate the prologue instructions for entry into an ARM or Thumb-2
12475 function. */
e2c671ba 12476void
e32bac5b 12477arm_expand_prologue (void)
e2c671ba 12478{
6d3d9133 12479 rtx amount;
2c849145 12480 rtx insn;
68dfd979 12481 rtx ip_rtx;
6d3d9133
NC
12482 unsigned long live_regs_mask;
12483 unsigned long func_type;
68dfd979 12484 int fp_offset = 0;
095bb276 12485 int saved_pretend_args = 0;
5848830f 12486 int saved_regs = 0;
b9705a21 12487 unsigned HOST_WIDE_INT args_to_push;
5848830f 12488 arm_stack_offsets *offsets;
d3236b4d 12489
6d3d9133 12490 func_type = arm_current_func_type ();
e2c671ba 12491
31fdb4d5 12492 /* Naked functions don't have prologues. */
6d3d9133 12493 if (IS_NAKED (func_type))
31fdb4d5
DE
12494 return;
12495
095bb276 12496 /* Make a copy of c_f_p_a_s as we may need to modify it locally. */
38173d38 12497 args_to_push = crtl->args.pretend_args_size;
f676971a 12498
6d3d9133 12499 /* Compute which register we will have to save onto the stack. */
954954d1
PB
12500 offsets = arm_get_frame_offsets ();
12501 live_regs_mask = offsets->saved_regs_mask;
e2c671ba 12502
68dfd979 12503 ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
d3236b4d 12504
5b3e6663
PB
12505 if (IS_STACKALIGN (func_type))
12506 {
12507 rtx dwarf;
12508 rtx r0;
12509 rtx r1;
12510 /* Handle a word-aligned stack pointer. We generate the following:
12511
12512 mov r0, sp
12513 bic r1, r0, #7
12514 mov sp, r1
12515 <save and restore r0 in normal prologue/epilogue>
12516 mov sp, r0
12517 bx lr
12518
12519 The unwinder doesn't need to know about the stack realignment.
12520 Just tell it we saved SP in r0. */
12521 gcc_assert (TARGET_THUMB2 && !arm_arch_notm && args_to_push == 0);
12522
12523 r0 = gen_rtx_REG (SImode, 0);
12524 r1 = gen_rtx_REG (SImode, 1);
44bfa35b
NF
12525 /* Use a real rtvec rather than NULL_RTVEC so the rest of the
12526 compiler won't choke. */
12527 dwarf = gen_rtx_UNSPEC (SImode, rtvec_alloc (0), UNSPEC_STACK_ALIGN);
5b3e6663
PB
12528 dwarf = gen_rtx_SET (VOIDmode, r0, dwarf);
12529 insn = gen_movsi (r0, stack_pointer_rtx);
12530 RTX_FRAME_RELATED_P (insn) = 1;
12531 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
12532 dwarf, REG_NOTES (insn));
12533 emit_insn (insn);
12534 emit_insn (gen_andsi3 (r1, r0, GEN_INT (~(HOST_WIDE_INT)7)));
12535 emit_insn (gen_movsi (stack_pointer_rtx, r1));
12536 }
12537
ec6237e4
PB
12538 /* For APCS frames, if IP register is clobbered
12539 when creating frame, save that register in a special
12540 way. */
12541 if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
e2c671ba 12542 {
7b8b8ade
NC
12543 if (IS_INTERRUPT (func_type))
12544 {
12545 /* Interrupt functions must not corrupt any registers.
12546 Creating a frame pointer however, corrupts the IP
12547 register, so we must push it first. */
12548 insn = emit_multi_reg_push (1 << IP_REGNUM);
121308d4
NC
12549
12550 /* Do not set RTX_FRAME_RELATED_P on this insn.
12551 The dwarf stack unwinding code only wants to see one
12552 stack decrement per function, and this is not it. If
12553 this instruction is labeled as being part of the frame
12554 creation sequence then dwarf2out_frame_debug_expr will
e6d29d15 12555 die when it encounters the assignment of IP to FP
121308d4
NC
12556 later on, since the use of SP here establishes SP as
12557 the CFA register and not IP.
12558
12559 Anyway this instruction is not really part of the stack
12560 frame creation although it is part of the prologue. */
7b8b8ade
NC
12561 }
12562 else if (IS_NESTED (func_type))
68dfd979
NC
12563 {
12564 /* The Static chain register is the same as the IP register
12565 used as a scratch register during stack frame creation.
12566 To get around this need to find somewhere to store IP
12567 whilst the frame is being created. We try the following
12568 places in order:
f676971a 12569
6d3d9133 12570 1. The last argument register.
68dfd979
NC
12571 2. A slot on the stack above the frame. (This only
12572 works if the function is not a varargs function).
095bb276
NC
12573 3. Register r3, after pushing the argument registers
12574 onto the stack.
6d3d9133 12575
34ce3d7b
JM
12576 Note - we only need to tell the dwarf2 backend about the SP
12577 adjustment in the second variant; the static chain register
12578 doesn't need to be unwound, as it doesn't contain a value
12579 inherited from the caller. */
d3236b4d 12580
6fb5fa3c 12581 if (df_regs_ever_live_p (3) == false)
d66437c5 12582 insn = emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
095bb276 12583 else if (args_to_push == 0)
68dfd979 12584 {
35596784
AJ
12585 gcc_assert(arm_compute_static_chain_stack_bytes() == 4);
12586 saved_regs += 4;
12587
34ce3d7b 12588 rtx dwarf;
34ce3d7b 12589
d66437c5
RE
12590 insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
12591 insn = emit_set_insn (gen_frame_mem (SImode, insn), ip_rtx);
68dfd979 12592 fp_offset = 4;
34ce3d7b
JM
12593
12594 /* Just tell the dwarf backend that we adjusted SP. */
12595 dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
d66437c5
RE
12596 plus_constant (stack_pointer_rtx,
12597 -fp_offset));
34ce3d7b
JM
12598 RTX_FRAME_RELATED_P (insn) = 1;
12599 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
12600 dwarf, REG_NOTES (insn));
68dfd979
NC
12601 }
12602 else
095bb276
NC
12603 {
12604 /* Store the args on the stack. */
3cb66fd7 12605 if (cfun->machine->uses_anonymous_args)
095bb276
NC
12606 insn = emit_multi_reg_push
12607 ((0xf0 >> (args_to_push / 4)) & 0xf);
12608 else
12609 insn = emit_insn
f676971a 12610 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
095bb276
NC
12611 GEN_INT (- args_to_push)));
12612
12613 RTX_FRAME_RELATED_P (insn) = 1;
12614
12615 saved_pretend_args = 1;
12616 fp_offset = args_to_push;
12617 args_to_push = 0;
12618
12619 /* Now reuse r3 to preserve IP. */
d66437c5 12620 emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
095bb276 12621 }
68dfd979
NC
12622 }
12623
d66437c5
RE
12624 insn = emit_set_insn (ip_rtx,
12625 plus_constant (stack_pointer_rtx, fp_offset));
8e56560e 12626 RTX_FRAME_RELATED_P (insn) = 1;
e2c671ba
RE
12627 }
12628
095bb276 12629 if (args_to_push)
e2c671ba 12630 {
6d3d9133 12631 /* Push the argument registers, or reserve space for them. */
3cb66fd7 12632 if (cfun->machine->uses_anonymous_args)
2c849145 12633 insn = emit_multi_reg_push
095bb276 12634 ((0xf0 >> (args_to_push / 4)) & 0xf);
e2c671ba 12635 else
2c849145 12636 insn = emit_insn
f676971a 12637 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
095bb276 12638 GEN_INT (- args_to_push)));
2c849145 12639 RTX_FRAME_RELATED_P (insn) = 1;
e2c671ba
RE
12640 }
12641
06bea5aa 12642 /* If this is an interrupt service routine, and the link register
ec6237e4
PB
12643 is going to be pushed, and we're not generating extra
12644 push of IP (needed when frame is needed and frame layout if apcs),
06bea5aa
NC
12645 subtracting four from LR now will mean that the function return
12646 can be done with a single instruction. */
3a7731fd 12647 if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
06bea5aa 12648 && (live_regs_mask & (1 << LR_REGNUM)) != 0
ec6237e4 12649 && !(frame_pointer_needed && TARGET_APCS_FRAME)
a15908a4 12650 && TARGET_ARM)
d66437c5
RE
12651 {
12652 rtx lr = gen_rtx_REG (SImode, LR_REGNUM);
12653
12654 emit_set_insn (lr, plus_constant (lr, -4));
12655 }
3a7731fd 12656
e2c671ba
RE
12657 if (live_regs_mask)
12658 {
5848830f 12659 saved_regs += bit_count (live_regs_mask) * 4;
954954d1
PB
12660 if (optimize_size && !frame_pointer_needed
12661 && saved_regs == offsets->saved_regs - offsets->saved_args)
12662 {
12663 /* If no coprocessor registers are being pushed and we don't have
12664 to worry about a frame pointer then push extra registers to
12665 create the stack frame. This is done is a way that does not
12666 alter the frame layout, so is independent of the epilogue. */
12667 int n;
12668 int frame;
12669 n = 0;
12670 while (n < 8 && (live_regs_mask & (1 << n)) == 0)
12671 n++;
12672 frame = offsets->outgoing_args - (offsets->saved_args + saved_regs);
12673 if (frame && n * 4 >= frame)
12674 {
12675 n = frame / 4;
12676 live_regs_mask |= (1 << n) - 1;
12677 saved_regs += frame;
12678 }
12679 }
12680 insn = emit_multi_reg_push (live_regs_mask);
2c849145 12681 RTX_FRAME_RELATED_P (insn) = 1;
e2c671ba 12682 }
d5b7b3ae 12683
6d3d9133 12684 if (! IS_VOLATILE (func_type))
5b3e6663 12685 saved_regs += arm_save_coproc_regs ();
b111229a 12686
5b3e6663
PB
12687 if (frame_pointer_needed && TARGET_ARM)
12688 {
12689 /* Create the new frame pointer. */
ec6237e4 12690 if (TARGET_APCS_FRAME)
9b66ebb1 12691 {
5b3e6663
PB
12692 insn = GEN_INT (-(4 + args_to_push + fp_offset));
12693 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
12694 RTX_FRAME_RELATED_P (insn) = 1;
9b66ebb1 12695
5b3e6663 12696 if (IS_NESTED (func_type))
9b66ebb1 12697 {
5b3e6663 12698 /* Recover the static chain register. */
6fb5fa3c 12699 if (!df_regs_ever_live_p (3)
5b3e6663
PB
12700 || saved_pretend_args)
12701 insn = gen_rtx_REG (SImode, 3);
38173d38 12702 else /* if (crtl->args.pretend_args_size == 0) */
9b66ebb1 12703 {
5b3e6663
PB
12704 insn = plus_constant (hard_frame_pointer_rtx, 4);
12705 insn = gen_frame_mem (SImode, insn);
9b66ebb1 12706 }
5b3e6663
PB
12707 emit_set_insn (ip_rtx, insn);
12708 /* Add a USE to stop propagate_one_insn() from barfing. */
12709 emit_insn (gen_prologue_use (ip_rtx));
9b66ebb1 12710 }
68dfd979 12711 }
ec6237e4
PB
12712 else
12713 {
12714 insn = GEN_INT (saved_regs - 4);
12715 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
12716 stack_pointer_rtx, insn));
12717 RTX_FRAME_RELATED_P (insn) = 1;
12718 }
2c849145 12719 }
e2c671ba 12720
5848830f 12721 if (offsets->outgoing_args != offsets->saved_args + saved_regs)
e2c671ba 12722 {
745b9093
JM
12723 /* This add can produce multiple insns for a large constant, so we
12724 need to get tricky. */
12725 rtx last = get_last_insn ();
5848830f
PB
12726
12727 amount = GEN_INT (offsets->saved_args + saved_regs
12728 - offsets->outgoing_args);
12729
2c849145
JM
12730 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
12731 amount));
745b9093
JM
12732 do
12733 {
12734 last = last ? NEXT_INSN (last) : get_insns ();
12735 RTX_FRAME_RELATED_P (last) = 1;
12736 }
12737 while (last != insn);
e04c2d6c
RE
12738
12739 /* If the frame pointer is needed, emit a special barrier that
12740 will prevent the scheduler from moving stores to the frame
12741 before the stack adjustment. */
12742 if (frame_pointer_needed)
3894f59e
RE
12743 insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
12744 hard_frame_pointer_rtx));
e2c671ba
RE
12745 }
12746
876f13b0 12747
5b3e6663
PB
12748 if (frame_pointer_needed && TARGET_THUMB2)
12749 thumb_set_frame_pointer (offsets);
12750
020a4035 12751 if (flag_pic && arm_pic_register != INVALID_REGNUM)
5b3e6663
PB
12752 {
12753 unsigned long mask;
12754
12755 mask = live_regs_mask;
12756 mask &= THUMB2_WORK_REGS;
12757 if (!IS_NESTED (func_type))
12758 mask |= (1 << IP_REGNUM);
12759 arm_load_pic_register (mask);
12760 }
876f13b0 12761
e2c671ba 12762 /* If we are profiling, make sure no instructions are scheduled before
f5a1b0d2 12763 the call to mcount. Similarly if the user has requested no
74d9c39f
DJ
12764 scheduling in the prolog. Similarly if we want non-call exceptions
12765 using the EABI unwinder, to prevent faulting instructions from being
12766 swapped with a stack adjustment. */
e3b5732b 12767 if (crtl->profile || !TARGET_SCHED_PROLOG
74d9c39f 12768 || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
e2c671ba 12769 emit_insn (gen_blockage ());
6f7ebcbb
NC
12770
12771 /* If the link register is being kept alive, with the return address in it,
12772 then make sure that it does not get reused by the ce2 pass. */
12773 if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
6fb5fa3c 12774 cfun->machine->lr_save_eliminated = 1;
e2c671ba 12775}
cce8749e 12776\f
5b3e6663
PB
12777/* Print condition code to STREAM. Helper function for arm_print_operand. */
12778static void
12779arm_print_condition (FILE *stream)
12780{
12781 if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
12782 {
12783 /* Branch conversion is not implemented for Thumb-2. */
12784 if (TARGET_THUMB)
12785 {
12786 output_operand_lossage ("predicated Thumb instruction");
12787 return;
12788 }
12789 if (current_insn_predicate != NULL)
12790 {
12791 output_operand_lossage
12792 ("predicated instruction in conditional sequence");
12793 return;
12794 }
12795
12796 fputs (arm_condition_codes[arm_current_cc], stream);
12797 }
12798 else if (current_insn_predicate)
12799 {
12800 enum arm_cond_code code;
12801
12802 if (TARGET_THUMB1)
12803 {
12804 output_operand_lossage ("predicated Thumb instruction");
12805 return;
12806 }
12807
12808 code = get_arm_condition_code (current_insn_predicate);
12809 fputs (arm_condition_codes[code], stream);
12810 }
12811}
12812
12813
9997d19d
RE
12814/* If CODE is 'd', then the X is a condition operand and the instruction
12815 should only be executed if the condition is true.
ddd5a7c1 12816 if CODE is 'D', then the X is a condition operand and the instruction
9997d19d
RE
12817 should only be executed if the condition is false: however, if the mode
12818 of the comparison is CCFPEmode, then always execute the instruction -- we
12819 do this because in these circumstances !GE does not necessarily imply LT;
12820 in these cases the instruction pattern will take care to make sure that
12821 an instruction containing %d will follow, thereby undoing the effects of
ddd5a7c1 12822 doing this instruction unconditionally.
9997d19d
RE
12823 If CODE is 'N' then X is a floating point operand that must be negated
12824 before output.
12825 If CODE is 'B' then output a bitwise inverted value of X (a const int).
12826 If X is a REG and CODE is `M', output a ldm/stm style multi-reg. */
9997d19d 12827void
e32bac5b 12828arm_print_operand (FILE *stream, rtx x, int code)
9997d19d
RE
12829{
12830 switch (code)
12831 {
12832 case '@':
f3139301 12833 fputs (ASM_COMMENT_START, stream);
9997d19d
RE
12834 return;
12835
d5b7b3ae
RE
12836 case '_':
12837 fputs (user_label_prefix, stream);
12838 return;
f676971a 12839
9997d19d 12840 case '|':
f3139301 12841 fputs (REGISTER_PREFIX, stream);
9997d19d
RE
12842 return;
12843
12844 case '?':
5b3e6663
PB
12845 arm_print_condition (stream);
12846 return;
cca0a211 12847
5b3e6663
PB
12848 case '(':
12849 /* Nothing in unified syntax, otherwise the current condition code. */
12850 if (!TARGET_UNIFIED_ASM)
12851 arm_print_condition (stream);
12852 break;
12853
12854 case ')':
12855 /* The current condition code in unified syntax, otherwise nothing. */
12856 if (TARGET_UNIFIED_ASM)
12857 arm_print_condition (stream);
12858 break;
12859
12860 case '.':
12861 /* The current condition code for a condition code setting instruction.
7a085dce 12862 Preceded by 's' in unified syntax, otherwise followed by 's'. */
5b3e6663
PB
12863 if (TARGET_UNIFIED_ASM)
12864 {
12865 fputc('s', stream);
12866 arm_print_condition (stream);
cca0a211 12867 }
5b3e6663 12868 else
cca0a211 12869 {
5b3e6663
PB
12870 arm_print_condition (stream);
12871 fputc('s', stream);
cca0a211 12872 }
9997d19d
RE
12873 return;
12874
5b3e6663
PB
12875 case '!':
12876 /* If the instruction is conditionally executed then print
12877 the current condition code, otherwise print 's'. */
12878 gcc_assert (TARGET_THUMB2 && TARGET_UNIFIED_ASM);
12879 if (current_insn_predicate)
12880 arm_print_condition (stream);
12881 else
12882 fputc('s', stream);
12883 break;
12884
88f77cba 12885 /* %# is a "break" sequence. It doesn't output anything, but is used to
cea618ac 12886 separate e.g. operand numbers from following text, if that text consists
88f77cba
JB
12887 of further digits which we don't want to be part of the operand
12888 number. */
12889 case '#':
12890 return;
12891
9997d19d
RE
12892 case 'N':
12893 {
12894 REAL_VALUE_TYPE r;
12895 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
12896 r = REAL_VALUE_NEGATE (r);
12897 fprintf (stream, "%s", fp_const_from_val (&r));
12898 }
12899 return;
12900
88f77cba
JB
12901 /* An integer without a preceding # sign. */
12902 case 'c':
12903 gcc_assert (GET_CODE (x) == CONST_INT);
12904 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
12905 return;
12906
9997d19d
RE
12907 case 'B':
12908 if (GET_CODE (x) == CONST_INT)
4bc74ece
NC
12909 {
12910 HOST_WIDE_INT val;
5895f793 12911 val = ARM_SIGN_EXTEND (~INTVAL (x));
36ba9cb8 12912 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
4bc74ece 12913 }
9997d19d
RE
12914 else
12915 {
12916 putc ('~', stream);
12917 output_addr_const (stream, x);
12918 }
12919 return;
12920
5b3e6663
PB
12921 case 'L':
12922 /* The low 16 bits of an immediate constant. */
12923 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL(x) & 0xffff);
12924 return;
12925
9997d19d
RE
12926 case 'i':
12927 fprintf (stream, "%s", arithmetic_instr (x, 1));
12928 return;
12929
9b6b54e2
NC
12930 /* Truncate Cirrus shift counts. */
12931 case 's':
12932 if (GET_CODE (x) == CONST_INT)
12933 {
12934 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
12935 return;
12936 }
12937 arm_print_operand (stream, x, 0);
12938 return;
12939
9997d19d
RE
12940 case 'I':
12941 fprintf (stream, "%s", arithmetic_instr (x, 0));
12942 return;
12943
12944 case 'S':
12945 {
12946 HOST_WIDE_INT val;
beed32b8
RE
12947 const char *shift;
12948
12949 if (!shift_operator (x, SImode))
12950 {
12951 output_operand_lossage ("invalid shift operand");
12952 break;
12953 }
12954
12955 shift = shift_op (x, &val);
9997d19d 12956
e2c671ba
RE
12957 if (shift)
12958 {
beed32b8 12959 fprintf (stream, ", %s ", shift);
e2c671ba
RE
12960 if (val == -1)
12961 arm_print_operand (stream, XEXP (x, 1), 0);
12962 else
4a0a75dd 12963 fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
e2c671ba 12964 }
9997d19d
RE
12965 }
12966 return;
12967
d5b7b3ae 12968 /* An explanation of the 'Q', 'R' and 'H' register operands:
f676971a 12969
d5b7b3ae
RE
12970 In a pair of registers containing a DI or DF value the 'Q'
12971 operand returns the register number of the register containing
093354e0 12972 the least significant part of the value. The 'R' operand returns
d5b7b3ae
RE
12973 the register number of the register containing the most
12974 significant part of the value.
f676971a 12975
d5b7b3ae
RE
12976 The 'H' operand returns the higher of the two register numbers.
12977 On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
093354e0 12978 same as the 'Q' operand, since the most significant part of the
d5b7b3ae
RE
12979 value is held in the lower number register. The reverse is true
12980 on systems where WORDS_BIG_ENDIAN is false.
f676971a 12981
d5b7b3ae
RE
12982 The purpose of these operands is to distinguish between cases
12983 where the endian-ness of the values is important (for example
12984 when they are added together), and cases where the endian-ness
12985 is irrelevant, but the order of register operations is important.
12986 For example when loading a value from memory into a register
12987 pair, the endian-ness does not matter. Provided that the value
12988 from the lower memory address is put into the lower numbered
12989 register, and the value from the higher address is put into the
12990 higher numbered register, the load will work regardless of whether
12991 the value being loaded is big-wordian or little-wordian. The
12992 order of the two register loads can matter however, if the address
12993 of the memory location is actually held in one of the registers
12994 being overwritten by the load. */
c1c2bc04 12995 case 'Q':
22de4c3d
RE
12996 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
12997 {
12998 output_operand_lossage ("invalid operand for code '%c'", code);
12999 return;
13000 }
13001
d5b7b3ae 13002 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
c1c2bc04
RE
13003 return;
13004
9997d19d 13005 case 'R':
22de4c3d
RE
13006 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13007 {
13008 output_operand_lossage ("invalid operand for code '%c'", code);
13009 return;
13010 }
13011
d5b7b3ae
RE
13012 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
13013 return;
13014
13015 case 'H':
22de4c3d
RE
13016 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13017 {
13018 output_operand_lossage ("invalid operand for code '%c'", code);
13019 return;
13020 }
13021
d5b7b3ae 13022 asm_fprintf (stream, "%r", REGNO (x) + 1);
9997d19d
RE
13023 return;
13024
88f77cba
JB
13025 case 'J':
13026 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13027 {
13028 output_operand_lossage ("invalid operand for code '%c'", code);
13029 return;
13030 }
13031
13032 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 3 : 2));
13033 return;
13034
13035 case 'K':
13036 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13037 {
13038 output_operand_lossage ("invalid operand for code '%c'", code);
13039 return;
13040 }
13041
13042 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 2 : 3));
13043 return;
13044
9997d19d 13045 case 'm':
f676971a 13046 asm_fprintf (stream, "%r",
d5b7b3ae
RE
13047 GET_CODE (XEXP (x, 0)) == REG
13048 ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
9997d19d
RE
13049 return;
13050
13051 case 'M':
dd18ae56 13052 asm_fprintf (stream, "{%r-%r}",
d5b7b3ae 13053 REGNO (x),
e9d7b180 13054 REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
9997d19d
RE
13055 return;
13056
88f77cba
JB
13057 /* Like 'M', but writing doubleword vector registers, for use by Neon
13058 insns. */
13059 case 'h':
13060 {
13061 int regno = (REGNO (x) - FIRST_VFP_REGNUM) / 2;
13062 int numregs = ARM_NUM_REGS (GET_MODE (x)) / 2;
13063 if (numregs == 1)
13064 asm_fprintf (stream, "{d%d}", regno);
13065 else
13066 asm_fprintf (stream, "{d%d-d%d}", regno, regno + numregs - 1);
13067 }
13068 return;
13069
9997d19d 13070 case 'd':
64e92a26
RE
13071 /* CONST_TRUE_RTX means always -- that's the default. */
13072 if (x == const_true_rtx)
d5b7b3ae 13073 return;
f676971a 13074
22de4c3d
RE
13075 if (!COMPARISON_P (x))
13076 {
13077 output_operand_lossage ("invalid operand for code '%c'", code);
13078 return;
13079 }
13080
defc0463
RE
13081 fputs (arm_condition_codes[get_arm_condition_code (x)],
13082 stream);
9997d19d
RE
13083 return;
13084
13085 case 'D':
112cdef5 13086 /* CONST_TRUE_RTX means not always -- i.e. never. We shouldn't ever
64e92a26
RE
13087 want to do that. */
13088 if (x == const_true_rtx)
22de4c3d 13089 {
4dad0aca 13090 output_operand_lossage ("instruction never executed");
22de4c3d
RE
13091 return;
13092 }
13093 if (!COMPARISON_P (x))
13094 {
13095 output_operand_lossage ("invalid operand for code '%c'", code);
13096 return;
13097 }
d5b7b3ae 13098
defc0463
RE
13099 fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
13100 (get_arm_condition_code (x))],
13101 stream);
9997d19d
RE
13102 return;
13103
9b6b54e2
NC
13104 /* Cirrus registers can be accessed in a variety of ways:
13105 single floating point (f)
13106 double floating point (d)
13107 32bit integer (fx)
13108 64bit integer (dx). */
13109 case 'W': /* Cirrus register in F mode. */
13110 case 'X': /* Cirrus register in D mode. */
13111 case 'Y': /* Cirrus register in FX mode. */
13112 case 'Z': /* Cirrus register in DX mode. */
e6d29d15
NS
13113 gcc_assert (GET_CODE (x) == REG
13114 && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
9b6b54e2
NC
13115
13116 fprintf (stream, "mv%s%s",
13117 code == 'W' ? "f"
13118 : code == 'X' ? "d"
13119 : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
13120
13121 return;
13122
13123 /* Print cirrus register in the mode specified by the register's mode. */
13124 case 'V':
13125 {
13126 int mode = GET_MODE (x);
13127
13128 if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
22de4c3d
RE
13129 {
13130 output_operand_lossage ("invalid operand for code '%c'", code);
13131 return;
13132 }
9b6b54e2
NC
13133
13134 fprintf (stream, "mv%s%s",
13135 mode == DFmode ? "d"
13136 : mode == SImode ? "fx"
13137 : mode == DImode ? "dx"
13138 : "f", reg_names[REGNO (x)] + 2);
13139
13140 return;
13141 }
13142
5a9335ef
NC
13143 case 'U':
13144 if (GET_CODE (x) != REG
13145 || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
13146 || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
13147 /* Bad value for wCG register number. */
22de4c3d
RE
13148 {
13149 output_operand_lossage ("invalid operand for code '%c'", code);
13150 return;
13151 }
13152
5a9335ef
NC
13153 else
13154 fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
13155 return;
13156
13157 /* Print an iWMMXt control register name. */
13158 case 'w':
13159 if (GET_CODE (x) != CONST_INT
13160 || INTVAL (x) < 0
13161 || INTVAL (x) >= 16)
13162 /* Bad value for wC register number. */
22de4c3d
RE
13163 {
13164 output_operand_lossage ("invalid operand for code '%c'", code);
13165 return;
13166 }
13167
5a9335ef
NC
13168 else
13169 {
13170 static const char * wc_reg_names [16] =
13171 {
13172 "wCID", "wCon", "wCSSF", "wCASF",
13173 "wC4", "wC5", "wC6", "wC7",
13174 "wCGR0", "wCGR1", "wCGR2", "wCGR3",
13175 "wC12", "wC13", "wC14", "wC15"
13176 };
f676971a 13177
5a9335ef
NC
13178 fprintf (stream, wc_reg_names [INTVAL (x)]);
13179 }
13180 return;
13181
88f77cba 13182 /* Print a VFP/Neon double precision or quad precision register name. */
9b66ebb1 13183 case 'P':
88f77cba 13184 case 'q':
9b66ebb1
PB
13185 {
13186 int mode = GET_MODE (x);
88f77cba
JB
13187 int is_quad = (code == 'q');
13188 int regno;
9b66ebb1 13189
88f77cba 13190 if (GET_MODE_SIZE (mode) != (is_quad ? 16 : 8))
22de4c3d
RE
13191 {
13192 output_operand_lossage ("invalid operand for code '%c'", code);
13193 return;
13194 }
9b66ebb1
PB
13195
13196 if (GET_CODE (x) != REG
13197 || !IS_VFP_REGNUM (REGNO (x)))
22de4c3d
RE
13198 {
13199 output_operand_lossage ("invalid operand for code '%c'", code);
13200 return;
13201 }
9b66ebb1 13202
88f77cba
JB
13203 regno = REGNO (x);
13204 if ((is_quad && !NEON_REGNO_OK_FOR_QUAD (regno))
13205 || (!is_quad && !VFP_REGNO_OK_FOR_DOUBLE (regno)))
22de4c3d
RE
13206 {
13207 output_operand_lossage ("invalid operand for code '%c'", code);
13208 return;
13209 }
9b66ebb1 13210
88f77cba
JB
13211 fprintf (stream, "%c%d", is_quad ? 'q' : 'd',
13212 (regno - FIRST_VFP_REGNUM) >> (is_quad ? 2 : 1));
13213 }
13214 return;
13215
13216 /* These two codes print the low/high doubleword register of a Neon quad
13217 register, respectively. For pair-structure types, can also print
13218 low/high quadword registers. */
13219 case 'e':
13220 case 'f':
13221 {
13222 int mode = GET_MODE (x);
13223 int regno;
13224
13225 if ((GET_MODE_SIZE (mode) != 16
13226 && GET_MODE_SIZE (mode) != 32) || GET_CODE (x) != REG)
13227 {
13228 output_operand_lossage ("invalid operand for code '%c'", code);
13229 return;
13230 }
13231
13232 regno = REGNO (x);
13233 if (!NEON_REGNO_OK_FOR_QUAD (regno))
13234 {
13235 output_operand_lossage ("invalid operand for code '%c'", code);
13236 return;
13237 }
13238
13239 if (GET_MODE_SIZE (mode) == 16)
13240 fprintf (stream, "d%d", ((regno - FIRST_VFP_REGNUM) >> 1)
13241 + (code == 'f' ? 1 : 0));
13242 else
13243 fprintf (stream, "q%d", ((regno - FIRST_VFP_REGNUM) >> 2)
13244 + (code == 'f' ? 1 : 0));
9b66ebb1
PB
13245 }
13246 return;
13247
f1adb0a9
JB
13248 /* Print a VFPv3 floating-point constant, represented as an integer
13249 index. */
13250 case 'G':
13251 {
13252 int index = vfp3_const_double_index (x);
13253 gcc_assert (index != -1);
13254 fprintf (stream, "%d", index);
13255 }
13256 return;
13257
88f77cba
JB
13258 /* Print bits representing opcode features for Neon.
13259
13260 Bit 0 is 1 for signed, 0 for unsigned. Floats count as signed
13261 and polynomials as unsigned.
13262
13263 Bit 1 is 1 for floats and polynomials, 0 for ordinary integers.
13264
13265 Bit 2 is 1 for rounding functions, 0 otherwise. */
13266
13267 /* Identify the type as 's', 'u', 'p' or 'f'. */
13268 case 'T':
13269 {
13270 HOST_WIDE_INT bits = INTVAL (x);
13271 fputc ("uspf"[bits & 3], stream);
13272 }
13273 return;
13274
13275 /* Likewise, but signed and unsigned integers are both 'i'. */
13276 case 'F':
13277 {
13278 HOST_WIDE_INT bits = INTVAL (x);
13279 fputc ("iipf"[bits & 3], stream);
13280 }
13281 return;
13282
13283 /* As for 'T', but emit 'u' instead of 'p'. */
13284 case 't':
13285 {
13286 HOST_WIDE_INT bits = INTVAL (x);
13287 fputc ("usuf"[bits & 3], stream);
13288 }
13289 return;
13290
13291 /* Bit 2: rounding (vs none). */
13292 case 'O':
13293 {
13294 HOST_WIDE_INT bits = INTVAL (x);
13295 fputs ((bits & 4) != 0 ? "r" : "", stream);
13296 }
13297 return;
13298
9997d19d
RE
13299 default:
13300 if (x == 0)
22de4c3d
RE
13301 {
13302 output_operand_lossage ("missing operand");
13303 return;
13304 }
9997d19d 13305
e6d29d15 13306 switch (GET_CODE (x))
9997d19d 13307 {
e6d29d15
NS
13308 case REG:
13309 asm_fprintf (stream, "%r", REGNO (x));
13310 break;
13311
13312 case MEM:
9997d19d
RE
13313 output_memory_reference_mode = GET_MODE (x);
13314 output_address (XEXP (x, 0));
e6d29d15
NS
13315 break;
13316
13317 case CONST_DOUBLE:
88f77cba
JB
13318 if (TARGET_NEON)
13319 {
13320 char fpstr[20];
13321 real_to_decimal (fpstr, CONST_DOUBLE_REAL_VALUE (x),
13322 sizeof (fpstr), 0, 1);
13323 fprintf (stream, "#%s", fpstr);
13324 }
13325 else
13326 fprintf (stream, "#%s", fp_immediate_constant (x));
e6d29d15
NS
13327 break;
13328
13329 default:
13330 gcc_assert (GET_CODE (x) != NEG);
9997d19d
RE
13331 fputc ('#', stream);
13332 output_addr_const (stream, x);
e6d29d15 13333 break;
9997d19d
RE
13334 }
13335 }
13336}
cce8749e 13337\f
301d03af
RS
13338/* Target hook for assembling integer objects. The ARM version needs to
13339 handle word-sized values specially. */
301d03af 13340static bool
e32bac5b 13341arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
301d03af 13342{
88f77cba
JB
13343 enum machine_mode mode;
13344
301d03af
RS
13345 if (size == UNITS_PER_WORD && aligned_p)
13346 {
13347 fputs ("\t.word\t", asm_out_file);
13348 output_addr_const (asm_out_file, x);
13349
13350 /* Mark symbols as position independent. We only do this in the
d6b4baa4 13351 .text segment, not in the .data segment. */
301d03af
RS
13352 if (NEED_GOT_RELOC && flag_pic && making_const_table &&
13353 (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
13354 {
9403b7f7
RS
13355 /* See legitimize_pic_address for an explanation of the
13356 TARGET_VXWORKS_RTP check. */
13357 if (TARGET_VXWORKS_RTP
13358 || (GET_CODE (x) == SYMBOL_REF && !SYMBOL_REF_LOCAL_P (x)))
301d03af 13359 fputs ("(GOT)", asm_out_file);
9403b7f7
RS
13360 else
13361 fputs ("(GOTOFF)", asm_out_file);
301d03af
RS
13362 }
13363 fputc ('\n', asm_out_file);
13364 return true;
13365 }
1d6e90ac 13366
88f77cba
JB
13367 mode = GET_MODE (x);
13368
13369 if (arm_vector_mode_supported_p (mode))
5a9335ef
NC
13370 {
13371 int i, units;
13372
e6d29d15 13373 gcc_assert (GET_CODE (x) == CONST_VECTOR);
5a9335ef
NC
13374
13375 units = CONST_VECTOR_NUNITS (x);
88f77cba 13376 size = GET_MODE_SIZE (GET_MODE_INNER (mode));
5a9335ef 13377
88f77cba
JB
13378 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
13379 for (i = 0; i < units; i++)
13380 {
874d42b9 13381 rtx elt = CONST_VECTOR_ELT (x, i);
88f77cba
JB
13382 assemble_integer
13383 (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
13384 }
13385 else
13386 for (i = 0; i < units; i++)
13387 {
13388 rtx elt = CONST_VECTOR_ELT (x, i);
13389 REAL_VALUE_TYPE rval;
5a9335ef 13390
88f77cba
JB
13391 REAL_VALUE_FROM_CONST_DOUBLE (rval, elt);
13392
13393 assemble_real
13394 (rval, GET_MODE_INNER (mode),
13395 i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT);
13396 }
5a9335ef
NC
13397
13398 return true;
13399 }
13400
301d03af
RS
13401 return default_assemble_integer (x, size, aligned_p);
13402}
7abc66b1 13403
7abc66b1 13404static void
9f296620 13405arm_elf_asm_cdtor (rtx symbol, int priority, bool is_ctor)
7abc66b1 13406{
50603eed
PB
13407 section *s;
13408
7abc66b1
JB
13409 if (!TARGET_AAPCS_BASED)
13410 {
9f296620
MM
13411 (is_ctor ?
13412 default_named_section_asm_out_constructor
13413 : default_named_section_asm_out_destructor) (symbol, priority);
7abc66b1
JB
13414 return;
13415 }
13416
13417 /* Put these in the .init_array section, using a special relocation. */
50603eed
PB
13418 if (priority != DEFAULT_INIT_PRIORITY)
13419 {
13420 char buf[18];
9f296620
MM
13421 sprintf (buf, "%s.%.5u",
13422 is_ctor ? ".init_array" : ".fini_array",
13423 priority);
50603eed
PB
13424 s = get_section (buf, SECTION_WRITE, NULL_TREE);
13425 }
9f296620 13426 else if (is_ctor)
50603eed 13427 s = ctors_section;
9f296620
MM
13428 else
13429 s = dtors_section;
50603eed
PB
13430
13431 switch_to_section (s);
7abc66b1
JB
13432 assemble_align (POINTER_SIZE);
13433 fputs ("\t.word\t", asm_out_file);
13434 output_addr_const (asm_out_file, symbol);
13435 fputs ("(target1)\n", asm_out_file);
13436}
9f296620
MM
13437
13438/* Add a function to the list of static constructors. */
13439
13440static void
13441arm_elf_asm_constructor (rtx symbol, int priority)
13442{
13443 arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/true);
13444}
13445
13446/* Add a function to the list of static destructors. */
13447
13448static void
13449arm_elf_asm_destructor (rtx symbol, int priority)
13450{
13451 arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/false);
13452}
301d03af 13453\f
cce8749e
CH
13454/* A finite state machine takes care of noticing whether or not instructions
13455 can be conditionally executed, and thus decrease execution time and code
13456 size by deleting branch instructions. The fsm is controlled by
13457 final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE. */
13458
13459/* The state of the fsm controlling condition codes are:
13460 0: normal, do nothing special
13461 1: make ASM_OUTPUT_OPCODE not output this instruction
13462 2: make ASM_OUTPUT_OPCODE not output this instruction
13463 3: make instructions conditional
13464 4: make instructions conditional
13465
13466 State transitions (state->state by whom under condition):
13467 0 -> 1 final_prescan_insn if the `target' is a label
13468 0 -> 2 final_prescan_insn if the `target' is an unconditional branch
13469 1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
13470 2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
4977bab6 13471 3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
cce8749e
CH
13472 (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
13473 4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
13474 (the target insn is arm_target_insn).
13475
ff9940b0
RE
13476 If the jump clobbers the conditions then we use states 2 and 4.
13477
13478 A similar thing can be done with conditional return insns.
13479
cce8749e
CH
13480 XXX In case the `target' is an unconditional branch, this conditionalising
13481 of the instructions always reduces code size, but not always execution
13482 time. But then, I want to reduce the code size to somewhere near what
13483 /bin/cc produces. */
13484
5b3e6663
PB
13485/* In addition to this, state is maintained for Thumb-2 COND_EXEC
13486 instructions. When a COND_EXEC instruction is seen the subsequent
13487 instructions are scanned so that multiple conditional instructions can be
13488 combined into a single IT block. arm_condexec_count and arm_condexec_mask
13489 specify the length and true/false mask for the IT block. These will be
13490 decremented/zeroed by arm_asm_output_opcode as the insns are output. */
13491
cce8749e
CH
13492/* Returns the index of the ARM condition code string in
13493 `arm_condition_codes'. COMPARISON should be an rtx like
13494 `(eq (...) (...))'. */
84ed5e79 13495static enum arm_cond_code
e32bac5b 13496get_arm_condition_code (rtx comparison)
cce8749e 13497{
5165176d 13498 enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
1d6e90ac
NC
13499 int code;
13500 enum rtx_code comp_code = GET_CODE (comparison);
5165176d
RE
13501
13502 if (GET_MODE_CLASS (mode) != MODE_CC)
84ed5e79 13503 mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
5165176d
RE
13504 XEXP (comparison, 1));
13505
13506 switch (mode)
cce8749e 13507 {
84ed5e79
RE
13508 case CC_DNEmode: code = ARM_NE; goto dominance;
13509 case CC_DEQmode: code = ARM_EQ; goto dominance;
13510 case CC_DGEmode: code = ARM_GE; goto dominance;
13511 case CC_DGTmode: code = ARM_GT; goto dominance;
13512 case CC_DLEmode: code = ARM_LE; goto dominance;
13513 case CC_DLTmode: code = ARM_LT; goto dominance;
13514 case CC_DGEUmode: code = ARM_CS; goto dominance;
13515 case CC_DGTUmode: code = ARM_HI; goto dominance;
13516 case CC_DLEUmode: code = ARM_LS; goto dominance;
13517 case CC_DLTUmode: code = ARM_CC;
13518
13519 dominance:
e6d29d15 13520 gcc_assert (comp_code == EQ || comp_code == NE);
84ed5e79
RE
13521
13522 if (comp_code == EQ)
13523 return ARM_INVERSE_CONDITION_CODE (code);
13524 return code;
13525
5165176d 13526 case CC_NOOVmode:
84ed5e79 13527 switch (comp_code)
5165176d 13528 {
84ed5e79
RE
13529 case NE: return ARM_NE;
13530 case EQ: return ARM_EQ;
13531 case GE: return ARM_PL;
13532 case LT: return ARM_MI;
e6d29d15 13533 default: gcc_unreachable ();
5165176d
RE
13534 }
13535
13536 case CC_Zmode:
84ed5e79 13537 switch (comp_code)
5165176d 13538 {
84ed5e79
RE
13539 case NE: return ARM_NE;
13540 case EQ: return ARM_EQ;
e6d29d15 13541 default: gcc_unreachable ();
5165176d
RE
13542 }
13543
defc0463
RE
13544 case CC_Nmode:
13545 switch (comp_code)
13546 {
13547 case NE: return ARM_MI;
13548 case EQ: return ARM_PL;
e6d29d15 13549 default: gcc_unreachable ();
defc0463
RE
13550 }
13551
5165176d 13552 case CCFPEmode:
e45b72c4
RE
13553 case CCFPmode:
13554 /* These encodings assume that AC=1 in the FPA system control
13555 byte. This allows us to handle all cases except UNEQ and
13556 LTGT. */
84ed5e79
RE
13557 switch (comp_code)
13558 {
13559 case GE: return ARM_GE;
13560 case GT: return ARM_GT;
13561 case LE: return ARM_LS;
13562 case LT: return ARM_MI;
e45b72c4
RE
13563 case NE: return ARM_NE;
13564 case EQ: return ARM_EQ;
13565 case ORDERED: return ARM_VC;
13566 case UNORDERED: return ARM_VS;
13567 case UNLT: return ARM_LT;
13568 case UNLE: return ARM_LE;
13569 case UNGT: return ARM_HI;
13570 case UNGE: return ARM_PL;
13571 /* UNEQ and LTGT do not have a representation. */
13572 case UNEQ: /* Fall through. */
13573 case LTGT: /* Fall through. */
e6d29d15 13574 default: gcc_unreachable ();
84ed5e79
RE
13575 }
13576
13577 case CC_SWPmode:
13578 switch (comp_code)
13579 {
13580 case NE: return ARM_NE;
13581 case EQ: return ARM_EQ;
13582 case GE: return ARM_LE;
13583 case GT: return ARM_LT;
13584 case LE: return ARM_GE;
13585 case LT: return ARM_GT;
13586 case GEU: return ARM_LS;
13587 case GTU: return ARM_CC;
13588 case LEU: return ARM_CS;
13589 case LTU: return ARM_HI;
e6d29d15 13590 default: gcc_unreachable ();
84ed5e79
RE
13591 }
13592
bd9c7e23
RE
13593 case CC_Cmode:
13594 switch (comp_code)
13595 {
13596 case LTU: return ARM_CS;
13597 case GEU: return ARM_CC;
e6d29d15 13598 default: gcc_unreachable ();
bd9c7e23 13599 }
f676971a 13600
5165176d 13601 case CCmode:
84ed5e79 13602 switch (comp_code)
5165176d 13603 {
84ed5e79
RE
13604 case NE: return ARM_NE;
13605 case EQ: return ARM_EQ;
13606 case GE: return ARM_GE;
13607 case GT: return ARM_GT;
13608 case LE: return ARM_LE;
13609 case LT: return ARM_LT;
13610 case GEU: return ARM_CS;
13611 case GTU: return ARM_HI;
13612 case LEU: return ARM_LS;
13613 case LTU: return ARM_CC;
e6d29d15 13614 default: gcc_unreachable ();
5165176d
RE
13615 }
13616
e6d29d15 13617 default: gcc_unreachable ();
cce8749e 13618 }
f3bb6135 13619}
cce8749e 13620
44c7bd63 13621/* Tell arm_asm_output_opcode to output IT blocks for conditionally executed
5b3e6663
PB
13622 instructions. */
13623void
13624thumb2_final_prescan_insn (rtx insn)
13625{
13626 rtx first_insn = insn;
13627 rtx body = PATTERN (insn);
13628 rtx predicate;
13629 enum arm_cond_code code;
13630 int n;
13631 int mask;
13632
13633 /* Remove the previous insn from the count of insns to be output. */
13634 if (arm_condexec_count)
13635 arm_condexec_count--;
13636
13637 /* Nothing to do if we are already inside a conditional block. */
13638 if (arm_condexec_count)
13639 return;
13640
13641 if (GET_CODE (body) != COND_EXEC)
13642 return;
13643
13644 /* Conditional jumps are implemented directly. */
13645 if (GET_CODE (insn) == JUMP_INSN)
13646 return;
13647
13648 predicate = COND_EXEC_TEST (body);
13649 arm_current_cc = get_arm_condition_code (predicate);
13650
13651 n = get_attr_ce_count (insn);
13652 arm_condexec_count = 1;
13653 arm_condexec_mask = (1 << n) - 1;
13654 arm_condexec_masklen = n;
13655 /* See if subsequent instructions can be combined into the same block. */
13656 for (;;)
13657 {
13658 insn = next_nonnote_insn (insn);
13659
13660 /* Jumping into the middle of an IT block is illegal, so a label or
13661 barrier terminates the block. */
13662 if (GET_CODE (insn) != INSN && GET_CODE(insn) != JUMP_INSN)
13663 break;
13664
13665 body = PATTERN (insn);
13666 /* USE and CLOBBER aren't really insns, so just skip them. */
13667 if (GET_CODE (body) == USE
13668 || GET_CODE (body) == CLOBBER)
5b0202af 13669 continue;
5b3e6663 13670
7a085dce 13671 /* ??? Recognize conditional jumps, and combine them with IT blocks. */
5b3e6663
PB
13672 if (GET_CODE (body) != COND_EXEC)
13673 break;
13674 /* Allow up to 4 conditionally executed instructions in a block. */
13675 n = get_attr_ce_count (insn);
13676 if (arm_condexec_masklen + n > 4)
13677 break;
13678
13679 predicate = COND_EXEC_TEST (body);
13680 code = get_arm_condition_code (predicate);
13681 mask = (1 << n) - 1;
13682 if (arm_current_cc == code)
13683 arm_condexec_mask |= (mask << arm_condexec_masklen);
13684 else if (arm_current_cc != ARM_INVERSE_CONDITION_CODE(code))
13685 break;
13686
13687 arm_condexec_count++;
13688 arm_condexec_masklen += n;
13689
13690 /* A jump must be the last instruction in a conditional block. */
13691 if (GET_CODE(insn) == JUMP_INSN)
13692 break;
13693 }
13694 /* Restore recog_data (getting the attributes of other insns can
13695 destroy this array, but final.c assumes that it remains intact
13696 across this call). */
13697 extract_constrain_insn_cached (first_insn);
13698}
13699
cce8749e 13700void
e32bac5b 13701arm_final_prescan_insn (rtx insn)
cce8749e
CH
13702{
13703 /* BODY will hold the body of INSN. */
1d6e90ac 13704 rtx body = PATTERN (insn);
cce8749e
CH
13705
13706 /* This will be 1 if trying to repeat the trick, and things need to be
13707 reversed if it appears to fail. */
13708 int reverse = 0;
13709
ff9940b0
RE
13710 /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
13711 taken are clobbered, even if the rtl suggests otherwise. It also
13712 means that we have to grub around within the jump expression to find
13713 out what the conditions are when the jump isn't taken. */
13714 int jump_clobbers = 0;
f676971a 13715
6354dc9b 13716 /* If we start with a return insn, we only succeed if we find another one. */
ff9940b0 13717 int seeking_return = 0;
f676971a 13718
cce8749e
CH
13719 /* START_INSN will hold the insn from where we start looking. This is the
13720 first insn after the following code_label if REVERSE is true. */
13721 rtx start_insn = insn;
13722
13723 /* If in state 4, check if the target branch is reached, in order to
13724 change back to state 0. */
13725 if (arm_ccfsm_state == 4)
13726 {
13727 if (insn == arm_target_insn)
f5a1b0d2
NC
13728 {
13729 arm_target_insn = NULL;
13730 arm_ccfsm_state = 0;
13731 }
cce8749e
CH
13732 return;
13733 }
13734
13735 /* If in state 3, it is possible to repeat the trick, if this insn is an
13736 unconditional branch to a label, and immediately following this branch
13737 is the previous target label which is only used once, and the label this
13738 branch jumps to is not too far off. */
13739 if (arm_ccfsm_state == 3)
13740 {
13741 if (simplejump_p (insn))
13742 {
13743 start_insn = next_nonnote_insn (start_insn);
13744 if (GET_CODE (start_insn) == BARRIER)
13745 {
13746 /* XXX Isn't this always a barrier? */
13747 start_insn = next_nonnote_insn (start_insn);
13748 }
13749 if (GET_CODE (start_insn) == CODE_LABEL
13750 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
13751 && LABEL_NUSES (start_insn) == 1)
13752 reverse = TRUE;
13753 else
13754 return;
13755 }
ff9940b0
RE
13756 else if (GET_CODE (body) == RETURN)
13757 {
13758 start_insn = next_nonnote_insn (start_insn);
13759 if (GET_CODE (start_insn) == BARRIER)
13760 start_insn = next_nonnote_insn (start_insn);
13761 if (GET_CODE (start_insn) == CODE_LABEL
13762 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
13763 && LABEL_NUSES (start_insn) == 1)
13764 {
13765 reverse = TRUE;
13766 seeking_return = 1;
13767 }
13768 else
13769 return;
13770 }
cce8749e
CH
13771 else
13772 return;
13773 }
13774
e6d29d15 13775 gcc_assert (!arm_ccfsm_state || reverse);
cce8749e
CH
13776 if (GET_CODE (insn) != JUMP_INSN)
13777 return;
13778
f676971a 13779 /* This jump might be paralleled with a clobber of the condition codes
ff9940b0
RE
13780 the jump should always come first */
13781 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
13782 body = XVECEXP (body, 0, 0);
13783
cce8749e
CH
13784 if (reverse
13785 || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
13786 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
13787 {
bd9c7e23
RE
13788 int insns_skipped;
13789 int fail = FALSE, succeed = FALSE;
cce8749e
CH
13790 /* Flag which part of the IF_THEN_ELSE is the LABEL_REF. */
13791 int then_not_else = TRUE;
ff9940b0 13792 rtx this_insn = start_insn, label = 0;
cce8749e 13793
f676971a 13794 /* If the jump cannot be done with one instruction, we cannot
e45b72c4 13795 conditionally execute the instruction in the inverse case. */
ff9940b0 13796 if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
5bbe2d40 13797 {
5bbe2d40
RE
13798 jump_clobbers = 1;
13799 return;
13800 }
f676971a 13801
cce8749e
CH
13802 /* Register the insn jumped to. */
13803 if (reverse)
ff9940b0
RE
13804 {
13805 if (!seeking_return)
13806 label = XEXP (SET_SRC (body), 0);
13807 }
cce8749e
CH
13808 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
13809 label = XEXP (XEXP (SET_SRC (body), 1), 0);
13810 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
13811 {
13812 label = XEXP (XEXP (SET_SRC (body), 2), 0);
13813 then_not_else = FALSE;
13814 }
ff9940b0
RE
13815 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
13816 seeking_return = 1;
13817 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
13818 {
13819 seeking_return = 1;
13820 then_not_else = FALSE;
13821 }
cce8749e 13822 else
e6d29d15 13823 gcc_unreachable ();
cce8749e
CH
13824
13825 /* See how many insns this branch skips, and what kind of insns. If all
13826 insns are okay, and the label or unconditional branch to the same
13827 label is not too far away, succeed. */
13828 for (insns_skipped = 0;
b36ba79f 13829 !fail && !succeed && insns_skipped++ < max_insns_skipped;)
cce8749e
CH
13830 {
13831 rtx scanbody;
13832
13833 this_insn = next_nonnote_insn (this_insn);
13834 if (!this_insn)
13835 break;
13836
cce8749e
CH
13837 switch (GET_CODE (this_insn))
13838 {
13839 case CODE_LABEL:
13840 /* Succeed if it is the target label, otherwise fail since
13841 control falls in from somewhere else. */
13842 if (this_insn == label)
13843 {
ff9940b0
RE
13844 if (jump_clobbers)
13845 {
13846 arm_ccfsm_state = 2;
13847 this_insn = next_nonnote_insn (this_insn);
13848 }
13849 else
13850 arm_ccfsm_state = 1;
cce8749e
CH
13851 succeed = TRUE;
13852 }
13853 else
13854 fail = TRUE;
13855 break;
13856
ff9940b0 13857 case BARRIER:
cce8749e 13858 /* Succeed if the following insn is the target label.
f676971a
EC
13859 Otherwise fail.
13860 If return insns are used then the last insn in a function
6354dc9b 13861 will be a barrier. */
cce8749e 13862 this_insn = next_nonnote_insn (this_insn);
ff9940b0 13863 if (this_insn && this_insn == label)
cce8749e 13864 {
ff9940b0
RE
13865 if (jump_clobbers)
13866 {
13867 arm_ccfsm_state = 2;
13868 this_insn = next_nonnote_insn (this_insn);
13869 }
13870 else
13871 arm_ccfsm_state = 1;
cce8749e
CH
13872 succeed = TRUE;
13873 }
13874 else
13875 fail = TRUE;
13876 break;
13877
ff9940b0 13878 case CALL_INSN:
68d560d4
RE
13879 /* The AAPCS says that conditional calls should not be
13880 used since they make interworking inefficient (the
13881 linker can't transform BL<cond> into BLX). That's
13882 only a problem if the machine has BLX. */
13883 if (arm_arch5)
13884 {
13885 fail = TRUE;
13886 break;
13887 }
13888
61f0ccff
RE
13889 /* Succeed if the following insn is the target label, or
13890 if the following two insns are a barrier and the
13891 target label. */
13892 this_insn = next_nonnote_insn (this_insn);
13893 if (this_insn && GET_CODE (this_insn) == BARRIER)
13894 this_insn = next_nonnote_insn (this_insn);
bd9c7e23 13895
61f0ccff
RE
13896 if (this_insn && this_insn == label
13897 && insns_skipped < max_insns_skipped)
13898 {
13899 if (jump_clobbers)
bd9c7e23 13900 {
61f0ccff
RE
13901 arm_ccfsm_state = 2;
13902 this_insn = next_nonnote_insn (this_insn);
bd9c7e23
RE
13903 }
13904 else
61f0ccff
RE
13905 arm_ccfsm_state = 1;
13906 succeed = TRUE;
bd9c7e23 13907 }
61f0ccff
RE
13908 else
13909 fail = TRUE;
ff9940b0 13910 break;
2b835d68 13911
cce8749e
CH
13912 case JUMP_INSN:
13913 /* If this is an unconditional branch to the same label, succeed.
13914 If it is to another label, do nothing. If it is conditional,
13915 fail. */
e32bac5b
RE
13916 /* XXX Probably, the tests for SET and the PC are
13917 unnecessary. */
cce8749e 13918
ed4c4348 13919 scanbody = PATTERN (this_insn);
ff9940b0
RE
13920 if (GET_CODE (scanbody) == SET
13921 && GET_CODE (SET_DEST (scanbody)) == PC)
cce8749e
CH
13922 {
13923 if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
13924 && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
13925 {
13926 arm_ccfsm_state = 2;
13927 succeed = TRUE;
13928 }
13929 else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
13930 fail = TRUE;
13931 }
112cdef5 13932 /* Fail if a conditional return is undesirable (e.g. on a
b36ba79f
RE
13933 StrongARM), but still allow this if optimizing for size. */
13934 else if (GET_CODE (scanbody) == RETURN
a72d4945 13935 && !use_return_insn (TRUE, NULL)
5895f793 13936 && !optimize_size)
b36ba79f 13937 fail = TRUE;
ff9940b0
RE
13938 else if (GET_CODE (scanbody) == RETURN
13939 && seeking_return)
13940 {
13941 arm_ccfsm_state = 2;
13942 succeed = TRUE;
13943 }
13944 else if (GET_CODE (scanbody) == PARALLEL)
13945 {
13946 switch (get_attr_conds (this_insn))
13947 {
13948 case CONDS_NOCOND:
13949 break;
13950 default:
13951 fail = TRUE;
13952 break;
13953 }
13954 }
4e67550b 13955 else
112cdef5 13956 fail = TRUE; /* Unrecognized jump (e.g. epilogue). */
4e67550b 13957
cce8749e
CH
13958 break;
13959
13960 case INSN:
ff9940b0
RE
13961 /* Instructions using or affecting the condition codes make it
13962 fail. */
ed4c4348 13963 scanbody = PATTERN (this_insn);
5895f793
RE
13964 if (!(GET_CODE (scanbody) == SET
13965 || GET_CODE (scanbody) == PARALLEL)
74641843 13966 || get_attr_conds (this_insn) != CONDS_NOCOND)
cce8749e 13967 fail = TRUE;
9b6b54e2
NC
13968
13969 /* A conditional cirrus instruction must be followed by
13970 a non Cirrus instruction. However, since we
13971 conditionalize instructions in this function and by
13972 the time we get here we can't add instructions
13973 (nops), because shorten_branches() has already been
13974 called, we will disable conditionalizing Cirrus
13975 instructions to be safe. */
13976 if (GET_CODE (scanbody) != USE
13977 && GET_CODE (scanbody) != CLOBBER
f0375c66 13978 && get_attr_cirrus (this_insn) != CIRRUS_NOT)
9b6b54e2 13979 fail = TRUE;
cce8749e
CH
13980 break;
13981
13982 default:
13983 break;
13984 }
13985 }
13986 if (succeed)
13987 {
ff9940b0 13988 if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
cce8749e 13989 arm_target_label = CODE_LABEL_NUMBER (label);
e6d29d15 13990 else
ff9940b0 13991 {
e6d29d15 13992 gcc_assert (seeking_return || arm_ccfsm_state == 2);
e0b92319 13993
ff9940b0
RE
13994 while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
13995 {
13996 this_insn = next_nonnote_insn (this_insn);
e6d29d15
NS
13997 gcc_assert (!this_insn
13998 || (GET_CODE (this_insn) != BARRIER
13999 && GET_CODE (this_insn) != CODE_LABEL));
ff9940b0
RE
14000 }
14001 if (!this_insn)
14002 {
d6b4baa4 14003 /* Oh, dear! we ran off the end.. give up. */
5b3e6663 14004 extract_constrain_insn_cached (insn);
ff9940b0 14005 arm_ccfsm_state = 0;
abaa26e5 14006 arm_target_insn = NULL;
ff9940b0
RE
14007 return;
14008 }
14009 arm_target_insn = this_insn;
14010 }
ff9940b0
RE
14011 if (jump_clobbers)
14012 {
e6d29d15 14013 gcc_assert (!reverse);
f676971a 14014 arm_current_cc =
ff9940b0
RE
14015 get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
14016 0), 0), 1));
14017 if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
14018 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
14019 if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
14020 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
14021 }
14022 else
14023 {
14024 /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
14025 what it was. */
14026 if (!reverse)
14027 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
14028 0));
14029 }
cce8749e 14030
cce8749e
CH
14031 if (reverse || then_not_else)
14032 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
14033 }
f676971a 14034
1ccbefce 14035 /* Restore recog_data (getting the attributes of other insns can
ff9940b0 14036 destroy this array, but final.c assumes that it remains intact
5b3e6663
PB
14037 across this call. */
14038 extract_constrain_insn_cached (insn);
14039 }
14040}
14041
14042/* Output IT instructions. */
14043void
14044thumb2_asm_output_opcode (FILE * stream)
14045{
14046 char buff[5];
14047 int n;
14048
14049 if (arm_condexec_mask)
14050 {
14051 for (n = 0; n < arm_condexec_masklen; n++)
14052 buff[n] = (arm_condexec_mask & (1 << n)) ? 't' : 'e';
14053 buff[n] = 0;
14054 asm_fprintf(stream, "i%s\t%s\n\t", buff,
14055 arm_condition_codes[arm_current_cc]);
14056 arm_condexec_mask = 0;
cce8749e 14057 }
f3bb6135 14058}
cce8749e 14059
4b02997f 14060/* Returns true if REGNO is a valid register
21b5653c 14061 for holding a quantity of type MODE. */
4b02997f 14062int
e32bac5b 14063arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
4b02997f
NC
14064{
14065 if (GET_MODE_CLASS (mode) == MODE_CC)
a6a5de04
RE
14066 return (regno == CC_REGNUM
14067 || (TARGET_HARD_FLOAT && TARGET_VFP
14068 && regno == VFPCC_REGNUM));
f676971a 14069
5b3e6663 14070 if (TARGET_THUMB1)
4b02997f
NC
14071 /* For the Thumb we only allow values bigger than SImode in
14072 registers 0 - 6, so that there is always a second low
14073 register available to hold the upper part of the value.
14074 We probably we ought to ensure that the register is the
14075 start of an even numbered register pair. */
e9d7b180 14076 return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
4b02997f 14077
a6a5de04
RE
14078 if (TARGET_HARD_FLOAT && TARGET_MAVERICK
14079 && IS_CIRRUS_REGNUM (regno))
9b6b54e2
NC
14080 /* We have outlawed SI values in Cirrus registers because they
14081 reside in the lower 32 bits, but SF values reside in the
14082 upper 32 bits. This causes gcc all sorts of grief. We can't
14083 even split the registers into pairs because Cirrus SI values
14084 get sign extended to 64bits-- aldyh. */
14085 return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
14086
a6a5de04
RE
14087 if (TARGET_HARD_FLOAT && TARGET_VFP
14088 && IS_VFP_REGNUM (regno))
9b66ebb1
PB
14089 {
14090 if (mode == SFmode || mode == SImode)
f1adb0a9 14091 return VFP_REGNO_OK_FOR_SINGLE (regno);
9b66ebb1 14092
9b66ebb1 14093 if (mode == DFmode)
f1adb0a9 14094 return VFP_REGNO_OK_FOR_DOUBLE (regno);
88f77cba
JB
14095
14096 if (TARGET_NEON)
14097 return (VALID_NEON_DREG_MODE (mode) && VFP_REGNO_OK_FOR_DOUBLE (regno))
14098 || (VALID_NEON_QREG_MODE (mode)
14099 && NEON_REGNO_OK_FOR_QUAD (regno))
14100 || (mode == TImode && NEON_REGNO_OK_FOR_NREGS (regno, 2))
14101 || (mode == EImode && NEON_REGNO_OK_FOR_NREGS (regno, 3))
14102 || (mode == OImode && NEON_REGNO_OK_FOR_NREGS (regno, 4))
14103 || (mode == CImode && NEON_REGNO_OK_FOR_NREGS (regno, 6))
14104 || (mode == XImode && NEON_REGNO_OK_FOR_NREGS (regno, 8));
14105
9b66ebb1
PB
14106 return FALSE;
14107 }
14108
a6a5de04
RE
14109 if (TARGET_REALLY_IWMMXT)
14110 {
14111 if (IS_IWMMXT_GR_REGNUM (regno))
14112 return mode == SImode;
5a9335ef 14113
a6a5de04
RE
14114 if (IS_IWMMXT_REGNUM (regno))
14115 return VALID_IWMMXT_REG_MODE (mode);
14116 }
14117
fdd695fd
PB
14118 /* We allow any value to be stored in the general registers.
14119 Restrict doubleword quantities to even register pairs so that we can
88f77cba
JB
14120 use ldrd. Do not allow Neon structure opaque modes in general registers;
14121 they would use too many. */
4b02997f 14122 if (regno <= LAST_ARM_REGNUM)
88f77cba
JB
14123 return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0)
14124 && !VALID_NEON_STRUCT_MODE (mode);
4b02997f 14125
a6a5de04 14126 if (regno == FRAME_POINTER_REGNUM
4b02997f
NC
14127 || regno == ARG_POINTER_REGNUM)
14128 /* We only allow integers in the fake hard registers. */
14129 return GET_MODE_CLASS (mode) == MODE_INT;
14130
3b684012 14131 /* The only registers left are the FPA registers
4b02997f 14132 which we only allow to hold FP values. */
a6a5de04
RE
14133 return (TARGET_HARD_FLOAT && TARGET_FPA
14134 && GET_MODE_CLASS (mode) == MODE_FLOAT
14135 && regno >= FIRST_FPA_REGNUM
14136 && regno <= LAST_FPA_REGNUM);
4b02997f
NC
14137}
14138
5b3e6663
PB
14139/* For efficiency and historical reasons LO_REGS, HI_REGS and CC_REGS are
14140 not used in arm mode. */
d5b7b3ae 14141int
e32bac5b 14142arm_regno_class (int regno)
d5b7b3ae 14143{
5b3e6663 14144 if (TARGET_THUMB1)
d5b7b3ae
RE
14145 {
14146 if (regno == STACK_POINTER_REGNUM)
14147 return STACK_REG;
14148 if (regno == CC_REGNUM)
14149 return CC_REG;
14150 if (regno < 8)
14151 return LO_REGS;
14152 return HI_REGS;
14153 }
14154
5b3e6663
PB
14155 if (TARGET_THUMB2 && regno < 8)
14156 return LO_REGS;
14157
d5b7b3ae
RE
14158 if ( regno <= LAST_ARM_REGNUM
14159 || regno == FRAME_POINTER_REGNUM
14160 || regno == ARG_POINTER_REGNUM)
5b3e6663 14161 return TARGET_THUMB2 ? HI_REGS : GENERAL_REGS;
f676971a 14162
9b66ebb1 14163 if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
5b3e6663 14164 return TARGET_THUMB2 ? CC_REG : NO_REGS;
d5b7b3ae 14165
9b6b54e2
NC
14166 if (IS_CIRRUS_REGNUM (regno))
14167 return CIRRUS_REGS;
14168
9b66ebb1 14169 if (IS_VFP_REGNUM (regno))
f1adb0a9
JB
14170 {
14171 if (regno <= D7_VFP_REGNUM)
14172 return VFP_D0_D7_REGS;
14173 else if (regno <= LAST_LO_VFP_REGNUM)
14174 return VFP_LO_REGS;
14175 else
14176 return VFP_HI_REGS;
14177 }
9b66ebb1 14178
5a9335ef
NC
14179 if (IS_IWMMXT_REGNUM (regno))
14180 return IWMMXT_REGS;
14181
e99faaaa
ILT
14182 if (IS_IWMMXT_GR_REGNUM (regno))
14183 return IWMMXT_GR_REGS;
14184
3b684012 14185 return FPA_REGS;
d5b7b3ae
RE
14186}
14187
14188/* Handle a special case when computing the offset
14189 of an argument from the frame pointer. */
14190int
e32bac5b 14191arm_debugger_arg_offset (int value, rtx addr)
d5b7b3ae
RE
14192{
14193 rtx insn;
14194
14195 /* We are only interested if dbxout_parms() failed to compute the offset. */
14196 if (value != 0)
14197 return 0;
14198
14199 /* We can only cope with the case where the address is held in a register. */
14200 if (GET_CODE (addr) != REG)
14201 return 0;
14202
14203 /* If we are using the frame pointer to point at the argument, then
14204 an offset of 0 is correct. */
cd2b33d0 14205 if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
d5b7b3ae 14206 return 0;
f676971a 14207
d5b7b3ae
RE
14208 /* If we are using the stack pointer to point at the
14209 argument, then an offset of 0 is correct. */
5b3e6663 14210 /* ??? Check this is consistent with thumb2 frame layout. */
5895f793 14211 if ((TARGET_THUMB || !frame_pointer_needed)
d5b7b3ae
RE
14212 && REGNO (addr) == SP_REGNUM)
14213 return 0;
f676971a 14214
d5b7b3ae
RE
14215 /* Oh dear. The argument is pointed to by a register rather
14216 than being held in a register, or being stored at a known
14217 offset from the frame pointer. Since GDB only understands
14218 those two kinds of argument we must translate the address
14219 held in the register into an offset from the frame pointer.
14220 We do this by searching through the insns for the function
14221 looking to see where this register gets its value. If the
4912a07c 14222 register is initialized from the frame pointer plus an offset
d5b7b3ae 14223 then we are in luck and we can continue, otherwise we give up.
f676971a 14224
d5b7b3ae
RE
14225 This code is exercised by producing debugging information
14226 for a function with arguments like this:
f676971a 14227
d5b7b3ae 14228 double func (double a, double b, int c, double d) {return d;}
f676971a 14229
d5b7b3ae
RE
14230 Without this code the stab for parameter 'd' will be set to
14231 an offset of 0 from the frame pointer, rather than 8. */
14232
14233 /* The if() statement says:
14234
14235 If the insn is a normal instruction
14236 and if the insn is setting the value in a register
14237 and if the register being set is the register holding the address of the argument
14238 and if the address is computing by an addition
14239 that involves adding to a register
14240 which is the frame pointer
14241 a constant integer
14242
d6b4baa4 14243 then... */
f676971a 14244
d5b7b3ae
RE
14245 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
14246 {
f676971a 14247 if ( GET_CODE (insn) == INSN
d5b7b3ae
RE
14248 && GET_CODE (PATTERN (insn)) == SET
14249 && REGNO (XEXP (PATTERN (insn), 0)) == REGNO (addr)
14250 && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
14251 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
cd2b33d0 14252 && REGNO (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
d5b7b3ae
RE
14253 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
14254 )
14255 {
14256 value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
f676971a 14257
d5b7b3ae
RE
14258 break;
14259 }
14260 }
f676971a 14261
d5b7b3ae
RE
14262 if (value == 0)
14263 {
14264 debug_rtx (addr);
d4ee4d25 14265 warning (0, "unable to compute real location of stacked parameter");
d5b7b3ae
RE
14266 value = 8; /* XXX magic hack */
14267 }
14268
14269 return value;
14270}
d5b7b3ae 14271\f
5a9335ef
NC
14272#define def_mbuiltin(MASK, NAME, TYPE, CODE) \
14273 do \
14274 { \
14275 if ((MASK) & insn_flags) \
c79efc4d
RÁE
14276 add_builtin_function ((NAME), (TYPE), (CODE), \
14277 BUILT_IN_MD, NULL, NULL_TREE); \
5a9335ef
NC
14278 } \
14279 while (0)
14280
14281struct builtin_description
14282{
14283 const unsigned int mask;
14284 const enum insn_code icode;
14285 const char * const name;
14286 const enum arm_builtins code;
14287 const enum rtx_code comparison;
14288 const unsigned int flag;
14289};
14290
14291static const struct builtin_description bdesc_2arg[] =
14292{
14293#define IWMMXT_BUILTIN(code, string, builtin) \
14294 { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
14295 ARM_BUILTIN_##builtin, 0, 0 },
14296
14297 IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
14298 IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
14299 IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
14300 IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
14301 IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
14302 IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
14303 IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
14304 IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
14305 IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
14306 IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
14307 IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
14308 IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
14309 IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
14310 IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
14311 IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
14312 IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
14313 IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
14314 IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
14315 IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
f07a6b21
BE
14316 IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
14317 IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
5a9335ef
NC
14318 IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
14319 IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
14320 IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
14321 IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
14322 IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
14323 IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
14324 IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
14325 IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
14326 IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
14327 IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
14328 IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
14329 IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
14330 IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
14331 IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
14332 IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
14333 IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
14334 IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
14335 IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
14336 IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
14337 IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
14338 IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
14339 IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
14340 IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
14341 IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
14342 IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
14343 IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
14344 IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
14345 IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
14346 IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
14347 IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
14348 IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
14349 IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
14350 IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
14351 IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
14352 IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
14353 IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
14354 IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
14355
14356#define IWMMXT_BUILTIN2(code, builtin) \
14357 { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
f676971a 14358
5a9335ef
NC
14359 IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
14360 IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
14361 IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
14362 IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
14363 IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
14364 IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
14365 IWMMXT_BUILTIN2 (ashlv4hi3_di, WSLLH)
88f77cba 14366 IWMMXT_BUILTIN2 (ashlv4hi3_iwmmxt, WSLLHI)
5a9335ef 14367 IWMMXT_BUILTIN2 (ashlv2si3_di, WSLLW)
88f77cba 14368 IWMMXT_BUILTIN2 (ashlv2si3_iwmmxt, WSLLWI)
5a9335ef
NC
14369 IWMMXT_BUILTIN2 (ashldi3_di, WSLLD)
14370 IWMMXT_BUILTIN2 (ashldi3_iwmmxt, WSLLDI)
14371 IWMMXT_BUILTIN2 (lshrv4hi3_di, WSRLH)
88f77cba 14372 IWMMXT_BUILTIN2 (lshrv4hi3_iwmmxt, WSRLHI)
5a9335ef 14373 IWMMXT_BUILTIN2 (lshrv2si3_di, WSRLW)
88f77cba 14374 IWMMXT_BUILTIN2 (lshrv2si3_iwmmxt, WSRLWI)
5a9335ef 14375 IWMMXT_BUILTIN2 (lshrdi3_di, WSRLD)
9b66ebb1 14376 IWMMXT_BUILTIN2 (lshrdi3_iwmmxt, WSRLDI)
5a9335ef 14377 IWMMXT_BUILTIN2 (ashrv4hi3_di, WSRAH)
88f77cba 14378 IWMMXT_BUILTIN2 (ashrv4hi3_iwmmxt, WSRAHI)
5a9335ef 14379 IWMMXT_BUILTIN2 (ashrv2si3_di, WSRAW)
88f77cba 14380 IWMMXT_BUILTIN2 (ashrv2si3_iwmmxt, WSRAWI)
5a9335ef 14381 IWMMXT_BUILTIN2 (ashrdi3_di, WSRAD)
9b66ebb1 14382 IWMMXT_BUILTIN2 (ashrdi3_iwmmxt, WSRADI)
5a9335ef
NC
14383 IWMMXT_BUILTIN2 (rorv4hi3_di, WRORH)
14384 IWMMXT_BUILTIN2 (rorv4hi3, WRORHI)
14385 IWMMXT_BUILTIN2 (rorv2si3_di, WRORW)
14386 IWMMXT_BUILTIN2 (rorv2si3, WRORWI)
14387 IWMMXT_BUILTIN2 (rordi3_di, WRORD)
14388 IWMMXT_BUILTIN2 (rordi3, WRORDI)
14389 IWMMXT_BUILTIN2 (iwmmxt_wmacuz, WMACUZ)
14390 IWMMXT_BUILTIN2 (iwmmxt_wmacsz, WMACSZ)
14391};
14392
14393static const struct builtin_description bdesc_1arg[] =
14394{
14395 IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
14396 IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
14397 IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
14398 IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
14399 IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
14400 IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
14401 IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
14402 IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
14403 IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
14404 IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
14405 IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
14406 IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
14407 IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
14408 IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
14409 IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
14410 IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
14411 IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
14412 IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
14413};
14414
14415/* Set up all the iWMMXt builtins. This is
14416 not called if TARGET_IWMMXT is zero. */
14417
14418static void
14419arm_init_iwmmxt_builtins (void)
14420{
14421 const struct builtin_description * d;
14422 size_t i;
14423 tree endlink = void_list_node;
14424
4a5eab38
PB
14425 tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
14426 tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
14427 tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
14428
5a9335ef
NC
14429 tree int_ftype_int
14430 = build_function_type (integer_type_node,
14431 tree_cons (NULL_TREE, integer_type_node, endlink));
14432 tree v8qi_ftype_v8qi_v8qi_int
14433 = build_function_type (V8QI_type_node,
14434 tree_cons (NULL_TREE, V8QI_type_node,
14435 tree_cons (NULL_TREE, V8QI_type_node,
14436 tree_cons (NULL_TREE,
14437 integer_type_node,
14438 endlink))));
14439 tree v4hi_ftype_v4hi_int
14440 = build_function_type (V4HI_type_node,
14441 tree_cons (NULL_TREE, V4HI_type_node,
14442 tree_cons (NULL_TREE, integer_type_node,
14443 endlink)));
14444 tree v2si_ftype_v2si_int
14445 = build_function_type (V2SI_type_node,
14446 tree_cons (NULL_TREE, V2SI_type_node,
14447 tree_cons (NULL_TREE, integer_type_node,
14448 endlink)));
14449 tree v2si_ftype_di_di
14450 = build_function_type (V2SI_type_node,
14451 tree_cons (NULL_TREE, long_long_integer_type_node,
14452 tree_cons (NULL_TREE, long_long_integer_type_node,
14453 endlink)));
14454 tree di_ftype_di_int
14455 = build_function_type (long_long_integer_type_node,
14456 tree_cons (NULL_TREE, long_long_integer_type_node,
14457 tree_cons (NULL_TREE, integer_type_node,
14458 endlink)));
14459 tree di_ftype_di_int_int
14460 = build_function_type (long_long_integer_type_node,
14461 tree_cons (NULL_TREE, long_long_integer_type_node,
14462 tree_cons (NULL_TREE, integer_type_node,
14463 tree_cons (NULL_TREE,
14464 integer_type_node,
14465 endlink))));
14466 tree int_ftype_v8qi
14467 = build_function_type (integer_type_node,
14468 tree_cons (NULL_TREE, V8QI_type_node,
14469 endlink));
14470 tree int_ftype_v4hi
14471 = build_function_type (integer_type_node,
14472 tree_cons (NULL_TREE, V4HI_type_node,
14473 endlink));
14474 tree int_ftype_v2si
14475 = build_function_type (integer_type_node,
14476 tree_cons (NULL_TREE, V2SI_type_node,
14477 endlink));
14478 tree int_ftype_v8qi_int
14479 = build_function_type (integer_type_node,
14480 tree_cons (NULL_TREE, V8QI_type_node,
14481 tree_cons (NULL_TREE, integer_type_node,
14482 endlink)));
14483 tree int_ftype_v4hi_int
14484 = build_function_type (integer_type_node,
14485 tree_cons (NULL_TREE, V4HI_type_node,
14486 tree_cons (NULL_TREE, integer_type_node,
14487 endlink)));
14488 tree int_ftype_v2si_int
14489 = build_function_type (integer_type_node,
14490 tree_cons (NULL_TREE, V2SI_type_node,
14491 tree_cons (NULL_TREE, integer_type_node,
14492 endlink)));
14493 tree v8qi_ftype_v8qi_int_int
14494 = build_function_type (V8QI_type_node,
14495 tree_cons (NULL_TREE, V8QI_type_node,
14496 tree_cons (NULL_TREE, integer_type_node,
14497 tree_cons (NULL_TREE,
14498 integer_type_node,
14499 endlink))));
14500 tree v4hi_ftype_v4hi_int_int
14501 = build_function_type (V4HI_type_node,
14502 tree_cons (NULL_TREE, V4HI_type_node,
14503 tree_cons (NULL_TREE, integer_type_node,
14504 tree_cons (NULL_TREE,
14505 integer_type_node,
14506 endlink))));
14507 tree v2si_ftype_v2si_int_int
14508 = build_function_type (V2SI_type_node,
14509 tree_cons (NULL_TREE, V2SI_type_node,
14510 tree_cons (NULL_TREE, integer_type_node,
14511 tree_cons (NULL_TREE,
14512 integer_type_node,
14513 endlink))));
14514 /* Miscellaneous. */
14515 tree v8qi_ftype_v4hi_v4hi
14516 = build_function_type (V8QI_type_node,
14517 tree_cons (NULL_TREE, V4HI_type_node,
14518 tree_cons (NULL_TREE, V4HI_type_node,
14519 endlink)));
14520 tree v4hi_ftype_v2si_v2si
14521 = build_function_type (V4HI_type_node,
14522 tree_cons (NULL_TREE, V2SI_type_node,
14523 tree_cons (NULL_TREE, V2SI_type_node,
14524 endlink)));
14525 tree v2si_ftype_v4hi_v4hi
14526 = build_function_type (V2SI_type_node,
14527 tree_cons (NULL_TREE, V4HI_type_node,
14528 tree_cons (NULL_TREE, V4HI_type_node,
14529 endlink)));
14530 tree v2si_ftype_v8qi_v8qi
14531 = build_function_type (V2SI_type_node,
14532 tree_cons (NULL_TREE, V8QI_type_node,
14533 tree_cons (NULL_TREE, V8QI_type_node,
14534 endlink)));
14535 tree v4hi_ftype_v4hi_di
14536 = build_function_type (V4HI_type_node,
14537 tree_cons (NULL_TREE, V4HI_type_node,
14538 tree_cons (NULL_TREE,
14539 long_long_integer_type_node,
14540 endlink)));
14541 tree v2si_ftype_v2si_di
14542 = build_function_type (V2SI_type_node,
14543 tree_cons (NULL_TREE, V2SI_type_node,
14544 tree_cons (NULL_TREE,
14545 long_long_integer_type_node,
14546 endlink)));
14547 tree void_ftype_int_int
14548 = build_function_type (void_type_node,
14549 tree_cons (NULL_TREE, integer_type_node,
14550 tree_cons (NULL_TREE, integer_type_node,
14551 endlink)));
14552 tree di_ftype_void
14553 = build_function_type (long_long_unsigned_type_node, endlink);
14554 tree di_ftype_v8qi
14555 = build_function_type (long_long_integer_type_node,
14556 tree_cons (NULL_TREE, V8QI_type_node,
14557 endlink));
14558 tree di_ftype_v4hi
14559 = build_function_type (long_long_integer_type_node,
14560 tree_cons (NULL_TREE, V4HI_type_node,
14561 endlink));
14562 tree di_ftype_v2si
14563 = build_function_type (long_long_integer_type_node,
14564 tree_cons (NULL_TREE, V2SI_type_node,
14565 endlink));
14566 tree v2si_ftype_v4hi
14567 = build_function_type (V2SI_type_node,
14568 tree_cons (NULL_TREE, V4HI_type_node,
14569 endlink));
14570 tree v4hi_ftype_v8qi
14571 = build_function_type (V4HI_type_node,
14572 tree_cons (NULL_TREE, V8QI_type_node,
14573 endlink));
14574
14575 tree di_ftype_di_v4hi_v4hi
14576 = build_function_type (long_long_unsigned_type_node,
14577 tree_cons (NULL_TREE,
14578 long_long_unsigned_type_node,
14579 tree_cons (NULL_TREE, V4HI_type_node,
14580 tree_cons (NULL_TREE,
14581 V4HI_type_node,
14582 endlink))));
14583
14584 tree di_ftype_v4hi_v4hi
14585 = build_function_type (long_long_unsigned_type_node,
14586 tree_cons (NULL_TREE, V4HI_type_node,
14587 tree_cons (NULL_TREE, V4HI_type_node,
14588 endlink)));
14589
14590 /* Normal vector binops. */
14591 tree v8qi_ftype_v8qi_v8qi
14592 = build_function_type (V8QI_type_node,
14593 tree_cons (NULL_TREE, V8QI_type_node,
14594 tree_cons (NULL_TREE, V8QI_type_node,
14595 endlink)));
14596 tree v4hi_ftype_v4hi_v4hi
14597 = build_function_type (V4HI_type_node,
14598 tree_cons (NULL_TREE, V4HI_type_node,
14599 tree_cons (NULL_TREE, V4HI_type_node,
14600 endlink)));
14601 tree v2si_ftype_v2si_v2si
14602 = build_function_type (V2SI_type_node,
14603 tree_cons (NULL_TREE, V2SI_type_node,
14604 tree_cons (NULL_TREE, V2SI_type_node,
14605 endlink)));
14606 tree di_ftype_di_di
14607 = build_function_type (long_long_unsigned_type_node,
14608 tree_cons (NULL_TREE, long_long_unsigned_type_node,
14609 tree_cons (NULL_TREE,
14610 long_long_unsigned_type_node,
14611 endlink)));
14612
14613 /* Add all builtins that are more or less simple operations on two
14614 operands. */
e97a46ce 14615 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
5a9335ef
NC
14616 {
14617 /* Use one of the operands; the target can have a different mode for
14618 mask-generating compares. */
14619 enum machine_mode mode;
14620 tree type;
14621
14622 if (d->name == 0)
14623 continue;
14624
14625 mode = insn_data[d->icode].operand[1].mode;
14626
14627 switch (mode)
14628 {
14629 case V8QImode:
14630 type = v8qi_ftype_v8qi_v8qi;
14631 break;
14632 case V4HImode:
14633 type = v4hi_ftype_v4hi_v4hi;
14634 break;
14635 case V2SImode:
14636 type = v2si_ftype_v2si_v2si;
14637 break;
14638 case DImode:
14639 type = di_ftype_di_di;
14640 break;
14641
14642 default:
e6d29d15 14643 gcc_unreachable ();
5a9335ef
NC
14644 }
14645
14646 def_mbuiltin (d->mask, d->name, type, d->code);
14647 }
14648
14649 /* Add the remaining MMX insns with somewhat more complicated types. */
14650 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
14651 def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
14652 def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
14653
14654 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
14655 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
14656 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
14657 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
14658 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
14659 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
14660
14661 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
14662 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
14663 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
14664 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
14665 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
14666 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
14667
14668 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
14669 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
14670 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
14671 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
14672 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
14673 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
14674
14675 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
14676 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
14677 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
14678 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
14679 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
14680 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
14681
14682 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
14683
14684 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
14685 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
14686 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
14687 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
14688
14689 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
14690 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
14691 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
14692 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
14693 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
14694 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
14695 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
14696 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
14697 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
14698
14699 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
14700 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
14701 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
14702
14703 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
14704 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
14705 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
14706
14707 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
14708 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
14709 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
14710 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
14711 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
14712 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
14713
14714 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
14715 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
14716 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
14717 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
14718 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
14719 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
14720 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
14721 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
14722 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
14723 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
14724 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
14725 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
14726
14727 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
14728 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
14729 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
14730 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
14731
14732 def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
14733 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
14734 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
14735 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
14736 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
14737 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
14738 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
14739}
14740
d3585b76
DJ
14741static void
14742arm_init_tls_builtins (void)
14743{
ebfe65a3 14744 tree ftype, decl;
d3585b76
DJ
14745
14746 ftype = build_function_type (ptr_type_node, void_list_node);
ebfe65a3
JJ
14747 decl = add_builtin_function ("__builtin_thread_pointer", ftype,
14748 ARM_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
14749 NULL, NULL_TREE);
14750 TREE_NOTHROW (decl) = 1;
14751 TREE_READONLY (decl) = 1;
d3585b76
DJ
14752}
14753
88f77cba
JB
14754typedef enum {
14755 T_V8QI = 0x0001,
14756 T_V4HI = 0x0002,
14757 T_V2SI = 0x0004,
14758 T_V2SF = 0x0008,
14759 T_DI = 0x0010,
14760 T_V16QI = 0x0020,
14761 T_V8HI = 0x0040,
14762 T_V4SI = 0x0080,
14763 T_V4SF = 0x0100,
14764 T_V2DI = 0x0200,
14765 T_TI = 0x0400,
14766 T_EI = 0x0800,
14767 T_OI = 0x1000
14768} neon_builtin_type_bits;
14769
14770#define v8qi_UP T_V8QI
14771#define v4hi_UP T_V4HI
14772#define v2si_UP T_V2SI
14773#define v2sf_UP T_V2SF
14774#define di_UP T_DI
14775#define v16qi_UP T_V16QI
14776#define v8hi_UP T_V8HI
14777#define v4si_UP T_V4SI
14778#define v4sf_UP T_V4SF
14779#define v2di_UP T_V2DI
14780#define ti_UP T_TI
14781#define ei_UP T_EI
14782#define oi_UP T_OI
14783
14784#define UP(X) X##_UP
14785
14786#define T_MAX 13
14787
14788typedef enum {
14789 NEON_BINOP,
14790 NEON_TERNOP,
14791 NEON_UNOP,
14792 NEON_GETLANE,
14793 NEON_SETLANE,
14794 NEON_CREATE,
14795 NEON_DUP,
14796 NEON_DUPLANE,
14797 NEON_COMBINE,
14798 NEON_SPLIT,
14799 NEON_LANEMUL,
14800 NEON_LANEMULL,
14801 NEON_LANEMULH,
14802 NEON_LANEMAC,
14803 NEON_SCALARMUL,
14804 NEON_SCALARMULL,
14805 NEON_SCALARMULH,
14806 NEON_SCALARMAC,
14807 NEON_CONVERT,
14808 NEON_FIXCONV,
14809 NEON_SELECT,
14810 NEON_RESULTPAIR,
14811 NEON_REINTERP,
14812 NEON_VTBL,
14813 NEON_VTBX,
14814 NEON_LOAD1,
14815 NEON_LOAD1LANE,
14816 NEON_STORE1,
14817 NEON_STORE1LANE,
14818 NEON_LOADSTRUCT,
14819 NEON_LOADSTRUCTLANE,
14820 NEON_STORESTRUCT,
14821 NEON_STORESTRUCTLANE,
14822 NEON_LOGICBINOP,
14823 NEON_SHIFTINSERT,
14824 NEON_SHIFTIMM,
14825 NEON_SHIFTACC
14826} neon_itype;
14827
14828typedef struct {
14829 const char *name;
14830 const neon_itype itype;
14831 const neon_builtin_type_bits bits;
14832 const enum insn_code codes[T_MAX];
14833 const unsigned int num_vars;
14834 unsigned int base_fcode;
14835} neon_builtin_datum;
14836
14837#define CF(N,X) CODE_FOR_neon_##N##X
14838
14839#define VAR1(T, N, A) \
14840 #N, NEON_##T, UP (A), { CF (N, A) }, 1, 0
14841#define VAR2(T, N, A, B) \
14842 #N, NEON_##T, UP (A) | UP (B), { CF (N, A), CF (N, B) }, 2, 0
14843#define VAR3(T, N, A, B, C) \
14844 #N, NEON_##T, UP (A) | UP (B) | UP (C), \
14845 { CF (N, A), CF (N, B), CF (N, C) }, 3, 0
14846#define VAR4(T, N, A, B, C, D) \
14847 #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D), \
14848 { CF (N, A), CF (N, B), CF (N, C), CF (N, D) }, 4, 0
14849#define VAR5(T, N, A, B, C, D, E) \
14850 #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E), \
14851 { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E) }, 5, 0
14852#define VAR6(T, N, A, B, C, D, E, F) \
14853 #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F), \
14854 { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F) }, 6, 0
14855#define VAR7(T, N, A, B, C, D, E, F, G) \
14856 #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G), \
14857 { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
14858 CF (N, G) }, 7, 0
14859#define VAR8(T, N, A, B, C, D, E, F, G, H) \
14860 #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
14861 | UP (H), \
14862 { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
14863 CF (N, G), CF (N, H) }, 8, 0
14864#define VAR9(T, N, A, B, C, D, E, F, G, H, I) \
14865 #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
14866 | UP (H) | UP (I), \
14867 { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
14868 CF (N, G), CF (N, H), CF (N, I) }, 9, 0
14869#define VAR10(T, N, A, B, C, D, E, F, G, H, I, J) \
14870 #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
14871 | UP (H) | UP (I) | UP (J), \
14872 { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
14873 CF (N, G), CF (N, H), CF (N, I), CF (N, J) }, 10, 0
14874
14875/* The mode entries in the following table correspond to the "key" type of the
14876 instruction variant, i.e. equivalent to that which would be specified after
14877 the assembler mnemonic, which usually refers to the last vector operand.
14878 (Signed/unsigned/polynomial types are not differentiated between though, and
14879 are all mapped onto the same mode for a given element size.) The modes
14880 listed per instruction should be the same as those defined for that
14881 instruction's pattern in neon.md.
14882 WARNING: Variants should be listed in the same increasing order as
14883 neon_builtin_type_bits. */
14884
14885static neon_builtin_datum neon_builtin_data[] =
14886{
14887 { VAR10 (BINOP, vadd,
14888 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14889 { VAR3 (BINOP, vaddl, v8qi, v4hi, v2si) },
14890 { VAR3 (BINOP, vaddw, v8qi, v4hi, v2si) },
14891 { VAR6 (BINOP, vhadd, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14892 { VAR8 (BINOP, vqadd, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14893 { VAR3 (BINOP, vaddhn, v8hi, v4si, v2di) },
14894 { VAR8 (BINOP, vmul, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14895 { VAR8 (TERNOP, vmla, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14896 { VAR3 (TERNOP, vmlal, v8qi, v4hi, v2si) },
14897 { VAR8 (TERNOP, vmls, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14898 { VAR3 (TERNOP, vmlsl, v8qi, v4hi, v2si) },
14899 { VAR4 (BINOP, vqdmulh, v4hi, v2si, v8hi, v4si) },
14900 { VAR2 (TERNOP, vqdmlal, v4hi, v2si) },
14901 { VAR2 (TERNOP, vqdmlsl, v4hi, v2si) },
14902 { VAR3 (BINOP, vmull, v8qi, v4hi, v2si) },
14903 { VAR2 (SCALARMULL, vmull_n, v4hi, v2si) },
14904 { VAR2 (LANEMULL, vmull_lane, v4hi, v2si) },
14905 { VAR2 (SCALARMULL, vqdmull_n, v4hi, v2si) },
14906 { VAR2 (LANEMULL, vqdmull_lane, v4hi, v2si) },
14907 { VAR4 (SCALARMULH, vqdmulh_n, v4hi, v2si, v8hi, v4si) },
14908 { VAR4 (LANEMULH, vqdmulh_lane, v4hi, v2si, v8hi, v4si) },
14909 { VAR2 (BINOP, vqdmull, v4hi, v2si) },
14910 { VAR8 (BINOP, vshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14911 { VAR8 (BINOP, vqshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14912 { VAR8 (SHIFTIMM, vshr_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14913 { VAR3 (SHIFTIMM, vshrn_n, v8hi, v4si, v2di) },
14914 { VAR3 (SHIFTIMM, vqshrn_n, v8hi, v4si, v2di) },
14915 { VAR3 (SHIFTIMM, vqshrun_n, v8hi, v4si, v2di) },
14916 { VAR8 (SHIFTIMM, vshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14917 { VAR8 (SHIFTIMM, vqshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14918 { VAR8 (SHIFTIMM, vqshlu_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14919 { VAR3 (SHIFTIMM, vshll_n, v8qi, v4hi, v2si) },
14920 { VAR8 (SHIFTACC, vsra_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14921 { VAR10 (BINOP, vsub,
14922 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14923 { VAR3 (BINOP, vsubl, v8qi, v4hi, v2si) },
14924 { VAR3 (BINOP, vsubw, v8qi, v4hi, v2si) },
14925 { VAR8 (BINOP, vqsub, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14926 { VAR6 (BINOP, vhsub, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14927 { VAR3 (BINOP, vsubhn, v8hi, v4si, v2di) },
14928 { VAR8 (BINOP, vceq, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14929 { VAR8 (BINOP, vcge, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14930 { VAR8 (BINOP, vcgt, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14931 { VAR2 (BINOP, vcage, v2sf, v4sf) },
14932 { VAR2 (BINOP, vcagt, v2sf, v4sf) },
14933 { VAR6 (BINOP, vtst, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14934 { VAR8 (BINOP, vabd, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14935 { VAR3 (BINOP, vabdl, v8qi, v4hi, v2si) },
14936 { VAR6 (TERNOP, vaba, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14937 { VAR3 (TERNOP, vabal, v8qi, v4hi, v2si) },
14938 { VAR8 (BINOP, vmax, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14939 { VAR8 (BINOP, vmin, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14940 { VAR4 (BINOP, vpadd, v8qi, v4hi, v2si, v2sf) },
14941 { VAR6 (UNOP, vpaddl, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14942 { VAR6 (BINOP, vpadal, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14943 { VAR4 (BINOP, vpmax, v8qi, v4hi, v2si, v2sf) },
14944 { VAR4 (BINOP, vpmin, v8qi, v4hi, v2si, v2sf) },
14945 { VAR2 (BINOP, vrecps, v2sf, v4sf) },
14946 { VAR2 (BINOP, vrsqrts, v2sf, v4sf) },
14947 { VAR8 (SHIFTINSERT, vsri_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14948 { VAR8 (SHIFTINSERT, vsli_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14949 { VAR8 (UNOP, vabs, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14950 { VAR6 (UNOP, vqabs, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14951 { VAR8 (UNOP, vneg, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14952 { VAR6 (UNOP, vqneg, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14953 { VAR6 (UNOP, vcls, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14954 { VAR6 (UNOP, vclz, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14955 { VAR2 (UNOP, vcnt, v8qi, v16qi) },
14956 { VAR4 (UNOP, vrecpe, v2si, v2sf, v4si, v4sf) },
14957 { VAR4 (UNOP, vrsqrte, v2si, v2sf, v4si, v4sf) },
14958 { VAR6 (UNOP, vmvn, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14959 /* FIXME: vget_lane supports more variants than this! */
14960 { VAR10 (GETLANE, vget_lane,
14961 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14962 { VAR10 (SETLANE, vset_lane,
14963 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14964 { VAR5 (CREATE, vcreate, v8qi, v4hi, v2si, v2sf, di) },
14965 { VAR10 (DUP, vdup_n,
14966 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14967 { VAR10 (DUPLANE, vdup_lane,
14968 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14969 { VAR5 (COMBINE, vcombine, v8qi, v4hi, v2si, v2sf, di) },
14970 { VAR5 (SPLIT, vget_high, v16qi, v8hi, v4si, v4sf, v2di) },
14971 { VAR5 (SPLIT, vget_low, v16qi, v8hi, v4si, v4sf, v2di) },
14972 { VAR3 (UNOP, vmovn, v8hi, v4si, v2di) },
14973 { VAR3 (UNOP, vqmovn, v8hi, v4si, v2di) },
14974 { VAR3 (UNOP, vqmovun, v8hi, v4si, v2di) },
14975 { VAR3 (UNOP, vmovl, v8qi, v4hi, v2si) },
14976 { VAR6 (LANEMUL, vmul_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
14977 { VAR6 (LANEMAC, vmla_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
14978 { VAR2 (LANEMAC, vmlal_lane, v4hi, v2si) },
14979 { VAR2 (LANEMAC, vqdmlal_lane, v4hi, v2si) },
14980 { VAR6 (LANEMAC, vmls_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
14981 { VAR2 (LANEMAC, vmlsl_lane, v4hi, v2si) },
14982 { VAR2 (LANEMAC, vqdmlsl_lane, v4hi, v2si) },
14983 { VAR6 (SCALARMUL, vmul_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
14984 { VAR6 (SCALARMAC, vmla_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
14985 { VAR2 (SCALARMAC, vmlal_n, v4hi, v2si) },
14986 { VAR2 (SCALARMAC, vqdmlal_n, v4hi, v2si) },
14987 { VAR6 (SCALARMAC, vmls_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
14988 { VAR2 (SCALARMAC, vmlsl_n, v4hi, v2si) },
14989 { VAR2 (SCALARMAC, vqdmlsl_n, v4hi, v2si) },
14990 { VAR10 (BINOP, vext,
14991 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14992 { VAR8 (UNOP, vrev64, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14993 { VAR4 (UNOP, vrev32, v8qi, v4hi, v16qi, v8hi) },
14994 { VAR2 (UNOP, vrev16, v8qi, v16qi) },
14995 { VAR4 (CONVERT, vcvt, v2si, v2sf, v4si, v4sf) },
14996 { VAR4 (FIXCONV, vcvt_n, v2si, v2sf, v4si, v4sf) },
14997 { VAR10 (SELECT, vbsl,
14998 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14999 { VAR1 (VTBL, vtbl1, v8qi) },
15000 { VAR1 (VTBL, vtbl2, v8qi) },
15001 { VAR1 (VTBL, vtbl3, v8qi) },
15002 { VAR1 (VTBL, vtbl4, v8qi) },
15003 { VAR1 (VTBX, vtbx1, v8qi) },
15004 { VAR1 (VTBX, vtbx2, v8qi) },
15005 { VAR1 (VTBX, vtbx3, v8qi) },
15006 { VAR1 (VTBX, vtbx4, v8qi) },
15007 { VAR8 (RESULTPAIR, vtrn, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15008 { VAR8 (RESULTPAIR, vzip, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15009 { VAR8 (RESULTPAIR, vuzp, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15010 { VAR5 (REINTERP, vreinterpretv8qi, v8qi, v4hi, v2si, v2sf, di) },
15011 { VAR5 (REINTERP, vreinterpretv4hi, v8qi, v4hi, v2si, v2sf, di) },
15012 { VAR5 (REINTERP, vreinterpretv2si, v8qi, v4hi, v2si, v2sf, di) },
15013 { VAR5 (REINTERP, vreinterpretv2sf, v8qi, v4hi, v2si, v2sf, di) },
15014 { VAR5 (REINTERP, vreinterpretdi, v8qi, v4hi, v2si, v2sf, di) },
15015 { VAR5 (REINTERP, vreinterpretv16qi, v16qi, v8hi, v4si, v4sf, v2di) },
15016 { VAR5 (REINTERP, vreinterpretv8hi, v16qi, v8hi, v4si, v4sf, v2di) },
15017 { VAR5 (REINTERP, vreinterpretv4si, v16qi, v8hi, v4si, v4sf, v2di) },
15018 { VAR5 (REINTERP, vreinterpretv4sf, v16qi, v8hi, v4si, v4sf, v2di) },
15019 { VAR5 (REINTERP, vreinterpretv2di, v16qi, v8hi, v4si, v4sf, v2di) },
15020 { VAR10 (LOAD1, vld1,
15021 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15022 { VAR10 (LOAD1LANE, vld1_lane,
15023 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15024 { VAR10 (LOAD1, vld1_dup,
15025 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15026 { VAR10 (STORE1, vst1,
15027 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15028 { VAR10 (STORE1LANE, vst1_lane,
15029 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15030 { VAR9 (LOADSTRUCT,
15031 vld2, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15032 { VAR7 (LOADSTRUCTLANE, vld2_lane,
15033 v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15034 { VAR5 (LOADSTRUCT, vld2_dup, v8qi, v4hi, v2si, v2sf, di) },
15035 { VAR9 (STORESTRUCT, vst2,
15036 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15037 { VAR7 (STORESTRUCTLANE, vst2_lane,
15038 v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15039 { VAR9 (LOADSTRUCT,
15040 vld3, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15041 { VAR7 (LOADSTRUCTLANE, vld3_lane,
15042 v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15043 { VAR5 (LOADSTRUCT, vld3_dup, v8qi, v4hi, v2si, v2sf, di) },
15044 { VAR9 (STORESTRUCT, vst3,
15045 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15046 { VAR7 (STORESTRUCTLANE, vst3_lane,
15047 v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15048 { VAR9 (LOADSTRUCT, vld4,
15049 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15050 { VAR7 (LOADSTRUCTLANE, vld4_lane,
15051 v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15052 { VAR5 (LOADSTRUCT, vld4_dup, v8qi, v4hi, v2si, v2sf, di) },
15053 { VAR9 (STORESTRUCT, vst4,
15054 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15055 { VAR7 (STORESTRUCTLANE, vst4_lane,
15056 v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15057 { VAR10 (LOGICBINOP, vand,
15058 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15059 { VAR10 (LOGICBINOP, vorr,
15060 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15061 { VAR10 (BINOP, veor,
15062 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15063 { VAR10 (LOGICBINOP, vbic,
15064 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15065 { VAR10 (LOGICBINOP, vorn,
15066 v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) }
15067};
15068
15069#undef CF
15070#undef VAR1
15071#undef VAR2
15072#undef VAR3
15073#undef VAR4
15074#undef VAR5
15075#undef VAR6
15076#undef VAR7
15077#undef VAR8
15078#undef VAR9
15079#undef VAR10
15080
15081static void
15082arm_init_neon_builtins (void)
15083{
15084 unsigned int i, fcode = ARM_BUILTIN_NEON_BASE;
15085
af06585a
JM
15086 tree neon_intQI_type_node;
15087 tree neon_intHI_type_node;
15088 tree neon_polyQI_type_node;
15089 tree neon_polyHI_type_node;
15090 tree neon_intSI_type_node;
15091 tree neon_intDI_type_node;
15092 tree neon_float_type_node;
15093
15094 tree intQI_pointer_node;
15095 tree intHI_pointer_node;
15096 tree intSI_pointer_node;
15097 tree intDI_pointer_node;
15098 tree float_pointer_node;
15099
15100 tree const_intQI_node;
15101 tree const_intHI_node;
15102 tree const_intSI_node;
15103 tree const_intDI_node;
15104 tree const_float_node;
15105
15106 tree const_intQI_pointer_node;
15107 tree const_intHI_pointer_node;
15108 tree const_intSI_pointer_node;
15109 tree const_intDI_pointer_node;
15110 tree const_float_pointer_node;
15111
15112 tree V8QI_type_node;
15113 tree V4HI_type_node;
15114 tree V2SI_type_node;
15115 tree V2SF_type_node;
15116 tree V16QI_type_node;
15117 tree V8HI_type_node;
15118 tree V4SI_type_node;
15119 tree V4SF_type_node;
15120 tree V2DI_type_node;
15121
15122 tree intUQI_type_node;
15123 tree intUHI_type_node;
15124 tree intUSI_type_node;
15125 tree intUDI_type_node;
15126
15127 tree intEI_type_node;
15128 tree intOI_type_node;
15129 tree intCI_type_node;
15130 tree intXI_type_node;
15131
15132 tree V8QI_pointer_node;
15133 tree V4HI_pointer_node;
15134 tree V2SI_pointer_node;
15135 tree V2SF_pointer_node;
15136 tree V16QI_pointer_node;
15137 tree V8HI_pointer_node;
15138 tree V4SI_pointer_node;
15139 tree V4SF_pointer_node;
15140 tree V2DI_pointer_node;
15141
15142 tree void_ftype_pv8qi_v8qi_v8qi;
15143 tree void_ftype_pv4hi_v4hi_v4hi;
15144 tree void_ftype_pv2si_v2si_v2si;
15145 tree void_ftype_pv2sf_v2sf_v2sf;
15146 tree void_ftype_pdi_di_di;
15147 tree void_ftype_pv16qi_v16qi_v16qi;
15148 tree void_ftype_pv8hi_v8hi_v8hi;
15149 tree void_ftype_pv4si_v4si_v4si;
15150 tree void_ftype_pv4sf_v4sf_v4sf;
15151 tree void_ftype_pv2di_v2di_v2di;
15152
15153 tree reinterp_ftype_dreg[5][5];
15154 tree reinterp_ftype_qreg[5][5];
15155 tree dreg_types[5], qreg_types[5];
15156
88f77cba
JB
15157 /* Create distinguished type nodes for NEON vector element types,
15158 and pointers to values of such types, so we can detect them later. */
af06585a
JM
15159 neon_intQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
15160 neon_intHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
15161 neon_polyQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
15162 neon_polyHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
15163 neon_intSI_type_node = make_signed_type (GET_MODE_PRECISION (SImode));
15164 neon_intDI_type_node = make_signed_type (GET_MODE_PRECISION (DImode));
15165 neon_float_type_node = make_node (REAL_TYPE);
15166 TYPE_PRECISION (neon_float_type_node) = FLOAT_TYPE_SIZE;
15167 layout_type (neon_float_type_node);
15168
bcbdbbb0
JM
15169 /* Define typedefs which exactly correspond to the modes we are basing vector
15170 types on. If you change these names you'll need to change
15171 the table used by arm_mangle_type too. */
15172 (*lang_hooks.types.register_builtin_type) (neon_intQI_type_node,
15173 "__builtin_neon_qi");
15174 (*lang_hooks.types.register_builtin_type) (neon_intHI_type_node,
15175 "__builtin_neon_hi");
15176 (*lang_hooks.types.register_builtin_type) (neon_intSI_type_node,
15177 "__builtin_neon_si");
15178 (*lang_hooks.types.register_builtin_type) (neon_float_type_node,
15179 "__builtin_neon_sf");
15180 (*lang_hooks.types.register_builtin_type) (neon_intDI_type_node,
15181 "__builtin_neon_di");
15182 (*lang_hooks.types.register_builtin_type) (neon_polyQI_type_node,
15183 "__builtin_neon_poly8");
15184 (*lang_hooks.types.register_builtin_type) (neon_polyHI_type_node,
15185 "__builtin_neon_poly16");
15186
af06585a
JM
15187 intQI_pointer_node = build_pointer_type (neon_intQI_type_node);
15188 intHI_pointer_node = build_pointer_type (neon_intHI_type_node);
15189 intSI_pointer_node = build_pointer_type (neon_intSI_type_node);
15190 intDI_pointer_node = build_pointer_type (neon_intDI_type_node);
15191 float_pointer_node = build_pointer_type (neon_float_type_node);
88f77cba
JB
15192
15193 /* Next create constant-qualified versions of the above types. */
af06585a
JM
15194 const_intQI_node = build_qualified_type (neon_intQI_type_node,
15195 TYPE_QUAL_CONST);
15196 const_intHI_node = build_qualified_type (neon_intHI_type_node,
15197 TYPE_QUAL_CONST);
15198 const_intSI_node = build_qualified_type (neon_intSI_type_node,
15199 TYPE_QUAL_CONST);
15200 const_intDI_node = build_qualified_type (neon_intDI_type_node,
15201 TYPE_QUAL_CONST);
15202 const_float_node = build_qualified_type (neon_float_type_node,
15203 TYPE_QUAL_CONST);
15204
15205 const_intQI_pointer_node = build_pointer_type (const_intQI_node);
15206 const_intHI_pointer_node = build_pointer_type (const_intHI_node);
15207 const_intSI_pointer_node = build_pointer_type (const_intSI_node);
15208 const_intDI_pointer_node = build_pointer_type (const_intDI_node);
15209 const_float_pointer_node = build_pointer_type (const_float_node);
88f77cba
JB
15210
15211 /* Now create vector types based on our NEON element types. */
15212 /* 64-bit vectors. */
af06585a 15213 V8QI_type_node =
88f77cba 15214 build_vector_type_for_mode (neon_intQI_type_node, V8QImode);
af06585a 15215 V4HI_type_node =
88f77cba 15216 build_vector_type_for_mode (neon_intHI_type_node, V4HImode);
af06585a 15217 V2SI_type_node =
88f77cba 15218 build_vector_type_for_mode (neon_intSI_type_node, V2SImode);
af06585a 15219 V2SF_type_node =
88f77cba
JB
15220 build_vector_type_for_mode (neon_float_type_node, V2SFmode);
15221 /* 128-bit vectors. */
af06585a 15222 V16QI_type_node =
88f77cba 15223 build_vector_type_for_mode (neon_intQI_type_node, V16QImode);
af06585a 15224 V8HI_type_node =
88f77cba 15225 build_vector_type_for_mode (neon_intHI_type_node, V8HImode);
af06585a 15226 V4SI_type_node =
88f77cba 15227 build_vector_type_for_mode (neon_intSI_type_node, V4SImode);
af06585a 15228 V4SF_type_node =
88f77cba 15229 build_vector_type_for_mode (neon_float_type_node, V4SFmode);
af06585a 15230 V2DI_type_node =
88f77cba
JB
15231 build_vector_type_for_mode (neon_intDI_type_node, V2DImode);
15232
15233 /* Unsigned integer types for various mode sizes. */
af06585a
JM
15234 intUQI_type_node = make_unsigned_type (GET_MODE_PRECISION (QImode));
15235 intUHI_type_node = make_unsigned_type (GET_MODE_PRECISION (HImode));
15236 intUSI_type_node = make_unsigned_type (GET_MODE_PRECISION (SImode));
15237 intUDI_type_node = make_unsigned_type (GET_MODE_PRECISION (DImode));
88f77cba 15238
bcbdbbb0
JM
15239 (*lang_hooks.types.register_builtin_type) (intUQI_type_node,
15240 "__builtin_neon_uqi");
15241 (*lang_hooks.types.register_builtin_type) (intUHI_type_node,
15242 "__builtin_neon_uhi");
15243 (*lang_hooks.types.register_builtin_type) (intUSI_type_node,
15244 "__builtin_neon_usi");
15245 (*lang_hooks.types.register_builtin_type) (intUDI_type_node,
15246 "__builtin_neon_udi");
15247
88f77cba 15248 /* Opaque integer types for structures of vectors. */
af06585a
JM
15249 intEI_type_node = make_signed_type (GET_MODE_PRECISION (EImode));
15250 intOI_type_node = make_signed_type (GET_MODE_PRECISION (OImode));
15251 intCI_type_node = make_signed_type (GET_MODE_PRECISION (CImode));
15252 intXI_type_node = make_signed_type (GET_MODE_PRECISION (XImode));
88f77cba 15253
bcbdbbb0
JM
15254 (*lang_hooks.types.register_builtin_type) (intTI_type_node,
15255 "__builtin_neon_ti");
15256 (*lang_hooks.types.register_builtin_type) (intEI_type_node,
15257 "__builtin_neon_ei");
15258 (*lang_hooks.types.register_builtin_type) (intOI_type_node,
15259 "__builtin_neon_oi");
15260 (*lang_hooks.types.register_builtin_type) (intCI_type_node,
15261 "__builtin_neon_ci");
15262 (*lang_hooks.types.register_builtin_type) (intXI_type_node,
15263 "__builtin_neon_xi");
15264
88f77cba 15265 /* Pointers to vector types. */
af06585a
JM
15266 V8QI_pointer_node = build_pointer_type (V8QI_type_node);
15267 V4HI_pointer_node = build_pointer_type (V4HI_type_node);
15268 V2SI_pointer_node = build_pointer_type (V2SI_type_node);
15269 V2SF_pointer_node = build_pointer_type (V2SF_type_node);
15270 V16QI_pointer_node = build_pointer_type (V16QI_type_node);
15271 V8HI_pointer_node = build_pointer_type (V8HI_type_node);
15272 V4SI_pointer_node = build_pointer_type (V4SI_type_node);
15273 V4SF_pointer_node = build_pointer_type (V4SF_type_node);
15274 V2DI_pointer_node = build_pointer_type (V2DI_type_node);
88f77cba
JB
15275
15276 /* Operations which return results as pairs. */
af06585a 15277 void_ftype_pv8qi_v8qi_v8qi =
88f77cba
JB
15278 build_function_type_list (void_type_node, V8QI_pointer_node, V8QI_type_node,
15279 V8QI_type_node, NULL);
af06585a 15280 void_ftype_pv4hi_v4hi_v4hi =
88f77cba
JB
15281 build_function_type_list (void_type_node, V4HI_pointer_node, V4HI_type_node,
15282 V4HI_type_node, NULL);
af06585a 15283 void_ftype_pv2si_v2si_v2si =
88f77cba
JB
15284 build_function_type_list (void_type_node, V2SI_pointer_node, V2SI_type_node,
15285 V2SI_type_node, NULL);
af06585a 15286 void_ftype_pv2sf_v2sf_v2sf =
88f77cba
JB
15287 build_function_type_list (void_type_node, V2SF_pointer_node, V2SF_type_node,
15288 V2SF_type_node, NULL);
af06585a 15289 void_ftype_pdi_di_di =
88f77cba
JB
15290 build_function_type_list (void_type_node, intDI_pointer_node,
15291 neon_intDI_type_node, neon_intDI_type_node, NULL);
af06585a 15292 void_ftype_pv16qi_v16qi_v16qi =
88f77cba
JB
15293 build_function_type_list (void_type_node, V16QI_pointer_node,
15294 V16QI_type_node, V16QI_type_node, NULL);
af06585a 15295 void_ftype_pv8hi_v8hi_v8hi =
88f77cba
JB
15296 build_function_type_list (void_type_node, V8HI_pointer_node, V8HI_type_node,
15297 V8HI_type_node, NULL);
af06585a 15298 void_ftype_pv4si_v4si_v4si =
88f77cba
JB
15299 build_function_type_list (void_type_node, V4SI_pointer_node, V4SI_type_node,
15300 V4SI_type_node, NULL);
af06585a 15301 void_ftype_pv4sf_v4sf_v4sf =
88f77cba
JB
15302 build_function_type_list (void_type_node, V4SF_pointer_node, V4SF_type_node,
15303 V4SF_type_node, NULL);
af06585a 15304 void_ftype_pv2di_v2di_v2di =
88f77cba
JB
15305 build_function_type_list (void_type_node, V2DI_pointer_node, V2DI_type_node,
15306 V2DI_type_node, NULL);
15307
88f77cba
JB
15308 dreg_types[0] = V8QI_type_node;
15309 dreg_types[1] = V4HI_type_node;
15310 dreg_types[2] = V2SI_type_node;
15311 dreg_types[3] = V2SF_type_node;
15312 dreg_types[4] = neon_intDI_type_node;
15313
15314 qreg_types[0] = V16QI_type_node;
15315 qreg_types[1] = V8HI_type_node;
15316 qreg_types[2] = V4SI_type_node;
15317 qreg_types[3] = V4SF_type_node;
15318 qreg_types[4] = V2DI_type_node;
15319
15320 for (i = 0; i < 5; i++)
15321 {
15322 int j;
15323 for (j = 0; j < 5; j++)
15324 {
15325 reinterp_ftype_dreg[i][j]
15326 = build_function_type_list (dreg_types[i], dreg_types[j], NULL);
15327 reinterp_ftype_qreg[i][j]
15328 = build_function_type_list (qreg_types[i], qreg_types[j], NULL);
15329 }
15330 }
15331
15332 for (i = 0; i < ARRAY_SIZE (neon_builtin_data); i++)
15333 {
15334 neon_builtin_datum *d = &neon_builtin_data[i];
15335 unsigned int j, codeidx = 0;
15336
15337 d->base_fcode = fcode;
15338
15339 for (j = 0; j < T_MAX; j++)
15340 {
15341 const char* const modenames[] = {
15342 "v8qi", "v4hi", "v2si", "v2sf", "di",
15343 "v16qi", "v8hi", "v4si", "v4sf", "v2di"
15344 };
15345 char namebuf[60];
15346 tree ftype = NULL;
15347 enum insn_code icode;
15348 int is_load = 0, is_store = 0;
15349
15350 if ((d->bits & (1 << j)) == 0)
15351 continue;
15352
15353 icode = d->codes[codeidx++];
15354
15355 switch (d->itype)
15356 {
15357 case NEON_LOAD1:
15358 case NEON_LOAD1LANE:
15359 case NEON_LOADSTRUCT:
15360 case NEON_LOADSTRUCTLANE:
15361 is_load = 1;
15362 /* Fall through. */
15363 case NEON_STORE1:
15364 case NEON_STORE1LANE:
15365 case NEON_STORESTRUCT:
15366 case NEON_STORESTRUCTLANE:
15367 if (!is_load)
15368 is_store = 1;
15369 /* Fall through. */
15370 case NEON_UNOP:
15371 case NEON_BINOP:
15372 case NEON_LOGICBINOP:
15373 case NEON_SHIFTINSERT:
15374 case NEON_TERNOP:
15375 case NEON_GETLANE:
15376 case NEON_SETLANE:
15377 case NEON_CREATE:
15378 case NEON_DUP:
15379 case NEON_DUPLANE:
15380 case NEON_SHIFTIMM:
15381 case NEON_SHIFTACC:
15382 case NEON_COMBINE:
15383 case NEON_SPLIT:
15384 case NEON_CONVERT:
15385 case NEON_FIXCONV:
15386 case NEON_LANEMUL:
15387 case NEON_LANEMULL:
15388 case NEON_LANEMULH:
15389 case NEON_LANEMAC:
15390 case NEON_SCALARMUL:
15391 case NEON_SCALARMULL:
15392 case NEON_SCALARMULH:
15393 case NEON_SCALARMAC:
15394 case NEON_SELECT:
15395 case NEON_VTBL:
15396 case NEON_VTBX:
15397 {
15398 int k;
15399 tree return_type = void_type_node, args = void_list_node;
15400
15401 /* Build a function type directly from the insn_data for this
15402 builtin. The build_function_type() function takes care of
15403 removing duplicates for us. */
15404 for (k = insn_data[icode].n_operands - 1; k >= 0; k--)
15405 {
15406 tree eltype;
15407
15408 if (is_load && k == 1)
15409 {
15410 /* Neon load patterns always have the memory operand
15411 (a SImode pointer) in the operand 1 position. We
15412 want a const pointer to the element type in that
15413 position. */
15414 gcc_assert (insn_data[icode].operand[k].mode == SImode);
15415
15416 switch (1 << j)
15417 {
15418 case T_V8QI:
15419 case T_V16QI:
15420 eltype = const_intQI_pointer_node;
15421 break;
15422
15423 case T_V4HI:
15424 case T_V8HI:
15425 eltype = const_intHI_pointer_node;
15426 break;
15427
15428 case T_V2SI:
15429 case T_V4SI:
15430 eltype = const_intSI_pointer_node;
15431 break;
15432
15433 case T_V2SF:
15434 case T_V4SF:
15435 eltype = const_float_pointer_node;
15436 break;
15437
15438 case T_DI:
15439 case T_V2DI:
15440 eltype = const_intDI_pointer_node;
15441 break;
15442
15443 default: gcc_unreachable ();
15444 }
15445 }
15446 else if (is_store && k == 0)
15447 {
15448 /* Similarly, Neon store patterns use operand 0 as
15449 the memory location to store to (a SImode pointer).
15450 Use a pointer to the element type of the store in
15451 that position. */
15452 gcc_assert (insn_data[icode].operand[k].mode == SImode);
15453
15454 switch (1 << j)
15455 {
15456 case T_V8QI:
15457 case T_V16QI:
15458 eltype = intQI_pointer_node;
15459 break;
15460
15461 case T_V4HI:
15462 case T_V8HI:
15463 eltype = intHI_pointer_node;
15464 break;
15465
15466 case T_V2SI:
15467 case T_V4SI:
15468 eltype = intSI_pointer_node;
15469 break;
15470
15471 case T_V2SF:
15472 case T_V4SF:
15473 eltype = float_pointer_node;
15474 break;
15475
15476 case T_DI:
15477 case T_V2DI:
15478 eltype = intDI_pointer_node;
15479 break;
15480
15481 default: gcc_unreachable ();
15482 }
15483 }
15484 else
15485 {
15486 switch (insn_data[icode].operand[k].mode)
15487 {
15488 case VOIDmode: eltype = void_type_node; break;
15489 /* Scalars. */
15490 case QImode: eltype = neon_intQI_type_node; break;
15491 case HImode: eltype = neon_intHI_type_node; break;
15492 case SImode: eltype = neon_intSI_type_node; break;
15493 case SFmode: eltype = neon_float_type_node; break;
15494 case DImode: eltype = neon_intDI_type_node; break;
15495 case TImode: eltype = intTI_type_node; break;
15496 case EImode: eltype = intEI_type_node; break;
15497 case OImode: eltype = intOI_type_node; break;
15498 case CImode: eltype = intCI_type_node; break;
15499 case XImode: eltype = intXI_type_node; break;
15500 /* 64-bit vectors. */
15501 case V8QImode: eltype = V8QI_type_node; break;
15502 case V4HImode: eltype = V4HI_type_node; break;
15503 case V2SImode: eltype = V2SI_type_node; break;
15504 case V2SFmode: eltype = V2SF_type_node; break;
15505 /* 128-bit vectors. */
15506 case V16QImode: eltype = V16QI_type_node; break;
15507 case V8HImode: eltype = V8HI_type_node; break;
15508 case V4SImode: eltype = V4SI_type_node; break;
15509 case V4SFmode: eltype = V4SF_type_node; break;
15510 case V2DImode: eltype = V2DI_type_node; break;
15511 default: gcc_unreachable ();
15512 }
15513 }
15514
15515 if (k == 0 && !is_store)
15516 return_type = eltype;
15517 else
15518 args = tree_cons (NULL_TREE, eltype, args);
15519 }
15520
15521 ftype = build_function_type (return_type, args);
15522 }
15523 break;
15524
15525 case NEON_RESULTPAIR:
15526 {
15527 switch (insn_data[icode].operand[1].mode)
15528 {
15529 case V8QImode: ftype = void_ftype_pv8qi_v8qi_v8qi; break;
15530 case V4HImode: ftype = void_ftype_pv4hi_v4hi_v4hi; break;
15531 case V2SImode: ftype = void_ftype_pv2si_v2si_v2si; break;
15532 case V2SFmode: ftype = void_ftype_pv2sf_v2sf_v2sf; break;
15533 case DImode: ftype = void_ftype_pdi_di_di; break;
15534 case V16QImode: ftype = void_ftype_pv16qi_v16qi_v16qi; break;
15535 case V8HImode: ftype = void_ftype_pv8hi_v8hi_v8hi; break;
15536 case V4SImode: ftype = void_ftype_pv4si_v4si_v4si; break;
15537 case V4SFmode: ftype = void_ftype_pv4sf_v4sf_v4sf; break;
15538 case V2DImode: ftype = void_ftype_pv2di_v2di_v2di; break;
15539 default: gcc_unreachable ();
15540 }
15541 }
15542 break;
15543
15544 case NEON_REINTERP:
15545 {
15546 /* We iterate over 5 doubleword types, then 5 quadword
15547 types. */
15548 int rhs = j % 5;
15549 switch (insn_data[icode].operand[0].mode)
15550 {
15551 case V8QImode: ftype = reinterp_ftype_dreg[0][rhs]; break;
15552 case V4HImode: ftype = reinterp_ftype_dreg[1][rhs]; break;
15553 case V2SImode: ftype = reinterp_ftype_dreg[2][rhs]; break;
15554 case V2SFmode: ftype = reinterp_ftype_dreg[3][rhs]; break;
15555 case DImode: ftype = reinterp_ftype_dreg[4][rhs]; break;
15556 case V16QImode: ftype = reinterp_ftype_qreg[0][rhs]; break;
15557 case V8HImode: ftype = reinterp_ftype_qreg[1][rhs]; break;
15558 case V4SImode: ftype = reinterp_ftype_qreg[2][rhs]; break;
15559 case V4SFmode: ftype = reinterp_ftype_qreg[3][rhs]; break;
15560 case V2DImode: ftype = reinterp_ftype_qreg[4][rhs]; break;
15561 default: gcc_unreachable ();
15562 }
15563 }
15564 break;
15565
15566 default:
15567 gcc_unreachable ();
15568 }
15569
15570 gcc_assert (ftype != NULL);
15571
15572 sprintf (namebuf, "__builtin_neon_%s%s", d->name, modenames[j]);
15573
15574 add_builtin_function (namebuf, ftype, fcode++, BUILT_IN_MD, NULL,
15575 NULL_TREE);
15576 }
15577 }
15578}
15579
5a9335ef
NC
15580static void
15581arm_init_builtins (void)
15582{
d3585b76
DJ
15583 arm_init_tls_builtins ();
15584
5a9335ef
NC
15585 if (TARGET_REALLY_IWMMXT)
15586 arm_init_iwmmxt_builtins ();
88f77cba
JB
15587
15588 if (TARGET_NEON)
15589 arm_init_neon_builtins ();
5a9335ef
NC
15590}
15591
15592/* Errors in the source file can cause expand_expr to return const0_rtx
15593 where we expect a vector. To avoid crashing, use one of the vector
15594 clear instructions. */
15595
15596static rtx
15597safe_vector_operand (rtx x, enum machine_mode mode)
15598{
15599 if (x != const0_rtx)
15600 return x;
15601 x = gen_reg_rtx (mode);
15602
15603 emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
15604 : gen_rtx_SUBREG (DImode, x, 0)));
15605 return x;
15606}
15607
15608/* Subroutine of arm_expand_builtin to take care of binop insns. */
15609
15610static rtx
15611arm_expand_binop_builtin (enum insn_code icode,
5039610b 15612 tree exp, rtx target)
5a9335ef
NC
15613{
15614 rtx pat;
5039610b
SL
15615 tree arg0 = CALL_EXPR_ARG (exp, 0);
15616 tree arg1 = CALL_EXPR_ARG (exp, 1);
84217346
MD
15617 rtx op0 = expand_normal (arg0);
15618 rtx op1 = expand_normal (arg1);
5a9335ef
NC
15619 enum machine_mode tmode = insn_data[icode].operand[0].mode;
15620 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
15621 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
15622
15623 if (VECTOR_MODE_P (mode0))
15624 op0 = safe_vector_operand (op0, mode0);
15625 if (VECTOR_MODE_P (mode1))
15626 op1 = safe_vector_operand (op1, mode1);
15627
15628 if (! target
15629 || GET_MODE (target) != tmode
15630 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15631 target = gen_reg_rtx (tmode);
15632
e6d29d15 15633 gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
5a9335ef
NC
15634
15635 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15636 op0 = copy_to_mode_reg (mode0, op0);
15637 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
15638 op1 = copy_to_mode_reg (mode1, op1);
15639
15640 pat = GEN_FCN (icode) (target, op0, op1);
15641 if (! pat)
15642 return 0;
15643 emit_insn (pat);
15644 return target;
15645}
15646
15647/* Subroutine of arm_expand_builtin to take care of unop insns. */
15648
15649static rtx
15650arm_expand_unop_builtin (enum insn_code icode,
5039610b 15651 tree exp, rtx target, int do_load)
5a9335ef
NC
15652{
15653 rtx pat;
5039610b 15654 tree arg0 = CALL_EXPR_ARG (exp, 0);
84217346 15655 rtx op0 = expand_normal (arg0);
5a9335ef
NC
15656 enum machine_mode tmode = insn_data[icode].operand[0].mode;
15657 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
15658
15659 if (! target
15660 || GET_MODE (target) != tmode
15661 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15662 target = gen_reg_rtx (tmode);
15663 if (do_load)
15664 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
15665 else
15666 {
15667 if (VECTOR_MODE_P (mode0))
15668 op0 = safe_vector_operand (op0, mode0);
15669
15670 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15671 op0 = copy_to_mode_reg (mode0, op0);
15672 }
15673
15674 pat = GEN_FCN (icode) (target, op0);
15675 if (! pat)
15676 return 0;
15677 emit_insn (pat);
15678 return target;
15679}
15680
88f77cba
JB
15681static int
15682neon_builtin_compare (const void *a, const void *b)
15683{
5ead67f6
KG
15684 const neon_builtin_datum *const key = (const neon_builtin_datum *) a;
15685 const neon_builtin_datum *const memb = (const neon_builtin_datum *) b;
88f77cba
JB
15686 unsigned int soughtcode = key->base_fcode;
15687
15688 if (soughtcode >= memb->base_fcode
15689 && soughtcode < memb->base_fcode + memb->num_vars)
15690 return 0;
15691 else if (soughtcode < memb->base_fcode)
15692 return -1;
15693 else
15694 return 1;
15695}
15696
15697static enum insn_code
15698locate_neon_builtin_icode (int fcode, neon_itype *itype)
15699{
15700 neon_builtin_datum key, *found;
15701 int idx;
15702
15703 key.base_fcode = fcode;
5ead67f6
KG
15704 found = (neon_builtin_datum *)
15705 bsearch (&key, &neon_builtin_data[0], ARRAY_SIZE (neon_builtin_data),
88f77cba
JB
15706 sizeof (neon_builtin_data[0]), neon_builtin_compare);
15707 gcc_assert (found);
15708 idx = fcode - (int) found->base_fcode;
15709 gcc_assert (idx >= 0 && idx < T_MAX && idx < (int)found->num_vars);
15710
15711 if (itype)
15712 *itype = found->itype;
15713
15714 return found->codes[idx];
15715}
15716
15717typedef enum {
15718 NEON_ARG_COPY_TO_REG,
15719 NEON_ARG_CONSTANT,
15720 NEON_ARG_STOP
15721} builtin_arg;
15722
15723#define NEON_MAX_BUILTIN_ARGS 5
15724
15725/* Expand a Neon builtin. */
15726static rtx
15727arm_expand_neon_args (rtx target, int icode, int have_retval,
15728 tree exp, ...)
15729{
15730 va_list ap;
15731 rtx pat;
15732 tree arg[NEON_MAX_BUILTIN_ARGS];
15733 rtx op[NEON_MAX_BUILTIN_ARGS];
15734 enum machine_mode tmode = insn_data[icode].operand[0].mode;
15735 enum machine_mode mode[NEON_MAX_BUILTIN_ARGS];
15736 int argc = 0;
15737
15738 if (have_retval
15739 && (!target
15740 || GET_MODE (target) != tmode
15741 || !(*insn_data[icode].operand[0].predicate) (target, tmode)))
15742 target = gen_reg_rtx (tmode);
15743
15744 va_start (ap, exp);
15745
15746 for (;;)
15747 {
15748 builtin_arg thisarg = va_arg (ap, int);
15749
15750 if (thisarg == NEON_ARG_STOP)
15751 break;
15752 else
15753 {
15754 arg[argc] = CALL_EXPR_ARG (exp, argc);
15755 op[argc] = expand_normal (arg[argc]);
15756 mode[argc] = insn_data[icode].operand[argc + have_retval].mode;
15757
15758 switch (thisarg)
15759 {
15760 case NEON_ARG_COPY_TO_REG:
15761 /*gcc_assert (GET_MODE (op[argc]) == mode[argc]);*/
15762 if (!(*insn_data[icode].operand[argc + have_retval].predicate)
15763 (op[argc], mode[argc]))
15764 op[argc] = copy_to_mode_reg (mode[argc], op[argc]);
15765 break;
15766
15767 case NEON_ARG_CONSTANT:
15768 /* FIXME: This error message is somewhat unhelpful. */
15769 if (!(*insn_data[icode].operand[argc + have_retval].predicate)
15770 (op[argc], mode[argc]))
15771 error ("argument must be a constant");
15772 break;
15773
15774 case NEON_ARG_STOP:
15775 gcc_unreachable ();
15776 }
15777
15778 argc++;
15779 }
15780 }
15781
15782 va_end (ap);
15783
15784 if (have_retval)
15785 switch (argc)
15786 {
15787 case 1:
15788 pat = GEN_FCN (icode) (target, op[0]);
15789 break;
15790
15791 case 2:
15792 pat = GEN_FCN (icode) (target, op[0], op[1]);
15793 break;
15794
15795 case 3:
15796 pat = GEN_FCN (icode) (target, op[0], op[1], op[2]);
15797 break;
15798
15799 case 4:
15800 pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3]);
15801 break;
15802
15803 case 5:
15804 pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3], op[4]);
15805 break;
15806
15807 default:
15808 gcc_unreachable ();
15809 }
15810 else
15811 switch (argc)
15812 {
15813 case 1:
15814 pat = GEN_FCN (icode) (op[0]);
15815 break;
15816
15817 case 2:
15818 pat = GEN_FCN (icode) (op[0], op[1]);
15819 break;
15820
15821 case 3:
15822 pat = GEN_FCN (icode) (op[0], op[1], op[2]);
15823 break;
15824
15825 case 4:
15826 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
15827 break;
15828
15829 case 5:
15830 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4]);
15831 break;
15832
15833 default:
15834 gcc_unreachable ();
15835 }
15836
15837 if (!pat)
15838 return 0;
15839
15840 emit_insn (pat);
15841
15842 return target;
15843}
15844
15845/* Expand a Neon builtin. These are "special" because they don't have symbolic
15846 constants defined per-instruction or per instruction-variant. Instead, the
15847 required info is looked up in the table neon_builtin_data. */
15848static rtx
15849arm_expand_neon_builtin (int fcode, tree exp, rtx target)
15850{
15851 neon_itype itype;
15852 enum insn_code icode = locate_neon_builtin_icode (fcode, &itype);
15853
15854 switch (itype)
15855 {
15856 case NEON_UNOP:
15857 case NEON_CONVERT:
15858 case NEON_DUPLANE:
15859 return arm_expand_neon_args (target, icode, 1, exp,
15860 NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_STOP);
15861
15862 case NEON_BINOP:
15863 case NEON_SETLANE:
15864 case NEON_SCALARMUL:
15865 case NEON_SCALARMULL:
15866 case NEON_SCALARMULH:
15867 case NEON_SHIFTINSERT:
15868 case NEON_LOGICBINOP:
15869 return arm_expand_neon_args (target, icode, 1, exp,
15870 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
15871 NEON_ARG_STOP);
15872
15873 case NEON_TERNOP:
15874 return arm_expand_neon_args (target, icode, 1, exp,
15875 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
15876 NEON_ARG_CONSTANT, NEON_ARG_STOP);
15877
15878 case NEON_GETLANE:
15879 case NEON_FIXCONV:
15880 case NEON_SHIFTIMM:
15881 return arm_expand_neon_args (target, icode, 1, exp,
15882 NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_CONSTANT,
15883 NEON_ARG_STOP);
15884
15885 case NEON_CREATE:
15886 return arm_expand_neon_args (target, icode, 1, exp,
15887 NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
15888
15889 case NEON_DUP:
15890 case NEON_SPLIT:
15891 case NEON_REINTERP:
15892 return arm_expand_neon_args (target, icode, 1, exp,
15893 NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
15894
15895 case NEON_COMBINE:
15896 case NEON_VTBL:
15897 return arm_expand_neon_args (target, icode, 1, exp,
15898 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
15899
15900 case NEON_RESULTPAIR:
15901 return arm_expand_neon_args (target, icode, 0, exp,
15902 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
15903 NEON_ARG_STOP);
15904
15905 case NEON_LANEMUL:
15906 case NEON_LANEMULL:
15907 case NEON_LANEMULH:
15908 return arm_expand_neon_args (target, icode, 1, exp,
15909 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
15910 NEON_ARG_CONSTANT, NEON_ARG_STOP);
15911
15912 case NEON_LANEMAC:
15913 return arm_expand_neon_args (target, icode, 1, exp,
15914 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
15915 NEON_ARG_CONSTANT, NEON_ARG_CONSTANT, NEON_ARG_STOP);
15916
15917 case NEON_SHIFTACC:
15918 return arm_expand_neon_args (target, icode, 1, exp,
15919 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
15920 NEON_ARG_CONSTANT, NEON_ARG_STOP);
15921
15922 case NEON_SCALARMAC:
15923 return arm_expand_neon_args (target, icode, 1, exp,
15924 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
15925 NEON_ARG_CONSTANT, NEON_ARG_STOP);
15926
15927 case NEON_SELECT:
15928 case NEON_VTBX:
15929 return arm_expand_neon_args (target, icode, 1, exp,
15930 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
15931 NEON_ARG_STOP);
15932
15933 case NEON_LOAD1:
15934 case NEON_LOADSTRUCT:
15935 return arm_expand_neon_args (target, icode, 1, exp,
15936 NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
15937
15938 case NEON_LOAD1LANE:
15939 case NEON_LOADSTRUCTLANE:
15940 return arm_expand_neon_args (target, icode, 1, exp,
15941 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
15942 NEON_ARG_STOP);
15943
15944 case NEON_STORE1:
15945 case NEON_STORESTRUCT:
15946 return arm_expand_neon_args (target, icode, 0, exp,
15947 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
15948
15949 case NEON_STORE1LANE:
15950 case NEON_STORESTRUCTLANE:
15951 return arm_expand_neon_args (target, icode, 0, exp,
15952 NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
15953 NEON_ARG_STOP);
15954 }
15955
15956 gcc_unreachable ();
15957}
15958
15959/* Emit code to reinterpret one Neon type as another, without altering bits. */
15960void
15961neon_reinterpret (rtx dest, rtx src)
15962{
15963 emit_move_insn (dest, gen_lowpart (GET_MODE (dest), src));
15964}
15965
15966/* Emit code to place a Neon pair result in memory locations (with equal
15967 registers). */
15968void
15969neon_emit_pair_result_insn (enum machine_mode mode,
15970 rtx (*intfn) (rtx, rtx, rtx, rtx), rtx destaddr,
15971 rtx op1, rtx op2)
15972{
15973 rtx mem = gen_rtx_MEM (mode, destaddr);
15974 rtx tmp1 = gen_reg_rtx (mode);
15975 rtx tmp2 = gen_reg_rtx (mode);
15976
15977 emit_insn (intfn (tmp1, op1, tmp2, op2));
15978
15979 emit_move_insn (mem, tmp1);
15980 mem = adjust_address (mem, mode, GET_MODE_SIZE (mode));
15981 emit_move_insn (mem, tmp2);
15982}
15983
15984/* Set up operands for a register copy from src to dest, taking care not to
15985 clobber registers in the process.
15986 FIXME: This has rather high polynomial complexity (O(n^3)?) but shouldn't
15987 be called with a large N, so that should be OK. */
15988
15989void
15990neon_disambiguate_copy (rtx *operands, rtx *dest, rtx *src, unsigned int count)
15991{
15992 unsigned int copied = 0, opctr = 0;
15993 unsigned int done = (1 << count) - 1;
15994 unsigned int i, j;
15995
15996 while (copied != done)
15997 {
15998 for (i = 0; i < count; i++)
15999 {
16000 int good = 1;
16001
16002 for (j = 0; good && j < count; j++)
16003 if (i != j && (copied & (1 << j)) == 0
16004 && reg_overlap_mentioned_p (src[j], dest[i]))
16005 good = 0;
16006
16007 if (good)
16008 {
16009 operands[opctr++] = dest[i];
16010 operands[opctr++] = src[i];
16011 copied |= 1 << i;
16012 }
16013 }
16014 }
16015
16016 gcc_assert (opctr == count * 2);
16017}
16018
5a9335ef
NC
16019/* Expand an expression EXP that calls a built-in function,
16020 with result going to TARGET if that's convenient
16021 (and in mode MODE if that's convenient).
16022 SUBTARGET may be used as the target for computing one of EXP's operands.
16023 IGNORE is nonzero if the value is to be ignored. */
16024
16025static rtx
16026arm_expand_builtin (tree exp,
16027 rtx target,
16028 rtx subtarget ATTRIBUTE_UNUSED,
16029 enum machine_mode mode ATTRIBUTE_UNUSED,
16030 int ignore ATTRIBUTE_UNUSED)
16031{
16032 const struct builtin_description * d;
16033 enum insn_code icode;
5039610b 16034 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
5a9335ef
NC
16035 tree arg0;
16036 tree arg1;
16037 tree arg2;
16038 rtx op0;
16039 rtx op1;
16040 rtx op2;
16041 rtx pat;
16042 int fcode = DECL_FUNCTION_CODE (fndecl);
16043 size_t i;
16044 enum machine_mode tmode;
16045 enum machine_mode mode0;
16046 enum machine_mode mode1;
16047 enum machine_mode mode2;
16048
88f77cba
JB
16049 if (fcode >= ARM_BUILTIN_NEON_BASE)
16050 return arm_expand_neon_builtin (fcode, exp, target);
16051
5a9335ef
NC
16052 switch (fcode)
16053 {
16054 case ARM_BUILTIN_TEXTRMSB:
16055 case ARM_BUILTIN_TEXTRMUB:
16056 case ARM_BUILTIN_TEXTRMSH:
16057 case ARM_BUILTIN_TEXTRMUH:
16058 case ARM_BUILTIN_TEXTRMSW:
16059 case ARM_BUILTIN_TEXTRMUW:
16060 icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
16061 : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
16062 : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
16063 : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
16064 : CODE_FOR_iwmmxt_textrmw);
16065
5039610b
SL
16066 arg0 = CALL_EXPR_ARG (exp, 0);
16067 arg1 = CALL_EXPR_ARG (exp, 1);
84217346
MD
16068 op0 = expand_normal (arg0);
16069 op1 = expand_normal (arg1);
5a9335ef
NC
16070 tmode = insn_data[icode].operand[0].mode;
16071 mode0 = insn_data[icode].operand[1].mode;
16072 mode1 = insn_data[icode].operand[2].mode;
16073
16074 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16075 op0 = copy_to_mode_reg (mode0, op0);
16076 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
16077 {
16078 /* @@@ better error message */
16079 error ("selector must be an immediate");
16080 return gen_reg_rtx (tmode);
16081 }
16082 if (target == 0
16083 || GET_MODE (target) != tmode
16084 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16085 target = gen_reg_rtx (tmode);
16086 pat = GEN_FCN (icode) (target, op0, op1);
16087 if (! pat)
16088 return 0;
16089 emit_insn (pat);
16090 return target;
16091
16092 case ARM_BUILTIN_TINSRB:
16093 case ARM_BUILTIN_TINSRH:
16094 case ARM_BUILTIN_TINSRW:
16095 icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
16096 : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
16097 : CODE_FOR_iwmmxt_tinsrw);
5039610b
SL
16098 arg0 = CALL_EXPR_ARG (exp, 0);
16099 arg1 = CALL_EXPR_ARG (exp, 1);
16100 arg2 = CALL_EXPR_ARG (exp, 2);
84217346
MD
16101 op0 = expand_normal (arg0);
16102 op1 = expand_normal (arg1);
16103 op2 = expand_normal (arg2);
5a9335ef
NC
16104 tmode = insn_data[icode].operand[0].mode;
16105 mode0 = insn_data[icode].operand[1].mode;
16106 mode1 = insn_data[icode].operand[2].mode;
16107 mode2 = insn_data[icode].operand[3].mode;
16108
16109 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16110 op0 = copy_to_mode_reg (mode0, op0);
16111 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
16112 op1 = copy_to_mode_reg (mode1, op1);
16113 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
16114 {
16115 /* @@@ better error message */
16116 error ("selector must be an immediate");
16117 return const0_rtx;
16118 }
16119 if (target == 0
16120 || GET_MODE (target) != tmode
16121 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16122 target = gen_reg_rtx (tmode);
16123 pat = GEN_FCN (icode) (target, op0, op1, op2);
16124 if (! pat)
16125 return 0;
16126 emit_insn (pat);
16127 return target;
16128
16129 case ARM_BUILTIN_SETWCX:
5039610b
SL
16130 arg0 = CALL_EXPR_ARG (exp, 0);
16131 arg1 = CALL_EXPR_ARG (exp, 1);
84217346
MD
16132 op0 = force_reg (SImode, expand_normal (arg0));
16133 op1 = expand_normal (arg1);
f07a6b21 16134 emit_insn (gen_iwmmxt_tmcr (op1, op0));
5a9335ef
NC
16135 return 0;
16136
16137 case ARM_BUILTIN_GETWCX:
5039610b 16138 arg0 = CALL_EXPR_ARG (exp, 0);
84217346 16139 op0 = expand_normal (arg0);
5a9335ef
NC
16140 target = gen_reg_rtx (SImode);
16141 emit_insn (gen_iwmmxt_tmrc (target, op0));
16142 return target;
16143
16144 case ARM_BUILTIN_WSHUFH:
16145 icode = CODE_FOR_iwmmxt_wshufh;
5039610b
SL
16146 arg0 = CALL_EXPR_ARG (exp, 0);
16147 arg1 = CALL_EXPR_ARG (exp, 1);
84217346
MD
16148 op0 = expand_normal (arg0);
16149 op1 = expand_normal (arg1);
5a9335ef
NC
16150 tmode = insn_data[icode].operand[0].mode;
16151 mode1 = insn_data[icode].operand[1].mode;
16152 mode2 = insn_data[icode].operand[2].mode;
16153
16154 if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
16155 op0 = copy_to_mode_reg (mode1, op0);
16156 if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
16157 {
16158 /* @@@ better error message */
16159 error ("mask must be an immediate");
16160 return const0_rtx;
16161 }
16162 if (target == 0
16163 || GET_MODE (target) != tmode
16164 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16165 target = gen_reg_rtx (tmode);
16166 pat = GEN_FCN (icode) (target, op0, op1);
16167 if (! pat)
16168 return 0;
16169 emit_insn (pat);
16170 return target;
16171
16172 case ARM_BUILTIN_WSADB:
5039610b 16173 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, exp, target);
5a9335ef 16174 case ARM_BUILTIN_WSADH:
5039610b 16175 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, exp, target);
5a9335ef 16176 case ARM_BUILTIN_WSADBZ:
5039610b 16177 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, exp, target);
5a9335ef 16178 case ARM_BUILTIN_WSADHZ:
5039610b 16179 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, exp, target);
5a9335ef
NC
16180
16181 /* Several three-argument builtins. */
16182 case ARM_BUILTIN_WMACS:
16183 case ARM_BUILTIN_WMACU:
16184 case ARM_BUILTIN_WALIGN:
16185 case ARM_BUILTIN_TMIA:
16186 case ARM_BUILTIN_TMIAPH:
16187 case ARM_BUILTIN_TMIATT:
16188 case ARM_BUILTIN_TMIATB:
16189 case ARM_BUILTIN_TMIABT:
16190 case ARM_BUILTIN_TMIABB:
16191 icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
16192 : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
16193 : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
16194 : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
16195 : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
16196 : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
16197 : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
16198 : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
16199 : CODE_FOR_iwmmxt_walign);
5039610b
SL
16200 arg0 = CALL_EXPR_ARG (exp, 0);
16201 arg1 = CALL_EXPR_ARG (exp, 1);
16202 arg2 = CALL_EXPR_ARG (exp, 2);
84217346
MD
16203 op0 = expand_normal (arg0);
16204 op1 = expand_normal (arg1);
16205 op2 = expand_normal (arg2);
5a9335ef
NC
16206 tmode = insn_data[icode].operand[0].mode;
16207 mode0 = insn_data[icode].operand[1].mode;
16208 mode1 = insn_data[icode].operand[2].mode;
16209 mode2 = insn_data[icode].operand[3].mode;
16210
16211 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16212 op0 = copy_to_mode_reg (mode0, op0);
16213 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
16214 op1 = copy_to_mode_reg (mode1, op1);
16215 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
16216 op2 = copy_to_mode_reg (mode2, op2);
16217 if (target == 0
16218 || GET_MODE (target) != tmode
16219 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16220 target = gen_reg_rtx (tmode);
16221 pat = GEN_FCN (icode) (target, op0, op1, op2);
16222 if (! pat)
16223 return 0;
16224 emit_insn (pat);
16225 return target;
f676971a 16226
5a9335ef
NC
16227 case ARM_BUILTIN_WZERO:
16228 target = gen_reg_rtx (DImode);
16229 emit_insn (gen_iwmmxt_clrdi (target));
16230 return target;
16231
d3585b76
DJ
16232 case ARM_BUILTIN_THREAD_POINTER:
16233 return arm_load_tp (target);
16234
5a9335ef
NC
16235 default:
16236 break;
16237 }
16238
e97a46ce 16239 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
5a9335ef 16240 if (d->code == (const enum arm_builtins) fcode)
5039610b 16241 return arm_expand_binop_builtin (d->icode, exp, target);
5a9335ef 16242
e97a46ce 16243 for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
5a9335ef 16244 if (d->code == (const enum arm_builtins) fcode)
5039610b 16245 return arm_expand_unop_builtin (d->icode, exp, target, 0);
5a9335ef
NC
16246
16247 /* @@@ Should really do something sensible here. */
16248 return NULL_RTX;
16249}
16250\f
1d6e90ac
NC
16251/* Return the number (counting from 0) of
16252 the least significant set bit in MASK. */
16253
e32bac5b 16254inline static int
b279b20a 16255number_of_first_bit_set (unsigned mask)
d5b7b3ae
RE
16256{
16257 int bit;
16258
16259 for (bit = 0;
16260 (mask & (1 << bit)) == 0;
5895f793 16261 ++bit)
d5b7b3ae
RE
16262 continue;
16263
16264 return bit;
16265}
16266
b279b20a
NC
16267/* Emit code to push or pop registers to or from the stack. F is the
16268 assembly file. MASK is the registers to push or pop. PUSH is
16269 nonzero if we should push, and zero if we should pop. For debugging
16270 output, if pushing, adjust CFA_OFFSET by the amount of space added
16271 to the stack. REAL_REGS should have the same number of bits set as
16272 MASK, and will be used instead (in the same order) to describe which
16273 registers were saved - this is used to mark the save slots when we
16274 push high registers after moving them to low registers. */
16275static void
16276thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
16277 unsigned long real_regs)
16278{
16279 int regno;
16280 int lo_mask = mask & 0xFF;
16281 int pushed_words = 0;
16282
e6d29d15 16283 gcc_assert (mask);
b279b20a
NC
16284
16285 if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
16286 {
16287 /* Special case. Do not generate a POP PC statement here, do it in
16288 thumb_exit() */
16289 thumb_exit (f, -1);
16290 return;
16291 }
16292
617a1b71
PB
16293 if (ARM_EABI_UNWIND_TABLES && push)
16294 {
16295 fprintf (f, "\t.save\t{");
16296 for (regno = 0; regno < 15; regno++)
16297 {
16298 if (real_regs & (1 << regno))
16299 {
16300 if (real_regs & ((1 << regno) -1))
16301 fprintf (f, ", ");
16302 asm_fprintf (f, "%r", regno);
16303 }
16304 }
16305 fprintf (f, "}\n");
16306 }
16307
b279b20a
NC
16308 fprintf (f, "\t%s\t{", push ? "push" : "pop");
16309
16310 /* Look at the low registers first. */
16311 for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
16312 {
16313 if (lo_mask & 1)
16314 {
16315 asm_fprintf (f, "%r", regno);
16316
16317 if ((lo_mask & ~1) != 0)
16318 fprintf (f, ", ");
16319
16320 pushed_words++;
16321 }
16322 }
16323
16324 if (push && (mask & (1 << LR_REGNUM)))
16325 {
16326 /* Catch pushing the LR. */
16327 if (mask & 0xFF)
16328 fprintf (f, ", ");
16329
16330 asm_fprintf (f, "%r", LR_REGNUM);
16331
16332 pushed_words++;
16333 }
16334 else if (!push && (mask & (1 << PC_REGNUM)))
16335 {
16336 /* Catch popping the PC. */
16337 if (TARGET_INTERWORK || TARGET_BACKTRACE
e3b5732b 16338 || crtl->calls_eh_return)
b279b20a
NC
16339 {
16340 /* The PC is never poped directly, instead
16341 it is popped into r3 and then BX is used. */
16342 fprintf (f, "}\n");
16343
16344 thumb_exit (f, -1);
16345
16346 return;
16347 }
16348 else
16349 {
16350 if (mask & 0xFF)
16351 fprintf (f, ", ");
16352
16353 asm_fprintf (f, "%r", PC_REGNUM);
16354 }
16355 }
16356
16357 fprintf (f, "}\n");
16358
16359 if (push && pushed_words && dwarf2out_do_frame ())
16360 {
16361 char *l = dwarf2out_cfi_label ();
16362 int pushed_mask = real_regs;
16363
16364 *cfa_offset += pushed_words * 4;
16365 dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
16366
16367 pushed_words = 0;
16368 pushed_mask = real_regs;
16369 for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
16370 {
16371 if (pushed_mask & 1)
16372 dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
16373 }
16374 }
16375}
16376
d5b7b3ae
RE
16377/* Generate code to return from a thumb function.
16378 If 'reg_containing_return_addr' is -1, then the return address is
16379 actually on the stack, at the stack pointer. */
16380static void
c9ca9b88 16381thumb_exit (FILE *f, int reg_containing_return_addr)
d5b7b3ae
RE
16382{
16383 unsigned regs_available_for_popping;
16384 unsigned regs_to_pop;
16385 int pops_needed;
16386 unsigned available;
16387 unsigned required;
16388 int mode;
16389 int size;
16390 int restore_a4 = FALSE;
16391
16392 /* Compute the registers we need to pop. */
16393 regs_to_pop = 0;
16394 pops_needed = 0;
16395
c9ca9b88 16396 if (reg_containing_return_addr == -1)
d5b7b3ae 16397 {
d5b7b3ae 16398 regs_to_pop |= 1 << LR_REGNUM;
5895f793 16399 ++pops_needed;
d5b7b3ae
RE
16400 }
16401
16402 if (TARGET_BACKTRACE)
16403 {
16404 /* Restore the (ARM) frame pointer and stack pointer. */
16405 regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
16406 pops_needed += 2;
16407 }
16408
16409 /* If there is nothing to pop then just emit the BX instruction and
16410 return. */
16411 if (pops_needed == 0)
16412 {
e3b5732b 16413 if (crtl->calls_eh_return)
c9ca9b88 16414 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
d5b7b3ae
RE
16415
16416 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
16417 return;
16418 }
16419 /* Otherwise if we are not supporting interworking and we have not created
16420 a backtrace structure and the function was not entered in ARM mode then
16421 just pop the return address straight into the PC. */
5895f793
RE
16422 else if (!TARGET_INTERWORK
16423 && !TARGET_BACKTRACE
c9ca9b88 16424 && !is_called_in_ARM_mode (current_function_decl)
e3b5732b 16425 && !crtl->calls_eh_return)
d5b7b3ae 16426 {
c9ca9b88 16427 asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
d5b7b3ae
RE
16428 return;
16429 }
16430
16431 /* Find out how many of the (return) argument registers we can corrupt. */
16432 regs_available_for_popping = 0;
16433
16434 /* If returning via __builtin_eh_return, the bottom three registers
16435 all contain information needed for the return. */
e3b5732b 16436 if (crtl->calls_eh_return)
d5b7b3ae
RE
16437 size = 12;
16438 else
16439 {
d5b7b3ae
RE
16440 /* If we can deduce the registers used from the function's
16441 return value. This is more reliable that examining
6fb5fa3c 16442 df_regs_ever_live_p () because that will be set if the register is
d5b7b3ae
RE
16443 ever used in the function, not just if the register is used
16444 to hold a return value. */
16445
38173d38
JH
16446 if (crtl->return_rtx != 0)
16447 mode = GET_MODE (crtl->return_rtx);
d5b7b3ae 16448 else
d5b7b3ae
RE
16449 mode = DECL_MODE (DECL_RESULT (current_function_decl));
16450
16451 size = GET_MODE_SIZE (mode);
16452
16453 if (size == 0)
16454 {
16455 /* In a void function we can use any argument register.
16456 In a function that returns a structure on the stack
16457 we can use the second and third argument registers. */
16458 if (mode == VOIDmode)
16459 regs_available_for_popping =
16460 (1 << ARG_REGISTER (1))
16461 | (1 << ARG_REGISTER (2))
16462 | (1 << ARG_REGISTER (3));
16463 else
16464 regs_available_for_popping =
16465 (1 << ARG_REGISTER (2))
16466 | (1 << ARG_REGISTER (3));
16467 }
16468 else if (size <= 4)
16469 regs_available_for_popping =
16470 (1 << ARG_REGISTER (2))
16471 | (1 << ARG_REGISTER (3));
16472 else if (size <= 8)
16473 regs_available_for_popping =
16474 (1 << ARG_REGISTER (3));
16475 }
16476
16477 /* Match registers to be popped with registers into which we pop them. */
16478 for (available = regs_available_for_popping,
16479 required = regs_to_pop;
16480 required != 0 && available != 0;
16481 available &= ~(available & - available),
16482 required &= ~(required & - required))
16483 -- pops_needed;
16484
16485 /* If we have any popping registers left over, remove them. */
16486 if (available > 0)
5895f793 16487 regs_available_for_popping &= ~available;
f676971a 16488
d5b7b3ae
RE
16489 /* Otherwise if we need another popping register we can use
16490 the fourth argument register. */
16491 else if (pops_needed)
16492 {
16493 /* If we have not found any free argument registers and
16494 reg a4 contains the return address, we must move it. */
16495 if (regs_available_for_popping == 0
16496 && reg_containing_return_addr == LAST_ARG_REGNUM)
16497 {
16498 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
16499 reg_containing_return_addr = LR_REGNUM;
16500 }
16501 else if (size > 12)
16502 {
16503 /* Register a4 is being used to hold part of the return value,
16504 but we have dire need of a free, low register. */
16505 restore_a4 = TRUE;
f676971a 16506
d5b7b3ae
RE
16507 asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
16508 }
f676971a 16509
d5b7b3ae
RE
16510 if (reg_containing_return_addr != LAST_ARG_REGNUM)
16511 {
16512 /* The fourth argument register is available. */
16513 regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
f676971a 16514
5895f793 16515 --pops_needed;
d5b7b3ae
RE
16516 }
16517 }
16518
16519 /* Pop as many registers as we can. */
980e61bb
DJ
16520 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
16521 regs_available_for_popping);
d5b7b3ae
RE
16522
16523 /* Process the registers we popped. */
16524 if (reg_containing_return_addr == -1)
16525 {
16526 /* The return address was popped into the lowest numbered register. */
5895f793 16527 regs_to_pop &= ~(1 << LR_REGNUM);
f676971a 16528
d5b7b3ae
RE
16529 reg_containing_return_addr =
16530 number_of_first_bit_set (regs_available_for_popping);
16531
16532 /* Remove this register for the mask of available registers, so that
6bc82793 16533 the return address will not be corrupted by further pops. */
5895f793 16534 regs_available_for_popping &= ~(1 << reg_containing_return_addr);
d5b7b3ae
RE
16535 }
16536
16537 /* If we popped other registers then handle them here. */
16538 if (regs_available_for_popping)
16539 {
16540 int frame_pointer;
f676971a 16541
d5b7b3ae
RE
16542 /* Work out which register currently contains the frame pointer. */
16543 frame_pointer = number_of_first_bit_set (regs_available_for_popping);
16544
16545 /* Move it into the correct place. */
16546 asm_fprintf (f, "\tmov\t%r, %r\n",
16547 ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
16548
16549 /* (Temporarily) remove it from the mask of popped registers. */
5895f793
RE
16550 regs_available_for_popping &= ~(1 << frame_pointer);
16551 regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
f676971a 16552
d5b7b3ae
RE
16553 if (regs_available_for_popping)
16554 {
16555 int stack_pointer;
f676971a 16556
d5b7b3ae
RE
16557 /* We popped the stack pointer as well,
16558 find the register that contains it. */
16559 stack_pointer = number_of_first_bit_set (regs_available_for_popping);
16560
16561 /* Move it into the stack register. */
16562 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
f676971a 16563
d5b7b3ae
RE
16564 /* At this point we have popped all necessary registers, so
16565 do not worry about restoring regs_available_for_popping
16566 to its correct value:
16567
16568 assert (pops_needed == 0)
16569 assert (regs_available_for_popping == (1 << frame_pointer))
16570 assert (regs_to_pop == (1 << STACK_POINTER)) */
16571 }
16572 else
16573 {
16574 /* Since we have just move the popped value into the frame
16575 pointer, the popping register is available for reuse, and
16576 we know that we still have the stack pointer left to pop. */
16577 regs_available_for_popping |= (1 << frame_pointer);
16578 }
16579 }
f676971a 16580
d5b7b3ae
RE
16581 /* If we still have registers left on the stack, but we no longer have
16582 any registers into which we can pop them, then we must move the return
16583 address into the link register and make available the register that
16584 contained it. */
16585 if (regs_available_for_popping == 0 && pops_needed > 0)
16586 {
16587 regs_available_for_popping |= 1 << reg_containing_return_addr;
f676971a 16588
d5b7b3ae
RE
16589 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
16590 reg_containing_return_addr);
f676971a 16591
d5b7b3ae
RE
16592 reg_containing_return_addr = LR_REGNUM;
16593 }
16594
16595 /* If we have registers left on the stack then pop some more.
16596 We know that at most we will want to pop FP and SP. */
16597 if (pops_needed > 0)
16598 {
16599 int popped_into;
16600 int move_to;
f676971a 16601
980e61bb
DJ
16602 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
16603 regs_available_for_popping);
d5b7b3ae
RE
16604
16605 /* We have popped either FP or SP.
16606 Move whichever one it is into the correct register. */
16607 popped_into = number_of_first_bit_set (regs_available_for_popping);
16608 move_to = number_of_first_bit_set (regs_to_pop);
16609
16610 asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
16611
5895f793 16612 regs_to_pop &= ~(1 << move_to);
d5b7b3ae 16613
5895f793 16614 --pops_needed;
d5b7b3ae 16615 }
f676971a 16616
d5b7b3ae
RE
16617 /* If we still have not popped everything then we must have only
16618 had one register available to us and we are now popping the SP. */
16619 if (pops_needed > 0)
16620 {
16621 int popped_into;
f676971a 16622
980e61bb
DJ
16623 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
16624 regs_available_for_popping);
d5b7b3ae
RE
16625
16626 popped_into = number_of_first_bit_set (regs_available_for_popping);
16627
16628 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
16629 /*
16630 assert (regs_to_pop == (1 << STACK_POINTER))
16631 assert (pops_needed == 1)
16632 */
16633 }
16634
16635 /* If necessary restore the a4 register. */
16636 if (restore_a4)
16637 {
16638 if (reg_containing_return_addr != LR_REGNUM)
16639 {
16640 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
16641 reg_containing_return_addr = LR_REGNUM;
16642 }
f676971a 16643
d5b7b3ae
RE
16644 asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
16645 }
16646
e3b5732b 16647 if (crtl->calls_eh_return)
c9ca9b88 16648 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
d5b7b3ae
RE
16649
16650 /* Return to caller. */
16651 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
16652}
16653
d5b7b3ae
RE
16654\f
16655void
5b3e6663 16656thumb1_final_prescan_insn (rtx insn)
d5b7b3ae 16657{
d5b7b3ae 16658 if (flag_print_asm_name)
9d98a694
AO
16659 asm_fprintf (asm_out_file, "%@ 0x%04x\n",
16660 INSN_ADDRESSES (INSN_UID (insn)));
d5b7b3ae
RE
16661}
16662
16663int
e32bac5b 16664thumb_shiftable_const (unsigned HOST_WIDE_INT val)
d5b7b3ae
RE
16665{
16666 unsigned HOST_WIDE_INT mask = 0xff;
16667 int i;
16668
16669 if (val == 0) /* XXX */
16670 return 0;
f676971a 16671
d5b7b3ae
RE
16672 for (i = 0; i < 25; i++)
16673 if ((val & (mask << i)) == val)
16674 return 1;
16675
16676 return 0;
16677}
16678
825dda42 16679/* Returns nonzero if the current function contains,
d5b7b3ae 16680 or might contain a far jump. */
5848830f
PB
16681static int
16682thumb_far_jump_used_p (void)
d5b7b3ae
RE
16683{
16684 rtx insn;
16685
16686 /* This test is only important for leaf functions. */
5895f793 16687 /* assert (!leaf_function_p ()); */
f676971a 16688
d5b7b3ae
RE
16689 /* If we have already decided that far jumps may be used,
16690 do not bother checking again, and always return true even if
16691 it turns out that they are not being used. Once we have made
16692 the decision that far jumps are present (and that hence the link
16693 register will be pushed onto the stack) we cannot go back on it. */
16694 if (cfun->machine->far_jump_used)
16695 return 1;
16696
16697 /* If this function is not being called from the prologue/epilogue
16698 generation code then it must be being called from the
16699 INITIAL_ELIMINATION_OFFSET macro. */
5848830f 16700 if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
d5b7b3ae
RE
16701 {
16702 /* In this case we know that we are being asked about the elimination
16703 of the arg pointer register. If that register is not being used,
16704 then there are no arguments on the stack, and we do not have to
16705 worry that a far jump might force the prologue to push the link
16706 register, changing the stack offsets. In this case we can just
16707 return false, since the presence of far jumps in the function will
16708 not affect stack offsets.
16709
16710 If the arg pointer is live (or if it was live, but has now been
16711 eliminated and so set to dead) then we do have to test to see if
16712 the function might contain a far jump. This test can lead to some
16713 false negatives, since before reload is completed, then length of
16714 branch instructions is not known, so gcc defaults to returning their
16715 longest length, which in turn sets the far jump attribute to true.
16716
16717 A false negative will not result in bad code being generated, but it
16718 will result in a needless push and pop of the link register. We
5848830f
PB
16719 hope that this does not occur too often.
16720
16721 If we need doubleword stack alignment this could affect the other
16722 elimination offsets so we can't risk getting it wrong. */
6fb5fa3c 16723 if (df_regs_ever_live_p (ARG_POINTER_REGNUM))
d5b7b3ae 16724 cfun->machine->arg_pointer_live = 1;
5895f793 16725 else if (!cfun->machine->arg_pointer_live)
d5b7b3ae
RE
16726 return 0;
16727 }
16728
16729 /* Check to see if the function contains a branch
16730 insn with the far jump attribute set. */
16731 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
16732 {
16733 if (GET_CODE (insn) == JUMP_INSN
16734 /* Ignore tablejump patterns. */
16735 && GET_CODE (PATTERN (insn)) != ADDR_VEC
16736 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
16737 && get_attr_far_jump (insn) == FAR_JUMP_YES
16738 )
16739 {
9a9f7594 16740 /* Record the fact that we have decided that
d5b7b3ae
RE
16741 the function does use far jumps. */
16742 cfun->machine->far_jump_used = 1;
16743 return 1;
16744 }
16745 }
f676971a 16746
d5b7b3ae
RE
16747 return 0;
16748}
16749
825dda42 16750/* Return nonzero if FUNC must be entered in ARM mode. */
d5b7b3ae 16751int
e32bac5b 16752is_called_in_ARM_mode (tree func)
d5b7b3ae 16753{
e6d29d15 16754 gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
d5b7b3ae 16755
696e78bf 16756 /* Ignore the problem about functions whose address is taken. */
d5b7b3ae
RE
16757 if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
16758 return TRUE;
16759
f676971a 16760#ifdef ARM_PE
91d231cb 16761 return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
d5b7b3ae
RE
16762#else
16763 return FALSE;
16764#endif
16765}
16766
d6b4baa4 16767/* The bits which aren't usefully expanded as rtl. */
cd2b33d0 16768const char *
e32bac5b 16769thumb_unexpanded_epilogue (void)
d5b7b3ae 16770{
954954d1 16771 arm_stack_offsets *offsets;
d5b7b3ae 16772 int regno;
b279b20a 16773 unsigned long live_regs_mask = 0;
d5b7b3ae 16774 int high_regs_pushed = 0;
d5b7b3ae 16775 int had_to_push_lr;
57934c39 16776 int size;
d5b7b3ae
RE
16777
16778 if (return_used_this_function)
16779 return "";
16780
58e60158
AN
16781 if (IS_NAKED (arm_current_func_type ()))
16782 return "";
16783
954954d1
PB
16784 offsets = arm_get_frame_offsets ();
16785 live_regs_mask = offsets->saved_regs_mask;
57934c39
PB
16786 high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
16787
16788 /* If we can deduce the registers used from the function's return value.
6fb5fa3c 16789 This is more reliable that examining df_regs_ever_live_p () because that
57934c39
PB
16790 will be set if the register is ever used in the function, not just if
16791 the register is used to hold a return value. */
4f5dfed0 16792 size = arm_size_return_regs ();
d5b7b3ae
RE
16793
16794 /* The prolog may have pushed some high registers to use as
112cdef5 16795 work registers. e.g. the testsuite file:
d5b7b3ae
RE
16796 gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
16797 compiles to produce:
16798 push {r4, r5, r6, r7, lr}
16799 mov r7, r9
16800 mov r6, r8
16801 push {r6, r7}
16802 as part of the prolog. We have to undo that pushing here. */
f676971a 16803
d5b7b3ae
RE
16804 if (high_regs_pushed)
16805 {
b279b20a 16806 unsigned long mask = live_regs_mask & 0xff;
d5b7b3ae 16807 int next_hi_reg;
d5b7b3ae 16808
57934c39
PB
16809 /* The available low registers depend on the size of the value we are
16810 returning. */
16811 if (size <= 12)
d5b7b3ae 16812 mask |= 1 << 3;
57934c39
PB
16813 if (size <= 8)
16814 mask |= 1 << 2;
d5b7b3ae
RE
16815
16816 if (mask == 0)
16817 /* Oh dear! We have no low registers into which we can pop
16818 high registers! */
400500c4
RK
16819 internal_error
16820 ("no low registers available for popping high registers");
f676971a 16821
d5b7b3ae 16822 for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
57934c39 16823 if (live_regs_mask & (1 << next_hi_reg))
d5b7b3ae
RE
16824 break;
16825
16826 while (high_regs_pushed)
16827 {
16828 /* Find lo register(s) into which the high register(s) can
16829 be popped. */
16830 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
16831 {
16832 if (mask & (1 << regno))
16833 high_regs_pushed--;
16834 if (high_regs_pushed == 0)
16835 break;
16836 }
16837
16838 mask &= (2 << regno) - 1; /* A noop if regno == 8 */
16839
d6b4baa4 16840 /* Pop the values into the low register(s). */
980e61bb 16841 thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
d5b7b3ae
RE
16842
16843 /* Move the value(s) into the high registers. */
16844 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
16845 {
16846 if (mask & (1 << regno))
16847 {
16848 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
16849 regno);
f676971a 16850
d5b7b3ae 16851 for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
57934c39 16852 if (live_regs_mask & (1 << next_hi_reg))
d5b7b3ae
RE
16853 break;
16854 }
16855 }
16856 }
57934c39 16857 live_regs_mask &= ~0x0f00;
d5b7b3ae
RE
16858 }
16859
57934c39
PB
16860 had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
16861 live_regs_mask &= 0xff;
16862
38173d38 16863 if (crtl->args.pretend_args_size == 0 || TARGET_BACKTRACE)
d5b7b3ae 16864 {
f676971a 16865 /* Pop the return address into the PC. */
57934c39 16866 if (had_to_push_lr)
d5b7b3ae
RE
16867 live_regs_mask |= 1 << PC_REGNUM;
16868
16869 /* Either no argument registers were pushed or a backtrace
16870 structure was created which includes an adjusted stack
16871 pointer, so just pop everything. */
16872 if (live_regs_mask)
980e61bb
DJ
16873 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
16874 live_regs_mask);
57934c39 16875
d5b7b3ae 16876 /* We have either just popped the return address into the
57934c39
PB
16877 PC or it is was kept in LR for the entire function. */
16878 if (!had_to_push_lr)
16879 thumb_exit (asm_out_file, LR_REGNUM);
d5b7b3ae
RE
16880 }
16881 else
16882 {
16883 /* Pop everything but the return address. */
d5b7b3ae 16884 if (live_regs_mask)
980e61bb
DJ
16885 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
16886 live_regs_mask);
d5b7b3ae
RE
16887
16888 if (had_to_push_lr)
57934c39
PB
16889 {
16890 if (size > 12)
16891 {
16892 /* We have no free low regs, so save one. */
16893 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
16894 LAST_ARG_REGNUM);
16895 }
16896
16897 /* Get the return address into a temporary register. */
16898 thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
16899 1 << LAST_ARG_REGNUM);
16900
16901 if (size > 12)
16902 {
16903 /* Move the return address to lr. */
16904 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
16905 LAST_ARG_REGNUM);
16906 /* Restore the low register. */
16907 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
16908 IP_REGNUM);
16909 regno = LR_REGNUM;
16910 }
16911 else
16912 regno = LAST_ARG_REGNUM;
16913 }
16914 else
16915 regno = LR_REGNUM;
f676971a 16916
d5b7b3ae
RE
16917 /* Remove the argument registers that were pushed onto the stack. */
16918 asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
16919 SP_REGNUM, SP_REGNUM,
38173d38 16920 crtl->args.pretend_args_size);
f676971a 16921
57934c39 16922 thumb_exit (asm_out_file, regno);
d5b7b3ae
RE
16923 }
16924
16925 return "";
16926}
16927
16928/* Functions to save and restore machine-specific function data. */
e2500fed 16929static struct machine_function *
e32bac5b 16930arm_init_machine_status (void)
d5b7b3ae 16931{
e2500fed
GK
16932 struct machine_function *machine;
16933 machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
6d3d9133 16934
f676971a 16935#if ARM_FT_UNKNOWN != 0
e2500fed 16936 machine->func_type = ARM_FT_UNKNOWN;
6d3d9133 16937#endif
e2500fed 16938 return machine;
f7a80099
NC
16939}
16940
d5b7b3ae
RE
16941/* Return an RTX indicating where the return address to the
16942 calling function can be found. */
16943rtx
e32bac5b 16944arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
d5b7b3ae 16945{
d5b7b3ae
RE
16946 if (count != 0)
16947 return NULL_RTX;
16948
61f0ccff 16949 return get_hard_reg_initial_val (Pmode, LR_REGNUM);
d5b7b3ae
RE
16950}
16951
16952/* Do anything needed before RTL is emitted for each function. */
16953void
e32bac5b 16954arm_init_expanders (void)
d5b7b3ae
RE
16955{
16956 /* Arrange to initialize and mark the machine per-function status. */
16957 init_machine_status = arm_init_machine_status;
3ac5ea7c
RH
16958
16959 /* This is to stop the combine pass optimizing away the alignment
16960 adjustment of va_arg. */
16961 /* ??? It is claimed that this should not be necessary. */
16962 if (cfun)
16963 mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
d5b7b3ae
RE
16964}
16965
0977774b 16966
2591db65
RE
16967/* Like arm_compute_initial_elimination offset. Simpler because there
16968 isn't an ABI specified frame pointer for Thumb. Instead, we set it
16969 to point at the base of the local variables after static stack
16970 space for a function has been allocated. */
0977774b 16971
5848830f
PB
16972HOST_WIDE_INT
16973thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
16974{
16975 arm_stack_offsets *offsets;
0977774b 16976
5848830f 16977 offsets = arm_get_frame_offsets ();
0977774b 16978
5848830f 16979 switch (from)
0977774b 16980 {
5848830f
PB
16981 case ARG_POINTER_REGNUM:
16982 switch (to)
16983 {
16984 case STACK_POINTER_REGNUM:
16985 return offsets->outgoing_args - offsets->saved_args;
0977774b 16986
5848830f
PB
16987 case FRAME_POINTER_REGNUM:
16988 return offsets->soft_frame - offsets->saved_args;
0977774b 16989
5848830f
PB
16990 case ARM_HARD_FRAME_POINTER_REGNUM:
16991 return offsets->saved_regs - offsets->saved_args;
0977774b 16992
2591db65
RE
16993 case THUMB_HARD_FRAME_POINTER_REGNUM:
16994 return offsets->locals_base - offsets->saved_args;
16995
5848830f 16996 default:
e6d29d15 16997 gcc_unreachable ();
5848830f
PB
16998 }
16999 break;
0977774b 17000
5848830f
PB
17001 case FRAME_POINTER_REGNUM:
17002 switch (to)
17003 {
17004 case STACK_POINTER_REGNUM:
17005 return offsets->outgoing_args - offsets->soft_frame;
0977774b 17006
5848830f
PB
17007 case ARM_HARD_FRAME_POINTER_REGNUM:
17008 return offsets->saved_regs - offsets->soft_frame;
0977774b 17009
2591db65
RE
17010 case THUMB_HARD_FRAME_POINTER_REGNUM:
17011 return offsets->locals_base - offsets->soft_frame;
17012
5848830f 17013 default:
e6d29d15 17014 gcc_unreachable ();
5848830f
PB
17015 }
17016 break;
0977774b 17017
5848830f 17018 default:
e6d29d15 17019 gcc_unreachable ();
5848830f 17020 }
0977774b
JT
17021}
17022
d5b7b3ae
RE
17023/* Generate the rest of a function's prologue. */
17024void
5b3e6663 17025thumb1_expand_prologue (void)
d5b7b3ae 17026{
980e61bb
DJ
17027 rtx insn, dwarf;
17028
5848830f
PB
17029 HOST_WIDE_INT amount;
17030 arm_stack_offsets *offsets;
6d3d9133 17031 unsigned long func_type;
3c7ad43e 17032 int regno;
57934c39 17033 unsigned long live_regs_mask;
6d3d9133
NC
17034
17035 func_type = arm_current_func_type ();
f676971a 17036
d5b7b3ae 17037 /* Naked functions don't have prologues. */
6d3d9133 17038 if (IS_NAKED (func_type))
d5b7b3ae
RE
17039 return;
17040
6d3d9133
NC
17041 if (IS_INTERRUPT (func_type))
17042 {
c725bd79 17043 error ("interrupt Service Routines cannot be coded in Thumb mode");
6d3d9133
NC
17044 return;
17045 }
17046
954954d1
PB
17047 offsets = arm_get_frame_offsets ();
17048 live_regs_mask = offsets->saved_regs_mask;
b279b20a
NC
17049 /* Load the pic register before setting the frame pointer,
17050 so we can use r7 as a temporary work register. */
020a4035 17051 if (flag_pic && arm_pic_register != INVALID_REGNUM)
e55ef7f4 17052 arm_load_pic_register (live_regs_mask);
876f13b0 17053
2591db65 17054 if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
a2503645
RS
17055 emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
17056 stack_pointer_rtx);
d5b7b3ae 17057
5848830f 17058 amount = offsets->outgoing_args - offsets->saved_regs;
d5b7b3ae
RE
17059 if (amount)
17060 {
d5b7b3ae 17061 if (amount < 512)
980e61bb
DJ
17062 {
17063 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
17064 GEN_INT (- amount)));
17065 RTX_FRAME_RELATED_P (insn) = 1;
17066 }
d5b7b3ae
RE
17067 else
17068 {
d5b7b3ae
RE
17069 rtx reg;
17070
17071 /* The stack decrement is too big for an immediate value in a single
17072 insn. In theory we could issue multiple subtracts, but after
17073 three of them it becomes more space efficient to place the full
17074 value in the constant pool and load into a register. (Also the
17075 ARM debugger really likes to see only one stack decrement per
17076 function). So instead we look for a scratch register into which
17077 we can load the decrement, and then we subtract this from the
17078 stack pointer. Unfortunately on the thumb the only available
17079 scratch registers are the argument registers, and we cannot use
17080 these as they may hold arguments to the function. Instead we
17081 attempt to locate a call preserved register which is used by this
17082 function. If we can find one, then we know that it will have
17083 been pushed at the start of the prologue and so we can corrupt
17084 it now. */
17085 for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
35596784 17086 if (live_regs_mask & (1 << regno))
d5b7b3ae
RE
17087 break;
17088
35596784 17089 gcc_assert(regno <= LAST_LO_REGNUM);
d5b7b3ae 17090
35596784 17091 reg = gen_rtx_REG (SImode, regno);
d5b7b3ae 17092
35596784 17093 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
980e61bb 17094
35596784
AJ
17095 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
17096 stack_pointer_rtx, reg));
17097 RTX_FRAME_RELATED_P (insn) = 1;
17098 dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
17099 plus_constant (stack_pointer_rtx,
17100 -amount));
17101 RTX_FRAME_RELATED_P (dwarf) = 1;
17102 REG_NOTES (insn)
17103 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
17104 REG_NOTES (insn));
d5b7b3ae 17105 }
2591db65
RE
17106 }
17107
17108 if (frame_pointer_needed)
5b3e6663 17109 thumb_set_frame_pointer (offsets);
f676971a 17110
74d9c39f
DJ
17111 /* If we are profiling, make sure no instructions are scheduled before
17112 the call to mcount. Similarly if the user has requested no
17113 scheduling in the prolog. Similarly if we want non-call exceptions
17114 using the EABI unwinder, to prevent faulting instructions from being
17115 swapped with a stack adjustment. */
e3b5732b 17116 if (crtl->profile || !TARGET_SCHED_PROLOG
74d9c39f 17117 || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
d5b7b3ae 17118 emit_insn (gen_blockage ());
3c7ad43e
PB
17119
17120 cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
57934c39
PB
17121 if (live_regs_mask & 0xff)
17122 cfun->machine->lr_save_eliminated = 0;
d5b7b3ae
RE
17123}
17124
57934c39 17125
d5b7b3ae 17126void
5b3e6663 17127thumb1_expand_epilogue (void)
d5b7b3ae 17128{
5848830f
PB
17129 HOST_WIDE_INT amount;
17130 arm_stack_offsets *offsets;
defc0463
RE
17131 int regno;
17132
6d3d9133
NC
17133 /* Naked functions don't have prologues. */
17134 if (IS_NAKED (arm_current_func_type ()))
d5b7b3ae
RE
17135 return;
17136
5848830f
PB
17137 offsets = arm_get_frame_offsets ();
17138 amount = offsets->outgoing_args - offsets->saved_regs;
17139
d5b7b3ae 17140 if (frame_pointer_needed)
2591db65
RE
17141 {
17142 emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
17143 amount = offsets->locals_base - offsets->saved_regs;
17144 }
e0b92319 17145
a3a531ec 17146 gcc_assert (amount >= 0);
2591db65 17147 if (amount)
d5b7b3ae 17148 {
d5b7b3ae
RE
17149 if (amount < 512)
17150 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
17151 GEN_INT (amount)));
17152 else
17153 {
17154 /* r3 is always free in the epilogue. */
f1c25d3b 17155 rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
d5b7b3ae
RE
17156
17157 emit_insn (gen_movsi (reg, GEN_INT (amount)));
17158 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
17159 }
17160 }
f676971a 17161
d5b7b3ae
RE
17162 /* Emit a USE (stack_pointer_rtx), so that
17163 the stack adjustment will not be deleted. */
6bacc7b0 17164 emit_insn (gen_prologue_use (stack_pointer_rtx));
d5b7b3ae 17165
e3b5732b 17166 if (crtl->profile || !TARGET_SCHED_PROLOG)
d5b7b3ae 17167 emit_insn (gen_blockage ());
defc0463
RE
17168
17169 /* Emit a clobber for each insn that will be restored in the epilogue,
17170 so that flow2 will get register lifetimes correct. */
17171 for (regno = 0; regno < 13; regno++)
6fb5fa3c 17172 if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
c41c1387 17173 emit_clobber (gen_rtx_REG (SImode, regno));
defc0463 17174
6fb5fa3c 17175 if (! df_regs_ever_live_p (LR_REGNUM))
c41c1387 17176 emit_use (gen_rtx_REG (SImode, LR_REGNUM));
d5b7b3ae
RE
17177}
17178
08c148a8 17179static void
5b3e6663 17180thumb1_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
d5b7b3ae 17181{
954954d1 17182 arm_stack_offsets *offsets;
b279b20a
NC
17183 unsigned long live_regs_mask = 0;
17184 unsigned long l_mask;
17185 unsigned high_regs_pushed = 0;
980e61bb 17186 int cfa_offset = 0;
d5b7b3ae
RE
17187 int regno;
17188
6d3d9133 17189 if (IS_NAKED (arm_current_func_type ()))
d5b7b3ae
RE
17190 return;
17191
17192 if (is_called_in_ARM_mode (current_function_decl))
17193 {
17194 const char * name;
17195
e6d29d15
NS
17196 gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
17197 gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
17198 == SYMBOL_REF);
d5b7b3ae 17199 name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
f676971a 17200
d5b7b3ae
RE
17201 /* Generate code sequence to switch us into Thumb mode. */
17202 /* The .code 32 directive has already been emitted by
6d77b53e 17203 ASM_DECLARE_FUNCTION_NAME. */
d5b7b3ae
RE
17204 asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
17205 asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
17206
17207 /* Generate a label, so that the debugger will notice the
17208 change in instruction sets. This label is also used by
17209 the assembler to bypass the ARM code when this function
17210 is called from a Thumb encoded function elsewhere in the
17211 same file. Hence the definition of STUB_NAME here must
d6b4baa4 17212 agree with the definition in gas/config/tc-arm.c. */
f676971a 17213
d5b7b3ae 17214#define STUB_NAME ".real_start_of"
f676971a 17215
761c70aa 17216 fprintf (f, "\t.code\t16\n");
d5b7b3ae
RE
17217#ifdef ARM_PE
17218 if (arm_dllexport_name_p (name))
e5951263 17219 name = arm_strip_name_encoding (name);
f676971a 17220#endif
d5b7b3ae 17221 asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
761c70aa 17222 fprintf (f, "\t.thumb_func\n");
d5b7b3ae
RE
17223 asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
17224 }
f676971a 17225
38173d38 17226 if (crtl->args.pretend_args_size)
d5b7b3ae 17227 {
617a1b71
PB
17228 /* Output unwind directive for the stack adjustment. */
17229 if (ARM_EABI_UNWIND_TABLES)
17230 fprintf (f, "\t.pad #%d\n",
38173d38 17231 crtl->args.pretend_args_size);
617a1b71 17232
3cb66fd7 17233 if (cfun->machine->uses_anonymous_args)
d5b7b3ae
RE
17234 {
17235 int num_pushes;
f676971a 17236
761c70aa 17237 fprintf (f, "\tpush\t{");
d5b7b3ae 17238
38173d38 17239 num_pushes = ARM_NUM_INTS (crtl->args.pretend_args_size);
f676971a 17240
d5b7b3ae
RE
17241 for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
17242 regno <= LAST_ARG_REGNUM;
5895f793 17243 regno++)
d5b7b3ae
RE
17244 asm_fprintf (f, "%r%s", regno,
17245 regno == LAST_ARG_REGNUM ? "" : ", ");
17246
761c70aa 17247 fprintf (f, "}\n");
d5b7b3ae
RE
17248 }
17249 else
f676971a 17250 asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
d5b7b3ae 17251 SP_REGNUM, SP_REGNUM,
38173d38 17252 crtl->args.pretend_args_size);
980e61bb
DJ
17253
17254 /* We don't need to record the stores for unwinding (would it
17255 help the debugger any if we did?), but record the change in
17256 the stack pointer. */
17257 if (dwarf2out_do_frame ())
17258 {
17259 char *l = dwarf2out_cfi_label ();
b279b20a 17260
38173d38 17261 cfa_offset = cfa_offset + crtl->args.pretend_args_size;
980e61bb
DJ
17262 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
17263 }
d5b7b3ae
RE
17264 }
17265
b279b20a 17266 /* Get the registers we are going to push. */
954954d1
PB
17267 offsets = arm_get_frame_offsets ();
17268 live_regs_mask = offsets->saved_regs_mask;
b279b20a 17269 /* Extract a mask of the ones we can give to the Thumb's push instruction. */
57934c39 17270 l_mask = live_regs_mask & 0x40ff;
b279b20a
NC
17271 /* Then count how many other high registers will need to be pushed. */
17272 high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
d5b7b3ae
RE
17273
17274 if (TARGET_BACKTRACE)
17275 {
b279b20a
NC
17276 unsigned offset;
17277 unsigned work_register;
f676971a 17278
d5b7b3ae
RE
17279 /* We have been asked to create a stack backtrace structure.
17280 The code looks like this:
f676971a 17281
d5b7b3ae
RE
17282 0 .align 2
17283 0 func:
17284 0 sub SP, #16 Reserve space for 4 registers.
57934c39 17285 2 push {R7} Push low registers.
d5b7b3ae
RE
17286 4 add R7, SP, #20 Get the stack pointer before the push.
17287 6 str R7, [SP, #8] Store the stack pointer (before reserving the space).
17288 8 mov R7, PC Get hold of the start of this code plus 12.
17289 10 str R7, [SP, #16] Store it.
17290 12 mov R7, FP Get hold of the current frame pointer.
17291 14 str R7, [SP, #4] Store it.
17292 16 mov R7, LR Get hold of the current return address.
17293 18 str R7, [SP, #12] Store it.
17294 20 add R7, SP, #16 Point at the start of the backtrace structure.
17295 22 mov FP, R7 Put this value into the frame pointer. */
17296
57934c39 17297 work_register = thumb_find_work_register (live_regs_mask);
f676971a 17298
617a1b71
PB
17299 if (ARM_EABI_UNWIND_TABLES)
17300 asm_fprintf (f, "\t.pad #16\n");
17301
d5b7b3ae
RE
17302 asm_fprintf
17303 (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
17304 SP_REGNUM, SP_REGNUM);
980e61bb
DJ
17305
17306 if (dwarf2out_do_frame ())
17307 {
17308 char *l = dwarf2out_cfi_label ();
b279b20a 17309
980e61bb
DJ
17310 cfa_offset = cfa_offset + 16;
17311 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
17312 }
17313
57934c39
PB
17314 if (l_mask)
17315 {
17316 thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
1a59548b 17317 offset = bit_count (l_mask) * UNITS_PER_WORD;
57934c39
PB
17318 }
17319 else
17320 offset = 0;
f676971a 17321
d5b7b3ae 17322 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
38173d38 17323 offset + 16 + crtl->args.pretend_args_size);
f676971a 17324
d5b7b3ae
RE
17325 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17326 offset + 4);
17327
17328 /* Make sure that the instruction fetching the PC is in the right place
17329 to calculate "start of backtrace creation code + 12". */
57934c39 17330 if (l_mask)
d5b7b3ae
RE
17331 {
17332 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
17333 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17334 offset + 12);
17335 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
17336 ARM_HARD_FRAME_POINTER_REGNUM);
17337 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17338 offset);
17339 }
17340 else
17341 {
17342 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
17343 ARM_HARD_FRAME_POINTER_REGNUM);
17344 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17345 offset);
17346 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
17347 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17348 offset + 12);
17349 }
f676971a 17350
d5b7b3ae
RE
17351 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
17352 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17353 offset + 8);
17354 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
17355 offset + 12);
17356 asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
17357 ARM_HARD_FRAME_POINTER_REGNUM, work_register);
17358 }
0fa2e4df 17359 /* Optimization: If we are not pushing any low registers but we are going
b279b20a
NC
17360 to push some high registers then delay our first push. This will just
17361 be a push of LR and we can combine it with the push of the first high
17362 register. */
17363 else if ((l_mask & 0xff) != 0
17364 || (high_regs_pushed == 0 && l_mask))
57934c39 17365 thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
d5b7b3ae 17366
d5b7b3ae
RE
17367 if (high_regs_pushed)
17368 {
b279b20a
NC
17369 unsigned pushable_regs;
17370 unsigned next_hi_reg;
d5b7b3ae
RE
17371
17372 for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
57934c39 17373 if (live_regs_mask & (1 << next_hi_reg))
e26053d1 17374 break;
d5b7b3ae 17375
57934c39 17376 pushable_regs = l_mask & 0xff;
d5b7b3ae
RE
17377
17378 if (pushable_regs == 0)
57934c39 17379 pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
d5b7b3ae
RE
17380
17381 while (high_regs_pushed > 0)
17382 {
b279b20a 17383 unsigned long real_regs_mask = 0;
980e61bb 17384
b279b20a 17385 for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
d5b7b3ae 17386 {
57934c39 17387 if (pushable_regs & (1 << regno))
d5b7b3ae
RE
17388 {
17389 asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
f676971a 17390
b279b20a 17391 high_regs_pushed --;
980e61bb 17392 real_regs_mask |= (1 << next_hi_reg);
f676971a 17393
d5b7b3ae 17394 if (high_regs_pushed)
aeaf4d25 17395 {
b279b20a
NC
17396 for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
17397 next_hi_reg --)
57934c39 17398 if (live_regs_mask & (1 << next_hi_reg))
d5b7b3ae 17399 break;
aeaf4d25 17400 }
d5b7b3ae
RE
17401 else
17402 {
57934c39 17403 pushable_regs &= ~((1 << regno) - 1);
d5b7b3ae
RE
17404 break;
17405 }
17406 }
17407 }
980e61bb 17408
b279b20a
NC
17409 /* If we had to find a work register and we have not yet
17410 saved the LR then add it to the list of regs to push. */
17411 if (l_mask == (1 << LR_REGNUM))
17412 {
17413 thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
17414 1, &cfa_offset,
17415 real_regs_mask | (1 << LR_REGNUM));
17416 l_mask = 0;
17417 }
17418 else
17419 thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
d5b7b3ae 17420 }
d5b7b3ae
RE
17421 }
17422}
17423
17424/* Handle the case of a double word load into a low register from
17425 a computed memory address. The computed address may involve a
17426 register which is overwritten by the load. */
cd2b33d0 17427const char *
e32bac5b 17428thumb_load_double_from_address (rtx *operands)
d5b7b3ae
RE
17429{
17430 rtx addr;
17431 rtx base;
17432 rtx offset;
17433 rtx arg1;
17434 rtx arg2;
f676971a 17435
e6d29d15
NS
17436 gcc_assert (GET_CODE (operands[0]) == REG);
17437 gcc_assert (GET_CODE (operands[1]) == MEM);
d5b7b3ae
RE
17438
17439 /* Get the memory address. */
17440 addr = XEXP (operands[1], 0);
f676971a 17441
d5b7b3ae
RE
17442 /* Work out how the memory address is computed. */
17443 switch (GET_CODE (addr))
17444 {
17445 case REG:
31fa16b6 17446 operands[2] = adjust_address (operands[1], SImode, 4);
e0b92319 17447
d5b7b3ae
RE
17448 if (REGNO (operands[0]) == REGNO (addr))
17449 {
17450 output_asm_insn ("ldr\t%H0, %2", operands);
17451 output_asm_insn ("ldr\t%0, %1", operands);
17452 }
17453 else
17454 {
17455 output_asm_insn ("ldr\t%0, %1", operands);
17456 output_asm_insn ("ldr\t%H0, %2", operands);
17457 }
17458 break;
f676971a 17459
d5b7b3ae
RE
17460 case CONST:
17461 /* Compute <address> + 4 for the high order load. */
31fa16b6 17462 operands[2] = adjust_address (operands[1], SImode, 4);
e0b92319 17463
d5b7b3ae
RE
17464 output_asm_insn ("ldr\t%0, %1", operands);
17465 output_asm_insn ("ldr\t%H0, %2", operands);
17466 break;
f676971a 17467
d5b7b3ae
RE
17468 case PLUS:
17469 arg1 = XEXP (addr, 0);
17470 arg2 = XEXP (addr, 1);
f676971a 17471
d5b7b3ae
RE
17472 if (CONSTANT_P (arg1))
17473 base = arg2, offset = arg1;
17474 else
17475 base = arg1, offset = arg2;
f676971a 17476
e6d29d15 17477 gcc_assert (GET_CODE (base) == REG);
d5b7b3ae
RE
17478
17479 /* Catch the case of <address> = <reg> + <reg> */
17480 if (GET_CODE (offset) == REG)
17481 {
17482 int reg_offset = REGNO (offset);
17483 int reg_base = REGNO (base);
17484 int reg_dest = REGNO (operands[0]);
f676971a 17485
d5b7b3ae
RE
17486 /* Add the base and offset registers together into the
17487 higher destination register. */
17488 asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
17489 reg_dest + 1, reg_base, reg_offset);
f676971a 17490
d5b7b3ae
RE
17491 /* Load the lower destination register from the address in
17492 the higher destination register. */
17493 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
17494 reg_dest, reg_dest + 1);
f676971a 17495
d5b7b3ae
RE
17496 /* Load the higher destination register from its own address
17497 plus 4. */
17498 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
17499 reg_dest + 1, reg_dest + 1);
17500 }
17501 else
17502 {
17503 /* Compute <address> + 4 for the high order load. */
31fa16b6 17504 operands[2] = adjust_address (operands[1], SImode, 4);
f676971a 17505
d5b7b3ae
RE
17506 /* If the computed address is held in the low order register
17507 then load the high order register first, otherwise always
17508 load the low order register first. */
17509 if (REGNO (operands[0]) == REGNO (base))
17510 {
17511 output_asm_insn ("ldr\t%H0, %2", operands);
17512 output_asm_insn ("ldr\t%0, %1", operands);
17513 }
17514 else
17515 {
17516 output_asm_insn ("ldr\t%0, %1", operands);
17517 output_asm_insn ("ldr\t%H0, %2", operands);
17518 }
17519 }
17520 break;
17521
17522 case LABEL_REF:
17523 /* With no registers to worry about we can just load the value
17524 directly. */
31fa16b6 17525 operands[2] = adjust_address (operands[1], SImode, 4);
f676971a 17526
d5b7b3ae
RE
17527 output_asm_insn ("ldr\t%H0, %2", operands);
17528 output_asm_insn ("ldr\t%0, %1", operands);
17529 break;
f676971a 17530
d5b7b3ae 17531 default:
e6d29d15 17532 gcc_unreachable ();
d5b7b3ae 17533 }
f676971a 17534
d5b7b3ae
RE
17535 return "";
17536}
17537
cd2b33d0 17538const char *
e32bac5b 17539thumb_output_move_mem_multiple (int n, rtx *operands)
d5b7b3ae
RE
17540{
17541 rtx tmp;
17542
17543 switch (n)
17544 {
17545 case 2:
ca356f3a 17546 if (REGNO (operands[4]) > REGNO (operands[5]))
d5b7b3ae 17547 {
ca356f3a
RE
17548 tmp = operands[4];
17549 operands[4] = operands[5];
17550 operands[5] = tmp;
d5b7b3ae 17551 }
ca356f3a
RE
17552 output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
17553 output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
d5b7b3ae
RE
17554 break;
17555
17556 case 3:
ca356f3a 17557 if (REGNO (operands[4]) > REGNO (operands[5]))
d5b7b3ae 17558 {
ca356f3a
RE
17559 tmp = operands[4];
17560 operands[4] = operands[5];
17561 operands[5] = tmp;
d5b7b3ae 17562 }
ca356f3a 17563 if (REGNO (operands[5]) > REGNO (operands[6]))
d5b7b3ae 17564 {
ca356f3a
RE
17565 tmp = operands[5];
17566 operands[5] = operands[6];
17567 operands[6] = tmp;
d5b7b3ae 17568 }
ca356f3a 17569 if (REGNO (operands[4]) > REGNO (operands[5]))
d5b7b3ae 17570 {
ca356f3a
RE
17571 tmp = operands[4];
17572 operands[4] = operands[5];
17573 operands[5] = tmp;
d5b7b3ae 17574 }
f676971a 17575
ca356f3a
RE
17576 output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
17577 output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
d5b7b3ae
RE
17578 break;
17579
17580 default:
e6d29d15 17581 gcc_unreachable ();
d5b7b3ae
RE
17582 }
17583
17584 return "";
17585}
17586
b12a00f1
RE
17587/* Output a call-via instruction for thumb state. */
17588const char *
17589thumb_call_via_reg (rtx reg)
17590{
17591 int regno = REGNO (reg);
17592 rtx *labelp;
17593
57ecec57 17594 gcc_assert (regno < LR_REGNUM);
b12a00f1
RE
17595
17596 /* If we are in the normal text section we can use a single instance
17597 per compilation unit. If we are doing function sections, then we need
17598 an entry per section, since we can't rely on reachability. */
d6b5193b 17599 if (in_section == text_section)
b12a00f1
RE
17600 {
17601 thumb_call_reg_needed = 1;
17602
17603 if (thumb_call_via_label[regno] == NULL)
17604 thumb_call_via_label[regno] = gen_label_rtx ();
17605 labelp = thumb_call_via_label + regno;
17606 }
17607 else
17608 {
17609 if (cfun->machine->call_via[regno] == NULL)
17610 cfun->machine->call_via[regno] = gen_label_rtx ();
17611 labelp = cfun->machine->call_via + regno;
17612 }
17613
17614 output_asm_insn ("bl\t%a0", labelp);
17615 return "";
17616}
17617
1d6e90ac 17618/* Routines for generating rtl. */
d5b7b3ae 17619void
70128ad9 17620thumb_expand_movmemqi (rtx *operands)
d5b7b3ae
RE
17621{
17622 rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
17623 rtx in = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
17624 HOST_WIDE_INT len = INTVAL (operands[2]);
17625 HOST_WIDE_INT offset = 0;
17626
17627 while (len >= 12)
17628 {
ca356f3a 17629 emit_insn (gen_movmem12b (out, in, out, in));
d5b7b3ae
RE
17630 len -= 12;
17631 }
f676971a 17632
d5b7b3ae
RE
17633 if (len >= 8)
17634 {
ca356f3a 17635 emit_insn (gen_movmem8b (out, in, out, in));
d5b7b3ae
RE
17636 len -= 8;
17637 }
f676971a 17638
d5b7b3ae
RE
17639 if (len >= 4)
17640 {
17641 rtx reg = gen_reg_rtx (SImode);
f1c25d3b
KH
17642 emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
17643 emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
d5b7b3ae
RE
17644 len -= 4;
17645 offset += 4;
17646 }
f676971a 17647
d5b7b3ae
RE
17648 if (len >= 2)
17649 {
17650 rtx reg = gen_reg_rtx (HImode);
f676971a 17651 emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
f1c25d3b
KH
17652 plus_constant (in, offset))));
17653 emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
d5b7b3ae
RE
17654 reg));
17655 len -= 2;
17656 offset += 2;
17657 }
f676971a 17658
d5b7b3ae
RE
17659 if (len)
17660 {
17661 rtx reg = gen_reg_rtx (QImode);
f1c25d3b
KH
17662 emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
17663 plus_constant (in, offset))));
17664 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
d5b7b3ae
RE
17665 reg));
17666 }
17667}
17668
d5b7b3ae 17669void
e32bac5b 17670thumb_reload_out_hi (rtx *operands)
d5b7b3ae
RE
17671{
17672 emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
17673}
17674
f676971a 17675/* Handle reading a half-word from memory during reload. */
d5b7b3ae 17676void
e32bac5b 17677thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
d5b7b3ae 17678{
e6d29d15 17679 gcc_unreachable ();
d5b7b3ae
RE
17680}
17681
c27ba912
DM
17682/* Return the length of a function name prefix
17683 that starts with the character 'c'. */
17684static int
e32bac5b 17685arm_get_strip_length (int c)
c27ba912
DM
17686{
17687 switch (c)
17688 {
17689 ARM_NAME_ENCODING_LENGTHS
f676971a 17690 default: return 0;
c27ba912
DM
17691 }
17692}
17693
17694/* Return a pointer to a function's name with any
17695 and all prefix encodings stripped from it. */
17696const char *
e32bac5b 17697arm_strip_name_encoding (const char *name)
c27ba912
DM
17698{
17699 int skip;
f676971a 17700
c27ba912
DM
17701 while ((skip = arm_get_strip_length (* name)))
17702 name += skip;
17703
17704 return name;
17705}
17706
e1944073
KW
17707/* If there is a '*' anywhere in the name's prefix, then
17708 emit the stripped name verbatim, otherwise prepend an
17709 underscore if leading underscores are being used. */
e1944073 17710void
e32bac5b 17711arm_asm_output_labelref (FILE *stream, const char *name)
e1944073
KW
17712{
17713 int skip;
17714 int verbatim = 0;
17715
17716 while ((skip = arm_get_strip_length (* name)))
17717 {
17718 verbatim |= (*name == '*');
17719 name += skip;
17720 }
17721
17722 if (verbatim)
17723 fputs (name, stream);
17724 else
17725 asm_fprintf (stream, "%U%s", name);
17726}
17727
6c6aa1af
PB
17728static void
17729arm_file_start (void)
17730{
17731 int val;
17732
5b3e6663
PB
17733 if (TARGET_UNIFIED_ASM)
17734 asm_fprintf (asm_out_file, "\t.syntax unified\n");
17735
6c6aa1af
PB
17736 if (TARGET_BPABI)
17737 {
17738 const char *fpu_name;
17739 if (arm_select[0].string)
17740 asm_fprintf (asm_out_file, "\t.cpu %s\n", arm_select[0].string);
17741 else if (arm_select[1].string)
17742 asm_fprintf (asm_out_file, "\t.arch %s\n", arm_select[1].string);
17743 else
17744 asm_fprintf (asm_out_file, "\t.cpu %s\n",
17745 all_cores[arm_default_cpu].name);
17746
17747 if (TARGET_SOFT_FLOAT)
17748 {
17749 if (TARGET_VFP)
17750 fpu_name = "softvfp";
17751 else
17752 fpu_name = "softfpa";
17753 }
17754 else
17755 {
f1adb0a9 17756 int set_float_abi_attributes = 0;
6c6aa1af
PB
17757 switch (arm_fpu_arch)
17758 {
17759 case FPUTYPE_FPA:
17760 fpu_name = "fpa";
17761 break;
17762 case FPUTYPE_FPA_EMU2:
17763 fpu_name = "fpe2";
17764 break;
17765 case FPUTYPE_FPA_EMU3:
17766 fpu_name = "fpe3";
17767 break;
17768 case FPUTYPE_MAVERICK:
17769 fpu_name = "maverick";
17770 break;
17771 case FPUTYPE_VFP:
6c6aa1af 17772 fpu_name = "vfp";
f1adb0a9
JB
17773 set_float_abi_attributes = 1;
17774 break;
302c3d8e
PB
17775 case FPUTYPE_VFP3D16:
17776 fpu_name = "vfpv3-d16";
17777 set_float_abi_attributes = 1;
17778 break;
f1adb0a9 17779 case FPUTYPE_VFP3:
302c3d8e 17780 fpu_name = "vfpv3";
f1adb0a9 17781 set_float_abi_attributes = 1;
6c6aa1af 17782 break;
88f77cba
JB
17783 case FPUTYPE_NEON:
17784 fpu_name = "neon";
17785 set_float_abi_attributes = 1;
17786 break;
6c6aa1af
PB
17787 default:
17788 abort();
17789 }
f1adb0a9
JB
17790 if (set_float_abi_attributes)
17791 {
17792 if (TARGET_HARD_FLOAT)
17793 asm_fprintf (asm_out_file, "\t.eabi_attribute 27, 3\n");
17794 if (TARGET_HARD_FLOAT_ABI)
17795 asm_fprintf (asm_out_file, "\t.eabi_attribute 28, 1\n");
17796 }
6c6aa1af
PB
17797 }
17798 asm_fprintf (asm_out_file, "\t.fpu %s\n", fpu_name);
17799
17800 /* Some of these attributes only apply when the corresponding features
17801 are used. However we don't have any easy way of figuring this out.
17802 Conservatively record the setting that would have been used. */
17803
6c6aa1af
PB
17804 /* Tag_ABI_FP_rounding. */
17805 if (flag_rounding_math)
17806 asm_fprintf (asm_out_file, "\t.eabi_attribute 19, 1\n");
17807 if (!flag_unsafe_math_optimizations)
17808 {
17809 /* Tag_ABI_FP_denomal. */
17810 asm_fprintf (asm_out_file, "\t.eabi_attribute 20, 1\n");
17811 /* Tag_ABI_FP_exceptions. */
17812 asm_fprintf (asm_out_file, "\t.eabi_attribute 21, 1\n");
17813 }
17814 /* Tag_ABI_FP_user_exceptions. */
17815 if (flag_signaling_nans)
17816 asm_fprintf (asm_out_file, "\t.eabi_attribute 22, 1\n");
17817 /* Tag_ABI_FP_number_model. */
17818 asm_fprintf (asm_out_file, "\t.eabi_attribute 23, %d\n",
17819 flag_finite_math_only ? 1 : 3);
17820
17821 /* Tag_ABI_align8_needed. */
17822 asm_fprintf (asm_out_file, "\t.eabi_attribute 24, 1\n");
17823 /* Tag_ABI_align8_preserved. */
17824 asm_fprintf (asm_out_file, "\t.eabi_attribute 25, 1\n");
17825 /* Tag_ABI_enum_size. */
17826 asm_fprintf (asm_out_file, "\t.eabi_attribute 26, %d\n",
17827 flag_short_enums ? 1 : 2);
17828
17829 /* Tag_ABI_optimization_goals. */
17830 if (optimize_size)
17831 val = 4;
17832 else if (optimize >= 2)
17833 val = 2;
17834 else if (optimize)
17835 val = 1;
17836 else
17837 val = 6;
17838 asm_fprintf (asm_out_file, "\t.eabi_attribute 30, %d\n", val);
b76c3c4b
PB
17839
17840 if (arm_lang_output_object_attributes_hook)
17841 arm_lang_output_object_attributes_hook();
6c6aa1af
PB
17842 }
17843 default_file_start();
17844}
17845
b12a00f1
RE
17846static void
17847arm_file_end (void)
17848{
17849 int regno;
17850
978e411f
CD
17851 if (NEED_INDICATE_EXEC_STACK)
17852 /* Add .note.GNU-stack. */
17853 file_end_indicate_exec_stack ();
17854
b12a00f1
RE
17855 if (! thumb_call_reg_needed)
17856 return;
17857
d6b5193b 17858 switch_to_section (text_section);
b12a00f1
RE
17859 asm_fprintf (asm_out_file, "\t.code 16\n");
17860 ASM_OUTPUT_ALIGN (asm_out_file, 1);
17861
57ecec57 17862 for (regno = 0; regno < LR_REGNUM; regno++)
b12a00f1
RE
17863 {
17864 rtx label = thumb_call_via_label[regno];
17865
17866 if (label != 0)
17867 {
17868 targetm.asm_out.internal_label (asm_out_file, "L",
17869 CODE_LABEL_NUMBER (label));
17870 asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
17871 }
17872 }
17873}
17874
fb49053f
RH
17875#ifndef ARM_PE
17876/* Symbols in the text segment can be accessed without indirecting via the
17877 constant pool; it may take an extra binary operation, but this is still
17878 faster than indirecting via memory. Don't do this when not optimizing,
17879 since we won't be calculating al of the offsets necessary to do this
17880 simplification. */
17881
17882static void
e32bac5b 17883arm_encode_section_info (tree decl, rtx rtl, int first)
fb49053f 17884{
3521b33c 17885 if (optimize > 0 && TREE_CONSTANT (decl))
c6a2438a 17886 SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
fb49053f 17887
d3585b76 17888 default_encode_section_info (decl, rtl, first);
fb49053f
RH
17889}
17890#endif /* !ARM_PE */
483ab821 17891
4977bab6 17892static void
e32bac5b 17893arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
4977bab6
ZW
17894{
17895 if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
17896 && !strcmp (prefix, "L"))
17897 {
17898 arm_ccfsm_state = 0;
17899 arm_target_insn = NULL;
17900 }
17901 default_internal_label (stream, prefix, labelno);
17902}
17903
c590b625
RH
17904/* Output code to add DELTA to the first argument, and then jump
17905 to FUNCTION. Used for C++ multiple inheritance. */
c590b625 17906static void
e32bac5b
RE
17907arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
17908 HOST_WIDE_INT delta,
17909 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
17910 tree function)
483ab821 17911{
9b66ebb1
PB
17912 static int thunk_label = 0;
17913 char label[256];
54b9e939 17914 char labelpc[256];
483ab821
MM
17915 int mi_delta = delta;
17916 const char *const mi_op = mi_delta < 0 ? "sub" : "add";
17917 int shift = 0;
61f71b34 17918 int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
483ab821
MM
17919 ? 1 : 0);
17920 if (mi_delta < 0)
17921 mi_delta = - mi_delta;
bf98ec6c 17922
5b3e6663 17923 if (TARGET_THUMB1)
9b66ebb1
PB
17924 {
17925 int labelno = thunk_label++;
17926 ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
bf98ec6c
PB
17927 /* Thunks are entered in arm mode when avaiable. */
17928 if (TARGET_THUMB1_ONLY)
17929 {
17930 /* push r3 so we can use it as a temporary. */
17931 /* TODO: Omit this save if r3 is not used. */
17932 fputs ("\tpush {r3}\n", file);
17933 fputs ("\tldr\tr3, ", file);
17934 }
17935 else
17936 {
17937 fputs ("\tldr\tr12, ", file);
17938 }
9b66ebb1
PB
17939 assemble_name (file, label);
17940 fputc ('\n', file);
54b9e939
KH
17941 if (flag_pic)
17942 {
17943 /* If we are generating PIC, the ldr instruction below loads
17944 "(target - 7) - .LTHUNKPCn" into r12. The pc reads as
17945 the address of the add + 8, so we have:
17946
17947 r12 = (target - 7) - .LTHUNKPCn + (.LTHUNKPCn + 8)
17948 = target + 1.
17949
17950 Note that we have "+ 1" because some versions of GNU ld
17951 don't set the low bit of the result for R_ARM_REL32
bf98ec6c
PB
17952 relocations against thumb function symbols.
17953 On ARMv6M this is +4, not +8. */
54b9e939
KH
17954 ASM_GENERATE_INTERNAL_LABEL (labelpc, "LTHUNKPC", labelno);
17955 assemble_name (file, labelpc);
17956 fputs (":\n", file);
bf98ec6c
PB
17957 if (TARGET_THUMB1_ONLY)
17958 {
17959 /* This is 2 insns after the start of the thunk, so we know it
17960 is 4-byte aligned. */
17961 fputs ("\tadd\tr3, pc, r3\n", file);
17962 fputs ("\tmov r12, r3\n", file);
17963 }
17964 else
17965 fputs ("\tadd\tr12, pc, r12\n", file);
54b9e939 17966 }
bf98ec6c
PB
17967 else if (TARGET_THUMB1_ONLY)
17968 fputs ("\tmov r12, r3\n", file);
9b66ebb1 17969 }
bf98ec6c 17970 if (TARGET_THUMB1_ONLY)
483ab821 17971 {
bf98ec6c
PB
17972 if (mi_delta > 255)
17973 {
17974 fputs ("\tldr\tr3, ", file);
17975 assemble_name (file, label);
17976 fputs ("+4\n", file);
17977 asm_fprintf (file, "\t%s\t%r, %r, r3\n",
17978 mi_op, this_regno, this_regno);
17979 }
17980 else if (mi_delta != 0)
17981 {
17982 asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
17983 mi_op, this_regno, this_regno,
17984 mi_delta);
17985 }
17986 }
17987 else
17988 {
17989 /* TODO: Use movw/movt for large constants when available. */
17990 while (mi_delta != 0)
17991 {
17992 if ((mi_delta & (3 << shift)) == 0)
17993 shift += 2;
17994 else
17995 {
17996 asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
17997 mi_op, this_regno, this_regno,
17998 mi_delta & (0xff << shift));
17999 mi_delta &= ~(0xff << shift);
18000 shift += 8;
18001 }
18002 }
483ab821 18003 }
5b3e6663 18004 if (TARGET_THUMB1)
9b66ebb1 18005 {
bf98ec6c
PB
18006 if (TARGET_THUMB1_ONLY)
18007 fputs ("\tpop\t{r3}\n", file);
18008
9b66ebb1
PB
18009 fprintf (file, "\tbx\tr12\n");
18010 ASM_OUTPUT_ALIGN (file, 2);
18011 assemble_name (file, label);
18012 fputs (":\n", file);
54b9e939
KH
18013 if (flag_pic)
18014 {
18015 /* Output ".word .LTHUNKn-7-.LTHUNKPCn". */
18016 rtx tem = XEXP (DECL_RTL (function), 0);
18017 tem = gen_rtx_PLUS (GET_MODE (tem), tem, GEN_INT (-7));
18018 tem = gen_rtx_MINUS (GET_MODE (tem),
18019 tem,
18020 gen_rtx_SYMBOL_REF (Pmode,
18021 ggc_strdup (labelpc)));
18022 assemble_integer (tem, 4, BITS_PER_WORD, 1);
18023 }
18024 else
18025 /* Output ".word .LTHUNKn". */
18026 assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
bf98ec6c
PB
18027
18028 if (TARGET_THUMB1_ONLY && mi_delta > 255)
18029 assemble_integer (GEN_INT(mi_delta), 4, BITS_PER_WORD, 1);
9b66ebb1
PB
18030 }
18031 else
18032 {
18033 fputs ("\tb\t", file);
18034 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
18035 if (NEED_PLT_RELOC)
18036 fputs ("(PLT)", file);
18037 fputc ('\n', file);
18038 }
483ab821 18039}
5a9335ef
NC
18040
18041int
6f5f2481 18042arm_emit_vector_const (FILE *file, rtx x)
5a9335ef
NC
18043{
18044 int i;
18045 const char * pattern;
18046
e6d29d15 18047 gcc_assert (GET_CODE (x) == CONST_VECTOR);
5a9335ef
NC
18048
18049 switch (GET_MODE (x))
18050 {
18051 case V2SImode: pattern = "%08x"; break;
18052 case V4HImode: pattern = "%04x"; break;
18053 case V8QImode: pattern = "%02x"; break;
e6d29d15 18054 default: gcc_unreachable ();
5a9335ef
NC
18055 }
18056
18057 fprintf (file, "0x");
18058 for (i = CONST_VECTOR_NUNITS (x); i--;)
18059 {
18060 rtx element;
18061
18062 element = CONST_VECTOR_ELT (x, i);
18063 fprintf (file, pattern, INTVAL (element));
18064 }
18065
18066 return 1;
18067}
18068
18069const char *
6f5f2481 18070arm_output_load_gr (rtx *operands)
5a9335ef
NC
18071{
18072 rtx reg;
18073 rtx offset;
18074 rtx wcgr;
18075 rtx sum;
f676971a 18076
5a9335ef
NC
18077 if (GET_CODE (operands [1]) != MEM
18078 || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
18079 || GET_CODE (reg = XEXP (sum, 0)) != REG
18080 || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
18081 || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
18082 return "wldrw%?\t%0, %1";
f676971a
EC
18083
18084 /* Fix up an out-of-range load of a GR register. */
5a9335ef
NC
18085 output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
18086 wcgr = operands[0];
18087 operands[0] = reg;
18088 output_asm_insn ("ldr%?\t%0, %1", operands);
18089
18090 operands[0] = wcgr;
18091 operands[1] = reg;
18092 output_asm_insn ("tmcr%?\t%0, %1", operands);
18093 output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
18094
18095 return "";
18096}
f9ba5949 18097
1cc9f5f5
KH
18098/* Worker function for TARGET_SETUP_INCOMING_VARARGS.
18099
18100 On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
18101 named arg and all anonymous args onto the stack.
18102 XXX I know the prologue shouldn't be pushing registers, but it is faster
18103 that way. */
18104
18105static void
18106arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
22ccaaee
JJ
18107 enum machine_mode mode,
18108 tree type,
1cc9f5f5
KH
18109 int *pretend_size,
18110 int second_time ATTRIBUTE_UNUSED)
18111{
22ccaaee
JJ
18112 int nregs = cum->nregs;
18113 if (nregs & 1
18114 && ARM_DOUBLEWORD_ALIGN
18115 && arm_needs_doubleword_align (mode, type))
18116 nregs++;
18117
1cc9f5f5 18118 cfun->machine->uses_anonymous_args = 1;
22ccaaee
JJ
18119 if (nregs < NUM_ARG_REGS)
18120 *pretend_size = (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
1cc9f5f5 18121}
9b66ebb1 18122
59b9a953 18123/* Return nonzero if the CONSUMER instruction (a store) does not need
9b66ebb1
PB
18124 PRODUCER's value to calculate the address. */
18125
18126int
18127arm_no_early_store_addr_dep (rtx producer, rtx consumer)
18128{
18129 rtx value = PATTERN (producer);
18130 rtx addr = PATTERN (consumer);
18131
18132 if (GET_CODE (value) == COND_EXEC)
18133 value = COND_EXEC_CODE (value);
18134 if (GET_CODE (value) == PARALLEL)
18135 value = XVECEXP (value, 0, 0);
18136 value = XEXP (value, 0);
18137 if (GET_CODE (addr) == COND_EXEC)
18138 addr = COND_EXEC_CODE (addr);
18139 if (GET_CODE (addr) == PARALLEL)
18140 addr = XVECEXP (addr, 0, 0);
18141 addr = XEXP (addr, 0);
f676971a 18142
9b66ebb1
PB
18143 return !reg_overlap_mentioned_p (value, addr);
18144}
18145
59b9a953 18146/* Return nonzero if the CONSUMER instruction (an ALU op) does not
9b66ebb1
PB
18147 have an early register shift value or amount dependency on the
18148 result of PRODUCER. */
18149
18150int
18151arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
18152{
18153 rtx value = PATTERN (producer);
18154 rtx op = PATTERN (consumer);
18155 rtx early_op;
18156
18157 if (GET_CODE (value) == COND_EXEC)
18158 value = COND_EXEC_CODE (value);
18159 if (GET_CODE (value) == PARALLEL)
18160 value = XVECEXP (value, 0, 0);
18161 value = XEXP (value, 0);
18162 if (GET_CODE (op) == COND_EXEC)
18163 op = COND_EXEC_CODE (op);
18164 if (GET_CODE (op) == PARALLEL)
18165 op = XVECEXP (op, 0, 0);
18166 op = XEXP (op, 1);
f676971a 18167
9b66ebb1
PB
18168 early_op = XEXP (op, 0);
18169 /* This is either an actual independent shift, or a shift applied to
18170 the first operand of another operation. We want the whole shift
18171 operation. */
18172 if (GET_CODE (early_op) == REG)
18173 early_op = op;
18174
18175 return !reg_overlap_mentioned_p (value, early_op);
18176}
18177
59b9a953 18178/* Return nonzero if the CONSUMER instruction (an ALU op) does not
9b66ebb1
PB
18179 have an early register shift value dependency on the result of
18180 PRODUCER. */
18181
18182int
18183arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
18184{
18185 rtx value = PATTERN (producer);
18186 rtx op = PATTERN (consumer);
18187 rtx early_op;
18188
18189 if (GET_CODE (value) == COND_EXEC)
18190 value = COND_EXEC_CODE (value);
18191 if (GET_CODE (value) == PARALLEL)
18192 value = XVECEXP (value, 0, 0);
18193 value = XEXP (value, 0);
18194 if (GET_CODE (op) == COND_EXEC)
18195 op = COND_EXEC_CODE (op);
18196 if (GET_CODE (op) == PARALLEL)
18197 op = XVECEXP (op, 0, 0);
18198 op = XEXP (op, 1);
f676971a 18199
9b66ebb1
PB
18200 early_op = XEXP (op, 0);
18201
18202 /* This is either an actual independent shift, or a shift applied to
18203 the first operand of another operation. We want the value being
18204 shifted, in either case. */
18205 if (GET_CODE (early_op) != REG)
18206 early_op = XEXP (early_op, 0);
f676971a 18207
9b66ebb1
PB
18208 return !reg_overlap_mentioned_p (value, early_op);
18209}
18210
59b9a953 18211/* Return nonzero if the CONSUMER (a mul or mac op) does not
9b66ebb1
PB
18212 have an early register mult dependency on the result of
18213 PRODUCER. */
18214
18215int
18216arm_no_early_mul_dep (rtx producer, rtx consumer)
18217{
18218 rtx value = PATTERN (producer);
18219 rtx op = PATTERN (consumer);
18220
18221 if (GET_CODE (value) == COND_EXEC)
18222 value = COND_EXEC_CODE (value);
18223 if (GET_CODE (value) == PARALLEL)
18224 value = XVECEXP (value, 0, 0);
18225 value = XEXP (value, 0);
18226 if (GET_CODE (op) == COND_EXEC)
18227 op = COND_EXEC_CODE (op);
18228 if (GET_CODE (op) == PARALLEL)
18229 op = XVECEXP (op, 0, 0);
18230 op = XEXP (op, 1);
f676971a 18231
9b66ebb1
PB
18232 return (GET_CODE (op) == PLUS
18233 && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
18234}
18235
70301b45
PB
18236/* We can't rely on the caller doing the proper promotion when
18237 using APCS or ATPCS. */
18238
18239static bool
586de218 18240arm_promote_prototypes (const_tree t ATTRIBUTE_UNUSED)
70301b45 18241{
b6685939 18242 return !TARGET_AAPCS_BASED;
70301b45
PB
18243}
18244
6b045785
PB
18245
18246/* AAPCS based ABIs use short enums by default. */
18247
18248static bool
18249arm_default_short_enums (void)
18250{
077fc835 18251 return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
6b045785 18252}
13c1cd82
PB
18253
18254
18255/* AAPCS requires that anonymous bitfields affect structure alignment. */
18256
18257static bool
18258arm_align_anon_bitfield (void)
18259{
18260 return TARGET_AAPCS_BASED;
18261}
4185ae53
PB
18262
18263
18264/* The generic C++ ABI says 64-bit (long long). The EABI says 32-bit. */
18265
18266static tree
18267arm_cxx_guard_type (void)
18268{
18269 return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
18270}
18271
c956e102
MS
18272/* Return non-zero if the consumer (a multiply-accumulate instruction)
18273 has an accumulator dependency on the result of the producer (a
18274 multiplication instruction) and no other dependency on that result. */
18275int
18276arm_mac_accumulator_is_mul_result (rtx producer, rtx consumer)
18277{
18278 rtx mul = PATTERN (producer);
18279 rtx mac = PATTERN (consumer);
18280 rtx mul_result;
18281 rtx mac_op0, mac_op1, mac_acc;
18282
18283 if (GET_CODE (mul) == COND_EXEC)
18284 mul = COND_EXEC_CODE (mul);
18285 if (GET_CODE (mac) == COND_EXEC)
18286 mac = COND_EXEC_CODE (mac);
18287
18288 /* Check that mul is of the form (set (...) (mult ...))
18289 and mla is of the form (set (...) (plus (mult ...) (...))). */
18290 if ((GET_CODE (mul) != SET || GET_CODE (XEXP (mul, 1)) != MULT)
18291 || (GET_CODE (mac) != SET || GET_CODE (XEXP (mac, 1)) != PLUS
18292 || GET_CODE (XEXP (XEXP (mac, 1), 0)) != MULT))
18293 return 0;
18294
18295 mul_result = XEXP (mul, 0);
18296 mac_op0 = XEXP (XEXP (XEXP (mac, 1), 0), 0);
18297 mac_op1 = XEXP (XEXP (XEXP (mac, 1), 0), 1);
18298 mac_acc = XEXP (XEXP (mac, 1), 1);
18299
18300 return (reg_overlap_mentioned_p (mul_result, mac_acc)
18301 && !reg_overlap_mentioned_p (mul_result, mac_op0)
18302 && !reg_overlap_mentioned_p (mul_result, mac_op1));
18303}
18304
4185ae53 18305
0fa2e4df 18306/* The EABI says test the least significant bit of a guard variable. */
4185ae53
PB
18307
18308static bool
18309arm_cxx_guard_mask_bit (void)
18310{
18311 return TARGET_AAPCS_BASED;
18312}
46e995e0
PB
18313
18314
18315/* The EABI specifies that all array cookies are 8 bytes long. */
18316
18317static tree
18318arm_get_cookie_size (tree type)
18319{
18320 tree size;
18321
18322 if (!TARGET_AAPCS_BASED)
18323 return default_cxx_get_cookie_size (type);
18324
7d60be94 18325 size = build_int_cst (sizetype, 8);
46e995e0
PB
18326 return size;
18327}
18328
18329
18330/* The EABI says that array cookies should also contain the element size. */
18331
18332static bool
18333arm_cookie_has_size (void)
18334{
18335 return TARGET_AAPCS_BASED;
18336}
44d10c10
PB
18337
18338
18339/* The EABI says constructors and destructors should return a pointer to
18340 the object constructed/destroyed. */
18341
18342static bool
18343arm_cxx_cdtor_returns_this (void)
18344{
18345 return TARGET_AAPCS_BASED;
18346}
c9ca9b88 18347
505970fc
MM
18348/* The EABI says that an inline function may never be the key
18349 method. */
18350
18351static bool
18352arm_cxx_key_method_may_be_inline (void)
18353{
18354 return !TARGET_AAPCS_BASED;
18355}
18356
1e731102
MM
18357static void
18358arm_cxx_determine_class_data_visibility (tree decl)
18359{
711b2998
JB
18360 if (!TARGET_AAPCS_BASED
18361 || !TARGET_DLLIMPORT_DECL_ATTRIBUTES)
1e731102 18362 return;
505970fc 18363
1e731102
MM
18364 /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
18365 is exported. However, on systems without dynamic vague linkage,
18366 \S 3.2.5.6 says that COMDAT class data has hidden linkage. */
18367 if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
18368 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
18369 else
18370 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
18371 DECL_VISIBILITY_SPECIFIED (decl) = 1;
18372}
e0b92319 18373
505970fc 18374static bool
1e731102 18375arm_cxx_class_data_always_comdat (void)
505970fc 18376{
1e731102
MM
18377 /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
18378 vague linkage if the class has no key function. */
18379 return !TARGET_AAPCS_BASED;
505970fc 18380}
c9ca9b88 18381
9f62c3e3
PB
18382
18383/* The EABI says __aeabi_atexit should be used to register static
18384 destructors. */
18385
18386static bool
18387arm_cxx_use_aeabi_atexit (void)
18388{
18389 return TARGET_AAPCS_BASED;
18390}
18391
18392
c9ca9b88
PB
18393void
18394arm_set_return_address (rtx source, rtx scratch)
18395{
18396 arm_stack_offsets *offsets;
18397 HOST_WIDE_INT delta;
18398 rtx addr;
18399 unsigned long saved_regs;
18400
954954d1
PB
18401 offsets = arm_get_frame_offsets ();
18402 saved_regs = offsets->saved_regs_mask;
c9ca9b88
PB
18403
18404 if ((saved_regs & (1 << LR_REGNUM)) == 0)
18405 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
18406 else
18407 {
18408 if (frame_pointer_needed)
18409 addr = plus_constant(hard_frame_pointer_rtx, -4);
18410 else
18411 {
18412 /* LR will be the first saved register. */
c9ca9b88
PB
18413 delta = offsets->outgoing_args - (offsets->frame + 4);
18414
f676971a 18415
c9ca9b88
PB
18416 if (delta >= 4096)
18417 {
18418 emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
18419 GEN_INT (delta & ~4095)));
18420 addr = scratch;
18421 delta &= 4095;
18422 }
18423 else
18424 addr = stack_pointer_rtx;
18425
18426 addr = plus_constant (addr, delta);
18427 }
31fa16b6 18428 emit_move_insn (gen_frame_mem (Pmode, addr), source);
c9ca9b88
PB
18429 }
18430}
18431
18432
18433void
18434thumb_set_return_address (rtx source, rtx scratch)
18435{
18436 arm_stack_offsets *offsets;
c9ca9b88 18437 HOST_WIDE_INT delta;
5b3e6663 18438 HOST_WIDE_INT limit;
c9ca9b88
PB
18439 int reg;
18440 rtx addr;
57934c39 18441 unsigned long mask;
c9ca9b88 18442
c41c1387 18443 emit_use (source);
c9ca9b88 18444
954954d1
PB
18445 offsets = arm_get_frame_offsets ();
18446 mask = offsets->saved_regs_mask;
57934c39 18447 if (mask & (1 << LR_REGNUM))
c9ca9b88 18448 {
5b3e6663 18449 limit = 1024;
c9ca9b88
PB
18450 /* Find the saved regs. */
18451 if (frame_pointer_needed)
18452 {
18453 delta = offsets->soft_frame - offsets->saved_args;
18454 reg = THUMB_HARD_FRAME_POINTER_REGNUM;
5b3e6663
PB
18455 if (TARGET_THUMB1)
18456 limit = 128;
c9ca9b88
PB
18457 }
18458 else
18459 {
18460 delta = offsets->outgoing_args - offsets->saved_args;
18461 reg = SP_REGNUM;
18462 }
18463 /* Allow for the stack frame. */
5b3e6663 18464 if (TARGET_THUMB1 && TARGET_BACKTRACE)
c9ca9b88
PB
18465 delta -= 16;
18466 /* The link register is always the first saved register. */
18467 delta -= 4;
f676971a 18468
c9ca9b88
PB
18469 /* Construct the address. */
18470 addr = gen_rtx_REG (SImode, reg);
5b3e6663 18471 if (delta > limit)
c9ca9b88
PB
18472 {
18473 emit_insn (gen_movsi (scratch, GEN_INT (delta)));
18474 emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
18475 addr = scratch;
18476 }
18477 else
18478 addr = plus_constant (addr, delta);
18479
31fa16b6 18480 emit_move_insn (gen_frame_mem (Pmode, addr), source);
c9ca9b88
PB
18481 }
18482 else
18483 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
18484}
18485
f676971a
EC
18486/* Implements target hook vector_mode_supported_p. */
18487bool
18488arm_vector_mode_supported_p (enum machine_mode mode)
18489{
88f77cba
JB
18490 /* Neon also supports V2SImode, etc. listed in the clause below. */
18491 if (TARGET_NEON && (mode == V2SFmode || mode == V4SImode || mode == V8HImode
18492 || mode == V16QImode || mode == V4SFmode || mode == V2DImode))
18493 return true;
18494
f676971a
EC
18495 if ((mode == V2SImode)
18496 || (mode == V4HImode)
18497 || (mode == V8QImode))
18498 return true;
18499
18500 return false;
18501}
273a2526
RS
18502
18503/* Implement TARGET_SHIFT_TRUNCATION_MASK. SImode shifts use normal
18504 ARM insns and therefore guarantee that the shift count is modulo 256.
18505 DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
18506 guarantee no particular behavior for out-of-range counts. */
18507
18508static unsigned HOST_WIDE_INT
18509arm_shift_truncation_mask (enum machine_mode mode)
18510{
18511 return mode == SImode ? 255 : 0;
18512}
2fa330b2
PB
18513
18514
18515/* Map internal gcc register numbers to DWARF2 register numbers. */
18516
18517unsigned int
18518arm_dbx_register_number (unsigned int regno)
18519{
18520 if (regno < 16)
18521 return regno;
18522
18523 /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
18524 compatibility. The EABI defines them as registers 96-103. */
18525 if (IS_FPA_REGNUM (regno))
18526 return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
18527
f1adb0a9 18528 /* FIXME: VFPv3 register numbering. */
2fa330b2
PB
18529 if (IS_VFP_REGNUM (regno))
18530 return 64 + regno - FIRST_VFP_REGNUM;
18531
18532 if (IS_IWMMXT_GR_REGNUM (regno))
18533 return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
18534
18535 if (IS_IWMMXT_REGNUM (regno))
18536 return 112 + regno - FIRST_IWMMXT_REGNUM;
18537
e6d29d15 18538 gcc_unreachable ();
2fa330b2
PB
18539}
18540
617a1b71
PB
18541
18542#ifdef TARGET_UNWIND_INFO
5b3e6663
PB
18543/* Emit unwind directives for a store-multiple instruction or stack pointer
18544 push during alignment.
18545 These should only ever be generated by the function prologue code, so
18546 expect them to have a particular form. */
617a1b71
PB
18547
18548static void
5b3e6663 18549arm_unwind_emit_sequence (FILE * asm_out_file, rtx p)
617a1b71
PB
18550{
18551 int i;
18552 HOST_WIDE_INT offset;
18553 HOST_WIDE_INT nregs;
18554 int reg_size;
18555 unsigned reg;
18556 unsigned lastreg;
18557 rtx e;
18558
617a1b71 18559 e = XVECEXP (p, 0, 0);
5b3e6663
PB
18560 if (GET_CODE (e) != SET)
18561 abort ();
18562
18563 /* First insn will adjust the stack pointer. */
617a1b71
PB
18564 if (GET_CODE (e) != SET
18565 || GET_CODE (XEXP (e, 0)) != REG
18566 || REGNO (XEXP (e, 0)) != SP_REGNUM
18567 || GET_CODE (XEXP (e, 1)) != PLUS)
18568 abort ();
18569
18570 offset = -INTVAL (XEXP (XEXP (e, 1), 1));
18571 nregs = XVECLEN (p, 0) - 1;
18572
18573 reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
18574 if (reg < 16)
18575 {
18576 /* The function prologue may also push pc, but not annotate it as it is
569b7f6a 18577 never restored. We turn this into a stack pointer adjustment. */
617a1b71
PB
18578 if (nregs * 4 == offset - 4)
18579 {
18580 fprintf (asm_out_file, "\t.pad #4\n");
18581 offset -= 4;
18582 }
18583 reg_size = 4;
8edfc4cc 18584 fprintf (asm_out_file, "\t.save {");
617a1b71
PB
18585 }
18586 else if (IS_VFP_REGNUM (reg))
18587 {
617a1b71 18588 reg_size = 8;
8edfc4cc 18589 fprintf (asm_out_file, "\t.vsave {");
617a1b71
PB
18590 }
18591 else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
18592 {
18593 /* FPA registers are done differently. */
ea40ba9c 18594 asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
617a1b71
PB
18595 return;
18596 }
18597 else
18598 /* Unknown register type. */
18599 abort ();
18600
18601 /* If the stack increment doesn't match the size of the saved registers,
18602 something has gone horribly wrong. */
18603 if (offset != nregs * reg_size)
18604 abort ();
18605
617a1b71
PB
18606 offset = 0;
18607 lastreg = 0;
18608 /* The remaining insns will describe the stores. */
18609 for (i = 1; i <= nregs; i++)
18610 {
18611 /* Expect (set (mem <addr>) (reg)).
18612 Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)). */
18613 e = XVECEXP (p, 0, i);
18614 if (GET_CODE (e) != SET
18615 || GET_CODE (XEXP (e, 0)) != MEM
18616 || GET_CODE (XEXP (e, 1)) != REG)
18617 abort ();
e0b92319 18618
617a1b71
PB
18619 reg = REGNO (XEXP (e, 1));
18620 if (reg < lastreg)
18621 abort ();
e0b92319 18622
617a1b71
PB
18623 if (i != 1)
18624 fprintf (asm_out_file, ", ");
18625 /* We can't use %r for vfp because we need to use the
18626 double precision register names. */
18627 if (IS_VFP_REGNUM (reg))
18628 asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
18629 else
18630 asm_fprintf (asm_out_file, "%r", reg);
18631
18632#ifdef ENABLE_CHECKING
18633 /* Check that the addresses are consecutive. */
18634 e = XEXP (XEXP (e, 0), 0);
18635 if (GET_CODE (e) == PLUS)
18636 {
18637 offset += reg_size;
18638 if (GET_CODE (XEXP (e, 0)) != REG
18639 || REGNO (XEXP (e, 0)) != SP_REGNUM
18640 || GET_CODE (XEXP (e, 1)) != CONST_INT
18641 || offset != INTVAL (XEXP (e, 1)))
18642 abort ();
18643 }
18644 else if (i != 1
18645 || GET_CODE (e) != REG
18646 || REGNO (e) != SP_REGNUM)
18647 abort ();
18648#endif
18649 }
18650 fprintf (asm_out_file, "}\n");
18651}
18652
18653/* Emit unwind directives for a SET. */
18654
18655static void
18656arm_unwind_emit_set (FILE * asm_out_file, rtx p)
18657{
18658 rtx e0;
18659 rtx e1;
5b3e6663 18660 unsigned reg;
617a1b71
PB
18661
18662 e0 = XEXP (p, 0);
18663 e1 = XEXP (p, 1);
18664 switch (GET_CODE (e0))
18665 {
18666 case MEM:
18667 /* Pushing a single register. */
18668 if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
18669 || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
18670 || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
18671 abort ();
18672
18673 asm_fprintf (asm_out_file, "\t.save ");
18674 if (IS_VFP_REGNUM (REGNO (e1)))
18675 asm_fprintf(asm_out_file, "{d%d}\n",
18676 (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
18677 else
18678 asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
18679 break;
18680
18681 case REG:
18682 if (REGNO (e0) == SP_REGNUM)
18683 {
18684 /* A stack increment. */
18685 if (GET_CODE (e1) != PLUS
18686 || GET_CODE (XEXP (e1, 0)) != REG
18687 || REGNO (XEXP (e1, 0)) != SP_REGNUM
18688 || GET_CODE (XEXP (e1, 1)) != CONST_INT)
18689 abort ();
18690
ea40ba9c 18691 asm_fprintf (asm_out_file, "\t.pad #%wd\n",
617a1b71
PB
18692 -INTVAL (XEXP (e1, 1)));
18693 }
18694 else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
18695 {
18696 HOST_WIDE_INT offset;
e0b92319 18697
617a1b71
PB
18698 if (GET_CODE (e1) == PLUS)
18699 {
18700 if (GET_CODE (XEXP (e1, 0)) != REG
18701 || GET_CODE (XEXP (e1, 1)) != CONST_INT)
18702 abort ();
18703 reg = REGNO (XEXP (e1, 0));
18704 offset = INTVAL (XEXP (e1, 1));
ea40ba9c 18705 asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
617a1b71
PB
18706 HARD_FRAME_POINTER_REGNUM, reg,
18707 INTVAL (XEXP (e1, 1)));
18708 }
18709 else if (GET_CODE (e1) == REG)
18710 {
18711 reg = REGNO (e1);
18712 asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
18713 HARD_FRAME_POINTER_REGNUM, reg);
18714 }
18715 else
18716 abort ();
18717 }
18718 else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
18719 {
18720 /* Move from sp to reg. */
18721 asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
18722 }
758ed9b2
PB
18723 else if (GET_CODE (e1) == PLUS
18724 && GET_CODE (XEXP (e1, 0)) == REG
18725 && REGNO (XEXP (e1, 0)) == SP_REGNUM
18726 && GET_CODE (XEXP (e1, 1)) == CONST_INT)
18727 {
18728 /* Set reg to offset from sp. */
18729 asm_fprintf (asm_out_file, "\t.movsp %r, #%d\n",
18730 REGNO (e0), (int)INTVAL(XEXP (e1, 1)));
18731 }
5b3e6663
PB
18732 else if (GET_CODE (e1) == UNSPEC && XINT (e1, 1) == UNSPEC_STACK_ALIGN)
18733 {
18734 /* Stack pointer save before alignment. */
18735 reg = REGNO (e0);
18736 asm_fprintf (asm_out_file, "\t.unwind_raw 0, 0x%x @ vsp = r%d\n",
18737 reg + 0x90, reg);
18738 }
617a1b71
PB
18739 else
18740 abort ();
18741 break;
18742
18743 default:
18744 abort ();
18745 }
18746}
18747
18748
18749/* Emit unwind directives for the given insn. */
18750
18751static void
18752arm_unwind_emit (FILE * asm_out_file, rtx insn)
18753{
18754 rtx pat;
18755
18756 if (!ARM_EABI_UNWIND_TABLES)
18757 return;
18758
e3b5732b 18759 if (!(flag_unwind_tables || crtl->uses_eh_lsda)
80efdb6a 18760 && (TREE_NOTHROW (current_function_decl)
ad516a74 18761 || crtl->all_throwers_are_sibcalls))
80efdb6a
PB
18762 return;
18763
617a1b71
PB
18764 if (GET_CODE (insn) == NOTE || !RTX_FRAME_RELATED_P (insn))
18765 return;
18766
18767 pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
18768 if (pat)
18769 pat = XEXP (pat, 0);
18770 else
18771 pat = PATTERN (insn);
18772
18773 switch (GET_CODE (pat))
18774 {
18775 case SET:
18776 arm_unwind_emit_set (asm_out_file, pat);
18777 break;
18778
18779 case SEQUENCE:
18780 /* Store multiple. */
5b3e6663 18781 arm_unwind_emit_sequence (asm_out_file, pat);
617a1b71
PB
18782 break;
18783
18784 default:
18785 abort();
18786 }
18787}
18788
18789
18790/* Output a reference from a function exception table to the type_info
18791 object X. The EABI specifies that the symbol should be relocated by
18792 an R_ARM_TARGET2 relocation. */
18793
18794static bool
18795arm_output_ttype (rtx x)
18796{
18797 fputs ("\t.word\t", asm_out_file);
18798 output_addr_const (asm_out_file, x);
18799 /* Use special relocations for symbol references. */
18800 if (GET_CODE (x) != CONST_INT)
18801 fputs ("(TARGET2)", asm_out_file);
18802 fputc ('\n', asm_out_file);
18803
18804 return TRUE;
18805}
18806#endif /* TARGET_UNWIND_INFO */
18807
18808
5b3e6663
PB
18809/* Handle UNSPEC DWARF call frame instructions. These are needed for dynamic
18810 stack alignment. */
18811
18812static void
18813arm_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
18814{
18815 rtx unspec = SET_SRC (pattern);
18816 gcc_assert (GET_CODE (unspec) == UNSPEC);
18817
18818 switch (index)
18819 {
18820 case UNSPEC_STACK_ALIGN:
18821 /* ??? We should set the CFA = (SP & ~7). At this point we haven't
18822 put anything on the stack, so hopefully it won't matter.
18823 CFA = SP will be correct after alignment. */
18824 dwarf2out_reg_save_reg (label, stack_pointer_rtx,
18825 SET_DEST (pattern));
18826 break;
18827 default:
18828 gcc_unreachable ();
18829 }
18830}
18831
18832
617a1b71
PB
18833/* Output unwind directives for the start/end of a function. */
18834
18835void
18836arm_output_fn_unwind (FILE * f, bool prologue)
18837{
18838 if (!ARM_EABI_UNWIND_TABLES)
18839 return;
18840
18841 if (prologue)
18842 fputs ("\t.fnstart\n", f);
18843 else
80efdb6a
PB
18844 {
18845 /* If this function will never be unwound, then mark it as such.
18846 The came condition is used in arm_unwind_emit to suppress
18847 the frame annotations. */
e3b5732b 18848 if (!(flag_unwind_tables || crtl->uses_eh_lsda)
80efdb6a 18849 && (TREE_NOTHROW (current_function_decl)
ad516a74 18850 || crtl->all_throwers_are_sibcalls))
80efdb6a
PB
18851 fputs("\t.cantunwind\n", f);
18852
18853 fputs ("\t.fnend\n", f);
18854 }
617a1b71 18855}
d3585b76
DJ
18856
18857static bool
18858arm_emit_tls_decoration (FILE *fp, rtx x)
18859{
18860 enum tls_reloc reloc;
18861 rtx val;
18862
18863 val = XVECEXP (x, 0, 0);
18864 reloc = INTVAL (XVECEXP (x, 0, 1));
18865
18866 output_addr_const (fp, val);
18867
18868 switch (reloc)
18869 {
18870 case TLS_GD32:
18871 fputs ("(tlsgd)", fp);
18872 break;
18873 case TLS_LDM32:
18874 fputs ("(tlsldm)", fp);
18875 break;
18876 case TLS_LDO32:
18877 fputs ("(tlsldo)", fp);
18878 break;
18879 case TLS_IE32:
18880 fputs ("(gottpoff)", fp);
18881 break;
18882 case TLS_LE32:
18883 fputs ("(tpoff)", fp);
18884 break;
18885 default:
18886 gcc_unreachable ();
18887 }
18888
18889 switch (reloc)
18890 {
18891 case TLS_GD32:
18892 case TLS_LDM32:
18893 case TLS_IE32:
18894 fputs (" + (. - ", fp);
18895 output_addr_const (fp, XVECEXP (x, 0, 2));
18896 fputs (" - ", fp);
18897 output_addr_const (fp, XVECEXP (x, 0, 3));
18898 fputc (')', fp);
18899 break;
18900 default:
18901 break;
18902 }
18903
18904 return TRUE;
18905}
18906
afcc986d
JM
18907/* ARM implementation of TARGET_ASM_OUTPUT_DWARF_DTPREL. */
18908
18909static void
18910arm_output_dwarf_dtprel (FILE *file, int size, rtx x)
18911{
18912 gcc_assert (size == 4);
18913 fputs ("\t.word\t", file);
18914 output_addr_const (file, x);
18915 fputs ("(tlsldo)", file);
18916}
18917
d3585b76
DJ
18918bool
18919arm_output_addr_const_extra (FILE *fp, rtx x)
18920{
18921 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
18922 return arm_emit_tls_decoration (fp, x);
f16fe45f
DJ
18923 else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
18924 {
18925 char label[256];
18926 int labelno = INTVAL (XVECEXP (x, 0, 0));
18927
18928 ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
18929 assemble_name_raw (fp, label);
18930
18931 return TRUE;
18932 }
d3585b76
DJ
18933 else if (GET_CODE (x) == CONST_VECTOR)
18934 return arm_emit_vector_const (fp, x);
18935
18936 return FALSE;
18937}
18938
5b3e6663
PB
18939/* Output assembly for a shift instruction.
18940 SET_FLAGS determines how the instruction modifies the condition codes.
7a085dce 18941 0 - Do not set condition codes.
5b3e6663
PB
18942 1 - Set condition codes.
18943 2 - Use smallest instruction. */
18944const char *
18945arm_output_shift(rtx * operands, int set_flags)
18946{
18947 char pattern[100];
18948 static const char flag_chars[3] = {'?', '.', '!'};
18949 const char *shift;
18950 HOST_WIDE_INT val;
18951 char c;
18952
18953 c = flag_chars[set_flags];
18954 if (TARGET_UNIFIED_ASM)
18955 {
18956 shift = shift_op(operands[3], &val);
18957 if (shift)
18958 {
18959 if (val != -1)
18960 operands[2] = GEN_INT(val);
18961 sprintf (pattern, "%s%%%c\t%%0, %%1, %%2", shift, c);
18962 }
18963 else
18964 sprintf (pattern, "mov%%%c\t%%0, %%1", c);
18965 }
18966 else
18967 sprintf (pattern, "mov%%%c\t%%0, %%1%%S3", c);
18968 output_asm_insn (pattern, operands);
18969 return "";
18970}
18971
18972/* Output a Thumb-2 casesi instruction. */
18973const char *
18974thumb2_output_casesi (rtx *operands)
18975{
18976 rtx diff_vec = PATTERN (next_real_insn (operands[2]));
18977
18978 gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
18979
18980 output_asm_insn ("cmp\t%0, %1", operands);
18981 output_asm_insn ("bhi\t%l3", operands);
18982 switch (GET_MODE(diff_vec))
18983 {
18984 case QImode:
18985 return "tbb\t[%|pc, %0]";
18986 case HImode:
18987 return "tbh\t[%|pc, %0, lsl #1]";
18988 case SImode:
18989 if (flag_pic)
18990 {
18991 output_asm_insn ("adr\t%4, %l2", operands);
18992 output_asm_insn ("ldr\t%5, [%4, %0, lsl #2]", operands);
18993 output_asm_insn ("add\t%4, %4, %5", operands);
18994 return "bx\t%4";
18995 }
18996 else
18997 {
18998 output_asm_insn ("adr\t%4, %l2", operands);
18999 return "ldr\t%|pc, [%4, %0, lsl #2]";
19000 }
19001 default:
19002 gcc_unreachable ();
19003 }
19004}
19005
bd4dc3cd
PB
19006/* Most ARM cores are single issue, but some newer ones can dual issue.
19007 The scheduler descriptions rely on this being correct. */
19008static int
19009arm_issue_rate (void)
19010{
19011 switch (arm_tune)
19012 {
19013 case cortexr4:
19014 case cortexa8:
19015 return 2;
19016
19017 default:
19018 return 1;
19019 }
19020}
19021
608063c3
JB
19022/* A table and a function to perform ARM-specific name mangling for
19023 NEON vector types in order to conform to the AAPCS (see "Procedure
19024 Call Standard for the ARM Architecture", Appendix A). To qualify
19025 for emission with the mangled names defined in that document, a
19026 vector type must not only be of the correct mode but also be
19027 composed of NEON vector element types (e.g. __builtin_neon_qi). */
19028typedef struct
19029{
19030 enum machine_mode mode;
19031 const char *element_type_name;
19032 const char *aapcs_name;
19033} arm_mangle_map_entry;
19034
19035static arm_mangle_map_entry arm_mangle_map[] = {
19036 /* 64-bit containerized types. */
19037 { V8QImode, "__builtin_neon_qi", "15__simd64_int8_t" },
19038 { V8QImode, "__builtin_neon_uqi", "16__simd64_uint8_t" },
19039 { V4HImode, "__builtin_neon_hi", "16__simd64_int16_t" },
19040 { V4HImode, "__builtin_neon_uhi", "17__simd64_uint16_t" },
19041 { V2SImode, "__builtin_neon_si", "16__simd64_int32_t" },
19042 { V2SImode, "__builtin_neon_usi", "17__simd64_uint32_t" },
19043 { V2SFmode, "__builtin_neon_sf", "18__simd64_float32_t" },
19044 { V8QImode, "__builtin_neon_poly8", "16__simd64_poly8_t" },
19045 { V4HImode, "__builtin_neon_poly16", "17__simd64_poly16_t" },
19046 /* 128-bit containerized types. */
19047 { V16QImode, "__builtin_neon_qi", "16__simd128_int8_t" },
19048 { V16QImode, "__builtin_neon_uqi", "17__simd128_uint8_t" },
19049 { V8HImode, "__builtin_neon_hi", "17__simd128_int16_t" },
19050 { V8HImode, "__builtin_neon_uhi", "18__simd128_uint16_t" },
19051 { V4SImode, "__builtin_neon_si", "17__simd128_int32_t" },
19052 { V4SImode, "__builtin_neon_usi", "18__simd128_uint32_t" },
19053 { V4SFmode, "__builtin_neon_sf", "19__simd128_float32_t" },
19054 { V16QImode, "__builtin_neon_poly8", "17__simd128_poly8_t" },
19055 { V8HImode, "__builtin_neon_poly16", "18__simd128_poly16_t" },
19056 { VOIDmode, NULL, NULL }
19057};
19058
19059const char *
3101faab 19060arm_mangle_type (const_tree type)
608063c3
JB
19061{
19062 arm_mangle_map_entry *pos = arm_mangle_map;
19063
19064 if (TREE_CODE (type) != VECTOR_TYPE)
19065 return NULL;
19066
19067 /* Check the mode of the vector type, and the name of the vector
19068 element type, against the table. */
19069 while (pos->mode != VOIDmode)
19070 {
19071 tree elt_type = TREE_TYPE (type);
19072
19073 if (pos->mode == TYPE_MODE (type)
19074 && TREE_CODE (TYPE_NAME (elt_type)) == TYPE_DECL
19075 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (elt_type))),
19076 pos->element_type_name))
19077 return pos->aapcs_name;
19078
19079 pos++;
19080 }
19081
19082 /* Use the default mangling for unrecognized (possibly user-defined)
19083 vector types. */
19084 return NULL;
19085}
19086
795dc4fc
PB
19087/* Order of allocation of core registers for Thumb: this allocation is
19088 written over the corresponding initial entries of the array
19089 initialized with REG_ALLOC_ORDER. We allocate all low registers
19090 first. Saving and restoring a low register is usually cheaper than
19091 using a call-clobbered high register. */
19092
19093static const int thumb_core_reg_alloc_order[] =
19094{
19095 3, 2, 1, 0, 4, 5, 6, 7,
19096 14, 12, 8, 9, 10, 11, 13, 15
19097};
19098
19099/* Adjust register allocation order when compiling for Thumb. */
19100
19101void
19102arm_order_regs_for_local_alloc (void)
19103{
19104 const int arm_reg_alloc_order[] = REG_ALLOC_ORDER;
19105 memcpy(reg_alloc_order, arm_reg_alloc_order, sizeof (reg_alloc_order));
19106 if (TARGET_THUMB)
19107 memcpy (reg_alloc_order, thumb_core_reg_alloc_order,
19108 sizeof (thumb_core_reg_alloc_order));
19109}
19110
f67358da
PB
19111/* Set default optimization options. */
19112void
19113arm_optimization_options (int level, int size ATTRIBUTE_UNUSED)
19114{
19115 /* Enable section anchors by default at -O1 or higher. */
19116 flag_section_anchors = (level > 0 ? 1 : 0);
19117}
19118
d3585b76 19119#include "gt-arm.h"
This page took 5.973685 seconds and 5 git commands to generate.