OSDN Git Service

PR bootstrap/45177
[pf3gnuchains/gcc-fork.git] / gcc / config / arm / arm.c
1 /* Output routines for GCC for ARM.
2    Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3    2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5    Contributed by Pieter `Tiggr' Schoenmakers (rcpieter@win.tue.nl)
6    and Martin Simmons (@harleqn.co.uk).
7    More major hacks by Richard Earnshaw (rearnsha@arm.com).
8
9    This file is part of GCC.
10
11    GCC is free software; you can redistribute it and/or modify it
12    under the terms of the GNU General Public License as published
13    by the Free Software Foundation; either version 3, or (at your
14    option) any later version.
15
16    GCC is distributed in the hope that it will be useful, but WITHOUT
17    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
19    License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with GCC; see the file COPYING3.  If not see
23    <http://www.gnu.org/licenses/>.  */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "obstack.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "insn-config.h"
35 #include "conditions.h"
36 #include "output.h"
37 #include "insn-attr.h"
38 #include "flags.h"
39 #include "reload.h"
40 #include "function.h"
41 #include "expr.h"
42 #include "optabs.h"
43 #include "diagnostic-core.h"
44 #include "toplev.h"
45 #include "recog.h"
46 #include "cgraph.h"
47 #include "ggc.h"
48 #include "except.h"
49 #include "c-family/c-pragma.h"  /* ??? */
50 #include "integrate.h"
51 #include "tm_p.h"
52 #include "target.h"
53 #include "target-def.h"
54 #include "debug.h"
55 #include "langhooks.h"
56 #include "df.h"
57 #include "intl.h"
58 #include "libfuncs.h"
59 #include "params.h"
60
61 /* Forward definitions of types.  */
62 typedef struct minipool_node    Mnode;
63 typedef struct minipool_fixup   Mfix;
64
65 void (*arm_lang_output_object_attributes_hook)(void);
66
67 /* Forward function declarations.  */
68 static int arm_compute_static_chain_stack_bytes (void);
69 static arm_stack_offsets *arm_get_frame_offsets (void);
70 static void arm_add_gc_roots (void);
71 static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
72                              HOST_WIDE_INT, rtx, rtx, int, int);
73 static unsigned bit_count (unsigned long);
74 static int arm_address_register_rtx_p (rtx, int);
75 static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
76 static int thumb2_legitimate_index_p (enum machine_mode, rtx, int);
77 static int thumb1_base_register_rtx_p (rtx, enum machine_mode, int);
78 static rtx arm_legitimize_address (rtx, rtx, enum machine_mode);
79 static rtx thumb_legitimize_address (rtx, rtx, enum machine_mode);
80 inline static int thumb1_index_register_rtx_p (rtx, int);
81 static bool arm_legitimate_address_p (enum machine_mode, rtx, bool);
82 static int thumb_far_jump_used_p (void);
83 static bool thumb_force_lr_save (void);
84 static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
85 static rtx emit_sfm (int, int);
86 static unsigned arm_size_return_regs (void);
87 static bool arm_assemble_integer (rtx, unsigned int, int);
88 static void arm_print_operand (FILE *, rtx, int);
89 static void arm_print_operand_address (FILE *, rtx);
90 static bool arm_print_operand_punct_valid_p (unsigned char code);
91 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
92 static arm_cc get_arm_condition_code (rtx);
93 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
94 static rtx is_jump_table (rtx);
95 static const char *output_multi_immediate (rtx *, const char *, const char *,
96                                            int, HOST_WIDE_INT);
97 static const char *shift_op (rtx, HOST_WIDE_INT *);
98 static struct machine_function *arm_init_machine_status (void);
99 static void thumb_exit (FILE *, int);
100 static rtx is_jump_table (rtx);
101 static HOST_WIDE_INT get_jump_table_size (rtx);
102 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
103 static Mnode *add_minipool_forward_ref (Mfix *);
104 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
105 static Mnode *add_minipool_backward_ref (Mfix *);
106 static void assign_minipool_offsets (Mfix *);
107 static void arm_print_value (FILE *, rtx);
108 static void dump_minipool (rtx);
109 static int arm_barrier_cost (rtx);
110 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
111 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
112 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
113                                rtx);
114 static void arm_reorg (void);
115 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
116 static unsigned long arm_compute_save_reg0_reg12_mask (void);
117 static unsigned long arm_compute_save_reg_mask (void);
118 static unsigned long arm_isr_value (tree);
119 static unsigned long arm_compute_func_type (void);
120 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
121 static tree arm_handle_pcs_attribute (tree *, tree, tree, int, bool *);
122 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
123 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
124 static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
125 #endif
126 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
127 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
128 static void thumb1_output_function_prologue (FILE *, HOST_WIDE_INT);
129 static int arm_comp_type_attributes (const_tree, const_tree);
130 static void arm_set_default_type_attributes (tree);
131 static int arm_adjust_cost (rtx, rtx, rtx, int);
132 static int count_insns_for_constant (HOST_WIDE_INT, int);
133 static int arm_get_strip_length (int);
134 static bool arm_function_ok_for_sibcall (tree, tree);
135 static enum machine_mode arm_promote_function_mode (const_tree,
136                                                     enum machine_mode, int *,
137                                                     const_tree, int);
138 static bool arm_return_in_memory (const_tree, const_tree);
139 static rtx arm_function_value (const_tree, const_tree, bool);
140 static rtx arm_libcall_value (enum machine_mode, const_rtx);
141
142 static void arm_internal_label (FILE *, const char *, unsigned long);
143 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
144                                  tree);
145 static bool arm_have_conditional_execution (void);
146 static bool arm_rtx_costs_1 (rtx, enum rtx_code, int*, bool);
147 static bool arm_size_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *);
148 static bool arm_slowmul_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
149 static bool arm_fastmul_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
150 static bool arm_xscale_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
151 static bool arm_9e_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
152 static bool arm_rtx_costs (rtx, int, int, int *, bool);
153 static int arm_address_cost (rtx, bool);
154 static bool arm_memory_load_p (rtx);
155 static bool arm_cirrus_insn_p (rtx);
156 static void cirrus_reorg (rtx);
157 static void arm_init_builtins (void);
158 static void arm_init_iwmmxt_builtins (void);
159 static rtx safe_vector_operand (rtx, enum machine_mode);
160 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
161 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
162 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
163 static void emit_constant_insn (rtx cond, rtx pattern);
164 static rtx emit_set_insn (rtx, rtx);
165 static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
166                                   tree, bool);
167 static rtx aapcs_allocate_return_reg (enum machine_mode, const_tree,
168                                       const_tree);
169 static int aapcs_select_return_coproc (const_tree, const_tree);
170
171 #ifdef OBJECT_FORMAT_ELF
172 static void arm_elf_asm_constructor (rtx, int) ATTRIBUTE_UNUSED;
173 static void arm_elf_asm_destructor (rtx, int) ATTRIBUTE_UNUSED;
174 #endif
175 #ifndef ARM_PE
176 static void arm_encode_section_info (tree, rtx, int);
177 #endif
178
179 static void arm_file_end (void);
180 static void arm_file_start (void);
181
182 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
183                                         tree, int *, int);
184 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
185                                    enum machine_mode, const_tree, bool);
186 static bool arm_promote_prototypes (const_tree);
187 static bool arm_default_short_enums (void);
188 static bool arm_align_anon_bitfield (void);
189 static bool arm_return_in_msb (const_tree);
190 static bool arm_must_pass_in_stack (enum machine_mode, const_tree);
191 static bool arm_return_in_memory (const_tree, const_tree);
192 #ifdef TARGET_UNWIND_INFO
193 static void arm_unwind_emit (FILE *, rtx);
194 static bool arm_output_ttype (rtx);
195 #endif
196 static void arm_dwarf_handle_frame_unspec (const char *, rtx, int);
197 static rtx arm_dwarf_register_span (rtx);
198
199 static tree arm_cxx_guard_type (void);
200 static bool arm_cxx_guard_mask_bit (void);
201 static tree arm_get_cookie_size (tree);
202 static bool arm_cookie_has_size (void);
203 static bool arm_cxx_cdtor_returns_this (void);
204 static bool arm_cxx_key_method_may_be_inline (void);
205 static void arm_cxx_determine_class_data_visibility (tree);
206 static bool arm_cxx_class_data_always_comdat (void);
207 static bool arm_cxx_use_aeabi_atexit (void);
208 static void arm_init_libfuncs (void);
209 static tree arm_build_builtin_va_list (void);
210 static void arm_expand_builtin_va_start (tree, rtx);
211 static tree arm_gimplify_va_arg_expr (tree, tree, gimple_seq *, gimple_seq *);
212 static bool arm_handle_option (size_t, const char *, int);
213 static void arm_target_help (void);
214 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
215 static bool arm_cannot_copy_insn_p (rtx);
216 static bool arm_tls_symbol_p (rtx x);
217 static int arm_issue_rate (void);
218 static void arm_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
219 static bool arm_allocate_stack_slots_for_args (void);
220 static const char *arm_invalid_parameter_type (const_tree t);
221 static const char *arm_invalid_return_type (const_tree t);
222 static tree arm_promoted_type (const_tree t);
223 static tree arm_convert_to_type (tree type, tree expr);
224 static bool arm_scalar_mode_supported_p (enum machine_mode);
225 static bool arm_frame_pointer_required (void);
226 static bool arm_can_eliminate (const int, const int);
227 static void arm_asm_trampoline_template (FILE *);
228 static void arm_trampoline_init (rtx, tree, rtx);
229 static rtx arm_trampoline_adjust_address (rtx);
230 static rtx arm_pic_static_addr (rtx orig, rtx reg);
231 static bool cortex_a9_sched_adjust_cost (rtx, rtx, rtx, int *);
232 static bool xscale_sched_adjust_cost (rtx, rtx, rtx, int *);
233
234 \f
235 /* Table of machine attributes.  */
236 static const struct attribute_spec arm_attribute_table[] =
237 {
238   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
239   /* Function calls made to this symbol must be done indirectly, because
240      it may lie outside of the 26 bit addressing range of a normal function
241      call.  */
242   { "long_call",    0, 0, false, true,  true,  NULL },
243   /* Whereas these functions are always known to reside within the 26 bit
244      addressing range.  */
245   { "short_call",   0, 0, false, true,  true,  NULL },
246   /* Specify the procedure call conventions for a function.  */
247   { "pcs",          1, 1, false, true,  true,  arm_handle_pcs_attribute },
248   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
249   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
250   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
251   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
252 #ifdef ARM_PE
253   /* ARM/PE has three new attributes:
254      interfacearm - ?
255      dllexport - for exporting a function/variable that will live in a dll
256      dllimport - for importing a function/variable from a dll
257
258      Microsoft allows multiple declspecs in one __declspec, separating
259      them with spaces.  We do NOT support this.  Instead, use __declspec
260      multiple times.
261   */
262   { "dllimport",    0, 0, true,  false, false, NULL },
263   { "dllexport",    0, 0, true,  false, false, NULL },
264   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
265 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
266   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
267   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
268   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
269 #endif
270   { NULL,           0, 0, false, false, false, NULL }
271 };
272 \f
273 /* Initialize the GCC target structure.  */
274 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
275 #undef  TARGET_MERGE_DECL_ATTRIBUTES
276 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
277 #endif
278
279 #undef TARGET_LEGITIMIZE_ADDRESS
280 #define TARGET_LEGITIMIZE_ADDRESS arm_legitimize_address
281
282 #undef  TARGET_ATTRIBUTE_TABLE
283 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
284
285 #undef TARGET_ASM_FILE_START
286 #define TARGET_ASM_FILE_START arm_file_start
287 #undef TARGET_ASM_FILE_END
288 #define TARGET_ASM_FILE_END arm_file_end
289
290 #undef  TARGET_ASM_ALIGNED_SI_OP
291 #define TARGET_ASM_ALIGNED_SI_OP NULL
292 #undef  TARGET_ASM_INTEGER
293 #define TARGET_ASM_INTEGER arm_assemble_integer
294
295 #undef TARGET_PRINT_OPERAND
296 #define TARGET_PRINT_OPERAND arm_print_operand
297 #undef TARGET_PRINT_OPERAND_ADDRESS
298 #define TARGET_PRINT_OPERAND_ADDRESS arm_print_operand_address
299 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
300 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P arm_print_operand_punct_valid_p
301
302 #undef  TARGET_ASM_FUNCTION_PROLOGUE
303 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
304
305 #undef  TARGET_ASM_FUNCTION_EPILOGUE
306 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
307
308 #undef  TARGET_DEFAULT_TARGET_FLAGS
309 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
310 #undef  TARGET_HANDLE_OPTION
311 #define TARGET_HANDLE_OPTION arm_handle_option
312 #undef  TARGET_HELP
313 #define TARGET_HELP arm_target_help
314
315 #undef  TARGET_COMP_TYPE_ATTRIBUTES
316 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
317
318 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
319 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
320
321 #undef  TARGET_SCHED_ADJUST_COST
322 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
323
324 #undef TARGET_ENCODE_SECTION_INFO
325 #ifdef ARM_PE
326 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
327 #else
328 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
329 #endif
330
331 #undef  TARGET_STRIP_NAME_ENCODING
332 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
333
334 #undef  TARGET_ASM_INTERNAL_LABEL
335 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
336
337 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
338 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
339
340 #undef  TARGET_FUNCTION_VALUE
341 #define TARGET_FUNCTION_VALUE arm_function_value
342
343 #undef  TARGET_LIBCALL_VALUE
344 #define TARGET_LIBCALL_VALUE arm_libcall_value
345
346 #undef  TARGET_ASM_OUTPUT_MI_THUNK
347 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
348 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
349 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
350
351 #undef  TARGET_RTX_COSTS
352 #define TARGET_RTX_COSTS arm_rtx_costs
353 #undef  TARGET_ADDRESS_COST
354 #define TARGET_ADDRESS_COST arm_address_cost
355
356 #undef TARGET_SHIFT_TRUNCATION_MASK
357 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
358 #undef TARGET_VECTOR_MODE_SUPPORTED_P
359 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
360
361 #undef  TARGET_MACHINE_DEPENDENT_REORG
362 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
363
364 #undef  TARGET_INIT_BUILTINS
365 #define TARGET_INIT_BUILTINS  arm_init_builtins
366 #undef  TARGET_EXPAND_BUILTIN
367 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
368
369 #undef TARGET_INIT_LIBFUNCS
370 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
371
372 #undef TARGET_PROMOTE_FUNCTION_MODE
373 #define TARGET_PROMOTE_FUNCTION_MODE arm_promote_function_mode
374 #undef TARGET_PROMOTE_PROTOTYPES
375 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
376 #undef TARGET_PASS_BY_REFERENCE
377 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
378 #undef TARGET_ARG_PARTIAL_BYTES
379 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
380
381 #undef  TARGET_SETUP_INCOMING_VARARGS
382 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
383
384 #undef TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS
385 #define TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS arm_allocate_stack_slots_for_args
386
387 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
388 #define TARGET_ASM_TRAMPOLINE_TEMPLATE arm_asm_trampoline_template
389 #undef TARGET_TRAMPOLINE_INIT
390 #define TARGET_TRAMPOLINE_INIT arm_trampoline_init
391 #undef TARGET_TRAMPOLINE_ADJUST_ADDRESS
392 #define TARGET_TRAMPOLINE_ADJUST_ADDRESS arm_trampoline_adjust_address
393
394 #undef TARGET_DEFAULT_SHORT_ENUMS
395 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
396
397 #undef TARGET_ALIGN_ANON_BITFIELD
398 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
399
400 #undef TARGET_NARROW_VOLATILE_BITFIELD
401 #define TARGET_NARROW_VOLATILE_BITFIELD hook_bool_void_false
402
403 #undef TARGET_CXX_GUARD_TYPE
404 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
405
406 #undef TARGET_CXX_GUARD_MASK_BIT
407 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
408
409 #undef TARGET_CXX_GET_COOKIE_SIZE
410 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
411
412 #undef TARGET_CXX_COOKIE_HAS_SIZE
413 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
414
415 #undef TARGET_CXX_CDTOR_RETURNS_THIS
416 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
417
418 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
419 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
420
421 #undef TARGET_CXX_USE_AEABI_ATEXIT
422 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
423
424 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
425 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
426   arm_cxx_determine_class_data_visibility
427
428 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
429 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
430
431 #undef TARGET_RETURN_IN_MSB
432 #define TARGET_RETURN_IN_MSB arm_return_in_msb
433
434 #undef TARGET_RETURN_IN_MEMORY
435 #define TARGET_RETURN_IN_MEMORY arm_return_in_memory
436
437 #undef TARGET_MUST_PASS_IN_STACK
438 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
439
440 #ifdef TARGET_UNWIND_INFO
441 #undef TARGET_ASM_UNWIND_EMIT
442 #define TARGET_ASM_UNWIND_EMIT arm_unwind_emit
443
444 /* EABI unwinding tables use a different format for the typeinfo tables.  */
445 #undef TARGET_ASM_TTYPE
446 #define TARGET_ASM_TTYPE arm_output_ttype
447
448 #undef TARGET_ARM_EABI_UNWINDER
449 #define TARGET_ARM_EABI_UNWINDER true
450 #endif /* TARGET_UNWIND_INFO */
451
452 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
453 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC arm_dwarf_handle_frame_unspec
454
455 #undef TARGET_DWARF_REGISTER_SPAN
456 #define TARGET_DWARF_REGISTER_SPAN arm_dwarf_register_span
457
458 #undef  TARGET_CANNOT_COPY_INSN_P
459 #define TARGET_CANNOT_COPY_INSN_P arm_cannot_copy_insn_p
460
461 #ifdef HAVE_AS_TLS
462 #undef TARGET_HAVE_TLS
463 #define TARGET_HAVE_TLS true
464 #endif
465
466 #undef TARGET_HAVE_CONDITIONAL_EXECUTION
467 #define TARGET_HAVE_CONDITIONAL_EXECUTION arm_have_conditional_execution
468
469 #undef TARGET_CANNOT_FORCE_CONST_MEM
470 #define TARGET_CANNOT_FORCE_CONST_MEM arm_cannot_force_const_mem
471
472 #undef TARGET_MAX_ANCHOR_OFFSET
473 #define TARGET_MAX_ANCHOR_OFFSET 4095
474
475 /* The minimum is set such that the total size of the block
476    for a particular anchor is -4088 + 1 + 4095 bytes, which is
477    divisible by eight, ensuring natural spacing of anchors.  */
478 #undef TARGET_MIN_ANCHOR_OFFSET
479 #define TARGET_MIN_ANCHOR_OFFSET -4088
480
481 #undef TARGET_SCHED_ISSUE_RATE
482 #define TARGET_SCHED_ISSUE_RATE arm_issue_rate
483
484 #undef TARGET_MANGLE_TYPE
485 #define TARGET_MANGLE_TYPE arm_mangle_type
486
487 #undef TARGET_BUILD_BUILTIN_VA_LIST
488 #define TARGET_BUILD_BUILTIN_VA_LIST arm_build_builtin_va_list
489 #undef TARGET_EXPAND_BUILTIN_VA_START
490 #define TARGET_EXPAND_BUILTIN_VA_START arm_expand_builtin_va_start
491 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
492 #define TARGET_GIMPLIFY_VA_ARG_EXPR arm_gimplify_va_arg_expr
493
494 #ifdef HAVE_AS_TLS
495 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
496 #define TARGET_ASM_OUTPUT_DWARF_DTPREL arm_output_dwarf_dtprel
497 #endif
498
499 #undef TARGET_LEGITIMATE_ADDRESS_P
500 #define TARGET_LEGITIMATE_ADDRESS_P     arm_legitimate_address_p
501
502 #undef TARGET_INVALID_PARAMETER_TYPE
503 #define TARGET_INVALID_PARAMETER_TYPE arm_invalid_parameter_type
504
505 #undef TARGET_INVALID_RETURN_TYPE
506 #define TARGET_INVALID_RETURN_TYPE arm_invalid_return_type
507
508 #undef TARGET_PROMOTED_TYPE
509 #define TARGET_PROMOTED_TYPE arm_promoted_type
510
511 #undef TARGET_CONVERT_TO_TYPE
512 #define TARGET_CONVERT_TO_TYPE arm_convert_to_type
513
514 #undef TARGET_SCALAR_MODE_SUPPORTED_P
515 #define TARGET_SCALAR_MODE_SUPPORTED_P arm_scalar_mode_supported_p
516
517 #undef TARGET_FRAME_POINTER_REQUIRED
518 #define TARGET_FRAME_POINTER_REQUIRED arm_frame_pointer_required
519
520 #undef TARGET_CAN_ELIMINATE
521 #define TARGET_CAN_ELIMINATE arm_can_eliminate
522
523 struct gcc_target targetm = TARGET_INITIALIZER;
524 \f
525 /* Obstack for minipool constant handling.  */
526 static struct obstack minipool_obstack;
527 static char *         minipool_startobj;
528
529 /* The maximum number of insns skipped which
530    will be conditionalised if possible.  */
531 static int max_insns_skipped = 5;
532
533 extern FILE * asm_out_file;
534
535 /* True if we are currently building a constant table.  */
536 int making_const_table;
537
538 /* The processor for which instructions should be scheduled.  */
539 enum processor_type arm_tune = arm_none;
540
541 /* The current tuning set.  */
542 const struct tune_params *current_tune;
543
544 /* Which floating point hardware to schedule for.  */
545 int arm_fpu_attr;
546
547 /* Which floating popint hardware to use.  */
548 const struct arm_fpu_desc *arm_fpu_desc;
549
550 /* Whether to use floating point hardware.  */
551 enum float_abi_type arm_float_abi;
552
553 /* Which __fp16 format to use.  */
554 enum arm_fp16_format_type arm_fp16_format;
555
556 /* Which ABI to use.  */
557 enum arm_abi_type arm_abi;
558
559 /* Which thread pointer model to use.  */
560 enum arm_tp_type target_thread_pointer = TP_AUTO;
561
562 /* Used to parse -mstructure_size_boundary command line option.  */
563 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
564
565 /* Used for Thumb call_via trampolines.  */
566 rtx thumb_call_via_label[14];
567 static int thumb_call_reg_needed;
568
569 /* Bit values used to identify processor capabilities.  */
570 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
571 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
572 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
573 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
574 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
575 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
576 #define FL_THUMB      (1 << 6)        /* Thumb aware */
577 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
578 #define FL_STRONG     (1 << 8)        /* StrongARM */
579 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
580 #define FL_XSCALE     (1 << 10)       /* XScale */
581 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
582 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
583                                          media instructions.  */
584 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
585 #define FL_WBUF       (1 << 14)       /* Schedule for write buffer ops.
586                                          Note: ARM6 & 7 derivatives only.  */
587 #define FL_ARCH6K     (1 << 15)       /* Architecture rel 6 K extensions.  */
588 #define FL_THUMB2     (1 << 16)       /* Thumb-2.  */
589 #define FL_NOTM       (1 << 17)       /* Instructions not present in the 'M'
590                                          profile.  */
591 #define FL_DIV        (1 << 18)       /* Hardware divide.  */
592 #define FL_VFPV3      (1 << 19)       /* Vector Floating Point V3.  */
593 #define FL_NEON       (1 << 20)       /* Neon instructions.  */
594 #define FL_ARCH7EM    (1 << 21)       /* Instructions present in the ARMv7E-M
595                                          architecture.  */
596
597 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
598
599 /* Flags that only effect tuning, not available instructions.  */
600 #define FL_TUNE         (FL_WBUF | FL_VFPV2 | FL_STRONG | FL_LDSCHED \
601                          | FL_CO_PROC)
602
603 #define FL_FOR_ARCH2    FL_NOTM
604 #define FL_FOR_ARCH3    (FL_FOR_ARCH2 | FL_MODE32)
605 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
606 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
607 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
608 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
609 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
610 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
611 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
612 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
613 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
614 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
615 #define FL_FOR_ARCH6K   (FL_FOR_ARCH6 | FL_ARCH6K)
616 #define FL_FOR_ARCH6Z   FL_FOR_ARCH6
617 #define FL_FOR_ARCH6ZK  FL_FOR_ARCH6K
618 #define FL_FOR_ARCH6T2  (FL_FOR_ARCH6 | FL_THUMB2)
619 #define FL_FOR_ARCH6M   (FL_FOR_ARCH6 & ~FL_NOTM)
620 #define FL_FOR_ARCH7    (FL_FOR_ARCH6T2 &~ FL_NOTM)
621 #define FL_FOR_ARCH7A   (FL_FOR_ARCH7 | FL_NOTM | FL_ARCH6K)
622 #define FL_FOR_ARCH7R   (FL_FOR_ARCH7A | FL_DIV)
623 #define FL_FOR_ARCH7M   (FL_FOR_ARCH7 | FL_DIV)
624 #define FL_FOR_ARCH7EM  (FL_FOR_ARCH7M | FL_ARCH7EM)
625
626 /* The bits in this mask specify which
627    instructions we are allowed to generate.  */
628 static unsigned long insn_flags = 0;
629
630 /* The bits in this mask specify which instruction scheduling options should
631    be used.  */
632 static unsigned long tune_flags = 0;
633
634 /* The following are used in the arm.md file as equivalents to bits
635    in the above two flag variables.  */
636
637 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
638 int arm_arch3m = 0;
639
640 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
641 int arm_arch4 = 0;
642
643 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
644 int arm_arch4t = 0;
645
646 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
647 int arm_arch5 = 0;
648
649 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
650 int arm_arch5e = 0;
651
652 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
653 int arm_arch6 = 0;
654
655 /* Nonzero if this chip supports the ARM 6K extensions.  */
656 int arm_arch6k = 0;
657
658 /* Nonzero if instructions not present in the 'M' profile can be used.  */
659 int arm_arch_notm = 0;
660
661 /* Nonzero if instructions present in ARMv7E-M can be used.  */
662 int arm_arch7em = 0;
663
664 /* Nonzero if this chip can benefit from load scheduling.  */
665 int arm_ld_sched = 0;
666
667 /* Nonzero if this chip is a StrongARM.  */
668 int arm_tune_strongarm = 0;
669
670 /* Nonzero if this chip is a Cirrus variant.  */
671 int arm_arch_cirrus = 0;
672
673 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
674 int arm_arch_iwmmxt = 0;
675
676 /* Nonzero if this chip is an XScale.  */
677 int arm_arch_xscale = 0;
678
679 /* Nonzero if tuning for XScale  */
680 int arm_tune_xscale = 0;
681
682 /* Nonzero if we want to tune for stores that access the write-buffer.
683    This typically means an ARM6 or ARM7 with MMU or MPU.  */
684 int arm_tune_wbuf = 0;
685
686 /* Nonzero if tuning for Cortex-A9.  */
687 int arm_tune_cortex_a9 = 0;
688
689 /* Nonzero if generating Thumb instructions.  */
690 int thumb_code = 0;
691
692 /* Nonzero if generating Thumb-1 instructions.  */
693 int thumb1_code = 0;
694
695 /* Nonzero if we should define __THUMB_INTERWORK__ in the
696    preprocessor.
697    XXX This is a bit of a hack, it's intended to help work around
698    problems in GLD which doesn't understand that armv5t code is
699    interworking clean.  */
700 int arm_cpp_interwork = 0;
701
702 /* Nonzero if chip supports Thumb 2.  */
703 int arm_arch_thumb2;
704
705 /* Nonzero if chip supports integer division instruction.  */
706 int arm_arch_hwdiv;
707
708 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference,
709    we must report the mode of the memory reference from
710    TARGET_PRINT_OPERAND to TARGET_PRINT_OPERAND_ADDRESS.  */
711 enum machine_mode output_memory_reference_mode;
712
713 /* The register number to be used for the PIC offset register.  */
714 unsigned arm_pic_register = INVALID_REGNUM;
715
716 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
717    the next function.  */
718 static int after_arm_reorg = 0;
719
720 enum arm_pcs arm_pcs_default;
721
722 /* For an explanation of these variables, see final_prescan_insn below.  */
723 int arm_ccfsm_state;
724 /* arm_current_cc is also used for Thumb-2 cond_exec blocks.  */
725 enum arm_cond_code arm_current_cc;
726
727 rtx arm_target_insn;
728 int arm_target_label;
729 /* The number of conditionally executed insns, including the current insn.  */
730 int arm_condexec_count = 0;
731 /* A bitmask specifying the patterns for the IT block.
732    Zero means do not output an IT block before this insn. */
733 int arm_condexec_mask = 0;
734 /* The number of bits used in arm_condexec_mask.  */
735 int arm_condexec_masklen = 0;
736
737 /* The condition codes of the ARM, and the inverse function.  */
738 static const char * const arm_condition_codes[] =
739 {
740   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
741   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
742 };
743
744 /* The register numbers in sequence, for passing to arm_gen_load_multiple.  */
745 int arm_regs_in_sequence[] =
746 {
747   0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
748 };
749
750 #define ARM_LSL_NAME (TARGET_UNIFIED_ASM ? "lsl" : "asl")
751 #define streq(string1, string2) (strcmp (string1, string2) == 0)
752
753 #define THUMB2_WORK_REGS (0xff & ~(  (1 << THUMB_HARD_FRAME_POINTER_REGNUM) \
754                                    | (1 << SP_REGNUM) | (1 << PC_REGNUM) \
755                                    | (1 << PIC_OFFSET_TABLE_REGNUM)))
756 \f
757 /* Initialization code.  */
758
759 struct processors
760 {
761   const char *const name;
762   enum processor_type core;
763   const char *arch;
764   const unsigned long flags;
765   const struct tune_params *const tune;
766 };
767
768 const struct tune_params arm_slowmul_tune =
769 {
770   arm_slowmul_rtx_costs,
771   NULL,
772   3
773 };
774
775 const struct tune_params arm_fastmul_tune =
776 {
777   arm_fastmul_rtx_costs,
778   NULL,
779   1
780 };
781
782 const struct tune_params arm_xscale_tune =
783 {
784   arm_xscale_rtx_costs,
785   xscale_sched_adjust_cost,
786   2
787 };
788
789 const struct tune_params arm_9e_tune =
790 {
791   arm_9e_rtx_costs,
792   NULL,
793   1
794 };
795
796 const struct tune_params arm_cortex_a9_tune =
797 {
798   arm_9e_rtx_costs,
799   cortex_a9_sched_adjust_cost,
800   1
801 };
802
803
804 /* Not all of these give usefully different compilation alternatives,
805    but there is no simple way of generalizing them.  */
806 static const struct processors all_cores[] =
807 {
808   /* ARM Cores */
809 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
810   {NAME, IDENT, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, &arm_##COSTS##_tune},
811 #include "arm-cores.def"
812 #undef ARM_CORE
813   {NULL, arm_none, NULL, 0, NULL}
814 };
815
816 static const struct processors all_architectures[] =
817 {
818   /* ARM Architectures */
819   /* We don't specify tuning costs here as it will be figured out
820      from the core.  */
821
822   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
823   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
824   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
825   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
826   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
827   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
828      implementations that support it, so we will leave it out for now.  */
829   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
830   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
831   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
832   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
833   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
834   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
835   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
836   {"armv6k",  mpcore,     "6K",  FL_CO_PROC |             FL_FOR_ARCH6K, NULL},
837   {"armv6z",  arm1176jzs, "6Z",  FL_CO_PROC |             FL_FOR_ARCH6Z, NULL},
838   {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC |             FL_FOR_ARCH6ZK, NULL},
839   {"armv6t2", arm1156t2s, "6T2", FL_CO_PROC |             FL_FOR_ARCH6T2, NULL},
840   {"armv6-m", cortexm1,   "6M",                           FL_FOR_ARCH6M, NULL},
841   {"armv7",   cortexa8,   "7",   FL_CO_PROC |             FL_FOR_ARCH7, NULL},
842   {"armv7-a", cortexa8,   "7A",  FL_CO_PROC |             FL_FOR_ARCH7A, NULL},
843   {"armv7-r", cortexr4,   "7R",  FL_CO_PROC |             FL_FOR_ARCH7R, NULL},
844   {"armv7-m", cortexm3,   "7M",  FL_CO_PROC |             FL_FOR_ARCH7M, NULL},
845   {"armv7e-m", cortexm4,  "7EM", FL_CO_PROC |             FL_FOR_ARCH7EM, NULL},
846   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
847   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
848   {"iwmmxt2", iwmmxt2,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
849   {NULL, arm_none, NULL, 0 , NULL}
850 };
851
852
853 /* These are populated as commandline arguments are processed, or NULL
854    if not specified.  */
855 static const struct processors *arm_selected_arch;
856 static const struct processors *arm_selected_cpu;
857 static const struct processors *arm_selected_tune;
858
859 /* The name of the preprocessor macro to define for this architecture.  */
860
861 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
862
863 /* Available values for -mfpu=.  */
864
865 static const struct arm_fpu_desc all_fpus[] =
866 {
867   {"fpa",               ARM_FP_MODEL_FPA, 0, VFP_NONE, false, false},
868   {"fpe2",              ARM_FP_MODEL_FPA, 2, VFP_NONE, false, false},
869   {"fpe3",              ARM_FP_MODEL_FPA, 3, VFP_NONE, false, false},
870   {"maverick",          ARM_FP_MODEL_MAVERICK, 0, VFP_NONE, false, false},
871   {"vfp",               ARM_FP_MODEL_VFP, 2, VFP_REG_D16, false, false},
872   {"vfpv3",             ARM_FP_MODEL_VFP, 3, VFP_REG_D32, false, false},
873   {"vfpv3-fp16",        ARM_FP_MODEL_VFP, 3, VFP_REG_D32, false, true},
874   {"vfpv3-d16",         ARM_FP_MODEL_VFP, 3, VFP_REG_D16, false, false},
875   {"vfpv3-d16-fp16",    ARM_FP_MODEL_VFP, 3, VFP_REG_D16, false, true},
876   {"vfpv3xd",           ARM_FP_MODEL_VFP, 3, VFP_REG_SINGLE, false, false},
877   {"vfpv3xd-fp16",      ARM_FP_MODEL_VFP, 3, VFP_REG_SINGLE, false, true},
878   {"neon",              ARM_FP_MODEL_VFP, 3, VFP_REG_D32, true , false},
879   {"neon-fp16",         ARM_FP_MODEL_VFP, 3, VFP_REG_D32, true , true },
880   {"vfpv4",             ARM_FP_MODEL_VFP, 4, VFP_REG_D32, false, true},
881   {"vfpv4-d16",         ARM_FP_MODEL_VFP, 4, VFP_REG_D16, false, true},
882   {"fpv4-sp-d16",       ARM_FP_MODEL_VFP, 4, VFP_REG_SINGLE, false, true},
883   {"neon-vfpv4",        ARM_FP_MODEL_VFP, 4, VFP_REG_D32, true, true},
884   /* Compatibility aliases.  */
885   {"vfp3",              ARM_FP_MODEL_VFP, 3, VFP_REG_D32, false, false},
886 };
887
888
889 struct float_abi
890 {
891   const char * name;
892   enum float_abi_type abi_type;
893 };
894
895
896 /* Available values for -mfloat-abi=.  */
897
898 static const struct float_abi all_float_abis[] =
899 {
900   {"soft",      ARM_FLOAT_ABI_SOFT},
901   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
902   {"hard",      ARM_FLOAT_ABI_HARD}
903 };
904
905
906 struct fp16_format
907 {
908   const char *name;
909   enum arm_fp16_format_type fp16_format_type;
910 };
911
912
913 /* Available values for -mfp16-format=.  */
914
915 static const struct fp16_format all_fp16_formats[] =
916 {
917   {"none",              ARM_FP16_FORMAT_NONE},
918   {"ieee",              ARM_FP16_FORMAT_IEEE},
919   {"alternative",       ARM_FP16_FORMAT_ALTERNATIVE}
920 };
921
922
923 struct abi_name
924 {
925   const char *name;
926   enum arm_abi_type abi_type;
927 };
928
929
930 /* Available values for -mabi=.  */
931
932 static const struct abi_name arm_all_abis[] =
933 {
934   {"apcs-gnu",    ARM_ABI_APCS},
935   {"atpcs",   ARM_ABI_ATPCS},
936   {"aapcs",   ARM_ABI_AAPCS},
937   {"iwmmxt",  ARM_ABI_IWMMXT},
938   {"aapcs-linux",   ARM_ABI_AAPCS_LINUX}
939 };
940
941 /* Supported TLS relocations.  */
942
943 enum tls_reloc {
944   TLS_GD32,
945   TLS_LDM32,
946   TLS_LDO32,
947   TLS_IE32,
948   TLS_LE32
949 };
950
951 /* The maximum number of insns to be used when loading a constant.  */
952 inline static int
953 arm_constant_limit (bool size_p)
954 {
955   return size_p ? 1 : current_tune->constant_limit;
956 }
957
958 /* Emit an insn that's a simple single-set.  Both the operands must be known
959    to be valid.  */
960 inline static rtx
961 emit_set_insn (rtx x, rtx y)
962 {
963   return emit_insn (gen_rtx_SET (VOIDmode, x, y));
964 }
965
966 /* Return the number of bits set in VALUE.  */
967 static unsigned
968 bit_count (unsigned long value)
969 {
970   unsigned long count = 0;
971
972   while (value)
973     {
974       count++;
975       value &= value - 1;  /* Clear the least-significant set bit.  */
976     }
977
978   return count;
979 }
980
981 /* Set up library functions unique to ARM.  */
982
983 static void
984 arm_init_libfuncs (void)
985 {
986   /* There are no special library functions unless we are using the
987      ARM BPABI.  */
988   if (!TARGET_BPABI)
989     return;
990
991   /* The functions below are described in Section 4 of the "Run-Time
992      ABI for the ARM architecture", Version 1.0.  */
993
994   /* Double-precision floating-point arithmetic.  Table 2.  */
995   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
996   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
997   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
998   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
999   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
1000
1001   /* Double-precision comparisons.  Table 3.  */
1002   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
1003   set_optab_libfunc (ne_optab, DFmode, NULL);
1004   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
1005   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
1006   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
1007   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
1008   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
1009
1010   /* Single-precision floating-point arithmetic.  Table 4.  */
1011   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
1012   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
1013   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
1014   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
1015   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
1016
1017   /* Single-precision comparisons.  Table 5.  */
1018   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
1019   set_optab_libfunc (ne_optab, SFmode, NULL);
1020   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
1021   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
1022   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
1023   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
1024   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
1025
1026   /* Floating-point to integer conversions.  Table 6.  */
1027   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
1028   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
1029   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
1030   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
1031   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
1032   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
1033   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
1034   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
1035
1036   /* Conversions between floating types.  Table 7.  */
1037   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
1038   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
1039
1040   /* Integer to floating-point conversions.  Table 8.  */
1041   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
1042   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
1043   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
1044   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
1045   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
1046   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
1047   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
1048   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
1049
1050   /* Long long.  Table 9.  */
1051   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
1052   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
1053   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
1054   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
1055   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
1056   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
1057   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
1058   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
1059
1060   /* Integer (32/32->32) division.  \S 4.3.1.  */
1061   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
1062   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
1063
1064   /* The divmod functions are designed so that they can be used for
1065      plain division, even though they return both the quotient and the
1066      remainder.  The quotient is returned in the usual location (i.e.,
1067      r0 for SImode, {r0, r1} for DImode), just as would be expected
1068      for an ordinary division routine.  Because the AAPCS calling
1069      conventions specify that all of { r0, r1, r2, r3 } are
1070      callee-saved registers, there is no need to tell the compiler
1071      explicitly that those registers are clobbered by these
1072      routines.  */
1073   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
1074   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
1075
1076   /* For SImode division the ABI provides div-without-mod routines,
1077      which are faster.  */
1078   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
1079   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
1080
1081   /* We don't have mod libcalls.  Fortunately gcc knows how to use the
1082      divmod libcalls instead.  */
1083   set_optab_libfunc (smod_optab, DImode, NULL);
1084   set_optab_libfunc (umod_optab, DImode, NULL);
1085   set_optab_libfunc (smod_optab, SImode, NULL);
1086   set_optab_libfunc (umod_optab, SImode, NULL);
1087
1088   /* Half-precision float operations.  The compiler handles all operations
1089      with NULL libfuncs by converting the SFmode.  */
1090   switch (arm_fp16_format)
1091     {
1092     case ARM_FP16_FORMAT_IEEE:
1093     case ARM_FP16_FORMAT_ALTERNATIVE:
1094
1095       /* Conversions.  */
1096       set_conv_libfunc (trunc_optab, HFmode, SFmode,
1097                         (arm_fp16_format == ARM_FP16_FORMAT_IEEE
1098                          ? "__gnu_f2h_ieee"
1099                          : "__gnu_f2h_alternative"));
1100       set_conv_libfunc (sext_optab, SFmode, HFmode, 
1101                         (arm_fp16_format == ARM_FP16_FORMAT_IEEE
1102                          ? "__gnu_h2f_ieee"
1103                          : "__gnu_h2f_alternative"));
1104       
1105       /* Arithmetic.  */
1106       set_optab_libfunc (add_optab, HFmode, NULL);
1107       set_optab_libfunc (sdiv_optab, HFmode, NULL);
1108       set_optab_libfunc (smul_optab, HFmode, NULL);
1109       set_optab_libfunc (neg_optab, HFmode, NULL);
1110       set_optab_libfunc (sub_optab, HFmode, NULL);
1111
1112       /* Comparisons.  */
1113       set_optab_libfunc (eq_optab, HFmode, NULL);
1114       set_optab_libfunc (ne_optab, HFmode, NULL);
1115       set_optab_libfunc (lt_optab, HFmode, NULL);
1116       set_optab_libfunc (le_optab, HFmode, NULL);
1117       set_optab_libfunc (ge_optab, HFmode, NULL);
1118       set_optab_libfunc (gt_optab, HFmode, NULL);
1119       set_optab_libfunc (unord_optab, HFmode, NULL);
1120       break;
1121
1122     default:
1123       break;
1124     }
1125
1126   if (TARGET_AAPCS_BASED)
1127     synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
1128 }
1129
1130 /* On AAPCS systems, this is the "struct __va_list".  */
1131 static GTY(()) tree va_list_type;
1132
1133 /* Return the type to use as __builtin_va_list.  */
1134 static tree
1135 arm_build_builtin_va_list (void)
1136 {
1137   tree va_list_name;
1138   tree ap_field;
1139   
1140   if (!TARGET_AAPCS_BASED)
1141     return std_build_builtin_va_list ();
1142
1143   /* AAPCS \S 7.1.4 requires that va_list be a typedef for a type
1144      defined as:
1145
1146        struct __va_list 
1147        {
1148          void *__ap;
1149        };
1150
1151      The C Library ABI further reinforces this definition in \S
1152      4.1.
1153
1154      We must follow this definition exactly.  The structure tag
1155      name is visible in C++ mangled names, and thus forms a part
1156      of the ABI.  The field name may be used by people who
1157      #include <stdarg.h>.  */
1158   /* Create the type.  */
1159   va_list_type = lang_hooks.types.make_type (RECORD_TYPE);
1160   /* Give it the required name.  */
1161   va_list_name = build_decl (BUILTINS_LOCATION,
1162                              TYPE_DECL,
1163                              get_identifier ("__va_list"),
1164                              va_list_type);
1165   DECL_ARTIFICIAL (va_list_name) = 1;
1166   TYPE_NAME (va_list_type) = va_list_name;
1167   /* Create the __ap field.  */
1168   ap_field = build_decl (BUILTINS_LOCATION,
1169                          FIELD_DECL, 
1170                          get_identifier ("__ap"),
1171                          ptr_type_node);
1172   DECL_ARTIFICIAL (ap_field) = 1;
1173   DECL_FIELD_CONTEXT (ap_field) = va_list_type;
1174   TYPE_FIELDS (va_list_type) = ap_field;
1175   /* Compute its layout.  */
1176   layout_type (va_list_type);
1177
1178   return va_list_type;
1179 }
1180
1181 /* Return an expression of type "void *" pointing to the next
1182    available argument in a variable-argument list.  VALIST is the
1183    user-level va_list object, of type __builtin_va_list.  */
1184 static tree
1185 arm_extract_valist_ptr (tree valist)
1186 {
1187   if (TREE_TYPE (valist) == error_mark_node)
1188     return error_mark_node;
1189
1190   /* On an AAPCS target, the pointer is stored within "struct
1191      va_list".  */
1192   if (TARGET_AAPCS_BASED)
1193     {
1194       tree ap_field = TYPE_FIELDS (TREE_TYPE (valist));
1195       valist = build3 (COMPONENT_REF, TREE_TYPE (ap_field), 
1196                        valist, ap_field, NULL_TREE);
1197     }
1198
1199   return valist;
1200 }
1201
1202 /* Implement TARGET_EXPAND_BUILTIN_VA_START.  */
1203 static void
1204 arm_expand_builtin_va_start (tree valist, rtx nextarg)
1205 {
1206   valist = arm_extract_valist_ptr (valist);
1207   std_expand_builtin_va_start (valist, nextarg);
1208 }
1209
1210 /* Implement TARGET_GIMPLIFY_VA_ARG_EXPR.  */
1211 static tree
1212 arm_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p, 
1213                           gimple_seq *post_p)
1214 {
1215   valist = arm_extract_valist_ptr (valist);
1216   return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
1217 }
1218
1219 /* Lookup NAME in SEL.  */
1220
1221 static const struct processors *
1222 arm_find_cpu (const char *name, const struct processors *sel, const char *desc)
1223 {
1224   if (!(name && *name))
1225     return NULL;
1226
1227   for (; sel->name != NULL; sel++)
1228     {
1229       if (streq (name, sel->name))
1230         return sel;
1231     }
1232
1233   error ("bad value (%s) for %s switch", name, desc);
1234   return NULL;
1235 }
1236
1237 /* Implement TARGET_HANDLE_OPTION.  */
1238
1239 static bool
1240 arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
1241 {
1242   switch (code)
1243     {
1244     case OPT_march_:
1245       arm_selected_arch = arm_find_cpu(arg, all_architectures, "-march");
1246       return true;
1247
1248     case OPT_mcpu_:
1249       arm_selected_cpu = arm_find_cpu(arg, all_cores, "-mcpu");
1250       return true;
1251
1252     case OPT_mhard_float:
1253       target_float_abi_name = "hard";
1254       return true;
1255
1256     case OPT_msoft_float:
1257       target_float_abi_name = "soft";
1258       return true;
1259
1260     case OPT_mtune_:
1261       arm_selected_tune = arm_find_cpu(arg, all_cores, "-mtune");
1262       return true;
1263
1264     default:
1265       return true;
1266     }
1267 }
1268
1269 static void
1270 arm_target_help (void)
1271 {
1272   int i;
1273   static int columns = 0;
1274   int remaining;
1275
1276   /* If we have not done so already, obtain the desired maximum width of
1277      the output.  Note - this is a duplication of the code at the start of
1278      gcc/opts.c:print_specific_help() - the two copies should probably be
1279      replaced by a single function.  */
1280   if (columns == 0)
1281     {
1282       const char *p;
1283
1284       GET_ENVIRONMENT (p, "COLUMNS");
1285       if (p != NULL)
1286         {
1287           int value = atoi (p);
1288
1289           if (value > 0)
1290             columns = value;
1291         }
1292
1293       if (columns == 0)
1294         /* Use a reasonable default.  */
1295         columns = 80;
1296     }
1297
1298   printf ("  Known ARM CPUs (for use with the -mcpu= and -mtune= options):\n");
1299
1300   /* The - 2 is because we know that the last entry in the array is NULL.  */
1301   i = ARRAY_SIZE (all_cores) - 2;
1302   gcc_assert (i > 0);
1303   printf ("    %s", all_cores[i].name);
1304   remaining = columns - (strlen (all_cores[i].name) + 4);
1305   gcc_assert (remaining >= 0);
1306
1307   while (i--)
1308     {
1309       int len = strlen (all_cores[i].name);
1310
1311       if (remaining > len + 2)
1312         {
1313           printf (", %s", all_cores[i].name);
1314           remaining -= len + 2;
1315         }
1316       else
1317         {
1318           if (remaining > 0)
1319             printf (",");
1320           printf ("\n    %s", all_cores[i].name);
1321           remaining = columns - (len + 4);
1322         }
1323     }
1324
1325   printf ("\n\n  Known ARM architectures (for use with the -march= option):\n");
1326
1327   i = ARRAY_SIZE (all_architectures) - 2;
1328   gcc_assert (i > 0);
1329   
1330   printf ("    %s", all_architectures[i].name);
1331   remaining = columns - (strlen (all_architectures[i].name) + 4);
1332   gcc_assert (remaining >= 0);
1333
1334   while (i--)
1335     {
1336       int len = strlen (all_architectures[i].name);
1337
1338       if (remaining > len + 2)
1339         {
1340           printf (", %s", all_architectures[i].name);
1341           remaining -= len + 2;
1342         }
1343       else
1344         {
1345           if (remaining > 0)
1346             printf (",");
1347           printf ("\n    %s", all_architectures[i].name);
1348           remaining = columns - (len + 4);
1349         }
1350     }
1351   printf ("\n");
1352
1353 }
1354
1355 /* Fix up any incompatible options that the user has specified.
1356    This has now turned into a maze.  */
1357 void
1358 arm_override_options (void)
1359 {
1360   unsigned i;
1361
1362   if (arm_selected_arch)
1363     {
1364       if (arm_selected_cpu)
1365         {
1366           /* Check for conflict between mcpu and march.  */
1367           if ((arm_selected_cpu->flags ^ arm_selected_arch->flags) & ~FL_TUNE)
1368             {
1369               warning (0, "switch -mcpu=%s conflicts with -march=%s switch",
1370                        arm_selected_cpu->name, arm_selected_arch->name);
1371               /* -march wins for code generation.
1372                  -mcpu wins for default tuning.  */
1373               if (!arm_selected_tune)
1374                 arm_selected_tune = arm_selected_cpu;
1375
1376               arm_selected_cpu = arm_selected_arch;
1377             }
1378           else
1379             /* -mcpu wins.  */
1380             arm_selected_arch = NULL;
1381         }
1382       else
1383         /* Pick a CPU based on the architecture.  */
1384         arm_selected_cpu = arm_selected_arch;
1385     }
1386
1387   /* If the user did not specify a processor, choose one for them.  */
1388   if (!arm_selected_cpu)
1389     {
1390       const struct processors * sel;
1391       unsigned int        sought;
1392
1393       arm_selected_cpu = &all_cores[TARGET_CPU_DEFAULT];
1394       if (!arm_selected_cpu->name)
1395         {
1396 #ifdef SUBTARGET_CPU_DEFAULT
1397           /* Use the subtarget default CPU if none was specified by
1398              configure.  */
1399           arm_selected_cpu = &all_cores[SUBTARGET_CPU_DEFAULT];
1400 #endif
1401           /* Default to ARM6.  */
1402           if (arm_selected_cpu->name)
1403             arm_selected_cpu = &all_cores[arm6];
1404         }
1405
1406       sel = arm_selected_cpu;
1407       insn_flags = sel->flags;
1408
1409       /* Now check to see if the user has specified some command line
1410          switch that require certain abilities from the cpu.  */
1411       sought = 0;
1412
1413       if (TARGET_INTERWORK || TARGET_THUMB)
1414         {
1415           sought |= (FL_THUMB | FL_MODE32);
1416
1417           /* There are no ARM processors that support both APCS-26 and
1418              interworking.  Therefore we force FL_MODE26 to be removed
1419              from insn_flags here (if it was set), so that the search
1420              below will always be able to find a compatible processor.  */
1421           insn_flags &= ~FL_MODE26;
1422         }
1423
1424       if (sought != 0 && ((sought & insn_flags) != sought))
1425         {
1426           /* Try to locate a CPU type that supports all of the abilities
1427              of the default CPU, plus the extra abilities requested by
1428              the user.  */
1429           for (sel = all_cores; sel->name != NULL; sel++)
1430             if ((sel->flags & sought) == (sought | insn_flags))
1431               break;
1432
1433           if (sel->name == NULL)
1434             {
1435               unsigned current_bit_count = 0;
1436               const struct processors * best_fit = NULL;
1437
1438               /* Ideally we would like to issue an error message here
1439                  saying that it was not possible to find a CPU compatible
1440                  with the default CPU, but which also supports the command
1441                  line options specified by the programmer, and so they
1442                  ought to use the -mcpu=<name> command line option to
1443                  override the default CPU type.
1444
1445                  If we cannot find a cpu that has both the
1446                  characteristics of the default cpu and the given
1447                  command line options we scan the array again looking
1448                  for a best match.  */
1449               for (sel = all_cores; sel->name != NULL; sel++)
1450                 if ((sel->flags & sought) == sought)
1451                   {
1452                     unsigned count;
1453
1454                     count = bit_count (sel->flags & insn_flags);
1455
1456                     if (count >= current_bit_count)
1457                       {
1458                         best_fit = sel;
1459                         current_bit_count = count;
1460                       }
1461                   }
1462
1463               gcc_assert (best_fit);
1464               sel = best_fit;
1465             }
1466
1467           arm_selected_cpu = sel;
1468         }
1469     }
1470
1471   gcc_assert (arm_selected_cpu);
1472   /* The selected cpu may be an architecture, so lookup tuning by core ID.  */
1473   if (!arm_selected_tune)
1474     arm_selected_tune = &all_cores[arm_selected_cpu->core];
1475
1476   sprintf (arm_arch_name, "__ARM_ARCH_%s__", arm_selected_cpu->arch);
1477   insn_flags = arm_selected_cpu->flags;
1478
1479   arm_tune = arm_selected_tune->core;
1480   tune_flags = arm_selected_tune->flags;
1481   current_tune = arm_selected_tune->tune;
1482
1483   if (target_fp16_format_name)
1484     {
1485       for (i = 0; i < ARRAY_SIZE (all_fp16_formats); i++)
1486         {
1487           if (streq (all_fp16_formats[i].name, target_fp16_format_name))
1488             {
1489               arm_fp16_format = all_fp16_formats[i].fp16_format_type;
1490               break;
1491             }
1492         }
1493       if (i == ARRAY_SIZE (all_fp16_formats))
1494         error ("invalid __fp16 format option: -mfp16-format=%s",
1495                target_fp16_format_name);
1496     }
1497   else
1498     arm_fp16_format = ARM_FP16_FORMAT_NONE;
1499
1500   if (target_abi_name)
1501     {
1502       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1503         {
1504           if (streq (arm_all_abis[i].name, target_abi_name))
1505             {
1506               arm_abi = arm_all_abis[i].abi_type;
1507               break;
1508             }
1509         }
1510       if (i == ARRAY_SIZE (arm_all_abis))
1511         error ("invalid ABI option: -mabi=%s", target_abi_name);
1512     }
1513   else
1514     arm_abi = ARM_DEFAULT_ABI;
1515
1516   /* Make sure that the processor choice does not conflict with any of the
1517      other command line choices.  */
1518   if (TARGET_ARM && !(insn_flags & FL_NOTM))
1519     error ("target CPU does not support ARM mode");
1520
1521   /* BPABI targets use linker tricks to allow interworking on cores
1522      without thumb support.  */
1523   if (TARGET_INTERWORK && !((insn_flags & FL_THUMB) || TARGET_BPABI))
1524     {
1525       warning (0, "target CPU does not support interworking" );
1526       target_flags &= ~MASK_INTERWORK;
1527     }
1528
1529   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1530     {
1531       warning (0, "target CPU does not support THUMB instructions");
1532       target_flags &= ~MASK_THUMB;
1533     }
1534
1535   if (TARGET_APCS_FRAME && TARGET_THUMB)
1536     {
1537       /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1538       target_flags &= ~MASK_APCS_FRAME;
1539     }
1540
1541   /* Callee super interworking implies thumb interworking.  Adding
1542      this to the flags here simplifies the logic elsewhere.  */
1543   if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
1544       target_flags |= MASK_INTERWORK;
1545
1546   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1547      from here where no function is being compiled currently.  */
1548   if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1549     warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1550
1551   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1552     warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1553
1554   if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
1555     warning (0, "enabling caller interworking support is only meaningful when compiling for the Thumb");
1556
1557   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1558     {
1559       warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1560       target_flags |= MASK_APCS_FRAME;
1561     }
1562
1563   if (TARGET_POKE_FUNCTION_NAME)
1564     target_flags |= MASK_APCS_FRAME;
1565
1566   if (TARGET_APCS_REENT && flag_pic)
1567     error ("-fpic and -mapcs-reent are incompatible");
1568
1569   if (TARGET_APCS_REENT)
1570     warning (0, "APCS reentrant code not supported.  Ignored");
1571
1572   /* If this target is normally configured to use APCS frames, warn if they
1573      are turned off and debugging is turned on.  */
1574   if (TARGET_ARM
1575       && write_symbols != NO_DEBUG
1576       && !TARGET_APCS_FRAME
1577       && (TARGET_DEFAULT & MASK_APCS_FRAME))
1578     warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1579
1580   if (TARGET_APCS_FLOAT)
1581     warning (0, "passing floating point arguments in fp regs not yet supported");
1582
1583   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
1584   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1585   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1586   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1587   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1588   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1589   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1590   arm_arch6k = (insn_flags & FL_ARCH6K) != 0;
1591   arm_arch_notm = (insn_flags & FL_NOTM) != 0;
1592   arm_arch7em = (insn_flags & FL_ARCH7EM) != 0;
1593   arm_arch_thumb2 = (insn_flags & FL_THUMB2) != 0;
1594   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1595   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1596
1597   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1598   arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1599   thumb_code = TARGET_ARM == 0;
1600   thumb1_code = TARGET_THUMB1 != 0;
1601   arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1602   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1603   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1604   arm_arch_hwdiv = (insn_flags & FL_DIV) != 0;
1605   arm_tune_cortex_a9 = (arm_tune == cortexa9) != 0;
1606
1607   /* If we are not using the default (ARM mode) section anchor offset
1608      ranges, then set the correct ranges now.  */
1609   if (TARGET_THUMB1)
1610     {
1611       /* Thumb-1 LDR instructions cannot have negative offsets.
1612          Permissible positive offset ranges are 5-bit (for byte loads),
1613          6-bit (for halfword loads), or 7-bit (for word loads).
1614          Empirical results suggest a 7-bit anchor range gives the best
1615          overall code size.  */
1616       targetm.min_anchor_offset = 0;
1617       targetm.max_anchor_offset = 127;
1618     }
1619   else if (TARGET_THUMB2)
1620     {
1621       /* The minimum is set such that the total size of the block
1622          for a particular anchor is 248 + 1 + 4095 bytes, which is
1623          divisible by eight, ensuring natural spacing of anchors.  */
1624       targetm.min_anchor_offset = -248;
1625       targetm.max_anchor_offset = 4095;
1626     }
1627
1628   /* V5 code we generate is completely interworking capable, so we turn off
1629      TARGET_INTERWORK here to avoid many tests later on.  */
1630
1631   /* XXX However, we must pass the right pre-processor defines to CPP
1632      or GLD can get confused.  This is a hack.  */
1633   if (TARGET_INTERWORK)
1634     arm_cpp_interwork = 1;
1635
1636   if (arm_arch5)
1637     target_flags &= ~MASK_INTERWORK;
1638
1639   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1640     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1641
1642   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1643     error ("iwmmxt abi requires an iwmmxt capable cpu");
1644
1645   if (target_fpu_name == NULL && target_fpe_name != NULL)
1646     {
1647       if (streq (target_fpe_name, "2"))
1648         target_fpu_name = "fpe2";
1649       else if (streq (target_fpe_name, "3"))
1650         target_fpu_name = "fpe3";
1651       else
1652         error ("invalid floating point emulation option: -mfpe=%s",
1653                target_fpe_name);
1654     }
1655
1656   if (target_fpu_name == NULL)
1657     {
1658 #ifdef FPUTYPE_DEFAULT
1659       target_fpu_name = FPUTYPE_DEFAULT;
1660 #else
1661       if (arm_arch_cirrus)
1662         target_fpu_name = "maverick";
1663       else
1664         target_fpu_name = "fpe2";
1665 #endif
1666     }
1667
1668   arm_fpu_desc = NULL;
1669   for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1670     {
1671       if (streq (all_fpus[i].name, target_fpu_name))
1672         {
1673           arm_fpu_desc = &all_fpus[i];
1674           break;
1675         }
1676     }
1677
1678   if (!arm_fpu_desc)
1679     {
1680       error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1681       return;
1682     }
1683
1684   switch (arm_fpu_desc->model)
1685     {
1686     case ARM_FP_MODEL_FPA:
1687       if (arm_fpu_desc->rev == 2)
1688         arm_fpu_attr = FPU_FPE2;
1689       else if (arm_fpu_desc->rev == 3)
1690         arm_fpu_attr = FPU_FPE3;
1691       else
1692         arm_fpu_attr = FPU_FPA;
1693       break;
1694
1695     case ARM_FP_MODEL_MAVERICK:
1696       arm_fpu_attr = FPU_MAVERICK;
1697       break;
1698
1699     case ARM_FP_MODEL_VFP:
1700       arm_fpu_attr = FPU_VFP;
1701       break;
1702
1703     default:
1704       gcc_unreachable();
1705     }
1706
1707   if (target_float_abi_name != NULL)
1708     {
1709       /* The user specified a FP ABI.  */
1710       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1711         {
1712           if (streq (all_float_abis[i].name, target_float_abi_name))
1713             {
1714               arm_float_abi = all_float_abis[i].abi_type;
1715               break;
1716             }
1717         }
1718       if (i == ARRAY_SIZE (all_float_abis))
1719         error ("invalid floating point abi: -mfloat-abi=%s",
1720                target_float_abi_name);
1721     }
1722   else
1723     arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1724
1725   if (TARGET_AAPCS_BASED
1726       && (arm_fpu_desc->model == ARM_FP_MODEL_FPA))
1727     error ("FPA is unsupported in the AAPCS");
1728
1729   if (TARGET_AAPCS_BASED)
1730     {
1731       if (TARGET_CALLER_INTERWORKING)
1732         error ("AAPCS does not support -mcaller-super-interworking");
1733       else
1734         if (TARGET_CALLEE_INTERWORKING)
1735           error ("AAPCS does not support -mcallee-super-interworking");
1736     }
1737
1738   /* FPA and iWMMXt are incompatible because the insn encodings overlap.
1739      VFP and iWMMXt can theoretically coexist, but it's unlikely such silicon
1740      will ever exist.  GCC makes no attempt to support this combination.  */
1741   if (TARGET_IWMMXT && !TARGET_SOFT_FLOAT)
1742     sorry ("iWMMXt and hardware floating point");
1743
1744   /* ??? iWMMXt insn patterns need auditing for Thumb-2.  */
1745   if (TARGET_THUMB2 && TARGET_IWMMXT)
1746     sorry ("Thumb-2 iWMMXt");
1747
1748   /* __fp16 support currently assumes the core has ldrh.  */
1749   if (!arm_arch4 && arm_fp16_format != ARM_FP16_FORMAT_NONE)
1750     sorry ("__fp16 and no ldrh");
1751
1752   /* If soft-float is specified then don't use FPU.  */
1753   if (TARGET_SOFT_FLOAT)
1754     arm_fpu_attr = FPU_NONE;
1755
1756   if (TARGET_AAPCS_BASED)
1757     {
1758       if (arm_abi == ARM_ABI_IWMMXT)
1759         arm_pcs_default = ARM_PCS_AAPCS_IWMMXT;
1760       else if (arm_float_abi == ARM_FLOAT_ABI_HARD
1761                && TARGET_HARD_FLOAT
1762                && TARGET_VFP)
1763         arm_pcs_default = ARM_PCS_AAPCS_VFP;
1764       else
1765         arm_pcs_default = ARM_PCS_AAPCS;
1766     }
1767   else
1768     {
1769       if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1770         sorry ("-mfloat-abi=hard and VFP");
1771
1772       if (arm_abi == ARM_ABI_APCS)
1773         arm_pcs_default = ARM_PCS_APCS;
1774       else
1775         arm_pcs_default = ARM_PCS_ATPCS;
1776     }
1777
1778   /* For arm2/3 there is no need to do any scheduling if there is only
1779      a floating point emulator, or we are doing software floating-point.  */
1780   if ((TARGET_SOFT_FLOAT
1781        || (TARGET_FPA && arm_fpu_desc->rev))
1782       && (tune_flags & FL_MODE32) == 0)
1783     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1784
1785   if (target_thread_switch)
1786     {
1787       if (strcmp (target_thread_switch, "soft") == 0)
1788         target_thread_pointer = TP_SOFT;
1789       else if (strcmp (target_thread_switch, "auto") == 0)
1790         target_thread_pointer = TP_AUTO;
1791       else if (strcmp (target_thread_switch, "cp15") == 0)
1792         target_thread_pointer = TP_CP15;
1793       else
1794         error ("invalid thread pointer option: -mtp=%s", target_thread_switch);
1795     }
1796
1797   /* Use the cp15 method if it is available.  */
1798   if (target_thread_pointer == TP_AUTO)
1799     {
1800       if (arm_arch6k && !TARGET_THUMB1)
1801         target_thread_pointer = TP_CP15;
1802       else
1803         target_thread_pointer = TP_SOFT;
1804     }
1805
1806   if (TARGET_HARD_TP && TARGET_THUMB1)
1807     error ("can not use -mtp=cp15 with 16-bit Thumb");
1808
1809   /* Override the default structure alignment for AAPCS ABI.  */
1810   if (TARGET_AAPCS_BASED)
1811     arm_structure_size_boundary = 8;
1812
1813   if (structure_size_string != NULL)
1814     {
1815       int size = strtol (structure_size_string, NULL, 0);
1816
1817       if (size == 8 || size == 32
1818           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1819         arm_structure_size_boundary = size;
1820       else
1821         warning (0, "structure size boundary can only be set to %s",
1822                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1823     }
1824
1825   if (!TARGET_ARM && TARGET_VXWORKS_RTP && flag_pic)
1826     {
1827       error ("RTP PIC is incompatible with Thumb");
1828       flag_pic = 0;
1829     }
1830
1831   /* If stack checking is disabled, we can use r10 as the PIC register,
1832      which keeps r9 available.  The EABI specifies r9 as the PIC register.  */
1833   if (flag_pic && TARGET_SINGLE_PIC_BASE)
1834     {
1835       if (TARGET_VXWORKS_RTP)
1836         warning (0, "RTP PIC is incompatible with -msingle-pic-base");
1837       arm_pic_register = (TARGET_APCS_STACK || TARGET_AAPCS_BASED) ? 9 : 10;
1838     }
1839
1840   if (flag_pic && TARGET_VXWORKS_RTP)
1841     arm_pic_register = 9;
1842
1843   if (arm_pic_register_string != NULL)
1844     {
1845       int pic_register = decode_reg_name (arm_pic_register_string);
1846
1847       if (!flag_pic)
1848         warning (0, "-mpic-register= is useless without -fpic");
1849
1850       /* Prevent the user from choosing an obviously stupid PIC register.  */
1851       else if (pic_register < 0 || call_used_regs[pic_register]
1852                || pic_register == HARD_FRAME_POINTER_REGNUM
1853                || pic_register == STACK_POINTER_REGNUM
1854                || pic_register >= PC_REGNUM
1855                || (TARGET_VXWORKS_RTP
1856                    && (unsigned int) pic_register != arm_pic_register))
1857         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1858       else
1859         arm_pic_register = pic_register;
1860     }
1861
1862   /* Enable -mfix-cortex-m3-ldrd by default for Cortex-M3 cores.  */
1863   if (fix_cm3_ldrd == 2)
1864     {
1865       if (arm_selected_cpu->core == cortexm3)
1866         fix_cm3_ldrd = 1;
1867       else
1868         fix_cm3_ldrd = 0;
1869     }
1870
1871   if (TARGET_THUMB1 && flag_schedule_insns)
1872     {
1873       /* Don't warn since it's on by default in -O2.  */
1874       flag_schedule_insns = 0;
1875     }
1876
1877   if (optimize_size)
1878     {
1879       /* If optimizing for size, bump the number of instructions that we
1880          are prepared to conditionally execute (even on a StrongARM).  */
1881       max_insns_skipped = 6;
1882     }
1883   else
1884     {
1885       /* StrongARM has early execution of branches, so a sequence
1886          that is worth skipping is shorter.  */
1887       if (arm_tune_strongarm)
1888         max_insns_skipped = 3;
1889     }
1890
1891   /* Hot/Cold partitioning is not currently supported, since we can't
1892      handle literal pool placement in that case.  */
1893   if (flag_reorder_blocks_and_partition)
1894     {
1895       inform (input_location,
1896               "-freorder-blocks-and-partition not supported on this architecture");
1897       flag_reorder_blocks_and_partition = 0;
1898       flag_reorder_blocks = 1;
1899     }
1900
1901   if (!PARAM_SET_P (PARAM_GCSE_UNRESTRICTED_COST)
1902       && flag_pic)
1903     /* Hoisting PIC address calculations more aggressively provides a small,
1904        but measurable, size reduction for PIC code.  Therefore, we decrease
1905        the bar for unrestricted expression hoisting to the cost of PIC address
1906        calculation, which is 2 instructions.  */
1907     set_param_value ("gcse-unrestricted-cost", 2);
1908
1909   /* Register global variables with the garbage collector.  */
1910   arm_add_gc_roots ();
1911 }
1912
1913 static void
1914 arm_add_gc_roots (void)
1915 {
1916   gcc_obstack_init(&minipool_obstack);
1917   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1918 }
1919 \f
1920 /* A table of known ARM exception types.
1921    For use with the interrupt function attribute.  */
1922
1923 typedef struct
1924 {
1925   const char *const arg;
1926   const unsigned long return_value;
1927 }
1928 isr_attribute_arg;
1929
1930 static const isr_attribute_arg isr_attribute_args [] =
1931 {
1932   { "IRQ",   ARM_FT_ISR },
1933   { "irq",   ARM_FT_ISR },
1934   { "FIQ",   ARM_FT_FIQ },
1935   { "fiq",   ARM_FT_FIQ },
1936   { "ABORT", ARM_FT_ISR },
1937   { "abort", ARM_FT_ISR },
1938   { "ABORT", ARM_FT_ISR },
1939   { "abort", ARM_FT_ISR },
1940   { "UNDEF", ARM_FT_EXCEPTION },
1941   { "undef", ARM_FT_EXCEPTION },
1942   { "SWI",   ARM_FT_EXCEPTION },
1943   { "swi",   ARM_FT_EXCEPTION },
1944   { NULL,    ARM_FT_NORMAL }
1945 };
1946
1947 /* Returns the (interrupt) function type of the current
1948    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1949
1950 static unsigned long
1951 arm_isr_value (tree argument)
1952 {
1953   const isr_attribute_arg * ptr;
1954   const char *              arg;
1955
1956   if (!arm_arch_notm)
1957     return ARM_FT_NORMAL | ARM_FT_STACKALIGN;
1958
1959   /* No argument - default to IRQ.  */
1960   if (argument == NULL_TREE)
1961     return ARM_FT_ISR;
1962
1963   /* Get the value of the argument.  */
1964   if (TREE_VALUE (argument) == NULL_TREE
1965       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1966     return ARM_FT_UNKNOWN;
1967
1968   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1969
1970   /* Check it against the list of known arguments.  */
1971   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1972     if (streq (arg, ptr->arg))
1973       return ptr->return_value;
1974
1975   /* An unrecognized interrupt type.  */
1976   return ARM_FT_UNKNOWN;
1977 }
1978
1979 /* Computes the type of the current function.  */
1980
1981 static unsigned long
1982 arm_compute_func_type (void)
1983 {
1984   unsigned long type = ARM_FT_UNKNOWN;
1985   tree a;
1986   tree attr;
1987
1988   gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
1989
1990   /* Decide if the current function is volatile.  Such functions
1991      never return, and many memory cycles can be saved by not storing
1992      register values that will never be needed again.  This optimization
1993      was added to speed up context switching in a kernel application.  */
1994   if (optimize > 0
1995       && (TREE_NOTHROW (current_function_decl)
1996           || !(flag_unwind_tables
1997                || (flag_exceptions && !USING_SJLJ_EXCEPTIONS)))
1998       && TREE_THIS_VOLATILE (current_function_decl))
1999     type |= ARM_FT_VOLATILE;
2000
2001   if (cfun->static_chain_decl != NULL)
2002     type |= ARM_FT_NESTED;
2003
2004   attr = DECL_ATTRIBUTES (current_function_decl);
2005
2006   a = lookup_attribute ("naked", attr);
2007   if (a != NULL_TREE)
2008     type |= ARM_FT_NAKED;
2009
2010   a = lookup_attribute ("isr", attr);
2011   if (a == NULL_TREE)
2012     a = lookup_attribute ("interrupt", attr);
2013
2014   if (a == NULL_TREE)
2015     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
2016   else
2017     type |= arm_isr_value (TREE_VALUE (a));
2018
2019   return type;
2020 }
2021
2022 /* Returns the type of the current function.  */
2023
2024 unsigned long
2025 arm_current_func_type (void)
2026 {
2027   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
2028     cfun->machine->func_type = arm_compute_func_type ();
2029
2030   return cfun->machine->func_type;
2031 }
2032
2033 bool
2034 arm_allocate_stack_slots_for_args (void)
2035 {
2036   /* Naked functions should not allocate stack slots for arguments.  */
2037   return !IS_NAKED (arm_current_func_type ());
2038 }
2039
2040 \f
2041 /* Output assembler code for a block containing the constant parts
2042    of a trampoline, leaving space for the variable parts.
2043
2044    On the ARM, (if r8 is the static chain regnum, and remembering that
2045    referencing pc adds an offset of 8) the trampoline looks like:
2046            ldr          r8, [pc, #0]
2047            ldr          pc, [pc]
2048            .word        static chain value
2049            .word        function's address
2050    XXX FIXME: When the trampoline returns, r8 will be clobbered.  */
2051
2052 static void
2053 arm_asm_trampoline_template (FILE *f)
2054 {
2055   if (TARGET_ARM)
2056     {
2057       asm_fprintf (f, "\tldr\t%r, [%r, #0]\n", STATIC_CHAIN_REGNUM, PC_REGNUM);
2058       asm_fprintf (f, "\tldr\t%r, [%r, #0]\n", PC_REGNUM, PC_REGNUM);
2059     }
2060   else if (TARGET_THUMB2)
2061     {
2062       /* The Thumb-2 trampoline is similar to the arm implementation.
2063          Unlike 16-bit Thumb, we enter the stub in thumb mode.  */
2064       asm_fprintf (f, "\tldr.w\t%r, [%r, #4]\n",
2065                    STATIC_CHAIN_REGNUM, PC_REGNUM);
2066       asm_fprintf (f, "\tldr.w\t%r, [%r, #4]\n", PC_REGNUM, PC_REGNUM);
2067     }
2068   else
2069     {
2070       ASM_OUTPUT_ALIGN (f, 2);
2071       fprintf (f, "\t.code\t16\n");
2072       fprintf (f, ".Ltrampoline_start:\n");
2073       asm_fprintf (f, "\tpush\t{r0, r1}\n");
2074       asm_fprintf (f, "\tldr\tr0, [%r, #8]\n", PC_REGNUM);
2075       asm_fprintf (f, "\tmov\t%r, r0\n", STATIC_CHAIN_REGNUM);
2076       asm_fprintf (f, "\tldr\tr0, [%r, #8]\n", PC_REGNUM);
2077       asm_fprintf (f, "\tstr\tr0, [%r, #4]\n", SP_REGNUM);
2078       asm_fprintf (f, "\tpop\t{r0, %r}\n", PC_REGNUM);
2079     }
2080   assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2081   assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2082 }
2083
2084 /* Emit RTL insns to initialize the variable parts of a trampoline.  */
2085
2086 static void
2087 arm_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
2088 {
2089   rtx fnaddr, mem, a_tramp;
2090
2091   emit_block_move (m_tramp, assemble_trampoline_template (),
2092                    GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
2093
2094   mem = adjust_address (m_tramp, SImode, TARGET_32BIT ? 8 : 12);
2095   emit_move_insn (mem, chain_value);
2096
2097   mem = adjust_address (m_tramp, SImode, TARGET_32BIT ? 12 : 16);
2098   fnaddr = XEXP (DECL_RTL (fndecl), 0);
2099   emit_move_insn (mem, fnaddr);
2100
2101   a_tramp = XEXP (m_tramp, 0);
2102   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__clear_cache"),
2103                      LCT_NORMAL, VOIDmode, 2, a_tramp, Pmode,
2104                      plus_constant (a_tramp, TRAMPOLINE_SIZE), Pmode);
2105 }
2106
2107 /* Thumb trampolines should be entered in thumb mode, so set
2108    the bottom bit of the address.  */
2109
2110 static rtx
2111 arm_trampoline_adjust_address (rtx addr)
2112 {
2113   if (TARGET_THUMB)
2114     addr = expand_simple_binop (Pmode, IOR, addr, const1_rtx,
2115                                 NULL, 0, OPTAB_LIB_WIDEN);
2116   return addr;
2117 }
2118 \f
2119 /* Return 1 if it is possible to return using a single instruction.
2120    If SIBLING is non-null, this is a test for a return before a sibling
2121    call.  SIBLING is the call insn, so we can examine its register usage.  */
2122
2123 int
2124 use_return_insn (int iscond, rtx sibling)
2125 {
2126   int regno;
2127   unsigned int func_type;
2128   unsigned long saved_int_regs;
2129   unsigned HOST_WIDE_INT stack_adjust;
2130   arm_stack_offsets *offsets;
2131
2132   /* Never use a return instruction before reload has run.  */
2133   if (!reload_completed)
2134     return 0;
2135
2136   func_type = arm_current_func_type ();
2137
2138   /* Naked, volatile and stack alignment functions need special
2139      consideration.  */
2140   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED | ARM_FT_STACKALIGN))
2141     return 0;
2142
2143   /* So do interrupt functions that use the frame pointer and Thumb
2144      interrupt functions.  */
2145   if (IS_INTERRUPT (func_type) && (frame_pointer_needed || TARGET_THUMB))
2146     return 0;
2147
2148   offsets = arm_get_frame_offsets ();
2149   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
2150
2151   /* As do variadic functions.  */
2152   if (crtl->args.pretend_args_size
2153       || cfun->machine->uses_anonymous_args
2154       /* Or if the function calls __builtin_eh_return () */
2155       || crtl->calls_eh_return
2156       /* Or if the function calls alloca */
2157       || cfun->calls_alloca
2158       /* Or if there is a stack adjustment.  However, if the stack pointer
2159          is saved on the stack, we can use a pre-incrementing stack load.  */
2160       || !(stack_adjust == 0 || (TARGET_APCS_FRAME && frame_pointer_needed
2161                                  && stack_adjust == 4)))
2162     return 0;
2163
2164   saved_int_regs = offsets->saved_regs_mask;
2165
2166   /* Unfortunately, the insn
2167
2168        ldmib sp, {..., sp, ...}
2169
2170      triggers a bug on most SA-110 based devices, such that the stack
2171      pointer won't be correctly restored if the instruction takes a
2172      page fault.  We work around this problem by popping r3 along with
2173      the other registers, since that is never slower than executing
2174      another instruction.
2175
2176      We test for !arm_arch5 here, because code for any architecture
2177      less than this could potentially be run on one of the buggy
2178      chips.  */
2179   if (stack_adjust == 4 && !arm_arch5 && TARGET_ARM)
2180     {
2181       /* Validate that r3 is a call-clobbered register (always true in
2182          the default abi) ...  */
2183       if (!call_used_regs[3])
2184         return 0;
2185
2186       /* ... that it isn't being used for a return value ... */
2187       if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
2188         return 0;
2189
2190       /* ... or for a tail-call argument ...  */
2191       if (sibling)
2192         {
2193           gcc_assert (GET_CODE (sibling) == CALL_INSN);
2194
2195           if (find_regno_fusage (sibling, USE, 3))
2196             return 0;
2197         }
2198
2199       /* ... and that there are no call-saved registers in r0-r2
2200          (always true in the default ABI).  */
2201       if (saved_int_regs & 0x7)
2202         return 0;
2203     }
2204
2205   /* Can't be done if interworking with Thumb, and any registers have been
2206      stacked.  */
2207   if (TARGET_INTERWORK && saved_int_regs != 0 && !IS_INTERRUPT(func_type))
2208     return 0;
2209
2210   /* On StrongARM, conditional returns are expensive if they aren't
2211      taken and multiple registers have been stacked.  */
2212   if (iscond && arm_tune_strongarm)
2213     {
2214       /* Conditional return when just the LR is stored is a simple
2215          conditional-load instruction, that's not expensive.  */
2216       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
2217         return 0;
2218
2219       if (flag_pic 
2220           && arm_pic_register != INVALID_REGNUM
2221           && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
2222         return 0;
2223     }
2224
2225   /* If there are saved registers but the LR isn't saved, then we need
2226      two instructions for the return.  */
2227   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
2228     return 0;
2229
2230   /* Can't be done if any of the FPA regs are pushed,
2231      since this also requires an insn.  */
2232   if (TARGET_HARD_FLOAT && TARGET_FPA)
2233     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
2234       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
2235         return 0;
2236
2237   /* Likewise VFP regs.  */
2238   if (TARGET_HARD_FLOAT && TARGET_VFP)
2239     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
2240       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
2241         return 0;
2242
2243   if (TARGET_REALLY_IWMMXT)
2244     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
2245       if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
2246         return 0;
2247
2248   return 1;
2249 }
2250
2251 /* Return TRUE if int I is a valid immediate ARM constant.  */
2252
2253 int
2254 const_ok_for_arm (HOST_WIDE_INT i)
2255 {
2256   int lowbit;
2257
2258   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
2259      be all zero, or all one.  */
2260   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
2261       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
2262           != ((~(unsigned HOST_WIDE_INT) 0)
2263               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
2264     return FALSE;
2265
2266   i &= (unsigned HOST_WIDE_INT) 0xffffffff;
2267
2268   /* Fast return for 0 and small values.  We must do this for zero, since
2269      the code below can't handle that one case.  */
2270   if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
2271     return TRUE;
2272
2273   /* Get the number of trailing zeros.  */
2274   lowbit = ffs((int) i) - 1;
2275   
2276   /* Only even shifts are allowed in ARM mode so round down to the
2277      nearest even number.  */
2278   if (TARGET_ARM)
2279     lowbit &= ~1;
2280
2281   if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
2282     return TRUE;
2283
2284   if (TARGET_ARM)
2285     {
2286       /* Allow rotated constants in ARM mode.  */
2287       if (lowbit <= 4
2288            && ((i & ~0xc000003f) == 0
2289                || (i & ~0xf000000f) == 0
2290                || (i & ~0xfc000003) == 0))
2291         return TRUE;
2292     }
2293   else
2294     {
2295       HOST_WIDE_INT v;
2296
2297       /* Allow repeated pattern.  */
2298       v = i & 0xff;
2299       v |= v << 16;
2300       if (i == v || i == (v | (v << 8)))
2301         return TRUE;
2302     }
2303
2304   return FALSE;
2305 }
2306
2307 /* Return true if I is a valid constant for the operation CODE.  */
2308 static int
2309 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
2310 {
2311   if (const_ok_for_arm (i))
2312     return 1;
2313
2314   switch (code)
2315     {
2316     case PLUS:
2317     case COMPARE:
2318     case EQ:
2319     case NE:
2320     case GT:
2321     case LE:
2322     case LT:
2323     case GE:
2324     case GEU:
2325     case LTU:
2326     case GTU:
2327     case LEU:
2328     case UNORDERED:
2329     case ORDERED:
2330     case UNEQ:
2331     case UNGE:
2332     case UNLT:
2333     case UNGT:
2334     case UNLE:
2335       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
2336
2337     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
2338     case XOR:
2339       return 0;
2340
2341     case IOR:
2342       if (TARGET_THUMB2)
2343         return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2344       return 0;
2345
2346     case AND:
2347       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2348
2349     default:
2350       gcc_unreachable ();
2351     }
2352 }
2353
2354 /* Emit a sequence of insns to handle a large constant.
2355    CODE is the code of the operation required, it can be any of SET, PLUS,
2356    IOR, AND, XOR, MINUS;
2357    MODE is the mode in which the operation is being performed;
2358    VAL is the integer to operate on;
2359    SOURCE is the other operand (a register, or a null-pointer for SET);
2360    SUBTARGETS means it is safe to create scratch registers if that will
2361    either produce a simpler sequence, or we will want to cse the values.
2362    Return value is the number of insns emitted.  */
2363
2364 /* ??? Tweak this for thumb2.  */
2365 int
2366 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
2367                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
2368 {
2369   rtx cond;
2370
2371   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
2372     cond = COND_EXEC_TEST (PATTERN (insn));
2373   else
2374     cond = NULL_RTX;
2375
2376   if (subtargets || code == SET
2377       || (GET_CODE (target) == REG && GET_CODE (source) == REG
2378           && REGNO (target) != REGNO (source)))
2379     {
2380       /* After arm_reorg has been called, we can't fix up expensive
2381          constants by pushing them into memory so we must synthesize
2382          them in-line, regardless of the cost.  This is only likely to
2383          be more costly on chips that have load delay slots and we are
2384          compiling without running the scheduler (so no splitting
2385          occurred before the final instruction emission).
2386
2387          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
2388       */
2389       if (!after_arm_reorg
2390           && !cond
2391           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
2392                                 1, 0)
2393               > (arm_constant_limit (optimize_function_for_size_p (cfun))
2394                  + (code != SET))))
2395         {
2396           if (code == SET)
2397             {
2398               /* Currently SET is the only monadic value for CODE, all
2399                  the rest are diadic.  */
2400               if (TARGET_USE_MOVT)
2401                 arm_emit_movpair (target, GEN_INT (val));
2402               else
2403                 emit_set_insn (target, GEN_INT (val));
2404
2405               return 1;
2406             }
2407           else
2408             {
2409               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
2410
2411               if (TARGET_USE_MOVT)
2412                 arm_emit_movpair (temp, GEN_INT (val));
2413               else
2414                 emit_set_insn (temp, GEN_INT (val));
2415
2416               /* For MINUS, the value is subtracted from, since we never
2417                  have subtraction of a constant.  */
2418               if (code == MINUS)
2419                 emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
2420               else
2421                 emit_set_insn (target,
2422                                gen_rtx_fmt_ee (code, mode, source, temp));
2423               return 2;
2424             }
2425         }
2426     }
2427
2428   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
2429                            1);
2430 }
2431
2432 /* Return the number of instructions required to synthesize the given
2433    constant, if we start emitting them from bit-position I.  */
2434 static int
2435 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
2436 {
2437   HOST_WIDE_INT temp1;
2438   int step_size = TARGET_ARM ? 2 : 1;
2439   int num_insns = 0;
2440
2441   gcc_assert (TARGET_ARM || i == 0);
2442
2443   do
2444     {
2445       int end;
2446
2447       if (i <= 0)
2448         i += 32;
2449       if (remainder & (((1 << step_size) - 1) << (i - step_size)))
2450         {
2451           end = i - 8;
2452           if (end < 0)
2453             end += 32;
2454           temp1 = remainder & ((0x0ff << end)
2455                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
2456           remainder &= ~temp1;
2457           num_insns++;
2458           i -= 8 - step_size;
2459         }
2460       i -= step_size;
2461     } while (remainder);
2462   return num_insns;
2463 }
2464
2465 static int
2466 find_best_start (unsigned HOST_WIDE_INT remainder)
2467 {
2468   int best_consecutive_zeros = 0;
2469   int i;
2470   int best_start = 0;
2471
2472   /* If we aren't targetting ARM, the best place to start is always at
2473      the bottom.  */
2474   if (! TARGET_ARM)
2475     return 0;
2476
2477   for (i = 0; i < 32; i += 2)
2478     {
2479       int consecutive_zeros = 0;
2480
2481       if (!(remainder & (3 << i)))
2482         {
2483           while ((i < 32) && !(remainder & (3 << i)))
2484             {
2485               consecutive_zeros += 2;
2486               i += 2;
2487             }
2488           if (consecutive_zeros > best_consecutive_zeros)
2489             {
2490               best_consecutive_zeros = consecutive_zeros;
2491               best_start = i - consecutive_zeros;
2492             }
2493           i -= 2;
2494         }
2495     }
2496
2497   /* So long as it won't require any more insns to do so, it's
2498      desirable to emit a small constant (in bits 0...9) in the last
2499      insn.  This way there is more chance that it can be combined with
2500      a later addressing insn to form a pre-indexed load or store
2501      operation.  Consider:
2502
2503            *((volatile int *)0xe0000100) = 1;
2504            *((volatile int *)0xe0000110) = 2;
2505
2506      We want this to wind up as:
2507
2508             mov rA, #0xe0000000
2509             mov rB, #1
2510             str rB, [rA, #0x100]
2511             mov rB, #2
2512             str rB, [rA, #0x110]
2513
2514      rather than having to synthesize both large constants from scratch.
2515
2516      Therefore, we calculate how many insns would be required to emit
2517      the constant starting from `best_start', and also starting from
2518      zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2519      yield a shorter sequence, we may as well use zero.  */
2520   if (best_start != 0
2521       && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2522       && (count_insns_for_constant (remainder, 0) <=
2523           count_insns_for_constant (remainder, best_start)))
2524     best_start = 0;
2525
2526   return best_start;
2527 }
2528
2529 /* Emit an instruction with the indicated PATTERN.  If COND is
2530    non-NULL, conditionalize the execution of the instruction on COND
2531    being true.  */
2532
2533 static void
2534 emit_constant_insn (rtx cond, rtx pattern)
2535 {
2536   if (cond)
2537     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
2538   emit_insn (pattern);
2539 }
2540
2541 /* As above, but extra parameter GENERATE which, if clear, suppresses
2542    RTL generation.  */
2543 /* ??? This needs more work for thumb2.  */
2544
2545 static int
2546 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
2547                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
2548                   int generate)
2549 {
2550   int can_invert = 0;
2551   int can_negate = 0;
2552   int final_invert = 0;
2553   int can_negate_initial = 0;
2554   int i;
2555   int num_bits_set = 0;
2556   int set_sign_bit_copies = 0;
2557   int clear_sign_bit_copies = 0;
2558   int clear_zero_bit_copies = 0;
2559   int set_zero_bit_copies = 0;
2560   int insns = 0;
2561   unsigned HOST_WIDE_INT temp1, temp2;
2562   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
2563   int step_size = TARGET_ARM ? 2 : 1;
2564
2565   /* Find out which operations are safe for a given CODE.  Also do a quick
2566      check for degenerate cases; these can occur when DImode operations
2567      are split.  */
2568   switch (code)
2569     {
2570     case SET:
2571       can_invert = 1;
2572       can_negate = 1;
2573       break;
2574
2575     case PLUS:
2576       can_negate = 1;
2577       can_negate_initial = 1;
2578       break;
2579
2580     case IOR:
2581       if (remainder == 0xffffffff)
2582         {
2583           if (generate)
2584             emit_constant_insn (cond,
2585                                 gen_rtx_SET (VOIDmode, target,
2586                                              GEN_INT (ARM_SIGN_EXTEND (val))));
2587           return 1;
2588         }
2589
2590       if (remainder == 0)
2591         {
2592           if (reload_completed && rtx_equal_p (target, source))
2593             return 0;
2594
2595           if (generate)
2596             emit_constant_insn (cond,
2597                                 gen_rtx_SET (VOIDmode, target, source));
2598           return 1;
2599         }
2600
2601       if (TARGET_THUMB2)
2602         can_invert = 1;
2603       break;
2604
2605     case AND:
2606       if (remainder == 0)
2607         {
2608           if (generate)
2609             emit_constant_insn (cond,
2610                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
2611           return 1;
2612         }
2613       if (remainder == 0xffffffff)
2614         {
2615           if (reload_completed && rtx_equal_p (target, source))
2616             return 0;
2617           if (generate)
2618             emit_constant_insn (cond,
2619                                 gen_rtx_SET (VOIDmode, target, source));
2620           return 1;
2621         }
2622       can_invert = 1;
2623       break;
2624
2625     case XOR:
2626       if (remainder == 0)
2627         {
2628           if (reload_completed && rtx_equal_p (target, source))
2629             return 0;
2630           if (generate)
2631             emit_constant_insn (cond,
2632                                 gen_rtx_SET (VOIDmode, target, source));
2633           return 1;
2634         }
2635
2636       if (remainder == 0xffffffff)
2637         {
2638           if (generate)
2639             emit_constant_insn (cond,
2640                                 gen_rtx_SET (VOIDmode, target,
2641                                              gen_rtx_NOT (mode, source)));
2642           return 1;
2643         }
2644       break;
2645
2646     case MINUS:
2647       /* We treat MINUS as (val - source), since (source - val) is always
2648          passed as (source + (-val)).  */
2649       if (remainder == 0)
2650         {
2651           if (generate)
2652             emit_constant_insn (cond,
2653                                 gen_rtx_SET (VOIDmode, target,
2654                                              gen_rtx_NEG (mode, source)));
2655           return 1;
2656         }
2657       if (const_ok_for_arm (val))
2658         {
2659           if (generate)
2660             emit_constant_insn (cond,
2661                                 gen_rtx_SET (VOIDmode, target,
2662                                              gen_rtx_MINUS (mode, GEN_INT (val),
2663                                                             source)));
2664           return 1;
2665         }
2666       can_negate = 1;
2667
2668       break;
2669
2670     default:
2671       gcc_unreachable ();
2672     }
2673
2674   /* If we can do it in one insn get out quickly.  */
2675   if (const_ok_for_arm (val)
2676       || (can_negate_initial && const_ok_for_arm (-val))
2677       || (can_invert && const_ok_for_arm (~val)))
2678     {
2679       if (generate)
2680         emit_constant_insn (cond,
2681                             gen_rtx_SET (VOIDmode, target,
2682                                          (source
2683                                           ? gen_rtx_fmt_ee (code, mode, source,
2684                                                             GEN_INT (val))
2685                                           : GEN_INT (val))));
2686       return 1;
2687     }
2688
2689   /* Calculate a few attributes that may be useful for specific
2690      optimizations.  */
2691   /* Count number of leading zeros.  */
2692   for (i = 31; i >= 0; i--)
2693     {
2694       if ((remainder & (1 << i)) == 0)
2695         clear_sign_bit_copies++;
2696       else
2697         break;
2698     }
2699
2700   /* Count number of leading 1's.  */
2701   for (i = 31; i >= 0; i--)
2702     {
2703       if ((remainder & (1 << i)) != 0)
2704         set_sign_bit_copies++;
2705       else
2706         break;
2707     }
2708
2709   /* Count number of trailing zero's.  */
2710   for (i = 0; i <= 31; i++)
2711     {
2712       if ((remainder & (1 << i)) == 0)
2713         clear_zero_bit_copies++;
2714       else
2715         break;
2716     }
2717
2718   /* Count number of trailing 1's.  */
2719   for (i = 0; i <= 31; i++)
2720     {
2721       if ((remainder & (1 << i)) != 0)
2722         set_zero_bit_copies++;
2723       else
2724         break;
2725     }
2726
2727   switch (code)
2728     {
2729     case SET:
2730       /* See if we can use movw.  */
2731       if (arm_arch_thumb2 && (remainder & 0xffff0000) == 0)
2732         {
2733           if (generate)
2734             emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
2735                                                    GEN_INT (val)));
2736           return 1;
2737         }
2738
2739       /* See if we can do this by sign_extending a constant that is known
2740          to be negative.  This is a good, way of doing it, since the shift
2741          may well merge into a subsequent insn.  */
2742       if (set_sign_bit_copies > 1)
2743         {
2744           if (const_ok_for_arm
2745               (temp1 = ARM_SIGN_EXTEND (remainder
2746                                         << (set_sign_bit_copies - 1))))
2747             {
2748               if (generate)
2749                 {
2750                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2751                   emit_constant_insn (cond,
2752                                       gen_rtx_SET (VOIDmode, new_src,
2753                                                    GEN_INT (temp1)));
2754                   emit_constant_insn (cond,
2755                                       gen_ashrsi3 (target, new_src,
2756                                                    GEN_INT (set_sign_bit_copies - 1)));
2757                 }
2758               return 2;
2759             }
2760           /* For an inverted constant, we will need to set the low bits,
2761              these will be shifted out of harm's way.  */
2762           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
2763           if (const_ok_for_arm (~temp1))
2764             {
2765               if (generate)
2766                 {
2767                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2768                   emit_constant_insn (cond,
2769                                       gen_rtx_SET (VOIDmode, new_src,
2770                                                    GEN_INT (temp1)));
2771                   emit_constant_insn (cond,
2772                                       gen_ashrsi3 (target, new_src,
2773                                                    GEN_INT (set_sign_bit_copies - 1)));
2774                 }
2775               return 2;
2776             }
2777         }
2778
2779       /* See if we can calculate the value as the difference between two
2780          valid immediates.  */
2781       if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
2782         {
2783           int topshift = clear_sign_bit_copies & ~1;
2784
2785           temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
2786                                    & (0xff000000 >> topshift));
2787
2788           /* If temp1 is zero, then that means the 9 most significant
2789              bits of remainder were 1 and we've caused it to overflow.
2790              When topshift is 0 we don't need to do anything since we
2791              can borrow from 'bit 32'.  */
2792           if (temp1 == 0 && topshift != 0)
2793             temp1 = 0x80000000 >> (topshift - 1);
2794
2795           temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
2796
2797           if (const_ok_for_arm (temp2))
2798             {
2799               if (generate)
2800                 {
2801                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2802                   emit_constant_insn (cond,
2803                                       gen_rtx_SET (VOIDmode, new_src,
2804                                                    GEN_INT (temp1)));
2805                   emit_constant_insn (cond,
2806                                       gen_addsi3 (target, new_src,
2807                                                   GEN_INT (-temp2)));
2808                 }
2809
2810               return 2;
2811             }
2812         }
2813
2814       /* See if we can generate this by setting the bottom (or the top)
2815          16 bits, and then shifting these into the other half of the
2816          word.  We only look for the simplest cases, to do more would cost
2817          too much.  Be careful, however, not to generate this when the
2818          alternative would take fewer insns.  */
2819       if (val & 0xffff0000)
2820         {
2821           temp1 = remainder & 0xffff0000;
2822           temp2 = remainder & 0x0000ffff;
2823
2824           /* Overlaps outside this range are best done using other methods.  */
2825           for (i = 9; i < 24; i++)
2826             {
2827               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
2828                   && !const_ok_for_arm (temp2))
2829                 {
2830                   rtx new_src = (subtargets
2831                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2832                                  : target);
2833                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2834                                             source, subtargets, generate);
2835                   source = new_src;
2836                   if (generate)
2837                     emit_constant_insn
2838                       (cond,
2839                        gen_rtx_SET
2840                        (VOIDmode, target,
2841                         gen_rtx_IOR (mode,
2842                                      gen_rtx_ASHIFT (mode, source,
2843                                                      GEN_INT (i)),
2844                                      source)));
2845                   return insns + 1;
2846                 }
2847             }
2848
2849           /* Don't duplicate cases already considered.  */
2850           for (i = 17; i < 24; i++)
2851             {
2852               if (((temp1 | (temp1 >> i)) == remainder)
2853                   && !const_ok_for_arm (temp1))
2854                 {
2855                   rtx new_src = (subtargets
2856                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2857                                  : target);
2858                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2859                                             source, subtargets, generate);
2860                   source = new_src;
2861                   if (generate)
2862                     emit_constant_insn
2863                       (cond,
2864                        gen_rtx_SET (VOIDmode, target,
2865                                     gen_rtx_IOR
2866                                     (mode,
2867                                      gen_rtx_LSHIFTRT (mode, source,
2868                                                        GEN_INT (i)),
2869                                      source)));
2870                   return insns + 1;
2871                 }
2872             }
2873         }
2874       break;
2875
2876     case IOR:
2877     case XOR:
2878       /* If we have IOR or XOR, and the constant can be loaded in a
2879          single instruction, and we can find a temporary to put it in,
2880          then this can be done in two instructions instead of 3-4.  */
2881       if (subtargets
2882           /* TARGET can't be NULL if SUBTARGETS is 0 */
2883           || (reload_completed && !reg_mentioned_p (target, source)))
2884         {
2885           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
2886             {
2887               if (generate)
2888                 {
2889                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2890
2891                   emit_constant_insn (cond,
2892                                       gen_rtx_SET (VOIDmode, sub,
2893                                                    GEN_INT (val)));
2894                   emit_constant_insn (cond,
2895                                       gen_rtx_SET (VOIDmode, target,
2896                                                    gen_rtx_fmt_ee (code, mode,
2897                                                                    source, sub)));
2898                 }
2899               return 2;
2900             }
2901         }
2902
2903       if (code == XOR)
2904         break;
2905
2906       /*  Convert.
2907           x = y | constant ( which is composed of set_sign_bit_copies of leading 1s
2908                              and the remainder 0s for e.g. 0xfff00000)
2909           x = ~(~(y ashift set_sign_bit_copies) lshiftrt set_sign_bit_copies)
2910
2911           This can be done in 2 instructions by using shifts with mov or mvn.
2912           e.g. for
2913           x = x | 0xfff00000;
2914           we generate.
2915           mvn   r0, r0, asl #12
2916           mvn   r0, r0, lsr #12  */
2917       if (set_sign_bit_copies > 8
2918           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
2919         {
2920           if (generate)
2921             {
2922               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2923               rtx shift = GEN_INT (set_sign_bit_copies);
2924
2925               emit_constant_insn
2926                 (cond,
2927                  gen_rtx_SET (VOIDmode, sub,
2928                               gen_rtx_NOT (mode,
2929                                            gen_rtx_ASHIFT (mode,
2930                                                            source,
2931                                                            shift))));
2932               emit_constant_insn
2933                 (cond,
2934                  gen_rtx_SET (VOIDmode, target,
2935                               gen_rtx_NOT (mode,
2936                                            gen_rtx_LSHIFTRT (mode, sub,
2937                                                              shift))));
2938             }
2939           return 2;
2940         }
2941
2942       /* Convert
2943           x = y | constant (which has set_zero_bit_copies number of trailing ones).
2944            to
2945           x = ~((~y lshiftrt set_zero_bit_copies) ashift set_zero_bit_copies).
2946
2947           For eg. r0 = r0 | 0xfff
2948                mvn      r0, r0, lsr #12
2949                mvn      r0, r0, asl #12
2950
2951       */
2952       if (set_zero_bit_copies > 8
2953           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2954         {
2955           if (generate)
2956             {
2957               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2958               rtx shift = GEN_INT (set_zero_bit_copies);
2959
2960               emit_constant_insn
2961                 (cond,
2962                  gen_rtx_SET (VOIDmode, sub,
2963                               gen_rtx_NOT (mode,
2964                                            gen_rtx_LSHIFTRT (mode,
2965                                                              source,
2966                                                              shift))));
2967               emit_constant_insn
2968                 (cond,
2969                  gen_rtx_SET (VOIDmode, target,
2970                               gen_rtx_NOT (mode,
2971                                            gen_rtx_ASHIFT (mode, sub,
2972                                                            shift))));
2973             }
2974           return 2;
2975         }
2976
2977       /* This will never be reached for Thumb2 because orn is a valid
2978          instruction. This is for Thumb1 and the ARM 32 bit cases.
2979
2980          x = y | constant (such that ~constant is a valid constant)
2981          Transform this to
2982          x = ~(~y & ~constant).
2983       */
2984       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
2985         {
2986           if (generate)
2987             {
2988               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2989               emit_constant_insn (cond,
2990                                   gen_rtx_SET (VOIDmode, sub,
2991                                                gen_rtx_NOT (mode, source)));
2992               source = sub;
2993               if (subtargets)
2994                 sub = gen_reg_rtx (mode);
2995               emit_constant_insn (cond,
2996                                   gen_rtx_SET (VOIDmode, sub,
2997                                                gen_rtx_AND (mode, source,
2998                                                             GEN_INT (temp1))));
2999               emit_constant_insn (cond,
3000                                   gen_rtx_SET (VOIDmode, target,
3001                                                gen_rtx_NOT (mode, sub)));
3002             }
3003           return 3;
3004         }
3005       break;
3006
3007     case AND:
3008       /* See if two shifts will do 2 or more insn's worth of work.  */
3009       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
3010         {
3011           HOST_WIDE_INT shift_mask = ((0xffffffff
3012                                        << (32 - clear_sign_bit_copies))
3013                                       & 0xffffffff);
3014
3015           if ((remainder | shift_mask) != 0xffffffff)
3016             {
3017               if (generate)
3018                 {
3019                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3020                   insns = arm_gen_constant (AND, mode, cond,
3021                                             remainder | shift_mask,
3022                                             new_src, source, subtargets, 1);
3023                   source = new_src;
3024                 }
3025               else
3026                 {
3027                   rtx targ = subtargets ? NULL_RTX : target;
3028                   insns = arm_gen_constant (AND, mode, cond,
3029                                             remainder | shift_mask,
3030                                             targ, source, subtargets, 0);
3031                 }
3032             }
3033
3034           if (generate)
3035             {
3036               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3037               rtx shift = GEN_INT (clear_sign_bit_copies);
3038
3039               emit_insn (gen_ashlsi3 (new_src, source, shift));
3040               emit_insn (gen_lshrsi3 (target, new_src, shift));
3041             }
3042
3043           return insns + 2;
3044         }
3045
3046       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
3047         {
3048           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
3049
3050           if ((remainder | shift_mask) != 0xffffffff)
3051             {
3052               if (generate)
3053                 {
3054                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3055
3056                   insns = arm_gen_constant (AND, mode, cond,
3057                                             remainder | shift_mask,
3058                                             new_src, source, subtargets, 1);
3059                   source = new_src;
3060                 }
3061               else
3062                 {
3063                   rtx targ = subtargets ? NULL_RTX : target;
3064
3065                   insns = arm_gen_constant (AND, mode, cond,
3066                                             remainder | shift_mask,
3067                                             targ, source, subtargets, 0);
3068                 }
3069             }
3070
3071           if (generate)
3072             {
3073               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3074               rtx shift = GEN_INT (clear_zero_bit_copies);
3075
3076               emit_insn (gen_lshrsi3 (new_src, source, shift));
3077               emit_insn (gen_ashlsi3 (target, new_src, shift));
3078             }
3079
3080           return insns + 2;
3081         }
3082
3083       break;
3084
3085     default:
3086       break;
3087     }
3088
3089   for (i = 0; i < 32; i++)
3090     if (remainder & (1 << i))
3091       num_bits_set++;
3092
3093   if ((code == AND)
3094       || (code != IOR && can_invert && num_bits_set > 16))
3095     remainder ^= 0xffffffff;
3096   else if (code == PLUS && num_bits_set > 16)
3097     remainder = (-remainder) & 0xffffffff;
3098
3099   /* For XOR, if more than half the bits are set and there's a sequence
3100      of more than 8 consecutive ones in the pattern then we can XOR by the
3101      inverted constant and then invert the final result; this may save an
3102      instruction and might also lead to the final mvn being merged with
3103      some other operation.  */
3104   else if (code == XOR && num_bits_set > 16
3105            && (count_insns_for_constant (remainder ^ 0xffffffff,
3106                                          find_best_start
3107                                          (remainder ^ 0xffffffff))
3108                < count_insns_for_constant (remainder,
3109                                            find_best_start (remainder))))
3110     {
3111       remainder ^= 0xffffffff;
3112       final_invert = 1;
3113     }
3114   else
3115     {
3116       can_invert = 0;
3117       can_negate = 0;
3118     }
3119
3120   /* Now try and find a way of doing the job in either two or three
3121      instructions.
3122      We start by looking for the largest block of zeros that are aligned on
3123      a 2-bit boundary, we then fill up the temps, wrapping around to the
3124      top of the word when we drop off the bottom.
3125      In the worst case this code should produce no more than four insns.
3126      Thumb-2 constants are shifted, not rotated, so the MSB is always the
3127      best place to start.  */
3128
3129   /* ??? Use thumb2 replicated constants when the high and low halfwords are
3130      the same.  */
3131   {
3132     /* Now start emitting the insns.  */
3133     i = find_best_start (remainder);
3134     do
3135       {
3136         int end;
3137
3138         if (i <= 0)
3139           i += 32;
3140         if (remainder & (3 << (i - 2)))
3141           {
3142             end = i - 8;
3143             if (end < 0)
3144               end += 32;
3145             temp1 = remainder & ((0x0ff << end)
3146                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
3147             remainder &= ~temp1;
3148
3149             if (generate)
3150               {
3151                 rtx new_src, temp1_rtx;
3152
3153                 if (code == SET || code == MINUS)
3154                   {
3155                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
3156                     if (can_invert && code != MINUS)
3157                       temp1 = ~temp1;
3158                   }
3159                 else
3160                   {
3161                     if ((final_invert || remainder) && subtargets)
3162                       new_src = gen_reg_rtx (mode);
3163                     else
3164                       new_src = target;
3165                     if (can_invert)
3166                       temp1 = ~temp1;
3167                     else if (can_negate)
3168                       temp1 = -temp1;
3169                   }
3170
3171                 temp1 = trunc_int_for_mode (temp1, mode);
3172                 temp1_rtx = GEN_INT (temp1);
3173
3174                 if (code == SET)
3175                   ;
3176                 else if (code == MINUS)
3177                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
3178                 else
3179                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
3180
3181                 emit_constant_insn (cond,
3182                                     gen_rtx_SET (VOIDmode, new_src,
3183                                                  temp1_rtx));
3184                 source = new_src;
3185               }
3186
3187             if (code == SET)
3188               {
3189                 can_invert = 0;
3190                 code = PLUS;
3191               }
3192             else if (code == MINUS)
3193               code = PLUS;
3194
3195             insns++;
3196             i -= 8 - step_size;
3197           }
3198         /* Arm allows rotates by a multiple of two. Thumb-2 allows arbitrary
3199            shifts.  */
3200         i -= step_size;
3201       }
3202     while (remainder);
3203   }
3204
3205   if (final_invert)
3206     {
3207       if (generate)
3208         emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
3209                                                gen_rtx_NOT (mode, source)));
3210       insns++;
3211     }
3212
3213   return insns;
3214 }
3215
3216 /* Canonicalize a comparison so that we are more likely to recognize it.
3217    This can be done for a few constant compares, where we can make the
3218    immediate value easier to load.  */
3219
3220 enum rtx_code
3221 arm_canonicalize_comparison (enum rtx_code code, rtx *op0, rtx *op1)
3222 {
3223   enum machine_mode mode;
3224   unsigned HOST_WIDE_INT i, maxval;
3225
3226   mode = GET_MODE (*op0);
3227   if (mode == VOIDmode)
3228     mode = GET_MODE (*op1);
3229
3230   maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
3231
3232   /* For DImode, we have GE/LT/GEU/LTU comparisons.  In ARM mode
3233      we can also use cmp/cmpeq for GTU/LEU.  GT/LE must be either
3234      reversed or (for constant OP1) adjusted to GE/LT.  Similarly
3235      for GTU/LEU in Thumb mode.  */
3236   if (mode == DImode)
3237     {
3238       rtx tem;
3239
3240       /* To keep things simple, always use the Cirrus cfcmp64 if it is
3241          available.  */
3242       if (TARGET_ARM && TARGET_HARD_FLOAT && TARGET_MAVERICK)
3243         return code;
3244
3245       if (code == GT || code == LE
3246           || (!TARGET_ARM && (code == GTU || code == LEU)))
3247         {
3248           /* Missing comparison.  First try to use an available
3249              comparison.  */
3250           if (GET_CODE (*op1) == CONST_INT)
3251             {
3252               i = INTVAL (*op1);
3253               switch (code)
3254                 {
3255                 case GT:
3256                 case LE:
3257                   if (i != maxval
3258                       && arm_const_double_by_immediates (GEN_INT (i + 1)))
3259                     {
3260                       *op1 = GEN_INT (i + 1);
3261                       return code == GT ? GE : LT;
3262                     }
3263                   break;
3264                 case GTU:
3265                 case LEU:
3266                   if (i != ~((unsigned HOST_WIDE_INT) 0)
3267                       && arm_const_double_by_immediates (GEN_INT (i + 1)))
3268                     {
3269                       *op1 = GEN_INT (i + 1);
3270                       return code == GTU ? GEU : LTU;
3271                     }
3272                   break;
3273                 default:
3274                   gcc_unreachable ();
3275                 }
3276             }
3277
3278           /* If that did not work, reverse the condition.  */
3279           tem = *op0;
3280           *op0 = *op1;
3281           *op1 = tem;
3282           return swap_condition (code);
3283         }
3284
3285       return code;
3286     }
3287
3288   /* Comparisons smaller than DImode.  Only adjust comparisons against
3289      an out-of-range constant.  */
3290   if (GET_CODE (*op1) != CONST_INT
3291       || const_ok_for_arm (INTVAL (*op1))
3292       || const_ok_for_arm (- INTVAL (*op1)))
3293     return code;
3294
3295   i = INTVAL (*op1);
3296
3297   switch (code)
3298     {
3299     case EQ:
3300     case NE:
3301       return code;
3302
3303     case GT:
3304     case LE:
3305       if (i != maxval
3306           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
3307         {
3308           *op1 = GEN_INT (i + 1);
3309           return code == GT ? GE : LT;
3310         }
3311       break;
3312
3313     case GE:
3314     case LT:
3315       if (i != ~maxval
3316           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
3317         {
3318           *op1 = GEN_INT (i - 1);
3319           return code == GE ? GT : LE;
3320         }
3321       break;
3322
3323     case GTU:
3324     case LEU:
3325       if (i != ~((unsigned HOST_WIDE_INT) 0)
3326           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
3327         {
3328           *op1 = GEN_INT (i + 1);
3329           return code == GTU ? GEU : LTU;
3330         }
3331       break;
3332
3333     case GEU:
3334     case LTU:
3335       if (i != 0
3336           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
3337         {
3338           *op1 = GEN_INT (i - 1);
3339           return code == GEU ? GTU : LEU;
3340         }
3341       break;
3342
3343     default:
3344       gcc_unreachable ();
3345     }
3346
3347   return code;
3348 }
3349
3350
3351 /* Define how to find the value returned by a function.  */
3352
3353 static rtx
3354 arm_function_value(const_tree type, const_tree func,
3355                    bool outgoing ATTRIBUTE_UNUSED)
3356 {
3357   enum machine_mode mode;
3358   int unsignedp ATTRIBUTE_UNUSED;
3359   rtx r ATTRIBUTE_UNUSED;
3360
3361   mode = TYPE_MODE (type);
3362
3363   if (TARGET_AAPCS_BASED)
3364     return aapcs_allocate_return_reg (mode, type, func);
3365
3366   /* Promote integer types.  */
3367   if (INTEGRAL_TYPE_P (type))
3368     mode = arm_promote_function_mode (type, mode, &unsignedp, func, 1);
3369
3370   /* Promotes small structs returned in a register to full-word size
3371      for big-endian AAPCS.  */
3372   if (arm_return_in_msb (type))
3373     {
3374       HOST_WIDE_INT size = int_size_in_bytes (type);
3375       if (size % UNITS_PER_WORD != 0)
3376         {
3377           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
3378           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
3379         }
3380     }
3381
3382   return LIBCALL_VALUE (mode);
3383 }
3384
3385 static int
3386 libcall_eq (const void *p1, const void *p2)
3387 {
3388   return rtx_equal_p ((const_rtx) p1, (const_rtx) p2);
3389 }
3390
3391 static hashval_t
3392 libcall_hash (const void *p1)
3393 {
3394   return hash_rtx ((const_rtx) p1, VOIDmode, NULL, NULL, FALSE);
3395 }
3396
3397 static void
3398 add_libcall (htab_t htab, rtx libcall)
3399 {
3400   *htab_find_slot (htab, libcall, INSERT) = libcall;
3401 }
3402
3403 static bool
3404 arm_libcall_uses_aapcs_base (const_rtx libcall)
3405 {
3406   static bool init_done = false;
3407   static htab_t libcall_htab;
3408
3409   if (!init_done)
3410     {
3411       init_done = true;
3412
3413       libcall_htab = htab_create (31, libcall_hash, libcall_eq,
3414                                   NULL);
3415       add_libcall (libcall_htab,
3416                    convert_optab_libfunc (sfloat_optab, SFmode, SImode));
3417       add_libcall (libcall_htab,
3418                    convert_optab_libfunc (sfloat_optab, DFmode, SImode));
3419       add_libcall (libcall_htab,
3420                    convert_optab_libfunc (sfloat_optab, SFmode, DImode));
3421       add_libcall (libcall_htab,
3422                    convert_optab_libfunc (sfloat_optab, DFmode, DImode));
3423       
3424       add_libcall (libcall_htab,
3425                    convert_optab_libfunc (ufloat_optab, SFmode, SImode));
3426       add_libcall (libcall_htab,
3427                    convert_optab_libfunc (ufloat_optab, DFmode, SImode));
3428       add_libcall (libcall_htab,
3429                    convert_optab_libfunc (ufloat_optab, SFmode, DImode));
3430       add_libcall (libcall_htab,
3431                    convert_optab_libfunc (ufloat_optab, DFmode, DImode));
3432
3433       add_libcall (libcall_htab,
3434                    convert_optab_libfunc (sext_optab, SFmode, HFmode));
3435       add_libcall (libcall_htab,
3436                    convert_optab_libfunc (trunc_optab, HFmode, SFmode));
3437       add_libcall (libcall_htab,
3438                    convert_optab_libfunc (sfix_optab, DImode, DFmode));
3439       add_libcall (libcall_htab,
3440                    convert_optab_libfunc (ufix_optab, DImode, DFmode));
3441       add_libcall (libcall_htab,
3442                    convert_optab_libfunc (sfix_optab, DImode, SFmode));
3443       add_libcall (libcall_htab,
3444                    convert_optab_libfunc (ufix_optab, DImode, SFmode));
3445     }
3446
3447   return libcall && htab_find (libcall_htab, libcall) != NULL;
3448 }
3449
3450 rtx
3451 arm_libcall_value (enum machine_mode mode, const_rtx libcall)
3452 {
3453   if (TARGET_AAPCS_BASED && arm_pcs_default != ARM_PCS_AAPCS
3454       && GET_MODE_CLASS (mode) == MODE_FLOAT)
3455     {
3456       /* The following libcalls return their result in integer registers,
3457          even though they return a floating point value.  */
3458       if (arm_libcall_uses_aapcs_base (libcall))
3459         return gen_rtx_REG (mode, ARG_REGISTER(1));
3460
3461     }
3462
3463   return LIBCALL_VALUE (mode);
3464 }
3465
3466 /* Determine the amount of memory needed to store the possible return
3467    registers of an untyped call.  */
3468 int
3469 arm_apply_result_size (void)
3470 {
3471   int size = 16;
3472
3473   if (TARGET_32BIT)
3474     {
3475       if (TARGET_HARD_FLOAT_ABI)
3476         {
3477           if (TARGET_VFP)
3478             size += 32;
3479           if (TARGET_FPA)
3480             size += 12;
3481           if (TARGET_MAVERICK)
3482             size += 8;
3483         }
3484       if (TARGET_IWMMXT_ABI)
3485         size += 8;
3486     }
3487
3488   return size;
3489 }
3490
3491 /* Decide whether TYPE should be returned in memory (true)
3492    or in a register (false).  FNTYPE is the type of the function making
3493    the call.  */
3494 static bool
3495 arm_return_in_memory (const_tree type, const_tree fntype)
3496 {
3497   HOST_WIDE_INT size;
3498
3499   size = int_size_in_bytes (type);  /* Negative if not fixed size.  */
3500
3501   if (TARGET_AAPCS_BASED)
3502     {
3503       /* Simple, non-aggregate types (ie not including vectors and
3504          complex) are always returned in a register (or registers).
3505          We don't care about which register here, so we can short-cut
3506          some of the detail.  */
3507       if (!AGGREGATE_TYPE_P (type)
3508           && TREE_CODE (type) != VECTOR_TYPE
3509           && TREE_CODE (type) != COMPLEX_TYPE)
3510         return false;
3511
3512       /* Any return value that is no larger than one word can be
3513          returned in r0.  */
3514       if (((unsigned HOST_WIDE_INT) size) <= UNITS_PER_WORD)
3515         return false;
3516
3517       /* Check any available co-processors to see if they accept the
3518          type as a register candidate (VFP, for example, can return
3519          some aggregates in consecutive registers).  These aren't
3520          available if the call is variadic.  */
3521       if (aapcs_select_return_coproc (type, fntype) >= 0)
3522         return false;
3523
3524       /* Vector values should be returned using ARM registers, not
3525          memory (unless they're over 16 bytes, which will break since
3526          we only have four call-clobbered registers to play with).  */
3527       if (TREE_CODE (type) == VECTOR_TYPE)
3528         return (size < 0 || size > (4 * UNITS_PER_WORD));
3529
3530       /* The rest go in memory.  */
3531       return true;
3532     }
3533
3534   if (TREE_CODE (type) == VECTOR_TYPE)
3535     return (size < 0 || size > (4 * UNITS_PER_WORD));
3536
3537   if (!AGGREGATE_TYPE_P (type) &&
3538       (TREE_CODE (type) != VECTOR_TYPE))
3539     /* All simple types are returned in registers.  */
3540     return false;
3541
3542   if (arm_abi != ARM_ABI_APCS)
3543     {
3544       /* ATPCS and later return aggregate types in memory only if they are
3545          larger than a word (or are variable size).  */
3546       return (size < 0 || size > UNITS_PER_WORD);
3547     }
3548
3549   /* For the arm-wince targets we choose to be compatible with Microsoft's
3550      ARM and Thumb compilers, which always return aggregates in memory.  */
3551 #ifndef ARM_WINCE
3552   /* All structures/unions bigger than one word are returned in memory.
3553      Also catch the case where int_size_in_bytes returns -1.  In this case
3554      the aggregate is either huge or of variable size, and in either case
3555      we will want to return it via memory and not in a register.  */
3556   if (size < 0 || size > UNITS_PER_WORD)
3557     return true;
3558
3559   if (TREE_CODE (type) == RECORD_TYPE)
3560     {
3561       tree field;
3562
3563       /* For a struct the APCS says that we only return in a register
3564          if the type is 'integer like' and every addressable element
3565          has an offset of zero.  For practical purposes this means
3566          that the structure can have at most one non bit-field element
3567          and that this element must be the first one in the structure.  */
3568
3569       /* Find the first field, ignoring non FIELD_DECL things which will
3570          have been created by C++.  */
3571       for (field = TYPE_FIELDS (type);
3572            field && TREE_CODE (field) != FIELD_DECL;
3573            field = DECL_CHAIN (field))
3574         continue;
3575
3576       if (field == NULL)
3577         return false; /* An empty structure.  Allowed by an extension to ANSI C.  */
3578
3579       /* Check that the first field is valid for returning in a register.  */
3580
3581       /* ... Floats are not allowed */
3582       if (FLOAT_TYPE_P (TREE_TYPE (field)))
3583         return true;
3584
3585       /* ... Aggregates that are not themselves valid for returning in
3586          a register are not allowed.  */
3587       if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
3588         return true;
3589
3590       /* Now check the remaining fields, if any.  Only bitfields are allowed,
3591          since they are not addressable.  */
3592       for (field = DECL_CHAIN (field);
3593            field;
3594            field = DECL_CHAIN (field))
3595         {
3596           if (TREE_CODE (field) != FIELD_DECL)
3597             continue;
3598
3599           if (!DECL_BIT_FIELD_TYPE (field))
3600             return true;
3601         }
3602
3603       return false;
3604     }
3605
3606   if (TREE_CODE (type) == UNION_TYPE)
3607     {
3608       tree field;
3609
3610       /* Unions can be returned in registers if every element is
3611          integral, or can be returned in an integer register.  */
3612       for (field = TYPE_FIELDS (type);
3613            field;
3614            field = DECL_CHAIN (field))
3615         {
3616           if (TREE_CODE (field) != FIELD_DECL)
3617             continue;
3618
3619           if (FLOAT_TYPE_P (TREE_TYPE (field)))
3620             return true;
3621
3622           if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
3623             return true;
3624         }
3625
3626       return false;
3627     }
3628 #endif /* not ARM_WINCE */
3629
3630   /* Return all other types in memory.  */
3631   return true;
3632 }
3633
3634 /* Indicate whether or not words of a double are in big-endian order.  */
3635
3636 int
3637 arm_float_words_big_endian (void)
3638 {
3639   if (TARGET_MAVERICK)
3640     return 0;
3641
3642   /* For FPA, float words are always big-endian.  For VFP, floats words
3643      follow the memory system mode.  */
3644
3645   if (TARGET_FPA)
3646     {
3647       return 1;
3648     }
3649
3650   if (TARGET_VFP)
3651     return (TARGET_BIG_END ? 1 : 0);
3652
3653   return 1;
3654 }
3655
3656 const struct pcs_attribute_arg
3657 {
3658   const char *arg;
3659   enum arm_pcs value;
3660 } pcs_attribute_args[] =
3661   {
3662     {"aapcs", ARM_PCS_AAPCS},
3663     {"aapcs-vfp", ARM_PCS_AAPCS_VFP},
3664 #if 0
3665     /* We could recognize these, but changes would be needed elsewhere
3666      * to implement them.  */
3667     {"aapcs-iwmmxt", ARM_PCS_AAPCS_IWMMXT},
3668     {"atpcs", ARM_PCS_ATPCS},
3669     {"apcs", ARM_PCS_APCS},
3670 #endif
3671     {NULL, ARM_PCS_UNKNOWN}
3672   };
3673
3674 static enum arm_pcs
3675 arm_pcs_from_attribute (tree attr)
3676 {
3677   const struct pcs_attribute_arg *ptr;
3678   const char *arg;
3679
3680   /* Get the value of the argument.  */
3681   if (TREE_VALUE (attr) == NULL_TREE
3682       || TREE_CODE (TREE_VALUE (attr)) != STRING_CST)
3683     return ARM_PCS_UNKNOWN;
3684
3685   arg = TREE_STRING_POINTER (TREE_VALUE (attr));
3686
3687   /* Check it against the list of known arguments.  */
3688   for (ptr = pcs_attribute_args; ptr->arg != NULL; ptr++)
3689     if (streq (arg, ptr->arg))
3690       return ptr->value;
3691
3692   /* An unrecognized interrupt type.  */
3693   return ARM_PCS_UNKNOWN;
3694 }
3695
3696 /* Get the PCS variant to use for this call.  TYPE is the function's type
3697    specification, DECL is the specific declartion.  DECL may be null if
3698    the call could be indirect or if this is a library call.  */
3699 static enum arm_pcs
3700 arm_get_pcs_model (const_tree type, const_tree decl)
3701 {
3702   bool user_convention = false;
3703   enum arm_pcs user_pcs = arm_pcs_default;
3704   tree attr;
3705
3706   gcc_assert (type);
3707
3708   attr = lookup_attribute ("pcs", TYPE_ATTRIBUTES (type));
3709   if (attr)
3710     {
3711       user_pcs = arm_pcs_from_attribute (TREE_VALUE (attr));
3712       user_convention = true;
3713     }
3714
3715   if (TARGET_AAPCS_BASED)
3716     {
3717       /* Detect varargs functions.  These always use the base rules
3718          (no argument is ever a candidate for a co-processor
3719          register).  */
3720       bool base_rules = stdarg_p (type);
3721       
3722       if (user_convention)
3723         {
3724           if (user_pcs > ARM_PCS_AAPCS_LOCAL)
3725             sorry ("Non-AAPCS derived PCS variant");
3726           else if (base_rules && user_pcs != ARM_PCS_AAPCS)
3727             error ("Variadic functions must use the base AAPCS variant");
3728         }
3729
3730       if (base_rules)
3731         return ARM_PCS_AAPCS;
3732       else if (user_convention)
3733         return user_pcs;
3734       else if (decl && flag_unit_at_a_time)
3735         {
3736           /* Local functions never leak outside this compilation unit,
3737              so we are free to use whatever conventions are
3738              appropriate.  */
3739           /* FIXME: remove CONST_CAST_TREE when cgraph is constified.  */
3740           struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE(decl));
3741           if (i && i->local)
3742             return ARM_PCS_AAPCS_LOCAL;
3743         }
3744     }
3745   else if (user_convention && user_pcs != arm_pcs_default)
3746     sorry ("PCS variant");
3747
3748   /* For everything else we use the target's default.  */
3749   return arm_pcs_default;
3750 }
3751
3752
3753 static void
3754 aapcs_vfp_cum_init (CUMULATIVE_ARGS *pcum  ATTRIBUTE_UNUSED,
3755                     const_tree fntype ATTRIBUTE_UNUSED,
3756                     rtx libcall ATTRIBUTE_UNUSED, 
3757                     const_tree fndecl ATTRIBUTE_UNUSED)
3758 {
3759   /* Record the unallocated VFP registers.  */
3760   pcum->aapcs_vfp_regs_free = (1 << NUM_VFP_ARG_REGS) - 1;
3761   pcum->aapcs_vfp_reg_alloc = 0;
3762 }
3763
3764 /* Walk down the type tree of TYPE counting consecutive base elements.
3765    If *MODEP is VOIDmode, then set it to the first valid floating point
3766    type.  If a non-floating point type is found, or if a floating point
3767    type that doesn't match a non-VOIDmode *MODEP is found, then return -1,
3768    otherwise return the count in the sub-tree.  */
3769 static int
3770 aapcs_vfp_sub_candidate (const_tree type, enum machine_mode *modep)
3771 {
3772   enum machine_mode mode;
3773   HOST_WIDE_INT size;
3774
3775   switch (TREE_CODE (type))
3776     {
3777     case REAL_TYPE:
3778       mode = TYPE_MODE (type);
3779       if (mode != DFmode && mode != SFmode)
3780         return -1;
3781
3782       if (*modep == VOIDmode)
3783         *modep = mode;
3784
3785       if (*modep == mode)
3786         return 1;
3787
3788       break;
3789
3790     case COMPLEX_TYPE:
3791       mode = TYPE_MODE (TREE_TYPE (type));
3792       if (mode != DFmode && mode != SFmode)
3793         return -1;
3794
3795       if (*modep == VOIDmode)
3796         *modep = mode;
3797
3798       if (*modep == mode)
3799         return 2;
3800
3801       break;
3802
3803     case VECTOR_TYPE:
3804       /* Use V2SImode and V4SImode as representatives of all 64-bit
3805          and 128-bit vector types, whether or not those modes are
3806          supported with the present options.  */
3807       size = int_size_in_bytes (type);
3808       switch (size)
3809         {
3810         case 8:
3811           mode = V2SImode;
3812           break;
3813         case 16:
3814           mode = V4SImode;
3815           break;
3816         default:
3817           return -1;
3818         }
3819
3820       if (*modep == VOIDmode)
3821         *modep = mode;
3822
3823       /* Vector modes are considered to be opaque: two vectors are
3824          equivalent for the purposes of being homogeneous aggregates
3825          if they are the same size.  */
3826       if (*modep == mode)
3827         return 1;
3828
3829       break;
3830
3831     case ARRAY_TYPE:
3832       {
3833         int count;
3834         tree index = TYPE_DOMAIN (type);
3835
3836         /* Can't handle incomplete types.  */
3837         if (!COMPLETE_TYPE_P(type))
3838           return -1;
3839
3840         count = aapcs_vfp_sub_candidate (TREE_TYPE (type), modep);
3841         if (count == -1
3842             || !index
3843             || !TYPE_MAX_VALUE (index)
3844             || !host_integerp (TYPE_MAX_VALUE (index), 1)
3845             || !TYPE_MIN_VALUE (index)
3846             || !host_integerp (TYPE_MIN_VALUE (index), 1)
3847             || count < 0)
3848           return -1;
3849
3850         count *= (1 + tree_low_cst (TYPE_MAX_VALUE (index), 1)
3851                       - tree_low_cst (TYPE_MIN_VALUE (index), 1));
3852
3853         /* There must be no padding.  */
3854         if (!host_integerp (TYPE_SIZE (type), 1)
3855             || (tree_low_cst (TYPE_SIZE (type), 1)
3856                 != count * GET_MODE_BITSIZE (*modep)))
3857           return -1;
3858
3859         return count;
3860       }
3861       
3862     case RECORD_TYPE:
3863       {
3864         int count = 0;
3865         int sub_count;
3866         tree field;
3867
3868         /* Can't handle incomplete types.  */
3869         if (!COMPLETE_TYPE_P(type))
3870           return -1;
3871
3872         for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3873           {
3874             if (TREE_CODE (field) != FIELD_DECL)
3875               continue;
3876
3877             sub_count = aapcs_vfp_sub_candidate (TREE_TYPE (field), modep);
3878             if (sub_count < 0)
3879               return -1;
3880             count += sub_count;
3881           }
3882
3883         /* There must be no padding.  */
3884         if (!host_integerp (TYPE_SIZE (type), 1)
3885             || (tree_low_cst (TYPE_SIZE (type), 1)
3886                 != count * GET_MODE_BITSIZE (*modep)))
3887           return -1;
3888
3889         return count;
3890       }
3891
3892     case UNION_TYPE:
3893     case QUAL_UNION_TYPE:
3894       {
3895         /* These aren't very interesting except in a degenerate case.  */
3896         int count = 0;
3897         int sub_count;
3898         tree field;
3899
3900         /* Can't handle incomplete types.  */
3901         if (!COMPLETE_TYPE_P(type))
3902           return -1;
3903
3904         for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3905           {
3906             if (TREE_CODE (field) != FIELD_DECL)
3907               continue;
3908
3909             sub_count = aapcs_vfp_sub_candidate (TREE_TYPE (field), modep);
3910             if (sub_count < 0)
3911               return -1;
3912             count = count > sub_count ? count : sub_count;
3913           }
3914
3915         /* There must be no padding.  */
3916         if (!host_integerp (TYPE_SIZE (type), 1)
3917             || (tree_low_cst (TYPE_SIZE (type), 1)
3918                 != count * GET_MODE_BITSIZE (*modep)))
3919           return -1;
3920
3921         return count;
3922       }
3923
3924     default:
3925       break;
3926     }
3927
3928   return -1;
3929 }
3930
3931 /* Return true if PCS_VARIANT should use VFP registers.  */
3932 static bool
3933 use_vfp_abi (enum arm_pcs pcs_variant, bool is_double)
3934 {
3935   if (pcs_variant == ARM_PCS_AAPCS_VFP)
3936     {
3937       static bool seen_thumb1_vfp = false;
3938
3939       if (TARGET_THUMB1 && !seen_thumb1_vfp)
3940         {
3941           sorry ("Thumb-1 hard-float VFP ABI");
3942           /* sorry() is not immediately fatal, so only display this once.  */
3943           seen_thumb1_vfp = true;
3944         }
3945
3946       return true;
3947     }
3948
3949   if (pcs_variant != ARM_PCS_AAPCS_LOCAL)
3950     return false;
3951
3952   return (TARGET_32BIT && TARGET_VFP && TARGET_HARD_FLOAT &&
3953           (TARGET_VFP_DOUBLE || !is_double));
3954 }
3955
3956 static bool
3957 aapcs_vfp_is_call_or_return_candidate (enum arm_pcs pcs_variant,
3958                                        enum machine_mode mode, const_tree type,
3959                                        enum machine_mode *base_mode, int *count)
3960 {
3961   enum machine_mode new_mode = VOIDmode;
3962
3963   if (GET_MODE_CLASS (mode) == MODE_FLOAT
3964       || GET_MODE_CLASS (mode) == MODE_VECTOR_INT
3965       || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
3966     {
3967       *count = 1;
3968       new_mode = mode;
3969     }
3970   else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3971     {
3972       *count = 2;
3973       new_mode = (mode == DCmode ? DFmode : SFmode);
3974     }
3975   else if (type && (mode == BLKmode || TREE_CODE (type) == VECTOR_TYPE))
3976     {
3977       int ag_count = aapcs_vfp_sub_candidate (type, &new_mode);
3978
3979       if (ag_count > 0 && ag_count <= 4)
3980         *count = ag_count;
3981       else
3982         return false;
3983     }
3984   else
3985     return false;
3986
3987
3988   if (!use_vfp_abi (pcs_variant, ARM_NUM_REGS (new_mode) > 1))
3989     return false;
3990
3991   *base_mode = new_mode;
3992   return true;
3993 }
3994
3995 static bool
3996 aapcs_vfp_is_return_candidate (enum arm_pcs pcs_variant,
3997                                enum machine_mode mode, const_tree type)
3998 {
3999   int count ATTRIBUTE_UNUSED;
4000   enum machine_mode ag_mode ATTRIBUTE_UNUSED;
4001
4002   if (!use_vfp_abi (pcs_variant, false))
4003     return false;
4004   return aapcs_vfp_is_call_or_return_candidate (pcs_variant, mode, type,
4005                                                 &ag_mode, &count);
4006 }
4007
4008 static bool
4009 aapcs_vfp_is_call_candidate (CUMULATIVE_ARGS *pcum, enum machine_mode mode, 
4010                              const_tree type)
4011 {
4012   if (!use_vfp_abi (pcum->pcs_variant, false))
4013     return false;
4014
4015   return aapcs_vfp_is_call_or_return_candidate (pcum->pcs_variant, mode, type,
4016                                                 &pcum->aapcs_vfp_rmode,
4017                                                 &pcum->aapcs_vfp_rcount);
4018 }
4019
4020 static bool
4021 aapcs_vfp_allocate (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4022                     const_tree type  ATTRIBUTE_UNUSED)
4023 {
4024   int shift = GET_MODE_SIZE (pcum->aapcs_vfp_rmode) / GET_MODE_SIZE (SFmode);
4025   unsigned mask = (1 << (shift * pcum->aapcs_vfp_rcount)) - 1;
4026   int regno;
4027   
4028   for (regno = 0; regno < NUM_VFP_ARG_REGS; regno += shift)
4029     if (((pcum->aapcs_vfp_regs_free >> regno) & mask) == mask)
4030       {
4031         pcum->aapcs_vfp_reg_alloc = mask << regno;
4032         if (mode == BLKmode || (mode == TImode && !TARGET_NEON))
4033           {
4034             int i;
4035             int rcount = pcum->aapcs_vfp_rcount;
4036             int rshift = shift;
4037             enum machine_mode rmode = pcum->aapcs_vfp_rmode;
4038             rtx par;
4039             if (!TARGET_NEON)
4040               {
4041                 /* Avoid using unsupported vector modes.  */
4042                 if (rmode == V2SImode)
4043                   rmode = DImode;
4044                 else if (rmode == V4SImode)
4045                   {
4046                     rmode = DImode;
4047                     rcount *= 2;
4048                     rshift /= 2;
4049                   }
4050               }
4051             par = gen_rtx_PARALLEL (mode, rtvec_alloc (rcount));
4052             for (i = 0; i < rcount; i++)
4053               {
4054                 rtx tmp = gen_rtx_REG (rmode, 
4055                                        FIRST_VFP_REGNUM + regno + i * rshift);
4056                 tmp = gen_rtx_EXPR_LIST
4057                   (VOIDmode, tmp, 
4058                    GEN_INT (i * GET_MODE_SIZE (rmode)));
4059                 XVECEXP (par, 0, i) = tmp;
4060               }
4061
4062             pcum->aapcs_reg = par;
4063           }
4064         else
4065           pcum->aapcs_reg = gen_rtx_REG (mode, FIRST_VFP_REGNUM + regno);
4066         return true;
4067       }
4068   return false;
4069 }
4070
4071 static rtx
4072 aapcs_vfp_allocate_return_reg (enum arm_pcs pcs_variant ATTRIBUTE_UNUSED,
4073                                enum machine_mode mode,
4074                                const_tree type ATTRIBUTE_UNUSED)
4075 {
4076   if (!use_vfp_abi (pcs_variant, false))
4077     return false;
4078
4079   if (mode == BLKmode || (mode == TImode && !TARGET_NEON))
4080     {
4081       int count;
4082       enum machine_mode ag_mode;
4083       int i;
4084       rtx par;
4085       int shift;
4086       
4087       aapcs_vfp_is_call_or_return_candidate (pcs_variant, mode, type,
4088                                              &ag_mode, &count);
4089
4090       if (!TARGET_NEON)
4091         {
4092           if (ag_mode == V2SImode)
4093             ag_mode = DImode;
4094           else if (ag_mode == V4SImode)
4095             {
4096               ag_mode = DImode;
4097               count *= 2;
4098             }
4099         }
4100       shift = GET_MODE_SIZE(ag_mode) / GET_MODE_SIZE(SFmode);
4101       par = gen_rtx_PARALLEL (mode, rtvec_alloc (count));
4102       for (i = 0; i < count; i++)
4103         {
4104           rtx tmp = gen_rtx_REG (ag_mode, FIRST_VFP_REGNUM + i * shift);
4105           tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, 
4106                                    GEN_INT (i * GET_MODE_SIZE (ag_mode)));
4107           XVECEXP (par, 0, i) = tmp;
4108         }
4109
4110       return par;
4111     }
4112
4113   return gen_rtx_REG (mode, FIRST_VFP_REGNUM);
4114 }
4115
4116 static void
4117 aapcs_vfp_advance (CUMULATIVE_ARGS *pcum  ATTRIBUTE_UNUSED,
4118                    enum machine_mode mode  ATTRIBUTE_UNUSED,
4119                    const_tree type  ATTRIBUTE_UNUSED)
4120 {
4121   pcum->aapcs_vfp_regs_free &= ~pcum->aapcs_vfp_reg_alloc;
4122   pcum->aapcs_vfp_reg_alloc = 0;
4123   return;
4124 }
4125
4126 #define AAPCS_CP(X)                             \
4127   {                                             \
4128     aapcs_ ## X ## _cum_init,                   \
4129     aapcs_ ## X ## _is_call_candidate,          \
4130     aapcs_ ## X ## _allocate,                   \
4131     aapcs_ ## X ## _is_return_candidate,        \
4132     aapcs_ ## X ## _allocate_return_reg,        \
4133     aapcs_ ## X ## _advance                     \
4134   }
4135
4136 /* Table of co-processors that can be used to pass arguments in
4137    registers.  Idealy no arugment should be a candidate for more than
4138    one co-processor table entry, but the table is processed in order
4139    and stops after the first match.  If that entry then fails to put
4140    the argument into a co-processor register, the argument will go on
4141    the stack.  */
4142 static struct 
4143 {
4144   /* Initialize co-processor related state in CUMULATIVE_ARGS structure.  */
4145   void (*cum_init) (CUMULATIVE_ARGS *, const_tree, rtx, const_tree);
4146
4147   /* Return true if an argument of mode MODE (or type TYPE if MODE is
4148      BLKmode) is a candidate for this co-processor's registers; this
4149      function should ignore any position-dependent state in
4150      CUMULATIVE_ARGS and only use call-type dependent information.  */
4151   bool (*is_call_candidate) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4152
4153   /* Return true if the argument does get a co-processor register; it
4154      should set aapcs_reg to an RTX of the register allocated as is
4155      required for a return from FUNCTION_ARG.  */
4156   bool (*allocate) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4157
4158   /* Return true if a result of mode MODE (or type TYPE if MODE is
4159      BLKmode) is can be returned in this co-processor's registers.  */
4160   bool (*is_return_candidate) (enum arm_pcs, enum machine_mode, const_tree);
4161
4162   /* Allocate and return an RTX element to hold the return type of a
4163      call, this routine must not fail and will only be called if
4164      is_return_candidate returned true with the same parameters.  */
4165   rtx (*allocate_return_reg) (enum arm_pcs, enum machine_mode, const_tree);
4166
4167   /* Finish processing this argument and prepare to start processing
4168      the next one.  */
4169   void (*advance) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4170 } aapcs_cp_arg_layout[ARM_NUM_COPROC_SLOTS] =
4171   {
4172     AAPCS_CP(vfp)
4173   };
4174
4175 #undef AAPCS_CP
4176
4177 static int
4178 aapcs_select_call_coproc (CUMULATIVE_ARGS *pcum, enum machine_mode mode, 
4179                           tree type)
4180 {
4181   int i;
4182
4183   for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4184     if (aapcs_cp_arg_layout[i].is_call_candidate (pcum, mode, type))
4185       return i;
4186
4187   return -1;
4188 }
4189
4190 static int
4191 aapcs_select_return_coproc (const_tree type, const_tree fntype)
4192 {
4193   /* We aren't passed a decl, so we can't check that a call is local.
4194      However, it isn't clear that that would be a win anyway, since it
4195      might limit some tail-calling opportunities.  */
4196   enum arm_pcs pcs_variant;
4197
4198   if (fntype)
4199     {
4200       const_tree fndecl = NULL_TREE;
4201
4202       if (TREE_CODE (fntype) == FUNCTION_DECL)
4203         {
4204           fndecl = fntype;
4205           fntype = TREE_TYPE (fntype);
4206         }
4207
4208       pcs_variant = arm_get_pcs_model (fntype, fndecl);
4209     }
4210   else
4211     pcs_variant = arm_pcs_default;
4212
4213   if (pcs_variant != ARM_PCS_AAPCS)
4214     {
4215       int i;
4216
4217       for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4218         if (aapcs_cp_arg_layout[i].is_return_candidate (pcs_variant, 
4219                                                         TYPE_MODE (type),
4220                                                         type))
4221           return i;
4222     }
4223   return -1;
4224 }
4225
4226 static rtx
4227 aapcs_allocate_return_reg (enum machine_mode mode, const_tree type,
4228                            const_tree fntype)
4229 {
4230   /* We aren't passed a decl, so we can't check that a call is local.
4231      However, it isn't clear that that would be a win anyway, since it
4232      might limit some tail-calling opportunities.  */
4233   enum arm_pcs pcs_variant;
4234   int unsignedp ATTRIBUTE_UNUSED;
4235
4236   if (fntype)
4237     {
4238       const_tree fndecl = NULL_TREE;
4239
4240       if (TREE_CODE (fntype) == FUNCTION_DECL)
4241         {
4242           fndecl = fntype;
4243           fntype = TREE_TYPE (fntype);
4244         }
4245
4246       pcs_variant = arm_get_pcs_model (fntype, fndecl);
4247     }
4248   else
4249     pcs_variant = arm_pcs_default;
4250
4251   /* Promote integer types.  */
4252   if (type && INTEGRAL_TYPE_P (type))
4253     mode = arm_promote_function_mode (type, mode, &unsignedp, fntype, 1);
4254
4255   if (pcs_variant != ARM_PCS_AAPCS)
4256     {
4257       int i;
4258
4259       for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4260         if (aapcs_cp_arg_layout[i].is_return_candidate (pcs_variant, mode,
4261                                                         type))
4262           return aapcs_cp_arg_layout[i].allocate_return_reg (pcs_variant,
4263                                                              mode, type);
4264     }
4265
4266   /* Promotes small structs returned in a register to full-word size
4267      for big-endian AAPCS.  */
4268   if (type && arm_return_in_msb (type))
4269     {
4270       HOST_WIDE_INT size = int_size_in_bytes (type);
4271       if (size % UNITS_PER_WORD != 0)
4272         {
4273           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
4274           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
4275         }
4276     }
4277
4278   return gen_rtx_REG (mode, R0_REGNUM);
4279 }
4280
4281 rtx
4282 aapcs_libcall_value (enum machine_mode mode)
4283 {
4284   return aapcs_allocate_return_reg (mode, NULL_TREE, NULL_TREE);
4285 }
4286
4287 /* Lay out a function argument using the AAPCS rules.  The rule
4288    numbers referred to here are those in the AAPCS.  */
4289 static void
4290 aapcs_layout_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4291                   tree type, int named)
4292 {
4293   int nregs, nregs2;
4294   int ncrn;
4295
4296   /* We only need to do this once per argument.  */
4297   if (pcum->aapcs_arg_processed)
4298     return;
4299
4300   pcum->aapcs_arg_processed = true;
4301
4302   /* Special case: if named is false then we are handling an incoming
4303      anonymous argument which is on the stack.  */
4304   if (!named)
4305     return;
4306   
4307   /* Is this a potential co-processor register candidate?  */
4308   if (pcum->pcs_variant != ARM_PCS_AAPCS)
4309     {
4310       int slot = aapcs_select_call_coproc (pcum, mode, type);
4311       pcum->aapcs_cprc_slot = slot;
4312
4313       /* We don't have to apply any of the rules from part B of the
4314          preparation phase, these are handled elsewhere in the
4315          compiler.  */
4316
4317       if (slot >= 0)
4318         {
4319           /* A Co-processor register candidate goes either in its own
4320              class of registers or on the stack.  */
4321           if (!pcum->aapcs_cprc_failed[slot])
4322             {
4323               /* C1.cp - Try to allocate the argument to co-processor
4324                  registers.  */
4325               if (aapcs_cp_arg_layout[slot].allocate (pcum, mode, type))
4326                 return;
4327
4328               /* C2.cp - Put the argument on the stack and note that we
4329                  can't assign any more candidates in this slot.  We also
4330                  need to note that we have allocated stack space, so that
4331                  we won't later try to split a non-cprc candidate between
4332                  core registers and the stack.  */
4333               pcum->aapcs_cprc_failed[slot] = true;
4334               pcum->can_split = false;
4335             }
4336
4337           /* We didn't get a register, so this argument goes on the
4338              stack.  */
4339           gcc_assert (pcum->can_split == false);
4340           return;
4341         }
4342     }
4343
4344   /* C3 - For double-word aligned arguments, round the NCRN up to the
4345      next even number.  */
4346   ncrn = pcum->aapcs_ncrn;
4347   if ((ncrn & 1) && arm_needs_doubleword_align (mode, type))
4348     ncrn++;
4349
4350   nregs = ARM_NUM_REGS2(mode, type);
4351
4352   /* Sigh, this test should really assert that nregs > 0, but a GCC
4353      extension allows empty structs and then gives them empty size; it
4354      then allows such a structure to be passed by value.  For some of
4355      the code below we have to pretend that such an argument has
4356      non-zero size so that we 'locate' it correctly either in
4357      registers or on the stack.  */
4358   gcc_assert (nregs >= 0);
4359
4360   nregs2 = nregs ? nregs : 1;
4361
4362   /* C4 - Argument fits entirely in core registers.  */
4363   if (ncrn + nregs2 <= NUM_ARG_REGS)
4364     {
4365       pcum->aapcs_reg = gen_rtx_REG (mode, ncrn);
4366       pcum->aapcs_next_ncrn = ncrn + nregs;
4367       return;
4368     }
4369
4370   /* C5 - Some core registers left and there are no arguments already
4371      on the stack: split this argument between the remaining core
4372      registers and the stack.  */
4373   if (ncrn < NUM_ARG_REGS && pcum->can_split)
4374     {
4375       pcum->aapcs_reg = gen_rtx_REG (mode, ncrn);
4376       pcum->aapcs_next_ncrn = NUM_ARG_REGS;
4377       pcum->aapcs_partial = (NUM_ARG_REGS - ncrn) * UNITS_PER_WORD;
4378       return;
4379     }
4380
4381   /* C6 - NCRN is set to 4.  */
4382   pcum->aapcs_next_ncrn = NUM_ARG_REGS;
4383
4384   /* C7,C8 - arugment goes on the stack.  We have nothing to do here.  */
4385   return;
4386 }
4387
4388 /* Initialize a variable CUM of type CUMULATIVE_ARGS
4389    for a call to a function whose data type is FNTYPE.
4390    For a library call, FNTYPE is NULL.  */
4391 void
4392 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
4393                           rtx libname,
4394                           tree fndecl ATTRIBUTE_UNUSED)
4395 {
4396   /* Long call handling.  */
4397   if (fntype)
4398     pcum->pcs_variant = arm_get_pcs_model (fntype, fndecl);
4399   else
4400     pcum->pcs_variant = arm_pcs_default;
4401
4402   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4403     {
4404       if (arm_libcall_uses_aapcs_base (libname))
4405         pcum->pcs_variant = ARM_PCS_AAPCS;
4406  
4407       pcum->aapcs_ncrn = pcum->aapcs_next_ncrn = 0;
4408       pcum->aapcs_reg = NULL_RTX;
4409       pcum->aapcs_partial = 0;
4410       pcum->aapcs_arg_processed = false;
4411       pcum->aapcs_cprc_slot = -1;
4412       pcum->can_split = true;
4413
4414       if (pcum->pcs_variant != ARM_PCS_AAPCS)
4415         {
4416           int i;
4417
4418           for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4419             {
4420               pcum->aapcs_cprc_failed[i] = false;
4421               aapcs_cp_arg_layout[i].cum_init (pcum, fntype, libname, fndecl);
4422             }
4423         }
4424       return;
4425     }
4426
4427   /* Legacy ABIs */
4428
4429   /* On the ARM, the offset starts at 0.  */
4430   pcum->nregs = 0;
4431   pcum->iwmmxt_nregs = 0;
4432   pcum->can_split = true;
4433
4434   /* Varargs vectors are treated the same as long long.
4435      named_count avoids having to change the way arm handles 'named' */
4436   pcum->named_count = 0;
4437   pcum->nargs = 0;
4438
4439   if (TARGET_REALLY_IWMMXT && fntype)
4440     {
4441       tree fn_arg;
4442
4443       for (fn_arg = TYPE_ARG_TYPES (fntype);
4444            fn_arg;
4445            fn_arg = TREE_CHAIN (fn_arg))
4446         pcum->named_count += 1;
4447
4448       if (! pcum->named_count)
4449         pcum->named_count = INT_MAX;
4450     }
4451 }
4452
4453
4454 /* Return true if mode/type need doubleword alignment.  */
4455 bool
4456 arm_needs_doubleword_align (enum machine_mode mode, tree type)
4457 {
4458   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
4459           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
4460 }
4461
4462
4463 /* Determine where to put an argument to a function.
4464    Value is zero to push the argument on the stack,
4465    or a hard register in which to store the argument.
4466
4467    MODE is the argument's machine mode.
4468    TYPE is the data type of the argument (as a tree).
4469     This is null for libcalls where that information may
4470     not be available.
4471    CUM is a variable of type CUMULATIVE_ARGS which gives info about
4472     the preceding args and about the function being called.
4473    NAMED is nonzero if this argument is a named parameter
4474     (otherwise it is an extra parameter matching an ellipsis).  */
4475
4476 rtx
4477 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4478                   tree type, int named)
4479 {
4480   int nregs;
4481
4482   /* Handle the special case quickly.  Pick an arbitrary value for op2 of
4483      a call insn (op3 of a call_value insn).  */
4484   if (mode == VOIDmode)
4485     return const0_rtx;
4486
4487   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4488     {
4489       aapcs_layout_arg (pcum, mode, type, named);
4490       return pcum->aapcs_reg;
4491     }
4492
4493   /* Varargs vectors are treated the same as long long.
4494      named_count avoids having to change the way arm handles 'named' */
4495   if (TARGET_IWMMXT_ABI
4496       && arm_vector_mode_supported_p (mode)
4497       && pcum->named_count > pcum->nargs + 1)
4498     {
4499       if (pcum->iwmmxt_nregs <= 9)
4500         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
4501       else
4502         {
4503           pcum->can_split = false;
4504           return NULL_RTX;
4505         }
4506     }
4507
4508   /* Put doubleword aligned quantities in even register pairs.  */
4509   if (pcum->nregs & 1
4510       && ARM_DOUBLEWORD_ALIGN
4511       && arm_needs_doubleword_align (mode, type))
4512     pcum->nregs++;
4513
4514   /* Only allow splitting an arg between regs and memory if all preceding
4515      args were allocated to regs.  For args passed by reference we only count
4516      the reference pointer.  */
4517   if (pcum->can_split)
4518     nregs = 1;
4519   else
4520     nregs = ARM_NUM_REGS2 (mode, type);
4521
4522   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
4523     return NULL_RTX;
4524
4525   return gen_rtx_REG (mode, pcum->nregs);
4526 }
4527
4528 static int
4529 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4530                        tree type, bool named)
4531 {
4532   int nregs = pcum->nregs;
4533
4534   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4535     {
4536       aapcs_layout_arg (pcum, mode, type, named);
4537       return pcum->aapcs_partial;
4538     }
4539
4540   if (TARGET_IWMMXT_ABI && arm_vector_mode_supported_p (mode))
4541     return 0;
4542
4543   if (NUM_ARG_REGS > nregs
4544       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
4545       && pcum->can_split)
4546     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
4547
4548   return 0;
4549 }
4550
4551 void
4552 arm_function_arg_advance (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4553                           tree type, bool named)
4554 {
4555   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4556     {
4557       aapcs_layout_arg (pcum, mode, type, named);
4558
4559       if (pcum->aapcs_cprc_slot >= 0)
4560         {
4561           aapcs_cp_arg_layout[pcum->aapcs_cprc_slot].advance (pcum, mode,
4562                                                               type);
4563           pcum->aapcs_cprc_slot = -1;
4564         }
4565
4566       /* Generic stuff.  */
4567       pcum->aapcs_arg_processed = false;
4568       pcum->aapcs_ncrn = pcum->aapcs_next_ncrn;
4569       pcum->aapcs_reg = NULL_RTX;
4570       pcum->aapcs_partial = 0;
4571     }
4572   else
4573     {
4574       pcum->nargs += 1;
4575       if (arm_vector_mode_supported_p (mode)
4576           && pcum->named_count > pcum->nargs
4577           && TARGET_IWMMXT_ABI)
4578         pcum->iwmmxt_nregs += 1;
4579       else
4580         pcum->nregs += ARM_NUM_REGS2 (mode, type);
4581     }
4582 }
4583
4584 /* Variable sized types are passed by reference.  This is a GCC
4585    extension to the ARM ABI.  */
4586
4587 static bool
4588 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
4589                        enum machine_mode mode ATTRIBUTE_UNUSED,
4590                        const_tree type, bool named ATTRIBUTE_UNUSED)
4591 {
4592   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
4593 }
4594 \f
4595 /* Encode the current state of the #pragma [no_]long_calls.  */
4596 typedef enum
4597 {
4598   OFF,          /* No #pragma [no_]long_calls is in effect.  */
4599   LONG,         /* #pragma long_calls is in effect.  */
4600   SHORT         /* #pragma no_long_calls is in effect.  */
4601 } arm_pragma_enum;
4602
4603 static arm_pragma_enum arm_pragma_long_calls = OFF;
4604
4605 void
4606 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
4607 {
4608   arm_pragma_long_calls = LONG;
4609 }
4610
4611 void
4612 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
4613 {
4614   arm_pragma_long_calls = SHORT;
4615 }
4616
4617 void
4618 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
4619 {
4620   arm_pragma_long_calls = OFF;
4621 }
4622 \f
4623 /* Handle an attribute requiring a FUNCTION_DECL;
4624    arguments as in struct attribute_spec.handler.  */
4625 static tree
4626 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
4627                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4628 {
4629   if (TREE_CODE (*node) != FUNCTION_DECL)
4630     {
4631       warning (OPT_Wattributes, "%qE attribute only applies to functions",
4632                name);
4633       *no_add_attrs = true;
4634     }
4635
4636   return NULL_TREE;
4637 }
4638
4639 /* Handle an "interrupt" or "isr" attribute;
4640    arguments as in struct attribute_spec.handler.  */
4641 static tree
4642 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
4643                           bool *no_add_attrs)
4644 {
4645   if (DECL_P (*node))
4646     {
4647       if (TREE_CODE (*node) != FUNCTION_DECL)
4648         {
4649           warning (OPT_Wattributes, "%qE attribute only applies to functions",
4650                    name);
4651           *no_add_attrs = true;
4652         }
4653       /* FIXME: the argument if any is checked for type attributes;
4654          should it be checked for decl ones?  */
4655     }
4656   else
4657     {
4658       if (TREE_CODE (*node) == FUNCTION_TYPE
4659           || TREE_CODE (*node) == METHOD_TYPE)
4660         {
4661           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
4662             {
4663               warning (OPT_Wattributes, "%qE attribute ignored",
4664                        name);
4665               *no_add_attrs = true;
4666             }
4667         }
4668       else if (TREE_CODE (*node) == POINTER_TYPE
4669                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
4670                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
4671                && arm_isr_value (args) != ARM_FT_UNKNOWN)
4672         {
4673           *node = build_variant_type_copy (*node);
4674           TREE_TYPE (*node) = build_type_attribute_variant
4675             (TREE_TYPE (*node),
4676              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
4677           *no_add_attrs = true;
4678         }
4679       else
4680         {
4681           /* Possibly pass this attribute on from the type to a decl.  */
4682           if (flags & ((int) ATTR_FLAG_DECL_NEXT
4683                        | (int) ATTR_FLAG_FUNCTION_NEXT
4684                        | (int) ATTR_FLAG_ARRAY_NEXT))
4685             {
4686               *no_add_attrs = true;
4687               return tree_cons (name, args, NULL_TREE);
4688             }
4689           else
4690             {
4691               warning (OPT_Wattributes, "%qE attribute ignored",
4692                        name);
4693             }
4694         }
4695     }
4696
4697   return NULL_TREE;
4698 }
4699
4700 /* Handle a "pcs" attribute; arguments as in struct
4701    attribute_spec.handler.  */
4702 static tree
4703 arm_handle_pcs_attribute (tree *node ATTRIBUTE_UNUSED, tree name, tree args,
4704                           int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4705 {
4706   if (arm_pcs_from_attribute (args) == ARM_PCS_UNKNOWN)
4707     {
4708       warning (OPT_Wattributes, "%qE attribute ignored", name);
4709       *no_add_attrs = true;
4710     }
4711   return NULL_TREE;
4712 }
4713
4714 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
4715 /* Handle the "notshared" attribute.  This attribute is another way of
4716    requesting hidden visibility.  ARM's compiler supports
4717    "__declspec(notshared)"; we support the same thing via an
4718    attribute.  */
4719
4720 static tree
4721 arm_handle_notshared_attribute (tree *node,
4722                                 tree name ATTRIBUTE_UNUSED,
4723                                 tree args ATTRIBUTE_UNUSED,
4724                                 int flags ATTRIBUTE_UNUSED,
4725                                 bool *no_add_attrs)
4726 {
4727   tree decl = TYPE_NAME (*node);
4728
4729   if (decl)
4730     {
4731       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
4732       DECL_VISIBILITY_SPECIFIED (decl) = 1;
4733       *no_add_attrs = false;
4734     }
4735   return NULL_TREE;
4736 }
4737 #endif
4738
4739 /* Return 0 if the attributes for two types are incompatible, 1 if they
4740    are compatible, and 2 if they are nearly compatible (which causes a
4741    warning to be generated).  */
4742 static int
4743 arm_comp_type_attributes (const_tree type1, const_tree type2)
4744 {
4745   int l1, l2, s1, s2;
4746
4747   /* Check for mismatch of non-default calling convention.  */
4748   if (TREE_CODE (type1) != FUNCTION_TYPE)
4749     return 1;
4750
4751   /* Check for mismatched call attributes.  */
4752   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
4753   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
4754   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
4755   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
4756
4757   /* Only bother to check if an attribute is defined.  */
4758   if (l1 | l2 | s1 | s2)
4759     {
4760       /* If one type has an attribute, the other must have the same attribute.  */
4761       if ((l1 != l2) || (s1 != s2))
4762         return 0;
4763
4764       /* Disallow mixed attributes.  */
4765       if ((l1 & s2) || (l2 & s1))
4766         return 0;
4767     }
4768
4769   /* Check for mismatched ISR attribute.  */
4770   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
4771   if (! l1)
4772     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
4773   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
4774   if (! l2)
4775     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
4776   if (l1 != l2)
4777     return 0;
4778
4779   return 1;
4780 }
4781
4782 /*  Assigns default attributes to newly defined type.  This is used to
4783     set short_call/long_call attributes for function types of
4784     functions defined inside corresponding #pragma scopes.  */
4785 static void
4786 arm_set_default_type_attributes (tree type)
4787 {
4788   /* Add __attribute__ ((long_call)) to all functions, when
4789      inside #pragma long_calls or __attribute__ ((short_call)),
4790      when inside #pragma no_long_calls.  */
4791   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
4792     {
4793       tree type_attr_list, attr_name;
4794       type_attr_list = TYPE_ATTRIBUTES (type);
4795
4796       if (arm_pragma_long_calls == LONG)
4797         attr_name = get_identifier ("long_call");
4798       else if (arm_pragma_long_calls == SHORT)
4799         attr_name = get_identifier ("short_call");
4800       else
4801         return;
4802
4803       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
4804       TYPE_ATTRIBUTES (type) = type_attr_list;
4805     }
4806 }
4807 \f
4808 /* Return true if DECL is known to be linked into section SECTION.  */
4809
4810 static bool
4811 arm_function_in_section_p (tree decl, section *section)
4812 {
4813   /* We can only be certain about functions defined in the same
4814      compilation unit.  */
4815   if (!TREE_STATIC (decl))
4816     return false;
4817
4818   /* Make sure that SYMBOL always binds to the definition in this
4819      compilation unit.  */
4820   if (!targetm.binds_local_p (decl))
4821     return false;
4822
4823   /* If DECL_SECTION_NAME is set, assume it is trustworthy.  */
4824   if (!DECL_SECTION_NAME (decl))
4825     {
4826       /* Make sure that we will not create a unique section for DECL.  */
4827       if (flag_function_sections || DECL_ONE_ONLY (decl))
4828         return false;
4829     }
4830
4831   return function_section (decl) == section;
4832 }
4833
4834 /* Return nonzero if a 32-bit "long_call" should be generated for
4835    a call from the current function to DECL.  We generate a long_call
4836    if the function:
4837
4838         a.  has an __attribute__((long call))
4839      or b.  is within the scope of a #pragma long_calls
4840      or c.  the -mlong-calls command line switch has been specified
4841
4842    However we do not generate a long call if the function:
4843
4844         d.  has an __attribute__ ((short_call))
4845      or e.  is inside the scope of a #pragma no_long_calls
4846      or f.  is defined in the same section as the current function.  */
4847
4848 bool
4849 arm_is_long_call_p (tree decl)
4850 {
4851   tree attrs;
4852
4853   if (!decl)
4854     return TARGET_LONG_CALLS;
4855
4856   attrs = TYPE_ATTRIBUTES (TREE_TYPE (decl));
4857   if (lookup_attribute ("short_call", attrs))
4858     return false;
4859
4860   /* For "f", be conservative, and only cater for cases in which the
4861      whole of the current function is placed in the same section.  */
4862   if (!flag_reorder_blocks_and_partition
4863       && TREE_CODE (decl) == FUNCTION_DECL
4864       && arm_function_in_section_p (decl, current_function_section ()))
4865     return false;
4866
4867   if (lookup_attribute ("long_call", attrs))
4868     return true;
4869
4870   return TARGET_LONG_CALLS;
4871 }
4872
4873 /* Return nonzero if it is ok to make a tail-call to DECL.  */
4874 static bool
4875 arm_function_ok_for_sibcall (tree decl, tree exp)
4876 {
4877   unsigned long func_type;
4878
4879   if (cfun->machine->sibcall_blocked)
4880     return false;
4881
4882   /* Never tailcall something for which we have no decl, or if we
4883      are generating code for Thumb-1.  */
4884   if (decl == NULL || TARGET_THUMB1)
4885     return false;
4886
4887   /* The PIC register is live on entry to VxWorks PLT entries, so we
4888      must make the call before restoring the PIC register.  */
4889   if (TARGET_VXWORKS_RTP && flag_pic && !targetm.binds_local_p (decl))
4890     return false;
4891
4892   /* Cannot tail-call to long calls, since these are out of range of
4893      a branch instruction.  */
4894   if (arm_is_long_call_p (decl))
4895     return false;
4896
4897   /* If we are interworking and the function is not declared static
4898      then we can't tail-call it unless we know that it exists in this
4899      compilation unit (since it might be a Thumb routine).  */
4900   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
4901     return false;
4902
4903   func_type = arm_current_func_type ();
4904   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
4905   if (IS_INTERRUPT (func_type))
4906     return false;
4907
4908   if (!VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
4909     {
4910       /* Check that the return value locations are the same.  For
4911          example that we aren't returning a value from the sibling in
4912          a VFP register but then need to transfer it to a core
4913          register.  */
4914       rtx a, b;
4915
4916       a = arm_function_value (TREE_TYPE (exp), decl, false);
4917       b = arm_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
4918                               cfun->decl, false);
4919       if (!rtx_equal_p (a, b))
4920         return false;
4921     }
4922
4923   /* Never tailcall if function may be called with a misaligned SP.  */
4924   if (IS_STACKALIGN (func_type))
4925     return false;
4926
4927   /* Everything else is ok.  */
4928   return true;
4929 }
4930
4931 \f
4932 /* Addressing mode support functions.  */
4933
4934 /* Return nonzero if X is a legitimate immediate operand when compiling
4935    for PIC.  We know that X satisfies CONSTANT_P and flag_pic is true.  */
4936 int
4937 legitimate_pic_operand_p (rtx x)
4938 {
4939   if (GET_CODE (x) == SYMBOL_REF
4940       || (GET_CODE (x) == CONST
4941           && GET_CODE (XEXP (x, 0)) == PLUS
4942           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
4943     return 0;
4944
4945   return 1;
4946 }
4947
4948 /* Record that the current function needs a PIC register.  Initialize
4949    cfun->machine->pic_reg if we have not already done so.  */
4950
4951 static void
4952 require_pic_register (void)
4953 {
4954   /* A lot of the logic here is made obscure by the fact that this
4955      routine gets called as part of the rtx cost estimation process.
4956      We don't want those calls to affect any assumptions about the real
4957      function; and further, we can't call entry_of_function() until we
4958      start the real expansion process.  */
4959   if (!crtl->uses_pic_offset_table)
4960     {
4961       gcc_assert (can_create_pseudo_p ());
4962       if (arm_pic_register != INVALID_REGNUM)
4963         {
4964           if (!cfun->machine->pic_reg)
4965             cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
4966
4967           /* Play games to avoid marking the function as needing pic
4968              if we are being called as part of the cost-estimation
4969              process.  */
4970           if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
4971             crtl->uses_pic_offset_table = 1;
4972         }
4973       else
4974         {
4975           rtx seq;
4976
4977           if (!cfun->machine->pic_reg)
4978             cfun->machine->pic_reg = gen_reg_rtx (Pmode);
4979
4980           /* Play games to avoid marking the function as needing pic
4981              if we are being called as part of the cost-estimation
4982              process.  */
4983           if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
4984             {
4985               crtl->uses_pic_offset_table = 1;
4986               start_sequence ();
4987
4988               arm_load_pic_register (0UL);
4989
4990               seq = get_insns ();
4991               end_sequence ();
4992               /* We can be called during expansion of PHI nodes, where
4993                  we can't yet emit instructions directly in the final
4994                  insn stream.  Queue the insns on the entry edge, they will
4995                  be committed after everything else is expanded.  */
4996               insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
4997             }
4998         }
4999     }
5000 }
5001
5002 rtx
5003 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
5004 {
5005   if (GET_CODE (orig) == SYMBOL_REF
5006       || GET_CODE (orig) == LABEL_REF)
5007     {
5008       rtx insn;
5009
5010       if (reg == 0)
5011         {
5012           gcc_assert (can_create_pseudo_p ());
5013           reg = gen_reg_rtx (Pmode);
5014         }
5015
5016       /* VxWorks does not impose a fixed gap between segments; the run-time
5017          gap can be different from the object-file gap.  We therefore can't
5018          use GOTOFF unless we are absolutely sure that the symbol is in the
5019          same segment as the GOT.  Unfortunately, the flexibility of linker
5020          scripts means that we can't be sure of that in general, so assume
5021          that GOTOFF is never valid on VxWorks.  */
5022       if ((GET_CODE (orig) == LABEL_REF
5023            || (GET_CODE (orig) == SYMBOL_REF &&
5024                SYMBOL_REF_LOCAL_P (orig)))
5025           && NEED_GOT_RELOC
5026           && !TARGET_VXWORKS_RTP)
5027         insn = arm_pic_static_addr (orig, reg);
5028       else
5029         {
5030           rtx pat;
5031           rtx mem;
5032
5033           /* If this function doesn't have a pic register, create one now.  */
5034           require_pic_register ();
5035
5036           pat = gen_calculate_pic_address (reg, cfun->machine->pic_reg, orig);
5037
5038           /* Make the MEM as close to a constant as possible.  */
5039           mem = SET_SRC (pat);
5040           gcc_assert (MEM_P (mem) && !MEM_VOLATILE_P (mem));
5041           MEM_READONLY_P (mem) = 1;
5042           MEM_NOTRAP_P (mem) = 1;
5043
5044           insn = emit_insn (pat);
5045         }
5046
5047       /* Put a REG_EQUAL note on this insn, so that it can be optimized
5048          by loop.  */
5049       set_unique_reg_note (insn, REG_EQUAL, orig);
5050
5051       return reg;
5052     }
5053   else if (GET_CODE (orig) == CONST)
5054     {
5055       rtx base, offset;
5056
5057       if (GET_CODE (XEXP (orig, 0)) == PLUS
5058           && XEXP (XEXP (orig, 0), 0) == cfun->machine->pic_reg)
5059         return orig;
5060
5061       /* Handle the case where we have: const (UNSPEC_TLS).  */
5062       if (GET_CODE (XEXP (orig, 0)) == UNSPEC
5063           && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
5064         return orig;
5065
5066       /* Handle the case where we have:
5067          const (plus (UNSPEC_TLS) (ADDEND)).  The ADDEND must be a
5068          CONST_INT.  */
5069       if (GET_CODE (XEXP (orig, 0)) == PLUS
5070           && GET_CODE (XEXP (XEXP (orig, 0), 0)) == UNSPEC
5071           && XINT (XEXP (XEXP (orig, 0), 0), 1) == UNSPEC_TLS)
5072         {
5073           gcc_assert (GET_CODE (XEXP (XEXP (orig, 0), 1)) == CONST_INT);
5074           return orig;
5075         }
5076
5077       if (reg == 0)
5078         {
5079           gcc_assert (can_create_pseudo_p ());
5080           reg = gen_reg_rtx (Pmode);
5081         }
5082
5083       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
5084
5085       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
5086       offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
5087                                        base == reg ? 0 : reg);
5088
5089       if (GET_CODE (offset) == CONST_INT)
5090         {
5091           /* The base register doesn't really matter, we only want to
5092              test the index for the appropriate mode.  */
5093           if (!arm_legitimate_index_p (mode, offset, SET, 0))
5094             {
5095               gcc_assert (can_create_pseudo_p ());
5096               offset = force_reg (Pmode, offset);
5097             }
5098
5099           if (GET_CODE (offset) == CONST_INT)
5100             return plus_constant (base, INTVAL (offset));
5101         }
5102
5103       if (GET_MODE_SIZE (mode) > 4
5104           && (GET_MODE_CLASS (mode) == MODE_INT
5105               || TARGET_SOFT_FLOAT))
5106         {
5107           emit_insn (gen_addsi3 (reg, base, offset));
5108           return reg;
5109         }
5110
5111       return gen_rtx_PLUS (Pmode, base, offset);
5112     }
5113
5114   return orig;
5115 }
5116
5117
5118 /* Find a spare register to use during the prolog of a function.  */
5119
5120 static int
5121 thumb_find_work_register (unsigned long pushed_regs_mask)
5122 {
5123   int reg;
5124
5125   /* Check the argument registers first as these are call-used.  The
5126      register allocation order means that sometimes r3 might be used
5127      but earlier argument registers might not, so check them all.  */
5128   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
5129     if (!df_regs_ever_live_p (reg))
5130       return reg;
5131
5132   /* Before going on to check the call-saved registers we can try a couple
5133      more ways of deducing that r3 is available.  The first is when we are
5134      pushing anonymous arguments onto the stack and we have less than 4
5135      registers worth of fixed arguments(*).  In this case r3 will be part of
5136      the variable argument list and so we can be sure that it will be
5137      pushed right at the start of the function.  Hence it will be available
5138      for the rest of the prologue.
5139      (*): ie crtl->args.pretend_args_size is greater than 0.  */
5140   if (cfun->machine->uses_anonymous_args
5141       && crtl->args.pretend_args_size > 0)
5142     return LAST_ARG_REGNUM;
5143
5144   /* The other case is when we have fixed arguments but less than 4 registers
5145      worth.  In this case r3 might be used in the body of the function, but
5146      it is not being used to convey an argument into the function.  In theory
5147      we could just check crtl->args.size to see how many bytes are
5148      being passed in argument registers, but it seems that it is unreliable.
5149      Sometimes it will have the value 0 when in fact arguments are being
5150      passed.  (See testcase execute/20021111-1.c for an example).  So we also
5151      check the args_info.nregs field as well.  The problem with this field is
5152      that it makes no allowances for arguments that are passed to the
5153      function but which are not used.  Hence we could miss an opportunity
5154      when a function has an unused argument in r3.  But it is better to be
5155      safe than to be sorry.  */
5156   if (! cfun->machine->uses_anonymous_args
5157       && crtl->args.size >= 0
5158       && crtl->args.size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
5159       && crtl->args.info.nregs < 4)
5160     return LAST_ARG_REGNUM;
5161
5162   /* Otherwise look for a call-saved register that is going to be pushed.  */
5163   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
5164     if (pushed_regs_mask & (1 << reg))
5165       return reg;
5166
5167   if (TARGET_THUMB2)
5168     {
5169       /* Thumb-2 can use high regs.  */
5170       for (reg = FIRST_HI_REGNUM; reg < 15; reg ++)
5171         if (pushed_regs_mask & (1 << reg))
5172           return reg;
5173     }
5174   /* Something went wrong - thumb_compute_save_reg_mask()
5175      should have arranged for a suitable register to be pushed.  */
5176   gcc_unreachable ();
5177 }
5178
5179 static GTY(()) int pic_labelno;
5180
5181 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
5182    low register.  */
5183
5184 void
5185 arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
5186 {
5187   rtx l1, labelno, pic_tmp, pic_rtx, pic_reg;
5188
5189   if (crtl->uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
5190     return;
5191
5192   gcc_assert (flag_pic);
5193
5194   pic_reg = cfun->machine->pic_reg;
5195   if (TARGET_VXWORKS_RTP)
5196     {
5197       pic_rtx = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE);
5198       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
5199       emit_insn (gen_pic_load_addr_32bit (pic_reg, pic_rtx));
5200
5201       emit_insn (gen_rtx_SET (Pmode, pic_reg, gen_rtx_MEM (Pmode, pic_reg)));
5202
5203       pic_tmp = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
5204       emit_insn (gen_pic_offset_arm (pic_reg, pic_reg, pic_tmp));
5205     }
5206   else
5207     {
5208       /* We use an UNSPEC rather than a LABEL_REF because this label
5209          never appears in the code stream.  */
5210
5211       labelno = GEN_INT (pic_labelno++);
5212       l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
5213       l1 = gen_rtx_CONST (VOIDmode, l1);
5214
5215       /* On the ARM the PC register contains 'dot + 8' at the time of the
5216          addition, on the Thumb it is 'dot + 4'.  */
5217       pic_rtx = plus_constant (l1, TARGET_ARM ? 8 : 4);
5218       pic_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, pic_rtx),
5219                                 UNSPEC_GOTSYM_OFF);
5220       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
5221
5222       if (TARGET_32BIT)
5223         {
5224           emit_insn (gen_pic_load_addr_32bit (pic_reg, pic_rtx));
5225           if (TARGET_ARM)
5226             emit_insn (gen_pic_add_dot_plus_eight (pic_reg, pic_reg, labelno));
5227           else
5228             emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
5229         }
5230       else /* TARGET_THUMB1 */
5231         {
5232           if (arm_pic_register != INVALID_REGNUM
5233               && REGNO (pic_reg) > LAST_LO_REGNUM)
5234             {
5235               /* We will have pushed the pic register, so we should always be
5236                  able to find a work register.  */
5237               pic_tmp = gen_rtx_REG (SImode,
5238                                      thumb_find_work_register (saved_regs));
5239               emit_insn (gen_pic_load_addr_thumb1 (pic_tmp, pic_rtx));
5240               emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
5241             }
5242           else
5243             emit_insn (gen_pic_load_addr_thumb1 (pic_reg, pic_rtx));
5244           emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
5245         }
5246     }
5247
5248   /* Need to emit this whether or not we obey regdecls,
5249      since setjmp/longjmp can cause life info to screw up.  */
5250   emit_use (pic_reg);
5251 }
5252
5253 /* Generate code to load the address of a static var when flag_pic is set.  */
5254 static rtx
5255 arm_pic_static_addr (rtx orig, rtx reg)
5256 {
5257   rtx l1, labelno, offset_rtx, insn;
5258
5259   gcc_assert (flag_pic);
5260
5261   /* We use an UNSPEC rather than a LABEL_REF because this label
5262      never appears in the code stream.  */
5263   labelno = GEN_INT (pic_labelno++);
5264   l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
5265   l1 = gen_rtx_CONST (VOIDmode, l1);
5266
5267   /* On the ARM the PC register contains 'dot + 8' at the time of the
5268      addition, on the Thumb it is 'dot + 4'.  */
5269   offset_rtx = plus_constant (l1, TARGET_ARM ? 8 : 4);
5270   offset_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, orig, offset_rtx),
5271                                UNSPEC_SYMBOL_OFFSET);
5272   offset_rtx = gen_rtx_CONST (Pmode, offset_rtx);
5273
5274   if (TARGET_32BIT)
5275     {
5276       emit_insn (gen_pic_load_addr_32bit (reg, offset_rtx));
5277       if (TARGET_ARM)
5278         insn = emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
5279       else
5280         insn = emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5281     }
5282   else /* TARGET_THUMB1 */
5283     {
5284       emit_insn (gen_pic_load_addr_thumb1 (reg, offset_rtx));
5285       insn = emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5286     }
5287
5288   return insn;
5289 }
5290
5291 /* Return nonzero if X is valid as an ARM state addressing register.  */
5292 static int
5293 arm_address_register_rtx_p (rtx x, int strict_p)
5294 {
5295   int regno;
5296
5297   if (GET_CODE (x) != REG)
5298     return 0;
5299
5300   regno = REGNO (x);
5301
5302   if (strict_p)
5303     return ARM_REGNO_OK_FOR_BASE_P (regno);
5304
5305   return (regno <= LAST_ARM_REGNUM
5306           || regno >= FIRST_PSEUDO_REGISTER
5307           || regno == FRAME_POINTER_REGNUM
5308           || regno == ARG_POINTER_REGNUM);
5309 }
5310
5311 /* Return TRUE if this rtx is the difference of a symbol and a label,
5312    and will reduce to a PC-relative relocation in the object file.
5313    Expressions like this can be left alone when generating PIC, rather
5314    than forced through the GOT.  */
5315 static int
5316 pcrel_constant_p (rtx x)
5317 {
5318   if (GET_CODE (x) == MINUS)
5319     return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
5320
5321   return FALSE;
5322 }
5323
5324 /* Return true if X will surely end up in an index register after next
5325    splitting pass.  */
5326 static bool
5327 will_be_in_index_register (const_rtx x)
5328 {
5329   /* arm.md: calculate_pic_address will split this into a register.  */
5330   return GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_SYM;
5331 }
5332
5333 /* Return nonzero if X is a valid ARM state address operand.  */
5334 int
5335 arm_legitimate_address_outer_p (enum machine_mode mode, rtx x, RTX_CODE outer,
5336                                 int strict_p)
5337 {
5338   bool use_ldrd;
5339   enum rtx_code code = GET_CODE (x);
5340
5341   if (arm_address_register_rtx_p (x, strict_p))
5342     return 1;
5343
5344   use_ldrd = (TARGET_LDRD
5345               && (mode == DImode
5346                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
5347
5348   if (code == POST_INC || code == PRE_DEC
5349       || ((code == PRE_INC || code == POST_DEC)
5350           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
5351     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
5352
5353   else if ((code == POST_MODIFY || code == PRE_MODIFY)
5354            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
5355            && GET_CODE (XEXP (x, 1)) == PLUS
5356            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5357     {
5358       rtx addend = XEXP (XEXP (x, 1), 1);
5359
5360       /* Don't allow ldrd post increment by register because it's hard
5361          to fixup invalid register choices.  */
5362       if (use_ldrd
5363           && GET_CODE (x) == POST_MODIFY
5364           && GET_CODE (addend) == REG)
5365         return 0;
5366
5367       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
5368               && arm_legitimate_index_p (mode, addend, outer, strict_p));
5369     }
5370
5371   /* After reload constants split into minipools will have addresses
5372      from a LABEL_REF.  */
5373   else if (reload_completed
5374            && (code == LABEL_REF
5375                || (code == CONST
5376                    && GET_CODE (XEXP (x, 0)) == PLUS
5377                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5378                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5379     return 1;
5380
5381   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
5382     return 0;
5383
5384   else if (code == PLUS)
5385     {
5386       rtx xop0 = XEXP (x, 0);
5387       rtx xop1 = XEXP (x, 1);
5388
5389       return ((arm_address_register_rtx_p (xop0, strict_p)
5390                && ((GET_CODE(xop1) == CONST_INT
5391                     && arm_legitimate_index_p (mode, xop1, outer, strict_p))
5392                    || (!strict_p && will_be_in_index_register (xop1))))
5393               || (arm_address_register_rtx_p (xop1, strict_p)
5394                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
5395     }
5396
5397 #if 0
5398   /* Reload currently can't handle MINUS, so disable this for now */
5399   else if (GET_CODE (x) == MINUS)
5400     {
5401       rtx xop0 = XEXP (x, 0);
5402       rtx xop1 = XEXP (x, 1);
5403
5404       return (arm_address_register_rtx_p (xop0, strict_p)
5405               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
5406     }
5407 #endif
5408
5409   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
5410            && code == SYMBOL_REF
5411            && CONSTANT_POOL_ADDRESS_P (x)
5412            && ! (flag_pic
5413                  && symbol_mentioned_p (get_pool_constant (x))
5414                  && ! pcrel_constant_p (get_pool_constant (x))))
5415     return 1;
5416
5417   return 0;
5418 }
5419
5420 /* Return nonzero if X is a valid Thumb-2 address operand.  */
5421 static int
5422 thumb2_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
5423 {
5424   bool use_ldrd;
5425   enum rtx_code code = GET_CODE (x);
5426   
5427   if (arm_address_register_rtx_p (x, strict_p))
5428     return 1;
5429
5430   use_ldrd = (TARGET_LDRD
5431               && (mode == DImode
5432                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
5433
5434   if (code == POST_INC || code == PRE_DEC
5435       || ((code == PRE_INC || code == POST_DEC)
5436           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
5437     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
5438
5439   else if ((code == POST_MODIFY || code == PRE_MODIFY)
5440            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
5441            && GET_CODE (XEXP (x, 1)) == PLUS
5442            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5443     {
5444       /* Thumb-2 only has autoincrement by constant.  */
5445       rtx addend = XEXP (XEXP (x, 1), 1);
5446       HOST_WIDE_INT offset;
5447
5448       if (GET_CODE (addend) != CONST_INT)
5449         return 0;
5450
5451       offset = INTVAL(addend);
5452       if (GET_MODE_SIZE (mode) <= 4)
5453         return (offset > -256 && offset < 256);
5454       
5455       return (use_ldrd && offset > -1024 && offset < 1024
5456               && (offset & 3) == 0);
5457     }
5458
5459   /* After reload constants split into minipools will have addresses
5460      from a LABEL_REF.  */
5461   else if (reload_completed
5462            && (code == LABEL_REF
5463                || (code == CONST
5464                    && GET_CODE (XEXP (x, 0)) == PLUS
5465                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5466                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5467     return 1;
5468
5469   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
5470     return 0;
5471
5472   else if (code == PLUS)
5473     {
5474       rtx xop0 = XEXP (x, 0);
5475       rtx xop1 = XEXP (x, 1);
5476
5477       return ((arm_address_register_rtx_p (xop0, strict_p)
5478                && (thumb2_legitimate_index_p (mode, xop1, strict_p)
5479                    || (!strict_p && will_be_in_index_register (xop1))))
5480               || (arm_address_register_rtx_p (xop1, strict_p)
5481                   && thumb2_legitimate_index_p (mode, xop0, strict_p)));
5482     }
5483
5484   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
5485            && code == SYMBOL_REF
5486            && CONSTANT_POOL_ADDRESS_P (x)
5487            && ! (flag_pic
5488                  && symbol_mentioned_p (get_pool_constant (x))
5489                  && ! pcrel_constant_p (get_pool_constant (x))))
5490     return 1;
5491
5492   return 0;
5493 }
5494
5495 /* Return nonzero if INDEX is valid for an address index operand in
5496    ARM state.  */
5497 static int
5498 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
5499                         int strict_p)
5500 {
5501   HOST_WIDE_INT range;
5502   enum rtx_code code = GET_CODE (index);
5503
5504   /* Standard coprocessor addressing modes.  */
5505   if (TARGET_HARD_FLOAT
5506       && (TARGET_FPA || TARGET_MAVERICK)
5507       && (GET_MODE_CLASS (mode) == MODE_FLOAT
5508           || (TARGET_MAVERICK && mode == DImode)))
5509     return (code == CONST_INT && INTVAL (index) < 1024
5510             && INTVAL (index) > -1024
5511             && (INTVAL (index) & 3) == 0);
5512
5513   if (TARGET_NEON
5514       && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode)))
5515     return (code == CONST_INT
5516             && INTVAL (index) < 1016
5517             && INTVAL (index) > -1024
5518             && (INTVAL (index) & 3) == 0);
5519
5520   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
5521     return (code == CONST_INT
5522             && INTVAL (index) < 1024
5523             && INTVAL (index) > -1024
5524             && (INTVAL (index) & 3) == 0);
5525
5526   if (arm_address_register_rtx_p (index, strict_p)
5527       && (GET_MODE_SIZE (mode) <= 4))
5528     return 1;
5529
5530   if (mode == DImode || mode == DFmode)
5531     {
5532       if (code == CONST_INT)
5533         {
5534           HOST_WIDE_INT val = INTVAL (index);
5535
5536           if (TARGET_LDRD)
5537             return val > -256 && val < 256;
5538           else
5539             return val > -4096 && val < 4092;
5540         }
5541
5542       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
5543     }
5544
5545   if (GET_MODE_SIZE (mode) <= 4
5546       && ! (arm_arch4
5547             && (mode == HImode
5548                 || mode == HFmode
5549                 || (mode == QImode && outer == SIGN_EXTEND))))
5550     {
5551       if (code == MULT)
5552         {
5553           rtx xiop0 = XEXP (index, 0);
5554           rtx xiop1 = XEXP (index, 1);
5555
5556           return ((arm_address_register_rtx_p (xiop0, strict_p)
5557                    && power_of_two_operand (xiop1, SImode))
5558                   || (arm_address_register_rtx_p (xiop1, strict_p)
5559                       && power_of_two_operand (xiop0, SImode)));
5560         }
5561       else if (code == LSHIFTRT || code == ASHIFTRT
5562                || code == ASHIFT || code == ROTATERT)
5563         {
5564           rtx op = XEXP (index, 1);
5565
5566           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
5567                   && GET_CODE (op) == CONST_INT
5568                   && INTVAL (op) > 0
5569                   && INTVAL (op) <= 31);
5570         }
5571     }
5572
5573   /* For ARM v4 we may be doing a sign-extend operation during the
5574      load.  */
5575   if (arm_arch4)
5576     {
5577       if (mode == HImode
5578           || mode == HFmode
5579           || (outer == SIGN_EXTEND && mode == QImode))
5580         range = 256;
5581       else
5582         range = 4096;
5583     }
5584   else
5585     range = (mode == HImode || mode == HFmode) ? 4095 : 4096;
5586
5587   return (code == CONST_INT
5588           && INTVAL (index) < range
5589           && INTVAL (index) > -range);
5590 }
5591
5592 /* Return true if OP is a valid index scaling factor for Thumb-2 address
5593    index operand.  i.e. 1, 2, 4 or 8.  */
5594 static bool
5595 thumb2_index_mul_operand (rtx op)
5596 {
5597   HOST_WIDE_INT val;
5598   
5599   if (GET_CODE(op) != CONST_INT)
5600     return false;
5601
5602   val = INTVAL(op);
5603   return (val == 1 || val == 2 || val == 4 || val == 8);
5604 }
5605   
5606 /* Return nonzero if INDEX is a valid Thumb-2 address index operand.  */
5607 static int
5608 thumb2_legitimate_index_p (enum machine_mode mode, rtx index, int strict_p)
5609 {
5610   enum rtx_code code = GET_CODE (index);
5611
5612   /* ??? Combine arm and thumb2 coprocessor addressing modes.  */
5613   /* Standard coprocessor addressing modes.  */
5614   if (TARGET_HARD_FLOAT
5615       && (TARGET_FPA || TARGET_MAVERICK)
5616       && (GET_MODE_CLASS (mode) == MODE_FLOAT
5617           || (TARGET_MAVERICK && mode == DImode)))
5618     return (code == CONST_INT && INTVAL (index) < 1024
5619             && INTVAL (index) > -1024
5620             && (INTVAL (index) & 3) == 0);
5621
5622   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
5623     {
5624       /* For DImode assume values will usually live in core regs
5625          and only allow LDRD addressing modes.  */
5626       if (!TARGET_LDRD || mode != DImode)
5627         return (code == CONST_INT
5628                 && INTVAL (index) < 1024
5629                 && INTVAL (index) > -1024
5630                 && (INTVAL (index) & 3) == 0);
5631     }
5632
5633   if (TARGET_NEON
5634       && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode)))
5635     return (code == CONST_INT
5636             && INTVAL (index) < 1016
5637             && INTVAL (index) > -1024
5638             && (INTVAL (index) & 3) == 0);
5639
5640   if (arm_address_register_rtx_p (index, strict_p)
5641       && (GET_MODE_SIZE (mode) <= 4))
5642     return 1;
5643
5644   if (mode == DImode || mode == DFmode)
5645     {
5646       if (code == CONST_INT)
5647         {
5648           HOST_WIDE_INT val = INTVAL (index);
5649           /* ??? Can we assume ldrd for thumb2?  */
5650           /* Thumb-2 ldrd only has reg+const addressing modes.  */
5651           /* ldrd supports offsets of +-1020.
5652              However the ldr fallback does not.  */
5653           return val > -256 && val < 256 && (val & 3) == 0;
5654         }
5655       else
5656         return 0;
5657     }
5658
5659   if (code == MULT)
5660     {
5661       rtx xiop0 = XEXP (index, 0);
5662       rtx xiop1 = XEXP (index, 1);
5663
5664       return ((arm_address_register_rtx_p (xiop0, strict_p)
5665                && thumb2_index_mul_operand (xiop1))
5666               || (arm_address_register_rtx_p (xiop1, strict_p)
5667                   && thumb2_index_mul_operand (xiop0)));
5668     }
5669   else if (code == ASHIFT)
5670     {
5671       rtx op = XEXP (index, 1);
5672
5673       return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
5674               && GET_CODE (op) == CONST_INT
5675               && INTVAL (op) > 0
5676               && INTVAL (op) <= 3);
5677     }
5678
5679   return (code == CONST_INT
5680           && INTVAL (index) < 4096
5681           && INTVAL (index) > -256);
5682 }
5683
5684 /* Return nonzero if X is valid as a 16-bit Thumb state base register.  */
5685 static int
5686 thumb1_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
5687 {
5688   int regno;
5689
5690   if (GET_CODE (x) != REG)
5691     return 0;
5692
5693   regno = REGNO (x);
5694
5695   if (strict_p)
5696     return THUMB1_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
5697
5698   return (regno <= LAST_LO_REGNUM
5699           || regno > LAST_VIRTUAL_REGISTER
5700           || regno == FRAME_POINTER_REGNUM
5701           || (GET_MODE_SIZE (mode) >= 4
5702               && (regno == STACK_POINTER_REGNUM
5703                   || regno >= FIRST_PSEUDO_REGISTER
5704                   || x == hard_frame_pointer_rtx
5705                   || x == arg_pointer_rtx)));
5706 }
5707
5708 /* Return nonzero if x is a legitimate index register.  This is the case
5709    for any base register that can access a QImode object.  */
5710 inline static int
5711 thumb1_index_register_rtx_p (rtx x, int strict_p)
5712 {
5713   return thumb1_base_register_rtx_p (x, QImode, strict_p);
5714 }
5715
5716 /* Return nonzero if x is a legitimate 16-bit Thumb-state address.
5717
5718    The AP may be eliminated to either the SP or the FP, so we use the
5719    least common denominator, e.g. SImode, and offsets from 0 to 64.
5720
5721    ??? Verify whether the above is the right approach.
5722
5723    ??? Also, the FP may be eliminated to the SP, so perhaps that
5724    needs special handling also.
5725
5726    ??? Look at how the mips16 port solves this problem.  It probably uses
5727    better ways to solve some of these problems.
5728
5729    Although it is not incorrect, we don't accept QImode and HImode
5730    addresses based on the frame pointer or arg pointer until the
5731    reload pass starts.  This is so that eliminating such addresses
5732    into stack based ones won't produce impossible code.  */
5733 static int
5734 thumb1_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
5735 {
5736   /* ??? Not clear if this is right.  Experiment.  */
5737   if (GET_MODE_SIZE (mode) < 4
5738       && !(reload_in_progress || reload_completed)
5739       && (reg_mentioned_p (frame_pointer_rtx, x)
5740           || reg_mentioned_p (arg_pointer_rtx, x)
5741           || reg_mentioned_p (virtual_incoming_args_rtx, x)
5742           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
5743           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
5744           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
5745     return 0;
5746
5747   /* Accept any base register.  SP only in SImode or larger.  */
5748   else if (thumb1_base_register_rtx_p (x, mode, strict_p))
5749     return 1;
5750
5751   /* This is PC relative data before arm_reorg runs.  */
5752   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
5753            && GET_CODE (x) == SYMBOL_REF
5754            && CONSTANT_POOL_ADDRESS_P (x) && !flag_pic)
5755     return 1;
5756
5757   /* This is PC relative data after arm_reorg runs.  */
5758   else if ((GET_MODE_SIZE (mode) >= 4 || mode == HFmode)
5759            && reload_completed
5760            && (GET_CODE (x) == LABEL_REF
5761                || (GET_CODE (x) == CONST
5762                    && GET_CODE (XEXP (x, 0)) == PLUS
5763                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5764                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5765     return 1;
5766
5767   /* Post-inc indexing only supported for SImode and larger.  */
5768   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
5769            && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p))
5770     return 1;
5771
5772   else if (GET_CODE (x) == PLUS)
5773     {
5774       /* REG+REG address can be any two index registers.  */
5775       /* We disallow FRAME+REG addressing since we know that FRAME
5776          will be replaced with STACK, and SP relative addressing only
5777          permits SP+OFFSET.  */
5778       if (GET_MODE_SIZE (mode) <= 4
5779           && XEXP (x, 0) != frame_pointer_rtx
5780           && XEXP (x, 1) != frame_pointer_rtx
5781           && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
5782           && (thumb1_index_register_rtx_p (XEXP (x, 1), strict_p)
5783               || (!strict_p && will_be_in_index_register (XEXP (x, 1)))))
5784         return 1;
5785
5786       /* REG+const has 5-7 bit offset for non-SP registers.  */
5787       else if ((thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
5788                 || XEXP (x, 0) == arg_pointer_rtx)
5789                && GET_CODE (XEXP (x, 1)) == CONST_INT
5790                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
5791         return 1;
5792
5793       /* REG+const has 10-bit offset for SP, but only SImode and
5794          larger is supported.  */
5795       /* ??? Should probably check for DI/DFmode overflow here
5796          just like GO_IF_LEGITIMATE_OFFSET does.  */
5797       else if (GET_CODE (XEXP (x, 0)) == REG
5798                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
5799                && GET_MODE_SIZE (mode) >= 4
5800                && GET_CODE (XEXP (x, 1)) == CONST_INT
5801                && INTVAL (XEXP (x, 1)) >= 0
5802                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
5803                && (INTVAL (XEXP (x, 1)) & 3) == 0)
5804         return 1;
5805
5806       else if (GET_CODE (XEXP (x, 0)) == REG
5807                && (REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
5808                    || REGNO (XEXP (x, 0)) == ARG_POINTER_REGNUM
5809                    || (REGNO (XEXP (x, 0)) >= FIRST_VIRTUAL_REGISTER
5810                        && REGNO (XEXP (x, 0)) <= LAST_VIRTUAL_REGISTER))
5811                && GET_MODE_SIZE (mode) >= 4
5812                && GET_CODE (XEXP (x, 1)) == CONST_INT
5813                && (INTVAL (XEXP (x, 1)) & 3) == 0)
5814         return 1;
5815     }
5816
5817   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
5818            && GET_MODE_SIZE (mode) == 4
5819            && GET_CODE (x) == SYMBOL_REF
5820            && CONSTANT_POOL_ADDRESS_P (x)
5821            && ! (flag_pic
5822                  && symbol_mentioned_p (get_pool_constant (x))
5823                  && ! pcrel_constant_p (get_pool_constant (x))))
5824     return 1;
5825
5826   return 0;
5827 }
5828
5829 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
5830    instruction of mode MODE.  */
5831 int
5832 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
5833 {
5834   switch (GET_MODE_SIZE (mode))
5835     {
5836     case 1:
5837       return val >= 0 && val < 32;
5838
5839     case 2:
5840       return val >= 0 && val < 64 && (val & 1) == 0;
5841
5842     default:
5843       return (val >= 0
5844               && (val + GET_MODE_SIZE (mode)) <= 128
5845               && (val & 3) == 0);
5846     }
5847 }
5848
5849 bool
5850 arm_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
5851 {
5852   if (TARGET_ARM)
5853     return arm_legitimate_address_outer_p (mode, x, SET, strict_p);
5854   else if (TARGET_THUMB2)
5855     return thumb2_legitimate_address_p (mode, x, strict_p);
5856   else /* if (TARGET_THUMB1) */
5857     return thumb1_legitimate_address_p (mode, x, strict_p);
5858 }
5859
5860 /* Build the SYMBOL_REF for __tls_get_addr.  */
5861
5862 static GTY(()) rtx tls_get_addr_libfunc;
5863
5864 static rtx
5865 get_tls_get_addr (void)
5866 {
5867   if (!tls_get_addr_libfunc)
5868     tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
5869   return tls_get_addr_libfunc;
5870 }
5871
5872 static rtx
5873 arm_load_tp (rtx target)
5874 {
5875   if (!target)
5876     target = gen_reg_rtx (SImode);
5877
5878   if (TARGET_HARD_TP)
5879     {
5880       /* Can return in any reg.  */
5881       emit_insn (gen_load_tp_hard (target));
5882     }
5883   else
5884     {
5885       /* Always returned in r0.  Immediately copy the result into a pseudo,
5886          otherwise other uses of r0 (e.g. setting up function arguments) may
5887          clobber the value.  */
5888
5889       rtx tmp;
5890
5891       emit_insn (gen_load_tp_soft ());
5892
5893       tmp = gen_rtx_REG (SImode, 0);
5894       emit_move_insn (target, tmp);
5895     }
5896   return target;
5897 }
5898
5899 static rtx
5900 load_tls_operand (rtx x, rtx reg)
5901 {
5902   rtx tmp;
5903
5904   if (reg == NULL_RTX)
5905     reg = gen_reg_rtx (SImode);
5906
5907   tmp = gen_rtx_CONST (SImode, x);
5908
5909   emit_move_insn (reg, tmp);
5910
5911   return reg;
5912 }
5913
5914 static rtx
5915 arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
5916 {
5917   rtx insns, label, labelno, sum;
5918
5919   start_sequence ();
5920
5921   labelno = GEN_INT (pic_labelno++);
5922   label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
5923   label = gen_rtx_CONST (VOIDmode, label);
5924
5925   sum = gen_rtx_UNSPEC (Pmode,
5926                         gen_rtvec (4, x, GEN_INT (reloc), label,
5927                                    GEN_INT (TARGET_ARM ? 8 : 4)),
5928                         UNSPEC_TLS);
5929   reg = load_tls_operand (sum, reg);
5930
5931   if (TARGET_ARM)
5932     emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
5933   else if (TARGET_THUMB2)
5934     emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5935   else /* TARGET_THUMB1 */
5936     emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5937
5938   *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX, LCT_PURE, /* LCT_CONST?  */
5939                                      Pmode, 1, reg, Pmode);
5940
5941   insns = get_insns ();
5942   end_sequence ();
5943
5944   return insns;
5945 }
5946
5947 rtx
5948 legitimize_tls_address (rtx x, rtx reg)
5949 {
5950   rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
5951   unsigned int model = SYMBOL_REF_TLS_MODEL (x);
5952
5953   switch (model)
5954     {
5955     case TLS_MODEL_GLOBAL_DYNAMIC:
5956       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
5957       dest = gen_reg_rtx (Pmode);
5958       emit_libcall_block (insns, dest, ret, x);
5959       return dest;
5960
5961     case TLS_MODEL_LOCAL_DYNAMIC:
5962       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
5963
5964       /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
5965          share the LDM result with other LD model accesses.  */
5966       eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
5967                             UNSPEC_TLS);
5968       dest = gen_reg_rtx (Pmode);
5969       emit_libcall_block (insns, dest, ret, eqv);
5970
5971       /* Load the addend.  */
5972       addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (TLS_LDO32)),
5973                                UNSPEC_TLS);
5974       addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
5975       return gen_rtx_PLUS (Pmode, dest, addend);
5976
5977     case TLS_MODEL_INITIAL_EXEC:
5978       labelno = GEN_INT (pic_labelno++);
5979       label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
5980       label = gen_rtx_CONST (VOIDmode, label);
5981       sum = gen_rtx_UNSPEC (Pmode,
5982                             gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
5983                                        GEN_INT (TARGET_ARM ? 8 : 4)),
5984                             UNSPEC_TLS);
5985       reg = load_tls_operand (sum, reg);
5986
5987       if (TARGET_ARM)
5988         emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
5989       else if (TARGET_THUMB2)
5990         emit_insn (gen_tls_load_dot_plus_four (reg, NULL, reg, labelno));
5991       else
5992         {
5993           emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5994           emit_move_insn (reg, gen_const_mem (SImode, reg));
5995         }
5996
5997       tp = arm_load_tp (NULL_RTX);
5998
5999       return gen_rtx_PLUS (Pmode, tp, reg);
6000
6001     case TLS_MODEL_LOCAL_EXEC:
6002       tp = arm_load_tp (NULL_RTX);
6003
6004       reg = gen_rtx_UNSPEC (Pmode,
6005                             gen_rtvec (2, x, GEN_INT (TLS_LE32)),
6006                             UNSPEC_TLS);
6007       reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
6008
6009       return gen_rtx_PLUS (Pmode, tp, reg);
6010
6011     default:
6012       abort ();
6013     }
6014 }
6015
6016 /* Try machine-dependent ways of modifying an illegitimate address
6017    to be legitimate.  If we find one, return the new, valid address.  */
6018 rtx
6019 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
6020 {
6021   if (!TARGET_ARM)
6022     {
6023       /* TODO: legitimize_address for Thumb2.  */
6024       if (TARGET_THUMB2)
6025         return x;
6026       return thumb_legitimize_address (x, orig_x, mode);
6027     }
6028
6029   if (arm_tls_symbol_p (x))
6030     return legitimize_tls_address (x, NULL_RTX);
6031
6032   if (GET_CODE (x) == PLUS)
6033     {
6034       rtx xop0 = XEXP (x, 0);
6035       rtx xop1 = XEXP (x, 1);
6036
6037       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
6038         xop0 = force_reg (SImode, xop0);
6039
6040       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
6041         xop1 = force_reg (SImode, xop1);
6042
6043       if (ARM_BASE_REGISTER_RTX_P (xop0)
6044           && GET_CODE (xop1) == CONST_INT)
6045         {
6046           HOST_WIDE_INT n, low_n;
6047           rtx base_reg, val;
6048           n = INTVAL (xop1);
6049
6050           /* VFP addressing modes actually allow greater offsets, but for
6051              now we just stick with the lowest common denominator.  */
6052           if (mode == DImode
6053               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
6054             {
6055               low_n = n & 0x0f;
6056               n &= ~0x0f;
6057               if (low_n > 4)
6058                 {
6059                   n += 16;
6060                   low_n -= 16;
6061                 }
6062             }
6063           else
6064             {
6065               low_n = ((mode) == TImode ? 0
6066                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
6067               n -= low_n;
6068             }
6069
6070           base_reg = gen_reg_rtx (SImode);
6071           val = force_operand (plus_constant (xop0, n), NULL_RTX);
6072           emit_move_insn (base_reg, val);
6073           x = plus_constant (base_reg, low_n);
6074         }
6075       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
6076         x = gen_rtx_PLUS (SImode, xop0, xop1);
6077     }
6078
6079   /* XXX We don't allow MINUS any more -- see comment in
6080      arm_legitimate_address_outer_p ().  */
6081   else if (GET_CODE (x) == MINUS)
6082     {
6083       rtx xop0 = XEXP (x, 0);
6084       rtx xop1 = XEXP (x, 1);
6085
6086       if (CONSTANT_P (xop0))
6087         xop0 = force_reg (SImode, xop0);
6088
6089       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
6090         xop1 = force_reg (SImode, xop1);
6091
6092       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
6093         x = gen_rtx_MINUS (SImode, xop0, xop1);
6094     }
6095
6096   /* Make sure to take full advantage of the pre-indexed addressing mode
6097      with absolute addresses which often allows for the base register to
6098      be factorized for multiple adjacent memory references, and it might
6099      even allows for the mini pool to be avoided entirely. */
6100   else if (GET_CODE (x) == CONST_INT && optimize > 0)
6101     {
6102       unsigned int bits;
6103       HOST_WIDE_INT mask, base, index;
6104       rtx base_reg;
6105
6106       /* ldr and ldrb can use a 12-bit index, ldrsb and the rest can only
6107          use a 8-bit index. So let's use a 12-bit index for SImode only and
6108          hope that arm_gen_constant will enable ldrb to use more bits. */
6109       bits = (mode == SImode) ? 12 : 8;
6110       mask = (1 << bits) - 1;
6111       base = INTVAL (x) & ~mask;
6112       index = INTVAL (x) & mask;
6113       if (bit_count (base & 0xffffffff) > (32 - bits)/2)
6114         {
6115           /* It'll most probably be more efficient to generate the base
6116              with more bits set and use a negative index instead. */
6117           base |= mask;
6118           index -= mask;
6119         }
6120       base_reg = force_reg (SImode, GEN_INT (base));
6121       x = plus_constant (base_reg, index);
6122     }
6123
6124   if (flag_pic)
6125     {
6126       /* We need to find and carefully transform any SYMBOL and LABEL
6127          references; so go back to the original address expression.  */
6128       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
6129
6130       if (new_x != orig_x)
6131         x = new_x;
6132     }
6133
6134   return x;
6135 }
6136
6137
6138 /* Try machine-dependent ways of modifying an illegitimate Thumb address
6139    to be legitimate.  If we find one, return the new, valid address.  */
6140 rtx
6141 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
6142 {
6143   if (arm_tls_symbol_p (x))
6144     return legitimize_tls_address (x, NULL_RTX);
6145
6146   if (GET_CODE (x) == PLUS
6147       && GET_CODE (XEXP (x, 1)) == CONST_INT
6148       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
6149           || INTVAL (XEXP (x, 1)) < 0))
6150     {
6151       rtx xop0 = XEXP (x, 0);
6152       rtx xop1 = XEXP (x, 1);
6153       HOST_WIDE_INT offset = INTVAL (xop1);
6154
6155       /* Try and fold the offset into a biasing of the base register and
6156          then offsetting that.  Don't do this when optimizing for space
6157          since it can cause too many CSEs.  */
6158       if (optimize_size && offset >= 0
6159           && offset < 256 + 31 * GET_MODE_SIZE (mode))
6160         {
6161           HOST_WIDE_INT delta;
6162
6163           if (offset >= 256)
6164             delta = offset - (256 - GET_MODE_SIZE (mode));
6165           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
6166             delta = 31 * GET_MODE_SIZE (mode);
6167           else
6168             delta = offset & (~31 * GET_MODE_SIZE (mode));
6169
6170           xop0 = force_operand (plus_constant (xop0, offset - delta),
6171                                 NULL_RTX);
6172           x = plus_constant (xop0, delta);
6173         }
6174       else if (offset < 0 && offset > -256)
6175         /* Small negative offsets are best done with a subtract before the
6176            dereference, forcing these into a register normally takes two
6177            instructions.  */
6178         x = force_operand (x, NULL_RTX);
6179       else
6180         {
6181           /* For the remaining cases, force the constant into a register.  */
6182           xop1 = force_reg (SImode, xop1);
6183           x = gen_rtx_PLUS (SImode, xop0, xop1);
6184         }
6185     }
6186   else if (GET_CODE (x) == PLUS
6187            && s_register_operand (XEXP (x, 1), SImode)
6188            && !s_register_operand (XEXP (x, 0), SImode))
6189     {
6190       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
6191
6192       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
6193     }
6194
6195   if (flag_pic)
6196     {
6197       /* We need to find and carefully transform any SYMBOL and LABEL
6198          references; so go back to the original address expression.  */
6199       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
6200
6201       if (new_x != orig_x)
6202         x = new_x;
6203     }
6204
6205   return x;
6206 }
6207
6208 rtx
6209 thumb_legitimize_reload_address (rtx *x_p,
6210                                  enum machine_mode mode,
6211                                  int opnum, int type,
6212                                  int ind_levels ATTRIBUTE_UNUSED)
6213 {
6214   rtx x = *x_p;
6215
6216   if (GET_CODE (x) == PLUS
6217       && GET_MODE_SIZE (mode) < 4
6218       && REG_P (XEXP (x, 0))
6219       && XEXP (x, 0) == stack_pointer_rtx
6220       && GET_CODE (XEXP (x, 1)) == CONST_INT
6221       && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
6222     {
6223       rtx orig_x = x;
6224
6225       x = copy_rtx (x);
6226       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
6227                    Pmode, VOIDmode, 0, 0, opnum, (enum reload_type) type);
6228       return x;
6229     }
6230
6231   /* If both registers are hi-regs, then it's better to reload the
6232      entire expression rather than each register individually.  That
6233      only requires one reload register rather than two.  */
6234   if (GET_CODE (x) == PLUS
6235       && REG_P (XEXP (x, 0))
6236       && REG_P (XEXP (x, 1))
6237       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
6238       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
6239     {
6240       rtx orig_x = x;
6241
6242       x = copy_rtx (x);
6243       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
6244                    Pmode, VOIDmode, 0, 0, opnum, (enum reload_type) type);
6245       return x;
6246     }
6247
6248   return NULL;
6249 }
6250
6251 /* Test for various thread-local symbols.  */
6252
6253 /* Return TRUE if X is a thread-local symbol.  */
6254
6255 static bool
6256 arm_tls_symbol_p (rtx x)
6257 {
6258   if (! TARGET_HAVE_TLS)
6259     return false;
6260
6261   if (GET_CODE (x) != SYMBOL_REF)
6262     return false;
6263
6264   return SYMBOL_REF_TLS_MODEL (x) != 0;
6265 }
6266
6267 /* Helper for arm_tls_referenced_p.  */
6268
6269 static int
6270 arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
6271 {
6272   if (GET_CODE (*x) == SYMBOL_REF)
6273     return SYMBOL_REF_TLS_MODEL (*x) != 0;
6274
6275   /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
6276      TLS offsets, not real symbol references.  */
6277   if (GET_CODE (*x) == UNSPEC
6278       && XINT (*x, 1) == UNSPEC_TLS)
6279     return -1;
6280
6281   return 0;
6282 }
6283
6284 /* Return TRUE if X contains any TLS symbol references.  */
6285
6286 bool
6287 arm_tls_referenced_p (rtx x)
6288 {
6289   if (! TARGET_HAVE_TLS)
6290     return false;
6291
6292   return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
6293 }
6294
6295 /* Implement TARGET_CANNOT_FORCE_CONST_MEM.  */
6296
6297 bool
6298 arm_cannot_force_const_mem (rtx x)
6299 {
6300   rtx base, offset;
6301
6302   if (ARM_OFFSETS_MUST_BE_WITHIN_SECTIONS_P)
6303     {
6304       split_const (x, &base, &offset);
6305       if (GET_CODE (base) == SYMBOL_REF
6306           && !offset_within_block_p (base, INTVAL (offset)))
6307         return true;
6308     }
6309   return arm_tls_referenced_p (x);
6310 }
6311 \f
6312 #define REG_OR_SUBREG_REG(X)                                            \
6313   (GET_CODE (X) == REG                                                  \
6314    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
6315
6316 #define REG_OR_SUBREG_RTX(X)                    \
6317    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
6318
6319 static inline int
6320 thumb1_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
6321 {
6322   enum machine_mode mode = GET_MODE (x);
6323   int total;
6324
6325   switch (code)
6326     {
6327     case ASHIFT:
6328     case ASHIFTRT:
6329     case LSHIFTRT:
6330     case ROTATERT:
6331     case PLUS:
6332     case MINUS:
6333     case COMPARE:
6334     case NEG:
6335     case NOT:
6336       return COSTS_N_INSNS (1);
6337
6338     case MULT:
6339       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6340         {
6341           int cycles = 0;
6342           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
6343
6344           while (i)
6345             {
6346               i >>= 2;
6347               cycles++;
6348             }
6349           return COSTS_N_INSNS (2) + cycles;
6350         }
6351       return COSTS_N_INSNS (1) + 16;
6352
6353     case SET:
6354       return (COSTS_N_INSNS (1)
6355               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
6356                      + GET_CODE (SET_DEST (x)) == MEM));
6357
6358     case CONST_INT:
6359       if (outer == SET)
6360         {
6361           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
6362             return 0;
6363           if (thumb_shiftable_const (INTVAL (x)))
6364             return COSTS_N_INSNS (2);
6365           return COSTS_N_INSNS (3);
6366         }
6367       else if ((outer == PLUS || outer == COMPARE)
6368                && INTVAL (x) < 256 && INTVAL (x) > -256)
6369         return 0;
6370       else if ((outer == IOR || outer == XOR || outer == AND)
6371                && INTVAL (x) < 256 && INTVAL (x) >= -256)
6372         return COSTS_N_INSNS (1);
6373       else if (outer == AND)
6374         {
6375           int i;
6376           /* This duplicates the tests in the andsi3 expander.  */
6377           for (i = 9; i <= 31; i++)
6378             if ((((HOST_WIDE_INT) 1) << i) - 1 == INTVAL (x)
6379                 || (((HOST_WIDE_INT) 1) << i) - 1 == ~INTVAL (x))
6380               return COSTS_N_INSNS (2);
6381         }
6382       else if (outer == ASHIFT || outer == ASHIFTRT
6383                || outer == LSHIFTRT)
6384         return 0;
6385       return COSTS_N_INSNS (2);
6386
6387     case CONST:
6388     case CONST_DOUBLE:
6389     case LABEL_REF:
6390     case SYMBOL_REF:
6391       return COSTS_N_INSNS (3);
6392
6393     case UDIV:
6394     case UMOD:
6395     case DIV:
6396     case MOD:
6397       return 100;
6398
6399     case TRUNCATE:
6400       return 99;
6401
6402     case AND:
6403     case XOR:
6404     case IOR:
6405       /* XXX guess.  */
6406       return 8;
6407
6408     case MEM:
6409       /* XXX another guess.  */
6410       /* Memory costs quite a lot for the first word, but subsequent words
6411          load at the equivalent of a single insn each.  */
6412       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
6413               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
6414                  ? 4 : 0));
6415
6416     case IF_THEN_ELSE:
6417       /* XXX a guess.  */
6418       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
6419         return 14;
6420       return 2;
6421
6422     case SIGN_EXTEND:
6423     case ZERO_EXTEND:
6424       total = mode == DImode ? COSTS_N_INSNS (1) : 0;
6425       total += thumb1_rtx_costs (XEXP (x, 0), GET_CODE (XEXP (x, 0)), code);
6426
6427       if (mode == SImode)
6428         return total;
6429
6430       if (arm_arch6)
6431         return total + COSTS_N_INSNS (1);
6432
6433       /* Assume a two-shift sequence.  Increase the cost slightly so
6434          we prefer actual shifts over an extend operation.  */
6435       return total + 1 + COSTS_N_INSNS (2);
6436
6437     default:
6438       return 99;
6439     }
6440 }
6441
6442 static inline bool
6443 arm_rtx_costs_1 (rtx x, enum rtx_code outer, int* total, bool speed)
6444 {
6445   enum machine_mode mode = GET_MODE (x);
6446   enum rtx_code subcode;
6447   rtx operand;
6448   enum rtx_code code = GET_CODE (x);
6449   *total = 0;
6450
6451   switch (code)
6452     {
6453     case MEM:
6454       /* Memory costs quite a lot for the first word, but subsequent words
6455          load at the equivalent of a single insn each.  */
6456       *total = COSTS_N_INSNS (2 + ARM_NUM_REGS (mode));
6457       return true;
6458
6459     case DIV:
6460     case MOD:
6461     case UDIV:
6462     case UMOD:
6463       if (TARGET_HARD_FLOAT && mode == SFmode)
6464         *total = COSTS_N_INSNS (2);
6465       else if (TARGET_HARD_FLOAT && mode == DFmode && !TARGET_VFP_SINGLE)
6466         *total = COSTS_N_INSNS (4);
6467       else
6468         *total = COSTS_N_INSNS (20);
6469       return false;
6470
6471     case ROTATE:
6472       if (GET_CODE (XEXP (x, 1)) == REG)
6473         *total = COSTS_N_INSNS (1); /* Need to subtract from 32 */
6474       else if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6475         *total = rtx_cost (XEXP (x, 1), code, speed);
6476
6477       /* Fall through */
6478     case ROTATERT:
6479       if (mode != SImode)
6480         {
6481           *total += COSTS_N_INSNS (4);
6482           return true;
6483         }
6484
6485       /* Fall through */
6486     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
6487       *total += rtx_cost (XEXP (x, 0), code, speed);
6488       if (mode == DImode)
6489         {
6490           *total += COSTS_N_INSNS (3);
6491           return true;
6492         }
6493
6494       *total += COSTS_N_INSNS (1);
6495       /* Increase the cost of complex shifts because they aren't any faster,
6496          and reduce dual issue opportunities.  */
6497       if (arm_tune_cortex_a9
6498           && outer != SET && GET_CODE (XEXP (x, 1)) != CONST_INT)
6499         ++*total;
6500
6501       return true;
6502
6503     case MINUS:
6504       if (mode == DImode)
6505         {
6506           *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
6507           if (GET_CODE (XEXP (x, 0)) == CONST_INT
6508               && const_ok_for_arm (INTVAL (XEXP (x, 0))))
6509             {
6510               *total += rtx_cost (XEXP (x, 1), code, speed);
6511               return true;
6512             }
6513
6514           if (GET_CODE (XEXP (x, 1)) == CONST_INT
6515               && const_ok_for_arm (INTVAL (XEXP (x, 1))))
6516             {
6517               *total += rtx_cost (XEXP (x, 0), code, speed);
6518               return true;
6519             }
6520
6521           return false;
6522         }
6523
6524       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6525         {
6526           if (TARGET_HARD_FLOAT
6527               && (mode == SFmode
6528                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
6529             {
6530               *total = COSTS_N_INSNS (1);
6531               if (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
6532                   && arm_const_double_rtx (XEXP (x, 0)))
6533                 {
6534                   *total += rtx_cost (XEXP (x, 1), code, speed);
6535                   return true;
6536                 }
6537
6538               if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
6539                   && arm_const_double_rtx (XEXP (x, 1)))
6540                 {
6541                   *total += rtx_cost (XEXP (x, 0), code, speed);
6542                   return true;
6543                 }
6544
6545               return false;
6546             }
6547           *total = COSTS_N_INSNS (20);
6548           return false;
6549         }
6550
6551       *total = COSTS_N_INSNS (1);
6552       if (GET_CODE (XEXP (x, 0)) == CONST_INT
6553           && const_ok_for_arm (INTVAL (XEXP (x, 0))))
6554         {
6555           *total += rtx_cost (XEXP (x, 1), code, speed);
6556           return true;
6557         }
6558
6559       subcode = GET_CODE (XEXP (x, 1));
6560       if (subcode == ASHIFT || subcode == ASHIFTRT
6561           || subcode == LSHIFTRT
6562           || subcode == ROTATE || subcode == ROTATERT)
6563         {
6564           *total += rtx_cost (XEXP (x, 0), code, speed);
6565           *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, speed);
6566           return true;
6567         }
6568
6569       /* A shift as a part of RSB costs no more than RSB itself.  */
6570       if (GET_CODE (XEXP (x, 0)) == MULT
6571           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
6572         {
6573           *total += rtx_cost (XEXP (XEXP (x, 0), 0), code, speed);
6574           *total += rtx_cost (XEXP (x, 1), code, speed);
6575           return true;
6576         }
6577
6578       if (subcode == MULT
6579           && power_of_two_operand (XEXP (XEXP (x, 1), 1), SImode))
6580         {
6581           *total += rtx_cost (XEXP (x, 0), code, speed);
6582           *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, speed);
6583           return true;
6584         }
6585
6586       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMPARE
6587           || GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMM_COMPARE)
6588         {
6589           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
6590           if (GET_CODE (XEXP (XEXP (x, 1), 0)) == REG
6591               && REGNO (XEXP (XEXP (x, 1), 0)) != CC_REGNUM)
6592             *total += COSTS_N_INSNS (1);
6593
6594           return true;
6595         }
6596
6597       /* Fall through */
6598
6599     case PLUS:
6600       if (code == PLUS && arm_arch6 && mode == SImode
6601           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
6602               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
6603         {
6604           *total = COSTS_N_INSNS (1);
6605           *total += rtx_cost (XEXP (XEXP (x, 0), 0), GET_CODE (XEXP (x, 0)),
6606                               speed);
6607           *total += rtx_cost (XEXP (x, 1), code, speed);
6608           return true;
6609         }
6610
6611       /* MLA: All arguments must be registers.  We filter out
6612          multiplication by a power of two, so that we fall down into
6613          the code below.  */
6614       if (GET_CODE (XEXP (x, 0)) == MULT
6615           && !power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
6616         {
6617           /* The cost comes from the cost of the multiply.  */
6618           return false;
6619         }
6620
6621       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6622         {
6623           if (TARGET_HARD_FLOAT
6624               && (mode == SFmode
6625                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
6626             {
6627               *total = COSTS_N_INSNS (1);
6628               if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
6629                   && arm_const_double_rtx (XEXP (x, 1)))
6630                 {
6631                   *total += rtx_cost (XEXP (x, 0), code, speed);
6632                   return true;
6633                 }
6634
6635               return false;
6636             }
6637
6638           *total = COSTS_N_INSNS (20);
6639           return false;
6640         }
6641
6642       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMPARE
6643           || GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMM_COMPARE)
6644         {
6645           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 1), code, speed);
6646           if (GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
6647               && REGNO (XEXP (XEXP (x, 0), 0)) != CC_REGNUM)
6648             *total += COSTS_N_INSNS (1);
6649           return true;
6650         }
6651
6652       /* Fall through */
6653
6654     case AND: case XOR: case IOR:
6655
6656       /* Normally the frame registers will be spilt into reg+const during
6657          reload, so it is a bad idea to combine them with other instructions,
6658          since then they might not be moved outside of loops.  As a compromise
6659          we allow integration with ops that have a constant as their second
6660          operand.  */
6661       if (REG_OR_SUBREG_REG (XEXP (x, 0))
6662           && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
6663           && GET_CODE (XEXP (x, 1)) != CONST_INT)
6664         *total = COSTS_N_INSNS (1);
6665
6666       if (mode == DImode)
6667         {
6668           *total += COSTS_N_INSNS (2);
6669           if (GET_CODE (XEXP (x, 1)) == CONST_INT
6670               && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
6671             {
6672               *total += rtx_cost (XEXP (x, 0), code, speed);
6673               return true;
6674             }
6675
6676           return false;
6677         }
6678
6679       *total += COSTS_N_INSNS (1);
6680       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6681           && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
6682         {
6683           *total += rtx_cost (XEXP (x, 0), code, speed);
6684           return true;
6685         }
6686       subcode = GET_CODE (XEXP (x, 0));
6687       if (subcode == ASHIFT || subcode == ASHIFTRT
6688           || subcode == LSHIFTRT
6689           || subcode == ROTATE || subcode == ROTATERT)
6690         {
6691           *total += rtx_cost (XEXP (x, 1), code, speed);
6692           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6693           return true;
6694         }
6695
6696       if (subcode == MULT
6697           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
6698         {
6699           *total += rtx_cost (XEXP (x, 1), code, speed);
6700           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6701           return true;
6702         }
6703
6704       if (subcode == UMIN || subcode == UMAX
6705           || subcode == SMIN || subcode == SMAX)
6706         {
6707           *total = COSTS_N_INSNS (3);
6708           return true;
6709         }
6710
6711       return false;
6712
6713     case MULT:
6714       /* This should have been handled by the CPU specific routines.  */
6715       gcc_unreachable ();
6716
6717     case TRUNCATE:
6718       if (arm_arch3m && mode == SImode
6719           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
6720           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
6721           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
6722               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
6723           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
6724               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
6725         {
6726           *total = rtx_cost (XEXP (XEXP (x, 0), 0), LSHIFTRT, speed);
6727           return true;
6728         }
6729       *total = COSTS_N_INSNS (2); /* Plus the cost of the MULT */
6730       return false;
6731
6732     case NEG:
6733       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6734         {
6735           if (TARGET_HARD_FLOAT
6736               && (mode == SFmode
6737                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
6738             {
6739               *total = COSTS_N_INSNS (1);
6740               return false;
6741             }
6742           *total = COSTS_N_INSNS (2);
6743           return false;
6744         }
6745
6746       /* Fall through */
6747     case NOT:
6748       *total = COSTS_N_INSNS (ARM_NUM_REGS(mode));
6749       if (mode == SImode && code == NOT)
6750         {
6751           subcode = GET_CODE (XEXP (x, 0));
6752           if (subcode == ASHIFT || subcode == ASHIFTRT
6753               || subcode == LSHIFTRT
6754               || subcode == ROTATE || subcode == ROTATERT
6755               || (subcode == MULT
6756                   && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode)))
6757             {
6758               *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6759               /* Register shifts cost an extra cycle.  */
6760               if (GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
6761                 *total += COSTS_N_INSNS (1) + rtx_cost (XEXP (XEXP (x, 0), 1),
6762                                                         subcode, speed);
6763               return true;
6764             }
6765         }
6766
6767       return false;
6768
6769     case IF_THEN_ELSE:
6770       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
6771         {
6772           *total = COSTS_N_INSNS (4);
6773           return true;
6774         }
6775
6776       operand = XEXP (x, 0);
6777
6778       if (!((GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMPARE
6779              || GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMM_COMPARE)
6780             && GET_CODE (XEXP (operand, 0)) == REG
6781             && REGNO (XEXP (operand, 0)) == CC_REGNUM))
6782         *total += COSTS_N_INSNS (1);
6783       *total += (rtx_cost (XEXP (x, 1), code, speed)
6784                  + rtx_cost (XEXP (x, 2), code, speed));
6785       return true;
6786
6787     case NE:
6788       if (mode == SImode && XEXP (x, 1) == const0_rtx)
6789         {
6790           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
6791           return true;
6792         }
6793       goto scc_insn;
6794
6795     case GE:
6796       if ((GET_CODE (XEXP (x, 0)) != REG || REGNO (XEXP (x, 0)) != CC_REGNUM)
6797           && mode == SImode && XEXP (x, 1) == const0_rtx)
6798         {
6799           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
6800           return true;
6801         }
6802       goto scc_insn;
6803
6804     case LT:
6805       if ((GET_CODE (XEXP (x, 0)) != REG || REGNO (XEXP (x, 0)) != CC_REGNUM)
6806           && mode == SImode && XEXP (x, 1) == const0_rtx)
6807         {
6808           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
6809           return true;
6810         }
6811       goto scc_insn;
6812
6813     case EQ:
6814     case GT:
6815     case LE:
6816     case GEU:
6817     case LTU:
6818     case GTU:
6819     case LEU:
6820     case UNORDERED:
6821     case ORDERED:
6822     case UNEQ:
6823     case UNGE:
6824     case UNLT:
6825     case UNGT:
6826     case UNLE:
6827     scc_insn:
6828       /* SCC insns.  In the case where the comparison has already been
6829          performed, then they cost 2 instructions.  Otherwise they need
6830          an additional comparison before them.  */
6831       *total = COSTS_N_INSNS (2);
6832       if (GET_CODE (XEXP (x, 0)) == REG && REGNO (XEXP (x, 0)) == CC_REGNUM)
6833         {
6834           return true;
6835         }
6836
6837       /* Fall through */
6838     case COMPARE:
6839       if (GET_CODE (XEXP (x, 0)) == REG && REGNO (XEXP (x, 0)) == CC_REGNUM)
6840         {
6841           *total = 0;
6842           return true;
6843         }
6844
6845       *total += COSTS_N_INSNS (1);
6846       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6847           && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
6848         {
6849           *total += rtx_cost (XEXP (x, 0), code, speed);
6850           return true;
6851         }
6852
6853       subcode = GET_CODE (XEXP (x, 0));
6854       if (subcode == ASHIFT || subcode == ASHIFTRT
6855           || subcode == LSHIFTRT
6856           || subcode == ROTATE || subcode == ROTATERT)
6857         {
6858           *total += rtx_cost (XEXP (x, 1), code, speed);
6859           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6860           return true;
6861         }
6862
6863       if (subcode == MULT
6864           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
6865         {
6866           *total += rtx_cost (XEXP (x, 1), code, speed);
6867           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6868           return true;
6869         }
6870       
6871       return false;
6872
6873     case UMIN:
6874     case UMAX:
6875     case SMIN:
6876     case SMAX:
6877       *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
6878       if (GET_CODE (XEXP (x, 1)) != CONST_INT
6879           || !const_ok_for_arm (INTVAL (XEXP (x, 1))))
6880         *total += rtx_cost (XEXP (x, 1), code, speed);
6881       return true;
6882
6883     case ABS:
6884       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6885         {
6886           if (TARGET_HARD_FLOAT
6887               && (mode == SFmode
6888                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
6889             {
6890               *total = COSTS_N_INSNS (1);
6891               return false;
6892             }
6893           *total = COSTS_N_INSNS (20);
6894           return false;
6895         }
6896       *total = COSTS_N_INSNS (1);
6897       if (mode == DImode)
6898         *total += COSTS_N_INSNS (3);
6899       return false;
6900
6901     case SIGN_EXTEND:
6902     case ZERO_EXTEND:
6903       *total = 0;
6904       if (GET_MODE_CLASS (mode) == MODE_INT)
6905         {
6906           rtx op = XEXP (x, 0);
6907           enum machine_mode opmode = GET_MODE (op);
6908
6909           if (mode == DImode)
6910             *total += COSTS_N_INSNS (1);
6911
6912           if (opmode != SImode)
6913             {
6914               if (MEM_P (op))
6915                 {
6916                   /* If !arm_arch4, we use one of the extendhisi2_mem
6917                      or movhi_bytes patterns for HImode.  For a QImode
6918                      sign extension, we first zero-extend from memory
6919                      and then perform a shift sequence.  */
6920                   if (!arm_arch4 && (opmode != QImode || code == SIGN_EXTEND))
6921                     *total += COSTS_N_INSNS (2);
6922                 }
6923               else if (arm_arch6)
6924                 *total += COSTS_N_INSNS (1);
6925
6926               /* We don't have the necessary insn, so we need to perform some
6927                  other operation.  */
6928               else if (TARGET_ARM && code == ZERO_EXTEND && mode == QImode)
6929                 /* An and with constant 255.  */
6930                 *total += COSTS_N_INSNS (1);
6931               else
6932                 /* A shift sequence.  Increase costs slightly to avoid
6933                    combining two shifts into an extend operation.  */
6934                 *total += COSTS_N_INSNS (2) + 1;
6935             }
6936
6937           return false;
6938         }
6939
6940       switch (GET_MODE (XEXP (x, 0)))
6941         {
6942         case V8QImode:
6943         case V4HImode:
6944         case V2SImode:
6945         case V4QImode:
6946         case V2HImode:
6947           *total = COSTS_N_INSNS (1);
6948           return false;
6949
6950         default:
6951           gcc_unreachable ();
6952         }
6953       gcc_unreachable ();
6954
6955     case ZERO_EXTRACT:
6956     case SIGN_EXTRACT:
6957       *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
6958       return true;
6959
6960     case CONST_INT:
6961       if (const_ok_for_arm (INTVAL (x))
6962           || const_ok_for_arm (~INTVAL (x)))
6963         *total = COSTS_N_INSNS (1);
6964       else
6965         *total = COSTS_N_INSNS (arm_gen_constant (SET, mode, NULL_RTX,
6966                                                   INTVAL (x), NULL_RTX,
6967                                                   NULL_RTX, 0, 0));
6968       return true;
6969
6970     case CONST:
6971     case LABEL_REF:
6972     case SYMBOL_REF:
6973       *total = COSTS_N_INSNS (3);
6974       return true;
6975
6976     case HIGH:
6977       *total = COSTS_N_INSNS (1);
6978       return true;
6979
6980     case LO_SUM:
6981       *total = COSTS_N_INSNS (1);
6982       *total += rtx_cost (XEXP (x, 0), code, speed);
6983       return true;
6984
6985     case CONST_DOUBLE:
6986       if (TARGET_HARD_FLOAT && vfp3_const_double_rtx (x)
6987           && (mode == SFmode || !TARGET_VFP_SINGLE))
6988         *total = COSTS_N_INSNS (1);
6989       else
6990         *total = COSTS_N_INSNS (4);
6991       return true;
6992
6993     default:
6994       *total = COSTS_N_INSNS (4);
6995       return false;
6996     }
6997 }
6998
6999 /* Estimates the size cost of thumb1 instructions.
7000    For now most of the code is copied from thumb1_rtx_costs. We need more
7001    fine grain tuning when we have more related test cases.  */
7002 static inline int
7003 thumb1_size_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
7004 {
7005   enum machine_mode mode = GET_MODE (x);
7006
7007   switch (code)
7008     {
7009     case ASHIFT:
7010     case ASHIFTRT:
7011     case LSHIFTRT:
7012     case ROTATERT:
7013     case PLUS:
7014     case MINUS:
7015     case COMPARE:
7016     case NEG:
7017     case NOT:
7018       return COSTS_N_INSNS (1);
7019
7020     case MULT:
7021       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7022         {
7023           /* Thumb1 mul instruction can't operate on const. We must Load it
7024              into a register first.  */
7025           int const_size = thumb1_size_rtx_costs (XEXP (x, 1), CONST_INT, SET);
7026           return COSTS_N_INSNS (1) + const_size;
7027         }
7028       return COSTS_N_INSNS (1);
7029
7030     case SET:
7031       return (COSTS_N_INSNS (1)
7032               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
7033                      + GET_CODE (SET_DEST (x)) == MEM));
7034
7035     case CONST_INT:
7036       if (outer == SET)
7037         {
7038           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
7039             return COSTS_N_INSNS (1);
7040           /* See split "TARGET_THUMB1 && satisfies_constraint_J".  */
7041           if (INTVAL (x) >= -255 && INTVAL (x) <= -1)
7042             return COSTS_N_INSNS (2);
7043           /* See split "TARGET_THUMB1 && satisfies_constraint_K".  */
7044           if (thumb_shiftable_const (INTVAL (x)))
7045             return COSTS_N_INSNS (2);
7046           return COSTS_N_INSNS (3);
7047         }
7048       else if ((outer == PLUS || outer == COMPARE)
7049                && INTVAL (x) < 256 && INTVAL (x) > -256)
7050         return 0;
7051       else if ((outer == IOR || outer == XOR || outer == AND)
7052                && INTVAL (x) < 256 && INTVAL (x) >= -256)
7053         return COSTS_N_INSNS (1);
7054       else if (outer == AND)
7055         {
7056           int i;
7057           /* This duplicates the tests in the andsi3 expander.  */
7058           for (i = 9; i <= 31; i++)
7059             if ((((HOST_WIDE_INT) 1) << i) - 1 == INTVAL (x)
7060                 || (((HOST_WIDE_INT) 1) << i) - 1 == ~INTVAL (x))
7061               return COSTS_N_INSNS (2);
7062         }
7063       else if (outer == ASHIFT || outer == ASHIFTRT
7064                || outer == LSHIFTRT)
7065         return 0;
7066       return COSTS_N_INSNS (2);
7067
7068     case CONST:
7069     case CONST_DOUBLE:
7070     case LABEL_REF:
7071     case SYMBOL_REF:
7072       return COSTS_N_INSNS (3);
7073
7074     case UDIV:
7075     case UMOD:
7076     case DIV:
7077     case MOD:
7078       return 100;
7079
7080     case TRUNCATE:
7081       return 99;
7082
7083     case AND:
7084     case XOR:
7085     case IOR:
7086       /* XXX guess.  */
7087       return 8;
7088
7089     case MEM:
7090       /* XXX another guess.  */
7091       /* Memory costs quite a lot for the first word, but subsequent words
7092          load at the equivalent of a single insn each.  */
7093       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
7094               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
7095                  ? 4 : 0));
7096
7097     case IF_THEN_ELSE:
7098       /* XXX a guess.  */
7099       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
7100         return 14;
7101       return 2;
7102
7103     case ZERO_EXTEND:
7104       /* XXX still guessing.  */
7105       switch (GET_MODE (XEXP (x, 0)))
7106         {
7107           case QImode:
7108             return (1 + (mode == DImode ? 4 : 0)
7109                     + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
7110
7111           case HImode:
7112             return (4 + (mode == DImode ? 4 : 0)
7113                     + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
7114
7115           case SImode:
7116             return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
7117
7118           default:
7119             return 99;
7120         }
7121
7122     default:
7123       return 99;
7124     }
7125 }
7126
7127 /* RTX costs when optimizing for size.  */
7128 static bool
7129 arm_size_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7130                     int *total)
7131 {
7132   enum machine_mode mode = GET_MODE (x);
7133   if (TARGET_THUMB1)
7134     {
7135       *total = thumb1_size_rtx_costs (x, code, outer_code);
7136       return true;
7137     }
7138
7139   /* FIXME: This makes no attempt to prefer narrow Thumb-2 instructions.  */
7140   switch (code)
7141     {
7142     case MEM:
7143       /* A memory access costs 1 insn if the mode is small, or the address is
7144          a single register, otherwise it costs one insn per word.  */
7145       if (REG_P (XEXP (x, 0)))
7146         *total = COSTS_N_INSNS (1);
7147       else if (flag_pic
7148                && GET_CODE (XEXP (x, 0)) == PLUS
7149                && will_be_in_index_register (XEXP (XEXP (x, 0), 1)))
7150         /* This will be split into two instructions.
7151            See arm.md:calculate_pic_address.  */
7152         *total = COSTS_N_INSNS (2);
7153       else
7154         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7155       return true;
7156
7157     case DIV:
7158     case MOD:
7159     case UDIV:
7160     case UMOD:
7161       /* Needs a libcall, so it costs about this.  */
7162       *total = COSTS_N_INSNS (2);
7163       return false;
7164
7165     case ROTATE:
7166       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
7167         {
7168           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, false);
7169           return true;
7170         }
7171       /* Fall through */
7172     case ROTATERT:
7173     case ASHIFT:
7174     case LSHIFTRT:
7175     case ASHIFTRT:
7176       if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
7177         {
7178           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code, false);
7179           return true;
7180         }
7181       else if (mode == SImode)
7182         {
7183           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, false);
7184           /* Slightly disparage register shifts, but not by much.  */
7185           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
7186             *total += 1 + rtx_cost (XEXP (x, 1), code, false);
7187           return true;
7188         }
7189
7190       /* Needs a libcall.  */
7191       *total = COSTS_N_INSNS (2);
7192       return false;
7193
7194     case MINUS:
7195       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7196           && (mode == SFmode || !TARGET_VFP_SINGLE))
7197         {
7198           *total = COSTS_N_INSNS (1);
7199           return false;
7200         }
7201
7202       if (mode == SImode)
7203         {
7204           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
7205           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
7206
7207           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
7208               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
7209               || subcode1 == ROTATE || subcode1 == ROTATERT
7210               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
7211               || subcode1 == ASHIFTRT)
7212             {
7213               /* It's just the cost of the two operands.  */
7214               *total = 0;
7215               return false;
7216             }
7217
7218           *total = COSTS_N_INSNS (1);
7219           return false;
7220         }
7221
7222       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7223       return false;
7224
7225     case PLUS:
7226       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7227           && (mode == SFmode || !TARGET_VFP_SINGLE))
7228         {
7229           *total = COSTS_N_INSNS (1);
7230           return false;
7231         }
7232
7233       /* A shift as a part of ADD costs nothing.  */
7234       if (GET_CODE (XEXP (x, 0)) == MULT
7235           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
7236         {
7237           *total = COSTS_N_INSNS (TARGET_THUMB2 ? 2 : 1);
7238           *total += rtx_cost (XEXP (XEXP (x, 0), 0), code, false);
7239           *total += rtx_cost (XEXP (x, 1), code, false);
7240           return true;
7241         }
7242
7243       /* Fall through */
7244     case AND: case XOR: case IOR:
7245       if (mode == SImode)
7246         {
7247           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
7248
7249           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
7250               || subcode == LSHIFTRT || subcode == ASHIFTRT
7251               || (code == AND && subcode == NOT))
7252             {
7253               /* It's just the cost of the two operands.  */
7254               *total = 0;
7255               return false;
7256             }
7257         }
7258
7259       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7260       return false;
7261
7262     case MULT:
7263       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7264       return false;
7265
7266     case NEG:
7267       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7268           && (mode == SFmode || !TARGET_VFP_SINGLE))
7269         {
7270           *total = COSTS_N_INSNS (1);
7271           return false;
7272         }
7273
7274       /* Fall through */
7275     case NOT:
7276       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7277
7278       return false;
7279
7280     case IF_THEN_ELSE:
7281       *total = 0;
7282       return false;
7283
7284     case COMPARE:
7285       if (cc_register (XEXP (x, 0), VOIDmode))
7286         * total = 0;
7287       else
7288         *total = COSTS_N_INSNS (1);
7289       return false;
7290
7291     case ABS:
7292       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7293           && (mode == SFmode || !TARGET_VFP_SINGLE))
7294         *total = COSTS_N_INSNS (1);
7295       else
7296         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
7297       return false;
7298
7299     case SIGN_EXTEND:
7300     case ZERO_EXTEND:
7301       return arm_rtx_costs_1 (x, outer_code, total, 0);
7302
7303     case CONST_INT:
7304       if (const_ok_for_arm (INTVAL (x)))
7305         /* A multiplication by a constant requires another instruction
7306            to load the constant to a register.  */
7307         *total = COSTS_N_INSNS ((outer_code == SET || outer_code == MULT)
7308                                 ? 1 : 0);
7309       else if (const_ok_for_arm (~INTVAL (x)))
7310         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
7311       else if (const_ok_for_arm (-INTVAL (x)))
7312         {
7313           if (outer_code == COMPARE || outer_code == PLUS
7314               || outer_code == MINUS)
7315             *total = 0;
7316           else
7317             *total = COSTS_N_INSNS (1);
7318         }
7319       else
7320         *total = COSTS_N_INSNS (2);
7321       return true;
7322
7323     case CONST:
7324     case LABEL_REF:
7325     case SYMBOL_REF:
7326       *total = COSTS_N_INSNS (2);
7327       return true;
7328
7329     case CONST_DOUBLE:
7330       *total = COSTS_N_INSNS (4);
7331       return true;
7332
7333     case HIGH:
7334     case LO_SUM:
7335       /* We prefer constant pool entries to MOVW/MOVT pairs, so bump the
7336          cost of these slightly.  */
7337       *total = COSTS_N_INSNS (1) + 1;
7338       return true;
7339
7340     default:
7341       if (mode != VOIDmode)
7342         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7343       else
7344         *total = COSTS_N_INSNS (4); /* How knows?  */
7345       return false;
7346     }
7347 }
7348
7349 /* RTX costs when optimizing for size.  */
7350 static bool
7351 arm_rtx_costs (rtx x, int code, int outer_code, int *total,
7352                bool speed)
7353 {
7354   if (!speed)
7355     return arm_size_rtx_costs (x, (enum rtx_code) code,
7356                                (enum rtx_code) outer_code, total);
7357   else
7358     return current_tune->rtx_costs (x, (enum rtx_code) code,
7359                                     (enum rtx_code) outer_code,
7360                                     total, speed);
7361 }
7362
7363 /* RTX costs for cores with a slow MUL implementation.  Thumb-2 is not
7364    supported on any "slowmul" cores, so it can be ignored.  */
7365
7366 static bool
7367 arm_slowmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7368                        int *total, bool speed)
7369 {
7370   enum machine_mode mode = GET_MODE (x);
7371
7372   if (TARGET_THUMB)
7373     {
7374       *total = thumb1_rtx_costs (x, code, outer_code);
7375       return true;
7376     }
7377
7378   switch (code)
7379     {
7380     case MULT:
7381       if (GET_MODE_CLASS (mode) == MODE_FLOAT
7382           || mode == DImode)
7383         {
7384           *total = COSTS_N_INSNS (20);
7385           return false;
7386         }
7387
7388       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7389         {
7390           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
7391                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
7392           int cost, const_ok = const_ok_for_arm (i);
7393           int j, booth_unit_size;
7394
7395           /* Tune as appropriate.  */
7396           cost = const_ok ? 4 : 8;
7397           booth_unit_size = 2;
7398           for (j = 0; i && j < 32; j += booth_unit_size)
7399             {
7400               i >>= booth_unit_size;
7401               cost++;
7402             }
7403
7404           *total = COSTS_N_INSNS (cost);
7405           *total += rtx_cost (XEXP (x, 0), code, speed);
7406           return true;
7407         }
7408
7409       *total = COSTS_N_INSNS (20);
7410       return false;
7411
7412     default:
7413       return arm_rtx_costs_1 (x, outer_code, total, speed);;
7414     }
7415 }
7416
7417
7418 /* RTX cost for cores with a fast multiply unit (M variants).  */
7419
7420 static bool
7421 arm_fastmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7422                        int *total, bool speed)
7423 {
7424   enum machine_mode mode = GET_MODE (x);
7425
7426   if (TARGET_THUMB1)
7427     {
7428       *total = thumb1_rtx_costs (x, code, outer_code);
7429       return true;
7430     }
7431
7432   /* ??? should thumb2 use different costs?  */
7433   switch (code)
7434     {
7435     case MULT:
7436       /* There is no point basing this on the tuning, since it is always the
7437          fast variant if it exists at all.  */
7438       if (mode == DImode
7439           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
7440           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7441               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7442         {
7443           *total = COSTS_N_INSNS(2);
7444           return false;
7445         }
7446
7447
7448       if (mode == DImode)
7449         {
7450           *total = COSTS_N_INSNS (5);
7451           return false;
7452         }
7453
7454       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7455         {
7456           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
7457                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
7458           int cost, const_ok = const_ok_for_arm (i);
7459           int j, booth_unit_size;
7460
7461           /* Tune as appropriate.  */
7462           cost = const_ok ? 4 : 8;
7463           booth_unit_size = 8;
7464           for (j = 0; i && j < 32; j += booth_unit_size)
7465             {
7466               i >>= booth_unit_size;
7467               cost++;
7468             }
7469
7470           *total = COSTS_N_INSNS(cost);
7471           return false;
7472         }
7473
7474       if (mode == SImode)
7475         {
7476           *total = COSTS_N_INSNS (4);
7477           return false;
7478         }
7479
7480       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7481         {
7482           if (TARGET_HARD_FLOAT
7483               && (mode == SFmode
7484                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
7485             {
7486               *total = COSTS_N_INSNS (1);
7487               return false;
7488             }
7489         }
7490
7491       /* Requires a lib call */
7492       *total = COSTS_N_INSNS (20);
7493       return false;
7494
7495     default:
7496       return arm_rtx_costs_1 (x, outer_code, total, speed);
7497     }
7498 }
7499
7500
7501 /* RTX cost for XScale CPUs.  Thumb-2 is not supported on any xscale cores,
7502    so it can be ignored.  */
7503
7504 static bool
7505 arm_xscale_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7506                       int *total, bool speed)
7507 {
7508   enum machine_mode mode = GET_MODE (x);
7509
7510   if (TARGET_THUMB)
7511     {
7512       *total = thumb1_rtx_costs (x, code, outer_code);
7513       return true;
7514     }
7515
7516   switch (code)
7517     {
7518     case COMPARE:
7519       if (GET_CODE (XEXP (x, 0)) != MULT)
7520         return arm_rtx_costs_1 (x, outer_code, total, speed);
7521
7522       /* A COMPARE of a MULT is slow on XScale; the muls instruction
7523          will stall until the multiplication is complete.  */
7524       *total = COSTS_N_INSNS (3);
7525       return false;
7526
7527     case MULT:
7528       /* There is no point basing this on the tuning, since it is always the
7529          fast variant if it exists at all.  */
7530       if (mode == DImode
7531           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
7532           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7533               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7534         {
7535           *total = COSTS_N_INSNS (2);
7536           return false;
7537         }
7538
7539
7540       if (mode == DImode)
7541         {
7542           *total = COSTS_N_INSNS (5);
7543           return false;
7544         }
7545
7546       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7547         {
7548           /* If operand 1 is a constant we can more accurately
7549              calculate the cost of the multiply.  The multiplier can
7550              retire 15 bits on the first cycle and a further 12 on the
7551              second.  We do, of course, have to load the constant into
7552              a register first.  */
7553           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
7554           /* There's a general overhead of one cycle.  */
7555           int cost = 1;
7556           unsigned HOST_WIDE_INT masked_const;
7557
7558           if (i & 0x80000000)
7559             i = ~i;
7560
7561           i &= (unsigned HOST_WIDE_INT) 0xffffffff;
7562
7563           masked_const = i & 0xffff8000;
7564           if (masked_const != 0)
7565             {
7566               cost++;
7567               masked_const = i & 0xf8000000;
7568               if (masked_const != 0)
7569                 cost++;
7570             }
7571           *total = COSTS_N_INSNS (cost);
7572           return false;
7573         }
7574
7575       if (mode == SImode)
7576         {
7577           *total = COSTS_N_INSNS (3);
7578           return false;
7579         }
7580
7581       /* Requires a lib call */
7582       *total = COSTS_N_INSNS (20);
7583       return false;
7584
7585     default:
7586       return arm_rtx_costs_1 (x, outer_code, total, speed);
7587     }
7588 }
7589
7590
7591 /* RTX costs for 9e (and later) cores.  */
7592
7593 static bool
7594 arm_9e_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7595                   int *total, bool speed)
7596 {
7597   enum machine_mode mode = GET_MODE (x);
7598
7599   if (TARGET_THUMB1)
7600     {
7601       switch (code)
7602         {
7603         case MULT:
7604           *total = COSTS_N_INSNS (3);
7605           return true;
7606
7607         default:
7608           *total = thumb1_rtx_costs (x, code, outer_code);
7609           return true;
7610         }
7611     }
7612
7613   switch (code)
7614     {
7615     case MULT:
7616       /* There is no point basing this on the tuning, since it is always the
7617          fast variant if it exists at all.  */
7618       if (mode == DImode
7619           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
7620           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7621               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7622         {
7623           *total = COSTS_N_INSNS (2);
7624           return false;
7625         }
7626
7627
7628       if (mode == DImode)
7629         {
7630           *total = COSTS_N_INSNS (5);
7631           return false;
7632         }
7633
7634       if (mode == SImode)
7635         {
7636           *total = COSTS_N_INSNS (2);
7637           return false;
7638         }
7639
7640       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7641         {
7642           if (TARGET_HARD_FLOAT
7643               && (mode == SFmode
7644                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
7645             {
7646               *total = COSTS_N_INSNS (1);
7647               return false;
7648             }
7649         }
7650
7651       *total = COSTS_N_INSNS (20);
7652       return false;
7653
7654     default:
7655       return arm_rtx_costs_1 (x, outer_code, total, speed);
7656     }
7657 }
7658 /* All address computations that can be done are free, but rtx cost returns
7659    the same for practically all of them.  So we weight the different types
7660    of address here in the order (most pref first):
7661    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
7662 static inline int
7663 arm_arm_address_cost (rtx x)
7664 {
7665   enum rtx_code c  = GET_CODE (x);
7666
7667   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
7668     return 0;
7669   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
7670     return 10;
7671
7672   if (c == PLUS)
7673     {
7674       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7675         return 2;
7676
7677       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
7678         return 3;
7679
7680       return 4;
7681     }
7682
7683   return 6;
7684 }
7685
7686 static inline int
7687 arm_thumb_address_cost (rtx x)
7688 {
7689   enum rtx_code c  = GET_CODE (x);
7690
7691   if (c == REG)
7692     return 1;
7693   if (c == PLUS
7694       && GET_CODE (XEXP (x, 0)) == REG
7695       && GET_CODE (XEXP (x, 1)) == CONST_INT)
7696     return 1;
7697
7698   return 2;
7699 }
7700
7701 static int
7702 arm_address_cost (rtx x, bool speed ATTRIBUTE_UNUSED)
7703 {
7704   return TARGET_32BIT ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
7705 }
7706
7707 /* Adjust cost hook for XScale.  */
7708 static bool
7709 xscale_sched_adjust_cost (rtx insn, rtx link, rtx dep, int * cost)
7710 {
7711   /* Some true dependencies can have a higher cost depending
7712      on precisely how certain input operands are used.  */
7713   if (REG_NOTE_KIND(link) == 0
7714       && recog_memoized (insn) >= 0
7715       && recog_memoized (dep) >= 0)
7716     {
7717       int shift_opnum = get_attr_shift (insn);
7718       enum attr_type attr_type = get_attr_type (dep);
7719
7720       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
7721          operand for INSN.  If we have a shifted input operand and the
7722          instruction we depend on is another ALU instruction, then we may
7723          have to account for an additional stall.  */
7724       if (shift_opnum != 0
7725           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
7726         {
7727           rtx shifted_operand;
7728           int opno;
7729
7730           /* Get the shifted operand.  */
7731           extract_insn (insn);
7732           shifted_operand = recog_data.operand[shift_opnum];
7733
7734           /* Iterate over all the operands in DEP.  If we write an operand
7735              that overlaps with SHIFTED_OPERAND, then we have increase the
7736              cost of this dependency.  */
7737           extract_insn (dep);
7738           preprocess_constraints ();
7739           for (opno = 0; opno < recog_data.n_operands; opno++)
7740             {
7741               /* We can ignore strict inputs.  */
7742               if (recog_data.operand_type[opno] == OP_IN)
7743                 continue;
7744
7745               if (reg_overlap_mentioned_p (recog_data.operand[opno],
7746                                            shifted_operand))
7747                 {
7748                   *cost = 2;
7749                   return false;
7750                 }
7751             }
7752         }
7753     }
7754   return true;
7755 }
7756
7757 /* Adjust cost hook for Cortex A9.  */
7758 static bool
7759 cortex_a9_sched_adjust_cost (rtx insn, rtx link, rtx dep, int * cost)
7760 {
7761   switch (REG_NOTE_KIND (link))
7762     {
7763     case REG_DEP_ANTI:
7764       *cost = 0;
7765       return false;
7766
7767     case REG_DEP_TRUE:
7768     case REG_DEP_OUTPUT:
7769         if (recog_memoized (insn) >= 0
7770             && recog_memoized (dep) >= 0)
7771           {
7772             if (GET_CODE (PATTERN (insn)) == SET)
7773               {
7774                 if (GET_MODE_CLASS 
7775                     (GET_MODE (SET_DEST (PATTERN (insn)))) == MODE_FLOAT
7776                   || GET_MODE_CLASS 
7777                     (GET_MODE (SET_SRC (PATTERN (insn)))) == MODE_FLOAT)
7778                   {
7779                     enum attr_type attr_type_insn = get_attr_type (insn);
7780                     enum attr_type attr_type_dep = get_attr_type (dep);
7781
7782                     /* By default all dependencies of the form
7783                        s0 = s0 <op> s1
7784                        s0 = s0 <op> s2
7785                        have an extra latency of 1 cycle because
7786                        of the input and output dependency in this
7787                        case. However this gets modeled as an true
7788                        dependency and hence all these checks.  */
7789                     if (REG_P (SET_DEST (PATTERN (insn)))
7790                         && REG_P (SET_DEST (PATTERN (dep)))
7791                         && reg_overlap_mentioned_p (SET_DEST (PATTERN (insn)),
7792                                                     SET_DEST (PATTERN (dep))))
7793                       {
7794                         /* FMACS is a special case where the dependant
7795                            instruction can be issued 3 cycles before
7796                            the normal latency in case of an output 
7797                            dependency.  */
7798                         if ((attr_type_insn == TYPE_FMACS
7799                              || attr_type_insn == TYPE_FMACD)
7800                             && (attr_type_dep == TYPE_FMACS
7801                                 || attr_type_dep == TYPE_FMACD))
7802                           {
7803                             if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
7804                               *cost = insn_default_latency (dep) - 3;
7805                             else
7806                               *cost = insn_default_latency (dep);
7807                             return false;
7808                           }
7809                         else
7810                           {
7811                             if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
7812                               *cost = insn_default_latency (dep) + 1;
7813                             else
7814                               *cost = insn_default_latency (dep);
7815                           }
7816                         return false;
7817                       }
7818                   }
7819               }
7820           }
7821         break;
7822
7823     default:
7824       gcc_unreachable ();
7825     }
7826
7827   return true;
7828 }
7829
7830 /* This function implements the target macro TARGET_SCHED_ADJUST_COST.
7831    It corrects the value of COST based on the relationship between
7832    INSN and DEP through the dependence LINK.  It returns the new
7833    value. There is a per-core adjust_cost hook to adjust scheduler costs
7834    and the per-core hook can choose to completely override the generic 
7835    adjust_cost function. Only put bits of code into arm_adjust_cost that 
7836    are common across all cores.  */
7837 static int
7838 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
7839 {
7840   rtx i_pat, d_pat;
7841
7842  /* When generating Thumb-1 code, we want to place flag-setting operations
7843     close to a conditional branch which depends on them, so that we can
7844     omit the comparison. */
7845   if (TARGET_THUMB1
7846       && REG_NOTE_KIND (link) == 0
7847       && recog_memoized (insn) == CODE_FOR_cbranchsi4_insn
7848       && recog_memoized (dep) >= 0
7849       && get_attr_conds (dep) == CONDS_SET)
7850     return 0;
7851
7852   if (current_tune->sched_adjust_cost != NULL)
7853     {
7854       if (!current_tune->sched_adjust_cost (insn, link, dep, &cost))
7855         return cost;
7856     }
7857
7858   /* XXX This is not strictly true for the FPA.  */
7859   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
7860       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
7861     return 0;
7862
7863   /* Call insns don't incur a stall, even if they follow a load.  */
7864   if (REG_NOTE_KIND (link) == 0
7865       && GET_CODE (insn) == CALL_INSN)
7866     return 1;
7867
7868   if ((i_pat = single_set (insn)) != NULL
7869       && GET_CODE (SET_SRC (i_pat)) == MEM
7870       && (d_pat = single_set (dep)) != NULL
7871       && GET_CODE (SET_DEST (d_pat)) == MEM)
7872     {
7873       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
7874       /* This is a load after a store, there is no conflict if the load reads
7875          from a cached area.  Assume that loads from the stack, and from the
7876          constant pool are cached, and that others will miss.  This is a
7877          hack.  */
7878
7879       if ((GET_CODE (src_mem) == SYMBOL_REF 
7880            && CONSTANT_POOL_ADDRESS_P (src_mem))
7881           || reg_mentioned_p (stack_pointer_rtx, src_mem)
7882           || reg_mentioned_p (frame_pointer_rtx, src_mem)
7883           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
7884         return 1;
7885     }
7886
7887   return cost;
7888 }
7889
7890 static int fp_consts_inited = 0;
7891
7892 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
7893 static const char * const strings_fp[8] =
7894 {
7895   "0",   "1",   "2",   "3",
7896   "4",   "5",   "0.5", "10"
7897 };
7898
7899 static REAL_VALUE_TYPE values_fp[8];
7900
7901 static void
7902 init_fp_table (void)
7903 {
7904   int i;
7905   REAL_VALUE_TYPE r;
7906
7907   if (TARGET_VFP)
7908     fp_consts_inited = 1;
7909   else
7910     fp_consts_inited = 8;
7911
7912   for (i = 0; i < fp_consts_inited; i++)
7913     {
7914       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
7915       values_fp[i] = r;
7916     }
7917 }
7918
7919 /* Return TRUE if rtx X is a valid immediate FP constant.  */
7920 int
7921 arm_const_double_rtx (rtx x)
7922 {
7923   REAL_VALUE_TYPE r;
7924   int i;
7925
7926   if (!fp_consts_inited)
7927     init_fp_table ();
7928
7929   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7930   if (REAL_VALUE_MINUS_ZERO (r))
7931     return 0;
7932
7933   for (i = 0; i < fp_consts_inited; i++)
7934     if (REAL_VALUES_EQUAL (r, values_fp[i]))
7935       return 1;
7936
7937   return 0;
7938 }
7939
7940 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
7941 int
7942 neg_const_double_rtx_ok_for_fpa (rtx x)
7943 {
7944   REAL_VALUE_TYPE r;
7945   int i;
7946
7947   if (!fp_consts_inited)
7948     init_fp_table ();
7949
7950   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7951   r = real_value_negate (&r);
7952   if (REAL_VALUE_MINUS_ZERO (r))
7953     return 0;
7954
7955   for (i = 0; i < 8; i++)
7956     if (REAL_VALUES_EQUAL (r, values_fp[i]))
7957       return 1;
7958
7959   return 0;
7960 }
7961
7962
7963 /* VFPv3 has a fairly wide range of representable immediates, formed from
7964    "quarter-precision" floating-point values. These can be evaluated using this
7965    formula (with ^ for exponentiation):
7966
7967      -1^s * n * 2^-r
7968
7969    Where 's' is a sign bit (0/1), 'n' and 'r' are integers such that
7970    16 <= n <= 31 and 0 <= r <= 7.
7971
7972    These values are mapped onto an 8-bit integer ABCDEFGH s.t.
7973
7974      - A (most-significant) is the sign bit.
7975      - BCD are the exponent (encoded as r XOR 3).
7976      - EFGH are the mantissa (encoded as n - 16).
7977 */
7978
7979 /* Return an integer index for a VFPv3 immediate operand X suitable for the
7980    fconst[sd] instruction, or -1 if X isn't suitable.  */
7981 static int
7982 vfp3_const_double_index (rtx x)
7983 {
7984   REAL_VALUE_TYPE r, m;
7985   int sign, exponent;
7986   unsigned HOST_WIDE_INT mantissa, mant_hi;
7987   unsigned HOST_WIDE_INT mask;
7988   HOST_WIDE_INT m1, m2;
7989   int point_pos = 2 * HOST_BITS_PER_WIDE_INT - 1;
7990
7991   if (!TARGET_VFP3 || GET_CODE (x) != CONST_DOUBLE)
7992     return -1;
7993
7994   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7995
7996   /* We can't represent these things, so detect them first.  */
7997   if (REAL_VALUE_ISINF (r) || REAL_VALUE_ISNAN (r) || REAL_VALUE_MINUS_ZERO (r))
7998     return -1;
7999
8000   /* Extract sign, exponent and mantissa.  */
8001   sign = REAL_VALUE_NEGATIVE (r) ? 1 : 0;
8002   r = real_value_abs (&r);
8003   exponent = REAL_EXP (&r);
8004   /* For the mantissa, we expand into two HOST_WIDE_INTS, apart from the
8005      highest (sign) bit, with a fixed binary point at bit point_pos.
8006      WARNING: If there's ever a VFP version which uses more than 2 * H_W_I - 1
8007      bits for the mantissa, this may fail (low bits would be lost).  */
8008   real_ldexp (&m, &r, point_pos - exponent);
8009   REAL_VALUE_TO_INT (&m1, &m2, m);
8010   mantissa = m1;
8011   mant_hi = m2;
8012
8013   /* If there are bits set in the low part of the mantissa, we can't
8014      represent this value.  */
8015   if (mantissa != 0)
8016     return -1;
8017
8018   /* Now make it so that mantissa contains the most-significant bits, and move
8019      the point_pos to indicate that the least-significant bits have been
8020      discarded.  */
8021   point_pos -= HOST_BITS_PER_WIDE_INT;
8022   mantissa = mant_hi;
8023
8024   /* We can permit four significant bits of mantissa only, plus a high bit
8025      which is always 1.  */
8026   mask = ((unsigned HOST_WIDE_INT)1 << (point_pos - 5)) - 1;
8027   if ((mantissa & mask) != 0)
8028     return -1;
8029
8030   /* Now we know the mantissa is in range, chop off the unneeded bits.  */
8031   mantissa >>= point_pos - 5;
8032
8033   /* The mantissa may be zero. Disallow that case. (It's possible to load the
8034      floating-point immediate zero with Neon using an integer-zero load, but
8035      that case is handled elsewhere.)  */
8036   if (mantissa == 0)
8037     return -1;
8038
8039   gcc_assert (mantissa >= 16 && mantissa <= 31);
8040
8041   /* The value of 5 here would be 4 if GCC used IEEE754-like encoding (where
8042      normalized significands are in the range [1, 2). (Our mantissa is shifted
8043      left 4 places at this point relative to normalized IEEE754 values).  GCC
8044      internally uses [0.5, 1) (see real.c), so the exponent returned from
8045      REAL_EXP must be altered.  */
8046   exponent = 5 - exponent;
8047
8048   if (exponent < 0 || exponent > 7)
8049     return -1;
8050
8051   /* Sign, mantissa and exponent are now in the correct form to plug into the
8052      formula described in the comment above.  */
8053   return (sign << 7) | ((exponent ^ 3) << 4) | (mantissa - 16);
8054 }
8055
8056 /* Return TRUE if rtx X is a valid immediate VFPv3 constant.  */
8057 int
8058 vfp3_const_double_rtx (rtx x)
8059 {
8060   if (!TARGET_VFP3)
8061     return 0;
8062
8063   return vfp3_const_double_index (x) != -1;
8064 }
8065
8066 /* Recognize immediates which can be used in various Neon instructions. Legal
8067    immediates are described by the following table (for VMVN variants, the
8068    bitwise inverse of the constant shown is recognized. In either case, VMOV
8069    is output and the correct instruction to use for a given constant is chosen
8070    by the assembler). The constant shown is replicated across all elements of
8071    the destination vector.
8072
8073    insn elems variant constant (binary)
8074    ---- ----- ------- -----------------
8075    vmov  i32     0    00000000 00000000 00000000 abcdefgh
8076    vmov  i32     1    00000000 00000000 abcdefgh 00000000
8077    vmov  i32     2    00000000 abcdefgh 00000000 00000000
8078    vmov  i32     3    abcdefgh 00000000 00000000 00000000
8079    vmov  i16     4    00000000 abcdefgh
8080    vmov  i16     5    abcdefgh 00000000
8081    vmvn  i32     6    00000000 00000000 00000000 abcdefgh
8082    vmvn  i32     7    00000000 00000000 abcdefgh 00000000
8083    vmvn  i32     8    00000000 abcdefgh 00000000 00000000
8084    vmvn  i32     9    abcdefgh 00000000 00000000 00000000
8085    vmvn  i16    10    00000000 abcdefgh
8086    vmvn  i16    11    abcdefgh 00000000
8087    vmov  i32    12    00000000 00000000 abcdefgh 11111111
8088    vmvn  i32    13    00000000 00000000 abcdefgh 11111111
8089    vmov  i32    14    00000000 abcdefgh 11111111 11111111
8090    vmvn  i32    15    00000000 abcdefgh 11111111 11111111
8091    vmov   i8    16    abcdefgh
8092    vmov  i64    17    aaaaaaaa bbbbbbbb cccccccc dddddddd
8093                       eeeeeeee ffffffff gggggggg hhhhhhhh
8094    vmov  f32    18    aBbbbbbc defgh000 00000000 00000000
8095
8096    For case 18, B = !b. Representable values are exactly those accepted by
8097    vfp3_const_double_index, but are output as floating-point numbers rather
8098    than indices.
8099
8100    Variants 0-5 (inclusive) may also be used as immediates for the second
8101    operand of VORR/VBIC instructions.
8102
8103    The INVERSE argument causes the bitwise inverse of the given operand to be
8104    recognized instead (used for recognizing legal immediates for the VAND/VORN
8105    pseudo-instructions). If INVERSE is true, the value placed in *MODCONST is
8106    *not* inverted (i.e. the pseudo-instruction forms vand/vorn should still be
8107    output, rather than the real insns vbic/vorr).
8108
8109    INVERSE makes no difference to the recognition of float vectors.
8110
8111    The return value is the variant of immediate as shown in the above table, or
8112    -1 if the given value doesn't match any of the listed patterns.
8113 */
8114 static int
8115 neon_valid_immediate (rtx op, enum machine_mode mode, int inverse,
8116                       rtx *modconst, int *elementwidth)
8117 {
8118 #define CHECK(STRIDE, ELSIZE, CLASS, TEST)      \
8119   matches = 1;                                  \
8120   for (i = 0; i < idx; i += (STRIDE))           \
8121     if (!(TEST))                                \
8122       matches = 0;                              \
8123   if (matches)                                  \
8124     {                                           \
8125       immtype = (CLASS);                        \
8126       elsize = (ELSIZE);                        \
8127       break;                                    \
8128     }
8129
8130   unsigned int i, elsize = 0, idx = 0, n_elts = CONST_VECTOR_NUNITS (op);
8131   unsigned int innersize = GET_MODE_SIZE (GET_MODE_INNER (mode));
8132   unsigned char bytes[16];
8133   int immtype = -1, matches;
8134   unsigned int invmask = inverse ? 0xff : 0;
8135
8136   /* Vectors of float constants.  */
8137   if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
8138     {
8139       rtx el0 = CONST_VECTOR_ELT (op, 0);
8140       REAL_VALUE_TYPE r0;
8141
8142       if (!vfp3_const_double_rtx (el0))
8143         return -1;
8144
8145       REAL_VALUE_FROM_CONST_DOUBLE (r0, el0);
8146
8147       for (i = 1; i < n_elts; i++)
8148         {
8149           rtx elt = CONST_VECTOR_ELT (op, i);
8150           REAL_VALUE_TYPE re;
8151
8152           REAL_VALUE_FROM_CONST_DOUBLE (re, elt);
8153
8154           if (!REAL_VALUES_EQUAL (r0, re))
8155             return -1;
8156         }
8157
8158       if (modconst)
8159         *modconst = CONST_VECTOR_ELT (op, 0);
8160
8161       if (elementwidth)
8162         *elementwidth = 0;
8163
8164       return 18;
8165     }
8166
8167   /* Splat vector constant out into a byte vector.  */
8168   for (i = 0; i < n_elts; i++)
8169     {
8170       rtx el = CONST_VECTOR_ELT (op, i);
8171       unsigned HOST_WIDE_INT elpart;
8172       unsigned int part, parts;
8173
8174       if (GET_CODE (el) == CONST_INT)
8175         {
8176           elpart = INTVAL (el);
8177           parts = 1;
8178         }
8179       else if (GET_CODE (el) == CONST_DOUBLE)
8180         {
8181           elpart = CONST_DOUBLE_LOW (el);
8182           parts = 2;
8183         }
8184       else
8185         gcc_unreachable ();
8186
8187       for (part = 0; part < parts; part++)
8188         {
8189           unsigned int byte;
8190           for (byte = 0; byte < innersize; byte++)
8191             {
8192               bytes[idx++] = (elpart & 0xff) ^ invmask;
8193               elpart >>= BITS_PER_UNIT;
8194             }
8195           if (GET_CODE (el) == CONST_DOUBLE)
8196             elpart = CONST_DOUBLE_HIGH (el);
8197         }
8198     }
8199
8200   /* Sanity check.  */
8201   gcc_assert (idx == GET_MODE_SIZE (mode));
8202
8203   do
8204     {
8205       CHECK (4, 32, 0, bytes[i] == bytes[0] && bytes[i + 1] == 0
8206                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
8207
8208       CHECK (4, 32, 1, bytes[i] == 0 && bytes[i + 1] == bytes[1]
8209                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
8210
8211       CHECK (4, 32, 2, bytes[i] == 0 && bytes[i + 1] == 0
8212                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
8213
8214       CHECK (4, 32, 3, bytes[i] == 0 && bytes[i + 1] == 0
8215                        && bytes[i + 2] == 0 && bytes[i + 3] == bytes[3]);
8216
8217       CHECK (2, 16, 4, bytes[i] == bytes[0] && bytes[i + 1] == 0);
8218
8219       CHECK (2, 16, 5, bytes[i] == 0 && bytes[i + 1] == bytes[1]);
8220
8221       CHECK (4, 32, 6, bytes[i] == bytes[0] && bytes[i + 1] == 0xff
8222                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
8223
8224       CHECK (4, 32, 7, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
8225                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
8226
8227       CHECK (4, 32, 8, bytes[i] == 0xff && bytes[i + 1] == 0xff
8228                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
8229
8230       CHECK (4, 32, 9, bytes[i] == 0xff && bytes[i + 1] == 0xff
8231                        && bytes[i + 2] == 0xff && bytes[i + 3] == bytes[3]);
8232
8233       CHECK (2, 16, 10, bytes[i] == bytes[0] && bytes[i + 1] == 0xff);
8234
8235       CHECK (2, 16, 11, bytes[i] == 0xff && bytes[i + 1] == bytes[1]);
8236
8237       CHECK (4, 32, 12, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
8238                         && bytes[i + 2] == 0 && bytes[i + 3] == 0);
8239
8240       CHECK (4, 32, 13, bytes[i] == 0 && bytes[i + 1] == bytes[1]
8241                         && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
8242
8243       CHECK (4, 32, 14, bytes[i] == 0xff && bytes[i + 1] == 0xff
8244                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
8245
8246       CHECK (4, 32, 15, bytes[i] == 0 && bytes[i + 1] == 0
8247                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
8248
8249       CHECK (1, 8, 16, bytes[i] == bytes[0]);
8250
8251       CHECK (1, 64, 17, (bytes[i] == 0 || bytes[i] == 0xff)
8252                         && bytes[i] == bytes[(i + 8) % idx]);
8253     }
8254   while (0);
8255
8256   if (immtype == -1)
8257     return -1;
8258
8259   if (elementwidth)
8260     *elementwidth = elsize;
8261
8262   if (modconst)
8263     {
8264       unsigned HOST_WIDE_INT imm = 0;
8265
8266       /* Un-invert bytes of recognized vector, if necessary.  */
8267       if (invmask != 0)
8268         for (i = 0; i < idx; i++)
8269           bytes[i] ^= invmask;
8270
8271       if (immtype == 17)
8272         {
8273           /* FIXME: Broken on 32-bit H_W_I hosts.  */
8274           gcc_assert (sizeof (HOST_WIDE_INT) == 8);
8275
8276           for (i = 0; i < 8; i++)
8277             imm |= (unsigned HOST_WIDE_INT) (bytes[i] ? 0xff : 0)
8278                    << (i * BITS_PER_UNIT);
8279
8280           *modconst = GEN_INT (imm);
8281         }
8282       else
8283         {
8284           unsigned HOST_WIDE_INT imm = 0;
8285
8286           for (i = 0; i < elsize / BITS_PER_UNIT; i++)
8287             imm |= (unsigned HOST_WIDE_INT) bytes[i] << (i * BITS_PER_UNIT);
8288
8289           *modconst = GEN_INT (imm);
8290         }
8291     }
8292
8293   return immtype;
8294 #undef CHECK
8295 }
8296
8297 /* Return TRUE if rtx X is legal for use as either a Neon VMOV (or, implicitly,
8298    VMVN) immediate. Write back width per element to *ELEMENTWIDTH (or zero for
8299    float elements), and a modified constant (whatever should be output for a
8300    VMOV) in *MODCONST.  */
8301
8302 int
8303 neon_immediate_valid_for_move (rtx op, enum machine_mode mode,
8304                                rtx *modconst, int *elementwidth)
8305 {
8306   rtx tmpconst;
8307   int tmpwidth;
8308   int retval = neon_valid_immediate (op, mode, 0, &tmpconst, &tmpwidth);
8309
8310   if (retval == -1)
8311     return 0;
8312
8313   if (modconst)
8314     *modconst = tmpconst;
8315
8316   if (elementwidth)
8317     *elementwidth = tmpwidth;
8318
8319   return 1;
8320 }
8321
8322 /* Return TRUE if rtx X is legal for use in a VORR or VBIC instruction.  If
8323    the immediate is valid, write a constant suitable for using as an operand
8324    to VORR/VBIC/VAND/VORN to *MODCONST and the corresponding element width to
8325    *ELEMENTWIDTH. See neon_valid_immediate for description of INVERSE.  */
8326
8327 int
8328 neon_immediate_valid_for_logic (rtx op, enum machine_mode mode, int inverse,
8329                                 rtx *modconst, int *elementwidth)
8330 {
8331   rtx tmpconst;
8332   int tmpwidth;
8333   int retval = neon_valid_immediate (op, mode, inverse, &tmpconst, &tmpwidth);
8334
8335   if (retval < 0 || retval > 5)
8336     return 0;
8337
8338   if (modconst)
8339     *modconst = tmpconst;
8340
8341   if (elementwidth)
8342     *elementwidth = tmpwidth;
8343
8344   return 1;
8345 }
8346
8347 /* Return a string suitable for output of Neon immediate logic operation
8348    MNEM.  */
8349
8350 char *
8351 neon_output_logic_immediate (const char *mnem, rtx *op2, enum machine_mode mode,
8352                              int inverse, int quad)
8353 {
8354   int width, is_valid;
8355   static char templ[40];
8356
8357   is_valid = neon_immediate_valid_for_logic (*op2, mode, inverse, op2, &width);
8358
8359   gcc_assert (is_valid != 0);
8360
8361   if (quad)
8362     sprintf (templ, "%s.i%d\t%%q0, %%2", mnem, width);
8363   else
8364     sprintf (templ, "%s.i%d\t%%P0, %%2", mnem, width);
8365
8366   return templ;
8367 }
8368
8369 /* Output a sequence of pairwise operations to implement a reduction.
8370    NOTE: We do "too much work" here, because pairwise operations work on two
8371    registers-worth of operands in one go. Unfortunately we can't exploit those
8372    extra calculations to do the full operation in fewer steps, I don't think.
8373    Although all vector elements of the result but the first are ignored, we
8374    actually calculate the same result in each of the elements. An alternative
8375    such as initially loading a vector with zero to use as each of the second
8376    operands would use up an additional register and take an extra instruction,
8377    for no particular gain.  */
8378
8379 void
8380 neon_pairwise_reduce (rtx op0, rtx op1, enum machine_mode mode,
8381                       rtx (*reduc) (rtx, rtx, rtx))
8382 {
8383   enum machine_mode inner = GET_MODE_INNER (mode);
8384   unsigned int i, parts = GET_MODE_SIZE (mode) / GET_MODE_SIZE (inner);
8385   rtx tmpsum = op1;
8386
8387   for (i = parts / 2; i >= 1; i /= 2)
8388     {
8389       rtx dest = (i == 1) ? op0 : gen_reg_rtx (mode);
8390       emit_insn (reduc (dest, tmpsum, tmpsum));
8391       tmpsum = dest;
8392     }
8393 }
8394
8395 /* If VALS is a vector constant that can be loaded into a register
8396    using VDUP, generate instructions to do so and return an RTX to
8397    assign to the register.  Otherwise return NULL_RTX.  */
8398
8399 static rtx
8400 neon_vdup_constant (rtx vals)
8401 {
8402   enum machine_mode mode = GET_MODE (vals);
8403   enum machine_mode inner_mode = GET_MODE_INNER (mode);
8404   int n_elts = GET_MODE_NUNITS (mode);
8405   bool all_same = true;
8406   rtx x;
8407   int i;
8408
8409   if (GET_CODE (vals) != CONST_VECTOR || GET_MODE_SIZE (inner_mode) > 4)
8410     return NULL_RTX;
8411
8412   for (i = 0; i < n_elts; ++i)
8413     {
8414       x = XVECEXP (vals, 0, i);
8415       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
8416         all_same = false;
8417     }
8418
8419   if (!all_same)
8420     /* The elements are not all the same.  We could handle repeating
8421        patterns of a mode larger than INNER_MODE here (e.g. int8x8_t
8422        {0, C, 0, C, 0, C, 0, C} which can be loaded using
8423        vdup.i16).  */
8424     return NULL_RTX;
8425
8426   /* We can load this constant by using VDUP and a constant in a
8427      single ARM register.  This will be cheaper than a vector
8428      load.  */
8429
8430   x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, 0));
8431   return gen_rtx_VEC_DUPLICATE (mode, x);
8432 }
8433
8434 /* Generate code to load VALS, which is a PARALLEL containing only
8435    constants (for vec_init) or CONST_VECTOR, efficiently into a
8436    register.  Returns an RTX to copy into the register, or NULL_RTX
8437    for a PARALLEL that can not be converted into a CONST_VECTOR.  */
8438
8439 rtx
8440 neon_make_constant (rtx vals)
8441 {
8442   enum machine_mode mode = GET_MODE (vals);
8443   rtx target;
8444   rtx const_vec = NULL_RTX;
8445   int n_elts = GET_MODE_NUNITS (mode);
8446   int n_const = 0;
8447   int i;
8448
8449   if (GET_CODE (vals) == CONST_VECTOR)
8450     const_vec = vals;
8451   else if (GET_CODE (vals) == PARALLEL)
8452     {
8453       /* A CONST_VECTOR must contain only CONST_INTs and
8454          CONST_DOUBLEs, but CONSTANT_P allows more (e.g. SYMBOL_REF).
8455          Only store valid constants in a CONST_VECTOR.  */
8456       for (i = 0; i < n_elts; ++i)
8457         {
8458           rtx x = XVECEXP (vals, 0, i);
8459           if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
8460             n_const++;
8461         }
8462       if (n_const == n_elts)
8463         const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
8464     }
8465   else
8466     gcc_unreachable ();
8467
8468   if (const_vec != NULL
8469       && neon_immediate_valid_for_move (const_vec, mode, NULL, NULL))
8470     /* Load using VMOV.  On Cortex-A8 this takes one cycle.  */
8471     return const_vec;
8472   else if ((target = neon_vdup_constant (vals)) != NULL_RTX)
8473     /* Loaded using VDUP.  On Cortex-A8 the VDUP takes one NEON
8474        pipeline cycle; creating the constant takes one or two ARM
8475        pipeline cycles.  */
8476     return target;
8477   else if (const_vec != NULL_RTX)
8478     /* Load from constant pool.  On Cortex-A8 this takes two cycles
8479        (for either double or quad vectors).  We can not take advantage
8480        of single-cycle VLD1 because we need a PC-relative addressing
8481        mode.  */
8482     return const_vec;
8483   else
8484     /* A PARALLEL containing something not valid inside CONST_VECTOR.
8485        We can not construct an initializer.  */
8486     return NULL_RTX;
8487 }
8488
8489 /* Initialize vector TARGET to VALS.  */
8490
8491 void
8492 neon_expand_vector_init (rtx target, rtx vals)
8493 {
8494   enum machine_mode mode = GET_MODE (target);
8495   enum machine_mode inner_mode = GET_MODE_INNER (mode);
8496   int n_elts = GET_MODE_NUNITS (mode);
8497   int n_var = 0, one_var = -1;
8498   bool all_same = true;
8499   rtx x, mem;
8500   int i;
8501
8502   for (i = 0; i < n_elts; ++i)
8503     {
8504       x = XVECEXP (vals, 0, i);
8505       if (!CONSTANT_P (x))
8506         ++n_var, one_var = i;
8507
8508       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
8509         all_same = false;
8510     }
8511
8512   if (n_var == 0)
8513     {
8514       rtx constant = neon_make_constant (vals);
8515       if (constant != NULL_RTX)
8516         {
8517           emit_move_insn (target, constant);
8518           return;
8519         }
8520     }
8521
8522   /* Splat a single non-constant element if we can.  */
8523   if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
8524     {
8525       x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, 0));
8526       emit_insn (gen_rtx_SET (VOIDmode, target,
8527                               gen_rtx_VEC_DUPLICATE (mode, x)));
8528       return;
8529     }
8530
8531   /* One field is non-constant.  Load constant then overwrite varying
8532      field.  This is more efficient than using the stack.  */
8533   if (n_var == 1)
8534     {
8535       rtx copy = copy_rtx (vals);
8536       rtx index = GEN_INT (one_var);
8537
8538       /* Load constant part of vector, substitute neighboring value for
8539          varying element.  */
8540       XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
8541       neon_expand_vector_init (target, copy);
8542
8543       /* Insert variable.  */
8544       x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, one_var));
8545       switch (mode)
8546         {
8547         case V8QImode:
8548           emit_insn (gen_neon_vset_lanev8qi (target, x, target, index));
8549           break;
8550         case V16QImode:
8551           emit_insn (gen_neon_vset_lanev16qi (target, x, target, index));
8552           break;
8553         case V4HImode:
8554           emit_insn (gen_neon_vset_lanev4hi (target, x, target, index));
8555           break;
8556         case V8HImode:
8557           emit_insn (gen_neon_vset_lanev8hi (target, x, target, index));
8558           break;
8559         case V2SImode:
8560           emit_insn (gen_neon_vset_lanev2si (target, x, target, index));
8561           break;
8562         case V4SImode:
8563           emit_insn (gen_neon_vset_lanev4si (target, x, target, index));
8564           break;
8565         case V2SFmode:
8566           emit_insn (gen_neon_vset_lanev2sf (target, x, target, index));
8567           break;
8568         case V4SFmode:
8569           emit_insn (gen_neon_vset_lanev4sf (target, x, target, index));
8570           break;
8571         case V2DImode:
8572           emit_insn (gen_neon_vset_lanev2di (target, x, target, index));
8573           break;
8574         default:
8575           gcc_unreachable ();
8576         }
8577       return;
8578     }
8579
8580   /* Construct the vector in memory one field at a time
8581      and load the whole vector.  */
8582   mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), 0);
8583   for (i = 0; i < n_elts; i++)
8584     emit_move_insn (adjust_address_nv (mem, inner_mode,
8585                                     i * GET_MODE_SIZE (inner_mode)),
8586                     XVECEXP (vals, 0, i));
8587   emit_move_insn (target, mem);
8588 }
8589
8590 /* Ensure OPERAND lies between LOW (inclusive) and HIGH (exclusive).  Raise
8591    ERR if it doesn't.  FIXME: NEON bounds checks occur late in compilation, so
8592    reported source locations are bogus.  */
8593
8594 static void
8595 bounds_check (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high,
8596               const char *err)
8597 {
8598   HOST_WIDE_INT lane;
8599
8600   gcc_assert (GET_CODE (operand) == CONST_INT);
8601
8602   lane = INTVAL (operand);
8603
8604   if (lane < low || lane >= high)
8605     error (err);
8606 }
8607
8608 /* Bounds-check lanes.  */
8609
8610 void
8611 neon_lane_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
8612 {
8613   bounds_check (operand, low, high, "lane out of range");
8614 }
8615
8616 /* Bounds-check constants.  */
8617
8618 void
8619 neon_const_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
8620 {
8621   bounds_check (operand, low, high, "constant out of range");
8622 }
8623
8624 HOST_WIDE_INT
8625 neon_element_bits (enum machine_mode mode)
8626 {
8627   if (mode == DImode)
8628     return GET_MODE_BITSIZE (mode);
8629   else
8630     return GET_MODE_BITSIZE (GET_MODE_INNER (mode));
8631 }
8632
8633 \f
8634 /* Predicates for `match_operand' and `match_operator'.  */
8635
8636 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
8637 int
8638 cirrus_memory_offset (rtx op)
8639 {
8640   /* Reject eliminable registers.  */
8641   if (! (reload_in_progress || reload_completed)
8642       && (   reg_mentioned_p (frame_pointer_rtx, op)
8643           || reg_mentioned_p (arg_pointer_rtx, op)
8644           || reg_mentioned_p (virtual_incoming_args_rtx, op)
8645           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8646           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8647           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8648     return 0;
8649
8650   if (GET_CODE (op) == MEM)
8651     {
8652       rtx ind;
8653
8654       ind = XEXP (op, 0);
8655
8656       /* Match: (mem (reg)).  */
8657       if (GET_CODE (ind) == REG)
8658         return 1;
8659
8660       /* Match:
8661          (mem (plus (reg)
8662                     (const))).  */
8663       if (GET_CODE (ind) == PLUS
8664           && GET_CODE (XEXP (ind, 0)) == REG
8665           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
8666           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
8667         return 1;
8668     }
8669
8670   return 0;
8671 }
8672
8673 /* Return TRUE if OP is a valid coprocessor memory address pattern.
8674    WB is true if full writeback address modes are allowed and is false
8675    if limited writeback address modes (POST_INC and PRE_DEC) are
8676    allowed.  */
8677
8678 int
8679 arm_coproc_mem_operand (rtx op, bool wb)
8680 {
8681   rtx ind;
8682
8683   /* Reject eliminable registers.  */
8684   if (! (reload_in_progress || reload_completed)
8685       && (   reg_mentioned_p (frame_pointer_rtx, op)
8686           || reg_mentioned_p (arg_pointer_rtx, op)
8687           || reg_mentioned_p (virtual_incoming_args_rtx, op)
8688           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8689           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8690           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8691     return FALSE;
8692
8693   /* Constants are converted into offsets from labels.  */
8694   if (GET_CODE (op) != MEM)
8695     return FALSE;
8696
8697   ind = XEXP (op, 0);
8698
8699   if (reload_completed
8700       && (GET_CODE (ind) == LABEL_REF
8701           || (GET_CODE (ind) == CONST
8702               && GET_CODE (XEXP (ind, 0)) == PLUS
8703               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
8704               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
8705     return TRUE;
8706
8707   /* Match: (mem (reg)).  */
8708   if (GET_CODE (ind) == REG)
8709     return arm_address_register_rtx_p (ind, 0);
8710
8711   /* Autoincremment addressing modes.  POST_INC and PRE_DEC are
8712      acceptable in any case (subject to verification by
8713      arm_address_register_rtx_p).  We need WB to be true to accept
8714      PRE_INC and POST_DEC.  */
8715   if (GET_CODE (ind) == POST_INC
8716       || GET_CODE (ind) == PRE_DEC
8717       || (wb
8718           && (GET_CODE (ind) == PRE_INC
8719               || GET_CODE (ind) == POST_DEC)))
8720     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
8721
8722   if (wb
8723       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
8724       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
8725       && GET_CODE (XEXP (ind, 1)) == PLUS
8726       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
8727     ind = XEXP (ind, 1);
8728
8729   /* Match:
8730      (plus (reg)
8731            (const)).  */
8732   if (GET_CODE (ind) == PLUS
8733       && GET_CODE (XEXP (ind, 0)) == REG
8734       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
8735       && GET_CODE (XEXP (ind, 1)) == CONST_INT
8736       && INTVAL (XEXP (ind, 1)) > -1024
8737       && INTVAL (XEXP (ind, 1)) <  1024
8738       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
8739     return TRUE;
8740
8741   return FALSE;
8742 }
8743
8744 /* Return TRUE if OP is a memory operand which we can load or store a vector
8745    to/from. TYPE is one of the following values:
8746     0 - Vector load/stor (vldr)
8747     1 - Core registers (ldm)
8748     2 - Element/structure loads (vld1)
8749  */
8750 int
8751 neon_vector_mem_operand (rtx op, int type)
8752 {
8753   rtx ind;
8754
8755   /* Reject eliminable registers.  */
8756   if (! (reload_in_progress || reload_completed)
8757       && (   reg_mentioned_p (frame_pointer_rtx, op)
8758           || reg_mentioned_p (arg_pointer_rtx, op)
8759           || reg_mentioned_p (virtual_incoming_args_rtx, op)
8760           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8761           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8762           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8763     return FALSE;
8764
8765   /* Constants are converted into offsets from labels.  */
8766   if (GET_CODE (op) != MEM)
8767     return FALSE;
8768
8769   ind = XEXP (op, 0);
8770
8771   if (reload_completed
8772       && (GET_CODE (ind) == LABEL_REF
8773           || (GET_CODE (ind) == CONST
8774               && GET_CODE (XEXP (ind, 0)) == PLUS
8775               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
8776               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
8777     return TRUE;
8778
8779   /* Match: (mem (reg)).  */
8780   if (GET_CODE (ind) == REG)
8781     return arm_address_register_rtx_p (ind, 0);
8782
8783   /* Allow post-increment with Neon registers.  */
8784   if (type != 1 && (GET_CODE (ind) == POST_INC || GET_CODE (ind) == PRE_DEC))
8785     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
8786
8787   /* FIXME: vld1 allows register post-modify.  */
8788
8789   /* Match:
8790      (plus (reg)
8791           (const)).  */
8792   if (type == 0
8793       && GET_CODE (ind) == PLUS
8794       && GET_CODE (XEXP (ind, 0)) == REG
8795       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
8796       && GET_CODE (XEXP (ind, 1)) == CONST_INT
8797       && INTVAL (XEXP (ind, 1)) > -1024
8798       && INTVAL (XEXP (ind, 1)) < 1016
8799       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
8800     return TRUE;
8801
8802   return FALSE;
8803 }
8804
8805 /* Return TRUE if OP is a mem suitable for loading/storing a Neon struct
8806    type.  */
8807 int
8808 neon_struct_mem_operand (rtx op)
8809 {
8810   rtx ind;
8811
8812   /* Reject eliminable registers.  */
8813   if (! (reload_in_progress || reload_completed)
8814       && (   reg_mentioned_p (frame_pointer_rtx, op)
8815           || reg_mentioned_p (arg_pointer_rtx, op)
8816           || reg_mentioned_p (virtual_incoming_args_rtx, op)
8817           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8818           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8819           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8820     return FALSE;
8821
8822   /* Constants are converted into offsets from labels.  */
8823   if (GET_CODE (op) != MEM)
8824     return FALSE;
8825
8826   ind = XEXP (op, 0);
8827
8828   if (reload_completed
8829       && (GET_CODE (ind) == LABEL_REF
8830           || (GET_CODE (ind) == CONST
8831               && GET_CODE (XEXP (ind, 0)) == PLUS
8832               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
8833               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
8834     return TRUE;
8835
8836   /* Match: (mem (reg)).  */
8837   if (GET_CODE (ind) == REG)
8838     return arm_address_register_rtx_p (ind, 0);
8839
8840   return FALSE;
8841 }
8842
8843 /* Return true if X is a register that will be eliminated later on.  */
8844 int
8845 arm_eliminable_register (rtx x)
8846 {
8847   return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
8848                        || REGNO (x) == ARG_POINTER_REGNUM
8849                        || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
8850                            && REGNO (x) <= LAST_VIRTUAL_REGISTER));
8851 }
8852
8853 /* Return GENERAL_REGS if a scratch register required to reload x to/from
8854    coprocessor registers.  Otherwise return NO_REGS.  */
8855
8856 enum reg_class
8857 coproc_secondary_reload_class (enum machine_mode mode, rtx x, bool wb)
8858 {
8859   if (mode == HFmode)
8860     {
8861       if (!TARGET_NEON_FP16)
8862         return GENERAL_REGS;
8863       if (s_register_operand (x, mode) || neon_vector_mem_operand (x, 2))
8864         return NO_REGS;
8865       return GENERAL_REGS;
8866     }
8867
8868   if (TARGET_NEON
8869       && (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
8870           || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
8871       && neon_vector_mem_operand (x, 0))
8872      return NO_REGS;
8873
8874   if (arm_coproc_mem_operand (x, wb) || s_register_operand (x, mode))
8875     return NO_REGS;
8876
8877   return GENERAL_REGS;
8878 }
8879
8880 /* Values which must be returned in the most-significant end of the return
8881    register.  */
8882
8883 static bool
8884 arm_return_in_msb (const_tree valtype)
8885 {
8886   return (TARGET_AAPCS_BASED
8887           && BYTES_BIG_ENDIAN
8888           && (AGGREGATE_TYPE_P (valtype)
8889               || TREE_CODE (valtype) == COMPLEX_TYPE));
8890 }
8891
8892 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
8893    Use by the Cirrus Maverick code which has to workaround
8894    a hardware bug triggered by such instructions.  */
8895 static bool
8896 arm_memory_load_p (rtx insn)
8897 {
8898   rtx body, lhs, rhs;;
8899
8900   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
8901     return false;
8902
8903   body = PATTERN (insn);
8904
8905   if (GET_CODE (body) != SET)
8906     return false;
8907
8908   lhs = XEXP (body, 0);
8909   rhs = XEXP (body, 1);
8910
8911   lhs = REG_OR_SUBREG_RTX (lhs);
8912
8913   /* If the destination is not a general purpose
8914      register we do not have to worry.  */
8915   if (GET_CODE (lhs) != REG
8916       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
8917     return false;
8918
8919   /* As well as loads from memory we also have to react
8920      to loads of invalid constants which will be turned
8921      into loads from the minipool.  */
8922   return (GET_CODE (rhs) == MEM
8923           || GET_CODE (rhs) == SYMBOL_REF
8924           || note_invalid_constants (insn, -1, false));
8925 }
8926
8927 /* Return TRUE if INSN is a Cirrus instruction.  */
8928 static bool
8929 arm_cirrus_insn_p (rtx insn)
8930 {
8931   enum attr_cirrus attr;
8932
8933   /* get_attr cannot accept USE or CLOBBER.  */
8934   if (!insn
8935       || GET_CODE (insn) != INSN
8936       || GET_CODE (PATTERN (insn)) == USE
8937       || GET_CODE (PATTERN (insn)) == CLOBBER)
8938     return 0;
8939
8940   attr = get_attr_cirrus (insn);
8941
8942   return attr != CIRRUS_NOT;
8943 }
8944
8945 /* Cirrus reorg for invalid instruction combinations.  */
8946 static void
8947 cirrus_reorg (rtx first)
8948 {
8949   enum attr_cirrus attr;
8950   rtx body = PATTERN (first);
8951   rtx t;
8952   int nops;
8953
8954   /* Any branch must be followed by 2 non Cirrus instructions.  */
8955   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
8956     {
8957       nops = 0;
8958       t = next_nonnote_insn (first);
8959
8960       if (arm_cirrus_insn_p (t))
8961         ++ nops;
8962
8963       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
8964         ++ nops;
8965
8966       while (nops --)
8967         emit_insn_after (gen_nop (), first);
8968
8969       return;
8970     }
8971
8972   /* (float (blah)) is in parallel with a clobber.  */
8973   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
8974     body = XVECEXP (body, 0, 0);
8975
8976   if (GET_CODE (body) == SET)
8977     {
8978       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
8979
8980       /* cfldrd, cfldr64, cfstrd, cfstr64 must
8981          be followed by a non Cirrus insn.  */
8982       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
8983         {
8984           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
8985             emit_insn_after (gen_nop (), first);
8986
8987           return;
8988         }
8989       else if (arm_memory_load_p (first))
8990         {
8991           unsigned int arm_regno;
8992
8993           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
8994              ldr/cfmv64hr combination where the Rd field is the same
8995              in both instructions must be split with a non Cirrus
8996              insn.  Example:
8997
8998              ldr r0, blah
8999              nop
9000              cfmvsr mvf0, r0.  */
9001
9002           /* Get Arm register number for ldr insn.  */
9003           if (GET_CODE (lhs) == REG)
9004             arm_regno = REGNO (lhs);
9005           else
9006             {
9007               gcc_assert (GET_CODE (rhs) == REG);
9008               arm_regno = REGNO (rhs);
9009             }
9010
9011           /* Next insn.  */
9012           first = next_nonnote_insn (first);
9013
9014           if (! arm_cirrus_insn_p (first))
9015             return;
9016
9017           body = PATTERN (first);
9018
9019           /* (float (blah)) is in parallel with a clobber.  */
9020           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
9021             body = XVECEXP (body, 0, 0);
9022
9023           if (GET_CODE (body) == FLOAT)
9024             body = XEXP (body, 0);
9025
9026           if (get_attr_cirrus (first) == CIRRUS_MOVE
9027               && GET_CODE (XEXP (body, 1)) == REG
9028               && arm_regno == REGNO (XEXP (body, 1)))
9029             emit_insn_after (gen_nop (), first);
9030
9031           return;
9032         }
9033     }
9034
9035   /* get_attr cannot accept USE or CLOBBER.  */
9036   if (!first
9037       || GET_CODE (first) != INSN
9038       || GET_CODE (PATTERN (first)) == USE
9039       || GET_CODE (PATTERN (first)) == CLOBBER)
9040     return;
9041
9042   attr = get_attr_cirrus (first);
9043
9044   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
9045      must be followed by a non-coprocessor instruction.  */
9046   if (attr == CIRRUS_COMPARE)
9047     {
9048       nops = 0;
9049
9050       t = next_nonnote_insn (first);
9051
9052       if (arm_cirrus_insn_p (t))
9053         ++ nops;
9054
9055       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
9056         ++ nops;
9057
9058       while (nops --)
9059         emit_insn_after (gen_nop (), first);
9060
9061       return;
9062     }
9063 }
9064
9065 /* Return TRUE if X references a SYMBOL_REF.  */
9066 int
9067 symbol_mentioned_p (rtx x)
9068 {
9069   const char * fmt;
9070   int i;
9071
9072   if (GET_CODE (x) == SYMBOL_REF)
9073     return 1;
9074
9075   /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
9076      are constant offsets, not symbols.  */
9077   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
9078     return 0;
9079
9080   fmt = GET_RTX_FORMAT (GET_CODE (x));
9081
9082   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
9083     {
9084       if (fmt[i] == 'E')
9085         {
9086           int j;
9087
9088           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9089             if (symbol_mentioned_p (XVECEXP (x, i, j)))
9090               return 1;
9091         }
9092       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
9093         return 1;
9094     }
9095
9096   return 0;
9097 }
9098
9099 /* Return TRUE if X references a LABEL_REF.  */
9100 int
9101 label_mentioned_p (rtx x)
9102 {
9103   const char * fmt;
9104   int i;
9105
9106   if (GET_CODE (x) == LABEL_REF)
9107     return 1;
9108
9109   /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
9110      instruction, but they are constant offsets, not symbols.  */
9111   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
9112     return 0;
9113
9114   fmt = GET_RTX_FORMAT (GET_CODE (x));
9115   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
9116     {
9117       if (fmt[i] == 'E')
9118         {
9119           int j;
9120
9121           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9122             if (label_mentioned_p (XVECEXP (x, i, j)))
9123               return 1;
9124         }
9125       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
9126         return 1;
9127     }
9128
9129   return 0;
9130 }
9131
9132 int
9133 tls_mentioned_p (rtx x)
9134 {
9135   switch (GET_CODE (x))
9136     {
9137     case CONST:
9138       return tls_mentioned_p (XEXP (x, 0));
9139
9140     case UNSPEC:
9141       if (XINT (x, 1) == UNSPEC_TLS)
9142         return 1;
9143
9144     default:
9145       return 0;
9146     }
9147 }
9148
9149 /* Must not copy any rtx that uses a pc-relative address.  */
9150
9151 static int
9152 arm_note_pic_base (rtx *x, void *date ATTRIBUTE_UNUSED)
9153 {
9154   if (GET_CODE (*x) == UNSPEC
9155       && XINT (*x, 1) == UNSPEC_PIC_BASE)
9156     return 1;
9157   return 0;
9158 }
9159
9160 static bool
9161 arm_cannot_copy_insn_p (rtx insn)
9162 {
9163   return for_each_rtx (&PATTERN (insn), arm_note_pic_base, NULL);
9164 }
9165
9166 enum rtx_code
9167 minmax_code (rtx x)
9168 {
9169   enum rtx_code code = GET_CODE (x);
9170
9171   switch (code)
9172     {
9173     case SMAX:
9174       return GE;
9175     case SMIN:
9176       return LE;
9177     case UMIN:
9178       return LEU;
9179     case UMAX:
9180       return GEU;
9181     default:
9182       gcc_unreachable ();
9183     }
9184 }
9185
9186 /* Return 1 if memory locations are adjacent.  */
9187 int
9188 adjacent_mem_locations (rtx a, rtx b)
9189 {
9190   /* We don't guarantee to preserve the order of these memory refs.  */
9191   if (volatile_refs_p (a) || volatile_refs_p (b))
9192     return 0;
9193
9194   if ((GET_CODE (XEXP (a, 0)) == REG
9195        || (GET_CODE (XEXP (a, 0)) == PLUS
9196            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
9197       && (GET_CODE (XEXP (b, 0)) == REG
9198           || (GET_CODE (XEXP (b, 0)) == PLUS
9199               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
9200     {
9201       HOST_WIDE_INT val0 = 0, val1 = 0;
9202       rtx reg0, reg1;
9203       int val_diff;
9204
9205       if (GET_CODE (XEXP (a, 0)) == PLUS)
9206         {
9207           reg0 = XEXP (XEXP (a, 0), 0);
9208           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
9209         }
9210       else
9211         reg0 = XEXP (a, 0);
9212
9213       if (GET_CODE (XEXP (b, 0)) == PLUS)
9214         {
9215           reg1 = XEXP (XEXP (b, 0), 0);
9216           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
9217         }
9218       else
9219         reg1 = XEXP (b, 0);
9220
9221       /* Don't accept any offset that will require multiple
9222          instructions to handle, since this would cause the
9223          arith_adjacentmem pattern to output an overlong sequence.  */
9224       if (!const_ok_for_op (val0, PLUS) || !const_ok_for_op (val1, PLUS))
9225         return 0;
9226
9227       /* Don't allow an eliminable register: register elimination can make
9228          the offset too large.  */
9229       if (arm_eliminable_register (reg0))
9230         return 0;
9231
9232       val_diff = val1 - val0;
9233
9234       if (arm_ld_sched)
9235         {
9236           /* If the target has load delay slots, then there's no benefit
9237              to using an ldm instruction unless the offset is zero and
9238              we are optimizing for size.  */
9239           return (optimize_size && (REGNO (reg0) == REGNO (reg1))
9240                   && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
9241                   && (val_diff == 4 || val_diff == -4));
9242         }
9243
9244       return ((REGNO (reg0) == REGNO (reg1))
9245               && (val_diff == 4 || val_diff == -4));
9246     }
9247
9248   return 0;
9249 }
9250
9251 /* Return true iff it would be profitable to turn a sequence of NOPS loads
9252    or stores (depending on IS_STORE) into a load-multiple or store-multiple
9253    instruction.  ADD_OFFSET is nonzero if the base address register needs
9254    to be modified with an add instruction before we can use it.  */
9255
9256 static bool
9257 multiple_operation_profitable_p (bool is_store ATTRIBUTE_UNUSED,
9258                                  int nops, HOST_WIDE_INT add_offset)
9259  {
9260   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
9261      if the offset isn't small enough.  The reason 2 ldrs are faster
9262      is because these ARMs are able to do more than one cache access
9263      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
9264      whilst the ARM8 has a double bandwidth cache.  This means that
9265      these cores can do both an instruction fetch and a data fetch in
9266      a single cycle, so the trick of calculating the address into a
9267      scratch register (one of the result regs) and then doing a load
9268      multiple actually becomes slower (and no smaller in code size).
9269      That is the transformation
9270
9271         ldr     rd1, [rbase + offset]
9272         ldr     rd2, [rbase + offset + 4]
9273
9274      to
9275
9276         add     rd1, rbase, offset
9277         ldmia   rd1, {rd1, rd2}
9278
9279      produces worse code -- '3 cycles + any stalls on rd2' instead of
9280      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
9281      access per cycle, the first sequence could never complete in less
9282      than 6 cycles, whereas the ldm sequence would only take 5 and
9283      would make better use of sequential accesses if not hitting the
9284      cache.
9285
9286      We cheat here and test 'arm_ld_sched' which we currently know to
9287      only be true for the ARM8, ARM9 and StrongARM.  If this ever
9288      changes, then the test below needs to be reworked.  */
9289   if (nops == 2 && arm_ld_sched && add_offset != 0)
9290     return false;
9291
9292   /* XScale has load-store double instructions, but they have stricter
9293      alignment requirements than load-store multiple, so we cannot
9294      use them.
9295
9296      For XScale ldm requires 2 + NREGS cycles to complete and blocks
9297      the pipeline until completion.
9298
9299         NREGS           CYCLES
9300           1               3
9301           2               4
9302           3               5
9303           4               6
9304
9305      An ldr instruction takes 1-3 cycles, but does not block the
9306      pipeline.
9307
9308         NREGS           CYCLES
9309           1              1-3
9310           2              2-6
9311           3              3-9
9312           4              4-12
9313
9314      Best case ldr will always win.  However, the more ldr instructions
9315      we issue, the less likely we are to be able to schedule them well.
9316      Using ldr instructions also increases code size.
9317
9318      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
9319      for counts of 3 or 4 regs.  */
9320   if (nops <= 2 && arm_tune_xscale && !optimize_size)
9321     return false;
9322   return true;
9323 }
9324
9325 /* Subroutine of load_multiple_sequence and store_multiple_sequence.
9326    Given an array of UNSORTED_OFFSETS, of which there are NOPS, compute
9327    an array ORDER which describes the sequence to use when accessing the
9328    offsets that produces an ascending order.  In this sequence, each
9329    offset must be larger by exactly 4 than the previous one.  ORDER[0]
9330    must have been filled in with the lowest offset by the caller.
9331    If UNSORTED_REGS is nonnull, it is an array of register numbers that
9332    we use to verify that ORDER produces an ascending order of registers.
9333    Return true if it was possible to construct such an order, false if
9334    not.  */
9335
9336 static bool
9337 compute_offset_order (int nops, HOST_WIDE_INT *unsorted_offsets, int *order,
9338                       int *unsorted_regs)
9339 {
9340   int i;
9341   for (i = 1; i < nops; i++)
9342     {
9343       int j;
9344
9345       order[i] = order[i - 1];
9346       for (j = 0; j < nops; j++)
9347         if (unsorted_offsets[j] == unsorted_offsets[order[i - 1]] + 4)
9348           {
9349             /* We must find exactly one offset that is higher than the
9350                previous one by 4.  */
9351             if (order[i] != order[i - 1])
9352               return false;
9353             order[i] = j;
9354           }
9355       if (order[i] == order[i - 1])
9356         return false;
9357       /* The register numbers must be ascending.  */
9358       if (unsorted_regs != NULL
9359           && unsorted_regs[order[i]] <= unsorted_regs[order[i - 1]])
9360         return false;
9361     }
9362   return true;
9363 }
9364
9365 /* Used to determine in a peephole whether a sequence of load
9366    instructions can be changed into a load-multiple instruction.
9367    NOPS is the number of separate load instructions we are examining.  The
9368    first NOPS entries in OPERANDS are the destination registers, the
9369    next NOPS entries are memory operands.  If this function is
9370    successful, *BASE is set to the common base register of the memory
9371    accesses; *LOAD_OFFSET is set to the first memory location's offset
9372    from that base register.
9373    REGS is an array filled in with the destination register numbers.
9374    SAVED_ORDER (if nonnull), is an array filled in with an order that maps
9375    insn numbers to to an ascending order of stores.  If CHECK_REGS is true,
9376    the sequence of registers in REGS matches the loads from ascending memory
9377    locations, and the function verifies that the register numbers are
9378    themselves ascending.  If CHECK_REGS is false, the register numbers
9379    are stored in the order they are found in the operands.  */
9380 static int
9381 load_multiple_sequence (rtx *operands, int nops, int *regs, int *saved_order,
9382                         int *base, HOST_WIDE_INT *load_offset, bool check_regs)
9383 {
9384   int unsorted_regs[MAX_LDM_STM_OPS];
9385   HOST_WIDE_INT unsorted_offsets[MAX_LDM_STM_OPS];
9386   int order[MAX_LDM_STM_OPS];
9387   rtx base_reg_rtx = NULL;
9388   int base_reg = -1;
9389   int i, ldm_case;
9390
9391   /* Can only handle up to MAX_LDM_STM_OPS insns at present, though could be
9392      easily extended if required.  */
9393   gcc_assert (nops >= 2 && nops <= MAX_LDM_STM_OPS);
9394
9395   memset (order, 0, MAX_LDM_STM_OPS * sizeof (int));
9396
9397   /* Loop over the operands and check that the memory references are
9398      suitable (i.e. immediate offsets from the same base register).  At
9399      the same time, extract the target register, and the memory
9400      offsets.  */
9401   for (i = 0; i < nops; i++)
9402     {
9403       rtx reg;
9404       rtx offset;
9405
9406       /* Convert a subreg of a mem into the mem itself.  */
9407       if (GET_CODE (operands[nops + i]) == SUBREG)
9408         operands[nops + i] = alter_subreg (operands + (nops + i));
9409
9410       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
9411
9412       /* Don't reorder volatile memory references; it doesn't seem worth
9413          looking for the case where the order is ok anyway.  */
9414       if (MEM_VOLATILE_P (operands[nops + i]))
9415         return 0;
9416
9417       offset = const0_rtx;
9418
9419       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
9420            || (GET_CODE (reg) == SUBREG
9421                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9422           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
9423               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
9424                    == REG)
9425                   || (GET_CODE (reg) == SUBREG
9426                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9427               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
9428                   == CONST_INT)))
9429         {
9430           if (i == 0)
9431             {
9432               base_reg = REGNO (reg);
9433               base_reg_rtx = reg;
9434               if (TARGET_THUMB1 && base_reg > LAST_LO_REGNUM)
9435                 return 0;
9436             }
9437           else if (base_reg != (int) REGNO (reg))
9438             /* Not addressed from the same base register.  */
9439             return 0;
9440
9441           unsorted_regs[i] = (GET_CODE (operands[i]) == REG
9442                               ? REGNO (operands[i])
9443                               : REGNO (SUBREG_REG (operands[i])));
9444
9445           /* If it isn't an integer register, or if it overwrites the
9446              base register but isn't the last insn in the list, then
9447              we can't do this.  */
9448           if (unsorted_regs[i] < 0
9449               || (TARGET_THUMB1 && unsorted_regs[i] > LAST_LO_REGNUM)
9450               || unsorted_regs[i] > 14
9451               || (i != nops - 1 && unsorted_regs[i] == base_reg))
9452             return 0;
9453
9454           unsorted_offsets[i] = INTVAL (offset);
9455           if (i == 0 || unsorted_offsets[i] < unsorted_offsets[order[0]])
9456             order[0] = i;
9457         }
9458       else
9459         /* Not a suitable memory address.  */
9460         return 0;
9461     }
9462
9463   /* All the useful information has now been extracted from the
9464      operands into unsorted_regs and unsorted_offsets; additionally,
9465      order[0] has been set to the lowest offset in the list.  Sort
9466      the offsets into order, verifying that they are adjacent, and
9467      check that the register numbers are ascending.  */
9468   if (!compute_offset_order (nops, unsorted_offsets, order,
9469                              check_regs ? unsorted_regs : NULL))
9470     return 0;
9471
9472   if (saved_order)
9473     memcpy (saved_order, order, sizeof order);
9474
9475   if (base)
9476     {
9477       *base = base_reg;
9478
9479       for (i = 0; i < nops; i++)
9480         regs[i] = unsorted_regs[check_regs ? order[i] : i];
9481
9482       *load_offset = unsorted_offsets[order[0]];
9483     }
9484
9485   if (TARGET_THUMB1
9486       && !peep2_reg_dead_p (nops, base_reg_rtx))
9487     return 0;
9488
9489   if (unsorted_offsets[order[0]] == 0)
9490     ldm_case = 1; /* ldmia */
9491   else if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
9492     ldm_case = 2; /* ldmib */
9493   else if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
9494     ldm_case = 3; /* ldmda */
9495   else if (TARGET_32BIT && unsorted_offsets[order[nops - 1]] == -4)
9496     ldm_case = 4; /* ldmdb */
9497   else if (const_ok_for_arm (unsorted_offsets[order[0]])
9498            || const_ok_for_arm (-unsorted_offsets[order[0]]))
9499     ldm_case = 5;
9500   else
9501     return 0;
9502
9503   if (!multiple_operation_profitable_p (false, nops,
9504                                         ldm_case == 5
9505                                         ? unsorted_offsets[order[0]] : 0))
9506     return 0;
9507
9508   return ldm_case;
9509 }
9510
9511 /* Used to determine in a peephole whether a sequence of store instructions can
9512    be changed into a store-multiple instruction.
9513    NOPS is the number of separate store instructions we are examining.
9514    NOPS_TOTAL is the total number of instructions recognized by the peephole
9515    pattern.
9516    The first NOPS entries in OPERANDS are the source registers, the next
9517    NOPS entries are memory operands.  If this function is successful, *BASE is
9518    set to the common base register of the memory accesses; *LOAD_OFFSET is set
9519    to the first memory location's offset from that base register.  REGS is an
9520    array filled in with the source register numbers, REG_RTXS (if nonnull) is
9521    likewise filled with the corresponding rtx's.
9522    SAVED_ORDER (if nonnull), is an array filled in with an order that maps insn
9523    numbers to to an ascending order of stores.
9524    If CHECK_REGS is true, the sequence of registers in *REGS matches the stores
9525    from ascending memory locations, and the function verifies that the register
9526    numbers are themselves ascending.  If CHECK_REGS is false, the register
9527    numbers are stored in the order they are found in the operands.  */
9528 static int
9529 store_multiple_sequence (rtx *operands, int nops, int nops_total,
9530                          int *regs, rtx *reg_rtxs, int *saved_order, int *base,
9531                          HOST_WIDE_INT *load_offset, bool check_regs)
9532 {
9533   int unsorted_regs[MAX_LDM_STM_OPS];
9534   rtx unsorted_reg_rtxs[MAX_LDM_STM_OPS];
9535   HOST_WIDE_INT unsorted_offsets[MAX_LDM_STM_OPS];
9536   int order[MAX_LDM_STM_OPS];
9537   int base_reg = -1;
9538   rtx base_reg_rtx = NULL;
9539   int i, stm_case;
9540
9541   /* Can only handle up to MAX_LDM_STM_OPS insns at present, though could be
9542      easily extended if required.  */
9543   gcc_assert (nops >= 2 && nops <= MAX_LDM_STM_OPS);
9544
9545   memset (order, 0, MAX_LDM_STM_OPS * sizeof (int));
9546
9547   /* Loop over the operands and check that the memory references are
9548      suitable (i.e. immediate offsets from the same base register).  At
9549      the same time, extract the target register, and the memory
9550      offsets.  */
9551   for (i = 0; i < nops; i++)
9552     {
9553       rtx reg;
9554       rtx offset;
9555
9556       /* Convert a subreg of a mem into the mem itself.  */
9557       if (GET_CODE (operands[nops + i]) == SUBREG)
9558         operands[nops + i] = alter_subreg (operands + (nops + i));
9559
9560       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
9561
9562       /* Don't reorder volatile memory references; it doesn't seem worth
9563          looking for the case where the order is ok anyway.  */
9564       if (MEM_VOLATILE_P (operands[nops + i]))
9565         return 0;
9566
9567       offset = const0_rtx;
9568
9569       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
9570            || (GET_CODE (reg) == SUBREG
9571                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9572           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
9573               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
9574                    == REG)
9575                   || (GET_CODE (reg) == SUBREG
9576                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9577               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
9578                   == CONST_INT)))
9579         {
9580           unsorted_reg_rtxs[i] = (GET_CODE (operands[i]) == REG
9581                                   ? operands[i] : SUBREG_REG (operands[i]));
9582           unsorted_regs[i] = REGNO (unsorted_reg_rtxs[i]);
9583
9584           if (i == 0)
9585             {
9586               base_reg = REGNO (reg);
9587               base_reg_rtx = reg;
9588               if (TARGET_THUMB1 && base_reg > LAST_LO_REGNUM)
9589                 return 0;
9590             }
9591           else if (base_reg != (int) REGNO (reg))
9592             /* Not addressed from the same base register.  */
9593             return 0;
9594
9595           /* If it isn't an integer register, then we can't do this.  */
9596           if (unsorted_regs[i] < 0
9597               || (TARGET_THUMB1 && unsorted_regs[i] > LAST_LO_REGNUM)
9598               || (TARGET_THUMB2 && unsorted_regs[i] == base_reg)
9599               || (TARGET_THUMB2 && unsorted_regs[i] == SP_REGNUM)
9600               || unsorted_regs[i] > 14)
9601             return 0;
9602
9603           unsorted_offsets[i] = INTVAL (offset);
9604           if (i == 0 || unsorted_offsets[i] < unsorted_offsets[order[0]])
9605             order[0] = i;
9606         }
9607       else
9608         /* Not a suitable memory address.  */
9609         return 0;
9610     }
9611
9612   /* All the useful information has now been extracted from the
9613      operands into unsorted_regs and unsorted_offsets; additionally,
9614      order[0] has been set to the lowest offset in the list.  Sort
9615      the offsets into order, verifying that they are adjacent, and
9616      check that the register numbers are ascending.  */
9617   if (!compute_offset_order (nops, unsorted_offsets, order,
9618                              check_regs ? unsorted_regs : NULL))
9619     return 0;
9620
9621   if (saved_order)
9622     memcpy (saved_order, order, sizeof order);
9623
9624   if (base)
9625     {
9626       *base = base_reg;
9627
9628       for (i = 0; i < nops; i++)
9629         {
9630           regs[i] = unsorted_regs[check_regs ? order[i] : i];
9631           if (reg_rtxs)
9632             reg_rtxs[i] = unsorted_reg_rtxs[check_regs ? order[i] : i];
9633         }
9634
9635       *load_offset = unsorted_offsets[order[0]];
9636     }
9637
9638   if (TARGET_THUMB1
9639       && !peep2_reg_dead_p (nops_total, base_reg_rtx))
9640     return 0;
9641
9642   if (unsorted_offsets[order[0]] == 0)
9643     stm_case = 1; /* stmia */
9644   else if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
9645     stm_case = 2; /* stmib */
9646   else if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
9647     stm_case = 3; /* stmda */
9648   else if (TARGET_32BIT && unsorted_offsets[order[nops - 1]] == -4)
9649     stm_case = 4; /* stmdb */
9650   else
9651     return 0;
9652
9653   if (!multiple_operation_profitable_p (false, nops, 0))
9654     return 0;
9655
9656   return stm_case;
9657 }
9658 \f
9659 /* Routines for use in generating RTL.  */
9660
9661 /* Generate a load-multiple instruction.  COUNT is the number of loads in
9662    the instruction; REGS and MEMS are arrays containing the operands.
9663    BASEREG is the base register to be used in addressing the memory operands.
9664    WBACK_OFFSET is nonzero if the instruction should update the base
9665    register.  */
9666
9667 static rtx
9668 arm_gen_load_multiple_1 (int count, int *regs, rtx *mems, rtx basereg,
9669                          HOST_WIDE_INT wback_offset)
9670 {
9671   int i = 0, j;
9672   rtx result;
9673
9674   if (!multiple_operation_profitable_p (false, count, 0))
9675     {
9676       rtx seq;
9677
9678       start_sequence ();
9679
9680       for (i = 0; i < count; i++)
9681         emit_move_insn (gen_rtx_REG (SImode, regs[i]), mems[i]);
9682
9683       if (wback_offset != 0)
9684         emit_move_insn (basereg, plus_constant (basereg, wback_offset));
9685
9686       seq = get_insns ();
9687       end_sequence ();
9688
9689       return seq;
9690     }
9691
9692   result = gen_rtx_PARALLEL (VOIDmode,
9693                              rtvec_alloc (count + (wback_offset != 0 ? 1 : 0)));
9694   if (wback_offset != 0)
9695     {
9696       XVECEXP (result, 0, 0)
9697         = gen_rtx_SET (VOIDmode, basereg,
9698                        plus_constant (basereg, wback_offset));
9699       i = 1;
9700       count++;
9701     }
9702
9703   for (j = 0; i < count; i++, j++)
9704     XVECEXP (result, 0, i)
9705       = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, regs[j]), mems[j]);
9706
9707   return result;
9708 }
9709
9710 /* Generate a store-multiple instruction.  COUNT is the number of stores in
9711    the instruction; REGS and MEMS are arrays containing the operands.
9712    BASEREG is the base register to be used in addressing the memory operands.
9713    WBACK_OFFSET is nonzero if the instruction should update the base
9714    register.  */
9715
9716 static rtx
9717 arm_gen_store_multiple_1 (int count, int *regs, rtx *mems, rtx basereg,
9718                           HOST_WIDE_INT wback_offset)
9719 {
9720   int i = 0, j;
9721   rtx result;
9722
9723   if (GET_CODE (basereg) == PLUS)
9724     basereg = XEXP (basereg, 0);
9725
9726   if (!multiple_operation_profitable_p (false, count, 0))
9727     {
9728       rtx seq;
9729
9730       start_sequence ();
9731
9732       for (i = 0; i < count; i++)
9733         emit_move_insn (mems[i], gen_rtx_REG (SImode, regs[i]));
9734
9735       if (wback_offset != 0)
9736         emit_move_insn (basereg, plus_constant (basereg, wback_offset));
9737
9738       seq = get_insns ();
9739       end_sequence ();
9740
9741       return seq;
9742     }
9743
9744   result = gen_rtx_PARALLEL (VOIDmode,
9745                              rtvec_alloc (count + (wback_offset != 0 ? 1 : 0)));
9746   if (wback_offset != 0)
9747     {
9748       XVECEXP (result, 0, 0)
9749         = gen_rtx_SET (VOIDmode, basereg,
9750                        plus_constant (basereg, wback_offset));
9751       i = 1;
9752       count++;
9753     }
9754
9755   for (j = 0; i < count; i++, j++)
9756     XVECEXP (result, 0, i)
9757       = gen_rtx_SET (VOIDmode, mems[j], gen_rtx_REG (SImode, regs[j]));
9758
9759   return result;
9760 }
9761
9762 /* Generate either a load-multiple or a store-multiple instruction.  This
9763    function can be used in situations where we can start with a single MEM
9764    rtx and adjust its address upwards.
9765    COUNT is the number of operations in the instruction, not counting a
9766    possible update of the base register.  REGS is an array containing the
9767    register operands.
9768    BASEREG is the base register to be used in addressing the memory operands,
9769    which are constructed from BASEMEM.
9770    WRITE_BACK specifies whether the generated instruction should include an
9771    update of the base register.
9772    OFFSETP is used to pass an offset to and from this function; this offset
9773    is not used when constructing the address (instead BASEMEM should have an
9774    appropriate offset in its address), it is used only for setting
9775    MEM_OFFSET.  It is updated only if WRITE_BACK is true.*/
9776
9777 static rtx
9778 arm_gen_multiple_op (bool is_load, int *regs, int count, rtx basereg,
9779                      bool write_back, rtx basemem, HOST_WIDE_INT *offsetp)
9780 {
9781   rtx mems[MAX_LDM_STM_OPS];
9782   HOST_WIDE_INT offset = *offsetp;
9783   int i;
9784
9785   gcc_assert (count <= MAX_LDM_STM_OPS);
9786
9787   if (GET_CODE (basereg) == PLUS)
9788     basereg = XEXP (basereg, 0);
9789
9790   for (i = 0; i < count; i++)
9791     {
9792       rtx addr = plus_constant (basereg, i * 4);
9793       mems[i] = adjust_automodify_address_nv (basemem, SImode, addr, offset);
9794       offset += 4;
9795     }
9796
9797   if (write_back)
9798     *offsetp = offset;
9799
9800   if (is_load)
9801     return arm_gen_load_multiple_1 (count, regs, mems, basereg,
9802                                     write_back ? 4 * count : 0);
9803   else
9804     return arm_gen_store_multiple_1 (count, regs, mems, basereg,
9805                                      write_back ? 4 * count : 0);
9806 }
9807
9808 rtx
9809 arm_gen_load_multiple (int *regs, int count, rtx basereg, int write_back,
9810                        rtx basemem, HOST_WIDE_INT *offsetp)
9811 {
9812   return arm_gen_multiple_op (TRUE, regs, count, basereg, write_back, basemem,
9813                               offsetp);
9814 }
9815
9816 rtx
9817 arm_gen_store_multiple (int *regs, int count, rtx basereg, int write_back,
9818                         rtx basemem, HOST_WIDE_INT *offsetp)
9819 {
9820   return arm_gen_multiple_op (FALSE, regs, count, basereg, write_back, basemem,
9821                               offsetp);
9822 }
9823
9824 /* Called from a peephole2 expander to turn a sequence of loads into an
9825    LDM instruction.  OPERANDS are the operands found by the peephole matcher;
9826    NOPS indicates how many separate loads we are trying to combine.  SORT_REGS
9827    is true if we can reorder the registers because they are used commutatively
9828    subsequently.
9829    Returns true iff we could generate a new instruction.  */
9830
9831 bool
9832 gen_ldm_seq (rtx *operands, int nops, bool sort_regs)
9833 {
9834   int regs[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
9835   rtx mems[MAX_LDM_STM_OPS];
9836   int i, j, base_reg;
9837   rtx base_reg_rtx;
9838   HOST_WIDE_INT offset;
9839   int write_back = FALSE;
9840   int ldm_case;
9841   rtx addr;
9842
9843   ldm_case = load_multiple_sequence (operands, nops, regs, mem_order,
9844                                      &base_reg, &offset, !sort_regs);
9845
9846   if (ldm_case == 0)
9847     return false;
9848
9849   if (sort_regs)
9850     for (i = 0; i < nops - 1; i++)
9851       for (j = i + 1; j < nops; j++)
9852         if (regs[i] > regs[j])
9853           {
9854             int t = regs[i];
9855             regs[i] = regs[j];
9856             regs[j] = t;
9857           }
9858   base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
9859
9860   if (TARGET_THUMB1)
9861     {
9862       gcc_assert (peep2_reg_dead_p (nops, base_reg_rtx));
9863       gcc_assert (ldm_case == 1 || ldm_case == 5);
9864       write_back = TRUE;
9865     }
9866
9867   if (ldm_case == 5)
9868     {
9869       rtx newbase = TARGET_THUMB1 ? base_reg_rtx : gen_rtx_REG (SImode, regs[0]);
9870       emit_insn (gen_addsi3 (newbase, base_reg_rtx, GEN_INT (offset)));
9871       offset = 0;
9872       if (!TARGET_THUMB1)
9873         {
9874           base_reg = regs[0];
9875           base_reg_rtx = newbase;
9876         }
9877     }
9878
9879   for (i = 0; i < nops; i++)
9880     {
9881       addr = plus_constant (base_reg_rtx, offset + i * 4);
9882       mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
9883                                               SImode, addr, 0);
9884     }
9885   emit_insn (arm_gen_load_multiple_1 (nops, regs, mems, base_reg_rtx,
9886                                       write_back ? offset + i * 4 : 0));
9887   return true;
9888 }
9889
9890 /* Called from a peephole2 expander to turn a sequence of stores into an
9891    STM instruction.  OPERANDS are the operands found by the peephole matcher;
9892    NOPS indicates how many separate stores we are trying to combine.
9893    Returns true iff we could generate a new instruction.  */
9894
9895 bool
9896 gen_stm_seq (rtx *operands, int nops)
9897 {
9898   int i;
9899   int regs[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
9900   rtx mems[MAX_LDM_STM_OPS];
9901   int base_reg;
9902   rtx base_reg_rtx;
9903   HOST_WIDE_INT offset;
9904   int write_back = FALSE;
9905   int stm_case;
9906   rtx addr;
9907   bool base_reg_dies;
9908
9909   stm_case = store_multiple_sequence (operands, nops, nops, regs, NULL,
9910                                       mem_order, &base_reg, &offset, true);
9911
9912   if (stm_case == 0)
9913     return false;
9914
9915   base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
9916
9917   base_reg_dies = peep2_reg_dead_p (nops, base_reg_rtx);
9918   if (TARGET_THUMB1)
9919     {
9920       gcc_assert (base_reg_dies);
9921       write_back = TRUE;
9922     }
9923
9924   if (stm_case == 5)
9925     {
9926       gcc_assert (base_reg_dies);
9927       emit_insn (gen_addsi3 (base_reg_rtx, base_reg_rtx, GEN_INT (offset)));
9928       offset = 0;
9929     }
9930
9931   addr = plus_constant (base_reg_rtx, offset);
9932
9933   for (i = 0; i < nops; i++)
9934     {
9935       addr = plus_constant (base_reg_rtx, offset + i * 4);
9936       mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
9937                                               SImode, addr, 0);
9938     }
9939   emit_insn (arm_gen_store_multiple_1 (nops, regs, mems, base_reg_rtx,
9940                                        write_back ? offset + i * 4 : 0));
9941   return true;
9942 }
9943
9944 /* Called from a peephole2 expander to turn a sequence of stores that are
9945    preceded by constant loads into an STM instruction.  OPERANDS are the
9946    operands found by the peephole matcher; NOPS indicates how many
9947    separate stores we are trying to combine; there are 2 * NOPS
9948    instructions in the peephole.
9949    Returns true iff we could generate a new instruction.  */
9950
9951 bool
9952 gen_const_stm_seq (rtx *operands, int nops)
9953 {
9954   int regs[MAX_LDM_STM_OPS], sorted_regs[MAX_LDM_STM_OPS];
9955   int reg_order[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
9956   rtx reg_rtxs[MAX_LDM_STM_OPS], orig_reg_rtxs[MAX_LDM_STM_OPS];
9957   rtx mems[MAX_LDM_STM_OPS];
9958   int base_reg;
9959   rtx base_reg_rtx;
9960   HOST_WIDE_INT offset;
9961   int write_back = FALSE;
9962   int stm_case;
9963   rtx addr;
9964   bool base_reg_dies;
9965   int i, j;
9966   HARD_REG_SET allocated;
9967
9968   stm_case = store_multiple_sequence (operands, nops, 2 * nops, regs, reg_rtxs,
9969                                       mem_order, &base_reg, &offset, false);
9970
9971   if (stm_case == 0)
9972     return false;
9973
9974   memcpy (orig_reg_rtxs, reg_rtxs, sizeof orig_reg_rtxs);
9975
9976   /* If the same register is used more than once, try to find a free
9977      register.  */
9978   CLEAR_HARD_REG_SET (allocated);
9979   for (i = 0; i < nops; i++)
9980     {
9981       for (j = i + 1; j < nops; j++)
9982         if (regs[i] == regs[j])
9983           {
9984             rtx t = peep2_find_free_register (0, nops * 2,
9985                                               TARGET_THUMB1 ? "l" : "r",
9986                                               SImode, &allocated);
9987             if (t == NULL_RTX)
9988               return false;
9989             reg_rtxs[i] = t;
9990             regs[i] = REGNO (t);
9991           }
9992     }
9993
9994   /* Compute an ordering that maps the register numbers to an ascending
9995      sequence.  */
9996   reg_order[0] = 0;
9997   for (i = 0; i < nops; i++)
9998     if (regs[i] < regs[reg_order[0]])
9999       reg_order[0] = i;
10000
10001   for (i = 1; i < nops; i++)
10002     {
10003       int this_order = reg_order[i - 1];
10004       for (j = 0; j < nops; j++)
10005         if (regs[j] > regs[reg_order[i - 1]]
10006             && (this_order == reg_order[i - 1]
10007                 || regs[j] < regs[this_order]))
10008           this_order = j;
10009       reg_order[i] = this_order;
10010     }
10011
10012   /* Ensure that registers that must be live after the instruction end
10013      up with the correct value.  */
10014   for (i = 0; i < nops; i++)
10015     {
10016       int this_order = reg_order[i];
10017       if ((this_order != mem_order[i]
10018            || orig_reg_rtxs[this_order] != reg_rtxs[this_order])
10019           && !peep2_reg_dead_p (nops * 2, orig_reg_rtxs[this_order]))
10020         return false;
10021     }
10022
10023   /* Load the constants.  */
10024   for (i = 0; i < nops; i++)
10025     {
10026       rtx op = operands[2 * nops + mem_order[i]];
10027       sorted_regs[i] = regs[reg_order[i]];
10028       emit_move_insn (reg_rtxs[reg_order[i]], op);
10029     }
10030
10031   base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
10032
10033   base_reg_dies = peep2_reg_dead_p (nops * 2, base_reg_rtx);
10034   if (TARGET_THUMB1)
10035     {
10036       gcc_assert (base_reg_dies);
10037       write_back = TRUE;
10038     }
10039
10040   if (stm_case == 5)
10041     {
10042       gcc_assert (base_reg_dies);
10043       emit_insn (gen_addsi3 (base_reg_rtx, base_reg_rtx, GEN_INT (offset)));
10044       offset = 0;
10045     }
10046
10047   addr = plus_constant (base_reg_rtx, offset);
10048
10049   for (i = 0; i < nops; i++)
10050     {
10051       addr = plus_constant (base_reg_rtx, offset + i * 4);
10052       mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
10053                                               SImode, addr, 0);
10054     }
10055   emit_insn (arm_gen_store_multiple_1 (nops, sorted_regs, mems, base_reg_rtx,
10056                                        write_back ? offset + i * 4 : 0));
10057   return true;
10058 }
10059
10060 int
10061 arm_gen_movmemqi (rtx *operands)
10062 {
10063   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
10064   HOST_WIDE_INT srcoffset, dstoffset;
10065   int i;
10066   rtx src, dst, srcbase, dstbase;
10067   rtx part_bytes_reg = NULL;
10068   rtx mem;
10069
10070   if (GET_CODE (operands[2]) != CONST_INT
10071       || GET_CODE (operands[3]) != CONST_INT
10072       || INTVAL (operands[2]) > 64
10073       || INTVAL (operands[3]) & 3)
10074     return 0;
10075
10076   dstbase = operands[0];
10077   srcbase = operands[1];
10078
10079   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
10080   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
10081
10082   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
10083   out_words_to_go = INTVAL (operands[2]) / 4;
10084   last_bytes = INTVAL (operands[2]) & 3;
10085   dstoffset = srcoffset = 0;
10086
10087   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
10088     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
10089
10090   for (i = 0; in_words_to_go >= 2; i+=4)
10091     {
10092       if (in_words_to_go > 4)
10093         emit_insn (arm_gen_load_multiple (arm_regs_in_sequence, 4, src,
10094                                           TRUE, srcbase, &srcoffset));
10095       else
10096         emit_insn (arm_gen_load_multiple (arm_regs_in_sequence, in_words_to_go,
10097                                           src, FALSE, srcbase,
10098                                           &srcoffset));
10099
10100       if (out_words_to_go)
10101         {
10102           if (out_words_to_go > 4)
10103             emit_insn (arm_gen_store_multiple (arm_regs_in_sequence, 4, dst,
10104                                                TRUE, dstbase, &dstoffset));
10105           else if (out_words_to_go != 1)
10106             emit_insn (arm_gen_store_multiple (arm_regs_in_sequence,
10107                                                out_words_to_go, dst,
10108                                                (last_bytes == 0
10109                                                 ? FALSE : TRUE),
10110                                                dstbase, &dstoffset));
10111           else
10112             {
10113               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
10114               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
10115               if (last_bytes != 0)
10116                 {
10117                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
10118                   dstoffset += 4;
10119                 }
10120             }
10121         }
10122
10123       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
10124       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
10125     }
10126
10127   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
10128   if (out_words_to_go)
10129     {
10130       rtx sreg;
10131
10132       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
10133       sreg = copy_to_reg (mem);
10134
10135       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
10136       emit_move_insn (mem, sreg);
10137       in_words_to_go--;
10138
10139       gcc_assert (!in_words_to_go);     /* Sanity check */
10140     }
10141
10142   if (in_words_to_go)
10143     {
10144       gcc_assert (in_words_to_go > 0);
10145
10146       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
10147       part_bytes_reg = copy_to_mode_reg (SImode, mem);
10148     }
10149
10150   gcc_assert (!last_bytes || part_bytes_reg);
10151
10152   if (BYTES_BIG_ENDIAN && last_bytes)
10153     {
10154       rtx tmp = gen_reg_rtx (SImode);
10155
10156       /* The bytes we want are in the top end of the word.  */
10157       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
10158                               GEN_INT (8 * (4 - last_bytes))));
10159       part_bytes_reg = tmp;
10160
10161       while (last_bytes)
10162         {
10163           mem = adjust_automodify_address (dstbase, QImode,
10164                                            plus_constant (dst, last_bytes - 1),
10165                                            dstoffset + last_bytes - 1);
10166           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
10167
10168           if (--last_bytes)
10169             {
10170               tmp = gen_reg_rtx (SImode);
10171               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
10172               part_bytes_reg = tmp;
10173             }
10174         }
10175
10176     }
10177   else
10178     {
10179       if (last_bytes > 1)
10180         {
10181           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
10182           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
10183           last_bytes -= 2;
10184           if (last_bytes)
10185             {
10186               rtx tmp = gen_reg_rtx (SImode);
10187               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
10188               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
10189               part_bytes_reg = tmp;
10190               dstoffset += 2;
10191             }
10192         }
10193
10194       if (last_bytes)
10195         {
10196           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
10197           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
10198         }
10199     }
10200
10201   return 1;
10202 }
10203
10204 /* Select a dominance comparison mode if possible for a test of the general
10205    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
10206    COND_OR == DOM_CC_X_AND_Y => (X && Y)
10207    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
10208    COND_OR == DOM_CC_X_OR_Y => (X || Y)
10209    In all cases OP will be either EQ or NE, but we don't need to know which
10210    here.  If we are unable to support a dominance comparison we return
10211    CC mode.  This will then fail to match for the RTL expressions that
10212    generate this call.  */
10213 enum machine_mode
10214 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
10215 {
10216   enum rtx_code cond1, cond2;
10217   int swapped = 0;
10218
10219   /* Currently we will probably get the wrong result if the individual
10220      comparisons are not simple.  This also ensures that it is safe to
10221      reverse a comparison if necessary.  */
10222   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
10223        != CCmode)
10224       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
10225           != CCmode))
10226     return CCmode;
10227
10228   /* The if_then_else variant of this tests the second condition if the
10229      first passes, but is true if the first fails.  Reverse the first
10230      condition to get a true "inclusive-or" expression.  */
10231   if (cond_or == DOM_CC_NX_OR_Y)
10232     cond1 = reverse_condition (cond1);
10233
10234   /* If the comparisons are not equal, and one doesn't dominate the other,
10235      then we can't do this.  */
10236   if (cond1 != cond2
10237       && !comparison_dominates_p (cond1, cond2)
10238       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
10239     return CCmode;
10240
10241   if (swapped)
10242     {
10243       enum rtx_code temp = cond1;
10244       cond1 = cond2;
10245       cond2 = temp;
10246     }
10247
10248   switch (cond1)
10249     {
10250     case EQ:
10251       if (cond_or == DOM_CC_X_AND_Y)
10252         return CC_DEQmode;
10253
10254       switch (cond2)
10255         {
10256         case EQ: return CC_DEQmode;
10257         case LE: return CC_DLEmode;
10258         case LEU: return CC_DLEUmode;
10259         case GE: return CC_DGEmode;
10260         case GEU: return CC_DGEUmode;
10261         default: gcc_unreachable ();
10262         }
10263
10264     case LT:
10265       if (cond_or == DOM_CC_X_AND_Y)
10266         return CC_DLTmode;
10267
10268       switch (cond2)
10269         {
10270         case  LT:
10271             return CC_DLTmode;
10272         case LE:
10273           return CC_DLEmode;
10274         case NE:
10275           return CC_DNEmode;
10276         default:
10277           gcc_unreachable ();
10278         }
10279
10280     case GT:
10281       if (cond_or == DOM_CC_X_AND_Y)
10282         return CC_DGTmode;
10283
10284       switch (cond2)
10285         {
10286         case GT:
10287           return CC_DGTmode;
10288         case GE:
10289           return CC_DGEmode;
10290         case NE:
10291           return CC_DNEmode;
10292         default:
10293           gcc_unreachable ();
10294         }
10295
10296     case LTU:
10297       if (cond_or == DOM_CC_X_AND_Y)
10298         return CC_DLTUmode;
10299
10300       switch (cond2)
10301         {
10302         case LTU:
10303           return CC_DLTUmode;
10304         case LEU:
10305           return CC_DLEUmode;
10306         case NE:
10307           return CC_DNEmode;
10308         default:
10309           gcc_unreachable ();
10310         }
10311
10312     case GTU:
10313       if (cond_or == DOM_CC_X_AND_Y)
10314         return CC_DGTUmode;
10315
10316       switch (cond2)
10317         {
10318         case GTU:
10319           return CC_DGTUmode;
10320         case GEU:
10321           return CC_DGEUmode;
10322         case NE:
10323           return CC_DNEmode;
10324         default:
10325           gcc_unreachable ();
10326         }
10327
10328     /* The remaining cases only occur when both comparisons are the
10329        same.  */
10330     case NE:
10331       gcc_assert (cond1 == cond2);
10332       return CC_DNEmode;
10333
10334     case LE:
10335       gcc_assert (cond1 == cond2);
10336       return CC_DLEmode;
10337
10338     case GE:
10339       gcc_assert (cond1 == cond2);
10340       return CC_DGEmode;
10341
10342     case LEU:
10343       gcc_assert (cond1 == cond2);
10344       return CC_DLEUmode;
10345
10346     case GEU:
10347       gcc_assert (cond1 == cond2);
10348       return CC_DGEUmode;
10349
10350     default:
10351       gcc_unreachable ();
10352     }
10353 }
10354
10355 enum machine_mode
10356 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
10357 {
10358   /* All floating point compares return CCFP if it is an equality
10359      comparison, and CCFPE otherwise.  */
10360   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
10361     {
10362       switch (op)
10363         {
10364         case EQ:
10365         case NE:
10366         case UNORDERED:
10367         case ORDERED:
10368         case UNLT:
10369         case UNLE:
10370         case UNGT:
10371         case UNGE:
10372         case UNEQ:
10373         case LTGT:
10374           return CCFPmode;
10375
10376         case LT:
10377         case LE:
10378         case GT:
10379         case GE:
10380           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
10381             return CCFPmode;
10382           return CCFPEmode;
10383
10384         default:
10385           gcc_unreachable ();
10386         }
10387     }
10388
10389   /* A compare with a shifted operand.  Because of canonicalization, the
10390      comparison will have to be swapped when we emit the assembler.  */
10391   if (GET_MODE (y) == SImode 
10392       && (REG_P (y) || (GET_CODE (y) == SUBREG))
10393       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
10394           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
10395           || GET_CODE (x) == ROTATERT))
10396     return CC_SWPmode;
10397
10398   /* This operation is performed swapped, but since we only rely on the Z
10399      flag we don't need an additional mode.  */
10400   if (GET_MODE (y) == SImode 
10401       && (REG_P (y) || (GET_CODE (y) == SUBREG))
10402       && GET_CODE (x) == NEG
10403       && (op == EQ || op == NE))
10404     return CC_Zmode;
10405
10406   /* This is a special case that is used by combine to allow a
10407      comparison of a shifted byte load to be split into a zero-extend
10408      followed by a comparison of the shifted integer (only valid for
10409      equalities and unsigned inequalities).  */
10410   if (GET_MODE (x) == SImode
10411       && GET_CODE (x) == ASHIFT
10412       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
10413       && GET_CODE (XEXP (x, 0)) == SUBREG
10414       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
10415       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
10416       && (op == EQ || op == NE
10417           || op == GEU || op == GTU || op == LTU || op == LEU)
10418       && GET_CODE (y) == CONST_INT)
10419     return CC_Zmode;
10420
10421   /* A construct for a conditional compare, if the false arm contains
10422      0, then both conditions must be true, otherwise either condition
10423      must be true.  Not all conditions are possible, so CCmode is
10424      returned if it can't be done.  */
10425   if (GET_CODE (x) == IF_THEN_ELSE
10426       && (XEXP (x, 2) == const0_rtx
10427           || XEXP (x, 2) == const1_rtx)
10428       && COMPARISON_P (XEXP (x, 0))
10429       && COMPARISON_P (XEXP (x, 1)))
10430     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
10431                                          INTVAL (XEXP (x, 2)));
10432
10433   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
10434   if (GET_CODE (x) == AND
10435       && COMPARISON_P (XEXP (x, 0))
10436       && COMPARISON_P (XEXP (x, 1)))
10437     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
10438                                          DOM_CC_X_AND_Y);
10439
10440   if (GET_CODE (x) == IOR
10441       && COMPARISON_P (XEXP (x, 0))
10442       && COMPARISON_P (XEXP (x, 1)))
10443     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
10444                                          DOM_CC_X_OR_Y);
10445
10446   /* An operation (on Thumb) where we want to test for a single bit.
10447      This is done by shifting that bit up into the top bit of a
10448      scratch register; we can then branch on the sign bit.  */
10449   if (TARGET_THUMB1
10450       && GET_MODE (x) == SImode
10451       && (op == EQ || op == NE)
10452       && GET_CODE (x) == ZERO_EXTRACT
10453       && XEXP (x, 1) == const1_rtx)
10454     return CC_Nmode;
10455
10456   /* An operation that sets the condition codes as a side-effect, the
10457      V flag is not set correctly, so we can only use comparisons where
10458      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
10459      instead.)  */
10460   /* ??? Does the ZERO_EXTRACT case really apply to thumb2?  */
10461   if (GET_MODE (x) == SImode
10462       && y == const0_rtx
10463       && (op == EQ || op == NE || op == LT || op == GE)
10464       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
10465           || GET_CODE (x) == AND || GET_CODE (x) == IOR
10466           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
10467           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
10468           || GET_CODE (x) == LSHIFTRT
10469           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
10470           || GET_CODE (x) == ROTATERT
10471           || (TARGET_32BIT && GET_CODE (x) == ZERO_EXTRACT)))
10472     return CC_NOOVmode;
10473
10474   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
10475     return CC_Zmode;
10476
10477   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
10478       && GET_CODE (x) == PLUS
10479       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
10480     return CC_Cmode;
10481
10482   if (GET_MODE (x) == DImode || GET_MODE (y) == DImode)
10483     {
10484       /* To keep things simple, always use the Cirrus cfcmp64 if it is
10485          available.  */
10486       if (TARGET_ARM && TARGET_HARD_FLOAT && TARGET_MAVERICK)
10487         return CCmode;
10488
10489       switch (op)
10490         {
10491         case EQ:
10492         case NE:
10493           /* A DImode comparison against zero can be implemented by
10494              or'ing the two halves together.  */
10495           if (y == const0_rtx)
10496             return CC_Zmode;
10497
10498           /* We can do an equality test in three Thumb instructions.  */
10499           if (!TARGET_ARM)
10500             return CC_Zmode;
10501
10502           /* FALLTHROUGH */
10503
10504         case LTU:
10505         case LEU:
10506         case GTU:
10507         case GEU:
10508           /* DImode unsigned comparisons can be implemented by cmp +
10509              cmpeq without a scratch register.  Not worth doing in
10510              Thumb-2.  */
10511           if (TARGET_ARM)
10512             return CC_CZmode;
10513
10514           /* FALLTHROUGH */
10515
10516         case LT:
10517         case LE:
10518         case GT:
10519         case GE:
10520           /* DImode signed and unsigned comparisons can be implemented
10521              by cmp + sbcs with a scratch register, but that does not
10522              set the Z flag - we must reverse GT/LE/GTU/LEU.  */
10523           gcc_assert (op != EQ && op != NE);
10524           return CC_NCVmode;
10525
10526         default:
10527           gcc_unreachable ();
10528         }
10529     }
10530
10531   return CCmode;
10532 }
10533
10534 /* X and Y are two things to compare using CODE.  Emit the compare insn and
10535    return the rtx for register 0 in the proper mode.  FP means this is a
10536    floating point compare: I don't think that it is needed on the arm.  */
10537 rtx
10538 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
10539 {
10540   enum machine_mode mode;
10541   rtx cc_reg;
10542   int dimode_comparison = GET_MODE (x) == DImode || GET_MODE (y) == DImode;
10543
10544   /* We might have X as a constant, Y as a register because of the predicates
10545      used for cmpdi.  If so, force X to a register here.  */
10546   if (dimode_comparison && !REG_P (x))
10547     x = force_reg (DImode, x);
10548
10549   mode = SELECT_CC_MODE (code, x, y);
10550   cc_reg = gen_rtx_REG (mode, CC_REGNUM);
10551
10552   if (dimode_comparison
10553       && !(TARGET_HARD_FLOAT && TARGET_MAVERICK)
10554       && mode != CC_CZmode)
10555     {
10556       rtx clobber, set;
10557
10558       /* To compare two non-zero values for equality, XOR them and
10559          then compare against zero.  Not used for ARM mode; there
10560          CC_CZmode is cheaper.  */
10561       if (mode == CC_Zmode && y != const0_rtx)
10562         {
10563           x = expand_binop (DImode, xor_optab, x, y, NULL_RTX, 0, OPTAB_WIDEN);
10564           y = const0_rtx;
10565         }
10566       /* A scratch register is required.  */
10567       clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (SImode));
10568       set = gen_rtx_SET (VOIDmode, cc_reg, gen_rtx_COMPARE (mode, x, y));
10569       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
10570     }
10571   else
10572     emit_set_insn (cc_reg, gen_rtx_COMPARE (mode, x, y));
10573
10574   return cc_reg;
10575 }
10576
10577 /* Generate a sequence of insns that will generate the correct return
10578    address mask depending on the physical architecture that the program
10579    is running on.  */
10580 rtx
10581 arm_gen_return_addr_mask (void)
10582 {
10583   rtx reg = gen_reg_rtx (Pmode);
10584
10585   emit_insn (gen_return_addr_mask (reg));
10586   return reg;
10587 }
10588
10589 void
10590 arm_reload_in_hi (rtx *operands)
10591 {
10592   rtx ref = operands[1];
10593   rtx base, scratch;
10594   HOST_WIDE_INT offset = 0;
10595
10596   if (GET_CODE (ref) == SUBREG)
10597     {
10598       offset = SUBREG_BYTE (ref);
10599       ref = SUBREG_REG (ref);
10600     }
10601
10602   if (GET_CODE (ref) == REG)
10603     {
10604       /* We have a pseudo which has been spilt onto the stack; there
10605          are two cases here: the first where there is a simple
10606          stack-slot replacement and a second where the stack-slot is
10607          out of range, or is used as a subreg.  */
10608       if (reg_equiv_mem[REGNO (ref)])
10609         {
10610           ref = reg_equiv_mem[REGNO (ref)];
10611           base = find_replacement (&XEXP (ref, 0));
10612         }
10613       else
10614         /* The slot is out of range, or was dressed up in a SUBREG.  */
10615         base = reg_equiv_address[REGNO (ref)];
10616     }
10617   else
10618     base = find_replacement (&XEXP (ref, 0));
10619
10620   /* Handle the case where the address is too complex to be offset by 1.  */
10621   if (GET_CODE (base) == MINUS
10622       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
10623     {
10624       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10625
10626       emit_set_insn (base_plus, base);
10627       base = base_plus;
10628     }
10629   else if (GET_CODE (base) == PLUS)
10630     {
10631       /* The addend must be CONST_INT, or we would have dealt with it above.  */
10632       HOST_WIDE_INT hi, lo;
10633
10634       offset += INTVAL (XEXP (base, 1));
10635       base = XEXP (base, 0);
10636
10637       /* Rework the address into a legal sequence of insns.  */
10638       /* Valid range for lo is -4095 -> 4095 */
10639       lo = (offset >= 0
10640             ? (offset & 0xfff)
10641             : -((-offset) & 0xfff));
10642
10643       /* Corner case, if lo is the max offset then we would be out of range
10644          once we have added the additional 1 below, so bump the msb into the
10645          pre-loading insn(s).  */
10646       if (lo == 4095)
10647         lo &= 0x7ff;
10648
10649       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
10650              ^ (HOST_WIDE_INT) 0x80000000)
10651             - (HOST_WIDE_INT) 0x80000000);
10652
10653       gcc_assert (hi + lo == offset);
10654
10655       if (hi != 0)
10656         {
10657           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10658
10659           /* Get the base address; addsi3 knows how to handle constants
10660              that require more than one insn.  */
10661           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
10662           base = base_plus;
10663           offset = lo;
10664         }
10665     }
10666
10667   /* Operands[2] may overlap operands[0] (though it won't overlap
10668      operands[1]), that's why we asked for a DImode reg -- so we can
10669      use the bit that does not overlap.  */
10670   if (REGNO (operands[2]) == REGNO (operands[0]))
10671     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10672   else
10673     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
10674
10675   emit_insn (gen_zero_extendqisi2 (scratch,
10676                                    gen_rtx_MEM (QImode,
10677                                                 plus_constant (base,
10678                                                                offset))));
10679   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
10680                                    gen_rtx_MEM (QImode,
10681                                                 plus_constant (base,
10682                                                                offset + 1))));
10683   if (!BYTES_BIG_ENDIAN)
10684     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
10685                    gen_rtx_IOR (SImode,
10686                                 gen_rtx_ASHIFT
10687                                 (SImode,
10688                                  gen_rtx_SUBREG (SImode, operands[0], 0),
10689                                  GEN_INT (8)),
10690                                 scratch));
10691   else
10692     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
10693                    gen_rtx_IOR (SImode,
10694                                 gen_rtx_ASHIFT (SImode, scratch,
10695                                                 GEN_INT (8)),
10696                                 gen_rtx_SUBREG (SImode, operands[0], 0)));
10697 }
10698
10699 /* Handle storing a half-word to memory during reload by synthesizing as two
10700    byte stores.  Take care not to clobber the input values until after we
10701    have moved them somewhere safe.  This code assumes that if the DImode
10702    scratch in operands[2] overlaps either the input value or output address
10703    in some way, then that value must die in this insn (we absolutely need
10704    two scratch registers for some corner cases).  */
10705 void
10706 arm_reload_out_hi (rtx *operands)
10707 {
10708   rtx ref = operands[0];
10709   rtx outval = operands[1];
10710   rtx base, scratch;
10711   HOST_WIDE_INT offset = 0;
10712
10713   if (GET_CODE (ref) == SUBREG)
10714     {
10715       offset = SUBREG_BYTE (ref);
10716       ref = SUBREG_REG (ref);
10717     }
10718
10719   if (GET_CODE (ref) == REG)
10720     {
10721       /* We have a pseudo which has been spilt onto the stack; there
10722          are two cases here: the first where there is a simple
10723          stack-slot replacement and a second where the stack-slot is
10724          out of range, or is used as a subreg.  */
10725       if (reg_equiv_mem[REGNO (ref)])
10726         {
10727           ref = reg_equiv_mem[REGNO (ref)];
10728           base = find_replacement (&XEXP (ref, 0));
10729         }
10730       else
10731         /* The slot is out of range, or was dressed up in a SUBREG.  */
10732         base = reg_equiv_address[REGNO (ref)];
10733     }
10734   else
10735     base = find_replacement (&XEXP (ref, 0));
10736
10737   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
10738
10739   /* Handle the case where the address is too complex to be offset by 1.  */
10740   if (GET_CODE (base) == MINUS
10741       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
10742     {
10743       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10744
10745       /* Be careful not to destroy OUTVAL.  */
10746       if (reg_overlap_mentioned_p (base_plus, outval))
10747         {
10748           /* Updating base_plus might destroy outval, see if we can
10749              swap the scratch and base_plus.  */
10750           if (!reg_overlap_mentioned_p (scratch, outval))
10751             {
10752               rtx tmp = scratch;
10753               scratch = base_plus;
10754               base_plus = tmp;
10755             }
10756           else
10757             {
10758               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
10759
10760               /* Be conservative and copy OUTVAL into the scratch now,
10761                  this should only be necessary if outval is a subreg
10762                  of something larger than a word.  */
10763               /* XXX Might this clobber base?  I can't see how it can,
10764                  since scratch is known to overlap with OUTVAL, and
10765                  must be wider than a word.  */
10766               emit_insn (gen_movhi (scratch_hi, outval));
10767               outval = scratch_hi;
10768             }
10769         }
10770
10771       emit_set_insn (base_plus, base);
10772       base = base_plus;
10773     }
10774   else if (GET_CODE (base) == PLUS)
10775     {
10776       /* The addend must be CONST_INT, or we would have dealt with it above.  */
10777       HOST_WIDE_INT hi, lo;
10778
10779       offset += INTVAL (XEXP (base, 1));
10780       base = XEXP (base, 0);
10781
10782       /* Rework the address into a legal sequence of insns.  */
10783       /* Valid range for lo is -4095 -> 4095 */
10784       lo = (offset >= 0
10785             ? (offset & 0xfff)
10786             : -((-offset) & 0xfff));
10787
10788       /* Corner case, if lo is the max offset then we would be out of range
10789          once we have added the additional 1 below, so bump the msb into the
10790          pre-loading insn(s).  */
10791       if (lo == 4095)
10792         lo &= 0x7ff;
10793
10794       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
10795              ^ (HOST_WIDE_INT) 0x80000000)
10796             - (HOST_WIDE_INT) 0x80000000);
10797
10798       gcc_assert (hi + lo == offset);
10799
10800       if (hi != 0)
10801         {
10802           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10803
10804           /* Be careful not to destroy OUTVAL.  */
10805           if (reg_overlap_mentioned_p (base_plus, outval))
10806             {
10807               /* Updating base_plus might destroy outval, see if we
10808                  can swap the scratch and base_plus.  */
10809               if (!reg_overlap_mentioned_p (scratch, outval))
10810                 {
10811                   rtx tmp = scratch;
10812                   scratch = base_plus;
10813                   base_plus = tmp;
10814                 }
10815               else
10816                 {
10817                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
10818
10819                   /* Be conservative and copy outval into scratch now,
10820                      this should only be necessary if outval is a
10821                      subreg of something larger than a word.  */
10822                   /* XXX Might this clobber base?  I can't see how it
10823                      can, since scratch is known to overlap with
10824                      outval.  */
10825                   emit_insn (gen_movhi (scratch_hi, outval));
10826                   outval = scratch_hi;
10827                 }
10828             }
10829
10830           /* Get the base address; addsi3 knows how to handle constants
10831              that require more than one insn.  */
10832           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
10833           base = base_plus;
10834           offset = lo;
10835         }
10836     }
10837
10838   if (BYTES_BIG_ENDIAN)
10839     {
10840       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
10841                                          plus_constant (base, offset + 1)),
10842                             gen_lowpart (QImode, outval)));
10843       emit_insn (gen_lshrsi3 (scratch,
10844                               gen_rtx_SUBREG (SImode, outval, 0),
10845                               GEN_INT (8)));
10846       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
10847                             gen_lowpart (QImode, scratch)));
10848     }
10849   else
10850     {
10851       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
10852                             gen_lowpart (QImode, outval)));
10853       emit_insn (gen_lshrsi3 (scratch,
10854                               gen_rtx_SUBREG (SImode, outval, 0),
10855                               GEN_INT (8)));
10856       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
10857                                          plus_constant (base, offset + 1)),
10858                             gen_lowpart (QImode, scratch)));
10859     }
10860 }
10861
10862 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
10863    (padded to the size of a word) should be passed in a register.  */
10864
10865 static bool
10866 arm_must_pass_in_stack (enum machine_mode mode, const_tree type)
10867 {
10868   if (TARGET_AAPCS_BASED)
10869     return must_pass_in_stack_var_size (mode, type);
10870   else
10871     return must_pass_in_stack_var_size_or_pad (mode, type);
10872 }
10873
10874
10875 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
10876    Return true if an argument passed on the stack should be padded upwards,
10877    i.e. if the least-significant byte has useful data.
10878    For legacy APCS ABIs we use the default.  For AAPCS based ABIs small
10879    aggregate types are placed in the lowest memory address.  */
10880
10881 bool
10882 arm_pad_arg_upward (enum machine_mode mode, const_tree type)
10883 {
10884   if (!TARGET_AAPCS_BASED)
10885     return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
10886
10887   if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
10888     return false;
10889
10890   return true;
10891 }
10892
10893
10894 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
10895    For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
10896    byte of the register has useful data, and return the opposite if the
10897    most significant byte does.
10898    For AAPCS, small aggregates and small complex types are always padded
10899    upwards.  */
10900
10901 bool
10902 arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
10903                     tree type, int first ATTRIBUTE_UNUSED)
10904 {
10905   if (TARGET_AAPCS_BASED
10906       && BYTES_BIG_ENDIAN
10907       && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10908       && int_size_in_bytes (type) <= 4)
10909     return true;
10910
10911   /* Otherwise, use default padding.  */
10912   return !BYTES_BIG_ENDIAN;
10913 }
10914
10915 \f
10916 /* Print a symbolic form of X to the debug file, F.  */
10917 static void
10918 arm_print_value (FILE *f, rtx x)
10919 {
10920   switch (GET_CODE (x))
10921     {
10922     case CONST_INT:
10923       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
10924       return;
10925
10926     case CONST_DOUBLE:
10927       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
10928       return;
10929
10930     case CONST_VECTOR:
10931       {
10932         int i;
10933
10934         fprintf (f, "<");
10935         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
10936           {
10937             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
10938             if (i < (CONST_VECTOR_NUNITS (x) - 1))
10939               fputc (',', f);
10940           }
10941         fprintf (f, ">");
10942       }
10943       return;
10944
10945     case CONST_STRING:
10946       fprintf (f, "\"%s\"", XSTR (x, 0));
10947       return;
10948
10949     case SYMBOL_REF:
10950       fprintf (f, "`%s'", XSTR (x, 0));
10951       return;
10952
10953     case LABEL_REF:
10954       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
10955       return;
10956
10957     case CONST:
10958       arm_print_value (f, XEXP (x, 0));
10959       return;
10960
10961     case PLUS:
10962       arm_print_value (f, XEXP (x, 0));
10963       fprintf (f, "+");
10964       arm_print_value (f, XEXP (x, 1));
10965       return;
10966
10967     case PC:
10968       fprintf (f, "pc");
10969       return;
10970
10971     default:
10972       fprintf (f, "????");
10973       return;
10974     }
10975 }
10976 \f
10977 /* Routines for manipulation of the constant pool.  */
10978
10979 /* Arm instructions cannot load a large constant directly into a
10980    register; they have to come from a pc relative load.  The constant
10981    must therefore be placed in the addressable range of the pc
10982    relative load.  Depending on the precise pc relative load
10983    instruction the range is somewhere between 256 bytes and 4k.  This
10984    means that we often have to dump a constant inside a function, and
10985    generate code to branch around it.
10986
10987    It is important to minimize this, since the branches will slow
10988    things down and make the code larger.
10989
10990    Normally we can hide the table after an existing unconditional
10991    branch so that there is no interruption of the flow, but in the
10992    worst case the code looks like this:
10993
10994         ldr     rn, L1
10995         ...
10996         b       L2
10997         align
10998         L1:     .long value
10999         L2:
11000         ...
11001
11002         ldr     rn, L3
11003         ...
11004         b       L4
11005         align
11006         L3:     .long value
11007         L4:
11008         ...
11009
11010    We fix this by performing a scan after scheduling, which notices
11011    which instructions need to have their operands fetched from the
11012    constant table and builds the table.
11013
11014    The algorithm starts by building a table of all the constants that
11015    need fixing up and all the natural barriers in the function (places
11016    where a constant table can be dropped without breaking the flow).
11017    For each fixup we note how far the pc-relative replacement will be
11018    able to reach and the offset of the instruction into the function.
11019
11020    Having built the table we then group the fixes together to form
11021    tables that are as large as possible (subject to addressing
11022    constraints) and emit each table of constants after the last
11023    barrier that is within range of all the instructions in the group.
11024    If a group does not contain a barrier, then we forcibly create one
11025    by inserting a jump instruction into the flow.  Once the table has
11026    been inserted, the insns are then modified to reference the
11027    relevant entry in the pool.
11028
11029    Possible enhancements to the algorithm (not implemented) are:
11030
11031    1) For some processors and object formats, there may be benefit in
11032    aligning the pools to the start of cache lines; this alignment
11033    would need to be taken into account when calculating addressability
11034    of a pool.  */
11035
11036 /* These typedefs are located at the start of this file, so that
11037    they can be used in the prototypes there.  This comment is to
11038    remind readers of that fact so that the following structures
11039    can be understood more easily.
11040
11041      typedef struct minipool_node    Mnode;
11042      typedef struct minipool_fixup   Mfix;  */
11043
11044 struct minipool_node
11045 {
11046   /* Doubly linked chain of entries.  */
11047   Mnode * next;
11048   Mnode * prev;
11049   /* The maximum offset into the code that this entry can be placed.  While
11050      pushing fixes for forward references, all entries are sorted in order
11051      of increasing max_address.  */
11052   HOST_WIDE_INT max_address;
11053   /* Similarly for an entry inserted for a backwards ref.  */
11054   HOST_WIDE_INT min_address;
11055   /* The number of fixes referencing this entry.  This can become zero
11056      if we "unpush" an entry.  In this case we ignore the entry when we
11057      come to emit the code.  */
11058   int refcount;
11059   /* The offset from the start of the minipool.  */
11060   HOST_WIDE_INT offset;
11061   /* The value in table.  */
11062   rtx value;
11063   /* The mode of value.  */
11064   enum machine_mode mode;
11065   /* The size of the value.  With iWMMXt enabled
11066      sizes > 4 also imply an alignment of 8-bytes.  */
11067   int fix_size;
11068 };
11069
11070 struct minipool_fixup
11071 {
11072   Mfix *            next;
11073   rtx               insn;
11074   HOST_WIDE_INT     address;
11075   rtx *             loc;
11076   enum machine_mode mode;
11077   int               fix_size;
11078   rtx               value;
11079   Mnode *           minipool;
11080   HOST_WIDE_INT     forwards;
11081   HOST_WIDE_INT     backwards;
11082 };
11083
11084 /* Fixes less than a word need padding out to a word boundary.  */
11085 #define MINIPOOL_FIX_SIZE(mode) \
11086   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
11087
11088 static Mnode *  minipool_vector_head;
11089 static Mnode *  minipool_vector_tail;
11090 static rtx      minipool_vector_label;
11091 static int      minipool_pad;
11092
11093 /* The linked list of all minipool fixes required for this function.  */
11094 Mfix *          minipool_fix_head;
11095 Mfix *          minipool_fix_tail;
11096 /* The fix entry for the current minipool, once it has been placed.  */
11097 Mfix *          minipool_barrier;
11098
11099 /* Determines if INSN is the start of a jump table.  Returns the end
11100    of the TABLE or NULL_RTX.  */
11101 static rtx
11102 is_jump_table (rtx insn)
11103 {
11104   rtx table;
11105
11106   if (GET_CODE (insn) == JUMP_INSN
11107       && JUMP_LABEL (insn) != NULL
11108       && ((table = next_real_insn (JUMP_LABEL (insn)))
11109           == next_real_insn (insn))
11110       && table != NULL
11111       && GET_CODE (table) == JUMP_INSN
11112       && (GET_CODE (PATTERN (table)) == ADDR_VEC
11113           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
11114     return table;
11115
11116   return NULL_RTX;
11117 }
11118
11119 #ifndef JUMP_TABLES_IN_TEXT_SECTION
11120 #define JUMP_TABLES_IN_TEXT_SECTION 0
11121 #endif
11122
11123 static HOST_WIDE_INT
11124 get_jump_table_size (rtx insn)
11125 {
11126   /* ADDR_VECs only take room if read-only data does into the text
11127      section.  */
11128   if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
11129     {
11130       rtx body = PATTERN (insn);
11131       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
11132       HOST_WIDE_INT size;
11133       HOST_WIDE_INT modesize;
11134
11135       modesize = GET_MODE_SIZE (GET_MODE (body));
11136       size = modesize * XVECLEN (body, elt);
11137       switch (modesize)
11138         {
11139         case 1:
11140           /* Round up size  of TBB table to a halfword boundary.  */
11141           size = (size + 1) & ~(HOST_WIDE_INT)1;
11142           break;
11143         case 2:
11144           /* No padding necessary for TBH.  */
11145           break;
11146         case 4:
11147           /* Add two bytes for alignment on Thumb.  */
11148           if (TARGET_THUMB)
11149             size += 2;
11150           break;
11151         default:
11152           gcc_unreachable ();
11153         }
11154       return size;
11155     }
11156
11157   return 0;
11158 }
11159
11160 /* Move a minipool fix MP from its current location to before MAX_MP.
11161    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
11162    constraints may need updating.  */
11163 static Mnode *
11164 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
11165                                HOST_WIDE_INT max_address)
11166 {
11167   /* The code below assumes these are different.  */
11168   gcc_assert (mp != max_mp);
11169
11170   if (max_mp == NULL)
11171     {
11172       if (max_address < mp->max_address)
11173         mp->max_address = max_address;
11174     }
11175   else
11176     {
11177       if (max_address > max_mp->max_address - mp->fix_size)
11178         mp->max_address = max_mp->max_address - mp->fix_size;
11179       else
11180         mp->max_address = max_address;
11181
11182       /* Unlink MP from its current position.  Since max_mp is non-null,
11183        mp->prev must be non-null.  */
11184       mp->prev->next = mp->next;
11185       if (mp->next != NULL)
11186         mp->next->prev = mp->prev;
11187       else
11188         minipool_vector_tail = mp->prev;
11189
11190       /* Re-insert it before MAX_MP.  */
11191       mp->next = max_mp;
11192       mp->prev = max_mp->prev;
11193       max_mp->prev = mp;
11194
11195       if (mp->prev != NULL)
11196         mp->prev->next = mp;
11197       else
11198         minipool_vector_head = mp;
11199     }
11200
11201   /* Save the new entry.  */
11202   max_mp = mp;
11203
11204   /* Scan over the preceding entries and adjust their addresses as
11205      required.  */
11206   while (mp->prev != NULL
11207          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
11208     {
11209       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
11210       mp = mp->prev;
11211     }
11212
11213   return max_mp;
11214 }
11215
11216 /* Add a constant to the minipool for a forward reference.  Returns the
11217    node added or NULL if the constant will not fit in this pool.  */
11218 static Mnode *
11219 add_minipool_forward_ref (Mfix *fix)
11220 {
11221   /* If set, max_mp is the first pool_entry that has a lower
11222      constraint than the one we are trying to add.  */
11223   Mnode *       max_mp = NULL;
11224   HOST_WIDE_INT max_address = fix->address + fix->forwards - minipool_pad;
11225   Mnode *       mp;
11226
11227   /* If the minipool starts before the end of FIX->INSN then this FIX
11228      can not be placed into the current pool.  Furthermore, adding the
11229      new constant pool entry may cause the pool to start FIX_SIZE bytes
11230      earlier.  */
11231   if (minipool_vector_head &&
11232       (fix->address + get_attr_length (fix->insn)
11233        >= minipool_vector_head->max_address - fix->fix_size))
11234     return NULL;
11235
11236   /* Scan the pool to see if a constant with the same value has
11237      already been added.  While we are doing this, also note the
11238      location where we must insert the constant if it doesn't already
11239      exist.  */
11240   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11241     {
11242       if (GET_CODE (fix->value) == GET_CODE (mp->value)
11243           && fix->mode == mp->mode
11244           && (GET_CODE (fix->value) != CODE_LABEL
11245               || (CODE_LABEL_NUMBER (fix->value)
11246                   == CODE_LABEL_NUMBER (mp->value)))
11247           && rtx_equal_p (fix->value, mp->value))
11248         {
11249           /* More than one fix references this entry.  */
11250           mp->refcount++;
11251           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
11252         }
11253
11254       /* Note the insertion point if necessary.  */
11255       if (max_mp == NULL
11256           && mp->max_address > max_address)
11257         max_mp = mp;
11258
11259       /* If we are inserting an 8-bytes aligned quantity and
11260          we have not already found an insertion point, then
11261          make sure that all such 8-byte aligned quantities are
11262          placed at the start of the pool.  */
11263       if (ARM_DOUBLEWORD_ALIGN
11264           && max_mp == NULL
11265           && fix->fix_size >= 8
11266           && mp->fix_size < 8)
11267         {
11268           max_mp = mp;
11269           max_address = mp->max_address;
11270         }
11271     }
11272
11273   /* The value is not currently in the minipool, so we need to create
11274      a new entry for it.  If MAX_MP is NULL, the entry will be put on
11275      the end of the list since the placement is less constrained than
11276      any existing entry.  Otherwise, we insert the new fix before
11277      MAX_MP and, if necessary, adjust the constraints on the other
11278      entries.  */
11279   mp = XNEW (Mnode);
11280   mp->fix_size = fix->fix_size;
11281   mp->mode = fix->mode;
11282   mp->value = fix->value;
11283   mp->refcount = 1;
11284   /* Not yet required for a backwards ref.  */
11285   mp->min_address = -65536;
11286
11287   if (max_mp == NULL)
11288     {
11289       mp->max_address = max_address;
11290       mp->next = NULL;
11291       mp->prev = minipool_vector_tail;
11292
11293       if (mp->prev == NULL)
11294         {
11295           minipool_vector_head = mp;
11296           minipool_vector_label = gen_label_rtx ();
11297         }
11298       else
11299         mp->prev->next = mp;
11300
11301       minipool_vector_tail = mp;
11302     }
11303   else
11304     {
11305       if (max_address > max_mp->max_address - mp->fix_size)
11306         mp->max_address = max_mp->max_address - mp->fix_size;
11307       else
11308         mp->max_address = max_address;
11309
11310       mp->next = max_mp;
11311       mp->prev = max_mp->prev;
11312       max_mp->prev = mp;
11313       if (mp->prev != NULL)
11314         mp->prev->next = mp;
11315       else
11316         minipool_vector_head = mp;
11317     }
11318
11319   /* Save the new entry.  */
11320   max_mp = mp;
11321
11322   /* Scan over the preceding entries and adjust their addresses as
11323      required.  */
11324   while (mp->prev != NULL
11325          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
11326     {
11327       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
11328       mp = mp->prev;
11329     }
11330
11331   return max_mp;
11332 }
11333
11334 static Mnode *
11335 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
11336                                 HOST_WIDE_INT  min_address)
11337 {
11338   HOST_WIDE_INT offset;
11339
11340   /* The code below assumes these are different.  */
11341   gcc_assert (mp != min_mp);
11342
11343   if (min_mp == NULL)
11344     {
11345       if (min_address > mp->min_address)
11346         mp->min_address = min_address;
11347     }
11348   else
11349     {
11350       /* We will adjust this below if it is too loose.  */
11351       mp->min_address = min_address;
11352
11353       /* Unlink MP from its current position.  Since min_mp is non-null,
11354          mp->next must be non-null.  */
11355       mp->next->prev = mp->prev;
11356       if (mp->prev != NULL)
11357         mp->prev->next = mp->next;
11358       else
11359         minipool_vector_head = mp->next;
11360
11361       /* Reinsert it after MIN_MP.  */
11362       mp->prev = min_mp;
11363       mp->next = min_mp->next;
11364       min_mp->next = mp;
11365       if (mp->next != NULL)
11366         mp->next->prev = mp;
11367       else
11368         minipool_vector_tail = mp;
11369     }
11370
11371   min_mp = mp;
11372
11373   offset = 0;
11374   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11375     {
11376       mp->offset = offset;
11377       if (mp->refcount > 0)
11378         offset += mp->fix_size;
11379
11380       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
11381         mp->next->min_address = mp->min_address + mp->fix_size;
11382     }
11383
11384   return min_mp;
11385 }
11386
11387 /* Add a constant to the minipool for a backward reference.  Returns the
11388    node added or NULL if the constant will not fit in this pool.
11389
11390    Note that the code for insertion for a backwards reference can be
11391    somewhat confusing because the calculated offsets for each fix do
11392    not take into account the size of the pool (which is still under
11393    construction.  */
11394 static Mnode *
11395 add_minipool_backward_ref (Mfix *fix)
11396 {
11397   /* If set, min_mp is the last pool_entry that has a lower constraint
11398      than the one we are trying to add.  */
11399   Mnode *min_mp = NULL;
11400   /* This can be negative, since it is only a constraint.  */
11401   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
11402   Mnode *mp;
11403
11404   /* If we can't reach the current pool from this insn, or if we can't
11405      insert this entry at the end of the pool without pushing other
11406      fixes out of range, then we don't try.  This ensures that we
11407      can't fail later on.  */
11408   if (min_address >= minipool_barrier->address
11409       || (minipool_vector_tail->min_address + fix->fix_size
11410           >= minipool_barrier->address))
11411     return NULL;
11412
11413   /* Scan the pool to see if a constant with the same value has
11414      already been added.  While we are doing this, also note the
11415      location where we must insert the constant if it doesn't already
11416      exist.  */
11417   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
11418     {
11419       if (GET_CODE (fix->value) == GET_CODE (mp->value)
11420           && fix->mode == mp->mode
11421           && (GET_CODE (fix->value) != CODE_LABEL
11422               || (CODE_LABEL_NUMBER (fix->value)
11423                   == CODE_LABEL_NUMBER (mp->value)))
11424           && rtx_equal_p (fix->value, mp->value)
11425           /* Check that there is enough slack to move this entry to the
11426              end of the table (this is conservative).  */
11427           && (mp->max_address
11428               > (minipool_barrier->address
11429                  + minipool_vector_tail->offset
11430                  + minipool_vector_tail->fix_size)))
11431         {
11432           mp->refcount++;
11433           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
11434         }
11435
11436       if (min_mp != NULL)
11437         mp->min_address += fix->fix_size;
11438       else
11439         {
11440           /* Note the insertion point if necessary.  */
11441           if (mp->min_address < min_address)
11442             {
11443               /* For now, we do not allow the insertion of 8-byte alignment
11444                  requiring nodes anywhere but at the start of the pool.  */
11445               if (ARM_DOUBLEWORD_ALIGN
11446                   && fix->fix_size >= 8 && mp->fix_size < 8)
11447                 return NULL;
11448               else
11449                 min_mp = mp;
11450             }
11451           else if (mp->max_address
11452                    < minipool_barrier->address + mp->offset + fix->fix_size)
11453             {
11454               /* Inserting before this entry would push the fix beyond
11455                  its maximum address (which can happen if we have
11456                  re-located a forwards fix); force the new fix to come
11457                  after it.  */
11458               if (ARM_DOUBLEWORD_ALIGN
11459                   && fix->fix_size >= 8 && mp->fix_size < 8)
11460                 return NULL;
11461               else
11462                 {
11463                   min_mp = mp;
11464                   min_address = mp->min_address + fix->fix_size;
11465                 }
11466             }
11467           /* Do not insert a non-8-byte aligned quantity before 8-byte
11468              aligned quantities.  */
11469           else if (ARM_DOUBLEWORD_ALIGN
11470                    && fix->fix_size < 8
11471                    && mp->fix_size >= 8)
11472             {
11473               min_mp = mp;
11474               min_address = mp->min_address + fix->fix_size;
11475             }
11476         }
11477     }
11478
11479   /* We need to create a new entry.  */
11480   mp = XNEW (Mnode);
11481   mp->fix_size = fix->fix_size;
11482   mp->mode = fix->mode;
11483   mp->value = fix->value;
11484   mp->refcount = 1;
11485   mp->max_address = minipool_barrier->address + 65536;
11486
11487   mp->min_address = min_address;
11488
11489   if (min_mp == NULL)
11490     {
11491       mp->prev = NULL;
11492       mp->next = minipool_vector_head;
11493
11494       if (mp->next == NULL)
11495         {
11496           minipool_vector_tail = mp;
11497           minipool_vector_label = gen_label_rtx ();
11498         }
11499       else
11500         mp->next->prev = mp;
11501
11502       minipool_vector_head = mp;
11503     }
11504   else
11505     {
11506       mp->next = min_mp->next;
11507       mp->prev = min_mp;
11508       min_mp->next = mp;
11509
11510       if (mp->next != NULL)
11511         mp->next->prev = mp;
11512       else
11513         minipool_vector_tail = mp;
11514     }
11515
11516   /* Save the new entry.  */
11517   min_mp = mp;
11518
11519   if (mp->prev)
11520     mp = mp->prev;
11521   else
11522     mp->offset = 0;
11523
11524   /* Scan over the following entries and adjust their offsets.  */
11525   while (mp->next != NULL)
11526     {
11527       if (mp->next->min_address < mp->min_address + mp->fix_size)
11528         mp->next->min_address = mp->min_address + mp->fix_size;
11529
11530       if (mp->refcount)
11531         mp->next->offset = mp->offset + mp->fix_size;
11532       else
11533         mp->next->offset = mp->offset;
11534
11535       mp = mp->next;
11536     }
11537
11538   return min_mp;
11539 }
11540
11541 static void
11542 assign_minipool_offsets (Mfix *barrier)
11543 {
11544   HOST_WIDE_INT offset = 0;
11545   Mnode *mp;
11546
11547   minipool_barrier = barrier;
11548
11549   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11550     {
11551       mp->offset = offset;
11552
11553       if (mp->refcount > 0)
11554         offset += mp->fix_size;
11555     }
11556 }
11557
11558 /* Output the literal table */
11559 static void
11560 dump_minipool (rtx scan)
11561 {
11562   Mnode * mp;
11563   Mnode * nmp;
11564   int align64 = 0;
11565
11566   if (ARM_DOUBLEWORD_ALIGN)
11567     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11568       if (mp->refcount > 0 && mp->fix_size >= 8)
11569         {
11570           align64 = 1;
11571           break;
11572         }
11573
11574   if (dump_file)
11575     fprintf (dump_file,
11576              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
11577              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
11578
11579   scan = emit_label_after (gen_label_rtx (), scan);
11580   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
11581   scan = emit_label_after (minipool_vector_label, scan);
11582
11583   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
11584     {
11585       if (mp->refcount > 0)
11586         {
11587           if (dump_file)
11588             {
11589               fprintf (dump_file,
11590                        ";;  Offset %u, min %ld, max %ld ",
11591                        (unsigned) mp->offset, (unsigned long) mp->min_address,
11592                        (unsigned long) mp->max_address);
11593               arm_print_value (dump_file, mp->value);
11594               fputc ('\n', dump_file);
11595             }
11596
11597           switch (mp->fix_size)
11598             {
11599 #ifdef HAVE_consttable_1
11600             case 1:
11601               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
11602               break;
11603
11604 #endif
11605 #ifdef HAVE_consttable_2
11606             case 2:
11607               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
11608               break;
11609
11610 #endif
11611 #ifdef HAVE_consttable_4
11612             case 4:
11613               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
11614               break;
11615
11616 #endif
11617 #ifdef HAVE_consttable_8
11618             case 8:
11619               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
11620               break;
11621
11622 #endif
11623 #ifdef HAVE_consttable_16
11624             case 16:
11625               scan = emit_insn_after (gen_consttable_16 (mp->value), scan);
11626               break;
11627
11628 #endif
11629             default:
11630               gcc_unreachable ();
11631             }
11632         }
11633
11634       nmp = mp->next;
11635       free (mp);
11636     }
11637
11638   minipool_vector_head = minipool_vector_tail = NULL;
11639   scan = emit_insn_after (gen_consttable_end (), scan);
11640   scan = emit_barrier_after (scan);
11641 }
11642
11643 /* Return the cost of forcibly inserting a barrier after INSN.  */
11644 static int
11645 arm_barrier_cost (rtx insn)
11646 {
11647   /* Basing the location of the pool on the loop depth is preferable,
11648      but at the moment, the basic block information seems to be
11649      corrupt by this stage of the compilation.  */
11650   int base_cost = 50;
11651   rtx next = next_nonnote_insn (insn);
11652
11653   if (next != NULL && GET_CODE (next) == CODE_LABEL)
11654     base_cost -= 20;
11655
11656   switch (GET_CODE (insn))
11657     {
11658     case CODE_LABEL:
11659       /* It will always be better to place the table before the label, rather
11660          than after it.  */
11661       return 50;
11662
11663     case INSN:
11664     case CALL_INSN:
11665       return base_cost;
11666
11667     case JUMP_INSN:
11668       return base_cost - 10;
11669
11670     default:
11671       return base_cost + 10;
11672     }
11673 }
11674
11675 /* Find the best place in the insn stream in the range
11676    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
11677    Create the barrier by inserting a jump and add a new fix entry for
11678    it.  */
11679 static Mfix *
11680 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
11681 {
11682   HOST_WIDE_INT count = 0;
11683   rtx barrier;
11684   rtx from = fix->insn;
11685   /* The instruction after which we will insert the jump.  */
11686   rtx selected = NULL;
11687   int selected_cost;
11688   /* The address at which the jump instruction will be placed.  */
11689   HOST_WIDE_INT selected_address;
11690   Mfix * new_fix;
11691   HOST_WIDE_INT max_count = max_address - fix->address;
11692   rtx label = gen_label_rtx ();
11693
11694   selected_cost = arm_barrier_cost (from);
11695   selected_address = fix->address;
11696
11697   while (from && count < max_count)
11698     {
11699       rtx tmp;
11700       int new_cost;
11701
11702       /* This code shouldn't have been called if there was a natural barrier
11703          within range.  */
11704       gcc_assert (GET_CODE (from) != BARRIER);
11705
11706       /* Count the length of this insn.  */
11707       count += get_attr_length (from);
11708
11709       /* If there is a jump table, add its length.  */
11710       tmp = is_jump_table (from);
11711       if (tmp != NULL)
11712         {
11713           count += get_jump_table_size (tmp);
11714
11715           /* Jump tables aren't in a basic block, so base the cost on
11716              the dispatch insn.  If we select this location, we will
11717              still put the pool after the table.  */
11718           new_cost = arm_barrier_cost (from);
11719
11720           if (count < max_count 
11721               && (!selected || new_cost <= selected_cost))
11722             {
11723               selected = tmp;
11724               selected_cost = new_cost;
11725               selected_address = fix->address + count;
11726             }
11727
11728           /* Continue after the dispatch table.  */
11729           from = NEXT_INSN (tmp);
11730           continue;
11731         }
11732
11733       new_cost = arm_barrier_cost (from);
11734
11735       if (count < max_count
11736           && (!selected || new_cost <= selected_cost))
11737         {
11738           selected = from;
11739           selected_cost = new_cost;
11740           selected_address = fix->address + count;
11741         }
11742
11743       from = NEXT_INSN (from);
11744     }
11745
11746   /* Make sure that we found a place to insert the jump.  */
11747   gcc_assert (selected);
11748
11749   /* Create a new JUMP_INSN that branches around a barrier.  */
11750   from = emit_jump_insn_after (gen_jump (label), selected);
11751   JUMP_LABEL (from) = label;
11752   barrier = emit_barrier_after (from);
11753   emit_label_after (label, barrier);
11754
11755   /* Create a minipool barrier entry for the new barrier.  */
11756   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
11757   new_fix->insn = barrier;
11758   new_fix->address = selected_address;
11759   new_fix->next = fix->next;
11760   fix->next = new_fix;
11761
11762   return new_fix;
11763 }
11764
11765 /* Record that there is a natural barrier in the insn stream at
11766    ADDRESS.  */
11767 static void
11768 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
11769 {
11770   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
11771
11772   fix->insn = insn;
11773   fix->address = address;
11774
11775   fix->next = NULL;
11776   if (minipool_fix_head != NULL)
11777     minipool_fix_tail->next = fix;
11778   else
11779     minipool_fix_head = fix;
11780
11781   minipool_fix_tail = fix;
11782 }
11783
11784 /* Record INSN, which will need fixing up to load a value from the
11785    minipool.  ADDRESS is the offset of the insn since the start of the
11786    function; LOC is a pointer to the part of the insn which requires
11787    fixing; VALUE is the constant that must be loaded, which is of type
11788    MODE.  */
11789 static void
11790 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
11791                    enum machine_mode mode, rtx value)
11792 {
11793   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
11794
11795   fix->insn = insn;
11796   fix->address = address;
11797   fix->loc = loc;
11798   fix->mode = mode;
11799   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
11800   fix->value = value;
11801   fix->forwards = get_attr_pool_range (insn);
11802   fix->backwards = get_attr_neg_pool_range (insn);
11803   fix->minipool = NULL;
11804
11805   /* If an insn doesn't have a range defined for it, then it isn't
11806      expecting to be reworked by this code.  Better to stop now than
11807      to generate duff assembly code.  */
11808   gcc_assert (fix->forwards || fix->backwards);
11809
11810   /* If an entry requires 8-byte alignment then assume all constant pools
11811      require 4 bytes of padding.  Trying to do this later on a per-pool
11812      basis is awkward because existing pool entries have to be modified.  */
11813   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size >= 8)
11814     minipool_pad = 4;
11815
11816   if (dump_file)
11817     {
11818       fprintf (dump_file,
11819                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
11820                GET_MODE_NAME (mode),
11821                INSN_UID (insn), (unsigned long) address,
11822                -1 * (long)fix->backwards, (long)fix->forwards);
11823       arm_print_value (dump_file, fix->value);
11824       fprintf (dump_file, "\n");
11825     }
11826
11827   /* Add it to the chain of fixes.  */
11828   fix->next = NULL;
11829
11830   if (minipool_fix_head != NULL)
11831     minipool_fix_tail->next = fix;
11832   else
11833     minipool_fix_head = fix;
11834
11835   minipool_fix_tail = fix;
11836 }
11837
11838 /* Return the cost of synthesizing a 64-bit constant VAL inline.
11839    Returns the number of insns needed, or 99 if we don't know how to
11840    do it.  */
11841 int
11842 arm_const_double_inline_cost (rtx val)
11843 {
11844   rtx lowpart, highpart;
11845   enum machine_mode mode;
11846
11847   mode = GET_MODE (val);
11848
11849   if (mode == VOIDmode)
11850     mode = DImode;
11851
11852   gcc_assert (GET_MODE_SIZE (mode) == 8);
11853
11854   lowpart = gen_lowpart (SImode, val);
11855   highpart = gen_highpart_mode (SImode, mode, val);
11856
11857   gcc_assert (GET_CODE (lowpart) == CONST_INT);
11858   gcc_assert (GET_CODE (highpart) == CONST_INT);
11859
11860   return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
11861                             NULL_RTX, NULL_RTX, 0, 0)
11862           + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
11863                               NULL_RTX, NULL_RTX, 0, 0));
11864 }
11865
11866 /* Return true if it is worthwhile to split a 64-bit constant into two
11867    32-bit operations.  This is the case if optimizing for size, or
11868    if we have load delay slots, or if one 32-bit part can be done with
11869    a single data operation.  */
11870 bool
11871 arm_const_double_by_parts (rtx val)
11872 {
11873   enum machine_mode mode = GET_MODE (val);
11874   rtx part;
11875
11876   if (optimize_size || arm_ld_sched)
11877     return true;
11878
11879   if (mode == VOIDmode)
11880     mode = DImode;
11881
11882   part = gen_highpart_mode (SImode, mode, val);
11883
11884   gcc_assert (GET_CODE (part) == CONST_INT);
11885
11886   if (const_ok_for_arm (INTVAL (part))
11887       || const_ok_for_arm (~INTVAL (part)))
11888     return true;
11889
11890   part = gen_lowpart (SImode, val);
11891
11892   gcc_assert (GET_CODE (part) == CONST_INT);
11893
11894   if (const_ok_for_arm (INTVAL (part))
11895       || const_ok_for_arm (~INTVAL (part)))
11896     return true;
11897
11898   return false;
11899 }
11900
11901 /* Return true if it is possible to inline both the high and low parts
11902    of a 64-bit constant into 32-bit data processing instructions.  */
11903 bool
11904 arm_const_double_by_immediates (rtx val)
11905 {
11906   enum machine_mode mode = GET_MODE (val);
11907   rtx part;
11908
11909   if (mode == VOIDmode)
11910     mode = DImode;
11911
11912   part = gen_highpart_mode (SImode, mode, val);
11913
11914   gcc_assert (GET_CODE (part) == CONST_INT);
11915
11916   if (!const_ok_for_arm (INTVAL (part)))
11917     return false;
11918
11919   part = gen_lowpart (SImode, val);
11920
11921   gcc_assert (GET_CODE (part) == CONST_INT);
11922
11923   if (!const_ok_for_arm (INTVAL (part)))
11924     return false;
11925
11926   return true;
11927 }
11928
11929 /* Scan INSN and note any of its operands that need fixing.
11930    If DO_PUSHES is false we do not actually push any of the fixups
11931    needed.  The function returns TRUE if any fixups were needed/pushed.
11932    This is used by arm_memory_load_p() which needs to know about loads
11933    of constants that will be converted into minipool loads.  */
11934 static bool
11935 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
11936 {
11937   bool result = false;
11938   int opno;
11939
11940   extract_insn (insn);
11941
11942   if (!constrain_operands (1))
11943     fatal_insn_not_found (insn);
11944
11945   if (recog_data.n_alternatives == 0)
11946     return false;
11947
11948   /* Fill in recog_op_alt with information about the constraints of
11949      this insn.  */
11950   preprocess_constraints ();
11951
11952   for (opno = 0; opno < recog_data.n_operands; opno++)
11953     {
11954       /* Things we need to fix can only occur in inputs.  */
11955       if (recog_data.operand_type[opno] != OP_IN)
11956         continue;
11957
11958       /* If this alternative is a memory reference, then any mention
11959          of constants in this alternative is really to fool reload
11960          into allowing us to accept one there.  We need to fix them up
11961          now so that we output the right code.  */
11962       if (recog_op_alt[opno][which_alternative].memory_ok)
11963         {
11964           rtx op = recog_data.operand[opno];
11965
11966           if (CONSTANT_P (op))
11967             {
11968               if (do_pushes)
11969                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
11970                                    recog_data.operand_mode[opno], op);
11971               result = true;
11972             }
11973           else if (GET_CODE (op) == MEM
11974                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
11975                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
11976             {
11977               if (do_pushes)
11978                 {
11979                   rtx cop = avoid_constant_pool_reference (op);
11980
11981                   /* Casting the address of something to a mode narrower
11982                      than a word can cause avoid_constant_pool_reference()
11983                      to return the pool reference itself.  That's no good to
11984                      us here.  Lets just hope that we can use the
11985                      constant pool value directly.  */
11986                   if (op == cop)
11987                     cop = get_pool_constant (XEXP (op, 0));
11988
11989                   push_minipool_fix (insn, address,
11990                                      recog_data.operand_loc[opno],
11991                                      recog_data.operand_mode[opno], cop);
11992                 }
11993
11994               result = true;
11995             }
11996         }
11997     }
11998
11999   return result;
12000 }
12001
12002 /* Convert instructions to their cc-clobbering variant if possible, since
12003    that allows us to use smaller encodings.  */
12004
12005 static void
12006 thumb2_reorg (void)
12007 {
12008   basic_block bb;
12009   regset_head live;
12010
12011   INIT_REG_SET (&live);
12012
12013   /* We are freeing block_for_insn in the toplev to keep compatibility
12014      with old MDEP_REORGS that are not CFG based.  Recompute it now.  */
12015   compute_bb_for_insn ();
12016   df_analyze ();
12017
12018   FOR_EACH_BB (bb)
12019     {
12020       rtx insn;
12021       COPY_REG_SET (&live, DF_LR_OUT (bb));
12022       df_simulate_initialize_backwards (bb, &live);
12023       FOR_BB_INSNS_REVERSE (bb, insn)
12024         {
12025           if (NONJUMP_INSN_P (insn)
12026               && !REGNO_REG_SET_P (&live, CC_REGNUM))
12027             {
12028               rtx pat = PATTERN (insn);
12029               if (GET_CODE (pat) == SET
12030                   && low_register_operand (XEXP (pat, 0), SImode)
12031                   && thumb_16bit_operator (XEXP (pat, 1), SImode)
12032                   && low_register_operand (XEXP (XEXP (pat, 1), 0), SImode)
12033                   && low_register_operand (XEXP (XEXP (pat, 1), 1), SImode))
12034                 {
12035                   rtx dst = XEXP (pat, 0);
12036                   rtx src = XEXP (pat, 1);
12037                   rtx op0 = XEXP (src, 0);
12038                   if (rtx_equal_p (dst, op0)
12039                       || GET_CODE (src) == PLUS || GET_CODE (src) == MINUS)
12040                     {
12041                       rtx ccreg = gen_rtx_REG (CCmode, CC_REGNUM);
12042                       rtx clobber = gen_rtx_CLOBBER (VOIDmode, ccreg);
12043                       rtvec vec = gen_rtvec (2, pat, clobber);
12044                       PATTERN (insn) = gen_rtx_PARALLEL (VOIDmode, vec);
12045                       INSN_CODE (insn) = -1;
12046                     }
12047                 }
12048             }
12049           if (NONDEBUG_INSN_P (insn))
12050             df_simulate_one_insn_backwards (bb, insn, &live);
12051         }
12052     }
12053   CLEAR_REG_SET (&live);
12054 }
12055
12056 /* Gcc puts the pool in the wrong place for ARM, since we can only
12057    load addresses a limited distance around the pc.  We do some
12058    special munging to move the constant pool values to the correct
12059    point in the code.  */
12060 static void
12061 arm_reorg (void)
12062 {
12063   rtx insn;
12064   HOST_WIDE_INT address = 0;
12065   Mfix * fix;
12066
12067   if (TARGET_THUMB2)
12068     thumb2_reorg ();
12069   
12070   minipool_fix_head = minipool_fix_tail = NULL;
12071
12072   /* The first insn must always be a note, or the code below won't
12073      scan it properly.  */
12074   insn = get_insns ();
12075   gcc_assert (GET_CODE (insn) == NOTE);
12076   minipool_pad = 0;
12077
12078   /* Scan all the insns and record the operands that will need fixing.  */
12079   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
12080     {
12081       if (TARGET_CIRRUS_FIX_INVALID_INSNS
12082           && (arm_cirrus_insn_p (insn)
12083               || GET_CODE (insn) == JUMP_INSN
12084               || arm_memory_load_p (insn)))
12085         cirrus_reorg (insn);
12086
12087       if (GET_CODE (insn) == BARRIER)
12088         push_minipool_barrier (insn, address);
12089       else if (INSN_P (insn))
12090         {
12091           rtx table;
12092
12093           note_invalid_constants (insn, address, true);
12094           address += get_attr_length (insn);
12095
12096           /* If the insn is a vector jump, add the size of the table
12097              and skip the table.  */
12098           if ((table = is_jump_table (insn)) != NULL)
12099             {
12100               address += get_jump_table_size (table);
12101               insn = table;
12102             }
12103         }
12104     }
12105
12106   fix = minipool_fix_head;
12107
12108   /* Now scan the fixups and perform the required changes.  */
12109   while (fix)
12110     {
12111       Mfix * ftmp;
12112       Mfix * fdel;
12113       Mfix *  last_added_fix;
12114       Mfix * last_barrier = NULL;
12115       Mfix * this_fix;
12116
12117       /* Skip any further barriers before the next fix.  */
12118       while (fix && GET_CODE (fix->insn) == BARRIER)
12119         fix = fix->next;
12120
12121       /* No more fixes.  */
12122       if (fix == NULL)
12123         break;
12124
12125       last_added_fix = NULL;
12126
12127       for (ftmp = fix; ftmp; ftmp = ftmp->next)
12128         {
12129           if (GET_CODE (ftmp->insn) == BARRIER)
12130             {
12131               if (ftmp->address >= minipool_vector_head->max_address)
12132                 break;
12133
12134               last_barrier = ftmp;
12135             }
12136           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
12137             break;
12138
12139           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
12140         }
12141
12142       /* If we found a barrier, drop back to that; any fixes that we
12143          could have reached but come after the barrier will now go in
12144          the next mini-pool.  */
12145       if (last_barrier != NULL)
12146         {
12147           /* Reduce the refcount for those fixes that won't go into this
12148              pool after all.  */
12149           for (fdel = last_barrier->next;
12150                fdel && fdel != ftmp;
12151                fdel = fdel->next)
12152             {
12153               fdel->minipool->refcount--;
12154               fdel->minipool = NULL;
12155             }
12156
12157           ftmp = last_barrier;
12158         }
12159       else
12160         {
12161           /* ftmp is first fix that we can't fit into this pool and
12162              there no natural barriers that we could use.  Insert a
12163              new barrier in the code somewhere between the previous
12164              fix and this one, and arrange to jump around it.  */
12165           HOST_WIDE_INT max_address;
12166
12167           /* The last item on the list of fixes must be a barrier, so
12168              we can never run off the end of the list of fixes without
12169              last_barrier being set.  */
12170           gcc_assert (ftmp);
12171
12172           max_address = minipool_vector_head->max_address;
12173           /* Check that there isn't another fix that is in range that
12174              we couldn't fit into this pool because the pool was
12175              already too large: we need to put the pool before such an
12176              instruction.  The pool itself may come just after the
12177              fix because create_fix_barrier also allows space for a
12178              jump instruction.  */
12179           if (ftmp->address < max_address)
12180             max_address = ftmp->address + 1;
12181
12182           last_barrier = create_fix_barrier (last_added_fix, max_address);
12183         }
12184
12185       assign_minipool_offsets (last_barrier);
12186
12187       while (ftmp)
12188         {
12189           if (GET_CODE (ftmp->insn) != BARRIER
12190               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
12191                   == NULL))
12192             break;
12193
12194           ftmp = ftmp->next;
12195         }
12196
12197       /* Scan over the fixes we have identified for this pool, fixing them
12198          up and adding the constants to the pool itself.  */
12199       for (this_fix = fix; this_fix && ftmp != this_fix;
12200            this_fix = this_fix->next)
12201         if (GET_CODE (this_fix->insn) != BARRIER)
12202           {
12203             rtx addr
12204               = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
12205                                                   minipool_vector_label),
12206                                this_fix->minipool->offset);
12207             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
12208           }
12209
12210       dump_minipool (last_barrier->insn);
12211       fix = ftmp;
12212     }
12213
12214   /* From now on we must synthesize any constants that we can't handle
12215      directly.  This can happen if the RTL gets split during final
12216      instruction generation.  */
12217   after_arm_reorg = 1;
12218
12219   /* Free the minipool memory.  */
12220   obstack_free (&minipool_obstack, minipool_startobj);
12221 }
12222 \f
12223 /* Routines to output assembly language.  */
12224
12225 /* If the rtx is the correct value then return the string of the number.
12226    In this way we can ensure that valid double constants are generated even
12227    when cross compiling.  */
12228 const char *
12229 fp_immediate_constant (rtx x)
12230 {
12231   REAL_VALUE_TYPE r;
12232   int i;
12233
12234   if (!fp_consts_inited)
12235     init_fp_table ();
12236
12237   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
12238   for (i = 0; i < 8; i++)
12239     if (REAL_VALUES_EQUAL (r, values_fp[i]))
12240       return strings_fp[i];
12241
12242   gcc_unreachable ();
12243 }
12244
12245 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
12246 static const char *
12247 fp_const_from_val (REAL_VALUE_TYPE *r)
12248 {
12249   int i;
12250
12251   if (!fp_consts_inited)
12252     init_fp_table ();
12253
12254   for (i = 0; i < 8; i++)
12255     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
12256       return strings_fp[i];
12257
12258   gcc_unreachable ();
12259 }
12260
12261 /* Output the operands of a LDM/STM instruction to STREAM.
12262    MASK is the ARM register set mask of which only bits 0-15 are important.
12263    REG is the base register, either the frame pointer or the stack pointer,
12264    INSTR is the possibly suffixed load or store instruction.
12265    RFE is nonzero if the instruction should also copy spsr to cpsr.  */
12266
12267 static void
12268 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
12269                  unsigned long mask, int rfe)
12270 {
12271   unsigned i;
12272   bool not_first = FALSE;
12273
12274   gcc_assert (!rfe || (mask & (1 << PC_REGNUM)));
12275   fputc ('\t', stream);
12276   asm_fprintf (stream, instr, reg);
12277   fputc ('{', stream);
12278
12279   for (i = 0; i <= LAST_ARM_REGNUM; i++)
12280     if (mask & (1 << i))
12281       {
12282         if (not_first)
12283           fprintf (stream, ", ");
12284
12285         asm_fprintf (stream, "%r", i);
12286         not_first = TRUE;
12287       }
12288
12289   if (rfe)
12290     fprintf (stream, "}^\n");
12291   else
12292     fprintf (stream, "}\n");
12293 }
12294
12295
12296 /* Output a FLDMD instruction to STREAM.
12297    BASE if the register containing the address.
12298    REG and COUNT specify the register range.
12299    Extra registers may be added to avoid hardware bugs.
12300
12301    We output FLDMD even for ARMv5 VFP implementations.  Although
12302    FLDMD is technically not supported until ARMv6, it is believed
12303    that all VFP implementations support its use in this context.  */
12304
12305 static void
12306 vfp_output_fldmd (FILE * stream, unsigned int base, int reg, int count)
12307 {
12308   int i;
12309
12310   /* Workaround ARM10 VFPr1 bug.  */
12311   if (count == 2 && !arm_arch6)
12312     {
12313       if (reg == 15)
12314         reg--;
12315       count++;
12316     }
12317
12318   /* FLDMD may not load more than 16 doubleword registers at a time. Split the
12319      load into multiple parts if we have to handle more than 16 registers.  */
12320   if (count > 16)
12321     {
12322       vfp_output_fldmd (stream, base, reg, 16);
12323       vfp_output_fldmd (stream, base, reg + 16, count - 16);
12324       return;
12325     }
12326
12327   fputc ('\t', stream);
12328   asm_fprintf (stream, "fldmfdd\t%r!, {", base);
12329
12330   for (i = reg; i < reg + count; i++)
12331     {
12332       if (i > reg)
12333         fputs (", ", stream);
12334       asm_fprintf (stream, "d%d", i);
12335     }
12336   fputs ("}\n", stream);
12337
12338 }
12339
12340
12341 /* Output the assembly for a store multiple.  */
12342
12343 const char *
12344 vfp_output_fstmd (rtx * operands)
12345 {
12346   char pattern[100];
12347   int p;
12348   int base;
12349   int i;
12350
12351   strcpy (pattern, "fstmfdd\t%m0!, {%P1");
12352   p = strlen (pattern);
12353
12354   gcc_assert (GET_CODE (operands[1]) == REG);
12355
12356   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
12357   for (i = 1; i < XVECLEN (operands[2], 0); i++)
12358     {
12359       p += sprintf (&pattern[p], ", d%d", base + i);
12360     }
12361   strcpy (&pattern[p], "}");
12362
12363   output_asm_insn (pattern, operands);
12364   return "";
12365 }
12366
12367
12368 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
12369    number of bytes pushed.  */
12370
12371 static int
12372 vfp_emit_fstmd (int base_reg, int count)
12373 {
12374   rtx par;
12375   rtx dwarf;
12376   rtx tmp, reg;
12377   int i;
12378
12379   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
12380      register pairs are stored by a store multiple insn.  We avoid this
12381      by pushing an extra pair.  */
12382   if (count == 2 && !arm_arch6)
12383     {
12384       if (base_reg == LAST_VFP_REGNUM - 3)
12385         base_reg -= 2;
12386       count++;
12387     }
12388
12389   /* FSTMD may not store more than 16 doubleword registers at once.  Split
12390      larger stores into multiple parts (up to a maximum of two, in
12391      practice).  */
12392   if (count > 16)
12393     {
12394       int saved;
12395       /* NOTE: base_reg is an internal register number, so each D register
12396          counts as 2.  */
12397       saved = vfp_emit_fstmd (base_reg + 32, count - 16);
12398       saved += vfp_emit_fstmd (base_reg, 16);
12399       return saved;
12400     }
12401
12402   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
12403   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
12404
12405   reg = gen_rtx_REG (DFmode, base_reg);
12406   base_reg += 2;
12407
12408   XVECEXP (par, 0, 0)
12409     = gen_rtx_SET (VOIDmode,
12410                    gen_frame_mem
12411                    (BLKmode,
12412                     gen_rtx_PRE_MODIFY (Pmode,
12413                                         stack_pointer_rtx,
12414                                         plus_constant
12415                                         (stack_pointer_rtx,
12416                                          - (count * 8)))
12417                     ),
12418                    gen_rtx_UNSPEC (BLKmode,
12419                                    gen_rtvec (1, reg),
12420                                    UNSPEC_PUSH_MULT));
12421
12422   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
12423                      plus_constant (stack_pointer_rtx, -(count * 8)));
12424   RTX_FRAME_RELATED_P (tmp) = 1;
12425   XVECEXP (dwarf, 0, 0) = tmp;
12426
12427   tmp = gen_rtx_SET (VOIDmode,
12428                      gen_frame_mem (DFmode, stack_pointer_rtx),
12429                      reg);
12430   RTX_FRAME_RELATED_P (tmp) = 1;
12431   XVECEXP (dwarf, 0, 1) = tmp;
12432
12433   for (i = 1; i < count; i++)
12434     {
12435       reg = gen_rtx_REG (DFmode, base_reg);
12436       base_reg += 2;
12437       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
12438
12439       tmp = gen_rtx_SET (VOIDmode,
12440                          gen_frame_mem (DFmode,
12441                                         plus_constant (stack_pointer_rtx,
12442                                                        i * 8)),
12443                          reg);
12444       RTX_FRAME_RELATED_P (tmp) = 1;
12445       XVECEXP (dwarf, 0, i + 1) = tmp;
12446     }
12447
12448   par = emit_insn (par);
12449   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
12450   RTX_FRAME_RELATED_P (par) = 1;
12451
12452   return count * 8;
12453 }
12454
12455 /* Emit a call instruction with pattern PAT.  ADDR is the address of
12456    the call target.  */
12457
12458 void
12459 arm_emit_call_insn (rtx pat, rtx addr)
12460 {
12461   rtx insn;
12462
12463   insn = emit_call_insn (pat);
12464
12465   /* The PIC register is live on entry to VxWorks PIC PLT entries.
12466      If the call might use such an entry, add a use of the PIC register
12467      to the instruction's CALL_INSN_FUNCTION_USAGE.  */
12468   if (TARGET_VXWORKS_RTP
12469       && flag_pic
12470       && GET_CODE (addr) == SYMBOL_REF
12471       && (SYMBOL_REF_DECL (addr)
12472           ? !targetm.binds_local_p (SYMBOL_REF_DECL (addr))
12473           : !SYMBOL_REF_LOCAL_P (addr)))
12474     {
12475       require_pic_register ();
12476       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), cfun->machine->pic_reg);
12477     }
12478 }
12479
12480 /* Output a 'call' insn.  */
12481 const char *
12482 output_call (rtx *operands)
12483 {
12484   gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly.  */
12485
12486   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
12487   if (REGNO (operands[0]) == LR_REGNUM)
12488     {
12489       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
12490       output_asm_insn ("mov%?\t%0, %|lr", operands);
12491     }
12492
12493   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
12494
12495   if (TARGET_INTERWORK || arm_arch4t)
12496     output_asm_insn ("bx%?\t%0", operands);
12497   else
12498     output_asm_insn ("mov%?\t%|pc, %0", operands);
12499
12500   return "";
12501 }
12502
12503 /* Output a 'call' insn that is a reference in memory. This is
12504    disabled for ARMv5 and we prefer a blx instead because otherwise
12505    there's a significant performance overhead.  */
12506 const char *
12507 output_call_mem (rtx *operands)
12508 {
12509   gcc_assert (!arm_arch5);
12510   if (TARGET_INTERWORK)
12511     {
12512       output_asm_insn ("ldr%?\t%|ip, %0", operands);
12513       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
12514       output_asm_insn ("bx%?\t%|ip", operands);
12515     }
12516   else if (regno_use_in (LR_REGNUM, operands[0]))
12517     {
12518       /* LR is used in the memory address.  We load the address in the
12519          first instruction.  It's safe to use IP as the target of the
12520          load since the call will kill it anyway.  */
12521       output_asm_insn ("ldr%?\t%|ip, %0", operands);
12522       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
12523       if (arm_arch4t)
12524         output_asm_insn ("bx%?\t%|ip", operands);
12525       else
12526         output_asm_insn ("mov%?\t%|pc, %|ip", operands);
12527     }
12528   else
12529     {
12530       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
12531       output_asm_insn ("ldr%?\t%|pc, %0", operands);
12532     }
12533
12534   return "";
12535 }
12536
12537
12538 /* Output a move from arm registers to an fpa registers.
12539    OPERANDS[0] is an fpa register.
12540    OPERANDS[1] is the first registers of an arm register pair.  */
12541 const char *
12542 output_mov_long_double_fpa_from_arm (rtx *operands)
12543 {
12544   int arm_reg0 = REGNO (operands[1]);
12545   rtx ops[3];
12546
12547   gcc_assert (arm_reg0 != IP_REGNUM);
12548
12549   ops[0] = gen_rtx_REG (SImode, arm_reg0);
12550   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
12551   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
12552
12553   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
12554   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
12555
12556   return "";
12557 }
12558
12559 /* Output a move from an fpa register to arm registers.
12560    OPERANDS[0] is the first registers of an arm register pair.
12561    OPERANDS[1] is an fpa register.  */
12562 const char *
12563 output_mov_long_double_arm_from_fpa (rtx *operands)
12564 {
12565   int arm_reg0 = REGNO (operands[0]);
12566   rtx ops[3];
12567
12568   gcc_assert (arm_reg0 != IP_REGNUM);
12569
12570   ops[0] = gen_rtx_REG (SImode, arm_reg0);
12571   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
12572   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
12573
12574   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
12575   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
12576   return "";
12577 }
12578
12579 /* Output a move from arm registers to arm registers of a long double
12580    OPERANDS[0] is the destination.
12581    OPERANDS[1] is the source.  */
12582 const char *
12583 output_mov_long_double_arm_from_arm (rtx *operands)
12584 {
12585   /* We have to be careful here because the two might overlap.  */
12586   int dest_start = REGNO (operands[0]);
12587   int src_start = REGNO (operands[1]);
12588   rtx ops[2];
12589   int i;
12590
12591   if (dest_start < src_start)
12592     {
12593       for (i = 0; i < 3; i++)
12594         {
12595           ops[0] = gen_rtx_REG (SImode, dest_start + i);
12596           ops[1] = gen_rtx_REG (SImode, src_start + i);
12597           output_asm_insn ("mov%?\t%0, %1", ops);
12598         }
12599     }
12600   else
12601     {
12602       for (i = 2; i >= 0; i--)
12603         {
12604           ops[0] = gen_rtx_REG (SImode, dest_start + i);
12605           ops[1] = gen_rtx_REG (SImode, src_start + i);
12606           output_asm_insn ("mov%?\t%0, %1", ops);
12607         }
12608     }
12609
12610   return "";
12611 }
12612
12613 void
12614 arm_emit_movpair (rtx dest, rtx src)
12615  {
12616   /* If the src is an immediate, simplify it.  */
12617   if (CONST_INT_P (src))
12618     {
12619       HOST_WIDE_INT val = INTVAL (src);
12620       emit_set_insn (dest, GEN_INT (val & 0x0000ffff));
12621       if ((val >> 16) & 0x0000ffff)
12622         emit_set_insn (gen_rtx_ZERO_EXTRACT (SImode, dest, GEN_INT (16),
12623                                              GEN_INT (16)),
12624                        GEN_INT ((val >> 16) & 0x0000ffff));
12625       return;
12626     }
12627    emit_set_insn (dest, gen_rtx_HIGH (SImode, src));
12628    emit_set_insn (dest, gen_rtx_LO_SUM (SImode, dest, src));
12629  }
12630
12631 /* Output a move from arm registers to an fpa registers.
12632    OPERANDS[0] is an fpa register.
12633    OPERANDS[1] is the first registers of an arm register pair.  */
12634 const char *
12635 output_mov_double_fpa_from_arm (rtx *operands)
12636 {
12637   int arm_reg0 = REGNO (operands[1]);
12638   rtx ops[2];
12639
12640   gcc_assert (arm_reg0 != IP_REGNUM);
12641
12642   ops[0] = gen_rtx_REG (SImode, arm_reg0);
12643   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
12644   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1}", ops);
12645   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
12646   return "";
12647 }
12648
12649 /* Output a move from an fpa register to arm registers.
12650    OPERANDS[0] is the first registers of an arm register pair.
12651    OPERANDS[1] is an fpa register.  */
12652 const char *
12653 output_mov_double_arm_from_fpa (rtx *operands)
12654 {
12655   int arm_reg0 = REGNO (operands[0]);
12656   rtx ops[2];
12657
12658   gcc_assert (arm_reg0 != IP_REGNUM);
12659
12660   ops[0] = gen_rtx_REG (SImode, arm_reg0);
12661   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
12662   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
12663   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1}", ops);
12664   return "";
12665 }
12666
12667 /* Output a move between double words.
12668    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
12669    or MEM<-REG and all MEMs must be offsettable addresses.  */
12670 const char *
12671 output_move_double (rtx *operands)
12672 {
12673   enum rtx_code code0 = GET_CODE (operands[0]);
12674   enum rtx_code code1 = GET_CODE (operands[1]);
12675   rtx otherops[3];
12676
12677   if (code0 == REG)
12678     {
12679       unsigned int reg0 = REGNO (operands[0]);
12680
12681       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
12682
12683       gcc_assert (code1 == MEM);  /* Constraints should ensure this.  */
12684
12685       switch (GET_CODE (XEXP (operands[1], 0)))
12686         {
12687         case REG:
12688           if (TARGET_LDRD
12689               && !(fix_cm3_ldrd && reg0 == REGNO(XEXP (operands[1], 0))))
12690             output_asm_insn ("ldr%(d%)\t%0, [%m1]", operands);
12691           else
12692             output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
12693           break;
12694
12695         case PRE_INC:
12696           gcc_assert (TARGET_LDRD);
12697           output_asm_insn ("ldr%(d%)\t%0, [%m1, #8]!", operands);
12698           break;
12699
12700         case PRE_DEC:
12701           if (TARGET_LDRD)
12702             output_asm_insn ("ldr%(d%)\t%0, [%m1, #-8]!", operands);
12703           else
12704             output_asm_insn ("ldm%(db%)\t%m1!, %M0", operands);
12705           break;
12706
12707         case POST_INC:
12708           if (TARGET_LDRD)
12709             output_asm_insn ("ldr%(d%)\t%0, [%m1], #8", operands);
12710           else
12711             output_asm_insn ("ldm%(ia%)\t%m1!, %M0", operands);
12712           break;
12713
12714         case POST_DEC:
12715           gcc_assert (TARGET_LDRD);
12716           output_asm_insn ("ldr%(d%)\t%0, [%m1], #-8", operands);
12717           break;
12718
12719         case PRE_MODIFY:
12720         case POST_MODIFY:
12721           /* Autoicrement addressing modes should never have overlapping
12722              base and destination registers, and overlapping index registers
12723              are already prohibited, so this doesn't need to worry about
12724              fix_cm3_ldrd.  */
12725           otherops[0] = operands[0];
12726           otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
12727           otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
12728
12729           if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
12730             {
12731               if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
12732                 {
12733                   /* Registers overlap so split out the increment.  */
12734                   output_asm_insn ("add%?\t%1, %1, %2", otherops);
12735                   output_asm_insn ("ldr%(d%)\t%0, [%1] @split", otherops);
12736                 }
12737               else
12738                 {
12739                   /* Use a single insn if we can.
12740                      FIXME: IWMMXT allows offsets larger than ldrd can
12741                      handle, fix these up with a pair of ldr.  */
12742                   if (TARGET_THUMB2
12743                       || GET_CODE (otherops[2]) != CONST_INT
12744                       || (INTVAL (otherops[2]) > -256
12745                           && INTVAL (otherops[2]) < 256))
12746                     output_asm_insn ("ldr%(d%)\t%0, [%1, %2]!", otherops);
12747                   else
12748                     {
12749                       output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
12750                       output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
12751                     }
12752                 }
12753             }
12754           else
12755             {
12756               /* Use a single insn if we can.
12757                  FIXME: IWMMXT allows offsets larger than ldrd can handle,
12758                  fix these up with a pair of ldr.  */
12759               if (TARGET_THUMB2
12760                   || GET_CODE (otherops[2]) != CONST_INT
12761                   || (INTVAL (otherops[2]) > -256
12762                       && INTVAL (otherops[2]) < 256))
12763                 output_asm_insn ("ldr%(d%)\t%0, [%1], %2", otherops);
12764               else
12765                 {
12766                   output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
12767                   output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
12768                 }
12769             }
12770           break;
12771
12772         case LABEL_REF:
12773         case CONST:
12774           /* We might be able to use ldrd %0, %1 here.  However the range is
12775              different to ldr/adr, and it is broken on some ARMv7-M
12776              implementations.  */
12777           /* Use the second register of the pair to avoid problematic
12778              overlap.  */
12779           otherops[1] = operands[1];
12780           output_asm_insn ("adr%?\t%0, %1", otherops);
12781           operands[1] = otherops[0];
12782           if (TARGET_LDRD)
12783             output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
12784           else
12785             output_asm_insn ("ldm%(ia%)\t%1, %M0", operands);
12786           break;
12787
12788           /* ??? This needs checking for thumb2.  */
12789         default:
12790           if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
12791                                GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
12792             {
12793               otherops[0] = operands[0];
12794               otherops[1] = XEXP (XEXP (operands[1], 0), 0);
12795               otherops[2] = XEXP (XEXP (operands[1], 0), 1);
12796
12797               if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
12798                 {
12799                   if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
12800                     {
12801                       switch ((int) INTVAL (otherops[2]))
12802                         {
12803                         case -8:
12804                           output_asm_insn ("ldm%(db%)\t%1, %M0", otherops);
12805                           return "";
12806                         case -4:
12807                           if (TARGET_THUMB2)
12808                             break;
12809                           output_asm_insn ("ldm%(da%)\t%1, %M0", otherops);
12810                           return "";
12811                         case 4:
12812                           if (TARGET_THUMB2)
12813                             break;
12814                           output_asm_insn ("ldm%(ib%)\t%1, %M0", otherops);
12815                           return "";
12816                         }
12817                     }
12818                   otherops[0] = gen_rtx_REG(SImode, REGNO(operands[0]) + 1);
12819                   operands[1] = otherops[0];
12820                   if (TARGET_LDRD
12821                       && (GET_CODE (otherops[2]) == REG
12822                           || TARGET_THUMB2
12823                           || (GET_CODE (otherops[2]) == CONST_INT
12824                               && INTVAL (otherops[2]) > -256
12825                               && INTVAL (otherops[2]) < 256)))
12826                     {
12827                       if (reg_overlap_mentioned_p (operands[0],
12828                                                    otherops[2]))
12829                         {
12830                           rtx tmp;
12831                           /* Swap base and index registers over to
12832                              avoid a conflict.  */
12833                           tmp = otherops[1];
12834                           otherops[1] = otherops[2];
12835                           otherops[2] = tmp;
12836                         }
12837                       /* If both registers conflict, it will usually
12838                          have been fixed by a splitter.  */
12839                       if (reg_overlap_mentioned_p (operands[0], otherops[2])
12840                           || (fix_cm3_ldrd && reg0 == REGNO (otherops[1])))
12841                         {
12842                           output_asm_insn ("add%?\t%0, %1, %2", otherops);
12843                           output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
12844                         }
12845                       else
12846                         {
12847                           otherops[0] = operands[0];
12848                           output_asm_insn ("ldr%(d%)\t%0, [%1, %2]", otherops);
12849                         }
12850                       return "";
12851                     }
12852
12853                   if (GET_CODE (otherops[2]) == CONST_INT)
12854                     {
12855                       if (!(const_ok_for_arm (INTVAL (otherops[2]))))
12856                         output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
12857                       else
12858                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
12859                     }
12860                   else
12861                     output_asm_insn ("add%?\t%0, %1, %2", otherops);
12862                 }
12863               else
12864                 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
12865
12866               if (TARGET_LDRD)
12867                 return "ldr%(d%)\t%0, [%1]";
12868
12869               return "ldm%(ia%)\t%1, %M0";
12870             }
12871           else
12872             {
12873               otherops[1] = adjust_address (operands[1], SImode, 4);
12874               /* Take care of overlapping base/data reg.  */
12875               if (reg_mentioned_p (operands[0], operands[1]))
12876                 {
12877                   output_asm_insn ("ldr%?\t%0, %1", otherops);
12878                   output_asm_insn ("ldr%?\t%0, %1", operands);
12879                 }
12880               else
12881                 {
12882                   output_asm_insn ("ldr%?\t%0, %1", operands);
12883                   output_asm_insn ("ldr%?\t%0, %1", otherops);
12884                 }
12885             }
12886         }
12887     }
12888   else
12889     {
12890       /* Constraints should ensure this.  */
12891       gcc_assert (code0 == MEM && code1 == REG);
12892       gcc_assert (REGNO (operands[1]) != IP_REGNUM);
12893
12894       switch (GET_CODE (XEXP (operands[0], 0)))
12895         {
12896         case REG:
12897           if (TARGET_LDRD)
12898             output_asm_insn ("str%(d%)\t%1, [%m0]", operands);
12899           else
12900             output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
12901           break;
12902
12903         case PRE_INC:
12904           gcc_assert (TARGET_LDRD);
12905           output_asm_insn ("str%(d%)\t%1, [%m0, #8]!", operands);
12906           break;
12907
12908         case PRE_DEC:
12909           if (TARGET_LDRD)
12910             output_asm_insn ("str%(d%)\t%1, [%m0, #-8]!", operands);
12911           else
12912             output_asm_insn ("stm%(db%)\t%m0!, %M1", operands);
12913           break;
12914
12915         case POST_INC:
12916           if (TARGET_LDRD)
12917             output_asm_insn ("str%(d%)\t%1, [%m0], #8", operands);
12918           else
12919             output_asm_insn ("stm%(ia%)\t%m0!, %M1", operands);
12920           break;
12921
12922         case POST_DEC:
12923           gcc_assert (TARGET_LDRD);
12924           output_asm_insn ("str%(d%)\t%1, [%m0], #-8", operands);
12925           break;
12926
12927         case PRE_MODIFY:
12928         case POST_MODIFY:
12929           otherops[0] = operands[1];
12930           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
12931           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
12932
12933           /* IWMMXT allows offsets larger than ldrd can handle,
12934              fix these up with a pair of ldr.  */
12935           if (!TARGET_THUMB2
12936               && GET_CODE (otherops[2]) == CONST_INT
12937               && (INTVAL(otherops[2]) <= -256
12938                   || INTVAL(otherops[2]) >= 256))
12939             {
12940               if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
12941                 {
12942                   output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
12943                   output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
12944                 }
12945               else
12946                 {
12947                   output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
12948                   output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
12949                 }
12950             }
12951           else if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
12952             output_asm_insn ("str%(d%)\t%0, [%1, %2]!", otherops);
12953           else
12954             output_asm_insn ("str%(d%)\t%0, [%1], %2", otherops);
12955           break;
12956
12957         case PLUS:
12958           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
12959           if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
12960             {
12961               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
12962                 {
12963                 case -8:
12964                   output_asm_insn ("stm%(db%)\t%m0, %M1", operands);
12965                   return "";
12966
12967                 case -4:
12968                   if (TARGET_THUMB2)
12969                     break;
12970                   output_asm_insn ("stm%(da%)\t%m0, %M1", operands);
12971                   return "";
12972
12973                 case 4:
12974                   if (TARGET_THUMB2)
12975                     break;
12976                   output_asm_insn ("stm%(ib%)\t%m0, %M1", operands);
12977                   return "";
12978                 }
12979             }
12980           if (TARGET_LDRD
12981               && (GET_CODE (otherops[2]) == REG
12982                   || TARGET_THUMB2
12983                   || (GET_CODE (otherops[2]) == CONST_INT
12984                       && INTVAL (otherops[2]) > -256
12985                       && INTVAL (otherops[2]) < 256)))
12986             {
12987               otherops[0] = operands[1];
12988               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
12989               output_asm_insn ("str%(d%)\t%0, [%1, %2]", otherops);
12990               return "";
12991             }
12992           /* Fall through */
12993
12994         default:
12995           otherops[0] = adjust_address (operands[0], SImode, 4);
12996           otherops[1] = operands[1];
12997           output_asm_insn ("str%?\t%1, %0", operands);
12998           output_asm_insn ("str%?\t%H1, %0", otherops);
12999         }
13000     }
13001
13002   return "";
13003 }
13004
13005 /* Output a move, load or store for quad-word vectors in ARM registers.  Only
13006    handles MEMs accepted by neon_vector_mem_operand with TYPE=1.  */
13007
13008 const char *
13009 output_move_quad (rtx *operands)
13010 {
13011   if (REG_P (operands[0]))
13012     {
13013       /* Load, or reg->reg move.  */
13014
13015       if (MEM_P (operands[1]))
13016         {
13017           switch (GET_CODE (XEXP (operands[1], 0)))
13018             {
13019             case REG:
13020               output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
13021               break;
13022
13023             case LABEL_REF:
13024             case CONST:
13025               output_asm_insn ("adr%?\t%0, %1", operands);
13026               output_asm_insn ("ldm%(ia%)\t%0, %M0", operands);
13027               break;
13028
13029             default:
13030               gcc_unreachable ();
13031             }
13032         }
13033       else
13034         {
13035           rtx ops[2];
13036           int dest, src, i;
13037
13038           gcc_assert (REG_P (operands[1]));
13039
13040           dest = REGNO (operands[0]);
13041           src = REGNO (operands[1]);
13042
13043           /* This seems pretty dumb, but hopefully GCC won't try to do it
13044              very often.  */
13045           if (dest < src)
13046             for (i = 0; i < 4; i++)
13047               {
13048                 ops[0] = gen_rtx_REG (SImode, dest + i);
13049                 ops[1] = gen_rtx_REG (SImode, src + i);
13050                 output_asm_insn ("mov%?\t%0, %1", ops);
13051               }
13052           else
13053             for (i = 3; i >= 0; i--)
13054               {
13055                 ops[0] = gen_rtx_REG (SImode, dest + i);
13056                 ops[1] = gen_rtx_REG (SImode, src + i);
13057                 output_asm_insn ("mov%?\t%0, %1", ops);
13058               }
13059         }
13060     }
13061   else
13062     {
13063       gcc_assert (MEM_P (operands[0]));
13064       gcc_assert (REG_P (operands[1]));
13065       gcc_assert (!reg_overlap_mentioned_p (operands[1], operands[0]));
13066
13067       switch (GET_CODE (XEXP (operands[0], 0)))
13068         {
13069         case REG:
13070           output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
13071           break;
13072
13073         default:
13074           gcc_unreachable ();
13075         }
13076     }
13077
13078   return "";
13079 }
13080
13081 /* Output a VFP load or store instruction.  */
13082
13083 const char *
13084 output_move_vfp (rtx *operands)
13085 {
13086   rtx reg, mem, addr, ops[2];
13087   int load = REG_P (operands[0]);
13088   int dp = GET_MODE_SIZE (GET_MODE (operands[0])) == 8;
13089   int integer_p = GET_MODE_CLASS (GET_MODE (operands[0])) == MODE_INT;
13090   const char *templ;
13091   char buff[50];
13092   enum machine_mode mode;
13093
13094   reg = operands[!load];
13095   mem = operands[load];
13096
13097   mode = GET_MODE (reg);
13098
13099   gcc_assert (REG_P (reg));
13100   gcc_assert (IS_VFP_REGNUM (REGNO (reg)));
13101   gcc_assert (mode == SFmode
13102               || mode == DFmode
13103               || mode == SImode
13104               || mode == DImode
13105               || (TARGET_NEON && VALID_NEON_DREG_MODE (mode)));
13106   gcc_assert (MEM_P (mem));
13107
13108   addr = XEXP (mem, 0);
13109
13110   switch (GET_CODE (addr))
13111     {
13112     case PRE_DEC:
13113       templ = "f%smdb%c%%?\t%%0!, {%%%s1}%s";
13114       ops[0] = XEXP (addr, 0);
13115       ops[1] = reg;
13116       break;
13117
13118     case POST_INC:
13119       templ = "f%smia%c%%?\t%%0!, {%%%s1}%s";
13120       ops[0] = XEXP (addr, 0);
13121       ops[1] = reg;
13122       break;
13123
13124     default:
13125       templ = "f%s%c%%?\t%%%s0, %%1%s";
13126       ops[0] = reg;
13127       ops[1] = mem;
13128       break;
13129     }
13130
13131   sprintf (buff, templ,
13132            load ? "ld" : "st",
13133            dp ? 'd' : 's',
13134            dp ? "P" : "",
13135            integer_p ? "\t%@ int" : "");
13136   output_asm_insn (buff, ops);
13137
13138   return "";
13139 }
13140
13141 /* Output a Neon quad-word load or store, or a load or store for
13142    larger structure modes.
13143
13144    WARNING: The ordering of elements is weird in big-endian mode,
13145    because we use VSTM, as required by the EABI.  GCC RTL defines
13146    element ordering based on in-memory order.  This can be differ
13147    from the architectural ordering of elements within a NEON register.
13148    The intrinsics defined in arm_neon.h use the NEON register element
13149    ordering, not the GCC RTL element ordering.
13150
13151    For example, the in-memory ordering of a big-endian a quadword
13152    vector with 16-bit elements when stored from register pair {d0,d1}
13153    will be (lowest address first, d0[N] is NEON register element N):
13154
13155      [d0[3], d0[2], d0[1], d0[0], d1[7], d1[6], d1[5], d1[4]]
13156
13157    When necessary, quadword registers (dN, dN+1) are moved to ARM
13158    registers from rN in the order:
13159
13160      dN -> (rN+1, rN), dN+1 -> (rN+3, rN+2)
13161
13162    So that STM/LDM can be used on vectors in ARM registers, and the
13163    same memory layout will result as if VSTM/VLDM were used.  */
13164
13165 const char *
13166 output_move_neon (rtx *operands)
13167 {
13168   rtx reg, mem, addr, ops[2];
13169   int regno, load = REG_P (operands[0]);
13170   const char *templ;
13171   char buff[50];
13172   enum machine_mode mode;
13173
13174   reg = operands[!load];
13175   mem = operands[load];
13176
13177   mode = GET_MODE (reg);
13178
13179   gcc_assert (REG_P (reg));
13180   regno = REGNO (reg);
13181   gcc_assert (VFP_REGNO_OK_FOR_DOUBLE (regno)
13182               || NEON_REGNO_OK_FOR_QUAD (regno));
13183   gcc_assert (VALID_NEON_DREG_MODE (mode)
13184               || VALID_NEON_QREG_MODE (mode)
13185               || VALID_NEON_STRUCT_MODE (mode));
13186   gcc_assert (MEM_P (mem));
13187
13188   addr = XEXP (mem, 0);
13189
13190   /* Strip off const from addresses like (const (plus (...))).  */
13191   if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
13192     addr = XEXP (addr, 0);
13193
13194   switch (GET_CODE (addr))
13195     {
13196     case POST_INC:
13197       templ = "v%smia%%?\t%%0!, %%h1";
13198       ops[0] = XEXP (addr, 0);
13199       ops[1] = reg;
13200       break;
13201
13202     case PRE_DEC:
13203       /* FIXME: We should be using vld1/vst1 here in BE mode?  */
13204       templ = "v%smdb%%?\t%%0!, %%h1";
13205       ops[0] = XEXP (addr, 0);
13206       ops[1] = reg;
13207       break;
13208     
13209     case POST_MODIFY:
13210       /* FIXME: Not currently enabled in neon_vector_mem_operand.  */
13211       gcc_unreachable ();
13212
13213     case LABEL_REF:
13214     case PLUS:
13215       {
13216         int nregs = HARD_REGNO_NREGS (REGNO (reg), mode) / 2;
13217         int i;
13218         int overlap = -1;
13219         for (i = 0; i < nregs; i++)
13220           {
13221             /* We're only using DImode here because it's a convenient size.  */
13222             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * i);
13223             ops[1] = adjust_address (mem, DImode, 8 * i);
13224             if (reg_overlap_mentioned_p (ops[0], mem))
13225               {
13226                 gcc_assert (overlap == -1);
13227                 overlap = i;
13228               }
13229             else
13230               {
13231                 sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
13232                 output_asm_insn (buff, ops);
13233               }
13234           }
13235         if (overlap != -1)
13236           {
13237             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * overlap);
13238             ops[1] = adjust_address (mem, SImode, 8 * overlap);
13239             sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
13240             output_asm_insn (buff, ops);
13241           }
13242
13243         return "";
13244       }
13245
13246     default:
13247       templ = "v%smia%%?\t%%m0, %%h1";
13248       ops[0] = mem;
13249       ops[1] = reg;
13250     }
13251
13252   sprintf (buff, templ, load ? "ld" : "st");
13253   output_asm_insn (buff, ops);
13254
13255   return "";
13256 }
13257
13258 /* Compute and return the length of neon_mov<mode>, where <mode> is
13259    one of VSTRUCT modes: EI, OI, CI or XI.  */
13260 int
13261 arm_attr_length_move_neon (rtx insn)
13262 {
13263   rtx reg, mem, addr;
13264   int load;
13265   enum machine_mode mode;
13266
13267   extract_insn_cached (insn);
13268
13269   if (REG_P (recog_data.operand[0]) && REG_P (recog_data.operand[1]))
13270     {
13271       mode = GET_MODE (recog_data.operand[0]);
13272       switch (mode)
13273         {
13274         case EImode:
13275         case OImode:
13276           return 8;
13277         case CImode:
13278           return 12;
13279         case XImode:
13280           return 16;
13281         default:
13282           gcc_unreachable ();
13283         }
13284     }
13285
13286   load = REG_P (recog_data.operand[0]);
13287   reg = recog_data.operand[!load];
13288   mem = recog_data.operand[load];
13289
13290   gcc_assert (MEM_P (mem));
13291
13292   mode = GET_MODE (reg);
13293   addr = XEXP (mem, 0);
13294
13295   /* Strip off const from addresses like (const (plus (...))).  */
13296   if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
13297     addr = XEXP (addr, 0);
13298
13299   if (GET_CODE (addr) == LABEL_REF || GET_CODE (addr) == PLUS)
13300     {
13301       int insns = HARD_REGNO_NREGS (REGNO (reg), mode) / 2;
13302       return insns * 4;
13303     }
13304   else
13305     return 4;
13306 }
13307
13308 /* Output an ADD r, s, #n where n may be too big for one instruction.
13309    If adding zero to one register, output nothing.  */
13310 const char *
13311 output_add_immediate (rtx *operands)
13312 {
13313   HOST_WIDE_INT n = INTVAL (operands[2]);
13314
13315   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
13316     {
13317       if (n < 0)
13318         output_multi_immediate (operands,
13319                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
13320                                 -n);
13321       else
13322         output_multi_immediate (operands,
13323                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
13324                                 n);
13325     }
13326
13327   return "";
13328 }
13329
13330 /* Output a multiple immediate operation.
13331    OPERANDS is the vector of operands referred to in the output patterns.
13332    INSTR1 is the output pattern to use for the first constant.
13333    INSTR2 is the output pattern to use for subsequent constants.
13334    IMMED_OP is the index of the constant slot in OPERANDS.
13335    N is the constant value.  */
13336 static const char *
13337 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
13338                         int immed_op, HOST_WIDE_INT n)
13339 {
13340 #if HOST_BITS_PER_WIDE_INT > 32
13341   n &= 0xffffffff;
13342 #endif
13343
13344   if (n == 0)
13345     {
13346       /* Quick and easy output.  */
13347       operands[immed_op] = const0_rtx;
13348       output_asm_insn (instr1, operands);
13349     }
13350   else
13351     {
13352       int i;
13353       const char * instr = instr1;
13354
13355       /* Note that n is never zero here (which would give no output).  */
13356       for (i = 0; i < 32; i += 2)
13357         {
13358           if (n & (3 << i))
13359             {
13360               operands[immed_op] = GEN_INT (n & (255 << i));
13361               output_asm_insn (instr, operands);
13362               instr = instr2;
13363               i += 6;
13364             }
13365         }
13366     }
13367
13368   return "";
13369 }
13370
13371 /* Return the name of a shifter operation.  */
13372 static const char *
13373 arm_shift_nmem(enum rtx_code code)
13374 {
13375   switch (code)
13376     {
13377     case ASHIFT:
13378       return ARM_LSL_NAME;
13379
13380     case ASHIFTRT:
13381       return "asr";
13382
13383     case LSHIFTRT:
13384       return "lsr";
13385
13386     case ROTATERT:
13387       return "ror";
13388
13389     default:
13390       abort();
13391     }
13392 }
13393
13394 /* Return the appropriate ARM instruction for the operation code.
13395    The returned result should not be overwritten.  OP is the rtx of the
13396    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
13397    was shifted.  */
13398 const char *
13399 arithmetic_instr (rtx op, int shift_first_arg)
13400 {
13401   switch (GET_CODE (op))
13402     {
13403     case PLUS:
13404       return "add";
13405
13406     case MINUS:
13407       return shift_first_arg ? "rsb" : "sub";
13408
13409     case IOR:
13410       return "orr";
13411
13412     case XOR:
13413       return "eor";
13414
13415     case AND:
13416       return "and";
13417
13418     case ASHIFT:
13419     case ASHIFTRT:
13420     case LSHIFTRT:
13421     case ROTATERT:
13422       return arm_shift_nmem(GET_CODE(op));
13423
13424     default:
13425       gcc_unreachable ();
13426     }
13427 }
13428
13429 /* Ensure valid constant shifts and return the appropriate shift mnemonic
13430    for the operation code.  The returned result should not be overwritten.
13431    OP is the rtx code of the shift.
13432    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
13433    shift.  */
13434 static const char *
13435 shift_op (rtx op, HOST_WIDE_INT *amountp)
13436 {
13437   const char * mnem;
13438   enum rtx_code code = GET_CODE (op);
13439
13440   switch (GET_CODE (XEXP (op, 1)))
13441     {
13442     case REG:
13443     case SUBREG:
13444       *amountp = -1;
13445       break;
13446
13447     case CONST_INT:
13448       *amountp = INTVAL (XEXP (op, 1));
13449       break;
13450
13451     default:
13452       gcc_unreachable ();
13453     }
13454
13455   switch (code)
13456     {
13457     case ROTATE:
13458       gcc_assert (*amountp != -1);
13459       *amountp = 32 - *amountp;
13460       code = ROTATERT;
13461
13462       /* Fall through.  */
13463
13464     case ASHIFT:
13465     case ASHIFTRT:
13466     case LSHIFTRT:
13467     case ROTATERT:
13468       mnem = arm_shift_nmem(code);
13469       break;
13470
13471     case MULT:
13472       /* We never have to worry about the amount being other than a
13473          power of 2, since this case can never be reloaded from a reg.  */
13474       gcc_assert (*amountp != -1);
13475       *amountp = int_log2 (*amountp);
13476       return ARM_LSL_NAME;
13477
13478     default:
13479       gcc_unreachable ();
13480     }
13481
13482   if (*amountp != -1)
13483     {
13484       /* This is not 100% correct, but follows from the desire to merge
13485          multiplication by a power of 2 with the recognizer for a
13486          shift.  >=32 is not a valid shift for "lsl", so we must try and
13487          output a shift that produces the correct arithmetical result.
13488          Using lsr #32 is identical except for the fact that the carry bit
13489          is not set correctly if we set the flags; but we never use the
13490          carry bit from such an operation, so we can ignore that.  */
13491       if (code == ROTATERT)
13492         /* Rotate is just modulo 32.  */
13493         *amountp &= 31;
13494       else if (*amountp != (*amountp & 31))
13495         {
13496           if (code == ASHIFT)
13497             mnem = "lsr";
13498           *amountp = 32;
13499         }
13500
13501       /* Shifts of 0 are no-ops.  */
13502       if (*amountp == 0)
13503         return NULL;
13504     }
13505
13506   return mnem;
13507 }
13508
13509 /* Obtain the shift from the POWER of two.  */
13510
13511 static HOST_WIDE_INT
13512 int_log2 (HOST_WIDE_INT power)
13513 {
13514   HOST_WIDE_INT shift = 0;
13515
13516   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
13517     {
13518       gcc_assert (shift <= 31);
13519       shift++;
13520     }
13521
13522   return shift;
13523 }
13524
13525 /* Output a .ascii pseudo-op, keeping track of lengths.  This is
13526    because /bin/as is horribly restrictive.  The judgement about
13527    whether or not each character is 'printable' (and can be output as
13528    is) or not (and must be printed with an octal escape) must be made
13529    with reference to the *host* character set -- the situation is
13530    similar to that discussed in the comments above pp_c_char in
13531    c-pretty-print.c.  */
13532
13533 #define MAX_ASCII_LEN 51
13534
13535 void
13536 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
13537 {
13538   int i;
13539   int len_so_far = 0;
13540
13541   fputs ("\t.ascii\t\"", stream);
13542
13543   for (i = 0; i < len; i++)
13544     {
13545       int c = p[i];
13546
13547       if (len_so_far >= MAX_ASCII_LEN)
13548         {
13549           fputs ("\"\n\t.ascii\t\"", stream);
13550           len_so_far = 0;
13551         }
13552
13553       if (ISPRINT (c))
13554         {
13555           if (c == '\\' || c == '\"')
13556             {
13557               putc ('\\', stream);
13558               len_so_far++;
13559             }
13560           putc (c, stream);
13561           len_so_far++;
13562         }
13563       else
13564         {
13565           fprintf (stream, "\\%03o", c);
13566           len_so_far += 4;
13567         }
13568     }
13569
13570   fputs ("\"\n", stream);
13571 }
13572 \f
13573 /* Compute the register save mask for registers 0 through 12
13574    inclusive.  This code is used by arm_compute_save_reg_mask.  */
13575
13576 static unsigned long
13577 arm_compute_save_reg0_reg12_mask (void)
13578 {
13579   unsigned long func_type = arm_current_func_type ();
13580   unsigned long save_reg_mask = 0;
13581   unsigned int reg;
13582
13583   if (IS_INTERRUPT (func_type))
13584     {
13585       unsigned int max_reg;
13586       /* Interrupt functions must not corrupt any registers,
13587          even call clobbered ones.  If this is a leaf function
13588          we can just examine the registers used by the RTL, but
13589          otherwise we have to assume that whatever function is
13590          called might clobber anything, and so we have to save
13591          all the call-clobbered registers as well.  */
13592       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
13593         /* FIQ handlers have registers r8 - r12 banked, so
13594            we only need to check r0 - r7, Normal ISRs only
13595            bank r14 and r15, so we must check up to r12.
13596            r13 is the stack pointer which is always preserved,
13597            so we do not need to consider it here.  */
13598         max_reg = 7;
13599       else
13600         max_reg = 12;
13601
13602       for (reg = 0; reg <= max_reg; reg++)
13603         if (df_regs_ever_live_p (reg)
13604             || (! current_function_is_leaf && call_used_regs[reg]))
13605           save_reg_mask |= (1 << reg);
13606
13607       /* Also save the pic base register if necessary.  */
13608       if (flag_pic
13609           && !TARGET_SINGLE_PIC_BASE
13610           && arm_pic_register != INVALID_REGNUM
13611           && crtl->uses_pic_offset_table)
13612         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
13613     }
13614   else if (IS_VOLATILE(func_type))
13615     {
13616       /* For noreturn functions we historically omitted register saves
13617          altogether.  However this really messes up debugging.  As a
13618          compromise save just the frame pointers.  Combined with the link
13619          register saved elsewhere this should be sufficient to get
13620          a backtrace.  */
13621       if (frame_pointer_needed)
13622         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
13623       if (df_regs_ever_live_p (ARM_HARD_FRAME_POINTER_REGNUM))
13624         save_reg_mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
13625       if (df_regs_ever_live_p (THUMB_HARD_FRAME_POINTER_REGNUM))
13626         save_reg_mask |= 1 << THUMB_HARD_FRAME_POINTER_REGNUM;
13627     }
13628   else
13629     {
13630       /* In the normal case we only need to save those registers
13631          which are call saved and which are used by this function.  */
13632       for (reg = 0; reg <= 11; reg++)
13633         if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
13634           save_reg_mask |= (1 << reg);
13635
13636       /* Handle the frame pointer as a special case.  */
13637       if (frame_pointer_needed)
13638         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
13639
13640       /* If we aren't loading the PIC register,
13641          don't stack it even though it may be live.  */
13642       if (flag_pic
13643           && !TARGET_SINGLE_PIC_BASE
13644           && arm_pic_register != INVALID_REGNUM
13645           && (df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM)
13646               || crtl->uses_pic_offset_table))
13647         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
13648
13649       /* The prologue will copy SP into R0, so save it.  */
13650       if (IS_STACKALIGN (func_type))
13651         save_reg_mask |= 1;
13652     }
13653
13654   /* Save registers so the exception handler can modify them.  */
13655   if (crtl->calls_eh_return)
13656     {
13657       unsigned int i;
13658
13659       for (i = 0; ; i++)
13660         {
13661           reg = EH_RETURN_DATA_REGNO (i);
13662           if (reg == INVALID_REGNUM)
13663             break;
13664           save_reg_mask |= 1 << reg;
13665         }
13666     }
13667
13668   return save_reg_mask;
13669 }
13670
13671
13672 /* Compute the number of bytes used to store the static chain register on the 
13673    stack, above the stack frame. We need to know this accurately to get the
13674    alignment of the rest of the stack frame correct. */
13675
13676 static int arm_compute_static_chain_stack_bytes (void)
13677 {
13678   unsigned long func_type = arm_current_func_type ();
13679   int static_chain_stack_bytes = 0;
13680
13681   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM &&
13682       IS_NESTED (func_type) &&
13683       df_regs_ever_live_p (3) && crtl->args.pretend_args_size == 0)
13684     static_chain_stack_bytes = 4;
13685
13686   return static_chain_stack_bytes;
13687 }
13688
13689
13690 /* Compute a bit mask of which registers need to be
13691    saved on the stack for the current function.
13692    This is used by arm_get_frame_offsets, which may add extra registers.  */
13693
13694 static unsigned long
13695 arm_compute_save_reg_mask (void)
13696 {
13697   unsigned int save_reg_mask = 0;
13698   unsigned long func_type = arm_current_func_type ();
13699   unsigned int reg;
13700
13701   if (IS_NAKED (func_type))
13702     /* This should never really happen.  */
13703     return 0;
13704
13705   /* If we are creating a stack frame, then we must save the frame pointer,
13706      IP (which will hold the old stack pointer), LR and the PC.  */
13707   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
13708     save_reg_mask |=
13709       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
13710       | (1 << IP_REGNUM)
13711       | (1 << LR_REGNUM)
13712       | (1 << PC_REGNUM);
13713
13714   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
13715
13716   /* Decide if we need to save the link register.
13717      Interrupt routines have their own banked link register,
13718      so they never need to save it.
13719      Otherwise if we do not use the link register we do not need to save
13720      it.  If we are pushing other registers onto the stack however, we
13721      can save an instruction in the epilogue by pushing the link register
13722      now and then popping it back into the PC.  This incurs extra memory
13723      accesses though, so we only do it when optimizing for size, and only
13724      if we know that we will not need a fancy return sequence.  */
13725   if (df_regs_ever_live_p (LR_REGNUM)
13726       || (save_reg_mask
13727           && optimize_size
13728           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
13729           && !crtl->calls_eh_return))
13730     save_reg_mask |= 1 << LR_REGNUM;
13731
13732   if (cfun->machine->lr_save_eliminated)
13733     save_reg_mask &= ~ (1 << LR_REGNUM);
13734
13735   if (TARGET_REALLY_IWMMXT
13736       && ((bit_count (save_reg_mask)
13737            + ARM_NUM_INTS (crtl->args.pretend_args_size +
13738                            arm_compute_static_chain_stack_bytes())
13739            ) % 2) != 0)
13740     {
13741       /* The total number of registers that are going to be pushed
13742          onto the stack is odd.  We need to ensure that the stack
13743          is 64-bit aligned before we start to save iWMMXt registers,
13744          and also before we start to create locals.  (A local variable
13745          might be a double or long long which we will load/store using
13746          an iWMMXt instruction).  Therefore we need to push another
13747          ARM register, so that the stack will be 64-bit aligned.  We
13748          try to avoid using the arg registers (r0 -r3) as they might be
13749          used to pass values in a tail call.  */
13750       for (reg = 4; reg <= 12; reg++)
13751         if ((save_reg_mask & (1 << reg)) == 0)
13752           break;
13753
13754       if (reg <= 12)
13755         save_reg_mask |= (1 << reg);
13756       else
13757         {
13758           cfun->machine->sibcall_blocked = 1;
13759           save_reg_mask |= (1 << 3);
13760         }
13761     }
13762
13763   /* We may need to push an additional register for use initializing the
13764      PIC base register.  */
13765   if (TARGET_THUMB2 && IS_NESTED (func_type) && flag_pic
13766       && (save_reg_mask & THUMB2_WORK_REGS) == 0)
13767     {
13768       reg = thumb_find_work_register (1 << 4);
13769       if (!call_used_regs[reg])
13770         save_reg_mask |= (1 << reg);
13771     }
13772
13773   return save_reg_mask;
13774 }
13775
13776
13777 /* Compute a bit mask of which registers need to be
13778    saved on the stack for the current function.  */
13779 static unsigned long
13780 thumb1_compute_save_reg_mask (void)
13781 {
13782   unsigned long mask;
13783   unsigned reg;
13784
13785   mask = 0;
13786   for (reg = 0; reg < 12; reg ++)
13787     if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
13788       mask |= 1 << reg;
13789
13790   if (flag_pic
13791       && !TARGET_SINGLE_PIC_BASE
13792       && arm_pic_register != INVALID_REGNUM
13793       && crtl->uses_pic_offset_table)
13794     mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
13795
13796   /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
13797   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
13798     mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
13799
13800   /* LR will also be pushed if any lo regs are pushed.  */
13801   if (mask & 0xff || thumb_force_lr_save ())
13802     mask |= (1 << LR_REGNUM);
13803
13804   /* Make sure we have a low work register if we need one.
13805      We will need one if we are going to push a high register,
13806      but we are not currently intending to push a low register.  */
13807   if ((mask & 0xff) == 0
13808       && ((mask & 0x0f00) || TARGET_BACKTRACE))
13809     {
13810       /* Use thumb_find_work_register to choose which register
13811          we will use.  If the register is live then we will
13812          have to push it.  Use LAST_LO_REGNUM as our fallback
13813          choice for the register to select.  */
13814       reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
13815       /* Make sure the register returned by thumb_find_work_register is
13816          not part of the return value.  */
13817       if (reg * UNITS_PER_WORD <= (unsigned) arm_size_return_regs ())
13818         reg = LAST_LO_REGNUM;
13819
13820       if (! call_used_regs[reg])
13821         mask |= 1 << reg;
13822     }
13823
13824   /* The 504 below is 8 bytes less than 512 because there are two possible
13825      alignment words.  We can't tell here if they will be present or not so we
13826      have to play it safe and assume that they are. */
13827   if ((CALLER_INTERWORKING_SLOT_SIZE +
13828        ROUND_UP_WORD (get_frame_size ()) +
13829        crtl->outgoing_args_size) >= 504)
13830     {
13831       /* This is the same as the code in thumb1_expand_prologue() which
13832          determines which register to use for stack decrement. */
13833       for (reg = LAST_ARG_REGNUM + 1; reg <= LAST_LO_REGNUM; reg++)
13834         if (mask & (1 << reg))
13835           break;
13836
13837       if (reg > LAST_LO_REGNUM)
13838         {
13839           /* Make sure we have a register available for stack decrement. */
13840           mask |= 1 << LAST_LO_REGNUM;
13841         }
13842     }
13843
13844   return mask;
13845 }
13846
13847
13848 /* Return the number of bytes required to save VFP registers.  */
13849 static int
13850 arm_get_vfp_saved_size (void)
13851 {
13852   unsigned int regno;
13853   int count;
13854   int saved;
13855
13856   saved = 0;
13857   /* Space for saved VFP registers.  */
13858   if (TARGET_HARD_FLOAT && TARGET_VFP)
13859     {
13860       count = 0;
13861       for (regno = FIRST_VFP_REGNUM;
13862            regno < LAST_VFP_REGNUM;
13863            regno += 2)
13864         {
13865           if ((!df_regs_ever_live_p (regno) || call_used_regs[regno])
13866               && (!df_regs_ever_live_p (regno + 1) || call_used_regs[regno + 1]))
13867             {
13868               if (count > 0)
13869                 {
13870                   /* Workaround ARM10 VFPr1 bug.  */
13871                   if (count == 2 && !arm_arch6)
13872                     count++;
13873                   saved += count * 8;
13874                 }
13875               count = 0;
13876             }
13877           else
13878             count++;
13879         }
13880       if (count > 0)
13881         {
13882           if (count == 2 && !arm_arch6)
13883             count++;
13884           saved += count * 8;
13885         }
13886     }
13887   return saved;
13888 }
13889
13890
13891 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
13892    everything bar the final return instruction.  */
13893 const char *
13894 output_return_instruction (rtx operand, int really_return, int reverse)
13895 {
13896   char conditional[10];
13897   char instr[100];
13898   unsigned reg;
13899   unsigned long live_regs_mask;
13900   unsigned long func_type;
13901   arm_stack_offsets *offsets;
13902
13903   func_type = arm_current_func_type ();
13904
13905   if (IS_NAKED (func_type))
13906     return "";
13907
13908   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
13909     {
13910       /* If this function was declared non-returning, and we have
13911          found a tail call, then we have to trust that the called
13912          function won't return.  */
13913       if (really_return)
13914         {
13915           rtx ops[2];
13916
13917           /* Otherwise, trap an attempted return by aborting.  */
13918           ops[0] = operand;
13919           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
13920                                        : "abort");
13921           assemble_external_libcall (ops[1]);
13922           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
13923         }
13924
13925       return "";
13926     }
13927
13928   gcc_assert (!cfun->calls_alloca || really_return);
13929
13930   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
13931
13932   cfun->machine->return_used_this_function = 1;
13933
13934   offsets = arm_get_frame_offsets ();
13935   live_regs_mask = offsets->saved_regs_mask;
13936
13937   if (live_regs_mask)
13938     {
13939       const char * return_reg;
13940
13941       /* If we do not have any special requirements for function exit
13942          (e.g. interworking) then we can load the return address
13943          directly into the PC.  Otherwise we must load it into LR.  */
13944       if (really_return
13945           && (IS_INTERRUPT (func_type) || !TARGET_INTERWORK))
13946         return_reg = reg_names[PC_REGNUM];
13947       else
13948         return_reg = reg_names[LR_REGNUM];
13949
13950       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
13951         {
13952           /* There are three possible reasons for the IP register
13953              being saved.  1) a stack frame was created, in which case
13954              IP contains the old stack pointer, or 2) an ISR routine
13955              corrupted it, or 3) it was saved to align the stack on
13956              iWMMXt.  In case 1, restore IP into SP, otherwise just
13957              restore IP.  */
13958           if (frame_pointer_needed)
13959             {
13960               live_regs_mask &= ~ (1 << IP_REGNUM);
13961               live_regs_mask |=   (1 << SP_REGNUM);
13962             }
13963           else
13964             gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
13965         }
13966
13967       /* On some ARM architectures it is faster to use LDR rather than
13968          LDM to load a single register.  On other architectures, the
13969          cost is the same.  In 26 bit mode, or for exception handlers,
13970          we have to use LDM to load the PC so that the CPSR is also
13971          restored.  */
13972       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
13973         if (live_regs_mask == (1U << reg))
13974           break;
13975
13976       if (reg <= LAST_ARM_REGNUM
13977           && (reg != LR_REGNUM
13978               || ! really_return
13979               || ! IS_INTERRUPT (func_type)))
13980         {
13981           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
13982                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
13983         }
13984       else
13985         {
13986           char *p;
13987           int first = 1;
13988
13989           /* Generate the load multiple instruction to restore the
13990              registers.  Note we can get here, even if
13991              frame_pointer_needed is true, but only if sp already
13992              points to the base of the saved core registers.  */
13993           if (live_regs_mask & (1 << SP_REGNUM))
13994             {
13995               unsigned HOST_WIDE_INT stack_adjust;
13996
13997               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
13998               gcc_assert (stack_adjust == 0 || stack_adjust == 4);
13999
14000               if (stack_adjust && arm_arch5 && TARGET_ARM)
14001                 if (TARGET_UNIFIED_ASM)
14002                   sprintf (instr, "ldmib%s\t%%|sp, {", conditional);
14003                 else
14004                   sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
14005               else
14006                 {
14007                   /* If we can't use ldmib (SA110 bug),
14008                      then try to pop r3 instead.  */
14009                   if (stack_adjust)
14010                     live_regs_mask |= 1 << 3;
14011                   
14012                   if (TARGET_UNIFIED_ASM)
14013                     sprintf (instr, "ldmfd%s\t%%|sp, {", conditional);
14014                   else
14015                     sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
14016                 }
14017             }
14018           else
14019             if (TARGET_UNIFIED_ASM)
14020               sprintf (instr, "pop%s\t{", conditional);
14021             else
14022               sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
14023
14024           p = instr + strlen (instr);
14025
14026           for (reg = 0; reg <= SP_REGNUM; reg++)
14027             if (live_regs_mask & (1 << reg))
14028               {
14029                 int l = strlen (reg_names[reg]);
14030
14031                 if (first)
14032                   first = 0;
14033                 else
14034                   {
14035                     memcpy (p, ", ", 2);
14036                     p += 2;
14037                   }
14038
14039                 memcpy (p, "%|", 2);
14040                 memcpy (p + 2, reg_names[reg], l);
14041                 p += l + 2;
14042               }
14043
14044           if (live_regs_mask & (1 << LR_REGNUM))
14045             {
14046               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
14047               /* If returning from an interrupt, restore the CPSR.  */
14048               if (IS_INTERRUPT (func_type))
14049                 strcat (p, "^");
14050             }
14051           else
14052             strcpy (p, "}");
14053         }
14054
14055       output_asm_insn (instr, & operand);
14056
14057       /* See if we need to generate an extra instruction to
14058          perform the actual function return.  */
14059       if (really_return
14060           && func_type != ARM_FT_INTERWORKED
14061           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
14062         {
14063           /* The return has already been handled
14064              by loading the LR into the PC.  */
14065           really_return = 0;
14066         }
14067     }
14068
14069   if (really_return)
14070     {
14071       switch ((int) ARM_FUNC_TYPE (func_type))
14072         {
14073         case ARM_FT_ISR:
14074         case ARM_FT_FIQ:
14075           /* ??? This is wrong for unified assembly syntax.  */
14076           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
14077           break;
14078
14079         case ARM_FT_INTERWORKED:
14080           sprintf (instr, "bx%s\t%%|lr", conditional);
14081           break;
14082
14083         case ARM_FT_EXCEPTION:
14084           /* ??? This is wrong for unified assembly syntax.  */
14085           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
14086           break;
14087
14088         default:
14089           /* Use bx if it's available.  */
14090           if (arm_arch5 || arm_arch4t)
14091             sprintf (instr, "bx%s\t%%|lr", conditional);
14092           else
14093             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
14094           break;
14095         }
14096
14097       output_asm_insn (instr, & operand);
14098     }
14099
14100   return "";
14101 }
14102
14103 /* Write the function name into the code section, directly preceding
14104    the function prologue.
14105
14106    Code will be output similar to this:
14107      t0
14108          .ascii "arm_poke_function_name", 0
14109          .align
14110      t1
14111          .word 0xff000000 + (t1 - t0)
14112      arm_poke_function_name
14113          mov     ip, sp
14114          stmfd   sp!, {fp, ip, lr, pc}
14115          sub     fp, ip, #4
14116
14117    When performing a stack backtrace, code can inspect the value
14118    of 'pc' stored at 'fp' + 0.  If the trace function then looks
14119    at location pc - 12 and the top 8 bits are set, then we know
14120    that there is a function name embedded immediately preceding this
14121    location and has length ((pc[-3]) & 0xff000000).
14122
14123    We assume that pc is declared as a pointer to an unsigned long.
14124
14125    It is of no benefit to output the function name if we are assembling
14126    a leaf function.  These function types will not contain a stack
14127    backtrace structure, therefore it is not possible to determine the
14128    function name.  */
14129 void
14130 arm_poke_function_name (FILE *stream, const char *name)
14131 {
14132   unsigned long alignlength;
14133   unsigned long length;
14134   rtx           x;
14135
14136   length      = strlen (name) + 1;
14137   alignlength = ROUND_UP_WORD (length);
14138
14139   ASM_OUTPUT_ASCII (stream, name, length);
14140   ASM_OUTPUT_ALIGN (stream, 2);
14141   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
14142   assemble_aligned_integer (UNITS_PER_WORD, x);
14143 }
14144
14145 /* Place some comments into the assembler stream
14146    describing the current function.  */
14147 static void
14148 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
14149 {
14150   unsigned long func_type;
14151
14152   if (TARGET_THUMB1)
14153     {
14154       thumb1_output_function_prologue (f, frame_size);
14155       return;
14156     }
14157
14158   /* Sanity check.  */
14159   gcc_assert (!arm_ccfsm_state && !arm_target_insn);
14160
14161   func_type = arm_current_func_type ();
14162
14163   switch ((int) ARM_FUNC_TYPE (func_type))
14164     {
14165     default:
14166     case ARM_FT_NORMAL:
14167       break;
14168     case ARM_FT_INTERWORKED:
14169       asm_fprintf (f, "\t%@ Function supports interworking.\n");
14170       break;
14171     case ARM_FT_ISR:
14172       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
14173       break;
14174     case ARM_FT_FIQ:
14175       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
14176       break;
14177     case ARM_FT_EXCEPTION:
14178       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
14179       break;
14180     }
14181
14182   if (IS_NAKED (func_type))
14183     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
14184
14185   if (IS_VOLATILE (func_type))
14186     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
14187
14188   if (IS_NESTED (func_type))
14189     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
14190   if (IS_STACKALIGN (func_type))
14191     asm_fprintf (f, "\t%@ Stack Align: May be called with mis-aligned SP.\n");
14192
14193   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
14194                crtl->args.size,
14195                crtl->args.pretend_args_size, frame_size);
14196
14197   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
14198                frame_pointer_needed,
14199                cfun->machine->uses_anonymous_args);
14200
14201   if (cfun->machine->lr_save_eliminated)
14202     asm_fprintf (f, "\t%@ link register save eliminated.\n");
14203
14204   if (crtl->calls_eh_return)
14205     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
14206
14207 }
14208
14209 const char *
14210 arm_output_epilogue (rtx sibling)
14211 {
14212   int reg;
14213   unsigned long saved_regs_mask;
14214   unsigned long func_type;
14215   /* Floats_offset is the offset from the "virtual" frame.  In an APCS
14216      frame that is $fp + 4 for a non-variadic function.  */
14217   int floats_offset = 0;
14218   rtx operands[3];
14219   FILE * f = asm_out_file;
14220   unsigned int lrm_count = 0;
14221   int really_return = (sibling == NULL);
14222   int start_reg;
14223   arm_stack_offsets *offsets;
14224
14225   /* If we have already generated the return instruction
14226      then it is futile to generate anything else.  */
14227   if (use_return_insn (FALSE, sibling) && 
14228       (cfun->machine->return_used_this_function != 0))
14229     return "";
14230
14231   func_type = arm_current_func_type ();
14232
14233   if (IS_NAKED (func_type))
14234     /* Naked functions don't have epilogues.  */
14235     return "";
14236
14237   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
14238     {
14239       rtx op;
14240
14241       /* A volatile function should never return.  Call abort.  */
14242       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
14243       assemble_external_libcall (op);
14244       output_asm_insn ("bl\t%a0", &op);
14245
14246       return "";
14247     }
14248
14249   /* If we are throwing an exception, then we really must be doing a
14250      return, so we can't tail-call.  */
14251   gcc_assert (!crtl->calls_eh_return || really_return);
14252
14253   offsets = arm_get_frame_offsets ();
14254   saved_regs_mask = offsets->saved_regs_mask;
14255
14256   if (TARGET_IWMMXT)
14257     lrm_count = bit_count (saved_regs_mask);
14258
14259   floats_offset = offsets->saved_args;
14260   /* Compute how far away the floats will be.  */
14261   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
14262     if (saved_regs_mask & (1 << reg))
14263       floats_offset += 4;
14264
14265   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
14266     {
14267       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
14268       int vfp_offset = offsets->frame;
14269
14270       if (TARGET_FPA_EMU2)
14271         {
14272           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
14273             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14274               {
14275                 floats_offset += 12;
14276                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
14277                              reg, FP_REGNUM, floats_offset - vfp_offset);
14278               }
14279         }
14280       else
14281         {
14282           start_reg = LAST_FPA_REGNUM;
14283
14284           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
14285             {
14286               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14287                 {
14288                   floats_offset += 12;
14289
14290                   /* We can't unstack more than four registers at once.  */
14291                   if (start_reg - reg == 3)
14292                     {
14293                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
14294                                    reg, FP_REGNUM, floats_offset - vfp_offset);
14295                       start_reg = reg - 1;
14296                     }
14297                 }
14298               else
14299                 {
14300                   if (reg != start_reg)
14301                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
14302                                  reg + 1, start_reg - reg,
14303                                  FP_REGNUM, floats_offset - vfp_offset);
14304                   start_reg = reg - 1;
14305                 }
14306             }
14307
14308           /* Just in case the last register checked also needs unstacking.  */
14309           if (reg != start_reg)
14310             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
14311                          reg + 1, start_reg - reg,
14312                          FP_REGNUM, floats_offset - vfp_offset);
14313         }
14314
14315       if (TARGET_HARD_FLOAT && TARGET_VFP)
14316         {
14317           int saved_size;
14318
14319           /* The fldmd insns do not have base+offset addressing
14320              modes, so we use IP to hold the address.  */
14321           saved_size = arm_get_vfp_saved_size ();
14322
14323           if (saved_size > 0)
14324             {
14325               floats_offset += saved_size;
14326               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
14327                            FP_REGNUM, floats_offset - vfp_offset);
14328             }
14329           start_reg = FIRST_VFP_REGNUM;
14330           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
14331             {
14332               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
14333                   && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
14334                 {
14335                   if (start_reg != reg)
14336                     vfp_output_fldmd (f, IP_REGNUM,
14337                                       (start_reg - FIRST_VFP_REGNUM) / 2,
14338                                       (reg - start_reg) / 2);
14339                   start_reg = reg + 2;
14340                 }
14341             }
14342           if (start_reg != reg)
14343             vfp_output_fldmd (f, IP_REGNUM,
14344                               (start_reg - FIRST_VFP_REGNUM) / 2,
14345                               (reg - start_reg) / 2);
14346         }
14347
14348       if (TARGET_IWMMXT)
14349         {
14350           /* The frame pointer is guaranteed to be non-double-word aligned.
14351              This is because it is set to (old_stack_pointer - 4) and the
14352              old_stack_pointer was double word aligned.  Thus the offset to
14353              the iWMMXt registers to be loaded must also be non-double-word
14354              sized, so that the resultant address *is* double-word aligned.
14355              We can ignore floats_offset since that was already included in
14356              the live_regs_mask.  */
14357           lrm_count += (lrm_count % 2 ? 2 : 1);
14358
14359           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
14360             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14361               {
14362                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
14363                              reg, FP_REGNUM, lrm_count * 4);
14364                 lrm_count += 2;
14365               }
14366         }
14367
14368       /* saved_regs_mask should contain the IP, which at the time of stack
14369          frame generation actually contains the old stack pointer.  So a
14370          quick way to unwind the stack is just pop the IP register directly
14371          into the stack pointer.  */
14372       gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
14373       saved_regs_mask &= ~ (1 << IP_REGNUM);
14374       saved_regs_mask |=   (1 << SP_REGNUM);
14375
14376       /* There are two registers left in saved_regs_mask - LR and PC.  We
14377          only need to restore the LR register (the return address), but to
14378          save time we can load it directly into the PC, unless we need a
14379          special function exit sequence, or we are not really returning.  */
14380       if (really_return
14381           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
14382           && !crtl->calls_eh_return)
14383         /* Delete the LR from the register mask, so that the LR on
14384            the stack is loaded into the PC in the register mask.  */
14385         saved_regs_mask &= ~ (1 << LR_REGNUM);
14386       else
14387         saved_regs_mask &= ~ (1 << PC_REGNUM);
14388
14389       /* We must use SP as the base register, because SP is one of the
14390          registers being restored.  If an interrupt or page fault
14391          happens in the ldm instruction, the SP might or might not
14392          have been restored.  That would be bad, as then SP will no
14393          longer indicate the safe area of stack, and we can get stack
14394          corruption.  Using SP as the base register means that it will
14395          be reset correctly to the original value, should an interrupt
14396          occur.  If the stack pointer already points at the right
14397          place, then omit the subtraction.  */
14398       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
14399           || cfun->calls_alloca)
14400         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
14401                      4 * bit_count (saved_regs_mask));
14402       print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask, 0);
14403
14404       if (IS_INTERRUPT (func_type))
14405         /* Interrupt handlers will have pushed the
14406            IP onto the stack, so restore it now.  */
14407         print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, 1 << IP_REGNUM, 0);
14408     }
14409   else
14410     {
14411       /* This branch is executed for ARM mode (non-apcs frames) and
14412          Thumb-2 mode. Frame layout is essentially the same for those
14413          cases, except that in ARM mode frame pointer points to the
14414          first saved register, while in Thumb-2 mode the frame pointer points
14415          to the last saved register.
14416
14417          It is possible to make frame pointer point to last saved
14418          register in both cases, and remove some conditionals below.
14419          That means that fp setup in prologue would be just "mov fp, sp"
14420          and sp restore in epilogue would be just "mov sp, fp", whereas
14421          now we have to use add/sub in those cases. However, the value
14422          of that would be marginal, as both mov and add/sub are 32-bit
14423          in ARM mode, and it would require extra conditionals
14424          in arm_expand_prologue to distingish ARM-apcs-frame case
14425          (where frame pointer is required to point at first register)
14426          and ARM-non-apcs-frame. Therefore, such change is postponed
14427          until real need arise.  */
14428       unsigned HOST_WIDE_INT amount;
14429       int rfe;
14430       /* Restore stack pointer if necessary.  */
14431       if (TARGET_ARM && frame_pointer_needed)
14432         {
14433           operands[0] = stack_pointer_rtx;
14434           operands[1] = hard_frame_pointer_rtx;
14435           
14436           operands[2] = GEN_INT (offsets->frame - offsets->saved_regs);
14437           output_add_immediate (operands);
14438         }
14439       else
14440         {
14441           if (frame_pointer_needed)
14442             {
14443               /* For Thumb-2 restore sp from the frame pointer.
14444                  Operand restrictions mean we have to incrememnt FP, then copy
14445                  to SP.  */
14446               amount = offsets->locals_base - offsets->saved_regs;
14447               operands[0] = hard_frame_pointer_rtx;
14448             }
14449           else
14450             {
14451               unsigned long count;
14452               operands[0] = stack_pointer_rtx;
14453               amount = offsets->outgoing_args - offsets->saved_regs;
14454               /* pop call clobbered registers if it avoids a
14455                  separate stack adjustment.  */
14456               count = offsets->saved_regs - offsets->saved_args;
14457               if (optimize_size
14458                   && count != 0
14459                   && !crtl->calls_eh_return
14460                   && bit_count(saved_regs_mask) * 4 == count
14461                   && !IS_INTERRUPT (func_type)
14462                   && !crtl->tail_call_emit)
14463                 {
14464                   unsigned long mask;
14465                   mask = (1 << (arm_size_return_regs() / 4)) - 1;
14466                   mask ^= 0xf;
14467                   mask &= ~saved_regs_mask;
14468                   reg = 0;
14469                   while (bit_count (mask) * 4 > amount)
14470                     {
14471                       while ((mask & (1 << reg)) == 0)
14472                         reg++;
14473                       mask &= ~(1 << reg);
14474                     }
14475                   if (bit_count (mask) * 4 == amount) {
14476                       amount = 0;
14477                       saved_regs_mask |= mask;
14478                   }
14479                 }
14480             }
14481           
14482           if (amount)
14483             {
14484               operands[1] = operands[0];
14485               operands[2] = GEN_INT (amount);
14486               output_add_immediate (operands);
14487             }
14488           if (frame_pointer_needed)
14489             asm_fprintf (f, "\tmov\t%r, %r\n",
14490                          SP_REGNUM, HARD_FRAME_POINTER_REGNUM);
14491         }
14492
14493       if (TARGET_FPA_EMU2)
14494         {
14495           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
14496             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14497               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
14498                            reg, SP_REGNUM);
14499         }
14500       else
14501         {
14502           start_reg = FIRST_FPA_REGNUM;
14503
14504           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
14505             {
14506               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14507                 {
14508                   if (reg - start_reg == 3)
14509                     {
14510                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
14511                                    start_reg, SP_REGNUM);
14512                       start_reg = reg + 1;
14513                     }
14514                 }
14515               else
14516                 {
14517                   if (reg != start_reg)
14518                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
14519                                  start_reg, reg - start_reg,
14520                                  SP_REGNUM);
14521
14522                   start_reg = reg + 1;
14523                 }
14524             }
14525
14526           /* Just in case the last register checked also needs unstacking.  */
14527           if (reg != start_reg)
14528             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
14529                          start_reg, reg - start_reg, SP_REGNUM);
14530         }
14531
14532       if (TARGET_HARD_FLOAT && TARGET_VFP)
14533         {
14534           int end_reg = LAST_VFP_REGNUM + 1;
14535
14536           /* Scan the registers in reverse order.  We need to match
14537              any groupings made in the prologue and generate matching
14538              pop operations.  */
14539           for (reg = LAST_VFP_REGNUM - 1; reg >= FIRST_VFP_REGNUM; reg -= 2)
14540             {
14541               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
14542                   && (!df_regs_ever_live_p (reg + 1)
14543                       || call_used_regs[reg + 1]))
14544                 {
14545                   if (end_reg > reg + 2)
14546                     vfp_output_fldmd (f, SP_REGNUM,
14547                                       (reg + 2 - FIRST_VFP_REGNUM) / 2,
14548                                       (end_reg - (reg + 2)) / 2);
14549                   end_reg = reg;
14550                 }
14551             }
14552           if (end_reg > reg + 2)
14553             vfp_output_fldmd (f, SP_REGNUM, 0,
14554                               (end_reg - (reg + 2)) / 2);
14555         }
14556
14557       if (TARGET_IWMMXT)
14558         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
14559           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14560             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
14561
14562       /* If we can, restore the LR into the PC.  */
14563       if (ARM_FUNC_TYPE (func_type) != ARM_FT_INTERWORKED
14564           && (TARGET_ARM || ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
14565           && !IS_STACKALIGN (func_type)
14566           && really_return
14567           && crtl->args.pretend_args_size == 0
14568           && saved_regs_mask & (1 << LR_REGNUM)
14569           && !crtl->calls_eh_return)
14570         {
14571           saved_regs_mask &= ~ (1 << LR_REGNUM);
14572           saved_regs_mask |=   (1 << PC_REGNUM);
14573           rfe = IS_INTERRUPT (func_type);
14574         }
14575       else
14576         rfe = 0;
14577
14578       /* Load the registers off the stack.  If we only have one register
14579          to load use the LDR instruction - it is faster.  For Thumb-2
14580          always use pop and the assembler will pick the best instruction.*/
14581       if (TARGET_ARM && saved_regs_mask == (1 << LR_REGNUM)
14582           && !IS_INTERRUPT(func_type))
14583         {
14584           asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
14585         }
14586       else if (saved_regs_mask)
14587         {
14588           if (saved_regs_mask & (1 << SP_REGNUM))
14589             /* Note - write back to the stack register is not enabled
14590                (i.e. "ldmfd sp!...").  We know that the stack pointer is
14591                in the list of registers and if we add writeback the
14592                instruction becomes UNPREDICTABLE.  */
14593             print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask,
14594                              rfe);
14595           else if (TARGET_ARM)
14596             print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, saved_regs_mask,
14597                              rfe);
14598           else
14599             print_multi_reg (f, "pop\t", SP_REGNUM, saved_regs_mask, 0);
14600         }
14601
14602       if (crtl->args.pretend_args_size)
14603         {
14604           /* Unwind the pre-pushed regs.  */
14605           operands[0] = operands[1] = stack_pointer_rtx;
14606           operands[2] = GEN_INT (crtl->args.pretend_args_size);
14607           output_add_immediate (operands);
14608         }
14609     }
14610
14611   /* We may have already restored PC directly from the stack.  */
14612   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
14613     return "";
14614
14615   /* Stack adjustment for exception handler.  */
14616   if (crtl->calls_eh_return)
14617     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
14618                  ARM_EH_STACKADJ_REGNUM);
14619
14620   /* Generate the return instruction.  */
14621   switch ((int) ARM_FUNC_TYPE (func_type))
14622     {
14623     case ARM_FT_ISR:
14624     case ARM_FT_FIQ:
14625       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
14626       break;
14627
14628     case ARM_FT_EXCEPTION:
14629       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
14630       break;
14631
14632     case ARM_FT_INTERWORKED:
14633       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
14634       break;
14635
14636     default:
14637       if (IS_STACKALIGN (func_type))
14638         {
14639           /* See comment in arm_expand_prologue.  */
14640           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, 0);
14641         }
14642       if (arm_arch5 || arm_arch4t)
14643         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
14644       else
14645         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
14646       break;
14647     }
14648
14649   return "";
14650 }
14651
14652 static void
14653 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
14654                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
14655 {
14656   arm_stack_offsets *offsets;
14657
14658   if (TARGET_THUMB1)
14659     {
14660       int regno;
14661
14662       /* Emit any call-via-reg trampolines that are needed for v4t support
14663          of call_reg and call_value_reg type insns.  */
14664       for (regno = 0; regno < LR_REGNUM; regno++)
14665         {
14666           rtx label = cfun->machine->call_via[regno];
14667
14668           if (label != NULL)
14669             {
14670               switch_to_section (function_section (current_function_decl));
14671               targetm.asm_out.internal_label (asm_out_file, "L",
14672                                               CODE_LABEL_NUMBER (label));
14673               asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
14674             }
14675         }
14676
14677       /* ??? Probably not safe to set this here, since it assumes that a
14678          function will be emitted as assembly immediately after we generate
14679          RTL for it.  This does not happen for inline functions.  */
14680       cfun->machine->return_used_this_function = 0;
14681     }
14682   else /* TARGET_32BIT */
14683     {
14684       /* We need to take into account any stack-frame rounding.  */
14685       offsets = arm_get_frame_offsets ();
14686
14687       gcc_assert (!use_return_insn (FALSE, NULL)
14688                   || (cfun->machine->return_used_this_function != 0)
14689                   || offsets->saved_regs == offsets->outgoing_args
14690                   || frame_pointer_needed);
14691
14692       /* Reset the ARM-specific per-function variables.  */
14693       after_arm_reorg = 0;
14694     }
14695 }
14696
14697 /* Generate and emit an insn that we will recognize as a push_multi.
14698    Unfortunately, since this insn does not reflect very well the actual
14699    semantics of the operation, we need to annotate the insn for the benefit
14700    of DWARF2 frame unwind information.  */
14701 static rtx
14702 emit_multi_reg_push (unsigned long mask)
14703 {
14704   int num_regs = 0;
14705   int num_dwarf_regs;
14706   int i, j;
14707   rtx par;
14708   rtx dwarf;
14709   int dwarf_par_index;
14710   rtx tmp, reg;
14711
14712   for (i = 0; i <= LAST_ARM_REGNUM; i++)
14713     if (mask & (1 << i))
14714       num_regs++;
14715
14716   gcc_assert (num_regs && num_regs <= 16);
14717
14718   /* We don't record the PC in the dwarf frame information.  */
14719   num_dwarf_regs = num_regs;
14720   if (mask & (1 << PC_REGNUM))
14721     num_dwarf_regs--;
14722
14723   /* For the body of the insn we are going to generate an UNSPEC in
14724      parallel with several USEs.  This allows the insn to be recognized
14725      by the push_multi pattern in the arm.md file.
14726
14727      The body of the insn looks something like this:
14728
14729        (parallel [
14730            (set (mem:BLK (pre_modify:SI (reg:SI sp)
14731                                         (const_int:SI <num>)))
14732                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
14733            (use (reg:SI XX))
14734            (use (reg:SI YY))
14735            ...
14736         ])
14737
14738      For the frame note however, we try to be more explicit and actually
14739      show each register being stored into the stack frame, plus a (single)
14740      decrement of the stack pointer.  We do it this way in order to be
14741      friendly to the stack unwinding code, which only wants to see a single
14742      stack decrement per instruction.  The RTL we generate for the note looks
14743      something like this:
14744
14745       (sequence [
14746            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
14747            (set (mem:SI (reg:SI sp)) (reg:SI r4))
14748            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI XX))
14749            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI YY))
14750            ...
14751         ])
14752
14753      FIXME:: In an ideal world the PRE_MODIFY would not exist and
14754      instead we'd have a parallel expression detailing all
14755      the stores to the various memory addresses so that debug
14756      information is more up-to-date. Remember however while writing
14757      this to take care of the constraints with the push instruction.
14758
14759      Note also that this has to be taken care of for the VFP registers.
14760
14761      For more see PR43399.  */
14762
14763   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
14764   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
14765   dwarf_par_index = 1;
14766
14767   for (i = 0; i <= LAST_ARM_REGNUM; i++)
14768     {
14769       if (mask & (1 << i))
14770         {
14771           reg = gen_rtx_REG (SImode, i);
14772
14773           XVECEXP (par, 0, 0)
14774             = gen_rtx_SET (VOIDmode,
14775                            gen_frame_mem
14776                            (BLKmode,
14777                             gen_rtx_PRE_MODIFY (Pmode,
14778                                                 stack_pointer_rtx,
14779                                                 plus_constant
14780                                                 (stack_pointer_rtx,
14781                                                  -4 * num_regs))
14782                             ),
14783                            gen_rtx_UNSPEC (BLKmode,
14784                                            gen_rtvec (1, reg),
14785                                            UNSPEC_PUSH_MULT));
14786
14787           if (i != PC_REGNUM)
14788             {
14789               tmp = gen_rtx_SET (VOIDmode,
14790                                  gen_frame_mem (SImode, stack_pointer_rtx),
14791                                  reg);
14792               RTX_FRAME_RELATED_P (tmp) = 1;
14793               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
14794               dwarf_par_index++;
14795             }
14796
14797           break;
14798         }
14799     }
14800
14801   for (j = 1, i++; j < num_regs; i++)
14802     {
14803       if (mask & (1 << i))
14804         {
14805           reg = gen_rtx_REG (SImode, i);
14806
14807           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
14808
14809           if (i != PC_REGNUM)
14810             {
14811               tmp
14812                 = gen_rtx_SET (VOIDmode,
14813                                gen_frame_mem
14814                                (SImode,
14815                                 plus_constant (stack_pointer_rtx,
14816                                                4 * j)),
14817                                reg);
14818               RTX_FRAME_RELATED_P (tmp) = 1;
14819               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
14820             }
14821
14822           j++;
14823         }
14824     }
14825
14826   par = emit_insn (par);
14827
14828   tmp = gen_rtx_SET (VOIDmode,
14829                      stack_pointer_rtx,
14830                      plus_constant (stack_pointer_rtx, -4 * num_regs));
14831   RTX_FRAME_RELATED_P (tmp) = 1;
14832   XVECEXP (dwarf, 0, 0) = tmp;
14833
14834   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
14835
14836   return par;
14837 }
14838
14839 /* Calculate the size of the return value that is passed in registers.  */
14840 static unsigned
14841 arm_size_return_regs (void)
14842 {
14843   enum machine_mode mode;
14844
14845   if (crtl->return_rtx != 0)
14846     mode = GET_MODE (crtl->return_rtx);
14847   else
14848     mode = DECL_MODE (DECL_RESULT (current_function_decl));
14849
14850   return GET_MODE_SIZE (mode);
14851 }
14852
14853 static rtx
14854 emit_sfm (int base_reg, int count)
14855 {
14856   rtx par;
14857   rtx dwarf;
14858   rtx tmp, reg;
14859   int i;
14860
14861   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
14862   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
14863
14864   reg = gen_rtx_REG (XFmode, base_reg++);
14865
14866   XVECEXP (par, 0, 0)
14867     = gen_rtx_SET (VOIDmode,
14868                    gen_frame_mem
14869                    (BLKmode,
14870                     gen_rtx_PRE_MODIFY (Pmode,
14871                                         stack_pointer_rtx,
14872                                         plus_constant
14873                                         (stack_pointer_rtx,
14874                                          -12 * count))
14875                     ),
14876                    gen_rtx_UNSPEC (BLKmode,
14877                                    gen_rtvec (1, reg),
14878                                    UNSPEC_PUSH_MULT));
14879   tmp = gen_rtx_SET (VOIDmode,
14880                      gen_frame_mem (XFmode, stack_pointer_rtx), reg);
14881   RTX_FRAME_RELATED_P (tmp) = 1;
14882   XVECEXP (dwarf, 0, 1) = tmp;
14883
14884   for (i = 1; i < count; i++)
14885     {
14886       reg = gen_rtx_REG (XFmode, base_reg++);
14887       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
14888
14889       tmp = gen_rtx_SET (VOIDmode,
14890                          gen_frame_mem (XFmode,
14891                                         plus_constant (stack_pointer_rtx,
14892                                                        i * 12)),
14893                          reg);
14894       RTX_FRAME_RELATED_P (tmp) = 1;
14895       XVECEXP (dwarf, 0, i + 1) = tmp;
14896     }
14897
14898   tmp = gen_rtx_SET (VOIDmode,
14899                      stack_pointer_rtx,
14900                      plus_constant (stack_pointer_rtx, -12 * count));
14901
14902   RTX_FRAME_RELATED_P (tmp) = 1;
14903   XVECEXP (dwarf, 0, 0) = tmp;
14904
14905   par = emit_insn (par);
14906   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
14907
14908   return par;
14909 }
14910
14911
14912 /* Return true if the current function needs to save/restore LR.  */
14913
14914 static bool
14915 thumb_force_lr_save (void)
14916 {
14917   return !cfun->machine->lr_save_eliminated
14918          && (!leaf_function_p ()
14919              || thumb_far_jump_used_p ()
14920              || df_regs_ever_live_p (LR_REGNUM));
14921 }
14922
14923
14924 /* Compute the distance from register FROM to register TO.
14925    These can be the arg pointer (26), the soft frame pointer (25),
14926    the stack pointer (13) or the hard frame pointer (11).
14927    In thumb mode r7 is used as the soft frame pointer, if needed.
14928    Typical stack layout looks like this:
14929
14930        old stack pointer -> |    |
14931                              ----
14932                             |    | \
14933                             |    |   saved arguments for
14934                             |    |   vararg functions
14935                             |    | /
14936                               --
14937    hard FP & arg pointer -> |    | \
14938                             |    |   stack
14939                             |    |   frame
14940                             |    | /
14941                               --
14942                             |    | \
14943                             |    |   call saved
14944                             |    |   registers
14945       soft frame pointer -> |    | /
14946                               --
14947                             |    | \
14948                             |    |   local
14949                             |    |   variables
14950      locals base pointer -> |    | /
14951                               --
14952                             |    | \
14953                             |    |   outgoing
14954                             |    |   arguments
14955    current stack pointer -> |    | /
14956                               --
14957
14958   For a given function some or all of these stack components
14959   may not be needed, giving rise to the possibility of
14960   eliminating some of the registers.
14961
14962   The values returned by this function must reflect the behavior
14963   of arm_expand_prologue() and arm_compute_save_reg_mask().
14964
14965   The sign of the number returned reflects the direction of stack
14966   growth, so the values are positive for all eliminations except
14967   from the soft frame pointer to the hard frame pointer.
14968
14969   SFP may point just inside the local variables block to ensure correct
14970   alignment.  */
14971
14972
14973 /* Calculate stack offsets.  These are used to calculate register elimination
14974    offsets and in prologue/epilogue code.  Also calculates which registers
14975    should be saved.  */
14976
14977 static arm_stack_offsets *
14978 arm_get_frame_offsets (void)
14979 {
14980   struct arm_stack_offsets *offsets;
14981   unsigned long func_type;
14982   int leaf;
14983   int saved;
14984   int core_saved;
14985   HOST_WIDE_INT frame_size;
14986   int i;
14987
14988   offsets = &cfun->machine->stack_offsets;
14989
14990   /* We need to know if we are a leaf function.  Unfortunately, it
14991      is possible to be called after start_sequence has been called,
14992      which causes get_insns to return the insns for the sequence,
14993      not the function, which will cause leaf_function_p to return
14994      the incorrect result.
14995
14996      to know about leaf functions once reload has completed, and the
14997      frame size cannot be changed after that time, so we can safely
14998      use the cached value.  */
14999
15000   if (reload_completed)
15001     return offsets;
15002
15003   /* Initially this is the size of the local variables.  It will translated
15004      into an offset once we have determined the size of preceding data.  */
15005   frame_size = ROUND_UP_WORD (get_frame_size ());
15006
15007   leaf = leaf_function_p ();
15008
15009   /* Space for variadic functions.  */
15010   offsets->saved_args = crtl->args.pretend_args_size;
15011
15012   /* In Thumb mode this is incorrect, but never used.  */
15013   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0) +
15014                    arm_compute_static_chain_stack_bytes();
15015
15016   if (TARGET_32BIT)
15017     {
15018       unsigned int regno;
15019
15020       offsets->saved_regs_mask = arm_compute_save_reg_mask ();
15021       core_saved = bit_count (offsets->saved_regs_mask) * 4;
15022       saved = core_saved;
15023
15024       /* We know that SP will be doubleword aligned on entry, and we must
15025          preserve that condition at any subroutine call.  We also require the
15026          soft frame pointer to be doubleword aligned.  */
15027
15028       if (TARGET_REALLY_IWMMXT)
15029         {
15030           /* Check for the call-saved iWMMXt registers.  */
15031           for (regno = FIRST_IWMMXT_REGNUM;
15032                regno <= LAST_IWMMXT_REGNUM;
15033                regno++)
15034             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
15035               saved += 8;
15036         }
15037
15038       func_type = arm_current_func_type ();
15039       if (! IS_VOLATILE (func_type))
15040         {
15041           /* Space for saved FPA registers.  */
15042           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
15043             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
15044             saved += 12;
15045
15046           /* Space for saved VFP registers.  */
15047           if (TARGET_HARD_FLOAT && TARGET_VFP)
15048             saved += arm_get_vfp_saved_size ();
15049         }
15050     }
15051   else /* TARGET_THUMB1 */
15052     {
15053       offsets->saved_regs_mask = thumb1_compute_save_reg_mask ();
15054       core_saved = bit_count (offsets->saved_regs_mask) * 4;
15055       saved = core_saved;
15056       if (TARGET_BACKTRACE)
15057         saved += 16;
15058     }
15059
15060   /* Saved registers include the stack frame.  */
15061   offsets->saved_regs = offsets->saved_args + saved +
15062                         arm_compute_static_chain_stack_bytes();
15063   offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
15064   /* A leaf function does not need any stack alignment if it has nothing
15065      on the stack.  */
15066   if (leaf && frame_size == 0)
15067     {
15068       offsets->outgoing_args = offsets->soft_frame;
15069       offsets->locals_base = offsets->soft_frame;
15070       return offsets;
15071     }
15072
15073   /* Ensure SFP has the correct alignment.  */
15074   if (ARM_DOUBLEWORD_ALIGN
15075       && (offsets->soft_frame & 7))
15076     {
15077       offsets->soft_frame += 4;
15078       /* Try to align stack by pushing an extra reg.  Don't bother doing this
15079          when there is a stack frame as the alignment will be rolled into
15080          the normal stack adjustment.  */
15081       if (frame_size + crtl->outgoing_args_size == 0)
15082         {
15083           int reg = -1;
15084
15085           /* If it is safe to use r3, then do so.  This sometimes 
15086              generates better code on Thumb-2 by avoiding the need to
15087              use 32-bit push/pop instructions.  */
15088           if (!crtl->tail_call_emit
15089               && arm_size_return_regs () <= 12
15090               && (offsets->saved_regs_mask & (1 << 3)) == 0)
15091             {
15092               reg = 3;
15093             }
15094           else
15095             for (i = 4; i <= (TARGET_THUMB1 ? LAST_LO_REGNUM : 11); i++)
15096               {
15097                 if ((offsets->saved_regs_mask & (1 << i)) == 0)
15098                   {
15099                     reg = i;
15100                     break;
15101                   }
15102               }
15103
15104           if (reg != -1)
15105             {
15106               offsets->saved_regs += 4;
15107               offsets->saved_regs_mask |= (1 << reg);
15108             }
15109         }
15110     }
15111
15112   offsets->locals_base = offsets->soft_frame + frame_size;
15113   offsets->outgoing_args = (offsets->locals_base
15114                             + crtl->outgoing_args_size);
15115
15116   if (ARM_DOUBLEWORD_ALIGN)
15117     {
15118       /* Ensure SP remains doubleword aligned.  */
15119       if (offsets->outgoing_args & 7)
15120         offsets->outgoing_args += 4;
15121       gcc_assert (!(offsets->outgoing_args & 7));
15122     }
15123
15124   return offsets;
15125 }
15126
15127
15128 /* Calculate the relative offsets for the different stack pointers.  Positive
15129    offsets are in the direction of stack growth.  */
15130
15131 HOST_WIDE_INT
15132 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
15133 {
15134   arm_stack_offsets *offsets;
15135
15136   offsets = arm_get_frame_offsets ();
15137
15138   /* OK, now we have enough information to compute the distances.
15139      There must be an entry in these switch tables for each pair
15140      of registers in ELIMINABLE_REGS, even if some of the entries
15141      seem to be redundant or useless.  */
15142   switch (from)
15143     {
15144     case ARG_POINTER_REGNUM:
15145       switch (to)
15146         {
15147         case THUMB_HARD_FRAME_POINTER_REGNUM:
15148           return 0;
15149
15150         case FRAME_POINTER_REGNUM:
15151           /* This is the reverse of the soft frame pointer
15152              to hard frame pointer elimination below.  */
15153           return offsets->soft_frame - offsets->saved_args;
15154
15155         case ARM_HARD_FRAME_POINTER_REGNUM:
15156           /* This is only non-zero in the case where the static chain register
15157              is stored above the frame.  */
15158           return offsets->frame - offsets->saved_args - 4;
15159
15160         case STACK_POINTER_REGNUM:
15161           /* If nothing has been pushed on the stack at all
15162              then this will return -4.  This *is* correct!  */
15163           return offsets->outgoing_args - (offsets->saved_args + 4);
15164
15165         default:
15166           gcc_unreachable ();
15167         }
15168       gcc_unreachable ();
15169
15170     case FRAME_POINTER_REGNUM:
15171       switch (to)
15172         {
15173         case THUMB_HARD_FRAME_POINTER_REGNUM:
15174           return 0;
15175
15176         case ARM_HARD_FRAME_POINTER_REGNUM:
15177           /* The hard frame pointer points to the top entry in the
15178              stack frame.  The soft frame pointer to the bottom entry
15179              in the stack frame.  If there is no stack frame at all,
15180              then they are identical.  */
15181
15182           return offsets->frame - offsets->soft_frame;
15183
15184         case STACK_POINTER_REGNUM:
15185           return offsets->outgoing_args - offsets->soft_frame;
15186
15187         default:
15188           gcc_unreachable ();
15189         }
15190       gcc_unreachable ();
15191
15192     default:
15193       /* You cannot eliminate from the stack pointer.
15194          In theory you could eliminate from the hard frame
15195          pointer to the stack pointer, but this will never
15196          happen, since if a stack frame is not needed the
15197          hard frame pointer will never be used.  */
15198       gcc_unreachable ();
15199     }
15200 }
15201
15202 /* Given FROM and TO register numbers, say whether this elimination is
15203    allowed.  Frame pointer elimination is automatically handled.
15204
15205    All eliminations are permissible.  Note that ARG_POINTER_REGNUM and
15206    HARD_FRAME_POINTER_REGNUM are in fact the same thing.  If we need a frame
15207    pointer, we must eliminate FRAME_POINTER_REGNUM into
15208    HARD_FRAME_POINTER_REGNUM and not into STACK_POINTER_REGNUM or
15209    ARG_POINTER_REGNUM.  */
15210
15211 bool
15212 arm_can_eliminate (const int from, const int to)
15213 {
15214   return ((to == FRAME_POINTER_REGNUM && from == ARG_POINTER_REGNUM) ? false :
15215           (to == STACK_POINTER_REGNUM && frame_pointer_needed) ? false :
15216           (to == ARM_HARD_FRAME_POINTER_REGNUM && TARGET_THUMB) ? false :
15217           (to == THUMB_HARD_FRAME_POINTER_REGNUM && TARGET_ARM) ? false :
15218            true);
15219 }
15220
15221 /* Emit RTL to save coprocessor registers on function entry.  Returns the
15222    number of bytes pushed.  */
15223
15224 static int
15225 arm_save_coproc_regs(void)
15226 {
15227   int saved_size = 0;
15228   unsigned reg;
15229   unsigned start_reg;
15230   rtx insn;
15231
15232   for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
15233     if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
15234       {
15235         insn = gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx);
15236         insn = gen_rtx_MEM (V2SImode, insn);
15237         insn = emit_set_insn (insn, gen_rtx_REG (V2SImode, reg));
15238         RTX_FRAME_RELATED_P (insn) = 1;
15239         saved_size += 8;
15240       }
15241
15242   /* Save any floating point call-saved registers used by this
15243      function.  */
15244   if (TARGET_FPA_EMU2)
15245     {
15246       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
15247         if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
15248           {
15249             insn = gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx);
15250             insn = gen_rtx_MEM (XFmode, insn);
15251             insn = emit_set_insn (insn, gen_rtx_REG (XFmode, reg));
15252             RTX_FRAME_RELATED_P (insn) = 1;
15253             saved_size += 12;
15254           }
15255     }
15256   else
15257     {
15258       start_reg = LAST_FPA_REGNUM;
15259
15260       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
15261         {
15262           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
15263             {
15264               if (start_reg - reg == 3)
15265                 {
15266                   insn = emit_sfm (reg, 4);
15267                   RTX_FRAME_RELATED_P (insn) = 1;
15268                   saved_size += 48;
15269                   start_reg = reg - 1;
15270                 }
15271             }
15272           else
15273             {
15274               if (start_reg != reg)
15275                 {
15276                   insn = emit_sfm (reg + 1, start_reg - reg);
15277                   RTX_FRAME_RELATED_P (insn) = 1;
15278                   saved_size += (start_reg - reg) * 12;
15279                 }
15280               start_reg = reg - 1;
15281             }
15282         }
15283
15284       if (start_reg != reg)
15285         {
15286           insn = emit_sfm (reg + 1, start_reg - reg);
15287           saved_size += (start_reg - reg) * 12;
15288           RTX_FRAME_RELATED_P (insn) = 1;
15289         }
15290     }
15291   if (TARGET_HARD_FLOAT && TARGET_VFP)
15292     {
15293       start_reg = FIRST_VFP_REGNUM;
15294
15295       for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
15296         {
15297           if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
15298               && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
15299             {
15300               if (start_reg != reg)
15301                 saved_size += vfp_emit_fstmd (start_reg,
15302                                               (reg - start_reg) / 2);
15303               start_reg = reg + 2;
15304             }
15305         }
15306       if (start_reg != reg)
15307         saved_size += vfp_emit_fstmd (start_reg,
15308                                       (reg - start_reg) / 2);
15309     }
15310   return saved_size;
15311 }
15312
15313
15314 /* Set the Thumb frame pointer from the stack pointer.  */
15315
15316 static void
15317 thumb_set_frame_pointer (arm_stack_offsets *offsets)
15318 {
15319   HOST_WIDE_INT amount;
15320   rtx insn, dwarf;
15321
15322   amount = offsets->outgoing_args - offsets->locals_base;
15323   if (amount < 1024)
15324     insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15325                                   stack_pointer_rtx, GEN_INT (amount)));
15326   else
15327     {
15328       emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
15329       /* Thumb-2 RTL patterns expect sp as the first input.  Thumb-1
15330          expects the first two operands to be the same.  */
15331       if (TARGET_THUMB2)
15332         {
15333           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15334                                         stack_pointer_rtx,
15335                                         hard_frame_pointer_rtx));
15336         }
15337       else
15338         {
15339           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15340                                         hard_frame_pointer_rtx,
15341                                         stack_pointer_rtx));
15342         }
15343       dwarf = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
15344                            plus_constant (stack_pointer_rtx, amount));
15345       RTX_FRAME_RELATED_P (dwarf) = 1;
15346       add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
15347     }
15348
15349   RTX_FRAME_RELATED_P (insn) = 1;
15350 }
15351
15352 /* Generate the prologue instructions for entry into an ARM or Thumb-2
15353    function.  */
15354 void
15355 arm_expand_prologue (void)
15356 {
15357   rtx amount;
15358   rtx insn;
15359   rtx ip_rtx;
15360   unsigned long live_regs_mask;
15361   unsigned long func_type;
15362   int fp_offset = 0;
15363   int saved_pretend_args = 0;
15364   int saved_regs = 0;
15365   unsigned HOST_WIDE_INT args_to_push;
15366   arm_stack_offsets *offsets;
15367
15368   func_type = arm_current_func_type ();
15369
15370   /* Naked functions don't have prologues.  */
15371   if (IS_NAKED (func_type))
15372     return;
15373
15374   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
15375   args_to_push = crtl->args.pretend_args_size;
15376
15377   /* Compute which register we will have to save onto the stack.  */
15378   offsets = arm_get_frame_offsets ();
15379   live_regs_mask = offsets->saved_regs_mask;
15380
15381   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
15382
15383   if (IS_STACKALIGN (func_type))
15384     {
15385       rtx dwarf;
15386       rtx r0;
15387       rtx r1;
15388       /* Handle a word-aligned stack pointer.  We generate the following:
15389
15390           mov r0, sp
15391           bic r1, r0, #7
15392           mov sp, r1
15393           <save and restore r0 in normal prologue/epilogue>
15394           mov sp, r0
15395           bx lr
15396
15397          The unwinder doesn't need to know about the stack realignment.
15398          Just tell it we saved SP in r0.  */
15399       gcc_assert (TARGET_THUMB2 && !arm_arch_notm && args_to_push == 0);
15400
15401       r0 = gen_rtx_REG (SImode, 0);
15402       r1 = gen_rtx_REG (SImode, 1);
15403       /* Use a real rtvec rather than NULL_RTVEC so the rest of the
15404          compiler won't choke.  */
15405       dwarf = gen_rtx_UNSPEC (SImode, rtvec_alloc (0), UNSPEC_STACK_ALIGN);
15406       dwarf = gen_rtx_SET (VOIDmode, r0, dwarf);
15407       insn = gen_movsi (r0, stack_pointer_rtx);
15408       RTX_FRAME_RELATED_P (insn) = 1;
15409       add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
15410       emit_insn (insn);
15411       emit_insn (gen_andsi3 (r1, r0, GEN_INT (~(HOST_WIDE_INT)7)));
15412       emit_insn (gen_movsi (stack_pointer_rtx, r1));
15413     }
15414
15415   /* For APCS frames, if IP register is clobbered
15416      when creating frame, save that register in a special
15417      way.  */
15418   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
15419     {
15420       if (IS_INTERRUPT (func_type))
15421         {
15422           /* Interrupt functions must not corrupt any registers.
15423              Creating a frame pointer however, corrupts the IP
15424              register, so we must push it first.  */
15425           insn = emit_multi_reg_push (1 << IP_REGNUM);
15426
15427           /* Do not set RTX_FRAME_RELATED_P on this insn.
15428              The dwarf stack unwinding code only wants to see one
15429              stack decrement per function, and this is not it.  If
15430              this instruction is labeled as being part of the frame
15431              creation sequence then dwarf2out_frame_debug_expr will
15432              die when it encounters the assignment of IP to FP
15433              later on, since the use of SP here establishes SP as
15434              the CFA register and not IP.
15435
15436              Anyway this instruction is not really part of the stack
15437              frame creation although it is part of the prologue.  */
15438         }
15439       else if (IS_NESTED (func_type))
15440         {
15441           /* The Static chain register is the same as the IP register
15442              used as a scratch register during stack frame creation.
15443              To get around this need to find somewhere to store IP
15444              whilst the frame is being created.  We try the following
15445              places in order:
15446
15447                1. The last argument register.
15448                2. A slot on the stack above the frame.  (This only
15449                   works if the function is not a varargs function).
15450                3. Register r3, after pushing the argument registers
15451                   onto the stack.
15452
15453              Note - we only need to tell the dwarf2 backend about the SP
15454              adjustment in the second variant; the static chain register
15455              doesn't need to be unwound, as it doesn't contain a value
15456              inherited from the caller.  */
15457
15458           if (df_regs_ever_live_p (3) == false)
15459             insn = emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
15460           else if (args_to_push == 0)
15461             {
15462               rtx dwarf;
15463
15464               gcc_assert(arm_compute_static_chain_stack_bytes() == 4);
15465               saved_regs += 4;
15466
15467               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
15468               insn = emit_set_insn (gen_frame_mem (SImode, insn), ip_rtx);
15469               fp_offset = 4;
15470
15471               /* Just tell the dwarf backend that we adjusted SP.  */
15472               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
15473                                    plus_constant (stack_pointer_rtx,
15474                                                   -fp_offset));
15475               RTX_FRAME_RELATED_P (insn) = 1;
15476               add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
15477             }
15478           else
15479             {
15480               /* Store the args on the stack.  */
15481               if (cfun->machine->uses_anonymous_args)
15482                 insn = emit_multi_reg_push
15483                   ((0xf0 >> (args_to_push / 4)) & 0xf);
15484               else
15485                 insn = emit_insn
15486                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
15487                                GEN_INT (- args_to_push)));
15488
15489               RTX_FRAME_RELATED_P (insn) = 1;
15490
15491               saved_pretend_args = 1;
15492               fp_offset = args_to_push;
15493               args_to_push = 0;
15494
15495               /* Now reuse r3 to preserve IP.  */
15496               emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
15497             }
15498         }
15499
15500       insn = emit_set_insn (ip_rtx,
15501                             plus_constant (stack_pointer_rtx, fp_offset));
15502       RTX_FRAME_RELATED_P (insn) = 1;
15503     }
15504
15505   if (args_to_push)
15506     {
15507       /* Push the argument registers, or reserve space for them.  */
15508       if (cfun->machine->uses_anonymous_args)
15509         insn = emit_multi_reg_push
15510           ((0xf0 >> (args_to_push / 4)) & 0xf);
15511       else
15512         insn = emit_insn
15513           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
15514                        GEN_INT (- args_to_push)));
15515       RTX_FRAME_RELATED_P (insn) = 1;
15516     }
15517
15518   /* If this is an interrupt service routine, and the link register
15519      is going to be pushed, and we're not generating extra
15520      push of IP (needed when frame is needed and frame layout if apcs),
15521      subtracting four from LR now will mean that the function return
15522      can be done with a single instruction.  */
15523   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
15524       && (live_regs_mask & (1 << LR_REGNUM)) != 0
15525       && !(frame_pointer_needed && TARGET_APCS_FRAME)
15526       && TARGET_ARM)
15527     {
15528       rtx lr = gen_rtx_REG (SImode, LR_REGNUM);
15529       
15530       emit_set_insn (lr, plus_constant (lr, -4));
15531     }
15532
15533   if (live_regs_mask)
15534     {
15535       saved_regs += bit_count (live_regs_mask) * 4;
15536       if (optimize_size && !frame_pointer_needed
15537           && saved_regs == offsets->saved_regs - offsets->saved_args)
15538         {
15539           /* If no coprocessor registers are being pushed and we don't have
15540              to worry about a frame pointer then push extra registers to
15541              create the stack frame.  This is done is a way that does not
15542              alter the frame layout, so is independent of the epilogue.  */
15543           int n;
15544           int frame;
15545           n = 0;
15546           while (n < 8 && (live_regs_mask & (1 << n)) == 0)
15547             n++;
15548           frame = offsets->outgoing_args - (offsets->saved_args + saved_regs);
15549           if (frame && n * 4 >= frame)
15550             {
15551               n = frame / 4;
15552               live_regs_mask |= (1 << n) - 1;
15553               saved_regs += frame;
15554             }
15555         }
15556       insn = emit_multi_reg_push (live_regs_mask);
15557       RTX_FRAME_RELATED_P (insn) = 1;
15558     }
15559
15560   if (! IS_VOLATILE (func_type))
15561     saved_regs += arm_save_coproc_regs ();
15562
15563   if (frame_pointer_needed && TARGET_ARM)
15564     {
15565       /* Create the new frame pointer.  */
15566       if (TARGET_APCS_FRAME)
15567         {
15568           insn = GEN_INT (-(4 + args_to_push + fp_offset));
15569           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
15570           RTX_FRAME_RELATED_P (insn) = 1;
15571
15572           if (IS_NESTED (func_type))
15573             {
15574               /* Recover the static chain register.  */
15575               if (!df_regs_ever_live_p (3)
15576                   || saved_pretend_args)
15577                 insn = gen_rtx_REG (SImode, 3);
15578               else /* if (crtl->args.pretend_args_size == 0) */
15579                 {
15580                   insn = plus_constant (hard_frame_pointer_rtx, 4);
15581                   insn = gen_frame_mem (SImode, insn);
15582                 }
15583               emit_set_insn (ip_rtx, insn);
15584               /* Add a USE to stop propagate_one_insn() from barfing.  */
15585               emit_insn (gen_prologue_use (ip_rtx));
15586             }
15587         }
15588       else
15589         {
15590           insn = GEN_INT (saved_regs - 4);
15591           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15592                                         stack_pointer_rtx, insn));
15593           RTX_FRAME_RELATED_P (insn) = 1;
15594         }
15595     }
15596
15597   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
15598     {
15599       /* This add can produce multiple insns for a large constant, so we
15600          need to get tricky.  */
15601       rtx last = get_last_insn ();
15602
15603       amount = GEN_INT (offsets->saved_args + saved_regs
15604                         - offsets->outgoing_args);
15605
15606       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
15607                                     amount));
15608       do
15609         {
15610           last = last ? NEXT_INSN (last) : get_insns ();
15611           RTX_FRAME_RELATED_P (last) = 1;
15612         }
15613       while (last != insn);
15614
15615       /* If the frame pointer is needed, emit a special barrier that
15616          will prevent the scheduler from moving stores to the frame
15617          before the stack adjustment.  */
15618       if (frame_pointer_needed)
15619         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
15620                                          hard_frame_pointer_rtx));
15621     }
15622
15623
15624   if (frame_pointer_needed && TARGET_THUMB2)
15625     thumb_set_frame_pointer (offsets);
15626
15627   if (flag_pic && arm_pic_register != INVALID_REGNUM)
15628     {
15629       unsigned long mask;
15630
15631       mask = live_regs_mask;
15632       mask &= THUMB2_WORK_REGS;
15633       if (!IS_NESTED (func_type))
15634         mask |= (1 << IP_REGNUM);
15635       arm_load_pic_register (mask);
15636     }
15637
15638   /* If we are profiling, make sure no instructions are scheduled before
15639      the call to mcount.  Similarly if the user has requested no
15640      scheduling in the prolog.  Similarly if we want non-call exceptions
15641      using the EABI unwinder, to prevent faulting instructions from being
15642      swapped with a stack adjustment.  */
15643   if (crtl->profile || !TARGET_SCHED_PROLOG
15644       || (ARM_EABI_UNWIND_TABLES && cfun->can_throw_non_call_exceptions))
15645     emit_insn (gen_blockage ());
15646
15647   /* If the link register is being kept alive, with the return address in it,
15648      then make sure that it does not get reused by the ce2 pass.  */
15649   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
15650     cfun->machine->lr_save_eliminated = 1;
15651 }
15652 \f
15653 /* Print condition code to STREAM.  Helper function for arm_print_operand.  */
15654 static void
15655 arm_print_condition (FILE *stream)
15656 {
15657   if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
15658     {
15659       /* Branch conversion is not implemented for Thumb-2.  */
15660       if (TARGET_THUMB)
15661         {
15662           output_operand_lossage ("predicated Thumb instruction");
15663           return;
15664         }
15665       if (current_insn_predicate != NULL)
15666         {
15667           output_operand_lossage
15668             ("predicated instruction in conditional sequence");
15669           return;
15670         }
15671
15672       fputs (arm_condition_codes[arm_current_cc], stream);
15673     }
15674   else if (current_insn_predicate)
15675     {
15676       enum arm_cond_code code;
15677
15678       if (TARGET_THUMB1)
15679         {
15680           output_operand_lossage ("predicated Thumb instruction");
15681           return;
15682         }
15683
15684       code = get_arm_condition_code (current_insn_predicate);
15685       fputs (arm_condition_codes[code], stream);
15686     }
15687 }
15688
15689
15690 /* If CODE is 'd', then the X is a condition operand and the instruction
15691    should only be executed if the condition is true.
15692    if CODE is 'D', then the X is a condition operand and the instruction
15693    should only be executed if the condition is false: however, if the mode
15694    of the comparison is CCFPEmode, then always execute the instruction -- we
15695    do this because in these circumstances !GE does not necessarily imply LT;
15696    in these cases the instruction pattern will take care to make sure that
15697    an instruction containing %d will follow, thereby undoing the effects of
15698    doing this instruction unconditionally.
15699    If CODE is 'N' then X is a floating point operand that must be negated
15700    before output.
15701    If CODE is 'B' then output a bitwise inverted value of X (a const int).
15702    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
15703 static void
15704 arm_print_operand (FILE *stream, rtx x, int code)
15705 {
15706   switch (code)
15707     {
15708     case '@':
15709       fputs (ASM_COMMENT_START, stream);
15710       return;
15711
15712     case '_':
15713       fputs (user_label_prefix, stream);
15714       return;
15715
15716     case '|':
15717       fputs (REGISTER_PREFIX, stream);
15718       return;
15719
15720     case '?':
15721       arm_print_condition (stream);
15722       return;
15723
15724     case '(':
15725       /* Nothing in unified syntax, otherwise the current condition code.  */
15726       if (!TARGET_UNIFIED_ASM)
15727         arm_print_condition (stream);
15728       break;
15729
15730     case ')':
15731       /* The current condition code in unified syntax, otherwise nothing.  */
15732       if (TARGET_UNIFIED_ASM)
15733         arm_print_condition (stream);
15734       break;
15735   
15736     case '.':
15737       /* The current condition code for a condition code setting instruction.
15738          Preceded by 's' in unified syntax, otherwise followed by 's'.  */
15739       if (TARGET_UNIFIED_ASM)
15740         {
15741           fputc('s', stream);
15742           arm_print_condition (stream);
15743         }
15744       else
15745         {
15746           arm_print_condition (stream);
15747           fputc('s', stream);
15748         }
15749       return;
15750
15751     case '!':
15752       /* If the instruction is conditionally executed then print
15753          the current condition code, otherwise print 's'.  */
15754       gcc_assert (TARGET_THUMB2 && TARGET_UNIFIED_ASM);
15755       if (current_insn_predicate)
15756         arm_print_condition (stream);
15757       else
15758         fputc('s', stream);
15759       break;
15760
15761     /* %# is a "break" sequence. It doesn't output anything, but is used to
15762        separate e.g. operand numbers from following text, if that text consists
15763        of further digits which we don't want to be part of the operand
15764        number.  */
15765     case '#':
15766       return;
15767
15768     case 'N':
15769       {
15770         REAL_VALUE_TYPE r;
15771         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
15772         r = real_value_negate (&r);
15773         fprintf (stream, "%s", fp_const_from_val (&r));
15774       }
15775       return;
15776
15777     /* An integer or symbol address without a preceding # sign.  */
15778     case 'c':
15779       switch (GET_CODE (x))
15780         {
15781         case CONST_INT:
15782           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
15783           break;
15784
15785         case SYMBOL_REF:
15786           output_addr_const (stream, x);
15787           break;
15788
15789         default:
15790           gcc_unreachable ();
15791         }
15792       return;
15793
15794     case 'B':
15795       if (GET_CODE (x) == CONST_INT)
15796         {
15797           HOST_WIDE_INT val;
15798           val = ARM_SIGN_EXTEND (~INTVAL (x));
15799           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
15800         }
15801       else
15802         {
15803           putc ('~', stream);
15804           output_addr_const (stream, x);
15805         }
15806       return;
15807
15808     case 'L':
15809       /* The low 16 bits of an immediate constant.  */
15810       fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL(x) & 0xffff);
15811       return;
15812
15813     case 'i':
15814       fprintf (stream, "%s", arithmetic_instr (x, 1));
15815       return;
15816
15817     /* Truncate Cirrus shift counts.  */
15818     case 's':
15819       if (GET_CODE (x) == CONST_INT)
15820         {
15821           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
15822           return;
15823         }
15824       arm_print_operand (stream, x, 0);
15825       return;
15826
15827     case 'I':
15828       fprintf (stream, "%s", arithmetic_instr (x, 0));
15829       return;
15830
15831     case 'S':
15832       {
15833         HOST_WIDE_INT val;
15834         const char *shift;
15835
15836         if (!shift_operator (x, SImode))
15837           {
15838             output_operand_lossage ("invalid shift operand");
15839             break;
15840           }
15841
15842         shift = shift_op (x, &val);
15843
15844         if (shift)
15845           {
15846             fprintf (stream, ", %s ", shift);
15847             if (val == -1)
15848               arm_print_operand (stream, XEXP (x, 1), 0);
15849             else
15850               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
15851           }
15852       }
15853       return;
15854
15855       /* An explanation of the 'Q', 'R' and 'H' register operands:
15856
15857          In a pair of registers containing a DI or DF value the 'Q'
15858          operand returns the register number of the register containing
15859          the least significant part of the value.  The 'R' operand returns
15860          the register number of the register containing the most
15861          significant part of the value.
15862
15863          The 'H' operand returns the higher of the two register numbers.
15864          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
15865          same as the 'Q' operand, since the most significant part of the
15866          value is held in the lower number register.  The reverse is true
15867          on systems where WORDS_BIG_ENDIAN is false.
15868
15869          The purpose of these operands is to distinguish between cases
15870          where the endian-ness of the values is important (for example
15871          when they are added together), and cases where the endian-ness
15872          is irrelevant, but the order of register operations is important.
15873          For example when loading a value from memory into a register
15874          pair, the endian-ness does not matter.  Provided that the value
15875          from the lower memory address is put into the lower numbered
15876          register, and the value from the higher address is put into the
15877          higher numbered register, the load will work regardless of whether
15878          the value being loaded is big-wordian or little-wordian.  The
15879          order of the two register loads can matter however, if the address
15880          of the memory location is actually held in one of the registers
15881          being overwritten by the load.
15882
15883          The 'Q' and 'R' constraints are also available for 64-bit
15884          constants.  */
15885     case 'Q':
15886       if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
15887         {
15888           rtx part = gen_lowpart (SImode, x);
15889           fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, INTVAL (part));
15890           return;
15891         }
15892
15893       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
15894         {
15895           output_operand_lossage ("invalid operand for code '%c'", code);
15896           return;
15897         }
15898
15899       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
15900       return;
15901
15902     case 'R':
15903       if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
15904         {
15905           enum machine_mode mode = GET_MODE (x);
15906           rtx part;
15907
15908           if (mode == VOIDmode)
15909             mode = DImode;
15910           part = gen_highpart_mode (SImode, mode, x);
15911           fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, INTVAL (part));
15912           return;
15913         }
15914
15915       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
15916         {
15917           output_operand_lossage ("invalid operand for code '%c'", code);
15918           return;
15919         }
15920
15921       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
15922       return;
15923
15924     case 'H':
15925       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
15926         {
15927           output_operand_lossage ("invalid operand for code '%c'", code);
15928           return;
15929         }
15930
15931       asm_fprintf (stream, "%r", REGNO (x) + 1);
15932       return;
15933
15934     case 'J':
15935       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
15936         {
15937           output_operand_lossage ("invalid operand for code '%c'", code);
15938           return;
15939         }
15940
15941       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 3 : 2));
15942       return;
15943
15944     case 'K':
15945       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
15946         {
15947           output_operand_lossage ("invalid operand for code '%c'", code);
15948           return;
15949         }
15950
15951       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 2 : 3));
15952       return;
15953
15954     case 'm':
15955       asm_fprintf (stream, "%r",
15956                    GET_CODE (XEXP (x, 0)) == REG
15957                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
15958       return;
15959
15960     case 'M':
15961       asm_fprintf (stream, "{%r-%r}",
15962                    REGNO (x),
15963                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
15964       return;
15965
15966     /* Like 'M', but writing doubleword vector registers, for use by Neon
15967        insns.  */
15968     case 'h':
15969       {
15970         int regno = (REGNO (x) - FIRST_VFP_REGNUM) / 2;
15971         int numregs = ARM_NUM_REGS (GET_MODE (x)) / 2;
15972         if (numregs == 1)
15973           asm_fprintf (stream, "{d%d}", regno);
15974         else
15975           asm_fprintf (stream, "{d%d-d%d}", regno, regno + numregs - 1);
15976       }
15977       return;
15978
15979     case 'd':
15980       /* CONST_TRUE_RTX means always -- that's the default.  */
15981       if (x == const_true_rtx)
15982         return;
15983
15984       if (!COMPARISON_P (x))
15985         {
15986           output_operand_lossage ("invalid operand for code '%c'", code);
15987           return;
15988         }
15989
15990       fputs (arm_condition_codes[get_arm_condition_code (x)],
15991              stream);
15992       return;
15993
15994     case 'D':
15995       /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
15996          want to do that.  */
15997       if (x == const_true_rtx)
15998         {
15999           output_operand_lossage ("instruction never executed");
16000           return;
16001         }
16002       if (!COMPARISON_P (x))
16003         {
16004           output_operand_lossage ("invalid operand for code '%c'", code);
16005           return;
16006         }
16007
16008       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
16009                                  (get_arm_condition_code (x))],
16010              stream);
16011       return;
16012
16013     /* Cirrus registers can be accessed in a variety of ways:
16014          single floating point (f)
16015          double floating point (d)
16016          32bit integer         (fx)
16017          64bit integer         (dx).  */
16018     case 'W':                   /* Cirrus register in F mode.  */
16019     case 'X':                   /* Cirrus register in D mode.  */
16020     case 'Y':                   /* Cirrus register in FX mode.  */
16021     case 'Z':                   /* Cirrus register in DX mode.  */
16022       gcc_assert (GET_CODE (x) == REG
16023                   && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
16024
16025       fprintf (stream, "mv%s%s",
16026                code == 'W' ? "f"
16027                : code == 'X' ? "d"
16028                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
16029
16030       return;
16031
16032     /* Print cirrus register in the mode specified by the register's mode.  */
16033     case 'V':
16034       {
16035         int mode = GET_MODE (x);
16036
16037         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
16038           {
16039             output_operand_lossage ("invalid operand for code '%c'", code);
16040             return;
16041           }
16042
16043         fprintf (stream, "mv%s%s",
16044                  mode == DFmode ? "d"
16045                  : mode == SImode ? "fx"
16046                  : mode == DImode ? "dx"
16047                  : "f", reg_names[REGNO (x)] + 2);
16048
16049         return;
16050       }
16051
16052     case 'U':
16053       if (GET_CODE (x) != REG
16054           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
16055           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
16056         /* Bad value for wCG register number.  */
16057         {
16058           output_operand_lossage ("invalid operand for code '%c'", code);
16059           return;
16060         }
16061
16062       else
16063         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
16064       return;
16065
16066       /* Print an iWMMXt control register name.  */
16067     case 'w':
16068       if (GET_CODE (x) != CONST_INT
16069           || INTVAL (x) < 0
16070           || INTVAL (x) >= 16)
16071         /* Bad value for wC register number.  */
16072         {
16073           output_operand_lossage ("invalid operand for code '%c'", code);
16074           return;
16075         }
16076
16077       else
16078         {
16079           static const char * wc_reg_names [16] =
16080             {
16081               "wCID",  "wCon",  "wCSSF", "wCASF",
16082               "wC4",   "wC5",   "wC6",   "wC7",
16083               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
16084               "wC12",  "wC13",  "wC14",  "wC15"
16085             };
16086
16087           fprintf (stream, wc_reg_names [INTVAL (x)]);
16088         }
16089       return;
16090
16091     /* Print the high single-precision register of a VFP double-precision
16092        register.  */
16093     case 'p':
16094       {
16095         int mode = GET_MODE (x);
16096         int regno;
16097
16098         if (GET_MODE_SIZE (mode) != 8 || GET_CODE (x) != REG)
16099           {
16100             output_operand_lossage ("invalid operand for code '%c'", code);
16101             return;
16102           }
16103
16104         regno = REGNO (x);
16105         if (!VFP_REGNO_OK_FOR_DOUBLE (regno))
16106           {
16107             output_operand_lossage ("invalid operand for code '%c'", code);
16108             return;
16109           }
16110
16111         fprintf (stream, "s%d", regno - FIRST_VFP_REGNUM + 1);
16112       }
16113       return;
16114
16115     /* Print a VFP/Neon double precision or quad precision register name.  */
16116     case 'P':
16117     case 'q':
16118       {
16119         int mode = GET_MODE (x);
16120         int is_quad = (code == 'q');
16121         int regno;
16122
16123         if (GET_MODE_SIZE (mode) != (is_quad ? 16 : 8))
16124           {
16125             output_operand_lossage ("invalid operand for code '%c'", code);
16126             return;
16127           }
16128
16129         if (GET_CODE (x) != REG
16130             || !IS_VFP_REGNUM (REGNO (x)))
16131           {
16132             output_operand_lossage ("invalid operand for code '%c'", code);
16133             return;
16134           }
16135
16136         regno = REGNO (x);
16137         if ((is_quad && !NEON_REGNO_OK_FOR_QUAD (regno))
16138             || (!is_quad && !VFP_REGNO_OK_FOR_DOUBLE (regno)))
16139           {
16140             output_operand_lossage ("invalid operand for code '%c'", code);
16141             return;
16142           }
16143
16144         fprintf (stream, "%c%d", is_quad ? 'q' : 'd',
16145           (regno - FIRST_VFP_REGNUM) >> (is_quad ? 2 : 1));
16146       }
16147       return;
16148
16149     /* These two codes print the low/high doubleword register of a Neon quad
16150        register, respectively.  For pair-structure types, can also print
16151        low/high quadword registers.  */
16152     case 'e':
16153     case 'f':
16154       {
16155         int mode = GET_MODE (x);
16156         int regno;
16157
16158         if ((GET_MODE_SIZE (mode) != 16
16159              && GET_MODE_SIZE (mode) != 32) || GET_CODE (x) != REG)
16160           {
16161             output_operand_lossage ("invalid operand for code '%c'", code);
16162             return;
16163           }
16164
16165         regno = REGNO (x);
16166         if (!NEON_REGNO_OK_FOR_QUAD (regno))
16167           {
16168             output_operand_lossage ("invalid operand for code '%c'", code);
16169             return;
16170           }
16171
16172         if (GET_MODE_SIZE (mode) == 16)
16173           fprintf (stream, "d%d", ((regno - FIRST_VFP_REGNUM) >> 1)
16174                                   + (code == 'f' ? 1 : 0));
16175         else
16176           fprintf (stream, "q%d", ((regno - FIRST_VFP_REGNUM) >> 2)
16177                                   + (code == 'f' ? 1 : 0));
16178       }
16179       return;
16180
16181     /* Print a VFPv3 floating-point constant, represented as an integer
16182        index.  */
16183     case 'G':
16184       {
16185         int index = vfp3_const_double_index (x);
16186         gcc_assert (index != -1);
16187         fprintf (stream, "%d", index);
16188       }
16189       return;
16190
16191     /* Print bits representing opcode features for Neon.
16192
16193        Bit 0 is 1 for signed, 0 for unsigned.  Floats count as signed
16194        and polynomials as unsigned.
16195
16196        Bit 1 is 1 for floats and polynomials, 0 for ordinary integers.
16197
16198        Bit 2 is 1 for rounding functions, 0 otherwise.  */
16199
16200     /* Identify the type as 's', 'u', 'p' or 'f'.  */
16201     case 'T':
16202       {
16203         HOST_WIDE_INT bits = INTVAL (x);
16204         fputc ("uspf"[bits & 3], stream);
16205       }
16206       return;
16207
16208     /* Likewise, but signed and unsigned integers are both 'i'.  */
16209     case 'F':
16210       {
16211         HOST_WIDE_INT bits = INTVAL (x);
16212         fputc ("iipf"[bits & 3], stream);
16213       }
16214       return;
16215
16216     /* As for 'T', but emit 'u' instead of 'p'.  */
16217     case 't':
16218       {
16219         HOST_WIDE_INT bits = INTVAL (x);
16220         fputc ("usuf"[bits & 3], stream);
16221       }
16222       return;
16223
16224     /* Bit 2: rounding (vs none).  */
16225     case 'O':
16226       {
16227         HOST_WIDE_INT bits = INTVAL (x);
16228         fputs ((bits & 4) != 0 ? "r" : "", stream);
16229       }
16230       return;
16231
16232     /* Memory operand for vld1/vst1 instruction.  */
16233     case 'A':
16234       {
16235         rtx addr;
16236         bool postinc = FALSE;
16237         gcc_assert (GET_CODE (x) == MEM);
16238         addr = XEXP (x, 0);
16239         if (GET_CODE (addr) == POST_INC)
16240           {
16241             postinc = 1;
16242             addr = XEXP (addr, 0);
16243           }
16244         asm_fprintf (stream, "[%r]", REGNO (addr));
16245         if (postinc)
16246           fputs("!", stream);
16247       }
16248       return;
16249
16250     /* Translate an S register number into a D register number and element index.  */
16251     case 'y':
16252       {
16253         int mode = GET_MODE (x);
16254         int regno;
16255
16256         if (GET_MODE_SIZE (mode) != 4 || GET_CODE (x) != REG)
16257           {
16258             output_operand_lossage ("invalid operand for code '%c'", code);
16259             return;
16260           }
16261
16262         regno = REGNO (x);
16263         if (!VFP_REGNO_OK_FOR_SINGLE (regno))
16264           {
16265             output_operand_lossage ("invalid operand for code '%c'", code);
16266             return;
16267           }
16268
16269         regno = regno - FIRST_VFP_REGNUM;
16270         fprintf (stream, "d%d[%d]", regno / 2, regno % 2);
16271       }
16272       return;
16273
16274     /* Register specifier for vld1.16/vst1.16.  Translate the S register
16275        number into a D register number and element index.  */
16276     case 'z':
16277       {
16278         int mode = GET_MODE (x);
16279         int regno;
16280
16281         if (GET_MODE_SIZE (mode) != 2 || GET_CODE (x) != REG)
16282           {
16283             output_operand_lossage ("invalid operand for code '%c'", code);
16284             return;
16285           }
16286
16287         regno = REGNO (x);
16288         if (!VFP_REGNO_OK_FOR_SINGLE (regno))
16289           {
16290             output_operand_lossage ("invalid operand for code '%c'", code);
16291             return;
16292           }
16293
16294         regno = regno - FIRST_VFP_REGNUM;
16295         fprintf (stream, "d%d[%d]", regno/2, ((regno % 2) ? 2 : 0));
16296       }
16297       return;
16298       
16299     default:
16300       if (x == 0)
16301         {
16302           output_operand_lossage ("missing operand");
16303           return;
16304         }
16305
16306       switch (GET_CODE (x))
16307         {
16308         case REG:
16309           asm_fprintf (stream, "%r", REGNO (x));
16310           break;
16311
16312         case MEM:
16313           output_memory_reference_mode = GET_MODE (x);
16314           output_address (XEXP (x, 0));
16315           break;
16316
16317         case CONST_DOUBLE:
16318           if (TARGET_NEON)
16319             {
16320               char fpstr[20];
16321               real_to_decimal (fpstr, CONST_DOUBLE_REAL_VALUE (x),
16322                                sizeof (fpstr), 0, 1);
16323               fprintf (stream, "#%s", fpstr);
16324             }
16325           else
16326             fprintf (stream, "#%s", fp_immediate_constant (x));
16327           break;
16328
16329         default:
16330           gcc_assert (GET_CODE (x) != NEG);
16331           fputc ('#', stream);
16332           if (GET_CODE (x) == HIGH)
16333             {
16334               fputs (":lower16:", stream);
16335               x = XEXP (x, 0);
16336             }
16337             
16338           output_addr_const (stream, x);
16339           break;
16340         }
16341     }
16342 }
16343 \f
16344 /* Target hook for printing a memory address.  */
16345 static void
16346 arm_print_operand_address (FILE *stream, rtx x)
16347 {
16348   if (TARGET_32BIT)
16349     {
16350       int is_minus = GET_CODE (x) == MINUS;
16351
16352       if (GET_CODE (x) == REG)
16353         asm_fprintf (stream, "[%r, #0]", REGNO (x));
16354       else if (GET_CODE (x) == PLUS || is_minus)
16355         {
16356           rtx base = XEXP (x, 0);
16357           rtx index = XEXP (x, 1);
16358           HOST_WIDE_INT offset = 0;
16359           if (GET_CODE (base) != REG
16360               || (GET_CODE (index) == REG && REGNO (index) == SP_REGNUM))
16361             {
16362               /* Ensure that BASE is a register.  */
16363               /* (one of them must be).  */
16364               /* Also ensure the SP is not used as in index register.  */
16365               rtx temp = base;
16366               base = index;
16367               index = temp;
16368             }
16369           switch (GET_CODE (index))
16370             {
16371             case CONST_INT:
16372               offset = INTVAL (index);
16373               if (is_minus)
16374                 offset = -offset;
16375               asm_fprintf (stream, "[%r, #%wd]",
16376                            REGNO (base), offset);
16377               break;
16378
16379             case REG:
16380               asm_fprintf (stream, "[%r, %s%r]",
16381                            REGNO (base), is_minus ? "-" : "",
16382                            REGNO (index));
16383               break;
16384
16385             case MULT:
16386             case ASHIFTRT:
16387             case LSHIFTRT:
16388             case ASHIFT:
16389             case ROTATERT:
16390               {
16391                 asm_fprintf (stream, "[%r, %s%r",
16392                              REGNO (base), is_minus ? "-" : "",
16393                              REGNO (XEXP (index, 0)));
16394                 arm_print_operand (stream, index, 'S');
16395                 fputs ("]", stream);
16396                 break;
16397               }
16398
16399             default:
16400               gcc_unreachable ();
16401             }
16402         }
16403       else if (GET_CODE (x) == PRE_INC || GET_CODE (x) == POST_INC
16404                || GET_CODE (x) == PRE_DEC || GET_CODE (x) == POST_DEC)
16405         {
16406           extern enum machine_mode output_memory_reference_mode;
16407
16408           gcc_assert (GET_CODE (XEXP (x, 0)) == REG);
16409
16410           if (GET_CODE (x) == PRE_DEC || GET_CODE (x) == PRE_INC)
16411             asm_fprintf (stream, "[%r, #%s%d]!",
16412                          REGNO (XEXP (x, 0)),
16413                          GET_CODE (x) == PRE_DEC ? "-" : "",
16414                          GET_MODE_SIZE (output_memory_reference_mode));
16415           else
16416             asm_fprintf (stream, "[%r], #%s%d",
16417                          REGNO (XEXP (x, 0)),
16418                          GET_CODE (x) == POST_DEC ? "-" : "",
16419                          GET_MODE_SIZE (output_memory_reference_mode));
16420         }
16421       else if (GET_CODE (x) == PRE_MODIFY)
16422         {
16423           asm_fprintf (stream, "[%r, ", REGNO (XEXP (x, 0)));
16424           if (GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
16425             asm_fprintf (stream, "#%wd]!",
16426                          INTVAL (XEXP (XEXP (x, 1), 1)));
16427           else
16428             asm_fprintf (stream, "%r]!",
16429                          REGNO (XEXP (XEXP (x, 1), 1)));
16430         }
16431       else if (GET_CODE (x) == POST_MODIFY)
16432         {
16433           asm_fprintf (stream, "[%r], ", REGNO (XEXP (x, 0)));
16434           if (GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
16435             asm_fprintf (stream, "#%wd",
16436                          INTVAL (XEXP (XEXP (x, 1), 1)));
16437           else
16438             asm_fprintf (stream, "%r",
16439                          REGNO (XEXP (XEXP (x, 1), 1)));
16440         }
16441       else output_addr_const (stream, x);
16442     }
16443   else
16444     {
16445       if (GET_CODE (x) == REG)
16446         asm_fprintf (stream, "[%r]", REGNO (x));
16447       else if (GET_CODE (x) == POST_INC)
16448         asm_fprintf (stream, "%r!", REGNO (XEXP (x, 0)));
16449       else if (GET_CODE (x) == PLUS)
16450         {
16451           gcc_assert (GET_CODE (XEXP (x, 0)) == REG);
16452           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
16453             asm_fprintf (stream, "[%r, #%wd]",
16454                          REGNO (XEXP (x, 0)),
16455                          INTVAL (XEXP (x, 1)));
16456           else
16457             asm_fprintf (stream, "[%r, %r]",
16458                          REGNO (XEXP (x, 0)),
16459                          REGNO (XEXP (x, 1)));
16460         }
16461       else
16462         output_addr_const (stream, x);
16463     }
16464 }
16465 \f
16466 /* Target hook for indicating whether a punctuation character for
16467    TARGET_PRINT_OPERAND is valid.  */
16468 static bool
16469 arm_print_operand_punct_valid_p (unsigned char code)
16470 {
16471   return (code == '@' || code == '|' || code == '.'
16472           || code == '(' || code == ')' || code == '#'
16473           || (TARGET_32BIT && (code == '?'))
16474           || (TARGET_THUMB2 && (code == '!'))
16475           || (TARGET_THUMB && (code == '_')));
16476 }
16477 \f
16478 /* Target hook for assembling integer objects.  The ARM version needs to
16479    handle word-sized values specially.  */
16480 static bool
16481 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
16482 {
16483   enum machine_mode mode;
16484
16485   if (size == UNITS_PER_WORD && aligned_p)
16486     {
16487       fputs ("\t.word\t", asm_out_file);
16488       output_addr_const (asm_out_file, x);
16489
16490       /* Mark symbols as position independent.  We only do this in the
16491          .text segment, not in the .data segment.  */
16492       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
16493           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
16494         {
16495           /* See legitimize_pic_address for an explanation of the
16496              TARGET_VXWORKS_RTP check.  */
16497           if (TARGET_VXWORKS_RTP
16498               || (GET_CODE (x) == SYMBOL_REF && !SYMBOL_REF_LOCAL_P (x)))
16499             fputs ("(GOT)", asm_out_file);
16500           else
16501             fputs ("(GOTOFF)", asm_out_file);
16502         }
16503       fputc ('\n', asm_out_file);
16504       return true;
16505     }
16506
16507   mode = GET_MODE (x);
16508
16509   if (arm_vector_mode_supported_p (mode))
16510     {
16511       int i, units;
16512
16513       gcc_assert (GET_CODE (x) == CONST_VECTOR);
16514
16515       units = CONST_VECTOR_NUNITS (x);
16516       size = GET_MODE_SIZE (GET_MODE_INNER (mode));
16517
16518       if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
16519         for (i = 0; i < units; i++)
16520           {
16521             rtx elt = CONST_VECTOR_ELT (x, i);
16522             assemble_integer
16523               (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
16524           }
16525       else
16526         for (i = 0; i < units; i++)
16527           {
16528             rtx elt = CONST_VECTOR_ELT (x, i);
16529             REAL_VALUE_TYPE rval;
16530
16531             REAL_VALUE_FROM_CONST_DOUBLE (rval, elt);
16532
16533             assemble_real
16534               (rval, GET_MODE_INNER (mode),
16535               i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT);
16536           }
16537
16538       return true;
16539     }
16540
16541   return default_assemble_integer (x, size, aligned_p);
16542 }
16543
16544 static void
16545 arm_elf_asm_cdtor (rtx symbol, int priority, bool is_ctor)
16546 {
16547   section *s;
16548
16549   if (!TARGET_AAPCS_BASED)
16550     {
16551       (is_ctor ? 
16552        default_named_section_asm_out_constructor 
16553        : default_named_section_asm_out_destructor) (symbol, priority);
16554       return;
16555     }
16556
16557   /* Put these in the .init_array section, using a special relocation.  */
16558   if (priority != DEFAULT_INIT_PRIORITY)
16559     {
16560       char buf[18];
16561       sprintf (buf, "%s.%.5u", 
16562                is_ctor ? ".init_array" : ".fini_array",
16563                priority);
16564       s = get_section (buf, SECTION_WRITE, NULL_TREE);
16565     }
16566   else if (is_ctor)
16567     s = ctors_section;
16568   else
16569     s = dtors_section;
16570
16571   switch_to_section (s);
16572   assemble_align (POINTER_SIZE);
16573   fputs ("\t.word\t", asm_out_file);
16574   output_addr_const (asm_out_file, symbol);
16575   fputs ("(target1)\n", asm_out_file);
16576 }
16577
16578 /* Add a function to the list of static constructors.  */
16579
16580 static void
16581 arm_elf_asm_constructor (rtx symbol, int priority)
16582 {
16583   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/true);
16584 }
16585
16586 /* Add a function to the list of static destructors.  */
16587
16588 static void
16589 arm_elf_asm_destructor (rtx symbol, int priority)
16590 {
16591   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/false);
16592 }
16593 \f
16594 /* A finite state machine takes care of noticing whether or not instructions
16595    can be conditionally executed, and thus decrease execution time and code
16596    size by deleting branch instructions.  The fsm is controlled by
16597    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
16598
16599 /* The state of the fsm controlling condition codes are:
16600    0: normal, do nothing special
16601    1: make ASM_OUTPUT_OPCODE not output this instruction
16602    2: make ASM_OUTPUT_OPCODE not output this instruction
16603    3: make instructions conditional
16604    4: make instructions conditional
16605
16606    State transitions (state->state by whom under condition):
16607    0 -> 1 final_prescan_insn if the `target' is a label
16608    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
16609    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
16610    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
16611    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
16612           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
16613    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
16614           (the target insn is arm_target_insn).
16615
16616    If the jump clobbers the conditions then we use states 2 and 4.
16617
16618    A similar thing can be done with conditional return insns.
16619
16620    XXX In case the `target' is an unconditional branch, this conditionalising
16621    of the instructions always reduces code size, but not always execution
16622    time.  But then, I want to reduce the code size to somewhere near what
16623    /bin/cc produces.  */
16624
16625 /* In addition to this, state is maintained for Thumb-2 COND_EXEC
16626    instructions.  When a COND_EXEC instruction is seen the subsequent
16627    instructions are scanned so that multiple conditional instructions can be
16628    combined into a single IT block.  arm_condexec_count and arm_condexec_mask
16629    specify the length and true/false mask for the IT block.  These will be
16630    decremented/zeroed by arm_asm_output_opcode as the insns are output.  */
16631
16632 /* Returns the index of the ARM condition code string in
16633    `arm_condition_codes'.  COMPARISON should be an rtx like
16634    `(eq (...) (...))'.  */
16635 static enum arm_cond_code
16636 get_arm_condition_code (rtx comparison)
16637 {
16638   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
16639   enum arm_cond_code code;
16640   enum rtx_code comp_code = GET_CODE (comparison);
16641
16642   if (GET_MODE_CLASS (mode) != MODE_CC)
16643     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
16644                            XEXP (comparison, 1));
16645
16646   switch (mode)
16647     {
16648     case CC_DNEmode: code = ARM_NE; goto dominance;
16649     case CC_DEQmode: code = ARM_EQ; goto dominance;
16650     case CC_DGEmode: code = ARM_GE; goto dominance;
16651     case CC_DGTmode: code = ARM_GT; goto dominance;
16652     case CC_DLEmode: code = ARM_LE; goto dominance;
16653     case CC_DLTmode: code = ARM_LT; goto dominance;
16654     case CC_DGEUmode: code = ARM_CS; goto dominance;
16655     case CC_DGTUmode: code = ARM_HI; goto dominance;
16656     case CC_DLEUmode: code = ARM_LS; goto dominance;
16657     case CC_DLTUmode: code = ARM_CC;
16658
16659     dominance:
16660       gcc_assert (comp_code == EQ || comp_code == NE);
16661
16662       if (comp_code == EQ)
16663         return ARM_INVERSE_CONDITION_CODE (code);
16664       return code;
16665
16666     case CC_NOOVmode:
16667       switch (comp_code)
16668         {
16669         case NE: return ARM_NE;
16670         case EQ: return ARM_EQ;
16671         case GE: return ARM_PL;
16672         case LT: return ARM_MI;
16673         default: gcc_unreachable ();
16674         }
16675
16676     case CC_Zmode:
16677       switch (comp_code)
16678         {
16679         case NE: return ARM_NE;
16680         case EQ: return ARM_EQ;
16681         default: gcc_unreachable ();
16682         }
16683
16684     case CC_Nmode:
16685       switch (comp_code)
16686         {
16687         case NE: return ARM_MI;
16688         case EQ: return ARM_PL;
16689         default: gcc_unreachable ();
16690         }
16691
16692     case CCFPEmode:
16693     case CCFPmode:
16694       /* These encodings assume that AC=1 in the FPA system control
16695          byte.  This allows us to handle all cases except UNEQ and
16696          LTGT.  */
16697       switch (comp_code)
16698         {
16699         case GE: return ARM_GE;
16700         case GT: return ARM_GT;
16701         case LE: return ARM_LS;
16702         case LT: return ARM_MI;
16703         case NE: return ARM_NE;
16704         case EQ: return ARM_EQ;
16705         case ORDERED: return ARM_VC;
16706         case UNORDERED: return ARM_VS;
16707         case UNLT: return ARM_LT;
16708         case UNLE: return ARM_LE;
16709         case UNGT: return ARM_HI;
16710         case UNGE: return ARM_PL;
16711           /* UNEQ and LTGT do not have a representation.  */
16712         case UNEQ: /* Fall through.  */
16713         case LTGT: /* Fall through.  */
16714         default: gcc_unreachable ();
16715         }
16716
16717     case CC_SWPmode:
16718       switch (comp_code)
16719         {
16720         case NE: return ARM_NE;
16721         case EQ: return ARM_EQ;
16722         case GE: return ARM_LE;
16723         case GT: return ARM_LT;
16724         case LE: return ARM_GE;
16725         case LT: return ARM_GT;
16726         case GEU: return ARM_LS;
16727         case GTU: return ARM_CC;
16728         case LEU: return ARM_CS;
16729         case LTU: return ARM_HI;
16730         default: gcc_unreachable ();
16731         }
16732
16733     case CC_Cmode:
16734       switch (comp_code)
16735         {
16736         case LTU: return ARM_CS;
16737         case GEU: return ARM_CC;
16738         default: gcc_unreachable ();
16739         }
16740
16741     case CC_CZmode:
16742       switch (comp_code)
16743         {
16744         case NE: return ARM_NE;
16745         case EQ: return ARM_EQ;
16746         case GEU: return ARM_CS;
16747         case GTU: return ARM_HI;
16748         case LEU: return ARM_LS;
16749         case LTU: return ARM_CC;
16750         default: gcc_unreachable ();
16751         }
16752
16753     case CC_NCVmode:
16754       switch (comp_code)
16755         {
16756         case GE: return ARM_GE;
16757         case LT: return ARM_LT;
16758         case GEU: return ARM_CS;
16759         case LTU: return ARM_CC;
16760         default: gcc_unreachable ();
16761         }
16762
16763     case CCmode:
16764       switch (comp_code)
16765         {
16766         case NE: return ARM_NE;
16767         case EQ: return ARM_EQ;
16768         case GE: return ARM_GE;
16769         case GT: return ARM_GT;
16770         case LE: return ARM_LE;
16771         case LT: return ARM_LT;
16772         case GEU: return ARM_CS;
16773         case GTU: return ARM_HI;
16774         case LEU: return ARM_LS;
16775         case LTU: return ARM_CC;
16776         default: gcc_unreachable ();
16777         }
16778
16779     default: gcc_unreachable ();
16780     }
16781 }
16782
16783 /* Tell arm_asm_output_opcode to output IT blocks for conditionally executed
16784    instructions.  */
16785 void
16786 thumb2_final_prescan_insn (rtx insn)
16787 {
16788   rtx first_insn = insn;
16789   rtx body = PATTERN (insn);
16790   rtx predicate;
16791   enum arm_cond_code code;
16792   int n;
16793   int mask;
16794
16795   /* Remove the previous insn from the count of insns to be output.  */
16796   if (arm_condexec_count)
16797       arm_condexec_count--;
16798
16799   /* Nothing to do if we are already inside a conditional block.  */
16800   if (arm_condexec_count)
16801     return;
16802
16803   if (GET_CODE (body) != COND_EXEC)
16804     return;
16805
16806   /* Conditional jumps are implemented directly.  */
16807   if (GET_CODE (insn) == JUMP_INSN)
16808     return;
16809
16810   predicate = COND_EXEC_TEST (body);
16811   arm_current_cc = get_arm_condition_code (predicate);
16812
16813   n = get_attr_ce_count (insn);
16814   arm_condexec_count = 1;
16815   arm_condexec_mask = (1 << n) - 1;
16816   arm_condexec_masklen = n;
16817   /* See if subsequent instructions can be combined into the same block.  */
16818   for (;;)
16819     {
16820       insn = next_nonnote_insn (insn);
16821
16822       /* Jumping into the middle of an IT block is illegal, so a label or
16823          barrier terminates the block.  */
16824       if (GET_CODE (insn) != INSN && GET_CODE(insn) != JUMP_INSN)
16825         break;
16826
16827       body = PATTERN (insn);
16828       /* USE and CLOBBER aren't really insns, so just skip them.  */
16829       if (GET_CODE (body) == USE
16830           || GET_CODE (body) == CLOBBER)
16831         continue;
16832
16833       /* ??? Recognize conditional jumps, and combine them with IT blocks.  */
16834       if (GET_CODE (body) != COND_EXEC)
16835         break;
16836       /* Allow up to 4 conditionally executed instructions in a block.  */
16837       n = get_attr_ce_count (insn);
16838       if (arm_condexec_masklen + n > 4)
16839         break;
16840
16841       predicate = COND_EXEC_TEST (body);
16842       code = get_arm_condition_code (predicate);
16843       mask = (1 << n) - 1;
16844       if (arm_current_cc == code)
16845         arm_condexec_mask |= (mask << arm_condexec_masklen);
16846       else if (arm_current_cc != ARM_INVERSE_CONDITION_CODE(code))
16847         break;
16848
16849       arm_condexec_count++;
16850       arm_condexec_masklen += n;
16851
16852       /* A jump must be the last instruction in a conditional block.  */
16853       if (GET_CODE(insn) == JUMP_INSN)
16854         break;
16855     }
16856   /* Restore recog_data (getting the attributes of other insns can
16857      destroy this array, but final.c assumes that it remains intact
16858      across this call).  */
16859   extract_constrain_insn_cached (first_insn);
16860 }
16861
16862 void
16863 arm_final_prescan_insn (rtx insn)
16864 {
16865   /* BODY will hold the body of INSN.  */
16866   rtx body = PATTERN (insn);
16867
16868   /* This will be 1 if trying to repeat the trick, and things need to be
16869      reversed if it appears to fail.  */
16870   int reverse = 0;
16871
16872   /* If we start with a return insn, we only succeed if we find another one.  */
16873   int seeking_return = 0;
16874
16875   /* START_INSN will hold the insn from where we start looking.  This is the
16876      first insn after the following code_label if REVERSE is true.  */
16877   rtx start_insn = insn;
16878
16879   /* If in state 4, check if the target branch is reached, in order to
16880      change back to state 0.  */
16881   if (arm_ccfsm_state == 4)
16882     {
16883       if (insn == arm_target_insn)
16884         {
16885           arm_target_insn = NULL;
16886           arm_ccfsm_state = 0;
16887         }
16888       return;
16889     }
16890
16891   /* If in state 3, it is possible to repeat the trick, if this insn is an
16892      unconditional branch to a label, and immediately following this branch
16893      is the previous target label which is only used once, and the label this
16894      branch jumps to is not too far off.  */
16895   if (arm_ccfsm_state == 3)
16896     {
16897       if (simplejump_p (insn))
16898         {
16899           start_insn = next_nonnote_insn (start_insn);
16900           if (GET_CODE (start_insn) == BARRIER)
16901             {
16902               /* XXX Isn't this always a barrier?  */
16903               start_insn = next_nonnote_insn (start_insn);
16904             }
16905           if (GET_CODE (start_insn) == CODE_LABEL
16906               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
16907               && LABEL_NUSES (start_insn) == 1)
16908             reverse = TRUE;
16909           else
16910             return;
16911         }
16912       else if (GET_CODE (body) == RETURN)
16913         {
16914           start_insn = next_nonnote_insn (start_insn);
16915           if (GET_CODE (start_insn) == BARRIER)
16916             start_insn = next_nonnote_insn (start_insn);
16917           if (GET_CODE (start_insn) == CODE_LABEL
16918               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
16919               && LABEL_NUSES (start_insn) == 1)
16920             {
16921               reverse = TRUE;
16922               seeking_return = 1;
16923             }
16924           else
16925             return;
16926         }
16927       else
16928         return;
16929     }
16930
16931   gcc_assert (!arm_ccfsm_state || reverse);
16932   if (GET_CODE (insn) != JUMP_INSN)
16933     return;
16934
16935   /* This jump might be paralleled with a clobber of the condition codes
16936      the jump should always come first */
16937   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
16938     body = XVECEXP (body, 0, 0);
16939
16940   if (reverse
16941       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
16942           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
16943     {
16944       int insns_skipped;
16945       int fail = FALSE, succeed = FALSE;
16946       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
16947       int then_not_else = TRUE;
16948       rtx this_insn = start_insn, label = 0;
16949
16950       /* Register the insn jumped to.  */
16951       if (reverse)
16952         {
16953           if (!seeking_return)
16954             label = XEXP (SET_SRC (body), 0);
16955         }
16956       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
16957         label = XEXP (XEXP (SET_SRC (body), 1), 0);
16958       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
16959         {
16960           label = XEXP (XEXP (SET_SRC (body), 2), 0);
16961           then_not_else = FALSE;
16962         }
16963       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
16964         seeking_return = 1;
16965       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
16966         {
16967           seeking_return = 1;
16968           then_not_else = FALSE;
16969         }
16970       else
16971         gcc_unreachable ();
16972
16973       /* See how many insns this branch skips, and what kind of insns.  If all
16974          insns are okay, and the label or unconditional branch to the same
16975          label is not too far away, succeed.  */
16976       for (insns_skipped = 0;
16977            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
16978         {
16979           rtx scanbody;
16980
16981           this_insn = next_nonnote_insn (this_insn);
16982           if (!this_insn)
16983             break;
16984
16985           switch (GET_CODE (this_insn))
16986             {
16987             case CODE_LABEL:
16988               /* Succeed if it is the target label, otherwise fail since
16989                  control falls in from somewhere else.  */
16990               if (this_insn == label)
16991                 {
16992                   arm_ccfsm_state = 1;
16993                   succeed = TRUE;
16994                 }
16995               else
16996                 fail = TRUE;
16997               break;
16998
16999             case BARRIER:
17000               /* Succeed if the following insn is the target label.
17001                  Otherwise fail.
17002                  If return insns are used then the last insn in a function
17003                  will be a barrier.  */
17004               this_insn = next_nonnote_insn (this_insn);
17005               if (this_insn && this_insn == label)
17006                 {
17007                   arm_ccfsm_state = 1;
17008                   succeed = TRUE;
17009                 }
17010               else
17011                 fail = TRUE;
17012               break;
17013
17014             case CALL_INSN:
17015               /* The AAPCS says that conditional calls should not be
17016                  used since they make interworking inefficient (the
17017                  linker can't transform BL<cond> into BLX).  That's
17018                  only a problem if the machine has BLX.  */
17019               if (arm_arch5)
17020                 {
17021                   fail = TRUE;
17022                   break;
17023                 }
17024
17025               /* Succeed if the following insn is the target label, or
17026                  if the following two insns are a barrier and the
17027                  target label.  */
17028               this_insn = next_nonnote_insn (this_insn);
17029               if (this_insn && GET_CODE (this_insn) == BARRIER)
17030                 this_insn = next_nonnote_insn (this_insn);
17031
17032               if (this_insn && this_insn == label
17033                   && insns_skipped < max_insns_skipped)
17034                 {
17035                   arm_ccfsm_state = 1;
17036                   succeed = TRUE;
17037                 }
17038               else
17039                 fail = TRUE;
17040               break;
17041
17042             case JUMP_INSN:
17043               /* If this is an unconditional branch to the same label, succeed.
17044                  If it is to another label, do nothing.  If it is conditional,
17045                  fail.  */
17046               /* XXX Probably, the tests for SET and the PC are
17047                  unnecessary.  */
17048
17049               scanbody = PATTERN (this_insn);
17050               if (GET_CODE (scanbody) == SET
17051                   && GET_CODE (SET_DEST (scanbody)) == PC)
17052                 {
17053                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
17054                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
17055                     {
17056                       arm_ccfsm_state = 2;
17057                       succeed = TRUE;
17058                     }
17059                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
17060                     fail = TRUE;
17061                 }
17062               /* Fail if a conditional return is undesirable (e.g. on a
17063                  StrongARM), but still allow this if optimizing for size.  */
17064               else if (GET_CODE (scanbody) == RETURN
17065                        && !use_return_insn (TRUE, NULL)
17066                        && !optimize_size)
17067                 fail = TRUE;
17068               else if (GET_CODE (scanbody) == RETURN
17069                        && seeking_return)
17070                 {
17071                   arm_ccfsm_state = 2;
17072                   succeed = TRUE;
17073                 }
17074               else if (GET_CODE (scanbody) == PARALLEL)
17075                 {
17076                   switch (get_attr_conds (this_insn))
17077                     {
17078                     case CONDS_NOCOND:
17079                       break;
17080                     default:
17081                       fail = TRUE;
17082                       break;
17083                     }
17084                 }
17085               else
17086                 fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
17087
17088               break;
17089
17090             case INSN:
17091               /* Instructions using or affecting the condition codes make it
17092                  fail.  */
17093               scanbody = PATTERN (this_insn);
17094               if (!(GET_CODE (scanbody) == SET
17095                     || GET_CODE (scanbody) == PARALLEL)
17096                   || get_attr_conds (this_insn) != CONDS_NOCOND)
17097                 fail = TRUE;
17098
17099               /* A conditional cirrus instruction must be followed by
17100                  a non Cirrus instruction.  However, since we
17101                  conditionalize instructions in this function and by
17102                  the time we get here we can't add instructions
17103                  (nops), because shorten_branches() has already been
17104                  called, we will disable conditionalizing Cirrus
17105                  instructions to be safe.  */
17106               if (GET_CODE (scanbody) != USE
17107                   && GET_CODE (scanbody) != CLOBBER
17108                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
17109                 fail = TRUE;
17110               break;
17111
17112             default:
17113               break;
17114             }
17115         }
17116       if (succeed)
17117         {
17118           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
17119             arm_target_label = CODE_LABEL_NUMBER (label);
17120           else
17121             {
17122               gcc_assert (seeking_return || arm_ccfsm_state == 2);
17123
17124               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
17125                 {
17126                   this_insn = next_nonnote_insn (this_insn);
17127                   gcc_assert (!this_insn
17128                               || (GET_CODE (this_insn) != BARRIER
17129                                   && GET_CODE (this_insn) != CODE_LABEL));
17130                 }
17131               if (!this_insn)
17132                 {
17133                   /* Oh, dear! we ran off the end.. give up.  */
17134                   extract_constrain_insn_cached (insn);
17135                   arm_ccfsm_state = 0;
17136                   arm_target_insn = NULL;
17137                   return;
17138                 }
17139               arm_target_insn = this_insn;
17140             }
17141
17142           /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
17143              what it was.  */
17144           if (!reverse)
17145             arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body), 0));
17146
17147           if (reverse || then_not_else)
17148             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
17149         }
17150
17151       /* Restore recog_data (getting the attributes of other insns can
17152          destroy this array, but final.c assumes that it remains intact
17153          across this call.  */
17154       extract_constrain_insn_cached (insn);
17155     }
17156 }
17157
17158 /* Output IT instructions.  */
17159 void
17160 thumb2_asm_output_opcode (FILE * stream)
17161 {
17162   char buff[5];
17163   int n;
17164
17165   if (arm_condexec_mask)
17166     {
17167       for (n = 0; n < arm_condexec_masklen; n++)
17168         buff[n] = (arm_condexec_mask & (1 << n)) ? 't' : 'e';
17169       buff[n] = 0;
17170       asm_fprintf(stream, "i%s\t%s\n\t", buff,
17171                   arm_condition_codes[arm_current_cc]);
17172       arm_condexec_mask = 0;
17173     }
17174 }
17175
17176 /* Returns true if REGNO is a valid register
17177    for holding a quantity of type MODE.  */
17178 int
17179 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
17180 {
17181   if (GET_MODE_CLASS (mode) == MODE_CC)
17182     return (regno == CC_REGNUM
17183             || (TARGET_HARD_FLOAT && TARGET_VFP
17184                 && regno == VFPCC_REGNUM));
17185
17186   if (TARGET_THUMB1)
17187     /* For the Thumb we only allow values bigger than SImode in
17188        registers 0 - 6, so that there is always a second low
17189        register available to hold the upper part of the value.
17190        We probably we ought to ensure that the register is the
17191        start of an even numbered register pair.  */
17192     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
17193
17194   if (TARGET_HARD_FLOAT && TARGET_MAVERICK
17195       && IS_CIRRUS_REGNUM (regno))
17196     /* We have outlawed SI values in Cirrus registers because they
17197        reside in the lower 32 bits, but SF values reside in the
17198        upper 32 bits.  This causes gcc all sorts of grief.  We can't
17199        even split the registers into pairs because Cirrus SI values
17200        get sign extended to 64bits-- aldyh.  */
17201     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
17202
17203   if (TARGET_HARD_FLOAT && TARGET_VFP
17204       && IS_VFP_REGNUM (regno))
17205     {
17206       if (mode == SFmode || mode == SImode)
17207         return VFP_REGNO_OK_FOR_SINGLE (regno);
17208
17209       if (mode == DFmode)
17210         return VFP_REGNO_OK_FOR_DOUBLE (regno);
17211
17212       /* VFP registers can hold HFmode values, but there is no point in
17213          putting them there unless we have hardware conversion insns. */
17214       if (mode == HFmode)
17215         return TARGET_FP16 && VFP_REGNO_OK_FOR_SINGLE (regno);
17216
17217       if (TARGET_NEON)
17218         return (VALID_NEON_DREG_MODE (mode) && VFP_REGNO_OK_FOR_DOUBLE (regno))
17219                || (VALID_NEON_QREG_MODE (mode)
17220                    && NEON_REGNO_OK_FOR_QUAD (regno))
17221                || (mode == TImode && NEON_REGNO_OK_FOR_NREGS (regno, 2))
17222                || (mode == EImode && NEON_REGNO_OK_FOR_NREGS (regno, 3))
17223                || (mode == OImode && NEON_REGNO_OK_FOR_NREGS (regno, 4))
17224                || (mode == CImode && NEON_REGNO_OK_FOR_NREGS (regno, 6))
17225                || (mode == XImode && NEON_REGNO_OK_FOR_NREGS (regno, 8));
17226
17227       return FALSE;
17228     }
17229
17230   if (TARGET_REALLY_IWMMXT)
17231     {
17232       if (IS_IWMMXT_GR_REGNUM (regno))
17233         return mode == SImode;
17234
17235       if (IS_IWMMXT_REGNUM (regno))
17236         return VALID_IWMMXT_REG_MODE (mode);
17237     }
17238   
17239   /* We allow almost any value to be stored in the general registers.
17240      Restrict doubleword quantities to even register pairs so that we can
17241      use ldrd.  Do not allow very large Neon structure opaque modes in
17242      general registers; they would use too many.  */
17243   if (regno <= LAST_ARM_REGNUM)
17244     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0)
17245       && ARM_NUM_REGS (mode) <= 4;
17246
17247   if (regno == FRAME_POINTER_REGNUM
17248       || regno == ARG_POINTER_REGNUM)
17249     /* We only allow integers in the fake hard registers.  */
17250     return GET_MODE_CLASS (mode) == MODE_INT;
17251
17252   /* The only registers left are the FPA registers
17253      which we only allow to hold FP values.  */
17254   return (TARGET_HARD_FLOAT && TARGET_FPA
17255           && GET_MODE_CLASS (mode) == MODE_FLOAT
17256           && regno >= FIRST_FPA_REGNUM
17257           && regno <= LAST_FPA_REGNUM);
17258 }
17259
17260 /* For efficiency and historical reasons LO_REGS, HI_REGS and CC_REGS are
17261    not used in arm mode.  */
17262
17263 enum reg_class
17264 arm_regno_class (int regno)
17265 {
17266   if (TARGET_THUMB1)
17267     {
17268       if (regno == STACK_POINTER_REGNUM)
17269         return STACK_REG;
17270       if (regno == CC_REGNUM)
17271         return CC_REG;
17272       if (regno < 8)
17273         return LO_REGS;
17274       return HI_REGS;
17275     }
17276
17277   if (TARGET_THUMB2 && regno < 8)
17278     return LO_REGS;
17279
17280   if (   regno <= LAST_ARM_REGNUM
17281       || regno == FRAME_POINTER_REGNUM
17282       || regno == ARG_POINTER_REGNUM)
17283     return TARGET_THUMB2 ? HI_REGS : GENERAL_REGS;
17284
17285   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
17286     return TARGET_THUMB2 ? CC_REG : NO_REGS;
17287
17288   if (IS_CIRRUS_REGNUM (regno))
17289     return CIRRUS_REGS;
17290
17291   if (IS_VFP_REGNUM (regno))
17292     {
17293       if (regno <= D7_VFP_REGNUM)
17294         return VFP_D0_D7_REGS;
17295       else if (regno <= LAST_LO_VFP_REGNUM)
17296         return VFP_LO_REGS;
17297       else
17298         return VFP_HI_REGS;
17299     }
17300
17301   if (IS_IWMMXT_REGNUM (regno))
17302     return IWMMXT_REGS;
17303
17304   if (IS_IWMMXT_GR_REGNUM (regno))
17305     return IWMMXT_GR_REGS;
17306
17307   return FPA_REGS;
17308 }
17309
17310 /* Handle a special case when computing the offset
17311    of an argument from the frame pointer.  */
17312 int
17313 arm_debugger_arg_offset (int value, rtx addr)
17314 {
17315   rtx insn;
17316
17317   /* We are only interested if dbxout_parms() failed to compute the offset.  */
17318   if (value != 0)
17319     return 0;
17320
17321   /* We can only cope with the case where the address is held in a register.  */
17322   if (GET_CODE (addr) != REG)
17323     return 0;
17324
17325   /* If we are using the frame pointer to point at the argument, then
17326      an offset of 0 is correct.  */
17327   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
17328     return 0;
17329
17330   /* If we are using the stack pointer to point at the
17331      argument, then an offset of 0 is correct.  */
17332   /* ??? Check this is consistent with thumb2 frame layout.  */
17333   if ((TARGET_THUMB || !frame_pointer_needed)
17334       && REGNO (addr) == SP_REGNUM)
17335     return 0;
17336
17337   /* Oh dear.  The argument is pointed to by a register rather
17338      than being held in a register, or being stored at a known
17339      offset from the frame pointer.  Since GDB only understands
17340      those two kinds of argument we must translate the address
17341      held in the register into an offset from the frame pointer.
17342      We do this by searching through the insns for the function
17343      looking to see where this register gets its value.  If the
17344      register is initialized from the frame pointer plus an offset
17345      then we are in luck and we can continue, otherwise we give up.
17346
17347      This code is exercised by producing debugging information
17348      for a function with arguments like this:
17349
17350            double func (double a, double b, int c, double d) {return d;}
17351
17352      Without this code the stab for parameter 'd' will be set to
17353      an offset of 0 from the frame pointer, rather than 8.  */
17354
17355   /* The if() statement says:
17356
17357      If the insn is a normal instruction
17358      and if the insn is setting the value in a register
17359      and if the register being set is the register holding the address of the argument
17360      and if the address is computing by an addition
17361      that involves adding to a register
17362      which is the frame pointer
17363      a constant integer
17364
17365      then...  */
17366
17367   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
17368     {
17369       if (   GET_CODE (insn) == INSN
17370           && GET_CODE (PATTERN (insn)) == SET
17371           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
17372           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
17373           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
17374           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
17375           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
17376              )
17377         {
17378           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
17379
17380           break;
17381         }
17382     }
17383
17384   if (value == 0)
17385     {
17386       debug_rtx (addr);
17387       warning (0, "unable to compute real location of stacked parameter");
17388       value = 8; /* XXX magic hack */
17389     }
17390
17391   return value;
17392 }
17393 \f
17394 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
17395   do                                                                    \
17396     {                                                                   \
17397       if ((MASK) & insn_flags)                                          \
17398         add_builtin_function ((NAME), (TYPE), (CODE),                   \
17399                              BUILT_IN_MD, NULL, NULL_TREE);             \
17400     }                                                                   \
17401   while (0)
17402
17403 struct builtin_description
17404 {
17405   const unsigned int       mask;
17406   const enum insn_code     icode;
17407   const char * const       name;
17408   const enum arm_builtins  code;
17409   const enum rtx_code      comparison;
17410   const unsigned int       flag;
17411 };
17412
17413 static const struct builtin_description bdesc_2arg[] =
17414 {
17415 #define IWMMXT_BUILTIN(code, string, builtin) \
17416   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
17417     ARM_BUILTIN_##builtin, UNKNOWN, 0 },
17418
17419   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
17420   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
17421   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
17422   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
17423   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
17424   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
17425   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
17426   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
17427   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
17428   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
17429   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
17430   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
17431   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
17432   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
17433   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
17434   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
17435   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
17436   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
17437   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
17438   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
17439   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
17440   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
17441   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
17442   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
17443   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
17444   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
17445   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
17446   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
17447   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
17448   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
17449   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
17450   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
17451   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
17452   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
17453   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
17454   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
17455   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
17456   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
17457   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
17458   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
17459   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
17460   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
17461   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
17462   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
17463   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
17464   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
17465   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
17466   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
17467   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
17468   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
17469   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
17470   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
17471   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
17472   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
17473   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
17474   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
17475   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
17476   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
17477
17478 #define IWMMXT_BUILTIN2(code, builtin) \
17479   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, UNKNOWN, 0 },
17480
17481   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
17482   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
17483   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
17484   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
17485   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
17486   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
17487   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
17488   IWMMXT_BUILTIN2 (ashlv4hi3_iwmmxt, WSLLHI)
17489   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
17490   IWMMXT_BUILTIN2 (ashlv2si3_iwmmxt, WSLLWI)
17491   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
17492   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
17493   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
17494   IWMMXT_BUILTIN2 (lshrv4hi3_iwmmxt, WSRLHI)
17495   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
17496   IWMMXT_BUILTIN2 (lshrv2si3_iwmmxt, WSRLWI)
17497   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
17498   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
17499   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
17500   IWMMXT_BUILTIN2 (ashrv4hi3_iwmmxt, WSRAHI)
17501   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
17502   IWMMXT_BUILTIN2 (ashrv2si3_iwmmxt, WSRAWI)
17503   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
17504   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
17505   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
17506   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
17507   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
17508   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
17509   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
17510   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
17511   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
17512   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
17513 };
17514
17515 static const struct builtin_description bdesc_1arg[] =
17516 {
17517   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
17518   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
17519   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
17520   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
17521   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
17522   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
17523   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
17524   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
17525   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
17526   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
17527   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
17528   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
17529   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
17530   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
17531   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
17532   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
17533   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
17534   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
17535 };
17536
17537 /* Set up all the iWMMXt builtins.  This is
17538    not called if TARGET_IWMMXT is zero.  */
17539
17540 static void
17541 arm_init_iwmmxt_builtins (void)
17542 {
17543   const struct builtin_description * d;
17544   size_t i;
17545   tree endlink = void_list_node;
17546
17547   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
17548   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
17549   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
17550
17551   tree int_ftype_int
17552     = build_function_type (integer_type_node,
17553                            tree_cons (NULL_TREE, integer_type_node, endlink));
17554   tree v8qi_ftype_v8qi_v8qi_int
17555     = build_function_type (V8QI_type_node,
17556                            tree_cons (NULL_TREE, V8QI_type_node,
17557                                       tree_cons (NULL_TREE, V8QI_type_node,
17558                                                  tree_cons (NULL_TREE,
17559                                                             integer_type_node,
17560                                                             endlink))));
17561   tree v4hi_ftype_v4hi_int
17562     = build_function_type (V4HI_type_node,
17563                            tree_cons (NULL_TREE, V4HI_type_node,
17564                                       tree_cons (NULL_TREE, integer_type_node,
17565                                                  endlink)));
17566   tree v2si_ftype_v2si_int
17567     = build_function_type (V2SI_type_node,
17568                            tree_cons (NULL_TREE, V2SI_type_node,
17569                                       tree_cons (NULL_TREE, integer_type_node,
17570                                                  endlink)));
17571   tree v2si_ftype_di_di
17572     = build_function_type (V2SI_type_node,
17573                            tree_cons (NULL_TREE, long_long_integer_type_node,
17574                                       tree_cons (NULL_TREE, long_long_integer_type_node,
17575                                                  endlink)));
17576   tree di_ftype_di_int
17577     = build_function_type (long_long_integer_type_node,
17578                            tree_cons (NULL_TREE, long_long_integer_type_node,
17579                                       tree_cons (NULL_TREE, integer_type_node,
17580                                                  endlink)));
17581   tree di_ftype_di_int_int
17582     = build_function_type (long_long_integer_type_node,
17583                            tree_cons (NULL_TREE, long_long_integer_type_node,
17584                                       tree_cons (NULL_TREE, integer_type_node,
17585                                                  tree_cons (NULL_TREE,
17586                                                             integer_type_node,
17587                                                             endlink))));
17588   tree int_ftype_v8qi
17589     = build_function_type (integer_type_node,
17590                            tree_cons (NULL_TREE, V8QI_type_node,
17591                                       endlink));
17592   tree int_ftype_v4hi
17593     = build_function_type (integer_type_node,
17594                            tree_cons (NULL_TREE, V4HI_type_node,
17595                                       endlink));
17596   tree int_ftype_v2si
17597     = build_function_type (integer_type_node,
17598                            tree_cons (NULL_TREE, V2SI_type_node,
17599                                       endlink));
17600   tree int_ftype_v8qi_int
17601     = build_function_type (integer_type_node,
17602                            tree_cons (NULL_TREE, V8QI_type_node,
17603                                       tree_cons (NULL_TREE, integer_type_node,
17604                                                  endlink)));
17605   tree int_ftype_v4hi_int
17606     = build_function_type (integer_type_node,
17607                            tree_cons (NULL_TREE, V4HI_type_node,
17608                                       tree_cons (NULL_TREE, integer_type_node,
17609                                                  endlink)));
17610   tree int_ftype_v2si_int
17611     = build_function_type (integer_type_node,
17612                            tree_cons (NULL_TREE, V2SI_type_node,
17613                                       tree_cons (NULL_TREE, integer_type_node,
17614                                                  endlink)));
17615   tree v8qi_ftype_v8qi_int_int
17616     = build_function_type (V8QI_type_node,
17617                            tree_cons (NULL_TREE, V8QI_type_node,
17618                                       tree_cons (NULL_TREE, integer_type_node,
17619                                                  tree_cons (NULL_TREE,
17620                                                             integer_type_node,
17621                                                             endlink))));
17622   tree v4hi_ftype_v4hi_int_int
17623     = build_function_type (V4HI_type_node,
17624                            tree_cons (NULL_TREE, V4HI_type_node,
17625                                       tree_cons (NULL_TREE, integer_type_node,
17626                                                  tree_cons (NULL_TREE,
17627                                                             integer_type_node,
17628                                                             endlink))));
17629   tree v2si_ftype_v2si_int_int
17630     = build_function_type (V2SI_type_node,
17631                            tree_cons (NULL_TREE, V2SI_type_node,
17632                                       tree_cons (NULL_TREE, integer_type_node,
17633                                                  tree_cons (NULL_TREE,
17634                                                             integer_type_node,
17635                                                             endlink))));
17636   /* Miscellaneous.  */
17637   tree v8qi_ftype_v4hi_v4hi
17638     = build_function_type (V8QI_type_node,
17639                            tree_cons (NULL_TREE, V4HI_type_node,
17640                                       tree_cons (NULL_TREE, V4HI_type_node,
17641                                                  endlink)));
17642   tree v4hi_ftype_v2si_v2si
17643     = build_function_type (V4HI_type_node,
17644                            tree_cons (NULL_TREE, V2SI_type_node,
17645                                       tree_cons (NULL_TREE, V2SI_type_node,
17646                                                  endlink)));
17647   tree v2si_ftype_v4hi_v4hi
17648     = build_function_type (V2SI_type_node,
17649                            tree_cons (NULL_TREE, V4HI_type_node,
17650                                       tree_cons (NULL_TREE, V4HI_type_node,
17651                                                  endlink)));
17652   tree v2si_ftype_v8qi_v8qi
17653     = build_function_type (V2SI_type_node,
17654                            tree_cons (NULL_TREE, V8QI_type_node,
17655                                       tree_cons (NULL_TREE, V8QI_type_node,
17656                                                  endlink)));
17657   tree v4hi_ftype_v4hi_di
17658     = build_function_type (V4HI_type_node,
17659                            tree_cons (NULL_TREE, V4HI_type_node,
17660                                       tree_cons (NULL_TREE,
17661                                                  long_long_integer_type_node,
17662                                                  endlink)));
17663   tree v2si_ftype_v2si_di
17664     = build_function_type (V2SI_type_node,
17665                            tree_cons (NULL_TREE, V2SI_type_node,
17666                                       tree_cons (NULL_TREE,
17667                                                  long_long_integer_type_node,
17668                                                  endlink)));
17669   tree void_ftype_int_int
17670     = build_function_type (void_type_node,
17671                            tree_cons (NULL_TREE, integer_type_node,
17672                                       tree_cons (NULL_TREE, integer_type_node,
17673                                                  endlink)));
17674   tree di_ftype_void
17675     = build_function_type (long_long_unsigned_type_node, endlink);
17676   tree di_ftype_v8qi
17677     = build_function_type (long_long_integer_type_node,
17678                            tree_cons (NULL_TREE, V8QI_type_node,
17679                                       endlink));
17680   tree di_ftype_v4hi
17681     = build_function_type (long_long_integer_type_node,
17682                            tree_cons (NULL_TREE, V4HI_type_node,
17683                                       endlink));
17684   tree di_ftype_v2si
17685     = build_function_type (long_long_integer_type_node,
17686                            tree_cons (NULL_TREE, V2SI_type_node,
17687                                       endlink));
17688   tree v2si_ftype_v4hi
17689     = build_function_type (V2SI_type_node,
17690                            tree_cons (NULL_TREE, V4HI_type_node,
17691                                       endlink));
17692   tree v4hi_ftype_v8qi
17693     = build_function_type (V4HI_type_node,
17694                            tree_cons (NULL_TREE, V8QI_type_node,
17695                                       endlink));
17696
17697   tree di_ftype_di_v4hi_v4hi
17698     = build_function_type (long_long_unsigned_type_node,
17699                            tree_cons (NULL_TREE,
17700                                       long_long_unsigned_type_node,
17701                                       tree_cons (NULL_TREE, V4HI_type_node,
17702                                                  tree_cons (NULL_TREE,
17703                                                             V4HI_type_node,
17704                                                             endlink))));
17705
17706   tree di_ftype_v4hi_v4hi
17707     = build_function_type (long_long_unsigned_type_node,
17708                            tree_cons (NULL_TREE, V4HI_type_node,
17709                                       tree_cons (NULL_TREE, V4HI_type_node,
17710                                                  endlink)));
17711
17712   /* Normal vector binops.  */
17713   tree v8qi_ftype_v8qi_v8qi
17714     = build_function_type (V8QI_type_node,
17715                            tree_cons (NULL_TREE, V8QI_type_node,
17716                                       tree_cons (NULL_TREE, V8QI_type_node,
17717                                                  endlink)));
17718   tree v4hi_ftype_v4hi_v4hi
17719     = build_function_type (V4HI_type_node,
17720                            tree_cons (NULL_TREE, V4HI_type_node,
17721                                       tree_cons (NULL_TREE, V4HI_type_node,
17722                                                  endlink)));
17723   tree v2si_ftype_v2si_v2si
17724     = build_function_type (V2SI_type_node,
17725                            tree_cons (NULL_TREE, V2SI_type_node,
17726                                       tree_cons (NULL_TREE, V2SI_type_node,
17727                                                  endlink)));
17728   tree di_ftype_di_di
17729     = build_function_type (long_long_unsigned_type_node,
17730                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
17731                                       tree_cons (NULL_TREE,
17732                                                  long_long_unsigned_type_node,
17733                                                  endlink)));
17734
17735   /* Add all builtins that are more or less simple operations on two
17736      operands.  */
17737   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
17738     {
17739       /* Use one of the operands; the target can have a different mode for
17740          mask-generating compares.  */
17741       enum machine_mode mode;
17742       tree type;
17743
17744       if (d->name == 0)
17745         continue;
17746
17747       mode = insn_data[d->icode].operand[1].mode;
17748
17749       switch (mode)
17750         {
17751         case V8QImode:
17752           type = v8qi_ftype_v8qi_v8qi;
17753           break;
17754         case V4HImode:
17755           type = v4hi_ftype_v4hi_v4hi;
17756           break;
17757         case V2SImode:
17758           type = v2si_ftype_v2si_v2si;
17759           break;
17760         case DImode:
17761           type = di_ftype_di_di;
17762           break;
17763
17764         default:
17765           gcc_unreachable ();
17766         }
17767
17768       def_mbuiltin (d->mask, d->name, type, d->code);
17769     }
17770
17771   /* Add the remaining MMX insns with somewhat more complicated types.  */
17772   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
17773   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
17774   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
17775
17776   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
17777   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
17778   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
17779   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
17780   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
17781   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
17782
17783   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
17784   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
17785   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
17786   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
17787   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
17788   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
17789
17790   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
17791   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
17792   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
17793   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
17794   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
17795   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
17796
17797   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
17798   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
17799   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
17800   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
17801   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
17802   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
17803
17804   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
17805
17806   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
17807   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
17808   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
17809   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
17810
17811   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
17812   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
17813   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
17814   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
17815   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
17816   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
17817   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
17818   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
17819   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
17820
17821   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
17822   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
17823   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
17824
17825   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
17826   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
17827   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
17828
17829   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
17830   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
17831   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
17832   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
17833   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
17834   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
17835
17836   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
17837   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
17838   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
17839   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
17840   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
17841   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
17842   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
17843   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
17844   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
17845   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
17846   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
17847   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
17848
17849   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
17850   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
17851   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
17852   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
17853
17854   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
17855   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
17856   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
17857   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
17858   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
17859   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
17860   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
17861 }
17862
17863 static void
17864 arm_init_tls_builtins (void)
17865 {
17866   tree ftype, decl;
17867
17868   ftype = build_function_type (ptr_type_node, void_list_node);
17869   decl = add_builtin_function ("__builtin_thread_pointer", ftype,
17870                                ARM_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
17871                                NULL, NULL_TREE);
17872   TREE_NOTHROW (decl) = 1;
17873   TREE_READONLY (decl) = 1;
17874 }
17875
17876 enum neon_builtin_type_bits {
17877   T_V8QI  = 0x0001,
17878   T_V4HI  = 0x0002,
17879   T_V2SI  = 0x0004,
17880   T_V2SF  = 0x0008,
17881   T_DI    = 0x0010,
17882   T_V16QI = 0x0020,
17883   T_V8HI  = 0x0040,
17884   T_V4SI  = 0x0080,
17885   T_V4SF  = 0x0100,
17886   T_V2DI  = 0x0200,
17887   T_TI    = 0x0400,
17888   T_EI    = 0x0800,
17889   T_OI    = 0x1000
17890 };
17891
17892 #define v8qi_UP  T_V8QI
17893 #define v4hi_UP  T_V4HI
17894 #define v2si_UP  T_V2SI
17895 #define v2sf_UP  T_V2SF
17896 #define di_UP    T_DI
17897 #define v16qi_UP T_V16QI
17898 #define v8hi_UP  T_V8HI
17899 #define v4si_UP  T_V4SI
17900 #define v4sf_UP  T_V4SF
17901 #define v2di_UP  T_V2DI
17902 #define ti_UP    T_TI
17903 #define ei_UP    T_EI
17904 #define oi_UP    T_OI
17905
17906 #define UP(X) X##_UP
17907
17908 #define T_MAX 13
17909
17910 typedef enum {
17911   NEON_BINOP,
17912   NEON_TERNOP,
17913   NEON_UNOP,
17914   NEON_GETLANE,
17915   NEON_SETLANE,
17916   NEON_CREATE,
17917   NEON_DUP,
17918   NEON_DUPLANE,
17919   NEON_COMBINE,
17920   NEON_SPLIT,
17921   NEON_LANEMUL,
17922   NEON_LANEMULL,
17923   NEON_LANEMULH,
17924   NEON_LANEMAC,
17925   NEON_SCALARMUL,
17926   NEON_SCALARMULL,
17927   NEON_SCALARMULH,
17928   NEON_SCALARMAC,
17929   NEON_CONVERT,
17930   NEON_FIXCONV,
17931   NEON_SELECT,
17932   NEON_RESULTPAIR,
17933   NEON_REINTERP,
17934   NEON_VTBL,
17935   NEON_VTBX,
17936   NEON_LOAD1,
17937   NEON_LOAD1LANE,
17938   NEON_STORE1,
17939   NEON_STORE1LANE,
17940   NEON_LOADSTRUCT,
17941   NEON_LOADSTRUCTLANE,
17942   NEON_STORESTRUCT,
17943   NEON_STORESTRUCTLANE,
17944   NEON_LOGICBINOP,
17945   NEON_SHIFTINSERT,
17946   NEON_SHIFTIMM,
17947   NEON_SHIFTACC
17948 } neon_itype;
17949
17950 typedef struct {
17951   const char *name;
17952   const neon_itype itype;
17953   const int bits;
17954   const enum insn_code codes[T_MAX];
17955   const unsigned int num_vars;
17956   unsigned int base_fcode;
17957 } neon_builtin_datum;
17958
17959 #define CF(N,X) CODE_FOR_neon_##N##X
17960
17961 #define VAR1(T, N, A) \
17962   #N, NEON_##T, UP (A), { CF (N, A) }, 1, 0
17963 #define VAR2(T, N, A, B) \
17964   #N, NEON_##T, UP (A) | UP (B), { CF (N, A), CF (N, B) }, 2, 0
17965 #define VAR3(T, N, A, B, C) \
17966   #N, NEON_##T, UP (A) | UP (B) | UP (C), \
17967   { CF (N, A), CF (N, B), CF (N, C) }, 3, 0
17968 #define VAR4(T, N, A, B, C, D) \
17969   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D), \
17970   { CF (N, A), CF (N, B), CF (N, C), CF (N, D) }, 4, 0
17971 #define VAR5(T, N, A, B, C, D, E) \
17972   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E), \
17973   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E) }, 5, 0
17974 #define VAR6(T, N, A, B, C, D, E, F) \
17975   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F), \
17976   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F) }, 6, 0
17977 #define VAR7(T, N, A, B, C, D, E, F, G) \
17978   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G), \
17979   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
17980     CF (N, G) }, 7, 0
17981 #define VAR8(T, N, A, B, C, D, E, F, G, H) \
17982   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
17983                 | UP (H), \
17984   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
17985     CF (N, G), CF (N, H) }, 8, 0
17986 #define VAR9(T, N, A, B, C, D, E, F, G, H, I) \
17987   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
17988                 | UP (H) | UP (I), \
17989   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
17990     CF (N, G), CF (N, H), CF (N, I) }, 9, 0
17991 #define VAR10(T, N, A, B, C, D, E, F, G, H, I, J) \
17992   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
17993                 | UP (H) | UP (I) | UP (J), \
17994   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
17995     CF (N, G), CF (N, H), CF (N, I), CF (N, J) }, 10, 0
17996
17997 /* The mode entries in the following table correspond to the "key" type of the
17998    instruction variant, i.e. equivalent to that which would be specified after
17999    the assembler mnemonic, which usually refers to the last vector operand.
18000    (Signed/unsigned/polynomial types are not differentiated between though, and
18001    are all mapped onto the same mode for a given element size.) The modes
18002    listed per instruction should be the same as those defined for that
18003    instruction's pattern in neon.md.
18004    WARNING: Variants should be listed in the same increasing order as
18005    neon_builtin_type_bits.  */
18006
18007 static neon_builtin_datum neon_builtin_data[] =
18008 {
18009   { VAR10 (BINOP, vadd,
18010            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18011   { VAR3 (BINOP, vaddl, v8qi, v4hi, v2si) },
18012   { VAR3 (BINOP, vaddw, v8qi, v4hi, v2si) },
18013   { VAR6 (BINOP, vhadd, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18014   { VAR8 (BINOP, vqadd, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18015   { VAR3 (BINOP, vaddhn, v8hi, v4si, v2di) },
18016   { VAR8 (BINOP, vmul, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18017   { VAR8 (TERNOP, vmla, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18018   { VAR3 (TERNOP, vmlal, v8qi, v4hi, v2si) },
18019   { VAR8 (TERNOP, vmls, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18020   { VAR3 (TERNOP, vmlsl, v8qi, v4hi, v2si) },
18021   { VAR4 (BINOP, vqdmulh, v4hi, v2si, v8hi, v4si) },
18022   { VAR2 (TERNOP, vqdmlal, v4hi, v2si) },
18023   { VAR2 (TERNOP, vqdmlsl, v4hi, v2si) },
18024   { VAR3 (BINOP, vmull, v8qi, v4hi, v2si) },
18025   { VAR2 (SCALARMULL, vmull_n, v4hi, v2si) },
18026   { VAR2 (LANEMULL, vmull_lane, v4hi, v2si) },
18027   { VAR2 (SCALARMULL, vqdmull_n, v4hi, v2si) },
18028   { VAR2 (LANEMULL, vqdmull_lane, v4hi, v2si) },
18029   { VAR4 (SCALARMULH, vqdmulh_n, v4hi, v2si, v8hi, v4si) },
18030   { VAR4 (LANEMULH, vqdmulh_lane, v4hi, v2si, v8hi, v4si) },
18031   { VAR2 (BINOP, vqdmull, v4hi, v2si) },
18032   { VAR8 (BINOP, vshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18033   { VAR8 (BINOP, vqshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18034   { VAR8 (SHIFTIMM, vshr_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18035   { VAR3 (SHIFTIMM, vshrn_n, v8hi, v4si, v2di) },
18036   { VAR3 (SHIFTIMM, vqshrn_n, v8hi, v4si, v2di) },
18037   { VAR3 (SHIFTIMM, vqshrun_n, v8hi, v4si, v2di) },
18038   { VAR8 (SHIFTIMM, vshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18039   { VAR8 (SHIFTIMM, vqshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18040   { VAR8 (SHIFTIMM, vqshlu_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18041   { VAR3 (SHIFTIMM, vshll_n, v8qi, v4hi, v2si) },
18042   { VAR8 (SHIFTACC, vsra_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18043   { VAR10 (BINOP, vsub,
18044            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18045   { VAR3 (BINOP, vsubl, v8qi, v4hi, v2si) },
18046   { VAR3 (BINOP, vsubw, v8qi, v4hi, v2si) },
18047   { VAR8 (BINOP, vqsub, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18048   { VAR6 (BINOP, vhsub, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18049   { VAR3 (BINOP, vsubhn, v8hi, v4si, v2di) },
18050   { VAR8 (BINOP, vceq, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18051   { VAR8 (BINOP, vcge, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18052   { VAR8 (BINOP, vcgt, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18053   { VAR2 (BINOP, vcage, v2sf, v4sf) },
18054   { VAR2 (BINOP, vcagt, v2sf, v4sf) },
18055   { VAR6 (BINOP, vtst, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18056   { VAR8 (BINOP, vabd, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18057   { VAR3 (BINOP, vabdl, v8qi, v4hi, v2si) },
18058   { VAR6 (TERNOP, vaba, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18059   { VAR3 (TERNOP, vabal, v8qi, v4hi, v2si) },
18060   { VAR8 (BINOP, vmax, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18061   { VAR8 (BINOP, vmin, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18062   { VAR4 (BINOP, vpadd, v8qi, v4hi, v2si, v2sf) },
18063   { VAR6 (UNOP, vpaddl, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18064   { VAR6 (BINOP, vpadal, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18065   { VAR4 (BINOP, vpmax, v8qi, v4hi, v2si, v2sf) },
18066   { VAR4 (BINOP, vpmin, v8qi, v4hi, v2si, v2sf) },
18067   { VAR2 (BINOP, vrecps, v2sf, v4sf) },
18068   { VAR2 (BINOP, vrsqrts, v2sf, v4sf) },
18069   { VAR8 (SHIFTINSERT, vsri_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18070   { VAR8 (SHIFTINSERT, vsli_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18071   { VAR8 (UNOP, vabs, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18072   { VAR6 (UNOP, vqabs, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18073   { VAR8 (UNOP, vneg, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18074   { VAR6 (UNOP, vqneg, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18075   { VAR6 (UNOP, vcls, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18076   { VAR6 (UNOP, vclz, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18077   { VAR2 (UNOP, vcnt, v8qi, v16qi) },
18078   { VAR4 (UNOP, vrecpe, v2si, v2sf, v4si, v4sf) },
18079   { VAR4 (UNOP, vrsqrte, v2si, v2sf, v4si, v4sf) },
18080   { VAR6 (UNOP, vmvn, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18081   /* FIXME: vget_lane supports more variants than this!  */
18082   { VAR10 (GETLANE, vget_lane,
18083            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18084   { VAR10 (SETLANE, vset_lane,
18085            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18086   { VAR5 (CREATE, vcreate, v8qi, v4hi, v2si, v2sf, di) },
18087   { VAR10 (DUP, vdup_n,
18088            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18089   { VAR10 (DUPLANE, vdup_lane,
18090            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18091   { VAR5 (COMBINE, vcombine, v8qi, v4hi, v2si, v2sf, di) },
18092   { VAR5 (SPLIT, vget_high, v16qi, v8hi, v4si, v4sf, v2di) },
18093   { VAR5 (SPLIT, vget_low, v16qi, v8hi, v4si, v4sf, v2di) },
18094   { VAR3 (UNOP, vmovn, v8hi, v4si, v2di) },
18095   { VAR3 (UNOP, vqmovn, v8hi, v4si, v2di) },
18096   { VAR3 (UNOP, vqmovun, v8hi, v4si, v2di) },
18097   { VAR3 (UNOP, vmovl, v8qi, v4hi, v2si) },
18098   { VAR6 (LANEMUL, vmul_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18099   { VAR6 (LANEMAC, vmla_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18100   { VAR2 (LANEMAC, vmlal_lane, v4hi, v2si) },
18101   { VAR2 (LANEMAC, vqdmlal_lane, v4hi, v2si) },
18102   { VAR6 (LANEMAC, vmls_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18103   { VAR2 (LANEMAC, vmlsl_lane, v4hi, v2si) },
18104   { VAR2 (LANEMAC, vqdmlsl_lane, v4hi, v2si) },
18105   { VAR6 (SCALARMUL, vmul_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18106   { VAR6 (SCALARMAC, vmla_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18107   { VAR2 (SCALARMAC, vmlal_n, v4hi, v2si) },
18108   { VAR2 (SCALARMAC, vqdmlal_n, v4hi, v2si) },
18109   { VAR6 (SCALARMAC, vmls_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18110   { VAR2 (SCALARMAC, vmlsl_n, v4hi, v2si) },
18111   { VAR2 (SCALARMAC, vqdmlsl_n, v4hi, v2si) },
18112   { VAR10 (BINOP, vext,
18113            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18114   { VAR8 (UNOP, vrev64, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18115   { VAR4 (UNOP, vrev32, v8qi, v4hi, v16qi, v8hi) },
18116   { VAR2 (UNOP, vrev16, v8qi, v16qi) },
18117   { VAR4 (CONVERT, vcvt, v2si, v2sf, v4si, v4sf) },
18118   { VAR4 (FIXCONV, vcvt_n, v2si, v2sf, v4si, v4sf) },
18119   { VAR10 (SELECT, vbsl,
18120            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18121   { VAR1 (VTBL, vtbl1, v8qi) },
18122   { VAR1 (VTBL, vtbl2, v8qi) },
18123   { VAR1 (VTBL, vtbl3, v8qi) },
18124   { VAR1 (VTBL, vtbl4, v8qi) },
18125   { VAR1 (VTBX, vtbx1, v8qi) },
18126   { VAR1 (VTBX, vtbx2, v8qi) },
18127   { VAR1 (VTBX, vtbx3, v8qi) },
18128   { VAR1 (VTBX, vtbx4, v8qi) },
18129   { VAR8 (RESULTPAIR, vtrn, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18130   { VAR8 (RESULTPAIR, vzip, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18131   { VAR8 (RESULTPAIR, vuzp, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18132   { VAR5 (REINTERP, vreinterpretv8qi, v8qi, v4hi, v2si, v2sf, di) },
18133   { VAR5 (REINTERP, vreinterpretv4hi, v8qi, v4hi, v2si, v2sf, di) },
18134   { VAR5 (REINTERP, vreinterpretv2si, v8qi, v4hi, v2si, v2sf, di) },
18135   { VAR5 (REINTERP, vreinterpretv2sf, v8qi, v4hi, v2si, v2sf, di) },
18136   { VAR5 (REINTERP, vreinterpretdi, v8qi, v4hi, v2si, v2sf, di) },
18137   { VAR5 (REINTERP, vreinterpretv16qi, v16qi, v8hi, v4si, v4sf, v2di) },
18138   { VAR5 (REINTERP, vreinterpretv8hi, v16qi, v8hi, v4si, v4sf, v2di) },
18139   { VAR5 (REINTERP, vreinterpretv4si, v16qi, v8hi, v4si, v4sf, v2di) },
18140   { VAR5 (REINTERP, vreinterpretv4sf, v16qi, v8hi, v4si, v4sf, v2di) },
18141   { VAR5 (REINTERP, vreinterpretv2di, v16qi, v8hi, v4si, v4sf, v2di) },
18142   { VAR10 (LOAD1, vld1,
18143            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18144   { VAR10 (LOAD1LANE, vld1_lane,
18145            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18146   { VAR10 (LOAD1, vld1_dup,
18147            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18148   { VAR10 (STORE1, vst1,
18149            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18150   { VAR10 (STORE1LANE, vst1_lane,
18151            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18152   { VAR9 (LOADSTRUCT,
18153           vld2, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18154   { VAR7 (LOADSTRUCTLANE, vld2_lane,
18155           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18156   { VAR5 (LOADSTRUCT, vld2_dup, v8qi, v4hi, v2si, v2sf, di) },
18157   { VAR9 (STORESTRUCT, vst2,
18158           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18159   { VAR7 (STORESTRUCTLANE, vst2_lane,
18160           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18161   { VAR9 (LOADSTRUCT,
18162           vld3, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18163   { VAR7 (LOADSTRUCTLANE, vld3_lane,
18164           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18165   { VAR5 (LOADSTRUCT, vld3_dup, v8qi, v4hi, v2si, v2sf, di) },
18166   { VAR9 (STORESTRUCT, vst3,
18167           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18168   { VAR7 (STORESTRUCTLANE, vst3_lane,
18169           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18170   { VAR9 (LOADSTRUCT, vld4,
18171           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18172   { VAR7 (LOADSTRUCTLANE, vld4_lane,
18173           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18174   { VAR5 (LOADSTRUCT, vld4_dup, v8qi, v4hi, v2si, v2sf, di) },
18175   { VAR9 (STORESTRUCT, vst4,
18176           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18177   { VAR7 (STORESTRUCTLANE, vst4_lane,
18178           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18179   { VAR10 (LOGICBINOP, vand,
18180            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18181   { VAR10 (LOGICBINOP, vorr,
18182            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18183   { VAR10 (BINOP, veor,
18184            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18185   { VAR10 (LOGICBINOP, vbic,
18186            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18187   { VAR10 (LOGICBINOP, vorn,
18188            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) }
18189 };
18190
18191 #undef CF
18192 #undef VAR1
18193 #undef VAR2
18194 #undef VAR3
18195 #undef VAR4
18196 #undef VAR5
18197 #undef VAR6
18198 #undef VAR7
18199 #undef VAR8
18200 #undef VAR9
18201 #undef VAR10
18202
18203 static void
18204 arm_init_neon_builtins (void)
18205 {
18206   unsigned int i, fcode = ARM_BUILTIN_NEON_BASE;
18207
18208   tree neon_intQI_type_node;
18209   tree neon_intHI_type_node;
18210   tree neon_polyQI_type_node;
18211   tree neon_polyHI_type_node;
18212   tree neon_intSI_type_node;
18213   tree neon_intDI_type_node;
18214   tree neon_float_type_node;
18215
18216   tree intQI_pointer_node;
18217   tree intHI_pointer_node;
18218   tree intSI_pointer_node;
18219   tree intDI_pointer_node;
18220   tree float_pointer_node;
18221
18222   tree const_intQI_node;
18223   tree const_intHI_node;
18224   tree const_intSI_node;
18225   tree const_intDI_node;
18226   tree const_float_node;
18227
18228   tree const_intQI_pointer_node;
18229   tree const_intHI_pointer_node;
18230   tree const_intSI_pointer_node;
18231   tree const_intDI_pointer_node;
18232   tree const_float_pointer_node;
18233
18234   tree V8QI_type_node;
18235   tree V4HI_type_node;
18236   tree V2SI_type_node;
18237   tree V2SF_type_node;
18238   tree V16QI_type_node;
18239   tree V8HI_type_node;
18240   tree V4SI_type_node;
18241   tree V4SF_type_node;
18242   tree V2DI_type_node;
18243
18244   tree intUQI_type_node;
18245   tree intUHI_type_node;
18246   tree intUSI_type_node;
18247   tree intUDI_type_node;
18248
18249   tree intEI_type_node;
18250   tree intOI_type_node;
18251   tree intCI_type_node;
18252   tree intXI_type_node;
18253
18254   tree V8QI_pointer_node;
18255   tree V4HI_pointer_node;
18256   tree V2SI_pointer_node;
18257   tree V2SF_pointer_node;
18258   tree V16QI_pointer_node;
18259   tree V8HI_pointer_node;
18260   tree V4SI_pointer_node;
18261   tree V4SF_pointer_node;
18262   tree V2DI_pointer_node;
18263
18264   tree void_ftype_pv8qi_v8qi_v8qi;
18265   tree void_ftype_pv4hi_v4hi_v4hi;
18266   tree void_ftype_pv2si_v2si_v2si;
18267   tree void_ftype_pv2sf_v2sf_v2sf;
18268   tree void_ftype_pdi_di_di;
18269   tree void_ftype_pv16qi_v16qi_v16qi;
18270   tree void_ftype_pv8hi_v8hi_v8hi;
18271   tree void_ftype_pv4si_v4si_v4si;
18272   tree void_ftype_pv4sf_v4sf_v4sf;
18273   tree void_ftype_pv2di_v2di_v2di;
18274
18275   tree reinterp_ftype_dreg[5][5];
18276   tree reinterp_ftype_qreg[5][5];
18277   tree dreg_types[5], qreg_types[5];
18278
18279   /* Create distinguished type nodes for NEON vector element types,
18280      and pointers to values of such types, so we can detect them later.  */
18281   neon_intQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
18282   neon_intHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
18283   neon_polyQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
18284   neon_polyHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
18285   neon_intSI_type_node = make_signed_type (GET_MODE_PRECISION (SImode));
18286   neon_intDI_type_node = make_signed_type (GET_MODE_PRECISION (DImode));
18287   neon_float_type_node = make_node (REAL_TYPE);
18288   TYPE_PRECISION (neon_float_type_node) = FLOAT_TYPE_SIZE;
18289   layout_type (neon_float_type_node);
18290
18291   /* Define typedefs which exactly correspond to the modes we are basing vector
18292      types on.  If you change these names you'll need to change
18293      the table used by arm_mangle_type too.  */
18294   (*lang_hooks.types.register_builtin_type) (neon_intQI_type_node,
18295                                              "__builtin_neon_qi");
18296   (*lang_hooks.types.register_builtin_type) (neon_intHI_type_node,
18297                                              "__builtin_neon_hi");
18298   (*lang_hooks.types.register_builtin_type) (neon_intSI_type_node,
18299                                              "__builtin_neon_si");
18300   (*lang_hooks.types.register_builtin_type) (neon_float_type_node,
18301                                              "__builtin_neon_sf");
18302   (*lang_hooks.types.register_builtin_type) (neon_intDI_type_node,
18303                                              "__builtin_neon_di");
18304   (*lang_hooks.types.register_builtin_type) (neon_polyQI_type_node,
18305                                              "__builtin_neon_poly8");
18306   (*lang_hooks.types.register_builtin_type) (neon_polyHI_type_node,
18307                                              "__builtin_neon_poly16");
18308
18309   intQI_pointer_node = build_pointer_type (neon_intQI_type_node);
18310   intHI_pointer_node = build_pointer_type (neon_intHI_type_node);
18311   intSI_pointer_node = build_pointer_type (neon_intSI_type_node);
18312   intDI_pointer_node = build_pointer_type (neon_intDI_type_node);
18313   float_pointer_node = build_pointer_type (neon_float_type_node);
18314
18315   /* Next create constant-qualified versions of the above types.  */
18316   const_intQI_node = build_qualified_type (neon_intQI_type_node,
18317                                            TYPE_QUAL_CONST);
18318   const_intHI_node = build_qualified_type (neon_intHI_type_node,
18319                                            TYPE_QUAL_CONST);
18320   const_intSI_node = build_qualified_type (neon_intSI_type_node,
18321                                            TYPE_QUAL_CONST);
18322   const_intDI_node = build_qualified_type (neon_intDI_type_node,
18323                                            TYPE_QUAL_CONST);
18324   const_float_node = build_qualified_type (neon_float_type_node,
18325                                            TYPE_QUAL_CONST);
18326
18327   const_intQI_pointer_node = build_pointer_type (const_intQI_node);
18328   const_intHI_pointer_node = build_pointer_type (const_intHI_node);
18329   const_intSI_pointer_node = build_pointer_type (const_intSI_node);
18330   const_intDI_pointer_node = build_pointer_type (const_intDI_node);
18331   const_float_pointer_node = build_pointer_type (const_float_node);
18332
18333   /* Now create vector types based on our NEON element types.  */
18334   /* 64-bit vectors.  */
18335   V8QI_type_node =
18336     build_vector_type_for_mode (neon_intQI_type_node, V8QImode);
18337   V4HI_type_node =
18338     build_vector_type_for_mode (neon_intHI_type_node, V4HImode);
18339   V2SI_type_node =
18340     build_vector_type_for_mode (neon_intSI_type_node, V2SImode);
18341   V2SF_type_node =
18342     build_vector_type_for_mode (neon_float_type_node, V2SFmode);
18343   /* 128-bit vectors.  */
18344   V16QI_type_node =
18345     build_vector_type_for_mode (neon_intQI_type_node, V16QImode);
18346   V8HI_type_node =
18347     build_vector_type_for_mode (neon_intHI_type_node, V8HImode);
18348   V4SI_type_node =
18349     build_vector_type_for_mode (neon_intSI_type_node, V4SImode);
18350   V4SF_type_node =
18351     build_vector_type_for_mode (neon_float_type_node, V4SFmode);
18352   V2DI_type_node =
18353     build_vector_type_for_mode (neon_intDI_type_node, V2DImode);
18354
18355   /* Unsigned integer types for various mode sizes.  */
18356   intUQI_type_node = make_unsigned_type (GET_MODE_PRECISION (QImode));
18357   intUHI_type_node = make_unsigned_type (GET_MODE_PRECISION (HImode));
18358   intUSI_type_node = make_unsigned_type (GET_MODE_PRECISION (SImode));
18359   intUDI_type_node = make_unsigned_type (GET_MODE_PRECISION (DImode));
18360
18361   (*lang_hooks.types.register_builtin_type) (intUQI_type_node,
18362                                              "__builtin_neon_uqi");
18363   (*lang_hooks.types.register_builtin_type) (intUHI_type_node,
18364                                              "__builtin_neon_uhi");
18365   (*lang_hooks.types.register_builtin_type) (intUSI_type_node,
18366                                              "__builtin_neon_usi");
18367   (*lang_hooks.types.register_builtin_type) (intUDI_type_node,
18368                                              "__builtin_neon_udi");
18369
18370   /* Opaque integer types for structures of vectors.  */
18371   intEI_type_node = make_signed_type (GET_MODE_PRECISION (EImode));
18372   intOI_type_node = make_signed_type (GET_MODE_PRECISION (OImode));
18373   intCI_type_node = make_signed_type (GET_MODE_PRECISION (CImode));
18374   intXI_type_node = make_signed_type (GET_MODE_PRECISION (XImode));
18375
18376   (*lang_hooks.types.register_builtin_type) (intTI_type_node,
18377                                              "__builtin_neon_ti");
18378   (*lang_hooks.types.register_builtin_type) (intEI_type_node,
18379                                              "__builtin_neon_ei");
18380   (*lang_hooks.types.register_builtin_type) (intOI_type_node,
18381                                              "__builtin_neon_oi");
18382   (*lang_hooks.types.register_builtin_type) (intCI_type_node,
18383                                              "__builtin_neon_ci");
18384   (*lang_hooks.types.register_builtin_type) (intXI_type_node,
18385                                              "__builtin_neon_xi");
18386
18387   /* Pointers to vector types.  */
18388   V8QI_pointer_node = build_pointer_type (V8QI_type_node);
18389   V4HI_pointer_node = build_pointer_type (V4HI_type_node);
18390   V2SI_pointer_node = build_pointer_type (V2SI_type_node);
18391   V2SF_pointer_node = build_pointer_type (V2SF_type_node);
18392   V16QI_pointer_node = build_pointer_type (V16QI_type_node);
18393   V8HI_pointer_node = build_pointer_type (V8HI_type_node);
18394   V4SI_pointer_node = build_pointer_type (V4SI_type_node);
18395   V4SF_pointer_node = build_pointer_type (V4SF_type_node);
18396   V2DI_pointer_node = build_pointer_type (V2DI_type_node);
18397
18398   /* Operations which return results as pairs.  */
18399   void_ftype_pv8qi_v8qi_v8qi =
18400     build_function_type_list (void_type_node, V8QI_pointer_node, V8QI_type_node,
18401                               V8QI_type_node, NULL);
18402   void_ftype_pv4hi_v4hi_v4hi =
18403     build_function_type_list (void_type_node, V4HI_pointer_node, V4HI_type_node,
18404                               V4HI_type_node, NULL);
18405   void_ftype_pv2si_v2si_v2si =
18406     build_function_type_list (void_type_node, V2SI_pointer_node, V2SI_type_node,
18407                               V2SI_type_node, NULL);
18408   void_ftype_pv2sf_v2sf_v2sf =
18409     build_function_type_list (void_type_node, V2SF_pointer_node, V2SF_type_node,
18410                               V2SF_type_node, NULL);
18411   void_ftype_pdi_di_di =
18412     build_function_type_list (void_type_node, intDI_pointer_node,
18413                               neon_intDI_type_node, neon_intDI_type_node, NULL);
18414   void_ftype_pv16qi_v16qi_v16qi =
18415     build_function_type_list (void_type_node, V16QI_pointer_node,
18416                               V16QI_type_node, V16QI_type_node, NULL);
18417   void_ftype_pv8hi_v8hi_v8hi =
18418     build_function_type_list (void_type_node, V8HI_pointer_node, V8HI_type_node,
18419                               V8HI_type_node, NULL);
18420   void_ftype_pv4si_v4si_v4si =
18421     build_function_type_list (void_type_node, V4SI_pointer_node, V4SI_type_node,
18422                               V4SI_type_node, NULL);
18423   void_ftype_pv4sf_v4sf_v4sf =
18424     build_function_type_list (void_type_node, V4SF_pointer_node, V4SF_type_node,
18425                               V4SF_type_node, NULL);
18426   void_ftype_pv2di_v2di_v2di =
18427     build_function_type_list (void_type_node, V2DI_pointer_node, V2DI_type_node,
18428                               V2DI_type_node, NULL);
18429
18430   dreg_types[0] = V8QI_type_node;
18431   dreg_types[1] = V4HI_type_node;
18432   dreg_types[2] = V2SI_type_node;
18433   dreg_types[3] = V2SF_type_node;
18434   dreg_types[4] = neon_intDI_type_node;
18435
18436   qreg_types[0] = V16QI_type_node;
18437   qreg_types[1] = V8HI_type_node;
18438   qreg_types[2] = V4SI_type_node;
18439   qreg_types[3] = V4SF_type_node;
18440   qreg_types[4] = V2DI_type_node;
18441
18442   for (i = 0; i < 5; i++)
18443     {
18444       int j;
18445       for (j = 0; j < 5; j++)
18446         {
18447           reinterp_ftype_dreg[i][j]
18448             = build_function_type_list (dreg_types[i], dreg_types[j], NULL);
18449           reinterp_ftype_qreg[i][j]
18450             = build_function_type_list (qreg_types[i], qreg_types[j], NULL);
18451         }
18452     }
18453
18454   for (i = 0; i < ARRAY_SIZE (neon_builtin_data); i++)
18455     {
18456       neon_builtin_datum *d = &neon_builtin_data[i];
18457       unsigned int j, codeidx = 0;
18458
18459       d->base_fcode = fcode;
18460
18461       for (j = 0; j < T_MAX; j++)
18462         {
18463           const char* const modenames[] = {
18464             "v8qi", "v4hi", "v2si", "v2sf", "di",
18465             "v16qi", "v8hi", "v4si", "v4sf", "v2di"
18466           };
18467           char namebuf[60];
18468           tree ftype = NULL;
18469           enum insn_code icode;
18470           int is_load = 0, is_store = 0;
18471
18472           if ((d->bits & (1 << j)) == 0)
18473             continue;
18474
18475           icode = d->codes[codeidx++];
18476
18477           switch (d->itype)
18478             {
18479             case NEON_LOAD1:
18480             case NEON_LOAD1LANE:
18481             case NEON_LOADSTRUCT:
18482             case NEON_LOADSTRUCTLANE:
18483               is_load = 1;
18484               /* Fall through.  */
18485             case NEON_STORE1:
18486             case NEON_STORE1LANE:
18487             case NEON_STORESTRUCT:
18488             case NEON_STORESTRUCTLANE:
18489               if (!is_load)
18490                 is_store = 1;
18491               /* Fall through.  */
18492             case NEON_UNOP:
18493             case NEON_BINOP:
18494             case NEON_LOGICBINOP:
18495             case NEON_SHIFTINSERT:
18496             case NEON_TERNOP:
18497             case NEON_GETLANE:
18498             case NEON_SETLANE:
18499             case NEON_CREATE:
18500             case NEON_DUP:
18501             case NEON_DUPLANE:
18502             case NEON_SHIFTIMM:
18503             case NEON_SHIFTACC:
18504             case NEON_COMBINE:
18505             case NEON_SPLIT:
18506             case NEON_CONVERT:
18507             case NEON_FIXCONV:
18508             case NEON_LANEMUL:
18509             case NEON_LANEMULL:
18510             case NEON_LANEMULH:
18511             case NEON_LANEMAC:
18512             case NEON_SCALARMUL:
18513             case NEON_SCALARMULL:
18514             case NEON_SCALARMULH:
18515             case NEON_SCALARMAC:
18516             case NEON_SELECT:
18517             case NEON_VTBL:
18518             case NEON_VTBX:
18519               {
18520                 int k;
18521                 tree return_type = void_type_node, args = void_list_node;
18522
18523                 /* Build a function type directly from the insn_data for this
18524                    builtin.  The build_function_type() function takes care of
18525                    removing duplicates for us.  */
18526                 for (k = insn_data[icode].n_operands - 1; k >= 0; k--)
18527                   {
18528                     tree eltype;
18529
18530                     if (is_load && k == 1)
18531                       {
18532                         /* Neon load patterns always have the memory operand
18533                            (a SImode pointer) in the operand 1 position.  We
18534                            want a const pointer to the element type in that
18535                            position.  */
18536                         gcc_assert (insn_data[icode].operand[k].mode == SImode);
18537
18538                         switch (1 << j)
18539                           {
18540                           case T_V8QI:
18541                           case T_V16QI:
18542                             eltype = const_intQI_pointer_node;
18543                             break;
18544
18545                           case T_V4HI:
18546                           case T_V8HI:
18547                             eltype = const_intHI_pointer_node;
18548                             break;
18549
18550                           case T_V2SI:
18551                           case T_V4SI:
18552                             eltype = const_intSI_pointer_node;
18553                             break;
18554
18555                           case T_V2SF:
18556                           case T_V4SF:
18557                             eltype = const_float_pointer_node;
18558                             break;
18559
18560                           case T_DI:
18561                           case T_V2DI:
18562                             eltype = const_intDI_pointer_node;
18563                             break;
18564
18565                           default: gcc_unreachable ();
18566                           }
18567                       }
18568                     else if (is_store && k == 0)
18569                       {
18570                         /* Similarly, Neon store patterns use operand 0 as
18571                            the memory location to store to (a SImode pointer).
18572                            Use a pointer to the element type of the store in
18573                            that position.  */
18574                         gcc_assert (insn_data[icode].operand[k].mode == SImode);
18575
18576                         switch (1 << j)
18577                           {
18578                           case T_V8QI:
18579                           case T_V16QI:
18580                             eltype = intQI_pointer_node;
18581                             break;
18582
18583                           case T_V4HI:
18584                           case T_V8HI:
18585                             eltype = intHI_pointer_node;
18586                             break;
18587
18588                           case T_V2SI:
18589                           case T_V4SI:
18590                             eltype = intSI_pointer_node;
18591                             break;
18592
18593                           case T_V2SF:
18594                           case T_V4SF:
18595                             eltype = float_pointer_node;
18596                             break;
18597
18598                           case T_DI:
18599                           case T_V2DI:
18600                             eltype = intDI_pointer_node;
18601                             break;
18602
18603                           default: gcc_unreachable ();
18604                           }
18605                       }
18606                     else
18607                       {
18608                         switch (insn_data[icode].operand[k].mode)
18609                           {
18610                           case VOIDmode: eltype = void_type_node; break;
18611                           /* Scalars.  */
18612                           case QImode: eltype = neon_intQI_type_node; break;
18613                           case HImode: eltype = neon_intHI_type_node; break;
18614                           case SImode: eltype = neon_intSI_type_node; break;
18615                           case SFmode: eltype = neon_float_type_node; break;
18616                           case DImode: eltype = neon_intDI_type_node; break;
18617                           case TImode: eltype = intTI_type_node; break;
18618                           case EImode: eltype = intEI_type_node; break;
18619                           case OImode: eltype = intOI_type_node; break;
18620                           case CImode: eltype = intCI_type_node; break;
18621                           case XImode: eltype = intXI_type_node; break;
18622                           /* 64-bit vectors.  */
18623                           case V8QImode: eltype = V8QI_type_node; break;
18624                           case V4HImode: eltype = V4HI_type_node; break;
18625                           case V2SImode: eltype = V2SI_type_node; break;
18626                           case V2SFmode: eltype = V2SF_type_node; break;
18627                           /* 128-bit vectors.  */
18628                           case V16QImode: eltype = V16QI_type_node; break;
18629                           case V8HImode: eltype = V8HI_type_node; break;
18630                           case V4SImode: eltype = V4SI_type_node; break;
18631                           case V4SFmode: eltype = V4SF_type_node; break;
18632                           case V2DImode: eltype = V2DI_type_node; break;
18633                           default: gcc_unreachable ();
18634                           }
18635                       }
18636
18637                     if (k == 0 && !is_store)
18638                       return_type = eltype;
18639                     else
18640                       args = tree_cons (NULL_TREE, eltype, args);
18641                   }
18642
18643                 ftype = build_function_type (return_type, args);
18644               }
18645               break;
18646
18647             case NEON_RESULTPAIR:
18648               {
18649                 switch (insn_data[icode].operand[1].mode)
18650                   {
18651                   case V8QImode: ftype = void_ftype_pv8qi_v8qi_v8qi; break;
18652                   case V4HImode: ftype = void_ftype_pv4hi_v4hi_v4hi; break;
18653                   case V2SImode: ftype = void_ftype_pv2si_v2si_v2si; break;
18654                   case V2SFmode: ftype = void_ftype_pv2sf_v2sf_v2sf; break;
18655                   case DImode: ftype = void_ftype_pdi_di_di; break;
18656                   case V16QImode: ftype = void_ftype_pv16qi_v16qi_v16qi; break;
18657                   case V8HImode: ftype = void_ftype_pv8hi_v8hi_v8hi; break;
18658                   case V4SImode: ftype = void_ftype_pv4si_v4si_v4si; break;
18659                   case V4SFmode: ftype = void_ftype_pv4sf_v4sf_v4sf; break;
18660                   case V2DImode: ftype = void_ftype_pv2di_v2di_v2di; break;
18661                   default: gcc_unreachable ();
18662                   }
18663               }
18664               break;
18665
18666             case NEON_REINTERP:
18667               {
18668                 /* We iterate over 5 doubleword types, then 5 quadword
18669                    types.  */
18670                 int rhs = j % 5;
18671                 switch (insn_data[icode].operand[0].mode)
18672                   {
18673                   case V8QImode: ftype = reinterp_ftype_dreg[0][rhs]; break;
18674                   case V4HImode: ftype = reinterp_ftype_dreg[1][rhs]; break;
18675                   case V2SImode: ftype = reinterp_ftype_dreg[2][rhs]; break;
18676                   case V2SFmode: ftype = reinterp_ftype_dreg[3][rhs]; break;
18677                   case DImode: ftype = reinterp_ftype_dreg[4][rhs]; break;
18678                   case V16QImode: ftype = reinterp_ftype_qreg[0][rhs]; break;
18679                   case V8HImode: ftype = reinterp_ftype_qreg[1][rhs]; break;
18680                   case V4SImode: ftype = reinterp_ftype_qreg[2][rhs]; break;
18681                   case V4SFmode: ftype = reinterp_ftype_qreg[3][rhs]; break;
18682                   case V2DImode: ftype = reinterp_ftype_qreg[4][rhs]; break;
18683                   default: gcc_unreachable ();
18684                   }
18685               }
18686               break;
18687
18688             default:
18689               gcc_unreachable ();
18690             }
18691
18692           gcc_assert (ftype != NULL);
18693
18694           sprintf (namebuf, "__builtin_neon_%s%s", d->name, modenames[j]);
18695
18696           add_builtin_function (namebuf, ftype, fcode++, BUILT_IN_MD, NULL,
18697                                 NULL_TREE);
18698         }
18699     }
18700 }
18701
18702 static void
18703 arm_init_fp16_builtins (void)
18704 {
18705   tree fp16_type = make_node (REAL_TYPE);
18706   TYPE_PRECISION (fp16_type) = 16;
18707   layout_type (fp16_type);
18708   (*lang_hooks.types.register_builtin_type) (fp16_type, "__fp16");
18709 }
18710
18711 static void
18712 arm_init_builtins (void)
18713 {
18714   arm_init_tls_builtins ();
18715
18716   if (TARGET_REALLY_IWMMXT)
18717     arm_init_iwmmxt_builtins ();
18718
18719   if (TARGET_NEON)
18720     arm_init_neon_builtins ();
18721
18722   if (arm_fp16_format)
18723     arm_init_fp16_builtins ();
18724 }
18725
18726 /* Implement TARGET_INVALID_PARAMETER_TYPE.  */
18727
18728 static const char *
18729 arm_invalid_parameter_type (const_tree t)
18730 {
18731   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
18732     return N_("function parameters cannot have __fp16 type");
18733   return NULL;
18734 }
18735
18736 /* Implement TARGET_INVALID_PARAMETER_TYPE.  */
18737
18738 static const char *
18739 arm_invalid_return_type (const_tree t)
18740 {
18741   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
18742     return N_("functions cannot return __fp16 type");
18743   return NULL;
18744 }
18745
18746 /* Implement TARGET_PROMOTED_TYPE.  */
18747
18748 static tree
18749 arm_promoted_type (const_tree t)
18750 {
18751   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
18752     return float_type_node;
18753   return NULL_TREE;
18754 }
18755
18756 /* Implement TARGET_CONVERT_TO_TYPE.
18757    Specifically, this hook implements the peculiarity of the ARM
18758    half-precision floating-point C semantics that requires conversions between
18759    __fp16 to or from double to do an intermediate conversion to float.  */
18760
18761 static tree
18762 arm_convert_to_type (tree type, tree expr)
18763 {
18764   tree fromtype = TREE_TYPE (expr);
18765   if (!SCALAR_FLOAT_TYPE_P (fromtype) || !SCALAR_FLOAT_TYPE_P (type))
18766     return NULL_TREE;
18767   if ((TYPE_PRECISION (fromtype) == 16 && TYPE_PRECISION (type) > 32)
18768       || (TYPE_PRECISION (type) == 16 && TYPE_PRECISION (fromtype) > 32))
18769     return convert (type, convert (float_type_node, expr));
18770   return NULL_TREE;
18771 }
18772
18773 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P.
18774    This simply adds HFmode as a supported mode; even though we don't
18775    implement arithmetic on this type directly, it's supported by
18776    optabs conversions, much the way the double-word arithmetic is
18777    special-cased in the default hook.  */
18778
18779 static bool
18780 arm_scalar_mode_supported_p (enum machine_mode mode)
18781 {
18782   if (mode == HFmode)
18783     return (arm_fp16_format != ARM_FP16_FORMAT_NONE);
18784   else
18785     return default_scalar_mode_supported_p (mode);
18786 }
18787
18788 /* Errors in the source file can cause expand_expr to return const0_rtx
18789    where we expect a vector.  To avoid crashing, use one of the vector
18790    clear instructions.  */
18791
18792 static rtx
18793 safe_vector_operand (rtx x, enum machine_mode mode)
18794 {
18795   if (x != const0_rtx)
18796     return x;
18797   x = gen_reg_rtx (mode);
18798
18799   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
18800                                : gen_rtx_SUBREG (DImode, x, 0)));
18801   return x;
18802 }
18803
18804 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
18805
18806 static rtx
18807 arm_expand_binop_builtin (enum insn_code icode,
18808                           tree exp, rtx target)
18809 {
18810   rtx pat;
18811   tree arg0 = CALL_EXPR_ARG (exp, 0);
18812   tree arg1 = CALL_EXPR_ARG (exp, 1);
18813   rtx op0 = expand_normal (arg0);
18814   rtx op1 = expand_normal (arg1);
18815   enum machine_mode tmode = insn_data[icode].operand[0].mode;
18816   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
18817   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
18818
18819   if (VECTOR_MODE_P (mode0))
18820     op0 = safe_vector_operand (op0, mode0);
18821   if (VECTOR_MODE_P (mode1))
18822     op1 = safe_vector_operand (op1, mode1);
18823
18824   if (! target
18825       || GET_MODE (target) != tmode
18826       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
18827     target = gen_reg_rtx (tmode);
18828
18829   gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
18830
18831   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
18832     op0 = copy_to_mode_reg (mode0, op0);
18833   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
18834     op1 = copy_to_mode_reg (mode1, op1);
18835
18836   pat = GEN_FCN (icode) (target, op0, op1);
18837   if (! pat)
18838     return 0;
18839   emit_insn (pat);
18840   return target;
18841 }
18842
18843 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
18844
18845 static rtx
18846 arm_expand_unop_builtin (enum insn_code icode,
18847                          tree exp, rtx target, int do_load)
18848 {
18849   rtx pat;
18850   tree arg0 = CALL_EXPR_ARG (exp, 0);
18851   rtx op0 = expand_normal (arg0);
18852   enum machine_mode tmode = insn_data[icode].operand[0].mode;
18853   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
18854
18855   if (! target
18856       || GET_MODE (target) != tmode
18857       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
18858     target = gen_reg_rtx (tmode);
18859   if (do_load)
18860     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
18861   else
18862     {
18863       if (VECTOR_MODE_P (mode0))
18864         op0 = safe_vector_operand (op0, mode0);
18865
18866       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
18867         op0 = copy_to_mode_reg (mode0, op0);
18868     }
18869
18870   pat = GEN_FCN (icode) (target, op0);
18871   if (! pat)
18872     return 0;
18873   emit_insn (pat);
18874   return target;
18875 }
18876
18877 static int
18878 neon_builtin_compare (const void *a, const void *b)
18879 {
18880   const neon_builtin_datum *const key = (const neon_builtin_datum *) a;
18881   const neon_builtin_datum *const memb = (const neon_builtin_datum *) b;
18882   unsigned int soughtcode = key->base_fcode;
18883
18884   if (soughtcode >= memb->base_fcode
18885       && soughtcode < memb->base_fcode + memb->num_vars)
18886     return 0;
18887   else if (soughtcode < memb->base_fcode)
18888     return -1;
18889   else
18890     return 1;
18891 }
18892
18893 static enum insn_code
18894 locate_neon_builtin_icode (int fcode, neon_itype *itype)
18895 {
18896   neon_builtin_datum key, *found;
18897   int idx;
18898
18899   key.base_fcode = fcode;
18900   found = (neon_builtin_datum *)
18901     bsearch (&key, &neon_builtin_data[0], ARRAY_SIZE (neon_builtin_data),
18902                    sizeof (neon_builtin_data[0]), neon_builtin_compare);
18903   gcc_assert (found);
18904   idx = fcode - (int) found->base_fcode;
18905   gcc_assert (idx >= 0 && idx < T_MAX && idx < (int)found->num_vars);
18906
18907   if (itype)
18908     *itype = found->itype;
18909
18910   return found->codes[idx];
18911 }
18912
18913 typedef enum {
18914   NEON_ARG_COPY_TO_REG,
18915   NEON_ARG_CONSTANT,
18916   NEON_ARG_STOP
18917 } builtin_arg;
18918
18919 #define NEON_MAX_BUILTIN_ARGS 5
18920
18921 /* Expand a Neon builtin.  */
18922 static rtx
18923 arm_expand_neon_args (rtx target, int icode, int have_retval,
18924                       tree exp, ...)
18925 {
18926   va_list ap;
18927   rtx pat;
18928   tree arg[NEON_MAX_BUILTIN_ARGS];
18929   rtx op[NEON_MAX_BUILTIN_ARGS];
18930   enum machine_mode tmode = insn_data[icode].operand[0].mode;
18931   enum machine_mode mode[NEON_MAX_BUILTIN_ARGS];
18932   int argc = 0;
18933
18934   if (have_retval
18935       && (!target
18936           || GET_MODE (target) != tmode
18937           || !(*insn_data[icode].operand[0].predicate) (target, tmode)))
18938     target = gen_reg_rtx (tmode);
18939
18940   va_start (ap, exp);
18941
18942   for (;;)
18943     {
18944       builtin_arg thisarg = (builtin_arg) va_arg (ap, int);
18945
18946       if (thisarg == NEON_ARG_STOP)
18947         break;
18948       else
18949         {
18950           arg[argc] = CALL_EXPR_ARG (exp, argc);
18951           op[argc] = expand_normal (arg[argc]);
18952           mode[argc] = insn_data[icode].operand[argc + have_retval].mode;
18953
18954           switch (thisarg)
18955             {
18956             case NEON_ARG_COPY_TO_REG:
18957               /*gcc_assert (GET_MODE (op[argc]) == mode[argc]);*/
18958               if (!(*insn_data[icode].operand[argc + have_retval].predicate)
18959                      (op[argc], mode[argc]))
18960                 op[argc] = copy_to_mode_reg (mode[argc], op[argc]);
18961               break;
18962
18963             case NEON_ARG_CONSTANT:
18964               /* FIXME: This error message is somewhat unhelpful.  */
18965               if (!(*insn_data[icode].operand[argc + have_retval].predicate)
18966                     (op[argc], mode[argc]))
18967                 error ("argument must be a constant");
18968               break;
18969
18970             case NEON_ARG_STOP:
18971               gcc_unreachable ();
18972             }
18973
18974           argc++;
18975         }
18976     }
18977
18978   va_end (ap);
18979
18980   if (have_retval)
18981     switch (argc)
18982       {
18983       case 1:
18984         pat = GEN_FCN (icode) (target, op[0]);
18985         break;
18986
18987       case 2:
18988         pat = GEN_FCN (icode) (target, op[0], op[1]);
18989         break;
18990
18991       case 3:
18992         pat = GEN_FCN (icode) (target, op[0], op[1], op[2]);
18993         break;
18994
18995       case 4:
18996         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3]);
18997         break;
18998
18999       case 5:
19000         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3], op[4]);
19001         break;
19002
19003       default:
19004         gcc_unreachable ();
19005       }
19006   else
19007     switch (argc)
19008       {
19009       case 1:
19010         pat = GEN_FCN (icode) (op[0]);
19011         break;
19012
19013       case 2:
19014         pat = GEN_FCN (icode) (op[0], op[1]);
19015         break;
19016
19017       case 3:
19018         pat = GEN_FCN (icode) (op[0], op[1], op[2]);
19019         break;
19020
19021       case 4:
19022         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
19023         break;
19024
19025       case 5:
19026         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4]);
19027         break;
19028
19029       default:
19030         gcc_unreachable ();
19031       }
19032
19033   if (!pat)
19034     return 0;
19035
19036   emit_insn (pat);
19037
19038   return target;
19039 }
19040
19041 /* Expand a Neon builtin. These are "special" because they don't have symbolic
19042    constants defined per-instruction or per instruction-variant. Instead, the
19043    required info is looked up in the table neon_builtin_data.  */
19044 static rtx
19045 arm_expand_neon_builtin (int fcode, tree exp, rtx target)
19046 {
19047   neon_itype itype;
19048   enum insn_code icode = locate_neon_builtin_icode (fcode, &itype);
19049
19050   switch (itype)
19051     {
19052     case NEON_UNOP:
19053     case NEON_CONVERT:
19054     case NEON_DUPLANE:
19055       return arm_expand_neon_args (target, icode, 1, exp,
19056         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_STOP);
19057
19058     case NEON_BINOP:
19059     case NEON_SETLANE:
19060     case NEON_SCALARMUL:
19061     case NEON_SCALARMULL:
19062     case NEON_SCALARMULH:
19063     case NEON_SHIFTINSERT:
19064     case NEON_LOGICBINOP:
19065       return arm_expand_neon_args (target, icode, 1, exp,
19066         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19067         NEON_ARG_STOP);
19068
19069     case NEON_TERNOP:
19070       return arm_expand_neon_args (target, icode, 1, exp,
19071         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19072         NEON_ARG_CONSTANT, NEON_ARG_STOP);
19073
19074     case NEON_GETLANE:
19075     case NEON_FIXCONV:
19076     case NEON_SHIFTIMM:
19077       return arm_expand_neon_args (target, icode, 1, exp,
19078         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_CONSTANT,
19079         NEON_ARG_STOP);
19080
19081     case NEON_CREATE:
19082       return arm_expand_neon_args (target, icode, 1, exp,
19083         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19084
19085     case NEON_DUP:
19086     case NEON_SPLIT:
19087     case NEON_REINTERP:
19088       return arm_expand_neon_args (target, icode, 1, exp,
19089         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19090
19091     case NEON_COMBINE:
19092     case NEON_VTBL:
19093       return arm_expand_neon_args (target, icode, 1, exp,
19094         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19095
19096     case NEON_RESULTPAIR:
19097       return arm_expand_neon_args (target, icode, 0, exp,
19098         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19099         NEON_ARG_STOP);
19100
19101     case NEON_LANEMUL:
19102     case NEON_LANEMULL:
19103     case NEON_LANEMULH:
19104       return arm_expand_neon_args (target, icode, 1, exp,
19105         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19106         NEON_ARG_CONSTANT, NEON_ARG_STOP);
19107
19108     case NEON_LANEMAC:
19109       return arm_expand_neon_args (target, icode, 1, exp,
19110         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19111         NEON_ARG_CONSTANT, NEON_ARG_CONSTANT, NEON_ARG_STOP);
19112
19113     case NEON_SHIFTACC:
19114       return arm_expand_neon_args (target, icode, 1, exp,
19115         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19116         NEON_ARG_CONSTANT, NEON_ARG_STOP);
19117
19118     case NEON_SCALARMAC:
19119       return arm_expand_neon_args (target, icode, 1, exp,
19120         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19121         NEON_ARG_CONSTANT, NEON_ARG_STOP);
19122
19123     case NEON_SELECT:
19124     case NEON_VTBX:
19125       return arm_expand_neon_args (target, icode, 1, exp,
19126         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19127         NEON_ARG_STOP);
19128
19129     case NEON_LOAD1:
19130     case NEON_LOADSTRUCT:
19131       return arm_expand_neon_args (target, icode, 1, exp,
19132         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19133
19134     case NEON_LOAD1LANE:
19135     case NEON_LOADSTRUCTLANE:
19136       return arm_expand_neon_args (target, icode, 1, exp,
19137         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19138         NEON_ARG_STOP);
19139
19140     case NEON_STORE1:
19141     case NEON_STORESTRUCT:
19142       return arm_expand_neon_args (target, icode, 0, exp,
19143         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19144
19145     case NEON_STORE1LANE:
19146     case NEON_STORESTRUCTLANE:
19147       return arm_expand_neon_args (target, icode, 0, exp,
19148         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19149         NEON_ARG_STOP);
19150     }
19151
19152   gcc_unreachable ();
19153 }
19154
19155 /* Emit code to reinterpret one Neon type as another, without altering bits.  */
19156 void
19157 neon_reinterpret (rtx dest, rtx src)
19158 {
19159   emit_move_insn (dest, gen_lowpart (GET_MODE (dest), src));
19160 }
19161
19162 /* Emit code to place a Neon pair result in memory locations (with equal
19163    registers).  */
19164 void
19165 neon_emit_pair_result_insn (enum machine_mode mode,
19166                             rtx (*intfn) (rtx, rtx, rtx, rtx), rtx destaddr,
19167                             rtx op1, rtx op2)
19168 {
19169   rtx mem = gen_rtx_MEM (mode, destaddr);
19170   rtx tmp1 = gen_reg_rtx (mode);
19171   rtx tmp2 = gen_reg_rtx (mode);
19172
19173   emit_insn (intfn (tmp1, op1, tmp2, op2));
19174
19175   emit_move_insn (mem, tmp1);
19176   mem = adjust_address (mem, mode, GET_MODE_SIZE (mode));
19177   emit_move_insn (mem, tmp2);
19178 }
19179
19180 /* Set up operands for a register copy from src to dest, taking care not to
19181    clobber registers in the process.
19182    FIXME: This has rather high polynomial complexity (O(n^3)?) but shouldn't
19183    be called with a large N, so that should be OK.  */
19184
19185 void
19186 neon_disambiguate_copy (rtx *operands, rtx *dest, rtx *src, unsigned int count)
19187 {
19188   unsigned int copied = 0, opctr = 0;
19189   unsigned int done = (1 << count) - 1;
19190   unsigned int i, j;
19191
19192   while (copied != done)
19193     {
19194       for (i = 0; i < count; i++)
19195         {
19196           int good = 1;
19197
19198           for (j = 0; good && j < count; j++)
19199             if (i != j && (copied & (1 << j)) == 0
19200                 && reg_overlap_mentioned_p (src[j], dest[i]))
19201               good = 0;
19202
19203           if (good)
19204             {
19205               operands[opctr++] = dest[i];
19206               operands[opctr++] = src[i];
19207               copied |= 1 << i;
19208             }
19209         }
19210     }
19211
19212   gcc_assert (opctr == count * 2);
19213 }
19214
19215 /* Expand an expression EXP that calls a built-in function,
19216    with result going to TARGET if that's convenient
19217    (and in mode MODE if that's convenient).
19218    SUBTARGET may be used as the target for computing one of EXP's operands.
19219    IGNORE is nonzero if the value is to be ignored.  */
19220
19221 static rtx
19222 arm_expand_builtin (tree exp,
19223                     rtx target,
19224                     rtx subtarget ATTRIBUTE_UNUSED,
19225                     enum machine_mode mode ATTRIBUTE_UNUSED,
19226                     int ignore ATTRIBUTE_UNUSED)
19227 {
19228   const struct builtin_description * d;
19229   enum insn_code    icode;
19230   tree              fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
19231   tree              arg0;
19232   tree              arg1;
19233   tree              arg2;
19234   rtx               op0;
19235   rtx               op1;
19236   rtx               op2;
19237   rtx               pat;
19238   int               fcode = DECL_FUNCTION_CODE (fndecl);
19239   size_t            i;
19240   enum machine_mode tmode;
19241   enum machine_mode mode0;
19242   enum machine_mode mode1;
19243   enum machine_mode mode2;
19244
19245   if (fcode >= ARM_BUILTIN_NEON_BASE)
19246     return arm_expand_neon_builtin (fcode, exp, target);
19247
19248   switch (fcode)
19249     {
19250     case ARM_BUILTIN_TEXTRMSB:
19251     case ARM_BUILTIN_TEXTRMUB:
19252     case ARM_BUILTIN_TEXTRMSH:
19253     case ARM_BUILTIN_TEXTRMUH:
19254     case ARM_BUILTIN_TEXTRMSW:
19255     case ARM_BUILTIN_TEXTRMUW:
19256       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
19257                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
19258                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
19259                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
19260                : CODE_FOR_iwmmxt_textrmw);
19261
19262       arg0 = CALL_EXPR_ARG (exp, 0);
19263       arg1 = CALL_EXPR_ARG (exp, 1);
19264       op0 = expand_normal (arg0);
19265       op1 = expand_normal (arg1);
19266       tmode = insn_data[icode].operand[0].mode;
19267       mode0 = insn_data[icode].operand[1].mode;
19268       mode1 = insn_data[icode].operand[2].mode;
19269
19270       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19271         op0 = copy_to_mode_reg (mode0, op0);
19272       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
19273         {
19274           /* @@@ better error message */
19275           error ("selector must be an immediate");
19276           return gen_reg_rtx (tmode);
19277         }
19278       if (target == 0
19279           || GET_MODE (target) != tmode
19280           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19281         target = gen_reg_rtx (tmode);
19282       pat = GEN_FCN (icode) (target, op0, op1);
19283       if (! pat)
19284         return 0;
19285       emit_insn (pat);
19286       return target;
19287
19288     case ARM_BUILTIN_TINSRB:
19289     case ARM_BUILTIN_TINSRH:
19290     case ARM_BUILTIN_TINSRW:
19291       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
19292                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
19293                : CODE_FOR_iwmmxt_tinsrw);
19294       arg0 = CALL_EXPR_ARG (exp, 0);
19295       arg1 = CALL_EXPR_ARG (exp, 1);
19296       arg2 = CALL_EXPR_ARG (exp, 2);
19297       op0 = expand_normal (arg0);
19298       op1 = expand_normal (arg1);
19299       op2 = expand_normal (arg2);
19300       tmode = insn_data[icode].operand[0].mode;
19301       mode0 = insn_data[icode].operand[1].mode;
19302       mode1 = insn_data[icode].operand[2].mode;
19303       mode2 = insn_data[icode].operand[3].mode;
19304
19305       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19306         op0 = copy_to_mode_reg (mode0, op0);
19307       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
19308         op1 = copy_to_mode_reg (mode1, op1);
19309       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
19310         {
19311           /* @@@ better error message */
19312           error ("selector must be an immediate");
19313           return const0_rtx;
19314         }
19315       if (target == 0
19316           || GET_MODE (target) != tmode
19317           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19318         target = gen_reg_rtx (tmode);
19319       pat = GEN_FCN (icode) (target, op0, op1, op2);
19320       if (! pat)
19321         return 0;
19322       emit_insn (pat);
19323       return target;
19324
19325     case ARM_BUILTIN_SETWCX:
19326       arg0 = CALL_EXPR_ARG (exp, 0);
19327       arg1 = CALL_EXPR_ARG (exp, 1);
19328       op0 = force_reg (SImode, expand_normal (arg0));
19329       op1 = expand_normal (arg1);
19330       emit_insn (gen_iwmmxt_tmcr (op1, op0));
19331       return 0;
19332
19333     case ARM_BUILTIN_GETWCX:
19334       arg0 = CALL_EXPR_ARG (exp, 0);
19335       op0 = expand_normal (arg0);
19336       target = gen_reg_rtx (SImode);
19337       emit_insn (gen_iwmmxt_tmrc (target, op0));
19338       return target;
19339
19340     case ARM_BUILTIN_WSHUFH:
19341       icode = CODE_FOR_iwmmxt_wshufh;
19342       arg0 = CALL_EXPR_ARG (exp, 0);
19343       arg1 = CALL_EXPR_ARG (exp, 1);
19344       op0 = expand_normal (arg0);
19345       op1 = expand_normal (arg1);
19346       tmode = insn_data[icode].operand[0].mode;
19347       mode1 = insn_data[icode].operand[1].mode;
19348       mode2 = insn_data[icode].operand[2].mode;
19349
19350       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
19351         op0 = copy_to_mode_reg (mode1, op0);
19352       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
19353         {
19354           /* @@@ better error message */
19355           error ("mask must be an immediate");
19356           return const0_rtx;
19357         }
19358       if (target == 0
19359           || GET_MODE (target) != tmode
19360           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19361         target = gen_reg_rtx (tmode);
19362       pat = GEN_FCN (icode) (target, op0, op1);
19363       if (! pat)
19364         return 0;
19365       emit_insn (pat);
19366       return target;
19367
19368     case ARM_BUILTIN_WSADB:
19369       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, exp, target);
19370     case ARM_BUILTIN_WSADH:
19371       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, exp, target);
19372     case ARM_BUILTIN_WSADBZ:
19373       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, exp, target);
19374     case ARM_BUILTIN_WSADHZ:
19375       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, exp, target);
19376
19377       /* Several three-argument builtins.  */
19378     case ARM_BUILTIN_WMACS:
19379     case ARM_BUILTIN_WMACU:
19380     case ARM_BUILTIN_WALIGN:
19381     case ARM_BUILTIN_TMIA:
19382     case ARM_BUILTIN_TMIAPH:
19383     case ARM_BUILTIN_TMIATT:
19384     case ARM_BUILTIN_TMIATB:
19385     case ARM_BUILTIN_TMIABT:
19386     case ARM_BUILTIN_TMIABB:
19387       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
19388                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
19389                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
19390                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
19391                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
19392                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
19393                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
19394                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
19395                : CODE_FOR_iwmmxt_walign);
19396       arg0 = CALL_EXPR_ARG (exp, 0);
19397       arg1 = CALL_EXPR_ARG (exp, 1);
19398       arg2 = CALL_EXPR_ARG (exp, 2);
19399       op0 = expand_normal (arg0);
19400       op1 = expand_normal (arg1);
19401       op2 = expand_normal (arg2);
19402       tmode = insn_data[icode].operand[0].mode;
19403       mode0 = insn_data[icode].operand[1].mode;
19404       mode1 = insn_data[icode].operand[2].mode;
19405       mode2 = insn_data[icode].operand[3].mode;
19406
19407       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19408         op0 = copy_to_mode_reg (mode0, op0);
19409       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
19410         op1 = copy_to_mode_reg (mode1, op1);
19411       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
19412         op2 = copy_to_mode_reg (mode2, op2);
19413       if (target == 0
19414           || GET_MODE (target) != tmode
19415           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19416         target = gen_reg_rtx (tmode);
19417       pat = GEN_FCN (icode) (target, op0, op1, op2);
19418       if (! pat)
19419         return 0;
19420       emit_insn (pat);
19421       return target;
19422
19423     case ARM_BUILTIN_WZERO:
19424       target = gen_reg_rtx (DImode);
19425       emit_insn (gen_iwmmxt_clrdi (target));
19426       return target;
19427
19428     case ARM_BUILTIN_THREAD_POINTER:
19429       return arm_load_tp (target);
19430
19431     default:
19432       break;
19433     }
19434
19435   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
19436     if (d->code == (const enum arm_builtins) fcode)
19437       return arm_expand_binop_builtin (d->icode, exp, target);
19438
19439   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
19440     if (d->code == (const enum arm_builtins) fcode)
19441       return arm_expand_unop_builtin (d->icode, exp, target, 0);
19442
19443   /* @@@ Should really do something sensible here.  */
19444   return NULL_RTX;
19445 }
19446 \f
19447 /* Return the number (counting from 0) of
19448    the least significant set bit in MASK.  */
19449
19450 inline static int
19451 number_of_first_bit_set (unsigned mask)
19452 {
19453   int bit;
19454
19455   for (bit = 0;
19456        (mask & (1 << bit)) == 0;
19457        ++bit)
19458     continue;
19459
19460   return bit;
19461 }
19462
19463 /* Emit code to push or pop registers to or from the stack.  F is the
19464    assembly file.  MASK is the registers to push or pop.  PUSH is
19465    nonzero if we should push, and zero if we should pop.  For debugging
19466    output, if pushing, adjust CFA_OFFSET by the amount of space added
19467    to the stack.  REAL_REGS should have the same number of bits set as
19468    MASK, and will be used instead (in the same order) to describe which
19469    registers were saved - this is used to mark the save slots when we
19470    push high registers after moving them to low registers.  */
19471 static void
19472 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
19473                unsigned long real_regs)
19474 {
19475   int regno;
19476   int lo_mask = mask & 0xFF;
19477   int pushed_words = 0;
19478
19479   gcc_assert (mask);
19480
19481   if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
19482     {
19483       /* Special case.  Do not generate a POP PC statement here, do it in
19484          thumb_exit() */
19485       thumb_exit (f, -1);
19486       return;
19487     }
19488
19489   if (ARM_EABI_UNWIND_TABLES && push)
19490     {
19491       fprintf (f, "\t.save\t{");
19492       for (regno = 0; regno < 15; regno++)
19493         {
19494           if (real_regs & (1 << regno))
19495             {
19496               if (real_regs & ((1 << regno) -1))
19497                 fprintf (f, ", ");
19498               asm_fprintf (f, "%r", regno);
19499             }
19500         }
19501       fprintf (f, "}\n");
19502     }
19503
19504   fprintf (f, "\t%s\t{", push ? "push" : "pop");
19505
19506   /* Look at the low registers first.  */
19507   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
19508     {
19509       if (lo_mask & 1)
19510         {
19511           asm_fprintf (f, "%r", regno);
19512
19513           if ((lo_mask & ~1) != 0)
19514             fprintf (f, ", ");
19515
19516           pushed_words++;
19517         }
19518     }
19519
19520   if (push && (mask & (1 << LR_REGNUM)))
19521     {
19522       /* Catch pushing the LR.  */
19523       if (mask & 0xFF)
19524         fprintf (f, ", ");
19525
19526       asm_fprintf (f, "%r", LR_REGNUM);
19527
19528       pushed_words++;
19529     }
19530   else if (!push && (mask & (1 << PC_REGNUM)))
19531     {
19532       /* Catch popping the PC.  */
19533       if (TARGET_INTERWORK || TARGET_BACKTRACE
19534           || crtl->calls_eh_return)
19535         {
19536           /* The PC is never poped directly, instead
19537              it is popped into r3 and then BX is used.  */
19538           fprintf (f, "}\n");
19539
19540           thumb_exit (f, -1);
19541
19542           return;
19543         }
19544       else
19545         {
19546           if (mask & 0xFF)
19547             fprintf (f, ", ");
19548
19549           asm_fprintf (f, "%r", PC_REGNUM);
19550         }
19551     }
19552
19553   fprintf (f, "}\n");
19554
19555   if (push && pushed_words && dwarf2out_do_frame ())
19556     {
19557       char *l = dwarf2out_cfi_label (false);
19558       int pushed_mask = real_regs;
19559
19560       *cfa_offset += pushed_words * 4;
19561       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
19562
19563       pushed_words = 0;
19564       pushed_mask = real_regs;
19565       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
19566         {
19567           if (pushed_mask & 1)
19568             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
19569         }
19570     }
19571 }
19572
19573 /* Generate code to return from a thumb function.
19574    If 'reg_containing_return_addr' is -1, then the return address is
19575    actually on the stack, at the stack pointer.  */
19576 static void
19577 thumb_exit (FILE *f, int reg_containing_return_addr)
19578 {
19579   unsigned regs_available_for_popping;
19580   unsigned regs_to_pop;
19581   int pops_needed;
19582   unsigned available;
19583   unsigned required;
19584   int mode;
19585   int size;
19586   int restore_a4 = FALSE;
19587
19588   /* Compute the registers we need to pop.  */
19589   regs_to_pop = 0;
19590   pops_needed = 0;
19591
19592   if (reg_containing_return_addr == -1)
19593     {
19594       regs_to_pop |= 1 << LR_REGNUM;
19595       ++pops_needed;
19596     }
19597
19598   if (TARGET_BACKTRACE)
19599     {
19600       /* Restore the (ARM) frame pointer and stack pointer.  */
19601       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
19602       pops_needed += 2;
19603     }
19604
19605   /* If there is nothing to pop then just emit the BX instruction and
19606      return.  */
19607   if (pops_needed == 0)
19608     {
19609       if (crtl->calls_eh_return)
19610         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
19611
19612       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
19613       return;
19614     }
19615   /* Otherwise if we are not supporting interworking and we have not created
19616      a backtrace structure and the function was not entered in ARM mode then
19617      just pop the return address straight into the PC.  */
19618   else if (!TARGET_INTERWORK
19619            && !TARGET_BACKTRACE
19620            && !is_called_in_ARM_mode (current_function_decl)
19621            && !crtl->calls_eh_return)
19622     {
19623       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
19624       return;
19625     }
19626
19627   /* Find out how many of the (return) argument registers we can corrupt.  */
19628   regs_available_for_popping = 0;
19629
19630   /* If returning via __builtin_eh_return, the bottom three registers
19631      all contain information needed for the return.  */
19632   if (crtl->calls_eh_return)
19633     size = 12;
19634   else
19635     {
19636       /* If we can deduce the registers used from the function's
19637          return value.  This is more reliable that examining
19638          df_regs_ever_live_p () because that will be set if the register is
19639          ever used in the function, not just if the register is used
19640          to hold a return value.  */
19641
19642       if (crtl->return_rtx != 0)
19643         mode = GET_MODE (crtl->return_rtx);
19644       else
19645         mode = DECL_MODE (DECL_RESULT (current_function_decl));
19646
19647       size = GET_MODE_SIZE (mode);
19648
19649       if (size == 0)
19650         {
19651           /* In a void function we can use any argument register.
19652              In a function that returns a structure on the stack
19653              we can use the second and third argument registers.  */
19654           if (mode == VOIDmode)
19655             regs_available_for_popping =
19656               (1 << ARG_REGISTER (1))
19657               | (1 << ARG_REGISTER (2))
19658               | (1 << ARG_REGISTER (3));
19659           else
19660             regs_available_for_popping =
19661               (1 << ARG_REGISTER (2))
19662               | (1 << ARG_REGISTER (3));
19663         }
19664       else if (size <= 4)
19665         regs_available_for_popping =
19666           (1 << ARG_REGISTER (2))
19667           | (1 << ARG_REGISTER (3));
19668       else if (size <= 8)
19669         regs_available_for_popping =
19670           (1 << ARG_REGISTER (3));
19671     }
19672
19673   /* Match registers to be popped with registers into which we pop them.  */
19674   for (available = regs_available_for_popping,
19675        required  = regs_to_pop;
19676        required != 0 && available != 0;
19677        available &= ~(available & - available),
19678        required  &= ~(required  & - required))
19679     -- pops_needed;
19680
19681   /* If we have any popping registers left over, remove them.  */
19682   if (available > 0)
19683     regs_available_for_popping &= ~available;
19684
19685   /* Otherwise if we need another popping register we can use
19686      the fourth argument register.  */
19687   else if (pops_needed)
19688     {
19689       /* If we have not found any free argument registers and
19690          reg a4 contains the return address, we must move it.  */
19691       if (regs_available_for_popping == 0
19692           && reg_containing_return_addr == LAST_ARG_REGNUM)
19693         {
19694           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
19695           reg_containing_return_addr = LR_REGNUM;
19696         }
19697       else if (size > 12)
19698         {
19699           /* Register a4 is being used to hold part of the return value,
19700              but we have dire need of a free, low register.  */
19701           restore_a4 = TRUE;
19702
19703           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
19704         }
19705
19706       if (reg_containing_return_addr != LAST_ARG_REGNUM)
19707         {
19708           /* The fourth argument register is available.  */
19709           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
19710
19711           --pops_needed;
19712         }
19713     }
19714
19715   /* Pop as many registers as we can.  */
19716   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
19717                  regs_available_for_popping);
19718
19719   /* Process the registers we popped.  */
19720   if (reg_containing_return_addr == -1)
19721     {
19722       /* The return address was popped into the lowest numbered register.  */
19723       regs_to_pop &= ~(1 << LR_REGNUM);
19724
19725       reg_containing_return_addr =
19726         number_of_first_bit_set (regs_available_for_popping);
19727
19728       /* Remove this register for the mask of available registers, so that
19729          the return address will not be corrupted by further pops.  */
19730       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
19731     }
19732
19733   /* If we popped other registers then handle them here.  */
19734   if (regs_available_for_popping)
19735     {
19736       int frame_pointer;
19737
19738       /* Work out which register currently contains the frame pointer.  */
19739       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
19740
19741       /* Move it into the correct place.  */
19742       asm_fprintf (f, "\tmov\t%r, %r\n",
19743                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
19744
19745       /* (Temporarily) remove it from the mask of popped registers.  */
19746       regs_available_for_popping &= ~(1 << frame_pointer);
19747       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
19748
19749       if (regs_available_for_popping)
19750         {
19751           int stack_pointer;
19752
19753           /* We popped the stack pointer as well,
19754              find the register that contains it.  */
19755           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
19756
19757           /* Move it into the stack register.  */
19758           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
19759
19760           /* At this point we have popped all necessary registers, so
19761              do not worry about restoring regs_available_for_popping
19762              to its correct value:
19763
19764              assert (pops_needed == 0)
19765              assert (regs_available_for_popping == (1 << frame_pointer))
19766              assert (regs_to_pop == (1 << STACK_POINTER))  */
19767         }
19768       else
19769         {
19770           /* Since we have just move the popped value into the frame
19771              pointer, the popping register is available for reuse, and
19772              we know that we still have the stack pointer left to pop.  */
19773           regs_available_for_popping |= (1 << frame_pointer);
19774         }
19775     }
19776
19777   /* If we still have registers left on the stack, but we no longer have
19778      any registers into which we can pop them, then we must move the return
19779      address into the link register and make available the register that
19780      contained it.  */
19781   if (regs_available_for_popping == 0 && pops_needed > 0)
19782     {
19783       regs_available_for_popping |= 1 << reg_containing_return_addr;
19784
19785       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
19786                    reg_containing_return_addr);
19787
19788       reg_containing_return_addr = LR_REGNUM;
19789     }
19790
19791   /* If we have registers left on the stack then pop some more.
19792      We know that at most we will want to pop FP and SP.  */
19793   if (pops_needed > 0)
19794     {
19795       int  popped_into;
19796       int  move_to;
19797
19798       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
19799                      regs_available_for_popping);
19800
19801       /* We have popped either FP or SP.
19802          Move whichever one it is into the correct register.  */
19803       popped_into = number_of_first_bit_set (regs_available_for_popping);
19804       move_to     = number_of_first_bit_set (regs_to_pop);
19805
19806       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
19807
19808       regs_to_pop &= ~(1 << move_to);
19809
19810       --pops_needed;
19811     }
19812
19813   /* If we still have not popped everything then we must have only
19814      had one register available to us and we are now popping the SP.  */
19815   if (pops_needed > 0)
19816     {
19817       int  popped_into;
19818
19819       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
19820                      regs_available_for_popping);
19821
19822       popped_into = number_of_first_bit_set (regs_available_for_popping);
19823
19824       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
19825       /*
19826         assert (regs_to_pop == (1 << STACK_POINTER))
19827         assert (pops_needed == 1)
19828       */
19829     }
19830
19831   /* If necessary restore the a4 register.  */
19832   if (restore_a4)
19833     {
19834       if (reg_containing_return_addr != LR_REGNUM)
19835         {
19836           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
19837           reg_containing_return_addr = LR_REGNUM;
19838         }
19839
19840       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
19841     }
19842
19843   if (crtl->calls_eh_return)
19844     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
19845
19846   /* Return to caller.  */
19847   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
19848 }
19849 \f
19850 /* Scan INSN just before assembler is output for it.
19851    For Thumb-1, we track the status of the condition codes; this
19852    information is used in the cbranchsi4_insn pattern.  */
19853 void
19854 thumb1_final_prescan_insn (rtx insn)
19855 {
19856   if (flag_print_asm_name)
19857     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
19858                  INSN_ADDRESSES (INSN_UID (insn)));
19859   /* Don't overwrite the previous setter when we get to a cbranch.  */
19860   if (INSN_CODE (insn) != CODE_FOR_cbranchsi4_insn)
19861     {
19862       enum attr_conds conds;
19863
19864       if (cfun->machine->thumb1_cc_insn)
19865         {
19866           if (modified_in_p (cfun->machine->thumb1_cc_op0, insn)
19867               || modified_in_p (cfun->machine->thumb1_cc_op1, insn))
19868             CC_STATUS_INIT;
19869         }
19870       conds = get_attr_conds (insn);
19871       if (conds == CONDS_SET)
19872         {
19873           rtx set = single_set (insn);
19874           cfun->machine->thumb1_cc_insn = insn;
19875           cfun->machine->thumb1_cc_op0 = SET_DEST (set);
19876           cfun->machine->thumb1_cc_op1 = const0_rtx;
19877           cfun->machine->thumb1_cc_mode = CC_NOOVmode;
19878           if (INSN_CODE (insn) == CODE_FOR_thumb1_subsi3_insn)
19879             {
19880               rtx src1 = XEXP (SET_SRC (set), 1);
19881               if (src1 == const0_rtx)
19882                 cfun->machine->thumb1_cc_mode = CCmode;
19883             }
19884         }
19885       else if (conds != CONDS_NOCOND)
19886         cfun->machine->thumb1_cc_insn = NULL_RTX;
19887     }
19888 }
19889
19890 int
19891 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
19892 {
19893   unsigned HOST_WIDE_INT mask = 0xff;
19894   int i;
19895
19896   val = val & (unsigned HOST_WIDE_INT)0xffffffffu;
19897   if (val == 0) /* XXX */
19898     return 0;
19899
19900   for (i = 0; i < 25; i++)
19901     if ((val & (mask << i)) == val)
19902       return 1;
19903
19904   return 0;
19905 }
19906
19907 /* Returns nonzero if the current function contains,
19908    or might contain a far jump.  */
19909 static int
19910 thumb_far_jump_used_p (void)
19911 {
19912   rtx insn;
19913
19914   /* This test is only important for leaf functions.  */
19915   /* assert (!leaf_function_p ()); */
19916
19917   /* If we have already decided that far jumps may be used,
19918      do not bother checking again, and always return true even if
19919      it turns out that they are not being used.  Once we have made
19920      the decision that far jumps are present (and that hence the link
19921      register will be pushed onto the stack) we cannot go back on it.  */
19922   if (cfun->machine->far_jump_used)
19923     return 1;
19924
19925   /* If this function is not being called from the prologue/epilogue
19926      generation code then it must be being called from the
19927      INITIAL_ELIMINATION_OFFSET macro.  */
19928   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
19929     {
19930       /* In this case we know that we are being asked about the elimination
19931          of the arg pointer register.  If that register is not being used,
19932          then there are no arguments on the stack, and we do not have to
19933          worry that a far jump might force the prologue to push the link
19934          register, changing the stack offsets.  In this case we can just
19935          return false, since the presence of far jumps in the function will
19936          not affect stack offsets.
19937
19938          If the arg pointer is live (or if it was live, but has now been
19939          eliminated and so set to dead) then we do have to test to see if
19940          the function might contain a far jump.  This test can lead to some
19941          false negatives, since before reload is completed, then length of
19942          branch instructions is not known, so gcc defaults to returning their
19943          longest length, which in turn sets the far jump attribute to true.
19944
19945          A false negative will not result in bad code being generated, but it
19946          will result in a needless push and pop of the link register.  We
19947          hope that this does not occur too often.
19948
19949          If we need doubleword stack alignment this could affect the other
19950          elimination offsets so we can't risk getting it wrong.  */
19951       if (df_regs_ever_live_p (ARG_POINTER_REGNUM))
19952         cfun->machine->arg_pointer_live = 1;
19953       else if (!cfun->machine->arg_pointer_live)
19954         return 0;
19955     }
19956
19957   /* Check to see if the function contains a branch
19958      insn with the far jump attribute set.  */
19959   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
19960     {
19961       if (GET_CODE (insn) == JUMP_INSN
19962           /* Ignore tablejump patterns.  */
19963           && GET_CODE (PATTERN (insn)) != ADDR_VEC
19964           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
19965           && get_attr_far_jump (insn) == FAR_JUMP_YES
19966           )
19967         {
19968           /* Record the fact that we have decided that
19969              the function does use far jumps.  */
19970           cfun->machine->far_jump_used = 1;
19971           return 1;
19972         }
19973     }
19974
19975   return 0;
19976 }
19977
19978 /* Return nonzero if FUNC must be entered in ARM mode.  */
19979 int
19980 is_called_in_ARM_mode (tree func)
19981 {
19982   gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
19983
19984   /* Ignore the problem about functions whose address is taken.  */
19985   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
19986     return TRUE;
19987
19988 #ifdef ARM_PE
19989   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
19990 #else
19991   return FALSE;
19992 #endif
19993 }
19994
19995 /* Given the stack offsets and register mask in OFFSETS, decide how
19996    many additional registers to push instead of subtracting a constant
19997    from SP.  For epilogues the principle is the same except we use pop.
19998    FOR_PROLOGUE indicates which we're generating.  */
19999 static int
20000 thumb1_extra_regs_pushed (arm_stack_offsets *offsets, bool for_prologue)
20001 {
20002   HOST_WIDE_INT amount;
20003   unsigned long live_regs_mask = offsets->saved_regs_mask;
20004   /* Extract a mask of the ones we can give to the Thumb's push/pop
20005      instruction.  */
20006   unsigned long l_mask = live_regs_mask & (for_prologue ? 0x40ff : 0xff);
20007   /* Then count how many other high registers will need to be pushed.  */
20008   unsigned long high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
20009   int n_free, reg_base;
20010
20011   if (!for_prologue && frame_pointer_needed)
20012     amount = offsets->locals_base - offsets->saved_regs;
20013   else
20014     amount = offsets->outgoing_args - offsets->saved_regs;
20015
20016   /* If the stack frame size is 512 exactly, we can save one load
20017      instruction, which should make this a win even when optimizing
20018      for speed.  */
20019   if (!optimize_size && amount != 512)
20020     return 0;
20021
20022   /* Can't do this if there are high registers to push.  */
20023   if (high_regs_pushed != 0)
20024     return 0;
20025
20026   /* Shouldn't do it in the prologue if no registers would normally
20027      be pushed at all.  In the epilogue, also allow it if we'll have
20028      a pop insn for the PC.  */
20029   if  (l_mask == 0
20030        && (for_prologue
20031            || TARGET_BACKTRACE
20032            || (live_regs_mask & 1 << LR_REGNUM) == 0
20033            || TARGET_INTERWORK
20034            || crtl->args.pretend_args_size != 0))
20035     return 0;
20036
20037   /* Don't do this if thumb_expand_prologue wants to emit instructions
20038      between the push and the stack frame allocation.  */
20039   if (for_prologue
20040       && ((flag_pic && arm_pic_register != INVALID_REGNUM)
20041           || (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)))
20042     return 0;
20043
20044   reg_base = 0;
20045   n_free = 0;
20046   if (!for_prologue)
20047     {
20048       reg_base = arm_size_return_regs () / UNITS_PER_WORD;
20049       live_regs_mask >>= reg_base;
20050     }
20051
20052   while (reg_base + n_free < 8 && !(live_regs_mask & 1)
20053          && (for_prologue || call_used_regs[reg_base + n_free]))
20054     {
20055       live_regs_mask >>= 1;
20056       n_free++;
20057     }
20058
20059   if (n_free == 0)
20060     return 0;
20061   gcc_assert (amount / 4 * 4 == amount);
20062
20063   if (amount >= 512 && (amount - n_free * 4) < 512)
20064     return (amount - 508) / 4;
20065   if (amount <= n_free * 4)
20066     return amount / 4;
20067   return 0;
20068 }
20069
20070 /* The bits which aren't usefully expanded as rtl.  */
20071 const char *
20072 thumb_unexpanded_epilogue (void)
20073 {
20074   arm_stack_offsets *offsets;
20075   int regno;
20076   unsigned long live_regs_mask = 0;
20077   int high_regs_pushed = 0;
20078   int extra_pop;
20079   int had_to_push_lr;
20080   int size;
20081
20082   if (cfun->machine->return_used_this_function != 0)
20083     return "";
20084
20085   if (IS_NAKED (arm_current_func_type ()))
20086     return "";
20087
20088   offsets = arm_get_frame_offsets ();
20089   live_regs_mask = offsets->saved_regs_mask;
20090   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
20091
20092   /* If we can deduce the registers used from the function's return value.
20093      This is more reliable that examining df_regs_ever_live_p () because that
20094      will be set if the register is ever used in the function, not just if
20095      the register is used to hold a return value.  */
20096   size = arm_size_return_regs ();
20097
20098   extra_pop = thumb1_extra_regs_pushed (offsets, false);
20099   if (extra_pop > 0)
20100     {
20101       unsigned long extra_mask = (1 << extra_pop) - 1;
20102       live_regs_mask |= extra_mask << (size / UNITS_PER_WORD);
20103     }
20104
20105   /* The prolog may have pushed some high registers to use as
20106      work registers.  e.g. the testsuite file:
20107      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
20108      compiles to produce:
20109         push    {r4, r5, r6, r7, lr}
20110         mov     r7, r9
20111         mov     r6, r8
20112         push    {r6, r7}
20113      as part of the prolog.  We have to undo that pushing here.  */
20114
20115   if (high_regs_pushed)
20116     {
20117       unsigned long mask = live_regs_mask & 0xff;
20118       int next_hi_reg;
20119
20120       /* The available low registers depend on the size of the value we are
20121          returning.  */
20122       if (size <= 12)
20123         mask |=  1 << 3;
20124       if (size <= 8)
20125         mask |= 1 << 2;
20126
20127       if (mask == 0)
20128         /* Oh dear!  We have no low registers into which we can pop
20129            high registers!  */
20130         internal_error
20131           ("no low registers available for popping high registers");
20132
20133       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
20134         if (live_regs_mask & (1 << next_hi_reg))
20135           break;
20136
20137       while (high_regs_pushed)
20138         {
20139           /* Find lo register(s) into which the high register(s) can
20140              be popped.  */
20141           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
20142             {
20143               if (mask & (1 << regno))
20144                 high_regs_pushed--;
20145               if (high_regs_pushed == 0)
20146                 break;
20147             }
20148
20149           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
20150
20151           /* Pop the values into the low register(s).  */
20152           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
20153
20154           /* Move the value(s) into the high registers.  */
20155           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
20156             {
20157               if (mask & (1 << regno))
20158                 {
20159                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
20160                                regno);
20161
20162                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
20163                     if (live_regs_mask & (1 << next_hi_reg))
20164                       break;
20165                 }
20166             }
20167         }
20168       live_regs_mask &= ~0x0f00;
20169     }
20170
20171   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
20172   live_regs_mask &= 0xff;
20173
20174   if (crtl->args.pretend_args_size == 0 || TARGET_BACKTRACE)
20175     {
20176       /* Pop the return address into the PC.  */
20177       if (had_to_push_lr)
20178         live_regs_mask |= 1 << PC_REGNUM;
20179
20180       /* Either no argument registers were pushed or a backtrace
20181          structure was created which includes an adjusted stack
20182          pointer, so just pop everything.  */
20183       if (live_regs_mask)
20184         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
20185                        live_regs_mask);
20186
20187       /* We have either just popped the return address into the
20188          PC or it is was kept in LR for the entire function.
20189          Note that thumb_pushpop has already called thumb_exit if the
20190          PC was in the list.  */
20191       if (!had_to_push_lr)
20192         thumb_exit (asm_out_file, LR_REGNUM);
20193     }
20194   else
20195     {
20196       /* Pop everything but the return address.  */
20197       if (live_regs_mask)
20198         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
20199                        live_regs_mask);
20200
20201       if (had_to_push_lr)
20202         {
20203           if (size > 12)
20204             {
20205               /* We have no free low regs, so save one.  */
20206               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
20207                            LAST_ARG_REGNUM);
20208             }
20209
20210           /* Get the return address into a temporary register.  */
20211           thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
20212                          1 << LAST_ARG_REGNUM);
20213
20214           if (size > 12)
20215             {
20216               /* Move the return address to lr.  */
20217               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
20218                            LAST_ARG_REGNUM);
20219               /* Restore the low register.  */
20220               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
20221                            IP_REGNUM);
20222               regno = LR_REGNUM;
20223             }
20224           else
20225             regno = LAST_ARG_REGNUM;
20226         }
20227       else
20228         regno = LR_REGNUM;
20229
20230       /* Remove the argument registers that were pushed onto the stack.  */
20231       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
20232                    SP_REGNUM, SP_REGNUM,
20233                    crtl->args.pretend_args_size);
20234
20235       thumb_exit (asm_out_file, regno);
20236     }
20237
20238   return "";
20239 }
20240
20241 /* Functions to save and restore machine-specific function data.  */
20242 static struct machine_function *
20243 arm_init_machine_status (void)
20244 {
20245   struct machine_function *machine;
20246   machine = ggc_alloc_cleared_machine_function ();
20247
20248 #if ARM_FT_UNKNOWN != 0
20249   machine->func_type = ARM_FT_UNKNOWN;
20250 #endif
20251   return machine;
20252 }
20253
20254 /* Return an RTX indicating where the return address to the
20255    calling function can be found.  */
20256 rtx
20257 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
20258 {
20259   if (count != 0)
20260     return NULL_RTX;
20261
20262   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
20263 }
20264
20265 /* Do anything needed before RTL is emitted for each function.  */
20266 void
20267 arm_init_expanders (void)
20268 {
20269   /* Arrange to initialize and mark the machine per-function status.  */
20270   init_machine_status = arm_init_machine_status;
20271
20272   /* This is to stop the combine pass optimizing away the alignment
20273      adjustment of va_arg.  */
20274   /* ??? It is claimed that this should not be necessary.  */
20275   if (cfun)
20276     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
20277 }
20278
20279
20280 /* Like arm_compute_initial_elimination offset.  Simpler because there
20281    isn't an ABI specified frame pointer for Thumb.  Instead, we set it
20282    to point at the base of the local variables after static stack
20283    space for a function has been allocated.  */
20284
20285 HOST_WIDE_INT
20286 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
20287 {
20288   arm_stack_offsets *offsets;
20289
20290   offsets = arm_get_frame_offsets ();
20291
20292   switch (from)
20293     {
20294     case ARG_POINTER_REGNUM:
20295       switch (to)
20296         {
20297         case STACK_POINTER_REGNUM:
20298           return offsets->outgoing_args - offsets->saved_args;
20299
20300         case FRAME_POINTER_REGNUM:
20301           return offsets->soft_frame - offsets->saved_args;
20302
20303         case ARM_HARD_FRAME_POINTER_REGNUM:
20304           return offsets->saved_regs - offsets->saved_args;
20305
20306         case THUMB_HARD_FRAME_POINTER_REGNUM:
20307           return offsets->locals_base - offsets->saved_args;
20308
20309         default:
20310           gcc_unreachable ();
20311         }
20312       break;
20313
20314     case FRAME_POINTER_REGNUM:
20315       switch (to)
20316         {
20317         case STACK_POINTER_REGNUM:
20318           return offsets->outgoing_args - offsets->soft_frame;
20319
20320         case ARM_HARD_FRAME_POINTER_REGNUM:
20321           return offsets->saved_regs - offsets->soft_frame;
20322
20323         case THUMB_HARD_FRAME_POINTER_REGNUM:
20324           return offsets->locals_base - offsets->soft_frame;
20325
20326         default:
20327           gcc_unreachable ();
20328         }
20329       break;
20330
20331     default:
20332       gcc_unreachable ();
20333     }
20334 }
20335
20336 /* Generate the rest of a function's prologue.  */
20337 void
20338 thumb1_expand_prologue (void)
20339 {
20340   rtx insn, dwarf;
20341
20342   HOST_WIDE_INT amount;
20343   arm_stack_offsets *offsets;
20344   unsigned long func_type;
20345   int regno;
20346   unsigned long live_regs_mask;
20347
20348   func_type = arm_current_func_type ();
20349
20350   /* Naked functions don't have prologues.  */
20351   if (IS_NAKED (func_type))
20352     return;
20353
20354   if (IS_INTERRUPT (func_type))
20355     {
20356       error ("interrupt Service Routines cannot be coded in Thumb mode");
20357       return;
20358     }
20359
20360   offsets = arm_get_frame_offsets ();
20361   live_regs_mask = offsets->saved_regs_mask;
20362   /* Load the pic register before setting the frame pointer,
20363      so we can use r7 as a temporary work register.  */
20364   if (flag_pic && arm_pic_register != INVALID_REGNUM)
20365     arm_load_pic_register (live_regs_mask);
20366
20367   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
20368     emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
20369                     stack_pointer_rtx);
20370
20371   amount = offsets->outgoing_args - offsets->saved_regs;
20372   amount -= 4 * thumb1_extra_regs_pushed (offsets, true);
20373   if (amount)
20374     {
20375       if (amount < 512)
20376         {
20377           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
20378                                         GEN_INT (- amount)));
20379           RTX_FRAME_RELATED_P (insn) = 1;
20380         }
20381       else
20382         {
20383           rtx reg;
20384
20385           /* The stack decrement is too big for an immediate value in a single
20386              insn.  In theory we could issue multiple subtracts, but after
20387              three of them it becomes more space efficient to place the full
20388              value in the constant pool and load into a register.  (Also the
20389              ARM debugger really likes to see only one stack decrement per
20390              function).  So instead we look for a scratch register into which
20391              we can load the decrement, and then we subtract this from the
20392              stack pointer.  Unfortunately on the thumb the only available
20393              scratch registers are the argument registers, and we cannot use
20394              these as they may hold arguments to the function.  Instead we
20395              attempt to locate a call preserved register which is used by this
20396              function.  If we can find one, then we know that it will have
20397              been pushed at the start of the prologue and so we can corrupt
20398              it now.  */
20399           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
20400             if (live_regs_mask & (1 << regno))
20401               break;
20402
20403           gcc_assert(regno <= LAST_LO_REGNUM);
20404
20405           reg = gen_rtx_REG (SImode, regno);
20406
20407           emit_insn (gen_movsi (reg, GEN_INT (- amount)));
20408
20409           insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
20410                                         stack_pointer_rtx, reg));
20411           RTX_FRAME_RELATED_P (insn) = 1;
20412           dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
20413                                plus_constant (stack_pointer_rtx,
20414                                               -amount));
20415           RTX_FRAME_RELATED_P (dwarf) = 1;
20416           add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
20417         }
20418     }
20419
20420   if (frame_pointer_needed)
20421     thumb_set_frame_pointer (offsets);
20422
20423   /* If we are profiling, make sure no instructions are scheduled before
20424      the call to mcount.  Similarly if the user has requested no
20425      scheduling in the prolog.  Similarly if we want non-call exceptions
20426      using the EABI unwinder, to prevent faulting instructions from being
20427      swapped with a stack adjustment.  */
20428   if (crtl->profile || !TARGET_SCHED_PROLOG
20429       || (ARM_EABI_UNWIND_TABLES && cfun->can_throw_non_call_exceptions))
20430     emit_insn (gen_blockage ());
20431
20432   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
20433   if (live_regs_mask & 0xff)
20434     cfun->machine->lr_save_eliminated = 0;
20435 }
20436
20437
20438 void
20439 thumb1_expand_epilogue (void)
20440 {
20441   HOST_WIDE_INT amount;
20442   arm_stack_offsets *offsets;
20443   int regno;
20444
20445   /* Naked functions don't have prologues.  */
20446   if (IS_NAKED (arm_current_func_type ()))
20447     return;
20448
20449   offsets = arm_get_frame_offsets ();
20450   amount = offsets->outgoing_args - offsets->saved_regs;
20451
20452   if (frame_pointer_needed)
20453     {
20454       emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
20455       amount = offsets->locals_base - offsets->saved_regs;
20456     }
20457   amount -= 4 * thumb1_extra_regs_pushed (offsets, false);
20458
20459   gcc_assert (amount >= 0);
20460   if (amount)
20461     {
20462       if (amount < 512)
20463         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
20464                                GEN_INT (amount)));
20465       else
20466         {
20467           /* r3 is always free in the epilogue.  */
20468           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
20469
20470           emit_insn (gen_movsi (reg, GEN_INT (amount)));
20471           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
20472         }
20473     }
20474
20475   /* Emit a USE (stack_pointer_rtx), so that
20476      the stack adjustment will not be deleted.  */
20477   emit_insn (gen_prologue_use (stack_pointer_rtx));
20478
20479   if (crtl->profile || !TARGET_SCHED_PROLOG)
20480     emit_insn (gen_blockage ());
20481
20482   /* Emit a clobber for each insn that will be restored in the epilogue,
20483      so that flow2 will get register lifetimes correct.  */
20484   for (regno = 0; regno < 13; regno++)
20485     if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
20486       emit_clobber (gen_rtx_REG (SImode, regno));
20487
20488   if (! df_regs_ever_live_p (LR_REGNUM))
20489     emit_use (gen_rtx_REG (SImode, LR_REGNUM));
20490 }
20491
20492 static void
20493 thumb1_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
20494 {
20495   arm_stack_offsets *offsets;
20496   unsigned long live_regs_mask = 0;
20497   unsigned long l_mask;
20498   unsigned high_regs_pushed = 0;
20499   int cfa_offset = 0;
20500   int regno;
20501
20502   if (IS_NAKED (arm_current_func_type ()))
20503     return;
20504
20505   if (is_called_in_ARM_mode (current_function_decl))
20506     {
20507       const char * name;
20508
20509       gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
20510       gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
20511                   == SYMBOL_REF);
20512       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
20513
20514       /* Generate code sequence to switch us into Thumb mode.  */
20515       /* The .code 32 directive has already been emitted by
20516          ASM_DECLARE_FUNCTION_NAME.  */
20517       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
20518       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
20519
20520       /* Generate a label, so that the debugger will notice the
20521          change in instruction sets.  This label is also used by
20522          the assembler to bypass the ARM code when this function
20523          is called from a Thumb encoded function elsewhere in the
20524          same file.  Hence the definition of STUB_NAME here must
20525          agree with the definition in gas/config/tc-arm.c.  */
20526
20527 #define STUB_NAME ".real_start_of"
20528
20529       fprintf (f, "\t.code\t16\n");
20530 #ifdef ARM_PE
20531       if (arm_dllexport_name_p (name))
20532         name = arm_strip_name_encoding (name);
20533 #endif
20534       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
20535       fprintf (f, "\t.thumb_func\n");
20536       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
20537     }
20538
20539   if (crtl->args.pretend_args_size)
20540     {
20541       /* Output unwind directive for the stack adjustment.  */
20542       if (ARM_EABI_UNWIND_TABLES)
20543         fprintf (f, "\t.pad #%d\n",
20544                  crtl->args.pretend_args_size);
20545
20546       if (cfun->machine->uses_anonymous_args)
20547         {
20548           int num_pushes;
20549
20550           fprintf (f, "\tpush\t{");
20551
20552           num_pushes = ARM_NUM_INTS (crtl->args.pretend_args_size);
20553
20554           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
20555                regno <= LAST_ARG_REGNUM;
20556                regno++)
20557             asm_fprintf (f, "%r%s", regno,
20558                          regno == LAST_ARG_REGNUM ? "" : ", ");
20559
20560           fprintf (f, "}\n");
20561         }
20562       else
20563         asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
20564                      SP_REGNUM, SP_REGNUM,
20565                      crtl->args.pretend_args_size);
20566
20567       /* We don't need to record the stores for unwinding (would it
20568          help the debugger any if we did?), but record the change in
20569          the stack pointer.  */
20570       if (dwarf2out_do_frame ())
20571         {
20572           char *l = dwarf2out_cfi_label (false);
20573
20574           cfa_offset = cfa_offset + crtl->args.pretend_args_size;
20575           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
20576         }
20577     }
20578
20579   /* Get the registers we are going to push.  */
20580   offsets = arm_get_frame_offsets ();
20581   live_regs_mask = offsets->saved_regs_mask;
20582   /* Extract a mask of the ones we can give to the Thumb's push instruction.  */
20583   l_mask = live_regs_mask & 0x40ff;
20584   /* Then count how many other high registers will need to be pushed.  */
20585   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
20586
20587   if (TARGET_BACKTRACE)
20588     {
20589       unsigned offset;
20590       unsigned work_register;
20591
20592       /* We have been asked to create a stack backtrace structure.
20593          The code looks like this:
20594
20595          0   .align 2
20596          0   func:
20597          0     sub   SP, #16         Reserve space for 4 registers.
20598          2     push  {R7}            Push low registers.
20599          4     add   R7, SP, #20     Get the stack pointer before the push.
20600          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
20601          8     mov   R7, PC          Get hold of the start of this code plus 12.
20602         10     str   R7, [SP, #16]   Store it.
20603         12     mov   R7, FP          Get hold of the current frame pointer.
20604         14     str   R7, [SP, #4]    Store it.
20605         16     mov   R7, LR          Get hold of the current return address.
20606         18     str   R7, [SP, #12]   Store it.
20607         20     add   R7, SP, #16     Point at the start of the backtrace structure.
20608         22     mov   FP, R7          Put this value into the frame pointer.  */
20609
20610       work_register = thumb_find_work_register (live_regs_mask);
20611
20612       if (ARM_EABI_UNWIND_TABLES)
20613         asm_fprintf (f, "\t.pad #16\n");
20614
20615       asm_fprintf
20616         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
20617          SP_REGNUM, SP_REGNUM);
20618
20619       if (dwarf2out_do_frame ())
20620         {
20621           char *l = dwarf2out_cfi_label (false);
20622
20623           cfa_offset = cfa_offset + 16;
20624           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
20625         }
20626
20627       if (l_mask)
20628         {
20629           thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
20630           offset = bit_count (l_mask) * UNITS_PER_WORD;
20631         }
20632       else
20633         offset = 0;
20634
20635       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
20636                    offset + 16 + crtl->args.pretend_args_size);
20637
20638       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20639                    offset + 4);
20640
20641       /* Make sure that the instruction fetching the PC is in the right place
20642          to calculate "start of backtrace creation code + 12".  */
20643       if (l_mask)
20644         {
20645           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
20646           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20647                        offset + 12);
20648           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
20649                        ARM_HARD_FRAME_POINTER_REGNUM);
20650           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20651                        offset);
20652         }
20653       else
20654         {
20655           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
20656                        ARM_HARD_FRAME_POINTER_REGNUM);
20657           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20658                        offset);
20659           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
20660           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20661                        offset + 12);
20662         }
20663
20664       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
20665       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20666                    offset + 8);
20667       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
20668                    offset + 12);
20669       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
20670                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
20671     }
20672   /* Optimization:  If we are not pushing any low registers but we are going
20673      to push some high registers then delay our first push.  This will just
20674      be a push of LR and we can combine it with the push of the first high
20675      register.  */
20676   else if ((l_mask & 0xff) != 0
20677            || (high_regs_pushed == 0 && l_mask))
20678     {
20679       unsigned long mask = l_mask;
20680       mask |= (1 << thumb1_extra_regs_pushed (offsets, true)) - 1;
20681       thumb_pushpop (f, mask, 1, &cfa_offset, mask);
20682     }
20683
20684   if (high_regs_pushed)
20685     {
20686       unsigned pushable_regs;
20687       unsigned next_hi_reg;
20688
20689       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
20690         if (live_regs_mask & (1 << next_hi_reg))
20691           break;
20692
20693       pushable_regs = l_mask & 0xff;
20694
20695       if (pushable_regs == 0)
20696         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
20697
20698       while (high_regs_pushed > 0)
20699         {
20700           unsigned long real_regs_mask = 0;
20701
20702           for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
20703             {
20704               if (pushable_regs & (1 << regno))
20705                 {
20706                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
20707
20708                   high_regs_pushed --;
20709                   real_regs_mask |= (1 << next_hi_reg);
20710
20711                   if (high_regs_pushed)
20712                     {
20713                       for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
20714                            next_hi_reg --)
20715                         if (live_regs_mask & (1 << next_hi_reg))
20716                           break;
20717                     }
20718                   else
20719                     {
20720                       pushable_regs &= ~((1 << regno) - 1);
20721                       break;
20722                     }
20723                 }
20724             }
20725
20726           /* If we had to find a work register and we have not yet
20727              saved the LR then add it to the list of regs to push.  */
20728           if (l_mask == (1 << LR_REGNUM))
20729             {
20730               thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
20731                              1, &cfa_offset,
20732                              real_regs_mask | (1 << LR_REGNUM));
20733               l_mask = 0;
20734             }
20735           else
20736             thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
20737         }
20738     }
20739 }
20740
20741 /* Handle the case of a double word load into a low register from
20742    a computed memory address.  The computed address may involve a
20743    register which is overwritten by the load.  */
20744 const char *
20745 thumb_load_double_from_address (rtx *operands)
20746 {
20747   rtx addr;
20748   rtx base;
20749   rtx offset;
20750   rtx arg1;
20751   rtx arg2;
20752
20753   gcc_assert (GET_CODE (operands[0]) == REG);
20754   gcc_assert (GET_CODE (operands[1]) == MEM);
20755
20756   /* Get the memory address.  */
20757   addr = XEXP (operands[1], 0);
20758
20759   /* Work out how the memory address is computed.  */
20760   switch (GET_CODE (addr))
20761     {
20762     case REG:
20763       operands[2] = adjust_address (operands[1], SImode, 4);
20764
20765       if (REGNO (operands[0]) == REGNO (addr))
20766         {
20767           output_asm_insn ("ldr\t%H0, %2", operands);
20768           output_asm_insn ("ldr\t%0, %1", operands);
20769         }
20770       else
20771         {
20772           output_asm_insn ("ldr\t%0, %1", operands);
20773           output_asm_insn ("ldr\t%H0, %2", operands);
20774         }
20775       break;
20776
20777     case CONST:
20778       /* Compute <address> + 4 for the high order load.  */
20779       operands[2] = adjust_address (operands[1], SImode, 4);
20780
20781       output_asm_insn ("ldr\t%0, %1", operands);
20782       output_asm_insn ("ldr\t%H0, %2", operands);
20783       break;
20784
20785     case PLUS:
20786       arg1   = XEXP (addr, 0);
20787       arg2   = XEXP (addr, 1);
20788
20789       if (CONSTANT_P (arg1))
20790         base = arg2, offset = arg1;
20791       else
20792         base = arg1, offset = arg2;
20793
20794       gcc_assert (GET_CODE (base) == REG);
20795
20796       /* Catch the case of <address> = <reg> + <reg> */
20797       if (GET_CODE (offset) == REG)
20798         {
20799           int reg_offset = REGNO (offset);
20800           int reg_base   = REGNO (base);
20801           int reg_dest   = REGNO (operands[0]);
20802
20803           /* Add the base and offset registers together into the
20804              higher destination register.  */
20805           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
20806                        reg_dest + 1, reg_base, reg_offset);
20807
20808           /* Load the lower destination register from the address in
20809              the higher destination register.  */
20810           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
20811                        reg_dest, reg_dest + 1);
20812
20813           /* Load the higher destination register from its own address
20814              plus 4.  */
20815           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
20816                        reg_dest + 1, reg_dest + 1);
20817         }
20818       else
20819         {
20820           /* Compute <address> + 4 for the high order load.  */
20821           operands[2] = adjust_address (operands[1], SImode, 4);
20822
20823           /* If the computed address is held in the low order register
20824              then load the high order register first, otherwise always
20825              load the low order register first.  */
20826           if (REGNO (operands[0]) == REGNO (base))
20827             {
20828               output_asm_insn ("ldr\t%H0, %2", operands);
20829               output_asm_insn ("ldr\t%0, %1", operands);
20830             }
20831           else
20832             {
20833               output_asm_insn ("ldr\t%0, %1", operands);
20834               output_asm_insn ("ldr\t%H0, %2", operands);
20835             }
20836         }
20837       break;
20838
20839     case LABEL_REF:
20840       /* With no registers to worry about we can just load the value
20841          directly.  */
20842       operands[2] = adjust_address (operands[1], SImode, 4);
20843
20844       output_asm_insn ("ldr\t%H0, %2", operands);
20845       output_asm_insn ("ldr\t%0, %1", operands);
20846       break;
20847
20848     default:
20849       gcc_unreachable ();
20850     }
20851
20852   return "";
20853 }
20854
20855 const char *
20856 thumb_output_move_mem_multiple (int n, rtx *operands)
20857 {
20858   rtx tmp;
20859
20860   switch (n)
20861     {
20862     case 2:
20863       if (REGNO (operands[4]) > REGNO (operands[5]))
20864         {
20865           tmp = operands[4];
20866           operands[4] = operands[5];
20867           operands[5] = tmp;
20868         }
20869       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
20870       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
20871       break;
20872
20873     case 3:
20874       if (REGNO (operands[4]) > REGNO (operands[5]))
20875         {
20876           tmp = operands[4];
20877           operands[4] = operands[5];
20878           operands[5] = tmp;
20879         }
20880       if (REGNO (operands[5]) > REGNO (operands[6]))
20881         {
20882           tmp = operands[5];
20883           operands[5] = operands[6];
20884           operands[6] = tmp;
20885         }
20886       if (REGNO (operands[4]) > REGNO (operands[5]))
20887         {
20888           tmp = operands[4];
20889           operands[4] = operands[5];
20890           operands[5] = tmp;
20891         }
20892
20893       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
20894       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
20895       break;
20896
20897     default:
20898       gcc_unreachable ();
20899     }
20900
20901   return "";
20902 }
20903
20904 /* Output a call-via instruction for thumb state.  */
20905 const char *
20906 thumb_call_via_reg (rtx reg)
20907 {
20908   int regno = REGNO (reg);
20909   rtx *labelp;
20910
20911   gcc_assert (regno < LR_REGNUM);
20912
20913   /* If we are in the normal text section we can use a single instance
20914      per compilation unit.  If we are doing function sections, then we need
20915      an entry per section, since we can't rely on reachability.  */
20916   if (in_section == text_section)
20917     {
20918       thumb_call_reg_needed = 1;
20919
20920       if (thumb_call_via_label[regno] == NULL)
20921         thumb_call_via_label[regno] = gen_label_rtx ();
20922       labelp = thumb_call_via_label + regno;
20923     }
20924   else
20925     {
20926       if (cfun->machine->call_via[regno] == NULL)
20927         cfun->machine->call_via[regno] = gen_label_rtx ();
20928       labelp = cfun->machine->call_via + regno;
20929     }
20930
20931   output_asm_insn ("bl\t%a0", labelp);
20932   return "";
20933 }
20934
20935 /* Routines for generating rtl.  */
20936 void
20937 thumb_expand_movmemqi (rtx *operands)
20938 {
20939   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
20940   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
20941   HOST_WIDE_INT len = INTVAL (operands[2]);
20942   HOST_WIDE_INT offset = 0;
20943
20944   while (len >= 12)
20945     {
20946       emit_insn (gen_movmem12b (out, in, out, in));
20947       len -= 12;
20948     }
20949
20950   if (len >= 8)
20951     {
20952       emit_insn (gen_movmem8b (out, in, out, in));
20953       len -= 8;
20954     }
20955
20956   if (len >= 4)
20957     {
20958       rtx reg = gen_reg_rtx (SImode);
20959       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
20960       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
20961       len -= 4;
20962       offset += 4;
20963     }
20964
20965   if (len >= 2)
20966     {
20967       rtx reg = gen_reg_rtx (HImode);
20968       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
20969                                               plus_constant (in, offset))));
20970       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
20971                             reg));
20972       len -= 2;
20973       offset += 2;
20974     }
20975
20976   if (len)
20977     {
20978       rtx reg = gen_reg_rtx (QImode);
20979       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
20980                                               plus_constant (in, offset))));
20981       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
20982                             reg));
20983     }
20984 }
20985
20986 void
20987 thumb_reload_out_hi (rtx *operands)
20988 {
20989   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
20990 }
20991
20992 /* Handle reading a half-word from memory during reload.  */
20993 void
20994 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
20995 {
20996   gcc_unreachable ();
20997 }
20998
20999 /* Return the length of a function name prefix
21000     that starts with the character 'c'.  */
21001 static int
21002 arm_get_strip_length (int c)
21003 {
21004   switch (c)
21005     {
21006     ARM_NAME_ENCODING_LENGTHS
21007       default: return 0;
21008     }
21009 }
21010
21011 /* Return a pointer to a function's name with any
21012    and all prefix encodings stripped from it.  */
21013 const char *
21014 arm_strip_name_encoding (const char *name)
21015 {
21016   int skip;
21017
21018   while ((skip = arm_get_strip_length (* name)))
21019     name += skip;
21020
21021   return name;
21022 }
21023
21024 /* If there is a '*' anywhere in the name's prefix, then
21025    emit the stripped name verbatim, otherwise prepend an
21026    underscore if leading underscores are being used.  */
21027 void
21028 arm_asm_output_labelref (FILE *stream, const char *name)
21029 {
21030   int skip;
21031   int verbatim = 0;
21032
21033   while ((skip = arm_get_strip_length (* name)))
21034     {
21035       verbatim |= (*name == '*');
21036       name += skip;
21037     }
21038
21039   if (verbatim)
21040     fputs (name, stream);
21041   else
21042     asm_fprintf (stream, "%U%s", name);
21043 }
21044
21045 static void
21046 arm_file_start (void)
21047 {
21048   int val;
21049
21050   if (TARGET_UNIFIED_ASM)
21051     asm_fprintf (asm_out_file, "\t.syntax unified\n");
21052
21053   if (TARGET_BPABI)
21054     {
21055       const char *fpu_name;
21056       if (arm_selected_arch)
21057         asm_fprintf (asm_out_file, "\t.arch %s\n", arm_selected_arch->name);
21058       else
21059         asm_fprintf (asm_out_file, "\t.cpu %s\n", arm_selected_cpu->name);
21060
21061       if (TARGET_SOFT_FLOAT)
21062         {
21063           if (TARGET_VFP)
21064             fpu_name = "softvfp";
21065           else
21066             fpu_name = "softfpa";
21067         }
21068       else
21069         {
21070           fpu_name = arm_fpu_desc->name;
21071           if (arm_fpu_desc->model == ARM_FP_MODEL_VFP)
21072             {
21073               if (TARGET_HARD_FLOAT)
21074                 asm_fprintf (asm_out_file, "\t.eabi_attribute 27, 3\n");
21075               if (TARGET_HARD_FLOAT_ABI)
21076                 asm_fprintf (asm_out_file, "\t.eabi_attribute 28, 1\n");
21077             }
21078         }
21079       asm_fprintf (asm_out_file, "\t.fpu %s\n", fpu_name);
21080
21081       /* Some of these attributes only apply when the corresponding features
21082          are used.  However we don't have any easy way of figuring this out.
21083          Conservatively record the setting that would have been used.  */
21084
21085       /* Tag_ABI_FP_rounding.  */
21086       if (flag_rounding_math)
21087         asm_fprintf (asm_out_file, "\t.eabi_attribute 19, 1\n");
21088       if (!flag_unsafe_math_optimizations)
21089         {
21090           /* Tag_ABI_FP_denomal.  */
21091           asm_fprintf (asm_out_file, "\t.eabi_attribute 20, 1\n");
21092           /* Tag_ABI_FP_exceptions.  */
21093           asm_fprintf (asm_out_file, "\t.eabi_attribute 21, 1\n");
21094         }
21095       /* Tag_ABI_FP_user_exceptions.  */
21096       if (flag_signaling_nans)
21097         asm_fprintf (asm_out_file, "\t.eabi_attribute 22, 1\n");
21098       /* Tag_ABI_FP_number_model.  */
21099       asm_fprintf (asm_out_file, "\t.eabi_attribute 23, %d\n", 
21100                    flag_finite_math_only ? 1 : 3);
21101
21102       /* Tag_ABI_align8_needed.  */
21103       asm_fprintf (asm_out_file, "\t.eabi_attribute 24, 1\n");
21104       /* Tag_ABI_align8_preserved.  */
21105       asm_fprintf (asm_out_file, "\t.eabi_attribute 25, 1\n");
21106       /* Tag_ABI_enum_size.  */
21107       asm_fprintf (asm_out_file, "\t.eabi_attribute 26, %d\n",
21108                    flag_short_enums ? 1 : 2);
21109
21110       /* Tag_ABI_optimization_goals.  */
21111       if (optimize_size)
21112         val = 4;
21113       else if (optimize >= 2)
21114         val = 2;
21115       else if (optimize)
21116         val = 1;
21117       else
21118         val = 6;
21119       asm_fprintf (asm_out_file, "\t.eabi_attribute 30, %d\n", val);
21120
21121       /* Tag_ABI_FP_16bit_format.  */
21122       if (arm_fp16_format)
21123         asm_fprintf (asm_out_file, "\t.eabi_attribute 38, %d\n",
21124                      (int)arm_fp16_format);
21125
21126       if (arm_lang_output_object_attributes_hook)
21127         arm_lang_output_object_attributes_hook();
21128     }
21129   default_file_start();
21130 }
21131
21132 static void
21133 arm_file_end (void)
21134 {
21135   int regno;
21136
21137   if (NEED_INDICATE_EXEC_STACK)
21138     /* Add .note.GNU-stack.  */
21139     file_end_indicate_exec_stack ();
21140
21141   if (! thumb_call_reg_needed)
21142     return;
21143
21144   switch_to_section (text_section);
21145   asm_fprintf (asm_out_file, "\t.code 16\n");
21146   ASM_OUTPUT_ALIGN (asm_out_file, 1);
21147
21148   for (regno = 0; regno < LR_REGNUM; regno++)
21149     {
21150       rtx label = thumb_call_via_label[regno];
21151
21152       if (label != 0)
21153         {
21154           targetm.asm_out.internal_label (asm_out_file, "L",
21155                                           CODE_LABEL_NUMBER (label));
21156           asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
21157         }
21158     }
21159 }
21160
21161 #ifndef ARM_PE
21162 /* Symbols in the text segment can be accessed without indirecting via the
21163    constant pool; it may take an extra binary operation, but this is still
21164    faster than indirecting via memory.  Don't do this when not optimizing,
21165    since we won't be calculating al of the offsets necessary to do this
21166    simplification.  */
21167
21168 static void
21169 arm_encode_section_info (tree decl, rtx rtl, int first)
21170 {
21171   if (optimize > 0 && TREE_CONSTANT (decl))
21172     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
21173
21174   default_encode_section_info (decl, rtl, first);
21175 }
21176 #endif /* !ARM_PE */
21177
21178 static void
21179 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
21180 {
21181   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
21182       && !strcmp (prefix, "L"))
21183     {
21184       arm_ccfsm_state = 0;
21185       arm_target_insn = NULL;
21186     }
21187   default_internal_label (stream, prefix, labelno);
21188 }
21189
21190 /* Output code to add DELTA to the first argument, and then jump
21191    to FUNCTION.  Used for C++ multiple inheritance.  */
21192 static void
21193 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
21194                      HOST_WIDE_INT delta,
21195                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
21196                      tree function)
21197 {
21198   static int thunk_label = 0;
21199   char label[256];
21200   char labelpc[256];
21201   int mi_delta = delta;
21202   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
21203   int shift = 0;
21204   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
21205                     ? 1 : 0);
21206   if (mi_delta < 0)
21207     mi_delta = - mi_delta;
21208
21209   if (TARGET_THUMB1)
21210     {
21211       int labelno = thunk_label++;
21212       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
21213       /* Thunks are entered in arm mode when avaiable.  */
21214       if (TARGET_THUMB1_ONLY)
21215         {
21216           /* push r3 so we can use it as a temporary.  */
21217           /* TODO: Omit this save if r3 is not used.  */
21218           fputs ("\tpush {r3}\n", file);
21219           fputs ("\tldr\tr3, ", file);
21220         }
21221       else
21222         {
21223           fputs ("\tldr\tr12, ", file);
21224         }
21225       assemble_name (file, label);
21226       fputc ('\n', file);
21227       if (flag_pic)
21228         {
21229           /* If we are generating PIC, the ldr instruction below loads
21230              "(target - 7) - .LTHUNKPCn" into r12.  The pc reads as
21231              the address of the add + 8, so we have:
21232
21233              r12 = (target - 7) - .LTHUNKPCn + (.LTHUNKPCn + 8)
21234                  = target + 1.
21235
21236              Note that we have "+ 1" because some versions of GNU ld
21237              don't set the low bit of the result for R_ARM_REL32
21238              relocations against thumb function symbols.
21239              On ARMv6M this is +4, not +8.  */
21240           ASM_GENERATE_INTERNAL_LABEL (labelpc, "LTHUNKPC", labelno);
21241           assemble_name (file, labelpc);
21242           fputs (":\n", file);
21243           if (TARGET_THUMB1_ONLY)
21244             {
21245               /* This is 2 insns after the start of the thunk, so we know it
21246                  is 4-byte aligned.  */
21247               fputs ("\tadd\tr3, pc, r3\n", file);
21248               fputs ("\tmov r12, r3\n", file);
21249             }
21250           else
21251             fputs ("\tadd\tr12, pc, r12\n", file);
21252         }
21253       else if (TARGET_THUMB1_ONLY)
21254         fputs ("\tmov r12, r3\n", file);
21255     }
21256   if (TARGET_THUMB1_ONLY)
21257     {
21258       if (mi_delta > 255)
21259         {
21260           fputs ("\tldr\tr3, ", file);
21261           assemble_name (file, label);
21262           fputs ("+4\n", file);
21263           asm_fprintf (file, "\t%s\t%r, %r, r3\n",
21264                        mi_op, this_regno, this_regno);
21265         }
21266       else if (mi_delta != 0)
21267         {
21268           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
21269                        mi_op, this_regno, this_regno,
21270                        mi_delta);
21271         }
21272     }
21273   else
21274     {
21275       /* TODO: Use movw/movt for large constants when available.  */
21276       while (mi_delta != 0)
21277         {
21278           if ((mi_delta & (3 << shift)) == 0)
21279             shift += 2;
21280           else
21281             {
21282               asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
21283                            mi_op, this_regno, this_regno,
21284                            mi_delta & (0xff << shift));
21285               mi_delta &= ~(0xff << shift);
21286               shift += 8;
21287             }
21288         }
21289     }
21290   if (TARGET_THUMB1)
21291     {
21292       if (TARGET_THUMB1_ONLY)
21293         fputs ("\tpop\t{r3}\n", file);
21294
21295       fprintf (file, "\tbx\tr12\n");
21296       ASM_OUTPUT_ALIGN (file, 2);
21297       assemble_name (file, label);
21298       fputs (":\n", file);
21299       if (flag_pic)
21300         {
21301           /* Output ".word .LTHUNKn-7-.LTHUNKPCn".  */
21302           rtx tem = XEXP (DECL_RTL (function), 0);
21303           tem = gen_rtx_PLUS (GET_MODE (tem), tem, GEN_INT (-7));
21304           tem = gen_rtx_MINUS (GET_MODE (tem),
21305                                tem,
21306                                gen_rtx_SYMBOL_REF (Pmode,
21307                                                    ggc_strdup (labelpc)));
21308           assemble_integer (tem, 4, BITS_PER_WORD, 1);
21309         }
21310       else
21311         /* Output ".word .LTHUNKn".  */
21312         assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
21313
21314       if (TARGET_THUMB1_ONLY && mi_delta > 255)
21315         assemble_integer (GEN_INT(mi_delta), 4, BITS_PER_WORD, 1);
21316     }
21317   else
21318     {
21319       fputs ("\tb\t", file);
21320       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
21321       if (NEED_PLT_RELOC)
21322         fputs ("(PLT)", file);
21323       fputc ('\n', file);
21324     }
21325 }
21326
21327 int
21328 arm_emit_vector_const (FILE *file, rtx x)
21329 {
21330   int i;
21331   const char * pattern;
21332
21333   gcc_assert (GET_CODE (x) == CONST_VECTOR);
21334
21335   switch (GET_MODE (x))
21336     {
21337     case V2SImode: pattern = "%08x"; break;
21338     case V4HImode: pattern = "%04x"; break;
21339     case V8QImode: pattern = "%02x"; break;
21340     default:       gcc_unreachable ();
21341     }
21342
21343   fprintf (file, "0x");
21344   for (i = CONST_VECTOR_NUNITS (x); i--;)
21345     {
21346       rtx element;
21347
21348       element = CONST_VECTOR_ELT (x, i);
21349       fprintf (file, pattern, INTVAL (element));
21350     }
21351
21352   return 1;
21353 }
21354
21355 /* Emit a fp16 constant appropriately padded to occupy a 4-byte word.
21356    HFmode constant pool entries are actually loaded with ldr.  */
21357 void
21358 arm_emit_fp16_const (rtx c)
21359 {
21360   REAL_VALUE_TYPE r;
21361   long bits;
21362
21363   REAL_VALUE_FROM_CONST_DOUBLE (r, c);
21364   bits = real_to_target (NULL, &r, HFmode);
21365   if (WORDS_BIG_ENDIAN)
21366     assemble_zeros (2);
21367   assemble_integer (GEN_INT (bits), 2, BITS_PER_WORD, 1);
21368   if (!WORDS_BIG_ENDIAN)
21369     assemble_zeros (2);
21370 }
21371
21372 const char *
21373 arm_output_load_gr (rtx *operands)
21374 {
21375   rtx reg;
21376   rtx offset;
21377   rtx wcgr;
21378   rtx sum;
21379
21380   if (GET_CODE (operands [1]) != MEM
21381       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
21382       || GET_CODE (reg = XEXP (sum, 0)) != REG
21383       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
21384       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
21385     return "wldrw%?\t%0, %1";
21386
21387   /* Fix up an out-of-range load of a GR register.  */
21388   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
21389   wcgr = operands[0];
21390   operands[0] = reg;
21391   output_asm_insn ("ldr%?\t%0, %1", operands);
21392
21393   operands[0] = wcgr;
21394   operands[1] = reg;
21395   output_asm_insn ("tmcr%?\t%0, %1", operands);
21396   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
21397
21398   return "";
21399 }
21400
21401 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
21402
21403    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
21404    named arg and all anonymous args onto the stack.
21405    XXX I know the prologue shouldn't be pushing registers, but it is faster
21406    that way.  */
21407
21408 static void
21409 arm_setup_incoming_varargs (CUMULATIVE_ARGS *pcum,
21410                             enum machine_mode mode,
21411                             tree type,
21412                             int *pretend_size,
21413                             int second_time ATTRIBUTE_UNUSED)
21414 {
21415   int nregs;
21416   
21417   cfun->machine->uses_anonymous_args = 1;
21418   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
21419     {
21420       nregs = pcum->aapcs_ncrn;
21421       if ((nregs & 1) && arm_needs_doubleword_align (mode, type))
21422         nregs++;
21423     }
21424   else
21425     nregs = pcum->nregs;
21426   
21427   if (nregs < NUM_ARG_REGS)
21428     *pretend_size = (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
21429 }
21430
21431 /* Return nonzero if the CONSUMER instruction (a store) does not need
21432    PRODUCER's value to calculate the address.  */
21433
21434 int
21435 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
21436 {
21437   rtx value = PATTERN (producer);
21438   rtx addr = PATTERN (consumer);
21439
21440   if (GET_CODE (value) == COND_EXEC)
21441     value = COND_EXEC_CODE (value);
21442   if (GET_CODE (value) == PARALLEL)
21443     value = XVECEXP (value, 0, 0);
21444   value = XEXP (value, 0);
21445   if (GET_CODE (addr) == COND_EXEC)
21446     addr = COND_EXEC_CODE (addr);
21447   if (GET_CODE (addr) == PARALLEL)
21448     addr = XVECEXP (addr, 0, 0);
21449   addr = XEXP (addr, 0);
21450
21451   return !reg_overlap_mentioned_p (value, addr);
21452 }
21453
21454 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
21455    have an early register shift value or amount dependency on the
21456    result of PRODUCER.  */
21457
21458 int
21459 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
21460 {
21461   rtx value = PATTERN (producer);
21462   rtx op = PATTERN (consumer);
21463   rtx early_op;
21464
21465   if (GET_CODE (value) == COND_EXEC)
21466     value = COND_EXEC_CODE (value);
21467   if (GET_CODE (value) == PARALLEL)
21468     value = XVECEXP (value, 0, 0);
21469   value = XEXP (value, 0);
21470   if (GET_CODE (op) == COND_EXEC)
21471     op = COND_EXEC_CODE (op);
21472   if (GET_CODE (op) == PARALLEL)
21473     op = XVECEXP (op, 0, 0);
21474   op = XEXP (op, 1);
21475
21476   early_op = XEXP (op, 0);
21477   /* This is either an actual independent shift, or a shift applied to
21478      the first operand of another operation.  We want the whole shift
21479      operation.  */
21480   if (GET_CODE (early_op) == REG)
21481     early_op = op;
21482
21483   return !reg_overlap_mentioned_p (value, early_op);
21484 }
21485
21486 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
21487    have an early register shift value dependency on the result of
21488    PRODUCER.  */
21489
21490 int
21491 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
21492 {
21493   rtx value = PATTERN (producer);
21494   rtx op = PATTERN (consumer);
21495   rtx early_op;
21496
21497   if (GET_CODE (value) == COND_EXEC)
21498     value = COND_EXEC_CODE (value);
21499   if (GET_CODE (value) == PARALLEL)
21500     value = XVECEXP (value, 0, 0);
21501   value = XEXP (value, 0);
21502   if (GET_CODE (op) == COND_EXEC)
21503     op = COND_EXEC_CODE (op);
21504   if (GET_CODE (op) == PARALLEL)
21505     op = XVECEXP (op, 0, 0);
21506   op = XEXP (op, 1);
21507
21508   early_op = XEXP (op, 0);
21509
21510   /* This is either an actual independent shift, or a shift applied to
21511      the first operand of another operation.  We want the value being
21512      shifted, in either case.  */
21513   if (GET_CODE (early_op) != REG)
21514     early_op = XEXP (early_op, 0);
21515
21516   return !reg_overlap_mentioned_p (value, early_op);
21517 }
21518
21519 /* Return nonzero if the CONSUMER (a mul or mac op) does not
21520    have an early register mult dependency on the result of
21521    PRODUCER.  */
21522
21523 int
21524 arm_no_early_mul_dep (rtx producer, rtx consumer)
21525 {
21526   rtx value = PATTERN (producer);
21527   rtx op = PATTERN (consumer);
21528
21529   if (GET_CODE (value) == COND_EXEC)
21530     value = COND_EXEC_CODE (value);
21531   if (GET_CODE (value) == PARALLEL)
21532     value = XVECEXP (value, 0, 0);
21533   value = XEXP (value, 0);
21534   if (GET_CODE (op) == COND_EXEC)
21535     op = COND_EXEC_CODE (op);
21536   if (GET_CODE (op) == PARALLEL)
21537     op = XVECEXP (op, 0, 0);
21538   op = XEXP (op, 1);
21539
21540   if (GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
21541     {
21542       if (GET_CODE (XEXP (op, 0)) == MULT)
21543         return !reg_overlap_mentioned_p (value, XEXP (op, 0));
21544       else
21545         return !reg_overlap_mentioned_p (value, XEXP (op, 1));
21546     }
21547
21548   return 0;
21549 }
21550
21551 /* We can't rely on the caller doing the proper promotion when
21552    using APCS or ATPCS.  */
21553
21554 static bool
21555 arm_promote_prototypes (const_tree t ATTRIBUTE_UNUSED)
21556 {
21557     return !TARGET_AAPCS_BASED;
21558 }
21559
21560 static enum machine_mode
21561 arm_promote_function_mode (const_tree type ATTRIBUTE_UNUSED,
21562                            enum machine_mode mode,
21563                            int *punsignedp ATTRIBUTE_UNUSED,
21564                            const_tree fntype ATTRIBUTE_UNUSED,
21565                            int for_return ATTRIBUTE_UNUSED)
21566 {
21567   if (GET_MODE_CLASS (mode) == MODE_INT
21568       && GET_MODE_SIZE (mode) < 4)
21569     return SImode;
21570
21571   return mode;
21572 }
21573
21574 /* AAPCS based ABIs use short enums by default.  */
21575
21576 static bool
21577 arm_default_short_enums (void)
21578 {
21579   return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
21580 }
21581
21582
21583 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
21584
21585 static bool
21586 arm_align_anon_bitfield (void)
21587 {
21588   return TARGET_AAPCS_BASED;
21589 }
21590
21591
21592 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
21593
21594 static tree
21595 arm_cxx_guard_type (void)
21596 {
21597   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
21598 }
21599
21600 /* Return non-zero if the consumer (a multiply-accumulate instruction)
21601    has an accumulator dependency on the result of the producer (a
21602    multiplication instruction) and no other dependency on that result.  */
21603 int
21604 arm_mac_accumulator_is_mul_result (rtx producer, rtx consumer)
21605 {
21606   rtx mul = PATTERN (producer);
21607   rtx mac = PATTERN (consumer);
21608   rtx mul_result;
21609   rtx mac_op0, mac_op1, mac_acc;
21610
21611   if (GET_CODE (mul) == COND_EXEC)
21612     mul = COND_EXEC_CODE (mul);
21613   if (GET_CODE (mac) == COND_EXEC)
21614     mac = COND_EXEC_CODE (mac);
21615
21616   /* Check that mul is of the form (set (...) (mult ...))
21617      and mla is of the form (set (...) (plus (mult ...) (...))).  */
21618   if ((GET_CODE (mul) != SET || GET_CODE (XEXP (mul, 1)) != MULT)
21619       || (GET_CODE (mac) != SET || GET_CODE (XEXP (mac, 1)) != PLUS
21620           || GET_CODE (XEXP (XEXP (mac, 1), 0)) != MULT))
21621     return 0;
21622
21623   mul_result = XEXP (mul, 0);
21624   mac_op0 = XEXP (XEXP (XEXP (mac, 1), 0), 0);
21625   mac_op1 = XEXP (XEXP (XEXP (mac, 1), 0), 1);
21626   mac_acc = XEXP (XEXP (mac, 1), 1);
21627
21628   return (reg_overlap_mentioned_p (mul_result, mac_acc)
21629           && !reg_overlap_mentioned_p (mul_result, mac_op0)
21630           && !reg_overlap_mentioned_p (mul_result, mac_op1));
21631 }
21632
21633
21634 /* The EABI says test the least significant bit of a guard variable.  */
21635
21636 static bool
21637 arm_cxx_guard_mask_bit (void)
21638 {
21639   return TARGET_AAPCS_BASED;
21640 }
21641
21642
21643 /* The EABI specifies that all array cookies are 8 bytes long.  */
21644
21645 static tree
21646 arm_get_cookie_size (tree type)
21647 {
21648   tree size;
21649
21650   if (!TARGET_AAPCS_BASED)
21651     return default_cxx_get_cookie_size (type);
21652
21653   size = build_int_cst (sizetype, 8);
21654   return size;
21655 }
21656
21657
21658 /* The EABI says that array cookies should also contain the element size.  */
21659
21660 static bool
21661 arm_cookie_has_size (void)
21662 {
21663   return TARGET_AAPCS_BASED;
21664 }
21665
21666
21667 /* The EABI says constructors and destructors should return a pointer to
21668    the object constructed/destroyed.  */
21669
21670 static bool
21671 arm_cxx_cdtor_returns_this (void)
21672 {
21673   return TARGET_AAPCS_BASED;
21674 }
21675
21676 /* The EABI says that an inline function may never be the key
21677    method.  */
21678
21679 static bool
21680 arm_cxx_key_method_may_be_inline (void)
21681 {
21682   return !TARGET_AAPCS_BASED;
21683 }
21684
21685 static void
21686 arm_cxx_determine_class_data_visibility (tree decl)
21687 {
21688   if (!TARGET_AAPCS_BASED
21689       || !TARGET_DLLIMPORT_DECL_ATTRIBUTES)
21690     return;
21691
21692   /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
21693      is exported.  However, on systems without dynamic vague linkage,
21694      \S 3.2.5.6 says that COMDAT class data has hidden linkage.  */
21695   if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
21696     DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
21697   else
21698     DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
21699   DECL_VISIBILITY_SPECIFIED (decl) = 1;
21700 }
21701
21702 static bool
21703 arm_cxx_class_data_always_comdat (void)
21704 {
21705   /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
21706      vague linkage if the class has no key function.  */
21707   return !TARGET_AAPCS_BASED;
21708 }
21709
21710
21711 /* The EABI says __aeabi_atexit should be used to register static
21712    destructors.  */
21713
21714 static bool
21715 arm_cxx_use_aeabi_atexit (void)
21716 {
21717   return TARGET_AAPCS_BASED;
21718 }
21719
21720
21721 void
21722 arm_set_return_address (rtx source, rtx scratch)
21723 {
21724   arm_stack_offsets *offsets;
21725   HOST_WIDE_INT delta;
21726   rtx addr;
21727   unsigned long saved_regs;
21728
21729   offsets = arm_get_frame_offsets ();
21730   saved_regs = offsets->saved_regs_mask;
21731
21732   if ((saved_regs & (1 << LR_REGNUM)) == 0)
21733     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
21734   else
21735     {
21736       if (frame_pointer_needed)
21737         addr = plus_constant(hard_frame_pointer_rtx, -4);
21738       else
21739         {
21740           /* LR will be the first saved register.  */
21741           delta = offsets->outgoing_args - (offsets->frame + 4);
21742
21743
21744           if (delta >= 4096)
21745             {
21746               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
21747                                      GEN_INT (delta & ~4095)));
21748               addr = scratch;
21749               delta &= 4095;
21750             }
21751           else
21752             addr = stack_pointer_rtx;
21753
21754           addr = plus_constant (addr, delta);
21755         }
21756       emit_move_insn (gen_frame_mem (Pmode, addr), source);
21757     }
21758 }
21759
21760
21761 void
21762 thumb_set_return_address (rtx source, rtx scratch)
21763 {
21764   arm_stack_offsets *offsets;
21765   HOST_WIDE_INT delta;
21766   HOST_WIDE_INT limit;
21767   int reg;
21768   rtx addr;
21769   unsigned long mask;
21770
21771   emit_use (source);
21772
21773   offsets = arm_get_frame_offsets ();
21774   mask = offsets->saved_regs_mask;
21775   if (mask & (1 << LR_REGNUM))
21776     {
21777       limit = 1024;
21778       /* Find the saved regs.  */
21779       if (frame_pointer_needed)
21780         {
21781           delta = offsets->soft_frame - offsets->saved_args;
21782           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
21783           if (TARGET_THUMB1)
21784             limit = 128;
21785         }
21786       else
21787         {
21788           delta = offsets->outgoing_args - offsets->saved_args;
21789           reg = SP_REGNUM;
21790         }
21791       /* Allow for the stack frame.  */
21792       if (TARGET_THUMB1 && TARGET_BACKTRACE)
21793         delta -= 16;
21794       /* The link register is always the first saved register.  */
21795       delta -= 4;
21796
21797       /* Construct the address.  */
21798       addr = gen_rtx_REG (SImode, reg);
21799       if (delta > limit)
21800         {
21801           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
21802           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
21803           addr = scratch;
21804         }
21805       else
21806         addr = plus_constant (addr, delta);
21807
21808       emit_move_insn (gen_frame_mem (Pmode, addr), source);
21809     }
21810   else
21811     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
21812 }
21813
21814 /* Implements target hook vector_mode_supported_p.  */
21815 bool
21816 arm_vector_mode_supported_p (enum machine_mode mode)
21817 {
21818   /* Neon also supports V2SImode, etc. listed in the clause below.  */
21819   if (TARGET_NEON && (mode == V2SFmode || mode == V4SImode || mode == V8HImode
21820       || mode == V16QImode || mode == V4SFmode || mode == V2DImode))
21821     return true;
21822
21823   if ((TARGET_NEON || TARGET_IWMMXT)
21824       && ((mode == V2SImode)
21825           || (mode == V4HImode)
21826           || (mode == V8QImode)))
21827     return true;
21828
21829   return false;
21830 }
21831
21832 /* Implements target hook small_register_classes_for_mode_p.  */
21833 bool
21834 arm_small_register_classes_for_mode_p (enum machine_mode mode ATTRIBUTE_UNUSED)
21835 {
21836   return TARGET_THUMB1;
21837 }
21838
21839 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
21840    ARM insns and therefore guarantee that the shift count is modulo 256.
21841    DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
21842    guarantee no particular behavior for out-of-range counts.  */
21843
21844 static unsigned HOST_WIDE_INT
21845 arm_shift_truncation_mask (enum machine_mode mode)
21846 {
21847   return mode == SImode ? 255 : 0;
21848 }
21849
21850
21851 /* Map internal gcc register numbers to DWARF2 register numbers.  */
21852
21853 unsigned int
21854 arm_dbx_register_number (unsigned int regno)
21855 {
21856   if (regno < 16)
21857     return regno;
21858
21859   /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
21860      compatibility.  The EABI defines them as registers 96-103.  */
21861   if (IS_FPA_REGNUM (regno))
21862     return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
21863
21864   if (IS_VFP_REGNUM (regno))
21865     {
21866       /* See comment in arm_dwarf_register_span.  */
21867       if (VFP_REGNO_OK_FOR_SINGLE (regno))
21868         return 64 + regno - FIRST_VFP_REGNUM;
21869       else
21870         return 256 + (regno - FIRST_VFP_REGNUM) / 2;
21871     }
21872
21873   if (IS_IWMMXT_GR_REGNUM (regno))
21874     return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
21875
21876   if (IS_IWMMXT_REGNUM (regno))
21877     return 112 + regno - FIRST_IWMMXT_REGNUM;
21878
21879   gcc_unreachable ();
21880 }
21881
21882 /* Dwarf models VFPv3 registers as 32 64-bit registers.
21883    GCC models tham as 64 32-bit registers, so we need to describe this to
21884    the DWARF generation code.  Other registers can use the default.  */
21885 static rtx
21886 arm_dwarf_register_span (rtx rtl)
21887 {
21888   unsigned regno;
21889   int nregs;
21890   int i;
21891   rtx p;
21892
21893   regno = REGNO (rtl);
21894   if (!IS_VFP_REGNUM (regno))
21895     return NULL_RTX;
21896
21897   /* XXX FIXME: The EABI defines two VFP register ranges:
21898         64-95: Legacy VFPv2 numbering for S0-S31 (obsolescent)
21899         256-287: D0-D31
21900      The recommended encoding for S0-S31 is a DW_OP_bit_piece of the
21901      corresponding D register.  Until GDB supports this, we shall use the
21902      legacy encodings.  We also use these encodings for D0-D15 for
21903      compatibility with older debuggers.  */
21904   if (VFP_REGNO_OK_FOR_SINGLE (regno))
21905     return NULL_RTX;
21906
21907   nregs = GET_MODE_SIZE (GET_MODE (rtl)) / 8;
21908   p = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nregs));
21909   regno = (regno - FIRST_VFP_REGNUM) / 2;
21910   for (i = 0; i < nregs; i++)
21911     XVECEXP (p, 0, i) = gen_rtx_REG (DImode, 256 + regno + i);
21912
21913   return p;
21914 }
21915
21916 #ifdef TARGET_UNWIND_INFO
21917 /* Emit unwind directives for a store-multiple instruction or stack pointer
21918    push during alignment.
21919    These should only ever be generated by the function prologue code, so
21920    expect them to have a particular form.  */
21921
21922 static void
21923 arm_unwind_emit_sequence (FILE * asm_out_file, rtx p)
21924 {
21925   int i;
21926   HOST_WIDE_INT offset;
21927   HOST_WIDE_INT nregs;
21928   int reg_size;
21929   unsigned reg;
21930   unsigned lastreg;
21931   rtx e;
21932
21933   e = XVECEXP (p, 0, 0);
21934   if (GET_CODE (e) != SET)
21935     abort ();
21936
21937   /* First insn will adjust the stack pointer.  */
21938   if (GET_CODE (e) != SET
21939       || GET_CODE (XEXP (e, 0)) != REG
21940       || REGNO (XEXP (e, 0)) != SP_REGNUM
21941       || GET_CODE (XEXP (e, 1)) != PLUS)
21942     abort ();
21943
21944   offset = -INTVAL (XEXP (XEXP (e, 1), 1));
21945   nregs = XVECLEN (p, 0) - 1;
21946
21947   reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
21948   if (reg < 16)
21949     {
21950       /* The function prologue may also push pc, but not annotate it as it is
21951          never restored.  We turn this into a stack pointer adjustment.  */
21952       if (nregs * 4 == offset - 4)
21953         {
21954           fprintf (asm_out_file, "\t.pad #4\n");
21955           offset -= 4;
21956         }
21957       reg_size = 4;
21958       fprintf (asm_out_file, "\t.save {");
21959     }
21960   else if (IS_VFP_REGNUM (reg))
21961     {
21962       reg_size = 8;
21963       fprintf (asm_out_file, "\t.vsave {");
21964     }
21965   else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
21966     {
21967       /* FPA registers are done differently.  */
21968       asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
21969       return;
21970     }
21971   else
21972     /* Unknown register type.  */
21973     abort ();
21974
21975   /* If the stack increment doesn't match the size of the saved registers,
21976      something has gone horribly wrong.  */
21977   if (offset != nregs * reg_size)
21978     abort ();
21979
21980   offset = 0;
21981   lastreg = 0;
21982   /* The remaining insns will describe the stores.  */
21983   for (i = 1; i <= nregs; i++)
21984     {
21985       /* Expect (set (mem <addr>) (reg)).
21986          Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)).  */
21987       e = XVECEXP (p, 0, i);
21988       if (GET_CODE (e) != SET
21989           || GET_CODE (XEXP (e, 0)) != MEM
21990           || GET_CODE (XEXP (e, 1)) != REG)
21991         abort ();
21992
21993       reg = REGNO (XEXP (e, 1));
21994       if (reg < lastreg)
21995         abort ();
21996
21997       if (i != 1)
21998         fprintf (asm_out_file, ", ");
21999       /* We can't use %r for vfp because we need to use the
22000          double precision register names.  */
22001       if (IS_VFP_REGNUM (reg))
22002         asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
22003       else
22004         asm_fprintf (asm_out_file, "%r", reg);
22005
22006 #ifdef ENABLE_CHECKING
22007       /* Check that the addresses are consecutive.  */
22008       e = XEXP (XEXP (e, 0), 0);
22009       if (GET_CODE (e) == PLUS)
22010         {
22011           offset += reg_size;
22012           if (GET_CODE (XEXP (e, 0)) != REG
22013               || REGNO (XEXP (e, 0)) != SP_REGNUM
22014               || GET_CODE (XEXP (e, 1)) != CONST_INT
22015               || offset != INTVAL (XEXP (e, 1)))
22016             abort ();
22017         }
22018       else if (i != 1
22019                || GET_CODE (e) != REG
22020                || REGNO (e) != SP_REGNUM)
22021         abort ();
22022 #endif
22023     }
22024   fprintf (asm_out_file, "}\n");
22025 }
22026
22027 /*  Emit unwind directives for a SET.  */
22028
22029 static void
22030 arm_unwind_emit_set (FILE * asm_out_file, rtx p)
22031 {
22032   rtx e0;
22033   rtx e1;
22034   unsigned reg;
22035
22036   e0 = XEXP (p, 0);
22037   e1 = XEXP (p, 1);
22038   switch (GET_CODE (e0))
22039     {
22040     case MEM:
22041       /* Pushing a single register.  */
22042       if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
22043           || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
22044           || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
22045         abort ();
22046
22047       asm_fprintf (asm_out_file, "\t.save ");
22048       if (IS_VFP_REGNUM (REGNO (e1)))
22049         asm_fprintf(asm_out_file, "{d%d}\n",
22050                     (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
22051       else
22052         asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
22053       break;
22054
22055     case REG:
22056       if (REGNO (e0) == SP_REGNUM)
22057         {
22058           /* A stack increment.  */
22059           if (GET_CODE (e1) != PLUS
22060               || GET_CODE (XEXP (e1, 0)) != REG
22061               || REGNO (XEXP (e1, 0)) != SP_REGNUM
22062               || GET_CODE (XEXP (e1, 1)) != CONST_INT)
22063             abort ();
22064
22065           asm_fprintf (asm_out_file, "\t.pad #%wd\n",
22066                        -INTVAL (XEXP (e1, 1)));
22067         }
22068       else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
22069         {
22070           HOST_WIDE_INT offset;
22071
22072           if (GET_CODE (e1) == PLUS)
22073             {
22074               if (GET_CODE (XEXP (e1, 0)) != REG
22075                   || GET_CODE (XEXP (e1, 1)) != CONST_INT)
22076                 abort ();
22077               reg = REGNO (XEXP (e1, 0));
22078               offset = INTVAL (XEXP (e1, 1));
22079               asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
22080                            HARD_FRAME_POINTER_REGNUM, reg,
22081                            offset);
22082             }
22083           else if (GET_CODE (e1) == REG)
22084             {
22085               reg = REGNO (e1);
22086               asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
22087                            HARD_FRAME_POINTER_REGNUM, reg);
22088             }
22089           else
22090             abort ();
22091         }
22092       else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
22093         {
22094           /* Move from sp to reg.  */
22095           asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
22096         }
22097      else if (GET_CODE (e1) == PLUS
22098               && GET_CODE (XEXP (e1, 0)) == REG
22099               && REGNO (XEXP (e1, 0)) == SP_REGNUM
22100               && GET_CODE (XEXP (e1, 1)) == CONST_INT)
22101         {
22102           /* Set reg to offset from sp.  */
22103           asm_fprintf (asm_out_file, "\t.movsp %r, #%d\n",
22104                        REGNO (e0), (int)INTVAL(XEXP (e1, 1)));
22105         }
22106       else if (GET_CODE (e1) == UNSPEC && XINT (e1, 1) == UNSPEC_STACK_ALIGN)
22107         {
22108           /* Stack pointer save before alignment.  */
22109           reg = REGNO (e0);
22110           asm_fprintf (asm_out_file, "\t.unwind_raw 0, 0x%x @ vsp = r%d\n",
22111                        reg + 0x90, reg);
22112         }
22113       else
22114         abort ();
22115       break;
22116
22117     default:
22118       abort ();
22119     }
22120 }
22121
22122
22123 /* Emit unwind directives for the given insn.  */
22124
22125 static void
22126 arm_unwind_emit (FILE * asm_out_file, rtx insn)
22127 {
22128   rtx pat;
22129
22130   if (!ARM_EABI_UNWIND_TABLES)
22131     return;
22132
22133   if (!(flag_unwind_tables || crtl->uses_eh_lsda)
22134       && (TREE_NOTHROW (current_function_decl)
22135           || crtl->all_throwers_are_sibcalls))
22136     return;
22137
22138   if (GET_CODE (insn) == NOTE || !RTX_FRAME_RELATED_P (insn))
22139     return;
22140
22141   pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
22142   if (pat)
22143     pat = XEXP (pat, 0);
22144   else
22145     pat = PATTERN (insn);
22146
22147   switch (GET_CODE (pat))
22148     {
22149     case SET:
22150       arm_unwind_emit_set (asm_out_file, pat);
22151       break;
22152
22153     case SEQUENCE:
22154       /* Store multiple.  */
22155       arm_unwind_emit_sequence (asm_out_file, pat);
22156       break;
22157
22158     default:
22159       abort();
22160     }
22161 }
22162
22163
22164 /* Output a reference from a function exception table to the type_info
22165    object X.  The EABI specifies that the symbol should be relocated by
22166    an R_ARM_TARGET2 relocation.  */
22167
22168 static bool
22169 arm_output_ttype (rtx x)
22170 {
22171   fputs ("\t.word\t", asm_out_file);
22172   output_addr_const (asm_out_file, x);
22173   /* Use special relocations for symbol references.  */
22174   if (GET_CODE (x) != CONST_INT)
22175     fputs ("(TARGET2)", asm_out_file);
22176   fputc ('\n', asm_out_file);
22177
22178   return TRUE;
22179 }
22180 #endif /* TARGET_UNWIND_INFO */
22181
22182
22183 /* Handle UNSPEC DWARF call frame instructions.  These are needed for dynamic
22184    stack alignment.  */
22185
22186 static void
22187 arm_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
22188 {
22189   rtx unspec = SET_SRC (pattern);
22190   gcc_assert (GET_CODE (unspec) == UNSPEC);
22191
22192   switch (index)
22193     {
22194     case UNSPEC_STACK_ALIGN:
22195       /* ??? We should set the CFA = (SP & ~7).  At this point we haven't
22196          put anything on the stack, so hopefully it won't matter.
22197          CFA = SP will be correct after alignment.  */
22198       dwarf2out_reg_save_reg (label, stack_pointer_rtx,
22199                               SET_DEST (pattern));
22200       break;
22201     default:
22202       gcc_unreachable ();
22203     }
22204 }
22205
22206
22207 /* Output unwind directives for the start/end of a function.  */
22208
22209 void
22210 arm_output_fn_unwind (FILE * f, bool prologue)
22211 {
22212   if (!ARM_EABI_UNWIND_TABLES)
22213     return;
22214
22215   if (prologue)
22216     fputs ("\t.fnstart\n", f);
22217   else
22218     {
22219       /* If this function will never be unwound, then mark it as such.
22220          The came condition is used in arm_unwind_emit to suppress
22221          the frame annotations.  */
22222       if (!(flag_unwind_tables || crtl->uses_eh_lsda)
22223           && (TREE_NOTHROW (current_function_decl)
22224               || crtl->all_throwers_are_sibcalls))
22225         fputs("\t.cantunwind\n", f);
22226
22227       fputs ("\t.fnend\n", f);
22228     }
22229 }
22230
22231 static bool
22232 arm_emit_tls_decoration (FILE *fp, rtx x)
22233 {
22234   enum tls_reloc reloc;
22235   rtx val;
22236
22237   val = XVECEXP (x, 0, 0);
22238   reloc = (enum tls_reloc) INTVAL (XVECEXP (x, 0, 1));
22239
22240   output_addr_const (fp, val);
22241
22242   switch (reloc)
22243     {
22244     case TLS_GD32:
22245       fputs ("(tlsgd)", fp);
22246       break;
22247     case TLS_LDM32:
22248       fputs ("(tlsldm)", fp);
22249       break;
22250     case TLS_LDO32:
22251       fputs ("(tlsldo)", fp);
22252       break;
22253     case TLS_IE32:
22254       fputs ("(gottpoff)", fp);
22255       break;
22256     case TLS_LE32:
22257       fputs ("(tpoff)", fp);
22258       break;
22259     default:
22260       gcc_unreachable ();
22261     }
22262
22263   switch (reloc)
22264     {
22265     case TLS_GD32:
22266     case TLS_LDM32:
22267     case TLS_IE32:
22268       fputs (" + (. - ", fp);
22269       output_addr_const (fp, XVECEXP (x, 0, 2));
22270       fputs (" - ", fp);
22271       output_addr_const (fp, XVECEXP (x, 0, 3));
22272       fputc (')', fp);
22273       break;
22274     default:
22275       break;
22276     }
22277
22278   return TRUE;
22279 }
22280
22281 /* ARM implementation of TARGET_ASM_OUTPUT_DWARF_DTPREL.  */
22282
22283 static void
22284 arm_output_dwarf_dtprel (FILE *file, int size, rtx x)
22285 {
22286   gcc_assert (size == 4);
22287   fputs ("\t.word\t", file);
22288   output_addr_const (file, x);
22289   fputs ("(tlsldo)", file);
22290 }
22291
22292 bool
22293 arm_output_addr_const_extra (FILE *fp, rtx x)
22294 {
22295   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
22296     return arm_emit_tls_decoration (fp, x);
22297   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
22298     {
22299       char label[256];
22300       int labelno = INTVAL (XVECEXP (x, 0, 0));
22301
22302       ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
22303       assemble_name_raw (fp, label);
22304
22305       return TRUE;
22306     }
22307   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_GOTSYM_OFF)
22308     {
22309       assemble_name (fp, "_GLOBAL_OFFSET_TABLE_");
22310       if (GOT_PCREL)
22311         fputs ("+.", fp);
22312       fputs ("-(", fp);
22313       output_addr_const (fp, XVECEXP (x, 0, 0));
22314       fputc (')', fp);
22315       return TRUE;
22316     }
22317   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_SYMBOL_OFFSET)
22318     {
22319       output_addr_const (fp, XVECEXP (x, 0, 0));
22320       if (GOT_PCREL)
22321         fputs ("+.", fp);
22322       fputs ("-(", fp);
22323       output_addr_const (fp, XVECEXP (x, 0, 1));
22324       fputc (')', fp);
22325       return TRUE;
22326     }
22327   else if (GET_CODE (x) == CONST_VECTOR)
22328     return arm_emit_vector_const (fp, x);
22329
22330   return FALSE;
22331 }
22332
22333 /* Output assembly for a shift instruction.
22334    SET_FLAGS determines how the instruction modifies the condition codes.
22335    0 - Do not set condition codes.
22336    1 - Set condition codes.
22337    2 - Use smallest instruction.  */
22338 const char *
22339 arm_output_shift(rtx * operands, int set_flags)
22340 {
22341   char pattern[100];
22342   static const char flag_chars[3] = {'?', '.', '!'};
22343   const char *shift;
22344   HOST_WIDE_INT val;
22345   char c;
22346   
22347   c = flag_chars[set_flags];
22348   if (TARGET_UNIFIED_ASM)
22349     {
22350       shift = shift_op(operands[3], &val);
22351       if (shift)
22352         {
22353           if (val != -1)
22354             operands[2] = GEN_INT(val);
22355           sprintf (pattern, "%s%%%c\t%%0, %%1, %%2", shift, c);
22356         }
22357       else
22358         sprintf (pattern, "mov%%%c\t%%0, %%1", c);
22359     }
22360   else
22361     sprintf (pattern, "mov%%%c\t%%0, %%1%%S3", c);
22362   output_asm_insn (pattern, operands);
22363   return "";
22364 }
22365
22366 /* Output a Thumb-1 casesi dispatch sequence.  */
22367 const char *
22368 thumb1_output_casesi (rtx *operands)
22369 {
22370   rtx diff_vec = PATTERN (next_real_insn (operands[0]));
22371
22372   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
22373
22374   switch (GET_MODE(diff_vec))
22375     {
22376     case QImode:
22377       return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned ? 
22378               "bl\t%___gnu_thumb1_case_uqi" : "bl\t%___gnu_thumb1_case_sqi");
22379     case HImode:
22380       return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned ? 
22381               "bl\t%___gnu_thumb1_case_uhi" : "bl\t%___gnu_thumb1_case_shi");
22382     case SImode:
22383       return "bl\t%___gnu_thumb1_case_si";
22384     default:
22385       gcc_unreachable ();
22386     }
22387 }
22388
22389 /* Output a Thumb-2 casesi instruction.  */
22390 const char *
22391 thumb2_output_casesi (rtx *operands)
22392 {
22393   rtx diff_vec = PATTERN (next_real_insn (operands[2]));
22394
22395   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
22396
22397   output_asm_insn ("cmp\t%0, %1", operands);
22398   output_asm_insn ("bhi\t%l3", operands);
22399   switch (GET_MODE(diff_vec))
22400     {
22401     case QImode:
22402       return "tbb\t[%|pc, %0]";
22403     case HImode:
22404       return "tbh\t[%|pc, %0, lsl #1]";
22405     case SImode:
22406       if (flag_pic)
22407         {
22408           output_asm_insn ("adr\t%4, %l2", operands);
22409           output_asm_insn ("ldr\t%5, [%4, %0, lsl #2]", operands);
22410           output_asm_insn ("add\t%4, %4, %5", operands);
22411           return "bx\t%4";
22412         }
22413       else
22414         {
22415           output_asm_insn ("adr\t%4, %l2", operands);
22416           return "ldr\t%|pc, [%4, %0, lsl #2]";
22417         }
22418     default:
22419       gcc_unreachable ();
22420     }
22421 }
22422
22423 /* Most ARM cores are single issue, but some newer ones can dual issue.
22424    The scheduler descriptions rely on this being correct.  */
22425 static int
22426 arm_issue_rate (void)
22427 {
22428   switch (arm_tune)
22429     {
22430     case cortexr4:
22431     case cortexr4f:
22432     case cortexa8:
22433     case cortexa9:
22434       return 2;
22435
22436     default:
22437       return 1;
22438     }
22439 }
22440
22441 /* A table and a function to perform ARM-specific name mangling for
22442    NEON vector types in order to conform to the AAPCS (see "Procedure
22443    Call Standard for the ARM Architecture", Appendix A).  To qualify
22444    for emission with the mangled names defined in that document, a
22445    vector type must not only be of the correct mode but also be
22446    composed of NEON vector element types (e.g. __builtin_neon_qi).  */
22447 typedef struct
22448 {
22449   enum machine_mode mode;
22450   const char *element_type_name;
22451   const char *aapcs_name;
22452 } arm_mangle_map_entry;
22453
22454 static arm_mangle_map_entry arm_mangle_map[] = {
22455   /* 64-bit containerized types.  */
22456   { V8QImode,  "__builtin_neon_qi",     "15__simd64_int8_t" },
22457   { V8QImode,  "__builtin_neon_uqi",    "16__simd64_uint8_t" },
22458   { V4HImode,  "__builtin_neon_hi",     "16__simd64_int16_t" },
22459   { V4HImode,  "__builtin_neon_uhi",    "17__simd64_uint16_t" },
22460   { V2SImode,  "__builtin_neon_si",     "16__simd64_int32_t" },
22461   { V2SImode,  "__builtin_neon_usi",    "17__simd64_uint32_t" },
22462   { V2SFmode,  "__builtin_neon_sf",     "18__simd64_float32_t" },
22463   { V8QImode,  "__builtin_neon_poly8",  "16__simd64_poly8_t" },
22464   { V4HImode,  "__builtin_neon_poly16", "17__simd64_poly16_t" },
22465   /* 128-bit containerized types.  */
22466   { V16QImode, "__builtin_neon_qi",     "16__simd128_int8_t" },
22467   { V16QImode, "__builtin_neon_uqi",    "17__simd128_uint8_t" },
22468   { V8HImode,  "__builtin_neon_hi",     "17__simd128_int16_t" },
22469   { V8HImode,  "__builtin_neon_uhi",    "18__simd128_uint16_t" },
22470   { V4SImode,  "__builtin_neon_si",     "17__simd128_int32_t" },
22471   { V4SImode,  "__builtin_neon_usi",    "18__simd128_uint32_t" },
22472   { V4SFmode,  "__builtin_neon_sf",     "19__simd128_float32_t" },
22473   { V16QImode, "__builtin_neon_poly8",  "17__simd128_poly8_t" },
22474   { V8HImode,  "__builtin_neon_poly16", "18__simd128_poly16_t" },
22475   { VOIDmode, NULL, NULL }
22476 };
22477
22478 const char *
22479 arm_mangle_type (const_tree type)
22480 {
22481   arm_mangle_map_entry *pos = arm_mangle_map;
22482
22483   /* The ARM ABI documents (10th October 2008) say that "__va_list"
22484      has to be managled as if it is in the "std" namespace.  */
22485   if (TARGET_AAPCS_BASED 
22486       && lang_hooks.types_compatible_p (CONST_CAST_TREE (type), va_list_type))
22487     {
22488       static bool warned;
22489       if (!warned && warn_psabi && !in_system_header)
22490         {
22491           warned = true;
22492           inform (input_location,
22493                   "the mangling of %<va_list%> has changed in GCC 4.4");
22494         }
22495       return "St9__va_list";
22496     }
22497
22498   /* Half-precision float.  */
22499   if (TREE_CODE (type) == REAL_TYPE && TYPE_PRECISION (type) == 16)
22500     return "Dh";
22501
22502   if (TREE_CODE (type) != VECTOR_TYPE)
22503     return NULL;
22504
22505   /* Check the mode of the vector type, and the name of the vector
22506      element type, against the table.  */
22507   while (pos->mode != VOIDmode)
22508     {
22509       tree elt_type = TREE_TYPE (type);
22510
22511       if (pos->mode == TYPE_MODE (type)
22512           && TREE_CODE (TYPE_NAME (elt_type)) == TYPE_DECL
22513           && !strcmp (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (elt_type))),
22514                       pos->element_type_name))
22515         return pos->aapcs_name;
22516
22517       pos++;
22518     }
22519
22520   /* Use the default mangling for unrecognized (possibly user-defined)
22521      vector types.  */
22522   return NULL;
22523 }
22524
22525 /* Order of allocation of core registers for Thumb: this allocation is
22526    written over the corresponding initial entries of the array
22527    initialized with REG_ALLOC_ORDER.  We allocate all low registers
22528    first.  Saving and restoring a low register is usually cheaper than
22529    using a call-clobbered high register.  */
22530
22531 static const int thumb_core_reg_alloc_order[] =
22532 {
22533    3,  2,  1,  0,  4,  5,  6,  7,
22534   14, 12,  8,  9, 10, 11, 13, 15
22535 };
22536
22537 /* Adjust register allocation order when compiling for Thumb.  */
22538
22539 void
22540 arm_order_regs_for_local_alloc (void)
22541 {
22542   const int arm_reg_alloc_order[] = REG_ALLOC_ORDER;
22543   memcpy(reg_alloc_order, arm_reg_alloc_order, sizeof (reg_alloc_order));
22544   if (TARGET_THUMB)
22545     memcpy (reg_alloc_order, thumb_core_reg_alloc_order,
22546             sizeof (thumb_core_reg_alloc_order));
22547 }
22548
22549 /* Set default optimization options.  */
22550 void
22551 arm_optimization_options (int level, int size ATTRIBUTE_UNUSED)
22552 {
22553   /* Enable section anchors by default at -O1 or higher.
22554      Use 2 to distinguish from an explicit -fsection-anchors
22555      given on the command line.  */
22556   if (level > 0)
22557     flag_section_anchors = 2;
22558 }
22559
22560 /* Implement TARGET_FRAME_POINTER_REQUIRED.  */
22561
22562 bool
22563 arm_frame_pointer_required (void)
22564 {
22565   return (cfun->has_nonlocal_label
22566           || SUBTARGET_FRAME_POINTER_REQUIRED
22567           || (TARGET_ARM && TARGET_APCS_FRAME && ! leaf_function_p ()));
22568 }
22569
22570 /* Only thumb1 can't support conditional execution, so return true if
22571    the target is not thumb1.  */
22572 static bool
22573 arm_have_conditional_execution (void)
22574 {
22575   return !TARGET_THUMB1;
22576 }
22577
22578 #include "gt-arm.h"