OSDN Git Service

gcc/
[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, 2011
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 "recog.h"
45 #include "cgraph.h"
46 #include "ggc.h"
47 #include "except.h"
48 #include "c-family/c-pragma.h"  /* ??? */
49 #include "integrate.h"
50 #include "tm_p.h"
51 #include "target.h"
52 #include "target-def.h"
53 #include "debug.h"
54 #include "langhooks.h"
55 #include "df.h"
56 #include "intl.h"
57 #include "libfuncs.h"
58 #include "params.h"
59 #include "opts.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 struct four_ints
68 {
69   int i[4];
70 };
71
72 /* Forward function declarations.  */
73 static bool arm_needs_doubleword_align (enum machine_mode, const_tree);
74 static int arm_compute_static_chain_stack_bytes (void);
75 static arm_stack_offsets *arm_get_frame_offsets (void);
76 static void arm_add_gc_roots (void);
77 static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
78                              HOST_WIDE_INT, rtx, rtx, int, int);
79 static unsigned bit_count (unsigned long);
80 static int arm_address_register_rtx_p (rtx, int);
81 static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
82 static int thumb2_legitimate_index_p (enum machine_mode, rtx, int);
83 static int thumb1_base_register_rtx_p (rtx, enum machine_mode, int);
84 static rtx arm_legitimize_address (rtx, rtx, enum machine_mode);
85 static rtx thumb_legitimize_address (rtx, rtx, enum machine_mode);
86 inline static int thumb1_index_register_rtx_p (rtx, int);
87 static bool arm_legitimate_address_p (enum machine_mode, rtx, bool);
88 static int thumb_far_jump_used_p (void);
89 static bool thumb_force_lr_save (void);
90 static rtx emit_sfm (int, int);
91 static unsigned arm_size_return_regs (void);
92 static bool arm_assemble_integer (rtx, unsigned int, int);
93 static void arm_print_operand (FILE *, rtx, int);
94 static void arm_print_operand_address (FILE *, rtx);
95 static bool arm_print_operand_punct_valid_p (unsigned char code);
96 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
97 static arm_cc get_arm_condition_code (rtx);
98 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
99 static rtx is_jump_table (rtx);
100 static const char *output_multi_immediate (rtx *, const char *, const char *,
101                                            int, HOST_WIDE_INT);
102 static const char *shift_op (rtx, HOST_WIDE_INT *);
103 static struct machine_function *arm_init_machine_status (void);
104 static void thumb_exit (FILE *, int);
105 static rtx is_jump_table (rtx);
106 static HOST_WIDE_INT get_jump_table_size (rtx);
107 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
108 static Mnode *add_minipool_forward_ref (Mfix *);
109 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
110 static Mnode *add_minipool_backward_ref (Mfix *);
111 static void assign_minipool_offsets (Mfix *);
112 static void arm_print_value (FILE *, rtx);
113 static void dump_minipool (rtx);
114 static int arm_barrier_cost (rtx);
115 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
116 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
117 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
118                                rtx);
119 static void arm_reorg (void);
120 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
121 static unsigned long arm_compute_save_reg0_reg12_mask (void);
122 static unsigned long arm_compute_save_reg_mask (void);
123 static unsigned long arm_isr_value (tree);
124 static unsigned long arm_compute_func_type (void);
125 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
126 static tree arm_handle_pcs_attribute (tree *, tree, tree, int, bool *);
127 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
128 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
129 static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
130 #endif
131 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
132 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
133 static int arm_comp_type_attributes (const_tree, const_tree);
134 static void arm_set_default_type_attributes (tree);
135 static int arm_adjust_cost (rtx, rtx, rtx, int);
136 static int optimal_immediate_sequence (enum rtx_code code,
137                                        unsigned HOST_WIDE_INT val,
138                                        struct four_ints *return_sequence);
139 static int optimal_immediate_sequence_1 (enum rtx_code code,
140                                          unsigned HOST_WIDE_INT val,
141                                          struct four_ints *return_sequence,
142                                          int i);
143 static int arm_get_strip_length (int);
144 static bool arm_function_ok_for_sibcall (tree, tree);
145 static enum machine_mode arm_promote_function_mode (const_tree,
146                                                     enum machine_mode, int *,
147                                                     const_tree, int);
148 static bool arm_return_in_memory (const_tree, const_tree);
149 static rtx arm_function_value (const_tree, const_tree, bool);
150 static rtx arm_libcall_value (enum machine_mode, const_rtx);
151
152 static void arm_internal_label (FILE *, const char *, unsigned long);
153 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
154                                  tree);
155 static bool arm_have_conditional_execution (void);
156 static bool arm_cannot_force_const_mem (enum machine_mode, rtx);
157 static bool arm_legitimate_constant_p (enum machine_mode, rtx);
158 static bool arm_rtx_costs_1 (rtx, enum rtx_code, int*, bool);
159 static bool arm_size_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *);
160 static bool arm_slowmul_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
161 static bool arm_fastmul_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
162 static bool arm_xscale_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
163 static bool arm_9e_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
164 static bool arm_rtx_costs (rtx, int, int, int, int *, bool);
165 static int arm_address_cost (rtx, bool);
166 static bool arm_memory_load_p (rtx);
167 static bool arm_cirrus_insn_p (rtx);
168 static void cirrus_reorg (rtx);
169 static void arm_init_builtins (void);
170 static void arm_init_iwmmxt_builtins (void);
171 static rtx safe_vector_operand (rtx, enum machine_mode);
172 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
173 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
174 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
175 static tree arm_builtin_decl (unsigned, bool);
176 static void emit_constant_insn (rtx cond, rtx pattern);
177 static rtx emit_set_insn (rtx, rtx);
178 static int arm_arg_partial_bytes (cumulative_args_t, enum machine_mode,
179                                   tree, bool);
180 static rtx arm_function_arg (cumulative_args_t, enum machine_mode,
181                              const_tree, bool);
182 static void arm_function_arg_advance (cumulative_args_t, enum machine_mode,
183                                       const_tree, bool);
184 static unsigned int arm_function_arg_boundary (enum machine_mode, const_tree);
185 static rtx aapcs_allocate_return_reg (enum machine_mode, const_tree,
186                                       const_tree);
187 static int aapcs_select_return_coproc (const_tree, const_tree);
188
189 #ifdef OBJECT_FORMAT_ELF
190 static void arm_elf_asm_constructor (rtx, int) ATTRIBUTE_UNUSED;
191 static void arm_elf_asm_destructor (rtx, int) ATTRIBUTE_UNUSED;
192 #endif
193 #ifndef ARM_PE
194 static void arm_encode_section_info (tree, rtx, int);
195 #endif
196
197 static void arm_file_end (void);
198 static void arm_file_start (void);
199
200 static void arm_setup_incoming_varargs (cumulative_args_t, enum machine_mode,
201                                         tree, int *, int);
202 static bool arm_pass_by_reference (cumulative_args_t,
203                                    enum machine_mode, const_tree, bool);
204 static bool arm_promote_prototypes (const_tree);
205 static bool arm_default_short_enums (void);
206 static bool arm_align_anon_bitfield (void);
207 static bool arm_return_in_msb (const_tree);
208 static bool arm_must_pass_in_stack (enum machine_mode, const_tree);
209 static bool arm_return_in_memory (const_tree, const_tree);
210 #if ARM_UNWIND_INFO
211 static void arm_unwind_emit (FILE *, rtx);
212 static bool arm_output_ttype (rtx);
213 static void arm_asm_emit_except_personality (rtx);
214 static void arm_asm_init_sections (void);
215 #endif
216 static rtx arm_dwarf_register_span (rtx);
217
218 static tree arm_cxx_guard_type (void);
219 static bool arm_cxx_guard_mask_bit (void);
220 static tree arm_get_cookie_size (tree);
221 static bool arm_cookie_has_size (void);
222 static bool arm_cxx_cdtor_returns_this (void);
223 static bool arm_cxx_key_method_may_be_inline (void);
224 static void arm_cxx_determine_class_data_visibility (tree);
225 static bool arm_cxx_class_data_always_comdat (void);
226 static bool arm_cxx_use_aeabi_atexit (void);
227 static void arm_init_libfuncs (void);
228 static tree arm_build_builtin_va_list (void);
229 static void arm_expand_builtin_va_start (tree, rtx);
230 static tree arm_gimplify_va_arg_expr (tree, tree, gimple_seq *, gimple_seq *);
231 static void arm_option_override (void);
232 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
233 static bool arm_cannot_copy_insn_p (rtx);
234 static bool arm_tls_symbol_p (rtx x);
235 static int arm_issue_rate (void);
236 static void arm_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
237 static bool arm_output_addr_const_extra (FILE *, rtx);
238 static bool arm_allocate_stack_slots_for_args (void);
239 static const char *arm_invalid_parameter_type (const_tree t);
240 static const char *arm_invalid_return_type (const_tree t);
241 static tree arm_promoted_type (const_tree t);
242 static tree arm_convert_to_type (tree type, tree expr);
243 static bool arm_scalar_mode_supported_p (enum machine_mode);
244 static bool arm_frame_pointer_required (void);
245 static bool arm_can_eliminate (const int, const int);
246 static void arm_asm_trampoline_template (FILE *);
247 static void arm_trampoline_init (rtx, tree, rtx);
248 static rtx arm_trampoline_adjust_address (rtx);
249 static rtx arm_pic_static_addr (rtx orig, rtx reg);
250 static bool cortex_a9_sched_adjust_cost (rtx, rtx, rtx, int *);
251 static bool xscale_sched_adjust_cost (rtx, rtx, rtx, int *);
252 static bool fa726te_sched_adjust_cost (rtx, rtx, rtx, int *);
253 static bool arm_array_mode_supported_p (enum machine_mode,
254                                         unsigned HOST_WIDE_INT);
255 static enum machine_mode arm_preferred_simd_mode (enum machine_mode);
256 static bool arm_class_likely_spilled_p (reg_class_t);
257 static bool arm_vector_alignment_reachable (const_tree type, bool is_packed);
258 static bool arm_builtin_support_vector_misalignment (enum machine_mode mode,
259                                                      const_tree type,
260                                                      int misalignment,
261                                                      bool is_packed);
262 static void arm_conditional_register_usage (void);
263 static reg_class_t arm_preferred_rename_class (reg_class_t rclass);
264 static unsigned int arm_autovectorize_vector_sizes (void);
265 static int arm_default_branch_cost (bool, bool);
266 static int arm_cortex_a5_branch_cost (bool, bool);
267
268 \f
269 /* Table of machine attributes.  */
270 static const struct attribute_spec arm_attribute_table[] =
271 {
272   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
273        affects_type_identity } */
274   /* Function calls made to this symbol must be done indirectly, because
275      it may lie outside of the 26 bit addressing range of a normal function
276      call.  */
277   { "long_call",    0, 0, false, true,  true,  NULL, false },
278   /* Whereas these functions are always known to reside within the 26 bit
279      addressing range.  */
280   { "short_call",   0, 0, false, true,  true,  NULL, false },
281   /* Specify the procedure call conventions for a function.  */
282   { "pcs",          1, 1, false, true,  true,  arm_handle_pcs_attribute,
283     false },
284   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
285   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute,
286     false },
287   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute,
288     false },
289   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute,
290     false },
291 #ifdef ARM_PE
292   /* ARM/PE has three new attributes:
293      interfacearm - ?
294      dllexport - for exporting a function/variable that will live in a dll
295      dllimport - for importing a function/variable from a dll
296
297      Microsoft allows multiple declspecs in one __declspec, separating
298      them with spaces.  We do NOT support this.  Instead, use __declspec
299      multiple times.
300   */
301   { "dllimport",    0, 0, true,  false, false, NULL, false },
302   { "dllexport",    0, 0, true,  false, false, NULL, false },
303   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute,
304     false },
305 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
306   { "dllimport",    0, 0, false, false, false, handle_dll_attribute, false },
307   { "dllexport",    0, 0, false, false, false, handle_dll_attribute, false },
308   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute,
309     false },
310 #endif
311   { NULL,           0, 0, false, false, false, NULL, false }
312 };
313 \f
314 /* Initialize the GCC target structure.  */
315 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
316 #undef  TARGET_MERGE_DECL_ATTRIBUTES
317 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
318 #endif
319
320 #undef TARGET_LEGITIMIZE_ADDRESS
321 #define TARGET_LEGITIMIZE_ADDRESS arm_legitimize_address
322
323 #undef  TARGET_ATTRIBUTE_TABLE
324 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
325
326 #undef TARGET_ASM_FILE_START
327 #define TARGET_ASM_FILE_START arm_file_start
328 #undef TARGET_ASM_FILE_END
329 #define TARGET_ASM_FILE_END arm_file_end
330
331 #undef  TARGET_ASM_ALIGNED_SI_OP
332 #define TARGET_ASM_ALIGNED_SI_OP NULL
333 #undef  TARGET_ASM_INTEGER
334 #define TARGET_ASM_INTEGER arm_assemble_integer
335
336 #undef TARGET_PRINT_OPERAND
337 #define TARGET_PRINT_OPERAND arm_print_operand
338 #undef TARGET_PRINT_OPERAND_ADDRESS
339 #define TARGET_PRINT_OPERAND_ADDRESS arm_print_operand_address
340 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
341 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P arm_print_operand_punct_valid_p
342
343 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
344 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA arm_output_addr_const_extra
345
346 #undef  TARGET_ASM_FUNCTION_PROLOGUE
347 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
348
349 #undef  TARGET_ASM_FUNCTION_EPILOGUE
350 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
351
352 #undef  TARGET_OPTION_OVERRIDE
353 #define TARGET_OPTION_OVERRIDE arm_option_override
354
355 #undef  TARGET_COMP_TYPE_ATTRIBUTES
356 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
357
358 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
359 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
360
361 #undef  TARGET_SCHED_ADJUST_COST
362 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
363
364 #undef TARGET_ENCODE_SECTION_INFO
365 #ifdef ARM_PE
366 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
367 #else
368 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
369 #endif
370
371 #undef  TARGET_STRIP_NAME_ENCODING
372 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
373
374 #undef  TARGET_ASM_INTERNAL_LABEL
375 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
376
377 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
378 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
379
380 #undef  TARGET_FUNCTION_VALUE
381 #define TARGET_FUNCTION_VALUE arm_function_value
382
383 #undef  TARGET_LIBCALL_VALUE
384 #define TARGET_LIBCALL_VALUE arm_libcall_value
385
386 #undef  TARGET_ASM_OUTPUT_MI_THUNK
387 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
388 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
389 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
390
391 #undef  TARGET_RTX_COSTS
392 #define TARGET_RTX_COSTS arm_rtx_costs
393 #undef  TARGET_ADDRESS_COST
394 #define TARGET_ADDRESS_COST arm_address_cost
395
396 #undef TARGET_SHIFT_TRUNCATION_MASK
397 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
398 #undef TARGET_VECTOR_MODE_SUPPORTED_P
399 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
400 #undef TARGET_ARRAY_MODE_SUPPORTED_P
401 #define TARGET_ARRAY_MODE_SUPPORTED_P arm_array_mode_supported_p
402 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
403 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE arm_preferred_simd_mode
404 #undef TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_SIZES
405 #define TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_SIZES \
406   arm_autovectorize_vector_sizes
407
408 #undef  TARGET_MACHINE_DEPENDENT_REORG
409 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
410
411 #undef  TARGET_INIT_BUILTINS
412 #define TARGET_INIT_BUILTINS  arm_init_builtins
413 #undef  TARGET_EXPAND_BUILTIN
414 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
415 #undef  TARGET_BUILTIN_DECL
416 #define TARGET_BUILTIN_DECL arm_builtin_decl
417
418 #undef TARGET_INIT_LIBFUNCS
419 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
420
421 #undef TARGET_PROMOTE_FUNCTION_MODE
422 #define TARGET_PROMOTE_FUNCTION_MODE arm_promote_function_mode
423 #undef TARGET_PROMOTE_PROTOTYPES
424 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
425 #undef TARGET_PASS_BY_REFERENCE
426 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
427 #undef TARGET_ARG_PARTIAL_BYTES
428 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
429 #undef TARGET_FUNCTION_ARG
430 #define TARGET_FUNCTION_ARG arm_function_arg
431 #undef TARGET_FUNCTION_ARG_ADVANCE
432 #define TARGET_FUNCTION_ARG_ADVANCE arm_function_arg_advance
433 #undef TARGET_FUNCTION_ARG_BOUNDARY
434 #define TARGET_FUNCTION_ARG_BOUNDARY arm_function_arg_boundary
435
436 #undef  TARGET_SETUP_INCOMING_VARARGS
437 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
438
439 #undef TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS
440 #define TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS arm_allocate_stack_slots_for_args
441
442 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
443 #define TARGET_ASM_TRAMPOLINE_TEMPLATE arm_asm_trampoline_template
444 #undef TARGET_TRAMPOLINE_INIT
445 #define TARGET_TRAMPOLINE_INIT arm_trampoline_init
446 #undef TARGET_TRAMPOLINE_ADJUST_ADDRESS
447 #define TARGET_TRAMPOLINE_ADJUST_ADDRESS arm_trampoline_adjust_address
448
449 #undef TARGET_DEFAULT_SHORT_ENUMS
450 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
451
452 #undef TARGET_ALIGN_ANON_BITFIELD
453 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
454
455 #undef TARGET_NARROW_VOLATILE_BITFIELD
456 #define TARGET_NARROW_VOLATILE_BITFIELD hook_bool_void_false
457
458 #undef TARGET_CXX_GUARD_TYPE
459 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
460
461 #undef TARGET_CXX_GUARD_MASK_BIT
462 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
463
464 #undef TARGET_CXX_GET_COOKIE_SIZE
465 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
466
467 #undef TARGET_CXX_COOKIE_HAS_SIZE
468 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
469
470 #undef TARGET_CXX_CDTOR_RETURNS_THIS
471 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
472
473 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
474 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
475
476 #undef TARGET_CXX_USE_AEABI_ATEXIT
477 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
478
479 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
480 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
481   arm_cxx_determine_class_data_visibility
482
483 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
484 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
485
486 #undef TARGET_RETURN_IN_MSB
487 #define TARGET_RETURN_IN_MSB arm_return_in_msb
488
489 #undef TARGET_RETURN_IN_MEMORY
490 #define TARGET_RETURN_IN_MEMORY arm_return_in_memory
491
492 #undef TARGET_MUST_PASS_IN_STACK
493 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
494
495 #if ARM_UNWIND_INFO
496 #undef TARGET_ASM_UNWIND_EMIT
497 #define TARGET_ASM_UNWIND_EMIT arm_unwind_emit
498
499 /* EABI unwinding tables use a different format for the typeinfo tables.  */
500 #undef TARGET_ASM_TTYPE
501 #define TARGET_ASM_TTYPE arm_output_ttype
502
503 #undef TARGET_ARM_EABI_UNWINDER
504 #define TARGET_ARM_EABI_UNWINDER true
505
506 #undef TARGET_ASM_EMIT_EXCEPT_PERSONALITY
507 #define TARGET_ASM_EMIT_EXCEPT_PERSONALITY arm_asm_emit_except_personality
508
509 #undef TARGET_ASM_INIT_SECTIONS
510 #define TARGET_ASM_INIT_SECTIONS arm_asm_init_sections
511 #endif /* ARM_UNWIND_INFO */
512
513 #undef TARGET_DWARF_REGISTER_SPAN
514 #define TARGET_DWARF_REGISTER_SPAN arm_dwarf_register_span
515
516 #undef  TARGET_CANNOT_COPY_INSN_P
517 #define TARGET_CANNOT_COPY_INSN_P arm_cannot_copy_insn_p
518
519 #ifdef HAVE_AS_TLS
520 #undef TARGET_HAVE_TLS
521 #define TARGET_HAVE_TLS true
522 #endif
523
524 #undef TARGET_HAVE_CONDITIONAL_EXECUTION
525 #define TARGET_HAVE_CONDITIONAL_EXECUTION arm_have_conditional_execution
526
527 #undef TARGET_LEGITIMATE_CONSTANT_P
528 #define TARGET_LEGITIMATE_CONSTANT_P arm_legitimate_constant_p
529
530 #undef TARGET_CANNOT_FORCE_CONST_MEM
531 #define TARGET_CANNOT_FORCE_CONST_MEM arm_cannot_force_const_mem
532
533 #undef TARGET_MAX_ANCHOR_OFFSET
534 #define TARGET_MAX_ANCHOR_OFFSET 4095
535
536 /* The minimum is set such that the total size of the block
537    for a particular anchor is -4088 + 1 + 4095 bytes, which is
538    divisible by eight, ensuring natural spacing of anchors.  */
539 #undef TARGET_MIN_ANCHOR_OFFSET
540 #define TARGET_MIN_ANCHOR_OFFSET -4088
541
542 #undef TARGET_SCHED_ISSUE_RATE
543 #define TARGET_SCHED_ISSUE_RATE arm_issue_rate
544
545 #undef TARGET_MANGLE_TYPE
546 #define TARGET_MANGLE_TYPE arm_mangle_type
547
548 #undef TARGET_BUILD_BUILTIN_VA_LIST
549 #define TARGET_BUILD_BUILTIN_VA_LIST arm_build_builtin_va_list
550 #undef TARGET_EXPAND_BUILTIN_VA_START
551 #define TARGET_EXPAND_BUILTIN_VA_START arm_expand_builtin_va_start
552 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
553 #define TARGET_GIMPLIFY_VA_ARG_EXPR arm_gimplify_va_arg_expr
554
555 #ifdef HAVE_AS_TLS
556 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
557 #define TARGET_ASM_OUTPUT_DWARF_DTPREL arm_output_dwarf_dtprel
558 #endif
559
560 #undef TARGET_LEGITIMATE_ADDRESS_P
561 #define TARGET_LEGITIMATE_ADDRESS_P     arm_legitimate_address_p
562
563 #undef TARGET_INVALID_PARAMETER_TYPE
564 #define TARGET_INVALID_PARAMETER_TYPE arm_invalid_parameter_type
565
566 #undef TARGET_INVALID_RETURN_TYPE
567 #define TARGET_INVALID_RETURN_TYPE arm_invalid_return_type
568
569 #undef TARGET_PROMOTED_TYPE
570 #define TARGET_PROMOTED_TYPE arm_promoted_type
571
572 #undef TARGET_CONVERT_TO_TYPE
573 #define TARGET_CONVERT_TO_TYPE arm_convert_to_type
574
575 #undef TARGET_SCALAR_MODE_SUPPORTED_P
576 #define TARGET_SCALAR_MODE_SUPPORTED_P arm_scalar_mode_supported_p
577
578 #undef TARGET_FRAME_POINTER_REQUIRED
579 #define TARGET_FRAME_POINTER_REQUIRED arm_frame_pointer_required
580
581 #undef TARGET_CAN_ELIMINATE
582 #define TARGET_CAN_ELIMINATE arm_can_eliminate
583
584 #undef TARGET_CONDITIONAL_REGISTER_USAGE
585 #define TARGET_CONDITIONAL_REGISTER_USAGE arm_conditional_register_usage
586
587 #undef TARGET_CLASS_LIKELY_SPILLED_P
588 #define TARGET_CLASS_LIKELY_SPILLED_P arm_class_likely_spilled_p
589
590 #undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
591 #define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE \
592   arm_vector_alignment_reachable
593
594 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
595 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT \
596   arm_builtin_support_vector_misalignment
597
598 #undef TARGET_PREFERRED_RENAME_CLASS
599 #define TARGET_PREFERRED_RENAME_CLASS \
600   arm_preferred_rename_class
601
602 struct gcc_target targetm = TARGET_INITIALIZER;
603 \f
604 /* Obstack for minipool constant handling.  */
605 static struct obstack minipool_obstack;
606 static char *         minipool_startobj;
607
608 /* The maximum number of insns skipped which
609    will be conditionalised if possible.  */
610 static int max_insns_skipped = 5;
611
612 extern FILE * asm_out_file;
613
614 /* True if we are currently building a constant table.  */
615 int making_const_table;
616
617 /* The processor for which instructions should be scheduled.  */
618 enum processor_type arm_tune = arm_none;
619
620 /* The current tuning set.  */
621 const struct tune_params *current_tune;
622
623 /* Which floating point hardware to schedule for.  */
624 int arm_fpu_attr;
625
626 /* Which floating popint hardware to use.  */
627 const struct arm_fpu_desc *arm_fpu_desc;
628
629 /* Used for Thumb call_via trampolines.  */
630 rtx thumb_call_via_label[14];
631 static int thumb_call_reg_needed;
632
633 /* Bit values used to identify processor capabilities.  */
634 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
635 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
636 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
637 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
638 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
639 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
640 #define FL_THUMB      (1 << 6)        /* Thumb aware */
641 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
642 #define FL_STRONG     (1 << 8)        /* StrongARM */
643 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
644 #define FL_XSCALE     (1 << 10)       /* XScale */
645 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
646 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
647                                          media instructions.  */
648 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
649 #define FL_WBUF       (1 << 14)       /* Schedule for write buffer ops.
650                                          Note: ARM6 & 7 derivatives only.  */
651 #define FL_ARCH6K     (1 << 15)       /* Architecture rel 6 K extensions.  */
652 #define FL_THUMB2     (1 << 16)       /* Thumb-2.  */
653 #define FL_NOTM       (1 << 17)       /* Instructions not present in the 'M'
654                                          profile.  */
655 #define FL_THUMB_DIV  (1 << 18)       /* Hardware divide (Thumb mode).  */
656 #define FL_VFPV3      (1 << 19)       /* Vector Floating Point V3.  */
657 #define FL_NEON       (1 << 20)       /* Neon instructions.  */
658 #define FL_ARCH7EM    (1 << 21)       /* Instructions present in the ARMv7E-M
659                                          architecture.  */
660 #define FL_ARCH7      (1 << 22)       /* Architecture 7.  */
661 #define FL_ARM_DIV    (1 << 23)       /* Hardware divide (ARM mode).  */
662
663 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
664
665 /* Flags that only effect tuning, not available instructions.  */
666 #define FL_TUNE         (FL_WBUF | FL_VFPV2 | FL_STRONG | FL_LDSCHED \
667                          | FL_CO_PROC)
668
669 #define FL_FOR_ARCH2    FL_NOTM
670 #define FL_FOR_ARCH3    (FL_FOR_ARCH2 | FL_MODE32)
671 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
672 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
673 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
674 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
675 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
676 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
677 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
678 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
679 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
680 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
681 #define FL_FOR_ARCH6K   (FL_FOR_ARCH6 | FL_ARCH6K)
682 #define FL_FOR_ARCH6Z   FL_FOR_ARCH6
683 #define FL_FOR_ARCH6ZK  FL_FOR_ARCH6K
684 #define FL_FOR_ARCH6T2  (FL_FOR_ARCH6 | FL_THUMB2)
685 #define FL_FOR_ARCH6M   (FL_FOR_ARCH6 & ~FL_NOTM)
686 #define FL_FOR_ARCH7    ((FL_FOR_ARCH6T2 & ~FL_NOTM) | FL_ARCH7)
687 #define FL_FOR_ARCH7A   (FL_FOR_ARCH7 | FL_NOTM | FL_ARCH6K)
688 #define FL_FOR_ARCH7R   (FL_FOR_ARCH7A | FL_THUMB_DIV)
689 #define FL_FOR_ARCH7M   (FL_FOR_ARCH7 | FL_THUMB_DIV)
690 #define FL_FOR_ARCH7EM  (FL_FOR_ARCH7M | FL_ARCH7EM)
691
692 /* The bits in this mask specify which
693    instructions we are allowed to generate.  */
694 static unsigned long insn_flags = 0;
695
696 /* The bits in this mask specify which instruction scheduling options should
697    be used.  */
698 static unsigned long tune_flags = 0;
699
700 /* The following are used in the arm.md file as equivalents to bits
701    in the above two flag variables.  */
702
703 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
704 int arm_arch3m = 0;
705
706 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
707 int arm_arch4 = 0;
708
709 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
710 int arm_arch4t = 0;
711
712 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
713 int arm_arch5 = 0;
714
715 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
716 int arm_arch5e = 0;
717
718 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
719 int arm_arch6 = 0;
720
721 /* Nonzero if this chip supports the ARM 6K extensions.  */
722 int arm_arch6k = 0;
723
724 /* Nonzero if this chip supports the ARM 7 extensions.  */
725 int arm_arch7 = 0;
726
727 /* Nonzero if instructions not present in the 'M' profile can be used.  */
728 int arm_arch_notm = 0;
729
730 /* Nonzero if instructions present in ARMv7E-M can be used.  */
731 int arm_arch7em = 0;
732
733 /* Nonzero if this chip can benefit from load scheduling.  */
734 int arm_ld_sched = 0;
735
736 /* Nonzero if this chip is a StrongARM.  */
737 int arm_tune_strongarm = 0;
738
739 /* Nonzero if this chip is a Cirrus variant.  */
740 int arm_arch_cirrus = 0;
741
742 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
743 int arm_arch_iwmmxt = 0;
744
745 /* Nonzero if this chip is an XScale.  */
746 int arm_arch_xscale = 0;
747
748 /* Nonzero if tuning for XScale  */
749 int arm_tune_xscale = 0;
750
751 /* Nonzero if we want to tune for stores that access the write-buffer.
752    This typically means an ARM6 or ARM7 with MMU or MPU.  */
753 int arm_tune_wbuf = 0;
754
755 /* Nonzero if tuning for Cortex-A9.  */
756 int arm_tune_cortex_a9 = 0;
757
758 /* Nonzero if generating Thumb instructions.  */
759 int thumb_code = 0;
760
761 /* Nonzero if generating Thumb-1 instructions.  */
762 int thumb1_code = 0;
763
764 /* Nonzero if we should define __THUMB_INTERWORK__ in the
765    preprocessor.
766    XXX This is a bit of a hack, it's intended to help work around
767    problems in GLD which doesn't understand that armv5t code is
768    interworking clean.  */
769 int arm_cpp_interwork = 0;
770
771 /* Nonzero if chip supports Thumb 2.  */
772 int arm_arch_thumb2;
773
774 /* Nonzero if chip supports integer division instruction.  */
775 int arm_arch_arm_hwdiv;
776 int arm_arch_thumb_hwdiv;
777
778 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference,
779    we must report the mode of the memory reference from
780    TARGET_PRINT_OPERAND to TARGET_PRINT_OPERAND_ADDRESS.  */
781 enum machine_mode output_memory_reference_mode;
782
783 /* The register number to be used for the PIC offset register.  */
784 unsigned arm_pic_register = INVALID_REGNUM;
785
786 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
787    the next function.  */
788 static int after_arm_reorg = 0;
789
790 enum arm_pcs arm_pcs_default;
791
792 /* For an explanation of these variables, see final_prescan_insn below.  */
793 int arm_ccfsm_state;
794 /* arm_current_cc is also used for Thumb-2 cond_exec blocks.  */
795 enum arm_cond_code arm_current_cc;
796
797 rtx arm_target_insn;
798 int arm_target_label;
799 /* The number of conditionally executed insns, including the current insn.  */
800 int arm_condexec_count = 0;
801 /* A bitmask specifying the patterns for the IT block.
802    Zero means do not output an IT block before this insn. */
803 int arm_condexec_mask = 0;
804 /* The number of bits used in arm_condexec_mask.  */
805 int arm_condexec_masklen = 0;
806
807 /* The condition codes of the ARM, and the inverse function.  */
808 static const char * const arm_condition_codes[] =
809 {
810   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
811   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
812 };
813
814 /* The register numbers in sequence, for passing to arm_gen_load_multiple.  */
815 int arm_regs_in_sequence[] =
816 {
817   0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
818 };
819
820 #define ARM_LSL_NAME (TARGET_UNIFIED_ASM ? "lsl" : "asl")
821 #define streq(string1, string2) (strcmp (string1, string2) == 0)
822
823 #define THUMB2_WORK_REGS (0xff & ~(  (1 << THUMB_HARD_FRAME_POINTER_REGNUM) \
824                                    | (1 << SP_REGNUM) | (1 << PC_REGNUM) \
825                                    | (1 << PIC_OFFSET_TABLE_REGNUM)))
826 \f
827 /* Initialization code.  */
828
829 struct processors
830 {
831   const char *const name;
832   enum processor_type core;
833   const char *arch;
834   const unsigned long flags;
835   const struct tune_params *const tune;
836 };
837
838
839 #define ARM_PREFETCH_NOT_BENEFICIAL 0, -1, -1
840 #define ARM_PREFETCH_BENEFICIAL(prefetch_slots,l1_size,l1_line_size) \
841   prefetch_slots, \
842   l1_size, \
843   l1_line_size
844
845 const struct tune_params arm_slowmul_tune =
846 {
847   arm_slowmul_rtx_costs,
848   NULL,
849   3,                                            /* Constant limit.  */
850   5,                                            /* Max cond insns.  */
851   ARM_PREFETCH_NOT_BENEFICIAL,
852   true,                                         /* Prefer constant pool.  */
853   arm_default_branch_cost
854 };
855
856 const struct tune_params arm_fastmul_tune =
857 {
858   arm_fastmul_rtx_costs,
859   NULL,
860   1,                                            /* Constant limit.  */
861   5,                                            /* Max cond insns.  */
862   ARM_PREFETCH_NOT_BENEFICIAL,
863   true,                                         /* Prefer constant pool.  */
864   arm_default_branch_cost
865 };
866
867 /* StrongARM has early execution of branches, so a sequence that is worth
868    skipping is shorter.  Set max_insns_skipped to a lower value.  */
869
870 const struct tune_params arm_strongarm_tune =
871 {
872   arm_fastmul_rtx_costs,
873   NULL,
874   1,                                            /* Constant limit.  */
875   3,                                            /* Max cond insns.  */
876   ARM_PREFETCH_NOT_BENEFICIAL,
877   true,                                         /* Prefer constant pool.  */
878   arm_default_branch_cost
879 };
880
881 const struct tune_params arm_xscale_tune =
882 {
883   arm_xscale_rtx_costs,
884   xscale_sched_adjust_cost,
885   2,                                            /* Constant limit.  */
886   3,                                            /* Max cond insns.  */
887   ARM_PREFETCH_NOT_BENEFICIAL,
888   true,                                         /* Prefer constant pool.  */
889   arm_default_branch_cost
890 };
891
892 const struct tune_params arm_9e_tune =
893 {
894   arm_9e_rtx_costs,
895   NULL,
896   1,                                            /* Constant limit.  */
897   5,                                            /* Max cond insns.  */
898   ARM_PREFETCH_NOT_BENEFICIAL,
899   true,                                         /* Prefer constant pool.  */
900   arm_default_branch_cost
901 };
902
903 const struct tune_params arm_v6t2_tune =
904 {
905   arm_9e_rtx_costs,
906   NULL,
907   1,                                            /* Constant limit.  */
908   5,                                            /* Max cond insns.  */
909   ARM_PREFETCH_NOT_BENEFICIAL,
910   false,                                        /* Prefer constant pool.  */
911   arm_default_branch_cost
912 };
913
914 /* Generic Cortex tuning.  Use more specific tunings if appropriate.  */
915 const struct tune_params arm_cortex_tune =
916 {
917   arm_9e_rtx_costs,
918   NULL,
919   1,                                            /* Constant limit.  */
920   5,                                            /* Max cond insns.  */
921   ARM_PREFETCH_NOT_BENEFICIAL,
922   false,                                        /* Prefer constant pool.  */
923   arm_default_branch_cost
924 };
925
926 /* Branches can be dual-issued on Cortex-A5, so conditional execution is
927    less appealing.  Set max_insns_skipped to a low value.  */
928
929 const struct tune_params arm_cortex_a5_tune =
930 {
931   arm_9e_rtx_costs,
932   NULL,
933   1,                                            /* Constant limit.  */
934   1,                                            /* Max cond insns.  */
935   ARM_PREFETCH_NOT_BENEFICIAL,
936   false,                                        /* Prefer constant pool.  */
937   arm_cortex_a5_branch_cost
938 };
939
940 const struct tune_params arm_cortex_a9_tune =
941 {
942   arm_9e_rtx_costs,
943   cortex_a9_sched_adjust_cost,
944   1,                                            /* Constant limit.  */
945   5,                                            /* Max cond insns.  */
946   ARM_PREFETCH_BENEFICIAL(4,32,32),
947   false,                                        /* Prefer constant pool.  */
948   arm_default_branch_cost
949 };
950
951 const struct tune_params arm_fa726te_tune =
952 {
953   arm_9e_rtx_costs,
954   fa726te_sched_adjust_cost,
955   1,                                            /* Constant limit.  */
956   5,                                            /* Max cond insns.  */
957   ARM_PREFETCH_NOT_BENEFICIAL,
958   true,                                         /* Prefer constant pool.  */
959   arm_default_branch_cost
960 };
961
962
963 /* Not all of these give usefully different compilation alternatives,
964    but there is no simple way of generalizing them.  */
965 static const struct processors all_cores[] =
966 {
967   /* ARM Cores */
968 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
969   {NAME, IDENT, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, &arm_##COSTS##_tune},
970 #include "arm-cores.def"
971 #undef ARM_CORE
972   {NULL, arm_none, NULL, 0, NULL}
973 };
974
975 static const struct processors all_architectures[] =
976 {
977   /* ARM Architectures */
978   /* We don't specify tuning costs here as it will be figured out
979      from the core.  */
980
981 #define ARM_ARCH(NAME, CORE, ARCH, FLAGS) \
982   {NAME, CORE, #ARCH, FLAGS, NULL},
983 #include "arm-arches.def"
984 #undef ARM_ARCH
985   {NULL, arm_none, NULL, 0 , NULL}
986 };
987
988
989 /* These are populated as commandline arguments are processed, or NULL
990    if not specified.  */
991 static const struct processors *arm_selected_arch;
992 static const struct processors *arm_selected_cpu;
993 static const struct processors *arm_selected_tune;
994
995 /* The name of the preprocessor macro to define for this architecture.  */
996
997 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
998
999 /* Available values for -mfpu=.  */
1000
1001 static const struct arm_fpu_desc all_fpus[] =
1002 {
1003 #define ARM_FPU(NAME, MODEL, REV, VFP_REGS, NEON, FP16) \
1004   { NAME, MODEL, REV, VFP_REGS, NEON, FP16 },
1005 #include "arm-fpus.def"
1006 #undef ARM_FPU
1007 };
1008
1009
1010 /* Supported TLS relocations.  */
1011
1012 enum tls_reloc {
1013   TLS_GD32,
1014   TLS_LDM32,
1015   TLS_LDO32,
1016   TLS_IE32,
1017   TLS_LE32,
1018   TLS_DESCSEQ   /* GNU scheme */
1019 };
1020
1021 /* The maximum number of insns to be used when loading a constant.  */
1022 inline static int
1023 arm_constant_limit (bool size_p)
1024 {
1025   return size_p ? 1 : current_tune->constant_limit;
1026 }
1027
1028 /* Emit an insn that's a simple single-set.  Both the operands must be known
1029    to be valid.  */
1030 inline static rtx
1031 emit_set_insn (rtx x, rtx y)
1032 {
1033   return emit_insn (gen_rtx_SET (VOIDmode, x, y));
1034 }
1035
1036 /* Return the number of bits set in VALUE.  */
1037 static unsigned
1038 bit_count (unsigned long value)
1039 {
1040   unsigned long count = 0;
1041
1042   while (value)
1043     {
1044       count++;
1045       value &= value - 1;  /* Clear the least-significant set bit.  */
1046     }
1047
1048   return count;
1049 }
1050
1051 typedef struct
1052 {
1053   enum machine_mode mode;
1054   const char *name;
1055 } arm_fixed_mode_set;
1056
1057 /* A small helper for setting fixed-point library libfuncs.  */
1058
1059 static void
1060 arm_set_fixed_optab_libfunc (optab optable, enum machine_mode mode,
1061                              const char *funcname, const char *modename,
1062                              int num_suffix)
1063 {
1064   char buffer[50];
1065   
1066   if (num_suffix == 0)
1067     sprintf (buffer, "__gnu_%s%s", funcname, modename);
1068   else
1069     sprintf (buffer, "__gnu_%s%s%d", funcname, modename, num_suffix);
1070   
1071   set_optab_libfunc (optable, mode, buffer);
1072 }
1073
1074 static void
1075 arm_set_fixed_conv_libfunc (convert_optab optable, enum machine_mode to,
1076                             enum machine_mode from, const char *funcname,
1077                             const char *toname, const char *fromname)
1078 {
1079   char buffer[50];
1080   const char *maybe_suffix_2 = "";
1081   
1082   /* Follow the logic for selecting a "2" suffix in fixed-bit.h.  */
1083   if (ALL_FIXED_POINT_MODE_P (from) && ALL_FIXED_POINT_MODE_P (to)
1084       && UNSIGNED_FIXED_POINT_MODE_P (from) == UNSIGNED_FIXED_POINT_MODE_P (to)
1085       && ALL_FRACT_MODE_P (from) == ALL_FRACT_MODE_P (to))
1086     maybe_suffix_2 = "2";
1087   
1088   sprintf (buffer, "__gnu_%s%s%s%s", funcname, fromname, toname,
1089            maybe_suffix_2);
1090
1091   set_conv_libfunc (optable, to, from, buffer);
1092 }
1093
1094 /* Set up library functions unique to ARM.  */
1095
1096 static void
1097 arm_init_libfuncs (void)
1098 {
1099   /* There are no special library functions unless we are using the
1100      ARM BPABI.  */
1101   if (!TARGET_BPABI)
1102     return;
1103
1104   /* The functions below are described in Section 4 of the "Run-Time
1105      ABI for the ARM architecture", Version 1.0.  */
1106
1107   /* Double-precision floating-point arithmetic.  Table 2.  */
1108   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
1109   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
1110   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
1111   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
1112   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
1113
1114   /* Double-precision comparisons.  Table 3.  */
1115   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
1116   set_optab_libfunc (ne_optab, DFmode, NULL);
1117   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
1118   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
1119   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
1120   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
1121   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
1122
1123   /* Single-precision floating-point arithmetic.  Table 4.  */
1124   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
1125   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
1126   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
1127   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
1128   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
1129
1130   /* Single-precision comparisons.  Table 5.  */
1131   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
1132   set_optab_libfunc (ne_optab, SFmode, NULL);
1133   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
1134   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
1135   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
1136   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
1137   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
1138
1139   /* Floating-point to integer conversions.  Table 6.  */
1140   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
1141   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
1142   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
1143   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
1144   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
1145   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
1146   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
1147   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
1148
1149   /* Conversions between floating types.  Table 7.  */
1150   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
1151   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
1152
1153   /* Integer to floating-point conversions.  Table 8.  */
1154   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
1155   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
1156   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
1157   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
1158   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
1159   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
1160   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
1161   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
1162
1163   /* Long long.  Table 9.  */
1164   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
1165   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
1166   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
1167   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
1168   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
1169   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
1170   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
1171   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
1172
1173   /* Integer (32/32->32) division.  \S 4.3.1.  */
1174   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
1175   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
1176
1177   /* The divmod functions are designed so that they can be used for
1178      plain division, even though they return both the quotient and the
1179      remainder.  The quotient is returned in the usual location (i.e.,
1180      r0 for SImode, {r0, r1} for DImode), just as would be expected
1181      for an ordinary division routine.  Because the AAPCS calling
1182      conventions specify that all of { r0, r1, r2, r3 } are
1183      callee-saved registers, there is no need to tell the compiler
1184      explicitly that those registers are clobbered by these
1185      routines.  */
1186   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
1187   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
1188
1189   /* For SImode division the ABI provides div-without-mod routines,
1190      which are faster.  */
1191   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
1192   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
1193
1194   /* We don't have mod libcalls.  Fortunately gcc knows how to use the
1195      divmod libcalls instead.  */
1196   set_optab_libfunc (smod_optab, DImode, NULL);
1197   set_optab_libfunc (umod_optab, DImode, NULL);
1198   set_optab_libfunc (smod_optab, SImode, NULL);
1199   set_optab_libfunc (umod_optab, SImode, NULL);
1200
1201   /* Half-precision float operations.  The compiler handles all operations
1202      with NULL libfuncs by converting the SFmode.  */
1203   switch (arm_fp16_format)
1204     {
1205     case ARM_FP16_FORMAT_IEEE:
1206     case ARM_FP16_FORMAT_ALTERNATIVE:
1207
1208       /* Conversions.  */
1209       set_conv_libfunc (trunc_optab, HFmode, SFmode,
1210                         (arm_fp16_format == ARM_FP16_FORMAT_IEEE
1211                          ? "__gnu_f2h_ieee"
1212                          : "__gnu_f2h_alternative"));
1213       set_conv_libfunc (sext_optab, SFmode, HFmode, 
1214                         (arm_fp16_format == ARM_FP16_FORMAT_IEEE
1215                          ? "__gnu_h2f_ieee"
1216                          : "__gnu_h2f_alternative"));
1217       
1218       /* Arithmetic.  */
1219       set_optab_libfunc (add_optab, HFmode, NULL);
1220       set_optab_libfunc (sdiv_optab, HFmode, NULL);
1221       set_optab_libfunc (smul_optab, HFmode, NULL);
1222       set_optab_libfunc (neg_optab, HFmode, NULL);
1223       set_optab_libfunc (sub_optab, HFmode, NULL);
1224
1225       /* Comparisons.  */
1226       set_optab_libfunc (eq_optab, HFmode, NULL);
1227       set_optab_libfunc (ne_optab, HFmode, NULL);
1228       set_optab_libfunc (lt_optab, HFmode, NULL);
1229       set_optab_libfunc (le_optab, HFmode, NULL);
1230       set_optab_libfunc (ge_optab, HFmode, NULL);
1231       set_optab_libfunc (gt_optab, HFmode, NULL);
1232       set_optab_libfunc (unord_optab, HFmode, NULL);
1233       break;
1234
1235     default:
1236       break;
1237     }
1238
1239   /* Use names prefixed with __gnu_ for fixed-point helper functions.  */
1240   {
1241     const arm_fixed_mode_set fixed_arith_modes[] =
1242       {
1243         { QQmode, "qq" },
1244         { UQQmode, "uqq" },
1245         { HQmode, "hq" },
1246         { UHQmode, "uhq" },
1247         { SQmode, "sq" },
1248         { USQmode, "usq" },
1249         { DQmode, "dq" },
1250         { UDQmode, "udq" },
1251         { TQmode, "tq" },
1252         { UTQmode, "utq" },
1253         { HAmode, "ha" },
1254         { UHAmode, "uha" },
1255         { SAmode, "sa" },
1256         { USAmode, "usa" },
1257         { DAmode, "da" },
1258         { UDAmode, "uda" },
1259         { TAmode, "ta" },
1260         { UTAmode, "uta" }
1261       };
1262     const arm_fixed_mode_set fixed_conv_modes[] =
1263       {
1264         { QQmode, "qq" },
1265         { UQQmode, "uqq" },
1266         { HQmode, "hq" },
1267         { UHQmode, "uhq" },
1268         { SQmode, "sq" },
1269         { USQmode, "usq" },
1270         { DQmode, "dq" },
1271         { UDQmode, "udq" },
1272         { TQmode, "tq" },
1273         { UTQmode, "utq" },
1274         { HAmode, "ha" },
1275         { UHAmode, "uha" },
1276         { SAmode, "sa" },
1277         { USAmode, "usa" },
1278         { DAmode, "da" },
1279         { UDAmode, "uda" },
1280         { TAmode, "ta" },
1281         { UTAmode, "uta" },
1282         { QImode, "qi" },
1283         { HImode, "hi" },
1284         { SImode, "si" },
1285         { DImode, "di" },
1286         { TImode, "ti" },
1287         { SFmode, "sf" },
1288         { DFmode, "df" }
1289       };
1290     unsigned int i, j;
1291
1292     for (i = 0; i < ARRAY_SIZE (fixed_arith_modes); i++)
1293       {
1294         arm_set_fixed_optab_libfunc (add_optab, fixed_arith_modes[i].mode,
1295                                      "add", fixed_arith_modes[i].name, 3);
1296         arm_set_fixed_optab_libfunc (ssadd_optab, fixed_arith_modes[i].mode,
1297                                      "ssadd", fixed_arith_modes[i].name, 3);
1298         arm_set_fixed_optab_libfunc (usadd_optab, fixed_arith_modes[i].mode,
1299                                      "usadd", fixed_arith_modes[i].name, 3);
1300         arm_set_fixed_optab_libfunc (sub_optab, fixed_arith_modes[i].mode,
1301                                      "sub", fixed_arith_modes[i].name, 3);
1302         arm_set_fixed_optab_libfunc (sssub_optab, fixed_arith_modes[i].mode,
1303                                      "sssub", fixed_arith_modes[i].name, 3);
1304         arm_set_fixed_optab_libfunc (ussub_optab, fixed_arith_modes[i].mode,
1305                                      "ussub", fixed_arith_modes[i].name, 3);
1306         arm_set_fixed_optab_libfunc (smul_optab, fixed_arith_modes[i].mode,
1307                                      "mul", fixed_arith_modes[i].name, 3);
1308         arm_set_fixed_optab_libfunc (ssmul_optab, fixed_arith_modes[i].mode,
1309                                      "ssmul", fixed_arith_modes[i].name, 3);
1310         arm_set_fixed_optab_libfunc (usmul_optab, fixed_arith_modes[i].mode,
1311                                      "usmul", fixed_arith_modes[i].name, 3);
1312         arm_set_fixed_optab_libfunc (sdiv_optab, fixed_arith_modes[i].mode,
1313                                      "div", fixed_arith_modes[i].name, 3);
1314         arm_set_fixed_optab_libfunc (udiv_optab, fixed_arith_modes[i].mode,
1315                                      "udiv", fixed_arith_modes[i].name, 3);
1316         arm_set_fixed_optab_libfunc (ssdiv_optab, fixed_arith_modes[i].mode,
1317                                      "ssdiv", fixed_arith_modes[i].name, 3);
1318         arm_set_fixed_optab_libfunc (usdiv_optab, fixed_arith_modes[i].mode,
1319                                      "usdiv", fixed_arith_modes[i].name, 3);
1320         arm_set_fixed_optab_libfunc (neg_optab, fixed_arith_modes[i].mode,
1321                                      "neg", fixed_arith_modes[i].name, 2);
1322         arm_set_fixed_optab_libfunc (ssneg_optab, fixed_arith_modes[i].mode,
1323                                      "ssneg", fixed_arith_modes[i].name, 2);
1324         arm_set_fixed_optab_libfunc (usneg_optab, fixed_arith_modes[i].mode,
1325                                      "usneg", fixed_arith_modes[i].name, 2);
1326         arm_set_fixed_optab_libfunc (ashl_optab, fixed_arith_modes[i].mode,
1327                                      "ashl", fixed_arith_modes[i].name, 3);
1328         arm_set_fixed_optab_libfunc (ashr_optab, fixed_arith_modes[i].mode,
1329                                      "ashr", fixed_arith_modes[i].name, 3);
1330         arm_set_fixed_optab_libfunc (lshr_optab, fixed_arith_modes[i].mode,
1331                                      "lshr", fixed_arith_modes[i].name, 3);
1332         arm_set_fixed_optab_libfunc (ssashl_optab, fixed_arith_modes[i].mode,
1333                                      "ssashl", fixed_arith_modes[i].name, 3);
1334         arm_set_fixed_optab_libfunc (usashl_optab, fixed_arith_modes[i].mode,
1335                                      "usashl", fixed_arith_modes[i].name, 3);
1336         arm_set_fixed_optab_libfunc (cmp_optab, fixed_arith_modes[i].mode,
1337                                      "cmp", fixed_arith_modes[i].name, 2);
1338       }
1339
1340     for (i = 0; i < ARRAY_SIZE (fixed_conv_modes); i++)
1341       for (j = 0; j < ARRAY_SIZE (fixed_conv_modes); j++)
1342         {
1343           if (i == j
1344               || (!ALL_FIXED_POINT_MODE_P (fixed_conv_modes[i].mode)
1345                   && !ALL_FIXED_POINT_MODE_P (fixed_conv_modes[j].mode)))
1346             continue;
1347
1348           arm_set_fixed_conv_libfunc (fract_optab, fixed_conv_modes[i].mode,
1349                                       fixed_conv_modes[j].mode, "fract",
1350                                       fixed_conv_modes[i].name,
1351                                       fixed_conv_modes[j].name);
1352           arm_set_fixed_conv_libfunc (satfract_optab,
1353                                       fixed_conv_modes[i].mode,
1354                                       fixed_conv_modes[j].mode, "satfract",
1355                                       fixed_conv_modes[i].name,
1356                                       fixed_conv_modes[j].name);
1357           arm_set_fixed_conv_libfunc (fractuns_optab,
1358                                       fixed_conv_modes[i].mode,
1359                                       fixed_conv_modes[j].mode, "fractuns",
1360                                       fixed_conv_modes[i].name,
1361                                       fixed_conv_modes[j].name);
1362           arm_set_fixed_conv_libfunc (satfractuns_optab,
1363                                       fixed_conv_modes[i].mode,
1364                                       fixed_conv_modes[j].mode, "satfractuns",
1365                                       fixed_conv_modes[i].name,
1366                                       fixed_conv_modes[j].name);
1367         }
1368   }
1369
1370   if (TARGET_AAPCS_BASED)
1371     synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
1372 }
1373
1374 /* On AAPCS systems, this is the "struct __va_list".  */
1375 static GTY(()) tree va_list_type;
1376
1377 /* Return the type to use as __builtin_va_list.  */
1378 static tree
1379 arm_build_builtin_va_list (void)
1380 {
1381   tree va_list_name;
1382   tree ap_field;
1383   
1384   if (!TARGET_AAPCS_BASED)
1385     return std_build_builtin_va_list ();
1386
1387   /* AAPCS \S 7.1.4 requires that va_list be a typedef for a type
1388      defined as:
1389
1390        struct __va_list 
1391        {
1392          void *__ap;
1393        };
1394
1395      The C Library ABI further reinforces this definition in \S
1396      4.1.
1397
1398      We must follow this definition exactly.  The structure tag
1399      name is visible in C++ mangled names, and thus forms a part
1400      of the ABI.  The field name may be used by people who
1401      #include <stdarg.h>.  */
1402   /* Create the type.  */
1403   va_list_type = lang_hooks.types.make_type (RECORD_TYPE);
1404   /* Give it the required name.  */
1405   va_list_name = build_decl (BUILTINS_LOCATION,
1406                              TYPE_DECL,
1407                              get_identifier ("__va_list"),
1408                              va_list_type);
1409   DECL_ARTIFICIAL (va_list_name) = 1;
1410   TYPE_NAME (va_list_type) = va_list_name;
1411   TYPE_STUB_DECL (va_list_type) = va_list_name;
1412   /* Create the __ap field.  */
1413   ap_field = build_decl (BUILTINS_LOCATION,
1414                          FIELD_DECL, 
1415                          get_identifier ("__ap"),
1416                          ptr_type_node);
1417   DECL_ARTIFICIAL (ap_field) = 1;
1418   DECL_FIELD_CONTEXT (ap_field) = va_list_type;
1419   TYPE_FIELDS (va_list_type) = ap_field;
1420   /* Compute its layout.  */
1421   layout_type (va_list_type);
1422
1423   return va_list_type;
1424 }
1425
1426 /* Return an expression of type "void *" pointing to the next
1427    available argument in a variable-argument list.  VALIST is the
1428    user-level va_list object, of type __builtin_va_list.  */
1429 static tree
1430 arm_extract_valist_ptr (tree valist)
1431 {
1432   if (TREE_TYPE (valist) == error_mark_node)
1433     return error_mark_node;
1434
1435   /* On an AAPCS target, the pointer is stored within "struct
1436      va_list".  */
1437   if (TARGET_AAPCS_BASED)
1438     {
1439       tree ap_field = TYPE_FIELDS (TREE_TYPE (valist));
1440       valist = build3 (COMPONENT_REF, TREE_TYPE (ap_field), 
1441                        valist, ap_field, NULL_TREE);
1442     }
1443
1444   return valist;
1445 }
1446
1447 /* Implement TARGET_EXPAND_BUILTIN_VA_START.  */
1448 static void
1449 arm_expand_builtin_va_start (tree valist, rtx nextarg)
1450 {
1451   valist = arm_extract_valist_ptr (valist);
1452   std_expand_builtin_va_start (valist, nextarg);
1453 }
1454
1455 /* Implement TARGET_GIMPLIFY_VA_ARG_EXPR.  */
1456 static tree
1457 arm_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p, 
1458                           gimple_seq *post_p)
1459 {
1460   valist = arm_extract_valist_ptr (valist);
1461   return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
1462 }
1463
1464 /* Fix up any incompatible options that the user has specified.  */
1465 static void
1466 arm_option_override (void)
1467 {
1468   if (global_options_set.x_arm_arch_option)
1469     arm_selected_arch = &all_architectures[arm_arch_option];
1470
1471   if (global_options_set.x_arm_cpu_option)
1472     arm_selected_cpu = &all_cores[(int) arm_cpu_option];
1473
1474   if (global_options_set.x_arm_tune_option)
1475     arm_selected_tune = &all_cores[(int) arm_tune_option];
1476
1477 #ifdef SUBTARGET_OVERRIDE_OPTIONS
1478   SUBTARGET_OVERRIDE_OPTIONS;
1479 #endif
1480
1481   if (arm_selected_arch)
1482     {
1483       if (arm_selected_cpu)
1484         {
1485           /* Check for conflict between mcpu and march.  */
1486           if ((arm_selected_cpu->flags ^ arm_selected_arch->flags) & ~FL_TUNE)
1487             {
1488               warning (0, "switch -mcpu=%s conflicts with -march=%s switch",
1489                        arm_selected_cpu->name, arm_selected_arch->name);
1490               /* -march wins for code generation.
1491                  -mcpu wins for default tuning.  */
1492               if (!arm_selected_tune)
1493                 arm_selected_tune = arm_selected_cpu;
1494
1495               arm_selected_cpu = arm_selected_arch;
1496             }
1497           else
1498             /* -mcpu wins.  */
1499             arm_selected_arch = NULL;
1500         }
1501       else
1502         /* Pick a CPU based on the architecture.  */
1503         arm_selected_cpu = arm_selected_arch;
1504     }
1505
1506   /* If the user did not specify a processor, choose one for them.  */
1507   if (!arm_selected_cpu)
1508     {
1509       const struct processors * sel;
1510       unsigned int        sought;
1511
1512       arm_selected_cpu = &all_cores[TARGET_CPU_DEFAULT];
1513       if (!arm_selected_cpu->name)
1514         {
1515 #ifdef SUBTARGET_CPU_DEFAULT
1516           /* Use the subtarget default CPU if none was specified by
1517              configure.  */
1518           arm_selected_cpu = &all_cores[SUBTARGET_CPU_DEFAULT];
1519 #endif
1520           /* Default to ARM6.  */
1521           if (!arm_selected_cpu->name)
1522             arm_selected_cpu = &all_cores[arm6];
1523         }
1524
1525       sel = arm_selected_cpu;
1526       insn_flags = sel->flags;
1527
1528       /* Now check to see if the user has specified some command line
1529          switch that require certain abilities from the cpu.  */
1530       sought = 0;
1531
1532       if (TARGET_INTERWORK || TARGET_THUMB)
1533         {
1534           sought |= (FL_THUMB | FL_MODE32);
1535
1536           /* There are no ARM processors that support both APCS-26 and
1537              interworking.  Therefore we force FL_MODE26 to be removed
1538              from insn_flags here (if it was set), so that the search
1539              below will always be able to find a compatible processor.  */
1540           insn_flags &= ~FL_MODE26;
1541         }
1542
1543       if (sought != 0 && ((sought & insn_flags) != sought))
1544         {
1545           /* Try to locate a CPU type that supports all of the abilities
1546              of the default CPU, plus the extra abilities requested by
1547              the user.  */
1548           for (sel = all_cores; sel->name != NULL; sel++)
1549             if ((sel->flags & sought) == (sought | insn_flags))
1550               break;
1551
1552           if (sel->name == NULL)
1553             {
1554               unsigned current_bit_count = 0;
1555               const struct processors * best_fit = NULL;
1556
1557               /* Ideally we would like to issue an error message here
1558                  saying that it was not possible to find a CPU compatible
1559                  with the default CPU, but which also supports the command
1560                  line options specified by the programmer, and so they
1561                  ought to use the -mcpu=<name> command line option to
1562                  override the default CPU type.
1563
1564                  If we cannot find a cpu that has both the
1565                  characteristics of the default cpu and the given
1566                  command line options we scan the array again looking
1567                  for a best match.  */
1568               for (sel = all_cores; sel->name != NULL; sel++)
1569                 if ((sel->flags & sought) == sought)
1570                   {
1571                     unsigned count;
1572
1573                     count = bit_count (sel->flags & insn_flags);
1574
1575                     if (count >= current_bit_count)
1576                       {
1577                         best_fit = sel;
1578                         current_bit_count = count;
1579                       }
1580                   }
1581
1582               gcc_assert (best_fit);
1583               sel = best_fit;
1584             }
1585
1586           arm_selected_cpu = sel;
1587         }
1588     }
1589
1590   gcc_assert (arm_selected_cpu);
1591   /* The selected cpu may be an architecture, so lookup tuning by core ID.  */
1592   if (!arm_selected_tune)
1593     arm_selected_tune = &all_cores[arm_selected_cpu->core];
1594
1595   sprintf (arm_arch_name, "__ARM_ARCH_%s__", arm_selected_cpu->arch);
1596   insn_flags = arm_selected_cpu->flags;
1597
1598   arm_tune = arm_selected_tune->core;
1599   tune_flags = arm_selected_tune->flags;
1600   current_tune = arm_selected_tune->tune;
1601
1602   /* Make sure that the processor choice does not conflict with any of the
1603      other command line choices.  */
1604   if (TARGET_ARM && !(insn_flags & FL_NOTM))
1605     error ("target CPU does not support ARM mode");
1606
1607   /* BPABI targets use linker tricks to allow interworking on cores
1608      without thumb support.  */
1609   if (TARGET_INTERWORK && !((insn_flags & FL_THUMB) || TARGET_BPABI))
1610     {
1611       warning (0, "target CPU does not support interworking" );
1612       target_flags &= ~MASK_INTERWORK;
1613     }
1614
1615   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1616     {
1617       warning (0, "target CPU does not support THUMB instructions");
1618       target_flags &= ~MASK_THUMB;
1619     }
1620
1621   if (TARGET_APCS_FRAME && TARGET_THUMB)
1622     {
1623       /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1624       target_flags &= ~MASK_APCS_FRAME;
1625     }
1626
1627   /* Callee super interworking implies thumb interworking.  Adding
1628      this to the flags here simplifies the logic elsewhere.  */
1629   if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
1630     target_flags |= MASK_INTERWORK;
1631
1632   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1633      from here where no function is being compiled currently.  */
1634   if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1635     warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1636
1637   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1638     warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1639
1640   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1641     {
1642       warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1643       target_flags |= MASK_APCS_FRAME;
1644     }
1645
1646   if (TARGET_POKE_FUNCTION_NAME)
1647     target_flags |= MASK_APCS_FRAME;
1648
1649   if (TARGET_APCS_REENT && flag_pic)
1650     error ("-fpic and -mapcs-reent are incompatible");
1651
1652   if (TARGET_APCS_REENT)
1653     warning (0, "APCS reentrant code not supported.  Ignored");
1654
1655   /* If this target is normally configured to use APCS frames, warn if they
1656      are turned off and debugging is turned on.  */
1657   if (TARGET_ARM
1658       && write_symbols != NO_DEBUG
1659       && !TARGET_APCS_FRAME
1660       && (TARGET_DEFAULT & MASK_APCS_FRAME))
1661     warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1662
1663   if (TARGET_APCS_FLOAT)
1664     warning (0, "passing floating point arguments in fp regs not yet supported");
1665
1666   if (TARGET_LITTLE_WORDS)
1667     warning (OPT_Wdeprecated, "%<mwords-little-endian%> is deprecated and "
1668              "will be removed in a future release");
1669
1670   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
1671   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1672   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1673   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1674   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1675   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1676   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1677   arm_arch6k = (insn_flags & FL_ARCH6K) != 0;
1678   arm_arch_notm = (insn_flags & FL_NOTM) != 0;
1679   arm_arch7 = (insn_flags & FL_ARCH7) != 0;
1680   arm_arch7em = (insn_flags & FL_ARCH7EM) != 0;
1681   arm_arch_thumb2 = (insn_flags & FL_THUMB2) != 0;
1682   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1683   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1684
1685   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1686   arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1687   thumb_code = TARGET_ARM == 0;
1688   thumb1_code = TARGET_THUMB1 != 0;
1689   arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1690   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1691   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1692   arm_arch_thumb_hwdiv = (insn_flags & FL_THUMB_DIV) != 0;
1693   arm_arch_arm_hwdiv = (insn_flags & FL_ARM_DIV) != 0;
1694   arm_tune_cortex_a9 = (arm_tune == cortexa9) != 0;
1695
1696   /* If we are not using the default (ARM mode) section anchor offset
1697      ranges, then set the correct ranges now.  */
1698   if (TARGET_THUMB1)
1699     {
1700       /* Thumb-1 LDR instructions cannot have negative offsets.
1701          Permissible positive offset ranges are 5-bit (for byte loads),
1702          6-bit (for halfword loads), or 7-bit (for word loads).
1703          Empirical results suggest a 7-bit anchor range gives the best
1704          overall code size.  */
1705       targetm.min_anchor_offset = 0;
1706       targetm.max_anchor_offset = 127;
1707     }
1708   else if (TARGET_THUMB2)
1709     {
1710       /* The minimum is set such that the total size of the block
1711          for a particular anchor is 248 + 1 + 4095 bytes, which is
1712          divisible by eight, ensuring natural spacing of anchors.  */
1713       targetm.min_anchor_offset = -248;
1714       targetm.max_anchor_offset = 4095;
1715     }
1716
1717   /* V5 code we generate is completely interworking capable, so we turn off
1718      TARGET_INTERWORK here to avoid many tests later on.  */
1719
1720   /* XXX However, we must pass the right pre-processor defines to CPP
1721      or GLD can get confused.  This is a hack.  */
1722   if (TARGET_INTERWORK)
1723     arm_cpp_interwork = 1;
1724
1725   if (arm_arch5)
1726     target_flags &= ~MASK_INTERWORK;
1727
1728   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1729     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1730
1731   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1732     error ("iwmmxt abi requires an iwmmxt capable cpu");
1733
1734   if (!global_options_set.x_arm_fpu_index)
1735     {
1736       const char *target_fpu_name;
1737       bool ok;
1738
1739 #ifdef FPUTYPE_DEFAULT
1740       target_fpu_name = FPUTYPE_DEFAULT;
1741 #else
1742       if (arm_arch_cirrus)
1743         target_fpu_name = "maverick";
1744       else
1745         target_fpu_name = "fpe2";
1746 #endif
1747
1748       ok = opt_enum_arg_to_value (OPT_mfpu_, target_fpu_name, &arm_fpu_index,
1749                                   CL_TARGET);
1750       gcc_assert (ok);
1751     }
1752
1753   arm_fpu_desc = &all_fpus[arm_fpu_index];
1754
1755   switch (arm_fpu_desc->model)
1756     {
1757     case ARM_FP_MODEL_FPA:
1758       if (arm_fpu_desc->rev == 2)
1759         arm_fpu_attr = FPU_FPE2;
1760       else if (arm_fpu_desc->rev == 3)
1761         arm_fpu_attr = FPU_FPE3;
1762       else
1763         arm_fpu_attr = FPU_FPA;
1764       break;
1765
1766     case ARM_FP_MODEL_MAVERICK:
1767       arm_fpu_attr = FPU_MAVERICK;
1768       break;
1769
1770     case ARM_FP_MODEL_VFP:
1771       arm_fpu_attr = FPU_VFP;
1772       break;
1773
1774     default:
1775       gcc_unreachable();
1776     }
1777
1778   if (TARGET_AAPCS_BASED
1779       && (arm_fpu_desc->model == ARM_FP_MODEL_FPA))
1780     error ("FPA is unsupported in the AAPCS");
1781
1782   if (TARGET_AAPCS_BASED)
1783     {
1784       if (TARGET_CALLER_INTERWORKING)
1785         error ("AAPCS does not support -mcaller-super-interworking");
1786       else
1787         if (TARGET_CALLEE_INTERWORKING)
1788           error ("AAPCS does not support -mcallee-super-interworking");
1789     }
1790
1791   /* FPA and iWMMXt are incompatible because the insn encodings overlap.
1792      VFP and iWMMXt can theoretically coexist, but it's unlikely such silicon
1793      will ever exist.  GCC makes no attempt to support this combination.  */
1794   if (TARGET_IWMMXT && !TARGET_SOFT_FLOAT)
1795     sorry ("iWMMXt and hardware floating point");
1796
1797   /* ??? iWMMXt insn patterns need auditing for Thumb-2.  */
1798   if (TARGET_THUMB2 && TARGET_IWMMXT)
1799     sorry ("Thumb-2 iWMMXt");
1800
1801   /* __fp16 support currently assumes the core has ldrh.  */
1802   if (!arm_arch4 && arm_fp16_format != ARM_FP16_FORMAT_NONE)
1803     sorry ("__fp16 and no ldrh");
1804
1805   /* If soft-float is specified then don't use FPU.  */
1806   if (TARGET_SOFT_FLOAT)
1807     arm_fpu_attr = FPU_NONE;
1808
1809   if (TARGET_AAPCS_BASED)
1810     {
1811       if (arm_abi == ARM_ABI_IWMMXT)
1812         arm_pcs_default = ARM_PCS_AAPCS_IWMMXT;
1813       else if (arm_float_abi == ARM_FLOAT_ABI_HARD
1814                && TARGET_HARD_FLOAT
1815                && TARGET_VFP)
1816         arm_pcs_default = ARM_PCS_AAPCS_VFP;
1817       else
1818         arm_pcs_default = ARM_PCS_AAPCS;
1819     }
1820   else
1821     {
1822       if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1823         sorry ("-mfloat-abi=hard and VFP");
1824
1825       if (arm_abi == ARM_ABI_APCS)
1826         arm_pcs_default = ARM_PCS_APCS;
1827       else
1828         arm_pcs_default = ARM_PCS_ATPCS;
1829     }
1830
1831   /* For arm2/3 there is no need to do any scheduling if there is only
1832      a floating point emulator, or we are doing software floating-point.  */
1833   if ((TARGET_SOFT_FLOAT
1834        || (TARGET_FPA && arm_fpu_desc->rev))
1835       && (tune_flags & FL_MODE32) == 0)
1836     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1837
1838   /* Use the cp15 method if it is available.  */
1839   if (target_thread_pointer == TP_AUTO)
1840     {
1841       if (arm_arch6k && !TARGET_THUMB1)
1842         target_thread_pointer = TP_CP15;
1843       else
1844         target_thread_pointer = TP_SOFT;
1845     }
1846
1847   if (TARGET_HARD_TP && TARGET_THUMB1)
1848     error ("can not use -mtp=cp15 with 16-bit Thumb");
1849
1850   /* Override the default structure alignment for AAPCS ABI.  */
1851   if (!global_options_set.x_arm_structure_size_boundary)
1852     {
1853       if (TARGET_AAPCS_BASED)
1854         arm_structure_size_boundary = 8;
1855     }
1856   else
1857     {
1858       if (arm_structure_size_boundary != 8
1859           && arm_structure_size_boundary != 32
1860           && !(ARM_DOUBLEWORD_ALIGN && arm_structure_size_boundary == 64))
1861         {
1862           if (ARM_DOUBLEWORD_ALIGN)
1863             warning (0,
1864                      "structure size boundary can only be set to 8, 32 or 64");
1865           else
1866             warning (0, "structure size boundary can only be set to 8 or 32");
1867           arm_structure_size_boundary
1868             = (TARGET_AAPCS_BASED ? 8 : DEFAULT_STRUCTURE_SIZE_BOUNDARY);
1869         }
1870     }
1871
1872   if (!TARGET_ARM && TARGET_VXWORKS_RTP && flag_pic)
1873     {
1874       error ("RTP PIC is incompatible with Thumb");
1875       flag_pic = 0;
1876     }
1877
1878   /* If stack checking is disabled, we can use r10 as the PIC register,
1879      which keeps r9 available.  The EABI specifies r9 as the PIC register.  */
1880   if (flag_pic && TARGET_SINGLE_PIC_BASE)
1881     {
1882       if (TARGET_VXWORKS_RTP)
1883         warning (0, "RTP PIC is incompatible with -msingle-pic-base");
1884       arm_pic_register = (TARGET_APCS_STACK || TARGET_AAPCS_BASED) ? 9 : 10;
1885     }
1886
1887   if (flag_pic && TARGET_VXWORKS_RTP)
1888     arm_pic_register = 9;
1889
1890   if (arm_pic_register_string != NULL)
1891     {
1892       int pic_register = decode_reg_name (arm_pic_register_string);
1893
1894       if (!flag_pic)
1895         warning (0, "-mpic-register= is useless without -fpic");
1896
1897       /* Prevent the user from choosing an obviously stupid PIC register.  */
1898       else if (pic_register < 0 || call_used_regs[pic_register]
1899                || pic_register == HARD_FRAME_POINTER_REGNUM
1900                || pic_register == STACK_POINTER_REGNUM
1901                || pic_register >= PC_REGNUM
1902                || (TARGET_VXWORKS_RTP
1903                    && (unsigned int) pic_register != arm_pic_register))
1904         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1905       else
1906         arm_pic_register = pic_register;
1907     }
1908
1909   /* Enable -mfix-cortex-m3-ldrd by default for Cortex-M3 cores.  */
1910   if (fix_cm3_ldrd == 2)
1911     {
1912       if (arm_selected_cpu->core == cortexm3)
1913         fix_cm3_ldrd = 1;
1914       else
1915         fix_cm3_ldrd = 0;
1916     }
1917
1918   /* Enable -munaligned-access by default for
1919      - all ARMv6 architecture-based processors
1920      - ARMv7-A, ARMv7-R, and ARMv7-M architecture-based processors.
1921
1922      Disable -munaligned-access by default for
1923      - all pre-ARMv6 architecture-based processors
1924      - ARMv6-M architecture-based processors.  */
1925
1926   if (unaligned_access == 2)
1927     {
1928       if (arm_arch6 && (arm_arch_notm || arm_arch7))
1929         unaligned_access = 1;
1930       else
1931         unaligned_access = 0;
1932     }
1933   else if (unaligned_access == 1
1934            && !(arm_arch6 && (arm_arch_notm || arm_arch7)))
1935     {
1936       warning (0, "target CPU does not support unaligned accesses");
1937       unaligned_access = 0;
1938     }
1939
1940   if (TARGET_THUMB1 && flag_schedule_insns)
1941     {
1942       /* Don't warn since it's on by default in -O2.  */
1943       flag_schedule_insns = 0;
1944     }
1945
1946   if (optimize_size)
1947     {
1948       /* If optimizing for size, bump the number of instructions that we
1949          are prepared to conditionally execute (even on a StrongARM).  */
1950       max_insns_skipped = 6;
1951     }
1952   else
1953     max_insns_skipped = current_tune->max_insns_skipped;
1954
1955   /* Hot/Cold partitioning is not currently supported, since we can't
1956      handle literal pool placement in that case.  */
1957   if (flag_reorder_blocks_and_partition)
1958     {
1959       inform (input_location,
1960               "-freorder-blocks-and-partition not supported on this architecture");
1961       flag_reorder_blocks_and_partition = 0;
1962       flag_reorder_blocks = 1;
1963     }
1964
1965   if (flag_pic)
1966     /* Hoisting PIC address calculations more aggressively provides a small,
1967        but measurable, size reduction for PIC code.  Therefore, we decrease
1968        the bar for unrestricted expression hoisting to the cost of PIC address
1969        calculation, which is 2 instructions.  */
1970     maybe_set_param_value (PARAM_GCSE_UNRESTRICTED_COST, 2,
1971                            global_options.x_param_values,
1972                            global_options_set.x_param_values);
1973
1974   /* ARM EABI defaults to strict volatile bitfields.  */
1975   if (TARGET_AAPCS_BASED && flag_strict_volatile_bitfields < 0)
1976     flag_strict_volatile_bitfields = 1;
1977
1978   /* Enable sw prefetching at -O3 for CPUS that have prefetch, and we have deemed
1979      it beneficial (signified by setting num_prefetch_slots to 1 or more.)  */
1980   if (flag_prefetch_loop_arrays < 0
1981       && HAVE_prefetch
1982       && optimize >= 3
1983       && current_tune->num_prefetch_slots > 0)
1984     flag_prefetch_loop_arrays = 1;
1985
1986   /* Set up parameters to be used in prefetching algorithm.  Do not override the
1987      defaults unless we are tuning for a core we have researched values for.  */
1988   if (current_tune->num_prefetch_slots > 0)
1989     maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
1990                            current_tune->num_prefetch_slots,
1991                            global_options.x_param_values,
1992                            global_options_set.x_param_values);
1993   if (current_tune->l1_cache_line_size >= 0)
1994     maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
1995                            current_tune->l1_cache_line_size,
1996                            global_options.x_param_values,
1997                            global_options_set.x_param_values);
1998   if (current_tune->l1_cache_size >= 0)
1999     maybe_set_param_value (PARAM_L1_CACHE_SIZE,
2000                            current_tune->l1_cache_size,
2001                            global_options.x_param_values,
2002                            global_options_set.x_param_values);
2003
2004   /* Register global variables with the garbage collector.  */
2005   arm_add_gc_roots ();
2006 }
2007
2008 static void
2009 arm_add_gc_roots (void)
2010 {
2011   gcc_obstack_init(&minipool_obstack);
2012   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
2013 }
2014 \f
2015 /* A table of known ARM exception types.
2016    For use with the interrupt function attribute.  */
2017
2018 typedef struct
2019 {
2020   const char *const arg;
2021   const unsigned long return_value;
2022 }
2023 isr_attribute_arg;
2024
2025 static const isr_attribute_arg isr_attribute_args [] =
2026 {
2027   { "IRQ",   ARM_FT_ISR },
2028   { "irq",   ARM_FT_ISR },
2029   { "FIQ",   ARM_FT_FIQ },
2030   { "fiq",   ARM_FT_FIQ },
2031   { "ABORT", ARM_FT_ISR },
2032   { "abort", ARM_FT_ISR },
2033   { "ABORT", ARM_FT_ISR },
2034   { "abort", ARM_FT_ISR },
2035   { "UNDEF", ARM_FT_EXCEPTION },
2036   { "undef", ARM_FT_EXCEPTION },
2037   { "SWI",   ARM_FT_EXCEPTION },
2038   { "swi",   ARM_FT_EXCEPTION },
2039   { NULL,    ARM_FT_NORMAL }
2040 };
2041
2042 /* Returns the (interrupt) function type of the current
2043    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
2044
2045 static unsigned long
2046 arm_isr_value (tree argument)
2047 {
2048   const isr_attribute_arg * ptr;
2049   const char *              arg;
2050
2051   if (!arm_arch_notm)
2052     return ARM_FT_NORMAL | ARM_FT_STACKALIGN;
2053
2054   /* No argument - default to IRQ.  */
2055   if (argument == NULL_TREE)
2056     return ARM_FT_ISR;
2057
2058   /* Get the value of the argument.  */
2059   if (TREE_VALUE (argument) == NULL_TREE
2060       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
2061     return ARM_FT_UNKNOWN;
2062
2063   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
2064
2065   /* Check it against the list of known arguments.  */
2066   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
2067     if (streq (arg, ptr->arg))
2068       return ptr->return_value;
2069
2070   /* An unrecognized interrupt type.  */
2071   return ARM_FT_UNKNOWN;
2072 }
2073
2074 /* Computes the type of the current function.  */
2075
2076 static unsigned long
2077 arm_compute_func_type (void)
2078 {
2079   unsigned long type = ARM_FT_UNKNOWN;
2080   tree a;
2081   tree attr;
2082
2083   gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
2084
2085   /* Decide if the current function is volatile.  Such functions
2086      never return, and many memory cycles can be saved by not storing
2087      register values that will never be needed again.  This optimization
2088      was added to speed up context switching in a kernel application.  */
2089   if (optimize > 0
2090       && (TREE_NOTHROW (current_function_decl)
2091           || !(flag_unwind_tables
2092                || (flag_exceptions
2093                    && arm_except_unwind_info (&global_options) != UI_SJLJ)))
2094       && TREE_THIS_VOLATILE (current_function_decl))
2095     type |= ARM_FT_VOLATILE;
2096
2097   if (cfun->static_chain_decl != NULL)
2098     type |= ARM_FT_NESTED;
2099
2100   attr = DECL_ATTRIBUTES (current_function_decl);
2101
2102   a = lookup_attribute ("naked", attr);
2103   if (a != NULL_TREE)
2104     type |= ARM_FT_NAKED;
2105
2106   a = lookup_attribute ("isr", attr);
2107   if (a == NULL_TREE)
2108     a = lookup_attribute ("interrupt", attr);
2109
2110   if (a == NULL_TREE)
2111     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
2112   else
2113     type |= arm_isr_value (TREE_VALUE (a));
2114
2115   return type;
2116 }
2117
2118 /* Returns the type of the current function.  */
2119
2120 unsigned long
2121 arm_current_func_type (void)
2122 {
2123   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
2124     cfun->machine->func_type = arm_compute_func_type ();
2125
2126   return cfun->machine->func_type;
2127 }
2128
2129 bool
2130 arm_allocate_stack_slots_for_args (void)
2131 {
2132   /* Naked functions should not allocate stack slots for arguments.  */
2133   return !IS_NAKED (arm_current_func_type ());
2134 }
2135
2136 \f
2137 /* Output assembler code for a block containing the constant parts
2138    of a trampoline, leaving space for the variable parts.
2139
2140    On the ARM, (if r8 is the static chain regnum, and remembering that
2141    referencing pc adds an offset of 8) the trampoline looks like:
2142            ldr          r8, [pc, #0]
2143            ldr          pc, [pc]
2144            .word        static chain value
2145            .word        function's address
2146    XXX FIXME: When the trampoline returns, r8 will be clobbered.  */
2147
2148 static void
2149 arm_asm_trampoline_template (FILE *f)
2150 {
2151   if (TARGET_ARM)
2152     {
2153       asm_fprintf (f, "\tldr\t%r, [%r, #0]\n", STATIC_CHAIN_REGNUM, PC_REGNUM);
2154       asm_fprintf (f, "\tldr\t%r, [%r, #0]\n", PC_REGNUM, PC_REGNUM);
2155     }
2156   else if (TARGET_THUMB2)
2157     {
2158       /* The Thumb-2 trampoline is similar to the arm implementation.
2159          Unlike 16-bit Thumb, we enter the stub in thumb mode.  */
2160       asm_fprintf (f, "\tldr.w\t%r, [%r, #4]\n",
2161                    STATIC_CHAIN_REGNUM, PC_REGNUM);
2162       asm_fprintf (f, "\tldr.w\t%r, [%r, #4]\n", PC_REGNUM, PC_REGNUM);
2163     }
2164   else
2165     {
2166       ASM_OUTPUT_ALIGN (f, 2);
2167       fprintf (f, "\t.code\t16\n");
2168       fprintf (f, ".Ltrampoline_start:\n");
2169       asm_fprintf (f, "\tpush\t{r0, r1}\n");
2170       asm_fprintf (f, "\tldr\tr0, [%r, #8]\n", PC_REGNUM);
2171       asm_fprintf (f, "\tmov\t%r, r0\n", STATIC_CHAIN_REGNUM);
2172       asm_fprintf (f, "\tldr\tr0, [%r, #8]\n", PC_REGNUM);
2173       asm_fprintf (f, "\tstr\tr0, [%r, #4]\n", SP_REGNUM);
2174       asm_fprintf (f, "\tpop\t{r0, %r}\n", PC_REGNUM);
2175     }
2176   assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2177   assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2178 }
2179
2180 /* Emit RTL insns to initialize the variable parts of a trampoline.  */
2181
2182 static void
2183 arm_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
2184 {
2185   rtx fnaddr, mem, a_tramp;
2186
2187   emit_block_move (m_tramp, assemble_trampoline_template (),
2188                    GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
2189
2190   mem = adjust_address (m_tramp, SImode, TARGET_32BIT ? 8 : 12);
2191   emit_move_insn (mem, chain_value);
2192
2193   mem = adjust_address (m_tramp, SImode, TARGET_32BIT ? 12 : 16);
2194   fnaddr = XEXP (DECL_RTL (fndecl), 0);
2195   emit_move_insn (mem, fnaddr);
2196
2197   a_tramp = XEXP (m_tramp, 0);
2198   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__clear_cache"),
2199                      LCT_NORMAL, VOIDmode, 2, a_tramp, Pmode,
2200                      plus_constant (a_tramp, TRAMPOLINE_SIZE), Pmode);
2201 }
2202
2203 /* Thumb trampolines should be entered in thumb mode, so set
2204    the bottom bit of the address.  */
2205
2206 static rtx
2207 arm_trampoline_adjust_address (rtx addr)
2208 {
2209   if (TARGET_THUMB)
2210     addr = expand_simple_binop (Pmode, IOR, addr, const1_rtx,
2211                                 NULL, 0, OPTAB_LIB_WIDEN);
2212   return addr;
2213 }
2214 \f
2215 /* Return 1 if it is possible to return using a single instruction.
2216    If SIBLING is non-null, this is a test for a return before a sibling
2217    call.  SIBLING is the call insn, so we can examine its register usage.  */
2218
2219 int
2220 use_return_insn (int iscond, rtx sibling)
2221 {
2222   int regno;
2223   unsigned int func_type;
2224   unsigned long saved_int_regs;
2225   unsigned HOST_WIDE_INT stack_adjust;
2226   arm_stack_offsets *offsets;
2227
2228   /* Never use a return instruction before reload has run.  */
2229   if (!reload_completed)
2230     return 0;
2231
2232   func_type = arm_current_func_type ();
2233
2234   /* Naked, volatile and stack alignment functions need special
2235      consideration.  */
2236   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED | ARM_FT_STACKALIGN))
2237     return 0;
2238
2239   /* So do interrupt functions that use the frame pointer and Thumb
2240      interrupt functions.  */
2241   if (IS_INTERRUPT (func_type) && (frame_pointer_needed || TARGET_THUMB))
2242     return 0;
2243
2244   offsets = arm_get_frame_offsets ();
2245   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
2246
2247   /* As do variadic functions.  */
2248   if (crtl->args.pretend_args_size
2249       || cfun->machine->uses_anonymous_args
2250       /* Or if the function calls __builtin_eh_return () */
2251       || crtl->calls_eh_return
2252       /* Or if the function calls alloca */
2253       || cfun->calls_alloca
2254       /* Or if there is a stack adjustment.  However, if the stack pointer
2255          is saved on the stack, we can use a pre-incrementing stack load.  */
2256       || !(stack_adjust == 0 || (TARGET_APCS_FRAME && frame_pointer_needed
2257                                  && stack_adjust == 4)))
2258     return 0;
2259
2260   saved_int_regs = offsets->saved_regs_mask;
2261
2262   /* Unfortunately, the insn
2263
2264        ldmib sp, {..., sp, ...}
2265
2266      triggers a bug on most SA-110 based devices, such that the stack
2267      pointer won't be correctly restored if the instruction takes a
2268      page fault.  We work around this problem by popping r3 along with
2269      the other registers, since that is never slower than executing
2270      another instruction.
2271
2272      We test for !arm_arch5 here, because code for any architecture
2273      less than this could potentially be run on one of the buggy
2274      chips.  */
2275   if (stack_adjust == 4 && !arm_arch5 && TARGET_ARM)
2276     {
2277       /* Validate that r3 is a call-clobbered register (always true in
2278          the default abi) ...  */
2279       if (!call_used_regs[3])
2280         return 0;
2281
2282       /* ... that it isn't being used for a return value ... */
2283       if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
2284         return 0;
2285
2286       /* ... or for a tail-call argument ...  */
2287       if (sibling)
2288         {
2289           gcc_assert (GET_CODE (sibling) == CALL_INSN);
2290
2291           if (find_regno_fusage (sibling, USE, 3))
2292             return 0;
2293         }
2294
2295       /* ... and that there are no call-saved registers in r0-r2
2296          (always true in the default ABI).  */
2297       if (saved_int_regs & 0x7)
2298         return 0;
2299     }
2300
2301   /* Can't be done if interworking with Thumb, and any registers have been
2302      stacked.  */
2303   if (TARGET_INTERWORK && saved_int_regs != 0 && !IS_INTERRUPT(func_type))
2304     return 0;
2305
2306   /* On StrongARM, conditional returns are expensive if they aren't
2307      taken and multiple registers have been stacked.  */
2308   if (iscond && arm_tune_strongarm)
2309     {
2310       /* Conditional return when just the LR is stored is a simple
2311          conditional-load instruction, that's not expensive.  */
2312       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
2313         return 0;
2314
2315       if (flag_pic 
2316           && arm_pic_register != INVALID_REGNUM
2317           && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
2318         return 0;
2319     }
2320
2321   /* If there are saved registers but the LR isn't saved, then we need
2322      two instructions for the return.  */
2323   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
2324     return 0;
2325
2326   /* Can't be done if any of the FPA regs are pushed,
2327      since this also requires an insn.  */
2328   if (TARGET_HARD_FLOAT && TARGET_FPA)
2329     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
2330       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
2331         return 0;
2332
2333   /* Likewise VFP regs.  */
2334   if (TARGET_HARD_FLOAT && TARGET_VFP)
2335     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
2336       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
2337         return 0;
2338
2339   if (TARGET_REALLY_IWMMXT)
2340     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
2341       if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
2342         return 0;
2343
2344   return 1;
2345 }
2346
2347 /* Return TRUE if int I is a valid immediate ARM constant.  */
2348
2349 int
2350 const_ok_for_arm (HOST_WIDE_INT i)
2351 {
2352   int lowbit;
2353
2354   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
2355      be all zero, or all one.  */
2356   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
2357       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
2358           != ((~(unsigned HOST_WIDE_INT) 0)
2359               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
2360     return FALSE;
2361
2362   i &= (unsigned HOST_WIDE_INT) 0xffffffff;
2363
2364   /* Fast return for 0 and small values.  We must do this for zero, since
2365      the code below can't handle that one case.  */
2366   if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
2367     return TRUE;
2368
2369   /* Get the number of trailing zeros.  */
2370   lowbit = ffs((int) i) - 1;
2371   
2372   /* Only even shifts are allowed in ARM mode so round down to the
2373      nearest even number.  */
2374   if (TARGET_ARM)
2375     lowbit &= ~1;
2376
2377   if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
2378     return TRUE;
2379
2380   if (TARGET_ARM)
2381     {
2382       /* Allow rotated constants in ARM mode.  */
2383       if (lowbit <= 4
2384            && ((i & ~0xc000003f) == 0
2385                || (i & ~0xf000000f) == 0
2386                || (i & ~0xfc000003) == 0))
2387         return TRUE;
2388     }
2389   else
2390     {
2391       HOST_WIDE_INT v;
2392
2393       /* Allow repeated patterns 0x00XY00XY or 0xXYXYXYXY.  */
2394       v = i & 0xff;
2395       v |= v << 16;
2396       if (i == v || i == (v | (v << 8)))
2397         return TRUE;
2398
2399       /* Allow repeated pattern 0xXY00XY00.  */
2400       v = i & 0xff00;
2401       v |= v << 16;
2402       if (i == v)
2403         return TRUE;
2404     }
2405
2406   return FALSE;
2407 }
2408
2409 /* Return true if I is a valid constant for the operation CODE.  */
2410 int
2411 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
2412 {
2413   if (const_ok_for_arm (i))
2414     return 1;
2415
2416   switch (code)
2417     {
2418     case SET:
2419       /* See if we can use movw.  */
2420       if (arm_arch_thumb2 && (i & 0xffff0000) == 0)
2421         return 1;
2422       else
2423         /* Otherwise, try mvn.  */
2424         return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2425
2426     case PLUS:
2427       /* See if we can use addw or subw.  */
2428       if (TARGET_THUMB2
2429           && ((i & 0xfffff000) == 0
2430               || ((-i) & 0xfffff000) == 0))
2431         return 1;
2432       /* else fall through.  */
2433
2434     case COMPARE:
2435     case EQ:
2436     case NE:
2437     case GT:
2438     case LE:
2439     case LT:
2440     case GE:
2441     case GEU:
2442     case LTU:
2443     case GTU:
2444     case LEU:
2445     case UNORDERED:
2446     case ORDERED:
2447     case UNEQ:
2448     case UNGE:
2449     case UNLT:
2450     case UNGT:
2451     case UNLE:
2452       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
2453
2454     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
2455     case XOR:
2456       return 0;
2457
2458     case IOR:
2459       if (TARGET_THUMB2)
2460         return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2461       return 0;
2462
2463     case AND:
2464       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2465
2466     default:
2467       gcc_unreachable ();
2468     }
2469 }
2470
2471 /* Emit a sequence of insns to handle a large constant.
2472    CODE is the code of the operation required, it can be any of SET, PLUS,
2473    IOR, AND, XOR, MINUS;
2474    MODE is the mode in which the operation is being performed;
2475    VAL is the integer to operate on;
2476    SOURCE is the other operand (a register, or a null-pointer for SET);
2477    SUBTARGETS means it is safe to create scratch registers if that will
2478    either produce a simpler sequence, or we will want to cse the values.
2479    Return value is the number of insns emitted.  */
2480
2481 /* ??? Tweak this for thumb2.  */
2482 int
2483 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
2484                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
2485 {
2486   rtx cond;
2487
2488   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
2489     cond = COND_EXEC_TEST (PATTERN (insn));
2490   else
2491     cond = NULL_RTX;
2492
2493   if (subtargets || code == SET
2494       || (GET_CODE (target) == REG && GET_CODE (source) == REG
2495           && REGNO (target) != REGNO (source)))
2496     {
2497       /* After arm_reorg has been called, we can't fix up expensive
2498          constants by pushing them into memory so we must synthesize
2499          them in-line, regardless of the cost.  This is only likely to
2500          be more costly on chips that have load delay slots and we are
2501          compiling without running the scheduler (so no splitting
2502          occurred before the final instruction emission).
2503
2504          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
2505       */
2506       if (!after_arm_reorg
2507           && !cond
2508           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
2509                                 1, 0)
2510               > (arm_constant_limit (optimize_function_for_size_p (cfun))
2511                  + (code != SET))))
2512         {
2513           if (code == SET)
2514             {
2515               /* Currently SET is the only monadic value for CODE, all
2516                  the rest are diadic.  */
2517               if (TARGET_USE_MOVT)
2518                 arm_emit_movpair (target, GEN_INT (val));
2519               else
2520                 emit_set_insn (target, GEN_INT (val));
2521
2522               return 1;
2523             }
2524           else
2525             {
2526               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
2527
2528               if (TARGET_USE_MOVT)
2529                 arm_emit_movpair (temp, GEN_INT (val));
2530               else
2531                 emit_set_insn (temp, GEN_INT (val));
2532
2533               /* For MINUS, the value is subtracted from, since we never
2534                  have subtraction of a constant.  */
2535               if (code == MINUS)
2536                 emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
2537               else
2538                 emit_set_insn (target,
2539                                gen_rtx_fmt_ee (code, mode, source, temp));
2540               return 2;
2541             }
2542         }
2543     }
2544
2545   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
2546                            1);
2547 }
2548
2549 /* Return a sequence of integers, in RETURN_SEQUENCE that fit into
2550    ARM/THUMB2 immediates, and add up to VAL.
2551    Thr function return value gives the number of insns required.  */
2552 static int
2553 optimal_immediate_sequence (enum rtx_code code, unsigned HOST_WIDE_INT val,
2554                             struct four_ints *return_sequence)
2555 {
2556   int best_consecutive_zeros = 0;
2557   int i;
2558   int best_start = 0;
2559   int insns1, insns2;
2560   struct four_ints tmp_sequence;
2561
2562   /* If we aren't targetting ARM, the best place to start is always at
2563      the bottom, otherwise look more closely.  */
2564   if (TARGET_ARM)
2565     {
2566       for (i = 0; i < 32; i += 2)
2567         {
2568           int consecutive_zeros = 0;
2569
2570           if (!(val & (3 << i)))
2571             {
2572               while ((i < 32) && !(val & (3 << i)))
2573                 {
2574                   consecutive_zeros += 2;
2575                   i += 2;
2576                 }
2577               if (consecutive_zeros > best_consecutive_zeros)
2578                 {
2579                   best_consecutive_zeros = consecutive_zeros;
2580                   best_start = i - consecutive_zeros;
2581                 }
2582               i -= 2;
2583             }
2584         }
2585     }
2586
2587   /* So long as it won't require any more insns to do so, it's
2588      desirable to emit a small constant (in bits 0...9) in the last
2589      insn.  This way there is more chance that it can be combined with
2590      a later addressing insn to form a pre-indexed load or store
2591      operation.  Consider:
2592
2593            *((volatile int *)0xe0000100) = 1;
2594            *((volatile int *)0xe0000110) = 2;
2595
2596      We want this to wind up as:
2597
2598             mov rA, #0xe0000000
2599             mov rB, #1
2600             str rB, [rA, #0x100]
2601             mov rB, #2
2602             str rB, [rA, #0x110]
2603
2604      rather than having to synthesize both large constants from scratch.
2605
2606      Therefore, we calculate how many insns would be required to emit
2607      the constant starting from `best_start', and also starting from
2608      zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2609      yield a shorter sequence, we may as well use zero.  */
2610   insns1 = optimal_immediate_sequence_1 (code, val, return_sequence, best_start);
2611   if (best_start != 0
2612       && ((((unsigned HOST_WIDE_INT) 1) << best_start) < val))
2613     {
2614       insns2 = optimal_immediate_sequence_1 (code, val, &tmp_sequence, 0);
2615       if (insns2 <= insns1)
2616         {
2617           *return_sequence = tmp_sequence;
2618           insns1 = insns2;
2619         }
2620     }
2621
2622   return insns1;
2623 }
2624
2625 /* As for optimal_immediate_sequence, but starting at bit-position I.  */
2626 static int
2627 optimal_immediate_sequence_1 (enum rtx_code code, unsigned HOST_WIDE_INT val,
2628                              struct four_ints *return_sequence, int i)
2629 {
2630   int remainder = val & 0xffffffff;
2631   int insns = 0;
2632
2633   /* Try and find a way of doing the job in either two or three
2634      instructions.
2635      
2636      In ARM mode we can use 8-bit constants, rotated to any 2-bit aligned
2637      location.  We start at position I.  This may be the MSB, or
2638      optimial_immediate_sequence may have positioned it at the largest block 
2639      of zeros that are aligned on a 2-bit boundary. We then fill up the temps,
2640      wrapping around to the top of the word when we drop off the bottom.
2641      In the worst case this code should produce no more than four insns.
2642
2643      In Thumb2 mode, we can use 32/16-bit replicated constants, and 8-bit
2644      constants, shifted to any arbitrary location.  We should always start
2645      at the MSB.  */
2646   do
2647     {
2648       int end;
2649       unsigned int b1, b2, b3, b4;
2650       unsigned HOST_WIDE_INT result;
2651       int loc;
2652
2653       gcc_assert (insns < 4);
2654
2655       if (i <= 0)
2656         i += 32;
2657
2658       /* First, find the next normal 12/8-bit shifted/rotated immediate.  */
2659       if (remainder & ((TARGET_ARM ? (3 << (i - 2)) : (1 << (i - 1)))))
2660         {
2661           loc = i;
2662           if (i <= 12 && TARGET_THUMB2 && code == PLUS)
2663             /* We can use addw/subw for the last 12 bits.  */
2664             result = remainder;
2665           else
2666             {
2667               /* Use an 8-bit shifted/rotated immediate.  */
2668               end = i - 8;
2669               if (end < 0)
2670                 end += 32;
2671               result = remainder & ((0x0ff << end)
2672                                    | ((i < end) ? (0xff >> (32 - end))
2673                                                 : 0));
2674               i -= 8;
2675             }
2676         }
2677       else
2678         {
2679           /* Arm allows rotates by a multiple of two. Thumb-2 allows
2680              arbitrary shifts.  */
2681           i -= TARGET_ARM ? 2 : 1;
2682           continue;
2683         }
2684
2685       /* Next, see if we can do a better job with a thumb2 replicated
2686          constant.
2687        
2688          We do it this way around to catch the cases like 0x01F001E0 where
2689          two 8-bit immediates would work, but a replicated constant would
2690          make it worse.
2691        
2692          TODO: 16-bit constants that don't clear all the bits, but still win.
2693          TODO: Arithmetic splitting for set/add/sub, rather than bitwise.  */
2694       if (TARGET_THUMB2)
2695         {
2696           b1 = (remainder & 0xff000000) >> 24;
2697           b2 = (remainder & 0x00ff0000) >> 16;
2698           b3 = (remainder & 0x0000ff00) >> 8;
2699           b4 = remainder & 0xff;
2700
2701           if (loc > 24)
2702             {
2703               /* The 8-bit immediate already found clears b1 (and maybe b2),
2704                  but must leave b3 and b4 alone.  */
2705
2706               /* First try to find a 32-bit replicated constant that clears
2707                  almost everything.  We can assume that we can't do it in one,
2708                  or else we wouldn't be here.  */
2709               unsigned int tmp = b1 & b2 & b3 & b4;
2710               unsigned int tmp2 = tmp + (tmp << 8) + (tmp << 16)
2711                                   + (tmp << 24);
2712               unsigned int matching_bytes = (tmp == b1) + (tmp == b2)
2713                                             + (tmp == b3) + (tmp == b4);
2714               if (tmp
2715                   && (matching_bytes >= 3
2716                       || (matching_bytes == 2
2717                           && const_ok_for_op (remainder & ~tmp2, code))))
2718                 {
2719                   /* At least 3 of the bytes match, and the fourth has at 
2720                      least as many bits set, or two of the bytes match
2721                      and it will only require one more insn to finish.  */
2722                   result = tmp2;
2723                   i = tmp != b1 ? 32
2724                       : tmp != b2 ? 24
2725                       : tmp != b3 ? 16
2726                       : 8;
2727                 }
2728
2729               /* Second, try to find a 16-bit replicated constant that can
2730                  leave three of the bytes clear.  If b2 or b4 is already
2731                  zero, then we can.  If the 8-bit from above would not
2732                  clear b2 anyway, then we still win.  */
2733               else if (b1 == b3 && (!b2 || !b4
2734                                || (remainder & 0x00ff0000 & ~result)))
2735                 {
2736                   result = remainder & 0xff00ff00;
2737                   i = 24;
2738                 }
2739             }
2740           else if (loc > 16)
2741             {
2742               /* The 8-bit immediate already found clears b2 (and maybe b3)
2743                  and we don't get here unless b1 is alredy clear, but it will
2744                  leave b4 unchanged.  */
2745
2746               /* If we can clear b2 and b4 at once, then we win, since the
2747                  8-bits couldn't possibly reach that far.  */
2748               if (b2 == b4)
2749                 {
2750                   result = remainder & 0x00ff00ff;
2751                   i = 16;
2752                 }
2753             }
2754         }
2755
2756       return_sequence->i[insns++] = result;
2757       remainder &= ~result;
2758
2759       if (code == SET || code == MINUS)
2760         code = PLUS;
2761     }
2762   while (remainder);
2763
2764   return insns;
2765 }
2766
2767 /* Emit an instruction with the indicated PATTERN.  If COND is
2768    non-NULL, conditionalize the execution of the instruction on COND
2769    being true.  */
2770
2771 static void
2772 emit_constant_insn (rtx cond, rtx pattern)
2773 {
2774   if (cond)
2775     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
2776   emit_insn (pattern);
2777 }
2778
2779 /* As above, but extra parameter GENERATE which, if clear, suppresses
2780    RTL generation.  */
2781
2782 static int
2783 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
2784                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
2785                   int generate)
2786 {
2787   int can_invert = 0;
2788   int can_negate = 0;
2789   int final_invert = 0;
2790   int i;
2791   int set_sign_bit_copies = 0;
2792   int clear_sign_bit_copies = 0;
2793   int clear_zero_bit_copies = 0;
2794   int set_zero_bit_copies = 0;
2795   int insns = 0, neg_insns, inv_insns;
2796   unsigned HOST_WIDE_INT temp1, temp2;
2797   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
2798   struct four_ints *immediates;
2799   struct four_ints pos_immediates, neg_immediates, inv_immediates;
2800
2801   /* Find out which operations are safe for a given CODE.  Also do a quick
2802      check for degenerate cases; these can occur when DImode operations
2803      are split.  */
2804   switch (code)
2805     {
2806     case SET:
2807       can_invert = 1;
2808       break;
2809
2810     case PLUS:
2811       can_negate = 1;
2812       break;
2813
2814     case IOR:
2815       if (remainder == 0xffffffff)
2816         {
2817           if (generate)
2818             emit_constant_insn (cond,
2819                                 gen_rtx_SET (VOIDmode, target,
2820                                              GEN_INT (ARM_SIGN_EXTEND (val))));
2821           return 1;
2822         }
2823
2824       if (remainder == 0)
2825         {
2826           if (reload_completed && rtx_equal_p (target, source))
2827             return 0;
2828
2829           if (generate)
2830             emit_constant_insn (cond,
2831                                 gen_rtx_SET (VOIDmode, target, source));
2832           return 1;
2833         }
2834       break;
2835
2836     case AND:
2837       if (remainder == 0)
2838         {
2839           if (generate)
2840             emit_constant_insn (cond,
2841                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
2842           return 1;
2843         }
2844       if (remainder == 0xffffffff)
2845         {
2846           if (reload_completed && rtx_equal_p (target, source))
2847             return 0;
2848           if (generate)
2849             emit_constant_insn (cond,
2850                                 gen_rtx_SET (VOIDmode, target, source));
2851           return 1;
2852         }
2853       can_invert = 1;
2854       break;
2855
2856     case XOR:
2857       if (remainder == 0)
2858         {
2859           if (reload_completed && rtx_equal_p (target, source))
2860             return 0;
2861           if (generate)
2862             emit_constant_insn (cond,
2863                                 gen_rtx_SET (VOIDmode, target, source));
2864           return 1;
2865         }
2866
2867       if (remainder == 0xffffffff)
2868         {
2869           if (generate)
2870             emit_constant_insn (cond,
2871                                 gen_rtx_SET (VOIDmode, target,
2872                                              gen_rtx_NOT (mode, source)));
2873           return 1;
2874         }
2875       final_invert = 1;
2876       break;
2877
2878     case MINUS:
2879       /* We treat MINUS as (val - source), since (source - val) is always
2880          passed as (source + (-val)).  */
2881       if (remainder == 0)
2882         {
2883           if (generate)
2884             emit_constant_insn (cond,
2885                                 gen_rtx_SET (VOIDmode, target,
2886                                              gen_rtx_NEG (mode, source)));
2887           return 1;
2888         }
2889       if (const_ok_for_arm (val))
2890         {
2891           if (generate)
2892             emit_constant_insn (cond,
2893                                 gen_rtx_SET (VOIDmode, target,
2894                                              gen_rtx_MINUS (mode, GEN_INT (val),
2895                                                             source)));
2896           return 1;
2897         }
2898
2899       break;
2900
2901     default:
2902       gcc_unreachable ();
2903     }
2904
2905   /* If we can do it in one insn get out quickly.  */
2906   if (const_ok_for_op (val, code))
2907     {
2908       if (generate)
2909         emit_constant_insn (cond,
2910                             gen_rtx_SET (VOIDmode, target,
2911                                          (source
2912                                           ? gen_rtx_fmt_ee (code, mode, source,
2913                                                             GEN_INT (val))
2914                                           : GEN_INT (val))));
2915       return 1;
2916     }
2917
2918   /* Calculate a few attributes that may be useful for specific
2919      optimizations.  */
2920   /* Count number of leading zeros.  */
2921   for (i = 31; i >= 0; i--)
2922     {
2923       if ((remainder & (1 << i)) == 0)
2924         clear_sign_bit_copies++;
2925       else
2926         break;
2927     }
2928
2929   /* Count number of leading 1's.  */
2930   for (i = 31; i >= 0; i--)
2931     {
2932       if ((remainder & (1 << i)) != 0)
2933         set_sign_bit_copies++;
2934       else
2935         break;
2936     }
2937
2938   /* Count number of trailing zero's.  */
2939   for (i = 0; i <= 31; i++)
2940     {
2941       if ((remainder & (1 << i)) == 0)
2942         clear_zero_bit_copies++;
2943       else
2944         break;
2945     }
2946
2947   /* Count number of trailing 1's.  */
2948   for (i = 0; i <= 31; i++)
2949     {
2950       if ((remainder & (1 << i)) != 0)
2951         set_zero_bit_copies++;
2952       else
2953         break;
2954     }
2955
2956   switch (code)
2957     {
2958     case SET:
2959       /* See if we can do this by sign_extending a constant that is known
2960          to be negative.  This is a good, way of doing it, since the shift
2961          may well merge into a subsequent insn.  */
2962       if (set_sign_bit_copies > 1)
2963         {
2964           if (const_ok_for_arm
2965               (temp1 = ARM_SIGN_EXTEND (remainder
2966                                         << (set_sign_bit_copies - 1))))
2967             {
2968               if (generate)
2969                 {
2970                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2971                   emit_constant_insn (cond,
2972                                       gen_rtx_SET (VOIDmode, new_src,
2973                                                    GEN_INT (temp1)));
2974                   emit_constant_insn (cond,
2975                                       gen_ashrsi3 (target, new_src,
2976                                                    GEN_INT (set_sign_bit_copies - 1)));
2977                 }
2978               return 2;
2979             }
2980           /* For an inverted constant, we will need to set the low bits,
2981              these will be shifted out of harm's way.  */
2982           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
2983           if (const_ok_for_arm (~temp1))
2984             {
2985               if (generate)
2986                 {
2987                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2988                   emit_constant_insn (cond,
2989                                       gen_rtx_SET (VOIDmode, new_src,
2990                                                    GEN_INT (temp1)));
2991                   emit_constant_insn (cond,
2992                                       gen_ashrsi3 (target, new_src,
2993                                                    GEN_INT (set_sign_bit_copies - 1)));
2994                 }
2995               return 2;
2996             }
2997         }
2998
2999       /* See if we can calculate the value as the difference between two
3000          valid immediates.  */
3001       if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
3002         {
3003           int topshift = clear_sign_bit_copies & ~1;
3004
3005           temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
3006                                    & (0xff000000 >> topshift));
3007
3008           /* If temp1 is zero, then that means the 9 most significant
3009              bits of remainder were 1 and we've caused it to overflow.
3010              When topshift is 0 we don't need to do anything since we
3011              can borrow from 'bit 32'.  */
3012           if (temp1 == 0 && topshift != 0)
3013             temp1 = 0x80000000 >> (topshift - 1);
3014
3015           temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
3016
3017           if (const_ok_for_arm (temp2))
3018             {
3019               if (generate)
3020                 {
3021                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3022                   emit_constant_insn (cond,
3023                                       gen_rtx_SET (VOIDmode, new_src,
3024                                                    GEN_INT (temp1)));
3025                   emit_constant_insn (cond,
3026                                       gen_addsi3 (target, new_src,
3027                                                   GEN_INT (-temp2)));
3028                 }
3029
3030               return 2;
3031             }
3032         }
3033
3034       /* See if we can generate this by setting the bottom (or the top)
3035          16 bits, and then shifting these into the other half of the
3036          word.  We only look for the simplest cases, to do more would cost
3037          too much.  Be careful, however, not to generate this when the
3038          alternative would take fewer insns.  */
3039       if (val & 0xffff0000)
3040         {
3041           temp1 = remainder & 0xffff0000;
3042           temp2 = remainder & 0x0000ffff;
3043
3044           /* Overlaps outside this range are best done using other methods.  */
3045           for (i = 9; i < 24; i++)
3046             {
3047               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
3048                   && !const_ok_for_arm (temp2))
3049                 {
3050                   rtx new_src = (subtargets
3051                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
3052                                  : target);
3053                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
3054                                             source, subtargets, generate);
3055                   source = new_src;
3056                   if (generate)
3057                     emit_constant_insn
3058                       (cond,
3059                        gen_rtx_SET
3060                        (VOIDmode, target,
3061                         gen_rtx_IOR (mode,
3062                                      gen_rtx_ASHIFT (mode, source,
3063                                                      GEN_INT (i)),
3064                                      source)));
3065                   return insns + 1;
3066                 }
3067             }
3068
3069           /* Don't duplicate cases already considered.  */
3070           for (i = 17; i < 24; i++)
3071             {
3072               if (((temp1 | (temp1 >> i)) == remainder)
3073                   && !const_ok_for_arm (temp1))
3074                 {
3075                   rtx new_src = (subtargets
3076                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
3077                                  : target);
3078                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
3079                                             source, subtargets, generate);
3080                   source = new_src;
3081                   if (generate)
3082                     emit_constant_insn
3083                       (cond,
3084                        gen_rtx_SET (VOIDmode, target,
3085                                     gen_rtx_IOR
3086                                     (mode,
3087                                      gen_rtx_LSHIFTRT (mode, source,
3088                                                        GEN_INT (i)),
3089                                      source)));
3090                   return insns + 1;
3091                 }
3092             }
3093         }
3094       break;
3095
3096     case IOR:
3097     case XOR:
3098       /* If we have IOR or XOR, and the constant can be loaded in a
3099          single instruction, and we can find a temporary to put it in,
3100          then this can be done in two instructions instead of 3-4.  */
3101       if (subtargets
3102           /* TARGET can't be NULL if SUBTARGETS is 0 */
3103           || (reload_completed && !reg_mentioned_p (target, source)))
3104         {
3105           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
3106             {
3107               if (generate)
3108                 {
3109                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
3110
3111                   emit_constant_insn (cond,
3112                                       gen_rtx_SET (VOIDmode, sub,
3113                                                    GEN_INT (val)));
3114                   emit_constant_insn (cond,
3115                                       gen_rtx_SET (VOIDmode, target,
3116                                                    gen_rtx_fmt_ee (code, mode,
3117                                                                    source, sub)));
3118                 }
3119               return 2;
3120             }
3121         }
3122
3123       if (code == XOR)
3124         break;
3125
3126       /*  Convert.
3127           x = y | constant ( which is composed of set_sign_bit_copies of leading 1s
3128                              and the remainder 0s for e.g. 0xfff00000)
3129           x = ~(~(y ashift set_sign_bit_copies) lshiftrt set_sign_bit_copies)
3130
3131           This can be done in 2 instructions by using shifts with mov or mvn.
3132           e.g. for
3133           x = x | 0xfff00000;
3134           we generate.
3135           mvn   r0, r0, asl #12
3136           mvn   r0, r0, lsr #12  */
3137       if (set_sign_bit_copies > 8
3138           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
3139         {
3140           if (generate)
3141             {
3142               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
3143               rtx shift = GEN_INT (set_sign_bit_copies);
3144
3145               emit_constant_insn
3146                 (cond,
3147                  gen_rtx_SET (VOIDmode, sub,
3148                               gen_rtx_NOT (mode,
3149                                            gen_rtx_ASHIFT (mode,
3150                                                            source,
3151                                                            shift))));
3152               emit_constant_insn
3153                 (cond,
3154                  gen_rtx_SET (VOIDmode, target,
3155                               gen_rtx_NOT (mode,
3156                                            gen_rtx_LSHIFTRT (mode, sub,
3157                                                              shift))));
3158             }
3159           return 2;
3160         }
3161
3162       /* Convert
3163           x = y | constant (which has set_zero_bit_copies number of trailing ones).
3164            to
3165           x = ~((~y lshiftrt set_zero_bit_copies) ashift set_zero_bit_copies).
3166
3167           For eg. r0 = r0 | 0xfff
3168                mvn      r0, r0, lsr #12
3169                mvn      r0, r0, asl #12
3170
3171       */
3172       if (set_zero_bit_copies > 8
3173           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
3174         {
3175           if (generate)
3176             {
3177               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
3178               rtx shift = GEN_INT (set_zero_bit_copies);
3179
3180               emit_constant_insn
3181                 (cond,
3182                  gen_rtx_SET (VOIDmode, sub,
3183                               gen_rtx_NOT (mode,
3184                                            gen_rtx_LSHIFTRT (mode,
3185                                                              source,
3186                                                              shift))));
3187               emit_constant_insn
3188                 (cond,
3189                  gen_rtx_SET (VOIDmode, target,
3190                               gen_rtx_NOT (mode,
3191                                            gen_rtx_ASHIFT (mode, sub,
3192                                                            shift))));
3193             }
3194           return 2;
3195         }
3196
3197       /* This will never be reached for Thumb2 because orn is a valid
3198          instruction. This is for Thumb1 and the ARM 32 bit cases.
3199
3200          x = y | constant (such that ~constant is a valid constant)
3201          Transform this to
3202          x = ~(~y & ~constant).
3203       */
3204       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
3205         {
3206           if (generate)
3207             {
3208               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
3209               emit_constant_insn (cond,
3210                                   gen_rtx_SET (VOIDmode, sub,
3211                                                gen_rtx_NOT (mode, source)));
3212               source = sub;
3213               if (subtargets)
3214                 sub = gen_reg_rtx (mode);
3215               emit_constant_insn (cond,
3216                                   gen_rtx_SET (VOIDmode, sub,
3217                                                gen_rtx_AND (mode, source,
3218                                                             GEN_INT (temp1))));
3219               emit_constant_insn (cond,
3220                                   gen_rtx_SET (VOIDmode, target,
3221                                                gen_rtx_NOT (mode, sub)));
3222             }
3223           return 3;
3224         }
3225       break;
3226
3227     case AND:
3228       /* See if two shifts will do 2 or more insn's worth of work.  */
3229       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
3230         {
3231           HOST_WIDE_INT shift_mask = ((0xffffffff
3232                                        << (32 - clear_sign_bit_copies))
3233                                       & 0xffffffff);
3234
3235           if ((remainder | shift_mask) != 0xffffffff)
3236             {
3237               if (generate)
3238                 {
3239                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3240                   insns = arm_gen_constant (AND, mode, cond,
3241                                             remainder | shift_mask,
3242                                             new_src, source, subtargets, 1);
3243                   source = new_src;
3244                 }
3245               else
3246                 {
3247                   rtx targ = subtargets ? NULL_RTX : target;
3248                   insns = arm_gen_constant (AND, mode, cond,
3249                                             remainder | shift_mask,
3250                                             targ, source, subtargets, 0);
3251                 }
3252             }
3253
3254           if (generate)
3255             {
3256               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3257               rtx shift = GEN_INT (clear_sign_bit_copies);
3258
3259               emit_insn (gen_ashlsi3 (new_src, source, shift));
3260               emit_insn (gen_lshrsi3 (target, new_src, shift));
3261             }
3262
3263           return insns + 2;
3264         }
3265
3266       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
3267         {
3268           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
3269
3270           if ((remainder | shift_mask) != 0xffffffff)
3271             {
3272               if (generate)
3273                 {
3274                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3275
3276                   insns = arm_gen_constant (AND, mode, cond,
3277                                             remainder | shift_mask,
3278                                             new_src, source, subtargets, 1);
3279                   source = new_src;
3280                 }
3281               else
3282                 {
3283                   rtx targ = subtargets ? NULL_RTX : target;
3284
3285                   insns = arm_gen_constant (AND, mode, cond,
3286                                             remainder | shift_mask,
3287                                             targ, source, subtargets, 0);
3288                 }
3289             }
3290
3291           if (generate)
3292             {
3293               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3294               rtx shift = GEN_INT (clear_zero_bit_copies);
3295
3296               emit_insn (gen_lshrsi3 (new_src, source, shift));
3297               emit_insn (gen_ashlsi3 (target, new_src, shift));
3298             }
3299
3300           return insns + 2;
3301         }
3302
3303       break;
3304
3305     default:
3306       break;
3307     }
3308
3309   /* Calculate what the instruction sequences would be if we generated it
3310      normally, negated, or inverted.  */
3311   if (code == AND)
3312     /* AND cannot be split into multiple insns, so invert and use BIC.  */
3313     insns = 99;
3314   else
3315     insns = optimal_immediate_sequence (code, remainder, &pos_immediates);
3316
3317   if (can_negate)
3318     neg_insns = optimal_immediate_sequence (code, (-remainder) & 0xffffffff,
3319                                             &neg_immediates);
3320   else
3321     neg_insns = 99;
3322
3323   if (can_invert || final_invert)
3324     inv_insns = optimal_immediate_sequence (code, remainder ^ 0xffffffff,
3325                                             &inv_immediates);
3326   else
3327     inv_insns = 99;
3328
3329   immediates = &pos_immediates;
3330
3331   /* Is the negated immediate sequence more efficient?  */
3332   if (neg_insns < insns && neg_insns <= inv_insns)
3333     {
3334       insns = neg_insns;
3335       immediates = &neg_immediates;
3336     }
3337   else
3338     can_negate = 0;
3339
3340   /* Is the inverted immediate sequence more efficient?
3341      We must allow for an extra NOT instruction for XOR operations, although
3342      there is some chance that the final 'mvn' will get optimized later.  */
3343   if ((inv_insns + 1) < insns || (!final_invert && inv_insns < insns))
3344     {
3345       insns = inv_insns;
3346       immediates = &inv_immediates;
3347     }
3348   else
3349     {
3350       can_invert = 0;
3351       final_invert = 0;
3352     }
3353
3354   /* Now output the chosen sequence as instructions.  */
3355   if (generate)
3356     {
3357       for (i = 0; i < insns; i++)
3358         {
3359           rtx new_src, temp1_rtx;
3360
3361           temp1 = immediates->i[i];
3362
3363           if (code == SET || code == MINUS)
3364             new_src = (subtargets ? gen_reg_rtx (mode) : target);
3365           else if ((final_invert || i < (insns - 1)) && subtargets)
3366             new_src = gen_reg_rtx (mode);
3367           else
3368             new_src = target;
3369
3370           if (can_invert)
3371             temp1 = ~temp1;
3372           else if (can_negate)
3373             temp1 = -temp1;
3374
3375           temp1 = trunc_int_for_mode (temp1, mode);
3376           temp1_rtx = GEN_INT (temp1);
3377
3378           if (code == SET)
3379             ;
3380           else if (code == MINUS)
3381             temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
3382           else
3383             temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
3384
3385           emit_constant_insn (cond,
3386                               gen_rtx_SET (VOIDmode, new_src,
3387                                            temp1_rtx));
3388           source = new_src;
3389
3390           if (code == SET)
3391             {
3392               can_negate = can_invert;
3393               can_invert = 0;
3394               code = PLUS;
3395             }
3396           else if (code == MINUS)
3397             code = PLUS;
3398         }
3399     }
3400
3401   if (final_invert)
3402     {
3403       if (generate)
3404         emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
3405                                                gen_rtx_NOT (mode, source)));
3406       insns++;
3407     }
3408
3409   return insns;
3410 }
3411
3412 /* Canonicalize a comparison so that we are more likely to recognize it.
3413    This can be done for a few constant compares, where we can make the
3414    immediate value easier to load.  */
3415
3416 enum rtx_code
3417 arm_canonicalize_comparison (enum rtx_code code, rtx *op0, rtx *op1)
3418 {
3419   enum machine_mode mode;
3420   unsigned HOST_WIDE_INT i, maxval;
3421
3422   mode = GET_MODE (*op0);
3423   if (mode == VOIDmode)
3424     mode = GET_MODE (*op1);
3425
3426   maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
3427
3428   /* For DImode, we have GE/LT/GEU/LTU comparisons.  In ARM mode
3429      we can also use cmp/cmpeq for GTU/LEU.  GT/LE must be either
3430      reversed or (for constant OP1) adjusted to GE/LT.  Similarly
3431      for GTU/LEU in Thumb mode.  */
3432   if (mode == DImode)
3433     {
3434       rtx tem;
3435
3436       /* To keep things simple, always use the Cirrus cfcmp64 if it is
3437          available.  */
3438       if (TARGET_ARM && TARGET_HARD_FLOAT && TARGET_MAVERICK)
3439         return code;
3440
3441       if (code == GT || code == LE
3442           || (!TARGET_ARM && (code == GTU || code == LEU)))
3443         {
3444           /* Missing comparison.  First try to use an available
3445              comparison.  */
3446           if (GET_CODE (*op1) == CONST_INT)
3447             {
3448               i = INTVAL (*op1);
3449               switch (code)
3450                 {
3451                 case GT:
3452                 case LE:
3453                   if (i != maxval
3454                       && arm_const_double_by_immediates (GEN_INT (i + 1)))
3455                     {
3456                       *op1 = GEN_INT (i + 1);
3457                       return code == GT ? GE : LT;
3458                     }
3459                   break;
3460                 case GTU:
3461                 case LEU:
3462                   if (i != ~((unsigned HOST_WIDE_INT) 0)
3463                       && arm_const_double_by_immediates (GEN_INT (i + 1)))
3464                     {
3465                       *op1 = GEN_INT (i + 1);
3466                       return code == GTU ? GEU : LTU;
3467                     }
3468                   break;
3469                 default:
3470                   gcc_unreachable ();
3471                 }
3472             }
3473
3474           /* If that did not work, reverse the condition.  */
3475           tem = *op0;
3476           *op0 = *op1;
3477           *op1 = tem;
3478           return swap_condition (code);
3479         }
3480
3481       return code;
3482     }
3483
3484   /* If *op0 is (zero_extend:SI (subreg:QI (reg:SI) 0)) and comparing
3485      with const0_rtx, change it to (and:SI (reg:SI) (const_int 255)),
3486      to facilitate possible combining with a cmp into 'ands'.  */
3487   if (mode == SImode
3488       && GET_CODE (*op0) == ZERO_EXTEND
3489       && GET_CODE (XEXP (*op0, 0)) == SUBREG
3490       && GET_MODE (XEXP (*op0, 0)) == QImode
3491       && GET_MODE (SUBREG_REG (XEXP (*op0, 0))) == SImode
3492       && subreg_lowpart_p (XEXP (*op0, 0))
3493       && *op1 == const0_rtx)
3494     *op0 = gen_rtx_AND (SImode, SUBREG_REG (XEXP (*op0, 0)),
3495                         GEN_INT (255));
3496
3497   /* Comparisons smaller than DImode.  Only adjust comparisons against
3498      an out-of-range constant.  */
3499   if (GET_CODE (*op1) != CONST_INT
3500       || const_ok_for_arm (INTVAL (*op1))
3501       || const_ok_for_arm (- INTVAL (*op1)))
3502     return code;
3503
3504   i = INTVAL (*op1);
3505
3506   switch (code)
3507     {
3508     case EQ:
3509     case NE:
3510       return code;
3511
3512     case GT:
3513     case LE:
3514       if (i != maxval
3515           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
3516         {
3517           *op1 = GEN_INT (i + 1);
3518           return code == GT ? GE : LT;
3519         }
3520       break;
3521
3522     case GE:
3523     case LT:
3524       if (i != ~maxval
3525           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
3526         {
3527           *op1 = GEN_INT (i - 1);
3528           return code == GE ? GT : LE;
3529         }
3530       break;
3531
3532     case GTU:
3533     case LEU:
3534       if (i != ~((unsigned HOST_WIDE_INT) 0)
3535           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
3536         {
3537           *op1 = GEN_INT (i + 1);
3538           return code == GTU ? GEU : LTU;
3539         }
3540       break;
3541
3542     case GEU:
3543     case LTU:
3544       if (i != 0
3545           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
3546         {
3547           *op1 = GEN_INT (i - 1);
3548           return code == GEU ? GTU : LEU;
3549         }
3550       break;
3551
3552     default:
3553       gcc_unreachable ();
3554     }
3555
3556   return code;
3557 }
3558
3559
3560 /* Define how to find the value returned by a function.  */
3561
3562 static rtx
3563 arm_function_value(const_tree type, const_tree func,
3564                    bool outgoing ATTRIBUTE_UNUSED)
3565 {
3566   enum machine_mode mode;
3567   int unsignedp ATTRIBUTE_UNUSED;
3568   rtx r ATTRIBUTE_UNUSED;
3569
3570   mode = TYPE_MODE (type);
3571
3572   if (TARGET_AAPCS_BASED)
3573     return aapcs_allocate_return_reg (mode, type, func);
3574
3575   /* Promote integer types.  */
3576   if (INTEGRAL_TYPE_P (type))
3577     mode = arm_promote_function_mode (type, mode, &unsignedp, func, 1);
3578
3579   /* Promotes small structs returned in a register to full-word size
3580      for big-endian AAPCS.  */
3581   if (arm_return_in_msb (type))
3582     {
3583       HOST_WIDE_INT size = int_size_in_bytes (type);
3584       if (size % UNITS_PER_WORD != 0)
3585         {
3586           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
3587           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
3588         }
3589     }
3590
3591   return LIBCALL_VALUE (mode);
3592 }
3593
3594 static int
3595 libcall_eq (const void *p1, const void *p2)
3596 {
3597   return rtx_equal_p ((const_rtx) p1, (const_rtx) p2);
3598 }
3599
3600 static hashval_t
3601 libcall_hash (const void *p1)
3602 {
3603   return hash_rtx ((const_rtx) p1, VOIDmode, NULL, NULL, FALSE);
3604 }
3605
3606 static void
3607 add_libcall (htab_t htab, rtx libcall)
3608 {
3609   *htab_find_slot (htab, libcall, INSERT) = libcall;
3610 }
3611
3612 static bool
3613 arm_libcall_uses_aapcs_base (const_rtx libcall)
3614 {
3615   static bool init_done = false;
3616   static htab_t libcall_htab;
3617
3618   if (!init_done)
3619     {
3620       init_done = true;
3621
3622       libcall_htab = htab_create (31, libcall_hash, libcall_eq,
3623                                   NULL);
3624       add_libcall (libcall_htab,
3625                    convert_optab_libfunc (sfloat_optab, SFmode, SImode));
3626       add_libcall (libcall_htab,
3627                    convert_optab_libfunc (sfloat_optab, DFmode, SImode));
3628       add_libcall (libcall_htab,
3629                    convert_optab_libfunc (sfloat_optab, SFmode, DImode));
3630       add_libcall (libcall_htab,
3631                    convert_optab_libfunc (sfloat_optab, DFmode, DImode));
3632       
3633       add_libcall (libcall_htab,
3634                    convert_optab_libfunc (ufloat_optab, SFmode, SImode));
3635       add_libcall (libcall_htab,
3636                    convert_optab_libfunc (ufloat_optab, DFmode, SImode));
3637       add_libcall (libcall_htab,
3638                    convert_optab_libfunc (ufloat_optab, SFmode, DImode));
3639       add_libcall (libcall_htab,
3640                    convert_optab_libfunc (ufloat_optab, DFmode, DImode));
3641
3642       add_libcall (libcall_htab,
3643                    convert_optab_libfunc (sext_optab, SFmode, HFmode));
3644       add_libcall (libcall_htab,
3645                    convert_optab_libfunc (trunc_optab, HFmode, SFmode));
3646       add_libcall (libcall_htab,
3647                    convert_optab_libfunc (sfix_optab, DImode, DFmode));
3648       add_libcall (libcall_htab,
3649                    convert_optab_libfunc (ufix_optab, DImode, DFmode));
3650       add_libcall (libcall_htab,
3651                    convert_optab_libfunc (sfix_optab, DImode, SFmode));
3652       add_libcall (libcall_htab,
3653                    convert_optab_libfunc (ufix_optab, DImode, SFmode));
3654
3655       /* Values from double-precision helper functions are returned in core
3656          registers if the selected core only supports single-precision
3657          arithmetic, even if we are using the hard-float ABI.  The same is
3658          true for single-precision helpers, but we will never be using the
3659          hard-float ABI on a CPU which doesn't support single-precision
3660          operations in hardware.  */
3661       add_libcall (libcall_htab, optab_libfunc (add_optab, DFmode));
3662       add_libcall (libcall_htab, optab_libfunc (sdiv_optab, DFmode));
3663       add_libcall (libcall_htab, optab_libfunc (smul_optab, DFmode));
3664       add_libcall (libcall_htab, optab_libfunc (neg_optab, DFmode));
3665       add_libcall (libcall_htab, optab_libfunc (sub_optab, DFmode));
3666       add_libcall (libcall_htab, optab_libfunc (eq_optab, DFmode));
3667       add_libcall (libcall_htab, optab_libfunc (lt_optab, DFmode));
3668       add_libcall (libcall_htab, optab_libfunc (le_optab, DFmode));
3669       add_libcall (libcall_htab, optab_libfunc (ge_optab, DFmode));
3670       add_libcall (libcall_htab, optab_libfunc (gt_optab, DFmode));
3671       add_libcall (libcall_htab, optab_libfunc (unord_optab, DFmode));
3672       add_libcall (libcall_htab, convert_optab_libfunc (sext_optab, DFmode,
3673                                                         SFmode));
3674       add_libcall (libcall_htab, convert_optab_libfunc (trunc_optab, SFmode,
3675                                                         DFmode));
3676     }
3677
3678   return libcall && htab_find (libcall_htab, libcall) != NULL;
3679 }
3680
3681 rtx
3682 arm_libcall_value (enum machine_mode mode, const_rtx libcall)
3683 {
3684   if (TARGET_AAPCS_BASED && arm_pcs_default != ARM_PCS_AAPCS
3685       && GET_MODE_CLASS (mode) == MODE_FLOAT)
3686     {
3687       /* The following libcalls return their result in integer registers,
3688          even though they return a floating point value.  */
3689       if (arm_libcall_uses_aapcs_base (libcall))
3690         return gen_rtx_REG (mode, ARG_REGISTER(1));
3691
3692     }
3693
3694   return LIBCALL_VALUE (mode);
3695 }
3696
3697 /* Determine the amount of memory needed to store the possible return
3698    registers of an untyped call.  */
3699 int
3700 arm_apply_result_size (void)
3701 {
3702   int size = 16;
3703
3704   if (TARGET_32BIT)
3705     {
3706       if (TARGET_HARD_FLOAT_ABI)
3707         {
3708           if (TARGET_VFP)
3709             size += 32;
3710           if (TARGET_FPA)
3711             size += 12;
3712           if (TARGET_MAVERICK)
3713             size += 8;
3714         }
3715       if (TARGET_IWMMXT_ABI)
3716         size += 8;
3717     }
3718
3719   return size;
3720 }
3721
3722 /* Decide whether TYPE should be returned in memory (true)
3723    or in a register (false).  FNTYPE is the type of the function making
3724    the call.  */
3725 static bool
3726 arm_return_in_memory (const_tree type, const_tree fntype)
3727 {
3728   HOST_WIDE_INT size;
3729
3730   size = int_size_in_bytes (type);  /* Negative if not fixed size.  */
3731
3732   if (TARGET_AAPCS_BASED)
3733     {
3734       /* Simple, non-aggregate types (ie not including vectors and
3735          complex) are always returned in a register (or registers).
3736          We don't care about which register here, so we can short-cut
3737          some of the detail.  */
3738       if (!AGGREGATE_TYPE_P (type)
3739           && TREE_CODE (type) != VECTOR_TYPE
3740           && TREE_CODE (type) != COMPLEX_TYPE)
3741         return false;
3742
3743       /* Any return value that is no larger than one word can be
3744          returned in r0.  */
3745       if (((unsigned HOST_WIDE_INT) size) <= UNITS_PER_WORD)
3746         return false;
3747
3748       /* Check any available co-processors to see if they accept the
3749          type as a register candidate (VFP, for example, can return
3750          some aggregates in consecutive registers).  These aren't
3751          available if the call is variadic.  */
3752       if (aapcs_select_return_coproc (type, fntype) >= 0)
3753         return false;
3754
3755       /* Vector values should be returned using ARM registers, not
3756          memory (unless they're over 16 bytes, which will break since
3757          we only have four call-clobbered registers to play with).  */
3758       if (TREE_CODE (type) == VECTOR_TYPE)
3759         return (size < 0 || size > (4 * UNITS_PER_WORD));
3760
3761       /* The rest go in memory.  */
3762       return true;
3763     }
3764
3765   if (TREE_CODE (type) == VECTOR_TYPE)
3766     return (size < 0 || size > (4 * UNITS_PER_WORD));
3767
3768   if (!AGGREGATE_TYPE_P (type) &&
3769       (TREE_CODE (type) != VECTOR_TYPE))
3770     /* All simple types are returned in registers.  */
3771     return false;
3772
3773   if (arm_abi != ARM_ABI_APCS)
3774     {
3775       /* ATPCS and later return aggregate types in memory only if they are
3776          larger than a word (or are variable size).  */
3777       return (size < 0 || size > UNITS_PER_WORD);
3778     }
3779
3780   /* For the arm-wince targets we choose to be compatible with Microsoft's
3781      ARM and Thumb compilers, which always return aggregates in memory.  */
3782 #ifndef ARM_WINCE
3783   /* All structures/unions bigger than one word are returned in memory.
3784      Also catch the case where int_size_in_bytes returns -1.  In this case
3785      the aggregate is either huge or of variable size, and in either case
3786      we will want to return it via memory and not in a register.  */
3787   if (size < 0 || size > UNITS_PER_WORD)
3788     return true;
3789
3790   if (TREE_CODE (type) == RECORD_TYPE)
3791     {
3792       tree field;
3793
3794       /* For a struct the APCS says that we only return in a register
3795          if the type is 'integer like' and every addressable element
3796          has an offset of zero.  For practical purposes this means
3797          that the structure can have at most one non bit-field element
3798          and that this element must be the first one in the structure.  */
3799
3800       /* Find the first field, ignoring non FIELD_DECL things which will
3801          have been created by C++.  */
3802       for (field = TYPE_FIELDS (type);
3803            field && TREE_CODE (field) != FIELD_DECL;
3804            field = DECL_CHAIN (field))
3805         continue;
3806
3807       if (field == NULL)
3808         return false; /* An empty structure.  Allowed by an extension to ANSI C.  */
3809
3810       /* Check that the first field is valid for returning in a register.  */
3811
3812       /* ... Floats are not allowed */
3813       if (FLOAT_TYPE_P (TREE_TYPE (field)))
3814         return true;
3815
3816       /* ... Aggregates that are not themselves valid for returning in
3817          a register are not allowed.  */
3818       if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
3819         return true;
3820
3821       /* Now check the remaining fields, if any.  Only bitfields are allowed,
3822          since they are not addressable.  */
3823       for (field = DECL_CHAIN (field);
3824            field;
3825            field = DECL_CHAIN (field))
3826         {
3827           if (TREE_CODE (field) != FIELD_DECL)
3828             continue;
3829
3830           if (!DECL_BIT_FIELD_TYPE (field))
3831             return true;
3832         }
3833
3834       return false;
3835     }
3836
3837   if (TREE_CODE (type) == UNION_TYPE)
3838     {
3839       tree field;
3840
3841       /* Unions can be returned in registers if every element is
3842          integral, or can be returned in an integer register.  */
3843       for (field = TYPE_FIELDS (type);
3844            field;
3845            field = DECL_CHAIN (field))
3846         {
3847           if (TREE_CODE (field) != FIELD_DECL)
3848             continue;
3849
3850           if (FLOAT_TYPE_P (TREE_TYPE (field)))
3851             return true;
3852
3853           if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
3854             return true;
3855         }
3856
3857       return false;
3858     }
3859 #endif /* not ARM_WINCE */
3860
3861   /* Return all other types in memory.  */
3862   return true;
3863 }
3864
3865 /* Indicate whether or not words of a double are in big-endian order.  */
3866
3867 int
3868 arm_float_words_big_endian (void)
3869 {
3870   if (TARGET_MAVERICK)
3871     return 0;
3872
3873   /* For FPA, float words are always big-endian.  For VFP, floats words
3874      follow the memory system mode.  */
3875
3876   if (TARGET_FPA)
3877     {
3878       return 1;
3879     }
3880
3881   if (TARGET_VFP)
3882     return (TARGET_BIG_END ? 1 : 0);
3883
3884   return 1;
3885 }
3886
3887 const struct pcs_attribute_arg
3888 {
3889   const char *arg;
3890   enum arm_pcs value;
3891 } pcs_attribute_args[] =
3892   {
3893     {"aapcs", ARM_PCS_AAPCS},
3894     {"aapcs-vfp", ARM_PCS_AAPCS_VFP},
3895 #if 0
3896     /* We could recognize these, but changes would be needed elsewhere
3897      * to implement them.  */
3898     {"aapcs-iwmmxt", ARM_PCS_AAPCS_IWMMXT},
3899     {"atpcs", ARM_PCS_ATPCS},
3900     {"apcs", ARM_PCS_APCS},
3901 #endif
3902     {NULL, ARM_PCS_UNKNOWN}
3903   };
3904
3905 static enum arm_pcs
3906 arm_pcs_from_attribute (tree attr)
3907 {
3908   const struct pcs_attribute_arg *ptr;
3909   const char *arg;
3910
3911   /* Get the value of the argument.  */
3912   if (TREE_VALUE (attr) == NULL_TREE
3913       || TREE_CODE (TREE_VALUE (attr)) != STRING_CST)
3914     return ARM_PCS_UNKNOWN;
3915
3916   arg = TREE_STRING_POINTER (TREE_VALUE (attr));
3917
3918   /* Check it against the list of known arguments.  */
3919   for (ptr = pcs_attribute_args; ptr->arg != NULL; ptr++)
3920     if (streq (arg, ptr->arg))
3921       return ptr->value;
3922
3923   /* An unrecognized interrupt type.  */
3924   return ARM_PCS_UNKNOWN;
3925 }
3926
3927 /* Get the PCS variant to use for this call.  TYPE is the function's type
3928    specification, DECL is the specific declartion.  DECL may be null if
3929    the call could be indirect or if this is a library call.  */
3930 static enum arm_pcs
3931 arm_get_pcs_model (const_tree type, const_tree decl)
3932 {
3933   bool user_convention = false;
3934   enum arm_pcs user_pcs = arm_pcs_default;
3935   tree attr;
3936
3937   gcc_assert (type);
3938
3939   attr = lookup_attribute ("pcs", TYPE_ATTRIBUTES (type));
3940   if (attr)
3941     {
3942       user_pcs = arm_pcs_from_attribute (TREE_VALUE (attr));
3943       user_convention = true;
3944     }
3945
3946   if (TARGET_AAPCS_BASED)
3947     {
3948       /* Detect varargs functions.  These always use the base rules
3949          (no argument is ever a candidate for a co-processor
3950          register).  */
3951       bool base_rules = stdarg_p (type);
3952       
3953       if (user_convention)
3954         {
3955           if (user_pcs > ARM_PCS_AAPCS_LOCAL)
3956             sorry ("non-AAPCS derived PCS variant");
3957           else if (base_rules && user_pcs != ARM_PCS_AAPCS)
3958             error ("variadic functions must use the base AAPCS variant");
3959         }
3960
3961       if (base_rules)
3962         return ARM_PCS_AAPCS;
3963       else if (user_convention)
3964         return user_pcs;
3965       else if (decl && flag_unit_at_a_time)
3966         {
3967           /* Local functions never leak outside this compilation unit,
3968              so we are free to use whatever conventions are
3969              appropriate.  */
3970           /* FIXME: remove CONST_CAST_TREE when cgraph is constified.  */
3971           struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE(decl));
3972           if (i && i->local)
3973             return ARM_PCS_AAPCS_LOCAL;
3974         }
3975     }
3976   else if (user_convention && user_pcs != arm_pcs_default)
3977     sorry ("PCS variant");
3978
3979   /* For everything else we use the target's default.  */
3980   return arm_pcs_default;
3981 }
3982
3983
3984 static void
3985 aapcs_vfp_cum_init (CUMULATIVE_ARGS *pcum  ATTRIBUTE_UNUSED,
3986                     const_tree fntype ATTRIBUTE_UNUSED,
3987                     rtx libcall ATTRIBUTE_UNUSED, 
3988                     const_tree fndecl ATTRIBUTE_UNUSED)
3989 {
3990   /* Record the unallocated VFP registers.  */
3991   pcum->aapcs_vfp_regs_free = (1 << NUM_VFP_ARG_REGS) - 1;
3992   pcum->aapcs_vfp_reg_alloc = 0;
3993 }
3994
3995 /* Walk down the type tree of TYPE counting consecutive base elements.
3996    If *MODEP is VOIDmode, then set it to the first valid floating point
3997    type.  If a non-floating point type is found, or if a floating point
3998    type that doesn't match a non-VOIDmode *MODEP is found, then return -1,
3999    otherwise return the count in the sub-tree.  */
4000 static int
4001 aapcs_vfp_sub_candidate (const_tree type, enum machine_mode *modep)
4002 {
4003   enum machine_mode mode;
4004   HOST_WIDE_INT size;
4005
4006   switch (TREE_CODE (type))
4007     {
4008     case REAL_TYPE:
4009       mode = TYPE_MODE (type);
4010       if (mode != DFmode && mode != SFmode)
4011         return -1;
4012
4013       if (*modep == VOIDmode)
4014         *modep = mode;
4015
4016       if (*modep == mode)
4017         return 1;
4018
4019       break;
4020
4021     case COMPLEX_TYPE:
4022       mode = TYPE_MODE (TREE_TYPE (type));
4023       if (mode != DFmode && mode != SFmode)
4024         return -1;
4025
4026       if (*modep == VOIDmode)
4027         *modep = mode;
4028
4029       if (*modep == mode)
4030         return 2;
4031
4032       break;
4033
4034     case VECTOR_TYPE:
4035       /* Use V2SImode and V4SImode as representatives of all 64-bit
4036          and 128-bit vector types, whether or not those modes are
4037          supported with the present options.  */
4038       size = int_size_in_bytes (type);
4039       switch (size)
4040         {
4041         case 8:
4042           mode = V2SImode;
4043           break;
4044         case 16:
4045           mode = V4SImode;
4046           break;
4047         default:
4048           return -1;
4049         }
4050
4051       if (*modep == VOIDmode)
4052         *modep = mode;
4053
4054       /* Vector modes are considered to be opaque: two vectors are
4055          equivalent for the purposes of being homogeneous aggregates
4056          if they are the same size.  */
4057       if (*modep == mode)
4058         return 1;
4059
4060       break;
4061
4062     case ARRAY_TYPE:
4063       {
4064         int count;
4065         tree index = TYPE_DOMAIN (type);
4066
4067         /* Can't handle incomplete types.  */
4068         if (!COMPLETE_TYPE_P(type))
4069           return -1;
4070
4071         count = aapcs_vfp_sub_candidate (TREE_TYPE (type), modep);
4072         if (count == -1
4073             || !index
4074             || !TYPE_MAX_VALUE (index)
4075             || !host_integerp (TYPE_MAX_VALUE (index), 1)
4076             || !TYPE_MIN_VALUE (index)
4077             || !host_integerp (TYPE_MIN_VALUE (index), 1)
4078             || count < 0)
4079           return -1;
4080
4081         count *= (1 + tree_low_cst (TYPE_MAX_VALUE (index), 1)
4082                       - tree_low_cst (TYPE_MIN_VALUE (index), 1));
4083
4084         /* There must be no padding.  */
4085         if (!host_integerp (TYPE_SIZE (type), 1)
4086             || (tree_low_cst (TYPE_SIZE (type), 1)
4087                 != count * GET_MODE_BITSIZE (*modep)))
4088           return -1;
4089
4090         return count;
4091       }
4092       
4093     case RECORD_TYPE:
4094       {
4095         int count = 0;
4096         int sub_count;
4097         tree field;
4098
4099         /* Can't handle incomplete types.  */
4100         if (!COMPLETE_TYPE_P(type))
4101           return -1;
4102
4103         for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4104           {
4105             if (TREE_CODE (field) != FIELD_DECL)
4106               continue;
4107
4108             sub_count = aapcs_vfp_sub_candidate (TREE_TYPE (field), modep);
4109             if (sub_count < 0)
4110               return -1;
4111             count += sub_count;
4112           }
4113
4114         /* There must be no padding.  */
4115         if (!host_integerp (TYPE_SIZE (type), 1)
4116             || (tree_low_cst (TYPE_SIZE (type), 1)
4117                 != count * GET_MODE_BITSIZE (*modep)))
4118           return -1;
4119
4120         return count;
4121       }
4122
4123     case UNION_TYPE:
4124     case QUAL_UNION_TYPE:
4125       {
4126         /* These aren't very interesting except in a degenerate case.  */
4127         int count = 0;
4128         int sub_count;
4129         tree field;
4130
4131         /* Can't handle incomplete types.  */
4132         if (!COMPLETE_TYPE_P(type))
4133           return -1;
4134
4135         for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4136           {
4137             if (TREE_CODE (field) != FIELD_DECL)
4138               continue;
4139
4140             sub_count = aapcs_vfp_sub_candidate (TREE_TYPE (field), modep);
4141             if (sub_count < 0)
4142               return -1;
4143             count = count > sub_count ? count : sub_count;
4144           }
4145
4146         /* There must be no padding.  */
4147         if (!host_integerp (TYPE_SIZE (type), 1)
4148             || (tree_low_cst (TYPE_SIZE (type), 1)
4149                 != count * GET_MODE_BITSIZE (*modep)))
4150           return -1;
4151
4152         return count;
4153       }
4154
4155     default:
4156       break;
4157     }
4158
4159   return -1;
4160 }
4161
4162 /* Return true if PCS_VARIANT should use VFP registers.  */
4163 static bool
4164 use_vfp_abi (enum arm_pcs pcs_variant, bool is_double)
4165 {
4166   if (pcs_variant == ARM_PCS_AAPCS_VFP)
4167     {
4168       static bool seen_thumb1_vfp = false;
4169
4170       if (TARGET_THUMB1 && !seen_thumb1_vfp)
4171         {
4172           sorry ("Thumb-1 hard-float VFP ABI");
4173           /* sorry() is not immediately fatal, so only display this once.  */
4174           seen_thumb1_vfp = true;
4175         }
4176
4177       return true;
4178     }
4179
4180   if (pcs_variant != ARM_PCS_AAPCS_LOCAL)
4181     return false;
4182
4183   return (TARGET_32BIT && TARGET_VFP && TARGET_HARD_FLOAT &&
4184           (TARGET_VFP_DOUBLE || !is_double));
4185 }
4186
4187 static bool
4188 aapcs_vfp_is_call_or_return_candidate (enum arm_pcs pcs_variant,
4189                                        enum machine_mode mode, const_tree type,
4190                                        enum machine_mode *base_mode, int *count)
4191 {
4192   enum machine_mode new_mode = VOIDmode;
4193
4194   if (GET_MODE_CLASS (mode) == MODE_FLOAT
4195       || GET_MODE_CLASS (mode) == MODE_VECTOR_INT
4196       || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
4197     {
4198       *count = 1;
4199       new_mode = mode;
4200     }
4201   else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
4202     {
4203       *count = 2;
4204       new_mode = (mode == DCmode ? DFmode : SFmode);
4205     }
4206   else if (type && (mode == BLKmode || TREE_CODE (type) == VECTOR_TYPE))
4207     {
4208       int ag_count = aapcs_vfp_sub_candidate (type, &new_mode);
4209
4210       if (ag_count > 0 && ag_count <= 4)
4211         *count = ag_count;
4212       else
4213         return false;
4214     }
4215   else
4216     return false;
4217
4218
4219   if (!use_vfp_abi (pcs_variant, ARM_NUM_REGS (new_mode) > 1))
4220     return false;
4221
4222   *base_mode = new_mode;
4223   return true;
4224 }
4225
4226 static bool
4227 aapcs_vfp_is_return_candidate (enum arm_pcs pcs_variant,
4228                                enum machine_mode mode, const_tree type)
4229 {
4230   int count ATTRIBUTE_UNUSED;
4231   enum machine_mode ag_mode ATTRIBUTE_UNUSED;
4232
4233   if (!use_vfp_abi (pcs_variant, false))
4234     return false;
4235   return aapcs_vfp_is_call_or_return_candidate (pcs_variant, mode, type,
4236                                                 &ag_mode, &count);
4237 }
4238
4239 static bool
4240 aapcs_vfp_is_call_candidate (CUMULATIVE_ARGS *pcum, enum machine_mode mode, 
4241                              const_tree type)
4242 {
4243   if (!use_vfp_abi (pcum->pcs_variant, false))
4244     return false;
4245
4246   return aapcs_vfp_is_call_or_return_candidate (pcum->pcs_variant, mode, type,
4247                                                 &pcum->aapcs_vfp_rmode,
4248                                                 &pcum->aapcs_vfp_rcount);
4249 }
4250
4251 static bool
4252 aapcs_vfp_allocate (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4253                     const_tree type  ATTRIBUTE_UNUSED)
4254 {
4255   int shift = GET_MODE_SIZE (pcum->aapcs_vfp_rmode) / GET_MODE_SIZE (SFmode);
4256   unsigned mask = (1 << (shift * pcum->aapcs_vfp_rcount)) - 1;
4257   int regno;
4258   
4259   for (regno = 0; regno < NUM_VFP_ARG_REGS; regno += shift)
4260     if (((pcum->aapcs_vfp_regs_free >> regno) & mask) == mask)
4261       {
4262         pcum->aapcs_vfp_reg_alloc = mask << regno;
4263         if (mode == BLKmode || (mode == TImode && !TARGET_NEON))
4264           {
4265             int i;
4266             int rcount = pcum->aapcs_vfp_rcount;
4267             int rshift = shift;
4268             enum machine_mode rmode = pcum->aapcs_vfp_rmode;
4269             rtx par;
4270             if (!TARGET_NEON)
4271               {
4272                 /* Avoid using unsupported vector modes.  */
4273                 if (rmode == V2SImode)
4274                   rmode = DImode;
4275                 else if (rmode == V4SImode)
4276                   {
4277                     rmode = DImode;
4278                     rcount *= 2;
4279                     rshift /= 2;
4280                   }
4281               }
4282             par = gen_rtx_PARALLEL (mode, rtvec_alloc (rcount));
4283             for (i = 0; i < rcount; i++)
4284               {
4285                 rtx tmp = gen_rtx_REG (rmode, 
4286                                        FIRST_VFP_REGNUM + regno + i * rshift);
4287                 tmp = gen_rtx_EXPR_LIST
4288                   (VOIDmode, tmp, 
4289                    GEN_INT (i * GET_MODE_SIZE (rmode)));
4290                 XVECEXP (par, 0, i) = tmp;
4291               }
4292
4293             pcum->aapcs_reg = par;
4294           }
4295         else
4296           pcum->aapcs_reg = gen_rtx_REG (mode, FIRST_VFP_REGNUM + regno);
4297         return true;
4298       }
4299   return false;
4300 }
4301
4302 static rtx
4303 aapcs_vfp_allocate_return_reg (enum arm_pcs pcs_variant ATTRIBUTE_UNUSED,
4304                                enum machine_mode mode,
4305                                const_tree type ATTRIBUTE_UNUSED)
4306 {
4307   if (!use_vfp_abi (pcs_variant, false))
4308     return NULL;
4309
4310   if (mode == BLKmode || (mode == TImode && !TARGET_NEON))
4311     {
4312       int count;
4313       enum machine_mode ag_mode;
4314       int i;
4315       rtx par;
4316       int shift;
4317       
4318       aapcs_vfp_is_call_or_return_candidate (pcs_variant, mode, type,
4319                                              &ag_mode, &count);
4320
4321       if (!TARGET_NEON)
4322         {
4323           if (ag_mode == V2SImode)
4324             ag_mode = DImode;
4325           else if (ag_mode == V4SImode)
4326             {
4327               ag_mode = DImode;
4328               count *= 2;
4329             }
4330         }
4331       shift = GET_MODE_SIZE(ag_mode) / GET_MODE_SIZE(SFmode);
4332       par = gen_rtx_PARALLEL (mode, rtvec_alloc (count));
4333       for (i = 0; i < count; i++)
4334         {
4335           rtx tmp = gen_rtx_REG (ag_mode, FIRST_VFP_REGNUM + i * shift);
4336           tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, 
4337                                    GEN_INT (i * GET_MODE_SIZE (ag_mode)));
4338           XVECEXP (par, 0, i) = tmp;
4339         }
4340
4341       return par;
4342     }
4343
4344   return gen_rtx_REG (mode, FIRST_VFP_REGNUM);
4345 }
4346
4347 static void
4348 aapcs_vfp_advance (CUMULATIVE_ARGS *pcum  ATTRIBUTE_UNUSED,
4349                    enum machine_mode mode  ATTRIBUTE_UNUSED,
4350                    const_tree type  ATTRIBUTE_UNUSED)
4351 {
4352   pcum->aapcs_vfp_regs_free &= ~pcum->aapcs_vfp_reg_alloc;
4353   pcum->aapcs_vfp_reg_alloc = 0;
4354   return;
4355 }
4356
4357 #define AAPCS_CP(X)                             \
4358   {                                             \
4359     aapcs_ ## X ## _cum_init,                   \
4360     aapcs_ ## X ## _is_call_candidate,          \
4361     aapcs_ ## X ## _allocate,                   \
4362     aapcs_ ## X ## _is_return_candidate,        \
4363     aapcs_ ## X ## _allocate_return_reg,        \
4364     aapcs_ ## X ## _advance                     \
4365   }
4366
4367 /* Table of co-processors that can be used to pass arguments in
4368    registers.  Idealy no arugment should be a candidate for more than
4369    one co-processor table entry, but the table is processed in order
4370    and stops after the first match.  If that entry then fails to put
4371    the argument into a co-processor register, the argument will go on
4372    the stack.  */
4373 static struct 
4374 {
4375   /* Initialize co-processor related state in CUMULATIVE_ARGS structure.  */
4376   void (*cum_init) (CUMULATIVE_ARGS *, const_tree, rtx, const_tree);
4377
4378   /* Return true if an argument of mode MODE (or type TYPE if MODE is
4379      BLKmode) is a candidate for this co-processor's registers; this
4380      function should ignore any position-dependent state in
4381      CUMULATIVE_ARGS and only use call-type dependent information.  */
4382   bool (*is_call_candidate) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4383
4384   /* Return true if the argument does get a co-processor register; it
4385      should set aapcs_reg to an RTX of the register allocated as is
4386      required for a return from FUNCTION_ARG.  */
4387   bool (*allocate) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4388
4389   /* Return true if a result of mode MODE (or type TYPE if MODE is
4390      BLKmode) is can be returned in this co-processor's registers.  */
4391   bool (*is_return_candidate) (enum arm_pcs, enum machine_mode, const_tree);
4392
4393   /* Allocate and return an RTX element to hold the return type of a
4394      call, this routine must not fail and will only be called if
4395      is_return_candidate returned true with the same parameters.  */
4396   rtx (*allocate_return_reg) (enum arm_pcs, enum machine_mode, const_tree);
4397
4398   /* Finish processing this argument and prepare to start processing
4399      the next one.  */
4400   void (*advance) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4401 } aapcs_cp_arg_layout[ARM_NUM_COPROC_SLOTS] =
4402   {
4403     AAPCS_CP(vfp)
4404   };
4405
4406 #undef AAPCS_CP
4407
4408 static int
4409 aapcs_select_call_coproc (CUMULATIVE_ARGS *pcum, enum machine_mode mode, 
4410                           const_tree type)
4411 {
4412   int i;
4413
4414   for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4415     if (aapcs_cp_arg_layout[i].is_call_candidate (pcum, mode, type))
4416       return i;
4417
4418   return -1;
4419 }
4420
4421 static int
4422 aapcs_select_return_coproc (const_tree type, const_tree fntype)
4423 {
4424   /* We aren't passed a decl, so we can't check that a call is local.
4425      However, it isn't clear that that would be a win anyway, since it
4426      might limit some tail-calling opportunities.  */
4427   enum arm_pcs pcs_variant;
4428
4429   if (fntype)
4430     {
4431       const_tree fndecl = NULL_TREE;
4432
4433       if (TREE_CODE (fntype) == FUNCTION_DECL)
4434         {
4435           fndecl = fntype;
4436           fntype = TREE_TYPE (fntype);
4437         }
4438
4439       pcs_variant = arm_get_pcs_model (fntype, fndecl);
4440     }
4441   else
4442     pcs_variant = arm_pcs_default;
4443
4444   if (pcs_variant != ARM_PCS_AAPCS)
4445     {
4446       int i;
4447
4448       for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4449         if (aapcs_cp_arg_layout[i].is_return_candidate (pcs_variant, 
4450                                                         TYPE_MODE (type),
4451                                                         type))
4452           return i;
4453     }
4454   return -1;
4455 }
4456
4457 static rtx
4458 aapcs_allocate_return_reg (enum machine_mode mode, const_tree type,
4459                            const_tree fntype)
4460 {
4461   /* We aren't passed a decl, so we can't check that a call is local.
4462      However, it isn't clear that that would be a win anyway, since it
4463      might limit some tail-calling opportunities.  */
4464   enum arm_pcs pcs_variant;
4465   int unsignedp ATTRIBUTE_UNUSED;
4466
4467   if (fntype)
4468     {
4469       const_tree fndecl = NULL_TREE;
4470
4471       if (TREE_CODE (fntype) == FUNCTION_DECL)
4472         {
4473           fndecl = fntype;
4474           fntype = TREE_TYPE (fntype);
4475         }
4476
4477       pcs_variant = arm_get_pcs_model (fntype, fndecl);
4478     }
4479   else
4480     pcs_variant = arm_pcs_default;
4481
4482   /* Promote integer types.  */
4483   if (type && INTEGRAL_TYPE_P (type))
4484     mode = arm_promote_function_mode (type, mode, &unsignedp, fntype, 1);
4485
4486   if (pcs_variant != ARM_PCS_AAPCS)
4487     {
4488       int i;
4489
4490       for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4491         if (aapcs_cp_arg_layout[i].is_return_candidate (pcs_variant, mode,
4492                                                         type))
4493           return aapcs_cp_arg_layout[i].allocate_return_reg (pcs_variant,
4494                                                              mode, type);
4495     }
4496
4497   /* Promotes small structs returned in a register to full-word size
4498      for big-endian AAPCS.  */
4499   if (type && arm_return_in_msb (type))
4500     {
4501       HOST_WIDE_INT size = int_size_in_bytes (type);
4502       if (size % UNITS_PER_WORD != 0)
4503         {
4504           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
4505           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
4506         }
4507     }
4508
4509   return gen_rtx_REG (mode, R0_REGNUM);
4510 }
4511
4512 rtx
4513 aapcs_libcall_value (enum machine_mode mode)
4514 {
4515   if (BYTES_BIG_ENDIAN && ALL_FIXED_POINT_MODE_P (mode)
4516       && GET_MODE_SIZE (mode) <= 4)
4517     mode = SImode;
4518
4519   return aapcs_allocate_return_reg (mode, NULL_TREE, NULL_TREE);
4520 }
4521
4522 /* Lay out a function argument using the AAPCS rules.  The rule
4523    numbers referred to here are those in the AAPCS.  */
4524 static void
4525 aapcs_layout_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4526                   const_tree type, bool named)
4527 {
4528   int nregs, nregs2;
4529   int ncrn;
4530
4531   /* We only need to do this once per argument.  */
4532   if (pcum->aapcs_arg_processed)
4533     return;
4534
4535   pcum->aapcs_arg_processed = true;
4536
4537   /* Special case: if named is false then we are handling an incoming
4538      anonymous argument which is on the stack.  */
4539   if (!named)
4540     return;
4541   
4542   /* Is this a potential co-processor register candidate?  */
4543   if (pcum->pcs_variant != ARM_PCS_AAPCS)
4544     {
4545       int slot = aapcs_select_call_coproc (pcum, mode, type);
4546       pcum->aapcs_cprc_slot = slot;
4547
4548       /* We don't have to apply any of the rules from part B of the
4549          preparation phase, these are handled elsewhere in the
4550          compiler.  */
4551
4552       if (slot >= 0)
4553         {
4554           /* A Co-processor register candidate goes either in its own
4555              class of registers or on the stack.  */
4556           if (!pcum->aapcs_cprc_failed[slot])
4557             {
4558               /* C1.cp - Try to allocate the argument to co-processor
4559                  registers.  */
4560               if (aapcs_cp_arg_layout[slot].allocate (pcum, mode, type))
4561                 return;
4562
4563               /* C2.cp - Put the argument on the stack and note that we
4564                  can't assign any more candidates in this slot.  We also
4565                  need to note that we have allocated stack space, so that
4566                  we won't later try to split a non-cprc candidate between
4567                  core registers and the stack.  */
4568               pcum->aapcs_cprc_failed[slot] = true;
4569               pcum->can_split = false;
4570             }
4571
4572           /* We didn't get a register, so this argument goes on the
4573              stack.  */
4574           gcc_assert (pcum->can_split == false);
4575           return;
4576         }
4577     }
4578
4579   /* C3 - For double-word aligned arguments, round the NCRN up to the
4580      next even number.  */
4581   ncrn = pcum->aapcs_ncrn;
4582   if ((ncrn & 1) && arm_needs_doubleword_align (mode, type))
4583     ncrn++;
4584
4585   nregs = ARM_NUM_REGS2(mode, type);
4586
4587   /* Sigh, this test should really assert that nregs > 0, but a GCC
4588      extension allows empty structs and then gives them empty size; it
4589      then allows such a structure to be passed by value.  For some of
4590      the code below we have to pretend that such an argument has
4591      non-zero size so that we 'locate' it correctly either in
4592      registers or on the stack.  */
4593   gcc_assert (nregs >= 0);
4594
4595   nregs2 = nregs ? nregs : 1;
4596
4597   /* C4 - Argument fits entirely in core registers.  */
4598   if (ncrn + nregs2 <= NUM_ARG_REGS)
4599     {
4600       pcum->aapcs_reg = gen_rtx_REG (mode, ncrn);
4601       pcum->aapcs_next_ncrn = ncrn + nregs;
4602       return;
4603     }
4604
4605   /* C5 - Some core registers left and there are no arguments already
4606      on the stack: split this argument between the remaining core
4607      registers and the stack.  */
4608   if (ncrn < NUM_ARG_REGS && pcum->can_split)
4609     {
4610       pcum->aapcs_reg = gen_rtx_REG (mode, ncrn);
4611       pcum->aapcs_next_ncrn = NUM_ARG_REGS;
4612       pcum->aapcs_partial = (NUM_ARG_REGS - ncrn) * UNITS_PER_WORD;
4613       return;
4614     }
4615
4616   /* C6 - NCRN is set to 4.  */
4617   pcum->aapcs_next_ncrn = NUM_ARG_REGS;
4618
4619   /* C7,C8 - arugment goes on the stack.  We have nothing to do here.  */
4620   return;
4621 }
4622
4623 /* Initialize a variable CUM of type CUMULATIVE_ARGS
4624    for a call to a function whose data type is FNTYPE.
4625    For a library call, FNTYPE is NULL.  */
4626 void
4627 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
4628                           rtx libname,
4629                           tree fndecl ATTRIBUTE_UNUSED)
4630 {
4631   /* Long call handling.  */
4632   if (fntype)
4633     pcum->pcs_variant = arm_get_pcs_model (fntype, fndecl);
4634   else
4635     pcum->pcs_variant = arm_pcs_default;
4636
4637   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4638     {
4639       if (arm_libcall_uses_aapcs_base (libname))
4640         pcum->pcs_variant = ARM_PCS_AAPCS;
4641  
4642       pcum->aapcs_ncrn = pcum->aapcs_next_ncrn = 0;
4643       pcum->aapcs_reg = NULL_RTX;
4644       pcum->aapcs_partial = 0;
4645       pcum->aapcs_arg_processed = false;
4646       pcum->aapcs_cprc_slot = -1;
4647       pcum->can_split = true;
4648
4649       if (pcum->pcs_variant != ARM_PCS_AAPCS)
4650         {
4651           int i;
4652
4653           for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4654             {
4655               pcum->aapcs_cprc_failed[i] = false;
4656               aapcs_cp_arg_layout[i].cum_init (pcum, fntype, libname, fndecl);
4657             }
4658         }
4659       return;
4660     }
4661
4662   /* Legacy ABIs */
4663
4664   /* On the ARM, the offset starts at 0.  */
4665   pcum->nregs = 0;
4666   pcum->iwmmxt_nregs = 0;
4667   pcum->can_split = true;
4668
4669   /* Varargs vectors are treated the same as long long.
4670      named_count avoids having to change the way arm handles 'named' */
4671   pcum->named_count = 0;
4672   pcum->nargs = 0;
4673
4674   if (TARGET_REALLY_IWMMXT && fntype)
4675     {
4676       tree fn_arg;
4677
4678       for (fn_arg = TYPE_ARG_TYPES (fntype);
4679            fn_arg;
4680            fn_arg = TREE_CHAIN (fn_arg))
4681         pcum->named_count += 1;
4682
4683       if (! pcum->named_count)
4684         pcum->named_count = INT_MAX;
4685     }
4686 }
4687
4688
4689 /* Return true if mode/type need doubleword alignment.  */
4690 static bool
4691 arm_needs_doubleword_align (enum machine_mode mode, const_tree type)
4692 {
4693   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
4694           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
4695 }
4696
4697
4698 /* Determine where to put an argument to a function.
4699    Value is zero to push the argument on the stack,
4700    or a hard register in which to store the argument.
4701
4702    MODE is the argument's machine mode.
4703    TYPE is the data type of the argument (as a tree).
4704     This is null for libcalls where that information may
4705     not be available.
4706    CUM is a variable of type CUMULATIVE_ARGS which gives info about
4707     the preceding args and about the function being called.
4708    NAMED is nonzero if this argument is a named parameter
4709     (otherwise it is an extra parameter matching an ellipsis).
4710
4711    On the ARM, normally the first 16 bytes are passed in registers r0-r3; all
4712    other arguments are passed on the stack.  If (NAMED == 0) (which happens
4713    only in assign_parms, since TARGET_SETUP_INCOMING_VARARGS is
4714    defined), say it is passed in the stack (function_prologue will
4715    indeed make it pass in the stack if necessary).  */
4716
4717 static rtx
4718 arm_function_arg (cumulative_args_t pcum_v, enum machine_mode mode,
4719                   const_tree type, bool named)
4720 {
4721   CUMULATIVE_ARGS *pcum = get_cumulative_args (pcum_v);
4722   int nregs;
4723
4724   /* Handle the special case quickly.  Pick an arbitrary value for op2 of
4725      a call insn (op3 of a call_value insn).  */
4726   if (mode == VOIDmode)
4727     return const0_rtx;
4728
4729   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4730     {
4731       aapcs_layout_arg (pcum, mode, type, named);
4732       return pcum->aapcs_reg;
4733     }
4734
4735   /* Varargs vectors are treated the same as long long.
4736      named_count avoids having to change the way arm handles 'named' */
4737   if (TARGET_IWMMXT_ABI
4738       && arm_vector_mode_supported_p (mode)
4739       && pcum->named_count > pcum->nargs + 1)
4740     {
4741       if (pcum->iwmmxt_nregs <= 9)
4742         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
4743       else
4744         {
4745           pcum->can_split = false;
4746           return NULL_RTX;
4747         }
4748     }
4749
4750   /* Put doubleword aligned quantities in even register pairs.  */
4751   if (pcum->nregs & 1
4752       && ARM_DOUBLEWORD_ALIGN
4753       && arm_needs_doubleword_align (mode, type))
4754     pcum->nregs++;
4755
4756   /* Only allow splitting an arg between regs and memory if all preceding
4757      args were allocated to regs.  For args passed by reference we only count
4758      the reference pointer.  */
4759   if (pcum->can_split)
4760     nregs = 1;
4761   else
4762     nregs = ARM_NUM_REGS2 (mode, type);
4763
4764   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
4765     return NULL_RTX;
4766
4767   return gen_rtx_REG (mode, pcum->nregs);
4768 }
4769
4770 static unsigned int
4771 arm_function_arg_boundary (enum machine_mode mode, const_tree type)
4772 {
4773   return (ARM_DOUBLEWORD_ALIGN && arm_needs_doubleword_align (mode, type)
4774           ? DOUBLEWORD_ALIGNMENT
4775           : PARM_BOUNDARY);
4776 }
4777
4778 static int
4779 arm_arg_partial_bytes (cumulative_args_t pcum_v, enum machine_mode mode,
4780                        tree type, bool named)
4781 {
4782   CUMULATIVE_ARGS *pcum = get_cumulative_args (pcum_v);
4783   int nregs = pcum->nregs;
4784
4785   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4786     {
4787       aapcs_layout_arg (pcum, mode, type, named);
4788       return pcum->aapcs_partial;
4789     }
4790
4791   if (TARGET_IWMMXT_ABI && arm_vector_mode_supported_p (mode))
4792     return 0;
4793
4794   if (NUM_ARG_REGS > nregs
4795       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
4796       && pcum->can_split)
4797     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
4798
4799   return 0;
4800 }
4801
4802 /* Update the data in PCUM to advance over an argument
4803    of mode MODE and data type TYPE.
4804    (TYPE is null for libcalls where that information may not be available.)  */
4805
4806 static void
4807 arm_function_arg_advance (cumulative_args_t pcum_v, enum machine_mode mode,
4808                           const_tree type, bool named)
4809 {
4810   CUMULATIVE_ARGS *pcum = get_cumulative_args (pcum_v);
4811
4812   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4813     {
4814       aapcs_layout_arg (pcum, mode, type, named);
4815
4816       if (pcum->aapcs_cprc_slot >= 0)
4817         {
4818           aapcs_cp_arg_layout[pcum->aapcs_cprc_slot].advance (pcum, mode,
4819                                                               type);
4820           pcum->aapcs_cprc_slot = -1;
4821         }
4822
4823       /* Generic stuff.  */
4824       pcum->aapcs_arg_processed = false;
4825       pcum->aapcs_ncrn = pcum->aapcs_next_ncrn;
4826       pcum->aapcs_reg = NULL_RTX;
4827       pcum->aapcs_partial = 0;
4828     }
4829   else
4830     {
4831       pcum->nargs += 1;
4832       if (arm_vector_mode_supported_p (mode)
4833           && pcum->named_count > pcum->nargs
4834           && TARGET_IWMMXT_ABI)
4835         pcum->iwmmxt_nregs += 1;
4836       else
4837         pcum->nregs += ARM_NUM_REGS2 (mode, type);
4838     }
4839 }
4840
4841 /* Variable sized types are passed by reference.  This is a GCC
4842    extension to the ARM ABI.  */
4843
4844 static bool
4845 arm_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
4846                        enum machine_mode mode ATTRIBUTE_UNUSED,
4847                        const_tree type, bool named ATTRIBUTE_UNUSED)
4848 {
4849   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
4850 }
4851 \f
4852 /* Encode the current state of the #pragma [no_]long_calls.  */
4853 typedef enum
4854 {
4855   OFF,          /* No #pragma [no_]long_calls is in effect.  */
4856   LONG,         /* #pragma long_calls is in effect.  */
4857   SHORT         /* #pragma no_long_calls is in effect.  */
4858 } arm_pragma_enum;
4859
4860 static arm_pragma_enum arm_pragma_long_calls = OFF;
4861
4862 void
4863 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
4864 {
4865   arm_pragma_long_calls = LONG;
4866 }
4867
4868 void
4869 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
4870 {
4871   arm_pragma_long_calls = SHORT;
4872 }
4873
4874 void
4875 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
4876 {
4877   arm_pragma_long_calls = OFF;
4878 }
4879 \f
4880 /* Handle an attribute requiring a FUNCTION_DECL;
4881    arguments as in struct attribute_spec.handler.  */
4882 static tree
4883 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
4884                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4885 {
4886   if (TREE_CODE (*node) != FUNCTION_DECL)
4887     {
4888       warning (OPT_Wattributes, "%qE attribute only applies to functions",
4889                name);
4890       *no_add_attrs = true;
4891     }
4892
4893   return NULL_TREE;
4894 }
4895
4896 /* Handle an "interrupt" or "isr" attribute;
4897    arguments as in struct attribute_spec.handler.  */
4898 static tree
4899 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
4900                           bool *no_add_attrs)
4901 {
4902   if (DECL_P (*node))
4903     {
4904       if (TREE_CODE (*node) != FUNCTION_DECL)
4905         {
4906           warning (OPT_Wattributes, "%qE attribute only applies to functions",
4907                    name);
4908           *no_add_attrs = true;
4909         }
4910       /* FIXME: the argument if any is checked for type attributes;
4911          should it be checked for decl ones?  */
4912     }
4913   else
4914     {
4915       if (TREE_CODE (*node) == FUNCTION_TYPE
4916           || TREE_CODE (*node) == METHOD_TYPE)
4917         {
4918           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
4919             {
4920               warning (OPT_Wattributes, "%qE attribute ignored",
4921                        name);
4922               *no_add_attrs = true;
4923             }
4924         }
4925       else if (TREE_CODE (*node) == POINTER_TYPE
4926                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
4927                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
4928                && arm_isr_value (args) != ARM_FT_UNKNOWN)
4929         {
4930           *node = build_variant_type_copy (*node);
4931           TREE_TYPE (*node) = build_type_attribute_variant
4932             (TREE_TYPE (*node),
4933              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
4934           *no_add_attrs = true;
4935         }
4936       else
4937         {
4938           /* Possibly pass this attribute on from the type to a decl.  */
4939           if (flags & ((int) ATTR_FLAG_DECL_NEXT
4940                        | (int) ATTR_FLAG_FUNCTION_NEXT
4941                        | (int) ATTR_FLAG_ARRAY_NEXT))
4942             {
4943               *no_add_attrs = true;
4944               return tree_cons (name, args, NULL_TREE);
4945             }
4946           else
4947             {
4948               warning (OPT_Wattributes, "%qE attribute ignored",
4949                        name);
4950             }
4951         }
4952     }
4953
4954   return NULL_TREE;
4955 }
4956
4957 /* Handle a "pcs" attribute; arguments as in struct
4958    attribute_spec.handler.  */
4959 static tree
4960 arm_handle_pcs_attribute (tree *node ATTRIBUTE_UNUSED, tree name, tree args,
4961                           int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4962 {
4963   if (arm_pcs_from_attribute (args) == ARM_PCS_UNKNOWN)
4964     {
4965       warning (OPT_Wattributes, "%qE attribute ignored", name);
4966       *no_add_attrs = true;
4967     }
4968   return NULL_TREE;
4969 }
4970
4971 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
4972 /* Handle the "notshared" attribute.  This attribute is another way of
4973    requesting hidden visibility.  ARM's compiler supports
4974    "__declspec(notshared)"; we support the same thing via an
4975    attribute.  */
4976
4977 static tree
4978 arm_handle_notshared_attribute (tree *node,
4979                                 tree name ATTRIBUTE_UNUSED,
4980                                 tree args ATTRIBUTE_UNUSED,
4981                                 int flags ATTRIBUTE_UNUSED,
4982                                 bool *no_add_attrs)
4983 {
4984   tree decl = TYPE_NAME (*node);
4985
4986   if (decl)
4987     {
4988       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
4989       DECL_VISIBILITY_SPECIFIED (decl) = 1;
4990       *no_add_attrs = false;
4991     }
4992   return NULL_TREE;
4993 }
4994 #endif
4995
4996 /* Return 0 if the attributes for two types are incompatible, 1 if they
4997    are compatible, and 2 if they are nearly compatible (which causes a
4998    warning to be generated).  */
4999 static int
5000 arm_comp_type_attributes (const_tree type1, const_tree type2)
5001 {
5002   int l1, l2, s1, s2;
5003
5004   /* Check for mismatch of non-default calling convention.  */
5005   if (TREE_CODE (type1) != FUNCTION_TYPE)
5006     return 1;
5007
5008   /* Check for mismatched call attributes.  */
5009   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
5010   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
5011   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
5012   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
5013
5014   /* Only bother to check if an attribute is defined.  */
5015   if (l1 | l2 | s1 | s2)
5016     {
5017       /* If one type has an attribute, the other must have the same attribute.  */
5018       if ((l1 != l2) || (s1 != s2))
5019         return 0;
5020
5021       /* Disallow mixed attributes.  */
5022       if ((l1 & s2) || (l2 & s1))
5023         return 0;
5024     }
5025
5026   /* Check for mismatched ISR attribute.  */
5027   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
5028   if (! l1)
5029     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
5030   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
5031   if (! l2)
5032     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
5033   if (l1 != l2)
5034     return 0;
5035
5036   return 1;
5037 }
5038
5039 /*  Assigns default attributes to newly defined type.  This is used to
5040     set short_call/long_call attributes for function types of
5041     functions defined inside corresponding #pragma scopes.  */
5042 static void
5043 arm_set_default_type_attributes (tree type)
5044 {
5045   /* Add __attribute__ ((long_call)) to all functions, when
5046      inside #pragma long_calls or __attribute__ ((short_call)),
5047      when inside #pragma no_long_calls.  */
5048   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
5049     {
5050       tree type_attr_list, attr_name;
5051       type_attr_list = TYPE_ATTRIBUTES (type);
5052
5053       if (arm_pragma_long_calls == LONG)
5054         attr_name = get_identifier ("long_call");
5055       else if (arm_pragma_long_calls == SHORT)
5056         attr_name = get_identifier ("short_call");
5057       else
5058         return;
5059
5060       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
5061       TYPE_ATTRIBUTES (type) = type_attr_list;
5062     }
5063 }
5064 \f
5065 /* Return true if DECL is known to be linked into section SECTION.  */
5066
5067 static bool
5068 arm_function_in_section_p (tree decl, section *section)
5069 {
5070   /* We can only be certain about functions defined in the same
5071      compilation unit.  */
5072   if (!TREE_STATIC (decl))
5073     return false;
5074
5075   /* Make sure that SYMBOL always binds to the definition in this
5076      compilation unit.  */
5077   if (!targetm.binds_local_p (decl))
5078     return false;
5079
5080   /* If DECL_SECTION_NAME is set, assume it is trustworthy.  */
5081   if (!DECL_SECTION_NAME (decl))
5082     {
5083       /* Make sure that we will not create a unique section for DECL.  */
5084       if (flag_function_sections || DECL_ONE_ONLY (decl))
5085         return false;
5086     }
5087
5088   return function_section (decl) == section;
5089 }
5090
5091 /* Return nonzero if a 32-bit "long_call" should be generated for
5092    a call from the current function to DECL.  We generate a long_call
5093    if the function:
5094
5095         a.  has an __attribute__((long call))
5096      or b.  is within the scope of a #pragma long_calls
5097      or c.  the -mlong-calls command line switch has been specified
5098
5099    However we do not generate a long call if the function:
5100
5101         d.  has an __attribute__ ((short_call))
5102      or e.  is inside the scope of a #pragma no_long_calls
5103      or f.  is defined in the same section as the current function.  */
5104
5105 bool
5106 arm_is_long_call_p (tree decl)
5107 {
5108   tree attrs;
5109
5110   if (!decl)
5111     return TARGET_LONG_CALLS;
5112
5113   attrs = TYPE_ATTRIBUTES (TREE_TYPE (decl));
5114   if (lookup_attribute ("short_call", attrs))
5115     return false;
5116
5117   /* For "f", be conservative, and only cater for cases in which the
5118      whole of the current function is placed in the same section.  */
5119   if (!flag_reorder_blocks_and_partition
5120       && TREE_CODE (decl) == FUNCTION_DECL
5121       && arm_function_in_section_p (decl, current_function_section ()))
5122     return false;
5123
5124   if (lookup_attribute ("long_call", attrs))
5125     return true;
5126
5127   return TARGET_LONG_CALLS;
5128 }
5129
5130 /* Return nonzero if it is ok to make a tail-call to DECL.  */
5131 static bool
5132 arm_function_ok_for_sibcall (tree decl, tree exp)
5133 {
5134   unsigned long func_type;
5135
5136   if (cfun->machine->sibcall_blocked)
5137     return false;
5138
5139   /* Never tailcall something for which we have no decl, or if we
5140      are generating code for Thumb-1.  */
5141   if (decl == NULL || TARGET_THUMB1)
5142     return false;
5143
5144   /* The PIC register is live on entry to VxWorks PLT entries, so we
5145      must make the call before restoring the PIC register.  */
5146   if (TARGET_VXWORKS_RTP && flag_pic && !targetm.binds_local_p (decl))
5147     return false;
5148
5149   /* Cannot tail-call to long calls, since these are out of range of
5150      a branch instruction.  */
5151   if (arm_is_long_call_p (decl))
5152     return false;
5153
5154   /* If we are interworking and the function is not declared static
5155      then we can't tail-call it unless we know that it exists in this
5156      compilation unit (since it might be a Thumb routine).  */
5157   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
5158     return false;
5159
5160   func_type = arm_current_func_type ();
5161   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
5162   if (IS_INTERRUPT (func_type))
5163     return false;
5164
5165   if (!VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
5166     {
5167       /* Check that the return value locations are the same.  For
5168          example that we aren't returning a value from the sibling in
5169          a VFP register but then need to transfer it to a core
5170          register.  */
5171       rtx a, b;
5172
5173       a = arm_function_value (TREE_TYPE (exp), decl, false);
5174       b = arm_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
5175                               cfun->decl, false);
5176       if (!rtx_equal_p (a, b))
5177         return false;
5178     }
5179
5180   /* Never tailcall if function may be called with a misaligned SP.  */
5181   if (IS_STACKALIGN (func_type))
5182     return false;
5183
5184   /* Everything else is ok.  */
5185   return true;
5186 }
5187
5188 \f
5189 /* Addressing mode support functions.  */
5190
5191 /* Return nonzero if X is a legitimate immediate operand when compiling
5192    for PIC.  We know that X satisfies CONSTANT_P and flag_pic is true.  */
5193 int
5194 legitimate_pic_operand_p (rtx x)
5195 {
5196   if (GET_CODE (x) == SYMBOL_REF
5197       || (GET_CODE (x) == CONST
5198           && GET_CODE (XEXP (x, 0)) == PLUS
5199           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
5200     return 0;
5201
5202   return 1;
5203 }
5204
5205 /* Record that the current function needs a PIC register.  Initialize
5206    cfun->machine->pic_reg if we have not already done so.  */
5207
5208 static void
5209 require_pic_register (void)
5210 {
5211   /* A lot of the logic here is made obscure by the fact that this
5212      routine gets called as part of the rtx cost estimation process.
5213      We don't want those calls to affect any assumptions about the real
5214      function; and further, we can't call entry_of_function() until we
5215      start the real expansion process.  */
5216   if (!crtl->uses_pic_offset_table)
5217     {
5218       gcc_assert (can_create_pseudo_p ());
5219       if (arm_pic_register != INVALID_REGNUM)
5220         {
5221           if (!cfun->machine->pic_reg)
5222             cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
5223
5224           /* Play games to avoid marking the function as needing pic
5225              if we are being called as part of the cost-estimation
5226              process.  */
5227           if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
5228             crtl->uses_pic_offset_table = 1;
5229         }
5230       else
5231         {
5232           rtx seq, insn;
5233
5234           if (!cfun->machine->pic_reg)
5235             cfun->machine->pic_reg = gen_reg_rtx (Pmode);
5236
5237           /* Play games to avoid marking the function as needing pic
5238              if we are being called as part of the cost-estimation
5239              process.  */
5240           if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
5241             {
5242               crtl->uses_pic_offset_table = 1;
5243               start_sequence ();
5244
5245               arm_load_pic_register (0UL);
5246
5247               seq = get_insns ();
5248               end_sequence ();
5249
5250               for (insn = seq; insn; insn = NEXT_INSN (insn))
5251                 if (INSN_P (insn))
5252                   INSN_LOCATOR (insn) = prologue_locator;
5253
5254               /* We can be called during expansion of PHI nodes, where
5255                  we can't yet emit instructions directly in the final
5256                  insn stream.  Queue the insns on the entry edge, they will
5257                  be committed after everything else is expanded.  */
5258               insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
5259             }
5260         }
5261     }
5262 }
5263
5264 rtx
5265 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
5266 {
5267   if (GET_CODE (orig) == SYMBOL_REF
5268       || GET_CODE (orig) == LABEL_REF)
5269     {
5270       rtx insn;
5271
5272       if (reg == 0)
5273         {
5274           gcc_assert (can_create_pseudo_p ());
5275           reg = gen_reg_rtx (Pmode);
5276         }
5277
5278       /* VxWorks does not impose a fixed gap between segments; the run-time
5279          gap can be different from the object-file gap.  We therefore can't
5280          use GOTOFF unless we are absolutely sure that the symbol is in the
5281          same segment as the GOT.  Unfortunately, the flexibility of linker
5282          scripts means that we can't be sure of that in general, so assume
5283          that GOTOFF is never valid on VxWorks.  */
5284       if ((GET_CODE (orig) == LABEL_REF
5285            || (GET_CODE (orig) == SYMBOL_REF &&
5286                SYMBOL_REF_LOCAL_P (orig)))
5287           && NEED_GOT_RELOC
5288           && !TARGET_VXWORKS_RTP)
5289         insn = arm_pic_static_addr (orig, reg);
5290       else
5291         {
5292           rtx pat;
5293           rtx mem;
5294
5295           /* If this function doesn't have a pic register, create one now.  */
5296           require_pic_register ();
5297
5298           pat = gen_calculate_pic_address (reg, cfun->machine->pic_reg, orig);
5299
5300           /* Make the MEM as close to a constant as possible.  */
5301           mem = SET_SRC (pat);
5302           gcc_assert (MEM_P (mem) && !MEM_VOLATILE_P (mem));
5303           MEM_READONLY_P (mem) = 1;
5304           MEM_NOTRAP_P (mem) = 1;
5305
5306           insn = emit_insn (pat);
5307         }
5308
5309       /* Put a REG_EQUAL note on this insn, so that it can be optimized
5310          by loop.  */
5311       set_unique_reg_note (insn, REG_EQUAL, orig);
5312
5313       return reg;
5314     }
5315   else if (GET_CODE (orig) == CONST)
5316     {
5317       rtx base, offset;
5318
5319       if (GET_CODE (XEXP (orig, 0)) == PLUS
5320           && XEXP (XEXP (orig, 0), 0) == cfun->machine->pic_reg)
5321         return orig;
5322
5323       /* Handle the case where we have: const (UNSPEC_TLS).  */
5324       if (GET_CODE (XEXP (orig, 0)) == UNSPEC
5325           && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
5326         return orig;
5327
5328       /* Handle the case where we have:
5329          const (plus (UNSPEC_TLS) (ADDEND)).  The ADDEND must be a
5330          CONST_INT.  */
5331       if (GET_CODE (XEXP (orig, 0)) == PLUS
5332           && GET_CODE (XEXP (XEXP (orig, 0), 0)) == UNSPEC
5333           && XINT (XEXP (XEXP (orig, 0), 0), 1) == UNSPEC_TLS)
5334         {
5335           gcc_assert (GET_CODE (XEXP (XEXP (orig, 0), 1)) == CONST_INT);
5336           return orig;
5337         }
5338
5339       if (reg == 0)
5340         {
5341           gcc_assert (can_create_pseudo_p ());
5342           reg = gen_reg_rtx (Pmode);
5343         }
5344
5345       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
5346
5347       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
5348       offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
5349                                        base == reg ? 0 : reg);
5350
5351       if (GET_CODE (offset) == CONST_INT)
5352         {
5353           /* The base register doesn't really matter, we only want to
5354              test the index for the appropriate mode.  */
5355           if (!arm_legitimate_index_p (mode, offset, SET, 0))
5356             {
5357               gcc_assert (can_create_pseudo_p ());
5358               offset = force_reg (Pmode, offset);
5359             }
5360
5361           if (GET_CODE (offset) == CONST_INT)
5362             return plus_constant (base, INTVAL (offset));
5363         }
5364
5365       if (GET_MODE_SIZE (mode) > 4
5366           && (GET_MODE_CLASS (mode) == MODE_INT
5367               || TARGET_SOFT_FLOAT))
5368         {
5369           emit_insn (gen_addsi3 (reg, base, offset));
5370           return reg;
5371         }
5372
5373       return gen_rtx_PLUS (Pmode, base, offset);
5374     }
5375
5376   return orig;
5377 }
5378
5379
5380 /* Find a spare register to use during the prolog of a function.  */
5381
5382 static int
5383 thumb_find_work_register (unsigned long pushed_regs_mask)
5384 {
5385   int reg;
5386
5387   /* Check the argument registers first as these are call-used.  The
5388      register allocation order means that sometimes r3 might be used
5389      but earlier argument registers might not, so check them all.  */
5390   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
5391     if (!df_regs_ever_live_p (reg))
5392       return reg;
5393
5394   /* Before going on to check the call-saved registers we can try a couple
5395      more ways of deducing that r3 is available.  The first is when we are
5396      pushing anonymous arguments onto the stack and we have less than 4
5397      registers worth of fixed arguments(*).  In this case r3 will be part of
5398      the variable argument list and so we can be sure that it will be
5399      pushed right at the start of the function.  Hence it will be available
5400      for the rest of the prologue.
5401      (*): ie crtl->args.pretend_args_size is greater than 0.  */
5402   if (cfun->machine->uses_anonymous_args
5403       && crtl->args.pretend_args_size > 0)
5404     return LAST_ARG_REGNUM;
5405
5406   /* The other case is when we have fixed arguments but less than 4 registers
5407      worth.  In this case r3 might be used in the body of the function, but
5408      it is not being used to convey an argument into the function.  In theory
5409      we could just check crtl->args.size to see how many bytes are
5410      being passed in argument registers, but it seems that it is unreliable.
5411      Sometimes it will have the value 0 when in fact arguments are being
5412      passed.  (See testcase execute/20021111-1.c for an example).  So we also
5413      check the args_info.nregs field as well.  The problem with this field is
5414      that it makes no allowances for arguments that are passed to the
5415      function but which are not used.  Hence we could miss an opportunity
5416      when a function has an unused argument in r3.  But it is better to be
5417      safe than to be sorry.  */
5418   if (! cfun->machine->uses_anonymous_args
5419       && crtl->args.size >= 0
5420       && crtl->args.size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
5421       && crtl->args.info.nregs < 4)
5422     return LAST_ARG_REGNUM;
5423
5424   /* Otherwise look for a call-saved register that is going to be pushed.  */
5425   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
5426     if (pushed_regs_mask & (1 << reg))
5427       return reg;
5428
5429   if (TARGET_THUMB2)
5430     {
5431       /* Thumb-2 can use high regs.  */
5432       for (reg = FIRST_HI_REGNUM; reg < 15; reg ++)
5433         if (pushed_regs_mask & (1 << reg))
5434           return reg;
5435     }
5436   /* Something went wrong - thumb_compute_save_reg_mask()
5437      should have arranged for a suitable register to be pushed.  */
5438   gcc_unreachable ();
5439 }
5440
5441 static GTY(()) int pic_labelno;
5442
5443 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
5444    low register.  */
5445
5446 void
5447 arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
5448 {
5449   rtx l1, labelno, pic_tmp, pic_rtx, pic_reg;
5450
5451   if (crtl->uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
5452     return;
5453
5454   gcc_assert (flag_pic);
5455
5456   pic_reg = cfun->machine->pic_reg;
5457   if (TARGET_VXWORKS_RTP)
5458     {
5459       pic_rtx = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE);
5460       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
5461       emit_insn (gen_pic_load_addr_32bit (pic_reg, pic_rtx));
5462
5463       emit_insn (gen_rtx_SET (Pmode, pic_reg, gen_rtx_MEM (Pmode, pic_reg)));
5464
5465       pic_tmp = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
5466       emit_insn (gen_pic_offset_arm (pic_reg, pic_reg, pic_tmp));
5467     }
5468   else
5469     {
5470       /* We use an UNSPEC rather than a LABEL_REF because this label
5471          never appears in the code stream.  */
5472
5473       labelno = GEN_INT (pic_labelno++);
5474       l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
5475       l1 = gen_rtx_CONST (VOIDmode, l1);
5476
5477       /* On the ARM the PC register contains 'dot + 8' at the time of the
5478          addition, on the Thumb it is 'dot + 4'.  */
5479       pic_rtx = plus_constant (l1, TARGET_ARM ? 8 : 4);
5480       pic_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, pic_rtx),
5481                                 UNSPEC_GOTSYM_OFF);
5482       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
5483
5484       if (TARGET_32BIT)
5485         {
5486           emit_insn (gen_pic_load_addr_32bit (pic_reg, pic_rtx));
5487           if (TARGET_ARM)
5488             emit_insn (gen_pic_add_dot_plus_eight (pic_reg, pic_reg, labelno));
5489           else
5490             emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
5491         }
5492       else /* TARGET_THUMB1 */
5493         {
5494           if (arm_pic_register != INVALID_REGNUM
5495               && REGNO (pic_reg) > LAST_LO_REGNUM)
5496             {
5497               /* We will have pushed the pic register, so we should always be
5498                  able to find a work register.  */
5499               pic_tmp = gen_rtx_REG (SImode,
5500                                      thumb_find_work_register (saved_regs));
5501               emit_insn (gen_pic_load_addr_thumb1 (pic_tmp, pic_rtx));
5502               emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
5503             }
5504           else
5505             emit_insn (gen_pic_load_addr_thumb1 (pic_reg, pic_rtx));
5506           emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
5507         }
5508     }
5509
5510   /* Need to emit this whether or not we obey regdecls,
5511      since setjmp/longjmp can cause life info to screw up.  */
5512   emit_use (pic_reg);
5513 }
5514
5515 /* Generate code to load the address of a static var when flag_pic is set.  */
5516 static rtx
5517 arm_pic_static_addr (rtx orig, rtx reg)
5518 {
5519   rtx l1, labelno, offset_rtx, insn;
5520
5521   gcc_assert (flag_pic);
5522
5523   /* We use an UNSPEC rather than a LABEL_REF because this label
5524      never appears in the code stream.  */
5525   labelno = GEN_INT (pic_labelno++);
5526   l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
5527   l1 = gen_rtx_CONST (VOIDmode, l1);
5528
5529   /* On the ARM the PC register contains 'dot + 8' at the time of the
5530      addition, on the Thumb it is 'dot + 4'.  */
5531   offset_rtx = plus_constant (l1, TARGET_ARM ? 8 : 4);
5532   offset_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, orig, offset_rtx),
5533                                UNSPEC_SYMBOL_OFFSET);
5534   offset_rtx = gen_rtx_CONST (Pmode, offset_rtx);
5535
5536   if (TARGET_32BIT)
5537     {
5538       emit_insn (gen_pic_load_addr_32bit (reg, offset_rtx));
5539       if (TARGET_ARM)
5540         insn = emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
5541       else
5542         insn = emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5543     }
5544   else /* TARGET_THUMB1 */
5545     {
5546       emit_insn (gen_pic_load_addr_thumb1 (reg, offset_rtx));
5547       insn = emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5548     }
5549
5550   return insn;
5551 }
5552
5553 /* Return nonzero if X is valid as an ARM state addressing register.  */
5554 static int
5555 arm_address_register_rtx_p (rtx x, int strict_p)
5556 {
5557   int regno;
5558
5559   if (GET_CODE (x) != REG)
5560     return 0;
5561
5562   regno = REGNO (x);
5563
5564   if (strict_p)
5565     return ARM_REGNO_OK_FOR_BASE_P (regno);
5566
5567   return (regno <= LAST_ARM_REGNUM
5568           || regno >= FIRST_PSEUDO_REGISTER
5569           || regno == FRAME_POINTER_REGNUM
5570           || regno == ARG_POINTER_REGNUM);
5571 }
5572
5573 /* Return TRUE if this rtx is the difference of a symbol and a label,
5574    and will reduce to a PC-relative relocation in the object file.
5575    Expressions like this can be left alone when generating PIC, rather
5576    than forced through the GOT.  */
5577 static int
5578 pcrel_constant_p (rtx x)
5579 {
5580   if (GET_CODE (x) == MINUS)
5581     return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
5582
5583   return FALSE;
5584 }
5585
5586 /* Return true if X will surely end up in an index register after next
5587    splitting pass.  */
5588 static bool
5589 will_be_in_index_register (const_rtx x)
5590 {
5591   /* arm.md: calculate_pic_address will split this into a register.  */
5592   return GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_SYM;
5593 }
5594
5595 /* Return nonzero if X is a valid ARM state address operand.  */
5596 int
5597 arm_legitimate_address_outer_p (enum machine_mode mode, rtx x, RTX_CODE outer,
5598                                 int strict_p)
5599 {
5600   bool use_ldrd;
5601   enum rtx_code code = GET_CODE (x);
5602
5603   if (arm_address_register_rtx_p (x, strict_p))
5604     return 1;
5605
5606   use_ldrd = (TARGET_LDRD
5607               && (mode == DImode
5608                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
5609
5610   if (code == POST_INC || code == PRE_DEC
5611       || ((code == PRE_INC || code == POST_DEC)
5612           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
5613     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
5614
5615   else if ((code == POST_MODIFY || code == PRE_MODIFY)
5616            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
5617            && GET_CODE (XEXP (x, 1)) == PLUS
5618            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5619     {
5620       rtx addend = XEXP (XEXP (x, 1), 1);
5621
5622       /* Don't allow ldrd post increment by register because it's hard
5623          to fixup invalid register choices.  */
5624       if (use_ldrd
5625           && GET_CODE (x) == POST_MODIFY
5626           && GET_CODE (addend) == REG)
5627         return 0;
5628
5629       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
5630               && arm_legitimate_index_p (mode, addend, outer, strict_p));
5631     }
5632
5633   /* After reload constants split into minipools will have addresses
5634      from a LABEL_REF.  */
5635   else if (reload_completed
5636            && (code == LABEL_REF
5637                || (code == CONST
5638                    && GET_CODE (XEXP (x, 0)) == PLUS
5639                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5640                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5641     return 1;
5642
5643   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
5644     return 0;
5645
5646   else if (code == PLUS)
5647     {
5648       rtx xop0 = XEXP (x, 0);
5649       rtx xop1 = XEXP (x, 1);
5650
5651       return ((arm_address_register_rtx_p (xop0, strict_p)
5652                && ((GET_CODE(xop1) == CONST_INT
5653                     && arm_legitimate_index_p (mode, xop1, outer, strict_p))
5654                    || (!strict_p && will_be_in_index_register (xop1))))
5655               || (arm_address_register_rtx_p (xop1, strict_p)
5656                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
5657     }
5658
5659 #if 0
5660   /* Reload currently can't handle MINUS, so disable this for now */
5661   else if (GET_CODE (x) == MINUS)
5662     {
5663       rtx xop0 = XEXP (x, 0);
5664       rtx xop1 = XEXP (x, 1);
5665
5666       return (arm_address_register_rtx_p (xop0, strict_p)
5667               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
5668     }
5669 #endif
5670
5671   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
5672            && code == SYMBOL_REF
5673            && CONSTANT_POOL_ADDRESS_P (x)
5674            && ! (flag_pic
5675                  && symbol_mentioned_p (get_pool_constant (x))
5676                  && ! pcrel_constant_p (get_pool_constant (x))))
5677     return 1;
5678
5679   return 0;
5680 }
5681
5682 /* Return nonzero if X is a valid Thumb-2 address operand.  */
5683 static int
5684 thumb2_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
5685 {
5686   bool use_ldrd;
5687   enum rtx_code code = GET_CODE (x);
5688   
5689   if (arm_address_register_rtx_p (x, strict_p))
5690     return 1;
5691
5692   use_ldrd = (TARGET_LDRD
5693               && (mode == DImode
5694                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
5695
5696   if (code == POST_INC || code == PRE_DEC
5697       || ((code == PRE_INC || code == POST_DEC)
5698           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
5699     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
5700
5701   else if ((code == POST_MODIFY || code == PRE_MODIFY)
5702            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
5703            && GET_CODE (XEXP (x, 1)) == PLUS
5704            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5705     {
5706       /* Thumb-2 only has autoincrement by constant.  */
5707       rtx addend = XEXP (XEXP (x, 1), 1);
5708       HOST_WIDE_INT offset;
5709
5710       if (GET_CODE (addend) != CONST_INT)
5711         return 0;
5712
5713       offset = INTVAL(addend);
5714       if (GET_MODE_SIZE (mode) <= 4)
5715         return (offset > -256 && offset < 256);
5716       
5717       return (use_ldrd && offset > -1024 && offset < 1024
5718               && (offset & 3) == 0);
5719     }
5720
5721   /* After reload constants split into minipools will have addresses
5722      from a LABEL_REF.  */
5723   else if (reload_completed
5724            && (code == LABEL_REF
5725                || (code == CONST
5726                    && GET_CODE (XEXP (x, 0)) == PLUS
5727                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5728                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5729     return 1;
5730
5731   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
5732     return 0;
5733
5734   else if (code == PLUS)
5735     {
5736       rtx xop0 = XEXP (x, 0);
5737       rtx xop1 = XEXP (x, 1);
5738
5739       return ((arm_address_register_rtx_p (xop0, strict_p)
5740                && (thumb2_legitimate_index_p (mode, xop1, strict_p)
5741                    || (!strict_p && will_be_in_index_register (xop1))))
5742               || (arm_address_register_rtx_p (xop1, strict_p)
5743                   && thumb2_legitimate_index_p (mode, xop0, strict_p)));
5744     }
5745
5746   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
5747            && code == SYMBOL_REF
5748            && CONSTANT_POOL_ADDRESS_P (x)
5749            && ! (flag_pic
5750                  && symbol_mentioned_p (get_pool_constant (x))
5751                  && ! pcrel_constant_p (get_pool_constant (x))))
5752     return 1;
5753
5754   return 0;
5755 }
5756
5757 /* Return nonzero if INDEX is valid for an address index operand in
5758    ARM state.  */
5759 static int
5760 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
5761                         int strict_p)
5762 {
5763   HOST_WIDE_INT range;
5764   enum rtx_code code = GET_CODE (index);
5765
5766   /* Standard coprocessor addressing modes.  */
5767   if (TARGET_HARD_FLOAT
5768       && (TARGET_VFP || TARGET_FPA || TARGET_MAVERICK)
5769       && (mode == SFmode || mode == DFmode
5770           || (TARGET_MAVERICK && mode == DImode)))
5771     return (code == CONST_INT && INTVAL (index) < 1024
5772             && INTVAL (index) > -1024
5773             && (INTVAL (index) & 3) == 0);
5774
5775   /* For quad modes, we restrict the constant offset to be slightly less
5776      than what the instruction format permits.  We do this because for
5777      quad mode moves, we will actually decompose them into two separate
5778      double-mode reads or writes.  INDEX must therefore be a valid
5779      (double-mode) offset and so should INDEX+8.  */
5780   if (TARGET_NEON && VALID_NEON_QREG_MODE (mode))
5781     return (code == CONST_INT
5782             && INTVAL (index) < 1016
5783             && INTVAL (index) > -1024
5784             && (INTVAL (index) & 3) == 0);
5785
5786   /* We have no such constraint on double mode offsets, so we permit the
5787      full range of the instruction format.  */
5788   if (TARGET_NEON && VALID_NEON_DREG_MODE (mode))
5789     return (code == CONST_INT
5790             && INTVAL (index) < 1024
5791             && INTVAL (index) > -1024
5792             && (INTVAL (index) & 3) == 0);
5793
5794   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
5795     return (code == CONST_INT
5796             && INTVAL (index) < 1024
5797             && INTVAL (index) > -1024
5798             && (INTVAL (index) & 3) == 0);
5799
5800   if (arm_address_register_rtx_p (index, strict_p)
5801       && (GET_MODE_SIZE (mode) <= 4))
5802     return 1;
5803
5804   if (mode == DImode || mode == DFmode)
5805     {
5806       if (code == CONST_INT)
5807         {
5808           HOST_WIDE_INT val = INTVAL (index);
5809
5810           if (TARGET_LDRD)
5811             return val > -256 && val < 256;
5812           else
5813             return val > -4096 && val < 4092;
5814         }
5815
5816       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
5817     }
5818
5819   if (GET_MODE_SIZE (mode) <= 4
5820       && ! (arm_arch4
5821             && (mode == HImode
5822                 || mode == HFmode
5823                 || (mode == QImode && outer == SIGN_EXTEND))))
5824     {
5825       if (code == MULT)
5826         {
5827           rtx xiop0 = XEXP (index, 0);
5828           rtx xiop1 = XEXP (index, 1);
5829
5830           return ((arm_address_register_rtx_p (xiop0, strict_p)
5831                    && power_of_two_operand (xiop1, SImode))
5832                   || (arm_address_register_rtx_p (xiop1, strict_p)
5833                       && power_of_two_operand (xiop0, SImode)));
5834         }
5835       else if (code == LSHIFTRT || code == ASHIFTRT
5836                || code == ASHIFT || code == ROTATERT)
5837         {
5838           rtx op = XEXP (index, 1);
5839
5840           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
5841                   && GET_CODE (op) == CONST_INT
5842                   && INTVAL (op) > 0
5843                   && INTVAL (op) <= 31);
5844         }
5845     }
5846
5847   /* For ARM v4 we may be doing a sign-extend operation during the
5848      load.  */
5849   if (arm_arch4)
5850     {
5851       if (mode == HImode
5852           || mode == HFmode
5853           || (outer == SIGN_EXTEND && mode == QImode))
5854         range = 256;
5855       else
5856         range = 4096;
5857     }
5858   else
5859     range = (mode == HImode || mode == HFmode) ? 4095 : 4096;
5860
5861   return (code == CONST_INT
5862           && INTVAL (index) < range
5863           && INTVAL (index) > -range);
5864 }
5865
5866 /* Return true if OP is a valid index scaling factor for Thumb-2 address
5867    index operand.  i.e. 1, 2, 4 or 8.  */
5868 static bool
5869 thumb2_index_mul_operand (rtx op)
5870 {
5871   HOST_WIDE_INT val;
5872   
5873   if (GET_CODE(op) != CONST_INT)
5874     return false;
5875
5876   val = INTVAL(op);
5877   return (val == 1 || val == 2 || val == 4 || val == 8);
5878 }
5879   
5880 /* Return nonzero if INDEX is a valid Thumb-2 address index operand.  */
5881 static int
5882 thumb2_legitimate_index_p (enum machine_mode mode, rtx index, int strict_p)
5883 {
5884   enum rtx_code code = GET_CODE (index);
5885
5886   /* ??? Combine arm and thumb2 coprocessor addressing modes.  */
5887   /* Standard coprocessor addressing modes.  */
5888   if (TARGET_HARD_FLOAT
5889       && (TARGET_VFP || TARGET_FPA || TARGET_MAVERICK)
5890       && (mode == SFmode || mode == DFmode
5891           || (TARGET_MAVERICK && mode == DImode)))
5892     return (code == CONST_INT && INTVAL (index) < 1024
5893             /* Thumb-2 allows only > -256 index range for it's core register
5894                load/stores. Since we allow SF/DF in core registers, we have
5895                to use the intersection between -256~4096 (core) and -1024~1024
5896                (coprocessor).  */
5897             && INTVAL (index) > -256
5898             && (INTVAL (index) & 3) == 0);
5899
5900   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
5901     {
5902       /* For DImode assume values will usually live in core regs
5903          and only allow LDRD addressing modes.  */
5904       if (!TARGET_LDRD || mode != DImode)
5905         return (code == CONST_INT
5906                 && INTVAL (index) < 1024
5907                 && INTVAL (index) > -1024
5908                 && (INTVAL (index) & 3) == 0);
5909     }
5910
5911   /* For quad modes, we restrict the constant offset to be slightly less
5912      than what the instruction format permits.  We do this because for
5913      quad mode moves, we will actually decompose them into two separate
5914      double-mode reads or writes.  INDEX must therefore be a valid
5915      (double-mode) offset and so should INDEX+8.  */
5916   if (TARGET_NEON && VALID_NEON_QREG_MODE (mode))
5917     return (code == CONST_INT
5918             && INTVAL (index) < 1016
5919             && INTVAL (index) > -1024
5920             && (INTVAL (index) & 3) == 0);
5921
5922   /* We have no such constraint on double mode offsets, so we permit the
5923      full range of the instruction format.  */
5924   if (TARGET_NEON && VALID_NEON_DREG_MODE (mode))
5925     return (code == CONST_INT
5926             && INTVAL (index) < 1024
5927             && INTVAL (index) > -1024
5928             && (INTVAL (index) & 3) == 0);
5929
5930   if (arm_address_register_rtx_p (index, strict_p)
5931       && (GET_MODE_SIZE (mode) <= 4))
5932     return 1;
5933
5934   if (mode == DImode || mode == DFmode)
5935     {
5936       if (code == CONST_INT)
5937         {
5938           HOST_WIDE_INT val = INTVAL (index);
5939           /* ??? Can we assume ldrd for thumb2?  */
5940           /* Thumb-2 ldrd only has reg+const addressing modes.  */
5941           /* ldrd supports offsets of +-1020.
5942              However the ldr fallback does not.  */
5943           return val > -256 && val < 256 && (val & 3) == 0;
5944         }
5945       else
5946         return 0;
5947     }
5948
5949   if (code == MULT)
5950     {
5951       rtx xiop0 = XEXP (index, 0);
5952       rtx xiop1 = XEXP (index, 1);
5953
5954       return ((arm_address_register_rtx_p (xiop0, strict_p)
5955                && thumb2_index_mul_operand (xiop1))
5956               || (arm_address_register_rtx_p (xiop1, strict_p)
5957                   && thumb2_index_mul_operand (xiop0)));
5958     }
5959   else if (code == ASHIFT)
5960     {
5961       rtx op = XEXP (index, 1);
5962
5963       return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
5964               && GET_CODE (op) == CONST_INT
5965               && INTVAL (op) > 0
5966               && INTVAL (op) <= 3);
5967     }
5968
5969   return (code == CONST_INT
5970           && INTVAL (index) < 4096
5971           && INTVAL (index) > -256);
5972 }
5973
5974 /* Return nonzero if X is valid as a 16-bit Thumb state base register.  */
5975 static int
5976 thumb1_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
5977 {
5978   int regno;
5979
5980   if (GET_CODE (x) != REG)
5981     return 0;
5982
5983   regno = REGNO (x);
5984
5985   if (strict_p)
5986     return THUMB1_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
5987
5988   return (regno <= LAST_LO_REGNUM
5989           || regno > LAST_VIRTUAL_REGISTER
5990           || regno == FRAME_POINTER_REGNUM
5991           || (GET_MODE_SIZE (mode) >= 4
5992               && (regno == STACK_POINTER_REGNUM
5993                   || regno >= FIRST_PSEUDO_REGISTER
5994                   || x == hard_frame_pointer_rtx
5995                   || x == arg_pointer_rtx)));
5996 }
5997
5998 /* Return nonzero if x is a legitimate index register.  This is the case
5999    for any base register that can access a QImode object.  */
6000 inline static int
6001 thumb1_index_register_rtx_p (rtx x, int strict_p)
6002 {
6003   return thumb1_base_register_rtx_p (x, QImode, strict_p);
6004 }
6005
6006 /* Return nonzero if x is a legitimate 16-bit Thumb-state address.
6007
6008    The AP may be eliminated to either the SP or the FP, so we use the
6009    least common denominator, e.g. SImode, and offsets from 0 to 64.
6010
6011    ??? Verify whether the above is the right approach.
6012
6013    ??? Also, the FP may be eliminated to the SP, so perhaps that
6014    needs special handling also.
6015
6016    ??? Look at how the mips16 port solves this problem.  It probably uses
6017    better ways to solve some of these problems.
6018
6019    Although it is not incorrect, we don't accept QImode and HImode
6020    addresses based on the frame pointer or arg pointer until the
6021    reload pass starts.  This is so that eliminating such addresses
6022    into stack based ones won't produce impossible code.  */
6023 int
6024 thumb1_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
6025 {
6026   /* ??? Not clear if this is right.  Experiment.  */
6027   if (GET_MODE_SIZE (mode) < 4
6028       && !(reload_in_progress || reload_completed)
6029       && (reg_mentioned_p (frame_pointer_rtx, x)
6030           || reg_mentioned_p (arg_pointer_rtx, x)
6031           || reg_mentioned_p (virtual_incoming_args_rtx, x)
6032           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
6033           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
6034           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
6035     return 0;
6036
6037   /* Accept any base register.  SP only in SImode or larger.  */
6038   else if (thumb1_base_register_rtx_p (x, mode, strict_p))
6039     return 1;
6040
6041   /* This is PC relative data before arm_reorg runs.  */
6042   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
6043            && GET_CODE (x) == SYMBOL_REF
6044            && CONSTANT_POOL_ADDRESS_P (x) && !flag_pic)
6045     return 1;
6046
6047   /* This is PC relative data after arm_reorg runs.  */
6048   else if ((GET_MODE_SIZE (mode) >= 4 || mode == HFmode)
6049            && reload_completed
6050            && (GET_CODE (x) == LABEL_REF
6051                || (GET_CODE (x) == CONST
6052                    && GET_CODE (XEXP (x, 0)) == PLUS
6053                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
6054                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
6055     return 1;
6056
6057   /* Post-inc indexing only supported for SImode and larger.  */
6058   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
6059            && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p))
6060     return 1;
6061
6062   else if (GET_CODE (x) == PLUS)
6063     {
6064       /* REG+REG address can be any two index registers.  */
6065       /* We disallow FRAME+REG addressing since we know that FRAME
6066          will be replaced with STACK, and SP relative addressing only
6067          permits SP+OFFSET.  */
6068       if (GET_MODE_SIZE (mode) <= 4
6069           && XEXP (x, 0) != frame_pointer_rtx
6070           && XEXP (x, 1) != frame_pointer_rtx
6071           && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
6072           && (thumb1_index_register_rtx_p (XEXP (x, 1), strict_p)
6073               || (!strict_p && will_be_in_index_register (XEXP (x, 1)))))
6074         return 1;
6075
6076       /* REG+const has 5-7 bit offset for non-SP registers.  */
6077       else if ((thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
6078                 || XEXP (x, 0) == arg_pointer_rtx)
6079                && GET_CODE (XEXP (x, 1)) == CONST_INT
6080                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
6081         return 1;
6082
6083       /* REG+const has 10-bit offset for SP, but only SImode and
6084          larger is supported.  */
6085       /* ??? Should probably check for DI/DFmode overflow here
6086          just like GO_IF_LEGITIMATE_OFFSET does.  */
6087       else if (GET_CODE (XEXP (x, 0)) == REG
6088                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
6089                && GET_MODE_SIZE (mode) >= 4
6090                && GET_CODE (XEXP (x, 1)) == CONST_INT
6091                && INTVAL (XEXP (x, 1)) >= 0
6092                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
6093                && (INTVAL (XEXP (x, 1)) & 3) == 0)
6094         return 1;
6095
6096       else if (GET_CODE (XEXP (x, 0)) == REG
6097                && (REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
6098                    || REGNO (XEXP (x, 0)) == ARG_POINTER_REGNUM
6099                    || (REGNO (XEXP (x, 0)) >= FIRST_VIRTUAL_REGISTER
6100                        && REGNO (XEXP (x, 0))
6101                           <= LAST_VIRTUAL_POINTER_REGISTER))
6102                && GET_MODE_SIZE (mode) >= 4
6103                && GET_CODE (XEXP (x, 1)) == CONST_INT
6104                && (INTVAL (XEXP (x, 1)) & 3) == 0)
6105         return 1;
6106     }
6107
6108   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
6109            && GET_MODE_SIZE (mode) == 4
6110            && GET_CODE (x) == SYMBOL_REF
6111            && CONSTANT_POOL_ADDRESS_P (x)
6112            && ! (flag_pic
6113                  && symbol_mentioned_p (get_pool_constant (x))
6114                  && ! pcrel_constant_p (get_pool_constant (x))))
6115     return 1;
6116
6117   return 0;
6118 }
6119
6120 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
6121    instruction of mode MODE.  */
6122 int
6123 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
6124 {
6125   switch (GET_MODE_SIZE (mode))
6126     {
6127     case 1:
6128       return val >= 0 && val < 32;
6129
6130     case 2:
6131       return val >= 0 && val < 64 && (val & 1) == 0;
6132
6133     default:
6134       return (val >= 0
6135               && (val + GET_MODE_SIZE (mode)) <= 128
6136               && (val & 3) == 0);
6137     }
6138 }
6139
6140 bool
6141 arm_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
6142 {
6143   if (TARGET_ARM)
6144     return arm_legitimate_address_outer_p (mode, x, SET, strict_p);
6145   else if (TARGET_THUMB2)
6146     return thumb2_legitimate_address_p (mode, x, strict_p);
6147   else /* if (TARGET_THUMB1) */
6148     return thumb1_legitimate_address_p (mode, x, strict_p);
6149 }
6150
6151 /* Build the SYMBOL_REF for __tls_get_addr.  */
6152
6153 static GTY(()) rtx tls_get_addr_libfunc;
6154
6155 static rtx
6156 get_tls_get_addr (void)
6157 {
6158   if (!tls_get_addr_libfunc)
6159     tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
6160   return tls_get_addr_libfunc;
6161 }
6162
6163 static rtx
6164 arm_load_tp (rtx target)
6165 {
6166   if (!target)
6167     target = gen_reg_rtx (SImode);
6168
6169   if (TARGET_HARD_TP)
6170     {
6171       /* Can return in any reg.  */
6172       emit_insn (gen_load_tp_hard (target));
6173     }
6174   else
6175     {
6176       /* Always returned in r0.  Immediately copy the result into a pseudo,
6177          otherwise other uses of r0 (e.g. setting up function arguments) may
6178          clobber the value.  */
6179
6180       rtx tmp;
6181
6182       emit_insn (gen_load_tp_soft ());
6183
6184       tmp = gen_rtx_REG (SImode, 0);
6185       emit_move_insn (target, tmp);
6186     }
6187   return target;
6188 }
6189
6190 static rtx
6191 load_tls_operand (rtx x, rtx reg)
6192 {
6193   rtx tmp;
6194
6195   if (reg == NULL_RTX)
6196     reg = gen_reg_rtx (SImode);
6197
6198   tmp = gen_rtx_CONST (SImode, x);
6199
6200   emit_move_insn (reg, tmp);
6201
6202   return reg;
6203 }
6204
6205 static rtx
6206 arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
6207 {
6208   rtx insns, label, labelno, sum;
6209
6210   gcc_assert (reloc != TLS_DESCSEQ);
6211   start_sequence ();
6212
6213   labelno = GEN_INT (pic_labelno++);
6214   label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
6215   label = gen_rtx_CONST (VOIDmode, label);
6216
6217   sum = gen_rtx_UNSPEC (Pmode,
6218                         gen_rtvec (4, x, GEN_INT (reloc), label,
6219                                    GEN_INT (TARGET_ARM ? 8 : 4)),
6220                         UNSPEC_TLS);
6221   reg = load_tls_operand (sum, reg);
6222
6223   if (TARGET_ARM)
6224     emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
6225   else
6226     emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
6227   
6228   *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX,
6229                                      LCT_PURE, /* LCT_CONST?  */
6230                                      Pmode, 1, reg, Pmode);
6231   
6232   insns = get_insns ();
6233   end_sequence ();
6234
6235   return insns;
6236 }
6237
6238 static rtx
6239 arm_tls_descseq_addr (rtx x, rtx reg)
6240 {
6241   rtx labelno = GEN_INT (pic_labelno++);
6242   rtx label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
6243   rtx sum = gen_rtx_UNSPEC (Pmode,
6244                             gen_rtvec (4, x, GEN_INT (TLS_DESCSEQ),
6245                                        gen_rtx_CONST (VOIDmode, label),
6246                                        GEN_INT (!TARGET_ARM)),
6247                             UNSPEC_TLS);
6248   rtx reg0 = load_tls_operand (sum, gen_rtx_REG (SImode, 0));
6249   
6250   emit_insn (gen_tlscall (x, labelno));
6251   if (!reg)
6252     reg = gen_reg_rtx (SImode);
6253   else
6254     gcc_assert (REGNO (reg) != 0);
6255
6256   emit_move_insn (reg, reg0);
6257
6258   return reg;
6259 }
6260
6261 rtx
6262 legitimize_tls_address (rtx x, rtx reg)
6263 {
6264   rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
6265   unsigned int model = SYMBOL_REF_TLS_MODEL (x);
6266
6267   switch (model)
6268     {
6269     case TLS_MODEL_GLOBAL_DYNAMIC:
6270       if (TARGET_GNU2_TLS)
6271         {
6272           reg = arm_tls_descseq_addr (x, reg);
6273
6274           tp = arm_load_tp (NULL_RTX);
6275           
6276           dest = gen_rtx_PLUS (Pmode, tp, reg);
6277         }
6278       else
6279         {
6280           /* Original scheme */
6281           insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
6282           dest = gen_reg_rtx (Pmode);
6283           emit_libcall_block (insns, dest, ret, x);
6284         }
6285       return dest;
6286
6287     case TLS_MODEL_LOCAL_DYNAMIC:
6288       if (TARGET_GNU2_TLS)
6289         {
6290           reg = arm_tls_descseq_addr (x, reg);
6291
6292           tp = arm_load_tp (NULL_RTX);
6293           
6294           dest = gen_rtx_PLUS (Pmode, tp, reg);
6295         }
6296       else
6297         {
6298           insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
6299           
6300           /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
6301              share the LDM result with other LD model accesses.  */
6302           eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
6303                                 UNSPEC_TLS);
6304           dest = gen_reg_rtx (Pmode);
6305           emit_libcall_block (insns, dest, ret, eqv);
6306           
6307           /* Load the addend.  */
6308           addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x,
6309                                                      GEN_INT (TLS_LDO32)),
6310                                    UNSPEC_TLS);
6311           addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
6312           dest = gen_rtx_PLUS (Pmode, dest, addend);
6313         }
6314       return dest;
6315
6316     case TLS_MODEL_INITIAL_EXEC:
6317       labelno = GEN_INT (pic_labelno++);
6318       label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
6319       label = gen_rtx_CONST (VOIDmode, label);
6320       sum = gen_rtx_UNSPEC (Pmode,
6321                             gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
6322                                        GEN_INT (TARGET_ARM ? 8 : 4)),
6323                             UNSPEC_TLS);
6324       reg = load_tls_operand (sum, reg);
6325
6326       if (TARGET_ARM)
6327         emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
6328       else if (TARGET_THUMB2)
6329         emit_insn (gen_tls_load_dot_plus_four (reg, NULL, reg, labelno));
6330       else
6331         {
6332           emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
6333           emit_move_insn (reg, gen_const_mem (SImode, reg));
6334         }
6335
6336       tp = arm_load_tp (NULL_RTX);
6337
6338       return gen_rtx_PLUS (Pmode, tp, reg);
6339
6340     case TLS_MODEL_LOCAL_EXEC:
6341       tp = arm_load_tp (NULL_RTX);
6342
6343       reg = gen_rtx_UNSPEC (Pmode,
6344                             gen_rtvec (2, x, GEN_INT (TLS_LE32)),
6345                             UNSPEC_TLS);
6346       reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
6347
6348       return gen_rtx_PLUS (Pmode, tp, reg);
6349
6350     default:
6351       abort ();
6352     }
6353 }
6354
6355 /* Try machine-dependent ways of modifying an illegitimate address
6356    to be legitimate.  If we find one, return the new, valid address.  */
6357 rtx
6358 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
6359 {
6360   if (!TARGET_ARM)
6361     {
6362       /* TODO: legitimize_address for Thumb2.  */
6363       if (TARGET_THUMB2)
6364         return x;
6365       return thumb_legitimize_address (x, orig_x, mode);
6366     }
6367
6368   if (arm_tls_symbol_p (x))
6369     return legitimize_tls_address (x, NULL_RTX);
6370
6371   if (GET_CODE (x) == PLUS)
6372     {
6373       rtx xop0 = XEXP (x, 0);
6374       rtx xop1 = XEXP (x, 1);
6375
6376       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
6377         xop0 = force_reg (SImode, xop0);
6378
6379       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
6380         xop1 = force_reg (SImode, xop1);
6381
6382       if (ARM_BASE_REGISTER_RTX_P (xop0)
6383           && GET_CODE (xop1) == CONST_INT)
6384         {
6385           HOST_WIDE_INT n, low_n;
6386           rtx base_reg, val;
6387           n = INTVAL (xop1);
6388
6389           /* VFP addressing modes actually allow greater offsets, but for
6390              now we just stick with the lowest common denominator.  */
6391           if (mode == DImode
6392               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
6393             {
6394               low_n = n & 0x0f;
6395               n &= ~0x0f;
6396               if (low_n > 4)
6397                 {
6398                   n += 16;
6399                   low_n -= 16;
6400                 }
6401             }
6402           else
6403             {
6404               low_n = ((mode) == TImode ? 0
6405                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
6406               n -= low_n;
6407             }
6408
6409           base_reg = gen_reg_rtx (SImode);
6410           val = force_operand (plus_constant (xop0, n), NULL_RTX);
6411           emit_move_insn (base_reg, val);
6412           x = plus_constant (base_reg, low_n);
6413         }
6414       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
6415         x = gen_rtx_PLUS (SImode, xop0, xop1);
6416     }
6417
6418   /* XXX We don't allow MINUS any more -- see comment in
6419      arm_legitimate_address_outer_p ().  */
6420   else if (GET_CODE (x) == MINUS)
6421     {
6422       rtx xop0 = XEXP (x, 0);
6423       rtx xop1 = XEXP (x, 1);
6424
6425       if (CONSTANT_P (xop0))
6426         xop0 = force_reg (SImode, xop0);
6427
6428       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
6429         xop1 = force_reg (SImode, xop1);
6430
6431       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
6432         x = gen_rtx_MINUS (SImode, xop0, xop1);
6433     }
6434
6435   /* Make sure to take full advantage of the pre-indexed addressing mode
6436      with absolute addresses which often allows for the base register to
6437      be factorized for multiple adjacent memory references, and it might
6438      even allows for the mini pool to be avoided entirely. */
6439   else if (GET_CODE (x) == CONST_INT && optimize > 0)
6440     {
6441       unsigned int bits;
6442       HOST_WIDE_INT mask, base, index;
6443       rtx base_reg;
6444
6445       /* ldr and ldrb can use a 12-bit index, ldrsb and the rest can only
6446          use a 8-bit index. So let's use a 12-bit index for SImode only and
6447          hope that arm_gen_constant will enable ldrb to use more bits. */
6448       bits = (mode == SImode) ? 12 : 8;
6449       mask = (1 << bits) - 1;
6450       base = INTVAL (x) & ~mask;
6451       index = INTVAL (x) & mask;
6452       if (bit_count (base & 0xffffffff) > (32 - bits)/2)
6453         {
6454           /* It'll most probably be more efficient to generate the base
6455              with more bits set and use a negative index instead. */
6456           base |= mask;
6457           index -= mask;
6458         }
6459       base_reg = force_reg (SImode, GEN_INT (base));
6460       x = plus_constant (base_reg, index);
6461     }
6462
6463   if (flag_pic)
6464     {
6465       /* We need to find and carefully transform any SYMBOL and LABEL
6466          references; so go back to the original address expression.  */
6467       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
6468
6469       if (new_x != orig_x)
6470         x = new_x;
6471     }
6472
6473   return x;
6474 }
6475
6476
6477 /* Try machine-dependent ways of modifying an illegitimate Thumb address
6478    to be legitimate.  If we find one, return the new, valid address.  */
6479 rtx
6480 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
6481 {
6482   if (arm_tls_symbol_p (x))
6483     return legitimize_tls_address (x, NULL_RTX);
6484
6485   if (GET_CODE (x) == PLUS
6486       && GET_CODE (XEXP (x, 1)) == CONST_INT
6487       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
6488           || INTVAL (XEXP (x, 1)) < 0))
6489     {
6490       rtx xop0 = XEXP (x, 0);
6491       rtx xop1 = XEXP (x, 1);
6492       HOST_WIDE_INT offset = INTVAL (xop1);
6493
6494       /* Try and fold the offset into a biasing of the base register and
6495          then offsetting that.  Don't do this when optimizing for space
6496          since it can cause too many CSEs.  */
6497       if (optimize_size && offset >= 0
6498           && offset < 256 + 31 * GET_MODE_SIZE (mode))
6499         {
6500           HOST_WIDE_INT delta;
6501
6502           if (offset >= 256)
6503             delta = offset - (256 - GET_MODE_SIZE (mode));
6504           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
6505             delta = 31 * GET_MODE_SIZE (mode);
6506           else
6507             delta = offset & (~31 * GET_MODE_SIZE (mode));
6508
6509           xop0 = force_operand (plus_constant (xop0, offset - delta),
6510                                 NULL_RTX);
6511           x = plus_constant (xop0, delta);
6512         }
6513       else if (offset < 0 && offset > -256)
6514         /* Small negative offsets are best done with a subtract before the
6515            dereference, forcing these into a register normally takes two
6516            instructions.  */
6517         x = force_operand (x, NULL_RTX);
6518       else
6519         {
6520           /* For the remaining cases, force the constant into a register.  */
6521           xop1 = force_reg (SImode, xop1);
6522           x = gen_rtx_PLUS (SImode, xop0, xop1);
6523         }
6524     }
6525   else if (GET_CODE (x) == PLUS
6526            && s_register_operand (XEXP (x, 1), SImode)
6527            && !s_register_operand (XEXP (x, 0), SImode))
6528     {
6529       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
6530
6531       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
6532     }
6533
6534   if (flag_pic)
6535     {
6536       /* We need to find and carefully transform any SYMBOL and LABEL
6537          references; so go back to the original address expression.  */
6538       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
6539
6540       if (new_x != orig_x)
6541         x = new_x;
6542     }
6543
6544   return x;
6545 }
6546
6547 bool
6548 arm_legitimize_reload_address (rtx *p,
6549                                enum machine_mode mode,
6550                                int opnum, int type,
6551                                int ind_levels ATTRIBUTE_UNUSED)
6552 {
6553   if (GET_CODE (*p) == PLUS
6554       && GET_CODE (XEXP (*p, 0)) == REG
6555       && ARM_REGNO_OK_FOR_BASE_P (REGNO (XEXP (*p, 0)))
6556       && GET_CODE (XEXP (*p, 1)) == CONST_INT)
6557     {
6558       HOST_WIDE_INT val = INTVAL (XEXP (*p, 1));
6559       HOST_WIDE_INT low, high;
6560
6561       /* Detect coprocessor load/stores.  */
6562       bool coproc_p = ((TARGET_HARD_FLOAT
6563                         && (TARGET_VFP || TARGET_FPA || TARGET_MAVERICK)
6564                         && (mode == SFmode || mode == DFmode
6565                             || (mode == DImode && TARGET_MAVERICK)))
6566                        || (TARGET_REALLY_IWMMXT
6567                            && VALID_IWMMXT_REG_MODE (mode))
6568                        || (TARGET_NEON
6569                            && (VALID_NEON_DREG_MODE (mode)
6570                                || VALID_NEON_QREG_MODE (mode))));
6571
6572       /* For some conditions, bail out when lower two bits are unaligned.  */
6573       if ((val & 0x3) != 0
6574           /* Coprocessor load/store indexes are 8-bits + '00' appended.  */
6575           && (coproc_p
6576               /* For DI, and DF under soft-float: */
6577               || ((mode == DImode || mode == DFmode)
6578                   /* Without ldrd, we use stm/ldm, which does not
6579                      fair well with unaligned bits.  */
6580                   && (! TARGET_LDRD
6581                       /* Thumb-2 ldrd/strd is [-1020,+1020] in steps of 4.  */
6582                       || TARGET_THUMB2))))
6583         return false;
6584
6585       /* When breaking down a [reg+index] reload address into [(reg+high)+low],
6586          of which the (reg+high) gets turned into a reload add insn,
6587          we try to decompose the index into high/low values that can often
6588          also lead to better reload CSE.
6589          For example:
6590                  ldr r0, [r2, #4100]  // Offset too large
6591                  ldr r1, [r2, #4104]  // Offset too large
6592
6593          is best reloaded as:
6594                  add t1, r2, #4096
6595                  ldr r0, [t1, #4]
6596                  add t2, r2, #4096
6597                  ldr r1, [t2, #8]
6598
6599          which post-reload CSE can simplify in most cases to eliminate the
6600          second add instruction:
6601                  add t1, r2, #4096
6602                  ldr r0, [t1, #4]
6603                  ldr r1, [t1, #8]
6604
6605          The idea here is that we want to split out the bits of the constant
6606          as a mask, rather than as subtracting the maximum offset that the
6607          respective type of load/store used can handle.
6608
6609          When encountering negative offsets, we can still utilize it even if
6610          the overall offset is positive; sometimes this may lead to an immediate
6611          that can be constructed with fewer instructions.
6612          For example:
6613                  ldr r0, [r2, #0x3FFFFC]
6614
6615          This is best reloaded as:
6616                  add t1, r2, #0x400000
6617                  ldr r0, [t1, #-4]
6618
6619          The trick for spotting this for a load insn with N bits of offset
6620          (i.e. bits N-1:0) is to look at bit N; if it is set, then chose a
6621          negative offset that is going to make bit N and all the bits below
6622          it become zero in the remainder part.
6623
6624          The SIGN_MAG_LOW_ADDR_BITS macro below implements this, with respect
6625          to sign-magnitude addressing (i.e. separate +- bit, or 1's complement),
6626          used in most cases of ARM load/store instructions.  */
6627
6628 #define SIGN_MAG_LOW_ADDR_BITS(VAL, N)                                  \
6629       (((VAL) & ((1 << (N)) - 1))                                       \
6630        ? (((VAL) & ((1 << ((N) + 1)) - 1)) ^ (1 << (N))) - (1 << (N))   \
6631        : 0)
6632
6633       if (coproc_p)
6634         {
6635           low = SIGN_MAG_LOW_ADDR_BITS (val, 10);
6636
6637           /* NEON quad-word load/stores are made of two double-word accesses,
6638              so the valid index range is reduced by 8. Treat as 9-bit range if
6639              we go over it.  */
6640           if (TARGET_NEON && VALID_NEON_QREG_MODE (mode) && low >= 1016)
6641             low = SIGN_MAG_LOW_ADDR_BITS (val, 9);
6642         }
6643       else if (GET_MODE_SIZE (mode) == 8)
6644         {
6645           if (TARGET_LDRD)
6646             low = (TARGET_THUMB2
6647                    ? SIGN_MAG_LOW_ADDR_BITS (val, 10)
6648                    : SIGN_MAG_LOW_ADDR_BITS (val, 8));
6649           else
6650             /* For pre-ARMv5TE (without ldrd), we use ldm/stm(db/da/ib)
6651                to access doublewords. The supported load/store offsets are
6652                -8, -4, and 4, which we try to produce here.  */
6653             low = ((val & 0xf) ^ 0x8) - 0x8;
6654         }
6655       else if (GET_MODE_SIZE (mode) < 8)
6656         {
6657           /* NEON element load/stores do not have an offset.  */
6658           if (TARGET_NEON_FP16 && mode == HFmode)
6659             return false;
6660
6661           if (TARGET_THUMB2)
6662             {
6663               /* Thumb-2 has an asymmetrical index range of (-256,4096).
6664                  Try the wider 12-bit range first, and re-try if the result
6665                  is out of range.  */
6666               low = SIGN_MAG_LOW_ADDR_BITS (val, 12);
6667               if (low < -255)
6668                 low = SIGN_MAG_LOW_ADDR_BITS (val, 8);
6669             }
6670           else
6671             {
6672               if (mode == HImode || mode == HFmode)
6673                 {
6674                   if (arm_arch4)
6675                     low = SIGN_MAG_LOW_ADDR_BITS (val, 8);
6676                   else
6677                     {
6678                       /* The storehi/movhi_bytes fallbacks can use only
6679                          [-4094,+4094] of the full ldrb/strb index range.  */
6680                       low = SIGN_MAG_LOW_ADDR_BITS (val, 12);
6681                       if (low == 4095 || low == -4095)
6682                         return false;
6683                     }
6684                 }
6685               else
6686                 low = SIGN_MAG_LOW_ADDR_BITS (val, 12);
6687             }
6688         }
6689       else
6690         return false;
6691
6692       high = ((((val - low) & (unsigned HOST_WIDE_INT) 0xffffffff)
6693                ^ (unsigned HOST_WIDE_INT) 0x80000000)
6694               - (unsigned HOST_WIDE_INT) 0x80000000);
6695       /* Check for overflow or zero */
6696       if (low == 0 || high == 0 || (high + low != val))
6697         return false;
6698
6699       /* Reload the high part into a base reg; leave the low part
6700          in the mem.  */
6701       *p = gen_rtx_PLUS (GET_MODE (*p),
6702                          gen_rtx_PLUS (GET_MODE (*p), XEXP (*p, 0),
6703                                        GEN_INT (high)),
6704                          GEN_INT (low));
6705       push_reload (XEXP (*p, 0), NULL_RTX, &XEXP (*p, 0), NULL,
6706                    MODE_BASE_REG_CLASS (mode), GET_MODE (*p),
6707                    VOIDmode, 0, 0, opnum, (enum reload_type) type);
6708       return true;
6709     }
6710
6711   return false;
6712 }
6713
6714 rtx
6715 thumb_legitimize_reload_address (rtx *x_p,
6716                                  enum machine_mode mode,
6717                                  int opnum, int type,
6718                                  int ind_levels ATTRIBUTE_UNUSED)
6719 {
6720   rtx x = *x_p;
6721
6722   if (GET_CODE (x) == PLUS
6723       && GET_MODE_SIZE (mode) < 4
6724       && REG_P (XEXP (x, 0))
6725       && XEXP (x, 0) == stack_pointer_rtx
6726       && GET_CODE (XEXP (x, 1)) == CONST_INT
6727       && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
6728     {
6729       rtx orig_x = x;
6730
6731       x = copy_rtx (x);
6732       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
6733                    Pmode, VOIDmode, 0, 0, opnum, (enum reload_type) type);
6734       return x;
6735     }
6736
6737   /* If both registers are hi-regs, then it's better to reload the
6738      entire expression rather than each register individually.  That
6739      only requires one reload register rather than two.  */
6740   if (GET_CODE (x) == PLUS
6741       && REG_P (XEXP (x, 0))
6742       && REG_P (XEXP (x, 1))
6743       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
6744       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
6745     {
6746       rtx orig_x = x;
6747
6748       x = copy_rtx (x);
6749       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
6750                    Pmode, VOIDmode, 0, 0, opnum, (enum reload_type) type);
6751       return x;
6752     }
6753
6754   return NULL;
6755 }
6756
6757 /* Test for various thread-local symbols.  */
6758
6759 /* Return TRUE if X is a thread-local symbol.  */
6760
6761 static bool
6762 arm_tls_symbol_p (rtx x)
6763 {
6764   if (! TARGET_HAVE_TLS)
6765     return false;
6766
6767   if (GET_CODE (x) != SYMBOL_REF)
6768     return false;
6769
6770   return SYMBOL_REF_TLS_MODEL (x) != 0;
6771 }
6772
6773 /* Helper for arm_tls_referenced_p.  */
6774
6775 static int
6776 arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
6777 {
6778   if (GET_CODE (*x) == SYMBOL_REF)
6779     return SYMBOL_REF_TLS_MODEL (*x) != 0;
6780
6781   /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
6782      TLS offsets, not real symbol references.  */
6783   if (GET_CODE (*x) == UNSPEC
6784       && XINT (*x, 1) == UNSPEC_TLS)
6785     return -1;
6786
6787   return 0;
6788 }
6789
6790 /* Return TRUE if X contains any TLS symbol references.  */
6791
6792 bool
6793 arm_tls_referenced_p (rtx x)
6794 {
6795   if (! TARGET_HAVE_TLS)
6796     return false;
6797
6798   return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
6799 }
6800
6801 /* Implement TARGET_LEGITIMATE_CONSTANT_P.
6802
6803    On the ARM, allow any integer (invalid ones are removed later by insn
6804    patterns), nice doubles and symbol_refs which refer to the function's
6805    constant pool XXX.
6806
6807    When generating pic allow anything.  */
6808
6809 static bool
6810 arm_legitimate_constant_p_1 (enum machine_mode mode, rtx x)
6811 {
6812   /* At present, we have no support for Neon structure constants, so forbid
6813      them here.  It might be possible to handle simple cases like 0 and -1
6814      in future.  */
6815   if (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode))
6816     return false;
6817
6818   return flag_pic || !label_mentioned_p (x);
6819 }
6820
6821 static bool
6822 thumb_legitimate_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
6823 {
6824   return (GET_CODE (x) == CONST_INT
6825           || GET_CODE (x) == CONST_DOUBLE
6826           || CONSTANT_ADDRESS_P (x)
6827           || flag_pic);
6828 }
6829
6830 static bool
6831 arm_legitimate_constant_p (enum machine_mode mode, rtx x)
6832 {
6833   return (!arm_cannot_force_const_mem (mode, x)
6834           && (TARGET_32BIT
6835               ? arm_legitimate_constant_p_1 (mode, x)
6836               : thumb_legitimate_constant_p (mode, x)));
6837 }
6838
6839 /* Implement TARGET_CANNOT_FORCE_CONST_MEM.  */
6840
6841 static bool
6842 arm_cannot_force_const_mem (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
6843 {
6844   rtx base, offset;
6845
6846   if (ARM_OFFSETS_MUST_BE_WITHIN_SECTIONS_P)
6847     {
6848       split_const (x, &base, &offset);
6849       if (GET_CODE (base) == SYMBOL_REF
6850           && !offset_within_block_p (base, INTVAL (offset)))
6851         return true;
6852     }
6853   return arm_tls_referenced_p (x);
6854 }
6855 \f
6856 #define REG_OR_SUBREG_REG(X)                                            \
6857   (GET_CODE (X) == REG                                                  \
6858    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
6859
6860 #define REG_OR_SUBREG_RTX(X)                    \
6861    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
6862
6863 static inline int
6864 thumb1_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
6865 {
6866   enum machine_mode mode = GET_MODE (x);
6867   int total;
6868
6869   switch (code)
6870     {
6871     case ASHIFT:
6872     case ASHIFTRT:
6873     case LSHIFTRT:
6874     case ROTATERT:
6875     case PLUS:
6876     case MINUS:
6877     case COMPARE:
6878     case NEG:
6879     case NOT:
6880       return COSTS_N_INSNS (1);
6881
6882     case MULT:
6883       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6884         {
6885           int cycles = 0;
6886           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
6887
6888           while (i)
6889             {
6890               i >>= 2;
6891               cycles++;
6892             }
6893           return COSTS_N_INSNS (2) + cycles;
6894         }
6895       return COSTS_N_INSNS (1) + 16;
6896
6897     case SET:
6898       return (COSTS_N_INSNS (1)
6899               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
6900                      + GET_CODE (SET_DEST (x)) == MEM));
6901
6902     case CONST_INT:
6903       if (outer == SET)
6904         {
6905           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
6906             return 0;
6907           if (thumb_shiftable_const (INTVAL (x)))
6908             return COSTS_N_INSNS (2);
6909           return COSTS_N_INSNS (3);
6910         }
6911       else if ((outer == PLUS || outer == COMPARE)
6912                && INTVAL (x) < 256 && INTVAL (x) > -256)
6913         return 0;
6914       else if ((outer == IOR || outer == XOR || outer == AND)
6915                && INTVAL (x) < 256 && INTVAL (x) >= -256)
6916         return COSTS_N_INSNS (1);
6917       else if (outer == AND)
6918         {
6919           int i;
6920           /* This duplicates the tests in the andsi3 expander.  */
6921           for (i = 9; i <= 31; i++)
6922             if ((((HOST_WIDE_INT) 1) << i) - 1 == INTVAL (x)
6923                 || (((HOST_WIDE_INT) 1) << i) - 1 == ~INTVAL (x))
6924               return COSTS_N_INSNS (2);
6925         }
6926       else if (outer == ASHIFT || outer == ASHIFTRT
6927                || outer == LSHIFTRT)
6928         return 0;
6929       return COSTS_N_INSNS (2);
6930
6931     case CONST:
6932     case CONST_DOUBLE:
6933     case LABEL_REF:
6934     case SYMBOL_REF:
6935       return COSTS_N_INSNS (3);
6936
6937     case UDIV:
6938     case UMOD:
6939     case DIV:
6940     case MOD:
6941       return 100;
6942
6943     case TRUNCATE:
6944       return 99;
6945
6946     case AND:
6947     case XOR:
6948     case IOR:
6949       /* XXX guess.  */
6950       return 8;
6951
6952     case MEM:
6953       /* XXX another guess.  */
6954       /* Memory costs quite a lot for the first word, but subsequent words
6955          load at the equivalent of a single insn each.  */
6956       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
6957               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
6958                  ? 4 : 0));
6959
6960     case IF_THEN_ELSE:
6961       /* XXX a guess.  */
6962       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
6963         return 14;
6964       return 2;
6965
6966     case SIGN_EXTEND:
6967     case ZERO_EXTEND:
6968       total = mode == DImode ? COSTS_N_INSNS (1) : 0;
6969       total += thumb1_rtx_costs (XEXP (x, 0), GET_CODE (XEXP (x, 0)), code);
6970
6971       if (mode == SImode)
6972         return total;
6973
6974       if (arm_arch6)
6975         return total + COSTS_N_INSNS (1);
6976
6977       /* Assume a two-shift sequence.  Increase the cost slightly so
6978          we prefer actual shifts over an extend operation.  */
6979       return total + 1 + COSTS_N_INSNS (2);
6980
6981     default:
6982       return 99;
6983     }
6984 }
6985
6986 static inline bool
6987 arm_rtx_costs_1 (rtx x, enum rtx_code outer, int* total, bool speed)
6988 {
6989   enum machine_mode mode = GET_MODE (x);
6990   enum rtx_code subcode;
6991   rtx operand;
6992   enum rtx_code code = GET_CODE (x);
6993   *total = 0;
6994
6995   switch (code)
6996     {
6997     case MEM:
6998       /* Memory costs quite a lot for the first word, but subsequent words
6999          load at the equivalent of a single insn each.  */
7000       *total = COSTS_N_INSNS (2 + ARM_NUM_REGS (mode));
7001       return true;
7002
7003     case DIV:
7004     case MOD:
7005     case UDIV:
7006     case UMOD:
7007       if (TARGET_HARD_FLOAT && mode == SFmode)
7008         *total = COSTS_N_INSNS (2);
7009       else if (TARGET_HARD_FLOAT && mode == DFmode && !TARGET_VFP_SINGLE)
7010         *total = COSTS_N_INSNS (4);
7011       else
7012         *total = COSTS_N_INSNS (20);
7013       return false;
7014
7015     case ROTATE:
7016       if (GET_CODE (XEXP (x, 1)) == REG)
7017         *total = COSTS_N_INSNS (1); /* Need to subtract from 32 */
7018       else if (GET_CODE (XEXP (x, 1)) != CONST_INT)
7019         *total = rtx_cost (XEXP (x, 1), code, 1, speed);
7020
7021       /* Fall through */
7022     case ROTATERT:
7023       if (mode != SImode)
7024         {
7025           *total += COSTS_N_INSNS (4);
7026           return true;
7027         }
7028
7029       /* Fall through */
7030     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
7031       *total += rtx_cost (XEXP (x, 0), code, 0, speed);
7032       if (mode == DImode)
7033         {
7034           *total += COSTS_N_INSNS (3);
7035           return true;
7036         }
7037
7038       *total += COSTS_N_INSNS (1);
7039       /* Increase the cost of complex shifts because they aren't any faster,
7040          and reduce dual issue opportunities.  */
7041       if (arm_tune_cortex_a9
7042           && outer != SET && GET_CODE (XEXP (x, 1)) != CONST_INT)
7043         ++*total;
7044
7045       return true;
7046
7047     case MINUS:
7048       if (mode == DImode)
7049         {
7050           *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7051           if (GET_CODE (XEXP (x, 0)) == CONST_INT
7052               && const_ok_for_arm (INTVAL (XEXP (x, 0))))
7053             {
7054               *total += rtx_cost (XEXP (x, 1), code, 1, speed);
7055               return true;
7056             }
7057
7058           if (GET_CODE (XEXP (x, 1)) == CONST_INT
7059               && const_ok_for_arm (INTVAL (XEXP (x, 1))))
7060             {
7061               *total += rtx_cost (XEXP (x, 0), code, 0, speed);
7062               return true;
7063             }
7064
7065           return false;
7066         }
7067
7068       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7069         {
7070           if (TARGET_HARD_FLOAT
7071               && (mode == SFmode
7072                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
7073             {
7074               *total = COSTS_N_INSNS (1);
7075               if (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
7076                   && arm_const_double_rtx (XEXP (x, 0)))
7077                 {
7078                   *total += rtx_cost (XEXP (x, 1), code, 1, speed);
7079                   return true;
7080                 }
7081
7082               if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
7083                   && arm_const_double_rtx (XEXP (x, 1)))
7084                 {
7085                   *total += rtx_cost (XEXP (x, 0), code, 0, speed);
7086                   return true;
7087                 }
7088
7089               return false;
7090             }
7091           *total = COSTS_N_INSNS (20);
7092           return false;
7093         }
7094
7095       *total = COSTS_N_INSNS (1);
7096       if (GET_CODE (XEXP (x, 0)) == CONST_INT
7097           && const_ok_for_arm (INTVAL (XEXP (x, 0))))
7098         {
7099           *total += rtx_cost (XEXP (x, 1), code, 1, speed);
7100           return true;
7101         }
7102
7103       subcode = GET_CODE (XEXP (x, 1));
7104       if (subcode == ASHIFT || subcode == ASHIFTRT
7105           || subcode == LSHIFTRT
7106           || subcode == ROTATE || subcode == ROTATERT)
7107         {
7108           *total += rtx_cost (XEXP (x, 0), code, 0, speed);
7109           *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, 0, speed);
7110           return true;
7111         }
7112
7113       /* A shift as a part of RSB costs no more than RSB itself.  */
7114       if (GET_CODE (XEXP (x, 0)) == MULT
7115           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
7116         {
7117           *total += rtx_cost (XEXP (XEXP (x, 0), 0), code, 0, speed);
7118           *total += rtx_cost (XEXP (x, 1), code, 1, speed);
7119           return true;
7120         }
7121
7122       if (subcode == MULT
7123           && power_of_two_operand (XEXP (XEXP (x, 1), 1), SImode))
7124         {
7125           *total += rtx_cost (XEXP (x, 0), code, 0, speed);
7126           *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, 0, speed);
7127           return true;
7128         }
7129
7130       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMPARE
7131           || GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMM_COMPARE)
7132         {
7133           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, 0, speed);
7134           if (GET_CODE (XEXP (XEXP (x, 1), 0)) == REG
7135               && REGNO (XEXP (XEXP (x, 1), 0)) != CC_REGNUM)
7136             *total += COSTS_N_INSNS (1);
7137
7138           return true;
7139         }
7140
7141       /* Fall through */
7142
7143     case PLUS:
7144       if (code == PLUS && arm_arch6 && mode == SImode
7145           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7146               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7147         {
7148           *total = COSTS_N_INSNS (1);
7149           *total += rtx_cost (XEXP (XEXP (x, 0), 0), GET_CODE (XEXP (x, 0)),
7150                               0, speed);
7151           *total += rtx_cost (XEXP (x, 1), code, 1, speed);
7152           return true;
7153         }
7154
7155       /* MLA: All arguments must be registers.  We filter out
7156          multiplication by a power of two, so that we fall down into
7157          the code below.  */
7158       if (GET_CODE (XEXP (x, 0)) == MULT
7159           && !power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
7160         {
7161           /* The cost comes from the cost of the multiply.  */
7162           return false;
7163         }
7164
7165       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7166         {
7167           if (TARGET_HARD_FLOAT
7168               && (mode == SFmode
7169                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
7170             {
7171               *total = COSTS_N_INSNS (1);
7172               if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
7173                   && arm_const_double_rtx (XEXP (x, 1)))
7174                 {
7175                   *total += rtx_cost (XEXP (x, 0), code, 0, speed);
7176                   return true;
7177                 }
7178
7179               return false;
7180             }
7181
7182           *total = COSTS_N_INSNS (20);
7183           return false;
7184         }
7185
7186       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMPARE
7187           || GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMM_COMPARE)
7188         {
7189           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 1), code, 1, speed);
7190           if (GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
7191               && REGNO (XEXP (XEXP (x, 0), 0)) != CC_REGNUM)
7192             *total += COSTS_N_INSNS (1);
7193           return true;
7194         }
7195
7196       /* Fall through */
7197
7198     case AND: case XOR: case IOR:
7199
7200       /* Normally the frame registers will be spilt into reg+const during
7201          reload, so it is a bad idea to combine them with other instructions,
7202          since then they might not be moved outside of loops.  As a compromise
7203          we allow integration with ops that have a constant as their second
7204          operand.  */
7205       if (REG_OR_SUBREG_REG (XEXP (x, 0))
7206           && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
7207           && GET_CODE (XEXP (x, 1)) != CONST_INT)
7208         *total = COSTS_N_INSNS (1);
7209
7210       if (mode == DImode)
7211         {
7212           *total += COSTS_N_INSNS (2);
7213           if (GET_CODE (XEXP (x, 1)) == CONST_INT
7214               && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
7215             {
7216               *total += rtx_cost (XEXP (x, 0), code, 0, speed);
7217               return true;
7218             }
7219
7220           return false;
7221         }
7222
7223       *total += COSTS_N_INSNS (1);
7224       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7225           && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
7226         {
7227           *total += rtx_cost (XEXP (x, 0), code, 0, speed);
7228           return true;
7229         }
7230       subcode = GET_CODE (XEXP (x, 0));
7231       if (subcode == ASHIFT || subcode == ASHIFTRT
7232           || subcode == LSHIFTRT
7233           || subcode == ROTATE || subcode == ROTATERT)
7234         {
7235           *total += rtx_cost (XEXP (x, 1), code, 1, speed);
7236           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, 0, speed);
7237           return true;
7238         }
7239
7240       if (subcode == MULT
7241           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
7242         {
7243           *total += rtx_cost (XEXP (x, 1), code, 1, speed);
7244           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, 0, speed);
7245           return true;
7246         }
7247
7248       if (subcode == UMIN || subcode == UMAX
7249           || subcode == SMIN || subcode == SMAX)
7250         {
7251           *total = COSTS_N_INSNS (3);
7252           return true;
7253         }
7254
7255       return false;
7256
7257     case MULT:
7258       /* This should have been handled by the CPU specific routines.  */
7259       gcc_unreachable ();
7260
7261     case TRUNCATE:
7262       if (arm_arch3m && mode == SImode
7263           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
7264           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
7265           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
7266               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
7267           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
7268               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
7269         {
7270           *total = rtx_cost (XEXP (XEXP (x, 0), 0), LSHIFTRT, 0, speed);
7271           return true;
7272         }
7273       *total = COSTS_N_INSNS (2); /* Plus the cost of the MULT */
7274       return false;
7275
7276     case NEG:
7277       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7278         {
7279           if (TARGET_HARD_FLOAT
7280               && (mode == SFmode
7281                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
7282             {
7283               *total = COSTS_N_INSNS (1);
7284               return false;
7285             }
7286           *total = COSTS_N_INSNS (2);
7287           return false;
7288         }
7289
7290       /* Fall through */
7291     case NOT:
7292       *total = COSTS_N_INSNS (ARM_NUM_REGS(mode));
7293       if (mode == SImode && code == NOT)
7294         {
7295           subcode = GET_CODE (XEXP (x, 0));
7296           if (subcode == ASHIFT || subcode == ASHIFTRT
7297               || subcode == LSHIFTRT
7298               || subcode == ROTATE || subcode == ROTATERT
7299               || (subcode == MULT
7300                   && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode)))
7301             {
7302               *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, 0, speed);
7303               /* Register shifts cost an extra cycle.  */
7304               if (GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
7305                 *total += COSTS_N_INSNS (1) + rtx_cost (XEXP (XEXP (x, 0), 1),
7306                                                         subcode, 1, speed);
7307               return true;
7308             }
7309         }
7310
7311       return false;
7312
7313     case IF_THEN_ELSE:
7314       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
7315         {
7316           *total = COSTS_N_INSNS (4);
7317           return true;
7318         }
7319
7320       operand = XEXP (x, 0);
7321
7322       if (!((GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMPARE
7323              || GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMM_COMPARE)
7324             && GET_CODE (XEXP (operand, 0)) == REG
7325             && REGNO (XEXP (operand, 0)) == CC_REGNUM))
7326         *total += COSTS_N_INSNS (1);
7327       *total += (rtx_cost (XEXP (x, 1), code, 1, speed)
7328                  + rtx_cost (XEXP (x, 2), code, 2, speed));
7329       return true;
7330
7331     case NE:
7332       if (mode == SImode && XEXP (x, 1) == const0_rtx)
7333         {
7334           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, 0, speed);
7335           return true;
7336         }
7337       goto scc_insn;
7338
7339     case GE:
7340       if ((GET_CODE (XEXP (x, 0)) != REG || REGNO (XEXP (x, 0)) != CC_REGNUM)
7341           && mode == SImode && XEXP (x, 1) == const0_rtx)
7342         {
7343           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, 0, speed);
7344           return true;
7345         }
7346       goto scc_insn;
7347
7348     case LT:
7349       if ((GET_CODE (XEXP (x, 0)) != REG || REGNO (XEXP (x, 0)) != CC_REGNUM)
7350           && mode == SImode && XEXP (x, 1) == const0_rtx)
7351         {
7352           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, 0, speed);
7353           return true;
7354         }
7355       goto scc_insn;
7356
7357     case EQ:
7358     case GT:
7359     case LE:
7360     case GEU:
7361     case LTU:
7362     case GTU:
7363     case LEU:
7364     case UNORDERED:
7365     case ORDERED:
7366     case UNEQ:
7367     case UNGE:
7368     case UNLT:
7369     case UNGT:
7370     case UNLE:
7371     scc_insn:
7372       /* SCC insns.  In the case where the comparison has already been
7373          performed, then they cost 2 instructions.  Otherwise they need
7374          an additional comparison before them.  */
7375       *total = COSTS_N_INSNS (2);
7376       if (GET_CODE (XEXP (x, 0)) == REG && REGNO (XEXP (x, 0)) == CC_REGNUM)
7377         {
7378           return true;
7379         }
7380
7381       /* Fall through */
7382     case COMPARE:
7383       if (GET_CODE (XEXP (x, 0)) == REG && REGNO (XEXP (x, 0)) == CC_REGNUM)
7384         {
7385           *total = 0;
7386           return true;
7387         }
7388
7389       *total += COSTS_N_INSNS (1);
7390       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7391           && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
7392         {
7393           *total += rtx_cost (XEXP (x, 0), code, 0, speed);
7394           return true;
7395         }
7396
7397       subcode = GET_CODE (XEXP (x, 0));
7398       if (subcode == ASHIFT || subcode == ASHIFTRT
7399           || subcode == LSHIFTRT
7400           || subcode == ROTATE || subcode == ROTATERT)
7401         {
7402           *total += rtx_cost (XEXP (x, 1), code, 1, speed);
7403           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, 0, speed);
7404           return true;
7405         }
7406
7407       if (subcode == MULT
7408           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
7409         {
7410           *total += rtx_cost (XEXP (x, 1), code, 1, speed);
7411           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, 0, speed);
7412           return true;
7413         }
7414       
7415       return false;
7416
7417     case UMIN:
7418     case UMAX:
7419     case SMIN:
7420     case SMAX:
7421       *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, 0, speed);
7422       if (GET_CODE (XEXP (x, 1)) != CONST_INT
7423           || !const_ok_for_arm (INTVAL (XEXP (x, 1))))
7424         *total += rtx_cost (XEXP (x, 1), code, 1, speed);
7425       return true;
7426
7427     case ABS:
7428       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7429         {
7430           if (TARGET_HARD_FLOAT
7431               && (mode == SFmode
7432                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
7433             {
7434               *total = COSTS_N_INSNS (1);
7435               return false;
7436             }
7437           *total = COSTS_N_INSNS (20);
7438           return false;
7439         }
7440       *total = COSTS_N_INSNS (1);
7441       if (mode == DImode)
7442         *total += COSTS_N_INSNS (3);
7443       return false;
7444
7445     case SIGN_EXTEND:
7446     case ZERO_EXTEND:
7447       *total = 0;
7448       if (GET_MODE_CLASS (mode) == MODE_INT)
7449         {
7450           rtx op = XEXP (x, 0);
7451           enum machine_mode opmode = GET_MODE (op);
7452
7453           if (mode == DImode)
7454             *total += COSTS_N_INSNS (1);
7455
7456           if (opmode != SImode)
7457             {
7458               if (MEM_P (op))
7459                 {
7460                   /* If !arm_arch4, we use one of the extendhisi2_mem
7461                      or movhi_bytes patterns for HImode.  For a QImode
7462                      sign extension, we first zero-extend from memory
7463                      and then perform a shift sequence.  */
7464                   if (!arm_arch4 && (opmode != QImode || code == SIGN_EXTEND))
7465                     *total += COSTS_N_INSNS (2);
7466                 }
7467               else if (arm_arch6)
7468                 *total += COSTS_N_INSNS (1);
7469
7470               /* We don't have the necessary insn, so we need to perform some
7471                  other operation.  */
7472               else if (TARGET_ARM && code == ZERO_EXTEND && mode == QImode)
7473                 /* An and with constant 255.  */
7474                 *total += COSTS_N_INSNS (1);
7475               else
7476                 /* A shift sequence.  Increase costs slightly to avoid
7477                    combining two shifts into an extend operation.  */
7478                 *total += COSTS_N_INSNS (2) + 1;
7479             }
7480
7481           return false;
7482         }
7483
7484       switch (GET_MODE (XEXP (x, 0)))
7485         {
7486         case V8QImode:
7487         case V4HImode:
7488         case V2SImode:
7489         case V4QImode:
7490         case V2HImode:
7491           *total = COSTS_N_INSNS (1);
7492           return false;
7493
7494         default:
7495           gcc_unreachable ();
7496         }
7497       gcc_unreachable ();
7498
7499     case ZERO_EXTRACT:
7500     case SIGN_EXTRACT:
7501       *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, 0, speed);
7502       return true;
7503
7504     case CONST_INT:
7505       if (const_ok_for_arm (INTVAL (x))
7506           || const_ok_for_arm (~INTVAL (x)))
7507         *total = COSTS_N_INSNS (1);
7508       else
7509         *total = COSTS_N_INSNS (arm_gen_constant (SET, mode, NULL_RTX,
7510                                                   INTVAL (x), NULL_RTX,
7511                                                   NULL_RTX, 0, 0));
7512       return true;
7513
7514     case CONST:
7515     case LABEL_REF:
7516     case SYMBOL_REF:
7517       *total = COSTS_N_INSNS (3);
7518       return true;
7519
7520     case HIGH:
7521       *total = COSTS_N_INSNS (1);
7522       return true;
7523
7524     case LO_SUM:
7525       *total = COSTS_N_INSNS (1);
7526       *total += rtx_cost (XEXP (x, 0), code, 0, speed);
7527       return true;
7528
7529     case CONST_DOUBLE:
7530       if (TARGET_HARD_FLOAT && vfp3_const_double_rtx (x)
7531           && (mode == SFmode || !TARGET_VFP_SINGLE))
7532         *total = COSTS_N_INSNS (1);
7533       else
7534         *total = COSTS_N_INSNS (4);
7535       return true;
7536
7537     case SET:
7538       return false;
7539
7540     default:
7541       *total = COSTS_N_INSNS (4);
7542       return false;
7543     }
7544 }
7545
7546 /* Estimates the size cost of thumb1 instructions.
7547    For now most of the code is copied from thumb1_rtx_costs. We need more
7548    fine grain tuning when we have more related test cases.  */
7549 static inline int
7550 thumb1_size_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
7551 {
7552   enum machine_mode mode = GET_MODE (x);
7553
7554   switch (code)
7555     {
7556     case ASHIFT:
7557     case ASHIFTRT:
7558     case LSHIFTRT:
7559     case ROTATERT:
7560     case PLUS:
7561     case MINUS:
7562     case COMPARE:
7563     case NEG:
7564     case NOT:
7565       return COSTS_N_INSNS (1);
7566
7567     case MULT:
7568       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7569         {
7570           /* Thumb1 mul instruction can't operate on const. We must Load it
7571              into a register first.  */
7572           int const_size = thumb1_size_rtx_costs (XEXP (x, 1), CONST_INT, SET);
7573           return COSTS_N_INSNS (1) + const_size;
7574         }
7575       return COSTS_N_INSNS (1);
7576
7577     case SET:
7578       return (COSTS_N_INSNS (1)
7579               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
7580                      + GET_CODE (SET_DEST (x)) == MEM));
7581
7582     case CONST_INT:
7583       if (outer == SET)
7584         {
7585           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
7586             return COSTS_N_INSNS (1);
7587           /* See split "TARGET_THUMB1 && satisfies_constraint_J".  */
7588           if (INTVAL (x) >= -255 && INTVAL (x) <= -1)
7589             return COSTS_N_INSNS (2);
7590           /* See split "TARGET_THUMB1 && satisfies_constraint_K".  */
7591           if (thumb_shiftable_const (INTVAL (x)))
7592             return COSTS_N_INSNS (2);
7593           return COSTS_N_INSNS (3);
7594         }
7595       else if ((outer == PLUS || outer == COMPARE)
7596                && INTVAL (x) < 256 && INTVAL (x) > -256)
7597         return 0;
7598       else if ((outer == IOR || outer == XOR || outer == AND)
7599                && INTVAL (x) < 256 && INTVAL (x) >= -256)
7600         return COSTS_N_INSNS (1);
7601       else if (outer == AND)
7602         {
7603           int i;
7604           /* This duplicates the tests in the andsi3 expander.  */
7605           for (i = 9; i <= 31; i++)
7606             if ((((HOST_WIDE_INT) 1) << i) - 1 == INTVAL (x)
7607                 || (((HOST_WIDE_INT) 1) << i) - 1 == ~INTVAL (x))
7608               return COSTS_N_INSNS (2);
7609         }
7610       else if (outer == ASHIFT || outer == ASHIFTRT
7611                || outer == LSHIFTRT)
7612         return 0;
7613       return COSTS_N_INSNS (2);
7614
7615     case CONST:
7616     case CONST_DOUBLE:
7617     case LABEL_REF:
7618     case SYMBOL_REF:
7619       return COSTS_N_INSNS (3);
7620
7621     case UDIV:
7622     case UMOD:
7623     case DIV:
7624     case MOD:
7625       return 100;
7626
7627     case TRUNCATE:
7628       return 99;
7629
7630     case AND:
7631     case XOR:
7632     case IOR:
7633       /* XXX guess.  */
7634       return 8;
7635
7636     case MEM:
7637       /* XXX another guess.  */
7638       /* Memory costs quite a lot for the first word, but subsequent words
7639          load at the equivalent of a single insn each.  */
7640       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
7641               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
7642                  ? 4 : 0));
7643
7644     case IF_THEN_ELSE:
7645       /* XXX a guess.  */
7646       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
7647         return 14;
7648       return 2;
7649
7650     case ZERO_EXTEND:
7651       /* XXX still guessing.  */
7652       switch (GET_MODE (XEXP (x, 0)))
7653         {
7654           case QImode:
7655             return (1 + (mode == DImode ? 4 : 0)
7656                     + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
7657
7658           case HImode:
7659             return (4 + (mode == DImode ? 4 : 0)
7660                     + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
7661
7662           case SImode:
7663             return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
7664
7665           default:
7666             return 99;
7667         }
7668
7669     default:
7670       return 99;
7671     }
7672 }
7673
7674 /* RTX costs when optimizing for size.  */
7675 static bool
7676 arm_size_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7677                     int *total)
7678 {
7679   enum machine_mode mode = GET_MODE (x);
7680   if (TARGET_THUMB1)
7681     {
7682       *total = thumb1_size_rtx_costs (x, code, outer_code);
7683       return true;
7684     }
7685
7686   /* FIXME: This makes no attempt to prefer narrow Thumb-2 instructions.  */
7687   switch (code)
7688     {
7689     case MEM:
7690       /* A memory access costs 1 insn if the mode is small, or the address is
7691          a single register, otherwise it costs one insn per word.  */
7692       if (REG_P (XEXP (x, 0)))
7693         *total = COSTS_N_INSNS (1);
7694       else if (flag_pic
7695                && GET_CODE (XEXP (x, 0)) == PLUS
7696                && will_be_in_index_register (XEXP (XEXP (x, 0), 1)))
7697         /* This will be split into two instructions.
7698            See arm.md:calculate_pic_address.  */
7699         *total = COSTS_N_INSNS (2);
7700       else
7701         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7702       return true;
7703
7704     case DIV:
7705     case MOD:
7706     case UDIV:
7707     case UMOD:
7708       /* Needs a libcall, so it costs about this.  */
7709       *total = COSTS_N_INSNS (2);
7710       return false;
7711
7712     case ROTATE:
7713       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
7714         {
7715           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, 0, false);
7716           return true;
7717         }
7718       /* Fall through */
7719     case ROTATERT:
7720     case ASHIFT:
7721     case LSHIFTRT:
7722     case ASHIFTRT:
7723       if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
7724         {
7725           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code, 0, false);
7726           return true;
7727         }
7728       else if (mode == SImode)
7729         {
7730           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, 0, false);
7731           /* Slightly disparage register shifts, but not by much.  */
7732           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
7733             *total += 1 + rtx_cost (XEXP (x, 1), code, 1, false);
7734           return true;
7735         }
7736
7737       /* Needs a libcall.  */
7738       *total = COSTS_N_INSNS (2);
7739       return false;
7740
7741     case MINUS:
7742       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7743           && (mode == SFmode || !TARGET_VFP_SINGLE))
7744         {
7745           *total = COSTS_N_INSNS (1);
7746           return false;
7747         }
7748
7749       if (mode == SImode)
7750         {
7751           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
7752           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
7753
7754           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
7755               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
7756               || subcode1 == ROTATE || subcode1 == ROTATERT
7757               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
7758               || subcode1 == ASHIFTRT)
7759             {
7760               /* It's just the cost of the two operands.  */
7761               *total = 0;
7762               return false;
7763             }
7764
7765           *total = COSTS_N_INSNS (1);
7766           return false;
7767         }
7768
7769       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7770       return false;
7771
7772     case PLUS:
7773       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7774           && (mode == SFmode || !TARGET_VFP_SINGLE))
7775         {
7776           *total = COSTS_N_INSNS (1);
7777           return false;
7778         }
7779
7780       /* A shift as a part of ADD costs nothing.  */
7781       if (GET_CODE (XEXP (x, 0)) == MULT
7782           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
7783         {
7784           *total = COSTS_N_INSNS (TARGET_THUMB2 ? 2 : 1);
7785           *total += rtx_cost (XEXP (XEXP (x, 0), 0), code, 0, false);
7786           *total += rtx_cost (XEXP (x, 1), code, 1, false);
7787           return true;
7788         }
7789
7790       /* Fall through */
7791     case AND: case XOR: case IOR:
7792       if (mode == SImode)
7793         {
7794           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
7795
7796           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
7797               || subcode == LSHIFTRT || subcode == ASHIFTRT
7798               || (code == AND && subcode == NOT))
7799             {
7800               /* It's just the cost of the two operands.  */
7801               *total = 0;
7802               return false;
7803             }
7804         }
7805
7806       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7807       return false;
7808
7809     case MULT:
7810       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7811       return false;
7812
7813     case NEG:
7814       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7815           && (mode == SFmode || !TARGET_VFP_SINGLE))
7816         {
7817           *total = COSTS_N_INSNS (1);
7818           return false;
7819         }
7820
7821       /* Fall through */
7822     case NOT:
7823       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7824
7825       return false;
7826
7827     case IF_THEN_ELSE:
7828       *total = 0;
7829       return false;
7830
7831     case COMPARE:
7832       if (cc_register (XEXP (x, 0), VOIDmode))
7833         * total = 0;
7834       else
7835         *total = COSTS_N_INSNS (1);
7836       return false;
7837
7838     case ABS:
7839       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7840           && (mode == SFmode || !TARGET_VFP_SINGLE))
7841         *total = COSTS_N_INSNS (1);
7842       else
7843         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
7844       return false;
7845
7846     case SIGN_EXTEND:
7847     case ZERO_EXTEND:
7848       return arm_rtx_costs_1 (x, outer_code, total, 0);
7849
7850     case CONST_INT:
7851       if (const_ok_for_arm (INTVAL (x)))
7852         /* A multiplication by a constant requires another instruction
7853            to load the constant to a register.  */
7854         *total = COSTS_N_INSNS ((outer_code == SET || outer_code == MULT)
7855                                 ? 1 : 0);
7856       else if (const_ok_for_arm (~INTVAL (x)))
7857         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
7858       else if (const_ok_for_arm (-INTVAL (x)))
7859         {
7860           if (outer_code == COMPARE || outer_code == PLUS
7861               || outer_code == MINUS)
7862             *total = 0;
7863           else
7864             *total = COSTS_N_INSNS (1);
7865         }
7866       else
7867         *total = COSTS_N_INSNS (2);
7868       return true;
7869
7870     case CONST:
7871     case LABEL_REF:
7872     case SYMBOL_REF:
7873       *total = COSTS_N_INSNS (2);
7874       return true;
7875
7876     case CONST_DOUBLE:
7877       *total = COSTS_N_INSNS (4);
7878       return true;
7879
7880     case HIGH:
7881     case LO_SUM:
7882       /* We prefer constant pool entries to MOVW/MOVT pairs, so bump the
7883          cost of these slightly.  */
7884       *total = COSTS_N_INSNS (1) + 1;
7885       return true;
7886
7887     case SET:
7888       return false;
7889
7890     default:
7891       if (mode != VOIDmode)
7892         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7893       else
7894         *total = COSTS_N_INSNS (4); /* How knows?  */
7895       return false;
7896     }
7897 }
7898
7899 /* RTX costs when optimizing for size.  */
7900 static bool
7901 arm_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
7902                int *total, bool speed)
7903 {
7904   if (!speed)
7905     return arm_size_rtx_costs (x, (enum rtx_code) code,
7906                                (enum rtx_code) outer_code, total);
7907   else
7908     return current_tune->rtx_costs (x, (enum rtx_code) code,
7909                                     (enum rtx_code) outer_code,
7910                                     total, speed);
7911 }
7912
7913 /* RTX costs for cores with a slow MUL implementation.  Thumb-2 is not
7914    supported on any "slowmul" cores, so it can be ignored.  */
7915
7916 static bool
7917 arm_slowmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7918                        int *total, bool speed)
7919 {
7920   enum machine_mode mode = GET_MODE (x);
7921
7922   if (TARGET_THUMB)
7923     {
7924       *total = thumb1_rtx_costs (x, code, outer_code);
7925       return true;
7926     }
7927
7928   switch (code)
7929     {
7930     case MULT:
7931       if (GET_MODE_CLASS (mode) == MODE_FLOAT
7932           || mode == DImode)
7933         {
7934           *total = COSTS_N_INSNS (20);
7935           return false;
7936         }
7937
7938       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7939         {
7940           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
7941                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
7942           int cost, const_ok = const_ok_for_arm (i);
7943           int j, booth_unit_size;
7944
7945           /* Tune as appropriate.  */
7946           cost = const_ok ? 4 : 8;
7947           booth_unit_size = 2;
7948           for (j = 0; i && j < 32; j += booth_unit_size)
7949             {
7950               i >>= booth_unit_size;
7951               cost++;
7952             }
7953
7954           *total = COSTS_N_INSNS (cost);
7955           *total += rtx_cost (XEXP (x, 0), code, 0, speed);
7956           return true;
7957         }
7958
7959       *total = COSTS_N_INSNS (20);
7960       return false;
7961
7962     default:
7963       return arm_rtx_costs_1 (x, outer_code, total, speed);;
7964     }
7965 }
7966
7967
7968 /* RTX cost for cores with a fast multiply unit (M variants).  */
7969
7970 static bool
7971 arm_fastmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7972                        int *total, bool speed)
7973 {
7974   enum machine_mode mode = GET_MODE (x);
7975
7976   if (TARGET_THUMB1)
7977     {
7978       *total = thumb1_rtx_costs (x, code, outer_code);
7979       return true;
7980     }
7981
7982   /* ??? should thumb2 use different costs?  */
7983   switch (code)
7984     {
7985     case MULT:
7986       /* There is no point basing this on the tuning, since it is always the
7987          fast variant if it exists at all.  */
7988       if (mode == DImode
7989           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
7990           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7991               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7992         {
7993           *total = COSTS_N_INSNS(2);
7994           return false;
7995         }
7996
7997
7998       if (mode == DImode)
7999         {
8000           *total = COSTS_N_INSNS (5);
8001           return false;
8002         }
8003
8004       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
8005         {
8006           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
8007                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
8008           int cost, const_ok = const_ok_for_arm (i);
8009           int j, booth_unit_size;
8010
8011           /* Tune as appropriate.  */
8012           cost = const_ok ? 4 : 8;
8013           booth_unit_size = 8;
8014           for (j = 0; i && j < 32; j += booth_unit_size)
8015             {
8016               i >>= booth_unit_size;
8017               cost++;
8018             }
8019
8020           *total = COSTS_N_INSNS(cost);
8021           return false;
8022         }
8023
8024       if (mode == SImode)
8025         {
8026           *total = COSTS_N_INSNS (4);
8027           return false;
8028         }
8029
8030       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
8031         {
8032           if (TARGET_HARD_FLOAT
8033               && (mode == SFmode
8034                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
8035             {
8036               *total = COSTS_N_INSNS (1);
8037               return false;
8038             }
8039         }
8040
8041       /* Requires a lib call */
8042       *total = COSTS_N_INSNS (20);
8043       return false;
8044
8045     default:
8046       return arm_rtx_costs_1 (x, outer_code, total, speed);
8047     }
8048 }
8049
8050
8051 /* RTX cost for XScale CPUs.  Thumb-2 is not supported on any xscale cores,
8052    so it can be ignored.  */
8053
8054 static bool
8055 arm_xscale_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
8056                       int *total, bool speed)
8057 {
8058   enum machine_mode mode = GET_MODE (x);
8059
8060   if (TARGET_THUMB)
8061     {
8062       *total = thumb1_rtx_costs (x, code, outer_code);
8063       return true;
8064     }
8065
8066   switch (code)
8067     {
8068     case COMPARE:
8069       if (GET_CODE (XEXP (x, 0)) != MULT)
8070         return arm_rtx_costs_1 (x, outer_code, total, speed);
8071
8072       /* A COMPARE of a MULT is slow on XScale; the muls instruction
8073          will stall until the multiplication is complete.  */
8074       *total = COSTS_N_INSNS (3);
8075       return false;
8076
8077     case MULT:
8078       /* There is no point basing this on the tuning, since it is always the
8079          fast variant if it exists at all.  */
8080       if (mode == DImode
8081           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
8082           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
8083               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
8084         {
8085           *total = COSTS_N_INSNS (2);
8086           return false;
8087         }
8088
8089
8090       if (mode == DImode)
8091         {
8092           *total = COSTS_N_INSNS (5);
8093           return false;
8094         }
8095
8096       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
8097         {
8098           /* If operand 1 is a constant we can more accurately
8099              calculate the cost of the multiply.  The multiplier can
8100              retire 15 bits on the first cycle and a further 12 on the
8101              second.  We do, of course, have to load the constant into
8102              a register first.  */
8103           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
8104           /* There's a general overhead of one cycle.  */
8105           int cost = 1;
8106           unsigned HOST_WIDE_INT masked_const;
8107
8108           if (i & 0x80000000)
8109             i = ~i;
8110
8111           i &= (unsigned HOST_WIDE_INT) 0xffffffff;
8112
8113           masked_const = i & 0xffff8000;
8114           if (masked_const != 0)
8115             {
8116               cost++;
8117               masked_const = i & 0xf8000000;
8118               if (masked_const != 0)
8119                 cost++;
8120             }
8121           *total = COSTS_N_INSNS (cost);
8122           return false;
8123         }
8124
8125       if (mode == SImode)
8126         {
8127           *total = COSTS_N_INSNS (3);
8128           return false;
8129         }
8130
8131       /* Requires a lib call */
8132       *total = COSTS_N_INSNS (20);
8133       return false;
8134
8135     default:
8136       return arm_rtx_costs_1 (x, outer_code, total, speed);
8137     }
8138 }
8139
8140
8141 /* RTX costs for 9e (and later) cores.  */
8142
8143 static bool
8144 arm_9e_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
8145                   int *total, bool speed)
8146 {
8147   enum machine_mode mode = GET_MODE (x);
8148
8149   if (TARGET_THUMB1)
8150     {
8151       switch (code)
8152         {
8153         case MULT:
8154           *total = COSTS_N_INSNS (3);
8155           return true;
8156
8157         default:
8158           *total = thumb1_rtx_costs (x, code, outer_code);
8159           return true;
8160         }
8161     }
8162
8163   switch (code)
8164     {
8165     case MULT:
8166       /* There is no point basing this on the tuning, since it is always the
8167          fast variant if it exists at all.  */
8168       if (mode == DImode
8169           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
8170           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
8171               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
8172         {
8173           *total = COSTS_N_INSNS (2);
8174           return false;
8175         }
8176
8177
8178       if (mode == DImode)
8179         {
8180           *total = COSTS_N_INSNS (5);
8181           return false;
8182         }
8183
8184       if (mode == SImode)
8185         {
8186           *total = COSTS_N_INSNS (2);
8187           return false;
8188         }
8189
8190       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
8191         {
8192           if (TARGET_HARD_FLOAT
8193               && (mode == SFmode
8194                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
8195             {
8196               *total = COSTS_N_INSNS (1);
8197               return false;
8198             }
8199         }
8200
8201       *total = COSTS_N_INSNS (20);
8202       return false;
8203
8204     default:
8205       return arm_rtx_costs_1 (x, outer_code, total, speed);
8206     }
8207 }
8208 /* All address computations that can be done are free, but rtx cost returns
8209    the same for practically all of them.  So we weight the different types
8210    of address here in the order (most pref first):
8211    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
8212 static inline int
8213 arm_arm_address_cost (rtx x)
8214 {
8215   enum rtx_code c  = GET_CODE (x);
8216
8217   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
8218     return 0;
8219   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
8220     return 10;
8221
8222   if (c == PLUS)
8223     {
8224       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
8225         return 2;
8226
8227       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
8228         return 3;
8229
8230       return 4;
8231     }
8232
8233   return 6;
8234 }
8235
8236 static inline int
8237 arm_thumb_address_cost (rtx x)
8238 {
8239   enum rtx_code c  = GET_CODE (x);
8240
8241   if (c == REG)
8242     return 1;
8243   if (c == PLUS
8244       && GET_CODE (XEXP (x, 0)) == REG
8245       && GET_CODE (XEXP (x, 1)) == CONST_INT)
8246     return 1;
8247
8248   return 2;
8249 }
8250
8251 static int
8252 arm_address_cost (rtx x, bool speed ATTRIBUTE_UNUSED)
8253 {
8254   return TARGET_32BIT ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
8255 }
8256
8257 /* Adjust cost hook for XScale.  */
8258 static bool
8259 xscale_sched_adjust_cost (rtx insn, rtx link, rtx dep, int * cost)
8260 {
8261   /* Some true dependencies can have a higher cost depending
8262      on precisely how certain input operands are used.  */
8263   if (REG_NOTE_KIND(link) == 0
8264       && recog_memoized (insn) >= 0
8265       && recog_memoized (dep) >= 0)
8266     {
8267       int shift_opnum = get_attr_shift (insn);
8268       enum attr_type attr_type = get_attr_type (dep);
8269
8270       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
8271          operand for INSN.  If we have a shifted input operand and the
8272          instruction we depend on is another ALU instruction, then we may
8273          have to account for an additional stall.  */
8274       if (shift_opnum != 0
8275           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
8276         {
8277           rtx shifted_operand;
8278           int opno;
8279
8280           /* Get the shifted operand.  */
8281           extract_insn (insn);
8282           shifted_operand = recog_data.operand[shift_opnum];
8283
8284           /* Iterate over all the operands in DEP.  If we write an operand
8285              that overlaps with SHIFTED_OPERAND, then we have increase the
8286              cost of this dependency.  */
8287           extract_insn (dep);
8288           preprocess_constraints ();
8289           for (opno = 0; opno < recog_data.n_operands; opno++)
8290             {
8291               /* We can ignore strict inputs.  */
8292               if (recog_data.operand_type[opno] == OP_IN)
8293                 continue;
8294
8295               if (reg_overlap_mentioned_p (recog_data.operand[opno],
8296                                            shifted_operand))
8297                 {
8298                   *cost = 2;
8299                   return false;
8300                 }
8301             }
8302         }
8303     }
8304   return true;
8305 }
8306
8307 /* Adjust cost hook for Cortex A9.  */
8308 static bool
8309 cortex_a9_sched_adjust_cost (rtx insn, rtx link, rtx dep, int * cost)
8310 {
8311   switch (REG_NOTE_KIND (link))
8312     {
8313     case REG_DEP_ANTI:
8314       *cost = 0;
8315       return false;
8316
8317     case REG_DEP_TRUE:
8318     case REG_DEP_OUTPUT:
8319         if (recog_memoized (insn) >= 0
8320             && recog_memoized (dep) >= 0)
8321           {
8322             if (GET_CODE (PATTERN (insn)) == SET)
8323               {
8324                 if (GET_MODE_CLASS 
8325                     (GET_MODE (SET_DEST (PATTERN (insn)))) == MODE_FLOAT
8326                   || GET_MODE_CLASS 
8327                     (GET_MODE (SET_SRC (PATTERN (insn)))) == MODE_FLOAT)
8328                   {
8329                     enum attr_type attr_type_insn = get_attr_type (insn);
8330                     enum attr_type attr_type_dep = get_attr_type (dep);
8331
8332                     /* By default all dependencies of the form
8333                        s0 = s0 <op> s1
8334                        s0 = s0 <op> s2
8335                        have an extra latency of 1 cycle because
8336                        of the input and output dependency in this
8337                        case. However this gets modeled as an true
8338                        dependency and hence all these checks.  */
8339                     if (REG_P (SET_DEST (PATTERN (insn)))
8340                         && REG_P (SET_DEST (PATTERN (dep)))
8341                         && reg_overlap_mentioned_p (SET_DEST (PATTERN (insn)),
8342                                                     SET_DEST (PATTERN (dep))))
8343                       {
8344                         /* FMACS is a special case where the dependant
8345                            instruction can be issued 3 cycles before
8346                            the normal latency in case of an output 
8347                            dependency.  */
8348                         if ((attr_type_insn == TYPE_FMACS
8349                              || attr_type_insn == TYPE_FMACD)
8350                             && (attr_type_dep == TYPE_FMACS
8351                                 || attr_type_dep == TYPE_FMACD))
8352                           {
8353                             if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
8354                               *cost = insn_default_latency (dep) - 3;
8355                             else
8356                               *cost = insn_default_latency (dep);
8357                             return false;
8358                           }
8359                         else
8360                           {
8361                             if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
8362                               *cost = insn_default_latency (dep) + 1;
8363                             else
8364                               *cost = insn_default_latency (dep);
8365                           }
8366                         return false;
8367                       }
8368                   }
8369               }
8370           }
8371         break;
8372
8373     default:
8374       gcc_unreachable ();
8375     }
8376
8377   return true;
8378 }
8379
8380 /* Adjust cost hook for FA726TE.  */
8381 static bool
8382 fa726te_sched_adjust_cost (rtx insn, rtx link, rtx dep, int * cost)
8383 {
8384   /* For FA726TE, true dependency on CPSR (i.e. set cond followed by predicated)
8385      have penalty of 3.  */
8386   if (REG_NOTE_KIND (link) == REG_DEP_TRUE
8387       && recog_memoized (insn) >= 0
8388       && recog_memoized (dep) >= 0
8389       && get_attr_conds (dep) == CONDS_SET)
8390     {
8391       /* Use of carry (e.g. 64-bit arithmetic) in ALU: 3-cycle latency.  */
8392       if (get_attr_conds (insn) == CONDS_USE
8393           && get_attr_type (insn) != TYPE_BRANCH)
8394         {
8395           *cost = 3;
8396           return false;
8397         }
8398
8399       if (GET_CODE (PATTERN (insn)) == COND_EXEC
8400           || get_attr_conds (insn) == CONDS_USE)
8401         {
8402           *cost = 0;
8403           return false;
8404         }
8405     }
8406
8407   return true;
8408 }
8409
8410 /* This function implements the target macro TARGET_SCHED_ADJUST_COST.
8411    It corrects the value of COST based on the relationship between
8412    INSN and DEP through the dependence LINK.  It returns the new
8413    value. There is a per-core adjust_cost hook to adjust scheduler costs
8414    and the per-core hook can choose to completely override the generic 
8415    adjust_cost function. Only put bits of code into arm_adjust_cost that 
8416    are common across all cores.  */
8417 static int
8418 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
8419 {
8420   rtx i_pat, d_pat;
8421
8422  /* When generating Thumb-1 code, we want to place flag-setting operations
8423     close to a conditional branch which depends on them, so that we can
8424     omit the comparison. */
8425   if (TARGET_THUMB1
8426       && REG_NOTE_KIND (link) == 0
8427       && recog_memoized (insn) == CODE_FOR_cbranchsi4_insn
8428       && recog_memoized (dep) >= 0
8429       && get_attr_conds (dep) == CONDS_SET)
8430     return 0;
8431
8432   if (current_tune->sched_adjust_cost != NULL)
8433     {
8434       if (!current_tune->sched_adjust_cost (insn, link, dep, &cost))
8435         return cost;
8436     }
8437
8438   /* XXX This is not strictly true for the FPA.  */
8439   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
8440       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
8441     return 0;
8442
8443   /* Call insns don't incur a stall, even if they follow a load.  */
8444   if (REG_NOTE_KIND (link) == 0
8445       && GET_CODE (insn) == CALL_INSN)
8446     return 1;
8447
8448   if ((i_pat = single_set (insn)) != NULL
8449       && GET_CODE (SET_SRC (i_pat)) == MEM
8450       && (d_pat = single_set (dep)) != NULL
8451       && GET_CODE (SET_DEST (d_pat)) == MEM)
8452     {
8453       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
8454       /* This is a load after a store, there is no conflict if the load reads
8455          from a cached area.  Assume that loads from the stack, and from the
8456          constant pool are cached, and that others will miss.  This is a
8457          hack.  */
8458
8459       if ((GET_CODE (src_mem) == SYMBOL_REF 
8460            && CONSTANT_POOL_ADDRESS_P (src_mem))
8461           || reg_mentioned_p (stack_pointer_rtx, src_mem)
8462           || reg_mentioned_p (frame_pointer_rtx, src_mem)
8463           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
8464         return 1;
8465     }
8466
8467   return cost;
8468 }
8469
8470 static int
8471 arm_default_branch_cost (bool speed_p, bool predictable_p ATTRIBUTE_UNUSED)
8472 {
8473   if (TARGET_32BIT)
8474     return (TARGET_THUMB2 && !speed_p) ? 1 : 4;
8475   else
8476     return (optimize > 0) ? 2 : 0;
8477 }
8478
8479 static int
8480 arm_cortex_a5_branch_cost (bool speed_p, bool predictable_p)
8481 {
8482   return speed_p ? 0 : arm_default_branch_cost (speed_p, predictable_p);
8483 }
8484
8485 static int fp_consts_inited = 0;
8486
8487 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
8488 static const char * const strings_fp[8] =
8489 {
8490   "0",   "1",   "2",   "3",
8491   "4",   "5",   "0.5", "10"
8492 };
8493
8494 static REAL_VALUE_TYPE values_fp[8];
8495
8496 static void
8497 init_fp_table (void)
8498 {
8499   int i;
8500   REAL_VALUE_TYPE r;
8501
8502   if (TARGET_VFP)
8503     fp_consts_inited = 1;
8504   else
8505     fp_consts_inited = 8;
8506
8507   for (i = 0; i < fp_consts_inited; i++)
8508     {
8509       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
8510       values_fp[i] = r;
8511     }
8512 }
8513
8514 /* Return TRUE if rtx X is a valid immediate FP constant.  */
8515 int
8516 arm_const_double_rtx (rtx x)
8517 {
8518   REAL_VALUE_TYPE r;
8519   int i;
8520
8521   if (!fp_consts_inited)
8522     init_fp_table ();
8523
8524   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8525   if (REAL_VALUE_MINUS_ZERO (r))
8526     return 0;
8527
8528   for (i = 0; i < fp_consts_inited; i++)
8529     if (REAL_VALUES_EQUAL (r, values_fp[i]))
8530       return 1;
8531
8532   return 0;
8533 }
8534
8535 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
8536 int
8537 neg_const_double_rtx_ok_for_fpa (rtx x)
8538 {
8539   REAL_VALUE_TYPE r;
8540   int i;
8541
8542   if (!fp_consts_inited)
8543     init_fp_table ();
8544
8545   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8546   r = real_value_negate (&r);
8547   if (REAL_VALUE_MINUS_ZERO (r))
8548     return 0;
8549
8550   for (i = 0; i < 8; i++)
8551     if (REAL_VALUES_EQUAL (r, values_fp[i]))
8552       return 1;
8553
8554   return 0;
8555 }
8556
8557
8558 /* VFPv3 has a fairly wide range of representable immediates, formed from
8559    "quarter-precision" floating-point values. These can be evaluated using this
8560    formula (with ^ for exponentiation):
8561
8562      -1^s * n * 2^-r
8563
8564    Where 's' is a sign bit (0/1), 'n' and 'r' are integers such that
8565    16 <= n <= 31 and 0 <= r <= 7.
8566
8567    These values are mapped onto an 8-bit integer ABCDEFGH s.t.
8568
8569      - A (most-significant) is the sign bit.
8570      - BCD are the exponent (encoded as r XOR 3).
8571      - EFGH are the mantissa (encoded as n - 16).
8572 */
8573
8574 /* Return an integer index for a VFPv3 immediate operand X suitable for the
8575    fconst[sd] instruction, or -1 if X isn't suitable.  */
8576 static int
8577 vfp3_const_double_index (rtx x)
8578 {
8579   REAL_VALUE_TYPE r, m;
8580   int sign, exponent;
8581   unsigned HOST_WIDE_INT mantissa, mant_hi;
8582   unsigned HOST_WIDE_INT mask;
8583   HOST_WIDE_INT m1, m2;
8584   int point_pos = 2 * HOST_BITS_PER_WIDE_INT - 1;
8585
8586   if (!TARGET_VFP3 || GET_CODE (x) != CONST_DOUBLE)
8587     return -1;
8588
8589   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8590
8591   /* We can't represent these things, so detect them first.  */
8592   if (REAL_VALUE_ISINF (r) || REAL_VALUE_ISNAN (r) || REAL_VALUE_MINUS_ZERO (r))
8593     return -1;
8594
8595   /* Extract sign, exponent and mantissa.  */
8596   sign = REAL_VALUE_NEGATIVE (r) ? 1 : 0;
8597   r = real_value_abs (&r);
8598   exponent = REAL_EXP (&r);
8599   /* For the mantissa, we expand into two HOST_WIDE_INTS, apart from the
8600      highest (sign) bit, with a fixed binary point at bit point_pos.
8601      WARNING: If there's ever a VFP version which uses more than 2 * H_W_I - 1
8602      bits for the mantissa, this may fail (low bits would be lost).  */
8603   real_ldexp (&m, &r, point_pos - exponent);
8604   REAL_VALUE_TO_INT (&m1, &m2, m);
8605   mantissa = m1;
8606   mant_hi = m2;
8607
8608   /* If there are bits set in the low part of the mantissa, we can't
8609      represent this value.  */
8610   if (mantissa != 0)
8611     return -1;
8612
8613   /* Now make it so that mantissa contains the most-significant bits, and move
8614      the point_pos to indicate that the least-significant bits have been
8615      discarded.  */
8616   point_pos -= HOST_BITS_PER_WIDE_INT;
8617   mantissa = mant_hi;
8618
8619   /* We can permit four significant bits of mantissa only, plus a high bit
8620      which is always 1.  */
8621   mask = ((unsigned HOST_WIDE_INT)1 << (point_pos - 5)) - 1;
8622   if ((mantissa & mask) != 0)
8623     return -1;
8624
8625   /* Now we know the mantissa is in range, chop off the unneeded bits.  */
8626   mantissa >>= point_pos - 5;
8627
8628   /* The mantissa may be zero. Disallow that case. (It's possible to load the
8629      floating-point immediate zero with Neon using an integer-zero load, but
8630      that case is handled elsewhere.)  */
8631   if (mantissa == 0)
8632     return -1;
8633
8634   gcc_assert (mantissa >= 16 && mantissa <= 31);
8635
8636   /* The value of 5 here would be 4 if GCC used IEEE754-like encoding (where
8637      normalized significands are in the range [1, 2). (Our mantissa is shifted
8638      left 4 places at this point relative to normalized IEEE754 values).  GCC
8639      internally uses [0.5, 1) (see real.c), so the exponent returned from
8640      REAL_EXP must be altered.  */
8641   exponent = 5 - exponent;
8642
8643   if (exponent < 0 || exponent > 7)
8644     return -1;
8645
8646   /* Sign, mantissa and exponent are now in the correct form to plug into the
8647      formula described in the comment above.  */
8648   return (sign << 7) | ((exponent ^ 3) << 4) | (mantissa - 16);
8649 }
8650
8651 /* Return TRUE if rtx X is a valid immediate VFPv3 constant.  */
8652 int
8653 vfp3_const_double_rtx (rtx x)
8654 {
8655   if (!TARGET_VFP3)
8656     return 0;
8657
8658   return vfp3_const_double_index (x) != -1;
8659 }
8660
8661 /* Recognize immediates which can be used in various Neon instructions. Legal
8662    immediates are described by the following table (for VMVN variants, the
8663    bitwise inverse of the constant shown is recognized. In either case, VMOV
8664    is output and the correct instruction to use for a given constant is chosen
8665    by the assembler). The constant shown is replicated across all elements of
8666    the destination vector.
8667
8668    insn elems variant constant (binary)
8669    ---- ----- ------- -----------------
8670    vmov  i32     0    00000000 00000000 00000000 abcdefgh
8671    vmov  i32     1    00000000 00000000 abcdefgh 00000000
8672    vmov  i32     2    00000000 abcdefgh 00000000 00000000
8673    vmov  i32     3    abcdefgh 00000000 00000000 00000000
8674    vmov  i16     4    00000000 abcdefgh
8675    vmov  i16     5    abcdefgh 00000000
8676    vmvn  i32     6    00000000 00000000 00000000 abcdefgh
8677    vmvn  i32     7    00000000 00000000 abcdefgh 00000000
8678    vmvn  i32     8    00000000 abcdefgh 00000000 00000000
8679    vmvn  i32     9    abcdefgh 00000000 00000000 00000000
8680    vmvn  i16    10    00000000 abcdefgh
8681    vmvn  i16    11    abcdefgh 00000000
8682    vmov  i32    12    00000000 00000000 abcdefgh 11111111
8683    vmvn  i32    13    00000000 00000000 abcdefgh 11111111
8684    vmov  i32    14    00000000 abcdefgh 11111111 11111111
8685    vmvn  i32    15    00000000 abcdefgh 11111111 11111111
8686    vmov   i8    16    abcdefgh
8687    vmov  i64    17    aaaaaaaa bbbbbbbb cccccccc dddddddd
8688                       eeeeeeee ffffffff gggggggg hhhhhhhh
8689    vmov  f32    18    aBbbbbbc defgh000 00000000 00000000
8690
8691    For case 18, B = !b. Representable values are exactly those accepted by
8692    vfp3_const_double_index, but are output as floating-point numbers rather
8693    than indices.
8694
8695    Variants 0-5 (inclusive) may also be used as immediates for the second
8696    operand of VORR/VBIC instructions.
8697
8698    The INVERSE argument causes the bitwise inverse of the given operand to be
8699    recognized instead (used for recognizing legal immediates for the VAND/VORN
8700    pseudo-instructions). If INVERSE is true, the value placed in *MODCONST is
8701    *not* inverted (i.e. the pseudo-instruction forms vand/vorn should still be
8702    output, rather than the real insns vbic/vorr).
8703
8704    INVERSE makes no difference to the recognition of float vectors.
8705
8706    The return value is the variant of immediate as shown in the above table, or
8707    -1 if the given value doesn't match any of the listed patterns.
8708 */
8709 static int
8710 neon_valid_immediate (rtx op, enum machine_mode mode, int inverse,
8711                       rtx *modconst, int *elementwidth)
8712 {
8713 #define CHECK(STRIDE, ELSIZE, CLASS, TEST)      \
8714   matches = 1;                                  \
8715   for (i = 0; i < idx; i += (STRIDE))           \
8716     if (!(TEST))                                \
8717       matches = 0;                              \
8718   if (matches)                                  \
8719     {                                           \
8720       immtype = (CLASS);                        \
8721       elsize = (ELSIZE);                        \
8722       break;                                    \
8723     }
8724
8725   unsigned int i, elsize = 0, idx = 0, n_elts = CONST_VECTOR_NUNITS (op);
8726   unsigned int innersize = GET_MODE_SIZE (GET_MODE_INNER (mode));
8727   unsigned char bytes[16];
8728   int immtype = -1, matches;
8729   unsigned int invmask = inverse ? 0xff : 0;
8730
8731   /* Vectors of float constants.  */
8732   if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
8733     {
8734       rtx el0 = CONST_VECTOR_ELT (op, 0);
8735       REAL_VALUE_TYPE r0;
8736
8737       if (!vfp3_const_double_rtx (el0))
8738         return -1;
8739
8740       REAL_VALUE_FROM_CONST_DOUBLE (r0, el0);
8741
8742       for (i = 1; i < n_elts; i++)
8743         {
8744           rtx elt = CONST_VECTOR_ELT (op, i);
8745           REAL_VALUE_TYPE re;
8746
8747           REAL_VALUE_FROM_CONST_DOUBLE (re, elt);
8748
8749           if (!REAL_VALUES_EQUAL (r0, re))
8750             return -1;
8751         }
8752
8753       if (modconst)
8754         *modconst = CONST_VECTOR_ELT (op, 0);
8755
8756       if (elementwidth)
8757         *elementwidth = 0;
8758
8759       return 18;
8760     }
8761
8762   /* Splat vector constant out into a byte vector.  */
8763   for (i = 0; i < n_elts; i++)
8764     {
8765       rtx el = CONST_VECTOR_ELT (op, i);
8766       unsigned HOST_WIDE_INT elpart;
8767       unsigned int part, parts;
8768
8769       if (GET_CODE (el) == CONST_INT)
8770         {
8771           elpart = INTVAL (el);
8772           parts = 1;
8773         }
8774       else if (GET_CODE (el) == CONST_DOUBLE)
8775         {
8776           elpart = CONST_DOUBLE_LOW (el);
8777           parts = 2;
8778         }
8779       else
8780         gcc_unreachable ();
8781
8782       for (part = 0; part < parts; part++)
8783         {
8784           unsigned int byte;
8785           for (byte = 0; byte < innersize; byte++)
8786             {
8787               bytes[idx++] = (elpart & 0xff) ^ invmask;
8788               elpart >>= BITS_PER_UNIT;
8789             }
8790           if (GET_CODE (el) == CONST_DOUBLE)
8791             elpart = CONST_DOUBLE_HIGH (el);
8792         }
8793     }
8794
8795   /* Sanity check.  */
8796   gcc_assert (idx == GET_MODE_SIZE (mode));
8797
8798   do
8799     {
8800       CHECK (4, 32, 0, bytes[i] == bytes[0] && bytes[i + 1] == 0
8801                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
8802
8803       CHECK (4, 32, 1, bytes[i] == 0 && bytes[i + 1] == bytes[1]
8804                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
8805
8806       CHECK (4, 32, 2, bytes[i] == 0 && bytes[i + 1] == 0
8807                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
8808
8809       CHECK (4, 32, 3, bytes[i] == 0 && bytes[i + 1] == 0
8810                        && bytes[i + 2] == 0 && bytes[i + 3] == bytes[3]);
8811
8812       CHECK (2, 16, 4, bytes[i] == bytes[0] && bytes[i + 1] == 0);
8813
8814       CHECK (2, 16, 5, bytes[i] == 0 && bytes[i + 1] == bytes[1]);
8815
8816       CHECK (4, 32, 6, bytes[i] == bytes[0] && bytes[i + 1] == 0xff
8817                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
8818
8819       CHECK (4, 32, 7, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
8820                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
8821
8822       CHECK (4, 32, 8, bytes[i] == 0xff && bytes[i + 1] == 0xff
8823                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
8824
8825       CHECK (4, 32, 9, bytes[i] == 0xff && bytes[i + 1] == 0xff
8826                        && bytes[i + 2] == 0xff && bytes[i + 3] == bytes[3]);
8827
8828       CHECK (2, 16, 10, bytes[i] == bytes[0] && bytes[i + 1] == 0xff);
8829
8830       CHECK (2, 16, 11, bytes[i] == 0xff && bytes[i + 1] == bytes[1]);
8831
8832       CHECK (4, 32, 12, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
8833                         && bytes[i + 2] == 0 && bytes[i + 3] == 0);
8834
8835       CHECK (4, 32, 13, bytes[i] == 0 && bytes[i + 1] == bytes[1]
8836                         && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
8837
8838       CHECK (4, 32, 14, bytes[i] == 0xff && bytes[i + 1] == 0xff
8839                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
8840
8841       CHECK (4, 32, 15, bytes[i] == 0 && bytes[i + 1] == 0
8842                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
8843
8844       CHECK (1, 8, 16, bytes[i] == bytes[0]);
8845
8846       CHECK (1, 64, 17, (bytes[i] == 0 || bytes[i] == 0xff)
8847                         && bytes[i] == bytes[(i + 8) % idx]);
8848     }
8849   while (0);
8850
8851   if (immtype == -1)
8852     return -1;
8853
8854   if (elementwidth)
8855     *elementwidth = elsize;
8856
8857   if (modconst)
8858     {
8859       unsigned HOST_WIDE_INT imm = 0;
8860
8861       /* Un-invert bytes of recognized vector, if necessary.  */
8862       if (invmask != 0)
8863         for (i = 0; i < idx; i++)
8864           bytes[i] ^= invmask;
8865
8866       if (immtype == 17)
8867         {
8868           /* FIXME: Broken on 32-bit H_W_I hosts.  */
8869           gcc_assert (sizeof (HOST_WIDE_INT) == 8);
8870
8871           for (i = 0; i < 8; i++)
8872             imm |= (unsigned HOST_WIDE_INT) (bytes[i] ? 0xff : 0)
8873                    << (i * BITS_PER_UNIT);
8874
8875           *modconst = GEN_INT (imm);
8876         }
8877       else
8878         {
8879           unsigned HOST_WIDE_INT imm = 0;
8880
8881           for (i = 0; i < elsize / BITS_PER_UNIT; i++)
8882             imm |= (unsigned HOST_WIDE_INT) bytes[i] << (i * BITS_PER_UNIT);
8883
8884           *modconst = GEN_INT (imm);
8885         }
8886     }
8887
8888   return immtype;
8889 #undef CHECK
8890 }
8891
8892 /* Return TRUE if rtx X is legal for use as either a Neon VMOV (or, implicitly,
8893    VMVN) immediate. Write back width per element to *ELEMENTWIDTH (or zero for
8894    float elements), and a modified constant (whatever should be output for a
8895    VMOV) in *MODCONST.  */
8896
8897 int
8898 neon_immediate_valid_for_move (rtx op, enum machine_mode mode,
8899                                rtx *modconst, int *elementwidth)
8900 {
8901   rtx tmpconst;
8902   int tmpwidth;
8903   int retval = neon_valid_immediate (op, mode, 0, &tmpconst, &tmpwidth);
8904
8905   if (retval == -1)
8906     return 0;
8907
8908   if (modconst)
8909     *modconst = tmpconst;
8910
8911   if (elementwidth)
8912     *elementwidth = tmpwidth;
8913
8914   return 1;
8915 }
8916
8917 /* Return TRUE if rtx X is legal for use in a VORR or VBIC instruction.  If
8918    the immediate is valid, write a constant suitable for using as an operand
8919    to VORR/VBIC/VAND/VORN to *MODCONST and the corresponding element width to
8920    *ELEMENTWIDTH. See neon_valid_immediate for description of INVERSE.  */
8921
8922 int
8923 neon_immediate_valid_for_logic (rtx op, enum machine_mode mode, int inverse,
8924                                 rtx *modconst, int *elementwidth)
8925 {
8926   rtx tmpconst;
8927   int tmpwidth;
8928   int retval = neon_valid_immediate (op, mode, inverse, &tmpconst, &tmpwidth);
8929
8930   if (retval < 0 || retval > 5)
8931     return 0;
8932
8933   if (modconst)
8934     *modconst = tmpconst;
8935
8936   if (elementwidth)
8937     *elementwidth = tmpwidth;
8938
8939   return 1;
8940 }
8941
8942 /* Return TRUE if rtx OP is legal for use in a VSHR or VSHL instruction.  If
8943    the immediate is valid, write a constant suitable for using as an operand
8944    to VSHR/VSHL to *MODCONST and the corresponding element width to
8945    *ELEMENTWIDTH. ISLEFTSHIFT is for determine left or right shift,
8946    because they have different limitations.  */
8947
8948 int
8949 neon_immediate_valid_for_shift (rtx op, enum machine_mode mode,
8950                                 rtx *modconst, int *elementwidth,
8951                                 bool isleftshift)
8952 {
8953   unsigned int innersize = GET_MODE_SIZE (GET_MODE_INNER (mode));
8954   unsigned int n_elts = CONST_VECTOR_NUNITS (op), i;
8955   unsigned HOST_WIDE_INT last_elt = 0;
8956   unsigned HOST_WIDE_INT maxshift;
8957
8958   /* Split vector constant out into a byte vector.  */
8959   for (i = 0; i < n_elts; i++)
8960     {
8961       rtx el = CONST_VECTOR_ELT (op, i);
8962       unsigned HOST_WIDE_INT elpart;
8963
8964       if (GET_CODE (el) == CONST_INT)
8965         elpart = INTVAL (el);
8966       else if (GET_CODE (el) == CONST_DOUBLE)
8967         return 0;
8968       else
8969         gcc_unreachable ();
8970
8971       if (i != 0 && elpart != last_elt)
8972         return 0;
8973
8974       last_elt = elpart;
8975     }
8976
8977   /* Shift less than element size.  */
8978   maxshift = innersize * 8;
8979
8980   if (isleftshift)
8981     {
8982       /* Left shift immediate value can be from 0 to <size>-1.  */
8983       if (last_elt >= maxshift)
8984         return 0;
8985     }
8986   else
8987     {
8988       /* Right shift immediate value can be from 1 to <size>.  */
8989       if (last_elt == 0 || last_elt > maxshift)
8990         return 0;
8991     }
8992
8993   if (elementwidth)
8994     *elementwidth = innersize * 8;
8995
8996   if (modconst)
8997     *modconst = CONST_VECTOR_ELT (op, 0);
8998
8999   return 1;
9000 }
9001
9002 /* Return a string suitable for output of Neon immediate logic operation
9003    MNEM.  */
9004
9005 char *
9006 neon_output_logic_immediate (const char *mnem, rtx *op2, enum machine_mode mode,
9007                              int inverse, int quad)
9008 {
9009   int width, is_valid;
9010   static char templ[40];
9011
9012   is_valid = neon_immediate_valid_for_logic (*op2, mode, inverse, op2, &width);
9013
9014   gcc_assert (is_valid != 0);
9015
9016   if (quad)
9017     sprintf (templ, "%s.i%d\t%%q0, %%2", mnem, width);
9018   else
9019     sprintf (templ, "%s.i%d\t%%P0, %%2", mnem, width);
9020
9021   return templ;
9022 }
9023
9024 /* Return a string suitable for output of Neon immediate shift operation
9025    (VSHR or VSHL) MNEM.  */
9026
9027 char *
9028 neon_output_shift_immediate (const char *mnem, char sign, rtx *op2,
9029                              enum machine_mode mode, int quad,
9030                              bool isleftshift)
9031 {
9032   int width, is_valid;
9033   static char templ[40];
9034
9035   is_valid = neon_immediate_valid_for_shift (*op2, mode, op2, &width, isleftshift);
9036   gcc_assert (is_valid != 0);
9037
9038   if (quad)
9039     sprintf (templ, "%s.%c%d\t%%q0, %%q1, %%2", mnem, sign, width);
9040   else
9041     sprintf (templ, "%s.%c%d\t%%P0, %%P1, %%2", mnem, sign, width);
9042
9043   return templ;
9044 }
9045
9046 /* Output a sequence of pairwise operations to implement a reduction.
9047    NOTE: We do "too much work" here, because pairwise operations work on two
9048    registers-worth of operands in one go. Unfortunately we can't exploit those
9049    extra calculations to do the full operation in fewer steps, I don't think.
9050    Although all vector elements of the result but the first are ignored, we
9051    actually calculate the same result in each of the elements. An alternative
9052    such as initially loading a vector with zero to use as each of the second
9053    operands would use up an additional register and take an extra instruction,
9054    for no particular gain.  */
9055
9056 void
9057 neon_pairwise_reduce (rtx op0, rtx op1, enum machine_mode mode,
9058                       rtx (*reduc) (rtx, rtx, rtx))
9059 {
9060   enum machine_mode inner = GET_MODE_INNER (mode);
9061   unsigned int i, parts = GET_MODE_SIZE (mode) / GET_MODE_SIZE (inner);
9062   rtx tmpsum = op1;
9063
9064   for (i = parts / 2; i >= 1; i /= 2)
9065     {
9066       rtx dest = (i == 1) ? op0 : gen_reg_rtx (mode);
9067       emit_insn (reduc (dest, tmpsum, tmpsum));
9068       tmpsum = dest;
9069     }
9070 }
9071
9072 /* If VALS is a vector constant that can be loaded into a register
9073    using VDUP, generate instructions to do so and return an RTX to
9074    assign to the register.  Otherwise return NULL_RTX.  */
9075
9076 static rtx
9077 neon_vdup_constant (rtx vals)
9078 {
9079   enum machine_mode mode = GET_MODE (vals);
9080   enum machine_mode inner_mode = GET_MODE_INNER (mode);
9081   int n_elts = GET_MODE_NUNITS (mode);
9082   bool all_same = true;
9083   rtx x;
9084   int i;
9085
9086   if (GET_CODE (vals) != CONST_VECTOR || GET_MODE_SIZE (inner_mode) > 4)
9087     return NULL_RTX;
9088
9089   for (i = 0; i < n_elts; ++i)
9090     {
9091       x = XVECEXP (vals, 0, i);
9092       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
9093         all_same = false;
9094     }
9095
9096   if (!all_same)
9097     /* The elements are not all the same.  We could handle repeating
9098        patterns of a mode larger than INNER_MODE here (e.g. int8x8_t
9099        {0, C, 0, C, 0, C, 0, C} which can be loaded using
9100        vdup.i16).  */
9101     return NULL_RTX;
9102
9103   /* We can load this constant by using VDUP and a constant in a
9104      single ARM register.  This will be cheaper than a vector
9105      load.  */
9106
9107   x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, 0));
9108   return gen_rtx_VEC_DUPLICATE (mode, x);
9109 }
9110
9111 /* Generate code to load VALS, which is a PARALLEL containing only
9112    constants (for vec_init) or CONST_VECTOR, efficiently into a
9113    register.  Returns an RTX to copy into the register, or NULL_RTX
9114    for a PARALLEL that can not be converted into a CONST_VECTOR.  */
9115
9116 rtx
9117 neon_make_constant (rtx vals)
9118 {
9119   enum machine_mode mode = GET_MODE (vals);
9120   rtx target;
9121   rtx const_vec = NULL_RTX;
9122   int n_elts = GET_MODE_NUNITS (mode);
9123   int n_const = 0;
9124   int i;
9125
9126   if (GET_CODE (vals) == CONST_VECTOR)
9127     const_vec = vals;
9128   else if (GET_CODE (vals) == PARALLEL)
9129     {
9130       /* A CONST_VECTOR must contain only CONST_INTs and
9131          CONST_DOUBLEs, but CONSTANT_P allows more (e.g. SYMBOL_REF).
9132          Only store valid constants in a CONST_VECTOR.  */
9133       for (i = 0; i < n_elts; ++i)
9134         {
9135           rtx x = XVECEXP (vals, 0, i);
9136           if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
9137             n_const++;
9138         }
9139       if (n_const == n_elts)
9140         const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
9141     }
9142   else
9143     gcc_unreachable ();
9144
9145   if (const_vec != NULL
9146       && neon_immediate_valid_for_move (const_vec, mode, NULL, NULL))
9147     /* Load using VMOV.  On Cortex-A8 this takes one cycle.  */
9148     return const_vec;
9149   else if ((target = neon_vdup_constant (vals)) != NULL_RTX)
9150     /* Loaded using VDUP.  On Cortex-A8 the VDUP takes one NEON
9151        pipeline cycle; creating the constant takes one or two ARM
9152        pipeline cycles.  */
9153     return target;
9154   else if (const_vec != NULL_RTX)
9155     /* Load from constant pool.  On Cortex-A8 this takes two cycles
9156        (for either double or quad vectors).  We can not take advantage
9157        of single-cycle VLD1 because we need a PC-relative addressing
9158        mode.  */
9159     return const_vec;
9160   else
9161     /* A PARALLEL containing something not valid inside CONST_VECTOR.
9162        We can not construct an initializer.  */
9163     return NULL_RTX;
9164 }
9165
9166 /* Initialize vector TARGET to VALS.  */
9167
9168 void
9169 neon_expand_vector_init (rtx target, rtx vals)
9170 {
9171   enum machine_mode mode = GET_MODE (target);
9172   enum machine_mode inner_mode = GET_MODE_INNER (mode);
9173   int n_elts = GET_MODE_NUNITS (mode);
9174   int n_var = 0, one_var = -1;
9175   bool all_same = true;
9176   rtx x, mem;
9177   int i;
9178
9179   for (i = 0; i < n_elts; ++i)
9180     {
9181       x = XVECEXP (vals, 0, i);
9182       if (!CONSTANT_P (x))
9183         ++n_var, one_var = i;
9184
9185       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
9186         all_same = false;
9187     }
9188
9189   if (n_var == 0)
9190     {
9191       rtx constant = neon_make_constant (vals);
9192       if (constant != NULL_RTX)
9193         {
9194           emit_move_insn (target, constant);
9195           return;
9196         }
9197     }
9198
9199   /* Splat a single non-constant element if we can.  */
9200   if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
9201     {
9202       x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, 0));
9203       emit_insn (gen_rtx_SET (VOIDmode, target,
9204                               gen_rtx_VEC_DUPLICATE (mode, x)));
9205       return;
9206     }
9207
9208   /* One field is non-constant.  Load constant then overwrite varying
9209      field.  This is more efficient than using the stack.  */
9210   if (n_var == 1)
9211     {
9212       rtx copy = copy_rtx (vals);
9213       rtx index = GEN_INT (one_var);
9214
9215       /* Load constant part of vector, substitute neighboring value for
9216          varying element.  */
9217       XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
9218       neon_expand_vector_init (target, copy);
9219
9220       /* Insert variable.  */
9221       x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, one_var));
9222       switch (mode)
9223         {
9224         case V8QImode:
9225           emit_insn (gen_neon_vset_lanev8qi (target, x, target, index));
9226           break;
9227         case V16QImode:
9228           emit_insn (gen_neon_vset_lanev16qi (target, x, target, index));
9229           break;
9230         case V4HImode:
9231           emit_insn (gen_neon_vset_lanev4hi (target, x, target, index));
9232           break;
9233         case V8HImode:
9234           emit_insn (gen_neon_vset_lanev8hi (target, x, target, index));
9235           break;
9236         case V2SImode:
9237           emit_insn (gen_neon_vset_lanev2si (target, x, target, index));
9238           break;
9239         case V4SImode:
9240           emit_insn (gen_neon_vset_lanev4si (target, x, target, index));
9241           break;
9242         case V2SFmode:
9243           emit_insn (gen_neon_vset_lanev2sf (target, x, target, index));
9244           break;
9245         case V4SFmode:
9246           emit_insn (gen_neon_vset_lanev4sf (target, x, target, index));
9247           break;
9248         case V2DImode:
9249           emit_insn (gen_neon_vset_lanev2di (target, x, target, index));
9250           break;
9251         default:
9252           gcc_unreachable ();
9253         }
9254       return;
9255     }
9256
9257   /* Construct the vector in memory one field at a time
9258      and load the whole vector.  */
9259   mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), 0);
9260   for (i = 0; i < n_elts; i++)
9261     emit_move_insn (adjust_address_nv (mem, inner_mode,
9262                                     i * GET_MODE_SIZE (inner_mode)),
9263                     XVECEXP (vals, 0, i));
9264   emit_move_insn (target, mem);
9265 }
9266
9267 /* Ensure OPERAND lies between LOW (inclusive) and HIGH (exclusive).  Raise
9268    ERR if it doesn't.  FIXME: NEON bounds checks occur late in compilation, so
9269    reported source locations are bogus.  */
9270
9271 static void
9272 bounds_check (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high,
9273               const char *err)
9274 {
9275   HOST_WIDE_INT lane;
9276
9277   gcc_assert (GET_CODE (operand) == CONST_INT);
9278
9279   lane = INTVAL (operand);
9280
9281   if (lane < low || lane >= high)
9282     error (err);
9283 }
9284
9285 /* Bounds-check lanes.  */
9286
9287 void
9288 neon_lane_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
9289 {
9290   bounds_check (operand, low, high, "lane out of range");
9291 }
9292
9293 /* Bounds-check constants.  */
9294
9295 void
9296 neon_const_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
9297 {
9298   bounds_check (operand, low, high, "constant out of range");
9299 }
9300
9301 HOST_WIDE_INT
9302 neon_element_bits (enum machine_mode mode)
9303 {
9304   if (mode == DImode)
9305     return GET_MODE_BITSIZE (mode);
9306   else
9307     return GET_MODE_BITSIZE (GET_MODE_INNER (mode));
9308 }
9309
9310 \f
9311 /* Predicates for `match_operand' and `match_operator'.  */
9312
9313 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
9314 int
9315 cirrus_memory_offset (rtx op)
9316 {
9317   /* Reject eliminable registers.  */
9318   if (! (reload_in_progress || reload_completed)
9319       && (   reg_mentioned_p (frame_pointer_rtx, op)
9320           || reg_mentioned_p (arg_pointer_rtx, op)
9321           || reg_mentioned_p (virtual_incoming_args_rtx, op)
9322           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
9323           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
9324           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
9325     return 0;
9326
9327   if (GET_CODE (op) == MEM)
9328     {
9329       rtx ind;
9330
9331       ind = XEXP (op, 0);
9332
9333       /* Match: (mem (reg)).  */
9334       if (GET_CODE (ind) == REG)
9335         return 1;
9336
9337       /* Match:
9338          (mem (plus (reg)
9339                     (const))).  */
9340       if (GET_CODE (ind) == PLUS
9341           && GET_CODE (XEXP (ind, 0)) == REG
9342           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
9343           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
9344         return 1;
9345     }
9346
9347   return 0;
9348 }
9349
9350 /* Return TRUE if OP is a valid coprocessor memory address pattern.
9351    WB is true if full writeback address modes are allowed and is false
9352    if limited writeback address modes (POST_INC and PRE_DEC) are
9353    allowed.  */
9354
9355 int
9356 arm_coproc_mem_operand (rtx op, bool wb)
9357 {
9358   rtx ind;
9359
9360   /* Reject eliminable registers.  */
9361   if (! (reload_in_progress || reload_completed)
9362       && (   reg_mentioned_p (frame_pointer_rtx, op)
9363           || reg_mentioned_p (arg_pointer_rtx, op)
9364           || reg_mentioned_p (virtual_incoming_args_rtx, op)
9365           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
9366           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
9367           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
9368     return FALSE;
9369
9370   /* Constants are converted into offsets from labels.  */
9371   if (GET_CODE (op) != MEM)
9372     return FALSE;
9373
9374   ind = XEXP (op, 0);
9375
9376   if (reload_completed
9377       && (GET_CODE (ind) == LABEL_REF
9378           || (GET_CODE (ind) == CONST
9379               && GET_CODE (XEXP (ind, 0)) == PLUS
9380               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
9381               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
9382     return TRUE;
9383
9384   /* Match: (mem (reg)).  */
9385   if (GET_CODE (ind) == REG)
9386     return arm_address_register_rtx_p (ind, 0);
9387
9388   /* Autoincremment addressing modes.  POST_INC and PRE_DEC are
9389      acceptable in any case (subject to verification by
9390      arm_address_register_rtx_p).  We need WB to be true to accept
9391      PRE_INC and POST_DEC.  */
9392   if (GET_CODE (ind) == POST_INC
9393       || GET_CODE (ind) == PRE_DEC
9394       || (wb
9395           && (GET_CODE (ind) == PRE_INC
9396               || GET_CODE (ind) == POST_DEC)))
9397     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
9398
9399   if (wb
9400       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
9401       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
9402       && GET_CODE (XEXP (ind, 1)) == PLUS
9403       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
9404     ind = XEXP (ind, 1);
9405
9406   /* Match:
9407      (plus (reg)
9408            (const)).  */
9409   if (GET_CODE (ind) == PLUS
9410       && GET_CODE (XEXP (ind, 0)) == REG
9411       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
9412       && GET_CODE (XEXP (ind, 1)) == CONST_INT
9413       && INTVAL (XEXP (ind, 1)) > -1024
9414       && INTVAL (XEXP (ind, 1)) <  1024
9415       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
9416     return TRUE;
9417
9418   return FALSE;
9419 }
9420
9421 /* Return TRUE if OP is a memory operand which we can load or store a vector
9422    to/from. TYPE is one of the following values:
9423     0 - Vector load/stor (vldr)
9424     1 - Core registers (ldm)
9425     2 - Element/structure loads (vld1)
9426  */
9427 int
9428 neon_vector_mem_operand (rtx op, int type)
9429 {
9430   rtx ind;
9431
9432   /* Reject eliminable registers.  */
9433   if (! (reload_in_progress || reload_completed)
9434       && (   reg_mentioned_p (frame_pointer_rtx, op)
9435           || reg_mentioned_p (arg_pointer_rtx, op)
9436           || reg_mentioned_p (virtual_incoming_args_rtx, op)
9437           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
9438           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
9439           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
9440     return FALSE;
9441
9442   /* Constants are converted into offsets from labels.  */
9443   if (GET_CODE (op) != MEM)
9444     return FALSE;
9445
9446   ind = XEXP (op, 0);
9447
9448   if (reload_completed
9449       && (GET_CODE (ind) == LABEL_REF
9450           || (GET_CODE (ind) == CONST
9451               && GET_CODE (XEXP (ind, 0)) == PLUS
9452               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
9453               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
9454     return TRUE;
9455
9456   /* Match: (mem (reg)).  */
9457   if (GET_CODE (ind) == REG)
9458     return arm_address_register_rtx_p (ind, 0);
9459
9460   /* Allow post-increment with Neon registers.  */
9461   if ((type != 1 && GET_CODE (ind) == POST_INC)
9462       || (type == 0 && GET_CODE (ind) == PRE_DEC))
9463     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
9464
9465   /* FIXME: vld1 allows register post-modify.  */
9466
9467   /* Match:
9468      (plus (reg)
9469           (const)).  */
9470   if (type == 0
9471       && GET_CODE (ind) == PLUS
9472       && GET_CODE (XEXP (ind, 0)) == REG
9473       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
9474       && GET_CODE (XEXP (ind, 1)) == CONST_INT
9475       && INTVAL (XEXP (ind, 1)) > -1024
9476       && INTVAL (XEXP (ind, 1)) < 1016
9477       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
9478     return TRUE;
9479
9480   return FALSE;
9481 }
9482
9483 /* Return TRUE if OP is a mem suitable for loading/storing a Neon struct
9484    type.  */
9485 int
9486 neon_struct_mem_operand (rtx op)
9487 {
9488   rtx ind;
9489
9490   /* Reject eliminable registers.  */
9491   if (! (reload_in_progress || reload_completed)
9492       && (   reg_mentioned_p (frame_pointer_rtx, op)
9493           || reg_mentioned_p (arg_pointer_rtx, op)
9494           || reg_mentioned_p (virtual_incoming_args_rtx, op)
9495           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
9496           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
9497           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
9498     return FALSE;
9499
9500   /* Constants are converted into offsets from labels.  */
9501   if (GET_CODE (op) != MEM)
9502     return FALSE;
9503
9504   ind = XEXP (op, 0);
9505
9506   if (reload_completed
9507       && (GET_CODE (ind) == LABEL_REF
9508           || (GET_CODE (ind) == CONST
9509               && GET_CODE (XEXP (ind, 0)) == PLUS
9510               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
9511               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
9512     return TRUE;
9513
9514   /* Match: (mem (reg)).  */
9515   if (GET_CODE (ind) == REG)
9516     return arm_address_register_rtx_p (ind, 0);
9517
9518   /* vldm/vstm allows POST_INC (ia) and PRE_DEC (db).  */
9519   if (GET_CODE (ind) == POST_INC
9520       || GET_CODE (ind) == PRE_DEC)
9521     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
9522
9523   return FALSE;
9524 }
9525
9526 /* Return true if X is a register that will be eliminated later on.  */
9527 int
9528 arm_eliminable_register (rtx x)
9529 {
9530   return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
9531                        || REGNO (x) == ARG_POINTER_REGNUM
9532                        || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
9533                            && REGNO (x) <= LAST_VIRTUAL_REGISTER));
9534 }
9535
9536 /* Return GENERAL_REGS if a scratch register required to reload x to/from
9537    coprocessor registers.  Otherwise return NO_REGS.  */
9538
9539 enum reg_class
9540 coproc_secondary_reload_class (enum machine_mode mode, rtx x, bool wb)
9541 {
9542   if (mode == HFmode)
9543     {
9544       if (!TARGET_NEON_FP16)
9545         return GENERAL_REGS;
9546       if (s_register_operand (x, mode) || neon_vector_mem_operand (x, 2))
9547         return NO_REGS;
9548       return GENERAL_REGS;
9549     }
9550
9551   /* The neon move patterns handle all legitimate vector and struct
9552      addresses.  */
9553   if (TARGET_NEON
9554       && (MEM_P (x) || GET_CODE (x) == CONST_VECTOR)
9555       && (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9556           || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9557           || VALID_NEON_STRUCT_MODE (mode)))
9558     return NO_REGS;
9559
9560   if (arm_coproc_mem_operand (x, wb) || s_register_operand (x, mode))
9561     return NO_REGS;
9562
9563   return GENERAL_REGS;
9564 }
9565
9566 /* Values which must be returned in the most-significant end of the return
9567    register.  */
9568
9569 static bool
9570 arm_return_in_msb (const_tree valtype)
9571 {
9572   return (TARGET_AAPCS_BASED
9573           && BYTES_BIG_ENDIAN
9574           && (AGGREGATE_TYPE_P (valtype)
9575               || TREE_CODE (valtype) == COMPLEX_TYPE
9576               || FIXED_POINT_TYPE_P (valtype)));
9577 }
9578
9579 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
9580    Use by the Cirrus Maverick code which has to workaround
9581    a hardware bug triggered by such instructions.  */
9582 static bool
9583 arm_memory_load_p (rtx insn)
9584 {
9585   rtx body, lhs, rhs;;
9586
9587   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
9588     return false;
9589
9590   body = PATTERN (insn);
9591
9592   if (GET_CODE (body) != SET)
9593     return false;
9594
9595   lhs = XEXP (body, 0);
9596   rhs = XEXP (body, 1);
9597
9598   lhs = REG_OR_SUBREG_RTX (lhs);
9599
9600   /* If the destination is not a general purpose
9601      register we do not have to worry.  */
9602   if (GET_CODE (lhs) != REG
9603       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
9604     return false;
9605
9606   /* As well as loads from memory we also have to react
9607      to loads of invalid constants which will be turned
9608      into loads from the minipool.  */
9609   return (GET_CODE (rhs) == MEM
9610           || GET_CODE (rhs) == SYMBOL_REF
9611           || note_invalid_constants (insn, -1, false));
9612 }
9613
9614 /* Return TRUE if INSN is a Cirrus instruction.  */
9615 static bool
9616 arm_cirrus_insn_p (rtx insn)
9617 {
9618   enum attr_cirrus attr;
9619
9620   /* get_attr cannot accept USE or CLOBBER.  */
9621   if (!insn
9622       || GET_CODE (insn) != INSN
9623       || GET_CODE (PATTERN (insn)) == USE
9624       || GET_CODE (PATTERN (insn)) == CLOBBER)
9625     return 0;
9626
9627   attr = get_attr_cirrus (insn);
9628
9629   return attr != CIRRUS_NOT;
9630 }
9631
9632 /* Cirrus reorg for invalid instruction combinations.  */
9633 static void
9634 cirrus_reorg (rtx first)
9635 {
9636   enum attr_cirrus attr;
9637   rtx body = PATTERN (first);
9638   rtx t;
9639   int nops;
9640
9641   /* Any branch must be followed by 2 non Cirrus instructions.  */
9642   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
9643     {
9644       nops = 0;
9645       t = next_nonnote_insn (first);
9646
9647       if (arm_cirrus_insn_p (t))
9648         ++ nops;
9649
9650       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
9651         ++ nops;
9652
9653       while (nops --)
9654         emit_insn_after (gen_nop (), first);
9655
9656       return;
9657     }
9658
9659   /* (float (blah)) is in parallel with a clobber.  */
9660   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
9661     body = XVECEXP (body, 0, 0);
9662
9663   if (GET_CODE (body) == SET)
9664     {
9665       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
9666
9667       /* cfldrd, cfldr64, cfstrd, cfstr64 must
9668          be followed by a non Cirrus insn.  */
9669       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
9670         {
9671           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
9672             emit_insn_after (gen_nop (), first);
9673
9674           return;
9675         }
9676       else if (arm_memory_load_p (first))
9677         {
9678           unsigned int arm_regno;
9679
9680           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
9681              ldr/cfmv64hr combination where the Rd field is the same
9682              in both instructions must be split with a non Cirrus
9683              insn.  Example:
9684
9685              ldr r0, blah
9686              nop
9687              cfmvsr mvf0, r0.  */
9688
9689           /* Get Arm register number for ldr insn.  */
9690           if (GET_CODE (lhs) == REG)
9691             arm_regno = REGNO (lhs);
9692           else
9693             {
9694               gcc_assert (GET_CODE (rhs) == REG);
9695               arm_regno = REGNO (rhs);
9696             }
9697
9698           /* Next insn.  */
9699           first = next_nonnote_insn (first);
9700
9701           if (! arm_cirrus_insn_p (first))
9702             return;
9703
9704           body = PATTERN (first);
9705
9706           /* (float (blah)) is in parallel with a clobber.  */
9707           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
9708             body = XVECEXP (body, 0, 0);
9709
9710           if (GET_CODE (body) == FLOAT)
9711             body = XEXP (body, 0);
9712
9713           if (get_attr_cirrus (first) == CIRRUS_MOVE
9714               && GET_CODE (XEXP (body, 1)) == REG
9715               && arm_regno == REGNO (XEXP (body, 1)))
9716             emit_insn_after (gen_nop (), first);
9717
9718           return;
9719         }
9720     }
9721
9722   /* get_attr cannot accept USE or CLOBBER.  */
9723   if (!first
9724       || GET_CODE (first) != INSN
9725       || GET_CODE (PATTERN (first)) == USE
9726       || GET_CODE (PATTERN (first)) == CLOBBER)
9727     return;
9728
9729   attr = get_attr_cirrus (first);
9730
9731   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
9732      must be followed by a non-coprocessor instruction.  */
9733   if (attr == CIRRUS_COMPARE)
9734     {
9735       nops = 0;
9736
9737       t = next_nonnote_insn (first);
9738
9739       if (arm_cirrus_insn_p (t))
9740         ++ nops;
9741
9742       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
9743         ++ nops;
9744
9745       while (nops --)
9746         emit_insn_after (gen_nop (), first);
9747
9748       return;
9749     }
9750 }
9751
9752 /* Return TRUE if X references a SYMBOL_REF.  */
9753 int
9754 symbol_mentioned_p (rtx x)
9755 {
9756   const char * fmt;
9757   int i;
9758
9759   if (GET_CODE (x) == SYMBOL_REF)
9760     return 1;
9761
9762   /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
9763      are constant offsets, not symbols.  */
9764   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
9765     return 0;
9766
9767   fmt = GET_RTX_FORMAT (GET_CODE (x));
9768
9769   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
9770     {
9771       if (fmt[i] == 'E')
9772         {
9773           int j;
9774
9775           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9776             if (symbol_mentioned_p (XVECEXP (x, i, j)))
9777               return 1;
9778         }
9779       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
9780         return 1;
9781     }
9782
9783   return 0;
9784 }
9785
9786 /* Return TRUE if X references a LABEL_REF.  */
9787 int
9788 label_mentioned_p (rtx x)
9789 {
9790   const char * fmt;
9791   int i;
9792
9793   if (GET_CODE (x) == LABEL_REF)
9794     return 1;
9795
9796   /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
9797      instruction, but they are constant offsets, not symbols.  */
9798   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
9799     return 0;
9800
9801   fmt = GET_RTX_FORMAT (GET_CODE (x));
9802   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
9803     {
9804       if (fmt[i] == 'E')
9805         {
9806           int j;
9807
9808           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9809             if (label_mentioned_p (XVECEXP (x, i, j)))
9810               return 1;
9811         }
9812       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
9813         return 1;
9814     }
9815
9816   return 0;
9817 }
9818
9819 int
9820 tls_mentioned_p (rtx x)
9821 {
9822   switch (GET_CODE (x))
9823     {
9824     case CONST:
9825       return tls_mentioned_p (XEXP (x, 0));
9826
9827     case UNSPEC:
9828       if (XINT (x, 1) == UNSPEC_TLS)
9829         return 1;
9830
9831     default:
9832       return 0;
9833     }
9834 }
9835
9836 /* Must not copy any rtx that uses a pc-relative address.  */
9837
9838 static int
9839 arm_note_pic_base (rtx *x, void *date ATTRIBUTE_UNUSED)
9840 {
9841   if (GET_CODE (*x) == UNSPEC
9842       && XINT (*x, 1) == UNSPEC_PIC_BASE)
9843     return 1;
9844   return 0;
9845 }
9846
9847 static bool
9848 arm_cannot_copy_insn_p (rtx insn)
9849 {
9850   /* The tls call insn cannot be copied, as it is paired with a data
9851      word.  */
9852   if (recog_memoized (insn) == CODE_FOR_tlscall)
9853     return true;
9854   
9855   return for_each_rtx (&PATTERN (insn), arm_note_pic_base, NULL);
9856 }
9857
9858 enum rtx_code
9859 minmax_code (rtx x)
9860 {
9861   enum rtx_code code = GET_CODE (x);
9862
9863   switch (code)
9864     {
9865     case SMAX:
9866       return GE;
9867     case SMIN:
9868       return LE;
9869     case UMIN:
9870       return LEU;
9871     case UMAX:
9872       return GEU;
9873     default:
9874       gcc_unreachable ();
9875     }
9876 }
9877
9878 /* Return 1 if memory locations are adjacent.  */
9879 int
9880 adjacent_mem_locations (rtx a, rtx b)
9881 {
9882   /* We don't guarantee to preserve the order of these memory refs.  */
9883   if (volatile_refs_p (a) || volatile_refs_p (b))
9884     return 0;
9885
9886   if ((GET_CODE (XEXP (a, 0)) == REG
9887        || (GET_CODE (XEXP (a, 0)) == PLUS
9888            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
9889       && (GET_CODE (XEXP (b, 0)) == REG
9890           || (GET_CODE (XEXP (b, 0)) == PLUS
9891               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
9892     {
9893       HOST_WIDE_INT val0 = 0, val1 = 0;
9894       rtx reg0, reg1;
9895       int val_diff;
9896
9897       if (GET_CODE (XEXP (a, 0)) == PLUS)
9898         {
9899           reg0 = XEXP (XEXP (a, 0), 0);
9900           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
9901         }
9902       else
9903         reg0 = XEXP (a, 0);
9904
9905       if (GET_CODE (XEXP (b, 0)) == PLUS)
9906         {
9907           reg1 = XEXP (XEXP (b, 0), 0);
9908           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
9909         }
9910       else
9911         reg1 = XEXP (b, 0);
9912
9913       /* Don't accept any offset that will require multiple
9914          instructions to handle, since this would cause the
9915          arith_adjacentmem pattern to output an overlong sequence.  */
9916       if (!const_ok_for_op (val0, PLUS) || !const_ok_for_op (val1, PLUS))
9917         return 0;
9918
9919       /* Don't allow an eliminable register: register elimination can make
9920          the offset too large.  */
9921       if (arm_eliminable_register (reg0))
9922         return 0;
9923
9924       val_diff = val1 - val0;
9925
9926       if (arm_ld_sched)
9927         {
9928           /* If the target has load delay slots, then there's no benefit
9929              to using an ldm instruction unless the offset is zero and
9930              we are optimizing for size.  */
9931           return (optimize_size && (REGNO (reg0) == REGNO (reg1))
9932                   && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
9933                   && (val_diff == 4 || val_diff == -4));
9934         }
9935
9936       return ((REGNO (reg0) == REGNO (reg1))
9937               && (val_diff == 4 || val_diff == -4));
9938     }
9939
9940   return 0;
9941 }
9942
9943 /* Return true iff it would be profitable to turn a sequence of NOPS loads
9944    or stores (depending on IS_STORE) into a load-multiple or store-multiple
9945    instruction.  ADD_OFFSET is nonzero if the base address register needs
9946    to be modified with an add instruction before we can use it.  */
9947
9948 static bool
9949 multiple_operation_profitable_p (bool is_store ATTRIBUTE_UNUSED,
9950                                  int nops, HOST_WIDE_INT add_offset)
9951  {
9952   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
9953      if the offset isn't small enough.  The reason 2 ldrs are faster
9954      is because these ARMs are able to do more than one cache access
9955      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
9956      whilst the ARM8 has a double bandwidth cache.  This means that
9957      these cores can do both an instruction fetch and a data fetch in
9958      a single cycle, so the trick of calculating the address into a
9959      scratch register (one of the result regs) and then doing a load
9960      multiple actually becomes slower (and no smaller in code size).
9961      That is the transformation
9962
9963         ldr     rd1, [rbase + offset]
9964         ldr     rd2, [rbase + offset + 4]
9965
9966      to
9967
9968         add     rd1, rbase, offset
9969         ldmia   rd1, {rd1, rd2}
9970
9971      produces worse code -- '3 cycles + any stalls on rd2' instead of
9972      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
9973      access per cycle, the first sequence could never complete in less
9974      than 6 cycles, whereas the ldm sequence would only take 5 and
9975      would make better use of sequential accesses if not hitting the
9976      cache.
9977
9978      We cheat here and test 'arm_ld_sched' which we currently know to
9979      only be true for the ARM8, ARM9 and StrongARM.  If this ever
9980      changes, then the test below needs to be reworked.  */
9981   if (nops == 2 && arm_ld_sched && add_offset != 0)
9982     return false;
9983
9984   /* XScale has load-store double instructions, but they have stricter
9985      alignment requirements than load-store multiple, so we cannot
9986      use them.
9987
9988      For XScale ldm requires 2 + NREGS cycles to complete and blocks
9989      the pipeline until completion.
9990
9991         NREGS           CYCLES
9992           1               3
9993           2               4
9994           3               5
9995           4               6
9996
9997      An ldr instruction takes 1-3 cycles, but does not block the
9998      pipeline.
9999
10000         NREGS           CYCLES
10001           1              1-3
10002           2              2-6
10003           3              3-9
10004           4              4-12
10005
10006      Best case ldr will always win.  However, the more ldr instructions
10007      we issue, the less likely we are to be able to schedule them well.
10008      Using ldr instructions also increases code size.
10009
10010      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
10011      for counts of 3 or 4 regs.  */
10012   if (nops <= 2 && arm_tune_xscale && !optimize_size)
10013     return false;
10014   return true;
10015 }
10016
10017 /* Subroutine of load_multiple_sequence and store_multiple_sequence.
10018    Given an array of UNSORTED_OFFSETS, of which there are NOPS, compute
10019    an array ORDER which describes the sequence to use when accessing the
10020    offsets that produces an ascending order.  In this sequence, each
10021    offset must be larger by exactly 4 than the previous one.  ORDER[0]
10022    must have been filled in with the lowest offset by the caller.
10023    If UNSORTED_REGS is nonnull, it is an array of register numbers that
10024    we use to verify that ORDER produces an ascending order of registers.
10025    Return true if it was possible to construct such an order, false if
10026    not.  */
10027
10028 static bool
10029 compute_offset_order (int nops, HOST_WIDE_INT *unsorted_offsets, int *order,
10030                       int *unsorted_regs)
10031 {
10032   int i;
10033   for (i = 1; i < nops; i++)
10034     {
10035       int j;
10036
10037       order[i] = order[i - 1];
10038       for (j = 0; j < nops; j++)
10039         if (unsorted_offsets[j] == unsorted_offsets[order[i - 1]] + 4)
10040           {
10041             /* We must find exactly one offset that is higher than the
10042                previous one by 4.  */
10043             if (order[i] != order[i - 1])
10044               return false;
10045             order[i] = j;
10046           }
10047       if (order[i] == order[i - 1])
10048         return false;
10049       /* The register numbers must be ascending.  */
10050       if (unsorted_regs != NULL
10051           && unsorted_regs[order[i]] <= unsorted_regs[order[i - 1]])
10052         return false;
10053     }
10054   return true;
10055 }
10056
10057 /* Used to determine in a peephole whether a sequence of load
10058    instructions can be changed into a load-multiple instruction.
10059    NOPS is the number of separate load instructions we are examining.  The
10060    first NOPS entries in OPERANDS are the destination registers, the
10061    next NOPS entries are memory operands.  If this function is
10062    successful, *BASE is set to the common base register of the memory
10063    accesses; *LOAD_OFFSET is set to the first memory location's offset
10064    from that base register.
10065    REGS is an array filled in with the destination register numbers.
10066    SAVED_ORDER (if nonnull), is an array filled in with an order that maps
10067    insn numbers to an ascending order of stores.  If CHECK_REGS is true,
10068    the sequence of registers in REGS matches the loads from ascending memory
10069    locations, and the function verifies that the register numbers are
10070    themselves ascending.  If CHECK_REGS is false, the register numbers
10071    are stored in the order they are found in the operands.  */
10072 static int
10073 load_multiple_sequence (rtx *operands, int nops, int *regs, int *saved_order,
10074                         int *base, HOST_WIDE_INT *load_offset, bool check_regs)
10075 {
10076   int unsorted_regs[MAX_LDM_STM_OPS];
10077   HOST_WIDE_INT unsorted_offsets[MAX_LDM_STM_OPS];
10078   int order[MAX_LDM_STM_OPS];
10079   rtx base_reg_rtx = NULL;
10080   int base_reg = -1;
10081   int i, ldm_case;
10082
10083   /* Can only handle up to MAX_LDM_STM_OPS insns at present, though could be
10084      easily extended if required.  */
10085   gcc_assert (nops >= 2 && nops <= MAX_LDM_STM_OPS);
10086
10087   memset (order, 0, MAX_LDM_STM_OPS * sizeof (int));
10088
10089   /* Loop over the operands and check that the memory references are
10090      suitable (i.e. immediate offsets from the same base register).  At
10091      the same time, extract the target register, and the memory
10092      offsets.  */
10093   for (i = 0; i < nops; i++)
10094     {
10095       rtx reg;
10096       rtx offset;
10097
10098       /* Convert a subreg of a mem into the mem itself.  */
10099       if (GET_CODE (operands[nops + i]) == SUBREG)
10100         operands[nops + i] = alter_subreg (operands + (nops + i));
10101
10102       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
10103
10104       /* Don't reorder volatile memory references; it doesn't seem worth
10105          looking for the case where the order is ok anyway.  */
10106       if (MEM_VOLATILE_P (operands[nops + i]))
10107         return 0;
10108
10109       offset = const0_rtx;
10110
10111       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
10112            || (GET_CODE (reg) == SUBREG
10113                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
10114           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
10115               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
10116                    == REG)
10117                   || (GET_CODE (reg) == SUBREG
10118                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
10119               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
10120                   == CONST_INT)))
10121         {
10122           if (i == 0)
10123             {
10124               base_reg = REGNO (reg);
10125               base_reg_rtx = reg;
10126               if (TARGET_THUMB1 && base_reg > LAST_LO_REGNUM)
10127                 return 0;
10128             }
10129           else if (base_reg != (int) REGNO (reg))
10130             /* Not addressed from the same base register.  */
10131             return 0;
10132
10133           unsorted_regs[i] = (GET_CODE (operands[i]) == REG
10134                               ? REGNO (operands[i])
10135                               : REGNO (SUBREG_REG (operands[i])));
10136
10137           /* If it isn't an integer register, or if it overwrites the
10138              base register but isn't the last insn in the list, then
10139              we can't do this.  */
10140           if (unsorted_regs[i] < 0
10141               || (TARGET_THUMB1 && unsorted_regs[i] > LAST_LO_REGNUM)
10142               || unsorted_regs[i] > 14
10143               || (i != nops - 1 && unsorted_regs[i] == base_reg))
10144             return 0;
10145
10146           unsorted_offsets[i] = INTVAL (offset);
10147           if (i == 0 || unsorted_offsets[i] < unsorted_offsets[order[0]])
10148             order[0] = i;
10149         }
10150       else
10151         /* Not a suitable memory address.  */
10152         return 0;
10153     }
10154
10155   /* All the useful information has now been extracted from the
10156      operands into unsorted_regs and unsorted_offsets; additionally,
10157      order[0] has been set to the lowest offset in the list.  Sort
10158      the offsets into order, verifying that they are adjacent, and
10159      check that the register numbers are ascending.  */
10160   if (!compute_offset_order (nops, unsorted_offsets, order,
10161                              check_regs ? unsorted_regs : NULL))
10162     return 0;
10163
10164   if (saved_order)
10165     memcpy (saved_order, order, sizeof order);
10166
10167   if (base)
10168     {
10169       *base = base_reg;
10170
10171       for (i = 0; i < nops; i++)
10172         regs[i] = unsorted_regs[check_regs ? order[i] : i];
10173
10174       *load_offset = unsorted_offsets[order[0]];
10175     }
10176
10177   if (TARGET_THUMB1
10178       && !peep2_reg_dead_p (nops, base_reg_rtx))
10179     return 0;
10180
10181   if (unsorted_offsets[order[0]] == 0)
10182     ldm_case = 1; /* ldmia */
10183   else if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
10184     ldm_case = 2; /* ldmib */
10185   else if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
10186     ldm_case = 3; /* ldmda */
10187   else if (TARGET_32BIT && unsorted_offsets[order[nops - 1]] == -4)
10188     ldm_case = 4; /* ldmdb */
10189   else if (const_ok_for_arm (unsorted_offsets[order[0]])
10190            || const_ok_for_arm (-unsorted_offsets[order[0]]))
10191     ldm_case = 5;
10192   else
10193     return 0;
10194
10195   if (!multiple_operation_profitable_p (false, nops,
10196                                         ldm_case == 5
10197                                         ? unsorted_offsets[order[0]] : 0))
10198     return 0;
10199
10200   return ldm_case;
10201 }
10202
10203 /* Used to determine in a peephole whether a sequence of store instructions can
10204    be changed into a store-multiple instruction.
10205    NOPS is the number of separate store instructions we are examining.
10206    NOPS_TOTAL is the total number of instructions recognized by the peephole
10207    pattern.
10208    The first NOPS entries in OPERANDS are the source registers, the next
10209    NOPS entries are memory operands.  If this function is successful, *BASE is
10210    set to the common base register of the memory accesses; *LOAD_OFFSET is set
10211    to the first memory location's offset from that base register.  REGS is an
10212    array filled in with the source register numbers, REG_RTXS (if nonnull) is
10213    likewise filled with the corresponding rtx's.
10214    SAVED_ORDER (if nonnull), is an array filled in with an order that maps insn
10215    numbers to an ascending order of stores.
10216    If CHECK_REGS is true, the sequence of registers in *REGS matches the stores
10217    from ascending memory locations, and the function verifies that the register
10218    numbers are themselves ascending.  If CHECK_REGS is false, the register
10219    numbers are stored in the order they are found in the operands.  */
10220 static int
10221 store_multiple_sequence (rtx *operands, int nops, int nops_total,
10222                          int *regs, rtx *reg_rtxs, int *saved_order, int *base,
10223                          HOST_WIDE_INT *load_offset, bool check_regs)
10224 {
10225   int unsorted_regs[MAX_LDM_STM_OPS];
10226   rtx unsorted_reg_rtxs[MAX_LDM_STM_OPS];
10227   HOST_WIDE_INT unsorted_offsets[MAX_LDM_STM_OPS];
10228   int order[MAX_LDM_STM_OPS];
10229   int base_reg = -1;
10230   rtx base_reg_rtx = NULL;
10231   int i, stm_case;
10232
10233   /* Can only handle up to MAX_LDM_STM_OPS insns at present, though could be
10234      easily extended if required.  */
10235   gcc_assert (nops >= 2 && nops <= MAX_LDM_STM_OPS);
10236
10237   memset (order, 0, MAX_LDM_STM_OPS * sizeof (int));
10238
10239   /* Loop over the operands and check that the memory references are
10240      suitable (i.e. immediate offsets from the same base register).  At
10241      the same time, extract the target register, and the memory
10242      offsets.  */
10243   for (i = 0; i < nops; i++)
10244     {
10245       rtx reg;
10246       rtx offset;
10247
10248       /* Convert a subreg of a mem into the mem itself.  */
10249       if (GET_CODE (operands[nops + i]) == SUBREG)
10250         operands[nops + i] = alter_subreg (operands + (nops + i));
10251
10252       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
10253
10254       /* Don't reorder volatile memory references; it doesn't seem worth
10255          looking for the case where the order is ok anyway.  */
10256       if (MEM_VOLATILE_P (operands[nops + i]))
10257         return 0;
10258
10259       offset = const0_rtx;
10260
10261       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
10262            || (GET_CODE (reg) == SUBREG
10263                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
10264           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
10265               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
10266                    == REG)
10267                   || (GET_CODE (reg) == SUBREG
10268                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
10269               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
10270                   == CONST_INT)))
10271         {
10272           unsorted_reg_rtxs[i] = (GET_CODE (operands[i]) == REG
10273                                   ? operands[i] : SUBREG_REG (operands[i]));
10274           unsorted_regs[i] = REGNO (unsorted_reg_rtxs[i]);
10275
10276           if (i == 0)
10277             {
10278               base_reg = REGNO (reg);
10279               base_reg_rtx = reg;
10280               if (TARGET_THUMB1 && base_reg > LAST_LO_REGNUM)
10281                 return 0;
10282             }
10283           else if (base_reg != (int) REGNO (reg))
10284             /* Not addressed from the same base register.  */
10285             return 0;
10286
10287           /* If it isn't an integer register, then we can't do this.  */
10288           if (unsorted_regs[i] < 0
10289               || (TARGET_THUMB1 && unsorted_regs[i] > LAST_LO_REGNUM)
10290               || (TARGET_THUMB2 && unsorted_regs[i] == base_reg)
10291               || (TARGET_THUMB2 && unsorted_regs[i] == SP_REGNUM)
10292               || unsorted_regs[i] > 14)
10293             return 0;
10294
10295           unsorted_offsets[i] = INTVAL (offset);
10296           if (i == 0 || unsorted_offsets[i] < unsorted_offsets[order[0]])
10297             order[0] = i;
10298         }
10299       else
10300         /* Not a suitable memory address.  */
10301         return 0;
10302     }
10303
10304   /* All the useful information has now been extracted from the
10305      operands into unsorted_regs and unsorted_offsets; additionally,
10306      order[0] has been set to the lowest offset in the list.  Sort
10307      the offsets into order, verifying that they are adjacent, and
10308      check that the register numbers are ascending.  */
10309   if (!compute_offset_order (nops, unsorted_offsets, order,
10310                              check_regs ? unsorted_regs : NULL))
10311     return 0;
10312
10313   if (saved_order)
10314     memcpy (saved_order, order, sizeof order);
10315
10316   if (base)
10317     {
10318       *base = base_reg;
10319
10320       for (i = 0; i < nops; i++)
10321         {
10322           regs[i] = unsorted_regs[check_regs ? order[i] : i];
10323           if (reg_rtxs)
10324             reg_rtxs[i] = unsorted_reg_rtxs[check_regs ? order[i] : i];
10325         }
10326
10327       *load_offset = unsorted_offsets[order[0]];
10328     }
10329
10330   if (TARGET_THUMB1
10331       && !peep2_reg_dead_p (nops_total, base_reg_rtx))
10332     return 0;
10333
10334   if (unsorted_offsets[order[0]] == 0)
10335     stm_case = 1; /* stmia */
10336   else if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
10337     stm_case = 2; /* stmib */
10338   else if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
10339     stm_case = 3; /* stmda */
10340   else if (TARGET_32BIT && unsorted_offsets[order[nops - 1]] == -4)
10341     stm_case = 4; /* stmdb */
10342   else
10343     return 0;
10344
10345   if (!multiple_operation_profitable_p (false, nops, 0))
10346     return 0;
10347
10348   return stm_case;
10349 }
10350 \f
10351 /* Routines for use in generating RTL.  */
10352
10353 /* Generate a load-multiple instruction.  COUNT is the number of loads in
10354    the instruction; REGS and MEMS are arrays containing the operands.
10355    BASEREG is the base register to be used in addressing the memory operands.
10356    WBACK_OFFSET is nonzero if the instruction should update the base
10357    register.  */
10358
10359 static rtx
10360 arm_gen_load_multiple_1 (int count, int *regs, rtx *mems, rtx basereg,
10361                          HOST_WIDE_INT wback_offset)
10362 {
10363   int i = 0, j;
10364   rtx result;
10365
10366   if (!multiple_operation_profitable_p (false, count, 0))
10367     {
10368       rtx seq;
10369
10370       start_sequence ();
10371
10372       for (i = 0; i < count; i++)
10373         emit_move_insn (gen_rtx_REG (SImode, regs[i]), mems[i]);
10374
10375       if (wback_offset != 0)
10376         emit_move_insn (basereg, plus_constant (basereg, wback_offset));
10377
10378       seq = get_insns ();
10379       end_sequence ();
10380
10381       return seq;
10382     }
10383
10384   result = gen_rtx_PARALLEL (VOIDmode,
10385                              rtvec_alloc (count + (wback_offset != 0 ? 1 : 0)));
10386   if (wback_offset != 0)
10387     {
10388       XVECEXP (result, 0, 0)
10389         = gen_rtx_SET (VOIDmode, basereg,
10390                        plus_constant (basereg, wback_offset));
10391       i = 1;
10392       count++;
10393     }
10394
10395   for (j = 0; i < count; i++, j++)
10396     XVECEXP (result, 0, i)
10397       = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, regs[j]), mems[j]);
10398
10399   return result;
10400 }
10401
10402 /* Generate a store-multiple instruction.  COUNT is the number of stores in
10403    the instruction; REGS and MEMS are arrays containing the operands.
10404    BASEREG is the base register to be used in addressing the memory operands.
10405    WBACK_OFFSET is nonzero if the instruction should update the base
10406    register.  */
10407
10408 static rtx
10409 arm_gen_store_multiple_1 (int count, int *regs, rtx *mems, rtx basereg,
10410                           HOST_WIDE_INT wback_offset)
10411 {
10412   int i = 0, j;
10413   rtx result;
10414
10415   if (GET_CODE (basereg) == PLUS)
10416     basereg = XEXP (basereg, 0);
10417
10418   if (!multiple_operation_profitable_p (false, count, 0))
10419     {
10420       rtx seq;
10421
10422       start_sequence ();
10423
10424       for (i = 0; i < count; i++)
10425         emit_move_insn (mems[i], gen_rtx_REG (SImode, regs[i]));
10426
10427       if (wback_offset != 0)
10428         emit_move_insn (basereg, plus_constant (basereg, wback_offset));
10429
10430       seq = get_insns ();
10431       end_sequence ();
10432
10433       return seq;
10434     }
10435
10436   result = gen_rtx_PARALLEL (VOIDmode,
10437                              rtvec_alloc (count + (wback_offset != 0 ? 1 : 0)));
10438   if (wback_offset != 0)
10439     {
10440       XVECEXP (result, 0, 0)
10441         = gen_rtx_SET (VOIDmode, basereg,
10442                        plus_constant (basereg, wback_offset));
10443       i = 1;
10444       count++;
10445     }
10446
10447   for (j = 0; i < count; i++, j++)
10448     XVECEXP (result, 0, i)
10449       = gen_rtx_SET (VOIDmode, mems[j], gen_rtx_REG (SImode, regs[j]));
10450
10451   return result;
10452 }
10453
10454 /* Generate either a load-multiple or a store-multiple instruction.  This
10455    function can be used in situations where we can start with a single MEM
10456    rtx and adjust its address upwards.
10457    COUNT is the number of operations in the instruction, not counting a
10458    possible update of the base register.  REGS is an array containing the
10459    register operands.
10460    BASEREG is the base register to be used in addressing the memory operands,
10461    which are constructed from BASEMEM.
10462    WRITE_BACK specifies whether the generated instruction should include an
10463    update of the base register.
10464    OFFSETP is used to pass an offset to and from this function; this offset
10465    is not used when constructing the address (instead BASEMEM should have an
10466    appropriate offset in its address), it is used only for setting
10467    MEM_OFFSET.  It is updated only if WRITE_BACK is true.*/
10468
10469 static rtx
10470 arm_gen_multiple_op (bool is_load, int *regs, int count, rtx basereg,
10471                      bool write_back, rtx basemem, HOST_WIDE_INT *offsetp)
10472 {
10473   rtx mems[MAX_LDM_STM_OPS];
10474   HOST_WIDE_INT offset = *offsetp;
10475   int i;
10476
10477   gcc_assert (count <= MAX_LDM_STM_OPS);
10478
10479   if (GET_CODE (basereg) == PLUS)
10480     basereg = XEXP (basereg, 0);
10481
10482   for (i = 0; i < count; i++)
10483     {
10484       rtx addr = plus_constant (basereg, i * 4);
10485       mems[i] = adjust_automodify_address_nv (basemem, SImode, addr, offset);
10486       offset += 4;
10487     }
10488
10489   if (write_back)
10490     *offsetp = offset;
10491
10492   if (is_load)
10493     return arm_gen_load_multiple_1 (count, regs, mems, basereg,
10494                                     write_back ? 4 * count : 0);
10495   else
10496     return arm_gen_store_multiple_1 (count, regs, mems, basereg,
10497                                      write_back ? 4 * count : 0);
10498 }
10499
10500 rtx
10501 arm_gen_load_multiple (int *regs, int count, rtx basereg, int write_back,
10502                        rtx basemem, HOST_WIDE_INT *offsetp)
10503 {
10504   return arm_gen_multiple_op (TRUE, regs, count, basereg, write_back, basemem,
10505                               offsetp);
10506 }
10507
10508 rtx
10509 arm_gen_store_multiple (int *regs, int count, rtx basereg, int write_back,
10510                         rtx basemem, HOST_WIDE_INT *offsetp)
10511 {
10512   return arm_gen_multiple_op (FALSE, regs, count, basereg, write_back, basemem,
10513                               offsetp);
10514 }
10515
10516 /* Called from a peephole2 expander to turn a sequence of loads into an
10517    LDM instruction.  OPERANDS are the operands found by the peephole matcher;
10518    NOPS indicates how many separate loads we are trying to combine.  SORT_REGS
10519    is true if we can reorder the registers because they are used commutatively
10520    subsequently.
10521    Returns true iff we could generate a new instruction.  */
10522
10523 bool
10524 gen_ldm_seq (rtx *operands, int nops, bool sort_regs)
10525 {
10526   int regs[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
10527   rtx mems[MAX_LDM_STM_OPS];
10528   int i, j, base_reg;
10529   rtx base_reg_rtx;
10530   HOST_WIDE_INT offset;
10531   int write_back = FALSE;
10532   int ldm_case;
10533   rtx addr;
10534
10535   ldm_case = load_multiple_sequence (operands, nops, regs, mem_order,
10536                                      &base_reg, &offset, !sort_regs);
10537
10538   if (ldm_case == 0)
10539     return false;
10540
10541   if (sort_regs)
10542     for (i = 0; i < nops - 1; i++)
10543       for (j = i + 1; j < nops; j++)
10544         if (regs[i] > regs[j])
10545           {
10546             int t = regs[i];
10547             regs[i] = regs[j];
10548             regs[j] = t;
10549           }
10550   base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
10551
10552   if (TARGET_THUMB1)
10553     {
10554       gcc_assert (peep2_reg_dead_p (nops, base_reg_rtx));
10555       gcc_assert (ldm_case == 1 || ldm_case == 5);
10556       write_back = TRUE;
10557     }
10558
10559   if (ldm_case == 5)
10560     {
10561       rtx newbase = TARGET_THUMB1 ? base_reg_rtx : gen_rtx_REG (SImode, regs[0]);
10562       emit_insn (gen_addsi3 (newbase, base_reg_rtx, GEN_INT (offset)));
10563       offset = 0;
10564       if (!TARGET_THUMB1)
10565         {
10566           base_reg = regs[0];
10567           base_reg_rtx = newbase;
10568         }
10569     }
10570
10571   for (i = 0; i < nops; i++)
10572     {
10573       addr = plus_constant (base_reg_rtx, offset + i * 4);
10574       mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
10575                                               SImode, addr, 0);
10576     }
10577   emit_insn (arm_gen_load_multiple_1 (nops, regs, mems, base_reg_rtx,
10578                                       write_back ? offset + i * 4 : 0));
10579   return true;
10580 }
10581
10582 /* Called from a peephole2 expander to turn a sequence of stores into an
10583    STM instruction.  OPERANDS are the operands found by the peephole matcher;
10584    NOPS indicates how many separate stores we are trying to combine.
10585    Returns true iff we could generate a new instruction.  */
10586
10587 bool
10588 gen_stm_seq (rtx *operands, int nops)
10589 {
10590   int i;
10591   int regs[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
10592   rtx mems[MAX_LDM_STM_OPS];
10593   int base_reg;
10594   rtx base_reg_rtx;
10595   HOST_WIDE_INT offset;
10596   int write_back = FALSE;
10597   int stm_case;
10598   rtx addr;
10599   bool base_reg_dies;
10600
10601   stm_case = store_multiple_sequence (operands, nops, nops, regs, NULL,
10602                                       mem_order, &base_reg, &offset, true);
10603
10604   if (stm_case == 0)
10605     return false;
10606
10607   base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
10608
10609   base_reg_dies = peep2_reg_dead_p (nops, base_reg_rtx);
10610   if (TARGET_THUMB1)
10611     {
10612       gcc_assert (base_reg_dies);
10613       write_back = TRUE;
10614     }
10615
10616   if (stm_case == 5)
10617     {
10618       gcc_assert (base_reg_dies);
10619       emit_insn (gen_addsi3 (base_reg_rtx, base_reg_rtx, GEN_INT (offset)));
10620       offset = 0;
10621     }
10622
10623   addr = plus_constant (base_reg_rtx, offset);
10624
10625   for (i = 0; i < nops; i++)
10626     {
10627       addr = plus_constant (base_reg_rtx, offset + i * 4);
10628       mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
10629                                               SImode, addr, 0);
10630     }
10631   emit_insn (arm_gen_store_multiple_1 (nops, regs, mems, base_reg_rtx,
10632                                        write_back ? offset + i * 4 : 0));
10633   return true;
10634 }
10635
10636 /* Called from a peephole2 expander to turn a sequence of stores that are
10637    preceded by constant loads into an STM instruction.  OPERANDS are the
10638    operands found by the peephole matcher; NOPS indicates how many
10639    separate stores we are trying to combine; there are 2 * NOPS
10640    instructions in the peephole.
10641    Returns true iff we could generate a new instruction.  */
10642
10643 bool
10644 gen_const_stm_seq (rtx *operands, int nops)
10645 {
10646   int regs[MAX_LDM_STM_OPS], sorted_regs[MAX_LDM_STM_OPS];
10647   int reg_order[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
10648   rtx reg_rtxs[MAX_LDM_STM_OPS], orig_reg_rtxs[MAX_LDM_STM_OPS];
10649   rtx mems[MAX_LDM_STM_OPS];
10650   int base_reg;
10651   rtx base_reg_rtx;
10652   HOST_WIDE_INT offset;
10653   int write_back = FALSE;
10654   int stm_case;
10655   rtx addr;
10656   bool base_reg_dies;
10657   int i, j;
10658   HARD_REG_SET allocated;
10659
10660   stm_case = store_multiple_sequence (operands, nops, 2 * nops, regs, reg_rtxs,
10661                                       mem_order, &base_reg, &offset, false);
10662
10663   if (stm_case == 0)
10664     return false;
10665
10666   memcpy (orig_reg_rtxs, reg_rtxs, sizeof orig_reg_rtxs);
10667
10668   /* If the same register is used more than once, try to find a free
10669      register.  */
10670   CLEAR_HARD_REG_SET (allocated);
10671   for (i = 0; i < nops; i++)
10672     {
10673       for (j = i + 1; j < nops; j++)
10674         if (regs[i] == regs[j])
10675           {
10676             rtx t = peep2_find_free_register (0, nops * 2,
10677                                               TARGET_THUMB1 ? "l" : "r",
10678                                               SImode, &allocated);
10679             if (t == NULL_RTX)
10680               return false;
10681             reg_rtxs[i] = t;
10682             regs[i] = REGNO (t);
10683           }
10684     }
10685
10686   /* Compute an ordering that maps the register numbers to an ascending
10687      sequence.  */
10688   reg_order[0] = 0;
10689   for (i = 0; i < nops; i++)
10690     if (regs[i] < regs[reg_order[0]])
10691       reg_order[0] = i;
10692
10693   for (i = 1; i < nops; i++)
10694     {
10695       int this_order = reg_order[i - 1];
10696       for (j = 0; j < nops; j++)
10697         if (regs[j] > regs[reg_order[i - 1]]
10698             && (this_order == reg_order[i - 1]
10699                 || regs[j] < regs[this_order]))
10700           this_order = j;
10701       reg_order[i] = this_order;
10702     }
10703
10704   /* Ensure that registers that must be live after the instruction end
10705      up with the correct value.  */
10706   for (i = 0; i < nops; i++)
10707     {
10708       int this_order = reg_order[i];
10709       if ((this_order != mem_order[i]
10710            || orig_reg_rtxs[this_order] != reg_rtxs[this_order])
10711           && !peep2_reg_dead_p (nops * 2, orig_reg_rtxs[this_order]))
10712         return false;
10713     }
10714
10715   /* Load the constants.  */
10716   for (i = 0; i < nops; i++)
10717     {
10718       rtx op = operands[2 * nops + mem_order[i]];
10719       sorted_regs[i] = regs[reg_order[i]];
10720       emit_move_insn (reg_rtxs[reg_order[i]], op);
10721     }
10722
10723   base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
10724
10725   base_reg_dies = peep2_reg_dead_p (nops * 2, base_reg_rtx);
10726   if (TARGET_THUMB1)
10727     {
10728       gcc_assert (base_reg_dies);
10729       write_back = TRUE;
10730     }
10731
10732   if (stm_case == 5)
10733     {
10734       gcc_assert (base_reg_dies);
10735       emit_insn (gen_addsi3 (base_reg_rtx, base_reg_rtx, GEN_INT (offset)));
10736       offset = 0;
10737     }
10738
10739   addr = plus_constant (base_reg_rtx, offset);
10740
10741   for (i = 0; i < nops; i++)
10742     {
10743       addr = plus_constant (base_reg_rtx, offset + i * 4);
10744       mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
10745                                               SImode, addr, 0);
10746     }
10747   emit_insn (arm_gen_store_multiple_1 (nops, sorted_regs, mems, base_reg_rtx,
10748                                        write_back ? offset + i * 4 : 0));
10749   return true;
10750 }
10751
10752 int
10753 arm_gen_movmemqi (rtx *operands)
10754 {
10755   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
10756   HOST_WIDE_INT srcoffset, dstoffset;
10757   int i;
10758   rtx src, dst, srcbase, dstbase;
10759   rtx part_bytes_reg = NULL;
10760   rtx mem;
10761
10762   if (GET_CODE (operands[2]) != CONST_INT
10763       || GET_CODE (operands[3]) != CONST_INT
10764       || INTVAL (operands[2]) > 64
10765       || INTVAL (operands[3]) & 3)
10766     return 0;
10767
10768   dstbase = operands[0];
10769   srcbase = operands[1];
10770
10771   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
10772   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
10773
10774   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
10775   out_words_to_go = INTVAL (operands[2]) / 4;
10776   last_bytes = INTVAL (operands[2]) & 3;
10777   dstoffset = srcoffset = 0;
10778
10779   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
10780     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
10781
10782   for (i = 0; in_words_to_go >= 2; i+=4)
10783     {
10784       if (in_words_to_go > 4)
10785         emit_insn (arm_gen_load_multiple (arm_regs_in_sequence, 4, src,
10786                                           TRUE, srcbase, &srcoffset));
10787       else
10788         emit_insn (arm_gen_load_multiple (arm_regs_in_sequence, in_words_to_go,
10789                                           src, FALSE, srcbase,
10790                                           &srcoffset));
10791
10792       if (out_words_to_go)
10793         {
10794           if (out_words_to_go > 4)
10795             emit_insn (arm_gen_store_multiple (arm_regs_in_sequence, 4, dst,
10796                                                TRUE, dstbase, &dstoffset));
10797           else if (out_words_to_go != 1)
10798             emit_insn (arm_gen_store_multiple (arm_regs_in_sequence,
10799                                                out_words_to_go, dst,
10800                                                (last_bytes == 0
10801                                                 ? FALSE : TRUE),
10802                                                dstbase, &dstoffset));
10803           else
10804             {
10805               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
10806               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
10807               if (last_bytes != 0)
10808                 {
10809                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
10810                   dstoffset += 4;
10811                 }
10812             }
10813         }
10814
10815       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
10816       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
10817     }
10818
10819   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
10820   if (out_words_to_go)
10821     {
10822       rtx sreg;
10823
10824       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
10825       sreg = copy_to_reg (mem);
10826
10827       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
10828       emit_move_insn (mem, sreg);
10829       in_words_to_go--;
10830
10831       gcc_assert (!in_words_to_go);     /* Sanity check */
10832     }
10833
10834   if (in_words_to_go)
10835     {
10836       gcc_assert (in_words_to_go > 0);
10837
10838       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
10839       part_bytes_reg = copy_to_mode_reg (SImode, mem);
10840     }
10841
10842   gcc_assert (!last_bytes || part_bytes_reg);
10843
10844   if (BYTES_BIG_ENDIAN && last_bytes)
10845     {
10846       rtx tmp = gen_reg_rtx (SImode);
10847
10848       /* The bytes we want are in the top end of the word.  */
10849       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
10850                               GEN_INT (8 * (4 - last_bytes))));
10851       part_bytes_reg = tmp;
10852
10853       while (last_bytes)
10854         {
10855           mem = adjust_automodify_address (dstbase, QImode,
10856                                            plus_constant (dst, last_bytes - 1),
10857                                            dstoffset + last_bytes - 1);
10858           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
10859
10860           if (--last_bytes)
10861             {
10862               tmp = gen_reg_rtx (SImode);
10863               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
10864               part_bytes_reg = tmp;
10865             }
10866         }
10867
10868     }
10869   else
10870     {
10871       if (last_bytes > 1)
10872         {
10873           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
10874           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
10875           last_bytes -= 2;
10876           if (last_bytes)
10877             {
10878               rtx tmp = gen_reg_rtx (SImode);
10879               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
10880               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
10881               part_bytes_reg = tmp;
10882               dstoffset += 2;
10883             }
10884         }
10885
10886       if (last_bytes)
10887         {
10888           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
10889           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
10890         }
10891     }
10892
10893   return 1;
10894 }
10895
10896 /* Select a dominance comparison mode if possible for a test of the general
10897    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
10898    COND_OR == DOM_CC_X_AND_Y => (X && Y)
10899    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
10900    COND_OR == DOM_CC_X_OR_Y => (X || Y)
10901    In all cases OP will be either EQ or NE, but we don't need to know which
10902    here.  If we are unable to support a dominance comparison we return
10903    CC mode.  This will then fail to match for the RTL expressions that
10904    generate this call.  */
10905 enum machine_mode
10906 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
10907 {
10908   enum rtx_code cond1, cond2;
10909   int swapped = 0;
10910
10911   /* Currently we will probably get the wrong result if the individual
10912      comparisons are not simple.  This also ensures that it is safe to
10913      reverse a comparison if necessary.  */
10914   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
10915        != CCmode)
10916       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
10917           != CCmode))
10918     return CCmode;
10919
10920   /* The if_then_else variant of this tests the second condition if the
10921      first passes, but is true if the first fails.  Reverse the first
10922      condition to get a true "inclusive-or" expression.  */
10923   if (cond_or == DOM_CC_NX_OR_Y)
10924     cond1 = reverse_condition (cond1);
10925
10926   /* If the comparisons are not equal, and one doesn't dominate the other,
10927      then we can't do this.  */
10928   if (cond1 != cond2
10929       && !comparison_dominates_p (cond1, cond2)
10930       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
10931     return CCmode;
10932
10933   if (swapped)
10934     {
10935       enum rtx_code temp = cond1;
10936       cond1 = cond2;
10937       cond2 = temp;
10938     }
10939
10940   switch (cond1)
10941     {
10942     case EQ:
10943       if (cond_or == DOM_CC_X_AND_Y)
10944         return CC_DEQmode;
10945
10946       switch (cond2)
10947         {
10948         case EQ: return CC_DEQmode;
10949         case LE: return CC_DLEmode;
10950         case LEU: return CC_DLEUmode;
10951         case GE: return CC_DGEmode;
10952         case GEU: return CC_DGEUmode;
10953         default: gcc_unreachable ();
10954         }
10955
10956     case LT:
10957       if (cond_or == DOM_CC_X_AND_Y)
10958         return CC_DLTmode;
10959
10960       switch (cond2)
10961         {
10962         case  LT:
10963             return CC_DLTmode;
10964         case LE:
10965           return CC_DLEmode;
10966         case NE:
10967           return CC_DNEmode;
10968         default:
10969           gcc_unreachable ();
10970         }
10971
10972     case GT:
10973       if (cond_or == DOM_CC_X_AND_Y)
10974         return CC_DGTmode;
10975
10976       switch (cond2)
10977         {
10978         case GT:
10979           return CC_DGTmode;
10980         case GE:
10981           return CC_DGEmode;
10982         case NE:
10983           return CC_DNEmode;
10984         default:
10985           gcc_unreachable ();
10986         }
10987
10988     case LTU:
10989       if (cond_or == DOM_CC_X_AND_Y)
10990         return CC_DLTUmode;
10991
10992       switch (cond2)
10993         {
10994         case LTU:
10995           return CC_DLTUmode;
10996         case LEU:
10997           return CC_DLEUmode;
10998         case NE:
10999           return CC_DNEmode;
11000         default:
11001           gcc_unreachable ();
11002         }
11003
11004     case GTU:
11005       if (cond_or == DOM_CC_X_AND_Y)
11006         return CC_DGTUmode;
11007
11008       switch (cond2)
11009         {
11010         case GTU:
11011           return CC_DGTUmode;
11012         case GEU:
11013           return CC_DGEUmode;
11014         case NE:
11015           return CC_DNEmode;
11016         default:
11017           gcc_unreachable ();
11018         }
11019
11020     /* The remaining cases only occur when both comparisons are the
11021        same.  */
11022     case NE:
11023       gcc_assert (cond1 == cond2);
11024       return CC_DNEmode;
11025
11026     case LE:
11027       gcc_assert (cond1 == cond2);
11028       return CC_DLEmode;
11029
11030     case GE:
11031       gcc_assert (cond1 == cond2);
11032       return CC_DGEmode;
11033
11034     case LEU:
11035       gcc_assert (cond1 == cond2);
11036       return CC_DLEUmode;
11037
11038     case GEU:
11039       gcc_assert (cond1 == cond2);
11040       return CC_DGEUmode;
11041
11042     default:
11043       gcc_unreachable ();
11044     }
11045 }
11046
11047 enum machine_mode
11048 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
11049 {
11050   /* All floating point compares return CCFP if it is an equality
11051      comparison, and CCFPE otherwise.  */
11052   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
11053     {
11054       switch (op)
11055         {
11056         case EQ:
11057         case NE:
11058         case UNORDERED:
11059         case ORDERED:
11060         case UNLT:
11061         case UNLE:
11062         case UNGT:
11063         case UNGE:
11064         case UNEQ:
11065         case LTGT:
11066           return CCFPmode;
11067
11068         case LT:
11069         case LE:
11070         case GT:
11071         case GE:
11072           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
11073             return CCFPmode;
11074           return CCFPEmode;
11075
11076         default:
11077           gcc_unreachable ();
11078         }
11079     }
11080
11081   /* A compare with a shifted operand.  Because of canonicalization, the
11082      comparison will have to be swapped when we emit the assembler.  */
11083   if (GET_MODE (y) == SImode 
11084       && (REG_P (y) || (GET_CODE (y) == SUBREG))
11085       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
11086           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
11087           || GET_CODE (x) == ROTATERT))
11088     return CC_SWPmode;
11089
11090   /* This operation is performed swapped, but since we only rely on the Z
11091      flag we don't need an additional mode.  */
11092   if (GET_MODE (y) == SImode 
11093       && (REG_P (y) || (GET_CODE (y) == SUBREG))
11094       && GET_CODE (x) == NEG
11095       && (op == EQ || op == NE))
11096     return CC_Zmode;
11097
11098   /* This is a special case that is used by combine to allow a
11099      comparison of a shifted byte load to be split into a zero-extend
11100      followed by a comparison of the shifted integer (only valid for
11101      equalities and unsigned inequalities).  */
11102   if (GET_MODE (x) == SImode
11103       && GET_CODE (x) == ASHIFT
11104       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
11105       && GET_CODE (XEXP (x, 0)) == SUBREG
11106       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
11107       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
11108       && (op == EQ || op == NE
11109           || op == GEU || op == GTU || op == LTU || op == LEU)
11110       && GET_CODE (y) == CONST_INT)
11111     return CC_Zmode;
11112
11113   /* A construct for a conditional compare, if the false arm contains
11114      0, then both conditions must be true, otherwise either condition
11115      must be true.  Not all conditions are possible, so CCmode is
11116      returned if it can't be done.  */
11117   if (GET_CODE (x) == IF_THEN_ELSE
11118       && (XEXP (x, 2) == const0_rtx
11119           || XEXP (x, 2) == const1_rtx)
11120       && COMPARISON_P (XEXP (x, 0))
11121       && COMPARISON_P (XEXP (x, 1)))
11122     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
11123                                          INTVAL (XEXP (x, 2)));
11124
11125   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
11126   if (GET_CODE (x) == AND
11127       && (op == EQ || op == NE)
11128       && COMPARISON_P (XEXP (x, 0))
11129       && COMPARISON_P (XEXP (x, 1)))
11130     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
11131                                          DOM_CC_X_AND_Y);
11132
11133   if (GET_CODE (x) == IOR
11134       && (op == EQ || op == NE)
11135       && COMPARISON_P (XEXP (x, 0))
11136       && COMPARISON_P (XEXP (x, 1)))
11137     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
11138                                          DOM_CC_X_OR_Y);
11139
11140   /* An operation (on Thumb) where we want to test for a single bit.
11141      This is done by shifting that bit up into the top bit of a
11142      scratch register; we can then branch on the sign bit.  */
11143   if (TARGET_THUMB1
11144       && GET_MODE (x) == SImode
11145       && (op == EQ || op == NE)
11146       && GET_CODE (x) == ZERO_EXTRACT
11147       && XEXP (x, 1) == const1_rtx)
11148     return CC_Nmode;
11149
11150   /* An operation that sets the condition codes as a side-effect, the
11151      V flag is not set correctly, so we can only use comparisons where
11152      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
11153      instead.)  */
11154   /* ??? Does the ZERO_EXTRACT case really apply to thumb2?  */
11155   if (GET_MODE (x) == SImode
11156       && y == const0_rtx
11157       && (op == EQ || op == NE || op == LT || op == GE)
11158       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
11159           || GET_CODE (x) == AND || GET_CODE (x) == IOR
11160           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
11161           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
11162           || GET_CODE (x) == LSHIFTRT
11163           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
11164           || GET_CODE (x) == ROTATERT
11165           || (TARGET_32BIT && GET_CODE (x) == ZERO_EXTRACT)))
11166     return CC_NOOVmode;
11167
11168   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
11169     return CC_Zmode;
11170
11171   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
11172       && GET_CODE (x) == PLUS
11173       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
11174     return CC_Cmode;
11175
11176   if (GET_MODE (x) == DImode || GET_MODE (y) == DImode)
11177     {
11178       /* To keep things simple, always use the Cirrus cfcmp64 if it is
11179          available.  */
11180       if (TARGET_ARM && TARGET_HARD_FLOAT && TARGET_MAVERICK)
11181         return CCmode;
11182
11183       switch (op)
11184         {
11185         case EQ:
11186         case NE:
11187           /* A DImode comparison against zero can be implemented by
11188              or'ing the two halves together.  */
11189           if (y == const0_rtx)
11190             return CC_Zmode;
11191
11192           /* We can do an equality test in three Thumb instructions.  */
11193           if (!TARGET_ARM)
11194             return CC_Zmode;
11195
11196           /* FALLTHROUGH */
11197
11198         case LTU:
11199         case LEU:
11200         case GTU:
11201         case GEU:
11202           /* DImode unsigned comparisons can be implemented by cmp +
11203              cmpeq without a scratch register.  Not worth doing in
11204              Thumb-2.  */
11205           if (TARGET_ARM)
11206             return CC_CZmode;
11207
11208           /* FALLTHROUGH */
11209
11210         case LT:
11211         case LE:
11212         case GT:
11213         case GE:
11214           /* DImode signed and unsigned comparisons can be implemented
11215              by cmp + sbcs with a scratch register, but that does not
11216              set the Z flag - we must reverse GT/LE/GTU/LEU.  */
11217           gcc_assert (op != EQ && op != NE);
11218           return CC_NCVmode;
11219
11220         default:
11221           gcc_unreachable ();
11222         }
11223     }
11224
11225   return CCmode;
11226 }
11227
11228 /* X and Y are two things to compare using CODE.  Emit the compare insn and
11229    return the rtx for register 0 in the proper mode.  FP means this is a
11230    floating point compare: I don't think that it is needed on the arm.  */
11231 rtx
11232 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
11233 {
11234   enum machine_mode mode;
11235   rtx cc_reg;
11236   int dimode_comparison = GET_MODE (x) == DImode || GET_MODE (y) == DImode;
11237
11238   /* We might have X as a constant, Y as a register because of the predicates
11239      used for cmpdi.  If so, force X to a register here.  */
11240   if (dimode_comparison && !REG_P (x))
11241     x = force_reg (DImode, x);
11242
11243   mode = SELECT_CC_MODE (code, x, y);
11244   cc_reg = gen_rtx_REG (mode, CC_REGNUM);
11245
11246   if (dimode_comparison
11247       && !(TARGET_HARD_FLOAT && TARGET_MAVERICK)
11248       && mode != CC_CZmode)
11249     {
11250       rtx clobber, set;
11251
11252       /* To compare two non-zero values for equality, XOR them and
11253          then compare against zero.  Not used for ARM mode; there
11254          CC_CZmode is cheaper.  */
11255       if (mode == CC_Zmode && y != const0_rtx)
11256         {
11257           x = expand_binop (DImode, xor_optab, x, y, NULL_RTX, 0, OPTAB_WIDEN);
11258           y = const0_rtx;
11259         }
11260       /* A scratch register is required.  */
11261       clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (SImode));
11262       set = gen_rtx_SET (VOIDmode, cc_reg, gen_rtx_COMPARE (mode, x, y));
11263       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
11264     }
11265   else
11266     emit_set_insn (cc_reg, gen_rtx_COMPARE (mode, x, y));
11267
11268   return cc_reg;
11269 }
11270
11271 /* Generate a sequence of insns that will generate the correct return
11272    address mask depending on the physical architecture that the program
11273    is running on.  */
11274 rtx
11275 arm_gen_return_addr_mask (void)
11276 {
11277   rtx reg = gen_reg_rtx (Pmode);
11278
11279   emit_insn (gen_return_addr_mask (reg));
11280   return reg;
11281 }
11282
11283 void
11284 arm_reload_in_hi (rtx *operands)
11285 {
11286   rtx ref = operands[1];
11287   rtx base, scratch;
11288   HOST_WIDE_INT offset = 0;
11289
11290   if (GET_CODE (ref) == SUBREG)
11291     {
11292       offset = SUBREG_BYTE (ref);
11293       ref = SUBREG_REG (ref);
11294     }
11295
11296   if (GET_CODE (ref) == REG)
11297     {
11298       /* We have a pseudo which has been spilt onto the stack; there
11299          are two cases here: the first where there is a simple
11300          stack-slot replacement and a second where the stack-slot is
11301          out of range, or is used as a subreg.  */
11302       if (reg_equiv_mem (REGNO (ref)))
11303         {
11304           ref = reg_equiv_mem (REGNO (ref));
11305           base = find_replacement (&XEXP (ref, 0));
11306         }
11307       else
11308         /* The slot is out of range, or was dressed up in a SUBREG.  */
11309         base = reg_equiv_address (REGNO (ref));
11310     }
11311   else
11312     base = find_replacement (&XEXP (ref, 0));
11313
11314   /* Handle the case where the address is too complex to be offset by 1.  */
11315   if (GET_CODE (base) == MINUS
11316       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
11317     {
11318       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
11319
11320       emit_set_insn (base_plus, base);
11321       base = base_plus;
11322     }
11323   else if (GET_CODE (base) == PLUS)
11324     {
11325       /* The addend must be CONST_INT, or we would have dealt with it above.  */
11326       HOST_WIDE_INT hi, lo;
11327
11328       offset += INTVAL (XEXP (base, 1));
11329       base = XEXP (base, 0);
11330
11331       /* Rework the address into a legal sequence of insns.  */
11332       /* Valid range for lo is -4095 -> 4095 */
11333       lo = (offset >= 0
11334             ? (offset & 0xfff)
11335             : -((-offset) & 0xfff));
11336
11337       /* Corner case, if lo is the max offset then we would be out of range
11338          once we have added the additional 1 below, so bump the msb into the
11339          pre-loading insn(s).  */
11340       if (lo == 4095)
11341         lo &= 0x7ff;
11342
11343       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
11344              ^ (HOST_WIDE_INT) 0x80000000)
11345             - (HOST_WIDE_INT) 0x80000000);
11346
11347       gcc_assert (hi + lo == offset);
11348
11349       if (hi != 0)
11350         {
11351           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
11352
11353           /* Get the base address; addsi3 knows how to handle constants
11354              that require more than one insn.  */
11355           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
11356           base = base_plus;
11357           offset = lo;
11358         }
11359     }
11360
11361   /* Operands[2] may overlap operands[0] (though it won't overlap
11362      operands[1]), that's why we asked for a DImode reg -- so we can
11363      use the bit that does not overlap.  */
11364   if (REGNO (operands[2]) == REGNO (operands[0]))
11365     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
11366   else
11367     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
11368
11369   emit_insn (gen_zero_extendqisi2 (scratch,
11370                                    gen_rtx_MEM (QImode,
11371                                                 plus_constant (base,
11372                                                                offset))));
11373   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
11374                                    gen_rtx_MEM (QImode,
11375                                                 plus_constant (base,
11376                                                                offset + 1))));
11377   if (!BYTES_BIG_ENDIAN)
11378     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
11379                    gen_rtx_IOR (SImode,
11380                                 gen_rtx_ASHIFT
11381                                 (SImode,
11382                                  gen_rtx_SUBREG (SImode, operands[0], 0),
11383                                  GEN_INT (8)),
11384                                 scratch));
11385   else
11386     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
11387                    gen_rtx_IOR (SImode,
11388                                 gen_rtx_ASHIFT (SImode, scratch,
11389                                                 GEN_INT (8)),
11390                                 gen_rtx_SUBREG (SImode, operands[0], 0)));
11391 }
11392
11393 /* Handle storing a half-word to memory during reload by synthesizing as two
11394    byte stores.  Take care not to clobber the input values until after we
11395    have moved them somewhere safe.  This code assumes that if the DImode
11396    scratch in operands[2] overlaps either the input value or output address
11397    in some way, then that value must die in this insn (we absolutely need
11398    two scratch registers for some corner cases).  */
11399 void
11400 arm_reload_out_hi (rtx *operands)
11401 {
11402   rtx ref = operands[0];
11403   rtx outval = operands[1];
11404   rtx base, scratch;
11405   HOST_WIDE_INT offset = 0;
11406
11407   if (GET_CODE (ref) == SUBREG)
11408     {
11409       offset = SUBREG_BYTE (ref);
11410       ref = SUBREG_REG (ref);
11411     }
11412
11413   if (GET_CODE (ref) == REG)
11414     {
11415       /* We have a pseudo which has been spilt onto the stack; there
11416          are two cases here: the first where there is a simple
11417          stack-slot replacement and a second where the stack-slot is
11418          out of range, or is used as a subreg.  */
11419       if (reg_equiv_mem (REGNO (ref)))
11420         {
11421           ref = reg_equiv_mem (REGNO (ref));
11422           base = find_replacement (&XEXP (ref, 0));
11423         }
11424       else
11425         /* The slot is out of range, or was dressed up in a SUBREG.  */
11426         base = reg_equiv_address (REGNO (ref));
11427     }
11428   else
11429     base = find_replacement (&XEXP (ref, 0));
11430
11431   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
11432
11433   /* Handle the case where the address is too complex to be offset by 1.  */
11434   if (GET_CODE (base) == MINUS
11435       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
11436     {
11437       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
11438
11439       /* Be careful not to destroy OUTVAL.  */
11440       if (reg_overlap_mentioned_p (base_plus, outval))
11441         {
11442           /* Updating base_plus might destroy outval, see if we can
11443              swap the scratch and base_plus.  */
11444           if (!reg_overlap_mentioned_p (scratch, outval))
11445             {
11446               rtx tmp = scratch;
11447               scratch = base_plus;
11448               base_plus = tmp;
11449             }
11450           else
11451             {
11452               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
11453
11454               /* Be conservative and copy OUTVAL into the scratch now,
11455                  this should only be necessary if outval is a subreg
11456                  of something larger than a word.  */
11457               /* XXX Might this clobber base?  I can't see how it can,
11458                  since scratch is known to overlap with OUTVAL, and
11459                  must be wider than a word.  */
11460               emit_insn (gen_movhi (scratch_hi, outval));
11461               outval = scratch_hi;
11462             }
11463         }
11464
11465       emit_set_insn (base_plus, base);
11466       base = base_plus;
11467     }
11468   else if (GET_CODE (base) == PLUS)
11469     {
11470       /* The addend must be CONST_INT, or we would have dealt with it above.  */
11471       HOST_WIDE_INT hi, lo;
11472
11473       offset += INTVAL (XEXP (base, 1));
11474       base = XEXP (base, 0);
11475
11476       /* Rework the address into a legal sequence of insns.  */
11477       /* Valid range for lo is -4095 -> 4095 */
11478       lo = (offset >= 0
11479             ? (offset & 0xfff)
11480             : -((-offset) & 0xfff));
11481
11482       /* Corner case, if lo is the max offset then we would be out of range
11483          once we have added the additional 1 below, so bump the msb into the
11484          pre-loading insn(s).  */
11485       if (lo == 4095)
11486         lo &= 0x7ff;
11487
11488       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
11489              ^ (HOST_WIDE_INT) 0x80000000)
11490             - (HOST_WIDE_INT) 0x80000000);
11491
11492       gcc_assert (hi + lo == offset);
11493
11494       if (hi != 0)
11495         {
11496           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
11497
11498           /* Be careful not to destroy OUTVAL.  */
11499           if (reg_overlap_mentioned_p (base_plus, outval))
11500             {
11501               /* Updating base_plus might destroy outval, see if we
11502                  can swap the scratch and base_plus.  */
11503               if (!reg_overlap_mentioned_p (scratch, outval))
11504                 {
11505                   rtx tmp = scratch;
11506                   scratch = base_plus;
11507                   base_plus = tmp;
11508                 }
11509               else
11510                 {
11511                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
11512
11513                   /* Be conservative and copy outval into scratch now,
11514                      this should only be necessary if outval is a
11515                      subreg of something larger than a word.  */
11516                   /* XXX Might this clobber base?  I can't see how it
11517                      can, since scratch is known to overlap with
11518                      outval.  */
11519                   emit_insn (gen_movhi (scratch_hi, outval));
11520                   outval = scratch_hi;
11521                 }
11522             }
11523
11524           /* Get the base address; addsi3 knows how to handle constants
11525              that require more than one insn.  */
11526           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
11527           base = base_plus;
11528           offset = lo;
11529         }
11530     }
11531
11532   if (BYTES_BIG_ENDIAN)
11533     {
11534       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
11535                                          plus_constant (base, offset + 1)),
11536                             gen_lowpart (QImode, outval)));
11537       emit_insn (gen_lshrsi3 (scratch,
11538                               gen_rtx_SUBREG (SImode, outval, 0),
11539                               GEN_INT (8)));
11540       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
11541                             gen_lowpart (QImode, scratch)));
11542     }
11543   else
11544     {
11545       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
11546                             gen_lowpart (QImode, outval)));
11547       emit_insn (gen_lshrsi3 (scratch,
11548                               gen_rtx_SUBREG (SImode, outval, 0),
11549                               GEN_INT (8)));
11550       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
11551                                          plus_constant (base, offset + 1)),
11552                             gen_lowpart (QImode, scratch)));
11553     }
11554 }
11555
11556 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
11557    (padded to the size of a word) should be passed in a register.  */
11558
11559 static bool
11560 arm_must_pass_in_stack (enum machine_mode mode, const_tree type)
11561 {
11562   if (TARGET_AAPCS_BASED)
11563     return must_pass_in_stack_var_size (mode, type);
11564   else
11565     return must_pass_in_stack_var_size_or_pad (mode, type);
11566 }
11567
11568
11569 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
11570    Return true if an argument passed on the stack should be padded upwards,
11571    i.e. if the least-significant byte has useful data.
11572    For legacy APCS ABIs we use the default.  For AAPCS based ABIs small
11573    aggregate types are placed in the lowest memory address.  */
11574
11575 bool
11576 arm_pad_arg_upward (enum machine_mode mode ATTRIBUTE_UNUSED, const_tree type)
11577 {
11578   if (!TARGET_AAPCS_BASED)
11579     return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
11580
11581   if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
11582     return false;
11583
11584   return true;
11585 }
11586
11587
11588 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
11589    Return !BYTES_BIG_ENDIAN if the least significant byte of the
11590    register has useful data, and return the opposite if the most
11591    significant byte does.  */
11592
11593 bool
11594 arm_pad_reg_upward (enum machine_mode mode,
11595                     tree type, int first ATTRIBUTE_UNUSED)
11596 {
11597   if (TARGET_AAPCS_BASED && BYTES_BIG_ENDIAN)
11598     {
11599       /* For AAPCS, small aggregates, small fixed-point types,
11600          and small complex types are always padded upwards.  */
11601       if (type)
11602         {
11603           if ((AGGREGATE_TYPE_P (type)
11604                || TREE_CODE (type) == COMPLEX_TYPE
11605                || FIXED_POINT_TYPE_P (type))
11606               && int_size_in_bytes (type) <= 4)
11607             return true;
11608         }
11609       else
11610         {
11611           if ((COMPLEX_MODE_P (mode) || ALL_FIXED_POINT_MODE_P (mode))
11612               && GET_MODE_SIZE (mode) <= 4)
11613             return true;
11614         }
11615     }
11616
11617   /* Otherwise, use default padding.  */
11618   return !BYTES_BIG_ENDIAN;
11619 }
11620
11621 \f
11622 /* Print a symbolic form of X to the debug file, F.  */
11623 static void
11624 arm_print_value (FILE *f, rtx x)
11625 {
11626   switch (GET_CODE (x))
11627     {
11628     case CONST_INT:
11629       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
11630       return;
11631
11632     case CONST_DOUBLE:
11633       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
11634       return;
11635
11636     case CONST_VECTOR:
11637       {
11638         int i;
11639
11640         fprintf (f, "<");
11641         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
11642           {
11643             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
11644             if (i < (CONST_VECTOR_NUNITS (x) - 1))
11645               fputc (',', f);
11646           }
11647         fprintf (f, ">");
11648       }
11649       return;
11650
11651     case CONST_STRING:
11652       fprintf (f, "\"%s\"", XSTR (x, 0));
11653       return;
11654
11655     case SYMBOL_REF:
11656       fprintf (f, "`%s'", XSTR (x, 0));
11657       return;
11658
11659     case LABEL_REF:
11660       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
11661       return;
11662
11663     case CONST:
11664       arm_print_value (f, XEXP (x, 0));
11665       return;
11666
11667     case PLUS:
11668       arm_print_value (f, XEXP (x, 0));
11669       fprintf (f, "+");
11670       arm_print_value (f, XEXP (x, 1));
11671       return;
11672
11673     case PC:
11674       fprintf (f, "pc");
11675       return;
11676
11677     default:
11678       fprintf (f, "????");
11679       return;
11680     }
11681 }
11682 \f
11683 /* Routines for manipulation of the constant pool.  */
11684
11685 /* Arm instructions cannot load a large constant directly into a
11686    register; they have to come from a pc relative load.  The constant
11687    must therefore be placed in the addressable range of the pc
11688    relative load.  Depending on the precise pc relative load
11689    instruction the range is somewhere between 256 bytes and 4k.  This
11690    means that we often have to dump a constant inside a function, and
11691    generate code to branch around it.
11692
11693    It is important to minimize this, since the branches will slow
11694    things down and make the code larger.
11695
11696    Normally we can hide the table after an existing unconditional
11697    branch so that there is no interruption of the flow, but in the
11698    worst case the code looks like this:
11699
11700         ldr     rn, L1
11701         ...
11702         b       L2
11703         align
11704         L1:     .long value
11705         L2:
11706         ...
11707
11708         ldr     rn, L3
11709         ...
11710         b       L4
11711         align
11712         L3:     .long value
11713         L4:
11714         ...
11715
11716    We fix this by performing a scan after scheduling, which notices
11717    which instructions need to have their operands fetched from the
11718    constant table and builds the table.
11719
11720    The algorithm starts by building a table of all the constants that
11721    need fixing up and all the natural barriers in the function (places
11722    where a constant table can be dropped without breaking the flow).
11723    For each fixup we note how far the pc-relative replacement will be
11724    able to reach and the offset of the instruction into the function.
11725
11726    Having built the table we then group the fixes together to form
11727    tables that are as large as possible (subject to addressing
11728    constraints) and emit each table of constants after the last
11729    barrier that is within range of all the instructions in the group.
11730    If a group does not contain a barrier, then we forcibly create one
11731    by inserting a jump instruction into the flow.  Once the table has
11732    been inserted, the insns are then modified to reference the
11733    relevant entry in the pool.
11734
11735    Possible enhancements to the algorithm (not implemented) are:
11736
11737    1) For some processors and object formats, there may be benefit in
11738    aligning the pools to the start of cache lines; this alignment
11739    would need to be taken into account when calculating addressability
11740    of a pool.  */
11741
11742 /* These typedefs are located at the start of this file, so that
11743    they can be used in the prototypes there.  This comment is to
11744    remind readers of that fact so that the following structures
11745    can be understood more easily.
11746
11747      typedef struct minipool_node    Mnode;
11748      typedef struct minipool_fixup   Mfix;  */
11749
11750 struct minipool_node
11751 {
11752   /* Doubly linked chain of entries.  */
11753   Mnode * next;
11754   Mnode * prev;
11755   /* The maximum offset into the code that this entry can be placed.  While
11756      pushing fixes for forward references, all entries are sorted in order
11757      of increasing max_address.  */
11758   HOST_WIDE_INT max_address;
11759   /* Similarly for an entry inserted for a backwards ref.  */
11760   HOST_WIDE_INT min_address;
11761   /* The number of fixes referencing this entry.  This can become zero
11762      if we "unpush" an entry.  In this case we ignore the entry when we
11763      come to emit the code.  */
11764   int refcount;
11765   /* The offset from the start of the minipool.  */
11766   HOST_WIDE_INT offset;
11767   /* The value in table.  */
11768   rtx value;
11769   /* The mode of value.  */
11770   enum machine_mode mode;
11771   /* The size of the value.  With iWMMXt enabled
11772      sizes > 4 also imply an alignment of 8-bytes.  */
11773   int fix_size;
11774 };
11775
11776 struct minipool_fixup
11777 {
11778   Mfix *            next;
11779   rtx               insn;
11780   HOST_WIDE_INT     address;
11781   rtx *             loc;
11782   enum machine_mode mode;
11783   int               fix_size;
11784   rtx               value;
11785   Mnode *           minipool;
11786   HOST_WIDE_INT     forwards;
11787   HOST_WIDE_INT     backwards;
11788 };
11789
11790 /* Fixes less than a word need padding out to a word boundary.  */
11791 #define MINIPOOL_FIX_SIZE(mode) \
11792   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
11793
11794 static Mnode *  minipool_vector_head;
11795 static Mnode *  minipool_vector_tail;
11796 static rtx      minipool_vector_label;
11797 static int      minipool_pad;
11798
11799 /* The linked list of all minipool fixes required for this function.  */
11800 Mfix *          minipool_fix_head;
11801 Mfix *          minipool_fix_tail;
11802 /* The fix entry for the current minipool, once it has been placed.  */
11803 Mfix *          minipool_barrier;
11804
11805 /* Determines if INSN is the start of a jump table.  Returns the end
11806    of the TABLE or NULL_RTX.  */
11807 static rtx
11808 is_jump_table (rtx insn)
11809 {
11810   rtx table;
11811
11812   if (jump_to_label_p (insn)
11813       && ((table = next_real_insn (JUMP_LABEL (insn)))
11814           == next_real_insn (insn))
11815       && table != NULL
11816       && GET_CODE (table) == JUMP_INSN
11817       && (GET_CODE (PATTERN (table)) == ADDR_VEC
11818           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
11819     return table;
11820
11821   return NULL_RTX;
11822 }
11823
11824 #ifndef JUMP_TABLES_IN_TEXT_SECTION
11825 #define JUMP_TABLES_IN_TEXT_SECTION 0
11826 #endif
11827
11828 static HOST_WIDE_INT
11829 get_jump_table_size (rtx insn)
11830 {
11831   /* ADDR_VECs only take room if read-only data does into the text
11832      section.  */
11833   if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
11834     {
11835       rtx body = PATTERN (insn);
11836       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
11837       HOST_WIDE_INT size;
11838       HOST_WIDE_INT modesize;
11839
11840       modesize = GET_MODE_SIZE (GET_MODE (body));
11841       size = modesize * XVECLEN (body, elt);
11842       switch (modesize)
11843         {
11844         case 1:
11845           /* Round up size  of TBB table to a halfword boundary.  */
11846           size = (size + 1) & ~(HOST_WIDE_INT)1;
11847           break;
11848         case 2:
11849           /* No padding necessary for TBH.  */
11850           break;
11851         case 4:
11852           /* Add two bytes for alignment on Thumb.  */
11853           if (TARGET_THUMB)
11854             size += 2;
11855           break;
11856         default:
11857           gcc_unreachable ();
11858         }
11859       return size;
11860     }
11861
11862   return 0;
11863 }
11864
11865 /* Return the maximum amount of padding that will be inserted before
11866    label LABEL.  */
11867
11868 static HOST_WIDE_INT
11869 get_label_padding (rtx label)
11870 {
11871   HOST_WIDE_INT align, min_insn_size;
11872
11873   align = 1 << label_to_alignment (label);
11874   min_insn_size = TARGET_THUMB ? 2 : 4;
11875   return align > min_insn_size ? align - min_insn_size : 0;
11876 }
11877
11878 /* Move a minipool fix MP from its current location to before MAX_MP.
11879    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
11880    constraints may need updating.  */
11881 static Mnode *
11882 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
11883                                HOST_WIDE_INT max_address)
11884 {
11885   /* The code below assumes these are different.  */
11886   gcc_assert (mp != max_mp);
11887
11888   if (max_mp == NULL)
11889     {
11890       if (max_address < mp->max_address)
11891         mp->max_address = max_address;
11892     }
11893   else
11894     {
11895       if (max_address > max_mp->max_address - mp->fix_size)
11896         mp->max_address = max_mp->max_address - mp->fix_size;
11897       else
11898         mp->max_address = max_address;
11899
11900       /* Unlink MP from its current position.  Since max_mp is non-null,
11901        mp->prev must be non-null.  */
11902       mp->prev->next = mp->next;
11903       if (mp->next != NULL)
11904         mp->next->prev = mp->prev;
11905       else
11906         minipool_vector_tail = mp->prev;
11907
11908       /* Re-insert it before MAX_MP.  */
11909       mp->next = max_mp;
11910       mp->prev = max_mp->prev;
11911       max_mp->prev = mp;
11912
11913       if (mp->prev != NULL)
11914         mp->prev->next = mp;
11915       else
11916         minipool_vector_head = mp;
11917     }
11918
11919   /* Save the new entry.  */
11920   max_mp = mp;
11921
11922   /* Scan over the preceding entries and adjust their addresses as
11923      required.  */
11924   while (mp->prev != NULL
11925          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
11926     {
11927       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
11928       mp = mp->prev;
11929     }
11930
11931   return max_mp;
11932 }
11933
11934 /* Add a constant to the minipool for a forward reference.  Returns the
11935    node added or NULL if the constant will not fit in this pool.  */
11936 static Mnode *
11937 add_minipool_forward_ref (Mfix *fix)
11938 {
11939   /* If set, max_mp is the first pool_entry that has a lower
11940      constraint than the one we are trying to add.  */
11941   Mnode *       max_mp = NULL;
11942   HOST_WIDE_INT max_address = fix->address + fix->forwards - minipool_pad;
11943   Mnode *       mp;
11944
11945   /* If the minipool starts before the end of FIX->INSN then this FIX
11946      can not be placed into the current pool.  Furthermore, adding the
11947      new constant pool entry may cause the pool to start FIX_SIZE bytes
11948      earlier.  */
11949   if (minipool_vector_head &&
11950       (fix->address + get_attr_length (fix->insn)
11951        >= minipool_vector_head->max_address - fix->fix_size))
11952     return NULL;
11953
11954   /* Scan the pool to see if a constant with the same value has
11955      already been added.  While we are doing this, also note the
11956      location where we must insert the constant if it doesn't already
11957      exist.  */
11958   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11959     {
11960       if (GET_CODE (fix->value) == GET_CODE (mp->value)
11961           && fix->mode == mp->mode
11962           && (GET_CODE (fix->value) != CODE_LABEL
11963               || (CODE_LABEL_NUMBER (fix->value)
11964                   == CODE_LABEL_NUMBER (mp->value)))
11965           && rtx_equal_p (fix->value, mp->value))
11966         {
11967           /* More than one fix references this entry.  */
11968           mp->refcount++;
11969           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
11970         }
11971
11972       /* Note the insertion point if necessary.  */
11973       if (max_mp == NULL
11974           && mp->max_address > max_address)
11975         max_mp = mp;
11976
11977       /* If we are inserting an 8-bytes aligned quantity and
11978          we have not already found an insertion point, then
11979          make sure that all such 8-byte aligned quantities are
11980          placed at the start of the pool.  */
11981       if (ARM_DOUBLEWORD_ALIGN
11982           && max_mp == NULL
11983           && fix->fix_size >= 8
11984           && mp->fix_size < 8)
11985         {
11986           max_mp = mp;
11987           max_address = mp->max_address;
11988         }
11989     }
11990
11991   /* The value is not currently in the minipool, so we need to create
11992      a new entry for it.  If MAX_MP is NULL, the entry will be put on
11993      the end of the list since the placement is less constrained than
11994      any existing entry.  Otherwise, we insert the new fix before
11995      MAX_MP and, if necessary, adjust the constraints on the other
11996      entries.  */
11997   mp = XNEW (Mnode);
11998   mp->fix_size = fix->fix_size;
11999   mp->mode = fix->mode;
12000   mp->value = fix->value;
12001   mp->refcount = 1;
12002   /* Not yet required for a backwards ref.  */
12003   mp->min_address = -65536;
12004
12005   if (max_mp == NULL)
12006     {
12007       mp->max_address = max_address;
12008       mp->next = NULL;
12009       mp->prev = minipool_vector_tail;
12010
12011       if (mp->prev == NULL)
12012         {
12013           minipool_vector_head = mp;
12014           minipool_vector_label = gen_label_rtx ();
12015         }
12016       else
12017         mp->prev->next = mp;
12018
12019       minipool_vector_tail = mp;
12020     }
12021   else
12022     {
12023       if (max_address > max_mp->max_address - mp->fix_size)
12024         mp->max_address = max_mp->max_address - mp->fix_size;
12025       else
12026         mp->max_address = max_address;
12027
12028       mp->next = max_mp;
12029       mp->prev = max_mp->prev;
12030       max_mp->prev = mp;
12031       if (mp->prev != NULL)
12032         mp->prev->next = mp;
12033       else
12034         minipool_vector_head = mp;
12035     }
12036
12037   /* Save the new entry.  */
12038   max_mp = mp;
12039
12040   /* Scan over the preceding entries and adjust their addresses as
12041      required.  */
12042   while (mp->prev != NULL
12043          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
12044     {
12045       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
12046       mp = mp->prev;
12047     }
12048
12049   return max_mp;
12050 }
12051
12052 static Mnode *
12053 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
12054                                 HOST_WIDE_INT  min_address)
12055 {
12056   HOST_WIDE_INT offset;
12057
12058   /* The code below assumes these are different.  */
12059   gcc_assert (mp != min_mp);
12060
12061   if (min_mp == NULL)
12062     {
12063       if (min_address > mp->min_address)
12064         mp->min_address = min_address;
12065     }
12066   else
12067     {
12068       /* We will adjust this below if it is too loose.  */
12069       mp->min_address = min_address;
12070
12071       /* Unlink MP from its current position.  Since min_mp is non-null,
12072          mp->next must be non-null.  */
12073       mp->next->prev = mp->prev;
12074       if (mp->prev != NULL)
12075         mp->prev->next = mp->next;
12076       else
12077         minipool_vector_head = mp->next;
12078
12079       /* Reinsert it after MIN_MP.  */
12080       mp->prev = min_mp;
12081       mp->next = min_mp->next;
12082       min_mp->next = mp;
12083       if (mp->next != NULL)
12084         mp->next->prev = mp;
12085       else
12086         minipool_vector_tail = mp;
12087     }
12088
12089   min_mp = mp;
12090
12091   offset = 0;
12092   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
12093     {
12094       mp->offset = offset;
12095       if (mp->refcount > 0)
12096         offset += mp->fix_size;
12097
12098       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
12099         mp->next->min_address = mp->min_address + mp->fix_size;
12100     }
12101
12102   return min_mp;
12103 }
12104
12105 /* Add a constant to the minipool for a backward reference.  Returns the
12106    node added or NULL if the constant will not fit in this pool.
12107
12108    Note that the code for insertion for a backwards reference can be
12109    somewhat confusing because the calculated offsets for each fix do
12110    not take into account the size of the pool (which is still under
12111    construction.  */
12112 static Mnode *
12113 add_minipool_backward_ref (Mfix *fix)
12114 {
12115   /* If set, min_mp is the last pool_entry that has a lower constraint
12116      than the one we are trying to add.  */
12117   Mnode *min_mp = NULL;
12118   /* This can be negative, since it is only a constraint.  */
12119   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
12120   Mnode *mp;
12121
12122   /* If we can't reach the current pool from this insn, or if we can't
12123      insert this entry at the end of the pool without pushing other
12124      fixes out of range, then we don't try.  This ensures that we
12125      can't fail later on.  */
12126   if (min_address >= minipool_barrier->address
12127       || (minipool_vector_tail->min_address + fix->fix_size
12128           >= minipool_barrier->address))
12129     return NULL;
12130
12131   /* Scan the pool to see if a constant with the same value has
12132      already been added.  While we are doing this, also note the
12133      location where we must insert the constant if it doesn't already
12134      exist.  */
12135   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
12136     {
12137       if (GET_CODE (fix->value) == GET_CODE (mp->value)
12138           && fix->mode == mp->mode
12139           && (GET_CODE (fix->value) != CODE_LABEL
12140               || (CODE_LABEL_NUMBER (fix->value)
12141                   == CODE_LABEL_NUMBER (mp->value)))
12142           && rtx_equal_p (fix->value, mp->value)
12143           /* Check that there is enough slack to move this entry to the
12144              end of the table (this is conservative).  */
12145           && (mp->max_address
12146               > (minipool_barrier->address
12147                  + minipool_vector_tail->offset
12148                  + minipool_vector_tail->fix_size)))
12149         {
12150           mp->refcount++;
12151           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
12152         }
12153
12154       if (min_mp != NULL)
12155         mp->min_address += fix->fix_size;
12156       else
12157         {
12158           /* Note the insertion point if necessary.  */
12159           if (mp->min_address < min_address)
12160             {
12161               /* For now, we do not allow the insertion of 8-byte alignment
12162                  requiring nodes anywhere but at the start of the pool.  */
12163               if (ARM_DOUBLEWORD_ALIGN
12164                   && fix->fix_size >= 8 && mp->fix_size < 8)
12165                 return NULL;
12166               else
12167                 min_mp = mp;
12168             }
12169           else if (mp->max_address
12170                    < minipool_barrier->address + mp->offset + fix->fix_size)
12171             {
12172               /* Inserting before this entry would push the fix beyond
12173                  its maximum address (which can happen if we have
12174                  re-located a forwards fix); force the new fix to come
12175                  after it.  */
12176               if (ARM_DOUBLEWORD_ALIGN
12177                   && fix->fix_size >= 8 && mp->fix_size < 8)
12178                 return NULL;
12179               else
12180                 {
12181                   min_mp = mp;
12182                   min_address = mp->min_address + fix->fix_size;
12183                 }
12184             }
12185           /* Do not insert a non-8-byte aligned quantity before 8-byte
12186              aligned quantities.  */
12187           else if (ARM_DOUBLEWORD_ALIGN
12188                    && fix->fix_size < 8
12189                    && mp->fix_size >= 8)
12190             {
12191               min_mp = mp;
12192               min_address = mp->min_address + fix->fix_size;
12193             }
12194         }
12195     }
12196
12197   /* We need to create a new entry.  */
12198   mp = XNEW (Mnode);
12199   mp->fix_size = fix->fix_size;
12200   mp->mode = fix->mode;
12201   mp->value = fix->value;
12202   mp->refcount = 1;
12203   mp->max_address = minipool_barrier->address + 65536;
12204
12205   mp->min_address = min_address;
12206
12207   if (min_mp == NULL)
12208     {
12209       mp->prev = NULL;
12210       mp->next = minipool_vector_head;
12211
12212       if (mp->next == NULL)
12213         {
12214           minipool_vector_tail = mp;
12215           minipool_vector_label = gen_label_rtx ();
12216         }
12217       else
12218         mp->next->prev = mp;
12219
12220       minipool_vector_head = mp;
12221     }
12222   else
12223     {
12224       mp->next = min_mp->next;
12225       mp->prev = min_mp;
12226       min_mp->next = mp;
12227
12228       if (mp->next != NULL)
12229         mp->next->prev = mp;
12230       else
12231         minipool_vector_tail = mp;
12232     }
12233
12234   /* Save the new entry.  */
12235   min_mp = mp;
12236
12237   if (mp->prev)
12238     mp = mp->prev;
12239   else
12240     mp->offset = 0;
12241
12242   /* Scan over the following entries and adjust their offsets.  */
12243   while (mp->next != NULL)
12244     {
12245       if (mp->next->min_address < mp->min_address + mp->fix_size)
12246         mp->next->min_address = mp->min_address + mp->fix_size;
12247
12248       if (mp->refcount)
12249         mp->next->offset = mp->offset + mp->fix_size;
12250       else
12251         mp->next->offset = mp->offset;
12252
12253       mp = mp->next;
12254     }
12255
12256   return min_mp;
12257 }
12258
12259 static void
12260 assign_minipool_offsets (Mfix *barrier)
12261 {
12262   HOST_WIDE_INT offset = 0;
12263   Mnode *mp;
12264
12265   minipool_barrier = barrier;
12266
12267   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
12268     {
12269       mp->offset = offset;
12270
12271       if (mp->refcount > 0)
12272         offset += mp->fix_size;
12273     }
12274 }
12275
12276 /* Output the literal table */
12277 static void
12278 dump_minipool (rtx scan)
12279 {
12280   Mnode * mp;
12281   Mnode * nmp;
12282   int align64 = 0;
12283
12284   if (ARM_DOUBLEWORD_ALIGN)
12285     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
12286       if (mp->refcount > 0 && mp->fix_size >= 8)
12287         {
12288           align64 = 1;
12289           break;
12290         }
12291
12292   if (dump_file)
12293     fprintf (dump_file,
12294              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
12295              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
12296
12297   scan = emit_label_after (gen_label_rtx (), scan);
12298   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
12299   scan = emit_label_after (minipool_vector_label, scan);
12300
12301   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
12302     {
12303       if (mp->refcount > 0)
12304         {
12305           if (dump_file)
12306             {
12307               fprintf (dump_file,
12308                        ";;  Offset %u, min %ld, max %ld ",
12309                        (unsigned) mp->offset, (unsigned long) mp->min_address,
12310                        (unsigned long) mp->max_address);
12311               arm_print_value (dump_file, mp->value);
12312               fputc ('\n', dump_file);
12313             }
12314
12315           switch (mp->fix_size)
12316             {
12317 #ifdef HAVE_consttable_1
12318             case 1:
12319               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
12320               break;
12321
12322 #endif
12323 #ifdef HAVE_consttable_2
12324             case 2:
12325               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
12326               break;
12327
12328 #endif
12329 #ifdef HAVE_consttable_4
12330             case 4:
12331               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
12332               break;
12333
12334 #endif
12335 #ifdef HAVE_consttable_8
12336             case 8:
12337               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
12338               break;
12339
12340 #endif
12341 #ifdef HAVE_consttable_16
12342             case 16:
12343               scan = emit_insn_after (gen_consttable_16 (mp->value), scan);
12344               break;
12345
12346 #endif
12347             default:
12348               gcc_unreachable ();
12349             }
12350         }
12351
12352       nmp = mp->next;
12353       free (mp);
12354     }
12355
12356   minipool_vector_head = minipool_vector_tail = NULL;
12357   scan = emit_insn_after (gen_consttable_end (), scan);
12358   scan = emit_barrier_after (scan);
12359 }
12360
12361 /* Return the cost of forcibly inserting a barrier after INSN.  */
12362 static int
12363 arm_barrier_cost (rtx insn)
12364 {
12365   /* Basing the location of the pool on the loop depth is preferable,
12366      but at the moment, the basic block information seems to be
12367      corrupt by this stage of the compilation.  */
12368   int base_cost = 50;
12369   rtx next = next_nonnote_insn (insn);
12370
12371   if (next != NULL && GET_CODE (next) == CODE_LABEL)
12372     base_cost -= 20;
12373
12374   switch (GET_CODE (insn))
12375     {
12376     case CODE_LABEL:
12377       /* It will always be better to place the table before the label, rather
12378          than after it.  */
12379       return 50;
12380
12381     case INSN:
12382     case CALL_INSN:
12383       return base_cost;
12384
12385     case JUMP_INSN:
12386       return base_cost - 10;
12387
12388     default:
12389       return base_cost + 10;
12390     }
12391 }
12392
12393 /* Find the best place in the insn stream in the range
12394    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
12395    Create the barrier by inserting a jump and add a new fix entry for
12396    it.  */
12397 static Mfix *
12398 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
12399 {
12400   HOST_WIDE_INT count = 0;
12401   rtx barrier;
12402   rtx from = fix->insn;
12403   /* The instruction after which we will insert the jump.  */
12404   rtx selected = NULL;
12405   int selected_cost;
12406   /* The address at which the jump instruction will be placed.  */
12407   HOST_WIDE_INT selected_address;
12408   Mfix * new_fix;
12409   HOST_WIDE_INT max_count = max_address - fix->address;
12410   rtx label = gen_label_rtx ();
12411
12412   selected_cost = arm_barrier_cost (from);
12413   selected_address = fix->address;
12414
12415   while (from && count < max_count)
12416     {
12417       rtx tmp;
12418       int new_cost;
12419
12420       /* This code shouldn't have been called if there was a natural barrier
12421          within range.  */
12422       gcc_assert (GET_CODE (from) != BARRIER);
12423
12424       /* Count the length of this insn.  This must stay in sync with the
12425          code that pushes minipool fixes.  */
12426       if (LABEL_P (from))
12427         count += get_label_padding (from);
12428       else
12429         count += get_attr_length (from);
12430
12431       /* If there is a jump table, add its length.  */
12432       tmp = is_jump_table (from);
12433       if (tmp != NULL)
12434         {
12435           count += get_jump_table_size (tmp);
12436
12437           /* Jump tables aren't in a basic block, so base the cost on
12438              the dispatch insn.  If we select this location, we will
12439              still put the pool after the table.  */
12440           new_cost = arm_barrier_cost (from);
12441
12442           if (count < max_count 
12443               && (!selected || new_cost <= selected_cost))
12444             {
12445               selected = tmp;
12446               selected_cost = new_cost;
12447               selected_address = fix->address + count;
12448             }
12449
12450           /* Continue after the dispatch table.  */
12451           from = NEXT_INSN (tmp);
12452           continue;
12453         }
12454
12455       new_cost = arm_barrier_cost (from);
12456
12457       if (count < max_count
12458           && (!selected || new_cost <= selected_cost))
12459         {
12460           selected = from;
12461           selected_cost = new_cost;
12462           selected_address = fix->address + count;
12463         }
12464
12465       from = NEXT_INSN (from);
12466     }
12467
12468   /* Make sure that we found a place to insert the jump.  */
12469   gcc_assert (selected);
12470
12471   /* Make sure we do not split a call and its corresponding
12472      CALL_ARG_LOCATION note.  */
12473   if (CALL_P (selected))
12474     {
12475       rtx next = NEXT_INSN (selected);
12476       if (next && NOTE_P (next)
12477           && NOTE_KIND (next) == NOTE_INSN_CALL_ARG_LOCATION)
12478           selected = next;
12479     }
12480
12481   /* Create a new JUMP_INSN that branches around a barrier.  */
12482   from = emit_jump_insn_after (gen_jump (label), selected);
12483   JUMP_LABEL (from) = label;
12484   barrier = emit_barrier_after (from);
12485   emit_label_after (label, barrier);
12486
12487   /* Create a minipool barrier entry for the new barrier.  */
12488   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
12489   new_fix->insn = barrier;
12490   new_fix->address = selected_address;
12491   new_fix->next = fix->next;
12492   fix->next = new_fix;
12493
12494   return new_fix;
12495 }
12496
12497 /* Record that there is a natural barrier in the insn stream at
12498    ADDRESS.  */
12499 static void
12500 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
12501 {
12502   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
12503
12504   fix->insn = insn;
12505   fix->address = address;
12506
12507   fix->next = NULL;
12508   if (minipool_fix_head != NULL)
12509     minipool_fix_tail->next = fix;
12510   else
12511     minipool_fix_head = fix;
12512
12513   minipool_fix_tail = fix;
12514 }
12515
12516 /* Record INSN, which will need fixing up to load a value from the
12517    minipool.  ADDRESS is the offset of the insn since the start of the
12518    function; LOC is a pointer to the part of the insn which requires
12519    fixing; VALUE is the constant that must be loaded, which is of type
12520    MODE.  */
12521 static void
12522 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
12523                    enum machine_mode mode, rtx value)
12524 {
12525   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
12526
12527   fix->insn = insn;
12528   fix->address = address;
12529   fix->loc = loc;
12530   fix->mode = mode;
12531   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
12532   fix->value = value;
12533   fix->forwards = get_attr_pool_range (insn);
12534   fix->backwards = get_attr_neg_pool_range (insn);
12535   fix->minipool = NULL;
12536
12537   /* If an insn doesn't have a range defined for it, then it isn't
12538      expecting to be reworked by this code.  Better to stop now than
12539      to generate duff assembly code.  */
12540   gcc_assert (fix->forwards || fix->backwards);
12541
12542   /* If an entry requires 8-byte alignment then assume all constant pools
12543      require 4 bytes of padding.  Trying to do this later on a per-pool
12544      basis is awkward because existing pool entries have to be modified.  */
12545   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size >= 8)
12546     minipool_pad = 4;
12547
12548   if (dump_file)
12549     {
12550       fprintf (dump_file,
12551                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
12552                GET_MODE_NAME (mode),
12553                INSN_UID (insn), (unsigned long) address,
12554                -1 * (long)fix->backwards, (long)fix->forwards);
12555       arm_print_value (dump_file, fix->value);
12556       fprintf (dump_file, "\n");
12557     }
12558
12559   /* Add it to the chain of fixes.  */
12560   fix->next = NULL;
12561
12562   if (minipool_fix_head != NULL)
12563     minipool_fix_tail->next = fix;
12564   else
12565     minipool_fix_head = fix;
12566
12567   minipool_fix_tail = fix;
12568 }
12569
12570 /* Return the cost of synthesizing a 64-bit constant VAL inline.
12571    Returns the number of insns needed, or 99 if we don't know how to
12572    do it.  */
12573 int
12574 arm_const_double_inline_cost (rtx val)
12575 {
12576   rtx lowpart, highpart;
12577   enum machine_mode mode;
12578
12579   mode = GET_MODE (val);
12580
12581   if (mode == VOIDmode)
12582     mode = DImode;
12583
12584   gcc_assert (GET_MODE_SIZE (mode) == 8);
12585
12586   lowpart = gen_lowpart (SImode, val);
12587   highpart = gen_highpart_mode (SImode, mode, val);
12588
12589   gcc_assert (GET_CODE (lowpart) == CONST_INT);
12590   gcc_assert (GET_CODE (highpart) == CONST_INT);
12591
12592   return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
12593                             NULL_RTX, NULL_RTX, 0, 0)
12594           + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
12595                               NULL_RTX, NULL_RTX, 0, 0));
12596 }
12597
12598 /* Return true if it is worthwhile to split a 64-bit constant into two
12599    32-bit operations.  This is the case if optimizing for size, or
12600    if we have load delay slots, or if one 32-bit part can be done with
12601    a single data operation.  */
12602 bool
12603 arm_const_double_by_parts (rtx val)
12604 {
12605   enum machine_mode mode = GET_MODE (val);
12606   rtx part;
12607
12608   if (optimize_size || arm_ld_sched)
12609     return true;
12610
12611   if (mode == VOIDmode)
12612     mode = DImode;
12613
12614   part = gen_highpart_mode (SImode, mode, val);
12615
12616   gcc_assert (GET_CODE (part) == CONST_INT);
12617
12618   if (const_ok_for_arm (INTVAL (part))
12619       || const_ok_for_arm (~INTVAL (part)))
12620     return true;
12621
12622   part = gen_lowpart (SImode, val);
12623
12624   gcc_assert (GET_CODE (part) == CONST_INT);
12625
12626   if (const_ok_for_arm (INTVAL (part))
12627       || const_ok_for_arm (~INTVAL (part)))
12628     return true;
12629
12630   return false;
12631 }
12632
12633 /* Return true if it is possible to inline both the high and low parts
12634    of a 64-bit constant into 32-bit data processing instructions.  */
12635 bool
12636 arm_const_double_by_immediates (rtx val)
12637 {
12638   enum machine_mode mode = GET_MODE (val);
12639   rtx part;
12640
12641   if (mode == VOIDmode)
12642     mode = DImode;
12643
12644   part = gen_highpart_mode (SImode, mode, val);
12645
12646   gcc_assert (GET_CODE (part) == CONST_INT);
12647
12648   if (!const_ok_for_arm (INTVAL (part)))
12649     return false;
12650
12651   part = gen_lowpart (SImode, val);
12652
12653   gcc_assert (GET_CODE (part) == CONST_INT);
12654
12655   if (!const_ok_for_arm (INTVAL (part)))
12656     return false;
12657
12658   return true;
12659 }
12660
12661 /* Scan INSN and note any of its operands that need fixing.
12662    If DO_PUSHES is false we do not actually push any of the fixups
12663    needed.  The function returns TRUE if any fixups were needed/pushed.
12664    This is used by arm_memory_load_p() which needs to know about loads
12665    of constants that will be converted into minipool loads.  */
12666 static bool
12667 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
12668 {
12669   bool result = false;
12670   int opno;
12671
12672   extract_insn (insn);
12673
12674   if (!constrain_operands (1))
12675     fatal_insn_not_found (insn);
12676
12677   if (recog_data.n_alternatives == 0)
12678     return false;
12679
12680   /* Fill in recog_op_alt with information about the constraints of
12681      this insn.  */
12682   preprocess_constraints ();
12683
12684   for (opno = 0; opno < recog_data.n_operands; opno++)
12685     {
12686       /* Things we need to fix can only occur in inputs.  */
12687       if (recog_data.operand_type[opno] != OP_IN)
12688         continue;
12689
12690       /* If this alternative is a memory reference, then any mention
12691          of constants in this alternative is really to fool reload
12692          into allowing us to accept one there.  We need to fix them up
12693          now so that we output the right code.  */
12694       if (recog_op_alt[opno][which_alternative].memory_ok)
12695         {
12696           rtx op = recog_data.operand[opno];
12697
12698           if (CONSTANT_P (op))
12699             {
12700               if (do_pushes)
12701                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
12702                                    recog_data.operand_mode[opno], op);
12703               result = true;
12704             }
12705           else if (GET_CODE (op) == MEM
12706                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
12707                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
12708             {
12709               if (do_pushes)
12710                 {
12711                   rtx cop = avoid_constant_pool_reference (op);
12712
12713                   /* Casting the address of something to a mode narrower
12714                      than a word can cause avoid_constant_pool_reference()
12715                      to return the pool reference itself.  That's no good to
12716                      us here.  Lets just hope that we can use the
12717                      constant pool value directly.  */
12718                   if (op == cop)
12719                     cop = get_pool_constant (XEXP (op, 0));
12720
12721                   push_minipool_fix (insn, address,
12722                                      recog_data.operand_loc[opno],
12723                                      recog_data.operand_mode[opno], cop);
12724                 }
12725
12726               result = true;
12727             }
12728         }
12729     }
12730
12731   return result;
12732 }
12733
12734 /* Convert instructions to their cc-clobbering variant if possible, since
12735    that allows us to use smaller encodings.  */
12736
12737 static void
12738 thumb2_reorg (void)
12739 {
12740   basic_block bb;
12741   regset_head live;
12742
12743   INIT_REG_SET (&live);
12744
12745   /* We are freeing block_for_insn in the toplev to keep compatibility
12746      with old MDEP_REORGS that are not CFG based.  Recompute it now.  */
12747   compute_bb_for_insn ();
12748   df_analyze ();
12749
12750   FOR_EACH_BB (bb)
12751     {
12752       rtx insn;
12753
12754       COPY_REG_SET (&live, DF_LR_OUT (bb));
12755       df_simulate_initialize_backwards (bb, &live);
12756       FOR_BB_INSNS_REVERSE (bb, insn)
12757         {
12758           if (NONJUMP_INSN_P (insn)
12759               && !REGNO_REG_SET_P (&live, CC_REGNUM))
12760             {
12761               rtx pat = PATTERN (insn);
12762               if (GET_CODE (pat) == SET
12763                   && low_register_operand (XEXP (pat, 0), SImode)
12764                   && thumb_16bit_operator (XEXP (pat, 1), SImode)
12765                   && low_register_operand (XEXP (XEXP (pat, 1), 0), SImode)
12766                   && low_register_operand (XEXP (XEXP (pat, 1), 1), SImode))
12767                 {
12768                   rtx dst = XEXP (pat, 0);
12769                   rtx src = XEXP (pat, 1);
12770                   rtx op0 = XEXP (src, 0);
12771                   rtx op1 = (GET_RTX_CLASS (GET_CODE (src)) == RTX_COMM_ARITH
12772                              ? XEXP (src, 1) : NULL);
12773
12774                   if (rtx_equal_p (dst, op0)
12775                       || GET_CODE (src) == PLUS || GET_CODE (src) == MINUS)
12776                     {
12777                       rtx ccreg = gen_rtx_REG (CCmode, CC_REGNUM);
12778                       rtx clobber = gen_rtx_CLOBBER (VOIDmode, ccreg);
12779                       rtvec vec = gen_rtvec (2, pat, clobber);
12780
12781                       PATTERN (insn) = gen_rtx_PARALLEL (VOIDmode, vec);
12782                       INSN_CODE (insn) = -1;
12783                     }
12784                   /* We can also handle a commutative operation where the
12785                      second operand matches the destination.  */
12786                   else if (op1 && rtx_equal_p (dst, op1))
12787                     {
12788                       rtx ccreg = gen_rtx_REG (CCmode, CC_REGNUM);
12789                       rtx clobber = gen_rtx_CLOBBER (VOIDmode, ccreg);
12790                       rtvec vec;
12791
12792                       src = copy_rtx (src);
12793                       XEXP (src, 0) = op1;
12794                       XEXP (src, 1) = op0;
12795                       pat = gen_rtx_SET (VOIDmode, dst, src);
12796                       vec = gen_rtvec (2, pat, clobber);
12797                       PATTERN (insn) = gen_rtx_PARALLEL (VOIDmode, vec);
12798                       INSN_CODE (insn) = -1;
12799                     }
12800                 }
12801             }
12802
12803           if (NONDEBUG_INSN_P (insn))
12804             df_simulate_one_insn_backwards (bb, insn, &live);
12805         }
12806     }
12807
12808   CLEAR_REG_SET (&live);
12809 }
12810
12811 /* Gcc puts the pool in the wrong place for ARM, since we can only
12812    load addresses a limited distance around the pc.  We do some
12813    special munging to move the constant pool values to the correct
12814    point in the code.  */
12815 static void
12816 arm_reorg (void)
12817 {
12818   rtx insn;
12819   HOST_WIDE_INT address = 0;
12820   Mfix * fix;
12821
12822   if (TARGET_THUMB2)
12823     thumb2_reorg ();
12824   
12825   minipool_fix_head = minipool_fix_tail = NULL;
12826
12827   /* The first insn must always be a note, or the code below won't
12828      scan it properly.  */
12829   insn = get_insns ();
12830   gcc_assert (GET_CODE (insn) == NOTE);
12831   minipool_pad = 0;
12832
12833   /* Scan all the insns and record the operands that will need fixing.  */
12834   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
12835     {
12836       if (TARGET_CIRRUS_FIX_INVALID_INSNS
12837           && (arm_cirrus_insn_p (insn)
12838               || GET_CODE (insn) == JUMP_INSN
12839               || arm_memory_load_p (insn)))
12840         cirrus_reorg (insn);
12841
12842       if (GET_CODE (insn) == BARRIER)
12843         push_minipool_barrier (insn, address);
12844       else if (INSN_P (insn))
12845         {
12846           rtx table;
12847
12848           note_invalid_constants (insn, address, true);
12849           address += get_attr_length (insn);
12850
12851           /* If the insn is a vector jump, add the size of the table
12852              and skip the table.  */
12853           if ((table = is_jump_table (insn)) != NULL)
12854             {
12855               address += get_jump_table_size (table);
12856               insn = table;
12857             }
12858         }
12859       else if (LABEL_P (insn))
12860         /* Add the worst-case padding due to alignment.  We don't add
12861            the _current_ padding because the minipool insertions
12862            themselves might change it.  */
12863         address += get_label_padding (insn);
12864     }
12865
12866   fix = minipool_fix_head;
12867
12868   /* Now scan the fixups and perform the required changes.  */
12869   while (fix)
12870     {
12871       Mfix * ftmp;
12872       Mfix * fdel;
12873       Mfix *  last_added_fix;
12874       Mfix * last_barrier = NULL;
12875       Mfix * this_fix;
12876
12877       /* Skip any further barriers before the next fix.  */
12878       while (fix && GET_CODE (fix->insn) == BARRIER)
12879         fix = fix->next;
12880
12881       /* No more fixes.  */
12882       if (fix == NULL)
12883         break;
12884
12885       last_added_fix = NULL;
12886
12887       for (ftmp = fix; ftmp; ftmp = ftmp->next)
12888         {
12889           if (GET_CODE (ftmp->insn) == BARRIER)
12890             {
12891               if (ftmp->address >= minipool_vector_head->max_address)
12892                 break;
12893
12894               last_barrier = ftmp;
12895             }
12896           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
12897             break;
12898
12899           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
12900         }
12901
12902       /* If we found a barrier, drop back to that; any fixes that we
12903          could have reached but come after the barrier will now go in
12904          the next mini-pool.  */
12905       if (last_barrier != NULL)
12906         {
12907           /* Reduce the refcount for those fixes that won't go into this
12908              pool after all.  */
12909           for (fdel = last_barrier->next;
12910                fdel && fdel != ftmp;
12911                fdel = fdel->next)
12912             {
12913               fdel->minipool->refcount--;
12914               fdel->minipool = NULL;
12915             }
12916
12917           ftmp = last_barrier;
12918         }
12919       else
12920         {
12921           /* ftmp is first fix that we can't fit into this pool and
12922              there no natural barriers that we could use.  Insert a
12923              new barrier in the code somewhere between the previous
12924              fix and this one, and arrange to jump around it.  */
12925           HOST_WIDE_INT max_address;
12926
12927           /* The last item on the list of fixes must be a barrier, so
12928              we can never run off the end of the list of fixes without
12929              last_barrier being set.  */
12930           gcc_assert (ftmp);
12931
12932           max_address = minipool_vector_head->max_address;
12933           /* Check that there isn't another fix that is in range that
12934              we couldn't fit into this pool because the pool was
12935              already too large: we need to put the pool before such an
12936              instruction.  The pool itself may come just after the
12937              fix because create_fix_barrier also allows space for a
12938              jump instruction.  */
12939           if (ftmp->address < max_address)
12940             max_address = ftmp->address + 1;
12941
12942           last_barrier = create_fix_barrier (last_added_fix, max_address);
12943         }
12944
12945       assign_minipool_offsets (last_barrier);
12946
12947       while (ftmp)
12948         {
12949           if (GET_CODE (ftmp->insn) != BARRIER
12950               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
12951                   == NULL))
12952             break;
12953
12954           ftmp = ftmp->next;
12955         }
12956
12957       /* Scan over the fixes we have identified for this pool, fixing them
12958          up and adding the constants to the pool itself.  */
12959       for (this_fix = fix; this_fix && ftmp != this_fix;
12960            this_fix = this_fix->next)
12961         if (GET_CODE (this_fix->insn) != BARRIER)
12962           {
12963             rtx addr
12964               = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
12965                                                   minipool_vector_label),
12966                                this_fix->minipool->offset);
12967             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
12968           }
12969
12970       dump_minipool (last_barrier->insn);
12971       fix = ftmp;
12972     }
12973
12974   /* From now on we must synthesize any constants that we can't handle
12975      directly.  This can happen if the RTL gets split during final
12976      instruction generation.  */
12977   after_arm_reorg = 1;
12978
12979   /* Free the minipool memory.  */
12980   obstack_free (&minipool_obstack, minipool_startobj);
12981 }
12982 \f
12983 /* Routines to output assembly language.  */
12984
12985 /* If the rtx is the correct value then return the string of the number.
12986    In this way we can ensure that valid double constants are generated even
12987    when cross compiling.  */
12988 const char *
12989 fp_immediate_constant (rtx x)
12990 {
12991   REAL_VALUE_TYPE r;
12992   int i;
12993
12994   if (!fp_consts_inited)
12995     init_fp_table ();
12996
12997   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
12998   for (i = 0; i < 8; i++)
12999     if (REAL_VALUES_EQUAL (r, values_fp[i]))
13000       return strings_fp[i];
13001
13002   gcc_unreachable ();
13003 }
13004
13005 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
13006 static const char *
13007 fp_const_from_val (REAL_VALUE_TYPE *r)
13008 {
13009   int i;
13010
13011   if (!fp_consts_inited)
13012     init_fp_table ();
13013
13014   for (i = 0; i < 8; i++)
13015     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
13016       return strings_fp[i];
13017
13018   gcc_unreachable ();
13019 }
13020
13021 /* Output the operands of a LDM/STM instruction to STREAM.
13022    MASK is the ARM register set mask of which only bits 0-15 are important.
13023    REG is the base register, either the frame pointer or the stack pointer,
13024    INSTR is the possibly suffixed load or store instruction.
13025    RFE is nonzero if the instruction should also copy spsr to cpsr.  */
13026
13027 static void
13028 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
13029                  unsigned long mask, int rfe)
13030 {
13031   unsigned i;
13032   bool not_first = FALSE;
13033
13034   gcc_assert (!rfe || (mask & (1 << PC_REGNUM)));
13035   fputc ('\t', stream);
13036   asm_fprintf (stream, instr, reg);
13037   fputc ('{', stream);
13038
13039   for (i = 0; i <= LAST_ARM_REGNUM; i++)
13040     if (mask & (1 << i))
13041       {
13042         if (not_first)
13043           fprintf (stream, ", ");
13044
13045         asm_fprintf (stream, "%r", i);
13046         not_first = TRUE;
13047       }
13048
13049   if (rfe)
13050     fprintf (stream, "}^\n");
13051   else
13052     fprintf (stream, "}\n");
13053 }
13054
13055
13056 /* Output a FLDMD instruction to STREAM.
13057    BASE if the register containing the address.
13058    REG and COUNT specify the register range.
13059    Extra registers may be added to avoid hardware bugs.
13060
13061    We output FLDMD even for ARMv5 VFP implementations.  Although
13062    FLDMD is technically not supported until ARMv6, it is believed
13063    that all VFP implementations support its use in this context.  */
13064
13065 static void
13066 vfp_output_fldmd (FILE * stream, unsigned int base, int reg, int count)
13067 {
13068   int i;
13069
13070   /* Workaround ARM10 VFPr1 bug.  */
13071   if (count == 2 && !arm_arch6)
13072     {
13073       if (reg == 15)
13074         reg--;
13075       count++;
13076     }
13077
13078   /* FLDMD may not load more than 16 doubleword registers at a time. Split the
13079      load into multiple parts if we have to handle more than 16 registers.  */
13080   if (count > 16)
13081     {
13082       vfp_output_fldmd (stream, base, reg, 16);
13083       vfp_output_fldmd (stream, base, reg + 16, count - 16);
13084       return;
13085     }
13086
13087   fputc ('\t', stream);
13088   asm_fprintf (stream, "fldmfdd\t%r!, {", base);
13089
13090   for (i = reg; i < reg + count; i++)
13091     {
13092       if (i > reg)
13093         fputs (", ", stream);
13094       asm_fprintf (stream, "d%d", i);
13095     }
13096   fputs ("}\n", stream);
13097
13098 }
13099
13100
13101 /* Output the assembly for a store multiple.  */
13102
13103 const char *
13104 vfp_output_fstmd (rtx * operands)
13105 {
13106   char pattern[100];
13107   int p;
13108   int base;
13109   int i;
13110
13111   strcpy (pattern, "fstmfdd%?\t%m0!, {%P1");
13112   p = strlen (pattern);
13113
13114   gcc_assert (GET_CODE (operands[1]) == REG);
13115
13116   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
13117   for (i = 1; i < XVECLEN (operands[2], 0); i++)
13118     {
13119       p += sprintf (&pattern[p], ", d%d", base + i);
13120     }
13121   strcpy (&pattern[p], "}");
13122
13123   output_asm_insn (pattern, operands);
13124   return "";
13125 }
13126
13127
13128 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
13129    number of bytes pushed.  */
13130
13131 static int
13132 vfp_emit_fstmd (int base_reg, int count)
13133 {
13134   rtx par;
13135   rtx dwarf;
13136   rtx tmp, reg;
13137   int i;
13138
13139   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
13140      register pairs are stored by a store multiple insn.  We avoid this
13141      by pushing an extra pair.  */
13142   if (count == 2 && !arm_arch6)
13143     {
13144       if (base_reg == LAST_VFP_REGNUM - 3)
13145         base_reg -= 2;
13146       count++;
13147     }
13148
13149   /* FSTMD may not store more than 16 doubleword registers at once.  Split
13150      larger stores into multiple parts (up to a maximum of two, in
13151      practice).  */
13152   if (count > 16)
13153     {
13154       int saved;
13155       /* NOTE: base_reg is an internal register number, so each D register
13156          counts as 2.  */
13157       saved = vfp_emit_fstmd (base_reg + 32, count - 16);
13158       saved += vfp_emit_fstmd (base_reg, 16);
13159       return saved;
13160     }
13161
13162   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
13163   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
13164
13165   reg = gen_rtx_REG (DFmode, base_reg);
13166   base_reg += 2;
13167
13168   XVECEXP (par, 0, 0)
13169     = gen_rtx_SET (VOIDmode,
13170                    gen_frame_mem
13171                    (BLKmode,
13172                     gen_rtx_PRE_MODIFY (Pmode,
13173                                         stack_pointer_rtx,
13174                                         plus_constant
13175                                         (stack_pointer_rtx,
13176                                          - (count * 8)))
13177                     ),
13178                    gen_rtx_UNSPEC (BLKmode,
13179                                    gen_rtvec (1, reg),
13180                                    UNSPEC_PUSH_MULT));
13181
13182   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
13183                      plus_constant (stack_pointer_rtx, -(count * 8)));
13184   RTX_FRAME_RELATED_P (tmp) = 1;
13185   XVECEXP (dwarf, 0, 0) = tmp;
13186
13187   tmp = gen_rtx_SET (VOIDmode,
13188                      gen_frame_mem (DFmode, stack_pointer_rtx),
13189                      reg);
13190   RTX_FRAME_RELATED_P (tmp) = 1;
13191   XVECEXP (dwarf, 0, 1) = tmp;
13192
13193   for (i = 1; i < count; i++)
13194     {
13195       reg = gen_rtx_REG (DFmode, base_reg);
13196       base_reg += 2;
13197       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
13198
13199       tmp = gen_rtx_SET (VOIDmode,
13200                          gen_frame_mem (DFmode,
13201                                         plus_constant (stack_pointer_rtx,
13202                                                        i * 8)),
13203                          reg);
13204       RTX_FRAME_RELATED_P (tmp) = 1;
13205       XVECEXP (dwarf, 0, i + 1) = tmp;
13206     }
13207
13208   par = emit_insn (par);
13209   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
13210   RTX_FRAME_RELATED_P (par) = 1;
13211
13212   return count * 8;
13213 }
13214
13215 /* Emit a call instruction with pattern PAT.  ADDR is the address of
13216    the call target.  */
13217
13218 void
13219 arm_emit_call_insn (rtx pat, rtx addr)
13220 {
13221   rtx insn;
13222
13223   insn = emit_call_insn (pat);
13224
13225   /* The PIC register is live on entry to VxWorks PIC PLT entries.
13226      If the call might use such an entry, add a use of the PIC register
13227      to the instruction's CALL_INSN_FUNCTION_USAGE.  */
13228   if (TARGET_VXWORKS_RTP
13229       && flag_pic
13230       && GET_CODE (addr) == SYMBOL_REF
13231       && (SYMBOL_REF_DECL (addr)
13232           ? !targetm.binds_local_p (SYMBOL_REF_DECL (addr))
13233           : !SYMBOL_REF_LOCAL_P (addr)))
13234     {
13235       require_pic_register ();
13236       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), cfun->machine->pic_reg);
13237     }
13238 }
13239
13240 /* Output a 'call' insn.  */
13241 const char *
13242 output_call (rtx *operands)
13243 {
13244   gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly.  */
13245
13246   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
13247   if (REGNO (operands[0]) == LR_REGNUM)
13248     {
13249       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
13250       output_asm_insn ("mov%?\t%0, %|lr", operands);
13251     }
13252
13253   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
13254
13255   if (TARGET_INTERWORK || arm_arch4t)
13256     output_asm_insn ("bx%?\t%0", operands);
13257   else
13258     output_asm_insn ("mov%?\t%|pc, %0", operands);
13259
13260   return "";
13261 }
13262
13263 /* Output a 'call' insn that is a reference in memory. This is
13264    disabled for ARMv5 and we prefer a blx instead because otherwise
13265    there's a significant performance overhead.  */
13266 const char *
13267 output_call_mem (rtx *operands)
13268 {
13269   gcc_assert (!arm_arch5);
13270   if (TARGET_INTERWORK)
13271     {
13272       output_asm_insn ("ldr%?\t%|ip, %0", operands);
13273       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
13274       output_asm_insn ("bx%?\t%|ip", operands);
13275     }
13276   else if (regno_use_in (LR_REGNUM, operands[0]))
13277     {
13278       /* LR is used in the memory address.  We load the address in the
13279          first instruction.  It's safe to use IP as the target of the
13280          load since the call will kill it anyway.  */
13281       output_asm_insn ("ldr%?\t%|ip, %0", operands);
13282       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
13283       if (arm_arch4t)
13284         output_asm_insn ("bx%?\t%|ip", operands);
13285       else
13286         output_asm_insn ("mov%?\t%|pc, %|ip", operands);
13287     }
13288   else
13289     {
13290       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
13291       output_asm_insn ("ldr%?\t%|pc, %0", operands);
13292     }
13293
13294   return "";
13295 }
13296
13297
13298 /* Output a move from arm registers to an fpa registers.
13299    OPERANDS[0] is an fpa register.
13300    OPERANDS[1] is the first registers of an arm register pair.  */
13301 const char *
13302 output_mov_long_double_fpa_from_arm (rtx *operands)
13303 {
13304   int arm_reg0 = REGNO (operands[1]);
13305   rtx ops[3];
13306
13307   gcc_assert (arm_reg0 != IP_REGNUM);
13308
13309   ops[0] = gen_rtx_REG (SImode, arm_reg0);
13310   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
13311   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
13312
13313   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
13314   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
13315
13316   return "";
13317 }
13318
13319 /* Output a move from an fpa register to arm registers.
13320    OPERANDS[0] is the first registers of an arm register pair.
13321    OPERANDS[1] is an fpa register.  */
13322 const char *
13323 output_mov_long_double_arm_from_fpa (rtx *operands)
13324 {
13325   int arm_reg0 = REGNO (operands[0]);
13326   rtx ops[3];
13327
13328   gcc_assert (arm_reg0 != IP_REGNUM);
13329
13330   ops[0] = gen_rtx_REG (SImode, arm_reg0);
13331   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
13332   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
13333
13334   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
13335   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
13336   return "";
13337 }
13338
13339 /* Output a move from arm registers to arm registers of a long double
13340    OPERANDS[0] is the destination.
13341    OPERANDS[1] is the source.  */
13342 const char *
13343 output_mov_long_double_arm_from_arm (rtx *operands)
13344 {
13345   /* We have to be careful here because the two might overlap.  */
13346   int dest_start = REGNO (operands[0]);
13347   int src_start = REGNO (operands[1]);
13348   rtx ops[2];
13349   int i;
13350
13351   if (dest_start < src_start)
13352     {
13353       for (i = 0; i < 3; i++)
13354         {
13355           ops[0] = gen_rtx_REG (SImode, dest_start + i);
13356           ops[1] = gen_rtx_REG (SImode, src_start + i);
13357           output_asm_insn ("mov%?\t%0, %1", ops);
13358         }
13359     }
13360   else
13361     {
13362       for (i = 2; i >= 0; i--)
13363         {
13364           ops[0] = gen_rtx_REG (SImode, dest_start + i);
13365           ops[1] = gen_rtx_REG (SImode, src_start + i);
13366           output_asm_insn ("mov%?\t%0, %1", ops);
13367         }
13368     }
13369
13370   return "";
13371 }
13372
13373 void
13374 arm_emit_movpair (rtx dest, rtx src)
13375  {
13376   /* If the src is an immediate, simplify it.  */
13377   if (CONST_INT_P (src))
13378     {
13379       HOST_WIDE_INT val = INTVAL (src);
13380       emit_set_insn (dest, GEN_INT (val & 0x0000ffff));
13381       if ((val >> 16) & 0x0000ffff)
13382         emit_set_insn (gen_rtx_ZERO_EXTRACT (SImode, dest, GEN_INT (16),
13383                                              GEN_INT (16)),
13384                        GEN_INT ((val >> 16) & 0x0000ffff));
13385       return;
13386     }
13387    emit_set_insn (dest, gen_rtx_HIGH (SImode, src));
13388    emit_set_insn (dest, gen_rtx_LO_SUM (SImode, dest, src));
13389  }
13390
13391 /* Output a move from arm registers to an fpa registers.
13392    OPERANDS[0] is an fpa register.
13393    OPERANDS[1] is the first registers of an arm register pair.  */
13394 const char *
13395 output_mov_double_fpa_from_arm (rtx *operands)
13396 {
13397   int arm_reg0 = REGNO (operands[1]);
13398   rtx ops[2];
13399
13400   gcc_assert (arm_reg0 != IP_REGNUM);
13401
13402   ops[0] = gen_rtx_REG (SImode, arm_reg0);
13403   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
13404   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1}", ops);
13405   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
13406   return "";
13407 }
13408
13409 /* Output a move from an fpa register to arm registers.
13410    OPERANDS[0] is the first registers of an arm register pair.
13411    OPERANDS[1] is an fpa register.  */
13412 const char *
13413 output_mov_double_arm_from_fpa (rtx *operands)
13414 {
13415   int arm_reg0 = REGNO (operands[0]);
13416   rtx ops[2];
13417
13418   gcc_assert (arm_reg0 != IP_REGNUM);
13419
13420   ops[0] = gen_rtx_REG (SImode, arm_reg0);
13421   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
13422   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
13423   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1}", ops);
13424   return "";
13425 }
13426
13427 /* Output a move between double words.  It must be REG<-MEM
13428    or MEM<-REG.  */
13429 const char *
13430 output_move_double (rtx *operands, bool emit, int *count)
13431 {
13432   enum rtx_code code0 = GET_CODE (operands[0]);
13433   enum rtx_code code1 = GET_CODE (operands[1]);
13434   rtx otherops[3];
13435   if (count)
13436     *count = 1;
13437
13438   /* The only case when this might happen is when 
13439      you are looking at the length of a DImode instruction
13440      that has an invalid constant in it.  */
13441   if (code0 == REG && code1 != MEM)
13442     {
13443       gcc_assert (!emit);
13444       *count = 2;
13445       return "";
13446     }
13447       
13448
13449   if (code0 == REG)
13450     {
13451       unsigned int reg0 = REGNO (operands[0]);
13452
13453       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
13454
13455       gcc_assert (code1 == MEM);  /* Constraints should ensure this.  */
13456
13457       switch (GET_CODE (XEXP (operands[1], 0)))
13458         {
13459         case REG:
13460
13461           if (emit)
13462             {
13463               if (TARGET_LDRD
13464                   && !(fix_cm3_ldrd && reg0 == REGNO(XEXP (operands[1], 0))))
13465                 output_asm_insn ("ldr%(d%)\t%0, [%m1]", operands);
13466               else
13467                 output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
13468             }
13469           break;
13470
13471         case PRE_INC:
13472           gcc_assert (TARGET_LDRD);
13473           if (emit)
13474             output_asm_insn ("ldr%(d%)\t%0, [%m1, #8]!", operands);
13475           
13476           break;
13477
13478         case PRE_DEC:
13479           if (emit)
13480             {
13481               if (TARGET_LDRD)
13482                 output_asm_insn ("ldr%(d%)\t%0, [%m1, #-8]!", operands);
13483               else
13484                 output_asm_insn ("ldm%(db%)\t%m1!, %M0", operands);
13485             }
13486           break;
13487
13488         case POST_INC:
13489           
13490           if (emit)
13491             {
13492               if (TARGET_LDRD)
13493                 output_asm_insn ("ldr%(d%)\t%0, [%m1], #8", operands);
13494               else
13495                 output_asm_insn ("ldm%(ia%)\t%m1!, %M0", operands);
13496             }
13497           break;
13498
13499         case POST_DEC:
13500           gcc_assert (TARGET_LDRD);
13501           if (emit)
13502             output_asm_insn ("ldr%(d%)\t%0, [%m1], #-8", operands);
13503           break;
13504
13505         case PRE_MODIFY:
13506         case POST_MODIFY:
13507           /* Autoicrement addressing modes should never have overlapping
13508              base and destination registers, and overlapping index registers
13509              are already prohibited, so this doesn't need to worry about
13510              fix_cm3_ldrd.  */
13511           otherops[0] = operands[0];
13512           otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
13513           otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
13514
13515           if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
13516             {
13517               if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
13518                 {
13519                   /* Registers overlap so split out the increment.  */
13520                   if (emit)
13521                     {
13522                       output_asm_insn ("add%?\t%1, %1, %2", otherops);
13523                       output_asm_insn ("ldr%(d%)\t%0, [%1] @split", otherops);
13524                     }
13525                   if (count)
13526                     *count = 2;
13527                 }
13528               else
13529                 {
13530                   /* Use a single insn if we can.
13531                      FIXME: IWMMXT allows offsets larger than ldrd can
13532                      handle, fix these up with a pair of ldr.  */
13533                   if (TARGET_THUMB2
13534                       || GET_CODE (otherops[2]) != CONST_INT
13535                       || (INTVAL (otherops[2]) > -256
13536                           && INTVAL (otherops[2]) < 256))
13537                     {
13538                       if (emit)
13539                         output_asm_insn ("ldr%(d%)\t%0, [%1, %2]!", otherops);
13540                     }
13541                   else
13542                     {
13543                       if (emit)
13544                         {
13545                           output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
13546                           output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
13547                         }
13548                       if (count)
13549                         *count = 2;
13550
13551                     }
13552                 }
13553             }
13554           else
13555             {
13556               /* Use a single insn if we can.
13557                  FIXME: IWMMXT allows offsets larger than ldrd can handle,
13558                  fix these up with a pair of ldr.  */
13559               if (TARGET_THUMB2
13560                   || GET_CODE (otherops[2]) != CONST_INT
13561                   || (INTVAL (otherops[2]) > -256
13562                       && INTVAL (otherops[2]) < 256))
13563                 {
13564                   if (emit)
13565                     output_asm_insn ("ldr%(d%)\t%0, [%1], %2", otherops);
13566                 }
13567               else
13568                 {
13569                   if (emit)
13570                     {
13571                       output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
13572                       output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
13573                     }
13574                   if (count)
13575                     *count = 2;
13576                 }
13577             }
13578           break;
13579
13580         case LABEL_REF:
13581         case CONST:
13582           /* We might be able to use ldrd %0, %1 here.  However the range is
13583              different to ldr/adr, and it is broken on some ARMv7-M
13584              implementations.  */
13585           /* Use the second register of the pair to avoid problematic
13586              overlap.  */
13587           otherops[1] = operands[1];
13588           if (emit)
13589             output_asm_insn ("adr%?\t%0, %1", otherops);
13590           operands[1] = otherops[0];
13591           if (emit)
13592             {
13593               if (TARGET_LDRD)
13594                 output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
13595               else
13596                 output_asm_insn ("ldm%(ia%)\t%1, %M0", operands);
13597             }
13598
13599           if (count)
13600             *count = 2;
13601           break;
13602
13603           /* ??? This needs checking for thumb2.  */
13604         default:
13605           if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
13606                                GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
13607             {
13608               otherops[0] = operands[0];
13609               otherops[1] = XEXP (XEXP (operands[1], 0), 0);
13610               otherops[2] = XEXP (XEXP (operands[1], 0), 1);
13611
13612               if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
13613                 {
13614                   if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
13615                     {
13616                       switch ((int) INTVAL (otherops[2]))
13617                         {
13618                         case -8:
13619                           if (emit)
13620                             output_asm_insn ("ldm%(db%)\t%1, %M0", otherops);
13621                           return "";
13622                         case -4:
13623                           if (TARGET_THUMB2)
13624                             break;
13625                           if (emit)
13626                             output_asm_insn ("ldm%(da%)\t%1, %M0", otherops);
13627                           return "";
13628                         case 4:
13629                           if (TARGET_THUMB2)
13630                             break;
13631                           if (emit)
13632                             output_asm_insn ("ldm%(ib%)\t%1, %M0", otherops);
13633                           return "";
13634                         }
13635                     }
13636                   otherops[0] = gen_rtx_REG(SImode, REGNO(operands[0]) + 1);
13637                   operands[1] = otherops[0];
13638                   if (TARGET_LDRD
13639                       && (GET_CODE (otherops[2]) == REG
13640                           || TARGET_THUMB2
13641                           || (GET_CODE (otherops[2]) == CONST_INT
13642                               && INTVAL (otherops[2]) > -256
13643                               && INTVAL (otherops[2]) < 256)))
13644                     {
13645                       if (reg_overlap_mentioned_p (operands[0],
13646                                                    otherops[2]))
13647                         {
13648                           rtx tmp;
13649                           /* Swap base and index registers over to
13650                              avoid a conflict.  */
13651                           tmp = otherops[1];
13652                           otherops[1] = otherops[2];
13653                           otherops[2] = tmp;
13654                         }
13655                       /* If both registers conflict, it will usually
13656                          have been fixed by a splitter.  */
13657                       if (reg_overlap_mentioned_p (operands[0], otherops[2])
13658                           || (fix_cm3_ldrd && reg0 == REGNO (otherops[1])))
13659                         {
13660                           if (emit)
13661                             {
13662                               output_asm_insn ("add%?\t%0, %1, %2", otherops);
13663                               output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
13664                             }
13665                           if (count)
13666                             *count = 2;
13667                         }
13668                       else
13669                         {
13670                           otherops[0] = operands[0];
13671                           if (emit)
13672                             output_asm_insn ("ldr%(d%)\t%0, [%1, %2]", otherops);
13673                         }
13674                       return "";
13675                     }
13676
13677                   if (GET_CODE (otherops[2]) == CONST_INT)
13678                     {                                                   
13679                       if (emit)
13680                         {
13681                           if (!(const_ok_for_arm (INTVAL (otherops[2]))))
13682                             output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
13683                           else
13684                             output_asm_insn ("add%?\t%0, %1, %2", otherops);
13685                         }
13686                         
13687                     }
13688                   else
13689                     {
13690                       if (emit)
13691                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
13692                     }
13693                 }
13694               else
13695                 {
13696                   if (emit)
13697                     output_asm_insn ("sub%?\t%0, %1, %2", otherops);
13698                 }
13699
13700               if (TARGET_LDRD)
13701                 return "ldr%(d%)\t%0, [%1]";
13702               
13703                 return "ldm%(ia%)\t%1, %M0";
13704             }
13705           else
13706             {
13707               otherops[1] = adjust_address (operands[1], SImode, 4);
13708               /* Take care of overlapping base/data reg.  */
13709               if (reg_mentioned_p (operands[0], operands[1]))
13710                 {
13711                   if (emit)
13712                     {
13713                       output_asm_insn ("ldr%?\t%0, %1", otherops);
13714                       output_asm_insn ("ldr%?\t%0, %1", operands);
13715                     }
13716                   if (count)
13717                     *count = 2;
13718
13719                 }
13720               else
13721                 {
13722                   if (emit)
13723                     {
13724                       output_asm_insn ("ldr%?\t%0, %1", operands);
13725                       output_asm_insn ("ldr%?\t%0, %1", otherops);
13726                     }
13727                   if (count)
13728                     *count = 2;
13729                 }
13730             }
13731         }
13732     }
13733   else
13734     {
13735       /* Constraints should ensure this.  */
13736       gcc_assert (code0 == MEM && code1 == REG);
13737       gcc_assert (REGNO (operands[1]) != IP_REGNUM);
13738
13739       switch (GET_CODE (XEXP (operands[0], 0)))
13740         {
13741         case REG:
13742           if (emit)
13743             {
13744               if (TARGET_LDRD)
13745                 output_asm_insn ("str%(d%)\t%1, [%m0]", operands);
13746               else
13747                 output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
13748             }
13749           break;
13750
13751         case PRE_INC:
13752           gcc_assert (TARGET_LDRD);
13753           if (emit)
13754             output_asm_insn ("str%(d%)\t%1, [%m0, #8]!", operands);
13755           break;
13756
13757         case PRE_DEC:
13758           if (emit)
13759             {
13760               if (TARGET_LDRD)
13761                 output_asm_insn ("str%(d%)\t%1, [%m0, #-8]!", operands);
13762               else
13763                 output_asm_insn ("stm%(db%)\t%m0!, %M1", operands);
13764             }
13765           break;
13766
13767         case POST_INC:
13768           if (emit)
13769             {
13770               if (TARGET_LDRD)
13771                 output_asm_insn ("str%(d%)\t%1, [%m0], #8", operands);
13772               else
13773                 output_asm_insn ("stm%(ia%)\t%m0!, %M1", operands);
13774             }
13775           break;
13776
13777         case POST_DEC:
13778           gcc_assert (TARGET_LDRD);
13779           if (emit)
13780             output_asm_insn ("str%(d%)\t%1, [%m0], #-8", operands);
13781           break;
13782
13783         case PRE_MODIFY:
13784         case POST_MODIFY:
13785           otherops[0] = operands[1];
13786           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
13787           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
13788
13789           /* IWMMXT allows offsets larger than ldrd can handle,
13790              fix these up with a pair of ldr.  */
13791           if (!TARGET_THUMB2
13792               && GET_CODE (otherops[2]) == CONST_INT
13793               && (INTVAL(otherops[2]) <= -256
13794                   || INTVAL(otherops[2]) >= 256))
13795             {
13796               if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
13797                 {
13798                   if (emit)
13799                     {
13800                       output_asm_insn ("str%?\t%0, [%1, %2]!", otherops);
13801                       output_asm_insn ("str%?\t%H0, [%1, #4]", otherops);
13802                     }
13803                   if (count)
13804                     *count = 2;
13805                 }
13806               else
13807                 {
13808                   if (emit)
13809                     {
13810                       output_asm_insn ("str%?\t%H0, [%1, #4]", otherops);
13811                       output_asm_insn ("str%?\t%0, [%1], %2", otherops);
13812                     }
13813                   if (count)
13814                     *count = 2;
13815                 }
13816             }
13817           else if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
13818             {
13819               if (emit)
13820                 output_asm_insn ("str%(d%)\t%0, [%1, %2]!", otherops);
13821             }
13822           else
13823             {
13824               if (emit)
13825                 output_asm_insn ("str%(d%)\t%0, [%1], %2", otherops);
13826             }
13827           break;
13828
13829         case PLUS:
13830           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
13831           if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
13832             {
13833               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
13834                 {
13835                 case -8:
13836                   if (emit)
13837                     output_asm_insn ("stm%(db%)\t%m0, %M1", operands);
13838                   return "";
13839
13840                 case -4:
13841                   if (TARGET_THUMB2)
13842                     break;
13843                   if (emit)
13844                     output_asm_insn ("stm%(da%)\t%m0, %M1", operands);
13845                   return "";
13846
13847                 case 4:
13848                   if (TARGET_THUMB2)
13849                     break;
13850                   if (emit)
13851                     output_asm_insn ("stm%(ib%)\t%m0, %M1", operands);
13852                   return "";
13853                 }
13854             }
13855           if (TARGET_LDRD
13856               && (GET_CODE (otherops[2]) == REG
13857                   || TARGET_THUMB2
13858                   || (GET_CODE (otherops[2]) == CONST_INT
13859                       && INTVAL (otherops[2]) > -256
13860                       && INTVAL (otherops[2]) < 256)))
13861             {
13862               otherops[0] = operands[1];
13863               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
13864               if (emit)
13865                 output_asm_insn ("str%(d%)\t%0, [%1, %2]", otherops);
13866               return "";
13867             }
13868           /* Fall through */
13869
13870         default:
13871           otherops[0] = adjust_address (operands[0], SImode, 4);
13872           otherops[1] = operands[1];
13873           if (emit)
13874             {
13875               output_asm_insn ("str%?\t%1, %0", operands);
13876               output_asm_insn ("str%?\t%H1, %0", otherops);
13877             }
13878           if (count)
13879             *count = 2;
13880
13881         }
13882     }
13883
13884   return "";
13885 }
13886
13887 /* Output a move, load or store for quad-word vectors in ARM registers.  Only
13888    handles MEMs accepted by neon_vector_mem_operand with TYPE=1.  */
13889
13890 const char *
13891 output_move_quad (rtx *operands)
13892 {
13893   if (REG_P (operands[0]))
13894     {
13895       /* Load, or reg->reg move.  */
13896
13897       if (MEM_P (operands[1]))
13898         {
13899           switch (GET_CODE (XEXP (operands[1], 0)))
13900             {
13901             case REG:
13902               output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
13903               break;
13904
13905             case LABEL_REF:
13906             case CONST:
13907               output_asm_insn ("adr%?\t%0, %1", operands);
13908               output_asm_insn ("ldm%(ia%)\t%0, %M0", operands);
13909               break;
13910
13911             default:
13912               gcc_unreachable ();
13913             }
13914         }
13915       else
13916         {
13917           rtx ops[2];
13918           int dest, src, i;
13919
13920           gcc_assert (REG_P (operands[1]));
13921
13922           dest = REGNO (operands[0]);
13923           src = REGNO (operands[1]);
13924
13925           /* This seems pretty dumb, but hopefully GCC won't try to do it
13926              very often.  */
13927           if (dest < src)
13928             for (i = 0; i < 4; i++)
13929               {
13930                 ops[0] = gen_rtx_REG (SImode, dest + i);
13931                 ops[1] = gen_rtx_REG (SImode, src + i);
13932                 output_asm_insn ("mov%?\t%0, %1", ops);
13933               }
13934           else
13935             for (i = 3; i >= 0; i--)
13936               {
13937                 ops[0] = gen_rtx_REG (SImode, dest + i);
13938                 ops[1] = gen_rtx_REG (SImode, src + i);
13939                 output_asm_insn ("mov%?\t%0, %1", ops);
13940               }
13941         }
13942     }
13943   else
13944     {
13945       gcc_assert (MEM_P (operands[0]));
13946       gcc_assert (REG_P (operands[1]));
13947       gcc_assert (!reg_overlap_mentioned_p (operands[1], operands[0]));
13948
13949       switch (GET_CODE (XEXP (operands[0], 0)))
13950         {
13951         case REG:
13952           output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
13953           break;
13954
13955         default:
13956           gcc_unreachable ();
13957         }
13958     }
13959
13960   return "";
13961 }
13962
13963 /* Output a VFP load or store instruction.  */
13964
13965 const char *
13966 output_move_vfp (rtx *operands)
13967 {
13968   rtx reg, mem, addr, ops[2];
13969   int load = REG_P (operands[0]);
13970   int dp = GET_MODE_SIZE (GET_MODE (operands[0])) == 8;
13971   int integer_p = GET_MODE_CLASS (GET_MODE (operands[0])) == MODE_INT;
13972   const char *templ;
13973   char buff[50];
13974   enum machine_mode mode;
13975
13976   reg = operands[!load];
13977   mem = operands[load];
13978
13979   mode = GET_MODE (reg);
13980
13981   gcc_assert (REG_P (reg));
13982   gcc_assert (IS_VFP_REGNUM (REGNO (reg)));
13983   gcc_assert (mode == SFmode
13984               || mode == DFmode
13985               || mode == SImode
13986               || mode == DImode
13987               || (TARGET_NEON && VALID_NEON_DREG_MODE (mode)));
13988   gcc_assert (MEM_P (mem));
13989
13990   addr = XEXP (mem, 0);
13991
13992   switch (GET_CODE (addr))
13993     {
13994     case PRE_DEC:
13995       templ = "f%smdb%c%%?\t%%0!, {%%%s1}%s";
13996       ops[0] = XEXP (addr, 0);
13997       ops[1] = reg;
13998       break;
13999
14000     case POST_INC:
14001       templ = "f%smia%c%%?\t%%0!, {%%%s1}%s";
14002       ops[0] = XEXP (addr, 0);
14003       ops[1] = reg;
14004       break;
14005
14006     default:
14007       templ = "f%s%c%%?\t%%%s0, %%1%s";
14008       ops[0] = reg;
14009       ops[1] = mem;
14010       break;
14011     }
14012
14013   sprintf (buff, templ,
14014            load ? "ld" : "st",
14015            dp ? 'd' : 's',
14016            dp ? "P" : "",
14017            integer_p ? "\t%@ int" : "");
14018   output_asm_insn (buff, ops);
14019
14020   return "";
14021 }
14022
14023 /* Output a Neon quad-word load or store, or a load or store for
14024    larger structure modes.
14025
14026    WARNING: The ordering of elements is weird in big-endian mode,
14027    because we use VSTM, as required by the EABI.  GCC RTL defines
14028    element ordering based on in-memory order.  This can be differ
14029    from the architectural ordering of elements within a NEON register.
14030    The intrinsics defined in arm_neon.h use the NEON register element
14031    ordering, not the GCC RTL element ordering.
14032
14033    For example, the in-memory ordering of a big-endian a quadword
14034    vector with 16-bit elements when stored from register pair {d0,d1}
14035    will be (lowest address first, d0[N] is NEON register element N):
14036
14037      [d0[3], d0[2], d0[1], d0[0], d1[7], d1[6], d1[5], d1[4]]
14038
14039    When necessary, quadword registers (dN, dN+1) are moved to ARM
14040    registers from rN in the order:
14041
14042      dN -> (rN+1, rN), dN+1 -> (rN+3, rN+2)
14043
14044    So that STM/LDM can be used on vectors in ARM registers, and the
14045    same memory layout will result as if VSTM/VLDM were used.  */
14046
14047 const char *
14048 output_move_neon (rtx *operands)
14049 {
14050   rtx reg, mem, addr, ops[2];
14051   int regno, load = REG_P (operands[0]);
14052   const char *templ;
14053   char buff[50];
14054   enum machine_mode mode;
14055
14056   reg = operands[!load];
14057   mem = operands[load];
14058
14059   mode = GET_MODE (reg);
14060
14061   gcc_assert (REG_P (reg));
14062   regno = REGNO (reg);
14063   gcc_assert (VFP_REGNO_OK_FOR_DOUBLE (regno)
14064               || NEON_REGNO_OK_FOR_QUAD (regno));
14065   gcc_assert (VALID_NEON_DREG_MODE (mode)
14066               || VALID_NEON_QREG_MODE (mode)
14067               || VALID_NEON_STRUCT_MODE (mode));
14068   gcc_assert (MEM_P (mem));
14069
14070   addr = XEXP (mem, 0);
14071
14072   /* Strip off const from addresses like (const (plus (...))).  */
14073   if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
14074     addr = XEXP (addr, 0);
14075
14076   switch (GET_CODE (addr))
14077     {
14078     case POST_INC:
14079       templ = "v%smia%%?\t%%0!, %%h1";
14080       ops[0] = XEXP (addr, 0);
14081       ops[1] = reg;
14082       break;
14083
14084     case PRE_DEC:
14085       /* FIXME: We should be using vld1/vst1 here in BE mode?  */
14086       templ = "v%smdb%%?\t%%0!, %%h1";
14087       ops[0] = XEXP (addr, 0);
14088       ops[1] = reg;
14089       break;
14090     
14091     case POST_MODIFY:
14092       /* FIXME: Not currently enabled in neon_vector_mem_operand.  */
14093       gcc_unreachable ();
14094
14095     case LABEL_REF:
14096     case PLUS:
14097       {
14098         int nregs = HARD_REGNO_NREGS (REGNO (reg), mode) / 2;
14099         int i;
14100         int overlap = -1;
14101         for (i = 0; i < nregs; i++)
14102           {
14103             /* We're only using DImode here because it's a convenient size.  */
14104             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * i);
14105             ops[1] = adjust_address (mem, DImode, 8 * i);
14106             if (reg_overlap_mentioned_p (ops[0], mem))
14107               {
14108                 gcc_assert (overlap == -1);
14109                 overlap = i;
14110               }
14111             else
14112               {
14113                 sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
14114                 output_asm_insn (buff, ops);
14115               }
14116           }
14117         if (overlap != -1)
14118           {
14119             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * overlap);
14120             ops[1] = adjust_address (mem, SImode, 8 * overlap);
14121             sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
14122             output_asm_insn (buff, ops);
14123           }
14124
14125         return "";
14126       }
14127
14128     default:
14129       templ = "v%smia%%?\t%%m0, %%h1";
14130       ops[0] = mem;
14131       ops[1] = reg;
14132     }
14133
14134   sprintf (buff, templ, load ? "ld" : "st");
14135   output_asm_insn (buff, ops);
14136
14137   return "";
14138 }
14139
14140 /* Compute and return the length of neon_mov<mode>, where <mode> is
14141    one of VSTRUCT modes: EI, OI, CI or XI.  */
14142 int
14143 arm_attr_length_move_neon (rtx insn)
14144 {
14145   rtx reg, mem, addr;
14146   int load;
14147   enum machine_mode mode;
14148
14149   extract_insn_cached (insn);
14150
14151   if (REG_P (recog_data.operand[0]) && REG_P (recog_data.operand[1]))
14152     {
14153       mode = GET_MODE (recog_data.operand[0]);
14154       switch (mode)
14155         {
14156         case EImode:
14157         case OImode:
14158           return 8;
14159         case CImode:
14160           return 12;
14161         case XImode:
14162           return 16;
14163         default:
14164           gcc_unreachable ();
14165         }
14166     }
14167
14168   load = REG_P (recog_data.operand[0]);
14169   reg = recog_data.operand[!load];
14170   mem = recog_data.operand[load];
14171
14172   gcc_assert (MEM_P (mem));
14173
14174   mode = GET_MODE (reg);
14175   addr = XEXP (mem, 0);
14176
14177   /* Strip off const from addresses like (const (plus (...))).  */
14178   if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
14179     addr = XEXP (addr, 0);
14180
14181   if (GET_CODE (addr) == LABEL_REF || GET_CODE (addr) == PLUS)
14182     {
14183       int insns = HARD_REGNO_NREGS (REGNO (reg), mode) / 2;
14184       return insns * 4;
14185     }
14186   else
14187     return 4;
14188 }
14189
14190 /* Return nonzero if the offset in the address is an immediate.  Otherwise,
14191    return zero.  */
14192
14193 int
14194 arm_address_offset_is_imm (rtx insn)
14195 {
14196   rtx mem, addr;
14197
14198   extract_insn_cached (insn);
14199
14200   if (REG_P (recog_data.operand[0]))
14201     return 0;
14202
14203   mem = recog_data.operand[0];
14204
14205   gcc_assert (MEM_P (mem));
14206
14207   addr = XEXP (mem, 0);
14208
14209   if (GET_CODE (addr) == REG
14210       || (GET_CODE (addr) == PLUS
14211           && GET_CODE (XEXP (addr, 0)) == REG
14212           && GET_CODE (XEXP (addr, 1)) == CONST_INT))
14213     return 1;
14214   else
14215     return 0;
14216 }
14217
14218 /* Output an ADD r, s, #n where n may be too big for one instruction.
14219    If adding zero to one register, output nothing.  */
14220 const char *
14221 output_add_immediate (rtx *operands)
14222 {
14223   HOST_WIDE_INT n = INTVAL (operands[2]);
14224
14225   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
14226     {
14227       if (n < 0)
14228         output_multi_immediate (operands,
14229                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
14230                                 -n);
14231       else
14232         output_multi_immediate (operands,
14233                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
14234                                 n);
14235     }
14236
14237   return "";
14238 }
14239
14240 /* Output a multiple immediate operation.
14241    OPERANDS is the vector of operands referred to in the output patterns.
14242    INSTR1 is the output pattern to use for the first constant.
14243    INSTR2 is the output pattern to use for subsequent constants.
14244    IMMED_OP is the index of the constant slot in OPERANDS.
14245    N is the constant value.  */
14246 static const char *
14247 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
14248                         int immed_op, HOST_WIDE_INT n)
14249 {
14250 #if HOST_BITS_PER_WIDE_INT > 32
14251   n &= 0xffffffff;
14252 #endif
14253
14254   if (n == 0)
14255     {
14256       /* Quick and easy output.  */
14257       operands[immed_op] = const0_rtx;
14258       output_asm_insn (instr1, operands);
14259     }
14260   else
14261     {
14262       int i;
14263       const char * instr = instr1;
14264
14265       /* Note that n is never zero here (which would give no output).  */
14266       for (i = 0; i < 32; i += 2)
14267         {
14268           if (n & (3 << i))
14269             {
14270               operands[immed_op] = GEN_INT (n & (255 << i));
14271               output_asm_insn (instr, operands);
14272               instr = instr2;
14273               i += 6;
14274             }
14275         }
14276     }
14277
14278   return "";
14279 }
14280
14281 /* Return the name of a shifter operation.  */
14282 static const char *
14283 arm_shift_nmem(enum rtx_code code)
14284 {
14285   switch (code)
14286     {
14287     case ASHIFT:
14288       return ARM_LSL_NAME;
14289
14290     case ASHIFTRT:
14291       return "asr";
14292
14293     case LSHIFTRT:
14294       return "lsr";
14295
14296     case ROTATERT:
14297       return "ror";
14298
14299     default:
14300       abort();
14301     }
14302 }
14303
14304 /* Return the appropriate ARM instruction for the operation code.
14305    The returned result should not be overwritten.  OP is the rtx of the
14306    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
14307    was shifted.  */
14308 const char *
14309 arithmetic_instr (rtx op, int shift_first_arg)
14310 {
14311   switch (GET_CODE (op))
14312     {
14313     case PLUS:
14314       return "add";
14315
14316     case MINUS:
14317       return shift_first_arg ? "rsb" : "sub";
14318
14319     case IOR:
14320       return "orr";
14321
14322     case XOR:
14323       return "eor";
14324
14325     case AND:
14326       return "and";
14327
14328     case ASHIFT:
14329     case ASHIFTRT:
14330     case LSHIFTRT:
14331     case ROTATERT:
14332       return arm_shift_nmem(GET_CODE(op));
14333
14334     default:
14335       gcc_unreachable ();
14336     }
14337 }
14338
14339 /* Ensure valid constant shifts and return the appropriate shift mnemonic
14340    for the operation code.  The returned result should not be overwritten.
14341    OP is the rtx code of the shift.
14342    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
14343    shift.  */
14344 static const char *
14345 shift_op (rtx op, HOST_WIDE_INT *amountp)
14346 {
14347   const char * mnem;
14348   enum rtx_code code = GET_CODE (op);
14349
14350   switch (GET_CODE (XEXP (op, 1)))
14351     {
14352     case REG:
14353     case SUBREG:
14354       *amountp = -1;
14355       break;
14356
14357     case CONST_INT:
14358       *amountp = INTVAL (XEXP (op, 1));
14359       break;
14360
14361     default:
14362       gcc_unreachable ();
14363     }
14364
14365   switch (code)
14366     {
14367     case ROTATE:
14368       gcc_assert (*amountp != -1);
14369       *amountp = 32 - *amountp;
14370       code = ROTATERT;
14371
14372       /* Fall through.  */
14373
14374     case ASHIFT:
14375     case ASHIFTRT:
14376     case LSHIFTRT:
14377     case ROTATERT:
14378       mnem = arm_shift_nmem(code);
14379       break;
14380
14381     case MULT:
14382       /* We never have to worry about the amount being other than a
14383          power of 2, since this case can never be reloaded from a reg.  */
14384       gcc_assert (*amountp != -1);
14385       *amountp = int_log2 (*amountp);
14386       return ARM_LSL_NAME;
14387
14388     default:
14389       gcc_unreachable ();
14390     }
14391
14392   if (*amountp != -1)
14393     {
14394       /* This is not 100% correct, but follows from the desire to merge
14395          multiplication by a power of 2 with the recognizer for a
14396          shift.  >=32 is not a valid shift for "lsl", so we must try and
14397          output a shift that produces the correct arithmetical result.
14398          Using lsr #32 is identical except for the fact that the carry bit
14399          is not set correctly if we set the flags; but we never use the
14400          carry bit from such an operation, so we can ignore that.  */
14401       if (code == ROTATERT)
14402         /* Rotate is just modulo 32.  */
14403         *amountp &= 31;
14404       else if (*amountp != (*amountp & 31))
14405         {
14406           if (code == ASHIFT)
14407             mnem = "lsr";
14408           *amountp = 32;
14409         }
14410
14411       /* Shifts of 0 are no-ops.  */
14412       if (*amountp == 0)
14413         return NULL;
14414     }
14415
14416   return mnem;
14417 }
14418
14419 /* Obtain the shift from the POWER of two.  */
14420
14421 static HOST_WIDE_INT
14422 int_log2 (HOST_WIDE_INT power)
14423 {
14424   HOST_WIDE_INT shift = 0;
14425
14426   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
14427     {
14428       gcc_assert (shift <= 31);
14429       shift++;
14430     }
14431
14432   return shift;
14433 }
14434
14435 /* Output a .ascii pseudo-op, keeping track of lengths.  This is
14436    because /bin/as is horribly restrictive.  The judgement about
14437    whether or not each character is 'printable' (and can be output as
14438    is) or not (and must be printed with an octal escape) must be made
14439    with reference to the *host* character set -- the situation is
14440    similar to that discussed in the comments above pp_c_char in
14441    c-pretty-print.c.  */
14442
14443 #define MAX_ASCII_LEN 51
14444
14445 void
14446 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
14447 {
14448   int i;
14449   int len_so_far = 0;
14450
14451   fputs ("\t.ascii\t\"", stream);
14452
14453   for (i = 0; i < len; i++)
14454     {
14455       int c = p[i];
14456
14457       if (len_so_far >= MAX_ASCII_LEN)
14458         {
14459           fputs ("\"\n\t.ascii\t\"", stream);
14460           len_so_far = 0;
14461         }
14462
14463       if (ISPRINT (c))
14464         {
14465           if (c == '\\' || c == '\"')
14466             {
14467               putc ('\\', stream);
14468               len_so_far++;
14469             }
14470           putc (c, stream);
14471           len_so_far++;
14472         }
14473       else
14474         {
14475           fprintf (stream, "\\%03o", c);
14476           len_so_far += 4;
14477         }
14478     }
14479
14480   fputs ("\"\n", stream);
14481 }
14482 \f
14483 /* Compute the register save mask for registers 0 through 12
14484    inclusive.  This code is used by arm_compute_save_reg_mask.  */
14485
14486 static unsigned long
14487 arm_compute_save_reg0_reg12_mask (void)
14488 {
14489   unsigned long func_type = arm_current_func_type ();
14490   unsigned long save_reg_mask = 0;
14491   unsigned int reg;
14492
14493   if (IS_INTERRUPT (func_type))
14494     {
14495       unsigned int max_reg;
14496       /* Interrupt functions must not corrupt any registers,
14497          even call clobbered ones.  If this is a leaf function
14498          we can just examine the registers used by the RTL, but
14499          otherwise we have to assume that whatever function is
14500          called might clobber anything, and so we have to save
14501          all the call-clobbered registers as well.  */
14502       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
14503         /* FIQ handlers have registers r8 - r12 banked, so
14504            we only need to check r0 - r7, Normal ISRs only
14505            bank r14 and r15, so we must check up to r12.
14506            r13 is the stack pointer which is always preserved,
14507            so we do not need to consider it here.  */
14508         max_reg = 7;
14509       else
14510         max_reg = 12;
14511
14512       for (reg = 0; reg <= max_reg; reg++)
14513         if (df_regs_ever_live_p (reg)
14514             || (! current_function_is_leaf && call_used_regs[reg]))
14515           save_reg_mask |= (1 << reg);
14516
14517       /* Also save the pic base register if necessary.  */
14518       if (flag_pic
14519           && !TARGET_SINGLE_PIC_BASE
14520           && arm_pic_register != INVALID_REGNUM
14521           && crtl->uses_pic_offset_table)
14522         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
14523     }
14524   else if (IS_VOLATILE(func_type))
14525     {
14526       /* For noreturn functions we historically omitted register saves
14527          altogether.  However this really messes up debugging.  As a
14528          compromise save just the frame pointers.  Combined with the link
14529          register saved elsewhere this should be sufficient to get
14530          a backtrace.  */
14531       if (frame_pointer_needed)
14532         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
14533       if (df_regs_ever_live_p (ARM_HARD_FRAME_POINTER_REGNUM))
14534         save_reg_mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
14535       if (df_regs_ever_live_p (THUMB_HARD_FRAME_POINTER_REGNUM))
14536         save_reg_mask |= 1 << THUMB_HARD_FRAME_POINTER_REGNUM;
14537     }
14538   else
14539     {
14540       /* In the normal case we only need to save those registers
14541          which are call saved and which are used by this function.  */
14542       for (reg = 0; reg <= 11; reg++)
14543         if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
14544           save_reg_mask |= (1 << reg);
14545
14546       /* Handle the frame pointer as a special case.  */
14547       if (frame_pointer_needed)
14548         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
14549
14550       /* If we aren't loading the PIC register,
14551          don't stack it even though it may be live.  */
14552       if (flag_pic
14553           && !TARGET_SINGLE_PIC_BASE
14554           && arm_pic_register != INVALID_REGNUM
14555           && (df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM)
14556               || crtl->uses_pic_offset_table))
14557         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
14558
14559       /* The prologue will copy SP into R0, so save it.  */
14560       if (IS_STACKALIGN (func_type))
14561         save_reg_mask |= 1;
14562     }
14563
14564   /* Save registers so the exception handler can modify them.  */
14565   if (crtl->calls_eh_return)
14566     {
14567       unsigned int i;
14568
14569       for (i = 0; ; i++)
14570         {
14571           reg = EH_RETURN_DATA_REGNO (i);
14572           if (reg == INVALID_REGNUM)
14573             break;
14574           save_reg_mask |= 1 << reg;
14575         }
14576     }
14577
14578   return save_reg_mask;
14579 }
14580
14581
14582 /* Compute the number of bytes used to store the static chain register on the 
14583    stack, above the stack frame. We need to know this accurately to get the
14584    alignment of the rest of the stack frame correct. */
14585
14586 static int arm_compute_static_chain_stack_bytes (void)
14587 {
14588   unsigned long func_type = arm_current_func_type ();
14589   int static_chain_stack_bytes = 0;
14590
14591   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM &&
14592       IS_NESTED (func_type) &&
14593       df_regs_ever_live_p (3) && crtl->args.pretend_args_size == 0)
14594     static_chain_stack_bytes = 4;
14595
14596   return static_chain_stack_bytes;
14597 }
14598
14599
14600 /* Compute a bit mask of which registers need to be
14601    saved on the stack for the current function.
14602    This is used by arm_get_frame_offsets, which may add extra registers.  */
14603
14604 static unsigned long
14605 arm_compute_save_reg_mask (void)
14606 {
14607   unsigned int save_reg_mask = 0;
14608   unsigned long func_type = arm_current_func_type ();
14609   unsigned int reg;
14610
14611   if (IS_NAKED (func_type))
14612     /* This should never really happen.  */
14613     return 0;
14614
14615   /* If we are creating a stack frame, then we must save the frame pointer,
14616      IP (which will hold the old stack pointer), LR and the PC.  */
14617   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
14618     save_reg_mask |=
14619       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
14620       | (1 << IP_REGNUM)
14621       | (1 << LR_REGNUM)
14622       | (1 << PC_REGNUM);
14623
14624   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
14625
14626   /* Decide if we need to save the link register.
14627      Interrupt routines have their own banked link register,
14628      so they never need to save it.
14629      Otherwise if we do not use the link register we do not need to save
14630      it.  If we are pushing other registers onto the stack however, we
14631      can save an instruction in the epilogue by pushing the link register
14632      now and then popping it back into the PC.  This incurs extra memory
14633      accesses though, so we only do it when optimizing for size, and only
14634      if we know that we will not need a fancy return sequence.  */
14635   if (df_regs_ever_live_p (LR_REGNUM)
14636       || (save_reg_mask
14637           && optimize_size
14638           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
14639           && !crtl->calls_eh_return))
14640     save_reg_mask |= 1 << LR_REGNUM;
14641
14642   if (cfun->machine->lr_save_eliminated)
14643     save_reg_mask &= ~ (1 << LR_REGNUM);
14644
14645   if (TARGET_REALLY_IWMMXT
14646       && ((bit_count (save_reg_mask)
14647            + ARM_NUM_INTS (crtl->args.pretend_args_size +
14648                            arm_compute_static_chain_stack_bytes())
14649            ) % 2) != 0)
14650     {
14651       /* The total number of registers that are going to be pushed
14652          onto the stack is odd.  We need to ensure that the stack
14653          is 64-bit aligned before we start to save iWMMXt registers,
14654          and also before we start to create locals.  (A local variable
14655          might be a double or long long which we will load/store using
14656          an iWMMXt instruction).  Therefore we need to push another
14657          ARM register, so that the stack will be 64-bit aligned.  We
14658          try to avoid using the arg registers (r0 -r3) as they might be
14659          used to pass values in a tail call.  */
14660       for (reg = 4; reg <= 12; reg++)
14661         if ((save_reg_mask & (1 << reg)) == 0)
14662           break;
14663
14664       if (reg <= 12)
14665         save_reg_mask |= (1 << reg);
14666       else
14667         {
14668           cfun->machine->sibcall_blocked = 1;
14669           save_reg_mask |= (1 << 3);
14670         }
14671     }
14672
14673   /* We may need to push an additional register for use initializing the
14674      PIC base register.  */
14675   if (TARGET_THUMB2 && IS_NESTED (func_type) && flag_pic
14676       && (save_reg_mask & THUMB2_WORK_REGS) == 0)
14677     {
14678       reg = thumb_find_work_register (1 << 4);
14679       if (!call_used_regs[reg])
14680         save_reg_mask |= (1 << reg);
14681     }
14682
14683   return save_reg_mask;
14684 }
14685
14686
14687 /* Compute a bit mask of which registers need to be
14688    saved on the stack for the current function.  */
14689 static unsigned long
14690 thumb1_compute_save_reg_mask (void)
14691 {
14692   unsigned long mask;
14693   unsigned reg;
14694
14695   mask = 0;
14696   for (reg = 0; reg < 12; reg ++)
14697     if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14698       mask |= 1 << reg;
14699
14700   if (flag_pic
14701       && !TARGET_SINGLE_PIC_BASE
14702       && arm_pic_register != INVALID_REGNUM
14703       && crtl->uses_pic_offset_table)
14704     mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
14705
14706   /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
14707   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
14708     mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
14709
14710   /* LR will also be pushed if any lo regs are pushed.  */
14711   if (mask & 0xff || thumb_force_lr_save ())
14712     mask |= (1 << LR_REGNUM);
14713
14714   /* Make sure we have a low work register if we need one.
14715      We will need one if we are going to push a high register,
14716      but we are not currently intending to push a low register.  */
14717   if ((mask & 0xff) == 0
14718       && ((mask & 0x0f00) || TARGET_BACKTRACE))
14719     {
14720       /* Use thumb_find_work_register to choose which register
14721          we will use.  If the register is live then we will
14722          have to push it.  Use LAST_LO_REGNUM as our fallback
14723          choice for the register to select.  */
14724       reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
14725       /* Make sure the register returned by thumb_find_work_register is
14726          not part of the return value.  */
14727       if (reg * UNITS_PER_WORD <= (unsigned) arm_size_return_regs ())
14728         reg = LAST_LO_REGNUM;
14729
14730       if (! call_used_regs[reg])
14731         mask |= 1 << reg;
14732     }
14733
14734   /* The 504 below is 8 bytes less than 512 because there are two possible
14735      alignment words.  We can't tell here if they will be present or not so we
14736      have to play it safe and assume that they are. */
14737   if ((CALLER_INTERWORKING_SLOT_SIZE +
14738        ROUND_UP_WORD (get_frame_size ()) +
14739        crtl->outgoing_args_size) >= 504)
14740     {
14741       /* This is the same as the code in thumb1_expand_prologue() which
14742          determines which register to use for stack decrement. */
14743       for (reg = LAST_ARG_REGNUM + 1; reg <= LAST_LO_REGNUM; reg++)
14744         if (mask & (1 << reg))
14745           break;
14746
14747       if (reg > LAST_LO_REGNUM)
14748         {
14749           /* Make sure we have a register available for stack decrement. */
14750           mask |= 1 << LAST_LO_REGNUM;
14751         }
14752     }
14753
14754   return mask;
14755 }
14756
14757
14758 /* Return the number of bytes required to save VFP registers.  */
14759 static int
14760 arm_get_vfp_saved_size (void)
14761 {
14762   unsigned int regno;
14763   int count;
14764   int saved;
14765
14766   saved = 0;
14767   /* Space for saved VFP registers.  */
14768   if (TARGET_HARD_FLOAT && TARGET_VFP)
14769     {
14770       count = 0;
14771       for (regno = FIRST_VFP_REGNUM;
14772            regno < LAST_VFP_REGNUM;
14773            regno += 2)
14774         {
14775           if ((!df_regs_ever_live_p (regno) || call_used_regs[regno])
14776               && (!df_regs_ever_live_p (regno + 1) || call_used_regs[regno + 1]))
14777             {
14778               if (count > 0)
14779                 {
14780                   /* Workaround ARM10 VFPr1 bug.  */
14781                   if (count == 2 && !arm_arch6)
14782                     count++;
14783                   saved += count * 8;
14784                 }
14785               count = 0;
14786             }
14787           else
14788             count++;
14789         }
14790       if (count > 0)
14791         {
14792           if (count == 2 && !arm_arch6)
14793             count++;
14794           saved += count * 8;
14795         }
14796     }
14797   return saved;
14798 }
14799
14800
14801 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
14802    everything bar the final return instruction.  */
14803 const char *
14804 output_return_instruction (rtx operand, int really_return, int reverse)
14805 {
14806   char conditional[10];
14807   char instr[100];
14808   unsigned reg;
14809   unsigned long live_regs_mask;
14810   unsigned long func_type;
14811   arm_stack_offsets *offsets;
14812
14813   func_type = arm_current_func_type ();
14814
14815   if (IS_NAKED (func_type))
14816     return "";
14817
14818   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
14819     {
14820       /* If this function was declared non-returning, and we have
14821          found a tail call, then we have to trust that the called
14822          function won't return.  */
14823       if (really_return)
14824         {
14825           rtx ops[2];
14826
14827           /* Otherwise, trap an attempted return by aborting.  */
14828           ops[0] = operand;
14829           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
14830                                        : "abort");
14831           assemble_external_libcall (ops[1]);
14832           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
14833         }
14834
14835       return "";
14836     }
14837
14838   gcc_assert (!cfun->calls_alloca || really_return);
14839
14840   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
14841
14842   cfun->machine->return_used_this_function = 1;
14843
14844   offsets = arm_get_frame_offsets ();
14845   live_regs_mask = offsets->saved_regs_mask;
14846
14847   if (live_regs_mask)
14848     {
14849       const char * return_reg;
14850
14851       /* If we do not have any special requirements for function exit
14852          (e.g. interworking) then we can load the return address
14853          directly into the PC.  Otherwise we must load it into LR.  */
14854       if (really_return
14855           && (IS_INTERRUPT (func_type) || !TARGET_INTERWORK))
14856         return_reg = reg_names[PC_REGNUM];
14857       else
14858         return_reg = reg_names[LR_REGNUM];
14859
14860       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
14861         {
14862           /* There are three possible reasons for the IP register
14863              being saved.  1) a stack frame was created, in which case
14864              IP contains the old stack pointer, or 2) an ISR routine
14865              corrupted it, or 3) it was saved to align the stack on
14866              iWMMXt.  In case 1, restore IP into SP, otherwise just
14867              restore IP.  */
14868           if (frame_pointer_needed)
14869             {
14870               live_regs_mask &= ~ (1 << IP_REGNUM);
14871               live_regs_mask |=   (1 << SP_REGNUM);
14872             }
14873           else
14874             gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
14875         }
14876
14877       /* On some ARM architectures it is faster to use LDR rather than
14878          LDM to load a single register.  On other architectures, the
14879          cost is the same.  In 26 bit mode, or for exception handlers,
14880          we have to use LDM to load the PC so that the CPSR is also
14881          restored.  */
14882       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
14883         if (live_regs_mask == (1U << reg))
14884           break;
14885
14886       if (reg <= LAST_ARM_REGNUM
14887           && (reg != LR_REGNUM
14888               || ! really_return
14889               || ! IS_INTERRUPT (func_type)))
14890         {
14891           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
14892                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
14893         }
14894       else
14895         {
14896           char *p;
14897           int first = 1;
14898
14899           /* Generate the load multiple instruction to restore the
14900              registers.  Note we can get here, even if
14901              frame_pointer_needed is true, but only if sp already
14902              points to the base of the saved core registers.  */
14903           if (live_regs_mask & (1 << SP_REGNUM))
14904             {
14905               unsigned HOST_WIDE_INT stack_adjust;
14906
14907               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
14908               gcc_assert (stack_adjust == 0 || stack_adjust == 4);
14909
14910               if (stack_adjust && arm_arch5 && TARGET_ARM)
14911                 if (TARGET_UNIFIED_ASM)
14912                   sprintf (instr, "ldmib%s\t%%|sp, {", conditional);
14913                 else
14914                   sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
14915               else
14916                 {
14917                   /* If we can't use ldmib (SA110 bug),
14918                      then try to pop r3 instead.  */
14919                   if (stack_adjust)
14920                     live_regs_mask |= 1 << 3;
14921                   
14922                   if (TARGET_UNIFIED_ASM)
14923                     sprintf (instr, "ldmfd%s\t%%|sp, {", conditional);
14924                   else
14925                     sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
14926                 }
14927             }
14928           else
14929             if (TARGET_UNIFIED_ASM)
14930               sprintf (instr, "pop%s\t{", conditional);
14931             else
14932               sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
14933
14934           p = instr + strlen (instr);
14935
14936           for (reg = 0; reg <= SP_REGNUM; reg++)
14937             if (live_regs_mask & (1 << reg))
14938               {
14939                 int l = strlen (reg_names[reg]);
14940
14941                 if (first)
14942                   first = 0;
14943                 else
14944                   {
14945                     memcpy (p, ", ", 2);
14946                     p += 2;
14947                   }
14948
14949                 memcpy (p, "%|", 2);
14950                 memcpy (p + 2, reg_names[reg], l);
14951                 p += l + 2;
14952               }
14953
14954           if (live_regs_mask & (1 << LR_REGNUM))
14955             {
14956               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
14957               /* If returning from an interrupt, restore the CPSR.  */
14958               if (IS_INTERRUPT (func_type))
14959                 strcat (p, "^");
14960             }
14961           else
14962             strcpy (p, "}");
14963         }
14964
14965       output_asm_insn (instr, & operand);
14966
14967       /* See if we need to generate an extra instruction to
14968          perform the actual function return.  */
14969       if (really_return
14970           && func_type != ARM_FT_INTERWORKED
14971           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
14972         {
14973           /* The return has already been handled
14974              by loading the LR into the PC.  */
14975           really_return = 0;
14976         }
14977     }
14978
14979   if (really_return)
14980     {
14981       switch ((int) ARM_FUNC_TYPE (func_type))
14982         {
14983         case ARM_FT_ISR:
14984         case ARM_FT_FIQ:
14985           /* ??? This is wrong for unified assembly syntax.  */
14986           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
14987           break;
14988
14989         case ARM_FT_INTERWORKED:
14990           sprintf (instr, "bx%s\t%%|lr", conditional);
14991           break;
14992
14993         case ARM_FT_EXCEPTION:
14994           /* ??? This is wrong for unified assembly syntax.  */
14995           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
14996           break;
14997
14998         default:
14999           /* Use bx if it's available.  */
15000           if (arm_arch5 || arm_arch4t)
15001             sprintf (instr, "bx%s\t%%|lr", conditional);
15002           else
15003             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
15004           break;
15005         }
15006
15007       output_asm_insn (instr, & operand);
15008     }
15009
15010   return "";
15011 }
15012
15013 /* Write the function name into the code section, directly preceding
15014    the function prologue.
15015
15016    Code will be output similar to this:
15017      t0
15018          .ascii "arm_poke_function_name", 0
15019          .align
15020      t1
15021          .word 0xff000000 + (t1 - t0)
15022      arm_poke_function_name
15023          mov     ip, sp
15024          stmfd   sp!, {fp, ip, lr, pc}
15025          sub     fp, ip, #4
15026
15027    When performing a stack backtrace, code can inspect the value
15028    of 'pc' stored at 'fp' + 0.  If the trace function then looks
15029    at location pc - 12 and the top 8 bits are set, then we know
15030    that there is a function name embedded immediately preceding this
15031    location and has length ((pc[-3]) & 0xff000000).
15032
15033    We assume that pc is declared as a pointer to an unsigned long.
15034
15035    It is of no benefit to output the function name if we are assembling
15036    a leaf function.  These function types will not contain a stack
15037    backtrace structure, therefore it is not possible to determine the
15038    function name.  */
15039 void
15040 arm_poke_function_name (FILE *stream, const char *name)
15041 {
15042   unsigned long alignlength;
15043   unsigned long length;
15044   rtx           x;
15045
15046   length      = strlen (name) + 1;
15047   alignlength = ROUND_UP_WORD (length);
15048
15049   ASM_OUTPUT_ASCII (stream, name, length);
15050   ASM_OUTPUT_ALIGN (stream, 2);
15051   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
15052   assemble_aligned_integer (UNITS_PER_WORD, x);
15053 }
15054
15055 /* Place some comments into the assembler stream
15056    describing the current function.  */
15057 static void
15058 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
15059 {
15060   unsigned long func_type;
15061
15062   /* ??? Do we want to print some of the below anyway?  */
15063   if (TARGET_THUMB1)
15064     return;
15065
15066   /* Sanity check.  */
15067   gcc_assert (!arm_ccfsm_state && !arm_target_insn);
15068
15069   func_type = arm_current_func_type ();
15070
15071   switch ((int) ARM_FUNC_TYPE (func_type))
15072     {
15073     default:
15074     case ARM_FT_NORMAL:
15075       break;
15076     case ARM_FT_INTERWORKED:
15077       asm_fprintf (f, "\t%@ Function supports interworking.\n");
15078       break;
15079     case ARM_FT_ISR:
15080       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
15081       break;
15082     case ARM_FT_FIQ:
15083       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
15084       break;
15085     case ARM_FT_EXCEPTION:
15086       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
15087       break;
15088     }
15089
15090   if (IS_NAKED (func_type))
15091     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
15092
15093   if (IS_VOLATILE (func_type))
15094     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
15095
15096   if (IS_NESTED (func_type))
15097     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
15098   if (IS_STACKALIGN (func_type))
15099     asm_fprintf (f, "\t%@ Stack Align: May be called with mis-aligned SP.\n");
15100
15101   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
15102                crtl->args.size,
15103                crtl->args.pretend_args_size, frame_size);
15104
15105   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
15106                frame_pointer_needed,
15107                cfun->machine->uses_anonymous_args);
15108
15109   if (cfun->machine->lr_save_eliminated)
15110     asm_fprintf (f, "\t%@ link register save eliminated.\n");
15111
15112   if (crtl->calls_eh_return)
15113     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
15114
15115 }
15116
15117 const char *
15118 arm_output_epilogue (rtx sibling)
15119 {
15120   int reg;
15121   unsigned long saved_regs_mask;
15122   unsigned long func_type;
15123   /* Floats_offset is the offset from the "virtual" frame.  In an APCS
15124      frame that is $fp + 4 for a non-variadic function.  */
15125   int floats_offset = 0;
15126   rtx operands[3];
15127   FILE * f = asm_out_file;
15128   unsigned int lrm_count = 0;
15129   int really_return = (sibling == NULL);
15130   int start_reg;
15131   arm_stack_offsets *offsets;
15132
15133   /* If we have already generated the return instruction
15134      then it is futile to generate anything else.  */
15135   if (use_return_insn (FALSE, sibling) && 
15136       (cfun->machine->return_used_this_function != 0))
15137     return "";
15138
15139   func_type = arm_current_func_type ();
15140
15141   if (IS_NAKED (func_type))
15142     /* Naked functions don't have epilogues.  */
15143     return "";
15144
15145   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
15146     {
15147       rtx op;
15148
15149       /* A volatile function should never return.  Call abort.  */
15150       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
15151       assemble_external_libcall (op);
15152       output_asm_insn ("bl\t%a0", &op);
15153
15154       return "";
15155     }
15156
15157   /* If we are throwing an exception, then we really must be doing a
15158      return, so we can't tail-call.  */
15159   gcc_assert (!crtl->calls_eh_return || really_return);
15160
15161   offsets = arm_get_frame_offsets ();
15162   saved_regs_mask = offsets->saved_regs_mask;
15163
15164   if (TARGET_IWMMXT)
15165     lrm_count = bit_count (saved_regs_mask);
15166
15167   floats_offset = offsets->saved_args;
15168   /* Compute how far away the floats will be.  */
15169   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
15170     if (saved_regs_mask & (1 << reg))
15171       floats_offset += 4;
15172
15173   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
15174     {
15175       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
15176       int vfp_offset = offsets->frame;
15177
15178       if (TARGET_FPA_EMU2)
15179         {
15180           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
15181             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
15182               {
15183                 floats_offset += 12;
15184                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
15185                              reg, FP_REGNUM, floats_offset - vfp_offset);
15186               }
15187         }
15188       else
15189         {
15190           start_reg = LAST_FPA_REGNUM;
15191
15192           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
15193             {
15194               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
15195                 {
15196                   floats_offset += 12;
15197
15198                   /* We can't unstack more than four registers at once.  */
15199                   if (start_reg - reg == 3)
15200                     {
15201                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
15202                                    reg, FP_REGNUM, floats_offset - vfp_offset);
15203                       start_reg = reg - 1;
15204                     }
15205                 }
15206               else
15207                 {
15208                   if (reg != start_reg)
15209                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
15210                                  reg + 1, start_reg - reg,
15211                                  FP_REGNUM, floats_offset - vfp_offset);
15212                   start_reg = reg - 1;
15213                 }
15214             }
15215
15216           /* Just in case the last register checked also needs unstacking.  */
15217           if (reg != start_reg)
15218             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
15219                          reg + 1, start_reg - reg,
15220                          FP_REGNUM, floats_offset - vfp_offset);
15221         }
15222
15223       if (TARGET_HARD_FLOAT && TARGET_VFP)
15224         {
15225           int saved_size;
15226
15227           /* The fldmd insns do not have base+offset addressing
15228              modes, so we use IP to hold the address.  */
15229           saved_size = arm_get_vfp_saved_size ();
15230
15231           if (saved_size > 0)
15232             {
15233               floats_offset += saved_size;
15234               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
15235                            FP_REGNUM, floats_offset - vfp_offset);
15236             }
15237           start_reg = FIRST_VFP_REGNUM;
15238           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
15239             {
15240               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
15241                   && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
15242                 {
15243                   if (start_reg != reg)
15244                     vfp_output_fldmd (f, IP_REGNUM,
15245                                       (start_reg - FIRST_VFP_REGNUM) / 2,
15246                                       (reg - start_reg) / 2);
15247                   start_reg = reg + 2;
15248                 }
15249             }
15250           if (start_reg != reg)
15251             vfp_output_fldmd (f, IP_REGNUM,
15252                               (start_reg - FIRST_VFP_REGNUM) / 2,
15253                               (reg - start_reg) / 2);
15254         }
15255
15256       if (TARGET_IWMMXT)
15257         {
15258           /* The frame pointer is guaranteed to be non-double-word aligned.
15259              This is because it is set to (old_stack_pointer - 4) and the
15260              old_stack_pointer was double word aligned.  Thus the offset to
15261              the iWMMXt registers to be loaded must also be non-double-word
15262              sized, so that the resultant address *is* double-word aligned.
15263              We can ignore floats_offset since that was already included in
15264              the live_regs_mask.  */
15265           lrm_count += (lrm_count % 2 ? 2 : 1);
15266
15267           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
15268             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
15269               {
15270                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
15271                              reg, FP_REGNUM, lrm_count * 4);
15272                 lrm_count += 2;
15273               }
15274         }
15275
15276       /* saved_regs_mask should contain the IP, which at the time of stack
15277          frame generation actually contains the old stack pointer.  So a
15278          quick way to unwind the stack is just pop the IP register directly
15279          into the stack pointer.  */
15280       gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
15281       saved_regs_mask &= ~ (1 << IP_REGNUM);
15282       saved_regs_mask |=   (1 << SP_REGNUM);
15283
15284       /* There are two registers left in saved_regs_mask - LR and PC.  We
15285          only need to restore the LR register (the return address), but to
15286          save time we can load it directly into the PC, unless we need a
15287          special function exit sequence, or we are not really returning.  */
15288       if (really_return
15289           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
15290           && !crtl->calls_eh_return)
15291         /* Delete the LR from the register mask, so that the LR on
15292            the stack is loaded into the PC in the register mask.  */
15293         saved_regs_mask &= ~ (1 << LR_REGNUM);
15294       else
15295         saved_regs_mask &= ~ (1 << PC_REGNUM);
15296
15297       /* We must use SP as the base register, because SP is one of the
15298          registers being restored.  If an interrupt or page fault
15299          happens in the ldm instruction, the SP might or might not
15300          have been restored.  That would be bad, as then SP will no
15301          longer indicate the safe area of stack, and we can get stack
15302          corruption.  Using SP as the base register means that it will
15303          be reset correctly to the original value, should an interrupt
15304          occur.  If the stack pointer already points at the right
15305          place, then omit the subtraction.  */
15306       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
15307           || cfun->calls_alloca)
15308         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
15309                      4 * bit_count (saved_regs_mask));
15310       print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask, 0);
15311
15312       if (IS_INTERRUPT (func_type))
15313         /* Interrupt handlers will have pushed the
15314            IP onto the stack, so restore it now.  */
15315         print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, 1 << IP_REGNUM, 0);
15316     }
15317   else
15318     {
15319       /* This branch is executed for ARM mode (non-apcs frames) and
15320          Thumb-2 mode. Frame layout is essentially the same for those
15321          cases, except that in ARM mode frame pointer points to the
15322          first saved register, while in Thumb-2 mode the frame pointer points
15323          to the last saved register.
15324
15325          It is possible to make frame pointer point to last saved
15326          register in both cases, and remove some conditionals below.
15327          That means that fp setup in prologue would be just "mov fp, sp"
15328          and sp restore in epilogue would be just "mov sp, fp", whereas
15329          now we have to use add/sub in those cases. However, the value
15330          of that would be marginal, as both mov and add/sub are 32-bit
15331          in ARM mode, and it would require extra conditionals
15332          in arm_expand_prologue to distingish ARM-apcs-frame case
15333          (where frame pointer is required to point at first register)
15334          and ARM-non-apcs-frame. Therefore, such change is postponed
15335          until real need arise.  */
15336       unsigned HOST_WIDE_INT amount;
15337       int rfe;
15338       /* Restore stack pointer if necessary.  */
15339       if (TARGET_ARM && frame_pointer_needed)
15340         {
15341           operands[0] = stack_pointer_rtx;
15342           operands[1] = hard_frame_pointer_rtx;
15343           
15344           operands[2] = GEN_INT (offsets->frame - offsets->saved_regs);
15345           output_add_immediate (operands);
15346         }
15347       else
15348         {
15349           if (frame_pointer_needed)
15350             {
15351               /* For Thumb-2 restore sp from the frame pointer.
15352                  Operand restrictions mean we have to incrememnt FP, then copy
15353                  to SP.  */
15354               amount = offsets->locals_base - offsets->saved_regs;
15355               operands[0] = hard_frame_pointer_rtx;
15356             }
15357           else
15358             {
15359               unsigned long count;
15360               operands[0] = stack_pointer_rtx;
15361               amount = offsets->outgoing_args - offsets->saved_regs;
15362               /* pop call clobbered registers if it avoids a
15363                  separate stack adjustment.  */
15364               count = offsets->saved_regs - offsets->saved_args;
15365               if (optimize_size
15366                   && count != 0
15367                   && !crtl->calls_eh_return
15368                   && bit_count(saved_regs_mask) * 4 == count
15369                   && !IS_INTERRUPT (func_type)
15370                   && !IS_STACKALIGN (func_type)
15371                   && !crtl->tail_call_emit)
15372                 {
15373                   unsigned long mask;
15374                   /* Preserve return values, of any size.  */
15375                   mask = (1 << ((arm_size_return_regs() + 3) / 4)) - 1;
15376                   mask ^= 0xf;
15377                   mask &= ~saved_regs_mask;
15378                   reg = 0;
15379                   while (bit_count (mask) * 4 > amount)
15380                     {
15381                       while ((mask & (1 << reg)) == 0)
15382                         reg++;
15383                       mask &= ~(1 << reg);
15384                     }
15385                   if (bit_count (mask) * 4 == amount) {
15386                       amount = 0;
15387                       saved_regs_mask |= mask;
15388                   }
15389                 }
15390             }
15391           
15392           if (amount)
15393             {
15394               operands[1] = operands[0];
15395               operands[2] = GEN_INT (amount);
15396               output_add_immediate (operands);
15397             }
15398           if (frame_pointer_needed)
15399             asm_fprintf (f, "\tmov\t%r, %r\n",
15400                          SP_REGNUM, HARD_FRAME_POINTER_REGNUM);
15401         }
15402
15403       if (TARGET_FPA_EMU2)
15404         {
15405           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
15406             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
15407               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
15408                            reg, SP_REGNUM);
15409         }
15410       else
15411         {
15412           start_reg = FIRST_FPA_REGNUM;
15413
15414           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
15415             {
15416               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
15417                 {
15418                   if (reg - start_reg == 3)
15419                     {
15420                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
15421                                    start_reg, SP_REGNUM);
15422                       start_reg = reg + 1;
15423                     }
15424                 }
15425               else
15426                 {
15427                   if (reg != start_reg)
15428                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
15429                                  start_reg, reg - start_reg,
15430                                  SP_REGNUM);
15431
15432                   start_reg = reg + 1;
15433                 }
15434             }
15435
15436           /* Just in case the last register checked also needs unstacking.  */
15437           if (reg != start_reg)
15438             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
15439                          start_reg, reg - start_reg, SP_REGNUM);
15440         }
15441
15442       if (TARGET_HARD_FLOAT && TARGET_VFP)
15443         {
15444           int end_reg = LAST_VFP_REGNUM + 1;
15445
15446           /* Scan the registers in reverse order.  We need to match
15447              any groupings made in the prologue and generate matching
15448              pop operations.  */
15449           for (reg = LAST_VFP_REGNUM - 1; reg >= FIRST_VFP_REGNUM; reg -= 2)
15450             {
15451               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
15452                   && (!df_regs_ever_live_p (reg + 1)
15453                       || call_used_regs[reg + 1]))
15454                 {
15455                   if (end_reg > reg + 2)
15456                     vfp_output_fldmd (f, SP_REGNUM,
15457                                       (reg + 2 - FIRST_VFP_REGNUM) / 2,
15458                                       (end_reg - (reg + 2)) / 2);
15459                   end_reg = reg;
15460                 }
15461             }
15462           if (end_reg > reg + 2)
15463             vfp_output_fldmd (f, SP_REGNUM, 0,
15464                               (end_reg - (reg + 2)) / 2);
15465         }
15466
15467       if (TARGET_IWMMXT)
15468         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
15469           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
15470             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
15471
15472       /* If we can, restore the LR into the PC.  */
15473       if (ARM_FUNC_TYPE (func_type) != ARM_FT_INTERWORKED
15474           && (TARGET_ARM || ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
15475           && !IS_STACKALIGN (func_type)
15476           && really_return
15477           && crtl->args.pretend_args_size == 0
15478           && saved_regs_mask & (1 << LR_REGNUM)
15479           && !crtl->calls_eh_return)
15480         {
15481           saved_regs_mask &= ~ (1 << LR_REGNUM);
15482           saved_regs_mask |=   (1 << PC_REGNUM);
15483           rfe = IS_INTERRUPT (func_type);
15484         }
15485       else
15486         rfe = 0;
15487
15488       /* Load the registers off the stack.  If we only have one register
15489          to load use the LDR instruction - it is faster.  For Thumb-2
15490          always use pop and the assembler will pick the best instruction.*/
15491       if (TARGET_ARM && saved_regs_mask == (1 << LR_REGNUM)
15492           && !IS_INTERRUPT(func_type))
15493         {
15494           asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
15495         }
15496       else if (saved_regs_mask)
15497         {
15498           if (saved_regs_mask & (1 << SP_REGNUM))
15499             /* Note - write back to the stack register is not enabled
15500                (i.e. "ldmfd sp!...").  We know that the stack pointer is
15501                in the list of registers and if we add writeback the
15502                instruction becomes UNPREDICTABLE.  */
15503             print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask,
15504                              rfe);
15505           else if (TARGET_ARM)
15506             print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, saved_regs_mask,
15507                              rfe);
15508           else
15509             print_multi_reg (f, "pop\t", SP_REGNUM, saved_regs_mask, 0);
15510         }
15511
15512       if (crtl->args.pretend_args_size)
15513         {
15514           /* Unwind the pre-pushed regs.  */
15515           operands[0] = operands[1] = stack_pointer_rtx;
15516           operands[2] = GEN_INT (crtl->args.pretend_args_size);
15517           output_add_immediate (operands);
15518         }
15519     }
15520
15521   /* We may have already restored PC directly from the stack.  */
15522   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
15523     return "";
15524
15525   /* Stack adjustment for exception handler.  */
15526   if (crtl->calls_eh_return)
15527     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
15528                  ARM_EH_STACKADJ_REGNUM);
15529
15530   /* Generate the return instruction.  */
15531   switch ((int) ARM_FUNC_TYPE (func_type))
15532     {
15533     case ARM_FT_ISR:
15534     case ARM_FT_FIQ:
15535       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
15536       break;
15537
15538     case ARM_FT_EXCEPTION:
15539       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
15540       break;
15541
15542     case ARM_FT_INTERWORKED:
15543       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
15544       break;
15545
15546     default:
15547       if (IS_STACKALIGN (func_type))
15548         {
15549           /* See comment in arm_expand_prologue.  */
15550           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, 0);
15551         }
15552       if (arm_arch5 || arm_arch4t)
15553         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
15554       else
15555         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
15556       break;
15557     }
15558
15559   return "";
15560 }
15561
15562 static void
15563 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
15564                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
15565 {
15566   arm_stack_offsets *offsets;
15567
15568   if (TARGET_THUMB1)
15569     {
15570       int regno;
15571
15572       /* Emit any call-via-reg trampolines that are needed for v4t support
15573          of call_reg and call_value_reg type insns.  */
15574       for (regno = 0; regno < LR_REGNUM; regno++)
15575         {
15576           rtx label = cfun->machine->call_via[regno];
15577
15578           if (label != NULL)
15579             {
15580               switch_to_section (function_section (current_function_decl));
15581               targetm.asm_out.internal_label (asm_out_file, "L",
15582                                               CODE_LABEL_NUMBER (label));
15583               asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
15584             }
15585         }
15586
15587       /* ??? Probably not safe to set this here, since it assumes that a
15588          function will be emitted as assembly immediately after we generate
15589          RTL for it.  This does not happen for inline functions.  */
15590       cfun->machine->return_used_this_function = 0;
15591     }
15592   else /* TARGET_32BIT */
15593     {
15594       /* We need to take into account any stack-frame rounding.  */
15595       offsets = arm_get_frame_offsets ();
15596
15597       gcc_assert (!use_return_insn (FALSE, NULL)
15598                   || (cfun->machine->return_used_this_function != 0)
15599                   || offsets->saved_regs == offsets->outgoing_args
15600                   || frame_pointer_needed);
15601
15602       /* Reset the ARM-specific per-function variables.  */
15603       after_arm_reorg = 0;
15604     }
15605 }
15606
15607 /* Generate and emit an insn that we will recognize as a push_multi.
15608    Unfortunately, since this insn does not reflect very well the actual
15609    semantics of the operation, we need to annotate the insn for the benefit
15610    of DWARF2 frame unwind information.  */
15611 static rtx
15612 emit_multi_reg_push (unsigned long mask)
15613 {
15614   int num_regs = 0;
15615   int num_dwarf_regs;
15616   int i, j;
15617   rtx par;
15618   rtx dwarf;
15619   int dwarf_par_index;
15620   rtx tmp, reg;
15621
15622   for (i = 0; i <= LAST_ARM_REGNUM; i++)
15623     if (mask & (1 << i))
15624       num_regs++;
15625
15626   gcc_assert (num_regs && num_regs <= 16);
15627
15628   /* We don't record the PC in the dwarf frame information.  */
15629   num_dwarf_regs = num_regs;
15630   if (mask & (1 << PC_REGNUM))
15631     num_dwarf_regs--;
15632
15633   /* For the body of the insn we are going to generate an UNSPEC in
15634      parallel with several USEs.  This allows the insn to be recognized
15635      by the push_multi pattern in the arm.md file.
15636
15637      The body of the insn looks something like this:
15638
15639        (parallel [
15640            (set (mem:BLK (pre_modify:SI (reg:SI sp)
15641                                         (const_int:SI <num>)))
15642                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
15643            (use (reg:SI XX))
15644            (use (reg:SI YY))
15645            ...
15646         ])
15647
15648      For the frame note however, we try to be more explicit and actually
15649      show each register being stored into the stack frame, plus a (single)
15650      decrement of the stack pointer.  We do it this way in order to be
15651      friendly to the stack unwinding code, which only wants to see a single
15652      stack decrement per instruction.  The RTL we generate for the note looks
15653      something like this:
15654
15655       (sequence [
15656            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
15657            (set (mem:SI (reg:SI sp)) (reg:SI r4))
15658            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI XX))
15659            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI YY))
15660            ...
15661         ])
15662
15663      FIXME:: In an ideal world the PRE_MODIFY would not exist and
15664      instead we'd have a parallel expression detailing all
15665      the stores to the various memory addresses so that debug
15666      information is more up-to-date. Remember however while writing
15667      this to take care of the constraints with the push instruction.
15668
15669      Note also that this has to be taken care of for the VFP registers.
15670
15671      For more see PR43399.  */
15672
15673   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
15674   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
15675   dwarf_par_index = 1;
15676
15677   for (i = 0; i <= LAST_ARM_REGNUM; i++)
15678     {
15679       if (mask & (1 << i))
15680         {
15681           reg = gen_rtx_REG (SImode, i);
15682
15683           XVECEXP (par, 0, 0)
15684             = gen_rtx_SET (VOIDmode,
15685                            gen_frame_mem
15686                            (BLKmode,
15687                             gen_rtx_PRE_MODIFY (Pmode,
15688                                                 stack_pointer_rtx,
15689                                                 plus_constant
15690                                                 (stack_pointer_rtx,
15691                                                  -4 * num_regs))
15692                             ),
15693                            gen_rtx_UNSPEC (BLKmode,
15694                                            gen_rtvec (1, reg),
15695                                            UNSPEC_PUSH_MULT));
15696
15697           if (i != PC_REGNUM)
15698             {
15699               tmp = gen_rtx_SET (VOIDmode,
15700                                  gen_frame_mem (SImode, stack_pointer_rtx),
15701                                  reg);
15702               RTX_FRAME_RELATED_P (tmp) = 1;
15703               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
15704               dwarf_par_index++;
15705             }
15706
15707           break;
15708         }
15709     }
15710
15711   for (j = 1, i++; j < num_regs; i++)
15712     {
15713       if (mask & (1 << i))
15714         {
15715           reg = gen_rtx_REG (SImode, i);
15716
15717           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
15718
15719           if (i != PC_REGNUM)
15720             {
15721               tmp
15722                 = gen_rtx_SET (VOIDmode,
15723                                gen_frame_mem
15724                                (SImode,
15725                                 plus_constant (stack_pointer_rtx,
15726                                                4 * j)),
15727                                reg);
15728               RTX_FRAME_RELATED_P (tmp) = 1;
15729               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
15730             }
15731
15732           j++;
15733         }
15734     }
15735
15736   par = emit_insn (par);
15737
15738   tmp = gen_rtx_SET (VOIDmode,
15739                      stack_pointer_rtx,
15740                      plus_constant (stack_pointer_rtx, -4 * num_regs));
15741   RTX_FRAME_RELATED_P (tmp) = 1;
15742   XVECEXP (dwarf, 0, 0) = tmp;
15743
15744   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
15745
15746   return par;
15747 }
15748
15749 /* Calculate the size of the return value that is passed in registers.  */
15750 static unsigned
15751 arm_size_return_regs (void)
15752 {
15753   enum machine_mode mode;
15754
15755   if (crtl->return_rtx != 0)
15756     mode = GET_MODE (crtl->return_rtx);
15757   else
15758     mode = DECL_MODE (DECL_RESULT (current_function_decl));
15759
15760   return GET_MODE_SIZE (mode);
15761 }
15762
15763 static rtx
15764 emit_sfm (int base_reg, int count)
15765 {
15766   rtx par;
15767   rtx dwarf;
15768   rtx tmp, reg;
15769   int i;
15770
15771   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
15772   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
15773
15774   reg = gen_rtx_REG (XFmode, base_reg++);
15775
15776   XVECEXP (par, 0, 0)
15777     = gen_rtx_SET (VOIDmode,
15778                    gen_frame_mem
15779                    (BLKmode,
15780                     gen_rtx_PRE_MODIFY (Pmode,
15781                                         stack_pointer_rtx,
15782                                         plus_constant
15783                                         (stack_pointer_rtx,
15784                                          -12 * count))
15785                     ),
15786                    gen_rtx_UNSPEC (BLKmode,
15787                                    gen_rtvec (1, reg),
15788                                    UNSPEC_PUSH_MULT));
15789   tmp = gen_rtx_SET (VOIDmode,
15790                      gen_frame_mem (XFmode, stack_pointer_rtx), reg);
15791   RTX_FRAME_RELATED_P (tmp) = 1;
15792   XVECEXP (dwarf, 0, 1) = tmp;
15793
15794   for (i = 1; i < count; i++)
15795     {
15796       reg = gen_rtx_REG (XFmode, base_reg++);
15797       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
15798
15799       tmp = gen_rtx_SET (VOIDmode,
15800                          gen_frame_mem (XFmode,
15801                                         plus_constant (stack_pointer_rtx,
15802                                                        i * 12)),
15803                          reg);
15804       RTX_FRAME_RELATED_P (tmp) = 1;
15805       XVECEXP (dwarf, 0, i + 1) = tmp;
15806     }
15807
15808   tmp = gen_rtx_SET (VOIDmode,
15809                      stack_pointer_rtx,
15810                      plus_constant (stack_pointer_rtx, -12 * count));
15811
15812   RTX_FRAME_RELATED_P (tmp) = 1;
15813   XVECEXP (dwarf, 0, 0) = tmp;
15814
15815   par = emit_insn (par);
15816   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
15817
15818   return par;
15819 }
15820
15821
15822 /* Return true if the current function needs to save/restore LR.  */
15823
15824 static bool
15825 thumb_force_lr_save (void)
15826 {
15827   return !cfun->machine->lr_save_eliminated
15828          && (!leaf_function_p ()
15829              || thumb_far_jump_used_p ()
15830              || df_regs_ever_live_p (LR_REGNUM));
15831 }
15832
15833
15834 /* Return true if r3 is used by any of the tail call insns in the
15835    current function.  */
15836
15837 static bool
15838 any_sibcall_uses_r3 (void)
15839 {
15840   edge_iterator ei;
15841   edge e;
15842
15843   if (!crtl->tail_call_emit)
15844     return false;
15845   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
15846     if (e->flags & EDGE_SIBCALL)
15847       {
15848         rtx call = BB_END (e->src);
15849         if (!CALL_P (call))
15850           call = prev_nonnote_nondebug_insn (call);
15851         gcc_assert (CALL_P (call) && SIBLING_CALL_P (call));
15852         if (find_regno_fusage (call, USE, 3))
15853           return true;
15854       }
15855   return false;
15856 }
15857
15858
15859 /* Compute the distance from register FROM to register TO.
15860    These can be the arg pointer (26), the soft frame pointer (25),
15861    the stack pointer (13) or the hard frame pointer (11).
15862    In thumb mode r7 is used as the soft frame pointer, if needed.
15863    Typical stack layout looks like this:
15864
15865        old stack pointer -> |    |
15866                              ----
15867                             |    | \
15868                             |    |   saved arguments for
15869                             |    |   vararg functions
15870                             |    | /
15871                               --
15872    hard FP & arg pointer -> |    | \
15873                             |    |   stack
15874                             |    |   frame
15875                             |    | /
15876                               --
15877                             |    | \
15878                             |    |   call saved
15879                             |    |   registers
15880       soft frame pointer -> |    | /
15881                               --
15882                             |    | \
15883                             |    |   local
15884                             |    |   variables
15885      locals base pointer -> |    | /
15886                               --
15887                             |    | \
15888                             |    |   outgoing
15889                             |    |   arguments
15890    current stack pointer -> |    | /
15891                               --
15892
15893   For a given function some or all of these stack components
15894   may not be needed, giving rise to the possibility of
15895   eliminating some of the registers.
15896
15897   The values returned by this function must reflect the behavior
15898   of arm_expand_prologue() and arm_compute_save_reg_mask().
15899
15900   The sign of the number returned reflects the direction of stack
15901   growth, so the values are positive for all eliminations except
15902   from the soft frame pointer to the hard frame pointer.
15903
15904   SFP may point just inside the local variables block to ensure correct
15905   alignment.  */
15906
15907
15908 /* Calculate stack offsets.  These are used to calculate register elimination
15909    offsets and in prologue/epilogue code.  Also calculates which registers
15910    should be saved.  */
15911
15912 static arm_stack_offsets *
15913 arm_get_frame_offsets (void)
15914 {
15915   struct arm_stack_offsets *offsets;
15916   unsigned long func_type;
15917   int leaf;
15918   int saved;
15919   int core_saved;
15920   HOST_WIDE_INT frame_size;
15921   int i;
15922
15923   offsets = &cfun->machine->stack_offsets;
15924
15925   /* We need to know if we are a leaf function.  Unfortunately, it
15926      is possible to be called after start_sequence has been called,
15927      which causes get_insns to return the insns for the sequence,
15928      not the function, which will cause leaf_function_p to return
15929      the incorrect result.
15930
15931      to know about leaf functions once reload has completed, and the
15932      frame size cannot be changed after that time, so we can safely
15933      use the cached value.  */
15934
15935   if (reload_completed)
15936     return offsets;
15937
15938   /* Initially this is the size of the local variables.  It will translated
15939      into an offset once we have determined the size of preceding data.  */
15940   frame_size = ROUND_UP_WORD (get_frame_size ());
15941
15942   leaf = leaf_function_p ();
15943
15944   /* Space for variadic functions.  */
15945   offsets->saved_args = crtl->args.pretend_args_size;
15946
15947   /* In Thumb mode this is incorrect, but never used.  */
15948   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0) +
15949                    arm_compute_static_chain_stack_bytes();
15950
15951   if (TARGET_32BIT)
15952     {
15953       unsigned int regno;
15954
15955       offsets->saved_regs_mask = arm_compute_save_reg_mask ();
15956       core_saved = bit_count (offsets->saved_regs_mask) * 4;
15957       saved = core_saved;
15958
15959       /* We know that SP will be doubleword aligned on entry, and we must
15960          preserve that condition at any subroutine call.  We also require the
15961          soft frame pointer to be doubleword aligned.  */
15962
15963       if (TARGET_REALLY_IWMMXT)
15964         {
15965           /* Check for the call-saved iWMMXt registers.  */
15966           for (regno = FIRST_IWMMXT_REGNUM;
15967                regno <= LAST_IWMMXT_REGNUM;
15968                regno++)
15969             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
15970               saved += 8;
15971         }
15972
15973       func_type = arm_current_func_type ();
15974       if (! IS_VOLATILE (func_type))
15975         {
15976           /* Space for saved FPA registers.  */
15977           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
15978             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
15979             saved += 12;
15980
15981           /* Space for saved VFP registers.  */
15982           if (TARGET_HARD_FLOAT && TARGET_VFP)
15983             saved += arm_get_vfp_saved_size ();
15984         }
15985     }
15986   else /* TARGET_THUMB1 */
15987     {
15988       offsets->saved_regs_mask = thumb1_compute_save_reg_mask ();
15989       core_saved = bit_count (offsets->saved_regs_mask) * 4;
15990       saved = core_saved;
15991       if (TARGET_BACKTRACE)
15992         saved += 16;
15993     }
15994
15995   /* Saved registers include the stack frame.  */
15996   offsets->saved_regs = offsets->saved_args + saved +
15997                         arm_compute_static_chain_stack_bytes();
15998   offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
15999   /* A leaf function does not need any stack alignment if it has nothing
16000      on the stack.  */
16001   if (leaf && frame_size == 0
16002       /* However if it calls alloca(), we have a dynamically allocated
16003          block of BIGGEST_ALIGNMENT on stack, so still do stack alignment.  */
16004       && ! cfun->calls_alloca)
16005     {
16006       offsets->outgoing_args = offsets->soft_frame;
16007       offsets->locals_base = offsets->soft_frame;
16008       return offsets;
16009     }
16010
16011   /* Ensure SFP has the correct alignment.  */
16012   if (ARM_DOUBLEWORD_ALIGN
16013       && (offsets->soft_frame & 7))
16014     {
16015       offsets->soft_frame += 4;
16016       /* Try to align stack by pushing an extra reg.  Don't bother doing this
16017          when there is a stack frame as the alignment will be rolled into
16018          the normal stack adjustment.  */
16019       if (frame_size + crtl->outgoing_args_size == 0)
16020         {
16021           int reg = -1;
16022
16023           /* If it is safe to use r3, then do so.  This sometimes 
16024              generates better code on Thumb-2 by avoiding the need to
16025              use 32-bit push/pop instructions.  */
16026           if (! any_sibcall_uses_r3 ()
16027               && arm_size_return_regs () <= 12
16028               && (offsets->saved_regs_mask & (1 << 3)) == 0)
16029             {
16030               reg = 3;
16031             }
16032           else
16033             for (i = 4; i <= (TARGET_THUMB1 ? LAST_LO_REGNUM : 11); i++)
16034               {
16035                 if ((offsets->saved_regs_mask & (1 << i)) == 0)
16036                   {
16037                     reg = i;
16038                     break;
16039                   }
16040               }
16041
16042           if (reg != -1)
16043             {
16044               offsets->saved_regs += 4;
16045               offsets->saved_regs_mask |= (1 << reg);
16046             }
16047         }
16048     }
16049
16050   offsets->locals_base = offsets->soft_frame + frame_size;
16051   offsets->outgoing_args = (offsets->locals_base
16052                             + crtl->outgoing_args_size);
16053
16054   if (ARM_DOUBLEWORD_ALIGN)
16055     {
16056       /* Ensure SP remains doubleword aligned.  */
16057       if (offsets->outgoing_args & 7)
16058         offsets->outgoing_args += 4;
16059       gcc_assert (!(offsets->outgoing_args & 7));
16060     }
16061
16062   return offsets;
16063 }
16064
16065
16066 /* Calculate the relative offsets for the different stack pointers.  Positive
16067    offsets are in the direction of stack growth.  */
16068
16069 HOST_WIDE_INT
16070 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
16071 {
16072   arm_stack_offsets *offsets;
16073
16074   offsets = arm_get_frame_offsets ();
16075
16076   /* OK, now we have enough information to compute the distances.
16077      There must be an entry in these switch tables for each pair
16078      of registers in ELIMINABLE_REGS, even if some of the entries
16079      seem to be redundant or useless.  */
16080   switch (from)
16081     {
16082     case ARG_POINTER_REGNUM:
16083       switch (to)
16084         {
16085         case THUMB_HARD_FRAME_POINTER_REGNUM:
16086           return 0;
16087
16088         case FRAME_POINTER_REGNUM:
16089           /* This is the reverse of the soft frame pointer
16090              to hard frame pointer elimination below.  */
16091           return offsets->soft_frame - offsets->saved_args;
16092
16093         case ARM_HARD_FRAME_POINTER_REGNUM:
16094           /* This is only non-zero in the case where the static chain register
16095              is stored above the frame.  */
16096           return offsets->frame - offsets->saved_args - 4;
16097
16098         case STACK_POINTER_REGNUM:
16099           /* If nothing has been pushed on the stack at all
16100              then this will return -4.  This *is* correct!  */
16101           return offsets->outgoing_args - (offsets->saved_args + 4);
16102
16103         default:
16104           gcc_unreachable ();
16105         }
16106       gcc_unreachable ();
16107
16108     case FRAME_POINTER_REGNUM:
16109       switch (to)
16110         {
16111         case THUMB_HARD_FRAME_POINTER_REGNUM:
16112           return 0;
16113
16114         case ARM_HARD_FRAME_POINTER_REGNUM:
16115           /* The hard frame pointer points to the top entry in the
16116              stack frame.  The soft frame pointer to the bottom entry
16117              in the stack frame.  If there is no stack frame at all,
16118              then they are identical.  */
16119
16120           return offsets->frame - offsets->soft_frame;
16121
16122         case STACK_POINTER_REGNUM:
16123           return offsets->outgoing_args - offsets->soft_frame;
16124
16125         default:
16126           gcc_unreachable ();
16127         }
16128       gcc_unreachable ();
16129
16130     default:
16131       /* You cannot eliminate from the stack pointer.
16132          In theory you could eliminate from the hard frame
16133          pointer to the stack pointer, but this will never
16134          happen, since if a stack frame is not needed the
16135          hard frame pointer will never be used.  */
16136       gcc_unreachable ();
16137     }
16138 }
16139
16140 /* Given FROM and TO register numbers, say whether this elimination is
16141    allowed.  Frame pointer elimination is automatically handled.
16142
16143    All eliminations are permissible.  Note that ARG_POINTER_REGNUM and
16144    HARD_FRAME_POINTER_REGNUM are in fact the same thing.  If we need a frame
16145    pointer, we must eliminate FRAME_POINTER_REGNUM into
16146    HARD_FRAME_POINTER_REGNUM and not into STACK_POINTER_REGNUM or
16147    ARG_POINTER_REGNUM.  */
16148
16149 bool
16150 arm_can_eliminate (const int from, const int to)
16151 {
16152   return ((to == FRAME_POINTER_REGNUM && from == ARG_POINTER_REGNUM) ? false :
16153           (to == STACK_POINTER_REGNUM && frame_pointer_needed) ? false :
16154           (to == ARM_HARD_FRAME_POINTER_REGNUM && TARGET_THUMB) ? false :
16155           (to == THUMB_HARD_FRAME_POINTER_REGNUM && TARGET_ARM) ? false :
16156            true);
16157 }
16158
16159 /* Emit RTL to save coprocessor registers on function entry.  Returns the
16160    number of bytes pushed.  */
16161
16162 static int
16163 arm_save_coproc_regs(void)
16164 {
16165   int saved_size = 0;
16166   unsigned reg;
16167   unsigned start_reg;
16168   rtx insn;
16169
16170   for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
16171     if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
16172       {
16173         insn = gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx);
16174         insn = gen_rtx_MEM (V2SImode, insn);
16175         insn = emit_set_insn (insn, gen_rtx_REG (V2SImode, reg));
16176         RTX_FRAME_RELATED_P (insn) = 1;
16177         saved_size += 8;
16178       }
16179
16180   /* Save any floating point call-saved registers used by this
16181      function.  */
16182   if (TARGET_FPA_EMU2)
16183     {
16184       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
16185         if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
16186           {
16187             insn = gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx);
16188             insn = gen_rtx_MEM (XFmode, insn);
16189             insn = emit_set_insn (insn, gen_rtx_REG (XFmode, reg));
16190             RTX_FRAME_RELATED_P (insn) = 1;
16191             saved_size += 12;
16192           }
16193     }
16194   else
16195     {
16196       start_reg = LAST_FPA_REGNUM;
16197
16198       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
16199         {
16200           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
16201             {
16202               if (start_reg - reg == 3)
16203                 {
16204                   insn = emit_sfm (reg, 4);
16205                   RTX_FRAME_RELATED_P (insn) = 1;
16206                   saved_size += 48;
16207                   start_reg = reg - 1;
16208                 }
16209             }
16210           else
16211             {
16212               if (start_reg != reg)
16213                 {
16214                   insn = emit_sfm (reg + 1, start_reg - reg);
16215                   RTX_FRAME_RELATED_P (insn) = 1;
16216                   saved_size += (start_reg - reg) * 12;
16217                 }
16218               start_reg = reg - 1;
16219             }
16220         }
16221
16222       if (start_reg != reg)
16223         {
16224           insn = emit_sfm (reg + 1, start_reg - reg);
16225           saved_size += (start_reg - reg) * 12;
16226           RTX_FRAME_RELATED_P (insn) = 1;
16227         }
16228     }
16229   if (TARGET_HARD_FLOAT && TARGET_VFP)
16230     {
16231       start_reg = FIRST_VFP_REGNUM;
16232
16233       for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
16234         {
16235           if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
16236               && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
16237             {
16238               if (start_reg != reg)
16239                 saved_size += vfp_emit_fstmd (start_reg,
16240                                               (reg - start_reg) / 2);
16241               start_reg = reg + 2;
16242             }
16243         }
16244       if (start_reg != reg)
16245         saved_size += vfp_emit_fstmd (start_reg,
16246                                       (reg - start_reg) / 2);
16247     }
16248   return saved_size;
16249 }
16250
16251
16252 /* Set the Thumb frame pointer from the stack pointer.  */
16253
16254 static void
16255 thumb_set_frame_pointer (arm_stack_offsets *offsets)
16256 {
16257   HOST_WIDE_INT amount;
16258   rtx insn, dwarf;
16259
16260   amount = offsets->outgoing_args - offsets->locals_base;
16261   if (amount < 1024)
16262     insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
16263                                   stack_pointer_rtx, GEN_INT (amount)));
16264   else
16265     {
16266       emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
16267       /* Thumb-2 RTL patterns expect sp as the first input.  Thumb-1
16268          expects the first two operands to be the same.  */
16269       if (TARGET_THUMB2)
16270         {
16271           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
16272                                         stack_pointer_rtx,
16273                                         hard_frame_pointer_rtx));
16274         }
16275       else
16276         {
16277           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
16278                                         hard_frame_pointer_rtx,
16279                                         stack_pointer_rtx));
16280         }
16281       dwarf = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
16282                            plus_constant (stack_pointer_rtx, amount));
16283       RTX_FRAME_RELATED_P (dwarf) = 1;
16284       add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
16285     }
16286
16287   RTX_FRAME_RELATED_P (insn) = 1;
16288 }
16289
16290 /* Generate the prologue instructions for entry into an ARM or Thumb-2
16291    function.  */
16292 void
16293 arm_expand_prologue (void)
16294 {
16295   rtx amount;
16296   rtx insn;
16297   rtx ip_rtx;
16298   unsigned long live_regs_mask;
16299   unsigned long func_type;
16300   int fp_offset = 0;
16301   int saved_pretend_args = 0;
16302   int saved_regs = 0;
16303   unsigned HOST_WIDE_INT args_to_push;
16304   arm_stack_offsets *offsets;
16305
16306   func_type = arm_current_func_type ();
16307
16308   /* Naked functions don't have prologues.  */
16309   if (IS_NAKED (func_type))
16310     return;
16311
16312   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
16313   args_to_push = crtl->args.pretend_args_size;
16314
16315   /* Compute which register we will have to save onto the stack.  */
16316   offsets = arm_get_frame_offsets ();
16317   live_regs_mask = offsets->saved_regs_mask;
16318
16319   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
16320
16321   if (IS_STACKALIGN (func_type))
16322     {
16323       rtx r0, r1;
16324
16325       /* Handle a word-aligned stack pointer.  We generate the following:
16326
16327           mov r0, sp
16328           bic r1, r0, #7
16329           mov sp, r1
16330           <save and restore r0 in normal prologue/epilogue>
16331           mov sp, r0
16332           bx lr
16333
16334          The unwinder doesn't need to know about the stack realignment.
16335          Just tell it we saved SP in r0.  */
16336       gcc_assert (TARGET_THUMB2 && !arm_arch_notm && args_to_push == 0);
16337
16338       r0 = gen_rtx_REG (SImode, 0);
16339       r1 = gen_rtx_REG (SImode, 1);
16340
16341       insn = emit_insn (gen_movsi (r0, stack_pointer_rtx));
16342       RTX_FRAME_RELATED_P (insn) = 1;
16343       add_reg_note (insn, REG_CFA_REGISTER, NULL);
16344
16345       emit_insn (gen_andsi3 (r1, r0, GEN_INT (~(HOST_WIDE_INT)7)));
16346
16347       /* ??? The CFA changes here, which may cause GDB to conclude that it
16348          has entered a different function.  That said, the unwind info is
16349          correct, individually, before and after this instruction because
16350          we've described the save of SP, which will override the default
16351          handling of SP as restoring from the CFA.  */
16352       emit_insn (gen_movsi (stack_pointer_rtx, r1));
16353     }
16354
16355   /* For APCS frames, if IP register is clobbered
16356      when creating frame, save that register in a special
16357      way.  */
16358   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
16359     {
16360       if (IS_INTERRUPT (func_type))
16361         {
16362           /* Interrupt functions must not corrupt any registers.
16363              Creating a frame pointer however, corrupts the IP
16364              register, so we must push it first.  */
16365           emit_multi_reg_push (1 << IP_REGNUM);
16366
16367           /* Do not set RTX_FRAME_RELATED_P on this insn.
16368              The dwarf stack unwinding code only wants to see one
16369              stack decrement per function, and this is not it.  If
16370              this instruction is labeled as being part of the frame
16371              creation sequence then dwarf2out_frame_debug_expr will
16372              die when it encounters the assignment of IP to FP
16373              later on, since the use of SP here establishes SP as
16374              the CFA register and not IP.
16375
16376              Anyway this instruction is not really part of the stack
16377              frame creation although it is part of the prologue.  */
16378         }
16379       else if (IS_NESTED (func_type))
16380         {
16381           /* The Static chain register is the same as the IP register
16382              used as a scratch register during stack frame creation.
16383              To get around this need to find somewhere to store IP
16384              whilst the frame is being created.  We try the following
16385              places in order:
16386
16387                1. The last argument register.
16388                2. A slot on the stack above the frame.  (This only
16389                   works if the function is not a varargs function).
16390                3. Register r3, after pushing the argument registers
16391                   onto the stack.
16392
16393              Note - we only need to tell the dwarf2 backend about the SP
16394              adjustment in the second variant; the static chain register
16395              doesn't need to be unwound, as it doesn't contain a value
16396              inherited from the caller.  */
16397
16398           if (df_regs_ever_live_p (3) == false)
16399             insn = emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
16400           else if (args_to_push == 0)
16401             {
16402               rtx dwarf;
16403
16404               gcc_assert(arm_compute_static_chain_stack_bytes() == 4);
16405               saved_regs += 4;
16406
16407               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
16408               insn = emit_set_insn (gen_frame_mem (SImode, insn), ip_rtx);
16409               fp_offset = 4;
16410
16411               /* Just tell the dwarf backend that we adjusted SP.  */
16412               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
16413                                    plus_constant (stack_pointer_rtx,
16414                                                   -fp_offset));
16415               RTX_FRAME_RELATED_P (insn) = 1;
16416               add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
16417             }
16418           else
16419             {
16420               /* Store the args on the stack.  */
16421               if (cfun->machine->uses_anonymous_args)
16422                 insn = emit_multi_reg_push
16423                   ((0xf0 >> (args_to_push / 4)) & 0xf);
16424               else
16425                 insn = emit_insn
16426                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
16427                                GEN_INT (- args_to_push)));
16428
16429               RTX_FRAME_RELATED_P (insn) = 1;
16430
16431               saved_pretend_args = 1;
16432               fp_offset = args_to_push;
16433               args_to_push = 0;
16434
16435               /* Now reuse r3 to preserve IP.  */
16436               emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
16437             }
16438         }
16439
16440       insn = emit_set_insn (ip_rtx,
16441                             plus_constant (stack_pointer_rtx, fp_offset));
16442       RTX_FRAME_RELATED_P (insn) = 1;
16443     }
16444
16445   if (args_to_push)
16446     {
16447       /* Push the argument registers, or reserve space for them.  */
16448       if (cfun->machine->uses_anonymous_args)
16449         insn = emit_multi_reg_push
16450           ((0xf0 >> (args_to_push / 4)) & 0xf);
16451       else
16452         insn = emit_insn
16453           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
16454                        GEN_INT (- args_to_push)));
16455       RTX_FRAME_RELATED_P (insn) = 1;
16456     }
16457
16458   /* If this is an interrupt service routine, and the link register
16459      is going to be pushed, and we're not generating extra
16460      push of IP (needed when frame is needed and frame layout if apcs),
16461      subtracting four from LR now will mean that the function return
16462      can be done with a single instruction.  */
16463   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
16464       && (live_regs_mask & (1 << LR_REGNUM)) != 0
16465       && !(frame_pointer_needed && TARGET_APCS_FRAME)
16466       && TARGET_ARM)
16467     {
16468       rtx lr = gen_rtx_REG (SImode, LR_REGNUM);
16469       
16470       emit_set_insn (lr, plus_constant (lr, -4));
16471     }
16472
16473   if (live_regs_mask)
16474     {
16475       saved_regs += bit_count (live_regs_mask) * 4;
16476       if (optimize_size && !frame_pointer_needed
16477           && saved_regs == offsets->saved_regs - offsets->saved_args)
16478         {
16479           /* If no coprocessor registers are being pushed and we don't have
16480              to worry about a frame pointer then push extra registers to
16481              create the stack frame.  This is done is a way that does not
16482              alter the frame layout, so is independent of the epilogue.  */
16483           int n;
16484           int frame;
16485           n = 0;
16486           while (n < 8 && (live_regs_mask & (1 << n)) == 0)
16487             n++;
16488           frame = offsets->outgoing_args - (offsets->saved_args + saved_regs);
16489           if (frame && n * 4 >= frame)
16490             {
16491               n = frame / 4;
16492               live_regs_mask |= (1 << n) - 1;
16493               saved_regs += frame;
16494             }
16495         }
16496       insn = emit_multi_reg_push (live_regs_mask);
16497       RTX_FRAME_RELATED_P (insn) = 1;
16498     }
16499
16500   if (! IS_VOLATILE (func_type))
16501     saved_regs += arm_save_coproc_regs ();
16502
16503   if (frame_pointer_needed && TARGET_ARM)
16504     {
16505       /* Create the new frame pointer.  */
16506       if (TARGET_APCS_FRAME)
16507         {
16508           insn = GEN_INT (-(4 + args_to_push + fp_offset));
16509           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
16510           RTX_FRAME_RELATED_P (insn) = 1;
16511
16512           if (IS_NESTED (func_type))
16513             {
16514               /* Recover the static chain register.  */
16515               if (!df_regs_ever_live_p (3)
16516                   || saved_pretend_args)
16517                 insn = gen_rtx_REG (SImode, 3);
16518               else /* if (crtl->args.pretend_args_size == 0) */
16519                 {
16520                   insn = plus_constant (hard_frame_pointer_rtx, 4);
16521                   insn = gen_frame_mem (SImode, insn);
16522                 }
16523               emit_set_insn (ip_rtx, insn);
16524               /* Add a USE to stop propagate_one_insn() from barfing.  */
16525               emit_insn (gen_prologue_use (ip_rtx));
16526             }
16527         }
16528       else
16529         {
16530           insn = GEN_INT (saved_regs - 4);
16531           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
16532                                         stack_pointer_rtx, insn));
16533           RTX_FRAME_RELATED_P (insn) = 1;
16534         }
16535     }
16536
16537   if (flag_stack_usage_info)
16538     current_function_static_stack_size
16539       = offsets->outgoing_args - offsets->saved_args;
16540
16541   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
16542     {
16543       /* This add can produce multiple insns for a large constant, so we
16544          need to get tricky.  */
16545       rtx last = get_last_insn ();
16546
16547       amount = GEN_INT (offsets->saved_args + saved_regs
16548                         - offsets->outgoing_args);
16549
16550       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
16551                                     amount));
16552       do
16553         {
16554           last = last ? NEXT_INSN (last) : get_insns ();
16555           RTX_FRAME_RELATED_P (last) = 1;
16556         }
16557       while (last != insn);
16558
16559       /* If the frame pointer is needed, emit a special barrier that
16560          will prevent the scheduler from moving stores to the frame
16561          before the stack adjustment.  */
16562       if (frame_pointer_needed)
16563         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
16564                                          hard_frame_pointer_rtx));
16565     }
16566
16567
16568   if (frame_pointer_needed && TARGET_THUMB2)
16569     thumb_set_frame_pointer (offsets);
16570
16571   if (flag_pic && arm_pic_register != INVALID_REGNUM)
16572     {
16573       unsigned long mask;
16574
16575       mask = live_regs_mask;
16576       mask &= THUMB2_WORK_REGS;
16577       if (!IS_NESTED (func_type))
16578         mask |= (1 << IP_REGNUM);
16579       arm_load_pic_register (mask);
16580     }
16581
16582   /* If we are profiling, make sure no instructions are scheduled before
16583      the call to mcount.  Similarly if the user has requested no
16584      scheduling in the prolog.  Similarly if we want non-call exceptions
16585      using the EABI unwinder, to prevent faulting instructions from being
16586      swapped with a stack adjustment.  */
16587   if (crtl->profile || !TARGET_SCHED_PROLOG
16588       || (arm_except_unwind_info (&global_options) == UI_TARGET
16589           && cfun->can_throw_non_call_exceptions))
16590     emit_insn (gen_blockage ());
16591
16592   /* If the link register is being kept alive, with the return address in it,
16593      then make sure that it does not get reused by the ce2 pass.  */
16594   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
16595     cfun->machine->lr_save_eliminated = 1;
16596 }
16597 \f
16598 /* Print condition code to STREAM.  Helper function for arm_print_operand.  */
16599 static void
16600 arm_print_condition (FILE *stream)
16601 {
16602   if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
16603     {
16604       /* Branch conversion is not implemented for Thumb-2.  */
16605       if (TARGET_THUMB)
16606         {
16607           output_operand_lossage ("predicated Thumb instruction");
16608           return;
16609         }
16610       if (current_insn_predicate != NULL)
16611         {
16612           output_operand_lossage
16613             ("predicated instruction in conditional sequence");
16614           return;
16615         }
16616
16617       fputs (arm_condition_codes[arm_current_cc], stream);
16618     }
16619   else if (current_insn_predicate)
16620     {
16621       enum arm_cond_code code;
16622
16623       if (TARGET_THUMB1)
16624         {
16625           output_operand_lossage ("predicated Thumb instruction");
16626           return;
16627         }
16628
16629       code = get_arm_condition_code (current_insn_predicate);
16630       fputs (arm_condition_codes[code], stream);
16631     }
16632 }
16633
16634
16635 /* If CODE is 'd', then the X is a condition operand and the instruction
16636    should only be executed if the condition is true.
16637    if CODE is 'D', then the X is a condition operand and the instruction
16638    should only be executed if the condition is false: however, if the mode
16639    of the comparison is CCFPEmode, then always execute the instruction -- we
16640    do this because in these circumstances !GE does not necessarily imply LT;
16641    in these cases the instruction pattern will take care to make sure that
16642    an instruction containing %d will follow, thereby undoing the effects of
16643    doing this instruction unconditionally.
16644    If CODE is 'N' then X is a floating point operand that must be negated
16645    before output.
16646    If CODE is 'B' then output a bitwise inverted value of X (a const int).
16647    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
16648 static void
16649 arm_print_operand (FILE *stream, rtx x, int code)
16650 {
16651   switch (code)
16652     {
16653     case '@':
16654       fputs (ASM_COMMENT_START, stream);
16655       return;
16656
16657     case '_':
16658       fputs (user_label_prefix, stream);
16659       return;
16660
16661     case '|':
16662       fputs (REGISTER_PREFIX, stream);
16663       return;
16664
16665     case '?':
16666       arm_print_condition (stream);
16667       return;
16668
16669     case '(':
16670       /* Nothing in unified syntax, otherwise the current condition code.  */
16671       if (!TARGET_UNIFIED_ASM)
16672         arm_print_condition (stream);
16673       break;
16674
16675     case ')':
16676       /* The current condition code in unified syntax, otherwise nothing.  */
16677       if (TARGET_UNIFIED_ASM)
16678         arm_print_condition (stream);
16679       break;
16680   
16681     case '.':
16682       /* The current condition code for a condition code setting instruction.
16683          Preceded by 's' in unified syntax, otherwise followed by 's'.  */
16684       if (TARGET_UNIFIED_ASM)
16685         {
16686           fputc('s', stream);
16687           arm_print_condition (stream);
16688         }
16689       else
16690         {
16691           arm_print_condition (stream);
16692           fputc('s', stream);
16693         }
16694       return;
16695
16696     case '!':
16697       /* If the instruction is conditionally executed then print
16698          the current condition code, otherwise print 's'.  */
16699       gcc_assert (TARGET_THUMB2 && TARGET_UNIFIED_ASM);
16700       if (current_insn_predicate)
16701         arm_print_condition (stream);
16702       else
16703         fputc('s', stream);
16704       break;
16705
16706     /* %# is a "break" sequence. It doesn't output anything, but is used to
16707        separate e.g. operand numbers from following text, if that text consists
16708        of further digits which we don't want to be part of the operand
16709        number.  */
16710     case '#':
16711       return;
16712
16713     case 'N':
16714       {
16715         REAL_VALUE_TYPE r;
16716         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
16717         r = real_value_negate (&r);
16718         fprintf (stream, "%s", fp_const_from_val (&r));
16719       }
16720       return;
16721
16722     /* An integer or symbol address without a preceding # sign.  */
16723     case 'c':
16724       switch (GET_CODE (x))
16725         {
16726         case CONST_INT:
16727           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
16728           break;
16729
16730         case SYMBOL_REF:
16731           output_addr_const (stream, x);
16732           break;
16733
16734         case CONST:
16735           if (GET_CODE (XEXP (x, 0)) == PLUS
16736               && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)
16737             {
16738               output_addr_const (stream, x);
16739               break;
16740             }
16741           /* Fall through.  */
16742
16743         default:
16744           output_operand_lossage ("Unsupported operand for code '%c'", code);
16745         }
16746       return;
16747
16748     case 'B':
16749       if (GET_CODE (x) == CONST_INT)
16750         {
16751           HOST_WIDE_INT val;
16752           val = ARM_SIGN_EXTEND (~INTVAL (x));
16753           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
16754         }
16755       else
16756         {
16757           putc ('~', stream);
16758           output_addr_const (stream, x);
16759         }
16760       return;
16761
16762     case 'L':
16763       /* The low 16 bits of an immediate constant.  */
16764       fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL(x) & 0xffff);
16765       return;
16766
16767     case 'i':
16768       fprintf (stream, "%s", arithmetic_instr (x, 1));
16769       return;
16770
16771     /* Truncate Cirrus shift counts.  */
16772     case 's':
16773       if (GET_CODE (x) == CONST_INT)
16774         {
16775           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
16776           return;
16777         }
16778       arm_print_operand (stream, x, 0);
16779       return;
16780
16781     case 'I':
16782       fprintf (stream, "%s", arithmetic_instr (x, 0));
16783       return;
16784
16785     case 'S':
16786       {
16787         HOST_WIDE_INT val;
16788         const char *shift;
16789
16790         if (!shift_operator (x, SImode))
16791           {
16792             output_operand_lossage ("invalid shift operand");
16793             break;
16794           }
16795
16796         shift = shift_op (x, &val);
16797
16798         if (shift)
16799           {
16800             fprintf (stream, ", %s ", shift);
16801             if (val == -1)
16802               arm_print_operand (stream, XEXP (x, 1), 0);
16803             else
16804               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
16805           }
16806       }
16807       return;
16808
16809       /* An explanation of the 'Q', 'R' and 'H' register operands:
16810
16811          In a pair of registers containing a DI or DF value the 'Q'
16812          operand returns the register number of the register containing
16813          the least significant part of the value.  The 'R' operand returns
16814          the register number of the register containing the most
16815          significant part of the value.
16816
16817          The 'H' operand returns the higher of the two register numbers.
16818          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
16819          same as the 'Q' operand, since the most significant part of the
16820          value is held in the lower number register.  The reverse is true
16821          on systems where WORDS_BIG_ENDIAN is false.
16822
16823          The purpose of these operands is to distinguish between cases
16824          where the endian-ness of the values is important (for example
16825          when they are added together), and cases where the endian-ness
16826          is irrelevant, but the order of register operations is important.
16827          For example when loading a value from memory into a register
16828          pair, the endian-ness does not matter.  Provided that the value
16829          from the lower memory address is put into the lower numbered
16830          register, and the value from the higher address is put into the
16831          higher numbered register, the load will work regardless of whether
16832          the value being loaded is big-wordian or little-wordian.  The
16833          order of the two register loads can matter however, if the address
16834          of the memory location is actually held in one of the registers
16835          being overwritten by the load.
16836
16837          The 'Q' and 'R' constraints are also available for 64-bit
16838          constants.  */
16839     case 'Q':
16840       if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
16841         {
16842           rtx part = gen_lowpart (SImode, x);
16843           fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, INTVAL (part));
16844           return;
16845         }
16846
16847       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16848         {
16849           output_operand_lossage ("invalid operand for code '%c'", code);
16850           return;
16851         }
16852
16853       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
16854       return;
16855
16856     case 'R':
16857       if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
16858         {
16859           enum machine_mode mode = GET_MODE (x);
16860           rtx part;
16861
16862           if (mode == VOIDmode)
16863             mode = DImode;
16864           part = gen_highpart_mode (SImode, mode, x);
16865           fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, INTVAL (part));
16866           return;
16867         }
16868
16869       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16870         {
16871           output_operand_lossage ("invalid operand for code '%c'", code);
16872           return;
16873         }
16874
16875       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
16876       return;
16877
16878     case 'H':
16879       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16880         {
16881           output_operand_lossage ("invalid operand for code '%c'", code);
16882           return;
16883         }
16884
16885       asm_fprintf (stream, "%r", REGNO (x) + 1);
16886       return;
16887
16888     case 'J':
16889       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16890         {
16891           output_operand_lossage ("invalid operand for code '%c'", code);
16892           return;
16893         }
16894
16895       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 3 : 2));
16896       return;
16897
16898     case 'K':
16899       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16900         {
16901           output_operand_lossage ("invalid operand for code '%c'", code);
16902           return;
16903         }
16904
16905       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 2 : 3));
16906       return;
16907
16908     case 'm':
16909       asm_fprintf (stream, "%r",
16910                    GET_CODE (XEXP (x, 0)) == REG
16911                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
16912       return;
16913
16914     case 'M':
16915       asm_fprintf (stream, "{%r-%r}",
16916                    REGNO (x),
16917                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
16918       return;
16919
16920     /* Like 'M', but writing doubleword vector registers, for use by Neon
16921        insns.  */
16922     case 'h':
16923       {
16924         int regno = (REGNO (x) - FIRST_VFP_REGNUM) / 2;
16925         int numregs = ARM_NUM_REGS (GET_MODE (x)) / 2;
16926         if (numregs == 1)
16927           asm_fprintf (stream, "{d%d}", regno);
16928         else
16929           asm_fprintf (stream, "{d%d-d%d}", regno, regno + numregs - 1);
16930       }
16931       return;
16932
16933     case 'd':
16934       /* CONST_TRUE_RTX means always -- that's the default.  */
16935       if (x == const_true_rtx)
16936         return;
16937
16938       if (!COMPARISON_P (x))
16939         {
16940           output_operand_lossage ("invalid operand for code '%c'", code);
16941           return;
16942         }
16943
16944       fputs (arm_condition_codes[get_arm_condition_code (x)],
16945              stream);
16946       return;
16947
16948     case 'D':
16949       /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
16950          want to do that.  */
16951       if (x == const_true_rtx)
16952         {
16953           output_operand_lossage ("instruction never executed");
16954           return;
16955         }
16956       if (!COMPARISON_P (x))
16957         {
16958           output_operand_lossage ("invalid operand for code '%c'", code);
16959           return;
16960         }
16961
16962       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
16963                                  (get_arm_condition_code (x))],
16964              stream);
16965       return;
16966
16967     /* Cirrus registers can be accessed in a variety of ways:
16968          single floating point (f)
16969          double floating point (d)
16970          32bit integer         (fx)
16971          64bit integer         (dx).  */
16972     case 'W':                   /* Cirrus register in F mode.  */
16973     case 'X':                   /* Cirrus register in D mode.  */
16974     case 'Y':                   /* Cirrus register in FX mode.  */
16975     case 'Z':                   /* Cirrus register in DX mode.  */
16976       gcc_assert (GET_CODE (x) == REG
16977                   && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
16978
16979       fprintf (stream, "mv%s%s",
16980                code == 'W' ? "f"
16981                : code == 'X' ? "d"
16982                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
16983
16984       return;
16985
16986     /* Print cirrus register in the mode specified by the register's mode.  */
16987     case 'V':
16988       {
16989         int mode = GET_MODE (x);
16990
16991         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
16992           {
16993             output_operand_lossage ("invalid operand for code '%c'", code);
16994             return;
16995           }
16996
16997         fprintf (stream, "mv%s%s",
16998                  mode == DFmode ? "d"
16999                  : mode == SImode ? "fx"
17000                  : mode == DImode ? "dx"
17001                  : "f", reg_names[REGNO (x)] + 2);
17002
17003         return;
17004       }
17005
17006     case 'U':
17007       if (GET_CODE (x) != REG
17008           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
17009           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
17010         /* Bad value for wCG register number.  */
17011         {
17012           output_operand_lossage ("invalid operand for code '%c'", code);
17013           return;
17014         }
17015
17016       else
17017         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
17018       return;
17019
17020       /* Print an iWMMXt control register name.  */
17021     case 'w':
17022       if (GET_CODE (x) != CONST_INT
17023           || INTVAL (x) < 0
17024           || INTVAL (x) >= 16)
17025         /* Bad value for wC register number.  */
17026         {
17027           output_operand_lossage ("invalid operand for code '%c'", code);
17028           return;
17029         }
17030
17031       else
17032         {
17033           static const char * wc_reg_names [16] =
17034             {
17035               "wCID",  "wCon",  "wCSSF", "wCASF",
17036               "wC4",   "wC5",   "wC6",   "wC7",
17037               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
17038               "wC12",  "wC13",  "wC14",  "wC15"
17039             };
17040
17041           fprintf (stream, wc_reg_names [INTVAL (x)]);
17042         }
17043       return;
17044
17045     /* Print the high single-precision register of a VFP double-precision
17046        register.  */
17047     case 'p':
17048       {
17049         int mode = GET_MODE (x);
17050         int regno;
17051
17052         if (GET_MODE_SIZE (mode) != 8 || GET_CODE (x) != REG)
17053           {
17054             output_operand_lossage ("invalid operand for code '%c'", code);
17055             return;
17056           }
17057
17058         regno = REGNO (x);
17059         if (!VFP_REGNO_OK_FOR_DOUBLE (regno))
17060           {
17061             output_operand_lossage ("invalid operand for code '%c'", code);
17062             return;
17063           }
17064
17065         fprintf (stream, "s%d", regno - FIRST_VFP_REGNUM + 1);
17066       }
17067       return;
17068
17069     /* Print a VFP/Neon double precision or quad precision register name.  */
17070     case 'P':
17071     case 'q':
17072       {
17073         int mode = GET_MODE (x);
17074         int is_quad = (code == 'q');
17075         int regno;
17076
17077         if (GET_MODE_SIZE (mode) != (is_quad ? 16 : 8))
17078           {
17079             output_operand_lossage ("invalid operand for code '%c'", code);
17080             return;
17081           }
17082
17083         if (GET_CODE (x) != REG
17084             || !IS_VFP_REGNUM (REGNO (x)))
17085           {
17086             output_operand_lossage ("invalid operand for code '%c'", code);
17087             return;
17088           }
17089
17090         regno = REGNO (x);
17091         if ((is_quad && !NEON_REGNO_OK_FOR_QUAD (regno))
17092             || (!is_quad && !VFP_REGNO_OK_FOR_DOUBLE (regno)))
17093           {
17094             output_operand_lossage ("invalid operand for code '%c'", code);
17095             return;
17096           }
17097
17098         fprintf (stream, "%c%d", is_quad ? 'q' : 'd',
17099           (regno - FIRST_VFP_REGNUM) >> (is_quad ? 2 : 1));
17100       }
17101       return;
17102
17103     /* These two codes print the low/high doubleword register of a Neon quad
17104        register, respectively.  For pair-structure types, can also print
17105        low/high quadword registers.  */
17106     case 'e':
17107     case 'f':
17108       {
17109         int mode = GET_MODE (x);
17110         int regno;
17111
17112         if ((GET_MODE_SIZE (mode) != 16
17113              && GET_MODE_SIZE (mode) != 32) || GET_CODE (x) != REG)
17114           {
17115             output_operand_lossage ("invalid operand for code '%c'", code);
17116             return;
17117           }
17118
17119         regno = REGNO (x);
17120         if (!NEON_REGNO_OK_FOR_QUAD (regno))
17121           {
17122             output_operand_lossage ("invalid operand for code '%c'", code);
17123             return;
17124           }
17125
17126         if (GET_MODE_SIZE (mode) == 16)
17127           fprintf (stream, "d%d", ((regno - FIRST_VFP_REGNUM) >> 1)
17128                                   + (code == 'f' ? 1 : 0));
17129         else
17130           fprintf (stream, "q%d", ((regno - FIRST_VFP_REGNUM) >> 2)
17131                                   + (code == 'f' ? 1 : 0));
17132       }
17133       return;
17134
17135     /* Print a VFPv3 floating-point constant, represented as an integer
17136        index.  */
17137     case 'G':
17138       {
17139         int index = vfp3_const_double_index (x);
17140         gcc_assert (index != -1);
17141         fprintf (stream, "%d", index);
17142       }
17143       return;
17144
17145     /* Print bits representing opcode features for Neon.
17146
17147        Bit 0 is 1 for signed, 0 for unsigned.  Floats count as signed
17148        and polynomials as unsigned.
17149
17150        Bit 1 is 1 for floats and polynomials, 0 for ordinary integers.
17151
17152        Bit 2 is 1 for rounding functions, 0 otherwise.  */
17153
17154     /* Identify the type as 's', 'u', 'p' or 'f'.  */
17155     case 'T':
17156       {
17157         HOST_WIDE_INT bits = INTVAL (x);
17158         fputc ("uspf"[bits & 3], stream);
17159       }
17160       return;
17161
17162     /* Likewise, but signed and unsigned integers are both 'i'.  */
17163     case 'F':
17164       {
17165         HOST_WIDE_INT bits = INTVAL (x);
17166         fputc ("iipf"[bits & 3], stream);
17167       }
17168       return;
17169
17170     /* As for 'T', but emit 'u' instead of 'p'.  */
17171     case 't':
17172       {
17173         HOST_WIDE_INT bits = INTVAL (x);
17174         fputc ("usuf"[bits & 3], stream);
17175       }
17176       return;
17177
17178     /* Bit 2: rounding (vs none).  */
17179     case 'O':
17180       {
17181         HOST_WIDE_INT bits = INTVAL (x);
17182         fputs ((bits & 4) != 0 ? "r" : "", stream);
17183       }
17184       return;
17185
17186     /* Memory operand for vld1/vst1 instruction.  */
17187     case 'A':
17188       {
17189         rtx addr;
17190         bool postinc = FALSE;
17191         unsigned align, memsize, align_bits;
17192
17193         gcc_assert (GET_CODE (x) == MEM);
17194         addr = XEXP (x, 0);
17195         if (GET_CODE (addr) == POST_INC)
17196           {
17197             postinc = 1;
17198             addr = XEXP (addr, 0);
17199           }
17200         asm_fprintf (stream, "[%r", REGNO (addr));
17201
17202         /* We know the alignment of this access, so we can emit a hint in the
17203            instruction (for some alignments) as an aid to the memory subsystem
17204            of the target.  */
17205         align = MEM_ALIGN (x) >> 3;
17206         memsize = MEM_SIZE (x);
17207         
17208         /* Only certain alignment specifiers are supported by the hardware.  */
17209         if (memsize == 16 && (align % 32) == 0)
17210           align_bits = 256;
17211         else if ((memsize == 8 || memsize == 16) && (align % 16) == 0)
17212           align_bits = 128;
17213         else if ((align % 8) == 0)
17214           align_bits = 64;
17215         else
17216           align_bits = 0;
17217         
17218         if (align_bits != 0)
17219           asm_fprintf (stream, ":%d", align_bits);
17220
17221         asm_fprintf (stream, "]");
17222
17223         if (postinc)
17224           fputs("!", stream);
17225       }
17226       return;
17227
17228     case 'C':
17229       {
17230         rtx addr;
17231
17232         gcc_assert (GET_CODE (x) == MEM);
17233         addr = XEXP (x, 0);
17234         gcc_assert (GET_CODE (addr) == REG);
17235         asm_fprintf (stream, "[%r]", REGNO (addr));
17236       }
17237       return;
17238
17239     /* Translate an S register number into a D register number and element index.  */
17240     case 'y':
17241       {
17242         int mode = GET_MODE (x);
17243         int regno;
17244
17245         if (GET_MODE_SIZE (mode) != 4 || GET_CODE (x) != REG)
17246           {
17247             output_operand_lossage ("invalid operand for code '%c'", code);
17248             return;
17249           }
17250
17251         regno = REGNO (x);
17252         if (!VFP_REGNO_OK_FOR_SINGLE (regno))
17253           {
17254             output_operand_lossage ("invalid operand for code '%c'", code);
17255             return;
17256           }
17257
17258         regno = regno - FIRST_VFP_REGNUM;
17259         fprintf (stream, "d%d[%d]", regno / 2, regno % 2);
17260       }
17261       return;
17262
17263     /* Register specifier for vld1.16/vst1.16.  Translate the S register
17264        number into a D register number and element index.  */
17265     case 'z':
17266       {
17267         int mode = GET_MODE (x);
17268         int regno;
17269
17270         if (GET_MODE_SIZE (mode) != 2 || GET_CODE (x) != REG)
17271           {
17272             output_operand_lossage ("invalid operand for code '%c'", code);
17273             return;
17274           }
17275
17276         regno = REGNO (x);
17277         if (!VFP_REGNO_OK_FOR_SINGLE (regno))
17278           {
17279             output_operand_lossage ("invalid operand for code '%c'", code);
17280             return;
17281           }
17282
17283         regno = regno - FIRST_VFP_REGNUM;
17284         fprintf (stream, "d%d[%d]", regno/2, ((regno % 2) ? 2 : 0));
17285       }
17286       return;
17287       
17288     default:
17289       if (x == 0)
17290         {
17291           output_operand_lossage ("missing operand");
17292           return;
17293         }
17294
17295       switch (GET_CODE (x))
17296         {
17297         case REG:
17298           asm_fprintf (stream, "%r", REGNO (x));
17299           break;
17300
17301         case MEM:
17302           output_memory_reference_mode = GET_MODE (x);
17303           output_address (XEXP (x, 0));
17304           break;
17305
17306         case CONST_DOUBLE:
17307           if (TARGET_NEON)
17308             {
17309               char fpstr[20];
17310               real_to_decimal (fpstr, CONST_DOUBLE_REAL_VALUE (x),
17311                                sizeof (fpstr), 0, 1);
17312               fprintf (stream, "#%s", fpstr);
17313             }
17314           else
17315             fprintf (stream, "#%s", fp_immediate_constant (x));
17316           break;
17317
17318         default:
17319           gcc_assert (GET_CODE (x) != NEG);
17320           fputc ('#', stream);
17321           if (GET_CODE (x) == HIGH)
17322             {
17323               fputs (":lower16:", stream);
17324               x = XEXP (x, 0);
17325             }
17326             
17327           output_addr_const (stream, x);
17328           break;
17329         }
17330     }
17331 }
17332 \f
17333 /* Target hook for printing a memory address.  */
17334 static void
17335 arm_print_operand_address (FILE *stream, rtx x)
17336 {
17337   if (TARGET_32BIT)
17338     {
17339       int is_minus = GET_CODE (x) == MINUS;
17340
17341       if (GET_CODE (x) == REG)
17342         asm_fprintf (stream, "[%r, #0]", REGNO (x));
17343       else if (GET_CODE (x) == PLUS || is_minus)
17344         {
17345           rtx base = XEXP (x, 0);
17346           rtx index = XEXP (x, 1);
17347           HOST_WIDE_INT offset = 0;
17348           if (GET_CODE (base) != REG
17349               || (GET_CODE (index) == REG && REGNO (index) == SP_REGNUM))
17350             {
17351               /* Ensure that BASE is a register.  */
17352               /* (one of them must be).  */
17353               /* Also ensure the SP is not used as in index register.  */
17354               rtx temp = base;
17355               base = index;
17356               index = temp;
17357             }
17358           switch (GET_CODE (index))
17359             {
17360             case CONST_INT:
17361               offset = INTVAL (index);
17362               if (is_minus)
17363                 offset = -offset;
17364               asm_fprintf (stream, "[%r, #%wd]",
17365                            REGNO (base), offset);
17366               break;
17367
17368             case REG:
17369               asm_fprintf (stream, "[%r, %s%r]",
17370                            REGNO (base), is_minus ? "-" : "",
17371                            REGNO (index));
17372               break;
17373
17374             case MULT:
17375             case ASHIFTRT:
17376             case LSHIFTRT:
17377             case ASHIFT:
17378             case ROTATERT:
17379               {
17380                 asm_fprintf (stream, "[%r, %s%r",
17381                              REGNO (base), is_minus ? "-" : "",
17382                              REGNO (XEXP (index, 0)));
17383                 arm_print_operand (stream, index, 'S');
17384                 fputs ("]", stream);
17385                 break;
17386               }
17387
17388             default:
17389               gcc_unreachable ();
17390             }
17391         }
17392       else if (GET_CODE (x) == PRE_INC || GET_CODE (x) == POST_INC
17393                || GET_CODE (x) == PRE_DEC || GET_CODE (x) == POST_DEC)
17394         {
17395           extern enum machine_mode output_memory_reference_mode;
17396
17397           gcc_assert (GET_CODE (XEXP (x, 0)) == REG);
17398
17399           if (GET_CODE (x) == PRE_DEC || GET_CODE (x) == PRE_INC)
17400             asm_fprintf (stream, "[%r, #%s%d]!",
17401                          REGNO (XEXP (x, 0)),
17402                          GET_CODE (x) == PRE_DEC ? "-" : "",
17403                          GET_MODE_SIZE (output_memory_reference_mode));
17404           else
17405             asm_fprintf (stream, "[%r], #%s%d",
17406                          REGNO (XEXP (x, 0)),
17407                          GET_CODE (x) == POST_DEC ? "-" : "",
17408                          GET_MODE_SIZE (output_memory_reference_mode));
17409         }
17410       else if (GET_CODE (x) == PRE_MODIFY)
17411         {
17412           asm_fprintf (stream, "[%r, ", REGNO (XEXP (x, 0)));
17413           if (GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
17414             asm_fprintf (stream, "#%wd]!",
17415                          INTVAL (XEXP (XEXP (x, 1), 1)));
17416           else
17417             asm_fprintf (stream, "%r]!",
17418                          REGNO (XEXP (XEXP (x, 1), 1)));
17419         }
17420       else if (GET_CODE (x) == POST_MODIFY)
17421         {
17422           asm_fprintf (stream, "[%r], ", REGNO (XEXP (x, 0)));
17423           if (GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
17424             asm_fprintf (stream, "#%wd",
17425                          INTVAL (XEXP (XEXP (x, 1), 1)));
17426           else
17427             asm_fprintf (stream, "%r",
17428                          REGNO (XEXP (XEXP (x, 1), 1)));
17429         }
17430       else output_addr_const (stream, x);
17431     }
17432   else
17433     {
17434       if (GET_CODE (x) == REG)
17435         asm_fprintf (stream, "[%r]", REGNO (x));
17436       else if (GET_CODE (x) == POST_INC)
17437         asm_fprintf (stream, "%r!", REGNO (XEXP (x, 0)));
17438       else if (GET_CODE (x) == PLUS)
17439         {
17440           gcc_assert (GET_CODE (XEXP (x, 0)) == REG);
17441           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
17442             asm_fprintf (stream, "[%r, #%wd]",
17443                          REGNO (XEXP (x, 0)),
17444                          INTVAL (XEXP (x, 1)));
17445           else
17446             asm_fprintf (stream, "[%r, %r]",
17447                          REGNO (XEXP (x, 0)),
17448                          REGNO (XEXP (x, 1)));
17449         }
17450       else
17451         output_addr_const (stream, x);
17452     }
17453 }
17454 \f
17455 /* Target hook for indicating whether a punctuation character for
17456    TARGET_PRINT_OPERAND is valid.  */
17457 static bool
17458 arm_print_operand_punct_valid_p (unsigned char code)
17459 {
17460   return (code == '@' || code == '|' || code == '.'
17461           || code == '(' || code == ')' || code == '#'
17462           || (TARGET_32BIT && (code == '?'))
17463           || (TARGET_THUMB2 && (code == '!'))
17464           || (TARGET_THUMB && (code == '_')));
17465 }
17466 \f
17467 /* Target hook for assembling integer objects.  The ARM version needs to
17468    handle word-sized values specially.  */
17469 static bool
17470 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
17471 {
17472   enum machine_mode mode;
17473
17474   if (size == UNITS_PER_WORD && aligned_p)
17475     {
17476       fputs ("\t.word\t", asm_out_file);
17477       output_addr_const (asm_out_file, x);
17478
17479       /* Mark symbols as position independent.  We only do this in the
17480          .text segment, not in the .data segment.  */
17481       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
17482           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
17483         {
17484           /* See legitimize_pic_address for an explanation of the
17485              TARGET_VXWORKS_RTP check.  */
17486           if (TARGET_VXWORKS_RTP
17487               || (GET_CODE (x) == SYMBOL_REF && !SYMBOL_REF_LOCAL_P (x)))
17488             fputs ("(GOT)", asm_out_file);
17489           else
17490             fputs ("(GOTOFF)", asm_out_file);
17491         }
17492       fputc ('\n', asm_out_file);
17493       return true;
17494     }
17495
17496   mode = GET_MODE (x);
17497
17498   if (arm_vector_mode_supported_p (mode))
17499     {
17500       int i, units;
17501
17502       gcc_assert (GET_CODE (x) == CONST_VECTOR);
17503
17504       units = CONST_VECTOR_NUNITS (x);
17505       size = GET_MODE_SIZE (GET_MODE_INNER (mode));
17506
17507       if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
17508         for (i = 0; i < units; i++)
17509           {
17510             rtx elt = CONST_VECTOR_ELT (x, i);
17511             assemble_integer
17512               (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
17513           }
17514       else
17515         for (i = 0; i < units; i++)
17516           {
17517             rtx elt = CONST_VECTOR_ELT (x, i);
17518             REAL_VALUE_TYPE rval;
17519
17520             REAL_VALUE_FROM_CONST_DOUBLE (rval, elt);
17521
17522             assemble_real
17523               (rval, GET_MODE_INNER (mode),
17524               i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT);
17525           }
17526
17527       return true;
17528     }
17529
17530   return default_assemble_integer (x, size, aligned_p);
17531 }
17532
17533 static void
17534 arm_elf_asm_cdtor (rtx symbol, int priority, bool is_ctor)
17535 {
17536   section *s;
17537
17538   if (!TARGET_AAPCS_BASED)
17539     {
17540       (is_ctor ? 
17541        default_named_section_asm_out_constructor 
17542        : default_named_section_asm_out_destructor) (symbol, priority);
17543       return;
17544     }
17545
17546   /* Put these in the .init_array section, using a special relocation.  */
17547   if (priority != DEFAULT_INIT_PRIORITY)
17548     {
17549       char buf[18];
17550       sprintf (buf, "%s.%.5u", 
17551                is_ctor ? ".init_array" : ".fini_array",
17552                priority);
17553       s = get_section (buf, SECTION_WRITE, NULL_TREE);
17554     }
17555   else if (is_ctor)
17556     s = ctors_section;
17557   else
17558     s = dtors_section;
17559
17560   switch_to_section (s);
17561   assemble_align (POINTER_SIZE);
17562   fputs ("\t.word\t", asm_out_file);
17563   output_addr_const (asm_out_file, symbol);
17564   fputs ("(target1)\n", asm_out_file);
17565 }
17566
17567 /* Add a function to the list of static constructors.  */
17568
17569 static void
17570 arm_elf_asm_constructor (rtx symbol, int priority)
17571 {
17572   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/true);
17573 }
17574
17575 /* Add a function to the list of static destructors.  */
17576
17577 static void
17578 arm_elf_asm_destructor (rtx symbol, int priority)
17579 {
17580   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/false);
17581 }
17582 \f
17583 /* A finite state machine takes care of noticing whether or not instructions
17584    can be conditionally executed, and thus decrease execution time and code
17585    size by deleting branch instructions.  The fsm is controlled by
17586    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
17587
17588 /* The state of the fsm controlling condition codes are:
17589    0: normal, do nothing special
17590    1: make ASM_OUTPUT_OPCODE not output this instruction
17591    2: make ASM_OUTPUT_OPCODE not output this instruction
17592    3: make instructions conditional
17593    4: make instructions conditional
17594
17595    State transitions (state->state by whom under condition):
17596    0 -> 1 final_prescan_insn if the `target' is a label
17597    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
17598    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
17599    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
17600    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
17601           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
17602    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
17603           (the target insn is arm_target_insn).
17604
17605    If the jump clobbers the conditions then we use states 2 and 4.
17606
17607    A similar thing can be done with conditional return insns.
17608
17609    XXX In case the `target' is an unconditional branch, this conditionalising
17610    of the instructions always reduces code size, but not always execution
17611    time.  But then, I want to reduce the code size to somewhere near what
17612    /bin/cc produces.  */
17613
17614 /* In addition to this, state is maintained for Thumb-2 COND_EXEC
17615    instructions.  When a COND_EXEC instruction is seen the subsequent
17616    instructions are scanned so that multiple conditional instructions can be
17617    combined into a single IT block.  arm_condexec_count and arm_condexec_mask
17618    specify the length and true/false mask for the IT block.  These will be
17619    decremented/zeroed by arm_asm_output_opcode as the insns are output.  */
17620
17621 /* Returns the index of the ARM condition code string in
17622    `arm_condition_codes', or ARM_NV if the comparison is invalid.
17623    COMPARISON should be an rtx like `(eq (...) (...))'.  */
17624 enum arm_cond_code
17625 maybe_get_arm_condition_code (rtx comparison)
17626 {
17627   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
17628   enum arm_cond_code code;
17629   enum rtx_code comp_code = GET_CODE (comparison);
17630
17631   if (GET_MODE_CLASS (mode) != MODE_CC)
17632     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
17633                            XEXP (comparison, 1));
17634
17635   switch (mode)
17636     {
17637     case CC_DNEmode: code = ARM_NE; goto dominance;
17638     case CC_DEQmode: code = ARM_EQ; goto dominance;
17639     case CC_DGEmode: code = ARM_GE; goto dominance;
17640     case CC_DGTmode: code = ARM_GT; goto dominance;
17641     case CC_DLEmode: code = ARM_LE; goto dominance;
17642     case CC_DLTmode: code = ARM_LT; goto dominance;
17643     case CC_DGEUmode: code = ARM_CS; goto dominance;
17644     case CC_DGTUmode: code = ARM_HI; goto dominance;
17645     case CC_DLEUmode: code = ARM_LS; goto dominance;
17646     case CC_DLTUmode: code = ARM_CC;
17647
17648     dominance:
17649       if (comp_code == EQ)
17650         return ARM_INVERSE_CONDITION_CODE (code);
17651       if (comp_code == NE)
17652         return code;
17653       return ARM_NV;
17654
17655     case CC_NOOVmode:
17656       switch (comp_code)
17657         {
17658         case NE: return ARM_NE;
17659         case EQ: return ARM_EQ;
17660         case GE: return ARM_PL;
17661         case LT: return ARM_MI;
17662         default: return ARM_NV;
17663         }
17664
17665     case CC_Zmode:
17666       switch (comp_code)
17667         {
17668         case NE: return ARM_NE;
17669         case EQ: return ARM_EQ;
17670         default: return ARM_NV;
17671         }
17672
17673     case CC_Nmode:
17674       switch (comp_code)
17675         {
17676         case NE: return ARM_MI;
17677         case EQ: return ARM_PL;
17678         default: return ARM_NV;
17679         }
17680
17681     case CCFPEmode:
17682     case CCFPmode:
17683       /* These encodings assume that AC=1 in the FPA system control
17684          byte.  This allows us to handle all cases except UNEQ and
17685          LTGT.  */
17686       switch (comp_code)
17687         {
17688         case GE: return ARM_GE;
17689         case GT: return ARM_GT;
17690         case LE: return ARM_LS;
17691         case LT: return ARM_MI;
17692         case NE: return ARM_NE;
17693         case EQ: return ARM_EQ;
17694         case ORDERED: return ARM_VC;
17695         case UNORDERED: return ARM_VS;
17696         case UNLT: return ARM_LT;
17697         case UNLE: return ARM_LE;
17698         case UNGT: return ARM_HI;
17699         case UNGE: return ARM_PL;
17700           /* UNEQ and LTGT do not have a representation.  */
17701         case UNEQ: /* Fall through.  */
17702         case LTGT: /* Fall through.  */
17703         default: return ARM_NV;
17704         }
17705
17706     case CC_SWPmode:
17707       switch (comp_code)
17708         {
17709         case NE: return ARM_NE;
17710         case EQ: return ARM_EQ;
17711         case GE: return ARM_LE;
17712         case GT: return ARM_LT;
17713         case LE: return ARM_GE;
17714         case LT: return ARM_GT;
17715         case GEU: return ARM_LS;
17716         case GTU: return ARM_CC;
17717         case LEU: return ARM_CS;
17718         case LTU: return ARM_HI;
17719         default: return ARM_NV;
17720         }
17721
17722     case CC_Cmode:
17723       switch (comp_code)
17724         {
17725         case LTU: return ARM_CS;
17726         case GEU: return ARM_CC;
17727         default: return ARM_NV;
17728         }
17729
17730     case CC_CZmode:
17731       switch (comp_code)
17732         {
17733         case NE: return ARM_NE;
17734         case EQ: return ARM_EQ;
17735         case GEU: return ARM_CS;
17736         case GTU: return ARM_HI;
17737         case LEU: return ARM_LS;
17738         case LTU: return ARM_CC;
17739         default: return ARM_NV;
17740         }
17741
17742     case CC_NCVmode:
17743       switch (comp_code)
17744         {
17745         case GE: return ARM_GE;
17746         case LT: return ARM_LT;
17747         case GEU: return ARM_CS;
17748         case LTU: return ARM_CC;
17749         default: return ARM_NV;
17750         }
17751
17752     case CCmode:
17753       switch (comp_code)
17754         {
17755         case NE: return ARM_NE;
17756         case EQ: return ARM_EQ;
17757         case GE: return ARM_GE;
17758         case GT: return ARM_GT;
17759         case LE: return ARM_LE;
17760         case LT: return ARM_LT;
17761         case GEU: return ARM_CS;
17762         case GTU: return ARM_HI;
17763         case LEU: return ARM_LS;
17764         case LTU: return ARM_CC;
17765         default: return ARM_NV;
17766         }
17767
17768     default: gcc_unreachable ();
17769     }
17770 }
17771
17772 /* Like maybe_get_arm_condition_code, but never return ARM_NV.  */
17773 static enum arm_cond_code
17774 get_arm_condition_code (rtx comparison)
17775 {
17776   enum arm_cond_code code = maybe_get_arm_condition_code (comparison);
17777   gcc_assert (code != ARM_NV);
17778   return code;
17779 }
17780
17781 /* Tell arm_asm_output_opcode to output IT blocks for conditionally executed
17782    instructions.  */
17783 void
17784 thumb2_final_prescan_insn (rtx insn)
17785 {
17786   rtx first_insn = insn;
17787   rtx body = PATTERN (insn);
17788   rtx predicate;
17789   enum arm_cond_code code;
17790   int n;
17791   int mask;
17792
17793   /* Remove the previous insn from the count of insns to be output.  */
17794   if (arm_condexec_count)
17795       arm_condexec_count--;
17796
17797   /* Nothing to do if we are already inside a conditional block.  */
17798   if (arm_condexec_count)
17799     return;
17800
17801   if (GET_CODE (body) != COND_EXEC)
17802     return;
17803
17804   /* Conditional jumps are implemented directly.  */
17805   if (GET_CODE (insn) == JUMP_INSN)
17806     return;
17807
17808   predicate = COND_EXEC_TEST (body);
17809   arm_current_cc = get_arm_condition_code (predicate);
17810
17811   n = get_attr_ce_count (insn);
17812   arm_condexec_count = 1;
17813   arm_condexec_mask = (1 << n) - 1;
17814   arm_condexec_masklen = n;
17815   /* See if subsequent instructions can be combined into the same block.  */
17816   for (;;)
17817     {
17818       insn = next_nonnote_insn (insn);
17819
17820       /* Jumping into the middle of an IT block is illegal, so a label or
17821          barrier terminates the block.  */
17822       if (GET_CODE (insn) != INSN && GET_CODE(insn) != JUMP_INSN)
17823         break;
17824
17825       body = PATTERN (insn);
17826       /* USE and CLOBBER aren't really insns, so just skip them.  */
17827       if (GET_CODE (body) == USE
17828           || GET_CODE (body) == CLOBBER)
17829         continue;
17830
17831       /* ??? Recognize conditional jumps, and combine them with IT blocks.  */
17832       if (GET_CODE (body) != COND_EXEC)
17833         break;
17834       /* Allow up to 4 conditionally executed instructions in a block.  */
17835       n = get_attr_ce_count (insn);
17836       if (arm_condexec_masklen + n > 4)
17837         break;
17838
17839       predicate = COND_EXEC_TEST (body);
17840       code = get_arm_condition_code (predicate);
17841       mask = (1 << n) - 1;
17842       if (arm_current_cc == code)
17843         arm_condexec_mask |= (mask << arm_condexec_masklen);
17844       else if (arm_current_cc != ARM_INVERSE_CONDITION_CODE(code))
17845         break;
17846
17847       arm_condexec_count++;
17848       arm_condexec_masklen += n;
17849
17850       /* A jump must be the last instruction in a conditional block.  */
17851       if (GET_CODE(insn) == JUMP_INSN)
17852         break;
17853     }
17854   /* Restore recog_data (getting the attributes of other insns can
17855      destroy this array, but final.c assumes that it remains intact
17856      across this call).  */
17857   extract_constrain_insn_cached (first_insn);
17858 }
17859
17860 void
17861 arm_final_prescan_insn (rtx insn)
17862 {
17863   /* BODY will hold the body of INSN.  */
17864   rtx body = PATTERN (insn);
17865
17866   /* This will be 1 if trying to repeat the trick, and things need to be
17867      reversed if it appears to fail.  */
17868   int reverse = 0;
17869
17870   /* If we start with a return insn, we only succeed if we find another one.  */
17871   int seeking_return = 0;
17872   enum rtx_code return_code = UNKNOWN;
17873
17874   /* START_INSN will hold the insn from where we start looking.  This is the
17875      first insn after the following code_label if REVERSE is true.  */
17876   rtx start_insn = insn;
17877
17878   /* If in state 4, check if the target branch is reached, in order to
17879      change back to state 0.  */
17880   if (arm_ccfsm_state == 4)
17881     {
17882       if (insn == arm_target_insn)
17883         {
17884           arm_target_insn = NULL;
17885           arm_ccfsm_state = 0;
17886         }
17887       return;
17888     }
17889
17890   /* If in state 3, it is possible to repeat the trick, if this insn is an
17891      unconditional branch to a label, and immediately following this branch
17892      is the previous target label which is only used once, and the label this
17893      branch jumps to is not too far off.  */
17894   if (arm_ccfsm_state == 3)
17895     {
17896       if (simplejump_p (insn))
17897         {
17898           start_insn = next_nonnote_insn (start_insn);
17899           if (GET_CODE (start_insn) == BARRIER)
17900             {
17901               /* XXX Isn't this always a barrier?  */
17902               start_insn = next_nonnote_insn (start_insn);
17903             }
17904           if (GET_CODE (start_insn) == CODE_LABEL
17905               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
17906               && LABEL_NUSES (start_insn) == 1)
17907             reverse = TRUE;
17908           else
17909             return;
17910         }
17911       else if (ANY_RETURN_P (body))
17912         {
17913           start_insn = next_nonnote_insn (start_insn);
17914           if (GET_CODE (start_insn) == BARRIER)
17915             start_insn = next_nonnote_insn (start_insn);
17916           if (GET_CODE (start_insn) == CODE_LABEL
17917               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
17918               && LABEL_NUSES (start_insn) == 1)
17919             {
17920               reverse = TRUE;
17921               seeking_return = 1;
17922               return_code = GET_CODE (body);
17923             }
17924           else
17925             return;
17926         }
17927       else
17928         return;
17929     }
17930
17931   gcc_assert (!arm_ccfsm_state || reverse);
17932   if (GET_CODE (insn) != JUMP_INSN)
17933     return;
17934
17935   /* This jump might be paralleled with a clobber of the condition codes
17936      the jump should always come first */
17937   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
17938     body = XVECEXP (body, 0, 0);
17939
17940   if (reverse
17941       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
17942           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
17943     {
17944       int insns_skipped;
17945       int fail = FALSE, succeed = FALSE;
17946       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
17947       int then_not_else = TRUE;
17948       rtx this_insn = start_insn, label = 0;
17949
17950       /* Register the insn jumped to.  */
17951       if (reverse)
17952         {
17953           if (!seeking_return)
17954             label = XEXP (SET_SRC (body), 0);
17955         }
17956       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
17957         label = XEXP (XEXP (SET_SRC (body), 1), 0);
17958       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
17959         {
17960           label = XEXP (XEXP (SET_SRC (body), 2), 0);
17961           then_not_else = FALSE;
17962         }
17963       else if (ANY_RETURN_P (XEXP (SET_SRC (body), 1)))
17964         {
17965           seeking_return = 1;
17966           return_code = GET_CODE (XEXP (SET_SRC (body), 1));
17967         }
17968       else if (ANY_RETURN_P (XEXP (SET_SRC (body), 2)))
17969         {
17970           seeking_return = 1;
17971           return_code = GET_CODE (XEXP (SET_SRC (body), 2));
17972           then_not_else = FALSE;
17973         }
17974       else
17975         gcc_unreachable ();
17976
17977       /* See how many insns this branch skips, and what kind of insns.  If all
17978          insns are okay, and the label or unconditional branch to the same
17979          label is not too far away, succeed.  */
17980       for (insns_skipped = 0;
17981            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
17982         {
17983           rtx scanbody;
17984
17985           this_insn = next_nonnote_insn (this_insn);
17986           if (!this_insn)
17987             break;
17988
17989           switch (GET_CODE (this_insn))
17990             {
17991             case CODE_LABEL:
17992               /* Succeed if it is the target label, otherwise fail since
17993                  control falls in from somewhere else.  */
17994               if (this_insn == label)
17995                 {
17996                   arm_ccfsm_state = 1;
17997                   succeed = TRUE;
17998                 }
17999               else
18000                 fail = TRUE;
18001               break;
18002
18003             case BARRIER:
18004               /* Succeed if the following insn is the target label.
18005                  Otherwise fail.
18006                  If return insns are used then the last insn in a function
18007                  will be a barrier.  */
18008               this_insn = next_nonnote_insn (this_insn);
18009               if (this_insn && this_insn == label)
18010                 {
18011                   arm_ccfsm_state = 1;
18012                   succeed = TRUE;
18013                 }
18014               else
18015                 fail = TRUE;
18016               break;
18017
18018             case CALL_INSN:
18019               /* The AAPCS says that conditional calls should not be
18020                  used since they make interworking inefficient (the
18021                  linker can't transform BL<cond> into BLX).  That's
18022                  only a problem if the machine has BLX.  */
18023               if (arm_arch5)
18024                 {
18025                   fail = TRUE;
18026                   break;
18027                 }
18028
18029               /* Succeed if the following insn is the target label, or
18030                  if the following two insns are a barrier and the
18031                  target label.  */
18032               this_insn = next_nonnote_insn (this_insn);
18033               if (this_insn && GET_CODE (this_insn) == BARRIER)
18034                 this_insn = next_nonnote_insn (this_insn);
18035
18036               if (this_insn && this_insn == label
18037                   && insns_skipped < max_insns_skipped)
18038                 {
18039                   arm_ccfsm_state = 1;
18040                   succeed = TRUE;
18041                 }
18042               else
18043                 fail = TRUE;
18044               break;
18045
18046             case JUMP_INSN:
18047               /* If this is an unconditional branch to the same label, succeed.
18048                  If it is to another label, do nothing.  If it is conditional,
18049                  fail.  */
18050               /* XXX Probably, the tests for SET and the PC are
18051                  unnecessary.  */
18052
18053               scanbody = PATTERN (this_insn);
18054               if (GET_CODE (scanbody) == SET
18055                   && GET_CODE (SET_DEST (scanbody)) == PC)
18056                 {
18057                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
18058                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
18059                     {
18060                       arm_ccfsm_state = 2;
18061                       succeed = TRUE;
18062                     }
18063                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
18064                     fail = TRUE;
18065                 }
18066               /* Fail if a conditional return is undesirable (e.g. on a
18067                  StrongARM), but still allow this if optimizing for size.  */
18068               else if (GET_CODE (scanbody) == return_code
18069                        && !use_return_insn (TRUE, NULL)
18070                        && !optimize_size)
18071                 fail = TRUE;
18072               else if (GET_CODE (scanbody) == return_code)
18073                 {
18074                   arm_ccfsm_state = 2;
18075                   succeed = TRUE;
18076                 }
18077               else if (GET_CODE (scanbody) == PARALLEL)
18078                 {
18079                   switch (get_attr_conds (this_insn))
18080                     {
18081                     case CONDS_NOCOND:
18082                       break;
18083                     default:
18084                       fail = TRUE;
18085                       break;
18086                     }
18087                 }
18088               else
18089                 fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
18090
18091               break;
18092
18093             case INSN:
18094               /* Instructions using or affecting the condition codes make it
18095                  fail.  */
18096               scanbody = PATTERN (this_insn);
18097               if (!(GET_CODE (scanbody) == SET
18098                     || GET_CODE (scanbody) == PARALLEL)
18099                   || get_attr_conds (this_insn) != CONDS_NOCOND)
18100                 fail = TRUE;
18101
18102               /* A conditional cirrus instruction must be followed by
18103                  a non Cirrus instruction.  However, since we
18104                  conditionalize instructions in this function and by
18105                  the time we get here we can't add instructions
18106                  (nops), because shorten_branches() has already been
18107                  called, we will disable conditionalizing Cirrus
18108                  instructions to be safe.  */
18109               if (GET_CODE (scanbody) != USE
18110                   && GET_CODE (scanbody) != CLOBBER
18111                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
18112                 fail = TRUE;
18113               break;
18114
18115             default:
18116               break;
18117             }
18118         }
18119       if (succeed)
18120         {
18121           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
18122             arm_target_label = CODE_LABEL_NUMBER (label);
18123           else
18124             {
18125               gcc_assert (seeking_return || arm_ccfsm_state == 2);
18126
18127               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
18128                 {
18129                   this_insn = next_nonnote_insn (this_insn);
18130                   gcc_assert (!this_insn
18131                               || (GET_CODE (this_insn) != BARRIER
18132                                   && GET_CODE (this_insn) != CODE_LABEL));
18133                 }
18134               if (!this_insn)
18135                 {
18136                   /* Oh, dear! we ran off the end.. give up.  */
18137                   extract_constrain_insn_cached (insn);
18138                   arm_ccfsm_state = 0;
18139                   arm_target_insn = NULL;
18140                   return;
18141                 }
18142               arm_target_insn = this_insn;
18143             }
18144
18145           /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
18146              what it was.  */
18147           if (!reverse)
18148             arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body), 0));
18149
18150           if (reverse || then_not_else)
18151             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
18152         }
18153
18154       /* Restore recog_data (getting the attributes of other insns can
18155          destroy this array, but final.c assumes that it remains intact
18156          across this call.  */
18157       extract_constrain_insn_cached (insn);
18158     }
18159 }
18160
18161 /* Output IT instructions.  */
18162 void
18163 thumb2_asm_output_opcode (FILE * stream)
18164 {
18165   char buff[5];
18166   int n;
18167
18168   if (arm_condexec_mask)
18169     {
18170       for (n = 0; n < arm_condexec_masklen; n++)
18171         buff[n] = (arm_condexec_mask & (1 << n)) ? 't' : 'e';
18172       buff[n] = 0;
18173       asm_fprintf(stream, "i%s\t%s\n\t", buff,
18174                   arm_condition_codes[arm_current_cc]);
18175       arm_condexec_mask = 0;
18176     }
18177 }
18178
18179 /* Returns true if REGNO is a valid register
18180    for holding a quantity of type MODE.  */
18181 int
18182 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
18183 {
18184   if (GET_MODE_CLASS (mode) == MODE_CC)
18185     return (regno == CC_REGNUM
18186             || (TARGET_HARD_FLOAT && TARGET_VFP
18187                 && regno == VFPCC_REGNUM));
18188
18189   if (TARGET_THUMB1)
18190     /* For the Thumb we only allow values bigger than SImode in
18191        registers 0 - 6, so that there is always a second low
18192        register available to hold the upper part of the value.
18193        We probably we ought to ensure that the register is the
18194        start of an even numbered register pair.  */
18195     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
18196
18197   if (TARGET_HARD_FLOAT && TARGET_MAVERICK
18198       && IS_CIRRUS_REGNUM (regno))
18199     /* We have outlawed SI values in Cirrus registers because they
18200        reside in the lower 32 bits, but SF values reside in the
18201        upper 32 bits.  This causes gcc all sorts of grief.  We can't
18202        even split the registers into pairs because Cirrus SI values
18203        get sign extended to 64bits-- aldyh.  */
18204     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
18205
18206   if (TARGET_HARD_FLOAT && TARGET_VFP
18207       && IS_VFP_REGNUM (regno))
18208     {
18209       if (mode == SFmode || mode == SImode)
18210         return VFP_REGNO_OK_FOR_SINGLE (regno);
18211
18212       if (mode == DFmode)
18213         return VFP_REGNO_OK_FOR_DOUBLE (regno);
18214
18215       /* VFP registers can hold HFmode values, but there is no point in
18216          putting them there unless we have hardware conversion insns. */
18217       if (mode == HFmode)
18218         return TARGET_FP16 && VFP_REGNO_OK_FOR_SINGLE (regno);
18219
18220       if (TARGET_NEON)
18221         return (VALID_NEON_DREG_MODE (mode) && VFP_REGNO_OK_FOR_DOUBLE (regno))
18222                || (VALID_NEON_QREG_MODE (mode)
18223                    && NEON_REGNO_OK_FOR_QUAD (regno))
18224                || (mode == TImode && NEON_REGNO_OK_FOR_NREGS (regno, 2))
18225                || (mode == EImode && NEON_REGNO_OK_FOR_NREGS (regno, 3))
18226                || (mode == OImode && NEON_REGNO_OK_FOR_NREGS (regno, 4))
18227                || (mode == CImode && NEON_REGNO_OK_FOR_NREGS (regno, 6))
18228                || (mode == XImode && NEON_REGNO_OK_FOR_NREGS (regno, 8));
18229
18230       return FALSE;
18231     }
18232
18233   if (TARGET_REALLY_IWMMXT)
18234     {
18235       if (IS_IWMMXT_GR_REGNUM (regno))
18236         return mode == SImode;
18237
18238       if (IS_IWMMXT_REGNUM (regno))
18239         return VALID_IWMMXT_REG_MODE (mode);
18240     }
18241   
18242   /* We allow almost any value to be stored in the general registers.
18243      Restrict doubleword quantities to even register pairs so that we can
18244      use ldrd.  Do not allow very large Neon structure opaque modes in
18245      general registers; they would use too many.  */
18246   if (regno <= LAST_ARM_REGNUM)
18247     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0)
18248       && ARM_NUM_REGS (mode) <= 4;
18249
18250   if (regno == FRAME_POINTER_REGNUM
18251       || regno == ARG_POINTER_REGNUM)
18252     /* We only allow integers in the fake hard registers.  */
18253     return GET_MODE_CLASS (mode) == MODE_INT;
18254
18255   /* The only registers left are the FPA registers
18256      which we only allow to hold FP values.  */
18257   return (TARGET_HARD_FLOAT && TARGET_FPA
18258           && GET_MODE_CLASS (mode) == MODE_FLOAT
18259           && regno >= FIRST_FPA_REGNUM
18260           && regno <= LAST_FPA_REGNUM);
18261 }
18262
18263 /* For efficiency and historical reasons LO_REGS, HI_REGS and CC_REGS are
18264    not used in arm mode.  */
18265
18266 enum reg_class
18267 arm_regno_class (int regno)
18268 {
18269   if (TARGET_THUMB1)
18270     {
18271       if (regno == STACK_POINTER_REGNUM)
18272         return STACK_REG;
18273       if (regno == CC_REGNUM)
18274         return CC_REG;
18275       if (regno < 8)
18276         return LO_REGS;
18277       return HI_REGS;
18278     }
18279
18280   if (TARGET_THUMB2 && regno < 8)
18281     return LO_REGS;
18282
18283   if (   regno <= LAST_ARM_REGNUM
18284       || regno == FRAME_POINTER_REGNUM
18285       || regno == ARG_POINTER_REGNUM)
18286     return TARGET_THUMB2 ? HI_REGS : GENERAL_REGS;
18287
18288   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
18289     return TARGET_THUMB2 ? CC_REG : NO_REGS;
18290
18291   if (IS_CIRRUS_REGNUM (regno))
18292     return CIRRUS_REGS;
18293
18294   if (IS_VFP_REGNUM (regno))
18295     {
18296       if (regno <= D7_VFP_REGNUM)
18297         return VFP_D0_D7_REGS;
18298       else if (regno <= LAST_LO_VFP_REGNUM)
18299         return VFP_LO_REGS;
18300       else
18301         return VFP_HI_REGS;
18302     }
18303
18304   if (IS_IWMMXT_REGNUM (regno))
18305     return IWMMXT_REGS;
18306
18307   if (IS_IWMMXT_GR_REGNUM (regno))
18308     return IWMMXT_GR_REGS;
18309
18310   return FPA_REGS;
18311 }
18312
18313 /* Handle a special case when computing the offset
18314    of an argument from the frame pointer.  */
18315 int
18316 arm_debugger_arg_offset (int value, rtx addr)
18317 {
18318   rtx insn;
18319
18320   /* We are only interested if dbxout_parms() failed to compute the offset.  */
18321   if (value != 0)
18322     return 0;
18323
18324   /* We can only cope with the case where the address is held in a register.  */
18325   if (GET_CODE (addr) != REG)
18326     return 0;
18327
18328   /* If we are using the frame pointer to point at the argument, then
18329      an offset of 0 is correct.  */
18330   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
18331     return 0;
18332
18333   /* If we are using the stack pointer to point at the
18334      argument, then an offset of 0 is correct.  */
18335   /* ??? Check this is consistent with thumb2 frame layout.  */
18336   if ((TARGET_THUMB || !frame_pointer_needed)
18337       && REGNO (addr) == SP_REGNUM)
18338     return 0;
18339
18340   /* Oh dear.  The argument is pointed to by a register rather
18341      than being held in a register, or being stored at a known
18342      offset from the frame pointer.  Since GDB only understands
18343      those two kinds of argument we must translate the address
18344      held in the register into an offset from the frame pointer.
18345      We do this by searching through the insns for the function
18346      looking to see where this register gets its value.  If the
18347      register is initialized from the frame pointer plus an offset
18348      then we are in luck and we can continue, otherwise we give up.
18349
18350      This code is exercised by producing debugging information
18351      for a function with arguments like this:
18352
18353            double func (double a, double b, int c, double d) {return d;}
18354
18355      Without this code the stab for parameter 'd' will be set to
18356      an offset of 0 from the frame pointer, rather than 8.  */
18357
18358   /* The if() statement says:
18359
18360      If the insn is a normal instruction
18361      and if the insn is setting the value in a register
18362      and if the register being set is the register holding the address of the argument
18363      and if the address is computing by an addition
18364      that involves adding to a register
18365      which is the frame pointer
18366      a constant integer
18367
18368      then...  */
18369
18370   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
18371     {
18372       if (   GET_CODE (insn) == INSN
18373           && GET_CODE (PATTERN (insn)) == SET
18374           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
18375           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
18376           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
18377           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
18378           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
18379              )
18380         {
18381           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
18382
18383           break;
18384         }
18385     }
18386
18387   if (value == 0)
18388     {
18389       debug_rtx (addr);
18390       warning (0, "unable to compute real location of stacked parameter");
18391       value = 8; /* XXX magic hack */
18392     }
18393
18394   return value;
18395 }
18396 \f
18397 typedef enum {
18398   T_V8QI,
18399   T_V4HI,
18400   T_V2SI,
18401   T_V2SF,
18402   T_DI,
18403   T_V16QI,
18404   T_V8HI,
18405   T_V4SI,
18406   T_V4SF,
18407   T_V2DI,
18408   T_TI,
18409   T_EI,
18410   T_OI,
18411   T_MAX         /* Size of enum.  Keep last.  */
18412 } neon_builtin_type_mode;
18413
18414 #define TYPE_MODE_BIT(X) (1 << (X))
18415
18416 #define TB_DREG (TYPE_MODE_BIT (T_V8QI) | TYPE_MODE_BIT (T_V4HI)        \
18417                  | TYPE_MODE_BIT (T_V2SI) | TYPE_MODE_BIT (T_V2SF)      \
18418                  | TYPE_MODE_BIT (T_DI))
18419 #define TB_QREG (TYPE_MODE_BIT (T_V16QI) | TYPE_MODE_BIT (T_V8HI)       \
18420                  | TYPE_MODE_BIT (T_V4SI) | TYPE_MODE_BIT (T_V4SF)      \
18421                  | TYPE_MODE_BIT (T_V2DI) | TYPE_MODE_BIT (T_TI))
18422
18423 #define v8qi_UP  T_V8QI
18424 #define v4hi_UP  T_V4HI
18425 #define v2si_UP  T_V2SI
18426 #define v2sf_UP  T_V2SF
18427 #define di_UP    T_DI
18428 #define v16qi_UP T_V16QI
18429 #define v8hi_UP  T_V8HI
18430 #define v4si_UP  T_V4SI
18431 #define v4sf_UP  T_V4SF
18432 #define v2di_UP  T_V2DI
18433 #define ti_UP    T_TI
18434 #define ei_UP    T_EI
18435 #define oi_UP    T_OI
18436
18437 #define UP(X) X##_UP
18438
18439 typedef enum {
18440   NEON_BINOP,
18441   NEON_TERNOP,
18442   NEON_UNOP,
18443   NEON_GETLANE,
18444   NEON_SETLANE,
18445   NEON_CREATE,
18446   NEON_DUP,
18447   NEON_DUPLANE,
18448   NEON_COMBINE,
18449   NEON_SPLIT,
18450   NEON_LANEMUL,
18451   NEON_LANEMULL,
18452   NEON_LANEMULH,
18453   NEON_LANEMAC,
18454   NEON_SCALARMUL,
18455   NEON_SCALARMULL,
18456   NEON_SCALARMULH,
18457   NEON_SCALARMAC,
18458   NEON_CONVERT,
18459   NEON_FIXCONV,
18460   NEON_SELECT,
18461   NEON_RESULTPAIR,
18462   NEON_REINTERP,
18463   NEON_VTBL,
18464   NEON_VTBX,
18465   NEON_LOAD1,
18466   NEON_LOAD1LANE,
18467   NEON_STORE1,
18468   NEON_STORE1LANE,
18469   NEON_LOADSTRUCT,
18470   NEON_LOADSTRUCTLANE,
18471   NEON_STORESTRUCT,
18472   NEON_STORESTRUCTLANE,
18473   NEON_LOGICBINOP,
18474   NEON_SHIFTINSERT,
18475   NEON_SHIFTIMM,
18476   NEON_SHIFTACC
18477 } neon_itype;
18478
18479 typedef struct {
18480   const char *name;
18481   const neon_itype itype;
18482   const neon_builtin_type_mode mode;
18483   const enum insn_code code;
18484   unsigned int fcode;
18485 } neon_builtin_datum;
18486
18487 #define CF(N,X) CODE_FOR_neon_##N##X
18488
18489 #define VAR1(T, N, A) \
18490   {#N, NEON_##T, UP (A), CF (N, A), 0}
18491 #define VAR2(T, N, A, B) \
18492   VAR1 (T, N, A), \
18493   {#N, NEON_##T, UP (B), CF (N, B), 0}
18494 #define VAR3(T, N, A, B, C) \
18495   VAR2 (T, N, A, B), \
18496   {#N, NEON_##T, UP (C), CF (N, C), 0}
18497 #define VAR4(T, N, A, B, C, D) \
18498   VAR3 (T, N, A, B, C), \
18499   {#N, NEON_##T, UP (D), CF (N, D), 0}
18500 #define VAR5(T, N, A, B, C, D, E) \
18501   VAR4 (T, N, A, B, C, D), \
18502   {#N, NEON_##T, UP (E), CF (N, E), 0}
18503 #define VAR6(T, N, A, B, C, D, E, F) \
18504   VAR5 (T, N, A, B, C, D, E), \
18505   {#N, NEON_##T, UP (F), CF (N, F), 0}
18506 #define VAR7(T, N, A, B, C, D, E, F, G) \
18507   VAR6 (T, N, A, B, C, D, E, F), \
18508   {#N, NEON_##T, UP (G), CF (N, G), 0}
18509 #define VAR8(T, N, A, B, C, D, E, F, G, H) \
18510   VAR7 (T, N, A, B, C, D, E, F, G), \
18511   {#N, NEON_##T, UP (H), CF (N, H), 0}
18512 #define VAR9(T, N, A, B, C, D, E, F, G, H, I) \
18513   VAR8 (T, N, A, B, C, D, E, F, G, H), \
18514   {#N, NEON_##T, UP (I), CF (N, I), 0}
18515 #define VAR10(T, N, A, B, C, D, E, F, G, H, I, J) \
18516   VAR9 (T, N, A, B, C, D, E, F, G, H, I), \
18517   {#N, NEON_##T, UP (J), CF (N, J), 0}
18518
18519 /* The mode entries in the following table correspond to the "key" type of the
18520    instruction variant, i.e. equivalent to that which would be specified after
18521    the assembler mnemonic, which usually refers to the last vector operand.
18522    (Signed/unsigned/polynomial types are not differentiated between though, and
18523    are all mapped onto the same mode for a given element size.) The modes
18524    listed per instruction should be the same as those defined for that
18525    instruction's pattern in neon.md.  */
18526
18527 static neon_builtin_datum neon_builtin_data[] =
18528 {
18529   VAR10 (BINOP, vadd,
18530          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18531   VAR3 (BINOP, vaddl, v8qi, v4hi, v2si),
18532   VAR3 (BINOP, vaddw, v8qi, v4hi, v2si),
18533   VAR6 (BINOP, vhadd, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
18534   VAR8 (BINOP, vqadd, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
18535   VAR3 (BINOP, vaddhn, v8hi, v4si, v2di),
18536   VAR8 (BINOP, vmul, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
18537   VAR8 (TERNOP, vmla, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
18538   VAR3 (TERNOP, vmlal, v8qi, v4hi, v2si),
18539   VAR8 (TERNOP, vmls, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
18540   VAR3 (TERNOP, vmlsl, v8qi, v4hi, v2si),
18541   VAR4 (BINOP, vqdmulh, v4hi, v2si, v8hi, v4si),
18542   VAR2 (TERNOP, vqdmlal, v4hi, v2si),
18543   VAR2 (TERNOP, vqdmlsl, v4hi, v2si),
18544   VAR3 (BINOP, vmull, v8qi, v4hi, v2si),
18545   VAR2 (SCALARMULL, vmull_n, v4hi, v2si),
18546   VAR2 (LANEMULL, vmull_lane, v4hi, v2si),
18547   VAR2 (SCALARMULL, vqdmull_n, v4hi, v2si),
18548   VAR2 (LANEMULL, vqdmull_lane, v4hi, v2si),
18549   VAR4 (SCALARMULH, vqdmulh_n, v4hi, v2si, v8hi, v4si),
18550   VAR4 (LANEMULH, vqdmulh_lane, v4hi, v2si, v8hi, v4si),
18551   VAR2 (BINOP, vqdmull, v4hi, v2si),
18552   VAR8 (BINOP, vshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
18553   VAR8 (BINOP, vqshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
18554   VAR8 (SHIFTIMM, vshr_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
18555   VAR3 (SHIFTIMM, vshrn_n, v8hi, v4si, v2di),
18556   VAR3 (SHIFTIMM, vqshrn_n, v8hi, v4si, v2di),
18557   VAR3 (SHIFTIMM, vqshrun_n, v8hi, v4si, v2di),
18558   VAR8 (SHIFTIMM, vshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
18559   VAR8 (SHIFTIMM, vqshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
18560   VAR8 (SHIFTIMM, vqshlu_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
18561   VAR3 (SHIFTIMM, vshll_n, v8qi, v4hi, v2si),
18562   VAR8 (SHIFTACC, vsra_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
18563   VAR10 (BINOP, vsub,
18564          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18565   VAR3 (BINOP, vsubl, v8qi, v4hi, v2si),
18566   VAR3 (BINOP, vsubw, v8qi, v4hi, v2si),
18567   VAR8 (BINOP, vqsub, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
18568   VAR6 (BINOP, vhsub, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
18569   VAR3 (BINOP, vsubhn, v8hi, v4si, v2di),
18570   VAR8 (BINOP, vceq, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
18571   VAR8 (BINOP, vcge, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
18572   VAR8 (BINOP, vcgt, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
18573   VAR2 (BINOP, vcage, v2sf, v4sf),
18574   VAR2 (BINOP, vcagt, v2sf, v4sf),
18575   VAR6 (BINOP, vtst, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
18576   VAR8 (BINOP, vabd, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
18577   VAR3 (BINOP, vabdl, v8qi, v4hi, v2si),
18578   VAR6 (TERNOP, vaba, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
18579   VAR3 (TERNOP, vabal, v8qi, v4hi, v2si),
18580   VAR8 (BINOP, vmax, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
18581   VAR8 (BINOP, vmin, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
18582   VAR4 (BINOP, vpadd, v8qi, v4hi, v2si, v2sf),
18583   VAR6 (UNOP, vpaddl, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
18584   VAR6 (BINOP, vpadal, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
18585   VAR4 (BINOP, vpmax, v8qi, v4hi, v2si, v2sf),
18586   VAR4 (BINOP, vpmin, v8qi, v4hi, v2si, v2sf),
18587   VAR2 (BINOP, vrecps, v2sf, v4sf),
18588   VAR2 (BINOP, vrsqrts, v2sf, v4sf),
18589   VAR8 (SHIFTINSERT, vsri_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
18590   VAR8 (SHIFTINSERT, vsli_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
18591   VAR8 (UNOP, vabs, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
18592   VAR6 (UNOP, vqabs, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
18593   VAR8 (UNOP, vneg, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
18594   VAR6 (UNOP, vqneg, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
18595   VAR6 (UNOP, vcls, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
18596   VAR6 (UNOP, vclz, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
18597   VAR2 (UNOP, vcnt, v8qi, v16qi),
18598   VAR4 (UNOP, vrecpe, v2si, v2sf, v4si, v4sf),
18599   VAR4 (UNOP, vrsqrte, v2si, v2sf, v4si, v4sf),
18600   VAR6 (UNOP, vmvn, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
18601   /* FIXME: vget_lane supports more variants than this!  */
18602   VAR10 (GETLANE, vget_lane,
18603          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18604   VAR10 (SETLANE, vset_lane,
18605          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18606   VAR5 (CREATE, vcreate, v8qi, v4hi, v2si, v2sf, di),
18607   VAR10 (DUP, vdup_n,
18608          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18609   VAR10 (DUPLANE, vdup_lane,
18610          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18611   VAR5 (COMBINE, vcombine, v8qi, v4hi, v2si, v2sf, di),
18612   VAR5 (SPLIT, vget_high, v16qi, v8hi, v4si, v4sf, v2di),
18613   VAR5 (SPLIT, vget_low, v16qi, v8hi, v4si, v4sf, v2di),
18614   VAR3 (UNOP, vmovn, v8hi, v4si, v2di),
18615   VAR3 (UNOP, vqmovn, v8hi, v4si, v2di),
18616   VAR3 (UNOP, vqmovun, v8hi, v4si, v2di),
18617   VAR3 (UNOP, vmovl, v8qi, v4hi, v2si),
18618   VAR6 (LANEMUL, vmul_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18619   VAR6 (LANEMAC, vmla_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18620   VAR2 (LANEMAC, vmlal_lane, v4hi, v2si),
18621   VAR2 (LANEMAC, vqdmlal_lane, v4hi, v2si),
18622   VAR6 (LANEMAC, vmls_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18623   VAR2 (LANEMAC, vmlsl_lane, v4hi, v2si),
18624   VAR2 (LANEMAC, vqdmlsl_lane, v4hi, v2si),
18625   VAR6 (SCALARMUL, vmul_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18626   VAR6 (SCALARMAC, vmla_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18627   VAR2 (SCALARMAC, vmlal_n, v4hi, v2si),
18628   VAR2 (SCALARMAC, vqdmlal_n, v4hi, v2si),
18629   VAR6 (SCALARMAC, vmls_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18630   VAR2 (SCALARMAC, vmlsl_n, v4hi, v2si),
18631   VAR2 (SCALARMAC, vqdmlsl_n, v4hi, v2si),
18632   VAR10 (BINOP, vext,
18633          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18634   VAR8 (UNOP, vrev64, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
18635   VAR4 (UNOP, vrev32, v8qi, v4hi, v16qi, v8hi),
18636   VAR2 (UNOP, vrev16, v8qi, v16qi),
18637   VAR4 (CONVERT, vcvt, v2si, v2sf, v4si, v4sf),
18638   VAR4 (FIXCONV, vcvt_n, v2si, v2sf, v4si, v4sf),
18639   VAR10 (SELECT, vbsl,
18640          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18641   VAR1 (VTBL, vtbl1, v8qi),
18642   VAR1 (VTBL, vtbl2, v8qi),
18643   VAR1 (VTBL, vtbl3, v8qi),
18644   VAR1 (VTBL, vtbl4, v8qi),
18645   VAR1 (VTBX, vtbx1, v8qi),
18646   VAR1 (VTBX, vtbx2, v8qi),
18647   VAR1 (VTBX, vtbx3, v8qi),
18648   VAR1 (VTBX, vtbx4, v8qi),
18649   VAR8 (RESULTPAIR, vtrn, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
18650   VAR8 (RESULTPAIR, vzip, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
18651   VAR8 (RESULTPAIR, vuzp, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
18652   VAR5 (REINTERP, vreinterpretv8qi, v8qi, v4hi, v2si, v2sf, di),
18653   VAR5 (REINTERP, vreinterpretv4hi, v8qi, v4hi, v2si, v2sf, di),
18654   VAR5 (REINTERP, vreinterpretv2si, v8qi, v4hi, v2si, v2sf, di),
18655   VAR5 (REINTERP, vreinterpretv2sf, v8qi, v4hi, v2si, v2sf, di),
18656   VAR5 (REINTERP, vreinterpretdi, v8qi, v4hi, v2si, v2sf, di),
18657   VAR5 (REINTERP, vreinterpretv16qi, v16qi, v8hi, v4si, v4sf, v2di),
18658   VAR5 (REINTERP, vreinterpretv8hi, v16qi, v8hi, v4si, v4sf, v2di),
18659   VAR5 (REINTERP, vreinterpretv4si, v16qi, v8hi, v4si, v4sf, v2di),
18660   VAR5 (REINTERP, vreinterpretv4sf, v16qi, v8hi, v4si, v4sf, v2di),
18661   VAR5 (REINTERP, vreinterpretv2di, v16qi, v8hi, v4si, v4sf, v2di),
18662   VAR10 (LOAD1, vld1,
18663          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18664   VAR10 (LOAD1LANE, vld1_lane,
18665          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18666   VAR10 (LOAD1, vld1_dup,
18667          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18668   VAR10 (STORE1, vst1,
18669          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18670   VAR10 (STORE1LANE, vst1_lane,
18671          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18672   VAR9 (LOADSTRUCT,
18673         vld2, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf),
18674   VAR7 (LOADSTRUCTLANE, vld2_lane,
18675         v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18676   VAR5 (LOADSTRUCT, vld2_dup, v8qi, v4hi, v2si, v2sf, di),
18677   VAR9 (STORESTRUCT, vst2,
18678         v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf),
18679   VAR7 (STORESTRUCTLANE, vst2_lane,
18680         v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18681   VAR9 (LOADSTRUCT,
18682         vld3, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf),
18683   VAR7 (LOADSTRUCTLANE, vld3_lane,
18684         v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18685   VAR5 (LOADSTRUCT, vld3_dup, v8qi, v4hi, v2si, v2sf, di),
18686   VAR9 (STORESTRUCT, vst3,
18687         v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf),
18688   VAR7 (STORESTRUCTLANE, vst3_lane,
18689         v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18690   VAR9 (LOADSTRUCT, vld4,
18691         v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf),
18692   VAR7 (LOADSTRUCTLANE, vld4_lane,
18693         v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18694   VAR5 (LOADSTRUCT, vld4_dup, v8qi, v4hi, v2si, v2sf, di),
18695   VAR9 (STORESTRUCT, vst4,
18696         v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf),
18697   VAR7 (STORESTRUCTLANE, vst4_lane,
18698         v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
18699   VAR10 (LOGICBINOP, vand,
18700          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18701   VAR10 (LOGICBINOP, vorr,
18702          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18703   VAR10 (BINOP, veor,
18704          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18705   VAR10 (LOGICBINOP, vbic,
18706          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
18707   VAR10 (LOGICBINOP, vorn,
18708          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di)
18709 };
18710
18711 #undef CF
18712 #undef VAR1
18713 #undef VAR2
18714 #undef VAR3
18715 #undef VAR4
18716 #undef VAR5
18717 #undef VAR6
18718 #undef VAR7
18719 #undef VAR8
18720 #undef VAR9
18721 #undef VAR10
18722
18723 /* Neon defines builtins from ARM_BUILTIN_MAX upwards, though they don't have
18724    symbolic names defined here (which would require too much duplication).
18725    FIXME?  */
18726 enum arm_builtins
18727 {
18728   ARM_BUILTIN_GETWCX,
18729   ARM_BUILTIN_SETWCX,
18730
18731   ARM_BUILTIN_WZERO,
18732
18733   ARM_BUILTIN_WAVG2BR,
18734   ARM_BUILTIN_WAVG2HR,
18735   ARM_BUILTIN_WAVG2B,
18736   ARM_BUILTIN_WAVG2H,
18737
18738   ARM_BUILTIN_WACCB,
18739   ARM_BUILTIN_WACCH,
18740   ARM_BUILTIN_WACCW,
18741
18742   ARM_BUILTIN_WMACS,
18743   ARM_BUILTIN_WMACSZ,
18744   ARM_BUILTIN_WMACU,
18745   ARM_BUILTIN_WMACUZ,
18746
18747   ARM_BUILTIN_WSADB,
18748   ARM_BUILTIN_WSADBZ,
18749   ARM_BUILTIN_WSADH,
18750   ARM_BUILTIN_WSADHZ,
18751
18752   ARM_BUILTIN_WALIGN,
18753
18754   ARM_BUILTIN_TMIA,
18755   ARM_BUILTIN_TMIAPH,
18756   ARM_BUILTIN_TMIABB,
18757   ARM_BUILTIN_TMIABT,
18758   ARM_BUILTIN_TMIATB,
18759   ARM_BUILTIN_TMIATT,
18760
18761   ARM_BUILTIN_TMOVMSKB,
18762   ARM_BUILTIN_TMOVMSKH,
18763   ARM_BUILTIN_TMOVMSKW,
18764
18765   ARM_BUILTIN_TBCSTB,
18766   ARM_BUILTIN_TBCSTH,
18767   ARM_BUILTIN_TBCSTW,
18768
18769   ARM_BUILTIN_WMADDS,
18770   ARM_BUILTIN_WMADDU,
18771
18772   ARM_BUILTIN_WPACKHSS,
18773   ARM_BUILTIN_WPACKWSS,
18774   ARM_BUILTIN_WPACKDSS,
18775   ARM_BUILTIN_WPACKHUS,
18776   ARM_BUILTIN_WPACKWUS,
18777   ARM_BUILTIN_WPACKDUS,
18778
18779   ARM_BUILTIN_WADDB,
18780   ARM_BUILTIN_WADDH,
18781   ARM_BUILTIN_WADDW,
18782   ARM_BUILTIN_WADDSSB,
18783   ARM_BUILTIN_WADDSSH,
18784   ARM_BUILTIN_WADDSSW,
18785   ARM_BUILTIN_WADDUSB,
18786   ARM_BUILTIN_WADDUSH,
18787   ARM_BUILTIN_WADDUSW,
18788   ARM_BUILTIN_WSUBB,
18789   ARM_BUILTIN_WSUBH,
18790   ARM_BUILTIN_WSUBW,
18791   ARM_BUILTIN_WSUBSSB,
18792   ARM_BUILTIN_WSUBSSH,
18793   ARM_BUILTIN_WSUBSSW,
18794   ARM_BUILTIN_WSUBUSB,
18795   ARM_BUILTIN_WSUBUSH,
18796   ARM_BUILTIN_WSUBUSW,
18797
18798   ARM_BUILTIN_WAND,
18799   ARM_BUILTIN_WANDN,
18800   ARM_BUILTIN_WOR,
18801   ARM_BUILTIN_WXOR,
18802
18803   ARM_BUILTIN_WCMPEQB,
18804   ARM_BUILTIN_WCMPEQH,
18805   ARM_BUILTIN_WCMPEQW,
18806   ARM_BUILTIN_WCMPGTUB,
18807   ARM_BUILTIN_WCMPGTUH,
18808   ARM_BUILTIN_WCMPGTUW,
18809   ARM_BUILTIN_WCMPGTSB,
18810   ARM_BUILTIN_WCMPGTSH,
18811   ARM_BUILTIN_WCMPGTSW,
18812
18813   ARM_BUILTIN_TEXTRMSB,
18814   ARM_BUILTIN_TEXTRMSH,
18815   ARM_BUILTIN_TEXTRMSW,
18816   ARM_BUILTIN_TEXTRMUB,
18817   ARM_BUILTIN_TEXTRMUH,
18818   ARM_BUILTIN_TEXTRMUW,
18819   ARM_BUILTIN_TINSRB,
18820   ARM_BUILTIN_TINSRH,
18821   ARM_BUILTIN_TINSRW,
18822
18823   ARM_BUILTIN_WMAXSW,
18824   ARM_BUILTIN_WMAXSH,
18825   ARM_BUILTIN_WMAXSB,
18826   ARM_BUILTIN_WMAXUW,
18827   ARM_BUILTIN_WMAXUH,
18828   ARM_BUILTIN_WMAXUB,
18829   ARM_BUILTIN_WMINSW,
18830   ARM_BUILTIN_WMINSH,
18831   ARM_BUILTIN_WMINSB,
18832   ARM_BUILTIN_WMINUW,
18833   ARM_BUILTIN_WMINUH,
18834   ARM_BUILTIN_WMINUB,
18835
18836   ARM_BUILTIN_WMULUM,
18837   ARM_BUILTIN_WMULSM,
18838   ARM_BUILTIN_WMULUL,
18839
18840   ARM_BUILTIN_PSADBH,
18841   ARM_BUILTIN_WSHUFH,
18842
18843   ARM_BUILTIN_WSLLH,
18844   ARM_BUILTIN_WSLLW,
18845   ARM_BUILTIN_WSLLD,
18846   ARM_BUILTIN_WSRAH,
18847   ARM_BUILTIN_WSRAW,
18848   ARM_BUILTIN_WSRAD,
18849   ARM_BUILTIN_WSRLH,
18850   ARM_BUILTIN_WSRLW,
18851   ARM_BUILTIN_WSRLD,
18852   ARM_BUILTIN_WRORH,
18853   ARM_BUILTIN_WRORW,
18854   ARM_BUILTIN_WRORD,
18855   ARM_BUILTIN_WSLLHI,
18856   ARM_BUILTIN_WSLLWI,
18857   ARM_BUILTIN_WSLLDI,
18858   ARM_BUILTIN_WSRAHI,
18859   ARM_BUILTIN_WSRAWI,
18860   ARM_BUILTIN_WSRADI,
18861   ARM_BUILTIN_WSRLHI,
18862   ARM_BUILTIN_WSRLWI,
18863   ARM_BUILTIN_WSRLDI,
18864   ARM_BUILTIN_WRORHI,
18865   ARM_BUILTIN_WRORWI,
18866   ARM_BUILTIN_WRORDI,
18867
18868   ARM_BUILTIN_WUNPCKIHB,
18869   ARM_BUILTIN_WUNPCKIHH,
18870   ARM_BUILTIN_WUNPCKIHW,
18871   ARM_BUILTIN_WUNPCKILB,
18872   ARM_BUILTIN_WUNPCKILH,
18873   ARM_BUILTIN_WUNPCKILW,
18874
18875   ARM_BUILTIN_WUNPCKEHSB,
18876   ARM_BUILTIN_WUNPCKEHSH,
18877   ARM_BUILTIN_WUNPCKEHSW,
18878   ARM_BUILTIN_WUNPCKEHUB,
18879   ARM_BUILTIN_WUNPCKEHUH,
18880   ARM_BUILTIN_WUNPCKEHUW,
18881   ARM_BUILTIN_WUNPCKELSB,
18882   ARM_BUILTIN_WUNPCKELSH,
18883   ARM_BUILTIN_WUNPCKELSW,
18884   ARM_BUILTIN_WUNPCKELUB,
18885   ARM_BUILTIN_WUNPCKELUH,
18886   ARM_BUILTIN_WUNPCKELUW,
18887
18888   ARM_BUILTIN_THREAD_POINTER,
18889
18890   ARM_BUILTIN_NEON_BASE,
18891
18892   ARM_BUILTIN_MAX = ARM_BUILTIN_NEON_BASE + ARRAY_SIZE (neon_builtin_data)
18893 };
18894
18895 static GTY(()) tree arm_builtin_decls[ARM_BUILTIN_MAX];
18896
18897 static void
18898 arm_init_neon_builtins (void)
18899 {
18900   unsigned int i, fcode;
18901   tree decl;
18902
18903   tree neon_intQI_type_node;
18904   tree neon_intHI_type_node;
18905   tree neon_polyQI_type_node;
18906   tree neon_polyHI_type_node;
18907   tree neon_intSI_type_node;
18908   tree neon_intDI_type_node;
18909   tree neon_float_type_node;
18910
18911   tree intQI_pointer_node;
18912   tree intHI_pointer_node;
18913   tree intSI_pointer_node;
18914   tree intDI_pointer_node;
18915   tree float_pointer_node;
18916
18917   tree const_intQI_node;
18918   tree const_intHI_node;
18919   tree const_intSI_node;
18920   tree const_intDI_node;
18921   tree const_float_node;
18922
18923   tree const_intQI_pointer_node;
18924   tree const_intHI_pointer_node;
18925   tree const_intSI_pointer_node;
18926   tree const_intDI_pointer_node;
18927   tree const_float_pointer_node;
18928
18929   tree V8QI_type_node;
18930   tree V4HI_type_node;
18931   tree V2SI_type_node;
18932   tree V2SF_type_node;
18933   tree V16QI_type_node;
18934   tree V8HI_type_node;
18935   tree V4SI_type_node;
18936   tree V4SF_type_node;
18937   tree V2DI_type_node;
18938
18939   tree intUQI_type_node;
18940   tree intUHI_type_node;
18941   tree intUSI_type_node;
18942   tree intUDI_type_node;
18943
18944   tree intEI_type_node;
18945   tree intOI_type_node;
18946   tree intCI_type_node;
18947   tree intXI_type_node;
18948
18949   tree V8QI_pointer_node;
18950   tree V4HI_pointer_node;
18951   tree V2SI_pointer_node;
18952   tree V2SF_pointer_node;
18953   tree V16QI_pointer_node;
18954   tree V8HI_pointer_node;
18955   tree V4SI_pointer_node;
18956   tree V4SF_pointer_node;
18957   tree V2DI_pointer_node;
18958
18959   tree void_ftype_pv8qi_v8qi_v8qi;
18960   tree void_ftype_pv4hi_v4hi_v4hi;
18961   tree void_ftype_pv2si_v2si_v2si;
18962   tree void_ftype_pv2sf_v2sf_v2sf;
18963   tree void_ftype_pdi_di_di;
18964   tree void_ftype_pv16qi_v16qi_v16qi;
18965   tree void_ftype_pv8hi_v8hi_v8hi;
18966   tree void_ftype_pv4si_v4si_v4si;
18967   tree void_ftype_pv4sf_v4sf_v4sf;
18968   tree void_ftype_pv2di_v2di_v2di;
18969
18970   tree reinterp_ftype_dreg[5][5];
18971   tree reinterp_ftype_qreg[5][5];
18972   tree dreg_types[5], qreg_types[5];
18973
18974   /* Create distinguished type nodes for NEON vector element types,
18975      and pointers to values of such types, so we can detect them later.  */
18976   neon_intQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
18977   neon_intHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
18978   neon_polyQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
18979   neon_polyHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
18980   neon_intSI_type_node = make_signed_type (GET_MODE_PRECISION (SImode));
18981   neon_intDI_type_node = make_signed_type (GET_MODE_PRECISION (DImode));
18982   neon_float_type_node = make_node (REAL_TYPE);
18983   TYPE_PRECISION (neon_float_type_node) = FLOAT_TYPE_SIZE;
18984   layout_type (neon_float_type_node);
18985
18986   /* Define typedefs which exactly correspond to the modes we are basing vector
18987      types on.  If you change these names you'll need to change
18988      the table used by arm_mangle_type too.  */
18989   (*lang_hooks.types.register_builtin_type) (neon_intQI_type_node,
18990                                              "__builtin_neon_qi");
18991   (*lang_hooks.types.register_builtin_type) (neon_intHI_type_node,
18992                                              "__builtin_neon_hi");
18993   (*lang_hooks.types.register_builtin_type) (neon_intSI_type_node,
18994                                              "__builtin_neon_si");
18995   (*lang_hooks.types.register_builtin_type) (neon_float_type_node,
18996                                              "__builtin_neon_sf");
18997   (*lang_hooks.types.register_builtin_type) (neon_intDI_type_node,
18998                                              "__builtin_neon_di");
18999   (*lang_hooks.types.register_builtin_type) (neon_polyQI_type_node,
19000                                              "__builtin_neon_poly8");
19001   (*lang_hooks.types.register_builtin_type) (neon_polyHI_type_node,
19002                                              "__builtin_neon_poly16");
19003
19004   intQI_pointer_node = build_pointer_type (neon_intQI_type_node);
19005   intHI_pointer_node = build_pointer_type (neon_intHI_type_node);
19006   intSI_pointer_node = build_pointer_type (neon_intSI_type_node);
19007   intDI_pointer_node = build_pointer_type (neon_intDI_type_node);
19008   float_pointer_node = build_pointer_type (neon_float_type_node);
19009
19010   /* Next create constant-qualified versions of the above types.  */
19011   const_intQI_node = build_qualified_type (neon_intQI_type_node,
19012                                            TYPE_QUAL_CONST);
19013   const_intHI_node = build_qualified_type (neon_intHI_type_node,
19014                                            TYPE_QUAL_CONST);
19015   const_intSI_node = build_qualified_type (neon_intSI_type_node,
19016                                            TYPE_QUAL_CONST);
19017   const_intDI_node = build_qualified_type (neon_intDI_type_node,
19018                                            TYPE_QUAL_CONST);
19019   const_float_node = build_qualified_type (neon_float_type_node,
19020                                            TYPE_QUAL_CONST);
19021
19022   const_intQI_pointer_node = build_pointer_type (const_intQI_node);
19023   const_intHI_pointer_node = build_pointer_type (const_intHI_node);
19024   const_intSI_pointer_node = build_pointer_type (const_intSI_node);
19025   const_intDI_pointer_node = build_pointer_type (const_intDI_node);
19026   const_float_pointer_node = build_pointer_type (const_float_node);
19027
19028   /* Now create vector types based on our NEON element types.  */
19029   /* 64-bit vectors.  */
19030   V8QI_type_node =
19031     build_vector_type_for_mode (neon_intQI_type_node, V8QImode);
19032   V4HI_type_node =
19033     build_vector_type_for_mode (neon_intHI_type_node, V4HImode);
19034   V2SI_type_node =
19035     build_vector_type_for_mode (neon_intSI_type_node, V2SImode);
19036   V2SF_type_node =
19037     build_vector_type_for_mode (neon_float_type_node, V2SFmode);
19038   /* 128-bit vectors.  */
19039   V16QI_type_node =
19040     build_vector_type_for_mode (neon_intQI_type_node, V16QImode);
19041   V8HI_type_node =
19042     build_vector_type_for_mode (neon_intHI_type_node, V8HImode);
19043   V4SI_type_node =
19044     build_vector_type_for_mode (neon_intSI_type_node, V4SImode);
19045   V4SF_type_node =
19046     build_vector_type_for_mode (neon_float_type_node, V4SFmode);
19047   V2DI_type_node =
19048     build_vector_type_for_mode (neon_intDI_type_node, V2DImode);
19049
19050   /* Unsigned integer types for various mode sizes.  */
19051   intUQI_type_node = make_unsigned_type (GET_MODE_PRECISION (QImode));
19052   intUHI_type_node = make_unsigned_type (GET_MODE_PRECISION (HImode));
19053   intUSI_type_node = make_unsigned_type (GET_MODE_PRECISION (SImode));
19054   intUDI_type_node = make_unsigned_type (GET_MODE_PRECISION (DImode));
19055
19056   (*lang_hooks.types.register_builtin_type) (intUQI_type_node,
19057                                              "__builtin_neon_uqi");
19058   (*lang_hooks.types.register_builtin_type) (intUHI_type_node,
19059                                              "__builtin_neon_uhi");
19060   (*lang_hooks.types.register_builtin_type) (intUSI_type_node,
19061                                              "__builtin_neon_usi");
19062   (*lang_hooks.types.register_builtin_type) (intUDI_type_node,
19063                                              "__builtin_neon_udi");
19064
19065   /* Opaque integer types for structures of vectors.  */
19066   intEI_type_node = make_signed_type (GET_MODE_PRECISION (EImode));
19067   intOI_type_node = make_signed_type (GET_MODE_PRECISION (OImode));
19068   intCI_type_node = make_signed_type (GET_MODE_PRECISION (CImode));
19069   intXI_type_node = make_signed_type (GET_MODE_PRECISION (XImode));
19070
19071   (*lang_hooks.types.register_builtin_type) (intTI_type_node,
19072                                              "__builtin_neon_ti");
19073   (*lang_hooks.types.register_builtin_type) (intEI_type_node,
19074                                              "__builtin_neon_ei");
19075   (*lang_hooks.types.register_builtin_type) (intOI_type_node,
19076                                              "__builtin_neon_oi");
19077   (*lang_hooks.types.register_builtin_type) (intCI_type_node,
19078                                              "__builtin_neon_ci");
19079   (*lang_hooks.types.register_builtin_type) (intXI_type_node,
19080                                              "__builtin_neon_xi");
19081
19082   /* Pointers to vector types.  */
19083   V8QI_pointer_node = build_pointer_type (V8QI_type_node);
19084   V4HI_pointer_node = build_pointer_type (V4HI_type_node);
19085   V2SI_pointer_node = build_pointer_type (V2SI_type_node);
19086   V2SF_pointer_node = build_pointer_type (V2SF_type_node);
19087   V16QI_pointer_node = build_pointer_type (V16QI_type_node);
19088   V8HI_pointer_node = build_pointer_type (V8HI_type_node);
19089   V4SI_pointer_node = build_pointer_type (V4SI_type_node);
19090   V4SF_pointer_node = build_pointer_type (V4SF_type_node);
19091   V2DI_pointer_node = build_pointer_type (V2DI_type_node);
19092
19093   /* Operations which return results as pairs.  */
19094   void_ftype_pv8qi_v8qi_v8qi =
19095     build_function_type_list (void_type_node, V8QI_pointer_node, V8QI_type_node,
19096                               V8QI_type_node, NULL);
19097   void_ftype_pv4hi_v4hi_v4hi =
19098     build_function_type_list (void_type_node, V4HI_pointer_node, V4HI_type_node,
19099                               V4HI_type_node, NULL);
19100   void_ftype_pv2si_v2si_v2si =
19101     build_function_type_list (void_type_node, V2SI_pointer_node, V2SI_type_node,
19102                               V2SI_type_node, NULL);
19103   void_ftype_pv2sf_v2sf_v2sf =
19104     build_function_type_list (void_type_node, V2SF_pointer_node, V2SF_type_node,
19105                               V2SF_type_node, NULL);
19106   void_ftype_pdi_di_di =
19107     build_function_type_list (void_type_node, intDI_pointer_node,
19108                               neon_intDI_type_node, neon_intDI_type_node, NULL);
19109   void_ftype_pv16qi_v16qi_v16qi =
19110     build_function_type_list (void_type_node, V16QI_pointer_node,
19111                               V16QI_type_node, V16QI_type_node, NULL);
19112   void_ftype_pv8hi_v8hi_v8hi =
19113     build_function_type_list (void_type_node, V8HI_pointer_node, V8HI_type_node,
19114                               V8HI_type_node, NULL);
19115   void_ftype_pv4si_v4si_v4si =
19116     build_function_type_list (void_type_node, V4SI_pointer_node, V4SI_type_node,
19117                               V4SI_type_node, NULL);
19118   void_ftype_pv4sf_v4sf_v4sf =
19119     build_function_type_list (void_type_node, V4SF_pointer_node, V4SF_type_node,
19120                               V4SF_type_node, NULL);
19121   void_ftype_pv2di_v2di_v2di =
19122     build_function_type_list (void_type_node, V2DI_pointer_node, V2DI_type_node,
19123                               V2DI_type_node, NULL);
19124
19125   dreg_types[0] = V8QI_type_node;
19126   dreg_types[1] = V4HI_type_node;
19127   dreg_types[2] = V2SI_type_node;
19128   dreg_types[3] = V2SF_type_node;
19129   dreg_types[4] = neon_intDI_type_node;
19130
19131   qreg_types[0] = V16QI_type_node;
19132   qreg_types[1] = V8HI_type_node;
19133   qreg_types[2] = V4SI_type_node;
19134   qreg_types[3] = V4SF_type_node;
19135   qreg_types[4] = V2DI_type_node;
19136
19137   for (i = 0; i < 5; i++)
19138     {
19139       int j;
19140       for (j = 0; j < 5; j++)
19141         {
19142           reinterp_ftype_dreg[i][j]
19143             = build_function_type_list (dreg_types[i], dreg_types[j], NULL);
19144           reinterp_ftype_qreg[i][j]
19145             = build_function_type_list (qreg_types[i], qreg_types[j], NULL);
19146         }
19147     }
19148
19149   for (i = 0, fcode = ARM_BUILTIN_NEON_BASE;
19150        i < ARRAY_SIZE (neon_builtin_data);
19151        i++, fcode++)
19152     {
19153       neon_builtin_datum *d = &neon_builtin_data[i];
19154
19155       const char* const modenames[] = {
19156         "v8qi", "v4hi", "v2si", "v2sf", "di",
19157         "v16qi", "v8hi", "v4si", "v4sf", "v2di",
19158         "ti", "ei", "oi"
19159       };
19160       char namebuf[60];
19161       tree ftype = NULL;
19162       int is_load = 0, is_store = 0;
19163
19164       gcc_assert (ARRAY_SIZE (modenames) == T_MAX);
19165
19166       d->fcode = fcode;
19167
19168       switch (d->itype)
19169         {
19170         case NEON_LOAD1:
19171         case NEON_LOAD1LANE:
19172         case NEON_LOADSTRUCT:
19173         case NEON_LOADSTRUCTLANE:
19174           is_load = 1;
19175           /* Fall through.  */
19176         case NEON_STORE1:
19177         case NEON_STORE1LANE:
19178         case NEON_STORESTRUCT:
19179         case NEON_STORESTRUCTLANE:
19180           if (!is_load)
19181             is_store = 1;
19182           /* Fall through.  */
19183         case NEON_UNOP:
19184         case NEON_BINOP:
19185         case NEON_LOGICBINOP:
19186         case NEON_SHIFTINSERT:
19187         case NEON_TERNOP:
19188         case NEON_GETLANE:
19189         case NEON_SETLANE:
19190         case NEON_CREATE:
19191         case NEON_DUP:
19192         case NEON_DUPLANE:
19193         case NEON_SHIFTIMM:
19194         case NEON_SHIFTACC:
19195         case NEON_COMBINE:
19196         case NEON_SPLIT:
19197         case NEON_CONVERT:
19198         case NEON_FIXCONV:
19199         case NEON_LANEMUL:
19200         case NEON_LANEMULL:
19201         case NEON_LANEMULH:
19202         case NEON_LANEMAC:
19203         case NEON_SCALARMUL:
19204         case NEON_SCALARMULL:
19205         case NEON_SCALARMULH:
19206         case NEON_SCALARMAC:
19207         case NEON_SELECT:
19208         case NEON_VTBL:
19209         case NEON_VTBX:
19210           {
19211             int k;
19212             tree return_type = void_type_node, args = void_list_node;
19213
19214             /* Build a function type directly from the insn_data for
19215                this builtin.  The build_function_type() function takes
19216                care of removing duplicates for us.  */
19217             for (k = insn_data[d->code].n_generator_args - 1; k >= 0; k--)
19218               {
19219                 tree eltype;
19220
19221                 if (is_load && k == 1)
19222                   {
19223                     /* Neon load patterns always have the memory
19224                        operand in the operand 1 position.  */
19225                     gcc_assert (insn_data[d->code].operand[k].predicate
19226                                 == neon_struct_operand);
19227
19228                     switch (d->mode)
19229                       {
19230                       case T_V8QI:
19231                       case T_V16QI:
19232                         eltype = const_intQI_pointer_node;
19233                         break;
19234
19235                       case T_V4HI:
19236                       case T_V8HI:
19237                         eltype = const_intHI_pointer_node;
19238                         break;
19239
19240                       case T_V2SI:
19241                       case T_V4SI:
19242                         eltype = const_intSI_pointer_node;
19243                         break;
19244
19245                       case T_V2SF:
19246                       case T_V4SF:
19247                         eltype = const_float_pointer_node;
19248                         break;
19249
19250                       case T_DI:
19251                       case T_V2DI:
19252                         eltype = const_intDI_pointer_node;
19253                         break;
19254
19255                       default: gcc_unreachable ();
19256                       }
19257                   }
19258                 else if (is_store && k == 0)
19259                   {
19260                     /* Similarly, Neon store patterns use operand 0 as
19261                        the memory location to store to.  */
19262                     gcc_assert (insn_data[d->code].operand[k].predicate
19263                                 == neon_struct_operand);
19264
19265                     switch (d->mode)
19266                       {
19267                       case T_V8QI:
19268                       case T_V16QI:
19269                         eltype = intQI_pointer_node;
19270                         break;
19271
19272                       case T_V4HI:
19273                       case T_V8HI:
19274                         eltype = intHI_pointer_node;
19275                         break;
19276
19277                       case T_V2SI:
19278                       case T_V4SI:
19279                         eltype = intSI_pointer_node;
19280                         break;
19281
19282                       case T_V2SF:
19283                       case T_V4SF:
19284                         eltype = float_pointer_node;
19285                         break;
19286
19287                       case T_DI:
19288                       case T_V2DI:
19289                         eltype = intDI_pointer_node;
19290                         break;
19291
19292                       default: gcc_unreachable ();
19293                       }
19294                   }
19295                 else
19296                   {
19297                     switch (insn_data[d->code].operand[k].mode)
19298                       {
19299                       case VOIDmode: eltype = void_type_node; break;
19300                         /* Scalars.  */
19301                       case QImode: eltype = neon_intQI_type_node; break;
19302                       case HImode: eltype = neon_intHI_type_node; break;
19303                       case SImode: eltype = neon_intSI_type_node; break;
19304                       case SFmode: eltype = neon_float_type_node; break;
19305                       case DImode: eltype = neon_intDI_type_node; break;
19306                       case TImode: eltype = intTI_type_node; break;
19307                       case EImode: eltype = intEI_type_node; break;
19308                       case OImode: eltype = intOI_type_node; break;
19309                       case CImode: eltype = intCI_type_node; break;
19310                       case XImode: eltype = intXI_type_node; break;
19311                         /* 64-bit vectors.  */
19312                       case V8QImode: eltype = V8QI_type_node; break;
19313                       case V4HImode: eltype = V4HI_type_node; break;
19314                       case V2SImode: eltype = V2SI_type_node; break;
19315                       case V2SFmode: eltype = V2SF_type_node; break;
19316                         /* 128-bit vectors.  */
19317                       case V16QImode: eltype = V16QI_type_node; break;
19318                       case V8HImode: eltype = V8HI_type_node; break;
19319                       case V4SImode: eltype = V4SI_type_node; break;
19320                       case V4SFmode: eltype = V4SF_type_node; break;
19321                       case V2DImode: eltype = V2DI_type_node; break;
19322                       default: gcc_unreachable ();
19323                       }
19324                   }
19325
19326                 if (k == 0 && !is_store)
19327                   return_type = eltype;
19328                 else
19329                   args = tree_cons (NULL_TREE, eltype, args);
19330               }
19331
19332             ftype = build_function_type (return_type, args);
19333           }
19334           break;
19335
19336         case NEON_RESULTPAIR:
19337           {
19338             switch (insn_data[d->code].operand[1].mode)
19339               {
19340               case V8QImode: ftype = void_ftype_pv8qi_v8qi_v8qi; break;
19341               case V4HImode: ftype = void_ftype_pv4hi_v4hi_v4hi; break;
19342               case V2SImode: ftype = void_ftype_pv2si_v2si_v2si; break;
19343               case V2SFmode: ftype = void_ftype_pv2sf_v2sf_v2sf; break;
19344               case DImode: ftype = void_ftype_pdi_di_di; break;
19345               case V16QImode: ftype = void_ftype_pv16qi_v16qi_v16qi; break;
19346               case V8HImode: ftype = void_ftype_pv8hi_v8hi_v8hi; break;
19347               case V4SImode: ftype = void_ftype_pv4si_v4si_v4si; break;
19348               case V4SFmode: ftype = void_ftype_pv4sf_v4sf_v4sf; break;
19349               case V2DImode: ftype = void_ftype_pv2di_v2di_v2di; break;
19350               default: gcc_unreachable ();
19351               }
19352           }
19353           break;
19354
19355         case NEON_REINTERP:
19356           {
19357             /* We iterate over 5 doubleword types, then 5 quadword
19358                types.  */
19359             int rhs = d->mode % 5;
19360             switch (insn_data[d->code].operand[0].mode)
19361               {
19362               case V8QImode: ftype = reinterp_ftype_dreg[0][rhs]; break;
19363               case V4HImode: ftype = reinterp_ftype_dreg[1][rhs]; break;
19364               case V2SImode: ftype = reinterp_ftype_dreg[2][rhs]; break;
19365               case V2SFmode: ftype = reinterp_ftype_dreg[3][rhs]; break;
19366               case DImode: ftype = reinterp_ftype_dreg[4][rhs]; break;
19367               case V16QImode: ftype = reinterp_ftype_qreg[0][rhs]; break;
19368               case V8HImode: ftype = reinterp_ftype_qreg[1][rhs]; break;
19369               case V4SImode: ftype = reinterp_ftype_qreg[2][rhs]; break;
19370               case V4SFmode: ftype = reinterp_ftype_qreg[3][rhs]; break;
19371               case V2DImode: ftype = reinterp_ftype_qreg[4][rhs]; break;
19372               default: gcc_unreachable ();
19373               }
19374           }
19375           break;
19376
19377         default:
19378           gcc_unreachable ();
19379         }
19380
19381       gcc_assert (ftype != NULL);
19382
19383       sprintf (namebuf, "__builtin_neon_%s%s", d->name, modenames[d->mode]);
19384
19385       decl = add_builtin_function (namebuf, ftype, fcode, BUILT_IN_MD, NULL,
19386                                    NULL_TREE);
19387       arm_builtin_decls[fcode] = decl;
19388     }
19389 }
19390
19391 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
19392   do                                                                    \
19393     {                                                                   \
19394       if ((MASK) & insn_flags)                                          \
19395         {                                                               \
19396           tree bdecl;                                                   \
19397           bdecl = add_builtin_function ((NAME), (TYPE), (CODE),         \
19398                                         BUILT_IN_MD, NULL, NULL_TREE);  \
19399           arm_builtin_decls[CODE] = bdecl;                              \
19400         }                                                               \
19401     }                                                                   \
19402   while (0)
19403   
19404 struct builtin_description
19405 {
19406   const unsigned int       mask;
19407   const enum insn_code     icode;
19408   const char * const       name;
19409   const enum arm_builtins  code;
19410   const enum rtx_code      comparison;
19411   const unsigned int       flag;
19412 };
19413   
19414 static const struct builtin_description bdesc_2arg[] =
19415 {
19416 #define IWMMXT_BUILTIN(code, string, builtin) \
19417   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
19418     ARM_BUILTIN_##builtin, UNKNOWN, 0 },
19419   
19420   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
19421   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
19422   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
19423   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
19424   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
19425   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
19426   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
19427   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
19428   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
19429   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
19430   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
19431   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
19432   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
19433   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
19434   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
19435   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
19436   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
19437   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
19438   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
19439   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
19440   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
19441   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
19442   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
19443   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
19444   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
19445   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
19446   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
19447   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
19448   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
19449   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
19450   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
19451   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
19452   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
19453   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
19454   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
19455   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
19456   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
19457   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
19458   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
19459   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
19460   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
19461   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
19462   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
19463   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
19464   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
19465   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
19466   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
19467   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
19468   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
19469   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
19470   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
19471   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
19472   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
19473   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
19474   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
19475   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
19476   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
19477   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
19478   
19479 #define IWMMXT_BUILTIN2(code, builtin) \
19480   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, UNKNOWN, 0 },
19481   
19482   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
19483   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
19484   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
19485   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
19486   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
19487   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
19488   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
19489   IWMMXT_BUILTIN2 (ashlv4hi3_iwmmxt, WSLLHI)
19490   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
19491   IWMMXT_BUILTIN2 (ashlv2si3_iwmmxt, WSLLWI)
19492   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
19493   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
19494   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
19495   IWMMXT_BUILTIN2 (lshrv4hi3_iwmmxt, WSRLHI)
19496   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
19497   IWMMXT_BUILTIN2 (lshrv2si3_iwmmxt, WSRLWI)
19498   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
19499   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
19500   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
19501   IWMMXT_BUILTIN2 (ashrv4hi3_iwmmxt, WSRAHI)
19502   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
19503   IWMMXT_BUILTIN2 (ashrv2si3_iwmmxt, WSRAWI)
19504   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
19505   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
19506   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
19507   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
19508   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
19509   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
19510   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
19511   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
19512   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
19513   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
19514 };
19515   
19516 static const struct builtin_description bdesc_1arg[] =
19517 {
19518   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
19519   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
19520   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
19521   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
19522   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
19523   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
19524   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
19525   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
19526   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
19527   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
19528   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
19529   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
19530   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
19531   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
19532   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
19533   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
19534   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
19535   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
19536 };
19537   
19538 /* Set up all the iWMMXt builtins.  This is not called if
19539    TARGET_IWMMXT is zero.  */
19540
19541 static void
19542 arm_init_iwmmxt_builtins (void)
19543 {
19544   const struct builtin_description * d;
19545   size_t i;
19546
19547   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
19548   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
19549   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
19550
19551   tree int_ftype_int
19552     = build_function_type_list (integer_type_node,
19553                                 integer_type_node, NULL_TREE);
19554   tree v8qi_ftype_v8qi_v8qi_int
19555     = build_function_type_list (V8QI_type_node,
19556                                 V8QI_type_node, V8QI_type_node,
19557                                 integer_type_node, NULL_TREE);
19558   tree v4hi_ftype_v4hi_int
19559     = build_function_type_list (V4HI_type_node,
19560                                 V4HI_type_node, integer_type_node, NULL_TREE);
19561   tree v2si_ftype_v2si_int
19562     = build_function_type_list (V2SI_type_node,
19563                                 V2SI_type_node, integer_type_node, NULL_TREE);
19564   tree v2si_ftype_di_di
19565     = build_function_type_list (V2SI_type_node,
19566                                 long_long_integer_type_node,
19567                                 long_long_integer_type_node,
19568                                 NULL_TREE);
19569   tree di_ftype_di_int
19570     = build_function_type_list (long_long_integer_type_node,
19571                                 long_long_integer_type_node,
19572                                 integer_type_node, NULL_TREE);
19573   tree di_ftype_di_int_int
19574     = build_function_type_list (long_long_integer_type_node,
19575                                 long_long_integer_type_node,
19576                                 integer_type_node,
19577                                 integer_type_node, NULL_TREE);
19578   tree int_ftype_v8qi
19579     = build_function_type_list (integer_type_node,
19580                                 V8QI_type_node, NULL_TREE);
19581   tree int_ftype_v4hi
19582     = build_function_type_list (integer_type_node,
19583                                 V4HI_type_node, NULL_TREE);
19584   tree int_ftype_v2si
19585     = build_function_type_list (integer_type_node,
19586                                 V2SI_type_node, NULL_TREE);
19587   tree int_ftype_v8qi_int
19588     = build_function_type_list (integer_type_node,
19589                                 V8QI_type_node, integer_type_node, NULL_TREE);
19590   tree int_ftype_v4hi_int
19591     = build_function_type_list (integer_type_node,
19592                                 V4HI_type_node, integer_type_node, NULL_TREE);
19593   tree int_ftype_v2si_int
19594     = build_function_type_list (integer_type_node,
19595                                 V2SI_type_node, integer_type_node, NULL_TREE);
19596   tree v8qi_ftype_v8qi_int_int
19597     = build_function_type_list (V8QI_type_node,
19598                                 V8QI_type_node, integer_type_node,
19599                                 integer_type_node, NULL_TREE);
19600   tree v4hi_ftype_v4hi_int_int
19601     = build_function_type_list (V4HI_type_node,
19602                                 V4HI_type_node, integer_type_node,
19603                                 integer_type_node, NULL_TREE);
19604   tree v2si_ftype_v2si_int_int
19605     = build_function_type_list (V2SI_type_node,
19606                                 V2SI_type_node, integer_type_node,
19607                                 integer_type_node, NULL_TREE);
19608   /* Miscellaneous.  */
19609   tree v8qi_ftype_v4hi_v4hi
19610     = build_function_type_list (V8QI_type_node,
19611                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
19612   tree v4hi_ftype_v2si_v2si
19613     = build_function_type_list (V4HI_type_node,
19614                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
19615   tree v2si_ftype_v4hi_v4hi
19616     = build_function_type_list (V2SI_type_node,
19617                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
19618   tree v2si_ftype_v8qi_v8qi
19619     = build_function_type_list (V2SI_type_node,
19620                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
19621   tree v4hi_ftype_v4hi_di
19622     = build_function_type_list (V4HI_type_node,
19623                                 V4HI_type_node, long_long_integer_type_node,
19624                                 NULL_TREE);
19625   tree v2si_ftype_v2si_di
19626     = build_function_type_list (V2SI_type_node,
19627                                 V2SI_type_node, long_long_integer_type_node,
19628                                 NULL_TREE);
19629   tree void_ftype_int_int
19630     = build_function_type_list (void_type_node,
19631                                 integer_type_node, integer_type_node,
19632                                 NULL_TREE);
19633   tree di_ftype_void
19634     = build_function_type_list (long_long_unsigned_type_node, NULL_TREE);
19635   tree di_ftype_v8qi
19636     = build_function_type_list (long_long_integer_type_node,
19637                                 V8QI_type_node, NULL_TREE);
19638   tree di_ftype_v4hi
19639     = build_function_type_list (long_long_integer_type_node,
19640                                 V4HI_type_node, NULL_TREE);
19641   tree di_ftype_v2si
19642     = build_function_type_list (long_long_integer_type_node,
19643                                 V2SI_type_node, NULL_TREE);
19644   tree v2si_ftype_v4hi
19645     = build_function_type_list (V2SI_type_node,
19646                                 V4HI_type_node, NULL_TREE);
19647   tree v4hi_ftype_v8qi
19648     = build_function_type_list (V4HI_type_node,
19649                                 V8QI_type_node, NULL_TREE);
19650
19651   tree di_ftype_di_v4hi_v4hi
19652     = build_function_type_list (long_long_unsigned_type_node,
19653                                 long_long_unsigned_type_node,
19654                                 V4HI_type_node, V4HI_type_node,
19655                                 NULL_TREE);
19656
19657   tree di_ftype_v4hi_v4hi
19658     = build_function_type_list (long_long_unsigned_type_node,
19659                                 V4HI_type_node,V4HI_type_node,
19660                                 NULL_TREE);
19661   
19662   /* Normal vector binops.  */
19663   tree v8qi_ftype_v8qi_v8qi
19664     = build_function_type_list (V8QI_type_node,
19665                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
19666   tree v4hi_ftype_v4hi_v4hi
19667     = build_function_type_list (V4HI_type_node,
19668                                 V4HI_type_node,V4HI_type_node, NULL_TREE);
19669   tree v2si_ftype_v2si_v2si
19670     = build_function_type_list (V2SI_type_node,
19671                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
19672   tree di_ftype_di_di
19673     = build_function_type_list (long_long_unsigned_type_node,
19674                                 long_long_unsigned_type_node,
19675                                 long_long_unsigned_type_node,
19676                                 NULL_TREE);
19677   
19678   /* Add all builtins that are more or less simple operations on two
19679      operands.  */
19680   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
19681     {
19682       /* Use one of the operands; the target can have a different mode for
19683          mask-generating compares.  */
19684       enum machine_mode mode;
19685       tree type;
19686
19687       if (d->name == 0)
19688         continue;
19689
19690       mode = insn_data[d->icode].operand[1].mode;
19691
19692       switch (mode)
19693         {
19694         case V8QImode:
19695           type = v8qi_ftype_v8qi_v8qi;
19696           break;
19697         case V4HImode:
19698           type = v4hi_ftype_v4hi_v4hi;
19699           break;
19700         case V2SImode:
19701           type = v2si_ftype_v2si_v2si;
19702           break;
19703         case DImode:
19704           type = di_ftype_di_di;
19705           break;
19706
19707         default:
19708           gcc_unreachable ();
19709         }
19710
19711       def_mbuiltin (d->mask, d->name, type, d->code);
19712     }
19713
19714   /* Add the remaining MMX insns with somewhat more complicated types.  */
19715 #define iwmmx_mbuiltin(NAME, TYPE, CODE)                        \
19716   def_mbuiltin (FL_IWMMXT, "__builtin_arm_" NAME, (TYPE),       \
19717                 ARM_BUILTIN_ ## CODE)
19718
19719   iwmmx_mbuiltin ("wzero", di_ftype_void, WZERO);
19720   iwmmx_mbuiltin ("setwcx", void_ftype_int_int, SETWCX);
19721   iwmmx_mbuiltin ("getwcx", int_ftype_int, GETWCX);
19722
19723   iwmmx_mbuiltin ("wsllh", v4hi_ftype_v4hi_di, WSLLH);
19724   iwmmx_mbuiltin ("wsllw", v2si_ftype_v2si_di, WSLLW);
19725   iwmmx_mbuiltin ("wslld", di_ftype_di_di, WSLLD);
19726   iwmmx_mbuiltin ("wsllhi", v4hi_ftype_v4hi_int, WSLLHI);
19727   iwmmx_mbuiltin ("wsllwi", v2si_ftype_v2si_int, WSLLWI);
19728   iwmmx_mbuiltin ("wslldi", di_ftype_di_int, WSLLDI);
19729
19730   iwmmx_mbuiltin ("wsrlh", v4hi_ftype_v4hi_di, WSRLH);
19731   iwmmx_mbuiltin ("wsrlw", v2si_ftype_v2si_di, WSRLW);
19732   iwmmx_mbuiltin ("wsrld", di_ftype_di_di, WSRLD);
19733   iwmmx_mbuiltin ("wsrlhi", v4hi_ftype_v4hi_int, WSRLHI);
19734   iwmmx_mbuiltin ("wsrlwi", v2si_ftype_v2si_int, WSRLWI);
19735   iwmmx_mbuiltin ("wsrldi", di_ftype_di_int, WSRLDI);
19736
19737   iwmmx_mbuiltin ("wsrah", v4hi_ftype_v4hi_di, WSRAH);
19738   iwmmx_mbuiltin ("wsraw", v2si_ftype_v2si_di, WSRAW);
19739   iwmmx_mbuiltin ("wsrad", di_ftype_di_di, WSRAD);
19740   iwmmx_mbuiltin ("wsrahi", v4hi_ftype_v4hi_int, WSRAHI);
19741   iwmmx_mbuiltin ("wsrawi", v2si_ftype_v2si_int, WSRAWI);
19742   iwmmx_mbuiltin ("wsradi", di_ftype_di_int, WSRADI);
19743
19744   iwmmx_mbuiltin ("wrorh", v4hi_ftype_v4hi_di, WRORH);
19745   iwmmx_mbuiltin ("wrorw", v2si_ftype_v2si_di, WRORW);
19746   iwmmx_mbuiltin ("wrord", di_ftype_di_di, WRORD);
19747   iwmmx_mbuiltin ("wrorhi", v4hi_ftype_v4hi_int, WRORHI);
19748   iwmmx_mbuiltin ("wrorwi", v2si_ftype_v2si_int, WRORWI);
19749   iwmmx_mbuiltin ("wrordi", di_ftype_di_int, WRORDI);
19750
19751   iwmmx_mbuiltin ("wshufh", v4hi_ftype_v4hi_int, WSHUFH);
19752
19753   iwmmx_mbuiltin ("wsadb", v2si_ftype_v8qi_v8qi, WSADB);
19754   iwmmx_mbuiltin ("wsadh", v2si_ftype_v4hi_v4hi, WSADH);
19755   iwmmx_mbuiltin ("wsadbz", v2si_ftype_v8qi_v8qi, WSADBZ);
19756   iwmmx_mbuiltin ("wsadhz", v2si_ftype_v4hi_v4hi, WSADHZ);
19757
19758   iwmmx_mbuiltin ("textrmsb", int_ftype_v8qi_int, TEXTRMSB);
19759   iwmmx_mbuiltin ("textrmsh", int_ftype_v4hi_int, TEXTRMSH);
19760   iwmmx_mbuiltin ("textrmsw", int_ftype_v2si_int, TEXTRMSW);
19761   iwmmx_mbuiltin ("textrmub", int_ftype_v8qi_int, TEXTRMUB);
19762   iwmmx_mbuiltin ("textrmuh", int_ftype_v4hi_int, TEXTRMUH);
19763   iwmmx_mbuiltin ("textrmuw", int_ftype_v2si_int, TEXTRMUW);
19764   iwmmx_mbuiltin ("tinsrb", v8qi_ftype_v8qi_int_int, TINSRB);
19765   iwmmx_mbuiltin ("tinsrh", v4hi_ftype_v4hi_int_int, TINSRH);
19766   iwmmx_mbuiltin ("tinsrw", v2si_ftype_v2si_int_int, TINSRW);
19767
19768   iwmmx_mbuiltin ("waccb", di_ftype_v8qi, WACCB);
19769   iwmmx_mbuiltin ("wacch", di_ftype_v4hi, WACCH);
19770   iwmmx_mbuiltin ("waccw", di_ftype_v2si, WACCW);
19771
19772   iwmmx_mbuiltin ("tmovmskb", int_ftype_v8qi, TMOVMSKB);
19773   iwmmx_mbuiltin ("tmovmskh", int_ftype_v4hi, TMOVMSKH);
19774   iwmmx_mbuiltin ("tmovmskw", int_ftype_v2si, TMOVMSKW);
19775
19776   iwmmx_mbuiltin ("wpackhss", v8qi_ftype_v4hi_v4hi, WPACKHSS);
19777   iwmmx_mbuiltin ("wpackhus", v8qi_ftype_v4hi_v4hi, WPACKHUS);
19778   iwmmx_mbuiltin ("wpackwus", v4hi_ftype_v2si_v2si, WPACKWUS);
19779   iwmmx_mbuiltin ("wpackwss", v4hi_ftype_v2si_v2si, WPACKWSS);
19780   iwmmx_mbuiltin ("wpackdus", v2si_ftype_di_di, WPACKDUS);
19781   iwmmx_mbuiltin ("wpackdss", v2si_ftype_di_di, WPACKDSS);
19782
19783   iwmmx_mbuiltin ("wunpckehub", v4hi_ftype_v8qi, WUNPCKEHUB);
19784   iwmmx_mbuiltin ("wunpckehuh", v2si_ftype_v4hi, WUNPCKEHUH);
19785   iwmmx_mbuiltin ("wunpckehuw", di_ftype_v2si, WUNPCKEHUW);
19786   iwmmx_mbuiltin ("wunpckehsb", v4hi_ftype_v8qi, WUNPCKEHSB);
19787   iwmmx_mbuiltin ("wunpckehsh", v2si_ftype_v4hi, WUNPCKEHSH);
19788   iwmmx_mbuiltin ("wunpckehsw", di_ftype_v2si, WUNPCKEHSW);
19789   iwmmx_mbuiltin ("wunpckelub", v4hi_ftype_v8qi, WUNPCKELUB);
19790   iwmmx_mbuiltin ("wunpckeluh", v2si_ftype_v4hi, WUNPCKELUH);
19791   iwmmx_mbuiltin ("wunpckeluw", di_ftype_v2si, WUNPCKELUW);
19792   iwmmx_mbuiltin ("wunpckelsb", v4hi_ftype_v8qi, WUNPCKELSB);
19793   iwmmx_mbuiltin ("wunpckelsh", v2si_ftype_v4hi, WUNPCKELSH);
19794   iwmmx_mbuiltin ("wunpckelsw", di_ftype_v2si, WUNPCKELSW);
19795
19796   iwmmx_mbuiltin ("wmacs", di_ftype_di_v4hi_v4hi, WMACS);
19797   iwmmx_mbuiltin ("wmacsz", di_ftype_v4hi_v4hi, WMACSZ);
19798   iwmmx_mbuiltin ("wmacu", di_ftype_di_v4hi_v4hi, WMACU);
19799   iwmmx_mbuiltin ("wmacuz", di_ftype_v4hi_v4hi, WMACUZ);
19800
19801   iwmmx_mbuiltin ("walign", v8qi_ftype_v8qi_v8qi_int, WALIGN);
19802   iwmmx_mbuiltin ("tmia", di_ftype_di_int_int, TMIA);
19803   iwmmx_mbuiltin ("tmiaph", di_ftype_di_int_int, TMIAPH);
19804   iwmmx_mbuiltin ("tmiabb", di_ftype_di_int_int, TMIABB);
19805   iwmmx_mbuiltin ("tmiabt", di_ftype_di_int_int, TMIABT);
19806   iwmmx_mbuiltin ("tmiatb", di_ftype_di_int_int, TMIATB);
19807   iwmmx_mbuiltin ("tmiatt", di_ftype_di_int_int, TMIATT);
19808
19809 #undef iwmmx_mbuiltin
19810 }
19811
19812 static void
19813 arm_init_tls_builtins (void)
19814 {
19815   tree ftype, decl;
19816
19817   ftype = build_function_type (ptr_type_node, void_list_node);
19818   decl = add_builtin_function ("__builtin_thread_pointer", ftype,
19819                                ARM_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
19820                                NULL, NULL_TREE);
19821   TREE_NOTHROW (decl) = 1;
19822   TREE_READONLY (decl) = 1;
19823   arm_builtin_decls[ARM_BUILTIN_THREAD_POINTER] = decl;
19824 }
19825
19826 static void
19827 arm_init_fp16_builtins (void)
19828 {
19829   tree fp16_type = make_node (REAL_TYPE);
19830   TYPE_PRECISION (fp16_type) = 16;
19831   layout_type (fp16_type);
19832   (*lang_hooks.types.register_builtin_type) (fp16_type, "__fp16");
19833 }
19834
19835 static void
19836 arm_init_builtins (void)
19837 {
19838   arm_init_tls_builtins ();
19839
19840   if (TARGET_REALLY_IWMMXT)
19841     arm_init_iwmmxt_builtins ();
19842
19843   if (TARGET_NEON)
19844     arm_init_neon_builtins ();
19845
19846   if (arm_fp16_format)
19847     arm_init_fp16_builtins ();
19848 }
19849
19850 /* Return the ARM builtin for CODE.  */
19851
19852 static tree
19853 arm_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
19854 {
19855   if (code >= ARM_BUILTIN_MAX)
19856     return error_mark_node;
19857
19858   return arm_builtin_decls[code];
19859 }
19860
19861 /* Implement TARGET_INVALID_PARAMETER_TYPE.  */
19862
19863 static const char *
19864 arm_invalid_parameter_type (const_tree t)
19865 {
19866   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
19867     return N_("function parameters cannot have __fp16 type");
19868   return NULL;
19869 }
19870
19871 /* Implement TARGET_INVALID_PARAMETER_TYPE.  */
19872
19873 static const char *
19874 arm_invalid_return_type (const_tree t)
19875 {
19876   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
19877     return N_("functions cannot return __fp16 type");
19878   return NULL;
19879 }
19880
19881 /* Implement TARGET_PROMOTED_TYPE.  */
19882
19883 static tree
19884 arm_promoted_type (const_tree t)
19885 {
19886   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
19887     return float_type_node;
19888   return NULL_TREE;
19889 }
19890
19891 /* Implement TARGET_CONVERT_TO_TYPE.
19892    Specifically, this hook implements the peculiarity of the ARM
19893    half-precision floating-point C semantics that requires conversions between
19894    __fp16 to or from double to do an intermediate conversion to float.  */
19895
19896 static tree
19897 arm_convert_to_type (tree type, tree expr)
19898 {
19899   tree fromtype = TREE_TYPE (expr);
19900   if (!SCALAR_FLOAT_TYPE_P (fromtype) || !SCALAR_FLOAT_TYPE_P (type))
19901     return NULL_TREE;
19902   if ((TYPE_PRECISION (fromtype) == 16 && TYPE_PRECISION (type) > 32)
19903       || (TYPE_PRECISION (type) == 16 && TYPE_PRECISION (fromtype) > 32))
19904     return convert (type, convert (float_type_node, expr));
19905   return NULL_TREE;
19906 }
19907
19908 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P.
19909    This simply adds HFmode as a supported mode; even though we don't
19910    implement arithmetic on this type directly, it's supported by
19911    optabs conversions, much the way the double-word arithmetic is
19912    special-cased in the default hook.  */
19913
19914 static bool
19915 arm_scalar_mode_supported_p (enum machine_mode mode)
19916 {
19917   if (mode == HFmode)
19918     return (arm_fp16_format != ARM_FP16_FORMAT_NONE);
19919   else if (ALL_FIXED_POINT_MODE_P (mode))
19920     return true;
19921   else
19922     return default_scalar_mode_supported_p (mode);
19923 }
19924
19925 /* Errors in the source file can cause expand_expr to return const0_rtx
19926    where we expect a vector.  To avoid crashing, use one of the vector
19927    clear instructions.  */
19928
19929 static rtx
19930 safe_vector_operand (rtx x, enum machine_mode mode)
19931 {
19932   if (x != const0_rtx)
19933     return x;
19934   x = gen_reg_rtx (mode);
19935
19936   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
19937                                : gen_rtx_SUBREG (DImode, x, 0)));
19938   return x;
19939 }
19940
19941 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
19942
19943 static rtx
19944 arm_expand_binop_builtin (enum insn_code icode,
19945                           tree exp, rtx target)
19946 {
19947   rtx pat;
19948   tree arg0 = CALL_EXPR_ARG (exp, 0);
19949   tree arg1 = CALL_EXPR_ARG (exp, 1);
19950   rtx op0 = expand_normal (arg0);
19951   rtx op1 = expand_normal (arg1);
19952   enum machine_mode tmode = insn_data[icode].operand[0].mode;
19953   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
19954   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
19955
19956   if (VECTOR_MODE_P (mode0))
19957     op0 = safe_vector_operand (op0, mode0);
19958   if (VECTOR_MODE_P (mode1))
19959     op1 = safe_vector_operand (op1, mode1);
19960
19961   if (! target
19962       || GET_MODE (target) != tmode
19963       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19964     target = gen_reg_rtx (tmode);
19965
19966   gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
19967
19968   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19969     op0 = copy_to_mode_reg (mode0, op0);
19970   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
19971     op1 = copy_to_mode_reg (mode1, op1);
19972
19973   pat = GEN_FCN (icode) (target, op0, op1);
19974   if (! pat)
19975     return 0;
19976   emit_insn (pat);
19977   return target;
19978 }
19979
19980 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
19981
19982 static rtx
19983 arm_expand_unop_builtin (enum insn_code icode,
19984                          tree exp, rtx target, int do_load)
19985 {
19986   rtx pat;
19987   tree arg0 = CALL_EXPR_ARG (exp, 0);
19988   rtx op0 = expand_normal (arg0);
19989   enum machine_mode tmode = insn_data[icode].operand[0].mode;
19990   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
19991
19992   if (! target
19993       || GET_MODE (target) != tmode
19994       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19995     target = gen_reg_rtx (tmode);
19996   if (do_load)
19997     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
19998   else
19999     {
20000       if (VECTOR_MODE_P (mode0))
20001         op0 = safe_vector_operand (op0, mode0);
20002
20003       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
20004         op0 = copy_to_mode_reg (mode0, op0);
20005     }
20006
20007   pat = GEN_FCN (icode) (target, op0);
20008   if (! pat)
20009     return 0;
20010   emit_insn (pat);
20011   return target;
20012 }
20013
20014 typedef enum {
20015   NEON_ARG_COPY_TO_REG,
20016   NEON_ARG_CONSTANT,
20017   NEON_ARG_MEMORY,
20018   NEON_ARG_STOP
20019 } builtin_arg;
20020
20021 #define NEON_MAX_BUILTIN_ARGS 5
20022
20023 /* EXP is a pointer argument to a Neon load or store intrinsic.  Derive
20024    and return an expression for the accessed memory.
20025
20026    The intrinsic function operates on a block of registers that has
20027    mode REG_MODE.  This block contains vectors of type TYPE_MODE.
20028    The function references the memory at EXP in mode MEM_MODE;
20029    this mode may be BLKmode if no more suitable mode is available.  */
20030
20031 static tree
20032 neon_dereference_pointer (tree exp, enum machine_mode mem_mode,
20033                           enum machine_mode reg_mode,
20034                           neon_builtin_type_mode type_mode)
20035 {
20036   HOST_WIDE_INT reg_size, vector_size, nvectors, nelems;
20037   tree elem_type, upper_bound, array_type;
20038
20039   /* Work out the size of the register block in bytes.  */
20040   reg_size = GET_MODE_SIZE (reg_mode);
20041
20042   /* Work out the size of each vector in bytes.  */
20043   gcc_assert (TYPE_MODE_BIT (type_mode) & (TB_DREG | TB_QREG));
20044   vector_size = (TYPE_MODE_BIT (type_mode) & TB_QREG ? 16 : 8);
20045
20046   /* Work out how many vectors there are.  */
20047   gcc_assert (reg_size % vector_size == 0);
20048   nvectors = reg_size / vector_size;
20049
20050   /* Work out how many elements are being loaded or stored.
20051      MEM_MODE == REG_MODE implies a one-to-one mapping between register
20052      and memory elements; anything else implies a lane load or store.  */
20053   if (mem_mode == reg_mode)
20054     nelems = vector_size * nvectors;
20055   else
20056     nelems = nvectors;
20057
20058   /* Work out the type of each element.  */
20059   gcc_assert (POINTER_TYPE_P (TREE_TYPE (exp)));
20060   elem_type = TREE_TYPE (TREE_TYPE (exp));
20061
20062   /* Create a type that describes the full access.  */
20063   upper_bound = build_int_cst (size_type_node, nelems - 1);
20064   array_type = build_array_type (elem_type, build_index_type (upper_bound));
20065
20066   /* Dereference EXP using that type.  */
20067   exp = convert (build_pointer_type (array_type), exp);
20068   return fold_build2 (MEM_REF, array_type, exp,
20069                       build_int_cst (TREE_TYPE (exp), 0));
20070 }
20071
20072 /* Expand a Neon builtin.  */
20073 static rtx
20074 arm_expand_neon_args (rtx target, int icode, int have_retval,
20075                       neon_builtin_type_mode type_mode,
20076                       tree exp, ...)
20077 {
20078   va_list ap;
20079   rtx pat;
20080   tree arg[NEON_MAX_BUILTIN_ARGS];
20081   rtx op[NEON_MAX_BUILTIN_ARGS];
20082   enum machine_mode tmode = insn_data[icode].operand[0].mode;
20083   enum machine_mode mode[NEON_MAX_BUILTIN_ARGS];
20084   enum machine_mode other_mode;
20085   int argc = 0;
20086   int opno;
20087
20088   if (have_retval
20089       && (!target
20090           || GET_MODE (target) != tmode
20091           || !(*insn_data[icode].operand[0].predicate) (target, tmode)))
20092     target = gen_reg_rtx (tmode);
20093
20094   va_start (ap, exp);
20095
20096   for (;;)
20097     {
20098       builtin_arg thisarg = (builtin_arg) va_arg (ap, int);
20099
20100       if (thisarg == NEON_ARG_STOP)
20101         break;
20102       else
20103         {
20104           opno = argc + have_retval;
20105           mode[argc] = insn_data[icode].operand[opno].mode;
20106           arg[argc] = CALL_EXPR_ARG (exp, argc);
20107           if (thisarg == NEON_ARG_MEMORY)
20108             {
20109               other_mode = insn_data[icode].operand[1 - opno].mode;
20110               arg[argc] = neon_dereference_pointer (arg[argc], mode[argc],
20111                                                     other_mode, type_mode);
20112             }
20113           op[argc] = expand_normal (arg[argc]);
20114
20115           switch (thisarg)
20116             {
20117             case NEON_ARG_COPY_TO_REG:
20118               /*gcc_assert (GET_MODE (op[argc]) == mode[argc]);*/
20119               if (!(*insn_data[icode].operand[opno].predicate)
20120                      (op[argc], mode[argc]))
20121                 op[argc] = copy_to_mode_reg (mode[argc], op[argc]);
20122               break;
20123
20124             case NEON_ARG_CONSTANT:
20125               /* FIXME: This error message is somewhat unhelpful.  */
20126               if (!(*insn_data[icode].operand[opno].predicate)
20127                     (op[argc], mode[argc]))
20128                 error ("argument must be a constant");
20129               break;
20130
20131             case NEON_ARG_MEMORY:
20132               gcc_assert (MEM_P (op[argc]));
20133               PUT_MODE (op[argc], mode[argc]);
20134               /* ??? arm_neon.h uses the same built-in functions for signed
20135                  and unsigned accesses, casting where necessary.  This isn't
20136                  alias safe.  */
20137               set_mem_alias_set (op[argc], 0);
20138               if (!(*insn_data[icode].operand[opno].predicate)
20139                     (op[argc], mode[argc]))
20140                 op[argc] = (replace_equiv_address
20141                             (op[argc], force_reg (Pmode, XEXP (op[argc], 0))));
20142               break;
20143
20144             case NEON_ARG_STOP:
20145               gcc_unreachable ();
20146             }
20147
20148           argc++;
20149         }
20150     }
20151
20152   va_end (ap);
20153
20154   if (have_retval)
20155     switch (argc)
20156       {
20157       case 1:
20158         pat = GEN_FCN (icode) (target, op[0]);
20159         break;
20160
20161       case 2:
20162         pat = GEN_FCN (icode) (target, op[0], op[1]);
20163         break;
20164
20165       case 3:
20166         pat = GEN_FCN (icode) (target, op[0], op[1], op[2]);
20167         break;
20168
20169       case 4:
20170         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3]);
20171         break;
20172
20173       case 5:
20174         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3], op[4]);
20175         break;
20176
20177       default:
20178         gcc_unreachable ();
20179       }
20180   else
20181     switch (argc)
20182       {
20183       case 1:
20184         pat = GEN_FCN (icode) (op[0]);
20185         break;
20186
20187       case 2:
20188         pat = GEN_FCN (icode) (op[0], op[1]);
20189         break;
20190
20191       case 3:
20192         pat = GEN_FCN (icode) (op[0], op[1], op[2]);
20193         break;
20194
20195       case 4:
20196         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
20197         break;
20198
20199       case 5:
20200         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4]);
20201         break;
20202
20203       default:
20204         gcc_unreachable ();
20205       }
20206
20207   if (!pat)
20208     return 0;
20209
20210   emit_insn (pat);
20211
20212   return target;
20213 }
20214
20215 /* Expand a Neon builtin. These are "special" because they don't have symbolic
20216    constants defined per-instruction or per instruction-variant. Instead, the
20217    required info is looked up in the table neon_builtin_data.  */
20218 static rtx
20219 arm_expand_neon_builtin (int fcode, tree exp, rtx target)
20220 {
20221   neon_builtin_datum *d = &neon_builtin_data[fcode - ARM_BUILTIN_NEON_BASE];
20222   neon_itype itype = d->itype;
20223   enum insn_code icode = d->code;
20224   neon_builtin_type_mode type_mode = d->mode;
20225
20226   switch (itype)
20227     {
20228     case NEON_UNOP:
20229     case NEON_CONVERT:
20230     case NEON_DUPLANE:
20231       return arm_expand_neon_args (target, icode, 1, type_mode, exp,
20232         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_STOP);
20233
20234     case NEON_BINOP:
20235     case NEON_SETLANE:
20236     case NEON_SCALARMUL:
20237     case NEON_SCALARMULL:
20238     case NEON_SCALARMULH:
20239     case NEON_SHIFTINSERT:
20240     case NEON_LOGICBINOP:
20241       return arm_expand_neon_args (target, icode, 1, type_mode, exp,
20242         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
20243         NEON_ARG_STOP);
20244
20245     case NEON_TERNOP:
20246       return arm_expand_neon_args (target, icode, 1, type_mode, exp,
20247         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
20248         NEON_ARG_CONSTANT, NEON_ARG_STOP);
20249
20250     case NEON_GETLANE:
20251     case NEON_FIXCONV:
20252     case NEON_SHIFTIMM:
20253       return arm_expand_neon_args (target, icode, 1, type_mode, exp,
20254         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_CONSTANT,
20255         NEON_ARG_STOP);
20256
20257     case NEON_CREATE:
20258       return arm_expand_neon_args (target, icode, 1, type_mode, exp,
20259         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
20260
20261     case NEON_DUP:
20262     case NEON_SPLIT:
20263     case NEON_REINTERP:
20264       return arm_expand_neon_args (target, icode, 1, type_mode, exp,
20265         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
20266
20267     case NEON_COMBINE:
20268     case NEON_VTBL:
20269       return arm_expand_neon_args (target, icode, 1, type_mode, exp,
20270         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
20271
20272     case NEON_RESULTPAIR:
20273       return arm_expand_neon_args (target, icode, 0, type_mode, exp,
20274         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
20275         NEON_ARG_STOP);
20276
20277     case NEON_LANEMUL:
20278     case NEON_LANEMULL:
20279     case NEON_LANEMULH:
20280       return arm_expand_neon_args (target, icode, 1, type_mode, exp,
20281         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
20282         NEON_ARG_CONSTANT, NEON_ARG_STOP);
20283
20284     case NEON_LANEMAC:
20285       return arm_expand_neon_args (target, icode, 1, type_mode, exp,
20286         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
20287         NEON_ARG_CONSTANT, NEON_ARG_CONSTANT, NEON_ARG_STOP);
20288
20289     case NEON_SHIFTACC:
20290       return arm_expand_neon_args (target, icode, 1, type_mode, exp,
20291         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
20292         NEON_ARG_CONSTANT, NEON_ARG_STOP);
20293
20294     case NEON_SCALARMAC:
20295       return arm_expand_neon_args (target, icode, 1, type_mode, exp,
20296         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
20297         NEON_ARG_CONSTANT, NEON_ARG_STOP);
20298
20299     case NEON_SELECT:
20300     case NEON_VTBX:
20301       return arm_expand_neon_args (target, icode, 1, type_mode, exp,
20302         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
20303         NEON_ARG_STOP);
20304
20305     case NEON_LOAD1:
20306     case NEON_LOADSTRUCT:
20307       return arm_expand_neon_args (target, icode, 1, type_mode, exp,
20308         NEON_ARG_MEMORY, NEON_ARG_STOP);
20309
20310     case NEON_LOAD1LANE:
20311     case NEON_LOADSTRUCTLANE:
20312       return arm_expand_neon_args (target, icode, 1, type_mode, exp,
20313         NEON_ARG_MEMORY, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
20314         NEON_ARG_STOP);
20315
20316     case NEON_STORE1:
20317     case NEON_STORESTRUCT:
20318       return arm_expand_neon_args (target, icode, 0, type_mode, exp,
20319         NEON_ARG_MEMORY, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
20320
20321     case NEON_STORE1LANE:
20322     case NEON_STORESTRUCTLANE:
20323       return arm_expand_neon_args (target, icode, 0, type_mode, exp,
20324         NEON_ARG_MEMORY, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
20325         NEON_ARG_STOP);
20326     }
20327
20328   gcc_unreachable ();
20329 }
20330
20331 /* Emit code to reinterpret one Neon type as another, without altering bits.  */
20332 void
20333 neon_reinterpret (rtx dest, rtx src)
20334 {
20335   emit_move_insn (dest, gen_lowpart (GET_MODE (dest), src));
20336 }
20337
20338 /* Emit code to place a Neon pair result in memory locations (with equal
20339    registers).  */
20340 void
20341 neon_emit_pair_result_insn (enum machine_mode mode,
20342                             rtx (*intfn) (rtx, rtx, rtx, rtx), rtx destaddr,
20343                             rtx op1, rtx op2)
20344 {
20345   rtx mem = gen_rtx_MEM (mode, destaddr);
20346   rtx tmp1 = gen_reg_rtx (mode);
20347   rtx tmp2 = gen_reg_rtx (mode);
20348
20349   emit_insn (intfn (tmp1, op1, op2, tmp2));
20350
20351   emit_move_insn (mem, tmp1);
20352   mem = adjust_address (mem, mode, GET_MODE_SIZE (mode));
20353   emit_move_insn (mem, tmp2);
20354 }
20355
20356 /* Set up operands for a register copy from src to dest, taking care not to
20357    clobber registers in the process.
20358    FIXME: This has rather high polynomial complexity (O(n^3)?) but shouldn't
20359    be called with a large N, so that should be OK.  */
20360
20361 void
20362 neon_disambiguate_copy (rtx *operands, rtx *dest, rtx *src, unsigned int count)
20363 {
20364   unsigned int copied = 0, opctr = 0;
20365   unsigned int done = (1 << count) - 1;
20366   unsigned int i, j;
20367
20368   while (copied != done)
20369     {
20370       for (i = 0; i < count; i++)
20371         {
20372           int good = 1;
20373
20374           for (j = 0; good && j < count; j++)
20375             if (i != j && (copied & (1 << j)) == 0
20376                 && reg_overlap_mentioned_p (src[j], dest[i]))
20377               good = 0;
20378
20379           if (good)
20380             {
20381               operands[opctr++] = dest[i];
20382               operands[opctr++] = src[i];
20383               copied |= 1 << i;
20384             }
20385         }
20386     }
20387
20388   gcc_assert (opctr == count * 2);
20389 }
20390
20391 /* Expand an expression EXP that calls a built-in function,
20392    with result going to TARGET if that's convenient
20393    (and in mode MODE if that's convenient).
20394    SUBTARGET may be used as the target for computing one of EXP's operands.
20395    IGNORE is nonzero if the value is to be ignored.  */
20396
20397 static rtx
20398 arm_expand_builtin (tree exp,
20399                     rtx target,
20400                     rtx subtarget ATTRIBUTE_UNUSED,
20401                     enum machine_mode mode ATTRIBUTE_UNUSED,
20402                     int ignore ATTRIBUTE_UNUSED)
20403 {
20404   const struct builtin_description * d;
20405   enum insn_code    icode;
20406   tree              fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
20407   tree              arg0;
20408   tree              arg1;
20409   tree              arg2;
20410   rtx               op0;
20411   rtx               op1;
20412   rtx               op2;
20413   rtx               pat;
20414   int               fcode = DECL_FUNCTION_CODE (fndecl);
20415   size_t            i;
20416   enum machine_mode tmode;
20417   enum machine_mode mode0;
20418   enum machine_mode mode1;
20419   enum machine_mode mode2;
20420
20421   if (fcode >= ARM_BUILTIN_NEON_BASE)
20422     return arm_expand_neon_builtin (fcode, exp, target);
20423
20424   switch (fcode)
20425     {
20426     case ARM_BUILTIN_TEXTRMSB:
20427     case ARM_BUILTIN_TEXTRMUB:
20428     case ARM_BUILTIN_TEXTRMSH:
20429     case ARM_BUILTIN_TEXTRMUH:
20430     case ARM_BUILTIN_TEXTRMSW:
20431     case ARM_BUILTIN_TEXTRMUW:
20432       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
20433                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
20434                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
20435                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
20436                : CODE_FOR_iwmmxt_textrmw);
20437
20438       arg0 = CALL_EXPR_ARG (exp, 0);
20439       arg1 = CALL_EXPR_ARG (exp, 1);
20440       op0 = expand_normal (arg0);
20441       op1 = expand_normal (arg1);
20442       tmode = insn_data[icode].operand[0].mode;
20443       mode0 = insn_data[icode].operand[1].mode;
20444       mode1 = insn_data[icode].operand[2].mode;
20445
20446       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
20447         op0 = copy_to_mode_reg (mode0, op0);
20448       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
20449         {
20450           /* @@@ better error message */
20451           error ("selector must be an immediate");
20452           return gen_reg_rtx (tmode);
20453         }
20454       if (target == 0
20455           || GET_MODE (target) != tmode
20456           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
20457         target = gen_reg_rtx (tmode);
20458       pat = GEN_FCN (icode) (target, op0, op1);
20459       if (! pat)
20460         return 0;
20461       emit_insn (pat);
20462       return target;
20463
20464     case ARM_BUILTIN_TINSRB:
20465     case ARM_BUILTIN_TINSRH:
20466     case ARM_BUILTIN_TINSRW:
20467       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
20468                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
20469                : CODE_FOR_iwmmxt_tinsrw);
20470       arg0 = CALL_EXPR_ARG (exp, 0);
20471       arg1 = CALL_EXPR_ARG (exp, 1);
20472       arg2 = CALL_EXPR_ARG (exp, 2);
20473       op0 = expand_normal (arg0);
20474       op1 = expand_normal (arg1);
20475       op2 = expand_normal (arg2);
20476       tmode = insn_data[icode].operand[0].mode;
20477       mode0 = insn_data[icode].operand[1].mode;
20478       mode1 = insn_data[icode].operand[2].mode;
20479       mode2 = insn_data[icode].operand[3].mode;
20480
20481       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
20482         op0 = copy_to_mode_reg (mode0, op0);
20483       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
20484         op1 = copy_to_mode_reg (mode1, op1);
20485       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
20486         {
20487           /* @@@ better error message */
20488           error ("selector must be an immediate");
20489           return const0_rtx;
20490         }
20491       if (target == 0
20492           || GET_MODE (target) != tmode
20493           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
20494         target = gen_reg_rtx (tmode);
20495       pat = GEN_FCN (icode) (target, op0, op1, op2);
20496       if (! pat)
20497         return 0;
20498       emit_insn (pat);
20499       return target;
20500
20501     case ARM_BUILTIN_SETWCX:
20502       arg0 = CALL_EXPR_ARG (exp, 0);
20503       arg1 = CALL_EXPR_ARG (exp, 1);
20504       op0 = force_reg (SImode, expand_normal (arg0));
20505       op1 = expand_normal (arg1);
20506       emit_insn (gen_iwmmxt_tmcr (op1, op0));
20507       return 0;
20508
20509     case ARM_BUILTIN_GETWCX:
20510       arg0 = CALL_EXPR_ARG (exp, 0);
20511       op0 = expand_normal (arg0);
20512       target = gen_reg_rtx (SImode);
20513       emit_insn (gen_iwmmxt_tmrc (target, op0));
20514       return target;
20515
20516     case ARM_BUILTIN_WSHUFH:
20517       icode = CODE_FOR_iwmmxt_wshufh;
20518       arg0 = CALL_EXPR_ARG (exp, 0);
20519       arg1 = CALL_EXPR_ARG (exp, 1);
20520       op0 = expand_normal (arg0);
20521       op1 = expand_normal (arg1);
20522       tmode = insn_data[icode].operand[0].mode;
20523       mode1 = insn_data[icode].operand[1].mode;
20524       mode2 = insn_data[icode].operand[2].mode;
20525
20526       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
20527         op0 = copy_to_mode_reg (mode1, op0);
20528       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
20529         {
20530           /* @@@ better error message */
20531           error ("mask must be an immediate");
20532           return const0_rtx;
20533         }
20534       if (target == 0
20535           || GET_MODE (target) != tmode
20536           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
20537         target = gen_reg_rtx (tmode);
20538       pat = GEN_FCN (icode) (target, op0, op1);
20539       if (! pat)
20540         return 0;
20541       emit_insn (pat);
20542       return target;
20543
20544     case ARM_BUILTIN_WSADB:
20545       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, exp, target);
20546     case ARM_BUILTIN_WSADH:
20547       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, exp, target);
20548     case ARM_BUILTIN_WSADBZ:
20549       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, exp, target);
20550     case ARM_BUILTIN_WSADHZ:
20551       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, exp, target);
20552
20553       /* Several three-argument builtins.  */
20554     case ARM_BUILTIN_WMACS:
20555     case ARM_BUILTIN_WMACU:
20556     case ARM_BUILTIN_WALIGN:
20557     case ARM_BUILTIN_TMIA:
20558     case ARM_BUILTIN_TMIAPH:
20559     case ARM_BUILTIN_TMIATT:
20560     case ARM_BUILTIN_TMIATB:
20561     case ARM_BUILTIN_TMIABT:
20562     case ARM_BUILTIN_TMIABB:
20563       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
20564                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
20565                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
20566                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
20567                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
20568                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
20569                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
20570                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
20571                : CODE_FOR_iwmmxt_walign);
20572       arg0 = CALL_EXPR_ARG (exp, 0);
20573       arg1 = CALL_EXPR_ARG (exp, 1);
20574       arg2 = CALL_EXPR_ARG (exp, 2);
20575       op0 = expand_normal (arg0);
20576       op1 = expand_normal (arg1);
20577       op2 = expand_normal (arg2);
20578       tmode = insn_data[icode].operand[0].mode;
20579       mode0 = insn_data[icode].operand[1].mode;
20580       mode1 = insn_data[icode].operand[2].mode;
20581       mode2 = insn_data[icode].operand[3].mode;
20582
20583       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
20584         op0 = copy_to_mode_reg (mode0, op0);
20585       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
20586         op1 = copy_to_mode_reg (mode1, op1);
20587       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
20588         op2 = copy_to_mode_reg (mode2, op2);
20589       if (target == 0
20590           || GET_MODE (target) != tmode
20591           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
20592         target = gen_reg_rtx (tmode);
20593       pat = GEN_FCN (icode) (target, op0, op1, op2);
20594       if (! pat)
20595         return 0;
20596       emit_insn (pat);
20597       return target;
20598
20599     case ARM_BUILTIN_WZERO:
20600       target = gen_reg_rtx (DImode);
20601       emit_insn (gen_iwmmxt_clrdi (target));
20602       return target;
20603
20604     case ARM_BUILTIN_THREAD_POINTER:
20605       return arm_load_tp (target);
20606
20607     default:
20608       break;
20609     }
20610
20611   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
20612     if (d->code == (const enum arm_builtins) fcode)
20613       return arm_expand_binop_builtin (d->icode, exp, target);
20614
20615   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
20616     if (d->code == (const enum arm_builtins) fcode)
20617       return arm_expand_unop_builtin (d->icode, exp, target, 0);
20618
20619   /* @@@ Should really do something sensible here.  */
20620   return NULL_RTX;
20621 }
20622 \f
20623 /* Return the number (counting from 0) of
20624    the least significant set bit in MASK.  */
20625
20626 inline static int
20627 number_of_first_bit_set (unsigned mask)
20628 {
20629   return ctz_hwi (mask);
20630 }
20631
20632 /* Like emit_multi_reg_push, but allowing for a different set of 
20633    registers to be described as saved.  MASK is the set of registers
20634    to be saved; REAL_REGS is the set of registers to be described as
20635    saved.  If REAL_REGS is 0, only describe the stack adjustment.  */
20636
20637 static rtx
20638 thumb1_emit_multi_reg_push (unsigned long mask, unsigned long real_regs)
20639 {
20640   unsigned long regno;
20641   rtx par[10], tmp, reg, insn;
20642   int i, j;
20643
20644   /* Build the parallel of the registers actually being stored.  */
20645   for (i = 0; mask; ++i, mask &= mask - 1)
20646     {
20647       regno = ctz_hwi (mask);
20648       reg = gen_rtx_REG (SImode, regno);
20649
20650       if (i == 0)
20651         tmp = gen_rtx_UNSPEC (BLKmode, gen_rtvec (1, reg), UNSPEC_PUSH_MULT);
20652       else
20653         tmp = gen_rtx_USE (VOIDmode, reg);
20654
20655       par[i] = tmp;
20656     }
20657
20658   tmp = plus_constant (stack_pointer_rtx, -4 * i);
20659   tmp = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, tmp);
20660   tmp = gen_frame_mem (BLKmode, tmp);
20661   tmp = gen_rtx_SET (VOIDmode, tmp, par[0]);
20662   par[0] = tmp;
20663
20664   tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (i, par));
20665   insn = emit_insn (tmp);
20666
20667   /* Always build the stack adjustment note for unwind info.  */
20668   tmp = plus_constant (stack_pointer_rtx, -4 * i);
20669   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx, tmp);
20670   par[0] = tmp;
20671
20672   /* Build the parallel of the registers recorded as saved for unwind.  */
20673   for (j = 0; real_regs; ++j, real_regs &= real_regs - 1)
20674     {
20675       regno = ctz_hwi (real_regs);
20676       reg = gen_rtx_REG (SImode, regno);
20677
20678       tmp = plus_constant (stack_pointer_rtx, j * 4);
20679       tmp = gen_frame_mem (SImode, tmp);
20680       tmp = gen_rtx_SET (VOIDmode, tmp, reg);
20681       RTX_FRAME_RELATED_P (tmp) = 1;
20682       par[j + 1] = tmp;
20683     }
20684
20685   if (j == 0)
20686     tmp = par[0];
20687   else
20688     {
20689       RTX_FRAME_RELATED_P (par[0]) = 1;
20690       tmp = gen_rtx_SEQUENCE (VOIDmode, gen_rtvec_v (j + 1, par));
20691     }
20692
20693   add_reg_note (insn, REG_FRAME_RELATED_EXPR, tmp);
20694
20695   return insn;
20696 }
20697
20698 /* Emit code to push or pop registers to or from the stack.  F is the
20699    assembly file.  MASK is the registers to pop.  */
20700 static void
20701 thumb_pop (FILE *f, unsigned long mask)
20702 {
20703   int regno;
20704   int lo_mask = mask & 0xFF;
20705   int pushed_words = 0;
20706
20707   gcc_assert (mask);
20708
20709   if (lo_mask == 0 && (mask & (1 << PC_REGNUM)))
20710     {
20711       /* Special case.  Do not generate a POP PC statement here, do it in
20712          thumb_exit() */
20713       thumb_exit (f, -1);
20714       return;
20715     }
20716
20717   fprintf (f, "\tpop\t{");
20718
20719   /* Look at the low registers first.  */
20720   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
20721     {
20722       if (lo_mask & 1)
20723         {
20724           asm_fprintf (f, "%r", regno);
20725
20726           if ((lo_mask & ~1) != 0)
20727             fprintf (f, ", ");
20728
20729           pushed_words++;
20730         }
20731     }
20732
20733   if (mask & (1 << PC_REGNUM))
20734     {
20735       /* Catch popping the PC.  */
20736       if (TARGET_INTERWORK || TARGET_BACKTRACE
20737           || crtl->calls_eh_return)
20738         {
20739           /* The PC is never poped directly, instead
20740              it is popped into r3 and then BX is used.  */
20741           fprintf (f, "}\n");
20742
20743           thumb_exit (f, -1);
20744
20745           return;
20746         }
20747       else
20748         {
20749           if (mask & 0xFF)
20750             fprintf (f, ", ");
20751
20752           asm_fprintf (f, "%r", PC_REGNUM);
20753         }
20754     }
20755
20756   fprintf (f, "}\n");
20757 }
20758
20759 /* Generate code to return from a thumb function.
20760    If 'reg_containing_return_addr' is -1, then the return address is
20761    actually on the stack, at the stack pointer.  */
20762 static void
20763 thumb_exit (FILE *f, int reg_containing_return_addr)
20764 {
20765   unsigned regs_available_for_popping;
20766   unsigned regs_to_pop;
20767   int pops_needed;
20768   unsigned available;
20769   unsigned required;
20770   int mode;
20771   int size;
20772   int restore_a4 = FALSE;
20773
20774   /* Compute the registers we need to pop.  */
20775   regs_to_pop = 0;
20776   pops_needed = 0;
20777
20778   if (reg_containing_return_addr == -1)
20779     {
20780       regs_to_pop |= 1 << LR_REGNUM;
20781       ++pops_needed;
20782     }
20783
20784   if (TARGET_BACKTRACE)
20785     {
20786       /* Restore the (ARM) frame pointer and stack pointer.  */
20787       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
20788       pops_needed += 2;
20789     }
20790
20791   /* If there is nothing to pop then just emit the BX instruction and
20792      return.  */
20793   if (pops_needed == 0)
20794     {
20795       if (crtl->calls_eh_return)
20796         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
20797
20798       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
20799       return;
20800     }
20801   /* Otherwise if we are not supporting interworking and we have not created
20802      a backtrace structure and the function was not entered in ARM mode then
20803      just pop the return address straight into the PC.  */
20804   else if (!TARGET_INTERWORK
20805            && !TARGET_BACKTRACE
20806            && !is_called_in_ARM_mode (current_function_decl)
20807            && !crtl->calls_eh_return)
20808     {
20809       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
20810       return;
20811     }
20812
20813   /* Find out how many of the (return) argument registers we can corrupt.  */
20814   regs_available_for_popping = 0;
20815
20816   /* If returning via __builtin_eh_return, the bottom three registers
20817      all contain information needed for the return.  */
20818   if (crtl->calls_eh_return)
20819     size = 12;
20820   else
20821     {
20822       /* If we can deduce the registers used from the function's
20823          return value.  This is more reliable that examining
20824          df_regs_ever_live_p () because that will be set if the register is
20825          ever used in the function, not just if the register is used
20826          to hold a return value.  */
20827
20828       if (crtl->return_rtx != 0)
20829         mode = GET_MODE (crtl->return_rtx);
20830       else
20831         mode = DECL_MODE (DECL_RESULT (current_function_decl));
20832
20833       size = GET_MODE_SIZE (mode);
20834
20835       if (size == 0)
20836         {
20837           /* In a void function we can use any argument register.
20838              In a function that returns a structure on the stack
20839              we can use the second and third argument registers.  */
20840           if (mode == VOIDmode)
20841             regs_available_for_popping =
20842               (1 << ARG_REGISTER (1))
20843               | (1 << ARG_REGISTER (2))
20844               | (1 << ARG_REGISTER (3));
20845           else
20846             regs_available_for_popping =
20847               (1 << ARG_REGISTER (2))
20848               | (1 << ARG_REGISTER (3));
20849         }
20850       else if (size <= 4)
20851         regs_available_for_popping =
20852           (1 << ARG_REGISTER (2))
20853           | (1 << ARG_REGISTER (3));
20854       else if (size <= 8)
20855         regs_available_for_popping =
20856           (1 << ARG_REGISTER (3));
20857     }
20858
20859   /* Match registers to be popped with registers into which we pop them.  */
20860   for (available = regs_available_for_popping,
20861        required  = regs_to_pop;
20862        required != 0 && available != 0;
20863        available &= ~(available & - available),
20864        required  &= ~(required  & - required))
20865     -- pops_needed;
20866
20867   /* If we have any popping registers left over, remove them.  */
20868   if (available > 0)
20869     regs_available_for_popping &= ~available;
20870
20871   /* Otherwise if we need another popping register we can use
20872      the fourth argument register.  */
20873   else if (pops_needed)
20874     {
20875       /* If we have not found any free argument registers and
20876          reg a4 contains the return address, we must move it.  */
20877       if (regs_available_for_popping == 0
20878           && reg_containing_return_addr == LAST_ARG_REGNUM)
20879         {
20880           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
20881           reg_containing_return_addr = LR_REGNUM;
20882         }
20883       else if (size > 12)
20884         {
20885           /* Register a4 is being used to hold part of the return value,
20886              but we have dire need of a free, low register.  */
20887           restore_a4 = TRUE;
20888
20889           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
20890         }
20891
20892       if (reg_containing_return_addr != LAST_ARG_REGNUM)
20893         {
20894           /* The fourth argument register is available.  */
20895           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
20896
20897           --pops_needed;
20898         }
20899     }
20900
20901   /* Pop as many registers as we can.  */
20902   thumb_pop (f, regs_available_for_popping);
20903
20904   /* Process the registers we popped.  */
20905   if (reg_containing_return_addr == -1)
20906     {
20907       /* The return address was popped into the lowest numbered register.  */
20908       regs_to_pop &= ~(1 << LR_REGNUM);
20909
20910       reg_containing_return_addr =
20911         number_of_first_bit_set (regs_available_for_popping);
20912
20913       /* Remove this register for the mask of available registers, so that
20914          the return address will not be corrupted by further pops.  */
20915       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
20916     }
20917
20918   /* If we popped other registers then handle them here.  */
20919   if (regs_available_for_popping)
20920     {
20921       int frame_pointer;
20922
20923       /* Work out which register currently contains the frame pointer.  */
20924       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
20925
20926       /* Move it into the correct place.  */
20927       asm_fprintf (f, "\tmov\t%r, %r\n",
20928                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
20929
20930       /* (Temporarily) remove it from the mask of popped registers.  */
20931       regs_available_for_popping &= ~(1 << frame_pointer);
20932       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
20933
20934       if (regs_available_for_popping)
20935         {
20936           int stack_pointer;
20937
20938           /* We popped the stack pointer as well,
20939              find the register that contains it.  */
20940           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
20941
20942           /* Move it into the stack register.  */
20943           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
20944
20945           /* At this point we have popped all necessary registers, so
20946              do not worry about restoring regs_available_for_popping
20947              to its correct value:
20948
20949              assert (pops_needed == 0)
20950              assert (regs_available_for_popping == (1 << frame_pointer))
20951              assert (regs_to_pop == (1 << STACK_POINTER))  */
20952         }
20953       else
20954         {
20955           /* Since we have just move the popped value into the frame
20956              pointer, the popping register is available for reuse, and
20957              we know that we still have the stack pointer left to pop.  */
20958           regs_available_for_popping |= (1 << frame_pointer);
20959         }
20960     }
20961
20962   /* If we still have registers left on the stack, but we no longer have
20963      any registers into which we can pop them, then we must move the return
20964      address into the link register and make available the register that
20965      contained it.  */
20966   if (regs_available_for_popping == 0 && pops_needed > 0)
20967     {
20968       regs_available_for_popping |= 1 << reg_containing_return_addr;
20969
20970       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
20971                    reg_containing_return_addr);
20972
20973       reg_containing_return_addr = LR_REGNUM;
20974     }
20975
20976   /* If we have registers left on the stack then pop some more.
20977      We know that at most we will want to pop FP and SP.  */
20978   if (pops_needed > 0)
20979     {
20980       int  popped_into;
20981       int  move_to;
20982
20983       thumb_pop (f, regs_available_for_popping);
20984
20985       /* We have popped either FP or SP.
20986          Move whichever one it is into the correct register.  */
20987       popped_into = number_of_first_bit_set (regs_available_for_popping);
20988       move_to     = number_of_first_bit_set (regs_to_pop);
20989
20990       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
20991
20992       regs_to_pop &= ~(1 << move_to);
20993
20994       --pops_needed;
20995     }
20996
20997   /* If we still have not popped everything then we must have only
20998      had one register available to us and we are now popping the SP.  */
20999   if (pops_needed > 0)
21000     {
21001       int  popped_into;
21002
21003       thumb_pop (f, regs_available_for_popping);
21004
21005       popped_into = number_of_first_bit_set (regs_available_for_popping);
21006
21007       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
21008       /*
21009         assert (regs_to_pop == (1 << STACK_POINTER))
21010         assert (pops_needed == 1)
21011       */
21012     }
21013
21014   /* If necessary restore the a4 register.  */
21015   if (restore_a4)
21016     {
21017       if (reg_containing_return_addr != LR_REGNUM)
21018         {
21019           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
21020           reg_containing_return_addr = LR_REGNUM;
21021         }
21022
21023       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
21024     }
21025
21026   if (crtl->calls_eh_return)
21027     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
21028
21029   /* Return to caller.  */
21030   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
21031 }
21032 \f
21033 /* Scan INSN just before assembler is output for it.
21034    For Thumb-1, we track the status of the condition codes; this
21035    information is used in the cbranchsi4_insn pattern.  */
21036 void
21037 thumb1_final_prescan_insn (rtx insn)
21038 {
21039   if (flag_print_asm_name)
21040     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
21041                  INSN_ADDRESSES (INSN_UID (insn)));
21042   /* Don't overwrite the previous setter when we get to a cbranch.  */
21043   if (INSN_CODE (insn) != CODE_FOR_cbranchsi4_insn)
21044     {
21045       enum attr_conds conds;
21046
21047       if (cfun->machine->thumb1_cc_insn)
21048         {
21049           if (modified_in_p (cfun->machine->thumb1_cc_op0, insn)
21050               || modified_in_p (cfun->machine->thumb1_cc_op1, insn))
21051             CC_STATUS_INIT;
21052         }
21053       conds = get_attr_conds (insn);
21054       if (conds == CONDS_SET)
21055         {
21056           rtx set = single_set (insn);
21057           cfun->machine->thumb1_cc_insn = insn;
21058           cfun->machine->thumb1_cc_op0 = SET_DEST (set);
21059           cfun->machine->thumb1_cc_op1 = const0_rtx;
21060           cfun->machine->thumb1_cc_mode = CC_NOOVmode;
21061           if (INSN_CODE (insn) == CODE_FOR_thumb1_subsi3_insn)
21062             {
21063               rtx src1 = XEXP (SET_SRC (set), 1);
21064               if (src1 == const0_rtx)
21065                 cfun->machine->thumb1_cc_mode = CCmode;
21066             }
21067         }
21068       else if (conds != CONDS_NOCOND)
21069         cfun->machine->thumb1_cc_insn = NULL_RTX;
21070     }
21071 }
21072
21073 int
21074 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
21075 {
21076   unsigned HOST_WIDE_INT mask = 0xff;
21077   int i;
21078
21079   val = val & (unsigned HOST_WIDE_INT)0xffffffffu;
21080   if (val == 0) /* XXX */
21081     return 0;
21082
21083   for (i = 0; i < 25; i++)
21084     if ((val & (mask << i)) == val)
21085       return 1;
21086
21087   return 0;
21088 }
21089
21090 /* Returns nonzero if the current function contains,
21091    or might contain a far jump.  */
21092 static int
21093 thumb_far_jump_used_p (void)
21094 {
21095   rtx insn;
21096
21097   /* This test is only important for leaf functions.  */
21098   /* assert (!leaf_function_p ()); */
21099
21100   /* If we have already decided that far jumps may be used,
21101      do not bother checking again, and always return true even if
21102      it turns out that they are not being used.  Once we have made
21103      the decision that far jumps are present (and that hence the link
21104      register will be pushed onto the stack) we cannot go back on it.  */
21105   if (cfun->machine->far_jump_used)
21106     return 1;
21107
21108   /* If this function is not being called from the prologue/epilogue
21109      generation code then it must be being called from the
21110      INITIAL_ELIMINATION_OFFSET macro.  */
21111   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
21112     {
21113       /* In this case we know that we are being asked about the elimination
21114          of the arg pointer register.  If that register is not being used,
21115          then there are no arguments on the stack, and we do not have to
21116          worry that a far jump might force the prologue to push the link
21117          register, changing the stack offsets.  In this case we can just
21118          return false, since the presence of far jumps in the function will
21119          not affect stack offsets.
21120
21121          If the arg pointer is live (or if it was live, but has now been
21122          eliminated and so set to dead) then we do have to test to see if
21123          the function might contain a far jump.  This test can lead to some
21124          false negatives, since before reload is completed, then length of
21125          branch instructions is not known, so gcc defaults to returning their
21126          longest length, which in turn sets the far jump attribute to true.
21127
21128          A false negative will not result in bad code being generated, but it
21129          will result in a needless push and pop of the link register.  We
21130          hope that this does not occur too often.
21131
21132          If we need doubleword stack alignment this could affect the other
21133          elimination offsets so we can't risk getting it wrong.  */
21134       if (df_regs_ever_live_p (ARG_POINTER_REGNUM))
21135         cfun->machine->arg_pointer_live = 1;
21136       else if (!cfun->machine->arg_pointer_live)
21137         return 0;
21138     }
21139
21140   /* Check to see if the function contains a branch
21141      insn with the far jump attribute set.  */
21142   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
21143     {
21144       if (GET_CODE (insn) == JUMP_INSN
21145           /* Ignore tablejump patterns.  */
21146           && GET_CODE (PATTERN (insn)) != ADDR_VEC
21147           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
21148           && get_attr_far_jump (insn) == FAR_JUMP_YES
21149           )
21150         {
21151           /* Record the fact that we have decided that
21152              the function does use far jumps.  */
21153           cfun->machine->far_jump_used = 1;
21154           return 1;
21155         }
21156     }
21157
21158   return 0;
21159 }
21160
21161 /* Return nonzero if FUNC must be entered in ARM mode.  */
21162 int
21163 is_called_in_ARM_mode (tree func)
21164 {
21165   gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
21166
21167   /* Ignore the problem about functions whose address is taken.  */
21168   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
21169     return TRUE;
21170
21171 #ifdef ARM_PE
21172   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
21173 #else
21174   return FALSE;
21175 #endif
21176 }
21177
21178 /* Given the stack offsets and register mask in OFFSETS, decide how
21179    many additional registers to push instead of subtracting a constant
21180    from SP.  For epilogues the principle is the same except we use pop.
21181    FOR_PROLOGUE indicates which we're generating.  */
21182 static int
21183 thumb1_extra_regs_pushed (arm_stack_offsets *offsets, bool for_prologue)
21184 {
21185   HOST_WIDE_INT amount;
21186   unsigned long live_regs_mask = offsets->saved_regs_mask;
21187   /* Extract a mask of the ones we can give to the Thumb's push/pop
21188      instruction.  */
21189   unsigned long l_mask = live_regs_mask & (for_prologue ? 0x40ff : 0xff);
21190   /* Then count how many other high registers will need to be pushed.  */
21191   unsigned long high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
21192   int n_free, reg_base;
21193
21194   if (!for_prologue && frame_pointer_needed)
21195     amount = offsets->locals_base - offsets->saved_regs;
21196   else
21197     amount = offsets->outgoing_args - offsets->saved_regs;
21198
21199   /* If the stack frame size is 512 exactly, we can save one load
21200      instruction, which should make this a win even when optimizing
21201      for speed.  */
21202   if (!optimize_size && amount != 512)
21203     return 0;
21204
21205   /* Can't do this if there are high registers to push.  */
21206   if (high_regs_pushed != 0)
21207     return 0;
21208
21209   /* Shouldn't do it in the prologue if no registers would normally
21210      be pushed at all.  In the epilogue, also allow it if we'll have
21211      a pop insn for the PC.  */
21212   if  (l_mask == 0
21213        && (for_prologue
21214            || TARGET_BACKTRACE
21215            || (live_regs_mask & 1 << LR_REGNUM) == 0
21216            || TARGET_INTERWORK
21217            || crtl->args.pretend_args_size != 0))
21218     return 0;
21219
21220   /* Don't do this if thumb_expand_prologue wants to emit instructions
21221      between the push and the stack frame allocation.  */
21222   if (for_prologue
21223       && ((flag_pic && arm_pic_register != INVALID_REGNUM)
21224           || (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)))
21225     return 0;
21226
21227   reg_base = 0;
21228   n_free = 0;
21229   if (!for_prologue)
21230     {
21231       reg_base = arm_size_return_regs () / UNITS_PER_WORD;
21232       live_regs_mask >>= reg_base;
21233     }
21234
21235   while (reg_base + n_free < 8 && !(live_regs_mask & 1)
21236          && (for_prologue || call_used_regs[reg_base + n_free]))
21237     {
21238       live_regs_mask >>= 1;
21239       n_free++;
21240     }
21241
21242   if (n_free == 0)
21243     return 0;
21244   gcc_assert (amount / 4 * 4 == amount);
21245
21246   if (amount >= 512 && (amount - n_free * 4) < 512)
21247     return (amount - 508) / 4;
21248   if (amount <= n_free * 4)
21249     return amount / 4;
21250   return 0;
21251 }
21252
21253 /* The bits which aren't usefully expanded as rtl.  */
21254 const char *
21255 thumb_unexpanded_epilogue (void)
21256 {
21257   arm_stack_offsets *offsets;
21258   int regno;
21259   unsigned long live_regs_mask = 0;
21260   int high_regs_pushed = 0;
21261   int extra_pop;
21262   int had_to_push_lr;
21263   int size;
21264
21265   if (cfun->machine->return_used_this_function != 0)
21266     return "";
21267
21268   if (IS_NAKED (arm_current_func_type ()))
21269     return "";
21270
21271   offsets = arm_get_frame_offsets ();
21272   live_regs_mask = offsets->saved_regs_mask;
21273   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
21274
21275   /* If we can deduce the registers used from the function's return value.
21276      This is more reliable that examining df_regs_ever_live_p () because that
21277      will be set if the register is ever used in the function, not just if
21278      the register is used to hold a return value.  */
21279   size = arm_size_return_regs ();
21280
21281   extra_pop = thumb1_extra_regs_pushed (offsets, false);
21282   if (extra_pop > 0)
21283     {
21284       unsigned long extra_mask = (1 << extra_pop) - 1;
21285       live_regs_mask |= extra_mask << (size / UNITS_PER_WORD);
21286     }
21287
21288   /* The prolog may have pushed some high registers to use as
21289      work registers.  e.g. the testsuite file:
21290      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
21291      compiles to produce:
21292         push    {r4, r5, r6, r7, lr}
21293         mov     r7, r9
21294         mov     r6, r8
21295         push    {r6, r7}
21296      as part of the prolog.  We have to undo that pushing here.  */
21297
21298   if (high_regs_pushed)
21299     {
21300       unsigned long mask = live_regs_mask & 0xff;
21301       int next_hi_reg;
21302
21303       /* The available low registers depend on the size of the value we are
21304          returning.  */
21305       if (size <= 12)
21306         mask |=  1 << 3;
21307       if (size <= 8)
21308         mask |= 1 << 2;
21309
21310       if (mask == 0)
21311         /* Oh dear!  We have no low registers into which we can pop
21312            high registers!  */
21313         internal_error
21314           ("no low registers available for popping high registers");
21315
21316       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
21317         if (live_regs_mask & (1 << next_hi_reg))
21318           break;
21319
21320       while (high_regs_pushed)
21321         {
21322           /* Find lo register(s) into which the high register(s) can
21323              be popped.  */
21324           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
21325             {
21326               if (mask & (1 << regno))
21327                 high_regs_pushed--;
21328               if (high_regs_pushed == 0)
21329                 break;
21330             }
21331
21332           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
21333
21334           /* Pop the values into the low register(s).  */
21335           thumb_pop (asm_out_file, mask);
21336
21337           /* Move the value(s) into the high registers.  */
21338           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
21339             {
21340               if (mask & (1 << regno))
21341                 {
21342                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
21343                                regno);
21344
21345                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
21346                     if (live_regs_mask & (1 << next_hi_reg))
21347                       break;
21348                 }
21349             }
21350         }
21351       live_regs_mask &= ~0x0f00;
21352     }
21353
21354   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
21355   live_regs_mask &= 0xff;
21356
21357   if (crtl->args.pretend_args_size == 0 || TARGET_BACKTRACE)
21358     {
21359       /* Pop the return address into the PC.  */
21360       if (had_to_push_lr)
21361         live_regs_mask |= 1 << PC_REGNUM;
21362
21363       /* Either no argument registers were pushed or a backtrace
21364          structure was created which includes an adjusted stack
21365          pointer, so just pop everything.  */
21366       if (live_regs_mask)
21367         thumb_pop (asm_out_file, live_regs_mask);
21368
21369       /* We have either just popped the return address into the
21370          PC or it is was kept in LR for the entire function.
21371          Note that thumb_pop has already called thumb_exit if the
21372          PC was in the list.  */
21373       if (!had_to_push_lr)
21374         thumb_exit (asm_out_file, LR_REGNUM);
21375     }
21376   else
21377     {
21378       /* Pop everything but the return address.  */
21379       if (live_regs_mask)
21380         thumb_pop (asm_out_file, live_regs_mask);
21381
21382       if (had_to_push_lr)
21383         {
21384           if (size > 12)
21385             {
21386               /* We have no free low regs, so save one.  */
21387               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
21388                            LAST_ARG_REGNUM);
21389             }
21390
21391           /* Get the return address into a temporary register.  */
21392           thumb_pop (asm_out_file, 1 << LAST_ARG_REGNUM);
21393
21394           if (size > 12)
21395             {
21396               /* Move the return address to lr.  */
21397               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
21398                            LAST_ARG_REGNUM);
21399               /* Restore the low register.  */
21400               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
21401                            IP_REGNUM);
21402               regno = LR_REGNUM;
21403             }
21404           else
21405             regno = LAST_ARG_REGNUM;
21406         }
21407       else
21408         regno = LR_REGNUM;
21409
21410       /* Remove the argument registers that were pushed onto the stack.  */
21411       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
21412                    SP_REGNUM, SP_REGNUM,
21413                    crtl->args.pretend_args_size);
21414
21415       thumb_exit (asm_out_file, regno);
21416     }
21417
21418   return "";
21419 }
21420
21421 /* Functions to save and restore machine-specific function data.  */
21422 static struct machine_function *
21423 arm_init_machine_status (void)
21424 {
21425   struct machine_function *machine;
21426   machine = ggc_alloc_cleared_machine_function ();
21427
21428 #if ARM_FT_UNKNOWN != 0
21429   machine->func_type = ARM_FT_UNKNOWN;
21430 #endif
21431   return machine;
21432 }
21433
21434 /* Return an RTX indicating where the return address to the
21435    calling function can be found.  */
21436 rtx
21437 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
21438 {
21439   if (count != 0)
21440     return NULL_RTX;
21441
21442   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
21443 }
21444
21445 /* Do anything needed before RTL is emitted for each function.  */
21446 void
21447 arm_init_expanders (void)
21448 {
21449   /* Arrange to initialize and mark the machine per-function status.  */
21450   init_machine_status = arm_init_machine_status;
21451
21452   /* This is to stop the combine pass optimizing away the alignment
21453      adjustment of va_arg.  */
21454   /* ??? It is claimed that this should not be necessary.  */
21455   if (cfun)
21456     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
21457 }
21458
21459
21460 /* Like arm_compute_initial_elimination offset.  Simpler because there
21461    isn't an ABI specified frame pointer for Thumb.  Instead, we set it
21462    to point at the base of the local variables after static stack
21463    space for a function has been allocated.  */
21464
21465 HOST_WIDE_INT
21466 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
21467 {
21468   arm_stack_offsets *offsets;
21469
21470   offsets = arm_get_frame_offsets ();
21471
21472   switch (from)
21473     {
21474     case ARG_POINTER_REGNUM:
21475       switch (to)
21476         {
21477         case STACK_POINTER_REGNUM:
21478           return offsets->outgoing_args - offsets->saved_args;
21479
21480         case FRAME_POINTER_REGNUM:
21481           return offsets->soft_frame - offsets->saved_args;
21482
21483         case ARM_HARD_FRAME_POINTER_REGNUM:
21484           return offsets->saved_regs - offsets->saved_args;
21485
21486         case THUMB_HARD_FRAME_POINTER_REGNUM:
21487           return offsets->locals_base - offsets->saved_args;
21488
21489         default:
21490           gcc_unreachable ();
21491         }
21492       break;
21493
21494     case FRAME_POINTER_REGNUM:
21495       switch (to)
21496         {
21497         case STACK_POINTER_REGNUM:
21498           return offsets->outgoing_args - offsets->soft_frame;
21499
21500         case ARM_HARD_FRAME_POINTER_REGNUM:
21501           return offsets->saved_regs - offsets->soft_frame;
21502
21503         case THUMB_HARD_FRAME_POINTER_REGNUM:
21504           return offsets->locals_base - offsets->soft_frame;
21505
21506         default:
21507           gcc_unreachable ();
21508         }
21509       break;
21510
21511     default:
21512       gcc_unreachable ();
21513     }
21514 }
21515
21516 /* Generate the function's prologue.  */
21517
21518 void
21519 thumb1_expand_prologue (void)
21520 {
21521   rtx insn;
21522
21523   HOST_WIDE_INT amount;
21524   arm_stack_offsets *offsets;
21525   unsigned long func_type;
21526   int regno;
21527   unsigned long live_regs_mask;
21528   unsigned long l_mask;
21529   unsigned high_regs_pushed = 0;
21530
21531   func_type = arm_current_func_type ();
21532
21533   /* Naked functions don't have prologues.  */
21534   if (IS_NAKED (func_type))
21535     return;
21536
21537   if (IS_INTERRUPT (func_type))
21538     {
21539       error ("interrupt Service Routines cannot be coded in Thumb mode");
21540       return;
21541     }
21542
21543   if (is_called_in_ARM_mode (current_function_decl))
21544     emit_insn (gen_prologue_thumb1_interwork ());
21545
21546   offsets = arm_get_frame_offsets ();
21547   live_regs_mask = offsets->saved_regs_mask;
21548
21549   /* Extract a mask of the ones we can give to the Thumb's push instruction.  */
21550   l_mask = live_regs_mask & 0x40ff;
21551   /* Then count how many other high registers will need to be pushed.  */
21552   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
21553
21554   if (crtl->args.pretend_args_size)
21555     {
21556       rtx x = GEN_INT (-crtl->args.pretend_args_size);
21557
21558       if (cfun->machine->uses_anonymous_args)
21559         {
21560           int num_pushes = ARM_NUM_INTS (crtl->args.pretend_args_size);
21561           unsigned long mask;
21562
21563           mask = 1ul << (LAST_ARG_REGNUM + 1);
21564           mask -= 1ul << (LAST_ARG_REGNUM + 1 - num_pushes);
21565
21566           insn = thumb1_emit_multi_reg_push (mask, 0);
21567         }
21568       else
21569         {
21570           insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
21571                                         stack_pointer_rtx, x));
21572         }
21573       RTX_FRAME_RELATED_P (insn) = 1;
21574     }
21575
21576   if (TARGET_BACKTRACE)
21577     {
21578       HOST_WIDE_INT offset = 0;
21579       unsigned work_register;
21580       rtx work_reg, x, arm_hfp_rtx;
21581
21582       /* We have been asked to create a stack backtrace structure.
21583          The code looks like this:
21584
21585          0   .align 2
21586          0   func:
21587          0     sub   SP, #16         Reserve space for 4 registers.
21588          2     push  {R7}            Push low registers.
21589          4     add   R7, SP, #20     Get the stack pointer before the push.
21590          6     str   R7, [SP, #8]    Store the stack pointer
21591                                         (before reserving the space).
21592          8     mov   R7, PC          Get hold of the start of this code + 12.
21593         10     str   R7, [SP, #16]   Store it.
21594         12     mov   R7, FP          Get hold of the current frame pointer.
21595         14     str   R7, [SP, #4]    Store it.
21596         16     mov   R7, LR          Get hold of the current return address.
21597         18     str   R7, [SP, #12]   Store it.
21598         20     add   R7, SP, #16     Point at the start of the
21599                                         backtrace structure.
21600         22     mov   FP, R7          Put this value into the frame pointer.  */
21601
21602       work_register = thumb_find_work_register (live_regs_mask);
21603       work_reg = gen_rtx_REG (SImode, work_register);
21604       arm_hfp_rtx = gen_rtx_REG (SImode, ARM_HARD_FRAME_POINTER_REGNUM);
21605
21606       insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
21607                                     stack_pointer_rtx, GEN_INT (-16)));
21608       RTX_FRAME_RELATED_P (insn) = 1;
21609
21610       if (l_mask)
21611         {
21612           insn = thumb1_emit_multi_reg_push (l_mask, l_mask);
21613           RTX_FRAME_RELATED_P (insn) = 1;
21614
21615           offset = bit_count (l_mask) * UNITS_PER_WORD;
21616         }
21617
21618       x = GEN_INT (offset + 16 + crtl->args.pretend_args_size);
21619       emit_insn (gen_addsi3 (work_reg, stack_pointer_rtx, x));
21620
21621       x = plus_constant (stack_pointer_rtx, offset + 4);
21622       x = gen_frame_mem (SImode, x);
21623       emit_move_insn (x, work_reg);
21624
21625       /* Make sure that the instruction fetching the PC is in the right place
21626          to calculate "start of backtrace creation code + 12".  */
21627       /* ??? The stores using the common WORK_REG ought to be enough to
21628          prevent the scheduler from doing anything weird.  Failing that
21629          we could always move all of the following into an UNSPEC_VOLATILE.  */
21630       if (l_mask)
21631         {
21632           x = gen_rtx_REG (SImode, PC_REGNUM);
21633           emit_move_insn (work_reg, x);
21634
21635           x = plus_constant (stack_pointer_rtx, offset + 12);
21636           x = gen_frame_mem (SImode, x);
21637           emit_move_insn (x, work_reg);
21638
21639           emit_move_insn (work_reg, arm_hfp_rtx);
21640
21641           x = plus_constant (stack_pointer_rtx, offset);
21642           x = gen_frame_mem (SImode, x);
21643           emit_move_insn (x, work_reg);
21644         }
21645       else
21646         {
21647           emit_move_insn (work_reg, arm_hfp_rtx);
21648
21649           x = plus_constant (stack_pointer_rtx, offset);
21650           x = gen_frame_mem (SImode, x);
21651           emit_move_insn (x, work_reg);
21652
21653           x = gen_rtx_REG (SImode, PC_REGNUM);
21654           emit_move_insn (work_reg, x);
21655
21656           x = plus_constant (stack_pointer_rtx, offset + 12);
21657           x = gen_frame_mem (SImode, x);
21658           emit_move_insn (x, work_reg);
21659         }
21660
21661       x = gen_rtx_REG (SImode, LR_REGNUM);
21662       emit_move_insn (work_reg, x);
21663
21664       x = plus_constant (stack_pointer_rtx, offset + 8);
21665       x = gen_frame_mem (SImode, x);
21666       emit_move_insn (x, work_reg);
21667
21668       x = GEN_INT (offset + 12);
21669       emit_insn (gen_addsi3 (work_reg, stack_pointer_rtx, x));
21670
21671       emit_move_insn (arm_hfp_rtx, work_reg);
21672     }
21673   /* Optimization:  If we are not pushing any low registers but we are going
21674      to push some high registers then delay our first push.  This will just
21675      be a push of LR and we can combine it with the push of the first high
21676      register.  */
21677   else if ((l_mask & 0xff) != 0
21678            || (high_regs_pushed == 0 && l_mask))
21679     {
21680       unsigned long mask = l_mask;
21681       mask |= (1 << thumb1_extra_regs_pushed (offsets, true)) - 1;
21682       insn = thumb1_emit_multi_reg_push (mask, mask);
21683       RTX_FRAME_RELATED_P (insn) = 1;
21684     }
21685
21686   if (high_regs_pushed)
21687     {
21688       unsigned pushable_regs;
21689       unsigned next_hi_reg;
21690
21691       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
21692         if (live_regs_mask & (1 << next_hi_reg))
21693           break;
21694
21695       pushable_regs = l_mask & 0xff;
21696
21697       if (pushable_regs == 0)
21698         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
21699
21700       while (high_regs_pushed > 0)
21701         {
21702           unsigned long real_regs_mask = 0;
21703
21704           for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
21705             {
21706               if (pushable_regs & (1 << regno))
21707                 {
21708                   emit_move_insn (gen_rtx_REG (SImode, regno),
21709                                   gen_rtx_REG (SImode, next_hi_reg));
21710
21711                   high_regs_pushed --;
21712                   real_regs_mask |= (1 << next_hi_reg);
21713
21714                   if (high_regs_pushed)
21715                     {
21716                       for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
21717                            next_hi_reg --)
21718                         if (live_regs_mask & (1 << next_hi_reg))
21719                           break;
21720                     }
21721                   else
21722                     {
21723                       pushable_regs &= ~((1 << regno) - 1);
21724                       break;
21725                     }
21726                 }
21727             }
21728
21729           /* If we had to find a work register and we have not yet
21730              saved the LR then add it to the list of regs to push.  */
21731           if (l_mask == (1 << LR_REGNUM))
21732             {
21733               pushable_regs |= l_mask;
21734               real_regs_mask |= l_mask;
21735               l_mask = 0;
21736             }
21737
21738           insn = thumb1_emit_multi_reg_push (pushable_regs, real_regs_mask);
21739           RTX_FRAME_RELATED_P (insn) = 1;
21740         }
21741     }
21742
21743   /* Load the pic register before setting the frame pointer,
21744      so we can use r7 as a temporary work register.  */
21745   if (flag_pic && arm_pic_register != INVALID_REGNUM)
21746     arm_load_pic_register (live_regs_mask);
21747
21748   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
21749     emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
21750                     stack_pointer_rtx);
21751
21752   if (flag_stack_usage_info)
21753     current_function_static_stack_size
21754       = offsets->outgoing_args - offsets->saved_args;
21755
21756   amount = offsets->outgoing_args - offsets->saved_regs;
21757   amount -= 4 * thumb1_extra_regs_pushed (offsets, true);
21758   if (amount)
21759     {
21760       if (amount < 512)
21761         {
21762           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
21763                                         GEN_INT (- amount)));
21764           RTX_FRAME_RELATED_P (insn) = 1;
21765         }
21766       else
21767         {
21768           rtx reg, dwarf;
21769
21770           /* The stack decrement is too big for an immediate value in a single
21771              insn.  In theory we could issue multiple subtracts, but after
21772              three of them it becomes more space efficient to place the full
21773              value in the constant pool and load into a register.  (Also the
21774              ARM debugger really likes to see only one stack decrement per
21775              function).  So instead we look for a scratch register into which
21776              we can load the decrement, and then we subtract this from the
21777              stack pointer.  Unfortunately on the thumb the only available
21778              scratch registers are the argument registers, and we cannot use
21779              these as they may hold arguments to the function.  Instead we
21780              attempt to locate a call preserved register which is used by this
21781              function.  If we can find one, then we know that it will have
21782              been pushed at the start of the prologue and so we can corrupt
21783              it now.  */
21784           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
21785             if (live_regs_mask & (1 << regno))
21786               break;
21787
21788           gcc_assert(regno <= LAST_LO_REGNUM);
21789
21790           reg = gen_rtx_REG (SImode, regno);
21791
21792           emit_insn (gen_movsi (reg, GEN_INT (- amount)));
21793
21794           insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
21795                                         stack_pointer_rtx, reg));
21796
21797           dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
21798                                plus_constant (stack_pointer_rtx,
21799                                               -amount));
21800           add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
21801           RTX_FRAME_RELATED_P (insn) = 1;
21802         }
21803     }
21804
21805   if (frame_pointer_needed)
21806     thumb_set_frame_pointer (offsets);
21807
21808   /* If we are profiling, make sure no instructions are scheduled before
21809      the call to mcount.  Similarly if the user has requested no
21810      scheduling in the prolog.  Similarly if we want non-call exceptions
21811      using the EABI unwinder, to prevent faulting instructions from being
21812      swapped with a stack adjustment.  */
21813   if (crtl->profile || !TARGET_SCHED_PROLOG
21814       || (arm_except_unwind_info (&global_options) == UI_TARGET
21815           && cfun->can_throw_non_call_exceptions))
21816     emit_insn (gen_blockage ());
21817
21818   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
21819   if (live_regs_mask & 0xff)
21820     cfun->machine->lr_save_eliminated = 0;
21821 }
21822
21823
21824 void
21825 thumb1_expand_epilogue (void)
21826 {
21827   HOST_WIDE_INT amount;
21828   arm_stack_offsets *offsets;
21829   int regno;
21830
21831   /* Naked functions don't have prologues.  */
21832   if (IS_NAKED (arm_current_func_type ()))
21833     return;
21834
21835   offsets = arm_get_frame_offsets ();
21836   amount = offsets->outgoing_args - offsets->saved_regs;
21837
21838   if (frame_pointer_needed)
21839     {
21840       emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
21841       amount = offsets->locals_base - offsets->saved_regs;
21842     }
21843   amount -= 4 * thumb1_extra_regs_pushed (offsets, false);
21844
21845   gcc_assert (amount >= 0);
21846   if (amount)
21847     {
21848       if (amount < 512)
21849         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
21850                                GEN_INT (amount)));
21851       else
21852         {
21853           /* r3 is always free in the epilogue.  */
21854           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
21855
21856           emit_insn (gen_movsi (reg, GEN_INT (amount)));
21857           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
21858         }
21859     }
21860
21861   /* Emit a USE (stack_pointer_rtx), so that
21862      the stack adjustment will not be deleted.  */
21863   emit_insn (gen_prologue_use (stack_pointer_rtx));
21864
21865   if (crtl->profile || !TARGET_SCHED_PROLOG)
21866     emit_insn (gen_blockage ());
21867
21868   /* Emit a clobber for each insn that will be restored in the epilogue,
21869      so that flow2 will get register lifetimes correct.  */
21870   for (regno = 0; regno < 13; regno++)
21871     if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
21872       emit_clobber (gen_rtx_REG (SImode, regno));
21873
21874   if (! df_regs_ever_live_p (LR_REGNUM))
21875     emit_use (gen_rtx_REG (SImode, LR_REGNUM));
21876 }
21877
21878 /* Implementation of insn prologue_thumb1_interwork.  This is the first
21879    "instruction" of a function called in ARM mode.  Swap to thumb mode.  */
21880
21881 const char *
21882 thumb1_output_interwork (void)
21883 {
21884   const char * name;
21885   FILE *f = asm_out_file;
21886
21887   gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
21888   gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
21889               == SYMBOL_REF);
21890   name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
21891
21892   /* Generate code sequence to switch us into Thumb mode.  */
21893   /* The .code 32 directive has already been emitted by
21894      ASM_DECLARE_FUNCTION_NAME.  */
21895   asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
21896   asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
21897
21898   /* Generate a label, so that the debugger will notice the
21899      change in instruction sets.  This label is also used by
21900      the assembler to bypass the ARM code when this function
21901      is called from a Thumb encoded function elsewhere in the
21902      same file.  Hence the definition of STUB_NAME here must
21903      agree with the definition in gas/config/tc-arm.c.  */
21904
21905 #define STUB_NAME ".real_start_of"
21906
21907   fprintf (f, "\t.code\t16\n");
21908 #ifdef ARM_PE
21909   if (arm_dllexport_name_p (name))
21910     name = arm_strip_name_encoding (name);
21911 #endif
21912   asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
21913   fprintf (f, "\t.thumb_func\n");
21914   asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
21915
21916   return "";
21917 }
21918
21919 /* Handle the case of a double word load into a low register from
21920    a computed memory address.  The computed address may involve a
21921    register which is overwritten by the load.  */
21922 const char *
21923 thumb_load_double_from_address (rtx *operands)
21924 {
21925   rtx addr;
21926   rtx base;
21927   rtx offset;
21928   rtx arg1;
21929   rtx arg2;
21930
21931   gcc_assert (GET_CODE (operands[0]) == REG);
21932   gcc_assert (GET_CODE (operands[1]) == MEM);
21933
21934   /* Get the memory address.  */
21935   addr = XEXP (operands[1], 0);
21936
21937   /* Work out how the memory address is computed.  */
21938   switch (GET_CODE (addr))
21939     {
21940     case REG:
21941       operands[2] = adjust_address (operands[1], SImode, 4);
21942
21943       if (REGNO (operands[0]) == REGNO (addr))
21944         {
21945           output_asm_insn ("ldr\t%H0, %2", operands);
21946           output_asm_insn ("ldr\t%0, %1", operands);
21947         }
21948       else
21949         {
21950           output_asm_insn ("ldr\t%0, %1", operands);
21951           output_asm_insn ("ldr\t%H0, %2", operands);
21952         }
21953       break;
21954
21955     case CONST:
21956       /* Compute <address> + 4 for the high order load.  */
21957       operands[2] = adjust_address (operands[1], SImode, 4);
21958
21959       output_asm_insn ("ldr\t%0, %1", operands);
21960       output_asm_insn ("ldr\t%H0, %2", operands);
21961       break;
21962
21963     case PLUS:
21964       arg1   = XEXP (addr, 0);
21965       arg2   = XEXP (addr, 1);
21966
21967       if (CONSTANT_P (arg1))
21968         base = arg2, offset = arg1;
21969       else
21970         base = arg1, offset = arg2;
21971
21972       gcc_assert (GET_CODE (base) == REG);
21973
21974       /* Catch the case of <address> = <reg> + <reg> */
21975       if (GET_CODE (offset) == REG)
21976         {
21977           int reg_offset = REGNO (offset);
21978           int reg_base   = REGNO (base);
21979           int reg_dest   = REGNO (operands[0]);
21980
21981           /* Add the base and offset registers together into the
21982              higher destination register.  */
21983           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
21984                        reg_dest + 1, reg_base, reg_offset);
21985
21986           /* Load the lower destination register from the address in
21987              the higher destination register.  */
21988           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
21989                        reg_dest, reg_dest + 1);
21990
21991           /* Load the higher destination register from its own address
21992              plus 4.  */
21993           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
21994                        reg_dest + 1, reg_dest + 1);
21995         }
21996       else
21997         {
21998           /* Compute <address> + 4 for the high order load.  */
21999           operands[2] = adjust_address (operands[1], SImode, 4);
22000
22001           /* If the computed address is held in the low order register
22002              then load the high order register first, otherwise always
22003              load the low order register first.  */
22004           if (REGNO (operands[0]) == REGNO (base))
22005             {
22006               output_asm_insn ("ldr\t%H0, %2", operands);
22007               output_asm_insn ("ldr\t%0, %1", operands);
22008             }
22009           else
22010             {
22011               output_asm_insn ("ldr\t%0, %1", operands);
22012               output_asm_insn ("ldr\t%H0, %2", operands);
22013             }
22014         }
22015       break;
22016
22017     case LABEL_REF:
22018       /* With no registers to worry about we can just load the value
22019          directly.  */
22020       operands[2] = adjust_address (operands[1], SImode, 4);
22021
22022       output_asm_insn ("ldr\t%H0, %2", operands);
22023       output_asm_insn ("ldr\t%0, %1", operands);
22024       break;
22025
22026     default:
22027       gcc_unreachable ();
22028     }
22029
22030   return "";
22031 }
22032
22033 const char *
22034 thumb_output_move_mem_multiple (int n, rtx *operands)
22035 {
22036   rtx tmp;
22037
22038   switch (n)
22039     {
22040     case 2:
22041       if (REGNO (operands[4]) > REGNO (operands[5]))
22042         {
22043           tmp = operands[4];
22044           operands[4] = operands[5];
22045           operands[5] = tmp;
22046         }
22047       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
22048       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
22049       break;
22050
22051     case 3:
22052       if (REGNO (operands[4]) > REGNO (operands[5]))
22053         {
22054           tmp = operands[4];
22055           operands[4] = operands[5];
22056           operands[5] = tmp;
22057         }
22058       if (REGNO (operands[5]) > REGNO (operands[6]))
22059         {
22060           tmp = operands[5];
22061           operands[5] = operands[6];
22062           operands[6] = tmp;
22063         }
22064       if (REGNO (operands[4]) > REGNO (operands[5]))
22065         {
22066           tmp = operands[4];
22067           operands[4] = operands[5];
22068           operands[5] = tmp;
22069         }
22070
22071       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
22072       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
22073       break;
22074
22075     default:
22076       gcc_unreachable ();
22077     }
22078
22079   return "";
22080 }
22081
22082 /* Output a call-via instruction for thumb state.  */
22083 const char *
22084 thumb_call_via_reg (rtx reg)
22085 {
22086   int regno = REGNO (reg);
22087   rtx *labelp;
22088
22089   gcc_assert (regno < LR_REGNUM);
22090
22091   /* If we are in the normal text section we can use a single instance
22092      per compilation unit.  If we are doing function sections, then we need
22093      an entry per section, since we can't rely on reachability.  */
22094   if (in_section == text_section)
22095     {
22096       thumb_call_reg_needed = 1;
22097
22098       if (thumb_call_via_label[regno] == NULL)
22099         thumb_call_via_label[regno] = gen_label_rtx ();
22100       labelp = thumb_call_via_label + regno;
22101     }
22102   else
22103     {
22104       if (cfun->machine->call_via[regno] == NULL)
22105         cfun->machine->call_via[regno] = gen_label_rtx ();
22106       labelp = cfun->machine->call_via + regno;
22107     }
22108
22109   output_asm_insn ("bl\t%a0", labelp);
22110   return "";
22111 }
22112
22113 /* Routines for generating rtl.  */
22114 void
22115 thumb_expand_movmemqi (rtx *operands)
22116 {
22117   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
22118   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
22119   HOST_WIDE_INT len = INTVAL (operands[2]);
22120   HOST_WIDE_INT offset = 0;
22121
22122   while (len >= 12)
22123     {
22124       emit_insn (gen_movmem12b (out, in, out, in));
22125       len -= 12;
22126     }
22127
22128   if (len >= 8)
22129     {
22130       emit_insn (gen_movmem8b (out, in, out, in));
22131       len -= 8;
22132     }
22133
22134   if (len >= 4)
22135     {
22136       rtx reg = gen_reg_rtx (SImode);
22137       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
22138       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
22139       len -= 4;
22140       offset += 4;
22141     }
22142
22143   if (len >= 2)
22144     {
22145       rtx reg = gen_reg_rtx (HImode);
22146       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
22147                                               plus_constant (in, offset))));
22148       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
22149                             reg));
22150       len -= 2;
22151       offset += 2;
22152     }
22153
22154   if (len)
22155     {
22156       rtx reg = gen_reg_rtx (QImode);
22157       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
22158                                               plus_constant (in, offset))));
22159       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
22160                             reg));
22161     }
22162 }
22163
22164 void
22165 thumb_reload_out_hi (rtx *operands)
22166 {
22167   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
22168 }
22169
22170 /* Handle reading a half-word from memory during reload.  */
22171 void
22172 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
22173 {
22174   gcc_unreachable ();
22175 }
22176
22177 /* Return the length of a function name prefix
22178     that starts with the character 'c'.  */
22179 static int
22180 arm_get_strip_length (int c)
22181 {
22182   switch (c)
22183     {
22184     ARM_NAME_ENCODING_LENGTHS
22185       default: return 0;
22186     }
22187 }
22188
22189 /* Return a pointer to a function's name with any
22190    and all prefix encodings stripped from it.  */
22191 const char *
22192 arm_strip_name_encoding (const char *name)
22193 {
22194   int skip;
22195
22196   while ((skip = arm_get_strip_length (* name)))
22197     name += skip;
22198
22199   return name;
22200 }
22201
22202 /* If there is a '*' anywhere in the name's prefix, then
22203    emit the stripped name verbatim, otherwise prepend an
22204    underscore if leading underscores are being used.  */
22205 void
22206 arm_asm_output_labelref (FILE *stream, const char *name)
22207 {
22208   int skip;
22209   int verbatim = 0;
22210
22211   while ((skip = arm_get_strip_length (* name)))
22212     {
22213       verbatim |= (*name == '*');
22214       name += skip;
22215     }
22216
22217   if (verbatim)
22218     fputs (name, stream);
22219   else
22220     asm_fprintf (stream, "%U%s", name);
22221 }
22222
22223 static void
22224 arm_file_start (void)
22225 {
22226   int val;
22227
22228   if (TARGET_UNIFIED_ASM)
22229     asm_fprintf (asm_out_file, "\t.syntax unified\n");
22230
22231   if (TARGET_BPABI)
22232     {
22233       const char *fpu_name;
22234       if (arm_selected_arch)
22235         asm_fprintf (asm_out_file, "\t.arch %s\n", arm_selected_arch->name);
22236       else if (strncmp (arm_selected_cpu->name, "generic", 7) == 0)
22237         asm_fprintf (asm_out_file, "\t.arch %s\n", arm_selected_cpu->name + 8);
22238       else
22239         asm_fprintf (asm_out_file, "\t.cpu %s\n", arm_selected_cpu->name);
22240
22241       if (TARGET_SOFT_FLOAT)
22242         {
22243           if (TARGET_VFP)
22244             fpu_name = "softvfp";
22245           else
22246             fpu_name = "softfpa";
22247         }
22248       else
22249         {
22250           fpu_name = arm_fpu_desc->name;
22251           if (arm_fpu_desc->model == ARM_FP_MODEL_VFP)
22252             {
22253               if (TARGET_HARD_FLOAT)
22254                 asm_fprintf (asm_out_file, "\t.eabi_attribute 27, 3\n");
22255               if (TARGET_HARD_FLOAT_ABI)
22256                 asm_fprintf (asm_out_file, "\t.eabi_attribute 28, 1\n");
22257             }
22258         }
22259       asm_fprintf (asm_out_file, "\t.fpu %s\n", fpu_name);
22260
22261       /* Some of these attributes only apply when the corresponding features
22262          are used.  However we don't have any easy way of figuring this out.
22263          Conservatively record the setting that would have been used.  */
22264
22265       /* Tag_ABI_FP_rounding.  */
22266       if (flag_rounding_math)
22267         asm_fprintf (asm_out_file, "\t.eabi_attribute 19, 1\n");
22268       if (!flag_unsafe_math_optimizations)
22269         {
22270           /* Tag_ABI_FP_denomal.  */
22271           asm_fprintf (asm_out_file, "\t.eabi_attribute 20, 1\n");
22272           /* Tag_ABI_FP_exceptions.  */
22273           asm_fprintf (asm_out_file, "\t.eabi_attribute 21, 1\n");
22274         }
22275       /* Tag_ABI_FP_user_exceptions.  */
22276       if (flag_signaling_nans)
22277         asm_fprintf (asm_out_file, "\t.eabi_attribute 22, 1\n");
22278       /* Tag_ABI_FP_number_model.  */
22279       asm_fprintf (asm_out_file, "\t.eabi_attribute 23, %d\n", 
22280                    flag_finite_math_only ? 1 : 3);
22281
22282       /* Tag_ABI_align8_needed.  */
22283       asm_fprintf (asm_out_file, "\t.eabi_attribute 24, 1\n");
22284       /* Tag_ABI_align8_preserved.  */
22285       asm_fprintf (asm_out_file, "\t.eabi_attribute 25, 1\n");
22286       /* Tag_ABI_enum_size.  */
22287       asm_fprintf (asm_out_file, "\t.eabi_attribute 26, %d\n",
22288                    flag_short_enums ? 1 : 2);
22289
22290       /* Tag_ABI_optimization_goals.  */
22291       if (optimize_size)
22292         val = 4;
22293       else if (optimize >= 2)
22294         val = 2;
22295       else if (optimize)
22296         val = 1;
22297       else
22298         val = 6;
22299       asm_fprintf (asm_out_file, "\t.eabi_attribute 30, %d\n", val);
22300
22301       /* Tag_CPU_unaligned_access.  */
22302       asm_fprintf (asm_out_file, "\t.eabi_attribute 34, %d\n",
22303                    unaligned_access);
22304
22305       /* Tag_ABI_FP_16bit_format.  */
22306       if (arm_fp16_format)
22307         asm_fprintf (asm_out_file, "\t.eabi_attribute 38, %d\n",
22308                      (int)arm_fp16_format);
22309
22310       if (arm_lang_output_object_attributes_hook)
22311         arm_lang_output_object_attributes_hook();
22312     }
22313   default_file_start();
22314 }
22315
22316 static void
22317 arm_file_end (void)
22318 {
22319   int regno;
22320
22321   if (NEED_INDICATE_EXEC_STACK)
22322     /* Add .note.GNU-stack.  */
22323     file_end_indicate_exec_stack ();
22324
22325   if (! thumb_call_reg_needed)
22326     return;
22327
22328   switch_to_section (text_section);
22329   asm_fprintf (asm_out_file, "\t.code 16\n");
22330   ASM_OUTPUT_ALIGN (asm_out_file, 1);
22331
22332   for (regno = 0; regno < LR_REGNUM; regno++)
22333     {
22334       rtx label = thumb_call_via_label[regno];
22335
22336       if (label != 0)
22337         {
22338           targetm.asm_out.internal_label (asm_out_file, "L",
22339                                           CODE_LABEL_NUMBER (label));
22340           asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
22341         }
22342     }
22343 }
22344
22345 #ifndef ARM_PE
22346 /* Symbols in the text segment can be accessed without indirecting via the
22347    constant pool; it may take an extra binary operation, but this is still
22348    faster than indirecting via memory.  Don't do this when not optimizing,
22349    since we won't be calculating al of the offsets necessary to do this
22350    simplification.  */
22351
22352 static void
22353 arm_encode_section_info (tree decl, rtx rtl, int first)
22354 {
22355   if (optimize > 0 && TREE_CONSTANT (decl))
22356     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
22357
22358   default_encode_section_info (decl, rtl, first);
22359 }
22360 #endif /* !ARM_PE */
22361
22362 static void
22363 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
22364 {
22365   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
22366       && !strcmp (prefix, "L"))
22367     {
22368       arm_ccfsm_state = 0;
22369       arm_target_insn = NULL;
22370     }
22371   default_internal_label (stream, prefix, labelno);
22372 }
22373
22374 /* Output code to add DELTA to the first argument, and then jump
22375    to FUNCTION.  Used for C++ multiple inheritance.  */
22376 static void
22377 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
22378                      HOST_WIDE_INT delta,
22379                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
22380                      tree function)
22381 {
22382   static int thunk_label = 0;
22383   char label[256];
22384   char labelpc[256];
22385   int mi_delta = delta;
22386   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
22387   int shift = 0;
22388   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
22389                     ? 1 : 0);
22390   if (mi_delta < 0)
22391     mi_delta = - mi_delta;
22392
22393   if (TARGET_THUMB1)
22394     {
22395       int labelno = thunk_label++;
22396       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
22397       /* Thunks are entered in arm mode when avaiable.  */
22398       if (TARGET_THUMB1_ONLY)
22399         {
22400           /* push r3 so we can use it as a temporary.  */
22401           /* TODO: Omit this save if r3 is not used.  */
22402           fputs ("\tpush {r3}\n", file);
22403           fputs ("\tldr\tr3, ", file);
22404         }
22405       else
22406         {
22407           fputs ("\tldr\tr12, ", file);
22408         }
22409       assemble_name (file, label);
22410       fputc ('\n', file);
22411       if (flag_pic)
22412         {
22413           /* If we are generating PIC, the ldr instruction below loads
22414              "(target - 7) - .LTHUNKPCn" into r12.  The pc reads as
22415              the address of the add + 8, so we have:
22416
22417              r12 = (target - 7) - .LTHUNKPCn + (.LTHUNKPCn + 8)
22418                  = target + 1.
22419
22420              Note that we have "+ 1" because some versions of GNU ld
22421              don't set the low bit of the result for R_ARM_REL32
22422              relocations against thumb function symbols.
22423              On ARMv6M this is +4, not +8.  */
22424           ASM_GENERATE_INTERNAL_LABEL (labelpc, "LTHUNKPC", labelno);
22425           assemble_name (file, labelpc);
22426           fputs (":\n", file);
22427           if (TARGET_THUMB1_ONLY)
22428             {
22429               /* This is 2 insns after the start of the thunk, so we know it
22430                  is 4-byte aligned.  */
22431               fputs ("\tadd\tr3, pc, r3\n", file);
22432               fputs ("\tmov r12, r3\n", file);
22433             }
22434           else
22435             fputs ("\tadd\tr12, pc, r12\n", file);
22436         }
22437       else if (TARGET_THUMB1_ONLY)
22438         fputs ("\tmov r12, r3\n", file);
22439     }
22440   if (TARGET_THUMB1_ONLY)
22441     {
22442       if (mi_delta > 255)
22443         {
22444           fputs ("\tldr\tr3, ", file);
22445           assemble_name (file, label);
22446           fputs ("+4\n", file);
22447           asm_fprintf (file, "\t%s\t%r, %r, r3\n",
22448                        mi_op, this_regno, this_regno);
22449         }
22450       else if (mi_delta != 0)
22451         {
22452           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
22453                        mi_op, this_regno, this_regno,
22454                        mi_delta);
22455         }
22456     }
22457   else
22458     {
22459       /* TODO: Use movw/movt for large constants when available.  */
22460       while (mi_delta != 0)
22461         {
22462           if ((mi_delta & (3 << shift)) == 0)
22463             shift += 2;
22464           else
22465             {
22466               asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
22467                            mi_op, this_regno, this_regno,
22468                            mi_delta & (0xff << shift));
22469               mi_delta &= ~(0xff << shift);
22470               shift += 8;
22471             }
22472         }
22473     }
22474   if (TARGET_THUMB1)
22475     {
22476       if (TARGET_THUMB1_ONLY)
22477         fputs ("\tpop\t{r3}\n", file);
22478
22479       fprintf (file, "\tbx\tr12\n");
22480       ASM_OUTPUT_ALIGN (file, 2);
22481       assemble_name (file, label);
22482       fputs (":\n", file);
22483       if (flag_pic)
22484         {
22485           /* Output ".word .LTHUNKn-7-.LTHUNKPCn".  */
22486           rtx tem = XEXP (DECL_RTL (function), 0);
22487           tem = gen_rtx_PLUS (GET_MODE (tem), tem, GEN_INT (-7));
22488           tem = gen_rtx_MINUS (GET_MODE (tem),
22489                                tem,
22490                                gen_rtx_SYMBOL_REF (Pmode,
22491                                                    ggc_strdup (labelpc)));
22492           assemble_integer (tem, 4, BITS_PER_WORD, 1);
22493         }
22494       else
22495         /* Output ".word .LTHUNKn".  */
22496         assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
22497
22498       if (TARGET_THUMB1_ONLY && mi_delta > 255)
22499         assemble_integer (GEN_INT(mi_delta), 4, BITS_PER_WORD, 1);
22500     }
22501   else
22502     {
22503       fputs ("\tb\t", file);
22504       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
22505       if (NEED_PLT_RELOC)
22506         fputs ("(PLT)", file);
22507       fputc ('\n', file);
22508     }
22509 }
22510
22511 int
22512 arm_emit_vector_const (FILE *file, rtx x)
22513 {
22514   int i;
22515   const char * pattern;
22516
22517   gcc_assert (GET_CODE (x) == CONST_VECTOR);
22518
22519   switch (GET_MODE (x))
22520     {
22521     case V2SImode: pattern = "%08x"; break;
22522     case V4HImode: pattern = "%04x"; break;
22523     case V8QImode: pattern = "%02x"; break;
22524     default:       gcc_unreachable ();
22525     }
22526
22527   fprintf (file, "0x");
22528   for (i = CONST_VECTOR_NUNITS (x); i--;)
22529     {
22530       rtx element;
22531
22532       element = CONST_VECTOR_ELT (x, i);
22533       fprintf (file, pattern, INTVAL (element));
22534     }
22535
22536   return 1;
22537 }
22538
22539 /* Emit a fp16 constant appropriately padded to occupy a 4-byte word.
22540    HFmode constant pool entries are actually loaded with ldr.  */
22541 void
22542 arm_emit_fp16_const (rtx c)
22543 {
22544   REAL_VALUE_TYPE r;
22545   long bits;
22546
22547   REAL_VALUE_FROM_CONST_DOUBLE (r, c);
22548   bits = real_to_target (NULL, &r, HFmode);
22549   if (WORDS_BIG_ENDIAN)
22550     assemble_zeros (2);
22551   assemble_integer (GEN_INT (bits), 2, BITS_PER_WORD, 1);
22552   if (!WORDS_BIG_ENDIAN)
22553     assemble_zeros (2);
22554 }
22555
22556 const char *
22557 arm_output_load_gr (rtx *operands)
22558 {
22559   rtx reg;
22560   rtx offset;
22561   rtx wcgr;
22562   rtx sum;
22563
22564   if (GET_CODE (operands [1]) != MEM
22565       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
22566       || GET_CODE (reg = XEXP (sum, 0)) != REG
22567       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
22568       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
22569     return "wldrw%?\t%0, %1";
22570
22571   /* Fix up an out-of-range load of a GR register.  */
22572   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
22573   wcgr = operands[0];
22574   operands[0] = reg;
22575   output_asm_insn ("ldr%?\t%0, %1", operands);
22576
22577   operands[0] = wcgr;
22578   operands[1] = reg;
22579   output_asm_insn ("tmcr%?\t%0, %1", operands);
22580   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
22581
22582   return "";
22583 }
22584
22585 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
22586
22587    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
22588    named arg and all anonymous args onto the stack.
22589    XXX I know the prologue shouldn't be pushing registers, but it is faster
22590    that way.  */
22591
22592 static void
22593 arm_setup_incoming_varargs (cumulative_args_t pcum_v,
22594                             enum machine_mode mode,
22595                             tree type,
22596                             int *pretend_size,
22597                             int second_time ATTRIBUTE_UNUSED)
22598 {
22599   CUMULATIVE_ARGS *pcum = get_cumulative_args (pcum_v);
22600   int nregs;
22601   
22602   cfun->machine->uses_anonymous_args = 1;
22603   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
22604     {
22605       nregs = pcum->aapcs_ncrn;
22606       if ((nregs & 1) && arm_needs_doubleword_align (mode, type))
22607         nregs++;
22608     }
22609   else
22610     nregs = pcum->nregs;
22611   
22612   if (nregs < NUM_ARG_REGS)
22613     *pretend_size = (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
22614 }
22615
22616 /* Return nonzero if the CONSUMER instruction (a store) does not need
22617    PRODUCER's value to calculate the address.  */
22618
22619 int
22620 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
22621 {
22622   rtx value = PATTERN (producer);
22623   rtx addr = PATTERN (consumer);
22624
22625   if (GET_CODE (value) == COND_EXEC)
22626     value = COND_EXEC_CODE (value);
22627   if (GET_CODE (value) == PARALLEL)
22628     value = XVECEXP (value, 0, 0);
22629   value = XEXP (value, 0);
22630   if (GET_CODE (addr) == COND_EXEC)
22631     addr = COND_EXEC_CODE (addr);
22632   if (GET_CODE (addr) == PARALLEL)
22633     addr = XVECEXP (addr, 0, 0);
22634   addr = XEXP (addr, 0);
22635
22636   return !reg_overlap_mentioned_p (value, addr);
22637 }
22638
22639 /* Return nonzero if the CONSUMER instruction (a store) does need
22640    PRODUCER's value to calculate the address.  */
22641
22642 int
22643 arm_early_store_addr_dep (rtx producer, rtx consumer)
22644 {
22645   return !arm_no_early_store_addr_dep (producer, consumer);
22646 }
22647
22648 /* Return nonzero if the CONSUMER instruction (a load) does need
22649    PRODUCER's value to calculate the address.  */
22650
22651 int
22652 arm_early_load_addr_dep (rtx producer, rtx consumer)
22653 {
22654   rtx value = PATTERN (producer);
22655   rtx addr = PATTERN (consumer);
22656
22657   if (GET_CODE (value) == COND_EXEC)
22658     value = COND_EXEC_CODE (value);
22659   if (GET_CODE (value) == PARALLEL)
22660     value = XVECEXP (value, 0, 0);
22661   value = XEXP (value, 0);
22662   if (GET_CODE (addr) == COND_EXEC)
22663     addr = COND_EXEC_CODE (addr);
22664   if (GET_CODE (addr) == PARALLEL)
22665     addr = XVECEXP (addr, 0, 0);
22666   addr = XEXP (addr, 1);
22667
22668   return reg_overlap_mentioned_p (value, addr);
22669 }
22670
22671 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
22672    have an early register shift value or amount dependency on the
22673    result of PRODUCER.  */
22674
22675 int
22676 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
22677 {
22678   rtx value = PATTERN (producer);
22679   rtx op = PATTERN (consumer);
22680   rtx early_op;
22681
22682   if (GET_CODE (value) == COND_EXEC)
22683     value = COND_EXEC_CODE (value);
22684   if (GET_CODE (value) == PARALLEL)
22685     value = XVECEXP (value, 0, 0);
22686   value = XEXP (value, 0);
22687   if (GET_CODE (op) == COND_EXEC)
22688     op = COND_EXEC_CODE (op);
22689   if (GET_CODE (op) == PARALLEL)
22690     op = XVECEXP (op, 0, 0);
22691   op = XEXP (op, 1);
22692
22693   early_op = XEXP (op, 0);
22694   /* This is either an actual independent shift, or a shift applied to
22695      the first operand of another operation.  We want the whole shift
22696      operation.  */
22697   if (GET_CODE (early_op) == REG)
22698     early_op = op;
22699
22700   return !reg_overlap_mentioned_p (value, early_op);
22701 }
22702
22703 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
22704    have an early register shift value dependency on the result of
22705    PRODUCER.  */
22706
22707 int
22708 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
22709 {
22710   rtx value = PATTERN (producer);
22711   rtx op = PATTERN (consumer);
22712   rtx early_op;
22713
22714   if (GET_CODE (value) == COND_EXEC)
22715     value = COND_EXEC_CODE (value);
22716   if (GET_CODE (value) == PARALLEL)
22717     value = XVECEXP (value, 0, 0);
22718   value = XEXP (value, 0);
22719   if (GET_CODE (op) == COND_EXEC)
22720     op = COND_EXEC_CODE (op);
22721   if (GET_CODE (op) == PARALLEL)
22722     op = XVECEXP (op, 0, 0);
22723   op = XEXP (op, 1);
22724
22725   early_op = XEXP (op, 0);
22726
22727   /* This is either an actual independent shift, or a shift applied to
22728      the first operand of another operation.  We want the value being
22729      shifted, in either case.  */
22730   if (GET_CODE (early_op) != REG)
22731     early_op = XEXP (early_op, 0);
22732
22733   return !reg_overlap_mentioned_p (value, early_op);
22734 }
22735
22736 /* Return nonzero if the CONSUMER (a mul or mac op) does not
22737    have an early register mult dependency on the result of
22738    PRODUCER.  */
22739
22740 int
22741 arm_no_early_mul_dep (rtx producer, rtx consumer)
22742 {
22743   rtx value = PATTERN (producer);
22744   rtx op = PATTERN (consumer);
22745
22746   if (GET_CODE (value) == COND_EXEC)
22747     value = COND_EXEC_CODE (value);
22748   if (GET_CODE (value) == PARALLEL)
22749     value = XVECEXP (value, 0, 0);
22750   value = XEXP (value, 0);
22751   if (GET_CODE (op) == COND_EXEC)
22752     op = COND_EXEC_CODE (op);
22753   if (GET_CODE (op) == PARALLEL)
22754     op = XVECEXP (op, 0, 0);
22755   op = XEXP (op, 1);
22756
22757   if (GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
22758     {
22759       if (GET_CODE (XEXP (op, 0)) == MULT)
22760         return !reg_overlap_mentioned_p (value, XEXP (op, 0));
22761       else
22762         return !reg_overlap_mentioned_p (value, XEXP (op, 1));
22763     }
22764
22765   return 0;
22766 }
22767
22768 /* We can't rely on the caller doing the proper promotion when
22769    using APCS or ATPCS.  */
22770
22771 static bool
22772 arm_promote_prototypes (const_tree t ATTRIBUTE_UNUSED)
22773 {
22774     return !TARGET_AAPCS_BASED;
22775 }
22776
22777 static enum machine_mode
22778 arm_promote_function_mode (const_tree type ATTRIBUTE_UNUSED,
22779                            enum machine_mode mode,
22780                            int *punsignedp ATTRIBUTE_UNUSED,
22781                            const_tree fntype ATTRIBUTE_UNUSED,
22782                            int for_return ATTRIBUTE_UNUSED)
22783 {
22784   if (GET_MODE_CLASS (mode) == MODE_INT
22785       && GET_MODE_SIZE (mode) < 4)
22786     return SImode;
22787
22788   return mode;
22789 }
22790
22791 /* AAPCS based ABIs use short enums by default.  */
22792
22793 static bool
22794 arm_default_short_enums (void)
22795 {
22796   return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
22797 }
22798
22799
22800 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
22801
22802 static bool
22803 arm_align_anon_bitfield (void)
22804 {
22805   return TARGET_AAPCS_BASED;
22806 }
22807
22808
22809 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
22810
22811 static tree
22812 arm_cxx_guard_type (void)
22813 {
22814   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
22815 }
22816
22817 /* Return non-zero if the consumer (a multiply-accumulate instruction)
22818    has an accumulator dependency on the result of the producer (a
22819    multiplication instruction) and no other dependency on that result.  */
22820 int
22821 arm_mac_accumulator_is_mul_result (rtx producer, rtx consumer)
22822 {
22823   rtx mul = PATTERN (producer);
22824   rtx mac = PATTERN (consumer);
22825   rtx mul_result;
22826   rtx mac_op0, mac_op1, mac_acc;
22827
22828   if (GET_CODE (mul) == COND_EXEC)
22829     mul = COND_EXEC_CODE (mul);
22830   if (GET_CODE (mac) == COND_EXEC)
22831     mac = COND_EXEC_CODE (mac);
22832
22833   /* Check that mul is of the form (set (...) (mult ...))
22834      and mla is of the form (set (...) (plus (mult ...) (...))).  */
22835   if ((GET_CODE (mul) != SET || GET_CODE (XEXP (mul, 1)) != MULT)
22836       || (GET_CODE (mac) != SET || GET_CODE (XEXP (mac, 1)) != PLUS
22837           || GET_CODE (XEXP (XEXP (mac, 1), 0)) != MULT))
22838     return 0;
22839
22840   mul_result = XEXP (mul, 0);
22841   mac_op0 = XEXP (XEXP (XEXP (mac, 1), 0), 0);
22842   mac_op1 = XEXP (XEXP (XEXP (mac, 1), 0), 1);
22843   mac_acc = XEXP (XEXP (mac, 1), 1);
22844
22845   return (reg_overlap_mentioned_p (mul_result, mac_acc)
22846           && !reg_overlap_mentioned_p (mul_result, mac_op0)
22847           && !reg_overlap_mentioned_p (mul_result, mac_op1));
22848 }
22849
22850
22851 /* The EABI says test the least significant bit of a guard variable.  */
22852
22853 static bool
22854 arm_cxx_guard_mask_bit (void)
22855 {
22856   return TARGET_AAPCS_BASED;
22857 }
22858
22859
22860 /* The EABI specifies that all array cookies are 8 bytes long.  */
22861
22862 static tree
22863 arm_get_cookie_size (tree type)
22864 {
22865   tree size;
22866
22867   if (!TARGET_AAPCS_BASED)
22868     return default_cxx_get_cookie_size (type);
22869
22870   size = build_int_cst (sizetype, 8);
22871   return size;
22872 }
22873
22874
22875 /* The EABI says that array cookies should also contain the element size.  */
22876
22877 static bool
22878 arm_cookie_has_size (void)
22879 {
22880   return TARGET_AAPCS_BASED;
22881 }
22882
22883
22884 /* The EABI says constructors and destructors should return a pointer to
22885    the object constructed/destroyed.  */
22886
22887 static bool
22888 arm_cxx_cdtor_returns_this (void)
22889 {
22890   return TARGET_AAPCS_BASED;
22891 }
22892
22893 /* The EABI says that an inline function may never be the key
22894    method.  */
22895
22896 static bool
22897 arm_cxx_key_method_may_be_inline (void)
22898 {
22899   return !TARGET_AAPCS_BASED;
22900 }
22901
22902 static void
22903 arm_cxx_determine_class_data_visibility (tree decl)
22904 {
22905   if (!TARGET_AAPCS_BASED
22906       || !TARGET_DLLIMPORT_DECL_ATTRIBUTES)
22907     return;
22908
22909   /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
22910      is exported.  However, on systems without dynamic vague linkage,
22911      \S 3.2.5.6 says that COMDAT class data has hidden linkage.  */
22912   if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
22913     DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
22914   else
22915     DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
22916   DECL_VISIBILITY_SPECIFIED (decl) = 1;
22917 }
22918
22919 static bool
22920 arm_cxx_class_data_always_comdat (void)
22921 {
22922   /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
22923      vague linkage if the class has no key function.  */
22924   return !TARGET_AAPCS_BASED;
22925 }
22926
22927
22928 /* The EABI says __aeabi_atexit should be used to register static
22929    destructors.  */
22930
22931 static bool
22932 arm_cxx_use_aeabi_atexit (void)
22933 {
22934   return TARGET_AAPCS_BASED;
22935 }
22936
22937
22938 void
22939 arm_set_return_address (rtx source, rtx scratch)
22940 {
22941   arm_stack_offsets *offsets;
22942   HOST_WIDE_INT delta;
22943   rtx addr;
22944   unsigned long saved_regs;
22945
22946   offsets = arm_get_frame_offsets ();
22947   saved_regs = offsets->saved_regs_mask;
22948
22949   if ((saved_regs & (1 << LR_REGNUM)) == 0)
22950     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
22951   else
22952     {
22953       if (frame_pointer_needed)
22954         addr = plus_constant(hard_frame_pointer_rtx, -4);
22955       else
22956         {
22957           /* LR will be the first saved register.  */
22958           delta = offsets->outgoing_args - (offsets->frame + 4);
22959
22960
22961           if (delta >= 4096)
22962             {
22963               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
22964                                      GEN_INT (delta & ~4095)));
22965               addr = scratch;
22966               delta &= 4095;
22967             }
22968           else
22969             addr = stack_pointer_rtx;
22970
22971           addr = plus_constant (addr, delta);
22972         }
22973       emit_move_insn (gen_frame_mem (Pmode, addr), source);
22974     }
22975 }
22976
22977
22978 void
22979 thumb_set_return_address (rtx source, rtx scratch)
22980 {
22981   arm_stack_offsets *offsets;
22982   HOST_WIDE_INT delta;
22983   HOST_WIDE_INT limit;
22984   int reg;
22985   rtx addr;
22986   unsigned long mask;
22987
22988   emit_use (source);
22989
22990   offsets = arm_get_frame_offsets ();
22991   mask = offsets->saved_regs_mask;
22992   if (mask & (1 << LR_REGNUM))
22993     {
22994       limit = 1024;
22995       /* Find the saved regs.  */
22996       if (frame_pointer_needed)
22997         {
22998           delta = offsets->soft_frame - offsets->saved_args;
22999           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
23000           if (TARGET_THUMB1)
23001             limit = 128;
23002         }
23003       else
23004         {
23005           delta = offsets->outgoing_args - offsets->saved_args;
23006           reg = SP_REGNUM;
23007         }
23008       /* Allow for the stack frame.  */
23009       if (TARGET_THUMB1 && TARGET_BACKTRACE)
23010         delta -= 16;
23011       /* The link register is always the first saved register.  */
23012       delta -= 4;
23013
23014       /* Construct the address.  */
23015       addr = gen_rtx_REG (SImode, reg);
23016       if (delta > limit)
23017         {
23018           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
23019           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
23020           addr = scratch;
23021         }
23022       else
23023         addr = plus_constant (addr, delta);
23024
23025       emit_move_insn (gen_frame_mem (Pmode, addr), source);
23026     }
23027   else
23028     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
23029 }
23030
23031 /* Implements target hook vector_mode_supported_p.  */
23032 bool
23033 arm_vector_mode_supported_p (enum machine_mode mode)
23034 {
23035   /* Neon also supports V2SImode, etc. listed in the clause below.  */
23036   if (TARGET_NEON && (mode == V2SFmode || mode == V4SImode || mode == V8HImode
23037       || mode == V16QImode || mode == V4SFmode || mode == V2DImode))
23038     return true;
23039
23040   if ((TARGET_NEON || TARGET_IWMMXT)
23041       && ((mode == V2SImode)
23042           || (mode == V4HImode)
23043           || (mode == V8QImode)))
23044     return true;
23045
23046   if (TARGET_INT_SIMD && (mode == V4UQQmode || mode == V4QQmode
23047       || mode == V2UHQmode || mode == V2HQmode || mode == V2UHAmode
23048       || mode == V2HAmode))
23049     return true;
23050
23051   return false;
23052 }
23053
23054 /* Implements target hook array_mode_supported_p.  */
23055
23056 static bool
23057 arm_array_mode_supported_p (enum machine_mode mode,
23058                             unsigned HOST_WIDE_INT nelems)
23059 {
23060   if (TARGET_NEON
23061       && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode))
23062       && (nelems >= 2 && nelems <= 4))
23063     return true;
23064
23065   return false;
23066 }
23067
23068 /* Use the option -mvectorize-with-neon-double to override the use of quardword
23069    registers when autovectorizing for Neon, at least until multiple vector
23070    widths are supported properly by the middle-end.  */
23071
23072 static enum machine_mode
23073 arm_preferred_simd_mode (enum machine_mode mode)
23074 {
23075   if (TARGET_NEON)
23076     switch (mode)
23077       {
23078       case SFmode:
23079         return TARGET_NEON_VECTORIZE_DOUBLE ? V2SFmode : V4SFmode;
23080       case SImode:
23081         return TARGET_NEON_VECTORIZE_DOUBLE ? V2SImode : V4SImode;
23082       case HImode:
23083         return TARGET_NEON_VECTORIZE_DOUBLE ? V4HImode : V8HImode;
23084       case QImode:
23085         return TARGET_NEON_VECTORIZE_DOUBLE ? V8QImode : V16QImode;
23086       case DImode:
23087         if (!TARGET_NEON_VECTORIZE_DOUBLE)
23088           return V2DImode;
23089         break;
23090
23091       default:;
23092       }
23093
23094   if (TARGET_REALLY_IWMMXT)
23095     switch (mode)
23096       {
23097       case SImode:
23098         return V2SImode;
23099       case HImode:
23100         return V4HImode;
23101       case QImode:
23102         return V8QImode;
23103
23104       default:;
23105       }
23106
23107   return word_mode;
23108 }
23109
23110 /* Implement TARGET_CLASS_LIKELY_SPILLED_P.
23111  
23112    We need to define this for LO_REGS on Thumb-1.  Otherwise we can end up
23113    using r0-r4 for function arguments, r7 for the stack frame and don't have
23114    enough left over to do doubleword arithmetic.  For Thumb-2 all the
23115    potentially problematic instructions accept high registers so this is not
23116    necessary.  Care needs to be taken to avoid adding new Thumb-2 patterns
23117    that require many low registers.  */
23118 static bool
23119 arm_class_likely_spilled_p (reg_class_t rclass)
23120 {
23121   if ((TARGET_THUMB1 && rclass == LO_REGS)
23122       || rclass  == CC_REG)
23123     return true;
23124
23125   return false;
23126 }
23127
23128 /* Implements target hook small_register_classes_for_mode_p.  */
23129 bool
23130 arm_small_register_classes_for_mode_p (enum machine_mode mode ATTRIBUTE_UNUSED)
23131 {
23132   return TARGET_THUMB1;
23133 }
23134
23135 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
23136    ARM insns and therefore guarantee that the shift count is modulo 256.
23137    DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
23138    guarantee no particular behavior for out-of-range counts.  */
23139
23140 static unsigned HOST_WIDE_INT
23141 arm_shift_truncation_mask (enum machine_mode mode)
23142 {
23143   return mode == SImode ? 255 : 0;
23144 }
23145
23146
23147 /* Map internal gcc register numbers to DWARF2 register numbers.  */
23148
23149 unsigned int
23150 arm_dbx_register_number (unsigned int regno)
23151 {
23152   if (regno < 16)
23153     return regno;
23154
23155   /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
23156      compatibility.  The EABI defines them as registers 96-103.  */
23157   if (IS_FPA_REGNUM (regno))
23158     return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
23159
23160   if (IS_VFP_REGNUM (regno))
23161     {
23162       /* See comment in arm_dwarf_register_span.  */
23163       if (VFP_REGNO_OK_FOR_SINGLE (regno))
23164         return 64 + regno - FIRST_VFP_REGNUM;
23165       else
23166         return 256 + (regno - FIRST_VFP_REGNUM) / 2;
23167     }
23168
23169   if (IS_IWMMXT_GR_REGNUM (regno))
23170     return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
23171
23172   if (IS_IWMMXT_REGNUM (regno))
23173     return 112 + regno - FIRST_IWMMXT_REGNUM;
23174
23175   gcc_unreachable ();
23176 }
23177
23178 /* Dwarf models VFPv3 registers as 32 64-bit registers.
23179    GCC models tham as 64 32-bit registers, so we need to describe this to
23180    the DWARF generation code.  Other registers can use the default.  */
23181 static rtx
23182 arm_dwarf_register_span (rtx rtl)
23183 {
23184   unsigned regno;
23185   int nregs;
23186   int i;
23187   rtx p;
23188
23189   regno = REGNO (rtl);
23190   if (!IS_VFP_REGNUM (regno))
23191     return NULL_RTX;
23192
23193   /* XXX FIXME: The EABI defines two VFP register ranges:
23194         64-95: Legacy VFPv2 numbering for S0-S31 (obsolescent)
23195         256-287: D0-D31
23196      The recommended encoding for S0-S31 is a DW_OP_bit_piece of the
23197      corresponding D register.  Until GDB supports this, we shall use the
23198      legacy encodings.  We also use these encodings for D0-D15 for
23199      compatibility with older debuggers.  */
23200   if (VFP_REGNO_OK_FOR_SINGLE (regno))
23201     return NULL_RTX;
23202
23203   nregs = GET_MODE_SIZE (GET_MODE (rtl)) / 8;
23204   p = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nregs));
23205   regno = (regno - FIRST_VFP_REGNUM) / 2;
23206   for (i = 0; i < nregs; i++)
23207     XVECEXP (p, 0, i) = gen_rtx_REG (DImode, 256 + regno + i);
23208
23209   return p;
23210 }
23211
23212 #if ARM_UNWIND_INFO
23213 /* Emit unwind directives for a store-multiple instruction or stack pointer
23214    push during alignment.
23215    These should only ever be generated by the function prologue code, so
23216    expect them to have a particular form.  */
23217
23218 static void
23219 arm_unwind_emit_sequence (FILE * asm_out_file, rtx p)
23220 {
23221   int i;
23222   HOST_WIDE_INT offset;
23223   HOST_WIDE_INT nregs;
23224   int reg_size;
23225   unsigned reg;
23226   unsigned lastreg;
23227   rtx e;
23228
23229   e = XVECEXP (p, 0, 0);
23230   if (GET_CODE (e) != SET)
23231     abort ();
23232
23233   /* First insn will adjust the stack pointer.  */
23234   if (GET_CODE (e) != SET
23235       || GET_CODE (XEXP (e, 0)) != REG
23236       || REGNO (XEXP (e, 0)) != SP_REGNUM
23237       || GET_CODE (XEXP (e, 1)) != PLUS)
23238     abort ();
23239
23240   offset = -INTVAL (XEXP (XEXP (e, 1), 1));
23241   nregs = XVECLEN (p, 0) - 1;
23242
23243   reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
23244   if (reg < 16)
23245     {
23246       /* The function prologue may also push pc, but not annotate it as it is
23247          never restored.  We turn this into a stack pointer adjustment.  */
23248       if (nregs * 4 == offset - 4)
23249         {
23250           fprintf (asm_out_file, "\t.pad #4\n");
23251           offset -= 4;
23252         }
23253       reg_size = 4;
23254       fprintf (asm_out_file, "\t.save {");
23255     }
23256   else if (IS_VFP_REGNUM (reg))
23257     {
23258       reg_size = 8;
23259       fprintf (asm_out_file, "\t.vsave {");
23260     }
23261   else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
23262     {
23263       /* FPA registers are done differently.  */
23264       asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
23265       return;
23266     }
23267   else
23268     /* Unknown register type.  */
23269     abort ();
23270
23271   /* If the stack increment doesn't match the size of the saved registers,
23272      something has gone horribly wrong.  */
23273   if (offset != nregs * reg_size)
23274     abort ();
23275
23276   offset = 0;
23277   lastreg = 0;
23278   /* The remaining insns will describe the stores.  */
23279   for (i = 1; i <= nregs; i++)
23280     {
23281       /* Expect (set (mem <addr>) (reg)).
23282          Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)).  */
23283       e = XVECEXP (p, 0, i);
23284       if (GET_CODE (e) != SET
23285           || GET_CODE (XEXP (e, 0)) != MEM
23286           || GET_CODE (XEXP (e, 1)) != REG)
23287         abort ();
23288
23289       reg = REGNO (XEXP (e, 1));
23290       if (reg < lastreg)
23291         abort ();
23292
23293       if (i != 1)
23294         fprintf (asm_out_file, ", ");
23295       /* We can't use %r for vfp because we need to use the
23296          double precision register names.  */
23297       if (IS_VFP_REGNUM (reg))
23298         asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
23299       else
23300         asm_fprintf (asm_out_file, "%r", reg);
23301
23302 #ifdef ENABLE_CHECKING
23303       /* Check that the addresses are consecutive.  */
23304       e = XEXP (XEXP (e, 0), 0);
23305       if (GET_CODE (e) == PLUS)
23306         {
23307           offset += reg_size;
23308           if (GET_CODE (XEXP (e, 0)) != REG
23309               || REGNO (XEXP (e, 0)) != SP_REGNUM
23310               || GET_CODE (XEXP (e, 1)) != CONST_INT
23311               || offset != INTVAL (XEXP (e, 1)))
23312             abort ();
23313         }
23314       else if (i != 1
23315                || GET_CODE (e) != REG
23316                || REGNO (e) != SP_REGNUM)
23317         abort ();
23318 #endif
23319     }
23320   fprintf (asm_out_file, "}\n");
23321 }
23322
23323 /*  Emit unwind directives for a SET.  */
23324
23325 static void
23326 arm_unwind_emit_set (FILE * asm_out_file, rtx p)
23327 {
23328   rtx e0;
23329   rtx e1;
23330   unsigned reg;
23331
23332   e0 = XEXP (p, 0);
23333   e1 = XEXP (p, 1);
23334   switch (GET_CODE (e0))
23335     {
23336     case MEM:
23337       /* Pushing a single register.  */
23338       if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
23339           || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
23340           || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
23341         abort ();
23342
23343       asm_fprintf (asm_out_file, "\t.save ");
23344       if (IS_VFP_REGNUM (REGNO (e1)))
23345         asm_fprintf(asm_out_file, "{d%d}\n",
23346                     (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
23347       else
23348         asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
23349       break;
23350
23351     case REG:
23352       if (REGNO (e0) == SP_REGNUM)
23353         {
23354           /* A stack increment.  */
23355           if (GET_CODE (e1) != PLUS
23356               || GET_CODE (XEXP (e1, 0)) != REG
23357               || REGNO (XEXP (e1, 0)) != SP_REGNUM
23358               || GET_CODE (XEXP (e1, 1)) != CONST_INT)
23359             abort ();
23360
23361           asm_fprintf (asm_out_file, "\t.pad #%wd\n",
23362                        -INTVAL (XEXP (e1, 1)));
23363         }
23364       else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
23365         {
23366           HOST_WIDE_INT offset;
23367
23368           if (GET_CODE (e1) == PLUS)
23369             {
23370               if (GET_CODE (XEXP (e1, 0)) != REG
23371                   || GET_CODE (XEXP (e1, 1)) != CONST_INT)
23372                 abort ();
23373               reg = REGNO (XEXP (e1, 0));
23374               offset = INTVAL (XEXP (e1, 1));
23375               asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
23376                            HARD_FRAME_POINTER_REGNUM, reg,
23377                            offset);
23378             }
23379           else if (GET_CODE (e1) == REG)
23380             {
23381               reg = REGNO (e1);
23382               asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
23383                            HARD_FRAME_POINTER_REGNUM, reg);
23384             }
23385           else
23386             abort ();
23387         }
23388       else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
23389         {
23390           /* Move from sp to reg.  */
23391           asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
23392         }
23393      else if (GET_CODE (e1) == PLUS
23394               && GET_CODE (XEXP (e1, 0)) == REG
23395               && REGNO (XEXP (e1, 0)) == SP_REGNUM
23396               && GET_CODE (XEXP (e1, 1)) == CONST_INT)
23397         {
23398           /* Set reg to offset from sp.  */
23399           asm_fprintf (asm_out_file, "\t.movsp %r, #%d\n",
23400                        REGNO (e0), (int)INTVAL(XEXP (e1, 1)));
23401         }
23402       else
23403         abort ();
23404       break;
23405
23406     default:
23407       abort ();
23408     }
23409 }
23410
23411
23412 /* Emit unwind directives for the given insn.  */
23413
23414 static void
23415 arm_unwind_emit (FILE * asm_out_file, rtx insn)
23416 {
23417   rtx note, pat;
23418   bool handled_one = false;
23419
23420   if (arm_except_unwind_info (&global_options) != UI_TARGET)
23421     return;
23422
23423   if (!(flag_unwind_tables || crtl->uses_eh_lsda)
23424       && (TREE_NOTHROW (current_function_decl)
23425           || crtl->all_throwers_are_sibcalls))
23426     return;
23427
23428   if (NOTE_P (insn) || !RTX_FRAME_RELATED_P (insn))
23429     return;
23430
23431   for (note = REG_NOTES (insn); note ; note = XEXP (note, 1))
23432     {
23433       pat = XEXP (note, 0);
23434       switch (REG_NOTE_KIND (note))
23435         {
23436         case REG_FRAME_RELATED_EXPR:
23437           goto found;
23438
23439         case REG_CFA_REGISTER:
23440           if (pat == NULL)
23441             {
23442               pat = PATTERN (insn);
23443               if (GET_CODE (pat) == PARALLEL)
23444                 pat = XVECEXP (pat, 0, 0);
23445             }
23446
23447           /* Only emitted for IS_STACKALIGN re-alignment.  */
23448           {
23449             rtx dest, src;
23450             unsigned reg;
23451
23452             src = SET_SRC (pat);
23453             dest = SET_DEST (pat);
23454
23455             gcc_assert (src == stack_pointer_rtx);
23456             reg = REGNO (dest);
23457             asm_fprintf (asm_out_file, "\t.unwind_raw 0, 0x%x @ vsp = r%d\n",
23458                          reg + 0x90, reg);
23459           }
23460           handled_one = true;
23461           break;
23462
23463         case REG_CFA_DEF_CFA:
23464         case REG_CFA_EXPRESSION:
23465         case REG_CFA_ADJUST_CFA:
23466         case REG_CFA_OFFSET:
23467           /* ??? Only handling here what we actually emit.  */
23468           gcc_unreachable ();
23469
23470         default:
23471           break;
23472         }
23473     }
23474   if (handled_one)
23475     return;
23476   pat = PATTERN (insn);
23477  found:
23478
23479   switch (GET_CODE (pat))
23480     {
23481     case SET:
23482       arm_unwind_emit_set (asm_out_file, pat);
23483       break;
23484
23485     case SEQUENCE:
23486       /* Store multiple.  */
23487       arm_unwind_emit_sequence (asm_out_file, pat);
23488       break;
23489
23490     default:
23491       abort();
23492     }
23493 }
23494
23495
23496 /* Output a reference from a function exception table to the type_info
23497    object X.  The EABI specifies that the symbol should be relocated by
23498    an R_ARM_TARGET2 relocation.  */
23499
23500 static bool
23501 arm_output_ttype (rtx x)
23502 {
23503   fputs ("\t.word\t", asm_out_file);
23504   output_addr_const (asm_out_file, x);
23505   /* Use special relocations for symbol references.  */
23506   if (GET_CODE (x) != CONST_INT)
23507     fputs ("(TARGET2)", asm_out_file);
23508   fputc ('\n', asm_out_file);
23509
23510   return TRUE;
23511 }
23512
23513 /* Implement TARGET_ASM_EMIT_EXCEPT_PERSONALITY.  */
23514
23515 static void
23516 arm_asm_emit_except_personality (rtx personality)
23517 {
23518   fputs ("\t.personality\t", asm_out_file);
23519   output_addr_const (asm_out_file, personality);
23520   fputc ('\n', asm_out_file);
23521 }
23522
23523 /* Implement TARGET_ASM_INITIALIZE_SECTIONS.  */
23524
23525 static void
23526 arm_asm_init_sections (void)
23527 {
23528   exception_section = get_unnamed_section (0, output_section_asm_op,
23529                                            "\t.handlerdata");
23530 }
23531 #endif /* ARM_UNWIND_INFO */
23532
23533 /* Output unwind directives for the start/end of a function.  */
23534
23535 void
23536 arm_output_fn_unwind (FILE * f, bool prologue)
23537 {
23538   if (arm_except_unwind_info (&global_options) != UI_TARGET)
23539     return;
23540
23541   if (prologue)
23542     fputs ("\t.fnstart\n", f);
23543   else
23544     {
23545       /* If this function will never be unwound, then mark it as such.
23546          The came condition is used in arm_unwind_emit to suppress
23547          the frame annotations.  */
23548       if (!(flag_unwind_tables || crtl->uses_eh_lsda)
23549           && (TREE_NOTHROW (current_function_decl)
23550               || crtl->all_throwers_are_sibcalls))
23551         fputs("\t.cantunwind\n", f);
23552
23553       fputs ("\t.fnend\n", f);
23554     }
23555 }
23556
23557 static bool
23558 arm_emit_tls_decoration (FILE *fp, rtx x)
23559 {
23560   enum tls_reloc reloc;
23561   rtx val;
23562
23563   val = XVECEXP (x, 0, 0);
23564   reloc = (enum tls_reloc) INTVAL (XVECEXP (x, 0, 1));
23565
23566   output_addr_const (fp, val);
23567
23568   switch (reloc)
23569     {
23570     case TLS_GD32:
23571       fputs ("(tlsgd)", fp);
23572       break;
23573     case TLS_LDM32:
23574       fputs ("(tlsldm)", fp);
23575       break;
23576     case TLS_LDO32:
23577       fputs ("(tlsldo)", fp);
23578       break;
23579     case TLS_IE32:
23580       fputs ("(gottpoff)", fp);
23581       break;
23582     case TLS_LE32:
23583       fputs ("(tpoff)", fp);
23584       break;
23585     case TLS_DESCSEQ:
23586       fputs ("(tlsdesc)", fp);
23587       break;
23588     default:
23589       gcc_unreachable ();
23590     }
23591
23592   switch (reloc)
23593     {
23594     case TLS_GD32:
23595     case TLS_LDM32:
23596     case TLS_IE32:
23597     case TLS_DESCSEQ:
23598       fputs (" + (. - ", fp);
23599       output_addr_const (fp, XVECEXP (x, 0, 2));
23600       /* For DESCSEQ the 3rd operand encodes thumbness, and is added */
23601       fputs (reloc == TLS_DESCSEQ ? " + " : " - ", fp);
23602       output_addr_const (fp, XVECEXP (x, 0, 3));
23603       fputc (')', fp);
23604       break;
23605     default:
23606       break;
23607     }
23608
23609   return TRUE;
23610 }
23611
23612 /* ARM implementation of TARGET_ASM_OUTPUT_DWARF_DTPREL.  */
23613
23614 static void
23615 arm_output_dwarf_dtprel (FILE *file, int size, rtx x)
23616 {
23617   gcc_assert (size == 4);
23618   fputs ("\t.word\t", file);
23619   output_addr_const (file, x);
23620   fputs ("(tlsldo)", file);
23621 }
23622
23623 /* Implement TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA.  */
23624
23625 static bool
23626 arm_output_addr_const_extra (FILE *fp, rtx x)
23627 {
23628   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
23629     return arm_emit_tls_decoration (fp, x);
23630   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
23631     {
23632       char label[256];
23633       int labelno = INTVAL (XVECEXP (x, 0, 0));
23634
23635       ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
23636       assemble_name_raw (fp, label);
23637
23638       return TRUE;
23639     }
23640   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_GOTSYM_OFF)
23641     {
23642       assemble_name (fp, "_GLOBAL_OFFSET_TABLE_");
23643       if (GOT_PCREL)
23644         fputs ("+.", fp);
23645       fputs ("-(", fp);
23646       output_addr_const (fp, XVECEXP (x, 0, 0));
23647       fputc (')', fp);
23648       return TRUE;
23649     }
23650   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_SYMBOL_OFFSET)
23651     {
23652       output_addr_const (fp, XVECEXP (x, 0, 0));
23653       if (GOT_PCREL)
23654         fputs ("+.", fp);
23655       fputs ("-(", fp);
23656       output_addr_const (fp, XVECEXP (x, 0, 1));
23657       fputc (')', fp);
23658       return TRUE;
23659     }
23660   else if (GET_CODE (x) == CONST_VECTOR)
23661     return arm_emit_vector_const (fp, x);
23662
23663   return FALSE;
23664 }
23665
23666 /* Output assembly for a shift instruction.
23667    SET_FLAGS determines how the instruction modifies the condition codes.
23668    0 - Do not set condition codes.
23669    1 - Set condition codes.
23670    2 - Use smallest instruction.  */
23671 const char *
23672 arm_output_shift(rtx * operands, int set_flags)
23673 {
23674   char pattern[100];
23675   static const char flag_chars[3] = {'?', '.', '!'};
23676   const char *shift;
23677   HOST_WIDE_INT val;
23678   char c;
23679   
23680   c = flag_chars[set_flags];
23681   if (TARGET_UNIFIED_ASM)
23682     {
23683       shift = shift_op(operands[3], &val);
23684       if (shift)
23685         {
23686           if (val != -1)
23687             operands[2] = GEN_INT(val);
23688           sprintf (pattern, "%s%%%c\t%%0, %%1, %%2", shift, c);
23689         }
23690       else
23691         sprintf (pattern, "mov%%%c\t%%0, %%1", c);
23692     }
23693   else
23694     sprintf (pattern, "mov%%%c\t%%0, %%1%%S3", c);
23695   output_asm_insn (pattern, operands);
23696   return "";
23697 }
23698
23699 /* Output a Thumb-1 casesi dispatch sequence.  */
23700 const char *
23701 thumb1_output_casesi (rtx *operands)
23702 {
23703   rtx diff_vec = PATTERN (next_real_insn (operands[0]));
23704
23705   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
23706
23707   switch (GET_MODE(diff_vec))
23708     {
23709     case QImode:
23710       return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned ? 
23711               "bl\t%___gnu_thumb1_case_uqi" : "bl\t%___gnu_thumb1_case_sqi");
23712     case HImode:
23713       return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned ? 
23714               "bl\t%___gnu_thumb1_case_uhi" : "bl\t%___gnu_thumb1_case_shi");
23715     case SImode:
23716       return "bl\t%___gnu_thumb1_case_si";
23717     default:
23718       gcc_unreachable ();
23719     }
23720 }
23721
23722 /* Output a Thumb-2 casesi instruction.  */
23723 const char *
23724 thumb2_output_casesi (rtx *operands)
23725 {
23726   rtx diff_vec = PATTERN (next_real_insn (operands[2]));
23727
23728   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
23729
23730   output_asm_insn ("cmp\t%0, %1", operands);
23731   output_asm_insn ("bhi\t%l3", operands);
23732   switch (GET_MODE(diff_vec))
23733     {
23734     case QImode:
23735       return "tbb\t[%|pc, %0]";
23736     case HImode:
23737       return "tbh\t[%|pc, %0, lsl #1]";
23738     case SImode:
23739       if (flag_pic)
23740         {
23741           output_asm_insn ("adr\t%4, %l2", operands);
23742           output_asm_insn ("ldr\t%5, [%4, %0, lsl #2]", operands);
23743           output_asm_insn ("add\t%4, %4, %5", operands);
23744           return "bx\t%4";
23745         }
23746       else
23747         {
23748           output_asm_insn ("adr\t%4, %l2", operands);
23749           return "ldr\t%|pc, [%4, %0, lsl #2]";
23750         }
23751     default:
23752       gcc_unreachable ();
23753     }
23754 }
23755
23756 /* Most ARM cores are single issue, but some newer ones can dual issue.
23757    The scheduler descriptions rely on this being correct.  */
23758 static int
23759 arm_issue_rate (void)
23760 {
23761   switch (arm_tune)
23762     {
23763     case cortexr4:
23764     case cortexr4f:
23765     case cortexr5:
23766     case genericv7a:
23767     case cortexa5:
23768     case cortexa8:
23769     case cortexa9:
23770     case fa726te:
23771       return 2;
23772
23773     default:
23774       return 1;
23775     }
23776 }
23777
23778 /* A table and a function to perform ARM-specific name mangling for
23779    NEON vector types in order to conform to the AAPCS (see "Procedure
23780    Call Standard for the ARM Architecture", Appendix A).  To qualify
23781    for emission with the mangled names defined in that document, a
23782    vector type must not only be of the correct mode but also be
23783    composed of NEON vector element types (e.g. __builtin_neon_qi).  */
23784 typedef struct
23785 {
23786   enum machine_mode mode;
23787   const char *element_type_name;
23788   const char *aapcs_name;
23789 } arm_mangle_map_entry;
23790
23791 static arm_mangle_map_entry arm_mangle_map[] = {
23792   /* 64-bit containerized types.  */
23793   { V8QImode,  "__builtin_neon_qi",     "15__simd64_int8_t" },
23794   { V8QImode,  "__builtin_neon_uqi",    "16__simd64_uint8_t" },
23795   { V4HImode,  "__builtin_neon_hi",     "16__simd64_int16_t" },
23796   { V4HImode,  "__builtin_neon_uhi",    "17__simd64_uint16_t" },
23797   { V2SImode,  "__builtin_neon_si",     "16__simd64_int32_t" },
23798   { V2SImode,  "__builtin_neon_usi",    "17__simd64_uint32_t" },
23799   { V2SFmode,  "__builtin_neon_sf",     "18__simd64_float32_t" },
23800   { V8QImode,  "__builtin_neon_poly8",  "16__simd64_poly8_t" },
23801   { V4HImode,  "__builtin_neon_poly16", "17__simd64_poly16_t" },
23802   /* 128-bit containerized types.  */
23803   { V16QImode, "__builtin_neon_qi",     "16__simd128_int8_t" },
23804   { V16QImode, "__builtin_neon_uqi",    "17__simd128_uint8_t" },
23805   { V8HImode,  "__builtin_neon_hi",     "17__simd128_int16_t" },
23806   { V8HImode,  "__builtin_neon_uhi",    "18__simd128_uint16_t" },
23807   { V4SImode,  "__builtin_neon_si",     "17__simd128_int32_t" },
23808   { V4SImode,  "__builtin_neon_usi",    "18__simd128_uint32_t" },
23809   { V4SFmode,  "__builtin_neon_sf",     "19__simd128_float32_t" },
23810   { V16QImode, "__builtin_neon_poly8",  "17__simd128_poly8_t" },
23811   { V8HImode,  "__builtin_neon_poly16", "18__simd128_poly16_t" },
23812   { VOIDmode, NULL, NULL }
23813 };
23814
23815 const char *
23816 arm_mangle_type (const_tree type)
23817 {
23818   arm_mangle_map_entry *pos = arm_mangle_map;
23819
23820   /* The ARM ABI documents (10th October 2008) say that "__va_list"
23821      has to be managled as if it is in the "std" namespace.  */
23822   if (TARGET_AAPCS_BASED 
23823       && lang_hooks.types_compatible_p (CONST_CAST_TREE (type), va_list_type))
23824     {
23825       static bool warned;
23826       if (!warned && warn_psabi && !in_system_header)
23827         {
23828           warned = true;
23829           inform (input_location,
23830                   "the mangling of %<va_list%> has changed in GCC 4.4");
23831         }
23832       return "St9__va_list";
23833     }
23834
23835   /* Half-precision float.  */
23836   if (TREE_CODE (type) == REAL_TYPE && TYPE_PRECISION (type) == 16)
23837     return "Dh";
23838
23839   if (TREE_CODE (type) != VECTOR_TYPE)
23840     return NULL;
23841
23842   /* Check the mode of the vector type, and the name of the vector
23843      element type, against the table.  */
23844   while (pos->mode != VOIDmode)
23845     {
23846       tree elt_type = TREE_TYPE (type);
23847
23848       if (pos->mode == TYPE_MODE (type)
23849           && TREE_CODE (TYPE_NAME (elt_type)) == TYPE_DECL
23850           && !strcmp (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (elt_type))),
23851                       pos->element_type_name))
23852         return pos->aapcs_name;
23853
23854       pos++;
23855     }
23856
23857   /* Use the default mangling for unrecognized (possibly user-defined)
23858      vector types.  */
23859   return NULL;
23860 }
23861
23862 /* Order of allocation of core registers for Thumb: this allocation is
23863    written over the corresponding initial entries of the array
23864    initialized with REG_ALLOC_ORDER.  We allocate all low registers
23865    first.  Saving and restoring a low register is usually cheaper than
23866    using a call-clobbered high register.  */
23867
23868 static const int thumb_core_reg_alloc_order[] =
23869 {
23870    3,  2,  1,  0,  4,  5,  6,  7,
23871   14, 12,  8,  9, 10, 11, 13, 15
23872 };
23873
23874 /* Adjust register allocation order when compiling for Thumb.  */
23875
23876 void
23877 arm_order_regs_for_local_alloc (void)
23878 {
23879   const int arm_reg_alloc_order[] = REG_ALLOC_ORDER;
23880   memcpy(reg_alloc_order, arm_reg_alloc_order, sizeof (reg_alloc_order));
23881   if (TARGET_THUMB)
23882     memcpy (reg_alloc_order, thumb_core_reg_alloc_order,
23883             sizeof (thumb_core_reg_alloc_order));
23884 }
23885
23886 /* Implement TARGET_FRAME_POINTER_REQUIRED.  */
23887
23888 bool
23889 arm_frame_pointer_required (void)
23890 {
23891   return (cfun->has_nonlocal_label
23892           || SUBTARGET_FRAME_POINTER_REQUIRED
23893           || (TARGET_ARM && TARGET_APCS_FRAME && ! leaf_function_p ()));
23894 }
23895
23896 /* Only thumb1 can't support conditional execution, so return true if
23897    the target is not thumb1.  */
23898 static bool
23899 arm_have_conditional_execution (void)
23900 {
23901   return !TARGET_THUMB1;
23902 }
23903
23904 /* Legitimize a memory reference for sync primitive implemented using
23905    ldrex / strex.  We currently force the form of the reference to be
23906    indirect without offset.  We do not yet support the indirect offset
23907    addressing supported by some ARM targets for these
23908    instructions.  */
23909 static rtx
23910 arm_legitimize_sync_memory (rtx memory)
23911 {
23912   rtx addr = force_reg (Pmode, XEXP (memory, 0));
23913   rtx legitimate_memory = gen_rtx_MEM (GET_MODE (memory), addr);
23914
23915   set_mem_alias_set (legitimate_memory, ALIAS_SET_MEMORY_BARRIER);
23916   MEM_VOLATILE_P (legitimate_memory) = MEM_VOLATILE_P (memory);
23917   return legitimate_memory;
23918 }
23919
23920 /* An instruction emitter. */
23921 typedef void (* emit_f) (int label, const char *, rtx *);
23922
23923 /* An instruction emitter that emits via the conventional
23924    output_asm_insn.  */
23925 static void
23926 arm_emit (int label ATTRIBUTE_UNUSED, const char *pattern, rtx *operands)
23927 {
23928   output_asm_insn (pattern, operands);
23929 }
23930
23931 /* Count the number of emitted synchronization instructions.  */
23932 static unsigned arm_insn_count;
23933
23934 /* An emitter that counts emitted instructions but does not actually
23935    emit instruction into the instruction stream.  */
23936 static void
23937 arm_count (int label,
23938            const char *pattern ATTRIBUTE_UNUSED,
23939            rtx *operands ATTRIBUTE_UNUSED)
23940 {
23941   if (! label)
23942     ++ arm_insn_count;
23943 }
23944
23945 /* Construct a pattern using conventional output formatting and feed
23946    it to output_asm_insn.  Provides a mechanism to construct the
23947    output pattern on the fly.  Note the hard limit on the pattern
23948    buffer size.  */
23949 static void ATTRIBUTE_PRINTF_4
23950 arm_output_asm_insn (emit_f emit, int label, rtx *operands,
23951                      const char *pattern, ...)
23952 {
23953   va_list ap;
23954   char buffer[256];
23955
23956   va_start (ap, pattern);
23957   vsprintf (buffer, pattern, ap);
23958   va_end (ap);
23959   emit (label, buffer, operands);
23960 }
23961
23962 /* Emit the memory barrier instruction, if any, provided by this
23963    target to a specified emitter.  */
23964 static void
23965 arm_process_output_memory_barrier (emit_f emit, rtx *operands)
23966 {
23967   if (TARGET_HAVE_DMB)
23968     {
23969       /* Note we issue a system level barrier. We should consider
23970          issuing a inner shareabilty zone barrier here instead, ie.
23971          "DMB ISH".  */
23972       emit (0, "dmb\tsy", operands);
23973       return;
23974     }
23975
23976   if (TARGET_HAVE_DMB_MCR)
23977     {
23978       emit (0, "mcr\tp15, 0, r0, c7, c10, 5", operands);
23979       return;
23980     }
23981
23982   gcc_unreachable ();
23983 }
23984
23985 /* Emit the memory barrier instruction, if any, provided by this
23986    target.  */
23987 const char *
23988 arm_output_memory_barrier (rtx *operands)
23989 {
23990   arm_process_output_memory_barrier (arm_emit, operands);
23991   return "";
23992 }
23993
23994 /* Helper to figure out the instruction suffix required on ldrex/strex
23995    for operations on an object of the specified mode.  */
23996 static const char *
23997 arm_ldrex_suffix (enum machine_mode mode)
23998 {
23999   switch (mode)
24000     {
24001     case QImode: return "b";
24002     case HImode: return "h";
24003     case SImode: return "";
24004     case DImode: return "d";
24005     default:
24006       gcc_unreachable ();
24007     }
24008   return "";
24009 }
24010
24011 /* Emit an ldrex{b,h,d, } instruction appropriate for the specified
24012    mode.  */
24013 static void
24014 arm_output_ldrex (emit_f emit,
24015                   enum machine_mode mode,
24016                   rtx target,
24017                   rtx memory)
24018 {
24019   const char *suffix = arm_ldrex_suffix (mode);
24020   rtx operands[2];
24021
24022   operands[0] = target;
24023   operands[1] = memory;
24024   arm_output_asm_insn (emit, 0, operands, "ldrex%s\t%%0, %%C1", suffix);
24025 }
24026
24027 /* Emit a strex{b,h,d, } instruction appropriate for the specified
24028    mode.  */
24029 static void
24030 arm_output_strex (emit_f emit,
24031                   enum machine_mode mode,
24032                   const char *cc,
24033                   rtx result,
24034                   rtx value,
24035                   rtx memory)
24036 {
24037   const char *suffix = arm_ldrex_suffix (mode);
24038   rtx operands[3];
24039
24040   operands[0] = result;
24041   operands[1] = value;
24042   operands[2] = memory;
24043   arm_output_asm_insn (emit, 0, operands, "strex%s%s\t%%0, %%1, %%C2", suffix,
24044                        cc);
24045 }
24046
24047 /* Helper to emit a two operand instruction.  */
24048 static void
24049 arm_output_op2 (emit_f emit, const char *mnemonic, rtx d, rtx s)
24050 {
24051   rtx operands[2];
24052
24053   operands[0] = d;
24054   operands[1] = s;
24055   arm_output_asm_insn (emit, 0, operands, "%s\t%%0, %%1", mnemonic);
24056 }
24057
24058 /* Helper to emit a three operand instruction.  */
24059 static void
24060 arm_output_op3 (emit_f emit, const char *mnemonic, rtx d, rtx a, rtx b)
24061 {
24062   rtx operands[3];
24063
24064   operands[0] = d;
24065   operands[1] = a;
24066   operands[2] = b;
24067   arm_output_asm_insn (emit, 0, operands, "%s\t%%0, %%1, %%2", mnemonic);
24068 }
24069
24070 /* Emit a load store exclusive synchronization loop.
24071
24072    do
24073      old_value = [mem]
24074      if old_value != required_value
24075        break;
24076      t1 = sync_op (old_value, new_value)
24077      [mem] = t1, t2 = [0|1]
24078    while ! t2
24079
24080    Note:
24081      t1 == t2 is not permitted
24082      t1 == old_value is permitted
24083
24084    required_value:
24085
24086    RTX register or const_int representing the required old_value for
24087    the modify to continue, if NULL no comparsion is performed.  */
24088 static void
24089 arm_output_sync_loop (emit_f emit,
24090                       enum machine_mode mode,
24091                       rtx old_value,
24092                       rtx memory,
24093                       rtx required_value,
24094                       rtx new_value,
24095                       rtx t1,
24096                       rtx t2,
24097                       enum attr_sync_op sync_op,
24098                       int early_barrier_required)
24099 {
24100   rtx operands[1];
24101
24102   gcc_assert (t1 != t2);
24103
24104   if (early_barrier_required)
24105     arm_process_output_memory_barrier (emit, NULL);
24106
24107   arm_output_asm_insn (emit, 1, operands, "%sLSYT%%=:", LOCAL_LABEL_PREFIX);
24108
24109   arm_output_ldrex (emit, mode, old_value, memory);
24110
24111   if (required_value)
24112     {
24113       rtx operands[2];
24114
24115       operands[0] = old_value;
24116       operands[1] = required_value;
24117       arm_output_asm_insn (emit, 0, operands, "cmp\t%%0, %%1");
24118       arm_output_asm_insn (emit, 0, operands, "bne\t%sLSYB%%=", LOCAL_LABEL_PREFIX);
24119     }
24120
24121   switch (sync_op)
24122     {
24123     case SYNC_OP_ADD:
24124       arm_output_op3 (emit, "add", t1, old_value, new_value);
24125       break;
24126
24127     case SYNC_OP_SUB:
24128       arm_output_op3 (emit, "sub", t1, old_value, new_value);
24129       break;
24130
24131     case SYNC_OP_IOR:
24132       arm_output_op3 (emit, "orr", t1, old_value, new_value);
24133       break;
24134
24135     case SYNC_OP_XOR:
24136       arm_output_op3 (emit, "eor", t1, old_value, new_value);
24137       break;
24138
24139     case SYNC_OP_AND:
24140       arm_output_op3 (emit,"and", t1, old_value, new_value);
24141       break;
24142
24143     case SYNC_OP_NAND:
24144       arm_output_op3 (emit, "and", t1, old_value, new_value);
24145       arm_output_op2 (emit, "mvn", t1, t1);
24146       break;
24147
24148     case SYNC_OP_NONE:
24149       t1 = new_value;
24150       break;
24151     }
24152
24153   if (t2)
24154     {
24155        arm_output_strex (emit, mode, "", t2, t1, memory);
24156        operands[0] = t2;
24157        arm_output_asm_insn (emit, 0, operands, "teq\t%%0, #0");
24158        arm_output_asm_insn (emit, 0, operands, "bne\t%sLSYT%%=",
24159                             LOCAL_LABEL_PREFIX);
24160     }
24161   else
24162     {
24163       /* Use old_value for the return value because for some operations
24164          the old_value can easily be restored.  This saves one register.  */
24165       arm_output_strex (emit, mode, "", old_value, t1, memory);
24166       operands[0] = old_value;
24167       arm_output_asm_insn (emit, 0, operands, "teq\t%%0, #0");
24168       arm_output_asm_insn (emit, 0, operands, "bne\t%sLSYT%%=",
24169                            LOCAL_LABEL_PREFIX);
24170
24171       switch (sync_op)
24172         {
24173         case SYNC_OP_ADD:
24174           arm_output_op3 (emit, "sub", old_value, t1, new_value);
24175           break;
24176
24177         case SYNC_OP_SUB:
24178           arm_output_op3 (emit, "add", old_value, t1, new_value);
24179           break;
24180
24181         case SYNC_OP_XOR:
24182           arm_output_op3 (emit, "eor", old_value, t1, new_value);
24183           break;
24184
24185         case SYNC_OP_NONE:
24186           arm_output_op2 (emit, "mov", old_value, required_value);
24187           break;
24188
24189         default:
24190           gcc_unreachable ();
24191         }
24192     }
24193
24194   arm_process_output_memory_barrier (emit, NULL);
24195   arm_output_asm_insn (emit, 1, operands, "%sLSYB%%=:", LOCAL_LABEL_PREFIX);
24196 }
24197
24198 static rtx
24199 arm_get_sync_operand (rtx *operands, int index, rtx default_value)
24200 {
24201   if (index > 0)
24202     default_value = operands[index - 1];
24203
24204   return default_value;
24205 }
24206
24207 #define FETCH_SYNC_OPERAND(NAME, DEFAULT) \
24208   arm_get_sync_operand (operands, (int) get_attr_sync_##NAME (insn), DEFAULT);
24209
24210 /* Extract the operands for a synchroniztion instruction from the
24211    instructions attributes and emit the instruction.  */
24212 static void
24213 arm_process_output_sync_insn (emit_f emit, rtx insn, rtx *operands)
24214 {
24215   rtx result, memory, required_value, new_value, t1, t2;
24216   int early_barrier;
24217   enum machine_mode mode;
24218   enum attr_sync_op sync_op;
24219
24220   result = FETCH_SYNC_OPERAND(result, 0);
24221   memory = FETCH_SYNC_OPERAND(memory, 0);
24222   required_value = FETCH_SYNC_OPERAND(required_value, 0);
24223   new_value = FETCH_SYNC_OPERAND(new_value, 0);
24224   t1 = FETCH_SYNC_OPERAND(t1, 0);
24225   t2 = FETCH_SYNC_OPERAND(t2, 0);
24226   early_barrier =
24227     get_attr_sync_release_barrier (insn) == SYNC_RELEASE_BARRIER_YES;
24228   sync_op = get_attr_sync_op (insn);
24229   mode = GET_MODE (memory);
24230
24231   arm_output_sync_loop (emit, mode, result, memory, required_value,
24232                         new_value, t1, t2, sync_op, early_barrier);
24233 }
24234
24235 /* Emit a synchronization instruction loop.  */
24236 const char *
24237 arm_output_sync_insn (rtx insn, rtx *operands)
24238 {
24239   arm_process_output_sync_insn (arm_emit, insn, operands);
24240   return "";
24241 }
24242
24243 /* Count the number of machine instruction that will be emitted for a
24244    synchronization instruction.  Note that the emitter used does not
24245    emit instructions, it just counts instructions being carefull not
24246    to count labels.  */
24247 unsigned int
24248 arm_sync_loop_insns (rtx insn, rtx *operands)
24249 {
24250   arm_insn_count = 0;
24251   arm_process_output_sync_insn (arm_count, insn, operands);
24252   return arm_insn_count;
24253 }
24254
24255 /* Helper to call a target sync instruction generator, dealing with
24256    the variation in operands required by the different generators.  */
24257 static rtx
24258 arm_call_generator (struct arm_sync_generator *generator, rtx old_value,
24259                     rtx memory, rtx required_value, rtx new_value)
24260 {
24261   switch (generator->op)
24262     {
24263     case arm_sync_generator_omn:
24264       gcc_assert (! required_value);
24265       return generator->u.omn (old_value, memory, new_value);
24266
24267     case arm_sync_generator_omrn:
24268       gcc_assert (required_value);
24269       return generator->u.omrn (old_value, memory, required_value, new_value);
24270     }
24271
24272   return NULL;
24273 }
24274
24275 /* Expand a synchronization loop. The synchronization loop is expanded
24276    as an opaque block of instructions in order to ensure that we do
24277    not subsequently get extraneous memory accesses inserted within the
24278    critical region. The exclusive access property of ldrex/strex is
24279    only guaranteed in there are no intervening memory accesses. */
24280 void
24281 arm_expand_sync (enum machine_mode mode,
24282                  struct arm_sync_generator *generator,
24283                  rtx target, rtx memory, rtx required_value, rtx new_value)
24284 {
24285   if (target == NULL)
24286     target = gen_reg_rtx (mode);
24287
24288   memory = arm_legitimize_sync_memory (memory);
24289   if (mode != SImode)
24290     {
24291       rtx load_temp = gen_reg_rtx (SImode);
24292
24293       if (required_value)
24294         required_value = convert_modes (SImode, mode, required_value, true);
24295
24296       new_value = convert_modes (SImode, mode, new_value, true);
24297       emit_insn (arm_call_generator (generator, load_temp, memory,
24298                                      required_value, new_value));
24299       emit_move_insn (target, gen_lowpart (mode, load_temp));
24300     }
24301   else
24302     {
24303       emit_insn (arm_call_generator (generator, target, memory, required_value,
24304                                      new_value));
24305     }
24306 }
24307
24308 static unsigned int
24309 arm_autovectorize_vector_sizes (void)
24310 {
24311   return TARGET_NEON_VECTORIZE_DOUBLE ? 0 : (16 | 8);
24312 }
24313
24314 static bool
24315 arm_vector_alignment_reachable (const_tree type, bool is_packed)
24316 {
24317   /* Vectors which aren't in packed structures will not be less aligned than
24318      the natural alignment of their element type, so this is safe.  */
24319   if (TARGET_NEON && !BYTES_BIG_ENDIAN)
24320     return !is_packed;
24321
24322   return default_builtin_vector_alignment_reachable (type, is_packed);
24323 }
24324
24325 static bool
24326 arm_builtin_support_vector_misalignment (enum machine_mode mode,
24327                                          const_tree type, int misalignment,
24328                                          bool is_packed)
24329 {
24330   if (TARGET_NEON && !BYTES_BIG_ENDIAN)
24331     {
24332       HOST_WIDE_INT align = TYPE_ALIGN_UNIT (type);
24333
24334       if (is_packed)
24335         return align == 1;
24336
24337       /* If the misalignment is unknown, we should be able to handle the access
24338          so long as it is not to a member of a packed data structure.  */
24339       if (misalignment == -1)
24340         return true;
24341
24342       /* Return true if the misalignment is a multiple of the natural alignment
24343          of the vector's element type.  This is probably always going to be
24344          true in practice, since we've already established that this isn't a
24345          packed access.  */
24346       return ((misalignment % align) == 0);
24347     }
24348   
24349   return default_builtin_support_vector_misalignment (mode, type, misalignment,
24350                                                       is_packed);
24351 }
24352
24353 static void
24354 arm_conditional_register_usage (void)
24355 {
24356   int regno;
24357
24358   if (TARGET_SOFT_FLOAT || TARGET_THUMB1 || !TARGET_FPA)
24359     {
24360       for (regno = FIRST_FPA_REGNUM;
24361            regno <= LAST_FPA_REGNUM; ++regno)
24362         fixed_regs[regno] = call_used_regs[regno] = 1;
24363     }
24364
24365   if (TARGET_THUMB1 && optimize_size)
24366     {
24367       /* When optimizing for size on Thumb-1, it's better not
24368         to use the HI regs, because of the overhead of
24369         stacking them.  */
24370       for (regno = FIRST_HI_REGNUM;
24371            regno <= LAST_HI_REGNUM; ++regno)
24372         fixed_regs[regno] = call_used_regs[regno] = 1;
24373     }
24374
24375   /* The link register can be clobbered by any branch insn,
24376      but we have no way to track that at present, so mark
24377      it as unavailable.  */
24378   if (TARGET_THUMB1)
24379     fixed_regs[LR_REGNUM] = call_used_regs[LR_REGNUM] = 1;
24380
24381   if (TARGET_32BIT && TARGET_HARD_FLOAT)
24382     {
24383       if (TARGET_MAVERICK)
24384         {
24385           for (regno = FIRST_FPA_REGNUM;
24386                regno <= LAST_FPA_REGNUM; ++ regno)
24387             fixed_regs[regno] = call_used_regs[regno] = 1;
24388           for (regno = FIRST_CIRRUS_FP_REGNUM;
24389                regno <= LAST_CIRRUS_FP_REGNUM; ++ regno)
24390             {
24391               fixed_regs[regno] = 0;
24392               call_used_regs[regno] = regno < FIRST_CIRRUS_FP_REGNUM + 4;
24393             }
24394         }
24395       if (TARGET_VFP)
24396         {
24397           /* VFPv3 registers are disabled when earlier VFP
24398              versions are selected due to the definition of
24399              LAST_VFP_REGNUM.  */
24400           for (regno = FIRST_VFP_REGNUM;
24401                regno <= LAST_VFP_REGNUM; ++ regno)
24402             {
24403               fixed_regs[regno] = 0;
24404               call_used_regs[regno] = regno < FIRST_VFP_REGNUM + 16
24405                 || regno >= FIRST_VFP_REGNUM + 32;
24406             }
24407         }
24408     }
24409
24410   if (TARGET_REALLY_IWMMXT)
24411     {
24412       regno = FIRST_IWMMXT_GR_REGNUM;
24413       /* The 2002/10/09 revision of the XScale ABI has wCG0
24414          and wCG1 as call-preserved registers.  The 2002/11/21
24415          revision changed this so that all wCG registers are
24416          scratch registers.  */
24417       for (regno = FIRST_IWMMXT_GR_REGNUM;
24418            regno <= LAST_IWMMXT_GR_REGNUM; ++ regno)
24419         fixed_regs[regno] = 0;
24420       /* The XScale ABI has wR0 - wR9 as scratch registers,
24421          the rest as call-preserved registers.  */
24422       for (regno = FIRST_IWMMXT_REGNUM;
24423            regno <= LAST_IWMMXT_REGNUM; ++ regno)
24424         {
24425           fixed_regs[regno] = 0;
24426           call_used_regs[regno] = regno < FIRST_IWMMXT_REGNUM + 10;
24427         }
24428     }
24429
24430   if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
24431     {
24432       fixed_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
24433       call_used_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
24434     }
24435   else if (TARGET_APCS_STACK)
24436     {
24437       fixed_regs[10]     = 1;
24438       call_used_regs[10] = 1;
24439     }
24440   /* -mcaller-super-interworking reserves r11 for calls to
24441      _interwork_r11_call_via_rN().  Making the register global
24442      is an easy way of ensuring that it remains valid for all
24443      calls.  */
24444   if (TARGET_APCS_FRAME || TARGET_CALLER_INTERWORKING
24445       || TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME)
24446     {
24447       fixed_regs[ARM_HARD_FRAME_POINTER_REGNUM] = 1;
24448       call_used_regs[ARM_HARD_FRAME_POINTER_REGNUM] = 1;
24449       if (TARGET_CALLER_INTERWORKING)
24450         global_regs[ARM_HARD_FRAME_POINTER_REGNUM] = 1;
24451     }
24452   SUBTARGET_CONDITIONAL_REGISTER_USAGE
24453 }
24454
24455 static reg_class_t
24456 arm_preferred_rename_class (reg_class_t rclass)
24457 {
24458   /* Thumb-2 instructions using LO_REGS may be smaller than instructions
24459      using GENERIC_REGS.  During register rename pass, we prefer LO_REGS,
24460      and code size can be reduced.  */
24461   if (TARGET_THUMB2 && rclass == GENERAL_REGS)
24462     return LO_REGS;
24463   else
24464     return NO_REGS;
24465 }
24466
24467 /* Compute the atrribute "length" of insn "*push_multi".
24468    So this function MUST be kept in sync with that insn pattern.  */
24469 int
24470 arm_attr_length_push_multi(rtx parallel_op, rtx first_op)
24471 {
24472   int i, regno, hi_reg;
24473   int num_saves = XVECLEN (parallel_op, 0);
24474
24475   /* ARM mode.  */
24476   if (TARGET_ARM)
24477     return 4;
24478   /* Thumb1 mode.  */
24479   if (TARGET_THUMB1)
24480     return 2;
24481
24482   /* Thumb2 mode.  */
24483   regno = REGNO (first_op);
24484   hi_reg = (REGNO_REG_CLASS (regno) == HI_REGS) && (regno != LR_REGNUM);
24485   for (i = 1; i < num_saves && !hi_reg; i++)
24486     {
24487       regno = REGNO (XEXP (XVECEXP (parallel_op, 0, i), 0));
24488       hi_reg |= (REGNO_REG_CLASS (regno) == HI_REGS) && (regno != LR_REGNUM);
24489     }
24490
24491   if (!hi_reg)
24492     return 2;
24493   return 4;
24494 }
24495
24496 /* Compute the number of instructions emitted by output_move_double.  */
24497 int
24498 arm_count_output_move_double_insns (rtx *operands)
24499 {
24500   int count;
24501   output_move_double (operands, false, &count);
24502   return count;
24503 }
24504
24505 #include "gt-arm.h"