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
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
60 /* Forward definitions of types.  */
61 typedef struct minipool_node    Mnode;
62 typedef struct minipool_fixup   Mfix;
63
64 void (*arm_lang_output_object_attributes_hook)(void);
65
66 /* Forward function declarations.  */
67 static bool arm_needs_doubleword_align (enum machine_mode, const_tree);
68 static int arm_compute_static_chain_stack_bytes (void);
69 static arm_stack_offsets *arm_get_frame_offsets (void);
70 static void arm_add_gc_roots (void);
71 static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
72                              HOST_WIDE_INT, rtx, rtx, int, int);
73 static unsigned bit_count (unsigned long);
74 static int arm_address_register_rtx_p (rtx, int);
75 static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
76 static int thumb2_legitimate_index_p (enum machine_mode, rtx, int);
77 static int thumb1_base_register_rtx_p (rtx, enum machine_mode, int);
78 static rtx arm_legitimize_address (rtx, rtx, enum machine_mode);
79 static rtx thumb_legitimize_address (rtx, rtx, enum machine_mode);
80 inline static int thumb1_index_register_rtx_p (rtx, int);
81 static bool arm_legitimate_address_p (enum machine_mode, rtx, bool);
82 static int thumb_far_jump_used_p (void);
83 static bool thumb_force_lr_save (void);
84 static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
85 static rtx emit_sfm (int, int);
86 static unsigned arm_size_return_regs (void);
87 static bool arm_assemble_integer (rtx, unsigned int, int);
88 static void arm_print_operand (FILE *, rtx, int);
89 static void arm_print_operand_address (FILE *, rtx);
90 static bool arm_print_operand_punct_valid_p (unsigned char code);
91 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
92 static arm_cc get_arm_condition_code (rtx);
93 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
94 static rtx is_jump_table (rtx);
95 static const char *output_multi_immediate (rtx *, const char *, const char *,
96                                            int, HOST_WIDE_INT);
97 static const char *shift_op (rtx, HOST_WIDE_INT *);
98 static struct machine_function *arm_init_machine_status (void);
99 static void thumb_exit (FILE *, int);
100 static rtx is_jump_table (rtx);
101 static HOST_WIDE_INT get_jump_table_size (rtx);
102 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
103 static Mnode *add_minipool_forward_ref (Mfix *);
104 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
105 static Mnode *add_minipool_backward_ref (Mfix *);
106 static void assign_minipool_offsets (Mfix *);
107 static void arm_print_value (FILE *, rtx);
108 static void dump_minipool (rtx);
109 static int arm_barrier_cost (rtx);
110 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
111 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
112 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
113                                rtx);
114 static void arm_reorg (void);
115 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
116 static unsigned long arm_compute_save_reg0_reg12_mask (void);
117 static unsigned long arm_compute_save_reg_mask (void);
118 static unsigned long arm_isr_value (tree);
119 static unsigned long arm_compute_func_type (void);
120 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
121 static tree arm_handle_pcs_attribute (tree *, tree, tree, int, bool *);
122 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
123 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
124 static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
125 #endif
126 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
127 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
128 static void thumb1_output_function_prologue (FILE *, HOST_WIDE_INT);
129 static int arm_comp_type_attributes (const_tree, const_tree);
130 static void arm_set_default_type_attributes (tree);
131 static int arm_adjust_cost (rtx, rtx, rtx, int);
132 static int count_insns_for_constant (HOST_WIDE_INT, int);
133 static int arm_get_strip_length (int);
134 static bool arm_function_ok_for_sibcall (tree, tree);
135 static enum machine_mode arm_promote_function_mode (const_tree,
136                                                     enum machine_mode, int *,
137                                                     const_tree, int);
138 static bool arm_return_in_memory (const_tree, const_tree);
139 static rtx arm_function_value (const_tree, const_tree, bool);
140 static rtx arm_libcall_value (enum machine_mode, const_rtx);
141
142 static void arm_internal_label (FILE *, const char *, unsigned long);
143 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
144                                  tree);
145 static bool arm_have_conditional_execution (void);
146 static bool arm_rtx_costs_1 (rtx, enum rtx_code, int*, bool);
147 static bool arm_size_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *);
148 static bool arm_slowmul_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
149 static bool arm_fastmul_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
150 static bool arm_xscale_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
151 static bool arm_9e_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
152 static bool arm_rtx_costs (rtx, int, int, int *, bool);
153 static int arm_address_cost (rtx, bool);
154 static bool arm_memory_load_p (rtx);
155 static bool arm_cirrus_insn_p (rtx);
156 static void cirrus_reorg (rtx);
157 static void arm_init_builtins (void);
158 static void arm_init_iwmmxt_builtins (void);
159 static rtx safe_vector_operand (rtx, enum machine_mode);
160 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
161 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
162 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
163 static void emit_constant_insn (rtx cond, rtx pattern);
164 static rtx emit_set_insn (rtx, rtx);
165 static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
166                                   tree, bool);
167 static rtx arm_function_arg (CUMULATIVE_ARGS *, enum machine_mode,
168                              const_tree, bool);
169 static void arm_function_arg_advance (CUMULATIVE_ARGS *, enum machine_mode,
170                                       const_tree, bool);
171 static unsigned int arm_function_arg_boundary (enum machine_mode, const_tree);
172 static rtx aapcs_allocate_return_reg (enum machine_mode, const_tree,
173                                       const_tree);
174 static int aapcs_select_return_coproc (const_tree, const_tree);
175
176 #ifdef OBJECT_FORMAT_ELF
177 static void arm_elf_asm_constructor (rtx, int) ATTRIBUTE_UNUSED;
178 static void arm_elf_asm_destructor (rtx, int) ATTRIBUTE_UNUSED;
179 #endif
180 #ifndef ARM_PE
181 static void arm_encode_section_info (tree, rtx, int);
182 #endif
183
184 static void arm_file_end (void);
185 static void arm_file_start (void);
186
187 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
188                                         tree, int *, int);
189 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
190                                    enum machine_mode, const_tree, bool);
191 static bool arm_promote_prototypes (const_tree);
192 static bool arm_default_short_enums (void);
193 static bool arm_align_anon_bitfield (void);
194 static bool arm_return_in_msb (const_tree);
195 static bool arm_must_pass_in_stack (enum machine_mode, const_tree);
196 static bool arm_return_in_memory (const_tree, const_tree);
197 #if ARM_UNWIND_INFO
198 static void arm_unwind_emit (FILE *, rtx);
199 static bool arm_output_ttype (rtx);
200 static void arm_asm_emit_except_personality (rtx);
201 static void arm_asm_init_sections (void);
202 #endif
203 static enum unwind_info_type arm_except_unwind_info (struct gcc_options *);
204 static void arm_dwarf_handle_frame_unspec (const char *, rtx, int);
205 static rtx arm_dwarf_register_span (rtx);
206
207 static tree arm_cxx_guard_type (void);
208 static bool arm_cxx_guard_mask_bit (void);
209 static tree arm_get_cookie_size (tree);
210 static bool arm_cookie_has_size (void);
211 static bool arm_cxx_cdtor_returns_this (void);
212 static bool arm_cxx_key_method_may_be_inline (void);
213 static void arm_cxx_determine_class_data_visibility (tree);
214 static bool arm_cxx_class_data_always_comdat (void);
215 static bool arm_cxx_use_aeabi_atexit (void);
216 static void arm_init_libfuncs (void);
217 static tree arm_build_builtin_va_list (void);
218 static void arm_expand_builtin_va_start (tree, rtx);
219 static tree arm_gimplify_va_arg_expr (tree, tree, gimple_seq *, gimple_seq *);
220 static void arm_option_override (void);
221 static bool arm_handle_option (size_t, const char *, int);
222 static void arm_target_help (void);
223 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
224 static bool arm_cannot_copy_insn_p (rtx);
225 static bool arm_tls_symbol_p (rtx x);
226 static int arm_issue_rate (void);
227 static void arm_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
228 static bool arm_output_addr_const_extra (FILE *, rtx);
229 static bool arm_allocate_stack_slots_for_args (void);
230 static const char *arm_invalid_parameter_type (const_tree t);
231 static const char *arm_invalid_return_type (const_tree t);
232 static tree arm_promoted_type (const_tree t);
233 static tree arm_convert_to_type (tree type, tree expr);
234 static bool arm_scalar_mode_supported_p (enum machine_mode);
235 static bool arm_frame_pointer_required (void);
236 static bool arm_can_eliminate (const int, const int);
237 static void arm_asm_trampoline_template (FILE *);
238 static void arm_trampoline_init (rtx, tree, rtx);
239 static rtx arm_trampoline_adjust_address (rtx);
240 static rtx arm_pic_static_addr (rtx orig, rtx reg);
241 static bool cortex_a9_sched_adjust_cost (rtx, rtx, rtx, int *);
242 static bool xscale_sched_adjust_cost (rtx, rtx, rtx, int *);
243 static enum machine_mode arm_preferred_simd_mode (enum machine_mode);
244 static bool arm_class_likely_spilled_p (reg_class_t);
245 static bool arm_vector_alignment_reachable (const_tree type, bool is_packed);
246 static bool arm_builtin_support_vector_misalignment (enum machine_mode mode,
247                                                      const_tree type,
248                                                      int misalignment,
249                                                      bool is_packed);
250 static void arm_conditional_register_usage (void);
251
252 \f
253 /* Table of machine attributes.  */
254 static const struct attribute_spec arm_attribute_table[] =
255 {
256   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
257   /* Function calls made to this symbol must be done indirectly, because
258      it may lie outside of the 26 bit addressing range of a normal function
259      call.  */
260   { "long_call",    0, 0, false, true,  true,  NULL },
261   /* Whereas these functions are always known to reside within the 26 bit
262      addressing range.  */
263   { "short_call",   0, 0, false, true,  true,  NULL },
264   /* Specify the procedure call conventions for a function.  */
265   { "pcs",          1, 1, false, true,  true,  arm_handle_pcs_attribute },
266   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
267   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
268   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
269   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
270 #ifdef ARM_PE
271   /* ARM/PE has three new attributes:
272      interfacearm - ?
273      dllexport - for exporting a function/variable that will live in a dll
274      dllimport - for importing a function/variable from a dll
275
276      Microsoft allows multiple declspecs in one __declspec, separating
277      them with spaces.  We do NOT support this.  Instead, use __declspec
278      multiple times.
279   */
280   { "dllimport",    0, 0, true,  false, false, NULL },
281   { "dllexport",    0, 0, true,  false, false, NULL },
282   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
283 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
284   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
285   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
286   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
287 #endif
288   { NULL,           0, 0, false, false, false, NULL }
289 };
290
291 /* Set default optimization options.  */
292 static const struct default_options arm_option_optimization_table[] =
293   {
294     /* Enable section anchors by default at -O1 or higher.  */
295     { OPT_LEVELS_1_PLUS, OPT_fsection_anchors, NULL, 1 },
296     { OPT_LEVELS_1_PLUS, OPT_fomit_frame_pointer, NULL, 1 },
297     { OPT_LEVELS_NONE, 0, NULL, 0 }
298   };
299 \f
300 /* Initialize the GCC target structure.  */
301 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
302 #undef  TARGET_MERGE_DECL_ATTRIBUTES
303 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
304 #endif
305
306 #undef TARGET_LEGITIMIZE_ADDRESS
307 #define TARGET_LEGITIMIZE_ADDRESS arm_legitimize_address
308
309 #undef  TARGET_ATTRIBUTE_TABLE
310 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
311
312 #undef TARGET_ASM_FILE_START
313 #define TARGET_ASM_FILE_START arm_file_start
314 #undef TARGET_ASM_FILE_END
315 #define TARGET_ASM_FILE_END arm_file_end
316
317 #undef  TARGET_ASM_ALIGNED_SI_OP
318 #define TARGET_ASM_ALIGNED_SI_OP NULL
319 #undef  TARGET_ASM_INTEGER
320 #define TARGET_ASM_INTEGER arm_assemble_integer
321
322 #undef TARGET_PRINT_OPERAND
323 #define TARGET_PRINT_OPERAND arm_print_operand
324 #undef TARGET_PRINT_OPERAND_ADDRESS
325 #define TARGET_PRINT_OPERAND_ADDRESS arm_print_operand_address
326 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
327 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P arm_print_operand_punct_valid_p
328
329 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
330 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA arm_output_addr_const_extra
331
332 #undef  TARGET_ASM_FUNCTION_PROLOGUE
333 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
334
335 #undef  TARGET_ASM_FUNCTION_EPILOGUE
336 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
337
338 #undef  TARGET_DEFAULT_TARGET_FLAGS
339 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
340 #undef  TARGET_HANDLE_OPTION
341 #define TARGET_HANDLE_OPTION arm_handle_option
342 #undef  TARGET_HELP
343 #define TARGET_HELP arm_target_help
344 #undef  TARGET_OPTION_OVERRIDE
345 #define TARGET_OPTION_OVERRIDE arm_option_override
346 #undef  TARGET_OPTION_OPTIMIZATION_TABLE
347 #define TARGET_OPTION_OPTIMIZATION_TABLE arm_option_optimization_table
348
349 #undef  TARGET_COMP_TYPE_ATTRIBUTES
350 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
351
352 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
353 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
354
355 #undef  TARGET_SCHED_ADJUST_COST
356 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
357
358 #undef TARGET_ENCODE_SECTION_INFO
359 #ifdef ARM_PE
360 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
361 #else
362 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
363 #endif
364
365 #undef  TARGET_STRIP_NAME_ENCODING
366 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
367
368 #undef  TARGET_ASM_INTERNAL_LABEL
369 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
370
371 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
372 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
373
374 #undef  TARGET_FUNCTION_VALUE
375 #define TARGET_FUNCTION_VALUE arm_function_value
376
377 #undef  TARGET_LIBCALL_VALUE
378 #define TARGET_LIBCALL_VALUE arm_libcall_value
379
380 #undef  TARGET_ASM_OUTPUT_MI_THUNK
381 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
382 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
383 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
384
385 #undef  TARGET_RTX_COSTS
386 #define TARGET_RTX_COSTS arm_rtx_costs
387 #undef  TARGET_ADDRESS_COST
388 #define TARGET_ADDRESS_COST arm_address_cost
389
390 #undef TARGET_SHIFT_TRUNCATION_MASK
391 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
392 #undef TARGET_VECTOR_MODE_SUPPORTED_P
393 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
394 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
395 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE arm_preferred_simd_mode
396
397 #undef  TARGET_MACHINE_DEPENDENT_REORG
398 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
399
400 #undef  TARGET_INIT_BUILTINS
401 #define TARGET_INIT_BUILTINS  arm_init_builtins
402 #undef  TARGET_EXPAND_BUILTIN
403 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
404
405 #undef TARGET_INIT_LIBFUNCS
406 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
407
408 #undef TARGET_PROMOTE_FUNCTION_MODE
409 #define TARGET_PROMOTE_FUNCTION_MODE arm_promote_function_mode
410 #undef TARGET_PROMOTE_PROTOTYPES
411 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
412 #undef TARGET_PASS_BY_REFERENCE
413 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
414 #undef TARGET_ARG_PARTIAL_BYTES
415 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
416 #undef TARGET_FUNCTION_ARG
417 #define TARGET_FUNCTION_ARG arm_function_arg
418 #undef TARGET_FUNCTION_ARG_ADVANCE
419 #define TARGET_FUNCTION_ARG_ADVANCE arm_function_arg_advance
420 #undef TARGET_FUNCTION_ARG_BOUNDARY
421 #define TARGET_FUNCTION_ARG_BOUNDARY arm_function_arg_boundary
422
423 #undef  TARGET_SETUP_INCOMING_VARARGS
424 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
425
426 #undef TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS
427 #define TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS arm_allocate_stack_slots_for_args
428
429 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
430 #define TARGET_ASM_TRAMPOLINE_TEMPLATE arm_asm_trampoline_template
431 #undef TARGET_TRAMPOLINE_INIT
432 #define TARGET_TRAMPOLINE_INIT arm_trampoline_init
433 #undef TARGET_TRAMPOLINE_ADJUST_ADDRESS
434 #define TARGET_TRAMPOLINE_ADJUST_ADDRESS arm_trampoline_adjust_address
435
436 #undef TARGET_DEFAULT_SHORT_ENUMS
437 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
438
439 #undef TARGET_ALIGN_ANON_BITFIELD
440 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
441
442 #undef TARGET_NARROW_VOLATILE_BITFIELD
443 #define TARGET_NARROW_VOLATILE_BITFIELD hook_bool_void_false
444
445 #undef TARGET_CXX_GUARD_TYPE
446 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
447
448 #undef TARGET_CXX_GUARD_MASK_BIT
449 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
450
451 #undef TARGET_CXX_GET_COOKIE_SIZE
452 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
453
454 #undef TARGET_CXX_COOKIE_HAS_SIZE
455 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
456
457 #undef TARGET_CXX_CDTOR_RETURNS_THIS
458 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
459
460 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
461 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
462
463 #undef TARGET_CXX_USE_AEABI_ATEXIT
464 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
465
466 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
467 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
468   arm_cxx_determine_class_data_visibility
469
470 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
471 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
472
473 #undef TARGET_RETURN_IN_MSB
474 #define TARGET_RETURN_IN_MSB arm_return_in_msb
475
476 #undef TARGET_RETURN_IN_MEMORY
477 #define TARGET_RETURN_IN_MEMORY arm_return_in_memory
478
479 #undef TARGET_MUST_PASS_IN_STACK
480 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
481
482 #if ARM_UNWIND_INFO
483 #undef TARGET_ASM_UNWIND_EMIT
484 #define TARGET_ASM_UNWIND_EMIT arm_unwind_emit
485
486 /* EABI unwinding tables use a different format for the typeinfo tables.  */
487 #undef TARGET_ASM_TTYPE
488 #define TARGET_ASM_TTYPE arm_output_ttype
489
490 #undef TARGET_ARM_EABI_UNWINDER
491 #define TARGET_ARM_EABI_UNWINDER true
492
493 #undef TARGET_ASM_EMIT_EXCEPT_PERSONALITY
494 #define TARGET_ASM_EMIT_EXCEPT_PERSONALITY arm_asm_emit_except_personality
495
496 #undef TARGET_ASM_INIT_SECTIONS
497 #define TARGET_ASM_INIT_SECTIONS arm_asm_init_sections
498 #endif /* ARM_UNWIND_INFO */
499
500 #undef TARGET_EXCEPT_UNWIND_INFO
501 #define TARGET_EXCEPT_UNWIND_INFO  arm_except_unwind_info
502
503 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
504 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC arm_dwarf_handle_frame_unspec
505
506 #undef TARGET_DWARF_REGISTER_SPAN
507 #define TARGET_DWARF_REGISTER_SPAN arm_dwarf_register_span
508
509 #undef  TARGET_CANNOT_COPY_INSN_P
510 #define TARGET_CANNOT_COPY_INSN_P arm_cannot_copy_insn_p
511
512 #ifdef HAVE_AS_TLS
513 #undef TARGET_HAVE_TLS
514 #define TARGET_HAVE_TLS true
515 #endif
516
517 #undef TARGET_HAVE_CONDITIONAL_EXECUTION
518 #define TARGET_HAVE_CONDITIONAL_EXECUTION arm_have_conditional_execution
519
520 #undef TARGET_CANNOT_FORCE_CONST_MEM
521 #define TARGET_CANNOT_FORCE_CONST_MEM arm_cannot_force_const_mem
522
523 #undef TARGET_MAX_ANCHOR_OFFSET
524 #define TARGET_MAX_ANCHOR_OFFSET 4095
525
526 /* The minimum is set such that the total size of the block
527    for a particular anchor is -4088 + 1 + 4095 bytes, which is
528    divisible by eight, ensuring natural spacing of anchors.  */
529 #undef TARGET_MIN_ANCHOR_OFFSET
530 #define TARGET_MIN_ANCHOR_OFFSET -4088
531
532 #undef TARGET_SCHED_ISSUE_RATE
533 #define TARGET_SCHED_ISSUE_RATE arm_issue_rate
534
535 #undef TARGET_MANGLE_TYPE
536 #define TARGET_MANGLE_TYPE arm_mangle_type
537
538 #undef TARGET_BUILD_BUILTIN_VA_LIST
539 #define TARGET_BUILD_BUILTIN_VA_LIST arm_build_builtin_va_list
540 #undef TARGET_EXPAND_BUILTIN_VA_START
541 #define TARGET_EXPAND_BUILTIN_VA_START arm_expand_builtin_va_start
542 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
543 #define TARGET_GIMPLIFY_VA_ARG_EXPR arm_gimplify_va_arg_expr
544
545 #ifdef HAVE_AS_TLS
546 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
547 #define TARGET_ASM_OUTPUT_DWARF_DTPREL arm_output_dwarf_dtprel
548 #endif
549
550 #undef TARGET_LEGITIMATE_ADDRESS_P
551 #define TARGET_LEGITIMATE_ADDRESS_P     arm_legitimate_address_p
552
553 #undef TARGET_INVALID_PARAMETER_TYPE
554 #define TARGET_INVALID_PARAMETER_TYPE arm_invalid_parameter_type
555
556 #undef TARGET_INVALID_RETURN_TYPE
557 #define TARGET_INVALID_RETURN_TYPE arm_invalid_return_type
558
559 #undef TARGET_PROMOTED_TYPE
560 #define TARGET_PROMOTED_TYPE arm_promoted_type
561
562 #undef TARGET_CONVERT_TO_TYPE
563 #define TARGET_CONVERT_TO_TYPE arm_convert_to_type
564
565 #undef TARGET_SCALAR_MODE_SUPPORTED_P
566 #define TARGET_SCALAR_MODE_SUPPORTED_P arm_scalar_mode_supported_p
567
568 #undef TARGET_FRAME_POINTER_REQUIRED
569 #define TARGET_FRAME_POINTER_REQUIRED arm_frame_pointer_required
570
571 #undef TARGET_CAN_ELIMINATE
572 #define TARGET_CAN_ELIMINATE arm_can_eliminate
573
574 #undef TARGET_CONDITIONAL_REGISTER_USAGE
575 #define TARGET_CONDITIONAL_REGISTER_USAGE arm_conditional_register_usage
576
577 #undef TARGET_CLASS_LIKELY_SPILLED_P
578 #define TARGET_CLASS_LIKELY_SPILLED_P arm_class_likely_spilled_p
579
580 #undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
581 #define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE \
582   arm_vector_alignment_reachable
583
584 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
585 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT \
586   arm_builtin_support_vector_misalignment
587
588 struct gcc_target targetm = TARGET_INITIALIZER;
589 \f
590 /* Obstack for minipool constant handling.  */
591 static struct obstack minipool_obstack;
592 static char *         minipool_startobj;
593
594 /* The maximum number of insns skipped which
595    will be conditionalised if possible.  */
596 static int max_insns_skipped = 5;
597
598 extern FILE * asm_out_file;
599
600 /* True if we are currently building a constant table.  */
601 int making_const_table;
602
603 /* The processor for which instructions should be scheduled.  */
604 enum processor_type arm_tune = arm_none;
605
606 /* The current tuning set.  */
607 const struct tune_params *current_tune;
608
609 /* Which floating point hardware to schedule for.  */
610 int arm_fpu_attr;
611
612 /* Which floating popint hardware to use.  */
613 const struct arm_fpu_desc *arm_fpu_desc;
614
615 /* Whether to use floating point hardware.  */
616 enum float_abi_type arm_float_abi;
617
618 /* Which __fp16 format to use.  */
619 enum arm_fp16_format_type arm_fp16_format;
620
621 /* Which ABI to use.  */
622 enum arm_abi_type arm_abi;
623
624 /* Which thread pointer model to use.  */
625 enum arm_tp_type target_thread_pointer = TP_AUTO;
626
627 /* Used to parse -mstructure_size_boundary command line option.  */
628 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
629
630 /* Used for Thumb call_via trampolines.  */
631 rtx thumb_call_via_label[14];
632 static int thumb_call_reg_needed;
633
634 /* Bit values used to identify processor capabilities.  */
635 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
636 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
637 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
638 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
639 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
640 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
641 #define FL_THUMB      (1 << 6)        /* Thumb aware */
642 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
643 #define FL_STRONG     (1 << 8)        /* StrongARM */
644 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
645 #define FL_XSCALE     (1 << 10)       /* XScale */
646 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
647 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
648                                          media instructions.  */
649 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
650 #define FL_WBUF       (1 << 14)       /* Schedule for write buffer ops.
651                                          Note: ARM6 & 7 derivatives only.  */
652 #define FL_ARCH6K     (1 << 15)       /* Architecture rel 6 K extensions.  */
653 #define FL_THUMB2     (1 << 16)       /* Thumb-2.  */
654 #define FL_NOTM       (1 << 17)       /* Instructions not present in the 'M'
655                                          profile.  */
656 #define FL_DIV        (1 << 18)       /* Hardware divide.  */
657 #define FL_VFPV3      (1 << 19)       /* Vector Floating Point V3.  */
658 #define FL_NEON       (1 << 20)       /* Neon instructions.  */
659 #define FL_ARCH7EM    (1 << 21)       /* Instructions present in the ARMv7E-M
660                                          architecture.  */
661 #define FL_ARCH7      (1 << 22)       /* Architecture 7.  */
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_DIV)
689 #define FL_FOR_ARCH7M   (FL_FOR_ARCH7 | FL_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_hwdiv;
776
777 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference,
778    we must report the mode of the memory reference from
779    TARGET_PRINT_OPERAND to TARGET_PRINT_OPERAND_ADDRESS.  */
780 enum machine_mode output_memory_reference_mode;
781
782 /* The register number to be used for the PIC offset register.  */
783 unsigned arm_pic_register = INVALID_REGNUM;
784
785 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
786    the next function.  */
787 static int after_arm_reorg = 0;
788
789 enum arm_pcs arm_pcs_default;
790
791 /* For an explanation of these variables, see final_prescan_insn below.  */
792 int arm_ccfsm_state;
793 /* arm_current_cc is also used for Thumb-2 cond_exec blocks.  */
794 enum arm_cond_code arm_current_cc;
795
796 rtx arm_target_insn;
797 int arm_target_label;
798 /* The number of conditionally executed insns, including the current insn.  */
799 int arm_condexec_count = 0;
800 /* A bitmask specifying the patterns for the IT block.
801    Zero means do not output an IT block before this insn. */
802 int arm_condexec_mask = 0;
803 /* The number of bits used in arm_condexec_mask.  */
804 int arm_condexec_masklen = 0;
805
806 /* The condition codes of the ARM, and the inverse function.  */
807 static const char * const arm_condition_codes[] =
808 {
809   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
810   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
811 };
812
813 /* The register numbers in sequence, for passing to arm_gen_load_multiple.  */
814 int arm_regs_in_sequence[] =
815 {
816   0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
817 };
818
819 #define ARM_LSL_NAME (TARGET_UNIFIED_ASM ? "lsl" : "asl")
820 #define streq(string1, string2) (strcmp (string1, string2) == 0)
821
822 #define THUMB2_WORK_REGS (0xff & ~(  (1 << THUMB_HARD_FRAME_POINTER_REGNUM) \
823                                    | (1 << SP_REGNUM) | (1 << PC_REGNUM) \
824                                    | (1 << PIC_OFFSET_TABLE_REGNUM)))
825 \f
826 /* Initialization code.  */
827
828 struct processors
829 {
830   const char *const name;
831   enum processor_type core;
832   const char *arch;
833   const unsigned long flags;
834   const struct tune_params *const tune;
835 };
836
837
838 #define ARM_PREFETCH_NOT_BENEFICIAL 0, -1, -1
839 #define ARM_PREFETCH_BENEFICIAL(prefetch_slots,l1_size,l1_line_size) \
840   prefetch_slots, \
841   l1_size, \
842   l1_line_size
843
844 const struct tune_params arm_slowmul_tune =
845 {
846   arm_slowmul_rtx_costs,
847   NULL,
848   3,
849   ARM_PREFETCH_NOT_BENEFICIAL
850 };
851
852 const struct tune_params arm_fastmul_tune =
853 {
854   arm_fastmul_rtx_costs,
855   NULL,
856   1,
857   ARM_PREFETCH_NOT_BENEFICIAL
858 };
859
860 const struct tune_params arm_xscale_tune =
861 {
862   arm_xscale_rtx_costs,
863   xscale_sched_adjust_cost,
864   2,
865   ARM_PREFETCH_NOT_BENEFICIAL
866 };
867
868 const struct tune_params arm_9e_tune =
869 {
870   arm_9e_rtx_costs,
871   NULL,
872   1,
873   ARM_PREFETCH_NOT_BENEFICIAL
874 };
875
876 const struct tune_params arm_cortex_a9_tune =
877 {
878   arm_9e_rtx_costs,
879   cortex_a9_sched_adjust_cost,
880   1,
881   ARM_PREFETCH_BENEFICIAL(4,32,32)
882 };
883
884
885 /* Not all of these give usefully different compilation alternatives,
886    but there is no simple way of generalizing them.  */
887 static const struct processors all_cores[] =
888 {
889   /* ARM Cores */
890 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
891   {NAME, IDENT, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, &arm_##COSTS##_tune},
892 #include "arm-cores.def"
893 #undef ARM_CORE
894   {NULL, arm_none, NULL, 0, NULL}
895 };
896
897 static const struct processors all_architectures[] =
898 {
899   /* ARM Architectures */
900   /* We don't specify tuning costs here as it will be figured out
901      from the core.  */
902
903   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
904   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
905   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
906   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
907   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
908   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
909      implementations that support it, so we will leave it out for now.  */
910   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
911   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
912   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
913   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
914   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
915   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
916   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
917   {"armv6k",  mpcore,     "6K",  FL_CO_PROC |             FL_FOR_ARCH6K, NULL},
918   {"armv6z",  arm1176jzs, "6Z",  FL_CO_PROC |             FL_FOR_ARCH6Z, NULL},
919   {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC |             FL_FOR_ARCH6ZK, NULL},
920   {"armv6t2", arm1156t2s, "6T2", FL_CO_PROC |             FL_FOR_ARCH6T2, NULL},
921   {"armv6-m", cortexm1,   "6M",                           FL_FOR_ARCH6M, NULL},
922   {"armv7",   cortexa8,   "7",   FL_CO_PROC |             FL_FOR_ARCH7, NULL},
923   {"armv7-a", cortexa8,   "7A",  FL_CO_PROC |             FL_FOR_ARCH7A, NULL},
924   {"armv7-r", cortexr4,   "7R",  FL_CO_PROC |             FL_FOR_ARCH7R, NULL},
925   {"armv7-m", cortexm3,   "7M",  FL_CO_PROC |             FL_FOR_ARCH7M, NULL},
926   {"armv7e-m", cortexm4,  "7EM", FL_CO_PROC |             FL_FOR_ARCH7EM, NULL},
927   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
928   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
929   {"iwmmxt2", iwmmxt2,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
930   {NULL, arm_none, NULL, 0 , NULL}
931 };
932
933
934 /* These are populated as commandline arguments are processed, or NULL
935    if not specified.  */
936 static const struct processors *arm_selected_arch;
937 static const struct processors *arm_selected_cpu;
938 static const struct processors *arm_selected_tune;
939
940 /* The name of the preprocessor macro to define for this architecture.  */
941
942 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
943
944 /* Available values for -mfpu=.  */
945
946 static const struct arm_fpu_desc all_fpus[] =
947 {
948   {"fpa",               ARM_FP_MODEL_FPA, 0, VFP_NONE, false, false},
949   {"fpe2",              ARM_FP_MODEL_FPA, 2, VFP_NONE, false, false},
950   {"fpe3",              ARM_FP_MODEL_FPA, 3, VFP_NONE, false, false},
951   {"maverick",          ARM_FP_MODEL_MAVERICK, 0, VFP_NONE, false, false},
952   {"vfp",               ARM_FP_MODEL_VFP, 2, VFP_REG_D16, false, false},
953   {"vfpv3",             ARM_FP_MODEL_VFP, 3, VFP_REG_D32, false, false},
954   {"vfpv3-fp16",        ARM_FP_MODEL_VFP, 3, VFP_REG_D32, false, true},
955   {"vfpv3-d16",         ARM_FP_MODEL_VFP, 3, VFP_REG_D16, false, false},
956   {"vfpv3-d16-fp16",    ARM_FP_MODEL_VFP, 3, VFP_REG_D16, false, true},
957   {"vfpv3xd",           ARM_FP_MODEL_VFP, 3, VFP_REG_SINGLE, false, false},
958   {"vfpv3xd-fp16",      ARM_FP_MODEL_VFP, 3, VFP_REG_SINGLE, false, true},
959   {"neon",              ARM_FP_MODEL_VFP, 3, VFP_REG_D32, true , false},
960   {"neon-fp16",         ARM_FP_MODEL_VFP, 3, VFP_REG_D32, true , true },
961   {"vfpv4",             ARM_FP_MODEL_VFP, 4, VFP_REG_D32, false, true},
962   {"vfpv4-d16",         ARM_FP_MODEL_VFP, 4, VFP_REG_D16, false, true},
963   {"fpv4-sp-d16",       ARM_FP_MODEL_VFP, 4, VFP_REG_SINGLE, false, true},
964   {"neon-vfpv4",        ARM_FP_MODEL_VFP, 4, VFP_REG_D32, true, true},
965   /* Compatibility aliases.  */
966   {"vfp3",              ARM_FP_MODEL_VFP, 3, VFP_REG_D32, false, false},
967 };
968
969
970 struct float_abi
971 {
972   const char * name;
973   enum float_abi_type abi_type;
974 };
975
976
977 /* Available values for -mfloat-abi=.  */
978
979 static const struct float_abi all_float_abis[] =
980 {
981   {"soft",      ARM_FLOAT_ABI_SOFT},
982   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
983   {"hard",      ARM_FLOAT_ABI_HARD}
984 };
985
986
987 struct fp16_format
988 {
989   const char *name;
990   enum arm_fp16_format_type fp16_format_type;
991 };
992
993
994 /* Available values for -mfp16-format=.  */
995
996 static const struct fp16_format all_fp16_formats[] =
997 {
998   {"none",              ARM_FP16_FORMAT_NONE},
999   {"ieee",              ARM_FP16_FORMAT_IEEE},
1000   {"alternative",       ARM_FP16_FORMAT_ALTERNATIVE}
1001 };
1002
1003
1004 struct abi_name
1005 {
1006   const char *name;
1007   enum arm_abi_type abi_type;
1008 };
1009
1010
1011 /* Available values for -mabi=.  */
1012
1013 static const struct abi_name arm_all_abis[] =
1014 {
1015   {"apcs-gnu",    ARM_ABI_APCS},
1016   {"atpcs",   ARM_ABI_ATPCS},
1017   {"aapcs",   ARM_ABI_AAPCS},
1018   {"iwmmxt",  ARM_ABI_IWMMXT},
1019   {"aapcs-linux",   ARM_ABI_AAPCS_LINUX}
1020 };
1021
1022 /* Supported TLS relocations.  */
1023
1024 enum tls_reloc {
1025   TLS_GD32,
1026   TLS_LDM32,
1027   TLS_LDO32,
1028   TLS_IE32,
1029   TLS_LE32
1030 };
1031
1032 /* The maximum number of insns to be used when loading a constant.  */
1033 inline static int
1034 arm_constant_limit (bool size_p)
1035 {
1036   return size_p ? 1 : current_tune->constant_limit;
1037 }
1038
1039 /* Emit an insn that's a simple single-set.  Both the operands must be known
1040    to be valid.  */
1041 inline static rtx
1042 emit_set_insn (rtx x, rtx y)
1043 {
1044   return emit_insn (gen_rtx_SET (VOIDmode, x, y));
1045 }
1046
1047 /* Return the number of bits set in VALUE.  */
1048 static unsigned
1049 bit_count (unsigned long value)
1050 {
1051   unsigned long count = 0;
1052
1053   while (value)
1054     {
1055       count++;
1056       value &= value - 1;  /* Clear the least-significant set bit.  */
1057     }
1058
1059   return count;
1060 }
1061
1062 /* Set up library functions unique to ARM.  */
1063
1064 static void
1065 arm_init_libfuncs (void)
1066 {
1067   /* There are no special library functions unless we are using the
1068      ARM BPABI.  */
1069   if (!TARGET_BPABI)
1070     return;
1071
1072   /* The functions below are described in Section 4 of the "Run-Time
1073      ABI for the ARM architecture", Version 1.0.  */
1074
1075   /* Double-precision floating-point arithmetic.  Table 2.  */
1076   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
1077   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
1078   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
1079   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
1080   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
1081
1082   /* Double-precision comparisons.  Table 3.  */
1083   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
1084   set_optab_libfunc (ne_optab, DFmode, NULL);
1085   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
1086   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
1087   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
1088   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
1089   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
1090
1091   /* Single-precision floating-point arithmetic.  Table 4.  */
1092   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
1093   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
1094   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
1095   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
1096   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
1097
1098   /* Single-precision comparisons.  Table 5.  */
1099   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
1100   set_optab_libfunc (ne_optab, SFmode, NULL);
1101   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
1102   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
1103   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
1104   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
1105   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
1106
1107   /* Floating-point to integer conversions.  Table 6.  */
1108   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
1109   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
1110   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
1111   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
1112   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
1113   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
1114   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
1115   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
1116
1117   /* Conversions between floating types.  Table 7.  */
1118   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
1119   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
1120
1121   /* Integer to floating-point conversions.  Table 8.  */
1122   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
1123   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
1124   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
1125   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
1126   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
1127   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
1128   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
1129   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
1130
1131   /* Long long.  Table 9.  */
1132   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
1133   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
1134   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
1135   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
1136   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
1137   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
1138   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
1139   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
1140
1141   /* Integer (32/32->32) division.  \S 4.3.1.  */
1142   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
1143   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
1144
1145   /* The divmod functions are designed so that they can be used for
1146      plain division, even though they return both the quotient and the
1147      remainder.  The quotient is returned in the usual location (i.e.,
1148      r0 for SImode, {r0, r1} for DImode), just as would be expected
1149      for an ordinary division routine.  Because the AAPCS calling
1150      conventions specify that all of { r0, r1, r2, r3 } are
1151      callee-saved registers, there is no need to tell the compiler
1152      explicitly that those registers are clobbered by these
1153      routines.  */
1154   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
1155   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
1156
1157   /* For SImode division the ABI provides div-without-mod routines,
1158      which are faster.  */
1159   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
1160   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
1161
1162   /* We don't have mod libcalls.  Fortunately gcc knows how to use the
1163      divmod libcalls instead.  */
1164   set_optab_libfunc (smod_optab, DImode, NULL);
1165   set_optab_libfunc (umod_optab, DImode, NULL);
1166   set_optab_libfunc (smod_optab, SImode, NULL);
1167   set_optab_libfunc (umod_optab, SImode, NULL);
1168
1169   /* Half-precision float operations.  The compiler handles all operations
1170      with NULL libfuncs by converting the SFmode.  */
1171   switch (arm_fp16_format)
1172     {
1173     case ARM_FP16_FORMAT_IEEE:
1174     case ARM_FP16_FORMAT_ALTERNATIVE:
1175
1176       /* Conversions.  */
1177       set_conv_libfunc (trunc_optab, HFmode, SFmode,
1178                         (arm_fp16_format == ARM_FP16_FORMAT_IEEE
1179                          ? "__gnu_f2h_ieee"
1180                          : "__gnu_f2h_alternative"));
1181       set_conv_libfunc (sext_optab, SFmode, HFmode, 
1182                         (arm_fp16_format == ARM_FP16_FORMAT_IEEE
1183                          ? "__gnu_h2f_ieee"
1184                          : "__gnu_h2f_alternative"));
1185       
1186       /* Arithmetic.  */
1187       set_optab_libfunc (add_optab, HFmode, NULL);
1188       set_optab_libfunc (sdiv_optab, HFmode, NULL);
1189       set_optab_libfunc (smul_optab, HFmode, NULL);
1190       set_optab_libfunc (neg_optab, HFmode, NULL);
1191       set_optab_libfunc (sub_optab, HFmode, NULL);
1192
1193       /* Comparisons.  */
1194       set_optab_libfunc (eq_optab, HFmode, NULL);
1195       set_optab_libfunc (ne_optab, HFmode, NULL);
1196       set_optab_libfunc (lt_optab, HFmode, NULL);
1197       set_optab_libfunc (le_optab, HFmode, NULL);
1198       set_optab_libfunc (ge_optab, HFmode, NULL);
1199       set_optab_libfunc (gt_optab, HFmode, NULL);
1200       set_optab_libfunc (unord_optab, HFmode, NULL);
1201       break;
1202
1203     default:
1204       break;
1205     }
1206
1207   if (TARGET_AAPCS_BASED)
1208     synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
1209 }
1210
1211 /* On AAPCS systems, this is the "struct __va_list".  */
1212 static GTY(()) tree va_list_type;
1213
1214 /* Return the type to use as __builtin_va_list.  */
1215 static tree
1216 arm_build_builtin_va_list (void)
1217 {
1218   tree va_list_name;
1219   tree ap_field;
1220   
1221   if (!TARGET_AAPCS_BASED)
1222     return std_build_builtin_va_list ();
1223
1224   /* AAPCS \S 7.1.4 requires that va_list be a typedef for a type
1225      defined as:
1226
1227        struct __va_list 
1228        {
1229          void *__ap;
1230        };
1231
1232      The C Library ABI further reinforces this definition in \S
1233      4.1.
1234
1235      We must follow this definition exactly.  The structure tag
1236      name is visible in C++ mangled names, and thus forms a part
1237      of the ABI.  The field name may be used by people who
1238      #include <stdarg.h>.  */
1239   /* Create the type.  */
1240   va_list_type = lang_hooks.types.make_type (RECORD_TYPE);
1241   /* Give it the required name.  */
1242   va_list_name = build_decl (BUILTINS_LOCATION,
1243                              TYPE_DECL,
1244                              get_identifier ("__va_list"),
1245                              va_list_type);
1246   DECL_ARTIFICIAL (va_list_name) = 1;
1247   TYPE_NAME (va_list_type) = va_list_name;
1248   TYPE_STUB_DECL (va_list_type) = va_list_name;
1249   /* Create the __ap field.  */
1250   ap_field = build_decl (BUILTINS_LOCATION,
1251                          FIELD_DECL, 
1252                          get_identifier ("__ap"),
1253                          ptr_type_node);
1254   DECL_ARTIFICIAL (ap_field) = 1;
1255   DECL_FIELD_CONTEXT (ap_field) = va_list_type;
1256   TYPE_FIELDS (va_list_type) = ap_field;
1257   /* Compute its layout.  */
1258   layout_type (va_list_type);
1259
1260   return va_list_type;
1261 }
1262
1263 /* Return an expression of type "void *" pointing to the next
1264    available argument in a variable-argument list.  VALIST is the
1265    user-level va_list object, of type __builtin_va_list.  */
1266 static tree
1267 arm_extract_valist_ptr (tree valist)
1268 {
1269   if (TREE_TYPE (valist) == error_mark_node)
1270     return error_mark_node;
1271
1272   /* On an AAPCS target, the pointer is stored within "struct
1273      va_list".  */
1274   if (TARGET_AAPCS_BASED)
1275     {
1276       tree ap_field = TYPE_FIELDS (TREE_TYPE (valist));
1277       valist = build3 (COMPONENT_REF, TREE_TYPE (ap_field), 
1278                        valist, ap_field, NULL_TREE);
1279     }
1280
1281   return valist;
1282 }
1283
1284 /* Implement TARGET_EXPAND_BUILTIN_VA_START.  */
1285 static void
1286 arm_expand_builtin_va_start (tree valist, rtx nextarg)
1287 {
1288   valist = arm_extract_valist_ptr (valist);
1289   std_expand_builtin_va_start (valist, nextarg);
1290 }
1291
1292 /* Implement TARGET_GIMPLIFY_VA_ARG_EXPR.  */
1293 static tree
1294 arm_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p, 
1295                           gimple_seq *post_p)
1296 {
1297   valist = arm_extract_valist_ptr (valist);
1298   return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
1299 }
1300
1301 /* Lookup NAME in SEL.  */
1302
1303 static const struct processors *
1304 arm_find_cpu (const char *name, const struct processors *sel, const char *desc)
1305 {
1306   if (!(name && *name))
1307     return NULL;
1308
1309   for (; sel->name != NULL; sel++)
1310     {
1311       if (streq (name, sel->name))
1312         return sel;
1313     }
1314
1315   error ("bad value (%s) for %s switch", name, desc);
1316   return NULL;
1317 }
1318
1319 /* Implement TARGET_HANDLE_OPTION.  */
1320
1321 static bool
1322 arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
1323 {
1324   switch (code)
1325     {
1326     case OPT_march_:
1327       arm_selected_arch = arm_find_cpu(arg, all_architectures, "-march");
1328       return true;
1329
1330     case OPT_mcpu_:
1331       arm_selected_cpu = arm_find_cpu(arg, all_cores, "-mcpu");
1332       return true;
1333
1334     case OPT_mhard_float:
1335       target_float_abi_name = "hard";
1336       return true;
1337
1338     case OPT_msoft_float:
1339       target_float_abi_name = "soft";
1340       return true;
1341
1342     case OPT_mtune_:
1343       arm_selected_tune = arm_find_cpu(arg, all_cores, "-mtune");
1344       return true;
1345
1346     default:
1347       return true;
1348     }
1349 }
1350
1351 static void
1352 arm_target_help (void)
1353 {
1354   int i;
1355   static int columns = 0;
1356   int remaining;
1357
1358   /* If we have not done so already, obtain the desired maximum width of
1359      the output.  Note - this is a duplication of the code at the start of
1360      gcc/opts.c:print_specific_help() - the two copies should probably be
1361      replaced by a single function.  */
1362   if (columns == 0)
1363     {
1364       const char *p;
1365
1366       p = getenv ("COLUMNS");
1367       if (p != NULL)
1368         {
1369           int value = atoi (p);
1370
1371           if (value > 0)
1372             columns = value;
1373         }
1374
1375       if (columns == 0)
1376         /* Use a reasonable default.  */
1377         columns = 80;
1378     }
1379
1380   printf ("  Known ARM CPUs (for use with the -mcpu= and -mtune= options):\n");
1381
1382   /* The - 2 is because we know that the last entry in the array is NULL.  */
1383   i = ARRAY_SIZE (all_cores) - 2;
1384   gcc_assert (i > 0);
1385   printf ("    %s", all_cores[i].name);
1386   remaining = columns - (strlen (all_cores[i].name) + 4);
1387   gcc_assert (remaining >= 0);
1388
1389   while (i--)
1390     {
1391       int len = strlen (all_cores[i].name);
1392
1393       if (remaining > len + 2)
1394         {
1395           printf (", %s", all_cores[i].name);
1396           remaining -= len + 2;
1397         }
1398       else
1399         {
1400           if (remaining > 0)
1401             printf (",");
1402           printf ("\n    %s", all_cores[i].name);
1403           remaining = columns - (len + 4);
1404         }
1405     }
1406
1407   printf ("\n\n  Known ARM architectures (for use with the -march= option):\n");
1408
1409   i = ARRAY_SIZE (all_architectures) - 2;
1410   gcc_assert (i > 0);
1411   
1412   printf ("    %s", all_architectures[i].name);
1413   remaining = columns - (strlen (all_architectures[i].name) + 4);
1414   gcc_assert (remaining >= 0);
1415
1416   while (i--)
1417     {
1418       int len = strlen (all_architectures[i].name);
1419
1420       if (remaining > len + 2)
1421         {
1422           printf (", %s", all_architectures[i].name);
1423           remaining -= len + 2;
1424         }
1425       else
1426         {
1427           if (remaining > 0)
1428             printf (",");
1429           printf ("\n    %s", all_architectures[i].name);
1430           remaining = columns - (len + 4);
1431         }
1432     }
1433   printf ("\n");
1434
1435 }
1436
1437 /* Fix up any incompatible options that the user has specified.  */
1438 static void
1439 arm_option_override (void)
1440 {
1441   unsigned i;
1442
1443 #ifdef SUBTARGET_OVERRIDE_OPTIONS
1444   SUBTARGET_OVERRIDE_OPTIONS;
1445 #endif
1446
1447   if (arm_selected_arch)
1448     {
1449       if (arm_selected_cpu)
1450         {
1451           /* Check for conflict between mcpu and march.  */
1452           if ((arm_selected_cpu->flags ^ arm_selected_arch->flags) & ~FL_TUNE)
1453             {
1454               warning (0, "switch -mcpu=%s conflicts with -march=%s switch",
1455                        arm_selected_cpu->name, arm_selected_arch->name);
1456               /* -march wins for code generation.
1457                  -mcpu wins for default tuning.  */
1458               if (!arm_selected_tune)
1459                 arm_selected_tune = arm_selected_cpu;
1460
1461               arm_selected_cpu = arm_selected_arch;
1462             }
1463           else
1464             /* -mcpu wins.  */
1465             arm_selected_arch = NULL;
1466         }
1467       else
1468         /* Pick a CPU based on the architecture.  */
1469         arm_selected_cpu = arm_selected_arch;
1470     }
1471
1472   /* If the user did not specify a processor, choose one for them.  */
1473   if (!arm_selected_cpu)
1474     {
1475       const struct processors * sel;
1476       unsigned int        sought;
1477
1478       arm_selected_cpu = &all_cores[TARGET_CPU_DEFAULT];
1479       if (!arm_selected_cpu->name)
1480         {
1481 #ifdef SUBTARGET_CPU_DEFAULT
1482           /* Use the subtarget default CPU if none was specified by
1483              configure.  */
1484           arm_selected_cpu = &all_cores[SUBTARGET_CPU_DEFAULT];
1485 #endif
1486           /* Default to ARM6.  */
1487           if (!arm_selected_cpu->name)
1488             arm_selected_cpu = &all_cores[arm6];
1489         }
1490
1491       sel = arm_selected_cpu;
1492       insn_flags = sel->flags;
1493
1494       /* Now check to see if the user has specified some command line
1495          switch that require certain abilities from the cpu.  */
1496       sought = 0;
1497
1498       if (TARGET_INTERWORK || TARGET_THUMB)
1499         {
1500           sought |= (FL_THUMB | FL_MODE32);
1501
1502           /* There are no ARM processors that support both APCS-26 and
1503              interworking.  Therefore we force FL_MODE26 to be removed
1504              from insn_flags here (if it was set), so that the search
1505              below will always be able to find a compatible processor.  */
1506           insn_flags &= ~FL_MODE26;
1507         }
1508
1509       if (sought != 0 && ((sought & insn_flags) != sought))
1510         {
1511           /* Try to locate a CPU type that supports all of the abilities
1512              of the default CPU, plus the extra abilities requested by
1513              the user.  */
1514           for (sel = all_cores; sel->name != NULL; sel++)
1515             if ((sel->flags & sought) == (sought | insn_flags))
1516               break;
1517
1518           if (sel->name == NULL)
1519             {
1520               unsigned current_bit_count = 0;
1521               const struct processors * best_fit = NULL;
1522
1523               /* Ideally we would like to issue an error message here
1524                  saying that it was not possible to find a CPU compatible
1525                  with the default CPU, but which also supports the command
1526                  line options specified by the programmer, and so they
1527                  ought to use the -mcpu=<name> command line option to
1528                  override the default CPU type.
1529
1530                  If we cannot find a cpu that has both the
1531                  characteristics of the default cpu and the given
1532                  command line options we scan the array again looking
1533                  for a best match.  */
1534               for (sel = all_cores; sel->name != NULL; sel++)
1535                 if ((sel->flags & sought) == sought)
1536                   {
1537                     unsigned count;
1538
1539                     count = bit_count (sel->flags & insn_flags);
1540
1541                     if (count >= current_bit_count)
1542                       {
1543                         best_fit = sel;
1544                         current_bit_count = count;
1545                       }
1546                   }
1547
1548               gcc_assert (best_fit);
1549               sel = best_fit;
1550             }
1551
1552           arm_selected_cpu = sel;
1553         }
1554     }
1555
1556   gcc_assert (arm_selected_cpu);
1557   /* The selected cpu may be an architecture, so lookup tuning by core ID.  */
1558   if (!arm_selected_tune)
1559     arm_selected_tune = &all_cores[arm_selected_cpu->core];
1560
1561   sprintf (arm_arch_name, "__ARM_ARCH_%s__", arm_selected_cpu->arch);
1562   insn_flags = arm_selected_cpu->flags;
1563
1564   arm_tune = arm_selected_tune->core;
1565   tune_flags = arm_selected_tune->flags;
1566   current_tune = arm_selected_tune->tune;
1567
1568   if (target_fp16_format_name)
1569     {
1570       for (i = 0; i < ARRAY_SIZE (all_fp16_formats); i++)
1571         {
1572           if (streq (all_fp16_formats[i].name, target_fp16_format_name))
1573             {
1574               arm_fp16_format = all_fp16_formats[i].fp16_format_type;
1575               break;
1576             }
1577         }
1578       if (i == ARRAY_SIZE (all_fp16_formats))
1579         error ("invalid __fp16 format option: -mfp16-format=%s",
1580                target_fp16_format_name);
1581     }
1582   else
1583     arm_fp16_format = ARM_FP16_FORMAT_NONE;
1584
1585   if (target_abi_name)
1586     {
1587       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1588         {
1589           if (streq (arm_all_abis[i].name, target_abi_name))
1590             {
1591               arm_abi = arm_all_abis[i].abi_type;
1592               break;
1593             }
1594         }
1595       if (i == ARRAY_SIZE (arm_all_abis))
1596         error ("invalid ABI option: -mabi=%s", target_abi_name);
1597     }
1598   else
1599     arm_abi = ARM_DEFAULT_ABI;
1600
1601   /* Make sure that the processor choice does not conflict with any of the
1602      other command line choices.  */
1603   if (TARGET_ARM && !(insn_flags & FL_NOTM))
1604     error ("target CPU does not support ARM mode");
1605
1606   /* BPABI targets use linker tricks to allow interworking on cores
1607      without thumb support.  */
1608   if (TARGET_INTERWORK && !((insn_flags & FL_THUMB) || TARGET_BPABI))
1609     {
1610       warning (0, "target CPU does not support interworking" );
1611       target_flags &= ~MASK_INTERWORK;
1612     }
1613
1614   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1615     {
1616       warning (0, "target CPU does not support THUMB instructions");
1617       target_flags &= ~MASK_THUMB;
1618     }
1619
1620   if (TARGET_APCS_FRAME && TARGET_THUMB)
1621     {
1622       /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1623       target_flags &= ~MASK_APCS_FRAME;
1624     }
1625
1626   /* Callee super interworking implies thumb interworking.  Adding
1627      this to the flags here simplifies the logic elsewhere.  */
1628   if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
1629     target_flags |= MASK_INTERWORK;
1630
1631   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1632      from here where no function is being compiled currently.  */
1633   if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1634     warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1635
1636   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1637     warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1638
1639   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1640     {
1641       warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1642       target_flags |= MASK_APCS_FRAME;
1643     }
1644
1645   if (TARGET_POKE_FUNCTION_NAME)
1646     target_flags |= MASK_APCS_FRAME;
1647
1648   if (TARGET_APCS_REENT && flag_pic)
1649     error ("-fpic and -mapcs-reent are incompatible");
1650
1651   if (TARGET_APCS_REENT)
1652     warning (0, "APCS reentrant code not supported.  Ignored");
1653
1654   /* If this target is normally configured to use APCS frames, warn if they
1655      are turned off and debugging is turned on.  */
1656   if (TARGET_ARM
1657       && write_symbols != NO_DEBUG
1658       && !TARGET_APCS_FRAME
1659       && (TARGET_DEFAULT & MASK_APCS_FRAME))
1660     warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1661
1662   if (TARGET_APCS_FLOAT)
1663     warning (0, "passing floating point arguments in fp regs not yet supported");
1664
1665   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
1666   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1667   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1668   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1669   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1670   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1671   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1672   arm_arch6k = (insn_flags & FL_ARCH6K) != 0;
1673   arm_arch_notm = (insn_flags & FL_NOTM) != 0;
1674   arm_arch7 = (insn_flags & FL_ARCH7) != 0;
1675   arm_arch7em = (insn_flags & FL_ARCH7EM) != 0;
1676   arm_arch_thumb2 = (insn_flags & FL_THUMB2) != 0;
1677   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1678   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1679
1680   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1681   arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1682   thumb_code = TARGET_ARM == 0;
1683   thumb1_code = TARGET_THUMB1 != 0;
1684   arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1685   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1686   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1687   arm_arch_hwdiv = (insn_flags & FL_DIV) != 0;
1688   arm_tune_cortex_a9 = (arm_tune == cortexa9) != 0;
1689
1690   /* If we are not using the default (ARM mode) section anchor offset
1691      ranges, then set the correct ranges now.  */
1692   if (TARGET_THUMB1)
1693     {
1694       /* Thumb-1 LDR instructions cannot have negative offsets.
1695          Permissible positive offset ranges are 5-bit (for byte loads),
1696          6-bit (for halfword loads), or 7-bit (for word loads).
1697          Empirical results suggest a 7-bit anchor range gives the best
1698          overall code size.  */
1699       targetm.min_anchor_offset = 0;
1700       targetm.max_anchor_offset = 127;
1701     }
1702   else if (TARGET_THUMB2)
1703     {
1704       /* The minimum is set such that the total size of the block
1705          for a particular anchor is 248 + 1 + 4095 bytes, which is
1706          divisible by eight, ensuring natural spacing of anchors.  */
1707       targetm.min_anchor_offset = -248;
1708       targetm.max_anchor_offset = 4095;
1709     }
1710
1711   /* V5 code we generate is completely interworking capable, so we turn off
1712      TARGET_INTERWORK here to avoid many tests later on.  */
1713
1714   /* XXX However, we must pass the right pre-processor defines to CPP
1715      or GLD can get confused.  This is a hack.  */
1716   if (TARGET_INTERWORK)
1717     arm_cpp_interwork = 1;
1718
1719   if (arm_arch5)
1720     target_flags &= ~MASK_INTERWORK;
1721
1722   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1723     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1724
1725   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1726     error ("iwmmxt abi requires an iwmmxt capable cpu");
1727
1728   if (target_fpu_name == NULL && target_fpe_name != NULL)
1729     {
1730       if (streq (target_fpe_name, "2"))
1731         target_fpu_name = "fpe2";
1732       else if (streq (target_fpe_name, "3"))
1733         target_fpu_name = "fpe3";
1734       else
1735         error ("invalid floating point emulation option: -mfpe=%s",
1736                target_fpe_name);
1737     }
1738
1739   if (target_fpu_name == NULL)
1740     {
1741 #ifdef FPUTYPE_DEFAULT
1742       target_fpu_name = FPUTYPE_DEFAULT;
1743 #else
1744       if (arm_arch_cirrus)
1745         target_fpu_name = "maverick";
1746       else
1747         target_fpu_name = "fpe2";
1748 #endif
1749     }
1750
1751   arm_fpu_desc = NULL;
1752   for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1753     {
1754       if (streq (all_fpus[i].name, target_fpu_name))
1755         {
1756           arm_fpu_desc = &all_fpus[i];
1757           break;
1758         }
1759     }
1760
1761   if (!arm_fpu_desc)
1762     {
1763       error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1764       return;
1765     }
1766
1767   switch (arm_fpu_desc->model)
1768     {
1769     case ARM_FP_MODEL_FPA:
1770       if (arm_fpu_desc->rev == 2)
1771         arm_fpu_attr = FPU_FPE2;
1772       else if (arm_fpu_desc->rev == 3)
1773         arm_fpu_attr = FPU_FPE3;
1774       else
1775         arm_fpu_attr = FPU_FPA;
1776       break;
1777
1778     case ARM_FP_MODEL_MAVERICK:
1779       arm_fpu_attr = FPU_MAVERICK;
1780       break;
1781
1782     case ARM_FP_MODEL_VFP:
1783       arm_fpu_attr = FPU_VFP;
1784       break;
1785
1786     default:
1787       gcc_unreachable();
1788     }
1789
1790   if (target_float_abi_name != NULL)
1791     {
1792       /* The user specified a FP ABI.  */
1793       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1794         {
1795           if (streq (all_float_abis[i].name, target_float_abi_name))
1796             {
1797               arm_float_abi = all_float_abis[i].abi_type;
1798               break;
1799             }
1800         }
1801       if (i == ARRAY_SIZE (all_float_abis))
1802         error ("invalid floating point abi: -mfloat-abi=%s",
1803                target_float_abi_name);
1804     }
1805   else
1806     arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1807
1808   if (TARGET_AAPCS_BASED
1809       && (arm_fpu_desc->model == ARM_FP_MODEL_FPA))
1810     error ("FPA is unsupported in the AAPCS");
1811
1812   if (TARGET_AAPCS_BASED)
1813     {
1814       if (TARGET_CALLER_INTERWORKING)
1815         error ("AAPCS does not support -mcaller-super-interworking");
1816       else
1817         if (TARGET_CALLEE_INTERWORKING)
1818           error ("AAPCS does not support -mcallee-super-interworking");
1819     }
1820
1821   /* FPA and iWMMXt are incompatible because the insn encodings overlap.
1822      VFP and iWMMXt can theoretically coexist, but it's unlikely such silicon
1823      will ever exist.  GCC makes no attempt to support this combination.  */
1824   if (TARGET_IWMMXT && !TARGET_SOFT_FLOAT)
1825     sorry ("iWMMXt and hardware floating point");
1826
1827   /* ??? iWMMXt insn patterns need auditing for Thumb-2.  */
1828   if (TARGET_THUMB2 && TARGET_IWMMXT)
1829     sorry ("Thumb-2 iWMMXt");
1830
1831   /* __fp16 support currently assumes the core has ldrh.  */
1832   if (!arm_arch4 && arm_fp16_format != ARM_FP16_FORMAT_NONE)
1833     sorry ("__fp16 and no ldrh");
1834
1835   /* If soft-float is specified then don't use FPU.  */
1836   if (TARGET_SOFT_FLOAT)
1837     arm_fpu_attr = FPU_NONE;
1838
1839   if (TARGET_AAPCS_BASED)
1840     {
1841       if (arm_abi == ARM_ABI_IWMMXT)
1842         arm_pcs_default = ARM_PCS_AAPCS_IWMMXT;
1843       else if (arm_float_abi == ARM_FLOAT_ABI_HARD
1844                && TARGET_HARD_FLOAT
1845                && TARGET_VFP)
1846         arm_pcs_default = ARM_PCS_AAPCS_VFP;
1847       else
1848         arm_pcs_default = ARM_PCS_AAPCS;
1849     }
1850   else
1851     {
1852       if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1853         sorry ("-mfloat-abi=hard and VFP");
1854
1855       if (arm_abi == ARM_ABI_APCS)
1856         arm_pcs_default = ARM_PCS_APCS;
1857       else
1858         arm_pcs_default = ARM_PCS_ATPCS;
1859     }
1860
1861   /* For arm2/3 there is no need to do any scheduling if there is only
1862      a floating point emulator, or we are doing software floating-point.  */
1863   if ((TARGET_SOFT_FLOAT
1864        || (TARGET_FPA && arm_fpu_desc->rev))
1865       && (tune_flags & FL_MODE32) == 0)
1866     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1867
1868   if (target_thread_switch)
1869     {
1870       if (strcmp (target_thread_switch, "soft") == 0)
1871         target_thread_pointer = TP_SOFT;
1872       else if (strcmp (target_thread_switch, "auto") == 0)
1873         target_thread_pointer = TP_AUTO;
1874       else if (strcmp (target_thread_switch, "cp15") == 0)
1875         target_thread_pointer = TP_CP15;
1876       else
1877         error ("invalid thread pointer option: -mtp=%s", target_thread_switch);
1878     }
1879
1880   /* Use the cp15 method if it is available.  */
1881   if (target_thread_pointer == TP_AUTO)
1882     {
1883       if (arm_arch6k && !TARGET_THUMB1)
1884         target_thread_pointer = TP_CP15;
1885       else
1886         target_thread_pointer = TP_SOFT;
1887     }
1888
1889   if (TARGET_HARD_TP && TARGET_THUMB1)
1890     error ("can not use -mtp=cp15 with 16-bit Thumb");
1891
1892   /* Override the default structure alignment for AAPCS ABI.  */
1893   if (TARGET_AAPCS_BASED)
1894     arm_structure_size_boundary = 8;
1895
1896   if (structure_size_string != NULL)
1897     {
1898       int size = strtol (structure_size_string, NULL, 0);
1899
1900       if (size == 8 || size == 32
1901           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1902         arm_structure_size_boundary = size;
1903       else
1904         warning (0, "structure size boundary can only be set to %s",
1905                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1906     }
1907
1908   if (!TARGET_ARM && TARGET_VXWORKS_RTP && flag_pic)
1909     {
1910       error ("RTP PIC is incompatible with Thumb");
1911       flag_pic = 0;
1912     }
1913
1914   /* If stack checking is disabled, we can use r10 as the PIC register,
1915      which keeps r9 available.  The EABI specifies r9 as the PIC register.  */
1916   if (flag_pic && TARGET_SINGLE_PIC_BASE)
1917     {
1918       if (TARGET_VXWORKS_RTP)
1919         warning (0, "RTP PIC is incompatible with -msingle-pic-base");
1920       arm_pic_register = (TARGET_APCS_STACK || TARGET_AAPCS_BASED) ? 9 : 10;
1921     }
1922
1923   if (flag_pic && TARGET_VXWORKS_RTP)
1924     arm_pic_register = 9;
1925
1926   if (arm_pic_register_string != NULL)
1927     {
1928       int pic_register = decode_reg_name (arm_pic_register_string);
1929
1930       if (!flag_pic)
1931         warning (0, "-mpic-register= is useless without -fpic");
1932
1933       /* Prevent the user from choosing an obviously stupid PIC register.  */
1934       else if (pic_register < 0 || call_used_regs[pic_register]
1935                || pic_register == HARD_FRAME_POINTER_REGNUM
1936                || pic_register == STACK_POINTER_REGNUM
1937                || pic_register >= PC_REGNUM
1938                || (TARGET_VXWORKS_RTP
1939                    && (unsigned int) pic_register != arm_pic_register))
1940         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1941       else
1942         arm_pic_register = pic_register;
1943     }
1944
1945   /* Enable -mfix-cortex-m3-ldrd by default for Cortex-M3 cores.  */
1946   if (fix_cm3_ldrd == 2)
1947     {
1948       if (arm_selected_cpu->core == cortexm3)
1949         fix_cm3_ldrd = 1;
1950       else
1951         fix_cm3_ldrd = 0;
1952     }
1953
1954   if (TARGET_THUMB1 && flag_schedule_insns)
1955     {
1956       /* Don't warn since it's on by default in -O2.  */
1957       flag_schedule_insns = 0;
1958     }
1959
1960   if (optimize_size)
1961     {
1962       /* If optimizing for size, bump the number of instructions that we
1963          are prepared to conditionally execute (even on a StrongARM).  */
1964       max_insns_skipped = 6;
1965     }
1966   else
1967     {
1968       /* StrongARM has early execution of branches, so a sequence
1969          that is worth skipping is shorter.  */
1970       if (arm_tune_strongarm)
1971         max_insns_skipped = 3;
1972     }
1973
1974   /* Hot/Cold partitioning is not currently supported, since we can't
1975      handle literal pool placement in that case.  */
1976   if (flag_reorder_blocks_and_partition)
1977     {
1978       inform (input_location,
1979               "-freorder-blocks-and-partition not supported on this architecture");
1980       flag_reorder_blocks_and_partition = 0;
1981       flag_reorder_blocks = 1;
1982     }
1983
1984   if (flag_pic)
1985     /* Hoisting PIC address calculations more aggressively provides a small,
1986        but measurable, size reduction for PIC code.  Therefore, we decrease
1987        the bar for unrestricted expression hoisting to the cost of PIC address
1988        calculation, which is 2 instructions.  */
1989     maybe_set_param_value (PARAM_GCSE_UNRESTRICTED_COST, 2,
1990                            global_options.x_param_values,
1991                            global_options_set.x_param_values);
1992
1993   /* ARM EABI defaults to strict volatile bitfields.  */
1994   if (TARGET_AAPCS_BASED && flag_strict_volatile_bitfields < 0)
1995     flag_strict_volatile_bitfields = 1;
1996
1997   /* Enable sw prefetching at -O3 for CPUS that have prefetch, and we have deemed
1998      it beneficial (signified by setting num_prefetch_slots to 1 or more.)  */
1999   if (flag_prefetch_loop_arrays < 0
2000       && HAVE_prefetch
2001       && optimize >= 3
2002       && current_tune->num_prefetch_slots > 0)
2003     flag_prefetch_loop_arrays = 1;
2004
2005   /* Set up parameters to be used in prefetching algorithm.  Do not override the
2006      defaults unless we are tuning for a core we have researched values for.  */
2007   if (current_tune->num_prefetch_slots > 0)
2008     maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
2009                            current_tune->num_prefetch_slots,
2010                            global_options.x_param_values,
2011                            global_options_set.x_param_values);
2012   if (current_tune->l1_cache_line_size >= 0)
2013     maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
2014                            current_tune->l1_cache_line_size,
2015                            global_options.x_param_values,
2016                            global_options_set.x_param_values);
2017   if (current_tune->l1_cache_size >= 0)
2018     maybe_set_param_value (PARAM_L1_CACHE_SIZE,
2019                            current_tune->l1_cache_size,
2020                            global_options.x_param_values,
2021                            global_options_set.x_param_values);
2022
2023   /* Register global variables with the garbage collector.  */
2024   arm_add_gc_roots ();
2025 }
2026
2027 static void
2028 arm_add_gc_roots (void)
2029 {
2030   gcc_obstack_init(&minipool_obstack);
2031   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
2032 }
2033 \f
2034 /* A table of known ARM exception types.
2035    For use with the interrupt function attribute.  */
2036
2037 typedef struct
2038 {
2039   const char *const arg;
2040   const unsigned long return_value;
2041 }
2042 isr_attribute_arg;
2043
2044 static const isr_attribute_arg isr_attribute_args [] =
2045 {
2046   { "IRQ",   ARM_FT_ISR },
2047   { "irq",   ARM_FT_ISR },
2048   { "FIQ",   ARM_FT_FIQ },
2049   { "fiq",   ARM_FT_FIQ },
2050   { "ABORT", ARM_FT_ISR },
2051   { "abort", ARM_FT_ISR },
2052   { "ABORT", ARM_FT_ISR },
2053   { "abort", ARM_FT_ISR },
2054   { "UNDEF", ARM_FT_EXCEPTION },
2055   { "undef", ARM_FT_EXCEPTION },
2056   { "SWI",   ARM_FT_EXCEPTION },
2057   { "swi",   ARM_FT_EXCEPTION },
2058   { NULL,    ARM_FT_NORMAL }
2059 };
2060
2061 /* Returns the (interrupt) function type of the current
2062    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
2063
2064 static unsigned long
2065 arm_isr_value (tree argument)
2066 {
2067   const isr_attribute_arg * ptr;
2068   const char *              arg;
2069
2070   if (!arm_arch_notm)
2071     return ARM_FT_NORMAL | ARM_FT_STACKALIGN;
2072
2073   /* No argument - default to IRQ.  */
2074   if (argument == NULL_TREE)
2075     return ARM_FT_ISR;
2076
2077   /* Get the value of the argument.  */
2078   if (TREE_VALUE (argument) == NULL_TREE
2079       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
2080     return ARM_FT_UNKNOWN;
2081
2082   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
2083
2084   /* Check it against the list of known arguments.  */
2085   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
2086     if (streq (arg, ptr->arg))
2087       return ptr->return_value;
2088
2089   /* An unrecognized interrupt type.  */
2090   return ARM_FT_UNKNOWN;
2091 }
2092
2093 /* Computes the type of the current function.  */
2094
2095 static unsigned long
2096 arm_compute_func_type (void)
2097 {
2098   unsigned long type = ARM_FT_UNKNOWN;
2099   tree a;
2100   tree attr;
2101
2102   gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
2103
2104   /* Decide if the current function is volatile.  Such functions
2105      never return, and many memory cycles can be saved by not storing
2106      register values that will never be needed again.  This optimization
2107      was added to speed up context switching in a kernel application.  */
2108   if (optimize > 0
2109       && (TREE_NOTHROW (current_function_decl)
2110           || !(flag_unwind_tables
2111                || (flag_exceptions
2112                    && arm_except_unwind_info (&global_options) != UI_SJLJ)))
2113       && TREE_THIS_VOLATILE (current_function_decl))
2114     type |= ARM_FT_VOLATILE;
2115
2116   if (cfun->static_chain_decl != NULL)
2117     type |= ARM_FT_NESTED;
2118
2119   attr = DECL_ATTRIBUTES (current_function_decl);
2120
2121   a = lookup_attribute ("naked", attr);
2122   if (a != NULL_TREE)
2123     type |= ARM_FT_NAKED;
2124
2125   a = lookup_attribute ("isr", attr);
2126   if (a == NULL_TREE)
2127     a = lookup_attribute ("interrupt", attr);
2128
2129   if (a == NULL_TREE)
2130     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
2131   else
2132     type |= arm_isr_value (TREE_VALUE (a));
2133
2134   return type;
2135 }
2136
2137 /* Returns the type of the current function.  */
2138
2139 unsigned long
2140 arm_current_func_type (void)
2141 {
2142   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
2143     cfun->machine->func_type = arm_compute_func_type ();
2144
2145   return cfun->machine->func_type;
2146 }
2147
2148 bool
2149 arm_allocate_stack_slots_for_args (void)
2150 {
2151   /* Naked functions should not allocate stack slots for arguments.  */
2152   return !IS_NAKED (arm_current_func_type ());
2153 }
2154
2155 \f
2156 /* Output assembler code for a block containing the constant parts
2157    of a trampoline, leaving space for the variable parts.
2158
2159    On the ARM, (if r8 is the static chain regnum, and remembering that
2160    referencing pc adds an offset of 8) the trampoline looks like:
2161            ldr          r8, [pc, #0]
2162            ldr          pc, [pc]
2163            .word        static chain value
2164            .word        function's address
2165    XXX FIXME: When the trampoline returns, r8 will be clobbered.  */
2166
2167 static void
2168 arm_asm_trampoline_template (FILE *f)
2169 {
2170   if (TARGET_ARM)
2171     {
2172       asm_fprintf (f, "\tldr\t%r, [%r, #0]\n", STATIC_CHAIN_REGNUM, PC_REGNUM);
2173       asm_fprintf (f, "\tldr\t%r, [%r, #0]\n", PC_REGNUM, PC_REGNUM);
2174     }
2175   else if (TARGET_THUMB2)
2176     {
2177       /* The Thumb-2 trampoline is similar to the arm implementation.
2178          Unlike 16-bit Thumb, we enter the stub in thumb mode.  */
2179       asm_fprintf (f, "\tldr.w\t%r, [%r, #4]\n",
2180                    STATIC_CHAIN_REGNUM, PC_REGNUM);
2181       asm_fprintf (f, "\tldr.w\t%r, [%r, #4]\n", PC_REGNUM, PC_REGNUM);
2182     }
2183   else
2184     {
2185       ASM_OUTPUT_ALIGN (f, 2);
2186       fprintf (f, "\t.code\t16\n");
2187       fprintf (f, ".Ltrampoline_start:\n");
2188       asm_fprintf (f, "\tpush\t{r0, r1}\n");
2189       asm_fprintf (f, "\tldr\tr0, [%r, #8]\n", PC_REGNUM);
2190       asm_fprintf (f, "\tmov\t%r, r0\n", STATIC_CHAIN_REGNUM);
2191       asm_fprintf (f, "\tldr\tr0, [%r, #8]\n", PC_REGNUM);
2192       asm_fprintf (f, "\tstr\tr0, [%r, #4]\n", SP_REGNUM);
2193       asm_fprintf (f, "\tpop\t{r0, %r}\n", PC_REGNUM);
2194     }
2195   assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2196   assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2197 }
2198
2199 /* Emit RTL insns to initialize the variable parts of a trampoline.  */
2200
2201 static void
2202 arm_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
2203 {
2204   rtx fnaddr, mem, a_tramp;
2205
2206   emit_block_move (m_tramp, assemble_trampoline_template (),
2207                    GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
2208
2209   mem = adjust_address (m_tramp, SImode, TARGET_32BIT ? 8 : 12);
2210   emit_move_insn (mem, chain_value);
2211
2212   mem = adjust_address (m_tramp, SImode, TARGET_32BIT ? 12 : 16);
2213   fnaddr = XEXP (DECL_RTL (fndecl), 0);
2214   emit_move_insn (mem, fnaddr);
2215
2216   a_tramp = XEXP (m_tramp, 0);
2217   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__clear_cache"),
2218                      LCT_NORMAL, VOIDmode, 2, a_tramp, Pmode,
2219                      plus_constant (a_tramp, TRAMPOLINE_SIZE), Pmode);
2220 }
2221
2222 /* Thumb trampolines should be entered in thumb mode, so set
2223    the bottom bit of the address.  */
2224
2225 static rtx
2226 arm_trampoline_adjust_address (rtx addr)
2227 {
2228   if (TARGET_THUMB)
2229     addr = expand_simple_binop (Pmode, IOR, addr, const1_rtx,
2230                                 NULL, 0, OPTAB_LIB_WIDEN);
2231   return addr;
2232 }
2233 \f
2234 /* Return 1 if it is possible to return using a single instruction.
2235    If SIBLING is non-null, this is a test for a return before a sibling
2236    call.  SIBLING is the call insn, so we can examine its register usage.  */
2237
2238 int
2239 use_return_insn (int iscond, rtx sibling)
2240 {
2241   int regno;
2242   unsigned int func_type;
2243   unsigned long saved_int_regs;
2244   unsigned HOST_WIDE_INT stack_adjust;
2245   arm_stack_offsets *offsets;
2246
2247   /* Never use a return instruction before reload has run.  */
2248   if (!reload_completed)
2249     return 0;
2250
2251   func_type = arm_current_func_type ();
2252
2253   /* Naked, volatile and stack alignment functions need special
2254      consideration.  */
2255   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED | ARM_FT_STACKALIGN))
2256     return 0;
2257
2258   /* So do interrupt functions that use the frame pointer and Thumb
2259      interrupt functions.  */
2260   if (IS_INTERRUPT (func_type) && (frame_pointer_needed || TARGET_THUMB))
2261     return 0;
2262
2263   offsets = arm_get_frame_offsets ();
2264   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
2265
2266   /* As do variadic functions.  */
2267   if (crtl->args.pretend_args_size
2268       || cfun->machine->uses_anonymous_args
2269       /* Or if the function calls __builtin_eh_return () */
2270       || crtl->calls_eh_return
2271       /* Or if the function calls alloca */
2272       || cfun->calls_alloca
2273       /* Or if there is a stack adjustment.  However, if the stack pointer
2274          is saved on the stack, we can use a pre-incrementing stack load.  */
2275       || !(stack_adjust == 0 || (TARGET_APCS_FRAME && frame_pointer_needed
2276                                  && stack_adjust == 4)))
2277     return 0;
2278
2279   saved_int_regs = offsets->saved_regs_mask;
2280
2281   /* Unfortunately, the insn
2282
2283        ldmib sp, {..., sp, ...}
2284
2285      triggers a bug on most SA-110 based devices, such that the stack
2286      pointer won't be correctly restored if the instruction takes a
2287      page fault.  We work around this problem by popping r3 along with
2288      the other registers, since that is never slower than executing
2289      another instruction.
2290
2291      We test for !arm_arch5 here, because code for any architecture
2292      less than this could potentially be run on one of the buggy
2293      chips.  */
2294   if (stack_adjust == 4 && !arm_arch5 && TARGET_ARM)
2295     {
2296       /* Validate that r3 is a call-clobbered register (always true in
2297          the default abi) ...  */
2298       if (!call_used_regs[3])
2299         return 0;
2300
2301       /* ... that it isn't being used for a return value ... */
2302       if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
2303         return 0;
2304
2305       /* ... or for a tail-call argument ...  */
2306       if (sibling)
2307         {
2308           gcc_assert (GET_CODE (sibling) == CALL_INSN);
2309
2310           if (find_regno_fusage (sibling, USE, 3))
2311             return 0;
2312         }
2313
2314       /* ... and that there are no call-saved registers in r0-r2
2315          (always true in the default ABI).  */
2316       if (saved_int_regs & 0x7)
2317         return 0;
2318     }
2319
2320   /* Can't be done if interworking with Thumb, and any registers have been
2321      stacked.  */
2322   if (TARGET_INTERWORK && saved_int_regs != 0 && !IS_INTERRUPT(func_type))
2323     return 0;
2324
2325   /* On StrongARM, conditional returns are expensive if they aren't
2326      taken and multiple registers have been stacked.  */
2327   if (iscond && arm_tune_strongarm)
2328     {
2329       /* Conditional return when just the LR is stored is a simple
2330          conditional-load instruction, that's not expensive.  */
2331       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
2332         return 0;
2333
2334       if (flag_pic 
2335           && arm_pic_register != INVALID_REGNUM
2336           && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
2337         return 0;
2338     }
2339
2340   /* If there are saved registers but the LR isn't saved, then we need
2341      two instructions for the return.  */
2342   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
2343     return 0;
2344
2345   /* Can't be done if any of the FPA regs are pushed,
2346      since this also requires an insn.  */
2347   if (TARGET_HARD_FLOAT && TARGET_FPA)
2348     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
2349       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
2350         return 0;
2351
2352   /* Likewise VFP regs.  */
2353   if (TARGET_HARD_FLOAT && TARGET_VFP)
2354     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
2355       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
2356         return 0;
2357
2358   if (TARGET_REALLY_IWMMXT)
2359     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
2360       if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
2361         return 0;
2362
2363   return 1;
2364 }
2365
2366 /* Return TRUE if int I is a valid immediate ARM constant.  */
2367
2368 int
2369 const_ok_for_arm (HOST_WIDE_INT i)
2370 {
2371   int lowbit;
2372
2373   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
2374      be all zero, or all one.  */
2375   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
2376       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
2377           != ((~(unsigned HOST_WIDE_INT) 0)
2378               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
2379     return FALSE;
2380
2381   i &= (unsigned HOST_WIDE_INT) 0xffffffff;
2382
2383   /* Fast return for 0 and small values.  We must do this for zero, since
2384      the code below can't handle that one case.  */
2385   if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
2386     return TRUE;
2387
2388   /* Get the number of trailing zeros.  */
2389   lowbit = ffs((int) i) - 1;
2390   
2391   /* Only even shifts are allowed in ARM mode so round down to the
2392      nearest even number.  */
2393   if (TARGET_ARM)
2394     lowbit &= ~1;
2395
2396   if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
2397     return TRUE;
2398
2399   if (TARGET_ARM)
2400     {
2401       /* Allow rotated constants in ARM mode.  */
2402       if (lowbit <= 4
2403            && ((i & ~0xc000003f) == 0
2404                || (i & ~0xf000000f) == 0
2405                || (i & ~0xfc000003) == 0))
2406         return TRUE;
2407     }
2408   else
2409     {
2410       HOST_WIDE_INT v;
2411
2412       /* Allow repeated patterns 0x00XY00XY or 0xXYXYXYXY.  */
2413       v = i & 0xff;
2414       v |= v << 16;
2415       if (i == v || i == (v | (v << 8)))
2416         return TRUE;
2417
2418       /* Allow repeated pattern 0xXY00XY00.  */
2419       v = i & 0xff00;
2420       v |= v << 16;
2421       if (i == v)
2422         return TRUE;
2423     }
2424
2425   return FALSE;
2426 }
2427
2428 /* Return true if I is a valid constant for the operation CODE.  */
2429 static int
2430 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
2431 {
2432   if (const_ok_for_arm (i))
2433     return 1;
2434
2435   switch (code)
2436     {
2437     case PLUS:
2438     case COMPARE:
2439     case EQ:
2440     case NE:
2441     case GT:
2442     case LE:
2443     case LT:
2444     case GE:
2445     case GEU:
2446     case LTU:
2447     case GTU:
2448     case LEU:
2449     case UNORDERED:
2450     case ORDERED:
2451     case UNEQ:
2452     case UNGE:
2453     case UNLT:
2454     case UNGT:
2455     case UNLE:
2456       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
2457
2458     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
2459     case XOR:
2460       return 0;
2461
2462     case IOR:
2463       if (TARGET_THUMB2)
2464         return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2465       return 0;
2466
2467     case AND:
2468       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2469
2470     default:
2471       gcc_unreachable ();
2472     }
2473 }
2474
2475 /* Emit a sequence of insns to handle a large constant.
2476    CODE is the code of the operation required, it can be any of SET, PLUS,
2477    IOR, AND, XOR, MINUS;
2478    MODE is the mode in which the operation is being performed;
2479    VAL is the integer to operate on;
2480    SOURCE is the other operand (a register, or a null-pointer for SET);
2481    SUBTARGETS means it is safe to create scratch registers if that will
2482    either produce a simpler sequence, or we will want to cse the values.
2483    Return value is the number of insns emitted.  */
2484
2485 /* ??? Tweak this for thumb2.  */
2486 int
2487 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
2488                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
2489 {
2490   rtx cond;
2491
2492   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
2493     cond = COND_EXEC_TEST (PATTERN (insn));
2494   else
2495     cond = NULL_RTX;
2496
2497   if (subtargets || code == SET
2498       || (GET_CODE (target) == REG && GET_CODE (source) == REG
2499           && REGNO (target) != REGNO (source)))
2500     {
2501       /* After arm_reorg has been called, we can't fix up expensive
2502          constants by pushing them into memory so we must synthesize
2503          them in-line, regardless of the cost.  This is only likely to
2504          be more costly on chips that have load delay slots and we are
2505          compiling without running the scheduler (so no splitting
2506          occurred before the final instruction emission).
2507
2508          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
2509       */
2510       if (!after_arm_reorg
2511           && !cond
2512           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
2513                                 1, 0)
2514               > (arm_constant_limit (optimize_function_for_size_p (cfun))
2515                  + (code != SET))))
2516         {
2517           if (code == SET)
2518             {
2519               /* Currently SET is the only monadic value for CODE, all
2520                  the rest are diadic.  */
2521               if (TARGET_USE_MOVT)
2522                 arm_emit_movpair (target, GEN_INT (val));
2523               else
2524                 emit_set_insn (target, GEN_INT (val));
2525
2526               return 1;
2527             }
2528           else
2529             {
2530               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
2531
2532               if (TARGET_USE_MOVT)
2533                 arm_emit_movpair (temp, GEN_INT (val));
2534               else
2535                 emit_set_insn (temp, GEN_INT (val));
2536
2537               /* For MINUS, the value is subtracted from, since we never
2538                  have subtraction of a constant.  */
2539               if (code == MINUS)
2540                 emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
2541               else
2542                 emit_set_insn (target,
2543                                gen_rtx_fmt_ee (code, mode, source, temp));
2544               return 2;
2545             }
2546         }
2547     }
2548
2549   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
2550                            1);
2551 }
2552
2553 /* Return the number of instructions required to synthesize the given
2554    constant, if we start emitting them from bit-position I.  */
2555 static int
2556 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
2557 {
2558   HOST_WIDE_INT temp1;
2559   int step_size = TARGET_ARM ? 2 : 1;
2560   int num_insns = 0;
2561
2562   gcc_assert (TARGET_ARM || i == 0);
2563
2564   do
2565     {
2566       int end;
2567
2568       if (i <= 0)
2569         i += 32;
2570       if (remainder & (((1 << step_size) - 1) << (i - step_size)))
2571         {
2572           end = i - 8;
2573           if (end < 0)
2574             end += 32;
2575           temp1 = remainder & ((0x0ff << end)
2576                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
2577           remainder &= ~temp1;
2578           num_insns++;
2579           i -= 8 - step_size;
2580         }
2581       i -= step_size;
2582     } while (remainder);
2583   return num_insns;
2584 }
2585
2586 static int
2587 find_best_start (unsigned HOST_WIDE_INT remainder)
2588 {
2589   int best_consecutive_zeros = 0;
2590   int i;
2591   int best_start = 0;
2592
2593   /* If we aren't targetting ARM, the best place to start is always at
2594      the bottom.  */
2595   if (! TARGET_ARM)
2596     return 0;
2597
2598   for (i = 0; i < 32; i += 2)
2599     {
2600       int consecutive_zeros = 0;
2601
2602       if (!(remainder & (3 << i)))
2603         {
2604           while ((i < 32) && !(remainder & (3 << i)))
2605             {
2606               consecutive_zeros += 2;
2607               i += 2;
2608             }
2609           if (consecutive_zeros > best_consecutive_zeros)
2610             {
2611               best_consecutive_zeros = consecutive_zeros;
2612               best_start = i - consecutive_zeros;
2613             }
2614           i -= 2;
2615         }
2616     }
2617
2618   /* So long as it won't require any more insns to do so, it's
2619      desirable to emit a small constant (in bits 0...9) in the last
2620      insn.  This way there is more chance that it can be combined with
2621      a later addressing insn to form a pre-indexed load or store
2622      operation.  Consider:
2623
2624            *((volatile int *)0xe0000100) = 1;
2625            *((volatile int *)0xe0000110) = 2;
2626
2627      We want this to wind up as:
2628
2629             mov rA, #0xe0000000
2630             mov rB, #1
2631             str rB, [rA, #0x100]
2632             mov rB, #2
2633             str rB, [rA, #0x110]
2634
2635      rather than having to synthesize both large constants from scratch.
2636
2637      Therefore, we calculate how many insns would be required to emit
2638      the constant starting from `best_start', and also starting from
2639      zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2640      yield a shorter sequence, we may as well use zero.  */
2641   if (best_start != 0
2642       && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2643       && (count_insns_for_constant (remainder, 0) <=
2644           count_insns_for_constant (remainder, best_start)))
2645     best_start = 0;
2646
2647   return best_start;
2648 }
2649
2650 /* Emit an instruction with the indicated PATTERN.  If COND is
2651    non-NULL, conditionalize the execution of the instruction on COND
2652    being true.  */
2653
2654 static void
2655 emit_constant_insn (rtx cond, rtx pattern)
2656 {
2657   if (cond)
2658     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
2659   emit_insn (pattern);
2660 }
2661
2662 /* As above, but extra parameter GENERATE which, if clear, suppresses
2663    RTL generation.  */
2664 /* ??? This needs more work for thumb2.  */
2665
2666 static int
2667 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
2668                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
2669                   int generate)
2670 {
2671   int can_invert = 0;
2672   int can_negate = 0;
2673   int final_invert = 0;
2674   int can_negate_initial = 0;
2675   int i;
2676   int num_bits_set = 0;
2677   int set_sign_bit_copies = 0;
2678   int clear_sign_bit_copies = 0;
2679   int clear_zero_bit_copies = 0;
2680   int set_zero_bit_copies = 0;
2681   int insns = 0;
2682   unsigned HOST_WIDE_INT temp1, temp2;
2683   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
2684   int step_size = TARGET_ARM ? 2 : 1;
2685
2686   /* Find out which operations are safe for a given CODE.  Also do a quick
2687      check for degenerate cases; these can occur when DImode operations
2688      are split.  */
2689   switch (code)
2690     {
2691     case SET:
2692       can_invert = 1;
2693       can_negate = 1;
2694       break;
2695
2696     case PLUS:
2697       can_negate = 1;
2698       can_negate_initial = 1;
2699       break;
2700
2701     case IOR:
2702       if (remainder == 0xffffffff)
2703         {
2704           if (generate)
2705             emit_constant_insn (cond,
2706                                 gen_rtx_SET (VOIDmode, target,
2707                                              GEN_INT (ARM_SIGN_EXTEND (val))));
2708           return 1;
2709         }
2710
2711       if (remainder == 0)
2712         {
2713           if (reload_completed && rtx_equal_p (target, source))
2714             return 0;
2715
2716           if (generate)
2717             emit_constant_insn (cond,
2718                                 gen_rtx_SET (VOIDmode, target, source));
2719           return 1;
2720         }
2721
2722       if (TARGET_THUMB2)
2723         can_invert = 1;
2724       break;
2725
2726     case AND:
2727       if (remainder == 0)
2728         {
2729           if (generate)
2730             emit_constant_insn (cond,
2731                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
2732           return 1;
2733         }
2734       if (remainder == 0xffffffff)
2735         {
2736           if (reload_completed && rtx_equal_p (target, source))
2737             return 0;
2738           if (generate)
2739             emit_constant_insn (cond,
2740                                 gen_rtx_SET (VOIDmode, target, source));
2741           return 1;
2742         }
2743       can_invert = 1;
2744       break;
2745
2746     case XOR:
2747       if (remainder == 0)
2748         {
2749           if (reload_completed && rtx_equal_p (target, source))
2750             return 0;
2751           if (generate)
2752             emit_constant_insn (cond,
2753                                 gen_rtx_SET (VOIDmode, target, source));
2754           return 1;
2755         }
2756
2757       if (remainder == 0xffffffff)
2758         {
2759           if (generate)
2760             emit_constant_insn (cond,
2761                                 gen_rtx_SET (VOIDmode, target,
2762                                              gen_rtx_NOT (mode, source)));
2763           return 1;
2764         }
2765       break;
2766
2767     case MINUS:
2768       /* We treat MINUS as (val - source), since (source - val) is always
2769          passed as (source + (-val)).  */
2770       if (remainder == 0)
2771         {
2772           if (generate)
2773             emit_constant_insn (cond,
2774                                 gen_rtx_SET (VOIDmode, target,
2775                                              gen_rtx_NEG (mode, source)));
2776           return 1;
2777         }
2778       if (const_ok_for_arm (val))
2779         {
2780           if (generate)
2781             emit_constant_insn (cond,
2782                                 gen_rtx_SET (VOIDmode, target,
2783                                              gen_rtx_MINUS (mode, GEN_INT (val),
2784                                                             source)));
2785           return 1;
2786         }
2787       can_negate = 1;
2788
2789       break;
2790
2791     default:
2792       gcc_unreachable ();
2793     }
2794
2795   /* If we can do it in one insn get out quickly.  */
2796   if (const_ok_for_arm (val)
2797       || (can_negate_initial && const_ok_for_arm (-val))
2798       || (can_invert && const_ok_for_arm (~val)))
2799     {
2800       if (generate)
2801         emit_constant_insn (cond,
2802                             gen_rtx_SET (VOIDmode, target,
2803                                          (source
2804                                           ? gen_rtx_fmt_ee (code, mode, source,
2805                                                             GEN_INT (val))
2806                                           : GEN_INT (val))));
2807       return 1;
2808     }
2809
2810   /* Calculate a few attributes that may be useful for specific
2811      optimizations.  */
2812   /* Count number of leading zeros.  */
2813   for (i = 31; i >= 0; i--)
2814     {
2815       if ((remainder & (1 << i)) == 0)
2816         clear_sign_bit_copies++;
2817       else
2818         break;
2819     }
2820
2821   /* Count number of leading 1's.  */
2822   for (i = 31; i >= 0; i--)
2823     {
2824       if ((remainder & (1 << i)) != 0)
2825         set_sign_bit_copies++;
2826       else
2827         break;
2828     }
2829
2830   /* Count number of trailing zero's.  */
2831   for (i = 0; i <= 31; i++)
2832     {
2833       if ((remainder & (1 << i)) == 0)
2834         clear_zero_bit_copies++;
2835       else
2836         break;
2837     }
2838
2839   /* Count number of trailing 1's.  */
2840   for (i = 0; i <= 31; i++)
2841     {
2842       if ((remainder & (1 << i)) != 0)
2843         set_zero_bit_copies++;
2844       else
2845         break;
2846     }
2847
2848   switch (code)
2849     {
2850     case SET:
2851       /* See if we can use movw.  */
2852       if (arm_arch_thumb2 && (remainder & 0xffff0000) == 0)
2853         {
2854           if (generate)
2855             emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
2856                                                    GEN_INT (val)));
2857           return 1;
2858         }
2859
2860       /* See if we can do this by sign_extending a constant that is known
2861          to be negative.  This is a good, way of doing it, since the shift
2862          may well merge into a subsequent insn.  */
2863       if (set_sign_bit_copies > 1)
2864         {
2865           if (const_ok_for_arm
2866               (temp1 = ARM_SIGN_EXTEND (remainder
2867                                         << (set_sign_bit_copies - 1))))
2868             {
2869               if (generate)
2870                 {
2871                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2872                   emit_constant_insn (cond,
2873                                       gen_rtx_SET (VOIDmode, new_src,
2874                                                    GEN_INT (temp1)));
2875                   emit_constant_insn (cond,
2876                                       gen_ashrsi3 (target, new_src,
2877                                                    GEN_INT (set_sign_bit_copies - 1)));
2878                 }
2879               return 2;
2880             }
2881           /* For an inverted constant, we will need to set the low bits,
2882              these will be shifted out of harm's way.  */
2883           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
2884           if (const_ok_for_arm (~temp1))
2885             {
2886               if (generate)
2887                 {
2888                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2889                   emit_constant_insn (cond,
2890                                       gen_rtx_SET (VOIDmode, new_src,
2891                                                    GEN_INT (temp1)));
2892                   emit_constant_insn (cond,
2893                                       gen_ashrsi3 (target, new_src,
2894                                                    GEN_INT (set_sign_bit_copies - 1)));
2895                 }
2896               return 2;
2897             }
2898         }
2899
2900       /* See if we can calculate the value as the difference between two
2901          valid immediates.  */
2902       if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
2903         {
2904           int topshift = clear_sign_bit_copies & ~1;
2905
2906           temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
2907                                    & (0xff000000 >> topshift));
2908
2909           /* If temp1 is zero, then that means the 9 most significant
2910              bits of remainder were 1 and we've caused it to overflow.
2911              When topshift is 0 we don't need to do anything since we
2912              can borrow from 'bit 32'.  */
2913           if (temp1 == 0 && topshift != 0)
2914             temp1 = 0x80000000 >> (topshift - 1);
2915
2916           temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
2917
2918           if (const_ok_for_arm (temp2))
2919             {
2920               if (generate)
2921                 {
2922                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2923                   emit_constant_insn (cond,
2924                                       gen_rtx_SET (VOIDmode, new_src,
2925                                                    GEN_INT (temp1)));
2926                   emit_constant_insn (cond,
2927                                       gen_addsi3 (target, new_src,
2928                                                   GEN_INT (-temp2)));
2929                 }
2930
2931               return 2;
2932             }
2933         }
2934
2935       /* See if we can generate this by setting the bottom (or the top)
2936          16 bits, and then shifting these into the other half of the
2937          word.  We only look for the simplest cases, to do more would cost
2938          too much.  Be careful, however, not to generate this when the
2939          alternative would take fewer insns.  */
2940       if (val & 0xffff0000)
2941         {
2942           temp1 = remainder & 0xffff0000;
2943           temp2 = remainder & 0x0000ffff;
2944
2945           /* Overlaps outside this range are best done using other methods.  */
2946           for (i = 9; i < 24; i++)
2947             {
2948               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
2949                   && !const_ok_for_arm (temp2))
2950                 {
2951                   rtx new_src = (subtargets
2952                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2953                                  : target);
2954                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2955                                             source, subtargets, generate);
2956                   source = new_src;
2957                   if (generate)
2958                     emit_constant_insn
2959                       (cond,
2960                        gen_rtx_SET
2961                        (VOIDmode, target,
2962                         gen_rtx_IOR (mode,
2963                                      gen_rtx_ASHIFT (mode, source,
2964                                                      GEN_INT (i)),
2965                                      source)));
2966                   return insns + 1;
2967                 }
2968             }
2969
2970           /* Don't duplicate cases already considered.  */
2971           for (i = 17; i < 24; i++)
2972             {
2973               if (((temp1 | (temp1 >> i)) == remainder)
2974                   && !const_ok_for_arm (temp1))
2975                 {
2976                   rtx new_src = (subtargets
2977                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2978                                  : target);
2979                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2980                                             source, subtargets, generate);
2981                   source = new_src;
2982                   if (generate)
2983                     emit_constant_insn
2984                       (cond,
2985                        gen_rtx_SET (VOIDmode, target,
2986                                     gen_rtx_IOR
2987                                     (mode,
2988                                      gen_rtx_LSHIFTRT (mode, source,
2989                                                        GEN_INT (i)),
2990                                      source)));
2991                   return insns + 1;
2992                 }
2993             }
2994         }
2995       break;
2996
2997     case IOR:
2998     case XOR:
2999       /* If we have IOR or XOR, and the constant can be loaded in a
3000          single instruction, and we can find a temporary to put it in,
3001          then this can be done in two instructions instead of 3-4.  */
3002       if (subtargets
3003           /* TARGET can't be NULL if SUBTARGETS is 0 */
3004           || (reload_completed && !reg_mentioned_p (target, source)))
3005         {
3006           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
3007             {
3008               if (generate)
3009                 {
3010                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
3011
3012                   emit_constant_insn (cond,
3013                                       gen_rtx_SET (VOIDmode, sub,
3014                                                    GEN_INT (val)));
3015                   emit_constant_insn (cond,
3016                                       gen_rtx_SET (VOIDmode, target,
3017                                                    gen_rtx_fmt_ee (code, mode,
3018                                                                    source, sub)));
3019                 }
3020               return 2;
3021             }
3022         }
3023
3024       if (code == XOR)
3025         break;
3026
3027       /*  Convert.
3028           x = y | constant ( which is composed of set_sign_bit_copies of leading 1s
3029                              and the remainder 0s for e.g. 0xfff00000)
3030           x = ~(~(y ashift set_sign_bit_copies) lshiftrt set_sign_bit_copies)
3031
3032           This can be done in 2 instructions by using shifts with mov or mvn.
3033           e.g. for
3034           x = x | 0xfff00000;
3035           we generate.
3036           mvn   r0, r0, asl #12
3037           mvn   r0, r0, lsr #12  */
3038       if (set_sign_bit_copies > 8
3039           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
3040         {
3041           if (generate)
3042             {
3043               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
3044               rtx shift = GEN_INT (set_sign_bit_copies);
3045
3046               emit_constant_insn
3047                 (cond,
3048                  gen_rtx_SET (VOIDmode, sub,
3049                               gen_rtx_NOT (mode,
3050                                            gen_rtx_ASHIFT (mode,
3051                                                            source,
3052                                                            shift))));
3053               emit_constant_insn
3054                 (cond,
3055                  gen_rtx_SET (VOIDmode, target,
3056                               gen_rtx_NOT (mode,
3057                                            gen_rtx_LSHIFTRT (mode, sub,
3058                                                              shift))));
3059             }
3060           return 2;
3061         }
3062
3063       /* Convert
3064           x = y | constant (which has set_zero_bit_copies number of trailing ones).
3065            to
3066           x = ~((~y lshiftrt set_zero_bit_copies) ashift set_zero_bit_copies).
3067
3068           For eg. r0 = r0 | 0xfff
3069                mvn      r0, r0, lsr #12
3070                mvn      r0, r0, asl #12
3071
3072       */
3073       if (set_zero_bit_copies > 8
3074           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
3075         {
3076           if (generate)
3077             {
3078               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
3079               rtx shift = GEN_INT (set_zero_bit_copies);
3080
3081               emit_constant_insn
3082                 (cond,
3083                  gen_rtx_SET (VOIDmode, sub,
3084                               gen_rtx_NOT (mode,
3085                                            gen_rtx_LSHIFTRT (mode,
3086                                                              source,
3087                                                              shift))));
3088               emit_constant_insn
3089                 (cond,
3090                  gen_rtx_SET (VOIDmode, target,
3091                               gen_rtx_NOT (mode,
3092                                            gen_rtx_ASHIFT (mode, sub,
3093                                                            shift))));
3094             }
3095           return 2;
3096         }
3097
3098       /* This will never be reached for Thumb2 because orn is a valid
3099          instruction. This is for Thumb1 and the ARM 32 bit cases.
3100
3101          x = y | constant (such that ~constant is a valid constant)
3102          Transform this to
3103          x = ~(~y & ~constant).
3104       */
3105       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
3106         {
3107           if (generate)
3108             {
3109               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
3110               emit_constant_insn (cond,
3111                                   gen_rtx_SET (VOIDmode, sub,
3112                                                gen_rtx_NOT (mode, source)));
3113               source = sub;
3114               if (subtargets)
3115                 sub = gen_reg_rtx (mode);
3116               emit_constant_insn (cond,
3117                                   gen_rtx_SET (VOIDmode, sub,
3118                                                gen_rtx_AND (mode, source,
3119                                                             GEN_INT (temp1))));
3120               emit_constant_insn (cond,
3121                                   gen_rtx_SET (VOIDmode, target,
3122                                                gen_rtx_NOT (mode, sub)));
3123             }
3124           return 3;
3125         }
3126       break;
3127
3128     case AND:
3129       /* See if two shifts will do 2 or more insn's worth of work.  */
3130       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
3131         {
3132           HOST_WIDE_INT shift_mask = ((0xffffffff
3133                                        << (32 - clear_sign_bit_copies))
3134                                       & 0xffffffff);
3135
3136           if ((remainder | shift_mask) != 0xffffffff)
3137             {
3138               if (generate)
3139                 {
3140                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3141                   insns = arm_gen_constant (AND, mode, cond,
3142                                             remainder | shift_mask,
3143                                             new_src, source, subtargets, 1);
3144                   source = new_src;
3145                 }
3146               else
3147                 {
3148                   rtx targ = subtargets ? NULL_RTX : target;
3149                   insns = arm_gen_constant (AND, mode, cond,
3150                                             remainder | shift_mask,
3151                                             targ, source, subtargets, 0);
3152                 }
3153             }
3154
3155           if (generate)
3156             {
3157               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3158               rtx shift = GEN_INT (clear_sign_bit_copies);
3159
3160               emit_insn (gen_ashlsi3 (new_src, source, shift));
3161               emit_insn (gen_lshrsi3 (target, new_src, shift));
3162             }
3163
3164           return insns + 2;
3165         }
3166
3167       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
3168         {
3169           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
3170
3171           if ((remainder | shift_mask) != 0xffffffff)
3172             {
3173               if (generate)
3174                 {
3175                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3176
3177                   insns = arm_gen_constant (AND, mode, cond,
3178                                             remainder | shift_mask,
3179                                             new_src, source, subtargets, 1);
3180                   source = new_src;
3181                 }
3182               else
3183                 {
3184                   rtx targ = subtargets ? NULL_RTX : target;
3185
3186                   insns = arm_gen_constant (AND, mode, cond,
3187                                             remainder | shift_mask,
3188                                             targ, source, subtargets, 0);
3189                 }
3190             }
3191
3192           if (generate)
3193             {
3194               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3195               rtx shift = GEN_INT (clear_zero_bit_copies);
3196
3197               emit_insn (gen_lshrsi3 (new_src, source, shift));
3198               emit_insn (gen_ashlsi3 (target, new_src, shift));
3199             }
3200
3201           return insns + 2;
3202         }
3203
3204       break;
3205
3206     default:
3207       break;
3208     }
3209
3210   for (i = 0; i < 32; i++)
3211     if (remainder & (1 << i))
3212       num_bits_set++;
3213
3214   if ((code == AND)
3215       || (code != IOR && can_invert && num_bits_set > 16))
3216     remainder ^= 0xffffffff;
3217   else if (code == PLUS && num_bits_set > 16)
3218     remainder = (-remainder) & 0xffffffff;
3219
3220   /* For XOR, if more than half the bits are set and there's a sequence
3221      of more than 8 consecutive ones in the pattern then we can XOR by the
3222      inverted constant and then invert the final result; this may save an
3223      instruction and might also lead to the final mvn being merged with
3224      some other operation.  */
3225   else if (code == XOR && num_bits_set > 16
3226            && (count_insns_for_constant (remainder ^ 0xffffffff,
3227                                          find_best_start
3228                                          (remainder ^ 0xffffffff))
3229                < count_insns_for_constant (remainder,
3230                                            find_best_start (remainder))))
3231     {
3232       remainder ^= 0xffffffff;
3233       final_invert = 1;
3234     }
3235   else
3236     {
3237       can_invert = 0;
3238       can_negate = 0;
3239     }
3240
3241   /* Now try and find a way of doing the job in either two or three
3242      instructions.
3243      We start by looking for the largest block of zeros that are aligned on
3244      a 2-bit boundary, we then fill up the temps, wrapping around to the
3245      top of the word when we drop off the bottom.
3246      In the worst case this code should produce no more than four insns.
3247      Thumb-2 constants are shifted, not rotated, so the MSB is always the
3248      best place to start.  */
3249
3250   /* ??? Use thumb2 replicated constants when the high and low halfwords are
3251      the same.  */
3252   {
3253     /* Now start emitting the insns.  */
3254     i = find_best_start (remainder);
3255     do
3256       {
3257         int end;
3258
3259         if (i <= 0)
3260           i += 32;
3261         if (remainder & (3 << (i - 2)))
3262           {
3263             end = i - 8;
3264             if (end < 0)
3265               end += 32;
3266             temp1 = remainder & ((0x0ff << end)
3267                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
3268             remainder &= ~temp1;
3269
3270             if (generate)
3271               {
3272                 rtx new_src, temp1_rtx;
3273
3274                 if (code == SET || code == MINUS)
3275                   {
3276                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
3277                     if (can_invert && code != MINUS)
3278                       temp1 = ~temp1;
3279                   }
3280                 else
3281                   {
3282                     if ((final_invert || remainder) && subtargets)
3283                       new_src = gen_reg_rtx (mode);
3284                     else
3285                       new_src = target;
3286                     if (can_invert)
3287                       temp1 = ~temp1;
3288                     else if (can_negate)
3289                       temp1 = -temp1;
3290                   }
3291
3292                 temp1 = trunc_int_for_mode (temp1, mode);
3293                 temp1_rtx = GEN_INT (temp1);
3294
3295                 if (code == SET)
3296                   ;
3297                 else if (code == MINUS)
3298                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
3299                 else
3300                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
3301
3302                 emit_constant_insn (cond,
3303                                     gen_rtx_SET (VOIDmode, new_src,
3304                                                  temp1_rtx));
3305                 source = new_src;
3306               }
3307
3308             if (code == SET)
3309               {
3310                 can_invert = 0;
3311                 code = PLUS;
3312               }
3313             else if (code == MINUS)
3314               code = PLUS;
3315
3316             insns++;
3317             i -= 8 - step_size;
3318           }
3319         /* Arm allows rotates by a multiple of two. Thumb-2 allows arbitrary
3320            shifts.  */
3321         i -= step_size;
3322       }
3323     while (remainder);
3324   }
3325
3326   if (final_invert)
3327     {
3328       if (generate)
3329         emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
3330                                                gen_rtx_NOT (mode, source)));
3331       insns++;
3332     }
3333
3334   return insns;
3335 }
3336
3337 /* Canonicalize a comparison so that we are more likely to recognize it.
3338    This can be done for a few constant compares, where we can make the
3339    immediate value easier to load.  */
3340
3341 enum rtx_code
3342 arm_canonicalize_comparison (enum rtx_code code, rtx *op0, rtx *op1)
3343 {
3344   enum machine_mode mode;
3345   unsigned HOST_WIDE_INT i, maxval;
3346
3347   mode = GET_MODE (*op0);
3348   if (mode == VOIDmode)
3349     mode = GET_MODE (*op1);
3350
3351   maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
3352
3353   /* For DImode, we have GE/LT/GEU/LTU comparisons.  In ARM mode
3354      we can also use cmp/cmpeq for GTU/LEU.  GT/LE must be either
3355      reversed or (for constant OP1) adjusted to GE/LT.  Similarly
3356      for GTU/LEU in Thumb mode.  */
3357   if (mode == DImode)
3358     {
3359       rtx tem;
3360
3361       /* To keep things simple, always use the Cirrus cfcmp64 if it is
3362          available.  */
3363       if (TARGET_ARM && TARGET_HARD_FLOAT && TARGET_MAVERICK)
3364         return code;
3365
3366       if (code == GT || code == LE
3367           || (!TARGET_ARM && (code == GTU || code == LEU)))
3368         {
3369           /* Missing comparison.  First try to use an available
3370              comparison.  */
3371           if (GET_CODE (*op1) == CONST_INT)
3372             {
3373               i = INTVAL (*op1);
3374               switch (code)
3375                 {
3376                 case GT:
3377                 case LE:
3378                   if (i != maxval
3379                       && arm_const_double_by_immediates (GEN_INT (i + 1)))
3380                     {
3381                       *op1 = GEN_INT (i + 1);
3382                       return code == GT ? GE : LT;
3383                     }
3384                   break;
3385                 case GTU:
3386                 case LEU:
3387                   if (i != ~((unsigned HOST_WIDE_INT) 0)
3388                       && arm_const_double_by_immediates (GEN_INT (i + 1)))
3389                     {
3390                       *op1 = GEN_INT (i + 1);
3391                       return code == GTU ? GEU : LTU;
3392                     }
3393                   break;
3394                 default:
3395                   gcc_unreachable ();
3396                 }
3397             }
3398
3399           /* If that did not work, reverse the condition.  */
3400           tem = *op0;
3401           *op0 = *op1;
3402           *op1 = tem;
3403           return swap_condition (code);
3404         }
3405
3406       return code;
3407     }
3408
3409   /* Comparisons smaller than DImode.  Only adjust comparisons against
3410      an out-of-range constant.  */
3411   if (GET_CODE (*op1) != CONST_INT
3412       || const_ok_for_arm (INTVAL (*op1))
3413       || const_ok_for_arm (- INTVAL (*op1)))
3414     return code;
3415
3416   i = INTVAL (*op1);
3417
3418   switch (code)
3419     {
3420     case EQ:
3421     case NE:
3422       return code;
3423
3424     case GT:
3425     case LE:
3426       if (i != maxval
3427           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
3428         {
3429           *op1 = GEN_INT (i + 1);
3430           return code == GT ? GE : LT;
3431         }
3432       break;
3433
3434     case GE:
3435     case LT:
3436       if (i != ~maxval
3437           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
3438         {
3439           *op1 = GEN_INT (i - 1);
3440           return code == GE ? GT : LE;
3441         }
3442       break;
3443
3444     case GTU:
3445     case LEU:
3446       if (i != ~((unsigned HOST_WIDE_INT) 0)
3447           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
3448         {
3449           *op1 = GEN_INT (i + 1);
3450           return code == GTU ? GEU : LTU;
3451         }
3452       break;
3453
3454     case GEU:
3455     case LTU:
3456       if (i != 0
3457           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
3458         {
3459           *op1 = GEN_INT (i - 1);
3460           return code == GEU ? GTU : LEU;
3461         }
3462       break;
3463
3464     default:
3465       gcc_unreachable ();
3466     }
3467
3468   return code;
3469 }
3470
3471
3472 /* Define how to find the value returned by a function.  */
3473
3474 static rtx
3475 arm_function_value(const_tree type, const_tree func,
3476                    bool outgoing ATTRIBUTE_UNUSED)
3477 {
3478   enum machine_mode mode;
3479   int unsignedp ATTRIBUTE_UNUSED;
3480   rtx r ATTRIBUTE_UNUSED;
3481
3482   mode = TYPE_MODE (type);
3483
3484   if (TARGET_AAPCS_BASED)
3485     return aapcs_allocate_return_reg (mode, type, func);
3486
3487   /* Promote integer types.  */
3488   if (INTEGRAL_TYPE_P (type))
3489     mode = arm_promote_function_mode (type, mode, &unsignedp, func, 1);
3490
3491   /* Promotes small structs returned in a register to full-word size
3492      for big-endian AAPCS.  */
3493   if (arm_return_in_msb (type))
3494     {
3495       HOST_WIDE_INT size = int_size_in_bytes (type);
3496       if (size % UNITS_PER_WORD != 0)
3497         {
3498           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
3499           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
3500         }
3501     }
3502
3503   return LIBCALL_VALUE (mode);
3504 }
3505
3506 static int
3507 libcall_eq (const void *p1, const void *p2)
3508 {
3509   return rtx_equal_p ((const_rtx) p1, (const_rtx) p2);
3510 }
3511
3512 static hashval_t
3513 libcall_hash (const void *p1)
3514 {
3515   return hash_rtx ((const_rtx) p1, VOIDmode, NULL, NULL, FALSE);
3516 }
3517
3518 static void
3519 add_libcall (htab_t htab, rtx libcall)
3520 {
3521   *htab_find_slot (htab, libcall, INSERT) = libcall;
3522 }
3523
3524 static bool
3525 arm_libcall_uses_aapcs_base (const_rtx libcall)
3526 {
3527   static bool init_done = false;
3528   static htab_t libcall_htab;
3529
3530   if (!init_done)
3531     {
3532       init_done = true;
3533
3534       libcall_htab = htab_create (31, libcall_hash, libcall_eq,
3535                                   NULL);
3536       add_libcall (libcall_htab,
3537                    convert_optab_libfunc (sfloat_optab, SFmode, SImode));
3538       add_libcall (libcall_htab,
3539                    convert_optab_libfunc (sfloat_optab, DFmode, SImode));
3540       add_libcall (libcall_htab,
3541                    convert_optab_libfunc (sfloat_optab, SFmode, DImode));
3542       add_libcall (libcall_htab,
3543                    convert_optab_libfunc (sfloat_optab, DFmode, DImode));
3544       
3545       add_libcall (libcall_htab,
3546                    convert_optab_libfunc (ufloat_optab, SFmode, SImode));
3547       add_libcall (libcall_htab,
3548                    convert_optab_libfunc (ufloat_optab, DFmode, SImode));
3549       add_libcall (libcall_htab,
3550                    convert_optab_libfunc (ufloat_optab, SFmode, DImode));
3551       add_libcall (libcall_htab,
3552                    convert_optab_libfunc (ufloat_optab, DFmode, DImode));
3553
3554       add_libcall (libcall_htab,
3555                    convert_optab_libfunc (sext_optab, SFmode, HFmode));
3556       add_libcall (libcall_htab,
3557                    convert_optab_libfunc (trunc_optab, HFmode, SFmode));
3558       add_libcall (libcall_htab,
3559                    convert_optab_libfunc (sfix_optab, DImode, DFmode));
3560       add_libcall (libcall_htab,
3561                    convert_optab_libfunc (ufix_optab, DImode, DFmode));
3562       add_libcall (libcall_htab,
3563                    convert_optab_libfunc (sfix_optab, DImode, SFmode));
3564       add_libcall (libcall_htab,
3565                    convert_optab_libfunc (ufix_optab, DImode, SFmode));
3566     }
3567
3568   return libcall && htab_find (libcall_htab, libcall) != NULL;
3569 }
3570
3571 rtx
3572 arm_libcall_value (enum machine_mode mode, const_rtx libcall)
3573 {
3574   if (TARGET_AAPCS_BASED && arm_pcs_default != ARM_PCS_AAPCS
3575       && GET_MODE_CLASS (mode) == MODE_FLOAT)
3576     {
3577       /* The following libcalls return their result in integer registers,
3578          even though they return a floating point value.  */
3579       if (arm_libcall_uses_aapcs_base (libcall))
3580         return gen_rtx_REG (mode, ARG_REGISTER(1));
3581
3582     }
3583
3584   return LIBCALL_VALUE (mode);
3585 }
3586
3587 /* Determine the amount of memory needed to store the possible return
3588    registers of an untyped call.  */
3589 int
3590 arm_apply_result_size (void)
3591 {
3592   int size = 16;
3593
3594   if (TARGET_32BIT)
3595     {
3596       if (TARGET_HARD_FLOAT_ABI)
3597         {
3598           if (TARGET_VFP)
3599             size += 32;
3600           if (TARGET_FPA)
3601             size += 12;
3602           if (TARGET_MAVERICK)
3603             size += 8;
3604         }
3605       if (TARGET_IWMMXT_ABI)
3606         size += 8;
3607     }
3608
3609   return size;
3610 }
3611
3612 /* Decide whether TYPE should be returned in memory (true)
3613    or in a register (false).  FNTYPE is the type of the function making
3614    the call.  */
3615 static bool
3616 arm_return_in_memory (const_tree type, const_tree fntype)
3617 {
3618   HOST_WIDE_INT size;
3619
3620   size = int_size_in_bytes (type);  /* Negative if not fixed size.  */
3621
3622   if (TARGET_AAPCS_BASED)
3623     {
3624       /* Simple, non-aggregate types (ie not including vectors and
3625          complex) are always returned in a register (or registers).
3626          We don't care about which register here, so we can short-cut
3627          some of the detail.  */
3628       if (!AGGREGATE_TYPE_P (type)
3629           && TREE_CODE (type) != VECTOR_TYPE
3630           && TREE_CODE (type) != COMPLEX_TYPE)
3631         return false;
3632
3633       /* Any return value that is no larger than one word can be
3634          returned in r0.  */
3635       if (((unsigned HOST_WIDE_INT) size) <= UNITS_PER_WORD)
3636         return false;
3637
3638       /* Check any available co-processors to see if they accept the
3639          type as a register candidate (VFP, for example, can return
3640          some aggregates in consecutive registers).  These aren't
3641          available if the call is variadic.  */
3642       if (aapcs_select_return_coproc (type, fntype) >= 0)
3643         return false;
3644
3645       /* Vector values should be returned using ARM registers, not
3646          memory (unless they're over 16 bytes, which will break since
3647          we only have four call-clobbered registers to play with).  */
3648       if (TREE_CODE (type) == VECTOR_TYPE)
3649         return (size < 0 || size > (4 * UNITS_PER_WORD));
3650
3651       /* The rest go in memory.  */
3652       return true;
3653     }
3654
3655   if (TREE_CODE (type) == VECTOR_TYPE)
3656     return (size < 0 || size > (4 * UNITS_PER_WORD));
3657
3658   if (!AGGREGATE_TYPE_P (type) &&
3659       (TREE_CODE (type) != VECTOR_TYPE))
3660     /* All simple types are returned in registers.  */
3661     return false;
3662
3663   if (arm_abi != ARM_ABI_APCS)
3664     {
3665       /* ATPCS and later return aggregate types in memory only if they are
3666          larger than a word (or are variable size).  */
3667       return (size < 0 || size > UNITS_PER_WORD);
3668     }
3669
3670   /* For the arm-wince targets we choose to be compatible with Microsoft's
3671      ARM and Thumb compilers, which always return aggregates in memory.  */
3672 #ifndef ARM_WINCE
3673   /* All structures/unions bigger than one word are returned in memory.
3674      Also catch the case where int_size_in_bytes returns -1.  In this case
3675      the aggregate is either huge or of variable size, and in either case
3676      we will want to return it via memory and not in a register.  */
3677   if (size < 0 || size > UNITS_PER_WORD)
3678     return true;
3679
3680   if (TREE_CODE (type) == RECORD_TYPE)
3681     {
3682       tree field;
3683
3684       /* For a struct the APCS says that we only return in a register
3685          if the type is 'integer like' and every addressable element
3686          has an offset of zero.  For practical purposes this means
3687          that the structure can have at most one non bit-field element
3688          and that this element must be the first one in the structure.  */
3689
3690       /* Find the first field, ignoring non FIELD_DECL things which will
3691          have been created by C++.  */
3692       for (field = TYPE_FIELDS (type);
3693            field && TREE_CODE (field) != FIELD_DECL;
3694            field = DECL_CHAIN (field))
3695         continue;
3696
3697       if (field == NULL)
3698         return false; /* An empty structure.  Allowed by an extension to ANSI C.  */
3699
3700       /* Check that the first field is valid for returning in a register.  */
3701
3702       /* ... Floats are not allowed */
3703       if (FLOAT_TYPE_P (TREE_TYPE (field)))
3704         return true;
3705
3706       /* ... Aggregates that are not themselves valid for returning in
3707          a register are not allowed.  */
3708       if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
3709         return true;
3710
3711       /* Now check the remaining fields, if any.  Only bitfields are allowed,
3712          since they are not addressable.  */
3713       for (field = DECL_CHAIN (field);
3714            field;
3715            field = DECL_CHAIN (field))
3716         {
3717           if (TREE_CODE (field) != FIELD_DECL)
3718             continue;
3719
3720           if (!DECL_BIT_FIELD_TYPE (field))
3721             return true;
3722         }
3723
3724       return false;
3725     }
3726
3727   if (TREE_CODE (type) == UNION_TYPE)
3728     {
3729       tree field;
3730
3731       /* Unions can be returned in registers if every element is
3732          integral, or can be returned in an integer register.  */
3733       for (field = TYPE_FIELDS (type);
3734            field;
3735            field = DECL_CHAIN (field))
3736         {
3737           if (TREE_CODE (field) != FIELD_DECL)
3738             continue;
3739
3740           if (FLOAT_TYPE_P (TREE_TYPE (field)))
3741             return true;
3742
3743           if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
3744             return true;
3745         }
3746
3747       return false;
3748     }
3749 #endif /* not ARM_WINCE */
3750
3751   /* Return all other types in memory.  */
3752   return true;
3753 }
3754
3755 /* Indicate whether or not words of a double are in big-endian order.  */
3756
3757 int
3758 arm_float_words_big_endian (void)
3759 {
3760   if (TARGET_MAVERICK)
3761     return 0;
3762
3763   /* For FPA, float words are always big-endian.  For VFP, floats words
3764      follow the memory system mode.  */
3765
3766   if (TARGET_FPA)
3767     {
3768       return 1;
3769     }
3770
3771   if (TARGET_VFP)
3772     return (TARGET_BIG_END ? 1 : 0);
3773
3774   return 1;
3775 }
3776
3777 const struct pcs_attribute_arg
3778 {
3779   const char *arg;
3780   enum arm_pcs value;
3781 } pcs_attribute_args[] =
3782   {
3783     {"aapcs", ARM_PCS_AAPCS},
3784     {"aapcs-vfp", ARM_PCS_AAPCS_VFP},
3785 #if 0
3786     /* We could recognize these, but changes would be needed elsewhere
3787      * to implement them.  */
3788     {"aapcs-iwmmxt", ARM_PCS_AAPCS_IWMMXT},
3789     {"atpcs", ARM_PCS_ATPCS},
3790     {"apcs", ARM_PCS_APCS},
3791 #endif
3792     {NULL, ARM_PCS_UNKNOWN}
3793   };
3794
3795 static enum arm_pcs
3796 arm_pcs_from_attribute (tree attr)
3797 {
3798   const struct pcs_attribute_arg *ptr;
3799   const char *arg;
3800
3801   /* Get the value of the argument.  */
3802   if (TREE_VALUE (attr) == NULL_TREE
3803       || TREE_CODE (TREE_VALUE (attr)) != STRING_CST)
3804     return ARM_PCS_UNKNOWN;
3805
3806   arg = TREE_STRING_POINTER (TREE_VALUE (attr));
3807
3808   /* Check it against the list of known arguments.  */
3809   for (ptr = pcs_attribute_args; ptr->arg != NULL; ptr++)
3810     if (streq (arg, ptr->arg))
3811       return ptr->value;
3812
3813   /* An unrecognized interrupt type.  */
3814   return ARM_PCS_UNKNOWN;
3815 }
3816
3817 /* Get the PCS variant to use for this call.  TYPE is the function's type
3818    specification, DECL is the specific declartion.  DECL may be null if
3819    the call could be indirect or if this is a library call.  */
3820 static enum arm_pcs
3821 arm_get_pcs_model (const_tree type, const_tree decl)
3822 {
3823   bool user_convention = false;
3824   enum arm_pcs user_pcs = arm_pcs_default;
3825   tree attr;
3826
3827   gcc_assert (type);
3828
3829   attr = lookup_attribute ("pcs", TYPE_ATTRIBUTES (type));
3830   if (attr)
3831     {
3832       user_pcs = arm_pcs_from_attribute (TREE_VALUE (attr));
3833       user_convention = true;
3834     }
3835
3836   if (TARGET_AAPCS_BASED)
3837     {
3838       /* Detect varargs functions.  These always use the base rules
3839          (no argument is ever a candidate for a co-processor
3840          register).  */
3841       bool base_rules = stdarg_p (type);
3842       
3843       if (user_convention)
3844         {
3845           if (user_pcs > ARM_PCS_AAPCS_LOCAL)
3846             sorry ("non-AAPCS derived PCS variant");
3847           else if (base_rules && user_pcs != ARM_PCS_AAPCS)
3848             error ("variadic functions must use the base AAPCS variant");
3849         }
3850
3851       if (base_rules)
3852         return ARM_PCS_AAPCS;
3853       else if (user_convention)
3854         return user_pcs;
3855       else if (decl && flag_unit_at_a_time)
3856         {
3857           /* Local functions never leak outside this compilation unit,
3858              so we are free to use whatever conventions are
3859              appropriate.  */
3860           /* FIXME: remove CONST_CAST_TREE when cgraph is constified.  */
3861           struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE(decl));
3862           if (i && i->local)
3863             return ARM_PCS_AAPCS_LOCAL;
3864         }
3865     }
3866   else if (user_convention && user_pcs != arm_pcs_default)
3867     sorry ("PCS variant");
3868
3869   /* For everything else we use the target's default.  */
3870   return arm_pcs_default;
3871 }
3872
3873
3874 static void
3875 aapcs_vfp_cum_init (CUMULATIVE_ARGS *pcum  ATTRIBUTE_UNUSED,
3876                     const_tree fntype ATTRIBUTE_UNUSED,
3877                     rtx libcall ATTRIBUTE_UNUSED, 
3878                     const_tree fndecl ATTRIBUTE_UNUSED)
3879 {
3880   /* Record the unallocated VFP registers.  */
3881   pcum->aapcs_vfp_regs_free = (1 << NUM_VFP_ARG_REGS) - 1;
3882   pcum->aapcs_vfp_reg_alloc = 0;
3883 }
3884
3885 /* Walk down the type tree of TYPE counting consecutive base elements.
3886    If *MODEP is VOIDmode, then set it to the first valid floating point
3887    type.  If a non-floating point type is found, or if a floating point
3888    type that doesn't match a non-VOIDmode *MODEP is found, then return -1,
3889    otherwise return the count in the sub-tree.  */
3890 static int
3891 aapcs_vfp_sub_candidate (const_tree type, enum machine_mode *modep)
3892 {
3893   enum machine_mode mode;
3894   HOST_WIDE_INT size;
3895
3896   switch (TREE_CODE (type))
3897     {
3898     case REAL_TYPE:
3899       mode = TYPE_MODE (type);
3900       if (mode != DFmode && mode != SFmode)
3901         return -1;
3902
3903       if (*modep == VOIDmode)
3904         *modep = mode;
3905
3906       if (*modep == mode)
3907         return 1;
3908
3909       break;
3910
3911     case COMPLEX_TYPE:
3912       mode = TYPE_MODE (TREE_TYPE (type));
3913       if (mode != DFmode && mode != SFmode)
3914         return -1;
3915
3916       if (*modep == VOIDmode)
3917         *modep = mode;
3918
3919       if (*modep == mode)
3920         return 2;
3921
3922       break;
3923
3924     case VECTOR_TYPE:
3925       /* Use V2SImode and V4SImode as representatives of all 64-bit
3926          and 128-bit vector types, whether or not those modes are
3927          supported with the present options.  */
3928       size = int_size_in_bytes (type);
3929       switch (size)
3930         {
3931         case 8:
3932           mode = V2SImode;
3933           break;
3934         case 16:
3935           mode = V4SImode;
3936           break;
3937         default:
3938           return -1;
3939         }
3940
3941       if (*modep == VOIDmode)
3942         *modep = mode;
3943
3944       /* Vector modes are considered to be opaque: two vectors are
3945          equivalent for the purposes of being homogeneous aggregates
3946          if they are the same size.  */
3947       if (*modep == mode)
3948         return 1;
3949
3950       break;
3951
3952     case ARRAY_TYPE:
3953       {
3954         int count;
3955         tree index = TYPE_DOMAIN (type);
3956
3957         /* Can't handle incomplete types.  */
3958         if (!COMPLETE_TYPE_P(type))
3959           return -1;
3960
3961         count = aapcs_vfp_sub_candidate (TREE_TYPE (type), modep);
3962         if (count == -1
3963             || !index
3964             || !TYPE_MAX_VALUE (index)
3965             || !host_integerp (TYPE_MAX_VALUE (index), 1)
3966             || !TYPE_MIN_VALUE (index)
3967             || !host_integerp (TYPE_MIN_VALUE (index), 1)
3968             || count < 0)
3969           return -1;
3970
3971         count *= (1 + tree_low_cst (TYPE_MAX_VALUE (index), 1)
3972                       - tree_low_cst (TYPE_MIN_VALUE (index), 1));
3973
3974         /* There must be no padding.  */
3975         if (!host_integerp (TYPE_SIZE (type), 1)
3976             || (tree_low_cst (TYPE_SIZE (type), 1)
3977                 != count * GET_MODE_BITSIZE (*modep)))
3978           return -1;
3979
3980         return count;
3981       }
3982       
3983     case RECORD_TYPE:
3984       {
3985         int count = 0;
3986         int sub_count;
3987         tree field;
3988
3989         /* Can't handle incomplete types.  */
3990         if (!COMPLETE_TYPE_P(type))
3991           return -1;
3992
3993         for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3994           {
3995             if (TREE_CODE (field) != FIELD_DECL)
3996               continue;
3997
3998             sub_count = aapcs_vfp_sub_candidate (TREE_TYPE (field), modep);
3999             if (sub_count < 0)
4000               return -1;
4001             count += sub_count;
4002           }
4003
4004         /* There must be no padding.  */
4005         if (!host_integerp (TYPE_SIZE (type), 1)
4006             || (tree_low_cst (TYPE_SIZE (type), 1)
4007                 != count * GET_MODE_BITSIZE (*modep)))
4008           return -1;
4009
4010         return count;
4011       }
4012
4013     case UNION_TYPE:
4014     case QUAL_UNION_TYPE:
4015       {
4016         /* These aren't very interesting except in a degenerate case.  */
4017         int count = 0;
4018         int sub_count;
4019         tree field;
4020
4021         /* Can't handle incomplete types.  */
4022         if (!COMPLETE_TYPE_P(type))
4023           return -1;
4024
4025         for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4026           {
4027             if (TREE_CODE (field) != FIELD_DECL)
4028               continue;
4029
4030             sub_count = aapcs_vfp_sub_candidate (TREE_TYPE (field), modep);
4031             if (sub_count < 0)
4032               return -1;
4033             count = count > sub_count ? count : sub_count;
4034           }
4035
4036         /* There must be no padding.  */
4037         if (!host_integerp (TYPE_SIZE (type), 1)
4038             || (tree_low_cst (TYPE_SIZE (type), 1)
4039                 != count * GET_MODE_BITSIZE (*modep)))
4040           return -1;
4041
4042         return count;
4043       }
4044
4045     default:
4046       break;
4047     }
4048
4049   return -1;
4050 }
4051
4052 /* Return true if PCS_VARIANT should use VFP registers.  */
4053 static bool
4054 use_vfp_abi (enum arm_pcs pcs_variant, bool is_double)
4055 {
4056   if (pcs_variant == ARM_PCS_AAPCS_VFP)
4057     {
4058       static bool seen_thumb1_vfp = false;
4059
4060       if (TARGET_THUMB1 && !seen_thumb1_vfp)
4061         {
4062           sorry ("Thumb-1 hard-float VFP ABI");
4063           /* sorry() is not immediately fatal, so only display this once.  */
4064           seen_thumb1_vfp = true;
4065         }
4066
4067       return true;
4068     }
4069
4070   if (pcs_variant != ARM_PCS_AAPCS_LOCAL)
4071     return false;
4072
4073   return (TARGET_32BIT && TARGET_VFP && TARGET_HARD_FLOAT &&
4074           (TARGET_VFP_DOUBLE || !is_double));
4075 }
4076
4077 static bool
4078 aapcs_vfp_is_call_or_return_candidate (enum arm_pcs pcs_variant,
4079                                        enum machine_mode mode, const_tree type,
4080                                        enum machine_mode *base_mode, int *count)
4081 {
4082   enum machine_mode new_mode = VOIDmode;
4083
4084   if (GET_MODE_CLASS (mode) == MODE_FLOAT
4085       || GET_MODE_CLASS (mode) == MODE_VECTOR_INT
4086       || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
4087     {
4088       *count = 1;
4089       new_mode = mode;
4090     }
4091   else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
4092     {
4093       *count = 2;
4094       new_mode = (mode == DCmode ? DFmode : SFmode);
4095     }
4096   else if (type && (mode == BLKmode || TREE_CODE (type) == VECTOR_TYPE))
4097     {
4098       int ag_count = aapcs_vfp_sub_candidate (type, &new_mode);
4099
4100       if (ag_count > 0 && ag_count <= 4)
4101         *count = ag_count;
4102       else
4103         return false;
4104     }
4105   else
4106     return false;
4107
4108
4109   if (!use_vfp_abi (pcs_variant, ARM_NUM_REGS (new_mode) > 1))
4110     return false;
4111
4112   *base_mode = new_mode;
4113   return true;
4114 }
4115
4116 static bool
4117 aapcs_vfp_is_return_candidate (enum arm_pcs pcs_variant,
4118                                enum machine_mode mode, const_tree type)
4119 {
4120   int count ATTRIBUTE_UNUSED;
4121   enum machine_mode ag_mode ATTRIBUTE_UNUSED;
4122
4123   if (!use_vfp_abi (pcs_variant, false))
4124     return false;
4125   return aapcs_vfp_is_call_or_return_candidate (pcs_variant, mode, type,
4126                                                 &ag_mode, &count);
4127 }
4128
4129 static bool
4130 aapcs_vfp_is_call_candidate (CUMULATIVE_ARGS *pcum, enum machine_mode mode, 
4131                              const_tree type)
4132 {
4133   if (!use_vfp_abi (pcum->pcs_variant, false))
4134     return false;
4135
4136   return aapcs_vfp_is_call_or_return_candidate (pcum->pcs_variant, mode, type,
4137                                                 &pcum->aapcs_vfp_rmode,
4138                                                 &pcum->aapcs_vfp_rcount);
4139 }
4140
4141 static bool
4142 aapcs_vfp_allocate (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4143                     const_tree type  ATTRIBUTE_UNUSED)
4144 {
4145   int shift = GET_MODE_SIZE (pcum->aapcs_vfp_rmode) / GET_MODE_SIZE (SFmode);
4146   unsigned mask = (1 << (shift * pcum->aapcs_vfp_rcount)) - 1;
4147   int regno;
4148   
4149   for (regno = 0; regno < NUM_VFP_ARG_REGS; regno += shift)
4150     if (((pcum->aapcs_vfp_regs_free >> regno) & mask) == mask)
4151       {
4152         pcum->aapcs_vfp_reg_alloc = mask << regno;
4153         if (mode == BLKmode || (mode == TImode && !TARGET_NEON))
4154           {
4155             int i;
4156             int rcount = pcum->aapcs_vfp_rcount;
4157             int rshift = shift;
4158             enum machine_mode rmode = pcum->aapcs_vfp_rmode;
4159             rtx par;
4160             if (!TARGET_NEON)
4161               {
4162                 /* Avoid using unsupported vector modes.  */
4163                 if (rmode == V2SImode)
4164                   rmode = DImode;
4165                 else if (rmode == V4SImode)
4166                   {
4167                     rmode = DImode;
4168                     rcount *= 2;
4169                     rshift /= 2;
4170                   }
4171               }
4172             par = gen_rtx_PARALLEL (mode, rtvec_alloc (rcount));
4173             for (i = 0; i < rcount; i++)
4174               {
4175                 rtx tmp = gen_rtx_REG (rmode, 
4176                                        FIRST_VFP_REGNUM + regno + i * rshift);
4177                 tmp = gen_rtx_EXPR_LIST
4178                   (VOIDmode, tmp, 
4179                    GEN_INT (i * GET_MODE_SIZE (rmode)));
4180                 XVECEXP (par, 0, i) = tmp;
4181               }
4182
4183             pcum->aapcs_reg = par;
4184           }
4185         else
4186           pcum->aapcs_reg = gen_rtx_REG (mode, FIRST_VFP_REGNUM + regno);
4187         return true;
4188       }
4189   return false;
4190 }
4191
4192 static rtx
4193 aapcs_vfp_allocate_return_reg (enum arm_pcs pcs_variant ATTRIBUTE_UNUSED,
4194                                enum machine_mode mode,
4195                                const_tree type ATTRIBUTE_UNUSED)
4196 {
4197   if (!use_vfp_abi (pcs_variant, false))
4198     return false;
4199
4200   if (mode == BLKmode || (mode == TImode && !TARGET_NEON))
4201     {
4202       int count;
4203       enum machine_mode ag_mode;
4204       int i;
4205       rtx par;
4206       int shift;
4207       
4208       aapcs_vfp_is_call_or_return_candidate (pcs_variant, mode, type,
4209                                              &ag_mode, &count);
4210
4211       if (!TARGET_NEON)
4212         {
4213           if (ag_mode == V2SImode)
4214             ag_mode = DImode;
4215           else if (ag_mode == V4SImode)
4216             {
4217               ag_mode = DImode;
4218               count *= 2;
4219             }
4220         }
4221       shift = GET_MODE_SIZE(ag_mode) / GET_MODE_SIZE(SFmode);
4222       par = gen_rtx_PARALLEL (mode, rtvec_alloc (count));
4223       for (i = 0; i < count; i++)
4224         {
4225           rtx tmp = gen_rtx_REG (ag_mode, FIRST_VFP_REGNUM + i * shift);
4226           tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, 
4227                                    GEN_INT (i * GET_MODE_SIZE (ag_mode)));
4228           XVECEXP (par, 0, i) = tmp;
4229         }
4230
4231       return par;
4232     }
4233
4234   return gen_rtx_REG (mode, FIRST_VFP_REGNUM);
4235 }
4236
4237 static void
4238 aapcs_vfp_advance (CUMULATIVE_ARGS *pcum  ATTRIBUTE_UNUSED,
4239                    enum machine_mode mode  ATTRIBUTE_UNUSED,
4240                    const_tree type  ATTRIBUTE_UNUSED)
4241 {
4242   pcum->aapcs_vfp_regs_free &= ~pcum->aapcs_vfp_reg_alloc;
4243   pcum->aapcs_vfp_reg_alloc = 0;
4244   return;
4245 }
4246
4247 #define AAPCS_CP(X)                             \
4248   {                                             \
4249     aapcs_ ## X ## _cum_init,                   \
4250     aapcs_ ## X ## _is_call_candidate,          \
4251     aapcs_ ## X ## _allocate,                   \
4252     aapcs_ ## X ## _is_return_candidate,        \
4253     aapcs_ ## X ## _allocate_return_reg,        \
4254     aapcs_ ## X ## _advance                     \
4255   }
4256
4257 /* Table of co-processors that can be used to pass arguments in
4258    registers.  Idealy no arugment should be a candidate for more than
4259    one co-processor table entry, but the table is processed in order
4260    and stops after the first match.  If that entry then fails to put
4261    the argument into a co-processor register, the argument will go on
4262    the stack.  */
4263 static struct 
4264 {
4265   /* Initialize co-processor related state in CUMULATIVE_ARGS structure.  */
4266   void (*cum_init) (CUMULATIVE_ARGS *, const_tree, rtx, const_tree);
4267
4268   /* Return true if an argument of mode MODE (or type TYPE if MODE is
4269      BLKmode) is a candidate for this co-processor's registers; this
4270      function should ignore any position-dependent state in
4271      CUMULATIVE_ARGS and only use call-type dependent information.  */
4272   bool (*is_call_candidate) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4273
4274   /* Return true if the argument does get a co-processor register; it
4275      should set aapcs_reg to an RTX of the register allocated as is
4276      required for a return from FUNCTION_ARG.  */
4277   bool (*allocate) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4278
4279   /* Return true if a result of mode MODE (or type TYPE if MODE is
4280      BLKmode) is can be returned in this co-processor's registers.  */
4281   bool (*is_return_candidate) (enum arm_pcs, enum machine_mode, const_tree);
4282
4283   /* Allocate and return an RTX element to hold the return type of a
4284      call, this routine must not fail and will only be called if
4285      is_return_candidate returned true with the same parameters.  */
4286   rtx (*allocate_return_reg) (enum arm_pcs, enum machine_mode, const_tree);
4287
4288   /* Finish processing this argument and prepare to start processing
4289      the next one.  */
4290   void (*advance) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4291 } aapcs_cp_arg_layout[ARM_NUM_COPROC_SLOTS] =
4292   {
4293     AAPCS_CP(vfp)
4294   };
4295
4296 #undef AAPCS_CP
4297
4298 static int
4299 aapcs_select_call_coproc (CUMULATIVE_ARGS *pcum, enum machine_mode mode, 
4300                           const_tree type)
4301 {
4302   int i;
4303
4304   for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4305     if (aapcs_cp_arg_layout[i].is_call_candidate (pcum, mode, type))
4306       return i;
4307
4308   return -1;
4309 }
4310
4311 static int
4312 aapcs_select_return_coproc (const_tree type, const_tree fntype)
4313 {
4314   /* We aren't passed a decl, so we can't check that a call is local.
4315      However, it isn't clear that that would be a win anyway, since it
4316      might limit some tail-calling opportunities.  */
4317   enum arm_pcs pcs_variant;
4318
4319   if (fntype)
4320     {
4321       const_tree fndecl = NULL_TREE;
4322
4323       if (TREE_CODE (fntype) == FUNCTION_DECL)
4324         {
4325           fndecl = fntype;
4326           fntype = TREE_TYPE (fntype);
4327         }
4328
4329       pcs_variant = arm_get_pcs_model (fntype, fndecl);
4330     }
4331   else
4332     pcs_variant = arm_pcs_default;
4333
4334   if (pcs_variant != ARM_PCS_AAPCS)
4335     {
4336       int i;
4337
4338       for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4339         if (aapcs_cp_arg_layout[i].is_return_candidate (pcs_variant, 
4340                                                         TYPE_MODE (type),
4341                                                         type))
4342           return i;
4343     }
4344   return -1;
4345 }
4346
4347 static rtx
4348 aapcs_allocate_return_reg (enum machine_mode mode, const_tree type,
4349                            const_tree fntype)
4350 {
4351   /* We aren't passed a decl, so we can't check that a call is local.
4352      However, it isn't clear that that would be a win anyway, since it
4353      might limit some tail-calling opportunities.  */
4354   enum arm_pcs pcs_variant;
4355   int unsignedp ATTRIBUTE_UNUSED;
4356
4357   if (fntype)
4358     {
4359       const_tree fndecl = NULL_TREE;
4360
4361       if (TREE_CODE (fntype) == FUNCTION_DECL)
4362         {
4363           fndecl = fntype;
4364           fntype = TREE_TYPE (fntype);
4365         }
4366
4367       pcs_variant = arm_get_pcs_model (fntype, fndecl);
4368     }
4369   else
4370     pcs_variant = arm_pcs_default;
4371
4372   /* Promote integer types.  */
4373   if (type && INTEGRAL_TYPE_P (type))
4374     mode = arm_promote_function_mode (type, mode, &unsignedp, fntype, 1);
4375
4376   if (pcs_variant != ARM_PCS_AAPCS)
4377     {
4378       int i;
4379
4380       for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4381         if (aapcs_cp_arg_layout[i].is_return_candidate (pcs_variant, mode,
4382                                                         type))
4383           return aapcs_cp_arg_layout[i].allocate_return_reg (pcs_variant,
4384                                                              mode, type);
4385     }
4386
4387   /* Promotes small structs returned in a register to full-word size
4388      for big-endian AAPCS.  */
4389   if (type && arm_return_in_msb (type))
4390     {
4391       HOST_WIDE_INT size = int_size_in_bytes (type);
4392       if (size % UNITS_PER_WORD != 0)
4393         {
4394           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
4395           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
4396         }
4397     }
4398
4399   return gen_rtx_REG (mode, R0_REGNUM);
4400 }
4401
4402 rtx
4403 aapcs_libcall_value (enum machine_mode mode)
4404 {
4405   return aapcs_allocate_return_reg (mode, NULL_TREE, NULL_TREE);
4406 }
4407
4408 /* Lay out a function argument using the AAPCS rules.  The rule
4409    numbers referred to here are those in the AAPCS.  */
4410 static void
4411 aapcs_layout_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4412                   const_tree type, bool named)
4413 {
4414   int nregs, nregs2;
4415   int ncrn;
4416
4417   /* We only need to do this once per argument.  */
4418   if (pcum->aapcs_arg_processed)
4419     return;
4420
4421   pcum->aapcs_arg_processed = true;
4422
4423   /* Special case: if named is false then we are handling an incoming
4424      anonymous argument which is on the stack.  */
4425   if (!named)
4426     return;
4427   
4428   /* Is this a potential co-processor register candidate?  */
4429   if (pcum->pcs_variant != ARM_PCS_AAPCS)
4430     {
4431       int slot = aapcs_select_call_coproc (pcum, mode, type);
4432       pcum->aapcs_cprc_slot = slot;
4433
4434       /* We don't have to apply any of the rules from part B of the
4435          preparation phase, these are handled elsewhere in the
4436          compiler.  */
4437
4438       if (slot >= 0)
4439         {
4440           /* A Co-processor register candidate goes either in its own
4441              class of registers or on the stack.  */
4442           if (!pcum->aapcs_cprc_failed[slot])
4443             {
4444               /* C1.cp - Try to allocate the argument to co-processor
4445                  registers.  */
4446               if (aapcs_cp_arg_layout[slot].allocate (pcum, mode, type))
4447                 return;
4448
4449               /* C2.cp - Put the argument on the stack and note that we
4450                  can't assign any more candidates in this slot.  We also
4451                  need to note that we have allocated stack space, so that
4452                  we won't later try to split a non-cprc candidate between
4453                  core registers and the stack.  */
4454               pcum->aapcs_cprc_failed[slot] = true;
4455               pcum->can_split = false;
4456             }
4457
4458           /* We didn't get a register, so this argument goes on the
4459              stack.  */
4460           gcc_assert (pcum->can_split == false);
4461           return;
4462         }
4463     }
4464
4465   /* C3 - For double-word aligned arguments, round the NCRN up to the
4466      next even number.  */
4467   ncrn = pcum->aapcs_ncrn;
4468   if ((ncrn & 1) && arm_needs_doubleword_align (mode, type))
4469     ncrn++;
4470
4471   nregs = ARM_NUM_REGS2(mode, type);
4472
4473   /* Sigh, this test should really assert that nregs > 0, but a GCC
4474      extension allows empty structs and then gives them empty size; it
4475      then allows such a structure to be passed by value.  For some of
4476      the code below we have to pretend that such an argument has
4477      non-zero size so that we 'locate' it correctly either in
4478      registers or on the stack.  */
4479   gcc_assert (nregs >= 0);
4480
4481   nregs2 = nregs ? nregs : 1;
4482
4483   /* C4 - Argument fits entirely in core registers.  */
4484   if (ncrn + nregs2 <= NUM_ARG_REGS)
4485     {
4486       pcum->aapcs_reg = gen_rtx_REG (mode, ncrn);
4487       pcum->aapcs_next_ncrn = ncrn + nregs;
4488       return;
4489     }
4490
4491   /* C5 - Some core registers left and there are no arguments already
4492      on the stack: split this argument between the remaining core
4493      registers and the stack.  */
4494   if (ncrn < NUM_ARG_REGS && pcum->can_split)
4495     {
4496       pcum->aapcs_reg = gen_rtx_REG (mode, ncrn);
4497       pcum->aapcs_next_ncrn = NUM_ARG_REGS;
4498       pcum->aapcs_partial = (NUM_ARG_REGS - ncrn) * UNITS_PER_WORD;
4499       return;
4500     }
4501
4502   /* C6 - NCRN is set to 4.  */
4503   pcum->aapcs_next_ncrn = NUM_ARG_REGS;
4504
4505   /* C7,C8 - arugment goes on the stack.  We have nothing to do here.  */
4506   return;
4507 }
4508
4509 /* Initialize a variable CUM of type CUMULATIVE_ARGS
4510    for a call to a function whose data type is FNTYPE.
4511    For a library call, FNTYPE is NULL.  */
4512 void
4513 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
4514                           rtx libname,
4515                           tree fndecl ATTRIBUTE_UNUSED)
4516 {
4517   /* Long call handling.  */
4518   if (fntype)
4519     pcum->pcs_variant = arm_get_pcs_model (fntype, fndecl);
4520   else
4521     pcum->pcs_variant = arm_pcs_default;
4522
4523   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4524     {
4525       if (arm_libcall_uses_aapcs_base (libname))
4526         pcum->pcs_variant = ARM_PCS_AAPCS;
4527  
4528       pcum->aapcs_ncrn = pcum->aapcs_next_ncrn = 0;
4529       pcum->aapcs_reg = NULL_RTX;
4530       pcum->aapcs_partial = 0;
4531       pcum->aapcs_arg_processed = false;
4532       pcum->aapcs_cprc_slot = -1;
4533       pcum->can_split = true;
4534
4535       if (pcum->pcs_variant != ARM_PCS_AAPCS)
4536         {
4537           int i;
4538
4539           for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4540             {
4541               pcum->aapcs_cprc_failed[i] = false;
4542               aapcs_cp_arg_layout[i].cum_init (pcum, fntype, libname, fndecl);
4543             }
4544         }
4545       return;
4546     }
4547
4548   /* Legacy ABIs */
4549
4550   /* On the ARM, the offset starts at 0.  */
4551   pcum->nregs = 0;
4552   pcum->iwmmxt_nregs = 0;
4553   pcum->can_split = true;
4554
4555   /* Varargs vectors are treated the same as long long.
4556      named_count avoids having to change the way arm handles 'named' */
4557   pcum->named_count = 0;
4558   pcum->nargs = 0;
4559
4560   if (TARGET_REALLY_IWMMXT && fntype)
4561     {
4562       tree fn_arg;
4563
4564       for (fn_arg = TYPE_ARG_TYPES (fntype);
4565            fn_arg;
4566            fn_arg = TREE_CHAIN (fn_arg))
4567         pcum->named_count += 1;
4568
4569       if (! pcum->named_count)
4570         pcum->named_count = INT_MAX;
4571     }
4572 }
4573
4574
4575 /* Return true if mode/type need doubleword alignment.  */
4576 static bool
4577 arm_needs_doubleword_align (enum machine_mode mode, const_tree type)
4578 {
4579   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
4580           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
4581 }
4582
4583
4584 /* Determine where to put an argument to a function.
4585    Value is zero to push the argument on the stack,
4586    or a hard register in which to store the argument.
4587
4588    MODE is the argument's machine mode.
4589    TYPE is the data type of the argument (as a tree).
4590     This is null for libcalls where that information may
4591     not be available.
4592    CUM is a variable of type CUMULATIVE_ARGS which gives info about
4593     the preceding args and about the function being called.
4594    NAMED is nonzero if this argument is a named parameter
4595     (otherwise it is an extra parameter matching an ellipsis).
4596
4597    On the ARM, normally the first 16 bytes are passed in registers r0-r3; all
4598    other arguments are passed on the stack.  If (NAMED == 0) (which happens
4599    only in assign_parms, since TARGET_SETUP_INCOMING_VARARGS is
4600    defined), say it is passed in the stack (function_prologue will
4601    indeed make it pass in the stack if necessary).  */
4602
4603 static rtx
4604 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4605                   const_tree type, bool named)
4606 {
4607   int nregs;
4608
4609   /* Handle the special case quickly.  Pick an arbitrary value for op2 of
4610      a call insn (op3 of a call_value insn).  */
4611   if (mode == VOIDmode)
4612     return const0_rtx;
4613
4614   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4615     {
4616       aapcs_layout_arg (pcum, mode, type, named);
4617       return pcum->aapcs_reg;
4618     }
4619
4620   /* Varargs vectors are treated the same as long long.
4621      named_count avoids having to change the way arm handles 'named' */
4622   if (TARGET_IWMMXT_ABI
4623       && arm_vector_mode_supported_p (mode)
4624       && pcum->named_count > pcum->nargs + 1)
4625     {
4626       if (pcum->iwmmxt_nregs <= 9)
4627         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
4628       else
4629         {
4630           pcum->can_split = false;
4631           return NULL_RTX;
4632         }
4633     }
4634
4635   /* Put doubleword aligned quantities in even register pairs.  */
4636   if (pcum->nregs & 1
4637       && ARM_DOUBLEWORD_ALIGN
4638       && arm_needs_doubleword_align (mode, type))
4639     pcum->nregs++;
4640
4641   /* Only allow splitting an arg between regs and memory if all preceding
4642      args were allocated to regs.  For args passed by reference we only count
4643      the reference pointer.  */
4644   if (pcum->can_split)
4645     nregs = 1;
4646   else
4647     nregs = ARM_NUM_REGS2 (mode, type);
4648
4649   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
4650     return NULL_RTX;
4651
4652   return gen_rtx_REG (mode, pcum->nregs);
4653 }
4654
4655 static unsigned int
4656 arm_function_arg_boundary (enum machine_mode mode, const_tree type)
4657 {
4658   return (ARM_DOUBLEWORD_ALIGN && arm_needs_doubleword_align (mode, type)
4659           ? DOUBLEWORD_ALIGNMENT
4660           : PARM_BOUNDARY);
4661 }
4662
4663 static int
4664 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4665                        tree type, bool named)
4666 {
4667   int nregs = pcum->nregs;
4668
4669   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4670     {
4671       aapcs_layout_arg (pcum, mode, type, named);
4672       return pcum->aapcs_partial;
4673     }
4674
4675   if (TARGET_IWMMXT_ABI && arm_vector_mode_supported_p (mode))
4676     return 0;
4677
4678   if (NUM_ARG_REGS > nregs
4679       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
4680       && pcum->can_split)
4681     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
4682
4683   return 0;
4684 }
4685
4686 /* Update the data in PCUM to advance over an argument
4687    of mode MODE and data type TYPE.
4688    (TYPE is null for libcalls where that information may not be available.)  */
4689
4690 static void
4691 arm_function_arg_advance (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4692                           const_tree type, bool named)
4693 {
4694   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4695     {
4696       aapcs_layout_arg (pcum, mode, type, named);
4697
4698       if (pcum->aapcs_cprc_slot >= 0)
4699         {
4700           aapcs_cp_arg_layout[pcum->aapcs_cprc_slot].advance (pcum, mode,
4701                                                               type);
4702           pcum->aapcs_cprc_slot = -1;
4703         }
4704
4705       /* Generic stuff.  */
4706       pcum->aapcs_arg_processed = false;
4707       pcum->aapcs_ncrn = pcum->aapcs_next_ncrn;
4708       pcum->aapcs_reg = NULL_RTX;
4709       pcum->aapcs_partial = 0;
4710     }
4711   else
4712     {
4713       pcum->nargs += 1;
4714       if (arm_vector_mode_supported_p (mode)
4715           && pcum->named_count > pcum->nargs
4716           && TARGET_IWMMXT_ABI)
4717         pcum->iwmmxt_nregs += 1;
4718       else
4719         pcum->nregs += ARM_NUM_REGS2 (mode, type);
4720     }
4721 }
4722
4723 /* Variable sized types are passed by reference.  This is a GCC
4724    extension to the ARM ABI.  */
4725
4726 static bool
4727 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
4728                        enum machine_mode mode ATTRIBUTE_UNUSED,
4729                        const_tree type, bool named ATTRIBUTE_UNUSED)
4730 {
4731   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
4732 }
4733 \f
4734 /* Encode the current state of the #pragma [no_]long_calls.  */
4735 typedef enum
4736 {
4737   OFF,          /* No #pragma [no_]long_calls is in effect.  */
4738   LONG,         /* #pragma long_calls is in effect.  */
4739   SHORT         /* #pragma no_long_calls is in effect.  */
4740 } arm_pragma_enum;
4741
4742 static arm_pragma_enum arm_pragma_long_calls = OFF;
4743
4744 void
4745 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
4746 {
4747   arm_pragma_long_calls = LONG;
4748 }
4749
4750 void
4751 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
4752 {
4753   arm_pragma_long_calls = SHORT;
4754 }
4755
4756 void
4757 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
4758 {
4759   arm_pragma_long_calls = OFF;
4760 }
4761 \f
4762 /* Handle an attribute requiring a FUNCTION_DECL;
4763    arguments as in struct attribute_spec.handler.  */
4764 static tree
4765 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
4766                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4767 {
4768   if (TREE_CODE (*node) != FUNCTION_DECL)
4769     {
4770       warning (OPT_Wattributes, "%qE attribute only applies to functions",
4771                name);
4772       *no_add_attrs = true;
4773     }
4774
4775   return NULL_TREE;
4776 }
4777
4778 /* Handle an "interrupt" or "isr" attribute;
4779    arguments as in struct attribute_spec.handler.  */
4780 static tree
4781 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
4782                           bool *no_add_attrs)
4783 {
4784   if (DECL_P (*node))
4785     {
4786       if (TREE_CODE (*node) != FUNCTION_DECL)
4787         {
4788           warning (OPT_Wattributes, "%qE attribute only applies to functions",
4789                    name);
4790           *no_add_attrs = true;
4791         }
4792       /* FIXME: the argument if any is checked for type attributes;
4793          should it be checked for decl ones?  */
4794     }
4795   else
4796     {
4797       if (TREE_CODE (*node) == FUNCTION_TYPE
4798           || TREE_CODE (*node) == METHOD_TYPE)
4799         {
4800           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
4801             {
4802               warning (OPT_Wattributes, "%qE attribute ignored",
4803                        name);
4804               *no_add_attrs = true;
4805             }
4806         }
4807       else if (TREE_CODE (*node) == POINTER_TYPE
4808                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
4809                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
4810                && arm_isr_value (args) != ARM_FT_UNKNOWN)
4811         {
4812           *node = build_variant_type_copy (*node);
4813           TREE_TYPE (*node) = build_type_attribute_variant
4814             (TREE_TYPE (*node),
4815              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
4816           *no_add_attrs = true;
4817         }
4818       else
4819         {
4820           /* Possibly pass this attribute on from the type to a decl.  */
4821           if (flags & ((int) ATTR_FLAG_DECL_NEXT
4822                        | (int) ATTR_FLAG_FUNCTION_NEXT
4823                        | (int) ATTR_FLAG_ARRAY_NEXT))
4824             {
4825               *no_add_attrs = true;
4826               return tree_cons (name, args, NULL_TREE);
4827             }
4828           else
4829             {
4830               warning (OPT_Wattributes, "%qE attribute ignored",
4831                        name);
4832             }
4833         }
4834     }
4835
4836   return NULL_TREE;
4837 }
4838
4839 /* Handle a "pcs" attribute; arguments as in struct
4840    attribute_spec.handler.  */
4841 static tree
4842 arm_handle_pcs_attribute (tree *node ATTRIBUTE_UNUSED, tree name, tree args,
4843                           int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4844 {
4845   if (arm_pcs_from_attribute (args) == ARM_PCS_UNKNOWN)
4846     {
4847       warning (OPT_Wattributes, "%qE attribute ignored", name);
4848       *no_add_attrs = true;
4849     }
4850   return NULL_TREE;
4851 }
4852
4853 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
4854 /* Handle the "notshared" attribute.  This attribute is another way of
4855    requesting hidden visibility.  ARM's compiler supports
4856    "__declspec(notshared)"; we support the same thing via an
4857    attribute.  */
4858
4859 static tree
4860 arm_handle_notshared_attribute (tree *node,
4861                                 tree name ATTRIBUTE_UNUSED,
4862                                 tree args ATTRIBUTE_UNUSED,
4863                                 int flags ATTRIBUTE_UNUSED,
4864                                 bool *no_add_attrs)
4865 {
4866   tree decl = TYPE_NAME (*node);
4867
4868   if (decl)
4869     {
4870       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
4871       DECL_VISIBILITY_SPECIFIED (decl) = 1;
4872       *no_add_attrs = false;
4873     }
4874   return NULL_TREE;
4875 }
4876 #endif
4877
4878 /* Return 0 if the attributes for two types are incompatible, 1 if they
4879    are compatible, and 2 if they are nearly compatible (which causes a
4880    warning to be generated).  */
4881 static int
4882 arm_comp_type_attributes (const_tree type1, const_tree type2)
4883 {
4884   int l1, l2, s1, s2;
4885
4886   /* Check for mismatch of non-default calling convention.  */
4887   if (TREE_CODE (type1) != FUNCTION_TYPE)
4888     return 1;
4889
4890   /* Check for mismatched call attributes.  */
4891   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
4892   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
4893   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
4894   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
4895
4896   /* Only bother to check if an attribute is defined.  */
4897   if (l1 | l2 | s1 | s2)
4898     {
4899       /* If one type has an attribute, the other must have the same attribute.  */
4900       if ((l1 != l2) || (s1 != s2))
4901         return 0;
4902
4903       /* Disallow mixed attributes.  */
4904       if ((l1 & s2) || (l2 & s1))
4905         return 0;
4906     }
4907
4908   /* Check for mismatched ISR attribute.  */
4909   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
4910   if (! l1)
4911     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
4912   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
4913   if (! l2)
4914     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
4915   if (l1 != l2)
4916     return 0;
4917
4918   return 1;
4919 }
4920
4921 /*  Assigns default attributes to newly defined type.  This is used to
4922     set short_call/long_call attributes for function types of
4923     functions defined inside corresponding #pragma scopes.  */
4924 static void
4925 arm_set_default_type_attributes (tree type)
4926 {
4927   /* Add __attribute__ ((long_call)) to all functions, when
4928      inside #pragma long_calls or __attribute__ ((short_call)),
4929      when inside #pragma no_long_calls.  */
4930   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
4931     {
4932       tree type_attr_list, attr_name;
4933       type_attr_list = TYPE_ATTRIBUTES (type);
4934
4935       if (arm_pragma_long_calls == LONG)
4936         attr_name = get_identifier ("long_call");
4937       else if (arm_pragma_long_calls == SHORT)
4938         attr_name = get_identifier ("short_call");
4939       else
4940         return;
4941
4942       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
4943       TYPE_ATTRIBUTES (type) = type_attr_list;
4944     }
4945 }
4946 \f
4947 /* Return true if DECL is known to be linked into section SECTION.  */
4948
4949 static bool
4950 arm_function_in_section_p (tree decl, section *section)
4951 {
4952   /* We can only be certain about functions defined in the same
4953      compilation unit.  */
4954   if (!TREE_STATIC (decl))
4955     return false;
4956
4957   /* Make sure that SYMBOL always binds to the definition in this
4958      compilation unit.  */
4959   if (!targetm.binds_local_p (decl))
4960     return false;
4961
4962   /* If DECL_SECTION_NAME is set, assume it is trustworthy.  */
4963   if (!DECL_SECTION_NAME (decl))
4964     {
4965       /* Make sure that we will not create a unique section for DECL.  */
4966       if (flag_function_sections || DECL_ONE_ONLY (decl))
4967         return false;
4968     }
4969
4970   return function_section (decl) == section;
4971 }
4972
4973 /* Return nonzero if a 32-bit "long_call" should be generated for
4974    a call from the current function to DECL.  We generate a long_call
4975    if the function:
4976
4977         a.  has an __attribute__((long call))
4978      or b.  is within the scope of a #pragma long_calls
4979      or c.  the -mlong-calls command line switch has been specified
4980
4981    However we do not generate a long call if the function:
4982
4983         d.  has an __attribute__ ((short_call))
4984      or e.  is inside the scope of a #pragma no_long_calls
4985      or f.  is defined in the same section as the current function.  */
4986
4987 bool
4988 arm_is_long_call_p (tree decl)
4989 {
4990   tree attrs;
4991
4992   if (!decl)
4993     return TARGET_LONG_CALLS;
4994
4995   attrs = TYPE_ATTRIBUTES (TREE_TYPE (decl));
4996   if (lookup_attribute ("short_call", attrs))
4997     return false;
4998
4999   /* For "f", be conservative, and only cater for cases in which the
5000      whole of the current function is placed in the same section.  */
5001   if (!flag_reorder_blocks_and_partition
5002       && TREE_CODE (decl) == FUNCTION_DECL
5003       && arm_function_in_section_p (decl, current_function_section ()))
5004     return false;
5005
5006   if (lookup_attribute ("long_call", attrs))
5007     return true;
5008
5009   return TARGET_LONG_CALLS;
5010 }
5011
5012 /* Return nonzero if it is ok to make a tail-call to DECL.  */
5013 static bool
5014 arm_function_ok_for_sibcall (tree decl, tree exp)
5015 {
5016   unsigned long func_type;
5017
5018   if (cfun->machine->sibcall_blocked)
5019     return false;
5020
5021   /* Never tailcall something for which we have no decl, or if we
5022      are generating code for Thumb-1.  */
5023   if (decl == NULL || TARGET_THUMB1)
5024     return false;
5025
5026   /* The PIC register is live on entry to VxWorks PLT entries, so we
5027      must make the call before restoring the PIC register.  */
5028   if (TARGET_VXWORKS_RTP && flag_pic && !targetm.binds_local_p (decl))
5029     return false;
5030
5031   /* Cannot tail-call to long calls, since these are out of range of
5032      a branch instruction.  */
5033   if (arm_is_long_call_p (decl))
5034     return false;
5035
5036   /* If we are interworking and the function is not declared static
5037      then we can't tail-call it unless we know that it exists in this
5038      compilation unit (since it might be a Thumb routine).  */
5039   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
5040     return false;
5041
5042   func_type = arm_current_func_type ();
5043   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
5044   if (IS_INTERRUPT (func_type))
5045     return false;
5046
5047   if (!VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
5048     {
5049       /* Check that the return value locations are the same.  For
5050          example that we aren't returning a value from the sibling in
5051          a VFP register but then need to transfer it to a core
5052          register.  */
5053       rtx a, b;
5054
5055       a = arm_function_value (TREE_TYPE (exp), decl, false);
5056       b = arm_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
5057                               cfun->decl, false);
5058       if (!rtx_equal_p (a, b))
5059         return false;
5060     }
5061
5062   /* Never tailcall if function may be called with a misaligned SP.  */
5063   if (IS_STACKALIGN (func_type))
5064     return false;
5065
5066   /* Everything else is ok.  */
5067   return true;
5068 }
5069
5070 \f
5071 /* Addressing mode support functions.  */
5072
5073 /* Return nonzero if X is a legitimate immediate operand when compiling
5074    for PIC.  We know that X satisfies CONSTANT_P and flag_pic is true.  */
5075 int
5076 legitimate_pic_operand_p (rtx x)
5077 {
5078   if (GET_CODE (x) == SYMBOL_REF
5079       || (GET_CODE (x) == CONST
5080           && GET_CODE (XEXP (x, 0)) == PLUS
5081           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
5082     return 0;
5083
5084   return 1;
5085 }
5086
5087 /* Record that the current function needs a PIC register.  Initialize
5088    cfun->machine->pic_reg if we have not already done so.  */
5089
5090 static void
5091 require_pic_register (void)
5092 {
5093   /* A lot of the logic here is made obscure by the fact that this
5094      routine gets called as part of the rtx cost estimation process.
5095      We don't want those calls to affect any assumptions about the real
5096      function; and further, we can't call entry_of_function() until we
5097      start the real expansion process.  */
5098   if (!crtl->uses_pic_offset_table)
5099     {
5100       gcc_assert (can_create_pseudo_p ());
5101       if (arm_pic_register != INVALID_REGNUM)
5102         {
5103           if (!cfun->machine->pic_reg)
5104             cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
5105
5106           /* Play games to avoid marking the function as needing pic
5107              if we are being called as part of the cost-estimation
5108              process.  */
5109           if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
5110             crtl->uses_pic_offset_table = 1;
5111         }
5112       else
5113         {
5114           rtx seq;
5115
5116           if (!cfun->machine->pic_reg)
5117             cfun->machine->pic_reg = gen_reg_rtx (Pmode);
5118
5119           /* Play games to avoid marking the function as needing pic
5120              if we are being called as part of the cost-estimation
5121              process.  */
5122           if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
5123             {
5124               crtl->uses_pic_offset_table = 1;
5125               start_sequence ();
5126
5127               arm_load_pic_register (0UL);
5128
5129               seq = get_insns ();
5130               end_sequence ();
5131               /* We can be called during expansion of PHI nodes, where
5132                  we can't yet emit instructions directly in the final
5133                  insn stream.  Queue the insns on the entry edge, they will
5134                  be committed after everything else is expanded.  */
5135               insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
5136             }
5137         }
5138     }
5139 }
5140
5141 rtx
5142 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
5143 {
5144   if (GET_CODE (orig) == SYMBOL_REF
5145       || GET_CODE (orig) == LABEL_REF)
5146     {
5147       rtx insn;
5148
5149       if (reg == 0)
5150         {
5151           gcc_assert (can_create_pseudo_p ());
5152           reg = gen_reg_rtx (Pmode);
5153         }
5154
5155       /* VxWorks does not impose a fixed gap between segments; the run-time
5156          gap can be different from the object-file gap.  We therefore can't
5157          use GOTOFF unless we are absolutely sure that the symbol is in the
5158          same segment as the GOT.  Unfortunately, the flexibility of linker
5159          scripts means that we can't be sure of that in general, so assume
5160          that GOTOFF is never valid on VxWorks.  */
5161       if ((GET_CODE (orig) == LABEL_REF
5162            || (GET_CODE (orig) == SYMBOL_REF &&
5163                SYMBOL_REF_LOCAL_P (orig)))
5164           && NEED_GOT_RELOC
5165           && !TARGET_VXWORKS_RTP)
5166         insn = arm_pic_static_addr (orig, reg);
5167       else
5168         {
5169           rtx pat;
5170           rtx mem;
5171
5172           /* If this function doesn't have a pic register, create one now.  */
5173           require_pic_register ();
5174
5175           pat = gen_calculate_pic_address (reg, cfun->machine->pic_reg, orig);
5176
5177           /* Make the MEM as close to a constant as possible.  */
5178           mem = SET_SRC (pat);
5179           gcc_assert (MEM_P (mem) && !MEM_VOLATILE_P (mem));
5180           MEM_READONLY_P (mem) = 1;
5181           MEM_NOTRAP_P (mem) = 1;
5182
5183           insn = emit_insn (pat);
5184         }
5185
5186       /* Put a REG_EQUAL note on this insn, so that it can be optimized
5187          by loop.  */
5188       set_unique_reg_note (insn, REG_EQUAL, orig);
5189
5190       return reg;
5191     }
5192   else if (GET_CODE (orig) == CONST)
5193     {
5194       rtx base, offset;
5195
5196       if (GET_CODE (XEXP (orig, 0)) == PLUS
5197           && XEXP (XEXP (orig, 0), 0) == cfun->machine->pic_reg)
5198         return orig;
5199
5200       /* Handle the case where we have: const (UNSPEC_TLS).  */
5201       if (GET_CODE (XEXP (orig, 0)) == UNSPEC
5202           && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
5203         return orig;
5204
5205       /* Handle the case where we have:
5206          const (plus (UNSPEC_TLS) (ADDEND)).  The ADDEND must be a
5207          CONST_INT.  */
5208       if (GET_CODE (XEXP (orig, 0)) == PLUS
5209           && GET_CODE (XEXP (XEXP (orig, 0), 0)) == UNSPEC
5210           && XINT (XEXP (XEXP (orig, 0), 0), 1) == UNSPEC_TLS)
5211         {
5212           gcc_assert (GET_CODE (XEXP (XEXP (orig, 0), 1)) == CONST_INT);
5213           return orig;
5214         }
5215
5216       if (reg == 0)
5217         {
5218           gcc_assert (can_create_pseudo_p ());
5219           reg = gen_reg_rtx (Pmode);
5220         }
5221
5222       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
5223
5224       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
5225       offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
5226                                        base == reg ? 0 : reg);
5227
5228       if (GET_CODE (offset) == CONST_INT)
5229         {
5230           /* The base register doesn't really matter, we only want to
5231              test the index for the appropriate mode.  */
5232           if (!arm_legitimate_index_p (mode, offset, SET, 0))
5233             {
5234               gcc_assert (can_create_pseudo_p ());
5235               offset = force_reg (Pmode, offset);
5236             }
5237
5238           if (GET_CODE (offset) == CONST_INT)
5239             return plus_constant (base, INTVAL (offset));
5240         }
5241
5242       if (GET_MODE_SIZE (mode) > 4
5243           && (GET_MODE_CLASS (mode) == MODE_INT
5244               || TARGET_SOFT_FLOAT))
5245         {
5246           emit_insn (gen_addsi3 (reg, base, offset));
5247           return reg;
5248         }
5249
5250       return gen_rtx_PLUS (Pmode, base, offset);
5251     }
5252
5253   return orig;
5254 }
5255
5256
5257 /* Find a spare register to use during the prolog of a function.  */
5258
5259 static int
5260 thumb_find_work_register (unsigned long pushed_regs_mask)
5261 {
5262   int reg;
5263
5264   /* Check the argument registers first as these are call-used.  The
5265      register allocation order means that sometimes r3 might be used
5266      but earlier argument registers might not, so check them all.  */
5267   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
5268     if (!df_regs_ever_live_p (reg))
5269       return reg;
5270
5271   /* Before going on to check the call-saved registers we can try a couple
5272      more ways of deducing that r3 is available.  The first is when we are
5273      pushing anonymous arguments onto the stack and we have less than 4
5274      registers worth of fixed arguments(*).  In this case r3 will be part of
5275      the variable argument list and so we can be sure that it will be
5276      pushed right at the start of the function.  Hence it will be available
5277      for the rest of the prologue.
5278      (*): ie crtl->args.pretend_args_size is greater than 0.  */
5279   if (cfun->machine->uses_anonymous_args
5280       && crtl->args.pretend_args_size > 0)
5281     return LAST_ARG_REGNUM;
5282
5283   /* The other case is when we have fixed arguments but less than 4 registers
5284      worth.  In this case r3 might be used in the body of the function, but
5285      it is not being used to convey an argument into the function.  In theory
5286      we could just check crtl->args.size to see how many bytes are
5287      being passed in argument registers, but it seems that it is unreliable.
5288      Sometimes it will have the value 0 when in fact arguments are being
5289      passed.  (See testcase execute/20021111-1.c for an example).  So we also
5290      check the args_info.nregs field as well.  The problem with this field is
5291      that it makes no allowances for arguments that are passed to the
5292      function but which are not used.  Hence we could miss an opportunity
5293      when a function has an unused argument in r3.  But it is better to be
5294      safe than to be sorry.  */
5295   if (! cfun->machine->uses_anonymous_args
5296       && crtl->args.size >= 0
5297       && crtl->args.size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
5298       && crtl->args.info.nregs < 4)
5299     return LAST_ARG_REGNUM;
5300
5301   /* Otherwise look for a call-saved register that is going to be pushed.  */
5302   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
5303     if (pushed_regs_mask & (1 << reg))
5304       return reg;
5305
5306   if (TARGET_THUMB2)
5307     {
5308       /* Thumb-2 can use high regs.  */
5309       for (reg = FIRST_HI_REGNUM; reg < 15; reg ++)
5310         if (pushed_regs_mask & (1 << reg))
5311           return reg;
5312     }
5313   /* Something went wrong - thumb_compute_save_reg_mask()
5314      should have arranged for a suitable register to be pushed.  */
5315   gcc_unreachable ();
5316 }
5317
5318 static GTY(()) int pic_labelno;
5319
5320 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
5321    low register.  */
5322
5323 void
5324 arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
5325 {
5326   rtx l1, labelno, pic_tmp, pic_rtx, pic_reg;
5327
5328   if (crtl->uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
5329     return;
5330
5331   gcc_assert (flag_pic);
5332
5333   pic_reg = cfun->machine->pic_reg;
5334   if (TARGET_VXWORKS_RTP)
5335     {
5336       pic_rtx = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE);
5337       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
5338       emit_insn (gen_pic_load_addr_32bit (pic_reg, pic_rtx));
5339
5340       emit_insn (gen_rtx_SET (Pmode, pic_reg, gen_rtx_MEM (Pmode, pic_reg)));
5341
5342       pic_tmp = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
5343       emit_insn (gen_pic_offset_arm (pic_reg, pic_reg, pic_tmp));
5344     }
5345   else
5346     {
5347       /* We use an UNSPEC rather than a LABEL_REF because this label
5348          never appears in the code stream.  */
5349
5350       labelno = GEN_INT (pic_labelno++);
5351       l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
5352       l1 = gen_rtx_CONST (VOIDmode, l1);
5353
5354       /* On the ARM the PC register contains 'dot + 8' at the time of the
5355          addition, on the Thumb it is 'dot + 4'.  */
5356       pic_rtx = plus_constant (l1, TARGET_ARM ? 8 : 4);
5357       pic_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, pic_rtx),
5358                                 UNSPEC_GOTSYM_OFF);
5359       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
5360
5361       if (TARGET_32BIT)
5362         {
5363           emit_insn (gen_pic_load_addr_32bit (pic_reg, pic_rtx));
5364           if (TARGET_ARM)
5365             emit_insn (gen_pic_add_dot_plus_eight (pic_reg, pic_reg, labelno));
5366           else
5367             emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
5368         }
5369       else /* TARGET_THUMB1 */
5370         {
5371           if (arm_pic_register != INVALID_REGNUM
5372               && REGNO (pic_reg) > LAST_LO_REGNUM)
5373             {
5374               /* We will have pushed the pic register, so we should always be
5375                  able to find a work register.  */
5376               pic_tmp = gen_rtx_REG (SImode,
5377                                      thumb_find_work_register (saved_regs));
5378               emit_insn (gen_pic_load_addr_thumb1 (pic_tmp, pic_rtx));
5379               emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
5380             }
5381           else
5382             emit_insn (gen_pic_load_addr_thumb1 (pic_reg, pic_rtx));
5383           emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
5384         }
5385     }
5386
5387   /* Need to emit this whether or not we obey regdecls,
5388      since setjmp/longjmp can cause life info to screw up.  */
5389   emit_use (pic_reg);
5390 }
5391
5392 /* Generate code to load the address of a static var when flag_pic is set.  */
5393 static rtx
5394 arm_pic_static_addr (rtx orig, rtx reg)
5395 {
5396   rtx l1, labelno, offset_rtx, insn;
5397
5398   gcc_assert (flag_pic);
5399
5400   /* We use an UNSPEC rather than a LABEL_REF because this label
5401      never appears in the code stream.  */
5402   labelno = GEN_INT (pic_labelno++);
5403   l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
5404   l1 = gen_rtx_CONST (VOIDmode, l1);
5405
5406   /* On the ARM the PC register contains 'dot + 8' at the time of the
5407      addition, on the Thumb it is 'dot + 4'.  */
5408   offset_rtx = plus_constant (l1, TARGET_ARM ? 8 : 4);
5409   offset_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, orig, offset_rtx),
5410                                UNSPEC_SYMBOL_OFFSET);
5411   offset_rtx = gen_rtx_CONST (Pmode, offset_rtx);
5412
5413   if (TARGET_32BIT)
5414     {
5415       emit_insn (gen_pic_load_addr_32bit (reg, offset_rtx));
5416       if (TARGET_ARM)
5417         insn = emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
5418       else
5419         insn = emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5420     }
5421   else /* TARGET_THUMB1 */
5422     {
5423       emit_insn (gen_pic_load_addr_thumb1 (reg, offset_rtx));
5424       insn = emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5425     }
5426
5427   return insn;
5428 }
5429
5430 /* Return nonzero if X is valid as an ARM state addressing register.  */
5431 static int
5432 arm_address_register_rtx_p (rtx x, int strict_p)
5433 {
5434   int regno;
5435
5436   if (GET_CODE (x) != REG)
5437     return 0;
5438
5439   regno = REGNO (x);
5440
5441   if (strict_p)
5442     return ARM_REGNO_OK_FOR_BASE_P (regno);
5443
5444   return (regno <= LAST_ARM_REGNUM
5445           || regno >= FIRST_PSEUDO_REGISTER
5446           || regno == FRAME_POINTER_REGNUM
5447           || regno == ARG_POINTER_REGNUM);
5448 }
5449
5450 /* Return TRUE if this rtx is the difference of a symbol and a label,
5451    and will reduce to a PC-relative relocation in the object file.
5452    Expressions like this can be left alone when generating PIC, rather
5453    than forced through the GOT.  */
5454 static int
5455 pcrel_constant_p (rtx x)
5456 {
5457   if (GET_CODE (x) == MINUS)
5458     return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
5459
5460   return FALSE;
5461 }
5462
5463 /* Return true if X will surely end up in an index register after next
5464    splitting pass.  */
5465 static bool
5466 will_be_in_index_register (const_rtx x)
5467 {
5468   /* arm.md: calculate_pic_address will split this into a register.  */
5469   return GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_SYM;
5470 }
5471
5472 /* Return nonzero if X is a valid ARM state address operand.  */
5473 int
5474 arm_legitimate_address_outer_p (enum machine_mode mode, rtx x, RTX_CODE outer,
5475                                 int strict_p)
5476 {
5477   bool use_ldrd;
5478   enum rtx_code code = GET_CODE (x);
5479
5480   if (arm_address_register_rtx_p (x, strict_p))
5481     return 1;
5482
5483   use_ldrd = (TARGET_LDRD
5484               && (mode == DImode
5485                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
5486
5487   if (code == POST_INC || code == PRE_DEC
5488       || ((code == PRE_INC || code == POST_DEC)
5489           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
5490     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
5491
5492   else if ((code == POST_MODIFY || code == PRE_MODIFY)
5493            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
5494            && GET_CODE (XEXP (x, 1)) == PLUS
5495            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5496     {
5497       rtx addend = XEXP (XEXP (x, 1), 1);
5498
5499       /* Don't allow ldrd post increment by register because it's hard
5500          to fixup invalid register choices.  */
5501       if (use_ldrd
5502           && GET_CODE (x) == POST_MODIFY
5503           && GET_CODE (addend) == REG)
5504         return 0;
5505
5506       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
5507               && arm_legitimate_index_p (mode, addend, outer, strict_p));
5508     }
5509
5510   /* After reload constants split into minipools will have addresses
5511      from a LABEL_REF.  */
5512   else if (reload_completed
5513            && (code == LABEL_REF
5514                || (code == CONST
5515                    && GET_CODE (XEXP (x, 0)) == PLUS
5516                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5517                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5518     return 1;
5519
5520   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
5521     return 0;
5522
5523   else if (code == PLUS)
5524     {
5525       rtx xop0 = XEXP (x, 0);
5526       rtx xop1 = XEXP (x, 1);
5527
5528       return ((arm_address_register_rtx_p (xop0, strict_p)
5529                && ((GET_CODE(xop1) == CONST_INT
5530                     && arm_legitimate_index_p (mode, xop1, outer, strict_p))
5531                    || (!strict_p && will_be_in_index_register (xop1))))
5532               || (arm_address_register_rtx_p (xop1, strict_p)
5533                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
5534     }
5535
5536 #if 0
5537   /* Reload currently can't handle MINUS, so disable this for now */
5538   else if (GET_CODE (x) == MINUS)
5539     {
5540       rtx xop0 = XEXP (x, 0);
5541       rtx xop1 = XEXP (x, 1);
5542
5543       return (arm_address_register_rtx_p (xop0, strict_p)
5544               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
5545     }
5546 #endif
5547
5548   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
5549            && code == SYMBOL_REF
5550            && CONSTANT_POOL_ADDRESS_P (x)
5551            && ! (flag_pic
5552                  && symbol_mentioned_p (get_pool_constant (x))
5553                  && ! pcrel_constant_p (get_pool_constant (x))))
5554     return 1;
5555
5556   return 0;
5557 }
5558
5559 /* Return nonzero if X is a valid Thumb-2 address operand.  */
5560 static int
5561 thumb2_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
5562 {
5563   bool use_ldrd;
5564   enum rtx_code code = GET_CODE (x);
5565   
5566   if (arm_address_register_rtx_p (x, strict_p))
5567     return 1;
5568
5569   use_ldrd = (TARGET_LDRD
5570               && (mode == DImode
5571                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
5572
5573   if (code == POST_INC || code == PRE_DEC
5574       || ((code == PRE_INC || code == POST_DEC)
5575           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
5576     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
5577
5578   else if ((code == POST_MODIFY || code == PRE_MODIFY)
5579            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
5580            && GET_CODE (XEXP (x, 1)) == PLUS
5581            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5582     {
5583       /* Thumb-2 only has autoincrement by constant.  */
5584       rtx addend = XEXP (XEXP (x, 1), 1);
5585       HOST_WIDE_INT offset;
5586
5587       if (GET_CODE (addend) != CONST_INT)
5588         return 0;
5589
5590       offset = INTVAL(addend);
5591       if (GET_MODE_SIZE (mode) <= 4)
5592         return (offset > -256 && offset < 256);
5593       
5594       return (use_ldrd && offset > -1024 && offset < 1024
5595               && (offset & 3) == 0);
5596     }
5597
5598   /* After reload constants split into minipools will have addresses
5599      from a LABEL_REF.  */
5600   else if (reload_completed
5601            && (code == LABEL_REF
5602                || (code == CONST
5603                    && GET_CODE (XEXP (x, 0)) == PLUS
5604                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5605                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5606     return 1;
5607
5608   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
5609     return 0;
5610
5611   else if (code == PLUS)
5612     {
5613       rtx xop0 = XEXP (x, 0);
5614       rtx xop1 = XEXP (x, 1);
5615
5616       return ((arm_address_register_rtx_p (xop0, strict_p)
5617                && (thumb2_legitimate_index_p (mode, xop1, strict_p)
5618                    || (!strict_p && will_be_in_index_register (xop1))))
5619               || (arm_address_register_rtx_p (xop1, strict_p)
5620                   && thumb2_legitimate_index_p (mode, xop0, strict_p)));
5621     }
5622
5623   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
5624            && code == SYMBOL_REF
5625            && CONSTANT_POOL_ADDRESS_P (x)
5626            && ! (flag_pic
5627                  && symbol_mentioned_p (get_pool_constant (x))
5628                  && ! pcrel_constant_p (get_pool_constant (x))))
5629     return 1;
5630
5631   return 0;
5632 }
5633
5634 /* Return nonzero if INDEX is valid for an address index operand in
5635    ARM state.  */
5636 static int
5637 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
5638                         int strict_p)
5639 {
5640   HOST_WIDE_INT range;
5641   enum rtx_code code = GET_CODE (index);
5642
5643   /* Standard coprocessor addressing modes.  */
5644   if (TARGET_HARD_FLOAT
5645       && (TARGET_FPA || TARGET_MAVERICK)
5646       && (GET_MODE_CLASS (mode) == MODE_FLOAT
5647           || (TARGET_MAVERICK && mode == DImode)))
5648     return (code == CONST_INT && INTVAL (index) < 1024
5649             && INTVAL (index) > -1024
5650             && (INTVAL (index) & 3) == 0);
5651
5652   /* For quad modes, we restrict the constant offset to be slightly less
5653      than what the instruction format permits.  We do this because for
5654      quad mode moves, we will actually decompose them into two separate
5655      double-mode reads or writes.  INDEX must therefore be a valid
5656      (double-mode) offset and so should INDEX+8.  */
5657   if (TARGET_NEON && VALID_NEON_QREG_MODE (mode))
5658     return (code == CONST_INT
5659             && INTVAL (index) < 1016
5660             && INTVAL (index) > -1024
5661             && (INTVAL (index) & 3) == 0);
5662
5663   /* We have no such constraint on double mode offsets, so we permit the
5664      full range of the instruction format.  */
5665   if (TARGET_NEON && VALID_NEON_DREG_MODE (mode))
5666     return (code == CONST_INT
5667             && INTVAL (index) < 1024
5668             && INTVAL (index) > -1024
5669             && (INTVAL (index) & 3) == 0);
5670
5671   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
5672     return (code == CONST_INT
5673             && INTVAL (index) < 1024
5674             && INTVAL (index) > -1024
5675             && (INTVAL (index) & 3) == 0);
5676
5677   if (arm_address_register_rtx_p (index, strict_p)
5678       && (GET_MODE_SIZE (mode) <= 4))
5679     return 1;
5680
5681   if (mode == DImode || mode == DFmode)
5682     {
5683       if (code == CONST_INT)
5684         {
5685           HOST_WIDE_INT val = INTVAL (index);
5686
5687           if (TARGET_LDRD)
5688             return val > -256 && val < 256;
5689           else
5690             return val > -4096 && val < 4092;
5691         }
5692
5693       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
5694     }
5695
5696   if (GET_MODE_SIZE (mode) <= 4
5697       && ! (arm_arch4
5698             && (mode == HImode
5699                 || mode == HFmode
5700                 || (mode == QImode && outer == SIGN_EXTEND))))
5701     {
5702       if (code == MULT)
5703         {
5704           rtx xiop0 = XEXP (index, 0);
5705           rtx xiop1 = XEXP (index, 1);
5706
5707           return ((arm_address_register_rtx_p (xiop0, strict_p)
5708                    && power_of_two_operand (xiop1, SImode))
5709                   || (arm_address_register_rtx_p (xiop1, strict_p)
5710                       && power_of_two_operand (xiop0, SImode)));
5711         }
5712       else if (code == LSHIFTRT || code == ASHIFTRT
5713                || code == ASHIFT || code == ROTATERT)
5714         {
5715           rtx op = XEXP (index, 1);
5716
5717           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
5718                   && GET_CODE (op) == CONST_INT
5719                   && INTVAL (op) > 0
5720                   && INTVAL (op) <= 31);
5721         }
5722     }
5723
5724   /* For ARM v4 we may be doing a sign-extend operation during the
5725      load.  */
5726   if (arm_arch4)
5727     {
5728       if (mode == HImode
5729           || mode == HFmode
5730           || (outer == SIGN_EXTEND && mode == QImode))
5731         range = 256;
5732       else
5733         range = 4096;
5734     }
5735   else
5736     range = (mode == HImode || mode == HFmode) ? 4095 : 4096;
5737
5738   return (code == CONST_INT
5739           && INTVAL (index) < range
5740           && INTVAL (index) > -range);
5741 }
5742
5743 /* Return true if OP is a valid index scaling factor for Thumb-2 address
5744    index operand.  i.e. 1, 2, 4 or 8.  */
5745 static bool
5746 thumb2_index_mul_operand (rtx op)
5747 {
5748   HOST_WIDE_INT val;
5749   
5750   if (GET_CODE(op) != CONST_INT)
5751     return false;
5752
5753   val = INTVAL(op);
5754   return (val == 1 || val == 2 || val == 4 || val == 8);
5755 }
5756   
5757 /* Return nonzero if INDEX is a valid Thumb-2 address index operand.  */
5758 static int
5759 thumb2_legitimate_index_p (enum machine_mode mode, rtx index, int strict_p)
5760 {
5761   enum rtx_code code = GET_CODE (index);
5762
5763   /* ??? Combine arm and thumb2 coprocessor addressing modes.  */
5764   /* Standard coprocessor addressing modes.  */
5765   if (TARGET_HARD_FLOAT
5766       && (TARGET_FPA || TARGET_MAVERICK)
5767       && (GET_MODE_CLASS (mode) == MODE_FLOAT
5768           || (TARGET_MAVERICK && mode == DImode)))
5769     return (code == CONST_INT && INTVAL (index) < 1024
5770             && INTVAL (index) > -1024
5771             && (INTVAL (index) & 3) == 0);
5772
5773   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
5774     {
5775       /* For DImode assume values will usually live in core regs
5776          and only allow LDRD addressing modes.  */
5777       if (!TARGET_LDRD || mode != DImode)
5778         return (code == CONST_INT
5779                 && INTVAL (index) < 1024
5780                 && INTVAL (index) > -1024
5781                 && (INTVAL (index) & 3) == 0);
5782     }
5783
5784   /* For quad modes, we restrict the constant offset to be slightly less
5785      than what the instruction format permits.  We do this because for
5786      quad mode moves, we will actually decompose them into two separate
5787      double-mode reads or writes.  INDEX must therefore be a valid
5788      (double-mode) offset and so should INDEX+8.  */
5789   if (TARGET_NEON && VALID_NEON_QREG_MODE (mode))
5790     return (code == CONST_INT
5791             && INTVAL (index) < 1016
5792             && INTVAL (index) > -1024
5793             && (INTVAL (index) & 3) == 0);
5794
5795   /* We have no such constraint on double mode offsets, so we permit the
5796      full range of the instruction format.  */
5797   if (TARGET_NEON && VALID_NEON_DREG_MODE (mode))
5798     return (code == CONST_INT
5799             && INTVAL (index) < 1024
5800             && INTVAL (index) > -1024
5801             && (INTVAL (index) & 3) == 0);
5802
5803   if (arm_address_register_rtx_p (index, strict_p)
5804       && (GET_MODE_SIZE (mode) <= 4))
5805     return 1;
5806
5807   if (mode == DImode || mode == DFmode)
5808     {
5809       if (code == CONST_INT)
5810         {
5811           HOST_WIDE_INT val = INTVAL (index);
5812           /* ??? Can we assume ldrd for thumb2?  */
5813           /* Thumb-2 ldrd only has reg+const addressing modes.  */
5814           /* ldrd supports offsets of +-1020.
5815              However the ldr fallback does not.  */
5816           return val > -256 && val < 256 && (val & 3) == 0;
5817         }
5818       else
5819         return 0;
5820     }
5821
5822   if (code == MULT)
5823     {
5824       rtx xiop0 = XEXP (index, 0);
5825       rtx xiop1 = XEXP (index, 1);
5826
5827       return ((arm_address_register_rtx_p (xiop0, strict_p)
5828                && thumb2_index_mul_operand (xiop1))
5829               || (arm_address_register_rtx_p (xiop1, strict_p)
5830                   && thumb2_index_mul_operand (xiop0)));
5831     }
5832   else if (code == ASHIFT)
5833     {
5834       rtx op = XEXP (index, 1);
5835
5836       return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
5837               && GET_CODE (op) == CONST_INT
5838               && INTVAL (op) > 0
5839               && INTVAL (op) <= 3);
5840     }
5841
5842   return (code == CONST_INT
5843           && INTVAL (index) < 4096
5844           && INTVAL (index) > -256);
5845 }
5846
5847 /* Return nonzero if X is valid as a 16-bit Thumb state base register.  */
5848 static int
5849 thumb1_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
5850 {
5851   int regno;
5852
5853   if (GET_CODE (x) != REG)
5854     return 0;
5855
5856   regno = REGNO (x);
5857
5858   if (strict_p)
5859     return THUMB1_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
5860
5861   return (regno <= LAST_LO_REGNUM
5862           || regno > LAST_VIRTUAL_REGISTER
5863           || regno == FRAME_POINTER_REGNUM
5864           || (GET_MODE_SIZE (mode) >= 4
5865               && (regno == STACK_POINTER_REGNUM
5866                   || regno >= FIRST_PSEUDO_REGISTER
5867                   || x == hard_frame_pointer_rtx
5868                   || x == arg_pointer_rtx)));
5869 }
5870
5871 /* Return nonzero if x is a legitimate index register.  This is the case
5872    for any base register that can access a QImode object.  */
5873 inline static int
5874 thumb1_index_register_rtx_p (rtx x, int strict_p)
5875 {
5876   return thumb1_base_register_rtx_p (x, QImode, strict_p);
5877 }
5878
5879 /* Return nonzero if x is a legitimate 16-bit Thumb-state address.
5880
5881    The AP may be eliminated to either the SP or the FP, so we use the
5882    least common denominator, e.g. SImode, and offsets from 0 to 64.
5883
5884    ??? Verify whether the above is the right approach.
5885
5886    ??? Also, the FP may be eliminated to the SP, so perhaps that
5887    needs special handling also.
5888
5889    ??? Look at how the mips16 port solves this problem.  It probably uses
5890    better ways to solve some of these problems.
5891
5892    Although it is not incorrect, we don't accept QImode and HImode
5893    addresses based on the frame pointer or arg pointer until the
5894    reload pass starts.  This is so that eliminating such addresses
5895    into stack based ones won't produce impossible code.  */
5896 static int
5897 thumb1_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
5898 {
5899   /* ??? Not clear if this is right.  Experiment.  */
5900   if (GET_MODE_SIZE (mode) < 4
5901       && !(reload_in_progress || reload_completed)
5902       && (reg_mentioned_p (frame_pointer_rtx, x)
5903           || reg_mentioned_p (arg_pointer_rtx, x)
5904           || reg_mentioned_p (virtual_incoming_args_rtx, x)
5905           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
5906           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
5907           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
5908     return 0;
5909
5910   /* Accept any base register.  SP only in SImode or larger.  */
5911   else if (thumb1_base_register_rtx_p (x, mode, strict_p))
5912     return 1;
5913
5914   /* This is PC relative data before arm_reorg runs.  */
5915   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
5916            && GET_CODE (x) == SYMBOL_REF
5917            && CONSTANT_POOL_ADDRESS_P (x) && !flag_pic)
5918     return 1;
5919
5920   /* This is PC relative data after arm_reorg runs.  */
5921   else if ((GET_MODE_SIZE (mode) >= 4 || mode == HFmode)
5922            && reload_completed
5923            && (GET_CODE (x) == LABEL_REF
5924                || (GET_CODE (x) == CONST
5925                    && GET_CODE (XEXP (x, 0)) == PLUS
5926                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5927                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5928     return 1;
5929
5930   /* Post-inc indexing only supported for SImode and larger.  */
5931   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
5932            && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p))
5933     return 1;
5934
5935   else if (GET_CODE (x) == PLUS)
5936     {
5937       /* REG+REG address can be any two index registers.  */
5938       /* We disallow FRAME+REG addressing since we know that FRAME
5939          will be replaced with STACK, and SP relative addressing only
5940          permits SP+OFFSET.  */
5941       if (GET_MODE_SIZE (mode) <= 4
5942           && XEXP (x, 0) != frame_pointer_rtx
5943           && XEXP (x, 1) != frame_pointer_rtx
5944           && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
5945           && (thumb1_index_register_rtx_p (XEXP (x, 1), strict_p)
5946               || (!strict_p && will_be_in_index_register (XEXP (x, 1)))))
5947         return 1;
5948
5949       /* REG+const has 5-7 bit offset for non-SP registers.  */
5950       else if ((thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
5951                 || XEXP (x, 0) == arg_pointer_rtx)
5952                && GET_CODE (XEXP (x, 1)) == CONST_INT
5953                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
5954         return 1;
5955
5956       /* REG+const has 10-bit offset for SP, but only SImode and
5957          larger is supported.  */
5958       /* ??? Should probably check for DI/DFmode overflow here
5959          just like GO_IF_LEGITIMATE_OFFSET does.  */
5960       else if (GET_CODE (XEXP (x, 0)) == REG
5961                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
5962                && GET_MODE_SIZE (mode) >= 4
5963                && GET_CODE (XEXP (x, 1)) == CONST_INT
5964                && INTVAL (XEXP (x, 1)) >= 0
5965                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
5966                && (INTVAL (XEXP (x, 1)) & 3) == 0)
5967         return 1;
5968
5969       else if (GET_CODE (XEXP (x, 0)) == REG
5970                && (REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
5971                    || REGNO (XEXP (x, 0)) == ARG_POINTER_REGNUM
5972                    || (REGNO (XEXP (x, 0)) >= FIRST_VIRTUAL_REGISTER
5973                        && REGNO (XEXP (x, 0))
5974                           <= LAST_VIRTUAL_POINTER_REGISTER))
5975                && GET_MODE_SIZE (mode) >= 4
5976                && GET_CODE (XEXP (x, 1)) == CONST_INT
5977                && (INTVAL (XEXP (x, 1)) & 3) == 0)
5978         return 1;
5979     }
5980
5981   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
5982            && GET_MODE_SIZE (mode) == 4
5983            && GET_CODE (x) == SYMBOL_REF
5984            && CONSTANT_POOL_ADDRESS_P (x)
5985            && ! (flag_pic
5986                  && symbol_mentioned_p (get_pool_constant (x))
5987                  && ! pcrel_constant_p (get_pool_constant (x))))
5988     return 1;
5989
5990   return 0;
5991 }
5992
5993 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
5994    instruction of mode MODE.  */
5995 int
5996 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
5997 {
5998   switch (GET_MODE_SIZE (mode))
5999     {
6000     case 1:
6001       return val >= 0 && val < 32;
6002
6003     case 2:
6004       return val >= 0 && val < 64 && (val & 1) == 0;
6005
6006     default:
6007       return (val >= 0
6008               && (val + GET_MODE_SIZE (mode)) <= 128
6009               && (val & 3) == 0);
6010     }
6011 }
6012
6013 bool
6014 arm_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
6015 {
6016   if (TARGET_ARM)
6017     return arm_legitimate_address_outer_p (mode, x, SET, strict_p);
6018   else if (TARGET_THUMB2)
6019     return thumb2_legitimate_address_p (mode, x, strict_p);
6020   else /* if (TARGET_THUMB1) */
6021     return thumb1_legitimate_address_p (mode, x, strict_p);
6022 }
6023
6024 /* Build the SYMBOL_REF for __tls_get_addr.  */
6025
6026 static GTY(()) rtx tls_get_addr_libfunc;
6027
6028 static rtx
6029 get_tls_get_addr (void)
6030 {
6031   if (!tls_get_addr_libfunc)
6032     tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
6033   return tls_get_addr_libfunc;
6034 }
6035
6036 static rtx
6037 arm_load_tp (rtx target)
6038 {
6039   if (!target)
6040     target = gen_reg_rtx (SImode);
6041
6042   if (TARGET_HARD_TP)
6043     {
6044       /* Can return in any reg.  */
6045       emit_insn (gen_load_tp_hard (target));
6046     }
6047   else
6048     {
6049       /* Always returned in r0.  Immediately copy the result into a pseudo,
6050          otherwise other uses of r0 (e.g. setting up function arguments) may
6051          clobber the value.  */
6052
6053       rtx tmp;
6054
6055       emit_insn (gen_load_tp_soft ());
6056
6057       tmp = gen_rtx_REG (SImode, 0);
6058       emit_move_insn (target, tmp);
6059     }
6060   return target;
6061 }
6062
6063 static rtx
6064 load_tls_operand (rtx x, rtx reg)
6065 {
6066   rtx tmp;
6067
6068   if (reg == NULL_RTX)
6069     reg = gen_reg_rtx (SImode);
6070
6071   tmp = gen_rtx_CONST (SImode, x);
6072
6073   emit_move_insn (reg, tmp);
6074
6075   return reg;
6076 }
6077
6078 static rtx
6079 arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
6080 {
6081   rtx insns, label, labelno, sum;
6082
6083   start_sequence ();
6084
6085   labelno = GEN_INT (pic_labelno++);
6086   label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
6087   label = gen_rtx_CONST (VOIDmode, label);
6088
6089   sum = gen_rtx_UNSPEC (Pmode,
6090                         gen_rtvec (4, x, GEN_INT (reloc), label,
6091                                    GEN_INT (TARGET_ARM ? 8 : 4)),
6092                         UNSPEC_TLS);
6093   reg = load_tls_operand (sum, reg);
6094
6095   if (TARGET_ARM)
6096     emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
6097   else if (TARGET_THUMB2)
6098     emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
6099   else /* TARGET_THUMB1 */
6100     emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
6101
6102   *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX, LCT_PURE, /* LCT_CONST?  */
6103                                      Pmode, 1, reg, Pmode);
6104
6105   insns = get_insns ();
6106   end_sequence ();
6107
6108   return insns;
6109 }
6110
6111 rtx
6112 legitimize_tls_address (rtx x, rtx reg)
6113 {
6114   rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
6115   unsigned int model = SYMBOL_REF_TLS_MODEL (x);
6116
6117   switch (model)
6118     {
6119     case TLS_MODEL_GLOBAL_DYNAMIC:
6120       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
6121       dest = gen_reg_rtx (Pmode);
6122       emit_libcall_block (insns, dest, ret, x);
6123       return dest;
6124
6125     case TLS_MODEL_LOCAL_DYNAMIC:
6126       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
6127
6128       /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
6129          share the LDM result with other LD model accesses.  */
6130       eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
6131                             UNSPEC_TLS);
6132       dest = gen_reg_rtx (Pmode);
6133       emit_libcall_block (insns, dest, ret, eqv);
6134
6135       /* Load the addend.  */
6136       addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (TLS_LDO32)),
6137                                UNSPEC_TLS);
6138       addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
6139       return gen_rtx_PLUS (Pmode, dest, addend);
6140
6141     case TLS_MODEL_INITIAL_EXEC:
6142       labelno = GEN_INT (pic_labelno++);
6143       label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
6144       label = gen_rtx_CONST (VOIDmode, label);
6145       sum = gen_rtx_UNSPEC (Pmode,
6146                             gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
6147                                        GEN_INT (TARGET_ARM ? 8 : 4)),
6148                             UNSPEC_TLS);
6149       reg = load_tls_operand (sum, reg);
6150
6151       if (TARGET_ARM)
6152         emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
6153       else if (TARGET_THUMB2)
6154         emit_insn (gen_tls_load_dot_plus_four (reg, NULL, reg, labelno));
6155       else
6156         {
6157           emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
6158           emit_move_insn (reg, gen_const_mem (SImode, reg));
6159         }
6160
6161       tp = arm_load_tp (NULL_RTX);
6162
6163       return gen_rtx_PLUS (Pmode, tp, reg);
6164
6165     case TLS_MODEL_LOCAL_EXEC:
6166       tp = arm_load_tp (NULL_RTX);
6167
6168       reg = gen_rtx_UNSPEC (Pmode,
6169                             gen_rtvec (2, x, GEN_INT (TLS_LE32)),
6170                             UNSPEC_TLS);
6171       reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
6172
6173       return gen_rtx_PLUS (Pmode, tp, reg);
6174
6175     default:
6176       abort ();
6177     }
6178 }
6179
6180 /* Try machine-dependent ways of modifying an illegitimate address
6181    to be legitimate.  If we find one, return the new, valid address.  */
6182 rtx
6183 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
6184 {
6185   if (!TARGET_ARM)
6186     {
6187       /* TODO: legitimize_address for Thumb2.  */
6188       if (TARGET_THUMB2)
6189         return x;
6190       return thumb_legitimize_address (x, orig_x, mode);
6191     }
6192
6193   if (arm_tls_symbol_p (x))
6194     return legitimize_tls_address (x, NULL_RTX);
6195
6196   if (GET_CODE (x) == PLUS)
6197     {
6198       rtx xop0 = XEXP (x, 0);
6199       rtx xop1 = XEXP (x, 1);
6200
6201       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
6202         xop0 = force_reg (SImode, xop0);
6203
6204       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
6205         xop1 = force_reg (SImode, xop1);
6206
6207       if (ARM_BASE_REGISTER_RTX_P (xop0)
6208           && GET_CODE (xop1) == CONST_INT)
6209         {
6210           HOST_WIDE_INT n, low_n;
6211           rtx base_reg, val;
6212           n = INTVAL (xop1);
6213
6214           /* VFP addressing modes actually allow greater offsets, but for
6215              now we just stick with the lowest common denominator.  */
6216           if (mode == DImode
6217               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
6218             {
6219               low_n = n & 0x0f;
6220               n &= ~0x0f;
6221               if (low_n > 4)
6222                 {
6223                   n += 16;
6224                   low_n -= 16;
6225                 }
6226             }
6227           else
6228             {
6229               low_n = ((mode) == TImode ? 0
6230                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
6231               n -= low_n;
6232             }
6233
6234           base_reg = gen_reg_rtx (SImode);
6235           val = force_operand (plus_constant (xop0, n), NULL_RTX);
6236           emit_move_insn (base_reg, val);
6237           x = plus_constant (base_reg, low_n);
6238         }
6239       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
6240         x = gen_rtx_PLUS (SImode, xop0, xop1);
6241     }
6242
6243   /* XXX We don't allow MINUS any more -- see comment in
6244      arm_legitimate_address_outer_p ().  */
6245   else if (GET_CODE (x) == MINUS)
6246     {
6247       rtx xop0 = XEXP (x, 0);
6248       rtx xop1 = XEXP (x, 1);
6249
6250       if (CONSTANT_P (xop0))
6251         xop0 = force_reg (SImode, xop0);
6252
6253       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
6254         xop1 = force_reg (SImode, xop1);
6255
6256       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
6257         x = gen_rtx_MINUS (SImode, xop0, xop1);
6258     }
6259
6260   /* Make sure to take full advantage of the pre-indexed addressing mode
6261      with absolute addresses which often allows for the base register to
6262      be factorized for multiple adjacent memory references, and it might
6263      even allows for the mini pool to be avoided entirely. */
6264   else if (GET_CODE (x) == CONST_INT && optimize > 0)
6265     {
6266       unsigned int bits;
6267       HOST_WIDE_INT mask, base, index;
6268       rtx base_reg;
6269
6270       /* ldr and ldrb can use a 12-bit index, ldrsb and the rest can only
6271          use a 8-bit index. So let's use a 12-bit index for SImode only and
6272          hope that arm_gen_constant will enable ldrb to use more bits. */
6273       bits = (mode == SImode) ? 12 : 8;
6274       mask = (1 << bits) - 1;
6275       base = INTVAL (x) & ~mask;
6276       index = INTVAL (x) & mask;
6277       if (bit_count (base & 0xffffffff) > (32 - bits)/2)
6278         {
6279           /* It'll most probably be more efficient to generate the base
6280              with more bits set and use a negative index instead. */
6281           base |= mask;
6282           index -= mask;
6283         }
6284       base_reg = force_reg (SImode, GEN_INT (base));
6285       x = plus_constant (base_reg, index);
6286     }
6287
6288   if (flag_pic)
6289     {
6290       /* We need to find and carefully transform any SYMBOL and LABEL
6291          references; so go back to the original address expression.  */
6292       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
6293
6294       if (new_x != orig_x)
6295         x = new_x;
6296     }
6297
6298   return x;
6299 }
6300
6301
6302 /* Try machine-dependent ways of modifying an illegitimate Thumb address
6303    to be legitimate.  If we find one, return the new, valid address.  */
6304 rtx
6305 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
6306 {
6307   if (arm_tls_symbol_p (x))
6308     return legitimize_tls_address (x, NULL_RTX);
6309
6310   if (GET_CODE (x) == PLUS
6311       && GET_CODE (XEXP (x, 1)) == CONST_INT
6312       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
6313           || INTVAL (XEXP (x, 1)) < 0))
6314     {
6315       rtx xop0 = XEXP (x, 0);
6316       rtx xop1 = XEXP (x, 1);
6317       HOST_WIDE_INT offset = INTVAL (xop1);
6318
6319       /* Try and fold the offset into a biasing of the base register and
6320          then offsetting that.  Don't do this when optimizing for space
6321          since it can cause too many CSEs.  */
6322       if (optimize_size && offset >= 0
6323           && offset < 256 + 31 * GET_MODE_SIZE (mode))
6324         {
6325           HOST_WIDE_INT delta;
6326
6327           if (offset >= 256)
6328             delta = offset - (256 - GET_MODE_SIZE (mode));
6329           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
6330             delta = 31 * GET_MODE_SIZE (mode);
6331           else
6332             delta = offset & (~31 * GET_MODE_SIZE (mode));
6333
6334           xop0 = force_operand (plus_constant (xop0, offset - delta),
6335                                 NULL_RTX);
6336           x = plus_constant (xop0, delta);
6337         }
6338       else if (offset < 0 && offset > -256)
6339         /* Small negative offsets are best done with a subtract before the
6340            dereference, forcing these into a register normally takes two
6341            instructions.  */
6342         x = force_operand (x, NULL_RTX);
6343       else
6344         {
6345           /* For the remaining cases, force the constant into a register.  */
6346           xop1 = force_reg (SImode, xop1);
6347           x = gen_rtx_PLUS (SImode, xop0, xop1);
6348         }
6349     }
6350   else if (GET_CODE (x) == PLUS
6351            && s_register_operand (XEXP (x, 1), SImode)
6352            && !s_register_operand (XEXP (x, 0), SImode))
6353     {
6354       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
6355
6356       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
6357     }
6358
6359   if (flag_pic)
6360     {
6361       /* We need to find and carefully transform any SYMBOL and LABEL
6362          references; so go back to the original address expression.  */
6363       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
6364
6365       if (new_x != orig_x)
6366         x = new_x;
6367     }
6368
6369   return x;
6370 }
6371
6372 rtx
6373 thumb_legitimize_reload_address (rtx *x_p,
6374                                  enum machine_mode mode,
6375                                  int opnum, int type,
6376                                  int ind_levels ATTRIBUTE_UNUSED)
6377 {
6378   rtx x = *x_p;
6379
6380   if (GET_CODE (x) == PLUS
6381       && GET_MODE_SIZE (mode) < 4
6382       && REG_P (XEXP (x, 0))
6383       && XEXP (x, 0) == stack_pointer_rtx
6384       && GET_CODE (XEXP (x, 1)) == CONST_INT
6385       && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
6386     {
6387       rtx orig_x = x;
6388
6389       x = copy_rtx (x);
6390       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
6391                    Pmode, VOIDmode, 0, 0, opnum, (enum reload_type) type);
6392       return x;
6393     }
6394
6395   /* If both registers are hi-regs, then it's better to reload the
6396      entire expression rather than each register individually.  That
6397      only requires one reload register rather than two.  */
6398   if (GET_CODE (x) == PLUS
6399       && REG_P (XEXP (x, 0))
6400       && REG_P (XEXP (x, 1))
6401       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
6402       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
6403     {
6404       rtx orig_x = x;
6405
6406       x = copy_rtx (x);
6407       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
6408                    Pmode, VOIDmode, 0, 0, opnum, (enum reload_type) type);
6409       return x;
6410     }
6411
6412   return NULL;
6413 }
6414
6415 /* Test for various thread-local symbols.  */
6416
6417 /* Return TRUE if X is a thread-local symbol.  */
6418
6419 static bool
6420 arm_tls_symbol_p (rtx x)
6421 {
6422   if (! TARGET_HAVE_TLS)
6423     return false;
6424
6425   if (GET_CODE (x) != SYMBOL_REF)
6426     return false;
6427
6428   return SYMBOL_REF_TLS_MODEL (x) != 0;
6429 }
6430
6431 /* Helper for arm_tls_referenced_p.  */
6432
6433 static int
6434 arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
6435 {
6436   if (GET_CODE (*x) == SYMBOL_REF)
6437     return SYMBOL_REF_TLS_MODEL (*x) != 0;
6438
6439   /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
6440      TLS offsets, not real symbol references.  */
6441   if (GET_CODE (*x) == UNSPEC
6442       && XINT (*x, 1) == UNSPEC_TLS)
6443     return -1;
6444
6445   return 0;
6446 }
6447
6448 /* Return TRUE if X contains any TLS symbol references.  */
6449
6450 bool
6451 arm_tls_referenced_p (rtx x)
6452 {
6453   if (! TARGET_HAVE_TLS)
6454     return false;
6455
6456   return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
6457 }
6458
6459 /* Implement TARGET_CANNOT_FORCE_CONST_MEM.  */
6460
6461 bool
6462 arm_cannot_force_const_mem (rtx x)
6463 {
6464   rtx base, offset;
6465
6466   if (ARM_OFFSETS_MUST_BE_WITHIN_SECTIONS_P)
6467     {
6468       split_const (x, &base, &offset);
6469       if (GET_CODE (base) == SYMBOL_REF
6470           && !offset_within_block_p (base, INTVAL (offset)))
6471         return true;
6472     }
6473   return arm_tls_referenced_p (x);
6474 }
6475 \f
6476 #define REG_OR_SUBREG_REG(X)                                            \
6477   (GET_CODE (X) == REG                                                  \
6478    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
6479
6480 #define REG_OR_SUBREG_RTX(X)                    \
6481    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
6482
6483 static inline int
6484 thumb1_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
6485 {
6486   enum machine_mode mode = GET_MODE (x);
6487   int total;
6488
6489   switch (code)
6490     {
6491     case ASHIFT:
6492     case ASHIFTRT:
6493     case LSHIFTRT:
6494     case ROTATERT:
6495     case PLUS:
6496     case MINUS:
6497     case COMPARE:
6498     case NEG:
6499     case NOT:
6500       return COSTS_N_INSNS (1);
6501
6502     case MULT:
6503       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6504         {
6505           int cycles = 0;
6506           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
6507
6508           while (i)
6509             {
6510               i >>= 2;
6511               cycles++;
6512             }
6513           return COSTS_N_INSNS (2) + cycles;
6514         }
6515       return COSTS_N_INSNS (1) + 16;
6516
6517     case SET:
6518       return (COSTS_N_INSNS (1)
6519               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
6520                      + GET_CODE (SET_DEST (x)) == MEM));
6521
6522     case CONST_INT:
6523       if (outer == SET)
6524         {
6525           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
6526             return 0;
6527           if (thumb_shiftable_const (INTVAL (x)))
6528             return COSTS_N_INSNS (2);
6529           return COSTS_N_INSNS (3);
6530         }
6531       else if ((outer == PLUS || outer == COMPARE)
6532                && INTVAL (x) < 256 && INTVAL (x) > -256)
6533         return 0;
6534       else if ((outer == IOR || outer == XOR || outer == AND)
6535                && INTVAL (x) < 256 && INTVAL (x) >= -256)
6536         return COSTS_N_INSNS (1);
6537       else if (outer == AND)
6538         {
6539           int i;
6540           /* This duplicates the tests in the andsi3 expander.  */
6541           for (i = 9; i <= 31; i++)
6542             if ((((HOST_WIDE_INT) 1) << i) - 1 == INTVAL (x)
6543                 || (((HOST_WIDE_INT) 1) << i) - 1 == ~INTVAL (x))
6544               return COSTS_N_INSNS (2);
6545         }
6546       else if (outer == ASHIFT || outer == ASHIFTRT
6547                || outer == LSHIFTRT)
6548         return 0;
6549       return COSTS_N_INSNS (2);
6550
6551     case CONST:
6552     case CONST_DOUBLE:
6553     case LABEL_REF:
6554     case SYMBOL_REF:
6555       return COSTS_N_INSNS (3);
6556
6557     case UDIV:
6558     case UMOD:
6559     case DIV:
6560     case MOD:
6561       return 100;
6562
6563     case TRUNCATE:
6564       return 99;
6565
6566     case AND:
6567     case XOR:
6568     case IOR:
6569       /* XXX guess.  */
6570       return 8;
6571
6572     case MEM:
6573       /* XXX another guess.  */
6574       /* Memory costs quite a lot for the first word, but subsequent words
6575          load at the equivalent of a single insn each.  */
6576       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
6577               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
6578                  ? 4 : 0));
6579
6580     case IF_THEN_ELSE:
6581       /* XXX a guess.  */
6582       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
6583         return 14;
6584       return 2;
6585
6586     case SIGN_EXTEND:
6587     case ZERO_EXTEND:
6588       total = mode == DImode ? COSTS_N_INSNS (1) : 0;
6589       total += thumb1_rtx_costs (XEXP (x, 0), GET_CODE (XEXP (x, 0)), code);
6590
6591       if (mode == SImode)
6592         return total;
6593
6594       if (arm_arch6)
6595         return total + COSTS_N_INSNS (1);
6596
6597       /* Assume a two-shift sequence.  Increase the cost slightly so
6598          we prefer actual shifts over an extend operation.  */
6599       return total + 1 + COSTS_N_INSNS (2);
6600
6601     default:
6602       return 99;
6603     }
6604 }
6605
6606 static inline bool
6607 arm_rtx_costs_1 (rtx x, enum rtx_code outer, int* total, bool speed)
6608 {
6609   enum machine_mode mode = GET_MODE (x);
6610   enum rtx_code subcode;
6611   rtx operand;
6612   enum rtx_code code = GET_CODE (x);
6613   *total = 0;
6614
6615   switch (code)
6616     {
6617     case MEM:
6618       /* Memory costs quite a lot for the first word, but subsequent words
6619          load at the equivalent of a single insn each.  */
6620       *total = COSTS_N_INSNS (2 + ARM_NUM_REGS (mode));
6621       return true;
6622
6623     case DIV:
6624     case MOD:
6625     case UDIV:
6626     case UMOD:
6627       if (TARGET_HARD_FLOAT && mode == SFmode)
6628         *total = COSTS_N_INSNS (2);
6629       else if (TARGET_HARD_FLOAT && mode == DFmode && !TARGET_VFP_SINGLE)
6630         *total = COSTS_N_INSNS (4);
6631       else
6632         *total = COSTS_N_INSNS (20);
6633       return false;
6634
6635     case ROTATE:
6636       if (GET_CODE (XEXP (x, 1)) == REG)
6637         *total = COSTS_N_INSNS (1); /* Need to subtract from 32 */
6638       else if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6639         *total = rtx_cost (XEXP (x, 1), code, speed);
6640
6641       /* Fall through */
6642     case ROTATERT:
6643       if (mode != SImode)
6644         {
6645           *total += COSTS_N_INSNS (4);
6646           return true;
6647         }
6648
6649       /* Fall through */
6650     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
6651       *total += rtx_cost (XEXP (x, 0), code, speed);
6652       if (mode == DImode)
6653         {
6654           *total += COSTS_N_INSNS (3);
6655           return true;
6656         }
6657
6658       *total += COSTS_N_INSNS (1);
6659       /* Increase the cost of complex shifts because they aren't any faster,
6660          and reduce dual issue opportunities.  */
6661       if (arm_tune_cortex_a9
6662           && outer != SET && GET_CODE (XEXP (x, 1)) != CONST_INT)
6663         ++*total;
6664
6665       return true;
6666
6667     case MINUS:
6668       if (mode == DImode)
6669         {
6670           *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
6671           if (GET_CODE (XEXP (x, 0)) == CONST_INT
6672               && const_ok_for_arm (INTVAL (XEXP (x, 0))))
6673             {
6674               *total += rtx_cost (XEXP (x, 1), code, speed);
6675               return true;
6676             }
6677
6678           if (GET_CODE (XEXP (x, 1)) == CONST_INT
6679               && const_ok_for_arm (INTVAL (XEXP (x, 1))))
6680             {
6681               *total += rtx_cost (XEXP (x, 0), code, speed);
6682               return true;
6683             }
6684
6685           return false;
6686         }
6687
6688       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6689         {
6690           if (TARGET_HARD_FLOAT
6691               && (mode == SFmode
6692                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
6693             {
6694               *total = COSTS_N_INSNS (1);
6695               if (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
6696                   && arm_const_double_rtx (XEXP (x, 0)))
6697                 {
6698                   *total += rtx_cost (XEXP (x, 1), code, speed);
6699                   return true;
6700                 }
6701
6702               if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
6703                   && arm_const_double_rtx (XEXP (x, 1)))
6704                 {
6705                   *total += rtx_cost (XEXP (x, 0), code, speed);
6706                   return true;
6707                 }
6708
6709               return false;
6710             }
6711           *total = COSTS_N_INSNS (20);
6712           return false;
6713         }
6714
6715       *total = COSTS_N_INSNS (1);
6716       if (GET_CODE (XEXP (x, 0)) == CONST_INT
6717           && const_ok_for_arm (INTVAL (XEXP (x, 0))))
6718         {
6719           *total += rtx_cost (XEXP (x, 1), code, speed);
6720           return true;
6721         }
6722
6723       subcode = GET_CODE (XEXP (x, 1));
6724       if (subcode == ASHIFT || subcode == ASHIFTRT
6725           || subcode == LSHIFTRT
6726           || subcode == ROTATE || subcode == ROTATERT)
6727         {
6728           *total += rtx_cost (XEXP (x, 0), code, speed);
6729           *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, speed);
6730           return true;
6731         }
6732
6733       /* A shift as a part of RSB costs no more than RSB itself.  */
6734       if (GET_CODE (XEXP (x, 0)) == MULT
6735           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
6736         {
6737           *total += rtx_cost (XEXP (XEXP (x, 0), 0), code, speed);
6738           *total += rtx_cost (XEXP (x, 1), code, speed);
6739           return true;
6740         }
6741
6742       if (subcode == MULT
6743           && power_of_two_operand (XEXP (XEXP (x, 1), 1), SImode))
6744         {
6745           *total += rtx_cost (XEXP (x, 0), code, speed);
6746           *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, speed);
6747           return true;
6748         }
6749
6750       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMPARE
6751           || GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMM_COMPARE)
6752         {
6753           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
6754           if (GET_CODE (XEXP (XEXP (x, 1), 0)) == REG
6755               && REGNO (XEXP (XEXP (x, 1), 0)) != CC_REGNUM)
6756             *total += COSTS_N_INSNS (1);
6757
6758           return true;
6759         }
6760
6761       /* Fall through */
6762
6763     case PLUS:
6764       if (code == PLUS && arm_arch6 && mode == SImode
6765           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
6766               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
6767         {
6768           *total = COSTS_N_INSNS (1);
6769           *total += rtx_cost (XEXP (XEXP (x, 0), 0), GET_CODE (XEXP (x, 0)),
6770                               speed);
6771           *total += rtx_cost (XEXP (x, 1), code, speed);
6772           return true;
6773         }
6774
6775       /* MLA: All arguments must be registers.  We filter out
6776          multiplication by a power of two, so that we fall down into
6777          the code below.  */
6778       if (GET_CODE (XEXP (x, 0)) == MULT
6779           && !power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
6780         {
6781           /* The cost comes from the cost of the multiply.  */
6782           return false;
6783         }
6784
6785       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6786         {
6787           if (TARGET_HARD_FLOAT
6788               && (mode == SFmode
6789                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
6790             {
6791               *total = COSTS_N_INSNS (1);
6792               if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
6793                   && arm_const_double_rtx (XEXP (x, 1)))
6794                 {
6795                   *total += rtx_cost (XEXP (x, 0), code, speed);
6796                   return true;
6797                 }
6798
6799               return false;
6800             }
6801
6802           *total = COSTS_N_INSNS (20);
6803           return false;
6804         }
6805
6806       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMPARE
6807           || GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMM_COMPARE)
6808         {
6809           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 1), code, speed);
6810           if (GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
6811               && REGNO (XEXP (XEXP (x, 0), 0)) != CC_REGNUM)
6812             *total += COSTS_N_INSNS (1);
6813           return true;
6814         }
6815
6816       /* Fall through */
6817
6818     case AND: case XOR: case IOR:
6819
6820       /* Normally the frame registers will be spilt into reg+const during
6821          reload, so it is a bad idea to combine them with other instructions,
6822          since then they might not be moved outside of loops.  As a compromise
6823          we allow integration with ops that have a constant as their second
6824          operand.  */
6825       if (REG_OR_SUBREG_REG (XEXP (x, 0))
6826           && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
6827           && GET_CODE (XEXP (x, 1)) != CONST_INT)
6828         *total = COSTS_N_INSNS (1);
6829
6830       if (mode == DImode)
6831         {
6832           *total += COSTS_N_INSNS (2);
6833           if (GET_CODE (XEXP (x, 1)) == CONST_INT
6834               && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
6835             {
6836               *total += rtx_cost (XEXP (x, 0), code, speed);
6837               return true;
6838             }
6839
6840           return false;
6841         }
6842
6843       *total += COSTS_N_INSNS (1);
6844       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6845           && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
6846         {
6847           *total += rtx_cost (XEXP (x, 0), code, speed);
6848           return true;
6849         }
6850       subcode = GET_CODE (XEXP (x, 0));
6851       if (subcode == ASHIFT || subcode == ASHIFTRT
6852           || subcode == LSHIFTRT
6853           || subcode == ROTATE || subcode == ROTATERT)
6854         {
6855           *total += rtx_cost (XEXP (x, 1), code, speed);
6856           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6857           return true;
6858         }
6859
6860       if (subcode == MULT
6861           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
6862         {
6863           *total += rtx_cost (XEXP (x, 1), code, speed);
6864           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6865           return true;
6866         }
6867
6868       if (subcode == UMIN || subcode == UMAX
6869           || subcode == SMIN || subcode == SMAX)
6870         {
6871           *total = COSTS_N_INSNS (3);
6872           return true;
6873         }
6874
6875       return false;
6876
6877     case MULT:
6878       /* This should have been handled by the CPU specific routines.  */
6879       gcc_unreachable ();
6880
6881     case TRUNCATE:
6882       if (arm_arch3m && mode == SImode
6883           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
6884           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
6885           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
6886               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
6887           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
6888               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
6889         {
6890           *total = rtx_cost (XEXP (XEXP (x, 0), 0), LSHIFTRT, speed);
6891           return true;
6892         }
6893       *total = COSTS_N_INSNS (2); /* Plus the cost of the MULT */
6894       return false;
6895
6896     case NEG:
6897       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6898         {
6899           if (TARGET_HARD_FLOAT
6900               && (mode == SFmode
6901                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
6902             {
6903               *total = COSTS_N_INSNS (1);
6904               return false;
6905             }
6906           *total = COSTS_N_INSNS (2);
6907           return false;
6908         }
6909
6910       /* Fall through */
6911     case NOT:
6912       *total = COSTS_N_INSNS (ARM_NUM_REGS(mode));
6913       if (mode == SImode && code == NOT)
6914         {
6915           subcode = GET_CODE (XEXP (x, 0));
6916           if (subcode == ASHIFT || subcode == ASHIFTRT
6917               || subcode == LSHIFTRT
6918               || subcode == ROTATE || subcode == ROTATERT
6919               || (subcode == MULT
6920                   && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode)))
6921             {
6922               *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6923               /* Register shifts cost an extra cycle.  */
6924               if (GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
6925                 *total += COSTS_N_INSNS (1) + rtx_cost (XEXP (XEXP (x, 0), 1),
6926                                                         subcode, speed);
6927               return true;
6928             }
6929         }
6930
6931       return false;
6932
6933     case IF_THEN_ELSE:
6934       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
6935         {
6936           *total = COSTS_N_INSNS (4);
6937           return true;
6938         }
6939
6940       operand = XEXP (x, 0);
6941
6942       if (!((GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMPARE
6943              || GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMM_COMPARE)
6944             && GET_CODE (XEXP (operand, 0)) == REG
6945             && REGNO (XEXP (operand, 0)) == CC_REGNUM))
6946         *total += COSTS_N_INSNS (1);
6947       *total += (rtx_cost (XEXP (x, 1), code, speed)
6948                  + rtx_cost (XEXP (x, 2), code, speed));
6949       return true;
6950
6951     case NE:
6952       if (mode == SImode && XEXP (x, 1) == const0_rtx)
6953         {
6954           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
6955           return true;
6956         }
6957       goto scc_insn;
6958
6959     case GE:
6960       if ((GET_CODE (XEXP (x, 0)) != REG || REGNO (XEXP (x, 0)) != CC_REGNUM)
6961           && mode == SImode && XEXP (x, 1) == const0_rtx)
6962         {
6963           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
6964           return true;
6965         }
6966       goto scc_insn;
6967
6968     case LT:
6969       if ((GET_CODE (XEXP (x, 0)) != REG || REGNO (XEXP (x, 0)) != CC_REGNUM)
6970           && mode == SImode && XEXP (x, 1) == const0_rtx)
6971         {
6972           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
6973           return true;
6974         }
6975       goto scc_insn;
6976
6977     case EQ:
6978     case GT:
6979     case LE:
6980     case GEU:
6981     case LTU:
6982     case GTU:
6983     case LEU:
6984     case UNORDERED:
6985     case ORDERED:
6986     case UNEQ:
6987     case UNGE:
6988     case UNLT:
6989     case UNGT:
6990     case UNLE:
6991     scc_insn:
6992       /* SCC insns.  In the case where the comparison has already been
6993          performed, then they cost 2 instructions.  Otherwise they need
6994          an additional comparison before them.  */
6995       *total = COSTS_N_INSNS (2);
6996       if (GET_CODE (XEXP (x, 0)) == REG && REGNO (XEXP (x, 0)) == CC_REGNUM)
6997         {
6998           return true;
6999         }
7000
7001       /* Fall through */
7002     case COMPARE:
7003       if (GET_CODE (XEXP (x, 0)) == REG && REGNO (XEXP (x, 0)) == CC_REGNUM)
7004         {
7005           *total = 0;
7006           return true;
7007         }
7008
7009       *total += COSTS_N_INSNS (1);
7010       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7011           && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
7012         {
7013           *total += rtx_cost (XEXP (x, 0), code, speed);
7014           return true;
7015         }
7016
7017       subcode = GET_CODE (XEXP (x, 0));
7018       if (subcode == ASHIFT || subcode == ASHIFTRT
7019           || subcode == LSHIFTRT
7020           || subcode == ROTATE || subcode == ROTATERT)
7021         {
7022           *total += rtx_cost (XEXP (x, 1), code, speed);
7023           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
7024           return true;
7025         }
7026
7027       if (subcode == MULT
7028           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
7029         {
7030           *total += rtx_cost (XEXP (x, 1), code, speed);
7031           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
7032           return true;
7033         }
7034       
7035       return false;
7036
7037     case UMIN:
7038     case UMAX:
7039     case SMIN:
7040     case SMAX:
7041       *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
7042       if (GET_CODE (XEXP (x, 1)) != CONST_INT
7043           || !const_ok_for_arm (INTVAL (XEXP (x, 1))))
7044         *total += rtx_cost (XEXP (x, 1), code, speed);
7045       return true;
7046
7047     case ABS:
7048       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7049         {
7050           if (TARGET_HARD_FLOAT
7051               && (mode == SFmode
7052                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
7053             {
7054               *total = COSTS_N_INSNS (1);
7055               return false;
7056             }
7057           *total = COSTS_N_INSNS (20);
7058           return false;
7059         }
7060       *total = COSTS_N_INSNS (1);
7061       if (mode == DImode)
7062         *total += COSTS_N_INSNS (3);
7063       return false;
7064
7065     case SIGN_EXTEND:
7066     case ZERO_EXTEND:
7067       *total = 0;
7068       if (GET_MODE_CLASS (mode) == MODE_INT)
7069         {
7070           rtx op = XEXP (x, 0);
7071           enum machine_mode opmode = GET_MODE (op);
7072
7073           if (mode == DImode)
7074             *total += COSTS_N_INSNS (1);
7075
7076           if (opmode != SImode)
7077             {
7078               if (MEM_P (op))
7079                 {
7080                   /* If !arm_arch4, we use one of the extendhisi2_mem
7081                      or movhi_bytes patterns for HImode.  For a QImode
7082                      sign extension, we first zero-extend from memory
7083                      and then perform a shift sequence.  */
7084                   if (!arm_arch4 && (opmode != QImode || code == SIGN_EXTEND))
7085                     *total += COSTS_N_INSNS (2);
7086                 }
7087               else if (arm_arch6)
7088                 *total += COSTS_N_INSNS (1);
7089
7090               /* We don't have the necessary insn, so we need to perform some
7091                  other operation.  */
7092               else if (TARGET_ARM && code == ZERO_EXTEND && mode == QImode)
7093                 /* An and with constant 255.  */
7094                 *total += COSTS_N_INSNS (1);
7095               else
7096                 /* A shift sequence.  Increase costs slightly to avoid
7097                    combining two shifts into an extend operation.  */
7098                 *total += COSTS_N_INSNS (2) + 1;
7099             }
7100
7101           return false;
7102         }
7103
7104       switch (GET_MODE (XEXP (x, 0)))
7105         {
7106         case V8QImode:
7107         case V4HImode:
7108         case V2SImode:
7109         case V4QImode:
7110         case V2HImode:
7111           *total = COSTS_N_INSNS (1);
7112           return false;
7113
7114         default:
7115           gcc_unreachable ();
7116         }
7117       gcc_unreachable ();
7118
7119     case ZERO_EXTRACT:
7120     case SIGN_EXTRACT:
7121       *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
7122       return true;
7123
7124     case CONST_INT:
7125       if (const_ok_for_arm (INTVAL (x))
7126           || const_ok_for_arm (~INTVAL (x)))
7127         *total = COSTS_N_INSNS (1);
7128       else
7129         *total = COSTS_N_INSNS (arm_gen_constant (SET, mode, NULL_RTX,
7130                                                   INTVAL (x), NULL_RTX,
7131                                                   NULL_RTX, 0, 0));
7132       return true;
7133
7134     case CONST:
7135     case LABEL_REF:
7136     case SYMBOL_REF:
7137       *total = COSTS_N_INSNS (3);
7138       return true;
7139
7140     case HIGH:
7141       *total = COSTS_N_INSNS (1);
7142       return true;
7143
7144     case LO_SUM:
7145       *total = COSTS_N_INSNS (1);
7146       *total += rtx_cost (XEXP (x, 0), code, speed);
7147       return true;
7148
7149     case CONST_DOUBLE:
7150       if (TARGET_HARD_FLOAT && vfp3_const_double_rtx (x)
7151           && (mode == SFmode || !TARGET_VFP_SINGLE))
7152         *total = COSTS_N_INSNS (1);
7153       else
7154         *total = COSTS_N_INSNS (4);
7155       return true;
7156
7157     default:
7158       *total = COSTS_N_INSNS (4);
7159       return false;
7160     }
7161 }
7162
7163 /* Estimates the size cost of thumb1 instructions.
7164    For now most of the code is copied from thumb1_rtx_costs. We need more
7165    fine grain tuning when we have more related test cases.  */
7166 static inline int
7167 thumb1_size_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
7168 {
7169   enum machine_mode mode = GET_MODE (x);
7170
7171   switch (code)
7172     {
7173     case ASHIFT:
7174     case ASHIFTRT:
7175     case LSHIFTRT:
7176     case ROTATERT:
7177     case PLUS:
7178     case MINUS:
7179     case COMPARE:
7180     case NEG:
7181     case NOT:
7182       return COSTS_N_INSNS (1);
7183
7184     case MULT:
7185       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7186         {
7187           /* Thumb1 mul instruction can't operate on const. We must Load it
7188              into a register first.  */
7189           int const_size = thumb1_size_rtx_costs (XEXP (x, 1), CONST_INT, SET);
7190           return COSTS_N_INSNS (1) + const_size;
7191         }
7192       return COSTS_N_INSNS (1);
7193
7194     case SET:
7195       return (COSTS_N_INSNS (1)
7196               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
7197                      + GET_CODE (SET_DEST (x)) == MEM));
7198
7199     case CONST_INT:
7200       if (outer == SET)
7201         {
7202           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
7203             return COSTS_N_INSNS (1);
7204           /* See split "TARGET_THUMB1 && satisfies_constraint_J".  */
7205           if (INTVAL (x) >= -255 && INTVAL (x) <= -1)
7206             return COSTS_N_INSNS (2);
7207           /* See split "TARGET_THUMB1 && satisfies_constraint_K".  */
7208           if (thumb_shiftable_const (INTVAL (x)))
7209             return COSTS_N_INSNS (2);
7210           return COSTS_N_INSNS (3);
7211         }
7212       else if ((outer == PLUS || outer == COMPARE)
7213                && INTVAL (x) < 256 && INTVAL (x) > -256)
7214         return 0;
7215       else if ((outer == IOR || outer == XOR || outer == AND)
7216                && INTVAL (x) < 256 && INTVAL (x) >= -256)
7217         return COSTS_N_INSNS (1);
7218       else if (outer == AND)
7219         {
7220           int i;
7221           /* This duplicates the tests in the andsi3 expander.  */
7222           for (i = 9; i <= 31; i++)
7223             if ((((HOST_WIDE_INT) 1) << i) - 1 == INTVAL (x)
7224                 || (((HOST_WIDE_INT) 1) << i) - 1 == ~INTVAL (x))
7225               return COSTS_N_INSNS (2);
7226         }
7227       else if (outer == ASHIFT || outer == ASHIFTRT
7228                || outer == LSHIFTRT)
7229         return 0;
7230       return COSTS_N_INSNS (2);
7231
7232     case CONST:
7233     case CONST_DOUBLE:
7234     case LABEL_REF:
7235     case SYMBOL_REF:
7236       return COSTS_N_INSNS (3);
7237
7238     case UDIV:
7239     case UMOD:
7240     case DIV:
7241     case MOD:
7242       return 100;
7243
7244     case TRUNCATE:
7245       return 99;
7246
7247     case AND:
7248     case XOR:
7249     case IOR:
7250       /* XXX guess.  */
7251       return 8;
7252
7253     case MEM:
7254       /* XXX another guess.  */
7255       /* Memory costs quite a lot for the first word, but subsequent words
7256          load at the equivalent of a single insn each.  */
7257       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
7258               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
7259                  ? 4 : 0));
7260
7261     case IF_THEN_ELSE:
7262       /* XXX a guess.  */
7263       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
7264         return 14;
7265       return 2;
7266
7267     case ZERO_EXTEND:
7268       /* XXX still guessing.  */
7269       switch (GET_MODE (XEXP (x, 0)))
7270         {
7271           case QImode:
7272             return (1 + (mode == DImode ? 4 : 0)
7273                     + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
7274
7275           case HImode:
7276             return (4 + (mode == DImode ? 4 : 0)
7277                     + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
7278
7279           case SImode:
7280             return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
7281
7282           default:
7283             return 99;
7284         }
7285
7286     default:
7287       return 99;
7288     }
7289 }
7290
7291 /* RTX costs when optimizing for size.  */
7292 static bool
7293 arm_size_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7294                     int *total)
7295 {
7296   enum machine_mode mode = GET_MODE (x);
7297   if (TARGET_THUMB1)
7298     {
7299       *total = thumb1_size_rtx_costs (x, code, outer_code);
7300       return true;
7301     }
7302
7303   /* FIXME: This makes no attempt to prefer narrow Thumb-2 instructions.  */
7304   switch (code)
7305     {
7306     case MEM:
7307       /* A memory access costs 1 insn if the mode is small, or the address is
7308          a single register, otherwise it costs one insn per word.  */
7309       if (REG_P (XEXP (x, 0)))
7310         *total = COSTS_N_INSNS (1);
7311       else if (flag_pic
7312                && GET_CODE (XEXP (x, 0)) == PLUS
7313                && will_be_in_index_register (XEXP (XEXP (x, 0), 1)))
7314         /* This will be split into two instructions.
7315            See arm.md:calculate_pic_address.  */
7316         *total = COSTS_N_INSNS (2);
7317       else
7318         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7319       return true;
7320
7321     case DIV:
7322     case MOD:
7323     case UDIV:
7324     case UMOD:
7325       /* Needs a libcall, so it costs about this.  */
7326       *total = COSTS_N_INSNS (2);
7327       return false;
7328
7329     case ROTATE:
7330       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
7331         {
7332           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, false);
7333           return true;
7334         }
7335       /* Fall through */
7336     case ROTATERT:
7337     case ASHIFT:
7338     case LSHIFTRT:
7339     case ASHIFTRT:
7340       if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
7341         {
7342           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code, false);
7343           return true;
7344         }
7345       else if (mode == SImode)
7346         {
7347           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, false);
7348           /* Slightly disparage register shifts, but not by much.  */
7349           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
7350             *total += 1 + rtx_cost (XEXP (x, 1), code, false);
7351           return true;
7352         }
7353
7354       /* Needs a libcall.  */
7355       *total = COSTS_N_INSNS (2);
7356       return false;
7357
7358     case MINUS:
7359       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7360           && (mode == SFmode || !TARGET_VFP_SINGLE))
7361         {
7362           *total = COSTS_N_INSNS (1);
7363           return false;
7364         }
7365
7366       if (mode == SImode)
7367         {
7368           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
7369           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
7370
7371           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
7372               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
7373               || subcode1 == ROTATE || subcode1 == ROTATERT
7374               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
7375               || subcode1 == ASHIFTRT)
7376             {
7377               /* It's just the cost of the two operands.  */
7378               *total = 0;
7379               return false;
7380             }
7381
7382           *total = COSTS_N_INSNS (1);
7383           return false;
7384         }
7385
7386       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7387       return false;
7388
7389     case PLUS:
7390       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7391           && (mode == SFmode || !TARGET_VFP_SINGLE))
7392         {
7393           *total = COSTS_N_INSNS (1);
7394           return false;
7395         }
7396
7397       /* A shift as a part of ADD costs nothing.  */
7398       if (GET_CODE (XEXP (x, 0)) == MULT
7399           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
7400         {
7401           *total = COSTS_N_INSNS (TARGET_THUMB2 ? 2 : 1);
7402           *total += rtx_cost (XEXP (XEXP (x, 0), 0), code, false);
7403           *total += rtx_cost (XEXP (x, 1), code, false);
7404           return true;
7405         }
7406
7407       /* Fall through */
7408     case AND: case XOR: case IOR:
7409       if (mode == SImode)
7410         {
7411           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
7412
7413           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
7414               || subcode == LSHIFTRT || subcode == ASHIFTRT
7415               || (code == AND && subcode == NOT))
7416             {
7417               /* It's just the cost of the two operands.  */
7418               *total = 0;
7419               return false;
7420             }
7421         }
7422
7423       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7424       return false;
7425
7426     case MULT:
7427       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7428       return false;
7429
7430     case NEG:
7431       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7432           && (mode == SFmode || !TARGET_VFP_SINGLE))
7433         {
7434           *total = COSTS_N_INSNS (1);
7435           return false;
7436         }
7437
7438       /* Fall through */
7439     case NOT:
7440       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7441
7442       return false;
7443
7444     case IF_THEN_ELSE:
7445       *total = 0;
7446       return false;
7447
7448     case COMPARE:
7449       if (cc_register (XEXP (x, 0), VOIDmode))
7450         * total = 0;
7451       else
7452         *total = COSTS_N_INSNS (1);
7453       return false;
7454
7455     case ABS:
7456       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7457           && (mode == SFmode || !TARGET_VFP_SINGLE))
7458         *total = COSTS_N_INSNS (1);
7459       else
7460         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
7461       return false;
7462
7463     case SIGN_EXTEND:
7464     case ZERO_EXTEND:
7465       return arm_rtx_costs_1 (x, outer_code, total, 0);
7466
7467     case CONST_INT:
7468       if (const_ok_for_arm (INTVAL (x)))
7469         /* A multiplication by a constant requires another instruction
7470            to load the constant to a register.  */
7471         *total = COSTS_N_INSNS ((outer_code == SET || outer_code == MULT)
7472                                 ? 1 : 0);
7473       else if (const_ok_for_arm (~INTVAL (x)))
7474         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
7475       else if (const_ok_for_arm (-INTVAL (x)))
7476         {
7477           if (outer_code == COMPARE || outer_code == PLUS
7478               || outer_code == MINUS)
7479             *total = 0;
7480           else
7481             *total = COSTS_N_INSNS (1);
7482         }
7483       else
7484         *total = COSTS_N_INSNS (2);
7485       return true;
7486
7487     case CONST:
7488     case LABEL_REF:
7489     case SYMBOL_REF:
7490       *total = COSTS_N_INSNS (2);
7491       return true;
7492
7493     case CONST_DOUBLE:
7494       *total = COSTS_N_INSNS (4);
7495       return true;
7496
7497     case HIGH:
7498     case LO_SUM:
7499       /* We prefer constant pool entries to MOVW/MOVT pairs, so bump the
7500          cost of these slightly.  */
7501       *total = COSTS_N_INSNS (1) + 1;
7502       return true;
7503
7504     default:
7505       if (mode != VOIDmode)
7506         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7507       else
7508         *total = COSTS_N_INSNS (4); /* How knows?  */
7509       return false;
7510     }
7511 }
7512
7513 /* RTX costs when optimizing for size.  */
7514 static bool
7515 arm_rtx_costs (rtx x, int code, int outer_code, int *total,
7516                bool speed)
7517 {
7518   if (!speed)
7519     return arm_size_rtx_costs (x, (enum rtx_code) code,
7520                                (enum rtx_code) outer_code, total);
7521   else
7522     return current_tune->rtx_costs (x, (enum rtx_code) code,
7523                                     (enum rtx_code) outer_code,
7524                                     total, speed);
7525 }
7526
7527 /* RTX costs for cores with a slow MUL implementation.  Thumb-2 is not
7528    supported on any "slowmul" cores, so it can be ignored.  */
7529
7530 static bool
7531 arm_slowmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7532                        int *total, bool speed)
7533 {
7534   enum machine_mode mode = GET_MODE (x);
7535
7536   if (TARGET_THUMB)
7537     {
7538       *total = thumb1_rtx_costs (x, code, outer_code);
7539       return true;
7540     }
7541
7542   switch (code)
7543     {
7544     case MULT:
7545       if (GET_MODE_CLASS (mode) == MODE_FLOAT
7546           || mode == DImode)
7547         {
7548           *total = COSTS_N_INSNS (20);
7549           return false;
7550         }
7551
7552       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7553         {
7554           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
7555                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
7556           int cost, const_ok = const_ok_for_arm (i);
7557           int j, booth_unit_size;
7558
7559           /* Tune as appropriate.  */
7560           cost = const_ok ? 4 : 8;
7561           booth_unit_size = 2;
7562           for (j = 0; i && j < 32; j += booth_unit_size)
7563             {
7564               i >>= booth_unit_size;
7565               cost++;
7566             }
7567
7568           *total = COSTS_N_INSNS (cost);
7569           *total += rtx_cost (XEXP (x, 0), code, speed);
7570           return true;
7571         }
7572
7573       *total = COSTS_N_INSNS (20);
7574       return false;
7575
7576     default:
7577       return arm_rtx_costs_1 (x, outer_code, total, speed);;
7578     }
7579 }
7580
7581
7582 /* RTX cost for cores with a fast multiply unit (M variants).  */
7583
7584 static bool
7585 arm_fastmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7586                        int *total, bool speed)
7587 {
7588   enum machine_mode mode = GET_MODE (x);
7589
7590   if (TARGET_THUMB1)
7591     {
7592       *total = thumb1_rtx_costs (x, code, outer_code);
7593       return true;
7594     }
7595
7596   /* ??? should thumb2 use different costs?  */
7597   switch (code)
7598     {
7599     case MULT:
7600       /* There is no point basing this on the tuning, since it is always the
7601          fast variant if it exists at all.  */
7602       if (mode == DImode
7603           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
7604           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7605               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7606         {
7607           *total = COSTS_N_INSNS(2);
7608           return false;
7609         }
7610
7611
7612       if (mode == DImode)
7613         {
7614           *total = COSTS_N_INSNS (5);
7615           return false;
7616         }
7617
7618       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7619         {
7620           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
7621                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
7622           int cost, const_ok = const_ok_for_arm (i);
7623           int j, booth_unit_size;
7624
7625           /* Tune as appropriate.  */
7626           cost = const_ok ? 4 : 8;
7627           booth_unit_size = 8;
7628           for (j = 0; i && j < 32; j += booth_unit_size)
7629             {
7630               i >>= booth_unit_size;
7631               cost++;
7632             }
7633
7634           *total = COSTS_N_INSNS(cost);
7635           return false;
7636         }
7637
7638       if (mode == SImode)
7639         {
7640           *total = COSTS_N_INSNS (4);
7641           return false;
7642         }
7643
7644       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7645         {
7646           if (TARGET_HARD_FLOAT
7647               && (mode == SFmode
7648                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
7649             {
7650               *total = COSTS_N_INSNS (1);
7651               return false;
7652             }
7653         }
7654
7655       /* Requires a lib call */
7656       *total = COSTS_N_INSNS (20);
7657       return false;
7658
7659     default:
7660       return arm_rtx_costs_1 (x, outer_code, total, speed);
7661     }
7662 }
7663
7664
7665 /* RTX cost for XScale CPUs.  Thumb-2 is not supported on any xscale cores,
7666    so it can be ignored.  */
7667
7668 static bool
7669 arm_xscale_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7670                       int *total, bool speed)
7671 {
7672   enum machine_mode mode = GET_MODE (x);
7673
7674   if (TARGET_THUMB)
7675     {
7676       *total = thumb1_rtx_costs (x, code, outer_code);
7677       return true;
7678     }
7679
7680   switch (code)
7681     {
7682     case COMPARE:
7683       if (GET_CODE (XEXP (x, 0)) != MULT)
7684         return arm_rtx_costs_1 (x, outer_code, total, speed);
7685
7686       /* A COMPARE of a MULT is slow on XScale; the muls instruction
7687          will stall until the multiplication is complete.  */
7688       *total = COSTS_N_INSNS (3);
7689       return false;
7690
7691     case MULT:
7692       /* There is no point basing this on the tuning, since it is always the
7693          fast variant if it exists at all.  */
7694       if (mode == DImode
7695           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
7696           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7697               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7698         {
7699           *total = COSTS_N_INSNS (2);
7700           return false;
7701         }
7702
7703
7704       if (mode == DImode)
7705         {
7706           *total = COSTS_N_INSNS (5);
7707           return false;
7708         }
7709
7710       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7711         {
7712           /* If operand 1 is a constant we can more accurately
7713              calculate the cost of the multiply.  The multiplier can
7714              retire 15 bits on the first cycle and a further 12 on the
7715              second.  We do, of course, have to load the constant into
7716              a register first.  */
7717           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
7718           /* There's a general overhead of one cycle.  */
7719           int cost = 1;
7720           unsigned HOST_WIDE_INT masked_const;
7721
7722           if (i & 0x80000000)
7723             i = ~i;
7724
7725           i &= (unsigned HOST_WIDE_INT) 0xffffffff;
7726
7727           masked_const = i & 0xffff8000;
7728           if (masked_const != 0)
7729             {
7730               cost++;
7731               masked_const = i & 0xf8000000;
7732               if (masked_const != 0)
7733                 cost++;
7734             }
7735           *total = COSTS_N_INSNS (cost);
7736           return false;
7737         }
7738
7739       if (mode == SImode)
7740         {
7741           *total = COSTS_N_INSNS (3);
7742           return false;
7743         }
7744
7745       /* Requires a lib call */
7746       *total = COSTS_N_INSNS (20);
7747       return false;
7748
7749     default:
7750       return arm_rtx_costs_1 (x, outer_code, total, speed);
7751     }
7752 }
7753
7754
7755 /* RTX costs for 9e (and later) cores.  */
7756
7757 static bool
7758 arm_9e_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7759                   int *total, bool speed)
7760 {
7761   enum machine_mode mode = GET_MODE (x);
7762
7763   if (TARGET_THUMB1)
7764     {
7765       switch (code)
7766         {
7767         case MULT:
7768           *total = COSTS_N_INSNS (3);
7769           return true;
7770
7771         default:
7772           *total = thumb1_rtx_costs (x, code, outer_code);
7773           return true;
7774         }
7775     }
7776
7777   switch (code)
7778     {
7779     case MULT:
7780       /* There is no point basing this on the tuning, since it is always the
7781          fast variant if it exists at all.  */
7782       if (mode == DImode
7783           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
7784           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7785               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7786         {
7787           *total = COSTS_N_INSNS (2);
7788           return false;
7789         }
7790
7791
7792       if (mode == DImode)
7793         {
7794           *total = COSTS_N_INSNS (5);
7795           return false;
7796         }
7797
7798       if (mode == SImode)
7799         {
7800           *total = COSTS_N_INSNS (2);
7801           return false;
7802         }
7803
7804       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7805         {
7806           if (TARGET_HARD_FLOAT
7807               && (mode == SFmode
7808                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
7809             {
7810               *total = COSTS_N_INSNS (1);
7811               return false;
7812             }
7813         }
7814
7815       *total = COSTS_N_INSNS (20);
7816       return false;
7817
7818     default:
7819       return arm_rtx_costs_1 (x, outer_code, total, speed);
7820     }
7821 }
7822 /* All address computations that can be done are free, but rtx cost returns
7823    the same for practically all of them.  So we weight the different types
7824    of address here in the order (most pref first):
7825    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
7826 static inline int
7827 arm_arm_address_cost (rtx x)
7828 {
7829   enum rtx_code c  = GET_CODE (x);
7830
7831   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
7832     return 0;
7833   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
7834     return 10;
7835
7836   if (c == PLUS)
7837     {
7838       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7839         return 2;
7840
7841       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
7842         return 3;
7843
7844       return 4;
7845     }
7846
7847   return 6;
7848 }
7849
7850 static inline int
7851 arm_thumb_address_cost (rtx x)
7852 {
7853   enum rtx_code c  = GET_CODE (x);
7854
7855   if (c == REG)
7856     return 1;
7857   if (c == PLUS
7858       && GET_CODE (XEXP (x, 0)) == REG
7859       && GET_CODE (XEXP (x, 1)) == CONST_INT)
7860     return 1;
7861
7862   return 2;
7863 }
7864
7865 static int
7866 arm_address_cost (rtx x, bool speed ATTRIBUTE_UNUSED)
7867 {
7868   return TARGET_32BIT ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
7869 }
7870
7871 /* Adjust cost hook for XScale.  */
7872 static bool
7873 xscale_sched_adjust_cost (rtx insn, rtx link, rtx dep, int * cost)
7874 {
7875   /* Some true dependencies can have a higher cost depending
7876      on precisely how certain input operands are used.  */
7877   if (REG_NOTE_KIND(link) == 0
7878       && recog_memoized (insn) >= 0
7879       && recog_memoized (dep) >= 0)
7880     {
7881       int shift_opnum = get_attr_shift (insn);
7882       enum attr_type attr_type = get_attr_type (dep);
7883
7884       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
7885          operand for INSN.  If we have a shifted input operand and the
7886          instruction we depend on is another ALU instruction, then we may
7887          have to account for an additional stall.  */
7888       if (shift_opnum != 0
7889           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
7890         {
7891           rtx shifted_operand;
7892           int opno;
7893
7894           /* Get the shifted operand.  */
7895           extract_insn (insn);
7896           shifted_operand = recog_data.operand[shift_opnum];
7897
7898           /* Iterate over all the operands in DEP.  If we write an operand
7899              that overlaps with SHIFTED_OPERAND, then we have increase the
7900              cost of this dependency.  */
7901           extract_insn (dep);
7902           preprocess_constraints ();
7903           for (opno = 0; opno < recog_data.n_operands; opno++)
7904             {
7905               /* We can ignore strict inputs.  */
7906               if (recog_data.operand_type[opno] == OP_IN)
7907                 continue;
7908
7909               if (reg_overlap_mentioned_p (recog_data.operand[opno],
7910                                            shifted_operand))
7911                 {
7912                   *cost = 2;
7913                   return false;
7914                 }
7915             }
7916         }
7917     }
7918   return true;
7919 }
7920
7921 /* Adjust cost hook for Cortex A9.  */
7922 static bool
7923 cortex_a9_sched_adjust_cost (rtx insn, rtx link, rtx dep, int * cost)
7924 {
7925   switch (REG_NOTE_KIND (link))
7926     {
7927     case REG_DEP_ANTI:
7928       *cost = 0;
7929       return false;
7930
7931     case REG_DEP_TRUE:
7932     case REG_DEP_OUTPUT:
7933         if (recog_memoized (insn) >= 0
7934             && recog_memoized (dep) >= 0)
7935           {
7936             if (GET_CODE (PATTERN (insn)) == SET)
7937               {
7938                 if (GET_MODE_CLASS 
7939                     (GET_MODE (SET_DEST (PATTERN (insn)))) == MODE_FLOAT
7940                   || GET_MODE_CLASS 
7941                     (GET_MODE (SET_SRC (PATTERN (insn)))) == MODE_FLOAT)
7942                   {
7943                     enum attr_type attr_type_insn = get_attr_type (insn);
7944                     enum attr_type attr_type_dep = get_attr_type (dep);
7945
7946                     /* By default all dependencies of the form
7947                        s0 = s0 <op> s1
7948                        s0 = s0 <op> s2
7949                        have an extra latency of 1 cycle because
7950                        of the input and output dependency in this
7951                        case. However this gets modeled as an true
7952                        dependency and hence all these checks.  */
7953                     if (REG_P (SET_DEST (PATTERN (insn)))
7954                         && REG_P (SET_DEST (PATTERN (dep)))
7955                         && reg_overlap_mentioned_p (SET_DEST (PATTERN (insn)),
7956                                                     SET_DEST (PATTERN (dep))))
7957                       {
7958                         /* FMACS is a special case where the dependant
7959                            instruction can be issued 3 cycles before
7960                            the normal latency in case of an output 
7961                            dependency.  */
7962                         if ((attr_type_insn == TYPE_FMACS
7963                              || attr_type_insn == TYPE_FMACD)
7964                             && (attr_type_dep == TYPE_FMACS
7965                                 || attr_type_dep == TYPE_FMACD))
7966                           {
7967                             if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
7968                               *cost = insn_default_latency (dep) - 3;
7969                             else
7970                               *cost = insn_default_latency (dep);
7971                             return false;
7972                           }
7973                         else
7974                           {
7975                             if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
7976                               *cost = insn_default_latency (dep) + 1;
7977                             else
7978                               *cost = insn_default_latency (dep);
7979                           }
7980                         return false;
7981                       }
7982                   }
7983               }
7984           }
7985         break;
7986
7987     default:
7988       gcc_unreachable ();
7989     }
7990
7991   return true;
7992 }
7993
7994 /* This function implements the target macro TARGET_SCHED_ADJUST_COST.
7995    It corrects the value of COST based on the relationship between
7996    INSN and DEP through the dependence LINK.  It returns the new
7997    value. There is a per-core adjust_cost hook to adjust scheduler costs
7998    and the per-core hook can choose to completely override the generic 
7999    adjust_cost function. Only put bits of code into arm_adjust_cost that 
8000    are common across all cores.  */
8001 static int
8002 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
8003 {
8004   rtx i_pat, d_pat;
8005
8006  /* When generating Thumb-1 code, we want to place flag-setting operations
8007     close to a conditional branch which depends on them, so that we can
8008     omit the comparison. */
8009   if (TARGET_THUMB1
8010       && REG_NOTE_KIND (link) == 0
8011       && recog_memoized (insn) == CODE_FOR_cbranchsi4_insn
8012       && recog_memoized (dep) >= 0
8013       && get_attr_conds (dep) == CONDS_SET)
8014     return 0;
8015
8016   if (current_tune->sched_adjust_cost != NULL)
8017     {
8018       if (!current_tune->sched_adjust_cost (insn, link, dep, &cost))
8019         return cost;
8020     }
8021
8022   /* XXX This is not strictly true for the FPA.  */
8023   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
8024       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
8025     return 0;
8026
8027   /* Call insns don't incur a stall, even if they follow a load.  */
8028   if (REG_NOTE_KIND (link) == 0
8029       && GET_CODE (insn) == CALL_INSN)
8030     return 1;
8031
8032   if ((i_pat = single_set (insn)) != NULL
8033       && GET_CODE (SET_SRC (i_pat)) == MEM
8034       && (d_pat = single_set (dep)) != NULL
8035       && GET_CODE (SET_DEST (d_pat)) == MEM)
8036     {
8037       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
8038       /* This is a load after a store, there is no conflict if the load reads
8039          from a cached area.  Assume that loads from the stack, and from the
8040          constant pool are cached, and that others will miss.  This is a
8041          hack.  */
8042
8043       if ((GET_CODE (src_mem) == SYMBOL_REF 
8044            && CONSTANT_POOL_ADDRESS_P (src_mem))
8045           || reg_mentioned_p (stack_pointer_rtx, src_mem)
8046           || reg_mentioned_p (frame_pointer_rtx, src_mem)
8047           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
8048         return 1;
8049     }
8050
8051   return cost;
8052 }
8053
8054 static int fp_consts_inited = 0;
8055
8056 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
8057 static const char * const strings_fp[8] =
8058 {
8059   "0",   "1",   "2",   "3",
8060   "4",   "5",   "0.5", "10"
8061 };
8062
8063 static REAL_VALUE_TYPE values_fp[8];
8064
8065 static void
8066 init_fp_table (void)
8067 {
8068   int i;
8069   REAL_VALUE_TYPE r;
8070
8071   if (TARGET_VFP)
8072     fp_consts_inited = 1;
8073   else
8074     fp_consts_inited = 8;
8075
8076   for (i = 0; i < fp_consts_inited; i++)
8077     {
8078       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
8079       values_fp[i] = r;
8080     }
8081 }
8082
8083 /* Return TRUE if rtx X is a valid immediate FP constant.  */
8084 int
8085 arm_const_double_rtx (rtx x)
8086 {
8087   REAL_VALUE_TYPE r;
8088   int i;
8089
8090   if (!fp_consts_inited)
8091     init_fp_table ();
8092
8093   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8094   if (REAL_VALUE_MINUS_ZERO (r))
8095     return 0;
8096
8097   for (i = 0; i < fp_consts_inited; i++)
8098     if (REAL_VALUES_EQUAL (r, values_fp[i]))
8099       return 1;
8100
8101   return 0;
8102 }
8103
8104 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
8105 int
8106 neg_const_double_rtx_ok_for_fpa (rtx x)
8107 {
8108   REAL_VALUE_TYPE r;
8109   int i;
8110
8111   if (!fp_consts_inited)
8112     init_fp_table ();
8113
8114   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8115   r = real_value_negate (&r);
8116   if (REAL_VALUE_MINUS_ZERO (r))
8117     return 0;
8118
8119   for (i = 0; i < 8; i++)
8120     if (REAL_VALUES_EQUAL (r, values_fp[i]))
8121       return 1;
8122
8123   return 0;
8124 }
8125
8126
8127 /* VFPv3 has a fairly wide range of representable immediates, formed from
8128    "quarter-precision" floating-point values. These can be evaluated using this
8129    formula (with ^ for exponentiation):
8130
8131      -1^s * n * 2^-r
8132
8133    Where 's' is a sign bit (0/1), 'n' and 'r' are integers such that
8134    16 <= n <= 31 and 0 <= r <= 7.
8135
8136    These values are mapped onto an 8-bit integer ABCDEFGH s.t.
8137
8138      - A (most-significant) is the sign bit.
8139      - BCD are the exponent (encoded as r XOR 3).
8140      - EFGH are the mantissa (encoded as n - 16).
8141 */
8142
8143 /* Return an integer index for a VFPv3 immediate operand X suitable for the
8144    fconst[sd] instruction, or -1 if X isn't suitable.  */
8145 static int
8146 vfp3_const_double_index (rtx x)
8147 {
8148   REAL_VALUE_TYPE r, m;
8149   int sign, exponent;
8150   unsigned HOST_WIDE_INT mantissa, mant_hi;
8151   unsigned HOST_WIDE_INT mask;
8152   HOST_WIDE_INT m1, m2;
8153   int point_pos = 2 * HOST_BITS_PER_WIDE_INT - 1;
8154
8155   if (!TARGET_VFP3 || GET_CODE (x) != CONST_DOUBLE)
8156     return -1;
8157
8158   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8159
8160   /* We can't represent these things, so detect them first.  */
8161   if (REAL_VALUE_ISINF (r) || REAL_VALUE_ISNAN (r) || REAL_VALUE_MINUS_ZERO (r))
8162     return -1;
8163
8164   /* Extract sign, exponent and mantissa.  */
8165   sign = REAL_VALUE_NEGATIVE (r) ? 1 : 0;
8166   r = real_value_abs (&r);
8167   exponent = REAL_EXP (&r);
8168   /* For the mantissa, we expand into two HOST_WIDE_INTS, apart from the
8169      highest (sign) bit, with a fixed binary point at bit point_pos.
8170      WARNING: If there's ever a VFP version which uses more than 2 * H_W_I - 1
8171      bits for the mantissa, this may fail (low bits would be lost).  */
8172   real_ldexp (&m, &r, point_pos - exponent);
8173   REAL_VALUE_TO_INT (&m1, &m2, m);
8174   mantissa = m1;
8175   mant_hi = m2;
8176
8177   /* If there are bits set in the low part of the mantissa, we can't
8178      represent this value.  */
8179   if (mantissa != 0)
8180     return -1;
8181
8182   /* Now make it so that mantissa contains the most-significant bits, and move
8183      the point_pos to indicate that the least-significant bits have been
8184      discarded.  */
8185   point_pos -= HOST_BITS_PER_WIDE_INT;
8186   mantissa = mant_hi;
8187
8188   /* We can permit four significant bits of mantissa only, plus a high bit
8189      which is always 1.  */
8190   mask = ((unsigned HOST_WIDE_INT)1 << (point_pos - 5)) - 1;
8191   if ((mantissa & mask) != 0)
8192     return -1;
8193
8194   /* Now we know the mantissa is in range, chop off the unneeded bits.  */
8195   mantissa >>= point_pos - 5;
8196
8197   /* The mantissa may be zero. Disallow that case. (It's possible to load the
8198      floating-point immediate zero with Neon using an integer-zero load, but
8199      that case is handled elsewhere.)  */
8200   if (mantissa == 0)
8201     return -1;
8202
8203   gcc_assert (mantissa >= 16 && mantissa <= 31);
8204
8205   /* The value of 5 here would be 4 if GCC used IEEE754-like encoding (where
8206      normalized significands are in the range [1, 2). (Our mantissa is shifted
8207      left 4 places at this point relative to normalized IEEE754 values).  GCC
8208      internally uses [0.5, 1) (see real.c), so the exponent returned from
8209      REAL_EXP must be altered.  */
8210   exponent = 5 - exponent;
8211
8212   if (exponent < 0 || exponent > 7)
8213     return -1;
8214
8215   /* Sign, mantissa and exponent are now in the correct form to plug into the
8216      formula described in the comment above.  */
8217   return (sign << 7) | ((exponent ^ 3) << 4) | (mantissa - 16);
8218 }
8219
8220 /* Return TRUE if rtx X is a valid immediate VFPv3 constant.  */
8221 int
8222 vfp3_const_double_rtx (rtx x)
8223 {
8224   if (!TARGET_VFP3)
8225     return 0;
8226
8227   return vfp3_const_double_index (x) != -1;
8228 }
8229
8230 /* Recognize immediates which can be used in various Neon instructions. Legal
8231    immediates are described by the following table (for VMVN variants, the
8232    bitwise inverse of the constant shown is recognized. In either case, VMOV
8233    is output and the correct instruction to use for a given constant is chosen
8234    by the assembler). The constant shown is replicated across all elements of
8235    the destination vector.
8236
8237    insn elems variant constant (binary)
8238    ---- ----- ------- -----------------
8239    vmov  i32     0    00000000 00000000 00000000 abcdefgh
8240    vmov  i32     1    00000000 00000000 abcdefgh 00000000
8241    vmov  i32     2    00000000 abcdefgh 00000000 00000000
8242    vmov  i32     3    abcdefgh 00000000 00000000 00000000
8243    vmov  i16     4    00000000 abcdefgh
8244    vmov  i16     5    abcdefgh 00000000
8245    vmvn  i32     6    00000000 00000000 00000000 abcdefgh
8246    vmvn  i32     7    00000000 00000000 abcdefgh 00000000
8247    vmvn  i32     8    00000000 abcdefgh 00000000 00000000
8248    vmvn  i32     9    abcdefgh 00000000 00000000 00000000
8249    vmvn  i16    10    00000000 abcdefgh
8250    vmvn  i16    11    abcdefgh 00000000
8251    vmov  i32    12    00000000 00000000 abcdefgh 11111111
8252    vmvn  i32    13    00000000 00000000 abcdefgh 11111111
8253    vmov  i32    14    00000000 abcdefgh 11111111 11111111
8254    vmvn  i32    15    00000000 abcdefgh 11111111 11111111
8255    vmov   i8    16    abcdefgh
8256    vmov  i64    17    aaaaaaaa bbbbbbbb cccccccc dddddddd
8257                       eeeeeeee ffffffff gggggggg hhhhhhhh
8258    vmov  f32    18    aBbbbbbc defgh000 00000000 00000000
8259
8260    For case 18, B = !b. Representable values are exactly those accepted by
8261    vfp3_const_double_index, but are output as floating-point numbers rather
8262    than indices.
8263
8264    Variants 0-5 (inclusive) may also be used as immediates for the second
8265    operand of VORR/VBIC instructions.
8266
8267    The INVERSE argument causes the bitwise inverse of the given operand to be
8268    recognized instead (used for recognizing legal immediates for the VAND/VORN
8269    pseudo-instructions). If INVERSE is true, the value placed in *MODCONST is
8270    *not* inverted (i.e. the pseudo-instruction forms vand/vorn should still be
8271    output, rather than the real insns vbic/vorr).
8272
8273    INVERSE makes no difference to the recognition of float vectors.
8274
8275    The return value is the variant of immediate as shown in the above table, or
8276    -1 if the given value doesn't match any of the listed patterns.
8277 */
8278 static int
8279 neon_valid_immediate (rtx op, enum machine_mode mode, int inverse,
8280                       rtx *modconst, int *elementwidth)
8281 {
8282 #define CHECK(STRIDE, ELSIZE, CLASS, TEST)      \
8283   matches = 1;                                  \
8284   for (i = 0; i < idx; i += (STRIDE))           \
8285     if (!(TEST))                                \
8286       matches = 0;                              \
8287   if (matches)                                  \
8288     {                                           \
8289       immtype = (CLASS);                        \
8290       elsize = (ELSIZE);                        \
8291       break;                                    \
8292     }
8293
8294   unsigned int i, elsize = 0, idx = 0, n_elts = CONST_VECTOR_NUNITS (op);
8295   unsigned int innersize = GET_MODE_SIZE (GET_MODE_INNER (mode));
8296   unsigned char bytes[16];
8297   int immtype = -1, matches;
8298   unsigned int invmask = inverse ? 0xff : 0;
8299
8300   /* Vectors of float constants.  */
8301   if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
8302     {
8303       rtx el0 = CONST_VECTOR_ELT (op, 0);
8304       REAL_VALUE_TYPE r0;
8305
8306       if (!vfp3_const_double_rtx (el0))
8307         return -1;
8308
8309       REAL_VALUE_FROM_CONST_DOUBLE (r0, el0);
8310
8311       for (i = 1; i < n_elts; i++)
8312         {
8313           rtx elt = CONST_VECTOR_ELT (op, i);
8314           REAL_VALUE_TYPE re;
8315
8316           REAL_VALUE_FROM_CONST_DOUBLE (re, elt);
8317
8318           if (!REAL_VALUES_EQUAL (r0, re))
8319             return -1;
8320         }
8321
8322       if (modconst)
8323         *modconst = CONST_VECTOR_ELT (op, 0);
8324
8325       if (elementwidth)
8326         *elementwidth = 0;
8327
8328       return 18;
8329     }
8330
8331   /* Splat vector constant out into a byte vector.  */
8332   for (i = 0; i < n_elts; i++)
8333     {
8334       rtx el = CONST_VECTOR_ELT (op, i);
8335       unsigned HOST_WIDE_INT elpart;
8336       unsigned int part, parts;
8337
8338       if (GET_CODE (el) == CONST_INT)
8339         {
8340           elpart = INTVAL (el);
8341           parts = 1;
8342         }
8343       else if (GET_CODE (el) == CONST_DOUBLE)
8344         {
8345           elpart = CONST_DOUBLE_LOW (el);
8346           parts = 2;
8347         }
8348       else
8349         gcc_unreachable ();
8350
8351       for (part = 0; part < parts; part++)
8352         {
8353           unsigned int byte;
8354           for (byte = 0; byte < innersize; byte++)
8355             {
8356               bytes[idx++] = (elpart & 0xff) ^ invmask;
8357               elpart >>= BITS_PER_UNIT;
8358             }
8359           if (GET_CODE (el) == CONST_DOUBLE)
8360             elpart = CONST_DOUBLE_HIGH (el);
8361         }
8362     }
8363
8364   /* Sanity check.  */
8365   gcc_assert (idx == GET_MODE_SIZE (mode));
8366
8367   do
8368     {
8369       CHECK (4, 32, 0, bytes[i] == bytes[0] && bytes[i + 1] == 0
8370                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
8371
8372       CHECK (4, 32, 1, bytes[i] == 0 && bytes[i + 1] == bytes[1]
8373                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
8374
8375       CHECK (4, 32, 2, bytes[i] == 0 && bytes[i + 1] == 0
8376                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
8377
8378       CHECK (4, 32, 3, bytes[i] == 0 && bytes[i + 1] == 0
8379                        && bytes[i + 2] == 0 && bytes[i + 3] == bytes[3]);
8380
8381       CHECK (2, 16, 4, bytes[i] == bytes[0] && bytes[i + 1] == 0);
8382
8383       CHECK (2, 16, 5, bytes[i] == 0 && bytes[i + 1] == bytes[1]);
8384
8385       CHECK (4, 32, 6, bytes[i] == bytes[0] && bytes[i + 1] == 0xff
8386                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
8387
8388       CHECK (4, 32, 7, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
8389                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
8390
8391       CHECK (4, 32, 8, bytes[i] == 0xff && bytes[i + 1] == 0xff
8392                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
8393
8394       CHECK (4, 32, 9, bytes[i] == 0xff && bytes[i + 1] == 0xff
8395                        && bytes[i + 2] == 0xff && bytes[i + 3] == bytes[3]);
8396
8397       CHECK (2, 16, 10, bytes[i] == bytes[0] && bytes[i + 1] == 0xff);
8398
8399       CHECK (2, 16, 11, bytes[i] == 0xff && bytes[i + 1] == bytes[1]);
8400
8401       CHECK (4, 32, 12, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
8402                         && bytes[i + 2] == 0 && bytes[i + 3] == 0);
8403
8404       CHECK (4, 32, 13, bytes[i] == 0 && bytes[i + 1] == bytes[1]
8405                         && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
8406
8407       CHECK (4, 32, 14, bytes[i] == 0xff && bytes[i + 1] == 0xff
8408                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
8409
8410       CHECK (4, 32, 15, bytes[i] == 0 && bytes[i + 1] == 0
8411                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
8412
8413       CHECK (1, 8, 16, bytes[i] == bytes[0]);
8414
8415       CHECK (1, 64, 17, (bytes[i] == 0 || bytes[i] == 0xff)
8416                         && bytes[i] == bytes[(i + 8) % idx]);
8417     }
8418   while (0);
8419
8420   if (immtype == -1)
8421     return -1;
8422
8423   if (elementwidth)
8424     *elementwidth = elsize;
8425
8426   if (modconst)
8427     {
8428       unsigned HOST_WIDE_INT imm = 0;
8429
8430       /* Un-invert bytes of recognized vector, if necessary.  */
8431       if (invmask != 0)
8432         for (i = 0; i < idx; i++)
8433           bytes[i] ^= invmask;
8434
8435       if (immtype == 17)
8436         {
8437           /* FIXME: Broken on 32-bit H_W_I hosts.  */
8438           gcc_assert (sizeof (HOST_WIDE_INT) == 8);
8439
8440           for (i = 0; i < 8; i++)
8441             imm |= (unsigned HOST_WIDE_INT) (bytes[i] ? 0xff : 0)
8442                    << (i * BITS_PER_UNIT);
8443
8444           *modconst = GEN_INT (imm);
8445         }
8446       else
8447         {
8448           unsigned HOST_WIDE_INT imm = 0;
8449
8450           for (i = 0; i < elsize / BITS_PER_UNIT; i++)
8451             imm |= (unsigned HOST_WIDE_INT) bytes[i] << (i * BITS_PER_UNIT);
8452
8453           *modconst = GEN_INT (imm);
8454         }
8455     }
8456
8457   return immtype;
8458 #undef CHECK
8459 }
8460
8461 /* Return TRUE if rtx X is legal for use as either a Neon VMOV (or, implicitly,
8462    VMVN) immediate. Write back width per element to *ELEMENTWIDTH (or zero for
8463    float elements), and a modified constant (whatever should be output for a
8464    VMOV) in *MODCONST.  */
8465
8466 int
8467 neon_immediate_valid_for_move (rtx op, enum machine_mode mode,
8468                                rtx *modconst, int *elementwidth)
8469 {
8470   rtx tmpconst;
8471   int tmpwidth;
8472   int retval = neon_valid_immediate (op, mode, 0, &tmpconst, &tmpwidth);
8473
8474   if (retval == -1)
8475     return 0;
8476
8477   if (modconst)
8478     *modconst = tmpconst;
8479
8480   if (elementwidth)
8481     *elementwidth = tmpwidth;
8482
8483   return 1;
8484 }
8485
8486 /* Return TRUE if rtx X is legal for use in a VORR or VBIC instruction.  If
8487    the immediate is valid, write a constant suitable for using as an operand
8488    to VORR/VBIC/VAND/VORN to *MODCONST and the corresponding element width to
8489    *ELEMENTWIDTH. See neon_valid_immediate for description of INVERSE.  */
8490
8491 int
8492 neon_immediate_valid_for_logic (rtx op, enum machine_mode mode, int inverse,
8493                                 rtx *modconst, int *elementwidth)
8494 {
8495   rtx tmpconst;
8496   int tmpwidth;
8497   int retval = neon_valid_immediate (op, mode, inverse, &tmpconst, &tmpwidth);
8498
8499   if (retval < 0 || retval > 5)
8500     return 0;
8501
8502   if (modconst)
8503     *modconst = tmpconst;
8504
8505   if (elementwidth)
8506     *elementwidth = tmpwidth;
8507
8508   return 1;
8509 }
8510
8511 /* Return a string suitable for output of Neon immediate logic operation
8512    MNEM.  */
8513
8514 char *
8515 neon_output_logic_immediate (const char *mnem, rtx *op2, enum machine_mode mode,
8516                              int inverse, int quad)
8517 {
8518   int width, is_valid;
8519   static char templ[40];
8520
8521   is_valid = neon_immediate_valid_for_logic (*op2, mode, inverse, op2, &width);
8522
8523   gcc_assert (is_valid != 0);
8524
8525   if (quad)
8526     sprintf (templ, "%s.i%d\t%%q0, %%2", mnem, width);
8527   else
8528     sprintf (templ, "%s.i%d\t%%P0, %%2", mnem, width);
8529
8530   return templ;
8531 }
8532
8533 /* Output a sequence of pairwise operations to implement a reduction.
8534    NOTE: We do "too much work" here, because pairwise operations work on two
8535    registers-worth of operands in one go. Unfortunately we can't exploit those
8536    extra calculations to do the full operation in fewer steps, I don't think.
8537    Although all vector elements of the result but the first are ignored, we
8538    actually calculate the same result in each of the elements. An alternative
8539    such as initially loading a vector with zero to use as each of the second
8540    operands would use up an additional register and take an extra instruction,
8541    for no particular gain.  */
8542
8543 void
8544 neon_pairwise_reduce (rtx op0, rtx op1, enum machine_mode mode,
8545                       rtx (*reduc) (rtx, rtx, rtx))
8546 {
8547   enum machine_mode inner = GET_MODE_INNER (mode);
8548   unsigned int i, parts = GET_MODE_SIZE (mode) / GET_MODE_SIZE (inner);
8549   rtx tmpsum = op1;
8550
8551   for (i = parts / 2; i >= 1; i /= 2)
8552     {
8553       rtx dest = (i == 1) ? op0 : gen_reg_rtx (mode);
8554       emit_insn (reduc (dest, tmpsum, tmpsum));
8555       tmpsum = dest;
8556     }
8557 }
8558
8559 /* If VALS is a vector constant that can be loaded into a register
8560    using VDUP, generate instructions to do so and return an RTX to
8561    assign to the register.  Otherwise return NULL_RTX.  */
8562
8563 static rtx
8564 neon_vdup_constant (rtx vals)
8565 {
8566   enum machine_mode mode = GET_MODE (vals);
8567   enum machine_mode inner_mode = GET_MODE_INNER (mode);
8568   int n_elts = GET_MODE_NUNITS (mode);
8569   bool all_same = true;
8570   rtx x;
8571   int i;
8572
8573   if (GET_CODE (vals) != CONST_VECTOR || GET_MODE_SIZE (inner_mode) > 4)
8574     return NULL_RTX;
8575
8576   for (i = 0; i < n_elts; ++i)
8577     {
8578       x = XVECEXP (vals, 0, i);
8579       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
8580         all_same = false;
8581     }
8582
8583   if (!all_same)
8584     /* The elements are not all the same.  We could handle repeating
8585        patterns of a mode larger than INNER_MODE here (e.g. int8x8_t
8586        {0, C, 0, C, 0, C, 0, C} which can be loaded using
8587        vdup.i16).  */
8588     return NULL_RTX;
8589
8590   /* We can load this constant by using VDUP and a constant in a
8591      single ARM register.  This will be cheaper than a vector
8592      load.  */
8593
8594   x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, 0));
8595   return gen_rtx_VEC_DUPLICATE (mode, x);
8596 }
8597
8598 /* Generate code to load VALS, which is a PARALLEL containing only
8599    constants (for vec_init) or CONST_VECTOR, efficiently into a
8600    register.  Returns an RTX to copy into the register, or NULL_RTX
8601    for a PARALLEL that can not be converted into a CONST_VECTOR.  */
8602
8603 rtx
8604 neon_make_constant (rtx vals)
8605 {
8606   enum machine_mode mode = GET_MODE (vals);
8607   rtx target;
8608   rtx const_vec = NULL_RTX;
8609   int n_elts = GET_MODE_NUNITS (mode);
8610   int n_const = 0;
8611   int i;
8612
8613   if (GET_CODE (vals) == CONST_VECTOR)
8614     const_vec = vals;
8615   else if (GET_CODE (vals) == PARALLEL)
8616     {
8617       /* A CONST_VECTOR must contain only CONST_INTs and
8618          CONST_DOUBLEs, but CONSTANT_P allows more (e.g. SYMBOL_REF).
8619          Only store valid constants in a CONST_VECTOR.  */
8620       for (i = 0; i < n_elts; ++i)
8621         {
8622           rtx x = XVECEXP (vals, 0, i);
8623           if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
8624             n_const++;
8625         }
8626       if (n_const == n_elts)
8627         const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
8628     }
8629   else
8630     gcc_unreachable ();
8631
8632   if (const_vec != NULL
8633       && neon_immediate_valid_for_move (const_vec, mode, NULL, NULL))
8634     /* Load using VMOV.  On Cortex-A8 this takes one cycle.  */
8635     return const_vec;
8636   else if ((target = neon_vdup_constant (vals)) != NULL_RTX)
8637     /* Loaded using VDUP.  On Cortex-A8 the VDUP takes one NEON
8638        pipeline cycle; creating the constant takes one or two ARM
8639        pipeline cycles.  */
8640     return target;
8641   else if (const_vec != NULL_RTX)
8642     /* Load from constant pool.  On Cortex-A8 this takes two cycles
8643        (for either double or quad vectors).  We can not take advantage
8644        of single-cycle VLD1 because we need a PC-relative addressing
8645        mode.  */
8646     return const_vec;
8647   else
8648     /* A PARALLEL containing something not valid inside CONST_VECTOR.
8649        We can not construct an initializer.  */
8650     return NULL_RTX;
8651 }
8652
8653 /* Initialize vector TARGET to VALS.  */
8654
8655 void
8656 neon_expand_vector_init (rtx target, rtx vals)
8657 {
8658   enum machine_mode mode = GET_MODE (target);
8659   enum machine_mode inner_mode = GET_MODE_INNER (mode);
8660   int n_elts = GET_MODE_NUNITS (mode);
8661   int n_var = 0, one_var = -1;
8662   bool all_same = true;
8663   rtx x, mem;
8664   int i;
8665
8666   for (i = 0; i < n_elts; ++i)
8667     {
8668       x = XVECEXP (vals, 0, i);
8669       if (!CONSTANT_P (x))
8670         ++n_var, one_var = i;
8671
8672       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
8673         all_same = false;
8674     }
8675
8676   if (n_var == 0)
8677     {
8678       rtx constant = neon_make_constant (vals);
8679       if (constant != NULL_RTX)
8680         {
8681           emit_move_insn (target, constant);
8682           return;
8683         }
8684     }
8685
8686   /* Splat a single non-constant element if we can.  */
8687   if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
8688     {
8689       x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, 0));
8690       emit_insn (gen_rtx_SET (VOIDmode, target,
8691                               gen_rtx_VEC_DUPLICATE (mode, x)));
8692       return;
8693     }
8694
8695   /* One field is non-constant.  Load constant then overwrite varying
8696      field.  This is more efficient than using the stack.  */
8697   if (n_var == 1)
8698     {
8699       rtx copy = copy_rtx (vals);
8700       rtx index = GEN_INT (one_var);
8701
8702       /* Load constant part of vector, substitute neighboring value for
8703          varying element.  */
8704       XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
8705       neon_expand_vector_init (target, copy);
8706
8707       /* Insert variable.  */
8708       x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, one_var));
8709       switch (mode)
8710         {
8711         case V8QImode:
8712           emit_insn (gen_neon_vset_lanev8qi (target, x, target, index));
8713           break;
8714         case V16QImode:
8715           emit_insn (gen_neon_vset_lanev16qi (target, x, target, index));
8716           break;
8717         case V4HImode:
8718           emit_insn (gen_neon_vset_lanev4hi (target, x, target, index));
8719           break;
8720         case V8HImode:
8721           emit_insn (gen_neon_vset_lanev8hi (target, x, target, index));
8722           break;
8723         case V2SImode:
8724           emit_insn (gen_neon_vset_lanev2si (target, x, target, index));
8725           break;
8726         case V4SImode:
8727           emit_insn (gen_neon_vset_lanev4si (target, x, target, index));
8728           break;
8729         case V2SFmode:
8730           emit_insn (gen_neon_vset_lanev2sf (target, x, target, index));
8731           break;
8732         case V4SFmode:
8733           emit_insn (gen_neon_vset_lanev4sf (target, x, target, index));
8734           break;
8735         case V2DImode:
8736           emit_insn (gen_neon_vset_lanev2di (target, x, target, index));
8737           break;
8738         default:
8739           gcc_unreachable ();
8740         }
8741       return;
8742     }
8743
8744   /* Construct the vector in memory one field at a time
8745      and load the whole vector.  */
8746   mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), 0);
8747   for (i = 0; i < n_elts; i++)
8748     emit_move_insn (adjust_address_nv (mem, inner_mode,
8749                                     i * GET_MODE_SIZE (inner_mode)),
8750                     XVECEXP (vals, 0, i));
8751   emit_move_insn (target, mem);
8752 }
8753
8754 /* Ensure OPERAND lies between LOW (inclusive) and HIGH (exclusive).  Raise
8755    ERR if it doesn't.  FIXME: NEON bounds checks occur late in compilation, so
8756    reported source locations are bogus.  */
8757
8758 static void
8759 bounds_check (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high,
8760               const char *err)
8761 {
8762   HOST_WIDE_INT lane;
8763
8764   gcc_assert (GET_CODE (operand) == CONST_INT);
8765
8766   lane = INTVAL (operand);
8767
8768   if (lane < low || lane >= high)
8769     error (err);
8770 }
8771
8772 /* Bounds-check lanes.  */
8773
8774 void
8775 neon_lane_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
8776 {
8777   bounds_check (operand, low, high, "lane out of range");
8778 }
8779
8780 /* Bounds-check constants.  */
8781
8782 void
8783 neon_const_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
8784 {
8785   bounds_check (operand, low, high, "constant out of range");
8786 }
8787
8788 HOST_WIDE_INT
8789 neon_element_bits (enum machine_mode mode)
8790 {
8791   if (mode == DImode)
8792     return GET_MODE_BITSIZE (mode);
8793   else
8794     return GET_MODE_BITSIZE (GET_MODE_INNER (mode));
8795 }
8796
8797 \f
8798 /* Predicates for `match_operand' and `match_operator'.  */
8799
8800 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
8801 int
8802 cirrus_memory_offset (rtx op)
8803 {
8804   /* Reject eliminable registers.  */
8805   if (! (reload_in_progress || reload_completed)
8806       && (   reg_mentioned_p (frame_pointer_rtx, op)
8807           || reg_mentioned_p (arg_pointer_rtx, op)
8808           || reg_mentioned_p (virtual_incoming_args_rtx, op)
8809           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8810           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8811           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8812     return 0;
8813
8814   if (GET_CODE (op) == MEM)
8815     {
8816       rtx ind;
8817
8818       ind = XEXP (op, 0);
8819
8820       /* Match: (mem (reg)).  */
8821       if (GET_CODE (ind) == REG)
8822         return 1;
8823
8824       /* Match:
8825          (mem (plus (reg)
8826                     (const))).  */
8827       if (GET_CODE (ind) == PLUS
8828           && GET_CODE (XEXP (ind, 0)) == REG
8829           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
8830           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
8831         return 1;
8832     }
8833
8834   return 0;
8835 }
8836
8837 /* Return TRUE if OP is a valid coprocessor memory address pattern.
8838    WB is true if full writeback address modes are allowed and is false
8839    if limited writeback address modes (POST_INC and PRE_DEC) are
8840    allowed.  */
8841
8842 int
8843 arm_coproc_mem_operand (rtx op, bool wb)
8844 {
8845   rtx ind;
8846
8847   /* Reject eliminable registers.  */
8848   if (! (reload_in_progress || reload_completed)
8849       && (   reg_mentioned_p (frame_pointer_rtx, op)
8850           || reg_mentioned_p (arg_pointer_rtx, op)
8851           || reg_mentioned_p (virtual_incoming_args_rtx, op)
8852           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8853           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8854           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8855     return FALSE;
8856
8857   /* Constants are converted into offsets from labels.  */
8858   if (GET_CODE (op) != MEM)
8859     return FALSE;
8860
8861   ind = XEXP (op, 0);
8862
8863   if (reload_completed
8864       && (GET_CODE (ind) == LABEL_REF
8865           || (GET_CODE (ind) == CONST
8866               && GET_CODE (XEXP (ind, 0)) == PLUS
8867               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
8868               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
8869     return TRUE;
8870
8871   /* Match: (mem (reg)).  */
8872   if (GET_CODE (ind) == REG)
8873     return arm_address_register_rtx_p (ind, 0);
8874
8875   /* Autoincremment addressing modes.  POST_INC and PRE_DEC are
8876      acceptable in any case (subject to verification by
8877      arm_address_register_rtx_p).  We need WB to be true to accept
8878      PRE_INC and POST_DEC.  */
8879   if (GET_CODE (ind) == POST_INC
8880       || GET_CODE (ind) == PRE_DEC
8881       || (wb
8882           && (GET_CODE (ind) == PRE_INC
8883               || GET_CODE (ind) == POST_DEC)))
8884     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
8885
8886   if (wb
8887       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
8888       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
8889       && GET_CODE (XEXP (ind, 1)) == PLUS
8890       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
8891     ind = XEXP (ind, 1);
8892
8893   /* Match:
8894      (plus (reg)
8895            (const)).  */
8896   if (GET_CODE (ind) == PLUS
8897       && GET_CODE (XEXP (ind, 0)) == REG
8898       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
8899       && GET_CODE (XEXP (ind, 1)) == CONST_INT
8900       && INTVAL (XEXP (ind, 1)) > -1024
8901       && INTVAL (XEXP (ind, 1)) <  1024
8902       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
8903     return TRUE;
8904
8905   return FALSE;
8906 }
8907
8908 /* Return TRUE if OP is a memory operand which we can load or store a vector
8909    to/from. TYPE is one of the following values:
8910     0 - Vector load/stor (vldr)
8911     1 - Core registers (ldm)
8912     2 - Element/structure loads (vld1)
8913  */
8914 int
8915 neon_vector_mem_operand (rtx op, int type)
8916 {
8917   rtx ind;
8918
8919   /* Reject eliminable registers.  */
8920   if (! (reload_in_progress || reload_completed)
8921       && (   reg_mentioned_p (frame_pointer_rtx, op)
8922           || reg_mentioned_p (arg_pointer_rtx, op)
8923           || reg_mentioned_p (virtual_incoming_args_rtx, op)
8924           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8925           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8926           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8927     return FALSE;
8928
8929   /* Constants are converted into offsets from labels.  */
8930   if (GET_CODE (op) != MEM)
8931     return FALSE;
8932
8933   ind = XEXP (op, 0);
8934
8935   if (reload_completed
8936       && (GET_CODE (ind) == LABEL_REF
8937           || (GET_CODE (ind) == CONST
8938               && GET_CODE (XEXP (ind, 0)) == PLUS
8939               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
8940               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
8941     return TRUE;
8942
8943   /* Match: (mem (reg)).  */
8944   if (GET_CODE (ind) == REG)
8945     return arm_address_register_rtx_p (ind, 0);
8946
8947   /* Allow post-increment with Neon registers.  */
8948   if ((type != 1 && GET_CODE (ind) == POST_INC)
8949       || (type == 0 && GET_CODE (ind) == PRE_DEC))
8950     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
8951
8952   /* FIXME: vld1 allows register post-modify.  */
8953
8954   /* Match:
8955      (plus (reg)
8956           (const)).  */
8957   if (type == 0
8958       && GET_CODE (ind) == PLUS
8959       && GET_CODE (XEXP (ind, 0)) == REG
8960       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
8961       && GET_CODE (XEXP (ind, 1)) == CONST_INT
8962       && INTVAL (XEXP (ind, 1)) > -1024
8963       && INTVAL (XEXP (ind, 1)) < 1016
8964       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
8965     return TRUE;
8966
8967   return FALSE;
8968 }
8969
8970 /* Return TRUE if OP is a mem suitable for loading/storing a Neon struct
8971    type.  */
8972 int
8973 neon_struct_mem_operand (rtx op)
8974 {
8975   rtx ind;
8976
8977   /* Reject eliminable registers.  */
8978   if (! (reload_in_progress || reload_completed)
8979       && (   reg_mentioned_p (frame_pointer_rtx, op)
8980           || reg_mentioned_p (arg_pointer_rtx, op)
8981           || reg_mentioned_p (virtual_incoming_args_rtx, op)
8982           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8983           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8984           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8985     return FALSE;
8986
8987   /* Constants are converted into offsets from labels.  */
8988   if (GET_CODE (op) != MEM)
8989     return FALSE;
8990
8991   ind = XEXP (op, 0);
8992
8993   if (reload_completed
8994       && (GET_CODE (ind) == LABEL_REF
8995           || (GET_CODE (ind) == CONST
8996               && GET_CODE (XEXP (ind, 0)) == PLUS
8997               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
8998               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
8999     return TRUE;
9000
9001   /* Match: (mem (reg)).  */
9002   if (GET_CODE (ind) == REG)
9003     return arm_address_register_rtx_p (ind, 0);
9004
9005   return FALSE;
9006 }
9007
9008 /* Return true if X is a register that will be eliminated later on.  */
9009 int
9010 arm_eliminable_register (rtx x)
9011 {
9012   return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
9013                        || REGNO (x) == ARG_POINTER_REGNUM
9014                        || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
9015                            && REGNO (x) <= LAST_VIRTUAL_REGISTER));
9016 }
9017
9018 /* Return GENERAL_REGS if a scratch register required to reload x to/from
9019    coprocessor registers.  Otherwise return NO_REGS.  */
9020
9021 enum reg_class
9022 coproc_secondary_reload_class (enum machine_mode mode, rtx x, bool wb)
9023 {
9024   if (mode == HFmode)
9025     {
9026       if (!TARGET_NEON_FP16)
9027         return GENERAL_REGS;
9028       if (s_register_operand (x, mode) || neon_vector_mem_operand (x, 2))
9029         return NO_REGS;
9030       return GENERAL_REGS;
9031     }
9032
9033   if (TARGET_NEON
9034       && (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9035           || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
9036       && neon_vector_mem_operand (x, 0))
9037      return NO_REGS;
9038
9039   if (arm_coproc_mem_operand (x, wb) || s_register_operand (x, mode))
9040     return NO_REGS;
9041
9042   return GENERAL_REGS;
9043 }
9044
9045 /* Values which must be returned in the most-significant end of the return
9046    register.  */
9047
9048 static bool
9049 arm_return_in_msb (const_tree valtype)
9050 {
9051   return (TARGET_AAPCS_BASED
9052           && BYTES_BIG_ENDIAN
9053           && (AGGREGATE_TYPE_P (valtype)
9054               || TREE_CODE (valtype) == COMPLEX_TYPE));
9055 }
9056
9057 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
9058    Use by the Cirrus Maverick code which has to workaround
9059    a hardware bug triggered by such instructions.  */
9060 static bool
9061 arm_memory_load_p (rtx insn)
9062 {
9063   rtx body, lhs, rhs;;
9064
9065   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
9066     return false;
9067
9068   body = PATTERN (insn);
9069
9070   if (GET_CODE (body) != SET)
9071     return false;
9072
9073   lhs = XEXP (body, 0);
9074   rhs = XEXP (body, 1);
9075
9076   lhs = REG_OR_SUBREG_RTX (lhs);
9077
9078   /* If the destination is not a general purpose
9079      register we do not have to worry.  */
9080   if (GET_CODE (lhs) != REG
9081       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
9082     return false;
9083
9084   /* As well as loads from memory we also have to react
9085      to loads of invalid constants which will be turned
9086      into loads from the minipool.  */
9087   return (GET_CODE (rhs) == MEM
9088           || GET_CODE (rhs) == SYMBOL_REF
9089           || note_invalid_constants (insn, -1, false));
9090 }
9091
9092 /* Return TRUE if INSN is a Cirrus instruction.  */
9093 static bool
9094 arm_cirrus_insn_p (rtx insn)
9095 {
9096   enum attr_cirrus attr;
9097
9098   /* get_attr cannot accept USE or CLOBBER.  */
9099   if (!insn
9100       || GET_CODE (insn) != INSN
9101       || GET_CODE (PATTERN (insn)) == USE
9102       || GET_CODE (PATTERN (insn)) == CLOBBER)
9103     return 0;
9104
9105   attr = get_attr_cirrus (insn);
9106
9107   return attr != CIRRUS_NOT;
9108 }
9109
9110 /* Cirrus reorg for invalid instruction combinations.  */
9111 static void
9112 cirrus_reorg (rtx first)
9113 {
9114   enum attr_cirrus attr;
9115   rtx body = PATTERN (first);
9116   rtx t;
9117   int nops;
9118
9119   /* Any branch must be followed by 2 non Cirrus instructions.  */
9120   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
9121     {
9122       nops = 0;
9123       t = next_nonnote_insn (first);
9124
9125       if (arm_cirrus_insn_p (t))
9126         ++ nops;
9127
9128       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
9129         ++ nops;
9130
9131       while (nops --)
9132         emit_insn_after (gen_nop (), first);
9133
9134       return;
9135     }
9136
9137   /* (float (blah)) is in parallel with a clobber.  */
9138   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
9139     body = XVECEXP (body, 0, 0);
9140
9141   if (GET_CODE (body) == SET)
9142     {
9143       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
9144
9145       /* cfldrd, cfldr64, cfstrd, cfstr64 must
9146          be followed by a non Cirrus insn.  */
9147       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
9148         {
9149           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
9150             emit_insn_after (gen_nop (), first);
9151
9152           return;
9153         }
9154       else if (arm_memory_load_p (first))
9155         {
9156           unsigned int arm_regno;
9157
9158           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
9159              ldr/cfmv64hr combination where the Rd field is the same
9160              in both instructions must be split with a non Cirrus
9161              insn.  Example:
9162
9163              ldr r0, blah
9164              nop
9165              cfmvsr mvf0, r0.  */
9166
9167           /* Get Arm register number for ldr insn.  */
9168           if (GET_CODE (lhs) == REG)
9169             arm_regno = REGNO (lhs);
9170           else
9171             {
9172               gcc_assert (GET_CODE (rhs) == REG);
9173               arm_regno = REGNO (rhs);
9174             }
9175
9176           /* Next insn.  */
9177           first = next_nonnote_insn (first);
9178
9179           if (! arm_cirrus_insn_p (first))
9180             return;
9181
9182           body = PATTERN (first);
9183
9184           /* (float (blah)) is in parallel with a clobber.  */
9185           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
9186             body = XVECEXP (body, 0, 0);
9187
9188           if (GET_CODE (body) == FLOAT)
9189             body = XEXP (body, 0);
9190
9191           if (get_attr_cirrus (first) == CIRRUS_MOVE
9192               && GET_CODE (XEXP (body, 1)) == REG
9193               && arm_regno == REGNO (XEXP (body, 1)))
9194             emit_insn_after (gen_nop (), first);
9195
9196           return;
9197         }
9198     }
9199
9200   /* get_attr cannot accept USE or CLOBBER.  */
9201   if (!first
9202       || GET_CODE (first) != INSN
9203       || GET_CODE (PATTERN (first)) == USE
9204       || GET_CODE (PATTERN (first)) == CLOBBER)
9205     return;
9206
9207   attr = get_attr_cirrus (first);
9208
9209   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
9210      must be followed by a non-coprocessor instruction.  */
9211   if (attr == CIRRUS_COMPARE)
9212     {
9213       nops = 0;
9214
9215       t = next_nonnote_insn (first);
9216
9217       if (arm_cirrus_insn_p (t))
9218         ++ nops;
9219
9220       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
9221         ++ nops;
9222
9223       while (nops --)
9224         emit_insn_after (gen_nop (), first);
9225
9226       return;
9227     }
9228 }
9229
9230 /* Return TRUE if X references a SYMBOL_REF.  */
9231 int
9232 symbol_mentioned_p (rtx x)
9233 {
9234   const char * fmt;
9235   int i;
9236
9237   if (GET_CODE (x) == SYMBOL_REF)
9238     return 1;
9239
9240   /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
9241      are constant offsets, not symbols.  */
9242   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
9243     return 0;
9244
9245   fmt = GET_RTX_FORMAT (GET_CODE (x));
9246
9247   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
9248     {
9249       if (fmt[i] == 'E')
9250         {
9251           int j;
9252
9253           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9254             if (symbol_mentioned_p (XVECEXP (x, i, j)))
9255               return 1;
9256         }
9257       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
9258         return 1;
9259     }
9260
9261   return 0;
9262 }
9263
9264 /* Return TRUE if X references a LABEL_REF.  */
9265 int
9266 label_mentioned_p (rtx x)
9267 {
9268   const char * fmt;
9269   int i;
9270
9271   if (GET_CODE (x) == LABEL_REF)
9272     return 1;
9273
9274   /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
9275      instruction, but they are constant offsets, not symbols.  */
9276   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
9277     return 0;
9278
9279   fmt = GET_RTX_FORMAT (GET_CODE (x));
9280   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
9281     {
9282       if (fmt[i] == 'E')
9283         {
9284           int j;
9285
9286           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9287             if (label_mentioned_p (XVECEXP (x, i, j)))
9288               return 1;
9289         }
9290       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
9291         return 1;
9292     }
9293
9294   return 0;
9295 }
9296
9297 int
9298 tls_mentioned_p (rtx x)
9299 {
9300   switch (GET_CODE (x))
9301     {
9302     case CONST:
9303       return tls_mentioned_p (XEXP (x, 0));
9304
9305     case UNSPEC:
9306       if (XINT (x, 1) == UNSPEC_TLS)
9307         return 1;
9308
9309     default:
9310       return 0;
9311     }
9312 }
9313
9314 /* Must not copy any rtx that uses a pc-relative address.  */
9315
9316 static int
9317 arm_note_pic_base (rtx *x, void *date ATTRIBUTE_UNUSED)
9318 {
9319   if (GET_CODE (*x) == UNSPEC
9320       && XINT (*x, 1) == UNSPEC_PIC_BASE)
9321     return 1;
9322   return 0;
9323 }
9324
9325 static bool
9326 arm_cannot_copy_insn_p (rtx insn)
9327 {
9328   return for_each_rtx (&PATTERN (insn), arm_note_pic_base, NULL);
9329 }
9330
9331 enum rtx_code
9332 minmax_code (rtx x)
9333 {
9334   enum rtx_code code = GET_CODE (x);
9335
9336   switch (code)
9337     {
9338     case SMAX:
9339       return GE;
9340     case SMIN:
9341       return LE;
9342     case UMIN:
9343       return LEU;
9344     case UMAX:
9345       return GEU;
9346     default:
9347       gcc_unreachable ();
9348     }
9349 }
9350
9351 /* Return 1 if memory locations are adjacent.  */
9352 int
9353 adjacent_mem_locations (rtx a, rtx b)
9354 {
9355   /* We don't guarantee to preserve the order of these memory refs.  */
9356   if (volatile_refs_p (a) || volatile_refs_p (b))
9357     return 0;
9358
9359   if ((GET_CODE (XEXP (a, 0)) == REG
9360        || (GET_CODE (XEXP (a, 0)) == PLUS
9361            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
9362       && (GET_CODE (XEXP (b, 0)) == REG
9363           || (GET_CODE (XEXP (b, 0)) == PLUS
9364               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
9365     {
9366       HOST_WIDE_INT val0 = 0, val1 = 0;
9367       rtx reg0, reg1;
9368       int val_diff;
9369
9370       if (GET_CODE (XEXP (a, 0)) == PLUS)
9371         {
9372           reg0 = XEXP (XEXP (a, 0), 0);
9373           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
9374         }
9375       else
9376         reg0 = XEXP (a, 0);
9377
9378       if (GET_CODE (XEXP (b, 0)) == PLUS)
9379         {
9380           reg1 = XEXP (XEXP (b, 0), 0);
9381           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
9382         }
9383       else
9384         reg1 = XEXP (b, 0);
9385
9386       /* Don't accept any offset that will require multiple
9387          instructions to handle, since this would cause the
9388          arith_adjacentmem pattern to output an overlong sequence.  */
9389       if (!const_ok_for_op (val0, PLUS) || !const_ok_for_op (val1, PLUS))
9390         return 0;
9391
9392       /* Don't allow an eliminable register: register elimination can make
9393          the offset too large.  */
9394       if (arm_eliminable_register (reg0))
9395         return 0;
9396
9397       val_diff = val1 - val0;
9398
9399       if (arm_ld_sched)
9400         {
9401           /* If the target has load delay slots, then there's no benefit
9402              to using an ldm instruction unless the offset is zero and
9403              we are optimizing for size.  */
9404           return (optimize_size && (REGNO (reg0) == REGNO (reg1))
9405                   && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
9406                   && (val_diff == 4 || val_diff == -4));
9407         }
9408
9409       return ((REGNO (reg0) == REGNO (reg1))
9410               && (val_diff == 4 || val_diff == -4));
9411     }
9412
9413   return 0;
9414 }
9415
9416 /* Return true iff it would be profitable to turn a sequence of NOPS loads
9417    or stores (depending on IS_STORE) into a load-multiple or store-multiple
9418    instruction.  ADD_OFFSET is nonzero if the base address register needs
9419    to be modified with an add instruction before we can use it.  */
9420
9421 static bool
9422 multiple_operation_profitable_p (bool is_store ATTRIBUTE_UNUSED,
9423                                  int nops, HOST_WIDE_INT add_offset)
9424  {
9425   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
9426      if the offset isn't small enough.  The reason 2 ldrs are faster
9427      is because these ARMs are able to do more than one cache access
9428      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
9429      whilst the ARM8 has a double bandwidth cache.  This means that
9430      these cores can do both an instruction fetch and a data fetch in
9431      a single cycle, so the trick of calculating the address into a
9432      scratch register (one of the result regs) and then doing a load
9433      multiple actually becomes slower (and no smaller in code size).
9434      That is the transformation
9435
9436         ldr     rd1, [rbase + offset]
9437         ldr     rd2, [rbase + offset + 4]
9438
9439      to
9440
9441         add     rd1, rbase, offset
9442         ldmia   rd1, {rd1, rd2}
9443
9444      produces worse code -- '3 cycles + any stalls on rd2' instead of
9445      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
9446      access per cycle, the first sequence could never complete in less
9447      than 6 cycles, whereas the ldm sequence would only take 5 and
9448      would make better use of sequential accesses if not hitting the
9449      cache.
9450
9451      We cheat here and test 'arm_ld_sched' which we currently know to
9452      only be true for the ARM8, ARM9 and StrongARM.  If this ever
9453      changes, then the test below needs to be reworked.  */
9454   if (nops == 2 && arm_ld_sched && add_offset != 0)
9455     return false;
9456
9457   /* XScale has load-store double instructions, but they have stricter
9458      alignment requirements than load-store multiple, so we cannot
9459      use them.
9460
9461      For XScale ldm requires 2 + NREGS cycles to complete and blocks
9462      the pipeline until completion.
9463
9464         NREGS           CYCLES
9465           1               3
9466           2               4
9467           3               5
9468           4               6
9469
9470      An ldr instruction takes 1-3 cycles, but does not block the
9471      pipeline.
9472
9473         NREGS           CYCLES
9474           1              1-3
9475           2              2-6
9476           3              3-9
9477           4              4-12
9478
9479      Best case ldr will always win.  However, the more ldr instructions
9480      we issue, the less likely we are to be able to schedule them well.
9481      Using ldr instructions also increases code size.
9482
9483      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
9484      for counts of 3 or 4 regs.  */
9485   if (nops <= 2 && arm_tune_xscale && !optimize_size)
9486     return false;
9487   return true;
9488 }
9489
9490 /* Subroutine of load_multiple_sequence and store_multiple_sequence.
9491    Given an array of UNSORTED_OFFSETS, of which there are NOPS, compute
9492    an array ORDER which describes the sequence to use when accessing the
9493    offsets that produces an ascending order.  In this sequence, each
9494    offset must be larger by exactly 4 than the previous one.  ORDER[0]
9495    must have been filled in with the lowest offset by the caller.
9496    If UNSORTED_REGS is nonnull, it is an array of register numbers that
9497    we use to verify that ORDER produces an ascending order of registers.
9498    Return true if it was possible to construct such an order, false if
9499    not.  */
9500
9501 static bool
9502 compute_offset_order (int nops, HOST_WIDE_INT *unsorted_offsets, int *order,
9503                       int *unsorted_regs)
9504 {
9505   int i;
9506   for (i = 1; i < nops; i++)
9507     {
9508       int j;
9509
9510       order[i] = order[i - 1];
9511       for (j = 0; j < nops; j++)
9512         if (unsorted_offsets[j] == unsorted_offsets[order[i - 1]] + 4)
9513           {
9514             /* We must find exactly one offset that is higher than the
9515                previous one by 4.  */
9516             if (order[i] != order[i - 1])
9517               return false;
9518             order[i] = j;
9519           }
9520       if (order[i] == order[i - 1])
9521         return false;
9522       /* The register numbers must be ascending.  */
9523       if (unsorted_regs != NULL
9524           && unsorted_regs[order[i]] <= unsorted_regs[order[i - 1]])
9525         return false;
9526     }
9527   return true;
9528 }
9529
9530 /* Used to determine in a peephole whether a sequence of load
9531    instructions can be changed into a load-multiple instruction.
9532    NOPS is the number of separate load instructions we are examining.  The
9533    first NOPS entries in OPERANDS are the destination registers, the
9534    next NOPS entries are memory operands.  If this function is
9535    successful, *BASE is set to the common base register of the memory
9536    accesses; *LOAD_OFFSET is set to the first memory location's offset
9537    from that base register.
9538    REGS is an array filled in with the destination register numbers.
9539    SAVED_ORDER (if nonnull), is an array filled in with an order that maps
9540    insn numbers to to an ascending order of stores.  If CHECK_REGS is true,
9541    the sequence of registers in REGS matches the loads from ascending memory
9542    locations, and the function verifies that the register numbers are
9543    themselves ascending.  If CHECK_REGS is false, the register numbers
9544    are stored in the order they are found in the operands.  */
9545 static int
9546 load_multiple_sequence (rtx *operands, int nops, int *regs, int *saved_order,
9547                         int *base, HOST_WIDE_INT *load_offset, bool check_regs)
9548 {
9549   int unsorted_regs[MAX_LDM_STM_OPS];
9550   HOST_WIDE_INT unsorted_offsets[MAX_LDM_STM_OPS];
9551   int order[MAX_LDM_STM_OPS];
9552   rtx base_reg_rtx = NULL;
9553   int base_reg = -1;
9554   int i, ldm_case;
9555
9556   /* Can only handle up to MAX_LDM_STM_OPS insns at present, though could be
9557      easily extended if required.  */
9558   gcc_assert (nops >= 2 && nops <= MAX_LDM_STM_OPS);
9559
9560   memset (order, 0, MAX_LDM_STM_OPS * sizeof (int));
9561
9562   /* Loop over the operands and check that the memory references are
9563      suitable (i.e. immediate offsets from the same base register).  At
9564      the same time, extract the target register, and the memory
9565      offsets.  */
9566   for (i = 0; i < nops; i++)
9567     {
9568       rtx reg;
9569       rtx offset;
9570
9571       /* Convert a subreg of a mem into the mem itself.  */
9572       if (GET_CODE (operands[nops + i]) == SUBREG)
9573         operands[nops + i] = alter_subreg (operands + (nops + i));
9574
9575       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
9576
9577       /* Don't reorder volatile memory references; it doesn't seem worth
9578          looking for the case where the order is ok anyway.  */
9579       if (MEM_VOLATILE_P (operands[nops + i]))
9580         return 0;
9581
9582       offset = const0_rtx;
9583
9584       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
9585            || (GET_CODE (reg) == SUBREG
9586                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9587           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
9588               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
9589                    == REG)
9590                   || (GET_CODE (reg) == SUBREG
9591                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9592               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
9593                   == CONST_INT)))
9594         {
9595           if (i == 0)
9596             {
9597               base_reg = REGNO (reg);
9598               base_reg_rtx = reg;
9599               if (TARGET_THUMB1 && base_reg > LAST_LO_REGNUM)
9600                 return 0;
9601             }
9602           else if (base_reg != (int) REGNO (reg))
9603             /* Not addressed from the same base register.  */
9604             return 0;
9605
9606           unsorted_regs[i] = (GET_CODE (operands[i]) == REG
9607                               ? REGNO (operands[i])
9608                               : REGNO (SUBREG_REG (operands[i])));
9609
9610           /* If it isn't an integer register, or if it overwrites the
9611              base register but isn't the last insn in the list, then
9612              we can't do this.  */
9613           if (unsorted_regs[i] < 0
9614               || (TARGET_THUMB1 && unsorted_regs[i] > LAST_LO_REGNUM)
9615               || unsorted_regs[i] > 14
9616               || (i != nops - 1 && unsorted_regs[i] == base_reg))
9617             return 0;
9618
9619           unsorted_offsets[i] = INTVAL (offset);
9620           if (i == 0 || unsorted_offsets[i] < unsorted_offsets[order[0]])
9621             order[0] = i;
9622         }
9623       else
9624         /* Not a suitable memory address.  */
9625         return 0;
9626     }
9627
9628   /* All the useful information has now been extracted from the
9629      operands into unsorted_regs and unsorted_offsets; additionally,
9630      order[0] has been set to the lowest offset in the list.  Sort
9631      the offsets into order, verifying that they are adjacent, and
9632      check that the register numbers are ascending.  */
9633   if (!compute_offset_order (nops, unsorted_offsets, order,
9634                              check_regs ? unsorted_regs : NULL))
9635     return 0;
9636
9637   if (saved_order)
9638     memcpy (saved_order, order, sizeof order);
9639
9640   if (base)
9641     {
9642       *base = base_reg;
9643
9644       for (i = 0; i < nops; i++)
9645         regs[i] = unsorted_regs[check_regs ? order[i] : i];
9646
9647       *load_offset = unsorted_offsets[order[0]];
9648     }
9649
9650   if (TARGET_THUMB1
9651       && !peep2_reg_dead_p (nops, base_reg_rtx))
9652     return 0;
9653
9654   if (unsorted_offsets[order[0]] == 0)
9655     ldm_case = 1; /* ldmia */
9656   else if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
9657     ldm_case = 2; /* ldmib */
9658   else if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
9659     ldm_case = 3; /* ldmda */
9660   else if (TARGET_32BIT && unsorted_offsets[order[nops - 1]] == -4)
9661     ldm_case = 4; /* ldmdb */
9662   else if (const_ok_for_arm (unsorted_offsets[order[0]])
9663            || const_ok_for_arm (-unsorted_offsets[order[0]]))
9664     ldm_case = 5;
9665   else
9666     return 0;
9667
9668   if (!multiple_operation_profitable_p (false, nops,
9669                                         ldm_case == 5
9670                                         ? unsorted_offsets[order[0]] : 0))
9671     return 0;
9672
9673   return ldm_case;
9674 }
9675
9676 /* Used to determine in a peephole whether a sequence of store instructions can
9677    be changed into a store-multiple instruction.
9678    NOPS is the number of separate store instructions we are examining.
9679    NOPS_TOTAL is the total number of instructions recognized by the peephole
9680    pattern.
9681    The first NOPS entries in OPERANDS are the source registers, the next
9682    NOPS entries are memory operands.  If this function is successful, *BASE is
9683    set to the common base register of the memory accesses; *LOAD_OFFSET is set
9684    to the first memory location's offset from that base register.  REGS is an
9685    array filled in with the source register numbers, REG_RTXS (if nonnull) is
9686    likewise filled with the corresponding rtx's.
9687    SAVED_ORDER (if nonnull), is an array filled in with an order that maps insn
9688    numbers to to an ascending order of stores.
9689    If CHECK_REGS is true, the sequence of registers in *REGS matches the stores
9690    from ascending memory locations, and the function verifies that the register
9691    numbers are themselves ascending.  If CHECK_REGS is false, the register
9692    numbers are stored in the order they are found in the operands.  */
9693 static int
9694 store_multiple_sequence (rtx *operands, int nops, int nops_total,
9695                          int *regs, rtx *reg_rtxs, int *saved_order, int *base,
9696                          HOST_WIDE_INT *load_offset, bool check_regs)
9697 {
9698   int unsorted_regs[MAX_LDM_STM_OPS];
9699   rtx unsorted_reg_rtxs[MAX_LDM_STM_OPS];
9700   HOST_WIDE_INT unsorted_offsets[MAX_LDM_STM_OPS];
9701   int order[MAX_LDM_STM_OPS];
9702   int base_reg = -1;
9703   rtx base_reg_rtx = NULL;
9704   int i, stm_case;
9705
9706   /* Can only handle up to MAX_LDM_STM_OPS insns at present, though could be
9707      easily extended if required.  */
9708   gcc_assert (nops >= 2 && nops <= MAX_LDM_STM_OPS);
9709
9710   memset (order, 0, MAX_LDM_STM_OPS * sizeof (int));
9711
9712   /* Loop over the operands and check that the memory references are
9713      suitable (i.e. immediate offsets from the same base register).  At
9714      the same time, extract the target register, and the memory
9715      offsets.  */
9716   for (i = 0; i < nops; i++)
9717     {
9718       rtx reg;
9719       rtx offset;
9720
9721       /* Convert a subreg of a mem into the mem itself.  */
9722       if (GET_CODE (operands[nops + i]) == SUBREG)
9723         operands[nops + i] = alter_subreg (operands + (nops + i));
9724
9725       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
9726
9727       /* Don't reorder volatile memory references; it doesn't seem worth
9728          looking for the case where the order is ok anyway.  */
9729       if (MEM_VOLATILE_P (operands[nops + i]))
9730         return 0;
9731
9732       offset = const0_rtx;
9733
9734       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
9735            || (GET_CODE (reg) == SUBREG
9736                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9737           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
9738               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
9739                    == REG)
9740                   || (GET_CODE (reg) == SUBREG
9741                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9742               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
9743                   == CONST_INT)))
9744         {
9745           unsorted_reg_rtxs[i] = (GET_CODE (operands[i]) == REG
9746                                   ? operands[i] : SUBREG_REG (operands[i]));
9747           unsorted_regs[i] = REGNO (unsorted_reg_rtxs[i]);
9748
9749           if (i == 0)
9750             {
9751               base_reg = REGNO (reg);
9752               base_reg_rtx = reg;
9753               if (TARGET_THUMB1 && base_reg > LAST_LO_REGNUM)
9754                 return 0;
9755             }
9756           else if (base_reg != (int) REGNO (reg))
9757             /* Not addressed from the same base register.  */
9758             return 0;
9759
9760           /* If it isn't an integer register, then we can't do this.  */
9761           if (unsorted_regs[i] < 0
9762               || (TARGET_THUMB1 && unsorted_regs[i] > LAST_LO_REGNUM)
9763               || (TARGET_THUMB2 && unsorted_regs[i] == base_reg)
9764               || (TARGET_THUMB2 && unsorted_regs[i] == SP_REGNUM)
9765               || unsorted_regs[i] > 14)
9766             return 0;
9767
9768           unsorted_offsets[i] = INTVAL (offset);
9769           if (i == 0 || unsorted_offsets[i] < unsorted_offsets[order[0]])
9770             order[0] = i;
9771         }
9772       else
9773         /* Not a suitable memory address.  */
9774         return 0;
9775     }
9776
9777   /* All the useful information has now been extracted from the
9778      operands into unsorted_regs and unsorted_offsets; additionally,
9779      order[0] has been set to the lowest offset in the list.  Sort
9780      the offsets into order, verifying that they are adjacent, and
9781      check that the register numbers are ascending.  */
9782   if (!compute_offset_order (nops, unsorted_offsets, order,
9783                              check_regs ? unsorted_regs : NULL))
9784     return 0;
9785
9786   if (saved_order)
9787     memcpy (saved_order, order, sizeof order);
9788
9789   if (base)
9790     {
9791       *base = base_reg;
9792
9793       for (i = 0; i < nops; i++)
9794         {
9795           regs[i] = unsorted_regs[check_regs ? order[i] : i];
9796           if (reg_rtxs)
9797             reg_rtxs[i] = unsorted_reg_rtxs[check_regs ? order[i] : i];
9798         }
9799
9800       *load_offset = unsorted_offsets[order[0]];
9801     }
9802
9803   if (TARGET_THUMB1
9804       && !peep2_reg_dead_p (nops_total, base_reg_rtx))
9805     return 0;
9806
9807   if (unsorted_offsets[order[0]] == 0)
9808     stm_case = 1; /* stmia */
9809   else if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
9810     stm_case = 2; /* stmib */
9811   else if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
9812     stm_case = 3; /* stmda */
9813   else if (TARGET_32BIT && unsorted_offsets[order[nops - 1]] == -4)
9814     stm_case = 4; /* stmdb */
9815   else
9816     return 0;
9817
9818   if (!multiple_operation_profitable_p (false, nops, 0))
9819     return 0;
9820
9821   return stm_case;
9822 }
9823 \f
9824 /* Routines for use in generating RTL.  */
9825
9826 /* Generate a load-multiple instruction.  COUNT is the number of loads in
9827    the instruction; REGS and MEMS are arrays containing the operands.
9828    BASEREG is the base register to be used in addressing the memory operands.
9829    WBACK_OFFSET is nonzero if the instruction should update the base
9830    register.  */
9831
9832 static rtx
9833 arm_gen_load_multiple_1 (int count, int *regs, rtx *mems, rtx basereg,
9834                          HOST_WIDE_INT wback_offset)
9835 {
9836   int i = 0, j;
9837   rtx result;
9838
9839   if (!multiple_operation_profitable_p (false, count, 0))
9840     {
9841       rtx seq;
9842
9843       start_sequence ();
9844
9845       for (i = 0; i < count; i++)
9846         emit_move_insn (gen_rtx_REG (SImode, regs[i]), mems[i]);
9847
9848       if (wback_offset != 0)
9849         emit_move_insn (basereg, plus_constant (basereg, wback_offset));
9850
9851       seq = get_insns ();
9852       end_sequence ();
9853
9854       return seq;
9855     }
9856
9857   result = gen_rtx_PARALLEL (VOIDmode,
9858                              rtvec_alloc (count + (wback_offset != 0 ? 1 : 0)));
9859   if (wback_offset != 0)
9860     {
9861       XVECEXP (result, 0, 0)
9862         = gen_rtx_SET (VOIDmode, basereg,
9863                        plus_constant (basereg, wback_offset));
9864       i = 1;
9865       count++;
9866     }
9867
9868   for (j = 0; i < count; i++, j++)
9869     XVECEXP (result, 0, i)
9870       = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, regs[j]), mems[j]);
9871
9872   return result;
9873 }
9874
9875 /* Generate a store-multiple instruction.  COUNT is the number of stores in
9876    the instruction; REGS and MEMS are arrays containing the operands.
9877    BASEREG is the base register to be used in addressing the memory operands.
9878    WBACK_OFFSET is nonzero if the instruction should update the base
9879    register.  */
9880
9881 static rtx
9882 arm_gen_store_multiple_1 (int count, int *regs, rtx *mems, rtx basereg,
9883                           HOST_WIDE_INT wback_offset)
9884 {
9885   int i = 0, j;
9886   rtx result;
9887
9888   if (GET_CODE (basereg) == PLUS)
9889     basereg = XEXP (basereg, 0);
9890
9891   if (!multiple_operation_profitable_p (false, count, 0))
9892     {
9893       rtx seq;
9894
9895       start_sequence ();
9896
9897       for (i = 0; i < count; i++)
9898         emit_move_insn (mems[i], gen_rtx_REG (SImode, regs[i]));
9899
9900       if (wback_offset != 0)
9901         emit_move_insn (basereg, plus_constant (basereg, wback_offset));
9902
9903       seq = get_insns ();
9904       end_sequence ();
9905
9906       return seq;
9907     }
9908
9909   result = gen_rtx_PARALLEL (VOIDmode,
9910                              rtvec_alloc (count + (wback_offset != 0 ? 1 : 0)));
9911   if (wback_offset != 0)
9912     {
9913       XVECEXP (result, 0, 0)
9914         = gen_rtx_SET (VOIDmode, basereg,
9915                        plus_constant (basereg, wback_offset));
9916       i = 1;
9917       count++;
9918     }
9919
9920   for (j = 0; i < count; i++, j++)
9921     XVECEXP (result, 0, i)
9922       = gen_rtx_SET (VOIDmode, mems[j], gen_rtx_REG (SImode, regs[j]));
9923
9924   return result;
9925 }
9926
9927 /* Generate either a load-multiple or a store-multiple instruction.  This
9928    function can be used in situations where we can start with a single MEM
9929    rtx and adjust its address upwards.
9930    COUNT is the number of operations in the instruction, not counting a
9931    possible update of the base register.  REGS is an array containing the
9932    register operands.
9933    BASEREG is the base register to be used in addressing the memory operands,
9934    which are constructed from BASEMEM.
9935    WRITE_BACK specifies whether the generated instruction should include an
9936    update of the base register.
9937    OFFSETP is used to pass an offset to and from this function; this offset
9938    is not used when constructing the address (instead BASEMEM should have an
9939    appropriate offset in its address), it is used only for setting
9940    MEM_OFFSET.  It is updated only if WRITE_BACK is true.*/
9941
9942 static rtx
9943 arm_gen_multiple_op (bool is_load, int *regs, int count, rtx basereg,
9944                      bool write_back, rtx basemem, HOST_WIDE_INT *offsetp)
9945 {
9946   rtx mems[MAX_LDM_STM_OPS];
9947   HOST_WIDE_INT offset = *offsetp;
9948   int i;
9949
9950   gcc_assert (count <= MAX_LDM_STM_OPS);
9951
9952   if (GET_CODE (basereg) == PLUS)
9953     basereg = XEXP (basereg, 0);
9954
9955   for (i = 0; i < count; i++)
9956     {
9957       rtx addr = plus_constant (basereg, i * 4);
9958       mems[i] = adjust_automodify_address_nv (basemem, SImode, addr, offset);
9959       offset += 4;
9960     }
9961
9962   if (write_back)
9963     *offsetp = offset;
9964
9965   if (is_load)
9966     return arm_gen_load_multiple_1 (count, regs, mems, basereg,
9967                                     write_back ? 4 * count : 0);
9968   else
9969     return arm_gen_store_multiple_1 (count, regs, mems, basereg,
9970                                      write_back ? 4 * count : 0);
9971 }
9972
9973 rtx
9974 arm_gen_load_multiple (int *regs, int count, rtx basereg, int write_back,
9975                        rtx basemem, HOST_WIDE_INT *offsetp)
9976 {
9977   return arm_gen_multiple_op (TRUE, regs, count, basereg, write_back, basemem,
9978                               offsetp);
9979 }
9980
9981 rtx
9982 arm_gen_store_multiple (int *regs, int count, rtx basereg, int write_back,
9983                         rtx basemem, HOST_WIDE_INT *offsetp)
9984 {
9985   return arm_gen_multiple_op (FALSE, regs, count, basereg, write_back, basemem,
9986                               offsetp);
9987 }
9988
9989 /* Called from a peephole2 expander to turn a sequence of loads into an
9990    LDM instruction.  OPERANDS are the operands found by the peephole matcher;
9991    NOPS indicates how many separate loads we are trying to combine.  SORT_REGS
9992    is true if we can reorder the registers because they are used commutatively
9993    subsequently.
9994    Returns true iff we could generate a new instruction.  */
9995
9996 bool
9997 gen_ldm_seq (rtx *operands, int nops, bool sort_regs)
9998 {
9999   int regs[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
10000   rtx mems[MAX_LDM_STM_OPS];
10001   int i, j, base_reg;
10002   rtx base_reg_rtx;
10003   HOST_WIDE_INT offset;
10004   int write_back = FALSE;
10005   int ldm_case;
10006   rtx addr;
10007
10008   ldm_case = load_multiple_sequence (operands, nops, regs, mem_order,
10009                                      &base_reg, &offset, !sort_regs);
10010
10011   if (ldm_case == 0)
10012     return false;
10013
10014   if (sort_regs)
10015     for (i = 0; i < nops - 1; i++)
10016       for (j = i + 1; j < nops; j++)
10017         if (regs[i] > regs[j])
10018           {
10019             int t = regs[i];
10020             regs[i] = regs[j];
10021             regs[j] = t;
10022           }
10023   base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
10024
10025   if (TARGET_THUMB1)
10026     {
10027       gcc_assert (peep2_reg_dead_p (nops, base_reg_rtx));
10028       gcc_assert (ldm_case == 1 || ldm_case == 5);
10029       write_back = TRUE;
10030     }
10031
10032   if (ldm_case == 5)
10033     {
10034       rtx newbase = TARGET_THUMB1 ? base_reg_rtx : gen_rtx_REG (SImode, regs[0]);
10035       emit_insn (gen_addsi3 (newbase, base_reg_rtx, GEN_INT (offset)));
10036       offset = 0;
10037       if (!TARGET_THUMB1)
10038         {
10039           base_reg = regs[0];
10040           base_reg_rtx = newbase;
10041         }
10042     }
10043
10044   for (i = 0; i < nops; i++)
10045     {
10046       addr = plus_constant (base_reg_rtx, offset + i * 4);
10047       mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
10048                                               SImode, addr, 0);
10049     }
10050   emit_insn (arm_gen_load_multiple_1 (nops, regs, mems, base_reg_rtx,
10051                                       write_back ? offset + i * 4 : 0));
10052   return true;
10053 }
10054
10055 /* Called from a peephole2 expander to turn a sequence of stores into an
10056    STM instruction.  OPERANDS are the operands found by the peephole matcher;
10057    NOPS indicates how many separate stores we are trying to combine.
10058    Returns true iff we could generate a new instruction.  */
10059
10060 bool
10061 gen_stm_seq (rtx *operands, int nops)
10062 {
10063   int i;
10064   int regs[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
10065   rtx mems[MAX_LDM_STM_OPS];
10066   int base_reg;
10067   rtx base_reg_rtx;
10068   HOST_WIDE_INT offset;
10069   int write_back = FALSE;
10070   int stm_case;
10071   rtx addr;
10072   bool base_reg_dies;
10073
10074   stm_case = store_multiple_sequence (operands, nops, nops, regs, NULL,
10075                                       mem_order, &base_reg, &offset, true);
10076
10077   if (stm_case == 0)
10078     return false;
10079
10080   base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
10081
10082   base_reg_dies = peep2_reg_dead_p (nops, base_reg_rtx);
10083   if (TARGET_THUMB1)
10084     {
10085       gcc_assert (base_reg_dies);
10086       write_back = TRUE;
10087     }
10088
10089   if (stm_case == 5)
10090     {
10091       gcc_assert (base_reg_dies);
10092       emit_insn (gen_addsi3 (base_reg_rtx, base_reg_rtx, GEN_INT (offset)));
10093       offset = 0;
10094     }
10095
10096   addr = plus_constant (base_reg_rtx, offset);
10097
10098   for (i = 0; i < nops; i++)
10099     {
10100       addr = plus_constant (base_reg_rtx, offset + i * 4);
10101       mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
10102                                               SImode, addr, 0);
10103     }
10104   emit_insn (arm_gen_store_multiple_1 (nops, regs, mems, base_reg_rtx,
10105                                        write_back ? offset + i * 4 : 0));
10106   return true;
10107 }
10108
10109 /* Called from a peephole2 expander to turn a sequence of stores that are
10110    preceded by constant loads into an STM instruction.  OPERANDS are the
10111    operands found by the peephole matcher; NOPS indicates how many
10112    separate stores we are trying to combine; there are 2 * NOPS
10113    instructions in the peephole.
10114    Returns true iff we could generate a new instruction.  */
10115
10116 bool
10117 gen_const_stm_seq (rtx *operands, int nops)
10118 {
10119   int regs[MAX_LDM_STM_OPS], sorted_regs[MAX_LDM_STM_OPS];
10120   int reg_order[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
10121   rtx reg_rtxs[MAX_LDM_STM_OPS], orig_reg_rtxs[MAX_LDM_STM_OPS];
10122   rtx mems[MAX_LDM_STM_OPS];
10123   int base_reg;
10124   rtx base_reg_rtx;
10125   HOST_WIDE_INT offset;
10126   int write_back = FALSE;
10127   int stm_case;
10128   rtx addr;
10129   bool base_reg_dies;
10130   int i, j;
10131   HARD_REG_SET allocated;
10132
10133   stm_case = store_multiple_sequence (operands, nops, 2 * nops, regs, reg_rtxs,
10134                                       mem_order, &base_reg, &offset, false);
10135
10136   if (stm_case == 0)
10137     return false;
10138
10139   memcpy (orig_reg_rtxs, reg_rtxs, sizeof orig_reg_rtxs);
10140
10141   /* If the same register is used more than once, try to find a free
10142      register.  */
10143   CLEAR_HARD_REG_SET (allocated);
10144   for (i = 0; i < nops; i++)
10145     {
10146       for (j = i + 1; j < nops; j++)
10147         if (regs[i] == regs[j])
10148           {
10149             rtx t = peep2_find_free_register (0, nops * 2,
10150                                               TARGET_THUMB1 ? "l" : "r",
10151                                               SImode, &allocated);
10152             if (t == NULL_RTX)
10153               return false;
10154             reg_rtxs[i] = t;
10155             regs[i] = REGNO (t);
10156           }
10157     }
10158
10159   /* Compute an ordering that maps the register numbers to an ascending
10160      sequence.  */
10161   reg_order[0] = 0;
10162   for (i = 0; i < nops; i++)
10163     if (regs[i] < regs[reg_order[0]])
10164       reg_order[0] = i;
10165
10166   for (i = 1; i < nops; i++)
10167     {
10168       int this_order = reg_order[i - 1];
10169       for (j = 0; j < nops; j++)
10170         if (regs[j] > regs[reg_order[i - 1]]
10171             && (this_order == reg_order[i - 1]
10172                 || regs[j] < regs[this_order]))
10173           this_order = j;
10174       reg_order[i] = this_order;
10175     }
10176
10177   /* Ensure that registers that must be live after the instruction end
10178      up with the correct value.  */
10179   for (i = 0; i < nops; i++)
10180     {
10181       int this_order = reg_order[i];
10182       if ((this_order != mem_order[i]
10183            || orig_reg_rtxs[this_order] != reg_rtxs[this_order])
10184           && !peep2_reg_dead_p (nops * 2, orig_reg_rtxs[this_order]))
10185         return false;
10186     }
10187
10188   /* Load the constants.  */
10189   for (i = 0; i < nops; i++)
10190     {
10191       rtx op = operands[2 * nops + mem_order[i]];
10192       sorted_regs[i] = regs[reg_order[i]];
10193       emit_move_insn (reg_rtxs[reg_order[i]], op);
10194     }
10195
10196   base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
10197
10198   base_reg_dies = peep2_reg_dead_p (nops * 2, base_reg_rtx);
10199   if (TARGET_THUMB1)
10200     {
10201       gcc_assert (base_reg_dies);
10202       write_back = TRUE;
10203     }
10204
10205   if (stm_case == 5)
10206     {
10207       gcc_assert (base_reg_dies);
10208       emit_insn (gen_addsi3 (base_reg_rtx, base_reg_rtx, GEN_INT (offset)));
10209       offset = 0;
10210     }
10211
10212   addr = plus_constant (base_reg_rtx, offset);
10213
10214   for (i = 0; i < nops; i++)
10215     {
10216       addr = plus_constant (base_reg_rtx, offset + i * 4);
10217       mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
10218                                               SImode, addr, 0);
10219     }
10220   emit_insn (arm_gen_store_multiple_1 (nops, sorted_regs, mems, base_reg_rtx,
10221                                        write_back ? offset + i * 4 : 0));
10222   return true;
10223 }
10224
10225 int
10226 arm_gen_movmemqi (rtx *operands)
10227 {
10228   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
10229   HOST_WIDE_INT srcoffset, dstoffset;
10230   int i;
10231   rtx src, dst, srcbase, dstbase;
10232   rtx part_bytes_reg = NULL;
10233   rtx mem;
10234
10235   if (GET_CODE (operands[2]) != CONST_INT
10236       || GET_CODE (operands[3]) != CONST_INT
10237       || INTVAL (operands[2]) > 64
10238       || INTVAL (operands[3]) & 3)
10239     return 0;
10240
10241   dstbase = operands[0];
10242   srcbase = operands[1];
10243
10244   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
10245   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
10246
10247   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
10248   out_words_to_go = INTVAL (operands[2]) / 4;
10249   last_bytes = INTVAL (operands[2]) & 3;
10250   dstoffset = srcoffset = 0;
10251
10252   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
10253     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
10254
10255   for (i = 0; in_words_to_go >= 2; i+=4)
10256     {
10257       if (in_words_to_go > 4)
10258         emit_insn (arm_gen_load_multiple (arm_regs_in_sequence, 4, src,
10259                                           TRUE, srcbase, &srcoffset));
10260       else
10261         emit_insn (arm_gen_load_multiple (arm_regs_in_sequence, in_words_to_go,
10262                                           src, FALSE, srcbase,
10263                                           &srcoffset));
10264
10265       if (out_words_to_go)
10266         {
10267           if (out_words_to_go > 4)
10268             emit_insn (arm_gen_store_multiple (arm_regs_in_sequence, 4, dst,
10269                                                TRUE, dstbase, &dstoffset));
10270           else if (out_words_to_go != 1)
10271             emit_insn (arm_gen_store_multiple (arm_regs_in_sequence,
10272                                                out_words_to_go, dst,
10273                                                (last_bytes == 0
10274                                                 ? FALSE : TRUE),
10275                                                dstbase, &dstoffset));
10276           else
10277             {
10278               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
10279               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
10280               if (last_bytes != 0)
10281                 {
10282                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
10283                   dstoffset += 4;
10284                 }
10285             }
10286         }
10287
10288       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
10289       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
10290     }
10291
10292   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
10293   if (out_words_to_go)
10294     {
10295       rtx sreg;
10296
10297       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
10298       sreg = copy_to_reg (mem);
10299
10300       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
10301       emit_move_insn (mem, sreg);
10302       in_words_to_go--;
10303
10304       gcc_assert (!in_words_to_go);     /* Sanity check */
10305     }
10306
10307   if (in_words_to_go)
10308     {
10309       gcc_assert (in_words_to_go > 0);
10310
10311       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
10312       part_bytes_reg = copy_to_mode_reg (SImode, mem);
10313     }
10314
10315   gcc_assert (!last_bytes || part_bytes_reg);
10316
10317   if (BYTES_BIG_ENDIAN && last_bytes)
10318     {
10319       rtx tmp = gen_reg_rtx (SImode);
10320
10321       /* The bytes we want are in the top end of the word.  */
10322       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
10323                               GEN_INT (8 * (4 - last_bytes))));
10324       part_bytes_reg = tmp;
10325
10326       while (last_bytes)
10327         {
10328           mem = adjust_automodify_address (dstbase, QImode,
10329                                            plus_constant (dst, last_bytes - 1),
10330                                            dstoffset + last_bytes - 1);
10331           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
10332
10333           if (--last_bytes)
10334             {
10335               tmp = gen_reg_rtx (SImode);
10336               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
10337               part_bytes_reg = tmp;
10338             }
10339         }
10340
10341     }
10342   else
10343     {
10344       if (last_bytes > 1)
10345         {
10346           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
10347           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
10348           last_bytes -= 2;
10349           if (last_bytes)
10350             {
10351               rtx tmp = gen_reg_rtx (SImode);
10352               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
10353               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
10354               part_bytes_reg = tmp;
10355               dstoffset += 2;
10356             }
10357         }
10358
10359       if (last_bytes)
10360         {
10361           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
10362           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
10363         }
10364     }
10365
10366   return 1;
10367 }
10368
10369 /* Select a dominance comparison mode if possible for a test of the general
10370    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
10371    COND_OR == DOM_CC_X_AND_Y => (X && Y)
10372    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
10373    COND_OR == DOM_CC_X_OR_Y => (X || Y)
10374    In all cases OP will be either EQ or NE, but we don't need to know which
10375    here.  If we are unable to support a dominance comparison we return
10376    CC mode.  This will then fail to match for the RTL expressions that
10377    generate this call.  */
10378 enum machine_mode
10379 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
10380 {
10381   enum rtx_code cond1, cond2;
10382   int swapped = 0;
10383
10384   /* Currently we will probably get the wrong result if the individual
10385      comparisons are not simple.  This also ensures that it is safe to
10386      reverse a comparison if necessary.  */
10387   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
10388        != CCmode)
10389       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
10390           != CCmode))
10391     return CCmode;
10392
10393   /* The if_then_else variant of this tests the second condition if the
10394      first passes, but is true if the first fails.  Reverse the first
10395      condition to get a true "inclusive-or" expression.  */
10396   if (cond_or == DOM_CC_NX_OR_Y)
10397     cond1 = reverse_condition (cond1);
10398
10399   /* If the comparisons are not equal, and one doesn't dominate the other,
10400      then we can't do this.  */
10401   if (cond1 != cond2
10402       && !comparison_dominates_p (cond1, cond2)
10403       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
10404     return CCmode;
10405
10406   if (swapped)
10407     {
10408       enum rtx_code temp = cond1;
10409       cond1 = cond2;
10410       cond2 = temp;
10411     }
10412
10413   switch (cond1)
10414     {
10415     case EQ:
10416       if (cond_or == DOM_CC_X_AND_Y)
10417         return CC_DEQmode;
10418
10419       switch (cond2)
10420         {
10421         case EQ: return CC_DEQmode;
10422         case LE: return CC_DLEmode;
10423         case LEU: return CC_DLEUmode;
10424         case GE: return CC_DGEmode;
10425         case GEU: return CC_DGEUmode;
10426         default: gcc_unreachable ();
10427         }
10428
10429     case LT:
10430       if (cond_or == DOM_CC_X_AND_Y)
10431         return CC_DLTmode;
10432
10433       switch (cond2)
10434         {
10435         case  LT:
10436             return CC_DLTmode;
10437         case LE:
10438           return CC_DLEmode;
10439         case NE:
10440           return CC_DNEmode;
10441         default:
10442           gcc_unreachable ();
10443         }
10444
10445     case GT:
10446       if (cond_or == DOM_CC_X_AND_Y)
10447         return CC_DGTmode;
10448
10449       switch (cond2)
10450         {
10451         case GT:
10452           return CC_DGTmode;
10453         case GE:
10454           return CC_DGEmode;
10455         case NE:
10456           return CC_DNEmode;
10457         default:
10458           gcc_unreachable ();
10459         }
10460
10461     case LTU:
10462       if (cond_or == DOM_CC_X_AND_Y)
10463         return CC_DLTUmode;
10464
10465       switch (cond2)
10466         {
10467         case LTU:
10468           return CC_DLTUmode;
10469         case LEU:
10470           return CC_DLEUmode;
10471         case NE:
10472           return CC_DNEmode;
10473         default:
10474           gcc_unreachable ();
10475         }
10476
10477     case GTU:
10478       if (cond_or == DOM_CC_X_AND_Y)
10479         return CC_DGTUmode;
10480
10481       switch (cond2)
10482         {
10483         case GTU:
10484           return CC_DGTUmode;
10485         case GEU:
10486           return CC_DGEUmode;
10487         case NE:
10488           return CC_DNEmode;
10489         default:
10490           gcc_unreachable ();
10491         }
10492
10493     /* The remaining cases only occur when both comparisons are the
10494        same.  */
10495     case NE:
10496       gcc_assert (cond1 == cond2);
10497       return CC_DNEmode;
10498
10499     case LE:
10500       gcc_assert (cond1 == cond2);
10501       return CC_DLEmode;
10502
10503     case GE:
10504       gcc_assert (cond1 == cond2);
10505       return CC_DGEmode;
10506
10507     case LEU:
10508       gcc_assert (cond1 == cond2);
10509       return CC_DLEUmode;
10510
10511     case GEU:
10512       gcc_assert (cond1 == cond2);
10513       return CC_DGEUmode;
10514
10515     default:
10516       gcc_unreachable ();
10517     }
10518 }
10519
10520 enum machine_mode
10521 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
10522 {
10523   /* All floating point compares return CCFP if it is an equality
10524      comparison, and CCFPE otherwise.  */
10525   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
10526     {
10527       switch (op)
10528         {
10529         case EQ:
10530         case NE:
10531         case UNORDERED:
10532         case ORDERED:
10533         case UNLT:
10534         case UNLE:
10535         case UNGT:
10536         case UNGE:
10537         case UNEQ:
10538         case LTGT:
10539           return CCFPmode;
10540
10541         case LT:
10542         case LE:
10543         case GT:
10544         case GE:
10545           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
10546             return CCFPmode;
10547           return CCFPEmode;
10548
10549         default:
10550           gcc_unreachable ();
10551         }
10552     }
10553
10554   /* A compare with a shifted operand.  Because of canonicalization, the
10555      comparison will have to be swapped when we emit the assembler.  */
10556   if (GET_MODE (y) == SImode 
10557       && (REG_P (y) || (GET_CODE (y) == SUBREG))
10558       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
10559           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
10560           || GET_CODE (x) == ROTATERT))
10561     return CC_SWPmode;
10562
10563   /* This operation is performed swapped, but since we only rely on the Z
10564      flag we don't need an additional mode.  */
10565   if (GET_MODE (y) == SImode 
10566       && (REG_P (y) || (GET_CODE (y) == SUBREG))
10567       && GET_CODE (x) == NEG
10568       && (op == EQ || op == NE))
10569     return CC_Zmode;
10570
10571   /* This is a special case that is used by combine to allow a
10572      comparison of a shifted byte load to be split into a zero-extend
10573      followed by a comparison of the shifted integer (only valid for
10574      equalities and unsigned inequalities).  */
10575   if (GET_MODE (x) == SImode
10576       && GET_CODE (x) == ASHIFT
10577       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
10578       && GET_CODE (XEXP (x, 0)) == SUBREG
10579       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
10580       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
10581       && (op == EQ || op == NE
10582           || op == GEU || op == GTU || op == LTU || op == LEU)
10583       && GET_CODE (y) == CONST_INT)
10584     return CC_Zmode;
10585
10586   /* A construct for a conditional compare, if the false arm contains
10587      0, then both conditions must be true, otherwise either condition
10588      must be true.  Not all conditions are possible, so CCmode is
10589      returned if it can't be done.  */
10590   if (GET_CODE (x) == IF_THEN_ELSE
10591       && (XEXP (x, 2) == const0_rtx
10592           || XEXP (x, 2) == const1_rtx)
10593       && COMPARISON_P (XEXP (x, 0))
10594       && COMPARISON_P (XEXP (x, 1)))
10595     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
10596                                          INTVAL (XEXP (x, 2)));
10597
10598   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
10599   if (GET_CODE (x) == AND
10600       && COMPARISON_P (XEXP (x, 0))
10601       && COMPARISON_P (XEXP (x, 1)))
10602     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
10603                                          DOM_CC_X_AND_Y);
10604
10605   if (GET_CODE (x) == IOR
10606       && COMPARISON_P (XEXP (x, 0))
10607       && COMPARISON_P (XEXP (x, 1)))
10608     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
10609                                          DOM_CC_X_OR_Y);
10610
10611   /* An operation (on Thumb) where we want to test for a single bit.
10612      This is done by shifting that bit up into the top bit of a
10613      scratch register; we can then branch on the sign bit.  */
10614   if (TARGET_THUMB1
10615       && GET_MODE (x) == SImode
10616       && (op == EQ || op == NE)
10617       && GET_CODE (x) == ZERO_EXTRACT
10618       && XEXP (x, 1) == const1_rtx)
10619     return CC_Nmode;
10620
10621   /* An operation that sets the condition codes as a side-effect, the
10622      V flag is not set correctly, so we can only use comparisons where
10623      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
10624      instead.)  */
10625   /* ??? Does the ZERO_EXTRACT case really apply to thumb2?  */
10626   if (GET_MODE (x) == SImode
10627       && y == const0_rtx
10628       && (op == EQ || op == NE || op == LT || op == GE)
10629       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
10630           || GET_CODE (x) == AND || GET_CODE (x) == IOR
10631           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
10632           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
10633           || GET_CODE (x) == LSHIFTRT
10634           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
10635           || GET_CODE (x) == ROTATERT
10636           || (TARGET_32BIT && GET_CODE (x) == ZERO_EXTRACT)))
10637     return CC_NOOVmode;
10638
10639   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
10640     return CC_Zmode;
10641
10642   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
10643       && GET_CODE (x) == PLUS
10644       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
10645     return CC_Cmode;
10646
10647   if (GET_MODE (x) == DImode || GET_MODE (y) == DImode)
10648     {
10649       /* To keep things simple, always use the Cirrus cfcmp64 if it is
10650          available.  */
10651       if (TARGET_ARM && TARGET_HARD_FLOAT && TARGET_MAVERICK)
10652         return CCmode;
10653
10654       switch (op)
10655         {
10656         case EQ:
10657         case NE:
10658           /* A DImode comparison against zero can be implemented by
10659              or'ing the two halves together.  */
10660           if (y == const0_rtx)
10661             return CC_Zmode;
10662
10663           /* We can do an equality test in three Thumb instructions.  */
10664           if (!TARGET_ARM)
10665             return CC_Zmode;
10666
10667           /* FALLTHROUGH */
10668
10669         case LTU:
10670         case LEU:
10671         case GTU:
10672         case GEU:
10673           /* DImode unsigned comparisons can be implemented by cmp +
10674              cmpeq without a scratch register.  Not worth doing in
10675              Thumb-2.  */
10676           if (TARGET_ARM)
10677             return CC_CZmode;
10678
10679           /* FALLTHROUGH */
10680
10681         case LT:
10682         case LE:
10683         case GT:
10684         case GE:
10685           /* DImode signed and unsigned comparisons can be implemented
10686              by cmp + sbcs with a scratch register, but that does not
10687              set the Z flag - we must reverse GT/LE/GTU/LEU.  */
10688           gcc_assert (op != EQ && op != NE);
10689           return CC_NCVmode;
10690
10691         default:
10692           gcc_unreachable ();
10693         }
10694     }
10695
10696   return CCmode;
10697 }
10698
10699 /* X and Y are two things to compare using CODE.  Emit the compare insn and
10700    return the rtx for register 0 in the proper mode.  FP means this is a
10701    floating point compare: I don't think that it is needed on the arm.  */
10702 rtx
10703 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
10704 {
10705   enum machine_mode mode;
10706   rtx cc_reg;
10707   int dimode_comparison = GET_MODE (x) == DImode || GET_MODE (y) == DImode;
10708
10709   /* We might have X as a constant, Y as a register because of the predicates
10710      used for cmpdi.  If so, force X to a register here.  */
10711   if (dimode_comparison && !REG_P (x))
10712     x = force_reg (DImode, x);
10713
10714   mode = SELECT_CC_MODE (code, x, y);
10715   cc_reg = gen_rtx_REG (mode, CC_REGNUM);
10716
10717   if (dimode_comparison
10718       && !(TARGET_HARD_FLOAT && TARGET_MAVERICK)
10719       && mode != CC_CZmode)
10720     {
10721       rtx clobber, set;
10722
10723       /* To compare two non-zero values for equality, XOR them and
10724          then compare against zero.  Not used for ARM mode; there
10725          CC_CZmode is cheaper.  */
10726       if (mode == CC_Zmode && y != const0_rtx)
10727         {
10728           x = expand_binop (DImode, xor_optab, x, y, NULL_RTX, 0, OPTAB_WIDEN);
10729           y = const0_rtx;
10730         }
10731       /* A scratch register is required.  */
10732       clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (SImode));
10733       set = gen_rtx_SET (VOIDmode, cc_reg, gen_rtx_COMPARE (mode, x, y));
10734       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
10735     }
10736   else
10737     emit_set_insn (cc_reg, gen_rtx_COMPARE (mode, x, y));
10738
10739   return cc_reg;
10740 }
10741
10742 /* Generate a sequence of insns that will generate the correct return
10743    address mask depending on the physical architecture that the program
10744    is running on.  */
10745 rtx
10746 arm_gen_return_addr_mask (void)
10747 {
10748   rtx reg = gen_reg_rtx (Pmode);
10749
10750   emit_insn (gen_return_addr_mask (reg));
10751   return reg;
10752 }
10753
10754 void
10755 arm_reload_in_hi (rtx *operands)
10756 {
10757   rtx ref = operands[1];
10758   rtx base, scratch;
10759   HOST_WIDE_INT offset = 0;
10760
10761   if (GET_CODE (ref) == SUBREG)
10762     {
10763       offset = SUBREG_BYTE (ref);
10764       ref = SUBREG_REG (ref);
10765     }
10766
10767   if (GET_CODE (ref) == REG)
10768     {
10769       /* We have a pseudo which has been spilt onto the stack; there
10770          are two cases here: the first where there is a simple
10771          stack-slot replacement and a second where the stack-slot is
10772          out of range, or is used as a subreg.  */
10773       if (reg_equiv_mem[REGNO (ref)])
10774         {
10775           ref = reg_equiv_mem[REGNO (ref)];
10776           base = find_replacement (&XEXP (ref, 0));
10777         }
10778       else
10779         /* The slot is out of range, or was dressed up in a SUBREG.  */
10780         base = reg_equiv_address[REGNO (ref)];
10781     }
10782   else
10783     base = find_replacement (&XEXP (ref, 0));
10784
10785   /* Handle the case where the address is too complex to be offset by 1.  */
10786   if (GET_CODE (base) == MINUS
10787       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
10788     {
10789       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10790
10791       emit_set_insn (base_plus, base);
10792       base = base_plus;
10793     }
10794   else if (GET_CODE (base) == PLUS)
10795     {
10796       /* The addend must be CONST_INT, or we would have dealt with it above.  */
10797       HOST_WIDE_INT hi, lo;
10798
10799       offset += INTVAL (XEXP (base, 1));
10800       base = XEXP (base, 0);
10801
10802       /* Rework the address into a legal sequence of insns.  */
10803       /* Valid range for lo is -4095 -> 4095 */
10804       lo = (offset >= 0
10805             ? (offset & 0xfff)
10806             : -((-offset) & 0xfff));
10807
10808       /* Corner case, if lo is the max offset then we would be out of range
10809          once we have added the additional 1 below, so bump the msb into the
10810          pre-loading insn(s).  */
10811       if (lo == 4095)
10812         lo &= 0x7ff;
10813
10814       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
10815              ^ (HOST_WIDE_INT) 0x80000000)
10816             - (HOST_WIDE_INT) 0x80000000);
10817
10818       gcc_assert (hi + lo == offset);
10819
10820       if (hi != 0)
10821         {
10822           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10823
10824           /* Get the base address; addsi3 knows how to handle constants
10825              that require more than one insn.  */
10826           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
10827           base = base_plus;
10828           offset = lo;
10829         }
10830     }
10831
10832   /* Operands[2] may overlap operands[0] (though it won't overlap
10833      operands[1]), that's why we asked for a DImode reg -- so we can
10834      use the bit that does not overlap.  */
10835   if (REGNO (operands[2]) == REGNO (operands[0]))
10836     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10837   else
10838     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
10839
10840   emit_insn (gen_zero_extendqisi2 (scratch,
10841                                    gen_rtx_MEM (QImode,
10842                                                 plus_constant (base,
10843                                                                offset))));
10844   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
10845                                    gen_rtx_MEM (QImode,
10846                                                 plus_constant (base,
10847                                                                offset + 1))));
10848   if (!BYTES_BIG_ENDIAN)
10849     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
10850                    gen_rtx_IOR (SImode,
10851                                 gen_rtx_ASHIFT
10852                                 (SImode,
10853                                  gen_rtx_SUBREG (SImode, operands[0], 0),
10854                                  GEN_INT (8)),
10855                                 scratch));
10856   else
10857     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
10858                    gen_rtx_IOR (SImode,
10859                                 gen_rtx_ASHIFT (SImode, scratch,
10860                                                 GEN_INT (8)),
10861                                 gen_rtx_SUBREG (SImode, operands[0], 0)));
10862 }
10863
10864 /* Handle storing a half-word to memory during reload by synthesizing as two
10865    byte stores.  Take care not to clobber the input values until after we
10866    have moved them somewhere safe.  This code assumes that if the DImode
10867    scratch in operands[2] overlaps either the input value or output address
10868    in some way, then that value must die in this insn (we absolutely need
10869    two scratch registers for some corner cases).  */
10870 void
10871 arm_reload_out_hi (rtx *operands)
10872 {
10873   rtx ref = operands[0];
10874   rtx outval = operands[1];
10875   rtx base, scratch;
10876   HOST_WIDE_INT offset = 0;
10877
10878   if (GET_CODE (ref) == SUBREG)
10879     {
10880       offset = SUBREG_BYTE (ref);
10881       ref = SUBREG_REG (ref);
10882     }
10883
10884   if (GET_CODE (ref) == REG)
10885     {
10886       /* We have a pseudo which has been spilt onto the stack; there
10887          are two cases here: the first where there is a simple
10888          stack-slot replacement and a second where the stack-slot is
10889          out of range, or is used as a subreg.  */
10890       if (reg_equiv_mem[REGNO (ref)])
10891         {
10892           ref = reg_equiv_mem[REGNO (ref)];
10893           base = find_replacement (&XEXP (ref, 0));
10894         }
10895       else
10896         /* The slot is out of range, or was dressed up in a SUBREG.  */
10897         base = reg_equiv_address[REGNO (ref)];
10898     }
10899   else
10900     base = find_replacement (&XEXP (ref, 0));
10901
10902   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
10903
10904   /* Handle the case where the address is too complex to be offset by 1.  */
10905   if (GET_CODE (base) == MINUS
10906       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
10907     {
10908       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10909
10910       /* Be careful not to destroy OUTVAL.  */
10911       if (reg_overlap_mentioned_p (base_plus, outval))
10912         {
10913           /* Updating base_plus might destroy outval, see if we can
10914              swap the scratch and base_plus.  */
10915           if (!reg_overlap_mentioned_p (scratch, outval))
10916             {
10917               rtx tmp = scratch;
10918               scratch = base_plus;
10919               base_plus = tmp;
10920             }
10921           else
10922             {
10923               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
10924
10925               /* Be conservative and copy OUTVAL into the scratch now,
10926                  this should only be necessary if outval is a subreg
10927                  of something larger than a word.  */
10928               /* XXX Might this clobber base?  I can't see how it can,
10929                  since scratch is known to overlap with OUTVAL, and
10930                  must be wider than a word.  */
10931               emit_insn (gen_movhi (scratch_hi, outval));
10932               outval = scratch_hi;
10933             }
10934         }
10935
10936       emit_set_insn (base_plus, base);
10937       base = base_plus;
10938     }
10939   else if (GET_CODE (base) == PLUS)
10940     {
10941       /* The addend must be CONST_INT, or we would have dealt with it above.  */
10942       HOST_WIDE_INT hi, lo;
10943
10944       offset += INTVAL (XEXP (base, 1));
10945       base = XEXP (base, 0);
10946
10947       /* Rework the address into a legal sequence of insns.  */
10948       /* Valid range for lo is -4095 -> 4095 */
10949       lo = (offset >= 0
10950             ? (offset & 0xfff)
10951             : -((-offset) & 0xfff));
10952
10953       /* Corner case, if lo is the max offset then we would be out of range
10954          once we have added the additional 1 below, so bump the msb into the
10955          pre-loading insn(s).  */
10956       if (lo == 4095)
10957         lo &= 0x7ff;
10958
10959       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
10960              ^ (HOST_WIDE_INT) 0x80000000)
10961             - (HOST_WIDE_INT) 0x80000000);
10962
10963       gcc_assert (hi + lo == offset);
10964
10965       if (hi != 0)
10966         {
10967           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10968
10969           /* Be careful not to destroy OUTVAL.  */
10970           if (reg_overlap_mentioned_p (base_plus, outval))
10971             {
10972               /* Updating base_plus might destroy outval, see if we
10973                  can swap the scratch and base_plus.  */
10974               if (!reg_overlap_mentioned_p (scratch, outval))
10975                 {
10976                   rtx tmp = scratch;
10977                   scratch = base_plus;
10978                   base_plus = tmp;
10979                 }
10980               else
10981                 {
10982                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
10983
10984                   /* Be conservative and copy outval into scratch now,
10985                      this should only be necessary if outval is a
10986                      subreg of something larger than a word.  */
10987                   /* XXX Might this clobber base?  I can't see how it
10988                      can, since scratch is known to overlap with
10989                      outval.  */
10990                   emit_insn (gen_movhi (scratch_hi, outval));
10991                   outval = scratch_hi;
10992                 }
10993             }
10994
10995           /* Get the base address; addsi3 knows how to handle constants
10996              that require more than one insn.  */
10997           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
10998           base = base_plus;
10999           offset = lo;
11000         }
11001     }
11002
11003   if (BYTES_BIG_ENDIAN)
11004     {
11005       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
11006                                          plus_constant (base, offset + 1)),
11007                             gen_lowpart (QImode, outval)));
11008       emit_insn (gen_lshrsi3 (scratch,
11009                               gen_rtx_SUBREG (SImode, outval, 0),
11010                               GEN_INT (8)));
11011       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
11012                             gen_lowpart (QImode, scratch)));
11013     }
11014   else
11015     {
11016       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
11017                             gen_lowpart (QImode, outval)));
11018       emit_insn (gen_lshrsi3 (scratch,
11019                               gen_rtx_SUBREG (SImode, outval, 0),
11020                               GEN_INT (8)));
11021       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
11022                                          plus_constant (base, offset + 1)),
11023                             gen_lowpart (QImode, scratch)));
11024     }
11025 }
11026
11027 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
11028    (padded to the size of a word) should be passed in a register.  */
11029
11030 static bool
11031 arm_must_pass_in_stack (enum machine_mode mode, const_tree type)
11032 {
11033   if (TARGET_AAPCS_BASED)
11034     return must_pass_in_stack_var_size (mode, type);
11035   else
11036     return must_pass_in_stack_var_size_or_pad (mode, type);
11037 }
11038
11039
11040 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
11041    Return true if an argument passed on the stack should be padded upwards,
11042    i.e. if the least-significant byte has useful data.
11043    For legacy APCS ABIs we use the default.  For AAPCS based ABIs small
11044    aggregate types are placed in the lowest memory address.  */
11045
11046 bool
11047 arm_pad_arg_upward (enum machine_mode mode, const_tree type)
11048 {
11049   if (!TARGET_AAPCS_BASED)
11050     return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
11051
11052   if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
11053     return false;
11054
11055   return true;
11056 }
11057
11058
11059 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
11060    For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
11061    byte of the register has useful data, and return the opposite if the
11062    most significant byte does.
11063    For AAPCS, small aggregates and small complex types are always padded
11064    upwards.  */
11065
11066 bool
11067 arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
11068                     tree type, int first ATTRIBUTE_UNUSED)
11069 {
11070   if (TARGET_AAPCS_BASED
11071       && BYTES_BIG_ENDIAN
11072       && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11073       && int_size_in_bytes (type) <= 4)
11074     return true;
11075
11076   /* Otherwise, use default padding.  */
11077   return !BYTES_BIG_ENDIAN;
11078 }
11079
11080 \f
11081 /* Print a symbolic form of X to the debug file, F.  */
11082 static void
11083 arm_print_value (FILE *f, rtx x)
11084 {
11085   switch (GET_CODE (x))
11086     {
11087     case CONST_INT:
11088       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
11089       return;
11090
11091     case CONST_DOUBLE:
11092       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
11093       return;
11094
11095     case CONST_VECTOR:
11096       {
11097         int i;
11098
11099         fprintf (f, "<");
11100         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
11101           {
11102             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
11103             if (i < (CONST_VECTOR_NUNITS (x) - 1))
11104               fputc (',', f);
11105           }
11106         fprintf (f, ">");
11107       }
11108       return;
11109
11110     case CONST_STRING:
11111       fprintf (f, "\"%s\"", XSTR (x, 0));
11112       return;
11113
11114     case SYMBOL_REF:
11115       fprintf (f, "`%s'", XSTR (x, 0));
11116       return;
11117
11118     case LABEL_REF:
11119       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
11120       return;
11121
11122     case CONST:
11123       arm_print_value (f, XEXP (x, 0));
11124       return;
11125
11126     case PLUS:
11127       arm_print_value (f, XEXP (x, 0));
11128       fprintf (f, "+");
11129       arm_print_value (f, XEXP (x, 1));
11130       return;
11131
11132     case PC:
11133       fprintf (f, "pc");
11134       return;
11135
11136     default:
11137       fprintf (f, "????");
11138       return;
11139     }
11140 }
11141 \f
11142 /* Routines for manipulation of the constant pool.  */
11143
11144 /* Arm instructions cannot load a large constant directly into a
11145    register; they have to come from a pc relative load.  The constant
11146    must therefore be placed in the addressable range of the pc
11147    relative load.  Depending on the precise pc relative load
11148    instruction the range is somewhere between 256 bytes and 4k.  This
11149    means that we often have to dump a constant inside a function, and
11150    generate code to branch around it.
11151
11152    It is important to minimize this, since the branches will slow
11153    things down and make the code larger.
11154
11155    Normally we can hide the table after an existing unconditional
11156    branch so that there is no interruption of the flow, but in the
11157    worst case the code looks like this:
11158
11159         ldr     rn, L1
11160         ...
11161         b       L2
11162         align
11163         L1:     .long value
11164         L2:
11165         ...
11166
11167         ldr     rn, L3
11168         ...
11169         b       L4
11170         align
11171         L3:     .long value
11172         L4:
11173         ...
11174
11175    We fix this by performing a scan after scheduling, which notices
11176    which instructions need to have their operands fetched from the
11177    constant table and builds the table.
11178
11179    The algorithm starts by building a table of all the constants that
11180    need fixing up and all the natural barriers in the function (places
11181    where a constant table can be dropped without breaking the flow).
11182    For each fixup we note how far the pc-relative replacement will be
11183    able to reach and the offset of the instruction into the function.
11184
11185    Having built the table we then group the fixes together to form
11186    tables that are as large as possible (subject to addressing
11187    constraints) and emit each table of constants after the last
11188    barrier that is within range of all the instructions in the group.
11189    If a group does not contain a barrier, then we forcibly create one
11190    by inserting a jump instruction into the flow.  Once the table has
11191    been inserted, the insns are then modified to reference the
11192    relevant entry in the pool.
11193
11194    Possible enhancements to the algorithm (not implemented) are:
11195
11196    1) For some processors and object formats, there may be benefit in
11197    aligning the pools to the start of cache lines; this alignment
11198    would need to be taken into account when calculating addressability
11199    of a pool.  */
11200
11201 /* These typedefs are located at the start of this file, so that
11202    they can be used in the prototypes there.  This comment is to
11203    remind readers of that fact so that the following structures
11204    can be understood more easily.
11205
11206      typedef struct minipool_node    Mnode;
11207      typedef struct minipool_fixup   Mfix;  */
11208
11209 struct minipool_node
11210 {
11211   /* Doubly linked chain of entries.  */
11212   Mnode * next;
11213   Mnode * prev;
11214   /* The maximum offset into the code that this entry can be placed.  While
11215      pushing fixes for forward references, all entries are sorted in order
11216      of increasing max_address.  */
11217   HOST_WIDE_INT max_address;
11218   /* Similarly for an entry inserted for a backwards ref.  */
11219   HOST_WIDE_INT min_address;
11220   /* The number of fixes referencing this entry.  This can become zero
11221      if we "unpush" an entry.  In this case we ignore the entry when we
11222      come to emit the code.  */
11223   int refcount;
11224   /* The offset from the start of the minipool.  */
11225   HOST_WIDE_INT offset;
11226   /* The value in table.  */
11227   rtx value;
11228   /* The mode of value.  */
11229   enum machine_mode mode;
11230   /* The size of the value.  With iWMMXt enabled
11231      sizes > 4 also imply an alignment of 8-bytes.  */
11232   int fix_size;
11233 };
11234
11235 struct minipool_fixup
11236 {
11237   Mfix *            next;
11238   rtx               insn;
11239   HOST_WIDE_INT     address;
11240   rtx *             loc;
11241   enum machine_mode mode;
11242   int               fix_size;
11243   rtx               value;
11244   Mnode *           minipool;
11245   HOST_WIDE_INT     forwards;
11246   HOST_WIDE_INT     backwards;
11247 };
11248
11249 /* Fixes less than a word need padding out to a word boundary.  */
11250 #define MINIPOOL_FIX_SIZE(mode) \
11251   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
11252
11253 static Mnode *  minipool_vector_head;
11254 static Mnode *  minipool_vector_tail;
11255 static rtx      minipool_vector_label;
11256 static int      minipool_pad;
11257
11258 /* The linked list of all minipool fixes required for this function.  */
11259 Mfix *          minipool_fix_head;
11260 Mfix *          minipool_fix_tail;
11261 /* The fix entry for the current minipool, once it has been placed.  */
11262 Mfix *          minipool_barrier;
11263
11264 /* Determines if INSN is the start of a jump table.  Returns the end
11265    of the TABLE or NULL_RTX.  */
11266 static rtx
11267 is_jump_table (rtx insn)
11268 {
11269   rtx table;
11270
11271   if (GET_CODE (insn) == JUMP_INSN
11272       && JUMP_LABEL (insn) != NULL
11273       && ((table = next_real_insn (JUMP_LABEL (insn)))
11274           == next_real_insn (insn))
11275       && table != NULL
11276       && GET_CODE (table) == JUMP_INSN
11277       && (GET_CODE (PATTERN (table)) == ADDR_VEC
11278           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
11279     return table;
11280
11281   return NULL_RTX;
11282 }
11283
11284 #ifndef JUMP_TABLES_IN_TEXT_SECTION
11285 #define JUMP_TABLES_IN_TEXT_SECTION 0
11286 #endif
11287
11288 static HOST_WIDE_INT
11289 get_jump_table_size (rtx insn)
11290 {
11291   /* ADDR_VECs only take room if read-only data does into the text
11292      section.  */
11293   if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
11294     {
11295       rtx body = PATTERN (insn);
11296       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
11297       HOST_WIDE_INT size;
11298       HOST_WIDE_INT modesize;
11299
11300       modesize = GET_MODE_SIZE (GET_MODE (body));
11301       size = modesize * XVECLEN (body, elt);
11302       switch (modesize)
11303         {
11304         case 1:
11305           /* Round up size  of TBB table to a halfword boundary.  */
11306           size = (size + 1) & ~(HOST_WIDE_INT)1;
11307           break;
11308         case 2:
11309           /* No padding necessary for TBH.  */
11310           break;
11311         case 4:
11312           /* Add two bytes for alignment on Thumb.  */
11313           if (TARGET_THUMB)
11314             size += 2;
11315           break;
11316         default:
11317           gcc_unreachable ();
11318         }
11319       return size;
11320     }
11321
11322   return 0;
11323 }
11324
11325 /* Move a minipool fix MP from its current location to before MAX_MP.
11326    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
11327    constraints may need updating.  */
11328 static Mnode *
11329 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
11330                                HOST_WIDE_INT max_address)
11331 {
11332   /* The code below assumes these are different.  */
11333   gcc_assert (mp != max_mp);
11334
11335   if (max_mp == NULL)
11336     {
11337       if (max_address < mp->max_address)
11338         mp->max_address = max_address;
11339     }
11340   else
11341     {
11342       if (max_address > max_mp->max_address - mp->fix_size)
11343         mp->max_address = max_mp->max_address - mp->fix_size;
11344       else
11345         mp->max_address = max_address;
11346
11347       /* Unlink MP from its current position.  Since max_mp is non-null,
11348        mp->prev must be non-null.  */
11349       mp->prev->next = mp->next;
11350       if (mp->next != NULL)
11351         mp->next->prev = mp->prev;
11352       else
11353         minipool_vector_tail = mp->prev;
11354
11355       /* Re-insert it before MAX_MP.  */
11356       mp->next = max_mp;
11357       mp->prev = max_mp->prev;
11358       max_mp->prev = mp;
11359
11360       if (mp->prev != NULL)
11361         mp->prev->next = mp;
11362       else
11363         minipool_vector_head = mp;
11364     }
11365
11366   /* Save the new entry.  */
11367   max_mp = mp;
11368
11369   /* Scan over the preceding entries and adjust their addresses as
11370      required.  */
11371   while (mp->prev != NULL
11372          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
11373     {
11374       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
11375       mp = mp->prev;
11376     }
11377
11378   return max_mp;
11379 }
11380
11381 /* Add a constant to the minipool for a forward reference.  Returns the
11382    node added or NULL if the constant will not fit in this pool.  */
11383 static Mnode *
11384 add_minipool_forward_ref (Mfix *fix)
11385 {
11386   /* If set, max_mp is the first pool_entry that has a lower
11387      constraint than the one we are trying to add.  */
11388   Mnode *       max_mp = NULL;
11389   HOST_WIDE_INT max_address = fix->address + fix->forwards - minipool_pad;
11390   Mnode *       mp;
11391
11392   /* If the minipool starts before the end of FIX->INSN then this FIX
11393      can not be placed into the current pool.  Furthermore, adding the
11394      new constant pool entry may cause the pool to start FIX_SIZE bytes
11395      earlier.  */
11396   if (minipool_vector_head &&
11397       (fix->address + get_attr_length (fix->insn)
11398        >= minipool_vector_head->max_address - fix->fix_size))
11399     return NULL;
11400
11401   /* Scan the pool to see if a constant with the same value has
11402      already been added.  While we are doing this, also note the
11403      location where we must insert the constant if it doesn't already
11404      exist.  */
11405   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11406     {
11407       if (GET_CODE (fix->value) == GET_CODE (mp->value)
11408           && fix->mode == mp->mode
11409           && (GET_CODE (fix->value) != CODE_LABEL
11410               || (CODE_LABEL_NUMBER (fix->value)
11411                   == CODE_LABEL_NUMBER (mp->value)))
11412           && rtx_equal_p (fix->value, mp->value))
11413         {
11414           /* More than one fix references this entry.  */
11415           mp->refcount++;
11416           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
11417         }
11418
11419       /* Note the insertion point if necessary.  */
11420       if (max_mp == NULL
11421           && mp->max_address > max_address)
11422         max_mp = mp;
11423
11424       /* If we are inserting an 8-bytes aligned quantity and
11425          we have not already found an insertion point, then
11426          make sure that all such 8-byte aligned quantities are
11427          placed at the start of the pool.  */
11428       if (ARM_DOUBLEWORD_ALIGN
11429           && max_mp == NULL
11430           && fix->fix_size >= 8
11431           && mp->fix_size < 8)
11432         {
11433           max_mp = mp;
11434           max_address = mp->max_address;
11435         }
11436     }
11437
11438   /* The value is not currently in the minipool, so we need to create
11439      a new entry for it.  If MAX_MP is NULL, the entry will be put on
11440      the end of the list since the placement is less constrained than
11441      any existing entry.  Otherwise, we insert the new fix before
11442      MAX_MP and, if necessary, adjust the constraints on the other
11443      entries.  */
11444   mp = XNEW (Mnode);
11445   mp->fix_size = fix->fix_size;
11446   mp->mode = fix->mode;
11447   mp->value = fix->value;
11448   mp->refcount = 1;
11449   /* Not yet required for a backwards ref.  */
11450   mp->min_address = -65536;
11451
11452   if (max_mp == NULL)
11453     {
11454       mp->max_address = max_address;
11455       mp->next = NULL;
11456       mp->prev = minipool_vector_tail;
11457
11458       if (mp->prev == NULL)
11459         {
11460           minipool_vector_head = mp;
11461           minipool_vector_label = gen_label_rtx ();
11462         }
11463       else
11464         mp->prev->next = mp;
11465
11466       minipool_vector_tail = mp;
11467     }
11468   else
11469     {
11470       if (max_address > max_mp->max_address - mp->fix_size)
11471         mp->max_address = max_mp->max_address - mp->fix_size;
11472       else
11473         mp->max_address = max_address;
11474
11475       mp->next = max_mp;
11476       mp->prev = max_mp->prev;
11477       max_mp->prev = mp;
11478       if (mp->prev != NULL)
11479         mp->prev->next = mp;
11480       else
11481         minipool_vector_head = mp;
11482     }
11483
11484   /* Save the new entry.  */
11485   max_mp = mp;
11486
11487   /* Scan over the preceding entries and adjust their addresses as
11488      required.  */
11489   while (mp->prev != NULL
11490          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
11491     {
11492       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
11493       mp = mp->prev;
11494     }
11495
11496   return max_mp;
11497 }
11498
11499 static Mnode *
11500 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
11501                                 HOST_WIDE_INT  min_address)
11502 {
11503   HOST_WIDE_INT offset;
11504
11505   /* The code below assumes these are different.  */
11506   gcc_assert (mp != min_mp);
11507
11508   if (min_mp == NULL)
11509     {
11510       if (min_address > mp->min_address)
11511         mp->min_address = min_address;
11512     }
11513   else
11514     {
11515       /* We will adjust this below if it is too loose.  */
11516       mp->min_address = min_address;
11517
11518       /* Unlink MP from its current position.  Since min_mp is non-null,
11519          mp->next must be non-null.  */
11520       mp->next->prev = mp->prev;
11521       if (mp->prev != NULL)
11522         mp->prev->next = mp->next;
11523       else
11524         minipool_vector_head = mp->next;
11525
11526       /* Reinsert it after MIN_MP.  */
11527       mp->prev = min_mp;
11528       mp->next = min_mp->next;
11529       min_mp->next = mp;
11530       if (mp->next != NULL)
11531         mp->next->prev = mp;
11532       else
11533         minipool_vector_tail = mp;
11534     }
11535
11536   min_mp = mp;
11537
11538   offset = 0;
11539   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11540     {
11541       mp->offset = offset;
11542       if (mp->refcount > 0)
11543         offset += mp->fix_size;
11544
11545       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
11546         mp->next->min_address = mp->min_address + mp->fix_size;
11547     }
11548
11549   return min_mp;
11550 }
11551
11552 /* Add a constant to the minipool for a backward reference.  Returns the
11553    node added or NULL if the constant will not fit in this pool.
11554
11555    Note that the code for insertion for a backwards reference can be
11556    somewhat confusing because the calculated offsets for each fix do
11557    not take into account the size of the pool (which is still under
11558    construction.  */
11559 static Mnode *
11560 add_minipool_backward_ref (Mfix *fix)
11561 {
11562   /* If set, min_mp is the last pool_entry that has a lower constraint
11563      than the one we are trying to add.  */
11564   Mnode *min_mp = NULL;
11565   /* This can be negative, since it is only a constraint.  */
11566   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
11567   Mnode *mp;
11568
11569   /* If we can't reach the current pool from this insn, or if we can't
11570      insert this entry at the end of the pool without pushing other
11571      fixes out of range, then we don't try.  This ensures that we
11572      can't fail later on.  */
11573   if (min_address >= minipool_barrier->address
11574       || (minipool_vector_tail->min_address + fix->fix_size
11575           >= minipool_barrier->address))
11576     return NULL;
11577
11578   /* Scan the pool to see if a constant with the same value has
11579      already been added.  While we are doing this, also note the
11580      location where we must insert the constant if it doesn't already
11581      exist.  */
11582   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
11583     {
11584       if (GET_CODE (fix->value) == GET_CODE (mp->value)
11585           && fix->mode == mp->mode
11586           && (GET_CODE (fix->value) != CODE_LABEL
11587               || (CODE_LABEL_NUMBER (fix->value)
11588                   == CODE_LABEL_NUMBER (mp->value)))
11589           && rtx_equal_p (fix->value, mp->value)
11590           /* Check that there is enough slack to move this entry to the
11591              end of the table (this is conservative).  */
11592           && (mp->max_address
11593               > (minipool_barrier->address
11594                  + minipool_vector_tail->offset
11595                  + minipool_vector_tail->fix_size)))
11596         {
11597           mp->refcount++;
11598           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
11599         }
11600
11601       if (min_mp != NULL)
11602         mp->min_address += fix->fix_size;
11603       else
11604         {
11605           /* Note the insertion point if necessary.  */
11606           if (mp->min_address < min_address)
11607             {
11608               /* For now, we do not allow the insertion of 8-byte alignment
11609                  requiring nodes anywhere but at the start of the pool.  */
11610               if (ARM_DOUBLEWORD_ALIGN
11611                   && fix->fix_size >= 8 && mp->fix_size < 8)
11612                 return NULL;
11613               else
11614                 min_mp = mp;
11615             }
11616           else if (mp->max_address
11617                    < minipool_barrier->address + mp->offset + fix->fix_size)
11618             {
11619               /* Inserting before this entry would push the fix beyond
11620                  its maximum address (which can happen if we have
11621                  re-located a forwards fix); force the new fix to come
11622                  after it.  */
11623               if (ARM_DOUBLEWORD_ALIGN
11624                   && fix->fix_size >= 8 && mp->fix_size < 8)
11625                 return NULL;
11626               else
11627                 {
11628                   min_mp = mp;
11629                   min_address = mp->min_address + fix->fix_size;
11630                 }
11631             }
11632           /* Do not insert a non-8-byte aligned quantity before 8-byte
11633              aligned quantities.  */
11634           else if (ARM_DOUBLEWORD_ALIGN
11635                    && fix->fix_size < 8
11636                    && mp->fix_size >= 8)
11637             {
11638               min_mp = mp;
11639               min_address = mp->min_address + fix->fix_size;
11640             }
11641         }
11642     }
11643
11644   /* We need to create a new entry.  */
11645   mp = XNEW (Mnode);
11646   mp->fix_size = fix->fix_size;
11647   mp->mode = fix->mode;
11648   mp->value = fix->value;
11649   mp->refcount = 1;
11650   mp->max_address = minipool_barrier->address + 65536;
11651
11652   mp->min_address = min_address;
11653
11654   if (min_mp == NULL)
11655     {
11656       mp->prev = NULL;
11657       mp->next = minipool_vector_head;
11658
11659       if (mp->next == NULL)
11660         {
11661           minipool_vector_tail = mp;
11662           minipool_vector_label = gen_label_rtx ();
11663         }
11664       else
11665         mp->next->prev = mp;
11666
11667       minipool_vector_head = mp;
11668     }
11669   else
11670     {
11671       mp->next = min_mp->next;
11672       mp->prev = min_mp;
11673       min_mp->next = mp;
11674
11675       if (mp->next != NULL)
11676         mp->next->prev = mp;
11677       else
11678         minipool_vector_tail = mp;
11679     }
11680
11681   /* Save the new entry.  */
11682   min_mp = mp;
11683
11684   if (mp->prev)
11685     mp = mp->prev;
11686   else
11687     mp->offset = 0;
11688
11689   /* Scan over the following entries and adjust their offsets.  */
11690   while (mp->next != NULL)
11691     {
11692       if (mp->next->min_address < mp->min_address + mp->fix_size)
11693         mp->next->min_address = mp->min_address + mp->fix_size;
11694
11695       if (mp->refcount)
11696         mp->next->offset = mp->offset + mp->fix_size;
11697       else
11698         mp->next->offset = mp->offset;
11699
11700       mp = mp->next;
11701     }
11702
11703   return min_mp;
11704 }
11705
11706 static void
11707 assign_minipool_offsets (Mfix *barrier)
11708 {
11709   HOST_WIDE_INT offset = 0;
11710   Mnode *mp;
11711
11712   minipool_barrier = barrier;
11713
11714   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11715     {
11716       mp->offset = offset;
11717
11718       if (mp->refcount > 0)
11719         offset += mp->fix_size;
11720     }
11721 }
11722
11723 /* Output the literal table */
11724 static void
11725 dump_minipool (rtx scan)
11726 {
11727   Mnode * mp;
11728   Mnode * nmp;
11729   int align64 = 0;
11730
11731   if (ARM_DOUBLEWORD_ALIGN)
11732     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11733       if (mp->refcount > 0 && mp->fix_size >= 8)
11734         {
11735           align64 = 1;
11736           break;
11737         }
11738
11739   if (dump_file)
11740     fprintf (dump_file,
11741              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
11742              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
11743
11744   scan = emit_label_after (gen_label_rtx (), scan);
11745   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
11746   scan = emit_label_after (minipool_vector_label, scan);
11747
11748   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
11749     {
11750       if (mp->refcount > 0)
11751         {
11752           if (dump_file)
11753             {
11754               fprintf (dump_file,
11755                        ";;  Offset %u, min %ld, max %ld ",
11756                        (unsigned) mp->offset, (unsigned long) mp->min_address,
11757                        (unsigned long) mp->max_address);
11758               arm_print_value (dump_file, mp->value);
11759               fputc ('\n', dump_file);
11760             }
11761
11762           switch (mp->fix_size)
11763             {
11764 #ifdef HAVE_consttable_1
11765             case 1:
11766               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
11767               break;
11768
11769 #endif
11770 #ifdef HAVE_consttable_2
11771             case 2:
11772               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
11773               break;
11774
11775 #endif
11776 #ifdef HAVE_consttable_4
11777             case 4:
11778               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
11779               break;
11780
11781 #endif
11782 #ifdef HAVE_consttable_8
11783             case 8:
11784               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
11785               break;
11786
11787 #endif
11788 #ifdef HAVE_consttable_16
11789             case 16:
11790               scan = emit_insn_after (gen_consttable_16 (mp->value), scan);
11791               break;
11792
11793 #endif
11794             default:
11795               gcc_unreachable ();
11796             }
11797         }
11798
11799       nmp = mp->next;
11800       free (mp);
11801     }
11802
11803   minipool_vector_head = minipool_vector_tail = NULL;
11804   scan = emit_insn_after (gen_consttable_end (), scan);
11805   scan = emit_barrier_after (scan);
11806 }
11807
11808 /* Return the cost of forcibly inserting a barrier after INSN.  */
11809 static int
11810 arm_barrier_cost (rtx insn)
11811 {
11812   /* Basing the location of the pool on the loop depth is preferable,
11813      but at the moment, the basic block information seems to be
11814      corrupt by this stage of the compilation.  */
11815   int base_cost = 50;
11816   rtx next = next_nonnote_insn (insn);
11817
11818   if (next != NULL && GET_CODE (next) == CODE_LABEL)
11819     base_cost -= 20;
11820
11821   switch (GET_CODE (insn))
11822     {
11823     case CODE_LABEL:
11824       /* It will always be better to place the table before the label, rather
11825          than after it.  */
11826       return 50;
11827
11828     case INSN:
11829     case CALL_INSN:
11830       return base_cost;
11831
11832     case JUMP_INSN:
11833       return base_cost - 10;
11834
11835     default:
11836       return base_cost + 10;
11837     }
11838 }
11839
11840 /* Find the best place in the insn stream in the range
11841    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
11842    Create the barrier by inserting a jump and add a new fix entry for
11843    it.  */
11844 static Mfix *
11845 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
11846 {
11847   HOST_WIDE_INT count = 0;
11848   rtx barrier;
11849   rtx from = fix->insn;
11850   /* The instruction after which we will insert the jump.  */
11851   rtx selected = NULL;
11852   int selected_cost;
11853   /* The address at which the jump instruction will be placed.  */
11854   HOST_WIDE_INT selected_address;
11855   Mfix * new_fix;
11856   HOST_WIDE_INT max_count = max_address - fix->address;
11857   rtx label = gen_label_rtx ();
11858
11859   selected_cost = arm_barrier_cost (from);
11860   selected_address = fix->address;
11861
11862   while (from && count < max_count)
11863     {
11864       rtx tmp;
11865       int new_cost;
11866
11867       /* This code shouldn't have been called if there was a natural barrier
11868          within range.  */
11869       gcc_assert (GET_CODE (from) != BARRIER);
11870
11871       /* Count the length of this insn.  */
11872       count += get_attr_length (from);
11873
11874       /* If there is a jump table, add its length.  */
11875       tmp = is_jump_table (from);
11876       if (tmp != NULL)
11877         {
11878           count += get_jump_table_size (tmp);
11879
11880           /* Jump tables aren't in a basic block, so base the cost on
11881              the dispatch insn.  If we select this location, we will
11882              still put the pool after the table.  */
11883           new_cost = arm_barrier_cost (from);
11884
11885           if (count < max_count 
11886               && (!selected || new_cost <= selected_cost))
11887             {
11888               selected = tmp;
11889               selected_cost = new_cost;
11890               selected_address = fix->address + count;
11891             }
11892
11893           /* Continue after the dispatch table.  */
11894           from = NEXT_INSN (tmp);
11895           continue;
11896         }
11897
11898       new_cost = arm_barrier_cost (from);
11899
11900       if (count < max_count
11901           && (!selected || new_cost <= selected_cost))
11902         {
11903           selected = from;
11904           selected_cost = new_cost;
11905           selected_address = fix->address + count;
11906         }
11907
11908       from = NEXT_INSN (from);
11909     }
11910
11911   /* Make sure that we found a place to insert the jump.  */
11912   gcc_assert (selected);
11913
11914   /* Create a new JUMP_INSN that branches around a barrier.  */
11915   from = emit_jump_insn_after (gen_jump (label), selected);
11916   JUMP_LABEL (from) = label;
11917   barrier = emit_barrier_after (from);
11918   emit_label_after (label, barrier);
11919
11920   /* Create a minipool barrier entry for the new barrier.  */
11921   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
11922   new_fix->insn = barrier;
11923   new_fix->address = selected_address;
11924   new_fix->next = fix->next;
11925   fix->next = new_fix;
11926
11927   return new_fix;
11928 }
11929
11930 /* Record that there is a natural barrier in the insn stream at
11931    ADDRESS.  */
11932 static void
11933 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
11934 {
11935   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
11936
11937   fix->insn = insn;
11938   fix->address = address;
11939
11940   fix->next = NULL;
11941   if (minipool_fix_head != NULL)
11942     minipool_fix_tail->next = fix;
11943   else
11944     minipool_fix_head = fix;
11945
11946   minipool_fix_tail = fix;
11947 }
11948
11949 /* Record INSN, which will need fixing up to load a value from the
11950    minipool.  ADDRESS is the offset of the insn since the start of the
11951    function; LOC is a pointer to the part of the insn which requires
11952    fixing; VALUE is the constant that must be loaded, which is of type
11953    MODE.  */
11954 static void
11955 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
11956                    enum machine_mode mode, rtx value)
11957 {
11958   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
11959
11960   fix->insn = insn;
11961   fix->address = address;
11962   fix->loc = loc;
11963   fix->mode = mode;
11964   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
11965   fix->value = value;
11966   fix->forwards = get_attr_pool_range (insn);
11967   fix->backwards = get_attr_neg_pool_range (insn);
11968   fix->minipool = NULL;
11969
11970   /* If an insn doesn't have a range defined for it, then it isn't
11971      expecting to be reworked by this code.  Better to stop now than
11972      to generate duff assembly code.  */
11973   gcc_assert (fix->forwards || fix->backwards);
11974
11975   /* If an entry requires 8-byte alignment then assume all constant pools
11976      require 4 bytes of padding.  Trying to do this later on a per-pool
11977      basis is awkward because existing pool entries have to be modified.  */
11978   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size >= 8)
11979     minipool_pad = 4;
11980
11981   if (dump_file)
11982     {
11983       fprintf (dump_file,
11984                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
11985                GET_MODE_NAME (mode),
11986                INSN_UID (insn), (unsigned long) address,
11987                -1 * (long)fix->backwards, (long)fix->forwards);
11988       arm_print_value (dump_file, fix->value);
11989       fprintf (dump_file, "\n");
11990     }
11991
11992   /* Add it to the chain of fixes.  */
11993   fix->next = NULL;
11994
11995   if (minipool_fix_head != NULL)
11996     minipool_fix_tail->next = fix;
11997   else
11998     minipool_fix_head = fix;
11999
12000   minipool_fix_tail = fix;
12001 }
12002
12003 /* Return the cost of synthesizing a 64-bit constant VAL inline.
12004    Returns the number of insns needed, or 99 if we don't know how to
12005    do it.  */
12006 int
12007 arm_const_double_inline_cost (rtx val)
12008 {
12009   rtx lowpart, highpart;
12010   enum machine_mode mode;
12011
12012   mode = GET_MODE (val);
12013
12014   if (mode == VOIDmode)
12015     mode = DImode;
12016
12017   gcc_assert (GET_MODE_SIZE (mode) == 8);
12018
12019   lowpart = gen_lowpart (SImode, val);
12020   highpart = gen_highpart_mode (SImode, mode, val);
12021
12022   gcc_assert (GET_CODE (lowpart) == CONST_INT);
12023   gcc_assert (GET_CODE (highpart) == CONST_INT);
12024
12025   return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
12026                             NULL_RTX, NULL_RTX, 0, 0)
12027           + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
12028                               NULL_RTX, NULL_RTX, 0, 0));
12029 }
12030
12031 /* Return true if it is worthwhile to split a 64-bit constant into two
12032    32-bit operations.  This is the case if optimizing for size, or
12033    if we have load delay slots, or if one 32-bit part can be done with
12034    a single data operation.  */
12035 bool
12036 arm_const_double_by_parts (rtx val)
12037 {
12038   enum machine_mode mode = GET_MODE (val);
12039   rtx part;
12040
12041   if (optimize_size || arm_ld_sched)
12042     return true;
12043
12044   if (mode == VOIDmode)
12045     mode = DImode;
12046
12047   part = gen_highpart_mode (SImode, mode, val);
12048
12049   gcc_assert (GET_CODE (part) == CONST_INT);
12050
12051   if (const_ok_for_arm (INTVAL (part))
12052       || const_ok_for_arm (~INTVAL (part)))
12053     return true;
12054
12055   part = gen_lowpart (SImode, val);
12056
12057   gcc_assert (GET_CODE (part) == CONST_INT);
12058
12059   if (const_ok_for_arm (INTVAL (part))
12060       || const_ok_for_arm (~INTVAL (part)))
12061     return true;
12062
12063   return false;
12064 }
12065
12066 /* Return true if it is possible to inline both the high and low parts
12067    of a 64-bit constant into 32-bit data processing instructions.  */
12068 bool
12069 arm_const_double_by_immediates (rtx val)
12070 {
12071   enum machine_mode mode = GET_MODE (val);
12072   rtx part;
12073
12074   if (mode == VOIDmode)
12075     mode = DImode;
12076
12077   part = gen_highpart_mode (SImode, mode, val);
12078
12079   gcc_assert (GET_CODE (part) == CONST_INT);
12080
12081   if (!const_ok_for_arm (INTVAL (part)))
12082     return false;
12083
12084   part = gen_lowpart (SImode, val);
12085
12086   gcc_assert (GET_CODE (part) == CONST_INT);
12087
12088   if (!const_ok_for_arm (INTVAL (part)))
12089     return false;
12090
12091   return true;
12092 }
12093
12094 /* Scan INSN and note any of its operands that need fixing.
12095    If DO_PUSHES is false we do not actually push any of the fixups
12096    needed.  The function returns TRUE if any fixups were needed/pushed.
12097    This is used by arm_memory_load_p() which needs to know about loads
12098    of constants that will be converted into minipool loads.  */
12099 static bool
12100 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
12101 {
12102   bool result = false;
12103   int opno;
12104
12105   extract_insn (insn);
12106
12107   if (!constrain_operands (1))
12108     fatal_insn_not_found (insn);
12109
12110   if (recog_data.n_alternatives == 0)
12111     return false;
12112
12113   /* Fill in recog_op_alt with information about the constraints of
12114      this insn.  */
12115   preprocess_constraints ();
12116
12117   for (opno = 0; opno < recog_data.n_operands; opno++)
12118     {
12119       /* Things we need to fix can only occur in inputs.  */
12120       if (recog_data.operand_type[opno] != OP_IN)
12121         continue;
12122
12123       /* If this alternative is a memory reference, then any mention
12124          of constants in this alternative is really to fool reload
12125          into allowing us to accept one there.  We need to fix them up
12126          now so that we output the right code.  */
12127       if (recog_op_alt[opno][which_alternative].memory_ok)
12128         {
12129           rtx op = recog_data.operand[opno];
12130
12131           if (CONSTANT_P (op))
12132             {
12133               if (do_pushes)
12134                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
12135                                    recog_data.operand_mode[opno], op);
12136               result = true;
12137             }
12138           else if (GET_CODE (op) == MEM
12139                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
12140                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
12141             {
12142               if (do_pushes)
12143                 {
12144                   rtx cop = avoid_constant_pool_reference (op);
12145
12146                   /* Casting the address of something to a mode narrower
12147                      than a word can cause avoid_constant_pool_reference()
12148                      to return the pool reference itself.  That's no good to
12149                      us here.  Lets just hope that we can use the
12150                      constant pool value directly.  */
12151                   if (op == cop)
12152                     cop = get_pool_constant (XEXP (op, 0));
12153
12154                   push_minipool_fix (insn, address,
12155                                      recog_data.operand_loc[opno],
12156                                      recog_data.operand_mode[opno], cop);
12157                 }
12158
12159               result = true;
12160             }
12161         }
12162     }
12163
12164   return result;
12165 }
12166
12167 /* Convert instructions to their cc-clobbering variant if possible, since
12168    that allows us to use smaller encodings.  */
12169
12170 static void
12171 thumb2_reorg (void)
12172 {
12173   basic_block bb;
12174   regset_head live;
12175
12176   INIT_REG_SET (&live);
12177
12178   /* We are freeing block_for_insn in the toplev to keep compatibility
12179      with old MDEP_REORGS that are not CFG based.  Recompute it now.  */
12180   compute_bb_for_insn ();
12181   df_analyze ();
12182
12183   FOR_EACH_BB (bb)
12184     {
12185       rtx insn;
12186       COPY_REG_SET (&live, DF_LR_OUT (bb));
12187       df_simulate_initialize_backwards (bb, &live);
12188       FOR_BB_INSNS_REVERSE (bb, insn)
12189         {
12190           if (NONJUMP_INSN_P (insn)
12191               && !REGNO_REG_SET_P (&live, CC_REGNUM))
12192             {
12193               rtx pat = PATTERN (insn);
12194               if (GET_CODE (pat) == SET
12195                   && low_register_operand (XEXP (pat, 0), SImode)
12196                   && thumb_16bit_operator (XEXP (pat, 1), SImode)
12197                   && low_register_operand (XEXP (XEXP (pat, 1), 0), SImode)
12198                   && low_register_operand (XEXP (XEXP (pat, 1), 1), SImode))
12199                 {
12200                   rtx dst = XEXP (pat, 0);
12201                   rtx src = XEXP (pat, 1);
12202                   rtx op0 = XEXP (src, 0);
12203                   if (rtx_equal_p (dst, op0)
12204                       || GET_CODE (src) == PLUS || GET_CODE (src) == MINUS)
12205                     {
12206                       rtx ccreg = gen_rtx_REG (CCmode, CC_REGNUM);
12207                       rtx clobber = gen_rtx_CLOBBER (VOIDmode, ccreg);
12208                       rtvec vec = gen_rtvec (2, pat, clobber);
12209                       PATTERN (insn) = gen_rtx_PARALLEL (VOIDmode, vec);
12210                       INSN_CODE (insn) = -1;
12211                     }
12212                 }
12213             }
12214           if (NONDEBUG_INSN_P (insn))
12215             df_simulate_one_insn_backwards (bb, insn, &live);
12216         }
12217     }
12218   CLEAR_REG_SET (&live);
12219 }
12220
12221 /* Gcc puts the pool in the wrong place for ARM, since we can only
12222    load addresses a limited distance around the pc.  We do some
12223    special munging to move the constant pool values to the correct
12224    point in the code.  */
12225 static void
12226 arm_reorg (void)
12227 {
12228   rtx insn;
12229   HOST_WIDE_INT address = 0;
12230   Mfix * fix;
12231
12232   if (TARGET_THUMB2)
12233     thumb2_reorg ();
12234   
12235   minipool_fix_head = minipool_fix_tail = NULL;
12236
12237   /* The first insn must always be a note, or the code below won't
12238      scan it properly.  */
12239   insn = get_insns ();
12240   gcc_assert (GET_CODE (insn) == NOTE);
12241   minipool_pad = 0;
12242
12243   /* Scan all the insns and record the operands that will need fixing.  */
12244   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
12245     {
12246       if (TARGET_CIRRUS_FIX_INVALID_INSNS
12247           && (arm_cirrus_insn_p (insn)
12248               || GET_CODE (insn) == JUMP_INSN
12249               || arm_memory_load_p (insn)))
12250         cirrus_reorg (insn);
12251
12252       if (GET_CODE (insn) == BARRIER)
12253         push_minipool_barrier (insn, address);
12254       else if (INSN_P (insn))
12255         {
12256           rtx table;
12257
12258           note_invalid_constants (insn, address, true);
12259           address += get_attr_length (insn);
12260
12261           /* If the insn is a vector jump, add the size of the table
12262              and skip the table.  */
12263           if ((table = is_jump_table (insn)) != NULL)
12264             {
12265               address += get_jump_table_size (table);
12266               insn = table;
12267             }
12268         }
12269     }
12270
12271   fix = minipool_fix_head;
12272
12273   /* Now scan the fixups and perform the required changes.  */
12274   while (fix)
12275     {
12276       Mfix * ftmp;
12277       Mfix * fdel;
12278       Mfix *  last_added_fix;
12279       Mfix * last_barrier = NULL;
12280       Mfix * this_fix;
12281
12282       /* Skip any further barriers before the next fix.  */
12283       while (fix && GET_CODE (fix->insn) == BARRIER)
12284         fix = fix->next;
12285
12286       /* No more fixes.  */
12287       if (fix == NULL)
12288         break;
12289
12290       last_added_fix = NULL;
12291
12292       for (ftmp = fix; ftmp; ftmp = ftmp->next)
12293         {
12294           if (GET_CODE (ftmp->insn) == BARRIER)
12295             {
12296               if (ftmp->address >= minipool_vector_head->max_address)
12297                 break;
12298
12299               last_barrier = ftmp;
12300             }
12301           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
12302             break;
12303
12304           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
12305         }
12306
12307       /* If we found a barrier, drop back to that; any fixes that we
12308          could have reached but come after the barrier will now go in
12309          the next mini-pool.  */
12310       if (last_barrier != NULL)
12311         {
12312           /* Reduce the refcount for those fixes that won't go into this
12313              pool after all.  */
12314           for (fdel = last_barrier->next;
12315                fdel && fdel != ftmp;
12316                fdel = fdel->next)
12317             {
12318               fdel->minipool->refcount--;
12319               fdel->minipool = NULL;
12320             }
12321
12322           ftmp = last_barrier;
12323         }
12324       else
12325         {
12326           /* ftmp is first fix that we can't fit into this pool and
12327              there no natural barriers that we could use.  Insert a
12328              new barrier in the code somewhere between the previous
12329              fix and this one, and arrange to jump around it.  */
12330           HOST_WIDE_INT max_address;
12331
12332           /* The last item on the list of fixes must be a barrier, so
12333              we can never run off the end of the list of fixes without
12334              last_barrier being set.  */
12335           gcc_assert (ftmp);
12336
12337           max_address = minipool_vector_head->max_address;
12338           /* Check that there isn't another fix that is in range that
12339              we couldn't fit into this pool because the pool was
12340              already too large: we need to put the pool before such an
12341              instruction.  The pool itself may come just after the
12342              fix because create_fix_barrier also allows space for a
12343              jump instruction.  */
12344           if (ftmp->address < max_address)
12345             max_address = ftmp->address + 1;
12346
12347           last_barrier = create_fix_barrier (last_added_fix, max_address);
12348         }
12349
12350       assign_minipool_offsets (last_barrier);
12351
12352       while (ftmp)
12353         {
12354           if (GET_CODE (ftmp->insn) != BARRIER
12355               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
12356                   == NULL))
12357             break;
12358
12359           ftmp = ftmp->next;
12360         }
12361
12362       /* Scan over the fixes we have identified for this pool, fixing them
12363          up and adding the constants to the pool itself.  */
12364       for (this_fix = fix; this_fix && ftmp != this_fix;
12365            this_fix = this_fix->next)
12366         if (GET_CODE (this_fix->insn) != BARRIER)
12367           {
12368             rtx addr
12369               = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
12370                                                   minipool_vector_label),
12371                                this_fix->minipool->offset);
12372             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
12373           }
12374
12375       dump_minipool (last_barrier->insn);
12376       fix = ftmp;
12377     }
12378
12379   /* From now on we must synthesize any constants that we can't handle
12380      directly.  This can happen if the RTL gets split during final
12381      instruction generation.  */
12382   after_arm_reorg = 1;
12383
12384   /* Free the minipool memory.  */
12385   obstack_free (&minipool_obstack, minipool_startobj);
12386 }
12387 \f
12388 /* Routines to output assembly language.  */
12389
12390 /* If the rtx is the correct value then return the string of the number.
12391    In this way we can ensure that valid double constants are generated even
12392    when cross compiling.  */
12393 const char *
12394 fp_immediate_constant (rtx x)
12395 {
12396   REAL_VALUE_TYPE r;
12397   int i;
12398
12399   if (!fp_consts_inited)
12400     init_fp_table ();
12401
12402   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
12403   for (i = 0; i < 8; i++)
12404     if (REAL_VALUES_EQUAL (r, values_fp[i]))
12405       return strings_fp[i];
12406
12407   gcc_unreachable ();
12408 }
12409
12410 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
12411 static const char *
12412 fp_const_from_val (REAL_VALUE_TYPE *r)
12413 {
12414   int i;
12415
12416   if (!fp_consts_inited)
12417     init_fp_table ();
12418
12419   for (i = 0; i < 8; i++)
12420     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
12421       return strings_fp[i];
12422
12423   gcc_unreachable ();
12424 }
12425
12426 /* Output the operands of a LDM/STM instruction to STREAM.
12427    MASK is the ARM register set mask of which only bits 0-15 are important.
12428    REG is the base register, either the frame pointer or the stack pointer,
12429    INSTR is the possibly suffixed load or store instruction.
12430    RFE is nonzero if the instruction should also copy spsr to cpsr.  */
12431
12432 static void
12433 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
12434                  unsigned long mask, int rfe)
12435 {
12436   unsigned i;
12437   bool not_first = FALSE;
12438
12439   gcc_assert (!rfe || (mask & (1 << PC_REGNUM)));
12440   fputc ('\t', stream);
12441   asm_fprintf (stream, instr, reg);
12442   fputc ('{', stream);
12443
12444   for (i = 0; i <= LAST_ARM_REGNUM; i++)
12445     if (mask & (1 << i))
12446       {
12447         if (not_first)
12448           fprintf (stream, ", ");
12449
12450         asm_fprintf (stream, "%r", i);
12451         not_first = TRUE;
12452       }
12453
12454   if (rfe)
12455     fprintf (stream, "}^\n");
12456   else
12457     fprintf (stream, "}\n");
12458 }
12459
12460
12461 /* Output a FLDMD instruction to STREAM.
12462    BASE if the register containing the address.
12463    REG and COUNT specify the register range.
12464    Extra registers may be added to avoid hardware bugs.
12465
12466    We output FLDMD even for ARMv5 VFP implementations.  Although
12467    FLDMD is technically not supported until ARMv6, it is believed
12468    that all VFP implementations support its use in this context.  */
12469
12470 static void
12471 vfp_output_fldmd (FILE * stream, unsigned int base, int reg, int count)
12472 {
12473   int i;
12474
12475   /* Workaround ARM10 VFPr1 bug.  */
12476   if (count == 2 && !arm_arch6)
12477     {
12478       if (reg == 15)
12479         reg--;
12480       count++;
12481     }
12482
12483   /* FLDMD may not load more than 16 doubleword registers at a time. Split the
12484      load into multiple parts if we have to handle more than 16 registers.  */
12485   if (count > 16)
12486     {
12487       vfp_output_fldmd (stream, base, reg, 16);
12488       vfp_output_fldmd (stream, base, reg + 16, count - 16);
12489       return;
12490     }
12491
12492   fputc ('\t', stream);
12493   asm_fprintf (stream, "fldmfdd\t%r!, {", base);
12494
12495   for (i = reg; i < reg + count; i++)
12496     {
12497       if (i > reg)
12498         fputs (", ", stream);
12499       asm_fprintf (stream, "d%d", i);
12500     }
12501   fputs ("}\n", stream);
12502
12503 }
12504
12505
12506 /* Output the assembly for a store multiple.  */
12507
12508 const char *
12509 vfp_output_fstmd (rtx * operands)
12510 {
12511   char pattern[100];
12512   int p;
12513   int base;
12514   int i;
12515
12516   strcpy (pattern, "fstmfdd\t%m0!, {%P1");
12517   p = strlen (pattern);
12518
12519   gcc_assert (GET_CODE (operands[1]) == REG);
12520
12521   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
12522   for (i = 1; i < XVECLEN (operands[2], 0); i++)
12523     {
12524       p += sprintf (&pattern[p], ", d%d", base + i);
12525     }
12526   strcpy (&pattern[p], "}");
12527
12528   output_asm_insn (pattern, operands);
12529   return "";
12530 }
12531
12532
12533 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
12534    number of bytes pushed.  */
12535
12536 static int
12537 vfp_emit_fstmd (int base_reg, int count)
12538 {
12539   rtx par;
12540   rtx dwarf;
12541   rtx tmp, reg;
12542   int i;
12543
12544   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
12545      register pairs are stored by a store multiple insn.  We avoid this
12546      by pushing an extra pair.  */
12547   if (count == 2 && !arm_arch6)
12548     {
12549       if (base_reg == LAST_VFP_REGNUM - 3)
12550         base_reg -= 2;
12551       count++;
12552     }
12553
12554   /* FSTMD may not store more than 16 doubleword registers at once.  Split
12555      larger stores into multiple parts (up to a maximum of two, in
12556      practice).  */
12557   if (count > 16)
12558     {
12559       int saved;
12560       /* NOTE: base_reg is an internal register number, so each D register
12561          counts as 2.  */
12562       saved = vfp_emit_fstmd (base_reg + 32, count - 16);
12563       saved += vfp_emit_fstmd (base_reg, 16);
12564       return saved;
12565     }
12566
12567   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
12568   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
12569
12570   reg = gen_rtx_REG (DFmode, base_reg);
12571   base_reg += 2;
12572
12573   XVECEXP (par, 0, 0)
12574     = gen_rtx_SET (VOIDmode,
12575                    gen_frame_mem
12576                    (BLKmode,
12577                     gen_rtx_PRE_MODIFY (Pmode,
12578                                         stack_pointer_rtx,
12579                                         plus_constant
12580                                         (stack_pointer_rtx,
12581                                          - (count * 8)))
12582                     ),
12583                    gen_rtx_UNSPEC (BLKmode,
12584                                    gen_rtvec (1, reg),
12585                                    UNSPEC_PUSH_MULT));
12586
12587   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
12588                      plus_constant (stack_pointer_rtx, -(count * 8)));
12589   RTX_FRAME_RELATED_P (tmp) = 1;
12590   XVECEXP (dwarf, 0, 0) = tmp;
12591
12592   tmp = gen_rtx_SET (VOIDmode,
12593                      gen_frame_mem (DFmode, stack_pointer_rtx),
12594                      reg);
12595   RTX_FRAME_RELATED_P (tmp) = 1;
12596   XVECEXP (dwarf, 0, 1) = tmp;
12597
12598   for (i = 1; i < count; i++)
12599     {
12600       reg = gen_rtx_REG (DFmode, base_reg);
12601       base_reg += 2;
12602       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
12603
12604       tmp = gen_rtx_SET (VOIDmode,
12605                          gen_frame_mem (DFmode,
12606                                         plus_constant (stack_pointer_rtx,
12607                                                        i * 8)),
12608                          reg);
12609       RTX_FRAME_RELATED_P (tmp) = 1;
12610       XVECEXP (dwarf, 0, i + 1) = tmp;
12611     }
12612
12613   par = emit_insn (par);
12614   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
12615   RTX_FRAME_RELATED_P (par) = 1;
12616
12617   return count * 8;
12618 }
12619
12620 /* Emit a call instruction with pattern PAT.  ADDR is the address of
12621    the call target.  */
12622
12623 void
12624 arm_emit_call_insn (rtx pat, rtx addr)
12625 {
12626   rtx insn;
12627
12628   insn = emit_call_insn (pat);
12629
12630   /* The PIC register is live on entry to VxWorks PIC PLT entries.
12631      If the call might use such an entry, add a use of the PIC register
12632      to the instruction's CALL_INSN_FUNCTION_USAGE.  */
12633   if (TARGET_VXWORKS_RTP
12634       && flag_pic
12635       && GET_CODE (addr) == SYMBOL_REF
12636       && (SYMBOL_REF_DECL (addr)
12637           ? !targetm.binds_local_p (SYMBOL_REF_DECL (addr))
12638           : !SYMBOL_REF_LOCAL_P (addr)))
12639     {
12640       require_pic_register ();
12641       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), cfun->machine->pic_reg);
12642     }
12643 }
12644
12645 /* Output a 'call' insn.  */
12646 const char *
12647 output_call (rtx *operands)
12648 {
12649   gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly.  */
12650
12651   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
12652   if (REGNO (operands[0]) == LR_REGNUM)
12653     {
12654       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
12655       output_asm_insn ("mov%?\t%0, %|lr", operands);
12656     }
12657
12658   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
12659
12660   if (TARGET_INTERWORK || arm_arch4t)
12661     output_asm_insn ("bx%?\t%0", operands);
12662   else
12663     output_asm_insn ("mov%?\t%|pc, %0", operands);
12664
12665   return "";
12666 }
12667
12668 /* Output a 'call' insn that is a reference in memory. This is
12669    disabled for ARMv5 and we prefer a blx instead because otherwise
12670    there's a significant performance overhead.  */
12671 const char *
12672 output_call_mem (rtx *operands)
12673 {
12674   gcc_assert (!arm_arch5);
12675   if (TARGET_INTERWORK)
12676     {
12677       output_asm_insn ("ldr%?\t%|ip, %0", operands);
12678       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
12679       output_asm_insn ("bx%?\t%|ip", operands);
12680     }
12681   else if (regno_use_in (LR_REGNUM, operands[0]))
12682     {
12683       /* LR is used in the memory address.  We load the address in the
12684          first instruction.  It's safe to use IP as the target of the
12685          load since the call will kill it anyway.  */
12686       output_asm_insn ("ldr%?\t%|ip, %0", operands);
12687       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
12688       if (arm_arch4t)
12689         output_asm_insn ("bx%?\t%|ip", operands);
12690       else
12691         output_asm_insn ("mov%?\t%|pc, %|ip", operands);
12692     }
12693   else
12694     {
12695       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
12696       output_asm_insn ("ldr%?\t%|pc, %0", operands);
12697     }
12698
12699   return "";
12700 }
12701
12702
12703 /* Output a move from arm registers to an fpa registers.
12704    OPERANDS[0] is an fpa register.
12705    OPERANDS[1] is the first registers of an arm register pair.  */
12706 const char *
12707 output_mov_long_double_fpa_from_arm (rtx *operands)
12708 {
12709   int arm_reg0 = REGNO (operands[1]);
12710   rtx ops[3];
12711
12712   gcc_assert (arm_reg0 != IP_REGNUM);
12713
12714   ops[0] = gen_rtx_REG (SImode, arm_reg0);
12715   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
12716   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
12717
12718   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
12719   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
12720
12721   return "";
12722 }
12723
12724 /* Output a move from an fpa register to arm registers.
12725    OPERANDS[0] is the first registers of an arm register pair.
12726    OPERANDS[1] is an fpa register.  */
12727 const char *
12728 output_mov_long_double_arm_from_fpa (rtx *operands)
12729 {
12730   int arm_reg0 = REGNO (operands[0]);
12731   rtx ops[3];
12732
12733   gcc_assert (arm_reg0 != IP_REGNUM);
12734
12735   ops[0] = gen_rtx_REG (SImode, arm_reg0);
12736   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
12737   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
12738
12739   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
12740   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
12741   return "";
12742 }
12743
12744 /* Output a move from arm registers to arm registers of a long double
12745    OPERANDS[0] is the destination.
12746    OPERANDS[1] is the source.  */
12747 const char *
12748 output_mov_long_double_arm_from_arm (rtx *operands)
12749 {
12750   /* We have to be careful here because the two might overlap.  */
12751   int dest_start = REGNO (operands[0]);
12752   int src_start = REGNO (operands[1]);
12753   rtx ops[2];
12754   int i;
12755
12756   if (dest_start < src_start)
12757     {
12758       for (i = 0; i < 3; i++)
12759         {
12760           ops[0] = gen_rtx_REG (SImode, dest_start + i);
12761           ops[1] = gen_rtx_REG (SImode, src_start + i);
12762           output_asm_insn ("mov%?\t%0, %1", ops);
12763         }
12764     }
12765   else
12766     {
12767       for (i = 2; i >= 0; i--)
12768         {
12769           ops[0] = gen_rtx_REG (SImode, dest_start + i);
12770           ops[1] = gen_rtx_REG (SImode, src_start + i);
12771           output_asm_insn ("mov%?\t%0, %1", ops);
12772         }
12773     }
12774
12775   return "";
12776 }
12777
12778 void
12779 arm_emit_movpair (rtx dest, rtx src)
12780  {
12781   /* If the src is an immediate, simplify it.  */
12782   if (CONST_INT_P (src))
12783     {
12784       HOST_WIDE_INT val = INTVAL (src);
12785       emit_set_insn (dest, GEN_INT (val & 0x0000ffff));
12786       if ((val >> 16) & 0x0000ffff)
12787         emit_set_insn (gen_rtx_ZERO_EXTRACT (SImode, dest, GEN_INT (16),
12788                                              GEN_INT (16)),
12789                        GEN_INT ((val >> 16) & 0x0000ffff));
12790       return;
12791     }
12792    emit_set_insn (dest, gen_rtx_HIGH (SImode, src));
12793    emit_set_insn (dest, gen_rtx_LO_SUM (SImode, dest, src));
12794  }
12795
12796 /* Output a move from arm registers to an fpa registers.
12797    OPERANDS[0] is an fpa register.
12798    OPERANDS[1] is the first registers of an arm register pair.  */
12799 const char *
12800 output_mov_double_fpa_from_arm (rtx *operands)
12801 {
12802   int arm_reg0 = REGNO (operands[1]);
12803   rtx ops[2];
12804
12805   gcc_assert (arm_reg0 != IP_REGNUM);
12806
12807   ops[0] = gen_rtx_REG (SImode, arm_reg0);
12808   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
12809   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1}", ops);
12810   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
12811   return "";
12812 }
12813
12814 /* Output a move from an fpa register to arm registers.
12815    OPERANDS[0] is the first registers of an arm register pair.
12816    OPERANDS[1] is an fpa register.  */
12817 const char *
12818 output_mov_double_arm_from_fpa (rtx *operands)
12819 {
12820   int arm_reg0 = REGNO (operands[0]);
12821   rtx ops[2];
12822
12823   gcc_assert (arm_reg0 != IP_REGNUM);
12824
12825   ops[0] = gen_rtx_REG (SImode, arm_reg0);
12826   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
12827   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
12828   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1}", ops);
12829   return "";
12830 }
12831
12832 /* Output a move between double words.
12833    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
12834    or MEM<-REG and all MEMs must be offsettable addresses.  */
12835 const char *
12836 output_move_double (rtx *operands)
12837 {
12838   enum rtx_code code0 = GET_CODE (operands[0]);
12839   enum rtx_code code1 = GET_CODE (operands[1]);
12840   rtx otherops[3];
12841
12842   if (code0 == REG)
12843     {
12844       unsigned int reg0 = REGNO (operands[0]);
12845
12846       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
12847
12848       gcc_assert (code1 == MEM);  /* Constraints should ensure this.  */
12849
12850       switch (GET_CODE (XEXP (operands[1], 0)))
12851         {
12852         case REG:
12853           if (TARGET_LDRD
12854               && !(fix_cm3_ldrd && reg0 == REGNO(XEXP (operands[1], 0))))
12855             output_asm_insn ("ldr%(d%)\t%0, [%m1]", operands);
12856           else
12857             output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
12858           break;
12859
12860         case PRE_INC:
12861           gcc_assert (TARGET_LDRD);
12862           output_asm_insn ("ldr%(d%)\t%0, [%m1, #8]!", operands);
12863           break;
12864
12865         case PRE_DEC:
12866           if (TARGET_LDRD)
12867             output_asm_insn ("ldr%(d%)\t%0, [%m1, #-8]!", operands);
12868           else
12869             output_asm_insn ("ldm%(db%)\t%m1!, %M0", operands);
12870           break;
12871
12872         case POST_INC:
12873           if (TARGET_LDRD)
12874             output_asm_insn ("ldr%(d%)\t%0, [%m1], #8", operands);
12875           else
12876             output_asm_insn ("ldm%(ia%)\t%m1!, %M0", operands);
12877           break;
12878
12879         case POST_DEC:
12880           gcc_assert (TARGET_LDRD);
12881           output_asm_insn ("ldr%(d%)\t%0, [%m1], #-8", operands);
12882           break;
12883
12884         case PRE_MODIFY:
12885         case POST_MODIFY:
12886           /* Autoicrement addressing modes should never have overlapping
12887              base and destination registers, and overlapping index registers
12888              are already prohibited, so this doesn't need to worry about
12889              fix_cm3_ldrd.  */
12890           otherops[0] = operands[0];
12891           otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
12892           otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
12893
12894           if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
12895             {
12896               if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
12897                 {
12898                   /* Registers overlap so split out the increment.  */
12899                   output_asm_insn ("add%?\t%1, %1, %2", otherops);
12900                   output_asm_insn ("ldr%(d%)\t%0, [%1] @split", otherops);
12901                 }
12902               else
12903                 {
12904                   /* Use a single insn if we can.
12905                      FIXME: IWMMXT allows offsets larger than ldrd can
12906                      handle, fix these up with a pair of ldr.  */
12907                   if (TARGET_THUMB2
12908                       || GET_CODE (otherops[2]) != CONST_INT
12909                       || (INTVAL (otherops[2]) > -256
12910                           && INTVAL (otherops[2]) < 256))
12911                     output_asm_insn ("ldr%(d%)\t%0, [%1, %2]!", otherops);
12912                   else
12913                     {
12914                       output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
12915                       output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
12916                     }
12917                 }
12918             }
12919           else
12920             {
12921               /* Use a single insn if we can.
12922                  FIXME: IWMMXT allows offsets larger than ldrd can handle,
12923                  fix these up with a pair of ldr.  */
12924               if (TARGET_THUMB2
12925                   || GET_CODE (otherops[2]) != CONST_INT
12926                   || (INTVAL (otherops[2]) > -256
12927                       && INTVAL (otherops[2]) < 256))
12928                 output_asm_insn ("ldr%(d%)\t%0, [%1], %2", otherops);
12929               else
12930                 {
12931                   output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
12932                   output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
12933                 }
12934             }
12935           break;
12936
12937         case LABEL_REF:
12938         case CONST:
12939           /* We might be able to use ldrd %0, %1 here.  However the range is
12940              different to ldr/adr, and it is broken on some ARMv7-M
12941              implementations.  */
12942           /* Use the second register of the pair to avoid problematic
12943              overlap.  */
12944           otherops[1] = operands[1];
12945           output_asm_insn ("adr%?\t%0, %1", otherops);
12946           operands[1] = otherops[0];
12947           if (TARGET_LDRD)
12948             output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
12949           else
12950             output_asm_insn ("ldm%(ia%)\t%1, %M0", operands);
12951           break;
12952
12953           /* ??? This needs checking for thumb2.  */
12954         default:
12955           if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
12956                                GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
12957             {
12958               otherops[0] = operands[0];
12959               otherops[1] = XEXP (XEXP (operands[1], 0), 0);
12960               otherops[2] = XEXP (XEXP (operands[1], 0), 1);
12961
12962               if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
12963                 {
12964                   if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
12965                     {
12966                       switch ((int) INTVAL (otherops[2]))
12967                         {
12968                         case -8:
12969                           output_asm_insn ("ldm%(db%)\t%1, %M0", otherops);
12970                           return "";
12971                         case -4:
12972                           if (TARGET_THUMB2)
12973                             break;
12974                           output_asm_insn ("ldm%(da%)\t%1, %M0", otherops);
12975                           return "";
12976                         case 4:
12977                           if (TARGET_THUMB2)
12978                             break;
12979                           output_asm_insn ("ldm%(ib%)\t%1, %M0", otherops);
12980                           return "";
12981                         }
12982                     }
12983                   otherops[0] = gen_rtx_REG(SImode, REGNO(operands[0]) + 1);
12984                   operands[1] = otherops[0];
12985                   if (TARGET_LDRD
12986                       && (GET_CODE (otherops[2]) == REG
12987                           || TARGET_THUMB2
12988                           || (GET_CODE (otherops[2]) == CONST_INT
12989                               && INTVAL (otherops[2]) > -256
12990                               && INTVAL (otherops[2]) < 256)))
12991                     {
12992                       if (reg_overlap_mentioned_p (operands[0],
12993                                                    otherops[2]))
12994                         {
12995                           rtx tmp;
12996                           /* Swap base and index registers over to
12997                              avoid a conflict.  */
12998                           tmp = otherops[1];
12999                           otherops[1] = otherops[2];
13000                           otherops[2] = tmp;
13001                         }
13002                       /* If both registers conflict, it will usually
13003                          have been fixed by a splitter.  */
13004                       if (reg_overlap_mentioned_p (operands[0], otherops[2])
13005                           || (fix_cm3_ldrd && reg0 == REGNO (otherops[1])))
13006                         {
13007                           output_asm_insn ("add%?\t%0, %1, %2", otherops);
13008                           output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
13009                         }
13010                       else
13011                         {
13012                           otherops[0] = operands[0];
13013                           output_asm_insn ("ldr%(d%)\t%0, [%1, %2]", otherops);
13014                         }
13015                       return "";
13016                     }
13017
13018                   if (GET_CODE (otherops[2]) == CONST_INT)
13019                     {
13020                       if (!(const_ok_for_arm (INTVAL (otherops[2]))))
13021                         output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
13022                       else
13023                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
13024                     }
13025                   else
13026                     output_asm_insn ("add%?\t%0, %1, %2", otherops);
13027                 }
13028               else
13029                 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
13030
13031               if (TARGET_LDRD)
13032                 return "ldr%(d%)\t%0, [%1]";
13033
13034               return "ldm%(ia%)\t%1, %M0";
13035             }
13036           else
13037             {
13038               otherops[1] = adjust_address (operands[1], SImode, 4);
13039               /* Take care of overlapping base/data reg.  */
13040               if (reg_mentioned_p (operands[0], operands[1]))
13041                 {
13042                   output_asm_insn ("ldr%?\t%0, %1", otherops);
13043                   output_asm_insn ("ldr%?\t%0, %1", operands);
13044                 }
13045               else
13046                 {
13047                   output_asm_insn ("ldr%?\t%0, %1", operands);
13048                   output_asm_insn ("ldr%?\t%0, %1", otherops);
13049                 }
13050             }
13051         }
13052     }
13053   else
13054     {
13055       /* Constraints should ensure this.  */
13056       gcc_assert (code0 == MEM && code1 == REG);
13057       gcc_assert (REGNO (operands[1]) != IP_REGNUM);
13058
13059       switch (GET_CODE (XEXP (operands[0], 0)))
13060         {
13061         case REG:
13062           if (TARGET_LDRD)
13063             output_asm_insn ("str%(d%)\t%1, [%m0]", operands);
13064           else
13065             output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
13066           break;
13067
13068         case PRE_INC:
13069           gcc_assert (TARGET_LDRD);
13070           output_asm_insn ("str%(d%)\t%1, [%m0, #8]!", operands);
13071           break;
13072
13073         case PRE_DEC:
13074           if (TARGET_LDRD)
13075             output_asm_insn ("str%(d%)\t%1, [%m0, #-8]!", operands);
13076           else
13077             output_asm_insn ("stm%(db%)\t%m0!, %M1", operands);
13078           break;
13079
13080         case POST_INC:
13081           if (TARGET_LDRD)
13082             output_asm_insn ("str%(d%)\t%1, [%m0], #8", operands);
13083           else
13084             output_asm_insn ("stm%(ia%)\t%m0!, %M1", operands);
13085           break;
13086
13087         case POST_DEC:
13088           gcc_assert (TARGET_LDRD);
13089           output_asm_insn ("str%(d%)\t%1, [%m0], #-8", operands);
13090           break;
13091
13092         case PRE_MODIFY:
13093         case POST_MODIFY:
13094           otherops[0] = operands[1];
13095           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
13096           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
13097
13098           /* IWMMXT allows offsets larger than ldrd can handle,
13099              fix these up with a pair of ldr.  */
13100           if (!TARGET_THUMB2
13101               && GET_CODE (otherops[2]) == CONST_INT
13102               && (INTVAL(otherops[2]) <= -256
13103                   || INTVAL(otherops[2]) >= 256))
13104             {
13105               if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
13106                 {
13107                   output_asm_insn ("str%?\t%0, [%1, %2]!", otherops);
13108                   output_asm_insn ("str%?\t%H0, [%1, #4]", otherops);
13109                 }
13110               else
13111                 {
13112                   output_asm_insn ("str%?\t%H0, [%1, #4]", otherops);
13113                   output_asm_insn ("str%?\t%0, [%1], %2", otherops);
13114                 }
13115             }
13116           else if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
13117             output_asm_insn ("str%(d%)\t%0, [%1, %2]!", otherops);
13118           else
13119             output_asm_insn ("str%(d%)\t%0, [%1], %2", otherops);
13120           break;
13121
13122         case PLUS:
13123           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
13124           if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
13125             {
13126               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
13127                 {
13128                 case -8:
13129                   output_asm_insn ("stm%(db%)\t%m0, %M1", operands);
13130                   return "";
13131
13132                 case -4:
13133                   if (TARGET_THUMB2)
13134                     break;
13135                   output_asm_insn ("stm%(da%)\t%m0, %M1", operands);
13136                   return "";
13137
13138                 case 4:
13139                   if (TARGET_THUMB2)
13140                     break;
13141                   output_asm_insn ("stm%(ib%)\t%m0, %M1", operands);
13142                   return "";
13143                 }
13144             }
13145           if (TARGET_LDRD
13146               && (GET_CODE (otherops[2]) == REG
13147                   || TARGET_THUMB2
13148                   || (GET_CODE (otherops[2]) == CONST_INT
13149                       && INTVAL (otherops[2]) > -256
13150                       && INTVAL (otherops[2]) < 256)))
13151             {
13152               otherops[0] = operands[1];
13153               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
13154               output_asm_insn ("str%(d%)\t%0, [%1, %2]", otherops);
13155               return "";
13156             }
13157           /* Fall through */
13158
13159         default:
13160           otherops[0] = adjust_address (operands[0], SImode, 4);
13161           otherops[1] = operands[1];
13162           output_asm_insn ("str%?\t%1, %0", operands);
13163           output_asm_insn ("str%?\t%H1, %0", otherops);
13164         }
13165     }
13166
13167   return "";
13168 }
13169
13170 /* Output a move, load or store for quad-word vectors in ARM registers.  Only
13171    handles MEMs accepted by neon_vector_mem_operand with TYPE=1.  */
13172
13173 const char *
13174 output_move_quad (rtx *operands)
13175 {
13176   if (REG_P (operands[0]))
13177     {
13178       /* Load, or reg->reg move.  */
13179
13180       if (MEM_P (operands[1]))
13181         {
13182           switch (GET_CODE (XEXP (operands[1], 0)))
13183             {
13184             case REG:
13185               output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
13186               break;
13187
13188             case LABEL_REF:
13189             case CONST:
13190               output_asm_insn ("adr%?\t%0, %1", operands);
13191               output_asm_insn ("ldm%(ia%)\t%0, %M0", operands);
13192               break;
13193
13194             default:
13195               gcc_unreachable ();
13196             }
13197         }
13198       else
13199         {
13200           rtx ops[2];
13201           int dest, src, i;
13202
13203           gcc_assert (REG_P (operands[1]));
13204
13205           dest = REGNO (operands[0]);
13206           src = REGNO (operands[1]);
13207
13208           /* This seems pretty dumb, but hopefully GCC won't try to do it
13209              very often.  */
13210           if (dest < src)
13211             for (i = 0; i < 4; i++)
13212               {
13213                 ops[0] = gen_rtx_REG (SImode, dest + i);
13214                 ops[1] = gen_rtx_REG (SImode, src + i);
13215                 output_asm_insn ("mov%?\t%0, %1", ops);
13216               }
13217           else
13218             for (i = 3; i >= 0; i--)
13219               {
13220                 ops[0] = gen_rtx_REG (SImode, dest + i);
13221                 ops[1] = gen_rtx_REG (SImode, src + i);
13222                 output_asm_insn ("mov%?\t%0, %1", ops);
13223               }
13224         }
13225     }
13226   else
13227     {
13228       gcc_assert (MEM_P (operands[0]));
13229       gcc_assert (REG_P (operands[1]));
13230       gcc_assert (!reg_overlap_mentioned_p (operands[1], operands[0]));
13231
13232       switch (GET_CODE (XEXP (operands[0], 0)))
13233         {
13234         case REG:
13235           output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
13236           break;
13237
13238         default:
13239           gcc_unreachable ();
13240         }
13241     }
13242
13243   return "";
13244 }
13245
13246 /* Output a VFP load or store instruction.  */
13247
13248 const char *
13249 output_move_vfp (rtx *operands)
13250 {
13251   rtx reg, mem, addr, ops[2];
13252   int load = REG_P (operands[0]);
13253   int dp = GET_MODE_SIZE (GET_MODE (operands[0])) == 8;
13254   int integer_p = GET_MODE_CLASS (GET_MODE (operands[0])) == MODE_INT;
13255   const char *templ;
13256   char buff[50];
13257   enum machine_mode mode;
13258
13259   reg = operands[!load];
13260   mem = operands[load];
13261
13262   mode = GET_MODE (reg);
13263
13264   gcc_assert (REG_P (reg));
13265   gcc_assert (IS_VFP_REGNUM (REGNO (reg)));
13266   gcc_assert (mode == SFmode
13267               || mode == DFmode
13268               || mode == SImode
13269               || mode == DImode
13270               || (TARGET_NEON && VALID_NEON_DREG_MODE (mode)));
13271   gcc_assert (MEM_P (mem));
13272
13273   addr = XEXP (mem, 0);
13274
13275   switch (GET_CODE (addr))
13276     {
13277     case PRE_DEC:
13278       templ = "f%smdb%c%%?\t%%0!, {%%%s1}%s";
13279       ops[0] = XEXP (addr, 0);
13280       ops[1] = reg;
13281       break;
13282
13283     case POST_INC:
13284       templ = "f%smia%c%%?\t%%0!, {%%%s1}%s";
13285       ops[0] = XEXP (addr, 0);
13286       ops[1] = reg;
13287       break;
13288
13289     default:
13290       templ = "f%s%c%%?\t%%%s0, %%1%s";
13291       ops[0] = reg;
13292       ops[1] = mem;
13293       break;
13294     }
13295
13296   sprintf (buff, templ,
13297            load ? "ld" : "st",
13298            dp ? 'd' : 's',
13299            dp ? "P" : "",
13300            integer_p ? "\t%@ int" : "");
13301   output_asm_insn (buff, ops);
13302
13303   return "";
13304 }
13305
13306 /* Output a Neon quad-word load or store, or a load or store for
13307    larger structure modes.
13308
13309    WARNING: The ordering of elements is weird in big-endian mode,
13310    because we use VSTM, as required by the EABI.  GCC RTL defines
13311    element ordering based on in-memory order.  This can be differ
13312    from the architectural ordering of elements within a NEON register.
13313    The intrinsics defined in arm_neon.h use the NEON register element
13314    ordering, not the GCC RTL element ordering.
13315
13316    For example, the in-memory ordering of a big-endian a quadword
13317    vector with 16-bit elements when stored from register pair {d0,d1}
13318    will be (lowest address first, d0[N] is NEON register element N):
13319
13320      [d0[3], d0[2], d0[1], d0[0], d1[7], d1[6], d1[5], d1[4]]
13321
13322    When necessary, quadword registers (dN, dN+1) are moved to ARM
13323    registers from rN in the order:
13324
13325      dN -> (rN+1, rN), dN+1 -> (rN+3, rN+2)
13326
13327    So that STM/LDM can be used on vectors in ARM registers, and the
13328    same memory layout will result as if VSTM/VLDM were used.  */
13329
13330 const char *
13331 output_move_neon (rtx *operands)
13332 {
13333   rtx reg, mem, addr, ops[2];
13334   int regno, load = REG_P (operands[0]);
13335   const char *templ;
13336   char buff[50];
13337   enum machine_mode mode;
13338
13339   reg = operands[!load];
13340   mem = operands[load];
13341
13342   mode = GET_MODE (reg);
13343
13344   gcc_assert (REG_P (reg));
13345   regno = REGNO (reg);
13346   gcc_assert (VFP_REGNO_OK_FOR_DOUBLE (regno)
13347               || NEON_REGNO_OK_FOR_QUAD (regno));
13348   gcc_assert (VALID_NEON_DREG_MODE (mode)
13349               || VALID_NEON_QREG_MODE (mode)
13350               || VALID_NEON_STRUCT_MODE (mode));
13351   gcc_assert (MEM_P (mem));
13352
13353   addr = XEXP (mem, 0);
13354
13355   /* Strip off const from addresses like (const (plus (...))).  */
13356   if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
13357     addr = XEXP (addr, 0);
13358
13359   switch (GET_CODE (addr))
13360     {
13361     case POST_INC:
13362       templ = "v%smia%%?\t%%0!, %%h1";
13363       ops[0] = XEXP (addr, 0);
13364       ops[1] = reg;
13365       break;
13366
13367     case PRE_DEC:
13368       /* FIXME: We should be using vld1/vst1 here in BE mode?  */
13369       templ = "v%smdb%%?\t%%0!, %%h1";
13370       ops[0] = XEXP (addr, 0);
13371       ops[1] = reg;
13372       break;
13373     
13374     case POST_MODIFY:
13375       /* FIXME: Not currently enabled in neon_vector_mem_operand.  */
13376       gcc_unreachable ();
13377
13378     case LABEL_REF:
13379     case PLUS:
13380       {
13381         int nregs = HARD_REGNO_NREGS (REGNO (reg), mode) / 2;
13382         int i;
13383         int overlap = -1;
13384         for (i = 0; i < nregs; i++)
13385           {
13386             /* We're only using DImode here because it's a convenient size.  */
13387             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * i);
13388             ops[1] = adjust_address (mem, DImode, 8 * i);
13389             if (reg_overlap_mentioned_p (ops[0], mem))
13390               {
13391                 gcc_assert (overlap == -1);
13392                 overlap = i;
13393               }
13394             else
13395               {
13396                 sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
13397                 output_asm_insn (buff, ops);
13398               }
13399           }
13400         if (overlap != -1)
13401           {
13402             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * overlap);
13403             ops[1] = adjust_address (mem, SImode, 8 * overlap);
13404             sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
13405             output_asm_insn (buff, ops);
13406           }
13407
13408         return "";
13409       }
13410
13411     default:
13412       templ = "v%smia%%?\t%%m0, %%h1";
13413       ops[0] = mem;
13414       ops[1] = reg;
13415     }
13416
13417   sprintf (buff, templ, load ? "ld" : "st");
13418   output_asm_insn (buff, ops);
13419
13420   return "";
13421 }
13422
13423 /* Compute and return the length of neon_mov<mode>, where <mode> is
13424    one of VSTRUCT modes: EI, OI, CI or XI.  */
13425 int
13426 arm_attr_length_move_neon (rtx insn)
13427 {
13428   rtx reg, mem, addr;
13429   int load;
13430   enum machine_mode mode;
13431
13432   extract_insn_cached (insn);
13433
13434   if (REG_P (recog_data.operand[0]) && REG_P (recog_data.operand[1]))
13435     {
13436       mode = GET_MODE (recog_data.operand[0]);
13437       switch (mode)
13438         {
13439         case EImode:
13440         case OImode:
13441           return 8;
13442         case CImode:
13443           return 12;
13444         case XImode:
13445           return 16;
13446         default:
13447           gcc_unreachable ();
13448         }
13449     }
13450
13451   load = REG_P (recog_data.operand[0]);
13452   reg = recog_data.operand[!load];
13453   mem = recog_data.operand[load];
13454
13455   gcc_assert (MEM_P (mem));
13456
13457   mode = GET_MODE (reg);
13458   addr = XEXP (mem, 0);
13459
13460   /* Strip off const from addresses like (const (plus (...))).  */
13461   if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
13462     addr = XEXP (addr, 0);
13463
13464   if (GET_CODE (addr) == LABEL_REF || GET_CODE (addr) == PLUS)
13465     {
13466       int insns = HARD_REGNO_NREGS (REGNO (reg), mode) / 2;
13467       return insns * 4;
13468     }
13469   else
13470     return 4;
13471 }
13472
13473 /* Return nonzero if the offset in the address is an immediate.  Otherwise,
13474    return zero.  */
13475
13476 int
13477 arm_address_offset_is_imm (rtx insn)
13478 {
13479   rtx mem, addr;
13480
13481   extract_insn_cached (insn);
13482
13483   if (REG_P (recog_data.operand[0]))
13484     return 0;
13485
13486   mem = recog_data.operand[0];
13487
13488   gcc_assert (MEM_P (mem));
13489
13490   addr = XEXP (mem, 0);
13491
13492   if (GET_CODE (addr) == REG
13493       || (GET_CODE (addr) == PLUS
13494           && GET_CODE (XEXP (addr, 0)) == REG
13495           && GET_CODE (XEXP (addr, 1)) == CONST_INT))
13496     return 1;
13497   else
13498     return 0;
13499 }
13500
13501 /* Output an ADD r, s, #n where n may be too big for one instruction.
13502    If adding zero to one register, output nothing.  */
13503 const char *
13504 output_add_immediate (rtx *operands)
13505 {
13506   HOST_WIDE_INT n = INTVAL (operands[2]);
13507
13508   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
13509     {
13510       if (n < 0)
13511         output_multi_immediate (operands,
13512                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
13513                                 -n);
13514       else
13515         output_multi_immediate (operands,
13516                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
13517                                 n);
13518     }
13519
13520   return "";
13521 }
13522
13523 /* Output a multiple immediate operation.
13524    OPERANDS is the vector of operands referred to in the output patterns.
13525    INSTR1 is the output pattern to use for the first constant.
13526    INSTR2 is the output pattern to use for subsequent constants.
13527    IMMED_OP is the index of the constant slot in OPERANDS.
13528    N is the constant value.  */
13529 static const char *
13530 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
13531                         int immed_op, HOST_WIDE_INT n)
13532 {
13533 #if HOST_BITS_PER_WIDE_INT > 32
13534   n &= 0xffffffff;
13535 #endif
13536
13537   if (n == 0)
13538     {
13539       /* Quick and easy output.  */
13540       operands[immed_op] = const0_rtx;
13541       output_asm_insn (instr1, operands);
13542     }
13543   else
13544     {
13545       int i;
13546       const char * instr = instr1;
13547
13548       /* Note that n is never zero here (which would give no output).  */
13549       for (i = 0; i < 32; i += 2)
13550         {
13551           if (n & (3 << i))
13552             {
13553               operands[immed_op] = GEN_INT (n & (255 << i));
13554               output_asm_insn (instr, operands);
13555               instr = instr2;
13556               i += 6;
13557             }
13558         }
13559     }
13560
13561   return "";
13562 }
13563
13564 /* Return the name of a shifter operation.  */
13565 static const char *
13566 arm_shift_nmem(enum rtx_code code)
13567 {
13568   switch (code)
13569     {
13570     case ASHIFT:
13571       return ARM_LSL_NAME;
13572
13573     case ASHIFTRT:
13574       return "asr";
13575
13576     case LSHIFTRT:
13577       return "lsr";
13578
13579     case ROTATERT:
13580       return "ror";
13581
13582     default:
13583       abort();
13584     }
13585 }
13586
13587 /* Return the appropriate ARM instruction for the operation code.
13588    The returned result should not be overwritten.  OP is the rtx of the
13589    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
13590    was shifted.  */
13591 const char *
13592 arithmetic_instr (rtx op, int shift_first_arg)
13593 {
13594   switch (GET_CODE (op))
13595     {
13596     case PLUS:
13597       return "add";
13598
13599     case MINUS:
13600       return shift_first_arg ? "rsb" : "sub";
13601
13602     case IOR:
13603       return "orr";
13604
13605     case XOR:
13606       return "eor";
13607
13608     case AND:
13609       return "and";
13610
13611     case ASHIFT:
13612     case ASHIFTRT:
13613     case LSHIFTRT:
13614     case ROTATERT:
13615       return arm_shift_nmem(GET_CODE(op));
13616
13617     default:
13618       gcc_unreachable ();
13619     }
13620 }
13621
13622 /* Ensure valid constant shifts and return the appropriate shift mnemonic
13623    for the operation code.  The returned result should not be overwritten.
13624    OP is the rtx code of the shift.
13625    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
13626    shift.  */
13627 static const char *
13628 shift_op (rtx op, HOST_WIDE_INT *amountp)
13629 {
13630   const char * mnem;
13631   enum rtx_code code = GET_CODE (op);
13632
13633   switch (GET_CODE (XEXP (op, 1)))
13634     {
13635     case REG:
13636     case SUBREG:
13637       *amountp = -1;
13638       break;
13639
13640     case CONST_INT:
13641       *amountp = INTVAL (XEXP (op, 1));
13642       break;
13643
13644     default:
13645       gcc_unreachable ();
13646     }
13647
13648   switch (code)
13649     {
13650     case ROTATE:
13651       gcc_assert (*amountp != -1);
13652       *amountp = 32 - *amountp;
13653       code = ROTATERT;
13654
13655       /* Fall through.  */
13656
13657     case ASHIFT:
13658     case ASHIFTRT:
13659     case LSHIFTRT:
13660     case ROTATERT:
13661       mnem = arm_shift_nmem(code);
13662       break;
13663
13664     case MULT:
13665       /* We never have to worry about the amount being other than a
13666          power of 2, since this case can never be reloaded from a reg.  */
13667       gcc_assert (*amountp != -1);
13668       *amountp = int_log2 (*amountp);
13669       return ARM_LSL_NAME;
13670
13671     default:
13672       gcc_unreachable ();
13673     }
13674
13675   if (*amountp != -1)
13676     {
13677       /* This is not 100% correct, but follows from the desire to merge
13678          multiplication by a power of 2 with the recognizer for a
13679          shift.  >=32 is not a valid shift for "lsl", so we must try and
13680          output a shift that produces the correct arithmetical result.
13681          Using lsr #32 is identical except for the fact that the carry bit
13682          is not set correctly if we set the flags; but we never use the
13683          carry bit from such an operation, so we can ignore that.  */
13684       if (code == ROTATERT)
13685         /* Rotate is just modulo 32.  */
13686         *amountp &= 31;
13687       else if (*amountp != (*amountp & 31))
13688         {
13689           if (code == ASHIFT)
13690             mnem = "lsr";
13691           *amountp = 32;
13692         }
13693
13694       /* Shifts of 0 are no-ops.  */
13695       if (*amountp == 0)
13696         return NULL;
13697     }
13698
13699   return mnem;
13700 }
13701
13702 /* Obtain the shift from the POWER of two.  */
13703
13704 static HOST_WIDE_INT
13705 int_log2 (HOST_WIDE_INT power)
13706 {
13707   HOST_WIDE_INT shift = 0;
13708
13709   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
13710     {
13711       gcc_assert (shift <= 31);
13712       shift++;
13713     }
13714
13715   return shift;
13716 }
13717
13718 /* Output a .ascii pseudo-op, keeping track of lengths.  This is
13719    because /bin/as is horribly restrictive.  The judgement about
13720    whether or not each character is 'printable' (and can be output as
13721    is) or not (and must be printed with an octal escape) must be made
13722    with reference to the *host* character set -- the situation is
13723    similar to that discussed in the comments above pp_c_char in
13724    c-pretty-print.c.  */
13725
13726 #define MAX_ASCII_LEN 51
13727
13728 void
13729 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
13730 {
13731   int i;
13732   int len_so_far = 0;
13733
13734   fputs ("\t.ascii\t\"", stream);
13735
13736   for (i = 0; i < len; i++)
13737     {
13738       int c = p[i];
13739
13740       if (len_so_far >= MAX_ASCII_LEN)
13741         {
13742           fputs ("\"\n\t.ascii\t\"", stream);
13743           len_so_far = 0;
13744         }
13745
13746       if (ISPRINT (c))
13747         {
13748           if (c == '\\' || c == '\"')
13749             {
13750               putc ('\\', stream);
13751               len_so_far++;
13752             }
13753           putc (c, stream);
13754           len_so_far++;
13755         }
13756       else
13757         {
13758           fprintf (stream, "\\%03o", c);
13759           len_so_far += 4;
13760         }
13761     }
13762
13763   fputs ("\"\n", stream);
13764 }
13765 \f
13766 /* Compute the register save mask for registers 0 through 12
13767    inclusive.  This code is used by arm_compute_save_reg_mask.  */
13768
13769 static unsigned long
13770 arm_compute_save_reg0_reg12_mask (void)
13771 {
13772   unsigned long func_type = arm_current_func_type ();
13773   unsigned long save_reg_mask = 0;
13774   unsigned int reg;
13775
13776   if (IS_INTERRUPT (func_type))
13777     {
13778       unsigned int max_reg;
13779       /* Interrupt functions must not corrupt any registers,
13780          even call clobbered ones.  If this is a leaf function
13781          we can just examine the registers used by the RTL, but
13782          otherwise we have to assume that whatever function is
13783          called might clobber anything, and so we have to save
13784          all the call-clobbered registers as well.  */
13785       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
13786         /* FIQ handlers have registers r8 - r12 banked, so
13787            we only need to check r0 - r7, Normal ISRs only
13788            bank r14 and r15, so we must check up to r12.
13789            r13 is the stack pointer which is always preserved,
13790            so we do not need to consider it here.  */
13791         max_reg = 7;
13792       else
13793         max_reg = 12;
13794
13795       for (reg = 0; reg <= max_reg; reg++)
13796         if (df_regs_ever_live_p (reg)
13797             || (! current_function_is_leaf && call_used_regs[reg]))
13798           save_reg_mask |= (1 << reg);
13799
13800       /* Also save the pic base register if necessary.  */
13801       if (flag_pic
13802           && !TARGET_SINGLE_PIC_BASE
13803           && arm_pic_register != INVALID_REGNUM
13804           && crtl->uses_pic_offset_table)
13805         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
13806     }
13807   else if (IS_VOLATILE(func_type))
13808     {
13809       /* For noreturn functions we historically omitted register saves
13810          altogether.  However this really messes up debugging.  As a
13811          compromise save just the frame pointers.  Combined with the link
13812          register saved elsewhere this should be sufficient to get
13813          a backtrace.  */
13814       if (frame_pointer_needed)
13815         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
13816       if (df_regs_ever_live_p (ARM_HARD_FRAME_POINTER_REGNUM))
13817         save_reg_mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
13818       if (df_regs_ever_live_p (THUMB_HARD_FRAME_POINTER_REGNUM))
13819         save_reg_mask |= 1 << THUMB_HARD_FRAME_POINTER_REGNUM;
13820     }
13821   else
13822     {
13823       /* In the normal case we only need to save those registers
13824          which are call saved and which are used by this function.  */
13825       for (reg = 0; reg <= 11; reg++)
13826         if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
13827           save_reg_mask |= (1 << reg);
13828
13829       /* Handle the frame pointer as a special case.  */
13830       if (frame_pointer_needed)
13831         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
13832
13833       /* If we aren't loading the PIC register,
13834          don't stack it even though it may be live.  */
13835       if (flag_pic
13836           && !TARGET_SINGLE_PIC_BASE
13837           && arm_pic_register != INVALID_REGNUM
13838           && (df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM)
13839               || crtl->uses_pic_offset_table))
13840         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
13841
13842       /* The prologue will copy SP into R0, so save it.  */
13843       if (IS_STACKALIGN (func_type))
13844         save_reg_mask |= 1;
13845     }
13846
13847   /* Save registers so the exception handler can modify them.  */
13848   if (crtl->calls_eh_return)
13849     {
13850       unsigned int i;
13851
13852       for (i = 0; ; i++)
13853         {
13854           reg = EH_RETURN_DATA_REGNO (i);
13855           if (reg == INVALID_REGNUM)
13856             break;
13857           save_reg_mask |= 1 << reg;
13858         }
13859     }
13860
13861   return save_reg_mask;
13862 }
13863
13864
13865 /* Compute the number of bytes used to store the static chain register on the 
13866    stack, above the stack frame. We need to know this accurately to get the
13867    alignment of the rest of the stack frame correct. */
13868
13869 static int arm_compute_static_chain_stack_bytes (void)
13870 {
13871   unsigned long func_type = arm_current_func_type ();
13872   int static_chain_stack_bytes = 0;
13873
13874   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM &&
13875       IS_NESTED (func_type) &&
13876       df_regs_ever_live_p (3) && crtl->args.pretend_args_size == 0)
13877     static_chain_stack_bytes = 4;
13878
13879   return static_chain_stack_bytes;
13880 }
13881
13882
13883 /* Compute a bit mask of which registers need to be
13884    saved on the stack for the current function.
13885    This is used by arm_get_frame_offsets, which may add extra registers.  */
13886
13887 static unsigned long
13888 arm_compute_save_reg_mask (void)
13889 {
13890   unsigned int save_reg_mask = 0;
13891   unsigned long func_type = arm_current_func_type ();
13892   unsigned int reg;
13893
13894   if (IS_NAKED (func_type))
13895     /* This should never really happen.  */
13896     return 0;
13897
13898   /* If we are creating a stack frame, then we must save the frame pointer,
13899      IP (which will hold the old stack pointer), LR and the PC.  */
13900   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
13901     save_reg_mask |=
13902       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
13903       | (1 << IP_REGNUM)
13904       | (1 << LR_REGNUM)
13905       | (1 << PC_REGNUM);
13906
13907   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
13908
13909   /* Decide if we need to save the link register.
13910      Interrupt routines have their own banked link register,
13911      so they never need to save it.
13912      Otherwise if we do not use the link register we do not need to save
13913      it.  If we are pushing other registers onto the stack however, we
13914      can save an instruction in the epilogue by pushing the link register
13915      now and then popping it back into the PC.  This incurs extra memory
13916      accesses though, so we only do it when optimizing for size, and only
13917      if we know that we will not need a fancy return sequence.  */
13918   if (df_regs_ever_live_p (LR_REGNUM)
13919       || (save_reg_mask
13920           && optimize_size
13921           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
13922           && !crtl->calls_eh_return))
13923     save_reg_mask |= 1 << LR_REGNUM;
13924
13925   if (cfun->machine->lr_save_eliminated)
13926     save_reg_mask &= ~ (1 << LR_REGNUM);
13927
13928   if (TARGET_REALLY_IWMMXT
13929       && ((bit_count (save_reg_mask)
13930            + ARM_NUM_INTS (crtl->args.pretend_args_size +
13931                            arm_compute_static_chain_stack_bytes())
13932            ) % 2) != 0)
13933     {
13934       /* The total number of registers that are going to be pushed
13935          onto the stack is odd.  We need to ensure that the stack
13936          is 64-bit aligned before we start to save iWMMXt registers,
13937          and also before we start to create locals.  (A local variable
13938          might be a double or long long which we will load/store using
13939          an iWMMXt instruction).  Therefore we need to push another
13940          ARM register, so that the stack will be 64-bit aligned.  We
13941          try to avoid using the arg registers (r0 -r3) as they might be
13942          used to pass values in a tail call.  */
13943       for (reg = 4; reg <= 12; reg++)
13944         if ((save_reg_mask & (1 << reg)) == 0)
13945           break;
13946
13947       if (reg <= 12)
13948         save_reg_mask |= (1 << reg);
13949       else
13950         {
13951           cfun->machine->sibcall_blocked = 1;
13952           save_reg_mask |= (1 << 3);
13953         }
13954     }
13955
13956   /* We may need to push an additional register for use initializing the
13957      PIC base register.  */
13958   if (TARGET_THUMB2 && IS_NESTED (func_type) && flag_pic
13959       && (save_reg_mask & THUMB2_WORK_REGS) == 0)
13960     {
13961       reg = thumb_find_work_register (1 << 4);
13962       if (!call_used_regs[reg])
13963         save_reg_mask |= (1 << reg);
13964     }
13965
13966   return save_reg_mask;
13967 }
13968
13969
13970 /* Compute a bit mask of which registers need to be
13971    saved on the stack for the current function.  */
13972 static unsigned long
13973 thumb1_compute_save_reg_mask (void)
13974 {
13975   unsigned long mask;
13976   unsigned reg;
13977
13978   mask = 0;
13979   for (reg = 0; reg < 12; reg ++)
13980     if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
13981       mask |= 1 << reg;
13982
13983   if (flag_pic
13984       && !TARGET_SINGLE_PIC_BASE
13985       && arm_pic_register != INVALID_REGNUM
13986       && crtl->uses_pic_offset_table)
13987     mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
13988
13989   /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
13990   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
13991     mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
13992
13993   /* LR will also be pushed if any lo regs are pushed.  */
13994   if (mask & 0xff || thumb_force_lr_save ())
13995     mask |= (1 << LR_REGNUM);
13996
13997   /* Make sure we have a low work register if we need one.
13998      We will need one if we are going to push a high register,
13999      but we are not currently intending to push a low register.  */
14000   if ((mask & 0xff) == 0
14001       && ((mask & 0x0f00) || TARGET_BACKTRACE))
14002     {
14003       /* Use thumb_find_work_register to choose which register
14004          we will use.  If the register is live then we will
14005          have to push it.  Use LAST_LO_REGNUM as our fallback
14006          choice for the register to select.  */
14007       reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
14008       /* Make sure the register returned by thumb_find_work_register is
14009          not part of the return value.  */
14010       if (reg * UNITS_PER_WORD <= (unsigned) arm_size_return_regs ())
14011         reg = LAST_LO_REGNUM;
14012
14013       if (! call_used_regs[reg])
14014         mask |= 1 << reg;
14015     }
14016
14017   /* The 504 below is 8 bytes less than 512 because there are two possible
14018      alignment words.  We can't tell here if they will be present or not so we
14019      have to play it safe and assume that they are. */
14020   if ((CALLER_INTERWORKING_SLOT_SIZE +
14021        ROUND_UP_WORD (get_frame_size ()) +
14022        crtl->outgoing_args_size) >= 504)
14023     {
14024       /* This is the same as the code in thumb1_expand_prologue() which
14025          determines which register to use for stack decrement. */
14026       for (reg = LAST_ARG_REGNUM + 1; reg <= LAST_LO_REGNUM; reg++)
14027         if (mask & (1 << reg))
14028           break;
14029
14030       if (reg > LAST_LO_REGNUM)
14031         {
14032           /* Make sure we have a register available for stack decrement. */
14033           mask |= 1 << LAST_LO_REGNUM;
14034         }
14035     }
14036
14037   return mask;
14038 }
14039
14040
14041 /* Return the number of bytes required to save VFP registers.  */
14042 static int
14043 arm_get_vfp_saved_size (void)
14044 {
14045   unsigned int regno;
14046   int count;
14047   int saved;
14048
14049   saved = 0;
14050   /* Space for saved VFP registers.  */
14051   if (TARGET_HARD_FLOAT && TARGET_VFP)
14052     {
14053       count = 0;
14054       for (regno = FIRST_VFP_REGNUM;
14055            regno < LAST_VFP_REGNUM;
14056            regno += 2)
14057         {
14058           if ((!df_regs_ever_live_p (regno) || call_used_regs[regno])
14059               && (!df_regs_ever_live_p (regno + 1) || call_used_regs[regno + 1]))
14060             {
14061               if (count > 0)
14062                 {
14063                   /* Workaround ARM10 VFPr1 bug.  */
14064                   if (count == 2 && !arm_arch6)
14065                     count++;
14066                   saved += count * 8;
14067                 }
14068               count = 0;
14069             }
14070           else
14071             count++;
14072         }
14073       if (count > 0)
14074         {
14075           if (count == 2 && !arm_arch6)
14076             count++;
14077           saved += count * 8;
14078         }
14079     }
14080   return saved;
14081 }
14082
14083
14084 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
14085    everything bar the final return instruction.  */
14086 const char *
14087 output_return_instruction (rtx operand, int really_return, int reverse)
14088 {
14089   char conditional[10];
14090   char instr[100];
14091   unsigned reg;
14092   unsigned long live_regs_mask;
14093   unsigned long func_type;
14094   arm_stack_offsets *offsets;
14095
14096   func_type = arm_current_func_type ();
14097
14098   if (IS_NAKED (func_type))
14099     return "";
14100
14101   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
14102     {
14103       /* If this function was declared non-returning, and we have
14104          found a tail call, then we have to trust that the called
14105          function won't return.  */
14106       if (really_return)
14107         {
14108           rtx ops[2];
14109
14110           /* Otherwise, trap an attempted return by aborting.  */
14111           ops[0] = operand;
14112           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
14113                                        : "abort");
14114           assemble_external_libcall (ops[1]);
14115           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
14116         }
14117
14118       return "";
14119     }
14120
14121   gcc_assert (!cfun->calls_alloca || really_return);
14122
14123   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
14124
14125   cfun->machine->return_used_this_function = 1;
14126
14127   offsets = arm_get_frame_offsets ();
14128   live_regs_mask = offsets->saved_regs_mask;
14129
14130   if (live_regs_mask)
14131     {
14132       const char * return_reg;
14133
14134       /* If we do not have any special requirements for function exit
14135          (e.g. interworking) then we can load the return address
14136          directly into the PC.  Otherwise we must load it into LR.  */
14137       if (really_return
14138           && (IS_INTERRUPT (func_type) || !TARGET_INTERWORK))
14139         return_reg = reg_names[PC_REGNUM];
14140       else
14141         return_reg = reg_names[LR_REGNUM];
14142
14143       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
14144         {
14145           /* There are three possible reasons for the IP register
14146              being saved.  1) a stack frame was created, in which case
14147              IP contains the old stack pointer, or 2) an ISR routine
14148              corrupted it, or 3) it was saved to align the stack on
14149              iWMMXt.  In case 1, restore IP into SP, otherwise just
14150              restore IP.  */
14151           if (frame_pointer_needed)
14152             {
14153               live_regs_mask &= ~ (1 << IP_REGNUM);
14154               live_regs_mask |=   (1 << SP_REGNUM);
14155             }
14156           else
14157             gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
14158         }
14159
14160       /* On some ARM architectures it is faster to use LDR rather than
14161          LDM to load a single register.  On other architectures, the
14162          cost is the same.  In 26 bit mode, or for exception handlers,
14163          we have to use LDM to load the PC so that the CPSR is also
14164          restored.  */
14165       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
14166         if (live_regs_mask == (1U << reg))
14167           break;
14168
14169       if (reg <= LAST_ARM_REGNUM
14170           && (reg != LR_REGNUM
14171               || ! really_return
14172               || ! IS_INTERRUPT (func_type)))
14173         {
14174           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
14175                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
14176         }
14177       else
14178         {
14179           char *p;
14180           int first = 1;
14181
14182           /* Generate the load multiple instruction to restore the
14183              registers.  Note we can get here, even if
14184              frame_pointer_needed is true, but only if sp already
14185              points to the base of the saved core registers.  */
14186           if (live_regs_mask & (1 << SP_REGNUM))
14187             {
14188               unsigned HOST_WIDE_INT stack_adjust;
14189
14190               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
14191               gcc_assert (stack_adjust == 0 || stack_adjust == 4);
14192
14193               if (stack_adjust && arm_arch5 && TARGET_ARM)
14194                 if (TARGET_UNIFIED_ASM)
14195                   sprintf (instr, "ldmib%s\t%%|sp, {", conditional);
14196                 else
14197                   sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
14198               else
14199                 {
14200                   /* If we can't use ldmib (SA110 bug),
14201                      then try to pop r3 instead.  */
14202                   if (stack_adjust)
14203                     live_regs_mask |= 1 << 3;
14204                   
14205                   if (TARGET_UNIFIED_ASM)
14206                     sprintf (instr, "ldmfd%s\t%%|sp, {", conditional);
14207                   else
14208                     sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
14209                 }
14210             }
14211           else
14212             if (TARGET_UNIFIED_ASM)
14213               sprintf (instr, "pop%s\t{", conditional);
14214             else
14215               sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
14216
14217           p = instr + strlen (instr);
14218
14219           for (reg = 0; reg <= SP_REGNUM; reg++)
14220             if (live_regs_mask & (1 << reg))
14221               {
14222                 int l = strlen (reg_names[reg]);
14223
14224                 if (first)
14225                   first = 0;
14226                 else
14227                   {
14228                     memcpy (p, ", ", 2);
14229                     p += 2;
14230                   }
14231
14232                 memcpy (p, "%|", 2);
14233                 memcpy (p + 2, reg_names[reg], l);
14234                 p += l + 2;
14235               }
14236
14237           if (live_regs_mask & (1 << LR_REGNUM))
14238             {
14239               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
14240               /* If returning from an interrupt, restore the CPSR.  */
14241               if (IS_INTERRUPT (func_type))
14242                 strcat (p, "^");
14243             }
14244           else
14245             strcpy (p, "}");
14246         }
14247
14248       output_asm_insn (instr, & operand);
14249
14250       /* See if we need to generate an extra instruction to
14251          perform the actual function return.  */
14252       if (really_return
14253           && func_type != ARM_FT_INTERWORKED
14254           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
14255         {
14256           /* The return has already been handled
14257              by loading the LR into the PC.  */
14258           really_return = 0;
14259         }
14260     }
14261
14262   if (really_return)
14263     {
14264       switch ((int) ARM_FUNC_TYPE (func_type))
14265         {
14266         case ARM_FT_ISR:
14267         case ARM_FT_FIQ:
14268           /* ??? This is wrong for unified assembly syntax.  */
14269           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
14270           break;
14271
14272         case ARM_FT_INTERWORKED:
14273           sprintf (instr, "bx%s\t%%|lr", conditional);
14274           break;
14275
14276         case ARM_FT_EXCEPTION:
14277           /* ??? This is wrong for unified assembly syntax.  */
14278           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
14279           break;
14280
14281         default:
14282           /* Use bx if it's available.  */
14283           if (arm_arch5 || arm_arch4t)
14284             sprintf (instr, "bx%s\t%%|lr", conditional);
14285           else
14286             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
14287           break;
14288         }
14289
14290       output_asm_insn (instr, & operand);
14291     }
14292
14293   return "";
14294 }
14295
14296 /* Write the function name into the code section, directly preceding
14297    the function prologue.
14298
14299    Code will be output similar to this:
14300      t0
14301          .ascii "arm_poke_function_name", 0
14302          .align
14303      t1
14304          .word 0xff000000 + (t1 - t0)
14305      arm_poke_function_name
14306          mov     ip, sp
14307          stmfd   sp!, {fp, ip, lr, pc}
14308          sub     fp, ip, #4
14309
14310    When performing a stack backtrace, code can inspect the value
14311    of 'pc' stored at 'fp' + 0.  If the trace function then looks
14312    at location pc - 12 and the top 8 bits are set, then we know
14313    that there is a function name embedded immediately preceding this
14314    location and has length ((pc[-3]) & 0xff000000).
14315
14316    We assume that pc is declared as a pointer to an unsigned long.
14317
14318    It is of no benefit to output the function name if we are assembling
14319    a leaf function.  These function types will not contain a stack
14320    backtrace structure, therefore it is not possible to determine the
14321    function name.  */
14322 void
14323 arm_poke_function_name (FILE *stream, const char *name)
14324 {
14325   unsigned long alignlength;
14326   unsigned long length;
14327   rtx           x;
14328
14329   length      = strlen (name) + 1;
14330   alignlength = ROUND_UP_WORD (length);
14331
14332   ASM_OUTPUT_ASCII (stream, name, length);
14333   ASM_OUTPUT_ALIGN (stream, 2);
14334   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
14335   assemble_aligned_integer (UNITS_PER_WORD, x);
14336 }
14337
14338 /* Place some comments into the assembler stream
14339    describing the current function.  */
14340 static void
14341 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
14342 {
14343   unsigned long func_type;
14344
14345   if (TARGET_THUMB1)
14346     {
14347       thumb1_output_function_prologue (f, frame_size);
14348       return;
14349     }
14350
14351   /* Sanity check.  */
14352   gcc_assert (!arm_ccfsm_state && !arm_target_insn);
14353
14354   func_type = arm_current_func_type ();
14355
14356   switch ((int) ARM_FUNC_TYPE (func_type))
14357     {
14358     default:
14359     case ARM_FT_NORMAL:
14360       break;
14361     case ARM_FT_INTERWORKED:
14362       asm_fprintf (f, "\t%@ Function supports interworking.\n");
14363       break;
14364     case ARM_FT_ISR:
14365       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
14366       break;
14367     case ARM_FT_FIQ:
14368       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
14369       break;
14370     case ARM_FT_EXCEPTION:
14371       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
14372       break;
14373     }
14374
14375   if (IS_NAKED (func_type))
14376     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
14377
14378   if (IS_VOLATILE (func_type))
14379     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
14380
14381   if (IS_NESTED (func_type))
14382     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
14383   if (IS_STACKALIGN (func_type))
14384     asm_fprintf (f, "\t%@ Stack Align: May be called with mis-aligned SP.\n");
14385
14386   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
14387                crtl->args.size,
14388                crtl->args.pretend_args_size, frame_size);
14389
14390   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
14391                frame_pointer_needed,
14392                cfun->machine->uses_anonymous_args);
14393
14394   if (cfun->machine->lr_save_eliminated)
14395     asm_fprintf (f, "\t%@ link register save eliminated.\n");
14396
14397   if (crtl->calls_eh_return)
14398     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
14399
14400 }
14401
14402 const char *
14403 arm_output_epilogue (rtx sibling)
14404 {
14405   int reg;
14406   unsigned long saved_regs_mask;
14407   unsigned long func_type;
14408   /* Floats_offset is the offset from the "virtual" frame.  In an APCS
14409      frame that is $fp + 4 for a non-variadic function.  */
14410   int floats_offset = 0;
14411   rtx operands[3];
14412   FILE * f = asm_out_file;
14413   unsigned int lrm_count = 0;
14414   int really_return = (sibling == NULL);
14415   int start_reg;
14416   arm_stack_offsets *offsets;
14417
14418   /* If we have already generated the return instruction
14419      then it is futile to generate anything else.  */
14420   if (use_return_insn (FALSE, sibling) && 
14421       (cfun->machine->return_used_this_function != 0))
14422     return "";
14423
14424   func_type = arm_current_func_type ();
14425
14426   if (IS_NAKED (func_type))
14427     /* Naked functions don't have epilogues.  */
14428     return "";
14429
14430   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
14431     {
14432       rtx op;
14433
14434       /* A volatile function should never return.  Call abort.  */
14435       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
14436       assemble_external_libcall (op);
14437       output_asm_insn ("bl\t%a0", &op);
14438
14439       return "";
14440     }
14441
14442   /* If we are throwing an exception, then we really must be doing a
14443      return, so we can't tail-call.  */
14444   gcc_assert (!crtl->calls_eh_return || really_return);
14445
14446   offsets = arm_get_frame_offsets ();
14447   saved_regs_mask = offsets->saved_regs_mask;
14448
14449   if (TARGET_IWMMXT)
14450     lrm_count = bit_count (saved_regs_mask);
14451
14452   floats_offset = offsets->saved_args;
14453   /* Compute how far away the floats will be.  */
14454   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
14455     if (saved_regs_mask & (1 << reg))
14456       floats_offset += 4;
14457
14458   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
14459     {
14460       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
14461       int vfp_offset = offsets->frame;
14462
14463       if (TARGET_FPA_EMU2)
14464         {
14465           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
14466             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14467               {
14468                 floats_offset += 12;
14469                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
14470                              reg, FP_REGNUM, floats_offset - vfp_offset);
14471               }
14472         }
14473       else
14474         {
14475           start_reg = LAST_FPA_REGNUM;
14476
14477           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
14478             {
14479               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14480                 {
14481                   floats_offset += 12;
14482
14483                   /* We can't unstack more than four registers at once.  */
14484                   if (start_reg - reg == 3)
14485                     {
14486                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
14487                                    reg, FP_REGNUM, floats_offset - vfp_offset);
14488                       start_reg = reg - 1;
14489                     }
14490                 }
14491               else
14492                 {
14493                   if (reg != start_reg)
14494                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
14495                                  reg + 1, start_reg - reg,
14496                                  FP_REGNUM, floats_offset - vfp_offset);
14497                   start_reg = reg - 1;
14498                 }
14499             }
14500
14501           /* Just in case the last register checked also needs unstacking.  */
14502           if (reg != start_reg)
14503             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
14504                          reg + 1, start_reg - reg,
14505                          FP_REGNUM, floats_offset - vfp_offset);
14506         }
14507
14508       if (TARGET_HARD_FLOAT && TARGET_VFP)
14509         {
14510           int saved_size;
14511
14512           /* The fldmd insns do not have base+offset addressing
14513              modes, so we use IP to hold the address.  */
14514           saved_size = arm_get_vfp_saved_size ();
14515
14516           if (saved_size > 0)
14517             {
14518               floats_offset += saved_size;
14519               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
14520                            FP_REGNUM, floats_offset - vfp_offset);
14521             }
14522           start_reg = FIRST_VFP_REGNUM;
14523           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
14524             {
14525               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
14526                   && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
14527                 {
14528                   if (start_reg != reg)
14529                     vfp_output_fldmd (f, IP_REGNUM,
14530                                       (start_reg - FIRST_VFP_REGNUM) / 2,
14531                                       (reg - start_reg) / 2);
14532                   start_reg = reg + 2;
14533                 }
14534             }
14535           if (start_reg != reg)
14536             vfp_output_fldmd (f, IP_REGNUM,
14537                               (start_reg - FIRST_VFP_REGNUM) / 2,
14538                               (reg - start_reg) / 2);
14539         }
14540
14541       if (TARGET_IWMMXT)
14542         {
14543           /* The frame pointer is guaranteed to be non-double-word aligned.
14544              This is because it is set to (old_stack_pointer - 4) and the
14545              old_stack_pointer was double word aligned.  Thus the offset to
14546              the iWMMXt registers to be loaded must also be non-double-word
14547              sized, so that the resultant address *is* double-word aligned.
14548              We can ignore floats_offset since that was already included in
14549              the live_regs_mask.  */
14550           lrm_count += (lrm_count % 2 ? 2 : 1);
14551
14552           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
14553             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14554               {
14555                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
14556                              reg, FP_REGNUM, lrm_count * 4);
14557                 lrm_count += 2;
14558               }
14559         }
14560
14561       /* saved_regs_mask should contain the IP, which at the time of stack
14562          frame generation actually contains the old stack pointer.  So a
14563          quick way to unwind the stack is just pop the IP register directly
14564          into the stack pointer.  */
14565       gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
14566       saved_regs_mask &= ~ (1 << IP_REGNUM);
14567       saved_regs_mask |=   (1 << SP_REGNUM);
14568
14569       /* There are two registers left in saved_regs_mask - LR and PC.  We
14570          only need to restore the LR register (the return address), but to
14571          save time we can load it directly into the PC, unless we need a
14572          special function exit sequence, or we are not really returning.  */
14573       if (really_return
14574           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
14575           && !crtl->calls_eh_return)
14576         /* Delete the LR from the register mask, so that the LR on
14577            the stack is loaded into the PC in the register mask.  */
14578         saved_regs_mask &= ~ (1 << LR_REGNUM);
14579       else
14580         saved_regs_mask &= ~ (1 << PC_REGNUM);
14581
14582       /* We must use SP as the base register, because SP is one of the
14583          registers being restored.  If an interrupt or page fault
14584          happens in the ldm instruction, the SP might or might not
14585          have been restored.  That would be bad, as then SP will no
14586          longer indicate the safe area of stack, and we can get stack
14587          corruption.  Using SP as the base register means that it will
14588          be reset correctly to the original value, should an interrupt
14589          occur.  If the stack pointer already points at the right
14590          place, then omit the subtraction.  */
14591       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
14592           || cfun->calls_alloca)
14593         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
14594                      4 * bit_count (saved_regs_mask));
14595       print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask, 0);
14596
14597       if (IS_INTERRUPT (func_type))
14598         /* Interrupt handlers will have pushed the
14599            IP onto the stack, so restore it now.  */
14600         print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, 1 << IP_REGNUM, 0);
14601     }
14602   else
14603     {
14604       /* This branch is executed for ARM mode (non-apcs frames) and
14605          Thumb-2 mode. Frame layout is essentially the same for those
14606          cases, except that in ARM mode frame pointer points to the
14607          first saved register, while in Thumb-2 mode the frame pointer points
14608          to the last saved register.
14609
14610          It is possible to make frame pointer point to last saved
14611          register in both cases, and remove some conditionals below.
14612          That means that fp setup in prologue would be just "mov fp, sp"
14613          and sp restore in epilogue would be just "mov sp, fp", whereas
14614          now we have to use add/sub in those cases. However, the value
14615          of that would be marginal, as both mov and add/sub are 32-bit
14616          in ARM mode, and it would require extra conditionals
14617          in arm_expand_prologue to distingish ARM-apcs-frame case
14618          (where frame pointer is required to point at first register)
14619          and ARM-non-apcs-frame. Therefore, such change is postponed
14620          until real need arise.  */
14621       unsigned HOST_WIDE_INT amount;
14622       int rfe;
14623       /* Restore stack pointer if necessary.  */
14624       if (TARGET_ARM && frame_pointer_needed)
14625         {
14626           operands[0] = stack_pointer_rtx;
14627           operands[1] = hard_frame_pointer_rtx;
14628           
14629           operands[2] = GEN_INT (offsets->frame - offsets->saved_regs);
14630           output_add_immediate (operands);
14631         }
14632       else
14633         {
14634           if (frame_pointer_needed)
14635             {
14636               /* For Thumb-2 restore sp from the frame pointer.
14637                  Operand restrictions mean we have to incrememnt FP, then copy
14638                  to SP.  */
14639               amount = offsets->locals_base - offsets->saved_regs;
14640               operands[0] = hard_frame_pointer_rtx;
14641             }
14642           else
14643             {
14644               unsigned long count;
14645               operands[0] = stack_pointer_rtx;
14646               amount = offsets->outgoing_args - offsets->saved_regs;
14647               /* pop call clobbered registers if it avoids a
14648                  separate stack adjustment.  */
14649               count = offsets->saved_regs - offsets->saved_args;
14650               if (optimize_size
14651                   && count != 0
14652                   && !crtl->calls_eh_return
14653                   && bit_count(saved_regs_mask) * 4 == count
14654                   && !IS_INTERRUPT (func_type)
14655                   && !crtl->tail_call_emit)
14656                 {
14657                   unsigned long mask;
14658                   /* Preserve return values, of any size.  */
14659                   mask = (1 << ((arm_size_return_regs() + 3) / 4)) - 1;
14660                   mask ^= 0xf;
14661                   mask &= ~saved_regs_mask;
14662                   reg = 0;
14663                   while (bit_count (mask) * 4 > amount)
14664                     {
14665                       while ((mask & (1 << reg)) == 0)
14666                         reg++;
14667                       mask &= ~(1 << reg);
14668                     }
14669                   if (bit_count (mask) * 4 == amount) {
14670                       amount = 0;
14671                       saved_regs_mask |= mask;
14672                   }
14673                 }
14674             }
14675           
14676           if (amount)
14677             {
14678               operands[1] = operands[0];
14679               operands[2] = GEN_INT (amount);
14680               output_add_immediate (operands);
14681             }
14682           if (frame_pointer_needed)
14683             asm_fprintf (f, "\tmov\t%r, %r\n",
14684                          SP_REGNUM, HARD_FRAME_POINTER_REGNUM);
14685         }
14686
14687       if (TARGET_FPA_EMU2)
14688         {
14689           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
14690             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14691               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
14692                            reg, SP_REGNUM);
14693         }
14694       else
14695         {
14696           start_reg = FIRST_FPA_REGNUM;
14697
14698           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
14699             {
14700               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14701                 {
14702                   if (reg - start_reg == 3)
14703                     {
14704                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
14705                                    start_reg, SP_REGNUM);
14706                       start_reg = reg + 1;
14707                     }
14708                 }
14709               else
14710                 {
14711                   if (reg != start_reg)
14712                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
14713                                  start_reg, reg - start_reg,
14714                                  SP_REGNUM);
14715
14716                   start_reg = reg + 1;
14717                 }
14718             }
14719
14720           /* Just in case the last register checked also needs unstacking.  */
14721           if (reg != start_reg)
14722             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
14723                          start_reg, reg - start_reg, SP_REGNUM);
14724         }
14725
14726       if (TARGET_HARD_FLOAT && TARGET_VFP)
14727         {
14728           int end_reg = LAST_VFP_REGNUM + 1;
14729
14730           /* Scan the registers in reverse order.  We need to match
14731              any groupings made in the prologue and generate matching
14732              pop operations.  */
14733           for (reg = LAST_VFP_REGNUM - 1; reg >= FIRST_VFP_REGNUM; reg -= 2)
14734             {
14735               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
14736                   && (!df_regs_ever_live_p (reg + 1)
14737                       || call_used_regs[reg + 1]))
14738                 {
14739                   if (end_reg > reg + 2)
14740                     vfp_output_fldmd (f, SP_REGNUM,
14741                                       (reg + 2 - FIRST_VFP_REGNUM) / 2,
14742                                       (end_reg - (reg + 2)) / 2);
14743                   end_reg = reg;
14744                 }
14745             }
14746           if (end_reg > reg + 2)
14747             vfp_output_fldmd (f, SP_REGNUM, 0,
14748                               (end_reg - (reg + 2)) / 2);
14749         }
14750
14751       if (TARGET_IWMMXT)
14752         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
14753           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14754             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
14755
14756       /* If we can, restore the LR into the PC.  */
14757       if (ARM_FUNC_TYPE (func_type) != ARM_FT_INTERWORKED
14758           && (TARGET_ARM || ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
14759           && !IS_STACKALIGN (func_type)
14760           && really_return
14761           && crtl->args.pretend_args_size == 0
14762           && saved_regs_mask & (1 << LR_REGNUM)
14763           && !crtl->calls_eh_return)
14764         {
14765           saved_regs_mask &= ~ (1 << LR_REGNUM);
14766           saved_regs_mask |=   (1 << PC_REGNUM);
14767           rfe = IS_INTERRUPT (func_type);
14768         }
14769       else
14770         rfe = 0;
14771
14772       /* Load the registers off the stack.  If we only have one register
14773          to load use the LDR instruction - it is faster.  For Thumb-2
14774          always use pop and the assembler will pick the best instruction.*/
14775       if (TARGET_ARM && saved_regs_mask == (1 << LR_REGNUM)
14776           && !IS_INTERRUPT(func_type))
14777         {
14778           asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
14779         }
14780       else if (saved_regs_mask)
14781         {
14782           if (saved_regs_mask & (1 << SP_REGNUM))
14783             /* Note - write back to the stack register is not enabled
14784                (i.e. "ldmfd sp!...").  We know that the stack pointer is
14785                in the list of registers and if we add writeback the
14786                instruction becomes UNPREDICTABLE.  */
14787             print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask,
14788                              rfe);
14789           else if (TARGET_ARM)
14790             print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, saved_regs_mask,
14791                              rfe);
14792           else
14793             print_multi_reg (f, "pop\t", SP_REGNUM, saved_regs_mask, 0);
14794         }
14795
14796       if (crtl->args.pretend_args_size)
14797         {
14798           /* Unwind the pre-pushed regs.  */
14799           operands[0] = operands[1] = stack_pointer_rtx;
14800           operands[2] = GEN_INT (crtl->args.pretend_args_size);
14801           output_add_immediate (operands);
14802         }
14803     }
14804
14805   /* We may have already restored PC directly from the stack.  */
14806   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
14807     return "";
14808
14809   /* Stack adjustment for exception handler.  */
14810   if (crtl->calls_eh_return)
14811     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
14812                  ARM_EH_STACKADJ_REGNUM);
14813
14814   /* Generate the return instruction.  */
14815   switch ((int) ARM_FUNC_TYPE (func_type))
14816     {
14817     case ARM_FT_ISR:
14818     case ARM_FT_FIQ:
14819       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
14820       break;
14821
14822     case ARM_FT_EXCEPTION:
14823       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
14824       break;
14825
14826     case ARM_FT_INTERWORKED:
14827       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
14828       break;
14829
14830     default:
14831       if (IS_STACKALIGN (func_type))
14832         {
14833           /* See comment in arm_expand_prologue.  */
14834           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, 0);
14835         }
14836       if (arm_arch5 || arm_arch4t)
14837         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
14838       else
14839         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
14840       break;
14841     }
14842
14843   return "";
14844 }
14845
14846 static void
14847 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
14848                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
14849 {
14850   arm_stack_offsets *offsets;
14851
14852   if (TARGET_THUMB1)
14853     {
14854       int regno;
14855
14856       /* Emit any call-via-reg trampolines that are needed for v4t support
14857          of call_reg and call_value_reg type insns.  */
14858       for (regno = 0; regno < LR_REGNUM; regno++)
14859         {
14860           rtx label = cfun->machine->call_via[regno];
14861
14862           if (label != NULL)
14863             {
14864               switch_to_section (function_section (current_function_decl));
14865               targetm.asm_out.internal_label (asm_out_file, "L",
14866                                               CODE_LABEL_NUMBER (label));
14867               asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
14868             }
14869         }
14870
14871       /* ??? Probably not safe to set this here, since it assumes that a
14872          function will be emitted as assembly immediately after we generate
14873          RTL for it.  This does not happen for inline functions.  */
14874       cfun->machine->return_used_this_function = 0;
14875     }
14876   else /* TARGET_32BIT */
14877     {
14878       /* We need to take into account any stack-frame rounding.  */
14879       offsets = arm_get_frame_offsets ();
14880
14881       gcc_assert (!use_return_insn (FALSE, NULL)
14882                   || (cfun->machine->return_used_this_function != 0)
14883                   || offsets->saved_regs == offsets->outgoing_args
14884                   || frame_pointer_needed);
14885
14886       /* Reset the ARM-specific per-function variables.  */
14887       after_arm_reorg = 0;
14888     }
14889 }
14890
14891 /* Generate and emit an insn that we will recognize as a push_multi.
14892    Unfortunately, since this insn does not reflect very well the actual
14893    semantics of the operation, we need to annotate the insn for the benefit
14894    of DWARF2 frame unwind information.  */
14895 static rtx
14896 emit_multi_reg_push (unsigned long mask)
14897 {
14898   int num_regs = 0;
14899   int num_dwarf_regs;
14900   int i, j;
14901   rtx par;
14902   rtx dwarf;
14903   int dwarf_par_index;
14904   rtx tmp, reg;
14905
14906   for (i = 0; i <= LAST_ARM_REGNUM; i++)
14907     if (mask & (1 << i))
14908       num_regs++;
14909
14910   gcc_assert (num_regs && num_regs <= 16);
14911
14912   /* We don't record the PC in the dwarf frame information.  */
14913   num_dwarf_regs = num_regs;
14914   if (mask & (1 << PC_REGNUM))
14915     num_dwarf_regs--;
14916
14917   /* For the body of the insn we are going to generate an UNSPEC in
14918      parallel with several USEs.  This allows the insn to be recognized
14919      by the push_multi pattern in the arm.md file.
14920
14921      The body of the insn looks something like this:
14922
14923        (parallel [
14924            (set (mem:BLK (pre_modify:SI (reg:SI sp)
14925                                         (const_int:SI <num>)))
14926                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
14927            (use (reg:SI XX))
14928            (use (reg:SI YY))
14929            ...
14930         ])
14931
14932      For the frame note however, we try to be more explicit and actually
14933      show each register being stored into the stack frame, plus a (single)
14934      decrement of the stack pointer.  We do it this way in order to be
14935      friendly to the stack unwinding code, which only wants to see a single
14936      stack decrement per instruction.  The RTL we generate for the note looks
14937      something like this:
14938
14939       (sequence [
14940            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
14941            (set (mem:SI (reg:SI sp)) (reg:SI r4))
14942            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI XX))
14943            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI YY))
14944            ...
14945         ])
14946
14947      FIXME:: In an ideal world the PRE_MODIFY would not exist and
14948      instead we'd have a parallel expression detailing all
14949      the stores to the various memory addresses so that debug
14950      information is more up-to-date. Remember however while writing
14951      this to take care of the constraints with the push instruction.
14952
14953      Note also that this has to be taken care of for the VFP registers.
14954
14955      For more see PR43399.  */
14956
14957   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
14958   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
14959   dwarf_par_index = 1;
14960
14961   for (i = 0; i <= LAST_ARM_REGNUM; i++)
14962     {
14963       if (mask & (1 << i))
14964         {
14965           reg = gen_rtx_REG (SImode, i);
14966
14967           XVECEXP (par, 0, 0)
14968             = gen_rtx_SET (VOIDmode,
14969                            gen_frame_mem
14970                            (BLKmode,
14971                             gen_rtx_PRE_MODIFY (Pmode,
14972                                                 stack_pointer_rtx,
14973                                                 plus_constant
14974                                                 (stack_pointer_rtx,
14975                                                  -4 * num_regs))
14976                             ),
14977                            gen_rtx_UNSPEC (BLKmode,
14978                                            gen_rtvec (1, reg),
14979                                            UNSPEC_PUSH_MULT));
14980
14981           if (i != PC_REGNUM)
14982             {
14983               tmp = gen_rtx_SET (VOIDmode,
14984                                  gen_frame_mem (SImode, stack_pointer_rtx),
14985                                  reg);
14986               RTX_FRAME_RELATED_P (tmp) = 1;
14987               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
14988               dwarf_par_index++;
14989             }
14990
14991           break;
14992         }
14993     }
14994
14995   for (j = 1, i++; j < num_regs; i++)
14996     {
14997       if (mask & (1 << i))
14998         {
14999           reg = gen_rtx_REG (SImode, i);
15000
15001           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
15002
15003           if (i != PC_REGNUM)
15004             {
15005               tmp
15006                 = gen_rtx_SET (VOIDmode,
15007                                gen_frame_mem
15008                                (SImode,
15009                                 plus_constant (stack_pointer_rtx,
15010                                                4 * j)),
15011                                reg);
15012               RTX_FRAME_RELATED_P (tmp) = 1;
15013               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
15014             }
15015
15016           j++;
15017         }
15018     }
15019
15020   par = emit_insn (par);
15021
15022   tmp = gen_rtx_SET (VOIDmode,
15023                      stack_pointer_rtx,
15024                      plus_constant (stack_pointer_rtx, -4 * num_regs));
15025   RTX_FRAME_RELATED_P (tmp) = 1;
15026   XVECEXP (dwarf, 0, 0) = tmp;
15027
15028   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
15029
15030   return par;
15031 }
15032
15033 /* Calculate the size of the return value that is passed in registers.  */
15034 static unsigned
15035 arm_size_return_regs (void)
15036 {
15037   enum machine_mode mode;
15038
15039   if (crtl->return_rtx != 0)
15040     mode = GET_MODE (crtl->return_rtx);
15041   else
15042     mode = DECL_MODE (DECL_RESULT (current_function_decl));
15043
15044   return GET_MODE_SIZE (mode);
15045 }
15046
15047 static rtx
15048 emit_sfm (int base_reg, int count)
15049 {
15050   rtx par;
15051   rtx dwarf;
15052   rtx tmp, reg;
15053   int i;
15054
15055   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
15056   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
15057
15058   reg = gen_rtx_REG (XFmode, base_reg++);
15059
15060   XVECEXP (par, 0, 0)
15061     = gen_rtx_SET (VOIDmode,
15062                    gen_frame_mem
15063                    (BLKmode,
15064                     gen_rtx_PRE_MODIFY (Pmode,
15065                                         stack_pointer_rtx,
15066                                         plus_constant
15067                                         (stack_pointer_rtx,
15068                                          -12 * count))
15069                     ),
15070                    gen_rtx_UNSPEC (BLKmode,
15071                                    gen_rtvec (1, reg),
15072                                    UNSPEC_PUSH_MULT));
15073   tmp = gen_rtx_SET (VOIDmode,
15074                      gen_frame_mem (XFmode, stack_pointer_rtx), reg);
15075   RTX_FRAME_RELATED_P (tmp) = 1;
15076   XVECEXP (dwarf, 0, 1) = tmp;
15077
15078   for (i = 1; i < count; i++)
15079     {
15080       reg = gen_rtx_REG (XFmode, base_reg++);
15081       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
15082
15083       tmp = gen_rtx_SET (VOIDmode,
15084                          gen_frame_mem (XFmode,
15085                                         plus_constant (stack_pointer_rtx,
15086                                                        i * 12)),
15087                          reg);
15088       RTX_FRAME_RELATED_P (tmp) = 1;
15089       XVECEXP (dwarf, 0, i + 1) = tmp;
15090     }
15091
15092   tmp = gen_rtx_SET (VOIDmode,
15093                      stack_pointer_rtx,
15094                      plus_constant (stack_pointer_rtx, -12 * count));
15095
15096   RTX_FRAME_RELATED_P (tmp) = 1;
15097   XVECEXP (dwarf, 0, 0) = tmp;
15098
15099   par = emit_insn (par);
15100   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
15101
15102   return par;
15103 }
15104
15105
15106 /* Return true if the current function needs to save/restore LR.  */
15107
15108 static bool
15109 thumb_force_lr_save (void)
15110 {
15111   return !cfun->machine->lr_save_eliminated
15112          && (!leaf_function_p ()
15113              || thumb_far_jump_used_p ()
15114              || df_regs_ever_live_p (LR_REGNUM));
15115 }
15116
15117
15118 /* Compute the distance from register FROM to register TO.
15119    These can be the arg pointer (26), the soft frame pointer (25),
15120    the stack pointer (13) or the hard frame pointer (11).
15121    In thumb mode r7 is used as the soft frame pointer, if needed.
15122    Typical stack layout looks like this:
15123
15124        old stack pointer -> |    |
15125                              ----
15126                             |    | \
15127                             |    |   saved arguments for
15128                             |    |   vararg functions
15129                             |    | /
15130                               --
15131    hard FP & arg pointer -> |    | \
15132                             |    |   stack
15133                             |    |   frame
15134                             |    | /
15135                               --
15136                             |    | \
15137                             |    |   call saved
15138                             |    |   registers
15139       soft frame pointer -> |    | /
15140                               --
15141                             |    | \
15142                             |    |   local
15143                             |    |   variables
15144      locals base pointer -> |    | /
15145                               --
15146                             |    | \
15147                             |    |   outgoing
15148                             |    |   arguments
15149    current stack pointer -> |    | /
15150                               --
15151
15152   For a given function some or all of these stack components
15153   may not be needed, giving rise to the possibility of
15154   eliminating some of the registers.
15155
15156   The values returned by this function must reflect the behavior
15157   of arm_expand_prologue() and arm_compute_save_reg_mask().
15158
15159   The sign of the number returned reflects the direction of stack
15160   growth, so the values are positive for all eliminations except
15161   from the soft frame pointer to the hard frame pointer.
15162
15163   SFP may point just inside the local variables block to ensure correct
15164   alignment.  */
15165
15166
15167 /* Calculate stack offsets.  These are used to calculate register elimination
15168    offsets and in prologue/epilogue code.  Also calculates which registers
15169    should be saved.  */
15170
15171 static arm_stack_offsets *
15172 arm_get_frame_offsets (void)
15173 {
15174   struct arm_stack_offsets *offsets;
15175   unsigned long func_type;
15176   int leaf;
15177   int saved;
15178   int core_saved;
15179   HOST_WIDE_INT frame_size;
15180   int i;
15181
15182   offsets = &cfun->machine->stack_offsets;
15183
15184   /* We need to know if we are a leaf function.  Unfortunately, it
15185      is possible to be called after start_sequence has been called,
15186      which causes get_insns to return the insns for the sequence,
15187      not the function, which will cause leaf_function_p to return
15188      the incorrect result.
15189
15190      to know about leaf functions once reload has completed, and the
15191      frame size cannot be changed after that time, so we can safely
15192      use the cached value.  */
15193
15194   if (reload_completed)
15195     return offsets;
15196
15197   /* Initially this is the size of the local variables.  It will translated
15198      into an offset once we have determined the size of preceding data.  */
15199   frame_size = ROUND_UP_WORD (get_frame_size ());
15200
15201   leaf = leaf_function_p ();
15202
15203   /* Space for variadic functions.  */
15204   offsets->saved_args = crtl->args.pretend_args_size;
15205
15206   /* In Thumb mode this is incorrect, but never used.  */
15207   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0) +
15208                    arm_compute_static_chain_stack_bytes();
15209
15210   if (TARGET_32BIT)
15211     {
15212       unsigned int regno;
15213
15214       offsets->saved_regs_mask = arm_compute_save_reg_mask ();
15215       core_saved = bit_count (offsets->saved_regs_mask) * 4;
15216       saved = core_saved;
15217
15218       /* We know that SP will be doubleword aligned on entry, and we must
15219          preserve that condition at any subroutine call.  We also require the
15220          soft frame pointer to be doubleword aligned.  */
15221
15222       if (TARGET_REALLY_IWMMXT)
15223         {
15224           /* Check for the call-saved iWMMXt registers.  */
15225           for (regno = FIRST_IWMMXT_REGNUM;
15226                regno <= LAST_IWMMXT_REGNUM;
15227                regno++)
15228             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
15229               saved += 8;
15230         }
15231
15232       func_type = arm_current_func_type ();
15233       if (! IS_VOLATILE (func_type))
15234         {
15235           /* Space for saved FPA registers.  */
15236           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
15237             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
15238             saved += 12;
15239
15240           /* Space for saved VFP registers.  */
15241           if (TARGET_HARD_FLOAT && TARGET_VFP)
15242             saved += arm_get_vfp_saved_size ();
15243         }
15244     }
15245   else /* TARGET_THUMB1 */
15246     {
15247       offsets->saved_regs_mask = thumb1_compute_save_reg_mask ();
15248       core_saved = bit_count (offsets->saved_regs_mask) * 4;
15249       saved = core_saved;
15250       if (TARGET_BACKTRACE)
15251         saved += 16;
15252     }
15253
15254   /* Saved registers include the stack frame.  */
15255   offsets->saved_regs = offsets->saved_args + saved +
15256                         arm_compute_static_chain_stack_bytes();
15257   offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
15258   /* A leaf function does not need any stack alignment if it has nothing
15259      on the stack.  */
15260   if (leaf && frame_size == 0)
15261     {
15262       offsets->outgoing_args = offsets->soft_frame;
15263       offsets->locals_base = offsets->soft_frame;
15264       return offsets;
15265     }
15266
15267   /* Ensure SFP has the correct alignment.  */
15268   if (ARM_DOUBLEWORD_ALIGN
15269       && (offsets->soft_frame & 7))
15270     {
15271       offsets->soft_frame += 4;
15272       /* Try to align stack by pushing an extra reg.  Don't bother doing this
15273          when there is a stack frame as the alignment will be rolled into
15274          the normal stack adjustment.  */
15275       if (frame_size + crtl->outgoing_args_size == 0)
15276         {
15277           int reg = -1;
15278
15279           /* If it is safe to use r3, then do so.  This sometimes 
15280              generates better code on Thumb-2 by avoiding the need to
15281              use 32-bit push/pop instructions.  */
15282           if (!crtl->tail_call_emit
15283               && arm_size_return_regs () <= 12
15284               && (offsets->saved_regs_mask & (1 << 3)) == 0)
15285             {
15286               reg = 3;
15287             }
15288           else
15289             for (i = 4; i <= (TARGET_THUMB1 ? LAST_LO_REGNUM : 11); i++)
15290               {
15291                 if ((offsets->saved_regs_mask & (1 << i)) == 0)
15292                   {
15293                     reg = i;
15294                     break;
15295                   }
15296               }
15297
15298           if (reg != -1)
15299             {
15300               offsets->saved_regs += 4;
15301               offsets->saved_regs_mask |= (1 << reg);
15302             }
15303         }
15304     }
15305
15306   offsets->locals_base = offsets->soft_frame + frame_size;
15307   offsets->outgoing_args = (offsets->locals_base
15308                             + crtl->outgoing_args_size);
15309
15310   if (ARM_DOUBLEWORD_ALIGN)
15311     {
15312       /* Ensure SP remains doubleword aligned.  */
15313       if (offsets->outgoing_args & 7)
15314         offsets->outgoing_args += 4;
15315       gcc_assert (!(offsets->outgoing_args & 7));
15316     }
15317
15318   return offsets;
15319 }
15320
15321
15322 /* Calculate the relative offsets for the different stack pointers.  Positive
15323    offsets are in the direction of stack growth.  */
15324
15325 HOST_WIDE_INT
15326 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
15327 {
15328   arm_stack_offsets *offsets;
15329
15330   offsets = arm_get_frame_offsets ();
15331
15332   /* OK, now we have enough information to compute the distances.
15333      There must be an entry in these switch tables for each pair
15334      of registers in ELIMINABLE_REGS, even if some of the entries
15335      seem to be redundant or useless.  */
15336   switch (from)
15337     {
15338     case ARG_POINTER_REGNUM:
15339       switch (to)
15340         {
15341         case THUMB_HARD_FRAME_POINTER_REGNUM:
15342           return 0;
15343
15344         case FRAME_POINTER_REGNUM:
15345           /* This is the reverse of the soft frame pointer
15346              to hard frame pointer elimination below.  */
15347           return offsets->soft_frame - offsets->saved_args;
15348
15349         case ARM_HARD_FRAME_POINTER_REGNUM:
15350           /* This is only non-zero in the case where the static chain register
15351              is stored above the frame.  */
15352           return offsets->frame - offsets->saved_args - 4;
15353
15354         case STACK_POINTER_REGNUM:
15355           /* If nothing has been pushed on the stack at all
15356              then this will return -4.  This *is* correct!  */
15357           return offsets->outgoing_args - (offsets->saved_args + 4);
15358
15359         default:
15360           gcc_unreachable ();
15361         }
15362       gcc_unreachable ();
15363
15364     case FRAME_POINTER_REGNUM:
15365       switch (to)
15366         {
15367         case THUMB_HARD_FRAME_POINTER_REGNUM:
15368           return 0;
15369
15370         case ARM_HARD_FRAME_POINTER_REGNUM:
15371           /* The hard frame pointer points to the top entry in the
15372              stack frame.  The soft frame pointer to the bottom entry
15373              in the stack frame.  If there is no stack frame at all,
15374              then they are identical.  */
15375
15376           return offsets->frame - offsets->soft_frame;
15377
15378         case STACK_POINTER_REGNUM:
15379           return offsets->outgoing_args - offsets->soft_frame;
15380
15381         default:
15382           gcc_unreachable ();
15383         }
15384       gcc_unreachable ();
15385
15386     default:
15387       /* You cannot eliminate from the stack pointer.
15388          In theory you could eliminate from the hard frame
15389          pointer to the stack pointer, but this will never
15390          happen, since if a stack frame is not needed the
15391          hard frame pointer will never be used.  */
15392       gcc_unreachable ();
15393     }
15394 }
15395
15396 /* Given FROM and TO register numbers, say whether this elimination is
15397    allowed.  Frame pointer elimination is automatically handled.
15398
15399    All eliminations are permissible.  Note that ARG_POINTER_REGNUM and
15400    HARD_FRAME_POINTER_REGNUM are in fact the same thing.  If we need a frame
15401    pointer, we must eliminate FRAME_POINTER_REGNUM into
15402    HARD_FRAME_POINTER_REGNUM and not into STACK_POINTER_REGNUM or
15403    ARG_POINTER_REGNUM.  */
15404
15405 bool
15406 arm_can_eliminate (const int from, const int to)
15407 {
15408   return ((to == FRAME_POINTER_REGNUM && from == ARG_POINTER_REGNUM) ? false :
15409           (to == STACK_POINTER_REGNUM && frame_pointer_needed) ? false :
15410           (to == ARM_HARD_FRAME_POINTER_REGNUM && TARGET_THUMB) ? false :
15411           (to == THUMB_HARD_FRAME_POINTER_REGNUM && TARGET_ARM) ? false :
15412            true);
15413 }
15414
15415 /* Emit RTL to save coprocessor registers on function entry.  Returns the
15416    number of bytes pushed.  */
15417
15418 static int
15419 arm_save_coproc_regs(void)
15420 {
15421   int saved_size = 0;
15422   unsigned reg;
15423   unsigned start_reg;
15424   rtx insn;
15425
15426   for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
15427     if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
15428       {
15429         insn = gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx);
15430         insn = gen_rtx_MEM (V2SImode, insn);
15431         insn = emit_set_insn (insn, gen_rtx_REG (V2SImode, reg));
15432         RTX_FRAME_RELATED_P (insn) = 1;
15433         saved_size += 8;
15434       }
15435
15436   /* Save any floating point call-saved registers used by this
15437      function.  */
15438   if (TARGET_FPA_EMU2)
15439     {
15440       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
15441         if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
15442           {
15443             insn = gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx);
15444             insn = gen_rtx_MEM (XFmode, insn);
15445             insn = emit_set_insn (insn, gen_rtx_REG (XFmode, reg));
15446             RTX_FRAME_RELATED_P (insn) = 1;
15447             saved_size += 12;
15448           }
15449     }
15450   else
15451     {
15452       start_reg = LAST_FPA_REGNUM;
15453
15454       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
15455         {
15456           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
15457             {
15458               if (start_reg - reg == 3)
15459                 {
15460                   insn = emit_sfm (reg, 4);
15461                   RTX_FRAME_RELATED_P (insn) = 1;
15462                   saved_size += 48;
15463                   start_reg = reg - 1;
15464                 }
15465             }
15466           else
15467             {
15468               if (start_reg != reg)
15469                 {
15470                   insn = emit_sfm (reg + 1, start_reg - reg);
15471                   RTX_FRAME_RELATED_P (insn) = 1;
15472                   saved_size += (start_reg - reg) * 12;
15473                 }
15474               start_reg = reg - 1;
15475             }
15476         }
15477
15478       if (start_reg != reg)
15479         {
15480           insn = emit_sfm (reg + 1, start_reg - reg);
15481           saved_size += (start_reg - reg) * 12;
15482           RTX_FRAME_RELATED_P (insn) = 1;
15483         }
15484     }
15485   if (TARGET_HARD_FLOAT && TARGET_VFP)
15486     {
15487       start_reg = FIRST_VFP_REGNUM;
15488
15489       for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
15490         {
15491           if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
15492               && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
15493             {
15494               if (start_reg != reg)
15495                 saved_size += vfp_emit_fstmd (start_reg,
15496                                               (reg - start_reg) / 2);
15497               start_reg = reg + 2;
15498             }
15499         }
15500       if (start_reg != reg)
15501         saved_size += vfp_emit_fstmd (start_reg,
15502                                       (reg - start_reg) / 2);
15503     }
15504   return saved_size;
15505 }
15506
15507
15508 /* Set the Thumb frame pointer from the stack pointer.  */
15509
15510 static void
15511 thumb_set_frame_pointer (arm_stack_offsets *offsets)
15512 {
15513   HOST_WIDE_INT amount;
15514   rtx insn, dwarf;
15515
15516   amount = offsets->outgoing_args - offsets->locals_base;
15517   if (amount < 1024)
15518     insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15519                                   stack_pointer_rtx, GEN_INT (amount)));
15520   else
15521     {
15522       emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
15523       /* Thumb-2 RTL patterns expect sp as the first input.  Thumb-1
15524          expects the first two operands to be the same.  */
15525       if (TARGET_THUMB2)
15526         {
15527           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15528                                         stack_pointer_rtx,
15529                                         hard_frame_pointer_rtx));
15530         }
15531       else
15532         {
15533           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15534                                         hard_frame_pointer_rtx,
15535                                         stack_pointer_rtx));
15536         }
15537       dwarf = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
15538                            plus_constant (stack_pointer_rtx, amount));
15539       RTX_FRAME_RELATED_P (dwarf) = 1;
15540       add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
15541     }
15542
15543   RTX_FRAME_RELATED_P (insn) = 1;
15544 }
15545
15546 /* Generate the prologue instructions for entry into an ARM or Thumb-2
15547    function.  */
15548 void
15549 arm_expand_prologue (void)
15550 {
15551   rtx amount;
15552   rtx insn;
15553   rtx ip_rtx;
15554   unsigned long live_regs_mask;
15555   unsigned long func_type;
15556   int fp_offset = 0;
15557   int saved_pretend_args = 0;
15558   int saved_regs = 0;
15559   unsigned HOST_WIDE_INT args_to_push;
15560   arm_stack_offsets *offsets;
15561
15562   func_type = arm_current_func_type ();
15563
15564   /* Naked functions don't have prologues.  */
15565   if (IS_NAKED (func_type))
15566     return;
15567
15568   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
15569   args_to_push = crtl->args.pretend_args_size;
15570
15571   /* Compute which register we will have to save onto the stack.  */
15572   offsets = arm_get_frame_offsets ();
15573   live_regs_mask = offsets->saved_regs_mask;
15574
15575   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
15576
15577   if (IS_STACKALIGN (func_type))
15578     {
15579       rtx dwarf;
15580       rtx r0;
15581       rtx r1;
15582       /* Handle a word-aligned stack pointer.  We generate the following:
15583
15584           mov r0, sp
15585           bic r1, r0, #7
15586           mov sp, r1
15587           <save and restore r0 in normal prologue/epilogue>
15588           mov sp, r0
15589           bx lr
15590
15591          The unwinder doesn't need to know about the stack realignment.
15592          Just tell it we saved SP in r0.  */
15593       gcc_assert (TARGET_THUMB2 && !arm_arch_notm && args_to_push == 0);
15594
15595       r0 = gen_rtx_REG (SImode, 0);
15596       r1 = gen_rtx_REG (SImode, 1);
15597       /* Use a real rtvec rather than NULL_RTVEC so the rest of the
15598          compiler won't choke.  */
15599       dwarf = gen_rtx_UNSPEC (SImode, rtvec_alloc (0), UNSPEC_STACK_ALIGN);
15600       dwarf = gen_rtx_SET (VOIDmode, r0, dwarf);
15601       insn = gen_movsi (r0, stack_pointer_rtx);
15602       RTX_FRAME_RELATED_P (insn) = 1;
15603       add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
15604       emit_insn (insn);
15605       emit_insn (gen_andsi3 (r1, r0, GEN_INT (~(HOST_WIDE_INT)7)));
15606       emit_insn (gen_movsi (stack_pointer_rtx, r1));
15607     }
15608
15609   /* For APCS frames, if IP register is clobbered
15610      when creating frame, save that register in a special
15611      way.  */
15612   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
15613     {
15614       if (IS_INTERRUPT (func_type))
15615         {
15616           /* Interrupt functions must not corrupt any registers.
15617              Creating a frame pointer however, corrupts the IP
15618              register, so we must push it first.  */
15619           insn = emit_multi_reg_push (1 << IP_REGNUM);
15620
15621           /* Do not set RTX_FRAME_RELATED_P on this insn.
15622              The dwarf stack unwinding code only wants to see one
15623              stack decrement per function, and this is not it.  If
15624              this instruction is labeled as being part of the frame
15625              creation sequence then dwarf2out_frame_debug_expr will
15626              die when it encounters the assignment of IP to FP
15627              later on, since the use of SP here establishes SP as
15628              the CFA register and not IP.
15629
15630              Anyway this instruction is not really part of the stack
15631              frame creation although it is part of the prologue.  */
15632         }
15633       else if (IS_NESTED (func_type))
15634         {
15635           /* The Static chain register is the same as the IP register
15636              used as a scratch register during stack frame creation.
15637              To get around this need to find somewhere to store IP
15638              whilst the frame is being created.  We try the following
15639              places in order:
15640
15641                1. The last argument register.
15642                2. A slot on the stack above the frame.  (This only
15643                   works if the function is not a varargs function).
15644                3. Register r3, after pushing the argument registers
15645                   onto the stack.
15646
15647              Note - we only need to tell the dwarf2 backend about the SP
15648              adjustment in the second variant; the static chain register
15649              doesn't need to be unwound, as it doesn't contain a value
15650              inherited from the caller.  */
15651
15652           if (df_regs_ever_live_p (3) == false)
15653             insn = emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
15654           else if (args_to_push == 0)
15655             {
15656               rtx dwarf;
15657
15658               gcc_assert(arm_compute_static_chain_stack_bytes() == 4);
15659               saved_regs += 4;
15660
15661               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
15662               insn = emit_set_insn (gen_frame_mem (SImode, insn), ip_rtx);
15663               fp_offset = 4;
15664
15665               /* Just tell the dwarf backend that we adjusted SP.  */
15666               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
15667                                    plus_constant (stack_pointer_rtx,
15668                                                   -fp_offset));
15669               RTX_FRAME_RELATED_P (insn) = 1;
15670               add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
15671             }
15672           else
15673             {
15674               /* Store the args on the stack.  */
15675               if (cfun->machine->uses_anonymous_args)
15676                 insn = emit_multi_reg_push
15677                   ((0xf0 >> (args_to_push / 4)) & 0xf);
15678               else
15679                 insn = emit_insn
15680                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
15681                                GEN_INT (- args_to_push)));
15682
15683               RTX_FRAME_RELATED_P (insn) = 1;
15684
15685               saved_pretend_args = 1;
15686               fp_offset = args_to_push;
15687               args_to_push = 0;
15688
15689               /* Now reuse r3 to preserve IP.  */
15690               emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
15691             }
15692         }
15693
15694       insn = emit_set_insn (ip_rtx,
15695                             plus_constant (stack_pointer_rtx, fp_offset));
15696       RTX_FRAME_RELATED_P (insn) = 1;
15697     }
15698
15699   if (args_to_push)
15700     {
15701       /* Push the argument registers, or reserve space for them.  */
15702       if (cfun->machine->uses_anonymous_args)
15703         insn = emit_multi_reg_push
15704           ((0xf0 >> (args_to_push / 4)) & 0xf);
15705       else
15706         insn = emit_insn
15707           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
15708                        GEN_INT (- args_to_push)));
15709       RTX_FRAME_RELATED_P (insn) = 1;
15710     }
15711
15712   /* If this is an interrupt service routine, and the link register
15713      is going to be pushed, and we're not generating extra
15714      push of IP (needed when frame is needed and frame layout if apcs),
15715      subtracting four from LR now will mean that the function return
15716      can be done with a single instruction.  */
15717   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
15718       && (live_regs_mask & (1 << LR_REGNUM)) != 0
15719       && !(frame_pointer_needed && TARGET_APCS_FRAME)
15720       && TARGET_ARM)
15721     {
15722       rtx lr = gen_rtx_REG (SImode, LR_REGNUM);
15723       
15724       emit_set_insn (lr, plus_constant (lr, -4));
15725     }
15726
15727   if (live_regs_mask)
15728     {
15729       saved_regs += bit_count (live_regs_mask) * 4;
15730       if (optimize_size && !frame_pointer_needed
15731           && saved_regs == offsets->saved_regs - offsets->saved_args)
15732         {
15733           /* If no coprocessor registers are being pushed and we don't have
15734              to worry about a frame pointer then push extra registers to
15735              create the stack frame.  This is done is a way that does not
15736              alter the frame layout, so is independent of the epilogue.  */
15737           int n;
15738           int frame;
15739           n = 0;
15740           while (n < 8 && (live_regs_mask & (1 << n)) == 0)
15741             n++;
15742           frame = offsets->outgoing_args - (offsets->saved_args + saved_regs);
15743           if (frame && n * 4 >= frame)
15744             {
15745               n = frame / 4;
15746               live_regs_mask |= (1 << n) - 1;
15747               saved_regs += frame;
15748             }
15749         }
15750       insn = emit_multi_reg_push (live_regs_mask);
15751       RTX_FRAME_RELATED_P (insn) = 1;
15752     }
15753
15754   if (! IS_VOLATILE (func_type))
15755     saved_regs += arm_save_coproc_regs ();
15756
15757   if (frame_pointer_needed && TARGET_ARM)
15758     {
15759       /* Create the new frame pointer.  */
15760       if (TARGET_APCS_FRAME)
15761         {
15762           insn = GEN_INT (-(4 + args_to_push + fp_offset));
15763           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
15764           RTX_FRAME_RELATED_P (insn) = 1;
15765
15766           if (IS_NESTED (func_type))
15767             {
15768               /* Recover the static chain register.  */
15769               if (!df_regs_ever_live_p (3)
15770                   || saved_pretend_args)
15771                 insn = gen_rtx_REG (SImode, 3);
15772               else /* if (crtl->args.pretend_args_size == 0) */
15773                 {
15774                   insn = plus_constant (hard_frame_pointer_rtx, 4);
15775                   insn = gen_frame_mem (SImode, insn);
15776                 }
15777               emit_set_insn (ip_rtx, insn);
15778               /* Add a USE to stop propagate_one_insn() from barfing.  */
15779               emit_insn (gen_prologue_use (ip_rtx));
15780             }
15781         }
15782       else
15783         {
15784           insn = GEN_INT (saved_regs - 4);
15785           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15786                                         stack_pointer_rtx, insn));
15787           RTX_FRAME_RELATED_P (insn) = 1;
15788         }
15789     }
15790
15791   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
15792     {
15793       /* This add can produce multiple insns for a large constant, so we
15794          need to get tricky.  */
15795       rtx last = get_last_insn ();
15796
15797       amount = GEN_INT (offsets->saved_args + saved_regs
15798                         - offsets->outgoing_args);
15799
15800       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
15801                                     amount));
15802       do
15803         {
15804           last = last ? NEXT_INSN (last) : get_insns ();
15805           RTX_FRAME_RELATED_P (last) = 1;
15806         }
15807       while (last != insn);
15808
15809       /* If the frame pointer is needed, emit a special barrier that
15810          will prevent the scheduler from moving stores to the frame
15811          before the stack adjustment.  */
15812       if (frame_pointer_needed)
15813         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
15814                                          hard_frame_pointer_rtx));
15815     }
15816
15817
15818   if (frame_pointer_needed && TARGET_THUMB2)
15819     thumb_set_frame_pointer (offsets);
15820
15821   if (flag_pic && arm_pic_register != INVALID_REGNUM)
15822     {
15823       unsigned long mask;
15824
15825       mask = live_regs_mask;
15826       mask &= THUMB2_WORK_REGS;
15827       if (!IS_NESTED (func_type))
15828         mask |= (1 << IP_REGNUM);
15829       arm_load_pic_register (mask);
15830     }
15831
15832   /* If we are profiling, make sure no instructions are scheduled before
15833      the call to mcount.  Similarly if the user has requested no
15834      scheduling in the prolog.  Similarly if we want non-call exceptions
15835      using the EABI unwinder, to prevent faulting instructions from being
15836      swapped with a stack adjustment.  */
15837   if (crtl->profile || !TARGET_SCHED_PROLOG
15838       || (arm_except_unwind_info (&global_options) == UI_TARGET
15839           && cfun->can_throw_non_call_exceptions))
15840     emit_insn (gen_blockage ());
15841
15842   /* If the link register is being kept alive, with the return address in it,
15843      then make sure that it does not get reused by the ce2 pass.  */
15844   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
15845     cfun->machine->lr_save_eliminated = 1;
15846 }
15847 \f
15848 /* Print condition code to STREAM.  Helper function for arm_print_operand.  */
15849 static void
15850 arm_print_condition (FILE *stream)
15851 {
15852   if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
15853     {
15854       /* Branch conversion is not implemented for Thumb-2.  */
15855       if (TARGET_THUMB)
15856         {
15857           output_operand_lossage ("predicated Thumb instruction");
15858           return;
15859         }
15860       if (current_insn_predicate != NULL)
15861         {
15862           output_operand_lossage
15863             ("predicated instruction in conditional sequence");
15864           return;
15865         }
15866
15867       fputs (arm_condition_codes[arm_current_cc], stream);
15868     }
15869   else if (current_insn_predicate)
15870     {
15871       enum arm_cond_code code;
15872
15873       if (TARGET_THUMB1)
15874         {
15875           output_operand_lossage ("predicated Thumb instruction");
15876           return;
15877         }
15878
15879       code = get_arm_condition_code (current_insn_predicate);
15880       fputs (arm_condition_codes[code], stream);
15881     }
15882 }
15883
15884
15885 /* If CODE is 'd', then the X is a condition operand and the instruction
15886    should only be executed if the condition is true.
15887    if CODE is 'D', then the X is a condition operand and the instruction
15888    should only be executed if the condition is false: however, if the mode
15889    of the comparison is CCFPEmode, then always execute the instruction -- we
15890    do this because in these circumstances !GE does not necessarily imply LT;
15891    in these cases the instruction pattern will take care to make sure that
15892    an instruction containing %d will follow, thereby undoing the effects of
15893    doing this instruction unconditionally.
15894    If CODE is 'N' then X is a floating point operand that must be negated
15895    before output.
15896    If CODE is 'B' then output a bitwise inverted value of X (a const int).
15897    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
15898 static void
15899 arm_print_operand (FILE *stream, rtx x, int code)
15900 {
15901   switch (code)
15902     {
15903     case '@':
15904       fputs (ASM_COMMENT_START, stream);
15905       return;
15906
15907     case '_':
15908       fputs (user_label_prefix, stream);
15909       return;
15910
15911     case '|':
15912       fputs (REGISTER_PREFIX, stream);
15913       return;
15914
15915     case '?':
15916       arm_print_condition (stream);
15917       return;
15918
15919     case '(':
15920       /* Nothing in unified syntax, otherwise the current condition code.  */
15921       if (!TARGET_UNIFIED_ASM)
15922         arm_print_condition (stream);
15923       break;
15924
15925     case ')':
15926       /* The current condition code in unified syntax, otherwise nothing.  */
15927       if (TARGET_UNIFIED_ASM)
15928         arm_print_condition (stream);
15929       break;
15930   
15931     case '.':
15932       /* The current condition code for a condition code setting instruction.
15933          Preceded by 's' in unified syntax, otherwise followed by 's'.  */
15934       if (TARGET_UNIFIED_ASM)
15935         {
15936           fputc('s', stream);
15937           arm_print_condition (stream);
15938         }
15939       else
15940         {
15941           arm_print_condition (stream);
15942           fputc('s', stream);
15943         }
15944       return;
15945
15946     case '!':
15947       /* If the instruction is conditionally executed then print
15948          the current condition code, otherwise print 's'.  */
15949       gcc_assert (TARGET_THUMB2 && TARGET_UNIFIED_ASM);
15950       if (current_insn_predicate)
15951         arm_print_condition (stream);
15952       else
15953         fputc('s', stream);
15954       break;
15955
15956     /* %# is a "break" sequence. It doesn't output anything, but is used to
15957        separate e.g. operand numbers from following text, if that text consists
15958        of further digits which we don't want to be part of the operand
15959        number.  */
15960     case '#':
15961       return;
15962
15963     case 'N':
15964       {
15965         REAL_VALUE_TYPE r;
15966         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
15967         r = real_value_negate (&r);
15968         fprintf (stream, "%s", fp_const_from_val (&r));
15969       }
15970       return;
15971
15972     /* An integer or symbol address without a preceding # sign.  */
15973     case 'c':
15974       switch (GET_CODE (x))
15975         {
15976         case CONST_INT:
15977           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
15978           break;
15979
15980         case SYMBOL_REF:
15981           output_addr_const (stream, x);
15982           break;
15983
15984         default:
15985           gcc_unreachable ();
15986         }
15987       return;
15988
15989     case 'B':
15990       if (GET_CODE (x) == CONST_INT)
15991         {
15992           HOST_WIDE_INT val;
15993           val = ARM_SIGN_EXTEND (~INTVAL (x));
15994           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
15995         }
15996       else
15997         {
15998           putc ('~', stream);
15999           output_addr_const (stream, x);
16000         }
16001       return;
16002
16003     case 'L':
16004       /* The low 16 bits of an immediate constant.  */
16005       fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL(x) & 0xffff);
16006       return;
16007
16008     case 'i':
16009       fprintf (stream, "%s", arithmetic_instr (x, 1));
16010       return;
16011
16012     /* Truncate Cirrus shift counts.  */
16013     case 's':
16014       if (GET_CODE (x) == CONST_INT)
16015         {
16016           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
16017           return;
16018         }
16019       arm_print_operand (stream, x, 0);
16020       return;
16021
16022     case 'I':
16023       fprintf (stream, "%s", arithmetic_instr (x, 0));
16024       return;
16025
16026     case 'S':
16027       {
16028         HOST_WIDE_INT val;
16029         const char *shift;
16030
16031         if (!shift_operator (x, SImode))
16032           {
16033             output_operand_lossage ("invalid shift operand");
16034             break;
16035           }
16036
16037         shift = shift_op (x, &val);
16038
16039         if (shift)
16040           {
16041             fprintf (stream, ", %s ", shift);
16042             if (val == -1)
16043               arm_print_operand (stream, XEXP (x, 1), 0);
16044             else
16045               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
16046           }
16047       }
16048       return;
16049
16050       /* An explanation of the 'Q', 'R' and 'H' register operands:
16051
16052          In a pair of registers containing a DI or DF value the 'Q'
16053          operand returns the register number of the register containing
16054          the least significant part of the value.  The 'R' operand returns
16055          the register number of the register containing the most
16056          significant part of the value.
16057
16058          The 'H' operand returns the higher of the two register numbers.
16059          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
16060          same as the 'Q' operand, since the most significant part of the
16061          value is held in the lower number register.  The reverse is true
16062          on systems where WORDS_BIG_ENDIAN is false.
16063
16064          The purpose of these operands is to distinguish between cases
16065          where the endian-ness of the values is important (for example
16066          when they are added together), and cases where the endian-ness
16067          is irrelevant, but the order of register operations is important.
16068          For example when loading a value from memory into a register
16069          pair, the endian-ness does not matter.  Provided that the value
16070          from the lower memory address is put into the lower numbered
16071          register, and the value from the higher address is put into the
16072          higher numbered register, the load will work regardless of whether
16073          the value being loaded is big-wordian or little-wordian.  The
16074          order of the two register loads can matter however, if the address
16075          of the memory location is actually held in one of the registers
16076          being overwritten by the load.
16077
16078          The 'Q' and 'R' constraints are also available for 64-bit
16079          constants.  */
16080     case 'Q':
16081       if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
16082         {
16083           rtx part = gen_lowpart (SImode, x);
16084           fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, INTVAL (part));
16085           return;
16086         }
16087
16088       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16089         {
16090           output_operand_lossage ("invalid operand for code '%c'", code);
16091           return;
16092         }
16093
16094       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
16095       return;
16096
16097     case 'R':
16098       if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
16099         {
16100           enum machine_mode mode = GET_MODE (x);
16101           rtx part;
16102
16103           if (mode == VOIDmode)
16104             mode = DImode;
16105           part = gen_highpart_mode (SImode, mode, x);
16106           fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, INTVAL (part));
16107           return;
16108         }
16109
16110       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16111         {
16112           output_operand_lossage ("invalid operand for code '%c'", code);
16113           return;
16114         }
16115
16116       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
16117       return;
16118
16119     case 'H':
16120       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16121         {
16122           output_operand_lossage ("invalid operand for code '%c'", code);
16123           return;
16124         }
16125
16126       asm_fprintf (stream, "%r", REGNO (x) + 1);
16127       return;
16128
16129     case 'J':
16130       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16131         {
16132           output_operand_lossage ("invalid operand for code '%c'", code);
16133           return;
16134         }
16135
16136       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 3 : 2));
16137       return;
16138
16139     case 'K':
16140       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16141         {
16142           output_operand_lossage ("invalid operand for code '%c'", code);
16143           return;
16144         }
16145
16146       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 2 : 3));
16147       return;
16148
16149     case 'm':
16150       asm_fprintf (stream, "%r",
16151                    GET_CODE (XEXP (x, 0)) == REG
16152                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
16153       return;
16154
16155     case 'M':
16156       asm_fprintf (stream, "{%r-%r}",
16157                    REGNO (x),
16158                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
16159       return;
16160
16161     /* Like 'M', but writing doubleword vector registers, for use by Neon
16162        insns.  */
16163     case 'h':
16164       {
16165         int regno = (REGNO (x) - FIRST_VFP_REGNUM) / 2;
16166         int numregs = ARM_NUM_REGS (GET_MODE (x)) / 2;
16167         if (numregs == 1)
16168           asm_fprintf (stream, "{d%d}", regno);
16169         else
16170           asm_fprintf (stream, "{d%d-d%d}", regno, regno + numregs - 1);
16171       }
16172       return;
16173
16174     case 'd':
16175       /* CONST_TRUE_RTX means always -- that's the default.  */
16176       if (x == const_true_rtx)
16177         return;
16178
16179       if (!COMPARISON_P (x))
16180         {
16181           output_operand_lossage ("invalid operand for code '%c'", code);
16182           return;
16183         }
16184
16185       fputs (arm_condition_codes[get_arm_condition_code (x)],
16186              stream);
16187       return;
16188
16189     case 'D':
16190       /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
16191          want to do that.  */
16192       if (x == const_true_rtx)
16193         {
16194           output_operand_lossage ("instruction never executed");
16195           return;
16196         }
16197       if (!COMPARISON_P (x))
16198         {
16199           output_operand_lossage ("invalid operand for code '%c'", code);
16200           return;
16201         }
16202
16203       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
16204                                  (get_arm_condition_code (x))],
16205              stream);
16206       return;
16207
16208     /* Cirrus registers can be accessed in a variety of ways:
16209          single floating point (f)
16210          double floating point (d)
16211          32bit integer         (fx)
16212          64bit integer         (dx).  */
16213     case 'W':                   /* Cirrus register in F mode.  */
16214     case 'X':                   /* Cirrus register in D mode.  */
16215     case 'Y':                   /* Cirrus register in FX mode.  */
16216     case 'Z':                   /* Cirrus register in DX mode.  */
16217       gcc_assert (GET_CODE (x) == REG
16218                   && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
16219
16220       fprintf (stream, "mv%s%s",
16221                code == 'W' ? "f"
16222                : code == 'X' ? "d"
16223                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
16224
16225       return;
16226
16227     /* Print cirrus register in the mode specified by the register's mode.  */
16228     case 'V':
16229       {
16230         int mode = GET_MODE (x);
16231
16232         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
16233           {
16234             output_operand_lossage ("invalid operand for code '%c'", code);
16235             return;
16236           }
16237
16238         fprintf (stream, "mv%s%s",
16239                  mode == DFmode ? "d"
16240                  : mode == SImode ? "fx"
16241                  : mode == DImode ? "dx"
16242                  : "f", reg_names[REGNO (x)] + 2);
16243
16244         return;
16245       }
16246
16247     case 'U':
16248       if (GET_CODE (x) != REG
16249           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
16250           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
16251         /* Bad value for wCG register number.  */
16252         {
16253           output_operand_lossage ("invalid operand for code '%c'", code);
16254           return;
16255         }
16256
16257       else
16258         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
16259       return;
16260
16261       /* Print an iWMMXt control register name.  */
16262     case 'w':
16263       if (GET_CODE (x) != CONST_INT
16264           || INTVAL (x) < 0
16265           || INTVAL (x) >= 16)
16266         /* Bad value for wC register number.  */
16267         {
16268           output_operand_lossage ("invalid operand for code '%c'", code);
16269           return;
16270         }
16271
16272       else
16273         {
16274           static const char * wc_reg_names [16] =
16275             {
16276               "wCID",  "wCon",  "wCSSF", "wCASF",
16277               "wC4",   "wC5",   "wC6",   "wC7",
16278               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
16279               "wC12",  "wC13",  "wC14",  "wC15"
16280             };
16281
16282           fprintf (stream, wc_reg_names [INTVAL (x)]);
16283         }
16284       return;
16285
16286     /* Print the high single-precision register of a VFP double-precision
16287        register.  */
16288     case 'p':
16289       {
16290         int mode = GET_MODE (x);
16291         int regno;
16292
16293         if (GET_MODE_SIZE (mode) != 8 || GET_CODE (x) != REG)
16294           {
16295             output_operand_lossage ("invalid operand for code '%c'", code);
16296             return;
16297           }
16298
16299         regno = REGNO (x);
16300         if (!VFP_REGNO_OK_FOR_DOUBLE (regno))
16301           {
16302             output_operand_lossage ("invalid operand for code '%c'", code);
16303             return;
16304           }
16305
16306         fprintf (stream, "s%d", regno - FIRST_VFP_REGNUM + 1);
16307       }
16308       return;
16309
16310     /* Print a VFP/Neon double precision or quad precision register name.  */
16311     case 'P':
16312     case 'q':
16313       {
16314         int mode = GET_MODE (x);
16315         int is_quad = (code == 'q');
16316         int regno;
16317
16318         if (GET_MODE_SIZE (mode) != (is_quad ? 16 : 8))
16319           {
16320             output_operand_lossage ("invalid operand for code '%c'", code);
16321             return;
16322           }
16323
16324         if (GET_CODE (x) != REG
16325             || !IS_VFP_REGNUM (REGNO (x)))
16326           {
16327             output_operand_lossage ("invalid operand for code '%c'", code);
16328             return;
16329           }
16330
16331         regno = REGNO (x);
16332         if ((is_quad && !NEON_REGNO_OK_FOR_QUAD (regno))
16333             || (!is_quad && !VFP_REGNO_OK_FOR_DOUBLE (regno)))
16334           {
16335             output_operand_lossage ("invalid operand for code '%c'", code);
16336             return;
16337           }
16338
16339         fprintf (stream, "%c%d", is_quad ? 'q' : 'd',
16340           (regno - FIRST_VFP_REGNUM) >> (is_quad ? 2 : 1));
16341       }
16342       return;
16343
16344     /* These two codes print the low/high doubleword register of a Neon quad
16345        register, respectively.  For pair-structure types, can also print
16346        low/high quadword registers.  */
16347     case 'e':
16348     case 'f':
16349       {
16350         int mode = GET_MODE (x);
16351         int regno;
16352
16353         if ((GET_MODE_SIZE (mode) != 16
16354              && GET_MODE_SIZE (mode) != 32) || GET_CODE (x) != REG)
16355           {
16356             output_operand_lossage ("invalid operand for code '%c'", code);
16357             return;
16358           }
16359
16360         regno = REGNO (x);
16361         if (!NEON_REGNO_OK_FOR_QUAD (regno))
16362           {
16363             output_operand_lossage ("invalid operand for code '%c'", code);
16364             return;
16365           }
16366
16367         if (GET_MODE_SIZE (mode) == 16)
16368           fprintf (stream, "d%d", ((regno - FIRST_VFP_REGNUM) >> 1)
16369                                   + (code == 'f' ? 1 : 0));
16370         else
16371           fprintf (stream, "q%d", ((regno - FIRST_VFP_REGNUM) >> 2)
16372                                   + (code == 'f' ? 1 : 0));
16373       }
16374       return;
16375
16376     /* Print a VFPv3 floating-point constant, represented as an integer
16377        index.  */
16378     case 'G':
16379       {
16380         int index = vfp3_const_double_index (x);
16381         gcc_assert (index != -1);
16382         fprintf (stream, "%d", index);
16383       }
16384       return;
16385
16386     /* Print bits representing opcode features for Neon.
16387
16388        Bit 0 is 1 for signed, 0 for unsigned.  Floats count as signed
16389        and polynomials as unsigned.
16390
16391        Bit 1 is 1 for floats and polynomials, 0 for ordinary integers.
16392
16393        Bit 2 is 1 for rounding functions, 0 otherwise.  */
16394
16395     /* Identify the type as 's', 'u', 'p' or 'f'.  */
16396     case 'T':
16397       {
16398         HOST_WIDE_INT bits = INTVAL (x);
16399         fputc ("uspf"[bits & 3], stream);
16400       }
16401       return;
16402
16403     /* Likewise, but signed and unsigned integers are both 'i'.  */
16404     case 'F':
16405       {
16406         HOST_WIDE_INT bits = INTVAL (x);
16407         fputc ("iipf"[bits & 3], stream);
16408       }
16409       return;
16410
16411     /* As for 'T', but emit 'u' instead of 'p'.  */
16412     case 't':
16413       {
16414         HOST_WIDE_INT bits = INTVAL (x);
16415         fputc ("usuf"[bits & 3], stream);
16416       }
16417       return;
16418
16419     /* Bit 2: rounding (vs none).  */
16420     case 'O':
16421       {
16422         HOST_WIDE_INT bits = INTVAL (x);
16423         fputs ((bits & 4) != 0 ? "r" : "", stream);
16424       }
16425       return;
16426
16427     /* Memory operand for vld1/vst1 instruction.  */
16428     case 'A':
16429       {
16430         rtx addr;
16431         bool postinc = FALSE;
16432         unsigned align, modesize, align_bits;
16433
16434         gcc_assert (GET_CODE (x) == MEM);
16435         addr = XEXP (x, 0);
16436         if (GET_CODE (addr) == POST_INC)
16437           {
16438             postinc = 1;
16439             addr = XEXP (addr, 0);
16440           }
16441         asm_fprintf (stream, "[%r", REGNO (addr));
16442
16443         /* We know the alignment of this access, so we can emit a hint in the
16444            instruction (for some alignments) as an aid to the memory subsystem
16445            of the target.  */
16446         align = MEM_ALIGN (x) >> 3;
16447         modesize = GET_MODE_SIZE (GET_MODE (x));
16448         
16449         /* Only certain alignment specifiers are supported by the hardware.  */
16450         if (modesize == 16 && (align % 32) == 0)
16451           align_bits = 256;
16452         else if ((modesize == 8 || modesize == 16) && (align % 16) == 0)
16453           align_bits = 128;
16454         else if ((align % 8) == 0)
16455           align_bits = 64;
16456         else
16457           align_bits = 0;
16458         
16459         if (align_bits != 0)
16460           asm_fprintf (stream, ":%d", align_bits);
16461
16462         asm_fprintf (stream, "]");
16463
16464         if (postinc)
16465           fputs("!", stream);
16466       }
16467       return;
16468
16469     case 'C':
16470       {
16471         rtx addr;
16472
16473         gcc_assert (GET_CODE (x) == MEM);
16474         addr = XEXP (x, 0);
16475         gcc_assert (GET_CODE (addr) == REG);
16476         asm_fprintf (stream, "[%r]", REGNO (addr));
16477       }
16478       return;
16479
16480     /* Translate an S register number into a D register number and element index.  */
16481     case 'y':
16482       {
16483         int mode = GET_MODE (x);
16484         int regno;
16485
16486         if (GET_MODE_SIZE (mode) != 4 || GET_CODE (x) != REG)
16487           {
16488             output_operand_lossage ("invalid operand for code '%c'", code);
16489             return;
16490           }
16491
16492         regno = REGNO (x);
16493         if (!VFP_REGNO_OK_FOR_SINGLE (regno))
16494           {
16495             output_operand_lossage ("invalid operand for code '%c'", code);
16496             return;
16497           }
16498
16499         regno = regno - FIRST_VFP_REGNUM;
16500         fprintf (stream, "d%d[%d]", regno / 2, regno % 2);
16501       }
16502       return;
16503
16504     /* Register specifier for vld1.16/vst1.16.  Translate the S register
16505        number into a D register number and element index.  */
16506     case 'z':
16507       {
16508         int mode = GET_MODE (x);
16509         int regno;
16510
16511         if (GET_MODE_SIZE (mode) != 2 || GET_CODE (x) != REG)
16512           {
16513             output_operand_lossage ("invalid operand for code '%c'", code);
16514             return;
16515           }
16516
16517         regno = REGNO (x);
16518         if (!VFP_REGNO_OK_FOR_SINGLE (regno))
16519           {
16520             output_operand_lossage ("invalid operand for code '%c'", code);
16521             return;
16522           }
16523
16524         regno = regno - FIRST_VFP_REGNUM;
16525         fprintf (stream, "d%d[%d]", regno/2, ((regno % 2) ? 2 : 0));
16526       }
16527       return;
16528       
16529     default:
16530       if (x == 0)
16531         {
16532           output_operand_lossage ("missing operand");
16533           return;
16534         }
16535
16536       switch (GET_CODE (x))
16537         {
16538         case REG:
16539           asm_fprintf (stream, "%r", REGNO (x));
16540           break;
16541
16542         case MEM:
16543           output_memory_reference_mode = GET_MODE (x);
16544           output_address (XEXP (x, 0));
16545           break;
16546
16547         case CONST_DOUBLE:
16548           if (TARGET_NEON)
16549             {
16550               char fpstr[20];
16551               real_to_decimal (fpstr, CONST_DOUBLE_REAL_VALUE (x),
16552                                sizeof (fpstr), 0, 1);
16553               fprintf (stream, "#%s", fpstr);
16554             }
16555           else
16556             fprintf (stream, "#%s", fp_immediate_constant (x));
16557           break;
16558
16559         default:
16560           gcc_assert (GET_CODE (x) != NEG);
16561           fputc ('#', stream);
16562           if (GET_CODE (x) == HIGH)
16563             {
16564               fputs (":lower16:", stream);
16565               x = XEXP (x, 0);
16566             }
16567             
16568           output_addr_const (stream, x);
16569           break;
16570         }
16571     }
16572 }
16573 \f
16574 /* Target hook for printing a memory address.  */
16575 static void
16576 arm_print_operand_address (FILE *stream, rtx x)
16577 {
16578   if (TARGET_32BIT)
16579     {
16580       int is_minus = GET_CODE (x) == MINUS;
16581
16582       if (GET_CODE (x) == REG)
16583         asm_fprintf (stream, "[%r, #0]", REGNO (x));
16584       else if (GET_CODE (x) == PLUS || is_minus)
16585         {
16586           rtx base = XEXP (x, 0);
16587           rtx index = XEXP (x, 1);
16588           HOST_WIDE_INT offset = 0;
16589           if (GET_CODE (base) != REG
16590               || (GET_CODE (index) == REG && REGNO (index) == SP_REGNUM))
16591             {
16592               /* Ensure that BASE is a register.  */
16593               /* (one of them must be).  */
16594               /* Also ensure the SP is not used as in index register.  */
16595               rtx temp = base;
16596               base = index;
16597               index = temp;
16598             }
16599           switch (GET_CODE (index))
16600             {
16601             case CONST_INT:
16602               offset = INTVAL (index);
16603               if (is_minus)
16604                 offset = -offset;
16605               asm_fprintf (stream, "[%r, #%wd]",
16606                            REGNO (base), offset);
16607               break;
16608
16609             case REG:
16610               asm_fprintf (stream, "[%r, %s%r]",
16611                            REGNO (base), is_minus ? "-" : "",
16612                            REGNO (index));
16613               break;
16614
16615             case MULT:
16616             case ASHIFTRT:
16617             case LSHIFTRT:
16618             case ASHIFT:
16619             case ROTATERT:
16620               {
16621                 asm_fprintf (stream, "[%r, %s%r",
16622                              REGNO (base), is_minus ? "-" : "",
16623                              REGNO (XEXP (index, 0)));
16624                 arm_print_operand (stream, index, 'S');
16625                 fputs ("]", stream);
16626                 break;
16627               }
16628
16629             default:
16630               gcc_unreachable ();
16631             }
16632         }
16633       else if (GET_CODE (x) == PRE_INC || GET_CODE (x) == POST_INC
16634                || GET_CODE (x) == PRE_DEC || GET_CODE (x) == POST_DEC)
16635         {
16636           extern enum machine_mode output_memory_reference_mode;
16637
16638           gcc_assert (GET_CODE (XEXP (x, 0)) == REG);
16639
16640           if (GET_CODE (x) == PRE_DEC || GET_CODE (x) == PRE_INC)
16641             asm_fprintf (stream, "[%r, #%s%d]!",
16642                          REGNO (XEXP (x, 0)),
16643                          GET_CODE (x) == PRE_DEC ? "-" : "",
16644                          GET_MODE_SIZE (output_memory_reference_mode));
16645           else
16646             asm_fprintf (stream, "[%r], #%s%d",
16647                          REGNO (XEXP (x, 0)),
16648                          GET_CODE (x) == POST_DEC ? "-" : "",
16649                          GET_MODE_SIZE (output_memory_reference_mode));
16650         }
16651       else if (GET_CODE (x) == PRE_MODIFY)
16652         {
16653           asm_fprintf (stream, "[%r, ", REGNO (XEXP (x, 0)));
16654           if (GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
16655             asm_fprintf (stream, "#%wd]!",
16656                          INTVAL (XEXP (XEXP (x, 1), 1)));
16657           else
16658             asm_fprintf (stream, "%r]!",
16659                          REGNO (XEXP (XEXP (x, 1), 1)));
16660         }
16661       else if (GET_CODE (x) == POST_MODIFY)
16662         {
16663           asm_fprintf (stream, "[%r], ", REGNO (XEXP (x, 0)));
16664           if (GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
16665             asm_fprintf (stream, "#%wd",
16666                          INTVAL (XEXP (XEXP (x, 1), 1)));
16667           else
16668             asm_fprintf (stream, "%r",
16669                          REGNO (XEXP (XEXP (x, 1), 1)));
16670         }
16671       else output_addr_const (stream, x);
16672     }
16673   else
16674     {
16675       if (GET_CODE (x) == REG)
16676         asm_fprintf (stream, "[%r]", REGNO (x));
16677       else if (GET_CODE (x) == POST_INC)
16678         asm_fprintf (stream, "%r!", REGNO (XEXP (x, 0)));
16679       else if (GET_CODE (x) == PLUS)
16680         {
16681           gcc_assert (GET_CODE (XEXP (x, 0)) == REG);
16682           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
16683             asm_fprintf (stream, "[%r, #%wd]",
16684                          REGNO (XEXP (x, 0)),
16685                          INTVAL (XEXP (x, 1)));
16686           else
16687             asm_fprintf (stream, "[%r, %r]",
16688                          REGNO (XEXP (x, 0)),
16689                          REGNO (XEXP (x, 1)));
16690         }
16691       else
16692         output_addr_const (stream, x);
16693     }
16694 }
16695 \f
16696 /* Target hook for indicating whether a punctuation character for
16697    TARGET_PRINT_OPERAND is valid.  */
16698 static bool
16699 arm_print_operand_punct_valid_p (unsigned char code)
16700 {
16701   return (code == '@' || code == '|' || code == '.'
16702           || code == '(' || code == ')' || code == '#'
16703           || (TARGET_32BIT && (code == '?'))
16704           || (TARGET_THUMB2 && (code == '!'))
16705           || (TARGET_THUMB && (code == '_')));
16706 }
16707 \f
16708 /* Target hook for assembling integer objects.  The ARM version needs to
16709    handle word-sized values specially.  */
16710 static bool
16711 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
16712 {
16713   enum machine_mode mode;
16714
16715   if (size == UNITS_PER_WORD && aligned_p)
16716     {
16717       fputs ("\t.word\t", asm_out_file);
16718       output_addr_const (asm_out_file, x);
16719
16720       /* Mark symbols as position independent.  We only do this in the
16721          .text segment, not in the .data segment.  */
16722       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
16723           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
16724         {
16725           /* See legitimize_pic_address for an explanation of the
16726              TARGET_VXWORKS_RTP check.  */
16727           if (TARGET_VXWORKS_RTP
16728               || (GET_CODE (x) == SYMBOL_REF && !SYMBOL_REF_LOCAL_P (x)))
16729             fputs ("(GOT)", asm_out_file);
16730           else
16731             fputs ("(GOTOFF)", asm_out_file);
16732         }
16733       fputc ('\n', asm_out_file);
16734       return true;
16735     }
16736
16737   mode = GET_MODE (x);
16738
16739   if (arm_vector_mode_supported_p (mode))
16740     {
16741       int i, units;
16742
16743       gcc_assert (GET_CODE (x) == CONST_VECTOR);
16744
16745       units = CONST_VECTOR_NUNITS (x);
16746       size = GET_MODE_SIZE (GET_MODE_INNER (mode));
16747
16748       if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
16749         for (i = 0; i < units; i++)
16750           {
16751             rtx elt = CONST_VECTOR_ELT (x, i);
16752             assemble_integer
16753               (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
16754           }
16755       else
16756         for (i = 0; i < units; i++)
16757           {
16758             rtx elt = CONST_VECTOR_ELT (x, i);
16759             REAL_VALUE_TYPE rval;
16760
16761             REAL_VALUE_FROM_CONST_DOUBLE (rval, elt);
16762
16763             assemble_real
16764               (rval, GET_MODE_INNER (mode),
16765               i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT);
16766           }
16767
16768       return true;
16769     }
16770
16771   return default_assemble_integer (x, size, aligned_p);
16772 }
16773
16774 static void
16775 arm_elf_asm_cdtor (rtx symbol, int priority, bool is_ctor)
16776 {
16777   section *s;
16778
16779   if (!TARGET_AAPCS_BASED)
16780     {
16781       (is_ctor ? 
16782        default_named_section_asm_out_constructor 
16783        : default_named_section_asm_out_destructor) (symbol, priority);
16784       return;
16785     }
16786
16787   /* Put these in the .init_array section, using a special relocation.  */
16788   if (priority != DEFAULT_INIT_PRIORITY)
16789     {
16790       char buf[18];
16791       sprintf (buf, "%s.%.5u", 
16792                is_ctor ? ".init_array" : ".fini_array",
16793                priority);
16794       s = get_section (buf, SECTION_WRITE, NULL_TREE);
16795     }
16796   else if (is_ctor)
16797     s = ctors_section;
16798   else
16799     s = dtors_section;
16800
16801   switch_to_section (s);
16802   assemble_align (POINTER_SIZE);
16803   fputs ("\t.word\t", asm_out_file);
16804   output_addr_const (asm_out_file, symbol);
16805   fputs ("(target1)\n", asm_out_file);
16806 }
16807
16808 /* Add a function to the list of static constructors.  */
16809
16810 static void
16811 arm_elf_asm_constructor (rtx symbol, int priority)
16812 {
16813   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/true);
16814 }
16815
16816 /* Add a function to the list of static destructors.  */
16817
16818 static void
16819 arm_elf_asm_destructor (rtx symbol, int priority)
16820 {
16821   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/false);
16822 }
16823 \f
16824 /* A finite state machine takes care of noticing whether or not instructions
16825    can be conditionally executed, and thus decrease execution time and code
16826    size by deleting branch instructions.  The fsm is controlled by
16827    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
16828
16829 /* The state of the fsm controlling condition codes are:
16830    0: normal, do nothing special
16831    1: make ASM_OUTPUT_OPCODE not output this instruction
16832    2: make ASM_OUTPUT_OPCODE not output this instruction
16833    3: make instructions conditional
16834    4: make instructions conditional
16835
16836    State transitions (state->state by whom under condition):
16837    0 -> 1 final_prescan_insn if the `target' is a label
16838    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
16839    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
16840    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
16841    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
16842           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
16843    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
16844           (the target insn is arm_target_insn).
16845
16846    If the jump clobbers the conditions then we use states 2 and 4.
16847
16848    A similar thing can be done with conditional return insns.
16849
16850    XXX In case the `target' is an unconditional branch, this conditionalising
16851    of the instructions always reduces code size, but not always execution
16852    time.  But then, I want to reduce the code size to somewhere near what
16853    /bin/cc produces.  */
16854
16855 /* In addition to this, state is maintained for Thumb-2 COND_EXEC
16856    instructions.  When a COND_EXEC instruction is seen the subsequent
16857    instructions are scanned so that multiple conditional instructions can be
16858    combined into a single IT block.  arm_condexec_count and arm_condexec_mask
16859    specify the length and true/false mask for the IT block.  These will be
16860    decremented/zeroed by arm_asm_output_opcode as the insns are output.  */
16861
16862 /* Returns the index of the ARM condition code string in
16863    `arm_condition_codes'.  COMPARISON should be an rtx like
16864    `(eq (...) (...))'.  */
16865 static enum arm_cond_code
16866 get_arm_condition_code (rtx comparison)
16867 {
16868   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
16869   enum arm_cond_code code;
16870   enum rtx_code comp_code = GET_CODE (comparison);
16871
16872   if (GET_MODE_CLASS (mode) != MODE_CC)
16873     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
16874                            XEXP (comparison, 1));
16875
16876   switch (mode)
16877     {
16878     case CC_DNEmode: code = ARM_NE; goto dominance;
16879     case CC_DEQmode: code = ARM_EQ; goto dominance;
16880     case CC_DGEmode: code = ARM_GE; goto dominance;
16881     case CC_DGTmode: code = ARM_GT; goto dominance;
16882     case CC_DLEmode: code = ARM_LE; goto dominance;
16883     case CC_DLTmode: code = ARM_LT; goto dominance;
16884     case CC_DGEUmode: code = ARM_CS; goto dominance;
16885     case CC_DGTUmode: code = ARM_HI; goto dominance;
16886     case CC_DLEUmode: code = ARM_LS; goto dominance;
16887     case CC_DLTUmode: code = ARM_CC;
16888
16889     dominance:
16890       gcc_assert (comp_code == EQ || comp_code == NE);
16891
16892       if (comp_code == EQ)
16893         return ARM_INVERSE_CONDITION_CODE (code);
16894       return code;
16895
16896     case CC_NOOVmode:
16897       switch (comp_code)
16898         {
16899         case NE: return ARM_NE;
16900         case EQ: return ARM_EQ;
16901         case GE: return ARM_PL;
16902         case LT: return ARM_MI;
16903         default: gcc_unreachable ();
16904         }
16905
16906     case CC_Zmode:
16907       switch (comp_code)
16908         {
16909         case NE: return ARM_NE;
16910         case EQ: return ARM_EQ;
16911         default: gcc_unreachable ();
16912         }
16913
16914     case CC_Nmode:
16915       switch (comp_code)
16916         {
16917         case NE: return ARM_MI;
16918         case EQ: return ARM_PL;
16919         default: gcc_unreachable ();
16920         }
16921
16922     case CCFPEmode:
16923     case CCFPmode:
16924       /* These encodings assume that AC=1 in the FPA system control
16925          byte.  This allows us to handle all cases except UNEQ and
16926          LTGT.  */
16927       switch (comp_code)
16928         {
16929         case GE: return ARM_GE;
16930         case GT: return ARM_GT;
16931         case LE: return ARM_LS;
16932         case LT: return ARM_MI;
16933         case NE: return ARM_NE;
16934         case EQ: return ARM_EQ;
16935         case ORDERED: return ARM_VC;
16936         case UNORDERED: return ARM_VS;
16937         case UNLT: return ARM_LT;
16938         case UNLE: return ARM_LE;
16939         case UNGT: return ARM_HI;
16940         case UNGE: return ARM_PL;
16941           /* UNEQ and LTGT do not have a representation.  */
16942         case UNEQ: /* Fall through.  */
16943         case LTGT: /* Fall through.  */
16944         default: gcc_unreachable ();
16945         }
16946
16947     case CC_SWPmode:
16948       switch (comp_code)
16949         {
16950         case NE: return ARM_NE;
16951         case EQ: return ARM_EQ;
16952         case GE: return ARM_LE;
16953         case GT: return ARM_LT;
16954         case LE: return ARM_GE;
16955         case LT: return ARM_GT;
16956         case GEU: return ARM_LS;
16957         case GTU: return ARM_CC;
16958         case LEU: return ARM_CS;
16959         case LTU: return ARM_HI;
16960         default: gcc_unreachable ();
16961         }
16962
16963     case CC_Cmode:
16964       switch (comp_code)
16965         {
16966         case LTU: return ARM_CS;
16967         case GEU: return ARM_CC;
16968         default: gcc_unreachable ();
16969         }
16970
16971     case CC_CZmode:
16972       switch (comp_code)
16973         {
16974         case NE: return ARM_NE;
16975         case EQ: return ARM_EQ;
16976         case GEU: return ARM_CS;
16977         case GTU: return ARM_HI;
16978         case LEU: return ARM_LS;
16979         case LTU: return ARM_CC;
16980         default: gcc_unreachable ();
16981         }
16982
16983     case CC_NCVmode:
16984       switch (comp_code)
16985         {
16986         case GE: return ARM_GE;
16987         case LT: return ARM_LT;
16988         case GEU: return ARM_CS;
16989         case LTU: return ARM_CC;
16990         default: gcc_unreachable ();
16991         }
16992
16993     case CCmode:
16994       switch (comp_code)
16995         {
16996         case NE: return ARM_NE;
16997         case EQ: return ARM_EQ;
16998         case GE: return ARM_GE;
16999         case GT: return ARM_GT;
17000         case LE: return ARM_LE;
17001         case LT: return ARM_LT;
17002         case GEU: return ARM_CS;
17003         case GTU: return ARM_HI;
17004         case LEU: return ARM_LS;
17005         case LTU: return ARM_CC;
17006         default: gcc_unreachable ();
17007         }
17008
17009     default: gcc_unreachable ();
17010     }
17011 }
17012
17013 /* Tell arm_asm_output_opcode to output IT blocks for conditionally executed
17014    instructions.  */
17015 void
17016 thumb2_final_prescan_insn (rtx insn)
17017 {
17018   rtx first_insn = insn;
17019   rtx body = PATTERN (insn);
17020   rtx predicate;
17021   enum arm_cond_code code;
17022   int n;
17023   int mask;
17024
17025   /* Remove the previous insn from the count of insns to be output.  */
17026   if (arm_condexec_count)
17027       arm_condexec_count--;
17028
17029   /* Nothing to do if we are already inside a conditional block.  */
17030   if (arm_condexec_count)
17031     return;
17032
17033   if (GET_CODE (body) != COND_EXEC)
17034     return;
17035
17036   /* Conditional jumps are implemented directly.  */
17037   if (GET_CODE (insn) == JUMP_INSN)
17038     return;
17039
17040   predicate = COND_EXEC_TEST (body);
17041   arm_current_cc = get_arm_condition_code (predicate);
17042
17043   n = get_attr_ce_count (insn);
17044   arm_condexec_count = 1;
17045   arm_condexec_mask = (1 << n) - 1;
17046   arm_condexec_masklen = n;
17047   /* See if subsequent instructions can be combined into the same block.  */
17048   for (;;)
17049     {
17050       insn = next_nonnote_insn (insn);
17051
17052       /* Jumping into the middle of an IT block is illegal, so a label or
17053          barrier terminates the block.  */
17054       if (GET_CODE (insn) != INSN && GET_CODE(insn) != JUMP_INSN)
17055         break;
17056
17057       body = PATTERN (insn);
17058       /* USE and CLOBBER aren't really insns, so just skip them.  */
17059       if (GET_CODE (body) == USE
17060           || GET_CODE (body) == CLOBBER)
17061         continue;
17062
17063       /* ??? Recognize conditional jumps, and combine them with IT blocks.  */
17064       if (GET_CODE (body) != COND_EXEC)
17065         break;
17066       /* Allow up to 4 conditionally executed instructions in a block.  */
17067       n = get_attr_ce_count (insn);
17068       if (arm_condexec_masklen + n > 4)
17069         break;
17070
17071       predicate = COND_EXEC_TEST (body);
17072       code = get_arm_condition_code (predicate);
17073       mask = (1 << n) - 1;
17074       if (arm_current_cc == code)
17075         arm_condexec_mask |= (mask << arm_condexec_masklen);
17076       else if (arm_current_cc != ARM_INVERSE_CONDITION_CODE(code))
17077         break;
17078
17079       arm_condexec_count++;
17080       arm_condexec_masklen += n;
17081
17082       /* A jump must be the last instruction in a conditional block.  */
17083       if (GET_CODE(insn) == JUMP_INSN)
17084         break;
17085     }
17086   /* Restore recog_data (getting the attributes of other insns can
17087      destroy this array, but final.c assumes that it remains intact
17088      across this call).  */
17089   extract_constrain_insn_cached (first_insn);
17090 }
17091
17092 void
17093 arm_final_prescan_insn (rtx insn)
17094 {
17095   /* BODY will hold the body of INSN.  */
17096   rtx body = PATTERN (insn);
17097
17098   /* This will be 1 if trying to repeat the trick, and things need to be
17099      reversed if it appears to fail.  */
17100   int reverse = 0;
17101
17102   /* If we start with a return insn, we only succeed if we find another one.  */
17103   int seeking_return = 0;
17104
17105   /* START_INSN will hold the insn from where we start looking.  This is the
17106      first insn after the following code_label if REVERSE is true.  */
17107   rtx start_insn = insn;
17108
17109   /* If in state 4, check if the target branch is reached, in order to
17110      change back to state 0.  */
17111   if (arm_ccfsm_state == 4)
17112     {
17113       if (insn == arm_target_insn)
17114         {
17115           arm_target_insn = NULL;
17116           arm_ccfsm_state = 0;
17117         }
17118       return;
17119     }
17120
17121   /* If in state 3, it is possible to repeat the trick, if this insn is an
17122      unconditional branch to a label, and immediately following this branch
17123      is the previous target label which is only used once, and the label this
17124      branch jumps to is not too far off.  */
17125   if (arm_ccfsm_state == 3)
17126     {
17127       if (simplejump_p (insn))
17128         {
17129           start_insn = next_nonnote_insn (start_insn);
17130           if (GET_CODE (start_insn) == BARRIER)
17131             {
17132               /* XXX Isn't this always a barrier?  */
17133               start_insn = next_nonnote_insn (start_insn);
17134             }
17135           if (GET_CODE (start_insn) == CODE_LABEL
17136               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
17137               && LABEL_NUSES (start_insn) == 1)
17138             reverse = TRUE;
17139           else
17140             return;
17141         }
17142       else if (GET_CODE (body) == RETURN)
17143         {
17144           start_insn = next_nonnote_insn (start_insn);
17145           if (GET_CODE (start_insn) == BARRIER)
17146             start_insn = next_nonnote_insn (start_insn);
17147           if (GET_CODE (start_insn) == CODE_LABEL
17148               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
17149               && LABEL_NUSES (start_insn) == 1)
17150             {
17151               reverse = TRUE;
17152               seeking_return = 1;
17153             }
17154           else
17155             return;
17156         }
17157       else
17158         return;
17159     }
17160
17161   gcc_assert (!arm_ccfsm_state || reverse);
17162   if (GET_CODE (insn) != JUMP_INSN)
17163     return;
17164
17165   /* This jump might be paralleled with a clobber of the condition codes
17166      the jump should always come first */
17167   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
17168     body = XVECEXP (body, 0, 0);
17169
17170   if (reverse
17171       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
17172           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
17173     {
17174       int insns_skipped;
17175       int fail = FALSE, succeed = FALSE;
17176       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
17177       int then_not_else = TRUE;
17178       rtx this_insn = start_insn, label = 0;
17179
17180       /* Register the insn jumped to.  */
17181       if (reverse)
17182         {
17183           if (!seeking_return)
17184             label = XEXP (SET_SRC (body), 0);
17185         }
17186       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
17187         label = XEXP (XEXP (SET_SRC (body), 1), 0);
17188       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
17189         {
17190           label = XEXP (XEXP (SET_SRC (body), 2), 0);
17191           then_not_else = FALSE;
17192         }
17193       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
17194         seeking_return = 1;
17195       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
17196         {
17197           seeking_return = 1;
17198           then_not_else = FALSE;
17199         }
17200       else
17201         gcc_unreachable ();
17202
17203       /* See how many insns this branch skips, and what kind of insns.  If all
17204          insns are okay, and the label or unconditional branch to the same
17205          label is not too far away, succeed.  */
17206       for (insns_skipped = 0;
17207            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
17208         {
17209           rtx scanbody;
17210
17211           this_insn = next_nonnote_insn (this_insn);
17212           if (!this_insn)
17213             break;
17214
17215           switch (GET_CODE (this_insn))
17216             {
17217             case CODE_LABEL:
17218               /* Succeed if it is the target label, otherwise fail since
17219                  control falls in from somewhere else.  */
17220               if (this_insn == label)
17221                 {
17222                   arm_ccfsm_state = 1;
17223                   succeed = TRUE;
17224                 }
17225               else
17226                 fail = TRUE;
17227               break;
17228
17229             case BARRIER:
17230               /* Succeed if the following insn is the target label.
17231                  Otherwise fail.
17232                  If return insns are used then the last insn in a function
17233                  will be a barrier.  */
17234               this_insn = next_nonnote_insn (this_insn);
17235               if (this_insn && this_insn == label)
17236                 {
17237                   arm_ccfsm_state = 1;
17238                   succeed = TRUE;
17239                 }
17240               else
17241                 fail = TRUE;
17242               break;
17243
17244             case CALL_INSN:
17245               /* The AAPCS says that conditional calls should not be
17246                  used since they make interworking inefficient (the
17247                  linker can't transform BL<cond> into BLX).  That's
17248                  only a problem if the machine has BLX.  */
17249               if (arm_arch5)
17250                 {
17251                   fail = TRUE;
17252                   break;
17253                 }
17254
17255               /* Succeed if the following insn is the target label, or
17256                  if the following two insns are a barrier and the
17257                  target label.  */
17258               this_insn = next_nonnote_insn (this_insn);
17259               if (this_insn && GET_CODE (this_insn) == BARRIER)
17260                 this_insn = next_nonnote_insn (this_insn);
17261
17262               if (this_insn && this_insn == label
17263                   && insns_skipped < max_insns_skipped)
17264                 {
17265                   arm_ccfsm_state = 1;
17266                   succeed = TRUE;
17267                 }
17268               else
17269                 fail = TRUE;
17270               break;
17271
17272             case JUMP_INSN:
17273               /* If this is an unconditional branch to the same label, succeed.
17274                  If it is to another label, do nothing.  If it is conditional,
17275                  fail.  */
17276               /* XXX Probably, the tests for SET and the PC are
17277                  unnecessary.  */
17278
17279               scanbody = PATTERN (this_insn);
17280               if (GET_CODE (scanbody) == SET
17281                   && GET_CODE (SET_DEST (scanbody)) == PC)
17282                 {
17283                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
17284                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
17285                     {
17286                       arm_ccfsm_state = 2;
17287                       succeed = TRUE;
17288                     }
17289                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
17290                     fail = TRUE;
17291                 }
17292               /* Fail if a conditional return is undesirable (e.g. on a
17293                  StrongARM), but still allow this if optimizing for size.  */
17294               else if (GET_CODE (scanbody) == RETURN
17295                        && !use_return_insn (TRUE, NULL)
17296                        && !optimize_size)
17297                 fail = TRUE;
17298               else if (GET_CODE (scanbody) == RETURN
17299                        && seeking_return)
17300                 {
17301                   arm_ccfsm_state = 2;
17302                   succeed = TRUE;
17303                 }
17304               else if (GET_CODE (scanbody) == PARALLEL)
17305                 {
17306                   switch (get_attr_conds (this_insn))
17307                     {
17308                     case CONDS_NOCOND:
17309                       break;
17310                     default:
17311                       fail = TRUE;
17312                       break;
17313                     }
17314                 }
17315               else
17316                 fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
17317
17318               break;
17319
17320             case INSN:
17321               /* Instructions using or affecting the condition codes make it
17322                  fail.  */
17323               scanbody = PATTERN (this_insn);
17324               if (!(GET_CODE (scanbody) == SET
17325                     || GET_CODE (scanbody) == PARALLEL)
17326                   || get_attr_conds (this_insn) != CONDS_NOCOND)
17327                 fail = TRUE;
17328
17329               /* A conditional cirrus instruction must be followed by
17330                  a non Cirrus instruction.  However, since we
17331                  conditionalize instructions in this function and by
17332                  the time we get here we can't add instructions
17333                  (nops), because shorten_branches() has already been
17334                  called, we will disable conditionalizing Cirrus
17335                  instructions to be safe.  */
17336               if (GET_CODE (scanbody) != USE
17337                   && GET_CODE (scanbody) != CLOBBER
17338                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
17339                 fail = TRUE;
17340               break;
17341
17342             default:
17343               break;
17344             }
17345         }
17346       if (succeed)
17347         {
17348           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
17349             arm_target_label = CODE_LABEL_NUMBER (label);
17350           else
17351             {
17352               gcc_assert (seeking_return || arm_ccfsm_state == 2);
17353
17354               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
17355                 {
17356                   this_insn = next_nonnote_insn (this_insn);
17357                   gcc_assert (!this_insn
17358                               || (GET_CODE (this_insn) != BARRIER
17359                                   && GET_CODE (this_insn) != CODE_LABEL));
17360                 }
17361               if (!this_insn)
17362                 {
17363                   /* Oh, dear! we ran off the end.. give up.  */
17364                   extract_constrain_insn_cached (insn);
17365                   arm_ccfsm_state = 0;
17366                   arm_target_insn = NULL;
17367                   return;
17368                 }
17369               arm_target_insn = this_insn;
17370             }
17371
17372           /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
17373              what it was.  */
17374           if (!reverse)
17375             arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body), 0));
17376
17377           if (reverse || then_not_else)
17378             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
17379         }
17380
17381       /* Restore recog_data (getting the attributes of other insns can
17382          destroy this array, but final.c assumes that it remains intact
17383          across this call.  */
17384       extract_constrain_insn_cached (insn);
17385     }
17386 }
17387
17388 /* Output IT instructions.  */
17389 void
17390 thumb2_asm_output_opcode (FILE * stream)
17391 {
17392   char buff[5];
17393   int n;
17394
17395   if (arm_condexec_mask)
17396     {
17397       for (n = 0; n < arm_condexec_masklen; n++)
17398         buff[n] = (arm_condexec_mask & (1 << n)) ? 't' : 'e';
17399       buff[n] = 0;
17400       asm_fprintf(stream, "i%s\t%s\n\t", buff,
17401                   arm_condition_codes[arm_current_cc]);
17402       arm_condexec_mask = 0;
17403     }
17404 }
17405
17406 /* Returns true if REGNO is a valid register
17407    for holding a quantity of type MODE.  */
17408 int
17409 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
17410 {
17411   if (GET_MODE_CLASS (mode) == MODE_CC)
17412     return (regno == CC_REGNUM
17413             || (TARGET_HARD_FLOAT && TARGET_VFP
17414                 && regno == VFPCC_REGNUM));
17415
17416   if (TARGET_THUMB1)
17417     /* For the Thumb we only allow values bigger than SImode in
17418        registers 0 - 6, so that there is always a second low
17419        register available to hold the upper part of the value.
17420        We probably we ought to ensure that the register is the
17421        start of an even numbered register pair.  */
17422     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
17423
17424   if (TARGET_HARD_FLOAT && TARGET_MAVERICK
17425       && IS_CIRRUS_REGNUM (regno))
17426     /* We have outlawed SI values in Cirrus registers because they
17427        reside in the lower 32 bits, but SF values reside in the
17428        upper 32 bits.  This causes gcc all sorts of grief.  We can't
17429        even split the registers into pairs because Cirrus SI values
17430        get sign extended to 64bits-- aldyh.  */
17431     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
17432
17433   if (TARGET_HARD_FLOAT && TARGET_VFP
17434       && IS_VFP_REGNUM (regno))
17435     {
17436       if (mode == SFmode || mode == SImode)
17437         return VFP_REGNO_OK_FOR_SINGLE (regno);
17438
17439       if (mode == DFmode)
17440         return VFP_REGNO_OK_FOR_DOUBLE (regno);
17441
17442       /* VFP registers can hold HFmode values, but there is no point in
17443          putting them there unless we have hardware conversion insns. */
17444       if (mode == HFmode)
17445         return TARGET_FP16 && VFP_REGNO_OK_FOR_SINGLE (regno);
17446
17447       if (TARGET_NEON)
17448         return (VALID_NEON_DREG_MODE (mode) && VFP_REGNO_OK_FOR_DOUBLE (regno))
17449                || (VALID_NEON_QREG_MODE (mode)
17450                    && NEON_REGNO_OK_FOR_QUAD (regno))
17451                || (mode == TImode && NEON_REGNO_OK_FOR_NREGS (regno, 2))
17452                || (mode == EImode && NEON_REGNO_OK_FOR_NREGS (regno, 3))
17453                || (mode == OImode && NEON_REGNO_OK_FOR_NREGS (regno, 4))
17454                || (mode == CImode && NEON_REGNO_OK_FOR_NREGS (regno, 6))
17455                || (mode == XImode && NEON_REGNO_OK_FOR_NREGS (regno, 8));
17456
17457       return FALSE;
17458     }
17459
17460   if (TARGET_REALLY_IWMMXT)
17461     {
17462       if (IS_IWMMXT_GR_REGNUM (regno))
17463         return mode == SImode;
17464
17465       if (IS_IWMMXT_REGNUM (regno))
17466         return VALID_IWMMXT_REG_MODE (mode);
17467     }
17468   
17469   /* We allow almost any value to be stored in the general registers.
17470      Restrict doubleword quantities to even register pairs so that we can
17471      use ldrd.  Do not allow very large Neon structure opaque modes in
17472      general registers; they would use too many.  */
17473   if (regno <= LAST_ARM_REGNUM)
17474     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0)
17475       && ARM_NUM_REGS (mode) <= 4;
17476
17477   if (regno == FRAME_POINTER_REGNUM
17478       || regno == ARG_POINTER_REGNUM)
17479     /* We only allow integers in the fake hard registers.  */
17480     return GET_MODE_CLASS (mode) == MODE_INT;
17481
17482   /* The only registers left are the FPA registers
17483      which we only allow to hold FP values.  */
17484   return (TARGET_HARD_FLOAT && TARGET_FPA
17485           && GET_MODE_CLASS (mode) == MODE_FLOAT
17486           && regno >= FIRST_FPA_REGNUM
17487           && regno <= LAST_FPA_REGNUM);
17488 }
17489
17490 /* For efficiency and historical reasons LO_REGS, HI_REGS and CC_REGS are
17491    not used in arm mode.  */
17492
17493 enum reg_class
17494 arm_regno_class (int regno)
17495 {
17496   if (TARGET_THUMB1)
17497     {
17498       if (regno == STACK_POINTER_REGNUM)
17499         return STACK_REG;
17500       if (regno == CC_REGNUM)
17501         return CC_REG;
17502       if (regno < 8)
17503         return LO_REGS;
17504       return HI_REGS;
17505     }
17506
17507   if (TARGET_THUMB2 && regno < 8)
17508     return LO_REGS;
17509
17510   if (   regno <= LAST_ARM_REGNUM
17511       || regno == FRAME_POINTER_REGNUM
17512       || regno == ARG_POINTER_REGNUM)
17513     return TARGET_THUMB2 ? HI_REGS : GENERAL_REGS;
17514
17515   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
17516     return TARGET_THUMB2 ? CC_REG : NO_REGS;
17517
17518   if (IS_CIRRUS_REGNUM (regno))
17519     return CIRRUS_REGS;
17520
17521   if (IS_VFP_REGNUM (regno))
17522     {
17523       if (regno <= D7_VFP_REGNUM)
17524         return VFP_D0_D7_REGS;
17525       else if (regno <= LAST_LO_VFP_REGNUM)
17526         return VFP_LO_REGS;
17527       else
17528         return VFP_HI_REGS;
17529     }
17530
17531   if (IS_IWMMXT_REGNUM (regno))
17532     return IWMMXT_REGS;
17533
17534   if (IS_IWMMXT_GR_REGNUM (regno))
17535     return IWMMXT_GR_REGS;
17536
17537   return FPA_REGS;
17538 }
17539
17540 /* Handle a special case when computing the offset
17541    of an argument from the frame pointer.  */
17542 int
17543 arm_debugger_arg_offset (int value, rtx addr)
17544 {
17545   rtx insn;
17546
17547   /* We are only interested if dbxout_parms() failed to compute the offset.  */
17548   if (value != 0)
17549     return 0;
17550
17551   /* We can only cope with the case where the address is held in a register.  */
17552   if (GET_CODE (addr) != REG)
17553     return 0;
17554
17555   /* If we are using the frame pointer to point at the argument, then
17556      an offset of 0 is correct.  */
17557   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
17558     return 0;
17559
17560   /* If we are using the stack pointer to point at the
17561      argument, then an offset of 0 is correct.  */
17562   /* ??? Check this is consistent with thumb2 frame layout.  */
17563   if ((TARGET_THUMB || !frame_pointer_needed)
17564       && REGNO (addr) == SP_REGNUM)
17565     return 0;
17566
17567   /* Oh dear.  The argument is pointed to by a register rather
17568      than being held in a register, or being stored at a known
17569      offset from the frame pointer.  Since GDB only understands
17570      those two kinds of argument we must translate the address
17571      held in the register into an offset from the frame pointer.
17572      We do this by searching through the insns for the function
17573      looking to see where this register gets its value.  If the
17574      register is initialized from the frame pointer plus an offset
17575      then we are in luck and we can continue, otherwise we give up.
17576
17577      This code is exercised by producing debugging information
17578      for a function with arguments like this:
17579
17580            double func (double a, double b, int c, double d) {return d;}
17581
17582      Without this code the stab for parameter 'd' will be set to
17583      an offset of 0 from the frame pointer, rather than 8.  */
17584
17585   /* The if() statement says:
17586
17587      If the insn is a normal instruction
17588      and if the insn is setting the value in a register
17589      and if the register being set is the register holding the address of the argument
17590      and if the address is computing by an addition
17591      that involves adding to a register
17592      which is the frame pointer
17593      a constant integer
17594
17595      then...  */
17596
17597   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
17598     {
17599       if (   GET_CODE (insn) == INSN
17600           && GET_CODE (PATTERN (insn)) == SET
17601           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
17602           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
17603           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
17604           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
17605           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
17606              )
17607         {
17608           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
17609
17610           break;
17611         }
17612     }
17613
17614   if (value == 0)
17615     {
17616       debug_rtx (addr);
17617       warning (0, "unable to compute real location of stacked parameter");
17618       value = 8; /* XXX magic hack */
17619     }
17620
17621   return value;
17622 }
17623 \f
17624 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
17625   do                                                                    \
17626     {                                                                   \
17627       if ((MASK) & insn_flags)                                          \
17628         add_builtin_function ((NAME), (TYPE), (CODE),                   \
17629                              BUILT_IN_MD, NULL, NULL_TREE);             \
17630     }                                                                   \
17631   while (0)
17632
17633 struct builtin_description
17634 {
17635   const unsigned int       mask;
17636   const enum insn_code     icode;
17637   const char * const       name;
17638   const enum arm_builtins  code;
17639   const enum rtx_code      comparison;
17640   const unsigned int       flag;
17641 };
17642
17643 static const struct builtin_description bdesc_2arg[] =
17644 {
17645 #define IWMMXT_BUILTIN(code, string, builtin) \
17646   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
17647     ARM_BUILTIN_##builtin, UNKNOWN, 0 },
17648
17649   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
17650   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
17651   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
17652   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
17653   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
17654   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
17655   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
17656   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
17657   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
17658   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
17659   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
17660   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
17661   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
17662   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
17663   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
17664   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
17665   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
17666   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
17667   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
17668   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
17669   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
17670   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
17671   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
17672   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
17673   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
17674   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
17675   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
17676   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
17677   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
17678   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
17679   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
17680   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
17681   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
17682   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
17683   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
17684   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
17685   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
17686   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
17687   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
17688   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
17689   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
17690   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
17691   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
17692   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
17693   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
17694   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
17695   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
17696   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
17697   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
17698   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
17699   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
17700   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
17701   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
17702   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
17703   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
17704   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
17705   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
17706   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
17707
17708 #define IWMMXT_BUILTIN2(code, builtin) \
17709   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, UNKNOWN, 0 },
17710
17711   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
17712   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
17713   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
17714   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
17715   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
17716   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
17717   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
17718   IWMMXT_BUILTIN2 (ashlv4hi3_iwmmxt, WSLLHI)
17719   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
17720   IWMMXT_BUILTIN2 (ashlv2si3_iwmmxt, WSLLWI)
17721   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
17722   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
17723   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
17724   IWMMXT_BUILTIN2 (lshrv4hi3_iwmmxt, WSRLHI)
17725   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
17726   IWMMXT_BUILTIN2 (lshrv2si3_iwmmxt, WSRLWI)
17727   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
17728   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
17729   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
17730   IWMMXT_BUILTIN2 (ashrv4hi3_iwmmxt, WSRAHI)
17731   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
17732   IWMMXT_BUILTIN2 (ashrv2si3_iwmmxt, WSRAWI)
17733   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
17734   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
17735   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
17736   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
17737   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
17738   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
17739   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
17740   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
17741   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
17742   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
17743 };
17744
17745 static const struct builtin_description bdesc_1arg[] =
17746 {
17747   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
17748   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
17749   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
17750   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
17751   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
17752   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
17753   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
17754   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
17755   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
17756   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
17757   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
17758   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
17759   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
17760   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
17761   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
17762   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
17763   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
17764   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
17765 };
17766
17767 /* Set up all the iWMMXt builtins.  This is
17768    not called if TARGET_IWMMXT is zero.  */
17769
17770 static void
17771 arm_init_iwmmxt_builtins (void)
17772 {
17773   const struct builtin_description * d;
17774   size_t i;
17775   tree endlink = void_list_node;
17776
17777   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
17778   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
17779   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
17780
17781   tree int_ftype_int
17782     = build_function_type (integer_type_node,
17783                            tree_cons (NULL_TREE, integer_type_node, endlink));
17784   tree v8qi_ftype_v8qi_v8qi_int
17785     = build_function_type (V8QI_type_node,
17786                            tree_cons (NULL_TREE, V8QI_type_node,
17787                                       tree_cons (NULL_TREE, V8QI_type_node,
17788                                                  tree_cons (NULL_TREE,
17789                                                             integer_type_node,
17790                                                             endlink))));
17791   tree v4hi_ftype_v4hi_int
17792     = build_function_type (V4HI_type_node,
17793                            tree_cons (NULL_TREE, V4HI_type_node,
17794                                       tree_cons (NULL_TREE, integer_type_node,
17795                                                  endlink)));
17796   tree v2si_ftype_v2si_int
17797     = build_function_type (V2SI_type_node,
17798                            tree_cons (NULL_TREE, V2SI_type_node,
17799                                       tree_cons (NULL_TREE, integer_type_node,
17800                                                  endlink)));
17801   tree v2si_ftype_di_di
17802     = build_function_type (V2SI_type_node,
17803                            tree_cons (NULL_TREE, long_long_integer_type_node,
17804                                       tree_cons (NULL_TREE, long_long_integer_type_node,
17805                                                  endlink)));
17806   tree di_ftype_di_int
17807     = build_function_type (long_long_integer_type_node,
17808                            tree_cons (NULL_TREE, long_long_integer_type_node,
17809                                       tree_cons (NULL_TREE, integer_type_node,
17810                                                  endlink)));
17811   tree di_ftype_di_int_int
17812     = build_function_type (long_long_integer_type_node,
17813                            tree_cons (NULL_TREE, long_long_integer_type_node,
17814                                       tree_cons (NULL_TREE, integer_type_node,
17815                                                  tree_cons (NULL_TREE,
17816                                                             integer_type_node,
17817                                                             endlink))));
17818   tree int_ftype_v8qi
17819     = build_function_type (integer_type_node,
17820                            tree_cons (NULL_TREE, V8QI_type_node,
17821                                       endlink));
17822   tree int_ftype_v4hi
17823     = build_function_type (integer_type_node,
17824                            tree_cons (NULL_TREE, V4HI_type_node,
17825                                       endlink));
17826   tree int_ftype_v2si
17827     = build_function_type (integer_type_node,
17828                            tree_cons (NULL_TREE, V2SI_type_node,
17829                                       endlink));
17830   tree int_ftype_v8qi_int
17831     = build_function_type (integer_type_node,
17832                            tree_cons (NULL_TREE, V8QI_type_node,
17833                                       tree_cons (NULL_TREE, integer_type_node,
17834                                                  endlink)));
17835   tree int_ftype_v4hi_int
17836     = build_function_type (integer_type_node,
17837                            tree_cons (NULL_TREE, V4HI_type_node,
17838                                       tree_cons (NULL_TREE, integer_type_node,
17839                                                  endlink)));
17840   tree int_ftype_v2si_int
17841     = build_function_type (integer_type_node,
17842                            tree_cons (NULL_TREE, V2SI_type_node,
17843                                       tree_cons (NULL_TREE, integer_type_node,
17844                                                  endlink)));
17845   tree v8qi_ftype_v8qi_int_int
17846     = build_function_type (V8QI_type_node,
17847                            tree_cons (NULL_TREE, V8QI_type_node,
17848                                       tree_cons (NULL_TREE, integer_type_node,
17849                                                  tree_cons (NULL_TREE,
17850                                                             integer_type_node,
17851                                                             endlink))));
17852   tree v4hi_ftype_v4hi_int_int
17853     = build_function_type (V4HI_type_node,
17854                            tree_cons (NULL_TREE, V4HI_type_node,
17855                                       tree_cons (NULL_TREE, integer_type_node,
17856                                                  tree_cons (NULL_TREE,
17857                                                             integer_type_node,
17858                                                             endlink))));
17859   tree v2si_ftype_v2si_int_int
17860     = build_function_type (V2SI_type_node,
17861                            tree_cons (NULL_TREE, V2SI_type_node,
17862                                       tree_cons (NULL_TREE, integer_type_node,
17863                                                  tree_cons (NULL_TREE,
17864                                                             integer_type_node,
17865                                                             endlink))));
17866   /* Miscellaneous.  */
17867   tree v8qi_ftype_v4hi_v4hi
17868     = build_function_type (V8QI_type_node,
17869                            tree_cons (NULL_TREE, V4HI_type_node,
17870                                       tree_cons (NULL_TREE, V4HI_type_node,
17871                                                  endlink)));
17872   tree v4hi_ftype_v2si_v2si
17873     = build_function_type (V4HI_type_node,
17874                            tree_cons (NULL_TREE, V2SI_type_node,
17875                                       tree_cons (NULL_TREE, V2SI_type_node,
17876                                                  endlink)));
17877   tree v2si_ftype_v4hi_v4hi
17878     = build_function_type (V2SI_type_node,
17879                            tree_cons (NULL_TREE, V4HI_type_node,
17880                                       tree_cons (NULL_TREE, V4HI_type_node,
17881                                                  endlink)));
17882   tree v2si_ftype_v8qi_v8qi
17883     = build_function_type (V2SI_type_node,
17884                            tree_cons (NULL_TREE, V8QI_type_node,
17885                                       tree_cons (NULL_TREE, V8QI_type_node,
17886                                                  endlink)));
17887   tree v4hi_ftype_v4hi_di
17888     = build_function_type (V4HI_type_node,
17889                            tree_cons (NULL_TREE, V4HI_type_node,
17890                                       tree_cons (NULL_TREE,
17891                                                  long_long_integer_type_node,
17892                                                  endlink)));
17893   tree v2si_ftype_v2si_di
17894     = build_function_type (V2SI_type_node,
17895                            tree_cons (NULL_TREE, V2SI_type_node,
17896                                       tree_cons (NULL_TREE,
17897                                                  long_long_integer_type_node,
17898                                                  endlink)));
17899   tree void_ftype_int_int
17900     = build_function_type (void_type_node,
17901                            tree_cons (NULL_TREE, integer_type_node,
17902                                       tree_cons (NULL_TREE, integer_type_node,
17903                                                  endlink)));
17904   tree di_ftype_void
17905     = build_function_type (long_long_unsigned_type_node, endlink);
17906   tree di_ftype_v8qi
17907     = build_function_type (long_long_integer_type_node,
17908                            tree_cons (NULL_TREE, V8QI_type_node,
17909                                       endlink));
17910   tree di_ftype_v4hi
17911     = build_function_type (long_long_integer_type_node,
17912                            tree_cons (NULL_TREE, V4HI_type_node,
17913                                       endlink));
17914   tree di_ftype_v2si
17915     = build_function_type (long_long_integer_type_node,
17916                            tree_cons (NULL_TREE, V2SI_type_node,
17917                                       endlink));
17918   tree v2si_ftype_v4hi
17919     = build_function_type (V2SI_type_node,
17920                            tree_cons (NULL_TREE, V4HI_type_node,
17921                                       endlink));
17922   tree v4hi_ftype_v8qi
17923     = build_function_type (V4HI_type_node,
17924                            tree_cons (NULL_TREE, V8QI_type_node,
17925                                       endlink));
17926
17927   tree di_ftype_di_v4hi_v4hi
17928     = build_function_type (long_long_unsigned_type_node,
17929                            tree_cons (NULL_TREE,
17930                                       long_long_unsigned_type_node,
17931                                       tree_cons (NULL_TREE, V4HI_type_node,
17932                                                  tree_cons (NULL_TREE,
17933                                                             V4HI_type_node,
17934                                                             endlink))));
17935
17936   tree di_ftype_v4hi_v4hi
17937     = build_function_type (long_long_unsigned_type_node,
17938                            tree_cons (NULL_TREE, V4HI_type_node,
17939                                       tree_cons (NULL_TREE, V4HI_type_node,
17940                                                  endlink)));
17941
17942   /* Normal vector binops.  */
17943   tree v8qi_ftype_v8qi_v8qi
17944     = build_function_type (V8QI_type_node,
17945                            tree_cons (NULL_TREE, V8QI_type_node,
17946                                       tree_cons (NULL_TREE, V8QI_type_node,
17947                                                  endlink)));
17948   tree v4hi_ftype_v4hi_v4hi
17949     = build_function_type (V4HI_type_node,
17950                            tree_cons (NULL_TREE, V4HI_type_node,
17951                                       tree_cons (NULL_TREE, V4HI_type_node,
17952                                                  endlink)));
17953   tree v2si_ftype_v2si_v2si
17954     = build_function_type (V2SI_type_node,
17955                            tree_cons (NULL_TREE, V2SI_type_node,
17956                                       tree_cons (NULL_TREE, V2SI_type_node,
17957                                                  endlink)));
17958   tree di_ftype_di_di
17959     = build_function_type (long_long_unsigned_type_node,
17960                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
17961                                       tree_cons (NULL_TREE,
17962                                                  long_long_unsigned_type_node,
17963                                                  endlink)));
17964
17965   /* Add all builtins that are more or less simple operations on two
17966      operands.  */
17967   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
17968     {
17969       /* Use one of the operands; the target can have a different mode for
17970          mask-generating compares.  */
17971       enum machine_mode mode;
17972       tree type;
17973
17974       if (d->name == 0)
17975         continue;
17976
17977       mode = insn_data[d->icode].operand[1].mode;
17978
17979       switch (mode)
17980         {
17981         case V8QImode:
17982           type = v8qi_ftype_v8qi_v8qi;
17983           break;
17984         case V4HImode:
17985           type = v4hi_ftype_v4hi_v4hi;
17986           break;
17987         case V2SImode:
17988           type = v2si_ftype_v2si_v2si;
17989           break;
17990         case DImode:
17991           type = di_ftype_di_di;
17992           break;
17993
17994         default:
17995           gcc_unreachable ();
17996         }
17997
17998       def_mbuiltin (d->mask, d->name, type, d->code);
17999     }
18000
18001   /* Add the remaining MMX insns with somewhat more complicated types.  */
18002   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
18003   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
18004   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
18005
18006   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
18007   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
18008   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
18009   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
18010   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
18011   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
18012
18013   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
18014   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
18015   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
18016   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
18017   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
18018   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
18019
18020   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
18021   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
18022   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
18023   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
18024   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
18025   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
18026
18027   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
18028   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
18029   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
18030   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
18031   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
18032   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
18033
18034   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
18035
18036   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
18037   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
18038   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
18039   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
18040
18041   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
18042   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
18043   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
18044   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
18045   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
18046   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
18047   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
18048   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
18049   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
18050
18051   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
18052   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
18053   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
18054
18055   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
18056   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
18057   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
18058
18059   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
18060   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
18061   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
18062   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
18063   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
18064   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
18065
18066   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
18067   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
18068   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
18069   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
18070   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
18071   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
18072   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
18073   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
18074   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
18075   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
18076   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
18077   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
18078
18079   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
18080   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
18081   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
18082   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
18083
18084   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
18085   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
18086   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
18087   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
18088   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
18089   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
18090   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
18091 }
18092
18093 static void
18094 arm_init_tls_builtins (void)
18095 {
18096   tree ftype, decl;
18097
18098   ftype = build_function_type (ptr_type_node, void_list_node);
18099   decl = add_builtin_function ("__builtin_thread_pointer", ftype,
18100                                ARM_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
18101                                NULL, NULL_TREE);
18102   TREE_NOTHROW (decl) = 1;
18103   TREE_READONLY (decl) = 1;
18104 }
18105
18106 enum neon_builtin_type_bits {
18107   T_V8QI  = 0x0001,
18108   T_V4HI  = 0x0002,
18109   T_V2SI  = 0x0004,
18110   T_V2SF  = 0x0008,
18111   T_DI    = 0x0010,
18112   T_V16QI = 0x0020,
18113   T_V8HI  = 0x0040,
18114   T_V4SI  = 0x0080,
18115   T_V4SF  = 0x0100,
18116   T_V2DI  = 0x0200,
18117   T_TI    = 0x0400,
18118   T_EI    = 0x0800,
18119   T_OI    = 0x1000
18120 };
18121
18122 #define v8qi_UP  T_V8QI
18123 #define v4hi_UP  T_V4HI
18124 #define v2si_UP  T_V2SI
18125 #define v2sf_UP  T_V2SF
18126 #define di_UP    T_DI
18127 #define v16qi_UP T_V16QI
18128 #define v8hi_UP  T_V8HI
18129 #define v4si_UP  T_V4SI
18130 #define v4sf_UP  T_V4SF
18131 #define v2di_UP  T_V2DI
18132 #define ti_UP    T_TI
18133 #define ei_UP    T_EI
18134 #define oi_UP    T_OI
18135
18136 #define UP(X) X##_UP
18137
18138 #define T_MAX 13
18139
18140 typedef enum {
18141   NEON_BINOP,
18142   NEON_TERNOP,
18143   NEON_UNOP,
18144   NEON_GETLANE,
18145   NEON_SETLANE,
18146   NEON_CREATE,
18147   NEON_DUP,
18148   NEON_DUPLANE,
18149   NEON_COMBINE,
18150   NEON_SPLIT,
18151   NEON_LANEMUL,
18152   NEON_LANEMULL,
18153   NEON_LANEMULH,
18154   NEON_LANEMAC,
18155   NEON_SCALARMUL,
18156   NEON_SCALARMULL,
18157   NEON_SCALARMULH,
18158   NEON_SCALARMAC,
18159   NEON_CONVERT,
18160   NEON_FIXCONV,
18161   NEON_SELECT,
18162   NEON_RESULTPAIR,
18163   NEON_REINTERP,
18164   NEON_VTBL,
18165   NEON_VTBX,
18166   NEON_LOAD1,
18167   NEON_LOAD1LANE,
18168   NEON_STORE1,
18169   NEON_STORE1LANE,
18170   NEON_LOADSTRUCT,
18171   NEON_LOADSTRUCTLANE,
18172   NEON_STORESTRUCT,
18173   NEON_STORESTRUCTLANE,
18174   NEON_LOGICBINOP,
18175   NEON_SHIFTINSERT,
18176   NEON_SHIFTIMM,
18177   NEON_SHIFTACC
18178 } neon_itype;
18179
18180 typedef struct {
18181   const char *name;
18182   const neon_itype itype;
18183   const int bits;
18184   const enum insn_code codes[T_MAX];
18185   const unsigned int num_vars;
18186   unsigned int base_fcode;
18187 } neon_builtin_datum;
18188
18189 #define CF(N,X) CODE_FOR_neon_##N##X
18190
18191 #define VAR1(T, N, A) \
18192   #N, NEON_##T, UP (A), { CF (N, A) }, 1, 0
18193 #define VAR2(T, N, A, B) \
18194   #N, NEON_##T, UP (A) | UP (B), { CF (N, A), CF (N, B) }, 2, 0
18195 #define VAR3(T, N, A, B, C) \
18196   #N, NEON_##T, UP (A) | UP (B) | UP (C), \
18197   { CF (N, A), CF (N, B), CF (N, C) }, 3, 0
18198 #define VAR4(T, N, A, B, C, D) \
18199   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D), \
18200   { CF (N, A), CF (N, B), CF (N, C), CF (N, D) }, 4, 0
18201 #define VAR5(T, N, A, B, C, D, E) \
18202   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E), \
18203   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E) }, 5, 0
18204 #define VAR6(T, N, A, B, C, D, E, F) \
18205   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F), \
18206   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F) }, 6, 0
18207 #define VAR7(T, N, A, B, C, D, E, F, G) \
18208   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G), \
18209   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
18210     CF (N, G) }, 7, 0
18211 #define VAR8(T, N, A, B, C, D, E, F, G, H) \
18212   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
18213                 | UP (H), \
18214   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
18215     CF (N, G), CF (N, H) }, 8, 0
18216 #define VAR9(T, N, A, B, C, D, E, F, G, H, I) \
18217   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
18218                 | UP (H) | UP (I), \
18219   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
18220     CF (N, G), CF (N, H), CF (N, I) }, 9, 0
18221 #define VAR10(T, N, A, B, C, D, E, F, G, H, I, J) \
18222   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
18223                 | UP (H) | UP (I) | UP (J), \
18224   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
18225     CF (N, G), CF (N, H), CF (N, I), CF (N, J) }, 10, 0
18226
18227 /* The mode entries in the following table correspond to the "key" type of the
18228    instruction variant, i.e. equivalent to that which would be specified after
18229    the assembler mnemonic, which usually refers to the last vector operand.
18230    (Signed/unsigned/polynomial types are not differentiated between though, and
18231    are all mapped onto the same mode for a given element size.) The modes
18232    listed per instruction should be the same as those defined for that
18233    instruction's pattern in neon.md.
18234    WARNING: Variants should be listed in the same increasing order as
18235    neon_builtin_type_bits.  */
18236
18237 static neon_builtin_datum neon_builtin_data[] =
18238 {
18239   { VAR10 (BINOP, vadd,
18240            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18241   { VAR3 (BINOP, vaddl, v8qi, v4hi, v2si) },
18242   { VAR3 (BINOP, vaddw, v8qi, v4hi, v2si) },
18243   { VAR6 (BINOP, vhadd, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18244   { VAR8 (BINOP, vqadd, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18245   { VAR3 (BINOP, vaddhn, v8hi, v4si, v2di) },
18246   { VAR8 (BINOP, vmul, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18247   { VAR8 (TERNOP, vmla, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18248   { VAR3 (TERNOP, vmlal, v8qi, v4hi, v2si) },
18249   { VAR8 (TERNOP, vmls, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18250   { VAR3 (TERNOP, vmlsl, v8qi, v4hi, v2si) },
18251   { VAR4 (BINOP, vqdmulh, v4hi, v2si, v8hi, v4si) },
18252   { VAR2 (TERNOP, vqdmlal, v4hi, v2si) },
18253   { VAR2 (TERNOP, vqdmlsl, v4hi, v2si) },
18254   { VAR3 (BINOP, vmull, v8qi, v4hi, v2si) },
18255   { VAR2 (SCALARMULL, vmull_n, v4hi, v2si) },
18256   { VAR2 (LANEMULL, vmull_lane, v4hi, v2si) },
18257   { VAR2 (SCALARMULL, vqdmull_n, v4hi, v2si) },
18258   { VAR2 (LANEMULL, vqdmull_lane, v4hi, v2si) },
18259   { VAR4 (SCALARMULH, vqdmulh_n, v4hi, v2si, v8hi, v4si) },
18260   { VAR4 (LANEMULH, vqdmulh_lane, v4hi, v2si, v8hi, v4si) },
18261   { VAR2 (BINOP, vqdmull, v4hi, v2si) },
18262   { VAR8 (BINOP, vshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18263   { VAR8 (BINOP, vqshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18264   { VAR8 (SHIFTIMM, vshr_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18265   { VAR3 (SHIFTIMM, vshrn_n, v8hi, v4si, v2di) },
18266   { VAR3 (SHIFTIMM, vqshrn_n, v8hi, v4si, v2di) },
18267   { VAR3 (SHIFTIMM, vqshrun_n, v8hi, v4si, v2di) },
18268   { VAR8 (SHIFTIMM, vshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18269   { VAR8 (SHIFTIMM, vqshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18270   { VAR8 (SHIFTIMM, vqshlu_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18271   { VAR3 (SHIFTIMM, vshll_n, v8qi, v4hi, v2si) },
18272   { VAR8 (SHIFTACC, vsra_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18273   { VAR10 (BINOP, vsub,
18274            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18275   { VAR3 (BINOP, vsubl, v8qi, v4hi, v2si) },
18276   { VAR3 (BINOP, vsubw, v8qi, v4hi, v2si) },
18277   { VAR8 (BINOP, vqsub, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18278   { VAR6 (BINOP, vhsub, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18279   { VAR3 (BINOP, vsubhn, v8hi, v4si, v2di) },
18280   { VAR8 (BINOP, vceq, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18281   { VAR8 (BINOP, vcge, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18282   { VAR8 (BINOP, vcgt, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18283   { VAR2 (BINOP, vcage, v2sf, v4sf) },
18284   { VAR2 (BINOP, vcagt, v2sf, v4sf) },
18285   { VAR6 (BINOP, vtst, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18286   { VAR8 (BINOP, vabd, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18287   { VAR3 (BINOP, vabdl, v8qi, v4hi, v2si) },
18288   { VAR6 (TERNOP, vaba, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18289   { VAR3 (TERNOP, vabal, v8qi, v4hi, v2si) },
18290   { VAR8 (BINOP, vmax, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18291   { VAR8 (BINOP, vmin, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18292   { VAR4 (BINOP, vpadd, v8qi, v4hi, v2si, v2sf) },
18293   { VAR6 (UNOP, vpaddl, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18294   { VAR6 (BINOP, vpadal, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18295   { VAR4 (BINOP, vpmax, v8qi, v4hi, v2si, v2sf) },
18296   { VAR4 (BINOP, vpmin, v8qi, v4hi, v2si, v2sf) },
18297   { VAR2 (BINOP, vrecps, v2sf, v4sf) },
18298   { VAR2 (BINOP, vrsqrts, v2sf, v4sf) },
18299   { VAR8 (SHIFTINSERT, vsri_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18300   { VAR8 (SHIFTINSERT, vsli_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18301   { VAR8 (UNOP, vabs, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18302   { VAR6 (UNOP, vqabs, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18303   { VAR8 (UNOP, vneg, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18304   { VAR6 (UNOP, vqneg, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18305   { VAR6 (UNOP, vcls, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18306   { VAR6 (UNOP, vclz, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18307   { VAR2 (UNOP, vcnt, v8qi, v16qi) },
18308   { VAR4 (UNOP, vrecpe, v2si, v2sf, v4si, v4sf) },
18309   { VAR4 (UNOP, vrsqrte, v2si, v2sf, v4si, v4sf) },
18310   { VAR6 (UNOP, vmvn, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18311   /* FIXME: vget_lane supports more variants than this!  */
18312   { VAR10 (GETLANE, vget_lane,
18313            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18314   { VAR10 (SETLANE, vset_lane,
18315            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18316   { VAR5 (CREATE, vcreate, v8qi, v4hi, v2si, v2sf, di) },
18317   { VAR10 (DUP, vdup_n,
18318            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18319   { VAR10 (DUPLANE, vdup_lane,
18320            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18321   { VAR5 (COMBINE, vcombine, v8qi, v4hi, v2si, v2sf, di) },
18322   { VAR5 (SPLIT, vget_high, v16qi, v8hi, v4si, v4sf, v2di) },
18323   { VAR5 (SPLIT, vget_low, v16qi, v8hi, v4si, v4sf, v2di) },
18324   { VAR3 (UNOP, vmovn, v8hi, v4si, v2di) },
18325   { VAR3 (UNOP, vqmovn, v8hi, v4si, v2di) },
18326   { VAR3 (UNOP, vqmovun, v8hi, v4si, v2di) },
18327   { VAR3 (UNOP, vmovl, v8qi, v4hi, v2si) },
18328   { VAR6 (LANEMUL, vmul_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18329   { VAR6 (LANEMAC, vmla_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18330   { VAR2 (LANEMAC, vmlal_lane, v4hi, v2si) },
18331   { VAR2 (LANEMAC, vqdmlal_lane, v4hi, v2si) },
18332   { VAR6 (LANEMAC, vmls_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18333   { VAR2 (LANEMAC, vmlsl_lane, v4hi, v2si) },
18334   { VAR2 (LANEMAC, vqdmlsl_lane, v4hi, v2si) },
18335   { VAR6 (SCALARMUL, vmul_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18336   { VAR6 (SCALARMAC, vmla_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18337   { VAR2 (SCALARMAC, vmlal_n, v4hi, v2si) },
18338   { VAR2 (SCALARMAC, vqdmlal_n, v4hi, v2si) },
18339   { VAR6 (SCALARMAC, vmls_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18340   { VAR2 (SCALARMAC, vmlsl_n, v4hi, v2si) },
18341   { VAR2 (SCALARMAC, vqdmlsl_n, v4hi, v2si) },
18342   { VAR10 (BINOP, vext,
18343            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18344   { VAR8 (UNOP, vrev64, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18345   { VAR4 (UNOP, vrev32, v8qi, v4hi, v16qi, v8hi) },
18346   { VAR2 (UNOP, vrev16, v8qi, v16qi) },
18347   { VAR4 (CONVERT, vcvt, v2si, v2sf, v4si, v4sf) },
18348   { VAR4 (FIXCONV, vcvt_n, v2si, v2sf, v4si, v4sf) },
18349   { VAR10 (SELECT, vbsl,
18350            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18351   { VAR1 (VTBL, vtbl1, v8qi) },
18352   { VAR1 (VTBL, vtbl2, v8qi) },
18353   { VAR1 (VTBL, vtbl3, v8qi) },
18354   { VAR1 (VTBL, vtbl4, v8qi) },
18355   { VAR1 (VTBX, vtbx1, v8qi) },
18356   { VAR1 (VTBX, vtbx2, v8qi) },
18357   { VAR1 (VTBX, vtbx3, v8qi) },
18358   { VAR1 (VTBX, vtbx4, v8qi) },
18359   { VAR8 (RESULTPAIR, vtrn, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18360   { VAR8 (RESULTPAIR, vzip, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18361   { VAR8 (RESULTPAIR, vuzp, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18362   { VAR5 (REINTERP, vreinterpretv8qi, v8qi, v4hi, v2si, v2sf, di) },
18363   { VAR5 (REINTERP, vreinterpretv4hi, v8qi, v4hi, v2si, v2sf, di) },
18364   { VAR5 (REINTERP, vreinterpretv2si, v8qi, v4hi, v2si, v2sf, di) },
18365   { VAR5 (REINTERP, vreinterpretv2sf, v8qi, v4hi, v2si, v2sf, di) },
18366   { VAR5 (REINTERP, vreinterpretdi, v8qi, v4hi, v2si, v2sf, di) },
18367   { VAR5 (REINTERP, vreinterpretv16qi, v16qi, v8hi, v4si, v4sf, v2di) },
18368   { VAR5 (REINTERP, vreinterpretv8hi, v16qi, v8hi, v4si, v4sf, v2di) },
18369   { VAR5 (REINTERP, vreinterpretv4si, v16qi, v8hi, v4si, v4sf, v2di) },
18370   { VAR5 (REINTERP, vreinterpretv4sf, v16qi, v8hi, v4si, v4sf, v2di) },
18371   { VAR5 (REINTERP, vreinterpretv2di, v16qi, v8hi, v4si, v4sf, v2di) },
18372   { VAR10 (LOAD1, vld1,
18373            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18374   { VAR10 (LOAD1LANE, vld1_lane,
18375            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18376   { VAR10 (LOAD1, vld1_dup,
18377            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18378   { VAR10 (STORE1, vst1,
18379            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18380   { VAR10 (STORE1LANE, vst1_lane,
18381            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18382   { VAR9 (LOADSTRUCT,
18383           vld2, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18384   { VAR7 (LOADSTRUCTLANE, vld2_lane,
18385           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18386   { VAR5 (LOADSTRUCT, vld2_dup, v8qi, v4hi, v2si, v2sf, di) },
18387   { VAR9 (STORESTRUCT, vst2,
18388           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18389   { VAR7 (STORESTRUCTLANE, vst2_lane,
18390           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18391   { VAR9 (LOADSTRUCT,
18392           vld3, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18393   { VAR7 (LOADSTRUCTLANE, vld3_lane,
18394           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18395   { VAR5 (LOADSTRUCT, vld3_dup, v8qi, v4hi, v2si, v2sf, di) },
18396   { VAR9 (STORESTRUCT, vst3,
18397           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18398   { VAR7 (STORESTRUCTLANE, vst3_lane,
18399           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18400   { VAR9 (LOADSTRUCT, vld4,
18401           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18402   { VAR7 (LOADSTRUCTLANE, vld4_lane,
18403           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18404   { VAR5 (LOADSTRUCT, vld4_dup, v8qi, v4hi, v2si, v2sf, di) },
18405   { VAR9 (STORESTRUCT, vst4,
18406           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18407   { VAR7 (STORESTRUCTLANE, vst4_lane,
18408           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18409   { VAR10 (LOGICBINOP, vand,
18410            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18411   { VAR10 (LOGICBINOP, vorr,
18412            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18413   { VAR10 (BINOP, veor,
18414            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18415   { VAR10 (LOGICBINOP, vbic,
18416            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18417   { VAR10 (LOGICBINOP, vorn,
18418            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) }
18419 };
18420
18421 #undef CF
18422 #undef VAR1
18423 #undef VAR2
18424 #undef VAR3
18425 #undef VAR4
18426 #undef VAR5
18427 #undef VAR6
18428 #undef VAR7
18429 #undef VAR8
18430 #undef VAR9
18431 #undef VAR10
18432
18433 static void
18434 arm_init_neon_builtins (void)
18435 {
18436   unsigned int i, fcode = ARM_BUILTIN_NEON_BASE;
18437
18438   tree neon_intQI_type_node;
18439   tree neon_intHI_type_node;
18440   tree neon_polyQI_type_node;
18441   tree neon_polyHI_type_node;
18442   tree neon_intSI_type_node;
18443   tree neon_intDI_type_node;
18444   tree neon_float_type_node;
18445
18446   tree intQI_pointer_node;
18447   tree intHI_pointer_node;
18448   tree intSI_pointer_node;
18449   tree intDI_pointer_node;
18450   tree float_pointer_node;
18451
18452   tree const_intQI_node;
18453   tree const_intHI_node;
18454   tree const_intSI_node;
18455   tree const_intDI_node;
18456   tree const_float_node;
18457
18458   tree const_intQI_pointer_node;
18459   tree const_intHI_pointer_node;
18460   tree const_intSI_pointer_node;
18461   tree const_intDI_pointer_node;
18462   tree const_float_pointer_node;
18463
18464   tree V8QI_type_node;
18465   tree V4HI_type_node;
18466   tree V2SI_type_node;
18467   tree V2SF_type_node;
18468   tree V16QI_type_node;
18469   tree V8HI_type_node;
18470   tree V4SI_type_node;
18471   tree V4SF_type_node;
18472   tree V2DI_type_node;
18473
18474   tree intUQI_type_node;
18475   tree intUHI_type_node;
18476   tree intUSI_type_node;
18477   tree intUDI_type_node;
18478
18479   tree intEI_type_node;
18480   tree intOI_type_node;
18481   tree intCI_type_node;
18482   tree intXI_type_node;
18483
18484   tree V8QI_pointer_node;
18485   tree V4HI_pointer_node;
18486   tree V2SI_pointer_node;
18487   tree V2SF_pointer_node;
18488   tree V16QI_pointer_node;
18489   tree V8HI_pointer_node;
18490   tree V4SI_pointer_node;
18491   tree V4SF_pointer_node;
18492   tree V2DI_pointer_node;
18493
18494   tree void_ftype_pv8qi_v8qi_v8qi;
18495   tree void_ftype_pv4hi_v4hi_v4hi;
18496   tree void_ftype_pv2si_v2si_v2si;
18497   tree void_ftype_pv2sf_v2sf_v2sf;
18498   tree void_ftype_pdi_di_di;
18499   tree void_ftype_pv16qi_v16qi_v16qi;
18500   tree void_ftype_pv8hi_v8hi_v8hi;
18501   tree void_ftype_pv4si_v4si_v4si;
18502   tree void_ftype_pv4sf_v4sf_v4sf;
18503   tree void_ftype_pv2di_v2di_v2di;
18504
18505   tree reinterp_ftype_dreg[5][5];
18506   tree reinterp_ftype_qreg[5][5];
18507   tree dreg_types[5], qreg_types[5];
18508
18509   /* Create distinguished type nodes for NEON vector element types,
18510      and pointers to values of such types, so we can detect them later.  */
18511   neon_intQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
18512   neon_intHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
18513   neon_polyQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
18514   neon_polyHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
18515   neon_intSI_type_node = make_signed_type (GET_MODE_PRECISION (SImode));
18516   neon_intDI_type_node = make_signed_type (GET_MODE_PRECISION (DImode));
18517   neon_float_type_node = make_node (REAL_TYPE);
18518   TYPE_PRECISION (neon_float_type_node) = FLOAT_TYPE_SIZE;
18519   layout_type (neon_float_type_node);
18520
18521   /* Define typedefs which exactly correspond to the modes we are basing vector
18522      types on.  If you change these names you'll need to change
18523      the table used by arm_mangle_type too.  */
18524   (*lang_hooks.types.register_builtin_type) (neon_intQI_type_node,
18525                                              "__builtin_neon_qi");
18526   (*lang_hooks.types.register_builtin_type) (neon_intHI_type_node,
18527                                              "__builtin_neon_hi");
18528   (*lang_hooks.types.register_builtin_type) (neon_intSI_type_node,
18529                                              "__builtin_neon_si");
18530   (*lang_hooks.types.register_builtin_type) (neon_float_type_node,
18531                                              "__builtin_neon_sf");
18532   (*lang_hooks.types.register_builtin_type) (neon_intDI_type_node,
18533                                              "__builtin_neon_di");
18534   (*lang_hooks.types.register_builtin_type) (neon_polyQI_type_node,
18535                                              "__builtin_neon_poly8");
18536   (*lang_hooks.types.register_builtin_type) (neon_polyHI_type_node,
18537                                              "__builtin_neon_poly16");
18538
18539   intQI_pointer_node = build_pointer_type (neon_intQI_type_node);
18540   intHI_pointer_node = build_pointer_type (neon_intHI_type_node);
18541   intSI_pointer_node = build_pointer_type (neon_intSI_type_node);
18542   intDI_pointer_node = build_pointer_type (neon_intDI_type_node);
18543   float_pointer_node = build_pointer_type (neon_float_type_node);
18544
18545   /* Next create constant-qualified versions of the above types.  */
18546   const_intQI_node = build_qualified_type (neon_intQI_type_node,
18547                                            TYPE_QUAL_CONST);
18548   const_intHI_node = build_qualified_type (neon_intHI_type_node,
18549                                            TYPE_QUAL_CONST);
18550   const_intSI_node = build_qualified_type (neon_intSI_type_node,
18551                                            TYPE_QUAL_CONST);
18552   const_intDI_node = build_qualified_type (neon_intDI_type_node,
18553                                            TYPE_QUAL_CONST);
18554   const_float_node = build_qualified_type (neon_float_type_node,
18555                                            TYPE_QUAL_CONST);
18556
18557   const_intQI_pointer_node = build_pointer_type (const_intQI_node);
18558   const_intHI_pointer_node = build_pointer_type (const_intHI_node);
18559   const_intSI_pointer_node = build_pointer_type (const_intSI_node);
18560   const_intDI_pointer_node = build_pointer_type (const_intDI_node);
18561   const_float_pointer_node = build_pointer_type (const_float_node);
18562
18563   /* Now create vector types based on our NEON element types.  */
18564   /* 64-bit vectors.  */
18565   V8QI_type_node =
18566     build_vector_type_for_mode (neon_intQI_type_node, V8QImode);
18567   V4HI_type_node =
18568     build_vector_type_for_mode (neon_intHI_type_node, V4HImode);
18569   V2SI_type_node =
18570     build_vector_type_for_mode (neon_intSI_type_node, V2SImode);
18571   V2SF_type_node =
18572     build_vector_type_for_mode (neon_float_type_node, V2SFmode);
18573   /* 128-bit vectors.  */
18574   V16QI_type_node =
18575     build_vector_type_for_mode (neon_intQI_type_node, V16QImode);
18576   V8HI_type_node =
18577     build_vector_type_for_mode (neon_intHI_type_node, V8HImode);
18578   V4SI_type_node =
18579     build_vector_type_for_mode (neon_intSI_type_node, V4SImode);
18580   V4SF_type_node =
18581     build_vector_type_for_mode (neon_float_type_node, V4SFmode);
18582   V2DI_type_node =
18583     build_vector_type_for_mode (neon_intDI_type_node, V2DImode);
18584
18585   /* Unsigned integer types for various mode sizes.  */
18586   intUQI_type_node = make_unsigned_type (GET_MODE_PRECISION (QImode));
18587   intUHI_type_node = make_unsigned_type (GET_MODE_PRECISION (HImode));
18588   intUSI_type_node = make_unsigned_type (GET_MODE_PRECISION (SImode));
18589   intUDI_type_node = make_unsigned_type (GET_MODE_PRECISION (DImode));
18590
18591   (*lang_hooks.types.register_builtin_type) (intUQI_type_node,
18592                                              "__builtin_neon_uqi");
18593   (*lang_hooks.types.register_builtin_type) (intUHI_type_node,
18594                                              "__builtin_neon_uhi");
18595   (*lang_hooks.types.register_builtin_type) (intUSI_type_node,
18596                                              "__builtin_neon_usi");
18597   (*lang_hooks.types.register_builtin_type) (intUDI_type_node,
18598                                              "__builtin_neon_udi");
18599
18600   /* Opaque integer types for structures of vectors.  */
18601   intEI_type_node = make_signed_type (GET_MODE_PRECISION (EImode));
18602   intOI_type_node = make_signed_type (GET_MODE_PRECISION (OImode));
18603   intCI_type_node = make_signed_type (GET_MODE_PRECISION (CImode));
18604   intXI_type_node = make_signed_type (GET_MODE_PRECISION (XImode));
18605
18606   (*lang_hooks.types.register_builtin_type) (intTI_type_node,
18607                                              "__builtin_neon_ti");
18608   (*lang_hooks.types.register_builtin_type) (intEI_type_node,
18609                                              "__builtin_neon_ei");
18610   (*lang_hooks.types.register_builtin_type) (intOI_type_node,
18611                                              "__builtin_neon_oi");
18612   (*lang_hooks.types.register_builtin_type) (intCI_type_node,
18613                                              "__builtin_neon_ci");
18614   (*lang_hooks.types.register_builtin_type) (intXI_type_node,
18615                                              "__builtin_neon_xi");
18616
18617   /* Pointers to vector types.  */
18618   V8QI_pointer_node = build_pointer_type (V8QI_type_node);
18619   V4HI_pointer_node = build_pointer_type (V4HI_type_node);
18620   V2SI_pointer_node = build_pointer_type (V2SI_type_node);
18621   V2SF_pointer_node = build_pointer_type (V2SF_type_node);
18622   V16QI_pointer_node = build_pointer_type (V16QI_type_node);
18623   V8HI_pointer_node = build_pointer_type (V8HI_type_node);
18624   V4SI_pointer_node = build_pointer_type (V4SI_type_node);
18625   V4SF_pointer_node = build_pointer_type (V4SF_type_node);
18626   V2DI_pointer_node = build_pointer_type (V2DI_type_node);
18627
18628   /* Operations which return results as pairs.  */
18629   void_ftype_pv8qi_v8qi_v8qi =
18630     build_function_type_list (void_type_node, V8QI_pointer_node, V8QI_type_node,
18631                               V8QI_type_node, NULL);
18632   void_ftype_pv4hi_v4hi_v4hi =
18633     build_function_type_list (void_type_node, V4HI_pointer_node, V4HI_type_node,
18634                               V4HI_type_node, NULL);
18635   void_ftype_pv2si_v2si_v2si =
18636     build_function_type_list (void_type_node, V2SI_pointer_node, V2SI_type_node,
18637                               V2SI_type_node, NULL);
18638   void_ftype_pv2sf_v2sf_v2sf =
18639     build_function_type_list (void_type_node, V2SF_pointer_node, V2SF_type_node,
18640                               V2SF_type_node, NULL);
18641   void_ftype_pdi_di_di =
18642     build_function_type_list (void_type_node, intDI_pointer_node,
18643                               neon_intDI_type_node, neon_intDI_type_node, NULL);
18644   void_ftype_pv16qi_v16qi_v16qi =
18645     build_function_type_list (void_type_node, V16QI_pointer_node,
18646                               V16QI_type_node, V16QI_type_node, NULL);
18647   void_ftype_pv8hi_v8hi_v8hi =
18648     build_function_type_list (void_type_node, V8HI_pointer_node, V8HI_type_node,
18649                               V8HI_type_node, NULL);
18650   void_ftype_pv4si_v4si_v4si =
18651     build_function_type_list (void_type_node, V4SI_pointer_node, V4SI_type_node,
18652                               V4SI_type_node, NULL);
18653   void_ftype_pv4sf_v4sf_v4sf =
18654     build_function_type_list (void_type_node, V4SF_pointer_node, V4SF_type_node,
18655                               V4SF_type_node, NULL);
18656   void_ftype_pv2di_v2di_v2di =
18657     build_function_type_list (void_type_node, V2DI_pointer_node, V2DI_type_node,
18658                               V2DI_type_node, NULL);
18659
18660   dreg_types[0] = V8QI_type_node;
18661   dreg_types[1] = V4HI_type_node;
18662   dreg_types[2] = V2SI_type_node;
18663   dreg_types[3] = V2SF_type_node;
18664   dreg_types[4] = neon_intDI_type_node;
18665
18666   qreg_types[0] = V16QI_type_node;
18667   qreg_types[1] = V8HI_type_node;
18668   qreg_types[2] = V4SI_type_node;
18669   qreg_types[3] = V4SF_type_node;
18670   qreg_types[4] = V2DI_type_node;
18671
18672   for (i = 0; i < 5; i++)
18673     {
18674       int j;
18675       for (j = 0; j < 5; j++)
18676         {
18677           reinterp_ftype_dreg[i][j]
18678             = build_function_type_list (dreg_types[i], dreg_types[j], NULL);
18679           reinterp_ftype_qreg[i][j]
18680             = build_function_type_list (qreg_types[i], qreg_types[j], NULL);
18681         }
18682     }
18683
18684   for (i = 0; i < ARRAY_SIZE (neon_builtin_data); i++)
18685     {
18686       neon_builtin_datum *d = &neon_builtin_data[i];
18687       unsigned int j, codeidx = 0;
18688
18689       d->base_fcode = fcode;
18690
18691       for (j = 0; j < T_MAX; j++)
18692         {
18693           const char* const modenames[] = {
18694             "v8qi", "v4hi", "v2si", "v2sf", "di",
18695             "v16qi", "v8hi", "v4si", "v4sf", "v2di"
18696           };
18697           char namebuf[60];
18698           tree ftype = NULL;
18699           enum insn_code icode;
18700           int is_load = 0, is_store = 0;
18701
18702           if ((d->bits & (1 << j)) == 0)
18703             continue;
18704
18705           icode = d->codes[codeidx++];
18706
18707           switch (d->itype)
18708             {
18709             case NEON_LOAD1:
18710             case NEON_LOAD1LANE:
18711             case NEON_LOADSTRUCT:
18712             case NEON_LOADSTRUCTLANE:
18713               is_load = 1;
18714               /* Fall through.  */
18715             case NEON_STORE1:
18716             case NEON_STORE1LANE:
18717             case NEON_STORESTRUCT:
18718             case NEON_STORESTRUCTLANE:
18719               if (!is_load)
18720                 is_store = 1;
18721               /* Fall through.  */
18722             case NEON_UNOP:
18723             case NEON_BINOP:
18724             case NEON_LOGICBINOP:
18725             case NEON_SHIFTINSERT:
18726             case NEON_TERNOP:
18727             case NEON_GETLANE:
18728             case NEON_SETLANE:
18729             case NEON_CREATE:
18730             case NEON_DUP:
18731             case NEON_DUPLANE:
18732             case NEON_SHIFTIMM:
18733             case NEON_SHIFTACC:
18734             case NEON_COMBINE:
18735             case NEON_SPLIT:
18736             case NEON_CONVERT:
18737             case NEON_FIXCONV:
18738             case NEON_LANEMUL:
18739             case NEON_LANEMULL:
18740             case NEON_LANEMULH:
18741             case NEON_LANEMAC:
18742             case NEON_SCALARMUL:
18743             case NEON_SCALARMULL:
18744             case NEON_SCALARMULH:
18745             case NEON_SCALARMAC:
18746             case NEON_SELECT:
18747             case NEON_VTBL:
18748             case NEON_VTBX:
18749               {
18750                 int k;
18751                 tree return_type = void_type_node, args = void_list_node;
18752
18753                 /* Build a function type directly from the insn_data for this
18754                    builtin.  The build_function_type() function takes care of
18755                    removing duplicates for us.  */
18756                 for (k = insn_data[icode].n_operands - 1; k >= 0; k--)
18757                   {
18758                     tree eltype;
18759
18760                     if (is_load && k == 1)
18761                       {
18762                         /* Neon load patterns always have the memory operand
18763                            (a SImode pointer) in the operand 1 position.  We
18764                            want a const pointer to the element type in that
18765                            position.  */
18766                         gcc_assert (insn_data[icode].operand[k].mode == SImode);
18767
18768                         switch (1 << j)
18769                           {
18770                           case T_V8QI:
18771                           case T_V16QI:
18772                             eltype = const_intQI_pointer_node;
18773                             break;
18774
18775                           case T_V4HI:
18776                           case T_V8HI:
18777                             eltype = const_intHI_pointer_node;
18778                             break;
18779
18780                           case T_V2SI:
18781                           case T_V4SI:
18782                             eltype = const_intSI_pointer_node;
18783                             break;
18784
18785                           case T_V2SF:
18786                           case T_V4SF:
18787                             eltype = const_float_pointer_node;
18788                             break;
18789
18790                           case T_DI:
18791                           case T_V2DI:
18792                             eltype = const_intDI_pointer_node;
18793                             break;
18794
18795                           default: gcc_unreachable ();
18796                           }
18797                       }
18798                     else if (is_store && k == 0)
18799                       {
18800                         /* Similarly, Neon store patterns use operand 0 as
18801                            the memory location to store to (a SImode pointer).
18802                            Use a pointer to the element type of the store in
18803                            that position.  */
18804                         gcc_assert (insn_data[icode].operand[k].mode == SImode);
18805
18806                         switch (1 << j)
18807                           {
18808                           case T_V8QI:
18809                           case T_V16QI:
18810                             eltype = intQI_pointer_node;
18811                             break;
18812
18813                           case T_V4HI:
18814                           case T_V8HI:
18815                             eltype = intHI_pointer_node;
18816                             break;
18817
18818                           case T_V2SI:
18819                           case T_V4SI:
18820                             eltype = intSI_pointer_node;
18821                             break;
18822
18823                           case T_V2SF:
18824                           case T_V4SF:
18825                             eltype = float_pointer_node;
18826                             break;
18827
18828                           case T_DI:
18829                           case T_V2DI:
18830                             eltype = intDI_pointer_node;
18831                             break;
18832
18833                           default: gcc_unreachable ();
18834                           }
18835                       }
18836                     else
18837                       {
18838                         switch (insn_data[icode].operand[k].mode)
18839                           {
18840                           case VOIDmode: eltype = void_type_node; break;
18841                           /* Scalars.  */
18842                           case QImode: eltype = neon_intQI_type_node; break;
18843                           case HImode: eltype = neon_intHI_type_node; break;
18844                           case SImode: eltype = neon_intSI_type_node; break;
18845                           case SFmode: eltype = neon_float_type_node; break;
18846                           case DImode: eltype = neon_intDI_type_node; break;
18847                           case TImode: eltype = intTI_type_node; break;
18848                           case EImode: eltype = intEI_type_node; break;
18849                           case OImode: eltype = intOI_type_node; break;
18850                           case CImode: eltype = intCI_type_node; break;
18851                           case XImode: eltype = intXI_type_node; break;
18852                           /* 64-bit vectors.  */
18853                           case V8QImode: eltype = V8QI_type_node; break;
18854                           case V4HImode: eltype = V4HI_type_node; break;
18855                           case V2SImode: eltype = V2SI_type_node; break;
18856                           case V2SFmode: eltype = V2SF_type_node; break;
18857                           /* 128-bit vectors.  */
18858                           case V16QImode: eltype = V16QI_type_node; break;
18859                           case V8HImode: eltype = V8HI_type_node; break;
18860                           case V4SImode: eltype = V4SI_type_node; break;
18861                           case V4SFmode: eltype = V4SF_type_node; break;
18862                           case V2DImode: eltype = V2DI_type_node; break;
18863                           default: gcc_unreachable ();
18864                           }
18865                       }
18866
18867                     if (k == 0 && !is_store)
18868                       return_type = eltype;
18869                     else
18870                       args = tree_cons (NULL_TREE, eltype, args);
18871                   }
18872
18873                 ftype = build_function_type (return_type, args);
18874               }
18875               break;
18876
18877             case NEON_RESULTPAIR:
18878               {
18879                 switch (insn_data[icode].operand[1].mode)
18880                   {
18881                   case V8QImode: ftype = void_ftype_pv8qi_v8qi_v8qi; break;
18882                   case V4HImode: ftype = void_ftype_pv4hi_v4hi_v4hi; break;
18883                   case V2SImode: ftype = void_ftype_pv2si_v2si_v2si; break;
18884                   case V2SFmode: ftype = void_ftype_pv2sf_v2sf_v2sf; break;
18885                   case DImode: ftype = void_ftype_pdi_di_di; break;
18886                   case V16QImode: ftype = void_ftype_pv16qi_v16qi_v16qi; break;
18887                   case V8HImode: ftype = void_ftype_pv8hi_v8hi_v8hi; break;
18888                   case V4SImode: ftype = void_ftype_pv4si_v4si_v4si; break;
18889                   case V4SFmode: ftype = void_ftype_pv4sf_v4sf_v4sf; break;
18890                   case V2DImode: ftype = void_ftype_pv2di_v2di_v2di; break;
18891                   default: gcc_unreachable ();
18892                   }
18893               }
18894               break;
18895
18896             case NEON_REINTERP:
18897               {
18898                 /* We iterate over 5 doubleword types, then 5 quadword
18899                    types.  */
18900                 int rhs = j % 5;
18901                 switch (insn_data[icode].operand[0].mode)
18902                   {
18903                   case V8QImode: ftype = reinterp_ftype_dreg[0][rhs]; break;
18904                   case V4HImode: ftype = reinterp_ftype_dreg[1][rhs]; break;
18905                   case V2SImode: ftype = reinterp_ftype_dreg[2][rhs]; break;
18906                   case V2SFmode: ftype = reinterp_ftype_dreg[3][rhs]; break;
18907                   case DImode: ftype = reinterp_ftype_dreg[4][rhs]; break;
18908                   case V16QImode: ftype = reinterp_ftype_qreg[0][rhs]; break;
18909                   case V8HImode: ftype = reinterp_ftype_qreg[1][rhs]; break;
18910                   case V4SImode: ftype = reinterp_ftype_qreg[2][rhs]; break;
18911                   case V4SFmode: ftype = reinterp_ftype_qreg[3][rhs]; break;
18912                   case V2DImode: ftype = reinterp_ftype_qreg[4][rhs]; break;
18913                   default: gcc_unreachable ();
18914                   }
18915               }
18916               break;
18917
18918             default:
18919               gcc_unreachable ();
18920             }
18921
18922           gcc_assert (ftype != NULL);
18923
18924           sprintf (namebuf, "__builtin_neon_%s%s", d->name, modenames[j]);
18925
18926           add_builtin_function (namebuf, ftype, fcode++, BUILT_IN_MD, NULL,
18927                                 NULL_TREE);
18928         }
18929     }
18930 }
18931
18932 static void
18933 arm_init_fp16_builtins (void)
18934 {
18935   tree fp16_type = make_node (REAL_TYPE);
18936   TYPE_PRECISION (fp16_type) = 16;
18937   layout_type (fp16_type);
18938   (*lang_hooks.types.register_builtin_type) (fp16_type, "__fp16");
18939 }
18940
18941 static void
18942 arm_init_builtins (void)
18943 {
18944   arm_init_tls_builtins ();
18945
18946   if (TARGET_REALLY_IWMMXT)
18947     arm_init_iwmmxt_builtins ();
18948
18949   if (TARGET_NEON)
18950     arm_init_neon_builtins ();
18951
18952   if (arm_fp16_format)
18953     arm_init_fp16_builtins ();
18954 }
18955
18956 /* Implement TARGET_INVALID_PARAMETER_TYPE.  */
18957
18958 static const char *
18959 arm_invalid_parameter_type (const_tree t)
18960 {
18961   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
18962     return N_("function parameters cannot have __fp16 type");
18963   return NULL;
18964 }
18965
18966 /* Implement TARGET_INVALID_PARAMETER_TYPE.  */
18967
18968 static const char *
18969 arm_invalid_return_type (const_tree t)
18970 {
18971   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
18972     return N_("functions cannot return __fp16 type");
18973   return NULL;
18974 }
18975
18976 /* Implement TARGET_PROMOTED_TYPE.  */
18977
18978 static tree
18979 arm_promoted_type (const_tree t)
18980 {
18981   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
18982     return float_type_node;
18983   return NULL_TREE;
18984 }
18985
18986 /* Implement TARGET_CONVERT_TO_TYPE.
18987    Specifically, this hook implements the peculiarity of the ARM
18988    half-precision floating-point C semantics that requires conversions between
18989    __fp16 to or from double to do an intermediate conversion to float.  */
18990
18991 static tree
18992 arm_convert_to_type (tree type, tree expr)
18993 {
18994   tree fromtype = TREE_TYPE (expr);
18995   if (!SCALAR_FLOAT_TYPE_P (fromtype) || !SCALAR_FLOAT_TYPE_P (type))
18996     return NULL_TREE;
18997   if ((TYPE_PRECISION (fromtype) == 16 && TYPE_PRECISION (type) > 32)
18998       || (TYPE_PRECISION (type) == 16 && TYPE_PRECISION (fromtype) > 32))
18999     return convert (type, convert (float_type_node, expr));
19000   return NULL_TREE;
19001 }
19002
19003 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P.
19004    This simply adds HFmode as a supported mode; even though we don't
19005    implement arithmetic on this type directly, it's supported by
19006    optabs conversions, much the way the double-word arithmetic is
19007    special-cased in the default hook.  */
19008
19009 static bool
19010 arm_scalar_mode_supported_p (enum machine_mode mode)
19011 {
19012   if (mode == HFmode)
19013     return (arm_fp16_format != ARM_FP16_FORMAT_NONE);
19014   else
19015     return default_scalar_mode_supported_p (mode);
19016 }
19017
19018 /* Errors in the source file can cause expand_expr to return const0_rtx
19019    where we expect a vector.  To avoid crashing, use one of the vector
19020    clear instructions.  */
19021
19022 static rtx
19023 safe_vector_operand (rtx x, enum machine_mode mode)
19024 {
19025   if (x != const0_rtx)
19026     return x;
19027   x = gen_reg_rtx (mode);
19028
19029   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
19030                                : gen_rtx_SUBREG (DImode, x, 0)));
19031   return x;
19032 }
19033
19034 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
19035
19036 static rtx
19037 arm_expand_binop_builtin (enum insn_code icode,
19038                           tree exp, rtx target)
19039 {
19040   rtx pat;
19041   tree arg0 = CALL_EXPR_ARG (exp, 0);
19042   tree arg1 = CALL_EXPR_ARG (exp, 1);
19043   rtx op0 = expand_normal (arg0);
19044   rtx op1 = expand_normal (arg1);
19045   enum machine_mode tmode = insn_data[icode].operand[0].mode;
19046   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
19047   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
19048
19049   if (VECTOR_MODE_P (mode0))
19050     op0 = safe_vector_operand (op0, mode0);
19051   if (VECTOR_MODE_P (mode1))
19052     op1 = safe_vector_operand (op1, mode1);
19053
19054   if (! target
19055       || GET_MODE (target) != tmode
19056       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19057     target = gen_reg_rtx (tmode);
19058
19059   gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
19060
19061   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19062     op0 = copy_to_mode_reg (mode0, op0);
19063   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
19064     op1 = copy_to_mode_reg (mode1, op1);
19065
19066   pat = GEN_FCN (icode) (target, op0, op1);
19067   if (! pat)
19068     return 0;
19069   emit_insn (pat);
19070   return target;
19071 }
19072
19073 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
19074
19075 static rtx
19076 arm_expand_unop_builtin (enum insn_code icode,
19077                          tree exp, rtx target, int do_load)
19078 {
19079   rtx pat;
19080   tree arg0 = CALL_EXPR_ARG (exp, 0);
19081   rtx op0 = expand_normal (arg0);
19082   enum machine_mode tmode = insn_data[icode].operand[0].mode;
19083   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
19084
19085   if (! target
19086       || GET_MODE (target) != tmode
19087       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19088     target = gen_reg_rtx (tmode);
19089   if (do_load)
19090     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
19091   else
19092     {
19093       if (VECTOR_MODE_P (mode0))
19094         op0 = safe_vector_operand (op0, mode0);
19095
19096       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19097         op0 = copy_to_mode_reg (mode0, op0);
19098     }
19099
19100   pat = GEN_FCN (icode) (target, op0);
19101   if (! pat)
19102     return 0;
19103   emit_insn (pat);
19104   return target;
19105 }
19106
19107 static int
19108 neon_builtin_compare (const void *a, const void *b)
19109 {
19110   const neon_builtin_datum *const key = (const neon_builtin_datum *) a;
19111   const neon_builtin_datum *const memb = (const neon_builtin_datum *) b;
19112   unsigned int soughtcode = key->base_fcode;
19113
19114   if (soughtcode >= memb->base_fcode
19115       && soughtcode < memb->base_fcode + memb->num_vars)
19116     return 0;
19117   else if (soughtcode < memb->base_fcode)
19118     return -1;
19119   else
19120     return 1;
19121 }
19122
19123 static enum insn_code
19124 locate_neon_builtin_icode (int fcode, neon_itype *itype)
19125 {
19126   neon_builtin_datum key
19127     = { NULL, (neon_itype) 0, 0, { CODE_FOR_nothing }, 0, 0 };
19128   neon_builtin_datum *found;
19129   int idx;
19130
19131   key.base_fcode = fcode;
19132   found = (neon_builtin_datum *)
19133     bsearch (&key, &neon_builtin_data[0], ARRAY_SIZE (neon_builtin_data),
19134                    sizeof (neon_builtin_data[0]), neon_builtin_compare);
19135   gcc_assert (found);
19136   idx = fcode - (int) found->base_fcode;
19137   gcc_assert (idx >= 0 && idx < T_MAX && idx < (int)found->num_vars);
19138
19139   if (itype)
19140     *itype = found->itype;
19141
19142   return found->codes[idx];
19143 }
19144
19145 typedef enum {
19146   NEON_ARG_COPY_TO_REG,
19147   NEON_ARG_CONSTANT,
19148   NEON_ARG_STOP
19149 } builtin_arg;
19150
19151 #define NEON_MAX_BUILTIN_ARGS 5
19152
19153 /* Expand a Neon builtin.  */
19154 static rtx
19155 arm_expand_neon_args (rtx target, int icode, int have_retval,
19156                       tree exp, ...)
19157 {
19158   va_list ap;
19159   rtx pat;
19160   tree arg[NEON_MAX_BUILTIN_ARGS];
19161   rtx op[NEON_MAX_BUILTIN_ARGS];
19162   enum machine_mode tmode = insn_data[icode].operand[0].mode;
19163   enum machine_mode mode[NEON_MAX_BUILTIN_ARGS];
19164   int argc = 0;
19165
19166   if (have_retval
19167       && (!target
19168           || GET_MODE (target) != tmode
19169           || !(*insn_data[icode].operand[0].predicate) (target, tmode)))
19170     target = gen_reg_rtx (tmode);
19171
19172   va_start (ap, exp);
19173
19174   for (;;)
19175     {
19176       builtin_arg thisarg = (builtin_arg) va_arg (ap, int);
19177
19178       if (thisarg == NEON_ARG_STOP)
19179         break;
19180       else
19181         {
19182           arg[argc] = CALL_EXPR_ARG (exp, argc);
19183           op[argc] = expand_normal (arg[argc]);
19184           mode[argc] = insn_data[icode].operand[argc + have_retval].mode;
19185
19186           switch (thisarg)
19187             {
19188             case NEON_ARG_COPY_TO_REG:
19189               /*gcc_assert (GET_MODE (op[argc]) == mode[argc]);*/
19190               if (!(*insn_data[icode].operand[argc + have_retval].predicate)
19191                      (op[argc], mode[argc]))
19192                 op[argc] = copy_to_mode_reg (mode[argc], op[argc]);
19193               break;
19194
19195             case NEON_ARG_CONSTANT:
19196               /* FIXME: This error message is somewhat unhelpful.  */
19197               if (!(*insn_data[icode].operand[argc + have_retval].predicate)
19198                     (op[argc], mode[argc]))
19199                 error ("argument must be a constant");
19200               break;
19201
19202             case NEON_ARG_STOP:
19203               gcc_unreachable ();
19204             }
19205
19206           argc++;
19207         }
19208     }
19209
19210   va_end (ap);
19211
19212   if (have_retval)
19213     switch (argc)
19214       {
19215       case 1:
19216         pat = GEN_FCN (icode) (target, op[0]);
19217         break;
19218
19219       case 2:
19220         pat = GEN_FCN (icode) (target, op[0], op[1]);
19221         break;
19222
19223       case 3:
19224         pat = GEN_FCN (icode) (target, op[0], op[1], op[2]);
19225         break;
19226
19227       case 4:
19228         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3]);
19229         break;
19230
19231       case 5:
19232         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3], op[4]);
19233         break;
19234
19235       default:
19236         gcc_unreachable ();
19237       }
19238   else
19239     switch (argc)
19240       {
19241       case 1:
19242         pat = GEN_FCN (icode) (op[0]);
19243         break;
19244
19245       case 2:
19246         pat = GEN_FCN (icode) (op[0], op[1]);
19247         break;
19248
19249       case 3:
19250         pat = GEN_FCN (icode) (op[0], op[1], op[2]);
19251         break;
19252
19253       case 4:
19254         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
19255         break;
19256
19257       case 5:
19258         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4]);
19259         break;
19260
19261       default:
19262         gcc_unreachable ();
19263       }
19264
19265   if (!pat)
19266     return 0;
19267
19268   emit_insn (pat);
19269
19270   return target;
19271 }
19272
19273 /* Expand a Neon builtin. These are "special" because they don't have symbolic
19274    constants defined per-instruction or per instruction-variant. Instead, the
19275    required info is looked up in the table neon_builtin_data.  */
19276 static rtx
19277 arm_expand_neon_builtin (int fcode, tree exp, rtx target)
19278 {
19279   neon_itype itype;
19280   enum insn_code icode = locate_neon_builtin_icode (fcode, &itype);
19281
19282   switch (itype)
19283     {
19284     case NEON_UNOP:
19285     case NEON_CONVERT:
19286     case NEON_DUPLANE:
19287       return arm_expand_neon_args (target, icode, 1, exp,
19288         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_STOP);
19289
19290     case NEON_BINOP:
19291     case NEON_SETLANE:
19292     case NEON_SCALARMUL:
19293     case NEON_SCALARMULL:
19294     case NEON_SCALARMULH:
19295     case NEON_SHIFTINSERT:
19296     case NEON_LOGICBINOP:
19297       return arm_expand_neon_args (target, icode, 1, exp,
19298         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19299         NEON_ARG_STOP);
19300
19301     case NEON_TERNOP:
19302       return arm_expand_neon_args (target, icode, 1, exp,
19303         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19304         NEON_ARG_CONSTANT, NEON_ARG_STOP);
19305
19306     case NEON_GETLANE:
19307     case NEON_FIXCONV:
19308     case NEON_SHIFTIMM:
19309       return arm_expand_neon_args (target, icode, 1, exp,
19310         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_CONSTANT,
19311         NEON_ARG_STOP);
19312
19313     case NEON_CREATE:
19314       return arm_expand_neon_args (target, icode, 1, exp,
19315         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19316
19317     case NEON_DUP:
19318     case NEON_SPLIT:
19319     case NEON_REINTERP:
19320       return arm_expand_neon_args (target, icode, 1, exp,
19321         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19322
19323     case NEON_COMBINE:
19324     case NEON_VTBL:
19325       return arm_expand_neon_args (target, icode, 1, exp,
19326         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19327
19328     case NEON_RESULTPAIR:
19329       return arm_expand_neon_args (target, icode, 0, exp,
19330         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19331         NEON_ARG_STOP);
19332
19333     case NEON_LANEMUL:
19334     case NEON_LANEMULL:
19335     case NEON_LANEMULH:
19336       return arm_expand_neon_args (target, icode, 1, exp,
19337         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19338         NEON_ARG_CONSTANT, NEON_ARG_STOP);
19339
19340     case NEON_LANEMAC:
19341       return arm_expand_neon_args (target, icode, 1, exp,
19342         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19343         NEON_ARG_CONSTANT, NEON_ARG_CONSTANT, NEON_ARG_STOP);
19344
19345     case NEON_SHIFTACC:
19346       return arm_expand_neon_args (target, icode, 1, exp,
19347         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19348         NEON_ARG_CONSTANT, NEON_ARG_STOP);
19349
19350     case NEON_SCALARMAC:
19351       return arm_expand_neon_args (target, icode, 1, exp,
19352         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19353         NEON_ARG_CONSTANT, NEON_ARG_STOP);
19354
19355     case NEON_SELECT:
19356     case NEON_VTBX:
19357       return arm_expand_neon_args (target, icode, 1, exp,
19358         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19359         NEON_ARG_STOP);
19360
19361     case NEON_LOAD1:
19362     case NEON_LOADSTRUCT:
19363       return arm_expand_neon_args (target, icode, 1, exp,
19364         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19365
19366     case NEON_LOAD1LANE:
19367     case NEON_LOADSTRUCTLANE:
19368       return arm_expand_neon_args (target, icode, 1, exp,
19369         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19370         NEON_ARG_STOP);
19371
19372     case NEON_STORE1:
19373     case NEON_STORESTRUCT:
19374       return arm_expand_neon_args (target, icode, 0, exp,
19375         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19376
19377     case NEON_STORE1LANE:
19378     case NEON_STORESTRUCTLANE:
19379       return arm_expand_neon_args (target, icode, 0, exp,
19380         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19381         NEON_ARG_STOP);
19382     }
19383
19384   gcc_unreachable ();
19385 }
19386
19387 /* Emit code to reinterpret one Neon type as another, without altering bits.  */
19388 void
19389 neon_reinterpret (rtx dest, rtx src)
19390 {
19391   emit_move_insn (dest, gen_lowpart (GET_MODE (dest), src));
19392 }
19393
19394 /* Emit code to place a Neon pair result in memory locations (with equal
19395    registers).  */
19396 void
19397 neon_emit_pair_result_insn (enum machine_mode mode,
19398                             rtx (*intfn) (rtx, rtx, rtx, rtx), rtx destaddr,
19399                             rtx op1, rtx op2)
19400 {
19401   rtx mem = gen_rtx_MEM (mode, destaddr);
19402   rtx tmp1 = gen_reg_rtx (mode);
19403   rtx tmp2 = gen_reg_rtx (mode);
19404
19405   emit_insn (intfn (tmp1, op1, tmp2, op2));
19406
19407   emit_move_insn (mem, tmp1);
19408   mem = adjust_address (mem, mode, GET_MODE_SIZE (mode));
19409   emit_move_insn (mem, tmp2);
19410 }
19411
19412 /* Set up operands for a register copy from src to dest, taking care not to
19413    clobber registers in the process.
19414    FIXME: This has rather high polynomial complexity (O(n^3)?) but shouldn't
19415    be called with a large N, so that should be OK.  */
19416
19417 void
19418 neon_disambiguate_copy (rtx *operands, rtx *dest, rtx *src, unsigned int count)
19419 {
19420   unsigned int copied = 0, opctr = 0;
19421   unsigned int done = (1 << count) - 1;
19422   unsigned int i, j;
19423
19424   while (copied != done)
19425     {
19426       for (i = 0; i < count; i++)
19427         {
19428           int good = 1;
19429
19430           for (j = 0; good && j < count; j++)
19431             if (i != j && (copied & (1 << j)) == 0
19432                 && reg_overlap_mentioned_p (src[j], dest[i]))
19433               good = 0;
19434
19435           if (good)
19436             {
19437               operands[opctr++] = dest[i];
19438               operands[opctr++] = src[i];
19439               copied |= 1 << i;
19440             }
19441         }
19442     }
19443
19444   gcc_assert (opctr == count * 2);
19445 }
19446
19447 /* Expand an expression EXP that calls a built-in function,
19448    with result going to TARGET if that's convenient
19449    (and in mode MODE if that's convenient).
19450    SUBTARGET may be used as the target for computing one of EXP's operands.
19451    IGNORE is nonzero if the value is to be ignored.  */
19452
19453 static rtx
19454 arm_expand_builtin (tree exp,
19455                     rtx target,
19456                     rtx subtarget ATTRIBUTE_UNUSED,
19457                     enum machine_mode mode ATTRIBUTE_UNUSED,
19458                     int ignore ATTRIBUTE_UNUSED)
19459 {
19460   const struct builtin_description * d;
19461   enum insn_code    icode;
19462   tree              fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
19463   tree              arg0;
19464   tree              arg1;
19465   tree              arg2;
19466   rtx               op0;
19467   rtx               op1;
19468   rtx               op2;
19469   rtx               pat;
19470   int               fcode = DECL_FUNCTION_CODE (fndecl);
19471   size_t            i;
19472   enum machine_mode tmode;
19473   enum machine_mode mode0;
19474   enum machine_mode mode1;
19475   enum machine_mode mode2;
19476
19477   if (fcode >= ARM_BUILTIN_NEON_BASE)
19478     return arm_expand_neon_builtin (fcode, exp, target);
19479
19480   switch (fcode)
19481     {
19482     case ARM_BUILTIN_TEXTRMSB:
19483     case ARM_BUILTIN_TEXTRMUB:
19484     case ARM_BUILTIN_TEXTRMSH:
19485     case ARM_BUILTIN_TEXTRMUH:
19486     case ARM_BUILTIN_TEXTRMSW:
19487     case ARM_BUILTIN_TEXTRMUW:
19488       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
19489                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
19490                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
19491                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
19492                : CODE_FOR_iwmmxt_textrmw);
19493
19494       arg0 = CALL_EXPR_ARG (exp, 0);
19495       arg1 = CALL_EXPR_ARG (exp, 1);
19496       op0 = expand_normal (arg0);
19497       op1 = expand_normal (arg1);
19498       tmode = insn_data[icode].operand[0].mode;
19499       mode0 = insn_data[icode].operand[1].mode;
19500       mode1 = insn_data[icode].operand[2].mode;
19501
19502       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19503         op0 = copy_to_mode_reg (mode0, op0);
19504       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
19505         {
19506           /* @@@ better error message */
19507           error ("selector must be an immediate");
19508           return gen_reg_rtx (tmode);
19509         }
19510       if (target == 0
19511           || GET_MODE (target) != tmode
19512           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19513         target = gen_reg_rtx (tmode);
19514       pat = GEN_FCN (icode) (target, op0, op1);
19515       if (! pat)
19516         return 0;
19517       emit_insn (pat);
19518       return target;
19519
19520     case ARM_BUILTIN_TINSRB:
19521     case ARM_BUILTIN_TINSRH:
19522     case ARM_BUILTIN_TINSRW:
19523       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
19524                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
19525                : CODE_FOR_iwmmxt_tinsrw);
19526       arg0 = CALL_EXPR_ARG (exp, 0);
19527       arg1 = CALL_EXPR_ARG (exp, 1);
19528       arg2 = CALL_EXPR_ARG (exp, 2);
19529       op0 = expand_normal (arg0);
19530       op1 = expand_normal (arg1);
19531       op2 = expand_normal (arg2);
19532       tmode = insn_data[icode].operand[0].mode;
19533       mode0 = insn_data[icode].operand[1].mode;
19534       mode1 = insn_data[icode].operand[2].mode;
19535       mode2 = insn_data[icode].operand[3].mode;
19536
19537       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19538         op0 = copy_to_mode_reg (mode0, op0);
19539       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
19540         op1 = copy_to_mode_reg (mode1, op1);
19541       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
19542         {
19543           /* @@@ better error message */
19544           error ("selector must be an immediate");
19545           return const0_rtx;
19546         }
19547       if (target == 0
19548           || GET_MODE (target) != tmode
19549           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19550         target = gen_reg_rtx (tmode);
19551       pat = GEN_FCN (icode) (target, op0, op1, op2);
19552       if (! pat)
19553         return 0;
19554       emit_insn (pat);
19555       return target;
19556
19557     case ARM_BUILTIN_SETWCX:
19558       arg0 = CALL_EXPR_ARG (exp, 0);
19559       arg1 = CALL_EXPR_ARG (exp, 1);
19560       op0 = force_reg (SImode, expand_normal (arg0));
19561       op1 = expand_normal (arg1);
19562       emit_insn (gen_iwmmxt_tmcr (op1, op0));
19563       return 0;
19564
19565     case ARM_BUILTIN_GETWCX:
19566       arg0 = CALL_EXPR_ARG (exp, 0);
19567       op0 = expand_normal (arg0);
19568       target = gen_reg_rtx (SImode);
19569       emit_insn (gen_iwmmxt_tmrc (target, op0));
19570       return target;
19571
19572     case ARM_BUILTIN_WSHUFH:
19573       icode = CODE_FOR_iwmmxt_wshufh;
19574       arg0 = CALL_EXPR_ARG (exp, 0);
19575       arg1 = CALL_EXPR_ARG (exp, 1);
19576       op0 = expand_normal (arg0);
19577       op1 = expand_normal (arg1);
19578       tmode = insn_data[icode].operand[0].mode;
19579       mode1 = insn_data[icode].operand[1].mode;
19580       mode2 = insn_data[icode].operand[2].mode;
19581
19582       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
19583         op0 = copy_to_mode_reg (mode1, op0);
19584       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
19585         {
19586           /* @@@ better error message */
19587           error ("mask must be an immediate");
19588           return const0_rtx;
19589         }
19590       if (target == 0
19591           || GET_MODE (target) != tmode
19592           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19593         target = gen_reg_rtx (tmode);
19594       pat = GEN_FCN (icode) (target, op0, op1);
19595       if (! pat)
19596         return 0;
19597       emit_insn (pat);
19598       return target;
19599
19600     case ARM_BUILTIN_WSADB:
19601       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, exp, target);
19602     case ARM_BUILTIN_WSADH:
19603       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, exp, target);
19604     case ARM_BUILTIN_WSADBZ:
19605       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, exp, target);
19606     case ARM_BUILTIN_WSADHZ:
19607       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, exp, target);
19608
19609       /* Several three-argument builtins.  */
19610     case ARM_BUILTIN_WMACS:
19611     case ARM_BUILTIN_WMACU:
19612     case ARM_BUILTIN_WALIGN:
19613     case ARM_BUILTIN_TMIA:
19614     case ARM_BUILTIN_TMIAPH:
19615     case ARM_BUILTIN_TMIATT:
19616     case ARM_BUILTIN_TMIATB:
19617     case ARM_BUILTIN_TMIABT:
19618     case ARM_BUILTIN_TMIABB:
19619       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
19620                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
19621                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
19622                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
19623                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
19624                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
19625                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
19626                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
19627                : CODE_FOR_iwmmxt_walign);
19628       arg0 = CALL_EXPR_ARG (exp, 0);
19629       arg1 = CALL_EXPR_ARG (exp, 1);
19630       arg2 = CALL_EXPR_ARG (exp, 2);
19631       op0 = expand_normal (arg0);
19632       op1 = expand_normal (arg1);
19633       op2 = expand_normal (arg2);
19634       tmode = insn_data[icode].operand[0].mode;
19635       mode0 = insn_data[icode].operand[1].mode;
19636       mode1 = insn_data[icode].operand[2].mode;
19637       mode2 = insn_data[icode].operand[3].mode;
19638
19639       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19640         op0 = copy_to_mode_reg (mode0, op0);
19641       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
19642         op1 = copy_to_mode_reg (mode1, op1);
19643       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
19644         op2 = copy_to_mode_reg (mode2, op2);
19645       if (target == 0
19646           || GET_MODE (target) != tmode
19647           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19648         target = gen_reg_rtx (tmode);
19649       pat = GEN_FCN (icode) (target, op0, op1, op2);
19650       if (! pat)
19651         return 0;
19652       emit_insn (pat);
19653       return target;
19654
19655     case ARM_BUILTIN_WZERO:
19656       target = gen_reg_rtx (DImode);
19657       emit_insn (gen_iwmmxt_clrdi (target));
19658       return target;
19659
19660     case ARM_BUILTIN_THREAD_POINTER:
19661       return arm_load_tp (target);
19662
19663     default:
19664       break;
19665     }
19666
19667   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
19668     if (d->code == (const enum arm_builtins) fcode)
19669       return arm_expand_binop_builtin (d->icode, exp, target);
19670
19671   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
19672     if (d->code == (const enum arm_builtins) fcode)
19673       return arm_expand_unop_builtin (d->icode, exp, target, 0);
19674
19675   /* @@@ Should really do something sensible here.  */
19676   return NULL_RTX;
19677 }
19678 \f
19679 /* Return the number (counting from 0) of
19680    the least significant set bit in MASK.  */
19681
19682 inline static int
19683 number_of_first_bit_set (unsigned mask)
19684 {
19685   int bit;
19686
19687   for (bit = 0;
19688        (mask & (1 << bit)) == 0;
19689        ++bit)
19690     continue;
19691
19692   return bit;
19693 }
19694
19695 /* Emit code to push or pop registers to or from the stack.  F is the
19696    assembly file.  MASK is the registers to push or pop.  PUSH is
19697    nonzero if we should push, and zero if we should pop.  For debugging
19698    output, if pushing, adjust CFA_OFFSET by the amount of space added
19699    to the stack.  REAL_REGS should have the same number of bits set as
19700    MASK, and will be used instead (in the same order) to describe which
19701    registers were saved - this is used to mark the save slots when we
19702    push high registers after moving them to low registers.  */
19703 static void
19704 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
19705                unsigned long real_regs)
19706 {
19707   int regno;
19708   int lo_mask = mask & 0xFF;
19709   int pushed_words = 0;
19710
19711   gcc_assert (mask);
19712
19713   if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
19714     {
19715       /* Special case.  Do not generate a POP PC statement here, do it in
19716          thumb_exit() */
19717       thumb_exit (f, -1);
19718       return;
19719     }
19720
19721   if (push && arm_except_unwind_info (&global_options) == UI_TARGET)
19722     {
19723       fprintf (f, "\t.save\t{");
19724       for (regno = 0; regno < 15; regno++)
19725         {
19726           if (real_regs & (1 << regno))
19727             {
19728               if (real_regs & ((1 << regno) -1))
19729                 fprintf (f, ", ");
19730               asm_fprintf (f, "%r", regno);
19731             }
19732         }
19733       fprintf (f, "}\n");
19734     }
19735
19736   fprintf (f, "\t%s\t{", push ? "push" : "pop");
19737
19738   /* Look at the low registers first.  */
19739   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
19740     {
19741       if (lo_mask & 1)
19742         {
19743           asm_fprintf (f, "%r", regno);
19744
19745           if ((lo_mask & ~1) != 0)
19746             fprintf (f, ", ");
19747
19748           pushed_words++;
19749         }
19750     }
19751
19752   if (push && (mask & (1 << LR_REGNUM)))
19753     {
19754       /* Catch pushing the LR.  */
19755       if (mask & 0xFF)
19756         fprintf (f, ", ");
19757
19758       asm_fprintf (f, "%r", LR_REGNUM);
19759
19760       pushed_words++;
19761     }
19762   else if (!push && (mask & (1 << PC_REGNUM)))
19763     {
19764       /* Catch popping the PC.  */
19765       if (TARGET_INTERWORK || TARGET_BACKTRACE
19766           || crtl->calls_eh_return)
19767         {
19768           /* The PC is never poped directly, instead
19769              it is popped into r3 and then BX is used.  */
19770           fprintf (f, "}\n");
19771
19772           thumb_exit (f, -1);
19773
19774           return;
19775         }
19776       else
19777         {
19778           if (mask & 0xFF)
19779             fprintf (f, ", ");
19780
19781           asm_fprintf (f, "%r", PC_REGNUM);
19782         }
19783     }
19784
19785   fprintf (f, "}\n");
19786
19787   if (push && pushed_words && dwarf2out_do_frame ())
19788     {
19789       char *l = dwarf2out_cfi_label (false);
19790       int pushed_mask = real_regs;
19791
19792       *cfa_offset += pushed_words * 4;
19793       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
19794
19795       pushed_words = 0;
19796       pushed_mask = real_regs;
19797       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
19798         {
19799           if (pushed_mask & 1)
19800             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
19801         }
19802     }
19803 }
19804
19805 /* Generate code to return from a thumb function.
19806    If 'reg_containing_return_addr' is -1, then the return address is
19807    actually on the stack, at the stack pointer.  */
19808 static void
19809 thumb_exit (FILE *f, int reg_containing_return_addr)
19810 {
19811   unsigned regs_available_for_popping;
19812   unsigned regs_to_pop;
19813   int pops_needed;
19814   unsigned available;
19815   unsigned required;
19816   int mode;
19817   int size;
19818   int restore_a4 = FALSE;
19819
19820   /* Compute the registers we need to pop.  */
19821   regs_to_pop = 0;
19822   pops_needed = 0;
19823
19824   if (reg_containing_return_addr == -1)
19825     {
19826       regs_to_pop |= 1 << LR_REGNUM;
19827       ++pops_needed;
19828     }
19829
19830   if (TARGET_BACKTRACE)
19831     {
19832       /* Restore the (ARM) frame pointer and stack pointer.  */
19833       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
19834       pops_needed += 2;
19835     }
19836
19837   /* If there is nothing to pop then just emit the BX instruction and
19838      return.  */
19839   if (pops_needed == 0)
19840     {
19841       if (crtl->calls_eh_return)
19842         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
19843
19844       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
19845       return;
19846     }
19847   /* Otherwise if we are not supporting interworking and we have not created
19848      a backtrace structure and the function was not entered in ARM mode then
19849      just pop the return address straight into the PC.  */
19850   else if (!TARGET_INTERWORK
19851            && !TARGET_BACKTRACE
19852            && !is_called_in_ARM_mode (current_function_decl)
19853            && !crtl->calls_eh_return)
19854     {
19855       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
19856       return;
19857     }
19858
19859   /* Find out how many of the (return) argument registers we can corrupt.  */
19860   regs_available_for_popping = 0;
19861
19862   /* If returning via __builtin_eh_return, the bottom three registers
19863      all contain information needed for the return.  */
19864   if (crtl->calls_eh_return)
19865     size = 12;
19866   else
19867     {
19868       /* If we can deduce the registers used from the function's
19869          return value.  This is more reliable that examining
19870          df_regs_ever_live_p () because that will be set if the register is
19871          ever used in the function, not just if the register is used
19872          to hold a return value.  */
19873
19874       if (crtl->return_rtx != 0)
19875         mode = GET_MODE (crtl->return_rtx);
19876       else
19877         mode = DECL_MODE (DECL_RESULT (current_function_decl));
19878
19879       size = GET_MODE_SIZE (mode);
19880
19881       if (size == 0)
19882         {
19883           /* In a void function we can use any argument register.
19884              In a function that returns a structure on the stack
19885              we can use the second and third argument registers.  */
19886           if (mode == VOIDmode)
19887             regs_available_for_popping =
19888               (1 << ARG_REGISTER (1))
19889               | (1 << ARG_REGISTER (2))
19890               | (1 << ARG_REGISTER (3));
19891           else
19892             regs_available_for_popping =
19893               (1 << ARG_REGISTER (2))
19894               | (1 << ARG_REGISTER (3));
19895         }
19896       else if (size <= 4)
19897         regs_available_for_popping =
19898           (1 << ARG_REGISTER (2))
19899           | (1 << ARG_REGISTER (3));
19900       else if (size <= 8)
19901         regs_available_for_popping =
19902           (1 << ARG_REGISTER (3));
19903     }
19904
19905   /* Match registers to be popped with registers into which we pop them.  */
19906   for (available = regs_available_for_popping,
19907        required  = regs_to_pop;
19908        required != 0 && available != 0;
19909        available &= ~(available & - available),
19910        required  &= ~(required  & - required))
19911     -- pops_needed;
19912
19913   /* If we have any popping registers left over, remove them.  */
19914   if (available > 0)
19915     regs_available_for_popping &= ~available;
19916
19917   /* Otherwise if we need another popping register we can use
19918      the fourth argument register.  */
19919   else if (pops_needed)
19920     {
19921       /* If we have not found any free argument registers and
19922          reg a4 contains the return address, we must move it.  */
19923       if (regs_available_for_popping == 0
19924           && reg_containing_return_addr == LAST_ARG_REGNUM)
19925         {
19926           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
19927           reg_containing_return_addr = LR_REGNUM;
19928         }
19929       else if (size > 12)
19930         {
19931           /* Register a4 is being used to hold part of the return value,
19932              but we have dire need of a free, low register.  */
19933           restore_a4 = TRUE;
19934
19935           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
19936         }
19937
19938       if (reg_containing_return_addr != LAST_ARG_REGNUM)
19939         {
19940           /* The fourth argument register is available.  */
19941           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
19942
19943           --pops_needed;
19944         }
19945     }
19946
19947   /* Pop as many registers as we can.  */
19948   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
19949                  regs_available_for_popping);
19950
19951   /* Process the registers we popped.  */
19952   if (reg_containing_return_addr == -1)
19953     {
19954       /* The return address was popped into the lowest numbered register.  */
19955       regs_to_pop &= ~(1 << LR_REGNUM);
19956
19957       reg_containing_return_addr =
19958         number_of_first_bit_set (regs_available_for_popping);
19959
19960       /* Remove this register for the mask of available registers, so that
19961          the return address will not be corrupted by further pops.  */
19962       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
19963     }
19964
19965   /* If we popped other registers then handle them here.  */
19966   if (regs_available_for_popping)
19967     {
19968       int frame_pointer;
19969
19970       /* Work out which register currently contains the frame pointer.  */
19971       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
19972
19973       /* Move it into the correct place.  */
19974       asm_fprintf (f, "\tmov\t%r, %r\n",
19975                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
19976
19977       /* (Temporarily) remove it from the mask of popped registers.  */
19978       regs_available_for_popping &= ~(1 << frame_pointer);
19979       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
19980
19981       if (regs_available_for_popping)
19982         {
19983           int stack_pointer;
19984
19985           /* We popped the stack pointer as well,
19986              find the register that contains it.  */
19987           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
19988
19989           /* Move it into the stack register.  */
19990           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
19991
19992           /* At this point we have popped all necessary registers, so
19993              do not worry about restoring regs_available_for_popping
19994              to its correct value:
19995
19996              assert (pops_needed == 0)
19997              assert (regs_available_for_popping == (1 << frame_pointer))
19998              assert (regs_to_pop == (1 << STACK_POINTER))  */
19999         }
20000       else
20001         {
20002           /* Since we have just move the popped value into the frame
20003              pointer, the popping register is available for reuse, and
20004              we know that we still have the stack pointer left to pop.  */
20005           regs_available_for_popping |= (1 << frame_pointer);
20006         }
20007     }
20008
20009   /* If we still have registers left on the stack, but we no longer have
20010      any registers into which we can pop them, then we must move the return
20011      address into the link register and make available the register that
20012      contained it.  */
20013   if (regs_available_for_popping == 0 && pops_needed > 0)
20014     {
20015       regs_available_for_popping |= 1 << reg_containing_return_addr;
20016
20017       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
20018                    reg_containing_return_addr);
20019
20020       reg_containing_return_addr = LR_REGNUM;
20021     }
20022
20023   /* If we have registers left on the stack then pop some more.
20024      We know that at most we will want to pop FP and SP.  */
20025   if (pops_needed > 0)
20026     {
20027       int  popped_into;
20028       int  move_to;
20029
20030       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
20031                      regs_available_for_popping);
20032
20033       /* We have popped either FP or SP.
20034          Move whichever one it is into the correct register.  */
20035       popped_into = number_of_first_bit_set (regs_available_for_popping);
20036       move_to     = number_of_first_bit_set (regs_to_pop);
20037
20038       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
20039
20040       regs_to_pop &= ~(1 << move_to);
20041
20042       --pops_needed;
20043     }
20044
20045   /* If we still have not popped everything then we must have only
20046      had one register available to us and we are now popping the SP.  */
20047   if (pops_needed > 0)
20048     {
20049       int  popped_into;
20050
20051       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
20052                      regs_available_for_popping);
20053
20054       popped_into = number_of_first_bit_set (regs_available_for_popping);
20055
20056       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
20057       /*
20058         assert (regs_to_pop == (1 << STACK_POINTER))
20059         assert (pops_needed == 1)
20060       */
20061     }
20062
20063   /* If necessary restore the a4 register.  */
20064   if (restore_a4)
20065     {
20066       if (reg_containing_return_addr != LR_REGNUM)
20067         {
20068           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
20069           reg_containing_return_addr = LR_REGNUM;
20070         }
20071
20072       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
20073     }
20074
20075   if (crtl->calls_eh_return)
20076     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
20077
20078   /* Return to caller.  */
20079   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
20080 }
20081 \f
20082 /* Scan INSN just before assembler is output for it.
20083    For Thumb-1, we track the status of the condition codes; this
20084    information is used in the cbranchsi4_insn pattern.  */
20085 void
20086 thumb1_final_prescan_insn (rtx insn)
20087 {
20088   if (flag_print_asm_name)
20089     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
20090                  INSN_ADDRESSES (INSN_UID (insn)));
20091   /* Don't overwrite the previous setter when we get to a cbranch.  */
20092   if (INSN_CODE (insn) != CODE_FOR_cbranchsi4_insn)
20093     {
20094       enum attr_conds conds;
20095
20096       if (cfun->machine->thumb1_cc_insn)
20097         {
20098           if (modified_in_p (cfun->machine->thumb1_cc_op0, insn)
20099               || modified_in_p (cfun->machine->thumb1_cc_op1, insn))
20100             CC_STATUS_INIT;
20101         }
20102       conds = get_attr_conds (insn);
20103       if (conds == CONDS_SET)
20104         {
20105           rtx set = single_set (insn);
20106           cfun->machine->thumb1_cc_insn = insn;
20107           cfun->machine->thumb1_cc_op0 = SET_DEST (set);
20108           cfun->machine->thumb1_cc_op1 = const0_rtx;
20109           cfun->machine->thumb1_cc_mode = CC_NOOVmode;
20110           if (INSN_CODE (insn) == CODE_FOR_thumb1_subsi3_insn)
20111             {
20112               rtx src1 = XEXP (SET_SRC (set), 1);
20113               if (src1 == const0_rtx)
20114                 cfun->machine->thumb1_cc_mode = CCmode;
20115             }
20116         }
20117       else if (conds != CONDS_NOCOND)
20118         cfun->machine->thumb1_cc_insn = NULL_RTX;
20119     }
20120 }
20121
20122 int
20123 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
20124 {
20125   unsigned HOST_WIDE_INT mask = 0xff;
20126   int i;
20127
20128   val = val & (unsigned HOST_WIDE_INT)0xffffffffu;
20129   if (val == 0) /* XXX */
20130     return 0;
20131
20132   for (i = 0; i < 25; i++)
20133     if ((val & (mask << i)) == val)
20134       return 1;
20135
20136   return 0;
20137 }
20138
20139 /* Returns nonzero if the current function contains,
20140    or might contain a far jump.  */
20141 static int
20142 thumb_far_jump_used_p (void)
20143 {
20144   rtx insn;
20145
20146   /* This test is only important for leaf functions.  */
20147   /* assert (!leaf_function_p ()); */
20148
20149   /* If we have already decided that far jumps may be used,
20150      do not bother checking again, and always return true even if
20151      it turns out that they are not being used.  Once we have made
20152      the decision that far jumps are present (and that hence the link
20153      register will be pushed onto the stack) we cannot go back on it.  */
20154   if (cfun->machine->far_jump_used)
20155     return 1;
20156
20157   /* If this function is not being called from the prologue/epilogue
20158      generation code then it must be being called from the
20159      INITIAL_ELIMINATION_OFFSET macro.  */
20160   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
20161     {
20162       /* In this case we know that we are being asked about the elimination
20163          of the arg pointer register.  If that register is not being used,
20164          then there are no arguments on the stack, and we do not have to
20165          worry that a far jump might force the prologue to push the link
20166          register, changing the stack offsets.  In this case we can just
20167          return false, since the presence of far jumps in the function will
20168          not affect stack offsets.
20169
20170          If the arg pointer is live (or if it was live, but has now been
20171          eliminated and so set to dead) then we do have to test to see if
20172          the function might contain a far jump.  This test can lead to some
20173          false negatives, since before reload is completed, then length of
20174          branch instructions is not known, so gcc defaults to returning their
20175          longest length, which in turn sets the far jump attribute to true.
20176
20177          A false negative will not result in bad code being generated, but it
20178          will result in a needless push and pop of the link register.  We
20179          hope that this does not occur too often.
20180
20181          If we need doubleword stack alignment this could affect the other
20182          elimination offsets so we can't risk getting it wrong.  */
20183       if (df_regs_ever_live_p (ARG_POINTER_REGNUM))
20184         cfun->machine->arg_pointer_live = 1;
20185       else if (!cfun->machine->arg_pointer_live)
20186         return 0;
20187     }
20188
20189   /* Check to see if the function contains a branch
20190      insn with the far jump attribute set.  */
20191   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
20192     {
20193       if (GET_CODE (insn) == JUMP_INSN
20194           /* Ignore tablejump patterns.  */
20195           && GET_CODE (PATTERN (insn)) != ADDR_VEC
20196           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
20197           && get_attr_far_jump (insn) == FAR_JUMP_YES
20198           )
20199         {
20200           /* Record the fact that we have decided that
20201              the function does use far jumps.  */
20202           cfun->machine->far_jump_used = 1;
20203           return 1;
20204         }
20205     }
20206
20207   return 0;
20208 }
20209
20210 /* Return nonzero if FUNC must be entered in ARM mode.  */
20211 int
20212 is_called_in_ARM_mode (tree func)
20213 {
20214   gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
20215
20216   /* Ignore the problem about functions whose address is taken.  */
20217   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
20218     return TRUE;
20219
20220 #ifdef ARM_PE
20221   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
20222 #else
20223   return FALSE;
20224 #endif
20225 }
20226
20227 /* Given the stack offsets and register mask in OFFSETS, decide how
20228    many additional registers to push instead of subtracting a constant
20229    from SP.  For epilogues the principle is the same except we use pop.
20230    FOR_PROLOGUE indicates which we're generating.  */
20231 static int
20232 thumb1_extra_regs_pushed (arm_stack_offsets *offsets, bool for_prologue)
20233 {
20234   HOST_WIDE_INT amount;
20235   unsigned long live_regs_mask = offsets->saved_regs_mask;
20236   /* Extract a mask of the ones we can give to the Thumb's push/pop
20237      instruction.  */
20238   unsigned long l_mask = live_regs_mask & (for_prologue ? 0x40ff : 0xff);
20239   /* Then count how many other high registers will need to be pushed.  */
20240   unsigned long high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
20241   int n_free, reg_base;
20242
20243   if (!for_prologue && frame_pointer_needed)
20244     amount = offsets->locals_base - offsets->saved_regs;
20245   else
20246     amount = offsets->outgoing_args - offsets->saved_regs;
20247
20248   /* If the stack frame size is 512 exactly, we can save one load
20249      instruction, which should make this a win even when optimizing
20250      for speed.  */
20251   if (!optimize_size && amount != 512)
20252     return 0;
20253
20254   /* Can't do this if there are high registers to push.  */
20255   if (high_regs_pushed != 0)
20256     return 0;
20257
20258   /* Shouldn't do it in the prologue if no registers would normally
20259      be pushed at all.  In the epilogue, also allow it if we'll have
20260      a pop insn for the PC.  */
20261   if  (l_mask == 0
20262        && (for_prologue
20263            || TARGET_BACKTRACE
20264            || (live_regs_mask & 1 << LR_REGNUM) == 0
20265            || TARGET_INTERWORK
20266            || crtl->args.pretend_args_size != 0))
20267     return 0;
20268
20269   /* Don't do this if thumb_expand_prologue wants to emit instructions
20270      between the push and the stack frame allocation.  */
20271   if (for_prologue
20272       && ((flag_pic && arm_pic_register != INVALID_REGNUM)
20273           || (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)))
20274     return 0;
20275
20276   reg_base = 0;
20277   n_free = 0;
20278   if (!for_prologue)
20279     {
20280       reg_base = arm_size_return_regs () / UNITS_PER_WORD;
20281       live_regs_mask >>= reg_base;
20282     }
20283
20284   while (reg_base + n_free < 8 && !(live_regs_mask & 1)
20285          && (for_prologue || call_used_regs[reg_base + n_free]))
20286     {
20287       live_regs_mask >>= 1;
20288       n_free++;
20289     }
20290
20291   if (n_free == 0)
20292     return 0;
20293   gcc_assert (amount / 4 * 4 == amount);
20294
20295   if (amount >= 512 && (amount - n_free * 4) < 512)
20296     return (amount - 508) / 4;
20297   if (amount <= n_free * 4)
20298     return amount / 4;
20299   return 0;
20300 }
20301
20302 /* The bits which aren't usefully expanded as rtl.  */
20303 const char *
20304 thumb_unexpanded_epilogue (void)
20305 {
20306   arm_stack_offsets *offsets;
20307   int regno;
20308   unsigned long live_regs_mask = 0;
20309   int high_regs_pushed = 0;
20310   int extra_pop;
20311   int had_to_push_lr;
20312   int size;
20313
20314   if (cfun->machine->return_used_this_function != 0)
20315     return "";
20316
20317   if (IS_NAKED (arm_current_func_type ()))
20318     return "";
20319
20320   offsets = arm_get_frame_offsets ();
20321   live_regs_mask = offsets->saved_regs_mask;
20322   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
20323
20324   /* If we can deduce the registers used from the function's return value.
20325      This is more reliable that examining df_regs_ever_live_p () because that
20326      will be set if the register is ever used in the function, not just if
20327      the register is used to hold a return value.  */
20328   size = arm_size_return_regs ();
20329
20330   extra_pop = thumb1_extra_regs_pushed (offsets, false);
20331   if (extra_pop > 0)
20332     {
20333       unsigned long extra_mask = (1 << extra_pop) - 1;
20334       live_regs_mask |= extra_mask << (size / UNITS_PER_WORD);
20335     }
20336
20337   /* The prolog may have pushed some high registers to use as
20338      work registers.  e.g. the testsuite file:
20339      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
20340      compiles to produce:
20341         push    {r4, r5, r6, r7, lr}
20342         mov     r7, r9
20343         mov     r6, r8
20344         push    {r6, r7}
20345      as part of the prolog.  We have to undo that pushing here.  */
20346
20347   if (high_regs_pushed)
20348     {
20349       unsigned long mask = live_regs_mask & 0xff;
20350       int next_hi_reg;
20351
20352       /* The available low registers depend on the size of the value we are
20353          returning.  */
20354       if (size <= 12)
20355         mask |=  1 << 3;
20356       if (size <= 8)
20357         mask |= 1 << 2;
20358
20359       if (mask == 0)
20360         /* Oh dear!  We have no low registers into which we can pop
20361            high registers!  */
20362         internal_error
20363           ("no low registers available for popping high registers");
20364
20365       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
20366         if (live_regs_mask & (1 << next_hi_reg))
20367           break;
20368
20369       while (high_regs_pushed)
20370         {
20371           /* Find lo register(s) into which the high register(s) can
20372              be popped.  */
20373           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
20374             {
20375               if (mask & (1 << regno))
20376                 high_regs_pushed--;
20377               if (high_regs_pushed == 0)
20378                 break;
20379             }
20380
20381           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
20382
20383           /* Pop the values into the low register(s).  */
20384           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
20385
20386           /* Move the value(s) into the high registers.  */
20387           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
20388             {
20389               if (mask & (1 << regno))
20390                 {
20391                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
20392                                regno);
20393
20394                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
20395                     if (live_regs_mask & (1 << next_hi_reg))
20396                       break;
20397                 }
20398             }
20399         }
20400       live_regs_mask &= ~0x0f00;
20401     }
20402
20403   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
20404   live_regs_mask &= 0xff;
20405
20406   if (crtl->args.pretend_args_size == 0 || TARGET_BACKTRACE)
20407     {
20408       /* Pop the return address into the PC.  */
20409       if (had_to_push_lr)
20410         live_regs_mask |= 1 << PC_REGNUM;
20411
20412       /* Either no argument registers were pushed or a backtrace
20413          structure was created which includes an adjusted stack
20414          pointer, so just pop everything.  */
20415       if (live_regs_mask)
20416         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
20417                        live_regs_mask);
20418
20419       /* We have either just popped the return address into the
20420          PC or it is was kept in LR for the entire function.
20421          Note that thumb_pushpop has already called thumb_exit if the
20422          PC was in the list.  */
20423       if (!had_to_push_lr)
20424         thumb_exit (asm_out_file, LR_REGNUM);
20425     }
20426   else
20427     {
20428       /* Pop everything but the return address.  */
20429       if (live_regs_mask)
20430         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
20431                        live_regs_mask);
20432
20433       if (had_to_push_lr)
20434         {
20435           if (size > 12)
20436             {
20437               /* We have no free low regs, so save one.  */
20438               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
20439                            LAST_ARG_REGNUM);
20440             }
20441
20442           /* Get the return address into a temporary register.  */
20443           thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
20444                          1 << LAST_ARG_REGNUM);
20445
20446           if (size > 12)
20447             {
20448               /* Move the return address to lr.  */
20449               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
20450                            LAST_ARG_REGNUM);
20451               /* Restore the low register.  */
20452               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
20453                            IP_REGNUM);
20454               regno = LR_REGNUM;
20455             }
20456           else
20457             regno = LAST_ARG_REGNUM;
20458         }
20459       else
20460         regno = LR_REGNUM;
20461
20462       /* Remove the argument registers that were pushed onto the stack.  */
20463       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
20464                    SP_REGNUM, SP_REGNUM,
20465                    crtl->args.pretend_args_size);
20466
20467       thumb_exit (asm_out_file, regno);
20468     }
20469
20470   return "";
20471 }
20472
20473 /* Functions to save and restore machine-specific function data.  */
20474 static struct machine_function *
20475 arm_init_machine_status (void)
20476 {
20477   struct machine_function *machine;
20478   machine = ggc_alloc_cleared_machine_function ();
20479
20480 #if ARM_FT_UNKNOWN != 0
20481   machine->func_type = ARM_FT_UNKNOWN;
20482 #endif
20483   return machine;
20484 }
20485
20486 /* Return an RTX indicating where the return address to the
20487    calling function can be found.  */
20488 rtx
20489 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
20490 {
20491   if (count != 0)
20492     return NULL_RTX;
20493
20494   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
20495 }
20496
20497 /* Do anything needed before RTL is emitted for each function.  */
20498 void
20499 arm_init_expanders (void)
20500 {
20501   /* Arrange to initialize and mark the machine per-function status.  */
20502   init_machine_status = arm_init_machine_status;
20503
20504   /* This is to stop the combine pass optimizing away the alignment
20505      adjustment of va_arg.  */
20506   /* ??? It is claimed that this should not be necessary.  */
20507   if (cfun)
20508     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
20509 }
20510
20511
20512 /* Like arm_compute_initial_elimination offset.  Simpler because there
20513    isn't an ABI specified frame pointer for Thumb.  Instead, we set it
20514    to point at the base of the local variables after static stack
20515    space for a function has been allocated.  */
20516
20517 HOST_WIDE_INT
20518 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
20519 {
20520   arm_stack_offsets *offsets;
20521
20522   offsets = arm_get_frame_offsets ();
20523
20524   switch (from)
20525     {
20526     case ARG_POINTER_REGNUM:
20527       switch (to)
20528         {
20529         case STACK_POINTER_REGNUM:
20530           return offsets->outgoing_args - offsets->saved_args;
20531
20532         case FRAME_POINTER_REGNUM:
20533           return offsets->soft_frame - offsets->saved_args;
20534
20535         case ARM_HARD_FRAME_POINTER_REGNUM:
20536           return offsets->saved_regs - offsets->saved_args;
20537
20538         case THUMB_HARD_FRAME_POINTER_REGNUM:
20539           return offsets->locals_base - offsets->saved_args;
20540
20541         default:
20542           gcc_unreachable ();
20543         }
20544       break;
20545
20546     case FRAME_POINTER_REGNUM:
20547       switch (to)
20548         {
20549         case STACK_POINTER_REGNUM:
20550           return offsets->outgoing_args - offsets->soft_frame;
20551
20552         case ARM_HARD_FRAME_POINTER_REGNUM:
20553           return offsets->saved_regs - offsets->soft_frame;
20554
20555         case THUMB_HARD_FRAME_POINTER_REGNUM:
20556           return offsets->locals_base - offsets->soft_frame;
20557
20558         default:
20559           gcc_unreachable ();
20560         }
20561       break;
20562
20563     default:
20564       gcc_unreachable ();
20565     }
20566 }
20567
20568 /* Generate the rest of a function's prologue.  */
20569 void
20570 thumb1_expand_prologue (void)
20571 {
20572   rtx insn, dwarf;
20573
20574   HOST_WIDE_INT amount;
20575   arm_stack_offsets *offsets;
20576   unsigned long func_type;
20577   int regno;
20578   unsigned long live_regs_mask;
20579
20580   func_type = arm_current_func_type ();
20581
20582   /* Naked functions don't have prologues.  */
20583   if (IS_NAKED (func_type))
20584     return;
20585
20586   if (IS_INTERRUPT (func_type))
20587     {
20588       error ("interrupt Service Routines cannot be coded in Thumb mode");
20589       return;
20590     }
20591
20592   offsets = arm_get_frame_offsets ();
20593   live_regs_mask = offsets->saved_regs_mask;
20594   /* Load the pic register before setting the frame pointer,
20595      so we can use r7 as a temporary work register.  */
20596   if (flag_pic && arm_pic_register != INVALID_REGNUM)
20597     arm_load_pic_register (live_regs_mask);
20598
20599   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
20600     emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
20601                     stack_pointer_rtx);
20602
20603   amount = offsets->outgoing_args - offsets->saved_regs;
20604   amount -= 4 * thumb1_extra_regs_pushed (offsets, true);
20605   if (amount)
20606     {
20607       if (amount < 512)
20608         {
20609           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
20610                                         GEN_INT (- amount)));
20611           RTX_FRAME_RELATED_P (insn) = 1;
20612         }
20613       else
20614         {
20615           rtx reg;
20616
20617           /* The stack decrement is too big for an immediate value in a single
20618              insn.  In theory we could issue multiple subtracts, but after
20619              three of them it becomes more space efficient to place the full
20620              value in the constant pool and load into a register.  (Also the
20621              ARM debugger really likes to see only one stack decrement per
20622              function).  So instead we look for a scratch register into which
20623              we can load the decrement, and then we subtract this from the
20624              stack pointer.  Unfortunately on the thumb the only available
20625              scratch registers are the argument registers, and we cannot use
20626              these as they may hold arguments to the function.  Instead we
20627              attempt to locate a call preserved register which is used by this
20628              function.  If we can find one, then we know that it will have
20629              been pushed at the start of the prologue and so we can corrupt
20630              it now.  */
20631           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
20632             if (live_regs_mask & (1 << regno))
20633               break;
20634
20635           gcc_assert(regno <= LAST_LO_REGNUM);
20636
20637           reg = gen_rtx_REG (SImode, regno);
20638
20639           emit_insn (gen_movsi (reg, GEN_INT (- amount)));
20640
20641           insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
20642                                         stack_pointer_rtx, reg));
20643           RTX_FRAME_RELATED_P (insn) = 1;
20644           dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
20645                                plus_constant (stack_pointer_rtx,
20646                                               -amount));
20647           RTX_FRAME_RELATED_P (dwarf) = 1;
20648           add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
20649         }
20650     }
20651
20652   if (frame_pointer_needed)
20653     thumb_set_frame_pointer (offsets);
20654
20655   /* If we are profiling, make sure no instructions are scheduled before
20656      the call to mcount.  Similarly if the user has requested no
20657      scheduling in the prolog.  Similarly if we want non-call exceptions
20658      using the EABI unwinder, to prevent faulting instructions from being
20659      swapped with a stack adjustment.  */
20660   if (crtl->profile || !TARGET_SCHED_PROLOG
20661       || (arm_except_unwind_info (&global_options) == UI_TARGET
20662           && cfun->can_throw_non_call_exceptions))
20663     emit_insn (gen_blockage ());
20664
20665   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
20666   if (live_regs_mask & 0xff)
20667     cfun->machine->lr_save_eliminated = 0;
20668 }
20669
20670
20671 void
20672 thumb1_expand_epilogue (void)
20673 {
20674   HOST_WIDE_INT amount;
20675   arm_stack_offsets *offsets;
20676   int regno;
20677
20678   /* Naked functions don't have prologues.  */
20679   if (IS_NAKED (arm_current_func_type ()))
20680     return;
20681
20682   offsets = arm_get_frame_offsets ();
20683   amount = offsets->outgoing_args - offsets->saved_regs;
20684
20685   if (frame_pointer_needed)
20686     {
20687       emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
20688       amount = offsets->locals_base - offsets->saved_regs;
20689     }
20690   amount -= 4 * thumb1_extra_regs_pushed (offsets, false);
20691
20692   gcc_assert (amount >= 0);
20693   if (amount)
20694     {
20695       if (amount < 512)
20696         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
20697                                GEN_INT (amount)));
20698       else
20699         {
20700           /* r3 is always free in the epilogue.  */
20701           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
20702
20703           emit_insn (gen_movsi (reg, GEN_INT (amount)));
20704           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
20705         }
20706     }
20707
20708   /* Emit a USE (stack_pointer_rtx), so that
20709      the stack adjustment will not be deleted.  */
20710   emit_insn (gen_prologue_use (stack_pointer_rtx));
20711
20712   if (crtl->profile || !TARGET_SCHED_PROLOG)
20713     emit_insn (gen_blockage ());
20714
20715   /* Emit a clobber for each insn that will be restored in the epilogue,
20716      so that flow2 will get register lifetimes correct.  */
20717   for (regno = 0; regno < 13; regno++)
20718     if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
20719       emit_clobber (gen_rtx_REG (SImode, regno));
20720
20721   if (! df_regs_ever_live_p (LR_REGNUM))
20722     emit_use (gen_rtx_REG (SImode, LR_REGNUM));
20723 }
20724
20725 static void
20726 thumb1_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
20727 {
20728   arm_stack_offsets *offsets;
20729   unsigned long live_regs_mask = 0;
20730   unsigned long l_mask;
20731   unsigned high_regs_pushed = 0;
20732   int cfa_offset = 0;
20733   int regno;
20734
20735   if (IS_NAKED (arm_current_func_type ()))
20736     return;
20737
20738   if (is_called_in_ARM_mode (current_function_decl))
20739     {
20740       const char * name;
20741
20742       gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
20743       gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
20744                   == SYMBOL_REF);
20745       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
20746
20747       /* Generate code sequence to switch us into Thumb mode.  */
20748       /* The .code 32 directive has already been emitted by
20749          ASM_DECLARE_FUNCTION_NAME.  */
20750       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
20751       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
20752
20753       /* Generate a label, so that the debugger will notice the
20754          change in instruction sets.  This label is also used by
20755          the assembler to bypass the ARM code when this function
20756          is called from a Thumb encoded function elsewhere in the
20757          same file.  Hence the definition of STUB_NAME here must
20758          agree with the definition in gas/config/tc-arm.c.  */
20759
20760 #define STUB_NAME ".real_start_of"
20761
20762       fprintf (f, "\t.code\t16\n");
20763 #ifdef ARM_PE
20764       if (arm_dllexport_name_p (name))
20765         name = arm_strip_name_encoding (name);
20766 #endif
20767       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
20768       fprintf (f, "\t.thumb_func\n");
20769       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
20770     }
20771
20772   if (crtl->args.pretend_args_size)
20773     {
20774       /* Output unwind directive for the stack adjustment.  */
20775       if (arm_except_unwind_info (&global_options) == UI_TARGET)
20776         fprintf (f, "\t.pad #%d\n",
20777                  crtl->args.pretend_args_size);
20778
20779       if (cfun->machine->uses_anonymous_args)
20780         {
20781           int num_pushes;
20782
20783           fprintf (f, "\tpush\t{");
20784
20785           num_pushes = ARM_NUM_INTS (crtl->args.pretend_args_size);
20786
20787           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
20788                regno <= LAST_ARG_REGNUM;
20789                regno++)
20790             asm_fprintf (f, "%r%s", regno,
20791                          regno == LAST_ARG_REGNUM ? "" : ", ");
20792
20793           fprintf (f, "}\n");
20794         }
20795       else
20796         asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
20797                      SP_REGNUM, SP_REGNUM,
20798                      crtl->args.pretend_args_size);
20799
20800       /* We don't need to record the stores for unwinding (would it
20801          help the debugger any if we did?), but record the change in
20802          the stack pointer.  */
20803       if (dwarf2out_do_frame ())
20804         {
20805           char *l = dwarf2out_cfi_label (false);
20806
20807           cfa_offset = cfa_offset + crtl->args.pretend_args_size;
20808           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
20809         }
20810     }
20811
20812   /* Get the registers we are going to push.  */
20813   offsets = arm_get_frame_offsets ();
20814   live_regs_mask = offsets->saved_regs_mask;
20815   /* Extract a mask of the ones we can give to the Thumb's push instruction.  */
20816   l_mask = live_regs_mask & 0x40ff;
20817   /* Then count how many other high registers will need to be pushed.  */
20818   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
20819
20820   if (TARGET_BACKTRACE)
20821     {
20822       unsigned offset;
20823       unsigned work_register;
20824
20825       /* We have been asked to create a stack backtrace structure.
20826          The code looks like this:
20827
20828          0   .align 2
20829          0   func:
20830          0     sub   SP, #16         Reserve space for 4 registers.
20831          2     push  {R7}            Push low registers.
20832          4     add   R7, SP, #20     Get the stack pointer before the push.
20833          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
20834          8     mov   R7, PC          Get hold of the start of this code plus 12.
20835         10     str   R7, [SP, #16]   Store it.
20836         12     mov   R7, FP          Get hold of the current frame pointer.
20837         14     str   R7, [SP, #4]    Store it.
20838         16     mov   R7, LR          Get hold of the current return address.
20839         18     str   R7, [SP, #12]   Store it.
20840         20     add   R7, SP, #16     Point at the start of the backtrace structure.
20841         22     mov   FP, R7          Put this value into the frame pointer.  */
20842
20843       work_register = thumb_find_work_register (live_regs_mask);
20844
20845       if (arm_except_unwind_info (&global_options) == UI_TARGET)
20846         asm_fprintf (f, "\t.pad #16\n");
20847
20848       asm_fprintf
20849         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
20850          SP_REGNUM, SP_REGNUM);
20851
20852       if (dwarf2out_do_frame ())
20853         {
20854           char *l = dwarf2out_cfi_label (false);
20855
20856           cfa_offset = cfa_offset + 16;
20857           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
20858         }
20859
20860       if (l_mask)
20861         {
20862           thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
20863           offset = bit_count (l_mask) * UNITS_PER_WORD;
20864         }
20865       else
20866         offset = 0;
20867
20868       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
20869                    offset + 16 + crtl->args.pretend_args_size);
20870
20871       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20872                    offset + 4);
20873
20874       /* Make sure that the instruction fetching the PC is in the right place
20875          to calculate "start of backtrace creation code + 12".  */
20876       if (l_mask)
20877         {
20878           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
20879           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20880                        offset + 12);
20881           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
20882                        ARM_HARD_FRAME_POINTER_REGNUM);
20883           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20884                        offset);
20885         }
20886       else
20887         {
20888           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
20889                        ARM_HARD_FRAME_POINTER_REGNUM);
20890           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20891                        offset);
20892           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
20893           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20894                        offset + 12);
20895         }
20896
20897       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
20898       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20899                    offset + 8);
20900       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
20901                    offset + 12);
20902       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
20903                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
20904     }
20905   /* Optimization:  If we are not pushing any low registers but we are going
20906      to push some high registers then delay our first push.  This will just
20907      be a push of LR and we can combine it with the push of the first high
20908      register.  */
20909   else if ((l_mask & 0xff) != 0
20910            || (high_regs_pushed == 0 && l_mask))
20911     {
20912       unsigned long mask = l_mask;
20913       mask |= (1 << thumb1_extra_regs_pushed (offsets, true)) - 1;
20914       thumb_pushpop (f, mask, 1, &cfa_offset, mask);
20915     }
20916
20917   if (high_regs_pushed)
20918     {
20919       unsigned pushable_regs;
20920       unsigned next_hi_reg;
20921
20922       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
20923         if (live_regs_mask & (1 << next_hi_reg))
20924           break;
20925
20926       pushable_regs = l_mask & 0xff;
20927
20928       if (pushable_regs == 0)
20929         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
20930
20931       while (high_regs_pushed > 0)
20932         {
20933           unsigned long real_regs_mask = 0;
20934
20935           for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
20936             {
20937               if (pushable_regs & (1 << regno))
20938                 {
20939                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
20940
20941                   high_regs_pushed --;
20942                   real_regs_mask |= (1 << next_hi_reg);
20943
20944                   if (high_regs_pushed)
20945                     {
20946                       for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
20947                            next_hi_reg --)
20948                         if (live_regs_mask & (1 << next_hi_reg))
20949                           break;
20950                     }
20951                   else
20952                     {
20953                       pushable_regs &= ~((1 << regno) - 1);
20954                       break;
20955                     }
20956                 }
20957             }
20958
20959           /* If we had to find a work register and we have not yet
20960              saved the LR then add it to the list of regs to push.  */
20961           if (l_mask == (1 << LR_REGNUM))
20962             {
20963               thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
20964                              1, &cfa_offset,
20965                              real_regs_mask | (1 << LR_REGNUM));
20966               l_mask = 0;
20967             }
20968           else
20969             thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
20970         }
20971     }
20972 }
20973
20974 /* Handle the case of a double word load into a low register from
20975    a computed memory address.  The computed address may involve a
20976    register which is overwritten by the load.  */
20977 const char *
20978 thumb_load_double_from_address (rtx *operands)
20979 {
20980   rtx addr;
20981   rtx base;
20982   rtx offset;
20983   rtx arg1;
20984   rtx arg2;
20985
20986   gcc_assert (GET_CODE (operands[0]) == REG);
20987   gcc_assert (GET_CODE (operands[1]) == MEM);
20988
20989   /* Get the memory address.  */
20990   addr = XEXP (operands[1], 0);
20991
20992   /* Work out how the memory address is computed.  */
20993   switch (GET_CODE (addr))
20994     {
20995     case REG:
20996       operands[2] = adjust_address (operands[1], SImode, 4);
20997
20998       if (REGNO (operands[0]) == REGNO (addr))
20999         {
21000           output_asm_insn ("ldr\t%H0, %2", operands);
21001           output_asm_insn ("ldr\t%0, %1", operands);
21002         }
21003       else
21004         {
21005           output_asm_insn ("ldr\t%0, %1", operands);
21006           output_asm_insn ("ldr\t%H0, %2", operands);
21007         }
21008       break;
21009
21010     case CONST:
21011       /* Compute <address> + 4 for the high order load.  */
21012       operands[2] = adjust_address (operands[1], SImode, 4);
21013
21014       output_asm_insn ("ldr\t%0, %1", operands);
21015       output_asm_insn ("ldr\t%H0, %2", operands);
21016       break;
21017
21018     case PLUS:
21019       arg1   = XEXP (addr, 0);
21020       arg2   = XEXP (addr, 1);
21021
21022       if (CONSTANT_P (arg1))
21023         base = arg2, offset = arg1;
21024       else
21025         base = arg1, offset = arg2;
21026
21027       gcc_assert (GET_CODE (base) == REG);
21028
21029       /* Catch the case of <address> = <reg> + <reg> */
21030       if (GET_CODE (offset) == REG)
21031         {
21032           int reg_offset = REGNO (offset);
21033           int reg_base   = REGNO (base);
21034           int reg_dest   = REGNO (operands[0]);
21035
21036           /* Add the base and offset registers together into the
21037              higher destination register.  */
21038           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
21039                        reg_dest + 1, reg_base, reg_offset);
21040
21041           /* Load the lower destination register from the address in
21042              the higher destination register.  */
21043           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
21044                        reg_dest, reg_dest + 1);
21045
21046           /* Load the higher destination register from its own address
21047              plus 4.  */
21048           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
21049                        reg_dest + 1, reg_dest + 1);
21050         }
21051       else
21052         {
21053           /* Compute <address> + 4 for the high order load.  */
21054           operands[2] = adjust_address (operands[1], SImode, 4);
21055
21056           /* If the computed address is held in the low order register
21057              then load the high order register first, otherwise always
21058              load the low order register first.  */
21059           if (REGNO (operands[0]) == REGNO (base))
21060             {
21061               output_asm_insn ("ldr\t%H0, %2", operands);
21062               output_asm_insn ("ldr\t%0, %1", operands);
21063             }
21064           else
21065             {
21066               output_asm_insn ("ldr\t%0, %1", operands);
21067               output_asm_insn ("ldr\t%H0, %2", operands);
21068             }
21069         }
21070       break;
21071
21072     case LABEL_REF:
21073       /* With no registers to worry about we can just load the value
21074          directly.  */
21075       operands[2] = adjust_address (operands[1], SImode, 4);
21076
21077       output_asm_insn ("ldr\t%H0, %2", operands);
21078       output_asm_insn ("ldr\t%0, %1", operands);
21079       break;
21080
21081     default:
21082       gcc_unreachable ();
21083     }
21084
21085   return "";
21086 }
21087
21088 const char *
21089 thumb_output_move_mem_multiple (int n, rtx *operands)
21090 {
21091   rtx tmp;
21092
21093   switch (n)
21094     {
21095     case 2:
21096       if (REGNO (operands[4]) > REGNO (operands[5]))
21097         {
21098           tmp = operands[4];
21099           operands[4] = operands[5];
21100           operands[5] = tmp;
21101         }
21102       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
21103       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
21104       break;
21105
21106     case 3:
21107       if (REGNO (operands[4]) > REGNO (operands[5]))
21108         {
21109           tmp = operands[4];
21110           operands[4] = operands[5];
21111           operands[5] = tmp;
21112         }
21113       if (REGNO (operands[5]) > REGNO (operands[6]))
21114         {
21115           tmp = operands[5];
21116           operands[5] = operands[6];
21117           operands[6] = tmp;
21118         }
21119       if (REGNO (operands[4]) > REGNO (operands[5]))
21120         {
21121           tmp = operands[4];
21122           operands[4] = operands[5];
21123           operands[5] = tmp;
21124         }
21125
21126       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
21127       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
21128       break;
21129
21130     default:
21131       gcc_unreachable ();
21132     }
21133
21134   return "";
21135 }
21136
21137 /* Output a call-via instruction for thumb state.  */
21138 const char *
21139 thumb_call_via_reg (rtx reg)
21140 {
21141   int regno = REGNO (reg);
21142   rtx *labelp;
21143
21144   gcc_assert (regno < LR_REGNUM);
21145
21146   /* If we are in the normal text section we can use a single instance
21147      per compilation unit.  If we are doing function sections, then we need
21148      an entry per section, since we can't rely on reachability.  */
21149   if (in_section == text_section)
21150     {
21151       thumb_call_reg_needed = 1;
21152
21153       if (thumb_call_via_label[regno] == NULL)
21154         thumb_call_via_label[regno] = gen_label_rtx ();
21155       labelp = thumb_call_via_label + regno;
21156     }
21157   else
21158     {
21159       if (cfun->machine->call_via[regno] == NULL)
21160         cfun->machine->call_via[regno] = gen_label_rtx ();
21161       labelp = cfun->machine->call_via + regno;
21162     }
21163
21164   output_asm_insn ("bl\t%a0", labelp);
21165   return "";
21166 }
21167
21168 /* Routines for generating rtl.  */
21169 void
21170 thumb_expand_movmemqi (rtx *operands)
21171 {
21172   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
21173   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
21174   HOST_WIDE_INT len = INTVAL (operands[2]);
21175   HOST_WIDE_INT offset = 0;
21176
21177   while (len >= 12)
21178     {
21179       emit_insn (gen_movmem12b (out, in, out, in));
21180       len -= 12;
21181     }
21182
21183   if (len >= 8)
21184     {
21185       emit_insn (gen_movmem8b (out, in, out, in));
21186       len -= 8;
21187     }
21188
21189   if (len >= 4)
21190     {
21191       rtx reg = gen_reg_rtx (SImode);
21192       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
21193       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
21194       len -= 4;
21195       offset += 4;
21196     }
21197
21198   if (len >= 2)
21199     {
21200       rtx reg = gen_reg_rtx (HImode);
21201       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
21202                                               plus_constant (in, offset))));
21203       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
21204                             reg));
21205       len -= 2;
21206       offset += 2;
21207     }
21208
21209   if (len)
21210     {
21211       rtx reg = gen_reg_rtx (QImode);
21212       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
21213                                               plus_constant (in, offset))));
21214       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
21215                             reg));
21216     }
21217 }
21218
21219 void
21220 thumb_reload_out_hi (rtx *operands)
21221 {
21222   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
21223 }
21224
21225 /* Handle reading a half-word from memory during reload.  */
21226 void
21227 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
21228 {
21229   gcc_unreachable ();
21230 }
21231
21232 /* Return the length of a function name prefix
21233     that starts with the character 'c'.  */
21234 static int
21235 arm_get_strip_length (int c)
21236 {
21237   switch (c)
21238     {
21239     ARM_NAME_ENCODING_LENGTHS
21240       default: return 0;
21241     }
21242 }
21243
21244 /* Return a pointer to a function's name with any
21245    and all prefix encodings stripped from it.  */
21246 const char *
21247 arm_strip_name_encoding (const char *name)
21248 {
21249   int skip;
21250
21251   while ((skip = arm_get_strip_length (* name)))
21252     name += skip;
21253
21254   return name;
21255 }
21256
21257 /* If there is a '*' anywhere in the name's prefix, then
21258    emit the stripped name verbatim, otherwise prepend an
21259    underscore if leading underscores are being used.  */
21260 void
21261 arm_asm_output_labelref (FILE *stream, const char *name)
21262 {
21263   int skip;
21264   int verbatim = 0;
21265
21266   while ((skip = arm_get_strip_length (* name)))
21267     {
21268       verbatim |= (*name == '*');
21269       name += skip;
21270     }
21271
21272   if (verbatim)
21273     fputs (name, stream);
21274   else
21275     asm_fprintf (stream, "%U%s", name);
21276 }
21277
21278 static void
21279 arm_file_start (void)
21280 {
21281   int val;
21282
21283   if (TARGET_UNIFIED_ASM)
21284     asm_fprintf (asm_out_file, "\t.syntax unified\n");
21285
21286   if (TARGET_BPABI)
21287     {
21288       const char *fpu_name;
21289       if (arm_selected_arch)
21290         asm_fprintf (asm_out_file, "\t.arch %s\n", arm_selected_arch->name);
21291       else
21292         asm_fprintf (asm_out_file, "\t.cpu %s\n", arm_selected_cpu->name);
21293
21294       if (TARGET_SOFT_FLOAT)
21295         {
21296           if (TARGET_VFP)
21297             fpu_name = "softvfp";
21298           else
21299             fpu_name = "softfpa";
21300         }
21301       else
21302         {
21303           fpu_name = arm_fpu_desc->name;
21304           if (arm_fpu_desc->model == ARM_FP_MODEL_VFP)
21305             {
21306               if (TARGET_HARD_FLOAT)
21307                 asm_fprintf (asm_out_file, "\t.eabi_attribute 27, 3\n");
21308               if (TARGET_HARD_FLOAT_ABI)
21309                 asm_fprintf (asm_out_file, "\t.eabi_attribute 28, 1\n");
21310             }
21311         }
21312       asm_fprintf (asm_out_file, "\t.fpu %s\n", fpu_name);
21313
21314       /* Some of these attributes only apply when the corresponding features
21315          are used.  However we don't have any easy way of figuring this out.
21316          Conservatively record the setting that would have been used.  */
21317
21318       /* Tag_ABI_FP_rounding.  */
21319       if (flag_rounding_math)
21320         asm_fprintf (asm_out_file, "\t.eabi_attribute 19, 1\n");
21321       if (!flag_unsafe_math_optimizations)
21322         {
21323           /* Tag_ABI_FP_denomal.  */
21324           asm_fprintf (asm_out_file, "\t.eabi_attribute 20, 1\n");
21325           /* Tag_ABI_FP_exceptions.  */
21326           asm_fprintf (asm_out_file, "\t.eabi_attribute 21, 1\n");
21327         }
21328       /* Tag_ABI_FP_user_exceptions.  */
21329       if (flag_signaling_nans)
21330         asm_fprintf (asm_out_file, "\t.eabi_attribute 22, 1\n");
21331       /* Tag_ABI_FP_number_model.  */
21332       asm_fprintf (asm_out_file, "\t.eabi_attribute 23, %d\n", 
21333                    flag_finite_math_only ? 1 : 3);
21334
21335       /* Tag_ABI_align8_needed.  */
21336       asm_fprintf (asm_out_file, "\t.eabi_attribute 24, 1\n");
21337       /* Tag_ABI_align8_preserved.  */
21338       asm_fprintf (asm_out_file, "\t.eabi_attribute 25, 1\n");
21339       /* Tag_ABI_enum_size.  */
21340       asm_fprintf (asm_out_file, "\t.eabi_attribute 26, %d\n",
21341                    flag_short_enums ? 1 : 2);
21342
21343       /* Tag_ABI_optimization_goals.  */
21344       if (optimize_size)
21345         val = 4;
21346       else if (optimize >= 2)
21347         val = 2;
21348       else if (optimize)
21349         val = 1;
21350       else
21351         val = 6;
21352       asm_fprintf (asm_out_file, "\t.eabi_attribute 30, %d\n", val);
21353
21354       /* Tag_ABI_FP_16bit_format.  */
21355       if (arm_fp16_format)
21356         asm_fprintf (asm_out_file, "\t.eabi_attribute 38, %d\n",
21357                      (int)arm_fp16_format);
21358
21359       if (arm_lang_output_object_attributes_hook)
21360         arm_lang_output_object_attributes_hook();
21361     }
21362   default_file_start();
21363 }
21364
21365 static void
21366 arm_file_end (void)
21367 {
21368   int regno;
21369
21370   if (NEED_INDICATE_EXEC_STACK)
21371     /* Add .note.GNU-stack.  */
21372     file_end_indicate_exec_stack ();
21373
21374   if (! thumb_call_reg_needed)
21375     return;
21376
21377   switch_to_section (text_section);
21378   asm_fprintf (asm_out_file, "\t.code 16\n");
21379   ASM_OUTPUT_ALIGN (asm_out_file, 1);
21380
21381   for (regno = 0; regno < LR_REGNUM; regno++)
21382     {
21383       rtx label = thumb_call_via_label[regno];
21384
21385       if (label != 0)
21386         {
21387           targetm.asm_out.internal_label (asm_out_file, "L",
21388                                           CODE_LABEL_NUMBER (label));
21389           asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
21390         }
21391     }
21392 }
21393
21394 #ifndef ARM_PE
21395 /* Symbols in the text segment can be accessed without indirecting via the
21396    constant pool; it may take an extra binary operation, but this is still
21397    faster than indirecting via memory.  Don't do this when not optimizing,
21398    since we won't be calculating al of the offsets necessary to do this
21399    simplification.  */
21400
21401 static void
21402 arm_encode_section_info (tree decl, rtx rtl, int first)
21403 {
21404   if (optimize > 0 && TREE_CONSTANT (decl))
21405     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
21406
21407   default_encode_section_info (decl, rtl, first);
21408 }
21409 #endif /* !ARM_PE */
21410
21411 static void
21412 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
21413 {
21414   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
21415       && !strcmp (prefix, "L"))
21416     {
21417       arm_ccfsm_state = 0;
21418       arm_target_insn = NULL;
21419     }
21420   default_internal_label (stream, prefix, labelno);
21421 }
21422
21423 /* Output code to add DELTA to the first argument, and then jump
21424    to FUNCTION.  Used for C++ multiple inheritance.  */
21425 static void
21426 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
21427                      HOST_WIDE_INT delta,
21428                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
21429                      tree function)
21430 {
21431   static int thunk_label = 0;
21432   char label[256];
21433   char labelpc[256];
21434   int mi_delta = delta;
21435   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
21436   int shift = 0;
21437   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
21438                     ? 1 : 0);
21439   if (mi_delta < 0)
21440     mi_delta = - mi_delta;
21441
21442   if (TARGET_THUMB1)
21443     {
21444       int labelno = thunk_label++;
21445       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
21446       /* Thunks are entered in arm mode when avaiable.  */
21447       if (TARGET_THUMB1_ONLY)
21448         {
21449           /* push r3 so we can use it as a temporary.  */
21450           /* TODO: Omit this save if r3 is not used.  */
21451           fputs ("\tpush {r3}\n", file);
21452           fputs ("\tldr\tr3, ", file);
21453         }
21454       else
21455         {
21456           fputs ("\tldr\tr12, ", file);
21457         }
21458       assemble_name (file, label);
21459       fputc ('\n', file);
21460       if (flag_pic)
21461         {
21462           /* If we are generating PIC, the ldr instruction below loads
21463              "(target - 7) - .LTHUNKPCn" into r12.  The pc reads as
21464              the address of the add + 8, so we have:
21465
21466              r12 = (target - 7) - .LTHUNKPCn + (.LTHUNKPCn + 8)
21467                  = target + 1.
21468
21469              Note that we have "+ 1" because some versions of GNU ld
21470              don't set the low bit of the result for R_ARM_REL32
21471              relocations against thumb function symbols.
21472              On ARMv6M this is +4, not +8.  */
21473           ASM_GENERATE_INTERNAL_LABEL (labelpc, "LTHUNKPC", labelno);
21474           assemble_name (file, labelpc);
21475           fputs (":\n", file);
21476           if (TARGET_THUMB1_ONLY)
21477             {
21478               /* This is 2 insns after the start of the thunk, so we know it
21479                  is 4-byte aligned.  */
21480               fputs ("\tadd\tr3, pc, r3\n", file);
21481               fputs ("\tmov r12, r3\n", file);
21482             }
21483           else
21484             fputs ("\tadd\tr12, pc, r12\n", file);
21485         }
21486       else if (TARGET_THUMB1_ONLY)
21487         fputs ("\tmov r12, r3\n", file);
21488     }
21489   if (TARGET_THUMB1_ONLY)
21490     {
21491       if (mi_delta > 255)
21492         {
21493           fputs ("\tldr\tr3, ", file);
21494           assemble_name (file, label);
21495           fputs ("+4\n", file);
21496           asm_fprintf (file, "\t%s\t%r, %r, r3\n",
21497                        mi_op, this_regno, this_regno);
21498         }
21499       else if (mi_delta != 0)
21500         {
21501           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
21502                        mi_op, this_regno, this_regno,
21503                        mi_delta);
21504         }
21505     }
21506   else
21507     {
21508       /* TODO: Use movw/movt for large constants when available.  */
21509       while (mi_delta != 0)
21510         {
21511           if ((mi_delta & (3 << shift)) == 0)
21512             shift += 2;
21513           else
21514             {
21515               asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
21516                            mi_op, this_regno, this_regno,
21517                            mi_delta & (0xff << shift));
21518               mi_delta &= ~(0xff << shift);
21519               shift += 8;
21520             }
21521         }
21522     }
21523   if (TARGET_THUMB1)
21524     {
21525       if (TARGET_THUMB1_ONLY)
21526         fputs ("\tpop\t{r3}\n", file);
21527
21528       fprintf (file, "\tbx\tr12\n");
21529       ASM_OUTPUT_ALIGN (file, 2);
21530       assemble_name (file, label);
21531       fputs (":\n", file);
21532       if (flag_pic)
21533         {
21534           /* Output ".word .LTHUNKn-7-.LTHUNKPCn".  */
21535           rtx tem = XEXP (DECL_RTL (function), 0);
21536           tem = gen_rtx_PLUS (GET_MODE (tem), tem, GEN_INT (-7));
21537           tem = gen_rtx_MINUS (GET_MODE (tem),
21538                                tem,
21539                                gen_rtx_SYMBOL_REF (Pmode,
21540                                                    ggc_strdup (labelpc)));
21541           assemble_integer (tem, 4, BITS_PER_WORD, 1);
21542         }
21543       else
21544         /* Output ".word .LTHUNKn".  */
21545         assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
21546
21547       if (TARGET_THUMB1_ONLY && mi_delta > 255)
21548         assemble_integer (GEN_INT(mi_delta), 4, BITS_PER_WORD, 1);
21549     }
21550   else
21551     {
21552       fputs ("\tb\t", file);
21553       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
21554       if (NEED_PLT_RELOC)
21555         fputs ("(PLT)", file);
21556       fputc ('\n', file);
21557     }
21558 }
21559
21560 int
21561 arm_emit_vector_const (FILE *file, rtx x)
21562 {
21563   int i;
21564   const char * pattern;
21565
21566   gcc_assert (GET_CODE (x) == CONST_VECTOR);
21567
21568   switch (GET_MODE (x))
21569     {
21570     case V2SImode: pattern = "%08x"; break;
21571     case V4HImode: pattern = "%04x"; break;
21572     case V8QImode: pattern = "%02x"; break;
21573     default:       gcc_unreachable ();
21574     }
21575
21576   fprintf (file, "0x");
21577   for (i = CONST_VECTOR_NUNITS (x); i--;)
21578     {
21579       rtx element;
21580
21581       element = CONST_VECTOR_ELT (x, i);
21582       fprintf (file, pattern, INTVAL (element));
21583     }
21584
21585   return 1;
21586 }
21587
21588 /* Emit a fp16 constant appropriately padded to occupy a 4-byte word.
21589    HFmode constant pool entries are actually loaded with ldr.  */
21590 void
21591 arm_emit_fp16_const (rtx c)
21592 {
21593   REAL_VALUE_TYPE r;
21594   long bits;
21595
21596   REAL_VALUE_FROM_CONST_DOUBLE (r, c);
21597   bits = real_to_target (NULL, &r, HFmode);
21598   if (WORDS_BIG_ENDIAN)
21599     assemble_zeros (2);
21600   assemble_integer (GEN_INT (bits), 2, BITS_PER_WORD, 1);
21601   if (!WORDS_BIG_ENDIAN)
21602     assemble_zeros (2);
21603 }
21604
21605 const char *
21606 arm_output_load_gr (rtx *operands)
21607 {
21608   rtx reg;
21609   rtx offset;
21610   rtx wcgr;
21611   rtx sum;
21612
21613   if (GET_CODE (operands [1]) != MEM
21614       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
21615       || GET_CODE (reg = XEXP (sum, 0)) != REG
21616       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
21617       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
21618     return "wldrw%?\t%0, %1";
21619
21620   /* Fix up an out-of-range load of a GR register.  */
21621   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
21622   wcgr = operands[0];
21623   operands[0] = reg;
21624   output_asm_insn ("ldr%?\t%0, %1", operands);
21625
21626   operands[0] = wcgr;
21627   operands[1] = reg;
21628   output_asm_insn ("tmcr%?\t%0, %1", operands);
21629   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
21630
21631   return "";
21632 }
21633
21634 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
21635
21636    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
21637    named arg and all anonymous args onto the stack.
21638    XXX I know the prologue shouldn't be pushing registers, but it is faster
21639    that way.  */
21640
21641 static void
21642 arm_setup_incoming_varargs (CUMULATIVE_ARGS *pcum,
21643                             enum machine_mode mode,
21644                             tree type,
21645                             int *pretend_size,
21646                             int second_time ATTRIBUTE_UNUSED)
21647 {
21648   int nregs;
21649   
21650   cfun->machine->uses_anonymous_args = 1;
21651   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
21652     {
21653       nregs = pcum->aapcs_ncrn;
21654       if ((nregs & 1) && arm_needs_doubleword_align (mode, type))
21655         nregs++;
21656     }
21657   else
21658     nregs = pcum->nregs;
21659   
21660   if (nregs < NUM_ARG_REGS)
21661     *pretend_size = (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
21662 }
21663
21664 /* Return nonzero if the CONSUMER instruction (a store) does not need
21665    PRODUCER's value to calculate the address.  */
21666
21667 int
21668 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
21669 {
21670   rtx value = PATTERN (producer);
21671   rtx addr = PATTERN (consumer);
21672
21673   if (GET_CODE (value) == COND_EXEC)
21674     value = COND_EXEC_CODE (value);
21675   if (GET_CODE (value) == PARALLEL)
21676     value = XVECEXP (value, 0, 0);
21677   value = XEXP (value, 0);
21678   if (GET_CODE (addr) == COND_EXEC)
21679     addr = COND_EXEC_CODE (addr);
21680   if (GET_CODE (addr) == PARALLEL)
21681     addr = XVECEXP (addr, 0, 0);
21682   addr = XEXP (addr, 0);
21683
21684   return !reg_overlap_mentioned_p (value, addr);
21685 }
21686
21687 /* Return nonzero if the CONSUMER instruction (a store) does need
21688    PRODUCER's value to calculate the address.  */
21689
21690 int
21691 arm_early_store_addr_dep (rtx producer, rtx consumer)
21692 {
21693   return !arm_no_early_store_addr_dep (producer, consumer);
21694 }
21695
21696 /* Return nonzero if the CONSUMER instruction (a load) does need
21697    PRODUCER's value to calculate the address.  */
21698
21699 int
21700 arm_early_load_addr_dep (rtx producer, rtx consumer)
21701 {
21702   rtx value = PATTERN (producer);
21703   rtx addr = PATTERN (consumer);
21704
21705   if (GET_CODE (value) == COND_EXEC)
21706     value = COND_EXEC_CODE (value);
21707   if (GET_CODE (value) == PARALLEL)
21708     value = XVECEXP (value, 0, 0);
21709   value = XEXP (value, 0);
21710   if (GET_CODE (addr) == COND_EXEC)
21711     addr = COND_EXEC_CODE (addr);
21712   if (GET_CODE (addr) == PARALLEL)
21713     addr = XVECEXP (addr, 0, 0);
21714   addr = XEXP (addr, 1);
21715
21716   return reg_overlap_mentioned_p (value, addr);
21717 }
21718
21719 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
21720    have an early register shift value or amount dependency on the
21721    result of PRODUCER.  */
21722
21723 int
21724 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
21725 {
21726   rtx value = PATTERN (producer);
21727   rtx op = PATTERN (consumer);
21728   rtx early_op;
21729
21730   if (GET_CODE (value) == COND_EXEC)
21731     value = COND_EXEC_CODE (value);
21732   if (GET_CODE (value) == PARALLEL)
21733     value = XVECEXP (value, 0, 0);
21734   value = XEXP (value, 0);
21735   if (GET_CODE (op) == COND_EXEC)
21736     op = COND_EXEC_CODE (op);
21737   if (GET_CODE (op) == PARALLEL)
21738     op = XVECEXP (op, 0, 0);
21739   op = XEXP (op, 1);
21740
21741   early_op = XEXP (op, 0);
21742   /* This is either an actual independent shift, or a shift applied to
21743      the first operand of another operation.  We want the whole shift
21744      operation.  */
21745   if (GET_CODE (early_op) == REG)
21746     early_op = op;
21747
21748   return !reg_overlap_mentioned_p (value, early_op);
21749 }
21750
21751 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
21752    have an early register shift value dependency on the result of
21753    PRODUCER.  */
21754
21755 int
21756 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
21757 {
21758   rtx value = PATTERN (producer);
21759   rtx op = PATTERN (consumer);
21760   rtx early_op;
21761
21762   if (GET_CODE (value) == COND_EXEC)
21763     value = COND_EXEC_CODE (value);
21764   if (GET_CODE (value) == PARALLEL)
21765     value = XVECEXP (value, 0, 0);
21766   value = XEXP (value, 0);
21767   if (GET_CODE (op) == COND_EXEC)
21768     op = COND_EXEC_CODE (op);
21769   if (GET_CODE (op) == PARALLEL)
21770     op = XVECEXP (op, 0, 0);
21771   op = XEXP (op, 1);
21772
21773   early_op = XEXP (op, 0);
21774
21775   /* This is either an actual independent shift, or a shift applied to
21776      the first operand of another operation.  We want the value being
21777      shifted, in either case.  */
21778   if (GET_CODE (early_op) != REG)
21779     early_op = XEXP (early_op, 0);
21780
21781   return !reg_overlap_mentioned_p (value, early_op);
21782 }
21783
21784 /* Return nonzero if the CONSUMER (a mul or mac op) does not
21785    have an early register mult dependency on the result of
21786    PRODUCER.  */
21787
21788 int
21789 arm_no_early_mul_dep (rtx producer, rtx consumer)
21790 {
21791   rtx value = PATTERN (producer);
21792   rtx op = PATTERN (consumer);
21793
21794   if (GET_CODE (value) == COND_EXEC)
21795     value = COND_EXEC_CODE (value);
21796   if (GET_CODE (value) == PARALLEL)
21797     value = XVECEXP (value, 0, 0);
21798   value = XEXP (value, 0);
21799   if (GET_CODE (op) == COND_EXEC)
21800     op = COND_EXEC_CODE (op);
21801   if (GET_CODE (op) == PARALLEL)
21802     op = XVECEXP (op, 0, 0);
21803   op = XEXP (op, 1);
21804
21805   if (GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
21806     {
21807       if (GET_CODE (XEXP (op, 0)) == MULT)
21808         return !reg_overlap_mentioned_p (value, XEXP (op, 0));
21809       else
21810         return !reg_overlap_mentioned_p (value, XEXP (op, 1));
21811     }
21812
21813   return 0;
21814 }
21815
21816 /* We can't rely on the caller doing the proper promotion when
21817    using APCS or ATPCS.  */
21818
21819 static bool
21820 arm_promote_prototypes (const_tree t ATTRIBUTE_UNUSED)
21821 {
21822     return !TARGET_AAPCS_BASED;
21823 }
21824
21825 static enum machine_mode
21826 arm_promote_function_mode (const_tree type ATTRIBUTE_UNUSED,
21827                            enum machine_mode mode,
21828                            int *punsignedp ATTRIBUTE_UNUSED,
21829                            const_tree fntype ATTRIBUTE_UNUSED,
21830                            int for_return ATTRIBUTE_UNUSED)
21831 {
21832   if (GET_MODE_CLASS (mode) == MODE_INT
21833       && GET_MODE_SIZE (mode) < 4)
21834     return SImode;
21835
21836   return mode;
21837 }
21838
21839 /* AAPCS based ABIs use short enums by default.  */
21840
21841 static bool
21842 arm_default_short_enums (void)
21843 {
21844   return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
21845 }
21846
21847
21848 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
21849
21850 static bool
21851 arm_align_anon_bitfield (void)
21852 {
21853   return TARGET_AAPCS_BASED;
21854 }
21855
21856
21857 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
21858
21859 static tree
21860 arm_cxx_guard_type (void)
21861 {
21862   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
21863 }
21864
21865 /* Return non-zero if the consumer (a multiply-accumulate instruction)
21866    has an accumulator dependency on the result of the producer (a
21867    multiplication instruction) and no other dependency on that result.  */
21868 int
21869 arm_mac_accumulator_is_mul_result (rtx producer, rtx consumer)
21870 {
21871   rtx mul = PATTERN (producer);
21872   rtx mac = PATTERN (consumer);
21873   rtx mul_result;
21874   rtx mac_op0, mac_op1, mac_acc;
21875
21876   if (GET_CODE (mul) == COND_EXEC)
21877     mul = COND_EXEC_CODE (mul);
21878   if (GET_CODE (mac) == COND_EXEC)
21879     mac = COND_EXEC_CODE (mac);
21880
21881   /* Check that mul is of the form (set (...) (mult ...))
21882      and mla is of the form (set (...) (plus (mult ...) (...))).  */
21883   if ((GET_CODE (mul) != SET || GET_CODE (XEXP (mul, 1)) != MULT)
21884       || (GET_CODE (mac) != SET || GET_CODE (XEXP (mac, 1)) != PLUS
21885           || GET_CODE (XEXP (XEXP (mac, 1), 0)) != MULT))
21886     return 0;
21887
21888   mul_result = XEXP (mul, 0);
21889   mac_op0 = XEXP (XEXP (XEXP (mac, 1), 0), 0);
21890   mac_op1 = XEXP (XEXP (XEXP (mac, 1), 0), 1);
21891   mac_acc = XEXP (XEXP (mac, 1), 1);
21892
21893   return (reg_overlap_mentioned_p (mul_result, mac_acc)
21894           && !reg_overlap_mentioned_p (mul_result, mac_op0)
21895           && !reg_overlap_mentioned_p (mul_result, mac_op1));
21896 }
21897
21898
21899 /* The EABI says test the least significant bit of a guard variable.  */
21900
21901 static bool
21902 arm_cxx_guard_mask_bit (void)
21903 {
21904   return TARGET_AAPCS_BASED;
21905 }
21906
21907
21908 /* The EABI specifies that all array cookies are 8 bytes long.  */
21909
21910 static tree
21911 arm_get_cookie_size (tree type)
21912 {
21913   tree size;
21914
21915   if (!TARGET_AAPCS_BASED)
21916     return default_cxx_get_cookie_size (type);
21917
21918   size = build_int_cst (sizetype, 8);
21919   return size;
21920 }
21921
21922
21923 /* The EABI says that array cookies should also contain the element size.  */
21924
21925 static bool
21926 arm_cookie_has_size (void)
21927 {
21928   return TARGET_AAPCS_BASED;
21929 }
21930
21931
21932 /* The EABI says constructors and destructors should return a pointer to
21933    the object constructed/destroyed.  */
21934
21935 static bool
21936 arm_cxx_cdtor_returns_this (void)
21937 {
21938   return TARGET_AAPCS_BASED;
21939 }
21940
21941 /* The EABI says that an inline function may never be the key
21942    method.  */
21943
21944 static bool
21945 arm_cxx_key_method_may_be_inline (void)
21946 {
21947   return !TARGET_AAPCS_BASED;
21948 }
21949
21950 static void
21951 arm_cxx_determine_class_data_visibility (tree decl)
21952 {
21953   if (!TARGET_AAPCS_BASED
21954       || !TARGET_DLLIMPORT_DECL_ATTRIBUTES)
21955     return;
21956
21957   /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
21958      is exported.  However, on systems without dynamic vague linkage,
21959      \S 3.2.5.6 says that COMDAT class data has hidden linkage.  */
21960   if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
21961     DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
21962   else
21963     DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
21964   DECL_VISIBILITY_SPECIFIED (decl) = 1;
21965 }
21966
21967 static bool
21968 arm_cxx_class_data_always_comdat (void)
21969 {
21970   /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
21971      vague linkage if the class has no key function.  */
21972   return !TARGET_AAPCS_BASED;
21973 }
21974
21975
21976 /* The EABI says __aeabi_atexit should be used to register static
21977    destructors.  */
21978
21979 static bool
21980 arm_cxx_use_aeabi_atexit (void)
21981 {
21982   return TARGET_AAPCS_BASED;
21983 }
21984
21985
21986 void
21987 arm_set_return_address (rtx source, rtx scratch)
21988 {
21989   arm_stack_offsets *offsets;
21990   HOST_WIDE_INT delta;
21991   rtx addr;
21992   unsigned long saved_regs;
21993
21994   offsets = arm_get_frame_offsets ();
21995   saved_regs = offsets->saved_regs_mask;
21996
21997   if ((saved_regs & (1 << LR_REGNUM)) == 0)
21998     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
21999   else
22000     {
22001       if (frame_pointer_needed)
22002         addr = plus_constant(hard_frame_pointer_rtx, -4);
22003       else
22004         {
22005           /* LR will be the first saved register.  */
22006           delta = offsets->outgoing_args - (offsets->frame + 4);
22007
22008
22009           if (delta >= 4096)
22010             {
22011               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
22012                                      GEN_INT (delta & ~4095)));
22013               addr = scratch;
22014               delta &= 4095;
22015             }
22016           else
22017             addr = stack_pointer_rtx;
22018
22019           addr = plus_constant (addr, delta);
22020         }
22021       emit_move_insn (gen_frame_mem (Pmode, addr), source);
22022     }
22023 }
22024
22025
22026 void
22027 thumb_set_return_address (rtx source, rtx scratch)
22028 {
22029   arm_stack_offsets *offsets;
22030   HOST_WIDE_INT delta;
22031   HOST_WIDE_INT limit;
22032   int reg;
22033   rtx addr;
22034   unsigned long mask;
22035
22036   emit_use (source);
22037
22038   offsets = arm_get_frame_offsets ();
22039   mask = offsets->saved_regs_mask;
22040   if (mask & (1 << LR_REGNUM))
22041     {
22042       limit = 1024;
22043       /* Find the saved regs.  */
22044       if (frame_pointer_needed)
22045         {
22046           delta = offsets->soft_frame - offsets->saved_args;
22047           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
22048           if (TARGET_THUMB1)
22049             limit = 128;
22050         }
22051       else
22052         {
22053           delta = offsets->outgoing_args - offsets->saved_args;
22054           reg = SP_REGNUM;
22055         }
22056       /* Allow for the stack frame.  */
22057       if (TARGET_THUMB1 && TARGET_BACKTRACE)
22058         delta -= 16;
22059       /* The link register is always the first saved register.  */
22060       delta -= 4;
22061
22062       /* Construct the address.  */
22063       addr = gen_rtx_REG (SImode, reg);
22064       if (delta > limit)
22065         {
22066           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
22067           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
22068           addr = scratch;
22069         }
22070       else
22071         addr = plus_constant (addr, delta);
22072
22073       emit_move_insn (gen_frame_mem (Pmode, addr), source);
22074     }
22075   else
22076     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
22077 }
22078
22079 /* Implements target hook vector_mode_supported_p.  */
22080 bool
22081 arm_vector_mode_supported_p (enum machine_mode mode)
22082 {
22083   /* Neon also supports V2SImode, etc. listed in the clause below.  */
22084   if (TARGET_NEON && (mode == V2SFmode || mode == V4SImode || mode == V8HImode
22085       || mode == V16QImode || mode == V4SFmode || mode == V2DImode))
22086     return true;
22087
22088   if ((TARGET_NEON || TARGET_IWMMXT)
22089       && ((mode == V2SImode)
22090           || (mode == V4HImode)
22091           || (mode == V8QImode)))
22092     return true;
22093
22094   return false;
22095 }
22096
22097 /* Use the option -mvectorize-with-neon-quad to override the use of doubleword
22098    registers when autovectorizing for Neon, at least until multiple vector
22099    widths are supported properly by the middle-end.  */
22100
22101 static enum machine_mode
22102 arm_preferred_simd_mode (enum machine_mode mode)
22103 {
22104   if (TARGET_NEON)
22105     switch (mode)
22106       {
22107       case SFmode:
22108         return TARGET_NEON_VECTORIZE_QUAD ? V4SFmode : V2SFmode;
22109       case SImode:
22110         return TARGET_NEON_VECTORIZE_QUAD ? V4SImode : V2SImode;
22111       case HImode:
22112         return TARGET_NEON_VECTORIZE_QUAD ? V8HImode : V4HImode;
22113       case QImode:
22114         return TARGET_NEON_VECTORIZE_QUAD ? V16QImode : V8QImode;
22115       case DImode:
22116         if (TARGET_NEON_VECTORIZE_QUAD)
22117           return V2DImode;
22118         break;
22119
22120       default:;
22121       }
22122
22123   if (TARGET_REALLY_IWMMXT)
22124     switch (mode)
22125       {
22126       case SImode:
22127         return V2SImode;
22128       case HImode:
22129         return V4HImode;
22130       case QImode:
22131         return V8QImode;
22132
22133       default:;
22134       }
22135
22136   return word_mode;
22137 }
22138
22139 /* Implement TARGET_CLASS_LIKELY_SPILLED_P.
22140  
22141    We need to define this for LO_REGS on thumb.  Otherwise we can end up
22142    using r0-r4 for function arguments, r7 for the stack frame and don't
22143    have enough left over to do doubleword arithmetic.  */
22144
22145 static bool
22146 arm_class_likely_spilled_p (reg_class_t rclass)
22147 {
22148   if ((TARGET_THUMB && rclass == LO_REGS)
22149       || rclass  == CC_REG)
22150     return true;
22151
22152   return false;
22153 }
22154
22155 /* Implements target hook small_register_classes_for_mode_p.  */
22156 bool
22157 arm_small_register_classes_for_mode_p (enum machine_mode mode ATTRIBUTE_UNUSED)
22158 {
22159   return TARGET_THUMB1;
22160 }
22161
22162 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
22163    ARM insns and therefore guarantee that the shift count is modulo 256.
22164    DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
22165    guarantee no particular behavior for out-of-range counts.  */
22166
22167 static unsigned HOST_WIDE_INT
22168 arm_shift_truncation_mask (enum machine_mode mode)
22169 {
22170   return mode == SImode ? 255 : 0;
22171 }
22172
22173
22174 /* Map internal gcc register numbers to DWARF2 register numbers.  */
22175
22176 unsigned int
22177 arm_dbx_register_number (unsigned int regno)
22178 {
22179   if (regno < 16)
22180     return regno;
22181
22182   /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
22183      compatibility.  The EABI defines them as registers 96-103.  */
22184   if (IS_FPA_REGNUM (regno))
22185     return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
22186
22187   if (IS_VFP_REGNUM (regno))
22188     {
22189       /* See comment in arm_dwarf_register_span.  */
22190       if (VFP_REGNO_OK_FOR_SINGLE (regno))
22191         return 64 + regno - FIRST_VFP_REGNUM;
22192       else
22193         return 256 + (regno - FIRST_VFP_REGNUM) / 2;
22194     }
22195
22196   if (IS_IWMMXT_GR_REGNUM (regno))
22197     return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
22198
22199   if (IS_IWMMXT_REGNUM (regno))
22200     return 112 + regno - FIRST_IWMMXT_REGNUM;
22201
22202   gcc_unreachable ();
22203 }
22204
22205 /* Dwarf models VFPv3 registers as 32 64-bit registers.
22206    GCC models tham as 64 32-bit registers, so we need to describe this to
22207    the DWARF generation code.  Other registers can use the default.  */
22208 static rtx
22209 arm_dwarf_register_span (rtx rtl)
22210 {
22211   unsigned regno;
22212   int nregs;
22213   int i;
22214   rtx p;
22215
22216   regno = REGNO (rtl);
22217   if (!IS_VFP_REGNUM (regno))
22218     return NULL_RTX;
22219
22220   /* XXX FIXME: The EABI defines two VFP register ranges:
22221         64-95: Legacy VFPv2 numbering for S0-S31 (obsolescent)
22222         256-287: D0-D31
22223      The recommended encoding for S0-S31 is a DW_OP_bit_piece of the
22224      corresponding D register.  Until GDB supports this, we shall use the
22225      legacy encodings.  We also use these encodings for D0-D15 for
22226      compatibility with older debuggers.  */
22227   if (VFP_REGNO_OK_FOR_SINGLE (regno))
22228     return NULL_RTX;
22229
22230   nregs = GET_MODE_SIZE (GET_MODE (rtl)) / 8;
22231   p = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nregs));
22232   regno = (regno - FIRST_VFP_REGNUM) / 2;
22233   for (i = 0; i < nregs; i++)
22234     XVECEXP (p, 0, i) = gen_rtx_REG (DImode, 256 + regno + i);
22235
22236   return p;
22237 }
22238
22239 #if ARM_UNWIND_INFO
22240 /* Emit unwind directives for a store-multiple instruction or stack pointer
22241    push during alignment.
22242    These should only ever be generated by the function prologue code, so
22243    expect them to have a particular form.  */
22244
22245 static void
22246 arm_unwind_emit_sequence (FILE * asm_out_file, rtx p)
22247 {
22248   int i;
22249   HOST_WIDE_INT offset;
22250   HOST_WIDE_INT nregs;
22251   int reg_size;
22252   unsigned reg;
22253   unsigned lastreg;
22254   rtx e;
22255
22256   e = XVECEXP (p, 0, 0);
22257   if (GET_CODE (e) != SET)
22258     abort ();
22259
22260   /* First insn will adjust the stack pointer.  */
22261   if (GET_CODE (e) != SET
22262       || GET_CODE (XEXP (e, 0)) != REG
22263       || REGNO (XEXP (e, 0)) != SP_REGNUM
22264       || GET_CODE (XEXP (e, 1)) != PLUS)
22265     abort ();
22266
22267   offset = -INTVAL (XEXP (XEXP (e, 1), 1));
22268   nregs = XVECLEN (p, 0) - 1;
22269
22270   reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
22271   if (reg < 16)
22272     {
22273       /* The function prologue may also push pc, but not annotate it as it is
22274          never restored.  We turn this into a stack pointer adjustment.  */
22275       if (nregs * 4 == offset - 4)
22276         {
22277           fprintf (asm_out_file, "\t.pad #4\n");
22278           offset -= 4;
22279         }
22280       reg_size = 4;
22281       fprintf (asm_out_file, "\t.save {");
22282     }
22283   else if (IS_VFP_REGNUM (reg))
22284     {
22285       reg_size = 8;
22286       fprintf (asm_out_file, "\t.vsave {");
22287     }
22288   else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
22289     {
22290       /* FPA registers are done differently.  */
22291       asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
22292       return;
22293     }
22294   else
22295     /* Unknown register type.  */
22296     abort ();
22297
22298   /* If the stack increment doesn't match the size of the saved registers,
22299      something has gone horribly wrong.  */
22300   if (offset != nregs * reg_size)
22301     abort ();
22302
22303   offset = 0;
22304   lastreg = 0;
22305   /* The remaining insns will describe the stores.  */
22306   for (i = 1; i <= nregs; i++)
22307     {
22308       /* Expect (set (mem <addr>) (reg)).
22309          Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)).  */
22310       e = XVECEXP (p, 0, i);
22311       if (GET_CODE (e) != SET
22312           || GET_CODE (XEXP (e, 0)) != MEM
22313           || GET_CODE (XEXP (e, 1)) != REG)
22314         abort ();
22315
22316       reg = REGNO (XEXP (e, 1));
22317       if (reg < lastreg)
22318         abort ();
22319
22320       if (i != 1)
22321         fprintf (asm_out_file, ", ");
22322       /* We can't use %r for vfp because we need to use the
22323          double precision register names.  */
22324       if (IS_VFP_REGNUM (reg))
22325         asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
22326       else
22327         asm_fprintf (asm_out_file, "%r", reg);
22328
22329 #ifdef ENABLE_CHECKING
22330       /* Check that the addresses are consecutive.  */
22331       e = XEXP (XEXP (e, 0), 0);
22332       if (GET_CODE (e) == PLUS)
22333         {
22334           offset += reg_size;
22335           if (GET_CODE (XEXP (e, 0)) != REG
22336               || REGNO (XEXP (e, 0)) != SP_REGNUM
22337               || GET_CODE (XEXP (e, 1)) != CONST_INT
22338               || offset != INTVAL (XEXP (e, 1)))
22339             abort ();
22340         }
22341       else if (i != 1
22342                || GET_CODE (e) != REG
22343                || REGNO (e) != SP_REGNUM)
22344         abort ();
22345 #endif
22346     }
22347   fprintf (asm_out_file, "}\n");
22348 }
22349
22350 /*  Emit unwind directives for a SET.  */
22351
22352 static void
22353 arm_unwind_emit_set (FILE * asm_out_file, rtx p)
22354 {
22355   rtx e0;
22356   rtx e1;
22357   unsigned reg;
22358
22359   e0 = XEXP (p, 0);
22360   e1 = XEXP (p, 1);
22361   switch (GET_CODE (e0))
22362     {
22363     case MEM:
22364       /* Pushing a single register.  */
22365       if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
22366           || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
22367           || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
22368         abort ();
22369
22370       asm_fprintf (asm_out_file, "\t.save ");
22371       if (IS_VFP_REGNUM (REGNO (e1)))
22372         asm_fprintf(asm_out_file, "{d%d}\n",
22373                     (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
22374       else
22375         asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
22376       break;
22377
22378     case REG:
22379       if (REGNO (e0) == SP_REGNUM)
22380         {
22381           /* A stack increment.  */
22382           if (GET_CODE (e1) != PLUS
22383               || GET_CODE (XEXP (e1, 0)) != REG
22384               || REGNO (XEXP (e1, 0)) != SP_REGNUM
22385               || GET_CODE (XEXP (e1, 1)) != CONST_INT)
22386             abort ();
22387
22388           asm_fprintf (asm_out_file, "\t.pad #%wd\n",
22389                        -INTVAL (XEXP (e1, 1)));
22390         }
22391       else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
22392         {
22393           HOST_WIDE_INT offset;
22394
22395           if (GET_CODE (e1) == PLUS)
22396             {
22397               if (GET_CODE (XEXP (e1, 0)) != REG
22398                   || GET_CODE (XEXP (e1, 1)) != CONST_INT)
22399                 abort ();
22400               reg = REGNO (XEXP (e1, 0));
22401               offset = INTVAL (XEXP (e1, 1));
22402               asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
22403                            HARD_FRAME_POINTER_REGNUM, reg,
22404                            offset);
22405             }
22406           else if (GET_CODE (e1) == REG)
22407             {
22408               reg = REGNO (e1);
22409               asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
22410                            HARD_FRAME_POINTER_REGNUM, reg);
22411             }
22412           else
22413             abort ();
22414         }
22415       else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
22416         {
22417           /* Move from sp to reg.  */
22418           asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
22419         }
22420      else if (GET_CODE (e1) == PLUS
22421               && GET_CODE (XEXP (e1, 0)) == REG
22422               && REGNO (XEXP (e1, 0)) == SP_REGNUM
22423               && GET_CODE (XEXP (e1, 1)) == CONST_INT)
22424         {
22425           /* Set reg to offset from sp.  */
22426           asm_fprintf (asm_out_file, "\t.movsp %r, #%d\n",
22427                        REGNO (e0), (int)INTVAL(XEXP (e1, 1)));
22428         }
22429       else if (GET_CODE (e1) == UNSPEC && XINT (e1, 1) == UNSPEC_STACK_ALIGN)
22430         {
22431           /* Stack pointer save before alignment.  */
22432           reg = REGNO (e0);
22433           asm_fprintf (asm_out_file, "\t.unwind_raw 0, 0x%x @ vsp = r%d\n",
22434                        reg + 0x90, reg);
22435         }
22436       else
22437         abort ();
22438       break;
22439
22440     default:
22441       abort ();
22442     }
22443 }
22444
22445
22446 /* Emit unwind directives for the given insn.  */
22447
22448 static void
22449 arm_unwind_emit (FILE * asm_out_file, rtx insn)
22450 {
22451   rtx pat;
22452
22453   if (arm_except_unwind_info (&global_options) != UI_TARGET)
22454     return;
22455
22456   if (!(flag_unwind_tables || crtl->uses_eh_lsda)
22457       && (TREE_NOTHROW (current_function_decl)
22458           || crtl->all_throwers_are_sibcalls))
22459     return;
22460
22461   if (GET_CODE (insn) == NOTE || !RTX_FRAME_RELATED_P (insn))
22462     return;
22463
22464   pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
22465   if (pat)
22466     pat = XEXP (pat, 0);
22467   else
22468     pat = PATTERN (insn);
22469
22470   switch (GET_CODE (pat))
22471     {
22472     case SET:
22473       arm_unwind_emit_set (asm_out_file, pat);
22474       break;
22475
22476     case SEQUENCE:
22477       /* Store multiple.  */
22478       arm_unwind_emit_sequence (asm_out_file, pat);
22479       break;
22480
22481     default:
22482       abort();
22483     }
22484 }
22485
22486
22487 /* Output a reference from a function exception table to the type_info
22488    object X.  The EABI specifies that the symbol should be relocated by
22489    an R_ARM_TARGET2 relocation.  */
22490
22491 static bool
22492 arm_output_ttype (rtx x)
22493 {
22494   fputs ("\t.word\t", asm_out_file);
22495   output_addr_const (asm_out_file, x);
22496   /* Use special relocations for symbol references.  */
22497   if (GET_CODE (x) != CONST_INT)
22498     fputs ("(TARGET2)", asm_out_file);
22499   fputc ('\n', asm_out_file);
22500
22501   return TRUE;
22502 }
22503
22504 /* Implement TARGET_ASM_EMIT_EXCEPT_PERSONALITY.  */
22505
22506 static void
22507 arm_asm_emit_except_personality (rtx personality)
22508 {
22509   fputs ("\t.personality\t", asm_out_file);
22510   output_addr_const (asm_out_file, personality);
22511   fputc ('\n', asm_out_file);
22512 }
22513
22514 /* Implement TARGET_ASM_INITIALIZE_SECTIONS.  */
22515
22516 static void
22517 arm_asm_init_sections (void)
22518 {
22519   exception_section = get_unnamed_section (0, output_section_asm_op,
22520                                            "\t.handlerdata");
22521 }
22522 #endif /* ARM_UNWIND_INFO */
22523
22524 /* Implement TARGET_EXCEPT_UNWIND_INFO.  */
22525
22526 static enum unwind_info_type
22527 arm_except_unwind_info (struct gcc_options *opts)
22528 {
22529   /* Honor the --enable-sjlj-exceptions configure switch.  */
22530 #ifdef CONFIG_SJLJ_EXCEPTIONS
22531   if (CONFIG_SJLJ_EXCEPTIONS)
22532     return UI_SJLJ;
22533 #endif
22534
22535   /* If not using ARM EABI unwind tables... */
22536   if (ARM_UNWIND_INFO)
22537     {
22538       /* For simplicity elsewhere in this file, indicate that all unwind
22539          info is disabled if we're not emitting unwind tables.  */
22540       if (!opts->x_flag_exceptions && !opts->x_flag_unwind_tables)
22541         return UI_NONE;
22542       else
22543         return UI_TARGET;
22544     }
22545
22546   /* ... we use sjlj exceptions for backwards compatibility.  */
22547   return UI_SJLJ;
22548 }
22549
22550
22551 /* Handle UNSPEC DWARF call frame instructions.  These are needed for dynamic
22552    stack alignment.  */
22553
22554 static void
22555 arm_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
22556 {
22557   rtx unspec = SET_SRC (pattern);
22558   gcc_assert (GET_CODE (unspec) == UNSPEC);
22559
22560   switch (index)
22561     {
22562     case UNSPEC_STACK_ALIGN:
22563       /* ??? We should set the CFA = (SP & ~7).  At this point we haven't
22564          put anything on the stack, so hopefully it won't matter.
22565          CFA = SP will be correct after alignment.  */
22566       dwarf2out_reg_save_reg (label, stack_pointer_rtx,
22567                               SET_DEST (pattern));
22568       break;
22569     default:
22570       gcc_unreachable ();
22571     }
22572 }
22573
22574
22575 /* Output unwind directives for the start/end of a function.  */
22576
22577 void
22578 arm_output_fn_unwind (FILE * f, bool prologue)
22579 {
22580   if (arm_except_unwind_info (&global_options) != UI_TARGET)
22581     return;
22582
22583   if (prologue)
22584     fputs ("\t.fnstart\n", f);
22585   else
22586     {
22587       /* If this function will never be unwound, then mark it as such.
22588          The came condition is used in arm_unwind_emit to suppress
22589          the frame annotations.  */
22590       if (!(flag_unwind_tables || crtl->uses_eh_lsda)
22591           && (TREE_NOTHROW (current_function_decl)
22592               || crtl->all_throwers_are_sibcalls))
22593         fputs("\t.cantunwind\n", f);
22594
22595       fputs ("\t.fnend\n", f);
22596     }
22597 }
22598
22599 static bool
22600 arm_emit_tls_decoration (FILE *fp, rtx x)
22601 {
22602   enum tls_reloc reloc;
22603   rtx val;
22604
22605   val = XVECEXP (x, 0, 0);
22606   reloc = (enum tls_reloc) INTVAL (XVECEXP (x, 0, 1));
22607
22608   output_addr_const (fp, val);
22609
22610   switch (reloc)
22611     {
22612     case TLS_GD32:
22613       fputs ("(tlsgd)", fp);
22614       break;
22615     case TLS_LDM32:
22616       fputs ("(tlsldm)", fp);
22617       break;
22618     case TLS_LDO32:
22619       fputs ("(tlsldo)", fp);
22620       break;
22621     case TLS_IE32:
22622       fputs ("(gottpoff)", fp);
22623       break;
22624     case TLS_LE32:
22625       fputs ("(tpoff)", fp);
22626       break;
22627     default:
22628       gcc_unreachable ();
22629     }
22630
22631   switch (reloc)
22632     {
22633     case TLS_GD32:
22634     case TLS_LDM32:
22635     case TLS_IE32:
22636       fputs (" + (. - ", fp);
22637       output_addr_const (fp, XVECEXP (x, 0, 2));
22638       fputs (" - ", fp);
22639       output_addr_const (fp, XVECEXP (x, 0, 3));
22640       fputc (')', fp);
22641       break;
22642     default:
22643       break;
22644     }
22645
22646   return TRUE;
22647 }
22648
22649 /* ARM implementation of TARGET_ASM_OUTPUT_DWARF_DTPREL.  */
22650
22651 static void
22652 arm_output_dwarf_dtprel (FILE *file, int size, rtx x)
22653 {
22654   gcc_assert (size == 4);
22655   fputs ("\t.word\t", file);
22656   output_addr_const (file, x);
22657   fputs ("(tlsldo)", file);
22658 }
22659
22660 /* Implement TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA.  */
22661
22662 static bool
22663 arm_output_addr_const_extra (FILE *fp, rtx x)
22664 {
22665   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
22666     return arm_emit_tls_decoration (fp, x);
22667   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
22668     {
22669       char label[256];
22670       int labelno = INTVAL (XVECEXP (x, 0, 0));
22671
22672       ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
22673       assemble_name_raw (fp, label);
22674
22675       return TRUE;
22676     }
22677   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_GOTSYM_OFF)
22678     {
22679       assemble_name (fp, "_GLOBAL_OFFSET_TABLE_");
22680       if (GOT_PCREL)
22681         fputs ("+.", fp);
22682       fputs ("-(", fp);
22683       output_addr_const (fp, XVECEXP (x, 0, 0));
22684       fputc (')', fp);
22685       return TRUE;
22686     }
22687   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_SYMBOL_OFFSET)
22688     {
22689       output_addr_const (fp, XVECEXP (x, 0, 0));
22690       if (GOT_PCREL)
22691         fputs ("+.", fp);
22692       fputs ("-(", fp);
22693       output_addr_const (fp, XVECEXP (x, 0, 1));
22694       fputc (')', fp);
22695       return TRUE;
22696     }
22697   else if (GET_CODE (x) == CONST_VECTOR)
22698     return arm_emit_vector_const (fp, x);
22699
22700   return FALSE;
22701 }
22702
22703 /* Output assembly for a shift instruction.
22704    SET_FLAGS determines how the instruction modifies the condition codes.
22705    0 - Do not set condition codes.
22706    1 - Set condition codes.
22707    2 - Use smallest instruction.  */
22708 const char *
22709 arm_output_shift(rtx * operands, int set_flags)
22710 {
22711   char pattern[100];
22712   static const char flag_chars[3] = {'?', '.', '!'};
22713   const char *shift;
22714   HOST_WIDE_INT val;
22715   char c;
22716   
22717   c = flag_chars[set_flags];
22718   if (TARGET_UNIFIED_ASM)
22719     {
22720       shift = shift_op(operands[3], &val);
22721       if (shift)
22722         {
22723           if (val != -1)
22724             operands[2] = GEN_INT(val);
22725           sprintf (pattern, "%s%%%c\t%%0, %%1, %%2", shift, c);
22726         }
22727       else
22728         sprintf (pattern, "mov%%%c\t%%0, %%1", c);
22729     }
22730   else
22731     sprintf (pattern, "mov%%%c\t%%0, %%1%%S3", c);
22732   output_asm_insn (pattern, operands);
22733   return "";
22734 }
22735
22736 /* Output a Thumb-1 casesi dispatch sequence.  */
22737 const char *
22738 thumb1_output_casesi (rtx *operands)
22739 {
22740   rtx diff_vec = PATTERN (next_real_insn (operands[0]));
22741
22742   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
22743
22744   switch (GET_MODE(diff_vec))
22745     {
22746     case QImode:
22747       return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned ? 
22748               "bl\t%___gnu_thumb1_case_uqi" : "bl\t%___gnu_thumb1_case_sqi");
22749     case HImode:
22750       return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned ? 
22751               "bl\t%___gnu_thumb1_case_uhi" : "bl\t%___gnu_thumb1_case_shi");
22752     case SImode:
22753       return "bl\t%___gnu_thumb1_case_si";
22754     default:
22755       gcc_unreachable ();
22756     }
22757 }
22758
22759 /* Output a Thumb-2 casesi instruction.  */
22760 const char *
22761 thumb2_output_casesi (rtx *operands)
22762 {
22763   rtx diff_vec = PATTERN (next_real_insn (operands[2]));
22764
22765   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
22766
22767   output_asm_insn ("cmp\t%0, %1", operands);
22768   output_asm_insn ("bhi\t%l3", operands);
22769   switch (GET_MODE(diff_vec))
22770     {
22771     case QImode:
22772       return "tbb\t[%|pc, %0]";
22773     case HImode:
22774       return "tbh\t[%|pc, %0, lsl #1]";
22775     case SImode:
22776       if (flag_pic)
22777         {
22778           output_asm_insn ("adr\t%4, %l2", operands);
22779           output_asm_insn ("ldr\t%5, [%4, %0, lsl #2]", operands);
22780           output_asm_insn ("add\t%4, %4, %5", operands);
22781           return "bx\t%4";
22782         }
22783       else
22784         {
22785           output_asm_insn ("adr\t%4, %l2", operands);
22786           return "ldr\t%|pc, [%4, %0, lsl #2]";
22787         }
22788     default:
22789       gcc_unreachable ();
22790     }
22791 }
22792
22793 /* Most ARM cores are single issue, but some newer ones can dual issue.
22794    The scheduler descriptions rely on this being correct.  */
22795 static int
22796 arm_issue_rate (void)
22797 {
22798   switch (arm_tune)
22799     {
22800     case cortexr4:
22801     case cortexr4f:
22802     case cortexa5:
22803     case cortexa8:
22804     case cortexa9:
22805       return 2;
22806
22807     default:
22808       return 1;
22809     }
22810 }
22811
22812 /* A table and a function to perform ARM-specific name mangling for
22813    NEON vector types in order to conform to the AAPCS (see "Procedure
22814    Call Standard for the ARM Architecture", Appendix A).  To qualify
22815    for emission with the mangled names defined in that document, a
22816    vector type must not only be of the correct mode but also be
22817    composed of NEON vector element types (e.g. __builtin_neon_qi).  */
22818 typedef struct
22819 {
22820   enum machine_mode mode;
22821   const char *element_type_name;
22822   const char *aapcs_name;
22823 } arm_mangle_map_entry;
22824
22825 static arm_mangle_map_entry arm_mangle_map[] = {
22826   /* 64-bit containerized types.  */
22827   { V8QImode,  "__builtin_neon_qi",     "15__simd64_int8_t" },
22828   { V8QImode,  "__builtin_neon_uqi",    "16__simd64_uint8_t" },
22829   { V4HImode,  "__builtin_neon_hi",     "16__simd64_int16_t" },
22830   { V4HImode,  "__builtin_neon_uhi",    "17__simd64_uint16_t" },
22831   { V2SImode,  "__builtin_neon_si",     "16__simd64_int32_t" },
22832   { V2SImode,  "__builtin_neon_usi",    "17__simd64_uint32_t" },
22833   { V2SFmode,  "__builtin_neon_sf",     "18__simd64_float32_t" },
22834   { V8QImode,  "__builtin_neon_poly8",  "16__simd64_poly8_t" },
22835   { V4HImode,  "__builtin_neon_poly16", "17__simd64_poly16_t" },
22836   /* 128-bit containerized types.  */
22837   { V16QImode, "__builtin_neon_qi",     "16__simd128_int8_t" },
22838   { V16QImode, "__builtin_neon_uqi",    "17__simd128_uint8_t" },
22839   { V8HImode,  "__builtin_neon_hi",     "17__simd128_int16_t" },
22840   { V8HImode,  "__builtin_neon_uhi",    "18__simd128_uint16_t" },
22841   { V4SImode,  "__builtin_neon_si",     "17__simd128_int32_t" },
22842   { V4SImode,  "__builtin_neon_usi",    "18__simd128_uint32_t" },
22843   { V4SFmode,  "__builtin_neon_sf",     "19__simd128_float32_t" },
22844   { V16QImode, "__builtin_neon_poly8",  "17__simd128_poly8_t" },
22845   { V8HImode,  "__builtin_neon_poly16", "18__simd128_poly16_t" },
22846   { VOIDmode, NULL, NULL }
22847 };
22848
22849 const char *
22850 arm_mangle_type (const_tree type)
22851 {
22852   arm_mangle_map_entry *pos = arm_mangle_map;
22853
22854   /* The ARM ABI documents (10th October 2008) say that "__va_list"
22855      has to be managled as if it is in the "std" namespace.  */
22856   if (TARGET_AAPCS_BASED 
22857       && lang_hooks.types_compatible_p (CONST_CAST_TREE (type), va_list_type))
22858     {
22859       static bool warned;
22860       if (!warned && warn_psabi && !in_system_header)
22861         {
22862           warned = true;
22863           inform (input_location,
22864                   "the mangling of %<va_list%> has changed in GCC 4.4");
22865         }
22866       return "St9__va_list";
22867     }
22868
22869   /* Half-precision float.  */
22870   if (TREE_CODE (type) == REAL_TYPE && TYPE_PRECISION (type) == 16)
22871     return "Dh";
22872
22873   if (TREE_CODE (type) != VECTOR_TYPE)
22874     return NULL;
22875
22876   /* Check the mode of the vector type, and the name of the vector
22877      element type, against the table.  */
22878   while (pos->mode != VOIDmode)
22879     {
22880       tree elt_type = TREE_TYPE (type);
22881
22882       if (pos->mode == TYPE_MODE (type)
22883           && TREE_CODE (TYPE_NAME (elt_type)) == TYPE_DECL
22884           && !strcmp (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (elt_type))),
22885                       pos->element_type_name))
22886         return pos->aapcs_name;
22887
22888       pos++;
22889     }
22890
22891   /* Use the default mangling for unrecognized (possibly user-defined)
22892      vector types.  */
22893   return NULL;
22894 }
22895
22896 /* Order of allocation of core registers for Thumb: this allocation is
22897    written over the corresponding initial entries of the array
22898    initialized with REG_ALLOC_ORDER.  We allocate all low registers
22899    first.  Saving and restoring a low register is usually cheaper than
22900    using a call-clobbered high register.  */
22901
22902 static const int thumb_core_reg_alloc_order[] =
22903 {
22904    3,  2,  1,  0,  4,  5,  6,  7,
22905   14, 12,  8,  9, 10, 11, 13, 15
22906 };
22907
22908 /* Adjust register allocation order when compiling for Thumb.  */
22909
22910 void
22911 arm_order_regs_for_local_alloc (void)
22912 {
22913   const int arm_reg_alloc_order[] = REG_ALLOC_ORDER;
22914   memcpy(reg_alloc_order, arm_reg_alloc_order, sizeof (reg_alloc_order));
22915   if (TARGET_THUMB)
22916     memcpy (reg_alloc_order, thumb_core_reg_alloc_order,
22917             sizeof (thumb_core_reg_alloc_order));
22918 }
22919
22920 /* Implement TARGET_FRAME_POINTER_REQUIRED.  */
22921
22922 bool
22923 arm_frame_pointer_required (void)
22924 {
22925   return (cfun->has_nonlocal_label
22926           || SUBTARGET_FRAME_POINTER_REQUIRED
22927           || (TARGET_ARM && TARGET_APCS_FRAME && ! leaf_function_p ()));
22928 }
22929
22930 /* Only thumb1 can't support conditional execution, so return true if
22931    the target is not thumb1.  */
22932 static bool
22933 arm_have_conditional_execution (void)
22934 {
22935   return !TARGET_THUMB1;
22936 }
22937
22938 /* Legitimize a memory reference for sync primitive implemented using
22939    ldrex / strex.  We currently force the form of the reference to be
22940    indirect without offset.  We do not yet support the indirect offset
22941    addressing supported by some ARM targets for these
22942    instructions.  */
22943 static rtx
22944 arm_legitimize_sync_memory (rtx memory)
22945 {
22946   rtx addr = force_reg (Pmode, XEXP (memory, 0));
22947   rtx legitimate_memory = gen_rtx_MEM (GET_MODE (memory), addr);
22948
22949   set_mem_alias_set (legitimate_memory, ALIAS_SET_MEMORY_BARRIER);
22950   MEM_VOLATILE_P (legitimate_memory) = MEM_VOLATILE_P (memory);
22951   return legitimate_memory;
22952 }
22953
22954 /* An instruction emitter. */
22955 typedef void (* emit_f) (int label, const char *, rtx *);
22956
22957 /* An instruction emitter that emits via the conventional
22958    output_asm_insn.  */
22959 static void
22960 arm_emit (int label ATTRIBUTE_UNUSED, const char *pattern, rtx *operands)
22961 {
22962   output_asm_insn (pattern, operands);
22963 }
22964
22965 /* Count the number of emitted synchronization instructions.  */
22966 static unsigned arm_insn_count;
22967
22968 /* An emitter that counts emitted instructions but does not actually
22969    emit instruction into the the instruction stream.  */
22970 static void
22971 arm_count (int label,
22972            const char *pattern ATTRIBUTE_UNUSED,
22973            rtx *operands ATTRIBUTE_UNUSED)
22974 {
22975   if (! label)
22976     ++ arm_insn_count;
22977 }
22978
22979 /* Construct a pattern using conventional output formatting and feed
22980    it to output_asm_insn.  Provides a mechanism to construct the
22981    output pattern on the fly.  Note the hard limit on the pattern
22982    buffer size.  */
22983 static void ATTRIBUTE_PRINTF_4
22984 arm_output_asm_insn (emit_f emit, int label, rtx *operands,
22985                      const char *pattern, ...)
22986 {
22987   va_list ap;
22988   char buffer[256];
22989
22990   va_start (ap, pattern);
22991   vsprintf (buffer, pattern, ap);
22992   va_end (ap);
22993   emit (label, buffer, operands);
22994 }
22995
22996 /* Emit the memory barrier instruction, if any, provided by this
22997    target to a specified emitter.  */
22998 static void
22999 arm_process_output_memory_barrier (emit_f emit, rtx *operands)
23000 {
23001   if (TARGET_HAVE_DMB)
23002     {
23003       /* Note we issue a system level barrier. We should consider
23004          issuing a inner shareabilty zone barrier here instead, ie.
23005          "DMB ISH".  */
23006       emit (0, "dmb\tsy", operands);
23007       return;
23008     }
23009
23010   if (TARGET_HAVE_DMB_MCR)
23011     {
23012       emit (0, "mcr\tp15, 0, r0, c7, c10, 5", operands);
23013       return;
23014     }
23015
23016   gcc_unreachable ();
23017 }
23018
23019 /* Emit the memory barrier instruction, if any, provided by this
23020    target.  */
23021 const char *
23022 arm_output_memory_barrier (rtx *operands)
23023 {
23024   arm_process_output_memory_barrier (arm_emit, operands);
23025   return "";
23026 }
23027
23028 /* Helper to figure out the instruction suffix required on ldrex/strex
23029    for operations on an object of the specified mode.  */
23030 static const char *
23031 arm_ldrex_suffix (enum machine_mode mode)
23032 {
23033   switch (mode)
23034     {
23035     case QImode: return "b";
23036     case HImode: return "h";
23037     case SImode: return "";
23038     case DImode: return "d";
23039     default:
23040       gcc_unreachable ();
23041     }
23042   return "";
23043 }
23044
23045 /* Emit an ldrex{b,h,d, } instruction appropriate for the specified
23046    mode.  */
23047 static void
23048 arm_output_ldrex (emit_f emit,
23049                   enum machine_mode mode,
23050                   rtx target,
23051                   rtx memory)
23052 {
23053   const char *suffix = arm_ldrex_suffix (mode);
23054   rtx operands[2];
23055
23056   operands[0] = target;
23057   operands[1] = memory;
23058   arm_output_asm_insn (emit, 0, operands, "ldrex%s\t%%0, %%C1", suffix);
23059 }
23060
23061 /* Emit a strex{b,h,d, } instruction appropriate for the specified
23062    mode.  */
23063 static void
23064 arm_output_strex (emit_f emit,
23065                   enum machine_mode mode,
23066                   const char *cc,
23067                   rtx result,
23068                   rtx value,
23069                   rtx memory)
23070 {
23071   const char *suffix = arm_ldrex_suffix (mode);
23072   rtx operands[3];
23073
23074   operands[0] = result;
23075   operands[1] = value;
23076   operands[2] = memory;
23077   arm_output_asm_insn (emit, 0, operands, "strex%s%s\t%%0, %%1, %%C2", suffix,
23078                        cc);
23079 }
23080
23081 /* Helper to emit a two operand instruction.  */
23082 static void
23083 arm_output_op2 (emit_f emit, const char *mnemonic, rtx d, rtx s)
23084 {
23085   rtx operands[2];
23086
23087   operands[0] = d;
23088   operands[1] = s;
23089   arm_output_asm_insn (emit, 0, operands, "%s\t%%0, %%1", mnemonic);
23090 }
23091
23092 /* Helper to emit a three operand instruction.  */
23093 static void
23094 arm_output_op3 (emit_f emit, const char *mnemonic, rtx d, rtx a, rtx b)
23095 {
23096   rtx operands[3];
23097
23098   operands[0] = d;
23099   operands[1] = a;
23100   operands[2] = b;
23101   arm_output_asm_insn (emit, 0, operands, "%s\t%%0, %%1, %%2", mnemonic);
23102 }
23103
23104 /* Emit a load store exclusive synchronization loop.
23105
23106    do
23107      old_value = [mem]
23108      if old_value != required_value
23109        break;
23110      t1 = sync_op (old_value, new_value)
23111      [mem] = t1, t2 = [0|1]
23112    while ! t2
23113
23114    Note:
23115      t1 == t2 is not permitted
23116      t1 == old_value is permitted
23117
23118    required_value:
23119
23120    RTX register or const_int representing the required old_value for
23121    the modify to continue, if NULL no comparsion is performed.  */
23122 static void
23123 arm_output_sync_loop (emit_f emit,
23124                       enum machine_mode mode,
23125                       rtx old_value,
23126                       rtx memory,
23127                       rtx required_value,
23128                       rtx new_value,
23129                       rtx t1,
23130                       rtx t2,
23131                       enum attr_sync_op sync_op,
23132                       int early_barrier_required)
23133 {
23134   rtx operands[1];
23135
23136   gcc_assert (t1 != t2);
23137
23138   if (early_barrier_required)
23139     arm_process_output_memory_barrier (emit, NULL);
23140
23141   arm_output_asm_insn (emit, 1, operands, "%sLSYT%%=:", LOCAL_LABEL_PREFIX);
23142
23143   arm_output_ldrex (emit, mode, old_value, memory);
23144
23145   if (required_value)
23146     {
23147       rtx operands[2];
23148
23149       operands[0] = old_value;
23150       operands[1] = required_value;
23151       arm_output_asm_insn (emit, 0, operands, "cmp\t%%0, %%1");
23152       arm_output_asm_insn (emit, 0, operands, "bne\t%sLSYB%%=", LOCAL_LABEL_PREFIX);
23153     }
23154
23155   switch (sync_op)
23156     {
23157     case SYNC_OP_ADD:
23158       arm_output_op3 (emit, "add", t1, old_value, new_value);
23159       break;
23160
23161     case SYNC_OP_SUB:
23162       arm_output_op3 (emit, "sub", t1, old_value, new_value);
23163       break;
23164
23165     case SYNC_OP_IOR:
23166       arm_output_op3 (emit, "orr", t1, old_value, new_value);
23167       break;
23168
23169     case SYNC_OP_XOR:
23170       arm_output_op3 (emit, "eor", t1, old_value, new_value);
23171       break;
23172
23173     case SYNC_OP_AND:
23174       arm_output_op3 (emit,"and", t1, old_value, new_value);
23175       break;
23176
23177     case SYNC_OP_NAND:
23178       arm_output_op3 (emit, "and", t1, old_value, new_value);
23179       arm_output_op2 (emit, "mvn", t1, t1);
23180       break;
23181
23182     case SYNC_OP_NONE:
23183       t1 = new_value;
23184       break;
23185     }
23186
23187   arm_output_strex (emit, mode, "", t2, t1, memory);
23188   operands[0] = t2;
23189   arm_output_asm_insn (emit, 0, operands, "teq\t%%0, #0");
23190   arm_output_asm_insn (emit, 0, operands, "bne\t%sLSYT%%=", LOCAL_LABEL_PREFIX);
23191
23192   arm_process_output_memory_barrier (emit, NULL);
23193   arm_output_asm_insn (emit, 1, operands, "%sLSYB%%=:", LOCAL_LABEL_PREFIX);
23194 }
23195
23196 static rtx
23197 arm_get_sync_operand (rtx *operands, int index, rtx default_value)
23198 {
23199   if (index > 0)
23200     default_value = operands[index - 1];
23201
23202   return default_value;
23203 }
23204
23205 #define FETCH_SYNC_OPERAND(NAME, DEFAULT) \
23206   arm_get_sync_operand (operands, (int) get_attr_sync_##NAME (insn), DEFAULT);
23207
23208 /* Extract the operands for a synchroniztion instruction from the
23209    instructions attributes and emit the instruction.  */
23210 static void
23211 arm_process_output_sync_insn (emit_f emit, rtx insn, rtx *operands)
23212 {
23213   rtx result, memory, required_value, new_value, t1, t2;
23214   int early_barrier;
23215   enum machine_mode mode;
23216   enum attr_sync_op sync_op;
23217
23218   result = FETCH_SYNC_OPERAND(result, 0);
23219   memory = FETCH_SYNC_OPERAND(memory, 0);
23220   required_value = FETCH_SYNC_OPERAND(required_value, 0);
23221   new_value = FETCH_SYNC_OPERAND(new_value, 0);
23222   t1 = FETCH_SYNC_OPERAND(t1, 0);
23223   t2 = FETCH_SYNC_OPERAND(t2, 0);
23224   early_barrier =
23225     get_attr_sync_release_barrier (insn) == SYNC_RELEASE_BARRIER_YES;
23226   sync_op = get_attr_sync_op (insn);
23227   mode = GET_MODE (memory);
23228
23229   arm_output_sync_loop (emit, mode, result, memory, required_value,
23230                         new_value, t1, t2, sync_op, early_barrier);
23231 }
23232
23233 /* Emit a synchronization instruction loop.  */
23234 const char *
23235 arm_output_sync_insn (rtx insn, rtx *operands)
23236 {
23237   arm_process_output_sync_insn (arm_emit, insn, operands);
23238   return "";
23239 }
23240
23241 /* Count the number of machine instruction that will be emitted for a
23242    synchronization instruction.  Note that the emitter used does not
23243    emit instructions, it just counts instructions being carefull not
23244    to count labels.  */
23245 unsigned int
23246 arm_sync_loop_insns (rtx insn, rtx *operands)
23247 {
23248   arm_insn_count = 0;
23249   arm_process_output_sync_insn (arm_count, insn, operands);
23250   return arm_insn_count;
23251 }
23252
23253 /* Helper to call a target sync instruction generator, dealing with
23254    the variation in operands required by the different generators.  */
23255 static rtx
23256 arm_call_generator (struct arm_sync_generator *generator, rtx old_value,
23257                     rtx memory, rtx required_value, rtx new_value)
23258 {
23259   switch (generator->op)
23260     {
23261     case arm_sync_generator_omn:
23262       gcc_assert (! required_value);
23263       return generator->u.omn (old_value, memory, new_value);
23264
23265     case arm_sync_generator_omrn:
23266       gcc_assert (required_value);
23267       return generator->u.omrn (old_value, memory, required_value, new_value);
23268     }
23269
23270   return NULL;
23271 }
23272
23273 /* Expand a synchronization loop. The synchronization loop is expanded
23274    as an opaque block of instructions in order to ensure that we do
23275    not subsequently get extraneous memory accesses inserted within the
23276    critical region. The exclusive access property of ldrex/strex is
23277    only guaranteed in there are no intervening memory accesses. */
23278 void
23279 arm_expand_sync (enum machine_mode mode,
23280                  struct arm_sync_generator *generator,
23281                  rtx target, rtx memory, rtx required_value, rtx new_value)
23282 {
23283   if (target == NULL)
23284     target = gen_reg_rtx (mode);
23285
23286   memory = arm_legitimize_sync_memory (memory);
23287   if (mode != SImode)
23288     {
23289       rtx load_temp = gen_reg_rtx (SImode);
23290
23291       if (required_value)
23292         required_value = convert_modes (SImode, mode, required_value, true);
23293
23294       new_value = convert_modes (SImode, mode, new_value, true);
23295       emit_insn (arm_call_generator (generator, load_temp, memory,
23296                                      required_value, new_value));
23297       emit_move_insn (target, gen_lowpart (mode, load_temp));
23298     }
23299   else
23300     {
23301       emit_insn (arm_call_generator (generator, target, memory, required_value,
23302                                      new_value));
23303     }
23304 }
23305
23306 static bool
23307 arm_vector_alignment_reachable (const_tree type, bool is_packed)
23308 {
23309   /* Vectors which aren't in packed structures will not be less aligned than
23310      the natural alignment of their element type, so this is safe.  */
23311   if (TARGET_NEON && !BYTES_BIG_ENDIAN)
23312     return !is_packed;
23313
23314   return default_builtin_vector_alignment_reachable (type, is_packed);
23315 }
23316
23317 static bool
23318 arm_builtin_support_vector_misalignment (enum machine_mode mode,
23319                                          const_tree type, int misalignment,
23320                                          bool is_packed)
23321 {
23322   if (TARGET_NEON && !BYTES_BIG_ENDIAN)
23323     {
23324       HOST_WIDE_INT align = TYPE_ALIGN_UNIT (type);
23325
23326       if (is_packed)
23327         return align == 1;
23328
23329       /* If the misalignment is unknown, we should be able to handle the access
23330          so long as it is not to a member of a packed data structure.  */
23331       if (misalignment == -1)
23332         return true;
23333
23334       /* Return true if the misalignment is a multiple of the natural alignment
23335          of the vector's element type.  This is probably always going to be
23336          true in practice, since we've already established that this isn't a
23337          packed access.  */
23338       return ((misalignment % align) == 0);
23339     }
23340   
23341   return default_builtin_support_vector_misalignment (mode, type, misalignment,
23342                                                       is_packed);
23343 }
23344
23345 static void
23346 arm_conditional_register_usage (void)
23347 {
23348   int regno;
23349
23350   if (TARGET_SOFT_FLOAT || TARGET_THUMB1 || !TARGET_FPA)
23351     {
23352       for (regno = FIRST_FPA_REGNUM;
23353            regno <= LAST_FPA_REGNUM; ++regno)
23354         fixed_regs[regno] = call_used_regs[regno] = 1;
23355     }
23356
23357   if (TARGET_THUMB1 && optimize_size)
23358     {
23359       /* When optimizing for size on Thumb-1, it's better not
23360         to use the HI regs, because of the overhead of
23361         stacking them.  */
23362       for (regno = FIRST_HI_REGNUM;
23363            regno <= LAST_HI_REGNUM; ++regno)
23364         fixed_regs[regno] = call_used_regs[regno] = 1;
23365     }
23366
23367   /* The link register can be clobbered by any branch insn,
23368      but we have no way to track that at present, so mark
23369      it as unavailable.  */
23370   if (TARGET_THUMB1)
23371     fixed_regs[LR_REGNUM] = call_used_regs[LR_REGNUM] = 1;
23372
23373   if (TARGET_32BIT && TARGET_HARD_FLOAT)
23374     {
23375       if (TARGET_MAVERICK)
23376         {
23377           for (regno = FIRST_FPA_REGNUM;
23378                regno <= LAST_FPA_REGNUM; ++ regno)
23379             fixed_regs[regno] = call_used_regs[regno] = 1;
23380           for (regno = FIRST_CIRRUS_FP_REGNUM;
23381                regno <= LAST_CIRRUS_FP_REGNUM; ++ regno)
23382             {
23383               fixed_regs[regno] = 0;
23384               call_used_regs[regno] = regno < FIRST_CIRRUS_FP_REGNUM + 4;
23385             }
23386         }
23387       if (TARGET_VFP)
23388         {
23389           /* VFPv3 registers are disabled when earlier VFP
23390              versions are selected due to the definition of
23391              LAST_VFP_REGNUM.  */
23392           for (regno = FIRST_VFP_REGNUM;
23393                regno <= LAST_VFP_REGNUM; ++ regno)
23394             {
23395               fixed_regs[regno] = 0;
23396               call_used_regs[regno] = regno < FIRST_VFP_REGNUM + 16
23397                 || regno >= FIRST_VFP_REGNUM + 32;
23398             }
23399         }
23400     }
23401
23402   if (TARGET_REALLY_IWMMXT)
23403     {
23404       regno = FIRST_IWMMXT_GR_REGNUM;
23405       /* The 2002/10/09 revision of the XScale ABI has wCG0
23406          and wCG1 as call-preserved registers.  The 2002/11/21
23407          revision changed this so that all wCG registers are
23408          scratch registers.  */
23409       for (regno = FIRST_IWMMXT_GR_REGNUM;
23410            regno <= LAST_IWMMXT_GR_REGNUM; ++ regno)
23411         fixed_regs[regno] = 0;
23412       /* The XScale ABI has wR0 - wR9 as scratch registers,
23413          the rest as call-preserved registers.  */
23414       for (regno = FIRST_IWMMXT_REGNUM;
23415            regno <= LAST_IWMMXT_REGNUM; ++ regno)
23416         {
23417           fixed_regs[regno] = 0;
23418           call_used_regs[regno] = regno < FIRST_IWMMXT_REGNUM + 10;
23419         }
23420     }
23421
23422   if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
23423     {
23424       fixed_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
23425       call_used_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
23426     }
23427   else if (TARGET_APCS_STACK)
23428     {
23429       fixed_regs[10]     = 1;
23430       call_used_regs[10] = 1;
23431     }
23432   /* -mcaller-super-interworking reserves r11 for calls to
23433      _interwork_r11_call_via_rN().  Making the register global
23434      is an easy way of ensuring that it remains valid for all
23435      calls.  */
23436   if (TARGET_APCS_FRAME || TARGET_CALLER_INTERWORKING
23437       || TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME)
23438     {
23439       fixed_regs[ARM_HARD_FRAME_POINTER_REGNUM] = 1;
23440       call_used_regs[ARM_HARD_FRAME_POINTER_REGNUM] = 1;
23441       if (TARGET_CALLER_INTERWORKING)
23442         global_regs[ARM_HARD_FRAME_POINTER_REGNUM] = 1;
23443     }
23444   SUBTARGET_CONDITIONAL_REGISTER_USAGE
23445 }
23446
23447 #include "gt-arm.h"