OSDN Git Service

2010-09-02 Ryan Mansfield <rmansfield@qnx.com>
[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 arm_function_arg (CUMULATIVE_ARGS *, enum machine_mode,
168                              const_tree, bool);
169 static void arm_function_arg_advance (CUMULATIVE_ARGS *, enum machine_mode,
170                                       const_tree, bool);
171 static rtx aapcs_allocate_return_reg (enum machine_mode, const_tree,
172                                       const_tree);
173 static int aapcs_select_return_coproc (const_tree, const_tree);
174
175 #ifdef OBJECT_FORMAT_ELF
176 static void arm_elf_asm_constructor (rtx, int) ATTRIBUTE_UNUSED;
177 static void arm_elf_asm_destructor (rtx, int) ATTRIBUTE_UNUSED;
178 #endif
179 #ifndef ARM_PE
180 static void arm_encode_section_info (tree, rtx, int);
181 #endif
182
183 static void arm_file_end (void);
184 static void arm_file_start (void);
185
186 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
187                                         tree, int *, int);
188 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
189                                    enum machine_mode, const_tree, bool);
190 static bool arm_promote_prototypes (const_tree);
191 static bool arm_default_short_enums (void);
192 static bool arm_align_anon_bitfield (void);
193 static bool arm_return_in_msb (const_tree);
194 static bool arm_must_pass_in_stack (enum machine_mode, const_tree);
195 static bool arm_return_in_memory (const_tree, const_tree);
196 #ifdef TARGET_UNWIND_INFO
197 static void arm_unwind_emit (FILE *, rtx);
198 static bool arm_output_ttype (rtx);
199 #endif
200 static void arm_dwarf_handle_frame_unspec (const char *, rtx, int);
201 static rtx arm_dwarf_register_span (rtx);
202
203 static tree arm_cxx_guard_type (void);
204 static bool arm_cxx_guard_mask_bit (void);
205 static tree arm_get_cookie_size (tree);
206 static bool arm_cookie_has_size (void);
207 static bool arm_cxx_cdtor_returns_this (void);
208 static bool arm_cxx_key_method_may_be_inline (void);
209 static void arm_cxx_determine_class_data_visibility (tree);
210 static bool arm_cxx_class_data_always_comdat (void);
211 static bool arm_cxx_use_aeabi_atexit (void);
212 static void arm_init_libfuncs (void);
213 static tree arm_build_builtin_va_list (void);
214 static void arm_expand_builtin_va_start (tree, rtx);
215 static tree arm_gimplify_va_arg_expr (tree, tree, gimple_seq *, gimple_seq *);
216 static bool arm_handle_option (size_t, const char *, int);
217 static void arm_target_help (void);
218 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
219 static bool arm_cannot_copy_insn_p (rtx);
220 static bool arm_tls_symbol_p (rtx x);
221 static int arm_issue_rate (void);
222 static void arm_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
223 static bool arm_allocate_stack_slots_for_args (void);
224 static const char *arm_invalid_parameter_type (const_tree t);
225 static const char *arm_invalid_return_type (const_tree t);
226 static tree arm_promoted_type (const_tree t);
227 static tree arm_convert_to_type (tree type, tree expr);
228 static bool arm_scalar_mode_supported_p (enum machine_mode);
229 static bool arm_frame_pointer_required (void);
230 static bool arm_can_eliminate (const int, const int);
231 static void arm_asm_trampoline_template (FILE *);
232 static void arm_trampoline_init (rtx, tree, rtx);
233 static rtx arm_trampoline_adjust_address (rtx);
234 static rtx arm_pic_static_addr (rtx orig, rtx reg);
235 static bool cortex_a9_sched_adjust_cost (rtx, rtx, rtx, int *);
236 static bool xscale_sched_adjust_cost (rtx, rtx, rtx, int *);
237
238 \f
239 /* Table of machine attributes.  */
240 static const struct attribute_spec arm_attribute_table[] =
241 {
242   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
243   /* Function calls made to this symbol must be done indirectly, because
244      it may lie outside of the 26 bit addressing range of a normal function
245      call.  */
246   { "long_call",    0, 0, false, true,  true,  NULL },
247   /* Whereas these functions are always known to reside within the 26 bit
248      addressing range.  */
249   { "short_call",   0, 0, false, true,  true,  NULL },
250   /* Specify the procedure call conventions for a function.  */
251   { "pcs",          1, 1, false, true,  true,  arm_handle_pcs_attribute },
252   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
253   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
254   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
255   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
256 #ifdef ARM_PE
257   /* ARM/PE has three new attributes:
258      interfacearm - ?
259      dllexport - for exporting a function/variable that will live in a dll
260      dllimport - for importing a function/variable from a dll
261
262      Microsoft allows multiple declspecs in one __declspec, separating
263      them with spaces.  We do NOT support this.  Instead, use __declspec
264      multiple times.
265   */
266   { "dllimport",    0, 0, true,  false, false, NULL },
267   { "dllexport",    0, 0, true,  false, false, NULL },
268   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
269 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
270   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
271   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
272   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
273 #endif
274   { NULL,           0, 0, false, false, false, NULL }
275 };
276 \f
277 /* Initialize the GCC target structure.  */
278 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
279 #undef  TARGET_MERGE_DECL_ATTRIBUTES
280 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
281 #endif
282
283 #undef TARGET_LEGITIMIZE_ADDRESS
284 #define TARGET_LEGITIMIZE_ADDRESS arm_legitimize_address
285
286 #undef  TARGET_ATTRIBUTE_TABLE
287 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
288
289 #undef TARGET_ASM_FILE_START
290 #define TARGET_ASM_FILE_START arm_file_start
291 #undef TARGET_ASM_FILE_END
292 #define TARGET_ASM_FILE_END arm_file_end
293
294 #undef  TARGET_ASM_ALIGNED_SI_OP
295 #define TARGET_ASM_ALIGNED_SI_OP NULL
296 #undef  TARGET_ASM_INTEGER
297 #define TARGET_ASM_INTEGER arm_assemble_integer
298
299 #undef TARGET_PRINT_OPERAND
300 #define TARGET_PRINT_OPERAND arm_print_operand
301 #undef TARGET_PRINT_OPERAND_ADDRESS
302 #define TARGET_PRINT_OPERAND_ADDRESS arm_print_operand_address
303 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
304 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P arm_print_operand_punct_valid_p
305
306 #undef  TARGET_ASM_FUNCTION_PROLOGUE
307 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
308
309 #undef  TARGET_ASM_FUNCTION_EPILOGUE
310 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
311
312 #undef  TARGET_DEFAULT_TARGET_FLAGS
313 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
314 #undef  TARGET_HANDLE_OPTION
315 #define TARGET_HANDLE_OPTION arm_handle_option
316 #undef  TARGET_HELP
317 #define TARGET_HELP arm_target_help
318
319 #undef  TARGET_COMP_TYPE_ATTRIBUTES
320 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
321
322 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
323 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
324
325 #undef  TARGET_SCHED_ADJUST_COST
326 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
327
328 #undef TARGET_ENCODE_SECTION_INFO
329 #ifdef ARM_PE
330 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
331 #else
332 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
333 #endif
334
335 #undef  TARGET_STRIP_NAME_ENCODING
336 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
337
338 #undef  TARGET_ASM_INTERNAL_LABEL
339 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
340
341 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
342 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
343
344 #undef  TARGET_FUNCTION_VALUE
345 #define TARGET_FUNCTION_VALUE arm_function_value
346
347 #undef  TARGET_LIBCALL_VALUE
348 #define TARGET_LIBCALL_VALUE arm_libcall_value
349
350 #undef  TARGET_ASM_OUTPUT_MI_THUNK
351 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
352 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
353 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
354
355 #undef  TARGET_RTX_COSTS
356 #define TARGET_RTX_COSTS arm_rtx_costs
357 #undef  TARGET_ADDRESS_COST
358 #define TARGET_ADDRESS_COST arm_address_cost
359
360 #undef TARGET_SHIFT_TRUNCATION_MASK
361 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
362 #undef TARGET_VECTOR_MODE_SUPPORTED_P
363 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
364
365 #undef  TARGET_MACHINE_DEPENDENT_REORG
366 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
367
368 #undef  TARGET_INIT_BUILTINS
369 #define TARGET_INIT_BUILTINS  arm_init_builtins
370 #undef  TARGET_EXPAND_BUILTIN
371 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
372
373 #undef TARGET_INIT_LIBFUNCS
374 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
375
376 #undef TARGET_PROMOTE_FUNCTION_MODE
377 #define TARGET_PROMOTE_FUNCTION_MODE arm_promote_function_mode
378 #undef TARGET_PROMOTE_PROTOTYPES
379 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
380 #undef TARGET_PASS_BY_REFERENCE
381 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
382 #undef TARGET_ARG_PARTIAL_BYTES
383 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
384 #undef TARGET_FUNCTION_ARG
385 #define TARGET_FUNCTION_ARG arm_function_arg
386 #undef TARGET_FUNCTION_ARG_ADVANCE
387 #define TARGET_FUNCTION_ARG_ADVANCE arm_function_arg_advance
388
389 #undef  TARGET_SETUP_INCOMING_VARARGS
390 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
391
392 #undef TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS
393 #define TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS arm_allocate_stack_slots_for_args
394
395 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
396 #define TARGET_ASM_TRAMPOLINE_TEMPLATE arm_asm_trampoline_template
397 #undef TARGET_TRAMPOLINE_INIT
398 #define TARGET_TRAMPOLINE_INIT arm_trampoline_init
399 #undef TARGET_TRAMPOLINE_ADJUST_ADDRESS
400 #define TARGET_TRAMPOLINE_ADJUST_ADDRESS arm_trampoline_adjust_address
401
402 #undef TARGET_DEFAULT_SHORT_ENUMS
403 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
404
405 #undef TARGET_ALIGN_ANON_BITFIELD
406 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
407
408 #undef TARGET_NARROW_VOLATILE_BITFIELD
409 #define TARGET_NARROW_VOLATILE_BITFIELD hook_bool_void_false
410
411 #undef TARGET_CXX_GUARD_TYPE
412 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
413
414 #undef TARGET_CXX_GUARD_MASK_BIT
415 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
416
417 #undef TARGET_CXX_GET_COOKIE_SIZE
418 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
419
420 #undef TARGET_CXX_COOKIE_HAS_SIZE
421 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
422
423 #undef TARGET_CXX_CDTOR_RETURNS_THIS
424 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
425
426 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
427 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
428
429 #undef TARGET_CXX_USE_AEABI_ATEXIT
430 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
431
432 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
433 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
434   arm_cxx_determine_class_data_visibility
435
436 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
437 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
438
439 #undef TARGET_RETURN_IN_MSB
440 #define TARGET_RETURN_IN_MSB arm_return_in_msb
441
442 #undef TARGET_RETURN_IN_MEMORY
443 #define TARGET_RETURN_IN_MEMORY arm_return_in_memory
444
445 #undef TARGET_MUST_PASS_IN_STACK
446 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
447
448 #ifdef TARGET_UNWIND_INFO
449 #undef TARGET_ASM_UNWIND_EMIT
450 #define TARGET_ASM_UNWIND_EMIT arm_unwind_emit
451
452 /* EABI unwinding tables use a different format for the typeinfo tables.  */
453 #undef TARGET_ASM_TTYPE
454 #define TARGET_ASM_TTYPE arm_output_ttype
455
456 #undef TARGET_ARM_EABI_UNWINDER
457 #define TARGET_ARM_EABI_UNWINDER true
458 #endif /* TARGET_UNWIND_INFO */
459
460 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
461 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC arm_dwarf_handle_frame_unspec
462
463 #undef TARGET_DWARF_REGISTER_SPAN
464 #define TARGET_DWARF_REGISTER_SPAN arm_dwarf_register_span
465
466 #undef  TARGET_CANNOT_COPY_INSN_P
467 #define TARGET_CANNOT_COPY_INSN_P arm_cannot_copy_insn_p
468
469 #ifdef HAVE_AS_TLS
470 #undef TARGET_HAVE_TLS
471 #define TARGET_HAVE_TLS true
472 #endif
473
474 #undef TARGET_HAVE_CONDITIONAL_EXECUTION
475 #define TARGET_HAVE_CONDITIONAL_EXECUTION arm_have_conditional_execution
476
477 #undef TARGET_CANNOT_FORCE_CONST_MEM
478 #define TARGET_CANNOT_FORCE_CONST_MEM arm_cannot_force_const_mem
479
480 #undef TARGET_MAX_ANCHOR_OFFSET
481 #define TARGET_MAX_ANCHOR_OFFSET 4095
482
483 /* The minimum is set such that the total size of the block
484    for a particular anchor is -4088 + 1 + 4095 bytes, which is
485    divisible by eight, ensuring natural spacing of anchors.  */
486 #undef TARGET_MIN_ANCHOR_OFFSET
487 #define TARGET_MIN_ANCHOR_OFFSET -4088
488
489 #undef TARGET_SCHED_ISSUE_RATE
490 #define TARGET_SCHED_ISSUE_RATE arm_issue_rate
491
492 #undef TARGET_MANGLE_TYPE
493 #define TARGET_MANGLE_TYPE arm_mangle_type
494
495 #undef TARGET_BUILD_BUILTIN_VA_LIST
496 #define TARGET_BUILD_BUILTIN_VA_LIST arm_build_builtin_va_list
497 #undef TARGET_EXPAND_BUILTIN_VA_START
498 #define TARGET_EXPAND_BUILTIN_VA_START arm_expand_builtin_va_start
499 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
500 #define TARGET_GIMPLIFY_VA_ARG_EXPR arm_gimplify_va_arg_expr
501
502 #ifdef HAVE_AS_TLS
503 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
504 #define TARGET_ASM_OUTPUT_DWARF_DTPREL arm_output_dwarf_dtprel
505 #endif
506
507 #undef TARGET_LEGITIMATE_ADDRESS_P
508 #define TARGET_LEGITIMATE_ADDRESS_P     arm_legitimate_address_p
509
510 #undef TARGET_INVALID_PARAMETER_TYPE
511 #define TARGET_INVALID_PARAMETER_TYPE arm_invalid_parameter_type
512
513 #undef TARGET_INVALID_RETURN_TYPE
514 #define TARGET_INVALID_RETURN_TYPE arm_invalid_return_type
515
516 #undef TARGET_PROMOTED_TYPE
517 #define TARGET_PROMOTED_TYPE arm_promoted_type
518
519 #undef TARGET_CONVERT_TO_TYPE
520 #define TARGET_CONVERT_TO_TYPE arm_convert_to_type
521
522 #undef TARGET_SCALAR_MODE_SUPPORTED_P
523 #define TARGET_SCALAR_MODE_SUPPORTED_P arm_scalar_mode_supported_p
524
525 #undef TARGET_FRAME_POINTER_REQUIRED
526 #define TARGET_FRAME_POINTER_REQUIRED arm_frame_pointer_required
527
528 #undef TARGET_CAN_ELIMINATE
529 #define TARGET_CAN_ELIMINATE arm_can_eliminate
530
531 struct gcc_target targetm = TARGET_INITIALIZER;
532 \f
533 /* Obstack for minipool constant handling.  */
534 static struct obstack minipool_obstack;
535 static char *         minipool_startobj;
536
537 /* The maximum number of insns skipped which
538    will be conditionalised if possible.  */
539 static int max_insns_skipped = 5;
540
541 extern FILE * asm_out_file;
542
543 /* True if we are currently building a constant table.  */
544 int making_const_table;
545
546 /* The processor for which instructions should be scheduled.  */
547 enum processor_type arm_tune = arm_none;
548
549 /* The current tuning set.  */
550 const struct tune_params *current_tune;
551
552 /* Which floating point hardware to schedule for.  */
553 int arm_fpu_attr;
554
555 /* Which floating popint hardware to use.  */
556 const struct arm_fpu_desc *arm_fpu_desc;
557
558 /* Whether to use floating point hardware.  */
559 enum float_abi_type arm_float_abi;
560
561 /* Which __fp16 format to use.  */
562 enum arm_fp16_format_type arm_fp16_format;
563
564 /* Which ABI to use.  */
565 enum arm_abi_type arm_abi;
566
567 /* Which thread pointer model to use.  */
568 enum arm_tp_type target_thread_pointer = TP_AUTO;
569
570 /* Used to parse -mstructure_size_boundary command line option.  */
571 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
572
573 /* Used for Thumb call_via trampolines.  */
574 rtx thumb_call_via_label[14];
575 static int thumb_call_reg_needed;
576
577 /* Bit values used to identify processor capabilities.  */
578 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
579 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
580 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
581 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
582 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
583 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
584 #define FL_THUMB      (1 << 6)        /* Thumb aware */
585 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
586 #define FL_STRONG     (1 << 8)        /* StrongARM */
587 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
588 #define FL_XSCALE     (1 << 10)       /* XScale */
589 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
590 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
591                                          media instructions.  */
592 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
593 #define FL_WBUF       (1 << 14)       /* Schedule for write buffer ops.
594                                          Note: ARM6 & 7 derivatives only.  */
595 #define FL_ARCH6K     (1 << 15)       /* Architecture rel 6 K extensions.  */
596 #define FL_THUMB2     (1 << 16)       /* Thumb-2.  */
597 #define FL_NOTM       (1 << 17)       /* Instructions not present in the 'M'
598                                          profile.  */
599 #define FL_DIV        (1 << 18)       /* Hardware divide.  */
600 #define FL_VFPV3      (1 << 19)       /* Vector Floating Point V3.  */
601 #define FL_NEON       (1 << 20)       /* Neon instructions.  */
602 #define FL_ARCH7EM    (1 << 21)       /* Instructions present in the ARMv7E-M
603                                          architecture.  */
604 #define FL_ARCH7      (1 << 22)       /* Architecture 7.  */
605
606 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
607
608 /* Flags that only effect tuning, not available instructions.  */
609 #define FL_TUNE         (FL_WBUF | FL_VFPV2 | FL_STRONG | FL_LDSCHED \
610                          | FL_CO_PROC)
611
612 #define FL_FOR_ARCH2    FL_NOTM
613 #define FL_FOR_ARCH3    (FL_FOR_ARCH2 | FL_MODE32)
614 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
615 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
616 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
617 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
618 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
619 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
620 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
621 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
622 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
623 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
624 #define FL_FOR_ARCH6K   (FL_FOR_ARCH6 | FL_ARCH6K)
625 #define FL_FOR_ARCH6Z   FL_FOR_ARCH6
626 #define FL_FOR_ARCH6ZK  FL_FOR_ARCH6K
627 #define FL_FOR_ARCH6T2  (FL_FOR_ARCH6 | FL_THUMB2)
628 #define FL_FOR_ARCH6M   (FL_FOR_ARCH6 & ~FL_NOTM)
629 #define FL_FOR_ARCH7    ((FL_FOR_ARCH6T2 & ~FL_NOTM) | FL_ARCH7)
630 #define FL_FOR_ARCH7A   (FL_FOR_ARCH7 | FL_NOTM | FL_ARCH6K)
631 #define FL_FOR_ARCH7R   (FL_FOR_ARCH7A | FL_DIV)
632 #define FL_FOR_ARCH7M   (FL_FOR_ARCH7 | FL_DIV)
633 #define FL_FOR_ARCH7EM  (FL_FOR_ARCH7M | FL_ARCH7EM)
634
635 /* The bits in this mask specify which
636    instructions we are allowed to generate.  */
637 static unsigned long insn_flags = 0;
638
639 /* The bits in this mask specify which instruction scheduling options should
640    be used.  */
641 static unsigned long tune_flags = 0;
642
643 /* The following are used in the arm.md file as equivalents to bits
644    in the above two flag variables.  */
645
646 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
647 int arm_arch3m = 0;
648
649 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
650 int arm_arch4 = 0;
651
652 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
653 int arm_arch4t = 0;
654
655 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
656 int arm_arch5 = 0;
657
658 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
659 int arm_arch5e = 0;
660
661 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
662 int arm_arch6 = 0;
663
664 /* Nonzero if this chip supports the ARM 6K extensions.  */
665 int arm_arch6k = 0;
666
667 /* Nonzero if this chip supports the ARM 7 extensions.  */
668 int arm_arch7 = 0;
669
670 /* Nonzero if instructions not present in the 'M' profile can be used.  */
671 int arm_arch_notm = 0;
672
673 /* Nonzero if instructions present in ARMv7E-M can be used.  */
674 int arm_arch7em = 0;
675
676 /* Nonzero if this chip can benefit from load scheduling.  */
677 int arm_ld_sched = 0;
678
679 /* Nonzero if this chip is a StrongARM.  */
680 int arm_tune_strongarm = 0;
681
682 /* Nonzero if this chip is a Cirrus variant.  */
683 int arm_arch_cirrus = 0;
684
685 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
686 int arm_arch_iwmmxt = 0;
687
688 /* Nonzero if this chip is an XScale.  */
689 int arm_arch_xscale = 0;
690
691 /* Nonzero if tuning for XScale  */
692 int arm_tune_xscale = 0;
693
694 /* Nonzero if we want to tune for stores that access the write-buffer.
695    This typically means an ARM6 or ARM7 with MMU or MPU.  */
696 int arm_tune_wbuf = 0;
697
698 /* Nonzero if tuning for Cortex-A9.  */
699 int arm_tune_cortex_a9 = 0;
700
701 /* Nonzero if generating Thumb instructions.  */
702 int thumb_code = 0;
703
704 /* Nonzero if generating Thumb-1 instructions.  */
705 int thumb1_code = 0;
706
707 /* Nonzero if we should define __THUMB_INTERWORK__ in the
708    preprocessor.
709    XXX This is a bit of a hack, it's intended to help work around
710    problems in GLD which doesn't understand that armv5t code is
711    interworking clean.  */
712 int arm_cpp_interwork = 0;
713
714 /* Nonzero if chip supports Thumb 2.  */
715 int arm_arch_thumb2;
716
717 /* Nonzero if chip supports integer division instruction.  */
718 int arm_arch_hwdiv;
719
720 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference,
721    we must report the mode of the memory reference from
722    TARGET_PRINT_OPERAND to TARGET_PRINT_OPERAND_ADDRESS.  */
723 enum machine_mode output_memory_reference_mode;
724
725 /* The register number to be used for the PIC offset register.  */
726 unsigned arm_pic_register = INVALID_REGNUM;
727
728 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
729    the next function.  */
730 static int after_arm_reorg = 0;
731
732 enum arm_pcs arm_pcs_default;
733
734 /* For an explanation of these variables, see final_prescan_insn below.  */
735 int arm_ccfsm_state;
736 /* arm_current_cc is also used for Thumb-2 cond_exec blocks.  */
737 enum arm_cond_code arm_current_cc;
738
739 rtx arm_target_insn;
740 int arm_target_label;
741 /* The number of conditionally executed insns, including the current insn.  */
742 int arm_condexec_count = 0;
743 /* A bitmask specifying the patterns for the IT block.
744    Zero means do not output an IT block before this insn. */
745 int arm_condexec_mask = 0;
746 /* The number of bits used in arm_condexec_mask.  */
747 int arm_condexec_masklen = 0;
748
749 /* The condition codes of the ARM, and the inverse function.  */
750 static const char * const arm_condition_codes[] =
751 {
752   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
753   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
754 };
755
756 /* The register numbers in sequence, for passing to arm_gen_load_multiple.  */
757 int arm_regs_in_sequence[] =
758 {
759   0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
760 };
761
762 #define ARM_LSL_NAME (TARGET_UNIFIED_ASM ? "lsl" : "asl")
763 #define streq(string1, string2) (strcmp (string1, string2) == 0)
764
765 #define THUMB2_WORK_REGS (0xff & ~(  (1 << THUMB_HARD_FRAME_POINTER_REGNUM) \
766                                    | (1 << SP_REGNUM) | (1 << PC_REGNUM) \
767                                    | (1 << PIC_OFFSET_TABLE_REGNUM)))
768 \f
769 /* Initialization code.  */
770
771 struct processors
772 {
773   const char *const name;
774   enum processor_type core;
775   const char *arch;
776   const unsigned long flags;
777   const struct tune_params *const tune;
778 };
779
780 const struct tune_params arm_slowmul_tune =
781 {
782   arm_slowmul_rtx_costs,
783   NULL,
784   3
785 };
786
787 const struct tune_params arm_fastmul_tune =
788 {
789   arm_fastmul_rtx_costs,
790   NULL,
791   1
792 };
793
794 const struct tune_params arm_xscale_tune =
795 {
796   arm_xscale_rtx_costs,
797   xscale_sched_adjust_cost,
798   2
799 };
800
801 const struct tune_params arm_9e_tune =
802 {
803   arm_9e_rtx_costs,
804   NULL,
805   1
806 };
807
808 const struct tune_params arm_cortex_a9_tune =
809 {
810   arm_9e_rtx_costs,
811   cortex_a9_sched_adjust_cost,
812   1
813 };
814
815
816 /* Not all of these give usefully different compilation alternatives,
817    but there is no simple way of generalizing them.  */
818 static const struct processors all_cores[] =
819 {
820   /* ARM Cores */
821 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
822   {NAME, IDENT, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, &arm_##COSTS##_tune},
823 #include "arm-cores.def"
824 #undef ARM_CORE
825   {NULL, arm_none, NULL, 0, NULL}
826 };
827
828 static const struct processors all_architectures[] =
829 {
830   /* ARM Architectures */
831   /* We don't specify tuning costs here as it will be figured out
832      from the core.  */
833
834   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
835   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
836   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
837   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
838   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
839   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
840      implementations that support it, so we will leave it out for now.  */
841   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
842   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
843   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
844   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
845   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
846   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
847   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
848   {"armv6k",  mpcore,     "6K",  FL_CO_PROC |             FL_FOR_ARCH6K, NULL},
849   {"armv6z",  arm1176jzs, "6Z",  FL_CO_PROC |             FL_FOR_ARCH6Z, NULL},
850   {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC |             FL_FOR_ARCH6ZK, NULL},
851   {"armv6t2", arm1156t2s, "6T2", FL_CO_PROC |             FL_FOR_ARCH6T2, NULL},
852   {"armv6-m", cortexm1,   "6M",                           FL_FOR_ARCH6M, NULL},
853   {"armv7",   cortexa8,   "7",   FL_CO_PROC |             FL_FOR_ARCH7, NULL},
854   {"armv7-a", cortexa8,   "7A",  FL_CO_PROC |             FL_FOR_ARCH7A, NULL},
855   {"armv7-r", cortexr4,   "7R",  FL_CO_PROC |             FL_FOR_ARCH7R, NULL},
856   {"armv7-m", cortexm3,   "7M",  FL_CO_PROC |             FL_FOR_ARCH7M, NULL},
857   {"armv7e-m", cortexm4,  "7EM", FL_CO_PROC |             FL_FOR_ARCH7EM, NULL},
858   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
859   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
860   {"iwmmxt2", iwmmxt2,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
861   {NULL, arm_none, NULL, 0 , NULL}
862 };
863
864
865 /* These are populated as commandline arguments are processed, or NULL
866    if not specified.  */
867 static const struct processors *arm_selected_arch;
868 static const struct processors *arm_selected_cpu;
869 static const struct processors *arm_selected_tune;
870
871 /* The name of the preprocessor macro to define for this architecture.  */
872
873 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
874
875 /* Available values for -mfpu=.  */
876
877 static const struct arm_fpu_desc all_fpus[] =
878 {
879   {"fpa",               ARM_FP_MODEL_FPA, 0, VFP_NONE, false, false},
880   {"fpe2",              ARM_FP_MODEL_FPA, 2, VFP_NONE, false, false},
881   {"fpe3",              ARM_FP_MODEL_FPA, 3, VFP_NONE, false, false},
882   {"maverick",          ARM_FP_MODEL_MAVERICK, 0, VFP_NONE, false, false},
883   {"vfp",               ARM_FP_MODEL_VFP, 2, VFP_REG_D16, false, false},
884   {"vfpv3",             ARM_FP_MODEL_VFP, 3, VFP_REG_D32, false, false},
885   {"vfpv3-fp16",        ARM_FP_MODEL_VFP, 3, VFP_REG_D32, false, true},
886   {"vfpv3-d16",         ARM_FP_MODEL_VFP, 3, VFP_REG_D16, false, false},
887   {"vfpv3-d16-fp16",    ARM_FP_MODEL_VFP, 3, VFP_REG_D16, false, true},
888   {"vfpv3xd",           ARM_FP_MODEL_VFP, 3, VFP_REG_SINGLE, false, false},
889   {"vfpv3xd-fp16",      ARM_FP_MODEL_VFP, 3, VFP_REG_SINGLE, false, true},
890   {"neon",              ARM_FP_MODEL_VFP, 3, VFP_REG_D32, true , false},
891   {"neon-fp16",         ARM_FP_MODEL_VFP, 3, VFP_REG_D32, true , true },
892   {"vfpv4",             ARM_FP_MODEL_VFP, 4, VFP_REG_D32, false, true},
893   {"vfpv4-d16",         ARM_FP_MODEL_VFP, 4, VFP_REG_D16, false, true},
894   {"fpv4-sp-d16",       ARM_FP_MODEL_VFP, 4, VFP_REG_SINGLE, false, true},
895   {"neon-vfpv4",        ARM_FP_MODEL_VFP, 4, VFP_REG_D32, true, true},
896   /* Compatibility aliases.  */
897   {"vfp3",              ARM_FP_MODEL_VFP, 3, VFP_REG_D32, false, false},
898 };
899
900
901 struct float_abi
902 {
903   const char * name;
904   enum float_abi_type abi_type;
905 };
906
907
908 /* Available values for -mfloat-abi=.  */
909
910 static const struct float_abi all_float_abis[] =
911 {
912   {"soft",      ARM_FLOAT_ABI_SOFT},
913   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
914   {"hard",      ARM_FLOAT_ABI_HARD}
915 };
916
917
918 struct fp16_format
919 {
920   const char *name;
921   enum arm_fp16_format_type fp16_format_type;
922 };
923
924
925 /* Available values for -mfp16-format=.  */
926
927 static const struct fp16_format all_fp16_formats[] =
928 {
929   {"none",              ARM_FP16_FORMAT_NONE},
930   {"ieee",              ARM_FP16_FORMAT_IEEE},
931   {"alternative",       ARM_FP16_FORMAT_ALTERNATIVE}
932 };
933
934
935 struct abi_name
936 {
937   const char *name;
938   enum arm_abi_type abi_type;
939 };
940
941
942 /* Available values for -mabi=.  */
943
944 static const struct abi_name arm_all_abis[] =
945 {
946   {"apcs-gnu",    ARM_ABI_APCS},
947   {"atpcs",   ARM_ABI_ATPCS},
948   {"aapcs",   ARM_ABI_AAPCS},
949   {"iwmmxt",  ARM_ABI_IWMMXT},
950   {"aapcs-linux",   ARM_ABI_AAPCS_LINUX}
951 };
952
953 /* Supported TLS relocations.  */
954
955 enum tls_reloc {
956   TLS_GD32,
957   TLS_LDM32,
958   TLS_LDO32,
959   TLS_IE32,
960   TLS_LE32
961 };
962
963 /* The maximum number of insns to be used when loading a constant.  */
964 inline static int
965 arm_constant_limit (bool size_p)
966 {
967   return size_p ? 1 : current_tune->constant_limit;
968 }
969
970 /* Emit an insn that's a simple single-set.  Both the operands must be known
971    to be valid.  */
972 inline static rtx
973 emit_set_insn (rtx x, rtx y)
974 {
975   return emit_insn (gen_rtx_SET (VOIDmode, x, y));
976 }
977
978 /* Return the number of bits set in VALUE.  */
979 static unsigned
980 bit_count (unsigned long value)
981 {
982   unsigned long count = 0;
983
984   while (value)
985     {
986       count++;
987       value &= value - 1;  /* Clear the least-significant set bit.  */
988     }
989
990   return count;
991 }
992
993 /* Set up library functions unique to ARM.  */
994
995 static void
996 arm_init_libfuncs (void)
997 {
998   /* There are no special library functions unless we are using the
999      ARM BPABI.  */
1000   if (!TARGET_BPABI)
1001     return;
1002
1003   /* The functions below are described in Section 4 of the "Run-Time
1004      ABI for the ARM architecture", Version 1.0.  */
1005
1006   /* Double-precision floating-point arithmetic.  Table 2.  */
1007   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
1008   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
1009   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
1010   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
1011   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
1012
1013   /* Double-precision comparisons.  Table 3.  */
1014   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
1015   set_optab_libfunc (ne_optab, DFmode, NULL);
1016   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
1017   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
1018   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
1019   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
1020   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
1021
1022   /* Single-precision floating-point arithmetic.  Table 4.  */
1023   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
1024   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
1025   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
1026   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
1027   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
1028
1029   /* Single-precision comparisons.  Table 5.  */
1030   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
1031   set_optab_libfunc (ne_optab, SFmode, NULL);
1032   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
1033   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
1034   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
1035   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
1036   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
1037
1038   /* Floating-point to integer conversions.  Table 6.  */
1039   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
1040   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
1041   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
1042   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
1043   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
1044   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
1045   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
1046   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
1047
1048   /* Conversions between floating types.  Table 7.  */
1049   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
1050   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
1051
1052   /* Integer to floating-point conversions.  Table 8.  */
1053   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
1054   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
1055   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
1056   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
1057   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
1058   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
1059   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
1060   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
1061
1062   /* Long long.  Table 9.  */
1063   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
1064   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
1065   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
1066   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
1067   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
1068   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
1069   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
1070   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
1071
1072   /* Integer (32/32->32) division.  \S 4.3.1.  */
1073   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
1074   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
1075
1076   /* The divmod functions are designed so that they can be used for
1077      plain division, even though they return both the quotient and the
1078      remainder.  The quotient is returned in the usual location (i.e.,
1079      r0 for SImode, {r0, r1} for DImode), just as would be expected
1080      for an ordinary division routine.  Because the AAPCS calling
1081      conventions specify that all of { r0, r1, r2, r3 } are
1082      callee-saved registers, there is no need to tell the compiler
1083      explicitly that those registers are clobbered by these
1084      routines.  */
1085   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
1086   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
1087
1088   /* For SImode division the ABI provides div-without-mod routines,
1089      which are faster.  */
1090   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
1091   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
1092
1093   /* We don't have mod libcalls.  Fortunately gcc knows how to use the
1094      divmod libcalls instead.  */
1095   set_optab_libfunc (smod_optab, DImode, NULL);
1096   set_optab_libfunc (umod_optab, DImode, NULL);
1097   set_optab_libfunc (smod_optab, SImode, NULL);
1098   set_optab_libfunc (umod_optab, SImode, NULL);
1099
1100   /* Half-precision float operations.  The compiler handles all operations
1101      with NULL libfuncs by converting the SFmode.  */
1102   switch (arm_fp16_format)
1103     {
1104     case ARM_FP16_FORMAT_IEEE:
1105     case ARM_FP16_FORMAT_ALTERNATIVE:
1106
1107       /* Conversions.  */
1108       set_conv_libfunc (trunc_optab, HFmode, SFmode,
1109                         (arm_fp16_format == ARM_FP16_FORMAT_IEEE
1110                          ? "__gnu_f2h_ieee"
1111                          : "__gnu_f2h_alternative"));
1112       set_conv_libfunc (sext_optab, SFmode, HFmode, 
1113                         (arm_fp16_format == ARM_FP16_FORMAT_IEEE
1114                          ? "__gnu_h2f_ieee"
1115                          : "__gnu_h2f_alternative"));
1116       
1117       /* Arithmetic.  */
1118       set_optab_libfunc (add_optab, HFmode, NULL);
1119       set_optab_libfunc (sdiv_optab, HFmode, NULL);
1120       set_optab_libfunc (smul_optab, HFmode, NULL);
1121       set_optab_libfunc (neg_optab, HFmode, NULL);
1122       set_optab_libfunc (sub_optab, HFmode, NULL);
1123
1124       /* Comparisons.  */
1125       set_optab_libfunc (eq_optab, HFmode, NULL);
1126       set_optab_libfunc (ne_optab, HFmode, NULL);
1127       set_optab_libfunc (lt_optab, HFmode, NULL);
1128       set_optab_libfunc (le_optab, HFmode, NULL);
1129       set_optab_libfunc (ge_optab, HFmode, NULL);
1130       set_optab_libfunc (gt_optab, HFmode, NULL);
1131       set_optab_libfunc (unord_optab, HFmode, NULL);
1132       break;
1133
1134     default:
1135       break;
1136     }
1137
1138   if (TARGET_AAPCS_BASED)
1139     synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
1140 }
1141
1142 /* On AAPCS systems, this is the "struct __va_list".  */
1143 static GTY(()) tree va_list_type;
1144
1145 /* Return the type to use as __builtin_va_list.  */
1146 static tree
1147 arm_build_builtin_va_list (void)
1148 {
1149   tree va_list_name;
1150   tree ap_field;
1151   
1152   if (!TARGET_AAPCS_BASED)
1153     return std_build_builtin_va_list ();
1154
1155   /* AAPCS \S 7.1.4 requires that va_list be a typedef for a type
1156      defined as:
1157
1158        struct __va_list 
1159        {
1160          void *__ap;
1161        };
1162
1163      The C Library ABI further reinforces this definition in \S
1164      4.1.
1165
1166      We must follow this definition exactly.  The structure tag
1167      name is visible in C++ mangled names, and thus forms a part
1168      of the ABI.  The field name may be used by people who
1169      #include <stdarg.h>.  */
1170   /* Create the type.  */
1171   va_list_type = lang_hooks.types.make_type (RECORD_TYPE);
1172   /* Give it the required name.  */
1173   va_list_name = build_decl (BUILTINS_LOCATION,
1174                              TYPE_DECL,
1175                              get_identifier ("__va_list"),
1176                              va_list_type);
1177   DECL_ARTIFICIAL (va_list_name) = 1;
1178   TYPE_NAME (va_list_type) = va_list_name;
1179   /* Create the __ap field.  */
1180   ap_field = build_decl (BUILTINS_LOCATION,
1181                          FIELD_DECL, 
1182                          get_identifier ("__ap"),
1183                          ptr_type_node);
1184   DECL_ARTIFICIAL (ap_field) = 1;
1185   DECL_FIELD_CONTEXT (ap_field) = va_list_type;
1186   TYPE_FIELDS (va_list_type) = ap_field;
1187   /* Compute its layout.  */
1188   layout_type (va_list_type);
1189
1190   return va_list_type;
1191 }
1192
1193 /* Return an expression of type "void *" pointing to the next
1194    available argument in a variable-argument list.  VALIST is the
1195    user-level va_list object, of type __builtin_va_list.  */
1196 static tree
1197 arm_extract_valist_ptr (tree valist)
1198 {
1199   if (TREE_TYPE (valist) == error_mark_node)
1200     return error_mark_node;
1201
1202   /* On an AAPCS target, the pointer is stored within "struct
1203      va_list".  */
1204   if (TARGET_AAPCS_BASED)
1205     {
1206       tree ap_field = TYPE_FIELDS (TREE_TYPE (valist));
1207       valist = build3 (COMPONENT_REF, TREE_TYPE (ap_field), 
1208                        valist, ap_field, NULL_TREE);
1209     }
1210
1211   return valist;
1212 }
1213
1214 /* Implement TARGET_EXPAND_BUILTIN_VA_START.  */
1215 static void
1216 arm_expand_builtin_va_start (tree valist, rtx nextarg)
1217 {
1218   valist = arm_extract_valist_ptr (valist);
1219   std_expand_builtin_va_start (valist, nextarg);
1220 }
1221
1222 /* Implement TARGET_GIMPLIFY_VA_ARG_EXPR.  */
1223 static tree
1224 arm_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p, 
1225                           gimple_seq *post_p)
1226 {
1227   valist = arm_extract_valist_ptr (valist);
1228   return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
1229 }
1230
1231 /* Lookup NAME in SEL.  */
1232
1233 static const struct processors *
1234 arm_find_cpu (const char *name, const struct processors *sel, const char *desc)
1235 {
1236   if (!(name && *name))
1237     return NULL;
1238
1239   for (; sel->name != NULL; sel++)
1240     {
1241       if (streq (name, sel->name))
1242         return sel;
1243     }
1244
1245   error ("bad value (%s) for %s switch", name, desc);
1246   return NULL;
1247 }
1248
1249 /* Implement TARGET_HANDLE_OPTION.  */
1250
1251 static bool
1252 arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
1253 {
1254   switch (code)
1255     {
1256     case OPT_march_:
1257       arm_selected_arch = arm_find_cpu(arg, all_architectures, "-march");
1258       return true;
1259
1260     case OPT_mcpu_:
1261       arm_selected_cpu = arm_find_cpu(arg, all_cores, "-mcpu");
1262       return true;
1263
1264     case OPT_mhard_float:
1265       target_float_abi_name = "hard";
1266       return true;
1267
1268     case OPT_msoft_float:
1269       target_float_abi_name = "soft";
1270       return true;
1271
1272     case OPT_mtune_:
1273       arm_selected_tune = arm_find_cpu(arg, all_cores, "-mtune");
1274       return true;
1275
1276     default:
1277       return true;
1278     }
1279 }
1280
1281 static void
1282 arm_target_help (void)
1283 {
1284   int i;
1285   static int columns = 0;
1286   int remaining;
1287
1288   /* If we have not done so already, obtain the desired maximum width of
1289      the output.  Note - this is a duplication of the code at the start of
1290      gcc/opts.c:print_specific_help() - the two copies should probably be
1291      replaced by a single function.  */
1292   if (columns == 0)
1293     {
1294       const char *p;
1295
1296       GET_ENVIRONMENT (p, "COLUMNS");
1297       if (p != NULL)
1298         {
1299           int value = atoi (p);
1300
1301           if (value > 0)
1302             columns = value;
1303         }
1304
1305       if (columns == 0)
1306         /* Use a reasonable default.  */
1307         columns = 80;
1308     }
1309
1310   printf ("  Known ARM CPUs (for use with the -mcpu= and -mtune= options):\n");
1311
1312   /* The - 2 is because we know that the last entry in the array is NULL.  */
1313   i = ARRAY_SIZE (all_cores) - 2;
1314   gcc_assert (i > 0);
1315   printf ("    %s", all_cores[i].name);
1316   remaining = columns - (strlen (all_cores[i].name) + 4);
1317   gcc_assert (remaining >= 0);
1318
1319   while (i--)
1320     {
1321       int len = strlen (all_cores[i].name);
1322
1323       if (remaining > len + 2)
1324         {
1325           printf (", %s", all_cores[i].name);
1326           remaining -= len + 2;
1327         }
1328       else
1329         {
1330           if (remaining > 0)
1331             printf (",");
1332           printf ("\n    %s", all_cores[i].name);
1333           remaining = columns - (len + 4);
1334         }
1335     }
1336
1337   printf ("\n\n  Known ARM architectures (for use with the -march= option):\n");
1338
1339   i = ARRAY_SIZE (all_architectures) - 2;
1340   gcc_assert (i > 0);
1341   
1342   printf ("    %s", all_architectures[i].name);
1343   remaining = columns - (strlen (all_architectures[i].name) + 4);
1344   gcc_assert (remaining >= 0);
1345
1346   while (i--)
1347     {
1348       int len = strlen (all_architectures[i].name);
1349
1350       if (remaining > len + 2)
1351         {
1352           printf (", %s", all_architectures[i].name);
1353           remaining -= len + 2;
1354         }
1355       else
1356         {
1357           if (remaining > 0)
1358             printf (",");
1359           printf ("\n    %s", all_architectures[i].name);
1360           remaining = columns - (len + 4);
1361         }
1362     }
1363   printf ("\n");
1364
1365 }
1366
1367 /* Fix up any incompatible options that the user has specified.
1368    This has now turned into a maze.  */
1369 void
1370 arm_override_options (void)
1371 {
1372   unsigned i;
1373
1374   if (arm_selected_arch)
1375     {
1376       if (arm_selected_cpu)
1377         {
1378           /* Check for conflict between mcpu and march.  */
1379           if ((arm_selected_cpu->flags ^ arm_selected_arch->flags) & ~FL_TUNE)
1380             {
1381               warning (0, "switch -mcpu=%s conflicts with -march=%s switch",
1382                        arm_selected_cpu->name, arm_selected_arch->name);
1383               /* -march wins for code generation.
1384                  -mcpu wins for default tuning.  */
1385               if (!arm_selected_tune)
1386                 arm_selected_tune = arm_selected_cpu;
1387
1388               arm_selected_cpu = arm_selected_arch;
1389             }
1390           else
1391             /* -mcpu wins.  */
1392             arm_selected_arch = NULL;
1393         }
1394       else
1395         /* Pick a CPU based on the architecture.  */
1396         arm_selected_cpu = arm_selected_arch;
1397     }
1398
1399   /* If the user did not specify a processor, choose one for them.  */
1400   if (!arm_selected_cpu)
1401     {
1402       const struct processors * sel;
1403       unsigned int        sought;
1404
1405       arm_selected_cpu = &all_cores[TARGET_CPU_DEFAULT];
1406       if (!arm_selected_cpu->name)
1407         {
1408 #ifdef SUBTARGET_CPU_DEFAULT
1409           /* Use the subtarget default CPU if none was specified by
1410              configure.  */
1411           arm_selected_cpu = &all_cores[SUBTARGET_CPU_DEFAULT];
1412 #endif
1413           /* Default to ARM6.  */
1414           if (!arm_selected_cpu->name)
1415             arm_selected_cpu = &all_cores[arm6];
1416         }
1417
1418       sel = arm_selected_cpu;
1419       insn_flags = sel->flags;
1420
1421       /* Now check to see if the user has specified some command line
1422          switch that require certain abilities from the cpu.  */
1423       sought = 0;
1424
1425       if (TARGET_INTERWORK || TARGET_THUMB)
1426         {
1427           sought |= (FL_THUMB | FL_MODE32);
1428
1429           /* There are no ARM processors that support both APCS-26 and
1430              interworking.  Therefore we force FL_MODE26 to be removed
1431              from insn_flags here (if it was set), so that the search
1432              below will always be able to find a compatible processor.  */
1433           insn_flags &= ~FL_MODE26;
1434         }
1435
1436       if (sought != 0 && ((sought & insn_flags) != sought))
1437         {
1438           /* Try to locate a CPU type that supports all of the abilities
1439              of the default CPU, plus the extra abilities requested by
1440              the user.  */
1441           for (sel = all_cores; sel->name != NULL; sel++)
1442             if ((sel->flags & sought) == (sought | insn_flags))
1443               break;
1444
1445           if (sel->name == NULL)
1446             {
1447               unsigned current_bit_count = 0;
1448               const struct processors * best_fit = NULL;
1449
1450               /* Ideally we would like to issue an error message here
1451                  saying that it was not possible to find a CPU compatible
1452                  with the default CPU, but which also supports the command
1453                  line options specified by the programmer, and so they
1454                  ought to use the -mcpu=<name> command line option to
1455                  override the default CPU type.
1456
1457                  If we cannot find a cpu that has both the
1458                  characteristics of the default cpu and the given
1459                  command line options we scan the array again looking
1460                  for a best match.  */
1461               for (sel = all_cores; sel->name != NULL; sel++)
1462                 if ((sel->flags & sought) == sought)
1463                   {
1464                     unsigned count;
1465
1466                     count = bit_count (sel->flags & insn_flags);
1467
1468                     if (count >= current_bit_count)
1469                       {
1470                         best_fit = sel;
1471                         current_bit_count = count;
1472                       }
1473                   }
1474
1475               gcc_assert (best_fit);
1476               sel = best_fit;
1477             }
1478
1479           arm_selected_cpu = sel;
1480         }
1481     }
1482
1483   gcc_assert (arm_selected_cpu);
1484   /* The selected cpu may be an architecture, so lookup tuning by core ID.  */
1485   if (!arm_selected_tune)
1486     arm_selected_tune = &all_cores[arm_selected_cpu->core];
1487
1488   sprintf (arm_arch_name, "__ARM_ARCH_%s__", arm_selected_cpu->arch);
1489   insn_flags = arm_selected_cpu->flags;
1490
1491   arm_tune = arm_selected_tune->core;
1492   tune_flags = arm_selected_tune->flags;
1493   current_tune = arm_selected_tune->tune;
1494
1495   if (target_fp16_format_name)
1496     {
1497       for (i = 0; i < ARRAY_SIZE (all_fp16_formats); i++)
1498         {
1499           if (streq (all_fp16_formats[i].name, target_fp16_format_name))
1500             {
1501               arm_fp16_format = all_fp16_formats[i].fp16_format_type;
1502               break;
1503             }
1504         }
1505       if (i == ARRAY_SIZE (all_fp16_formats))
1506         error ("invalid __fp16 format option: -mfp16-format=%s",
1507                target_fp16_format_name);
1508     }
1509   else
1510     arm_fp16_format = ARM_FP16_FORMAT_NONE;
1511
1512   if (target_abi_name)
1513     {
1514       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1515         {
1516           if (streq (arm_all_abis[i].name, target_abi_name))
1517             {
1518               arm_abi = arm_all_abis[i].abi_type;
1519               break;
1520             }
1521         }
1522       if (i == ARRAY_SIZE (arm_all_abis))
1523         error ("invalid ABI option: -mabi=%s", target_abi_name);
1524     }
1525   else
1526     arm_abi = ARM_DEFAULT_ABI;
1527
1528   /* Make sure that the processor choice does not conflict with any of the
1529      other command line choices.  */
1530   if (TARGET_ARM && !(insn_flags & FL_NOTM))
1531     error ("target CPU does not support ARM mode");
1532
1533   /* BPABI targets use linker tricks to allow interworking on cores
1534      without thumb support.  */
1535   if (TARGET_INTERWORK && !((insn_flags & FL_THUMB) || TARGET_BPABI))
1536     {
1537       warning (0, "target CPU does not support interworking" );
1538       target_flags &= ~MASK_INTERWORK;
1539     }
1540
1541   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1542     {
1543       warning (0, "target CPU does not support THUMB instructions");
1544       target_flags &= ~MASK_THUMB;
1545     }
1546
1547   if (TARGET_APCS_FRAME && TARGET_THUMB)
1548     {
1549       /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1550       target_flags &= ~MASK_APCS_FRAME;
1551     }
1552
1553   /* Callee super interworking implies thumb interworking.  Adding
1554      this to the flags here simplifies the logic elsewhere.  */
1555   if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
1556     target_flags |= MASK_INTERWORK;
1557
1558   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1559      from here where no function is being compiled currently.  */
1560   if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1561     warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1562
1563   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1564     warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1565
1566   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1567     {
1568       warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1569       target_flags |= MASK_APCS_FRAME;
1570     }
1571
1572   if (TARGET_POKE_FUNCTION_NAME)
1573     target_flags |= MASK_APCS_FRAME;
1574
1575   if (TARGET_APCS_REENT && flag_pic)
1576     error ("-fpic and -mapcs-reent are incompatible");
1577
1578   if (TARGET_APCS_REENT)
1579     warning (0, "APCS reentrant code not supported.  Ignored");
1580
1581   /* If this target is normally configured to use APCS frames, warn if they
1582      are turned off and debugging is turned on.  */
1583   if (TARGET_ARM
1584       && write_symbols != NO_DEBUG
1585       && !TARGET_APCS_FRAME
1586       && (TARGET_DEFAULT & MASK_APCS_FRAME))
1587     warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1588
1589   if (TARGET_APCS_FLOAT)
1590     warning (0, "passing floating point arguments in fp regs not yet supported");
1591
1592   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
1593   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1594   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1595   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1596   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1597   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1598   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1599   arm_arch6k = (insn_flags & FL_ARCH6K) != 0;
1600   arm_arch_notm = (insn_flags & FL_NOTM) != 0;
1601   arm_arch7 = (insn_flags & FL_ARCH7) != 0;
1602   arm_arch7em = (insn_flags & FL_ARCH7EM) != 0;
1603   arm_arch_thumb2 = (insn_flags & FL_THUMB2) != 0;
1604   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1605   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1606
1607   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1608   arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1609   thumb_code = TARGET_ARM == 0;
1610   thumb1_code = TARGET_THUMB1 != 0;
1611   arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1612   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1613   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1614   arm_arch_hwdiv = (insn_flags & FL_DIV) != 0;
1615   arm_tune_cortex_a9 = (arm_tune == cortexa9) != 0;
1616
1617   /* If we are not using the default (ARM mode) section anchor offset
1618      ranges, then set the correct ranges now.  */
1619   if (TARGET_THUMB1)
1620     {
1621       /* Thumb-1 LDR instructions cannot have negative offsets.
1622          Permissible positive offset ranges are 5-bit (for byte loads),
1623          6-bit (for halfword loads), or 7-bit (for word loads).
1624          Empirical results suggest a 7-bit anchor range gives the best
1625          overall code size.  */
1626       targetm.min_anchor_offset = 0;
1627       targetm.max_anchor_offset = 127;
1628     }
1629   else if (TARGET_THUMB2)
1630     {
1631       /* The minimum is set such that the total size of the block
1632          for a particular anchor is 248 + 1 + 4095 bytes, which is
1633          divisible by eight, ensuring natural spacing of anchors.  */
1634       targetm.min_anchor_offset = -248;
1635       targetm.max_anchor_offset = 4095;
1636     }
1637
1638   /* V5 code we generate is completely interworking capable, so we turn off
1639      TARGET_INTERWORK here to avoid many tests later on.  */
1640
1641   /* XXX However, we must pass the right pre-processor defines to CPP
1642      or GLD can get confused.  This is a hack.  */
1643   if (TARGET_INTERWORK)
1644     arm_cpp_interwork = 1;
1645
1646   if (arm_arch5)
1647     target_flags &= ~MASK_INTERWORK;
1648
1649   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1650     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1651
1652   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1653     error ("iwmmxt abi requires an iwmmxt capable cpu");
1654
1655   if (target_fpu_name == NULL && target_fpe_name != NULL)
1656     {
1657       if (streq (target_fpe_name, "2"))
1658         target_fpu_name = "fpe2";
1659       else if (streq (target_fpe_name, "3"))
1660         target_fpu_name = "fpe3";
1661       else
1662         error ("invalid floating point emulation option: -mfpe=%s",
1663                target_fpe_name);
1664     }
1665
1666   if (target_fpu_name == NULL)
1667     {
1668 #ifdef FPUTYPE_DEFAULT
1669       target_fpu_name = FPUTYPE_DEFAULT;
1670 #else
1671       if (arm_arch_cirrus)
1672         target_fpu_name = "maverick";
1673       else
1674         target_fpu_name = "fpe2";
1675 #endif
1676     }
1677
1678   arm_fpu_desc = NULL;
1679   for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1680     {
1681       if (streq (all_fpus[i].name, target_fpu_name))
1682         {
1683           arm_fpu_desc = &all_fpus[i];
1684           break;
1685         }
1686     }
1687
1688   if (!arm_fpu_desc)
1689     {
1690       error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1691       return;
1692     }
1693
1694   switch (arm_fpu_desc->model)
1695     {
1696     case ARM_FP_MODEL_FPA:
1697       if (arm_fpu_desc->rev == 2)
1698         arm_fpu_attr = FPU_FPE2;
1699       else if (arm_fpu_desc->rev == 3)
1700         arm_fpu_attr = FPU_FPE3;
1701       else
1702         arm_fpu_attr = FPU_FPA;
1703       break;
1704
1705     case ARM_FP_MODEL_MAVERICK:
1706       arm_fpu_attr = FPU_MAVERICK;
1707       break;
1708
1709     case ARM_FP_MODEL_VFP:
1710       arm_fpu_attr = FPU_VFP;
1711       break;
1712
1713     default:
1714       gcc_unreachable();
1715     }
1716
1717   if (target_float_abi_name != NULL)
1718     {
1719       /* The user specified a FP ABI.  */
1720       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1721         {
1722           if (streq (all_float_abis[i].name, target_float_abi_name))
1723             {
1724               arm_float_abi = all_float_abis[i].abi_type;
1725               break;
1726             }
1727         }
1728       if (i == ARRAY_SIZE (all_float_abis))
1729         error ("invalid floating point abi: -mfloat-abi=%s",
1730                target_float_abi_name);
1731     }
1732   else
1733     arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1734
1735   if (TARGET_AAPCS_BASED
1736       && (arm_fpu_desc->model == ARM_FP_MODEL_FPA))
1737     error ("FPA is unsupported in the AAPCS");
1738
1739   if (TARGET_AAPCS_BASED)
1740     {
1741       if (TARGET_CALLER_INTERWORKING)
1742         error ("AAPCS does not support -mcaller-super-interworking");
1743       else
1744         if (TARGET_CALLEE_INTERWORKING)
1745           error ("AAPCS does not support -mcallee-super-interworking");
1746     }
1747
1748   /* FPA and iWMMXt are incompatible because the insn encodings overlap.
1749      VFP and iWMMXt can theoretically coexist, but it's unlikely such silicon
1750      will ever exist.  GCC makes no attempt to support this combination.  */
1751   if (TARGET_IWMMXT && !TARGET_SOFT_FLOAT)
1752     sorry ("iWMMXt and hardware floating point");
1753
1754   /* ??? iWMMXt insn patterns need auditing for Thumb-2.  */
1755   if (TARGET_THUMB2 && TARGET_IWMMXT)
1756     sorry ("Thumb-2 iWMMXt");
1757
1758   /* __fp16 support currently assumes the core has ldrh.  */
1759   if (!arm_arch4 && arm_fp16_format != ARM_FP16_FORMAT_NONE)
1760     sorry ("__fp16 and no ldrh");
1761
1762   /* If soft-float is specified then don't use FPU.  */
1763   if (TARGET_SOFT_FLOAT)
1764     arm_fpu_attr = FPU_NONE;
1765
1766   if (TARGET_AAPCS_BASED)
1767     {
1768       if (arm_abi == ARM_ABI_IWMMXT)
1769         arm_pcs_default = ARM_PCS_AAPCS_IWMMXT;
1770       else if (arm_float_abi == ARM_FLOAT_ABI_HARD
1771                && TARGET_HARD_FLOAT
1772                && TARGET_VFP)
1773         arm_pcs_default = ARM_PCS_AAPCS_VFP;
1774       else
1775         arm_pcs_default = ARM_PCS_AAPCS;
1776     }
1777   else
1778     {
1779       if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1780         sorry ("-mfloat-abi=hard and VFP");
1781
1782       if (arm_abi == ARM_ABI_APCS)
1783         arm_pcs_default = ARM_PCS_APCS;
1784       else
1785         arm_pcs_default = ARM_PCS_ATPCS;
1786     }
1787
1788   /* For arm2/3 there is no need to do any scheduling if there is only
1789      a floating point emulator, or we are doing software floating-point.  */
1790   if ((TARGET_SOFT_FLOAT
1791        || (TARGET_FPA && arm_fpu_desc->rev))
1792       && (tune_flags & FL_MODE32) == 0)
1793     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1794
1795   if (target_thread_switch)
1796     {
1797       if (strcmp (target_thread_switch, "soft") == 0)
1798         target_thread_pointer = TP_SOFT;
1799       else if (strcmp (target_thread_switch, "auto") == 0)
1800         target_thread_pointer = TP_AUTO;
1801       else if (strcmp (target_thread_switch, "cp15") == 0)
1802         target_thread_pointer = TP_CP15;
1803       else
1804         error ("invalid thread pointer option: -mtp=%s", target_thread_switch);
1805     }
1806
1807   /* Use the cp15 method if it is available.  */
1808   if (target_thread_pointer == TP_AUTO)
1809     {
1810       if (arm_arch6k && !TARGET_THUMB1)
1811         target_thread_pointer = TP_CP15;
1812       else
1813         target_thread_pointer = TP_SOFT;
1814     }
1815
1816   if (TARGET_HARD_TP && TARGET_THUMB1)
1817     error ("can not use -mtp=cp15 with 16-bit Thumb");
1818
1819   /* Override the default structure alignment for AAPCS ABI.  */
1820   if (TARGET_AAPCS_BASED)
1821     arm_structure_size_boundary = 8;
1822
1823   if (structure_size_string != NULL)
1824     {
1825       int size = strtol (structure_size_string, NULL, 0);
1826
1827       if (size == 8 || size == 32
1828           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1829         arm_structure_size_boundary = size;
1830       else
1831         warning (0, "structure size boundary can only be set to %s",
1832                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1833     }
1834
1835   if (!TARGET_ARM && TARGET_VXWORKS_RTP && flag_pic)
1836     {
1837       error ("RTP PIC is incompatible with Thumb");
1838       flag_pic = 0;
1839     }
1840
1841   /* If stack checking is disabled, we can use r10 as the PIC register,
1842      which keeps r9 available.  The EABI specifies r9 as the PIC register.  */
1843   if (flag_pic && TARGET_SINGLE_PIC_BASE)
1844     {
1845       if (TARGET_VXWORKS_RTP)
1846         warning (0, "RTP PIC is incompatible with -msingle-pic-base");
1847       arm_pic_register = (TARGET_APCS_STACK || TARGET_AAPCS_BASED) ? 9 : 10;
1848     }
1849
1850   if (flag_pic && TARGET_VXWORKS_RTP)
1851     arm_pic_register = 9;
1852
1853   if (arm_pic_register_string != NULL)
1854     {
1855       int pic_register = decode_reg_name (arm_pic_register_string);
1856
1857       if (!flag_pic)
1858         warning (0, "-mpic-register= is useless without -fpic");
1859
1860       /* Prevent the user from choosing an obviously stupid PIC register.  */
1861       else if (pic_register < 0 || call_used_regs[pic_register]
1862                || pic_register == HARD_FRAME_POINTER_REGNUM
1863                || pic_register == STACK_POINTER_REGNUM
1864                || pic_register >= PC_REGNUM
1865                || (TARGET_VXWORKS_RTP
1866                    && (unsigned int) pic_register != arm_pic_register))
1867         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1868       else
1869         arm_pic_register = pic_register;
1870     }
1871
1872   /* Enable -mfix-cortex-m3-ldrd by default for Cortex-M3 cores.  */
1873   if (fix_cm3_ldrd == 2)
1874     {
1875       if (arm_selected_cpu->core == cortexm3)
1876         fix_cm3_ldrd = 1;
1877       else
1878         fix_cm3_ldrd = 0;
1879     }
1880
1881   if (TARGET_THUMB1 && flag_schedule_insns)
1882     {
1883       /* Don't warn since it's on by default in -O2.  */
1884       flag_schedule_insns = 0;
1885     }
1886
1887   if (optimize_size)
1888     {
1889       /* If optimizing for size, bump the number of instructions that we
1890          are prepared to conditionally execute (even on a StrongARM).  */
1891       max_insns_skipped = 6;
1892     }
1893   else
1894     {
1895       /* StrongARM has early execution of branches, so a sequence
1896          that is worth skipping is shorter.  */
1897       if (arm_tune_strongarm)
1898         max_insns_skipped = 3;
1899     }
1900
1901   /* Hot/Cold partitioning is not currently supported, since we can't
1902      handle literal pool placement in that case.  */
1903   if (flag_reorder_blocks_and_partition)
1904     {
1905       inform (input_location,
1906               "-freorder-blocks-and-partition not supported on this architecture");
1907       flag_reorder_blocks_and_partition = 0;
1908       flag_reorder_blocks = 1;
1909     }
1910
1911   if (!PARAM_SET_P (PARAM_GCSE_UNRESTRICTED_COST)
1912       && flag_pic)
1913     /* Hoisting PIC address calculations more aggressively provides a small,
1914        but measurable, size reduction for PIC code.  Therefore, we decrease
1915        the bar for unrestricted expression hoisting to the cost of PIC address
1916        calculation, which is 2 instructions.  */
1917     set_param_value ("gcse-unrestricted-cost", 2);
1918
1919   /* Register global variables with the garbage collector.  */
1920   arm_add_gc_roots ();
1921 }
1922
1923 static void
1924 arm_add_gc_roots (void)
1925 {
1926   gcc_obstack_init(&minipool_obstack);
1927   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1928 }
1929 \f
1930 /* A table of known ARM exception types.
1931    For use with the interrupt function attribute.  */
1932
1933 typedef struct
1934 {
1935   const char *const arg;
1936   const unsigned long return_value;
1937 }
1938 isr_attribute_arg;
1939
1940 static const isr_attribute_arg isr_attribute_args [] =
1941 {
1942   { "IRQ",   ARM_FT_ISR },
1943   { "irq",   ARM_FT_ISR },
1944   { "FIQ",   ARM_FT_FIQ },
1945   { "fiq",   ARM_FT_FIQ },
1946   { "ABORT", ARM_FT_ISR },
1947   { "abort", ARM_FT_ISR },
1948   { "ABORT", ARM_FT_ISR },
1949   { "abort", ARM_FT_ISR },
1950   { "UNDEF", ARM_FT_EXCEPTION },
1951   { "undef", ARM_FT_EXCEPTION },
1952   { "SWI",   ARM_FT_EXCEPTION },
1953   { "swi",   ARM_FT_EXCEPTION },
1954   { NULL,    ARM_FT_NORMAL }
1955 };
1956
1957 /* Returns the (interrupt) function type of the current
1958    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1959
1960 static unsigned long
1961 arm_isr_value (tree argument)
1962 {
1963   const isr_attribute_arg * ptr;
1964   const char *              arg;
1965
1966   if (!arm_arch_notm)
1967     return ARM_FT_NORMAL | ARM_FT_STACKALIGN;
1968
1969   /* No argument - default to IRQ.  */
1970   if (argument == NULL_TREE)
1971     return ARM_FT_ISR;
1972
1973   /* Get the value of the argument.  */
1974   if (TREE_VALUE (argument) == NULL_TREE
1975       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1976     return ARM_FT_UNKNOWN;
1977
1978   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1979
1980   /* Check it against the list of known arguments.  */
1981   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1982     if (streq (arg, ptr->arg))
1983       return ptr->return_value;
1984
1985   /* An unrecognized interrupt type.  */
1986   return ARM_FT_UNKNOWN;
1987 }
1988
1989 /* Computes the type of the current function.  */
1990
1991 static unsigned long
1992 arm_compute_func_type (void)
1993 {
1994   unsigned long type = ARM_FT_UNKNOWN;
1995   tree a;
1996   tree attr;
1997
1998   gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
1999
2000   /* Decide if the current function is volatile.  Such functions
2001      never return, and many memory cycles can be saved by not storing
2002      register values that will never be needed again.  This optimization
2003      was added to speed up context switching in a kernel application.  */
2004   if (optimize > 0
2005       && (TREE_NOTHROW (current_function_decl)
2006           || !(flag_unwind_tables
2007                || (flag_exceptions && !USING_SJLJ_EXCEPTIONS)))
2008       && TREE_THIS_VOLATILE (current_function_decl))
2009     type |= ARM_FT_VOLATILE;
2010
2011   if (cfun->static_chain_decl != NULL)
2012     type |= ARM_FT_NESTED;
2013
2014   attr = DECL_ATTRIBUTES (current_function_decl);
2015
2016   a = lookup_attribute ("naked", attr);
2017   if (a != NULL_TREE)
2018     type |= ARM_FT_NAKED;
2019
2020   a = lookup_attribute ("isr", attr);
2021   if (a == NULL_TREE)
2022     a = lookup_attribute ("interrupt", attr);
2023
2024   if (a == NULL_TREE)
2025     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
2026   else
2027     type |= arm_isr_value (TREE_VALUE (a));
2028
2029   return type;
2030 }
2031
2032 /* Returns the type of the current function.  */
2033
2034 unsigned long
2035 arm_current_func_type (void)
2036 {
2037   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
2038     cfun->machine->func_type = arm_compute_func_type ();
2039
2040   return cfun->machine->func_type;
2041 }
2042
2043 bool
2044 arm_allocate_stack_slots_for_args (void)
2045 {
2046   /* Naked functions should not allocate stack slots for arguments.  */
2047   return !IS_NAKED (arm_current_func_type ());
2048 }
2049
2050 \f
2051 /* Output assembler code for a block containing the constant parts
2052    of a trampoline, leaving space for the variable parts.
2053
2054    On the ARM, (if r8 is the static chain regnum, and remembering that
2055    referencing pc adds an offset of 8) the trampoline looks like:
2056            ldr          r8, [pc, #0]
2057            ldr          pc, [pc]
2058            .word        static chain value
2059            .word        function's address
2060    XXX FIXME: When the trampoline returns, r8 will be clobbered.  */
2061
2062 static void
2063 arm_asm_trampoline_template (FILE *f)
2064 {
2065   if (TARGET_ARM)
2066     {
2067       asm_fprintf (f, "\tldr\t%r, [%r, #0]\n", STATIC_CHAIN_REGNUM, PC_REGNUM);
2068       asm_fprintf (f, "\tldr\t%r, [%r, #0]\n", PC_REGNUM, PC_REGNUM);
2069     }
2070   else if (TARGET_THUMB2)
2071     {
2072       /* The Thumb-2 trampoline is similar to the arm implementation.
2073          Unlike 16-bit Thumb, we enter the stub in thumb mode.  */
2074       asm_fprintf (f, "\tldr.w\t%r, [%r, #4]\n",
2075                    STATIC_CHAIN_REGNUM, PC_REGNUM);
2076       asm_fprintf (f, "\tldr.w\t%r, [%r, #4]\n", PC_REGNUM, PC_REGNUM);
2077     }
2078   else
2079     {
2080       ASM_OUTPUT_ALIGN (f, 2);
2081       fprintf (f, "\t.code\t16\n");
2082       fprintf (f, ".Ltrampoline_start:\n");
2083       asm_fprintf (f, "\tpush\t{r0, r1}\n");
2084       asm_fprintf (f, "\tldr\tr0, [%r, #8]\n", PC_REGNUM);
2085       asm_fprintf (f, "\tmov\t%r, r0\n", STATIC_CHAIN_REGNUM);
2086       asm_fprintf (f, "\tldr\tr0, [%r, #8]\n", PC_REGNUM);
2087       asm_fprintf (f, "\tstr\tr0, [%r, #4]\n", SP_REGNUM);
2088       asm_fprintf (f, "\tpop\t{r0, %r}\n", PC_REGNUM);
2089     }
2090   assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2091   assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2092 }
2093
2094 /* Emit RTL insns to initialize the variable parts of a trampoline.  */
2095
2096 static void
2097 arm_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
2098 {
2099   rtx fnaddr, mem, a_tramp;
2100
2101   emit_block_move (m_tramp, assemble_trampoline_template (),
2102                    GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
2103
2104   mem = adjust_address (m_tramp, SImode, TARGET_32BIT ? 8 : 12);
2105   emit_move_insn (mem, chain_value);
2106
2107   mem = adjust_address (m_tramp, SImode, TARGET_32BIT ? 12 : 16);
2108   fnaddr = XEXP (DECL_RTL (fndecl), 0);
2109   emit_move_insn (mem, fnaddr);
2110
2111   a_tramp = XEXP (m_tramp, 0);
2112   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__clear_cache"),
2113                      LCT_NORMAL, VOIDmode, 2, a_tramp, Pmode,
2114                      plus_constant (a_tramp, TRAMPOLINE_SIZE), Pmode);
2115 }
2116
2117 /* Thumb trampolines should be entered in thumb mode, so set
2118    the bottom bit of the address.  */
2119
2120 static rtx
2121 arm_trampoline_adjust_address (rtx addr)
2122 {
2123   if (TARGET_THUMB)
2124     addr = expand_simple_binop (Pmode, IOR, addr, const1_rtx,
2125                                 NULL, 0, OPTAB_LIB_WIDEN);
2126   return addr;
2127 }
2128 \f
2129 /* Return 1 if it is possible to return using a single instruction.
2130    If SIBLING is non-null, this is a test for a return before a sibling
2131    call.  SIBLING is the call insn, so we can examine its register usage.  */
2132
2133 int
2134 use_return_insn (int iscond, rtx sibling)
2135 {
2136   int regno;
2137   unsigned int func_type;
2138   unsigned long saved_int_regs;
2139   unsigned HOST_WIDE_INT stack_adjust;
2140   arm_stack_offsets *offsets;
2141
2142   /* Never use a return instruction before reload has run.  */
2143   if (!reload_completed)
2144     return 0;
2145
2146   func_type = arm_current_func_type ();
2147
2148   /* Naked, volatile and stack alignment functions need special
2149      consideration.  */
2150   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED | ARM_FT_STACKALIGN))
2151     return 0;
2152
2153   /* So do interrupt functions that use the frame pointer and Thumb
2154      interrupt functions.  */
2155   if (IS_INTERRUPT (func_type) && (frame_pointer_needed || TARGET_THUMB))
2156     return 0;
2157
2158   offsets = arm_get_frame_offsets ();
2159   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
2160
2161   /* As do variadic functions.  */
2162   if (crtl->args.pretend_args_size
2163       || cfun->machine->uses_anonymous_args
2164       /* Or if the function calls __builtin_eh_return () */
2165       || crtl->calls_eh_return
2166       /* Or if the function calls alloca */
2167       || cfun->calls_alloca
2168       /* Or if there is a stack adjustment.  However, if the stack pointer
2169          is saved on the stack, we can use a pre-incrementing stack load.  */
2170       || !(stack_adjust == 0 || (TARGET_APCS_FRAME && frame_pointer_needed
2171                                  && stack_adjust == 4)))
2172     return 0;
2173
2174   saved_int_regs = offsets->saved_regs_mask;
2175
2176   /* Unfortunately, the insn
2177
2178        ldmib sp, {..., sp, ...}
2179
2180      triggers a bug on most SA-110 based devices, such that the stack
2181      pointer won't be correctly restored if the instruction takes a
2182      page fault.  We work around this problem by popping r3 along with
2183      the other registers, since that is never slower than executing
2184      another instruction.
2185
2186      We test for !arm_arch5 here, because code for any architecture
2187      less than this could potentially be run on one of the buggy
2188      chips.  */
2189   if (stack_adjust == 4 && !arm_arch5 && TARGET_ARM)
2190     {
2191       /* Validate that r3 is a call-clobbered register (always true in
2192          the default abi) ...  */
2193       if (!call_used_regs[3])
2194         return 0;
2195
2196       /* ... that it isn't being used for a return value ... */
2197       if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
2198         return 0;
2199
2200       /* ... or for a tail-call argument ...  */
2201       if (sibling)
2202         {
2203           gcc_assert (GET_CODE (sibling) == CALL_INSN);
2204
2205           if (find_regno_fusage (sibling, USE, 3))
2206             return 0;
2207         }
2208
2209       /* ... and that there are no call-saved registers in r0-r2
2210          (always true in the default ABI).  */
2211       if (saved_int_regs & 0x7)
2212         return 0;
2213     }
2214
2215   /* Can't be done if interworking with Thumb, and any registers have been
2216      stacked.  */
2217   if (TARGET_INTERWORK && saved_int_regs != 0 && !IS_INTERRUPT(func_type))
2218     return 0;
2219
2220   /* On StrongARM, conditional returns are expensive if they aren't
2221      taken and multiple registers have been stacked.  */
2222   if (iscond && arm_tune_strongarm)
2223     {
2224       /* Conditional return when just the LR is stored is a simple
2225          conditional-load instruction, that's not expensive.  */
2226       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
2227         return 0;
2228
2229       if (flag_pic 
2230           && arm_pic_register != INVALID_REGNUM
2231           && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
2232         return 0;
2233     }
2234
2235   /* If there are saved registers but the LR isn't saved, then we need
2236      two instructions for the return.  */
2237   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
2238     return 0;
2239
2240   /* Can't be done if any of the FPA regs are pushed,
2241      since this also requires an insn.  */
2242   if (TARGET_HARD_FLOAT && TARGET_FPA)
2243     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
2244       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
2245         return 0;
2246
2247   /* Likewise VFP regs.  */
2248   if (TARGET_HARD_FLOAT && TARGET_VFP)
2249     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
2250       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
2251         return 0;
2252
2253   if (TARGET_REALLY_IWMMXT)
2254     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
2255       if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
2256         return 0;
2257
2258   return 1;
2259 }
2260
2261 /* Return TRUE if int I is a valid immediate ARM constant.  */
2262
2263 int
2264 const_ok_for_arm (HOST_WIDE_INT i)
2265 {
2266   int lowbit;
2267
2268   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
2269      be all zero, or all one.  */
2270   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
2271       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
2272           != ((~(unsigned HOST_WIDE_INT) 0)
2273               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
2274     return FALSE;
2275
2276   i &= (unsigned HOST_WIDE_INT) 0xffffffff;
2277
2278   /* Fast return for 0 and small values.  We must do this for zero, since
2279      the code below can't handle that one case.  */
2280   if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
2281     return TRUE;
2282
2283   /* Get the number of trailing zeros.  */
2284   lowbit = ffs((int) i) - 1;
2285   
2286   /* Only even shifts are allowed in ARM mode so round down to the
2287      nearest even number.  */
2288   if (TARGET_ARM)
2289     lowbit &= ~1;
2290
2291   if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
2292     return TRUE;
2293
2294   if (TARGET_ARM)
2295     {
2296       /* Allow rotated constants in ARM mode.  */
2297       if (lowbit <= 4
2298            && ((i & ~0xc000003f) == 0
2299                || (i & ~0xf000000f) == 0
2300                || (i & ~0xfc000003) == 0))
2301         return TRUE;
2302     }
2303   else
2304     {
2305       HOST_WIDE_INT v;
2306
2307       /* Allow repeated pattern.  */
2308       v = i & 0xff;
2309       v |= v << 16;
2310       if (i == v || i == (v | (v << 8)))
2311         return TRUE;
2312     }
2313
2314   return FALSE;
2315 }
2316
2317 /* Return true if I is a valid constant for the operation CODE.  */
2318 static int
2319 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
2320 {
2321   if (const_ok_for_arm (i))
2322     return 1;
2323
2324   switch (code)
2325     {
2326     case PLUS:
2327     case COMPARE:
2328     case EQ:
2329     case NE:
2330     case GT:
2331     case LE:
2332     case LT:
2333     case GE:
2334     case GEU:
2335     case LTU:
2336     case GTU:
2337     case LEU:
2338     case UNORDERED:
2339     case ORDERED:
2340     case UNEQ:
2341     case UNGE:
2342     case UNLT:
2343     case UNGT:
2344     case UNLE:
2345       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
2346
2347     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
2348     case XOR:
2349       return 0;
2350
2351     case IOR:
2352       if (TARGET_THUMB2)
2353         return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2354       return 0;
2355
2356     case AND:
2357       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2358
2359     default:
2360       gcc_unreachable ();
2361     }
2362 }
2363
2364 /* Emit a sequence of insns to handle a large constant.
2365    CODE is the code of the operation required, it can be any of SET, PLUS,
2366    IOR, AND, XOR, MINUS;
2367    MODE is the mode in which the operation is being performed;
2368    VAL is the integer to operate on;
2369    SOURCE is the other operand (a register, or a null-pointer for SET);
2370    SUBTARGETS means it is safe to create scratch registers if that will
2371    either produce a simpler sequence, or we will want to cse the values.
2372    Return value is the number of insns emitted.  */
2373
2374 /* ??? Tweak this for thumb2.  */
2375 int
2376 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
2377                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
2378 {
2379   rtx cond;
2380
2381   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
2382     cond = COND_EXEC_TEST (PATTERN (insn));
2383   else
2384     cond = NULL_RTX;
2385
2386   if (subtargets || code == SET
2387       || (GET_CODE (target) == REG && GET_CODE (source) == REG
2388           && REGNO (target) != REGNO (source)))
2389     {
2390       /* After arm_reorg has been called, we can't fix up expensive
2391          constants by pushing them into memory so we must synthesize
2392          them in-line, regardless of the cost.  This is only likely to
2393          be more costly on chips that have load delay slots and we are
2394          compiling without running the scheduler (so no splitting
2395          occurred before the final instruction emission).
2396
2397          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
2398       */
2399       if (!after_arm_reorg
2400           && !cond
2401           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
2402                                 1, 0)
2403               > (arm_constant_limit (optimize_function_for_size_p (cfun))
2404                  + (code != SET))))
2405         {
2406           if (code == SET)
2407             {
2408               /* Currently SET is the only monadic value for CODE, all
2409                  the rest are diadic.  */
2410               if (TARGET_USE_MOVT)
2411                 arm_emit_movpair (target, GEN_INT (val));
2412               else
2413                 emit_set_insn (target, GEN_INT (val));
2414
2415               return 1;
2416             }
2417           else
2418             {
2419               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
2420
2421               if (TARGET_USE_MOVT)
2422                 arm_emit_movpair (temp, GEN_INT (val));
2423               else
2424                 emit_set_insn (temp, GEN_INT (val));
2425
2426               /* For MINUS, the value is subtracted from, since we never
2427                  have subtraction of a constant.  */
2428               if (code == MINUS)
2429                 emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
2430               else
2431                 emit_set_insn (target,
2432                                gen_rtx_fmt_ee (code, mode, source, temp));
2433               return 2;
2434             }
2435         }
2436     }
2437
2438   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
2439                            1);
2440 }
2441
2442 /* Return the number of instructions required to synthesize the given
2443    constant, if we start emitting them from bit-position I.  */
2444 static int
2445 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
2446 {
2447   HOST_WIDE_INT temp1;
2448   int step_size = TARGET_ARM ? 2 : 1;
2449   int num_insns = 0;
2450
2451   gcc_assert (TARGET_ARM || i == 0);
2452
2453   do
2454     {
2455       int end;
2456
2457       if (i <= 0)
2458         i += 32;
2459       if (remainder & (((1 << step_size) - 1) << (i - step_size)))
2460         {
2461           end = i - 8;
2462           if (end < 0)
2463             end += 32;
2464           temp1 = remainder & ((0x0ff << end)
2465                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
2466           remainder &= ~temp1;
2467           num_insns++;
2468           i -= 8 - step_size;
2469         }
2470       i -= step_size;
2471     } while (remainder);
2472   return num_insns;
2473 }
2474
2475 static int
2476 find_best_start (unsigned HOST_WIDE_INT remainder)
2477 {
2478   int best_consecutive_zeros = 0;
2479   int i;
2480   int best_start = 0;
2481
2482   /* If we aren't targetting ARM, the best place to start is always at
2483      the bottom.  */
2484   if (! TARGET_ARM)
2485     return 0;
2486
2487   for (i = 0; i < 32; i += 2)
2488     {
2489       int consecutive_zeros = 0;
2490
2491       if (!(remainder & (3 << i)))
2492         {
2493           while ((i < 32) && !(remainder & (3 << i)))
2494             {
2495               consecutive_zeros += 2;
2496               i += 2;
2497             }
2498           if (consecutive_zeros > best_consecutive_zeros)
2499             {
2500               best_consecutive_zeros = consecutive_zeros;
2501               best_start = i - consecutive_zeros;
2502             }
2503           i -= 2;
2504         }
2505     }
2506
2507   /* So long as it won't require any more insns to do so, it's
2508      desirable to emit a small constant (in bits 0...9) in the last
2509      insn.  This way there is more chance that it can be combined with
2510      a later addressing insn to form a pre-indexed load or store
2511      operation.  Consider:
2512
2513            *((volatile int *)0xe0000100) = 1;
2514            *((volatile int *)0xe0000110) = 2;
2515
2516      We want this to wind up as:
2517
2518             mov rA, #0xe0000000
2519             mov rB, #1
2520             str rB, [rA, #0x100]
2521             mov rB, #2
2522             str rB, [rA, #0x110]
2523
2524      rather than having to synthesize both large constants from scratch.
2525
2526      Therefore, we calculate how many insns would be required to emit
2527      the constant starting from `best_start', and also starting from
2528      zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2529      yield a shorter sequence, we may as well use zero.  */
2530   if (best_start != 0
2531       && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2532       && (count_insns_for_constant (remainder, 0) <=
2533           count_insns_for_constant (remainder, best_start)))
2534     best_start = 0;
2535
2536   return best_start;
2537 }
2538
2539 /* Emit an instruction with the indicated PATTERN.  If COND is
2540    non-NULL, conditionalize the execution of the instruction on COND
2541    being true.  */
2542
2543 static void
2544 emit_constant_insn (rtx cond, rtx pattern)
2545 {
2546   if (cond)
2547     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
2548   emit_insn (pattern);
2549 }
2550
2551 /* As above, but extra parameter GENERATE which, if clear, suppresses
2552    RTL generation.  */
2553 /* ??? This needs more work for thumb2.  */
2554
2555 static int
2556 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
2557                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
2558                   int generate)
2559 {
2560   int can_invert = 0;
2561   int can_negate = 0;
2562   int final_invert = 0;
2563   int can_negate_initial = 0;
2564   int i;
2565   int num_bits_set = 0;
2566   int set_sign_bit_copies = 0;
2567   int clear_sign_bit_copies = 0;
2568   int clear_zero_bit_copies = 0;
2569   int set_zero_bit_copies = 0;
2570   int insns = 0;
2571   unsigned HOST_WIDE_INT temp1, temp2;
2572   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
2573   int step_size = TARGET_ARM ? 2 : 1;
2574
2575   /* Find out which operations are safe for a given CODE.  Also do a quick
2576      check for degenerate cases; these can occur when DImode operations
2577      are split.  */
2578   switch (code)
2579     {
2580     case SET:
2581       can_invert = 1;
2582       can_negate = 1;
2583       break;
2584
2585     case PLUS:
2586       can_negate = 1;
2587       can_negate_initial = 1;
2588       break;
2589
2590     case IOR:
2591       if (remainder == 0xffffffff)
2592         {
2593           if (generate)
2594             emit_constant_insn (cond,
2595                                 gen_rtx_SET (VOIDmode, target,
2596                                              GEN_INT (ARM_SIGN_EXTEND (val))));
2597           return 1;
2598         }
2599
2600       if (remainder == 0)
2601         {
2602           if (reload_completed && rtx_equal_p (target, source))
2603             return 0;
2604
2605           if (generate)
2606             emit_constant_insn (cond,
2607                                 gen_rtx_SET (VOIDmode, target, source));
2608           return 1;
2609         }
2610
2611       if (TARGET_THUMB2)
2612         can_invert = 1;
2613       break;
2614
2615     case AND:
2616       if (remainder == 0)
2617         {
2618           if (generate)
2619             emit_constant_insn (cond,
2620                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
2621           return 1;
2622         }
2623       if (remainder == 0xffffffff)
2624         {
2625           if (reload_completed && rtx_equal_p (target, source))
2626             return 0;
2627           if (generate)
2628             emit_constant_insn (cond,
2629                                 gen_rtx_SET (VOIDmode, target, source));
2630           return 1;
2631         }
2632       can_invert = 1;
2633       break;
2634
2635     case XOR:
2636       if (remainder == 0)
2637         {
2638           if (reload_completed && rtx_equal_p (target, source))
2639             return 0;
2640           if (generate)
2641             emit_constant_insn (cond,
2642                                 gen_rtx_SET (VOIDmode, target, source));
2643           return 1;
2644         }
2645
2646       if (remainder == 0xffffffff)
2647         {
2648           if (generate)
2649             emit_constant_insn (cond,
2650                                 gen_rtx_SET (VOIDmode, target,
2651                                              gen_rtx_NOT (mode, source)));
2652           return 1;
2653         }
2654       break;
2655
2656     case MINUS:
2657       /* We treat MINUS as (val - source), since (source - val) is always
2658          passed as (source + (-val)).  */
2659       if (remainder == 0)
2660         {
2661           if (generate)
2662             emit_constant_insn (cond,
2663                                 gen_rtx_SET (VOIDmode, target,
2664                                              gen_rtx_NEG (mode, source)));
2665           return 1;
2666         }
2667       if (const_ok_for_arm (val))
2668         {
2669           if (generate)
2670             emit_constant_insn (cond,
2671                                 gen_rtx_SET (VOIDmode, target,
2672                                              gen_rtx_MINUS (mode, GEN_INT (val),
2673                                                             source)));
2674           return 1;
2675         }
2676       can_negate = 1;
2677
2678       break;
2679
2680     default:
2681       gcc_unreachable ();
2682     }
2683
2684   /* If we can do it in one insn get out quickly.  */
2685   if (const_ok_for_arm (val)
2686       || (can_negate_initial && const_ok_for_arm (-val))
2687       || (can_invert && const_ok_for_arm (~val)))
2688     {
2689       if (generate)
2690         emit_constant_insn (cond,
2691                             gen_rtx_SET (VOIDmode, target,
2692                                          (source
2693                                           ? gen_rtx_fmt_ee (code, mode, source,
2694                                                             GEN_INT (val))
2695                                           : GEN_INT (val))));
2696       return 1;
2697     }
2698
2699   /* Calculate a few attributes that may be useful for specific
2700      optimizations.  */
2701   /* Count number of leading zeros.  */
2702   for (i = 31; i >= 0; i--)
2703     {
2704       if ((remainder & (1 << i)) == 0)
2705         clear_sign_bit_copies++;
2706       else
2707         break;
2708     }
2709
2710   /* Count number of leading 1's.  */
2711   for (i = 31; i >= 0; i--)
2712     {
2713       if ((remainder & (1 << i)) != 0)
2714         set_sign_bit_copies++;
2715       else
2716         break;
2717     }
2718
2719   /* Count number of trailing zero's.  */
2720   for (i = 0; i <= 31; i++)
2721     {
2722       if ((remainder & (1 << i)) == 0)
2723         clear_zero_bit_copies++;
2724       else
2725         break;
2726     }
2727
2728   /* Count number of trailing 1's.  */
2729   for (i = 0; i <= 31; i++)
2730     {
2731       if ((remainder & (1 << i)) != 0)
2732         set_zero_bit_copies++;
2733       else
2734         break;
2735     }
2736
2737   switch (code)
2738     {
2739     case SET:
2740       /* See if we can use movw.  */
2741       if (arm_arch_thumb2 && (remainder & 0xffff0000) == 0)
2742         {
2743           if (generate)
2744             emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
2745                                                    GEN_INT (val)));
2746           return 1;
2747         }
2748
2749       /* See if we can do this by sign_extending a constant that is known
2750          to be negative.  This is a good, way of doing it, since the shift
2751          may well merge into a subsequent insn.  */
2752       if (set_sign_bit_copies > 1)
2753         {
2754           if (const_ok_for_arm
2755               (temp1 = ARM_SIGN_EXTEND (remainder
2756                                         << (set_sign_bit_copies - 1))))
2757             {
2758               if (generate)
2759                 {
2760                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2761                   emit_constant_insn (cond,
2762                                       gen_rtx_SET (VOIDmode, new_src,
2763                                                    GEN_INT (temp1)));
2764                   emit_constant_insn (cond,
2765                                       gen_ashrsi3 (target, new_src,
2766                                                    GEN_INT (set_sign_bit_copies - 1)));
2767                 }
2768               return 2;
2769             }
2770           /* For an inverted constant, we will need to set the low bits,
2771              these will be shifted out of harm's way.  */
2772           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
2773           if (const_ok_for_arm (~temp1))
2774             {
2775               if (generate)
2776                 {
2777                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2778                   emit_constant_insn (cond,
2779                                       gen_rtx_SET (VOIDmode, new_src,
2780                                                    GEN_INT (temp1)));
2781                   emit_constant_insn (cond,
2782                                       gen_ashrsi3 (target, new_src,
2783                                                    GEN_INT (set_sign_bit_copies - 1)));
2784                 }
2785               return 2;
2786             }
2787         }
2788
2789       /* See if we can calculate the value as the difference between two
2790          valid immediates.  */
2791       if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
2792         {
2793           int topshift = clear_sign_bit_copies & ~1;
2794
2795           temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
2796                                    & (0xff000000 >> topshift));
2797
2798           /* If temp1 is zero, then that means the 9 most significant
2799              bits of remainder were 1 and we've caused it to overflow.
2800              When topshift is 0 we don't need to do anything since we
2801              can borrow from 'bit 32'.  */
2802           if (temp1 == 0 && topshift != 0)
2803             temp1 = 0x80000000 >> (topshift - 1);
2804
2805           temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
2806
2807           if (const_ok_for_arm (temp2))
2808             {
2809               if (generate)
2810                 {
2811                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2812                   emit_constant_insn (cond,
2813                                       gen_rtx_SET (VOIDmode, new_src,
2814                                                    GEN_INT (temp1)));
2815                   emit_constant_insn (cond,
2816                                       gen_addsi3 (target, new_src,
2817                                                   GEN_INT (-temp2)));
2818                 }
2819
2820               return 2;
2821             }
2822         }
2823
2824       /* See if we can generate this by setting the bottom (or the top)
2825          16 bits, and then shifting these into the other half of the
2826          word.  We only look for the simplest cases, to do more would cost
2827          too much.  Be careful, however, not to generate this when the
2828          alternative would take fewer insns.  */
2829       if (val & 0xffff0000)
2830         {
2831           temp1 = remainder & 0xffff0000;
2832           temp2 = remainder & 0x0000ffff;
2833
2834           /* Overlaps outside this range are best done using other methods.  */
2835           for (i = 9; i < 24; i++)
2836             {
2837               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
2838                   && !const_ok_for_arm (temp2))
2839                 {
2840                   rtx new_src = (subtargets
2841                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2842                                  : target);
2843                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2844                                             source, subtargets, generate);
2845                   source = new_src;
2846                   if (generate)
2847                     emit_constant_insn
2848                       (cond,
2849                        gen_rtx_SET
2850                        (VOIDmode, target,
2851                         gen_rtx_IOR (mode,
2852                                      gen_rtx_ASHIFT (mode, source,
2853                                                      GEN_INT (i)),
2854                                      source)));
2855                   return insns + 1;
2856                 }
2857             }
2858
2859           /* Don't duplicate cases already considered.  */
2860           for (i = 17; i < 24; i++)
2861             {
2862               if (((temp1 | (temp1 >> i)) == remainder)
2863                   && !const_ok_for_arm (temp1))
2864                 {
2865                   rtx new_src = (subtargets
2866                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2867                                  : target);
2868                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2869                                             source, subtargets, generate);
2870                   source = new_src;
2871                   if (generate)
2872                     emit_constant_insn
2873                       (cond,
2874                        gen_rtx_SET (VOIDmode, target,
2875                                     gen_rtx_IOR
2876                                     (mode,
2877                                      gen_rtx_LSHIFTRT (mode, source,
2878                                                        GEN_INT (i)),
2879                                      source)));
2880                   return insns + 1;
2881                 }
2882             }
2883         }
2884       break;
2885
2886     case IOR:
2887     case XOR:
2888       /* If we have IOR or XOR, and the constant can be loaded in a
2889          single instruction, and we can find a temporary to put it in,
2890          then this can be done in two instructions instead of 3-4.  */
2891       if (subtargets
2892           /* TARGET can't be NULL if SUBTARGETS is 0 */
2893           || (reload_completed && !reg_mentioned_p (target, source)))
2894         {
2895           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
2896             {
2897               if (generate)
2898                 {
2899                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2900
2901                   emit_constant_insn (cond,
2902                                       gen_rtx_SET (VOIDmode, sub,
2903                                                    GEN_INT (val)));
2904                   emit_constant_insn (cond,
2905                                       gen_rtx_SET (VOIDmode, target,
2906                                                    gen_rtx_fmt_ee (code, mode,
2907                                                                    source, sub)));
2908                 }
2909               return 2;
2910             }
2911         }
2912
2913       if (code == XOR)
2914         break;
2915
2916       /*  Convert.
2917           x = y | constant ( which is composed of set_sign_bit_copies of leading 1s
2918                              and the remainder 0s for e.g. 0xfff00000)
2919           x = ~(~(y ashift set_sign_bit_copies) lshiftrt set_sign_bit_copies)
2920
2921           This can be done in 2 instructions by using shifts with mov or mvn.
2922           e.g. for
2923           x = x | 0xfff00000;
2924           we generate.
2925           mvn   r0, r0, asl #12
2926           mvn   r0, r0, lsr #12  */
2927       if (set_sign_bit_copies > 8
2928           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
2929         {
2930           if (generate)
2931             {
2932               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2933               rtx shift = GEN_INT (set_sign_bit_copies);
2934
2935               emit_constant_insn
2936                 (cond,
2937                  gen_rtx_SET (VOIDmode, sub,
2938                               gen_rtx_NOT (mode,
2939                                            gen_rtx_ASHIFT (mode,
2940                                                            source,
2941                                                            shift))));
2942               emit_constant_insn
2943                 (cond,
2944                  gen_rtx_SET (VOIDmode, target,
2945                               gen_rtx_NOT (mode,
2946                                            gen_rtx_LSHIFTRT (mode, sub,
2947                                                              shift))));
2948             }
2949           return 2;
2950         }
2951
2952       /* Convert
2953           x = y | constant (which has set_zero_bit_copies number of trailing ones).
2954            to
2955           x = ~((~y lshiftrt set_zero_bit_copies) ashift set_zero_bit_copies).
2956
2957           For eg. r0 = r0 | 0xfff
2958                mvn      r0, r0, lsr #12
2959                mvn      r0, r0, asl #12
2960
2961       */
2962       if (set_zero_bit_copies > 8
2963           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2964         {
2965           if (generate)
2966             {
2967               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2968               rtx shift = GEN_INT (set_zero_bit_copies);
2969
2970               emit_constant_insn
2971                 (cond,
2972                  gen_rtx_SET (VOIDmode, sub,
2973                               gen_rtx_NOT (mode,
2974                                            gen_rtx_LSHIFTRT (mode,
2975                                                              source,
2976                                                              shift))));
2977               emit_constant_insn
2978                 (cond,
2979                  gen_rtx_SET (VOIDmode, target,
2980                               gen_rtx_NOT (mode,
2981                                            gen_rtx_ASHIFT (mode, sub,
2982                                                            shift))));
2983             }
2984           return 2;
2985         }
2986
2987       /* This will never be reached for Thumb2 because orn is a valid
2988          instruction. This is for Thumb1 and the ARM 32 bit cases.
2989
2990          x = y | constant (such that ~constant is a valid constant)
2991          Transform this to
2992          x = ~(~y & ~constant).
2993       */
2994       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
2995         {
2996           if (generate)
2997             {
2998               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2999               emit_constant_insn (cond,
3000                                   gen_rtx_SET (VOIDmode, sub,
3001                                                gen_rtx_NOT (mode, source)));
3002               source = sub;
3003               if (subtargets)
3004                 sub = gen_reg_rtx (mode);
3005               emit_constant_insn (cond,
3006                                   gen_rtx_SET (VOIDmode, sub,
3007                                                gen_rtx_AND (mode, source,
3008                                                             GEN_INT (temp1))));
3009               emit_constant_insn (cond,
3010                                   gen_rtx_SET (VOIDmode, target,
3011                                                gen_rtx_NOT (mode, sub)));
3012             }
3013           return 3;
3014         }
3015       break;
3016
3017     case AND:
3018       /* See if two shifts will do 2 or more insn's worth of work.  */
3019       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
3020         {
3021           HOST_WIDE_INT shift_mask = ((0xffffffff
3022                                        << (32 - clear_sign_bit_copies))
3023                                       & 0xffffffff);
3024
3025           if ((remainder | shift_mask) != 0xffffffff)
3026             {
3027               if (generate)
3028                 {
3029                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3030                   insns = arm_gen_constant (AND, mode, cond,
3031                                             remainder | shift_mask,
3032                                             new_src, source, subtargets, 1);
3033                   source = new_src;
3034                 }
3035               else
3036                 {
3037                   rtx targ = subtargets ? NULL_RTX : target;
3038                   insns = arm_gen_constant (AND, mode, cond,
3039                                             remainder | shift_mask,
3040                                             targ, source, subtargets, 0);
3041                 }
3042             }
3043
3044           if (generate)
3045             {
3046               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3047               rtx shift = GEN_INT (clear_sign_bit_copies);
3048
3049               emit_insn (gen_ashlsi3 (new_src, source, shift));
3050               emit_insn (gen_lshrsi3 (target, new_src, shift));
3051             }
3052
3053           return insns + 2;
3054         }
3055
3056       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
3057         {
3058           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
3059
3060           if ((remainder | shift_mask) != 0xffffffff)
3061             {
3062               if (generate)
3063                 {
3064                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3065
3066                   insns = arm_gen_constant (AND, mode, cond,
3067                                             remainder | shift_mask,
3068                                             new_src, source, subtargets, 1);
3069                   source = new_src;
3070                 }
3071               else
3072                 {
3073                   rtx targ = subtargets ? NULL_RTX : target;
3074
3075                   insns = arm_gen_constant (AND, mode, cond,
3076                                             remainder | shift_mask,
3077                                             targ, source, subtargets, 0);
3078                 }
3079             }
3080
3081           if (generate)
3082             {
3083               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3084               rtx shift = GEN_INT (clear_zero_bit_copies);
3085
3086               emit_insn (gen_lshrsi3 (new_src, source, shift));
3087               emit_insn (gen_ashlsi3 (target, new_src, shift));
3088             }
3089
3090           return insns + 2;
3091         }
3092
3093       break;
3094
3095     default:
3096       break;
3097     }
3098
3099   for (i = 0; i < 32; i++)
3100     if (remainder & (1 << i))
3101       num_bits_set++;
3102
3103   if ((code == AND)
3104       || (code != IOR && can_invert && num_bits_set > 16))
3105     remainder ^= 0xffffffff;
3106   else if (code == PLUS && num_bits_set > 16)
3107     remainder = (-remainder) & 0xffffffff;
3108
3109   /* For XOR, if more than half the bits are set and there's a sequence
3110      of more than 8 consecutive ones in the pattern then we can XOR by the
3111      inverted constant and then invert the final result; this may save an
3112      instruction and might also lead to the final mvn being merged with
3113      some other operation.  */
3114   else if (code == XOR && num_bits_set > 16
3115            && (count_insns_for_constant (remainder ^ 0xffffffff,
3116                                          find_best_start
3117                                          (remainder ^ 0xffffffff))
3118                < count_insns_for_constant (remainder,
3119                                            find_best_start (remainder))))
3120     {
3121       remainder ^= 0xffffffff;
3122       final_invert = 1;
3123     }
3124   else
3125     {
3126       can_invert = 0;
3127       can_negate = 0;
3128     }
3129
3130   /* Now try and find a way of doing the job in either two or three
3131      instructions.
3132      We start by looking for the largest block of zeros that are aligned on
3133      a 2-bit boundary, we then fill up the temps, wrapping around to the
3134      top of the word when we drop off the bottom.
3135      In the worst case this code should produce no more than four insns.
3136      Thumb-2 constants are shifted, not rotated, so the MSB is always the
3137      best place to start.  */
3138
3139   /* ??? Use thumb2 replicated constants when the high and low halfwords are
3140      the same.  */
3141   {
3142     /* Now start emitting the insns.  */
3143     i = find_best_start (remainder);
3144     do
3145       {
3146         int end;
3147
3148         if (i <= 0)
3149           i += 32;
3150         if (remainder & (3 << (i - 2)))
3151           {
3152             end = i - 8;
3153             if (end < 0)
3154               end += 32;
3155             temp1 = remainder & ((0x0ff << end)
3156                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
3157             remainder &= ~temp1;
3158
3159             if (generate)
3160               {
3161                 rtx new_src, temp1_rtx;
3162
3163                 if (code == SET || code == MINUS)
3164                   {
3165                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
3166                     if (can_invert && code != MINUS)
3167                       temp1 = ~temp1;
3168                   }
3169                 else
3170                   {
3171                     if ((final_invert || remainder) && subtargets)
3172                       new_src = gen_reg_rtx (mode);
3173                     else
3174                       new_src = target;
3175                     if (can_invert)
3176                       temp1 = ~temp1;
3177                     else if (can_negate)
3178                       temp1 = -temp1;
3179                   }
3180
3181                 temp1 = trunc_int_for_mode (temp1, mode);
3182                 temp1_rtx = GEN_INT (temp1);
3183
3184                 if (code == SET)
3185                   ;
3186                 else if (code == MINUS)
3187                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
3188                 else
3189                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
3190
3191                 emit_constant_insn (cond,
3192                                     gen_rtx_SET (VOIDmode, new_src,
3193                                                  temp1_rtx));
3194                 source = new_src;
3195               }
3196
3197             if (code == SET)
3198               {
3199                 can_invert = 0;
3200                 code = PLUS;
3201               }
3202             else if (code == MINUS)
3203               code = PLUS;
3204
3205             insns++;
3206             i -= 8 - step_size;
3207           }
3208         /* Arm allows rotates by a multiple of two. Thumb-2 allows arbitrary
3209            shifts.  */
3210         i -= step_size;
3211       }
3212     while (remainder);
3213   }
3214
3215   if (final_invert)
3216     {
3217       if (generate)
3218         emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
3219                                                gen_rtx_NOT (mode, source)));
3220       insns++;
3221     }
3222
3223   return insns;
3224 }
3225
3226 /* Canonicalize a comparison so that we are more likely to recognize it.
3227    This can be done for a few constant compares, where we can make the
3228    immediate value easier to load.  */
3229
3230 enum rtx_code
3231 arm_canonicalize_comparison (enum rtx_code code, rtx *op0, rtx *op1)
3232 {
3233   enum machine_mode mode;
3234   unsigned HOST_WIDE_INT i, maxval;
3235
3236   mode = GET_MODE (*op0);
3237   if (mode == VOIDmode)
3238     mode = GET_MODE (*op1);
3239
3240   maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
3241
3242   /* For DImode, we have GE/LT/GEU/LTU comparisons.  In ARM mode
3243      we can also use cmp/cmpeq for GTU/LEU.  GT/LE must be either
3244      reversed or (for constant OP1) adjusted to GE/LT.  Similarly
3245      for GTU/LEU in Thumb mode.  */
3246   if (mode == DImode)
3247     {
3248       rtx tem;
3249
3250       /* To keep things simple, always use the Cirrus cfcmp64 if it is
3251          available.  */
3252       if (TARGET_ARM && TARGET_HARD_FLOAT && TARGET_MAVERICK)
3253         return code;
3254
3255       if (code == GT || code == LE
3256           || (!TARGET_ARM && (code == GTU || code == LEU)))
3257         {
3258           /* Missing comparison.  First try to use an available
3259              comparison.  */
3260           if (GET_CODE (*op1) == CONST_INT)
3261             {
3262               i = INTVAL (*op1);
3263               switch (code)
3264                 {
3265                 case GT:
3266                 case LE:
3267                   if (i != maxval
3268                       && arm_const_double_by_immediates (GEN_INT (i + 1)))
3269                     {
3270                       *op1 = GEN_INT (i + 1);
3271                       return code == GT ? GE : LT;
3272                     }
3273                   break;
3274                 case GTU:
3275                 case LEU:
3276                   if (i != ~((unsigned HOST_WIDE_INT) 0)
3277                       && arm_const_double_by_immediates (GEN_INT (i + 1)))
3278                     {
3279                       *op1 = GEN_INT (i + 1);
3280                       return code == GTU ? GEU : LTU;
3281                     }
3282                   break;
3283                 default:
3284                   gcc_unreachable ();
3285                 }
3286             }
3287
3288           /* If that did not work, reverse the condition.  */
3289           tem = *op0;
3290           *op0 = *op1;
3291           *op1 = tem;
3292           return swap_condition (code);
3293         }
3294
3295       return code;
3296     }
3297
3298   /* Comparisons smaller than DImode.  Only adjust comparisons against
3299      an out-of-range constant.  */
3300   if (GET_CODE (*op1) != CONST_INT
3301       || const_ok_for_arm (INTVAL (*op1))
3302       || const_ok_for_arm (- INTVAL (*op1)))
3303     return code;
3304
3305   i = INTVAL (*op1);
3306
3307   switch (code)
3308     {
3309     case EQ:
3310     case NE:
3311       return code;
3312
3313     case GT:
3314     case LE:
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 == GT ? GE : LT;
3320         }
3321       break;
3322
3323     case GE:
3324     case LT:
3325       if (i != ~maxval
3326           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
3327         {
3328           *op1 = GEN_INT (i - 1);
3329           return code == GE ? GT : LE;
3330         }
3331       break;
3332
3333     case GTU:
3334     case LEU:
3335       if (i != ~((unsigned HOST_WIDE_INT) 0)
3336           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
3337         {
3338           *op1 = GEN_INT (i + 1);
3339           return code == GTU ? GEU : LTU;
3340         }
3341       break;
3342
3343     case GEU:
3344     case LTU:
3345       if (i != 0
3346           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
3347         {
3348           *op1 = GEN_INT (i - 1);
3349           return code == GEU ? GTU : LEU;
3350         }
3351       break;
3352
3353     default:
3354       gcc_unreachable ();
3355     }
3356
3357   return code;
3358 }
3359
3360
3361 /* Define how to find the value returned by a function.  */
3362
3363 static rtx
3364 arm_function_value(const_tree type, const_tree func,
3365                    bool outgoing ATTRIBUTE_UNUSED)
3366 {
3367   enum machine_mode mode;
3368   int unsignedp ATTRIBUTE_UNUSED;
3369   rtx r ATTRIBUTE_UNUSED;
3370
3371   mode = TYPE_MODE (type);
3372
3373   if (TARGET_AAPCS_BASED)
3374     return aapcs_allocate_return_reg (mode, type, func);
3375
3376   /* Promote integer types.  */
3377   if (INTEGRAL_TYPE_P (type))
3378     mode = arm_promote_function_mode (type, mode, &unsignedp, func, 1);
3379
3380   /* Promotes small structs returned in a register to full-word size
3381      for big-endian AAPCS.  */
3382   if (arm_return_in_msb (type))
3383     {
3384       HOST_WIDE_INT size = int_size_in_bytes (type);
3385       if (size % UNITS_PER_WORD != 0)
3386         {
3387           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
3388           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
3389         }
3390     }
3391
3392   return LIBCALL_VALUE (mode);
3393 }
3394
3395 static int
3396 libcall_eq (const void *p1, const void *p2)
3397 {
3398   return rtx_equal_p ((const_rtx) p1, (const_rtx) p2);
3399 }
3400
3401 static hashval_t
3402 libcall_hash (const void *p1)
3403 {
3404   return hash_rtx ((const_rtx) p1, VOIDmode, NULL, NULL, FALSE);
3405 }
3406
3407 static void
3408 add_libcall (htab_t htab, rtx libcall)
3409 {
3410   *htab_find_slot (htab, libcall, INSERT) = libcall;
3411 }
3412
3413 static bool
3414 arm_libcall_uses_aapcs_base (const_rtx libcall)
3415 {
3416   static bool init_done = false;
3417   static htab_t libcall_htab;
3418
3419   if (!init_done)
3420     {
3421       init_done = true;
3422
3423       libcall_htab = htab_create (31, libcall_hash, libcall_eq,
3424                                   NULL);
3425       add_libcall (libcall_htab,
3426                    convert_optab_libfunc (sfloat_optab, SFmode, SImode));
3427       add_libcall (libcall_htab,
3428                    convert_optab_libfunc (sfloat_optab, DFmode, SImode));
3429       add_libcall (libcall_htab,
3430                    convert_optab_libfunc (sfloat_optab, SFmode, DImode));
3431       add_libcall (libcall_htab,
3432                    convert_optab_libfunc (sfloat_optab, DFmode, DImode));
3433       
3434       add_libcall (libcall_htab,
3435                    convert_optab_libfunc (ufloat_optab, SFmode, SImode));
3436       add_libcall (libcall_htab,
3437                    convert_optab_libfunc (ufloat_optab, DFmode, SImode));
3438       add_libcall (libcall_htab,
3439                    convert_optab_libfunc (ufloat_optab, SFmode, DImode));
3440       add_libcall (libcall_htab,
3441                    convert_optab_libfunc (ufloat_optab, DFmode, DImode));
3442
3443       add_libcall (libcall_htab,
3444                    convert_optab_libfunc (sext_optab, SFmode, HFmode));
3445       add_libcall (libcall_htab,
3446                    convert_optab_libfunc (trunc_optab, HFmode, SFmode));
3447       add_libcall (libcall_htab,
3448                    convert_optab_libfunc (sfix_optab, DImode, DFmode));
3449       add_libcall (libcall_htab,
3450                    convert_optab_libfunc (ufix_optab, DImode, DFmode));
3451       add_libcall (libcall_htab,
3452                    convert_optab_libfunc (sfix_optab, DImode, SFmode));
3453       add_libcall (libcall_htab,
3454                    convert_optab_libfunc (ufix_optab, DImode, SFmode));
3455     }
3456
3457   return libcall && htab_find (libcall_htab, libcall) != NULL;
3458 }
3459
3460 rtx
3461 arm_libcall_value (enum machine_mode mode, const_rtx libcall)
3462 {
3463   if (TARGET_AAPCS_BASED && arm_pcs_default != ARM_PCS_AAPCS
3464       && GET_MODE_CLASS (mode) == MODE_FLOAT)
3465     {
3466       /* The following libcalls return their result in integer registers,
3467          even though they return a floating point value.  */
3468       if (arm_libcall_uses_aapcs_base (libcall))
3469         return gen_rtx_REG (mode, ARG_REGISTER(1));
3470
3471     }
3472
3473   return LIBCALL_VALUE (mode);
3474 }
3475
3476 /* Determine the amount of memory needed to store the possible return
3477    registers of an untyped call.  */
3478 int
3479 arm_apply_result_size (void)
3480 {
3481   int size = 16;
3482
3483   if (TARGET_32BIT)
3484     {
3485       if (TARGET_HARD_FLOAT_ABI)
3486         {
3487           if (TARGET_VFP)
3488             size += 32;
3489           if (TARGET_FPA)
3490             size += 12;
3491           if (TARGET_MAVERICK)
3492             size += 8;
3493         }
3494       if (TARGET_IWMMXT_ABI)
3495         size += 8;
3496     }
3497
3498   return size;
3499 }
3500
3501 /* Decide whether TYPE should be returned in memory (true)
3502    or in a register (false).  FNTYPE is the type of the function making
3503    the call.  */
3504 static bool
3505 arm_return_in_memory (const_tree type, const_tree fntype)
3506 {
3507   HOST_WIDE_INT size;
3508
3509   size = int_size_in_bytes (type);  /* Negative if not fixed size.  */
3510
3511   if (TARGET_AAPCS_BASED)
3512     {
3513       /* Simple, non-aggregate types (ie not including vectors and
3514          complex) are always returned in a register (or registers).
3515          We don't care about which register here, so we can short-cut
3516          some of the detail.  */
3517       if (!AGGREGATE_TYPE_P (type)
3518           && TREE_CODE (type) != VECTOR_TYPE
3519           && TREE_CODE (type) != COMPLEX_TYPE)
3520         return false;
3521
3522       /* Any return value that is no larger than one word can be
3523          returned in r0.  */
3524       if (((unsigned HOST_WIDE_INT) size) <= UNITS_PER_WORD)
3525         return false;
3526
3527       /* Check any available co-processors to see if they accept the
3528          type as a register candidate (VFP, for example, can return
3529          some aggregates in consecutive registers).  These aren't
3530          available if the call is variadic.  */
3531       if (aapcs_select_return_coproc (type, fntype) >= 0)
3532         return false;
3533
3534       /* Vector values should be returned using ARM registers, not
3535          memory (unless they're over 16 bytes, which will break since
3536          we only have four call-clobbered registers to play with).  */
3537       if (TREE_CODE (type) == VECTOR_TYPE)
3538         return (size < 0 || size > (4 * UNITS_PER_WORD));
3539
3540       /* The rest go in memory.  */
3541       return true;
3542     }
3543
3544   if (TREE_CODE (type) == VECTOR_TYPE)
3545     return (size < 0 || size > (4 * UNITS_PER_WORD));
3546
3547   if (!AGGREGATE_TYPE_P (type) &&
3548       (TREE_CODE (type) != VECTOR_TYPE))
3549     /* All simple types are returned in registers.  */
3550     return false;
3551
3552   if (arm_abi != ARM_ABI_APCS)
3553     {
3554       /* ATPCS and later return aggregate types in memory only if they are
3555          larger than a word (or are variable size).  */
3556       return (size < 0 || size > UNITS_PER_WORD);
3557     }
3558
3559   /* For the arm-wince targets we choose to be compatible with Microsoft's
3560      ARM and Thumb compilers, which always return aggregates in memory.  */
3561 #ifndef ARM_WINCE
3562   /* All structures/unions bigger than one word are returned in memory.
3563      Also catch the case where int_size_in_bytes returns -1.  In this case
3564      the aggregate is either huge or of variable size, and in either case
3565      we will want to return it via memory and not in a register.  */
3566   if (size < 0 || size > UNITS_PER_WORD)
3567     return true;
3568
3569   if (TREE_CODE (type) == RECORD_TYPE)
3570     {
3571       tree field;
3572
3573       /* For a struct the APCS says that we only return in a register
3574          if the type is 'integer like' and every addressable element
3575          has an offset of zero.  For practical purposes this means
3576          that the structure can have at most one non bit-field element
3577          and that this element must be the first one in the structure.  */
3578
3579       /* Find the first field, ignoring non FIELD_DECL things which will
3580          have been created by C++.  */
3581       for (field = TYPE_FIELDS (type);
3582            field && TREE_CODE (field) != FIELD_DECL;
3583            field = DECL_CHAIN (field))
3584         continue;
3585
3586       if (field == NULL)
3587         return false; /* An empty structure.  Allowed by an extension to ANSI C.  */
3588
3589       /* Check that the first field is valid for returning in a register.  */
3590
3591       /* ... Floats are not allowed */
3592       if (FLOAT_TYPE_P (TREE_TYPE (field)))
3593         return true;
3594
3595       /* ... Aggregates that are not themselves valid for returning in
3596          a register are not allowed.  */
3597       if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
3598         return true;
3599
3600       /* Now check the remaining fields, if any.  Only bitfields are allowed,
3601          since they are not addressable.  */
3602       for (field = DECL_CHAIN (field);
3603            field;
3604            field = DECL_CHAIN (field))
3605         {
3606           if (TREE_CODE (field) != FIELD_DECL)
3607             continue;
3608
3609           if (!DECL_BIT_FIELD_TYPE (field))
3610             return true;
3611         }
3612
3613       return false;
3614     }
3615
3616   if (TREE_CODE (type) == UNION_TYPE)
3617     {
3618       tree field;
3619
3620       /* Unions can be returned in registers if every element is
3621          integral, or can be returned in an integer register.  */
3622       for (field = TYPE_FIELDS (type);
3623            field;
3624            field = DECL_CHAIN (field))
3625         {
3626           if (TREE_CODE (field) != FIELD_DECL)
3627             continue;
3628
3629           if (FLOAT_TYPE_P (TREE_TYPE (field)))
3630             return true;
3631
3632           if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
3633             return true;
3634         }
3635
3636       return false;
3637     }
3638 #endif /* not ARM_WINCE */
3639
3640   /* Return all other types in memory.  */
3641   return true;
3642 }
3643
3644 /* Indicate whether or not words of a double are in big-endian order.  */
3645
3646 int
3647 arm_float_words_big_endian (void)
3648 {
3649   if (TARGET_MAVERICK)
3650     return 0;
3651
3652   /* For FPA, float words are always big-endian.  For VFP, floats words
3653      follow the memory system mode.  */
3654
3655   if (TARGET_FPA)
3656     {
3657       return 1;
3658     }
3659
3660   if (TARGET_VFP)
3661     return (TARGET_BIG_END ? 1 : 0);
3662
3663   return 1;
3664 }
3665
3666 const struct pcs_attribute_arg
3667 {
3668   const char *arg;
3669   enum arm_pcs value;
3670 } pcs_attribute_args[] =
3671   {
3672     {"aapcs", ARM_PCS_AAPCS},
3673     {"aapcs-vfp", ARM_PCS_AAPCS_VFP},
3674 #if 0
3675     /* We could recognize these, but changes would be needed elsewhere
3676      * to implement them.  */
3677     {"aapcs-iwmmxt", ARM_PCS_AAPCS_IWMMXT},
3678     {"atpcs", ARM_PCS_ATPCS},
3679     {"apcs", ARM_PCS_APCS},
3680 #endif
3681     {NULL, ARM_PCS_UNKNOWN}
3682   };
3683
3684 static enum arm_pcs
3685 arm_pcs_from_attribute (tree attr)
3686 {
3687   const struct pcs_attribute_arg *ptr;
3688   const char *arg;
3689
3690   /* Get the value of the argument.  */
3691   if (TREE_VALUE (attr) == NULL_TREE
3692       || TREE_CODE (TREE_VALUE (attr)) != STRING_CST)
3693     return ARM_PCS_UNKNOWN;
3694
3695   arg = TREE_STRING_POINTER (TREE_VALUE (attr));
3696
3697   /* Check it against the list of known arguments.  */
3698   for (ptr = pcs_attribute_args; ptr->arg != NULL; ptr++)
3699     if (streq (arg, ptr->arg))
3700       return ptr->value;
3701
3702   /* An unrecognized interrupt type.  */
3703   return ARM_PCS_UNKNOWN;
3704 }
3705
3706 /* Get the PCS variant to use for this call.  TYPE is the function's type
3707    specification, DECL is the specific declartion.  DECL may be null if
3708    the call could be indirect or if this is a library call.  */
3709 static enum arm_pcs
3710 arm_get_pcs_model (const_tree type, const_tree decl)
3711 {
3712   bool user_convention = false;
3713   enum arm_pcs user_pcs = arm_pcs_default;
3714   tree attr;
3715
3716   gcc_assert (type);
3717
3718   attr = lookup_attribute ("pcs", TYPE_ATTRIBUTES (type));
3719   if (attr)
3720     {
3721       user_pcs = arm_pcs_from_attribute (TREE_VALUE (attr));
3722       user_convention = true;
3723     }
3724
3725   if (TARGET_AAPCS_BASED)
3726     {
3727       /* Detect varargs functions.  These always use the base rules
3728          (no argument is ever a candidate for a co-processor
3729          register).  */
3730       bool base_rules = stdarg_p (type);
3731       
3732       if (user_convention)
3733         {
3734           if (user_pcs > ARM_PCS_AAPCS_LOCAL)
3735             sorry ("Non-AAPCS derived PCS variant");
3736           else if (base_rules && user_pcs != ARM_PCS_AAPCS)
3737             error ("Variadic functions must use the base AAPCS variant");
3738         }
3739
3740       if (base_rules)
3741         return ARM_PCS_AAPCS;
3742       else if (user_convention)
3743         return user_pcs;
3744       else if (decl && flag_unit_at_a_time)
3745         {
3746           /* Local functions never leak outside this compilation unit,
3747              so we are free to use whatever conventions are
3748              appropriate.  */
3749           /* FIXME: remove CONST_CAST_TREE when cgraph is constified.  */
3750           struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE(decl));
3751           if (i && i->local)
3752             return ARM_PCS_AAPCS_LOCAL;
3753         }
3754     }
3755   else if (user_convention && user_pcs != arm_pcs_default)
3756     sorry ("PCS variant");
3757
3758   /* For everything else we use the target's default.  */
3759   return arm_pcs_default;
3760 }
3761
3762
3763 static void
3764 aapcs_vfp_cum_init (CUMULATIVE_ARGS *pcum  ATTRIBUTE_UNUSED,
3765                     const_tree fntype ATTRIBUTE_UNUSED,
3766                     rtx libcall ATTRIBUTE_UNUSED, 
3767                     const_tree fndecl ATTRIBUTE_UNUSED)
3768 {
3769   /* Record the unallocated VFP registers.  */
3770   pcum->aapcs_vfp_regs_free = (1 << NUM_VFP_ARG_REGS) - 1;
3771   pcum->aapcs_vfp_reg_alloc = 0;
3772 }
3773
3774 /* Walk down the type tree of TYPE counting consecutive base elements.
3775    If *MODEP is VOIDmode, then set it to the first valid floating point
3776    type.  If a non-floating point type is found, or if a floating point
3777    type that doesn't match a non-VOIDmode *MODEP is found, then return -1,
3778    otherwise return the count in the sub-tree.  */
3779 static int
3780 aapcs_vfp_sub_candidate (const_tree type, enum machine_mode *modep)
3781 {
3782   enum machine_mode mode;
3783   HOST_WIDE_INT size;
3784
3785   switch (TREE_CODE (type))
3786     {
3787     case REAL_TYPE:
3788       mode = TYPE_MODE (type);
3789       if (mode != DFmode && mode != SFmode)
3790         return -1;
3791
3792       if (*modep == VOIDmode)
3793         *modep = mode;
3794
3795       if (*modep == mode)
3796         return 1;
3797
3798       break;
3799
3800     case COMPLEX_TYPE:
3801       mode = TYPE_MODE (TREE_TYPE (type));
3802       if (mode != DFmode && mode != SFmode)
3803         return -1;
3804
3805       if (*modep == VOIDmode)
3806         *modep = mode;
3807
3808       if (*modep == mode)
3809         return 2;
3810
3811       break;
3812
3813     case VECTOR_TYPE:
3814       /* Use V2SImode and V4SImode as representatives of all 64-bit
3815          and 128-bit vector types, whether or not those modes are
3816          supported with the present options.  */
3817       size = int_size_in_bytes (type);
3818       switch (size)
3819         {
3820         case 8:
3821           mode = V2SImode;
3822           break;
3823         case 16:
3824           mode = V4SImode;
3825           break;
3826         default:
3827           return -1;
3828         }
3829
3830       if (*modep == VOIDmode)
3831         *modep = mode;
3832
3833       /* Vector modes are considered to be opaque: two vectors are
3834          equivalent for the purposes of being homogeneous aggregates
3835          if they are the same size.  */
3836       if (*modep == mode)
3837         return 1;
3838
3839       break;
3840
3841     case ARRAY_TYPE:
3842       {
3843         int count;
3844         tree index = TYPE_DOMAIN (type);
3845
3846         /* Can't handle incomplete types.  */
3847         if (!COMPLETE_TYPE_P(type))
3848           return -1;
3849
3850         count = aapcs_vfp_sub_candidate (TREE_TYPE (type), modep);
3851         if (count == -1
3852             || !index
3853             || !TYPE_MAX_VALUE (index)
3854             || !host_integerp (TYPE_MAX_VALUE (index), 1)
3855             || !TYPE_MIN_VALUE (index)
3856             || !host_integerp (TYPE_MIN_VALUE (index), 1)
3857             || count < 0)
3858           return -1;
3859
3860         count *= (1 + tree_low_cst (TYPE_MAX_VALUE (index), 1)
3861                       - tree_low_cst (TYPE_MIN_VALUE (index), 1));
3862
3863         /* There must be no padding.  */
3864         if (!host_integerp (TYPE_SIZE (type), 1)
3865             || (tree_low_cst (TYPE_SIZE (type), 1)
3866                 != count * GET_MODE_BITSIZE (*modep)))
3867           return -1;
3868
3869         return count;
3870       }
3871       
3872     case RECORD_TYPE:
3873       {
3874         int count = 0;
3875         int sub_count;
3876         tree field;
3877
3878         /* Can't handle incomplete types.  */
3879         if (!COMPLETE_TYPE_P(type))
3880           return -1;
3881
3882         for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3883           {
3884             if (TREE_CODE (field) != FIELD_DECL)
3885               continue;
3886
3887             sub_count = aapcs_vfp_sub_candidate (TREE_TYPE (field), modep);
3888             if (sub_count < 0)
3889               return -1;
3890             count += sub_count;
3891           }
3892
3893         /* There must be no padding.  */
3894         if (!host_integerp (TYPE_SIZE (type), 1)
3895             || (tree_low_cst (TYPE_SIZE (type), 1)
3896                 != count * GET_MODE_BITSIZE (*modep)))
3897           return -1;
3898
3899         return count;
3900       }
3901
3902     case UNION_TYPE:
3903     case QUAL_UNION_TYPE:
3904       {
3905         /* These aren't very interesting except in a degenerate case.  */
3906         int count = 0;
3907         int sub_count;
3908         tree field;
3909
3910         /* Can't handle incomplete types.  */
3911         if (!COMPLETE_TYPE_P(type))
3912           return -1;
3913
3914         for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3915           {
3916             if (TREE_CODE (field) != FIELD_DECL)
3917               continue;
3918
3919             sub_count = aapcs_vfp_sub_candidate (TREE_TYPE (field), modep);
3920             if (sub_count < 0)
3921               return -1;
3922             count = count > sub_count ? count : sub_count;
3923           }
3924
3925         /* There must be no padding.  */
3926         if (!host_integerp (TYPE_SIZE (type), 1)
3927             || (tree_low_cst (TYPE_SIZE (type), 1)
3928                 != count * GET_MODE_BITSIZE (*modep)))
3929           return -1;
3930
3931         return count;
3932       }
3933
3934     default:
3935       break;
3936     }
3937
3938   return -1;
3939 }
3940
3941 /* Return true if PCS_VARIANT should use VFP registers.  */
3942 static bool
3943 use_vfp_abi (enum arm_pcs pcs_variant, bool is_double)
3944 {
3945   if (pcs_variant == ARM_PCS_AAPCS_VFP)
3946     {
3947       static bool seen_thumb1_vfp = false;
3948
3949       if (TARGET_THUMB1 && !seen_thumb1_vfp)
3950         {
3951           sorry ("Thumb-1 hard-float VFP ABI");
3952           /* sorry() is not immediately fatal, so only display this once.  */
3953           seen_thumb1_vfp = true;
3954         }
3955
3956       return true;
3957     }
3958
3959   if (pcs_variant != ARM_PCS_AAPCS_LOCAL)
3960     return false;
3961
3962   return (TARGET_32BIT && TARGET_VFP && TARGET_HARD_FLOAT &&
3963           (TARGET_VFP_DOUBLE || !is_double));
3964 }
3965
3966 static bool
3967 aapcs_vfp_is_call_or_return_candidate (enum arm_pcs pcs_variant,
3968                                        enum machine_mode mode, const_tree type,
3969                                        enum machine_mode *base_mode, int *count)
3970 {
3971   enum machine_mode new_mode = VOIDmode;
3972
3973   if (GET_MODE_CLASS (mode) == MODE_FLOAT
3974       || GET_MODE_CLASS (mode) == MODE_VECTOR_INT
3975       || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
3976     {
3977       *count = 1;
3978       new_mode = mode;
3979     }
3980   else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3981     {
3982       *count = 2;
3983       new_mode = (mode == DCmode ? DFmode : SFmode);
3984     }
3985   else if (type && (mode == BLKmode || TREE_CODE (type) == VECTOR_TYPE))
3986     {
3987       int ag_count = aapcs_vfp_sub_candidate (type, &new_mode);
3988
3989       if (ag_count > 0 && ag_count <= 4)
3990         *count = ag_count;
3991       else
3992         return false;
3993     }
3994   else
3995     return false;
3996
3997
3998   if (!use_vfp_abi (pcs_variant, ARM_NUM_REGS (new_mode) > 1))
3999     return false;
4000
4001   *base_mode = new_mode;
4002   return true;
4003 }
4004
4005 static bool
4006 aapcs_vfp_is_return_candidate (enum arm_pcs pcs_variant,
4007                                enum machine_mode mode, const_tree type)
4008 {
4009   int count ATTRIBUTE_UNUSED;
4010   enum machine_mode ag_mode ATTRIBUTE_UNUSED;
4011
4012   if (!use_vfp_abi (pcs_variant, false))
4013     return false;
4014   return aapcs_vfp_is_call_or_return_candidate (pcs_variant, mode, type,
4015                                                 &ag_mode, &count);
4016 }
4017
4018 static bool
4019 aapcs_vfp_is_call_candidate (CUMULATIVE_ARGS *pcum, enum machine_mode mode, 
4020                              const_tree type)
4021 {
4022   if (!use_vfp_abi (pcum->pcs_variant, false))
4023     return false;
4024
4025   return aapcs_vfp_is_call_or_return_candidate (pcum->pcs_variant, mode, type,
4026                                                 &pcum->aapcs_vfp_rmode,
4027                                                 &pcum->aapcs_vfp_rcount);
4028 }
4029
4030 static bool
4031 aapcs_vfp_allocate (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4032                     const_tree type  ATTRIBUTE_UNUSED)
4033 {
4034   int shift = GET_MODE_SIZE (pcum->aapcs_vfp_rmode) / GET_MODE_SIZE (SFmode);
4035   unsigned mask = (1 << (shift * pcum->aapcs_vfp_rcount)) - 1;
4036   int regno;
4037   
4038   for (regno = 0; regno < NUM_VFP_ARG_REGS; regno += shift)
4039     if (((pcum->aapcs_vfp_regs_free >> regno) & mask) == mask)
4040       {
4041         pcum->aapcs_vfp_reg_alloc = mask << regno;
4042         if (mode == BLKmode || (mode == TImode && !TARGET_NEON))
4043           {
4044             int i;
4045             int rcount = pcum->aapcs_vfp_rcount;
4046             int rshift = shift;
4047             enum machine_mode rmode = pcum->aapcs_vfp_rmode;
4048             rtx par;
4049             if (!TARGET_NEON)
4050               {
4051                 /* Avoid using unsupported vector modes.  */
4052                 if (rmode == V2SImode)
4053                   rmode = DImode;
4054                 else if (rmode == V4SImode)
4055                   {
4056                     rmode = DImode;
4057                     rcount *= 2;
4058                     rshift /= 2;
4059                   }
4060               }
4061             par = gen_rtx_PARALLEL (mode, rtvec_alloc (rcount));
4062             for (i = 0; i < rcount; i++)
4063               {
4064                 rtx tmp = gen_rtx_REG (rmode, 
4065                                        FIRST_VFP_REGNUM + regno + i * rshift);
4066                 tmp = gen_rtx_EXPR_LIST
4067                   (VOIDmode, tmp, 
4068                    GEN_INT (i * GET_MODE_SIZE (rmode)));
4069                 XVECEXP (par, 0, i) = tmp;
4070               }
4071
4072             pcum->aapcs_reg = par;
4073           }
4074         else
4075           pcum->aapcs_reg = gen_rtx_REG (mode, FIRST_VFP_REGNUM + regno);
4076         return true;
4077       }
4078   return false;
4079 }
4080
4081 static rtx
4082 aapcs_vfp_allocate_return_reg (enum arm_pcs pcs_variant ATTRIBUTE_UNUSED,
4083                                enum machine_mode mode,
4084                                const_tree type ATTRIBUTE_UNUSED)
4085 {
4086   if (!use_vfp_abi (pcs_variant, false))
4087     return false;
4088
4089   if (mode == BLKmode || (mode == TImode && !TARGET_NEON))
4090     {
4091       int count;
4092       enum machine_mode ag_mode;
4093       int i;
4094       rtx par;
4095       int shift;
4096       
4097       aapcs_vfp_is_call_or_return_candidate (pcs_variant, mode, type,
4098                                              &ag_mode, &count);
4099
4100       if (!TARGET_NEON)
4101         {
4102           if (ag_mode == V2SImode)
4103             ag_mode = DImode;
4104           else if (ag_mode == V4SImode)
4105             {
4106               ag_mode = DImode;
4107               count *= 2;
4108             }
4109         }
4110       shift = GET_MODE_SIZE(ag_mode) / GET_MODE_SIZE(SFmode);
4111       par = gen_rtx_PARALLEL (mode, rtvec_alloc (count));
4112       for (i = 0; i < count; i++)
4113         {
4114           rtx tmp = gen_rtx_REG (ag_mode, FIRST_VFP_REGNUM + i * shift);
4115           tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, 
4116                                    GEN_INT (i * GET_MODE_SIZE (ag_mode)));
4117           XVECEXP (par, 0, i) = tmp;
4118         }
4119
4120       return par;
4121     }
4122
4123   return gen_rtx_REG (mode, FIRST_VFP_REGNUM);
4124 }
4125
4126 static void
4127 aapcs_vfp_advance (CUMULATIVE_ARGS *pcum  ATTRIBUTE_UNUSED,
4128                    enum machine_mode mode  ATTRIBUTE_UNUSED,
4129                    const_tree type  ATTRIBUTE_UNUSED)
4130 {
4131   pcum->aapcs_vfp_regs_free &= ~pcum->aapcs_vfp_reg_alloc;
4132   pcum->aapcs_vfp_reg_alloc = 0;
4133   return;
4134 }
4135
4136 #define AAPCS_CP(X)                             \
4137   {                                             \
4138     aapcs_ ## X ## _cum_init,                   \
4139     aapcs_ ## X ## _is_call_candidate,          \
4140     aapcs_ ## X ## _allocate,                   \
4141     aapcs_ ## X ## _is_return_candidate,        \
4142     aapcs_ ## X ## _allocate_return_reg,        \
4143     aapcs_ ## X ## _advance                     \
4144   }
4145
4146 /* Table of co-processors that can be used to pass arguments in
4147    registers.  Idealy no arugment should be a candidate for more than
4148    one co-processor table entry, but the table is processed in order
4149    and stops after the first match.  If that entry then fails to put
4150    the argument into a co-processor register, the argument will go on
4151    the stack.  */
4152 static struct 
4153 {
4154   /* Initialize co-processor related state in CUMULATIVE_ARGS structure.  */
4155   void (*cum_init) (CUMULATIVE_ARGS *, const_tree, rtx, const_tree);
4156
4157   /* Return true if an argument of mode MODE (or type TYPE if MODE is
4158      BLKmode) is a candidate for this co-processor's registers; this
4159      function should ignore any position-dependent state in
4160      CUMULATIVE_ARGS and only use call-type dependent information.  */
4161   bool (*is_call_candidate) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4162
4163   /* Return true if the argument does get a co-processor register; it
4164      should set aapcs_reg to an RTX of the register allocated as is
4165      required for a return from FUNCTION_ARG.  */
4166   bool (*allocate) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4167
4168   /* Return true if a result of mode MODE (or type TYPE if MODE is
4169      BLKmode) is can be returned in this co-processor's registers.  */
4170   bool (*is_return_candidate) (enum arm_pcs, enum machine_mode, const_tree);
4171
4172   /* Allocate and return an RTX element to hold the return type of a
4173      call, this routine must not fail and will only be called if
4174      is_return_candidate returned true with the same parameters.  */
4175   rtx (*allocate_return_reg) (enum arm_pcs, enum machine_mode, const_tree);
4176
4177   /* Finish processing this argument and prepare to start processing
4178      the next one.  */
4179   void (*advance) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4180 } aapcs_cp_arg_layout[ARM_NUM_COPROC_SLOTS] =
4181   {
4182     AAPCS_CP(vfp)
4183   };
4184
4185 #undef AAPCS_CP
4186
4187 static int
4188 aapcs_select_call_coproc (CUMULATIVE_ARGS *pcum, enum machine_mode mode, 
4189                           const_tree type)
4190 {
4191   int i;
4192
4193   for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4194     if (aapcs_cp_arg_layout[i].is_call_candidate (pcum, mode, type))
4195       return i;
4196
4197   return -1;
4198 }
4199
4200 static int
4201 aapcs_select_return_coproc (const_tree type, const_tree fntype)
4202 {
4203   /* We aren't passed a decl, so we can't check that a call is local.
4204      However, it isn't clear that that would be a win anyway, since it
4205      might limit some tail-calling opportunities.  */
4206   enum arm_pcs pcs_variant;
4207
4208   if (fntype)
4209     {
4210       const_tree fndecl = NULL_TREE;
4211
4212       if (TREE_CODE (fntype) == FUNCTION_DECL)
4213         {
4214           fndecl = fntype;
4215           fntype = TREE_TYPE (fntype);
4216         }
4217
4218       pcs_variant = arm_get_pcs_model (fntype, fndecl);
4219     }
4220   else
4221     pcs_variant = arm_pcs_default;
4222
4223   if (pcs_variant != ARM_PCS_AAPCS)
4224     {
4225       int i;
4226
4227       for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4228         if (aapcs_cp_arg_layout[i].is_return_candidate (pcs_variant, 
4229                                                         TYPE_MODE (type),
4230                                                         type))
4231           return i;
4232     }
4233   return -1;
4234 }
4235
4236 static rtx
4237 aapcs_allocate_return_reg (enum machine_mode mode, const_tree type,
4238                            const_tree fntype)
4239 {
4240   /* We aren't passed a decl, so we can't check that a call is local.
4241      However, it isn't clear that that would be a win anyway, since it
4242      might limit some tail-calling opportunities.  */
4243   enum arm_pcs pcs_variant;
4244   int unsignedp ATTRIBUTE_UNUSED;
4245
4246   if (fntype)
4247     {
4248       const_tree fndecl = NULL_TREE;
4249
4250       if (TREE_CODE (fntype) == FUNCTION_DECL)
4251         {
4252           fndecl = fntype;
4253           fntype = TREE_TYPE (fntype);
4254         }
4255
4256       pcs_variant = arm_get_pcs_model (fntype, fndecl);
4257     }
4258   else
4259     pcs_variant = arm_pcs_default;
4260
4261   /* Promote integer types.  */
4262   if (type && INTEGRAL_TYPE_P (type))
4263     mode = arm_promote_function_mode (type, mode, &unsignedp, fntype, 1);
4264
4265   if (pcs_variant != ARM_PCS_AAPCS)
4266     {
4267       int i;
4268
4269       for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4270         if (aapcs_cp_arg_layout[i].is_return_candidate (pcs_variant, mode,
4271                                                         type))
4272           return aapcs_cp_arg_layout[i].allocate_return_reg (pcs_variant,
4273                                                              mode, type);
4274     }
4275
4276   /* Promotes small structs returned in a register to full-word size
4277      for big-endian AAPCS.  */
4278   if (type && arm_return_in_msb (type))
4279     {
4280       HOST_WIDE_INT size = int_size_in_bytes (type);
4281       if (size % UNITS_PER_WORD != 0)
4282         {
4283           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
4284           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
4285         }
4286     }
4287
4288   return gen_rtx_REG (mode, R0_REGNUM);
4289 }
4290
4291 rtx
4292 aapcs_libcall_value (enum machine_mode mode)
4293 {
4294   return aapcs_allocate_return_reg (mode, NULL_TREE, NULL_TREE);
4295 }
4296
4297 /* Lay out a function argument using the AAPCS rules.  The rule
4298    numbers referred to here are those in the AAPCS.  */
4299 static void
4300 aapcs_layout_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4301                   const_tree type, bool named)
4302 {
4303   int nregs, nregs2;
4304   int ncrn;
4305
4306   /* We only need to do this once per argument.  */
4307   if (pcum->aapcs_arg_processed)
4308     return;
4309
4310   pcum->aapcs_arg_processed = true;
4311
4312   /* Special case: if named is false then we are handling an incoming
4313      anonymous argument which is on the stack.  */
4314   if (!named)
4315     return;
4316   
4317   /* Is this a potential co-processor register candidate?  */
4318   if (pcum->pcs_variant != ARM_PCS_AAPCS)
4319     {
4320       int slot = aapcs_select_call_coproc (pcum, mode, type);
4321       pcum->aapcs_cprc_slot = slot;
4322
4323       /* We don't have to apply any of the rules from part B of the
4324          preparation phase, these are handled elsewhere in the
4325          compiler.  */
4326
4327       if (slot >= 0)
4328         {
4329           /* A Co-processor register candidate goes either in its own
4330              class of registers or on the stack.  */
4331           if (!pcum->aapcs_cprc_failed[slot])
4332             {
4333               /* C1.cp - Try to allocate the argument to co-processor
4334                  registers.  */
4335               if (aapcs_cp_arg_layout[slot].allocate (pcum, mode, type))
4336                 return;
4337
4338               /* C2.cp - Put the argument on the stack and note that we
4339                  can't assign any more candidates in this slot.  We also
4340                  need to note that we have allocated stack space, so that
4341                  we won't later try to split a non-cprc candidate between
4342                  core registers and the stack.  */
4343               pcum->aapcs_cprc_failed[slot] = true;
4344               pcum->can_split = false;
4345             }
4346
4347           /* We didn't get a register, so this argument goes on the
4348              stack.  */
4349           gcc_assert (pcum->can_split == false);
4350           return;
4351         }
4352     }
4353
4354   /* C3 - For double-word aligned arguments, round the NCRN up to the
4355      next even number.  */
4356   ncrn = pcum->aapcs_ncrn;
4357   if ((ncrn & 1) && arm_needs_doubleword_align (mode, type))
4358     ncrn++;
4359
4360   nregs = ARM_NUM_REGS2(mode, type);
4361
4362   /* Sigh, this test should really assert that nregs > 0, but a GCC
4363      extension allows empty structs and then gives them empty size; it
4364      then allows such a structure to be passed by value.  For some of
4365      the code below we have to pretend that such an argument has
4366      non-zero size so that we 'locate' it correctly either in
4367      registers or on the stack.  */
4368   gcc_assert (nregs >= 0);
4369
4370   nregs2 = nregs ? nregs : 1;
4371
4372   /* C4 - Argument fits entirely in core registers.  */
4373   if (ncrn + nregs2 <= NUM_ARG_REGS)
4374     {
4375       pcum->aapcs_reg = gen_rtx_REG (mode, ncrn);
4376       pcum->aapcs_next_ncrn = ncrn + nregs;
4377       return;
4378     }
4379
4380   /* C5 - Some core registers left and there are no arguments already
4381      on the stack: split this argument between the remaining core
4382      registers and the stack.  */
4383   if (ncrn < NUM_ARG_REGS && pcum->can_split)
4384     {
4385       pcum->aapcs_reg = gen_rtx_REG (mode, ncrn);
4386       pcum->aapcs_next_ncrn = NUM_ARG_REGS;
4387       pcum->aapcs_partial = (NUM_ARG_REGS - ncrn) * UNITS_PER_WORD;
4388       return;
4389     }
4390
4391   /* C6 - NCRN is set to 4.  */
4392   pcum->aapcs_next_ncrn = NUM_ARG_REGS;
4393
4394   /* C7,C8 - arugment goes on the stack.  We have nothing to do here.  */
4395   return;
4396 }
4397
4398 /* Initialize a variable CUM of type CUMULATIVE_ARGS
4399    for a call to a function whose data type is FNTYPE.
4400    For a library call, FNTYPE is NULL.  */
4401 void
4402 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
4403                           rtx libname,
4404                           tree fndecl ATTRIBUTE_UNUSED)
4405 {
4406   /* Long call handling.  */
4407   if (fntype)
4408     pcum->pcs_variant = arm_get_pcs_model (fntype, fndecl);
4409   else
4410     pcum->pcs_variant = arm_pcs_default;
4411
4412   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4413     {
4414       if (arm_libcall_uses_aapcs_base (libname))
4415         pcum->pcs_variant = ARM_PCS_AAPCS;
4416  
4417       pcum->aapcs_ncrn = pcum->aapcs_next_ncrn = 0;
4418       pcum->aapcs_reg = NULL_RTX;
4419       pcum->aapcs_partial = 0;
4420       pcum->aapcs_arg_processed = false;
4421       pcum->aapcs_cprc_slot = -1;
4422       pcum->can_split = true;
4423
4424       if (pcum->pcs_variant != ARM_PCS_AAPCS)
4425         {
4426           int i;
4427
4428           for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4429             {
4430               pcum->aapcs_cprc_failed[i] = false;
4431               aapcs_cp_arg_layout[i].cum_init (pcum, fntype, libname, fndecl);
4432             }
4433         }
4434       return;
4435     }
4436
4437   /* Legacy ABIs */
4438
4439   /* On the ARM, the offset starts at 0.  */
4440   pcum->nregs = 0;
4441   pcum->iwmmxt_nregs = 0;
4442   pcum->can_split = true;
4443
4444   /* Varargs vectors are treated the same as long long.
4445      named_count avoids having to change the way arm handles 'named' */
4446   pcum->named_count = 0;
4447   pcum->nargs = 0;
4448
4449   if (TARGET_REALLY_IWMMXT && fntype)
4450     {
4451       tree fn_arg;
4452
4453       for (fn_arg = TYPE_ARG_TYPES (fntype);
4454            fn_arg;
4455            fn_arg = TREE_CHAIN (fn_arg))
4456         pcum->named_count += 1;
4457
4458       if (! pcum->named_count)
4459         pcum->named_count = INT_MAX;
4460     }
4461 }
4462
4463
4464 /* Return true if mode/type need doubleword alignment.  */
4465 bool
4466 arm_needs_doubleword_align (enum machine_mode mode, const_tree type)
4467 {
4468   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
4469           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
4470 }
4471
4472
4473 /* Determine where to put an argument to a function.
4474    Value is zero to push the argument on the stack,
4475    or a hard register in which to store the argument.
4476
4477    MODE is the argument's machine mode.
4478    TYPE is the data type of the argument (as a tree).
4479     This is null for libcalls where that information may
4480     not be available.
4481    CUM is a variable of type CUMULATIVE_ARGS which gives info about
4482     the preceding args and about the function being called.
4483    NAMED is nonzero if this argument is a named parameter
4484     (otherwise it is an extra parameter matching an ellipsis).
4485
4486    On the ARM, normally the first 16 bytes are passed in registers r0-r3; all
4487    other arguments are passed on the stack.  If (NAMED == 0) (which happens
4488    only in assign_parms, since TARGET_SETUP_INCOMING_VARARGS is
4489    defined), say it is passed in the stack (function_prologue will
4490    indeed make it pass in the stack if necessary).  */
4491
4492 static rtx
4493 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4494                   const_tree type, bool named)
4495 {
4496   int nregs;
4497
4498   /* Handle the special case quickly.  Pick an arbitrary value for op2 of
4499      a call insn (op3 of a call_value insn).  */
4500   if (mode == VOIDmode)
4501     return const0_rtx;
4502
4503   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4504     {
4505       aapcs_layout_arg (pcum, mode, type, named);
4506       return pcum->aapcs_reg;
4507     }
4508
4509   /* Varargs vectors are treated the same as long long.
4510      named_count avoids having to change the way arm handles 'named' */
4511   if (TARGET_IWMMXT_ABI
4512       && arm_vector_mode_supported_p (mode)
4513       && pcum->named_count > pcum->nargs + 1)
4514     {
4515       if (pcum->iwmmxt_nregs <= 9)
4516         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
4517       else
4518         {
4519           pcum->can_split = false;
4520           return NULL_RTX;
4521         }
4522     }
4523
4524   /* Put doubleword aligned quantities in even register pairs.  */
4525   if (pcum->nregs & 1
4526       && ARM_DOUBLEWORD_ALIGN
4527       && arm_needs_doubleword_align (mode, type))
4528     pcum->nregs++;
4529
4530   /* Only allow splitting an arg between regs and memory if all preceding
4531      args were allocated to regs.  For args passed by reference we only count
4532      the reference pointer.  */
4533   if (pcum->can_split)
4534     nregs = 1;
4535   else
4536     nregs = ARM_NUM_REGS2 (mode, type);
4537
4538   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
4539     return NULL_RTX;
4540
4541   return gen_rtx_REG (mode, pcum->nregs);
4542 }
4543
4544 static int
4545 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4546                        tree type, bool named)
4547 {
4548   int nregs = pcum->nregs;
4549
4550   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4551     {
4552       aapcs_layout_arg (pcum, mode, type, named);
4553       return pcum->aapcs_partial;
4554     }
4555
4556   if (TARGET_IWMMXT_ABI && arm_vector_mode_supported_p (mode))
4557     return 0;
4558
4559   if (NUM_ARG_REGS > nregs
4560       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
4561       && pcum->can_split)
4562     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
4563
4564   return 0;
4565 }
4566
4567 /* Update the data in PCUM to advance over an argument
4568    of mode MODE and data type TYPE.
4569    (TYPE is null for libcalls where that information may not be available.)  */
4570
4571 static void
4572 arm_function_arg_advance (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4573                           const_tree type, bool named)
4574 {
4575   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4576     {
4577       aapcs_layout_arg (pcum, mode, type, named);
4578
4579       if (pcum->aapcs_cprc_slot >= 0)
4580         {
4581           aapcs_cp_arg_layout[pcum->aapcs_cprc_slot].advance (pcum, mode,
4582                                                               type);
4583           pcum->aapcs_cprc_slot = -1;
4584         }
4585
4586       /* Generic stuff.  */
4587       pcum->aapcs_arg_processed = false;
4588       pcum->aapcs_ncrn = pcum->aapcs_next_ncrn;
4589       pcum->aapcs_reg = NULL_RTX;
4590       pcum->aapcs_partial = 0;
4591     }
4592   else
4593     {
4594       pcum->nargs += 1;
4595       if (arm_vector_mode_supported_p (mode)
4596           && pcum->named_count > pcum->nargs
4597           && TARGET_IWMMXT_ABI)
4598         pcum->iwmmxt_nregs += 1;
4599       else
4600         pcum->nregs += ARM_NUM_REGS2 (mode, type);
4601     }
4602 }
4603
4604 /* Variable sized types are passed by reference.  This is a GCC
4605    extension to the ARM ABI.  */
4606
4607 static bool
4608 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
4609                        enum machine_mode mode ATTRIBUTE_UNUSED,
4610                        const_tree type, bool named ATTRIBUTE_UNUSED)
4611 {
4612   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
4613 }
4614 \f
4615 /* Encode the current state of the #pragma [no_]long_calls.  */
4616 typedef enum
4617 {
4618   OFF,          /* No #pragma [no_]long_calls is in effect.  */
4619   LONG,         /* #pragma long_calls is in effect.  */
4620   SHORT         /* #pragma no_long_calls is in effect.  */
4621 } arm_pragma_enum;
4622
4623 static arm_pragma_enum arm_pragma_long_calls = OFF;
4624
4625 void
4626 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
4627 {
4628   arm_pragma_long_calls = LONG;
4629 }
4630
4631 void
4632 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
4633 {
4634   arm_pragma_long_calls = SHORT;
4635 }
4636
4637 void
4638 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
4639 {
4640   arm_pragma_long_calls = OFF;
4641 }
4642 \f
4643 /* Handle an attribute requiring a FUNCTION_DECL;
4644    arguments as in struct attribute_spec.handler.  */
4645 static tree
4646 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
4647                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4648 {
4649   if (TREE_CODE (*node) != FUNCTION_DECL)
4650     {
4651       warning (OPT_Wattributes, "%qE attribute only applies to functions",
4652                name);
4653       *no_add_attrs = true;
4654     }
4655
4656   return NULL_TREE;
4657 }
4658
4659 /* Handle an "interrupt" or "isr" attribute;
4660    arguments as in struct attribute_spec.handler.  */
4661 static tree
4662 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
4663                           bool *no_add_attrs)
4664 {
4665   if (DECL_P (*node))
4666     {
4667       if (TREE_CODE (*node) != FUNCTION_DECL)
4668         {
4669           warning (OPT_Wattributes, "%qE attribute only applies to functions",
4670                    name);
4671           *no_add_attrs = true;
4672         }
4673       /* FIXME: the argument if any is checked for type attributes;
4674          should it be checked for decl ones?  */
4675     }
4676   else
4677     {
4678       if (TREE_CODE (*node) == FUNCTION_TYPE
4679           || TREE_CODE (*node) == METHOD_TYPE)
4680         {
4681           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
4682             {
4683               warning (OPT_Wattributes, "%qE attribute ignored",
4684                        name);
4685               *no_add_attrs = true;
4686             }
4687         }
4688       else if (TREE_CODE (*node) == POINTER_TYPE
4689                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
4690                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
4691                && arm_isr_value (args) != ARM_FT_UNKNOWN)
4692         {
4693           *node = build_variant_type_copy (*node);
4694           TREE_TYPE (*node) = build_type_attribute_variant
4695             (TREE_TYPE (*node),
4696              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
4697           *no_add_attrs = true;
4698         }
4699       else
4700         {
4701           /* Possibly pass this attribute on from the type to a decl.  */
4702           if (flags & ((int) ATTR_FLAG_DECL_NEXT
4703                        | (int) ATTR_FLAG_FUNCTION_NEXT
4704                        | (int) ATTR_FLAG_ARRAY_NEXT))
4705             {
4706               *no_add_attrs = true;
4707               return tree_cons (name, args, NULL_TREE);
4708             }
4709           else
4710             {
4711               warning (OPT_Wattributes, "%qE attribute ignored",
4712                        name);
4713             }
4714         }
4715     }
4716
4717   return NULL_TREE;
4718 }
4719
4720 /* Handle a "pcs" attribute; arguments as in struct
4721    attribute_spec.handler.  */
4722 static tree
4723 arm_handle_pcs_attribute (tree *node ATTRIBUTE_UNUSED, tree name, tree args,
4724                           int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4725 {
4726   if (arm_pcs_from_attribute (args) == ARM_PCS_UNKNOWN)
4727     {
4728       warning (OPT_Wattributes, "%qE attribute ignored", name);
4729       *no_add_attrs = true;
4730     }
4731   return NULL_TREE;
4732 }
4733
4734 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
4735 /* Handle the "notshared" attribute.  This attribute is another way of
4736    requesting hidden visibility.  ARM's compiler supports
4737    "__declspec(notshared)"; we support the same thing via an
4738    attribute.  */
4739
4740 static tree
4741 arm_handle_notshared_attribute (tree *node,
4742                                 tree name ATTRIBUTE_UNUSED,
4743                                 tree args ATTRIBUTE_UNUSED,
4744                                 int flags ATTRIBUTE_UNUSED,
4745                                 bool *no_add_attrs)
4746 {
4747   tree decl = TYPE_NAME (*node);
4748
4749   if (decl)
4750     {
4751       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
4752       DECL_VISIBILITY_SPECIFIED (decl) = 1;
4753       *no_add_attrs = false;
4754     }
4755   return NULL_TREE;
4756 }
4757 #endif
4758
4759 /* Return 0 if the attributes for two types are incompatible, 1 if they
4760    are compatible, and 2 if they are nearly compatible (which causes a
4761    warning to be generated).  */
4762 static int
4763 arm_comp_type_attributes (const_tree type1, const_tree type2)
4764 {
4765   int l1, l2, s1, s2;
4766
4767   /* Check for mismatch of non-default calling convention.  */
4768   if (TREE_CODE (type1) != FUNCTION_TYPE)
4769     return 1;
4770
4771   /* Check for mismatched call attributes.  */
4772   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
4773   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
4774   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
4775   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
4776
4777   /* Only bother to check if an attribute is defined.  */
4778   if (l1 | l2 | s1 | s2)
4779     {
4780       /* If one type has an attribute, the other must have the same attribute.  */
4781       if ((l1 != l2) || (s1 != s2))
4782         return 0;
4783
4784       /* Disallow mixed attributes.  */
4785       if ((l1 & s2) || (l2 & s1))
4786         return 0;
4787     }
4788
4789   /* Check for mismatched ISR attribute.  */
4790   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
4791   if (! l1)
4792     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
4793   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
4794   if (! l2)
4795     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
4796   if (l1 != l2)
4797     return 0;
4798
4799   return 1;
4800 }
4801
4802 /*  Assigns default attributes to newly defined type.  This is used to
4803     set short_call/long_call attributes for function types of
4804     functions defined inside corresponding #pragma scopes.  */
4805 static void
4806 arm_set_default_type_attributes (tree type)
4807 {
4808   /* Add __attribute__ ((long_call)) to all functions, when
4809      inside #pragma long_calls or __attribute__ ((short_call)),
4810      when inside #pragma no_long_calls.  */
4811   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
4812     {
4813       tree type_attr_list, attr_name;
4814       type_attr_list = TYPE_ATTRIBUTES (type);
4815
4816       if (arm_pragma_long_calls == LONG)
4817         attr_name = get_identifier ("long_call");
4818       else if (arm_pragma_long_calls == SHORT)
4819         attr_name = get_identifier ("short_call");
4820       else
4821         return;
4822
4823       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
4824       TYPE_ATTRIBUTES (type) = type_attr_list;
4825     }
4826 }
4827 \f
4828 /* Return true if DECL is known to be linked into section SECTION.  */
4829
4830 static bool
4831 arm_function_in_section_p (tree decl, section *section)
4832 {
4833   /* We can only be certain about functions defined in the same
4834      compilation unit.  */
4835   if (!TREE_STATIC (decl))
4836     return false;
4837
4838   /* Make sure that SYMBOL always binds to the definition in this
4839      compilation unit.  */
4840   if (!targetm.binds_local_p (decl))
4841     return false;
4842
4843   /* If DECL_SECTION_NAME is set, assume it is trustworthy.  */
4844   if (!DECL_SECTION_NAME (decl))
4845     {
4846       /* Make sure that we will not create a unique section for DECL.  */
4847       if (flag_function_sections || DECL_ONE_ONLY (decl))
4848         return false;
4849     }
4850
4851   return function_section (decl) == section;
4852 }
4853
4854 /* Return nonzero if a 32-bit "long_call" should be generated for
4855    a call from the current function to DECL.  We generate a long_call
4856    if the function:
4857
4858         a.  has an __attribute__((long call))
4859      or b.  is within the scope of a #pragma long_calls
4860      or c.  the -mlong-calls command line switch has been specified
4861
4862    However we do not generate a long call if the function:
4863
4864         d.  has an __attribute__ ((short_call))
4865      or e.  is inside the scope of a #pragma no_long_calls
4866      or f.  is defined in the same section as the current function.  */
4867
4868 bool
4869 arm_is_long_call_p (tree decl)
4870 {
4871   tree attrs;
4872
4873   if (!decl)
4874     return TARGET_LONG_CALLS;
4875
4876   attrs = TYPE_ATTRIBUTES (TREE_TYPE (decl));
4877   if (lookup_attribute ("short_call", attrs))
4878     return false;
4879
4880   /* For "f", be conservative, and only cater for cases in which the
4881      whole of the current function is placed in the same section.  */
4882   if (!flag_reorder_blocks_and_partition
4883       && TREE_CODE (decl) == FUNCTION_DECL
4884       && arm_function_in_section_p (decl, current_function_section ()))
4885     return false;
4886
4887   if (lookup_attribute ("long_call", attrs))
4888     return true;
4889
4890   return TARGET_LONG_CALLS;
4891 }
4892
4893 /* Return nonzero if it is ok to make a tail-call to DECL.  */
4894 static bool
4895 arm_function_ok_for_sibcall (tree decl, tree exp)
4896 {
4897   unsigned long func_type;
4898
4899   if (cfun->machine->sibcall_blocked)
4900     return false;
4901
4902   /* Never tailcall something for which we have no decl, or if we
4903      are generating code for Thumb-1.  */
4904   if (decl == NULL || TARGET_THUMB1)
4905     return false;
4906
4907   /* The PIC register is live on entry to VxWorks PLT entries, so we
4908      must make the call before restoring the PIC register.  */
4909   if (TARGET_VXWORKS_RTP && flag_pic && !targetm.binds_local_p (decl))
4910     return false;
4911
4912   /* Cannot tail-call to long calls, since these are out of range of
4913      a branch instruction.  */
4914   if (arm_is_long_call_p (decl))
4915     return false;
4916
4917   /* If we are interworking and the function is not declared static
4918      then we can't tail-call it unless we know that it exists in this
4919      compilation unit (since it might be a Thumb routine).  */
4920   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
4921     return false;
4922
4923   func_type = arm_current_func_type ();
4924   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
4925   if (IS_INTERRUPT (func_type))
4926     return false;
4927
4928   if (!VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
4929     {
4930       /* Check that the return value locations are the same.  For
4931          example that we aren't returning a value from the sibling in
4932          a VFP register but then need to transfer it to a core
4933          register.  */
4934       rtx a, b;
4935
4936       a = arm_function_value (TREE_TYPE (exp), decl, false);
4937       b = arm_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
4938                               cfun->decl, false);
4939       if (!rtx_equal_p (a, b))
4940         return false;
4941     }
4942
4943   /* Never tailcall if function may be called with a misaligned SP.  */
4944   if (IS_STACKALIGN (func_type))
4945     return false;
4946
4947   /* Everything else is ok.  */
4948   return true;
4949 }
4950
4951 \f
4952 /* Addressing mode support functions.  */
4953
4954 /* Return nonzero if X is a legitimate immediate operand when compiling
4955    for PIC.  We know that X satisfies CONSTANT_P and flag_pic is true.  */
4956 int
4957 legitimate_pic_operand_p (rtx x)
4958 {
4959   if (GET_CODE (x) == SYMBOL_REF
4960       || (GET_CODE (x) == CONST
4961           && GET_CODE (XEXP (x, 0)) == PLUS
4962           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
4963     return 0;
4964
4965   return 1;
4966 }
4967
4968 /* Record that the current function needs a PIC register.  Initialize
4969    cfun->machine->pic_reg if we have not already done so.  */
4970
4971 static void
4972 require_pic_register (void)
4973 {
4974   /* A lot of the logic here is made obscure by the fact that this
4975      routine gets called as part of the rtx cost estimation process.
4976      We don't want those calls to affect any assumptions about the real
4977      function; and further, we can't call entry_of_function() until we
4978      start the real expansion process.  */
4979   if (!crtl->uses_pic_offset_table)
4980     {
4981       gcc_assert (can_create_pseudo_p ());
4982       if (arm_pic_register != INVALID_REGNUM)
4983         {
4984           if (!cfun->machine->pic_reg)
4985             cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
4986
4987           /* Play games to avoid marking the function as needing pic
4988              if we are being called as part of the cost-estimation
4989              process.  */
4990           if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
4991             crtl->uses_pic_offset_table = 1;
4992         }
4993       else
4994         {
4995           rtx seq;
4996
4997           if (!cfun->machine->pic_reg)
4998             cfun->machine->pic_reg = gen_reg_rtx (Pmode);
4999
5000           /* Play games to avoid marking the function as needing pic
5001              if we are being called as part of the cost-estimation
5002              process.  */
5003           if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
5004             {
5005               crtl->uses_pic_offset_table = 1;
5006               start_sequence ();
5007
5008               arm_load_pic_register (0UL);
5009
5010               seq = get_insns ();
5011               end_sequence ();
5012               /* We can be called during expansion of PHI nodes, where
5013                  we can't yet emit instructions directly in the final
5014                  insn stream.  Queue the insns on the entry edge, they will
5015                  be committed after everything else is expanded.  */
5016               insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
5017             }
5018         }
5019     }
5020 }
5021
5022 rtx
5023 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
5024 {
5025   if (GET_CODE (orig) == SYMBOL_REF
5026       || GET_CODE (orig) == LABEL_REF)
5027     {
5028       rtx insn;
5029
5030       if (reg == 0)
5031         {
5032           gcc_assert (can_create_pseudo_p ());
5033           reg = gen_reg_rtx (Pmode);
5034         }
5035
5036       /* VxWorks does not impose a fixed gap between segments; the run-time
5037          gap can be different from the object-file gap.  We therefore can't
5038          use GOTOFF unless we are absolutely sure that the symbol is in the
5039          same segment as the GOT.  Unfortunately, the flexibility of linker
5040          scripts means that we can't be sure of that in general, so assume
5041          that GOTOFF is never valid on VxWorks.  */
5042       if ((GET_CODE (orig) == LABEL_REF
5043            || (GET_CODE (orig) == SYMBOL_REF &&
5044                SYMBOL_REF_LOCAL_P (orig)))
5045           && NEED_GOT_RELOC
5046           && !TARGET_VXWORKS_RTP)
5047         insn = arm_pic_static_addr (orig, reg);
5048       else
5049         {
5050           rtx pat;
5051           rtx mem;
5052
5053           /* If this function doesn't have a pic register, create one now.  */
5054           require_pic_register ();
5055
5056           pat = gen_calculate_pic_address (reg, cfun->machine->pic_reg, orig);
5057
5058           /* Make the MEM as close to a constant as possible.  */
5059           mem = SET_SRC (pat);
5060           gcc_assert (MEM_P (mem) && !MEM_VOLATILE_P (mem));
5061           MEM_READONLY_P (mem) = 1;
5062           MEM_NOTRAP_P (mem) = 1;
5063
5064           insn = emit_insn (pat);
5065         }
5066
5067       /* Put a REG_EQUAL note on this insn, so that it can be optimized
5068          by loop.  */
5069       set_unique_reg_note (insn, REG_EQUAL, orig);
5070
5071       return reg;
5072     }
5073   else if (GET_CODE (orig) == CONST)
5074     {
5075       rtx base, offset;
5076
5077       if (GET_CODE (XEXP (orig, 0)) == PLUS
5078           && XEXP (XEXP (orig, 0), 0) == cfun->machine->pic_reg)
5079         return orig;
5080
5081       /* Handle the case where we have: const (UNSPEC_TLS).  */
5082       if (GET_CODE (XEXP (orig, 0)) == UNSPEC
5083           && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
5084         return orig;
5085
5086       /* Handle the case where we have:
5087          const (plus (UNSPEC_TLS) (ADDEND)).  The ADDEND must be a
5088          CONST_INT.  */
5089       if (GET_CODE (XEXP (orig, 0)) == PLUS
5090           && GET_CODE (XEXP (XEXP (orig, 0), 0)) == UNSPEC
5091           && XINT (XEXP (XEXP (orig, 0), 0), 1) == UNSPEC_TLS)
5092         {
5093           gcc_assert (GET_CODE (XEXP (XEXP (orig, 0), 1)) == CONST_INT);
5094           return orig;
5095         }
5096
5097       if (reg == 0)
5098         {
5099           gcc_assert (can_create_pseudo_p ());
5100           reg = gen_reg_rtx (Pmode);
5101         }
5102
5103       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
5104
5105       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
5106       offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
5107                                        base == reg ? 0 : reg);
5108
5109       if (GET_CODE (offset) == CONST_INT)
5110         {
5111           /* The base register doesn't really matter, we only want to
5112              test the index for the appropriate mode.  */
5113           if (!arm_legitimate_index_p (mode, offset, SET, 0))
5114             {
5115               gcc_assert (can_create_pseudo_p ());
5116               offset = force_reg (Pmode, offset);
5117             }
5118
5119           if (GET_CODE (offset) == CONST_INT)
5120             return plus_constant (base, INTVAL (offset));
5121         }
5122
5123       if (GET_MODE_SIZE (mode) > 4
5124           && (GET_MODE_CLASS (mode) == MODE_INT
5125               || TARGET_SOFT_FLOAT))
5126         {
5127           emit_insn (gen_addsi3 (reg, base, offset));
5128           return reg;
5129         }
5130
5131       return gen_rtx_PLUS (Pmode, base, offset);
5132     }
5133
5134   return orig;
5135 }
5136
5137
5138 /* Find a spare register to use during the prolog of a function.  */
5139
5140 static int
5141 thumb_find_work_register (unsigned long pushed_regs_mask)
5142 {
5143   int reg;
5144
5145   /* Check the argument registers first as these are call-used.  The
5146      register allocation order means that sometimes r3 might be used
5147      but earlier argument registers might not, so check them all.  */
5148   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
5149     if (!df_regs_ever_live_p (reg))
5150       return reg;
5151
5152   /* Before going on to check the call-saved registers we can try a couple
5153      more ways of deducing that r3 is available.  The first is when we are
5154      pushing anonymous arguments onto the stack and we have less than 4
5155      registers worth of fixed arguments(*).  In this case r3 will be part of
5156      the variable argument list and so we can be sure that it will be
5157      pushed right at the start of the function.  Hence it will be available
5158      for the rest of the prologue.
5159      (*): ie crtl->args.pretend_args_size is greater than 0.  */
5160   if (cfun->machine->uses_anonymous_args
5161       && crtl->args.pretend_args_size > 0)
5162     return LAST_ARG_REGNUM;
5163
5164   /* The other case is when we have fixed arguments but less than 4 registers
5165      worth.  In this case r3 might be used in the body of the function, but
5166      it is not being used to convey an argument into the function.  In theory
5167      we could just check crtl->args.size to see how many bytes are
5168      being passed in argument registers, but it seems that it is unreliable.
5169      Sometimes it will have the value 0 when in fact arguments are being
5170      passed.  (See testcase execute/20021111-1.c for an example).  So we also
5171      check the args_info.nregs field as well.  The problem with this field is
5172      that it makes no allowances for arguments that are passed to the
5173      function but which are not used.  Hence we could miss an opportunity
5174      when a function has an unused argument in r3.  But it is better to be
5175      safe than to be sorry.  */
5176   if (! cfun->machine->uses_anonymous_args
5177       && crtl->args.size >= 0
5178       && crtl->args.size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
5179       && crtl->args.info.nregs < 4)
5180     return LAST_ARG_REGNUM;
5181
5182   /* Otherwise look for a call-saved register that is going to be pushed.  */
5183   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
5184     if (pushed_regs_mask & (1 << reg))
5185       return reg;
5186
5187   if (TARGET_THUMB2)
5188     {
5189       /* Thumb-2 can use high regs.  */
5190       for (reg = FIRST_HI_REGNUM; reg < 15; reg ++)
5191         if (pushed_regs_mask & (1 << reg))
5192           return reg;
5193     }
5194   /* Something went wrong - thumb_compute_save_reg_mask()
5195      should have arranged for a suitable register to be pushed.  */
5196   gcc_unreachable ();
5197 }
5198
5199 static GTY(()) int pic_labelno;
5200
5201 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
5202    low register.  */
5203
5204 void
5205 arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
5206 {
5207   rtx l1, labelno, pic_tmp, pic_rtx, pic_reg;
5208
5209   if (crtl->uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
5210     return;
5211
5212   gcc_assert (flag_pic);
5213
5214   pic_reg = cfun->machine->pic_reg;
5215   if (TARGET_VXWORKS_RTP)
5216     {
5217       pic_rtx = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE);
5218       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
5219       emit_insn (gen_pic_load_addr_32bit (pic_reg, pic_rtx));
5220
5221       emit_insn (gen_rtx_SET (Pmode, pic_reg, gen_rtx_MEM (Pmode, pic_reg)));
5222
5223       pic_tmp = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
5224       emit_insn (gen_pic_offset_arm (pic_reg, pic_reg, pic_tmp));
5225     }
5226   else
5227     {
5228       /* We use an UNSPEC rather than a LABEL_REF because this label
5229          never appears in the code stream.  */
5230
5231       labelno = GEN_INT (pic_labelno++);
5232       l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
5233       l1 = gen_rtx_CONST (VOIDmode, l1);
5234
5235       /* On the ARM the PC register contains 'dot + 8' at the time of the
5236          addition, on the Thumb it is 'dot + 4'.  */
5237       pic_rtx = plus_constant (l1, TARGET_ARM ? 8 : 4);
5238       pic_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, pic_rtx),
5239                                 UNSPEC_GOTSYM_OFF);
5240       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
5241
5242       if (TARGET_32BIT)
5243         {
5244           emit_insn (gen_pic_load_addr_32bit (pic_reg, pic_rtx));
5245           if (TARGET_ARM)
5246             emit_insn (gen_pic_add_dot_plus_eight (pic_reg, pic_reg, labelno));
5247           else
5248             emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
5249         }
5250       else /* TARGET_THUMB1 */
5251         {
5252           if (arm_pic_register != INVALID_REGNUM
5253               && REGNO (pic_reg) > LAST_LO_REGNUM)
5254             {
5255               /* We will have pushed the pic register, so we should always be
5256                  able to find a work register.  */
5257               pic_tmp = gen_rtx_REG (SImode,
5258                                      thumb_find_work_register (saved_regs));
5259               emit_insn (gen_pic_load_addr_thumb1 (pic_tmp, pic_rtx));
5260               emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
5261             }
5262           else
5263             emit_insn (gen_pic_load_addr_thumb1 (pic_reg, pic_rtx));
5264           emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
5265         }
5266     }
5267
5268   /* Need to emit this whether or not we obey regdecls,
5269      since setjmp/longjmp can cause life info to screw up.  */
5270   emit_use (pic_reg);
5271 }
5272
5273 /* Generate code to load the address of a static var when flag_pic is set.  */
5274 static rtx
5275 arm_pic_static_addr (rtx orig, rtx reg)
5276 {
5277   rtx l1, labelno, offset_rtx, insn;
5278
5279   gcc_assert (flag_pic);
5280
5281   /* We use an UNSPEC rather than a LABEL_REF because this label
5282      never appears in the code stream.  */
5283   labelno = GEN_INT (pic_labelno++);
5284   l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
5285   l1 = gen_rtx_CONST (VOIDmode, l1);
5286
5287   /* On the ARM the PC register contains 'dot + 8' at the time of the
5288      addition, on the Thumb it is 'dot + 4'.  */
5289   offset_rtx = plus_constant (l1, TARGET_ARM ? 8 : 4);
5290   offset_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, orig, offset_rtx),
5291                                UNSPEC_SYMBOL_OFFSET);
5292   offset_rtx = gen_rtx_CONST (Pmode, offset_rtx);
5293
5294   if (TARGET_32BIT)
5295     {
5296       emit_insn (gen_pic_load_addr_32bit (reg, offset_rtx));
5297       if (TARGET_ARM)
5298         insn = emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
5299       else
5300         insn = emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5301     }
5302   else /* TARGET_THUMB1 */
5303     {
5304       emit_insn (gen_pic_load_addr_thumb1 (reg, offset_rtx));
5305       insn = emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5306     }
5307
5308   return insn;
5309 }
5310
5311 /* Return nonzero if X is valid as an ARM state addressing register.  */
5312 static int
5313 arm_address_register_rtx_p (rtx x, int strict_p)
5314 {
5315   int regno;
5316
5317   if (GET_CODE (x) != REG)
5318     return 0;
5319
5320   regno = REGNO (x);
5321
5322   if (strict_p)
5323     return ARM_REGNO_OK_FOR_BASE_P (regno);
5324
5325   return (regno <= LAST_ARM_REGNUM
5326           || regno >= FIRST_PSEUDO_REGISTER
5327           || regno == FRAME_POINTER_REGNUM
5328           || regno == ARG_POINTER_REGNUM);
5329 }
5330
5331 /* Return TRUE if this rtx is the difference of a symbol and a label,
5332    and will reduce to a PC-relative relocation in the object file.
5333    Expressions like this can be left alone when generating PIC, rather
5334    than forced through the GOT.  */
5335 static int
5336 pcrel_constant_p (rtx x)
5337 {
5338   if (GET_CODE (x) == MINUS)
5339     return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
5340
5341   return FALSE;
5342 }
5343
5344 /* Return true if X will surely end up in an index register after next
5345    splitting pass.  */
5346 static bool
5347 will_be_in_index_register (const_rtx x)
5348 {
5349   /* arm.md: calculate_pic_address will split this into a register.  */
5350   return GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_SYM;
5351 }
5352
5353 /* Return nonzero if X is a valid ARM state address operand.  */
5354 int
5355 arm_legitimate_address_outer_p (enum machine_mode mode, rtx x, RTX_CODE outer,
5356                                 int strict_p)
5357 {
5358   bool use_ldrd;
5359   enum rtx_code code = GET_CODE (x);
5360
5361   if (arm_address_register_rtx_p (x, strict_p))
5362     return 1;
5363
5364   use_ldrd = (TARGET_LDRD
5365               && (mode == DImode
5366                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
5367
5368   if (code == POST_INC || code == PRE_DEC
5369       || ((code == PRE_INC || code == POST_DEC)
5370           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
5371     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
5372
5373   else if ((code == POST_MODIFY || code == PRE_MODIFY)
5374            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
5375            && GET_CODE (XEXP (x, 1)) == PLUS
5376            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5377     {
5378       rtx addend = XEXP (XEXP (x, 1), 1);
5379
5380       /* Don't allow ldrd post increment by register because it's hard
5381          to fixup invalid register choices.  */
5382       if (use_ldrd
5383           && GET_CODE (x) == POST_MODIFY
5384           && GET_CODE (addend) == REG)
5385         return 0;
5386
5387       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
5388               && arm_legitimate_index_p (mode, addend, outer, strict_p));
5389     }
5390
5391   /* After reload constants split into minipools will have addresses
5392      from a LABEL_REF.  */
5393   else if (reload_completed
5394            && (code == LABEL_REF
5395                || (code == CONST
5396                    && GET_CODE (XEXP (x, 0)) == PLUS
5397                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5398                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5399     return 1;
5400
5401   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
5402     return 0;
5403
5404   else if (code == PLUS)
5405     {
5406       rtx xop0 = XEXP (x, 0);
5407       rtx xop1 = XEXP (x, 1);
5408
5409       return ((arm_address_register_rtx_p (xop0, strict_p)
5410                && ((GET_CODE(xop1) == CONST_INT
5411                     && arm_legitimate_index_p (mode, xop1, outer, strict_p))
5412                    || (!strict_p && will_be_in_index_register (xop1))))
5413               || (arm_address_register_rtx_p (xop1, strict_p)
5414                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
5415     }
5416
5417 #if 0
5418   /* Reload currently can't handle MINUS, so disable this for now */
5419   else if (GET_CODE (x) == MINUS)
5420     {
5421       rtx xop0 = XEXP (x, 0);
5422       rtx xop1 = XEXP (x, 1);
5423
5424       return (arm_address_register_rtx_p (xop0, strict_p)
5425               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
5426     }
5427 #endif
5428
5429   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
5430            && code == SYMBOL_REF
5431            && CONSTANT_POOL_ADDRESS_P (x)
5432            && ! (flag_pic
5433                  && symbol_mentioned_p (get_pool_constant (x))
5434                  && ! pcrel_constant_p (get_pool_constant (x))))
5435     return 1;
5436
5437   return 0;
5438 }
5439
5440 /* Return nonzero if X is a valid Thumb-2 address operand.  */
5441 static int
5442 thumb2_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
5443 {
5444   bool use_ldrd;
5445   enum rtx_code code = GET_CODE (x);
5446   
5447   if (arm_address_register_rtx_p (x, strict_p))
5448     return 1;
5449
5450   use_ldrd = (TARGET_LDRD
5451               && (mode == DImode
5452                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
5453
5454   if (code == POST_INC || code == PRE_DEC
5455       || ((code == PRE_INC || code == POST_DEC)
5456           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
5457     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
5458
5459   else if ((code == POST_MODIFY || code == PRE_MODIFY)
5460            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
5461            && GET_CODE (XEXP (x, 1)) == PLUS
5462            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5463     {
5464       /* Thumb-2 only has autoincrement by constant.  */
5465       rtx addend = XEXP (XEXP (x, 1), 1);
5466       HOST_WIDE_INT offset;
5467
5468       if (GET_CODE (addend) != CONST_INT)
5469         return 0;
5470
5471       offset = INTVAL(addend);
5472       if (GET_MODE_SIZE (mode) <= 4)
5473         return (offset > -256 && offset < 256);
5474       
5475       return (use_ldrd && offset > -1024 && offset < 1024
5476               && (offset & 3) == 0);
5477     }
5478
5479   /* After reload constants split into minipools will have addresses
5480      from a LABEL_REF.  */
5481   else if (reload_completed
5482            && (code == LABEL_REF
5483                || (code == CONST
5484                    && GET_CODE (XEXP (x, 0)) == PLUS
5485                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5486                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5487     return 1;
5488
5489   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
5490     return 0;
5491
5492   else if (code == PLUS)
5493     {
5494       rtx xop0 = XEXP (x, 0);
5495       rtx xop1 = XEXP (x, 1);
5496
5497       return ((arm_address_register_rtx_p (xop0, strict_p)
5498                && (thumb2_legitimate_index_p (mode, xop1, strict_p)
5499                    || (!strict_p && will_be_in_index_register (xop1))))
5500               || (arm_address_register_rtx_p (xop1, strict_p)
5501                   && thumb2_legitimate_index_p (mode, xop0, strict_p)));
5502     }
5503
5504   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
5505            && code == SYMBOL_REF
5506            && CONSTANT_POOL_ADDRESS_P (x)
5507            && ! (flag_pic
5508                  && symbol_mentioned_p (get_pool_constant (x))
5509                  && ! pcrel_constant_p (get_pool_constant (x))))
5510     return 1;
5511
5512   return 0;
5513 }
5514
5515 /* Return nonzero if INDEX is valid for an address index operand in
5516    ARM state.  */
5517 static int
5518 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
5519                         int strict_p)
5520 {
5521   HOST_WIDE_INT range;
5522   enum rtx_code code = GET_CODE (index);
5523
5524   /* Standard coprocessor addressing modes.  */
5525   if (TARGET_HARD_FLOAT
5526       && (TARGET_FPA || TARGET_MAVERICK)
5527       && (GET_MODE_CLASS (mode) == MODE_FLOAT
5528           || (TARGET_MAVERICK && mode == DImode)))
5529     return (code == CONST_INT && INTVAL (index) < 1024
5530             && INTVAL (index) > -1024
5531             && (INTVAL (index) & 3) == 0);
5532
5533   if (TARGET_NEON
5534       && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode)))
5535     return (code == CONST_INT
5536             && INTVAL (index) < 1016
5537             && INTVAL (index) > -1024
5538             && (INTVAL (index) & 3) == 0);
5539
5540   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
5541     return (code == CONST_INT
5542             && INTVAL (index) < 1024
5543             && INTVAL (index) > -1024
5544             && (INTVAL (index) & 3) == 0);
5545
5546   if (arm_address_register_rtx_p (index, strict_p)
5547       && (GET_MODE_SIZE (mode) <= 4))
5548     return 1;
5549
5550   if (mode == DImode || mode == DFmode)
5551     {
5552       if (code == CONST_INT)
5553         {
5554           HOST_WIDE_INT val = INTVAL (index);
5555
5556           if (TARGET_LDRD)
5557             return val > -256 && val < 256;
5558           else
5559             return val > -4096 && val < 4092;
5560         }
5561
5562       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
5563     }
5564
5565   if (GET_MODE_SIZE (mode) <= 4
5566       && ! (arm_arch4
5567             && (mode == HImode
5568                 || mode == HFmode
5569                 || (mode == QImode && outer == SIGN_EXTEND))))
5570     {
5571       if (code == MULT)
5572         {
5573           rtx xiop0 = XEXP (index, 0);
5574           rtx xiop1 = XEXP (index, 1);
5575
5576           return ((arm_address_register_rtx_p (xiop0, strict_p)
5577                    && power_of_two_operand (xiop1, SImode))
5578                   || (arm_address_register_rtx_p (xiop1, strict_p)
5579                       && power_of_two_operand (xiop0, SImode)));
5580         }
5581       else if (code == LSHIFTRT || code == ASHIFTRT
5582                || code == ASHIFT || code == ROTATERT)
5583         {
5584           rtx op = XEXP (index, 1);
5585
5586           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
5587                   && GET_CODE (op) == CONST_INT
5588                   && INTVAL (op) > 0
5589                   && INTVAL (op) <= 31);
5590         }
5591     }
5592
5593   /* For ARM v4 we may be doing a sign-extend operation during the
5594      load.  */
5595   if (arm_arch4)
5596     {
5597       if (mode == HImode
5598           || mode == HFmode
5599           || (outer == SIGN_EXTEND && mode == QImode))
5600         range = 256;
5601       else
5602         range = 4096;
5603     }
5604   else
5605     range = (mode == HImode || mode == HFmode) ? 4095 : 4096;
5606
5607   return (code == CONST_INT
5608           && INTVAL (index) < range
5609           && INTVAL (index) > -range);
5610 }
5611
5612 /* Return true if OP is a valid index scaling factor for Thumb-2 address
5613    index operand.  i.e. 1, 2, 4 or 8.  */
5614 static bool
5615 thumb2_index_mul_operand (rtx op)
5616 {
5617   HOST_WIDE_INT val;
5618   
5619   if (GET_CODE(op) != CONST_INT)
5620     return false;
5621
5622   val = INTVAL(op);
5623   return (val == 1 || val == 2 || val == 4 || val == 8);
5624 }
5625   
5626 /* Return nonzero if INDEX is a valid Thumb-2 address index operand.  */
5627 static int
5628 thumb2_legitimate_index_p (enum machine_mode mode, rtx index, int strict_p)
5629 {
5630   enum rtx_code code = GET_CODE (index);
5631
5632   /* ??? Combine arm and thumb2 coprocessor addressing modes.  */
5633   /* Standard coprocessor addressing modes.  */
5634   if (TARGET_HARD_FLOAT
5635       && (TARGET_FPA || TARGET_MAVERICK)
5636       && (GET_MODE_CLASS (mode) == MODE_FLOAT
5637           || (TARGET_MAVERICK && mode == DImode)))
5638     return (code == CONST_INT && INTVAL (index) < 1024
5639             && INTVAL (index) > -1024
5640             && (INTVAL (index) & 3) == 0);
5641
5642   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
5643     {
5644       /* For DImode assume values will usually live in core regs
5645          and only allow LDRD addressing modes.  */
5646       if (!TARGET_LDRD || mode != DImode)
5647         return (code == CONST_INT
5648                 && INTVAL (index) < 1024
5649                 && INTVAL (index) > -1024
5650                 && (INTVAL (index) & 3) == 0);
5651     }
5652
5653   if (TARGET_NEON
5654       && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode)))
5655     return (code == CONST_INT
5656             && INTVAL (index) < 1016
5657             && INTVAL (index) > -1024
5658             && (INTVAL (index) & 3) == 0);
5659
5660   if (arm_address_register_rtx_p (index, strict_p)
5661       && (GET_MODE_SIZE (mode) <= 4))
5662     return 1;
5663
5664   if (mode == DImode || mode == DFmode)
5665     {
5666       if (code == CONST_INT)
5667         {
5668           HOST_WIDE_INT val = INTVAL (index);
5669           /* ??? Can we assume ldrd for thumb2?  */
5670           /* Thumb-2 ldrd only has reg+const addressing modes.  */
5671           /* ldrd supports offsets of +-1020.
5672              However the ldr fallback does not.  */
5673           return val > -256 && val < 256 && (val & 3) == 0;
5674         }
5675       else
5676         return 0;
5677     }
5678
5679   if (code == MULT)
5680     {
5681       rtx xiop0 = XEXP (index, 0);
5682       rtx xiop1 = XEXP (index, 1);
5683
5684       return ((arm_address_register_rtx_p (xiop0, strict_p)
5685                && thumb2_index_mul_operand (xiop1))
5686               || (arm_address_register_rtx_p (xiop1, strict_p)
5687                   && thumb2_index_mul_operand (xiop0)));
5688     }
5689   else if (code == ASHIFT)
5690     {
5691       rtx op = XEXP (index, 1);
5692
5693       return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
5694               && GET_CODE (op) == CONST_INT
5695               && INTVAL (op) > 0
5696               && INTVAL (op) <= 3);
5697     }
5698
5699   return (code == CONST_INT
5700           && INTVAL (index) < 4096
5701           && INTVAL (index) > -256);
5702 }
5703
5704 /* Return nonzero if X is valid as a 16-bit Thumb state base register.  */
5705 static int
5706 thumb1_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
5707 {
5708   int regno;
5709
5710   if (GET_CODE (x) != REG)
5711     return 0;
5712
5713   regno = REGNO (x);
5714
5715   if (strict_p)
5716     return THUMB1_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
5717
5718   return (regno <= LAST_LO_REGNUM
5719           || regno > LAST_VIRTUAL_REGISTER
5720           || regno == FRAME_POINTER_REGNUM
5721           || (GET_MODE_SIZE (mode) >= 4
5722               && (regno == STACK_POINTER_REGNUM
5723                   || regno >= FIRST_PSEUDO_REGISTER
5724                   || x == hard_frame_pointer_rtx
5725                   || x == arg_pointer_rtx)));
5726 }
5727
5728 /* Return nonzero if x is a legitimate index register.  This is the case
5729    for any base register that can access a QImode object.  */
5730 inline static int
5731 thumb1_index_register_rtx_p (rtx x, int strict_p)
5732 {
5733   return thumb1_base_register_rtx_p (x, QImode, strict_p);
5734 }
5735
5736 /* Return nonzero if x is a legitimate 16-bit Thumb-state address.
5737
5738    The AP may be eliminated to either the SP or the FP, so we use the
5739    least common denominator, e.g. SImode, and offsets from 0 to 64.
5740
5741    ??? Verify whether the above is the right approach.
5742
5743    ??? Also, the FP may be eliminated to the SP, so perhaps that
5744    needs special handling also.
5745
5746    ??? Look at how the mips16 port solves this problem.  It probably uses
5747    better ways to solve some of these problems.
5748
5749    Although it is not incorrect, we don't accept QImode and HImode
5750    addresses based on the frame pointer or arg pointer until the
5751    reload pass starts.  This is so that eliminating such addresses
5752    into stack based ones won't produce impossible code.  */
5753 static int
5754 thumb1_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
5755 {
5756   /* ??? Not clear if this is right.  Experiment.  */
5757   if (GET_MODE_SIZE (mode) < 4
5758       && !(reload_in_progress || reload_completed)
5759       && (reg_mentioned_p (frame_pointer_rtx, x)
5760           || reg_mentioned_p (arg_pointer_rtx, x)
5761           || reg_mentioned_p (virtual_incoming_args_rtx, x)
5762           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
5763           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
5764           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
5765     return 0;
5766
5767   /* Accept any base register.  SP only in SImode or larger.  */
5768   else if (thumb1_base_register_rtx_p (x, mode, strict_p))
5769     return 1;
5770
5771   /* This is PC relative data before arm_reorg runs.  */
5772   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
5773            && GET_CODE (x) == SYMBOL_REF
5774            && CONSTANT_POOL_ADDRESS_P (x) && !flag_pic)
5775     return 1;
5776
5777   /* This is PC relative data after arm_reorg runs.  */
5778   else if ((GET_MODE_SIZE (mode) >= 4 || mode == HFmode)
5779            && reload_completed
5780            && (GET_CODE (x) == LABEL_REF
5781                || (GET_CODE (x) == CONST
5782                    && GET_CODE (XEXP (x, 0)) == PLUS
5783                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5784                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5785     return 1;
5786
5787   /* Post-inc indexing only supported for SImode and larger.  */
5788   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
5789            && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p))
5790     return 1;
5791
5792   else if (GET_CODE (x) == PLUS)
5793     {
5794       /* REG+REG address can be any two index registers.  */
5795       /* We disallow FRAME+REG addressing since we know that FRAME
5796          will be replaced with STACK, and SP relative addressing only
5797          permits SP+OFFSET.  */
5798       if (GET_MODE_SIZE (mode) <= 4
5799           && XEXP (x, 0) != frame_pointer_rtx
5800           && XEXP (x, 1) != frame_pointer_rtx
5801           && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
5802           && (thumb1_index_register_rtx_p (XEXP (x, 1), strict_p)
5803               || (!strict_p && will_be_in_index_register (XEXP (x, 1)))))
5804         return 1;
5805
5806       /* REG+const has 5-7 bit offset for non-SP registers.  */
5807       else if ((thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
5808                 || XEXP (x, 0) == arg_pointer_rtx)
5809                && GET_CODE (XEXP (x, 1)) == CONST_INT
5810                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
5811         return 1;
5812
5813       /* REG+const has 10-bit offset for SP, but only SImode and
5814          larger is supported.  */
5815       /* ??? Should probably check for DI/DFmode overflow here
5816          just like GO_IF_LEGITIMATE_OFFSET does.  */
5817       else if (GET_CODE (XEXP (x, 0)) == REG
5818                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
5819                && GET_MODE_SIZE (mode) >= 4
5820                && GET_CODE (XEXP (x, 1)) == CONST_INT
5821                && INTVAL (XEXP (x, 1)) >= 0
5822                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
5823                && (INTVAL (XEXP (x, 1)) & 3) == 0)
5824         return 1;
5825
5826       else if (GET_CODE (XEXP (x, 0)) == REG
5827                && (REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
5828                    || REGNO (XEXP (x, 0)) == ARG_POINTER_REGNUM
5829                    || (REGNO (XEXP (x, 0)) >= FIRST_VIRTUAL_REGISTER
5830                        && REGNO (XEXP (x, 0)) <= LAST_VIRTUAL_REGISTER))
5831                && GET_MODE_SIZE (mode) >= 4
5832                && GET_CODE (XEXP (x, 1)) == CONST_INT
5833                && (INTVAL (XEXP (x, 1)) & 3) == 0)
5834         return 1;
5835     }
5836
5837   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
5838            && GET_MODE_SIZE (mode) == 4
5839            && GET_CODE (x) == SYMBOL_REF
5840            && CONSTANT_POOL_ADDRESS_P (x)
5841            && ! (flag_pic
5842                  && symbol_mentioned_p (get_pool_constant (x))
5843                  && ! pcrel_constant_p (get_pool_constant (x))))
5844     return 1;
5845
5846   return 0;
5847 }
5848
5849 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
5850    instruction of mode MODE.  */
5851 int
5852 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
5853 {
5854   switch (GET_MODE_SIZE (mode))
5855     {
5856     case 1:
5857       return val >= 0 && val < 32;
5858
5859     case 2:
5860       return val >= 0 && val < 64 && (val & 1) == 0;
5861
5862     default:
5863       return (val >= 0
5864               && (val + GET_MODE_SIZE (mode)) <= 128
5865               && (val & 3) == 0);
5866     }
5867 }
5868
5869 bool
5870 arm_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
5871 {
5872   if (TARGET_ARM)
5873     return arm_legitimate_address_outer_p (mode, x, SET, strict_p);
5874   else if (TARGET_THUMB2)
5875     return thumb2_legitimate_address_p (mode, x, strict_p);
5876   else /* if (TARGET_THUMB1) */
5877     return thumb1_legitimate_address_p (mode, x, strict_p);
5878 }
5879
5880 /* Build the SYMBOL_REF for __tls_get_addr.  */
5881
5882 static GTY(()) rtx tls_get_addr_libfunc;
5883
5884 static rtx
5885 get_tls_get_addr (void)
5886 {
5887   if (!tls_get_addr_libfunc)
5888     tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
5889   return tls_get_addr_libfunc;
5890 }
5891
5892 static rtx
5893 arm_load_tp (rtx target)
5894 {
5895   if (!target)
5896     target = gen_reg_rtx (SImode);
5897
5898   if (TARGET_HARD_TP)
5899     {
5900       /* Can return in any reg.  */
5901       emit_insn (gen_load_tp_hard (target));
5902     }
5903   else
5904     {
5905       /* Always returned in r0.  Immediately copy the result into a pseudo,
5906          otherwise other uses of r0 (e.g. setting up function arguments) may
5907          clobber the value.  */
5908
5909       rtx tmp;
5910
5911       emit_insn (gen_load_tp_soft ());
5912
5913       tmp = gen_rtx_REG (SImode, 0);
5914       emit_move_insn (target, tmp);
5915     }
5916   return target;
5917 }
5918
5919 static rtx
5920 load_tls_operand (rtx x, rtx reg)
5921 {
5922   rtx tmp;
5923
5924   if (reg == NULL_RTX)
5925     reg = gen_reg_rtx (SImode);
5926
5927   tmp = gen_rtx_CONST (SImode, x);
5928
5929   emit_move_insn (reg, tmp);
5930
5931   return reg;
5932 }
5933
5934 static rtx
5935 arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
5936 {
5937   rtx insns, label, labelno, sum;
5938
5939   start_sequence ();
5940
5941   labelno = GEN_INT (pic_labelno++);
5942   label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
5943   label = gen_rtx_CONST (VOIDmode, label);
5944
5945   sum = gen_rtx_UNSPEC (Pmode,
5946                         gen_rtvec (4, x, GEN_INT (reloc), label,
5947                                    GEN_INT (TARGET_ARM ? 8 : 4)),
5948                         UNSPEC_TLS);
5949   reg = load_tls_operand (sum, reg);
5950
5951   if (TARGET_ARM)
5952     emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
5953   else if (TARGET_THUMB2)
5954     emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5955   else /* TARGET_THUMB1 */
5956     emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5957
5958   *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX, LCT_PURE, /* LCT_CONST?  */
5959                                      Pmode, 1, reg, Pmode);
5960
5961   insns = get_insns ();
5962   end_sequence ();
5963
5964   return insns;
5965 }
5966
5967 rtx
5968 legitimize_tls_address (rtx x, rtx reg)
5969 {
5970   rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
5971   unsigned int model = SYMBOL_REF_TLS_MODEL (x);
5972
5973   switch (model)
5974     {
5975     case TLS_MODEL_GLOBAL_DYNAMIC:
5976       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
5977       dest = gen_reg_rtx (Pmode);
5978       emit_libcall_block (insns, dest, ret, x);
5979       return dest;
5980
5981     case TLS_MODEL_LOCAL_DYNAMIC:
5982       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
5983
5984       /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
5985          share the LDM result with other LD model accesses.  */
5986       eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
5987                             UNSPEC_TLS);
5988       dest = gen_reg_rtx (Pmode);
5989       emit_libcall_block (insns, dest, ret, eqv);
5990
5991       /* Load the addend.  */
5992       addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (TLS_LDO32)),
5993                                UNSPEC_TLS);
5994       addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
5995       return gen_rtx_PLUS (Pmode, dest, addend);
5996
5997     case TLS_MODEL_INITIAL_EXEC:
5998       labelno = GEN_INT (pic_labelno++);
5999       label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
6000       label = gen_rtx_CONST (VOIDmode, label);
6001       sum = gen_rtx_UNSPEC (Pmode,
6002                             gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
6003                                        GEN_INT (TARGET_ARM ? 8 : 4)),
6004                             UNSPEC_TLS);
6005       reg = load_tls_operand (sum, reg);
6006
6007       if (TARGET_ARM)
6008         emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
6009       else if (TARGET_THUMB2)
6010         emit_insn (gen_tls_load_dot_plus_four (reg, NULL, reg, labelno));
6011       else
6012         {
6013           emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
6014           emit_move_insn (reg, gen_const_mem (SImode, reg));
6015         }
6016
6017       tp = arm_load_tp (NULL_RTX);
6018
6019       return gen_rtx_PLUS (Pmode, tp, reg);
6020
6021     case TLS_MODEL_LOCAL_EXEC:
6022       tp = arm_load_tp (NULL_RTX);
6023
6024       reg = gen_rtx_UNSPEC (Pmode,
6025                             gen_rtvec (2, x, GEN_INT (TLS_LE32)),
6026                             UNSPEC_TLS);
6027       reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
6028
6029       return gen_rtx_PLUS (Pmode, tp, reg);
6030
6031     default:
6032       abort ();
6033     }
6034 }
6035
6036 /* Try machine-dependent ways of modifying an illegitimate address
6037    to be legitimate.  If we find one, return the new, valid address.  */
6038 rtx
6039 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
6040 {
6041   if (!TARGET_ARM)
6042     {
6043       /* TODO: legitimize_address for Thumb2.  */
6044       if (TARGET_THUMB2)
6045         return x;
6046       return thumb_legitimize_address (x, orig_x, mode);
6047     }
6048
6049   if (arm_tls_symbol_p (x))
6050     return legitimize_tls_address (x, NULL_RTX);
6051
6052   if (GET_CODE (x) == PLUS)
6053     {
6054       rtx xop0 = XEXP (x, 0);
6055       rtx xop1 = XEXP (x, 1);
6056
6057       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
6058         xop0 = force_reg (SImode, xop0);
6059
6060       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
6061         xop1 = force_reg (SImode, xop1);
6062
6063       if (ARM_BASE_REGISTER_RTX_P (xop0)
6064           && GET_CODE (xop1) == CONST_INT)
6065         {
6066           HOST_WIDE_INT n, low_n;
6067           rtx base_reg, val;
6068           n = INTVAL (xop1);
6069
6070           /* VFP addressing modes actually allow greater offsets, but for
6071              now we just stick with the lowest common denominator.  */
6072           if (mode == DImode
6073               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
6074             {
6075               low_n = n & 0x0f;
6076               n &= ~0x0f;
6077               if (low_n > 4)
6078                 {
6079                   n += 16;
6080                   low_n -= 16;
6081                 }
6082             }
6083           else
6084             {
6085               low_n = ((mode) == TImode ? 0
6086                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
6087               n -= low_n;
6088             }
6089
6090           base_reg = gen_reg_rtx (SImode);
6091           val = force_operand (plus_constant (xop0, n), NULL_RTX);
6092           emit_move_insn (base_reg, val);
6093           x = plus_constant (base_reg, low_n);
6094         }
6095       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
6096         x = gen_rtx_PLUS (SImode, xop0, xop1);
6097     }
6098
6099   /* XXX We don't allow MINUS any more -- see comment in
6100      arm_legitimate_address_outer_p ().  */
6101   else if (GET_CODE (x) == MINUS)
6102     {
6103       rtx xop0 = XEXP (x, 0);
6104       rtx xop1 = XEXP (x, 1);
6105
6106       if (CONSTANT_P (xop0))
6107         xop0 = force_reg (SImode, xop0);
6108
6109       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
6110         xop1 = force_reg (SImode, xop1);
6111
6112       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
6113         x = gen_rtx_MINUS (SImode, xop0, xop1);
6114     }
6115
6116   /* Make sure to take full advantage of the pre-indexed addressing mode
6117      with absolute addresses which often allows for the base register to
6118      be factorized for multiple adjacent memory references, and it might
6119      even allows for the mini pool to be avoided entirely. */
6120   else if (GET_CODE (x) == CONST_INT && optimize > 0)
6121     {
6122       unsigned int bits;
6123       HOST_WIDE_INT mask, base, index;
6124       rtx base_reg;
6125
6126       /* ldr and ldrb can use a 12-bit index, ldrsb and the rest can only
6127          use a 8-bit index. So let's use a 12-bit index for SImode only and
6128          hope that arm_gen_constant will enable ldrb to use more bits. */
6129       bits = (mode == SImode) ? 12 : 8;
6130       mask = (1 << bits) - 1;
6131       base = INTVAL (x) & ~mask;
6132       index = INTVAL (x) & mask;
6133       if (bit_count (base & 0xffffffff) > (32 - bits)/2)
6134         {
6135           /* It'll most probably be more efficient to generate the base
6136              with more bits set and use a negative index instead. */
6137           base |= mask;
6138           index -= mask;
6139         }
6140       base_reg = force_reg (SImode, GEN_INT (base));
6141       x = plus_constant (base_reg, index);
6142     }
6143
6144   if (flag_pic)
6145     {
6146       /* We need to find and carefully transform any SYMBOL and LABEL
6147          references; so go back to the original address expression.  */
6148       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
6149
6150       if (new_x != orig_x)
6151         x = new_x;
6152     }
6153
6154   return x;
6155 }
6156
6157
6158 /* Try machine-dependent ways of modifying an illegitimate Thumb address
6159    to be legitimate.  If we find one, return the new, valid address.  */
6160 rtx
6161 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
6162 {
6163   if (arm_tls_symbol_p (x))
6164     return legitimize_tls_address (x, NULL_RTX);
6165
6166   if (GET_CODE (x) == PLUS
6167       && GET_CODE (XEXP (x, 1)) == CONST_INT
6168       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
6169           || INTVAL (XEXP (x, 1)) < 0))
6170     {
6171       rtx xop0 = XEXP (x, 0);
6172       rtx xop1 = XEXP (x, 1);
6173       HOST_WIDE_INT offset = INTVAL (xop1);
6174
6175       /* Try and fold the offset into a biasing of the base register and
6176          then offsetting that.  Don't do this when optimizing for space
6177          since it can cause too many CSEs.  */
6178       if (optimize_size && offset >= 0
6179           && offset < 256 + 31 * GET_MODE_SIZE (mode))
6180         {
6181           HOST_WIDE_INT delta;
6182
6183           if (offset >= 256)
6184             delta = offset - (256 - GET_MODE_SIZE (mode));
6185           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
6186             delta = 31 * GET_MODE_SIZE (mode);
6187           else
6188             delta = offset & (~31 * GET_MODE_SIZE (mode));
6189
6190           xop0 = force_operand (plus_constant (xop0, offset - delta),
6191                                 NULL_RTX);
6192           x = plus_constant (xop0, delta);
6193         }
6194       else if (offset < 0 && offset > -256)
6195         /* Small negative offsets are best done with a subtract before the
6196            dereference, forcing these into a register normally takes two
6197            instructions.  */
6198         x = force_operand (x, NULL_RTX);
6199       else
6200         {
6201           /* For the remaining cases, force the constant into a register.  */
6202           xop1 = force_reg (SImode, xop1);
6203           x = gen_rtx_PLUS (SImode, xop0, xop1);
6204         }
6205     }
6206   else if (GET_CODE (x) == PLUS
6207            && s_register_operand (XEXP (x, 1), SImode)
6208            && !s_register_operand (XEXP (x, 0), SImode))
6209     {
6210       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
6211
6212       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
6213     }
6214
6215   if (flag_pic)
6216     {
6217       /* We need to find and carefully transform any SYMBOL and LABEL
6218          references; so go back to the original address expression.  */
6219       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
6220
6221       if (new_x != orig_x)
6222         x = new_x;
6223     }
6224
6225   return x;
6226 }
6227
6228 rtx
6229 thumb_legitimize_reload_address (rtx *x_p,
6230                                  enum machine_mode mode,
6231                                  int opnum, int type,
6232                                  int ind_levels ATTRIBUTE_UNUSED)
6233 {
6234   rtx x = *x_p;
6235
6236   if (GET_CODE (x) == PLUS
6237       && GET_MODE_SIZE (mode) < 4
6238       && REG_P (XEXP (x, 0))
6239       && XEXP (x, 0) == stack_pointer_rtx
6240       && GET_CODE (XEXP (x, 1)) == CONST_INT
6241       && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
6242     {
6243       rtx orig_x = x;
6244
6245       x = copy_rtx (x);
6246       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
6247                    Pmode, VOIDmode, 0, 0, opnum, (enum reload_type) type);
6248       return x;
6249     }
6250
6251   /* If both registers are hi-regs, then it's better to reload the
6252      entire expression rather than each register individually.  That
6253      only requires one reload register rather than two.  */
6254   if (GET_CODE (x) == PLUS
6255       && REG_P (XEXP (x, 0))
6256       && REG_P (XEXP (x, 1))
6257       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
6258       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
6259     {
6260       rtx orig_x = x;
6261
6262       x = copy_rtx (x);
6263       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
6264                    Pmode, VOIDmode, 0, 0, opnum, (enum reload_type) type);
6265       return x;
6266     }
6267
6268   return NULL;
6269 }
6270
6271 /* Test for various thread-local symbols.  */
6272
6273 /* Return TRUE if X is a thread-local symbol.  */
6274
6275 static bool
6276 arm_tls_symbol_p (rtx x)
6277 {
6278   if (! TARGET_HAVE_TLS)
6279     return false;
6280
6281   if (GET_CODE (x) != SYMBOL_REF)
6282     return false;
6283
6284   return SYMBOL_REF_TLS_MODEL (x) != 0;
6285 }
6286
6287 /* Helper for arm_tls_referenced_p.  */
6288
6289 static int
6290 arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
6291 {
6292   if (GET_CODE (*x) == SYMBOL_REF)
6293     return SYMBOL_REF_TLS_MODEL (*x) != 0;
6294
6295   /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
6296      TLS offsets, not real symbol references.  */
6297   if (GET_CODE (*x) == UNSPEC
6298       && XINT (*x, 1) == UNSPEC_TLS)
6299     return -1;
6300
6301   return 0;
6302 }
6303
6304 /* Return TRUE if X contains any TLS symbol references.  */
6305
6306 bool
6307 arm_tls_referenced_p (rtx x)
6308 {
6309   if (! TARGET_HAVE_TLS)
6310     return false;
6311
6312   return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
6313 }
6314
6315 /* Implement TARGET_CANNOT_FORCE_CONST_MEM.  */
6316
6317 bool
6318 arm_cannot_force_const_mem (rtx x)
6319 {
6320   rtx base, offset;
6321
6322   if (ARM_OFFSETS_MUST_BE_WITHIN_SECTIONS_P)
6323     {
6324       split_const (x, &base, &offset);
6325       if (GET_CODE (base) == SYMBOL_REF
6326           && !offset_within_block_p (base, INTVAL (offset)))
6327         return true;
6328     }
6329   return arm_tls_referenced_p (x);
6330 }
6331 \f
6332 #define REG_OR_SUBREG_REG(X)                                            \
6333   (GET_CODE (X) == REG                                                  \
6334    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
6335
6336 #define REG_OR_SUBREG_RTX(X)                    \
6337    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
6338
6339 static inline int
6340 thumb1_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
6341 {
6342   enum machine_mode mode = GET_MODE (x);
6343   int total;
6344
6345   switch (code)
6346     {
6347     case ASHIFT:
6348     case ASHIFTRT:
6349     case LSHIFTRT:
6350     case ROTATERT:
6351     case PLUS:
6352     case MINUS:
6353     case COMPARE:
6354     case NEG:
6355     case NOT:
6356       return COSTS_N_INSNS (1);
6357
6358     case MULT:
6359       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6360         {
6361           int cycles = 0;
6362           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
6363
6364           while (i)
6365             {
6366               i >>= 2;
6367               cycles++;
6368             }
6369           return COSTS_N_INSNS (2) + cycles;
6370         }
6371       return COSTS_N_INSNS (1) + 16;
6372
6373     case SET:
6374       return (COSTS_N_INSNS (1)
6375               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
6376                      + GET_CODE (SET_DEST (x)) == MEM));
6377
6378     case CONST_INT:
6379       if (outer == SET)
6380         {
6381           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
6382             return 0;
6383           if (thumb_shiftable_const (INTVAL (x)))
6384             return COSTS_N_INSNS (2);
6385           return COSTS_N_INSNS (3);
6386         }
6387       else if ((outer == PLUS || outer == COMPARE)
6388                && INTVAL (x) < 256 && INTVAL (x) > -256)
6389         return 0;
6390       else if ((outer == IOR || outer == XOR || outer == AND)
6391                && INTVAL (x) < 256 && INTVAL (x) >= -256)
6392         return COSTS_N_INSNS (1);
6393       else if (outer == AND)
6394         {
6395           int i;
6396           /* This duplicates the tests in the andsi3 expander.  */
6397           for (i = 9; i <= 31; i++)
6398             if ((((HOST_WIDE_INT) 1) << i) - 1 == INTVAL (x)
6399                 || (((HOST_WIDE_INT) 1) << i) - 1 == ~INTVAL (x))
6400               return COSTS_N_INSNS (2);
6401         }
6402       else if (outer == ASHIFT || outer == ASHIFTRT
6403                || outer == LSHIFTRT)
6404         return 0;
6405       return COSTS_N_INSNS (2);
6406
6407     case CONST:
6408     case CONST_DOUBLE:
6409     case LABEL_REF:
6410     case SYMBOL_REF:
6411       return COSTS_N_INSNS (3);
6412
6413     case UDIV:
6414     case UMOD:
6415     case DIV:
6416     case MOD:
6417       return 100;
6418
6419     case TRUNCATE:
6420       return 99;
6421
6422     case AND:
6423     case XOR:
6424     case IOR:
6425       /* XXX guess.  */
6426       return 8;
6427
6428     case MEM:
6429       /* XXX another guess.  */
6430       /* Memory costs quite a lot for the first word, but subsequent words
6431          load at the equivalent of a single insn each.  */
6432       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
6433               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
6434                  ? 4 : 0));
6435
6436     case IF_THEN_ELSE:
6437       /* XXX a guess.  */
6438       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
6439         return 14;
6440       return 2;
6441
6442     case SIGN_EXTEND:
6443     case ZERO_EXTEND:
6444       total = mode == DImode ? COSTS_N_INSNS (1) : 0;
6445       total += thumb1_rtx_costs (XEXP (x, 0), GET_CODE (XEXP (x, 0)), code);
6446
6447       if (mode == SImode)
6448         return total;
6449
6450       if (arm_arch6)
6451         return total + COSTS_N_INSNS (1);
6452
6453       /* Assume a two-shift sequence.  Increase the cost slightly so
6454          we prefer actual shifts over an extend operation.  */
6455       return total + 1 + COSTS_N_INSNS (2);
6456
6457     default:
6458       return 99;
6459     }
6460 }
6461
6462 static inline bool
6463 arm_rtx_costs_1 (rtx x, enum rtx_code outer, int* total, bool speed)
6464 {
6465   enum machine_mode mode = GET_MODE (x);
6466   enum rtx_code subcode;
6467   rtx operand;
6468   enum rtx_code code = GET_CODE (x);
6469   *total = 0;
6470
6471   switch (code)
6472     {
6473     case MEM:
6474       /* Memory costs quite a lot for the first word, but subsequent words
6475          load at the equivalent of a single insn each.  */
6476       *total = COSTS_N_INSNS (2 + ARM_NUM_REGS (mode));
6477       return true;
6478
6479     case DIV:
6480     case MOD:
6481     case UDIV:
6482     case UMOD:
6483       if (TARGET_HARD_FLOAT && mode == SFmode)
6484         *total = COSTS_N_INSNS (2);
6485       else if (TARGET_HARD_FLOAT && mode == DFmode && !TARGET_VFP_SINGLE)
6486         *total = COSTS_N_INSNS (4);
6487       else
6488         *total = COSTS_N_INSNS (20);
6489       return false;
6490
6491     case ROTATE:
6492       if (GET_CODE (XEXP (x, 1)) == REG)
6493         *total = COSTS_N_INSNS (1); /* Need to subtract from 32 */
6494       else if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6495         *total = rtx_cost (XEXP (x, 1), code, speed);
6496
6497       /* Fall through */
6498     case ROTATERT:
6499       if (mode != SImode)
6500         {
6501           *total += COSTS_N_INSNS (4);
6502           return true;
6503         }
6504
6505       /* Fall through */
6506     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
6507       *total += rtx_cost (XEXP (x, 0), code, speed);
6508       if (mode == DImode)
6509         {
6510           *total += COSTS_N_INSNS (3);
6511           return true;
6512         }
6513
6514       *total += COSTS_N_INSNS (1);
6515       /* Increase the cost of complex shifts because they aren't any faster,
6516          and reduce dual issue opportunities.  */
6517       if (arm_tune_cortex_a9
6518           && outer != SET && GET_CODE (XEXP (x, 1)) != CONST_INT)
6519         ++*total;
6520
6521       return true;
6522
6523     case MINUS:
6524       if (mode == DImode)
6525         {
6526           *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
6527           if (GET_CODE (XEXP (x, 0)) == CONST_INT
6528               && const_ok_for_arm (INTVAL (XEXP (x, 0))))
6529             {
6530               *total += rtx_cost (XEXP (x, 1), code, speed);
6531               return true;
6532             }
6533
6534           if (GET_CODE (XEXP (x, 1)) == CONST_INT
6535               && const_ok_for_arm (INTVAL (XEXP (x, 1))))
6536             {
6537               *total += rtx_cost (XEXP (x, 0), code, speed);
6538               return true;
6539             }
6540
6541           return false;
6542         }
6543
6544       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6545         {
6546           if (TARGET_HARD_FLOAT
6547               && (mode == SFmode
6548                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
6549             {
6550               *total = COSTS_N_INSNS (1);
6551               if (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
6552                   && arm_const_double_rtx (XEXP (x, 0)))
6553                 {
6554                   *total += rtx_cost (XEXP (x, 1), code, speed);
6555                   return true;
6556                 }
6557
6558               if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
6559                   && arm_const_double_rtx (XEXP (x, 1)))
6560                 {
6561                   *total += rtx_cost (XEXP (x, 0), code, speed);
6562                   return true;
6563                 }
6564
6565               return false;
6566             }
6567           *total = COSTS_N_INSNS (20);
6568           return false;
6569         }
6570
6571       *total = COSTS_N_INSNS (1);
6572       if (GET_CODE (XEXP (x, 0)) == CONST_INT
6573           && const_ok_for_arm (INTVAL (XEXP (x, 0))))
6574         {
6575           *total += rtx_cost (XEXP (x, 1), code, speed);
6576           return true;
6577         }
6578
6579       subcode = GET_CODE (XEXP (x, 1));
6580       if (subcode == ASHIFT || subcode == ASHIFTRT
6581           || subcode == LSHIFTRT
6582           || subcode == ROTATE || subcode == ROTATERT)
6583         {
6584           *total += rtx_cost (XEXP (x, 0), code, speed);
6585           *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, speed);
6586           return true;
6587         }
6588
6589       /* A shift as a part of RSB costs no more than RSB itself.  */
6590       if (GET_CODE (XEXP (x, 0)) == MULT
6591           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
6592         {
6593           *total += rtx_cost (XEXP (XEXP (x, 0), 0), code, speed);
6594           *total += rtx_cost (XEXP (x, 1), code, speed);
6595           return true;
6596         }
6597
6598       if (subcode == MULT
6599           && power_of_two_operand (XEXP (XEXP (x, 1), 1), SImode))
6600         {
6601           *total += rtx_cost (XEXP (x, 0), code, speed);
6602           *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, speed);
6603           return true;
6604         }
6605
6606       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMPARE
6607           || GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMM_COMPARE)
6608         {
6609           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
6610           if (GET_CODE (XEXP (XEXP (x, 1), 0)) == REG
6611               && REGNO (XEXP (XEXP (x, 1), 0)) != CC_REGNUM)
6612             *total += COSTS_N_INSNS (1);
6613
6614           return true;
6615         }
6616
6617       /* Fall through */
6618
6619     case PLUS:
6620       if (code == PLUS && arm_arch6 && mode == SImode
6621           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
6622               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
6623         {
6624           *total = COSTS_N_INSNS (1);
6625           *total += rtx_cost (XEXP (XEXP (x, 0), 0), GET_CODE (XEXP (x, 0)),
6626                               speed);
6627           *total += rtx_cost (XEXP (x, 1), code, speed);
6628           return true;
6629         }
6630
6631       /* MLA: All arguments must be registers.  We filter out
6632          multiplication by a power of two, so that we fall down into
6633          the code below.  */
6634       if (GET_CODE (XEXP (x, 0)) == MULT
6635           && !power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
6636         {
6637           /* The cost comes from the cost of the multiply.  */
6638           return false;
6639         }
6640
6641       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6642         {
6643           if (TARGET_HARD_FLOAT
6644               && (mode == SFmode
6645                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
6646             {
6647               *total = COSTS_N_INSNS (1);
6648               if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
6649                   && arm_const_double_rtx (XEXP (x, 1)))
6650                 {
6651                   *total += rtx_cost (XEXP (x, 0), code, speed);
6652                   return true;
6653                 }
6654
6655               return false;
6656             }
6657
6658           *total = COSTS_N_INSNS (20);
6659           return false;
6660         }
6661
6662       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMPARE
6663           || GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMM_COMPARE)
6664         {
6665           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 1), code, speed);
6666           if (GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
6667               && REGNO (XEXP (XEXP (x, 0), 0)) != CC_REGNUM)
6668             *total += COSTS_N_INSNS (1);
6669           return true;
6670         }
6671
6672       /* Fall through */
6673
6674     case AND: case XOR: case IOR:
6675
6676       /* Normally the frame registers will be spilt into reg+const during
6677          reload, so it is a bad idea to combine them with other instructions,
6678          since then they might not be moved outside of loops.  As a compromise
6679          we allow integration with ops that have a constant as their second
6680          operand.  */
6681       if (REG_OR_SUBREG_REG (XEXP (x, 0))
6682           && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
6683           && GET_CODE (XEXP (x, 1)) != CONST_INT)
6684         *total = COSTS_N_INSNS (1);
6685
6686       if (mode == DImode)
6687         {
6688           *total += COSTS_N_INSNS (2);
6689           if (GET_CODE (XEXP (x, 1)) == CONST_INT
6690               && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
6691             {
6692               *total += rtx_cost (XEXP (x, 0), code, speed);
6693               return true;
6694             }
6695
6696           return false;
6697         }
6698
6699       *total += COSTS_N_INSNS (1);
6700       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6701           && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
6702         {
6703           *total += rtx_cost (XEXP (x, 0), code, speed);
6704           return true;
6705         }
6706       subcode = GET_CODE (XEXP (x, 0));
6707       if (subcode == ASHIFT || subcode == ASHIFTRT
6708           || subcode == LSHIFTRT
6709           || subcode == ROTATE || subcode == ROTATERT)
6710         {
6711           *total += rtx_cost (XEXP (x, 1), code, speed);
6712           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6713           return true;
6714         }
6715
6716       if (subcode == MULT
6717           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
6718         {
6719           *total += rtx_cost (XEXP (x, 1), code, speed);
6720           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6721           return true;
6722         }
6723
6724       if (subcode == UMIN || subcode == UMAX
6725           || subcode == SMIN || subcode == SMAX)
6726         {
6727           *total = COSTS_N_INSNS (3);
6728           return true;
6729         }
6730
6731       return false;
6732
6733     case MULT:
6734       /* This should have been handled by the CPU specific routines.  */
6735       gcc_unreachable ();
6736
6737     case TRUNCATE:
6738       if (arm_arch3m && mode == SImode
6739           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
6740           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
6741           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
6742               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
6743           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
6744               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
6745         {
6746           *total = rtx_cost (XEXP (XEXP (x, 0), 0), LSHIFTRT, speed);
6747           return true;
6748         }
6749       *total = COSTS_N_INSNS (2); /* Plus the cost of the MULT */
6750       return false;
6751
6752     case NEG:
6753       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6754         {
6755           if (TARGET_HARD_FLOAT
6756               && (mode == SFmode
6757                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
6758             {
6759               *total = COSTS_N_INSNS (1);
6760               return false;
6761             }
6762           *total = COSTS_N_INSNS (2);
6763           return false;
6764         }
6765
6766       /* Fall through */
6767     case NOT:
6768       *total = COSTS_N_INSNS (ARM_NUM_REGS(mode));
6769       if (mode == SImode && code == NOT)
6770         {
6771           subcode = GET_CODE (XEXP (x, 0));
6772           if (subcode == ASHIFT || subcode == ASHIFTRT
6773               || subcode == LSHIFTRT
6774               || subcode == ROTATE || subcode == ROTATERT
6775               || (subcode == MULT
6776                   && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode)))
6777             {
6778               *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6779               /* Register shifts cost an extra cycle.  */
6780               if (GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
6781                 *total += COSTS_N_INSNS (1) + rtx_cost (XEXP (XEXP (x, 0), 1),
6782                                                         subcode, speed);
6783               return true;
6784             }
6785         }
6786
6787       return false;
6788
6789     case IF_THEN_ELSE:
6790       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
6791         {
6792           *total = COSTS_N_INSNS (4);
6793           return true;
6794         }
6795
6796       operand = XEXP (x, 0);
6797
6798       if (!((GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMPARE
6799              || GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMM_COMPARE)
6800             && GET_CODE (XEXP (operand, 0)) == REG
6801             && REGNO (XEXP (operand, 0)) == CC_REGNUM))
6802         *total += COSTS_N_INSNS (1);
6803       *total += (rtx_cost (XEXP (x, 1), code, speed)
6804                  + rtx_cost (XEXP (x, 2), code, speed));
6805       return true;
6806
6807     case NE:
6808       if (mode == SImode && XEXP (x, 1) == const0_rtx)
6809         {
6810           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
6811           return true;
6812         }
6813       goto scc_insn;
6814
6815     case GE:
6816       if ((GET_CODE (XEXP (x, 0)) != REG || REGNO (XEXP (x, 0)) != CC_REGNUM)
6817           && mode == SImode && XEXP (x, 1) == const0_rtx)
6818         {
6819           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
6820           return true;
6821         }
6822       goto scc_insn;
6823
6824     case LT:
6825       if ((GET_CODE (XEXP (x, 0)) != REG || REGNO (XEXP (x, 0)) != CC_REGNUM)
6826           && mode == SImode && XEXP (x, 1) == const0_rtx)
6827         {
6828           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
6829           return true;
6830         }
6831       goto scc_insn;
6832
6833     case EQ:
6834     case GT:
6835     case LE:
6836     case GEU:
6837     case LTU:
6838     case GTU:
6839     case LEU:
6840     case UNORDERED:
6841     case ORDERED:
6842     case UNEQ:
6843     case UNGE:
6844     case UNLT:
6845     case UNGT:
6846     case UNLE:
6847     scc_insn:
6848       /* SCC insns.  In the case where the comparison has already been
6849          performed, then they cost 2 instructions.  Otherwise they need
6850          an additional comparison before them.  */
6851       *total = COSTS_N_INSNS (2);
6852       if (GET_CODE (XEXP (x, 0)) == REG && REGNO (XEXP (x, 0)) == CC_REGNUM)
6853         {
6854           return true;
6855         }
6856
6857       /* Fall through */
6858     case COMPARE:
6859       if (GET_CODE (XEXP (x, 0)) == REG && REGNO (XEXP (x, 0)) == CC_REGNUM)
6860         {
6861           *total = 0;
6862           return true;
6863         }
6864
6865       *total += COSTS_N_INSNS (1);
6866       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6867           && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
6868         {
6869           *total += rtx_cost (XEXP (x, 0), code, speed);
6870           return true;
6871         }
6872
6873       subcode = GET_CODE (XEXP (x, 0));
6874       if (subcode == ASHIFT || subcode == ASHIFTRT
6875           || subcode == LSHIFTRT
6876           || subcode == ROTATE || subcode == ROTATERT)
6877         {
6878           *total += rtx_cost (XEXP (x, 1), code, speed);
6879           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6880           return true;
6881         }
6882
6883       if (subcode == MULT
6884           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
6885         {
6886           *total += rtx_cost (XEXP (x, 1), code, speed);
6887           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6888           return true;
6889         }
6890       
6891       return false;
6892
6893     case UMIN:
6894     case UMAX:
6895     case SMIN:
6896     case SMAX:
6897       *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
6898       if (GET_CODE (XEXP (x, 1)) != CONST_INT
6899           || !const_ok_for_arm (INTVAL (XEXP (x, 1))))
6900         *total += rtx_cost (XEXP (x, 1), code, speed);
6901       return true;
6902
6903     case ABS:
6904       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6905         {
6906           if (TARGET_HARD_FLOAT
6907               && (mode == SFmode
6908                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
6909             {
6910               *total = COSTS_N_INSNS (1);
6911               return false;
6912             }
6913           *total = COSTS_N_INSNS (20);
6914           return false;
6915         }
6916       *total = COSTS_N_INSNS (1);
6917       if (mode == DImode)
6918         *total += COSTS_N_INSNS (3);
6919       return false;
6920
6921     case SIGN_EXTEND:
6922     case ZERO_EXTEND:
6923       *total = 0;
6924       if (GET_MODE_CLASS (mode) == MODE_INT)
6925         {
6926           rtx op = XEXP (x, 0);
6927           enum machine_mode opmode = GET_MODE (op);
6928
6929           if (mode == DImode)
6930             *total += COSTS_N_INSNS (1);
6931
6932           if (opmode != SImode)
6933             {
6934               if (MEM_P (op))
6935                 {
6936                   /* If !arm_arch4, we use one of the extendhisi2_mem
6937                      or movhi_bytes patterns for HImode.  For a QImode
6938                      sign extension, we first zero-extend from memory
6939                      and then perform a shift sequence.  */
6940                   if (!arm_arch4 && (opmode != QImode || code == SIGN_EXTEND))
6941                     *total += COSTS_N_INSNS (2);
6942                 }
6943               else if (arm_arch6)
6944                 *total += COSTS_N_INSNS (1);
6945
6946               /* We don't have the necessary insn, so we need to perform some
6947                  other operation.  */
6948               else if (TARGET_ARM && code == ZERO_EXTEND && mode == QImode)
6949                 /* An and with constant 255.  */
6950                 *total += COSTS_N_INSNS (1);
6951               else
6952                 /* A shift sequence.  Increase costs slightly to avoid
6953                    combining two shifts into an extend operation.  */
6954                 *total += COSTS_N_INSNS (2) + 1;
6955             }
6956
6957           return false;
6958         }
6959
6960       switch (GET_MODE (XEXP (x, 0)))
6961         {
6962         case V8QImode:
6963         case V4HImode:
6964         case V2SImode:
6965         case V4QImode:
6966         case V2HImode:
6967           *total = COSTS_N_INSNS (1);
6968           return false;
6969
6970         default:
6971           gcc_unreachable ();
6972         }
6973       gcc_unreachable ();
6974
6975     case ZERO_EXTRACT:
6976     case SIGN_EXTRACT:
6977       *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
6978       return true;
6979
6980     case CONST_INT:
6981       if (const_ok_for_arm (INTVAL (x))
6982           || const_ok_for_arm (~INTVAL (x)))
6983         *total = COSTS_N_INSNS (1);
6984       else
6985         *total = COSTS_N_INSNS (arm_gen_constant (SET, mode, NULL_RTX,
6986                                                   INTVAL (x), NULL_RTX,
6987                                                   NULL_RTX, 0, 0));
6988       return true;
6989
6990     case CONST:
6991     case LABEL_REF:
6992     case SYMBOL_REF:
6993       *total = COSTS_N_INSNS (3);
6994       return true;
6995
6996     case HIGH:
6997       *total = COSTS_N_INSNS (1);
6998       return true;
6999
7000     case LO_SUM:
7001       *total = COSTS_N_INSNS (1);
7002       *total += rtx_cost (XEXP (x, 0), code, speed);
7003       return true;
7004
7005     case CONST_DOUBLE:
7006       if (TARGET_HARD_FLOAT && vfp3_const_double_rtx (x)
7007           && (mode == SFmode || !TARGET_VFP_SINGLE))
7008         *total = COSTS_N_INSNS (1);
7009       else
7010         *total = COSTS_N_INSNS (4);
7011       return true;
7012
7013     default:
7014       *total = COSTS_N_INSNS (4);
7015       return false;
7016     }
7017 }
7018
7019 /* Estimates the size cost of thumb1 instructions.
7020    For now most of the code is copied from thumb1_rtx_costs. We need more
7021    fine grain tuning when we have more related test cases.  */
7022 static inline int
7023 thumb1_size_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
7024 {
7025   enum machine_mode mode = GET_MODE (x);
7026
7027   switch (code)
7028     {
7029     case ASHIFT:
7030     case ASHIFTRT:
7031     case LSHIFTRT:
7032     case ROTATERT:
7033     case PLUS:
7034     case MINUS:
7035     case COMPARE:
7036     case NEG:
7037     case NOT:
7038       return COSTS_N_INSNS (1);
7039
7040     case MULT:
7041       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7042         {
7043           /* Thumb1 mul instruction can't operate on const. We must Load it
7044              into a register first.  */
7045           int const_size = thumb1_size_rtx_costs (XEXP (x, 1), CONST_INT, SET);
7046           return COSTS_N_INSNS (1) + const_size;
7047         }
7048       return COSTS_N_INSNS (1);
7049
7050     case SET:
7051       return (COSTS_N_INSNS (1)
7052               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
7053                      + GET_CODE (SET_DEST (x)) == MEM));
7054
7055     case CONST_INT:
7056       if (outer == SET)
7057         {
7058           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
7059             return COSTS_N_INSNS (1);
7060           /* See split "TARGET_THUMB1 && satisfies_constraint_J".  */
7061           if (INTVAL (x) >= -255 && INTVAL (x) <= -1)
7062             return COSTS_N_INSNS (2);
7063           /* See split "TARGET_THUMB1 && satisfies_constraint_K".  */
7064           if (thumb_shiftable_const (INTVAL (x)))
7065             return COSTS_N_INSNS (2);
7066           return COSTS_N_INSNS (3);
7067         }
7068       else if ((outer == PLUS || outer == COMPARE)
7069                && INTVAL (x) < 256 && INTVAL (x) > -256)
7070         return 0;
7071       else if ((outer == IOR || outer == XOR || outer == AND)
7072                && INTVAL (x) < 256 && INTVAL (x) >= -256)
7073         return COSTS_N_INSNS (1);
7074       else if (outer == AND)
7075         {
7076           int i;
7077           /* This duplicates the tests in the andsi3 expander.  */
7078           for (i = 9; i <= 31; i++)
7079             if ((((HOST_WIDE_INT) 1) << i) - 1 == INTVAL (x)
7080                 || (((HOST_WIDE_INT) 1) << i) - 1 == ~INTVAL (x))
7081               return COSTS_N_INSNS (2);
7082         }
7083       else if (outer == ASHIFT || outer == ASHIFTRT
7084                || outer == LSHIFTRT)
7085         return 0;
7086       return COSTS_N_INSNS (2);
7087
7088     case CONST:
7089     case CONST_DOUBLE:
7090     case LABEL_REF:
7091     case SYMBOL_REF:
7092       return COSTS_N_INSNS (3);
7093
7094     case UDIV:
7095     case UMOD:
7096     case DIV:
7097     case MOD:
7098       return 100;
7099
7100     case TRUNCATE:
7101       return 99;
7102
7103     case AND:
7104     case XOR:
7105     case IOR:
7106       /* XXX guess.  */
7107       return 8;
7108
7109     case MEM:
7110       /* XXX another guess.  */
7111       /* Memory costs quite a lot for the first word, but subsequent words
7112          load at the equivalent of a single insn each.  */
7113       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
7114               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
7115                  ? 4 : 0));
7116
7117     case IF_THEN_ELSE:
7118       /* XXX a guess.  */
7119       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
7120         return 14;
7121       return 2;
7122
7123     case ZERO_EXTEND:
7124       /* XXX still guessing.  */
7125       switch (GET_MODE (XEXP (x, 0)))
7126         {
7127           case QImode:
7128             return (1 + (mode == DImode ? 4 : 0)
7129                     + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
7130
7131           case HImode:
7132             return (4 + (mode == DImode ? 4 : 0)
7133                     + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
7134
7135           case SImode:
7136             return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
7137
7138           default:
7139             return 99;
7140         }
7141
7142     default:
7143       return 99;
7144     }
7145 }
7146
7147 /* RTX costs when optimizing for size.  */
7148 static bool
7149 arm_size_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7150                     int *total)
7151 {
7152   enum machine_mode mode = GET_MODE (x);
7153   if (TARGET_THUMB1)
7154     {
7155       *total = thumb1_size_rtx_costs (x, code, outer_code);
7156       return true;
7157     }
7158
7159   /* FIXME: This makes no attempt to prefer narrow Thumb-2 instructions.  */
7160   switch (code)
7161     {
7162     case MEM:
7163       /* A memory access costs 1 insn if the mode is small, or the address is
7164          a single register, otherwise it costs one insn per word.  */
7165       if (REG_P (XEXP (x, 0)))
7166         *total = COSTS_N_INSNS (1);
7167       else if (flag_pic
7168                && GET_CODE (XEXP (x, 0)) == PLUS
7169                && will_be_in_index_register (XEXP (XEXP (x, 0), 1)))
7170         /* This will be split into two instructions.
7171            See arm.md:calculate_pic_address.  */
7172         *total = COSTS_N_INSNS (2);
7173       else
7174         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7175       return true;
7176
7177     case DIV:
7178     case MOD:
7179     case UDIV:
7180     case UMOD:
7181       /* Needs a libcall, so it costs about this.  */
7182       *total = COSTS_N_INSNS (2);
7183       return false;
7184
7185     case ROTATE:
7186       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
7187         {
7188           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, false);
7189           return true;
7190         }
7191       /* Fall through */
7192     case ROTATERT:
7193     case ASHIFT:
7194     case LSHIFTRT:
7195     case ASHIFTRT:
7196       if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
7197         {
7198           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code, false);
7199           return true;
7200         }
7201       else if (mode == SImode)
7202         {
7203           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, false);
7204           /* Slightly disparage register shifts, but not by much.  */
7205           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
7206             *total += 1 + rtx_cost (XEXP (x, 1), code, false);
7207           return true;
7208         }
7209
7210       /* Needs a libcall.  */
7211       *total = COSTS_N_INSNS (2);
7212       return false;
7213
7214     case MINUS:
7215       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7216           && (mode == SFmode || !TARGET_VFP_SINGLE))
7217         {
7218           *total = COSTS_N_INSNS (1);
7219           return false;
7220         }
7221
7222       if (mode == SImode)
7223         {
7224           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
7225           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
7226
7227           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
7228               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
7229               || subcode1 == ROTATE || subcode1 == ROTATERT
7230               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
7231               || subcode1 == ASHIFTRT)
7232             {
7233               /* It's just the cost of the two operands.  */
7234               *total = 0;
7235               return false;
7236             }
7237
7238           *total = COSTS_N_INSNS (1);
7239           return false;
7240         }
7241
7242       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7243       return false;
7244
7245     case PLUS:
7246       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7247           && (mode == SFmode || !TARGET_VFP_SINGLE))
7248         {
7249           *total = COSTS_N_INSNS (1);
7250           return false;
7251         }
7252
7253       /* A shift as a part of ADD costs nothing.  */
7254       if (GET_CODE (XEXP (x, 0)) == MULT
7255           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
7256         {
7257           *total = COSTS_N_INSNS (TARGET_THUMB2 ? 2 : 1);
7258           *total += rtx_cost (XEXP (XEXP (x, 0), 0), code, false);
7259           *total += rtx_cost (XEXP (x, 1), code, false);
7260           return true;
7261         }
7262
7263       /* Fall through */
7264     case AND: case XOR: case IOR:
7265       if (mode == SImode)
7266         {
7267           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
7268
7269           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
7270               || subcode == LSHIFTRT || subcode == ASHIFTRT
7271               || (code == AND && subcode == NOT))
7272             {
7273               /* It's just the cost of the two operands.  */
7274               *total = 0;
7275               return false;
7276             }
7277         }
7278
7279       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7280       return false;
7281
7282     case MULT:
7283       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7284       return false;
7285
7286     case NEG:
7287       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7288           && (mode == SFmode || !TARGET_VFP_SINGLE))
7289         {
7290           *total = COSTS_N_INSNS (1);
7291           return false;
7292         }
7293
7294       /* Fall through */
7295     case NOT:
7296       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7297
7298       return false;
7299
7300     case IF_THEN_ELSE:
7301       *total = 0;
7302       return false;
7303
7304     case COMPARE:
7305       if (cc_register (XEXP (x, 0), VOIDmode))
7306         * total = 0;
7307       else
7308         *total = COSTS_N_INSNS (1);
7309       return false;
7310
7311     case ABS:
7312       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7313           && (mode == SFmode || !TARGET_VFP_SINGLE))
7314         *total = COSTS_N_INSNS (1);
7315       else
7316         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
7317       return false;
7318
7319     case SIGN_EXTEND:
7320     case ZERO_EXTEND:
7321       return arm_rtx_costs_1 (x, outer_code, total, 0);
7322
7323     case CONST_INT:
7324       if (const_ok_for_arm (INTVAL (x)))
7325         /* A multiplication by a constant requires another instruction
7326            to load the constant to a register.  */
7327         *total = COSTS_N_INSNS ((outer_code == SET || outer_code == MULT)
7328                                 ? 1 : 0);
7329       else if (const_ok_for_arm (~INTVAL (x)))
7330         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
7331       else if (const_ok_for_arm (-INTVAL (x)))
7332         {
7333           if (outer_code == COMPARE || outer_code == PLUS
7334               || outer_code == MINUS)
7335             *total = 0;
7336           else
7337             *total = COSTS_N_INSNS (1);
7338         }
7339       else
7340         *total = COSTS_N_INSNS (2);
7341       return true;
7342
7343     case CONST:
7344     case LABEL_REF:
7345     case SYMBOL_REF:
7346       *total = COSTS_N_INSNS (2);
7347       return true;
7348
7349     case CONST_DOUBLE:
7350       *total = COSTS_N_INSNS (4);
7351       return true;
7352
7353     case HIGH:
7354     case LO_SUM:
7355       /* We prefer constant pool entries to MOVW/MOVT pairs, so bump the
7356          cost of these slightly.  */
7357       *total = COSTS_N_INSNS (1) + 1;
7358       return true;
7359
7360     default:
7361       if (mode != VOIDmode)
7362         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7363       else
7364         *total = COSTS_N_INSNS (4); /* How knows?  */
7365       return false;
7366     }
7367 }
7368
7369 /* RTX costs when optimizing for size.  */
7370 static bool
7371 arm_rtx_costs (rtx x, int code, int outer_code, int *total,
7372                bool speed)
7373 {
7374   if (!speed)
7375     return arm_size_rtx_costs (x, (enum rtx_code) code,
7376                                (enum rtx_code) outer_code, total);
7377   else
7378     return current_tune->rtx_costs (x, (enum rtx_code) code,
7379                                     (enum rtx_code) outer_code,
7380                                     total, speed);
7381 }
7382
7383 /* RTX costs for cores with a slow MUL implementation.  Thumb-2 is not
7384    supported on any "slowmul" cores, so it can be ignored.  */
7385
7386 static bool
7387 arm_slowmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7388                        int *total, bool speed)
7389 {
7390   enum machine_mode mode = GET_MODE (x);
7391
7392   if (TARGET_THUMB)
7393     {
7394       *total = thumb1_rtx_costs (x, code, outer_code);
7395       return true;
7396     }
7397
7398   switch (code)
7399     {
7400     case MULT:
7401       if (GET_MODE_CLASS (mode) == MODE_FLOAT
7402           || mode == DImode)
7403         {
7404           *total = COSTS_N_INSNS (20);
7405           return false;
7406         }
7407
7408       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7409         {
7410           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
7411                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
7412           int cost, const_ok = const_ok_for_arm (i);
7413           int j, booth_unit_size;
7414
7415           /* Tune as appropriate.  */
7416           cost = const_ok ? 4 : 8;
7417           booth_unit_size = 2;
7418           for (j = 0; i && j < 32; j += booth_unit_size)
7419             {
7420               i >>= booth_unit_size;
7421               cost++;
7422             }
7423
7424           *total = COSTS_N_INSNS (cost);
7425           *total += rtx_cost (XEXP (x, 0), code, speed);
7426           return true;
7427         }
7428
7429       *total = COSTS_N_INSNS (20);
7430       return false;
7431
7432     default:
7433       return arm_rtx_costs_1 (x, outer_code, total, speed);;
7434     }
7435 }
7436
7437
7438 /* RTX cost for cores with a fast multiply unit (M variants).  */
7439
7440 static bool
7441 arm_fastmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7442                        int *total, bool speed)
7443 {
7444   enum machine_mode mode = GET_MODE (x);
7445
7446   if (TARGET_THUMB1)
7447     {
7448       *total = thumb1_rtx_costs (x, code, outer_code);
7449       return true;
7450     }
7451
7452   /* ??? should thumb2 use different costs?  */
7453   switch (code)
7454     {
7455     case MULT:
7456       /* There is no point basing this on the tuning, since it is always the
7457          fast variant if it exists at all.  */
7458       if (mode == DImode
7459           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
7460           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7461               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7462         {
7463           *total = COSTS_N_INSNS(2);
7464           return false;
7465         }
7466
7467
7468       if (mode == DImode)
7469         {
7470           *total = COSTS_N_INSNS (5);
7471           return false;
7472         }
7473
7474       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7475         {
7476           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
7477                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
7478           int cost, const_ok = const_ok_for_arm (i);
7479           int j, booth_unit_size;
7480
7481           /* Tune as appropriate.  */
7482           cost = const_ok ? 4 : 8;
7483           booth_unit_size = 8;
7484           for (j = 0; i && j < 32; j += booth_unit_size)
7485             {
7486               i >>= booth_unit_size;
7487               cost++;
7488             }
7489
7490           *total = COSTS_N_INSNS(cost);
7491           return false;
7492         }
7493
7494       if (mode == SImode)
7495         {
7496           *total = COSTS_N_INSNS (4);
7497           return false;
7498         }
7499
7500       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7501         {
7502           if (TARGET_HARD_FLOAT
7503               && (mode == SFmode
7504                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
7505             {
7506               *total = COSTS_N_INSNS (1);
7507               return false;
7508             }
7509         }
7510
7511       /* Requires a lib call */
7512       *total = COSTS_N_INSNS (20);
7513       return false;
7514
7515     default:
7516       return arm_rtx_costs_1 (x, outer_code, total, speed);
7517     }
7518 }
7519
7520
7521 /* RTX cost for XScale CPUs.  Thumb-2 is not supported on any xscale cores,
7522    so it can be ignored.  */
7523
7524 static bool
7525 arm_xscale_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7526                       int *total, bool speed)
7527 {
7528   enum machine_mode mode = GET_MODE (x);
7529
7530   if (TARGET_THUMB)
7531     {
7532       *total = thumb1_rtx_costs (x, code, outer_code);
7533       return true;
7534     }
7535
7536   switch (code)
7537     {
7538     case COMPARE:
7539       if (GET_CODE (XEXP (x, 0)) != MULT)
7540         return arm_rtx_costs_1 (x, outer_code, total, speed);
7541
7542       /* A COMPARE of a MULT is slow on XScale; the muls instruction
7543          will stall until the multiplication is complete.  */
7544       *total = COSTS_N_INSNS (3);
7545       return false;
7546
7547     case MULT:
7548       /* There is no point basing this on the tuning, since it is always the
7549          fast variant if it exists at all.  */
7550       if (mode == DImode
7551           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
7552           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7553               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7554         {
7555           *total = COSTS_N_INSNS (2);
7556           return false;
7557         }
7558
7559
7560       if (mode == DImode)
7561         {
7562           *total = COSTS_N_INSNS (5);
7563           return false;
7564         }
7565
7566       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7567         {
7568           /* If operand 1 is a constant we can more accurately
7569              calculate the cost of the multiply.  The multiplier can
7570              retire 15 bits on the first cycle and a further 12 on the
7571              second.  We do, of course, have to load the constant into
7572              a register first.  */
7573           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
7574           /* There's a general overhead of one cycle.  */
7575           int cost = 1;
7576           unsigned HOST_WIDE_INT masked_const;
7577
7578           if (i & 0x80000000)
7579             i = ~i;
7580
7581           i &= (unsigned HOST_WIDE_INT) 0xffffffff;
7582
7583           masked_const = i & 0xffff8000;
7584           if (masked_const != 0)
7585             {
7586               cost++;
7587               masked_const = i & 0xf8000000;
7588               if (masked_const != 0)
7589                 cost++;
7590             }
7591           *total = COSTS_N_INSNS (cost);
7592           return false;
7593         }
7594
7595       if (mode == SImode)
7596         {
7597           *total = COSTS_N_INSNS (3);
7598           return false;
7599         }
7600
7601       /* Requires a lib call */
7602       *total = COSTS_N_INSNS (20);
7603       return false;
7604
7605     default:
7606       return arm_rtx_costs_1 (x, outer_code, total, speed);
7607     }
7608 }
7609
7610
7611 /* RTX costs for 9e (and later) cores.  */
7612
7613 static bool
7614 arm_9e_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7615                   int *total, bool speed)
7616 {
7617   enum machine_mode mode = GET_MODE (x);
7618
7619   if (TARGET_THUMB1)
7620     {
7621       switch (code)
7622         {
7623         case MULT:
7624           *total = COSTS_N_INSNS (3);
7625           return true;
7626
7627         default:
7628           *total = thumb1_rtx_costs (x, code, outer_code);
7629           return true;
7630         }
7631     }
7632
7633   switch (code)
7634     {
7635     case MULT:
7636       /* There is no point basing this on the tuning, since it is always the
7637          fast variant if it exists at all.  */
7638       if (mode == DImode
7639           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
7640           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7641               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7642         {
7643           *total = COSTS_N_INSNS (2);
7644           return false;
7645         }
7646
7647
7648       if (mode == DImode)
7649         {
7650           *total = COSTS_N_INSNS (5);
7651           return false;
7652         }
7653
7654       if (mode == SImode)
7655         {
7656           *total = COSTS_N_INSNS (2);
7657           return false;
7658         }
7659
7660       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7661         {
7662           if (TARGET_HARD_FLOAT
7663               && (mode == SFmode
7664                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
7665             {
7666               *total = COSTS_N_INSNS (1);
7667               return false;
7668             }
7669         }
7670
7671       *total = COSTS_N_INSNS (20);
7672       return false;
7673
7674     default:
7675       return arm_rtx_costs_1 (x, outer_code, total, speed);
7676     }
7677 }
7678 /* All address computations that can be done are free, but rtx cost returns
7679    the same for practically all of them.  So we weight the different types
7680    of address here in the order (most pref first):
7681    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
7682 static inline int
7683 arm_arm_address_cost (rtx x)
7684 {
7685   enum rtx_code c  = GET_CODE (x);
7686
7687   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
7688     return 0;
7689   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
7690     return 10;
7691
7692   if (c == PLUS)
7693     {
7694       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7695         return 2;
7696
7697       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
7698         return 3;
7699
7700       return 4;
7701     }
7702
7703   return 6;
7704 }
7705
7706 static inline int
7707 arm_thumb_address_cost (rtx x)
7708 {
7709   enum rtx_code c  = GET_CODE (x);
7710
7711   if (c == REG)
7712     return 1;
7713   if (c == PLUS
7714       && GET_CODE (XEXP (x, 0)) == REG
7715       && GET_CODE (XEXP (x, 1)) == CONST_INT)
7716     return 1;
7717
7718   return 2;
7719 }
7720
7721 static int
7722 arm_address_cost (rtx x, bool speed ATTRIBUTE_UNUSED)
7723 {
7724   return TARGET_32BIT ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
7725 }
7726
7727 /* Adjust cost hook for XScale.  */
7728 static bool
7729 xscale_sched_adjust_cost (rtx insn, rtx link, rtx dep, int * cost)
7730 {
7731   /* Some true dependencies can have a higher cost depending
7732      on precisely how certain input operands are used.  */
7733   if (REG_NOTE_KIND(link) == 0
7734       && recog_memoized (insn) >= 0
7735       && recog_memoized (dep) >= 0)
7736     {
7737       int shift_opnum = get_attr_shift (insn);
7738       enum attr_type attr_type = get_attr_type (dep);
7739
7740       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
7741          operand for INSN.  If we have a shifted input operand and the
7742          instruction we depend on is another ALU instruction, then we may
7743          have to account for an additional stall.  */
7744       if (shift_opnum != 0
7745           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
7746         {
7747           rtx shifted_operand;
7748           int opno;
7749
7750           /* Get the shifted operand.  */
7751           extract_insn (insn);
7752           shifted_operand = recog_data.operand[shift_opnum];
7753
7754           /* Iterate over all the operands in DEP.  If we write an operand
7755              that overlaps with SHIFTED_OPERAND, then we have increase the
7756              cost of this dependency.  */
7757           extract_insn (dep);
7758           preprocess_constraints ();
7759           for (opno = 0; opno < recog_data.n_operands; opno++)
7760             {
7761               /* We can ignore strict inputs.  */
7762               if (recog_data.operand_type[opno] == OP_IN)
7763                 continue;
7764
7765               if (reg_overlap_mentioned_p (recog_data.operand[opno],
7766                                            shifted_operand))
7767                 {
7768                   *cost = 2;
7769                   return false;
7770                 }
7771             }
7772         }
7773     }
7774   return true;
7775 }
7776
7777 /* Adjust cost hook for Cortex A9.  */
7778 static bool
7779 cortex_a9_sched_adjust_cost (rtx insn, rtx link, rtx dep, int * cost)
7780 {
7781   switch (REG_NOTE_KIND (link))
7782     {
7783     case REG_DEP_ANTI:
7784       *cost = 0;
7785       return false;
7786
7787     case REG_DEP_TRUE:
7788     case REG_DEP_OUTPUT:
7789         if (recog_memoized (insn) >= 0
7790             && recog_memoized (dep) >= 0)
7791           {
7792             if (GET_CODE (PATTERN (insn)) == SET)
7793               {
7794                 if (GET_MODE_CLASS 
7795                     (GET_MODE (SET_DEST (PATTERN (insn)))) == MODE_FLOAT
7796                   || GET_MODE_CLASS 
7797                     (GET_MODE (SET_SRC (PATTERN (insn)))) == MODE_FLOAT)
7798                   {
7799                     enum attr_type attr_type_insn = get_attr_type (insn);
7800                     enum attr_type attr_type_dep = get_attr_type (dep);
7801
7802                     /* By default all dependencies of the form
7803                        s0 = s0 <op> s1
7804                        s0 = s0 <op> s2
7805                        have an extra latency of 1 cycle because
7806                        of the input and output dependency in this
7807                        case. However this gets modeled as an true
7808                        dependency and hence all these checks.  */
7809                     if (REG_P (SET_DEST (PATTERN (insn)))
7810                         && REG_P (SET_DEST (PATTERN (dep)))
7811                         && reg_overlap_mentioned_p (SET_DEST (PATTERN (insn)),
7812                                                     SET_DEST (PATTERN (dep))))
7813                       {
7814                         /* FMACS is a special case where the dependant
7815                            instruction can be issued 3 cycles before
7816                            the normal latency in case of an output 
7817                            dependency.  */
7818                         if ((attr_type_insn == TYPE_FMACS
7819                              || attr_type_insn == TYPE_FMACD)
7820                             && (attr_type_dep == TYPE_FMACS
7821                                 || attr_type_dep == TYPE_FMACD))
7822                           {
7823                             if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
7824                               *cost = insn_default_latency (dep) - 3;
7825                             else
7826                               *cost = insn_default_latency (dep);
7827                             return false;
7828                           }
7829                         else
7830                           {
7831                             if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
7832                               *cost = insn_default_latency (dep) + 1;
7833                             else
7834                               *cost = insn_default_latency (dep);
7835                           }
7836                         return false;
7837                       }
7838                   }
7839               }
7840           }
7841         break;
7842
7843     default:
7844       gcc_unreachable ();
7845     }
7846
7847   return true;
7848 }
7849
7850 /* This function implements the target macro TARGET_SCHED_ADJUST_COST.
7851    It corrects the value of COST based on the relationship between
7852    INSN and DEP through the dependence LINK.  It returns the new
7853    value. There is a per-core adjust_cost hook to adjust scheduler costs
7854    and the per-core hook can choose to completely override the generic 
7855    adjust_cost function. Only put bits of code into arm_adjust_cost that 
7856    are common across all cores.  */
7857 static int
7858 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
7859 {
7860   rtx i_pat, d_pat;
7861
7862  /* When generating Thumb-1 code, we want to place flag-setting operations
7863     close to a conditional branch which depends on them, so that we can
7864     omit the comparison. */
7865   if (TARGET_THUMB1
7866       && REG_NOTE_KIND (link) == 0
7867       && recog_memoized (insn) == CODE_FOR_cbranchsi4_insn
7868       && recog_memoized (dep) >= 0
7869       && get_attr_conds (dep) == CONDS_SET)
7870     return 0;
7871
7872   if (current_tune->sched_adjust_cost != NULL)
7873     {
7874       if (!current_tune->sched_adjust_cost (insn, link, dep, &cost))
7875         return cost;
7876     }
7877
7878   /* XXX This is not strictly true for the FPA.  */
7879   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
7880       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
7881     return 0;
7882
7883   /* Call insns don't incur a stall, even if they follow a load.  */
7884   if (REG_NOTE_KIND (link) == 0
7885       && GET_CODE (insn) == CALL_INSN)
7886     return 1;
7887
7888   if ((i_pat = single_set (insn)) != NULL
7889       && GET_CODE (SET_SRC (i_pat)) == MEM
7890       && (d_pat = single_set (dep)) != NULL
7891       && GET_CODE (SET_DEST (d_pat)) == MEM)
7892     {
7893       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
7894       /* This is a load after a store, there is no conflict if the load reads
7895          from a cached area.  Assume that loads from the stack, and from the
7896          constant pool are cached, and that others will miss.  This is a
7897          hack.  */
7898
7899       if ((GET_CODE (src_mem) == SYMBOL_REF 
7900            && CONSTANT_POOL_ADDRESS_P (src_mem))
7901           || reg_mentioned_p (stack_pointer_rtx, src_mem)
7902           || reg_mentioned_p (frame_pointer_rtx, src_mem)
7903           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
7904         return 1;
7905     }
7906
7907   return cost;
7908 }
7909
7910 static int fp_consts_inited = 0;
7911
7912 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
7913 static const char * const strings_fp[8] =
7914 {
7915   "0",   "1",   "2",   "3",
7916   "4",   "5",   "0.5", "10"
7917 };
7918
7919 static REAL_VALUE_TYPE values_fp[8];
7920
7921 static void
7922 init_fp_table (void)
7923 {
7924   int i;
7925   REAL_VALUE_TYPE r;
7926
7927   if (TARGET_VFP)
7928     fp_consts_inited = 1;
7929   else
7930     fp_consts_inited = 8;
7931
7932   for (i = 0; i < fp_consts_inited; i++)
7933     {
7934       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
7935       values_fp[i] = r;
7936     }
7937 }
7938
7939 /* Return TRUE if rtx X is a valid immediate FP constant.  */
7940 int
7941 arm_const_double_rtx (rtx x)
7942 {
7943   REAL_VALUE_TYPE r;
7944   int i;
7945
7946   if (!fp_consts_inited)
7947     init_fp_table ();
7948
7949   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7950   if (REAL_VALUE_MINUS_ZERO (r))
7951     return 0;
7952
7953   for (i = 0; i < fp_consts_inited; i++)
7954     if (REAL_VALUES_EQUAL (r, values_fp[i]))
7955       return 1;
7956
7957   return 0;
7958 }
7959
7960 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
7961 int
7962 neg_const_double_rtx_ok_for_fpa (rtx x)
7963 {
7964   REAL_VALUE_TYPE r;
7965   int i;
7966
7967   if (!fp_consts_inited)
7968     init_fp_table ();
7969
7970   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7971   r = real_value_negate (&r);
7972   if (REAL_VALUE_MINUS_ZERO (r))
7973     return 0;
7974
7975   for (i = 0; i < 8; i++)
7976     if (REAL_VALUES_EQUAL (r, values_fp[i]))
7977       return 1;
7978
7979   return 0;
7980 }
7981
7982
7983 /* VFPv3 has a fairly wide range of representable immediates, formed from
7984    "quarter-precision" floating-point values. These can be evaluated using this
7985    formula (with ^ for exponentiation):
7986
7987      -1^s * n * 2^-r
7988
7989    Where 's' is a sign bit (0/1), 'n' and 'r' are integers such that
7990    16 <= n <= 31 and 0 <= r <= 7.
7991
7992    These values are mapped onto an 8-bit integer ABCDEFGH s.t.
7993
7994      - A (most-significant) is the sign bit.
7995      - BCD are the exponent (encoded as r XOR 3).
7996      - EFGH are the mantissa (encoded as n - 16).
7997 */
7998
7999 /* Return an integer index for a VFPv3 immediate operand X suitable for the
8000    fconst[sd] instruction, or -1 if X isn't suitable.  */
8001 static int
8002 vfp3_const_double_index (rtx x)
8003 {
8004   REAL_VALUE_TYPE r, m;
8005   int sign, exponent;
8006   unsigned HOST_WIDE_INT mantissa, mant_hi;
8007   unsigned HOST_WIDE_INT mask;
8008   HOST_WIDE_INT m1, m2;
8009   int point_pos = 2 * HOST_BITS_PER_WIDE_INT - 1;
8010
8011   if (!TARGET_VFP3 || GET_CODE (x) != CONST_DOUBLE)
8012     return -1;
8013
8014   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8015
8016   /* We can't represent these things, so detect them first.  */
8017   if (REAL_VALUE_ISINF (r) || REAL_VALUE_ISNAN (r) || REAL_VALUE_MINUS_ZERO (r))
8018     return -1;
8019
8020   /* Extract sign, exponent and mantissa.  */
8021   sign = REAL_VALUE_NEGATIVE (r) ? 1 : 0;
8022   r = real_value_abs (&r);
8023   exponent = REAL_EXP (&r);
8024   /* For the mantissa, we expand into two HOST_WIDE_INTS, apart from the
8025      highest (sign) bit, with a fixed binary point at bit point_pos.
8026      WARNING: If there's ever a VFP version which uses more than 2 * H_W_I - 1
8027      bits for the mantissa, this may fail (low bits would be lost).  */
8028   real_ldexp (&m, &r, point_pos - exponent);
8029   REAL_VALUE_TO_INT (&m1, &m2, m);
8030   mantissa = m1;
8031   mant_hi = m2;
8032
8033   /* If there are bits set in the low part of the mantissa, we can't
8034      represent this value.  */
8035   if (mantissa != 0)
8036     return -1;
8037
8038   /* Now make it so that mantissa contains the most-significant bits, and move
8039      the point_pos to indicate that the least-significant bits have been
8040      discarded.  */
8041   point_pos -= HOST_BITS_PER_WIDE_INT;
8042   mantissa = mant_hi;
8043
8044   /* We can permit four significant bits of mantissa only, plus a high bit
8045      which is always 1.  */
8046   mask = ((unsigned HOST_WIDE_INT)1 << (point_pos - 5)) - 1;
8047   if ((mantissa & mask) != 0)
8048     return -1;
8049
8050   /* Now we know the mantissa is in range, chop off the unneeded bits.  */
8051   mantissa >>= point_pos - 5;
8052
8053   /* The mantissa may be zero. Disallow that case. (It's possible to load the
8054      floating-point immediate zero with Neon using an integer-zero load, but
8055      that case is handled elsewhere.)  */
8056   if (mantissa == 0)
8057     return -1;
8058
8059   gcc_assert (mantissa >= 16 && mantissa <= 31);
8060
8061   /* The value of 5 here would be 4 if GCC used IEEE754-like encoding (where
8062      normalized significands are in the range [1, 2). (Our mantissa is shifted
8063      left 4 places at this point relative to normalized IEEE754 values).  GCC
8064      internally uses [0.5, 1) (see real.c), so the exponent returned from
8065      REAL_EXP must be altered.  */
8066   exponent = 5 - exponent;
8067
8068   if (exponent < 0 || exponent > 7)
8069     return -1;
8070
8071   /* Sign, mantissa and exponent are now in the correct form to plug into the
8072      formula described in the comment above.  */
8073   return (sign << 7) | ((exponent ^ 3) << 4) | (mantissa - 16);
8074 }
8075
8076 /* Return TRUE if rtx X is a valid immediate VFPv3 constant.  */
8077 int
8078 vfp3_const_double_rtx (rtx x)
8079 {
8080   if (!TARGET_VFP3)
8081     return 0;
8082
8083   return vfp3_const_double_index (x) != -1;
8084 }
8085
8086 /* Recognize immediates which can be used in various Neon instructions. Legal
8087    immediates are described by the following table (for VMVN variants, the
8088    bitwise inverse of the constant shown is recognized. In either case, VMOV
8089    is output and the correct instruction to use for a given constant is chosen
8090    by the assembler). The constant shown is replicated across all elements of
8091    the destination vector.
8092
8093    insn elems variant constant (binary)
8094    ---- ----- ------- -----------------
8095    vmov  i32     0    00000000 00000000 00000000 abcdefgh
8096    vmov  i32     1    00000000 00000000 abcdefgh 00000000
8097    vmov  i32     2    00000000 abcdefgh 00000000 00000000
8098    vmov  i32     3    abcdefgh 00000000 00000000 00000000
8099    vmov  i16     4    00000000 abcdefgh
8100    vmov  i16     5    abcdefgh 00000000
8101    vmvn  i32     6    00000000 00000000 00000000 abcdefgh
8102    vmvn  i32     7    00000000 00000000 abcdefgh 00000000
8103    vmvn  i32     8    00000000 abcdefgh 00000000 00000000
8104    vmvn  i32     9    abcdefgh 00000000 00000000 00000000
8105    vmvn  i16    10    00000000 abcdefgh
8106    vmvn  i16    11    abcdefgh 00000000
8107    vmov  i32    12    00000000 00000000 abcdefgh 11111111
8108    vmvn  i32    13    00000000 00000000 abcdefgh 11111111
8109    vmov  i32    14    00000000 abcdefgh 11111111 11111111
8110    vmvn  i32    15    00000000 abcdefgh 11111111 11111111
8111    vmov   i8    16    abcdefgh
8112    vmov  i64    17    aaaaaaaa bbbbbbbb cccccccc dddddddd
8113                       eeeeeeee ffffffff gggggggg hhhhhhhh
8114    vmov  f32    18    aBbbbbbc defgh000 00000000 00000000
8115
8116    For case 18, B = !b. Representable values are exactly those accepted by
8117    vfp3_const_double_index, but are output as floating-point numbers rather
8118    than indices.
8119
8120    Variants 0-5 (inclusive) may also be used as immediates for the second
8121    operand of VORR/VBIC instructions.
8122
8123    The INVERSE argument causes the bitwise inverse of the given operand to be
8124    recognized instead (used for recognizing legal immediates for the VAND/VORN
8125    pseudo-instructions). If INVERSE is true, the value placed in *MODCONST is
8126    *not* inverted (i.e. the pseudo-instruction forms vand/vorn should still be
8127    output, rather than the real insns vbic/vorr).
8128
8129    INVERSE makes no difference to the recognition of float vectors.
8130
8131    The return value is the variant of immediate as shown in the above table, or
8132    -1 if the given value doesn't match any of the listed patterns.
8133 */
8134 static int
8135 neon_valid_immediate (rtx op, enum machine_mode mode, int inverse,
8136                       rtx *modconst, int *elementwidth)
8137 {
8138 #define CHECK(STRIDE, ELSIZE, CLASS, TEST)      \
8139   matches = 1;                                  \
8140   for (i = 0; i < idx; i += (STRIDE))           \
8141     if (!(TEST))                                \
8142       matches = 0;                              \
8143   if (matches)                                  \
8144     {                                           \
8145       immtype = (CLASS);                        \
8146       elsize = (ELSIZE);                        \
8147       break;                                    \
8148     }
8149
8150   unsigned int i, elsize = 0, idx = 0, n_elts = CONST_VECTOR_NUNITS (op);
8151   unsigned int innersize = GET_MODE_SIZE (GET_MODE_INNER (mode));
8152   unsigned char bytes[16];
8153   int immtype = -1, matches;
8154   unsigned int invmask = inverse ? 0xff : 0;
8155
8156   /* Vectors of float constants.  */
8157   if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
8158     {
8159       rtx el0 = CONST_VECTOR_ELT (op, 0);
8160       REAL_VALUE_TYPE r0;
8161
8162       if (!vfp3_const_double_rtx (el0))
8163         return -1;
8164
8165       REAL_VALUE_FROM_CONST_DOUBLE (r0, el0);
8166
8167       for (i = 1; i < n_elts; i++)
8168         {
8169           rtx elt = CONST_VECTOR_ELT (op, i);
8170           REAL_VALUE_TYPE re;
8171
8172           REAL_VALUE_FROM_CONST_DOUBLE (re, elt);
8173
8174           if (!REAL_VALUES_EQUAL (r0, re))
8175             return -1;
8176         }
8177
8178       if (modconst)
8179         *modconst = CONST_VECTOR_ELT (op, 0);
8180
8181       if (elementwidth)
8182         *elementwidth = 0;
8183
8184       return 18;
8185     }
8186
8187   /* Splat vector constant out into a byte vector.  */
8188   for (i = 0; i < n_elts; i++)
8189     {
8190       rtx el = CONST_VECTOR_ELT (op, i);
8191       unsigned HOST_WIDE_INT elpart;
8192       unsigned int part, parts;
8193
8194       if (GET_CODE (el) == CONST_INT)
8195         {
8196           elpart = INTVAL (el);
8197           parts = 1;
8198         }
8199       else if (GET_CODE (el) == CONST_DOUBLE)
8200         {
8201           elpart = CONST_DOUBLE_LOW (el);
8202           parts = 2;
8203         }
8204       else
8205         gcc_unreachable ();
8206
8207       for (part = 0; part < parts; part++)
8208         {
8209           unsigned int byte;
8210           for (byte = 0; byte < innersize; byte++)
8211             {
8212               bytes[idx++] = (elpart & 0xff) ^ invmask;
8213               elpart >>= BITS_PER_UNIT;
8214             }
8215           if (GET_CODE (el) == CONST_DOUBLE)
8216             elpart = CONST_DOUBLE_HIGH (el);
8217         }
8218     }
8219
8220   /* Sanity check.  */
8221   gcc_assert (idx == GET_MODE_SIZE (mode));
8222
8223   do
8224     {
8225       CHECK (4, 32, 0, bytes[i] == bytes[0] && bytes[i + 1] == 0
8226                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
8227
8228       CHECK (4, 32, 1, bytes[i] == 0 && bytes[i + 1] == bytes[1]
8229                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
8230
8231       CHECK (4, 32, 2, bytes[i] == 0 && bytes[i + 1] == 0
8232                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
8233
8234       CHECK (4, 32, 3, bytes[i] == 0 && bytes[i + 1] == 0
8235                        && bytes[i + 2] == 0 && bytes[i + 3] == bytes[3]);
8236
8237       CHECK (2, 16, 4, bytes[i] == bytes[0] && bytes[i + 1] == 0);
8238
8239       CHECK (2, 16, 5, bytes[i] == 0 && bytes[i + 1] == bytes[1]);
8240
8241       CHECK (4, 32, 6, bytes[i] == bytes[0] && bytes[i + 1] == 0xff
8242                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
8243
8244       CHECK (4, 32, 7, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
8245                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
8246
8247       CHECK (4, 32, 8, bytes[i] == 0xff && bytes[i + 1] == 0xff
8248                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
8249
8250       CHECK (4, 32, 9, bytes[i] == 0xff && bytes[i + 1] == 0xff
8251                        && bytes[i + 2] == 0xff && bytes[i + 3] == bytes[3]);
8252
8253       CHECK (2, 16, 10, bytes[i] == bytes[0] && bytes[i + 1] == 0xff);
8254
8255       CHECK (2, 16, 11, bytes[i] == 0xff && bytes[i + 1] == bytes[1]);
8256
8257       CHECK (4, 32, 12, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
8258                         && bytes[i + 2] == 0 && bytes[i + 3] == 0);
8259
8260       CHECK (4, 32, 13, bytes[i] == 0 && bytes[i + 1] == bytes[1]
8261                         && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
8262
8263       CHECK (4, 32, 14, bytes[i] == 0xff && bytes[i + 1] == 0xff
8264                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
8265
8266       CHECK (4, 32, 15, bytes[i] == 0 && bytes[i + 1] == 0
8267                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
8268
8269       CHECK (1, 8, 16, bytes[i] == bytes[0]);
8270
8271       CHECK (1, 64, 17, (bytes[i] == 0 || bytes[i] == 0xff)
8272                         && bytes[i] == bytes[(i + 8) % idx]);
8273     }
8274   while (0);
8275
8276   if (immtype == -1)
8277     return -1;
8278
8279   if (elementwidth)
8280     *elementwidth = elsize;
8281
8282   if (modconst)
8283     {
8284       unsigned HOST_WIDE_INT imm = 0;
8285
8286       /* Un-invert bytes of recognized vector, if necessary.  */
8287       if (invmask != 0)
8288         for (i = 0; i < idx; i++)
8289           bytes[i] ^= invmask;
8290
8291       if (immtype == 17)
8292         {
8293           /* FIXME: Broken on 32-bit H_W_I hosts.  */
8294           gcc_assert (sizeof (HOST_WIDE_INT) == 8);
8295
8296           for (i = 0; i < 8; i++)
8297             imm |= (unsigned HOST_WIDE_INT) (bytes[i] ? 0xff : 0)
8298                    << (i * BITS_PER_UNIT);
8299
8300           *modconst = GEN_INT (imm);
8301         }
8302       else
8303         {
8304           unsigned HOST_WIDE_INT imm = 0;
8305
8306           for (i = 0; i < elsize / BITS_PER_UNIT; i++)
8307             imm |= (unsigned HOST_WIDE_INT) bytes[i] << (i * BITS_PER_UNIT);
8308
8309           *modconst = GEN_INT (imm);
8310         }
8311     }
8312
8313   return immtype;
8314 #undef CHECK
8315 }
8316
8317 /* Return TRUE if rtx X is legal for use as either a Neon VMOV (or, implicitly,
8318    VMVN) immediate. Write back width per element to *ELEMENTWIDTH (or zero for
8319    float elements), and a modified constant (whatever should be output for a
8320    VMOV) in *MODCONST.  */
8321
8322 int
8323 neon_immediate_valid_for_move (rtx op, enum machine_mode mode,
8324                                rtx *modconst, int *elementwidth)
8325 {
8326   rtx tmpconst;
8327   int tmpwidth;
8328   int retval = neon_valid_immediate (op, mode, 0, &tmpconst, &tmpwidth);
8329
8330   if (retval == -1)
8331     return 0;
8332
8333   if (modconst)
8334     *modconst = tmpconst;
8335
8336   if (elementwidth)
8337     *elementwidth = tmpwidth;
8338
8339   return 1;
8340 }
8341
8342 /* Return TRUE if rtx X is legal for use in a VORR or VBIC instruction.  If
8343    the immediate is valid, write a constant suitable for using as an operand
8344    to VORR/VBIC/VAND/VORN to *MODCONST and the corresponding element width to
8345    *ELEMENTWIDTH. See neon_valid_immediate for description of INVERSE.  */
8346
8347 int
8348 neon_immediate_valid_for_logic (rtx op, enum machine_mode mode, int inverse,
8349                                 rtx *modconst, int *elementwidth)
8350 {
8351   rtx tmpconst;
8352   int tmpwidth;
8353   int retval = neon_valid_immediate (op, mode, inverse, &tmpconst, &tmpwidth);
8354
8355   if (retval < 0 || retval > 5)
8356     return 0;
8357
8358   if (modconst)
8359     *modconst = tmpconst;
8360
8361   if (elementwidth)
8362     *elementwidth = tmpwidth;
8363
8364   return 1;
8365 }
8366
8367 /* Return a string suitable for output of Neon immediate logic operation
8368    MNEM.  */
8369
8370 char *
8371 neon_output_logic_immediate (const char *mnem, rtx *op2, enum machine_mode mode,
8372                              int inverse, int quad)
8373 {
8374   int width, is_valid;
8375   static char templ[40];
8376
8377   is_valid = neon_immediate_valid_for_logic (*op2, mode, inverse, op2, &width);
8378
8379   gcc_assert (is_valid != 0);
8380
8381   if (quad)
8382     sprintf (templ, "%s.i%d\t%%q0, %%2", mnem, width);
8383   else
8384     sprintf (templ, "%s.i%d\t%%P0, %%2", mnem, width);
8385
8386   return templ;
8387 }
8388
8389 /* Output a sequence of pairwise operations to implement a reduction.
8390    NOTE: We do "too much work" here, because pairwise operations work on two
8391    registers-worth of operands in one go. Unfortunately we can't exploit those
8392    extra calculations to do the full operation in fewer steps, I don't think.
8393    Although all vector elements of the result but the first are ignored, we
8394    actually calculate the same result in each of the elements. An alternative
8395    such as initially loading a vector with zero to use as each of the second
8396    operands would use up an additional register and take an extra instruction,
8397    for no particular gain.  */
8398
8399 void
8400 neon_pairwise_reduce (rtx op0, rtx op1, enum machine_mode mode,
8401                       rtx (*reduc) (rtx, rtx, rtx))
8402 {
8403   enum machine_mode inner = GET_MODE_INNER (mode);
8404   unsigned int i, parts = GET_MODE_SIZE (mode) / GET_MODE_SIZE (inner);
8405   rtx tmpsum = op1;
8406
8407   for (i = parts / 2; i >= 1; i /= 2)
8408     {
8409       rtx dest = (i == 1) ? op0 : gen_reg_rtx (mode);
8410       emit_insn (reduc (dest, tmpsum, tmpsum));
8411       tmpsum = dest;
8412     }
8413 }
8414
8415 /* If VALS is a vector constant that can be loaded into a register
8416    using VDUP, generate instructions to do so and return an RTX to
8417    assign to the register.  Otherwise return NULL_RTX.  */
8418
8419 static rtx
8420 neon_vdup_constant (rtx vals)
8421 {
8422   enum machine_mode mode = GET_MODE (vals);
8423   enum machine_mode inner_mode = GET_MODE_INNER (mode);
8424   int n_elts = GET_MODE_NUNITS (mode);
8425   bool all_same = true;
8426   rtx x;
8427   int i;
8428
8429   if (GET_CODE (vals) != CONST_VECTOR || GET_MODE_SIZE (inner_mode) > 4)
8430     return NULL_RTX;
8431
8432   for (i = 0; i < n_elts; ++i)
8433     {
8434       x = XVECEXP (vals, 0, i);
8435       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
8436         all_same = false;
8437     }
8438
8439   if (!all_same)
8440     /* The elements are not all the same.  We could handle repeating
8441        patterns of a mode larger than INNER_MODE here (e.g. int8x8_t
8442        {0, C, 0, C, 0, C, 0, C} which can be loaded using
8443        vdup.i16).  */
8444     return NULL_RTX;
8445
8446   /* We can load this constant by using VDUP and a constant in a
8447      single ARM register.  This will be cheaper than a vector
8448      load.  */
8449
8450   x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, 0));
8451   return gen_rtx_VEC_DUPLICATE (mode, x);
8452 }
8453
8454 /* Generate code to load VALS, which is a PARALLEL containing only
8455    constants (for vec_init) or CONST_VECTOR, efficiently into a
8456    register.  Returns an RTX to copy into the register, or NULL_RTX
8457    for a PARALLEL that can not be converted into a CONST_VECTOR.  */
8458
8459 rtx
8460 neon_make_constant (rtx vals)
8461 {
8462   enum machine_mode mode = GET_MODE (vals);
8463   rtx target;
8464   rtx const_vec = NULL_RTX;
8465   int n_elts = GET_MODE_NUNITS (mode);
8466   int n_const = 0;
8467   int i;
8468
8469   if (GET_CODE (vals) == CONST_VECTOR)
8470     const_vec = vals;
8471   else if (GET_CODE (vals) == PARALLEL)
8472     {
8473       /* A CONST_VECTOR must contain only CONST_INTs and
8474          CONST_DOUBLEs, but CONSTANT_P allows more (e.g. SYMBOL_REF).
8475          Only store valid constants in a CONST_VECTOR.  */
8476       for (i = 0; i < n_elts; ++i)
8477         {
8478           rtx x = XVECEXP (vals, 0, i);
8479           if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
8480             n_const++;
8481         }
8482       if (n_const == n_elts)
8483         const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
8484     }
8485   else
8486     gcc_unreachable ();
8487
8488   if (const_vec != NULL
8489       && neon_immediate_valid_for_move (const_vec, mode, NULL, NULL))
8490     /* Load using VMOV.  On Cortex-A8 this takes one cycle.  */
8491     return const_vec;
8492   else if ((target = neon_vdup_constant (vals)) != NULL_RTX)
8493     /* Loaded using VDUP.  On Cortex-A8 the VDUP takes one NEON
8494        pipeline cycle; creating the constant takes one or two ARM
8495        pipeline cycles.  */
8496     return target;
8497   else if (const_vec != NULL_RTX)
8498     /* Load from constant pool.  On Cortex-A8 this takes two cycles
8499        (for either double or quad vectors).  We can not take advantage
8500        of single-cycle VLD1 because we need a PC-relative addressing
8501        mode.  */
8502     return const_vec;
8503   else
8504     /* A PARALLEL containing something not valid inside CONST_VECTOR.
8505        We can not construct an initializer.  */
8506     return NULL_RTX;
8507 }
8508
8509 /* Initialize vector TARGET to VALS.  */
8510
8511 void
8512 neon_expand_vector_init (rtx target, rtx vals)
8513 {
8514   enum machine_mode mode = GET_MODE (target);
8515   enum machine_mode inner_mode = GET_MODE_INNER (mode);
8516   int n_elts = GET_MODE_NUNITS (mode);
8517   int n_var = 0, one_var = -1;
8518   bool all_same = true;
8519   rtx x, mem;
8520   int i;
8521
8522   for (i = 0; i < n_elts; ++i)
8523     {
8524       x = XVECEXP (vals, 0, i);
8525       if (!CONSTANT_P (x))
8526         ++n_var, one_var = i;
8527
8528       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
8529         all_same = false;
8530     }
8531
8532   if (n_var == 0)
8533     {
8534       rtx constant = neon_make_constant (vals);
8535       if (constant != NULL_RTX)
8536         {
8537           emit_move_insn (target, constant);
8538           return;
8539         }
8540     }
8541
8542   /* Splat a single non-constant element if we can.  */
8543   if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
8544     {
8545       x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, 0));
8546       emit_insn (gen_rtx_SET (VOIDmode, target,
8547                               gen_rtx_VEC_DUPLICATE (mode, x)));
8548       return;
8549     }
8550
8551   /* One field is non-constant.  Load constant then overwrite varying
8552      field.  This is more efficient than using the stack.  */
8553   if (n_var == 1)
8554     {
8555       rtx copy = copy_rtx (vals);
8556       rtx index = GEN_INT (one_var);
8557
8558       /* Load constant part of vector, substitute neighboring value for
8559          varying element.  */
8560       XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
8561       neon_expand_vector_init (target, copy);
8562
8563       /* Insert variable.  */
8564       x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, one_var));
8565       switch (mode)
8566         {
8567         case V8QImode:
8568           emit_insn (gen_neon_vset_lanev8qi (target, x, target, index));
8569           break;
8570         case V16QImode:
8571           emit_insn (gen_neon_vset_lanev16qi (target, x, target, index));
8572           break;
8573         case V4HImode:
8574           emit_insn (gen_neon_vset_lanev4hi (target, x, target, index));
8575           break;
8576         case V8HImode:
8577           emit_insn (gen_neon_vset_lanev8hi (target, x, target, index));
8578           break;
8579         case V2SImode:
8580           emit_insn (gen_neon_vset_lanev2si (target, x, target, index));
8581           break;
8582         case V4SImode:
8583           emit_insn (gen_neon_vset_lanev4si (target, x, target, index));
8584           break;
8585         case V2SFmode:
8586           emit_insn (gen_neon_vset_lanev2sf (target, x, target, index));
8587           break;
8588         case V4SFmode:
8589           emit_insn (gen_neon_vset_lanev4sf (target, x, target, index));
8590           break;
8591         case V2DImode:
8592           emit_insn (gen_neon_vset_lanev2di (target, x, target, index));
8593           break;
8594         default:
8595           gcc_unreachable ();
8596         }
8597       return;
8598     }
8599
8600   /* Construct the vector in memory one field at a time
8601      and load the whole vector.  */
8602   mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), 0);
8603   for (i = 0; i < n_elts; i++)
8604     emit_move_insn (adjust_address_nv (mem, inner_mode,
8605                                     i * GET_MODE_SIZE (inner_mode)),
8606                     XVECEXP (vals, 0, i));
8607   emit_move_insn (target, mem);
8608 }
8609
8610 /* Ensure OPERAND lies between LOW (inclusive) and HIGH (exclusive).  Raise
8611    ERR if it doesn't.  FIXME: NEON bounds checks occur late in compilation, so
8612    reported source locations are bogus.  */
8613
8614 static void
8615 bounds_check (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high,
8616               const char *err)
8617 {
8618   HOST_WIDE_INT lane;
8619
8620   gcc_assert (GET_CODE (operand) == CONST_INT);
8621
8622   lane = INTVAL (operand);
8623
8624   if (lane < low || lane >= high)
8625     error (err);
8626 }
8627
8628 /* Bounds-check lanes.  */
8629
8630 void
8631 neon_lane_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
8632 {
8633   bounds_check (operand, low, high, "lane out of range");
8634 }
8635
8636 /* Bounds-check constants.  */
8637
8638 void
8639 neon_const_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
8640 {
8641   bounds_check (operand, low, high, "constant out of range");
8642 }
8643
8644 HOST_WIDE_INT
8645 neon_element_bits (enum machine_mode mode)
8646 {
8647   if (mode == DImode)
8648     return GET_MODE_BITSIZE (mode);
8649   else
8650     return GET_MODE_BITSIZE (GET_MODE_INNER (mode));
8651 }
8652
8653 \f
8654 /* Predicates for `match_operand' and `match_operator'.  */
8655
8656 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
8657 int
8658 cirrus_memory_offset (rtx op)
8659 {
8660   /* Reject eliminable registers.  */
8661   if (! (reload_in_progress || reload_completed)
8662       && (   reg_mentioned_p (frame_pointer_rtx, op)
8663           || reg_mentioned_p (arg_pointer_rtx, op)
8664           || reg_mentioned_p (virtual_incoming_args_rtx, op)
8665           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8666           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8667           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8668     return 0;
8669
8670   if (GET_CODE (op) == MEM)
8671     {
8672       rtx ind;
8673
8674       ind = XEXP (op, 0);
8675
8676       /* Match: (mem (reg)).  */
8677       if (GET_CODE (ind) == REG)
8678         return 1;
8679
8680       /* Match:
8681          (mem (plus (reg)
8682                     (const))).  */
8683       if (GET_CODE (ind) == PLUS
8684           && GET_CODE (XEXP (ind, 0)) == REG
8685           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
8686           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
8687         return 1;
8688     }
8689
8690   return 0;
8691 }
8692
8693 /* Return TRUE if OP is a valid coprocessor memory address pattern.
8694    WB is true if full writeback address modes are allowed and is false
8695    if limited writeback address modes (POST_INC and PRE_DEC) are
8696    allowed.  */
8697
8698 int
8699 arm_coproc_mem_operand (rtx op, bool wb)
8700 {
8701   rtx ind;
8702
8703   /* Reject eliminable registers.  */
8704   if (! (reload_in_progress || reload_completed)
8705       && (   reg_mentioned_p (frame_pointer_rtx, op)
8706           || reg_mentioned_p (arg_pointer_rtx, op)
8707           || reg_mentioned_p (virtual_incoming_args_rtx, op)
8708           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8709           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8710           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8711     return FALSE;
8712
8713   /* Constants are converted into offsets from labels.  */
8714   if (GET_CODE (op) != MEM)
8715     return FALSE;
8716
8717   ind = XEXP (op, 0);
8718
8719   if (reload_completed
8720       && (GET_CODE (ind) == LABEL_REF
8721           || (GET_CODE (ind) == CONST
8722               && GET_CODE (XEXP (ind, 0)) == PLUS
8723               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
8724               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
8725     return TRUE;
8726
8727   /* Match: (mem (reg)).  */
8728   if (GET_CODE (ind) == REG)
8729     return arm_address_register_rtx_p (ind, 0);
8730
8731   /* Autoincremment addressing modes.  POST_INC and PRE_DEC are
8732      acceptable in any case (subject to verification by
8733      arm_address_register_rtx_p).  We need WB to be true to accept
8734      PRE_INC and POST_DEC.  */
8735   if (GET_CODE (ind) == POST_INC
8736       || GET_CODE (ind) == PRE_DEC
8737       || (wb
8738           && (GET_CODE (ind) == PRE_INC
8739               || GET_CODE (ind) == POST_DEC)))
8740     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
8741
8742   if (wb
8743       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
8744       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
8745       && GET_CODE (XEXP (ind, 1)) == PLUS
8746       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
8747     ind = XEXP (ind, 1);
8748
8749   /* Match:
8750      (plus (reg)
8751            (const)).  */
8752   if (GET_CODE (ind) == PLUS
8753       && GET_CODE (XEXP (ind, 0)) == REG
8754       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
8755       && GET_CODE (XEXP (ind, 1)) == CONST_INT
8756       && INTVAL (XEXP (ind, 1)) > -1024
8757       && INTVAL (XEXP (ind, 1)) <  1024
8758       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
8759     return TRUE;
8760
8761   return FALSE;
8762 }
8763
8764 /* Return TRUE if OP is a memory operand which we can load or store a vector
8765    to/from. TYPE is one of the following values:
8766     0 - Vector load/stor (vldr)
8767     1 - Core registers (ldm)
8768     2 - Element/structure loads (vld1)
8769  */
8770 int
8771 neon_vector_mem_operand (rtx op, int type)
8772 {
8773   rtx ind;
8774
8775   /* Reject eliminable registers.  */
8776   if (! (reload_in_progress || reload_completed)
8777       && (   reg_mentioned_p (frame_pointer_rtx, op)
8778           || reg_mentioned_p (arg_pointer_rtx, op)
8779           || reg_mentioned_p (virtual_incoming_args_rtx, op)
8780           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8781           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8782           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8783     return FALSE;
8784
8785   /* Constants are converted into offsets from labels.  */
8786   if (GET_CODE (op) != MEM)
8787     return FALSE;
8788
8789   ind = XEXP (op, 0);
8790
8791   if (reload_completed
8792       && (GET_CODE (ind) == LABEL_REF
8793           || (GET_CODE (ind) == CONST
8794               && GET_CODE (XEXP (ind, 0)) == PLUS
8795               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
8796               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
8797     return TRUE;
8798
8799   /* Match: (mem (reg)).  */
8800   if (GET_CODE (ind) == REG)
8801     return arm_address_register_rtx_p (ind, 0);
8802
8803   /* Allow post-increment with Neon registers.  */
8804   if (type != 1 && (GET_CODE (ind) == POST_INC || GET_CODE (ind) == PRE_DEC))
8805     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
8806
8807   /* FIXME: vld1 allows register post-modify.  */
8808
8809   /* Match:
8810      (plus (reg)
8811           (const)).  */
8812   if (type == 0
8813       && GET_CODE (ind) == PLUS
8814       && GET_CODE (XEXP (ind, 0)) == REG
8815       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
8816       && GET_CODE (XEXP (ind, 1)) == CONST_INT
8817       && INTVAL (XEXP (ind, 1)) > -1024
8818       && INTVAL (XEXP (ind, 1)) < 1016
8819       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
8820     return TRUE;
8821
8822   return FALSE;
8823 }
8824
8825 /* Return TRUE if OP is a mem suitable for loading/storing a Neon struct
8826    type.  */
8827 int
8828 neon_struct_mem_operand (rtx op)
8829 {
8830   rtx ind;
8831
8832   /* Reject eliminable registers.  */
8833   if (! (reload_in_progress || reload_completed)
8834       && (   reg_mentioned_p (frame_pointer_rtx, op)
8835           || reg_mentioned_p (arg_pointer_rtx, op)
8836           || reg_mentioned_p (virtual_incoming_args_rtx, op)
8837           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8838           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8839           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8840     return FALSE;
8841
8842   /* Constants are converted into offsets from labels.  */
8843   if (GET_CODE (op) != MEM)
8844     return FALSE;
8845
8846   ind = XEXP (op, 0);
8847
8848   if (reload_completed
8849       && (GET_CODE (ind) == LABEL_REF
8850           || (GET_CODE (ind) == CONST
8851               && GET_CODE (XEXP (ind, 0)) == PLUS
8852               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
8853               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
8854     return TRUE;
8855
8856   /* Match: (mem (reg)).  */
8857   if (GET_CODE (ind) == REG)
8858     return arm_address_register_rtx_p (ind, 0);
8859
8860   return FALSE;
8861 }
8862
8863 /* Return true if X is a register that will be eliminated later on.  */
8864 int
8865 arm_eliminable_register (rtx x)
8866 {
8867   return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
8868                        || REGNO (x) == ARG_POINTER_REGNUM
8869                        || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
8870                            && REGNO (x) <= LAST_VIRTUAL_REGISTER));
8871 }
8872
8873 /* Return GENERAL_REGS if a scratch register required to reload x to/from
8874    coprocessor registers.  Otherwise return NO_REGS.  */
8875
8876 enum reg_class
8877 coproc_secondary_reload_class (enum machine_mode mode, rtx x, bool wb)
8878 {
8879   if (mode == HFmode)
8880     {
8881       if (!TARGET_NEON_FP16)
8882         return GENERAL_REGS;
8883       if (s_register_operand (x, mode) || neon_vector_mem_operand (x, 2))
8884         return NO_REGS;
8885       return GENERAL_REGS;
8886     }
8887
8888   if (TARGET_NEON
8889       && (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
8890           || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
8891       && neon_vector_mem_operand (x, 0))
8892      return NO_REGS;
8893
8894   if (arm_coproc_mem_operand (x, wb) || s_register_operand (x, mode))
8895     return NO_REGS;
8896
8897   return GENERAL_REGS;
8898 }
8899
8900 /* Values which must be returned in the most-significant end of the return
8901    register.  */
8902
8903 static bool
8904 arm_return_in_msb (const_tree valtype)
8905 {
8906   return (TARGET_AAPCS_BASED
8907           && BYTES_BIG_ENDIAN
8908           && (AGGREGATE_TYPE_P (valtype)
8909               || TREE_CODE (valtype) == COMPLEX_TYPE));
8910 }
8911
8912 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
8913    Use by the Cirrus Maverick code which has to workaround
8914    a hardware bug triggered by such instructions.  */
8915 static bool
8916 arm_memory_load_p (rtx insn)
8917 {
8918   rtx body, lhs, rhs;;
8919
8920   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
8921     return false;
8922
8923   body = PATTERN (insn);
8924
8925   if (GET_CODE (body) != SET)
8926     return false;
8927
8928   lhs = XEXP (body, 0);
8929   rhs = XEXP (body, 1);
8930
8931   lhs = REG_OR_SUBREG_RTX (lhs);
8932
8933   /* If the destination is not a general purpose
8934      register we do not have to worry.  */
8935   if (GET_CODE (lhs) != REG
8936       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
8937     return false;
8938
8939   /* As well as loads from memory we also have to react
8940      to loads of invalid constants which will be turned
8941      into loads from the minipool.  */
8942   return (GET_CODE (rhs) == MEM
8943           || GET_CODE (rhs) == SYMBOL_REF
8944           || note_invalid_constants (insn, -1, false));
8945 }
8946
8947 /* Return TRUE if INSN is a Cirrus instruction.  */
8948 static bool
8949 arm_cirrus_insn_p (rtx insn)
8950 {
8951   enum attr_cirrus attr;
8952
8953   /* get_attr cannot accept USE or CLOBBER.  */
8954   if (!insn
8955       || GET_CODE (insn) != INSN
8956       || GET_CODE (PATTERN (insn)) == USE
8957       || GET_CODE (PATTERN (insn)) == CLOBBER)
8958     return 0;
8959
8960   attr = get_attr_cirrus (insn);
8961
8962   return attr != CIRRUS_NOT;
8963 }
8964
8965 /* Cirrus reorg for invalid instruction combinations.  */
8966 static void
8967 cirrus_reorg (rtx first)
8968 {
8969   enum attr_cirrus attr;
8970   rtx body = PATTERN (first);
8971   rtx t;
8972   int nops;
8973
8974   /* Any branch must be followed by 2 non Cirrus instructions.  */
8975   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
8976     {
8977       nops = 0;
8978       t = next_nonnote_insn (first);
8979
8980       if (arm_cirrus_insn_p (t))
8981         ++ nops;
8982
8983       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
8984         ++ nops;
8985
8986       while (nops --)
8987         emit_insn_after (gen_nop (), first);
8988
8989       return;
8990     }
8991
8992   /* (float (blah)) is in parallel with a clobber.  */
8993   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
8994     body = XVECEXP (body, 0, 0);
8995
8996   if (GET_CODE (body) == SET)
8997     {
8998       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
8999
9000       /* cfldrd, cfldr64, cfstrd, cfstr64 must
9001          be followed by a non Cirrus insn.  */
9002       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
9003         {
9004           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
9005             emit_insn_after (gen_nop (), first);
9006
9007           return;
9008         }
9009       else if (arm_memory_load_p (first))
9010         {
9011           unsigned int arm_regno;
9012
9013           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
9014              ldr/cfmv64hr combination where the Rd field is the same
9015              in both instructions must be split with a non Cirrus
9016              insn.  Example:
9017
9018              ldr r0, blah
9019              nop
9020              cfmvsr mvf0, r0.  */
9021
9022           /* Get Arm register number for ldr insn.  */
9023           if (GET_CODE (lhs) == REG)
9024             arm_regno = REGNO (lhs);
9025           else
9026             {
9027               gcc_assert (GET_CODE (rhs) == REG);
9028               arm_regno = REGNO (rhs);
9029             }
9030
9031           /* Next insn.  */
9032           first = next_nonnote_insn (first);
9033
9034           if (! arm_cirrus_insn_p (first))
9035             return;
9036
9037           body = PATTERN (first);
9038
9039           /* (float (blah)) is in parallel with a clobber.  */
9040           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
9041             body = XVECEXP (body, 0, 0);
9042
9043           if (GET_CODE (body) == FLOAT)
9044             body = XEXP (body, 0);
9045
9046           if (get_attr_cirrus (first) == CIRRUS_MOVE
9047               && GET_CODE (XEXP (body, 1)) == REG
9048               && arm_regno == REGNO (XEXP (body, 1)))
9049             emit_insn_after (gen_nop (), first);
9050
9051           return;
9052         }
9053     }
9054
9055   /* get_attr cannot accept USE or CLOBBER.  */
9056   if (!first
9057       || GET_CODE (first) != INSN
9058       || GET_CODE (PATTERN (first)) == USE
9059       || GET_CODE (PATTERN (first)) == CLOBBER)
9060     return;
9061
9062   attr = get_attr_cirrus (first);
9063
9064   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
9065      must be followed by a non-coprocessor instruction.  */
9066   if (attr == CIRRUS_COMPARE)
9067     {
9068       nops = 0;
9069
9070       t = next_nonnote_insn (first);
9071
9072       if (arm_cirrus_insn_p (t))
9073         ++ nops;
9074
9075       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
9076         ++ nops;
9077
9078       while (nops --)
9079         emit_insn_after (gen_nop (), first);
9080
9081       return;
9082     }
9083 }
9084
9085 /* Return TRUE if X references a SYMBOL_REF.  */
9086 int
9087 symbol_mentioned_p (rtx x)
9088 {
9089   const char * fmt;
9090   int i;
9091
9092   if (GET_CODE (x) == SYMBOL_REF)
9093     return 1;
9094
9095   /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
9096      are constant offsets, not symbols.  */
9097   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
9098     return 0;
9099
9100   fmt = GET_RTX_FORMAT (GET_CODE (x));
9101
9102   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
9103     {
9104       if (fmt[i] == 'E')
9105         {
9106           int j;
9107
9108           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9109             if (symbol_mentioned_p (XVECEXP (x, i, j)))
9110               return 1;
9111         }
9112       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
9113         return 1;
9114     }
9115
9116   return 0;
9117 }
9118
9119 /* Return TRUE if X references a LABEL_REF.  */
9120 int
9121 label_mentioned_p (rtx x)
9122 {
9123   const char * fmt;
9124   int i;
9125
9126   if (GET_CODE (x) == LABEL_REF)
9127     return 1;
9128
9129   /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
9130      instruction, but they are constant offsets, not symbols.  */
9131   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
9132     return 0;
9133
9134   fmt = GET_RTX_FORMAT (GET_CODE (x));
9135   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
9136     {
9137       if (fmt[i] == 'E')
9138         {
9139           int j;
9140
9141           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9142             if (label_mentioned_p (XVECEXP (x, i, j)))
9143               return 1;
9144         }
9145       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
9146         return 1;
9147     }
9148
9149   return 0;
9150 }
9151
9152 int
9153 tls_mentioned_p (rtx x)
9154 {
9155   switch (GET_CODE (x))
9156     {
9157     case CONST:
9158       return tls_mentioned_p (XEXP (x, 0));
9159
9160     case UNSPEC:
9161       if (XINT (x, 1) == UNSPEC_TLS)
9162         return 1;
9163
9164     default:
9165       return 0;
9166     }
9167 }
9168
9169 /* Must not copy any rtx that uses a pc-relative address.  */
9170
9171 static int
9172 arm_note_pic_base (rtx *x, void *date ATTRIBUTE_UNUSED)
9173 {
9174   if (GET_CODE (*x) == UNSPEC
9175       && XINT (*x, 1) == UNSPEC_PIC_BASE)
9176     return 1;
9177   return 0;
9178 }
9179
9180 static bool
9181 arm_cannot_copy_insn_p (rtx insn)
9182 {
9183   return for_each_rtx (&PATTERN (insn), arm_note_pic_base, NULL);
9184 }
9185
9186 enum rtx_code
9187 minmax_code (rtx x)
9188 {
9189   enum rtx_code code = GET_CODE (x);
9190
9191   switch (code)
9192     {
9193     case SMAX:
9194       return GE;
9195     case SMIN:
9196       return LE;
9197     case UMIN:
9198       return LEU;
9199     case UMAX:
9200       return GEU;
9201     default:
9202       gcc_unreachable ();
9203     }
9204 }
9205
9206 /* Return 1 if memory locations are adjacent.  */
9207 int
9208 adjacent_mem_locations (rtx a, rtx b)
9209 {
9210   /* We don't guarantee to preserve the order of these memory refs.  */
9211   if (volatile_refs_p (a) || volatile_refs_p (b))
9212     return 0;
9213
9214   if ((GET_CODE (XEXP (a, 0)) == REG
9215        || (GET_CODE (XEXP (a, 0)) == PLUS
9216            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
9217       && (GET_CODE (XEXP (b, 0)) == REG
9218           || (GET_CODE (XEXP (b, 0)) == PLUS
9219               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
9220     {
9221       HOST_WIDE_INT val0 = 0, val1 = 0;
9222       rtx reg0, reg1;
9223       int val_diff;
9224
9225       if (GET_CODE (XEXP (a, 0)) == PLUS)
9226         {
9227           reg0 = XEXP (XEXP (a, 0), 0);
9228           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
9229         }
9230       else
9231         reg0 = XEXP (a, 0);
9232
9233       if (GET_CODE (XEXP (b, 0)) == PLUS)
9234         {
9235           reg1 = XEXP (XEXP (b, 0), 0);
9236           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
9237         }
9238       else
9239         reg1 = XEXP (b, 0);
9240
9241       /* Don't accept any offset that will require multiple
9242          instructions to handle, since this would cause the
9243          arith_adjacentmem pattern to output an overlong sequence.  */
9244       if (!const_ok_for_op (val0, PLUS) || !const_ok_for_op (val1, PLUS))
9245         return 0;
9246
9247       /* Don't allow an eliminable register: register elimination can make
9248          the offset too large.  */
9249       if (arm_eliminable_register (reg0))
9250         return 0;
9251
9252       val_diff = val1 - val0;
9253
9254       if (arm_ld_sched)
9255         {
9256           /* If the target has load delay slots, then there's no benefit
9257              to using an ldm instruction unless the offset is zero and
9258              we are optimizing for size.  */
9259           return (optimize_size && (REGNO (reg0) == REGNO (reg1))
9260                   && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
9261                   && (val_diff == 4 || val_diff == -4));
9262         }
9263
9264       return ((REGNO (reg0) == REGNO (reg1))
9265               && (val_diff == 4 || val_diff == -4));
9266     }
9267
9268   return 0;
9269 }
9270
9271 /* Return true iff it would be profitable to turn a sequence of NOPS loads
9272    or stores (depending on IS_STORE) into a load-multiple or store-multiple
9273    instruction.  ADD_OFFSET is nonzero if the base address register needs
9274    to be modified with an add instruction before we can use it.  */
9275
9276 static bool
9277 multiple_operation_profitable_p (bool is_store ATTRIBUTE_UNUSED,
9278                                  int nops, HOST_WIDE_INT add_offset)
9279  {
9280   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
9281      if the offset isn't small enough.  The reason 2 ldrs are faster
9282      is because these ARMs are able to do more than one cache access
9283      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
9284      whilst the ARM8 has a double bandwidth cache.  This means that
9285      these cores can do both an instruction fetch and a data fetch in
9286      a single cycle, so the trick of calculating the address into a
9287      scratch register (one of the result regs) and then doing a load
9288      multiple actually becomes slower (and no smaller in code size).
9289      That is the transformation
9290
9291         ldr     rd1, [rbase + offset]
9292         ldr     rd2, [rbase + offset + 4]
9293
9294      to
9295
9296         add     rd1, rbase, offset
9297         ldmia   rd1, {rd1, rd2}
9298
9299      produces worse code -- '3 cycles + any stalls on rd2' instead of
9300      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
9301      access per cycle, the first sequence could never complete in less
9302      than 6 cycles, whereas the ldm sequence would only take 5 and
9303      would make better use of sequential accesses if not hitting the
9304      cache.
9305
9306      We cheat here and test 'arm_ld_sched' which we currently know to
9307      only be true for the ARM8, ARM9 and StrongARM.  If this ever
9308      changes, then the test below needs to be reworked.  */
9309   if (nops == 2 && arm_ld_sched && add_offset != 0)
9310     return false;
9311
9312   /* XScale has load-store double instructions, but they have stricter
9313      alignment requirements than load-store multiple, so we cannot
9314      use them.
9315
9316      For XScale ldm requires 2 + NREGS cycles to complete and blocks
9317      the pipeline until completion.
9318
9319         NREGS           CYCLES
9320           1               3
9321           2               4
9322           3               5
9323           4               6
9324
9325      An ldr instruction takes 1-3 cycles, but does not block the
9326      pipeline.
9327
9328         NREGS           CYCLES
9329           1              1-3
9330           2              2-6
9331           3              3-9
9332           4              4-12
9333
9334      Best case ldr will always win.  However, the more ldr instructions
9335      we issue, the less likely we are to be able to schedule them well.
9336      Using ldr instructions also increases code size.
9337
9338      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
9339      for counts of 3 or 4 regs.  */
9340   if (nops <= 2 && arm_tune_xscale && !optimize_size)
9341     return false;
9342   return true;
9343 }
9344
9345 /* Subroutine of load_multiple_sequence and store_multiple_sequence.
9346    Given an array of UNSORTED_OFFSETS, of which there are NOPS, compute
9347    an array ORDER which describes the sequence to use when accessing the
9348    offsets that produces an ascending order.  In this sequence, each
9349    offset must be larger by exactly 4 than the previous one.  ORDER[0]
9350    must have been filled in with the lowest offset by the caller.
9351    If UNSORTED_REGS is nonnull, it is an array of register numbers that
9352    we use to verify that ORDER produces an ascending order of registers.
9353    Return true if it was possible to construct such an order, false if
9354    not.  */
9355
9356 static bool
9357 compute_offset_order (int nops, HOST_WIDE_INT *unsorted_offsets, int *order,
9358                       int *unsorted_regs)
9359 {
9360   int i;
9361   for (i = 1; i < nops; i++)
9362     {
9363       int j;
9364
9365       order[i] = order[i - 1];
9366       for (j = 0; j < nops; j++)
9367         if (unsorted_offsets[j] == unsorted_offsets[order[i - 1]] + 4)
9368           {
9369             /* We must find exactly one offset that is higher than the
9370                previous one by 4.  */
9371             if (order[i] != order[i - 1])
9372               return false;
9373             order[i] = j;
9374           }
9375       if (order[i] == order[i - 1])
9376         return false;
9377       /* The register numbers must be ascending.  */
9378       if (unsorted_regs != NULL
9379           && unsorted_regs[order[i]] <= unsorted_regs[order[i - 1]])
9380         return false;
9381     }
9382   return true;
9383 }
9384
9385 /* Used to determine in a peephole whether a sequence of load
9386    instructions can be changed into a load-multiple instruction.
9387    NOPS is the number of separate load instructions we are examining.  The
9388    first NOPS entries in OPERANDS are the destination registers, the
9389    next NOPS entries are memory operands.  If this function is
9390    successful, *BASE is set to the common base register of the memory
9391    accesses; *LOAD_OFFSET is set to the first memory location's offset
9392    from that base register.
9393    REGS is an array filled in with the destination register numbers.
9394    SAVED_ORDER (if nonnull), is an array filled in with an order that maps
9395    insn numbers to to an ascending order of stores.  If CHECK_REGS is true,
9396    the sequence of registers in REGS matches the loads from ascending memory
9397    locations, and the function verifies that the register numbers are
9398    themselves ascending.  If CHECK_REGS is false, the register numbers
9399    are stored in the order they are found in the operands.  */
9400 static int
9401 load_multiple_sequence (rtx *operands, int nops, int *regs, int *saved_order,
9402                         int *base, HOST_WIDE_INT *load_offset, bool check_regs)
9403 {
9404   int unsorted_regs[MAX_LDM_STM_OPS];
9405   HOST_WIDE_INT unsorted_offsets[MAX_LDM_STM_OPS];
9406   int order[MAX_LDM_STM_OPS];
9407   rtx base_reg_rtx = NULL;
9408   int base_reg = -1;
9409   int i, ldm_case;
9410
9411   /* Can only handle up to MAX_LDM_STM_OPS insns at present, though could be
9412      easily extended if required.  */
9413   gcc_assert (nops >= 2 && nops <= MAX_LDM_STM_OPS);
9414
9415   memset (order, 0, MAX_LDM_STM_OPS * sizeof (int));
9416
9417   /* Loop over the operands and check that the memory references are
9418      suitable (i.e. immediate offsets from the same base register).  At
9419      the same time, extract the target register, and the memory
9420      offsets.  */
9421   for (i = 0; i < nops; i++)
9422     {
9423       rtx reg;
9424       rtx offset;
9425
9426       /* Convert a subreg of a mem into the mem itself.  */
9427       if (GET_CODE (operands[nops + i]) == SUBREG)
9428         operands[nops + i] = alter_subreg (operands + (nops + i));
9429
9430       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
9431
9432       /* Don't reorder volatile memory references; it doesn't seem worth
9433          looking for the case where the order is ok anyway.  */
9434       if (MEM_VOLATILE_P (operands[nops + i]))
9435         return 0;
9436
9437       offset = const0_rtx;
9438
9439       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
9440            || (GET_CODE (reg) == SUBREG
9441                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9442           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
9443               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
9444                    == REG)
9445                   || (GET_CODE (reg) == SUBREG
9446                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9447               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
9448                   == CONST_INT)))
9449         {
9450           if (i == 0)
9451             {
9452               base_reg = REGNO (reg);
9453               base_reg_rtx = reg;
9454               if (TARGET_THUMB1 && base_reg > LAST_LO_REGNUM)
9455                 return 0;
9456             }
9457           else if (base_reg != (int) REGNO (reg))
9458             /* Not addressed from the same base register.  */
9459             return 0;
9460
9461           unsorted_regs[i] = (GET_CODE (operands[i]) == REG
9462                               ? REGNO (operands[i])
9463                               : REGNO (SUBREG_REG (operands[i])));
9464
9465           /* If it isn't an integer register, or if it overwrites the
9466              base register but isn't the last insn in the list, then
9467              we can't do this.  */
9468           if (unsorted_regs[i] < 0
9469               || (TARGET_THUMB1 && unsorted_regs[i] > LAST_LO_REGNUM)
9470               || unsorted_regs[i] > 14
9471               || (i != nops - 1 && unsorted_regs[i] == base_reg))
9472             return 0;
9473
9474           unsorted_offsets[i] = INTVAL (offset);
9475           if (i == 0 || unsorted_offsets[i] < unsorted_offsets[order[0]])
9476             order[0] = i;
9477         }
9478       else
9479         /* Not a suitable memory address.  */
9480         return 0;
9481     }
9482
9483   /* All the useful information has now been extracted from the
9484      operands into unsorted_regs and unsorted_offsets; additionally,
9485      order[0] has been set to the lowest offset in the list.  Sort
9486      the offsets into order, verifying that they are adjacent, and
9487      check that the register numbers are ascending.  */
9488   if (!compute_offset_order (nops, unsorted_offsets, order,
9489                              check_regs ? unsorted_regs : NULL))
9490     return 0;
9491
9492   if (saved_order)
9493     memcpy (saved_order, order, sizeof order);
9494
9495   if (base)
9496     {
9497       *base = base_reg;
9498
9499       for (i = 0; i < nops; i++)
9500         regs[i] = unsorted_regs[check_regs ? order[i] : i];
9501
9502       *load_offset = unsorted_offsets[order[0]];
9503     }
9504
9505   if (TARGET_THUMB1
9506       && !peep2_reg_dead_p (nops, base_reg_rtx))
9507     return 0;
9508
9509   if (unsorted_offsets[order[0]] == 0)
9510     ldm_case = 1; /* ldmia */
9511   else if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
9512     ldm_case = 2; /* ldmib */
9513   else if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
9514     ldm_case = 3; /* ldmda */
9515   else if (TARGET_32BIT && unsorted_offsets[order[nops - 1]] == -4)
9516     ldm_case = 4; /* ldmdb */
9517   else if (const_ok_for_arm (unsorted_offsets[order[0]])
9518            || const_ok_for_arm (-unsorted_offsets[order[0]]))
9519     ldm_case = 5;
9520   else
9521     return 0;
9522
9523   if (!multiple_operation_profitable_p (false, nops,
9524                                         ldm_case == 5
9525                                         ? unsorted_offsets[order[0]] : 0))
9526     return 0;
9527
9528   return ldm_case;
9529 }
9530
9531 /* Used to determine in a peephole whether a sequence of store instructions can
9532    be changed into a store-multiple instruction.
9533    NOPS is the number of separate store instructions we are examining.
9534    NOPS_TOTAL is the total number of instructions recognized by the peephole
9535    pattern.
9536    The first NOPS entries in OPERANDS are the source registers, the next
9537    NOPS entries are memory operands.  If this function is successful, *BASE is
9538    set to the common base register of the memory accesses; *LOAD_OFFSET is set
9539    to the first memory location's offset from that base register.  REGS is an
9540    array filled in with the source register numbers, REG_RTXS (if nonnull) is
9541    likewise filled with the corresponding rtx's.
9542    SAVED_ORDER (if nonnull), is an array filled in with an order that maps insn
9543    numbers to to an ascending order of stores.
9544    If CHECK_REGS is true, the sequence of registers in *REGS matches the stores
9545    from ascending memory locations, and the function verifies that the register
9546    numbers are themselves ascending.  If CHECK_REGS is false, the register
9547    numbers are stored in the order they are found in the operands.  */
9548 static int
9549 store_multiple_sequence (rtx *operands, int nops, int nops_total,
9550                          int *regs, rtx *reg_rtxs, int *saved_order, int *base,
9551                          HOST_WIDE_INT *load_offset, bool check_regs)
9552 {
9553   int unsorted_regs[MAX_LDM_STM_OPS];
9554   rtx unsorted_reg_rtxs[MAX_LDM_STM_OPS];
9555   HOST_WIDE_INT unsorted_offsets[MAX_LDM_STM_OPS];
9556   int order[MAX_LDM_STM_OPS];
9557   int base_reg = -1;
9558   rtx base_reg_rtx = NULL;
9559   int i, stm_case;
9560
9561   /* Can only handle up to MAX_LDM_STM_OPS insns at present, though could be
9562      easily extended if required.  */
9563   gcc_assert (nops >= 2 && nops <= MAX_LDM_STM_OPS);
9564
9565   memset (order, 0, MAX_LDM_STM_OPS * sizeof (int));
9566
9567   /* Loop over the operands and check that the memory references are
9568      suitable (i.e. immediate offsets from the same base register).  At
9569      the same time, extract the target register, and the memory
9570      offsets.  */
9571   for (i = 0; i < nops; i++)
9572     {
9573       rtx reg;
9574       rtx offset;
9575
9576       /* Convert a subreg of a mem into the mem itself.  */
9577       if (GET_CODE (operands[nops + i]) == SUBREG)
9578         operands[nops + i] = alter_subreg (operands + (nops + i));
9579
9580       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
9581
9582       /* Don't reorder volatile memory references; it doesn't seem worth
9583          looking for the case where the order is ok anyway.  */
9584       if (MEM_VOLATILE_P (operands[nops + i]))
9585         return 0;
9586
9587       offset = const0_rtx;
9588
9589       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
9590            || (GET_CODE (reg) == SUBREG
9591                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9592           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
9593               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
9594                    == REG)
9595                   || (GET_CODE (reg) == SUBREG
9596                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9597               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
9598                   == CONST_INT)))
9599         {
9600           unsorted_reg_rtxs[i] = (GET_CODE (operands[i]) == REG
9601                                   ? operands[i] : SUBREG_REG (operands[i]));
9602           unsorted_regs[i] = REGNO (unsorted_reg_rtxs[i]);
9603
9604           if (i == 0)
9605             {
9606               base_reg = REGNO (reg);
9607               base_reg_rtx = reg;
9608               if (TARGET_THUMB1 && base_reg > LAST_LO_REGNUM)
9609                 return 0;
9610             }
9611           else if (base_reg != (int) REGNO (reg))
9612             /* Not addressed from the same base register.  */
9613             return 0;
9614
9615           /* If it isn't an integer register, then we can't do this.  */
9616           if (unsorted_regs[i] < 0
9617               || (TARGET_THUMB1 && unsorted_regs[i] > LAST_LO_REGNUM)
9618               || (TARGET_THUMB2 && unsorted_regs[i] == base_reg)
9619               || (TARGET_THUMB2 && unsorted_regs[i] == SP_REGNUM)
9620               || unsorted_regs[i] > 14)
9621             return 0;
9622
9623           unsorted_offsets[i] = INTVAL (offset);
9624           if (i == 0 || unsorted_offsets[i] < unsorted_offsets[order[0]])
9625             order[0] = i;
9626         }
9627       else
9628         /* Not a suitable memory address.  */
9629         return 0;
9630     }
9631
9632   /* All the useful information has now been extracted from the
9633      operands into unsorted_regs and unsorted_offsets; additionally,
9634      order[0] has been set to the lowest offset in the list.  Sort
9635      the offsets into order, verifying that they are adjacent, and
9636      check that the register numbers are ascending.  */
9637   if (!compute_offset_order (nops, unsorted_offsets, order,
9638                              check_regs ? unsorted_regs : NULL))
9639     return 0;
9640
9641   if (saved_order)
9642     memcpy (saved_order, order, sizeof order);
9643
9644   if (base)
9645     {
9646       *base = base_reg;
9647
9648       for (i = 0; i < nops; i++)
9649         {
9650           regs[i] = unsorted_regs[check_regs ? order[i] : i];
9651           if (reg_rtxs)
9652             reg_rtxs[i] = unsorted_reg_rtxs[check_regs ? order[i] : i];
9653         }
9654
9655       *load_offset = unsorted_offsets[order[0]];
9656     }
9657
9658   if (TARGET_THUMB1
9659       && !peep2_reg_dead_p (nops_total, base_reg_rtx))
9660     return 0;
9661
9662   if (unsorted_offsets[order[0]] == 0)
9663     stm_case = 1; /* stmia */
9664   else if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
9665     stm_case = 2; /* stmib */
9666   else if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
9667     stm_case = 3; /* stmda */
9668   else if (TARGET_32BIT && unsorted_offsets[order[nops - 1]] == -4)
9669     stm_case = 4; /* stmdb */
9670   else
9671     return 0;
9672
9673   if (!multiple_operation_profitable_p (false, nops, 0))
9674     return 0;
9675
9676   return stm_case;
9677 }
9678 \f
9679 /* Routines for use in generating RTL.  */
9680
9681 /* Generate a load-multiple instruction.  COUNT is the number of loads in
9682    the instruction; REGS and MEMS are arrays containing the operands.
9683    BASEREG is the base register to be used in addressing the memory operands.
9684    WBACK_OFFSET is nonzero if the instruction should update the base
9685    register.  */
9686
9687 static rtx
9688 arm_gen_load_multiple_1 (int count, int *regs, rtx *mems, rtx basereg,
9689                          HOST_WIDE_INT wback_offset)
9690 {
9691   int i = 0, j;
9692   rtx result;
9693
9694   if (!multiple_operation_profitable_p (false, count, 0))
9695     {
9696       rtx seq;
9697
9698       start_sequence ();
9699
9700       for (i = 0; i < count; i++)
9701         emit_move_insn (gen_rtx_REG (SImode, regs[i]), mems[i]);
9702
9703       if (wback_offset != 0)
9704         emit_move_insn (basereg, plus_constant (basereg, wback_offset));
9705
9706       seq = get_insns ();
9707       end_sequence ();
9708
9709       return seq;
9710     }
9711
9712   result = gen_rtx_PARALLEL (VOIDmode,
9713                              rtvec_alloc (count + (wback_offset != 0 ? 1 : 0)));
9714   if (wback_offset != 0)
9715     {
9716       XVECEXP (result, 0, 0)
9717         = gen_rtx_SET (VOIDmode, basereg,
9718                        plus_constant (basereg, wback_offset));
9719       i = 1;
9720       count++;
9721     }
9722
9723   for (j = 0; i < count; i++, j++)
9724     XVECEXP (result, 0, i)
9725       = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, regs[j]), mems[j]);
9726
9727   return result;
9728 }
9729
9730 /* Generate a store-multiple instruction.  COUNT is the number of stores in
9731    the instruction; REGS and MEMS are arrays containing the operands.
9732    BASEREG is the base register to be used in addressing the memory operands.
9733    WBACK_OFFSET is nonzero if the instruction should update the base
9734    register.  */
9735
9736 static rtx
9737 arm_gen_store_multiple_1 (int count, int *regs, rtx *mems, rtx basereg,
9738                           HOST_WIDE_INT wback_offset)
9739 {
9740   int i = 0, j;
9741   rtx result;
9742
9743   if (GET_CODE (basereg) == PLUS)
9744     basereg = XEXP (basereg, 0);
9745
9746   if (!multiple_operation_profitable_p (false, count, 0))
9747     {
9748       rtx seq;
9749
9750       start_sequence ();
9751
9752       for (i = 0; i < count; i++)
9753         emit_move_insn (mems[i], gen_rtx_REG (SImode, regs[i]));
9754
9755       if (wback_offset != 0)
9756         emit_move_insn (basereg, plus_constant (basereg, wback_offset));
9757
9758       seq = get_insns ();
9759       end_sequence ();
9760
9761       return seq;
9762     }
9763
9764   result = gen_rtx_PARALLEL (VOIDmode,
9765                              rtvec_alloc (count + (wback_offset != 0 ? 1 : 0)));
9766   if (wback_offset != 0)
9767     {
9768       XVECEXP (result, 0, 0)
9769         = gen_rtx_SET (VOIDmode, basereg,
9770                        plus_constant (basereg, wback_offset));
9771       i = 1;
9772       count++;
9773     }
9774
9775   for (j = 0; i < count; i++, j++)
9776     XVECEXP (result, 0, i)
9777       = gen_rtx_SET (VOIDmode, mems[j], gen_rtx_REG (SImode, regs[j]));
9778
9779   return result;
9780 }
9781
9782 /* Generate either a load-multiple or a store-multiple instruction.  This
9783    function can be used in situations where we can start with a single MEM
9784    rtx and adjust its address upwards.
9785    COUNT is the number of operations in the instruction, not counting a
9786    possible update of the base register.  REGS is an array containing the
9787    register operands.
9788    BASEREG is the base register to be used in addressing the memory operands,
9789    which are constructed from BASEMEM.
9790    WRITE_BACK specifies whether the generated instruction should include an
9791    update of the base register.
9792    OFFSETP is used to pass an offset to and from this function; this offset
9793    is not used when constructing the address (instead BASEMEM should have an
9794    appropriate offset in its address), it is used only for setting
9795    MEM_OFFSET.  It is updated only if WRITE_BACK is true.*/
9796
9797 static rtx
9798 arm_gen_multiple_op (bool is_load, int *regs, int count, rtx basereg,
9799                      bool write_back, rtx basemem, HOST_WIDE_INT *offsetp)
9800 {
9801   rtx mems[MAX_LDM_STM_OPS];
9802   HOST_WIDE_INT offset = *offsetp;
9803   int i;
9804
9805   gcc_assert (count <= MAX_LDM_STM_OPS);
9806
9807   if (GET_CODE (basereg) == PLUS)
9808     basereg = XEXP (basereg, 0);
9809
9810   for (i = 0; i < count; i++)
9811     {
9812       rtx addr = plus_constant (basereg, i * 4);
9813       mems[i] = adjust_automodify_address_nv (basemem, SImode, addr, offset);
9814       offset += 4;
9815     }
9816
9817   if (write_back)
9818     *offsetp = offset;
9819
9820   if (is_load)
9821     return arm_gen_load_multiple_1 (count, regs, mems, basereg,
9822                                     write_back ? 4 * count : 0);
9823   else
9824     return arm_gen_store_multiple_1 (count, regs, mems, basereg,
9825                                      write_back ? 4 * count : 0);
9826 }
9827
9828 rtx
9829 arm_gen_load_multiple (int *regs, int count, rtx basereg, int write_back,
9830                        rtx basemem, HOST_WIDE_INT *offsetp)
9831 {
9832   return arm_gen_multiple_op (TRUE, regs, count, basereg, write_back, basemem,
9833                               offsetp);
9834 }
9835
9836 rtx
9837 arm_gen_store_multiple (int *regs, int count, rtx basereg, int write_back,
9838                         rtx basemem, HOST_WIDE_INT *offsetp)
9839 {
9840   return arm_gen_multiple_op (FALSE, regs, count, basereg, write_back, basemem,
9841                               offsetp);
9842 }
9843
9844 /* Called from a peephole2 expander to turn a sequence of loads into an
9845    LDM instruction.  OPERANDS are the operands found by the peephole matcher;
9846    NOPS indicates how many separate loads we are trying to combine.  SORT_REGS
9847    is true if we can reorder the registers because they are used commutatively
9848    subsequently.
9849    Returns true iff we could generate a new instruction.  */
9850
9851 bool
9852 gen_ldm_seq (rtx *operands, int nops, bool sort_regs)
9853 {
9854   int regs[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
9855   rtx mems[MAX_LDM_STM_OPS];
9856   int i, j, base_reg;
9857   rtx base_reg_rtx;
9858   HOST_WIDE_INT offset;
9859   int write_back = FALSE;
9860   int ldm_case;
9861   rtx addr;
9862
9863   ldm_case = load_multiple_sequence (operands, nops, regs, mem_order,
9864                                      &base_reg, &offset, !sort_regs);
9865
9866   if (ldm_case == 0)
9867     return false;
9868
9869   if (sort_regs)
9870     for (i = 0; i < nops - 1; i++)
9871       for (j = i + 1; j < nops; j++)
9872         if (regs[i] > regs[j])
9873           {
9874             int t = regs[i];
9875             regs[i] = regs[j];
9876             regs[j] = t;
9877           }
9878   base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
9879
9880   if (TARGET_THUMB1)
9881     {
9882       gcc_assert (peep2_reg_dead_p (nops, base_reg_rtx));
9883       gcc_assert (ldm_case == 1 || ldm_case == 5);
9884       write_back = TRUE;
9885     }
9886
9887   if (ldm_case == 5)
9888     {
9889       rtx newbase = TARGET_THUMB1 ? base_reg_rtx : gen_rtx_REG (SImode, regs[0]);
9890       emit_insn (gen_addsi3 (newbase, base_reg_rtx, GEN_INT (offset)));
9891       offset = 0;
9892       if (!TARGET_THUMB1)
9893         {
9894           base_reg = regs[0];
9895           base_reg_rtx = newbase;
9896         }
9897     }
9898
9899   for (i = 0; i < nops; i++)
9900     {
9901       addr = plus_constant (base_reg_rtx, offset + i * 4);
9902       mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
9903                                               SImode, addr, 0);
9904     }
9905   emit_insn (arm_gen_load_multiple_1 (nops, regs, mems, base_reg_rtx,
9906                                       write_back ? offset + i * 4 : 0));
9907   return true;
9908 }
9909
9910 /* Called from a peephole2 expander to turn a sequence of stores into an
9911    STM instruction.  OPERANDS are the operands found by the peephole matcher;
9912    NOPS indicates how many separate stores we are trying to combine.
9913    Returns true iff we could generate a new instruction.  */
9914
9915 bool
9916 gen_stm_seq (rtx *operands, int nops)
9917 {
9918   int i;
9919   int regs[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
9920   rtx mems[MAX_LDM_STM_OPS];
9921   int base_reg;
9922   rtx base_reg_rtx;
9923   HOST_WIDE_INT offset;
9924   int write_back = FALSE;
9925   int stm_case;
9926   rtx addr;
9927   bool base_reg_dies;
9928
9929   stm_case = store_multiple_sequence (operands, nops, nops, regs, NULL,
9930                                       mem_order, &base_reg, &offset, true);
9931
9932   if (stm_case == 0)
9933     return false;
9934
9935   base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
9936
9937   base_reg_dies = peep2_reg_dead_p (nops, base_reg_rtx);
9938   if (TARGET_THUMB1)
9939     {
9940       gcc_assert (base_reg_dies);
9941       write_back = TRUE;
9942     }
9943
9944   if (stm_case == 5)
9945     {
9946       gcc_assert (base_reg_dies);
9947       emit_insn (gen_addsi3 (base_reg_rtx, base_reg_rtx, GEN_INT (offset)));
9948       offset = 0;
9949     }
9950
9951   addr = plus_constant (base_reg_rtx, offset);
9952
9953   for (i = 0; i < nops; i++)
9954     {
9955       addr = plus_constant (base_reg_rtx, offset + i * 4);
9956       mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
9957                                               SImode, addr, 0);
9958     }
9959   emit_insn (arm_gen_store_multiple_1 (nops, regs, mems, base_reg_rtx,
9960                                        write_back ? offset + i * 4 : 0));
9961   return true;
9962 }
9963
9964 /* Called from a peephole2 expander to turn a sequence of stores that are
9965    preceded by constant loads into an STM instruction.  OPERANDS are the
9966    operands found by the peephole matcher; NOPS indicates how many
9967    separate stores we are trying to combine; there are 2 * NOPS
9968    instructions in the peephole.
9969    Returns true iff we could generate a new instruction.  */
9970
9971 bool
9972 gen_const_stm_seq (rtx *operands, int nops)
9973 {
9974   int regs[MAX_LDM_STM_OPS], sorted_regs[MAX_LDM_STM_OPS];
9975   int reg_order[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
9976   rtx reg_rtxs[MAX_LDM_STM_OPS], orig_reg_rtxs[MAX_LDM_STM_OPS];
9977   rtx mems[MAX_LDM_STM_OPS];
9978   int base_reg;
9979   rtx base_reg_rtx;
9980   HOST_WIDE_INT offset;
9981   int write_back = FALSE;
9982   int stm_case;
9983   rtx addr;
9984   bool base_reg_dies;
9985   int i, j;
9986   HARD_REG_SET allocated;
9987
9988   stm_case = store_multiple_sequence (operands, nops, 2 * nops, regs, reg_rtxs,
9989                                       mem_order, &base_reg, &offset, false);
9990
9991   if (stm_case == 0)
9992     return false;
9993
9994   memcpy (orig_reg_rtxs, reg_rtxs, sizeof orig_reg_rtxs);
9995
9996   /* If the same register is used more than once, try to find a free
9997      register.  */
9998   CLEAR_HARD_REG_SET (allocated);
9999   for (i = 0; i < nops; i++)
10000     {
10001       for (j = i + 1; j < nops; j++)
10002         if (regs[i] == regs[j])
10003           {
10004             rtx t = peep2_find_free_register (0, nops * 2,
10005                                               TARGET_THUMB1 ? "l" : "r",
10006                                               SImode, &allocated);
10007             if (t == NULL_RTX)
10008               return false;
10009             reg_rtxs[i] = t;
10010             regs[i] = REGNO (t);
10011           }
10012     }
10013
10014   /* Compute an ordering that maps the register numbers to an ascending
10015      sequence.  */
10016   reg_order[0] = 0;
10017   for (i = 0; i < nops; i++)
10018     if (regs[i] < regs[reg_order[0]])
10019       reg_order[0] = i;
10020
10021   for (i = 1; i < nops; i++)
10022     {
10023       int this_order = reg_order[i - 1];
10024       for (j = 0; j < nops; j++)
10025         if (regs[j] > regs[reg_order[i - 1]]
10026             && (this_order == reg_order[i - 1]
10027                 || regs[j] < regs[this_order]))
10028           this_order = j;
10029       reg_order[i] = this_order;
10030     }
10031
10032   /* Ensure that registers that must be live after the instruction end
10033      up with the correct value.  */
10034   for (i = 0; i < nops; i++)
10035     {
10036       int this_order = reg_order[i];
10037       if ((this_order != mem_order[i]
10038            || orig_reg_rtxs[this_order] != reg_rtxs[this_order])
10039           && !peep2_reg_dead_p (nops * 2, orig_reg_rtxs[this_order]))
10040         return false;
10041     }
10042
10043   /* Load the constants.  */
10044   for (i = 0; i < nops; i++)
10045     {
10046       rtx op = operands[2 * nops + mem_order[i]];
10047       sorted_regs[i] = regs[reg_order[i]];
10048       emit_move_insn (reg_rtxs[reg_order[i]], op);
10049     }
10050
10051   base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
10052
10053   base_reg_dies = peep2_reg_dead_p (nops * 2, base_reg_rtx);
10054   if (TARGET_THUMB1)
10055     {
10056       gcc_assert (base_reg_dies);
10057       write_back = TRUE;
10058     }
10059
10060   if (stm_case == 5)
10061     {
10062       gcc_assert (base_reg_dies);
10063       emit_insn (gen_addsi3 (base_reg_rtx, base_reg_rtx, GEN_INT (offset)));
10064       offset = 0;
10065     }
10066
10067   addr = plus_constant (base_reg_rtx, offset);
10068
10069   for (i = 0; i < nops; i++)
10070     {
10071       addr = plus_constant (base_reg_rtx, offset + i * 4);
10072       mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
10073                                               SImode, addr, 0);
10074     }
10075   emit_insn (arm_gen_store_multiple_1 (nops, sorted_regs, mems, base_reg_rtx,
10076                                        write_back ? offset + i * 4 : 0));
10077   return true;
10078 }
10079
10080 int
10081 arm_gen_movmemqi (rtx *operands)
10082 {
10083   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
10084   HOST_WIDE_INT srcoffset, dstoffset;
10085   int i;
10086   rtx src, dst, srcbase, dstbase;
10087   rtx part_bytes_reg = NULL;
10088   rtx mem;
10089
10090   if (GET_CODE (operands[2]) != CONST_INT
10091       || GET_CODE (operands[3]) != CONST_INT
10092       || INTVAL (operands[2]) > 64
10093       || INTVAL (operands[3]) & 3)
10094     return 0;
10095
10096   dstbase = operands[0];
10097   srcbase = operands[1];
10098
10099   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
10100   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
10101
10102   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
10103   out_words_to_go = INTVAL (operands[2]) / 4;
10104   last_bytes = INTVAL (operands[2]) & 3;
10105   dstoffset = srcoffset = 0;
10106
10107   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
10108     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
10109
10110   for (i = 0; in_words_to_go >= 2; i+=4)
10111     {
10112       if (in_words_to_go > 4)
10113         emit_insn (arm_gen_load_multiple (arm_regs_in_sequence, 4, src,
10114                                           TRUE, srcbase, &srcoffset));
10115       else
10116         emit_insn (arm_gen_load_multiple (arm_regs_in_sequence, in_words_to_go,
10117                                           src, FALSE, srcbase,
10118                                           &srcoffset));
10119
10120       if (out_words_to_go)
10121         {
10122           if (out_words_to_go > 4)
10123             emit_insn (arm_gen_store_multiple (arm_regs_in_sequence, 4, dst,
10124                                                TRUE, dstbase, &dstoffset));
10125           else if (out_words_to_go != 1)
10126             emit_insn (arm_gen_store_multiple (arm_regs_in_sequence,
10127                                                out_words_to_go, dst,
10128                                                (last_bytes == 0
10129                                                 ? FALSE : TRUE),
10130                                                dstbase, &dstoffset));
10131           else
10132             {
10133               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
10134               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
10135               if (last_bytes != 0)
10136                 {
10137                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
10138                   dstoffset += 4;
10139                 }
10140             }
10141         }
10142
10143       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
10144       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
10145     }
10146
10147   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
10148   if (out_words_to_go)
10149     {
10150       rtx sreg;
10151
10152       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
10153       sreg = copy_to_reg (mem);
10154
10155       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
10156       emit_move_insn (mem, sreg);
10157       in_words_to_go--;
10158
10159       gcc_assert (!in_words_to_go);     /* Sanity check */
10160     }
10161
10162   if (in_words_to_go)
10163     {
10164       gcc_assert (in_words_to_go > 0);
10165
10166       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
10167       part_bytes_reg = copy_to_mode_reg (SImode, mem);
10168     }
10169
10170   gcc_assert (!last_bytes || part_bytes_reg);
10171
10172   if (BYTES_BIG_ENDIAN && last_bytes)
10173     {
10174       rtx tmp = gen_reg_rtx (SImode);
10175
10176       /* The bytes we want are in the top end of the word.  */
10177       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
10178                               GEN_INT (8 * (4 - last_bytes))));
10179       part_bytes_reg = tmp;
10180
10181       while (last_bytes)
10182         {
10183           mem = adjust_automodify_address (dstbase, QImode,
10184                                            plus_constant (dst, last_bytes - 1),
10185                                            dstoffset + last_bytes - 1);
10186           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
10187
10188           if (--last_bytes)
10189             {
10190               tmp = gen_reg_rtx (SImode);
10191               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
10192               part_bytes_reg = tmp;
10193             }
10194         }
10195
10196     }
10197   else
10198     {
10199       if (last_bytes > 1)
10200         {
10201           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
10202           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
10203           last_bytes -= 2;
10204           if (last_bytes)
10205             {
10206               rtx tmp = gen_reg_rtx (SImode);
10207               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
10208               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
10209               part_bytes_reg = tmp;
10210               dstoffset += 2;
10211             }
10212         }
10213
10214       if (last_bytes)
10215         {
10216           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
10217           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
10218         }
10219     }
10220
10221   return 1;
10222 }
10223
10224 /* Select a dominance comparison mode if possible for a test of the general
10225    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
10226    COND_OR == DOM_CC_X_AND_Y => (X && Y)
10227    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
10228    COND_OR == DOM_CC_X_OR_Y => (X || Y)
10229    In all cases OP will be either EQ or NE, but we don't need to know which
10230    here.  If we are unable to support a dominance comparison we return
10231    CC mode.  This will then fail to match for the RTL expressions that
10232    generate this call.  */
10233 enum machine_mode
10234 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
10235 {
10236   enum rtx_code cond1, cond2;
10237   int swapped = 0;
10238
10239   /* Currently we will probably get the wrong result if the individual
10240      comparisons are not simple.  This also ensures that it is safe to
10241      reverse a comparison if necessary.  */
10242   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
10243        != CCmode)
10244       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
10245           != CCmode))
10246     return CCmode;
10247
10248   /* The if_then_else variant of this tests the second condition if the
10249      first passes, but is true if the first fails.  Reverse the first
10250      condition to get a true "inclusive-or" expression.  */
10251   if (cond_or == DOM_CC_NX_OR_Y)
10252     cond1 = reverse_condition (cond1);
10253
10254   /* If the comparisons are not equal, and one doesn't dominate the other,
10255      then we can't do this.  */
10256   if (cond1 != cond2
10257       && !comparison_dominates_p (cond1, cond2)
10258       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
10259     return CCmode;
10260
10261   if (swapped)
10262     {
10263       enum rtx_code temp = cond1;
10264       cond1 = cond2;
10265       cond2 = temp;
10266     }
10267
10268   switch (cond1)
10269     {
10270     case EQ:
10271       if (cond_or == DOM_CC_X_AND_Y)
10272         return CC_DEQmode;
10273
10274       switch (cond2)
10275         {
10276         case EQ: return CC_DEQmode;
10277         case LE: return CC_DLEmode;
10278         case LEU: return CC_DLEUmode;
10279         case GE: return CC_DGEmode;
10280         case GEU: return CC_DGEUmode;
10281         default: gcc_unreachable ();
10282         }
10283
10284     case LT:
10285       if (cond_or == DOM_CC_X_AND_Y)
10286         return CC_DLTmode;
10287
10288       switch (cond2)
10289         {
10290         case  LT:
10291             return CC_DLTmode;
10292         case LE:
10293           return CC_DLEmode;
10294         case NE:
10295           return CC_DNEmode;
10296         default:
10297           gcc_unreachable ();
10298         }
10299
10300     case GT:
10301       if (cond_or == DOM_CC_X_AND_Y)
10302         return CC_DGTmode;
10303
10304       switch (cond2)
10305         {
10306         case GT:
10307           return CC_DGTmode;
10308         case GE:
10309           return CC_DGEmode;
10310         case NE:
10311           return CC_DNEmode;
10312         default:
10313           gcc_unreachable ();
10314         }
10315
10316     case LTU:
10317       if (cond_or == DOM_CC_X_AND_Y)
10318         return CC_DLTUmode;
10319
10320       switch (cond2)
10321         {
10322         case LTU:
10323           return CC_DLTUmode;
10324         case LEU:
10325           return CC_DLEUmode;
10326         case NE:
10327           return CC_DNEmode;
10328         default:
10329           gcc_unreachable ();
10330         }
10331
10332     case GTU:
10333       if (cond_or == DOM_CC_X_AND_Y)
10334         return CC_DGTUmode;
10335
10336       switch (cond2)
10337         {
10338         case GTU:
10339           return CC_DGTUmode;
10340         case GEU:
10341           return CC_DGEUmode;
10342         case NE:
10343           return CC_DNEmode;
10344         default:
10345           gcc_unreachable ();
10346         }
10347
10348     /* The remaining cases only occur when both comparisons are the
10349        same.  */
10350     case NE:
10351       gcc_assert (cond1 == cond2);
10352       return CC_DNEmode;
10353
10354     case LE:
10355       gcc_assert (cond1 == cond2);
10356       return CC_DLEmode;
10357
10358     case GE:
10359       gcc_assert (cond1 == cond2);
10360       return CC_DGEmode;
10361
10362     case LEU:
10363       gcc_assert (cond1 == cond2);
10364       return CC_DLEUmode;
10365
10366     case GEU:
10367       gcc_assert (cond1 == cond2);
10368       return CC_DGEUmode;
10369
10370     default:
10371       gcc_unreachable ();
10372     }
10373 }
10374
10375 enum machine_mode
10376 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
10377 {
10378   /* All floating point compares return CCFP if it is an equality
10379      comparison, and CCFPE otherwise.  */
10380   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
10381     {
10382       switch (op)
10383         {
10384         case EQ:
10385         case NE:
10386         case UNORDERED:
10387         case ORDERED:
10388         case UNLT:
10389         case UNLE:
10390         case UNGT:
10391         case UNGE:
10392         case UNEQ:
10393         case LTGT:
10394           return CCFPmode;
10395
10396         case LT:
10397         case LE:
10398         case GT:
10399         case GE:
10400           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
10401             return CCFPmode;
10402           return CCFPEmode;
10403
10404         default:
10405           gcc_unreachable ();
10406         }
10407     }
10408
10409   /* A compare with a shifted operand.  Because of canonicalization, the
10410      comparison will have to be swapped when we emit the assembler.  */
10411   if (GET_MODE (y) == SImode 
10412       && (REG_P (y) || (GET_CODE (y) == SUBREG))
10413       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
10414           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
10415           || GET_CODE (x) == ROTATERT))
10416     return CC_SWPmode;
10417
10418   /* This operation is performed swapped, but since we only rely on the Z
10419      flag we don't need an additional mode.  */
10420   if (GET_MODE (y) == SImode 
10421       && (REG_P (y) || (GET_CODE (y) == SUBREG))
10422       && GET_CODE (x) == NEG
10423       && (op == EQ || op == NE))
10424     return CC_Zmode;
10425
10426   /* This is a special case that is used by combine to allow a
10427      comparison of a shifted byte load to be split into a zero-extend
10428      followed by a comparison of the shifted integer (only valid for
10429      equalities and unsigned inequalities).  */
10430   if (GET_MODE (x) == SImode
10431       && GET_CODE (x) == ASHIFT
10432       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
10433       && GET_CODE (XEXP (x, 0)) == SUBREG
10434       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
10435       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
10436       && (op == EQ || op == NE
10437           || op == GEU || op == GTU || op == LTU || op == LEU)
10438       && GET_CODE (y) == CONST_INT)
10439     return CC_Zmode;
10440
10441   /* A construct for a conditional compare, if the false arm contains
10442      0, then both conditions must be true, otherwise either condition
10443      must be true.  Not all conditions are possible, so CCmode is
10444      returned if it can't be done.  */
10445   if (GET_CODE (x) == IF_THEN_ELSE
10446       && (XEXP (x, 2) == const0_rtx
10447           || XEXP (x, 2) == const1_rtx)
10448       && COMPARISON_P (XEXP (x, 0))
10449       && COMPARISON_P (XEXP (x, 1)))
10450     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
10451                                          INTVAL (XEXP (x, 2)));
10452
10453   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
10454   if (GET_CODE (x) == AND
10455       && COMPARISON_P (XEXP (x, 0))
10456       && COMPARISON_P (XEXP (x, 1)))
10457     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
10458                                          DOM_CC_X_AND_Y);
10459
10460   if (GET_CODE (x) == IOR
10461       && COMPARISON_P (XEXP (x, 0))
10462       && COMPARISON_P (XEXP (x, 1)))
10463     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
10464                                          DOM_CC_X_OR_Y);
10465
10466   /* An operation (on Thumb) where we want to test for a single bit.
10467      This is done by shifting that bit up into the top bit of a
10468      scratch register; we can then branch on the sign bit.  */
10469   if (TARGET_THUMB1
10470       && GET_MODE (x) == SImode
10471       && (op == EQ || op == NE)
10472       && GET_CODE (x) == ZERO_EXTRACT
10473       && XEXP (x, 1) == const1_rtx)
10474     return CC_Nmode;
10475
10476   /* An operation that sets the condition codes as a side-effect, the
10477      V flag is not set correctly, so we can only use comparisons where
10478      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
10479      instead.)  */
10480   /* ??? Does the ZERO_EXTRACT case really apply to thumb2?  */
10481   if (GET_MODE (x) == SImode
10482       && y == const0_rtx
10483       && (op == EQ || op == NE || op == LT || op == GE)
10484       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
10485           || GET_CODE (x) == AND || GET_CODE (x) == IOR
10486           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
10487           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
10488           || GET_CODE (x) == LSHIFTRT
10489           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
10490           || GET_CODE (x) == ROTATERT
10491           || (TARGET_32BIT && GET_CODE (x) == ZERO_EXTRACT)))
10492     return CC_NOOVmode;
10493
10494   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
10495     return CC_Zmode;
10496
10497   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
10498       && GET_CODE (x) == PLUS
10499       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
10500     return CC_Cmode;
10501
10502   if (GET_MODE (x) == DImode || GET_MODE (y) == DImode)
10503     {
10504       /* To keep things simple, always use the Cirrus cfcmp64 if it is
10505          available.  */
10506       if (TARGET_ARM && TARGET_HARD_FLOAT && TARGET_MAVERICK)
10507         return CCmode;
10508
10509       switch (op)
10510         {
10511         case EQ:
10512         case NE:
10513           /* A DImode comparison against zero can be implemented by
10514              or'ing the two halves together.  */
10515           if (y == const0_rtx)
10516             return CC_Zmode;
10517
10518           /* We can do an equality test in three Thumb instructions.  */
10519           if (!TARGET_ARM)
10520             return CC_Zmode;
10521
10522           /* FALLTHROUGH */
10523
10524         case LTU:
10525         case LEU:
10526         case GTU:
10527         case GEU:
10528           /* DImode unsigned comparisons can be implemented by cmp +
10529              cmpeq without a scratch register.  Not worth doing in
10530              Thumb-2.  */
10531           if (TARGET_ARM)
10532             return CC_CZmode;
10533
10534           /* FALLTHROUGH */
10535
10536         case LT:
10537         case LE:
10538         case GT:
10539         case GE:
10540           /* DImode signed and unsigned comparisons can be implemented
10541              by cmp + sbcs with a scratch register, but that does not
10542              set the Z flag - we must reverse GT/LE/GTU/LEU.  */
10543           gcc_assert (op != EQ && op != NE);
10544           return CC_NCVmode;
10545
10546         default:
10547           gcc_unreachable ();
10548         }
10549     }
10550
10551   return CCmode;
10552 }
10553
10554 /* X and Y are two things to compare using CODE.  Emit the compare insn and
10555    return the rtx for register 0 in the proper mode.  FP means this is a
10556    floating point compare: I don't think that it is needed on the arm.  */
10557 rtx
10558 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
10559 {
10560   enum machine_mode mode;
10561   rtx cc_reg;
10562   int dimode_comparison = GET_MODE (x) == DImode || GET_MODE (y) == DImode;
10563
10564   /* We might have X as a constant, Y as a register because of the predicates
10565      used for cmpdi.  If so, force X to a register here.  */
10566   if (dimode_comparison && !REG_P (x))
10567     x = force_reg (DImode, x);
10568
10569   mode = SELECT_CC_MODE (code, x, y);
10570   cc_reg = gen_rtx_REG (mode, CC_REGNUM);
10571
10572   if (dimode_comparison
10573       && !(TARGET_HARD_FLOAT && TARGET_MAVERICK)
10574       && mode != CC_CZmode)
10575     {
10576       rtx clobber, set;
10577
10578       /* To compare two non-zero values for equality, XOR them and
10579          then compare against zero.  Not used for ARM mode; there
10580          CC_CZmode is cheaper.  */
10581       if (mode == CC_Zmode && y != const0_rtx)
10582         {
10583           x = expand_binop (DImode, xor_optab, x, y, NULL_RTX, 0, OPTAB_WIDEN);
10584           y = const0_rtx;
10585         }
10586       /* A scratch register is required.  */
10587       clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (SImode));
10588       set = gen_rtx_SET (VOIDmode, cc_reg, gen_rtx_COMPARE (mode, x, y));
10589       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
10590     }
10591   else
10592     emit_set_insn (cc_reg, gen_rtx_COMPARE (mode, x, y));
10593
10594   return cc_reg;
10595 }
10596
10597 /* Generate a sequence of insns that will generate the correct return
10598    address mask depending on the physical architecture that the program
10599    is running on.  */
10600 rtx
10601 arm_gen_return_addr_mask (void)
10602 {
10603   rtx reg = gen_reg_rtx (Pmode);
10604
10605   emit_insn (gen_return_addr_mask (reg));
10606   return reg;
10607 }
10608
10609 void
10610 arm_reload_in_hi (rtx *operands)
10611 {
10612   rtx ref = operands[1];
10613   rtx base, scratch;
10614   HOST_WIDE_INT offset = 0;
10615
10616   if (GET_CODE (ref) == SUBREG)
10617     {
10618       offset = SUBREG_BYTE (ref);
10619       ref = SUBREG_REG (ref);
10620     }
10621
10622   if (GET_CODE (ref) == REG)
10623     {
10624       /* We have a pseudo which has been spilt onto the stack; there
10625          are two cases here: the first where there is a simple
10626          stack-slot replacement and a second where the stack-slot is
10627          out of range, or is used as a subreg.  */
10628       if (reg_equiv_mem[REGNO (ref)])
10629         {
10630           ref = reg_equiv_mem[REGNO (ref)];
10631           base = find_replacement (&XEXP (ref, 0));
10632         }
10633       else
10634         /* The slot is out of range, or was dressed up in a SUBREG.  */
10635         base = reg_equiv_address[REGNO (ref)];
10636     }
10637   else
10638     base = find_replacement (&XEXP (ref, 0));
10639
10640   /* Handle the case where the address is too complex to be offset by 1.  */
10641   if (GET_CODE (base) == MINUS
10642       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
10643     {
10644       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10645
10646       emit_set_insn (base_plus, base);
10647       base = base_plus;
10648     }
10649   else if (GET_CODE (base) == PLUS)
10650     {
10651       /* The addend must be CONST_INT, or we would have dealt with it above.  */
10652       HOST_WIDE_INT hi, lo;
10653
10654       offset += INTVAL (XEXP (base, 1));
10655       base = XEXP (base, 0);
10656
10657       /* Rework the address into a legal sequence of insns.  */
10658       /* Valid range for lo is -4095 -> 4095 */
10659       lo = (offset >= 0
10660             ? (offset & 0xfff)
10661             : -((-offset) & 0xfff));
10662
10663       /* Corner case, if lo is the max offset then we would be out of range
10664          once we have added the additional 1 below, so bump the msb into the
10665          pre-loading insn(s).  */
10666       if (lo == 4095)
10667         lo &= 0x7ff;
10668
10669       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
10670              ^ (HOST_WIDE_INT) 0x80000000)
10671             - (HOST_WIDE_INT) 0x80000000);
10672
10673       gcc_assert (hi + lo == offset);
10674
10675       if (hi != 0)
10676         {
10677           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10678
10679           /* Get the base address; addsi3 knows how to handle constants
10680              that require more than one insn.  */
10681           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
10682           base = base_plus;
10683           offset = lo;
10684         }
10685     }
10686
10687   /* Operands[2] may overlap operands[0] (though it won't overlap
10688      operands[1]), that's why we asked for a DImode reg -- so we can
10689      use the bit that does not overlap.  */
10690   if (REGNO (operands[2]) == REGNO (operands[0]))
10691     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10692   else
10693     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
10694
10695   emit_insn (gen_zero_extendqisi2 (scratch,
10696                                    gen_rtx_MEM (QImode,
10697                                                 plus_constant (base,
10698                                                                offset))));
10699   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
10700                                    gen_rtx_MEM (QImode,
10701                                                 plus_constant (base,
10702                                                                offset + 1))));
10703   if (!BYTES_BIG_ENDIAN)
10704     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
10705                    gen_rtx_IOR (SImode,
10706                                 gen_rtx_ASHIFT
10707                                 (SImode,
10708                                  gen_rtx_SUBREG (SImode, operands[0], 0),
10709                                  GEN_INT (8)),
10710                                 scratch));
10711   else
10712     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
10713                    gen_rtx_IOR (SImode,
10714                                 gen_rtx_ASHIFT (SImode, scratch,
10715                                                 GEN_INT (8)),
10716                                 gen_rtx_SUBREG (SImode, operands[0], 0)));
10717 }
10718
10719 /* Handle storing a half-word to memory during reload by synthesizing as two
10720    byte stores.  Take care not to clobber the input values until after we
10721    have moved them somewhere safe.  This code assumes that if the DImode
10722    scratch in operands[2] overlaps either the input value or output address
10723    in some way, then that value must die in this insn (we absolutely need
10724    two scratch registers for some corner cases).  */
10725 void
10726 arm_reload_out_hi (rtx *operands)
10727 {
10728   rtx ref = operands[0];
10729   rtx outval = operands[1];
10730   rtx base, scratch;
10731   HOST_WIDE_INT offset = 0;
10732
10733   if (GET_CODE (ref) == SUBREG)
10734     {
10735       offset = SUBREG_BYTE (ref);
10736       ref = SUBREG_REG (ref);
10737     }
10738
10739   if (GET_CODE (ref) == REG)
10740     {
10741       /* We have a pseudo which has been spilt onto the stack; there
10742          are two cases here: the first where there is a simple
10743          stack-slot replacement and a second where the stack-slot is
10744          out of range, or is used as a subreg.  */
10745       if (reg_equiv_mem[REGNO (ref)])
10746         {
10747           ref = reg_equiv_mem[REGNO (ref)];
10748           base = find_replacement (&XEXP (ref, 0));
10749         }
10750       else
10751         /* The slot is out of range, or was dressed up in a SUBREG.  */
10752         base = reg_equiv_address[REGNO (ref)];
10753     }
10754   else
10755     base = find_replacement (&XEXP (ref, 0));
10756
10757   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
10758
10759   /* Handle the case where the address is too complex to be offset by 1.  */
10760   if (GET_CODE (base) == MINUS
10761       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
10762     {
10763       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10764
10765       /* Be careful not to destroy OUTVAL.  */
10766       if (reg_overlap_mentioned_p (base_plus, outval))
10767         {
10768           /* Updating base_plus might destroy outval, see if we can
10769              swap the scratch and base_plus.  */
10770           if (!reg_overlap_mentioned_p (scratch, outval))
10771             {
10772               rtx tmp = scratch;
10773               scratch = base_plus;
10774               base_plus = tmp;
10775             }
10776           else
10777             {
10778               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
10779
10780               /* Be conservative and copy OUTVAL into the scratch now,
10781                  this should only be necessary if outval is a subreg
10782                  of something larger than a word.  */
10783               /* XXX Might this clobber base?  I can't see how it can,
10784                  since scratch is known to overlap with OUTVAL, and
10785                  must be wider than a word.  */
10786               emit_insn (gen_movhi (scratch_hi, outval));
10787               outval = scratch_hi;
10788             }
10789         }
10790
10791       emit_set_insn (base_plus, base);
10792       base = base_plus;
10793     }
10794   else if (GET_CODE (base) == PLUS)
10795     {
10796       /* The addend must be CONST_INT, or we would have dealt with it above.  */
10797       HOST_WIDE_INT hi, lo;
10798
10799       offset += INTVAL (XEXP (base, 1));
10800       base = XEXP (base, 0);
10801
10802       /* Rework the address into a legal sequence of insns.  */
10803       /* Valid range for lo is -4095 -> 4095 */
10804       lo = (offset >= 0
10805             ? (offset & 0xfff)
10806             : -((-offset) & 0xfff));
10807
10808       /* Corner case, if lo is the max offset then we would be out of range
10809          once we have added the additional 1 below, so bump the msb into the
10810          pre-loading insn(s).  */
10811       if (lo == 4095)
10812         lo &= 0x7ff;
10813
10814       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
10815              ^ (HOST_WIDE_INT) 0x80000000)
10816             - (HOST_WIDE_INT) 0x80000000);
10817
10818       gcc_assert (hi + lo == offset);
10819
10820       if (hi != 0)
10821         {
10822           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10823
10824           /* Be careful not to destroy OUTVAL.  */
10825           if (reg_overlap_mentioned_p (base_plus, outval))
10826             {
10827               /* Updating base_plus might destroy outval, see if we
10828                  can swap the scratch and base_plus.  */
10829               if (!reg_overlap_mentioned_p (scratch, outval))
10830                 {
10831                   rtx tmp = scratch;
10832                   scratch = base_plus;
10833                   base_plus = tmp;
10834                 }
10835               else
10836                 {
10837                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
10838
10839                   /* Be conservative and copy outval into scratch now,
10840                      this should only be necessary if outval is a
10841                      subreg of something larger than a word.  */
10842                   /* XXX Might this clobber base?  I can't see how it
10843                      can, since scratch is known to overlap with
10844                      outval.  */
10845                   emit_insn (gen_movhi (scratch_hi, outval));
10846                   outval = scratch_hi;
10847                 }
10848             }
10849
10850           /* Get the base address; addsi3 knows how to handle constants
10851              that require more than one insn.  */
10852           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
10853           base = base_plus;
10854           offset = lo;
10855         }
10856     }
10857
10858   if (BYTES_BIG_ENDIAN)
10859     {
10860       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
10861                                          plus_constant (base, offset + 1)),
10862                             gen_lowpart (QImode, outval)));
10863       emit_insn (gen_lshrsi3 (scratch,
10864                               gen_rtx_SUBREG (SImode, outval, 0),
10865                               GEN_INT (8)));
10866       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
10867                             gen_lowpart (QImode, scratch)));
10868     }
10869   else
10870     {
10871       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
10872                             gen_lowpart (QImode, outval)));
10873       emit_insn (gen_lshrsi3 (scratch,
10874                               gen_rtx_SUBREG (SImode, outval, 0),
10875                               GEN_INT (8)));
10876       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
10877                                          plus_constant (base, offset + 1)),
10878                             gen_lowpart (QImode, scratch)));
10879     }
10880 }
10881
10882 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
10883    (padded to the size of a word) should be passed in a register.  */
10884
10885 static bool
10886 arm_must_pass_in_stack (enum machine_mode mode, const_tree type)
10887 {
10888   if (TARGET_AAPCS_BASED)
10889     return must_pass_in_stack_var_size (mode, type);
10890   else
10891     return must_pass_in_stack_var_size_or_pad (mode, type);
10892 }
10893
10894
10895 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
10896    Return true if an argument passed on the stack should be padded upwards,
10897    i.e. if the least-significant byte has useful data.
10898    For legacy APCS ABIs we use the default.  For AAPCS based ABIs small
10899    aggregate types are placed in the lowest memory address.  */
10900
10901 bool
10902 arm_pad_arg_upward (enum machine_mode mode, const_tree type)
10903 {
10904   if (!TARGET_AAPCS_BASED)
10905     return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
10906
10907   if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
10908     return false;
10909
10910   return true;
10911 }
10912
10913
10914 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
10915    For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
10916    byte of the register has useful data, and return the opposite if the
10917    most significant byte does.
10918    For AAPCS, small aggregates and small complex types are always padded
10919    upwards.  */
10920
10921 bool
10922 arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
10923                     tree type, int first ATTRIBUTE_UNUSED)
10924 {
10925   if (TARGET_AAPCS_BASED
10926       && BYTES_BIG_ENDIAN
10927       && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10928       && int_size_in_bytes (type) <= 4)
10929     return true;
10930
10931   /* Otherwise, use default padding.  */
10932   return !BYTES_BIG_ENDIAN;
10933 }
10934
10935 \f
10936 /* Print a symbolic form of X to the debug file, F.  */
10937 static void
10938 arm_print_value (FILE *f, rtx x)
10939 {
10940   switch (GET_CODE (x))
10941     {
10942     case CONST_INT:
10943       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
10944       return;
10945
10946     case CONST_DOUBLE:
10947       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
10948       return;
10949
10950     case CONST_VECTOR:
10951       {
10952         int i;
10953
10954         fprintf (f, "<");
10955         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
10956           {
10957             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
10958             if (i < (CONST_VECTOR_NUNITS (x) - 1))
10959               fputc (',', f);
10960           }
10961         fprintf (f, ">");
10962       }
10963       return;
10964
10965     case CONST_STRING:
10966       fprintf (f, "\"%s\"", XSTR (x, 0));
10967       return;
10968
10969     case SYMBOL_REF:
10970       fprintf (f, "`%s'", XSTR (x, 0));
10971       return;
10972
10973     case LABEL_REF:
10974       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
10975       return;
10976
10977     case CONST:
10978       arm_print_value (f, XEXP (x, 0));
10979       return;
10980
10981     case PLUS:
10982       arm_print_value (f, XEXP (x, 0));
10983       fprintf (f, "+");
10984       arm_print_value (f, XEXP (x, 1));
10985       return;
10986
10987     case PC:
10988       fprintf (f, "pc");
10989       return;
10990
10991     default:
10992       fprintf (f, "????");
10993       return;
10994     }
10995 }
10996 \f
10997 /* Routines for manipulation of the constant pool.  */
10998
10999 /* Arm instructions cannot load a large constant directly into a
11000    register; they have to come from a pc relative load.  The constant
11001    must therefore be placed in the addressable range of the pc
11002    relative load.  Depending on the precise pc relative load
11003    instruction the range is somewhere between 256 bytes and 4k.  This
11004    means that we often have to dump a constant inside a function, and
11005    generate code to branch around it.
11006
11007    It is important to minimize this, since the branches will slow
11008    things down and make the code larger.
11009
11010    Normally we can hide the table after an existing unconditional
11011    branch so that there is no interruption of the flow, but in the
11012    worst case the code looks like this:
11013
11014         ldr     rn, L1
11015         ...
11016         b       L2
11017         align
11018         L1:     .long value
11019         L2:
11020         ...
11021
11022         ldr     rn, L3
11023         ...
11024         b       L4
11025         align
11026         L3:     .long value
11027         L4:
11028         ...
11029
11030    We fix this by performing a scan after scheduling, which notices
11031    which instructions need to have their operands fetched from the
11032    constant table and builds the table.
11033
11034    The algorithm starts by building a table of all the constants that
11035    need fixing up and all the natural barriers in the function (places
11036    where a constant table can be dropped without breaking the flow).
11037    For each fixup we note how far the pc-relative replacement will be
11038    able to reach and the offset of the instruction into the function.
11039
11040    Having built the table we then group the fixes together to form
11041    tables that are as large as possible (subject to addressing
11042    constraints) and emit each table of constants after the last
11043    barrier that is within range of all the instructions in the group.
11044    If a group does not contain a barrier, then we forcibly create one
11045    by inserting a jump instruction into the flow.  Once the table has
11046    been inserted, the insns are then modified to reference the
11047    relevant entry in the pool.
11048
11049    Possible enhancements to the algorithm (not implemented) are:
11050
11051    1) For some processors and object formats, there may be benefit in
11052    aligning the pools to the start of cache lines; this alignment
11053    would need to be taken into account when calculating addressability
11054    of a pool.  */
11055
11056 /* These typedefs are located at the start of this file, so that
11057    they can be used in the prototypes there.  This comment is to
11058    remind readers of that fact so that the following structures
11059    can be understood more easily.
11060
11061      typedef struct minipool_node    Mnode;
11062      typedef struct minipool_fixup   Mfix;  */
11063
11064 struct minipool_node
11065 {
11066   /* Doubly linked chain of entries.  */
11067   Mnode * next;
11068   Mnode * prev;
11069   /* The maximum offset into the code that this entry can be placed.  While
11070      pushing fixes for forward references, all entries are sorted in order
11071      of increasing max_address.  */
11072   HOST_WIDE_INT max_address;
11073   /* Similarly for an entry inserted for a backwards ref.  */
11074   HOST_WIDE_INT min_address;
11075   /* The number of fixes referencing this entry.  This can become zero
11076      if we "unpush" an entry.  In this case we ignore the entry when we
11077      come to emit the code.  */
11078   int refcount;
11079   /* The offset from the start of the minipool.  */
11080   HOST_WIDE_INT offset;
11081   /* The value in table.  */
11082   rtx value;
11083   /* The mode of value.  */
11084   enum machine_mode mode;
11085   /* The size of the value.  With iWMMXt enabled
11086      sizes > 4 also imply an alignment of 8-bytes.  */
11087   int fix_size;
11088 };
11089
11090 struct minipool_fixup
11091 {
11092   Mfix *            next;
11093   rtx               insn;
11094   HOST_WIDE_INT     address;
11095   rtx *             loc;
11096   enum machine_mode mode;
11097   int               fix_size;
11098   rtx               value;
11099   Mnode *           minipool;
11100   HOST_WIDE_INT     forwards;
11101   HOST_WIDE_INT     backwards;
11102 };
11103
11104 /* Fixes less than a word need padding out to a word boundary.  */
11105 #define MINIPOOL_FIX_SIZE(mode) \
11106   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
11107
11108 static Mnode *  minipool_vector_head;
11109 static Mnode *  minipool_vector_tail;
11110 static rtx      minipool_vector_label;
11111 static int      minipool_pad;
11112
11113 /* The linked list of all minipool fixes required for this function.  */
11114 Mfix *          minipool_fix_head;
11115 Mfix *          minipool_fix_tail;
11116 /* The fix entry for the current minipool, once it has been placed.  */
11117 Mfix *          minipool_barrier;
11118
11119 /* Determines if INSN is the start of a jump table.  Returns the end
11120    of the TABLE or NULL_RTX.  */
11121 static rtx
11122 is_jump_table (rtx insn)
11123 {
11124   rtx table;
11125
11126   if (GET_CODE (insn) == JUMP_INSN
11127       && JUMP_LABEL (insn) != NULL
11128       && ((table = next_real_insn (JUMP_LABEL (insn)))
11129           == next_real_insn (insn))
11130       && table != NULL
11131       && GET_CODE (table) == JUMP_INSN
11132       && (GET_CODE (PATTERN (table)) == ADDR_VEC
11133           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
11134     return table;
11135
11136   return NULL_RTX;
11137 }
11138
11139 #ifndef JUMP_TABLES_IN_TEXT_SECTION
11140 #define JUMP_TABLES_IN_TEXT_SECTION 0
11141 #endif
11142
11143 static HOST_WIDE_INT
11144 get_jump_table_size (rtx insn)
11145 {
11146   /* ADDR_VECs only take room if read-only data does into the text
11147      section.  */
11148   if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
11149     {
11150       rtx body = PATTERN (insn);
11151       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
11152       HOST_WIDE_INT size;
11153       HOST_WIDE_INT modesize;
11154
11155       modesize = GET_MODE_SIZE (GET_MODE (body));
11156       size = modesize * XVECLEN (body, elt);
11157       switch (modesize)
11158         {
11159         case 1:
11160           /* Round up size  of TBB table to a halfword boundary.  */
11161           size = (size + 1) & ~(HOST_WIDE_INT)1;
11162           break;
11163         case 2:
11164           /* No padding necessary for TBH.  */
11165           break;
11166         case 4:
11167           /* Add two bytes for alignment on Thumb.  */
11168           if (TARGET_THUMB)
11169             size += 2;
11170           break;
11171         default:
11172           gcc_unreachable ();
11173         }
11174       return size;
11175     }
11176
11177   return 0;
11178 }
11179
11180 /* Move a minipool fix MP from its current location to before MAX_MP.
11181    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
11182    constraints may need updating.  */
11183 static Mnode *
11184 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
11185                                HOST_WIDE_INT max_address)
11186 {
11187   /* The code below assumes these are different.  */
11188   gcc_assert (mp != max_mp);
11189
11190   if (max_mp == NULL)
11191     {
11192       if (max_address < mp->max_address)
11193         mp->max_address = max_address;
11194     }
11195   else
11196     {
11197       if (max_address > max_mp->max_address - mp->fix_size)
11198         mp->max_address = max_mp->max_address - mp->fix_size;
11199       else
11200         mp->max_address = max_address;
11201
11202       /* Unlink MP from its current position.  Since max_mp is non-null,
11203        mp->prev must be non-null.  */
11204       mp->prev->next = mp->next;
11205       if (mp->next != NULL)
11206         mp->next->prev = mp->prev;
11207       else
11208         minipool_vector_tail = mp->prev;
11209
11210       /* Re-insert it before MAX_MP.  */
11211       mp->next = max_mp;
11212       mp->prev = max_mp->prev;
11213       max_mp->prev = mp;
11214
11215       if (mp->prev != NULL)
11216         mp->prev->next = mp;
11217       else
11218         minipool_vector_head = mp;
11219     }
11220
11221   /* Save the new entry.  */
11222   max_mp = mp;
11223
11224   /* Scan over the preceding entries and adjust their addresses as
11225      required.  */
11226   while (mp->prev != NULL
11227          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
11228     {
11229       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
11230       mp = mp->prev;
11231     }
11232
11233   return max_mp;
11234 }
11235
11236 /* Add a constant to the minipool for a forward reference.  Returns the
11237    node added or NULL if the constant will not fit in this pool.  */
11238 static Mnode *
11239 add_minipool_forward_ref (Mfix *fix)
11240 {
11241   /* If set, max_mp is the first pool_entry that has a lower
11242      constraint than the one we are trying to add.  */
11243   Mnode *       max_mp = NULL;
11244   HOST_WIDE_INT max_address = fix->address + fix->forwards - minipool_pad;
11245   Mnode *       mp;
11246
11247   /* If the minipool starts before the end of FIX->INSN then this FIX
11248      can not be placed into the current pool.  Furthermore, adding the
11249      new constant pool entry may cause the pool to start FIX_SIZE bytes
11250      earlier.  */
11251   if (minipool_vector_head &&
11252       (fix->address + get_attr_length (fix->insn)
11253        >= minipool_vector_head->max_address - fix->fix_size))
11254     return NULL;
11255
11256   /* Scan the pool to see if a constant with the same value has
11257      already been added.  While we are doing this, also note the
11258      location where we must insert the constant if it doesn't already
11259      exist.  */
11260   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11261     {
11262       if (GET_CODE (fix->value) == GET_CODE (mp->value)
11263           && fix->mode == mp->mode
11264           && (GET_CODE (fix->value) != CODE_LABEL
11265               || (CODE_LABEL_NUMBER (fix->value)
11266                   == CODE_LABEL_NUMBER (mp->value)))
11267           && rtx_equal_p (fix->value, mp->value))
11268         {
11269           /* More than one fix references this entry.  */
11270           mp->refcount++;
11271           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
11272         }
11273
11274       /* Note the insertion point if necessary.  */
11275       if (max_mp == NULL
11276           && mp->max_address > max_address)
11277         max_mp = mp;
11278
11279       /* If we are inserting an 8-bytes aligned quantity and
11280          we have not already found an insertion point, then
11281          make sure that all such 8-byte aligned quantities are
11282          placed at the start of the pool.  */
11283       if (ARM_DOUBLEWORD_ALIGN
11284           && max_mp == NULL
11285           && fix->fix_size >= 8
11286           && mp->fix_size < 8)
11287         {
11288           max_mp = mp;
11289           max_address = mp->max_address;
11290         }
11291     }
11292
11293   /* The value is not currently in the minipool, so we need to create
11294      a new entry for it.  If MAX_MP is NULL, the entry will be put on
11295      the end of the list since the placement is less constrained than
11296      any existing entry.  Otherwise, we insert the new fix before
11297      MAX_MP and, if necessary, adjust the constraints on the other
11298      entries.  */
11299   mp = XNEW (Mnode);
11300   mp->fix_size = fix->fix_size;
11301   mp->mode = fix->mode;
11302   mp->value = fix->value;
11303   mp->refcount = 1;
11304   /* Not yet required for a backwards ref.  */
11305   mp->min_address = -65536;
11306
11307   if (max_mp == NULL)
11308     {
11309       mp->max_address = max_address;
11310       mp->next = NULL;
11311       mp->prev = minipool_vector_tail;
11312
11313       if (mp->prev == NULL)
11314         {
11315           minipool_vector_head = mp;
11316           minipool_vector_label = gen_label_rtx ();
11317         }
11318       else
11319         mp->prev->next = mp;
11320
11321       minipool_vector_tail = mp;
11322     }
11323   else
11324     {
11325       if (max_address > max_mp->max_address - mp->fix_size)
11326         mp->max_address = max_mp->max_address - mp->fix_size;
11327       else
11328         mp->max_address = max_address;
11329
11330       mp->next = max_mp;
11331       mp->prev = max_mp->prev;
11332       max_mp->prev = mp;
11333       if (mp->prev != NULL)
11334         mp->prev->next = mp;
11335       else
11336         minipool_vector_head = mp;
11337     }
11338
11339   /* Save the new entry.  */
11340   max_mp = mp;
11341
11342   /* Scan over the preceding entries and adjust their addresses as
11343      required.  */
11344   while (mp->prev != NULL
11345          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
11346     {
11347       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
11348       mp = mp->prev;
11349     }
11350
11351   return max_mp;
11352 }
11353
11354 static Mnode *
11355 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
11356                                 HOST_WIDE_INT  min_address)
11357 {
11358   HOST_WIDE_INT offset;
11359
11360   /* The code below assumes these are different.  */
11361   gcc_assert (mp != min_mp);
11362
11363   if (min_mp == NULL)
11364     {
11365       if (min_address > mp->min_address)
11366         mp->min_address = min_address;
11367     }
11368   else
11369     {
11370       /* We will adjust this below if it is too loose.  */
11371       mp->min_address = min_address;
11372
11373       /* Unlink MP from its current position.  Since min_mp is non-null,
11374          mp->next must be non-null.  */
11375       mp->next->prev = mp->prev;
11376       if (mp->prev != NULL)
11377         mp->prev->next = mp->next;
11378       else
11379         minipool_vector_head = mp->next;
11380
11381       /* Reinsert it after MIN_MP.  */
11382       mp->prev = min_mp;
11383       mp->next = min_mp->next;
11384       min_mp->next = mp;
11385       if (mp->next != NULL)
11386         mp->next->prev = mp;
11387       else
11388         minipool_vector_tail = mp;
11389     }
11390
11391   min_mp = mp;
11392
11393   offset = 0;
11394   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11395     {
11396       mp->offset = offset;
11397       if (mp->refcount > 0)
11398         offset += mp->fix_size;
11399
11400       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
11401         mp->next->min_address = mp->min_address + mp->fix_size;
11402     }
11403
11404   return min_mp;
11405 }
11406
11407 /* Add a constant to the minipool for a backward reference.  Returns the
11408    node added or NULL if the constant will not fit in this pool.
11409
11410    Note that the code for insertion for a backwards reference can be
11411    somewhat confusing because the calculated offsets for each fix do
11412    not take into account the size of the pool (which is still under
11413    construction.  */
11414 static Mnode *
11415 add_minipool_backward_ref (Mfix *fix)
11416 {
11417   /* If set, min_mp is the last pool_entry that has a lower constraint
11418      than the one we are trying to add.  */
11419   Mnode *min_mp = NULL;
11420   /* This can be negative, since it is only a constraint.  */
11421   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
11422   Mnode *mp;
11423
11424   /* If we can't reach the current pool from this insn, or if we can't
11425      insert this entry at the end of the pool without pushing other
11426      fixes out of range, then we don't try.  This ensures that we
11427      can't fail later on.  */
11428   if (min_address >= minipool_barrier->address
11429       || (minipool_vector_tail->min_address + fix->fix_size
11430           >= minipool_barrier->address))
11431     return NULL;
11432
11433   /* Scan the pool to see if a constant with the same value has
11434      already been added.  While we are doing this, also note the
11435      location where we must insert the constant if it doesn't already
11436      exist.  */
11437   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
11438     {
11439       if (GET_CODE (fix->value) == GET_CODE (mp->value)
11440           && fix->mode == mp->mode
11441           && (GET_CODE (fix->value) != CODE_LABEL
11442               || (CODE_LABEL_NUMBER (fix->value)
11443                   == CODE_LABEL_NUMBER (mp->value)))
11444           && rtx_equal_p (fix->value, mp->value)
11445           /* Check that there is enough slack to move this entry to the
11446              end of the table (this is conservative).  */
11447           && (mp->max_address
11448               > (minipool_barrier->address
11449                  + minipool_vector_tail->offset
11450                  + minipool_vector_tail->fix_size)))
11451         {
11452           mp->refcount++;
11453           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
11454         }
11455
11456       if (min_mp != NULL)
11457         mp->min_address += fix->fix_size;
11458       else
11459         {
11460           /* Note the insertion point if necessary.  */
11461           if (mp->min_address < min_address)
11462             {
11463               /* For now, we do not allow the insertion of 8-byte alignment
11464                  requiring nodes anywhere but at the start of the pool.  */
11465               if (ARM_DOUBLEWORD_ALIGN
11466                   && fix->fix_size >= 8 && mp->fix_size < 8)
11467                 return NULL;
11468               else
11469                 min_mp = mp;
11470             }
11471           else if (mp->max_address
11472                    < minipool_barrier->address + mp->offset + fix->fix_size)
11473             {
11474               /* Inserting before this entry would push the fix beyond
11475                  its maximum address (which can happen if we have
11476                  re-located a forwards fix); force the new fix to come
11477                  after it.  */
11478               if (ARM_DOUBLEWORD_ALIGN
11479                   && fix->fix_size >= 8 && mp->fix_size < 8)
11480                 return NULL;
11481               else
11482                 {
11483                   min_mp = mp;
11484                   min_address = mp->min_address + fix->fix_size;
11485                 }
11486             }
11487           /* Do not insert a non-8-byte aligned quantity before 8-byte
11488              aligned quantities.  */
11489           else if (ARM_DOUBLEWORD_ALIGN
11490                    && fix->fix_size < 8
11491                    && mp->fix_size >= 8)
11492             {
11493               min_mp = mp;
11494               min_address = mp->min_address + fix->fix_size;
11495             }
11496         }
11497     }
11498
11499   /* We need to create a new entry.  */
11500   mp = XNEW (Mnode);
11501   mp->fix_size = fix->fix_size;
11502   mp->mode = fix->mode;
11503   mp->value = fix->value;
11504   mp->refcount = 1;
11505   mp->max_address = minipool_barrier->address + 65536;
11506
11507   mp->min_address = min_address;
11508
11509   if (min_mp == NULL)
11510     {
11511       mp->prev = NULL;
11512       mp->next = minipool_vector_head;
11513
11514       if (mp->next == NULL)
11515         {
11516           minipool_vector_tail = mp;
11517           minipool_vector_label = gen_label_rtx ();
11518         }
11519       else
11520         mp->next->prev = mp;
11521
11522       minipool_vector_head = mp;
11523     }
11524   else
11525     {
11526       mp->next = min_mp->next;
11527       mp->prev = min_mp;
11528       min_mp->next = mp;
11529
11530       if (mp->next != NULL)
11531         mp->next->prev = mp;
11532       else
11533         minipool_vector_tail = mp;
11534     }
11535
11536   /* Save the new entry.  */
11537   min_mp = mp;
11538
11539   if (mp->prev)
11540     mp = mp->prev;
11541   else
11542     mp->offset = 0;
11543
11544   /* Scan over the following entries and adjust their offsets.  */
11545   while (mp->next != NULL)
11546     {
11547       if (mp->next->min_address < mp->min_address + mp->fix_size)
11548         mp->next->min_address = mp->min_address + mp->fix_size;
11549
11550       if (mp->refcount)
11551         mp->next->offset = mp->offset + mp->fix_size;
11552       else
11553         mp->next->offset = mp->offset;
11554
11555       mp = mp->next;
11556     }
11557
11558   return min_mp;
11559 }
11560
11561 static void
11562 assign_minipool_offsets (Mfix *barrier)
11563 {
11564   HOST_WIDE_INT offset = 0;
11565   Mnode *mp;
11566
11567   minipool_barrier = barrier;
11568
11569   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11570     {
11571       mp->offset = offset;
11572
11573       if (mp->refcount > 0)
11574         offset += mp->fix_size;
11575     }
11576 }
11577
11578 /* Output the literal table */
11579 static void
11580 dump_minipool (rtx scan)
11581 {
11582   Mnode * mp;
11583   Mnode * nmp;
11584   int align64 = 0;
11585
11586   if (ARM_DOUBLEWORD_ALIGN)
11587     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11588       if (mp->refcount > 0 && mp->fix_size >= 8)
11589         {
11590           align64 = 1;
11591           break;
11592         }
11593
11594   if (dump_file)
11595     fprintf (dump_file,
11596              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
11597              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
11598
11599   scan = emit_label_after (gen_label_rtx (), scan);
11600   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
11601   scan = emit_label_after (minipool_vector_label, scan);
11602
11603   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
11604     {
11605       if (mp->refcount > 0)
11606         {
11607           if (dump_file)
11608             {
11609               fprintf (dump_file,
11610                        ";;  Offset %u, min %ld, max %ld ",
11611                        (unsigned) mp->offset, (unsigned long) mp->min_address,
11612                        (unsigned long) mp->max_address);
11613               arm_print_value (dump_file, mp->value);
11614               fputc ('\n', dump_file);
11615             }
11616
11617           switch (mp->fix_size)
11618             {
11619 #ifdef HAVE_consttable_1
11620             case 1:
11621               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
11622               break;
11623
11624 #endif
11625 #ifdef HAVE_consttable_2
11626             case 2:
11627               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
11628               break;
11629
11630 #endif
11631 #ifdef HAVE_consttable_4
11632             case 4:
11633               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
11634               break;
11635
11636 #endif
11637 #ifdef HAVE_consttable_8
11638             case 8:
11639               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
11640               break;
11641
11642 #endif
11643 #ifdef HAVE_consttable_16
11644             case 16:
11645               scan = emit_insn_after (gen_consttable_16 (mp->value), scan);
11646               break;
11647
11648 #endif
11649             default:
11650               gcc_unreachable ();
11651             }
11652         }
11653
11654       nmp = mp->next;
11655       free (mp);
11656     }
11657
11658   minipool_vector_head = minipool_vector_tail = NULL;
11659   scan = emit_insn_after (gen_consttable_end (), scan);
11660   scan = emit_barrier_after (scan);
11661 }
11662
11663 /* Return the cost of forcibly inserting a barrier after INSN.  */
11664 static int
11665 arm_barrier_cost (rtx insn)
11666 {
11667   /* Basing the location of the pool on the loop depth is preferable,
11668      but at the moment, the basic block information seems to be
11669      corrupt by this stage of the compilation.  */
11670   int base_cost = 50;
11671   rtx next = next_nonnote_insn (insn);
11672
11673   if (next != NULL && GET_CODE (next) == CODE_LABEL)
11674     base_cost -= 20;
11675
11676   switch (GET_CODE (insn))
11677     {
11678     case CODE_LABEL:
11679       /* It will always be better to place the table before the label, rather
11680          than after it.  */
11681       return 50;
11682
11683     case INSN:
11684     case CALL_INSN:
11685       return base_cost;
11686
11687     case JUMP_INSN:
11688       return base_cost - 10;
11689
11690     default:
11691       return base_cost + 10;
11692     }
11693 }
11694
11695 /* Find the best place in the insn stream in the range
11696    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
11697    Create the barrier by inserting a jump and add a new fix entry for
11698    it.  */
11699 static Mfix *
11700 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
11701 {
11702   HOST_WIDE_INT count = 0;
11703   rtx barrier;
11704   rtx from = fix->insn;
11705   /* The instruction after which we will insert the jump.  */
11706   rtx selected = NULL;
11707   int selected_cost;
11708   /* The address at which the jump instruction will be placed.  */
11709   HOST_WIDE_INT selected_address;
11710   Mfix * new_fix;
11711   HOST_WIDE_INT max_count = max_address - fix->address;
11712   rtx label = gen_label_rtx ();
11713
11714   selected_cost = arm_barrier_cost (from);
11715   selected_address = fix->address;
11716
11717   while (from && count < max_count)
11718     {
11719       rtx tmp;
11720       int new_cost;
11721
11722       /* This code shouldn't have been called if there was a natural barrier
11723          within range.  */
11724       gcc_assert (GET_CODE (from) != BARRIER);
11725
11726       /* Count the length of this insn.  */
11727       count += get_attr_length (from);
11728
11729       /* If there is a jump table, add its length.  */
11730       tmp = is_jump_table (from);
11731       if (tmp != NULL)
11732         {
11733           count += get_jump_table_size (tmp);
11734
11735           /* Jump tables aren't in a basic block, so base the cost on
11736              the dispatch insn.  If we select this location, we will
11737              still put the pool after the table.  */
11738           new_cost = arm_barrier_cost (from);
11739
11740           if (count < max_count 
11741               && (!selected || new_cost <= selected_cost))
11742             {
11743               selected = tmp;
11744               selected_cost = new_cost;
11745               selected_address = fix->address + count;
11746             }
11747
11748           /* Continue after the dispatch table.  */
11749           from = NEXT_INSN (tmp);
11750           continue;
11751         }
11752
11753       new_cost = arm_barrier_cost (from);
11754
11755       if (count < max_count
11756           && (!selected || new_cost <= selected_cost))
11757         {
11758           selected = from;
11759           selected_cost = new_cost;
11760           selected_address = fix->address + count;
11761         }
11762
11763       from = NEXT_INSN (from);
11764     }
11765
11766   /* Make sure that we found a place to insert the jump.  */
11767   gcc_assert (selected);
11768
11769   /* Create a new JUMP_INSN that branches around a barrier.  */
11770   from = emit_jump_insn_after (gen_jump (label), selected);
11771   JUMP_LABEL (from) = label;
11772   barrier = emit_barrier_after (from);
11773   emit_label_after (label, barrier);
11774
11775   /* Create a minipool barrier entry for the new barrier.  */
11776   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
11777   new_fix->insn = barrier;
11778   new_fix->address = selected_address;
11779   new_fix->next = fix->next;
11780   fix->next = new_fix;
11781
11782   return new_fix;
11783 }
11784
11785 /* Record that there is a natural barrier in the insn stream at
11786    ADDRESS.  */
11787 static void
11788 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
11789 {
11790   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
11791
11792   fix->insn = insn;
11793   fix->address = address;
11794
11795   fix->next = NULL;
11796   if (minipool_fix_head != NULL)
11797     minipool_fix_tail->next = fix;
11798   else
11799     minipool_fix_head = fix;
11800
11801   minipool_fix_tail = fix;
11802 }
11803
11804 /* Record INSN, which will need fixing up to load a value from the
11805    minipool.  ADDRESS is the offset of the insn since the start of the
11806    function; LOC is a pointer to the part of the insn which requires
11807    fixing; VALUE is the constant that must be loaded, which is of type
11808    MODE.  */
11809 static void
11810 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
11811                    enum machine_mode mode, rtx value)
11812 {
11813   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
11814
11815   fix->insn = insn;
11816   fix->address = address;
11817   fix->loc = loc;
11818   fix->mode = mode;
11819   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
11820   fix->value = value;
11821   fix->forwards = get_attr_pool_range (insn);
11822   fix->backwards = get_attr_neg_pool_range (insn);
11823   fix->minipool = NULL;
11824
11825   /* If an insn doesn't have a range defined for it, then it isn't
11826      expecting to be reworked by this code.  Better to stop now than
11827      to generate duff assembly code.  */
11828   gcc_assert (fix->forwards || fix->backwards);
11829
11830   /* If an entry requires 8-byte alignment then assume all constant pools
11831      require 4 bytes of padding.  Trying to do this later on a per-pool
11832      basis is awkward because existing pool entries have to be modified.  */
11833   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size >= 8)
11834     minipool_pad = 4;
11835
11836   if (dump_file)
11837     {
11838       fprintf (dump_file,
11839                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
11840                GET_MODE_NAME (mode),
11841                INSN_UID (insn), (unsigned long) address,
11842                -1 * (long)fix->backwards, (long)fix->forwards);
11843       arm_print_value (dump_file, fix->value);
11844       fprintf (dump_file, "\n");
11845     }
11846
11847   /* Add it to the chain of fixes.  */
11848   fix->next = NULL;
11849
11850   if (minipool_fix_head != NULL)
11851     minipool_fix_tail->next = fix;
11852   else
11853     minipool_fix_head = fix;
11854
11855   minipool_fix_tail = fix;
11856 }
11857
11858 /* Return the cost of synthesizing a 64-bit constant VAL inline.
11859    Returns the number of insns needed, or 99 if we don't know how to
11860    do it.  */
11861 int
11862 arm_const_double_inline_cost (rtx val)
11863 {
11864   rtx lowpart, highpart;
11865   enum machine_mode mode;
11866
11867   mode = GET_MODE (val);
11868
11869   if (mode == VOIDmode)
11870     mode = DImode;
11871
11872   gcc_assert (GET_MODE_SIZE (mode) == 8);
11873
11874   lowpart = gen_lowpart (SImode, val);
11875   highpart = gen_highpart_mode (SImode, mode, val);
11876
11877   gcc_assert (GET_CODE (lowpart) == CONST_INT);
11878   gcc_assert (GET_CODE (highpart) == CONST_INT);
11879
11880   return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
11881                             NULL_RTX, NULL_RTX, 0, 0)
11882           + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
11883                               NULL_RTX, NULL_RTX, 0, 0));
11884 }
11885
11886 /* Return true if it is worthwhile to split a 64-bit constant into two
11887    32-bit operations.  This is the case if optimizing for size, or
11888    if we have load delay slots, or if one 32-bit part can be done with
11889    a single data operation.  */
11890 bool
11891 arm_const_double_by_parts (rtx val)
11892 {
11893   enum machine_mode mode = GET_MODE (val);
11894   rtx part;
11895
11896   if (optimize_size || arm_ld_sched)
11897     return true;
11898
11899   if (mode == VOIDmode)
11900     mode = DImode;
11901
11902   part = gen_highpart_mode (SImode, mode, val);
11903
11904   gcc_assert (GET_CODE (part) == CONST_INT);
11905
11906   if (const_ok_for_arm (INTVAL (part))
11907       || const_ok_for_arm (~INTVAL (part)))
11908     return true;
11909
11910   part = gen_lowpart (SImode, val);
11911
11912   gcc_assert (GET_CODE (part) == CONST_INT);
11913
11914   if (const_ok_for_arm (INTVAL (part))
11915       || const_ok_for_arm (~INTVAL (part)))
11916     return true;
11917
11918   return false;
11919 }
11920
11921 /* Return true if it is possible to inline both the high and low parts
11922    of a 64-bit constant into 32-bit data processing instructions.  */
11923 bool
11924 arm_const_double_by_immediates (rtx val)
11925 {
11926   enum machine_mode mode = GET_MODE (val);
11927   rtx part;
11928
11929   if (mode == VOIDmode)
11930     mode = DImode;
11931
11932   part = gen_highpart_mode (SImode, mode, val);
11933
11934   gcc_assert (GET_CODE (part) == CONST_INT);
11935
11936   if (!const_ok_for_arm (INTVAL (part)))
11937     return false;
11938
11939   part = gen_lowpart (SImode, val);
11940
11941   gcc_assert (GET_CODE (part) == CONST_INT);
11942
11943   if (!const_ok_for_arm (INTVAL (part)))
11944     return false;
11945
11946   return true;
11947 }
11948
11949 /* Scan INSN and note any of its operands that need fixing.
11950    If DO_PUSHES is false we do not actually push any of the fixups
11951    needed.  The function returns TRUE if any fixups were needed/pushed.
11952    This is used by arm_memory_load_p() which needs to know about loads
11953    of constants that will be converted into minipool loads.  */
11954 static bool
11955 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
11956 {
11957   bool result = false;
11958   int opno;
11959
11960   extract_insn (insn);
11961
11962   if (!constrain_operands (1))
11963     fatal_insn_not_found (insn);
11964
11965   if (recog_data.n_alternatives == 0)
11966     return false;
11967
11968   /* Fill in recog_op_alt with information about the constraints of
11969      this insn.  */
11970   preprocess_constraints ();
11971
11972   for (opno = 0; opno < recog_data.n_operands; opno++)
11973     {
11974       /* Things we need to fix can only occur in inputs.  */
11975       if (recog_data.operand_type[opno] != OP_IN)
11976         continue;
11977
11978       /* If this alternative is a memory reference, then any mention
11979          of constants in this alternative is really to fool reload
11980          into allowing us to accept one there.  We need to fix them up
11981          now so that we output the right code.  */
11982       if (recog_op_alt[opno][which_alternative].memory_ok)
11983         {
11984           rtx op = recog_data.operand[opno];
11985
11986           if (CONSTANT_P (op))
11987             {
11988               if (do_pushes)
11989                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
11990                                    recog_data.operand_mode[opno], op);
11991               result = true;
11992             }
11993           else if (GET_CODE (op) == MEM
11994                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
11995                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
11996             {
11997               if (do_pushes)
11998                 {
11999                   rtx cop = avoid_constant_pool_reference (op);
12000
12001                   /* Casting the address of something to a mode narrower
12002                      than a word can cause avoid_constant_pool_reference()
12003                      to return the pool reference itself.  That's no good to
12004                      us here.  Lets just hope that we can use the
12005                      constant pool value directly.  */
12006                   if (op == cop)
12007                     cop = get_pool_constant (XEXP (op, 0));
12008
12009                   push_minipool_fix (insn, address,
12010                                      recog_data.operand_loc[opno],
12011                                      recog_data.operand_mode[opno], cop);
12012                 }
12013
12014               result = true;
12015             }
12016         }
12017     }
12018
12019   return result;
12020 }
12021
12022 /* Convert instructions to their cc-clobbering variant if possible, since
12023    that allows us to use smaller encodings.  */
12024
12025 static void
12026 thumb2_reorg (void)
12027 {
12028   basic_block bb;
12029   regset_head live;
12030
12031   INIT_REG_SET (&live);
12032
12033   /* We are freeing block_for_insn in the toplev to keep compatibility
12034      with old MDEP_REORGS that are not CFG based.  Recompute it now.  */
12035   compute_bb_for_insn ();
12036   df_analyze ();
12037
12038   FOR_EACH_BB (bb)
12039     {
12040       rtx insn;
12041       COPY_REG_SET (&live, DF_LR_OUT (bb));
12042       df_simulate_initialize_backwards (bb, &live);
12043       FOR_BB_INSNS_REVERSE (bb, insn)
12044         {
12045           if (NONJUMP_INSN_P (insn)
12046               && !REGNO_REG_SET_P (&live, CC_REGNUM))
12047             {
12048               rtx pat = PATTERN (insn);
12049               if (GET_CODE (pat) == SET
12050                   && low_register_operand (XEXP (pat, 0), SImode)
12051                   && thumb_16bit_operator (XEXP (pat, 1), SImode)
12052                   && low_register_operand (XEXP (XEXP (pat, 1), 0), SImode)
12053                   && low_register_operand (XEXP (XEXP (pat, 1), 1), SImode))
12054                 {
12055                   rtx dst = XEXP (pat, 0);
12056                   rtx src = XEXP (pat, 1);
12057                   rtx op0 = XEXP (src, 0);
12058                   if (rtx_equal_p (dst, op0)
12059                       || GET_CODE (src) == PLUS || GET_CODE (src) == MINUS)
12060                     {
12061                       rtx ccreg = gen_rtx_REG (CCmode, CC_REGNUM);
12062                       rtx clobber = gen_rtx_CLOBBER (VOIDmode, ccreg);
12063                       rtvec vec = gen_rtvec (2, pat, clobber);
12064                       PATTERN (insn) = gen_rtx_PARALLEL (VOIDmode, vec);
12065                       INSN_CODE (insn) = -1;
12066                     }
12067                 }
12068             }
12069           if (NONDEBUG_INSN_P (insn))
12070             df_simulate_one_insn_backwards (bb, insn, &live);
12071         }
12072     }
12073   CLEAR_REG_SET (&live);
12074 }
12075
12076 /* Gcc puts the pool in the wrong place for ARM, since we can only
12077    load addresses a limited distance around the pc.  We do some
12078    special munging to move the constant pool values to the correct
12079    point in the code.  */
12080 static void
12081 arm_reorg (void)
12082 {
12083   rtx insn;
12084   HOST_WIDE_INT address = 0;
12085   Mfix * fix;
12086
12087   if (TARGET_THUMB2)
12088     thumb2_reorg ();
12089   
12090   minipool_fix_head = minipool_fix_tail = NULL;
12091
12092   /* The first insn must always be a note, or the code below won't
12093      scan it properly.  */
12094   insn = get_insns ();
12095   gcc_assert (GET_CODE (insn) == NOTE);
12096   minipool_pad = 0;
12097
12098   /* Scan all the insns and record the operands that will need fixing.  */
12099   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
12100     {
12101       if (TARGET_CIRRUS_FIX_INVALID_INSNS
12102           && (arm_cirrus_insn_p (insn)
12103               || GET_CODE (insn) == JUMP_INSN
12104               || arm_memory_load_p (insn)))
12105         cirrus_reorg (insn);
12106
12107       if (GET_CODE (insn) == BARRIER)
12108         push_minipool_barrier (insn, address);
12109       else if (INSN_P (insn))
12110         {
12111           rtx table;
12112
12113           note_invalid_constants (insn, address, true);
12114           address += get_attr_length (insn);
12115
12116           /* If the insn is a vector jump, add the size of the table
12117              and skip the table.  */
12118           if ((table = is_jump_table (insn)) != NULL)
12119             {
12120               address += get_jump_table_size (table);
12121               insn = table;
12122             }
12123         }
12124     }
12125
12126   fix = minipool_fix_head;
12127
12128   /* Now scan the fixups and perform the required changes.  */
12129   while (fix)
12130     {
12131       Mfix * ftmp;
12132       Mfix * fdel;
12133       Mfix *  last_added_fix;
12134       Mfix * last_barrier = NULL;
12135       Mfix * this_fix;
12136
12137       /* Skip any further barriers before the next fix.  */
12138       while (fix && GET_CODE (fix->insn) == BARRIER)
12139         fix = fix->next;
12140
12141       /* No more fixes.  */
12142       if (fix == NULL)
12143         break;
12144
12145       last_added_fix = NULL;
12146
12147       for (ftmp = fix; ftmp; ftmp = ftmp->next)
12148         {
12149           if (GET_CODE (ftmp->insn) == BARRIER)
12150             {
12151               if (ftmp->address >= minipool_vector_head->max_address)
12152                 break;
12153
12154               last_barrier = ftmp;
12155             }
12156           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
12157             break;
12158
12159           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
12160         }
12161
12162       /* If we found a barrier, drop back to that; any fixes that we
12163          could have reached but come after the barrier will now go in
12164          the next mini-pool.  */
12165       if (last_barrier != NULL)
12166         {
12167           /* Reduce the refcount for those fixes that won't go into this
12168              pool after all.  */
12169           for (fdel = last_barrier->next;
12170                fdel && fdel != ftmp;
12171                fdel = fdel->next)
12172             {
12173               fdel->minipool->refcount--;
12174               fdel->minipool = NULL;
12175             }
12176
12177           ftmp = last_barrier;
12178         }
12179       else
12180         {
12181           /* ftmp is first fix that we can't fit into this pool and
12182              there no natural barriers that we could use.  Insert a
12183              new barrier in the code somewhere between the previous
12184              fix and this one, and arrange to jump around it.  */
12185           HOST_WIDE_INT max_address;
12186
12187           /* The last item on the list of fixes must be a barrier, so
12188              we can never run off the end of the list of fixes without
12189              last_barrier being set.  */
12190           gcc_assert (ftmp);
12191
12192           max_address = minipool_vector_head->max_address;
12193           /* Check that there isn't another fix that is in range that
12194              we couldn't fit into this pool because the pool was
12195              already too large: we need to put the pool before such an
12196              instruction.  The pool itself may come just after the
12197              fix because create_fix_barrier also allows space for a
12198              jump instruction.  */
12199           if (ftmp->address < max_address)
12200             max_address = ftmp->address + 1;
12201
12202           last_barrier = create_fix_barrier (last_added_fix, max_address);
12203         }
12204
12205       assign_minipool_offsets (last_barrier);
12206
12207       while (ftmp)
12208         {
12209           if (GET_CODE (ftmp->insn) != BARRIER
12210               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
12211                   == NULL))
12212             break;
12213
12214           ftmp = ftmp->next;
12215         }
12216
12217       /* Scan over the fixes we have identified for this pool, fixing them
12218          up and adding the constants to the pool itself.  */
12219       for (this_fix = fix; this_fix && ftmp != this_fix;
12220            this_fix = this_fix->next)
12221         if (GET_CODE (this_fix->insn) != BARRIER)
12222           {
12223             rtx addr
12224               = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
12225                                                   minipool_vector_label),
12226                                this_fix->minipool->offset);
12227             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
12228           }
12229
12230       dump_minipool (last_barrier->insn);
12231       fix = ftmp;
12232     }
12233
12234   /* From now on we must synthesize any constants that we can't handle
12235      directly.  This can happen if the RTL gets split during final
12236      instruction generation.  */
12237   after_arm_reorg = 1;
12238
12239   /* Free the minipool memory.  */
12240   obstack_free (&minipool_obstack, minipool_startobj);
12241 }
12242 \f
12243 /* Routines to output assembly language.  */
12244
12245 /* If the rtx is the correct value then return the string of the number.
12246    In this way we can ensure that valid double constants are generated even
12247    when cross compiling.  */
12248 const char *
12249 fp_immediate_constant (rtx x)
12250 {
12251   REAL_VALUE_TYPE r;
12252   int i;
12253
12254   if (!fp_consts_inited)
12255     init_fp_table ();
12256
12257   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
12258   for (i = 0; i < 8; i++)
12259     if (REAL_VALUES_EQUAL (r, values_fp[i]))
12260       return strings_fp[i];
12261
12262   gcc_unreachable ();
12263 }
12264
12265 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
12266 static const char *
12267 fp_const_from_val (REAL_VALUE_TYPE *r)
12268 {
12269   int i;
12270
12271   if (!fp_consts_inited)
12272     init_fp_table ();
12273
12274   for (i = 0; i < 8; i++)
12275     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
12276       return strings_fp[i];
12277
12278   gcc_unreachable ();
12279 }
12280
12281 /* Output the operands of a LDM/STM instruction to STREAM.
12282    MASK is the ARM register set mask of which only bits 0-15 are important.
12283    REG is the base register, either the frame pointer or the stack pointer,
12284    INSTR is the possibly suffixed load or store instruction.
12285    RFE is nonzero if the instruction should also copy spsr to cpsr.  */
12286
12287 static void
12288 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
12289                  unsigned long mask, int rfe)
12290 {
12291   unsigned i;
12292   bool not_first = FALSE;
12293
12294   gcc_assert (!rfe || (mask & (1 << PC_REGNUM)));
12295   fputc ('\t', stream);
12296   asm_fprintf (stream, instr, reg);
12297   fputc ('{', stream);
12298
12299   for (i = 0; i <= LAST_ARM_REGNUM; i++)
12300     if (mask & (1 << i))
12301       {
12302         if (not_first)
12303           fprintf (stream, ", ");
12304
12305         asm_fprintf (stream, "%r", i);
12306         not_first = TRUE;
12307       }
12308
12309   if (rfe)
12310     fprintf (stream, "}^\n");
12311   else
12312     fprintf (stream, "}\n");
12313 }
12314
12315
12316 /* Output a FLDMD instruction to STREAM.
12317    BASE if the register containing the address.
12318    REG and COUNT specify the register range.
12319    Extra registers may be added to avoid hardware bugs.
12320
12321    We output FLDMD even for ARMv5 VFP implementations.  Although
12322    FLDMD is technically not supported until ARMv6, it is believed
12323    that all VFP implementations support its use in this context.  */
12324
12325 static void
12326 vfp_output_fldmd (FILE * stream, unsigned int base, int reg, int count)
12327 {
12328   int i;
12329
12330   /* Workaround ARM10 VFPr1 bug.  */
12331   if (count == 2 && !arm_arch6)
12332     {
12333       if (reg == 15)
12334         reg--;
12335       count++;
12336     }
12337
12338   /* FLDMD may not load more than 16 doubleword registers at a time. Split the
12339      load into multiple parts if we have to handle more than 16 registers.  */
12340   if (count > 16)
12341     {
12342       vfp_output_fldmd (stream, base, reg, 16);
12343       vfp_output_fldmd (stream, base, reg + 16, count - 16);
12344       return;
12345     }
12346
12347   fputc ('\t', stream);
12348   asm_fprintf (stream, "fldmfdd\t%r!, {", base);
12349
12350   for (i = reg; i < reg + count; i++)
12351     {
12352       if (i > reg)
12353         fputs (", ", stream);
12354       asm_fprintf (stream, "d%d", i);
12355     }
12356   fputs ("}\n", stream);
12357
12358 }
12359
12360
12361 /* Output the assembly for a store multiple.  */
12362
12363 const char *
12364 vfp_output_fstmd (rtx * operands)
12365 {
12366   char pattern[100];
12367   int p;
12368   int base;
12369   int i;
12370
12371   strcpy (pattern, "fstmfdd\t%m0!, {%P1");
12372   p = strlen (pattern);
12373
12374   gcc_assert (GET_CODE (operands[1]) == REG);
12375
12376   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
12377   for (i = 1; i < XVECLEN (operands[2], 0); i++)
12378     {
12379       p += sprintf (&pattern[p], ", d%d", base + i);
12380     }
12381   strcpy (&pattern[p], "}");
12382
12383   output_asm_insn (pattern, operands);
12384   return "";
12385 }
12386
12387
12388 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
12389    number of bytes pushed.  */
12390
12391 static int
12392 vfp_emit_fstmd (int base_reg, int count)
12393 {
12394   rtx par;
12395   rtx dwarf;
12396   rtx tmp, reg;
12397   int i;
12398
12399   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
12400      register pairs are stored by a store multiple insn.  We avoid this
12401      by pushing an extra pair.  */
12402   if (count == 2 && !arm_arch6)
12403     {
12404       if (base_reg == LAST_VFP_REGNUM - 3)
12405         base_reg -= 2;
12406       count++;
12407     }
12408
12409   /* FSTMD may not store more than 16 doubleword registers at once.  Split
12410      larger stores into multiple parts (up to a maximum of two, in
12411      practice).  */
12412   if (count > 16)
12413     {
12414       int saved;
12415       /* NOTE: base_reg is an internal register number, so each D register
12416          counts as 2.  */
12417       saved = vfp_emit_fstmd (base_reg + 32, count - 16);
12418       saved += vfp_emit_fstmd (base_reg, 16);
12419       return saved;
12420     }
12421
12422   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
12423   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
12424
12425   reg = gen_rtx_REG (DFmode, base_reg);
12426   base_reg += 2;
12427
12428   XVECEXP (par, 0, 0)
12429     = gen_rtx_SET (VOIDmode,
12430                    gen_frame_mem
12431                    (BLKmode,
12432                     gen_rtx_PRE_MODIFY (Pmode,
12433                                         stack_pointer_rtx,
12434                                         plus_constant
12435                                         (stack_pointer_rtx,
12436                                          - (count * 8)))
12437                     ),
12438                    gen_rtx_UNSPEC (BLKmode,
12439                                    gen_rtvec (1, reg),
12440                                    UNSPEC_PUSH_MULT));
12441
12442   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
12443                      plus_constant (stack_pointer_rtx, -(count * 8)));
12444   RTX_FRAME_RELATED_P (tmp) = 1;
12445   XVECEXP (dwarf, 0, 0) = tmp;
12446
12447   tmp = gen_rtx_SET (VOIDmode,
12448                      gen_frame_mem (DFmode, stack_pointer_rtx),
12449                      reg);
12450   RTX_FRAME_RELATED_P (tmp) = 1;
12451   XVECEXP (dwarf, 0, 1) = tmp;
12452
12453   for (i = 1; i < count; i++)
12454     {
12455       reg = gen_rtx_REG (DFmode, base_reg);
12456       base_reg += 2;
12457       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
12458
12459       tmp = gen_rtx_SET (VOIDmode,
12460                          gen_frame_mem (DFmode,
12461                                         plus_constant (stack_pointer_rtx,
12462                                                        i * 8)),
12463                          reg);
12464       RTX_FRAME_RELATED_P (tmp) = 1;
12465       XVECEXP (dwarf, 0, i + 1) = tmp;
12466     }
12467
12468   par = emit_insn (par);
12469   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
12470   RTX_FRAME_RELATED_P (par) = 1;
12471
12472   return count * 8;
12473 }
12474
12475 /* Emit a call instruction with pattern PAT.  ADDR is the address of
12476    the call target.  */
12477
12478 void
12479 arm_emit_call_insn (rtx pat, rtx addr)
12480 {
12481   rtx insn;
12482
12483   insn = emit_call_insn (pat);
12484
12485   /* The PIC register is live on entry to VxWorks PIC PLT entries.
12486      If the call might use such an entry, add a use of the PIC register
12487      to the instruction's CALL_INSN_FUNCTION_USAGE.  */
12488   if (TARGET_VXWORKS_RTP
12489       && flag_pic
12490       && GET_CODE (addr) == SYMBOL_REF
12491       && (SYMBOL_REF_DECL (addr)
12492           ? !targetm.binds_local_p (SYMBOL_REF_DECL (addr))
12493           : !SYMBOL_REF_LOCAL_P (addr)))
12494     {
12495       require_pic_register ();
12496       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), cfun->machine->pic_reg);
12497     }
12498 }
12499
12500 /* Output a 'call' insn.  */
12501 const char *
12502 output_call (rtx *operands)
12503 {
12504   gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly.  */
12505
12506   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
12507   if (REGNO (operands[0]) == LR_REGNUM)
12508     {
12509       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
12510       output_asm_insn ("mov%?\t%0, %|lr", operands);
12511     }
12512
12513   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
12514
12515   if (TARGET_INTERWORK || arm_arch4t)
12516     output_asm_insn ("bx%?\t%0", operands);
12517   else
12518     output_asm_insn ("mov%?\t%|pc, %0", operands);
12519
12520   return "";
12521 }
12522
12523 /* Output a 'call' insn that is a reference in memory. This is
12524    disabled for ARMv5 and we prefer a blx instead because otherwise
12525    there's a significant performance overhead.  */
12526 const char *
12527 output_call_mem (rtx *operands)
12528 {
12529   gcc_assert (!arm_arch5);
12530   if (TARGET_INTERWORK)
12531     {
12532       output_asm_insn ("ldr%?\t%|ip, %0", operands);
12533       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
12534       output_asm_insn ("bx%?\t%|ip", operands);
12535     }
12536   else if (regno_use_in (LR_REGNUM, operands[0]))
12537     {
12538       /* LR is used in the memory address.  We load the address in the
12539          first instruction.  It's safe to use IP as the target of the
12540          load since the call will kill it anyway.  */
12541       output_asm_insn ("ldr%?\t%|ip, %0", operands);
12542       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
12543       if (arm_arch4t)
12544         output_asm_insn ("bx%?\t%|ip", operands);
12545       else
12546         output_asm_insn ("mov%?\t%|pc, %|ip", operands);
12547     }
12548   else
12549     {
12550       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
12551       output_asm_insn ("ldr%?\t%|pc, %0", operands);
12552     }
12553
12554   return "";
12555 }
12556
12557
12558 /* Output a move from arm registers to an fpa registers.
12559    OPERANDS[0] is an fpa register.
12560    OPERANDS[1] is the first registers of an arm register pair.  */
12561 const char *
12562 output_mov_long_double_fpa_from_arm (rtx *operands)
12563 {
12564   int arm_reg0 = REGNO (operands[1]);
12565   rtx ops[3];
12566
12567   gcc_assert (arm_reg0 != IP_REGNUM);
12568
12569   ops[0] = gen_rtx_REG (SImode, arm_reg0);
12570   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
12571   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
12572
12573   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
12574   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
12575
12576   return "";
12577 }
12578
12579 /* Output a move from an fpa register to arm registers.
12580    OPERANDS[0] is the first registers of an arm register pair.
12581    OPERANDS[1] is an fpa register.  */
12582 const char *
12583 output_mov_long_double_arm_from_fpa (rtx *operands)
12584 {
12585   int arm_reg0 = REGNO (operands[0]);
12586   rtx ops[3];
12587
12588   gcc_assert (arm_reg0 != IP_REGNUM);
12589
12590   ops[0] = gen_rtx_REG (SImode, arm_reg0);
12591   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
12592   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
12593
12594   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
12595   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
12596   return "";
12597 }
12598
12599 /* Output a move from arm registers to arm registers of a long double
12600    OPERANDS[0] is the destination.
12601    OPERANDS[1] is the source.  */
12602 const char *
12603 output_mov_long_double_arm_from_arm (rtx *operands)
12604 {
12605   /* We have to be careful here because the two might overlap.  */
12606   int dest_start = REGNO (operands[0]);
12607   int src_start = REGNO (operands[1]);
12608   rtx ops[2];
12609   int i;
12610
12611   if (dest_start < src_start)
12612     {
12613       for (i = 0; i < 3; i++)
12614         {
12615           ops[0] = gen_rtx_REG (SImode, dest_start + i);
12616           ops[1] = gen_rtx_REG (SImode, src_start + i);
12617           output_asm_insn ("mov%?\t%0, %1", ops);
12618         }
12619     }
12620   else
12621     {
12622       for (i = 2; i >= 0; i--)
12623         {
12624           ops[0] = gen_rtx_REG (SImode, dest_start + i);
12625           ops[1] = gen_rtx_REG (SImode, src_start + i);
12626           output_asm_insn ("mov%?\t%0, %1", ops);
12627         }
12628     }
12629
12630   return "";
12631 }
12632
12633 void
12634 arm_emit_movpair (rtx dest, rtx src)
12635  {
12636   /* If the src is an immediate, simplify it.  */
12637   if (CONST_INT_P (src))
12638     {
12639       HOST_WIDE_INT val = INTVAL (src);
12640       emit_set_insn (dest, GEN_INT (val & 0x0000ffff));
12641       if ((val >> 16) & 0x0000ffff)
12642         emit_set_insn (gen_rtx_ZERO_EXTRACT (SImode, dest, GEN_INT (16),
12643                                              GEN_INT (16)),
12644                        GEN_INT ((val >> 16) & 0x0000ffff));
12645       return;
12646     }
12647    emit_set_insn (dest, gen_rtx_HIGH (SImode, src));
12648    emit_set_insn (dest, gen_rtx_LO_SUM (SImode, dest, src));
12649  }
12650
12651 /* Output a move from arm registers to an fpa registers.
12652    OPERANDS[0] is an fpa register.
12653    OPERANDS[1] is the first registers of an arm register pair.  */
12654 const char *
12655 output_mov_double_fpa_from_arm (rtx *operands)
12656 {
12657   int arm_reg0 = REGNO (operands[1]);
12658   rtx ops[2];
12659
12660   gcc_assert (arm_reg0 != IP_REGNUM);
12661
12662   ops[0] = gen_rtx_REG (SImode, arm_reg0);
12663   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
12664   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1}", ops);
12665   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
12666   return "";
12667 }
12668
12669 /* Output a move from an fpa register to arm registers.
12670    OPERANDS[0] is the first registers of an arm register pair.
12671    OPERANDS[1] is an fpa register.  */
12672 const char *
12673 output_mov_double_arm_from_fpa (rtx *operands)
12674 {
12675   int arm_reg0 = REGNO (operands[0]);
12676   rtx ops[2];
12677
12678   gcc_assert (arm_reg0 != IP_REGNUM);
12679
12680   ops[0] = gen_rtx_REG (SImode, arm_reg0);
12681   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
12682   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
12683   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1}", ops);
12684   return "";
12685 }
12686
12687 /* Output a move between double words.
12688    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
12689    or MEM<-REG and all MEMs must be offsettable addresses.  */
12690 const char *
12691 output_move_double (rtx *operands)
12692 {
12693   enum rtx_code code0 = GET_CODE (operands[0]);
12694   enum rtx_code code1 = GET_CODE (operands[1]);
12695   rtx otherops[3];
12696
12697   if (code0 == REG)
12698     {
12699       unsigned int reg0 = REGNO (operands[0]);
12700
12701       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
12702
12703       gcc_assert (code1 == MEM);  /* Constraints should ensure this.  */
12704
12705       switch (GET_CODE (XEXP (operands[1], 0)))
12706         {
12707         case REG:
12708           if (TARGET_LDRD
12709               && !(fix_cm3_ldrd && reg0 == REGNO(XEXP (operands[1], 0))))
12710             output_asm_insn ("ldr%(d%)\t%0, [%m1]", operands);
12711           else
12712             output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
12713           break;
12714
12715         case PRE_INC:
12716           gcc_assert (TARGET_LDRD);
12717           output_asm_insn ("ldr%(d%)\t%0, [%m1, #8]!", operands);
12718           break;
12719
12720         case PRE_DEC:
12721           if (TARGET_LDRD)
12722             output_asm_insn ("ldr%(d%)\t%0, [%m1, #-8]!", operands);
12723           else
12724             output_asm_insn ("ldm%(db%)\t%m1!, %M0", operands);
12725           break;
12726
12727         case POST_INC:
12728           if (TARGET_LDRD)
12729             output_asm_insn ("ldr%(d%)\t%0, [%m1], #8", operands);
12730           else
12731             output_asm_insn ("ldm%(ia%)\t%m1!, %M0", operands);
12732           break;
12733
12734         case POST_DEC:
12735           gcc_assert (TARGET_LDRD);
12736           output_asm_insn ("ldr%(d%)\t%0, [%m1], #-8", operands);
12737           break;
12738
12739         case PRE_MODIFY:
12740         case POST_MODIFY:
12741           /* Autoicrement addressing modes should never have overlapping
12742              base and destination registers, and overlapping index registers
12743              are already prohibited, so this doesn't need to worry about
12744              fix_cm3_ldrd.  */
12745           otherops[0] = operands[0];
12746           otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
12747           otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
12748
12749           if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
12750             {
12751               if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
12752                 {
12753                   /* Registers overlap so split out the increment.  */
12754                   output_asm_insn ("add%?\t%1, %1, %2", otherops);
12755                   output_asm_insn ("ldr%(d%)\t%0, [%1] @split", otherops);
12756                 }
12757               else
12758                 {
12759                   /* Use a single insn if we can.
12760                      FIXME: IWMMXT allows offsets larger than ldrd can
12761                      handle, fix these up with a pair of ldr.  */
12762                   if (TARGET_THUMB2
12763                       || GET_CODE (otherops[2]) != CONST_INT
12764                       || (INTVAL (otherops[2]) > -256
12765                           && INTVAL (otherops[2]) < 256))
12766                     output_asm_insn ("ldr%(d%)\t%0, [%1, %2]!", otherops);
12767                   else
12768                     {
12769                       output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
12770                       output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
12771                     }
12772                 }
12773             }
12774           else
12775             {
12776               /* Use a single insn if we can.
12777                  FIXME: IWMMXT allows offsets larger than ldrd can handle,
12778                  fix these up with a pair of ldr.  */
12779               if (TARGET_THUMB2
12780                   || GET_CODE (otherops[2]) != CONST_INT
12781                   || (INTVAL (otherops[2]) > -256
12782                       && INTVAL (otherops[2]) < 256))
12783                 output_asm_insn ("ldr%(d%)\t%0, [%1], %2", otherops);
12784               else
12785                 {
12786                   output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
12787                   output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
12788                 }
12789             }
12790           break;
12791
12792         case LABEL_REF:
12793         case CONST:
12794           /* We might be able to use ldrd %0, %1 here.  However the range is
12795              different to ldr/adr, and it is broken on some ARMv7-M
12796              implementations.  */
12797           /* Use the second register of the pair to avoid problematic
12798              overlap.  */
12799           otherops[1] = operands[1];
12800           output_asm_insn ("adr%?\t%0, %1", otherops);
12801           operands[1] = otherops[0];
12802           if (TARGET_LDRD)
12803             output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
12804           else
12805             output_asm_insn ("ldm%(ia%)\t%1, %M0", operands);
12806           break;
12807
12808           /* ??? This needs checking for thumb2.  */
12809         default:
12810           if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
12811                                GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
12812             {
12813               otherops[0] = operands[0];
12814               otherops[1] = XEXP (XEXP (operands[1], 0), 0);
12815               otherops[2] = XEXP (XEXP (operands[1], 0), 1);
12816
12817               if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
12818                 {
12819                   if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
12820                     {
12821                       switch ((int) INTVAL (otherops[2]))
12822                         {
12823                         case -8:
12824                           output_asm_insn ("ldm%(db%)\t%1, %M0", otherops);
12825                           return "";
12826                         case -4:
12827                           if (TARGET_THUMB2)
12828                             break;
12829                           output_asm_insn ("ldm%(da%)\t%1, %M0", otherops);
12830                           return "";
12831                         case 4:
12832                           if (TARGET_THUMB2)
12833                             break;
12834                           output_asm_insn ("ldm%(ib%)\t%1, %M0", otherops);
12835                           return "";
12836                         }
12837                     }
12838                   otherops[0] = gen_rtx_REG(SImode, REGNO(operands[0]) + 1);
12839                   operands[1] = otherops[0];
12840                   if (TARGET_LDRD
12841                       && (GET_CODE (otherops[2]) == REG
12842                           || TARGET_THUMB2
12843                           || (GET_CODE (otherops[2]) == CONST_INT
12844                               && INTVAL (otherops[2]) > -256
12845                               && INTVAL (otherops[2]) < 256)))
12846                     {
12847                       if (reg_overlap_mentioned_p (operands[0],
12848                                                    otherops[2]))
12849                         {
12850                           rtx tmp;
12851                           /* Swap base and index registers over to
12852                              avoid a conflict.  */
12853                           tmp = otherops[1];
12854                           otherops[1] = otherops[2];
12855                           otherops[2] = tmp;
12856                         }
12857                       /* If both registers conflict, it will usually
12858                          have been fixed by a splitter.  */
12859                       if (reg_overlap_mentioned_p (operands[0], otherops[2])
12860                           || (fix_cm3_ldrd && reg0 == REGNO (otherops[1])))
12861                         {
12862                           output_asm_insn ("add%?\t%0, %1, %2", otherops);
12863                           output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
12864                         }
12865                       else
12866                         {
12867                           otherops[0] = operands[0];
12868                           output_asm_insn ("ldr%(d%)\t%0, [%1, %2]", otherops);
12869                         }
12870                       return "";
12871                     }
12872
12873                   if (GET_CODE (otherops[2]) == CONST_INT)
12874                     {
12875                       if (!(const_ok_for_arm (INTVAL (otherops[2]))))
12876                         output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
12877                       else
12878                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
12879                     }
12880                   else
12881                     output_asm_insn ("add%?\t%0, %1, %2", otherops);
12882                 }
12883               else
12884                 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
12885
12886               if (TARGET_LDRD)
12887                 return "ldr%(d%)\t%0, [%1]";
12888
12889               return "ldm%(ia%)\t%1, %M0";
12890             }
12891           else
12892             {
12893               otherops[1] = adjust_address (operands[1], SImode, 4);
12894               /* Take care of overlapping base/data reg.  */
12895               if (reg_mentioned_p (operands[0], operands[1]))
12896                 {
12897                   output_asm_insn ("ldr%?\t%0, %1", otherops);
12898                   output_asm_insn ("ldr%?\t%0, %1", operands);
12899                 }
12900               else
12901                 {
12902                   output_asm_insn ("ldr%?\t%0, %1", operands);
12903                   output_asm_insn ("ldr%?\t%0, %1", otherops);
12904                 }
12905             }
12906         }
12907     }
12908   else
12909     {
12910       /* Constraints should ensure this.  */
12911       gcc_assert (code0 == MEM && code1 == REG);
12912       gcc_assert (REGNO (operands[1]) != IP_REGNUM);
12913
12914       switch (GET_CODE (XEXP (operands[0], 0)))
12915         {
12916         case REG:
12917           if (TARGET_LDRD)
12918             output_asm_insn ("str%(d%)\t%1, [%m0]", operands);
12919           else
12920             output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
12921           break;
12922
12923         case PRE_INC:
12924           gcc_assert (TARGET_LDRD);
12925           output_asm_insn ("str%(d%)\t%1, [%m0, #8]!", operands);
12926           break;
12927
12928         case PRE_DEC:
12929           if (TARGET_LDRD)
12930             output_asm_insn ("str%(d%)\t%1, [%m0, #-8]!", operands);
12931           else
12932             output_asm_insn ("stm%(db%)\t%m0!, %M1", operands);
12933           break;
12934
12935         case POST_INC:
12936           if (TARGET_LDRD)
12937             output_asm_insn ("str%(d%)\t%1, [%m0], #8", operands);
12938           else
12939             output_asm_insn ("stm%(ia%)\t%m0!, %M1", operands);
12940           break;
12941
12942         case POST_DEC:
12943           gcc_assert (TARGET_LDRD);
12944           output_asm_insn ("str%(d%)\t%1, [%m0], #-8", operands);
12945           break;
12946
12947         case PRE_MODIFY:
12948         case POST_MODIFY:
12949           otherops[0] = operands[1];
12950           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
12951           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
12952
12953           /* IWMMXT allows offsets larger than ldrd can handle,
12954              fix these up with a pair of ldr.  */
12955           if (!TARGET_THUMB2
12956               && GET_CODE (otherops[2]) == CONST_INT
12957               && (INTVAL(otherops[2]) <= -256
12958                   || INTVAL(otherops[2]) >= 256))
12959             {
12960               if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
12961                 {
12962                   output_asm_insn ("str%?\t%0, [%1, %2]!", otherops);
12963                   output_asm_insn ("str%?\t%H0, [%1, #4]", otherops);
12964                 }
12965               else
12966                 {
12967                   output_asm_insn ("str%?\t%H0, [%1, #4]", otherops);
12968                   output_asm_insn ("str%?\t%0, [%1], %2", otherops);
12969                 }
12970             }
12971           else if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
12972             output_asm_insn ("str%(d%)\t%0, [%1, %2]!", otherops);
12973           else
12974             output_asm_insn ("str%(d%)\t%0, [%1], %2", otherops);
12975           break;
12976
12977         case PLUS:
12978           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
12979           if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
12980             {
12981               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
12982                 {
12983                 case -8:
12984                   output_asm_insn ("stm%(db%)\t%m0, %M1", operands);
12985                   return "";
12986
12987                 case -4:
12988                   if (TARGET_THUMB2)
12989                     break;
12990                   output_asm_insn ("stm%(da%)\t%m0, %M1", operands);
12991                   return "";
12992
12993                 case 4:
12994                   if (TARGET_THUMB2)
12995                     break;
12996                   output_asm_insn ("stm%(ib%)\t%m0, %M1", operands);
12997                   return "";
12998                 }
12999             }
13000           if (TARGET_LDRD
13001               && (GET_CODE (otherops[2]) == REG
13002                   || TARGET_THUMB2
13003                   || (GET_CODE (otherops[2]) == CONST_INT
13004                       && INTVAL (otherops[2]) > -256
13005                       && INTVAL (otherops[2]) < 256)))
13006             {
13007               otherops[0] = operands[1];
13008               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
13009               output_asm_insn ("str%(d%)\t%0, [%1, %2]", otherops);
13010               return "";
13011             }
13012           /* Fall through */
13013
13014         default:
13015           otherops[0] = adjust_address (operands[0], SImode, 4);
13016           otherops[1] = operands[1];
13017           output_asm_insn ("str%?\t%1, %0", operands);
13018           output_asm_insn ("str%?\t%H1, %0", otherops);
13019         }
13020     }
13021
13022   return "";
13023 }
13024
13025 /* Output a move, load or store for quad-word vectors in ARM registers.  Only
13026    handles MEMs accepted by neon_vector_mem_operand with TYPE=1.  */
13027
13028 const char *
13029 output_move_quad (rtx *operands)
13030 {
13031   if (REG_P (operands[0]))
13032     {
13033       /* Load, or reg->reg move.  */
13034
13035       if (MEM_P (operands[1]))
13036         {
13037           switch (GET_CODE (XEXP (operands[1], 0)))
13038             {
13039             case REG:
13040               output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
13041               break;
13042
13043             case LABEL_REF:
13044             case CONST:
13045               output_asm_insn ("adr%?\t%0, %1", operands);
13046               output_asm_insn ("ldm%(ia%)\t%0, %M0", operands);
13047               break;
13048
13049             default:
13050               gcc_unreachable ();
13051             }
13052         }
13053       else
13054         {
13055           rtx ops[2];
13056           int dest, src, i;
13057
13058           gcc_assert (REG_P (operands[1]));
13059
13060           dest = REGNO (operands[0]);
13061           src = REGNO (operands[1]);
13062
13063           /* This seems pretty dumb, but hopefully GCC won't try to do it
13064              very often.  */
13065           if (dest < src)
13066             for (i = 0; i < 4; i++)
13067               {
13068                 ops[0] = gen_rtx_REG (SImode, dest + i);
13069                 ops[1] = gen_rtx_REG (SImode, src + i);
13070                 output_asm_insn ("mov%?\t%0, %1", ops);
13071               }
13072           else
13073             for (i = 3; i >= 0; i--)
13074               {
13075                 ops[0] = gen_rtx_REG (SImode, dest + i);
13076                 ops[1] = gen_rtx_REG (SImode, src + i);
13077                 output_asm_insn ("mov%?\t%0, %1", ops);
13078               }
13079         }
13080     }
13081   else
13082     {
13083       gcc_assert (MEM_P (operands[0]));
13084       gcc_assert (REG_P (operands[1]));
13085       gcc_assert (!reg_overlap_mentioned_p (operands[1], operands[0]));
13086
13087       switch (GET_CODE (XEXP (operands[0], 0)))
13088         {
13089         case REG:
13090           output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
13091           break;
13092
13093         default:
13094           gcc_unreachable ();
13095         }
13096     }
13097
13098   return "";
13099 }
13100
13101 /* Output a VFP load or store instruction.  */
13102
13103 const char *
13104 output_move_vfp (rtx *operands)
13105 {
13106   rtx reg, mem, addr, ops[2];
13107   int load = REG_P (operands[0]);
13108   int dp = GET_MODE_SIZE (GET_MODE (operands[0])) == 8;
13109   int integer_p = GET_MODE_CLASS (GET_MODE (operands[0])) == MODE_INT;
13110   const char *templ;
13111   char buff[50];
13112   enum machine_mode mode;
13113
13114   reg = operands[!load];
13115   mem = operands[load];
13116
13117   mode = GET_MODE (reg);
13118
13119   gcc_assert (REG_P (reg));
13120   gcc_assert (IS_VFP_REGNUM (REGNO (reg)));
13121   gcc_assert (mode == SFmode
13122               || mode == DFmode
13123               || mode == SImode
13124               || mode == DImode
13125               || (TARGET_NEON && VALID_NEON_DREG_MODE (mode)));
13126   gcc_assert (MEM_P (mem));
13127
13128   addr = XEXP (mem, 0);
13129
13130   switch (GET_CODE (addr))
13131     {
13132     case PRE_DEC:
13133       templ = "f%smdb%c%%?\t%%0!, {%%%s1}%s";
13134       ops[0] = XEXP (addr, 0);
13135       ops[1] = reg;
13136       break;
13137
13138     case POST_INC:
13139       templ = "f%smia%c%%?\t%%0!, {%%%s1}%s";
13140       ops[0] = XEXP (addr, 0);
13141       ops[1] = reg;
13142       break;
13143
13144     default:
13145       templ = "f%s%c%%?\t%%%s0, %%1%s";
13146       ops[0] = reg;
13147       ops[1] = mem;
13148       break;
13149     }
13150
13151   sprintf (buff, templ,
13152            load ? "ld" : "st",
13153            dp ? 'd' : 's',
13154            dp ? "P" : "",
13155            integer_p ? "\t%@ int" : "");
13156   output_asm_insn (buff, ops);
13157
13158   return "";
13159 }
13160
13161 /* Output a Neon quad-word load or store, or a load or store for
13162    larger structure modes.
13163
13164    WARNING: The ordering of elements is weird in big-endian mode,
13165    because we use VSTM, as required by the EABI.  GCC RTL defines
13166    element ordering based on in-memory order.  This can be differ
13167    from the architectural ordering of elements within a NEON register.
13168    The intrinsics defined in arm_neon.h use the NEON register element
13169    ordering, not the GCC RTL element ordering.
13170
13171    For example, the in-memory ordering of a big-endian a quadword
13172    vector with 16-bit elements when stored from register pair {d0,d1}
13173    will be (lowest address first, d0[N] is NEON register element N):
13174
13175      [d0[3], d0[2], d0[1], d0[0], d1[7], d1[6], d1[5], d1[4]]
13176
13177    When necessary, quadword registers (dN, dN+1) are moved to ARM
13178    registers from rN in the order:
13179
13180      dN -> (rN+1, rN), dN+1 -> (rN+3, rN+2)
13181
13182    So that STM/LDM can be used on vectors in ARM registers, and the
13183    same memory layout will result as if VSTM/VLDM were used.  */
13184
13185 const char *
13186 output_move_neon (rtx *operands)
13187 {
13188   rtx reg, mem, addr, ops[2];
13189   int regno, load = REG_P (operands[0]);
13190   const char *templ;
13191   char buff[50];
13192   enum machine_mode mode;
13193
13194   reg = operands[!load];
13195   mem = operands[load];
13196
13197   mode = GET_MODE (reg);
13198
13199   gcc_assert (REG_P (reg));
13200   regno = REGNO (reg);
13201   gcc_assert (VFP_REGNO_OK_FOR_DOUBLE (regno)
13202               || NEON_REGNO_OK_FOR_QUAD (regno));
13203   gcc_assert (VALID_NEON_DREG_MODE (mode)
13204               || VALID_NEON_QREG_MODE (mode)
13205               || VALID_NEON_STRUCT_MODE (mode));
13206   gcc_assert (MEM_P (mem));
13207
13208   addr = XEXP (mem, 0);
13209
13210   /* Strip off const from addresses like (const (plus (...))).  */
13211   if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
13212     addr = XEXP (addr, 0);
13213
13214   switch (GET_CODE (addr))
13215     {
13216     case POST_INC:
13217       templ = "v%smia%%?\t%%0!, %%h1";
13218       ops[0] = XEXP (addr, 0);
13219       ops[1] = reg;
13220       break;
13221
13222     case PRE_DEC:
13223       /* FIXME: We should be using vld1/vst1 here in BE mode?  */
13224       templ = "v%smdb%%?\t%%0!, %%h1";
13225       ops[0] = XEXP (addr, 0);
13226       ops[1] = reg;
13227       break;
13228     
13229     case POST_MODIFY:
13230       /* FIXME: Not currently enabled in neon_vector_mem_operand.  */
13231       gcc_unreachable ();
13232
13233     case LABEL_REF:
13234     case PLUS:
13235       {
13236         int nregs = HARD_REGNO_NREGS (REGNO (reg), mode) / 2;
13237         int i;
13238         int overlap = -1;
13239         for (i = 0; i < nregs; i++)
13240           {
13241             /* We're only using DImode here because it's a convenient size.  */
13242             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * i);
13243             ops[1] = adjust_address (mem, DImode, 8 * i);
13244             if (reg_overlap_mentioned_p (ops[0], mem))
13245               {
13246                 gcc_assert (overlap == -1);
13247                 overlap = i;
13248               }
13249             else
13250               {
13251                 sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
13252                 output_asm_insn (buff, ops);
13253               }
13254           }
13255         if (overlap != -1)
13256           {
13257             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * overlap);
13258             ops[1] = adjust_address (mem, SImode, 8 * overlap);
13259             sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
13260             output_asm_insn (buff, ops);
13261           }
13262
13263         return "";
13264       }
13265
13266     default:
13267       templ = "v%smia%%?\t%%m0, %%h1";
13268       ops[0] = mem;
13269       ops[1] = reg;
13270     }
13271
13272   sprintf (buff, templ, load ? "ld" : "st");
13273   output_asm_insn (buff, ops);
13274
13275   return "";
13276 }
13277
13278 /* Compute and return the length of neon_mov<mode>, where <mode> is
13279    one of VSTRUCT modes: EI, OI, CI or XI.  */
13280 int
13281 arm_attr_length_move_neon (rtx insn)
13282 {
13283   rtx reg, mem, addr;
13284   int load;
13285   enum machine_mode mode;
13286
13287   extract_insn_cached (insn);
13288
13289   if (REG_P (recog_data.operand[0]) && REG_P (recog_data.operand[1]))
13290     {
13291       mode = GET_MODE (recog_data.operand[0]);
13292       switch (mode)
13293         {
13294         case EImode:
13295         case OImode:
13296           return 8;
13297         case CImode:
13298           return 12;
13299         case XImode:
13300           return 16;
13301         default:
13302           gcc_unreachable ();
13303         }
13304     }
13305
13306   load = REG_P (recog_data.operand[0]);
13307   reg = recog_data.operand[!load];
13308   mem = recog_data.operand[load];
13309
13310   gcc_assert (MEM_P (mem));
13311
13312   mode = GET_MODE (reg);
13313   addr = XEXP (mem, 0);
13314
13315   /* Strip off const from addresses like (const (plus (...))).  */
13316   if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
13317     addr = XEXP (addr, 0);
13318
13319   if (GET_CODE (addr) == LABEL_REF || GET_CODE (addr) == PLUS)
13320     {
13321       int insns = HARD_REGNO_NREGS (REGNO (reg), mode) / 2;
13322       return insns * 4;
13323     }
13324   else
13325     return 4;
13326 }
13327
13328 /* Output an ADD r, s, #n where n may be too big for one instruction.
13329    If adding zero to one register, output nothing.  */
13330 const char *
13331 output_add_immediate (rtx *operands)
13332 {
13333   HOST_WIDE_INT n = INTVAL (operands[2]);
13334
13335   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
13336     {
13337       if (n < 0)
13338         output_multi_immediate (operands,
13339                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
13340                                 -n);
13341       else
13342         output_multi_immediate (operands,
13343                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
13344                                 n);
13345     }
13346
13347   return "";
13348 }
13349
13350 /* Output a multiple immediate operation.
13351    OPERANDS is the vector of operands referred to in the output patterns.
13352    INSTR1 is the output pattern to use for the first constant.
13353    INSTR2 is the output pattern to use for subsequent constants.
13354    IMMED_OP is the index of the constant slot in OPERANDS.
13355    N is the constant value.  */
13356 static const char *
13357 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
13358                         int immed_op, HOST_WIDE_INT n)
13359 {
13360 #if HOST_BITS_PER_WIDE_INT > 32
13361   n &= 0xffffffff;
13362 #endif
13363
13364   if (n == 0)
13365     {
13366       /* Quick and easy output.  */
13367       operands[immed_op] = const0_rtx;
13368       output_asm_insn (instr1, operands);
13369     }
13370   else
13371     {
13372       int i;
13373       const char * instr = instr1;
13374
13375       /* Note that n is never zero here (which would give no output).  */
13376       for (i = 0; i < 32; i += 2)
13377         {
13378           if (n & (3 << i))
13379             {
13380               operands[immed_op] = GEN_INT (n & (255 << i));
13381               output_asm_insn (instr, operands);
13382               instr = instr2;
13383               i += 6;
13384             }
13385         }
13386     }
13387
13388   return "";
13389 }
13390
13391 /* Return the name of a shifter operation.  */
13392 static const char *
13393 arm_shift_nmem(enum rtx_code code)
13394 {
13395   switch (code)
13396     {
13397     case ASHIFT:
13398       return ARM_LSL_NAME;
13399
13400     case ASHIFTRT:
13401       return "asr";
13402
13403     case LSHIFTRT:
13404       return "lsr";
13405
13406     case ROTATERT:
13407       return "ror";
13408
13409     default:
13410       abort();
13411     }
13412 }
13413
13414 /* Return the appropriate ARM instruction for the operation code.
13415    The returned result should not be overwritten.  OP is the rtx of the
13416    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
13417    was shifted.  */
13418 const char *
13419 arithmetic_instr (rtx op, int shift_first_arg)
13420 {
13421   switch (GET_CODE (op))
13422     {
13423     case PLUS:
13424       return "add";
13425
13426     case MINUS:
13427       return shift_first_arg ? "rsb" : "sub";
13428
13429     case IOR:
13430       return "orr";
13431
13432     case XOR:
13433       return "eor";
13434
13435     case AND:
13436       return "and";
13437
13438     case ASHIFT:
13439     case ASHIFTRT:
13440     case LSHIFTRT:
13441     case ROTATERT:
13442       return arm_shift_nmem(GET_CODE(op));
13443
13444     default:
13445       gcc_unreachable ();
13446     }
13447 }
13448
13449 /* Ensure valid constant shifts and return the appropriate shift mnemonic
13450    for the operation code.  The returned result should not be overwritten.
13451    OP is the rtx code of the shift.
13452    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
13453    shift.  */
13454 static const char *
13455 shift_op (rtx op, HOST_WIDE_INT *amountp)
13456 {
13457   const char * mnem;
13458   enum rtx_code code = GET_CODE (op);
13459
13460   switch (GET_CODE (XEXP (op, 1)))
13461     {
13462     case REG:
13463     case SUBREG:
13464       *amountp = -1;
13465       break;
13466
13467     case CONST_INT:
13468       *amountp = INTVAL (XEXP (op, 1));
13469       break;
13470
13471     default:
13472       gcc_unreachable ();
13473     }
13474
13475   switch (code)
13476     {
13477     case ROTATE:
13478       gcc_assert (*amountp != -1);
13479       *amountp = 32 - *amountp;
13480       code = ROTATERT;
13481
13482       /* Fall through.  */
13483
13484     case ASHIFT:
13485     case ASHIFTRT:
13486     case LSHIFTRT:
13487     case ROTATERT:
13488       mnem = arm_shift_nmem(code);
13489       break;
13490
13491     case MULT:
13492       /* We never have to worry about the amount being other than a
13493          power of 2, since this case can never be reloaded from a reg.  */
13494       gcc_assert (*amountp != -1);
13495       *amountp = int_log2 (*amountp);
13496       return ARM_LSL_NAME;
13497
13498     default:
13499       gcc_unreachable ();
13500     }
13501
13502   if (*amountp != -1)
13503     {
13504       /* This is not 100% correct, but follows from the desire to merge
13505          multiplication by a power of 2 with the recognizer for a
13506          shift.  >=32 is not a valid shift for "lsl", so we must try and
13507          output a shift that produces the correct arithmetical result.
13508          Using lsr #32 is identical except for the fact that the carry bit
13509          is not set correctly if we set the flags; but we never use the
13510          carry bit from such an operation, so we can ignore that.  */
13511       if (code == ROTATERT)
13512         /* Rotate is just modulo 32.  */
13513         *amountp &= 31;
13514       else if (*amountp != (*amountp & 31))
13515         {
13516           if (code == ASHIFT)
13517             mnem = "lsr";
13518           *amountp = 32;
13519         }
13520
13521       /* Shifts of 0 are no-ops.  */
13522       if (*amountp == 0)
13523         return NULL;
13524     }
13525
13526   return mnem;
13527 }
13528
13529 /* Obtain the shift from the POWER of two.  */
13530
13531 static HOST_WIDE_INT
13532 int_log2 (HOST_WIDE_INT power)
13533 {
13534   HOST_WIDE_INT shift = 0;
13535
13536   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
13537     {
13538       gcc_assert (shift <= 31);
13539       shift++;
13540     }
13541
13542   return shift;
13543 }
13544
13545 /* Output a .ascii pseudo-op, keeping track of lengths.  This is
13546    because /bin/as is horribly restrictive.  The judgement about
13547    whether or not each character is 'printable' (and can be output as
13548    is) or not (and must be printed with an octal escape) must be made
13549    with reference to the *host* character set -- the situation is
13550    similar to that discussed in the comments above pp_c_char in
13551    c-pretty-print.c.  */
13552
13553 #define MAX_ASCII_LEN 51
13554
13555 void
13556 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
13557 {
13558   int i;
13559   int len_so_far = 0;
13560
13561   fputs ("\t.ascii\t\"", stream);
13562
13563   for (i = 0; i < len; i++)
13564     {
13565       int c = p[i];
13566
13567       if (len_so_far >= MAX_ASCII_LEN)
13568         {
13569           fputs ("\"\n\t.ascii\t\"", stream);
13570           len_so_far = 0;
13571         }
13572
13573       if (ISPRINT (c))
13574         {
13575           if (c == '\\' || c == '\"')
13576             {
13577               putc ('\\', stream);
13578               len_so_far++;
13579             }
13580           putc (c, stream);
13581           len_so_far++;
13582         }
13583       else
13584         {
13585           fprintf (stream, "\\%03o", c);
13586           len_so_far += 4;
13587         }
13588     }
13589
13590   fputs ("\"\n", stream);
13591 }
13592 \f
13593 /* Compute the register save mask for registers 0 through 12
13594    inclusive.  This code is used by arm_compute_save_reg_mask.  */
13595
13596 static unsigned long
13597 arm_compute_save_reg0_reg12_mask (void)
13598 {
13599   unsigned long func_type = arm_current_func_type ();
13600   unsigned long save_reg_mask = 0;
13601   unsigned int reg;
13602
13603   if (IS_INTERRUPT (func_type))
13604     {
13605       unsigned int max_reg;
13606       /* Interrupt functions must not corrupt any registers,
13607          even call clobbered ones.  If this is a leaf function
13608          we can just examine the registers used by the RTL, but
13609          otherwise we have to assume that whatever function is
13610          called might clobber anything, and so we have to save
13611          all the call-clobbered registers as well.  */
13612       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
13613         /* FIQ handlers have registers r8 - r12 banked, so
13614            we only need to check r0 - r7, Normal ISRs only
13615            bank r14 and r15, so we must check up to r12.
13616            r13 is the stack pointer which is always preserved,
13617            so we do not need to consider it here.  */
13618         max_reg = 7;
13619       else
13620         max_reg = 12;
13621
13622       for (reg = 0; reg <= max_reg; reg++)
13623         if (df_regs_ever_live_p (reg)
13624             || (! current_function_is_leaf && call_used_regs[reg]))
13625           save_reg_mask |= (1 << reg);
13626
13627       /* Also save the pic base register if necessary.  */
13628       if (flag_pic
13629           && !TARGET_SINGLE_PIC_BASE
13630           && arm_pic_register != INVALID_REGNUM
13631           && crtl->uses_pic_offset_table)
13632         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
13633     }
13634   else if (IS_VOLATILE(func_type))
13635     {
13636       /* For noreturn functions we historically omitted register saves
13637          altogether.  However this really messes up debugging.  As a
13638          compromise save just the frame pointers.  Combined with the link
13639          register saved elsewhere this should be sufficient to get
13640          a backtrace.  */
13641       if (frame_pointer_needed)
13642         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
13643       if (df_regs_ever_live_p (ARM_HARD_FRAME_POINTER_REGNUM))
13644         save_reg_mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
13645       if (df_regs_ever_live_p (THUMB_HARD_FRAME_POINTER_REGNUM))
13646         save_reg_mask |= 1 << THUMB_HARD_FRAME_POINTER_REGNUM;
13647     }
13648   else
13649     {
13650       /* In the normal case we only need to save those registers
13651          which are call saved and which are used by this function.  */
13652       for (reg = 0; reg <= 11; reg++)
13653         if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
13654           save_reg_mask |= (1 << reg);
13655
13656       /* Handle the frame pointer as a special case.  */
13657       if (frame_pointer_needed)
13658         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
13659
13660       /* If we aren't loading the PIC register,
13661          don't stack it even though it may be live.  */
13662       if (flag_pic
13663           && !TARGET_SINGLE_PIC_BASE
13664           && arm_pic_register != INVALID_REGNUM
13665           && (df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM)
13666               || crtl->uses_pic_offset_table))
13667         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
13668
13669       /* The prologue will copy SP into R0, so save it.  */
13670       if (IS_STACKALIGN (func_type))
13671         save_reg_mask |= 1;
13672     }
13673
13674   /* Save registers so the exception handler can modify them.  */
13675   if (crtl->calls_eh_return)
13676     {
13677       unsigned int i;
13678
13679       for (i = 0; ; i++)
13680         {
13681           reg = EH_RETURN_DATA_REGNO (i);
13682           if (reg == INVALID_REGNUM)
13683             break;
13684           save_reg_mask |= 1 << reg;
13685         }
13686     }
13687
13688   return save_reg_mask;
13689 }
13690
13691
13692 /* Compute the number of bytes used to store the static chain register on the 
13693    stack, above the stack frame. We need to know this accurately to get the
13694    alignment of the rest of the stack frame correct. */
13695
13696 static int arm_compute_static_chain_stack_bytes (void)
13697 {
13698   unsigned long func_type = arm_current_func_type ();
13699   int static_chain_stack_bytes = 0;
13700
13701   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM &&
13702       IS_NESTED (func_type) &&
13703       df_regs_ever_live_p (3) && crtl->args.pretend_args_size == 0)
13704     static_chain_stack_bytes = 4;
13705
13706   return static_chain_stack_bytes;
13707 }
13708
13709
13710 /* Compute a bit mask of which registers need to be
13711    saved on the stack for the current function.
13712    This is used by arm_get_frame_offsets, which may add extra registers.  */
13713
13714 static unsigned long
13715 arm_compute_save_reg_mask (void)
13716 {
13717   unsigned int save_reg_mask = 0;
13718   unsigned long func_type = arm_current_func_type ();
13719   unsigned int reg;
13720
13721   if (IS_NAKED (func_type))
13722     /* This should never really happen.  */
13723     return 0;
13724
13725   /* If we are creating a stack frame, then we must save the frame pointer,
13726      IP (which will hold the old stack pointer), LR and the PC.  */
13727   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
13728     save_reg_mask |=
13729       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
13730       | (1 << IP_REGNUM)
13731       | (1 << LR_REGNUM)
13732       | (1 << PC_REGNUM);
13733
13734   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
13735
13736   /* Decide if we need to save the link register.
13737      Interrupt routines have their own banked link register,
13738      so they never need to save it.
13739      Otherwise if we do not use the link register we do not need to save
13740      it.  If we are pushing other registers onto the stack however, we
13741      can save an instruction in the epilogue by pushing the link register
13742      now and then popping it back into the PC.  This incurs extra memory
13743      accesses though, so we only do it when optimizing for size, and only
13744      if we know that we will not need a fancy return sequence.  */
13745   if (df_regs_ever_live_p (LR_REGNUM)
13746       || (save_reg_mask
13747           && optimize_size
13748           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
13749           && !crtl->calls_eh_return))
13750     save_reg_mask |= 1 << LR_REGNUM;
13751
13752   if (cfun->machine->lr_save_eliminated)
13753     save_reg_mask &= ~ (1 << LR_REGNUM);
13754
13755   if (TARGET_REALLY_IWMMXT
13756       && ((bit_count (save_reg_mask)
13757            + ARM_NUM_INTS (crtl->args.pretend_args_size +
13758                            arm_compute_static_chain_stack_bytes())
13759            ) % 2) != 0)
13760     {
13761       /* The total number of registers that are going to be pushed
13762          onto the stack is odd.  We need to ensure that the stack
13763          is 64-bit aligned before we start to save iWMMXt registers,
13764          and also before we start to create locals.  (A local variable
13765          might be a double or long long which we will load/store using
13766          an iWMMXt instruction).  Therefore we need to push another
13767          ARM register, so that the stack will be 64-bit aligned.  We
13768          try to avoid using the arg registers (r0 -r3) as they might be
13769          used to pass values in a tail call.  */
13770       for (reg = 4; reg <= 12; reg++)
13771         if ((save_reg_mask & (1 << reg)) == 0)
13772           break;
13773
13774       if (reg <= 12)
13775         save_reg_mask |= (1 << reg);
13776       else
13777         {
13778           cfun->machine->sibcall_blocked = 1;
13779           save_reg_mask |= (1 << 3);
13780         }
13781     }
13782
13783   /* We may need to push an additional register for use initializing the
13784      PIC base register.  */
13785   if (TARGET_THUMB2 && IS_NESTED (func_type) && flag_pic
13786       && (save_reg_mask & THUMB2_WORK_REGS) == 0)
13787     {
13788       reg = thumb_find_work_register (1 << 4);
13789       if (!call_used_regs[reg])
13790         save_reg_mask |= (1 << reg);
13791     }
13792
13793   return save_reg_mask;
13794 }
13795
13796
13797 /* Compute a bit mask of which registers need to be
13798    saved on the stack for the current function.  */
13799 static unsigned long
13800 thumb1_compute_save_reg_mask (void)
13801 {
13802   unsigned long mask;
13803   unsigned reg;
13804
13805   mask = 0;
13806   for (reg = 0; reg < 12; reg ++)
13807     if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
13808       mask |= 1 << reg;
13809
13810   if (flag_pic
13811       && !TARGET_SINGLE_PIC_BASE
13812       && arm_pic_register != INVALID_REGNUM
13813       && crtl->uses_pic_offset_table)
13814     mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
13815
13816   /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
13817   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
13818     mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
13819
13820   /* LR will also be pushed if any lo regs are pushed.  */
13821   if (mask & 0xff || thumb_force_lr_save ())
13822     mask |= (1 << LR_REGNUM);
13823
13824   /* Make sure we have a low work register if we need one.
13825      We will need one if we are going to push a high register,
13826      but we are not currently intending to push a low register.  */
13827   if ((mask & 0xff) == 0
13828       && ((mask & 0x0f00) || TARGET_BACKTRACE))
13829     {
13830       /* Use thumb_find_work_register to choose which register
13831          we will use.  If the register is live then we will
13832          have to push it.  Use LAST_LO_REGNUM as our fallback
13833          choice for the register to select.  */
13834       reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
13835       /* Make sure the register returned by thumb_find_work_register is
13836          not part of the return value.  */
13837       if (reg * UNITS_PER_WORD <= (unsigned) arm_size_return_regs ())
13838         reg = LAST_LO_REGNUM;
13839
13840       if (! call_used_regs[reg])
13841         mask |= 1 << reg;
13842     }
13843
13844   /* The 504 below is 8 bytes less than 512 because there are two possible
13845      alignment words.  We can't tell here if they will be present or not so we
13846      have to play it safe and assume that they are. */
13847   if ((CALLER_INTERWORKING_SLOT_SIZE +
13848        ROUND_UP_WORD (get_frame_size ()) +
13849        crtl->outgoing_args_size) >= 504)
13850     {
13851       /* This is the same as the code in thumb1_expand_prologue() which
13852          determines which register to use for stack decrement. */
13853       for (reg = LAST_ARG_REGNUM + 1; reg <= LAST_LO_REGNUM; reg++)
13854         if (mask & (1 << reg))
13855           break;
13856
13857       if (reg > LAST_LO_REGNUM)
13858         {
13859           /* Make sure we have a register available for stack decrement. */
13860           mask |= 1 << LAST_LO_REGNUM;
13861         }
13862     }
13863
13864   return mask;
13865 }
13866
13867
13868 /* Return the number of bytes required to save VFP registers.  */
13869 static int
13870 arm_get_vfp_saved_size (void)
13871 {
13872   unsigned int regno;
13873   int count;
13874   int saved;
13875
13876   saved = 0;
13877   /* Space for saved VFP registers.  */
13878   if (TARGET_HARD_FLOAT && TARGET_VFP)
13879     {
13880       count = 0;
13881       for (regno = FIRST_VFP_REGNUM;
13882            regno < LAST_VFP_REGNUM;
13883            regno += 2)
13884         {
13885           if ((!df_regs_ever_live_p (regno) || call_used_regs[regno])
13886               && (!df_regs_ever_live_p (regno + 1) || call_used_regs[regno + 1]))
13887             {
13888               if (count > 0)
13889                 {
13890                   /* Workaround ARM10 VFPr1 bug.  */
13891                   if (count == 2 && !arm_arch6)
13892                     count++;
13893                   saved += count * 8;
13894                 }
13895               count = 0;
13896             }
13897           else
13898             count++;
13899         }
13900       if (count > 0)
13901         {
13902           if (count == 2 && !arm_arch6)
13903             count++;
13904           saved += count * 8;
13905         }
13906     }
13907   return saved;
13908 }
13909
13910
13911 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
13912    everything bar the final return instruction.  */
13913 const char *
13914 output_return_instruction (rtx operand, int really_return, int reverse)
13915 {
13916   char conditional[10];
13917   char instr[100];
13918   unsigned reg;
13919   unsigned long live_regs_mask;
13920   unsigned long func_type;
13921   arm_stack_offsets *offsets;
13922
13923   func_type = arm_current_func_type ();
13924
13925   if (IS_NAKED (func_type))
13926     return "";
13927
13928   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
13929     {
13930       /* If this function was declared non-returning, and we have
13931          found a tail call, then we have to trust that the called
13932          function won't return.  */
13933       if (really_return)
13934         {
13935           rtx ops[2];
13936
13937           /* Otherwise, trap an attempted return by aborting.  */
13938           ops[0] = operand;
13939           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
13940                                        : "abort");
13941           assemble_external_libcall (ops[1]);
13942           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
13943         }
13944
13945       return "";
13946     }
13947
13948   gcc_assert (!cfun->calls_alloca || really_return);
13949
13950   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
13951
13952   cfun->machine->return_used_this_function = 1;
13953
13954   offsets = arm_get_frame_offsets ();
13955   live_regs_mask = offsets->saved_regs_mask;
13956
13957   if (live_regs_mask)
13958     {
13959       const char * return_reg;
13960
13961       /* If we do not have any special requirements for function exit
13962          (e.g. interworking) then we can load the return address
13963          directly into the PC.  Otherwise we must load it into LR.  */
13964       if (really_return
13965           && (IS_INTERRUPT (func_type) || !TARGET_INTERWORK))
13966         return_reg = reg_names[PC_REGNUM];
13967       else
13968         return_reg = reg_names[LR_REGNUM];
13969
13970       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
13971         {
13972           /* There are three possible reasons for the IP register
13973              being saved.  1) a stack frame was created, in which case
13974              IP contains the old stack pointer, or 2) an ISR routine
13975              corrupted it, or 3) it was saved to align the stack on
13976              iWMMXt.  In case 1, restore IP into SP, otherwise just
13977              restore IP.  */
13978           if (frame_pointer_needed)
13979             {
13980               live_regs_mask &= ~ (1 << IP_REGNUM);
13981               live_regs_mask |=   (1 << SP_REGNUM);
13982             }
13983           else
13984             gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
13985         }
13986
13987       /* On some ARM architectures it is faster to use LDR rather than
13988          LDM to load a single register.  On other architectures, the
13989          cost is the same.  In 26 bit mode, or for exception handlers,
13990          we have to use LDM to load the PC so that the CPSR is also
13991          restored.  */
13992       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
13993         if (live_regs_mask == (1U << reg))
13994           break;
13995
13996       if (reg <= LAST_ARM_REGNUM
13997           && (reg != LR_REGNUM
13998               || ! really_return
13999               || ! IS_INTERRUPT (func_type)))
14000         {
14001           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
14002                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
14003         }
14004       else
14005         {
14006           char *p;
14007           int first = 1;
14008
14009           /* Generate the load multiple instruction to restore the
14010              registers.  Note we can get here, even if
14011              frame_pointer_needed is true, but only if sp already
14012              points to the base of the saved core registers.  */
14013           if (live_regs_mask & (1 << SP_REGNUM))
14014             {
14015               unsigned HOST_WIDE_INT stack_adjust;
14016
14017               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
14018               gcc_assert (stack_adjust == 0 || stack_adjust == 4);
14019
14020               if (stack_adjust && arm_arch5 && TARGET_ARM)
14021                 if (TARGET_UNIFIED_ASM)
14022                   sprintf (instr, "ldmib%s\t%%|sp, {", conditional);
14023                 else
14024                   sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
14025               else
14026                 {
14027                   /* If we can't use ldmib (SA110 bug),
14028                      then try to pop r3 instead.  */
14029                   if (stack_adjust)
14030                     live_regs_mask |= 1 << 3;
14031                   
14032                   if (TARGET_UNIFIED_ASM)
14033                     sprintf (instr, "ldmfd%s\t%%|sp, {", conditional);
14034                   else
14035                     sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
14036                 }
14037             }
14038           else
14039             if (TARGET_UNIFIED_ASM)
14040               sprintf (instr, "pop%s\t{", conditional);
14041             else
14042               sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
14043
14044           p = instr + strlen (instr);
14045
14046           for (reg = 0; reg <= SP_REGNUM; reg++)
14047             if (live_regs_mask & (1 << reg))
14048               {
14049                 int l = strlen (reg_names[reg]);
14050
14051                 if (first)
14052                   first = 0;
14053                 else
14054                   {
14055                     memcpy (p, ", ", 2);
14056                     p += 2;
14057                   }
14058
14059                 memcpy (p, "%|", 2);
14060                 memcpy (p + 2, reg_names[reg], l);
14061                 p += l + 2;
14062               }
14063
14064           if (live_regs_mask & (1 << LR_REGNUM))
14065             {
14066               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
14067               /* If returning from an interrupt, restore the CPSR.  */
14068               if (IS_INTERRUPT (func_type))
14069                 strcat (p, "^");
14070             }
14071           else
14072             strcpy (p, "}");
14073         }
14074
14075       output_asm_insn (instr, & operand);
14076
14077       /* See if we need to generate an extra instruction to
14078          perform the actual function return.  */
14079       if (really_return
14080           && func_type != ARM_FT_INTERWORKED
14081           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
14082         {
14083           /* The return has already been handled
14084              by loading the LR into the PC.  */
14085           really_return = 0;
14086         }
14087     }
14088
14089   if (really_return)
14090     {
14091       switch ((int) ARM_FUNC_TYPE (func_type))
14092         {
14093         case ARM_FT_ISR:
14094         case ARM_FT_FIQ:
14095           /* ??? This is wrong for unified assembly syntax.  */
14096           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
14097           break;
14098
14099         case ARM_FT_INTERWORKED:
14100           sprintf (instr, "bx%s\t%%|lr", conditional);
14101           break;
14102
14103         case ARM_FT_EXCEPTION:
14104           /* ??? This is wrong for unified assembly syntax.  */
14105           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
14106           break;
14107
14108         default:
14109           /* Use bx if it's available.  */
14110           if (arm_arch5 || arm_arch4t)
14111             sprintf (instr, "bx%s\t%%|lr", conditional);
14112           else
14113             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
14114           break;
14115         }
14116
14117       output_asm_insn (instr, & operand);
14118     }
14119
14120   return "";
14121 }
14122
14123 /* Write the function name into the code section, directly preceding
14124    the function prologue.
14125
14126    Code will be output similar to this:
14127      t0
14128          .ascii "arm_poke_function_name", 0
14129          .align
14130      t1
14131          .word 0xff000000 + (t1 - t0)
14132      arm_poke_function_name
14133          mov     ip, sp
14134          stmfd   sp!, {fp, ip, lr, pc}
14135          sub     fp, ip, #4
14136
14137    When performing a stack backtrace, code can inspect the value
14138    of 'pc' stored at 'fp' + 0.  If the trace function then looks
14139    at location pc - 12 and the top 8 bits are set, then we know
14140    that there is a function name embedded immediately preceding this
14141    location and has length ((pc[-3]) & 0xff000000).
14142
14143    We assume that pc is declared as a pointer to an unsigned long.
14144
14145    It is of no benefit to output the function name if we are assembling
14146    a leaf function.  These function types will not contain a stack
14147    backtrace structure, therefore it is not possible to determine the
14148    function name.  */
14149 void
14150 arm_poke_function_name (FILE *stream, const char *name)
14151 {
14152   unsigned long alignlength;
14153   unsigned long length;
14154   rtx           x;
14155
14156   length      = strlen (name) + 1;
14157   alignlength = ROUND_UP_WORD (length);
14158
14159   ASM_OUTPUT_ASCII (stream, name, length);
14160   ASM_OUTPUT_ALIGN (stream, 2);
14161   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
14162   assemble_aligned_integer (UNITS_PER_WORD, x);
14163 }
14164
14165 /* Place some comments into the assembler stream
14166    describing the current function.  */
14167 static void
14168 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
14169 {
14170   unsigned long func_type;
14171
14172   if (TARGET_THUMB1)
14173     {
14174       thumb1_output_function_prologue (f, frame_size);
14175       return;
14176     }
14177
14178   /* Sanity check.  */
14179   gcc_assert (!arm_ccfsm_state && !arm_target_insn);
14180
14181   func_type = arm_current_func_type ();
14182
14183   switch ((int) ARM_FUNC_TYPE (func_type))
14184     {
14185     default:
14186     case ARM_FT_NORMAL:
14187       break;
14188     case ARM_FT_INTERWORKED:
14189       asm_fprintf (f, "\t%@ Function supports interworking.\n");
14190       break;
14191     case ARM_FT_ISR:
14192       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
14193       break;
14194     case ARM_FT_FIQ:
14195       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
14196       break;
14197     case ARM_FT_EXCEPTION:
14198       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
14199       break;
14200     }
14201
14202   if (IS_NAKED (func_type))
14203     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
14204
14205   if (IS_VOLATILE (func_type))
14206     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
14207
14208   if (IS_NESTED (func_type))
14209     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
14210   if (IS_STACKALIGN (func_type))
14211     asm_fprintf (f, "\t%@ Stack Align: May be called with mis-aligned SP.\n");
14212
14213   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
14214                crtl->args.size,
14215                crtl->args.pretend_args_size, frame_size);
14216
14217   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
14218                frame_pointer_needed,
14219                cfun->machine->uses_anonymous_args);
14220
14221   if (cfun->machine->lr_save_eliminated)
14222     asm_fprintf (f, "\t%@ link register save eliminated.\n");
14223
14224   if (crtl->calls_eh_return)
14225     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
14226
14227 }
14228
14229 const char *
14230 arm_output_epilogue (rtx sibling)
14231 {
14232   int reg;
14233   unsigned long saved_regs_mask;
14234   unsigned long func_type;
14235   /* Floats_offset is the offset from the "virtual" frame.  In an APCS
14236      frame that is $fp + 4 for a non-variadic function.  */
14237   int floats_offset = 0;
14238   rtx operands[3];
14239   FILE * f = asm_out_file;
14240   unsigned int lrm_count = 0;
14241   int really_return = (sibling == NULL);
14242   int start_reg;
14243   arm_stack_offsets *offsets;
14244
14245   /* If we have already generated the return instruction
14246      then it is futile to generate anything else.  */
14247   if (use_return_insn (FALSE, sibling) && 
14248       (cfun->machine->return_used_this_function != 0))
14249     return "";
14250
14251   func_type = arm_current_func_type ();
14252
14253   if (IS_NAKED (func_type))
14254     /* Naked functions don't have epilogues.  */
14255     return "";
14256
14257   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
14258     {
14259       rtx op;
14260
14261       /* A volatile function should never return.  Call abort.  */
14262       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
14263       assemble_external_libcall (op);
14264       output_asm_insn ("bl\t%a0", &op);
14265
14266       return "";
14267     }
14268
14269   /* If we are throwing an exception, then we really must be doing a
14270      return, so we can't tail-call.  */
14271   gcc_assert (!crtl->calls_eh_return || really_return);
14272
14273   offsets = arm_get_frame_offsets ();
14274   saved_regs_mask = offsets->saved_regs_mask;
14275
14276   if (TARGET_IWMMXT)
14277     lrm_count = bit_count (saved_regs_mask);
14278
14279   floats_offset = offsets->saved_args;
14280   /* Compute how far away the floats will be.  */
14281   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
14282     if (saved_regs_mask & (1 << reg))
14283       floats_offset += 4;
14284
14285   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
14286     {
14287       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
14288       int vfp_offset = offsets->frame;
14289
14290       if (TARGET_FPA_EMU2)
14291         {
14292           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
14293             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14294               {
14295                 floats_offset += 12;
14296                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
14297                              reg, FP_REGNUM, floats_offset - vfp_offset);
14298               }
14299         }
14300       else
14301         {
14302           start_reg = LAST_FPA_REGNUM;
14303
14304           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
14305             {
14306               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14307                 {
14308                   floats_offset += 12;
14309
14310                   /* We can't unstack more than four registers at once.  */
14311                   if (start_reg - reg == 3)
14312                     {
14313                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
14314                                    reg, FP_REGNUM, floats_offset - vfp_offset);
14315                       start_reg = reg - 1;
14316                     }
14317                 }
14318               else
14319                 {
14320                   if (reg != start_reg)
14321                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
14322                                  reg + 1, start_reg - reg,
14323                                  FP_REGNUM, floats_offset - vfp_offset);
14324                   start_reg = reg - 1;
14325                 }
14326             }
14327
14328           /* Just in case the last register checked also needs unstacking.  */
14329           if (reg != start_reg)
14330             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
14331                          reg + 1, start_reg - reg,
14332                          FP_REGNUM, floats_offset - vfp_offset);
14333         }
14334
14335       if (TARGET_HARD_FLOAT && TARGET_VFP)
14336         {
14337           int saved_size;
14338
14339           /* The fldmd insns do not have base+offset addressing
14340              modes, so we use IP to hold the address.  */
14341           saved_size = arm_get_vfp_saved_size ();
14342
14343           if (saved_size > 0)
14344             {
14345               floats_offset += saved_size;
14346               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
14347                            FP_REGNUM, floats_offset - vfp_offset);
14348             }
14349           start_reg = FIRST_VFP_REGNUM;
14350           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
14351             {
14352               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
14353                   && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
14354                 {
14355                   if (start_reg != reg)
14356                     vfp_output_fldmd (f, IP_REGNUM,
14357                                       (start_reg - FIRST_VFP_REGNUM) / 2,
14358                                       (reg - start_reg) / 2);
14359                   start_reg = reg + 2;
14360                 }
14361             }
14362           if (start_reg != reg)
14363             vfp_output_fldmd (f, IP_REGNUM,
14364                               (start_reg - FIRST_VFP_REGNUM) / 2,
14365                               (reg - start_reg) / 2);
14366         }
14367
14368       if (TARGET_IWMMXT)
14369         {
14370           /* The frame pointer is guaranteed to be non-double-word aligned.
14371              This is because it is set to (old_stack_pointer - 4) and the
14372              old_stack_pointer was double word aligned.  Thus the offset to
14373              the iWMMXt registers to be loaded must also be non-double-word
14374              sized, so that the resultant address *is* double-word aligned.
14375              We can ignore floats_offset since that was already included in
14376              the live_regs_mask.  */
14377           lrm_count += (lrm_count % 2 ? 2 : 1);
14378
14379           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
14380             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14381               {
14382                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
14383                              reg, FP_REGNUM, lrm_count * 4);
14384                 lrm_count += 2;
14385               }
14386         }
14387
14388       /* saved_regs_mask should contain the IP, which at the time of stack
14389          frame generation actually contains the old stack pointer.  So a
14390          quick way to unwind the stack is just pop the IP register directly
14391          into the stack pointer.  */
14392       gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
14393       saved_regs_mask &= ~ (1 << IP_REGNUM);
14394       saved_regs_mask |=   (1 << SP_REGNUM);
14395
14396       /* There are two registers left in saved_regs_mask - LR and PC.  We
14397          only need to restore the LR register (the return address), but to
14398          save time we can load it directly into the PC, unless we need a
14399          special function exit sequence, or we are not really returning.  */
14400       if (really_return
14401           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
14402           && !crtl->calls_eh_return)
14403         /* Delete the LR from the register mask, so that the LR on
14404            the stack is loaded into the PC in the register mask.  */
14405         saved_regs_mask &= ~ (1 << LR_REGNUM);
14406       else
14407         saved_regs_mask &= ~ (1 << PC_REGNUM);
14408
14409       /* We must use SP as the base register, because SP is one of the
14410          registers being restored.  If an interrupt or page fault
14411          happens in the ldm instruction, the SP might or might not
14412          have been restored.  That would be bad, as then SP will no
14413          longer indicate the safe area of stack, and we can get stack
14414          corruption.  Using SP as the base register means that it will
14415          be reset correctly to the original value, should an interrupt
14416          occur.  If the stack pointer already points at the right
14417          place, then omit the subtraction.  */
14418       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
14419           || cfun->calls_alloca)
14420         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
14421                      4 * bit_count (saved_regs_mask));
14422       print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask, 0);
14423
14424       if (IS_INTERRUPT (func_type))
14425         /* Interrupt handlers will have pushed the
14426            IP onto the stack, so restore it now.  */
14427         print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, 1 << IP_REGNUM, 0);
14428     }
14429   else
14430     {
14431       /* This branch is executed for ARM mode (non-apcs frames) and
14432          Thumb-2 mode. Frame layout is essentially the same for those
14433          cases, except that in ARM mode frame pointer points to the
14434          first saved register, while in Thumb-2 mode the frame pointer points
14435          to the last saved register.
14436
14437          It is possible to make frame pointer point to last saved
14438          register in both cases, and remove some conditionals below.
14439          That means that fp setup in prologue would be just "mov fp, sp"
14440          and sp restore in epilogue would be just "mov sp, fp", whereas
14441          now we have to use add/sub in those cases. However, the value
14442          of that would be marginal, as both mov and add/sub are 32-bit
14443          in ARM mode, and it would require extra conditionals
14444          in arm_expand_prologue to distingish ARM-apcs-frame case
14445          (where frame pointer is required to point at first register)
14446          and ARM-non-apcs-frame. Therefore, such change is postponed
14447          until real need arise.  */
14448       unsigned HOST_WIDE_INT amount;
14449       int rfe;
14450       /* Restore stack pointer if necessary.  */
14451       if (TARGET_ARM && frame_pointer_needed)
14452         {
14453           operands[0] = stack_pointer_rtx;
14454           operands[1] = hard_frame_pointer_rtx;
14455           
14456           operands[2] = GEN_INT (offsets->frame - offsets->saved_regs);
14457           output_add_immediate (operands);
14458         }
14459       else
14460         {
14461           if (frame_pointer_needed)
14462             {
14463               /* For Thumb-2 restore sp from the frame pointer.
14464                  Operand restrictions mean we have to incrememnt FP, then copy
14465                  to SP.  */
14466               amount = offsets->locals_base - offsets->saved_regs;
14467               operands[0] = hard_frame_pointer_rtx;
14468             }
14469           else
14470             {
14471               unsigned long count;
14472               operands[0] = stack_pointer_rtx;
14473               amount = offsets->outgoing_args - offsets->saved_regs;
14474               /* pop call clobbered registers if it avoids a
14475                  separate stack adjustment.  */
14476               count = offsets->saved_regs - offsets->saved_args;
14477               if (optimize_size
14478                   && count != 0
14479                   && !crtl->calls_eh_return
14480                   && bit_count(saved_regs_mask) * 4 == count
14481                   && !IS_INTERRUPT (func_type)
14482                   && !crtl->tail_call_emit)
14483                 {
14484                   unsigned long mask;
14485                   /* Preserve return values, of any size.  */
14486                   mask = (1 << ((arm_size_return_regs() + 3) / 4)) - 1;
14487                   mask ^= 0xf;
14488                   mask &= ~saved_regs_mask;
14489                   reg = 0;
14490                   while (bit_count (mask) * 4 > amount)
14491                     {
14492                       while ((mask & (1 << reg)) == 0)
14493                         reg++;
14494                       mask &= ~(1 << reg);
14495                     }
14496                   if (bit_count (mask) * 4 == amount) {
14497                       amount = 0;
14498                       saved_regs_mask |= mask;
14499                   }
14500                 }
14501             }
14502           
14503           if (amount)
14504             {
14505               operands[1] = operands[0];
14506               operands[2] = GEN_INT (amount);
14507               output_add_immediate (operands);
14508             }
14509           if (frame_pointer_needed)
14510             asm_fprintf (f, "\tmov\t%r, %r\n",
14511                          SP_REGNUM, HARD_FRAME_POINTER_REGNUM);
14512         }
14513
14514       if (TARGET_FPA_EMU2)
14515         {
14516           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
14517             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14518               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
14519                            reg, SP_REGNUM);
14520         }
14521       else
14522         {
14523           start_reg = FIRST_FPA_REGNUM;
14524
14525           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
14526             {
14527               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14528                 {
14529                   if (reg - start_reg == 3)
14530                     {
14531                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
14532                                    start_reg, SP_REGNUM);
14533                       start_reg = reg + 1;
14534                     }
14535                 }
14536               else
14537                 {
14538                   if (reg != start_reg)
14539                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
14540                                  start_reg, reg - start_reg,
14541                                  SP_REGNUM);
14542
14543                   start_reg = reg + 1;
14544                 }
14545             }
14546
14547           /* Just in case the last register checked also needs unstacking.  */
14548           if (reg != start_reg)
14549             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
14550                          start_reg, reg - start_reg, SP_REGNUM);
14551         }
14552
14553       if (TARGET_HARD_FLOAT && TARGET_VFP)
14554         {
14555           int end_reg = LAST_VFP_REGNUM + 1;
14556
14557           /* Scan the registers in reverse order.  We need to match
14558              any groupings made in the prologue and generate matching
14559              pop operations.  */
14560           for (reg = LAST_VFP_REGNUM - 1; reg >= FIRST_VFP_REGNUM; reg -= 2)
14561             {
14562               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
14563                   && (!df_regs_ever_live_p (reg + 1)
14564                       || call_used_regs[reg + 1]))
14565                 {
14566                   if (end_reg > reg + 2)
14567                     vfp_output_fldmd (f, SP_REGNUM,
14568                                       (reg + 2 - FIRST_VFP_REGNUM) / 2,
14569                                       (end_reg - (reg + 2)) / 2);
14570                   end_reg = reg;
14571                 }
14572             }
14573           if (end_reg > reg + 2)
14574             vfp_output_fldmd (f, SP_REGNUM, 0,
14575                               (end_reg - (reg + 2)) / 2);
14576         }
14577
14578       if (TARGET_IWMMXT)
14579         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
14580           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14581             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
14582
14583       /* If we can, restore the LR into the PC.  */
14584       if (ARM_FUNC_TYPE (func_type) != ARM_FT_INTERWORKED
14585           && (TARGET_ARM || ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
14586           && !IS_STACKALIGN (func_type)
14587           && really_return
14588           && crtl->args.pretend_args_size == 0
14589           && saved_regs_mask & (1 << LR_REGNUM)
14590           && !crtl->calls_eh_return)
14591         {
14592           saved_regs_mask &= ~ (1 << LR_REGNUM);
14593           saved_regs_mask |=   (1 << PC_REGNUM);
14594           rfe = IS_INTERRUPT (func_type);
14595         }
14596       else
14597         rfe = 0;
14598
14599       /* Load the registers off the stack.  If we only have one register
14600          to load use the LDR instruction - it is faster.  For Thumb-2
14601          always use pop and the assembler will pick the best instruction.*/
14602       if (TARGET_ARM && saved_regs_mask == (1 << LR_REGNUM)
14603           && !IS_INTERRUPT(func_type))
14604         {
14605           asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
14606         }
14607       else if (saved_regs_mask)
14608         {
14609           if (saved_regs_mask & (1 << SP_REGNUM))
14610             /* Note - write back to the stack register is not enabled
14611                (i.e. "ldmfd sp!...").  We know that the stack pointer is
14612                in the list of registers and if we add writeback the
14613                instruction becomes UNPREDICTABLE.  */
14614             print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask,
14615                              rfe);
14616           else if (TARGET_ARM)
14617             print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, saved_regs_mask,
14618                              rfe);
14619           else
14620             print_multi_reg (f, "pop\t", SP_REGNUM, saved_regs_mask, 0);
14621         }
14622
14623       if (crtl->args.pretend_args_size)
14624         {
14625           /* Unwind the pre-pushed regs.  */
14626           operands[0] = operands[1] = stack_pointer_rtx;
14627           operands[2] = GEN_INT (crtl->args.pretend_args_size);
14628           output_add_immediate (operands);
14629         }
14630     }
14631
14632   /* We may have already restored PC directly from the stack.  */
14633   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
14634     return "";
14635
14636   /* Stack adjustment for exception handler.  */
14637   if (crtl->calls_eh_return)
14638     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
14639                  ARM_EH_STACKADJ_REGNUM);
14640
14641   /* Generate the return instruction.  */
14642   switch ((int) ARM_FUNC_TYPE (func_type))
14643     {
14644     case ARM_FT_ISR:
14645     case ARM_FT_FIQ:
14646       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
14647       break;
14648
14649     case ARM_FT_EXCEPTION:
14650       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
14651       break;
14652
14653     case ARM_FT_INTERWORKED:
14654       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
14655       break;
14656
14657     default:
14658       if (IS_STACKALIGN (func_type))
14659         {
14660           /* See comment in arm_expand_prologue.  */
14661           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, 0);
14662         }
14663       if (arm_arch5 || arm_arch4t)
14664         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
14665       else
14666         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
14667       break;
14668     }
14669
14670   return "";
14671 }
14672
14673 static void
14674 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
14675                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
14676 {
14677   arm_stack_offsets *offsets;
14678
14679   if (TARGET_THUMB1)
14680     {
14681       int regno;
14682
14683       /* Emit any call-via-reg trampolines that are needed for v4t support
14684          of call_reg and call_value_reg type insns.  */
14685       for (regno = 0; regno < LR_REGNUM; regno++)
14686         {
14687           rtx label = cfun->machine->call_via[regno];
14688
14689           if (label != NULL)
14690             {
14691               switch_to_section (function_section (current_function_decl));
14692               targetm.asm_out.internal_label (asm_out_file, "L",
14693                                               CODE_LABEL_NUMBER (label));
14694               asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
14695             }
14696         }
14697
14698       /* ??? Probably not safe to set this here, since it assumes that a
14699          function will be emitted as assembly immediately after we generate
14700          RTL for it.  This does not happen for inline functions.  */
14701       cfun->machine->return_used_this_function = 0;
14702     }
14703   else /* TARGET_32BIT */
14704     {
14705       /* We need to take into account any stack-frame rounding.  */
14706       offsets = arm_get_frame_offsets ();
14707
14708       gcc_assert (!use_return_insn (FALSE, NULL)
14709                   || (cfun->machine->return_used_this_function != 0)
14710                   || offsets->saved_regs == offsets->outgoing_args
14711                   || frame_pointer_needed);
14712
14713       /* Reset the ARM-specific per-function variables.  */
14714       after_arm_reorg = 0;
14715     }
14716 }
14717
14718 /* Generate and emit an insn that we will recognize as a push_multi.
14719    Unfortunately, since this insn does not reflect very well the actual
14720    semantics of the operation, we need to annotate the insn for the benefit
14721    of DWARF2 frame unwind information.  */
14722 static rtx
14723 emit_multi_reg_push (unsigned long mask)
14724 {
14725   int num_regs = 0;
14726   int num_dwarf_regs;
14727   int i, j;
14728   rtx par;
14729   rtx dwarf;
14730   int dwarf_par_index;
14731   rtx tmp, reg;
14732
14733   for (i = 0; i <= LAST_ARM_REGNUM; i++)
14734     if (mask & (1 << i))
14735       num_regs++;
14736
14737   gcc_assert (num_regs && num_regs <= 16);
14738
14739   /* We don't record the PC in the dwarf frame information.  */
14740   num_dwarf_regs = num_regs;
14741   if (mask & (1 << PC_REGNUM))
14742     num_dwarf_regs--;
14743
14744   /* For the body of the insn we are going to generate an UNSPEC in
14745      parallel with several USEs.  This allows the insn to be recognized
14746      by the push_multi pattern in the arm.md file.
14747
14748      The body of the insn looks something like this:
14749
14750        (parallel [
14751            (set (mem:BLK (pre_modify:SI (reg:SI sp)
14752                                         (const_int:SI <num>)))
14753                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
14754            (use (reg:SI XX))
14755            (use (reg:SI YY))
14756            ...
14757         ])
14758
14759      For the frame note however, we try to be more explicit and actually
14760      show each register being stored into the stack frame, plus a (single)
14761      decrement of the stack pointer.  We do it this way in order to be
14762      friendly to the stack unwinding code, which only wants to see a single
14763      stack decrement per instruction.  The RTL we generate for the note looks
14764      something like this:
14765
14766       (sequence [
14767            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
14768            (set (mem:SI (reg:SI sp)) (reg:SI r4))
14769            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI XX))
14770            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI YY))
14771            ...
14772         ])
14773
14774      FIXME:: In an ideal world the PRE_MODIFY would not exist and
14775      instead we'd have a parallel expression detailing all
14776      the stores to the various memory addresses so that debug
14777      information is more up-to-date. Remember however while writing
14778      this to take care of the constraints with the push instruction.
14779
14780      Note also that this has to be taken care of for the VFP registers.
14781
14782      For more see PR43399.  */
14783
14784   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
14785   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
14786   dwarf_par_index = 1;
14787
14788   for (i = 0; i <= LAST_ARM_REGNUM; i++)
14789     {
14790       if (mask & (1 << i))
14791         {
14792           reg = gen_rtx_REG (SImode, i);
14793
14794           XVECEXP (par, 0, 0)
14795             = gen_rtx_SET (VOIDmode,
14796                            gen_frame_mem
14797                            (BLKmode,
14798                             gen_rtx_PRE_MODIFY (Pmode,
14799                                                 stack_pointer_rtx,
14800                                                 plus_constant
14801                                                 (stack_pointer_rtx,
14802                                                  -4 * num_regs))
14803                             ),
14804                            gen_rtx_UNSPEC (BLKmode,
14805                                            gen_rtvec (1, reg),
14806                                            UNSPEC_PUSH_MULT));
14807
14808           if (i != PC_REGNUM)
14809             {
14810               tmp = gen_rtx_SET (VOIDmode,
14811                                  gen_frame_mem (SImode, stack_pointer_rtx),
14812                                  reg);
14813               RTX_FRAME_RELATED_P (tmp) = 1;
14814               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
14815               dwarf_par_index++;
14816             }
14817
14818           break;
14819         }
14820     }
14821
14822   for (j = 1, i++; j < num_regs; i++)
14823     {
14824       if (mask & (1 << i))
14825         {
14826           reg = gen_rtx_REG (SImode, i);
14827
14828           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
14829
14830           if (i != PC_REGNUM)
14831             {
14832               tmp
14833                 = gen_rtx_SET (VOIDmode,
14834                                gen_frame_mem
14835                                (SImode,
14836                                 plus_constant (stack_pointer_rtx,
14837                                                4 * j)),
14838                                reg);
14839               RTX_FRAME_RELATED_P (tmp) = 1;
14840               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
14841             }
14842
14843           j++;
14844         }
14845     }
14846
14847   par = emit_insn (par);
14848
14849   tmp = gen_rtx_SET (VOIDmode,
14850                      stack_pointer_rtx,
14851                      plus_constant (stack_pointer_rtx, -4 * num_regs));
14852   RTX_FRAME_RELATED_P (tmp) = 1;
14853   XVECEXP (dwarf, 0, 0) = tmp;
14854
14855   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
14856
14857   return par;
14858 }
14859
14860 /* Calculate the size of the return value that is passed in registers.  */
14861 static unsigned
14862 arm_size_return_regs (void)
14863 {
14864   enum machine_mode mode;
14865
14866   if (crtl->return_rtx != 0)
14867     mode = GET_MODE (crtl->return_rtx);
14868   else
14869     mode = DECL_MODE (DECL_RESULT (current_function_decl));
14870
14871   return GET_MODE_SIZE (mode);
14872 }
14873
14874 static rtx
14875 emit_sfm (int base_reg, int count)
14876 {
14877   rtx par;
14878   rtx dwarf;
14879   rtx tmp, reg;
14880   int i;
14881
14882   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
14883   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
14884
14885   reg = gen_rtx_REG (XFmode, base_reg++);
14886
14887   XVECEXP (par, 0, 0)
14888     = gen_rtx_SET (VOIDmode,
14889                    gen_frame_mem
14890                    (BLKmode,
14891                     gen_rtx_PRE_MODIFY (Pmode,
14892                                         stack_pointer_rtx,
14893                                         plus_constant
14894                                         (stack_pointer_rtx,
14895                                          -12 * count))
14896                     ),
14897                    gen_rtx_UNSPEC (BLKmode,
14898                                    gen_rtvec (1, reg),
14899                                    UNSPEC_PUSH_MULT));
14900   tmp = gen_rtx_SET (VOIDmode,
14901                      gen_frame_mem (XFmode, stack_pointer_rtx), reg);
14902   RTX_FRAME_RELATED_P (tmp) = 1;
14903   XVECEXP (dwarf, 0, 1) = tmp;
14904
14905   for (i = 1; i < count; i++)
14906     {
14907       reg = gen_rtx_REG (XFmode, base_reg++);
14908       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
14909
14910       tmp = gen_rtx_SET (VOIDmode,
14911                          gen_frame_mem (XFmode,
14912                                         plus_constant (stack_pointer_rtx,
14913                                                        i * 12)),
14914                          reg);
14915       RTX_FRAME_RELATED_P (tmp) = 1;
14916       XVECEXP (dwarf, 0, i + 1) = tmp;
14917     }
14918
14919   tmp = gen_rtx_SET (VOIDmode,
14920                      stack_pointer_rtx,
14921                      plus_constant (stack_pointer_rtx, -12 * count));
14922
14923   RTX_FRAME_RELATED_P (tmp) = 1;
14924   XVECEXP (dwarf, 0, 0) = tmp;
14925
14926   par = emit_insn (par);
14927   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
14928
14929   return par;
14930 }
14931
14932
14933 /* Return true if the current function needs to save/restore LR.  */
14934
14935 static bool
14936 thumb_force_lr_save (void)
14937 {
14938   return !cfun->machine->lr_save_eliminated
14939          && (!leaf_function_p ()
14940              || thumb_far_jump_used_p ()
14941              || df_regs_ever_live_p (LR_REGNUM));
14942 }
14943
14944
14945 /* Compute the distance from register FROM to register TO.
14946    These can be the arg pointer (26), the soft frame pointer (25),
14947    the stack pointer (13) or the hard frame pointer (11).
14948    In thumb mode r7 is used as the soft frame pointer, if needed.
14949    Typical stack layout looks like this:
14950
14951        old stack pointer -> |    |
14952                              ----
14953                             |    | \
14954                             |    |   saved arguments for
14955                             |    |   vararg functions
14956                             |    | /
14957                               --
14958    hard FP & arg pointer -> |    | \
14959                             |    |   stack
14960                             |    |   frame
14961                             |    | /
14962                               --
14963                             |    | \
14964                             |    |   call saved
14965                             |    |   registers
14966       soft frame pointer -> |    | /
14967                               --
14968                             |    | \
14969                             |    |   local
14970                             |    |   variables
14971      locals base pointer -> |    | /
14972                               --
14973                             |    | \
14974                             |    |   outgoing
14975                             |    |   arguments
14976    current stack pointer -> |    | /
14977                               --
14978
14979   For a given function some or all of these stack components
14980   may not be needed, giving rise to the possibility of
14981   eliminating some of the registers.
14982
14983   The values returned by this function must reflect the behavior
14984   of arm_expand_prologue() and arm_compute_save_reg_mask().
14985
14986   The sign of the number returned reflects the direction of stack
14987   growth, so the values are positive for all eliminations except
14988   from the soft frame pointer to the hard frame pointer.
14989
14990   SFP may point just inside the local variables block to ensure correct
14991   alignment.  */
14992
14993
14994 /* Calculate stack offsets.  These are used to calculate register elimination
14995    offsets and in prologue/epilogue code.  Also calculates which registers
14996    should be saved.  */
14997
14998 static arm_stack_offsets *
14999 arm_get_frame_offsets (void)
15000 {
15001   struct arm_stack_offsets *offsets;
15002   unsigned long func_type;
15003   int leaf;
15004   int saved;
15005   int core_saved;
15006   HOST_WIDE_INT frame_size;
15007   int i;
15008
15009   offsets = &cfun->machine->stack_offsets;
15010
15011   /* We need to know if we are a leaf function.  Unfortunately, it
15012      is possible to be called after start_sequence has been called,
15013      which causes get_insns to return the insns for the sequence,
15014      not the function, which will cause leaf_function_p to return
15015      the incorrect result.
15016
15017      to know about leaf functions once reload has completed, and the
15018      frame size cannot be changed after that time, so we can safely
15019      use the cached value.  */
15020
15021   if (reload_completed)
15022     return offsets;
15023
15024   /* Initially this is the size of the local variables.  It will translated
15025      into an offset once we have determined the size of preceding data.  */
15026   frame_size = ROUND_UP_WORD (get_frame_size ());
15027
15028   leaf = leaf_function_p ();
15029
15030   /* Space for variadic functions.  */
15031   offsets->saved_args = crtl->args.pretend_args_size;
15032
15033   /* In Thumb mode this is incorrect, but never used.  */
15034   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0) +
15035                    arm_compute_static_chain_stack_bytes();
15036
15037   if (TARGET_32BIT)
15038     {
15039       unsigned int regno;
15040
15041       offsets->saved_regs_mask = arm_compute_save_reg_mask ();
15042       core_saved = bit_count (offsets->saved_regs_mask) * 4;
15043       saved = core_saved;
15044
15045       /* We know that SP will be doubleword aligned on entry, and we must
15046          preserve that condition at any subroutine call.  We also require the
15047          soft frame pointer to be doubleword aligned.  */
15048
15049       if (TARGET_REALLY_IWMMXT)
15050         {
15051           /* Check for the call-saved iWMMXt registers.  */
15052           for (regno = FIRST_IWMMXT_REGNUM;
15053                regno <= LAST_IWMMXT_REGNUM;
15054                regno++)
15055             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
15056               saved += 8;
15057         }
15058
15059       func_type = arm_current_func_type ();
15060       if (! IS_VOLATILE (func_type))
15061         {
15062           /* Space for saved FPA registers.  */
15063           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
15064             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
15065             saved += 12;
15066
15067           /* Space for saved VFP registers.  */
15068           if (TARGET_HARD_FLOAT && TARGET_VFP)
15069             saved += arm_get_vfp_saved_size ();
15070         }
15071     }
15072   else /* TARGET_THUMB1 */
15073     {
15074       offsets->saved_regs_mask = thumb1_compute_save_reg_mask ();
15075       core_saved = bit_count (offsets->saved_regs_mask) * 4;
15076       saved = core_saved;
15077       if (TARGET_BACKTRACE)
15078         saved += 16;
15079     }
15080
15081   /* Saved registers include the stack frame.  */
15082   offsets->saved_regs = offsets->saved_args + saved +
15083                         arm_compute_static_chain_stack_bytes();
15084   offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
15085   /* A leaf function does not need any stack alignment if it has nothing
15086      on the stack.  */
15087   if (leaf && frame_size == 0)
15088     {
15089       offsets->outgoing_args = offsets->soft_frame;
15090       offsets->locals_base = offsets->soft_frame;
15091       return offsets;
15092     }
15093
15094   /* Ensure SFP has the correct alignment.  */
15095   if (ARM_DOUBLEWORD_ALIGN
15096       && (offsets->soft_frame & 7))
15097     {
15098       offsets->soft_frame += 4;
15099       /* Try to align stack by pushing an extra reg.  Don't bother doing this
15100          when there is a stack frame as the alignment will be rolled into
15101          the normal stack adjustment.  */
15102       if (frame_size + crtl->outgoing_args_size == 0)
15103         {
15104           int reg = -1;
15105
15106           /* If it is safe to use r3, then do so.  This sometimes 
15107              generates better code on Thumb-2 by avoiding the need to
15108              use 32-bit push/pop instructions.  */
15109           if (!crtl->tail_call_emit
15110               && arm_size_return_regs () <= 12
15111               && (offsets->saved_regs_mask & (1 << 3)) == 0)
15112             {
15113               reg = 3;
15114             }
15115           else
15116             for (i = 4; i <= (TARGET_THUMB1 ? LAST_LO_REGNUM : 11); i++)
15117               {
15118                 if ((offsets->saved_regs_mask & (1 << i)) == 0)
15119                   {
15120                     reg = i;
15121                     break;
15122                   }
15123               }
15124
15125           if (reg != -1)
15126             {
15127               offsets->saved_regs += 4;
15128               offsets->saved_regs_mask |= (1 << reg);
15129             }
15130         }
15131     }
15132
15133   offsets->locals_base = offsets->soft_frame + frame_size;
15134   offsets->outgoing_args = (offsets->locals_base
15135                             + crtl->outgoing_args_size);
15136
15137   if (ARM_DOUBLEWORD_ALIGN)
15138     {
15139       /* Ensure SP remains doubleword aligned.  */
15140       if (offsets->outgoing_args & 7)
15141         offsets->outgoing_args += 4;
15142       gcc_assert (!(offsets->outgoing_args & 7));
15143     }
15144
15145   return offsets;
15146 }
15147
15148
15149 /* Calculate the relative offsets for the different stack pointers.  Positive
15150    offsets are in the direction of stack growth.  */
15151
15152 HOST_WIDE_INT
15153 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
15154 {
15155   arm_stack_offsets *offsets;
15156
15157   offsets = arm_get_frame_offsets ();
15158
15159   /* OK, now we have enough information to compute the distances.
15160      There must be an entry in these switch tables for each pair
15161      of registers in ELIMINABLE_REGS, even if some of the entries
15162      seem to be redundant or useless.  */
15163   switch (from)
15164     {
15165     case ARG_POINTER_REGNUM:
15166       switch (to)
15167         {
15168         case THUMB_HARD_FRAME_POINTER_REGNUM:
15169           return 0;
15170
15171         case FRAME_POINTER_REGNUM:
15172           /* This is the reverse of the soft frame pointer
15173              to hard frame pointer elimination below.  */
15174           return offsets->soft_frame - offsets->saved_args;
15175
15176         case ARM_HARD_FRAME_POINTER_REGNUM:
15177           /* This is only non-zero in the case where the static chain register
15178              is stored above the frame.  */
15179           return offsets->frame - offsets->saved_args - 4;
15180
15181         case STACK_POINTER_REGNUM:
15182           /* If nothing has been pushed on the stack at all
15183              then this will return -4.  This *is* correct!  */
15184           return offsets->outgoing_args - (offsets->saved_args + 4);
15185
15186         default:
15187           gcc_unreachable ();
15188         }
15189       gcc_unreachable ();
15190
15191     case FRAME_POINTER_REGNUM:
15192       switch (to)
15193         {
15194         case THUMB_HARD_FRAME_POINTER_REGNUM:
15195           return 0;
15196
15197         case ARM_HARD_FRAME_POINTER_REGNUM:
15198           /* The hard frame pointer points to the top entry in the
15199              stack frame.  The soft frame pointer to the bottom entry
15200              in the stack frame.  If there is no stack frame at all,
15201              then they are identical.  */
15202
15203           return offsets->frame - offsets->soft_frame;
15204
15205         case STACK_POINTER_REGNUM:
15206           return offsets->outgoing_args - offsets->soft_frame;
15207
15208         default:
15209           gcc_unreachable ();
15210         }
15211       gcc_unreachable ();
15212
15213     default:
15214       /* You cannot eliminate from the stack pointer.
15215          In theory you could eliminate from the hard frame
15216          pointer to the stack pointer, but this will never
15217          happen, since if a stack frame is not needed the
15218          hard frame pointer will never be used.  */
15219       gcc_unreachable ();
15220     }
15221 }
15222
15223 /* Given FROM and TO register numbers, say whether this elimination is
15224    allowed.  Frame pointer elimination is automatically handled.
15225
15226    All eliminations are permissible.  Note that ARG_POINTER_REGNUM and
15227    HARD_FRAME_POINTER_REGNUM are in fact the same thing.  If we need a frame
15228    pointer, we must eliminate FRAME_POINTER_REGNUM into
15229    HARD_FRAME_POINTER_REGNUM and not into STACK_POINTER_REGNUM or
15230    ARG_POINTER_REGNUM.  */
15231
15232 bool
15233 arm_can_eliminate (const int from, const int to)
15234 {
15235   return ((to == FRAME_POINTER_REGNUM && from == ARG_POINTER_REGNUM) ? false :
15236           (to == STACK_POINTER_REGNUM && frame_pointer_needed) ? false :
15237           (to == ARM_HARD_FRAME_POINTER_REGNUM && TARGET_THUMB) ? false :
15238           (to == THUMB_HARD_FRAME_POINTER_REGNUM && TARGET_ARM) ? false :
15239            true);
15240 }
15241
15242 /* Emit RTL to save coprocessor registers on function entry.  Returns the
15243    number of bytes pushed.  */
15244
15245 static int
15246 arm_save_coproc_regs(void)
15247 {
15248   int saved_size = 0;
15249   unsigned reg;
15250   unsigned start_reg;
15251   rtx insn;
15252
15253   for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
15254     if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
15255       {
15256         insn = gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx);
15257         insn = gen_rtx_MEM (V2SImode, insn);
15258         insn = emit_set_insn (insn, gen_rtx_REG (V2SImode, reg));
15259         RTX_FRAME_RELATED_P (insn) = 1;
15260         saved_size += 8;
15261       }
15262
15263   /* Save any floating point call-saved registers used by this
15264      function.  */
15265   if (TARGET_FPA_EMU2)
15266     {
15267       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
15268         if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
15269           {
15270             insn = gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx);
15271             insn = gen_rtx_MEM (XFmode, insn);
15272             insn = emit_set_insn (insn, gen_rtx_REG (XFmode, reg));
15273             RTX_FRAME_RELATED_P (insn) = 1;
15274             saved_size += 12;
15275           }
15276     }
15277   else
15278     {
15279       start_reg = LAST_FPA_REGNUM;
15280
15281       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
15282         {
15283           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
15284             {
15285               if (start_reg - reg == 3)
15286                 {
15287                   insn = emit_sfm (reg, 4);
15288                   RTX_FRAME_RELATED_P (insn) = 1;
15289                   saved_size += 48;
15290                   start_reg = reg - 1;
15291                 }
15292             }
15293           else
15294             {
15295               if (start_reg != reg)
15296                 {
15297                   insn = emit_sfm (reg + 1, start_reg - reg);
15298                   RTX_FRAME_RELATED_P (insn) = 1;
15299                   saved_size += (start_reg - reg) * 12;
15300                 }
15301               start_reg = reg - 1;
15302             }
15303         }
15304
15305       if (start_reg != reg)
15306         {
15307           insn = emit_sfm (reg + 1, start_reg - reg);
15308           saved_size += (start_reg - reg) * 12;
15309           RTX_FRAME_RELATED_P (insn) = 1;
15310         }
15311     }
15312   if (TARGET_HARD_FLOAT && TARGET_VFP)
15313     {
15314       start_reg = FIRST_VFP_REGNUM;
15315
15316       for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
15317         {
15318           if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
15319               && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
15320             {
15321               if (start_reg != reg)
15322                 saved_size += vfp_emit_fstmd (start_reg,
15323                                               (reg - start_reg) / 2);
15324               start_reg = reg + 2;
15325             }
15326         }
15327       if (start_reg != reg)
15328         saved_size += vfp_emit_fstmd (start_reg,
15329                                       (reg - start_reg) / 2);
15330     }
15331   return saved_size;
15332 }
15333
15334
15335 /* Set the Thumb frame pointer from the stack pointer.  */
15336
15337 static void
15338 thumb_set_frame_pointer (arm_stack_offsets *offsets)
15339 {
15340   HOST_WIDE_INT amount;
15341   rtx insn, dwarf;
15342
15343   amount = offsets->outgoing_args - offsets->locals_base;
15344   if (amount < 1024)
15345     insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15346                                   stack_pointer_rtx, GEN_INT (amount)));
15347   else
15348     {
15349       emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
15350       /* Thumb-2 RTL patterns expect sp as the first input.  Thumb-1
15351          expects the first two operands to be the same.  */
15352       if (TARGET_THUMB2)
15353         {
15354           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15355                                         stack_pointer_rtx,
15356                                         hard_frame_pointer_rtx));
15357         }
15358       else
15359         {
15360           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15361                                         hard_frame_pointer_rtx,
15362                                         stack_pointer_rtx));
15363         }
15364       dwarf = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
15365                            plus_constant (stack_pointer_rtx, amount));
15366       RTX_FRAME_RELATED_P (dwarf) = 1;
15367       add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
15368     }
15369
15370   RTX_FRAME_RELATED_P (insn) = 1;
15371 }
15372
15373 /* Generate the prologue instructions for entry into an ARM or Thumb-2
15374    function.  */
15375 void
15376 arm_expand_prologue (void)
15377 {
15378   rtx amount;
15379   rtx insn;
15380   rtx ip_rtx;
15381   unsigned long live_regs_mask;
15382   unsigned long func_type;
15383   int fp_offset = 0;
15384   int saved_pretend_args = 0;
15385   int saved_regs = 0;
15386   unsigned HOST_WIDE_INT args_to_push;
15387   arm_stack_offsets *offsets;
15388
15389   func_type = arm_current_func_type ();
15390
15391   /* Naked functions don't have prologues.  */
15392   if (IS_NAKED (func_type))
15393     return;
15394
15395   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
15396   args_to_push = crtl->args.pretend_args_size;
15397
15398   /* Compute which register we will have to save onto the stack.  */
15399   offsets = arm_get_frame_offsets ();
15400   live_regs_mask = offsets->saved_regs_mask;
15401
15402   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
15403
15404   if (IS_STACKALIGN (func_type))
15405     {
15406       rtx dwarf;
15407       rtx r0;
15408       rtx r1;
15409       /* Handle a word-aligned stack pointer.  We generate the following:
15410
15411           mov r0, sp
15412           bic r1, r0, #7
15413           mov sp, r1
15414           <save and restore r0 in normal prologue/epilogue>
15415           mov sp, r0
15416           bx lr
15417
15418          The unwinder doesn't need to know about the stack realignment.
15419          Just tell it we saved SP in r0.  */
15420       gcc_assert (TARGET_THUMB2 && !arm_arch_notm && args_to_push == 0);
15421
15422       r0 = gen_rtx_REG (SImode, 0);
15423       r1 = gen_rtx_REG (SImode, 1);
15424       /* Use a real rtvec rather than NULL_RTVEC so the rest of the
15425          compiler won't choke.  */
15426       dwarf = gen_rtx_UNSPEC (SImode, rtvec_alloc (0), UNSPEC_STACK_ALIGN);
15427       dwarf = gen_rtx_SET (VOIDmode, r0, dwarf);
15428       insn = gen_movsi (r0, stack_pointer_rtx);
15429       RTX_FRAME_RELATED_P (insn) = 1;
15430       add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
15431       emit_insn (insn);
15432       emit_insn (gen_andsi3 (r1, r0, GEN_INT (~(HOST_WIDE_INT)7)));
15433       emit_insn (gen_movsi (stack_pointer_rtx, r1));
15434     }
15435
15436   /* For APCS frames, if IP register is clobbered
15437      when creating frame, save that register in a special
15438      way.  */
15439   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
15440     {
15441       if (IS_INTERRUPT (func_type))
15442         {
15443           /* Interrupt functions must not corrupt any registers.
15444              Creating a frame pointer however, corrupts the IP
15445              register, so we must push it first.  */
15446           insn = emit_multi_reg_push (1 << IP_REGNUM);
15447
15448           /* Do not set RTX_FRAME_RELATED_P on this insn.
15449              The dwarf stack unwinding code only wants to see one
15450              stack decrement per function, and this is not it.  If
15451              this instruction is labeled as being part of the frame
15452              creation sequence then dwarf2out_frame_debug_expr will
15453              die when it encounters the assignment of IP to FP
15454              later on, since the use of SP here establishes SP as
15455              the CFA register and not IP.
15456
15457              Anyway this instruction is not really part of the stack
15458              frame creation although it is part of the prologue.  */
15459         }
15460       else if (IS_NESTED (func_type))
15461         {
15462           /* The Static chain register is the same as the IP register
15463              used as a scratch register during stack frame creation.
15464              To get around this need to find somewhere to store IP
15465              whilst the frame is being created.  We try the following
15466              places in order:
15467
15468                1. The last argument register.
15469                2. A slot on the stack above the frame.  (This only
15470                   works if the function is not a varargs function).
15471                3. Register r3, after pushing the argument registers
15472                   onto the stack.
15473
15474              Note - we only need to tell the dwarf2 backend about the SP
15475              adjustment in the second variant; the static chain register
15476              doesn't need to be unwound, as it doesn't contain a value
15477              inherited from the caller.  */
15478
15479           if (df_regs_ever_live_p (3) == false)
15480             insn = emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
15481           else if (args_to_push == 0)
15482             {
15483               rtx dwarf;
15484
15485               gcc_assert(arm_compute_static_chain_stack_bytes() == 4);
15486               saved_regs += 4;
15487
15488               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
15489               insn = emit_set_insn (gen_frame_mem (SImode, insn), ip_rtx);
15490               fp_offset = 4;
15491
15492               /* Just tell the dwarf backend that we adjusted SP.  */
15493               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
15494                                    plus_constant (stack_pointer_rtx,
15495                                                   -fp_offset));
15496               RTX_FRAME_RELATED_P (insn) = 1;
15497               add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
15498             }
15499           else
15500             {
15501               /* Store the args on the stack.  */
15502               if (cfun->machine->uses_anonymous_args)
15503                 insn = emit_multi_reg_push
15504                   ((0xf0 >> (args_to_push / 4)) & 0xf);
15505               else
15506                 insn = emit_insn
15507                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
15508                                GEN_INT (- args_to_push)));
15509
15510               RTX_FRAME_RELATED_P (insn) = 1;
15511
15512               saved_pretend_args = 1;
15513               fp_offset = args_to_push;
15514               args_to_push = 0;
15515
15516               /* Now reuse r3 to preserve IP.  */
15517               emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
15518             }
15519         }
15520
15521       insn = emit_set_insn (ip_rtx,
15522                             plus_constant (stack_pointer_rtx, fp_offset));
15523       RTX_FRAME_RELATED_P (insn) = 1;
15524     }
15525
15526   if (args_to_push)
15527     {
15528       /* Push the argument registers, or reserve space for them.  */
15529       if (cfun->machine->uses_anonymous_args)
15530         insn = emit_multi_reg_push
15531           ((0xf0 >> (args_to_push / 4)) & 0xf);
15532       else
15533         insn = emit_insn
15534           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
15535                        GEN_INT (- args_to_push)));
15536       RTX_FRAME_RELATED_P (insn) = 1;
15537     }
15538
15539   /* If this is an interrupt service routine, and the link register
15540      is going to be pushed, and we're not generating extra
15541      push of IP (needed when frame is needed and frame layout if apcs),
15542      subtracting four from LR now will mean that the function return
15543      can be done with a single instruction.  */
15544   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
15545       && (live_regs_mask & (1 << LR_REGNUM)) != 0
15546       && !(frame_pointer_needed && TARGET_APCS_FRAME)
15547       && TARGET_ARM)
15548     {
15549       rtx lr = gen_rtx_REG (SImode, LR_REGNUM);
15550       
15551       emit_set_insn (lr, plus_constant (lr, -4));
15552     }
15553
15554   if (live_regs_mask)
15555     {
15556       saved_regs += bit_count (live_regs_mask) * 4;
15557       if (optimize_size && !frame_pointer_needed
15558           && saved_regs == offsets->saved_regs - offsets->saved_args)
15559         {
15560           /* If no coprocessor registers are being pushed and we don't have
15561              to worry about a frame pointer then push extra registers to
15562              create the stack frame.  This is done is a way that does not
15563              alter the frame layout, so is independent of the epilogue.  */
15564           int n;
15565           int frame;
15566           n = 0;
15567           while (n < 8 && (live_regs_mask & (1 << n)) == 0)
15568             n++;
15569           frame = offsets->outgoing_args - (offsets->saved_args + saved_regs);
15570           if (frame && n * 4 >= frame)
15571             {
15572               n = frame / 4;
15573               live_regs_mask |= (1 << n) - 1;
15574               saved_regs += frame;
15575             }
15576         }
15577       insn = emit_multi_reg_push (live_regs_mask);
15578       RTX_FRAME_RELATED_P (insn) = 1;
15579     }
15580
15581   if (! IS_VOLATILE (func_type))
15582     saved_regs += arm_save_coproc_regs ();
15583
15584   if (frame_pointer_needed && TARGET_ARM)
15585     {
15586       /* Create the new frame pointer.  */
15587       if (TARGET_APCS_FRAME)
15588         {
15589           insn = GEN_INT (-(4 + args_to_push + fp_offset));
15590           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
15591           RTX_FRAME_RELATED_P (insn) = 1;
15592
15593           if (IS_NESTED (func_type))
15594             {
15595               /* Recover the static chain register.  */
15596               if (!df_regs_ever_live_p (3)
15597                   || saved_pretend_args)
15598                 insn = gen_rtx_REG (SImode, 3);
15599               else /* if (crtl->args.pretend_args_size == 0) */
15600                 {
15601                   insn = plus_constant (hard_frame_pointer_rtx, 4);
15602                   insn = gen_frame_mem (SImode, insn);
15603                 }
15604               emit_set_insn (ip_rtx, insn);
15605               /* Add a USE to stop propagate_one_insn() from barfing.  */
15606               emit_insn (gen_prologue_use (ip_rtx));
15607             }
15608         }
15609       else
15610         {
15611           insn = GEN_INT (saved_regs - 4);
15612           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15613                                         stack_pointer_rtx, insn));
15614           RTX_FRAME_RELATED_P (insn) = 1;
15615         }
15616     }
15617
15618   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
15619     {
15620       /* This add can produce multiple insns for a large constant, so we
15621          need to get tricky.  */
15622       rtx last = get_last_insn ();
15623
15624       amount = GEN_INT (offsets->saved_args + saved_regs
15625                         - offsets->outgoing_args);
15626
15627       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
15628                                     amount));
15629       do
15630         {
15631           last = last ? NEXT_INSN (last) : get_insns ();
15632           RTX_FRAME_RELATED_P (last) = 1;
15633         }
15634       while (last != insn);
15635
15636       /* If the frame pointer is needed, emit a special barrier that
15637          will prevent the scheduler from moving stores to the frame
15638          before the stack adjustment.  */
15639       if (frame_pointer_needed)
15640         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
15641                                          hard_frame_pointer_rtx));
15642     }
15643
15644
15645   if (frame_pointer_needed && TARGET_THUMB2)
15646     thumb_set_frame_pointer (offsets);
15647
15648   if (flag_pic && arm_pic_register != INVALID_REGNUM)
15649     {
15650       unsigned long mask;
15651
15652       mask = live_regs_mask;
15653       mask &= THUMB2_WORK_REGS;
15654       if (!IS_NESTED (func_type))
15655         mask |= (1 << IP_REGNUM);
15656       arm_load_pic_register (mask);
15657     }
15658
15659   /* If we are profiling, make sure no instructions are scheduled before
15660      the call to mcount.  Similarly if the user has requested no
15661      scheduling in the prolog.  Similarly if we want non-call exceptions
15662      using the EABI unwinder, to prevent faulting instructions from being
15663      swapped with a stack adjustment.  */
15664   if (crtl->profile || !TARGET_SCHED_PROLOG
15665       || (ARM_EABI_UNWIND_TABLES && cfun->can_throw_non_call_exceptions))
15666     emit_insn (gen_blockage ());
15667
15668   /* If the link register is being kept alive, with the return address in it,
15669      then make sure that it does not get reused by the ce2 pass.  */
15670   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
15671     cfun->machine->lr_save_eliminated = 1;
15672 }
15673 \f
15674 /* Print condition code to STREAM.  Helper function for arm_print_operand.  */
15675 static void
15676 arm_print_condition (FILE *stream)
15677 {
15678   if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
15679     {
15680       /* Branch conversion is not implemented for Thumb-2.  */
15681       if (TARGET_THUMB)
15682         {
15683           output_operand_lossage ("predicated Thumb instruction");
15684           return;
15685         }
15686       if (current_insn_predicate != NULL)
15687         {
15688           output_operand_lossage
15689             ("predicated instruction in conditional sequence");
15690           return;
15691         }
15692
15693       fputs (arm_condition_codes[arm_current_cc], stream);
15694     }
15695   else if (current_insn_predicate)
15696     {
15697       enum arm_cond_code code;
15698
15699       if (TARGET_THUMB1)
15700         {
15701           output_operand_lossage ("predicated Thumb instruction");
15702           return;
15703         }
15704
15705       code = get_arm_condition_code (current_insn_predicate);
15706       fputs (arm_condition_codes[code], stream);
15707     }
15708 }
15709
15710
15711 /* If CODE is 'd', then the X is a condition operand and the instruction
15712    should only be executed if the condition is true.
15713    if CODE is 'D', then the X is a condition operand and the instruction
15714    should only be executed if the condition is false: however, if the mode
15715    of the comparison is CCFPEmode, then always execute the instruction -- we
15716    do this because in these circumstances !GE does not necessarily imply LT;
15717    in these cases the instruction pattern will take care to make sure that
15718    an instruction containing %d will follow, thereby undoing the effects of
15719    doing this instruction unconditionally.
15720    If CODE is 'N' then X is a floating point operand that must be negated
15721    before output.
15722    If CODE is 'B' then output a bitwise inverted value of X (a const int).
15723    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
15724 static void
15725 arm_print_operand (FILE *stream, rtx x, int code)
15726 {
15727   switch (code)
15728     {
15729     case '@':
15730       fputs (ASM_COMMENT_START, stream);
15731       return;
15732
15733     case '_':
15734       fputs (user_label_prefix, stream);
15735       return;
15736
15737     case '|':
15738       fputs (REGISTER_PREFIX, stream);
15739       return;
15740
15741     case '?':
15742       arm_print_condition (stream);
15743       return;
15744
15745     case '(':
15746       /* Nothing in unified syntax, otherwise the current condition code.  */
15747       if (!TARGET_UNIFIED_ASM)
15748         arm_print_condition (stream);
15749       break;
15750
15751     case ')':
15752       /* The current condition code in unified syntax, otherwise nothing.  */
15753       if (TARGET_UNIFIED_ASM)
15754         arm_print_condition (stream);
15755       break;
15756   
15757     case '.':
15758       /* The current condition code for a condition code setting instruction.
15759          Preceded by 's' in unified syntax, otherwise followed by 's'.  */
15760       if (TARGET_UNIFIED_ASM)
15761         {
15762           fputc('s', stream);
15763           arm_print_condition (stream);
15764         }
15765       else
15766         {
15767           arm_print_condition (stream);
15768           fputc('s', stream);
15769         }
15770       return;
15771
15772     case '!':
15773       /* If the instruction is conditionally executed then print
15774          the current condition code, otherwise print 's'.  */
15775       gcc_assert (TARGET_THUMB2 && TARGET_UNIFIED_ASM);
15776       if (current_insn_predicate)
15777         arm_print_condition (stream);
15778       else
15779         fputc('s', stream);
15780       break;
15781
15782     /* %# is a "break" sequence. It doesn't output anything, but is used to
15783        separate e.g. operand numbers from following text, if that text consists
15784        of further digits which we don't want to be part of the operand
15785        number.  */
15786     case '#':
15787       return;
15788
15789     case 'N':
15790       {
15791         REAL_VALUE_TYPE r;
15792         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
15793         r = real_value_negate (&r);
15794         fprintf (stream, "%s", fp_const_from_val (&r));
15795       }
15796       return;
15797
15798     /* An integer or symbol address without a preceding # sign.  */
15799     case 'c':
15800       switch (GET_CODE (x))
15801         {
15802         case CONST_INT:
15803           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
15804           break;
15805
15806         case SYMBOL_REF:
15807           output_addr_const (stream, x);
15808           break;
15809
15810         default:
15811           gcc_unreachable ();
15812         }
15813       return;
15814
15815     case 'B':
15816       if (GET_CODE (x) == CONST_INT)
15817         {
15818           HOST_WIDE_INT val;
15819           val = ARM_SIGN_EXTEND (~INTVAL (x));
15820           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
15821         }
15822       else
15823         {
15824           putc ('~', stream);
15825           output_addr_const (stream, x);
15826         }
15827       return;
15828
15829     case 'L':
15830       /* The low 16 bits of an immediate constant.  */
15831       fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL(x) & 0xffff);
15832       return;
15833
15834     case 'i':
15835       fprintf (stream, "%s", arithmetic_instr (x, 1));
15836       return;
15837
15838     /* Truncate Cirrus shift counts.  */
15839     case 's':
15840       if (GET_CODE (x) == CONST_INT)
15841         {
15842           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
15843           return;
15844         }
15845       arm_print_operand (stream, x, 0);
15846       return;
15847
15848     case 'I':
15849       fprintf (stream, "%s", arithmetic_instr (x, 0));
15850       return;
15851
15852     case 'S':
15853       {
15854         HOST_WIDE_INT val;
15855         const char *shift;
15856
15857         if (!shift_operator (x, SImode))
15858           {
15859             output_operand_lossage ("invalid shift operand");
15860             break;
15861           }
15862
15863         shift = shift_op (x, &val);
15864
15865         if (shift)
15866           {
15867             fprintf (stream, ", %s ", shift);
15868             if (val == -1)
15869               arm_print_operand (stream, XEXP (x, 1), 0);
15870             else
15871               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
15872           }
15873       }
15874       return;
15875
15876       /* An explanation of the 'Q', 'R' and 'H' register operands:
15877
15878          In a pair of registers containing a DI or DF value the 'Q'
15879          operand returns the register number of the register containing
15880          the least significant part of the value.  The 'R' operand returns
15881          the register number of the register containing the most
15882          significant part of the value.
15883
15884          The 'H' operand returns the higher of the two register numbers.
15885          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
15886          same as the 'Q' operand, since the most significant part of the
15887          value is held in the lower number register.  The reverse is true
15888          on systems where WORDS_BIG_ENDIAN is false.
15889
15890          The purpose of these operands is to distinguish between cases
15891          where the endian-ness of the values is important (for example
15892          when they are added together), and cases where the endian-ness
15893          is irrelevant, but the order of register operations is important.
15894          For example when loading a value from memory into a register
15895          pair, the endian-ness does not matter.  Provided that the value
15896          from the lower memory address is put into the lower numbered
15897          register, and the value from the higher address is put into the
15898          higher numbered register, the load will work regardless of whether
15899          the value being loaded is big-wordian or little-wordian.  The
15900          order of the two register loads can matter however, if the address
15901          of the memory location is actually held in one of the registers
15902          being overwritten by the load.
15903
15904          The 'Q' and 'R' constraints are also available for 64-bit
15905          constants.  */
15906     case 'Q':
15907       if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
15908         {
15909           rtx part = gen_lowpart (SImode, x);
15910           fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, INTVAL (part));
15911           return;
15912         }
15913
15914       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
15915         {
15916           output_operand_lossage ("invalid operand for code '%c'", code);
15917           return;
15918         }
15919
15920       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
15921       return;
15922
15923     case 'R':
15924       if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
15925         {
15926           enum machine_mode mode = GET_MODE (x);
15927           rtx part;
15928
15929           if (mode == VOIDmode)
15930             mode = DImode;
15931           part = gen_highpart_mode (SImode, mode, x);
15932           fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, INTVAL (part));
15933           return;
15934         }
15935
15936       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
15937         {
15938           output_operand_lossage ("invalid operand for code '%c'", code);
15939           return;
15940         }
15941
15942       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
15943       return;
15944
15945     case 'H':
15946       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
15947         {
15948           output_operand_lossage ("invalid operand for code '%c'", code);
15949           return;
15950         }
15951
15952       asm_fprintf (stream, "%r", REGNO (x) + 1);
15953       return;
15954
15955     case 'J':
15956       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
15957         {
15958           output_operand_lossage ("invalid operand for code '%c'", code);
15959           return;
15960         }
15961
15962       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 3 : 2));
15963       return;
15964
15965     case 'K':
15966       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
15967         {
15968           output_operand_lossage ("invalid operand for code '%c'", code);
15969           return;
15970         }
15971
15972       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 2 : 3));
15973       return;
15974
15975     case 'm':
15976       asm_fprintf (stream, "%r",
15977                    GET_CODE (XEXP (x, 0)) == REG
15978                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
15979       return;
15980
15981     case 'M':
15982       asm_fprintf (stream, "{%r-%r}",
15983                    REGNO (x),
15984                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
15985       return;
15986
15987     /* Like 'M', but writing doubleword vector registers, for use by Neon
15988        insns.  */
15989     case 'h':
15990       {
15991         int regno = (REGNO (x) - FIRST_VFP_REGNUM) / 2;
15992         int numregs = ARM_NUM_REGS (GET_MODE (x)) / 2;
15993         if (numregs == 1)
15994           asm_fprintf (stream, "{d%d}", regno);
15995         else
15996           asm_fprintf (stream, "{d%d-d%d}", regno, regno + numregs - 1);
15997       }
15998       return;
15999
16000     case 'd':
16001       /* CONST_TRUE_RTX means always -- that's the default.  */
16002       if (x == const_true_rtx)
16003         return;
16004
16005       if (!COMPARISON_P (x))
16006         {
16007           output_operand_lossage ("invalid operand for code '%c'", code);
16008           return;
16009         }
16010
16011       fputs (arm_condition_codes[get_arm_condition_code (x)],
16012              stream);
16013       return;
16014
16015     case 'D':
16016       /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
16017          want to do that.  */
16018       if (x == const_true_rtx)
16019         {
16020           output_operand_lossage ("instruction never executed");
16021           return;
16022         }
16023       if (!COMPARISON_P (x))
16024         {
16025           output_operand_lossage ("invalid operand for code '%c'", code);
16026           return;
16027         }
16028
16029       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
16030                                  (get_arm_condition_code (x))],
16031              stream);
16032       return;
16033
16034     /* Cirrus registers can be accessed in a variety of ways:
16035          single floating point (f)
16036          double floating point (d)
16037          32bit integer         (fx)
16038          64bit integer         (dx).  */
16039     case 'W':                   /* Cirrus register in F mode.  */
16040     case 'X':                   /* Cirrus register in D mode.  */
16041     case 'Y':                   /* Cirrus register in FX mode.  */
16042     case 'Z':                   /* Cirrus register in DX mode.  */
16043       gcc_assert (GET_CODE (x) == REG
16044                   && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
16045
16046       fprintf (stream, "mv%s%s",
16047                code == 'W' ? "f"
16048                : code == 'X' ? "d"
16049                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
16050
16051       return;
16052
16053     /* Print cirrus register in the mode specified by the register's mode.  */
16054     case 'V':
16055       {
16056         int mode = GET_MODE (x);
16057
16058         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
16059           {
16060             output_operand_lossage ("invalid operand for code '%c'", code);
16061             return;
16062           }
16063
16064         fprintf (stream, "mv%s%s",
16065                  mode == DFmode ? "d"
16066                  : mode == SImode ? "fx"
16067                  : mode == DImode ? "dx"
16068                  : "f", reg_names[REGNO (x)] + 2);
16069
16070         return;
16071       }
16072
16073     case 'U':
16074       if (GET_CODE (x) != REG
16075           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
16076           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
16077         /* Bad value for wCG register number.  */
16078         {
16079           output_operand_lossage ("invalid operand for code '%c'", code);
16080           return;
16081         }
16082
16083       else
16084         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
16085       return;
16086
16087       /* Print an iWMMXt control register name.  */
16088     case 'w':
16089       if (GET_CODE (x) != CONST_INT
16090           || INTVAL (x) < 0
16091           || INTVAL (x) >= 16)
16092         /* Bad value for wC register number.  */
16093         {
16094           output_operand_lossage ("invalid operand for code '%c'", code);
16095           return;
16096         }
16097
16098       else
16099         {
16100           static const char * wc_reg_names [16] =
16101             {
16102               "wCID",  "wCon",  "wCSSF", "wCASF",
16103               "wC4",   "wC5",   "wC6",   "wC7",
16104               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
16105               "wC12",  "wC13",  "wC14",  "wC15"
16106             };
16107
16108           fprintf (stream, wc_reg_names [INTVAL (x)]);
16109         }
16110       return;
16111
16112     /* Print the high single-precision register of a VFP double-precision
16113        register.  */
16114     case 'p':
16115       {
16116         int mode = GET_MODE (x);
16117         int regno;
16118
16119         if (GET_MODE_SIZE (mode) != 8 || GET_CODE (x) != REG)
16120           {
16121             output_operand_lossage ("invalid operand for code '%c'", code);
16122             return;
16123           }
16124
16125         regno = REGNO (x);
16126         if (!VFP_REGNO_OK_FOR_DOUBLE (regno))
16127           {
16128             output_operand_lossage ("invalid operand for code '%c'", code);
16129             return;
16130           }
16131
16132         fprintf (stream, "s%d", regno - FIRST_VFP_REGNUM + 1);
16133       }
16134       return;
16135
16136     /* Print a VFP/Neon double precision or quad precision register name.  */
16137     case 'P':
16138     case 'q':
16139       {
16140         int mode = GET_MODE (x);
16141         int is_quad = (code == 'q');
16142         int regno;
16143
16144         if (GET_MODE_SIZE (mode) != (is_quad ? 16 : 8))
16145           {
16146             output_operand_lossage ("invalid operand for code '%c'", code);
16147             return;
16148           }
16149
16150         if (GET_CODE (x) != REG
16151             || !IS_VFP_REGNUM (REGNO (x)))
16152           {
16153             output_operand_lossage ("invalid operand for code '%c'", code);
16154             return;
16155           }
16156
16157         regno = REGNO (x);
16158         if ((is_quad && !NEON_REGNO_OK_FOR_QUAD (regno))
16159             || (!is_quad && !VFP_REGNO_OK_FOR_DOUBLE (regno)))
16160           {
16161             output_operand_lossage ("invalid operand for code '%c'", code);
16162             return;
16163           }
16164
16165         fprintf (stream, "%c%d", is_quad ? 'q' : 'd',
16166           (regno - FIRST_VFP_REGNUM) >> (is_quad ? 2 : 1));
16167       }
16168       return;
16169
16170     /* These two codes print the low/high doubleword register of a Neon quad
16171        register, respectively.  For pair-structure types, can also print
16172        low/high quadword registers.  */
16173     case 'e':
16174     case 'f':
16175       {
16176         int mode = GET_MODE (x);
16177         int regno;
16178
16179         if ((GET_MODE_SIZE (mode) != 16
16180              && GET_MODE_SIZE (mode) != 32) || GET_CODE (x) != REG)
16181           {
16182             output_operand_lossage ("invalid operand for code '%c'", code);
16183             return;
16184           }
16185
16186         regno = REGNO (x);
16187         if (!NEON_REGNO_OK_FOR_QUAD (regno))
16188           {
16189             output_operand_lossage ("invalid operand for code '%c'", code);
16190             return;
16191           }
16192
16193         if (GET_MODE_SIZE (mode) == 16)
16194           fprintf (stream, "d%d", ((regno - FIRST_VFP_REGNUM) >> 1)
16195                                   + (code == 'f' ? 1 : 0));
16196         else
16197           fprintf (stream, "q%d", ((regno - FIRST_VFP_REGNUM) >> 2)
16198                                   + (code == 'f' ? 1 : 0));
16199       }
16200       return;
16201
16202     /* Print a VFPv3 floating-point constant, represented as an integer
16203        index.  */
16204     case 'G':
16205       {
16206         int index = vfp3_const_double_index (x);
16207         gcc_assert (index != -1);
16208         fprintf (stream, "%d", index);
16209       }
16210       return;
16211
16212     /* Print bits representing opcode features for Neon.
16213
16214        Bit 0 is 1 for signed, 0 for unsigned.  Floats count as signed
16215        and polynomials as unsigned.
16216
16217        Bit 1 is 1 for floats and polynomials, 0 for ordinary integers.
16218
16219        Bit 2 is 1 for rounding functions, 0 otherwise.  */
16220
16221     /* Identify the type as 's', 'u', 'p' or 'f'.  */
16222     case 'T':
16223       {
16224         HOST_WIDE_INT bits = INTVAL (x);
16225         fputc ("uspf"[bits & 3], stream);
16226       }
16227       return;
16228
16229     /* Likewise, but signed and unsigned integers are both 'i'.  */
16230     case 'F':
16231       {
16232         HOST_WIDE_INT bits = INTVAL (x);
16233         fputc ("iipf"[bits & 3], stream);
16234       }
16235       return;
16236
16237     /* As for 'T', but emit 'u' instead of 'p'.  */
16238     case 't':
16239       {
16240         HOST_WIDE_INT bits = INTVAL (x);
16241         fputc ("usuf"[bits & 3], stream);
16242       }
16243       return;
16244
16245     /* Bit 2: rounding (vs none).  */
16246     case 'O':
16247       {
16248         HOST_WIDE_INT bits = INTVAL (x);
16249         fputs ((bits & 4) != 0 ? "r" : "", stream);
16250       }
16251       return;
16252
16253     /* Memory operand for vld1/vst1 instruction.  */
16254     case 'A':
16255       {
16256         rtx addr;
16257         bool postinc = FALSE;
16258         gcc_assert (GET_CODE (x) == MEM);
16259         addr = XEXP (x, 0);
16260         if (GET_CODE (addr) == POST_INC)
16261           {
16262             postinc = 1;
16263             addr = XEXP (addr, 0);
16264           }
16265         asm_fprintf (stream, "[%r]", REGNO (addr));
16266         if (postinc)
16267           fputs("!", stream);
16268       }
16269       return;
16270
16271     case 'C':
16272       {
16273         rtx addr;
16274
16275         gcc_assert (GET_CODE (x) == MEM);
16276         addr = XEXP (x, 0);
16277         gcc_assert (GET_CODE (addr) == REG);
16278         asm_fprintf (stream, "[%r]", REGNO (addr));
16279       }
16280       return;
16281
16282     /* Translate an S register number into a D register number and element index.  */
16283     case 'y':
16284       {
16285         int mode = GET_MODE (x);
16286         int regno;
16287
16288         if (GET_MODE_SIZE (mode) != 4 || GET_CODE (x) != REG)
16289           {
16290             output_operand_lossage ("invalid operand for code '%c'", code);
16291             return;
16292           }
16293
16294         regno = REGNO (x);
16295         if (!VFP_REGNO_OK_FOR_SINGLE (regno))
16296           {
16297             output_operand_lossage ("invalid operand for code '%c'", code);
16298             return;
16299           }
16300
16301         regno = regno - FIRST_VFP_REGNUM;
16302         fprintf (stream, "d%d[%d]", regno / 2, regno % 2);
16303       }
16304       return;
16305
16306     /* Register specifier for vld1.16/vst1.16.  Translate the S register
16307        number into a D register number and element index.  */
16308     case 'z':
16309       {
16310         int mode = GET_MODE (x);
16311         int regno;
16312
16313         if (GET_MODE_SIZE (mode) != 2 || GET_CODE (x) != REG)
16314           {
16315             output_operand_lossage ("invalid operand for code '%c'", code);
16316             return;
16317           }
16318
16319         regno = REGNO (x);
16320         if (!VFP_REGNO_OK_FOR_SINGLE (regno))
16321           {
16322             output_operand_lossage ("invalid operand for code '%c'", code);
16323             return;
16324           }
16325
16326         regno = regno - FIRST_VFP_REGNUM;
16327         fprintf (stream, "d%d[%d]", regno/2, ((regno % 2) ? 2 : 0));
16328       }
16329       return;
16330       
16331     default:
16332       if (x == 0)
16333         {
16334           output_operand_lossage ("missing operand");
16335           return;
16336         }
16337
16338       switch (GET_CODE (x))
16339         {
16340         case REG:
16341           asm_fprintf (stream, "%r", REGNO (x));
16342           break;
16343
16344         case MEM:
16345           output_memory_reference_mode = GET_MODE (x);
16346           output_address (XEXP (x, 0));
16347           break;
16348
16349         case CONST_DOUBLE:
16350           if (TARGET_NEON)
16351             {
16352               char fpstr[20];
16353               real_to_decimal (fpstr, CONST_DOUBLE_REAL_VALUE (x),
16354                                sizeof (fpstr), 0, 1);
16355               fprintf (stream, "#%s", fpstr);
16356             }
16357           else
16358             fprintf (stream, "#%s", fp_immediate_constant (x));
16359           break;
16360
16361         default:
16362           gcc_assert (GET_CODE (x) != NEG);
16363           fputc ('#', stream);
16364           if (GET_CODE (x) == HIGH)
16365             {
16366               fputs (":lower16:", stream);
16367               x = XEXP (x, 0);
16368             }
16369             
16370           output_addr_const (stream, x);
16371           break;
16372         }
16373     }
16374 }
16375 \f
16376 /* Target hook for printing a memory address.  */
16377 static void
16378 arm_print_operand_address (FILE *stream, rtx x)
16379 {
16380   if (TARGET_32BIT)
16381     {
16382       int is_minus = GET_CODE (x) == MINUS;
16383
16384       if (GET_CODE (x) == REG)
16385         asm_fprintf (stream, "[%r, #0]", REGNO (x));
16386       else if (GET_CODE (x) == PLUS || is_minus)
16387         {
16388           rtx base = XEXP (x, 0);
16389           rtx index = XEXP (x, 1);
16390           HOST_WIDE_INT offset = 0;
16391           if (GET_CODE (base) != REG
16392               || (GET_CODE (index) == REG && REGNO (index) == SP_REGNUM))
16393             {
16394               /* Ensure that BASE is a register.  */
16395               /* (one of them must be).  */
16396               /* Also ensure the SP is not used as in index register.  */
16397               rtx temp = base;
16398               base = index;
16399               index = temp;
16400             }
16401           switch (GET_CODE (index))
16402             {
16403             case CONST_INT:
16404               offset = INTVAL (index);
16405               if (is_minus)
16406                 offset = -offset;
16407               asm_fprintf (stream, "[%r, #%wd]",
16408                            REGNO (base), offset);
16409               break;
16410
16411             case REG:
16412               asm_fprintf (stream, "[%r, %s%r]",
16413                            REGNO (base), is_minus ? "-" : "",
16414                            REGNO (index));
16415               break;
16416
16417             case MULT:
16418             case ASHIFTRT:
16419             case LSHIFTRT:
16420             case ASHIFT:
16421             case ROTATERT:
16422               {
16423                 asm_fprintf (stream, "[%r, %s%r",
16424                              REGNO (base), is_minus ? "-" : "",
16425                              REGNO (XEXP (index, 0)));
16426                 arm_print_operand (stream, index, 'S');
16427                 fputs ("]", stream);
16428                 break;
16429               }
16430
16431             default:
16432               gcc_unreachable ();
16433             }
16434         }
16435       else if (GET_CODE (x) == PRE_INC || GET_CODE (x) == POST_INC
16436                || GET_CODE (x) == PRE_DEC || GET_CODE (x) == POST_DEC)
16437         {
16438           extern enum machine_mode output_memory_reference_mode;
16439
16440           gcc_assert (GET_CODE (XEXP (x, 0)) == REG);
16441
16442           if (GET_CODE (x) == PRE_DEC || GET_CODE (x) == PRE_INC)
16443             asm_fprintf (stream, "[%r, #%s%d]!",
16444                          REGNO (XEXP (x, 0)),
16445                          GET_CODE (x) == PRE_DEC ? "-" : "",
16446                          GET_MODE_SIZE (output_memory_reference_mode));
16447           else
16448             asm_fprintf (stream, "[%r], #%s%d",
16449                          REGNO (XEXP (x, 0)),
16450                          GET_CODE (x) == POST_DEC ? "-" : "",
16451                          GET_MODE_SIZE (output_memory_reference_mode));
16452         }
16453       else if (GET_CODE (x) == PRE_MODIFY)
16454         {
16455           asm_fprintf (stream, "[%r, ", REGNO (XEXP (x, 0)));
16456           if (GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
16457             asm_fprintf (stream, "#%wd]!",
16458                          INTVAL (XEXP (XEXP (x, 1), 1)));
16459           else
16460             asm_fprintf (stream, "%r]!",
16461                          REGNO (XEXP (XEXP (x, 1), 1)));
16462         }
16463       else if (GET_CODE (x) == POST_MODIFY)
16464         {
16465           asm_fprintf (stream, "[%r], ", REGNO (XEXP (x, 0)));
16466           if (GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
16467             asm_fprintf (stream, "#%wd",
16468                          INTVAL (XEXP (XEXP (x, 1), 1)));
16469           else
16470             asm_fprintf (stream, "%r",
16471                          REGNO (XEXP (XEXP (x, 1), 1)));
16472         }
16473       else output_addr_const (stream, x);
16474     }
16475   else
16476     {
16477       if (GET_CODE (x) == REG)
16478         asm_fprintf (stream, "[%r]", REGNO (x));
16479       else if (GET_CODE (x) == POST_INC)
16480         asm_fprintf (stream, "%r!", REGNO (XEXP (x, 0)));
16481       else if (GET_CODE (x) == PLUS)
16482         {
16483           gcc_assert (GET_CODE (XEXP (x, 0)) == REG);
16484           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
16485             asm_fprintf (stream, "[%r, #%wd]",
16486                          REGNO (XEXP (x, 0)),
16487                          INTVAL (XEXP (x, 1)));
16488           else
16489             asm_fprintf (stream, "[%r, %r]",
16490                          REGNO (XEXP (x, 0)),
16491                          REGNO (XEXP (x, 1)));
16492         }
16493       else
16494         output_addr_const (stream, x);
16495     }
16496 }
16497 \f
16498 /* Target hook for indicating whether a punctuation character for
16499    TARGET_PRINT_OPERAND is valid.  */
16500 static bool
16501 arm_print_operand_punct_valid_p (unsigned char code)
16502 {
16503   return (code == '@' || code == '|' || code == '.'
16504           || code == '(' || code == ')' || code == '#'
16505           || (TARGET_32BIT && (code == '?'))
16506           || (TARGET_THUMB2 && (code == '!'))
16507           || (TARGET_THUMB && (code == '_')));
16508 }
16509 \f
16510 /* Target hook for assembling integer objects.  The ARM version needs to
16511    handle word-sized values specially.  */
16512 static bool
16513 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
16514 {
16515   enum machine_mode mode;
16516
16517   if (size == UNITS_PER_WORD && aligned_p)
16518     {
16519       fputs ("\t.word\t", asm_out_file);
16520       output_addr_const (asm_out_file, x);
16521
16522       /* Mark symbols as position independent.  We only do this in the
16523          .text segment, not in the .data segment.  */
16524       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
16525           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
16526         {
16527           /* See legitimize_pic_address for an explanation of the
16528              TARGET_VXWORKS_RTP check.  */
16529           if (TARGET_VXWORKS_RTP
16530               || (GET_CODE (x) == SYMBOL_REF && !SYMBOL_REF_LOCAL_P (x)))
16531             fputs ("(GOT)", asm_out_file);
16532           else
16533             fputs ("(GOTOFF)", asm_out_file);
16534         }
16535       fputc ('\n', asm_out_file);
16536       return true;
16537     }
16538
16539   mode = GET_MODE (x);
16540
16541   if (arm_vector_mode_supported_p (mode))
16542     {
16543       int i, units;
16544
16545       gcc_assert (GET_CODE (x) == CONST_VECTOR);
16546
16547       units = CONST_VECTOR_NUNITS (x);
16548       size = GET_MODE_SIZE (GET_MODE_INNER (mode));
16549
16550       if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
16551         for (i = 0; i < units; i++)
16552           {
16553             rtx elt = CONST_VECTOR_ELT (x, i);
16554             assemble_integer
16555               (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
16556           }
16557       else
16558         for (i = 0; i < units; i++)
16559           {
16560             rtx elt = CONST_VECTOR_ELT (x, i);
16561             REAL_VALUE_TYPE rval;
16562
16563             REAL_VALUE_FROM_CONST_DOUBLE (rval, elt);
16564
16565             assemble_real
16566               (rval, GET_MODE_INNER (mode),
16567               i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT);
16568           }
16569
16570       return true;
16571     }
16572
16573   return default_assemble_integer (x, size, aligned_p);
16574 }
16575
16576 static void
16577 arm_elf_asm_cdtor (rtx symbol, int priority, bool is_ctor)
16578 {
16579   section *s;
16580
16581   if (!TARGET_AAPCS_BASED)
16582     {
16583       (is_ctor ? 
16584        default_named_section_asm_out_constructor 
16585        : default_named_section_asm_out_destructor) (symbol, priority);
16586       return;
16587     }
16588
16589   /* Put these in the .init_array section, using a special relocation.  */
16590   if (priority != DEFAULT_INIT_PRIORITY)
16591     {
16592       char buf[18];
16593       sprintf (buf, "%s.%.5u", 
16594                is_ctor ? ".init_array" : ".fini_array",
16595                priority);
16596       s = get_section (buf, SECTION_WRITE, NULL_TREE);
16597     }
16598   else if (is_ctor)
16599     s = ctors_section;
16600   else
16601     s = dtors_section;
16602
16603   switch_to_section (s);
16604   assemble_align (POINTER_SIZE);
16605   fputs ("\t.word\t", asm_out_file);
16606   output_addr_const (asm_out_file, symbol);
16607   fputs ("(target1)\n", asm_out_file);
16608 }
16609
16610 /* Add a function to the list of static constructors.  */
16611
16612 static void
16613 arm_elf_asm_constructor (rtx symbol, int priority)
16614 {
16615   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/true);
16616 }
16617
16618 /* Add a function to the list of static destructors.  */
16619
16620 static void
16621 arm_elf_asm_destructor (rtx symbol, int priority)
16622 {
16623   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/false);
16624 }
16625 \f
16626 /* A finite state machine takes care of noticing whether or not instructions
16627    can be conditionally executed, and thus decrease execution time and code
16628    size by deleting branch instructions.  The fsm is controlled by
16629    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
16630
16631 /* The state of the fsm controlling condition codes are:
16632    0: normal, do nothing special
16633    1: make ASM_OUTPUT_OPCODE not output this instruction
16634    2: make ASM_OUTPUT_OPCODE not output this instruction
16635    3: make instructions conditional
16636    4: make instructions conditional
16637
16638    State transitions (state->state by whom under condition):
16639    0 -> 1 final_prescan_insn if the `target' is a label
16640    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
16641    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
16642    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
16643    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
16644           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
16645    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
16646           (the target insn is arm_target_insn).
16647
16648    If the jump clobbers the conditions then we use states 2 and 4.
16649
16650    A similar thing can be done with conditional return insns.
16651
16652    XXX In case the `target' is an unconditional branch, this conditionalising
16653    of the instructions always reduces code size, but not always execution
16654    time.  But then, I want to reduce the code size to somewhere near what
16655    /bin/cc produces.  */
16656
16657 /* In addition to this, state is maintained for Thumb-2 COND_EXEC
16658    instructions.  When a COND_EXEC instruction is seen the subsequent
16659    instructions are scanned so that multiple conditional instructions can be
16660    combined into a single IT block.  arm_condexec_count and arm_condexec_mask
16661    specify the length and true/false mask for the IT block.  These will be
16662    decremented/zeroed by arm_asm_output_opcode as the insns are output.  */
16663
16664 /* Returns the index of the ARM condition code string in
16665    `arm_condition_codes'.  COMPARISON should be an rtx like
16666    `(eq (...) (...))'.  */
16667 static enum arm_cond_code
16668 get_arm_condition_code (rtx comparison)
16669 {
16670   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
16671   enum arm_cond_code code;
16672   enum rtx_code comp_code = GET_CODE (comparison);
16673
16674   if (GET_MODE_CLASS (mode) != MODE_CC)
16675     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
16676                            XEXP (comparison, 1));
16677
16678   switch (mode)
16679     {
16680     case CC_DNEmode: code = ARM_NE; goto dominance;
16681     case CC_DEQmode: code = ARM_EQ; goto dominance;
16682     case CC_DGEmode: code = ARM_GE; goto dominance;
16683     case CC_DGTmode: code = ARM_GT; goto dominance;
16684     case CC_DLEmode: code = ARM_LE; goto dominance;
16685     case CC_DLTmode: code = ARM_LT; goto dominance;
16686     case CC_DGEUmode: code = ARM_CS; goto dominance;
16687     case CC_DGTUmode: code = ARM_HI; goto dominance;
16688     case CC_DLEUmode: code = ARM_LS; goto dominance;
16689     case CC_DLTUmode: code = ARM_CC;
16690
16691     dominance:
16692       gcc_assert (comp_code == EQ || comp_code == NE);
16693
16694       if (comp_code == EQ)
16695         return ARM_INVERSE_CONDITION_CODE (code);
16696       return code;
16697
16698     case CC_NOOVmode:
16699       switch (comp_code)
16700         {
16701         case NE: return ARM_NE;
16702         case EQ: return ARM_EQ;
16703         case GE: return ARM_PL;
16704         case LT: return ARM_MI;
16705         default: gcc_unreachable ();
16706         }
16707
16708     case CC_Zmode:
16709       switch (comp_code)
16710         {
16711         case NE: return ARM_NE;
16712         case EQ: return ARM_EQ;
16713         default: gcc_unreachable ();
16714         }
16715
16716     case CC_Nmode:
16717       switch (comp_code)
16718         {
16719         case NE: return ARM_MI;
16720         case EQ: return ARM_PL;
16721         default: gcc_unreachable ();
16722         }
16723
16724     case CCFPEmode:
16725     case CCFPmode:
16726       /* These encodings assume that AC=1 in the FPA system control
16727          byte.  This allows us to handle all cases except UNEQ and
16728          LTGT.  */
16729       switch (comp_code)
16730         {
16731         case GE: return ARM_GE;
16732         case GT: return ARM_GT;
16733         case LE: return ARM_LS;
16734         case LT: return ARM_MI;
16735         case NE: return ARM_NE;
16736         case EQ: return ARM_EQ;
16737         case ORDERED: return ARM_VC;
16738         case UNORDERED: return ARM_VS;
16739         case UNLT: return ARM_LT;
16740         case UNLE: return ARM_LE;
16741         case UNGT: return ARM_HI;
16742         case UNGE: return ARM_PL;
16743           /* UNEQ and LTGT do not have a representation.  */
16744         case UNEQ: /* Fall through.  */
16745         case LTGT: /* Fall through.  */
16746         default: gcc_unreachable ();
16747         }
16748
16749     case CC_SWPmode:
16750       switch (comp_code)
16751         {
16752         case NE: return ARM_NE;
16753         case EQ: return ARM_EQ;
16754         case GE: return ARM_LE;
16755         case GT: return ARM_LT;
16756         case LE: return ARM_GE;
16757         case LT: return ARM_GT;
16758         case GEU: return ARM_LS;
16759         case GTU: return ARM_CC;
16760         case LEU: return ARM_CS;
16761         case LTU: return ARM_HI;
16762         default: gcc_unreachable ();
16763         }
16764
16765     case CC_Cmode:
16766       switch (comp_code)
16767         {
16768         case LTU: return ARM_CS;
16769         case GEU: return ARM_CC;
16770         default: gcc_unreachable ();
16771         }
16772
16773     case CC_CZmode:
16774       switch (comp_code)
16775         {
16776         case NE: return ARM_NE;
16777         case EQ: return ARM_EQ;
16778         case GEU: return ARM_CS;
16779         case GTU: return ARM_HI;
16780         case LEU: return ARM_LS;
16781         case LTU: return ARM_CC;
16782         default: gcc_unreachable ();
16783         }
16784
16785     case CC_NCVmode:
16786       switch (comp_code)
16787         {
16788         case GE: return ARM_GE;
16789         case LT: return ARM_LT;
16790         case GEU: return ARM_CS;
16791         case LTU: return ARM_CC;
16792         default: gcc_unreachable ();
16793         }
16794
16795     case CCmode:
16796       switch (comp_code)
16797         {
16798         case NE: return ARM_NE;
16799         case EQ: return ARM_EQ;
16800         case GE: return ARM_GE;
16801         case GT: return ARM_GT;
16802         case LE: return ARM_LE;
16803         case LT: return ARM_LT;
16804         case GEU: return ARM_CS;
16805         case GTU: return ARM_HI;
16806         case LEU: return ARM_LS;
16807         case LTU: return ARM_CC;
16808         default: gcc_unreachable ();
16809         }
16810
16811     default: gcc_unreachable ();
16812     }
16813 }
16814
16815 /* Tell arm_asm_output_opcode to output IT blocks for conditionally executed
16816    instructions.  */
16817 void
16818 thumb2_final_prescan_insn (rtx insn)
16819 {
16820   rtx first_insn = insn;
16821   rtx body = PATTERN (insn);
16822   rtx predicate;
16823   enum arm_cond_code code;
16824   int n;
16825   int mask;
16826
16827   /* Remove the previous insn from the count of insns to be output.  */
16828   if (arm_condexec_count)
16829       arm_condexec_count--;
16830
16831   /* Nothing to do if we are already inside a conditional block.  */
16832   if (arm_condexec_count)
16833     return;
16834
16835   if (GET_CODE (body) != COND_EXEC)
16836     return;
16837
16838   /* Conditional jumps are implemented directly.  */
16839   if (GET_CODE (insn) == JUMP_INSN)
16840     return;
16841
16842   predicate = COND_EXEC_TEST (body);
16843   arm_current_cc = get_arm_condition_code (predicate);
16844
16845   n = get_attr_ce_count (insn);
16846   arm_condexec_count = 1;
16847   arm_condexec_mask = (1 << n) - 1;
16848   arm_condexec_masklen = n;
16849   /* See if subsequent instructions can be combined into the same block.  */
16850   for (;;)
16851     {
16852       insn = next_nonnote_insn (insn);
16853
16854       /* Jumping into the middle of an IT block is illegal, so a label or
16855          barrier terminates the block.  */
16856       if (GET_CODE (insn) != INSN && GET_CODE(insn) != JUMP_INSN)
16857         break;
16858
16859       body = PATTERN (insn);
16860       /* USE and CLOBBER aren't really insns, so just skip them.  */
16861       if (GET_CODE (body) == USE
16862           || GET_CODE (body) == CLOBBER)
16863         continue;
16864
16865       /* ??? Recognize conditional jumps, and combine them with IT blocks.  */
16866       if (GET_CODE (body) != COND_EXEC)
16867         break;
16868       /* Allow up to 4 conditionally executed instructions in a block.  */
16869       n = get_attr_ce_count (insn);
16870       if (arm_condexec_masklen + n > 4)
16871         break;
16872
16873       predicate = COND_EXEC_TEST (body);
16874       code = get_arm_condition_code (predicate);
16875       mask = (1 << n) - 1;
16876       if (arm_current_cc == code)
16877         arm_condexec_mask |= (mask << arm_condexec_masklen);
16878       else if (arm_current_cc != ARM_INVERSE_CONDITION_CODE(code))
16879         break;
16880
16881       arm_condexec_count++;
16882       arm_condexec_masklen += n;
16883
16884       /* A jump must be the last instruction in a conditional block.  */
16885       if (GET_CODE(insn) == JUMP_INSN)
16886         break;
16887     }
16888   /* Restore recog_data (getting the attributes of other insns can
16889      destroy this array, but final.c assumes that it remains intact
16890      across this call).  */
16891   extract_constrain_insn_cached (first_insn);
16892 }
16893
16894 void
16895 arm_final_prescan_insn (rtx insn)
16896 {
16897   /* BODY will hold the body of INSN.  */
16898   rtx body = PATTERN (insn);
16899
16900   /* This will be 1 if trying to repeat the trick, and things need to be
16901      reversed if it appears to fail.  */
16902   int reverse = 0;
16903
16904   /* If we start with a return insn, we only succeed if we find another one.  */
16905   int seeking_return = 0;
16906
16907   /* START_INSN will hold the insn from where we start looking.  This is the
16908      first insn after the following code_label if REVERSE is true.  */
16909   rtx start_insn = insn;
16910
16911   /* If in state 4, check if the target branch is reached, in order to
16912      change back to state 0.  */
16913   if (arm_ccfsm_state == 4)
16914     {
16915       if (insn == arm_target_insn)
16916         {
16917           arm_target_insn = NULL;
16918           arm_ccfsm_state = 0;
16919         }
16920       return;
16921     }
16922
16923   /* If in state 3, it is possible to repeat the trick, if this insn is an
16924      unconditional branch to a label, and immediately following this branch
16925      is the previous target label which is only used once, and the label this
16926      branch jumps to is not too far off.  */
16927   if (arm_ccfsm_state == 3)
16928     {
16929       if (simplejump_p (insn))
16930         {
16931           start_insn = next_nonnote_insn (start_insn);
16932           if (GET_CODE (start_insn) == BARRIER)
16933             {
16934               /* XXX Isn't this always a barrier?  */
16935               start_insn = next_nonnote_insn (start_insn);
16936             }
16937           if (GET_CODE (start_insn) == CODE_LABEL
16938               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
16939               && LABEL_NUSES (start_insn) == 1)
16940             reverse = TRUE;
16941           else
16942             return;
16943         }
16944       else if (GET_CODE (body) == RETURN)
16945         {
16946           start_insn = next_nonnote_insn (start_insn);
16947           if (GET_CODE (start_insn) == BARRIER)
16948             start_insn = next_nonnote_insn (start_insn);
16949           if (GET_CODE (start_insn) == CODE_LABEL
16950               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
16951               && LABEL_NUSES (start_insn) == 1)
16952             {
16953               reverse = TRUE;
16954               seeking_return = 1;
16955             }
16956           else
16957             return;
16958         }
16959       else
16960         return;
16961     }
16962
16963   gcc_assert (!arm_ccfsm_state || reverse);
16964   if (GET_CODE (insn) != JUMP_INSN)
16965     return;
16966
16967   /* This jump might be paralleled with a clobber of the condition codes
16968      the jump should always come first */
16969   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
16970     body = XVECEXP (body, 0, 0);
16971
16972   if (reverse
16973       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
16974           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
16975     {
16976       int insns_skipped;
16977       int fail = FALSE, succeed = FALSE;
16978       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
16979       int then_not_else = TRUE;
16980       rtx this_insn = start_insn, label = 0;
16981
16982       /* Register the insn jumped to.  */
16983       if (reverse)
16984         {
16985           if (!seeking_return)
16986             label = XEXP (SET_SRC (body), 0);
16987         }
16988       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
16989         label = XEXP (XEXP (SET_SRC (body), 1), 0);
16990       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
16991         {
16992           label = XEXP (XEXP (SET_SRC (body), 2), 0);
16993           then_not_else = FALSE;
16994         }
16995       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
16996         seeking_return = 1;
16997       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
16998         {
16999           seeking_return = 1;
17000           then_not_else = FALSE;
17001         }
17002       else
17003         gcc_unreachable ();
17004
17005       /* See how many insns this branch skips, and what kind of insns.  If all
17006          insns are okay, and the label or unconditional branch to the same
17007          label is not too far away, succeed.  */
17008       for (insns_skipped = 0;
17009            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
17010         {
17011           rtx scanbody;
17012
17013           this_insn = next_nonnote_insn (this_insn);
17014           if (!this_insn)
17015             break;
17016
17017           switch (GET_CODE (this_insn))
17018             {
17019             case CODE_LABEL:
17020               /* Succeed if it is the target label, otherwise fail since
17021                  control falls in from somewhere else.  */
17022               if (this_insn == label)
17023                 {
17024                   arm_ccfsm_state = 1;
17025                   succeed = TRUE;
17026                 }
17027               else
17028                 fail = TRUE;
17029               break;
17030
17031             case BARRIER:
17032               /* Succeed if the following insn is the target label.
17033                  Otherwise fail.
17034                  If return insns are used then the last insn in a function
17035                  will be a barrier.  */
17036               this_insn = next_nonnote_insn (this_insn);
17037               if (this_insn && this_insn == label)
17038                 {
17039                   arm_ccfsm_state = 1;
17040                   succeed = TRUE;
17041                 }
17042               else
17043                 fail = TRUE;
17044               break;
17045
17046             case CALL_INSN:
17047               /* The AAPCS says that conditional calls should not be
17048                  used since they make interworking inefficient (the
17049                  linker can't transform BL<cond> into BLX).  That's
17050                  only a problem if the machine has BLX.  */
17051               if (arm_arch5)
17052                 {
17053                   fail = TRUE;
17054                   break;
17055                 }
17056
17057               /* Succeed if the following insn is the target label, or
17058                  if the following two insns are a barrier and the
17059                  target label.  */
17060               this_insn = next_nonnote_insn (this_insn);
17061               if (this_insn && GET_CODE (this_insn) == BARRIER)
17062                 this_insn = next_nonnote_insn (this_insn);
17063
17064               if (this_insn && this_insn == label
17065                   && insns_skipped < max_insns_skipped)
17066                 {
17067                   arm_ccfsm_state = 1;
17068                   succeed = TRUE;
17069                 }
17070               else
17071                 fail = TRUE;
17072               break;
17073
17074             case JUMP_INSN:
17075               /* If this is an unconditional branch to the same label, succeed.
17076                  If it is to another label, do nothing.  If it is conditional,
17077                  fail.  */
17078               /* XXX Probably, the tests for SET and the PC are
17079                  unnecessary.  */
17080
17081               scanbody = PATTERN (this_insn);
17082               if (GET_CODE (scanbody) == SET
17083                   && GET_CODE (SET_DEST (scanbody)) == PC)
17084                 {
17085                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
17086                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
17087                     {
17088                       arm_ccfsm_state = 2;
17089                       succeed = TRUE;
17090                     }
17091                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
17092                     fail = TRUE;
17093                 }
17094               /* Fail if a conditional return is undesirable (e.g. on a
17095                  StrongARM), but still allow this if optimizing for size.  */
17096               else if (GET_CODE (scanbody) == RETURN
17097                        && !use_return_insn (TRUE, NULL)
17098                        && !optimize_size)
17099                 fail = TRUE;
17100               else if (GET_CODE (scanbody) == RETURN
17101                        && seeking_return)
17102                 {
17103                   arm_ccfsm_state = 2;
17104                   succeed = TRUE;
17105                 }
17106               else if (GET_CODE (scanbody) == PARALLEL)
17107                 {
17108                   switch (get_attr_conds (this_insn))
17109                     {
17110                     case CONDS_NOCOND:
17111                       break;
17112                     default:
17113                       fail = TRUE;
17114                       break;
17115                     }
17116                 }
17117               else
17118                 fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
17119
17120               break;
17121
17122             case INSN:
17123               /* Instructions using or affecting the condition codes make it
17124                  fail.  */
17125               scanbody = PATTERN (this_insn);
17126               if (!(GET_CODE (scanbody) == SET
17127                     || GET_CODE (scanbody) == PARALLEL)
17128                   || get_attr_conds (this_insn) != CONDS_NOCOND)
17129                 fail = TRUE;
17130
17131               /* A conditional cirrus instruction must be followed by
17132                  a non Cirrus instruction.  However, since we
17133                  conditionalize instructions in this function and by
17134                  the time we get here we can't add instructions
17135                  (nops), because shorten_branches() has already been
17136                  called, we will disable conditionalizing Cirrus
17137                  instructions to be safe.  */
17138               if (GET_CODE (scanbody) != USE
17139                   && GET_CODE (scanbody) != CLOBBER
17140                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
17141                 fail = TRUE;
17142               break;
17143
17144             default:
17145               break;
17146             }
17147         }
17148       if (succeed)
17149         {
17150           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
17151             arm_target_label = CODE_LABEL_NUMBER (label);
17152           else
17153             {
17154               gcc_assert (seeking_return || arm_ccfsm_state == 2);
17155
17156               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
17157                 {
17158                   this_insn = next_nonnote_insn (this_insn);
17159                   gcc_assert (!this_insn
17160                               || (GET_CODE (this_insn) != BARRIER
17161                                   && GET_CODE (this_insn) != CODE_LABEL));
17162                 }
17163               if (!this_insn)
17164                 {
17165                   /* Oh, dear! we ran off the end.. give up.  */
17166                   extract_constrain_insn_cached (insn);
17167                   arm_ccfsm_state = 0;
17168                   arm_target_insn = NULL;
17169                   return;
17170                 }
17171               arm_target_insn = this_insn;
17172             }
17173
17174           /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
17175              what it was.  */
17176           if (!reverse)
17177             arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body), 0));
17178
17179           if (reverse || then_not_else)
17180             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
17181         }
17182
17183       /* Restore recog_data (getting the attributes of other insns can
17184          destroy this array, but final.c assumes that it remains intact
17185          across this call.  */
17186       extract_constrain_insn_cached (insn);
17187     }
17188 }
17189
17190 /* Output IT instructions.  */
17191 void
17192 thumb2_asm_output_opcode (FILE * stream)
17193 {
17194   char buff[5];
17195   int n;
17196
17197   if (arm_condexec_mask)
17198     {
17199       for (n = 0; n < arm_condexec_masklen; n++)
17200         buff[n] = (arm_condexec_mask & (1 << n)) ? 't' : 'e';
17201       buff[n] = 0;
17202       asm_fprintf(stream, "i%s\t%s\n\t", buff,
17203                   arm_condition_codes[arm_current_cc]);
17204       arm_condexec_mask = 0;
17205     }
17206 }
17207
17208 /* Returns true if REGNO is a valid register
17209    for holding a quantity of type MODE.  */
17210 int
17211 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
17212 {
17213   if (GET_MODE_CLASS (mode) == MODE_CC)
17214     return (regno == CC_REGNUM
17215             || (TARGET_HARD_FLOAT && TARGET_VFP
17216                 && regno == VFPCC_REGNUM));
17217
17218   if (TARGET_THUMB1)
17219     /* For the Thumb we only allow values bigger than SImode in
17220        registers 0 - 6, so that there is always a second low
17221        register available to hold the upper part of the value.
17222        We probably we ought to ensure that the register is the
17223        start of an even numbered register pair.  */
17224     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
17225
17226   if (TARGET_HARD_FLOAT && TARGET_MAVERICK
17227       && IS_CIRRUS_REGNUM (regno))
17228     /* We have outlawed SI values in Cirrus registers because they
17229        reside in the lower 32 bits, but SF values reside in the
17230        upper 32 bits.  This causes gcc all sorts of grief.  We can't
17231        even split the registers into pairs because Cirrus SI values
17232        get sign extended to 64bits-- aldyh.  */
17233     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
17234
17235   if (TARGET_HARD_FLOAT && TARGET_VFP
17236       && IS_VFP_REGNUM (regno))
17237     {
17238       if (mode == SFmode || mode == SImode)
17239         return VFP_REGNO_OK_FOR_SINGLE (regno);
17240
17241       if (mode == DFmode)
17242         return VFP_REGNO_OK_FOR_DOUBLE (regno);
17243
17244       /* VFP registers can hold HFmode values, but there is no point in
17245          putting them there unless we have hardware conversion insns. */
17246       if (mode == HFmode)
17247         return TARGET_FP16 && VFP_REGNO_OK_FOR_SINGLE (regno);
17248
17249       if (TARGET_NEON)
17250         return (VALID_NEON_DREG_MODE (mode) && VFP_REGNO_OK_FOR_DOUBLE (regno))
17251                || (VALID_NEON_QREG_MODE (mode)
17252                    && NEON_REGNO_OK_FOR_QUAD (regno))
17253                || (mode == TImode && NEON_REGNO_OK_FOR_NREGS (regno, 2))
17254                || (mode == EImode && NEON_REGNO_OK_FOR_NREGS (regno, 3))
17255                || (mode == OImode && NEON_REGNO_OK_FOR_NREGS (regno, 4))
17256                || (mode == CImode && NEON_REGNO_OK_FOR_NREGS (regno, 6))
17257                || (mode == XImode && NEON_REGNO_OK_FOR_NREGS (regno, 8));
17258
17259       return FALSE;
17260     }
17261
17262   if (TARGET_REALLY_IWMMXT)
17263     {
17264       if (IS_IWMMXT_GR_REGNUM (regno))
17265         return mode == SImode;
17266
17267       if (IS_IWMMXT_REGNUM (regno))
17268         return VALID_IWMMXT_REG_MODE (mode);
17269     }
17270   
17271   /* We allow almost any value to be stored in the general registers.
17272      Restrict doubleword quantities to even register pairs so that we can
17273      use ldrd.  Do not allow very large Neon structure opaque modes in
17274      general registers; they would use too many.  */
17275   if (regno <= LAST_ARM_REGNUM)
17276     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0)
17277       && ARM_NUM_REGS (mode) <= 4;
17278
17279   if (regno == FRAME_POINTER_REGNUM
17280       || regno == ARG_POINTER_REGNUM)
17281     /* We only allow integers in the fake hard registers.  */
17282     return GET_MODE_CLASS (mode) == MODE_INT;
17283
17284   /* The only registers left are the FPA registers
17285      which we only allow to hold FP values.  */
17286   return (TARGET_HARD_FLOAT && TARGET_FPA
17287           && GET_MODE_CLASS (mode) == MODE_FLOAT
17288           && regno >= FIRST_FPA_REGNUM
17289           && regno <= LAST_FPA_REGNUM);
17290 }
17291
17292 /* For efficiency and historical reasons LO_REGS, HI_REGS and CC_REGS are
17293    not used in arm mode.  */
17294
17295 enum reg_class
17296 arm_regno_class (int regno)
17297 {
17298   if (TARGET_THUMB1)
17299     {
17300       if (regno == STACK_POINTER_REGNUM)
17301         return STACK_REG;
17302       if (regno == CC_REGNUM)
17303         return CC_REG;
17304       if (regno < 8)
17305         return LO_REGS;
17306       return HI_REGS;
17307     }
17308
17309   if (TARGET_THUMB2 && regno < 8)
17310     return LO_REGS;
17311
17312   if (   regno <= LAST_ARM_REGNUM
17313       || regno == FRAME_POINTER_REGNUM
17314       || regno == ARG_POINTER_REGNUM)
17315     return TARGET_THUMB2 ? HI_REGS : GENERAL_REGS;
17316
17317   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
17318     return TARGET_THUMB2 ? CC_REG : NO_REGS;
17319
17320   if (IS_CIRRUS_REGNUM (regno))
17321     return CIRRUS_REGS;
17322
17323   if (IS_VFP_REGNUM (regno))
17324     {
17325       if (regno <= D7_VFP_REGNUM)
17326         return VFP_D0_D7_REGS;
17327       else if (regno <= LAST_LO_VFP_REGNUM)
17328         return VFP_LO_REGS;
17329       else
17330         return VFP_HI_REGS;
17331     }
17332
17333   if (IS_IWMMXT_REGNUM (regno))
17334     return IWMMXT_REGS;
17335
17336   if (IS_IWMMXT_GR_REGNUM (regno))
17337     return IWMMXT_GR_REGS;
17338
17339   return FPA_REGS;
17340 }
17341
17342 /* Handle a special case when computing the offset
17343    of an argument from the frame pointer.  */
17344 int
17345 arm_debugger_arg_offset (int value, rtx addr)
17346 {
17347   rtx insn;
17348
17349   /* We are only interested if dbxout_parms() failed to compute the offset.  */
17350   if (value != 0)
17351     return 0;
17352
17353   /* We can only cope with the case where the address is held in a register.  */
17354   if (GET_CODE (addr) != REG)
17355     return 0;
17356
17357   /* If we are using the frame pointer to point at the argument, then
17358      an offset of 0 is correct.  */
17359   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
17360     return 0;
17361
17362   /* If we are using the stack pointer to point at the
17363      argument, then an offset of 0 is correct.  */
17364   /* ??? Check this is consistent with thumb2 frame layout.  */
17365   if ((TARGET_THUMB || !frame_pointer_needed)
17366       && REGNO (addr) == SP_REGNUM)
17367     return 0;
17368
17369   /* Oh dear.  The argument is pointed to by a register rather
17370      than being held in a register, or being stored at a known
17371      offset from the frame pointer.  Since GDB only understands
17372      those two kinds of argument we must translate the address
17373      held in the register into an offset from the frame pointer.
17374      We do this by searching through the insns for the function
17375      looking to see where this register gets its value.  If the
17376      register is initialized from the frame pointer plus an offset
17377      then we are in luck and we can continue, otherwise we give up.
17378
17379      This code is exercised by producing debugging information
17380      for a function with arguments like this:
17381
17382            double func (double a, double b, int c, double d) {return d;}
17383
17384      Without this code the stab for parameter 'd' will be set to
17385      an offset of 0 from the frame pointer, rather than 8.  */
17386
17387   /* The if() statement says:
17388
17389      If the insn is a normal instruction
17390      and if the insn is setting the value in a register
17391      and if the register being set is the register holding the address of the argument
17392      and if the address is computing by an addition
17393      that involves adding to a register
17394      which is the frame pointer
17395      a constant integer
17396
17397      then...  */
17398
17399   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
17400     {
17401       if (   GET_CODE (insn) == INSN
17402           && GET_CODE (PATTERN (insn)) == SET
17403           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
17404           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
17405           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
17406           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
17407           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
17408              )
17409         {
17410           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
17411
17412           break;
17413         }
17414     }
17415
17416   if (value == 0)
17417     {
17418       debug_rtx (addr);
17419       warning (0, "unable to compute real location of stacked parameter");
17420       value = 8; /* XXX magic hack */
17421     }
17422
17423   return value;
17424 }
17425 \f
17426 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
17427   do                                                                    \
17428     {                                                                   \
17429       if ((MASK) & insn_flags)                                          \
17430         add_builtin_function ((NAME), (TYPE), (CODE),                   \
17431                              BUILT_IN_MD, NULL, NULL_TREE);             \
17432     }                                                                   \
17433   while (0)
17434
17435 struct builtin_description
17436 {
17437   const unsigned int       mask;
17438   const enum insn_code     icode;
17439   const char * const       name;
17440   const enum arm_builtins  code;
17441   const enum rtx_code      comparison;
17442   const unsigned int       flag;
17443 };
17444
17445 static const struct builtin_description bdesc_2arg[] =
17446 {
17447 #define IWMMXT_BUILTIN(code, string, builtin) \
17448   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
17449     ARM_BUILTIN_##builtin, UNKNOWN, 0 },
17450
17451   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
17452   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
17453   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
17454   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
17455   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
17456   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
17457   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
17458   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
17459   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
17460   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
17461   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
17462   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
17463   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
17464   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
17465   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
17466   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
17467   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
17468   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
17469   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
17470   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
17471   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
17472   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
17473   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
17474   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
17475   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
17476   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
17477   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
17478   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
17479   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
17480   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
17481   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
17482   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
17483   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
17484   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
17485   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
17486   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
17487   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
17488   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
17489   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
17490   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
17491   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
17492   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
17493   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
17494   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
17495   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
17496   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
17497   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
17498   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
17499   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
17500   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
17501   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
17502   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
17503   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
17504   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
17505   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
17506   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
17507   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
17508   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
17509
17510 #define IWMMXT_BUILTIN2(code, builtin) \
17511   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, UNKNOWN, 0 },
17512
17513   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
17514   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
17515   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
17516   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
17517   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
17518   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
17519   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
17520   IWMMXT_BUILTIN2 (ashlv4hi3_iwmmxt, WSLLHI)
17521   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
17522   IWMMXT_BUILTIN2 (ashlv2si3_iwmmxt, WSLLWI)
17523   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
17524   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
17525   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
17526   IWMMXT_BUILTIN2 (lshrv4hi3_iwmmxt, WSRLHI)
17527   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
17528   IWMMXT_BUILTIN2 (lshrv2si3_iwmmxt, WSRLWI)
17529   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
17530   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
17531   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
17532   IWMMXT_BUILTIN2 (ashrv4hi3_iwmmxt, WSRAHI)
17533   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
17534   IWMMXT_BUILTIN2 (ashrv2si3_iwmmxt, WSRAWI)
17535   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
17536   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
17537   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
17538   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
17539   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
17540   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
17541   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
17542   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
17543   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
17544   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
17545 };
17546
17547 static const struct builtin_description bdesc_1arg[] =
17548 {
17549   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
17550   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
17551   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
17552   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
17553   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
17554   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
17555   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
17556   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
17557   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
17558   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
17559   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
17560   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
17561   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
17562   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
17563   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
17564   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
17565   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
17566   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
17567 };
17568
17569 /* Set up all the iWMMXt builtins.  This is
17570    not called if TARGET_IWMMXT is zero.  */
17571
17572 static void
17573 arm_init_iwmmxt_builtins (void)
17574 {
17575   const struct builtin_description * d;
17576   size_t i;
17577   tree endlink = void_list_node;
17578
17579   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
17580   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
17581   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
17582
17583   tree int_ftype_int
17584     = build_function_type (integer_type_node,
17585                            tree_cons (NULL_TREE, integer_type_node, endlink));
17586   tree v8qi_ftype_v8qi_v8qi_int
17587     = build_function_type (V8QI_type_node,
17588                            tree_cons (NULL_TREE, V8QI_type_node,
17589                                       tree_cons (NULL_TREE, V8QI_type_node,
17590                                                  tree_cons (NULL_TREE,
17591                                                             integer_type_node,
17592                                                             endlink))));
17593   tree v4hi_ftype_v4hi_int
17594     = build_function_type (V4HI_type_node,
17595                            tree_cons (NULL_TREE, V4HI_type_node,
17596                                       tree_cons (NULL_TREE, integer_type_node,
17597                                                  endlink)));
17598   tree v2si_ftype_v2si_int
17599     = build_function_type (V2SI_type_node,
17600                            tree_cons (NULL_TREE, V2SI_type_node,
17601                                       tree_cons (NULL_TREE, integer_type_node,
17602                                                  endlink)));
17603   tree v2si_ftype_di_di
17604     = build_function_type (V2SI_type_node,
17605                            tree_cons (NULL_TREE, long_long_integer_type_node,
17606                                       tree_cons (NULL_TREE, long_long_integer_type_node,
17607                                                  endlink)));
17608   tree di_ftype_di_int
17609     = build_function_type (long_long_integer_type_node,
17610                            tree_cons (NULL_TREE, long_long_integer_type_node,
17611                                       tree_cons (NULL_TREE, integer_type_node,
17612                                                  endlink)));
17613   tree di_ftype_di_int_int
17614     = build_function_type (long_long_integer_type_node,
17615                            tree_cons (NULL_TREE, long_long_integer_type_node,
17616                                       tree_cons (NULL_TREE, integer_type_node,
17617                                                  tree_cons (NULL_TREE,
17618                                                             integer_type_node,
17619                                                             endlink))));
17620   tree int_ftype_v8qi
17621     = build_function_type (integer_type_node,
17622                            tree_cons (NULL_TREE, V8QI_type_node,
17623                                       endlink));
17624   tree int_ftype_v4hi
17625     = build_function_type (integer_type_node,
17626                            tree_cons (NULL_TREE, V4HI_type_node,
17627                                       endlink));
17628   tree int_ftype_v2si
17629     = build_function_type (integer_type_node,
17630                            tree_cons (NULL_TREE, V2SI_type_node,
17631                                       endlink));
17632   tree int_ftype_v8qi_int
17633     = build_function_type (integer_type_node,
17634                            tree_cons (NULL_TREE, V8QI_type_node,
17635                                       tree_cons (NULL_TREE, integer_type_node,
17636                                                  endlink)));
17637   tree int_ftype_v4hi_int
17638     = build_function_type (integer_type_node,
17639                            tree_cons (NULL_TREE, V4HI_type_node,
17640                                       tree_cons (NULL_TREE, integer_type_node,
17641                                                  endlink)));
17642   tree int_ftype_v2si_int
17643     = build_function_type (integer_type_node,
17644                            tree_cons (NULL_TREE, V2SI_type_node,
17645                                       tree_cons (NULL_TREE, integer_type_node,
17646                                                  endlink)));
17647   tree v8qi_ftype_v8qi_int_int
17648     = build_function_type (V8QI_type_node,
17649                            tree_cons (NULL_TREE, V8QI_type_node,
17650                                       tree_cons (NULL_TREE, integer_type_node,
17651                                                  tree_cons (NULL_TREE,
17652                                                             integer_type_node,
17653                                                             endlink))));
17654   tree v4hi_ftype_v4hi_int_int
17655     = build_function_type (V4HI_type_node,
17656                            tree_cons (NULL_TREE, V4HI_type_node,
17657                                       tree_cons (NULL_TREE, integer_type_node,
17658                                                  tree_cons (NULL_TREE,
17659                                                             integer_type_node,
17660                                                             endlink))));
17661   tree v2si_ftype_v2si_int_int
17662     = build_function_type (V2SI_type_node,
17663                            tree_cons (NULL_TREE, V2SI_type_node,
17664                                       tree_cons (NULL_TREE, integer_type_node,
17665                                                  tree_cons (NULL_TREE,
17666                                                             integer_type_node,
17667                                                             endlink))));
17668   /* Miscellaneous.  */
17669   tree v8qi_ftype_v4hi_v4hi
17670     = build_function_type (V8QI_type_node,
17671                            tree_cons (NULL_TREE, V4HI_type_node,
17672                                       tree_cons (NULL_TREE, V4HI_type_node,
17673                                                  endlink)));
17674   tree v4hi_ftype_v2si_v2si
17675     = build_function_type (V4HI_type_node,
17676                            tree_cons (NULL_TREE, V2SI_type_node,
17677                                       tree_cons (NULL_TREE, V2SI_type_node,
17678                                                  endlink)));
17679   tree v2si_ftype_v4hi_v4hi
17680     = build_function_type (V2SI_type_node,
17681                            tree_cons (NULL_TREE, V4HI_type_node,
17682                                       tree_cons (NULL_TREE, V4HI_type_node,
17683                                                  endlink)));
17684   tree v2si_ftype_v8qi_v8qi
17685     = build_function_type (V2SI_type_node,
17686                            tree_cons (NULL_TREE, V8QI_type_node,
17687                                       tree_cons (NULL_TREE, V8QI_type_node,
17688                                                  endlink)));
17689   tree v4hi_ftype_v4hi_di
17690     = build_function_type (V4HI_type_node,
17691                            tree_cons (NULL_TREE, V4HI_type_node,
17692                                       tree_cons (NULL_TREE,
17693                                                  long_long_integer_type_node,
17694                                                  endlink)));
17695   tree v2si_ftype_v2si_di
17696     = build_function_type (V2SI_type_node,
17697                            tree_cons (NULL_TREE, V2SI_type_node,
17698                                       tree_cons (NULL_TREE,
17699                                                  long_long_integer_type_node,
17700                                                  endlink)));
17701   tree void_ftype_int_int
17702     = build_function_type (void_type_node,
17703                            tree_cons (NULL_TREE, integer_type_node,
17704                                       tree_cons (NULL_TREE, integer_type_node,
17705                                                  endlink)));
17706   tree di_ftype_void
17707     = build_function_type (long_long_unsigned_type_node, endlink);
17708   tree di_ftype_v8qi
17709     = build_function_type (long_long_integer_type_node,
17710                            tree_cons (NULL_TREE, V8QI_type_node,
17711                                       endlink));
17712   tree di_ftype_v4hi
17713     = build_function_type (long_long_integer_type_node,
17714                            tree_cons (NULL_TREE, V4HI_type_node,
17715                                       endlink));
17716   tree di_ftype_v2si
17717     = build_function_type (long_long_integer_type_node,
17718                            tree_cons (NULL_TREE, V2SI_type_node,
17719                                       endlink));
17720   tree v2si_ftype_v4hi
17721     = build_function_type (V2SI_type_node,
17722                            tree_cons (NULL_TREE, V4HI_type_node,
17723                                       endlink));
17724   tree v4hi_ftype_v8qi
17725     = build_function_type (V4HI_type_node,
17726                            tree_cons (NULL_TREE, V8QI_type_node,
17727                                       endlink));
17728
17729   tree di_ftype_di_v4hi_v4hi
17730     = build_function_type (long_long_unsigned_type_node,
17731                            tree_cons (NULL_TREE,
17732                                       long_long_unsigned_type_node,
17733                                       tree_cons (NULL_TREE, V4HI_type_node,
17734                                                  tree_cons (NULL_TREE,
17735                                                             V4HI_type_node,
17736                                                             endlink))));
17737
17738   tree di_ftype_v4hi_v4hi
17739     = build_function_type (long_long_unsigned_type_node,
17740                            tree_cons (NULL_TREE, V4HI_type_node,
17741                                       tree_cons (NULL_TREE, V4HI_type_node,
17742                                                  endlink)));
17743
17744   /* Normal vector binops.  */
17745   tree v8qi_ftype_v8qi_v8qi
17746     = build_function_type (V8QI_type_node,
17747                            tree_cons (NULL_TREE, V8QI_type_node,
17748                                       tree_cons (NULL_TREE, V8QI_type_node,
17749                                                  endlink)));
17750   tree v4hi_ftype_v4hi_v4hi
17751     = build_function_type (V4HI_type_node,
17752                            tree_cons (NULL_TREE, V4HI_type_node,
17753                                       tree_cons (NULL_TREE, V4HI_type_node,
17754                                                  endlink)));
17755   tree v2si_ftype_v2si_v2si
17756     = build_function_type (V2SI_type_node,
17757                            tree_cons (NULL_TREE, V2SI_type_node,
17758                                       tree_cons (NULL_TREE, V2SI_type_node,
17759                                                  endlink)));
17760   tree di_ftype_di_di
17761     = build_function_type (long_long_unsigned_type_node,
17762                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
17763                                       tree_cons (NULL_TREE,
17764                                                  long_long_unsigned_type_node,
17765                                                  endlink)));
17766
17767   /* Add all builtins that are more or less simple operations on two
17768      operands.  */
17769   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
17770     {
17771       /* Use one of the operands; the target can have a different mode for
17772          mask-generating compares.  */
17773       enum machine_mode mode;
17774       tree type;
17775
17776       if (d->name == 0)
17777         continue;
17778
17779       mode = insn_data[d->icode].operand[1].mode;
17780
17781       switch (mode)
17782         {
17783         case V8QImode:
17784           type = v8qi_ftype_v8qi_v8qi;
17785           break;
17786         case V4HImode:
17787           type = v4hi_ftype_v4hi_v4hi;
17788           break;
17789         case V2SImode:
17790           type = v2si_ftype_v2si_v2si;
17791           break;
17792         case DImode:
17793           type = di_ftype_di_di;
17794           break;
17795
17796         default:
17797           gcc_unreachable ();
17798         }
17799
17800       def_mbuiltin (d->mask, d->name, type, d->code);
17801     }
17802
17803   /* Add the remaining MMX insns with somewhat more complicated types.  */
17804   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
17805   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
17806   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
17807
17808   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
17809   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
17810   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
17811   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
17812   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
17813   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
17814
17815   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
17816   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
17817   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
17818   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
17819   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
17820   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
17821
17822   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
17823   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
17824   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
17825   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
17826   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
17827   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
17828
17829   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
17830   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
17831   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
17832   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
17833   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
17834   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
17835
17836   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
17837
17838   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
17839   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
17840   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
17841   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
17842
17843   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
17844   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
17845   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
17846   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
17847   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
17848   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
17849   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
17850   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
17851   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
17852
17853   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
17854   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
17855   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
17856
17857   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
17858   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
17859   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
17860
17861   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
17862   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
17863   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
17864   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
17865   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
17866   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
17867
17868   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
17869   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
17870   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
17871   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
17872   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
17873   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
17874   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
17875   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
17876   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
17877   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
17878   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
17879   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
17880
17881   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
17882   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
17883   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
17884   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
17885
17886   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
17887   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
17888   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
17889   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
17890   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
17891   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
17892   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
17893 }
17894
17895 static void
17896 arm_init_tls_builtins (void)
17897 {
17898   tree ftype, decl;
17899
17900   ftype = build_function_type (ptr_type_node, void_list_node);
17901   decl = add_builtin_function ("__builtin_thread_pointer", ftype,
17902                                ARM_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
17903                                NULL, NULL_TREE);
17904   TREE_NOTHROW (decl) = 1;
17905   TREE_READONLY (decl) = 1;
17906 }
17907
17908 enum neon_builtin_type_bits {
17909   T_V8QI  = 0x0001,
17910   T_V4HI  = 0x0002,
17911   T_V2SI  = 0x0004,
17912   T_V2SF  = 0x0008,
17913   T_DI    = 0x0010,
17914   T_V16QI = 0x0020,
17915   T_V8HI  = 0x0040,
17916   T_V4SI  = 0x0080,
17917   T_V4SF  = 0x0100,
17918   T_V2DI  = 0x0200,
17919   T_TI    = 0x0400,
17920   T_EI    = 0x0800,
17921   T_OI    = 0x1000
17922 };
17923
17924 #define v8qi_UP  T_V8QI
17925 #define v4hi_UP  T_V4HI
17926 #define v2si_UP  T_V2SI
17927 #define v2sf_UP  T_V2SF
17928 #define di_UP    T_DI
17929 #define v16qi_UP T_V16QI
17930 #define v8hi_UP  T_V8HI
17931 #define v4si_UP  T_V4SI
17932 #define v4sf_UP  T_V4SF
17933 #define v2di_UP  T_V2DI
17934 #define ti_UP    T_TI
17935 #define ei_UP    T_EI
17936 #define oi_UP    T_OI
17937
17938 #define UP(X) X##_UP
17939
17940 #define T_MAX 13
17941
17942 typedef enum {
17943   NEON_BINOP,
17944   NEON_TERNOP,
17945   NEON_UNOP,
17946   NEON_GETLANE,
17947   NEON_SETLANE,
17948   NEON_CREATE,
17949   NEON_DUP,
17950   NEON_DUPLANE,
17951   NEON_COMBINE,
17952   NEON_SPLIT,
17953   NEON_LANEMUL,
17954   NEON_LANEMULL,
17955   NEON_LANEMULH,
17956   NEON_LANEMAC,
17957   NEON_SCALARMUL,
17958   NEON_SCALARMULL,
17959   NEON_SCALARMULH,
17960   NEON_SCALARMAC,
17961   NEON_CONVERT,
17962   NEON_FIXCONV,
17963   NEON_SELECT,
17964   NEON_RESULTPAIR,
17965   NEON_REINTERP,
17966   NEON_VTBL,
17967   NEON_VTBX,
17968   NEON_LOAD1,
17969   NEON_LOAD1LANE,
17970   NEON_STORE1,
17971   NEON_STORE1LANE,
17972   NEON_LOADSTRUCT,
17973   NEON_LOADSTRUCTLANE,
17974   NEON_STORESTRUCT,
17975   NEON_STORESTRUCTLANE,
17976   NEON_LOGICBINOP,
17977   NEON_SHIFTINSERT,
17978   NEON_SHIFTIMM,
17979   NEON_SHIFTACC
17980 } neon_itype;
17981
17982 typedef struct {
17983   const char *name;
17984   const neon_itype itype;
17985   const int bits;
17986   const enum insn_code codes[T_MAX];
17987   const unsigned int num_vars;
17988   unsigned int base_fcode;
17989 } neon_builtin_datum;
17990
17991 #define CF(N,X) CODE_FOR_neon_##N##X
17992
17993 #define VAR1(T, N, A) \
17994   #N, NEON_##T, UP (A), { CF (N, A) }, 1, 0
17995 #define VAR2(T, N, A, B) \
17996   #N, NEON_##T, UP (A) | UP (B), { CF (N, A), CF (N, B) }, 2, 0
17997 #define VAR3(T, N, A, B, C) \
17998   #N, NEON_##T, UP (A) | UP (B) | UP (C), \
17999   { CF (N, A), CF (N, B), CF (N, C) }, 3, 0
18000 #define VAR4(T, N, A, B, C, D) \
18001   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D), \
18002   { CF (N, A), CF (N, B), CF (N, C), CF (N, D) }, 4, 0
18003 #define VAR5(T, N, A, B, C, D, E) \
18004   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E), \
18005   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E) }, 5, 0
18006 #define VAR6(T, N, A, B, C, D, E, F) \
18007   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F), \
18008   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F) }, 6, 0
18009 #define VAR7(T, N, A, B, C, D, E, F, G) \
18010   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G), \
18011   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
18012     CF (N, G) }, 7, 0
18013 #define VAR8(T, N, A, B, C, D, E, F, G, H) \
18014   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
18015                 | UP (H), \
18016   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
18017     CF (N, G), CF (N, H) }, 8, 0
18018 #define VAR9(T, N, A, B, C, D, E, F, G, H, I) \
18019   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
18020                 | UP (H) | UP (I), \
18021   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
18022     CF (N, G), CF (N, H), CF (N, I) }, 9, 0
18023 #define VAR10(T, N, A, B, C, D, E, F, G, H, I, J) \
18024   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
18025                 | UP (H) | UP (I) | UP (J), \
18026   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
18027     CF (N, G), CF (N, H), CF (N, I), CF (N, J) }, 10, 0
18028
18029 /* The mode entries in the following table correspond to the "key" type of the
18030    instruction variant, i.e. equivalent to that which would be specified after
18031    the assembler mnemonic, which usually refers to the last vector operand.
18032    (Signed/unsigned/polynomial types are not differentiated between though, and
18033    are all mapped onto the same mode for a given element size.) The modes
18034    listed per instruction should be the same as those defined for that
18035    instruction's pattern in neon.md.
18036    WARNING: Variants should be listed in the same increasing order as
18037    neon_builtin_type_bits.  */
18038
18039 static neon_builtin_datum neon_builtin_data[] =
18040 {
18041   { VAR10 (BINOP, vadd,
18042            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18043   { VAR3 (BINOP, vaddl, v8qi, v4hi, v2si) },
18044   { VAR3 (BINOP, vaddw, v8qi, v4hi, v2si) },
18045   { VAR6 (BINOP, vhadd, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18046   { VAR8 (BINOP, vqadd, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18047   { VAR3 (BINOP, vaddhn, v8hi, v4si, v2di) },
18048   { VAR8 (BINOP, vmul, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18049   { VAR8 (TERNOP, vmla, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18050   { VAR3 (TERNOP, vmlal, v8qi, v4hi, v2si) },
18051   { VAR8 (TERNOP, vmls, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18052   { VAR3 (TERNOP, vmlsl, v8qi, v4hi, v2si) },
18053   { VAR4 (BINOP, vqdmulh, v4hi, v2si, v8hi, v4si) },
18054   { VAR2 (TERNOP, vqdmlal, v4hi, v2si) },
18055   { VAR2 (TERNOP, vqdmlsl, v4hi, v2si) },
18056   { VAR3 (BINOP, vmull, v8qi, v4hi, v2si) },
18057   { VAR2 (SCALARMULL, vmull_n, v4hi, v2si) },
18058   { VAR2 (LANEMULL, vmull_lane, v4hi, v2si) },
18059   { VAR2 (SCALARMULL, vqdmull_n, v4hi, v2si) },
18060   { VAR2 (LANEMULL, vqdmull_lane, v4hi, v2si) },
18061   { VAR4 (SCALARMULH, vqdmulh_n, v4hi, v2si, v8hi, v4si) },
18062   { VAR4 (LANEMULH, vqdmulh_lane, v4hi, v2si, v8hi, v4si) },
18063   { VAR2 (BINOP, vqdmull, v4hi, v2si) },
18064   { VAR8 (BINOP, vshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18065   { VAR8 (BINOP, vqshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18066   { VAR8 (SHIFTIMM, vshr_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18067   { VAR3 (SHIFTIMM, vshrn_n, v8hi, v4si, v2di) },
18068   { VAR3 (SHIFTIMM, vqshrn_n, v8hi, v4si, v2di) },
18069   { VAR3 (SHIFTIMM, vqshrun_n, v8hi, v4si, v2di) },
18070   { VAR8 (SHIFTIMM, vshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18071   { VAR8 (SHIFTIMM, vqshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18072   { VAR8 (SHIFTIMM, vqshlu_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18073   { VAR3 (SHIFTIMM, vshll_n, v8qi, v4hi, v2si) },
18074   { VAR8 (SHIFTACC, vsra_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18075   { VAR10 (BINOP, vsub,
18076            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18077   { VAR3 (BINOP, vsubl, v8qi, v4hi, v2si) },
18078   { VAR3 (BINOP, vsubw, v8qi, v4hi, v2si) },
18079   { VAR8 (BINOP, vqsub, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18080   { VAR6 (BINOP, vhsub, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18081   { VAR3 (BINOP, vsubhn, v8hi, v4si, v2di) },
18082   { VAR8 (BINOP, vceq, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18083   { VAR8 (BINOP, vcge, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18084   { VAR8 (BINOP, vcgt, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18085   { VAR2 (BINOP, vcage, v2sf, v4sf) },
18086   { VAR2 (BINOP, vcagt, v2sf, v4sf) },
18087   { VAR6 (BINOP, vtst, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18088   { VAR8 (BINOP, vabd, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18089   { VAR3 (BINOP, vabdl, v8qi, v4hi, v2si) },
18090   { VAR6 (TERNOP, vaba, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18091   { VAR3 (TERNOP, vabal, v8qi, v4hi, v2si) },
18092   { VAR8 (BINOP, vmax, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18093   { VAR8 (BINOP, vmin, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18094   { VAR4 (BINOP, vpadd, v8qi, v4hi, v2si, v2sf) },
18095   { VAR6 (UNOP, vpaddl, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18096   { VAR6 (BINOP, vpadal, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18097   { VAR4 (BINOP, vpmax, v8qi, v4hi, v2si, v2sf) },
18098   { VAR4 (BINOP, vpmin, v8qi, v4hi, v2si, v2sf) },
18099   { VAR2 (BINOP, vrecps, v2sf, v4sf) },
18100   { VAR2 (BINOP, vrsqrts, v2sf, v4sf) },
18101   { VAR8 (SHIFTINSERT, vsri_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18102   { VAR8 (SHIFTINSERT, vsli_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18103   { VAR8 (UNOP, vabs, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18104   { VAR6 (UNOP, vqabs, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18105   { VAR8 (UNOP, vneg, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18106   { VAR6 (UNOP, vqneg, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18107   { VAR6 (UNOP, vcls, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18108   { VAR6 (UNOP, vclz, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18109   { VAR2 (UNOP, vcnt, v8qi, v16qi) },
18110   { VAR4 (UNOP, vrecpe, v2si, v2sf, v4si, v4sf) },
18111   { VAR4 (UNOP, vrsqrte, v2si, v2sf, v4si, v4sf) },
18112   { VAR6 (UNOP, vmvn, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18113   /* FIXME: vget_lane supports more variants than this!  */
18114   { VAR10 (GETLANE, vget_lane,
18115            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18116   { VAR10 (SETLANE, vset_lane,
18117            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18118   { VAR5 (CREATE, vcreate, v8qi, v4hi, v2si, v2sf, di) },
18119   { VAR10 (DUP, vdup_n,
18120            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18121   { VAR10 (DUPLANE, vdup_lane,
18122            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18123   { VAR5 (COMBINE, vcombine, v8qi, v4hi, v2si, v2sf, di) },
18124   { VAR5 (SPLIT, vget_high, v16qi, v8hi, v4si, v4sf, v2di) },
18125   { VAR5 (SPLIT, vget_low, v16qi, v8hi, v4si, v4sf, v2di) },
18126   { VAR3 (UNOP, vmovn, v8hi, v4si, v2di) },
18127   { VAR3 (UNOP, vqmovn, v8hi, v4si, v2di) },
18128   { VAR3 (UNOP, vqmovun, v8hi, v4si, v2di) },
18129   { VAR3 (UNOP, vmovl, v8qi, v4hi, v2si) },
18130   { VAR6 (LANEMUL, vmul_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18131   { VAR6 (LANEMAC, vmla_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18132   { VAR2 (LANEMAC, vmlal_lane, v4hi, v2si) },
18133   { VAR2 (LANEMAC, vqdmlal_lane, v4hi, v2si) },
18134   { VAR6 (LANEMAC, vmls_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18135   { VAR2 (LANEMAC, vmlsl_lane, v4hi, v2si) },
18136   { VAR2 (LANEMAC, vqdmlsl_lane, v4hi, v2si) },
18137   { VAR6 (SCALARMUL, vmul_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18138   { VAR6 (SCALARMAC, vmla_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18139   { VAR2 (SCALARMAC, vmlal_n, v4hi, v2si) },
18140   { VAR2 (SCALARMAC, vqdmlal_n, v4hi, v2si) },
18141   { VAR6 (SCALARMAC, vmls_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18142   { VAR2 (SCALARMAC, vmlsl_n, v4hi, v2si) },
18143   { VAR2 (SCALARMAC, vqdmlsl_n, v4hi, v2si) },
18144   { VAR10 (BINOP, vext,
18145            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18146   { VAR8 (UNOP, vrev64, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18147   { VAR4 (UNOP, vrev32, v8qi, v4hi, v16qi, v8hi) },
18148   { VAR2 (UNOP, vrev16, v8qi, v16qi) },
18149   { VAR4 (CONVERT, vcvt, v2si, v2sf, v4si, v4sf) },
18150   { VAR4 (FIXCONV, vcvt_n, v2si, v2sf, v4si, v4sf) },
18151   { VAR10 (SELECT, vbsl,
18152            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18153   { VAR1 (VTBL, vtbl1, v8qi) },
18154   { VAR1 (VTBL, vtbl2, v8qi) },
18155   { VAR1 (VTBL, vtbl3, v8qi) },
18156   { VAR1 (VTBL, vtbl4, v8qi) },
18157   { VAR1 (VTBX, vtbx1, v8qi) },
18158   { VAR1 (VTBX, vtbx2, v8qi) },
18159   { VAR1 (VTBX, vtbx3, v8qi) },
18160   { VAR1 (VTBX, vtbx4, v8qi) },
18161   { VAR8 (RESULTPAIR, vtrn, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18162   { VAR8 (RESULTPAIR, vzip, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18163   { VAR8 (RESULTPAIR, vuzp, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18164   { VAR5 (REINTERP, vreinterpretv8qi, v8qi, v4hi, v2si, v2sf, di) },
18165   { VAR5 (REINTERP, vreinterpretv4hi, v8qi, v4hi, v2si, v2sf, di) },
18166   { VAR5 (REINTERP, vreinterpretv2si, v8qi, v4hi, v2si, v2sf, di) },
18167   { VAR5 (REINTERP, vreinterpretv2sf, v8qi, v4hi, v2si, v2sf, di) },
18168   { VAR5 (REINTERP, vreinterpretdi, v8qi, v4hi, v2si, v2sf, di) },
18169   { VAR5 (REINTERP, vreinterpretv16qi, v16qi, v8hi, v4si, v4sf, v2di) },
18170   { VAR5 (REINTERP, vreinterpretv8hi, v16qi, v8hi, v4si, v4sf, v2di) },
18171   { VAR5 (REINTERP, vreinterpretv4si, v16qi, v8hi, v4si, v4sf, v2di) },
18172   { VAR5 (REINTERP, vreinterpretv4sf, v16qi, v8hi, v4si, v4sf, v2di) },
18173   { VAR5 (REINTERP, vreinterpretv2di, v16qi, v8hi, v4si, v4sf, v2di) },
18174   { VAR10 (LOAD1, vld1,
18175            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18176   { VAR10 (LOAD1LANE, vld1_lane,
18177            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18178   { VAR10 (LOAD1, vld1_dup,
18179            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18180   { VAR10 (STORE1, vst1,
18181            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18182   { VAR10 (STORE1LANE, vst1_lane,
18183            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18184   { VAR9 (LOADSTRUCT,
18185           vld2, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18186   { VAR7 (LOADSTRUCTLANE, vld2_lane,
18187           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18188   { VAR5 (LOADSTRUCT, vld2_dup, v8qi, v4hi, v2si, v2sf, di) },
18189   { VAR9 (STORESTRUCT, vst2,
18190           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18191   { VAR7 (STORESTRUCTLANE, vst2_lane,
18192           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18193   { VAR9 (LOADSTRUCT,
18194           vld3, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18195   { VAR7 (LOADSTRUCTLANE, vld3_lane,
18196           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18197   { VAR5 (LOADSTRUCT, vld3_dup, v8qi, v4hi, v2si, v2sf, di) },
18198   { VAR9 (STORESTRUCT, vst3,
18199           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18200   { VAR7 (STORESTRUCTLANE, vst3_lane,
18201           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18202   { VAR9 (LOADSTRUCT, vld4,
18203           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18204   { VAR7 (LOADSTRUCTLANE, vld4_lane,
18205           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18206   { VAR5 (LOADSTRUCT, vld4_dup, v8qi, v4hi, v2si, v2sf, di) },
18207   { VAR9 (STORESTRUCT, vst4,
18208           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18209   { VAR7 (STORESTRUCTLANE, vst4_lane,
18210           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18211   { VAR10 (LOGICBINOP, vand,
18212            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18213   { VAR10 (LOGICBINOP, vorr,
18214            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18215   { VAR10 (BINOP, veor,
18216            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18217   { VAR10 (LOGICBINOP, vbic,
18218            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18219   { VAR10 (LOGICBINOP, vorn,
18220            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) }
18221 };
18222
18223 #undef CF
18224 #undef VAR1
18225 #undef VAR2
18226 #undef VAR3
18227 #undef VAR4
18228 #undef VAR5
18229 #undef VAR6
18230 #undef VAR7
18231 #undef VAR8
18232 #undef VAR9
18233 #undef VAR10
18234
18235 static void
18236 arm_init_neon_builtins (void)
18237 {
18238   unsigned int i, fcode = ARM_BUILTIN_NEON_BASE;
18239
18240   tree neon_intQI_type_node;
18241   tree neon_intHI_type_node;
18242   tree neon_polyQI_type_node;
18243   tree neon_polyHI_type_node;
18244   tree neon_intSI_type_node;
18245   tree neon_intDI_type_node;
18246   tree neon_float_type_node;
18247
18248   tree intQI_pointer_node;
18249   tree intHI_pointer_node;
18250   tree intSI_pointer_node;
18251   tree intDI_pointer_node;
18252   tree float_pointer_node;
18253
18254   tree const_intQI_node;
18255   tree const_intHI_node;
18256   tree const_intSI_node;
18257   tree const_intDI_node;
18258   tree const_float_node;
18259
18260   tree const_intQI_pointer_node;
18261   tree const_intHI_pointer_node;
18262   tree const_intSI_pointer_node;
18263   tree const_intDI_pointer_node;
18264   tree const_float_pointer_node;
18265
18266   tree V8QI_type_node;
18267   tree V4HI_type_node;
18268   tree V2SI_type_node;
18269   tree V2SF_type_node;
18270   tree V16QI_type_node;
18271   tree V8HI_type_node;
18272   tree V4SI_type_node;
18273   tree V4SF_type_node;
18274   tree V2DI_type_node;
18275
18276   tree intUQI_type_node;
18277   tree intUHI_type_node;
18278   tree intUSI_type_node;
18279   tree intUDI_type_node;
18280
18281   tree intEI_type_node;
18282   tree intOI_type_node;
18283   tree intCI_type_node;
18284   tree intXI_type_node;
18285
18286   tree V8QI_pointer_node;
18287   tree V4HI_pointer_node;
18288   tree V2SI_pointer_node;
18289   tree V2SF_pointer_node;
18290   tree V16QI_pointer_node;
18291   tree V8HI_pointer_node;
18292   tree V4SI_pointer_node;
18293   tree V4SF_pointer_node;
18294   tree V2DI_pointer_node;
18295
18296   tree void_ftype_pv8qi_v8qi_v8qi;
18297   tree void_ftype_pv4hi_v4hi_v4hi;
18298   tree void_ftype_pv2si_v2si_v2si;
18299   tree void_ftype_pv2sf_v2sf_v2sf;
18300   tree void_ftype_pdi_di_di;
18301   tree void_ftype_pv16qi_v16qi_v16qi;
18302   tree void_ftype_pv8hi_v8hi_v8hi;
18303   tree void_ftype_pv4si_v4si_v4si;
18304   tree void_ftype_pv4sf_v4sf_v4sf;
18305   tree void_ftype_pv2di_v2di_v2di;
18306
18307   tree reinterp_ftype_dreg[5][5];
18308   tree reinterp_ftype_qreg[5][5];
18309   tree dreg_types[5], qreg_types[5];
18310
18311   /* Create distinguished type nodes for NEON vector element types,
18312      and pointers to values of such types, so we can detect them later.  */
18313   neon_intQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
18314   neon_intHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
18315   neon_polyQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
18316   neon_polyHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
18317   neon_intSI_type_node = make_signed_type (GET_MODE_PRECISION (SImode));
18318   neon_intDI_type_node = make_signed_type (GET_MODE_PRECISION (DImode));
18319   neon_float_type_node = make_node (REAL_TYPE);
18320   TYPE_PRECISION (neon_float_type_node) = FLOAT_TYPE_SIZE;
18321   layout_type (neon_float_type_node);
18322
18323   /* Define typedefs which exactly correspond to the modes we are basing vector
18324      types on.  If you change these names you'll need to change
18325      the table used by arm_mangle_type too.  */
18326   (*lang_hooks.types.register_builtin_type) (neon_intQI_type_node,
18327                                              "__builtin_neon_qi");
18328   (*lang_hooks.types.register_builtin_type) (neon_intHI_type_node,
18329                                              "__builtin_neon_hi");
18330   (*lang_hooks.types.register_builtin_type) (neon_intSI_type_node,
18331                                              "__builtin_neon_si");
18332   (*lang_hooks.types.register_builtin_type) (neon_float_type_node,
18333                                              "__builtin_neon_sf");
18334   (*lang_hooks.types.register_builtin_type) (neon_intDI_type_node,
18335                                              "__builtin_neon_di");
18336   (*lang_hooks.types.register_builtin_type) (neon_polyQI_type_node,
18337                                              "__builtin_neon_poly8");
18338   (*lang_hooks.types.register_builtin_type) (neon_polyHI_type_node,
18339                                              "__builtin_neon_poly16");
18340
18341   intQI_pointer_node = build_pointer_type (neon_intQI_type_node);
18342   intHI_pointer_node = build_pointer_type (neon_intHI_type_node);
18343   intSI_pointer_node = build_pointer_type (neon_intSI_type_node);
18344   intDI_pointer_node = build_pointer_type (neon_intDI_type_node);
18345   float_pointer_node = build_pointer_type (neon_float_type_node);
18346
18347   /* Next create constant-qualified versions of the above types.  */
18348   const_intQI_node = build_qualified_type (neon_intQI_type_node,
18349                                            TYPE_QUAL_CONST);
18350   const_intHI_node = build_qualified_type (neon_intHI_type_node,
18351                                            TYPE_QUAL_CONST);
18352   const_intSI_node = build_qualified_type (neon_intSI_type_node,
18353                                            TYPE_QUAL_CONST);
18354   const_intDI_node = build_qualified_type (neon_intDI_type_node,
18355                                            TYPE_QUAL_CONST);
18356   const_float_node = build_qualified_type (neon_float_type_node,
18357                                            TYPE_QUAL_CONST);
18358
18359   const_intQI_pointer_node = build_pointer_type (const_intQI_node);
18360   const_intHI_pointer_node = build_pointer_type (const_intHI_node);
18361   const_intSI_pointer_node = build_pointer_type (const_intSI_node);
18362   const_intDI_pointer_node = build_pointer_type (const_intDI_node);
18363   const_float_pointer_node = build_pointer_type (const_float_node);
18364
18365   /* Now create vector types based on our NEON element types.  */
18366   /* 64-bit vectors.  */
18367   V8QI_type_node =
18368     build_vector_type_for_mode (neon_intQI_type_node, V8QImode);
18369   V4HI_type_node =
18370     build_vector_type_for_mode (neon_intHI_type_node, V4HImode);
18371   V2SI_type_node =
18372     build_vector_type_for_mode (neon_intSI_type_node, V2SImode);
18373   V2SF_type_node =
18374     build_vector_type_for_mode (neon_float_type_node, V2SFmode);
18375   /* 128-bit vectors.  */
18376   V16QI_type_node =
18377     build_vector_type_for_mode (neon_intQI_type_node, V16QImode);
18378   V8HI_type_node =
18379     build_vector_type_for_mode (neon_intHI_type_node, V8HImode);
18380   V4SI_type_node =
18381     build_vector_type_for_mode (neon_intSI_type_node, V4SImode);
18382   V4SF_type_node =
18383     build_vector_type_for_mode (neon_float_type_node, V4SFmode);
18384   V2DI_type_node =
18385     build_vector_type_for_mode (neon_intDI_type_node, V2DImode);
18386
18387   /* Unsigned integer types for various mode sizes.  */
18388   intUQI_type_node = make_unsigned_type (GET_MODE_PRECISION (QImode));
18389   intUHI_type_node = make_unsigned_type (GET_MODE_PRECISION (HImode));
18390   intUSI_type_node = make_unsigned_type (GET_MODE_PRECISION (SImode));
18391   intUDI_type_node = make_unsigned_type (GET_MODE_PRECISION (DImode));
18392
18393   (*lang_hooks.types.register_builtin_type) (intUQI_type_node,
18394                                              "__builtin_neon_uqi");
18395   (*lang_hooks.types.register_builtin_type) (intUHI_type_node,
18396                                              "__builtin_neon_uhi");
18397   (*lang_hooks.types.register_builtin_type) (intUSI_type_node,
18398                                              "__builtin_neon_usi");
18399   (*lang_hooks.types.register_builtin_type) (intUDI_type_node,
18400                                              "__builtin_neon_udi");
18401
18402   /* Opaque integer types for structures of vectors.  */
18403   intEI_type_node = make_signed_type (GET_MODE_PRECISION (EImode));
18404   intOI_type_node = make_signed_type (GET_MODE_PRECISION (OImode));
18405   intCI_type_node = make_signed_type (GET_MODE_PRECISION (CImode));
18406   intXI_type_node = make_signed_type (GET_MODE_PRECISION (XImode));
18407
18408   (*lang_hooks.types.register_builtin_type) (intTI_type_node,
18409                                              "__builtin_neon_ti");
18410   (*lang_hooks.types.register_builtin_type) (intEI_type_node,
18411                                              "__builtin_neon_ei");
18412   (*lang_hooks.types.register_builtin_type) (intOI_type_node,
18413                                              "__builtin_neon_oi");
18414   (*lang_hooks.types.register_builtin_type) (intCI_type_node,
18415                                              "__builtin_neon_ci");
18416   (*lang_hooks.types.register_builtin_type) (intXI_type_node,
18417                                              "__builtin_neon_xi");
18418
18419   /* Pointers to vector types.  */
18420   V8QI_pointer_node = build_pointer_type (V8QI_type_node);
18421   V4HI_pointer_node = build_pointer_type (V4HI_type_node);
18422   V2SI_pointer_node = build_pointer_type (V2SI_type_node);
18423   V2SF_pointer_node = build_pointer_type (V2SF_type_node);
18424   V16QI_pointer_node = build_pointer_type (V16QI_type_node);
18425   V8HI_pointer_node = build_pointer_type (V8HI_type_node);
18426   V4SI_pointer_node = build_pointer_type (V4SI_type_node);
18427   V4SF_pointer_node = build_pointer_type (V4SF_type_node);
18428   V2DI_pointer_node = build_pointer_type (V2DI_type_node);
18429
18430   /* Operations which return results as pairs.  */
18431   void_ftype_pv8qi_v8qi_v8qi =
18432     build_function_type_list (void_type_node, V8QI_pointer_node, V8QI_type_node,
18433                               V8QI_type_node, NULL);
18434   void_ftype_pv4hi_v4hi_v4hi =
18435     build_function_type_list (void_type_node, V4HI_pointer_node, V4HI_type_node,
18436                               V4HI_type_node, NULL);
18437   void_ftype_pv2si_v2si_v2si =
18438     build_function_type_list (void_type_node, V2SI_pointer_node, V2SI_type_node,
18439                               V2SI_type_node, NULL);
18440   void_ftype_pv2sf_v2sf_v2sf =
18441     build_function_type_list (void_type_node, V2SF_pointer_node, V2SF_type_node,
18442                               V2SF_type_node, NULL);
18443   void_ftype_pdi_di_di =
18444     build_function_type_list (void_type_node, intDI_pointer_node,
18445                               neon_intDI_type_node, neon_intDI_type_node, NULL);
18446   void_ftype_pv16qi_v16qi_v16qi =
18447     build_function_type_list (void_type_node, V16QI_pointer_node,
18448                               V16QI_type_node, V16QI_type_node, NULL);
18449   void_ftype_pv8hi_v8hi_v8hi =
18450     build_function_type_list (void_type_node, V8HI_pointer_node, V8HI_type_node,
18451                               V8HI_type_node, NULL);
18452   void_ftype_pv4si_v4si_v4si =
18453     build_function_type_list (void_type_node, V4SI_pointer_node, V4SI_type_node,
18454                               V4SI_type_node, NULL);
18455   void_ftype_pv4sf_v4sf_v4sf =
18456     build_function_type_list (void_type_node, V4SF_pointer_node, V4SF_type_node,
18457                               V4SF_type_node, NULL);
18458   void_ftype_pv2di_v2di_v2di =
18459     build_function_type_list (void_type_node, V2DI_pointer_node, V2DI_type_node,
18460                               V2DI_type_node, NULL);
18461
18462   dreg_types[0] = V8QI_type_node;
18463   dreg_types[1] = V4HI_type_node;
18464   dreg_types[2] = V2SI_type_node;
18465   dreg_types[3] = V2SF_type_node;
18466   dreg_types[4] = neon_intDI_type_node;
18467
18468   qreg_types[0] = V16QI_type_node;
18469   qreg_types[1] = V8HI_type_node;
18470   qreg_types[2] = V4SI_type_node;
18471   qreg_types[3] = V4SF_type_node;
18472   qreg_types[4] = V2DI_type_node;
18473
18474   for (i = 0; i < 5; i++)
18475     {
18476       int j;
18477       for (j = 0; j < 5; j++)
18478         {
18479           reinterp_ftype_dreg[i][j]
18480             = build_function_type_list (dreg_types[i], dreg_types[j], NULL);
18481           reinterp_ftype_qreg[i][j]
18482             = build_function_type_list (qreg_types[i], qreg_types[j], NULL);
18483         }
18484     }
18485
18486   for (i = 0; i < ARRAY_SIZE (neon_builtin_data); i++)
18487     {
18488       neon_builtin_datum *d = &neon_builtin_data[i];
18489       unsigned int j, codeidx = 0;
18490
18491       d->base_fcode = fcode;
18492
18493       for (j = 0; j < T_MAX; j++)
18494         {
18495           const char* const modenames[] = {
18496             "v8qi", "v4hi", "v2si", "v2sf", "di",
18497             "v16qi", "v8hi", "v4si", "v4sf", "v2di"
18498           };
18499           char namebuf[60];
18500           tree ftype = NULL;
18501           enum insn_code icode;
18502           int is_load = 0, is_store = 0;
18503
18504           if ((d->bits & (1 << j)) == 0)
18505             continue;
18506
18507           icode = d->codes[codeidx++];
18508
18509           switch (d->itype)
18510             {
18511             case NEON_LOAD1:
18512             case NEON_LOAD1LANE:
18513             case NEON_LOADSTRUCT:
18514             case NEON_LOADSTRUCTLANE:
18515               is_load = 1;
18516               /* Fall through.  */
18517             case NEON_STORE1:
18518             case NEON_STORE1LANE:
18519             case NEON_STORESTRUCT:
18520             case NEON_STORESTRUCTLANE:
18521               if (!is_load)
18522                 is_store = 1;
18523               /* Fall through.  */
18524             case NEON_UNOP:
18525             case NEON_BINOP:
18526             case NEON_LOGICBINOP:
18527             case NEON_SHIFTINSERT:
18528             case NEON_TERNOP:
18529             case NEON_GETLANE:
18530             case NEON_SETLANE:
18531             case NEON_CREATE:
18532             case NEON_DUP:
18533             case NEON_DUPLANE:
18534             case NEON_SHIFTIMM:
18535             case NEON_SHIFTACC:
18536             case NEON_COMBINE:
18537             case NEON_SPLIT:
18538             case NEON_CONVERT:
18539             case NEON_FIXCONV:
18540             case NEON_LANEMUL:
18541             case NEON_LANEMULL:
18542             case NEON_LANEMULH:
18543             case NEON_LANEMAC:
18544             case NEON_SCALARMUL:
18545             case NEON_SCALARMULL:
18546             case NEON_SCALARMULH:
18547             case NEON_SCALARMAC:
18548             case NEON_SELECT:
18549             case NEON_VTBL:
18550             case NEON_VTBX:
18551               {
18552                 int k;
18553                 tree return_type = void_type_node, args = void_list_node;
18554
18555                 /* Build a function type directly from the insn_data for this
18556                    builtin.  The build_function_type() function takes care of
18557                    removing duplicates for us.  */
18558                 for (k = insn_data[icode].n_operands - 1; k >= 0; k--)
18559                   {
18560                     tree eltype;
18561
18562                     if (is_load && k == 1)
18563                       {
18564                         /* Neon load patterns always have the memory operand
18565                            (a SImode pointer) in the operand 1 position.  We
18566                            want a const pointer to the element type in that
18567                            position.  */
18568                         gcc_assert (insn_data[icode].operand[k].mode == SImode);
18569
18570                         switch (1 << j)
18571                           {
18572                           case T_V8QI:
18573                           case T_V16QI:
18574                             eltype = const_intQI_pointer_node;
18575                             break;
18576
18577                           case T_V4HI:
18578                           case T_V8HI:
18579                             eltype = const_intHI_pointer_node;
18580                             break;
18581
18582                           case T_V2SI:
18583                           case T_V4SI:
18584                             eltype = const_intSI_pointer_node;
18585                             break;
18586
18587                           case T_V2SF:
18588                           case T_V4SF:
18589                             eltype = const_float_pointer_node;
18590                             break;
18591
18592                           case T_DI:
18593                           case T_V2DI:
18594                             eltype = const_intDI_pointer_node;
18595                             break;
18596
18597                           default: gcc_unreachable ();
18598                           }
18599                       }
18600                     else if (is_store && k == 0)
18601                       {
18602                         /* Similarly, Neon store patterns use operand 0 as
18603                            the memory location to store to (a SImode pointer).
18604                            Use a pointer to the element type of the store in
18605                            that position.  */
18606                         gcc_assert (insn_data[icode].operand[k].mode == SImode);
18607
18608                         switch (1 << j)
18609                           {
18610                           case T_V8QI:
18611                           case T_V16QI:
18612                             eltype = intQI_pointer_node;
18613                             break;
18614
18615                           case T_V4HI:
18616                           case T_V8HI:
18617                             eltype = intHI_pointer_node;
18618                             break;
18619
18620                           case T_V2SI:
18621                           case T_V4SI:
18622                             eltype = intSI_pointer_node;
18623                             break;
18624
18625                           case T_V2SF:
18626                           case T_V4SF:
18627                             eltype = float_pointer_node;
18628                             break;
18629
18630                           case T_DI:
18631                           case T_V2DI:
18632                             eltype = intDI_pointer_node;
18633                             break;
18634
18635                           default: gcc_unreachable ();
18636                           }
18637                       }
18638                     else
18639                       {
18640                         switch (insn_data[icode].operand[k].mode)
18641                           {
18642                           case VOIDmode: eltype = void_type_node; break;
18643                           /* Scalars.  */
18644                           case QImode: eltype = neon_intQI_type_node; break;
18645                           case HImode: eltype = neon_intHI_type_node; break;
18646                           case SImode: eltype = neon_intSI_type_node; break;
18647                           case SFmode: eltype = neon_float_type_node; break;
18648                           case DImode: eltype = neon_intDI_type_node; break;
18649                           case TImode: eltype = intTI_type_node; break;
18650                           case EImode: eltype = intEI_type_node; break;
18651                           case OImode: eltype = intOI_type_node; break;
18652                           case CImode: eltype = intCI_type_node; break;
18653                           case XImode: eltype = intXI_type_node; break;
18654                           /* 64-bit vectors.  */
18655                           case V8QImode: eltype = V8QI_type_node; break;
18656                           case V4HImode: eltype = V4HI_type_node; break;
18657                           case V2SImode: eltype = V2SI_type_node; break;
18658                           case V2SFmode: eltype = V2SF_type_node; break;
18659                           /* 128-bit vectors.  */
18660                           case V16QImode: eltype = V16QI_type_node; break;
18661                           case V8HImode: eltype = V8HI_type_node; break;
18662                           case V4SImode: eltype = V4SI_type_node; break;
18663                           case V4SFmode: eltype = V4SF_type_node; break;
18664                           case V2DImode: eltype = V2DI_type_node; break;
18665                           default: gcc_unreachable ();
18666                           }
18667                       }
18668
18669                     if (k == 0 && !is_store)
18670                       return_type = eltype;
18671                     else
18672                       args = tree_cons (NULL_TREE, eltype, args);
18673                   }
18674
18675                 ftype = build_function_type (return_type, args);
18676               }
18677               break;
18678
18679             case NEON_RESULTPAIR:
18680               {
18681                 switch (insn_data[icode].operand[1].mode)
18682                   {
18683                   case V8QImode: ftype = void_ftype_pv8qi_v8qi_v8qi; break;
18684                   case V4HImode: ftype = void_ftype_pv4hi_v4hi_v4hi; break;
18685                   case V2SImode: ftype = void_ftype_pv2si_v2si_v2si; break;
18686                   case V2SFmode: ftype = void_ftype_pv2sf_v2sf_v2sf; break;
18687                   case DImode: ftype = void_ftype_pdi_di_di; break;
18688                   case V16QImode: ftype = void_ftype_pv16qi_v16qi_v16qi; break;
18689                   case V8HImode: ftype = void_ftype_pv8hi_v8hi_v8hi; break;
18690                   case V4SImode: ftype = void_ftype_pv4si_v4si_v4si; break;
18691                   case V4SFmode: ftype = void_ftype_pv4sf_v4sf_v4sf; break;
18692                   case V2DImode: ftype = void_ftype_pv2di_v2di_v2di; break;
18693                   default: gcc_unreachable ();
18694                   }
18695               }
18696               break;
18697
18698             case NEON_REINTERP:
18699               {
18700                 /* We iterate over 5 doubleword types, then 5 quadword
18701                    types.  */
18702                 int rhs = j % 5;
18703                 switch (insn_data[icode].operand[0].mode)
18704                   {
18705                   case V8QImode: ftype = reinterp_ftype_dreg[0][rhs]; break;
18706                   case V4HImode: ftype = reinterp_ftype_dreg[1][rhs]; break;
18707                   case V2SImode: ftype = reinterp_ftype_dreg[2][rhs]; break;
18708                   case V2SFmode: ftype = reinterp_ftype_dreg[3][rhs]; break;
18709                   case DImode: ftype = reinterp_ftype_dreg[4][rhs]; break;
18710                   case V16QImode: ftype = reinterp_ftype_qreg[0][rhs]; break;
18711                   case V8HImode: ftype = reinterp_ftype_qreg[1][rhs]; break;
18712                   case V4SImode: ftype = reinterp_ftype_qreg[2][rhs]; break;
18713                   case V4SFmode: ftype = reinterp_ftype_qreg[3][rhs]; break;
18714                   case V2DImode: ftype = reinterp_ftype_qreg[4][rhs]; break;
18715                   default: gcc_unreachable ();
18716                   }
18717               }
18718               break;
18719
18720             default:
18721               gcc_unreachable ();
18722             }
18723
18724           gcc_assert (ftype != NULL);
18725
18726           sprintf (namebuf, "__builtin_neon_%s%s", d->name, modenames[j]);
18727
18728           add_builtin_function (namebuf, ftype, fcode++, BUILT_IN_MD, NULL,
18729                                 NULL_TREE);
18730         }
18731     }
18732 }
18733
18734 static void
18735 arm_init_fp16_builtins (void)
18736 {
18737   tree fp16_type = make_node (REAL_TYPE);
18738   TYPE_PRECISION (fp16_type) = 16;
18739   layout_type (fp16_type);
18740   (*lang_hooks.types.register_builtin_type) (fp16_type, "__fp16");
18741 }
18742
18743 static void
18744 arm_init_builtins (void)
18745 {
18746   arm_init_tls_builtins ();
18747
18748   if (TARGET_REALLY_IWMMXT)
18749     arm_init_iwmmxt_builtins ();
18750
18751   if (TARGET_NEON)
18752     arm_init_neon_builtins ();
18753
18754   if (arm_fp16_format)
18755     arm_init_fp16_builtins ();
18756 }
18757
18758 /* Implement TARGET_INVALID_PARAMETER_TYPE.  */
18759
18760 static const char *
18761 arm_invalid_parameter_type (const_tree t)
18762 {
18763   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
18764     return N_("function parameters cannot have __fp16 type");
18765   return NULL;
18766 }
18767
18768 /* Implement TARGET_INVALID_PARAMETER_TYPE.  */
18769
18770 static const char *
18771 arm_invalid_return_type (const_tree t)
18772 {
18773   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
18774     return N_("functions cannot return __fp16 type");
18775   return NULL;
18776 }
18777
18778 /* Implement TARGET_PROMOTED_TYPE.  */
18779
18780 static tree
18781 arm_promoted_type (const_tree t)
18782 {
18783   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
18784     return float_type_node;
18785   return NULL_TREE;
18786 }
18787
18788 /* Implement TARGET_CONVERT_TO_TYPE.
18789    Specifically, this hook implements the peculiarity of the ARM
18790    half-precision floating-point C semantics that requires conversions between
18791    __fp16 to or from double to do an intermediate conversion to float.  */
18792
18793 static tree
18794 arm_convert_to_type (tree type, tree expr)
18795 {
18796   tree fromtype = TREE_TYPE (expr);
18797   if (!SCALAR_FLOAT_TYPE_P (fromtype) || !SCALAR_FLOAT_TYPE_P (type))
18798     return NULL_TREE;
18799   if ((TYPE_PRECISION (fromtype) == 16 && TYPE_PRECISION (type) > 32)
18800       || (TYPE_PRECISION (type) == 16 && TYPE_PRECISION (fromtype) > 32))
18801     return convert (type, convert (float_type_node, expr));
18802   return NULL_TREE;
18803 }
18804
18805 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P.
18806    This simply adds HFmode as a supported mode; even though we don't
18807    implement arithmetic on this type directly, it's supported by
18808    optabs conversions, much the way the double-word arithmetic is
18809    special-cased in the default hook.  */
18810
18811 static bool
18812 arm_scalar_mode_supported_p (enum machine_mode mode)
18813 {
18814   if (mode == HFmode)
18815     return (arm_fp16_format != ARM_FP16_FORMAT_NONE);
18816   else
18817     return default_scalar_mode_supported_p (mode);
18818 }
18819
18820 /* Errors in the source file can cause expand_expr to return const0_rtx
18821    where we expect a vector.  To avoid crashing, use one of the vector
18822    clear instructions.  */
18823
18824 static rtx
18825 safe_vector_operand (rtx x, enum machine_mode mode)
18826 {
18827   if (x != const0_rtx)
18828     return x;
18829   x = gen_reg_rtx (mode);
18830
18831   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
18832                                : gen_rtx_SUBREG (DImode, x, 0)));
18833   return x;
18834 }
18835
18836 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
18837
18838 static rtx
18839 arm_expand_binop_builtin (enum insn_code icode,
18840                           tree exp, rtx target)
18841 {
18842   rtx pat;
18843   tree arg0 = CALL_EXPR_ARG (exp, 0);
18844   tree arg1 = CALL_EXPR_ARG (exp, 1);
18845   rtx op0 = expand_normal (arg0);
18846   rtx op1 = expand_normal (arg1);
18847   enum machine_mode tmode = insn_data[icode].operand[0].mode;
18848   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
18849   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
18850
18851   if (VECTOR_MODE_P (mode0))
18852     op0 = safe_vector_operand (op0, mode0);
18853   if (VECTOR_MODE_P (mode1))
18854     op1 = safe_vector_operand (op1, mode1);
18855
18856   if (! target
18857       || GET_MODE (target) != tmode
18858       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
18859     target = gen_reg_rtx (tmode);
18860
18861   gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
18862
18863   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
18864     op0 = copy_to_mode_reg (mode0, op0);
18865   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
18866     op1 = copy_to_mode_reg (mode1, op1);
18867
18868   pat = GEN_FCN (icode) (target, op0, op1);
18869   if (! pat)
18870     return 0;
18871   emit_insn (pat);
18872   return target;
18873 }
18874
18875 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
18876
18877 static rtx
18878 arm_expand_unop_builtin (enum insn_code icode,
18879                          tree exp, rtx target, int do_load)
18880 {
18881   rtx pat;
18882   tree arg0 = CALL_EXPR_ARG (exp, 0);
18883   rtx op0 = expand_normal (arg0);
18884   enum machine_mode tmode = insn_data[icode].operand[0].mode;
18885   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
18886
18887   if (! target
18888       || GET_MODE (target) != tmode
18889       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
18890     target = gen_reg_rtx (tmode);
18891   if (do_load)
18892     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
18893   else
18894     {
18895       if (VECTOR_MODE_P (mode0))
18896         op0 = safe_vector_operand (op0, mode0);
18897
18898       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
18899         op0 = copy_to_mode_reg (mode0, op0);
18900     }
18901
18902   pat = GEN_FCN (icode) (target, op0);
18903   if (! pat)
18904     return 0;
18905   emit_insn (pat);
18906   return target;
18907 }
18908
18909 static int
18910 neon_builtin_compare (const void *a, const void *b)
18911 {
18912   const neon_builtin_datum *const key = (const neon_builtin_datum *) a;
18913   const neon_builtin_datum *const memb = (const neon_builtin_datum *) b;
18914   unsigned int soughtcode = key->base_fcode;
18915
18916   if (soughtcode >= memb->base_fcode
18917       && soughtcode < memb->base_fcode + memb->num_vars)
18918     return 0;
18919   else if (soughtcode < memb->base_fcode)
18920     return -1;
18921   else
18922     return 1;
18923 }
18924
18925 static enum insn_code
18926 locate_neon_builtin_icode (int fcode, neon_itype *itype)
18927 {
18928   neon_builtin_datum key, *found;
18929   int idx;
18930
18931   key.base_fcode = fcode;
18932   found = (neon_builtin_datum *)
18933     bsearch (&key, &neon_builtin_data[0], ARRAY_SIZE (neon_builtin_data),
18934                    sizeof (neon_builtin_data[0]), neon_builtin_compare);
18935   gcc_assert (found);
18936   idx = fcode - (int) found->base_fcode;
18937   gcc_assert (idx >= 0 && idx < T_MAX && idx < (int)found->num_vars);
18938
18939   if (itype)
18940     *itype = found->itype;
18941
18942   return found->codes[idx];
18943 }
18944
18945 typedef enum {
18946   NEON_ARG_COPY_TO_REG,
18947   NEON_ARG_CONSTANT,
18948   NEON_ARG_STOP
18949 } builtin_arg;
18950
18951 #define NEON_MAX_BUILTIN_ARGS 5
18952
18953 /* Expand a Neon builtin.  */
18954 static rtx
18955 arm_expand_neon_args (rtx target, int icode, int have_retval,
18956                       tree exp, ...)
18957 {
18958   va_list ap;
18959   rtx pat;
18960   tree arg[NEON_MAX_BUILTIN_ARGS];
18961   rtx op[NEON_MAX_BUILTIN_ARGS];
18962   enum machine_mode tmode = insn_data[icode].operand[0].mode;
18963   enum machine_mode mode[NEON_MAX_BUILTIN_ARGS];
18964   int argc = 0;
18965
18966   if (have_retval
18967       && (!target
18968           || GET_MODE (target) != tmode
18969           || !(*insn_data[icode].operand[0].predicate) (target, tmode)))
18970     target = gen_reg_rtx (tmode);
18971
18972   va_start (ap, exp);
18973
18974   for (;;)
18975     {
18976       builtin_arg thisarg = (builtin_arg) va_arg (ap, int);
18977
18978       if (thisarg == NEON_ARG_STOP)
18979         break;
18980       else
18981         {
18982           arg[argc] = CALL_EXPR_ARG (exp, argc);
18983           op[argc] = expand_normal (arg[argc]);
18984           mode[argc] = insn_data[icode].operand[argc + have_retval].mode;
18985
18986           switch (thisarg)
18987             {
18988             case NEON_ARG_COPY_TO_REG:
18989               /*gcc_assert (GET_MODE (op[argc]) == mode[argc]);*/
18990               if (!(*insn_data[icode].operand[argc + have_retval].predicate)
18991                      (op[argc], mode[argc]))
18992                 op[argc] = copy_to_mode_reg (mode[argc], op[argc]);
18993               break;
18994
18995             case NEON_ARG_CONSTANT:
18996               /* FIXME: This error message is somewhat unhelpful.  */
18997               if (!(*insn_data[icode].operand[argc + have_retval].predicate)
18998                     (op[argc], mode[argc]))
18999                 error ("argument must be a constant");
19000               break;
19001
19002             case NEON_ARG_STOP:
19003               gcc_unreachable ();
19004             }
19005
19006           argc++;
19007         }
19008     }
19009
19010   va_end (ap);
19011
19012   if (have_retval)
19013     switch (argc)
19014       {
19015       case 1:
19016         pat = GEN_FCN (icode) (target, op[0]);
19017         break;
19018
19019       case 2:
19020         pat = GEN_FCN (icode) (target, op[0], op[1]);
19021         break;
19022
19023       case 3:
19024         pat = GEN_FCN (icode) (target, op[0], op[1], op[2]);
19025         break;
19026
19027       case 4:
19028         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3]);
19029         break;
19030
19031       case 5:
19032         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3], op[4]);
19033         break;
19034
19035       default:
19036         gcc_unreachable ();
19037       }
19038   else
19039     switch (argc)
19040       {
19041       case 1:
19042         pat = GEN_FCN (icode) (op[0]);
19043         break;
19044
19045       case 2:
19046         pat = GEN_FCN (icode) (op[0], op[1]);
19047         break;
19048
19049       case 3:
19050         pat = GEN_FCN (icode) (op[0], op[1], op[2]);
19051         break;
19052
19053       case 4:
19054         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
19055         break;
19056
19057       case 5:
19058         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4]);
19059         break;
19060
19061       default:
19062         gcc_unreachable ();
19063       }
19064
19065   if (!pat)
19066     return 0;
19067
19068   emit_insn (pat);
19069
19070   return target;
19071 }
19072
19073 /* Expand a Neon builtin. These are "special" because they don't have symbolic
19074    constants defined per-instruction or per instruction-variant. Instead, the
19075    required info is looked up in the table neon_builtin_data.  */
19076 static rtx
19077 arm_expand_neon_builtin (int fcode, tree exp, rtx target)
19078 {
19079   neon_itype itype;
19080   enum insn_code icode = locate_neon_builtin_icode (fcode, &itype);
19081
19082   switch (itype)
19083     {
19084     case NEON_UNOP:
19085     case NEON_CONVERT:
19086     case NEON_DUPLANE:
19087       return arm_expand_neon_args (target, icode, 1, exp,
19088         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_STOP);
19089
19090     case NEON_BINOP:
19091     case NEON_SETLANE:
19092     case NEON_SCALARMUL:
19093     case NEON_SCALARMULL:
19094     case NEON_SCALARMULH:
19095     case NEON_SHIFTINSERT:
19096     case NEON_LOGICBINOP:
19097       return arm_expand_neon_args (target, icode, 1, exp,
19098         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19099         NEON_ARG_STOP);
19100
19101     case NEON_TERNOP:
19102       return arm_expand_neon_args (target, icode, 1, exp,
19103         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19104         NEON_ARG_CONSTANT, NEON_ARG_STOP);
19105
19106     case NEON_GETLANE:
19107     case NEON_FIXCONV:
19108     case NEON_SHIFTIMM:
19109       return arm_expand_neon_args (target, icode, 1, exp,
19110         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_CONSTANT,
19111         NEON_ARG_STOP);
19112
19113     case NEON_CREATE:
19114       return arm_expand_neon_args (target, icode, 1, exp,
19115         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19116
19117     case NEON_DUP:
19118     case NEON_SPLIT:
19119     case NEON_REINTERP:
19120       return arm_expand_neon_args (target, icode, 1, exp,
19121         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19122
19123     case NEON_COMBINE:
19124     case NEON_VTBL:
19125       return arm_expand_neon_args (target, icode, 1, exp,
19126         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19127
19128     case NEON_RESULTPAIR:
19129       return arm_expand_neon_args (target, icode, 0, exp,
19130         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19131         NEON_ARG_STOP);
19132
19133     case NEON_LANEMUL:
19134     case NEON_LANEMULL:
19135     case NEON_LANEMULH:
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_CONSTANT, NEON_ARG_STOP);
19139
19140     case NEON_LANEMAC:
19141       return arm_expand_neon_args (target, icode, 1, exp,
19142         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19143         NEON_ARG_CONSTANT, NEON_ARG_CONSTANT, NEON_ARG_STOP);
19144
19145     case NEON_SHIFTACC:
19146       return arm_expand_neon_args (target, icode, 1, exp,
19147         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19148         NEON_ARG_CONSTANT, NEON_ARG_STOP);
19149
19150     case NEON_SCALARMAC:
19151       return arm_expand_neon_args (target, icode, 1, exp,
19152         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19153         NEON_ARG_CONSTANT, NEON_ARG_STOP);
19154
19155     case NEON_SELECT:
19156     case NEON_VTBX:
19157       return arm_expand_neon_args (target, icode, 1, exp,
19158         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19159         NEON_ARG_STOP);
19160
19161     case NEON_LOAD1:
19162     case NEON_LOADSTRUCT:
19163       return arm_expand_neon_args (target, icode, 1, exp,
19164         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19165
19166     case NEON_LOAD1LANE:
19167     case NEON_LOADSTRUCTLANE:
19168       return arm_expand_neon_args (target, icode, 1, exp,
19169         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19170         NEON_ARG_STOP);
19171
19172     case NEON_STORE1:
19173     case NEON_STORESTRUCT:
19174       return arm_expand_neon_args (target, icode, 0, exp,
19175         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19176
19177     case NEON_STORE1LANE:
19178     case NEON_STORESTRUCTLANE:
19179       return arm_expand_neon_args (target, icode, 0, exp,
19180         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19181         NEON_ARG_STOP);
19182     }
19183
19184   gcc_unreachable ();
19185 }
19186
19187 /* Emit code to reinterpret one Neon type as another, without altering bits.  */
19188 void
19189 neon_reinterpret (rtx dest, rtx src)
19190 {
19191   emit_move_insn (dest, gen_lowpart (GET_MODE (dest), src));
19192 }
19193
19194 /* Emit code to place a Neon pair result in memory locations (with equal
19195    registers).  */
19196 void
19197 neon_emit_pair_result_insn (enum machine_mode mode,
19198                             rtx (*intfn) (rtx, rtx, rtx, rtx), rtx destaddr,
19199                             rtx op1, rtx op2)
19200 {
19201   rtx mem = gen_rtx_MEM (mode, destaddr);
19202   rtx tmp1 = gen_reg_rtx (mode);
19203   rtx tmp2 = gen_reg_rtx (mode);
19204
19205   emit_insn (intfn (tmp1, op1, tmp2, op2));
19206
19207   emit_move_insn (mem, tmp1);
19208   mem = adjust_address (mem, mode, GET_MODE_SIZE (mode));
19209   emit_move_insn (mem, tmp2);
19210 }
19211
19212 /* Set up operands for a register copy from src to dest, taking care not to
19213    clobber registers in the process.
19214    FIXME: This has rather high polynomial complexity (O(n^3)?) but shouldn't
19215    be called with a large N, so that should be OK.  */
19216
19217 void
19218 neon_disambiguate_copy (rtx *operands, rtx *dest, rtx *src, unsigned int count)
19219 {
19220   unsigned int copied = 0, opctr = 0;
19221   unsigned int done = (1 << count) - 1;
19222   unsigned int i, j;
19223
19224   while (copied != done)
19225     {
19226       for (i = 0; i < count; i++)
19227         {
19228           int good = 1;
19229
19230           for (j = 0; good && j < count; j++)
19231             if (i != j && (copied & (1 << j)) == 0
19232                 && reg_overlap_mentioned_p (src[j], dest[i]))
19233               good = 0;
19234
19235           if (good)
19236             {
19237               operands[opctr++] = dest[i];
19238               operands[opctr++] = src[i];
19239               copied |= 1 << i;
19240             }
19241         }
19242     }
19243
19244   gcc_assert (opctr == count * 2);
19245 }
19246
19247 /* Expand an expression EXP that calls a built-in function,
19248    with result going to TARGET if that's convenient
19249    (and in mode MODE if that's convenient).
19250    SUBTARGET may be used as the target for computing one of EXP's operands.
19251    IGNORE is nonzero if the value is to be ignored.  */
19252
19253 static rtx
19254 arm_expand_builtin (tree exp,
19255                     rtx target,
19256                     rtx subtarget ATTRIBUTE_UNUSED,
19257                     enum machine_mode mode ATTRIBUTE_UNUSED,
19258                     int ignore ATTRIBUTE_UNUSED)
19259 {
19260   const struct builtin_description * d;
19261   enum insn_code    icode;
19262   tree              fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
19263   tree              arg0;
19264   tree              arg1;
19265   tree              arg2;
19266   rtx               op0;
19267   rtx               op1;
19268   rtx               op2;
19269   rtx               pat;
19270   int               fcode = DECL_FUNCTION_CODE (fndecl);
19271   size_t            i;
19272   enum machine_mode tmode;
19273   enum machine_mode mode0;
19274   enum machine_mode mode1;
19275   enum machine_mode mode2;
19276
19277   if (fcode >= ARM_BUILTIN_NEON_BASE)
19278     return arm_expand_neon_builtin (fcode, exp, target);
19279
19280   switch (fcode)
19281     {
19282     case ARM_BUILTIN_TEXTRMSB:
19283     case ARM_BUILTIN_TEXTRMUB:
19284     case ARM_BUILTIN_TEXTRMSH:
19285     case ARM_BUILTIN_TEXTRMUH:
19286     case ARM_BUILTIN_TEXTRMSW:
19287     case ARM_BUILTIN_TEXTRMUW:
19288       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
19289                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
19290                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
19291                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
19292                : CODE_FOR_iwmmxt_textrmw);
19293
19294       arg0 = CALL_EXPR_ARG (exp, 0);
19295       arg1 = CALL_EXPR_ARG (exp, 1);
19296       op0 = expand_normal (arg0);
19297       op1 = expand_normal (arg1);
19298       tmode = insn_data[icode].operand[0].mode;
19299       mode0 = insn_data[icode].operand[1].mode;
19300       mode1 = insn_data[icode].operand[2].mode;
19301
19302       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19303         op0 = copy_to_mode_reg (mode0, op0);
19304       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
19305         {
19306           /* @@@ better error message */
19307           error ("selector must be an immediate");
19308           return gen_reg_rtx (tmode);
19309         }
19310       if (target == 0
19311           || GET_MODE (target) != tmode
19312           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19313         target = gen_reg_rtx (tmode);
19314       pat = GEN_FCN (icode) (target, op0, op1);
19315       if (! pat)
19316         return 0;
19317       emit_insn (pat);
19318       return target;
19319
19320     case ARM_BUILTIN_TINSRB:
19321     case ARM_BUILTIN_TINSRH:
19322     case ARM_BUILTIN_TINSRW:
19323       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
19324                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
19325                : CODE_FOR_iwmmxt_tinsrw);
19326       arg0 = CALL_EXPR_ARG (exp, 0);
19327       arg1 = CALL_EXPR_ARG (exp, 1);
19328       arg2 = CALL_EXPR_ARG (exp, 2);
19329       op0 = expand_normal (arg0);
19330       op1 = expand_normal (arg1);
19331       op2 = expand_normal (arg2);
19332       tmode = insn_data[icode].operand[0].mode;
19333       mode0 = insn_data[icode].operand[1].mode;
19334       mode1 = insn_data[icode].operand[2].mode;
19335       mode2 = insn_data[icode].operand[3].mode;
19336
19337       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19338         op0 = copy_to_mode_reg (mode0, op0);
19339       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
19340         op1 = copy_to_mode_reg (mode1, op1);
19341       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
19342         {
19343           /* @@@ better error message */
19344           error ("selector must be an immediate");
19345           return const0_rtx;
19346         }
19347       if (target == 0
19348           || GET_MODE (target) != tmode
19349           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19350         target = gen_reg_rtx (tmode);
19351       pat = GEN_FCN (icode) (target, op0, op1, op2);
19352       if (! pat)
19353         return 0;
19354       emit_insn (pat);
19355       return target;
19356
19357     case ARM_BUILTIN_SETWCX:
19358       arg0 = CALL_EXPR_ARG (exp, 0);
19359       arg1 = CALL_EXPR_ARG (exp, 1);
19360       op0 = force_reg (SImode, expand_normal (arg0));
19361       op1 = expand_normal (arg1);
19362       emit_insn (gen_iwmmxt_tmcr (op1, op0));
19363       return 0;
19364
19365     case ARM_BUILTIN_GETWCX:
19366       arg0 = CALL_EXPR_ARG (exp, 0);
19367       op0 = expand_normal (arg0);
19368       target = gen_reg_rtx (SImode);
19369       emit_insn (gen_iwmmxt_tmrc (target, op0));
19370       return target;
19371
19372     case ARM_BUILTIN_WSHUFH:
19373       icode = CODE_FOR_iwmmxt_wshufh;
19374       arg0 = CALL_EXPR_ARG (exp, 0);
19375       arg1 = CALL_EXPR_ARG (exp, 1);
19376       op0 = expand_normal (arg0);
19377       op1 = expand_normal (arg1);
19378       tmode = insn_data[icode].operand[0].mode;
19379       mode1 = insn_data[icode].operand[1].mode;
19380       mode2 = insn_data[icode].operand[2].mode;
19381
19382       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
19383         op0 = copy_to_mode_reg (mode1, op0);
19384       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
19385         {
19386           /* @@@ better error message */
19387           error ("mask must be an immediate");
19388           return const0_rtx;
19389         }
19390       if (target == 0
19391           || GET_MODE (target) != tmode
19392           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19393         target = gen_reg_rtx (tmode);
19394       pat = GEN_FCN (icode) (target, op0, op1);
19395       if (! pat)
19396         return 0;
19397       emit_insn (pat);
19398       return target;
19399
19400     case ARM_BUILTIN_WSADB:
19401       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, exp, target);
19402     case ARM_BUILTIN_WSADH:
19403       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, exp, target);
19404     case ARM_BUILTIN_WSADBZ:
19405       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, exp, target);
19406     case ARM_BUILTIN_WSADHZ:
19407       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, exp, target);
19408
19409       /* Several three-argument builtins.  */
19410     case ARM_BUILTIN_WMACS:
19411     case ARM_BUILTIN_WMACU:
19412     case ARM_BUILTIN_WALIGN:
19413     case ARM_BUILTIN_TMIA:
19414     case ARM_BUILTIN_TMIAPH:
19415     case ARM_BUILTIN_TMIATT:
19416     case ARM_BUILTIN_TMIATB:
19417     case ARM_BUILTIN_TMIABT:
19418     case ARM_BUILTIN_TMIABB:
19419       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
19420                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
19421                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
19422                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
19423                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
19424                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
19425                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
19426                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
19427                : CODE_FOR_iwmmxt_walign);
19428       arg0 = CALL_EXPR_ARG (exp, 0);
19429       arg1 = CALL_EXPR_ARG (exp, 1);
19430       arg2 = CALL_EXPR_ARG (exp, 2);
19431       op0 = expand_normal (arg0);
19432       op1 = expand_normal (arg1);
19433       op2 = expand_normal (arg2);
19434       tmode = insn_data[icode].operand[0].mode;
19435       mode0 = insn_data[icode].operand[1].mode;
19436       mode1 = insn_data[icode].operand[2].mode;
19437       mode2 = insn_data[icode].operand[3].mode;
19438
19439       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19440         op0 = copy_to_mode_reg (mode0, op0);
19441       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
19442         op1 = copy_to_mode_reg (mode1, op1);
19443       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
19444         op2 = copy_to_mode_reg (mode2, op2);
19445       if (target == 0
19446           || GET_MODE (target) != tmode
19447           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19448         target = gen_reg_rtx (tmode);
19449       pat = GEN_FCN (icode) (target, op0, op1, op2);
19450       if (! pat)
19451         return 0;
19452       emit_insn (pat);
19453       return target;
19454
19455     case ARM_BUILTIN_WZERO:
19456       target = gen_reg_rtx (DImode);
19457       emit_insn (gen_iwmmxt_clrdi (target));
19458       return target;
19459
19460     case ARM_BUILTIN_THREAD_POINTER:
19461       return arm_load_tp (target);
19462
19463     default:
19464       break;
19465     }
19466
19467   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
19468     if (d->code == (const enum arm_builtins) fcode)
19469       return arm_expand_binop_builtin (d->icode, exp, target);
19470
19471   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
19472     if (d->code == (const enum arm_builtins) fcode)
19473       return arm_expand_unop_builtin (d->icode, exp, target, 0);
19474
19475   /* @@@ Should really do something sensible here.  */
19476   return NULL_RTX;
19477 }
19478 \f
19479 /* Return the number (counting from 0) of
19480    the least significant set bit in MASK.  */
19481
19482 inline static int
19483 number_of_first_bit_set (unsigned mask)
19484 {
19485   int bit;
19486
19487   for (bit = 0;
19488        (mask & (1 << bit)) == 0;
19489        ++bit)
19490     continue;
19491
19492   return bit;
19493 }
19494
19495 /* Emit code to push or pop registers to or from the stack.  F is the
19496    assembly file.  MASK is the registers to push or pop.  PUSH is
19497    nonzero if we should push, and zero if we should pop.  For debugging
19498    output, if pushing, adjust CFA_OFFSET by the amount of space added
19499    to the stack.  REAL_REGS should have the same number of bits set as
19500    MASK, and will be used instead (in the same order) to describe which
19501    registers were saved - this is used to mark the save slots when we
19502    push high registers after moving them to low registers.  */
19503 static void
19504 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
19505                unsigned long real_regs)
19506 {
19507   int regno;
19508   int lo_mask = mask & 0xFF;
19509   int pushed_words = 0;
19510
19511   gcc_assert (mask);
19512
19513   if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
19514     {
19515       /* Special case.  Do not generate a POP PC statement here, do it in
19516          thumb_exit() */
19517       thumb_exit (f, -1);
19518       return;
19519     }
19520
19521   if (ARM_EABI_UNWIND_TABLES && push)
19522     {
19523       fprintf (f, "\t.save\t{");
19524       for (regno = 0; regno < 15; regno++)
19525         {
19526           if (real_regs & (1 << regno))
19527             {
19528               if (real_regs & ((1 << regno) -1))
19529                 fprintf (f, ", ");
19530               asm_fprintf (f, "%r", regno);
19531             }
19532         }
19533       fprintf (f, "}\n");
19534     }
19535
19536   fprintf (f, "\t%s\t{", push ? "push" : "pop");
19537
19538   /* Look at the low registers first.  */
19539   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
19540     {
19541       if (lo_mask & 1)
19542         {
19543           asm_fprintf (f, "%r", regno);
19544
19545           if ((lo_mask & ~1) != 0)
19546             fprintf (f, ", ");
19547
19548           pushed_words++;
19549         }
19550     }
19551
19552   if (push && (mask & (1 << LR_REGNUM)))
19553     {
19554       /* Catch pushing the LR.  */
19555       if (mask & 0xFF)
19556         fprintf (f, ", ");
19557
19558       asm_fprintf (f, "%r", LR_REGNUM);
19559
19560       pushed_words++;
19561     }
19562   else if (!push && (mask & (1 << PC_REGNUM)))
19563     {
19564       /* Catch popping the PC.  */
19565       if (TARGET_INTERWORK || TARGET_BACKTRACE
19566           || crtl->calls_eh_return)
19567         {
19568           /* The PC is never poped directly, instead
19569              it is popped into r3 and then BX is used.  */
19570           fprintf (f, "}\n");
19571
19572           thumb_exit (f, -1);
19573
19574           return;
19575         }
19576       else
19577         {
19578           if (mask & 0xFF)
19579             fprintf (f, ", ");
19580
19581           asm_fprintf (f, "%r", PC_REGNUM);
19582         }
19583     }
19584
19585   fprintf (f, "}\n");
19586
19587   if (push && pushed_words && dwarf2out_do_frame ())
19588     {
19589       char *l = dwarf2out_cfi_label (false);
19590       int pushed_mask = real_regs;
19591
19592       *cfa_offset += pushed_words * 4;
19593       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
19594
19595       pushed_words = 0;
19596       pushed_mask = real_regs;
19597       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
19598         {
19599           if (pushed_mask & 1)
19600             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
19601         }
19602     }
19603 }
19604
19605 /* Generate code to return from a thumb function.
19606    If 'reg_containing_return_addr' is -1, then the return address is
19607    actually on the stack, at the stack pointer.  */
19608 static void
19609 thumb_exit (FILE *f, int reg_containing_return_addr)
19610 {
19611   unsigned regs_available_for_popping;
19612   unsigned regs_to_pop;
19613   int pops_needed;
19614   unsigned available;
19615   unsigned required;
19616   int mode;
19617   int size;
19618   int restore_a4 = FALSE;
19619
19620   /* Compute the registers we need to pop.  */
19621   regs_to_pop = 0;
19622   pops_needed = 0;
19623
19624   if (reg_containing_return_addr == -1)
19625     {
19626       regs_to_pop |= 1 << LR_REGNUM;
19627       ++pops_needed;
19628     }
19629
19630   if (TARGET_BACKTRACE)
19631     {
19632       /* Restore the (ARM) frame pointer and stack pointer.  */
19633       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
19634       pops_needed += 2;
19635     }
19636
19637   /* If there is nothing to pop then just emit the BX instruction and
19638      return.  */
19639   if (pops_needed == 0)
19640     {
19641       if (crtl->calls_eh_return)
19642         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
19643
19644       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
19645       return;
19646     }
19647   /* Otherwise if we are not supporting interworking and we have not created
19648      a backtrace structure and the function was not entered in ARM mode then
19649      just pop the return address straight into the PC.  */
19650   else if (!TARGET_INTERWORK
19651            && !TARGET_BACKTRACE
19652            && !is_called_in_ARM_mode (current_function_decl)
19653            && !crtl->calls_eh_return)
19654     {
19655       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
19656       return;
19657     }
19658
19659   /* Find out how many of the (return) argument registers we can corrupt.  */
19660   regs_available_for_popping = 0;
19661
19662   /* If returning via __builtin_eh_return, the bottom three registers
19663      all contain information needed for the return.  */
19664   if (crtl->calls_eh_return)
19665     size = 12;
19666   else
19667     {
19668       /* If we can deduce the registers used from the function's
19669          return value.  This is more reliable that examining
19670          df_regs_ever_live_p () because that will be set if the register is
19671          ever used in the function, not just if the register is used
19672          to hold a return value.  */
19673
19674       if (crtl->return_rtx != 0)
19675         mode = GET_MODE (crtl->return_rtx);
19676       else
19677         mode = DECL_MODE (DECL_RESULT (current_function_decl));
19678
19679       size = GET_MODE_SIZE (mode);
19680
19681       if (size == 0)
19682         {
19683           /* In a void function we can use any argument register.
19684              In a function that returns a structure on the stack
19685              we can use the second and third argument registers.  */
19686           if (mode == VOIDmode)
19687             regs_available_for_popping =
19688               (1 << ARG_REGISTER (1))
19689               | (1 << ARG_REGISTER (2))
19690               | (1 << ARG_REGISTER (3));
19691           else
19692             regs_available_for_popping =
19693               (1 << ARG_REGISTER (2))
19694               | (1 << ARG_REGISTER (3));
19695         }
19696       else if (size <= 4)
19697         regs_available_for_popping =
19698           (1 << ARG_REGISTER (2))
19699           | (1 << ARG_REGISTER (3));
19700       else if (size <= 8)
19701         regs_available_for_popping =
19702           (1 << ARG_REGISTER (3));
19703     }
19704
19705   /* Match registers to be popped with registers into which we pop them.  */
19706   for (available = regs_available_for_popping,
19707        required  = regs_to_pop;
19708        required != 0 && available != 0;
19709        available &= ~(available & - available),
19710        required  &= ~(required  & - required))
19711     -- pops_needed;
19712
19713   /* If we have any popping registers left over, remove them.  */
19714   if (available > 0)
19715     regs_available_for_popping &= ~available;
19716
19717   /* Otherwise if we need another popping register we can use
19718      the fourth argument register.  */
19719   else if (pops_needed)
19720     {
19721       /* If we have not found any free argument registers and
19722          reg a4 contains the return address, we must move it.  */
19723       if (regs_available_for_popping == 0
19724           && reg_containing_return_addr == LAST_ARG_REGNUM)
19725         {
19726           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
19727           reg_containing_return_addr = LR_REGNUM;
19728         }
19729       else if (size > 12)
19730         {
19731           /* Register a4 is being used to hold part of the return value,
19732              but we have dire need of a free, low register.  */
19733           restore_a4 = TRUE;
19734
19735           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
19736         }
19737
19738       if (reg_containing_return_addr != LAST_ARG_REGNUM)
19739         {
19740           /* The fourth argument register is available.  */
19741           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
19742
19743           --pops_needed;
19744         }
19745     }
19746
19747   /* Pop as many registers as we can.  */
19748   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
19749                  regs_available_for_popping);
19750
19751   /* Process the registers we popped.  */
19752   if (reg_containing_return_addr == -1)
19753     {
19754       /* The return address was popped into the lowest numbered register.  */
19755       regs_to_pop &= ~(1 << LR_REGNUM);
19756
19757       reg_containing_return_addr =
19758         number_of_first_bit_set (regs_available_for_popping);
19759
19760       /* Remove this register for the mask of available registers, so that
19761          the return address will not be corrupted by further pops.  */
19762       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
19763     }
19764
19765   /* If we popped other registers then handle them here.  */
19766   if (regs_available_for_popping)
19767     {
19768       int frame_pointer;
19769
19770       /* Work out which register currently contains the frame pointer.  */
19771       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
19772
19773       /* Move it into the correct place.  */
19774       asm_fprintf (f, "\tmov\t%r, %r\n",
19775                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
19776
19777       /* (Temporarily) remove it from the mask of popped registers.  */
19778       regs_available_for_popping &= ~(1 << frame_pointer);
19779       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
19780
19781       if (regs_available_for_popping)
19782         {
19783           int stack_pointer;
19784
19785           /* We popped the stack pointer as well,
19786              find the register that contains it.  */
19787           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
19788
19789           /* Move it into the stack register.  */
19790           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
19791
19792           /* At this point we have popped all necessary registers, so
19793              do not worry about restoring regs_available_for_popping
19794              to its correct value:
19795
19796              assert (pops_needed == 0)
19797              assert (regs_available_for_popping == (1 << frame_pointer))
19798              assert (regs_to_pop == (1 << STACK_POINTER))  */
19799         }
19800       else
19801         {
19802           /* Since we have just move the popped value into the frame
19803              pointer, the popping register is available for reuse, and
19804              we know that we still have the stack pointer left to pop.  */
19805           regs_available_for_popping |= (1 << frame_pointer);
19806         }
19807     }
19808
19809   /* If we still have registers left on the stack, but we no longer have
19810      any registers into which we can pop them, then we must move the return
19811      address into the link register and make available the register that
19812      contained it.  */
19813   if (regs_available_for_popping == 0 && pops_needed > 0)
19814     {
19815       regs_available_for_popping |= 1 << reg_containing_return_addr;
19816
19817       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
19818                    reg_containing_return_addr);
19819
19820       reg_containing_return_addr = LR_REGNUM;
19821     }
19822
19823   /* If we have registers left on the stack then pop some more.
19824      We know that at most we will want to pop FP and SP.  */
19825   if (pops_needed > 0)
19826     {
19827       int  popped_into;
19828       int  move_to;
19829
19830       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
19831                      regs_available_for_popping);
19832
19833       /* We have popped either FP or SP.
19834          Move whichever one it is into the correct register.  */
19835       popped_into = number_of_first_bit_set (regs_available_for_popping);
19836       move_to     = number_of_first_bit_set (regs_to_pop);
19837
19838       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
19839
19840       regs_to_pop &= ~(1 << move_to);
19841
19842       --pops_needed;
19843     }
19844
19845   /* If we still have not popped everything then we must have only
19846      had one register available to us and we are now popping the SP.  */
19847   if (pops_needed > 0)
19848     {
19849       int  popped_into;
19850
19851       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
19852                      regs_available_for_popping);
19853
19854       popped_into = number_of_first_bit_set (regs_available_for_popping);
19855
19856       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
19857       /*
19858         assert (regs_to_pop == (1 << STACK_POINTER))
19859         assert (pops_needed == 1)
19860       */
19861     }
19862
19863   /* If necessary restore the a4 register.  */
19864   if (restore_a4)
19865     {
19866       if (reg_containing_return_addr != LR_REGNUM)
19867         {
19868           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
19869           reg_containing_return_addr = LR_REGNUM;
19870         }
19871
19872       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
19873     }
19874
19875   if (crtl->calls_eh_return)
19876     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
19877
19878   /* Return to caller.  */
19879   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
19880 }
19881 \f
19882 /* Scan INSN just before assembler is output for it.
19883    For Thumb-1, we track the status of the condition codes; this
19884    information is used in the cbranchsi4_insn pattern.  */
19885 void
19886 thumb1_final_prescan_insn (rtx insn)
19887 {
19888   if (flag_print_asm_name)
19889     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
19890                  INSN_ADDRESSES (INSN_UID (insn)));
19891   /* Don't overwrite the previous setter when we get to a cbranch.  */
19892   if (INSN_CODE (insn) != CODE_FOR_cbranchsi4_insn)
19893     {
19894       enum attr_conds conds;
19895
19896       if (cfun->machine->thumb1_cc_insn)
19897         {
19898           if (modified_in_p (cfun->machine->thumb1_cc_op0, insn)
19899               || modified_in_p (cfun->machine->thumb1_cc_op1, insn))
19900             CC_STATUS_INIT;
19901         }
19902       conds = get_attr_conds (insn);
19903       if (conds == CONDS_SET)
19904         {
19905           rtx set = single_set (insn);
19906           cfun->machine->thumb1_cc_insn = insn;
19907           cfun->machine->thumb1_cc_op0 = SET_DEST (set);
19908           cfun->machine->thumb1_cc_op1 = const0_rtx;
19909           cfun->machine->thumb1_cc_mode = CC_NOOVmode;
19910           if (INSN_CODE (insn) == CODE_FOR_thumb1_subsi3_insn)
19911             {
19912               rtx src1 = XEXP (SET_SRC (set), 1);
19913               if (src1 == const0_rtx)
19914                 cfun->machine->thumb1_cc_mode = CCmode;
19915             }
19916         }
19917       else if (conds != CONDS_NOCOND)
19918         cfun->machine->thumb1_cc_insn = NULL_RTX;
19919     }
19920 }
19921
19922 int
19923 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
19924 {
19925   unsigned HOST_WIDE_INT mask = 0xff;
19926   int i;
19927
19928   val = val & (unsigned HOST_WIDE_INT)0xffffffffu;
19929   if (val == 0) /* XXX */
19930     return 0;
19931
19932   for (i = 0; i < 25; i++)
19933     if ((val & (mask << i)) == val)
19934       return 1;
19935
19936   return 0;
19937 }
19938
19939 /* Returns nonzero if the current function contains,
19940    or might contain a far jump.  */
19941 static int
19942 thumb_far_jump_used_p (void)
19943 {
19944   rtx insn;
19945
19946   /* This test is only important for leaf functions.  */
19947   /* assert (!leaf_function_p ()); */
19948
19949   /* If we have already decided that far jumps may be used,
19950      do not bother checking again, and always return true even if
19951      it turns out that they are not being used.  Once we have made
19952      the decision that far jumps are present (and that hence the link
19953      register will be pushed onto the stack) we cannot go back on it.  */
19954   if (cfun->machine->far_jump_used)
19955     return 1;
19956
19957   /* If this function is not being called from the prologue/epilogue
19958      generation code then it must be being called from the
19959      INITIAL_ELIMINATION_OFFSET macro.  */
19960   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
19961     {
19962       /* In this case we know that we are being asked about the elimination
19963          of the arg pointer register.  If that register is not being used,
19964          then there are no arguments on the stack, and we do not have to
19965          worry that a far jump might force the prologue to push the link
19966          register, changing the stack offsets.  In this case we can just
19967          return false, since the presence of far jumps in the function will
19968          not affect stack offsets.
19969
19970          If the arg pointer is live (or if it was live, but has now been
19971          eliminated and so set to dead) then we do have to test to see if
19972          the function might contain a far jump.  This test can lead to some
19973          false negatives, since before reload is completed, then length of
19974          branch instructions is not known, so gcc defaults to returning their
19975          longest length, which in turn sets the far jump attribute to true.
19976
19977          A false negative will not result in bad code being generated, but it
19978          will result in a needless push and pop of the link register.  We
19979          hope that this does not occur too often.
19980
19981          If we need doubleword stack alignment this could affect the other
19982          elimination offsets so we can't risk getting it wrong.  */
19983       if (df_regs_ever_live_p (ARG_POINTER_REGNUM))
19984         cfun->machine->arg_pointer_live = 1;
19985       else if (!cfun->machine->arg_pointer_live)
19986         return 0;
19987     }
19988
19989   /* Check to see if the function contains a branch
19990      insn with the far jump attribute set.  */
19991   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
19992     {
19993       if (GET_CODE (insn) == JUMP_INSN
19994           /* Ignore tablejump patterns.  */
19995           && GET_CODE (PATTERN (insn)) != ADDR_VEC
19996           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
19997           && get_attr_far_jump (insn) == FAR_JUMP_YES
19998           )
19999         {
20000           /* Record the fact that we have decided that
20001              the function does use far jumps.  */
20002           cfun->machine->far_jump_used = 1;
20003           return 1;
20004         }
20005     }
20006
20007   return 0;
20008 }
20009
20010 /* Return nonzero if FUNC must be entered in ARM mode.  */
20011 int
20012 is_called_in_ARM_mode (tree func)
20013 {
20014   gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
20015
20016   /* Ignore the problem about functions whose address is taken.  */
20017   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
20018     return TRUE;
20019
20020 #ifdef ARM_PE
20021   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
20022 #else
20023   return FALSE;
20024 #endif
20025 }
20026
20027 /* Given the stack offsets and register mask in OFFSETS, decide how
20028    many additional registers to push instead of subtracting a constant
20029    from SP.  For epilogues the principle is the same except we use pop.
20030    FOR_PROLOGUE indicates which we're generating.  */
20031 static int
20032 thumb1_extra_regs_pushed (arm_stack_offsets *offsets, bool for_prologue)
20033 {
20034   HOST_WIDE_INT amount;
20035   unsigned long live_regs_mask = offsets->saved_regs_mask;
20036   /* Extract a mask of the ones we can give to the Thumb's push/pop
20037      instruction.  */
20038   unsigned long l_mask = live_regs_mask & (for_prologue ? 0x40ff : 0xff);
20039   /* Then count how many other high registers will need to be pushed.  */
20040   unsigned long high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
20041   int n_free, reg_base;
20042
20043   if (!for_prologue && frame_pointer_needed)
20044     amount = offsets->locals_base - offsets->saved_regs;
20045   else
20046     amount = offsets->outgoing_args - offsets->saved_regs;
20047
20048   /* If the stack frame size is 512 exactly, we can save one load
20049      instruction, which should make this a win even when optimizing
20050      for speed.  */
20051   if (!optimize_size && amount != 512)
20052     return 0;
20053
20054   /* Can't do this if there are high registers to push.  */
20055   if (high_regs_pushed != 0)
20056     return 0;
20057
20058   /* Shouldn't do it in the prologue if no registers would normally
20059      be pushed at all.  In the epilogue, also allow it if we'll have
20060      a pop insn for the PC.  */
20061   if  (l_mask == 0
20062        && (for_prologue
20063            || TARGET_BACKTRACE
20064            || (live_regs_mask & 1 << LR_REGNUM) == 0
20065            || TARGET_INTERWORK
20066            || crtl->args.pretend_args_size != 0))
20067     return 0;
20068
20069   /* Don't do this if thumb_expand_prologue wants to emit instructions
20070      between the push and the stack frame allocation.  */
20071   if (for_prologue
20072       && ((flag_pic && arm_pic_register != INVALID_REGNUM)
20073           || (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)))
20074     return 0;
20075
20076   reg_base = 0;
20077   n_free = 0;
20078   if (!for_prologue)
20079     {
20080       reg_base = arm_size_return_regs () / UNITS_PER_WORD;
20081       live_regs_mask >>= reg_base;
20082     }
20083
20084   while (reg_base + n_free < 8 && !(live_regs_mask & 1)
20085          && (for_prologue || call_used_regs[reg_base + n_free]))
20086     {
20087       live_regs_mask >>= 1;
20088       n_free++;
20089     }
20090
20091   if (n_free == 0)
20092     return 0;
20093   gcc_assert (amount / 4 * 4 == amount);
20094
20095   if (amount >= 512 && (amount - n_free * 4) < 512)
20096     return (amount - 508) / 4;
20097   if (amount <= n_free * 4)
20098     return amount / 4;
20099   return 0;
20100 }
20101
20102 /* The bits which aren't usefully expanded as rtl.  */
20103 const char *
20104 thumb_unexpanded_epilogue (void)
20105 {
20106   arm_stack_offsets *offsets;
20107   int regno;
20108   unsigned long live_regs_mask = 0;
20109   int high_regs_pushed = 0;
20110   int extra_pop;
20111   int had_to_push_lr;
20112   int size;
20113
20114   if (cfun->machine->return_used_this_function != 0)
20115     return "";
20116
20117   if (IS_NAKED (arm_current_func_type ()))
20118     return "";
20119
20120   offsets = arm_get_frame_offsets ();
20121   live_regs_mask = offsets->saved_regs_mask;
20122   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
20123
20124   /* If we can deduce the registers used from the function's return value.
20125      This is more reliable that examining df_regs_ever_live_p () because that
20126      will be set if the register is ever used in the function, not just if
20127      the register is used to hold a return value.  */
20128   size = arm_size_return_regs ();
20129
20130   extra_pop = thumb1_extra_regs_pushed (offsets, false);
20131   if (extra_pop > 0)
20132     {
20133       unsigned long extra_mask = (1 << extra_pop) - 1;
20134       live_regs_mask |= extra_mask << (size / UNITS_PER_WORD);
20135     }
20136
20137   /* The prolog may have pushed some high registers to use as
20138      work registers.  e.g. the testsuite file:
20139      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
20140      compiles to produce:
20141         push    {r4, r5, r6, r7, lr}
20142         mov     r7, r9
20143         mov     r6, r8
20144         push    {r6, r7}
20145      as part of the prolog.  We have to undo that pushing here.  */
20146
20147   if (high_regs_pushed)
20148     {
20149       unsigned long mask = live_regs_mask & 0xff;
20150       int next_hi_reg;
20151
20152       /* The available low registers depend on the size of the value we are
20153          returning.  */
20154       if (size <= 12)
20155         mask |=  1 << 3;
20156       if (size <= 8)
20157         mask |= 1 << 2;
20158
20159       if (mask == 0)
20160         /* Oh dear!  We have no low registers into which we can pop
20161            high registers!  */
20162         internal_error
20163           ("no low registers available for popping high registers");
20164
20165       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
20166         if (live_regs_mask & (1 << next_hi_reg))
20167           break;
20168
20169       while (high_regs_pushed)
20170         {
20171           /* Find lo register(s) into which the high register(s) can
20172              be popped.  */
20173           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
20174             {
20175               if (mask & (1 << regno))
20176                 high_regs_pushed--;
20177               if (high_regs_pushed == 0)
20178                 break;
20179             }
20180
20181           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
20182
20183           /* Pop the values into the low register(s).  */
20184           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
20185
20186           /* Move the value(s) into the high registers.  */
20187           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
20188             {
20189               if (mask & (1 << regno))
20190                 {
20191                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
20192                                regno);
20193
20194                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
20195                     if (live_regs_mask & (1 << next_hi_reg))
20196                       break;
20197                 }
20198             }
20199         }
20200       live_regs_mask &= ~0x0f00;
20201     }
20202
20203   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
20204   live_regs_mask &= 0xff;
20205
20206   if (crtl->args.pretend_args_size == 0 || TARGET_BACKTRACE)
20207     {
20208       /* Pop the return address into the PC.  */
20209       if (had_to_push_lr)
20210         live_regs_mask |= 1 << PC_REGNUM;
20211
20212       /* Either no argument registers were pushed or a backtrace
20213          structure was created which includes an adjusted stack
20214          pointer, so just pop everything.  */
20215       if (live_regs_mask)
20216         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
20217                        live_regs_mask);
20218
20219       /* We have either just popped the return address into the
20220          PC or it is was kept in LR for the entire function.
20221          Note that thumb_pushpop has already called thumb_exit if the
20222          PC was in the list.  */
20223       if (!had_to_push_lr)
20224         thumb_exit (asm_out_file, LR_REGNUM);
20225     }
20226   else
20227     {
20228       /* Pop everything but the return address.  */
20229       if (live_regs_mask)
20230         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
20231                        live_regs_mask);
20232
20233       if (had_to_push_lr)
20234         {
20235           if (size > 12)
20236             {
20237               /* We have no free low regs, so save one.  */
20238               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
20239                            LAST_ARG_REGNUM);
20240             }
20241
20242           /* Get the return address into a temporary register.  */
20243           thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
20244                          1 << LAST_ARG_REGNUM);
20245
20246           if (size > 12)
20247             {
20248               /* Move the return address to lr.  */
20249               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
20250                            LAST_ARG_REGNUM);
20251               /* Restore the low register.  */
20252               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
20253                            IP_REGNUM);
20254               regno = LR_REGNUM;
20255             }
20256           else
20257             regno = LAST_ARG_REGNUM;
20258         }
20259       else
20260         regno = LR_REGNUM;
20261
20262       /* Remove the argument registers that were pushed onto the stack.  */
20263       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
20264                    SP_REGNUM, SP_REGNUM,
20265                    crtl->args.pretend_args_size);
20266
20267       thumb_exit (asm_out_file, regno);
20268     }
20269
20270   return "";
20271 }
20272
20273 /* Functions to save and restore machine-specific function data.  */
20274 static struct machine_function *
20275 arm_init_machine_status (void)
20276 {
20277   struct machine_function *machine;
20278   machine = ggc_alloc_cleared_machine_function ();
20279
20280 #if ARM_FT_UNKNOWN != 0
20281   machine->func_type = ARM_FT_UNKNOWN;
20282 #endif
20283   return machine;
20284 }
20285
20286 /* Return an RTX indicating where the return address to the
20287    calling function can be found.  */
20288 rtx
20289 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
20290 {
20291   if (count != 0)
20292     return NULL_RTX;
20293
20294   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
20295 }
20296
20297 /* Do anything needed before RTL is emitted for each function.  */
20298 void
20299 arm_init_expanders (void)
20300 {
20301   /* Arrange to initialize and mark the machine per-function status.  */
20302   init_machine_status = arm_init_machine_status;
20303
20304   /* This is to stop the combine pass optimizing away the alignment
20305      adjustment of va_arg.  */
20306   /* ??? It is claimed that this should not be necessary.  */
20307   if (cfun)
20308     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
20309 }
20310
20311
20312 /* Like arm_compute_initial_elimination offset.  Simpler because there
20313    isn't an ABI specified frame pointer for Thumb.  Instead, we set it
20314    to point at the base of the local variables after static stack
20315    space for a function has been allocated.  */
20316
20317 HOST_WIDE_INT
20318 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
20319 {
20320   arm_stack_offsets *offsets;
20321
20322   offsets = arm_get_frame_offsets ();
20323
20324   switch (from)
20325     {
20326     case ARG_POINTER_REGNUM:
20327       switch (to)
20328         {
20329         case STACK_POINTER_REGNUM:
20330           return offsets->outgoing_args - offsets->saved_args;
20331
20332         case FRAME_POINTER_REGNUM:
20333           return offsets->soft_frame - offsets->saved_args;
20334
20335         case ARM_HARD_FRAME_POINTER_REGNUM:
20336           return offsets->saved_regs - offsets->saved_args;
20337
20338         case THUMB_HARD_FRAME_POINTER_REGNUM:
20339           return offsets->locals_base - offsets->saved_args;
20340
20341         default:
20342           gcc_unreachable ();
20343         }
20344       break;
20345
20346     case FRAME_POINTER_REGNUM:
20347       switch (to)
20348         {
20349         case STACK_POINTER_REGNUM:
20350           return offsets->outgoing_args - offsets->soft_frame;
20351
20352         case ARM_HARD_FRAME_POINTER_REGNUM:
20353           return offsets->saved_regs - offsets->soft_frame;
20354
20355         case THUMB_HARD_FRAME_POINTER_REGNUM:
20356           return offsets->locals_base - offsets->soft_frame;
20357
20358         default:
20359           gcc_unreachable ();
20360         }
20361       break;
20362
20363     default:
20364       gcc_unreachable ();
20365     }
20366 }
20367
20368 /* Generate the rest of a function's prologue.  */
20369 void
20370 thumb1_expand_prologue (void)
20371 {
20372   rtx insn, dwarf;
20373
20374   HOST_WIDE_INT amount;
20375   arm_stack_offsets *offsets;
20376   unsigned long func_type;
20377   int regno;
20378   unsigned long live_regs_mask;
20379
20380   func_type = arm_current_func_type ();
20381
20382   /* Naked functions don't have prologues.  */
20383   if (IS_NAKED (func_type))
20384     return;
20385
20386   if (IS_INTERRUPT (func_type))
20387     {
20388       error ("interrupt Service Routines cannot be coded in Thumb mode");
20389       return;
20390     }
20391
20392   offsets = arm_get_frame_offsets ();
20393   live_regs_mask = offsets->saved_regs_mask;
20394   /* Load the pic register before setting the frame pointer,
20395      so we can use r7 as a temporary work register.  */
20396   if (flag_pic && arm_pic_register != INVALID_REGNUM)
20397     arm_load_pic_register (live_regs_mask);
20398
20399   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
20400     emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
20401                     stack_pointer_rtx);
20402
20403   amount = offsets->outgoing_args - offsets->saved_regs;
20404   amount -= 4 * thumb1_extra_regs_pushed (offsets, true);
20405   if (amount)
20406     {
20407       if (amount < 512)
20408         {
20409           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
20410                                         GEN_INT (- amount)));
20411           RTX_FRAME_RELATED_P (insn) = 1;
20412         }
20413       else
20414         {
20415           rtx reg;
20416
20417           /* The stack decrement is too big for an immediate value in a single
20418              insn.  In theory we could issue multiple subtracts, but after
20419              three of them it becomes more space efficient to place the full
20420              value in the constant pool and load into a register.  (Also the
20421              ARM debugger really likes to see only one stack decrement per
20422              function).  So instead we look for a scratch register into which
20423              we can load the decrement, and then we subtract this from the
20424              stack pointer.  Unfortunately on the thumb the only available
20425              scratch registers are the argument registers, and we cannot use
20426              these as they may hold arguments to the function.  Instead we
20427              attempt to locate a call preserved register which is used by this
20428              function.  If we can find one, then we know that it will have
20429              been pushed at the start of the prologue and so we can corrupt
20430              it now.  */
20431           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
20432             if (live_regs_mask & (1 << regno))
20433               break;
20434
20435           gcc_assert(regno <= LAST_LO_REGNUM);
20436
20437           reg = gen_rtx_REG (SImode, regno);
20438
20439           emit_insn (gen_movsi (reg, GEN_INT (- amount)));
20440
20441           insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
20442                                         stack_pointer_rtx, reg));
20443           RTX_FRAME_RELATED_P (insn) = 1;
20444           dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
20445                                plus_constant (stack_pointer_rtx,
20446                                               -amount));
20447           RTX_FRAME_RELATED_P (dwarf) = 1;
20448           add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
20449         }
20450     }
20451
20452   if (frame_pointer_needed)
20453     thumb_set_frame_pointer (offsets);
20454
20455   /* If we are profiling, make sure no instructions are scheduled before
20456      the call to mcount.  Similarly if the user has requested no
20457      scheduling in the prolog.  Similarly if we want non-call exceptions
20458      using the EABI unwinder, to prevent faulting instructions from being
20459      swapped with a stack adjustment.  */
20460   if (crtl->profile || !TARGET_SCHED_PROLOG
20461       || (ARM_EABI_UNWIND_TABLES && cfun->can_throw_non_call_exceptions))
20462     emit_insn (gen_blockage ());
20463
20464   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
20465   if (live_regs_mask & 0xff)
20466     cfun->machine->lr_save_eliminated = 0;
20467 }
20468
20469
20470 void
20471 thumb1_expand_epilogue (void)
20472 {
20473   HOST_WIDE_INT amount;
20474   arm_stack_offsets *offsets;
20475   int regno;
20476
20477   /* Naked functions don't have prologues.  */
20478   if (IS_NAKED (arm_current_func_type ()))
20479     return;
20480
20481   offsets = arm_get_frame_offsets ();
20482   amount = offsets->outgoing_args - offsets->saved_regs;
20483
20484   if (frame_pointer_needed)
20485     {
20486       emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
20487       amount = offsets->locals_base - offsets->saved_regs;
20488     }
20489   amount -= 4 * thumb1_extra_regs_pushed (offsets, false);
20490
20491   gcc_assert (amount >= 0);
20492   if (amount)
20493     {
20494       if (amount < 512)
20495         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
20496                                GEN_INT (amount)));
20497       else
20498         {
20499           /* r3 is always free in the epilogue.  */
20500           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
20501
20502           emit_insn (gen_movsi (reg, GEN_INT (amount)));
20503           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
20504         }
20505     }
20506
20507   /* Emit a USE (stack_pointer_rtx), so that
20508      the stack adjustment will not be deleted.  */
20509   emit_insn (gen_prologue_use (stack_pointer_rtx));
20510
20511   if (crtl->profile || !TARGET_SCHED_PROLOG)
20512     emit_insn (gen_blockage ());
20513
20514   /* Emit a clobber for each insn that will be restored in the epilogue,
20515      so that flow2 will get register lifetimes correct.  */
20516   for (regno = 0; regno < 13; regno++)
20517     if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
20518       emit_clobber (gen_rtx_REG (SImode, regno));
20519
20520   if (! df_regs_ever_live_p (LR_REGNUM))
20521     emit_use (gen_rtx_REG (SImode, LR_REGNUM));
20522 }
20523
20524 static void
20525 thumb1_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
20526 {
20527   arm_stack_offsets *offsets;
20528   unsigned long live_regs_mask = 0;
20529   unsigned long l_mask;
20530   unsigned high_regs_pushed = 0;
20531   int cfa_offset = 0;
20532   int regno;
20533
20534   if (IS_NAKED (arm_current_func_type ()))
20535     return;
20536
20537   if (is_called_in_ARM_mode (current_function_decl))
20538     {
20539       const char * name;
20540
20541       gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
20542       gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
20543                   == SYMBOL_REF);
20544       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
20545
20546       /* Generate code sequence to switch us into Thumb mode.  */
20547       /* The .code 32 directive has already been emitted by
20548          ASM_DECLARE_FUNCTION_NAME.  */
20549       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
20550       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
20551
20552       /* Generate a label, so that the debugger will notice the
20553          change in instruction sets.  This label is also used by
20554          the assembler to bypass the ARM code when this function
20555          is called from a Thumb encoded function elsewhere in the
20556          same file.  Hence the definition of STUB_NAME here must
20557          agree with the definition in gas/config/tc-arm.c.  */
20558
20559 #define STUB_NAME ".real_start_of"
20560
20561       fprintf (f, "\t.code\t16\n");
20562 #ifdef ARM_PE
20563       if (arm_dllexport_name_p (name))
20564         name = arm_strip_name_encoding (name);
20565 #endif
20566       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
20567       fprintf (f, "\t.thumb_func\n");
20568       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
20569     }
20570
20571   if (crtl->args.pretend_args_size)
20572     {
20573       /* Output unwind directive for the stack adjustment.  */
20574       if (ARM_EABI_UNWIND_TABLES)
20575         fprintf (f, "\t.pad #%d\n",
20576                  crtl->args.pretend_args_size);
20577
20578       if (cfun->machine->uses_anonymous_args)
20579         {
20580           int num_pushes;
20581
20582           fprintf (f, "\tpush\t{");
20583
20584           num_pushes = ARM_NUM_INTS (crtl->args.pretend_args_size);
20585
20586           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
20587                regno <= LAST_ARG_REGNUM;
20588                regno++)
20589             asm_fprintf (f, "%r%s", regno,
20590                          regno == LAST_ARG_REGNUM ? "" : ", ");
20591
20592           fprintf (f, "}\n");
20593         }
20594       else
20595         asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
20596                      SP_REGNUM, SP_REGNUM,
20597                      crtl->args.pretend_args_size);
20598
20599       /* We don't need to record the stores for unwinding (would it
20600          help the debugger any if we did?), but record the change in
20601          the stack pointer.  */
20602       if (dwarf2out_do_frame ())
20603         {
20604           char *l = dwarf2out_cfi_label (false);
20605
20606           cfa_offset = cfa_offset + crtl->args.pretend_args_size;
20607           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
20608         }
20609     }
20610
20611   /* Get the registers we are going to push.  */
20612   offsets = arm_get_frame_offsets ();
20613   live_regs_mask = offsets->saved_regs_mask;
20614   /* Extract a mask of the ones we can give to the Thumb's push instruction.  */
20615   l_mask = live_regs_mask & 0x40ff;
20616   /* Then count how many other high registers will need to be pushed.  */
20617   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
20618
20619   if (TARGET_BACKTRACE)
20620     {
20621       unsigned offset;
20622       unsigned work_register;
20623
20624       /* We have been asked to create a stack backtrace structure.
20625          The code looks like this:
20626
20627          0   .align 2
20628          0   func:
20629          0     sub   SP, #16         Reserve space for 4 registers.
20630          2     push  {R7}            Push low registers.
20631          4     add   R7, SP, #20     Get the stack pointer before the push.
20632          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
20633          8     mov   R7, PC          Get hold of the start of this code plus 12.
20634         10     str   R7, [SP, #16]   Store it.
20635         12     mov   R7, FP          Get hold of the current frame pointer.
20636         14     str   R7, [SP, #4]    Store it.
20637         16     mov   R7, LR          Get hold of the current return address.
20638         18     str   R7, [SP, #12]   Store it.
20639         20     add   R7, SP, #16     Point at the start of the backtrace structure.
20640         22     mov   FP, R7          Put this value into the frame pointer.  */
20641
20642       work_register = thumb_find_work_register (live_regs_mask);
20643
20644       if (ARM_EABI_UNWIND_TABLES)
20645         asm_fprintf (f, "\t.pad #16\n");
20646
20647       asm_fprintf
20648         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
20649          SP_REGNUM, SP_REGNUM);
20650
20651       if (dwarf2out_do_frame ())
20652         {
20653           char *l = dwarf2out_cfi_label (false);
20654
20655           cfa_offset = cfa_offset + 16;
20656           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
20657         }
20658
20659       if (l_mask)
20660         {
20661           thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
20662           offset = bit_count (l_mask) * UNITS_PER_WORD;
20663         }
20664       else
20665         offset = 0;
20666
20667       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
20668                    offset + 16 + crtl->args.pretend_args_size);
20669
20670       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20671                    offset + 4);
20672
20673       /* Make sure that the instruction fetching the PC is in the right place
20674          to calculate "start of backtrace creation code + 12".  */
20675       if (l_mask)
20676         {
20677           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
20678           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20679                        offset + 12);
20680           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
20681                        ARM_HARD_FRAME_POINTER_REGNUM);
20682           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20683                        offset);
20684         }
20685       else
20686         {
20687           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
20688                        ARM_HARD_FRAME_POINTER_REGNUM);
20689           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20690                        offset);
20691           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
20692           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20693                        offset + 12);
20694         }
20695
20696       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
20697       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20698                    offset + 8);
20699       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
20700                    offset + 12);
20701       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
20702                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
20703     }
20704   /* Optimization:  If we are not pushing any low registers but we are going
20705      to push some high registers then delay our first push.  This will just
20706      be a push of LR and we can combine it with the push of the first high
20707      register.  */
20708   else if ((l_mask & 0xff) != 0
20709            || (high_regs_pushed == 0 && l_mask))
20710     {
20711       unsigned long mask = l_mask;
20712       mask |= (1 << thumb1_extra_regs_pushed (offsets, true)) - 1;
20713       thumb_pushpop (f, mask, 1, &cfa_offset, mask);
20714     }
20715
20716   if (high_regs_pushed)
20717     {
20718       unsigned pushable_regs;
20719       unsigned next_hi_reg;
20720
20721       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
20722         if (live_regs_mask & (1 << next_hi_reg))
20723           break;
20724
20725       pushable_regs = l_mask & 0xff;
20726
20727       if (pushable_regs == 0)
20728         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
20729
20730       while (high_regs_pushed > 0)
20731         {
20732           unsigned long real_regs_mask = 0;
20733
20734           for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
20735             {
20736               if (pushable_regs & (1 << regno))
20737                 {
20738                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
20739
20740                   high_regs_pushed --;
20741                   real_regs_mask |= (1 << next_hi_reg);
20742
20743                   if (high_regs_pushed)
20744                     {
20745                       for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
20746                            next_hi_reg --)
20747                         if (live_regs_mask & (1 << next_hi_reg))
20748                           break;
20749                     }
20750                   else
20751                     {
20752                       pushable_regs &= ~((1 << regno) - 1);
20753                       break;
20754                     }
20755                 }
20756             }
20757
20758           /* If we had to find a work register and we have not yet
20759              saved the LR then add it to the list of regs to push.  */
20760           if (l_mask == (1 << LR_REGNUM))
20761             {
20762               thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
20763                              1, &cfa_offset,
20764                              real_regs_mask | (1 << LR_REGNUM));
20765               l_mask = 0;
20766             }
20767           else
20768             thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
20769         }
20770     }
20771 }
20772
20773 /* Handle the case of a double word load into a low register from
20774    a computed memory address.  The computed address may involve a
20775    register which is overwritten by the load.  */
20776 const char *
20777 thumb_load_double_from_address (rtx *operands)
20778 {
20779   rtx addr;
20780   rtx base;
20781   rtx offset;
20782   rtx arg1;
20783   rtx arg2;
20784
20785   gcc_assert (GET_CODE (operands[0]) == REG);
20786   gcc_assert (GET_CODE (operands[1]) == MEM);
20787
20788   /* Get the memory address.  */
20789   addr = XEXP (operands[1], 0);
20790
20791   /* Work out how the memory address is computed.  */
20792   switch (GET_CODE (addr))
20793     {
20794     case REG:
20795       operands[2] = adjust_address (operands[1], SImode, 4);
20796
20797       if (REGNO (operands[0]) == REGNO (addr))
20798         {
20799           output_asm_insn ("ldr\t%H0, %2", operands);
20800           output_asm_insn ("ldr\t%0, %1", operands);
20801         }
20802       else
20803         {
20804           output_asm_insn ("ldr\t%0, %1", operands);
20805           output_asm_insn ("ldr\t%H0, %2", operands);
20806         }
20807       break;
20808
20809     case CONST:
20810       /* Compute <address> + 4 for the high order load.  */
20811       operands[2] = adjust_address (operands[1], SImode, 4);
20812
20813       output_asm_insn ("ldr\t%0, %1", operands);
20814       output_asm_insn ("ldr\t%H0, %2", operands);
20815       break;
20816
20817     case PLUS:
20818       arg1   = XEXP (addr, 0);
20819       arg2   = XEXP (addr, 1);
20820
20821       if (CONSTANT_P (arg1))
20822         base = arg2, offset = arg1;
20823       else
20824         base = arg1, offset = arg2;
20825
20826       gcc_assert (GET_CODE (base) == REG);
20827
20828       /* Catch the case of <address> = <reg> + <reg> */
20829       if (GET_CODE (offset) == REG)
20830         {
20831           int reg_offset = REGNO (offset);
20832           int reg_base   = REGNO (base);
20833           int reg_dest   = REGNO (operands[0]);
20834
20835           /* Add the base and offset registers together into the
20836              higher destination register.  */
20837           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
20838                        reg_dest + 1, reg_base, reg_offset);
20839
20840           /* Load the lower destination register from the address in
20841              the higher destination register.  */
20842           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
20843                        reg_dest, reg_dest + 1);
20844
20845           /* Load the higher destination register from its own address
20846              plus 4.  */
20847           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
20848                        reg_dest + 1, reg_dest + 1);
20849         }
20850       else
20851         {
20852           /* Compute <address> + 4 for the high order load.  */
20853           operands[2] = adjust_address (operands[1], SImode, 4);
20854
20855           /* If the computed address is held in the low order register
20856              then load the high order register first, otherwise always
20857              load the low order register first.  */
20858           if (REGNO (operands[0]) == REGNO (base))
20859             {
20860               output_asm_insn ("ldr\t%H0, %2", operands);
20861               output_asm_insn ("ldr\t%0, %1", operands);
20862             }
20863           else
20864             {
20865               output_asm_insn ("ldr\t%0, %1", operands);
20866               output_asm_insn ("ldr\t%H0, %2", operands);
20867             }
20868         }
20869       break;
20870
20871     case LABEL_REF:
20872       /* With no registers to worry about we can just load the value
20873          directly.  */
20874       operands[2] = adjust_address (operands[1], SImode, 4);
20875
20876       output_asm_insn ("ldr\t%H0, %2", operands);
20877       output_asm_insn ("ldr\t%0, %1", operands);
20878       break;
20879
20880     default:
20881       gcc_unreachable ();
20882     }
20883
20884   return "";
20885 }
20886
20887 const char *
20888 thumb_output_move_mem_multiple (int n, rtx *operands)
20889 {
20890   rtx tmp;
20891
20892   switch (n)
20893     {
20894     case 2:
20895       if (REGNO (operands[4]) > REGNO (operands[5]))
20896         {
20897           tmp = operands[4];
20898           operands[4] = operands[5];
20899           operands[5] = tmp;
20900         }
20901       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
20902       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
20903       break;
20904
20905     case 3:
20906       if (REGNO (operands[4]) > REGNO (operands[5]))
20907         {
20908           tmp = operands[4];
20909           operands[4] = operands[5];
20910           operands[5] = tmp;
20911         }
20912       if (REGNO (operands[5]) > REGNO (operands[6]))
20913         {
20914           tmp = operands[5];
20915           operands[5] = operands[6];
20916           operands[6] = tmp;
20917         }
20918       if (REGNO (operands[4]) > REGNO (operands[5]))
20919         {
20920           tmp = operands[4];
20921           operands[4] = operands[5];
20922           operands[5] = tmp;
20923         }
20924
20925       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
20926       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
20927       break;
20928
20929     default:
20930       gcc_unreachable ();
20931     }
20932
20933   return "";
20934 }
20935
20936 /* Output a call-via instruction for thumb state.  */
20937 const char *
20938 thumb_call_via_reg (rtx reg)
20939 {
20940   int regno = REGNO (reg);
20941   rtx *labelp;
20942
20943   gcc_assert (regno < LR_REGNUM);
20944
20945   /* If we are in the normal text section we can use a single instance
20946      per compilation unit.  If we are doing function sections, then we need
20947      an entry per section, since we can't rely on reachability.  */
20948   if (in_section == text_section)
20949     {
20950       thumb_call_reg_needed = 1;
20951
20952       if (thumb_call_via_label[regno] == NULL)
20953         thumb_call_via_label[regno] = gen_label_rtx ();
20954       labelp = thumb_call_via_label + regno;
20955     }
20956   else
20957     {
20958       if (cfun->machine->call_via[regno] == NULL)
20959         cfun->machine->call_via[regno] = gen_label_rtx ();
20960       labelp = cfun->machine->call_via + regno;
20961     }
20962
20963   output_asm_insn ("bl\t%a0", labelp);
20964   return "";
20965 }
20966
20967 /* Routines for generating rtl.  */
20968 void
20969 thumb_expand_movmemqi (rtx *operands)
20970 {
20971   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
20972   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
20973   HOST_WIDE_INT len = INTVAL (operands[2]);
20974   HOST_WIDE_INT offset = 0;
20975
20976   while (len >= 12)
20977     {
20978       emit_insn (gen_movmem12b (out, in, out, in));
20979       len -= 12;
20980     }
20981
20982   if (len >= 8)
20983     {
20984       emit_insn (gen_movmem8b (out, in, out, in));
20985       len -= 8;
20986     }
20987
20988   if (len >= 4)
20989     {
20990       rtx reg = gen_reg_rtx (SImode);
20991       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
20992       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
20993       len -= 4;
20994       offset += 4;
20995     }
20996
20997   if (len >= 2)
20998     {
20999       rtx reg = gen_reg_rtx (HImode);
21000       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
21001                                               plus_constant (in, offset))));
21002       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
21003                             reg));
21004       len -= 2;
21005       offset += 2;
21006     }
21007
21008   if (len)
21009     {
21010       rtx reg = gen_reg_rtx (QImode);
21011       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
21012                                               plus_constant (in, offset))));
21013       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
21014                             reg));
21015     }
21016 }
21017
21018 void
21019 thumb_reload_out_hi (rtx *operands)
21020 {
21021   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
21022 }
21023
21024 /* Handle reading a half-word from memory during reload.  */
21025 void
21026 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
21027 {
21028   gcc_unreachable ();
21029 }
21030
21031 /* Return the length of a function name prefix
21032     that starts with the character 'c'.  */
21033 static int
21034 arm_get_strip_length (int c)
21035 {
21036   switch (c)
21037     {
21038     ARM_NAME_ENCODING_LENGTHS
21039       default: return 0;
21040     }
21041 }
21042
21043 /* Return a pointer to a function's name with any
21044    and all prefix encodings stripped from it.  */
21045 const char *
21046 arm_strip_name_encoding (const char *name)
21047 {
21048   int skip;
21049
21050   while ((skip = arm_get_strip_length (* name)))
21051     name += skip;
21052
21053   return name;
21054 }
21055
21056 /* If there is a '*' anywhere in the name's prefix, then
21057    emit the stripped name verbatim, otherwise prepend an
21058    underscore if leading underscores are being used.  */
21059 void
21060 arm_asm_output_labelref (FILE *stream, const char *name)
21061 {
21062   int skip;
21063   int verbatim = 0;
21064
21065   while ((skip = arm_get_strip_length (* name)))
21066     {
21067       verbatim |= (*name == '*');
21068       name += skip;
21069     }
21070
21071   if (verbatim)
21072     fputs (name, stream);
21073   else
21074     asm_fprintf (stream, "%U%s", name);
21075 }
21076
21077 static void
21078 arm_file_start (void)
21079 {
21080   int val;
21081
21082   if (TARGET_UNIFIED_ASM)
21083     asm_fprintf (asm_out_file, "\t.syntax unified\n");
21084
21085   if (TARGET_BPABI)
21086     {
21087       const char *fpu_name;
21088       if (arm_selected_arch)
21089         asm_fprintf (asm_out_file, "\t.arch %s\n", arm_selected_arch->name);
21090       else
21091         asm_fprintf (asm_out_file, "\t.cpu %s\n", arm_selected_cpu->name);
21092
21093       if (TARGET_SOFT_FLOAT)
21094         {
21095           if (TARGET_VFP)
21096             fpu_name = "softvfp";
21097           else
21098             fpu_name = "softfpa";
21099         }
21100       else
21101         {
21102           fpu_name = arm_fpu_desc->name;
21103           if (arm_fpu_desc->model == ARM_FP_MODEL_VFP)
21104             {
21105               if (TARGET_HARD_FLOAT)
21106                 asm_fprintf (asm_out_file, "\t.eabi_attribute 27, 3\n");
21107               if (TARGET_HARD_FLOAT_ABI)
21108                 asm_fprintf (asm_out_file, "\t.eabi_attribute 28, 1\n");
21109             }
21110         }
21111       asm_fprintf (asm_out_file, "\t.fpu %s\n", fpu_name);
21112
21113       /* Some of these attributes only apply when the corresponding features
21114          are used.  However we don't have any easy way of figuring this out.
21115          Conservatively record the setting that would have been used.  */
21116
21117       /* Tag_ABI_FP_rounding.  */
21118       if (flag_rounding_math)
21119         asm_fprintf (asm_out_file, "\t.eabi_attribute 19, 1\n");
21120       if (!flag_unsafe_math_optimizations)
21121         {
21122           /* Tag_ABI_FP_denomal.  */
21123           asm_fprintf (asm_out_file, "\t.eabi_attribute 20, 1\n");
21124           /* Tag_ABI_FP_exceptions.  */
21125           asm_fprintf (asm_out_file, "\t.eabi_attribute 21, 1\n");
21126         }
21127       /* Tag_ABI_FP_user_exceptions.  */
21128       if (flag_signaling_nans)
21129         asm_fprintf (asm_out_file, "\t.eabi_attribute 22, 1\n");
21130       /* Tag_ABI_FP_number_model.  */
21131       asm_fprintf (asm_out_file, "\t.eabi_attribute 23, %d\n", 
21132                    flag_finite_math_only ? 1 : 3);
21133
21134       /* Tag_ABI_align8_needed.  */
21135       asm_fprintf (asm_out_file, "\t.eabi_attribute 24, 1\n");
21136       /* Tag_ABI_align8_preserved.  */
21137       asm_fprintf (asm_out_file, "\t.eabi_attribute 25, 1\n");
21138       /* Tag_ABI_enum_size.  */
21139       asm_fprintf (asm_out_file, "\t.eabi_attribute 26, %d\n",
21140                    flag_short_enums ? 1 : 2);
21141
21142       /* Tag_ABI_optimization_goals.  */
21143       if (optimize_size)
21144         val = 4;
21145       else if (optimize >= 2)
21146         val = 2;
21147       else if (optimize)
21148         val = 1;
21149       else
21150         val = 6;
21151       asm_fprintf (asm_out_file, "\t.eabi_attribute 30, %d\n", val);
21152
21153       /* Tag_ABI_FP_16bit_format.  */
21154       if (arm_fp16_format)
21155         asm_fprintf (asm_out_file, "\t.eabi_attribute 38, %d\n",
21156                      (int)arm_fp16_format);
21157
21158       if (arm_lang_output_object_attributes_hook)
21159         arm_lang_output_object_attributes_hook();
21160     }
21161   default_file_start();
21162 }
21163
21164 static void
21165 arm_file_end (void)
21166 {
21167   int regno;
21168
21169   if (NEED_INDICATE_EXEC_STACK)
21170     /* Add .note.GNU-stack.  */
21171     file_end_indicate_exec_stack ();
21172
21173   if (! thumb_call_reg_needed)
21174     return;
21175
21176   switch_to_section (text_section);
21177   asm_fprintf (asm_out_file, "\t.code 16\n");
21178   ASM_OUTPUT_ALIGN (asm_out_file, 1);
21179
21180   for (regno = 0; regno < LR_REGNUM; regno++)
21181     {
21182       rtx label = thumb_call_via_label[regno];
21183
21184       if (label != 0)
21185         {
21186           targetm.asm_out.internal_label (asm_out_file, "L",
21187                                           CODE_LABEL_NUMBER (label));
21188           asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
21189         }
21190     }
21191 }
21192
21193 #ifndef ARM_PE
21194 /* Symbols in the text segment can be accessed without indirecting via the
21195    constant pool; it may take an extra binary operation, but this is still
21196    faster than indirecting via memory.  Don't do this when not optimizing,
21197    since we won't be calculating al of the offsets necessary to do this
21198    simplification.  */
21199
21200 static void
21201 arm_encode_section_info (tree decl, rtx rtl, int first)
21202 {
21203   if (optimize > 0 && TREE_CONSTANT (decl))
21204     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
21205
21206   default_encode_section_info (decl, rtl, first);
21207 }
21208 #endif /* !ARM_PE */
21209
21210 static void
21211 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
21212 {
21213   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
21214       && !strcmp (prefix, "L"))
21215     {
21216       arm_ccfsm_state = 0;
21217       arm_target_insn = NULL;
21218     }
21219   default_internal_label (stream, prefix, labelno);
21220 }
21221
21222 /* Output code to add DELTA to the first argument, and then jump
21223    to FUNCTION.  Used for C++ multiple inheritance.  */
21224 static void
21225 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
21226                      HOST_WIDE_INT delta,
21227                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
21228                      tree function)
21229 {
21230   static int thunk_label = 0;
21231   char label[256];
21232   char labelpc[256];
21233   int mi_delta = delta;
21234   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
21235   int shift = 0;
21236   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
21237                     ? 1 : 0);
21238   if (mi_delta < 0)
21239     mi_delta = - mi_delta;
21240
21241   if (TARGET_THUMB1)
21242     {
21243       int labelno = thunk_label++;
21244       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
21245       /* Thunks are entered in arm mode when avaiable.  */
21246       if (TARGET_THUMB1_ONLY)
21247         {
21248           /* push r3 so we can use it as a temporary.  */
21249           /* TODO: Omit this save if r3 is not used.  */
21250           fputs ("\tpush {r3}\n", file);
21251           fputs ("\tldr\tr3, ", file);
21252         }
21253       else
21254         {
21255           fputs ("\tldr\tr12, ", file);
21256         }
21257       assemble_name (file, label);
21258       fputc ('\n', file);
21259       if (flag_pic)
21260         {
21261           /* If we are generating PIC, the ldr instruction below loads
21262              "(target - 7) - .LTHUNKPCn" into r12.  The pc reads as
21263              the address of the add + 8, so we have:
21264
21265              r12 = (target - 7) - .LTHUNKPCn + (.LTHUNKPCn + 8)
21266                  = target + 1.
21267
21268              Note that we have "+ 1" because some versions of GNU ld
21269              don't set the low bit of the result for R_ARM_REL32
21270              relocations against thumb function symbols.
21271              On ARMv6M this is +4, not +8.  */
21272           ASM_GENERATE_INTERNAL_LABEL (labelpc, "LTHUNKPC", labelno);
21273           assemble_name (file, labelpc);
21274           fputs (":\n", file);
21275           if (TARGET_THUMB1_ONLY)
21276             {
21277               /* This is 2 insns after the start of the thunk, so we know it
21278                  is 4-byte aligned.  */
21279               fputs ("\tadd\tr3, pc, r3\n", file);
21280               fputs ("\tmov r12, r3\n", file);
21281             }
21282           else
21283             fputs ("\tadd\tr12, pc, r12\n", file);
21284         }
21285       else if (TARGET_THUMB1_ONLY)
21286         fputs ("\tmov r12, r3\n", file);
21287     }
21288   if (TARGET_THUMB1_ONLY)
21289     {
21290       if (mi_delta > 255)
21291         {
21292           fputs ("\tldr\tr3, ", file);
21293           assemble_name (file, label);
21294           fputs ("+4\n", file);
21295           asm_fprintf (file, "\t%s\t%r, %r, r3\n",
21296                        mi_op, this_regno, this_regno);
21297         }
21298       else if (mi_delta != 0)
21299         {
21300           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
21301                        mi_op, this_regno, this_regno,
21302                        mi_delta);
21303         }
21304     }
21305   else
21306     {
21307       /* TODO: Use movw/movt for large constants when available.  */
21308       while (mi_delta != 0)
21309         {
21310           if ((mi_delta & (3 << shift)) == 0)
21311             shift += 2;
21312           else
21313             {
21314               asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
21315                            mi_op, this_regno, this_regno,
21316                            mi_delta & (0xff << shift));
21317               mi_delta &= ~(0xff << shift);
21318               shift += 8;
21319             }
21320         }
21321     }
21322   if (TARGET_THUMB1)
21323     {
21324       if (TARGET_THUMB1_ONLY)
21325         fputs ("\tpop\t{r3}\n", file);
21326
21327       fprintf (file, "\tbx\tr12\n");
21328       ASM_OUTPUT_ALIGN (file, 2);
21329       assemble_name (file, label);
21330       fputs (":\n", file);
21331       if (flag_pic)
21332         {
21333           /* Output ".word .LTHUNKn-7-.LTHUNKPCn".  */
21334           rtx tem = XEXP (DECL_RTL (function), 0);
21335           tem = gen_rtx_PLUS (GET_MODE (tem), tem, GEN_INT (-7));
21336           tem = gen_rtx_MINUS (GET_MODE (tem),
21337                                tem,
21338                                gen_rtx_SYMBOL_REF (Pmode,
21339                                                    ggc_strdup (labelpc)));
21340           assemble_integer (tem, 4, BITS_PER_WORD, 1);
21341         }
21342       else
21343         /* Output ".word .LTHUNKn".  */
21344         assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
21345
21346       if (TARGET_THUMB1_ONLY && mi_delta > 255)
21347         assemble_integer (GEN_INT(mi_delta), 4, BITS_PER_WORD, 1);
21348     }
21349   else
21350     {
21351       fputs ("\tb\t", file);
21352       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
21353       if (NEED_PLT_RELOC)
21354         fputs ("(PLT)", file);
21355       fputc ('\n', file);
21356     }
21357 }
21358
21359 int
21360 arm_emit_vector_const (FILE *file, rtx x)
21361 {
21362   int i;
21363   const char * pattern;
21364
21365   gcc_assert (GET_CODE (x) == CONST_VECTOR);
21366
21367   switch (GET_MODE (x))
21368     {
21369     case V2SImode: pattern = "%08x"; break;
21370     case V4HImode: pattern = "%04x"; break;
21371     case V8QImode: pattern = "%02x"; break;
21372     default:       gcc_unreachable ();
21373     }
21374
21375   fprintf (file, "0x");
21376   for (i = CONST_VECTOR_NUNITS (x); i--;)
21377     {
21378       rtx element;
21379
21380       element = CONST_VECTOR_ELT (x, i);
21381       fprintf (file, pattern, INTVAL (element));
21382     }
21383
21384   return 1;
21385 }
21386
21387 /* Emit a fp16 constant appropriately padded to occupy a 4-byte word.
21388    HFmode constant pool entries are actually loaded with ldr.  */
21389 void
21390 arm_emit_fp16_const (rtx c)
21391 {
21392   REAL_VALUE_TYPE r;
21393   long bits;
21394
21395   REAL_VALUE_FROM_CONST_DOUBLE (r, c);
21396   bits = real_to_target (NULL, &r, HFmode);
21397   if (WORDS_BIG_ENDIAN)
21398     assemble_zeros (2);
21399   assemble_integer (GEN_INT (bits), 2, BITS_PER_WORD, 1);
21400   if (!WORDS_BIG_ENDIAN)
21401     assemble_zeros (2);
21402 }
21403
21404 const char *
21405 arm_output_load_gr (rtx *operands)
21406 {
21407   rtx reg;
21408   rtx offset;
21409   rtx wcgr;
21410   rtx sum;
21411
21412   if (GET_CODE (operands [1]) != MEM
21413       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
21414       || GET_CODE (reg = XEXP (sum, 0)) != REG
21415       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
21416       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
21417     return "wldrw%?\t%0, %1";
21418
21419   /* Fix up an out-of-range load of a GR register.  */
21420   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
21421   wcgr = operands[0];
21422   operands[0] = reg;
21423   output_asm_insn ("ldr%?\t%0, %1", operands);
21424
21425   operands[0] = wcgr;
21426   operands[1] = reg;
21427   output_asm_insn ("tmcr%?\t%0, %1", operands);
21428   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
21429
21430   return "";
21431 }
21432
21433 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
21434
21435    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
21436    named arg and all anonymous args onto the stack.
21437    XXX I know the prologue shouldn't be pushing registers, but it is faster
21438    that way.  */
21439
21440 static void
21441 arm_setup_incoming_varargs (CUMULATIVE_ARGS *pcum,
21442                             enum machine_mode mode,
21443                             tree type,
21444                             int *pretend_size,
21445                             int second_time ATTRIBUTE_UNUSED)
21446 {
21447   int nregs;
21448   
21449   cfun->machine->uses_anonymous_args = 1;
21450   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
21451     {
21452       nregs = pcum->aapcs_ncrn;
21453       if ((nregs & 1) && arm_needs_doubleword_align (mode, type))
21454         nregs++;
21455     }
21456   else
21457     nregs = pcum->nregs;
21458   
21459   if (nregs < NUM_ARG_REGS)
21460     *pretend_size = (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
21461 }
21462
21463 /* Return nonzero if the CONSUMER instruction (a store) does not need
21464    PRODUCER's value to calculate the address.  */
21465
21466 int
21467 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
21468 {
21469   rtx value = PATTERN (producer);
21470   rtx addr = PATTERN (consumer);
21471
21472   if (GET_CODE (value) == COND_EXEC)
21473     value = COND_EXEC_CODE (value);
21474   if (GET_CODE (value) == PARALLEL)
21475     value = XVECEXP (value, 0, 0);
21476   value = XEXP (value, 0);
21477   if (GET_CODE (addr) == COND_EXEC)
21478     addr = COND_EXEC_CODE (addr);
21479   if (GET_CODE (addr) == PARALLEL)
21480     addr = XVECEXP (addr, 0, 0);
21481   addr = XEXP (addr, 0);
21482
21483   return !reg_overlap_mentioned_p (value, addr);
21484 }
21485
21486 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
21487    have an early register shift value or amount dependency on the
21488    result of PRODUCER.  */
21489
21490 int
21491 arm_no_early_alu_shift_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   /* This is either an actual independent shift, or a shift applied to
21510      the first operand of another operation.  We want the whole shift
21511      operation.  */
21512   if (GET_CODE (early_op) == REG)
21513     early_op = op;
21514
21515   return !reg_overlap_mentioned_p (value, early_op);
21516 }
21517
21518 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
21519    have an early register shift value dependency on the result of
21520    PRODUCER.  */
21521
21522 int
21523 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
21524 {
21525   rtx value = PATTERN (producer);
21526   rtx op = PATTERN (consumer);
21527   rtx early_op;
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   early_op = XEXP (op, 0);
21541
21542   /* This is either an actual independent shift, or a shift applied to
21543      the first operand of another operation.  We want the value being
21544      shifted, in either case.  */
21545   if (GET_CODE (early_op) != REG)
21546     early_op = XEXP (early_op, 0);
21547
21548   return !reg_overlap_mentioned_p (value, early_op);
21549 }
21550
21551 /* Return nonzero if the CONSUMER (a mul or mac op) does not
21552    have an early register mult dependency on the result of
21553    PRODUCER.  */
21554
21555 int
21556 arm_no_early_mul_dep (rtx producer, rtx consumer)
21557 {
21558   rtx value = PATTERN (producer);
21559   rtx op = PATTERN (consumer);
21560
21561   if (GET_CODE (value) == COND_EXEC)
21562     value = COND_EXEC_CODE (value);
21563   if (GET_CODE (value) == PARALLEL)
21564     value = XVECEXP (value, 0, 0);
21565   value = XEXP (value, 0);
21566   if (GET_CODE (op) == COND_EXEC)
21567     op = COND_EXEC_CODE (op);
21568   if (GET_CODE (op) == PARALLEL)
21569     op = XVECEXP (op, 0, 0);
21570   op = XEXP (op, 1);
21571
21572   if (GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
21573     {
21574       if (GET_CODE (XEXP (op, 0)) == MULT)
21575         return !reg_overlap_mentioned_p (value, XEXP (op, 0));
21576       else
21577         return !reg_overlap_mentioned_p (value, XEXP (op, 1));
21578     }
21579
21580   return 0;
21581 }
21582
21583 /* We can't rely on the caller doing the proper promotion when
21584    using APCS or ATPCS.  */
21585
21586 static bool
21587 arm_promote_prototypes (const_tree t ATTRIBUTE_UNUSED)
21588 {
21589     return !TARGET_AAPCS_BASED;
21590 }
21591
21592 static enum machine_mode
21593 arm_promote_function_mode (const_tree type ATTRIBUTE_UNUSED,
21594                            enum machine_mode mode,
21595                            int *punsignedp ATTRIBUTE_UNUSED,
21596                            const_tree fntype ATTRIBUTE_UNUSED,
21597                            int for_return ATTRIBUTE_UNUSED)
21598 {
21599   if (GET_MODE_CLASS (mode) == MODE_INT
21600       && GET_MODE_SIZE (mode) < 4)
21601     return SImode;
21602
21603   return mode;
21604 }
21605
21606 /* AAPCS based ABIs use short enums by default.  */
21607
21608 static bool
21609 arm_default_short_enums (void)
21610 {
21611   return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
21612 }
21613
21614
21615 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
21616
21617 static bool
21618 arm_align_anon_bitfield (void)
21619 {
21620   return TARGET_AAPCS_BASED;
21621 }
21622
21623
21624 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
21625
21626 static tree
21627 arm_cxx_guard_type (void)
21628 {
21629   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
21630 }
21631
21632 /* Return non-zero if the consumer (a multiply-accumulate instruction)
21633    has an accumulator dependency on the result of the producer (a
21634    multiplication instruction) and no other dependency on that result.  */
21635 int
21636 arm_mac_accumulator_is_mul_result (rtx producer, rtx consumer)
21637 {
21638   rtx mul = PATTERN (producer);
21639   rtx mac = PATTERN (consumer);
21640   rtx mul_result;
21641   rtx mac_op0, mac_op1, mac_acc;
21642
21643   if (GET_CODE (mul) == COND_EXEC)
21644     mul = COND_EXEC_CODE (mul);
21645   if (GET_CODE (mac) == COND_EXEC)
21646     mac = COND_EXEC_CODE (mac);
21647
21648   /* Check that mul is of the form (set (...) (mult ...))
21649      and mla is of the form (set (...) (plus (mult ...) (...))).  */
21650   if ((GET_CODE (mul) != SET || GET_CODE (XEXP (mul, 1)) != MULT)
21651       || (GET_CODE (mac) != SET || GET_CODE (XEXP (mac, 1)) != PLUS
21652           || GET_CODE (XEXP (XEXP (mac, 1), 0)) != MULT))
21653     return 0;
21654
21655   mul_result = XEXP (mul, 0);
21656   mac_op0 = XEXP (XEXP (XEXP (mac, 1), 0), 0);
21657   mac_op1 = XEXP (XEXP (XEXP (mac, 1), 0), 1);
21658   mac_acc = XEXP (XEXP (mac, 1), 1);
21659
21660   return (reg_overlap_mentioned_p (mul_result, mac_acc)
21661           && !reg_overlap_mentioned_p (mul_result, mac_op0)
21662           && !reg_overlap_mentioned_p (mul_result, mac_op1));
21663 }
21664
21665
21666 /* The EABI says test the least significant bit of a guard variable.  */
21667
21668 static bool
21669 arm_cxx_guard_mask_bit (void)
21670 {
21671   return TARGET_AAPCS_BASED;
21672 }
21673
21674
21675 /* The EABI specifies that all array cookies are 8 bytes long.  */
21676
21677 static tree
21678 arm_get_cookie_size (tree type)
21679 {
21680   tree size;
21681
21682   if (!TARGET_AAPCS_BASED)
21683     return default_cxx_get_cookie_size (type);
21684
21685   size = build_int_cst (sizetype, 8);
21686   return size;
21687 }
21688
21689
21690 /* The EABI says that array cookies should also contain the element size.  */
21691
21692 static bool
21693 arm_cookie_has_size (void)
21694 {
21695   return TARGET_AAPCS_BASED;
21696 }
21697
21698
21699 /* The EABI says constructors and destructors should return a pointer to
21700    the object constructed/destroyed.  */
21701
21702 static bool
21703 arm_cxx_cdtor_returns_this (void)
21704 {
21705   return TARGET_AAPCS_BASED;
21706 }
21707
21708 /* The EABI says that an inline function may never be the key
21709    method.  */
21710
21711 static bool
21712 arm_cxx_key_method_may_be_inline (void)
21713 {
21714   return !TARGET_AAPCS_BASED;
21715 }
21716
21717 static void
21718 arm_cxx_determine_class_data_visibility (tree decl)
21719 {
21720   if (!TARGET_AAPCS_BASED
21721       || !TARGET_DLLIMPORT_DECL_ATTRIBUTES)
21722     return;
21723
21724   /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
21725      is exported.  However, on systems without dynamic vague linkage,
21726      \S 3.2.5.6 says that COMDAT class data has hidden linkage.  */
21727   if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
21728     DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
21729   else
21730     DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
21731   DECL_VISIBILITY_SPECIFIED (decl) = 1;
21732 }
21733
21734 static bool
21735 arm_cxx_class_data_always_comdat (void)
21736 {
21737   /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
21738      vague linkage if the class has no key function.  */
21739   return !TARGET_AAPCS_BASED;
21740 }
21741
21742
21743 /* The EABI says __aeabi_atexit should be used to register static
21744    destructors.  */
21745
21746 static bool
21747 arm_cxx_use_aeabi_atexit (void)
21748 {
21749   return TARGET_AAPCS_BASED;
21750 }
21751
21752
21753 void
21754 arm_set_return_address (rtx source, rtx scratch)
21755 {
21756   arm_stack_offsets *offsets;
21757   HOST_WIDE_INT delta;
21758   rtx addr;
21759   unsigned long saved_regs;
21760
21761   offsets = arm_get_frame_offsets ();
21762   saved_regs = offsets->saved_regs_mask;
21763
21764   if ((saved_regs & (1 << LR_REGNUM)) == 0)
21765     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
21766   else
21767     {
21768       if (frame_pointer_needed)
21769         addr = plus_constant(hard_frame_pointer_rtx, -4);
21770       else
21771         {
21772           /* LR will be the first saved register.  */
21773           delta = offsets->outgoing_args - (offsets->frame + 4);
21774
21775
21776           if (delta >= 4096)
21777             {
21778               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
21779                                      GEN_INT (delta & ~4095)));
21780               addr = scratch;
21781               delta &= 4095;
21782             }
21783           else
21784             addr = stack_pointer_rtx;
21785
21786           addr = plus_constant (addr, delta);
21787         }
21788       emit_move_insn (gen_frame_mem (Pmode, addr), source);
21789     }
21790 }
21791
21792
21793 void
21794 thumb_set_return_address (rtx source, rtx scratch)
21795 {
21796   arm_stack_offsets *offsets;
21797   HOST_WIDE_INT delta;
21798   HOST_WIDE_INT limit;
21799   int reg;
21800   rtx addr;
21801   unsigned long mask;
21802
21803   emit_use (source);
21804
21805   offsets = arm_get_frame_offsets ();
21806   mask = offsets->saved_regs_mask;
21807   if (mask & (1 << LR_REGNUM))
21808     {
21809       limit = 1024;
21810       /* Find the saved regs.  */
21811       if (frame_pointer_needed)
21812         {
21813           delta = offsets->soft_frame - offsets->saved_args;
21814           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
21815           if (TARGET_THUMB1)
21816             limit = 128;
21817         }
21818       else
21819         {
21820           delta = offsets->outgoing_args - offsets->saved_args;
21821           reg = SP_REGNUM;
21822         }
21823       /* Allow for the stack frame.  */
21824       if (TARGET_THUMB1 && TARGET_BACKTRACE)
21825         delta -= 16;
21826       /* The link register is always the first saved register.  */
21827       delta -= 4;
21828
21829       /* Construct the address.  */
21830       addr = gen_rtx_REG (SImode, reg);
21831       if (delta > limit)
21832         {
21833           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
21834           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
21835           addr = scratch;
21836         }
21837       else
21838         addr = plus_constant (addr, delta);
21839
21840       emit_move_insn (gen_frame_mem (Pmode, addr), source);
21841     }
21842   else
21843     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
21844 }
21845
21846 /* Implements target hook vector_mode_supported_p.  */
21847 bool
21848 arm_vector_mode_supported_p (enum machine_mode mode)
21849 {
21850   /* Neon also supports V2SImode, etc. listed in the clause below.  */
21851   if (TARGET_NEON && (mode == V2SFmode || mode == V4SImode || mode == V8HImode
21852       || mode == V16QImode || mode == V4SFmode || mode == V2DImode))
21853     return true;
21854
21855   if ((TARGET_NEON || TARGET_IWMMXT)
21856       && ((mode == V2SImode)
21857           || (mode == V4HImode)
21858           || (mode == V8QImode)))
21859     return true;
21860
21861   return false;
21862 }
21863
21864 /* Implements target hook small_register_classes_for_mode_p.  */
21865 bool
21866 arm_small_register_classes_for_mode_p (enum machine_mode mode ATTRIBUTE_UNUSED)
21867 {
21868   return TARGET_THUMB1;
21869 }
21870
21871 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
21872    ARM insns and therefore guarantee that the shift count is modulo 256.
21873    DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
21874    guarantee no particular behavior for out-of-range counts.  */
21875
21876 static unsigned HOST_WIDE_INT
21877 arm_shift_truncation_mask (enum machine_mode mode)
21878 {
21879   return mode == SImode ? 255 : 0;
21880 }
21881
21882
21883 /* Map internal gcc register numbers to DWARF2 register numbers.  */
21884
21885 unsigned int
21886 arm_dbx_register_number (unsigned int regno)
21887 {
21888   if (regno < 16)
21889     return regno;
21890
21891   /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
21892      compatibility.  The EABI defines them as registers 96-103.  */
21893   if (IS_FPA_REGNUM (regno))
21894     return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
21895
21896   if (IS_VFP_REGNUM (regno))
21897     {
21898       /* See comment in arm_dwarf_register_span.  */
21899       if (VFP_REGNO_OK_FOR_SINGLE (regno))
21900         return 64 + regno - FIRST_VFP_REGNUM;
21901       else
21902         return 256 + (regno - FIRST_VFP_REGNUM) / 2;
21903     }
21904
21905   if (IS_IWMMXT_GR_REGNUM (regno))
21906     return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
21907
21908   if (IS_IWMMXT_REGNUM (regno))
21909     return 112 + regno - FIRST_IWMMXT_REGNUM;
21910
21911   gcc_unreachable ();
21912 }
21913
21914 /* Dwarf models VFPv3 registers as 32 64-bit registers.
21915    GCC models tham as 64 32-bit registers, so we need to describe this to
21916    the DWARF generation code.  Other registers can use the default.  */
21917 static rtx
21918 arm_dwarf_register_span (rtx rtl)
21919 {
21920   unsigned regno;
21921   int nregs;
21922   int i;
21923   rtx p;
21924
21925   regno = REGNO (rtl);
21926   if (!IS_VFP_REGNUM (regno))
21927     return NULL_RTX;
21928
21929   /* XXX FIXME: The EABI defines two VFP register ranges:
21930         64-95: Legacy VFPv2 numbering for S0-S31 (obsolescent)
21931         256-287: D0-D31
21932      The recommended encoding for S0-S31 is a DW_OP_bit_piece of the
21933      corresponding D register.  Until GDB supports this, we shall use the
21934      legacy encodings.  We also use these encodings for D0-D15 for
21935      compatibility with older debuggers.  */
21936   if (VFP_REGNO_OK_FOR_SINGLE (regno))
21937     return NULL_RTX;
21938
21939   nregs = GET_MODE_SIZE (GET_MODE (rtl)) / 8;
21940   p = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nregs));
21941   regno = (regno - FIRST_VFP_REGNUM) / 2;
21942   for (i = 0; i < nregs; i++)
21943     XVECEXP (p, 0, i) = gen_rtx_REG (DImode, 256 + regno + i);
21944
21945   return p;
21946 }
21947
21948 #ifdef TARGET_UNWIND_INFO
21949 /* Emit unwind directives for a store-multiple instruction or stack pointer
21950    push during alignment.
21951    These should only ever be generated by the function prologue code, so
21952    expect them to have a particular form.  */
21953
21954 static void
21955 arm_unwind_emit_sequence (FILE * asm_out_file, rtx p)
21956 {
21957   int i;
21958   HOST_WIDE_INT offset;
21959   HOST_WIDE_INT nregs;
21960   int reg_size;
21961   unsigned reg;
21962   unsigned lastreg;
21963   rtx e;
21964
21965   e = XVECEXP (p, 0, 0);
21966   if (GET_CODE (e) != SET)
21967     abort ();
21968
21969   /* First insn will adjust the stack pointer.  */
21970   if (GET_CODE (e) != SET
21971       || GET_CODE (XEXP (e, 0)) != REG
21972       || REGNO (XEXP (e, 0)) != SP_REGNUM
21973       || GET_CODE (XEXP (e, 1)) != PLUS)
21974     abort ();
21975
21976   offset = -INTVAL (XEXP (XEXP (e, 1), 1));
21977   nregs = XVECLEN (p, 0) - 1;
21978
21979   reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
21980   if (reg < 16)
21981     {
21982       /* The function prologue may also push pc, but not annotate it as it is
21983          never restored.  We turn this into a stack pointer adjustment.  */
21984       if (nregs * 4 == offset - 4)
21985         {
21986           fprintf (asm_out_file, "\t.pad #4\n");
21987           offset -= 4;
21988         }
21989       reg_size = 4;
21990       fprintf (asm_out_file, "\t.save {");
21991     }
21992   else if (IS_VFP_REGNUM (reg))
21993     {
21994       reg_size = 8;
21995       fprintf (asm_out_file, "\t.vsave {");
21996     }
21997   else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
21998     {
21999       /* FPA registers are done differently.  */
22000       asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
22001       return;
22002     }
22003   else
22004     /* Unknown register type.  */
22005     abort ();
22006
22007   /* If the stack increment doesn't match the size of the saved registers,
22008      something has gone horribly wrong.  */
22009   if (offset != nregs * reg_size)
22010     abort ();
22011
22012   offset = 0;
22013   lastreg = 0;
22014   /* The remaining insns will describe the stores.  */
22015   for (i = 1; i <= nregs; i++)
22016     {
22017       /* Expect (set (mem <addr>) (reg)).
22018          Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)).  */
22019       e = XVECEXP (p, 0, i);
22020       if (GET_CODE (e) != SET
22021           || GET_CODE (XEXP (e, 0)) != MEM
22022           || GET_CODE (XEXP (e, 1)) != REG)
22023         abort ();
22024
22025       reg = REGNO (XEXP (e, 1));
22026       if (reg < lastreg)
22027         abort ();
22028
22029       if (i != 1)
22030         fprintf (asm_out_file, ", ");
22031       /* We can't use %r for vfp because we need to use the
22032          double precision register names.  */
22033       if (IS_VFP_REGNUM (reg))
22034         asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
22035       else
22036         asm_fprintf (asm_out_file, "%r", reg);
22037
22038 #ifdef ENABLE_CHECKING
22039       /* Check that the addresses are consecutive.  */
22040       e = XEXP (XEXP (e, 0), 0);
22041       if (GET_CODE (e) == PLUS)
22042         {
22043           offset += reg_size;
22044           if (GET_CODE (XEXP (e, 0)) != REG
22045               || REGNO (XEXP (e, 0)) != SP_REGNUM
22046               || GET_CODE (XEXP (e, 1)) != CONST_INT
22047               || offset != INTVAL (XEXP (e, 1)))
22048             abort ();
22049         }
22050       else if (i != 1
22051                || GET_CODE (e) != REG
22052                || REGNO (e) != SP_REGNUM)
22053         abort ();
22054 #endif
22055     }
22056   fprintf (asm_out_file, "}\n");
22057 }
22058
22059 /*  Emit unwind directives for a SET.  */
22060
22061 static void
22062 arm_unwind_emit_set (FILE * asm_out_file, rtx p)
22063 {
22064   rtx e0;
22065   rtx e1;
22066   unsigned reg;
22067
22068   e0 = XEXP (p, 0);
22069   e1 = XEXP (p, 1);
22070   switch (GET_CODE (e0))
22071     {
22072     case MEM:
22073       /* Pushing a single register.  */
22074       if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
22075           || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
22076           || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
22077         abort ();
22078
22079       asm_fprintf (asm_out_file, "\t.save ");
22080       if (IS_VFP_REGNUM (REGNO (e1)))
22081         asm_fprintf(asm_out_file, "{d%d}\n",
22082                     (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
22083       else
22084         asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
22085       break;
22086
22087     case REG:
22088       if (REGNO (e0) == SP_REGNUM)
22089         {
22090           /* A stack increment.  */
22091           if (GET_CODE (e1) != PLUS
22092               || GET_CODE (XEXP (e1, 0)) != REG
22093               || REGNO (XEXP (e1, 0)) != SP_REGNUM
22094               || GET_CODE (XEXP (e1, 1)) != CONST_INT)
22095             abort ();
22096
22097           asm_fprintf (asm_out_file, "\t.pad #%wd\n",
22098                        -INTVAL (XEXP (e1, 1)));
22099         }
22100       else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
22101         {
22102           HOST_WIDE_INT offset;
22103
22104           if (GET_CODE (e1) == PLUS)
22105             {
22106               if (GET_CODE (XEXP (e1, 0)) != REG
22107                   || GET_CODE (XEXP (e1, 1)) != CONST_INT)
22108                 abort ();
22109               reg = REGNO (XEXP (e1, 0));
22110               offset = INTVAL (XEXP (e1, 1));
22111               asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
22112                            HARD_FRAME_POINTER_REGNUM, reg,
22113                            offset);
22114             }
22115           else if (GET_CODE (e1) == REG)
22116             {
22117               reg = REGNO (e1);
22118               asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
22119                            HARD_FRAME_POINTER_REGNUM, reg);
22120             }
22121           else
22122             abort ();
22123         }
22124       else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
22125         {
22126           /* Move from sp to reg.  */
22127           asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
22128         }
22129      else if (GET_CODE (e1) == PLUS
22130               && GET_CODE (XEXP (e1, 0)) == REG
22131               && REGNO (XEXP (e1, 0)) == SP_REGNUM
22132               && GET_CODE (XEXP (e1, 1)) == CONST_INT)
22133         {
22134           /* Set reg to offset from sp.  */
22135           asm_fprintf (asm_out_file, "\t.movsp %r, #%d\n",
22136                        REGNO (e0), (int)INTVAL(XEXP (e1, 1)));
22137         }
22138       else if (GET_CODE (e1) == UNSPEC && XINT (e1, 1) == UNSPEC_STACK_ALIGN)
22139         {
22140           /* Stack pointer save before alignment.  */
22141           reg = REGNO (e0);
22142           asm_fprintf (asm_out_file, "\t.unwind_raw 0, 0x%x @ vsp = r%d\n",
22143                        reg + 0x90, reg);
22144         }
22145       else
22146         abort ();
22147       break;
22148
22149     default:
22150       abort ();
22151     }
22152 }
22153
22154
22155 /* Emit unwind directives for the given insn.  */
22156
22157 static void
22158 arm_unwind_emit (FILE * asm_out_file, rtx insn)
22159 {
22160   rtx pat;
22161
22162   if (!ARM_EABI_UNWIND_TABLES)
22163     return;
22164
22165   if (!(flag_unwind_tables || crtl->uses_eh_lsda)
22166       && (TREE_NOTHROW (current_function_decl)
22167           || crtl->all_throwers_are_sibcalls))
22168     return;
22169
22170   if (GET_CODE (insn) == NOTE || !RTX_FRAME_RELATED_P (insn))
22171     return;
22172
22173   pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
22174   if (pat)
22175     pat = XEXP (pat, 0);
22176   else
22177     pat = PATTERN (insn);
22178
22179   switch (GET_CODE (pat))
22180     {
22181     case SET:
22182       arm_unwind_emit_set (asm_out_file, pat);
22183       break;
22184
22185     case SEQUENCE:
22186       /* Store multiple.  */
22187       arm_unwind_emit_sequence (asm_out_file, pat);
22188       break;
22189
22190     default:
22191       abort();
22192     }
22193 }
22194
22195
22196 /* Output a reference from a function exception table to the type_info
22197    object X.  The EABI specifies that the symbol should be relocated by
22198    an R_ARM_TARGET2 relocation.  */
22199
22200 static bool
22201 arm_output_ttype (rtx x)
22202 {
22203   fputs ("\t.word\t", asm_out_file);
22204   output_addr_const (asm_out_file, x);
22205   /* Use special relocations for symbol references.  */
22206   if (GET_CODE (x) != CONST_INT)
22207     fputs ("(TARGET2)", asm_out_file);
22208   fputc ('\n', asm_out_file);
22209
22210   return TRUE;
22211 }
22212 #endif /* TARGET_UNWIND_INFO */
22213
22214
22215 /* Handle UNSPEC DWARF call frame instructions.  These are needed for dynamic
22216    stack alignment.  */
22217
22218 static void
22219 arm_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
22220 {
22221   rtx unspec = SET_SRC (pattern);
22222   gcc_assert (GET_CODE (unspec) == UNSPEC);
22223
22224   switch (index)
22225     {
22226     case UNSPEC_STACK_ALIGN:
22227       /* ??? We should set the CFA = (SP & ~7).  At this point we haven't
22228          put anything on the stack, so hopefully it won't matter.
22229          CFA = SP will be correct after alignment.  */
22230       dwarf2out_reg_save_reg (label, stack_pointer_rtx,
22231                               SET_DEST (pattern));
22232       break;
22233     default:
22234       gcc_unreachable ();
22235     }
22236 }
22237
22238
22239 /* Output unwind directives for the start/end of a function.  */
22240
22241 void
22242 arm_output_fn_unwind (FILE * f, bool prologue)
22243 {
22244   if (!ARM_EABI_UNWIND_TABLES)
22245     return;
22246
22247   if (prologue)
22248     fputs ("\t.fnstart\n", f);
22249   else
22250     {
22251       /* If this function will never be unwound, then mark it as such.
22252          The came condition is used in arm_unwind_emit to suppress
22253          the frame annotations.  */
22254       if (!(flag_unwind_tables || crtl->uses_eh_lsda)
22255           && (TREE_NOTHROW (current_function_decl)
22256               || crtl->all_throwers_are_sibcalls))
22257         fputs("\t.cantunwind\n", f);
22258
22259       fputs ("\t.fnend\n", f);
22260     }
22261 }
22262
22263 static bool
22264 arm_emit_tls_decoration (FILE *fp, rtx x)
22265 {
22266   enum tls_reloc reloc;
22267   rtx val;
22268
22269   val = XVECEXP (x, 0, 0);
22270   reloc = (enum tls_reloc) INTVAL (XVECEXP (x, 0, 1));
22271
22272   output_addr_const (fp, val);
22273
22274   switch (reloc)
22275     {
22276     case TLS_GD32:
22277       fputs ("(tlsgd)", fp);
22278       break;
22279     case TLS_LDM32:
22280       fputs ("(tlsldm)", fp);
22281       break;
22282     case TLS_LDO32:
22283       fputs ("(tlsldo)", fp);
22284       break;
22285     case TLS_IE32:
22286       fputs ("(gottpoff)", fp);
22287       break;
22288     case TLS_LE32:
22289       fputs ("(tpoff)", fp);
22290       break;
22291     default:
22292       gcc_unreachable ();
22293     }
22294
22295   switch (reloc)
22296     {
22297     case TLS_GD32:
22298     case TLS_LDM32:
22299     case TLS_IE32:
22300       fputs (" + (. - ", fp);
22301       output_addr_const (fp, XVECEXP (x, 0, 2));
22302       fputs (" - ", fp);
22303       output_addr_const (fp, XVECEXP (x, 0, 3));
22304       fputc (')', fp);
22305       break;
22306     default:
22307       break;
22308     }
22309
22310   return TRUE;
22311 }
22312
22313 /* ARM implementation of TARGET_ASM_OUTPUT_DWARF_DTPREL.  */
22314
22315 static void
22316 arm_output_dwarf_dtprel (FILE *file, int size, rtx x)
22317 {
22318   gcc_assert (size == 4);
22319   fputs ("\t.word\t", file);
22320   output_addr_const (file, x);
22321   fputs ("(tlsldo)", file);
22322 }
22323
22324 bool
22325 arm_output_addr_const_extra (FILE *fp, rtx x)
22326 {
22327   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
22328     return arm_emit_tls_decoration (fp, x);
22329   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
22330     {
22331       char label[256];
22332       int labelno = INTVAL (XVECEXP (x, 0, 0));
22333
22334       ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
22335       assemble_name_raw (fp, label);
22336
22337       return TRUE;
22338     }
22339   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_GOTSYM_OFF)
22340     {
22341       assemble_name (fp, "_GLOBAL_OFFSET_TABLE_");
22342       if (GOT_PCREL)
22343         fputs ("+.", fp);
22344       fputs ("-(", fp);
22345       output_addr_const (fp, XVECEXP (x, 0, 0));
22346       fputc (')', fp);
22347       return TRUE;
22348     }
22349   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_SYMBOL_OFFSET)
22350     {
22351       output_addr_const (fp, XVECEXP (x, 0, 0));
22352       if (GOT_PCREL)
22353         fputs ("+.", fp);
22354       fputs ("-(", fp);
22355       output_addr_const (fp, XVECEXP (x, 0, 1));
22356       fputc (')', fp);
22357       return TRUE;
22358     }
22359   else if (GET_CODE (x) == CONST_VECTOR)
22360     return arm_emit_vector_const (fp, x);
22361
22362   return FALSE;
22363 }
22364
22365 /* Output assembly for a shift instruction.
22366    SET_FLAGS determines how the instruction modifies the condition codes.
22367    0 - Do not set condition codes.
22368    1 - Set condition codes.
22369    2 - Use smallest instruction.  */
22370 const char *
22371 arm_output_shift(rtx * operands, int set_flags)
22372 {
22373   char pattern[100];
22374   static const char flag_chars[3] = {'?', '.', '!'};
22375   const char *shift;
22376   HOST_WIDE_INT val;
22377   char c;
22378   
22379   c = flag_chars[set_flags];
22380   if (TARGET_UNIFIED_ASM)
22381     {
22382       shift = shift_op(operands[3], &val);
22383       if (shift)
22384         {
22385           if (val != -1)
22386             operands[2] = GEN_INT(val);
22387           sprintf (pattern, "%s%%%c\t%%0, %%1, %%2", shift, c);
22388         }
22389       else
22390         sprintf (pattern, "mov%%%c\t%%0, %%1", c);
22391     }
22392   else
22393     sprintf (pattern, "mov%%%c\t%%0, %%1%%S3", c);
22394   output_asm_insn (pattern, operands);
22395   return "";
22396 }
22397
22398 /* Output a Thumb-1 casesi dispatch sequence.  */
22399 const char *
22400 thumb1_output_casesi (rtx *operands)
22401 {
22402   rtx diff_vec = PATTERN (next_real_insn (operands[0]));
22403
22404   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
22405
22406   switch (GET_MODE(diff_vec))
22407     {
22408     case QImode:
22409       return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned ? 
22410               "bl\t%___gnu_thumb1_case_uqi" : "bl\t%___gnu_thumb1_case_sqi");
22411     case HImode:
22412       return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned ? 
22413               "bl\t%___gnu_thumb1_case_uhi" : "bl\t%___gnu_thumb1_case_shi");
22414     case SImode:
22415       return "bl\t%___gnu_thumb1_case_si";
22416     default:
22417       gcc_unreachable ();
22418     }
22419 }
22420
22421 /* Output a Thumb-2 casesi instruction.  */
22422 const char *
22423 thumb2_output_casesi (rtx *operands)
22424 {
22425   rtx diff_vec = PATTERN (next_real_insn (operands[2]));
22426
22427   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
22428
22429   output_asm_insn ("cmp\t%0, %1", operands);
22430   output_asm_insn ("bhi\t%l3", operands);
22431   switch (GET_MODE(diff_vec))
22432     {
22433     case QImode:
22434       return "tbb\t[%|pc, %0]";
22435     case HImode:
22436       return "tbh\t[%|pc, %0, lsl #1]";
22437     case SImode:
22438       if (flag_pic)
22439         {
22440           output_asm_insn ("adr\t%4, %l2", operands);
22441           output_asm_insn ("ldr\t%5, [%4, %0, lsl #2]", operands);
22442           output_asm_insn ("add\t%4, %4, %5", operands);
22443           return "bx\t%4";
22444         }
22445       else
22446         {
22447           output_asm_insn ("adr\t%4, %l2", operands);
22448           return "ldr\t%|pc, [%4, %0, lsl #2]";
22449         }
22450     default:
22451       gcc_unreachable ();
22452     }
22453 }
22454
22455 /* Most ARM cores are single issue, but some newer ones can dual issue.
22456    The scheduler descriptions rely on this being correct.  */
22457 static int
22458 arm_issue_rate (void)
22459 {
22460   switch (arm_tune)
22461     {
22462     case cortexr4:
22463     case cortexr4f:
22464     case cortexa5:
22465     case cortexa8:
22466     case cortexa9:
22467       return 2;
22468
22469     default:
22470       return 1;
22471     }
22472 }
22473
22474 /* A table and a function to perform ARM-specific name mangling for
22475    NEON vector types in order to conform to the AAPCS (see "Procedure
22476    Call Standard for the ARM Architecture", Appendix A).  To qualify
22477    for emission with the mangled names defined in that document, a
22478    vector type must not only be of the correct mode but also be
22479    composed of NEON vector element types (e.g. __builtin_neon_qi).  */
22480 typedef struct
22481 {
22482   enum machine_mode mode;
22483   const char *element_type_name;
22484   const char *aapcs_name;
22485 } arm_mangle_map_entry;
22486
22487 static arm_mangle_map_entry arm_mangle_map[] = {
22488   /* 64-bit containerized types.  */
22489   { V8QImode,  "__builtin_neon_qi",     "15__simd64_int8_t" },
22490   { V8QImode,  "__builtin_neon_uqi",    "16__simd64_uint8_t" },
22491   { V4HImode,  "__builtin_neon_hi",     "16__simd64_int16_t" },
22492   { V4HImode,  "__builtin_neon_uhi",    "17__simd64_uint16_t" },
22493   { V2SImode,  "__builtin_neon_si",     "16__simd64_int32_t" },
22494   { V2SImode,  "__builtin_neon_usi",    "17__simd64_uint32_t" },
22495   { V2SFmode,  "__builtin_neon_sf",     "18__simd64_float32_t" },
22496   { V8QImode,  "__builtin_neon_poly8",  "16__simd64_poly8_t" },
22497   { V4HImode,  "__builtin_neon_poly16", "17__simd64_poly16_t" },
22498   /* 128-bit containerized types.  */
22499   { V16QImode, "__builtin_neon_qi",     "16__simd128_int8_t" },
22500   { V16QImode, "__builtin_neon_uqi",    "17__simd128_uint8_t" },
22501   { V8HImode,  "__builtin_neon_hi",     "17__simd128_int16_t" },
22502   { V8HImode,  "__builtin_neon_uhi",    "18__simd128_uint16_t" },
22503   { V4SImode,  "__builtin_neon_si",     "17__simd128_int32_t" },
22504   { V4SImode,  "__builtin_neon_usi",    "18__simd128_uint32_t" },
22505   { V4SFmode,  "__builtin_neon_sf",     "19__simd128_float32_t" },
22506   { V16QImode, "__builtin_neon_poly8",  "17__simd128_poly8_t" },
22507   { V8HImode,  "__builtin_neon_poly16", "18__simd128_poly16_t" },
22508   { VOIDmode, NULL, NULL }
22509 };
22510
22511 const char *
22512 arm_mangle_type (const_tree type)
22513 {
22514   arm_mangle_map_entry *pos = arm_mangle_map;
22515
22516   /* The ARM ABI documents (10th October 2008) say that "__va_list"
22517      has to be managled as if it is in the "std" namespace.  */
22518   if (TARGET_AAPCS_BASED 
22519       && lang_hooks.types_compatible_p (CONST_CAST_TREE (type), va_list_type))
22520     {
22521       static bool warned;
22522       if (!warned && warn_psabi && !in_system_header)
22523         {
22524           warned = true;
22525           inform (input_location,
22526                   "the mangling of %<va_list%> has changed in GCC 4.4");
22527         }
22528       return "St9__va_list";
22529     }
22530
22531   /* Half-precision float.  */
22532   if (TREE_CODE (type) == REAL_TYPE && TYPE_PRECISION (type) == 16)
22533     return "Dh";
22534
22535   if (TREE_CODE (type) != VECTOR_TYPE)
22536     return NULL;
22537
22538   /* Check the mode of the vector type, and the name of the vector
22539      element type, against the table.  */
22540   while (pos->mode != VOIDmode)
22541     {
22542       tree elt_type = TREE_TYPE (type);
22543
22544       if (pos->mode == TYPE_MODE (type)
22545           && TREE_CODE (TYPE_NAME (elt_type)) == TYPE_DECL
22546           && !strcmp (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (elt_type))),
22547                       pos->element_type_name))
22548         return pos->aapcs_name;
22549
22550       pos++;
22551     }
22552
22553   /* Use the default mangling for unrecognized (possibly user-defined)
22554      vector types.  */
22555   return NULL;
22556 }
22557
22558 /* Order of allocation of core registers for Thumb: this allocation is
22559    written over the corresponding initial entries of the array
22560    initialized with REG_ALLOC_ORDER.  We allocate all low registers
22561    first.  Saving and restoring a low register is usually cheaper than
22562    using a call-clobbered high register.  */
22563
22564 static const int thumb_core_reg_alloc_order[] =
22565 {
22566    3,  2,  1,  0,  4,  5,  6,  7,
22567   14, 12,  8,  9, 10, 11, 13, 15
22568 };
22569
22570 /* Adjust register allocation order when compiling for Thumb.  */
22571
22572 void
22573 arm_order_regs_for_local_alloc (void)
22574 {
22575   const int arm_reg_alloc_order[] = REG_ALLOC_ORDER;
22576   memcpy(reg_alloc_order, arm_reg_alloc_order, sizeof (reg_alloc_order));
22577   if (TARGET_THUMB)
22578     memcpy (reg_alloc_order, thumb_core_reg_alloc_order,
22579             sizeof (thumb_core_reg_alloc_order));
22580 }
22581
22582 /* Set default optimization options.  */
22583 void
22584 arm_optimization_options (int level, int size ATTRIBUTE_UNUSED)
22585 {
22586   /* Enable section anchors by default at -O1 or higher.
22587      Use 2 to distinguish from an explicit -fsection-anchors
22588      given on the command line.  */
22589   if (level > 0)
22590     flag_section_anchors = 2;
22591 }
22592
22593 /* Implement TARGET_FRAME_POINTER_REQUIRED.  */
22594
22595 bool
22596 arm_frame_pointer_required (void)
22597 {
22598   return (cfun->has_nonlocal_label
22599           || SUBTARGET_FRAME_POINTER_REQUIRED
22600           || (TARGET_ARM && TARGET_APCS_FRAME && ! leaf_function_p ()));
22601 }
22602
22603 /* Only thumb1 can't support conditional execution, so return true if
22604    the target is not thumb1.  */
22605 static bool
22606 arm_have_conditional_execution (void)
22607 {
22608   return !TARGET_THUMB1;
22609 }
22610
22611 /* Legitimize a memory reference for sync primitive implemented using
22612    ldrex / strex.  We currently force the form of the reference to be
22613    indirect without offset.  We do not yet support the indirect offset
22614    addressing supported by some ARM targets for these
22615    instructions.  */
22616 static rtx
22617 arm_legitimize_sync_memory (rtx memory)
22618 {
22619   rtx addr = force_reg (Pmode, XEXP (memory, 0));
22620   rtx legitimate_memory = gen_rtx_MEM (GET_MODE (memory), addr);
22621
22622   set_mem_alias_set (legitimate_memory, ALIAS_SET_MEMORY_BARRIER);
22623   MEM_VOLATILE_P (legitimate_memory) = MEM_VOLATILE_P (memory);
22624   return legitimate_memory;
22625 }
22626
22627 /* An instruction emitter. */
22628 typedef void (* emit_f) (int label, const char *, rtx *);
22629
22630 /* An instruction emitter that emits via the conventional
22631    output_asm_insn.  */
22632 static void
22633 arm_emit (int label ATTRIBUTE_UNUSED, const char *pattern, rtx *operands)
22634 {
22635   output_asm_insn (pattern, operands);
22636 }
22637
22638 /* Count the number of emitted synchronization instructions.  */
22639 static unsigned arm_insn_count;
22640
22641 /* An emitter that counts emitted instructions but does not actually
22642    emit instruction into the the instruction stream.  */
22643 static void
22644 arm_count (int label,
22645            const char *pattern ATTRIBUTE_UNUSED,
22646            rtx *operands ATTRIBUTE_UNUSED)
22647 {
22648   if (! label)
22649     ++ arm_insn_count;
22650 }
22651
22652 /* Construct a pattern using conventional output formatting and feed
22653    it to output_asm_insn.  Provides a mechanism to construct the
22654    output pattern on the fly.  Note the hard limit on the pattern
22655    buffer size.  */
22656 static void
22657 arm_output_asm_insn (emit_f emit, int label, rtx *operands,
22658                      const char *pattern, ...)
22659 {
22660   va_list ap;
22661   char buffer[256];
22662
22663   va_start (ap, pattern);
22664   vsprintf (buffer, pattern, ap);
22665   va_end (ap);
22666   emit (label, buffer, operands);
22667 }
22668
22669 /* Emit the memory barrier instruction, if any, provided by this
22670    target to a specified emitter.  */
22671 static void
22672 arm_process_output_memory_barrier (emit_f emit, rtx *operands)
22673 {
22674   if (TARGET_HAVE_DMB)
22675     {
22676       /* Note we issue a system level barrier. We should consider
22677          issuing a inner shareabilty zone barrier here instead, ie.
22678          "DMB ISH".  */
22679       emit (0, "dmb\tsy", operands);
22680       return;
22681     }
22682
22683   if (TARGET_HAVE_DMB_MCR)
22684     {
22685       emit (0, "mcr\tp15, 0, r0, c7, c10, 5", operands);
22686       return;
22687     }
22688
22689   gcc_unreachable ();
22690 }
22691
22692 /* Emit the memory barrier instruction, if any, provided by this
22693    target.  */
22694 const char *
22695 arm_output_memory_barrier (rtx *operands)
22696 {
22697   arm_process_output_memory_barrier (arm_emit, operands);
22698   return "";
22699 }
22700
22701 /* Helper to figure out the instruction suffix required on ldrex/strex
22702    for operations on an object of the specified mode.  */
22703 static const char *
22704 arm_ldrex_suffix (enum machine_mode mode)
22705 {
22706   switch (mode)
22707     {
22708     case QImode: return "b";
22709     case HImode: return "h";
22710     case SImode: return "";
22711     case DImode: return "d";
22712     default:
22713       gcc_unreachable ();
22714     }
22715   return "";
22716 }
22717
22718 /* Emit an ldrex{b,h,d, } instruction appropriate for the specified
22719    mode.  */
22720 static void
22721 arm_output_ldrex (emit_f emit,
22722                   enum machine_mode mode,
22723                   rtx target,
22724                   rtx memory)
22725 {
22726   const char *suffix = arm_ldrex_suffix (mode);
22727   rtx operands[2];
22728
22729   operands[0] = target;
22730   operands[1] = memory;
22731   arm_output_asm_insn (emit, 0, operands, "ldrex%s\t%%0, %%C1", suffix);
22732 }
22733
22734 /* Emit a strex{b,h,d, } instruction appropriate for the specified
22735    mode.  */
22736 static void
22737 arm_output_strex (emit_f emit,
22738                   enum machine_mode mode,
22739                   const char *cc,
22740                   rtx result,
22741                   rtx value,
22742                   rtx memory)
22743 {
22744   const char *suffix = arm_ldrex_suffix (mode);
22745   rtx operands[3];
22746
22747   operands[0] = result;
22748   operands[1] = value;
22749   operands[2] = memory;
22750   arm_output_asm_insn (emit, 0, operands, "strex%s%s\t%%0, %%1, %%C2", suffix,
22751                        cc);
22752 }
22753
22754 /* Helper to emit a two operand instruction.  */
22755 static void
22756 arm_output_op2 (emit_f emit, const char *mnemonic, rtx d, rtx s)
22757 {
22758   rtx operands[2];
22759
22760   operands[0] = d;
22761   operands[1] = s;
22762   arm_output_asm_insn (emit, 0, operands, "%s\t%%0, %%1", mnemonic);
22763 }
22764
22765 /* Helper to emit a three operand instruction.  */
22766 static void
22767 arm_output_op3 (emit_f emit, const char *mnemonic, rtx d, rtx a, rtx b)
22768 {
22769   rtx operands[3];
22770
22771   operands[0] = d;
22772   operands[1] = a;
22773   operands[2] = b;
22774   arm_output_asm_insn (emit, 0, operands, "%s\t%%0, %%1, %%2", mnemonic);
22775 }
22776
22777 /* Emit a load store exclusive synchronization loop.
22778
22779    do
22780      old_value = [mem]
22781      if old_value != required_value
22782        break;
22783      t1 = sync_op (old_value, new_value)
22784      [mem] = t1, t2 = [0|1]
22785    while ! t2
22786
22787    Note:
22788      t1 == t2 is not permitted
22789      t1 == old_value is permitted
22790
22791    required_value:
22792
22793    RTX register or const_int representing the required old_value for
22794    the modify to continue, if NULL no comparsion is performed.  */
22795 static void
22796 arm_output_sync_loop (emit_f emit,
22797                       enum machine_mode mode,
22798                       rtx old_value,
22799                       rtx memory,
22800                       rtx required_value,
22801                       rtx new_value,
22802                       rtx t1,
22803                       rtx t2,
22804                       enum attr_sync_op sync_op,
22805                       int early_barrier_required)
22806 {
22807   rtx operands[1];
22808
22809   gcc_assert (t1 != t2);
22810
22811   if (early_barrier_required)
22812     arm_process_output_memory_barrier (emit, NULL);
22813
22814   arm_output_asm_insn (emit, 1, operands, "%sLSYT%%=:", LOCAL_LABEL_PREFIX);
22815
22816   arm_output_ldrex (emit, mode, old_value, memory);
22817
22818   if (required_value)
22819     {
22820       rtx operands[2];
22821
22822       operands[0] = old_value;
22823       operands[1] = required_value;
22824       arm_output_asm_insn (emit, 0, operands, "cmp\t%%0, %%1");
22825       arm_output_asm_insn (emit, 0, operands, "bne\t%sLSYB%%=", LOCAL_LABEL_PREFIX);
22826     }
22827
22828   switch (sync_op)
22829     {
22830     case SYNC_OP_ADD:
22831       arm_output_op3 (emit, "add", t1, old_value, new_value);
22832       break;
22833
22834     case SYNC_OP_SUB:
22835       arm_output_op3 (emit, "sub", t1, old_value, new_value);
22836       break;
22837
22838     case SYNC_OP_IOR:
22839       arm_output_op3 (emit, "orr", t1, old_value, new_value);
22840       break;
22841
22842     case SYNC_OP_XOR:
22843       arm_output_op3 (emit, "eor", t1, old_value, new_value);
22844       break;
22845
22846     case SYNC_OP_AND:
22847       arm_output_op3 (emit,"and", t1, old_value, new_value);
22848       break;
22849
22850     case SYNC_OP_NAND:
22851       arm_output_op3 (emit, "and", t1, old_value, new_value);
22852       arm_output_op2 (emit, "mvn", t1, t1);
22853       break;
22854
22855     case SYNC_OP_NONE:
22856       t1 = new_value;
22857       break;
22858     }
22859
22860   arm_output_strex (emit, mode, "", t2, t1, memory);
22861   operands[0] = t2;
22862   arm_output_asm_insn (emit, 0, operands, "teq\t%%0, #0");
22863   arm_output_asm_insn (emit, 0, operands, "bne\t%sLSYT%%=", LOCAL_LABEL_PREFIX);
22864
22865   arm_process_output_memory_barrier (emit, NULL);
22866   arm_output_asm_insn (emit, 1, operands, "%sLSYB%%=:", LOCAL_LABEL_PREFIX);
22867 }
22868
22869 static rtx
22870 arm_get_sync_operand (rtx *operands, int index, rtx default_value)
22871 {
22872   if (index > 0)
22873     default_value = operands[index - 1];
22874
22875   return default_value;
22876 }
22877
22878 #define FETCH_SYNC_OPERAND(NAME, DEFAULT) \
22879   arm_get_sync_operand (operands, (int) get_attr_sync_##NAME (insn), DEFAULT);
22880
22881 /* Extract the operands for a synchroniztion instruction from the
22882    instructions attributes and emit the instruction.  */
22883 static void
22884 arm_process_output_sync_insn (emit_f emit, rtx insn, rtx *operands)
22885 {
22886   rtx result, memory, required_value, new_value, t1, t2;
22887   int early_barrier;
22888   enum machine_mode mode;
22889   enum attr_sync_op sync_op;
22890
22891   result = FETCH_SYNC_OPERAND(result, 0);
22892   memory = FETCH_SYNC_OPERAND(memory, 0);
22893   required_value = FETCH_SYNC_OPERAND(required_value, 0);
22894   new_value = FETCH_SYNC_OPERAND(new_value, 0);
22895   t1 = FETCH_SYNC_OPERAND(t1, 0);
22896   t2 = FETCH_SYNC_OPERAND(t2, 0);
22897   early_barrier =
22898     get_attr_sync_release_barrier (insn) == SYNC_RELEASE_BARRIER_YES;
22899   sync_op = get_attr_sync_op (insn);
22900   mode = GET_MODE (memory);
22901
22902   arm_output_sync_loop (emit, mode, result, memory, required_value,
22903                         new_value, t1, t2, sync_op, early_barrier);
22904 }
22905
22906 /* Emit a synchronization instruction loop.  */
22907 const char *
22908 arm_output_sync_insn (rtx insn, rtx *operands)
22909 {
22910   arm_process_output_sync_insn (arm_emit, insn, operands);
22911   return "";
22912 }
22913
22914 /* Count the number of machine instruction that will be emitted for a
22915    synchronization instruction.  Note that the emitter used does not
22916    emit instructions, it just counts instructions being carefull not
22917    to count labels.  */
22918 unsigned int
22919 arm_sync_loop_insns (rtx insn, rtx *operands)
22920 {
22921   arm_insn_count = 0;
22922   arm_process_output_sync_insn (arm_count, insn, operands);
22923   return arm_insn_count;
22924 }
22925
22926 /* Helper to call a target sync instruction generator, dealing with
22927    the variation in operands required by the different generators.  */
22928 static rtx
22929 arm_call_generator (struct arm_sync_generator *generator, rtx old_value,
22930                     rtx memory, rtx required_value, rtx new_value)
22931 {
22932   switch (generator->op)
22933     {
22934     case arm_sync_generator_omn:
22935       gcc_assert (! required_value);
22936       return generator->u.omn (old_value, memory, new_value);
22937
22938     case arm_sync_generator_omrn:
22939       gcc_assert (required_value);
22940       return generator->u.omrn (old_value, memory, required_value, new_value);
22941     }
22942
22943   return NULL;
22944 }
22945
22946 /* Expand a synchronization loop. The synchronization loop is expanded
22947    as an opaque block of instructions in order to ensure that we do
22948    not subsequently get extraneous memory accesses inserted within the
22949    critical region. The exclusive access property of ldrex/strex is
22950    only guaranteed in there are no intervening memory accesses. */
22951 void
22952 arm_expand_sync (enum machine_mode mode,
22953                  struct arm_sync_generator *generator,
22954                  rtx target, rtx memory, rtx required_value, rtx new_value)
22955 {
22956   if (target == NULL)
22957     target = gen_reg_rtx (mode);
22958
22959   memory = arm_legitimize_sync_memory (memory);
22960   if (mode != SImode)
22961     {
22962       rtx load_temp = gen_reg_rtx (SImode);
22963
22964       if (required_value)
22965         required_value = convert_modes (SImode, mode, required_value, true);
22966
22967       new_value = convert_modes (SImode, mode, new_value, true);
22968       emit_insn (arm_call_generator (generator, load_temp, memory,
22969                                      required_value, new_value));
22970       emit_move_insn (target, gen_lowpart (mode, load_temp));
22971     }
22972   else
22973     {
22974       emit_insn (arm_call_generator (generator, target, memory, required_value,
22975                                      new_value));
22976     }
22977 }
22978
22979 #include "gt-arm.h"