OSDN Git Service

PR target/46881
[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 static reg_class_t arm_preferred_rename_class (reg_class_t rclass);
252
253 \f
254 /* Table of machine attributes.  */
255 static const struct attribute_spec arm_attribute_table[] =
256 {
257   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
258   /* Function calls made to this symbol must be done indirectly, because
259      it may lie outside of the 26 bit addressing range of a normal function
260      call.  */
261   { "long_call",    0, 0, false, true,  true,  NULL },
262   /* Whereas these functions are always known to reside within the 26 bit
263      addressing range.  */
264   { "short_call",   0, 0, false, true,  true,  NULL },
265   /* Specify the procedure call conventions for a function.  */
266   { "pcs",          1, 1, false, true,  true,  arm_handle_pcs_attribute },
267   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
268   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
269   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
270   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
271 #ifdef ARM_PE
272   /* ARM/PE has three new attributes:
273      interfacearm - ?
274      dllexport - for exporting a function/variable that will live in a dll
275      dllimport - for importing a function/variable from a dll
276
277      Microsoft allows multiple declspecs in one __declspec, separating
278      them with spaces.  We do NOT support this.  Instead, use __declspec
279      multiple times.
280   */
281   { "dllimport",    0, 0, true,  false, false, NULL },
282   { "dllexport",    0, 0, true,  false, false, NULL },
283   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
284 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
285   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
286   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
287   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
288 #endif
289   { NULL,           0, 0, false, false, false, NULL }
290 };
291
292 /* Set default optimization options.  */
293 static const struct default_options arm_option_optimization_table[] =
294   {
295     /* Enable section anchors by default at -O1 or higher.  */
296     { OPT_LEVELS_1_PLUS, OPT_fsection_anchors, NULL, 1 },
297     { OPT_LEVELS_1_PLUS, OPT_fomit_frame_pointer, NULL, 1 },
298     { OPT_LEVELS_NONE, 0, NULL, 0 }
299   };
300 \f
301 /* Initialize the GCC target structure.  */
302 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
303 #undef  TARGET_MERGE_DECL_ATTRIBUTES
304 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
305 #endif
306
307 #undef TARGET_LEGITIMIZE_ADDRESS
308 #define TARGET_LEGITIMIZE_ADDRESS arm_legitimize_address
309
310 #undef  TARGET_ATTRIBUTE_TABLE
311 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
312
313 #undef TARGET_ASM_FILE_START
314 #define TARGET_ASM_FILE_START arm_file_start
315 #undef TARGET_ASM_FILE_END
316 #define TARGET_ASM_FILE_END arm_file_end
317
318 #undef  TARGET_ASM_ALIGNED_SI_OP
319 #define TARGET_ASM_ALIGNED_SI_OP NULL
320 #undef  TARGET_ASM_INTEGER
321 #define TARGET_ASM_INTEGER arm_assemble_integer
322
323 #undef TARGET_PRINT_OPERAND
324 #define TARGET_PRINT_OPERAND arm_print_operand
325 #undef TARGET_PRINT_OPERAND_ADDRESS
326 #define TARGET_PRINT_OPERAND_ADDRESS arm_print_operand_address
327 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
328 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P arm_print_operand_punct_valid_p
329
330 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
331 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA arm_output_addr_const_extra
332
333 #undef  TARGET_ASM_FUNCTION_PROLOGUE
334 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
335
336 #undef  TARGET_ASM_FUNCTION_EPILOGUE
337 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
338
339 #undef  TARGET_DEFAULT_TARGET_FLAGS
340 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
341 #undef  TARGET_HANDLE_OPTION
342 #define TARGET_HANDLE_OPTION arm_handle_option
343 #undef  TARGET_HELP
344 #define TARGET_HELP arm_target_help
345 #undef  TARGET_OPTION_OVERRIDE
346 #define TARGET_OPTION_OVERRIDE arm_option_override
347 #undef  TARGET_OPTION_OPTIMIZATION_TABLE
348 #define TARGET_OPTION_OPTIMIZATION_TABLE arm_option_optimization_table
349
350 #undef  TARGET_COMP_TYPE_ATTRIBUTES
351 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
352
353 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
354 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
355
356 #undef  TARGET_SCHED_ADJUST_COST
357 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
358
359 #undef TARGET_ENCODE_SECTION_INFO
360 #ifdef ARM_PE
361 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
362 #else
363 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
364 #endif
365
366 #undef  TARGET_STRIP_NAME_ENCODING
367 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
368
369 #undef  TARGET_ASM_INTERNAL_LABEL
370 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
371
372 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
373 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
374
375 #undef  TARGET_FUNCTION_VALUE
376 #define TARGET_FUNCTION_VALUE arm_function_value
377
378 #undef  TARGET_LIBCALL_VALUE
379 #define TARGET_LIBCALL_VALUE arm_libcall_value
380
381 #undef  TARGET_ASM_OUTPUT_MI_THUNK
382 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
383 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
384 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
385
386 #undef  TARGET_RTX_COSTS
387 #define TARGET_RTX_COSTS arm_rtx_costs
388 #undef  TARGET_ADDRESS_COST
389 #define TARGET_ADDRESS_COST arm_address_cost
390
391 #undef TARGET_SHIFT_TRUNCATION_MASK
392 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
393 #undef TARGET_VECTOR_MODE_SUPPORTED_P
394 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
395 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
396 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE arm_preferred_simd_mode
397
398 #undef  TARGET_MACHINE_DEPENDENT_REORG
399 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
400
401 #undef  TARGET_INIT_BUILTINS
402 #define TARGET_INIT_BUILTINS  arm_init_builtins
403 #undef  TARGET_EXPAND_BUILTIN
404 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
405
406 #undef TARGET_INIT_LIBFUNCS
407 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
408
409 #undef TARGET_PROMOTE_FUNCTION_MODE
410 #define TARGET_PROMOTE_FUNCTION_MODE arm_promote_function_mode
411 #undef TARGET_PROMOTE_PROTOTYPES
412 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
413 #undef TARGET_PASS_BY_REFERENCE
414 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
415 #undef TARGET_ARG_PARTIAL_BYTES
416 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
417 #undef TARGET_FUNCTION_ARG
418 #define TARGET_FUNCTION_ARG arm_function_arg
419 #undef TARGET_FUNCTION_ARG_ADVANCE
420 #define TARGET_FUNCTION_ARG_ADVANCE arm_function_arg_advance
421 #undef TARGET_FUNCTION_ARG_BOUNDARY
422 #define TARGET_FUNCTION_ARG_BOUNDARY arm_function_arg_boundary
423
424 #undef  TARGET_SETUP_INCOMING_VARARGS
425 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
426
427 #undef TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS
428 #define TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS arm_allocate_stack_slots_for_args
429
430 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
431 #define TARGET_ASM_TRAMPOLINE_TEMPLATE arm_asm_trampoline_template
432 #undef TARGET_TRAMPOLINE_INIT
433 #define TARGET_TRAMPOLINE_INIT arm_trampoline_init
434 #undef TARGET_TRAMPOLINE_ADJUST_ADDRESS
435 #define TARGET_TRAMPOLINE_ADJUST_ADDRESS arm_trampoline_adjust_address
436
437 #undef TARGET_DEFAULT_SHORT_ENUMS
438 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
439
440 #undef TARGET_ALIGN_ANON_BITFIELD
441 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
442
443 #undef TARGET_NARROW_VOLATILE_BITFIELD
444 #define TARGET_NARROW_VOLATILE_BITFIELD hook_bool_void_false
445
446 #undef TARGET_CXX_GUARD_TYPE
447 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
448
449 #undef TARGET_CXX_GUARD_MASK_BIT
450 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
451
452 #undef TARGET_CXX_GET_COOKIE_SIZE
453 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
454
455 #undef TARGET_CXX_COOKIE_HAS_SIZE
456 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
457
458 #undef TARGET_CXX_CDTOR_RETURNS_THIS
459 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
460
461 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
462 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
463
464 #undef TARGET_CXX_USE_AEABI_ATEXIT
465 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
466
467 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
468 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
469   arm_cxx_determine_class_data_visibility
470
471 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
472 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
473
474 #undef TARGET_RETURN_IN_MSB
475 #define TARGET_RETURN_IN_MSB arm_return_in_msb
476
477 #undef TARGET_RETURN_IN_MEMORY
478 #define TARGET_RETURN_IN_MEMORY arm_return_in_memory
479
480 #undef TARGET_MUST_PASS_IN_STACK
481 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
482
483 #if ARM_UNWIND_INFO
484 #undef TARGET_ASM_UNWIND_EMIT
485 #define TARGET_ASM_UNWIND_EMIT arm_unwind_emit
486
487 /* EABI unwinding tables use a different format for the typeinfo tables.  */
488 #undef TARGET_ASM_TTYPE
489 #define TARGET_ASM_TTYPE arm_output_ttype
490
491 #undef TARGET_ARM_EABI_UNWINDER
492 #define TARGET_ARM_EABI_UNWINDER true
493
494 #undef TARGET_ASM_EMIT_EXCEPT_PERSONALITY
495 #define TARGET_ASM_EMIT_EXCEPT_PERSONALITY arm_asm_emit_except_personality
496
497 #undef TARGET_ASM_INIT_SECTIONS
498 #define TARGET_ASM_INIT_SECTIONS arm_asm_init_sections
499 #endif /* ARM_UNWIND_INFO */
500
501 #undef TARGET_EXCEPT_UNWIND_INFO
502 #define TARGET_EXCEPT_UNWIND_INFO  arm_except_unwind_info
503
504 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
505 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC arm_dwarf_handle_frame_unspec
506
507 #undef TARGET_DWARF_REGISTER_SPAN
508 #define TARGET_DWARF_REGISTER_SPAN arm_dwarf_register_span
509
510 #undef  TARGET_CANNOT_COPY_INSN_P
511 #define TARGET_CANNOT_COPY_INSN_P arm_cannot_copy_insn_p
512
513 #ifdef HAVE_AS_TLS
514 #undef TARGET_HAVE_TLS
515 #define TARGET_HAVE_TLS true
516 #endif
517
518 #undef TARGET_HAVE_CONDITIONAL_EXECUTION
519 #define TARGET_HAVE_CONDITIONAL_EXECUTION arm_have_conditional_execution
520
521 #undef TARGET_CANNOT_FORCE_CONST_MEM
522 #define TARGET_CANNOT_FORCE_CONST_MEM arm_cannot_force_const_mem
523
524 #undef TARGET_MAX_ANCHOR_OFFSET
525 #define TARGET_MAX_ANCHOR_OFFSET 4095
526
527 /* The minimum is set such that the total size of the block
528    for a particular anchor is -4088 + 1 + 4095 bytes, which is
529    divisible by eight, ensuring natural spacing of anchors.  */
530 #undef TARGET_MIN_ANCHOR_OFFSET
531 #define TARGET_MIN_ANCHOR_OFFSET -4088
532
533 #undef TARGET_SCHED_ISSUE_RATE
534 #define TARGET_SCHED_ISSUE_RATE arm_issue_rate
535
536 #undef TARGET_MANGLE_TYPE
537 #define TARGET_MANGLE_TYPE arm_mangle_type
538
539 #undef TARGET_BUILD_BUILTIN_VA_LIST
540 #define TARGET_BUILD_BUILTIN_VA_LIST arm_build_builtin_va_list
541 #undef TARGET_EXPAND_BUILTIN_VA_START
542 #define TARGET_EXPAND_BUILTIN_VA_START arm_expand_builtin_va_start
543 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
544 #define TARGET_GIMPLIFY_VA_ARG_EXPR arm_gimplify_va_arg_expr
545
546 #ifdef HAVE_AS_TLS
547 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
548 #define TARGET_ASM_OUTPUT_DWARF_DTPREL arm_output_dwarf_dtprel
549 #endif
550
551 #undef TARGET_LEGITIMATE_ADDRESS_P
552 #define TARGET_LEGITIMATE_ADDRESS_P     arm_legitimate_address_p
553
554 #undef TARGET_INVALID_PARAMETER_TYPE
555 #define TARGET_INVALID_PARAMETER_TYPE arm_invalid_parameter_type
556
557 #undef TARGET_INVALID_RETURN_TYPE
558 #define TARGET_INVALID_RETURN_TYPE arm_invalid_return_type
559
560 #undef TARGET_PROMOTED_TYPE
561 #define TARGET_PROMOTED_TYPE arm_promoted_type
562
563 #undef TARGET_CONVERT_TO_TYPE
564 #define TARGET_CONVERT_TO_TYPE arm_convert_to_type
565
566 #undef TARGET_SCALAR_MODE_SUPPORTED_P
567 #define TARGET_SCALAR_MODE_SUPPORTED_P arm_scalar_mode_supported_p
568
569 #undef TARGET_FRAME_POINTER_REQUIRED
570 #define TARGET_FRAME_POINTER_REQUIRED arm_frame_pointer_required
571
572 #undef TARGET_CAN_ELIMINATE
573 #define TARGET_CAN_ELIMINATE arm_can_eliminate
574
575 #undef TARGET_CONDITIONAL_REGISTER_USAGE
576 #define TARGET_CONDITIONAL_REGISTER_USAGE arm_conditional_register_usage
577
578 #undef TARGET_CLASS_LIKELY_SPILLED_P
579 #define TARGET_CLASS_LIKELY_SPILLED_P arm_class_likely_spilled_p
580
581 #undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
582 #define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE \
583   arm_vector_alignment_reachable
584
585 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
586 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT \
587   arm_builtin_support_vector_misalignment
588
589 #undef TARGET_PREFERRED_RENAME_CLASS
590 #define TARGET_PREFERRED_RENAME_CLASS \
591   arm_preferred_rename_class
592
593 struct gcc_target targetm = TARGET_INITIALIZER;
594 \f
595 /* Obstack for minipool constant handling.  */
596 static struct obstack minipool_obstack;
597 static char *         minipool_startobj;
598
599 /* The maximum number of insns skipped which
600    will be conditionalised if possible.  */
601 static int max_insns_skipped = 5;
602
603 extern FILE * asm_out_file;
604
605 /* True if we are currently building a constant table.  */
606 int making_const_table;
607
608 /* The processor for which instructions should be scheduled.  */
609 enum processor_type arm_tune = arm_none;
610
611 /* The current tuning set.  */
612 const struct tune_params *current_tune;
613
614 /* Which floating point hardware to schedule for.  */
615 int arm_fpu_attr;
616
617 /* Which floating popint hardware to use.  */
618 const struct arm_fpu_desc *arm_fpu_desc;
619
620 /* Whether to use floating point hardware.  */
621 enum float_abi_type arm_float_abi;
622
623 /* Which __fp16 format to use.  */
624 enum arm_fp16_format_type arm_fp16_format;
625
626 /* Which ABI to use.  */
627 enum arm_abi_type arm_abi;
628
629 /* Which thread pointer model to use.  */
630 enum arm_tp_type target_thread_pointer = TP_AUTO;
631
632 /* Used to parse -mstructure_size_boundary command line option.  */
633 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
634
635 /* Used for Thumb call_via trampolines.  */
636 rtx thumb_call_via_label[14];
637 static int thumb_call_reg_needed;
638
639 /* Bit values used to identify processor capabilities.  */
640 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
641 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
642 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
643 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
644 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
645 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
646 #define FL_THUMB      (1 << 6)        /* Thumb aware */
647 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
648 #define FL_STRONG     (1 << 8)        /* StrongARM */
649 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
650 #define FL_XSCALE     (1 << 10)       /* XScale */
651 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
652 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
653                                          media instructions.  */
654 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
655 #define FL_WBUF       (1 << 14)       /* Schedule for write buffer ops.
656                                          Note: ARM6 & 7 derivatives only.  */
657 #define FL_ARCH6K     (1 << 15)       /* Architecture rel 6 K extensions.  */
658 #define FL_THUMB2     (1 << 16)       /* Thumb-2.  */
659 #define FL_NOTM       (1 << 17)       /* Instructions not present in the 'M'
660                                          profile.  */
661 #define FL_DIV        (1 << 18)       /* Hardware divide.  */
662 #define FL_VFPV3      (1 << 19)       /* Vector Floating Point V3.  */
663 #define FL_NEON       (1 << 20)       /* Neon instructions.  */
664 #define FL_ARCH7EM    (1 << 21)       /* Instructions present in the ARMv7E-M
665                                          architecture.  */
666 #define FL_ARCH7      (1 << 22)       /* Architecture 7.  */
667
668 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
669
670 /* Flags that only effect tuning, not available instructions.  */
671 #define FL_TUNE         (FL_WBUF | FL_VFPV2 | FL_STRONG | FL_LDSCHED \
672                          | FL_CO_PROC)
673
674 #define FL_FOR_ARCH2    FL_NOTM
675 #define FL_FOR_ARCH3    (FL_FOR_ARCH2 | FL_MODE32)
676 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
677 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
678 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
679 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
680 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
681 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
682 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
683 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
684 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
685 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
686 #define FL_FOR_ARCH6K   (FL_FOR_ARCH6 | FL_ARCH6K)
687 #define FL_FOR_ARCH6Z   FL_FOR_ARCH6
688 #define FL_FOR_ARCH6ZK  FL_FOR_ARCH6K
689 #define FL_FOR_ARCH6T2  (FL_FOR_ARCH6 | FL_THUMB2)
690 #define FL_FOR_ARCH6M   (FL_FOR_ARCH6 & ~FL_NOTM)
691 #define FL_FOR_ARCH7    ((FL_FOR_ARCH6T2 & ~FL_NOTM) | FL_ARCH7)
692 #define FL_FOR_ARCH7A   (FL_FOR_ARCH7 | FL_NOTM | FL_ARCH6K)
693 #define FL_FOR_ARCH7R   (FL_FOR_ARCH7A | FL_DIV)
694 #define FL_FOR_ARCH7M   (FL_FOR_ARCH7 | FL_DIV)
695 #define FL_FOR_ARCH7EM  (FL_FOR_ARCH7M | FL_ARCH7EM)
696
697 /* The bits in this mask specify which
698    instructions we are allowed to generate.  */
699 static unsigned long insn_flags = 0;
700
701 /* The bits in this mask specify which instruction scheduling options should
702    be used.  */
703 static unsigned long tune_flags = 0;
704
705 /* The following are used in the arm.md file as equivalents to bits
706    in the above two flag variables.  */
707
708 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
709 int arm_arch3m = 0;
710
711 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
712 int arm_arch4 = 0;
713
714 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
715 int arm_arch4t = 0;
716
717 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
718 int arm_arch5 = 0;
719
720 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
721 int arm_arch5e = 0;
722
723 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
724 int arm_arch6 = 0;
725
726 /* Nonzero if this chip supports the ARM 6K extensions.  */
727 int arm_arch6k = 0;
728
729 /* Nonzero if this chip supports the ARM 7 extensions.  */
730 int arm_arch7 = 0;
731
732 /* Nonzero if instructions not present in the 'M' profile can be used.  */
733 int arm_arch_notm = 0;
734
735 /* Nonzero if instructions present in ARMv7E-M can be used.  */
736 int arm_arch7em = 0;
737
738 /* Nonzero if this chip can benefit from load scheduling.  */
739 int arm_ld_sched = 0;
740
741 /* Nonzero if this chip is a StrongARM.  */
742 int arm_tune_strongarm = 0;
743
744 /* Nonzero if this chip is a Cirrus variant.  */
745 int arm_arch_cirrus = 0;
746
747 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
748 int arm_arch_iwmmxt = 0;
749
750 /* Nonzero if this chip is an XScale.  */
751 int arm_arch_xscale = 0;
752
753 /* Nonzero if tuning for XScale  */
754 int arm_tune_xscale = 0;
755
756 /* Nonzero if we want to tune for stores that access the write-buffer.
757    This typically means an ARM6 or ARM7 with MMU or MPU.  */
758 int arm_tune_wbuf = 0;
759
760 /* Nonzero if tuning for Cortex-A9.  */
761 int arm_tune_cortex_a9 = 0;
762
763 /* Nonzero if generating Thumb instructions.  */
764 int thumb_code = 0;
765
766 /* Nonzero if generating Thumb-1 instructions.  */
767 int thumb1_code = 0;
768
769 /* Nonzero if we should define __THUMB_INTERWORK__ in the
770    preprocessor.
771    XXX This is a bit of a hack, it's intended to help work around
772    problems in GLD which doesn't understand that armv5t code is
773    interworking clean.  */
774 int arm_cpp_interwork = 0;
775
776 /* Nonzero if chip supports Thumb 2.  */
777 int arm_arch_thumb2;
778
779 /* Nonzero if chip supports integer division instruction.  */
780 int arm_arch_hwdiv;
781
782 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference,
783    we must report the mode of the memory reference from
784    TARGET_PRINT_OPERAND to TARGET_PRINT_OPERAND_ADDRESS.  */
785 enum machine_mode output_memory_reference_mode;
786
787 /* The register number to be used for the PIC offset register.  */
788 unsigned arm_pic_register = INVALID_REGNUM;
789
790 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
791    the next function.  */
792 static int after_arm_reorg = 0;
793
794 enum arm_pcs arm_pcs_default;
795
796 /* For an explanation of these variables, see final_prescan_insn below.  */
797 int arm_ccfsm_state;
798 /* arm_current_cc is also used for Thumb-2 cond_exec blocks.  */
799 enum arm_cond_code arm_current_cc;
800
801 rtx arm_target_insn;
802 int arm_target_label;
803 /* The number of conditionally executed insns, including the current insn.  */
804 int arm_condexec_count = 0;
805 /* A bitmask specifying the patterns for the IT block.
806    Zero means do not output an IT block before this insn. */
807 int arm_condexec_mask = 0;
808 /* The number of bits used in arm_condexec_mask.  */
809 int arm_condexec_masklen = 0;
810
811 /* The condition codes of the ARM, and the inverse function.  */
812 static const char * const arm_condition_codes[] =
813 {
814   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
815   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
816 };
817
818 /* The register numbers in sequence, for passing to arm_gen_load_multiple.  */
819 int arm_regs_in_sequence[] =
820 {
821   0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
822 };
823
824 #define ARM_LSL_NAME (TARGET_UNIFIED_ASM ? "lsl" : "asl")
825 #define streq(string1, string2) (strcmp (string1, string2) == 0)
826
827 #define THUMB2_WORK_REGS (0xff & ~(  (1 << THUMB_HARD_FRAME_POINTER_REGNUM) \
828                                    | (1 << SP_REGNUM) | (1 << PC_REGNUM) \
829                                    | (1 << PIC_OFFSET_TABLE_REGNUM)))
830 \f
831 /* Initialization code.  */
832
833 struct processors
834 {
835   const char *const name;
836   enum processor_type core;
837   const char *arch;
838   const unsigned long flags;
839   const struct tune_params *const tune;
840 };
841
842
843 #define ARM_PREFETCH_NOT_BENEFICIAL 0, -1, -1
844 #define ARM_PREFETCH_BENEFICIAL(prefetch_slots,l1_size,l1_line_size) \
845   prefetch_slots, \
846   l1_size, \
847   l1_line_size
848
849 const struct tune_params arm_slowmul_tune =
850 {
851   arm_slowmul_rtx_costs,
852   NULL,
853   3,
854   ARM_PREFETCH_NOT_BENEFICIAL
855 };
856
857 const struct tune_params arm_fastmul_tune =
858 {
859   arm_fastmul_rtx_costs,
860   NULL,
861   1,
862   ARM_PREFETCH_NOT_BENEFICIAL
863 };
864
865 const struct tune_params arm_xscale_tune =
866 {
867   arm_xscale_rtx_costs,
868   xscale_sched_adjust_cost,
869   2,
870   ARM_PREFETCH_NOT_BENEFICIAL
871 };
872
873 const struct tune_params arm_9e_tune =
874 {
875   arm_9e_rtx_costs,
876   NULL,
877   1,
878   ARM_PREFETCH_NOT_BENEFICIAL
879 };
880
881 const struct tune_params arm_cortex_a9_tune =
882 {
883   arm_9e_rtx_costs,
884   cortex_a9_sched_adjust_cost,
885   1,
886   ARM_PREFETCH_BENEFICIAL(4,32,32)
887 };
888
889
890 /* Not all of these give usefully different compilation alternatives,
891    but there is no simple way of generalizing them.  */
892 static const struct processors all_cores[] =
893 {
894   /* ARM Cores */
895 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
896   {NAME, IDENT, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, &arm_##COSTS##_tune},
897 #include "arm-cores.def"
898 #undef ARM_CORE
899   {NULL, arm_none, NULL, 0, NULL}
900 };
901
902 static const struct processors all_architectures[] =
903 {
904   /* ARM Architectures */
905   /* We don't specify tuning costs here as it will be figured out
906      from the core.  */
907
908   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
909   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
910   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
911   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
912   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
913   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
914      implementations that support it, so we will leave it out for now.  */
915   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
916   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
917   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
918   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
919   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
920   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
921   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
922   {"armv6k",  mpcore,     "6K",  FL_CO_PROC |             FL_FOR_ARCH6K, NULL},
923   {"armv6z",  arm1176jzs, "6Z",  FL_CO_PROC |             FL_FOR_ARCH6Z, NULL},
924   {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC |             FL_FOR_ARCH6ZK, NULL},
925   {"armv6t2", arm1156t2s, "6T2", FL_CO_PROC |             FL_FOR_ARCH6T2, NULL},
926   {"armv6-m", cortexm1,   "6M",                           FL_FOR_ARCH6M, NULL},
927   {"armv7",   cortexa8,   "7",   FL_CO_PROC |             FL_FOR_ARCH7, NULL},
928   {"armv7-a", cortexa8,   "7A",  FL_CO_PROC |             FL_FOR_ARCH7A, NULL},
929   {"armv7-r", cortexr4,   "7R",  FL_CO_PROC |             FL_FOR_ARCH7R, NULL},
930   {"armv7-m", cortexm3,   "7M",  FL_CO_PROC |             FL_FOR_ARCH7M, NULL},
931   {"armv7e-m", cortexm4,  "7EM", FL_CO_PROC |             FL_FOR_ARCH7EM, NULL},
932   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
933   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
934   {"iwmmxt2", iwmmxt2,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
935   {NULL, arm_none, NULL, 0 , NULL}
936 };
937
938
939 /* These are populated as commandline arguments are processed, or NULL
940    if not specified.  */
941 static const struct processors *arm_selected_arch;
942 static const struct processors *arm_selected_cpu;
943 static const struct processors *arm_selected_tune;
944
945 /* The name of the preprocessor macro to define for this architecture.  */
946
947 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
948
949 /* Available values for -mfpu=.  */
950
951 static const struct arm_fpu_desc all_fpus[] =
952 {
953   {"fpa",               ARM_FP_MODEL_FPA, 0, VFP_NONE, false, false},
954   {"fpe2",              ARM_FP_MODEL_FPA, 2, VFP_NONE, false, false},
955   {"fpe3",              ARM_FP_MODEL_FPA, 3, VFP_NONE, false, false},
956   {"maverick",          ARM_FP_MODEL_MAVERICK, 0, VFP_NONE, false, false},
957   {"vfp",               ARM_FP_MODEL_VFP, 2, VFP_REG_D16, false, false},
958   {"vfpv3",             ARM_FP_MODEL_VFP, 3, VFP_REG_D32, false, false},
959   {"vfpv3-fp16",        ARM_FP_MODEL_VFP, 3, VFP_REG_D32, false, true},
960   {"vfpv3-d16",         ARM_FP_MODEL_VFP, 3, VFP_REG_D16, false, false},
961   {"vfpv3-d16-fp16",    ARM_FP_MODEL_VFP, 3, VFP_REG_D16, false, true},
962   {"vfpv3xd",           ARM_FP_MODEL_VFP, 3, VFP_REG_SINGLE, false, false},
963   {"vfpv3xd-fp16",      ARM_FP_MODEL_VFP, 3, VFP_REG_SINGLE, false, true},
964   {"neon",              ARM_FP_MODEL_VFP, 3, VFP_REG_D32, true , false},
965   {"neon-fp16",         ARM_FP_MODEL_VFP, 3, VFP_REG_D32, true , true },
966   {"vfpv4",             ARM_FP_MODEL_VFP, 4, VFP_REG_D32, false, true},
967   {"vfpv4-d16",         ARM_FP_MODEL_VFP, 4, VFP_REG_D16, false, true},
968   {"fpv4-sp-d16",       ARM_FP_MODEL_VFP, 4, VFP_REG_SINGLE, false, true},
969   {"neon-vfpv4",        ARM_FP_MODEL_VFP, 4, VFP_REG_D32, true, true},
970   /* Compatibility aliases.  */
971   {"vfp3",              ARM_FP_MODEL_VFP, 3, VFP_REG_D32, false, false},
972 };
973
974
975 struct float_abi
976 {
977   const char * name;
978   enum float_abi_type abi_type;
979 };
980
981
982 /* Available values for -mfloat-abi=.  */
983
984 static const struct float_abi all_float_abis[] =
985 {
986   {"soft",      ARM_FLOAT_ABI_SOFT},
987   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
988   {"hard",      ARM_FLOAT_ABI_HARD}
989 };
990
991
992 struct fp16_format
993 {
994   const char *name;
995   enum arm_fp16_format_type fp16_format_type;
996 };
997
998
999 /* Available values for -mfp16-format=.  */
1000
1001 static const struct fp16_format all_fp16_formats[] =
1002 {
1003   {"none",              ARM_FP16_FORMAT_NONE},
1004   {"ieee",              ARM_FP16_FORMAT_IEEE},
1005   {"alternative",       ARM_FP16_FORMAT_ALTERNATIVE}
1006 };
1007
1008
1009 struct abi_name
1010 {
1011   const char *name;
1012   enum arm_abi_type abi_type;
1013 };
1014
1015
1016 /* Available values for -mabi=.  */
1017
1018 static const struct abi_name arm_all_abis[] =
1019 {
1020   {"apcs-gnu",    ARM_ABI_APCS},
1021   {"atpcs",   ARM_ABI_ATPCS},
1022   {"aapcs",   ARM_ABI_AAPCS},
1023   {"iwmmxt",  ARM_ABI_IWMMXT},
1024   {"aapcs-linux",   ARM_ABI_AAPCS_LINUX}
1025 };
1026
1027 /* Supported TLS relocations.  */
1028
1029 enum tls_reloc {
1030   TLS_GD32,
1031   TLS_LDM32,
1032   TLS_LDO32,
1033   TLS_IE32,
1034   TLS_LE32
1035 };
1036
1037 /* The maximum number of insns to be used when loading a constant.  */
1038 inline static int
1039 arm_constant_limit (bool size_p)
1040 {
1041   return size_p ? 1 : current_tune->constant_limit;
1042 }
1043
1044 /* Emit an insn that's a simple single-set.  Both the operands must be known
1045    to be valid.  */
1046 inline static rtx
1047 emit_set_insn (rtx x, rtx y)
1048 {
1049   return emit_insn (gen_rtx_SET (VOIDmode, x, y));
1050 }
1051
1052 /* Return the number of bits set in VALUE.  */
1053 static unsigned
1054 bit_count (unsigned long value)
1055 {
1056   unsigned long count = 0;
1057
1058   while (value)
1059     {
1060       count++;
1061       value &= value - 1;  /* Clear the least-significant set bit.  */
1062     }
1063
1064   return count;
1065 }
1066
1067 /* Set up library functions unique to ARM.  */
1068
1069 static void
1070 arm_init_libfuncs (void)
1071 {
1072   /* There are no special library functions unless we are using the
1073      ARM BPABI.  */
1074   if (!TARGET_BPABI)
1075     return;
1076
1077   /* The functions below are described in Section 4 of the "Run-Time
1078      ABI for the ARM architecture", Version 1.0.  */
1079
1080   /* Double-precision floating-point arithmetic.  Table 2.  */
1081   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
1082   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
1083   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
1084   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
1085   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
1086
1087   /* Double-precision comparisons.  Table 3.  */
1088   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
1089   set_optab_libfunc (ne_optab, DFmode, NULL);
1090   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
1091   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
1092   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
1093   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
1094   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
1095
1096   /* Single-precision floating-point arithmetic.  Table 4.  */
1097   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
1098   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
1099   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
1100   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
1101   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
1102
1103   /* Single-precision comparisons.  Table 5.  */
1104   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
1105   set_optab_libfunc (ne_optab, SFmode, NULL);
1106   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
1107   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
1108   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
1109   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
1110   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
1111
1112   /* Floating-point to integer conversions.  Table 6.  */
1113   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
1114   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
1115   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
1116   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
1117   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
1118   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
1119   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
1120   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
1121
1122   /* Conversions between floating types.  Table 7.  */
1123   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
1124   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
1125
1126   /* Integer to floating-point conversions.  Table 8.  */
1127   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
1128   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
1129   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
1130   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
1131   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
1132   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
1133   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
1134   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
1135
1136   /* Long long.  Table 9.  */
1137   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
1138   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
1139   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
1140   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
1141   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
1142   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
1143   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
1144   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
1145
1146   /* Integer (32/32->32) division.  \S 4.3.1.  */
1147   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
1148   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
1149
1150   /* The divmod functions are designed so that they can be used for
1151      plain division, even though they return both the quotient and the
1152      remainder.  The quotient is returned in the usual location (i.e.,
1153      r0 for SImode, {r0, r1} for DImode), just as would be expected
1154      for an ordinary division routine.  Because the AAPCS calling
1155      conventions specify that all of { r0, r1, r2, r3 } are
1156      callee-saved registers, there is no need to tell the compiler
1157      explicitly that those registers are clobbered by these
1158      routines.  */
1159   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
1160   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
1161
1162   /* For SImode division the ABI provides div-without-mod routines,
1163      which are faster.  */
1164   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
1165   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
1166
1167   /* We don't have mod libcalls.  Fortunately gcc knows how to use the
1168      divmod libcalls instead.  */
1169   set_optab_libfunc (smod_optab, DImode, NULL);
1170   set_optab_libfunc (umod_optab, DImode, NULL);
1171   set_optab_libfunc (smod_optab, SImode, NULL);
1172   set_optab_libfunc (umod_optab, SImode, NULL);
1173
1174   /* Half-precision float operations.  The compiler handles all operations
1175      with NULL libfuncs by converting the SFmode.  */
1176   switch (arm_fp16_format)
1177     {
1178     case ARM_FP16_FORMAT_IEEE:
1179     case ARM_FP16_FORMAT_ALTERNATIVE:
1180
1181       /* Conversions.  */
1182       set_conv_libfunc (trunc_optab, HFmode, SFmode,
1183                         (arm_fp16_format == ARM_FP16_FORMAT_IEEE
1184                          ? "__gnu_f2h_ieee"
1185                          : "__gnu_f2h_alternative"));
1186       set_conv_libfunc (sext_optab, SFmode, HFmode, 
1187                         (arm_fp16_format == ARM_FP16_FORMAT_IEEE
1188                          ? "__gnu_h2f_ieee"
1189                          : "__gnu_h2f_alternative"));
1190       
1191       /* Arithmetic.  */
1192       set_optab_libfunc (add_optab, HFmode, NULL);
1193       set_optab_libfunc (sdiv_optab, HFmode, NULL);
1194       set_optab_libfunc (smul_optab, HFmode, NULL);
1195       set_optab_libfunc (neg_optab, HFmode, NULL);
1196       set_optab_libfunc (sub_optab, HFmode, NULL);
1197
1198       /* Comparisons.  */
1199       set_optab_libfunc (eq_optab, HFmode, NULL);
1200       set_optab_libfunc (ne_optab, HFmode, NULL);
1201       set_optab_libfunc (lt_optab, HFmode, NULL);
1202       set_optab_libfunc (le_optab, HFmode, NULL);
1203       set_optab_libfunc (ge_optab, HFmode, NULL);
1204       set_optab_libfunc (gt_optab, HFmode, NULL);
1205       set_optab_libfunc (unord_optab, HFmode, NULL);
1206       break;
1207
1208     default:
1209       break;
1210     }
1211
1212   if (TARGET_AAPCS_BASED)
1213     synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
1214 }
1215
1216 /* On AAPCS systems, this is the "struct __va_list".  */
1217 static GTY(()) tree va_list_type;
1218
1219 /* Return the type to use as __builtin_va_list.  */
1220 static tree
1221 arm_build_builtin_va_list (void)
1222 {
1223   tree va_list_name;
1224   tree ap_field;
1225   
1226   if (!TARGET_AAPCS_BASED)
1227     return std_build_builtin_va_list ();
1228
1229   /* AAPCS \S 7.1.4 requires that va_list be a typedef for a type
1230      defined as:
1231
1232        struct __va_list 
1233        {
1234          void *__ap;
1235        };
1236
1237      The C Library ABI further reinforces this definition in \S
1238      4.1.
1239
1240      We must follow this definition exactly.  The structure tag
1241      name is visible in C++ mangled names, and thus forms a part
1242      of the ABI.  The field name may be used by people who
1243      #include <stdarg.h>.  */
1244   /* Create the type.  */
1245   va_list_type = lang_hooks.types.make_type (RECORD_TYPE);
1246   /* Give it the required name.  */
1247   va_list_name = build_decl (BUILTINS_LOCATION,
1248                              TYPE_DECL,
1249                              get_identifier ("__va_list"),
1250                              va_list_type);
1251   DECL_ARTIFICIAL (va_list_name) = 1;
1252   TYPE_NAME (va_list_type) = va_list_name;
1253   TYPE_STUB_DECL (va_list_type) = va_list_name;
1254   /* Create the __ap field.  */
1255   ap_field = build_decl (BUILTINS_LOCATION,
1256                          FIELD_DECL, 
1257                          get_identifier ("__ap"),
1258                          ptr_type_node);
1259   DECL_ARTIFICIAL (ap_field) = 1;
1260   DECL_FIELD_CONTEXT (ap_field) = va_list_type;
1261   TYPE_FIELDS (va_list_type) = ap_field;
1262   /* Compute its layout.  */
1263   layout_type (va_list_type);
1264
1265   return va_list_type;
1266 }
1267
1268 /* Return an expression of type "void *" pointing to the next
1269    available argument in a variable-argument list.  VALIST is the
1270    user-level va_list object, of type __builtin_va_list.  */
1271 static tree
1272 arm_extract_valist_ptr (tree valist)
1273 {
1274   if (TREE_TYPE (valist) == error_mark_node)
1275     return error_mark_node;
1276
1277   /* On an AAPCS target, the pointer is stored within "struct
1278      va_list".  */
1279   if (TARGET_AAPCS_BASED)
1280     {
1281       tree ap_field = TYPE_FIELDS (TREE_TYPE (valist));
1282       valist = build3 (COMPONENT_REF, TREE_TYPE (ap_field), 
1283                        valist, ap_field, NULL_TREE);
1284     }
1285
1286   return valist;
1287 }
1288
1289 /* Implement TARGET_EXPAND_BUILTIN_VA_START.  */
1290 static void
1291 arm_expand_builtin_va_start (tree valist, rtx nextarg)
1292 {
1293   valist = arm_extract_valist_ptr (valist);
1294   std_expand_builtin_va_start (valist, nextarg);
1295 }
1296
1297 /* Implement TARGET_GIMPLIFY_VA_ARG_EXPR.  */
1298 static tree
1299 arm_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p, 
1300                           gimple_seq *post_p)
1301 {
1302   valist = arm_extract_valist_ptr (valist);
1303   return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
1304 }
1305
1306 /* Lookup NAME in SEL.  */
1307
1308 static const struct processors *
1309 arm_find_cpu (const char *name, const struct processors *sel, const char *desc)
1310 {
1311   if (!(name && *name))
1312     return NULL;
1313
1314   for (; sel->name != NULL; sel++)
1315     {
1316       if (streq (name, sel->name))
1317         return sel;
1318     }
1319
1320   error ("bad value (%s) for %s switch", name, desc);
1321   return NULL;
1322 }
1323
1324 /* Implement TARGET_HANDLE_OPTION.  */
1325
1326 static bool
1327 arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
1328 {
1329   switch (code)
1330     {
1331     case OPT_march_:
1332       arm_selected_arch = arm_find_cpu(arg, all_architectures, "-march");
1333       return true;
1334
1335     case OPT_mcpu_:
1336       arm_selected_cpu = arm_find_cpu(arg, all_cores, "-mcpu");
1337       return true;
1338
1339     case OPT_mhard_float:
1340       target_float_abi_name = "hard";
1341       return true;
1342
1343     case OPT_msoft_float:
1344       target_float_abi_name = "soft";
1345       return true;
1346
1347     case OPT_mtune_:
1348       arm_selected_tune = arm_find_cpu(arg, all_cores, "-mtune");
1349       return true;
1350
1351     default:
1352       return true;
1353     }
1354 }
1355
1356 static void
1357 arm_target_help (void)
1358 {
1359   int i;
1360   static int columns = 0;
1361   int remaining;
1362
1363   /* If we have not done so already, obtain the desired maximum width of
1364      the output.  Note - this is a duplication of the code at the start of
1365      gcc/opts.c:print_specific_help() - the two copies should probably be
1366      replaced by a single function.  */
1367   if (columns == 0)
1368     {
1369       const char *p;
1370
1371       p = getenv ("COLUMNS");
1372       if (p != NULL)
1373         {
1374           int value = atoi (p);
1375
1376           if (value > 0)
1377             columns = value;
1378         }
1379
1380       if (columns == 0)
1381         /* Use a reasonable default.  */
1382         columns = 80;
1383     }
1384
1385   printf ("  Known ARM CPUs (for use with the -mcpu= and -mtune= options):\n");
1386
1387   /* The - 2 is because we know that the last entry in the array is NULL.  */
1388   i = ARRAY_SIZE (all_cores) - 2;
1389   gcc_assert (i > 0);
1390   printf ("    %s", all_cores[i].name);
1391   remaining = columns - (strlen (all_cores[i].name) + 4);
1392   gcc_assert (remaining >= 0);
1393
1394   while (i--)
1395     {
1396       int len = strlen (all_cores[i].name);
1397
1398       if (remaining > len + 2)
1399         {
1400           printf (", %s", all_cores[i].name);
1401           remaining -= len + 2;
1402         }
1403       else
1404         {
1405           if (remaining > 0)
1406             printf (",");
1407           printf ("\n    %s", all_cores[i].name);
1408           remaining = columns - (len + 4);
1409         }
1410     }
1411
1412   printf ("\n\n  Known ARM architectures (for use with the -march= option):\n");
1413
1414   i = ARRAY_SIZE (all_architectures) - 2;
1415   gcc_assert (i > 0);
1416   
1417   printf ("    %s", all_architectures[i].name);
1418   remaining = columns - (strlen (all_architectures[i].name) + 4);
1419   gcc_assert (remaining >= 0);
1420
1421   while (i--)
1422     {
1423       int len = strlen (all_architectures[i].name);
1424
1425       if (remaining > len + 2)
1426         {
1427           printf (", %s", all_architectures[i].name);
1428           remaining -= len + 2;
1429         }
1430       else
1431         {
1432           if (remaining > 0)
1433             printf (",");
1434           printf ("\n    %s", all_architectures[i].name);
1435           remaining = columns - (len + 4);
1436         }
1437     }
1438   printf ("\n");
1439
1440 }
1441
1442 /* Fix up any incompatible options that the user has specified.  */
1443 static void
1444 arm_option_override (void)
1445 {
1446   unsigned i;
1447
1448 #ifdef SUBTARGET_OVERRIDE_OPTIONS
1449   SUBTARGET_OVERRIDE_OPTIONS;
1450 #endif
1451
1452   if (arm_selected_arch)
1453     {
1454       if (arm_selected_cpu)
1455         {
1456           /* Check for conflict between mcpu and march.  */
1457           if ((arm_selected_cpu->flags ^ arm_selected_arch->flags) & ~FL_TUNE)
1458             {
1459               warning (0, "switch -mcpu=%s conflicts with -march=%s switch",
1460                        arm_selected_cpu->name, arm_selected_arch->name);
1461               /* -march wins for code generation.
1462                  -mcpu wins for default tuning.  */
1463               if (!arm_selected_tune)
1464                 arm_selected_tune = arm_selected_cpu;
1465
1466               arm_selected_cpu = arm_selected_arch;
1467             }
1468           else
1469             /* -mcpu wins.  */
1470             arm_selected_arch = NULL;
1471         }
1472       else
1473         /* Pick a CPU based on the architecture.  */
1474         arm_selected_cpu = arm_selected_arch;
1475     }
1476
1477   /* If the user did not specify a processor, choose one for them.  */
1478   if (!arm_selected_cpu)
1479     {
1480       const struct processors * sel;
1481       unsigned int        sought;
1482
1483       arm_selected_cpu = &all_cores[TARGET_CPU_DEFAULT];
1484       if (!arm_selected_cpu->name)
1485         {
1486 #ifdef SUBTARGET_CPU_DEFAULT
1487           /* Use the subtarget default CPU if none was specified by
1488              configure.  */
1489           arm_selected_cpu = &all_cores[SUBTARGET_CPU_DEFAULT];
1490 #endif
1491           /* Default to ARM6.  */
1492           if (!arm_selected_cpu->name)
1493             arm_selected_cpu = &all_cores[arm6];
1494         }
1495
1496       sel = arm_selected_cpu;
1497       insn_flags = sel->flags;
1498
1499       /* Now check to see if the user has specified some command line
1500          switch that require certain abilities from the cpu.  */
1501       sought = 0;
1502
1503       if (TARGET_INTERWORK || TARGET_THUMB)
1504         {
1505           sought |= (FL_THUMB | FL_MODE32);
1506
1507           /* There are no ARM processors that support both APCS-26 and
1508              interworking.  Therefore we force FL_MODE26 to be removed
1509              from insn_flags here (if it was set), so that the search
1510              below will always be able to find a compatible processor.  */
1511           insn_flags &= ~FL_MODE26;
1512         }
1513
1514       if (sought != 0 && ((sought & insn_flags) != sought))
1515         {
1516           /* Try to locate a CPU type that supports all of the abilities
1517              of the default CPU, plus the extra abilities requested by
1518              the user.  */
1519           for (sel = all_cores; sel->name != NULL; sel++)
1520             if ((sel->flags & sought) == (sought | insn_flags))
1521               break;
1522
1523           if (sel->name == NULL)
1524             {
1525               unsigned current_bit_count = 0;
1526               const struct processors * best_fit = NULL;
1527
1528               /* Ideally we would like to issue an error message here
1529                  saying that it was not possible to find a CPU compatible
1530                  with the default CPU, but which also supports the command
1531                  line options specified by the programmer, and so they
1532                  ought to use the -mcpu=<name> command line option to
1533                  override the default CPU type.
1534
1535                  If we cannot find a cpu that has both the
1536                  characteristics of the default cpu and the given
1537                  command line options we scan the array again looking
1538                  for a best match.  */
1539               for (sel = all_cores; sel->name != NULL; sel++)
1540                 if ((sel->flags & sought) == sought)
1541                   {
1542                     unsigned count;
1543
1544                     count = bit_count (sel->flags & insn_flags);
1545
1546                     if (count >= current_bit_count)
1547                       {
1548                         best_fit = sel;
1549                         current_bit_count = count;
1550                       }
1551                   }
1552
1553               gcc_assert (best_fit);
1554               sel = best_fit;
1555             }
1556
1557           arm_selected_cpu = sel;
1558         }
1559     }
1560
1561   gcc_assert (arm_selected_cpu);
1562   /* The selected cpu may be an architecture, so lookup tuning by core ID.  */
1563   if (!arm_selected_tune)
1564     arm_selected_tune = &all_cores[arm_selected_cpu->core];
1565
1566   sprintf (arm_arch_name, "__ARM_ARCH_%s__", arm_selected_cpu->arch);
1567   insn_flags = arm_selected_cpu->flags;
1568
1569   arm_tune = arm_selected_tune->core;
1570   tune_flags = arm_selected_tune->flags;
1571   current_tune = arm_selected_tune->tune;
1572
1573   if (target_fp16_format_name)
1574     {
1575       for (i = 0; i < ARRAY_SIZE (all_fp16_formats); i++)
1576         {
1577           if (streq (all_fp16_formats[i].name, target_fp16_format_name))
1578             {
1579               arm_fp16_format = all_fp16_formats[i].fp16_format_type;
1580               break;
1581             }
1582         }
1583       if (i == ARRAY_SIZE (all_fp16_formats))
1584         error ("invalid __fp16 format option: -mfp16-format=%s",
1585                target_fp16_format_name);
1586     }
1587   else
1588     arm_fp16_format = ARM_FP16_FORMAT_NONE;
1589
1590   if (target_abi_name)
1591     {
1592       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1593         {
1594           if (streq (arm_all_abis[i].name, target_abi_name))
1595             {
1596               arm_abi = arm_all_abis[i].abi_type;
1597               break;
1598             }
1599         }
1600       if (i == ARRAY_SIZE (arm_all_abis))
1601         error ("invalid ABI option: -mabi=%s", target_abi_name);
1602     }
1603   else
1604     arm_abi = ARM_DEFAULT_ABI;
1605
1606   /* Make sure that the processor choice does not conflict with any of the
1607      other command line choices.  */
1608   if (TARGET_ARM && !(insn_flags & FL_NOTM))
1609     error ("target CPU does not support ARM mode");
1610
1611   /* BPABI targets use linker tricks to allow interworking on cores
1612      without thumb support.  */
1613   if (TARGET_INTERWORK && !((insn_flags & FL_THUMB) || TARGET_BPABI))
1614     {
1615       warning (0, "target CPU does not support interworking" );
1616       target_flags &= ~MASK_INTERWORK;
1617     }
1618
1619   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1620     {
1621       warning (0, "target CPU does not support THUMB instructions");
1622       target_flags &= ~MASK_THUMB;
1623     }
1624
1625   if (TARGET_APCS_FRAME && TARGET_THUMB)
1626     {
1627       /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1628       target_flags &= ~MASK_APCS_FRAME;
1629     }
1630
1631   /* Callee super interworking implies thumb interworking.  Adding
1632      this to the flags here simplifies the logic elsewhere.  */
1633   if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
1634     target_flags |= MASK_INTERWORK;
1635
1636   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1637      from here where no function is being compiled currently.  */
1638   if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1639     warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1640
1641   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1642     warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1643
1644   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1645     {
1646       warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1647       target_flags |= MASK_APCS_FRAME;
1648     }
1649
1650   if (TARGET_POKE_FUNCTION_NAME)
1651     target_flags |= MASK_APCS_FRAME;
1652
1653   if (TARGET_APCS_REENT && flag_pic)
1654     error ("-fpic and -mapcs-reent are incompatible");
1655
1656   if (TARGET_APCS_REENT)
1657     warning (0, "APCS reentrant code not supported.  Ignored");
1658
1659   /* If this target is normally configured to use APCS frames, warn if they
1660      are turned off and debugging is turned on.  */
1661   if (TARGET_ARM
1662       && write_symbols != NO_DEBUG
1663       && !TARGET_APCS_FRAME
1664       && (TARGET_DEFAULT & MASK_APCS_FRAME))
1665     warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1666
1667   if (TARGET_APCS_FLOAT)
1668     warning (0, "passing floating point arguments in fp regs not yet supported");
1669
1670   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
1671   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1672   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1673   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1674   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1675   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1676   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1677   arm_arch6k = (insn_flags & FL_ARCH6K) != 0;
1678   arm_arch_notm = (insn_flags & FL_NOTM) != 0;
1679   arm_arch7 = (insn_flags & FL_ARCH7) != 0;
1680   arm_arch7em = (insn_flags & FL_ARCH7EM) != 0;
1681   arm_arch_thumb2 = (insn_flags & FL_THUMB2) != 0;
1682   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1683   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1684
1685   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1686   arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1687   thumb_code = TARGET_ARM == 0;
1688   thumb1_code = TARGET_THUMB1 != 0;
1689   arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1690   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1691   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1692   arm_arch_hwdiv = (insn_flags & FL_DIV) != 0;
1693   arm_tune_cortex_a9 = (arm_tune == cortexa9) != 0;
1694
1695   /* If we are not using the default (ARM mode) section anchor offset
1696      ranges, then set the correct ranges now.  */
1697   if (TARGET_THUMB1)
1698     {
1699       /* Thumb-1 LDR instructions cannot have negative offsets.
1700          Permissible positive offset ranges are 5-bit (for byte loads),
1701          6-bit (for halfword loads), or 7-bit (for word loads).
1702          Empirical results suggest a 7-bit anchor range gives the best
1703          overall code size.  */
1704       targetm.min_anchor_offset = 0;
1705       targetm.max_anchor_offset = 127;
1706     }
1707   else if (TARGET_THUMB2)
1708     {
1709       /* The minimum is set such that the total size of the block
1710          for a particular anchor is 248 + 1 + 4095 bytes, which is
1711          divisible by eight, ensuring natural spacing of anchors.  */
1712       targetm.min_anchor_offset = -248;
1713       targetm.max_anchor_offset = 4095;
1714     }
1715
1716   /* V5 code we generate is completely interworking capable, so we turn off
1717      TARGET_INTERWORK here to avoid many tests later on.  */
1718
1719   /* XXX However, we must pass the right pre-processor defines to CPP
1720      or GLD can get confused.  This is a hack.  */
1721   if (TARGET_INTERWORK)
1722     arm_cpp_interwork = 1;
1723
1724   if (arm_arch5)
1725     target_flags &= ~MASK_INTERWORK;
1726
1727   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1728     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1729
1730   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1731     error ("iwmmxt abi requires an iwmmxt capable cpu");
1732
1733   if (target_fpu_name == NULL && target_fpe_name != NULL)
1734     {
1735       if (streq (target_fpe_name, "2"))
1736         target_fpu_name = "fpe2";
1737       else if (streq (target_fpe_name, "3"))
1738         target_fpu_name = "fpe3";
1739       else
1740         error ("invalid floating point emulation option: -mfpe=%s",
1741                target_fpe_name);
1742     }
1743
1744   if (target_fpu_name == NULL)
1745     {
1746 #ifdef FPUTYPE_DEFAULT
1747       target_fpu_name = FPUTYPE_DEFAULT;
1748 #else
1749       if (arm_arch_cirrus)
1750         target_fpu_name = "maverick";
1751       else
1752         target_fpu_name = "fpe2";
1753 #endif
1754     }
1755
1756   arm_fpu_desc = NULL;
1757   for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1758     {
1759       if (streq (all_fpus[i].name, target_fpu_name))
1760         {
1761           arm_fpu_desc = &all_fpus[i];
1762           break;
1763         }
1764     }
1765
1766   if (!arm_fpu_desc)
1767     {
1768       error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1769       return;
1770     }
1771
1772   switch (arm_fpu_desc->model)
1773     {
1774     case ARM_FP_MODEL_FPA:
1775       if (arm_fpu_desc->rev == 2)
1776         arm_fpu_attr = FPU_FPE2;
1777       else if (arm_fpu_desc->rev == 3)
1778         arm_fpu_attr = FPU_FPE3;
1779       else
1780         arm_fpu_attr = FPU_FPA;
1781       break;
1782
1783     case ARM_FP_MODEL_MAVERICK:
1784       arm_fpu_attr = FPU_MAVERICK;
1785       break;
1786
1787     case ARM_FP_MODEL_VFP:
1788       arm_fpu_attr = FPU_VFP;
1789       break;
1790
1791     default:
1792       gcc_unreachable();
1793     }
1794
1795   if (target_float_abi_name != NULL)
1796     {
1797       /* The user specified a FP ABI.  */
1798       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1799         {
1800           if (streq (all_float_abis[i].name, target_float_abi_name))
1801             {
1802               arm_float_abi = all_float_abis[i].abi_type;
1803               break;
1804             }
1805         }
1806       if (i == ARRAY_SIZE (all_float_abis))
1807         error ("invalid floating point abi: -mfloat-abi=%s",
1808                target_float_abi_name);
1809     }
1810   else
1811     arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1812
1813   if (TARGET_AAPCS_BASED
1814       && (arm_fpu_desc->model == ARM_FP_MODEL_FPA))
1815     error ("FPA is unsupported in the AAPCS");
1816
1817   if (TARGET_AAPCS_BASED)
1818     {
1819       if (TARGET_CALLER_INTERWORKING)
1820         error ("AAPCS does not support -mcaller-super-interworking");
1821       else
1822         if (TARGET_CALLEE_INTERWORKING)
1823           error ("AAPCS does not support -mcallee-super-interworking");
1824     }
1825
1826   /* FPA and iWMMXt are incompatible because the insn encodings overlap.
1827      VFP and iWMMXt can theoretically coexist, but it's unlikely such silicon
1828      will ever exist.  GCC makes no attempt to support this combination.  */
1829   if (TARGET_IWMMXT && !TARGET_SOFT_FLOAT)
1830     sorry ("iWMMXt and hardware floating point");
1831
1832   /* ??? iWMMXt insn patterns need auditing for Thumb-2.  */
1833   if (TARGET_THUMB2 && TARGET_IWMMXT)
1834     sorry ("Thumb-2 iWMMXt");
1835
1836   /* __fp16 support currently assumes the core has ldrh.  */
1837   if (!arm_arch4 && arm_fp16_format != ARM_FP16_FORMAT_NONE)
1838     sorry ("__fp16 and no ldrh");
1839
1840   /* If soft-float is specified then don't use FPU.  */
1841   if (TARGET_SOFT_FLOAT)
1842     arm_fpu_attr = FPU_NONE;
1843
1844   if (TARGET_AAPCS_BASED)
1845     {
1846       if (arm_abi == ARM_ABI_IWMMXT)
1847         arm_pcs_default = ARM_PCS_AAPCS_IWMMXT;
1848       else if (arm_float_abi == ARM_FLOAT_ABI_HARD
1849                && TARGET_HARD_FLOAT
1850                && TARGET_VFP)
1851         arm_pcs_default = ARM_PCS_AAPCS_VFP;
1852       else
1853         arm_pcs_default = ARM_PCS_AAPCS;
1854     }
1855   else
1856     {
1857       if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1858         sorry ("-mfloat-abi=hard and VFP");
1859
1860       if (arm_abi == ARM_ABI_APCS)
1861         arm_pcs_default = ARM_PCS_APCS;
1862       else
1863         arm_pcs_default = ARM_PCS_ATPCS;
1864     }
1865
1866   /* For arm2/3 there is no need to do any scheduling if there is only
1867      a floating point emulator, or we are doing software floating-point.  */
1868   if ((TARGET_SOFT_FLOAT
1869        || (TARGET_FPA && arm_fpu_desc->rev))
1870       && (tune_flags & FL_MODE32) == 0)
1871     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1872
1873   if (target_thread_switch)
1874     {
1875       if (strcmp (target_thread_switch, "soft") == 0)
1876         target_thread_pointer = TP_SOFT;
1877       else if (strcmp (target_thread_switch, "auto") == 0)
1878         target_thread_pointer = TP_AUTO;
1879       else if (strcmp (target_thread_switch, "cp15") == 0)
1880         target_thread_pointer = TP_CP15;
1881       else
1882         error ("invalid thread pointer option: -mtp=%s", target_thread_switch);
1883     }
1884
1885   /* Use the cp15 method if it is available.  */
1886   if (target_thread_pointer == TP_AUTO)
1887     {
1888       if (arm_arch6k && !TARGET_THUMB1)
1889         target_thread_pointer = TP_CP15;
1890       else
1891         target_thread_pointer = TP_SOFT;
1892     }
1893
1894   if (TARGET_HARD_TP && TARGET_THUMB1)
1895     error ("can not use -mtp=cp15 with 16-bit Thumb");
1896
1897   /* Override the default structure alignment for AAPCS ABI.  */
1898   if (TARGET_AAPCS_BASED)
1899     arm_structure_size_boundary = 8;
1900
1901   if (structure_size_string != NULL)
1902     {
1903       int size = strtol (structure_size_string, NULL, 0);
1904
1905       if (size == 8 || size == 32
1906           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1907         arm_structure_size_boundary = size;
1908       else
1909         warning (0, "structure size boundary can only be set to %s",
1910                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1911     }
1912
1913   if (!TARGET_ARM && TARGET_VXWORKS_RTP && flag_pic)
1914     {
1915       error ("RTP PIC is incompatible with Thumb");
1916       flag_pic = 0;
1917     }
1918
1919   /* If stack checking is disabled, we can use r10 as the PIC register,
1920      which keeps r9 available.  The EABI specifies r9 as the PIC register.  */
1921   if (flag_pic && TARGET_SINGLE_PIC_BASE)
1922     {
1923       if (TARGET_VXWORKS_RTP)
1924         warning (0, "RTP PIC is incompatible with -msingle-pic-base");
1925       arm_pic_register = (TARGET_APCS_STACK || TARGET_AAPCS_BASED) ? 9 : 10;
1926     }
1927
1928   if (flag_pic && TARGET_VXWORKS_RTP)
1929     arm_pic_register = 9;
1930
1931   if (arm_pic_register_string != NULL)
1932     {
1933       int pic_register = decode_reg_name (arm_pic_register_string);
1934
1935       if (!flag_pic)
1936         warning (0, "-mpic-register= is useless without -fpic");
1937
1938       /* Prevent the user from choosing an obviously stupid PIC register.  */
1939       else if (pic_register < 0 || call_used_regs[pic_register]
1940                || pic_register == HARD_FRAME_POINTER_REGNUM
1941                || pic_register == STACK_POINTER_REGNUM
1942                || pic_register >= PC_REGNUM
1943                || (TARGET_VXWORKS_RTP
1944                    && (unsigned int) pic_register != arm_pic_register))
1945         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1946       else
1947         arm_pic_register = pic_register;
1948     }
1949
1950   /* Enable -mfix-cortex-m3-ldrd by default for Cortex-M3 cores.  */
1951   if (fix_cm3_ldrd == 2)
1952     {
1953       if (arm_selected_cpu->core == cortexm3)
1954         fix_cm3_ldrd = 1;
1955       else
1956         fix_cm3_ldrd = 0;
1957     }
1958
1959   if (TARGET_THUMB1 && flag_schedule_insns)
1960     {
1961       /* Don't warn since it's on by default in -O2.  */
1962       flag_schedule_insns = 0;
1963     }
1964
1965   if (optimize_size)
1966     {
1967       /* If optimizing for size, bump the number of instructions that we
1968          are prepared to conditionally execute (even on a StrongARM).  */
1969       max_insns_skipped = 6;
1970     }
1971   else
1972     {
1973       /* StrongARM has early execution of branches, so a sequence
1974          that is worth skipping is shorter.  */
1975       if (arm_tune_strongarm)
1976         max_insns_skipped = 3;
1977     }
1978
1979   /* Hot/Cold partitioning is not currently supported, since we can't
1980      handle literal pool placement in that case.  */
1981   if (flag_reorder_blocks_and_partition)
1982     {
1983       inform (input_location,
1984               "-freorder-blocks-and-partition not supported on this architecture");
1985       flag_reorder_blocks_and_partition = 0;
1986       flag_reorder_blocks = 1;
1987     }
1988
1989   if (flag_pic)
1990     /* Hoisting PIC address calculations more aggressively provides a small,
1991        but measurable, size reduction for PIC code.  Therefore, we decrease
1992        the bar for unrestricted expression hoisting to the cost of PIC address
1993        calculation, which is 2 instructions.  */
1994     maybe_set_param_value (PARAM_GCSE_UNRESTRICTED_COST, 2,
1995                            global_options.x_param_values,
1996                            global_options_set.x_param_values);
1997
1998   /* ARM EABI defaults to strict volatile bitfields.  */
1999   if (TARGET_AAPCS_BASED && flag_strict_volatile_bitfields < 0)
2000     flag_strict_volatile_bitfields = 1;
2001
2002   /* Enable sw prefetching at -O3 for CPUS that have prefetch, and we have deemed
2003      it beneficial (signified by setting num_prefetch_slots to 1 or more.)  */
2004   if (flag_prefetch_loop_arrays < 0
2005       && HAVE_prefetch
2006       && optimize >= 3
2007       && current_tune->num_prefetch_slots > 0)
2008     flag_prefetch_loop_arrays = 1;
2009
2010   /* Set up parameters to be used in prefetching algorithm.  Do not override the
2011      defaults unless we are tuning for a core we have researched values for.  */
2012   if (current_tune->num_prefetch_slots > 0)
2013     maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
2014                            current_tune->num_prefetch_slots,
2015                            global_options.x_param_values,
2016                            global_options_set.x_param_values);
2017   if (current_tune->l1_cache_line_size >= 0)
2018     maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
2019                            current_tune->l1_cache_line_size,
2020                            global_options.x_param_values,
2021                            global_options_set.x_param_values);
2022   if (current_tune->l1_cache_size >= 0)
2023     maybe_set_param_value (PARAM_L1_CACHE_SIZE,
2024                            current_tune->l1_cache_size,
2025                            global_options.x_param_values,
2026                            global_options_set.x_param_values);
2027
2028   /* Register global variables with the garbage collector.  */
2029   arm_add_gc_roots ();
2030 }
2031
2032 static void
2033 arm_add_gc_roots (void)
2034 {
2035   gcc_obstack_init(&minipool_obstack);
2036   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
2037 }
2038 \f
2039 /* A table of known ARM exception types.
2040    For use with the interrupt function attribute.  */
2041
2042 typedef struct
2043 {
2044   const char *const arg;
2045   const unsigned long return_value;
2046 }
2047 isr_attribute_arg;
2048
2049 static const isr_attribute_arg isr_attribute_args [] =
2050 {
2051   { "IRQ",   ARM_FT_ISR },
2052   { "irq",   ARM_FT_ISR },
2053   { "FIQ",   ARM_FT_FIQ },
2054   { "fiq",   ARM_FT_FIQ },
2055   { "ABORT", ARM_FT_ISR },
2056   { "abort", ARM_FT_ISR },
2057   { "ABORT", ARM_FT_ISR },
2058   { "abort", ARM_FT_ISR },
2059   { "UNDEF", ARM_FT_EXCEPTION },
2060   { "undef", ARM_FT_EXCEPTION },
2061   { "SWI",   ARM_FT_EXCEPTION },
2062   { "swi",   ARM_FT_EXCEPTION },
2063   { NULL,    ARM_FT_NORMAL }
2064 };
2065
2066 /* Returns the (interrupt) function type of the current
2067    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
2068
2069 static unsigned long
2070 arm_isr_value (tree argument)
2071 {
2072   const isr_attribute_arg * ptr;
2073   const char *              arg;
2074
2075   if (!arm_arch_notm)
2076     return ARM_FT_NORMAL | ARM_FT_STACKALIGN;
2077
2078   /* No argument - default to IRQ.  */
2079   if (argument == NULL_TREE)
2080     return ARM_FT_ISR;
2081
2082   /* Get the value of the argument.  */
2083   if (TREE_VALUE (argument) == NULL_TREE
2084       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
2085     return ARM_FT_UNKNOWN;
2086
2087   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
2088
2089   /* Check it against the list of known arguments.  */
2090   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
2091     if (streq (arg, ptr->arg))
2092       return ptr->return_value;
2093
2094   /* An unrecognized interrupt type.  */
2095   return ARM_FT_UNKNOWN;
2096 }
2097
2098 /* Computes the type of the current function.  */
2099
2100 static unsigned long
2101 arm_compute_func_type (void)
2102 {
2103   unsigned long type = ARM_FT_UNKNOWN;
2104   tree a;
2105   tree attr;
2106
2107   gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
2108
2109   /* Decide if the current function is volatile.  Such functions
2110      never return, and many memory cycles can be saved by not storing
2111      register values that will never be needed again.  This optimization
2112      was added to speed up context switching in a kernel application.  */
2113   if (optimize > 0
2114       && (TREE_NOTHROW (current_function_decl)
2115           || !(flag_unwind_tables
2116                || (flag_exceptions
2117                    && arm_except_unwind_info (&global_options) != UI_SJLJ)))
2118       && TREE_THIS_VOLATILE (current_function_decl))
2119     type |= ARM_FT_VOLATILE;
2120
2121   if (cfun->static_chain_decl != NULL)
2122     type |= ARM_FT_NESTED;
2123
2124   attr = DECL_ATTRIBUTES (current_function_decl);
2125
2126   a = lookup_attribute ("naked", attr);
2127   if (a != NULL_TREE)
2128     type |= ARM_FT_NAKED;
2129
2130   a = lookup_attribute ("isr", attr);
2131   if (a == NULL_TREE)
2132     a = lookup_attribute ("interrupt", attr);
2133
2134   if (a == NULL_TREE)
2135     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
2136   else
2137     type |= arm_isr_value (TREE_VALUE (a));
2138
2139   return type;
2140 }
2141
2142 /* Returns the type of the current function.  */
2143
2144 unsigned long
2145 arm_current_func_type (void)
2146 {
2147   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
2148     cfun->machine->func_type = arm_compute_func_type ();
2149
2150   return cfun->machine->func_type;
2151 }
2152
2153 bool
2154 arm_allocate_stack_slots_for_args (void)
2155 {
2156   /* Naked functions should not allocate stack slots for arguments.  */
2157   return !IS_NAKED (arm_current_func_type ());
2158 }
2159
2160 \f
2161 /* Output assembler code for a block containing the constant parts
2162    of a trampoline, leaving space for the variable parts.
2163
2164    On the ARM, (if r8 is the static chain regnum, and remembering that
2165    referencing pc adds an offset of 8) the trampoline looks like:
2166            ldr          r8, [pc, #0]
2167            ldr          pc, [pc]
2168            .word        static chain value
2169            .word        function's address
2170    XXX FIXME: When the trampoline returns, r8 will be clobbered.  */
2171
2172 static void
2173 arm_asm_trampoline_template (FILE *f)
2174 {
2175   if (TARGET_ARM)
2176     {
2177       asm_fprintf (f, "\tldr\t%r, [%r, #0]\n", STATIC_CHAIN_REGNUM, PC_REGNUM);
2178       asm_fprintf (f, "\tldr\t%r, [%r, #0]\n", PC_REGNUM, PC_REGNUM);
2179     }
2180   else if (TARGET_THUMB2)
2181     {
2182       /* The Thumb-2 trampoline is similar to the arm implementation.
2183          Unlike 16-bit Thumb, we enter the stub in thumb mode.  */
2184       asm_fprintf (f, "\tldr.w\t%r, [%r, #4]\n",
2185                    STATIC_CHAIN_REGNUM, PC_REGNUM);
2186       asm_fprintf (f, "\tldr.w\t%r, [%r, #4]\n", PC_REGNUM, PC_REGNUM);
2187     }
2188   else
2189     {
2190       ASM_OUTPUT_ALIGN (f, 2);
2191       fprintf (f, "\t.code\t16\n");
2192       fprintf (f, ".Ltrampoline_start:\n");
2193       asm_fprintf (f, "\tpush\t{r0, r1}\n");
2194       asm_fprintf (f, "\tldr\tr0, [%r, #8]\n", PC_REGNUM);
2195       asm_fprintf (f, "\tmov\t%r, r0\n", STATIC_CHAIN_REGNUM);
2196       asm_fprintf (f, "\tldr\tr0, [%r, #8]\n", PC_REGNUM);
2197       asm_fprintf (f, "\tstr\tr0, [%r, #4]\n", SP_REGNUM);
2198       asm_fprintf (f, "\tpop\t{r0, %r}\n", PC_REGNUM);
2199     }
2200   assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2201   assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2202 }
2203
2204 /* Emit RTL insns to initialize the variable parts of a trampoline.  */
2205
2206 static void
2207 arm_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
2208 {
2209   rtx fnaddr, mem, a_tramp;
2210
2211   emit_block_move (m_tramp, assemble_trampoline_template (),
2212                    GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
2213
2214   mem = adjust_address (m_tramp, SImode, TARGET_32BIT ? 8 : 12);
2215   emit_move_insn (mem, chain_value);
2216
2217   mem = adjust_address (m_tramp, SImode, TARGET_32BIT ? 12 : 16);
2218   fnaddr = XEXP (DECL_RTL (fndecl), 0);
2219   emit_move_insn (mem, fnaddr);
2220
2221   a_tramp = XEXP (m_tramp, 0);
2222   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__clear_cache"),
2223                      LCT_NORMAL, VOIDmode, 2, a_tramp, Pmode,
2224                      plus_constant (a_tramp, TRAMPOLINE_SIZE), Pmode);
2225 }
2226
2227 /* Thumb trampolines should be entered in thumb mode, so set
2228    the bottom bit of the address.  */
2229
2230 static rtx
2231 arm_trampoline_adjust_address (rtx addr)
2232 {
2233   if (TARGET_THUMB)
2234     addr = expand_simple_binop (Pmode, IOR, addr, const1_rtx,
2235                                 NULL, 0, OPTAB_LIB_WIDEN);
2236   return addr;
2237 }
2238 \f
2239 /* Return 1 if it is possible to return using a single instruction.
2240    If SIBLING is non-null, this is a test for a return before a sibling
2241    call.  SIBLING is the call insn, so we can examine its register usage.  */
2242
2243 int
2244 use_return_insn (int iscond, rtx sibling)
2245 {
2246   int regno;
2247   unsigned int func_type;
2248   unsigned long saved_int_regs;
2249   unsigned HOST_WIDE_INT stack_adjust;
2250   arm_stack_offsets *offsets;
2251
2252   /* Never use a return instruction before reload has run.  */
2253   if (!reload_completed)
2254     return 0;
2255
2256   func_type = arm_current_func_type ();
2257
2258   /* Naked, volatile and stack alignment functions need special
2259      consideration.  */
2260   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED | ARM_FT_STACKALIGN))
2261     return 0;
2262
2263   /* So do interrupt functions that use the frame pointer and Thumb
2264      interrupt functions.  */
2265   if (IS_INTERRUPT (func_type) && (frame_pointer_needed || TARGET_THUMB))
2266     return 0;
2267
2268   offsets = arm_get_frame_offsets ();
2269   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
2270
2271   /* As do variadic functions.  */
2272   if (crtl->args.pretend_args_size
2273       || cfun->machine->uses_anonymous_args
2274       /* Or if the function calls __builtin_eh_return () */
2275       || crtl->calls_eh_return
2276       /* Or if the function calls alloca */
2277       || cfun->calls_alloca
2278       /* Or if there is a stack adjustment.  However, if the stack pointer
2279          is saved on the stack, we can use a pre-incrementing stack load.  */
2280       || !(stack_adjust == 0 || (TARGET_APCS_FRAME && frame_pointer_needed
2281                                  && stack_adjust == 4)))
2282     return 0;
2283
2284   saved_int_regs = offsets->saved_regs_mask;
2285
2286   /* Unfortunately, the insn
2287
2288        ldmib sp, {..., sp, ...}
2289
2290      triggers a bug on most SA-110 based devices, such that the stack
2291      pointer won't be correctly restored if the instruction takes a
2292      page fault.  We work around this problem by popping r3 along with
2293      the other registers, since that is never slower than executing
2294      another instruction.
2295
2296      We test for !arm_arch5 here, because code for any architecture
2297      less than this could potentially be run on one of the buggy
2298      chips.  */
2299   if (stack_adjust == 4 && !arm_arch5 && TARGET_ARM)
2300     {
2301       /* Validate that r3 is a call-clobbered register (always true in
2302          the default abi) ...  */
2303       if (!call_used_regs[3])
2304         return 0;
2305
2306       /* ... that it isn't being used for a return value ... */
2307       if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
2308         return 0;
2309
2310       /* ... or for a tail-call argument ...  */
2311       if (sibling)
2312         {
2313           gcc_assert (GET_CODE (sibling) == CALL_INSN);
2314
2315           if (find_regno_fusage (sibling, USE, 3))
2316             return 0;
2317         }
2318
2319       /* ... and that there are no call-saved registers in r0-r2
2320          (always true in the default ABI).  */
2321       if (saved_int_regs & 0x7)
2322         return 0;
2323     }
2324
2325   /* Can't be done if interworking with Thumb, and any registers have been
2326      stacked.  */
2327   if (TARGET_INTERWORK && saved_int_regs != 0 && !IS_INTERRUPT(func_type))
2328     return 0;
2329
2330   /* On StrongARM, conditional returns are expensive if they aren't
2331      taken and multiple registers have been stacked.  */
2332   if (iscond && arm_tune_strongarm)
2333     {
2334       /* Conditional return when just the LR is stored is a simple
2335          conditional-load instruction, that's not expensive.  */
2336       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
2337         return 0;
2338
2339       if (flag_pic 
2340           && arm_pic_register != INVALID_REGNUM
2341           && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
2342         return 0;
2343     }
2344
2345   /* If there are saved registers but the LR isn't saved, then we need
2346      two instructions for the return.  */
2347   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
2348     return 0;
2349
2350   /* Can't be done if any of the FPA regs are pushed,
2351      since this also requires an insn.  */
2352   if (TARGET_HARD_FLOAT && TARGET_FPA)
2353     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
2354       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
2355         return 0;
2356
2357   /* Likewise VFP regs.  */
2358   if (TARGET_HARD_FLOAT && TARGET_VFP)
2359     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
2360       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
2361         return 0;
2362
2363   if (TARGET_REALLY_IWMMXT)
2364     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
2365       if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
2366         return 0;
2367
2368   return 1;
2369 }
2370
2371 /* Return TRUE if int I is a valid immediate ARM constant.  */
2372
2373 int
2374 const_ok_for_arm (HOST_WIDE_INT i)
2375 {
2376   int lowbit;
2377
2378   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
2379      be all zero, or all one.  */
2380   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
2381       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
2382           != ((~(unsigned HOST_WIDE_INT) 0)
2383               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
2384     return FALSE;
2385
2386   i &= (unsigned HOST_WIDE_INT) 0xffffffff;
2387
2388   /* Fast return for 0 and small values.  We must do this for zero, since
2389      the code below can't handle that one case.  */
2390   if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
2391     return TRUE;
2392
2393   /* Get the number of trailing zeros.  */
2394   lowbit = ffs((int) i) - 1;
2395   
2396   /* Only even shifts are allowed in ARM mode so round down to the
2397      nearest even number.  */
2398   if (TARGET_ARM)
2399     lowbit &= ~1;
2400
2401   if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
2402     return TRUE;
2403
2404   if (TARGET_ARM)
2405     {
2406       /* Allow rotated constants in ARM mode.  */
2407       if (lowbit <= 4
2408            && ((i & ~0xc000003f) == 0
2409                || (i & ~0xf000000f) == 0
2410                || (i & ~0xfc000003) == 0))
2411         return TRUE;
2412     }
2413   else
2414     {
2415       HOST_WIDE_INT v;
2416
2417       /* Allow repeated patterns 0x00XY00XY or 0xXYXYXYXY.  */
2418       v = i & 0xff;
2419       v |= v << 16;
2420       if (i == v || i == (v | (v << 8)))
2421         return TRUE;
2422
2423       /* Allow repeated pattern 0xXY00XY00.  */
2424       v = i & 0xff00;
2425       v |= v << 16;
2426       if (i == v)
2427         return TRUE;
2428     }
2429
2430   return FALSE;
2431 }
2432
2433 /* Return true if I is a valid constant for the operation CODE.  */
2434 static int
2435 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
2436 {
2437   if (const_ok_for_arm (i))
2438     return 1;
2439
2440   switch (code)
2441     {
2442     case PLUS:
2443     case COMPARE:
2444     case EQ:
2445     case NE:
2446     case GT:
2447     case LE:
2448     case LT:
2449     case GE:
2450     case GEU:
2451     case LTU:
2452     case GTU:
2453     case LEU:
2454     case UNORDERED:
2455     case ORDERED:
2456     case UNEQ:
2457     case UNGE:
2458     case UNLT:
2459     case UNGT:
2460     case UNLE:
2461       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
2462
2463     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
2464     case XOR:
2465       return 0;
2466
2467     case IOR:
2468       if (TARGET_THUMB2)
2469         return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2470       return 0;
2471
2472     case AND:
2473       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2474
2475     default:
2476       gcc_unreachable ();
2477     }
2478 }
2479
2480 /* Emit a sequence of insns to handle a large constant.
2481    CODE is the code of the operation required, it can be any of SET, PLUS,
2482    IOR, AND, XOR, MINUS;
2483    MODE is the mode in which the operation is being performed;
2484    VAL is the integer to operate on;
2485    SOURCE is the other operand (a register, or a null-pointer for SET);
2486    SUBTARGETS means it is safe to create scratch registers if that will
2487    either produce a simpler sequence, or we will want to cse the values.
2488    Return value is the number of insns emitted.  */
2489
2490 /* ??? Tweak this for thumb2.  */
2491 int
2492 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
2493                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
2494 {
2495   rtx cond;
2496
2497   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
2498     cond = COND_EXEC_TEST (PATTERN (insn));
2499   else
2500     cond = NULL_RTX;
2501
2502   if (subtargets || code == SET
2503       || (GET_CODE (target) == REG && GET_CODE (source) == REG
2504           && REGNO (target) != REGNO (source)))
2505     {
2506       /* After arm_reorg has been called, we can't fix up expensive
2507          constants by pushing them into memory so we must synthesize
2508          them in-line, regardless of the cost.  This is only likely to
2509          be more costly on chips that have load delay slots and we are
2510          compiling without running the scheduler (so no splitting
2511          occurred before the final instruction emission).
2512
2513          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
2514       */
2515       if (!after_arm_reorg
2516           && !cond
2517           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
2518                                 1, 0)
2519               > (arm_constant_limit (optimize_function_for_size_p (cfun))
2520                  + (code != SET))))
2521         {
2522           if (code == SET)
2523             {
2524               /* Currently SET is the only monadic value for CODE, all
2525                  the rest are diadic.  */
2526               if (TARGET_USE_MOVT)
2527                 arm_emit_movpair (target, GEN_INT (val));
2528               else
2529                 emit_set_insn (target, GEN_INT (val));
2530
2531               return 1;
2532             }
2533           else
2534             {
2535               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
2536
2537               if (TARGET_USE_MOVT)
2538                 arm_emit_movpair (temp, GEN_INT (val));
2539               else
2540                 emit_set_insn (temp, GEN_INT (val));
2541
2542               /* For MINUS, the value is subtracted from, since we never
2543                  have subtraction of a constant.  */
2544               if (code == MINUS)
2545                 emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
2546               else
2547                 emit_set_insn (target,
2548                                gen_rtx_fmt_ee (code, mode, source, temp));
2549               return 2;
2550             }
2551         }
2552     }
2553
2554   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
2555                            1);
2556 }
2557
2558 /* Return the number of instructions required to synthesize the given
2559    constant, if we start emitting them from bit-position I.  */
2560 static int
2561 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
2562 {
2563   HOST_WIDE_INT temp1;
2564   int step_size = TARGET_ARM ? 2 : 1;
2565   int num_insns = 0;
2566
2567   gcc_assert (TARGET_ARM || i == 0);
2568
2569   do
2570     {
2571       int end;
2572
2573       if (i <= 0)
2574         i += 32;
2575       if (remainder & (((1 << step_size) - 1) << (i - step_size)))
2576         {
2577           end = i - 8;
2578           if (end < 0)
2579             end += 32;
2580           temp1 = remainder & ((0x0ff << end)
2581                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
2582           remainder &= ~temp1;
2583           num_insns++;
2584           i -= 8 - step_size;
2585         }
2586       i -= step_size;
2587     } while (remainder);
2588   return num_insns;
2589 }
2590
2591 static int
2592 find_best_start (unsigned HOST_WIDE_INT remainder)
2593 {
2594   int best_consecutive_zeros = 0;
2595   int i;
2596   int best_start = 0;
2597
2598   /* If we aren't targetting ARM, the best place to start is always at
2599      the bottom.  */
2600   if (! TARGET_ARM)
2601     return 0;
2602
2603   for (i = 0; i < 32; i += 2)
2604     {
2605       int consecutive_zeros = 0;
2606
2607       if (!(remainder & (3 << i)))
2608         {
2609           while ((i < 32) && !(remainder & (3 << i)))
2610             {
2611               consecutive_zeros += 2;
2612               i += 2;
2613             }
2614           if (consecutive_zeros > best_consecutive_zeros)
2615             {
2616               best_consecutive_zeros = consecutive_zeros;
2617               best_start = i - consecutive_zeros;
2618             }
2619           i -= 2;
2620         }
2621     }
2622
2623   /* So long as it won't require any more insns to do so, it's
2624      desirable to emit a small constant (in bits 0...9) in the last
2625      insn.  This way there is more chance that it can be combined with
2626      a later addressing insn to form a pre-indexed load or store
2627      operation.  Consider:
2628
2629            *((volatile int *)0xe0000100) = 1;
2630            *((volatile int *)0xe0000110) = 2;
2631
2632      We want this to wind up as:
2633
2634             mov rA, #0xe0000000
2635             mov rB, #1
2636             str rB, [rA, #0x100]
2637             mov rB, #2
2638             str rB, [rA, #0x110]
2639
2640      rather than having to synthesize both large constants from scratch.
2641
2642      Therefore, we calculate how many insns would be required to emit
2643      the constant starting from `best_start', and also starting from
2644      zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2645      yield a shorter sequence, we may as well use zero.  */
2646   if (best_start != 0
2647       && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2648       && (count_insns_for_constant (remainder, 0) <=
2649           count_insns_for_constant (remainder, best_start)))
2650     best_start = 0;
2651
2652   return best_start;
2653 }
2654
2655 /* Emit an instruction with the indicated PATTERN.  If COND is
2656    non-NULL, conditionalize the execution of the instruction on COND
2657    being true.  */
2658
2659 static void
2660 emit_constant_insn (rtx cond, rtx pattern)
2661 {
2662   if (cond)
2663     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
2664   emit_insn (pattern);
2665 }
2666
2667 /* As above, but extra parameter GENERATE which, if clear, suppresses
2668    RTL generation.  */
2669 /* ??? This needs more work for thumb2.  */
2670
2671 static int
2672 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
2673                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
2674                   int generate)
2675 {
2676   int can_invert = 0;
2677   int can_negate = 0;
2678   int final_invert = 0;
2679   int can_negate_initial = 0;
2680   int i;
2681   int num_bits_set = 0;
2682   int set_sign_bit_copies = 0;
2683   int clear_sign_bit_copies = 0;
2684   int clear_zero_bit_copies = 0;
2685   int set_zero_bit_copies = 0;
2686   int insns = 0;
2687   unsigned HOST_WIDE_INT temp1, temp2;
2688   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
2689   int step_size = TARGET_ARM ? 2 : 1;
2690
2691   /* Find out which operations are safe for a given CODE.  Also do a quick
2692      check for degenerate cases; these can occur when DImode operations
2693      are split.  */
2694   switch (code)
2695     {
2696     case SET:
2697       can_invert = 1;
2698       can_negate = 1;
2699       break;
2700
2701     case PLUS:
2702       can_negate = 1;
2703       can_negate_initial = 1;
2704       break;
2705
2706     case IOR:
2707       if (remainder == 0xffffffff)
2708         {
2709           if (generate)
2710             emit_constant_insn (cond,
2711                                 gen_rtx_SET (VOIDmode, target,
2712                                              GEN_INT (ARM_SIGN_EXTEND (val))));
2713           return 1;
2714         }
2715
2716       if (remainder == 0)
2717         {
2718           if (reload_completed && rtx_equal_p (target, source))
2719             return 0;
2720
2721           if (generate)
2722             emit_constant_insn (cond,
2723                                 gen_rtx_SET (VOIDmode, target, source));
2724           return 1;
2725         }
2726
2727       if (TARGET_THUMB2)
2728         can_invert = 1;
2729       break;
2730
2731     case AND:
2732       if (remainder == 0)
2733         {
2734           if (generate)
2735             emit_constant_insn (cond,
2736                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
2737           return 1;
2738         }
2739       if (remainder == 0xffffffff)
2740         {
2741           if (reload_completed && rtx_equal_p (target, source))
2742             return 0;
2743           if (generate)
2744             emit_constant_insn (cond,
2745                                 gen_rtx_SET (VOIDmode, target, source));
2746           return 1;
2747         }
2748       can_invert = 1;
2749       break;
2750
2751     case XOR:
2752       if (remainder == 0)
2753         {
2754           if (reload_completed && rtx_equal_p (target, source))
2755             return 0;
2756           if (generate)
2757             emit_constant_insn (cond,
2758                                 gen_rtx_SET (VOIDmode, target, source));
2759           return 1;
2760         }
2761
2762       if (remainder == 0xffffffff)
2763         {
2764           if (generate)
2765             emit_constant_insn (cond,
2766                                 gen_rtx_SET (VOIDmode, target,
2767                                              gen_rtx_NOT (mode, source)));
2768           return 1;
2769         }
2770       break;
2771
2772     case MINUS:
2773       /* We treat MINUS as (val - source), since (source - val) is always
2774          passed as (source + (-val)).  */
2775       if (remainder == 0)
2776         {
2777           if (generate)
2778             emit_constant_insn (cond,
2779                                 gen_rtx_SET (VOIDmode, target,
2780                                              gen_rtx_NEG (mode, source)));
2781           return 1;
2782         }
2783       if (const_ok_for_arm (val))
2784         {
2785           if (generate)
2786             emit_constant_insn (cond,
2787                                 gen_rtx_SET (VOIDmode, target,
2788                                              gen_rtx_MINUS (mode, GEN_INT (val),
2789                                                             source)));
2790           return 1;
2791         }
2792       can_negate = 1;
2793
2794       break;
2795
2796     default:
2797       gcc_unreachable ();
2798     }
2799
2800   /* If we can do it in one insn get out quickly.  */
2801   if (const_ok_for_arm (val)
2802       || (can_negate_initial && const_ok_for_arm (-val))
2803       || (can_invert && const_ok_for_arm (~val)))
2804     {
2805       if (generate)
2806         emit_constant_insn (cond,
2807                             gen_rtx_SET (VOIDmode, target,
2808                                          (source
2809                                           ? gen_rtx_fmt_ee (code, mode, source,
2810                                                             GEN_INT (val))
2811                                           : GEN_INT (val))));
2812       return 1;
2813     }
2814
2815   /* Calculate a few attributes that may be useful for specific
2816      optimizations.  */
2817   /* Count number of leading zeros.  */
2818   for (i = 31; i >= 0; i--)
2819     {
2820       if ((remainder & (1 << i)) == 0)
2821         clear_sign_bit_copies++;
2822       else
2823         break;
2824     }
2825
2826   /* Count number of leading 1's.  */
2827   for (i = 31; i >= 0; i--)
2828     {
2829       if ((remainder & (1 << i)) != 0)
2830         set_sign_bit_copies++;
2831       else
2832         break;
2833     }
2834
2835   /* Count number of trailing zero's.  */
2836   for (i = 0; i <= 31; i++)
2837     {
2838       if ((remainder & (1 << i)) == 0)
2839         clear_zero_bit_copies++;
2840       else
2841         break;
2842     }
2843
2844   /* Count number of trailing 1's.  */
2845   for (i = 0; i <= 31; i++)
2846     {
2847       if ((remainder & (1 << i)) != 0)
2848         set_zero_bit_copies++;
2849       else
2850         break;
2851     }
2852
2853   switch (code)
2854     {
2855     case SET:
2856       /* See if we can use movw.  */
2857       if (arm_arch_thumb2 && (remainder & 0xffff0000) == 0)
2858         {
2859           if (generate)
2860             emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
2861                                                    GEN_INT (val)));
2862           return 1;
2863         }
2864
2865       /* See if we can do this by sign_extending a constant that is known
2866          to be negative.  This is a good, way of doing it, since the shift
2867          may well merge into a subsequent insn.  */
2868       if (set_sign_bit_copies > 1)
2869         {
2870           if (const_ok_for_arm
2871               (temp1 = ARM_SIGN_EXTEND (remainder
2872                                         << (set_sign_bit_copies - 1))))
2873             {
2874               if (generate)
2875                 {
2876                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2877                   emit_constant_insn (cond,
2878                                       gen_rtx_SET (VOIDmode, new_src,
2879                                                    GEN_INT (temp1)));
2880                   emit_constant_insn (cond,
2881                                       gen_ashrsi3 (target, new_src,
2882                                                    GEN_INT (set_sign_bit_copies - 1)));
2883                 }
2884               return 2;
2885             }
2886           /* For an inverted constant, we will need to set the low bits,
2887              these will be shifted out of harm's way.  */
2888           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
2889           if (const_ok_for_arm (~temp1))
2890             {
2891               if (generate)
2892                 {
2893                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2894                   emit_constant_insn (cond,
2895                                       gen_rtx_SET (VOIDmode, new_src,
2896                                                    GEN_INT (temp1)));
2897                   emit_constant_insn (cond,
2898                                       gen_ashrsi3 (target, new_src,
2899                                                    GEN_INT (set_sign_bit_copies - 1)));
2900                 }
2901               return 2;
2902             }
2903         }
2904
2905       /* See if we can calculate the value as the difference between two
2906          valid immediates.  */
2907       if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
2908         {
2909           int topshift = clear_sign_bit_copies & ~1;
2910
2911           temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
2912                                    & (0xff000000 >> topshift));
2913
2914           /* If temp1 is zero, then that means the 9 most significant
2915              bits of remainder were 1 and we've caused it to overflow.
2916              When topshift is 0 we don't need to do anything since we
2917              can borrow from 'bit 32'.  */
2918           if (temp1 == 0 && topshift != 0)
2919             temp1 = 0x80000000 >> (topshift - 1);
2920
2921           temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
2922
2923           if (const_ok_for_arm (temp2))
2924             {
2925               if (generate)
2926                 {
2927                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2928                   emit_constant_insn (cond,
2929                                       gen_rtx_SET (VOIDmode, new_src,
2930                                                    GEN_INT (temp1)));
2931                   emit_constant_insn (cond,
2932                                       gen_addsi3 (target, new_src,
2933                                                   GEN_INT (-temp2)));
2934                 }
2935
2936               return 2;
2937             }
2938         }
2939
2940       /* See if we can generate this by setting the bottom (or the top)
2941          16 bits, and then shifting these into the other half of the
2942          word.  We only look for the simplest cases, to do more would cost
2943          too much.  Be careful, however, not to generate this when the
2944          alternative would take fewer insns.  */
2945       if (val & 0xffff0000)
2946         {
2947           temp1 = remainder & 0xffff0000;
2948           temp2 = remainder & 0x0000ffff;
2949
2950           /* Overlaps outside this range are best done using other methods.  */
2951           for (i = 9; i < 24; i++)
2952             {
2953               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
2954                   && !const_ok_for_arm (temp2))
2955                 {
2956                   rtx new_src = (subtargets
2957                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2958                                  : target);
2959                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2960                                             source, subtargets, generate);
2961                   source = new_src;
2962                   if (generate)
2963                     emit_constant_insn
2964                       (cond,
2965                        gen_rtx_SET
2966                        (VOIDmode, target,
2967                         gen_rtx_IOR (mode,
2968                                      gen_rtx_ASHIFT (mode, source,
2969                                                      GEN_INT (i)),
2970                                      source)));
2971                   return insns + 1;
2972                 }
2973             }
2974
2975           /* Don't duplicate cases already considered.  */
2976           for (i = 17; i < 24; i++)
2977             {
2978               if (((temp1 | (temp1 >> i)) == remainder)
2979                   && !const_ok_for_arm (temp1))
2980                 {
2981                   rtx new_src = (subtargets
2982                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2983                                  : target);
2984                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2985                                             source, subtargets, generate);
2986                   source = new_src;
2987                   if (generate)
2988                     emit_constant_insn
2989                       (cond,
2990                        gen_rtx_SET (VOIDmode, target,
2991                                     gen_rtx_IOR
2992                                     (mode,
2993                                      gen_rtx_LSHIFTRT (mode, source,
2994                                                        GEN_INT (i)),
2995                                      source)));
2996                   return insns + 1;
2997                 }
2998             }
2999         }
3000       break;
3001
3002     case IOR:
3003     case XOR:
3004       /* If we have IOR or XOR, and the constant can be loaded in a
3005          single instruction, and we can find a temporary to put it in,
3006          then this can be done in two instructions instead of 3-4.  */
3007       if (subtargets
3008           /* TARGET can't be NULL if SUBTARGETS is 0 */
3009           || (reload_completed && !reg_mentioned_p (target, source)))
3010         {
3011           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
3012             {
3013               if (generate)
3014                 {
3015                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
3016
3017                   emit_constant_insn (cond,
3018                                       gen_rtx_SET (VOIDmode, sub,
3019                                                    GEN_INT (val)));
3020                   emit_constant_insn (cond,
3021                                       gen_rtx_SET (VOIDmode, target,
3022                                                    gen_rtx_fmt_ee (code, mode,
3023                                                                    source, sub)));
3024                 }
3025               return 2;
3026             }
3027         }
3028
3029       if (code == XOR)
3030         break;
3031
3032       /*  Convert.
3033           x = y | constant ( which is composed of set_sign_bit_copies of leading 1s
3034                              and the remainder 0s for e.g. 0xfff00000)
3035           x = ~(~(y ashift set_sign_bit_copies) lshiftrt set_sign_bit_copies)
3036
3037           This can be done in 2 instructions by using shifts with mov or mvn.
3038           e.g. for
3039           x = x | 0xfff00000;
3040           we generate.
3041           mvn   r0, r0, asl #12
3042           mvn   r0, r0, lsr #12  */
3043       if (set_sign_bit_copies > 8
3044           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
3045         {
3046           if (generate)
3047             {
3048               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
3049               rtx shift = GEN_INT (set_sign_bit_copies);
3050
3051               emit_constant_insn
3052                 (cond,
3053                  gen_rtx_SET (VOIDmode, sub,
3054                               gen_rtx_NOT (mode,
3055                                            gen_rtx_ASHIFT (mode,
3056                                                            source,
3057                                                            shift))));
3058               emit_constant_insn
3059                 (cond,
3060                  gen_rtx_SET (VOIDmode, target,
3061                               gen_rtx_NOT (mode,
3062                                            gen_rtx_LSHIFTRT (mode, sub,
3063                                                              shift))));
3064             }
3065           return 2;
3066         }
3067
3068       /* Convert
3069           x = y | constant (which has set_zero_bit_copies number of trailing ones).
3070            to
3071           x = ~((~y lshiftrt set_zero_bit_copies) ashift set_zero_bit_copies).
3072
3073           For eg. r0 = r0 | 0xfff
3074                mvn      r0, r0, lsr #12
3075                mvn      r0, r0, asl #12
3076
3077       */
3078       if (set_zero_bit_copies > 8
3079           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
3080         {
3081           if (generate)
3082             {
3083               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
3084               rtx shift = GEN_INT (set_zero_bit_copies);
3085
3086               emit_constant_insn
3087                 (cond,
3088                  gen_rtx_SET (VOIDmode, sub,
3089                               gen_rtx_NOT (mode,
3090                                            gen_rtx_LSHIFTRT (mode,
3091                                                              source,
3092                                                              shift))));
3093               emit_constant_insn
3094                 (cond,
3095                  gen_rtx_SET (VOIDmode, target,
3096                               gen_rtx_NOT (mode,
3097                                            gen_rtx_ASHIFT (mode, sub,
3098                                                            shift))));
3099             }
3100           return 2;
3101         }
3102
3103       /* This will never be reached for Thumb2 because orn is a valid
3104          instruction. This is for Thumb1 and the ARM 32 bit cases.
3105
3106          x = y | constant (such that ~constant is a valid constant)
3107          Transform this to
3108          x = ~(~y & ~constant).
3109       */
3110       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
3111         {
3112           if (generate)
3113             {
3114               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
3115               emit_constant_insn (cond,
3116                                   gen_rtx_SET (VOIDmode, sub,
3117                                                gen_rtx_NOT (mode, source)));
3118               source = sub;
3119               if (subtargets)
3120                 sub = gen_reg_rtx (mode);
3121               emit_constant_insn (cond,
3122                                   gen_rtx_SET (VOIDmode, sub,
3123                                                gen_rtx_AND (mode, source,
3124                                                             GEN_INT (temp1))));
3125               emit_constant_insn (cond,
3126                                   gen_rtx_SET (VOIDmode, target,
3127                                                gen_rtx_NOT (mode, sub)));
3128             }
3129           return 3;
3130         }
3131       break;
3132
3133     case AND:
3134       /* See if two shifts will do 2 or more insn's worth of work.  */
3135       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
3136         {
3137           HOST_WIDE_INT shift_mask = ((0xffffffff
3138                                        << (32 - clear_sign_bit_copies))
3139                                       & 0xffffffff);
3140
3141           if ((remainder | shift_mask) != 0xffffffff)
3142             {
3143               if (generate)
3144                 {
3145                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3146                   insns = arm_gen_constant (AND, mode, cond,
3147                                             remainder | shift_mask,
3148                                             new_src, source, subtargets, 1);
3149                   source = new_src;
3150                 }
3151               else
3152                 {
3153                   rtx targ = subtargets ? NULL_RTX : target;
3154                   insns = arm_gen_constant (AND, mode, cond,
3155                                             remainder | shift_mask,
3156                                             targ, source, subtargets, 0);
3157                 }
3158             }
3159
3160           if (generate)
3161             {
3162               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3163               rtx shift = GEN_INT (clear_sign_bit_copies);
3164
3165               emit_insn (gen_ashlsi3 (new_src, source, shift));
3166               emit_insn (gen_lshrsi3 (target, new_src, shift));
3167             }
3168
3169           return insns + 2;
3170         }
3171
3172       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
3173         {
3174           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
3175
3176           if ((remainder | shift_mask) != 0xffffffff)
3177             {
3178               if (generate)
3179                 {
3180                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3181
3182                   insns = arm_gen_constant (AND, mode, cond,
3183                                             remainder | shift_mask,
3184                                             new_src, source, subtargets, 1);
3185                   source = new_src;
3186                 }
3187               else
3188                 {
3189                   rtx targ = subtargets ? NULL_RTX : target;
3190
3191                   insns = arm_gen_constant (AND, mode, cond,
3192                                             remainder | shift_mask,
3193                                             targ, source, subtargets, 0);
3194                 }
3195             }
3196
3197           if (generate)
3198             {
3199               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3200               rtx shift = GEN_INT (clear_zero_bit_copies);
3201
3202               emit_insn (gen_lshrsi3 (new_src, source, shift));
3203               emit_insn (gen_ashlsi3 (target, new_src, shift));
3204             }
3205
3206           return insns + 2;
3207         }
3208
3209       break;
3210
3211     default:
3212       break;
3213     }
3214
3215   for (i = 0; i < 32; i++)
3216     if (remainder & (1 << i))
3217       num_bits_set++;
3218
3219   if ((code == AND)
3220       || (code != IOR && can_invert && num_bits_set > 16))
3221     remainder ^= 0xffffffff;
3222   else if (code == PLUS && num_bits_set > 16)
3223     remainder = (-remainder) & 0xffffffff;
3224
3225   /* For XOR, if more than half the bits are set and there's a sequence
3226      of more than 8 consecutive ones in the pattern then we can XOR by the
3227      inverted constant and then invert the final result; this may save an
3228      instruction and might also lead to the final mvn being merged with
3229      some other operation.  */
3230   else if (code == XOR && num_bits_set > 16
3231            && (count_insns_for_constant (remainder ^ 0xffffffff,
3232                                          find_best_start
3233                                          (remainder ^ 0xffffffff))
3234                < count_insns_for_constant (remainder,
3235                                            find_best_start (remainder))))
3236     {
3237       remainder ^= 0xffffffff;
3238       final_invert = 1;
3239     }
3240   else
3241     {
3242       can_invert = 0;
3243       can_negate = 0;
3244     }
3245
3246   /* Now try and find a way of doing the job in either two or three
3247      instructions.
3248      We start by looking for the largest block of zeros that are aligned on
3249      a 2-bit boundary, we then fill up the temps, wrapping around to the
3250      top of the word when we drop off the bottom.
3251      In the worst case this code should produce no more than four insns.
3252      Thumb-2 constants are shifted, not rotated, so the MSB is always the
3253      best place to start.  */
3254
3255   /* ??? Use thumb2 replicated constants when the high and low halfwords are
3256      the same.  */
3257   {
3258     /* Now start emitting the insns.  */
3259     i = find_best_start (remainder);
3260     do
3261       {
3262         int end;
3263
3264         if (i <= 0)
3265           i += 32;
3266         if (remainder & (3 << (i - 2)))
3267           {
3268             end = i - 8;
3269             if (end < 0)
3270               end += 32;
3271             temp1 = remainder & ((0x0ff << end)
3272                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
3273             remainder &= ~temp1;
3274
3275             if (generate)
3276               {
3277                 rtx new_src, temp1_rtx;
3278
3279                 if (code == SET || code == MINUS)
3280                   {
3281                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
3282                     if (can_invert && code != MINUS)
3283                       temp1 = ~temp1;
3284                   }
3285                 else
3286                   {
3287                     if ((final_invert || remainder) && subtargets)
3288                       new_src = gen_reg_rtx (mode);
3289                     else
3290                       new_src = target;
3291                     if (can_invert)
3292                       temp1 = ~temp1;
3293                     else if (can_negate)
3294                       temp1 = -temp1;
3295                   }
3296
3297                 temp1 = trunc_int_for_mode (temp1, mode);
3298                 temp1_rtx = GEN_INT (temp1);
3299
3300                 if (code == SET)
3301                   ;
3302                 else if (code == MINUS)
3303                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
3304                 else
3305                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
3306
3307                 emit_constant_insn (cond,
3308                                     gen_rtx_SET (VOIDmode, new_src,
3309                                                  temp1_rtx));
3310                 source = new_src;
3311               }
3312
3313             if (code == SET)
3314               {
3315                 can_invert = 0;
3316                 code = PLUS;
3317               }
3318             else if (code == MINUS)
3319               code = PLUS;
3320
3321             insns++;
3322             i -= 8 - step_size;
3323           }
3324         /* Arm allows rotates by a multiple of two. Thumb-2 allows arbitrary
3325            shifts.  */
3326         i -= step_size;
3327       }
3328     while (remainder);
3329   }
3330
3331   if (final_invert)
3332     {
3333       if (generate)
3334         emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
3335                                                gen_rtx_NOT (mode, source)));
3336       insns++;
3337     }
3338
3339   return insns;
3340 }
3341
3342 /* Canonicalize a comparison so that we are more likely to recognize it.
3343    This can be done for a few constant compares, where we can make the
3344    immediate value easier to load.  */
3345
3346 enum rtx_code
3347 arm_canonicalize_comparison (enum rtx_code code, rtx *op0, rtx *op1)
3348 {
3349   enum machine_mode mode;
3350   unsigned HOST_WIDE_INT i, maxval;
3351
3352   mode = GET_MODE (*op0);
3353   if (mode == VOIDmode)
3354     mode = GET_MODE (*op1);
3355
3356   maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
3357
3358   /* For DImode, we have GE/LT/GEU/LTU comparisons.  In ARM mode
3359      we can also use cmp/cmpeq for GTU/LEU.  GT/LE must be either
3360      reversed or (for constant OP1) adjusted to GE/LT.  Similarly
3361      for GTU/LEU in Thumb mode.  */
3362   if (mode == DImode)
3363     {
3364       rtx tem;
3365
3366       /* To keep things simple, always use the Cirrus cfcmp64 if it is
3367          available.  */
3368       if (TARGET_ARM && TARGET_HARD_FLOAT && TARGET_MAVERICK)
3369         return code;
3370
3371       if (code == GT || code == LE
3372           || (!TARGET_ARM && (code == GTU || code == LEU)))
3373         {
3374           /* Missing comparison.  First try to use an available
3375              comparison.  */
3376           if (GET_CODE (*op1) == CONST_INT)
3377             {
3378               i = INTVAL (*op1);
3379               switch (code)
3380                 {
3381                 case GT:
3382                 case LE:
3383                   if (i != maxval
3384                       && arm_const_double_by_immediates (GEN_INT (i + 1)))
3385                     {
3386                       *op1 = GEN_INT (i + 1);
3387                       return code == GT ? GE : LT;
3388                     }
3389                   break;
3390                 case GTU:
3391                 case LEU:
3392                   if (i != ~((unsigned HOST_WIDE_INT) 0)
3393                       && arm_const_double_by_immediates (GEN_INT (i + 1)))
3394                     {
3395                       *op1 = GEN_INT (i + 1);
3396                       return code == GTU ? GEU : LTU;
3397                     }
3398                   break;
3399                 default:
3400                   gcc_unreachable ();
3401                 }
3402             }
3403
3404           /* If that did not work, reverse the condition.  */
3405           tem = *op0;
3406           *op0 = *op1;
3407           *op1 = tem;
3408           return swap_condition (code);
3409         }
3410
3411       return code;
3412     }
3413
3414   /* Comparisons smaller than DImode.  Only adjust comparisons against
3415      an out-of-range constant.  */
3416   if (GET_CODE (*op1) != CONST_INT
3417       || const_ok_for_arm (INTVAL (*op1))
3418       || const_ok_for_arm (- INTVAL (*op1)))
3419     return code;
3420
3421   i = INTVAL (*op1);
3422
3423   switch (code)
3424     {
3425     case EQ:
3426     case NE:
3427       return code;
3428
3429     case GT:
3430     case LE:
3431       if (i != maxval
3432           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
3433         {
3434           *op1 = GEN_INT (i + 1);
3435           return code == GT ? GE : LT;
3436         }
3437       break;
3438
3439     case GE:
3440     case LT:
3441       if (i != ~maxval
3442           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
3443         {
3444           *op1 = GEN_INT (i - 1);
3445           return code == GE ? GT : LE;
3446         }
3447       break;
3448
3449     case GTU:
3450     case LEU:
3451       if (i != ~((unsigned HOST_WIDE_INT) 0)
3452           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
3453         {
3454           *op1 = GEN_INT (i + 1);
3455           return code == GTU ? GEU : LTU;
3456         }
3457       break;
3458
3459     case GEU:
3460     case LTU:
3461       if (i != 0
3462           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
3463         {
3464           *op1 = GEN_INT (i - 1);
3465           return code == GEU ? GTU : LEU;
3466         }
3467       break;
3468
3469     default:
3470       gcc_unreachable ();
3471     }
3472
3473   return code;
3474 }
3475
3476
3477 /* Define how to find the value returned by a function.  */
3478
3479 static rtx
3480 arm_function_value(const_tree type, const_tree func,
3481                    bool outgoing ATTRIBUTE_UNUSED)
3482 {
3483   enum machine_mode mode;
3484   int unsignedp ATTRIBUTE_UNUSED;
3485   rtx r ATTRIBUTE_UNUSED;
3486
3487   mode = TYPE_MODE (type);
3488
3489   if (TARGET_AAPCS_BASED)
3490     return aapcs_allocate_return_reg (mode, type, func);
3491
3492   /* Promote integer types.  */
3493   if (INTEGRAL_TYPE_P (type))
3494     mode = arm_promote_function_mode (type, mode, &unsignedp, func, 1);
3495
3496   /* Promotes small structs returned in a register to full-word size
3497      for big-endian AAPCS.  */
3498   if (arm_return_in_msb (type))
3499     {
3500       HOST_WIDE_INT size = int_size_in_bytes (type);
3501       if (size % UNITS_PER_WORD != 0)
3502         {
3503           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
3504           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
3505         }
3506     }
3507
3508   return LIBCALL_VALUE (mode);
3509 }
3510
3511 static int
3512 libcall_eq (const void *p1, const void *p2)
3513 {
3514   return rtx_equal_p ((const_rtx) p1, (const_rtx) p2);
3515 }
3516
3517 static hashval_t
3518 libcall_hash (const void *p1)
3519 {
3520   return hash_rtx ((const_rtx) p1, VOIDmode, NULL, NULL, FALSE);
3521 }
3522
3523 static void
3524 add_libcall (htab_t htab, rtx libcall)
3525 {
3526   *htab_find_slot (htab, libcall, INSERT) = libcall;
3527 }
3528
3529 static bool
3530 arm_libcall_uses_aapcs_base (const_rtx libcall)
3531 {
3532   static bool init_done = false;
3533   static htab_t libcall_htab;
3534
3535   if (!init_done)
3536     {
3537       init_done = true;
3538
3539       libcall_htab = htab_create (31, libcall_hash, libcall_eq,
3540                                   NULL);
3541       add_libcall (libcall_htab,
3542                    convert_optab_libfunc (sfloat_optab, SFmode, SImode));
3543       add_libcall (libcall_htab,
3544                    convert_optab_libfunc (sfloat_optab, DFmode, SImode));
3545       add_libcall (libcall_htab,
3546                    convert_optab_libfunc (sfloat_optab, SFmode, DImode));
3547       add_libcall (libcall_htab,
3548                    convert_optab_libfunc (sfloat_optab, DFmode, DImode));
3549       
3550       add_libcall (libcall_htab,
3551                    convert_optab_libfunc (ufloat_optab, SFmode, SImode));
3552       add_libcall (libcall_htab,
3553                    convert_optab_libfunc (ufloat_optab, DFmode, SImode));
3554       add_libcall (libcall_htab,
3555                    convert_optab_libfunc (ufloat_optab, SFmode, DImode));
3556       add_libcall (libcall_htab,
3557                    convert_optab_libfunc (ufloat_optab, DFmode, DImode));
3558
3559       add_libcall (libcall_htab,
3560                    convert_optab_libfunc (sext_optab, SFmode, HFmode));
3561       add_libcall (libcall_htab,
3562                    convert_optab_libfunc (trunc_optab, HFmode, SFmode));
3563       add_libcall (libcall_htab,
3564                    convert_optab_libfunc (sfix_optab, DImode, DFmode));
3565       add_libcall (libcall_htab,
3566                    convert_optab_libfunc (ufix_optab, DImode, DFmode));
3567       add_libcall (libcall_htab,
3568                    convert_optab_libfunc (sfix_optab, DImode, SFmode));
3569       add_libcall (libcall_htab,
3570                    convert_optab_libfunc (ufix_optab, DImode, SFmode));
3571     }
3572
3573   return libcall && htab_find (libcall_htab, libcall) != NULL;
3574 }
3575
3576 rtx
3577 arm_libcall_value (enum machine_mode mode, const_rtx libcall)
3578 {
3579   if (TARGET_AAPCS_BASED && arm_pcs_default != ARM_PCS_AAPCS
3580       && GET_MODE_CLASS (mode) == MODE_FLOAT)
3581     {
3582       /* The following libcalls return their result in integer registers,
3583          even though they return a floating point value.  */
3584       if (arm_libcall_uses_aapcs_base (libcall))
3585         return gen_rtx_REG (mode, ARG_REGISTER(1));
3586
3587     }
3588
3589   return LIBCALL_VALUE (mode);
3590 }
3591
3592 /* Determine the amount of memory needed to store the possible return
3593    registers of an untyped call.  */
3594 int
3595 arm_apply_result_size (void)
3596 {
3597   int size = 16;
3598
3599   if (TARGET_32BIT)
3600     {
3601       if (TARGET_HARD_FLOAT_ABI)
3602         {
3603           if (TARGET_VFP)
3604             size += 32;
3605           if (TARGET_FPA)
3606             size += 12;
3607           if (TARGET_MAVERICK)
3608             size += 8;
3609         }
3610       if (TARGET_IWMMXT_ABI)
3611         size += 8;
3612     }
3613
3614   return size;
3615 }
3616
3617 /* Decide whether TYPE should be returned in memory (true)
3618    or in a register (false).  FNTYPE is the type of the function making
3619    the call.  */
3620 static bool
3621 arm_return_in_memory (const_tree type, const_tree fntype)
3622 {
3623   HOST_WIDE_INT size;
3624
3625   size = int_size_in_bytes (type);  /* Negative if not fixed size.  */
3626
3627   if (TARGET_AAPCS_BASED)
3628     {
3629       /* Simple, non-aggregate types (ie not including vectors and
3630          complex) are always returned in a register (or registers).
3631          We don't care about which register here, so we can short-cut
3632          some of the detail.  */
3633       if (!AGGREGATE_TYPE_P (type)
3634           && TREE_CODE (type) != VECTOR_TYPE
3635           && TREE_CODE (type) != COMPLEX_TYPE)
3636         return false;
3637
3638       /* Any return value that is no larger than one word can be
3639          returned in r0.  */
3640       if (((unsigned HOST_WIDE_INT) size) <= UNITS_PER_WORD)
3641         return false;
3642
3643       /* Check any available co-processors to see if they accept the
3644          type as a register candidate (VFP, for example, can return
3645          some aggregates in consecutive registers).  These aren't
3646          available if the call is variadic.  */
3647       if (aapcs_select_return_coproc (type, fntype) >= 0)
3648         return false;
3649
3650       /* Vector values should be returned using ARM registers, not
3651          memory (unless they're over 16 bytes, which will break since
3652          we only have four call-clobbered registers to play with).  */
3653       if (TREE_CODE (type) == VECTOR_TYPE)
3654         return (size < 0 || size > (4 * UNITS_PER_WORD));
3655
3656       /* The rest go in memory.  */
3657       return true;
3658     }
3659
3660   if (TREE_CODE (type) == VECTOR_TYPE)
3661     return (size < 0 || size > (4 * UNITS_PER_WORD));
3662
3663   if (!AGGREGATE_TYPE_P (type) &&
3664       (TREE_CODE (type) != VECTOR_TYPE))
3665     /* All simple types are returned in registers.  */
3666     return false;
3667
3668   if (arm_abi != ARM_ABI_APCS)
3669     {
3670       /* ATPCS and later return aggregate types in memory only if they are
3671          larger than a word (or are variable size).  */
3672       return (size < 0 || size > UNITS_PER_WORD);
3673     }
3674
3675   /* For the arm-wince targets we choose to be compatible with Microsoft's
3676      ARM and Thumb compilers, which always return aggregates in memory.  */
3677 #ifndef ARM_WINCE
3678   /* All structures/unions bigger than one word are returned in memory.
3679      Also catch the case where int_size_in_bytes returns -1.  In this case
3680      the aggregate is either huge or of variable size, and in either case
3681      we will want to return it via memory and not in a register.  */
3682   if (size < 0 || size > UNITS_PER_WORD)
3683     return true;
3684
3685   if (TREE_CODE (type) == RECORD_TYPE)
3686     {
3687       tree field;
3688
3689       /* For a struct the APCS says that we only return in a register
3690          if the type is 'integer like' and every addressable element
3691          has an offset of zero.  For practical purposes this means
3692          that the structure can have at most one non bit-field element
3693          and that this element must be the first one in the structure.  */
3694
3695       /* Find the first field, ignoring non FIELD_DECL things which will
3696          have been created by C++.  */
3697       for (field = TYPE_FIELDS (type);
3698            field && TREE_CODE (field) != FIELD_DECL;
3699            field = DECL_CHAIN (field))
3700         continue;
3701
3702       if (field == NULL)
3703         return false; /* An empty structure.  Allowed by an extension to ANSI C.  */
3704
3705       /* Check that the first field is valid for returning in a register.  */
3706
3707       /* ... Floats are not allowed */
3708       if (FLOAT_TYPE_P (TREE_TYPE (field)))
3709         return true;
3710
3711       /* ... Aggregates that are not themselves valid for returning in
3712          a register are not allowed.  */
3713       if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
3714         return true;
3715
3716       /* Now check the remaining fields, if any.  Only bitfields are allowed,
3717          since they are not addressable.  */
3718       for (field = DECL_CHAIN (field);
3719            field;
3720            field = DECL_CHAIN (field))
3721         {
3722           if (TREE_CODE (field) != FIELD_DECL)
3723             continue;
3724
3725           if (!DECL_BIT_FIELD_TYPE (field))
3726             return true;
3727         }
3728
3729       return false;
3730     }
3731
3732   if (TREE_CODE (type) == UNION_TYPE)
3733     {
3734       tree field;
3735
3736       /* Unions can be returned in registers if every element is
3737          integral, or can be returned in an integer register.  */
3738       for (field = TYPE_FIELDS (type);
3739            field;
3740            field = DECL_CHAIN (field))
3741         {
3742           if (TREE_CODE (field) != FIELD_DECL)
3743             continue;
3744
3745           if (FLOAT_TYPE_P (TREE_TYPE (field)))
3746             return true;
3747
3748           if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
3749             return true;
3750         }
3751
3752       return false;
3753     }
3754 #endif /* not ARM_WINCE */
3755
3756   /* Return all other types in memory.  */
3757   return true;
3758 }
3759
3760 /* Indicate whether or not words of a double are in big-endian order.  */
3761
3762 int
3763 arm_float_words_big_endian (void)
3764 {
3765   if (TARGET_MAVERICK)
3766     return 0;
3767
3768   /* For FPA, float words are always big-endian.  For VFP, floats words
3769      follow the memory system mode.  */
3770
3771   if (TARGET_FPA)
3772     {
3773       return 1;
3774     }
3775
3776   if (TARGET_VFP)
3777     return (TARGET_BIG_END ? 1 : 0);
3778
3779   return 1;
3780 }
3781
3782 const struct pcs_attribute_arg
3783 {
3784   const char *arg;
3785   enum arm_pcs value;
3786 } pcs_attribute_args[] =
3787   {
3788     {"aapcs", ARM_PCS_AAPCS},
3789     {"aapcs-vfp", ARM_PCS_AAPCS_VFP},
3790 #if 0
3791     /* We could recognize these, but changes would be needed elsewhere
3792      * to implement them.  */
3793     {"aapcs-iwmmxt", ARM_PCS_AAPCS_IWMMXT},
3794     {"atpcs", ARM_PCS_ATPCS},
3795     {"apcs", ARM_PCS_APCS},
3796 #endif
3797     {NULL, ARM_PCS_UNKNOWN}
3798   };
3799
3800 static enum arm_pcs
3801 arm_pcs_from_attribute (tree attr)
3802 {
3803   const struct pcs_attribute_arg *ptr;
3804   const char *arg;
3805
3806   /* Get the value of the argument.  */
3807   if (TREE_VALUE (attr) == NULL_TREE
3808       || TREE_CODE (TREE_VALUE (attr)) != STRING_CST)
3809     return ARM_PCS_UNKNOWN;
3810
3811   arg = TREE_STRING_POINTER (TREE_VALUE (attr));
3812
3813   /* Check it against the list of known arguments.  */
3814   for (ptr = pcs_attribute_args; ptr->arg != NULL; ptr++)
3815     if (streq (arg, ptr->arg))
3816       return ptr->value;
3817
3818   /* An unrecognized interrupt type.  */
3819   return ARM_PCS_UNKNOWN;
3820 }
3821
3822 /* Get the PCS variant to use for this call.  TYPE is the function's type
3823    specification, DECL is the specific declartion.  DECL may be null if
3824    the call could be indirect or if this is a library call.  */
3825 static enum arm_pcs
3826 arm_get_pcs_model (const_tree type, const_tree decl)
3827 {
3828   bool user_convention = false;
3829   enum arm_pcs user_pcs = arm_pcs_default;
3830   tree attr;
3831
3832   gcc_assert (type);
3833
3834   attr = lookup_attribute ("pcs", TYPE_ATTRIBUTES (type));
3835   if (attr)
3836     {
3837       user_pcs = arm_pcs_from_attribute (TREE_VALUE (attr));
3838       user_convention = true;
3839     }
3840
3841   if (TARGET_AAPCS_BASED)
3842     {
3843       /* Detect varargs functions.  These always use the base rules
3844          (no argument is ever a candidate for a co-processor
3845          register).  */
3846       bool base_rules = stdarg_p (type);
3847       
3848       if (user_convention)
3849         {
3850           if (user_pcs > ARM_PCS_AAPCS_LOCAL)
3851             sorry ("non-AAPCS derived PCS variant");
3852           else if (base_rules && user_pcs != ARM_PCS_AAPCS)
3853             error ("variadic functions must use the base AAPCS variant");
3854         }
3855
3856       if (base_rules)
3857         return ARM_PCS_AAPCS;
3858       else if (user_convention)
3859         return user_pcs;
3860       else if (decl && flag_unit_at_a_time)
3861         {
3862           /* Local functions never leak outside this compilation unit,
3863              so we are free to use whatever conventions are
3864              appropriate.  */
3865           /* FIXME: remove CONST_CAST_TREE when cgraph is constified.  */
3866           struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE(decl));
3867           if (i && i->local)
3868             return ARM_PCS_AAPCS_LOCAL;
3869         }
3870     }
3871   else if (user_convention && user_pcs != arm_pcs_default)
3872     sorry ("PCS variant");
3873
3874   /* For everything else we use the target's default.  */
3875   return arm_pcs_default;
3876 }
3877
3878
3879 static void
3880 aapcs_vfp_cum_init (CUMULATIVE_ARGS *pcum  ATTRIBUTE_UNUSED,
3881                     const_tree fntype ATTRIBUTE_UNUSED,
3882                     rtx libcall ATTRIBUTE_UNUSED, 
3883                     const_tree fndecl ATTRIBUTE_UNUSED)
3884 {
3885   /* Record the unallocated VFP registers.  */
3886   pcum->aapcs_vfp_regs_free = (1 << NUM_VFP_ARG_REGS) - 1;
3887   pcum->aapcs_vfp_reg_alloc = 0;
3888 }
3889
3890 /* Walk down the type tree of TYPE counting consecutive base elements.
3891    If *MODEP is VOIDmode, then set it to the first valid floating point
3892    type.  If a non-floating point type is found, or if a floating point
3893    type that doesn't match a non-VOIDmode *MODEP is found, then return -1,
3894    otherwise return the count in the sub-tree.  */
3895 static int
3896 aapcs_vfp_sub_candidate (const_tree type, enum machine_mode *modep)
3897 {
3898   enum machine_mode mode;
3899   HOST_WIDE_INT size;
3900
3901   switch (TREE_CODE (type))
3902     {
3903     case REAL_TYPE:
3904       mode = TYPE_MODE (type);
3905       if (mode != DFmode && mode != SFmode)
3906         return -1;
3907
3908       if (*modep == VOIDmode)
3909         *modep = mode;
3910
3911       if (*modep == mode)
3912         return 1;
3913
3914       break;
3915
3916     case COMPLEX_TYPE:
3917       mode = TYPE_MODE (TREE_TYPE (type));
3918       if (mode != DFmode && mode != SFmode)
3919         return -1;
3920
3921       if (*modep == VOIDmode)
3922         *modep = mode;
3923
3924       if (*modep == mode)
3925         return 2;
3926
3927       break;
3928
3929     case VECTOR_TYPE:
3930       /* Use V2SImode and V4SImode as representatives of all 64-bit
3931          and 128-bit vector types, whether or not those modes are
3932          supported with the present options.  */
3933       size = int_size_in_bytes (type);
3934       switch (size)
3935         {
3936         case 8:
3937           mode = V2SImode;
3938           break;
3939         case 16:
3940           mode = V4SImode;
3941           break;
3942         default:
3943           return -1;
3944         }
3945
3946       if (*modep == VOIDmode)
3947         *modep = mode;
3948
3949       /* Vector modes are considered to be opaque: two vectors are
3950          equivalent for the purposes of being homogeneous aggregates
3951          if they are the same size.  */
3952       if (*modep == mode)
3953         return 1;
3954
3955       break;
3956
3957     case ARRAY_TYPE:
3958       {
3959         int count;
3960         tree index = TYPE_DOMAIN (type);
3961
3962         /* Can't handle incomplete types.  */
3963         if (!COMPLETE_TYPE_P(type))
3964           return -1;
3965
3966         count = aapcs_vfp_sub_candidate (TREE_TYPE (type), modep);
3967         if (count == -1
3968             || !index
3969             || !TYPE_MAX_VALUE (index)
3970             || !host_integerp (TYPE_MAX_VALUE (index), 1)
3971             || !TYPE_MIN_VALUE (index)
3972             || !host_integerp (TYPE_MIN_VALUE (index), 1)
3973             || count < 0)
3974           return -1;
3975
3976         count *= (1 + tree_low_cst (TYPE_MAX_VALUE (index), 1)
3977                       - tree_low_cst (TYPE_MIN_VALUE (index), 1));
3978
3979         /* There must be no padding.  */
3980         if (!host_integerp (TYPE_SIZE (type), 1)
3981             || (tree_low_cst (TYPE_SIZE (type), 1)
3982                 != count * GET_MODE_BITSIZE (*modep)))
3983           return -1;
3984
3985         return count;
3986       }
3987       
3988     case RECORD_TYPE:
3989       {
3990         int count = 0;
3991         int sub_count;
3992         tree field;
3993
3994         /* Can't handle incomplete types.  */
3995         if (!COMPLETE_TYPE_P(type))
3996           return -1;
3997
3998         for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3999           {
4000             if (TREE_CODE (field) != FIELD_DECL)
4001               continue;
4002
4003             sub_count = aapcs_vfp_sub_candidate (TREE_TYPE (field), modep);
4004             if (sub_count < 0)
4005               return -1;
4006             count += sub_count;
4007           }
4008
4009         /* There must be no padding.  */
4010         if (!host_integerp (TYPE_SIZE (type), 1)
4011             || (tree_low_cst (TYPE_SIZE (type), 1)
4012                 != count * GET_MODE_BITSIZE (*modep)))
4013           return -1;
4014
4015         return count;
4016       }
4017
4018     case UNION_TYPE:
4019     case QUAL_UNION_TYPE:
4020       {
4021         /* These aren't very interesting except in a degenerate case.  */
4022         int count = 0;
4023         int sub_count;
4024         tree field;
4025
4026         /* Can't handle incomplete types.  */
4027         if (!COMPLETE_TYPE_P(type))
4028           return -1;
4029
4030         for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4031           {
4032             if (TREE_CODE (field) != FIELD_DECL)
4033               continue;
4034
4035             sub_count = aapcs_vfp_sub_candidate (TREE_TYPE (field), modep);
4036             if (sub_count < 0)
4037               return -1;
4038             count = count > sub_count ? count : sub_count;
4039           }
4040
4041         /* There must be no padding.  */
4042         if (!host_integerp (TYPE_SIZE (type), 1)
4043             || (tree_low_cst (TYPE_SIZE (type), 1)
4044                 != count * GET_MODE_BITSIZE (*modep)))
4045           return -1;
4046
4047         return count;
4048       }
4049
4050     default:
4051       break;
4052     }
4053
4054   return -1;
4055 }
4056
4057 /* Return true if PCS_VARIANT should use VFP registers.  */
4058 static bool
4059 use_vfp_abi (enum arm_pcs pcs_variant, bool is_double)
4060 {
4061   if (pcs_variant == ARM_PCS_AAPCS_VFP)
4062     {
4063       static bool seen_thumb1_vfp = false;
4064
4065       if (TARGET_THUMB1 && !seen_thumb1_vfp)
4066         {
4067           sorry ("Thumb-1 hard-float VFP ABI");
4068           /* sorry() is not immediately fatal, so only display this once.  */
4069           seen_thumb1_vfp = true;
4070         }
4071
4072       return true;
4073     }
4074
4075   if (pcs_variant != ARM_PCS_AAPCS_LOCAL)
4076     return false;
4077
4078   return (TARGET_32BIT && TARGET_VFP && TARGET_HARD_FLOAT &&
4079           (TARGET_VFP_DOUBLE || !is_double));
4080 }
4081
4082 static bool
4083 aapcs_vfp_is_call_or_return_candidate (enum arm_pcs pcs_variant,
4084                                        enum machine_mode mode, const_tree type,
4085                                        enum machine_mode *base_mode, int *count)
4086 {
4087   enum machine_mode new_mode = VOIDmode;
4088
4089   if (GET_MODE_CLASS (mode) == MODE_FLOAT
4090       || GET_MODE_CLASS (mode) == MODE_VECTOR_INT
4091       || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
4092     {
4093       *count = 1;
4094       new_mode = mode;
4095     }
4096   else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
4097     {
4098       *count = 2;
4099       new_mode = (mode == DCmode ? DFmode : SFmode);
4100     }
4101   else if (type && (mode == BLKmode || TREE_CODE (type) == VECTOR_TYPE))
4102     {
4103       int ag_count = aapcs_vfp_sub_candidate (type, &new_mode);
4104
4105       if (ag_count > 0 && ag_count <= 4)
4106         *count = ag_count;
4107       else
4108         return false;
4109     }
4110   else
4111     return false;
4112
4113
4114   if (!use_vfp_abi (pcs_variant, ARM_NUM_REGS (new_mode) > 1))
4115     return false;
4116
4117   *base_mode = new_mode;
4118   return true;
4119 }
4120
4121 static bool
4122 aapcs_vfp_is_return_candidate (enum arm_pcs pcs_variant,
4123                                enum machine_mode mode, const_tree type)
4124 {
4125   int count ATTRIBUTE_UNUSED;
4126   enum machine_mode ag_mode ATTRIBUTE_UNUSED;
4127
4128   if (!use_vfp_abi (pcs_variant, false))
4129     return false;
4130   return aapcs_vfp_is_call_or_return_candidate (pcs_variant, mode, type,
4131                                                 &ag_mode, &count);
4132 }
4133
4134 static bool
4135 aapcs_vfp_is_call_candidate (CUMULATIVE_ARGS *pcum, enum machine_mode mode, 
4136                              const_tree type)
4137 {
4138   if (!use_vfp_abi (pcum->pcs_variant, false))
4139     return false;
4140
4141   return aapcs_vfp_is_call_or_return_candidate (pcum->pcs_variant, mode, type,
4142                                                 &pcum->aapcs_vfp_rmode,
4143                                                 &pcum->aapcs_vfp_rcount);
4144 }
4145
4146 static bool
4147 aapcs_vfp_allocate (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4148                     const_tree type  ATTRIBUTE_UNUSED)
4149 {
4150   int shift = GET_MODE_SIZE (pcum->aapcs_vfp_rmode) / GET_MODE_SIZE (SFmode);
4151   unsigned mask = (1 << (shift * pcum->aapcs_vfp_rcount)) - 1;
4152   int regno;
4153   
4154   for (regno = 0; regno < NUM_VFP_ARG_REGS; regno += shift)
4155     if (((pcum->aapcs_vfp_regs_free >> regno) & mask) == mask)
4156       {
4157         pcum->aapcs_vfp_reg_alloc = mask << regno;
4158         if (mode == BLKmode || (mode == TImode && !TARGET_NEON))
4159           {
4160             int i;
4161             int rcount = pcum->aapcs_vfp_rcount;
4162             int rshift = shift;
4163             enum machine_mode rmode = pcum->aapcs_vfp_rmode;
4164             rtx par;
4165             if (!TARGET_NEON)
4166               {
4167                 /* Avoid using unsupported vector modes.  */
4168                 if (rmode == V2SImode)
4169                   rmode = DImode;
4170                 else if (rmode == V4SImode)
4171                   {
4172                     rmode = DImode;
4173                     rcount *= 2;
4174                     rshift /= 2;
4175                   }
4176               }
4177             par = gen_rtx_PARALLEL (mode, rtvec_alloc (rcount));
4178             for (i = 0; i < rcount; i++)
4179               {
4180                 rtx tmp = gen_rtx_REG (rmode, 
4181                                        FIRST_VFP_REGNUM + regno + i * rshift);
4182                 tmp = gen_rtx_EXPR_LIST
4183                   (VOIDmode, tmp, 
4184                    GEN_INT (i * GET_MODE_SIZE (rmode)));
4185                 XVECEXP (par, 0, i) = tmp;
4186               }
4187
4188             pcum->aapcs_reg = par;
4189           }
4190         else
4191           pcum->aapcs_reg = gen_rtx_REG (mode, FIRST_VFP_REGNUM + regno);
4192         return true;
4193       }
4194   return false;
4195 }
4196
4197 static rtx
4198 aapcs_vfp_allocate_return_reg (enum arm_pcs pcs_variant ATTRIBUTE_UNUSED,
4199                                enum machine_mode mode,
4200                                const_tree type ATTRIBUTE_UNUSED)
4201 {
4202   if (!use_vfp_abi (pcs_variant, false))
4203     return false;
4204
4205   if (mode == BLKmode || (mode == TImode && !TARGET_NEON))
4206     {
4207       int count;
4208       enum machine_mode ag_mode;
4209       int i;
4210       rtx par;
4211       int shift;
4212       
4213       aapcs_vfp_is_call_or_return_candidate (pcs_variant, mode, type,
4214                                              &ag_mode, &count);
4215
4216       if (!TARGET_NEON)
4217         {
4218           if (ag_mode == V2SImode)
4219             ag_mode = DImode;
4220           else if (ag_mode == V4SImode)
4221             {
4222               ag_mode = DImode;
4223               count *= 2;
4224             }
4225         }
4226       shift = GET_MODE_SIZE(ag_mode) / GET_MODE_SIZE(SFmode);
4227       par = gen_rtx_PARALLEL (mode, rtvec_alloc (count));
4228       for (i = 0; i < count; i++)
4229         {
4230           rtx tmp = gen_rtx_REG (ag_mode, FIRST_VFP_REGNUM + i * shift);
4231           tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, 
4232                                    GEN_INT (i * GET_MODE_SIZE (ag_mode)));
4233           XVECEXP (par, 0, i) = tmp;
4234         }
4235
4236       return par;
4237     }
4238
4239   return gen_rtx_REG (mode, FIRST_VFP_REGNUM);
4240 }
4241
4242 static void
4243 aapcs_vfp_advance (CUMULATIVE_ARGS *pcum  ATTRIBUTE_UNUSED,
4244                    enum machine_mode mode  ATTRIBUTE_UNUSED,
4245                    const_tree type  ATTRIBUTE_UNUSED)
4246 {
4247   pcum->aapcs_vfp_regs_free &= ~pcum->aapcs_vfp_reg_alloc;
4248   pcum->aapcs_vfp_reg_alloc = 0;
4249   return;
4250 }
4251
4252 #define AAPCS_CP(X)                             \
4253   {                                             \
4254     aapcs_ ## X ## _cum_init,                   \
4255     aapcs_ ## X ## _is_call_candidate,          \
4256     aapcs_ ## X ## _allocate,                   \
4257     aapcs_ ## X ## _is_return_candidate,        \
4258     aapcs_ ## X ## _allocate_return_reg,        \
4259     aapcs_ ## X ## _advance                     \
4260   }
4261
4262 /* Table of co-processors that can be used to pass arguments in
4263    registers.  Idealy no arugment should be a candidate for more than
4264    one co-processor table entry, but the table is processed in order
4265    and stops after the first match.  If that entry then fails to put
4266    the argument into a co-processor register, the argument will go on
4267    the stack.  */
4268 static struct 
4269 {
4270   /* Initialize co-processor related state in CUMULATIVE_ARGS structure.  */
4271   void (*cum_init) (CUMULATIVE_ARGS *, const_tree, rtx, const_tree);
4272
4273   /* Return true if an argument of mode MODE (or type TYPE if MODE is
4274      BLKmode) is a candidate for this co-processor's registers; this
4275      function should ignore any position-dependent state in
4276      CUMULATIVE_ARGS and only use call-type dependent information.  */
4277   bool (*is_call_candidate) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4278
4279   /* Return true if the argument does get a co-processor register; it
4280      should set aapcs_reg to an RTX of the register allocated as is
4281      required for a return from FUNCTION_ARG.  */
4282   bool (*allocate) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4283
4284   /* Return true if a result of mode MODE (or type TYPE if MODE is
4285      BLKmode) is can be returned in this co-processor's registers.  */
4286   bool (*is_return_candidate) (enum arm_pcs, enum machine_mode, const_tree);
4287
4288   /* Allocate and return an RTX element to hold the return type of a
4289      call, this routine must not fail and will only be called if
4290      is_return_candidate returned true with the same parameters.  */
4291   rtx (*allocate_return_reg) (enum arm_pcs, enum machine_mode, const_tree);
4292
4293   /* Finish processing this argument and prepare to start processing
4294      the next one.  */
4295   void (*advance) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4296 } aapcs_cp_arg_layout[ARM_NUM_COPROC_SLOTS] =
4297   {
4298     AAPCS_CP(vfp)
4299   };
4300
4301 #undef AAPCS_CP
4302
4303 static int
4304 aapcs_select_call_coproc (CUMULATIVE_ARGS *pcum, enum machine_mode mode, 
4305                           const_tree type)
4306 {
4307   int i;
4308
4309   for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4310     if (aapcs_cp_arg_layout[i].is_call_candidate (pcum, mode, type))
4311       return i;
4312
4313   return -1;
4314 }
4315
4316 static int
4317 aapcs_select_return_coproc (const_tree type, const_tree fntype)
4318 {
4319   /* We aren't passed a decl, so we can't check that a call is local.
4320      However, it isn't clear that that would be a win anyway, since it
4321      might limit some tail-calling opportunities.  */
4322   enum arm_pcs pcs_variant;
4323
4324   if (fntype)
4325     {
4326       const_tree fndecl = NULL_TREE;
4327
4328       if (TREE_CODE (fntype) == FUNCTION_DECL)
4329         {
4330           fndecl = fntype;
4331           fntype = TREE_TYPE (fntype);
4332         }
4333
4334       pcs_variant = arm_get_pcs_model (fntype, fndecl);
4335     }
4336   else
4337     pcs_variant = arm_pcs_default;
4338
4339   if (pcs_variant != ARM_PCS_AAPCS)
4340     {
4341       int i;
4342
4343       for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4344         if (aapcs_cp_arg_layout[i].is_return_candidate (pcs_variant, 
4345                                                         TYPE_MODE (type),
4346                                                         type))
4347           return i;
4348     }
4349   return -1;
4350 }
4351
4352 static rtx
4353 aapcs_allocate_return_reg (enum machine_mode mode, const_tree type,
4354                            const_tree fntype)
4355 {
4356   /* We aren't passed a decl, so we can't check that a call is local.
4357      However, it isn't clear that that would be a win anyway, since it
4358      might limit some tail-calling opportunities.  */
4359   enum arm_pcs pcs_variant;
4360   int unsignedp ATTRIBUTE_UNUSED;
4361
4362   if (fntype)
4363     {
4364       const_tree fndecl = NULL_TREE;
4365
4366       if (TREE_CODE (fntype) == FUNCTION_DECL)
4367         {
4368           fndecl = fntype;
4369           fntype = TREE_TYPE (fntype);
4370         }
4371
4372       pcs_variant = arm_get_pcs_model (fntype, fndecl);
4373     }
4374   else
4375     pcs_variant = arm_pcs_default;
4376
4377   /* Promote integer types.  */
4378   if (type && INTEGRAL_TYPE_P (type))
4379     mode = arm_promote_function_mode (type, mode, &unsignedp, fntype, 1);
4380
4381   if (pcs_variant != ARM_PCS_AAPCS)
4382     {
4383       int i;
4384
4385       for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4386         if (aapcs_cp_arg_layout[i].is_return_candidate (pcs_variant, mode,
4387                                                         type))
4388           return aapcs_cp_arg_layout[i].allocate_return_reg (pcs_variant,
4389                                                              mode, type);
4390     }
4391
4392   /* Promotes small structs returned in a register to full-word size
4393      for big-endian AAPCS.  */
4394   if (type && arm_return_in_msb (type))
4395     {
4396       HOST_WIDE_INT size = int_size_in_bytes (type);
4397       if (size % UNITS_PER_WORD != 0)
4398         {
4399           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
4400           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
4401         }
4402     }
4403
4404   return gen_rtx_REG (mode, R0_REGNUM);
4405 }
4406
4407 rtx
4408 aapcs_libcall_value (enum machine_mode mode)
4409 {
4410   return aapcs_allocate_return_reg (mode, NULL_TREE, NULL_TREE);
4411 }
4412
4413 /* Lay out a function argument using the AAPCS rules.  The rule
4414    numbers referred to here are those in the AAPCS.  */
4415 static void
4416 aapcs_layout_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4417                   const_tree type, bool named)
4418 {
4419   int nregs, nregs2;
4420   int ncrn;
4421
4422   /* We only need to do this once per argument.  */
4423   if (pcum->aapcs_arg_processed)
4424     return;
4425
4426   pcum->aapcs_arg_processed = true;
4427
4428   /* Special case: if named is false then we are handling an incoming
4429      anonymous argument which is on the stack.  */
4430   if (!named)
4431     return;
4432   
4433   /* Is this a potential co-processor register candidate?  */
4434   if (pcum->pcs_variant != ARM_PCS_AAPCS)
4435     {
4436       int slot = aapcs_select_call_coproc (pcum, mode, type);
4437       pcum->aapcs_cprc_slot = slot;
4438
4439       /* We don't have to apply any of the rules from part B of the
4440          preparation phase, these are handled elsewhere in the
4441          compiler.  */
4442
4443       if (slot >= 0)
4444         {
4445           /* A Co-processor register candidate goes either in its own
4446              class of registers or on the stack.  */
4447           if (!pcum->aapcs_cprc_failed[slot])
4448             {
4449               /* C1.cp - Try to allocate the argument to co-processor
4450                  registers.  */
4451               if (aapcs_cp_arg_layout[slot].allocate (pcum, mode, type))
4452                 return;
4453
4454               /* C2.cp - Put the argument on the stack and note that we
4455                  can't assign any more candidates in this slot.  We also
4456                  need to note that we have allocated stack space, so that
4457                  we won't later try to split a non-cprc candidate between
4458                  core registers and the stack.  */
4459               pcum->aapcs_cprc_failed[slot] = true;
4460               pcum->can_split = false;
4461             }
4462
4463           /* We didn't get a register, so this argument goes on the
4464              stack.  */
4465           gcc_assert (pcum->can_split == false);
4466           return;
4467         }
4468     }
4469
4470   /* C3 - For double-word aligned arguments, round the NCRN up to the
4471      next even number.  */
4472   ncrn = pcum->aapcs_ncrn;
4473   if ((ncrn & 1) && arm_needs_doubleword_align (mode, type))
4474     ncrn++;
4475
4476   nregs = ARM_NUM_REGS2(mode, type);
4477
4478   /* Sigh, this test should really assert that nregs > 0, but a GCC
4479      extension allows empty structs and then gives them empty size; it
4480      then allows such a structure to be passed by value.  For some of
4481      the code below we have to pretend that such an argument has
4482      non-zero size so that we 'locate' it correctly either in
4483      registers or on the stack.  */
4484   gcc_assert (nregs >= 0);
4485
4486   nregs2 = nregs ? nregs : 1;
4487
4488   /* C4 - Argument fits entirely in core registers.  */
4489   if (ncrn + nregs2 <= NUM_ARG_REGS)
4490     {
4491       pcum->aapcs_reg = gen_rtx_REG (mode, ncrn);
4492       pcum->aapcs_next_ncrn = ncrn + nregs;
4493       return;
4494     }
4495
4496   /* C5 - Some core registers left and there are no arguments already
4497      on the stack: split this argument between the remaining core
4498      registers and the stack.  */
4499   if (ncrn < NUM_ARG_REGS && pcum->can_split)
4500     {
4501       pcum->aapcs_reg = gen_rtx_REG (mode, ncrn);
4502       pcum->aapcs_next_ncrn = NUM_ARG_REGS;
4503       pcum->aapcs_partial = (NUM_ARG_REGS - ncrn) * UNITS_PER_WORD;
4504       return;
4505     }
4506
4507   /* C6 - NCRN is set to 4.  */
4508   pcum->aapcs_next_ncrn = NUM_ARG_REGS;
4509
4510   /* C7,C8 - arugment goes on the stack.  We have nothing to do here.  */
4511   return;
4512 }
4513
4514 /* Initialize a variable CUM of type CUMULATIVE_ARGS
4515    for a call to a function whose data type is FNTYPE.
4516    For a library call, FNTYPE is NULL.  */
4517 void
4518 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
4519                           rtx libname,
4520                           tree fndecl ATTRIBUTE_UNUSED)
4521 {
4522   /* Long call handling.  */
4523   if (fntype)
4524     pcum->pcs_variant = arm_get_pcs_model (fntype, fndecl);
4525   else
4526     pcum->pcs_variant = arm_pcs_default;
4527
4528   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4529     {
4530       if (arm_libcall_uses_aapcs_base (libname))
4531         pcum->pcs_variant = ARM_PCS_AAPCS;
4532  
4533       pcum->aapcs_ncrn = pcum->aapcs_next_ncrn = 0;
4534       pcum->aapcs_reg = NULL_RTX;
4535       pcum->aapcs_partial = 0;
4536       pcum->aapcs_arg_processed = false;
4537       pcum->aapcs_cprc_slot = -1;
4538       pcum->can_split = true;
4539
4540       if (pcum->pcs_variant != ARM_PCS_AAPCS)
4541         {
4542           int i;
4543
4544           for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4545             {
4546               pcum->aapcs_cprc_failed[i] = false;
4547               aapcs_cp_arg_layout[i].cum_init (pcum, fntype, libname, fndecl);
4548             }
4549         }
4550       return;
4551     }
4552
4553   /* Legacy ABIs */
4554
4555   /* On the ARM, the offset starts at 0.  */
4556   pcum->nregs = 0;
4557   pcum->iwmmxt_nregs = 0;
4558   pcum->can_split = true;
4559
4560   /* Varargs vectors are treated the same as long long.
4561      named_count avoids having to change the way arm handles 'named' */
4562   pcum->named_count = 0;
4563   pcum->nargs = 0;
4564
4565   if (TARGET_REALLY_IWMMXT && fntype)
4566     {
4567       tree fn_arg;
4568
4569       for (fn_arg = TYPE_ARG_TYPES (fntype);
4570            fn_arg;
4571            fn_arg = TREE_CHAIN (fn_arg))
4572         pcum->named_count += 1;
4573
4574       if (! pcum->named_count)
4575         pcum->named_count = INT_MAX;
4576     }
4577 }
4578
4579
4580 /* Return true if mode/type need doubleword alignment.  */
4581 static bool
4582 arm_needs_doubleword_align (enum machine_mode mode, const_tree type)
4583 {
4584   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
4585           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
4586 }
4587
4588
4589 /* Determine where to put an argument to a function.
4590    Value is zero to push the argument on the stack,
4591    or a hard register in which to store the argument.
4592
4593    MODE is the argument's machine mode.
4594    TYPE is the data type of the argument (as a tree).
4595     This is null for libcalls where that information may
4596     not be available.
4597    CUM is a variable of type CUMULATIVE_ARGS which gives info about
4598     the preceding args and about the function being called.
4599    NAMED is nonzero if this argument is a named parameter
4600     (otherwise it is an extra parameter matching an ellipsis).
4601
4602    On the ARM, normally the first 16 bytes are passed in registers r0-r3; all
4603    other arguments are passed on the stack.  If (NAMED == 0) (which happens
4604    only in assign_parms, since TARGET_SETUP_INCOMING_VARARGS is
4605    defined), say it is passed in the stack (function_prologue will
4606    indeed make it pass in the stack if necessary).  */
4607
4608 static rtx
4609 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4610                   const_tree type, bool named)
4611 {
4612   int nregs;
4613
4614   /* Handle the special case quickly.  Pick an arbitrary value for op2 of
4615      a call insn (op3 of a call_value insn).  */
4616   if (mode == VOIDmode)
4617     return const0_rtx;
4618
4619   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4620     {
4621       aapcs_layout_arg (pcum, mode, type, named);
4622       return pcum->aapcs_reg;
4623     }
4624
4625   /* Varargs vectors are treated the same as long long.
4626      named_count avoids having to change the way arm handles 'named' */
4627   if (TARGET_IWMMXT_ABI
4628       && arm_vector_mode_supported_p (mode)
4629       && pcum->named_count > pcum->nargs + 1)
4630     {
4631       if (pcum->iwmmxt_nregs <= 9)
4632         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
4633       else
4634         {
4635           pcum->can_split = false;
4636           return NULL_RTX;
4637         }
4638     }
4639
4640   /* Put doubleword aligned quantities in even register pairs.  */
4641   if (pcum->nregs & 1
4642       && ARM_DOUBLEWORD_ALIGN
4643       && arm_needs_doubleword_align (mode, type))
4644     pcum->nregs++;
4645
4646   /* Only allow splitting an arg between regs and memory if all preceding
4647      args were allocated to regs.  For args passed by reference we only count
4648      the reference pointer.  */
4649   if (pcum->can_split)
4650     nregs = 1;
4651   else
4652     nregs = ARM_NUM_REGS2 (mode, type);
4653
4654   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
4655     return NULL_RTX;
4656
4657   return gen_rtx_REG (mode, pcum->nregs);
4658 }
4659
4660 static unsigned int
4661 arm_function_arg_boundary (enum machine_mode mode, const_tree type)
4662 {
4663   return (ARM_DOUBLEWORD_ALIGN && arm_needs_doubleword_align (mode, type)
4664           ? DOUBLEWORD_ALIGNMENT
4665           : PARM_BOUNDARY);
4666 }
4667
4668 static int
4669 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4670                        tree type, bool named)
4671 {
4672   int nregs = pcum->nregs;
4673
4674   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4675     {
4676       aapcs_layout_arg (pcum, mode, type, named);
4677       return pcum->aapcs_partial;
4678     }
4679
4680   if (TARGET_IWMMXT_ABI && arm_vector_mode_supported_p (mode))
4681     return 0;
4682
4683   if (NUM_ARG_REGS > nregs
4684       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
4685       && pcum->can_split)
4686     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
4687
4688   return 0;
4689 }
4690
4691 /* Update the data in PCUM to advance over an argument
4692    of mode MODE and data type TYPE.
4693    (TYPE is null for libcalls where that information may not be available.)  */
4694
4695 static void
4696 arm_function_arg_advance (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4697                           const_tree type, bool named)
4698 {
4699   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4700     {
4701       aapcs_layout_arg (pcum, mode, type, named);
4702
4703       if (pcum->aapcs_cprc_slot >= 0)
4704         {
4705           aapcs_cp_arg_layout[pcum->aapcs_cprc_slot].advance (pcum, mode,
4706                                                               type);
4707           pcum->aapcs_cprc_slot = -1;
4708         }
4709
4710       /* Generic stuff.  */
4711       pcum->aapcs_arg_processed = false;
4712       pcum->aapcs_ncrn = pcum->aapcs_next_ncrn;
4713       pcum->aapcs_reg = NULL_RTX;
4714       pcum->aapcs_partial = 0;
4715     }
4716   else
4717     {
4718       pcum->nargs += 1;
4719       if (arm_vector_mode_supported_p (mode)
4720           && pcum->named_count > pcum->nargs
4721           && TARGET_IWMMXT_ABI)
4722         pcum->iwmmxt_nregs += 1;
4723       else
4724         pcum->nregs += ARM_NUM_REGS2 (mode, type);
4725     }
4726 }
4727
4728 /* Variable sized types are passed by reference.  This is a GCC
4729    extension to the ARM ABI.  */
4730
4731 static bool
4732 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
4733                        enum machine_mode mode ATTRIBUTE_UNUSED,
4734                        const_tree type, bool named ATTRIBUTE_UNUSED)
4735 {
4736   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
4737 }
4738 \f
4739 /* Encode the current state of the #pragma [no_]long_calls.  */
4740 typedef enum
4741 {
4742   OFF,          /* No #pragma [no_]long_calls is in effect.  */
4743   LONG,         /* #pragma long_calls is in effect.  */
4744   SHORT         /* #pragma no_long_calls is in effect.  */
4745 } arm_pragma_enum;
4746
4747 static arm_pragma_enum arm_pragma_long_calls = OFF;
4748
4749 void
4750 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
4751 {
4752   arm_pragma_long_calls = LONG;
4753 }
4754
4755 void
4756 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
4757 {
4758   arm_pragma_long_calls = SHORT;
4759 }
4760
4761 void
4762 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
4763 {
4764   arm_pragma_long_calls = OFF;
4765 }
4766 \f
4767 /* Handle an attribute requiring a FUNCTION_DECL;
4768    arguments as in struct attribute_spec.handler.  */
4769 static tree
4770 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
4771                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4772 {
4773   if (TREE_CODE (*node) != FUNCTION_DECL)
4774     {
4775       warning (OPT_Wattributes, "%qE attribute only applies to functions",
4776                name);
4777       *no_add_attrs = true;
4778     }
4779
4780   return NULL_TREE;
4781 }
4782
4783 /* Handle an "interrupt" or "isr" attribute;
4784    arguments as in struct attribute_spec.handler.  */
4785 static tree
4786 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
4787                           bool *no_add_attrs)
4788 {
4789   if (DECL_P (*node))
4790     {
4791       if (TREE_CODE (*node) != FUNCTION_DECL)
4792         {
4793           warning (OPT_Wattributes, "%qE attribute only applies to functions",
4794                    name);
4795           *no_add_attrs = true;
4796         }
4797       /* FIXME: the argument if any is checked for type attributes;
4798          should it be checked for decl ones?  */
4799     }
4800   else
4801     {
4802       if (TREE_CODE (*node) == FUNCTION_TYPE
4803           || TREE_CODE (*node) == METHOD_TYPE)
4804         {
4805           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
4806             {
4807               warning (OPT_Wattributes, "%qE attribute ignored",
4808                        name);
4809               *no_add_attrs = true;
4810             }
4811         }
4812       else if (TREE_CODE (*node) == POINTER_TYPE
4813                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
4814                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
4815                && arm_isr_value (args) != ARM_FT_UNKNOWN)
4816         {
4817           *node = build_variant_type_copy (*node);
4818           TREE_TYPE (*node) = build_type_attribute_variant
4819             (TREE_TYPE (*node),
4820              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
4821           *no_add_attrs = true;
4822         }
4823       else
4824         {
4825           /* Possibly pass this attribute on from the type to a decl.  */
4826           if (flags & ((int) ATTR_FLAG_DECL_NEXT
4827                        | (int) ATTR_FLAG_FUNCTION_NEXT
4828                        | (int) ATTR_FLAG_ARRAY_NEXT))
4829             {
4830               *no_add_attrs = true;
4831               return tree_cons (name, args, NULL_TREE);
4832             }
4833           else
4834             {
4835               warning (OPT_Wattributes, "%qE attribute ignored",
4836                        name);
4837             }
4838         }
4839     }
4840
4841   return NULL_TREE;
4842 }
4843
4844 /* Handle a "pcs" attribute; arguments as in struct
4845    attribute_spec.handler.  */
4846 static tree
4847 arm_handle_pcs_attribute (tree *node ATTRIBUTE_UNUSED, tree name, tree args,
4848                           int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4849 {
4850   if (arm_pcs_from_attribute (args) == ARM_PCS_UNKNOWN)
4851     {
4852       warning (OPT_Wattributes, "%qE attribute ignored", name);
4853       *no_add_attrs = true;
4854     }
4855   return NULL_TREE;
4856 }
4857
4858 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
4859 /* Handle the "notshared" attribute.  This attribute is another way of
4860    requesting hidden visibility.  ARM's compiler supports
4861    "__declspec(notshared)"; we support the same thing via an
4862    attribute.  */
4863
4864 static tree
4865 arm_handle_notshared_attribute (tree *node,
4866                                 tree name ATTRIBUTE_UNUSED,
4867                                 tree args ATTRIBUTE_UNUSED,
4868                                 int flags ATTRIBUTE_UNUSED,
4869                                 bool *no_add_attrs)
4870 {
4871   tree decl = TYPE_NAME (*node);
4872
4873   if (decl)
4874     {
4875       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
4876       DECL_VISIBILITY_SPECIFIED (decl) = 1;
4877       *no_add_attrs = false;
4878     }
4879   return NULL_TREE;
4880 }
4881 #endif
4882
4883 /* Return 0 if the attributes for two types are incompatible, 1 if they
4884    are compatible, and 2 if they are nearly compatible (which causes a
4885    warning to be generated).  */
4886 static int
4887 arm_comp_type_attributes (const_tree type1, const_tree type2)
4888 {
4889   int l1, l2, s1, s2;
4890
4891   /* Check for mismatch of non-default calling convention.  */
4892   if (TREE_CODE (type1) != FUNCTION_TYPE)
4893     return 1;
4894
4895   /* Check for mismatched call attributes.  */
4896   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
4897   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
4898   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
4899   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
4900
4901   /* Only bother to check if an attribute is defined.  */
4902   if (l1 | l2 | s1 | s2)
4903     {
4904       /* If one type has an attribute, the other must have the same attribute.  */
4905       if ((l1 != l2) || (s1 != s2))
4906         return 0;
4907
4908       /* Disallow mixed attributes.  */
4909       if ((l1 & s2) || (l2 & s1))
4910         return 0;
4911     }
4912
4913   /* Check for mismatched ISR attribute.  */
4914   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
4915   if (! l1)
4916     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
4917   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
4918   if (! l2)
4919     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
4920   if (l1 != l2)
4921     return 0;
4922
4923   return 1;
4924 }
4925
4926 /*  Assigns default attributes to newly defined type.  This is used to
4927     set short_call/long_call attributes for function types of
4928     functions defined inside corresponding #pragma scopes.  */
4929 static void
4930 arm_set_default_type_attributes (tree type)
4931 {
4932   /* Add __attribute__ ((long_call)) to all functions, when
4933      inside #pragma long_calls or __attribute__ ((short_call)),
4934      when inside #pragma no_long_calls.  */
4935   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
4936     {
4937       tree type_attr_list, attr_name;
4938       type_attr_list = TYPE_ATTRIBUTES (type);
4939
4940       if (arm_pragma_long_calls == LONG)
4941         attr_name = get_identifier ("long_call");
4942       else if (arm_pragma_long_calls == SHORT)
4943         attr_name = get_identifier ("short_call");
4944       else
4945         return;
4946
4947       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
4948       TYPE_ATTRIBUTES (type) = type_attr_list;
4949     }
4950 }
4951 \f
4952 /* Return true if DECL is known to be linked into section SECTION.  */
4953
4954 static bool
4955 arm_function_in_section_p (tree decl, section *section)
4956 {
4957   /* We can only be certain about functions defined in the same
4958      compilation unit.  */
4959   if (!TREE_STATIC (decl))
4960     return false;
4961
4962   /* Make sure that SYMBOL always binds to the definition in this
4963      compilation unit.  */
4964   if (!targetm.binds_local_p (decl))
4965     return false;
4966
4967   /* If DECL_SECTION_NAME is set, assume it is trustworthy.  */
4968   if (!DECL_SECTION_NAME (decl))
4969     {
4970       /* Make sure that we will not create a unique section for DECL.  */
4971       if (flag_function_sections || DECL_ONE_ONLY (decl))
4972         return false;
4973     }
4974
4975   return function_section (decl) == section;
4976 }
4977
4978 /* Return nonzero if a 32-bit "long_call" should be generated for
4979    a call from the current function to DECL.  We generate a long_call
4980    if the function:
4981
4982         a.  has an __attribute__((long call))
4983      or b.  is within the scope of a #pragma long_calls
4984      or c.  the -mlong-calls command line switch has been specified
4985
4986    However we do not generate a long call if the function:
4987
4988         d.  has an __attribute__ ((short_call))
4989      or e.  is inside the scope of a #pragma no_long_calls
4990      or f.  is defined in the same section as the current function.  */
4991
4992 bool
4993 arm_is_long_call_p (tree decl)
4994 {
4995   tree attrs;
4996
4997   if (!decl)
4998     return TARGET_LONG_CALLS;
4999
5000   attrs = TYPE_ATTRIBUTES (TREE_TYPE (decl));
5001   if (lookup_attribute ("short_call", attrs))
5002     return false;
5003
5004   /* For "f", be conservative, and only cater for cases in which the
5005      whole of the current function is placed in the same section.  */
5006   if (!flag_reorder_blocks_and_partition
5007       && TREE_CODE (decl) == FUNCTION_DECL
5008       && arm_function_in_section_p (decl, current_function_section ()))
5009     return false;
5010
5011   if (lookup_attribute ("long_call", attrs))
5012     return true;
5013
5014   return TARGET_LONG_CALLS;
5015 }
5016
5017 /* Return nonzero if it is ok to make a tail-call to DECL.  */
5018 static bool
5019 arm_function_ok_for_sibcall (tree decl, tree exp)
5020 {
5021   unsigned long func_type;
5022
5023   if (cfun->machine->sibcall_blocked)
5024     return false;
5025
5026   /* Never tailcall something for which we have no decl, or if we
5027      are generating code for Thumb-1.  */
5028   if (decl == NULL || TARGET_THUMB1)
5029     return false;
5030
5031   /* The PIC register is live on entry to VxWorks PLT entries, so we
5032      must make the call before restoring the PIC register.  */
5033   if (TARGET_VXWORKS_RTP && flag_pic && !targetm.binds_local_p (decl))
5034     return false;
5035
5036   /* Cannot tail-call to long calls, since these are out of range of
5037      a branch instruction.  */
5038   if (arm_is_long_call_p (decl))
5039     return false;
5040
5041   /* If we are interworking and the function is not declared static
5042      then we can't tail-call it unless we know that it exists in this
5043      compilation unit (since it might be a Thumb routine).  */
5044   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
5045     return false;
5046
5047   func_type = arm_current_func_type ();
5048   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
5049   if (IS_INTERRUPT (func_type))
5050     return false;
5051
5052   if (!VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
5053     {
5054       /* Check that the return value locations are the same.  For
5055          example that we aren't returning a value from the sibling in
5056          a VFP register but then need to transfer it to a core
5057          register.  */
5058       rtx a, b;
5059
5060       a = arm_function_value (TREE_TYPE (exp), decl, false);
5061       b = arm_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
5062                               cfun->decl, false);
5063       if (!rtx_equal_p (a, b))
5064         return false;
5065     }
5066
5067   /* Never tailcall if function may be called with a misaligned SP.  */
5068   if (IS_STACKALIGN (func_type))
5069     return false;
5070
5071   /* Everything else is ok.  */
5072   return true;
5073 }
5074
5075 \f
5076 /* Addressing mode support functions.  */
5077
5078 /* Return nonzero if X is a legitimate immediate operand when compiling
5079    for PIC.  We know that X satisfies CONSTANT_P and flag_pic is true.  */
5080 int
5081 legitimate_pic_operand_p (rtx x)
5082 {
5083   if (GET_CODE (x) == SYMBOL_REF
5084       || (GET_CODE (x) == CONST
5085           && GET_CODE (XEXP (x, 0)) == PLUS
5086           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
5087     return 0;
5088
5089   return 1;
5090 }
5091
5092 /* Record that the current function needs a PIC register.  Initialize
5093    cfun->machine->pic_reg if we have not already done so.  */
5094
5095 static void
5096 require_pic_register (void)
5097 {
5098   /* A lot of the logic here is made obscure by the fact that this
5099      routine gets called as part of the rtx cost estimation process.
5100      We don't want those calls to affect any assumptions about the real
5101      function; and further, we can't call entry_of_function() until we
5102      start the real expansion process.  */
5103   if (!crtl->uses_pic_offset_table)
5104     {
5105       gcc_assert (can_create_pseudo_p ());
5106       if (arm_pic_register != INVALID_REGNUM)
5107         {
5108           if (!cfun->machine->pic_reg)
5109             cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
5110
5111           /* Play games to avoid marking the function as needing pic
5112              if we are being called as part of the cost-estimation
5113              process.  */
5114           if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
5115             crtl->uses_pic_offset_table = 1;
5116         }
5117       else
5118         {
5119           rtx seq;
5120
5121           if (!cfun->machine->pic_reg)
5122             cfun->machine->pic_reg = gen_reg_rtx (Pmode);
5123
5124           /* Play games to avoid marking the function as needing pic
5125              if we are being called as part of the cost-estimation
5126              process.  */
5127           if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
5128             {
5129               crtl->uses_pic_offset_table = 1;
5130               start_sequence ();
5131
5132               arm_load_pic_register (0UL);
5133
5134               seq = get_insns ();
5135               end_sequence ();
5136               /* We can be called during expansion of PHI nodes, where
5137                  we can't yet emit instructions directly in the final
5138                  insn stream.  Queue the insns on the entry edge, they will
5139                  be committed after everything else is expanded.  */
5140               insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
5141             }
5142         }
5143     }
5144 }
5145
5146 rtx
5147 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
5148 {
5149   if (GET_CODE (orig) == SYMBOL_REF
5150       || GET_CODE (orig) == LABEL_REF)
5151     {
5152       rtx insn;
5153
5154       if (reg == 0)
5155         {
5156           gcc_assert (can_create_pseudo_p ());
5157           reg = gen_reg_rtx (Pmode);
5158         }
5159
5160       /* VxWorks does not impose a fixed gap between segments; the run-time
5161          gap can be different from the object-file gap.  We therefore can't
5162          use GOTOFF unless we are absolutely sure that the symbol is in the
5163          same segment as the GOT.  Unfortunately, the flexibility of linker
5164          scripts means that we can't be sure of that in general, so assume
5165          that GOTOFF is never valid on VxWorks.  */
5166       if ((GET_CODE (orig) == LABEL_REF
5167            || (GET_CODE (orig) == SYMBOL_REF &&
5168                SYMBOL_REF_LOCAL_P (orig)))
5169           && NEED_GOT_RELOC
5170           && !TARGET_VXWORKS_RTP)
5171         insn = arm_pic_static_addr (orig, reg);
5172       else
5173         {
5174           rtx pat;
5175           rtx mem;
5176
5177           /* If this function doesn't have a pic register, create one now.  */
5178           require_pic_register ();
5179
5180           pat = gen_calculate_pic_address (reg, cfun->machine->pic_reg, orig);
5181
5182           /* Make the MEM as close to a constant as possible.  */
5183           mem = SET_SRC (pat);
5184           gcc_assert (MEM_P (mem) && !MEM_VOLATILE_P (mem));
5185           MEM_READONLY_P (mem) = 1;
5186           MEM_NOTRAP_P (mem) = 1;
5187
5188           insn = emit_insn (pat);
5189         }
5190
5191       /* Put a REG_EQUAL note on this insn, so that it can be optimized
5192          by loop.  */
5193       set_unique_reg_note (insn, REG_EQUAL, orig);
5194
5195       return reg;
5196     }
5197   else if (GET_CODE (orig) == CONST)
5198     {
5199       rtx base, offset;
5200
5201       if (GET_CODE (XEXP (orig, 0)) == PLUS
5202           && XEXP (XEXP (orig, 0), 0) == cfun->machine->pic_reg)
5203         return orig;
5204
5205       /* Handle the case where we have: const (UNSPEC_TLS).  */
5206       if (GET_CODE (XEXP (orig, 0)) == UNSPEC
5207           && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
5208         return orig;
5209
5210       /* Handle the case where we have:
5211          const (plus (UNSPEC_TLS) (ADDEND)).  The ADDEND must be a
5212          CONST_INT.  */
5213       if (GET_CODE (XEXP (orig, 0)) == PLUS
5214           && GET_CODE (XEXP (XEXP (orig, 0), 0)) == UNSPEC
5215           && XINT (XEXP (XEXP (orig, 0), 0), 1) == UNSPEC_TLS)
5216         {
5217           gcc_assert (GET_CODE (XEXP (XEXP (orig, 0), 1)) == CONST_INT);
5218           return orig;
5219         }
5220
5221       if (reg == 0)
5222         {
5223           gcc_assert (can_create_pseudo_p ());
5224           reg = gen_reg_rtx (Pmode);
5225         }
5226
5227       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
5228
5229       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
5230       offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
5231                                        base == reg ? 0 : reg);
5232
5233       if (GET_CODE (offset) == CONST_INT)
5234         {
5235           /* The base register doesn't really matter, we only want to
5236              test the index for the appropriate mode.  */
5237           if (!arm_legitimate_index_p (mode, offset, SET, 0))
5238             {
5239               gcc_assert (can_create_pseudo_p ());
5240               offset = force_reg (Pmode, offset);
5241             }
5242
5243           if (GET_CODE (offset) == CONST_INT)
5244             return plus_constant (base, INTVAL (offset));
5245         }
5246
5247       if (GET_MODE_SIZE (mode) > 4
5248           && (GET_MODE_CLASS (mode) == MODE_INT
5249               || TARGET_SOFT_FLOAT))
5250         {
5251           emit_insn (gen_addsi3 (reg, base, offset));
5252           return reg;
5253         }
5254
5255       return gen_rtx_PLUS (Pmode, base, offset);
5256     }
5257
5258   return orig;
5259 }
5260
5261
5262 /* Find a spare register to use during the prolog of a function.  */
5263
5264 static int
5265 thumb_find_work_register (unsigned long pushed_regs_mask)
5266 {
5267   int reg;
5268
5269   /* Check the argument registers first as these are call-used.  The
5270      register allocation order means that sometimes r3 might be used
5271      but earlier argument registers might not, so check them all.  */
5272   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
5273     if (!df_regs_ever_live_p (reg))
5274       return reg;
5275
5276   /* Before going on to check the call-saved registers we can try a couple
5277      more ways of deducing that r3 is available.  The first is when we are
5278      pushing anonymous arguments onto the stack and we have less than 4
5279      registers worth of fixed arguments(*).  In this case r3 will be part of
5280      the variable argument list and so we can be sure that it will be
5281      pushed right at the start of the function.  Hence it will be available
5282      for the rest of the prologue.
5283      (*): ie crtl->args.pretend_args_size is greater than 0.  */
5284   if (cfun->machine->uses_anonymous_args
5285       && crtl->args.pretend_args_size > 0)
5286     return LAST_ARG_REGNUM;
5287
5288   /* The other case is when we have fixed arguments but less than 4 registers
5289      worth.  In this case r3 might be used in the body of the function, but
5290      it is not being used to convey an argument into the function.  In theory
5291      we could just check crtl->args.size to see how many bytes are
5292      being passed in argument registers, but it seems that it is unreliable.
5293      Sometimes it will have the value 0 when in fact arguments are being
5294      passed.  (See testcase execute/20021111-1.c for an example).  So we also
5295      check the args_info.nregs field as well.  The problem with this field is
5296      that it makes no allowances for arguments that are passed to the
5297      function but which are not used.  Hence we could miss an opportunity
5298      when a function has an unused argument in r3.  But it is better to be
5299      safe than to be sorry.  */
5300   if (! cfun->machine->uses_anonymous_args
5301       && crtl->args.size >= 0
5302       && crtl->args.size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
5303       && crtl->args.info.nregs < 4)
5304     return LAST_ARG_REGNUM;
5305
5306   /* Otherwise look for a call-saved register that is going to be pushed.  */
5307   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
5308     if (pushed_regs_mask & (1 << reg))
5309       return reg;
5310
5311   if (TARGET_THUMB2)
5312     {
5313       /* Thumb-2 can use high regs.  */
5314       for (reg = FIRST_HI_REGNUM; reg < 15; reg ++)
5315         if (pushed_regs_mask & (1 << reg))
5316           return reg;
5317     }
5318   /* Something went wrong - thumb_compute_save_reg_mask()
5319      should have arranged for a suitable register to be pushed.  */
5320   gcc_unreachable ();
5321 }
5322
5323 static GTY(()) int pic_labelno;
5324
5325 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
5326    low register.  */
5327
5328 void
5329 arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
5330 {
5331   rtx l1, labelno, pic_tmp, pic_rtx, pic_reg;
5332
5333   if (crtl->uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
5334     return;
5335
5336   gcc_assert (flag_pic);
5337
5338   pic_reg = cfun->machine->pic_reg;
5339   if (TARGET_VXWORKS_RTP)
5340     {
5341       pic_rtx = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE);
5342       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
5343       emit_insn (gen_pic_load_addr_32bit (pic_reg, pic_rtx));
5344
5345       emit_insn (gen_rtx_SET (Pmode, pic_reg, gen_rtx_MEM (Pmode, pic_reg)));
5346
5347       pic_tmp = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
5348       emit_insn (gen_pic_offset_arm (pic_reg, pic_reg, pic_tmp));
5349     }
5350   else
5351     {
5352       /* We use an UNSPEC rather than a LABEL_REF because this label
5353          never appears in the code stream.  */
5354
5355       labelno = GEN_INT (pic_labelno++);
5356       l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
5357       l1 = gen_rtx_CONST (VOIDmode, l1);
5358
5359       /* On the ARM the PC register contains 'dot + 8' at the time of the
5360          addition, on the Thumb it is 'dot + 4'.  */
5361       pic_rtx = plus_constant (l1, TARGET_ARM ? 8 : 4);
5362       pic_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, pic_rtx),
5363                                 UNSPEC_GOTSYM_OFF);
5364       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
5365
5366       if (TARGET_32BIT)
5367         {
5368           emit_insn (gen_pic_load_addr_32bit (pic_reg, pic_rtx));
5369           if (TARGET_ARM)
5370             emit_insn (gen_pic_add_dot_plus_eight (pic_reg, pic_reg, labelno));
5371           else
5372             emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
5373         }
5374       else /* TARGET_THUMB1 */
5375         {
5376           if (arm_pic_register != INVALID_REGNUM
5377               && REGNO (pic_reg) > LAST_LO_REGNUM)
5378             {
5379               /* We will have pushed the pic register, so we should always be
5380                  able to find a work register.  */
5381               pic_tmp = gen_rtx_REG (SImode,
5382                                      thumb_find_work_register (saved_regs));
5383               emit_insn (gen_pic_load_addr_thumb1 (pic_tmp, pic_rtx));
5384               emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
5385             }
5386           else
5387             emit_insn (gen_pic_load_addr_thumb1 (pic_reg, pic_rtx));
5388           emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
5389         }
5390     }
5391
5392   /* Need to emit this whether or not we obey regdecls,
5393      since setjmp/longjmp can cause life info to screw up.  */
5394   emit_use (pic_reg);
5395 }
5396
5397 /* Generate code to load the address of a static var when flag_pic is set.  */
5398 static rtx
5399 arm_pic_static_addr (rtx orig, rtx reg)
5400 {
5401   rtx l1, labelno, offset_rtx, insn;
5402
5403   gcc_assert (flag_pic);
5404
5405   /* We use an UNSPEC rather than a LABEL_REF because this label
5406      never appears in the code stream.  */
5407   labelno = GEN_INT (pic_labelno++);
5408   l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
5409   l1 = gen_rtx_CONST (VOIDmode, l1);
5410
5411   /* On the ARM the PC register contains 'dot + 8' at the time of the
5412      addition, on the Thumb it is 'dot + 4'.  */
5413   offset_rtx = plus_constant (l1, TARGET_ARM ? 8 : 4);
5414   offset_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, orig, offset_rtx),
5415                                UNSPEC_SYMBOL_OFFSET);
5416   offset_rtx = gen_rtx_CONST (Pmode, offset_rtx);
5417
5418   if (TARGET_32BIT)
5419     {
5420       emit_insn (gen_pic_load_addr_32bit (reg, offset_rtx));
5421       if (TARGET_ARM)
5422         insn = emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
5423       else
5424         insn = emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5425     }
5426   else /* TARGET_THUMB1 */
5427     {
5428       emit_insn (gen_pic_load_addr_thumb1 (reg, offset_rtx));
5429       insn = emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5430     }
5431
5432   return insn;
5433 }
5434
5435 /* Return nonzero if X is valid as an ARM state addressing register.  */
5436 static int
5437 arm_address_register_rtx_p (rtx x, int strict_p)
5438 {
5439   int regno;
5440
5441   if (GET_CODE (x) != REG)
5442     return 0;
5443
5444   regno = REGNO (x);
5445
5446   if (strict_p)
5447     return ARM_REGNO_OK_FOR_BASE_P (regno);
5448
5449   return (regno <= LAST_ARM_REGNUM
5450           || regno >= FIRST_PSEUDO_REGISTER
5451           || regno == FRAME_POINTER_REGNUM
5452           || regno == ARG_POINTER_REGNUM);
5453 }
5454
5455 /* Return TRUE if this rtx is the difference of a symbol and a label,
5456    and will reduce to a PC-relative relocation in the object file.
5457    Expressions like this can be left alone when generating PIC, rather
5458    than forced through the GOT.  */
5459 static int
5460 pcrel_constant_p (rtx x)
5461 {
5462   if (GET_CODE (x) == MINUS)
5463     return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
5464
5465   return FALSE;
5466 }
5467
5468 /* Return true if X will surely end up in an index register after next
5469    splitting pass.  */
5470 static bool
5471 will_be_in_index_register (const_rtx x)
5472 {
5473   /* arm.md: calculate_pic_address will split this into a register.  */
5474   return GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_SYM;
5475 }
5476
5477 /* Return nonzero if X is a valid ARM state address operand.  */
5478 int
5479 arm_legitimate_address_outer_p (enum machine_mode mode, rtx x, RTX_CODE outer,
5480                                 int strict_p)
5481 {
5482   bool use_ldrd;
5483   enum rtx_code code = GET_CODE (x);
5484
5485   if (arm_address_register_rtx_p (x, strict_p))
5486     return 1;
5487
5488   use_ldrd = (TARGET_LDRD
5489               && (mode == DImode
5490                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
5491
5492   if (code == POST_INC || code == PRE_DEC
5493       || ((code == PRE_INC || code == POST_DEC)
5494           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
5495     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
5496
5497   else if ((code == POST_MODIFY || code == PRE_MODIFY)
5498            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
5499            && GET_CODE (XEXP (x, 1)) == PLUS
5500            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5501     {
5502       rtx addend = XEXP (XEXP (x, 1), 1);
5503
5504       /* Don't allow ldrd post increment by register because it's hard
5505          to fixup invalid register choices.  */
5506       if (use_ldrd
5507           && GET_CODE (x) == POST_MODIFY
5508           && GET_CODE (addend) == REG)
5509         return 0;
5510
5511       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
5512               && arm_legitimate_index_p (mode, addend, outer, strict_p));
5513     }
5514
5515   /* After reload constants split into minipools will have addresses
5516      from a LABEL_REF.  */
5517   else if (reload_completed
5518            && (code == LABEL_REF
5519                || (code == CONST
5520                    && GET_CODE (XEXP (x, 0)) == PLUS
5521                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5522                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5523     return 1;
5524
5525   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
5526     return 0;
5527
5528   else if (code == PLUS)
5529     {
5530       rtx xop0 = XEXP (x, 0);
5531       rtx xop1 = XEXP (x, 1);
5532
5533       return ((arm_address_register_rtx_p (xop0, strict_p)
5534                && ((GET_CODE(xop1) == CONST_INT
5535                     && arm_legitimate_index_p (mode, xop1, outer, strict_p))
5536                    || (!strict_p && will_be_in_index_register (xop1))))
5537               || (arm_address_register_rtx_p (xop1, strict_p)
5538                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
5539     }
5540
5541 #if 0
5542   /* Reload currently can't handle MINUS, so disable this for now */
5543   else if (GET_CODE (x) == MINUS)
5544     {
5545       rtx xop0 = XEXP (x, 0);
5546       rtx xop1 = XEXP (x, 1);
5547
5548       return (arm_address_register_rtx_p (xop0, strict_p)
5549               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
5550     }
5551 #endif
5552
5553   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
5554            && code == SYMBOL_REF
5555            && CONSTANT_POOL_ADDRESS_P (x)
5556            && ! (flag_pic
5557                  && symbol_mentioned_p (get_pool_constant (x))
5558                  && ! pcrel_constant_p (get_pool_constant (x))))
5559     return 1;
5560
5561   return 0;
5562 }
5563
5564 /* Return nonzero if X is a valid Thumb-2 address operand.  */
5565 static int
5566 thumb2_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
5567 {
5568   bool use_ldrd;
5569   enum rtx_code code = GET_CODE (x);
5570   
5571   if (arm_address_register_rtx_p (x, strict_p))
5572     return 1;
5573
5574   use_ldrd = (TARGET_LDRD
5575               && (mode == DImode
5576                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
5577
5578   if (code == POST_INC || code == PRE_DEC
5579       || ((code == PRE_INC || code == POST_DEC)
5580           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
5581     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
5582
5583   else if ((code == POST_MODIFY || code == PRE_MODIFY)
5584            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
5585            && GET_CODE (XEXP (x, 1)) == PLUS
5586            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5587     {
5588       /* Thumb-2 only has autoincrement by constant.  */
5589       rtx addend = XEXP (XEXP (x, 1), 1);
5590       HOST_WIDE_INT offset;
5591
5592       if (GET_CODE (addend) != CONST_INT)
5593         return 0;
5594
5595       offset = INTVAL(addend);
5596       if (GET_MODE_SIZE (mode) <= 4)
5597         return (offset > -256 && offset < 256);
5598       
5599       return (use_ldrd && offset > -1024 && offset < 1024
5600               && (offset & 3) == 0);
5601     }
5602
5603   /* After reload constants split into minipools will have addresses
5604      from a LABEL_REF.  */
5605   else if (reload_completed
5606            && (code == LABEL_REF
5607                || (code == CONST
5608                    && GET_CODE (XEXP (x, 0)) == PLUS
5609                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5610                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5611     return 1;
5612
5613   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
5614     return 0;
5615
5616   else if (code == PLUS)
5617     {
5618       rtx xop0 = XEXP (x, 0);
5619       rtx xop1 = XEXP (x, 1);
5620
5621       return ((arm_address_register_rtx_p (xop0, strict_p)
5622                && (thumb2_legitimate_index_p (mode, xop1, strict_p)
5623                    || (!strict_p && will_be_in_index_register (xop1))))
5624               || (arm_address_register_rtx_p (xop1, strict_p)
5625                   && thumb2_legitimate_index_p (mode, xop0, strict_p)));
5626     }
5627
5628   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
5629            && code == SYMBOL_REF
5630            && CONSTANT_POOL_ADDRESS_P (x)
5631            && ! (flag_pic
5632                  && symbol_mentioned_p (get_pool_constant (x))
5633                  && ! pcrel_constant_p (get_pool_constant (x))))
5634     return 1;
5635
5636   return 0;
5637 }
5638
5639 /* Return nonzero if INDEX is valid for an address index operand in
5640    ARM state.  */
5641 static int
5642 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
5643                         int strict_p)
5644 {
5645   HOST_WIDE_INT range;
5646   enum rtx_code code = GET_CODE (index);
5647
5648   /* Standard coprocessor addressing modes.  */
5649   if (TARGET_HARD_FLOAT
5650       && (TARGET_FPA || TARGET_MAVERICK)
5651       && (GET_MODE_CLASS (mode) == MODE_FLOAT
5652           || (TARGET_MAVERICK && mode == DImode)))
5653     return (code == CONST_INT && INTVAL (index) < 1024
5654             && INTVAL (index) > -1024
5655             && (INTVAL (index) & 3) == 0);
5656
5657   /* For quad modes, we restrict the constant offset to be slightly less
5658      than what the instruction format permits.  We do this because for
5659      quad mode moves, we will actually decompose them into two separate
5660      double-mode reads or writes.  INDEX must therefore be a valid
5661      (double-mode) offset and so should INDEX+8.  */
5662   if (TARGET_NEON && VALID_NEON_QREG_MODE (mode))
5663     return (code == CONST_INT
5664             && INTVAL (index) < 1016
5665             && INTVAL (index) > -1024
5666             && (INTVAL (index) & 3) == 0);
5667
5668   /* We have no such constraint on double mode offsets, so we permit the
5669      full range of the instruction format.  */
5670   if (TARGET_NEON && VALID_NEON_DREG_MODE (mode))
5671     return (code == CONST_INT
5672             && INTVAL (index) < 1024
5673             && INTVAL (index) > -1024
5674             && (INTVAL (index) & 3) == 0);
5675
5676   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
5677     return (code == CONST_INT
5678             && INTVAL (index) < 1024
5679             && INTVAL (index) > -1024
5680             && (INTVAL (index) & 3) == 0);
5681
5682   if (arm_address_register_rtx_p (index, strict_p)
5683       && (GET_MODE_SIZE (mode) <= 4))
5684     return 1;
5685
5686   if (mode == DImode || mode == DFmode)
5687     {
5688       if (code == CONST_INT)
5689         {
5690           HOST_WIDE_INT val = INTVAL (index);
5691
5692           if (TARGET_LDRD)
5693             return val > -256 && val < 256;
5694           else
5695             return val > -4096 && val < 4092;
5696         }
5697
5698       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
5699     }
5700
5701   if (GET_MODE_SIZE (mode) <= 4
5702       && ! (arm_arch4
5703             && (mode == HImode
5704                 || mode == HFmode
5705                 || (mode == QImode && outer == SIGN_EXTEND))))
5706     {
5707       if (code == MULT)
5708         {
5709           rtx xiop0 = XEXP (index, 0);
5710           rtx xiop1 = XEXP (index, 1);
5711
5712           return ((arm_address_register_rtx_p (xiop0, strict_p)
5713                    && power_of_two_operand (xiop1, SImode))
5714                   || (arm_address_register_rtx_p (xiop1, strict_p)
5715                       && power_of_two_operand (xiop0, SImode)));
5716         }
5717       else if (code == LSHIFTRT || code == ASHIFTRT
5718                || code == ASHIFT || code == ROTATERT)
5719         {
5720           rtx op = XEXP (index, 1);
5721
5722           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
5723                   && GET_CODE (op) == CONST_INT
5724                   && INTVAL (op) > 0
5725                   && INTVAL (op) <= 31);
5726         }
5727     }
5728
5729   /* For ARM v4 we may be doing a sign-extend operation during the
5730      load.  */
5731   if (arm_arch4)
5732     {
5733       if (mode == HImode
5734           || mode == HFmode
5735           || (outer == SIGN_EXTEND && mode == QImode))
5736         range = 256;
5737       else
5738         range = 4096;
5739     }
5740   else
5741     range = (mode == HImode || mode == HFmode) ? 4095 : 4096;
5742
5743   return (code == CONST_INT
5744           && INTVAL (index) < range
5745           && INTVAL (index) > -range);
5746 }
5747
5748 /* Return true if OP is a valid index scaling factor for Thumb-2 address
5749    index operand.  i.e. 1, 2, 4 or 8.  */
5750 static bool
5751 thumb2_index_mul_operand (rtx op)
5752 {
5753   HOST_WIDE_INT val;
5754   
5755   if (GET_CODE(op) != CONST_INT)
5756     return false;
5757
5758   val = INTVAL(op);
5759   return (val == 1 || val == 2 || val == 4 || val == 8);
5760 }
5761   
5762 /* Return nonzero if INDEX is a valid Thumb-2 address index operand.  */
5763 static int
5764 thumb2_legitimate_index_p (enum machine_mode mode, rtx index, int strict_p)
5765 {
5766   enum rtx_code code = GET_CODE (index);
5767
5768   /* ??? Combine arm and thumb2 coprocessor addressing modes.  */
5769   /* Standard coprocessor addressing modes.  */
5770   if (TARGET_HARD_FLOAT
5771       && (TARGET_FPA || TARGET_MAVERICK)
5772       && (GET_MODE_CLASS (mode) == MODE_FLOAT
5773           || (TARGET_MAVERICK && mode == DImode)))
5774     return (code == CONST_INT && INTVAL (index) < 1024
5775             && INTVAL (index) > -1024
5776             && (INTVAL (index) & 3) == 0);
5777
5778   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
5779     {
5780       /* For DImode assume values will usually live in core regs
5781          and only allow LDRD addressing modes.  */
5782       if (!TARGET_LDRD || mode != DImode)
5783         return (code == CONST_INT
5784                 && INTVAL (index) < 1024
5785                 && INTVAL (index) > -1024
5786                 && (INTVAL (index) & 3) == 0);
5787     }
5788
5789   /* For quad modes, we restrict the constant offset to be slightly less
5790      than what the instruction format permits.  We do this because for
5791      quad mode moves, we will actually decompose them into two separate
5792      double-mode reads or writes.  INDEX must therefore be a valid
5793      (double-mode) offset and so should INDEX+8.  */
5794   if (TARGET_NEON && VALID_NEON_QREG_MODE (mode))
5795     return (code == CONST_INT
5796             && INTVAL (index) < 1016
5797             && INTVAL (index) > -1024
5798             && (INTVAL (index) & 3) == 0);
5799
5800   /* We have no such constraint on double mode offsets, so we permit the
5801      full range of the instruction format.  */
5802   if (TARGET_NEON && VALID_NEON_DREG_MODE (mode))
5803     return (code == CONST_INT
5804             && INTVAL (index) < 1024
5805             && INTVAL (index) > -1024
5806             && (INTVAL (index) & 3) == 0);
5807
5808   if (arm_address_register_rtx_p (index, strict_p)
5809       && (GET_MODE_SIZE (mode) <= 4))
5810     return 1;
5811
5812   if (mode == DImode || mode == DFmode)
5813     {
5814       if (code == CONST_INT)
5815         {
5816           HOST_WIDE_INT val = INTVAL (index);
5817           /* ??? Can we assume ldrd for thumb2?  */
5818           /* Thumb-2 ldrd only has reg+const addressing modes.  */
5819           /* ldrd supports offsets of +-1020.
5820              However the ldr fallback does not.  */
5821           return val > -256 && val < 256 && (val & 3) == 0;
5822         }
5823       else
5824         return 0;
5825     }
5826
5827   if (code == MULT)
5828     {
5829       rtx xiop0 = XEXP (index, 0);
5830       rtx xiop1 = XEXP (index, 1);
5831
5832       return ((arm_address_register_rtx_p (xiop0, strict_p)
5833                && thumb2_index_mul_operand (xiop1))
5834               || (arm_address_register_rtx_p (xiop1, strict_p)
5835                   && thumb2_index_mul_operand (xiop0)));
5836     }
5837   else if (code == ASHIFT)
5838     {
5839       rtx op = XEXP (index, 1);
5840
5841       return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
5842               && GET_CODE (op) == CONST_INT
5843               && INTVAL (op) > 0
5844               && INTVAL (op) <= 3);
5845     }
5846
5847   return (code == CONST_INT
5848           && INTVAL (index) < 4096
5849           && INTVAL (index) > -256);
5850 }
5851
5852 /* Return nonzero if X is valid as a 16-bit Thumb state base register.  */
5853 static int
5854 thumb1_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
5855 {
5856   int regno;
5857
5858   if (GET_CODE (x) != REG)
5859     return 0;
5860
5861   regno = REGNO (x);
5862
5863   if (strict_p)
5864     return THUMB1_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
5865
5866   return (regno <= LAST_LO_REGNUM
5867           || regno > LAST_VIRTUAL_REGISTER
5868           || regno == FRAME_POINTER_REGNUM
5869           || (GET_MODE_SIZE (mode) >= 4
5870               && (regno == STACK_POINTER_REGNUM
5871                   || regno >= FIRST_PSEUDO_REGISTER
5872                   || x == hard_frame_pointer_rtx
5873                   || x == arg_pointer_rtx)));
5874 }
5875
5876 /* Return nonzero if x is a legitimate index register.  This is the case
5877    for any base register that can access a QImode object.  */
5878 inline static int
5879 thumb1_index_register_rtx_p (rtx x, int strict_p)
5880 {
5881   return thumb1_base_register_rtx_p (x, QImode, strict_p);
5882 }
5883
5884 /* Return nonzero if x is a legitimate 16-bit Thumb-state address.
5885
5886    The AP may be eliminated to either the SP or the FP, so we use the
5887    least common denominator, e.g. SImode, and offsets from 0 to 64.
5888
5889    ??? Verify whether the above is the right approach.
5890
5891    ??? Also, the FP may be eliminated to the SP, so perhaps that
5892    needs special handling also.
5893
5894    ??? Look at how the mips16 port solves this problem.  It probably uses
5895    better ways to solve some of these problems.
5896
5897    Although it is not incorrect, we don't accept QImode and HImode
5898    addresses based on the frame pointer or arg pointer until the
5899    reload pass starts.  This is so that eliminating such addresses
5900    into stack based ones won't produce impossible code.  */
5901 static int
5902 thumb1_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
5903 {
5904   /* ??? Not clear if this is right.  Experiment.  */
5905   if (GET_MODE_SIZE (mode) < 4
5906       && !(reload_in_progress || reload_completed)
5907       && (reg_mentioned_p (frame_pointer_rtx, x)
5908           || reg_mentioned_p (arg_pointer_rtx, x)
5909           || reg_mentioned_p (virtual_incoming_args_rtx, x)
5910           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
5911           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
5912           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
5913     return 0;
5914
5915   /* Accept any base register.  SP only in SImode or larger.  */
5916   else if (thumb1_base_register_rtx_p (x, mode, strict_p))
5917     return 1;
5918
5919   /* This is PC relative data before arm_reorg runs.  */
5920   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
5921            && GET_CODE (x) == SYMBOL_REF
5922            && CONSTANT_POOL_ADDRESS_P (x) && !flag_pic)
5923     return 1;
5924
5925   /* This is PC relative data after arm_reorg runs.  */
5926   else if ((GET_MODE_SIZE (mode) >= 4 || mode == HFmode)
5927            && reload_completed
5928            && (GET_CODE (x) == LABEL_REF
5929                || (GET_CODE (x) == CONST
5930                    && GET_CODE (XEXP (x, 0)) == PLUS
5931                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5932                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5933     return 1;
5934
5935   /* Post-inc indexing only supported for SImode and larger.  */
5936   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
5937            && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p))
5938     return 1;
5939
5940   else if (GET_CODE (x) == PLUS)
5941     {
5942       /* REG+REG address can be any two index registers.  */
5943       /* We disallow FRAME+REG addressing since we know that FRAME
5944          will be replaced with STACK, and SP relative addressing only
5945          permits SP+OFFSET.  */
5946       if (GET_MODE_SIZE (mode) <= 4
5947           && XEXP (x, 0) != frame_pointer_rtx
5948           && XEXP (x, 1) != frame_pointer_rtx
5949           && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
5950           && (thumb1_index_register_rtx_p (XEXP (x, 1), strict_p)
5951               || (!strict_p && will_be_in_index_register (XEXP (x, 1)))))
5952         return 1;
5953
5954       /* REG+const has 5-7 bit offset for non-SP registers.  */
5955       else if ((thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
5956                 || XEXP (x, 0) == arg_pointer_rtx)
5957                && GET_CODE (XEXP (x, 1)) == CONST_INT
5958                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
5959         return 1;
5960
5961       /* REG+const has 10-bit offset for SP, but only SImode and
5962          larger is supported.  */
5963       /* ??? Should probably check for DI/DFmode overflow here
5964          just like GO_IF_LEGITIMATE_OFFSET does.  */
5965       else if (GET_CODE (XEXP (x, 0)) == REG
5966                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
5967                && GET_MODE_SIZE (mode) >= 4
5968                && GET_CODE (XEXP (x, 1)) == CONST_INT
5969                && INTVAL (XEXP (x, 1)) >= 0
5970                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
5971                && (INTVAL (XEXP (x, 1)) & 3) == 0)
5972         return 1;
5973
5974       else if (GET_CODE (XEXP (x, 0)) == REG
5975                && (REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
5976                    || REGNO (XEXP (x, 0)) == ARG_POINTER_REGNUM
5977                    || (REGNO (XEXP (x, 0)) >= FIRST_VIRTUAL_REGISTER
5978                        && REGNO (XEXP (x, 0))
5979                           <= LAST_VIRTUAL_POINTER_REGISTER))
5980                && GET_MODE_SIZE (mode) >= 4
5981                && GET_CODE (XEXP (x, 1)) == CONST_INT
5982                && (INTVAL (XEXP (x, 1)) & 3) == 0)
5983         return 1;
5984     }
5985
5986   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
5987            && GET_MODE_SIZE (mode) == 4
5988            && GET_CODE (x) == SYMBOL_REF
5989            && CONSTANT_POOL_ADDRESS_P (x)
5990            && ! (flag_pic
5991                  && symbol_mentioned_p (get_pool_constant (x))
5992                  && ! pcrel_constant_p (get_pool_constant (x))))
5993     return 1;
5994
5995   return 0;
5996 }
5997
5998 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
5999    instruction of mode MODE.  */
6000 int
6001 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
6002 {
6003   switch (GET_MODE_SIZE (mode))
6004     {
6005     case 1:
6006       return val >= 0 && val < 32;
6007
6008     case 2:
6009       return val >= 0 && val < 64 && (val & 1) == 0;
6010
6011     default:
6012       return (val >= 0
6013               && (val + GET_MODE_SIZE (mode)) <= 128
6014               && (val & 3) == 0);
6015     }
6016 }
6017
6018 bool
6019 arm_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
6020 {
6021   if (TARGET_ARM)
6022     return arm_legitimate_address_outer_p (mode, x, SET, strict_p);
6023   else if (TARGET_THUMB2)
6024     return thumb2_legitimate_address_p (mode, x, strict_p);
6025   else /* if (TARGET_THUMB1) */
6026     return thumb1_legitimate_address_p (mode, x, strict_p);
6027 }
6028
6029 /* Build the SYMBOL_REF for __tls_get_addr.  */
6030
6031 static GTY(()) rtx tls_get_addr_libfunc;
6032
6033 static rtx
6034 get_tls_get_addr (void)
6035 {
6036   if (!tls_get_addr_libfunc)
6037     tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
6038   return tls_get_addr_libfunc;
6039 }
6040
6041 static rtx
6042 arm_load_tp (rtx target)
6043 {
6044   if (!target)
6045     target = gen_reg_rtx (SImode);
6046
6047   if (TARGET_HARD_TP)
6048     {
6049       /* Can return in any reg.  */
6050       emit_insn (gen_load_tp_hard (target));
6051     }
6052   else
6053     {
6054       /* Always returned in r0.  Immediately copy the result into a pseudo,
6055          otherwise other uses of r0 (e.g. setting up function arguments) may
6056          clobber the value.  */
6057
6058       rtx tmp;
6059
6060       emit_insn (gen_load_tp_soft ());
6061
6062       tmp = gen_rtx_REG (SImode, 0);
6063       emit_move_insn (target, tmp);
6064     }
6065   return target;
6066 }
6067
6068 static rtx
6069 load_tls_operand (rtx x, rtx reg)
6070 {
6071   rtx tmp;
6072
6073   if (reg == NULL_RTX)
6074     reg = gen_reg_rtx (SImode);
6075
6076   tmp = gen_rtx_CONST (SImode, x);
6077
6078   emit_move_insn (reg, tmp);
6079
6080   return reg;
6081 }
6082
6083 static rtx
6084 arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
6085 {
6086   rtx insns, label, labelno, sum;
6087
6088   start_sequence ();
6089
6090   labelno = GEN_INT (pic_labelno++);
6091   label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
6092   label = gen_rtx_CONST (VOIDmode, label);
6093
6094   sum = gen_rtx_UNSPEC (Pmode,
6095                         gen_rtvec (4, x, GEN_INT (reloc), label,
6096                                    GEN_INT (TARGET_ARM ? 8 : 4)),
6097                         UNSPEC_TLS);
6098   reg = load_tls_operand (sum, reg);
6099
6100   if (TARGET_ARM)
6101     emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
6102   else if (TARGET_THUMB2)
6103     emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
6104   else /* TARGET_THUMB1 */
6105     emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
6106
6107   *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX, LCT_PURE, /* LCT_CONST?  */
6108                                      Pmode, 1, reg, Pmode);
6109
6110   insns = get_insns ();
6111   end_sequence ();
6112
6113   return insns;
6114 }
6115
6116 rtx
6117 legitimize_tls_address (rtx x, rtx reg)
6118 {
6119   rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
6120   unsigned int model = SYMBOL_REF_TLS_MODEL (x);
6121
6122   switch (model)
6123     {
6124     case TLS_MODEL_GLOBAL_DYNAMIC:
6125       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
6126       dest = gen_reg_rtx (Pmode);
6127       emit_libcall_block (insns, dest, ret, x);
6128       return dest;
6129
6130     case TLS_MODEL_LOCAL_DYNAMIC:
6131       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
6132
6133       /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
6134          share the LDM result with other LD model accesses.  */
6135       eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
6136                             UNSPEC_TLS);
6137       dest = gen_reg_rtx (Pmode);
6138       emit_libcall_block (insns, dest, ret, eqv);
6139
6140       /* Load the addend.  */
6141       addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (TLS_LDO32)),
6142                                UNSPEC_TLS);
6143       addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
6144       return gen_rtx_PLUS (Pmode, dest, addend);
6145
6146     case TLS_MODEL_INITIAL_EXEC:
6147       labelno = GEN_INT (pic_labelno++);
6148       label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
6149       label = gen_rtx_CONST (VOIDmode, label);
6150       sum = gen_rtx_UNSPEC (Pmode,
6151                             gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
6152                                        GEN_INT (TARGET_ARM ? 8 : 4)),
6153                             UNSPEC_TLS);
6154       reg = load_tls_operand (sum, reg);
6155
6156       if (TARGET_ARM)
6157         emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
6158       else if (TARGET_THUMB2)
6159         emit_insn (gen_tls_load_dot_plus_four (reg, NULL, reg, labelno));
6160       else
6161         {
6162           emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
6163           emit_move_insn (reg, gen_const_mem (SImode, reg));
6164         }
6165
6166       tp = arm_load_tp (NULL_RTX);
6167
6168       return gen_rtx_PLUS (Pmode, tp, reg);
6169
6170     case TLS_MODEL_LOCAL_EXEC:
6171       tp = arm_load_tp (NULL_RTX);
6172
6173       reg = gen_rtx_UNSPEC (Pmode,
6174                             gen_rtvec (2, x, GEN_INT (TLS_LE32)),
6175                             UNSPEC_TLS);
6176       reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
6177
6178       return gen_rtx_PLUS (Pmode, tp, reg);
6179
6180     default:
6181       abort ();
6182     }
6183 }
6184
6185 /* Try machine-dependent ways of modifying an illegitimate address
6186    to be legitimate.  If we find one, return the new, valid address.  */
6187 rtx
6188 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
6189 {
6190   if (!TARGET_ARM)
6191     {
6192       /* TODO: legitimize_address for Thumb2.  */
6193       if (TARGET_THUMB2)
6194         return x;
6195       return thumb_legitimize_address (x, orig_x, mode);
6196     }
6197
6198   if (arm_tls_symbol_p (x))
6199     return legitimize_tls_address (x, NULL_RTX);
6200
6201   if (GET_CODE (x) == PLUS)
6202     {
6203       rtx xop0 = XEXP (x, 0);
6204       rtx xop1 = XEXP (x, 1);
6205
6206       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
6207         xop0 = force_reg (SImode, xop0);
6208
6209       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
6210         xop1 = force_reg (SImode, xop1);
6211
6212       if (ARM_BASE_REGISTER_RTX_P (xop0)
6213           && GET_CODE (xop1) == CONST_INT)
6214         {
6215           HOST_WIDE_INT n, low_n;
6216           rtx base_reg, val;
6217           n = INTVAL (xop1);
6218
6219           /* VFP addressing modes actually allow greater offsets, but for
6220              now we just stick with the lowest common denominator.  */
6221           if (mode == DImode
6222               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
6223             {
6224               low_n = n & 0x0f;
6225               n &= ~0x0f;
6226               if (low_n > 4)
6227                 {
6228                   n += 16;
6229                   low_n -= 16;
6230                 }
6231             }
6232           else
6233             {
6234               low_n = ((mode) == TImode ? 0
6235                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
6236               n -= low_n;
6237             }
6238
6239           base_reg = gen_reg_rtx (SImode);
6240           val = force_operand (plus_constant (xop0, n), NULL_RTX);
6241           emit_move_insn (base_reg, val);
6242           x = plus_constant (base_reg, low_n);
6243         }
6244       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
6245         x = gen_rtx_PLUS (SImode, xop0, xop1);
6246     }
6247
6248   /* XXX We don't allow MINUS any more -- see comment in
6249      arm_legitimate_address_outer_p ().  */
6250   else if (GET_CODE (x) == MINUS)
6251     {
6252       rtx xop0 = XEXP (x, 0);
6253       rtx xop1 = XEXP (x, 1);
6254
6255       if (CONSTANT_P (xop0))
6256         xop0 = force_reg (SImode, xop0);
6257
6258       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
6259         xop1 = force_reg (SImode, xop1);
6260
6261       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
6262         x = gen_rtx_MINUS (SImode, xop0, xop1);
6263     }
6264
6265   /* Make sure to take full advantage of the pre-indexed addressing mode
6266      with absolute addresses which often allows for the base register to
6267      be factorized for multiple adjacent memory references, and it might
6268      even allows for the mini pool to be avoided entirely. */
6269   else if (GET_CODE (x) == CONST_INT && optimize > 0)
6270     {
6271       unsigned int bits;
6272       HOST_WIDE_INT mask, base, index;
6273       rtx base_reg;
6274
6275       /* ldr and ldrb can use a 12-bit index, ldrsb and the rest can only
6276          use a 8-bit index. So let's use a 12-bit index for SImode only and
6277          hope that arm_gen_constant will enable ldrb to use more bits. */
6278       bits = (mode == SImode) ? 12 : 8;
6279       mask = (1 << bits) - 1;
6280       base = INTVAL (x) & ~mask;
6281       index = INTVAL (x) & mask;
6282       if (bit_count (base & 0xffffffff) > (32 - bits)/2)
6283         {
6284           /* It'll most probably be more efficient to generate the base
6285              with more bits set and use a negative index instead. */
6286           base |= mask;
6287           index -= mask;
6288         }
6289       base_reg = force_reg (SImode, GEN_INT (base));
6290       x = plus_constant (base_reg, index);
6291     }
6292
6293   if (flag_pic)
6294     {
6295       /* We need to find and carefully transform any SYMBOL and LABEL
6296          references; so go back to the original address expression.  */
6297       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
6298
6299       if (new_x != orig_x)
6300         x = new_x;
6301     }
6302
6303   return x;
6304 }
6305
6306
6307 /* Try machine-dependent ways of modifying an illegitimate Thumb address
6308    to be legitimate.  If we find one, return the new, valid address.  */
6309 rtx
6310 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
6311 {
6312   if (arm_tls_symbol_p (x))
6313     return legitimize_tls_address (x, NULL_RTX);
6314
6315   if (GET_CODE (x) == PLUS
6316       && GET_CODE (XEXP (x, 1)) == CONST_INT
6317       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
6318           || INTVAL (XEXP (x, 1)) < 0))
6319     {
6320       rtx xop0 = XEXP (x, 0);
6321       rtx xop1 = XEXP (x, 1);
6322       HOST_WIDE_INT offset = INTVAL (xop1);
6323
6324       /* Try and fold the offset into a biasing of the base register and
6325          then offsetting that.  Don't do this when optimizing for space
6326          since it can cause too many CSEs.  */
6327       if (optimize_size && offset >= 0
6328           && offset < 256 + 31 * GET_MODE_SIZE (mode))
6329         {
6330           HOST_WIDE_INT delta;
6331
6332           if (offset >= 256)
6333             delta = offset - (256 - GET_MODE_SIZE (mode));
6334           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
6335             delta = 31 * GET_MODE_SIZE (mode);
6336           else
6337             delta = offset & (~31 * GET_MODE_SIZE (mode));
6338
6339           xop0 = force_operand (plus_constant (xop0, offset - delta),
6340                                 NULL_RTX);
6341           x = plus_constant (xop0, delta);
6342         }
6343       else if (offset < 0 && offset > -256)
6344         /* Small negative offsets are best done with a subtract before the
6345            dereference, forcing these into a register normally takes two
6346            instructions.  */
6347         x = force_operand (x, NULL_RTX);
6348       else
6349         {
6350           /* For the remaining cases, force the constant into a register.  */
6351           xop1 = force_reg (SImode, xop1);
6352           x = gen_rtx_PLUS (SImode, xop0, xop1);
6353         }
6354     }
6355   else if (GET_CODE (x) == PLUS
6356            && s_register_operand (XEXP (x, 1), SImode)
6357            && !s_register_operand (XEXP (x, 0), SImode))
6358     {
6359       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
6360
6361       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
6362     }
6363
6364   if (flag_pic)
6365     {
6366       /* We need to find and carefully transform any SYMBOL and LABEL
6367          references; so go back to the original address expression.  */
6368       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
6369
6370       if (new_x != orig_x)
6371         x = new_x;
6372     }
6373
6374   return x;
6375 }
6376
6377 rtx
6378 thumb_legitimize_reload_address (rtx *x_p,
6379                                  enum machine_mode mode,
6380                                  int opnum, int type,
6381                                  int ind_levels ATTRIBUTE_UNUSED)
6382 {
6383   rtx x = *x_p;
6384
6385   if (GET_CODE (x) == PLUS
6386       && GET_MODE_SIZE (mode) < 4
6387       && REG_P (XEXP (x, 0))
6388       && XEXP (x, 0) == stack_pointer_rtx
6389       && GET_CODE (XEXP (x, 1)) == CONST_INT
6390       && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
6391     {
6392       rtx orig_x = x;
6393
6394       x = copy_rtx (x);
6395       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
6396                    Pmode, VOIDmode, 0, 0, opnum, (enum reload_type) type);
6397       return x;
6398     }
6399
6400   /* If both registers are hi-regs, then it's better to reload the
6401      entire expression rather than each register individually.  That
6402      only requires one reload register rather than two.  */
6403   if (GET_CODE (x) == PLUS
6404       && REG_P (XEXP (x, 0))
6405       && REG_P (XEXP (x, 1))
6406       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
6407       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
6408     {
6409       rtx orig_x = x;
6410
6411       x = copy_rtx (x);
6412       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
6413                    Pmode, VOIDmode, 0, 0, opnum, (enum reload_type) type);
6414       return x;
6415     }
6416
6417   return NULL;
6418 }
6419
6420 /* Test for various thread-local symbols.  */
6421
6422 /* Return TRUE if X is a thread-local symbol.  */
6423
6424 static bool
6425 arm_tls_symbol_p (rtx x)
6426 {
6427   if (! TARGET_HAVE_TLS)
6428     return false;
6429
6430   if (GET_CODE (x) != SYMBOL_REF)
6431     return false;
6432
6433   return SYMBOL_REF_TLS_MODEL (x) != 0;
6434 }
6435
6436 /* Helper for arm_tls_referenced_p.  */
6437
6438 static int
6439 arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
6440 {
6441   if (GET_CODE (*x) == SYMBOL_REF)
6442     return SYMBOL_REF_TLS_MODEL (*x) != 0;
6443
6444   /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
6445      TLS offsets, not real symbol references.  */
6446   if (GET_CODE (*x) == UNSPEC
6447       && XINT (*x, 1) == UNSPEC_TLS)
6448     return -1;
6449
6450   return 0;
6451 }
6452
6453 /* Return TRUE if X contains any TLS symbol references.  */
6454
6455 bool
6456 arm_tls_referenced_p (rtx x)
6457 {
6458   if (! TARGET_HAVE_TLS)
6459     return false;
6460
6461   return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
6462 }
6463
6464 /* Implement TARGET_CANNOT_FORCE_CONST_MEM.  */
6465
6466 bool
6467 arm_cannot_force_const_mem (rtx x)
6468 {
6469   rtx base, offset;
6470
6471   if (ARM_OFFSETS_MUST_BE_WITHIN_SECTIONS_P)
6472     {
6473       split_const (x, &base, &offset);
6474       if (GET_CODE (base) == SYMBOL_REF
6475           && !offset_within_block_p (base, INTVAL (offset)))
6476         return true;
6477     }
6478   return arm_tls_referenced_p (x);
6479 }
6480 \f
6481 #define REG_OR_SUBREG_REG(X)                                            \
6482   (GET_CODE (X) == REG                                                  \
6483    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
6484
6485 #define REG_OR_SUBREG_RTX(X)                    \
6486    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
6487
6488 static inline int
6489 thumb1_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
6490 {
6491   enum machine_mode mode = GET_MODE (x);
6492   int total;
6493
6494   switch (code)
6495     {
6496     case ASHIFT:
6497     case ASHIFTRT:
6498     case LSHIFTRT:
6499     case ROTATERT:
6500     case PLUS:
6501     case MINUS:
6502     case COMPARE:
6503     case NEG:
6504     case NOT:
6505       return COSTS_N_INSNS (1);
6506
6507     case MULT:
6508       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6509         {
6510           int cycles = 0;
6511           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
6512
6513           while (i)
6514             {
6515               i >>= 2;
6516               cycles++;
6517             }
6518           return COSTS_N_INSNS (2) + cycles;
6519         }
6520       return COSTS_N_INSNS (1) + 16;
6521
6522     case SET:
6523       return (COSTS_N_INSNS (1)
6524               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
6525                      + GET_CODE (SET_DEST (x)) == MEM));
6526
6527     case CONST_INT:
6528       if (outer == SET)
6529         {
6530           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
6531             return 0;
6532           if (thumb_shiftable_const (INTVAL (x)))
6533             return COSTS_N_INSNS (2);
6534           return COSTS_N_INSNS (3);
6535         }
6536       else if ((outer == PLUS || outer == COMPARE)
6537                && INTVAL (x) < 256 && INTVAL (x) > -256)
6538         return 0;
6539       else if ((outer == IOR || outer == XOR || outer == AND)
6540                && INTVAL (x) < 256 && INTVAL (x) >= -256)
6541         return COSTS_N_INSNS (1);
6542       else if (outer == AND)
6543         {
6544           int i;
6545           /* This duplicates the tests in the andsi3 expander.  */
6546           for (i = 9; i <= 31; i++)
6547             if ((((HOST_WIDE_INT) 1) << i) - 1 == INTVAL (x)
6548                 || (((HOST_WIDE_INT) 1) << i) - 1 == ~INTVAL (x))
6549               return COSTS_N_INSNS (2);
6550         }
6551       else if (outer == ASHIFT || outer == ASHIFTRT
6552                || outer == LSHIFTRT)
6553         return 0;
6554       return COSTS_N_INSNS (2);
6555
6556     case CONST:
6557     case CONST_DOUBLE:
6558     case LABEL_REF:
6559     case SYMBOL_REF:
6560       return COSTS_N_INSNS (3);
6561
6562     case UDIV:
6563     case UMOD:
6564     case DIV:
6565     case MOD:
6566       return 100;
6567
6568     case TRUNCATE:
6569       return 99;
6570
6571     case AND:
6572     case XOR:
6573     case IOR:
6574       /* XXX guess.  */
6575       return 8;
6576
6577     case MEM:
6578       /* XXX another guess.  */
6579       /* Memory costs quite a lot for the first word, but subsequent words
6580          load at the equivalent of a single insn each.  */
6581       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
6582               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
6583                  ? 4 : 0));
6584
6585     case IF_THEN_ELSE:
6586       /* XXX a guess.  */
6587       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
6588         return 14;
6589       return 2;
6590
6591     case SIGN_EXTEND:
6592     case ZERO_EXTEND:
6593       total = mode == DImode ? COSTS_N_INSNS (1) : 0;
6594       total += thumb1_rtx_costs (XEXP (x, 0), GET_CODE (XEXP (x, 0)), code);
6595
6596       if (mode == SImode)
6597         return total;
6598
6599       if (arm_arch6)
6600         return total + COSTS_N_INSNS (1);
6601
6602       /* Assume a two-shift sequence.  Increase the cost slightly so
6603          we prefer actual shifts over an extend operation.  */
6604       return total + 1 + COSTS_N_INSNS (2);
6605
6606     default:
6607       return 99;
6608     }
6609 }
6610
6611 static inline bool
6612 arm_rtx_costs_1 (rtx x, enum rtx_code outer, int* total, bool speed)
6613 {
6614   enum machine_mode mode = GET_MODE (x);
6615   enum rtx_code subcode;
6616   rtx operand;
6617   enum rtx_code code = GET_CODE (x);
6618   *total = 0;
6619
6620   switch (code)
6621     {
6622     case MEM:
6623       /* Memory costs quite a lot for the first word, but subsequent words
6624          load at the equivalent of a single insn each.  */
6625       *total = COSTS_N_INSNS (2 + ARM_NUM_REGS (mode));
6626       return true;
6627
6628     case DIV:
6629     case MOD:
6630     case UDIV:
6631     case UMOD:
6632       if (TARGET_HARD_FLOAT && mode == SFmode)
6633         *total = COSTS_N_INSNS (2);
6634       else if (TARGET_HARD_FLOAT && mode == DFmode && !TARGET_VFP_SINGLE)
6635         *total = COSTS_N_INSNS (4);
6636       else
6637         *total = COSTS_N_INSNS (20);
6638       return false;
6639
6640     case ROTATE:
6641       if (GET_CODE (XEXP (x, 1)) == REG)
6642         *total = COSTS_N_INSNS (1); /* Need to subtract from 32 */
6643       else if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6644         *total = rtx_cost (XEXP (x, 1), code, speed);
6645
6646       /* Fall through */
6647     case ROTATERT:
6648       if (mode != SImode)
6649         {
6650           *total += COSTS_N_INSNS (4);
6651           return true;
6652         }
6653
6654       /* Fall through */
6655     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
6656       *total += rtx_cost (XEXP (x, 0), code, speed);
6657       if (mode == DImode)
6658         {
6659           *total += COSTS_N_INSNS (3);
6660           return true;
6661         }
6662
6663       *total += COSTS_N_INSNS (1);
6664       /* Increase the cost of complex shifts because they aren't any faster,
6665          and reduce dual issue opportunities.  */
6666       if (arm_tune_cortex_a9
6667           && outer != SET && GET_CODE (XEXP (x, 1)) != CONST_INT)
6668         ++*total;
6669
6670       return true;
6671
6672     case MINUS:
6673       if (mode == DImode)
6674         {
6675           *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
6676           if (GET_CODE (XEXP (x, 0)) == CONST_INT
6677               && const_ok_for_arm (INTVAL (XEXP (x, 0))))
6678             {
6679               *total += rtx_cost (XEXP (x, 1), code, speed);
6680               return true;
6681             }
6682
6683           if (GET_CODE (XEXP (x, 1)) == CONST_INT
6684               && const_ok_for_arm (INTVAL (XEXP (x, 1))))
6685             {
6686               *total += rtx_cost (XEXP (x, 0), code, speed);
6687               return true;
6688             }
6689
6690           return false;
6691         }
6692
6693       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6694         {
6695           if (TARGET_HARD_FLOAT
6696               && (mode == SFmode
6697                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
6698             {
6699               *total = COSTS_N_INSNS (1);
6700               if (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
6701                   && arm_const_double_rtx (XEXP (x, 0)))
6702                 {
6703                   *total += rtx_cost (XEXP (x, 1), code, speed);
6704                   return true;
6705                 }
6706
6707               if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
6708                   && arm_const_double_rtx (XEXP (x, 1)))
6709                 {
6710                   *total += rtx_cost (XEXP (x, 0), code, speed);
6711                   return true;
6712                 }
6713
6714               return false;
6715             }
6716           *total = COSTS_N_INSNS (20);
6717           return false;
6718         }
6719
6720       *total = COSTS_N_INSNS (1);
6721       if (GET_CODE (XEXP (x, 0)) == CONST_INT
6722           && const_ok_for_arm (INTVAL (XEXP (x, 0))))
6723         {
6724           *total += rtx_cost (XEXP (x, 1), code, speed);
6725           return true;
6726         }
6727
6728       subcode = GET_CODE (XEXP (x, 1));
6729       if (subcode == ASHIFT || subcode == ASHIFTRT
6730           || subcode == LSHIFTRT
6731           || subcode == ROTATE || subcode == ROTATERT)
6732         {
6733           *total += rtx_cost (XEXP (x, 0), code, speed);
6734           *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, speed);
6735           return true;
6736         }
6737
6738       /* A shift as a part of RSB costs no more than RSB itself.  */
6739       if (GET_CODE (XEXP (x, 0)) == MULT
6740           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
6741         {
6742           *total += rtx_cost (XEXP (XEXP (x, 0), 0), code, speed);
6743           *total += rtx_cost (XEXP (x, 1), code, speed);
6744           return true;
6745         }
6746
6747       if (subcode == MULT
6748           && power_of_two_operand (XEXP (XEXP (x, 1), 1), SImode))
6749         {
6750           *total += rtx_cost (XEXP (x, 0), code, speed);
6751           *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, speed);
6752           return true;
6753         }
6754
6755       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMPARE
6756           || GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMM_COMPARE)
6757         {
6758           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
6759           if (GET_CODE (XEXP (XEXP (x, 1), 0)) == REG
6760               && REGNO (XEXP (XEXP (x, 1), 0)) != CC_REGNUM)
6761             *total += COSTS_N_INSNS (1);
6762
6763           return true;
6764         }
6765
6766       /* Fall through */
6767
6768     case PLUS:
6769       if (code == PLUS && arm_arch6 && mode == SImode
6770           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
6771               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
6772         {
6773           *total = COSTS_N_INSNS (1);
6774           *total += rtx_cost (XEXP (XEXP (x, 0), 0), GET_CODE (XEXP (x, 0)),
6775                               speed);
6776           *total += rtx_cost (XEXP (x, 1), code, speed);
6777           return true;
6778         }
6779
6780       /* MLA: All arguments must be registers.  We filter out
6781          multiplication by a power of two, so that we fall down into
6782          the code below.  */
6783       if (GET_CODE (XEXP (x, 0)) == MULT
6784           && !power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
6785         {
6786           /* The cost comes from the cost of the multiply.  */
6787           return false;
6788         }
6789
6790       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6791         {
6792           if (TARGET_HARD_FLOAT
6793               && (mode == SFmode
6794                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
6795             {
6796               *total = COSTS_N_INSNS (1);
6797               if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
6798                   && arm_const_double_rtx (XEXP (x, 1)))
6799                 {
6800                   *total += rtx_cost (XEXP (x, 0), code, speed);
6801                   return true;
6802                 }
6803
6804               return false;
6805             }
6806
6807           *total = COSTS_N_INSNS (20);
6808           return false;
6809         }
6810
6811       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMPARE
6812           || GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMM_COMPARE)
6813         {
6814           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 1), code, speed);
6815           if (GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
6816               && REGNO (XEXP (XEXP (x, 0), 0)) != CC_REGNUM)
6817             *total += COSTS_N_INSNS (1);
6818           return true;
6819         }
6820
6821       /* Fall through */
6822
6823     case AND: case XOR: case IOR:
6824
6825       /* Normally the frame registers will be spilt into reg+const during
6826          reload, so it is a bad idea to combine them with other instructions,
6827          since then they might not be moved outside of loops.  As a compromise
6828          we allow integration with ops that have a constant as their second
6829          operand.  */
6830       if (REG_OR_SUBREG_REG (XEXP (x, 0))
6831           && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
6832           && GET_CODE (XEXP (x, 1)) != CONST_INT)
6833         *total = COSTS_N_INSNS (1);
6834
6835       if (mode == DImode)
6836         {
6837           *total += COSTS_N_INSNS (2);
6838           if (GET_CODE (XEXP (x, 1)) == CONST_INT
6839               && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
6840             {
6841               *total += rtx_cost (XEXP (x, 0), code, speed);
6842               return true;
6843             }
6844
6845           return false;
6846         }
6847
6848       *total += COSTS_N_INSNS (1);
6849       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6850           && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
6851         {
6852           *total += rtx_cost (XEXP (x, 0), code, speed);
6853           return true;
6854         }
6855       subcode = GET_CODE (XEXP (x, 0));
6856       if (subcode == ASHIFT || subcode == ASHIFTRT
6857           || subcode == LSHIFTRT
6858           || subcode == ROTATE || subcode == ROTATERT)
6859         {
6860           *total += rtx_cost (XEXP (x, 1), code, speed);
6861           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6862           return true;
6863         }
6864
6865       if (subcode == MULT
6866           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
6867         {
6868           *total += rtx_cost (XEXP (x, 1), code, speed);
6869           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6870           return true;
6871         }
6872
6873       if (subcode == UMIN || subcode == UMAX
6874           || subcode == SMIN || subcode == SMAX)
6875         {
6876           *total = COSTS_N_INSNS (3);
6877           return true;
6878         }
6879
6880       return false;
6881
6882     case MULT:
6883       /* This should have been handled by the CPU specific routines.  */
6884       gcc_unreachable ();
6885
6886     case TRUNCATE:
6887       if (arm_arch3m && mode == SImode
6888           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
6889           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
6890           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
6891               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
6892           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
6893               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
6894         {
6895           *total = rtx_cost (XEXP (XEXP (x, 0), 0), LSHIFTRT, speed);
6896           return true;
6897         }
6898       *total = COSTS_N_INSNS (2); /* Plus the cost of the MULT */
6899       return false;
6900
6901     case NEG:
6902       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6903         {
6904           if (TARGET_HARD_FLOAT
6905               && (mode == SFmode
6906                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
6907             {
6908               *total = COSTS_N_INSNS (1);
6909               return false;
6910             }
6911           *total = COSTS_N_INSNS (2);
6912           return false;
6913         }
6914
6915       /* Fall through */
6916     case NOT:
6917       *total = COSTS_N_INSNS (ARM_NUM_REGS(mode));
6918       if (mode == SImode && code == NOT)
6919         {
6920           subcode = GET_CODE (XEXP (x, 0));
6921           if (subcode == ASHIFT || subcode == ASHIFTRT
6922               || subcode == LSHIFTRT
6923               || subcode == ROTATE || subcode == ROTATERT
6924               || (subcode == MULT
6925                   && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode)))
6926             {
6927               *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6928               /* Register shifts cost an extra cycle.  */
6929               if (GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
6930                 *total += COSTS_N_INSNS (1) + rtx_cost (XEXP (XEXP (x, 0), 1),
6931                                                         subcode, speed);
6932               return true;
6933             }
6934         }
6935
6936       return false;
6937
6938     case IF_THEN_ELSE:
6939       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
6940         {
6941           *total = COSTS_N_INSNS (4);
6942           return true;
6943         }
6944
6945       operand = XEXP (x, 0);
6946
6947       if (!((GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMPARE
6948              || GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMM_COMPARE)
6949             && GET_CODE (XEXP (operand, 0)) == REG
6950             && REGNO (XEXP (operand, 0)) == CC_REGNUM))
6951         *total += COSTS_N_INSNS (1);
6952       *total += (rtx_cost (XEXP (x, 1), code, speed)
6953                  + rtx_cost (XEXP (x, 2), code, speed));
6954       return true;
6955
6956     case NE:
6957       if (mode == SImode && XEXP (x, 1) == const0_rtx)
6958         {
6959           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
6960           return true;
6961         }
6962       goto scc_insn;
6963
6964     case GE:
6965       if ((GET_CODE (XEXP (x, 0)) != REG || REGNO (XEXP (x, 0)) != CC_REGNUM)
6966           && mode == SImode && XEXP (x, 1) == const0_rtx)
6967         {
6968           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
6969           return true;
6970         }
6971       goto scc_insn;
6972
6973     case LT:
6974       if ((GET_CODE (XEXP (x, 0)) != REG || REGNO (XEXP (x, 0)) != CC_REGNUM)
6975           && mode == SImode && XEXP (x, 1) == const0_rtx)
6976         {
6977           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
6978           return true;
6979         }
6980       goto scc_insn;
6981
6982     case EQ:
6983     case GT:
6984     case LE:
6985     case GEU:
6986     case LTU:
6987     case GTU:
6988     case LEU:
6989     case UNORDERED:
6990     case ORDERED:
6991     case UNEQ:
6992     case UNGE:
6993     case UNLT:
6994     case UNGT:
6995     case UNLE:
6996     scc_insn:
6997       /* SCC insns.  In the case where the comparison has already been
6998          performed, then they cost 2 instructions.  Otherwise they need
6999          an additional comparison before them.  */
7000       *total = COSTS_N_INSNS (2);
7001       if (GET_CODE (XEXP (x, 0)) == REG && REGNO (XEXP (x, 0)) == CC_REGNUM)
7002         {
7003           return true;
7004         }
7005
7006       /* Fall through */
7007     case COMPARE:
7008       if (GET_CODE (XEXP (x, 0)) == REG && REGNO (XEXP (x, 0)) == CC_REGNUM)
7009         {
7010           *total = 0;
7011           return true;
7012         }
7013
7014       *total += COSTS_N_INSNS (1);
7015       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7016           && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
7017         {
7018           *total += rtx_cost (XEXP (x, 0), code, speed);
7019           return true;
7020         }
7021
7022       subcode = GET_CODE (XEXP (x, 0));
7023       if (subcode == ASHIFT || subcode == ASHIFTRT
7024           || subcode == LSHIFTRT
7025           || subcode == ROTATE || subcode == ROTATERT)
7026         {
7027           *total += rtx_cost (XEXP (x, 1), code, speed);
7028           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
7029           return true;
7030         }
7031
7032       if (subcode == MULT
7033           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
7034         {
7035           *total += rtx_cost (XEXP (x, 1), code, speed);
7036           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
7037           return true;
7038         }
7039       
7040       return false;
7041
7042     case UMIN:
7043     case UMAX:
7044     case SMIN:
7045     case SMAX:
7046       *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
7047       if (GET_CODE (XEXP (x, 1)) != CONST_INT
7048           || !const_ok_for_arm (INTVAL (XEXP (x, 1))))
7049         *total += rtx_cost (XEXP (x, 1), code, speed);
7050       return true;
7051
7052     case ABS:
7053       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7054         {
7055           if (TARGET_HARD_FLOAT
7056               && (mode == SFmode
7057                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
7058             {
7059               *total = COSTS_N_INSNS (1);
7060               return false;
7061             }
7062           *total = COSTS_N_INSNS (20);
7063           return false;
7064         }
7065       *total = COSTS_N_INSNS (1);
7066       if (mode == DImode)
7067         *total += COSTS_N_INSNS (3);
7068       return false;
7069
7070     case SIGN_EXTEND:
7071     case ZERO_EXTEND:
7072       *total = 0;
7073       if (GET_MODE_CLASS (mode) == MODE_INT)
7074         {
7075           rtx op = XEXP (x, 0);
7076           enum machine_mode opmode = GET_MODE (op);
7077
7078           if (mode == DImode)
7079             *total += COSTS_N_INSNS (1);
7080
7081           if (opmode != SImode)
7082             {
7083               if (MEM_P (op))
7084                 {
7085                   /* If !arm_arch4, we use one of the extendhisi2_mem
7086                      or movhi_bytes patterns for HImode.  For a QImode
7087                      sign extension, we first zero-extend from memory
7088                      and then perform a shift sequence.  */
7089                   if (!arm_arch4 && (opmode != QImode || code == SIGN_EXTEND))
7090                     *total += COSTS_N_INSNS (2);
7091                 }
7092               else if (arm_arch6)
7093                 *total += COSTS_N_INSNS (1);
7094
7095               /* We don't have the necessary insn, so we need to perform some
7096                  other operation.  */
7097               else if (TARGET_ARM && code == ZERO_EXTEND && mode == QImode)
7098                 /* An and with constant 255.  */
7099                 *total += COSTS_N_INSNS (1);
7100               else
7101                 /* A shift sequence.  Increase costs slightly to avoid
7102                    combining two shifts into an extend operation.  */
7103                 *total += COSTS_N_INSNS (2) + 1;
7104             }
7105
7106           return false;
7107         }
7108
7109       switch (GET_MODE (XEXP (x, 0)))
7110         {
7111         case V8QImode:
7112         case V4HImode:
7113         case V2SImode:
7114         case V4QImode:
7115         case V2HImode:
7116           *total = COSTS_N_INSNS (1);
7117           return false;
7118
7119         default:
7120           gcc_unreachable ();
7121         }
7122       gcc_unreachable ();
7123
7124     case ZERO_EXTRACT:
7125     case SIGN_EXTRACT:
7126       *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
7127       return true;
7128
7129     case CONST_INT:
7130       if (const_ok_for_arm (INTVAL (x))
7131           || const_ok_for_arm (~INTVAL (x)))
7132         *total = COSTS_N_INSNS (1);
7133       else
7134         *total = COSTS_N_INSNS (arm_gen_constant (SET, mode, NULL_RTX,
7135                                                   INTVAL (x), NULL_RTX,
7136                                                   NULL_RTX, 0, 0));
7137       return true;
7138
7139     case CONST:
7140     case LABEL_REF:
7141     case SYMBOL_REF:
7142       *total = COSTS_N_INSNS (3);
7143       return true;
7144
7145     case HIGH:
7146       *total = COSTS_N_INSNS (1);
7147       return true;
7148
7149     case LO_SUM:
7150       *total = COSTS_N_INSNS (1);
7151       *total += rtx_cost (XEXP (x, 0), code, speed);
7152       return true;
7153
7154     case CONST_DOUBLE:
7155       if (TARGET_HARD_FLOAT && vfp3_const_double_rtx (x)
7156           && (mode == SFmode || !TARGET_VFP_SINGLE))
7157         *total = COSTS_N_INSNS (1);
7158       else
7159         *total = COSTS_N_INSNS (4);
7160       return true;
7161
7162     default:
7163       *total = COSTS_N_INSNS (4);
7164       return false;
7165     }
7166 }
7167
7168 /* Estimates the size cost of thumb1 instructions.
7169    For now most of the code is copied from thumb1_rtx_costs. We need more
7170    fine grain tuning when we have more related test cases.  */
7171 static inline int
7172 thumb1_size_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
7173 {
7174   enum machine_mode mode = GET_MODE (x);
7175
7176   switch (code)
7177     {
7178     case ASHIFT:
7179     case ASHIFTRT:
7180     case LSHIFTRT:
7181     case ROTATERT:
7182     case PLUS:
7183     case MINUS:
7184     case COMPARE:
7185     case NEG:
7186     case NOT:
7187       return COSTS_N_INSNS (1);
7188
7189     case MULT:
7190       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7191         {
7192           /* Thumb1 mul instruction can't operate on const. We must Load it
7193              into a register first.  */
7194           int const_size = thumb1_size_rtx_costs (XEXP (x, 1), CONST_INT, SET);
7195           return COSTS_N_INSNS (1) + const_size;
7196         }
7197       return COSTS_N_INSNS (1);
7198
7199     case SET:
7200       return (COSTS_N_INSNS (1)
7201               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
7202                      + GET_CODE (SET_DEST (x)) == MEM));
7203
7204     case CONST_INT:
7205       if (outer == SET)
7206         {
7207           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
7208             return COSTS_N_INSNS (1);
7209           /* See split "TARGET_THUMB1 && satisfies_constraint_J".  */
7210           if (INTVAL (x) >= -255 && INTVAL (x) <= -1)
7211             return COSTS_N_INSNS (2);
7212           /* See split "TARGET_THUMB1 && satisfies_constraint_K".  */
7213           if (thumb_shiftable_const (INTVAL (x)))
7214             return COSTS_N_INSNS (2);
7215           return COSTS_N_INSNS (3);
7216         }
7217       else if ((outer == PLUS || outer == COMPARE)
7218                && INTVAL (x) < 256 && INTVAL (x) > -256)
7219         return 0;
7220       else if ((outer == IOR || outer == XOR || outer == AND)
7221                && INTVAL (x) < 256 && INTVAL (x) >= -256)
7222         return COSTS_N_INSNS (1);
7223       else if (outer == AND)
7224         {
7225           int i;
7226           /* This duplicates the tests in the andsi3 expander.  */
7227           for (i = 9; i <= 31; i++)
7228             if ((((HOST_WIDE_INT) 1) << i) - 1 == INTVAL (x)
7229                 || (((HOST_WIDE_INT) 1) << i) - 1 == ~INTVAL (x))
7230               return COSTS_N_INSNS (2);
7231         }
7232       else if (outer == ASHIFT || outer == ASHIFTRT
7233                || outer == LSHIFTRT)
7234         return 0;
7235       return COSTS_N_INSNS (2);
7236
7237     case CONST:
7238     case CONST_DOUBLE:
7239     case LABEL_REF:
7240     case SYMBOL_REF:
7241       return COSTS_N_INSNS (3);
7242
7243     case UDIV:
7244     case UMOD:
7245     case DIV:
7246     case MOD:
7247       return 100;
7248
7249     case TRUNCATE:
7250       return 99;
7251
7252     case AND:
7253     case XOR:
7254     case IOR:
7255       /* XXX guess.  */
7256       return 8;
7257
7258     case MEM:
7259       /* XXX another guess.  */
7260       /* Memory costs quite a lot for the first word, but subsequent words
7261          load at the equivalent of a single insn each.  */
7262       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
7263               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
7264                  ? 4 : 0));
7265
7266     case IF_THEN_ELSE:
7267       /* XXX a guess.  */
7268       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
7269         return 14;
7270       return 2;
7271
7272     case ZERO_EXTEND:
7273       /* XXX still guessing.  */
7274       switch (GET_MODE (XEXP (x, 0)))
7275         {
7276           case QImode:
7277             return (1 + (mode == DImode ? 4 : 0)
7278                     + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
7279
7280           case HImode:
7281             return (4 + (mode == DImode ? 4 : 0)
7282                     + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
7283
7284           case SImode:
7285             return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
7286
7287           default:
7288             return 99;
7289         }
7290
7291     default:
7292       return 99;
7293     }
7294 }
7295
7296 /* RTX costs when optimizing for size.  */
7297 static bool
7298 arm_size_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7299                     int *total)
7300 {
7301   enum machine_mode mode = GET_MODE (x);
7302   if (TARGET_THUMB1)
7303     {
7304       *total = thumb1_size_rtx_costs (x, code, outer_code);
7305       return true;
7306     }
7307
7308   /* FIXME: This makes no attempt to prefer narrow Thumb-2 instructions.  */
7309   switch (code)
7310     {
7311     case MEM:
7312       /* A memory access costs 1 insn if the mode is small, or the address is
7313          a single register, otherwise it costs one insn per word.  */
7314       if (REG_P (XEXP (x, 0)))
7315         *total = COSTS_N_INSNS (1);
7316       else if (flag_pic
7317                && GET_CODE (XEXP (x, 0)) == PLUS
7318                && will_be_in_index_register (XEXP (XEXP (x, 0), 1)))
7319         /* This will be split into two instructions.
7320            See arm.md:calculate_pic_address.  */
7321         *total = COSTS_N_INSNS (2);
7322       else
7323         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7324       return true;
7325
7326     case DIV:
7327     case MOD:
7328     case UDIV:
7329     case UMOD:
7330       /* Needs a libcall, so it costs about this.  */
7331       *total = COSTS_N_INSNS (2);
7332       return false;
7333
7334     case ROTATE:
7335       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
7336         {
7337           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, false);
7338           return true;
7339         }
7340       /* Fall through */
7341     case ROTATERT:
7342     case ASHIFT:
7343     case LSHIFTRT:
7344     case ASHIFTRT:
7345       if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
7346         {
7347           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code, false);
7348           return true;
7349         }
7350       else if (mode == SImode)
7351         {
7352           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, false);
7353           /* Slightly disparage register shifts, but not by much.  */
7354           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
7355             *total += 1 + rtx_cost (XEXP (x, 1), code, false);
7356           return true;
7357         }
7358
7359       /* Needs a libcall.  */
7360       *total = COSTS_N_INSNS (2);
7361       return false;
7362
7363     case MINUS:
7364       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7365           && (mode == SFmode || !TARGET_VFP_SINGLE))
7366         {
7367           *total = COSTS_N_INSNS (1);
7368           return false;
7369         }
7370
7371       if (mode == SImode)
7372         {
7373           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
7374           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
7375
7376           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
7377               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
7378               || subcode1 == ROTATE || subcode1 == ROTATERT
7379               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
7380               || subcode1 == ASHIFTRT)
7381             {
7382               /* It's just the cost of the two operands.  */
7383               *total = 0;
7384               return false;
7385             }
7386
7387           *total = COSTS_N_INSNS (1);
7388           return false;
7389         }
7390
7391       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7392       return false;
7393
7394     case PLUS:
7395       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7396           && (mode == SFmode || !TARGET_VFP_SINGLE))
7397         {
7398           *total = COSTS_N_INSNS (1);
7399           return false;
7400         }
7401
7402       /* A shift as a part of ADD costs nothing.  */
7403       if (GET_CODE (XEXP (x, 0)) == MULT
7404           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
7405         {
7406           *total = COSTS_N_INSNS (TARGET_THUMB2 ? 2 : 1);
7407           *total += rtx_cost (XEXP (XEXP (x, 0), 0), code, false);
7408           *total += rtx_cost (XEXP (x, 1), code, false);
7409           return true;
7410         }
7411
7412       /* Fall through */
7413     case AND: case XOR: case IOR:
7414       if (mode == SImode)
7415         {
7416           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
7417
7418           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
7419               || subcode == LSHIFTRT || subcode == ASHIFTRT
7420               || (code == AND && subcode == NOT))
7421             {
7422               /* It's just the cost of the two operands.  */
7423               *total = 0;
7424               return false;
7425             }
7426         }
7427
7428       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7429       return false;
7430
7431     case MULT:
7432       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7433       return false;
7434
7435     case NEG:
7436       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7437           && (mode == SFmode || !TARGET_VFP_SINGLE))
7438         {
7439           *total = COSTS_N_INSNS (1);
7440           return false;
7441         }
7442
7443       /* Fall through */
7444     case NOT:
7445       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7446
7447       return false;
7448
7449     case IF_THEN_ELSE:
7450       *total = 0;
7451       return false;
7452
7453     case COMPARE:
7454       if (cc_register (XEXP (x, 0), VOIDmode))
7455         * total = 0;
7456       else
7457         *total = COSTS_N_INSNS (1);
7458       return false;
7459
7460     case ABS:
7461       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
7462           && (mode == SFmode || !TARGET_VFP_SINGLE))
7463         *total = COSTS_N_INSNS (1);
7464       else
7465         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
7466       return false;
7467
7468     case SIGN_EXTEND:
7469     case ZERO_EXTEND:
7470       return arm_rtx_costs_1 (x, outer_code, total, 0);
7471
7472     case CONST_INT:
7473       if (const_ok_for_arm (INTVAL (x)))
7474         /* A multiplication by a constant requires another instruction
7475            to load the constant to a register.  */
7476         *total = COSTS_N_INSNS ((outer_code == SET || outer_code == MULT)
7477                                 ? 1 : 0);
7478       else if (const_ok_for_arm (~INTVAL (x)))
7479         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
7480       else if (const_ok_for_arm (-INTVAL (x)))
7481         {
7482           if (outer_code == COMPARE || outer_code == PLUS
7483               || outer_code == MINUS)
7484             *total = 0;
7485           else
7486             *total = COSTS_N_INSNS (1);
7487         }
7488       else
7489         *total = COSTS_N_INSNS (2);
7490       return true;
7491
7492     case CONST:
7493     case LABEL_REF:
7494     case SYMBOL_REF:
7495       *total = COSTS_N_INSNS (2);
7496       return true;
7497
7498     case CONST_DOUBLE:
7499       *total = COSTS_N_INSNS (4);
7500       return true;
7501
7502     case HIGH:
7503     case LO_SUM:
7504       /* We prefer constant pool entries to MOVW/MOVT pairs, so bump the
7505          cost of these slightly.  */
7506       *total = COSTS_N_INSNS (1) + 1;
7507       return true;
7508
7509     default:
7510       if (mode != VOIDmode)
7511         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7512       else
7513         *total = COSTS_N_INSNS (4); /* How knows?  */
7514       return false;
7515     }
7516 }
7517
7518 /* RTX costs when optimizing for size.  */
7519 static bool
7520 arm_rtx_costs (rtx x, int code, int outer_code, int *total,
7521                bool speed)
7522 {
7523   if (!speed)
7524     return arm_size_rtx_costs (x, (enum rtx_code) code,
7525                                (enum rtx_code) outer_code, total);
7526   else
7527     return current_tune->rtx_costs (x, (enum rtx_code) code,
7528                                     (enum rtx_code) outer_code,
7529                                     total, speed);
7530 }
7531
7532 /* RTX costs for cores with a slow MUL implementation.  Thumb-2 is not
7533    supported on any "slowmul" cores, so it can be ignored.  */
7534
7535 static bool
7536 arm_slowmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7537                        int *total, bool speed)
7538 {
7539   enum machine_mode mode = GET_MODE (x);
7540
7541   if (TARGET_THUMB)
7542     {
7543       *total = thumb1_rtx_costs (x, code, outer_code);
7544       return true;
7545     }
7546
7547   switch (code)
7548     {
7549     case MULT:
7550       if (GET_MODE_CLASS (mode) == MODE_FLOAT
7551           || mode == DImode)
7552         {
7553           *total = COSTS_N_INSNS (20);
7554           return false;
7555         }
7556
7557       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7558         {
7559           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
7560                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
7561           int cost, const_ok = const_ok_for_arm (i);
7562           int j, booth_unit_size;
7563
7564           /* Tune as appropriate.  */
7565           cost = const_ok ? 4 : 8;
7566           booth_unit_size = 2;
7567           for (j = 0; i && j < 32; j += booth_unit_size)
7568             {
7569               i >>= booth_unit_size;
7570               cost++;
7571             }
7572
7573           *total = COSTS_N_INSNS (cost);
7574           *total += rtx_cost (XEXP (x, 0), code, speed);
7575           return true;
7576         }
7577
7578       *total = COSTS_N_INSNS (20);
7579       return false;
7580
7581     default:
7582       return arm_rtx_costs_1 (x, outer_code, total, speed);;
7583     }
7584 }
7585
7586
7587 /* RTX cost for cores with a fast multiply unit (M variants).  */
7588
7589 static bool
7590 arm_fastmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7591                        int *total, bool speed)
7592 {
7593   enum machine_mode mode = GET_MODE (x);
7594
7595   if (TARGET_THUMB1)
7596     {
7597       *total = thumb1_rtx_costs (x, code, outer_code);
7598       return true;
7599     }
7600
7601   /* ??? should thumb2 use different costs?  */
7602   switch (code)
7603     {
7604     case MULT:
7605       /* There is no point basing this on the tuning, since it is always the
7606          fast variant if it exists at all.  */
7607       if (mode == DImode
7608           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
7609           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7610               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7611         {
7612           *total = COSTS_N_INSNS(2);
7613           return false;
7614         }
7615
7616
7617       if (mode == DImode)
7618         {
7619           *total = COSTS_N_INSNS (5);
7620           return false;
7621         }
7622
7623       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7624         {
7625           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
7626                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
7627           int cost, const_ok = const_ok_for_arm (i);
7628           int j, booth_unit_size;
7629
7630           /* Tune as appropriate.  */
7631           cost = const_ok ? 4 : 8;
7632           booth_unit_size = 8;
7633           for (j = 0; i && j < 32; j += booth_unit_size)
7634             {
7635               i >>= booth_unit_size;
7636               cost++;
7637             }
7638
7639           *total = COSTS_N_INSNS(cost);
7640           return false;
7641         }
7642
7643       if (mode == SImode)
7644         {
7645           *total = COSTS_N_INSNS (4);
7646           return false;
7647         }
7648
7649       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7650         {
7651           if (TARGET_HARD_FLOAT
7652               && (mode == SFmode
7653                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
7654             {
7655               *total = COSTS_N_INSNS (1);
7656               return false;
7657             }
7658         }
7659
7660       /* Requires a lib call */
7661       *total = COSTS_N_INSNS (20);
7662       return false;
7663
7664     default:
7665       return arm_rtx_costs_1 (x, outer_code, total, speed);
7666     }
7667 }
7668
7669
7670 /* RTX cost for XScale CPUs.  Thumb-2 is not supported on any xscale cores,
7671    so it can be ignored.  */
7672
7673 static bool
7674 arm_xscale_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7675                       int *total, bool speed)
7676 {
7677   enum machine_mode mode = GET_MODE (x);
7678
7679   if (TARGET_THUMB)
7680     {
7681       *total = thumb1_rtx_costs (x, code, outer_code);
7682       return true;
7683     }
7684
7685   switch (code)
7686     {
7687     case COMPARE:
7688       if (GET_CODE (XEXP (x, 0)) != MULT)
7689         return arm_rtx_costs_1 (x, outer_code, total, speed);
7690
7691       /* A COMPARE of a MULT is slow on XScale; the muls instruction
7692          will stall until the multiplication is complete.  */
7693       *total = COSTS_N_INSNS (3);
7694       return false;
7695
7696     case MULT:
7697       /* There is no point basing this on the tuning, since it is always the
7698          fast variant if it exists at all.  */
7699       if (mode == DImode
7700           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
7701           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7702               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7703         {
7704           *total = COSTS_N_INSNS (2);
7705           return false;
7706         }
7707
7708
7709       if (mode == DImode)
7710         {
7711           *total = COSTS_N_INSNS (5);
7712           return false;
7713         }
7714
7715       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7716         {
7717           /* If operand 1 is a constant we can more accurately
7718              calculate the cost of the multiply.  The multiplier can
7719              retire 15 bits on the first cycle and a further 12 on the
7720              second.  We do, of course, have to load the constant into
7721              a register first.  */
7722           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
7723           /* There's a general overhead of one cycle.  */
7724           int cost = 1;
7725           unsigned HOST_WIDE_INT masked_const;
7726
7727           if (i & 0x80000000)
7728             i = ~i;
7729
7730           i &= (unsigned HOST_WIDE_INT) 0xffffffff;
7731
7732           masked_const = i & 0xffff8000;
7733           if (masked_const != 0)
7734             {
7735               cost++;
7736               masked_const = i & 0xf8000000;
7737               if (masked_const != 0)
7738                 cost++;
7739             }
7740           *total = COSTS_N_INSNS (cost);
7741           return false;
7742         }
7743
7744       if (mode == SImode)
7745         {
7746           *total = COSTS_N_INSNS (3);
7747           return false;
7748         }
7749
7750       /* Requires a lib call */
7751       *total = COSTS_N_INSNS (20);
7752       return false;
7753
7754     default:
7755       return arm_rtx_costs_1 (x, outer_code, total, speed);
7756     }
7757 }
7758
7759
7760 /* RTX costs for 9e (and later) cores.  */
7761
7762 static bool
7763 arm_9e_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7764                   int *total, bool speed)
7765 {
7766   enum machine_mode mode = GET_MODE (x);
7767
7768   if (TARGET_THUMB1)
7769     {
7770       switch (code)
7771         {
7772         case MULT:
7773           *total = COSTS_N_INSNS (3);
7774           return true;
7775
7776         default:
7777           *total = thumb1_rtx_costs (x, code, outer_code);
7778           return true;
7779         }
7780     }
7781
7782   switch (code)
7783     {
7784     case MULT:
7785       /* There is no point basing this on the tuning, since it is always the
7786          fast variant if it exists at all.  */
7787       if (mode == DImode
7788           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
7789           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7790               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7791         {
7792           *total = COSTS_N_INSNS (2);
7793           return false;
7794         }
7795
7796
7797       if (mode == DImode)
7798         {
7799           *total = COSTS_N_INSNS (5);
7800           return false;
7801         }
7802
7803       if (mode == SImode)
7804         {
7805           *total = COSTS_N_INSNS (2);
7806           return false;
7807         }
7808
7809       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7810         {
7811           if (TARGET_HARD_FLOAT
7812               && (mode == SFmode
7813                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
7814             {
7815               *total = COSTS_N_INSNS (1);
7816               return false;
7817             }
7818         }
7819
7820       *total = COSTS_N_INSNS (20);
7821       return false;
7822
7823     default:
7824       return arm_rtx_costs_1 (x, outer_code, total, speed);
7825     }
7826 }
7827 /* All address computations that can be done are free, but rtx cost returns
7828    the same for practically all of them.  So we weight the different types
7829    of address here in the order (most pref first):
7830    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
7831 static inline int
7832 arm_arm_address_cost (rtx x)
7833 {
7834   enum rtx_code c  = GET_CODE (x);
7835
7836   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
7837     return 0;
7838   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
7839     return 10;
7840
7841   if (c == PLUS)
7842     {
7843       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7844         return 2;
7845
7846       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
7847         return 3;
7848
7849       return 4;
7850     }
7851
7852   return 6;
7853 }
7854
7855 static inline int
7856 arm_thumb_address_cost (rtx x)
7857 {
7858   enum rtx_code c  = GET_CODE (x);
7859
7860   if (c == REG)
7861     return 1;
7862   if (c == PLUS
7863       && GET_CODE (XEXP (x, 0)) == REG
7864       && GET_CODE (XEXP (x, 1)) == CONST_INT)
7865     return 1;
7866
7867   return 2;
7868 }
7869
7870 static int
7871 arm_address_cost (rtx x, bool speed ATTRIBUTE_UNUSED)
7872 {
7873   return TARGET_32BIT ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
7874 }
7875
7876 /* Adjust cost hook for XScale.  */
7877 static bool
7878 xscale_sched_adjust_cost (rtx insn, rtx link, rtx dep, int * cost)
7879 {
7880   /* Some true dependencies can have a higher cost depending
7881      on precisely how certain input operands are used.  */
7882   if (REG_NOTE_KIND(link) == 0
7883       && recog_memoized (insn) >= 0
7884       && recog_memoized (dep) >= 0)
7885     {
7886       int shift_opnum = get_attr_shift (insn);
7887       enum attr_type attr_type = get_attr_type (dep);
7888
7889       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
7890          operand for INSN.  If we have a shifted input operand and the
7891          instruction we depend on is another ALU instruction, then we may
7892          have to account for an additional stall.  */
7893       if (shift_opnum != 0
7894           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
7895         {
7896           rtx shifted_operand;
7897           int opno;
7898
7899           /* Get the shifted operand.  */
7900           extract_insn (insn);
7901           shifted_operand = recog_data.operand[shift_opnum];
7902
7903           /* Iterate over all the operands in DEP.  If we write an operand
7904              that overlaps with SHIFTED_OPERAND, then we have increase the
7905              cost of this dependency.  */
7906           extract_insn (dep);
7907           preprocess_constraints ();
7908           for (opno = 0; opno < recog_data.n_operands; opno++)
7909             {
7910               /* We can ignore strict inputs.  */
7911               if (recog_data.operand_type[opno] == OP_IN)
7912                 continue;
7913
7914               if (reg_overlap_mentioned_p (recog_data.operand[opno],
7915                                            shifted_operand))
7916                 {
7917                   *cost = 2;
7918                   return false;
7919                 }
7920             }
7921         }
7922     }
7923   return true;
7924 }
7925
7926 /* Adjust cost hook for Cortex A9.  */
7927 static bool
7928 cortex_a9_sched_adjust_cost (rtx insn, rtx link, rtx dep, int * cost)
7929 {
7930   switch (REG_NOTE_KIND (link))
7931     {
7932     case REG_DEP_ANTI:
7933       *cost = 0;
7934       return false;
7935
7936     case REG_DEP_TRUE:
7937     case REG_DEP_OUTPUT:
7938         if (recog_memoized (insn) >= 0
7939             && recog_memoized (dep) >= 0)
7940           {
7941             if (GET_CODE (PATTERN (insn)) == SET)
7942               {
7943                 if (GET_MODE_CLASS 
7944                     (GET_MODE (SET_DEST (PATTERN (insn)))) == MODE_FLOAT
7945                   || GET_MODE_CLASS 
7946                     (GET_MODE (SET_SRC (PATTERN (insn)))) == MODE_FLOAT)
7947                   {
7948                     enum attr_type attr_type_insn = get_attr_type (insn);
7949                     enum attr_type attr_type_dep = get_attr_type (dep);
7950
7951                     /* By default all dependencies of the form
7952                        s0 = s0 <op> s1
7953                        s0 = s0 <op> s2
7954                        have an extra latency of 1 cycle because
7955                        of the input and output dependency in this
7956                        case. However this gets modeled as an true
7957                        dependency and hence all these checks.  */
7958                     if (REG_P (SET_DEST (PATTERN (insn)))
7959                         && REG_P (SET_DEST (PATTERN (dep)))
7960                         && reg_overlap_mentioned_p (SET_DEST (PATTERN (insn)),
7961                                                     SET_DEST (PATTERN (dep))))
7962                       {
7963                         /* FMACS is a special case where the dependant
7964                            instruction can be issued 3 cycles before
7965                            the normal latency in case of an output 
7966                            dependency.  */
7967                         if ((attr_type_insn == TYPE_FMACS
7968                              || attr_type_insn == TYPE_FMACD)
7969                             && (attr_type_dep == TYPE_FMACS
7970                                 || attr_type_dep == TYPE_FMACD))
7971                           {
7972                             if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
7973                               *cost = insn_default_latency (dep) - 3;
7974                             else
7975                               *cost = insn_default_latency (dep);
7976                             return false;
7977                           }
7978                         else
7979                           {
7980                             if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
7981                               *cost = insn_default_latency (dep) + 1;
7982                             else
7983                               *cost = insn_default_latency (dep);
7984                           }
7985                         return false;
7986                       }
7987                   }
7988               }
7989           }
7990         break;
7991
7992     default:
7993       gcc_unreachable ();
7994     }
7995
7996   return true;
7997 }
7998
7999 /* This function implements the target macro TARGET_SCHED_ADJUST_COST.
8000    It corrects the value of COST based on the relationship between
8001    INSN and DEP through the dependence LINK.  It returns the new
8002    value. There is a per-core adjust_cost hook to adjust scheduler costs
8003    and the per-core hook can choose to completely override the generic 
8004    adjust_cost function. Only put bits of code into arm_adjust_cost that 
8005    are common across all cores.  */
8006 static int
8007 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
8008 {
8009   rtx i_pat, d_pat;
8010
8011  /* When generating Thumb-1 code, we want to place flag-setting operations
8012     close to a conditional branch which depends on them, so that we can
8013     omit the comparison. */
8014   if (TARGET_THUMB1
8015       && REG_NOTE_KIND (link) == 0
8016       && recog_memoized (insn) == CODE_FOR_cbranchsi4_insn
8017       && recog_memoized (dep) >= 0
8018       && get_attr_conds (dep) == CONDS_SET)
8019     return 0;
8020
8021   if (current_tune->sched_adjust_cost != NULL)
8022     {
8023       if (!current_tune->sched_adjust_cost (insn, link, dep, &cost))
8024         return cost;
8025     }
8026
8027   /* XXX This is not strictly true for the FPA.  */
8028   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
8029       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
8030     return 0;
8031
8032   /* Call insns don't incur a stall, even if they follow a load.  */
8033   if (REG_NOTE_KIND (link) == 0
8034       && GET_CODE (insn) == CALL_INSN)
8035     return 1;
8036
8037   if ((i_pat = single_set (insn)) != NULL
8038       && GET_CODE (SET_SRC (i_pat)) == MEM
8039       && (d_pat = single_set (dep)) != NULL
8040       && GET_CODE (SET_DEST (d_pat)) == MEM)
8041     {
8042       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
8043       /* This is a load after a store, there is no conflict if the load reads
8044          from a cached area.  Assume that loads from the stack, and from the
8045          constant pool are cached, and that others will miss.  This is a
8046          hack.  */
8047
8048       if ((GET_CODE (src_mem) == SYMBOL_REF 
8049            && CONSTANT_POOL_ADDRESS_P (src_mem))
8050           || reg_mentioned_p (stack_pointer_rtx, src_mem)
8051           || reg_mentioned_p (frame_pointer_rtx, src_mem)
8052           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
8053         return 1;
8054     }
8055
8056   return cost;
8057 }
8058
8059 static int fp_consts_inited = 0;
8060
8061 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
8062 static const char * const strings_fp[8] =
8063 {
8064   "0",   "1",   "2",   "3",
8065   "4",   "5",   "0.5", "10"
8066 };
8067
8068 static REAL_VALUE_TYPE values_fp[8];
8069
8070 static void
8071 init_fp_table (void)
8072 {
8073   int i;
8074   REAL_VALUE_TYPE r;
8075
8076   if (TARGET_VFP)
8077     fp_consts_inited = 1;
8078   else
8079     fp_consts_inited = 8;
8080
8081   for (i = 0; i < fp_consts_inited; i++)
8082     {
8083       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
8084       values_fp[i] = r;
8085     }
8086 }
8087
8088 /* Return TRUE if rtx X is a valid immediate FP constant.  */
8089 int
8090 arm_const_double_rtx (rtx x)
8091 {
8092   REAL_VALUE_TYPE r;
8093   int i;
8094
8095   if (!fp_consts_inited)
8096     init_fp_table ();
8097
8098   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8099   if (REAL_VALUE_MINUS_ZERO (r))
8100     return 0;
8101
8102   for (i = 0; i < fp_consts_inited; i++)
8103     if (REAL_VALUES_EQUAL (r, values_fp[i]))
8104       return 1;
8105
8106   return 0;
8107 }
8108
8109 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
8110 int
8111 neg_const_double_rtx_ok_for_fpa (rtx x)
8112 {
8113   REAL_VALUE_TYPE r;
8114   int i;
8115
8116   if (!fp_consts_inited)
8117     init_fp_table ();
8118
8119   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8120   r = real_value_negate (&r);
8121   if (REAL_VALUE_MINUS_ZERO (r))
8122     return 0;
8123
8124   for (i = 0; i < 8; i++)
8125     if (REAL_VALUES_EQUAL (r, values_fp[i]))
8126       return 1;
8127
8128   return 0;
8129 }
8130
8131
8132 /* VFPv3 has a fairly wide range of representable immediates, formed from
8133    "quarter-precision" floating-point values. These can be evaluated using this
8134    formula (with ^ for exponentiation):
8135
8136      -1^s * n * 2^-r
8137
8138    Where 's' is a sign bit (0/1), 'n' and 'r' are integers such that
8139    16 <= n <= 31 and 0 <= r <= 7.
8140
8141    These values are mapped onto an 8-bit integer ABCDEFGH s.t.
8142
8143      - A (most-significant) is the sign bit.
8144      - BCD are the exponent (encoded as r XOR 3).
8145      - EFGH are the mantissa (encoded as n - 16).
8146 */
8147
8148 /* Return an integer index for a VFPv3 immediate operand X suitable for the
8149    fconst[sd] instruction, or -1 if X isn't suitable.  */
8150 static int
8151 vfp3_const_double_index (rtx x)
8152 {
8153   REAL_VALUE_TYPE r, m;
8154   int sign, exponent;
8155   unsigned HOST_WIDE_INT mantissa, mant_hi;
8156   unsigned HOST_WIDE_INT mask;
8157   HOST_WIDE_INT m1, m2;
8158   int point_pos = 2 * HOST_BITS_PER_WIDE_INT - 1;
8159
8160   if (!TARGET_VFP3 || GET_CODE (x) != CONST_DOUBLE)
8161     return -1;
8162
8163   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8164
8165   /* We can't represent these things, so detect them first.  */
8166   if (REAL_VALUE_ISINF (r) || REAL_VALUE_ISNAN (r) || REAL_VALUE_MINUS_ZERO (r))
8167     return -1;
8168
8169   /* Extract sign, exponent and mantissa.  */
8170   sign = REAL_VALUE_NEGATIVE (r) ? 1 : 0;
8171   r = real_value_abs (&r);
8172   exponent = REAL_EXP (&r);
8173   /* For the mantissa, we expand into two HOST_WIDE_INTS, apart from the
8174      highest (sign) bit, with a fixed binary point at bit point_pos.
8175      WARNING: If there's ever a VFP version which uses more than 2 * H_W_I - 1
8176      bits for the mantissa, this may fail (low bits would be lost).  */
8177   real_ldexp (&m, &r, point_pos - exponent);
8178   REAL_VALUE_TO_INT (&m1, &m2, m);
8179   mantissa = m1;
8180   mant_hi = m2;
8181
8182   /* If there are bits set in the low part of the mantissa, we can't
8183      represent this value.  */
8184   if (mantissa != 0)
8185     return -1;
8186
8187   /* Now make it so that mantissa contains the most-significant bits, and move
8188      the point_pos to indicate that the least-significant bits have been
8189      discarded.  */
8190   point_pos -= HOST_BITS_PER_WIDE_INT;
8191   mantissa = mant_hi;
8192
8193   /* We can permit four significant bits of mantissa only, plus a high bit
8194      which is always 1.  */
8195   mask = ((unsigned HOST_WIDE_INT)1 << (point_pos - 5)) - 1;
8196   if ((mantissa & mask) != 0)
8197     return -1;
8198
8199   /* Now we know the mantissa is in range, chop off the unneeded bits.  */
8200   mantissa >>= point_pos - 5;
8201
8202   /* The mantissa may be zero. Disallow that case. (It's possible to load the
8203      floating-point immediate zero with Neon using an integer-zero load, but
8204      that case is handled elsewhere.)  */
8205   if (mantissa == 0)
8206     return -1;
8207
8208   gcc_assert (mantissa >= 16 && mantissa <= 31);
8209
8210   /* The value of 5 here would be 4 if GCC used IEEE754-like encoding (where
8211      normalized significands are in the range [1, 2). (Our mantissa is shifted
8212      left 4 places at this point relative to normalized IEEE754 values).  GCC
8213      internally uses [0.5, 1) (see real.c), so the exponent returned from
8214      REAL_EXP must be altered.  */
8215   exponent = 5 - exponent;
8216
8217   if (exponent < 0 || exponent > 7)
8218     return -1;
8219
8220   /* Sign, mantissa and exponent are now in the correct form to plug into the
8221      formula described in the comment above.  */
8222   return (sign << 7) | ((exponent ^ 3) << 4) | (mantissa - 16);
8223 }
8224
8225 /* Return TRUE if rtx X is a valid immediate VFPv3 constant.  */
8226 int
8227 vfp3_const_double_rtx (rtx x)
8228 {
8229   if (!TARGET_VFP3)
8230     return 0;
8231
8232   return vfp3_const_double_index (x) != -1;
8233 }
8234
8235 /* Recognize immediates which can be used in various Neon instructions. Legal
8236    immediates are described by the following table (for VMVN variants, the
8237    bitwise inverse of the constant shown is recognized. In either case, VMOV
8238    is output and the correct instruction to use for a given constant is chosen
8239    by the assembler). The constant shown is replicated across all elements of
8240    the destination vector.
8241
8242    insn elems variant constant (binary)
8243    ---- ----- ------- -----------------
8244    vmov  i32     0    00000000 00000000 00000000 abcdefgh
8245    vmov  i32     1    00000000 00000000 abcdefgh 00000000
8246    vmov  i32     2    00000000 abcdefgh 00000000 00000000
8247    vmov  i32     3    abcdefgh 00000000 00000000 00000000
8248    vmov  i16     4    00000000 abcdefgh
8249    vmov  i16     5    abcdefgh 00000000
8250    vmvn  i32     6    00000000 00000000 00000000 abcdefgh
8251    vmvn  i32     7    00000000 00000000 abcdefgh 00000000
8252    vmvn  i32     8    00000000 abcdefgh 00000000 00000000
8253    vmvn  i32     9    abcdefgh 00000000 00000000 00000000
8254    vmvn  i16    10    00000000 abcdefgh
8255    vmvn  i16    11    abcdefgh 00000000
8256    vmov  i32    12    00000000 00000000 abcdefgh 11111111
8257    vmvn  i32    13    00000000 00000000 abcdefgh 11111111
8258    vmov  i32    14    00000000 abcdefgh 11111111 11111111
8259    vmvn  i32    15    00000000 abcdefgh 11111111 11111111
8260    vmov   i8    16    abcdefgh
8261    vmov  i64    17    aaaaaaaa bbbbbbbb cccccccc dddddddd
8262                       eeeeeeee ffffffff gggggggg hhhhhhhh
8263    vmov  f32    18    aBbbbbbc defgh000 00000000 00000000
8264
8265    For case 18, B = !b. Representable values are exactly those accepted by
8266    vfp3_const_double_index, but are output as floating-point numbers rather
8267    than indices.
8268
8269    Variants 0-5 (inclusive) may also be used as immediates for the second
8270    operand of VORR/VBIC instructions.
8271
8272    The INVERSE argument causes the bitwise inverse of the given operand to be
8273    recognized instead (used for recognizing legal immediates for the VAND/VORN
8274    pseudo-instructions). If INVERSE is true, the value placed in *MODCONST is
8275    *not* inverted (i.e. the pseudo-instruction forms vand/vorn should still be
8276    output, rather than the real insns vbic/vorr).
8277
8278    INVERSE makes no difference to the recognition of float vectors.
8279
8280    The return value is the variant of immediate as shown in the above table, or
8281    -1 if the given value doesn't match any of the listed patterns.
8282 */
8283 static int
8284 neon_valid_immediate (rtx op, enum machine_mode mode, int inverse,
8285                       rtx *modconst, int *elementwidth)
8286 {
8287 #define CHECK(STRIDE, ELSIZE, CLASS, TEST)      \
8288   matches = 1;                                  \
8289   for (i = 0; i < idx; i += (STRIDE))           \
8290     if (!(TEST))                                \
8291       matches = 0;                              \
8292   if (matches)                                  \
8293     {                                           \
8294       immtype = (CLASS);                        \
8295       elsize = (ELSIZE);                        \
8296       break;                                    \
8297     }
8298
8299   unsigned int i, elsize = 0, idx = 0, n_elts = CONST_VECTOR_NUNITS (op);
8300   unsigned int innersize = GET_MODE_SIZE (GET_MODE_INNER (mode));
8301   unsigned char bytes[16];
8302   int immtype = -1, matches;
8303   unsigned int invmask = inverse ? 0xff : 0;
8304
8305   /* Vectors of float constants.  */
8306   if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
8307     {
8308       rtx el0 = CONST_VECTOR_ELT (op, 0);
8309       REAL_VALUE_TYPE r0;
8310
8311       if (!vfp3_const_double_rtx (el0))
8312         return -1;
8313
8314       REAL_VALUE_FROM_CONST_DOUBLE (r0, el0);
8315
8316       for (i = 1; i < n_elts; i++)
8317         {
8318           rtx elt = CONST_VECTOR_ELT (op, i);
8319           REAL_VALUE_TYPE re;
8320
8321           REAL_VALUE_FROM_CONST_DOUBLE (re, elt);
8322
8323           if (!REAL_VALUES_EQUAL (r0, re))
8324             return -1;
8325         }
8326
8327       if (modconst)
8328         *modconst = CONST_VECTOR_ELT (op, 0);
8329
8330       if (elementwidth)
8331         *elementwidth = 0;
8332
8333       return 18;
8334     }
8335
8336   /* Splat vector constant out into a byte vector.  */
8337   for (i = 0; i < n_elts; i++)
8338     {
8339       rtx el = CONST_VECTOR_ELT (op, i);
8340       unsigned HOST_WIDE_INT elpart;
8341       unsigned int part, parts;
8342
8343       if (GET_CODE (el) == CONST_INT)
8344         {
8345           elpart = INTVAL (el);
8346           parts = 1;
8347         }
8348       else if (GET_CODE (el) == CONST_DOUBLE)
8349         {
8350           elpart = CONST_DOUBLE_LOW (el);
8351           parts = 2;
8352         }
8353       else
8354         gcc_unreachable ();
8355
8356       for (part = 0; part < parts; part++)
8357         {
8358           unsigned int byte;
8359           for (byte = 0; byte < innersize; byte++)
8360             {
8361               bytes[idx++] = (elpart & 0xff) ^ invmask;
8362               elpart >>= BITS_PER_UNIT;
8363             }
8364           if (GET_CODE (el) == CONST_DOUBLE)
8365             elpart = CONST_DOUBLE_HIGH (el);
8366         }
8367     }
8368
8369   /* Sanity check.  */
8370   gcc_assert (idx == GET_MODE_SIZE (mode));
8371
8372   do
8373     {
8374       CHECK (4, 32, 0, bytes[i] == bytes[0] && bytes[i + 1] == 0
8375                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
8376
8377       CHECK (4, 32, 1, bytes[i] == 0 && bytes[i + 1] == bytes[1]
8378                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
8379
8380       CHECK (4, 32, 2, bytes[i] == 0 && bytes[i + 1] == 0
8381                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
8382
8383       CHECK (4, 32, 3, bytes[i] == 0 && bytes[i + 1] == 0
8384                        && bytes[i + 2] == 0 && bytes[i + 3] == bytes[3]);
8385
8386       CHECK (2, 16, 4, bytes[i] == bytes[0] && bytes[i + 1] == 0);
8387
8388       CHECK (2, 16, 5, bytes[i] == 0 && bytes[i + 1] == bytes[1]);
8389
8390       CHECK (4, 32, 6, bytes[i] == bytes[0] && bytes[i + 1] == 0xff
8391                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
8392
8393       CHECK (4, 32, 7, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
8394                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
8395
8396       CHECK (4, 32, 8, bytes[i] == 0xff && bytes[i + 1] == 0xff
8397                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
8398
8399       CHECK (4, 32, 9, bytes[i] == 0xff && bytes[i + 1] == 0xff
8400                        && bytes[i + 2] == 0xff && bytes[i + 3] == bytes[3]);
8401
8402       CHECK (2, 16, 10, bytes[i] == bytes[0] && bytes[i + 1] == 0xff);
8403
8404       CHECK (2, 16, 11, bytes[i] == 0xff && bytes[i + 1] == bytes[1]);
8405
8406       CHECK (4, 32, 12, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
8407                         && bytes[i + 2] == 0 && bytes[i + 3] == 0);
8408
8409       CHECK (4, 32, 13, bytes[i] == 0 && bytes[i + 1] == bytes[1]
8410                         && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
8411
8412       CHECK (4, 32, 14, bytes[i] == 0xff && bytes[i + 1] == 0xff
8413                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
8414
8415       CHECK (4, 32, 15, bytes[i] == 0 && bytes[i + 1] == 0
8416                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
8417
8418       CHECK (1, 8, 16, bytes[i] == bytes[0]);
8419
8420       CHECK (1, 64, 17, (bytes[i] == 0 || bytes[i] == 0xff)
8421                         && bytes[i] == bytes[(i + 8) % idx]);
8422     }
8423   while (0);
8424
8425   if (immtype == -1)
8426     return -1;
8427
8428   if (elementwidth)
8429     *elementwidth = elsize;
8430
8431   if (modconst)
8432     {
8433       unsigned HOST_WIDE_INT imm = 0;
8434
8435       /* Un-invert bytes of recognized vector, if necessary.  */
8436       if (invmask != 0)
8437         for (i = 0; i < idx; i++)
8438           bytes[i] ^= invmask;
8439
8440       if (immtype == 17)
8441         {
8442           /* FIXME: Broken on 32-bit H_W_I hosts.  */
8443           gcc_assert (sizeof (HOST_WIDE_INT) == 8);
8444
8445           for (i = 0; i < 8; i++)
8446             imm |= (unsigned HOST_WIDE_INT) (bytes[i] ? 0xff : 0)
8447                    << (i * BITS_PER_UNIT);
8448
8449           *modconst = GEN_INT (imm);
8450         }
8451       else
8452         {
8453           unsigned HOST_WIDE_INT imm = 0;
8454
8455           for (i = 0; i < elsize / BITS_PER_UNIT; i++)
8456             imm |= (unsigned HOST_WIDE_INT) bytes[i] << (i * BITS_PER_UNIT);
8457
8458           *modconst = GEN_INT (imm);
8459         }
8460     }
8461
8462   return immtype;
8463 #undef CHECK
8464 }
8465
8466 /* Return TRUE if rtx X is legal for use as either a Neon VMOV (or, implicitly,
8467    VMVN) immediate. Write back width per element to *ELEMENTWIDTH (or zero for
8468    float elements), and a modified constant (whatever should be output for a
8469    VMOV) in *MODCONST.  */
8470
8471 int
8472 neon_immediate_valid_for_move (rtx op, enum machine_mode mode,
8473                                rtx *modconst, int *elementwidth)
8474 {
8475   rtx tmpconst;
8476   int tmpwidth;
8477   int retval = neon_valid_immediate (op, mode, 0, &tmpconst, &tmpwidth);
8478
8479   if (retval == -1)
8480     return 0;
8481
8482   if (modconst)
8483     *modconst = tmpconst;
8484
8485   if (elementwidth)
8486     *elementwidth = tmpwidth;
8487
8488   return 1;
8489 }
8490
8491 /* Return TRUE if rtx X is legal for use in a VORR or VBIC instruction.  If
8492    the immediate is valid, write a constant suitable for using as an operand
8493    to VORR/VBIC/VAND/VORN to *MODCONST and the corresponding element width to
8494    *ELEMENTWIDTH. See neon_valid_immediate for description of INVERSE.  */
8495
8496 int
8497 neon_immediate_valid_for_logic (rtx op, enum machine_mode mode, int inverse,
8498                                 rtx *modconst, int *elementwidth)
8499 {
8500   rtx tmpconst;
8501   int tmpwidth;
8502   int retval = neon_valid_immediate (op, mode, inverse, &tmpconst, &tmpwidth);
8503
8504   if (retval < 0 || retval > 5)
8505     return 0;
8506
8507   if (modconst)
8508     *modconst = tmpconst;
8509
8510   if (elementwidth)
8511     *elementwidth = tmpwidth;
8512
8513   return 1;
8514 }
8515
8516 /* Return a string suitable for output of Neon immediate logic operation
8517    MNEM.  */
8518
8519 char *
8520 neon_output_logic_immediate (const char *mnem, rtx *op2, enum machine_mode mode,
8521                              int inverse, int quad)
8522 {
8523   int width, is_valid;
8524   static char templ[40];
8525
8526   is_valid = neon_immediate_valid_for_logic (*op2, mode, inverse, op2, &width);
8527
8528   gcc_assert (is_valid != 0);
8529
8530   if (quad)
8531     sprintf (templ, "%s.i%d\t%%q0, %%2", mnem, width);
8532   else
8533     sprintf (templ, "%s.i%d\t%%P0, %%2", mnem, width);
8534
8535   return templ;
8536 }
8537
8538 /* Output a sequence of pairwise operations to implement a reduction.
8539    NOTE: We do "too much work" here, because pairwise operations work on two
8540    registers-worth of operands in one go. Unfortunately we can't exploit those
8541    extra calculations to do the full operation in fewer steps, I don't think.
8542    Although all vector elements of the result but the first are ignored, we
8543    actually calculate the same result in each of the elements. An alternative
8544    such as initially loading a vector with zero to use as each of the second
8545    operands would use up an additional register and take an extra instruction,
8546    for no particular gain.  */
8547
8548 void
8549 neon_pairwise_reduce (rtx op0, rtx op1, enum machine_mode mode,
8550                       rtx (*reduc) (rtx, rtx, rtx))
8551 {
8552   enum machine_mode inner = GET_MODE_INNER (mode);
8553   unsigned int i, parts = GET_MODE_SIZE (mode) / GET_MODE_SIZE (inner);
8554   rtx tmpsum = op1;
8555
8556   for (i = parts / 2; i >= 1; i /= 2)
8557     {
8558       rtx dest = (i == 1) ? op0 : gen_reg_rtx (mode);
8559       emit_insn (reduc (dest, tmpsum, tmpsum));
8560       tmpsum = dest;
8561     }
8562 }
8563
8564 /* If VALS is a vector constant that can be loaded into a register
8565    using VDUP, generate instructions to do so and return an RTX to
8566    assign to the register.  Otherwise return NULL_RTX.  */
8567
8568 static rtx
8569 neon_vdup_constant (rtx vals)
8570 {
8571   enum machine_mode mode = GET_MODE (vals);
8572   enum machine_mode inner_mode = GET_MODE_INNER (mode);
8573   int n_elts = GET_MODE_NUNITS (mode);
8574   bool all_same = true;
8575   rtx x;
8576   int i;
8577
8578   if (GET_CODE (vals) != CONST_VECTOR || GET_MODE_SIZE (inner_mode) > 4)
8579     return NULL_RTX;
8580
8581   for (i = 0; i < n_elts; ++i)
8582     {
8583       x = XVECEXP (vals, 0, i);
8584       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
8585         all_same = false;
8586     }
8587
8588   if (!all_same)
8589     /* The elements are not all the same.  We could handle repeating
8590        patterns of a mode larger than INNER_MODE here (e.g. int8x8_t
8591        {0, C, 0, C, 0, C, 0, C} which can be loaded using
8592        vdup.i16).  */
8593     return NULL_RTX;
8594
8595   /* We can load this constant by using VDUP and a constant in a
8596      single ARM register.  This will be cheaper than a vector
8597      load.  */
8598
8599   x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, 0));
8600   return gen_rtx_VEC_DUPLICATE (mode, x);
8601 }
8602
8603 /* Generate code to load VALS, which is a PARALLEL containing only
8604    constants (for vec_init) or CONST_VECTOR, efficiently into a
8605    register.  Returns an RTX to copy into the register, or NULL_RTX
8606    for a PARALLEL that can not be converted into a CONST_VECTOR.  */
8607
8608 rtx
8609 neon_make_constant (rtx vals)
8610 {
8611   enum machine_mode mode = GET_MODE (vals);
8612   rtx target;
8613   rtx const_vec = NULL_RTX;
8614   int n_elts = GET_MODE_NUNITS (mode);
8615   int n_const = 0;
8616   int i;
8617
8618   if (GET_CODE (vals) == CONST_VECTOR)
8619     const_vec = vals;
8620   else if (GET_CODE (vals) == PARALLEL)
8621     {
8622       /* A CONST_VECTOR must contain only CONST_INTs and
8623          CONST_DOUBLEs, but CONSTANT_P allows more (e.g. SYMBOL_REF).
8624          Only store valid constants in a CONST_VECTOR.  */
8625       for (i = 0; i < n_elts; ++i)
8626         {
8627           rtx x = XVECEXP (vals, 0, i);
8628           if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
8629             n_const++;
8630         }
8631       if (n_const == n_elts)
8632         const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
8633     }
8634   else
8635     gcc_unreachable ();
8636
8637   if (const_vec != NULL
8638       && neon_immediate_valid_for_move (const_vec, mode, NULL, NULL))
8639     /* Load using VMOV.  On Cortex-A8 this takes one cycle.  */
8640     return const_vec;
8641   else if ((target = neon_vdup_constant (vals)) != NULL_RTX)
8642     /* Loaded using VDUP.  On Cortex-A8 the VDUP takes one NEON
8643        pipeline cycle; creating the constant takes one or two ARM
8644        pipeline cycles.  */
8645     return target;
8646   else if (const_vec != NULL_RTX)
8647     /* Load from constant pool.  On Cortex-A8 this takes two cycles
8648        (for either double or quad vectors).  We can not take advantage
8649        of single-cycle VLD1 because we need a PC-relative addressing
8650        mode.  */
8651     return const_vec;
8652   else
8653     /* A PARALLEL containing something not valid inside CONST_VECTOR.
8654        We can not construct an initializer.  */
8655     return NULL_RTX;
8656 }
8657
8658 /* Initialize vector TARGET to VALS.  */
8659
8660 void
8661 neon_expand_vector_init (rtx target, rtx vals)
8662 {
8663   enum machine_mode mode = GET_MODE (target);
8664   enum machine_mode inner_mode = GET_MODE_INNER (mode);
8665   int n_elts = GET_MODE_NUNITS (mode);
8666   int n_var = 0, one_var = -1;
8667   bool all_same = true;
8668   rtx x, mem;
8669   int i;
8670
8671   for (i = 0; i < n_elts; ++i)
8672     {
8673       x = XVECEXP (vals, 0, i);
8674       if (!CONSTANT_P (x))
8675         ++n_var, one_var = i;
8676
8677       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
8678         all_same = false;
8679     }
8680
8681   if (n_var == 0)
8682     {
8683       rtx constant = neon_make_constant (vals);
8684       if (constant != NULL_RTX)
8685         {
8686           emit_move_insn (target, constant);
8687           return;
8688         }
8689     }
8690
8691   /* Splat a single non-constant element if we can.  */
8692   if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
8693     {
8694       x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, 0));
8695       emit_insn (gen_rtx_SET (VOIDmode, target,
8696                               gen_rtx_VEC_DUPLICATE (mode, x)));
8697       return;
8698     }
8699
8700   /* One field is non-constant.  Load constant then overwrite varying
8701      field.  This is more efficient than using the stack.  */
8702   if (n_var == 1)
8703     {
8704       rtx copy = copy_rtx (vals);
8705       rtx index = GEN_INT (one_var);
8706
8707       /* Load constant part of vector, substitute neighboring value for
8708          varying element.  */
8709       XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
8710       neon_expand_vector_init (target, copy);
8711
8712       /* Insert variable.  */
8713       x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, one_var));
8714       switch (mode)
8715         {
8716         case V8QImode:
8717           emit_insn (gen_neon_vset_lanev8qi (target, x, target, index));
8718           break;
8719         case V16QImode:
8720           emit_insn (gen_neon_vset_lanev16qi (target, x, target, index));
8721           break;
8722         case V4HImode:
8723           emit_insn (gen_neon_vset_lanev4hi (target, x, target, index));
8724           break;
8725         case V8HImode:
8726           emit_insn (gen_neon_vset_lanev8hi (target, x, target, index));
8727           break;
8728         case V2SImode:
8729           emit_insn (gen_neon_vset_lanev2si (target, x, target, index));
8730           break;
8731         case V4SImode:
8732           emit_insn (gen_neon_vset_lanev4si (target, x, target, index));
8733           break;
8734         case V2SFmode:
8735           emit_insn (gen_neon_vset_lanev2sf (target, x, target, index));
8736           break;
8737         case V4SFmode:
8738           emit_insn (gen_neon_vset_lanev4sf (target, x, target, index));
8739           break;
8740         case V2DImode:
8741           emit_insn (gen_neon_vset_lanev2di (target, x, target, index));
8742           break;
8743         default:
8744           gcc_unreachable ();
8745         }
8746       return;
8747     }
8748
8749   /* Construct the vector in memory one field at a time
8750      and load the whole vector.  */
8751   mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), 0);
8752   for (i = 0; i < n_elts; i++)
8753     emit_move_insn (adjust_address_nv (mem, inner_mode,
8754                                     i * GET_MODE_SIZE (inner_mode)),
8755                     XVECEXP (vals, 0, i));
8756   emit_move_insn (target, mem);
8757 }
8758
8759 /* Ensure OPERAND lies between LOW (inclusive) and HIGH (exclusive).  Raise
8760    ERR if it doesn't.  FIXME: NEON bounds checks occur late in compilation, so
8761    reported source locations are bogus.  */
8762
8763 static void
8764 bounds_check (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high,
8765               const char *err)
8766 {
8767   HOST_WIDE_INT lane;
8768
8769   gcc_assert (GET_CODE (operand) == CONST_INT);
8770
8771   lane = INTVAL (operand);
8772
8773   if (lane < low || lane >= high)
8774     error (err);
8775 }
8776
8777 /* Bounds-check lanes.  */
8778
8779 void
8780 neon_lane_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
8781 {
8782   bounds_check (operand, low, high, "lane out of range");
8783 }
8784
8785 /* Bounds-check constants.  */
8786
8787 void
8788 neon_const_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
8789 {
8790   bounds_check (operand, low, high, "constant out of range");
8791 }
8792
8793 HOST_WIDE_INT
8794 neon_element_bits (enum machine_mode mode)
8795 {
8796   if (mode == DImode)
8797     return GET_MODE_BITSIZE (mode);
8798   else
8799     return GET_MODE_BITSIZE (GET_MODE_INNER (mode));
8800 }
8801
8802 \f
8803 /* Predicates for `match_operand' and `match_operator'.  */
8804
8805 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
8806 int
8807 cirrus_memory_offset (rtx op)
8808 {
8809   /* Reject eliminable registers.  */
8810   if (! (reload_in_progress || reload_completed)
8811       && (   reg_mentioned_p (frame_pointer_rtx, op)
8812           || reg_mentioned_p (arg_pointer_rtx, op)
8813           || reg_mentioned_p (virtual_incoming_args_rtx, op)
8814           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8815           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8816           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8817     return 0;
8818
8819   if (GET_CODE (op) == MEM)
8820     {
8821       rtx ind;
8822
8823       ind = XEXP (op, 0);
8824
8825       /* Match: (mem (reg)).  */
8826       if (GET_CODE (ind) == REG)
8827         return 1;
8828
8829       /* Match:
8830          (mem (plus (reg)
8831                     (const))).  */
8832       if (GET_CODE (ind) == PLUS
8833           && GET_CODE (XEXP (ind, 0)) == REG
8834           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
8835           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
8836         return 1;
8837     }
8838
8839   return 0;
8840 }
8841
8842 /* Return TRUE if OP is a valid coprocessor memory address pattern.
8843    WB is true if full writeback address modes are allowed and is false
8844    if limited writeback address modes (POST_INC and PRE_DEC) are
8845    allowed.  */
8846
8847 int
8848 arm_coproc_mem_operand (rtx op, bool wb)
8849 {
8850   rtx ind;
8851
8852   /* Reject eliminable registers.  */
8853   if (! (reload_in_progress || reload_completed)
8854       && (   reg_mentioned_p (frame_pointer_rtx, op)
8855           || reg_mentioned_p (arg_pointer_rtx, op)
8856           || reg_mentioned_p (virtual_incoming_args_rtx, op)
8857           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8858           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8859           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8860     return FALSE;
8861
8862   /* Constants are converted into offsets from labels.  */
8863   if (GET_CODE (op) != MEM)
8864     return FALSE;
8865
8866   ind = XEXP (op, 0);
8867
8868   if (reload_completed
8869       && (GET_CODE (ind) == LABEL_REF
8870           || (GET_CODE (ind) == CONST
8871               && GET_CODE (XEXP (ind, 0)) == PLUS
8872               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
8873               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
8874     return TRUE;
8875
8876   /* Match: (mem (reg)).  */
8877   if (GET_CODE (ind) == REG)
8878     return arm_address_register_rtx_p (ind, 0);
8879
8880   /* Autoincremment addressing modes.  POST_INC and PRE_DEC are
8881      acceptable in any case (subject to verification by
8882      arm_address_register_rtx_p).  We need WB to be true to accept
8883      PRE_INC and POST_DEC.  */
8884   if (GET_CODE (ind) == POST_INC
8885       || GET_CODE (ind) == PRE_DEC
8886       || (wb
8887           && (GET_CODE (ind) == PRE_INC
8888               || GET_CODE (ind) == POST_DEC)))
8889     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
8890
8891   if (wb
8892       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
8893       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
8894       && GET_CODE (XEXP (ind, 1)) == PLUS
8895       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
8896     ind = XEXP (ind, 1);
8897
8898   /* Match:
8899      (plus (reg)
8900            (const)).  */
8901   if (GET_CODE (ind) == PLUS
8902       && GET_CODE (XEXP (ind, 0)) == REG
8903       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
8904       && GET_CODE (XEXP (ind, 1)) == CONST_INT
8905       && INTVAL (XEXP (ind, 1)) > -1024
8906       && INTVAL (XEXP (ind, 1)) <  1024
8907       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
8908     return TRUE;
8909
8910   return FALSE;
8911 }
8912
8913 /* Return TRUE if OP is a memory operand which we can load or store a vector
8914    to/from. TYPE is one of the following values:
8915     0 - Vector load/stor (vldr)
8916     1 - Core registers (ldm)
8917     2 - Element/structure loads (vld1)
8918  */
8919 int
8920 neon_vector_mem_operand (rtx op, int type)
8921 {
8922   rtx ind;
8923
8924   /* Reject eliminable registers.  */
8925   if (! (reload_in_progress || reload_completed)
8926       && (   reg_mentioned_p (frame_pointer_rtx, op)
8927           || reg_mentioned_p (arg_pointer_rtx, op)
8928           || reg_mentioned_p (virtual_incoming_args_rtx, op)
8929           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8930           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8931           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8932     return FALSE;
8933
8934   /* Constants are converted into offsets from labels.  */
8935   if (GET_CODE (op) != MEM)
8936     return FALSE;
8937
8938   ind = XEXP (op, 0);
8939
8940   if (reload_completed
8941       && (GET_CODE (ind) == LABEL_REF
8942           || (GET_CODE (ind) == CONST
8943               && GET_CODE (XEXP (ind, 0)) == PLUS
8944               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
8945               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
8946     return TRUE;
8947
8948   /* Match: (mem (reg)).  */
8949   if (GET_CODE (ind) == REG)
8950     return arm_address_register_rtx_p (ind, 0);
8951
8952   /* Allow post-increment with Neon registers.  */
8953   if ((type != 1 && GET_CODE (ind) == POST_INC)
8954       || (type == 0 && GET_CODE (ind) == PRE_DEC))
8955     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
8956
8957   /* FIXME: vld1 allows register post-modify.  */
8958
8959   /* Match:
8960      (plus (reg)
8961           (const)).  */
8962   if (type == 0
8963       && GET_CODE (ind) == PLUS
8964       && GET_CODE (XEXP (ind, 0)) == REG
8965       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
8966       && GET_CODE (XEXP (ind, 1)) == CONST_INT
8967       && INTVAL (XEXP (ind, 1)) > -1024
8968       && INTVAL (XEXP (ind, 1)) < 1016
8969       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
8970     return TRUE;
8971
8972   return FALSE;
8973 }
8974
8975 /* Return TRUE if OP is a mem suitable for loading/storing a Neon struct
8976    type.  */
8977 int
8978 neon_struct_mem_operand (rtx op)
8979 {
8980   rtx ind;
8981
8982   /* Reject eliminable registers.  */
8983   if (! (reload_in_progress || reload_completed)
8984       && (   reg_mentioned_p (frame_pointer_rtx, op)
8985           || reg_mentioned_p (arg_pointer_rtx, op)
8986           || reg_mentioned_p (virtual_incoming_args_rtx, op)
8987           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8988           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8989           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8990     return FALSE;
8991
8992   /* Constants are converted into offsets from labels.  */
8993   if (GET_CODE (op) != MEM)
8994     return FALSE;
8995
8996   ind = XEXP (op, 0);
8997
8998   if (reload_completed
8999       && (GET_CODE (ind) == LABEL_REF
9000           || (GET_CODE (ind) == CONST
9001               && GET_CODE (XEXP (ind, 0)) == PLUS
9002               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
9003               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
9004     return TRUE;
9005
9006   /* Match: (mem (reg)).  */
9007   if (GET_CODE (ind) == REG)
9008     return arm_address_register_rtx_p (ind, 0);
9009
9010   return FALSE;
9011 }
9012
9013 /* Return true if X is a register that will be eliminated later on.  */
9014 int
9015 arm_eliminable_register (rtx x)
9016 {
9017   return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
9018                        || REGNO (x) == ARG_POINTER_REGNUM
9019                        || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
9020                            && REGNO (x) <= LAST_VIRTUAL_REGISTER));
9021 }
9022
9023 /* Return GENERAL_REGS if a scratch register required to reload x to/from
9024    coprocessor registers.  Otherwise return NO_REGS.  */
9025
9026 enum reg_class
9027 coproc_secondary_reload_class (enum machine_mode mode, rtx x, bool wb)
9028 {
9029   if (mode == HFmode)
9030     {
9031       if (!TARGET_NEON_FP16)
9032         return GENERAL_REGS;
9033       if (s_register_operand (x, mode) || neon_vector_mem_operand (x, 2))
9034         return NO_REGS;
9035       return GENERAL_REGS;
9036     }
9037
9038   if (TARGET_NEON
9039       && (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9040           || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
9041       && neon_vector_mem_operand (x, 0))
9042      return NO_REGS;
9043
9044   if (arm_coproc_mem_operand (x, wb) || s_register_operand (x, mode))
9045     return NO_REGS;
9046
9047   return GENERAL_REGS;
9048 }
9049
9050 /* Values which must be returned in the most-significant end of the return
9051    register.  */
9052
9053 static bool
9054 arm_return_in_msb (const_tree valtype)
9055 {
9056   return (TARGET_AAPCS_BASED
9057           && BYTES_BIG_ENDIAN
9058           && (AGGREGATE_TYPE_P (valtype)
9059               || TREE_CODE (valtype) == COMPLEX_TYPE));
9060 }
9061
9062 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
9063    Use by the Cirrus Maverick code which has to workaround
9064    a hardware bug triggered by such instructions.  */
9065 static bool
9066 arm_memory_load_p (rtx insn)
9067 {
9068   rtx body, lhs, rhs;;
9069
9070   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
9071     return false;
9072
9073   body = PATTERN (insn);
9074
9075   if (GET_CODE (body) != SET)
9076     return false;
9077
9078   lhs = XEXP (body, 0);
9079   rhs = XEXP (body, 1);
9080
9081   lhs = REG_OR_SUBREG_RTX (lhs);
9082
9083   /* If the destination is not a general purpose
9084      register we do not have to worry.  */
9085   if (GET_CODE (lhs) != REG
9086       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
9087     return false;
9088
9089   /* As well as loads from memory we also have to react
9090      to loads of invalid constants which will be turned
9091      into loads from the minipool.  */
9092   return (GET_CODE (rhs) == MEM
9093           || GET_CODE (rhs) == SYMBOL_REF
9094           || note_invalid_constants (insn, -1, false));
9095 }
9096
9097 /* Return TRUE if INSN is a Cirrus instruction.  */
9098 static bool
9099 arm_cirrus_insn_p (rtx insn)
9100 {
9101   enum attr_cirrus attr;
9102
9103   /* get_attr cannot accept USE or CLOBBER.  */
9104   if (!insn
9105       || GET_CODE (insn) != INSN
9106       || GET_CODE (PATTERN (insn)) == USE
9107       || GET_CODE (PATTERN (insn)) == CLOBBER)
9108     return 0;
9109
9110   attr = get_attr_cirrus (insn);
9111
9112   return attr != CIRRUS_NOT;
9113 }
9114
9115 /* Cirrus reorg for invalid instruction combinations.  */
9116 static void
9117 cirrus_reorg (rtx first)
9118 {
9119   enum attr_cirrus attr;
9120   rtx body = PATTERN (first);
9121   rtx t;
9122   int nops;
9123
9124   /* Any branch must be followed by 2 non Cirrus instructions.  */
9125   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
9126     {
9127       nops = 0;
9128       t = next_nonnote_insn (first);
9129
9130       if (arm_cirrus_insn_p (t))
9131         ++ nops;
9132
9133       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
9134         ++ nops;
9135
9136       while (nops --)
9137         emit_insn_after (gen_nop (), first);
9138
9139       return;
9140     }
9141
9142   /* (float (blah)) is in parallel with a clobber.  */
9143   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
9144     body = XVECEXP (body, 0, 0);
9145
9146   if (GET_CODE (body) == SET)
9147     {
9148       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
9149
9150       /* cfldrd, cfldr64, cfstrd, cfstr64 must
9151          be followed by a non Cirrus insn.  */
9152       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
9153         {
9154           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
9155             emit_insn_after (gen_nop (), first);
9156
9157           return;
9158         }
9159       else if (arm_memory_load_p (first))
9160         {
9161           unsigned int arm_regno;
9162
9163           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
9164              ldr/cfmv64hr combination where the Rd field is the same
9165              in both instructions must be split with a non Cirrus
9166              insn.  Example:
9167
9168              ldr r0, blah
9169              nop
9170              cfmvsr mvf0, r0.  */
9171
9172           /* Get Arm register number for ldr insn.  */
9173           if (GET_CODE (lhs) == REG)
9174             arm_regno = REGNO (lhs);
9175           else
9176             {
9177               gcc_assert (GET_CODE (rhs) == REG);
9178               arm_regno = REGNO (rhs);
9179             }
9180
9181           /* Next insn.  */
9182           first = next_nonnote_insn (first);
9183
9184           if (! arm_cirrus_insn_p (first))
9185             return;
9186
9187           body = PATTERN (first);
9188
9189           /* (float (blah)) is in parallel with a clobber.  */
9190           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
9191             body = XVECEXP (body, 0, 0);
9192
9193           if (GET_CODE (body) == FLOAT)
9194             body = XEXP (body, 0);
9195
9196           if (get_attr_cirrus (first) == CIRRUS_MOVE
9197               && GET_CODE (XEXP (body, 1)) == REG
9198               && arm_regno == REGNO (XEXP (body, 1)))
9199             emit_insn_after (gen_nop (), first);
9200
9201           return;
9202         }
9203     }
9204
9205   /* get_attr cannot accept USE or CLOBBER.  */
9206   if (!first
9207       || GET_CODE (first) != INSN
9208       || GET_CODE (PATTERN (first)) == USE
9209       || GET_CODE (PATTERN (first)) == CLOBBER)
9210     return;
9211
9212   attr = get_attr_cirrus (first);
9213
9214   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
9215      must be followed by a non-coprocessor instruction.  */
9216   if (attr == CIRRUS_COMPARE)
9217     {
9218       nops = 0;
9219
9220       t = next_nonnote_insn (first);
9221
9222       if (arm_cirrus_insn_p (t))
9223         ++ nops;
9224
9225       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
9226         ++ nops;
9227
9228       while (nops --)
9229         emit_insn_after (gen_nop (), first);
9230
9231       return;
9232     }
9233 }
9234
9235 /* Return TRUE if X references a SYMBOL_REF.  */
9236 int
9237 symbol_mentioned_p (rtx x)
9238 {
9239   const char * fmt;
9240   int i;
9241
9242   if (GET_CODE (x) == SYMBOL_REF)
9243     return 1;
9244
9245   /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
9246      are constant offsets, not symbols.  */
9247   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
9248     return 0;
9249
9250   fmt = GET_RTX_FORMAT (GET_CODE (x));
9251
9252   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
9253     {
9254       if (fmt[i] == 'E')
9255         {
9256           int j;
9257
9258           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9259             if (symbol_mentioned_p (XVECEXP (x, i, j)))
9260               return 1;
9261         }
9262       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
9263         return 1;
9264     }
9265
9266   return 0;
9267 }
9268
9269 /* Return TRUE if X references a LABEL_REF.  */
9270 int
9271 label_mentioned_p (rtx x)
9272 {
9273   const char * fmt;
9274   int i;
9275
9276   if (GET_CODE (x) == LABEL_REF)
9277     return 1;
9278
9279   /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
9280      instruction, but they are constant offsets, not symbols.  */
9281   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
9282     return 0;
9283
9284   fmt = GET_RTX_FORMAT (GET_CODE (x));
9285   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
9286     {
9287       if (fmt[i] == 'E')
9288         {
9289           int j;
9290
9291           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9292             if (label_mentioned_p (XVECEXP (x, i, j)))
9293               return 1;
9294         }
9295       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
9296         return 1;
9297     }
9298
9299   return 0;
9300 }
9301
9302 int
9303 tls_mentioned_p (rtx x)
9304 {
9305   switch (GET_CODE (x))
9306     {
9307     case CONST:
9308       return tls_mentioned_p (XEXP (x, 0));
9309
9310     case UNSPEC:
9311       if (XINT (x, 1) == UNSPEC_TLS)
9312         return 1;
9313
9314     default:
9315       return 0;
9316     }
9317 }
9318
9319 /* Must not copy any rtx that uses a pc-relative address.  */
9320
9321 static int
9322 arm_note_pic_base (rtx *x, void *date ATTRIBUTE_UNUSED)
9323 {
9324   if (GET_CODE (*x) == UNSPEC
9325       && XINT (*x, 1) == UNSPEC_PIC_BASE)
9326     return 1;
9327   return 0;
9328 }
9329
9330 static bool
9331 arm_cannot_copy_insn_p (rtx insn)
9332 {
9333   return for_each_rtx (&PATTERN (insn), arm_note_pic_base, NULL);
9334 }
9335
9336 enum rtx_code
9337 minmax_code (rtx x)
9338 {
9339   enum rtx_code code = GET_CODE (x);
9340
9341   switch (code)
9342     {
9343     case SMAX:
9344       return GE;
9345     case SMIN:
9346       return LE;
9347     case UMIN:
9348       return LEU;
9349     case UMAX:
9350       return GEU;
9351     default:
9352       gcc_unreachable ();
9353     }
9354 }
9355
9356 /* Return 1 if memory locations are adjacent.  */
9357 int
9358 adjacent_mem_locations (rtx a, rtx b)
9359 {
9360   /* We don't guarantee to preserve the order of these memory refs.  */
9361   if (volatile_refs_p (a) || volatile_refs_p (b))
9362     return 0;
9363
9364   if ((GET_CODE (XEXP (a, 0)) == REG
9365        || (GET_CODE (XEXP (a, 0)) == PLUS
9366            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
9367       && (GET_CODE (XEXP (b, 0)) == REG
9368           || (GET_CODE (XEXP (b, 0)) == PLUS
9369               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
9370     {
9371       HOST_WIDE_INT val0 = 0, val1 = 0;
9372       rtx reg0, reg1;
9373       int val_diff;
9374
9375       if (GET_CODE (XEXP (a, 0)) == PLUS)
9376         {
9377           reg0 = XEXP (XEXP (a, 0), 0);
9378           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
9379         }
9380       else
9381         reg0 = XEXP (a, 0);
9382
9383       if (GET_CODE (XEXP (b, 0)) == PLUS)
9384         {
9385           reg1 = XEXP (XEXP (b, 0), 0);
9386           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
9387         }
9388       else
9389         reg1 = XEXP (b, 0);
9390
9391       /* Don't accept any offset that will require multiple
9392          instructions to handle, since this would cause the
9393          arith_adjacentmem pattern to output an overlong sequence.  */
9394       if (!const_ok_for_op (val0, PLUS) || !const_ok_for_op (val1, PLUS))
9395         return 0;
9396
9397       /* Don't allow an eliminable register: register elimination can make
9398          the offset too large.  */
9399       if (arm_eliminable_register (reg0))
9400         return 0;
9401
9402       val_diff = val1 - val0;
9403
9404       if (arm_ld_sched)
9405         {
9406           /* If the target has load delay slots, then there's no benefit
9407              to using an ldm instruction unless the offset is zero and
9408              we are optimizing for size.  */
9409           return (optimize_size && (REGNO (reg0) == REGNO (reg1))
9410                   && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
9411                   && (val_diff == 4 || val_diff == -4));
9412         }
9413
9414       return ((REGNO (reg0) == REGNO (reg1))
9415               && (val_diff == 4 || val_diff == -4));
9416     }
9417
9418   return 0;
9419 }
9420
9421 /* Return true iff it would be profitable to turn a sequence of NOPS loads
9422    or stores (depending on IS_STORE) into a load-multiple or store-multiple
9423    instruction.  ADD_OFFSET is nonzero if the base address register needs
9424    to be modified with an add instruction before we can use it.  */
9425
9426 static bool
9427 multiple_operation_profitable_p (bool is_store ATTRIBUTE_UNUSED,
9428                                  int nops, HOST_WIDE_INT add_offset)
9429  {
9430   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
9431      if the offset isn't small enough.  The reason 2 ldrs are faster
9432      is because these ARMs are able to do more than one cache access
9433      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
9434      whilst the ARM8 has a double bandwidth cache.  This means that
9435      these cores can do both an instruction fetch and a data fetch in
9436      a single cycle, so the trick of calculating the address into a
9437      scratch register (one of the result regs) and then doing a load
9438      multiple actually becomes slower (and no smaller in code size).
9439      That is the transformation
9440
9441         ldr     rd1, [rbase + offset]
9442         ldr     rd2, [rbase + offset + 4]
9443
9444      to
9445
9446         add     rd1, rbase, offset
9447         ldmia   rd1, {rd1, rd2}
9448
9449      produces worse code -- '3 cycles + any stalls on rd2' instead of
9450      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
9451      access per cycle, the first sequence could never complete in less
9452      than 6 cycles, whereas the ldm sequence would only take 5 and
9453      would make better use of sequential accesses if not hitting the
9454      cache.
9455
9456      We cheat here and test 'arm_ld_sched' which we currently know to
9457      only be true for the ARM8, ARM9 and StrongARM.  If this ever
9458      changes, then the test below needs to be reworked.  */
9459   if (nops == 2 && arm_ld_sched && add_offset != 0)
9460     return false;
9461
9462   /* XScale has load-store double instructions, but they have stricter
9463      alignment requirements than load-store multiple, so we cannot
9464      use them.
9465
9466      For XScale ldm requires 2 + NREGS cycles to complete and blocks
9467      the pipeline until completion.
9468
9469         NREGS           CYCLES
9470           1               3
9471           2               4
9472           3               5
9473           4               6
9474
9475      An ldr instruction takes 1-3 cycles, but does not block the
9476      pipeline.
9477
9478         NREGS           CYCLES
9479           1              1-3
9480           2              2-6
9481           3              3-9
9482           4              4-12
9483
9484      Best case ldr will always win.  However, the more ldr instructions
9485      we issue, the less likely we are to be able to schedule them well.
9486      Using ldr instructions also increases code size.
9487
9488      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
9489      for counts of 3 or 4 regs.  */
9490   if (nops <= 2 && arm_tune_xscale && !optimize_size)
9491     return false;
9492   return true;
9493 }
9494
9495 /* Subroutine of load_multiple_sequence and store_multiple_sequence.
9496    Given an array of UNSORTED_OFFSETS, of which there are NOPS, compute
9497    an array ORDER which describes the sequence to use when accessing the
9498    offsets that produces an ascending order.  In this sequence, each
9499    offset must be larger by exactly 4 than the previous one.  ORDER[0]
9500    must have been filled in with the lowest offset by the caller.
9501    If UNSORTED_REGS is nonnull, it is an array of register numbers that
9502    we use to verify that ORDER produces an ascending order of registers.
9503    Return true if it was possible to construct such an order, false if
9504    not.  */
9505
9506 static bool
9507 compute_offset_order (int nops, HOST_WIDE_INT *unsorted_offsets, int *order,
9508                       int *unsorted_regs)
9509 {
9510   int i;
9511   for (i = 1; i < nops; i++)
9512     {
9513       int j;
9514
9515       order[i] = order[i - 1];
9516       for (j = 0; j < nops; j++)
9517         if (unsorted_offsets[j] == unsorted_offsets[order[i - 1]] + 4)
9518           {
9519             /* We must find exactly one offset that is higher than the
9520                previous one by 4.  */
9521             if (order[i] != order[i - 1])
9522               return false;
9523             order[i] = j;
9524           }
9525       if (order[i] == order[i - 1])
9526         return false;
9527       /* The register numbers must be ascending.  */
9528       if (unsorted_regs != NULL
9529           && unsorted_regs[order[i]] <= unsorted_regs[order[i - 1]])
9530         return false;
9531     }
9532   return true;
9533 }
9534
9535 /* Used to determine in a peephole whether a sequence of load
9536    instructions can be changed into a load-multiple instruction.
9537    NOPS is the number of separate load instructions we are examining.  The
9538    first NOPS entries in OPERANDS are the destination registers, the
9539    next NOPS entries are memory operands.  If this function is
9540    successful, *BASE is set to the common base register of the memory
9541    accesses; *LOAD_OFFSET is set to the first memory location's offset
9542    from that base register.
9543    REGS is an array filled in with the destination register numbers.
9544    SAVED_ORDER (if nonnull), is an array filled in with an order that maps
9545    insn numbers to to an ascending order of stores.  If CHECK_REGS is true,
9546    the sequence of registers in REGS matches the loads from ascending memory
9547    locations, and the function verifies that the register numbers are
9548    themselves ascending.  If CHECK_REGS is false, the register numbers
9549    are stored in the order they are found in the operands.  */
9550 static int
9551 load_multiple_sequence (rtx *operands, int nops, int *regs, int *saved_order,
9552                         int *base, HOST_WIDE_INT *load_offset, bool check_regs)
9553 {
9554   int unsorted_regs[MAX_LDM_STM_OPS];
9555   HOST_WIDE_INT unsorted_offsets[MAX_LDM_STM_OPS];
9556   int order[MAX_LDM_STM_OPS];
9557   rtx base_reg_rtx = NULL;
9558   int base_reg = -1;
9559   int i, ldm_case;
9560
9561   /* Can only handle up to MAX_LDM_STM_OPS insns at present, though could be
9562      easily extended if required.  */
9563   gcc_assert (nops >= 2 && nops <= MAX_LDM_STM_OPS);
9564
9565   memset (order, 0, MAX_LDM_STM_OPS * sizeof (int));
9566
9567   /* Loop over the operands and check that the memory references are
9568      suitable (i.e. immediate offsets from the same base register).  At
9569      the same time, extract the target register, and the memory
9570      offsets.  */
9571   for (i = 0; i < nops; i++)
9572     {
9573       rtx reg;
9574       rtx offset;
9575
9576       /* Convert a subreg of a mem into the mem itself.  */
9577       if (GET_CODE (operands[nops + i]) == SUBREG)
9578         operands[nops + i] = alter_subreg (operands + (nops + i));
9579
9580       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
9581
9582       /* Don't reorder volatile memory references; it doesn't seem worth
9583          looking for the case where the order is ok anyway.  */
9584       if (MEM_VOLATILE_P (operands[nops + i]))
9585         return 0;
9586
9587       offset = const0_rtx;
9588
9589       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
9590            || (GET_CODE (reg) == SUBREG
9591                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9592           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
9593               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
9594                    == REG)
9595                   || (GET_CODE (reg) == SUBREG
9596                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9597               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
9598                   == CONST_INT)))
9599         {
9600           if (i == 0)
9601             {
9602               base_reg = REGNO (reg);
9603               base_reg_rtx = reg;
9604               if (TARGET_THUMB1 && base_reg > LAST_LO_REGNUM)
9605                 return 0;
9606             }
9607           else if (base_reg != (int) REGNO (reg))
9608             /* Not addressed from the same base register.  */
9609             return 0;
9610
9611           unsorted_regs[i] = (GET_CODE (operands[i]) == REG
9612                               ? REGNO (operands[i])
9613                               : REGNO (SUBREG_REG (operands[i])));
9614
9615           /* If it isn't an integer register, or if it overwrites the
9616              base register but isn't the last insn in the list, then
9617              we can't do this.  */
9618           if (unsorted_regs[i] < 0
9619               || (TARGET_THUMB1 && unsorted_regs[i] > LAST_LO_REGNUM)
9620               || unsorted_regs[i] > 14
9621               || (i != nops - 1 && unsorted_regs[i] == base_reg))
9622             return 0;
9623
9624           unsorted_offsets[i] = INTVAL (offset);
9625           if (i == 0 || unsorted_offsets[i] < unsorted_offsets[order[0]])
9626             order[0] = i;
9627         }
9628       else
9629         /* Not a suitable memory address.  */
9630         return 0;
9631     }
9632
9633   /* All the useful information has now been extracted from the
9634      operands into unsorted_regs and unsorted_offsets; additionally,
9635      order[0] has been set to the lowest offset in the list.  Sort
9636      the offsets into order, verifying that they are adjacent, and
9637      check that the register numbers are ascending.  */
9638   if (!compute_offset_order (nops, unsorted_offsets, order,
9639                              check_regs ? unsorted_regs : NULL))
9640     return 0;
9641
9642   if (saved_order)
9643     memcpy (saved_order, order, sizeof order);
9644
9645   if (base)
9646     {
9647       *base = base_reg;
9648
9649       for (i = 0; i < nops; i++)
9650         regs[i] = unsorted_regs[check_regs ? order[i] : i];
9651
9652       *load_offset = unsorted_offsets[order[0]];
9653     }
9654
9655   if (TARGET_THUMB1
9656       && !peep2_reg_dead_p (nops, base_reg_rtx))
9657     return 0;
9658
9659   if (unsorted_offsets[order[0]] == 0)
9660     ldm_case = 1; /* ldmia */
9661   else if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
9662     ldm_case = 2; /* ldmib */
9663   else if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
9664     ldm_case = 3; /* ldmda */
9665   else if (TARGET_32BIT && unsorted_offsets[order[nops - 1]] == -4)
9666     ldm_case = 4; /* ldmdb */
9667   else if (const_ok_for_arm (unsorted_offsets[order[0]])
9668            || const_ok_for_arm (-unsorted_offsets[order[0]]))
9669     ldm_case = 5;
9670   else
9671     return 0;
9672
9673   if (!multiple_operation_profitable_p (false, nops,
9674                                         ldm_case == 5
9675                                         ? unsorted_offsets[order[0]] : 0))
9676     return 0;
9677
9678   return ldm_case;
9679 }
9680
9681 /* Used to determine in a peephole whether a sequence of store instructions can
9682    be changed into a store-multiple instruction.
9683    NOPS is the number of separate store instructions we are examining.
9684    NOPS_TOTAL is the total number of instructions recognized by the peephole
9685    pattern.
9686    The first NOPS entries in OPERANDS are the source registers, the next
9687    NOPS entries are memory operands.  If this function is successful, *BASE is
9688    set to the common base register of the memory accesses; *LOAD_OFFSET is set
9689    to the first memory location's offset from that base register.  REGS is an
9690    array filled in with the source register numbers, REG_RTXS (if nonnull) is
9691    likewise filled with the corresponding rtx's.
9692    SAVED_ORDER (if nonnull), is an array filled in with an order that maps insn
9693    numbers to to an ascending order of stores.
9694    If CHECK_REGS is true, the sequence of registers in *REGS matches the stores
9695    from ascending memory locations, and the function verifies that the register
9696    numbers are themselves ascending.  If CHECK_REGS is false, the register
9697    numbers are stored in the order they are found in the operands.  */
9698 static int
9699 store_multiple_sequence (rtx *operands, int nops, int nops_total,
9700                          int *regs, rtx *reg_rtxs, int *saved_order, int *base,
9701                          HOST_WIDE_INT *load_offset, bool check_regs)
9702 {
9703   int unsorted_regs[MAX_LDM_STM_OPS];
9704   rtx unsorted_reg_rtxs[MAX_LDM_STM_OPS];
9705   HOST_WIDE_INT unsorted_offsets[MAX_LDM_STM_OPS];
9706   int order[MAX_LDM_STM_OPS];
9707   int base_reg = -1;
9708   rtx base_reg_rtx = NULL;
9709   int i, stm_case;
9710
9711   /* Can only handle up to MAX_LDM_STM_OPS insns at present, though could be
9712      easily extended if required.  */
9713   gcc_assert (nops >= 2 && nops <= MAX_LDM_STM_OPS);
9714
9715   memset (order, 0, MAX_LDM_STM_OPS * sizeof (int));
9716
9717   /* Loop over the operands and check that the memory references are
9718      suitable (i.e. immediate offsets from the same base register).  At
9719      the same time, extract the target register, and the memory
9720      offsets.  */
9721   for (i = 0; i < nops; i++)
9722     {
9723       rtx reg;
9724       rtx offset;
9725
9726       /* Convert a subreg of a mem into the mem itself.  */
9727       if (GET_CODE (operands[nops + i]) == SUBREG)
9728         operands[nops + i] = alter_subreg (operands + (nops + i));
9729
9730       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
9731
9732       /* Don't reorder volatile memory references; it doesn't seem worth
9733          looking for the case where the order is ok anyway.  */
9734       if (MEM_VOLATILE_P (operands[nops + i]))
9735         return 0;
9736
9737       offset = const0_rtx;
9738
9739       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
9740            || (GET_CODE (reg) == SUBREG
9741                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9742           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
9743               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
9744                    == REG)
9745                   || (GET_CODE (reg) == SUBREG
9746                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
9747               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
9748                   == CONST_INT)))
9749         {
9750           unsorted_reg_rtxs[i] = (GET_CODE (operands[i]) == REG
9751                                   ? operands[i] : SUBREG_REG (operands[i]));
9752           unsorted_regs[i] = REGNO (unsorted_reg_rtxs[i]);
9753
9754           if (i == 0)
9755             {
9756               base_reg = REGNO (reg);
9757               base_reg_rtx = reg;
9758               if (TARGET_THUMB1 && base_reg > LAST_LO_REGNUM)
9759                 return 0;
9760             }
9761           else if (base_reg != (int) REGNO (reg))
9762             /* Not addressed from the same base register.  */
9763             return 0;
9764
9765           /* If it isn't an integer register, then we can't do this.  */
9766           if (unsorted_regs[i] < 0
9767               || (TARGET_THUMB1 && unsorted_regs[i] > LAST_LO_REGNUM)
9768               || (TARGET_THUMB2 && unsorted_regs[i] == base_reg)
9769               || (TARGET_THUMB2 && unsorted_regs[i] == SP_REGNUM)
9770               || unsorted_regs[i] > 14)
9771             return 0;
9772
9773           unsorted_offsets[i] = INTVAL (offset);
9774           if (i == 0 || unsorted_offsets[i] < unsorted_offsets[order[0]])
9775             order[0] = i;
9776         }
9777       else
9778         /* Not a suitable memory address.  */
9779         return 0;
9780     }
9781
9782   /* All the useful information has now been extracted from the
9783      operands into unsorted_regs and unsorted_offsets; additionally,
9784      order[0] has been set to the lowest offset in the list.  Sort
9785      the offsets into order, verifying that they are adjacent, and
9786      check that the register numbers are ascending.  */
9787   if (!compute_offset_order (nops, unsorted_offsets, order,
9788                              check_regs ? unsorted_regs : NULL))
9789     return 0;
9790
9791   if (saved_order)
9792     memcpy (saved_order, order, sizeof order);
9793
9794   if (base)
9795     {
9796       *base = base_reg;
9797
9798       for (i = 0; i < nops; i++)
9799         {
9800           regs[i] = unsorted_regs[check_regs ? order[i] : i];
9801           if (reg_rtxs)
9802             reg_rtxs[i] = unsorted_reg_rtxs[check_regs ? order[i] : i];
9803         }
9804
9805       *load_offset = unsorted_offsets[order[0]];
9806     }
9807
9808   if (TARGET_THUMB1
9809       && !peep2_reg_dead_p (nops_total, base_reg_rtx))
9810     return 0;
9811
9812   if (unsorted_offsets[order[0]] == 0)
9813     stm_case = 1; /* stmia */
9814   else if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
9815     stm_case = 2; /* stmib */
9816   else if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
9817     stm_case = 3; /* stmda */
9818   else if (TARGET_32BIT && unsorted_offsets[order[nops - 1]] == -4)
9819     stm_case = 4; /* stmdb */
9820   else
9821     return 0;
9822
9823   if (!multiple_operation_profitable_p (false, nops, 0))
9824     return 0;
9825
9826   return stm_case;
9827 }
9828 \f
9829 /* Routines for use in generating RTL.  */
9830
9831 /* Generate a load-multiple instruction.  COUNT is the number of loads in
9832    the instruction; REGS and MEMS are arrays containing the operands.
9833    BASEREG is the base register to be used in addressing the memory operands.
9834    WBACK_OFFSET is nonzero if the instruction should update the base
9835    register.  */
9836
9837 static rtx
9838 arm_gen_load_multiple_1 (int count, int *regs, rtx *mems, rtx basereg,
9839                          HOST_WIDE_INT wback_offset)
9840 {
9841   int i = 0, j;
9842   rtx result;
9843
9844   if (!multiple_operation_profitable_p (false, count, 0))
9845     {
9846       rtx seq;
9847
9848       start_sequence ();
9849
9850       for (i = 0; i < count; i++)
9851         emit_move_insn (gen_rtx_REG (SImode, regs[i]), mems[i]);
9852
9853       if (wback_offset != 0)
9854         emit_move_insn (basereg, plus_constant (basereg, wback_offset));
9855
9856       seq = get_insns ();
9857       end_sequence ();
9858
9859       return seq;
9860     }
9861
9862   result = gen_rtx_PARALLEL (VOIDmode,
9863                              rtvec_alloc (count + (wback_offset != 0 ? 1 : 0)));
9864   if (wback_offset != 0)
9865     {
9866       XVECEXP (result, 0, 0)
9867         = gen_rtx_SET (VOIDmode, basereg,
9868                        plus_constant (basereg, wback_offset));
9869       i = 1;
9870       count++;
9871     }
9872
9873   for (j = 0; i < count; i++, j++)
9874     XVECEXP (result, 0, i)
9875       = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, regs[j]), mems[j]);
9876
9877   return result;
9878 }
9879
9880 /* Generate a store-multiple instruction.  COUNT is the number of stores in
9881    the instruction; REGS and MEMS are arrays containing the operands.
9882    BASEREG is the base register to be used in addressing the memory operands.
9883    WBACK_OFFSET is nonzero if the instruction should update the base
9884    register.  */
9885
9886 static rtx
9887 arm_gen_store_multiple_1 (int count, int *regs, rtx *mems, rtx basereg,
9888                           HOST_WIDE_INT wback_offset)
9889 {
9890   int i = 0, j;
9891   rtx result;
9892
9893   if (GET_CODE (basereg) == PLUS)
9894     basereg = XEXP (basereg, 0);
9895
9896   if (!multiple_operation_profitable_p (false, count, 0))
9897     {
9898       rtx seq;
9899
9900       start_sequence ();
9901
9902       for (i = 0; i < count; i++)
9903         emit_move_insn (mems[i], gen_rtx_REG (SImode, regs[i]));
9904
9905       if (wback_offset != 0)
9906         emit_move_insn (basereg, plus_constant (basereg, wback_offset));
9907
9908       seq = get_insns ();
9909       end_sequence ();
9910
9911       return seq;
9912     }
9913
9914   result = gen_rtx_PARALLEL (VOIDmode,
9915                              rtvec_alloc (count + (wback_offset != 0 ? 1 : 0)));
9916   if (wback_offset != 0)
9917     {
9918       XVECEXP (result, 0, 0)
9919         = gen_rtx_SET (VOIDmode, basereg,
9920                        plus_constant (basereg, wback_offset));
9921       i = 1;
9922       count++;
9923     }
9924
9925   for (j = 0; i < count; i++, j++)
9926     XVECEXP (result, 0, i)
9927       = gen_rtx_SET (VOIDmode, mems[j], gen_rtx_REG (SImode, regs[j]));
9928
9929   return result;
9930 }
9931
9932 /* Generate either a load-multiple or a store-multiple instruction.  This
9933    function can be used in situations where we can start with a single MEM
9934    rtx and adjust its address upwards.
9935    COUNT is the number of operations in the instruction, not counting a
9936    possible update of the base register.  REGS is an array containing the
9937    register operands.
9938    BASEREG is the base register to be used in addressing the memory operands,
9939    which are constructed from BASEMEM.
9940    WRITE_BACK specifies whether the generated instruction should include an
9941    update of the base register.
9942    OFFSETP is used to pass an offset to and from this function; this offset
9943    is not used when constructing the address (instead BASEMEM should have an
9944    appropriate offset in its address), it is used only for setting
9945    MEM_OFFSET.  It is updated only if WRITE_BACK is true.*/
9946
9947 static rtx
9948 arm_gen_multiple_op (bool is_load, int *regs, int count, rtx basereg,
9949                      bool write_back, rtx basemem, HOST_WIDE_INT *offsetp)
9950 {
9951   rtx mems[MAX_LDM_STM_OPS];
9952   HOST_WIDE_INT offset = *offsetp;
9953   int i;
9954
9955   gcc_assert (count <= MAX_LDM_STM_OPS);
9956
9957   if (GET_CODE (basereg) == PLUS)
9958     basereg = XEXP (basereg, 0);
9959
9960   for (i = 0; i < count; i++)
9961     {
9962       rtx addr = plus_constant (basereg, i * 4);
9963       mems[i] = adjust_automodify_address_nv (basemem, SImode, addr, offset);
9964       offset += 4;
9965     }
9966
9967   if (write_back)
9968     *offsetp = offset;
9969
9970   if (is_load)
9971     return arm_gen_load_multiple_1 (count, regs, mems, basereg,
9972                                     write_back ? 4 * count : 0);
9973   else
9974     return arm_gen_store_multiple_1 (count, regs, mems, basereg,
9975                                      write_back ? 4 * count : 0);
9976 }
9977
9978 rtx
9979 arm_gen_load_multiple (int *regs, int count, rtx basereg, int write_back,
9980                        rtx basemem, HOST_WIDE_INT *offsetp)
9981 {
9982   return arm_gen_multiple_op (TRUE, regs, count, basereg, write_back, basemem,
9983                               offsetp);
9984 }
9985
9986 rtx
9987 arm_gen_store_multiple (int *regs, int count, rtx basereg, int write_back,
9988                         rtx basemem, HOST_WIDE_INT *offsetp)
9989 {
9990   return arm_gen_multiple_op (FALSE, regs, count, basereg, write_back, basemem,
9991                               offsetp);
9992 }
9993
9994 /* Called from a peephole2 expander to turn a sequence of loads into an
9995    LDM instruction.  OPERANDS are the operands found by the peephole matcher;
9996    NOPS indicates how many separate loads we are trying to combine.  SORT_REGS
9997    is true if we can reorder the registers because they are used commutatively
9998    subsequently.
9999    Returns true iff we could generate a new instruction.  */
10000
10001 bool
10002 gen_ldm_seq (rtx *operands, int nops, bool sort_regs)
10003 {
10004   int regs[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
10005   rtx mems[MAX_LDM_STM_OPS];
10006   int i, j, base_reg;
10007   rtx base_reg_rtx;
10008   HOST_WIDE_INT offset;
10009   int write_back = FALSE;
10010   int ldm_case;
10011   rtx addr;
10012
10013   ldm_case = load_multiple_sequence (operands, nops, regs, mem_order,
10014                                      &base_reg, &offset, !sort_regs);
10015
10016   if (ldm_case == 0)
10017     return false;
10018
10019   if (sort_regs)
10020     for (i = 0; i < nops - 1; i++)
10021       for (j = i + 1; j < nops; j++)
10022         if (regs[i] > regs[j])
10023           {
10024             int t = regs[i];
10025             regs[i] = regs[j];
10026             regs[j] = t;
10027           }
10028   base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
10029
10030   if (TARGET_THUMB1)
10031     {
10032       gcc_assert (peep2_reg_dead_p (nops, base_reg_rtx));
10033       gcc_assert (ldm_case == 1 || ldm_case == 5);
10034       write_back = TRUE;
10035     }
10036
10037   if (ldm_case == 5)
10038     {
10039       rtx newbase = TARGET_THUMB1 ? base_reg_rtx : gen_rtx_REG (SImode, regs[0]);
10040       emit_insn (gen_addsi3 (newbase, base_reg_rtx, GEN_INT (offset)));
10041       offset = 0;
10042       if (!TARGET_THUMB1)
10043         {
10044           base_reg = regs[0];
10045           base_reg_rtx = newbase;
10046         }
10047     }
10048
10049   for (i = 0; i < nops; i++)
10050     {
10051       addr = plus_constant (base_reg_rtx, offset + i * 4);
10052       mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
10053                                               SImode, addr, 0);
10054     }
10055   emit_insn (arm_gen_load_multiple_1 (nops, regs, mems, base_reg_rtx,
10056                                       write_back ? offset + i * 4 : 0));
10057   return true;
10058 }
10059
10060 /* Called from a peephole2 expander to turn a sequence of stores into an
10061    STM instruction.  OPERANDS are the operands found by the peephole matcher;
10062    NOPS indicates how many separate stores we are trying to combine.
10063    Returns true iff we could generate a new instruction.  */
10064
10065 bool
10066 gen_stm_seq (rtx *operands, int nops)
10067 {
10068   int i;
10069   int regs[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
10070   rtx mems[MAX_LDM_STM_OPS];
10071   int base_reg;
10072   rtx base_reg_rtx;
10073   HOST_WIDE_INT offset;
10074   int write_back = FALSE;
10075   int stm_case;
10076   rtx addr;
10077   bool base_reg_dies;
10078
10079   stm_case = store_multiple_sequence (operands, nops, nops, regs, NULL,
10080                                       mem_order, &base_reg, &offset, true);
10081
10082   if (stm_case == 0)
10083     return false;
10084
10085   base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
10086
10087   base_reg_dies = peep2_reg_dead_p (nops, base_reg_rtx);
10088   if (TARGET_THUMB1)
10089     {
10090       gcc_assert (base_reg_dies);
10091       write_back = TRUE;
10092     }
10093
10094   if (stm_case == 5)
10095     {
10096       gcc_assert (base_reg_dies);
10097       emit_insn (gen_addsi3 (base_reg_rtx, base_reg_rtx, GEN_INT (offset)));
10098       offset = 0;
10099     }
10100
10101   addr = plus_constant (base_reg_rtx, offset);
10102
10103   for (i = 0; i < nops; i++)
10104     {
10105       addr = plus_constant (base_reg_rtx, offset + i * 4);
10106       mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
10107                                               SImode, addr, 0);
10108     }
10109   emit_insn (arm_gen_store_multiple_1 (nops, regs, mems, base_reg_rtx,
10110                                        write_back ? offset + i * 4 : 0));
10111   return true;
10112 }
10113
10114 /* Called from a peephole2 expander to turn a sequence of stores that are
10115    preceded by constant loads into an STM instruction.  OPERANDS are the
10116    operands found by the peephole matcher; NOPS indicates how many
10117    separate stores we are trying to combine; there are 2 * NOPS
10118    instructions in the peephole.
10119    Returns true iff we could generate a new instruction.  */
10120
10121 bool
10122 gen_const_stm_seq (rtx *operands, int nops)
10123 {
10124   int regs[MAX_LDM_STM_OPS], sorted_regs[MAX_LDM_STM_OPS];
10125   int reg_order[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
10126   rtx reg_rtxs[MAX_LDM_STM_OPS], orig_reg_rtxs[MAX_LDM_STM_OPS];
10127   rtx mems[MAX_LDM_STM_OPS];
10128   int base_reg;
10129   rtx base_reg_rtx;
10130   HOST_WIDE_INT offset;
10131   int write_back = FALSE;
10132   int stm_case;
10133   rtx addr;
10134   bool base_reg_dies;
10135   int i, j;
10136   HARD_REG_SET allocated;
10137
10138   stm_case = store_multiple_sequence (operands, nops, 2 * nops, regs, reg_rtxs,
10139                                       mem_order, &base_reg, &offset, false);
10140
10141   if (stm_case == 0)
10142     return false;
10143
10144   memcpy (orig_reg_rtxs, reg_rtxs, sizeof orig_reg_rtxs);
10145
10146   /* If the same register is used more than once, try to find a free
10147      register.  */
10148   CLEAR_HARD_REG_SET (allocated);
10149   for (i = 0; i < nops; i++)
10150     {
10151       for (j = i + 1; j < nops; j++)
10152         if (regs[i] == regs[j])
10153           {
10154             rtx t = peep2_find_free_register (0, nops * 2,
10155                                               TARGET_THUMB1 ? "l" : "r",
10156                                               SImode, &allocated);
10157             if (t == NULL_RTX)
10158               return false;
10159             reg_rtxs[i] = t;
10160             regs[i] = REGNO (t);
10161           }
10162     }
10163
10164   /* Compute an ordering that maps the register numbers to an ascending
10165      sequence.  */
10166   reg_order[0] = 0;
10167   for (i = 0; i < nops; i++)
10168     if (regs[i] < regs[reg_order[0]])
10169       reg_order[0] = i;
10170
10171   for (i = 1; i < nops; i++)
10172     {
10173       int this_order = reg_order[i - 1];
10174       for (j = 0; j < nops; j++)
10175         if (regs[j] > regs[reg_order[i - 1]]
10176             && (this_order == reg_order[i - 1]
10177                 || regs[j] < regs[this_order]))
10178           this_order = j;
10179       reg_order[i] = this_order;
10180     }
10181
10182   /* Ensure that registers that must be live after the instruction end
10183      up with the correct value.  */
10184   for (i = 0; i < nops; i++)
10185     {
10186       int this_order = reg_order[i];
10187       if ((this_order != mem_order[i]
10188            || orig_reg_rtxs[this_order] != reg_rtxs[this_order])
10189           && !peep2_reg_dead_p (nops * 2, orig_reg_rtxs[this_order]))
10190         return false;
10191     }
10192
10193   /* Load the constants.  */
10194   for (i = 0; i < nops; i++)
10195     {
10196       rtx op = operands[2 * nops + mem_order[i]];
10197       sorted_regs[i] = regs[reg_order[i]];
10198       emit_move_insn (reg_rtxs[reg_order[i]], op);
10199     }
10200
10201   base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
10202
10203   base_reg_dies = peep2_reg_dead_p (nops * 2, base_reg_rtx);
10204   if (TARGET_THUMB1)
10205     {
10206       gcc_assert (base_reg_dies);
10207       write_back = TRUE;
10208     }
10209
10210   if (stm_case == 5)
10211     {
10212       gcc_assert (base_reg_dies);
10213       emit_insn (gen_addsi3 (base_reg_rtx, base_reg_rtx, GEN_INT (offset)));
10214       offset = 0;
10215     }
10216
10217   addr = plus_constant (base_reg_rtx, offset);
10218
10219   for (i = 0; i < nops; i++)
10220     {
10221       addr = plus_constant (base_reg_rtx, offset + i * 4);
10222       mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
10223                                               SImode, addr, 0);
10224     }
10225   emit_insn (arm_gen_store_multiple_1 (nops, sorted_regs, mems, base_reg_rtx,
10226                                        write_back ? offset + i * 4 : 0));
10227   return true;
10228 }
10229
10230 int
10231 arm_gen_movmemqi (rtx *operands)
10232 {
10233   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
10234   HOST_WIDE_INT srcoffset, dstoffset;
10235   int i;
10236   rtx src, dst, srcbase, dstbase;
10237   rtx part_bytes_reg = NULL;
10238   rtx mem;
10239
10240   if (GET_CODE (operands[2]) != CONST_INT
10241       || GET_CODE (operands[3]) != CONST_INT
10242       || INTVAL (operands[2]) > 64
10243       || INTVAL (operands[3]) & 3)
10244     return 0;
10245
10246   dstbase = operands[0];
10247   srcbase = operands[1];
10248
10249   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
10250   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
10251
10252   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
10253   out_words_to_go = INTVAL (operands[2]) / 4;
10254   last_bytes = INTVAL (operands[2]) & 3;
10255   dstoffset = srcoffset = 0;
10256
10257   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
10258     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
10259
10260   for (i = 0; in_words_to_go >= 2; i+=4)
10261     {
10262       if (in_words_to_go > 4)
10263         emit_insn (arm_gen_load_multiple (arm_regs_in_sequence, 4, src,
10264                                           TRUE, srcbase, &srcoffset));
10265       else
10266         emit_insn (arm_gen_load_multiple (arm_regs_in_sequence, in_words_to_go,
10267                                           src, FALSE, srcbase,
10268                                           &srcoffset));
10269
10270       if (out_words_to_go)
10271         {
10272           if (out_words_to_go > 4)
10273             emit_insn (arm_gen_store_multiple (arm_regs_in_sequence, 4, dst,
10274                                                TRUE, dstbase, &dstoffset));
10275           else if (out_words_to_go != 1)
10276             emit_insn (arm_gen_store_multiple (arm_regs_in_sequence,
10277                                                out_words_to_go, dst,
10278                                                (last_bytes == 0
10279                                                 ? FALSE : TRUE),
10280                                                dstbase, &dstoffset));
10281           else
10282             {
10283               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
10284               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
10285               if (last_bytes != 0)
10286                 {
10287                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
10288                   dstoffset += 4;
10289                 }
10290             }
10291         }
10292
10293       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
10294       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
10295     }
10296
10297   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
10298   if (out_words_to_go)
10299     {
10300       rtx sreg;
10301
10302       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
10303       sreg = copy_to_reg (mem);
10304
10305       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
10306       emit_move_insn (mem, sreg);
10307       in_words_to_go--;
10308
10309       gcc_assert (!in_words_to_go);     /* Sanity check */
10310     }
10311
10312   if (in_words_to_go)
10313     {
10314       gcc_assert (in_words_to_go > 0);
10315
10316       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
10317       part_bytes_reg = copy_to_mode_reg (SImode, mem);
10318     }
10319
10320   gcc_assert (!last_bytes || part_bytes_reg);
10321
10322   if (BYTES_BIG_ENDIAN && last_bytes)
10323     {
10324       rtx tmp = gen_reg_rtx (SImode);
10325
10326       /* The bytes we want are in the top end of the word.  */
10327       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
10328                               GEN_INT (8 * (4 - last_bytes))));
10329       part_bytes_reg = tmp;
10330
10331       while (last_bytes)
10332         {
10333           mem = adjust_automodify_address (dstbase, QImode,
10334                                            plus_constant (dst, last_bytes - 1),
10335                                            dstoffset + last_bytes - 1);
10336           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
10337
10338           if (--last_bytes)
10339             {
10340               tmp = gen_reg_rtx (SImode);
10341               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
10342               part_bytes_reg = tmp;
10343             }
10344         }
10345
10346     }
10347   else
10348     {
10349       if (last_bytes > 1)
10350         {
10351           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
10352           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
10353           last_bytes -= 2;
10354           if (last_bytes)
10355             {
10356               rtx tmp = gen_reg_rtx (SImode);
10357               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
10358               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
10359               part_bytes_reg = tmp;
10360               dstoffset += 2;
10361             }
10362         }
10363
10364       if (last_bytes)
10365         {
10366           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
10367           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
10368         }
10369     }
10370
10371   return 1;
10372 }
10373
10374 /* Select a dominance comparison mode if possible for a test of the general
10375    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
10376    COND_OR == DOM_CC_X_AND_Y => (X && Y)
10377    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
10378    COND_OR == DOM_CC_X_OR_Y => (X || Y)
10379    In all cases OP will be either EQ or NE, but we don't need to know which
10380    here.  If we are unable to support a dominance comparison we return
10381    CC mode.  This will then fail to match for the RTL expressions that
10382    generate this call.  */
10383 enum machine_mode
10384 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
10385 {
10386   enum rtx_code cond1, cond2;
10387   int swapped = 0;
10388
10389   /* Currently we will probably get the wrong result if the individual
10390      comparisons are not simple.  This also ensures that it is safe to
10391      reverse a comparison if necessary.  */
10392   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
10393        != CCmode)
10394       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
10395           != CCmode))
10396     return CCmode;
10397
10398   /* The if_then_else variant of this tests the second condition if the
10399      first passes, but is true if the first fails.  Reverse the first
10400      condition to get a true "inclusive-or" expression.  */
10401   if (cond_or == DOM_CC_NX_OR_Y)
10402     cond1 = reverse_condition (cond1);
10403
10404   /* If the comparisons are not equal, and one doesn't dominate the other,
10405      then we can't do this.  */
10406   if (cond1 != cond2
10407       && !comparison_dominates_p (cond1, cond2)
10408       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
10409     return CCmode;
10410
10411   if (swapped)
10412     {
10413       enum rtx_code temp = cond1;
10414       cond1 = cond2;
10415       cond2 = temp;
10416     }
10417
10418   switch (cond1)
10419     {
10420     case EQ:
10421       if (cond_or == DOM_CC_X_AND_Y)
10422         return CC_DEQmode;
10423
10424       switch (cond2)
10425         {
10426         case EQ: return CC_DEQmode;
10427         case LE: return CC_DLEmode;
10428         case LEU: return CC_DLEUmode;
10429         case GE: return CC_DGEmode;
10430         case GEU: return CC_DGEUmode;
10431         default: gcc_unreachable ();
10432         }
10433
10434     case LT:
10435       if (cond_or == DOM_CC_X_AND_Y)
10436         return CC_DLTmode;
10437
10438       switch (cond2)
10439         {
10440         case  LT:
10441             return CC_DLTmode;
10442         case LE:
10443           return CC_DLEmode;
10444         case NE:
10445           return CC_DNEmode;
10446         default:
10447           gcc_unreachable ();
10448         }
10449
10450     case GT:
10451       if (cond_or == DOM_CC_X_AND_Y)
10452         return CC_DGTmode;
10453
10454       switch (cond2)
10455         {
10456         case GT:
10457           return CC_DGTmode;
10458         case GE:
10459           return CC_DGEmode;
10460         case NE:
10461           return CC_DNEmode;
10462         default:
10463           gcc_unreachable ();
10464         }
10465
10466     case LTU:
10467       if (cond_or == DOM_CC_X_AND_Y)
10468         return CC_DLTUmode;
10469
10470       switch (cond2)
10471         {
10472         case LTU:
10473           return CC_DLTUmode;
10474         case LEU:
10475           return CC_DLEUmode;
10476         case NE:
10477           return CC_DNEmode;
10478         default:
10479           gcc_unreachable ();
10480         }
10481
10482     case GTU:
10483       if (cond_or == DOM_CC_X_AND_Y)
10484         return CC_DGTUmode;
10485
10486       switch (cond2)
10487         {
10488         case GTU:
10489           return CC_DGTUmode;
10490         case GEU:
10491           return CC_DGEUmode;
10492         case NE:
10493           return CC_DNEmode;
10494         default:
10495           gcc_unreachable ();
10496         }
10497
10498     /* The remaining cases only occur when both comparisons are the
10499        same.  */
10500     case NE:
10501       gcc_assert (cond1 == cond2);
10502       return CC_DNEmode;
10503
10504     case LE:
10505       gcc_assert (cond1 == cond2);
10506       return CC_DLEmode;
10507
10508     case GE:
10509       gcc_assert (cond1 == cond2);
10510       return CC_DGEmode;
10511
10512     case LEU:
10513       gcc_assert (cond1 == cond2);
10514       return CC_DLEUmode;
10515
10516     case GEU:
10517       gcc_assert (cond1 == cond2);
10518       return CC_DGEUmode;
10519
10520     default:
10521       gcc_unreachable ();
10522     }
10523 }
10524
10525 enum machine_mode
10526 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
10527 {
10528   /* All floating point compares return CCFP if it is an equality
10529      comparison, and CCFPE otherwise.  */
10530   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
10531     {
10532       switch (op)
10533         {
10534         case EQ:
10535         case NE:
10536         case UNORDERED:
10537         case ORDERED:
10538         case UNLT:
10539         case UNLE:
10540         case UNGT:
10541         case UNGE:
10542         case UNEQ:
10543         case LTGT:
10544           return CCFPmode;
10545
10546         case LT:
10547         case LE:
10548         case GT:
10549         case GE:
10550           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
10551             return CCFPmode;
10552           return CCFPEmode;
10553
10554         default:
10555           gcc_unreachable ();
10556         }
10557     }
10558
10559   /* A compare with a shifted operand.  Because of canonicalization, the
10560      comparison will have to be swapped when we emit the assembler.  */
10561   if (GET_MODE (y) == SImode 
10562       && (REG_P (y) || (GET_CODE (y) == SUBREG))
10563       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
10564           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
10565           || GET_CODE (x) == ROTATERT))
10566     return CC_SWPmode;
10567
10568   /* This operation is performed swapped, but since we only rely on the Z
10569      flag we don't need an additional mode.  */
10570   if (GET_MODE (y) == SImode 
10571       && (REG_P (y) || (GET_CODE (y) == SUBREG))
10572       && GET_CODE (x) == NEG
10573       && (op == EQ || op == NE))
10574     return CC_Zmode;
10575
10576   /* This is a special case that is used by combine to allow a
10577      comparison of a shifted byte load to be split into a zero-extend
10578      followed by a comparison of the shifted integer (only valid for
10579      equalities and unsigned inequalities).  */
10580   if (GET_MODE (x) == SImode
10581       && GET_CODE (x) == ASHIFT
10582       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
10583       && GET_CODE (XEXP (x, 0)) == SUBREG
10584       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
10585       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
10586       && (op == EQ || op == NE
10587           || op == GEU || op == GTU || op == LTU || op == LEU)
10588       && GET_CODE (y) == CONST_INT)
10589     return CC_Zmode;
10590
10591   /* A construct for a conditional compare, if the false arm contains
10592      0, then both conditions must be true, otherwise either condition
10593      must be true.  Not all conditions are possible, so CCmode is
10594      returned if it can't be done.  */
10595   if (GET_CODE (x) == IF_THEN_ELSE
10596       && (XEXP (x, 2) == const0_rtx
10597           || XEXP (x, 2) == const1_rtx)
10598       && COMPARISON_P (XEXP (x, 0))
10599       && COMPARISON_P (XEXP (x, 1)))
10600     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
10601                                          INTVAL (XEXP (x, 2)));
10602
10603   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
10604   if (GET_CODE (x) == AND
10605       && COMPARISON_P (XEXP (x, 0))
10606       && COMPARISON_P (XEXP (x, 1)))
10607     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
10608                                          DOM_CC_X_AND_Y);
10609
10610   if (GET_CODE (x) == IOR
10611       && COMPARISON_P (XEXP (x, 0))
10612       && COMPARISON_P (XEXP (x, 1)))
10613     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
10614                                          DOM_CC_X_OR_Y);
10615
10616   /* An operation (on Thumb) where we want to test for a single bit.
10617      This is done by shifting that bit up into the top bit of a
10618      scratch register; we can then branch on the sign bit.  */
10619   if (TARGET_THUMB1
10620       && GET_MODE (x) == SImode
10621       && (op == EQ || op == NE)
10622       && GET_CODE (x) == ZERO_EXTRACT
10623       && XEXP (x, 1) == const1_rtx)
10624     return CC_Nmode;
10625
10626   /* An operation that sets the condition codes as a side-effect, the
10627      V flag is not set correctly, so we can only use comparisons where
10628      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
10629      instead.)  */
10630   /* ??? Does the ZERO_EXTRACT case really apply to thumb2?  */
10631   if (GET_MODE (x) == SImode
10632       && y == const0_rtx
10633       && (op == EQ || op == NE || op == LT || op == GE)
10634       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
10635           || GET_CODE (x) == AND || GET_CODE (x) == IOR
10636           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
10637           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
10638           || GET_CODE (x) == LSHIFTRT
10639           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
10640           || GET_CODE (x) == ROTATERT
10641           || (TARGET_32BIT && GET_CODE (x) == ZERO_EXTRACT)))
10642     return CC_NOOVmode;
10643
10644   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
10645     return CC_Zmode;
10646
10647   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
10648       && GET_CODE (x) == PLUS
10649       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
10650     return CC_Cmode;
10651
10652   if (GET_MODE (x) == DImode || GET_MODE (y) == DImode)
10653     {
10654       /* To keep things simple, always use the Cirrus cfcmp64 if it is
10655          available.  */
10656       if (TARGET_ARM && TARGET_HARD_FLOAT && TARGET_MAVERICK)
10657         return CCmode;
10658
10659       switch (op)
10660         {
10661         case EQ:
10662         case NE:
10663           /* A DImode comparison against zero can be implemented by
10664              or'ing the two halves together.  */
10665           if (y == const0_rtx)
10666             return CC_Zmode;
10667
10668           /* We can do an equality test in three Thumb instructions.  */
10669           if (!TARGET_ARM)
10670             return CC_Zmode;
10671
10672           /* FALLTHROUGH */
10673
10674         case LTU:
10675         case LEU:
10676         case GTU:
10677         case GEU:
10678           /* DImode unsigned comparisons can be implemented by cmp +
10679              cmpeq without a scratch register.  Not worth doing in
10680              Thumb-2.  */
10681           if (TARGET_ARM)
10682             return CC_CZmode;
10683
10684           /* FALLTHROUGH */
10685
10686         case LT:
10687         case LE:
10688         case GT:
10689         case GE:
10690           /* DImode signed and unsigned comparisons can be implemented
10691              by cmp + sbcs with a scratch register, but that does not
10692              set the Z flag - we must reverse GT/LE/GTU/LEU.  */
10693           gcc_assert (op != EQ && op != NE);
10694           return CC_NCVmode;
10695
10696         default:
10697           gcc_unreachable ();
10698         }
10699     }
10700
10701   return CCmode;
10702 }
10703
10704 /* X and Y are two things to compare using CODE.  Emit the compare insn and
10705    return the rtx for register 0 in the proper mode.  FP means this is a
10706    floating point compare: I don't think that it is needed on the arm.  */
10707 rtx
10708 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
10709 {
10710   enum machine_mode mode;
10711   rtx cc_reg;
10712   int dimode_comparison = GET_MODE (x) == DImode || GET_MODE (y) == DImode;
10713
10714   /* We might have X as a constant, Y as a register because of the predicates
10715      used for cmpdi.  If so, force X to a register here.  */
10716   if (dimode_comparison && !REG_P (x))
10717     x = force_reg (DImode, x);
10718
10719   mode = SELECT_CC_MODE (code, x, y);
10720   cc_reg = gen_rtx_REG (mode, CC_REGNUM);
10721
10722   if (dimode_comparison
10723       && !(TARGET_HARD_FLOAT && TARGET_MAVERICK)
10724       && mode != CC_CZmode)
10725     {
10726       rtx clobber, set;
10727
10728       /* To compare two non-zero values for equality, XOR them and
10729          then compare against zero.  Not used for ARM mode; there
10730          CC_CZmode is cheaper.  */
10731       if (mode == CC_Zmode && y != const0_rtx)
10732         {
10733           x = expand_binop (DImode, xor_optab, x, y, NULL_RTX, 0, OPTAB_WIDEN);
10734           y = const0_rtx;
10735         }
10736       /* A scratch register is required.  */
10737       clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (SImode));
10738       set = gen_rtx_SET (VOIDmode, cc_reg, gen_rtx_COMPARE (mode, x, y));
10739       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
10740     }
10741   else
10742     emit_set_insn (cc_reg, gen_rtx_COMPARE (mode, x, y));
10743
10744   return cc_reg;
10745 }
10746
10747 /* Generate a sequence of insns that will generate the correct return
10748    address mask depending on the physical architecture that the program
10749    is running on.  */
10750 rtx
10751 arm_gen_return_addr_mask (void)
10752 {
10753   rtx reg = gen_reg_rtx (Pmode);
10754
10755   emit_insn (gen_return_addr_mask (reg));
10756   return reg;
10757 }
10758
10759 void
10760 arm_reload_in_hi (rtx *operands)
10761 {
10762   rtx ref = operands[1];
10763   rtx base, scratch;
10764   HOST_WIDE_INT offset = 0;
10765
10766   if (GET_CODE (ref) == SUBREG)
10767     {
10768       offset = SUBREG_BYTE (ref);
10769       ref = SUBREG_REG (ref);
10770     }
10771
10772   if (GET_CODE (ref) == REG)
10773     {
10774       /* We have a pseudo which has been spilt onto the stack; there
10775          are two cases here: the first where there is a simple
10776          stack-slot replacement and a second where the stack-slot is
10777          out of range, or is used as a subreg.  */
10778       if (reg_equiv_mem[REGNO (ref)])
10779         {
10780           ref = reg_equiv_mem[REGNO (ref)];
10781           base = find_replacement (&XEXP (ref, 0));
10782         }
10783       else
10784         /* The slot is out of range, or was dressed up in a SUBREG.  */
10785         base = reg_equiv_address[REGNO (ref)];
10786     }
10787   else
10788     base = find_replacement (&XEXP (ref, 0));
10789
10790   /* Handle the case where the address is too complex to be offset by 1.  */
10791   if (GET_CODE (base) == MINUS
10792       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
10793     {
10794       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10795
10796       emit_set_insn (base_plus, base);
10797       base = base_plus;
10798     }
10799   else if (GET_CODE (base) == PLUS)
10800     {
10801       /* The addend must be CONST_INT, or we would have dealt with it above.  */
10802       HOST_WIDE_INT hi, lo;
10803
10804       offset += INTVAL (XEXP (base, 1));
10805       base = XEXP (base, 0);
10806
10807       /* Rework the address into a legal sequence of insns.  */
10808       /* Valid range for lo is -4095 -> 4095 */
10809       lo = (offset >= 0
10810             ? (offset & 0xfff)
10811             : -((-offset) & 0xfff));
10812
10813       /* Corner case, if lo is the max offset then we would be out of range
10814          once we have added the additional 1 below, so bump the msb into the
10815          pre-loading insn(s).  */
10816       if (lo == 4095)
10817         lo &= 0x7ff;
10818
10819       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
10820              ^ (HOST_WIDE_INT) 0x80000000)
10821             - (HOST_WIDE_INT) 0x80000000);
10822
10823       gcc_assert (hi + lo == offset);
10824
10825       if (hi != 0)
10826         {
10827           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10828
10829           /* Get the base address; addsi3 knows how to handle constants
10830              that require more than one insn.  */
10831           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
10832           base = base_plus;
10833           offset = lo;
10834         }
10835     }
10836
10837   /* Operands[2] may overlap operands[0] (though it won't overlap
10838      operands[1]), that's why we asked for a DImode reg -- so we can
10839      use the bit that does not overlap.  */
10840   if (REGNO (operands[2]) == REGNO (operands[0]))
10841     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10842   else
10843     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
10844
10845   emit_insn (gen_zero_extendqisi2 (scratch,
10846                                    gen_rtx_MEM (QImode,
10847                                                 plus_constant (base,
10848                                                                offset))));
10849   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
10850                                    gen_rtx_MEM (QImode,
10851                                                 plus_constant (base,
10852                                                                offset + 1))));
10853   if (!BYTES_BIG_ENDIAN)
10854     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
10855                    gen_rtx_IOR (SImode,
10856                                 gen_rtx_ASHIFT
10857                                 (SImode,
10858                                  gen_rtx_SUBREG (SImode, operands[0], 0),
10859                                  GEN_INT (8)),
10860                                 scratch));
10861   else
10862     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
10863                    gen_rtx_IOR (SImode,
10864                                 gen_rtx_ASHIFT (SImode, scratch,
10865                                                 GEN_INT (8)),
10866                                 gen_rtx_SUBREG (SImode, operands[0], 0)));
10867 }
10868
10869 /* Handle storing a half-word to memory during reload by synthesizing as two
10870    byte stores.  Take care not to clobber the input values until after we
10871    have moved them somewhere safe.  This code assumes that if the DImode
10872    scratch in operands[2] overlaps either the input value or output address
10873    in some way, then that value must die in this insn (we absolutely need
10874    two scratch registers for some corner cases).  */
10875 void
10876 arm_reload_out_hi (rtx *operands)
10877 {
10878   rtx ref = operands[0];
10879   rtx outval = operands[1];
10880   rtx base, scratch;
10881   HOST_WIDE_INT offset = 0;
10882
10883   if (GET_CODE (ref) == SUBREG)
10884     {
10885       offset = SUBREG_BYTE (ref);
10886       ref = SUBREG_REG (ref);
10887     }
10888
10889   if (GET_CODE (ref) == REG)
10890     {
10891       /* We have a pseudo which has been spilt onto the stack; there
10892          are two cases here: the first where there is a simple
10893          stack-slot replacement and a second where the stack-slot is
10894          out of range, or is used as a subreg.  */
10895       if (reg_equiv_mem[REGNO (ref)])
10896         {
10897           ref = reg_equiv_mem[REGNO (ref)];
10898           base = find_replacement (&XEXP (ref, 0));
10899         }
10900       else
10901         /* The slot is out of range, or was dressed up in a SUBREG.  */
10902         base = reg_equiv_address[REGNO (ref)];
10903     }
10904   else
10905     base = find_replacement (&XEXP (ref, 0));
10906
10907   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
10908
10909   /* Handle the case where the address is too complex to be offset by 1.  */
10910   if (GET_CODE (base) == MINUS
10911       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
10912     {
10913       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10914
10915       /* Be careful not to destroy OUTVAL.  */
10916       if (reg_overlap_mentioned_p (base_plus, outval))
10917         {
10918           /* Updating base_plus might destroy outval, see if we can
10919              swap the scratch and base_plus.  */
10920           if (!reg_overlap_mentioned_p (scratch, outval))
10921             {
10922               rtx tmp = scratch;
10923               scratch = base_plus;
10924               base_plus = tmp;
10925             }
10926           else
10927             {
10928               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
10929
10930               /* Be conservative and copy OUTVAL into the scratch now,
10931                  this should only be necessary if outval is a subreg
10932                  of something larger than a word.  */
10933               /* XXX Might this clobber base?  I can't see how it can,
10934                  since scratch is known to overlap with OUTVAL, and
10935                  must be wider than a word.  */
10936               emit_insn (gen_movhi (scratch_hi, outval));
10937               outval = scratch_hi;
10938             }
10939         }
10940
10941       emit_set_insn (base_plus, base);
10942       base = base_plus;
10943     }
10944   else if (GET_CODE (base) == PLUS)
10945     {
10946       /* The addend must be CONST_INT, or we would have dealt with it above.  */
10947       HOST_WIDE_INT hi, lo;
10948
10949       offset += INTVAL (XEXP (base, 1));
10950       base = XEXP (base, 0);
10951
10952       /* Rework the address into a legal sequence of insns.  */
10953       /* Valid range for lo is -4095 -> 4095 */
10954       lo = (offset >= 0
10955             ? (offset & 0xfff)
10956             : -((-offset) & 0xfff));
10957
10958       /* Corner case, if lo is the max offset then we would be out of range
10959          once we have added the additional 1 below, so bump the msb into the
10960          pre-loading insn(s).  */
10961       if (lo == 4095)
10962         lo &= 0x7ff;
10963
10964       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
10965              ^ (HOST_WIDE_INT) 0x80000000)
10966             - (HOST_WIDE_INT) 0x80000000);
10967
10968       gcc_assert (hi + lo == offset);
10969
10970       if (hi != 0)
10971         {
10972           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
10973
10974           /* Be careful not to destroy OUTVAL.  */
10975           if (reg_overlap_mentioned_p (base_plus, outval))
10976             {
10977               /* Updating base_plus might destroy outval, see if we
10978                  can swap the scratch and base_plus.  */
10979               if (!reg_overlap_mentioned_p (scratch, outval))
10980                 {
10981                   rtx tmp = scratch;
10982                   scratch = base_plus;
10983                   base_plus = tmp;
10984                 }
10985               else
10986                 {
10987                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
10988
10989                   /* Be conservative and copy outval into scratch now,
10990                      this should only be necessary if outval is a
10991                      subreg of something larger than a word.  */
10992                   /* XXX Might this clobber base?  I can't see how it
10993                      can, since scratch is known to overlap with
10994                      outval.  */
10995                   emit_insn (gen_movhi (scratch_hi, outval));
10996                   outval = scratch_hi;
10997                 }
10998             }
10999
11000           /* Get the base address; addsi3 knows how to handle constants
11001              that require more than one insn.  */
11002           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
11003           base = base_plus;
11004           offset = lo;
11005         }
11006     }
11007
11008   if (BYTES_BIG_ENDIAN)
11009     {
11010       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
11011                                          plus_constant (base, offset + 1)),
11012                             gen_lowpart (QImode, outval)));
11013       emit_insn (gen_lshrsi3 (scratch,
11014                               gen_rtx_SUBREG (SImode, outval, 0),
11015                               GEN_INT (8)));
11016       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
11017                             gen_lowpart (QImode, scratch)));
11018     }
11019   else
11020     {
11021       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
11022                             gen_lowpart (QImode, outval)));
11023       emit_insn (gen_lshrsi3 (scratch,
11024                               gen_rtx_SUBREG (SImode, outval, 0),
11025                               GEN_INT (8)));
11026       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
11027                                          plus_constant (base, offset + 1)),
11028                             gen_lowpart (QImode, scratch)));
11029     }
11030 }
11031
11032 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
11033    (padded to the size of a word) should be passed in a register.  */
11034
11035 static bool
11036 arm_must_pass_in_stack (enum machine_mode mode, const_tree type)
11037 {
11038   if (TARGET_AAPCS_BASED)
11039     return must_pass_in_stack_var_size (mode, type);
11040   else
11041     return must_pass_in_stack_var_size_or_pad (mode, type);
11042 }
11043
11044
11045 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
11046    Return true if an argument passed on the stack should be padded upwards,
11047    i.e. if the least-significant byte has useful data.
11048    For legacy APCS ABIs we use the default.  For AAPCS based ABIs small
11049    aggregate types are placed in the lowest memory address.  */
11050
11051 bool
11052 arm_pad_arg_upward (enum machine_mode mode, const_tree type)
11053 {
11054   if (!TARGET_AAPCS_BASED)
11055     return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
11056
11057   if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
11058     return false;
11059
11060   return true;
11061 }
11062
11063
11064 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
11065    For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
11066    byte of the register has useful data, and return the opposite if the
11067    most significant byte does.
11068    For AAPCS, small aggregates and small complex types are always padded
11069    upwards.  */
11070
11071 bool
11072 arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
11073                     tree type, int first ATTRIBUTE_UNUSED)
11074 {
11075   if (TARGET_AAPCS_BASED
11076       && BYTES_BIG_ENDIAN
11077       && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11078       && int_size_in_bytes (type) <= 4)
11079     return true;
11080
11081   /* Otherwise, use default padding.  */
11082   return !BYTES_BIG_ENDIAN;
11083 }
11084
11085 \f
11086 /* Print a symbolic form of X to the debug file, F.  */
11087 static void
11088 arm_print_value (FILE *f, rtx x)
11089 {
11090   switch (GET_CODE (x))
11091     {
11092     case CONST_INT:
11093       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
11094       return;
11095
11096     case CONST_DOUBLE:
11097       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
11098       return;
11099
11100     case CONST_VECTOR:
11101       {
11102         int i;
11103
11104         fprintf (f, "<");
11105         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
11106           {
11107             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
11108             if (i < (CONST_VECTOR_NUNITS (x) - 1))
11109               fputc (',', f);
11110           }
11111         fprintf (f, ">");
11112       }
11113       return;
11114
11115     case CONST_STRING:
11116       fprintf (f, "\"%s\"", XSTR (x, 0));
11117       return;
11118
11119     case SYMBOL_REF:
11120       fprintf (f, "`%s'", XSTR (x, 0));
11121       return;
11122
11123     case LABEL_REF:
11124       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
11125       return;
11126
11127     case CONST:
11128       arm_print_value (f, XEXP (x, 0));
11129       return;
11130
11131     case PLUS:
11132       arm_print_value (f, XEXP (x, 0));
11133       fprintf (f, "+");
11134       arm_print_value (f, XEXP (x, 1));
11135       return;
11136
11137     case PC:
11138       fprintf (f, "pc");
11139       return;
11140
11141     default:
11142       fprintf (f, "????");
11143       return;
11144     }
11145 }
11146 \f
11147 /* Routines for manipulation of the constant pool.  */
11148
11149 /* Arm instructions cannot load a large constant directly into a
11150    register; they have to come from a pc relative load.  The constant
11151    must therefore be placed in the addressable range of the pc
11152    relative load.  Depending on the precise pc relative load
11153    instruction the range is somewhere between 256 bytes and 4k.  This
11154    means that we often have to dump a constant inside a function, and
11155    generate code to branch around it.
11156
11157    It is important to minimize this, since the branches will slow
11158    things down and make the code larger.
11159
11160    Normally we can hide the table after an existing unconditional
11161    branch so that there is no interruption of the flow, but in the
11162    worst case the code looks like this:
11163
11164         ldr     rn, L1
11165         ...
11166         b       L2
11167         align
11168         L1:     .long value
11169         L2:
11170         ...
11171
11172         ldr     rn, L3
11173         ...
11174         b       L4
11175         align
11176         L3:     .long value
11177         L4:
11178         ...
11179
11180    We fix this by performing a scan after scheduling, which notices
11181    which instructions need to have their operands fetched from the
11182    constant table and builds the table.
11183
11184    The algorithm starts by building a table of all the constants that
11185    need fixing up and all the natural barriers in the function (places
11186    where a constant table can be dropped without breaking the flow).
11187    For each fixup we note how far the pc-relative replacement will be
11188    able to reach and the offset of the instruction into the function.
11189
11190    Having built the table we then group the fixes together to form
11191    tables that are as large as possible (subject to addressing
11192    constraints) and emit each table of constants after the last
11193    barrier that is within range of all the instructions in the group.
11194    If a group does not contain a barrier, then we forcibly create one
11195    by inserting a jump instruction into the flow.  Once the table has
11196    been inserted, the insns are then modified to reference the
11197    relevant entry in the pool.
11198
11199    Possible enhancements to the algorithm (not implemented) are:
11200
11201    1) For some processors and object formats, there may be benefit in
11202    aligning the pools to the start of cache lines; this alignment
11203    would need to be taken into account when calculating addressability
11204    of a pool.  */
11205
11206 /* These typedefs are located at the start of this file, so that
11207    they can be used in the prototypes there.  This comment is to
11208    remind readers of that fact so that the following structures
11209    can be understood more easily.
11210
11211      typedef struct minipool_node    Mnode;
11212      typedef struct minipool_fixup   Mfix;  */
11213
11214 struct minipool_node
11215 {
11216   /* Doubly linked chain of entries.  */
11217   Mnode * next;
11218   Mnode * prev;
11219   /* The maximum offset into the code that this entry can be placed.  While
11220      pushing fixes for forward references, all entries are sorted in order
11221      of increasing max_address.  */
11222   HOST_WIDE_INT max_address;
11223   /* Similarly for an entry inserted for a backwards ref.  */
11224   HOST_WIDE_INT min_address;
11225   /* The number of fixes referencing this entry.  This can become zero
11226      if we "unpush" an entry.  In this case we ignore the entry when we
11227      come to emit the code.  */
11228   int refcount;
11229   /* The offset from the start of the minipool.  */
11230   HOST_WIDE_INT offset;
11231   /* The value in table.  */
11232   rtx value;
11233   /* The mode of value.  */
11234   enum machine_mode mode;
11235   /* The size of the value.  With iWMMXt enabled
11236      sizes > 4 also imply an alignment of 8-bytes.  */
11237   int fix_size;
11238 };
11239
11240 struct minipool_fixup
11241 {
11242   Mfix *            next;
11243   rtx               insn;
11244   HOST_WIDE_INT     address;
11245   rtx *             loc;
11246   enum machine_mode mode;
11247   int               fix_size;
11248   rtx               value;
11249   Mnode *           minipool;
11250   HOST_WIDE_INT     forwards;
11251   HOST_WIDE_INT     backwards;
11252 };
11253
11254 /* Fixes less than a word need padding out to a word boundary.  */
11255 #define MINIPOOL_FIX_SIZE(mode) \
11256   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
11257
11258 static Mnode *  minipool_vector_head;
11259 static Mnode *  minipool_vector_tail;
11260 static rtx      minipool_vector_label;
11261 static int      minipool_pad;
11262
11263 /* The linked list of all minipool fixes required for this function.  */
11264 Mfix *          minipool_fix_head;
11265 Mfix *          minipool_fix_tail;
11266 /* The fix entry for the current minipool, once it has been placed.  */
11267 Mfix *          minipool_barrier;
11268
11269 /* Determines if INSN is the start of a jump table.  Returns the end
11270    of the TABLE or NULL_RTX.  */
11271 static rtx
11272 is_jump_table (rtx insn)
11273 {
11274   rtx table;
11275
11276   if (GET_CODE (insn) == JUMP_INSN
11277       && JUMP_LABEL (insn) != NULL
11278       && ((table = next_real_insn (JUMP_LABEL (insn)))
11279           == next_real_insn (insn))
11280       && table != NULL
11281       && GET_CODE (table) == JUMP_INSN
11282       && (GET_CODE (PATTERN (table)) == ADDR_VEC
11283           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
11284     return table;
11285
11286   return NULL_RTX;
11287 }
11288
11289 #ifndef JUMP_TABLES_IN_TEXT_SECTION
11290 #define JUMP_TABLES_IN_TEXT_SECTION 0
11291 #endif
11292
11293 static HOST_WIDE_INT
11294 get_jump_table_size (rtx insn)
11295 {
11296   /* ADDR_VECs only take room if read-only data does into the text
11297      section.  */
11298   if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
11299     {
11300       rtx body = PATTERN (insn);
11301       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
11302       HOST_WIDE_INT size;
11303       HOST_WIDE_INT modesize;
11304
11305       modesize = GET_MODE_SIZE (GET_MODE (body));
11306       size = modesize * XVECLEN (body, elt);
11307       switch (modesize)
11308         {
11309         case 1:
11310           /* Round up size  of TBB table to a halfword boundary.  */
11311           size = (size + 1) & ~(HOST_WIDE_INT)1;
11312           break;
11313         case 2:
11314           /* No padding necessary for TBH.  */
11315           break;
11316         case 4:
11317           /* Add two bytes for alignment on Thumb.  */
11318           if (TARGET_THUMB)
11319             size += 2;
11320           break;
11321         default:
11322           gcc_unreachable ();
11323         }
11324       return size;
11325     }
11326
11327   return 0;
11328 }
11329
11330 /* Move a minipool fix MP from its current location to before MAX_MP.
11331    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
11332    constraints may need updating.  */
11333 static Mnode *
11334 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
11335                                HOST_WIDE_INT max_address)
11336 {
11337   /* The code below assumes these are different.  */
11338   gcc_assert (mp != max_mp);
11339
11340   if (max_mp == NULL)
11341     {
11342       if (max_address < mp->max_address)
11343         mp->max_address = max_address;
11344     }
11345   else
11346     {
11347       if (max_address > max_mp->max_address - mp->fix_size)
11348         mp->max_address = max_mp->max_address - mp->fix_size;
11349       else
11350         mp->max_address = max_address;
11351
11352       /* Unlink MP from its current position.  Since max_mp is non-null,
11353        mp->prev must be non-null.  */
11354       mp->prev->next = mp->next;
11355       if (mp->next != NULL)
11356         mp->next->prev = mp->prev;
11357       else
11358         minipool_vector_tail = mp->prev;
11359
11360       /* Re-insert it before MAX_MP.  */
11361       mp->next = max_mp;
11362       mp->prev = max_mp->prev;
11363       max_mp->prev = mp;
11364
11365       if (mp->prev != NULL)
11366         mp->prev->next = mp;
11367       else
11368         minipool_vector_head = mp;
11369     }
11370
11371   /* Save the new entry.  */
11372   max_mp = mp;
11373
11374   /* Scan over the preceding entries and adjust their addresses as
11375      required.  */
11376   while (mp->prev != NULL
11377          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
11378     {
11379       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
11380       mp = mp->prev;
11381     }
11382
11383   return max_mp;
11384 }
11385
11386 /* Add a constant to the minipool for a forward reference.  Returns the
11387    node added or NULL if the constant will not fit in this pool.  */
11388 static Mnode *
11389 add_minipool_forward_ref (Mfix *fix)
11390 {
11391   /* If set, max_mp is the first pool_entry that has a lower
11392      constraint than the one we are trying to add.  */
11393   Mnode *       max_mp = NULL;
11394   HOST_WIDE_INT max_address = fix->address + fix->forwards - minipool_pad;
11395   Mnode *       mp;
11396
11397   /* If the minipool starts before the end of FIX->INSN then this FIX
11398      can not be placed into the current pool.  Furthermore, adding the
11399      new constant pool entry may cause the pool to start FIX_SIZE bytes
11400      earlier.  */
11401   if (minipool_vector_head &&
11402       (fix->address + get_attr_length (fix->insn)
11403        >= minipool_vector_head->max_address - fix->fix_size))
11404     return NULL;
11405
11406   /* Scan the pool to see if a constant with the same value has
11407      already been added.  While we are doing this, also note the
11408      location where we must insert the constant if it doesn't already
11409      exist.  */
11410   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11411     {
11412       if (GET_CODE (fix->value) == GET_CODE (mp->value)
11413           && fix->mode == mp->mode
11414           && (GET_CODE (fix->value) != CODE_LABEL
11415               || (CODE_LABEL_NUMBER (fix->value)
11416                   == CODE_LABEL_NUMBER (mp->value)))
11417           && rtx_equal_p (fix->value, mp->value))
11418         {
11419           /* More than one fix references this entry.  */
11420           mp->refcount++;
11421           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
11422         }
11423
11424       /* Note the insertion point if necessary.  */
11425       if (max_mp == NULL
11426           && mp->max_address > max_address)
11427         max_mp = mp;
11428
11429       /* If we are inserting an 8-bytes aligned quantity and
11430          we have not already found an insertion point, then
11431          make sure that all such 8-byte aligned quantities are
11432          placed at the start of the pool.  */
11433       if (ARM_DOUBLEWORD_ALIGN
11434           && max_mp == NULL
11435           && fix->fix_size >= 8
11436           && mp->fix_size < 8)
11437         {
11438           max_mp = mp;
11439           max_address = mp->max_address;
11440         }
11441     }
11442
11443   /* The value is not currently in the minipool, so we need to create
11444      a new entry for it.  If MAX_MP is NULL, the entry will be put on
11445      the end of the list since the placement is less constrained than
11446      any existing entry.  Otherwise, we insert the new fix before
11447      MAX_MP and, if necessary, adjust the constraints on the other
11448      entries.  */
11449   mp = XNEW (Mnode);
11450   mp->fix_size = fix->fix_size;
11451   mp->mode = fix->mode;
11452   mp->value = fix->value;
11453   mp->refcount = 1;
11454   /* Not yet required for a backwards ref.  */
11455   mp->min_address = -65536;
11456
11457   if (max_mp == NULL)
11458     {
11459       mp->max_address = max_address;
11460       mp->next = NULL;
11461       mp->prev = minipool_vector_tail;
11462
11463       if (mp->prev == NULL)
11464         {
11465           minipool_vector_head = mp;
11466           minipool_vector_label = gen_label_rtx ();
11467         }
11468       else
11469         mp->prev->next = mp;
11470
11471       minipool_vector_tail = mp;
11472     }
11473   else
11474     {
11475       if (max_address > max_mp->max_address - mp->fix_size)
11476         mp->max_address = max_mp->max_address - mp->fix_size;
11477       else
11478         mp->max_address = max_address;
11479
11480       mp->next = max_mp;
11481       mp->prev = max_mp->prev;
11482       max_mp->prev = mp;
11483       if (mp->prev != NULL)
11484         mp->prev->next = mp;
11485       else
11486         minipool_vector_head = mp;
11487     }
11488
11489   /* Save the new entry.  */
11490   max_mp = mp;
11491
11492   /* Scan over the preceding entries and adjust their addresses as
11493      required.  */
11494   while (mp->prev != NULL
11495          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
11496     {
11497       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
11498       mp = mp->prev;
11499     }
11500
11501   return max_mp;
11502 }
11503
11504 static Mnode *
11505 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
11506                                 HOST_WIDE_INT  min_address)
11507 {
11508   HOST_WIDE_INT offset;
11509
11510   /* The code below assumes these are different.  */
11511   gcc_assert (mp != min_mp);
11512
11513   if (min_mp == NULL)
11514     {
11515       if (min_address > mp->min_address)
11516         mp->min_address = min_address;
11517     }
11518   else
11519     {
11520       /* We will adjust this below if it is too loose.  */
11521       mp->min_address = min_address;
11522
11523       /* Unlink MP from its current position.  Since min_mp is non-null,
11524          mp->next must be non-null.  */
11525       mp->next->prev = mp->prev;
11526       if (mp->prev != NULL)
11527         mp->prev->next = mp->next;
11528       else
11529         minipool_vector_head = mp->next;
11530
11531       /* Reinsert it after MIN_MP.  */
11532       mp->prev = min_mp;
11533       mp->next = min_mp->next;
11534       min_mp->next = mp;
11535       if (mp->next != NULL)
11536         mp->next->prev = mp;
11537       else
11538         minipool_vector_tail = mp;
11539     }
11540
11541   min_mp = mp;
11542
11543   offset = 0;
11544   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11545     {
11546       mp->offset = offset;
11547       if (mp->refcount > 0)
11548         offset += mp->fix_size;
11549
11550       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
11551         mp->next->min_address = mp->min_address + mp->fix_size;
11552     }
11553
11554   return min_mp;
11555 }
11556
11557 /* Add a constant to the minipool for a backward reference.  Returns the
11558    node added or NULL if the constant will not fit in this pool.
11559
11560    Note that the code for insertion for a backwards reference can be
11561    somewhat confusing because the calculated offsets for each fix do
11562    not take into account the size of the pool (which is still under
11563    construction.  */
11564 static Mnode *
11565 add_minipool_backward_ref (Mfix *fix)
11566 {
11567   /* If set, min_mp is the last pool_entry that has a lower constraint
11568      than the one we are trying to add.  */
11569   Mnode *min_mp = NULL;
11570   /* This can be negative, since it is only a constraint.  */
11571   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
11572   Mnode *mp;
11573
11574   /* If we can't reach the current pool from this insn, or if we can't
11575      insert this entry at the end of the pool without pushing other
11576      fixes out of range, then we don't try.  This ensures that we
11577      can't fail later on.  */
11578   if (min_address >= minipool_barrier->address
11579       || (minipool_vector_tail->min_address + fix->fix_size
11580           >= minipool_barrier->address))
11581     return NULL;
11582
11583   /* Scan the pool to see if a constant with the same value has
11584      already been added.  While we are doing this, also note the
11585      location where we must insert the constant if it doesn't already
11586      exist.  */
11587   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
11588     {
11589       if (GET_CODE (fix->value) == GET_CODE (mp->value)
11590           && fix->mode == mp->mode
11591           && (GET_CODE (fix->value) != CODE_LABEL
11592               || (CODE_LABEL_NUMBER (fix->value)
11593                   == CODE_LABEL_NUMBER (mp->value)))
11594           && rtx_equal_p (fix->value, mp->value)
11595           /* Check that there is enough slack to move this entry to the
11596              end of the table (this is conservative).  */
11597           && (mp->max_address
11598               > (minipool_barrier->address
11599                  + minipool_vector_tail->offset
11600                  + minipool_vector_tail->fix_size)))
11601         {
11602           mp->refcount++;
11603           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
11604         }
11605
11606       if (min_mp != NULL)
11607         mp->min_address += fix->fix_size;
11608       else
11609         {
11610           /* Note the insertion point if necessary.  */
11611           if (mp->min_address < min_address)
11612             {
11613               /* For now, we do not allow the insertion of 8-byte alignment
11614                  requiring nodes anywhere but at the start of the pool.  */
11615               if (ARM_DOUBLEWORD_ALIGN
11616                   && fix->fix_size >= 8 && mp->fix_size < 8)
11617                 return NULL;
11618               else
11619                 min_mp = mp;
11620             }
11621           else if (mp->max_address
11622                    < minipool_barrier->address + mp->offset + fix->fix_size)
11623             {
11624               /* Inserting before this entry would push the fix beyond
11625                  its maximum address (which can happen if we have
11626                  re-located a forwards fix); force the new fix to come
11627                  after it.  */
11628               if (ARM_DOUBLEWORD_ALIGN
11629                   && fix->fix_size >= 8 && mp->fix_size < 8)
11630                 return NULL;
11631               else
11632                 {
11633                   min_mp = mp;
11634                   min_address = mp->min_address + fix->fix_size;
11635                 }
11636             }
11637           /* Do not insert a non-8-byte aligned quantity before 8-byte
11638              aligned quantities.  */
11639           else if (ARM_DOUBLEWORD_ALIGN
11640                    && fix->fix_size < 8
11641                    && mp->fix_size >= 8)
11642             {
11643               min_mp = mp;
11644               min_address = mp->min_address + fix->fix_size;
11645             }
11646         }
11647     }
11648
11649   /* We need to create a new entry.  */
11650   mp = XNEW (Mnode);
11651   mp->fix_size = fix->fix_size;
11652   mp->mode = fix->mode;
11653   mp->value = fix->value;
11654   mp->refcount = 1;
11655   mp->max_address = minipool_barrier->address + 65536;
11656
11657   mp->min_address = min_address;
11658
11659   if (min_mp == NULL)
11660     {
11661       mp->prev = NULL;
11662       mp->next = minipool_vector_head;
11663
11664       if (mp->next == NULL)
11665         {
11666           minipool_vector_tail = mp;
11667           minipool_vector_label = gen_label_rtx ();
11668         }
11669       else
11670         mp->next->prev = mp;
11671
11672       minipool_vector_head = mp;
11673     }
11674   else
11675     {
11676       mp->next = min_mp->next;
11677       mp->prev = min_mp;
11678       min_mp->next = mp;
11679
11680       if (mp->next != NULL)
11681         mp->next->prev = mp;
11682       else
11683         minipool_vector_tail = mp;
11684     }
11685
11686   /* Save the new entry.  */
11687   min_mp = mp;
11688
11689   if (mp->prev)
11690     mp = mp->prev;
11691   else
11692     mp->offset = 0;
11693
11694   /* Scan over the following entries and adjust their offsets.  */
11695   while (mp->next != NULL)
11696     {
11697       if (mp->next->min_address < mp->min_address + mp->fix_size)
11698         mp->next->min_address = mp->min_address + mp->fix_size;
11699
11700       if (mp->refcount)
11701         mp->next->offset = mp->offset + mp->fix_size;
11702       else
11703         mp->next->offset = mp->offset;
11704
11705       mp = mp->next;
11706     }
11707
11708   return min_mp;
11709 }
11710
11711 static void
11712 assign_minipool_offsets (Mfix *barrier)
11713 {
11714   HOST_WIDE_INT offset = 0;
11715   Mnode *mp;
11716
11717   minipool_barrier = barrier;
11718
11719   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11720     {
11721       mp->offset = offset;
11722
11723       if (mp->refcount > 0)
11724         offset += mp->fix_size;
11725     }
11726 }
11727
11728 /* Output the literal table */
11729 static void
11730 dump_minipool (rtx scan)
11731 {
11732   Mnode * mp;
11733   Mnode * nmp;
11734   int align64 = 0;
11735
11736   if (ARM_DOUBLEWORD_ALIGN)
11737     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
11738       if (mp->refcount > 0 && mp->fix_size >= 8)
11739         {
11740           align64 = 1;
11741           break;
11742         }
11743
11744   if (dump_file)
11745     fprintf (dump_file,
11746              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
11747              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
11748
11749   scan = emit_label_after (gen_label_rtx (), scan);
11750   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
11751   scan = emit_label_after (minipool_vector_label, scan);
11752
11753   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
11754     {
11755       if (mp->refcount > 0)
11756         {
11757           if (dump_file)
11758             {
11759               fprintf (dump_file,
11760                        ";;  Offset %u, min %ld, max %ld ",
11761                        (unsigned) mp->offset, (unsigned long) mp->min_address,
11762                        (unsigned long) mp->max_address);
11763               arm_print_value (dump_file, mp->value);
11764               fputc ('\n', dump_file);
11765             }
11766
11767           switch (mp->fix_size)
11768             {
11769 #ifdef HAVE_consttable_1
11770             case 1:
11771               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
11772               break;
11773
11774 #endif
11775 #ifdef HAVE_consttable_2
11776             case 2:
11777               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
11778               break;
11779
11780 #endif
11781 #ifdef HAVE_consttable_4
11782             case 4:
11783               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
11784               break;
11785
11786 #endif
11787 #ifdef HAVE_consttable_8
11788             case 8:
11789               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
11790               break;
11791
11792 #endif
11793 #ifdef HAVE_consttable_16
11794             case 16:
11795               scan = emit_insn_after (gen_consttable_16 (mp->value), scan);
11796               break;
11797
11798 #endif
11799             default:
11800               gcc_unreachable ();
11801             }
11802         }
11803
11804       nmp = mp->next;
11805       free (mp);
11806     }
11807
11808   minipool_vector_head = minipool_vector_tail = NULL;
11809   scan = emit_insn_after (gen_consttable_end (), scan);
11810   scan = emit_barrier_after (scan);
11811 }
11812
11813 /* Return the cost of forcibly inserting a barrier after INSN.  */
11814 static int
11815 arm_barrier_cost (rtx insn)
11816 {
11817   /* Basing the location of the pool on the loop depth is preferable,
11818      but at the moment, the basic block information seems to be
11819      corrupt by this stage of the compilation.  */
11820   int base_cost = 50;
11821   rtx next = next_nonnote_insn (insn);
11822
11823   if (next != NULL && GET_CODE (next) == CODE_LABEL)
11824     base_cost -= 20;
11825
11826   switch (GET_CODE (insn))
11827     {
11828     case CODE_LABEL:
11829       /* It will always be better to place the table before the label, rather
11830          than after it.  */
11831       return 50;
11832
11833     case INSN:
11834     case CALL_INSN:
11835       return base_cost;
11836
11837     case JUMP_INSN:
11838       return base_cost - 10;
11839
11840     default:
11841       return base_cost + 10;
11842     }
11843 }
11844
11845 /* Find the best place in the insn stream in the range
11846    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
11847    Create the barrier by inserting a jump and add a new fix entry for
11848    it.  */
11849 static Mfix *
11850 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
11851 {
11852   HOST_WIDE_INT count = 0;
11853   rtx barrier;
11854   rtx from = fix->insn;
11855   /* The instruction after which we will insert the jump.  */
11856   rtx selected = NULL;
11857   int selected_cost;
11858   /* The address at which the jump instruction will be placed.  */
11859   HOST_WIDE_INT selected_address;
11860   Mfix * new_fix;
11861   HOST_WIDE_INT max_count = max_address - fix->address;
11862   rtx label = gen_label_rtx ();
11863
11864   selected_cost = arm_barrier_cost (from);
11865   selected_address = fix->address;
11866
11867   while (from && count < max_count)
11868     {
11869       rtx tmp;
11870       int new_cost;
11871
11872       /* This code shouldn't have been called if there was a natural barrier
11873          within range.  */
11874       gcc_assert (GET_CODE (from) != BARRIER);
11875
11876       /* Count the length of this insn.  */
11877       count += get_attr_length (from);
11878
11879       /* If there is a jump table, add its length.  */
11880       tmp = is_jump_table (from);
11881       if (tmp != NULL)
11882         {
11883           count += get_jump_table_size (tmp);
11884
11885           /* Jump tables aren't in a basic block, so base the cost on
11886              the dispatch insn.  If we select this location, we will
11887              still put the pool after the table.  */
11888           new_cost = arm_barrier_cost (from);
11889
11890           if (count < max_count 
11891               && (!selected || new_cost <= selected_cost))
11892             {
11893               selected = tmp;
11894               selected_cost = new_cost;
11895               selected_address = fix->address + count;
11896             }
11897
11898           /* Continue after the dispatch table.  */
11899           from = NEXT_INSN (tmp);
11900           continue;
11901         }
11902
11903       new_cost = arm_barrier_cost (from);
11904
11905       if (count < max_count
11906           && (!selected || new_cost <= selected_cost))
11907         {
11908           selected = from;
11909           selected_cost = new_cost;
11910           selected_address = fix->address + count;
11911         }
11912
11913       from = NEXT_INSN (from);
11914     }
11915
11916   /* Make sure that we found a place to insert the jump.  */
11917   gcc_assert (selected);
11918
11919   /* Create a new JUMP_INSN that branches around a barrier.  */
11920   from = emit_jump_insn_after (gen_jump (label), selected);
11921   JUMP_LABEL (from) = label;
11922   barrier = emit_barrier_after (from);
11923   emit_label_after (label, barrier);
11924
11925   /* Create a minipool barrier entry for the new barrier.  */
11926   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
11927   new_fix->insn = barrier;
11928   new_fix->address = selected_address;
11929   new_fix->next = fix->next;
11930   fix->next = new_fix;
11931
11932   return new_fix;
11933 }
11934
11935 /* Record that there is a natural barrier in the insn stream at
11936    ADDRESS.  */
11937 static void
11938 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
11939 {
11940   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
11941
11942   fix->insn = insn;
11943   fix->address = address;
11944
11945   fix->next = NULL;
11946   if (minipool_fix_head != NULL)
11947     minipool_fix_tail->next = fix;
11948   else
11949     minipool_fix_head = fix;
11950
11951   minipool_fix_tail = fix;
11952 }
11953
11954 /* Record INSN, which will need fixing up to load a value from the
11955    minipool.  ADDRESS is the offset of the insn since the start of the
11956    function; LOC is a pointer to the part of the insn which requires
11957    fixing; VALUE is the constant that must be loaded, which is of type
11958    MODE.  */
11959 static void
11960 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
11961                    enum machine_mode mode, rtx value)
11962 {
11963   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
11964
11965   fix->insn = insn;
11966   fix->address = address;
11967   fix->loc = loc;
11968   fix->mode = mode;
11969   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
11970   fix->value = value;
11971   fix->forwards = get_attr_pool_range (insn);
11972   fix->backwards = get_attr_neg_pool_range (insn);
11973   fix->minipool = NULL;
11974
11975   /* If an insn doesn't have a range defined for it, then it isn't
11976      expecting to be reworked by this code.  Better to stop now than
11977      to generate duff assembly code.  */
11978   gcc_assert (fix->forwards || fix->backwards);
11979
11980   /* If an entry requires 8-byte alignment then assume all constant pools
11981      require 4 bytes of padding.  Trying to do this later on a per-pool
11982      basis is awkward because existing pool entries have to be modified.  */
11983   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size >= 8)
11984     minipool_pad = 4;
11985
11986   if (dump_file)
11987     {
11988       fprintf (dump_file,
11989                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
11990                GET_MODE_NAME (mode),
11991                INSN_UID (insn), (unsigned long) address,
11992                -1 * (long)fix->backwards, (long)fix->forwards);
11993       arm_print_value (dump_file, fix->value);
11994       fprintf (dump_file, "\n");
11995     }
11996
11997   /* Add it to the chain of fixes.  */
11998   fix->next = NULL;
11999
12000   if (minipool_fix_head != NULL)
12001     minipool_fix_tail->next = fix;
12002   else
12003     minipool_fix_head = fix;
12004
12005   minipool_fix_tail = fix;
12006 }
12007
12008 /* Return the cost of synthesizing a 64-bit constant VAL inline.
12009    Returns the number of insns needed, or 99 if we don't know how to
12010    do it.  */
12011 int
12012 arm_const_double_inline_cost (rtx val)
12013 {
12014   rtx lowpart, highpart;
12015   enum machine_mode mode;
12016
12017   mode = GET_MODE (val);
12018
12019   if (mode == VOIDmode)
12020     mode = DImode;
12021
12022   gcc_assert (GET_MODE_SIZE (mode) == 8);
12023
12024   lowpart = gen_lowpart (SImode, val);
12025   highpart = gen_highpart_mode (SImode, mode, val);
12026
12027   gcc_assert (GET_CODE (lowpart) == CONST_INT);
12028   gcc_assert (GET_CODE (highpart) == CONST_INT);
12029
12030   return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
12031                             NULL_RTX, NULL_RTX, 0, 0)
12032           + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
12033                               NULL_RTX, NULL_RTX, 0, 0));
12034 }
12035
12036 /* Return true if it is worthwhile to split a 64-bit constant into two
12037    32-bit operations.  This is the case if optimizing for size, or
12038    if we have load delay slots, or if one 32-bit part can be done with
12039    a single data operation.  */
12040 bool
12041 arm_const_double_by_parts (rtx val)
12042 {
12043   enum machine_mode mode = GET_MODE (val);
12044   rtx part;
12045
12046   if (optimize_size || arm_ld_sched)
12047     return true;
12048
12049   if (mode == VOIDmode)
12050     mode = DImode;
12051
12052   part = gen_highpart_mode (SImode, mode, val);
12053
12054   gcc_assert (GET_CODE (part) == CONST_INT);
12055
12056   if (const_ok_for_arm (INTVAL (part))
12057       || const_ok_for_arm (~INTVAL (part)))
12058     return true;
12059
12060   part = gen_lowpart (SImode, val);
12061
12062   gcc_assert (GET_CODE (part) == CONST_INT);
12063
12064   if (const_ok_for_arm (INTVAL (part))
12065       || const_ok_for_arm (~INTVAL (part)))
12066     return true;
12067
12068   return false;
12069 }
12070
12071 /* Return true if it is possible to inline both the high and low parts
12072    of a 64-bit constant into 32-bit data processing instructions.  */
12073 bool
12074 arm_const_double_by_immediates (rtx val)
12075 {
12076   enum machine_mode mode = GET_MODE (val);
12077   rtx part;
12078
12079   if (mode == VOIDmode)
12080     mode = DImode;
12081
12082   part = gen_highpart_mode (SImode, mode, val);
12083
12084   gcc_assert (GET_CODE (part) == CONST_INT);
12085
12086   if (!const_ok_for_arm (INTVAL (part)))
12087     return false;
12088
12089   part = gen_lowpart (SImode, val);
12090
12091   gcc_assert (GET_CODE (part) == CONST_INT);
12092
12093   if (!const_ok_for_arm (INTVAL (part)))
12094     return false;
12095
12096   return true;
12097 }
12098
12099 /* Scan INSN and note any of its operands that need fixing.
12100    If DO_PUSHES is false we do not actually push any of the fixups
12101    needed.  The function returns TRUE if any fixups were needed/pushed.
12102    This is used by arm_memory_load_p() which needs to know about loads
12103    of constants that will be converted into minipool loads.  */
12104 static bool
12105 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
12106 {
12107   bool result = false;
12108   int opno;
12109
12110   extract_insn (insn);
12111
12112   if (!constrain_operands (1))
12113     fatal_insn_not_found (insn);
12114
12115   if (recog_data.n_alternatives == 0)
12116     return false;
12117
12118   /* Fill in recog_op_alt with information about the constraints of
12119      this insn.  */
12120   preprocess_constraints ();
12121
12122   for (opno = 0; opno < recog_data.n_operands; opno++)
12123     {
12124       /* Things we need to fix can only occur in inputs.  */
12125       if (recog_data.operand_type[opno] != OP_IN)
12126         continue;
12127
12128       /* If this alternative is a memory reference, then any mention
12129          of constants in this alternative is really to fool reload
12130          into allowing us to accept one there.  We need to fix them up
12131          now so that we output the right code.  */
12132       if (recog_op_alt[opno][which_alternative].memory_ok)
12133         {
12134           rtx op = recog_data.operand[opno];
12135
12136           if (CONSTANT_P (op))
12137             {
12138               if (do_pushes)
12139                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
12140                                    recog_data.operand_mode[opno], op);
12141               result = true;
12142             }
12143           else if (GET_CODE (op) == MEM
12144                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
12145                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
12146             {
12147               if (do_pushes)
12148                 {
12149                   rtx cop = avoid_constant_pool_reference (op);
12150
12151                   /* Casting the address of something to a mode narrower
12152                      than a word can cause avoid_constant_pool_reference()
12153                      to return the pool reference itself.  That's no good to
12154                      us here.  Lets just hope that we can use the
12155                      constant pool value directly.  */
12156                   if (op == cop)
12157                     cop = get_pool_constant (XEXP (op, 0));
12158
12159                   push_minipool_fix (insn, address,
12160                                      recog_data.operand_loc[opno],
12161                                      recog_data.operand_mode[opno], cop);
12162                 }
12163
12164               result = true;
12165             }
12166         }
12167     }
12168
12169   return result;
12170 }
12171
12172 /* Convert instructions to their cc-clobbering variant if possible, since
12173    that allows us to use smaller encodings.  */
12174
12175 static void
12176 thumb2_reorg (void)
12177 {
12178   basic_block bb;
12179   regset_head live;
12180
12181   INIT_REG_SET (&live);
12182
12183   /* We are freeing block_for_insn in the toplev to keep compatibility
12184      with old MDEP_REORGS that are not CFG based.  Recompute it now.  */
12185   compute_bb_for_insn ();
12186   df_analyze ();
12187
12188   FOR_EACH_BB (bb)
12189     {
12190       rtx insn;
12191
12192       COPY_REG_SET (&live, DF_LR_OUT (bb));
12193       df_simulate_initialize_backwards (bb, &live);
12194       FOR_BB_INSNS_REVERSE (bb, insn)
12195         {
12196           if (NONJUMP_INSN_P (insn)
12197               && !REGNO_REG_SET_P (&live, CC_REGNUM))
12198             {
12199               rtx pat = PATTERN (insn);
12200               if (GET_CODE (pat) == SET
12201                   && low_register_operand (XEXP (pat, 0), SImode)
12202                   && thumb_16bit_operator (XEXP (pat, 1), SImode)
12203                   && low_register_operand (XEXP (XEXP (pat, 1), 0), SImode)
12204                   && low_register_operand (XEXP (XEXP (pat, 1), 1), SImode))
12205                 {
12206                   rtx dst = XEXP (pat, 0);
12207                   rtx src = XEXP (pat, 1);
12208                   rtx op0 = XEXP (src, 0);
12209                   rtx op1 = (GET_RTX_CLASS (GET_CODE (src)) == RTX_COMM_ARITH
12210                              ? XEXP (src, 1) : NULL);
12211
12212                   if (rtx_equal_p (dst, op0)
12213                       || GET_CODE (src) == PLUS || GET_CODE (src) == MINUS)
12214                     {
12215                       rtx ccreg = gen_rtx_REG (CCmode, CC_REGNUM);
12216                       rtx clobber = gen_rtx_CLOBBER (VOIDmode, ccreg);
12217                       rtvec vec = gen_rtvec (2, pat, clobber);
12218
12219                       PATTERN (insn) = gen_rtx_PARALLEL (VOIDmode, vec);
12220                       INSN_CODE (insn) = -1;
12221                     }
12222                   /* We can also handle a commutative operation where the
12223                      second operand matches the destination.  */
12224                   else if (op1 && rtx_equal_p (dst, op1))
12225                     {
12226                       rtx ccreg = gen_rtx_REG (CCmode, CC_REGNUM);
12227                       rtx clobber = gen_rtx_CLOBBER (VOIDmode, ccreg);
12228                       rtvec vec;
12229
12230                       src = copy_rtx (src);
12231                       XEXP (src, 0) = op1;
12232                       XEXP (src, 1) = op0;
12233                       pat = gen_rtx_SET (VOIDmode, dst, src);
12234                       vec = gen_rtvec (2, pat, clobber);
12235                       PATTERN (insn) = gen_rtx_PARALLEL (VOIDmode, vec);
12236                       INSN_CODE (insn) = -1;
12237                     }
12238                 }
12239             }
12240
12241           if (NONDEBUG_INSN_P (insn))
12242             df_simulate_one_insn_backwards (bb, insn, &live);
12243         }
12244     }
12245
12246   CLEAR_REG_SET (&live);
12247 }
12248
12249 /* Gcc puts the pool in the wrong place for ARM, since we can only
12250    load addresses a limited distance around the pc.  We do some
12251    special munging to move the constant pool values to the correct
12252    point in the code.  */
12253 static void
12254 arm_reorg (void)
12255 {
12256   rtx insn;
12257   HOST_WIDE_INT address = 0;
12258   Mfix * fix;
12259
12260   if (TARGET_THUMB2)
12261     thumb2_reorg ();
12262   
12263   minipool_fix_head = minipool_fix_tail = NULL;
12264
12265   /* The first insn must always be a note, or the code below won't
12266      scan it properly.  */
12267   insn = get_insns ();
12268   gcc_assert (GET_CODE (insn) == NOTE);
12269   minipool_pad = 0;
12270
12271   /* Scan all the insns and record the operands that will need fixing.  */
12272   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
12273     {
12274       if (TARGET_CIRRUS_FIX_INVALID_INSNS
12275           && (arm_cirrus_insn_p (insn)
12276               || GET_CODE (insn) == JUMP_INSN
12277               || arm_memory_load_p (insn)))
12278         cirrus_reorg (insn);
12279
12280       if (GET_CODE (insn) == BARRIER)
12281         push_minipool_barrier (insn, address);
12282       else if (INSN_P (insn))
12283         {
12284           rtx table;
12285
12286           note_invalid_constants (insn, address, true);
12287           address += get_attr_length (insn);
12288
12289           /* If the insn is a vector jump, add the size of the table
12290              and skip the table.  */
12291           if ((table = is_jump_table (insn)) != NULL)
12292             {
12293               address += get_jump_table_size (table);
12294               insn = table;
12295             }
12296         }
12297     }
12298
12299   fix = minipool_fix_head;
12300
12301   /* Now scan the fixups and perform the required changes.  */
12302   while (fix)
12303     {
12304       Mfix * ftmp;
12305       Mfix * fdel;
12306       Mfix *  last_added_fix;
12307       Mfix * last_barrier = NULL;
12308       Mfix * this_fix;
12309
12310       /* Skip any further barriers before the next fix.  */
12311       while (fix && GET_CODE (fix->insn) == BARRIER)
12312         fix = fix->next;
12313
12314       /* No more fixes.  */
12315       if (fix == NULL)
12316         break;
12317
12318       last_added_fix = NULL;
12319
12320       for (ftmp = fix; ftmp; ftmp = ftmp->next)
12321         {
12322           if (GET_CODE (ftmp->insn) == BARRIER)
12323             {
12324               if (ftmp->address >= minipool_vector_head->max_address)
12325                 break;
12326
12327               last_barrier = ftmp;
12328             }
12329           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
12330             break;
12331
12332           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
12333         }
12334
12335       /* If we found a barrier, drop back to that; any fixes that we
12336          could have reached but come after the barrier will now go in
12337          the next mini-pool.  */
12338       if (last_barrier != NULL)
12339         {
12340           /* Reduce the refcount for those fixes that won't go into this
12341              pool after all.  */
12342           for (fdel = last_barrier->next;
12343                fdel && fdel != ftmp;
12344                fdel = fdel->next)
12345             {
12346               fdel->minipool->refcount--;
12347               fdel->minipool = NULL;
12348             }
12349
12350           ftmp = last_barrier;
12351         }
12352       else
12353         {
12354           /* ftmp is first fix that we can't fit into this pool and
12355              there no natural barriers that we could use.  Insert a
12356              new barrier in the code somewhere between the previous
12357              fix and this one, and arrange to jump around it.  */
12358           HOST_WIDE_INT max_address;
12359
12360           /* The last item on the list of fixes must be a barrier, so
12361              we can never run off the end of the list of fixes without
12362              last_barrier being set.  */
12363           gcc_assert (ftmp);
12364
12365           max_address = minipool_vector_head->max_address;
12366           /* Check that there isn't another fix that is in range that
12367              we couldn't fit into this pool because the pool was
12368              already too large: we need to put the pool before such an
12369              instruction.  The pool itself may come just after the
12370              fix because create_fix_barrier also allows space for a
12371              jump instruction.  */
12372           if (ftmp->address < max_address)
12373             max_address = ftmp->address + 1;
12374
12375           last_barrier = create_fix_barrier (last_added_fix, max_address);
12376         }
12377
12378       assign_minipool_offsets (last_barrier);
12379
12380       while (ftmp)
12381         {
12382           if (GET_CODE (ftmp->insn) != BARRIER
12383               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
12384                   == NULL))
12385             break;
12386
12387           ftmp = ftmp->next;
12388         }
12389
12390       /* Scan over the fixes we have identified for this pool, fixing them
12391          up and adding the constants to the pool itself.  */
12392       for (this_fix = fix; this_fix && ftmp != this_fix;
12393            this_fix = this_fix->next)
12394         if (GET_CODE (this_fix->insn) != BARRIER)
12395           {
12396             rtx addr
12397               = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
12398                                                   minipool_vector_label),
12399                                this_fix->minipool->offset);
12400             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
12401           }
12402
12403       dump_minipool (last_barrier->insn);
12404       fix = ftmp;
12405     }
12406
12407   /* From now on we must synthesize any constants that we can't handle
12408      directly.  This can happen if the RTL gets split during final
12409      instruction generation.  */
12410   after_arm_reorg = 1;
12411
12412   /* Free the minipool memory.  */
12413   obstack_free (&minipool_obstack, minipool_startobj);
12414 }
12415 \f
12416 /* Routines to output assembly language.  */
12417
12418 /* If the rtx is the correct value then return the string of the number.
12419    In this way we can ensure that valid double constants are generated even
12420    when cross compiling.  */
12421 const char *
12422 fp_immediate_constant (rtx x)
12423 {
12424   REAL_VALUE_TYPE r;
12425   int i;
12426
12427   if (!fp_consts_inited)
12428     init_fp_table ();
12429
12430   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
12431   for (i = 0; i < 8; i++)
12432     if (REAL_VALUES_EQUAL (r, values_fp[i]))
12433       return strings_fp[i];
12434
12435   gcc_unreachable ();
12436 }
12437
12438 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
12439 static const char *
12440 fp_const_from_val (REAL_VALUE_TYPE *r)
12441 {
12442   int i;
12443
12444   if (!fp_consts_inited)
12445     init_fp_table ();
12446
12447   for (i = 0; i < 8; i++)
12448     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
12449       return strings_fp[i];
12450
12451   gcc_unreachable ();
12452 }
12453
12454 /* Output the operands of a LDM/STM instruction to STREAM.
12455    MASK is the ARM register set mask of which only bits 0-15 are important.
12456    REG is the base register, either the frame pointer or the stack pointer,
12457    INSTR is the possibly suffixed load or store instruction.
12458    RFE is nonzero if the instruction should also copy spsr to cpsr.  */
12459
12460 static void
12461 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
12462                  unsigned long mask, int rfe)
12463 {
12464   unsigned i;
12465   bool not_first = FALSE;
12466
12467   gcc_assert (!rfe || (mask & (1 << PC_REGNUM)));
12468   fputc ('\t', stream);
12469   asm_fprintf (stream, instr, reg);
12470   fputc ('{', stream);
12471
12472   for (i = 0; i <= LAST_ARM_REGNUM; i++)
12473     if (mask & (1 << i))
12474       {
12475         if (not_first)
12476           fprintf (stream, ", ");
12477
12478         asm_fprintf (stream, "%r", i);
12479         not_first = TRUE;
12480       }
12481
12482   if (rfe)
12483     fprintf (stream, "}^\n");
12484   else
12485     fprintf (stream, "}\n");
12486 }
12487
12488
12489 /* Output a FLDMD instruction to STREAM.
12490    BASE if the register containing the address.
12491    REG and COUNT specify the register range.
12492    Extra registers may be added to avoid hardware bugs.
12493
12494    We output FLDMD even for ARMv5 VFP implementations.  Although
12495    FLDMD is technically not supported until ARMv6, it is believed
12496    that all VFP implementations support its use in this context.  */
12497
12498 static void
12499 vfp_output_fldmd (FILE * stream, unsigned int base, int reg, int count)
12500 {
12501   int i;
12502
12503   /* Workaround ARM10 VFPr1 bug.  */
12504   if (count == 2 && !arm_arch6)
12505     {
12506       if (reg == 15)
12507         reg--;
12508       count++;
12509     }
12510
12511   /* FLDMD may not load more than 16 doubleword registers at a time. Split the
12512      load into multiple parts if we have to handle more than 16 registers.  */
12513   if (count > 16)
12514     {
12515       vfp_output_fldmd (stream, base, reg, 16);
12516       vfp_output_fldmd (stream, base, reg + 16, count - 16);
12517       return;
12518     }
12519
12520   fputc ('\t', stream);
12521   asm_fprintf (stream, "fldmfdd\t%r!, {", base);
12522
12523   for (i = reg; i < reg + count; i++)
12524     {
12525       if (i > reg)
12526         fputs (", ", stream);
12527       asm_fprintf (stream, "d%d", i);
12528     }
12529   fputs ("}\n", stream);
12530
12531 }
12532
12533
12534 /* Output the assembly for a store multiple.  */
12535
12536 const char *
12537 vfp_output_fstmd (rtx * operands)
12538 {
12539   char pattern[100];
12540   int p;
12541   int base;
12542   int i;
12543
12544   strcpy (pattern, "fstmfdd\t%m0!, {%P1");
12545   p = strlen (pattern);
12546
12547   gcc_assert (GET_CODE (operands[1]) == REG);
12548
12549   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
12550   for (i = 1; i < XVECLEN (operands[2], 0); i++)
12551     {
12552       p += sprintf (&pattern[p], ", d%d", base + i);
12553     }
12554   strcpy (&pattern[p], "}");
12555
12556   output_asm_insn (pattern, operands);
12557   return "";
12558 }
12559
12560
12561 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
12562    number of bytes pushed.  */
12563
12564 static int
12565 vfp_emit_fstmd (int base_reg, int count)
12566 {
12567   rtx par;
12568   rtx dwarf;
12569   rtx tmp, reg;
12570   int i;
12571
12572   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
12573      register pairs are stored by a store multiple insn.  We avoid this
12574      by pushing an extra pair.  */
12575   if (count == 2 && !arm_arch6)
12576     {
12577       if (base_reg == LAST_VFP_REGNUM - 3)
12578         base_reg -= 2;
12579       count++;
12580     }
12581
12582   /* FSTMD may not store more than 16 doubleword registers at once.  Split
12583      larger stores into multiple parts (up to a maximum of two, in
12584      practice).  */
12585   if (count > 16)
12586     {
12587       int saved;
12588       /* NOTE: base_reg is an internal register number, so each D register
12589          counts as 2.  */
12590       saved = vfp_emit_fstmd (base_reg + 32, count - 16);
12591       saved += vfp_emit_fstmd (base_reg, 16);
12592       return saved;
12593     }
12594
12595   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
12596   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
12597
12598   reg = gen_rtx_REG (DFmode, base_reg);
12599   base_reg += 2;
12600
12601   XVECEXP (par, 0, 0)
12602     = gen_rtx_SET (VOIDmode,
12603                    gen_frame_mem
12604                    (BLKmode,
12605                     gen_rtx_PRE_MODIFY (Pmode,
12606                                         stack_pointer_rtx,
12607                                         plus_constant
12608                                         (stack_pointer_rtx,
12609                                          - (count * 8)))
12610                     ),
12611                    gen_rtx_UNSPEC (BLKmode,
12612                                    gen_rtvec (1, reg),
12613                                    UNSPEC_PUSH_MULT));
12614
12615   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
12616                      plus_constant (stack_pointer_rtx, -(count * 8)));
12617   RTX_FRAME_RELATED_P (tmp) = 1;
12618   XVECEXP (dwarf, 0, 0) = tmp;
12619
12620   tmp = gen_rtx_SET (VOIDmode,
12621                      gen_frame_mem (DFmode, stack_pointer_rtx),
12622                      reg);
12623   RTX_FRAME_RELATED_P (tmp) = 1;
12624   XVECEXP (dwarf, 0, 1) = tmp;
12625
12626   for (i = 1; i < count; i++)
12627     {
12628       reg = gen_rtx_REG (DFmode, base_reg);
12629       base_reg += 2;
12630       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
12631
12632       tmp = gen_rtx_SET (VOIDmode,
12633                          gen_frame_mem (DFmode,
12634                                         plus_constant (stack_pointer_rtx,
12635                                                        i * 8)),
12636                          reg);
12637       RTX_FRAME_RELATED_P (tmp) = 1;
12638       XVECEXP (dwarf, 0, i + 1) = tmp;
12639     }
12640
12641   par = emit_insn (par);
12642   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
12643   RTX_FRAME_RELATED_P (par) = 1;
12644
12645   return count * 8;
12646 }
12647
12648 /* Emit a call instruction with pattern PAT.  ADDR is the address of
12649    the call target.  */
12650
12651 void
12652 arm_emit_call_insn (rtx pat, rtx addr)
12653 {
12654   rtx insn;
12655
12656   insn = emit_call_insn (pat);
12657
12658   /* The PIC register is live on entry to VxWorks PIC PLT entries.
12659      If the call might use such an entry, add a use of the PIC register
12660      to the instruction's CALL_INSN_FUNCTION_USAGE.  */
12661   if (TARGET_VXWORKS_RTP
12662       && flag_pic
12663       && GET_CODE (addr) == SYMBOL_REF
12664       && (SYMBOL_REF_DECL (addr)
12665           ? !targetm.binds_local_p (SYMBOL_REF_DECL (addr))
12666           : !SYMBOL_REF_LOCAL_P (addr)))
12667     {
12668       require_pic_register ();
12669       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), cfun->machine->pic_reg);
12670     }
12671 }
12672
12673 /* Output a 'call' insn.  */
12674 const char *
12675 output_call (rtx *operands)
12676 {
12677   gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly.  */
12678
12679   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
12680   if (REGNO (operands[0]) == LR_REGNUM)
12681     {
12682       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
12683       output_asm_insn ("mov%?\t%0, %|lr", operands);
12684     }
12685
12686   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
12687
12688   if (TARGET_INTERWORK || arm_arch4t)
12689     output_asm_insn ("bx%?\t%0", operands);
12690   else
12691     output_asm_insn ("mov%?\t%|pc, %0", operands);
12692
12693   return "";
12694 }
12695
12696 /* Output a 'call' insn that is a reference in memory. This is
12697    disabled for ARMv5 and we prefer a blx instead because otherwise
12698    there's a significant performance overhead.  */
12699 const char *
12700 output_call_mem (rtx *operands)
12701 {
12702   gcc_assert (!arm_arch5);
12703   if (TARGET_INTERWORK)
12704     {
12705       output_asm_insn ("ldr%?\t%|ip, %0", operands);
12706       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
12707       output_asm_insn ("bx%?\t%|ip", operands);
12708     }
12709   else if (regno_use_in (LR_REGNUM, operands[0]))
12710     {
12711       /* LR is used in the memory address.  We load the address in the
12712          first instruction.  It's safe to use IP as the target of the
12713          load since the call will kill it anyway.  */
12714       output_asm_insn ("ldr%?\t%|ip, %0", operands);
12715       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
12716       if (arm_arch4t)
12717         output_asm_insn ("bx%?\t%|ip", operands);
12718       else
12719         output_asm_insn ("mov%?\t%|pc, %|ip", operands);
12720     }
12721   else
12722     {
12723       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
12724       output_asm_insn ("ldr%?\t%|pc, %0", operands);
12725     }
12726
12727   return "";
12728 }
12729
12730
12731 /* Output a move from arm registers to an fpa registers.
12732    OPERANDS[0] is an fpa register.
12733    OPERANDS[1] is the first registers of an arm register pair.  */
12734 const char *
12735 output_mov_long_double_fpa_from_arm (rtx *operands)
12736 {
12737   int arm_reg0 = REGNO (operands[1]);
12738   rtx ops[3];
12739
12740   gcc_assert (arm_reg0 != IP_REGNUM);
12741
12742   ops[0] = gen_rtx_REG (SImode, arm_reg0);
12743   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
12744   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
12745
12746   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
12747   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
12748
12749   return "";
12750 }
12751
12752 /* Output a move from an fpa register to arm registers.
12753    OPERANDS[0] is the first registers of an arm register pair.
12754    OPERANDS[1] is an fpa register.  */
12755 const char *
12756 output_mov_long_double_arm_from_fpa (rtx *operands)
12757 {
12758   int arm_reg0 = REGNO (operands[0]);
12759   rtx ops[3];
12760
12761   gcc_assert (arm_reg0 != IP_REGNUM);
12762
12763   ops[0] = gen_rtx_REG (SImode, arm_reg0);
12764   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
12765   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
12766
12767   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
12768   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
12769   return "";
12770 }
12771
12772 /* Output a move from arm registers to arm registers of a long double
12773    OPERANDS[0] is the destination.
12774    OPERANDS[1] is the source.  */
12775 const char *
12776 output_mov_long_double_arm_from_arm (rtx *operands)
12777 {
12778   /* We have to be careful here because the two might overlap.  */
12779   int dest_start = REGNO (operands[0]);
12780   int src_start = REGNO (operands[1]);
12781   rtx ops[2];
12782   int i;
12783
12784   if (dest_start < src_start)
12785     {
12786       for (i = 0; i < 3; i++)
12787         {
12788           ops[0] = gen_rtx_REG (SImode, dest_start + i);
12789           ops[1] = gen_rtx_REG (SImode, src_start + i);
12790           output_asm_insn ("mov%?\t%0, %1", ops);
12791         }
12792     }
12793   else
12794     {
12795       for (i = 2; i >= 0; i--)
12796         {
12797           ops[0] = gen_rtx_REG (SImode, dest_start + i);
12798           ops[1] = gen_rtx_REG (SImode, src_start + i);
12799           output_asm_insn ("mov%?\t%0, %1", ops);
12800         }
12801     }
12802
12803   return "";
12804 }
12805
12806 void
12807 arm_emit_movpair (rtx dest, rtx src)
12808  {
12809   /* If the src is an immediate, simplify it.  */
12810   if (CONST_INT_P (src))
12811     {
12812       HOST_WIDE_INT val = INTVAL (src);
12813       emit_set_insn (dest, GEN_INT (val & 0x0000ffff));
12814       if ((val >> 16) & 0x0000ffff)
12815         emit_set_insn (gen_rtx_ZERO_EXTRACT (SImode, dest, GEN_INT (16),
12816                                              GEN_INT (16)),
12817                        GEN_INT ((val >> 16) & 0x0000ffff));
12818       return;
12819     }
12820    emit_set_insn (dest, gen_rtx_HIGH (SImode, src));
12821    emit_set_insn (dest, gen_rtx_LO_SUM (SImode, dest, src));
12822  }
12823
12824 /* Output a move from arm registers to an fpa registers.
12825    OPERANDS[0] is an fpa register.
12826    OPERANDS[1] is the first registers of an arm register pair.  */
12827 const char *
12828 output_mov_double_fpa_from_arm (rtx *operands)
12829 {
12830   int arm_reg0 = REGNO (operands[1]);
12831   rtx ops[2];
12832
12833   gcc_assert (arm_reg0 != IP_REGNUM);
12834
12835   ops[0] = gen_rtx_REG (SImode, arm_reg0);
12836   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
12837   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1}", ops);
12838   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
12839   return "";
12840 }
12841
12842 /* Output a move from an fpa register to arm registers.
12843    OPERANDS[0] is the first registers of an arm register pair.
12844    OPERANDS[1] is an fpa register.  */
12845 const char *
12846 output_mov_double_arm_from_fpa (rtx *operands)
12847 {
12848   int arm_reg0 = REGNO (operands[0]);
12849   rtx ops[2];
12850
12851   gcc_assert (arm_reg0 != IP_REGNUM);
12852
12853   ops[0] = gen_rtx_REG (SImode, arm_reg0);
12854   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
12855   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
12856   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1}", ops);
12857   return "";
12858 }
12859
12860 /* Output a move between double words.
12861    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
12862    or MEM<-REG and all MEMs must be offsettable addresses.  */
12863 const char *
12864 output_move_double (rtx *operands)
12865 {
12866   enum rtx_code code0 = GET_CODE (operands[0]);
12867   enum rtx_code code1 = GET_CODE (operands[1]);
12868   rtx otherops[3];
12869
12870   if (code0 == REG)
12871     {
12872       unsigned int reg0 = REGNO (operands[0]);
12873
12874       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
12875
12876       gcc_assert (code1 == MEM);  /* Constraints should ensure this.  */
12877
12878       switch (GET_CODE (XEXP (operands[1], 0)))
12879         {
12880         case REG:
12881           if (TARGET_LDRD
12882               && !(fix_cm3_ldrd && reg0 == REGNO(XEXP (operands[1], 0))))
12883             output_asm_insn ("ldr%(d%)\t%0, [%m1]", operands);
12884           else
12885             output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
12886           break;
12887
12888         case PRE_INC:
12889           gcc_assert (TARGET_LDRD);
12890           output_asm_insn ("ldr%(d%)\t%0, [%m1, #8]!", operands);
12891           break;
12892
12893         case PRE_DEC:
12894           if (TARGET_LDRD)
12895             output_asm_insn ("ldr%(d%)\t%0, [%m1, #-8]!", operands);
12896           else
12897             output_asm_insn ("ldm%(db%)\t%m1!, %M0", operands);
12898           break;
12899
12900         case POST_INC:
12901           if (TARGET_LDRD)
12902             output_asm_insn ("ldr%(d%)\t%0, [%m1], #8", operands);
12903           else
12904             output_asm_insn ("ldm%(ia%)\t%m1!, %M0", operands);
12905           break;
12906
12907         case POST_DEC:
12908           gcc_assert (TARGET_LDRD);
12909           output_asm_insn ("ldr%(d%)\t%0, [%m1], #-8", operands);
12910           break;
12911
12912         case PRE_MODIFY:
12913         case POST_MODIFY:
12914           /* Autoicrement addressing modes should never have overlapping
12915              base and destination registers, and overlapping index registers
12916              are already prohibited, so this doesn't need to worry about
12917              fix_cm3_ldrd.  */
12918           otherops[0] = operands[0];
12919           otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
12920           otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
12921
12922           if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
12923             {
12924               if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
12925                 {
12926                   /* Registers overlap so split out the increment.  */
12927                   output_asm_insn ("add%?\t%1, %1, %2", otherops);
12928                   output_asm_insn ("ldr%(d%)\t%0, [%1] @split", otherops);
12929                 }
12930               else
12931                 {
12932                   /* Use a single insn if we can.
12933                      FIXME: IWMMXT allows offsets larger than ldrd can
12934                      handle, fix these up with a pair of ldr.  */
12935                   if (TARGET_THUMB2
12936                       || GET_CODE (otherops[2]) != CONST_INT
12937                       || (INTVAL (otherops[2]) > -256
12938                           && INTVAL (otherops[2]) < 256))
12939                     output_asm_insn ("ldr%(d%)\t%0, [%1, %2]!", otherops);
12940                   else
12941                     {
12942                       output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
12943                       output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
12944                     }
12945                 }
12946             }
12947           else
12948             {
12949               /* Use a single insn if we can.
12950                  FIXME: IWMMXT allows offsets larger than ldrd can handle,
12951                  fix these up with a pair of ldr.  */
12952               if (TARGET_THUMB2
12953                   || GET_CODE (otherops[2]) != CONST_INT
12954                   || (INTVAL (otherops[2]) > -256
12955                       && INTVAL (otherops[2]) < 256))
12956                 output_asm_insn ("ldr%(d%)\t%0, [%1], %2", otherops);
12957               else
12958                 {
12959                   output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
12960                   output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
12961                 }
12962             }
12963           break;
12964
12965         case LABEL_REF:
12966         case CONST:
12967           /* We might be able to use ldrd %0, %1 here.  However the range is
12968              different to ldr/adr, and it is broken on some ARMv7-M
12969              implementations.  */
12970           /* Use the second register of the pair to avoid problematic
12971              overlap.  */
12972           otherops[1] = operands[1];
12973           output_asm_insn ("adr%?\t%0, %1", otherops);
12974           operands[1] = otherops[0];
12975           if (TARGET_LDRD)
12976             output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
12977           else
12978             output_asm_insn ("ldm%(ia%)\t%1, %M0", operands);
12979           break;
12980
12981           /* ??? This needs checking for thumb2.  */
12982         default:
12983           if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
12984                                GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
12985             {
12986               otherops[0] = operands[0];
12987               otherops[1] = XEXP (XEXP (operands[1], 0), 0);
12988               otherops[2] = XEXP (XEXP (operands[1], 0), 1);
12989
12990               if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
12991                 {
12992                   if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
12993                     {
12994                       switch ((int) INTVAL (otherops[2]))
12995                         {
12996                         case -8:
12997                           output_asm_insn ("ldm%(db%)\t%1, %M0", otherops);
12998                           return "";
12999                         case -4:
13000                           if (TARGET_THUMB2)
13001                             break;
13002                           output_asm_insn ("ldm%(da%)\t%1, %M0", otherops);
13003                           return "";
13004                         case 4:
13005                           if (TARGET_THUMB2)
13006                             break;
13007                           output_asm_insn ("ldm%(ib%)\t%1, %M0", otherops);
13008                           return "";
13009                         }
13010                     }
13011                   otherops[0] = gen_rtx_REG(SImode, REGNO(operands[0]) + 1);
13012                   operands[1] = otherops[0];
13013                   if (TARGET_LDRD
13014                       && (GET_CODE (otherops[2]) == REG
13015                           || TARGET_THUMB2
13016                           || (GET_CODE (otherops[2]) == CONST_INT
13017                               && INTVAL (otherops[2]) > -256
13018                               && INTVAL (otherops[2]) < 256)))
13019                     {
13020                       if (reg_overlap_mentioned_p (operands[0],
13021                                                    otherops[2]))
13022                         {
13023                           rtx tmp;
13024                           /* Swap base and index registers over to
13025                              avoid a conflict.  */
13026                           tmp = otherops[1];
13027                           otherops[1] = otherops[2];
13028                           otherops[2] = tmp;
13029                         }
13030                       /* If both registers conflict, it will usually
13031                          have been fixed by a splitter.  */
13032                       if (reg_overlap_mentioned_p (operands[0], otherops[2])
13033                           || (fix_cm3_ldrd && reg0 == REGNO (otherops[1])))
13034                         {
13035                           output_asm_insn ("add%?\t%0, %1, %2", otherops);
13036                           output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
13037                         }
13038                       else
13039                         {
13040                           otherops[0] = operands[0];
13041                           output_asm_insn ("ldr%(d%)\t%0, [%1, %2]", otherops);
13042                         }
13043                       return "";
13044                     }
13045
13046                   if (GET_CODE (otherops[2]) == CONST_INT)
13047                     {
13048                       if (!(const_ok_for_arm (INTVAL (otherops[2]))))
13049                         output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
13050                       else
13051                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
13052                     }
13053                   else
13054                     output_asm_insn ("add%?\t%0, %1, %2", otherops);
13055                 }
13056               else
13057                 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
13058
13059               if (TARGET_LDRD)
13060                 return "ldr%(d%)\t%0, [%1]";
13061
13062               return "ldm%(ia%)\t%1, %M0";
13063             }
13064           else
13065             {
13066               otherops[1] = adjust_address (operands[1], SImode, 4);
13067               /* Take care of overlapping base/data reg.  */
13068               if (reg_mentioned_p (operands[0], operands[1]))
13069                 {
13070                   output_asm_insn ("ldr%?\t%0, %1", otherops);
13071                   output_asm_insn ("ldr%?\t%0, %1", operands);
13072                 }
13073               else
13074                 {
13075                   output_asm_insn ("ldr%?\t%0, %1", operands);
13076                   output_asm_insn ("ldr%?\t%0, %1", otherops);
13077                 }
13078             }
13079         }
13080     }
13081   else
13082     {
13083       /* Constraints should ensure this.  */
13084       gcc_assert (code0 == MEM && code1 == REG);
13085       gcc_assert (REGNO (operands[1]) != IP_REGNUM);
13086
13087       switch (GET_CODE (XEXP (operands[0], 0)))
13088         {
13089         case REG:
13090           if (TARGET_LDRD)
13091             output_asm_insn ("str%(d%)\t%1, [%m0]", operands);
13092           else
13093             output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
13094           break;
13095
13096         case PRE_INC:
13097           gcc_assert (TARGET_LDRD);
13098           output_asm_insn ("str%(d%)\t%1, [%m0, #8]!", operands);
13099           break;
13100
13101         case PRE_DEC:
13102           if (TARGET_LDRD)
13103             output_asm_insn ("str%(d%)\t%1, [%m0, #-8]!", operands);
13104           else
13105             output_asm_insn ("stm%(db%)\t%m0!, %M1", operands);
13106           break;
13107
13108         case POST_INC:
13109           if (TARGET_LDRD)
13110             output_asm_insn ("str%(d%)\t%1, [%m0], #8", operands);
13111           else
13112             output_asm_insn ("stm%(ia%)\t%m0!, %M1", operands);
13113           break;
13114
13115         case POST_DEC:
13116           gcc_assert (TARGET_LDRD);
13117           output_asm_insn ("str%(d%)\t%1, [%m0], #-8", operands);
13118           break;
13119
13120         case PRE_MODIFY:
13121         case POST_MODIFY:
13122           otherops[0] = operands[1];
13123           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
13124           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
13125
13126           /* IWMMXT allows offsets larger than ldrd can handle,
13127              fix these up with a pair of ldr.  */
13128           if (!TARGET_THUMB2
13129               && GET_CODE (otherops[2]) == CONST_INT
13130               && (INTVAL(otherops[2]) <= -256
13131                   || INTVAL(otherops[2]) >= 256))
13132             {
13133               if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
13134                 {
13135                   output_asm_insn ("str%?\t%0, [%1, %2]!", otherops);
13136                   output_asm_insn ("str%?\t%H0, [%1, #4]", otherops);
13137                 }
13138               else
13139                 {
13140                   output_asm_insn ("str%?\t%H0, [%1, #4]", otherops);
13141                   output_asm_insn ("str%?\t%0, [%1], %2", otherops);
13142                 }
13143             }
13144           else if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
13145             output_asm_insn ("str%(d%)\t%0, [%1, %2]!", otherops);
13146           else
13147             output_asm_insn ("str%(d%)\t%0, [%1], %2", otherops);
13148           break;
13149
13150         case PLUS:
13151           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
13152           if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
13153             {
13154               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
13155                 {
13156                 case -8:
13157                   output_asm_insn ("stm%(db%)\t%m0, %M1", operands);
13158                   return "";
13159
13160                 case -4:
13161                   if (TARGET_THUMB2)
13162                     break;
13163                   output_asm_insn ("stm%(da%)\t%m0, %M1", operands);
13164                   return "";
13165
13166                 case 4:
13167                   if (TARGET_THUMB2)
13168                     break;
13169                   output_asm_insn ("stm%(ib%)\t%m0, %M1", operands);
13170                   return "";
13171                 }
13172             }
13173           if (TARGET_LDRD
13174               && (GET_CODE (otherops[2]) == REG
13175                   || TARGET_THUMB2
13176                   || (GET_CODE (otherops[2]) == CONST_INT
13177                       && INTVAL (otherops[2]) > -256
13178                       && INTVAL (otherops[2]) < 256)))
13179             {
13180               otherops[0] = operands[1];
13181               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
13182               output_asm_insn ("str%(d%)\t%0, [%1, %2]", otherops);
13183               return "";
13184             }
13185           /* Fall through */
13186
13187         default:
13188           otherops[0] = adjust_address (operands[0], SImode, 4);
13189           otherops[1] = operands[1];
13190           output_asm_insn ("str%?\t%1, %0", operands);
13191           output_asm_insn ("str%?\t%H1, %0", otherops);
13192         }
13193     }
13194
13195   return "";
13196 }
13197
13198 /* Output a move, load or store for quad-word vectors in ARM registers.  Only
13199    handles MEMs accepted by neon_vector_mem_operand with TYPE=1.  */
13200
13201 const char *
13202 output_move_quad (rtx *operands)
13203 {
13204   if (REG_P (operands[0]))
13205     {
13206       /* Load, or reg->reg move.  */
13207
13208       if (MEM_P (operands[1]))
13209         {
13210           switch (GET_CODE (XEXP (operands[1], 0)))
13211             {
13212             case REG:
13213               output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
13214               break;
13215
13216             case LABEL_REF:
13217             case CONST:
13218               output_asm_insn ("adr%?\t%0, %1", operands);
13219               output_asm_insn ("ldm%(ia%)\t%0, %M0", operands);
13220               break;
13221
13222             default:
13223               gcc_unreachable ();
13224             }
13225         }
13226       else
13227         {
13228           rtx ops[2];
13229           int dest, src, i;
13230
13231           gcc_assert (REG_P (operands[1]));
13232
13233           dest = REGNO (operands[0]);
13234           src = REGNO (operands[1]);
13235
13236           /* This seems pretty dumb, but hopefully GCC won't try to do it
13237              very often.  */
13238           if (dest < src)
13239             for (i = 0; i < 4; i++)
13240               {
13241                 ops[0] = gen_rtx_REG (SImode, dest + i);
13242                 ops[1] = gen_rtx_REG (SImode, src + i);
13243                 output_asm_insn ("mov%?\t%0, %1", ops);
13244               }
13245           else
13246             for (i = 3; i >= 0; i--)
13247               {
13248                 ops[0] = gen_rtx_REG (SImode, dest + i);
13249                 ops[1] = gen_rtx_REG (SImode, src + i);
13250                 output_asm_insn ("mov%?\t%0, %1", ops);
13251               }
13252         }
13253     }
13254   else
13255     {
13256       gcc_assert (MEM_P (operands[0]));
13257       gcc_assert (REG_P (operands[1]));
13258       gcc_assert (!reg_overlap_mentioned_p (operands[1], operands[0]));
13259
13260       switch (GET_CODE (XEXP (operands[0], 0)))
13261         {
13262         case REG:
13263           output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
13264           break;
13265
13266         default:
13267           gcc_unreachable ();
13268         }
13269     }
13270
13271   return "";
13272 }
13273
13274 /* Output a VFP load or store instruction.  */
13275
13276 const char *
13277 output_move_vfp (rtx *operands)
13278 {
13279   rtx reg, mem, addr, ops[2];
13280   int load = REG_P (operands[0]);
13281   int dp = GET_MODE_SIZE (GET_MODE (operands[0])) == 8;
13282   int integer_p = GET_MODE_CLASS (GET_MODE (operands[0])) == MODE_INT;
13283   const char *templ;
13284   char buff[50];
13285   enum machine_mode mode;
13286
13287   reg = operands[!load];
13288   mem = operands[load];
13289
13290   mode = GET_MODE (reg);
13291
13292   gcc_assert (REG_P (reg));
13293   gcc_assert (IS_VFP_REGNUM (REGNO (reg)));
13294   gcc_assert (mode == SFmode
13295               || mode == DFmode
13296               || mode == SImode
13297               || mode == DImode
13298               || (TARGET_NEON && VALID_NEON_DREG_MODE (mode)));
13299   gcc_assert (MEM_P (mem));
13300
13301   addr = XEXP (mem, 0);
13302
13303   switch (GET_CODE (addr))
13304     {
13305     case PRE_DEC:
13306       templ = "f%smdb%c%%?\t%%0!, {%%%s1}%s";
13307       ops[0] = XEXP (addr, 0);
13308       ops[1] = reg;
13309       break;
13310
13311     case POST_INC:
13312       templ = "f%smia%c%%?\t%%0!, {%%%s1}%s";
13313       ops[0] = XEXP (addr, 0);
13314       ops[1] = reg;
13315       break;
13316
13317     default:
13318       templ = "f%s%c%%?\t%%%s0, %%1%s";
13319       ops[0] = reg;
13320       ops[1] = mem;
13321       break;
13322     }
13323
13324   sprintf (buff, templ,
13325            load ? "ld" : "st",
13326            dp ? 'd' : 's',
13327            dp ? "P" : "",
13328            integer_p ? "\t%@ int" : "");
13329   output_asm_insn (buff, ops);
13330
13331   return "";
13332 }
13333
13334 /* Output a Neon quad-word load or store, or a load or store for
13335    larger structure modes.
13336
13337    WARNING: The ordering of elements is weird in big-endian mode,
13338    because we use VSTM, as required by the EABI.  GCC RTL defines
13339    element ordering based on in-memory order.  This can be differ
13340    from the architectural ordering of elements within a NEON register.
13341    The intrinsics defined in arm_neon.h use the NEON register element
13342    ordering, not the GCC RTL element ordering.
13343
13344    For example, the in-memory ordering of a big-endian a quadword
13345    vector with 16-bit elements when stored from register pair {d0,d1}
13346    will be (lowest address first, d0[N] is NEON register element N):
13347
13348      [d0[3], d0[2], d0[1], d0[0], d1[7], d1[6], d1[5], d1[4]]
13349
13350    When necessary, quadword registers (dN, dN+1) are moved to ARM
13351    registers from rN in the order:
13352
13353      dN -> (rN+1, rN), dN+1 -> (rN+3, rN+2)
13354
13355    So that STM/LDM can be used on vectors in ARM registers, and the
13356    same memory layout will result as if VSTM/VLDM were used.  */
13357
13358 const char *
13359 output_move_neon (rtx *operands)
13360 {
13361   rtx reg, mem, addr, ops[2];
13362   int regno, load = REG_P (operands[0]);
13363   const char *templ;
13364   char buff[50];
13365   enum machine_mode mode;
13366
13367   reg = operands[!load];
13368   mem = operands[load];
13369
13370   mode = GET_MODE (reg);
13371
13372   gcc_assert (REG_P (reg));
13373   regno = REGNO (reg);
13374   gcc_assert (VFP_REGNO_OK_FOR_DOUBLE (regno)
13375               || NEON_REGNO_OK_FOR_QUAD (regno));
13376   gcc_assert (VALID_NEON_DREG_MODE (mode)
13377               || VALID_NEON_QREG_MODE (mode)
13378               || VALID_NEON_STRUCT_MODE (mode));
13379   gcc_assert (MEM_P (mem));
13380
13381   addr = XEXP (mem, 0);
13382
13383   /* Strip off const from addresses like (const (plus (...))).  */
13384   if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
13385     addr = XEXP (addr, 0);
13386
13387   switch (GET_CODE (addr))
13388     {
13389     case POST_INC:
13390       templ = "v%smia%%?\t%%0!, %%h1";
13391       ops[0] = XEXP (addr, 0);
13392       ops[1] = reg;
13393       break;
13394
13395     case PRE_DEC:
13396       /* FIXME: We should be using vld1/vst1 here in BE mode?  */
13397       templ = "v%smdb%%?\t%%0!, %%h1";
13398       ops[0] = XEXP (addr, 0);
13399       ops[1] = reg;
13400       break;
13401     
13402     case POST_MODIFY:
13403       /* FIXME: Not currently enabled in neon_vector_mem_operand.  */
13404       gcc_unreachable ();
13405
13406     case LABEL_REF:
13407     case PLUS:
13408       {
13409         int nregs = HARD_REGNO_NREGS (REGNO (reg), mode) / 2;
13410         int i;
13411         int overlap = -1;
13412         for (i = 0; i < nregs; i++)
13413           {
13414             /* We're only using DImode here because it's a convenient size.  */
13415             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * i);
13416             ops[1] = adjust_address (mem, DImode, 8 * i);
13417             if (reg_overlap_mentioned_p (ops[0], mem))
13418               {
13419                 gcc_assert (overlap == -1);
13420                 overlap = i;
13421               }
13422             else
13423               {
13424                 sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
13425                 output_asm_insn (buff, ops);
13426               }
13427           }
13428         if (overlap != -1)
13429           {
13430             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * overlap);
13431             ops[1] = adjust_address (mem, SImode, 8 * overlap);
13432             sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
13433             output_asm_insn (buff, ops);
13434           }
13435
13436         return "";
13437       }
13438
13439     default:
13440       templ = "v%smia%%?\t%%m0, %%h1";
13441       ops[0] = mem;
13442       ops[1] = reg;
13443     }
13444
13445   sprintf (buff, templ, load ? "ld" : "st");
13446   output_asm_insn (buff, ops);
13447
13448   return "";
13449 }
13450
13451 /* Compute and return the length of neon_mov<mode>, where <mode> is
13452    one of VSTRUCT modes: EI, OI, CI or XI.  */
13453 int
13454 arm_attr_length_move_neon (rtx insn)
13455 {
13456   rtx reg, mem, addr;
13457   int load;
13458   enum machine_mode mode;
13459
13460   extract_insn_cached (insn);
13461
13462   if (REG_P (recog_data.operand[0]) && REG_P (recog_data.operand[1]))
13463     {
13464       mode = GET_MODE (recog_data.operand[0]);
13465       switch (mode)
13466         {
13467         case EImode:
13468         case OImode:
13469           return 8;
13470         case CImode:
13471           return 12;
13472         case XImode:
13473           return 16;
13474         default:
13475           gcc_unreachable ();
13476         }
13477     }
13478
13479   load = REG_P (recog_data.operand[0]);
13480   reg = recog_data.operand[!load];
13481   mem = recog_data.operand[load];
13482
13483   gcc_assert (MEM_P (mem));
13484
13485   mode = GET_MODE (reg);
13486   addr = XEXP (mem, 0);
13487
13488   /* Strip off const from addresses like (const (plus (...))).  */
13489   if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
13490     addr = XEXP (addr, 0);
13491
13492   if (GET_CODE (addr) == LABEL_REF || GET_CODE (addr) == PLUS)
13493     {
13494       int insns = HARD_REGNO_NREGS (REGNO (reg), mode) / 2;
13495       return insns * 4;
13496     }
13497   else
13498     return 4;
13499 }
13500
13501 /* Return nonzero if the offset in the address is an immediate.  Otherwise,
13502    return zero.  */
13503
13504 int
13505 arm_address_offset_is_imm (rtx insn)
13506 {
13507   rtx mem, addr;
13508
13509   extract_insn_cached (insn);
13510
13511   if (REG_P (recog_data.operand[0]))
13512     return 0;
13513
13514   mem = recog_data.operand[0];
13515
13516   gcc_assert (MEM_P (mem));
13517
13518   addr = XEXP (mem, 0);
13519
13520   if (GET_CODE (addr) == REG
13521       || (GET_CODE (addr) == PLUS
13522           && GET_CODE (XEXP (addr, 0)) == REG
13523           && GET_CODE (XEXP (addr, 1)) == CONST_INT))
13524     return 1;
13525   else
13526     return 0;
13527 }
13528
13529 /* Output an ADD r, s, #n where n may be too big for one instruction.
13530    If adding zero to one register, output nothing.  */
13531 const char *
13532 output_add_immediate (rtx *operands)
13533 {
13534   HOST_WIDE_INT n = INTVAL (operands[2]);
13535
13536   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
13537     {
13538       if (n < 0)
13539         output_multi_immediate (operands,
13540                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
13541                                 -n);
13542       else
13543         output_multi_immediate (operands,
13544                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
13545                                 n);
13546     }
13547
13548   return "";
13549 }
13550
13551 /* Output a multiple immediate operation.
13552    OPERANDS is the vector of operands referred to in the output patterns.
13553    INSTR1 is the output pattern to use for the first constant.
13554    INSTR2 is the output pattern to use for subsequent constants.
13555    IMMED_OP is the index of the constant slot in OPERANDS.
13556    N is the constant value.  */
13557 static const char *
13558 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
13559                         int immed_op, HOST_WIDE_INT n)
13560 {
13561 #if HOST_BITS_PER_WIDE_INT > 32
13562   n &= 0xffffffff;
13563 #endif
13564
13565   if (n == 0)
13566     {
13567       /* Quick and easy output.  */
13568       operands[immed_op] = const0_rtx;
13569       output_asm_insn (instr1, operands);
13570     }
13571   else
13572     {
13573       int i;
13574       const char * instr = instr1;
13575
13576       /* Note that n is never zero here (which would give no output).  */
13577       for (i = 0; i < 32; i += 2)
13578         {
13579           if (n & (3 << i))
13580             {
13581               operands[immed_op] = GEN_INT (n & (255 << i));
13582               output_asm_insn (instr, operands);
13583               instr = instr2;
13584               i += 6;
13585             }
13586         }
13587     }
13588
13589   return "";
13590 }
13591
13592 /* Return the name of a shifter operation.  */
13593 static const char *
13594 arm_shift_nmem(enum rtx_code code)
13595 {
13596   switch (code)
13597     {
13598     case ASHIFT:
13599       return ARM_LSL_NAME;
13600
13601     case ASHIFTRT:
13602       return "asr";
13603
13604     case LSHIFTRT:
13605       return "lsr";
13606
13607     case ROTATERT:
13608       return "ror";
13609
13610     default:
13611       abort();
13612     }
13613 }
13614
13615 /* Return the appropriate ARM instruction for the operation code.
13616    The returned result should not be overwritten.  OP is the rtx of the
13617    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
13618    was shifted.  */
13619 const char *
13620 arithmetic_instr (rtx op, int shift_first_arg)
13621 {
13622   switch (GET_CODE (op))
13623     {
13624     case PLUS:
13625       return "add";
13626
13627     case MINUS:
13628       return shift_first_arg ? "rsb" : "sub";
13629
13630     case IOR:
13631       return "orr";
13632
13633     case XOR:
13634       return "eor";
13635
13636     case AND:
13637       return "and";
13638
13639     case ASHIFT:
13640     case ASHIFTRT:
13641     case LSHIFTRT:
13642     case ROTATERT:
13643       return arm_shift_nmem(GET_CODE(op));
13644
13645     default:
13646       gcc_unreachable ();
13647     }
13648 }
13649
13650 /* Ensure valid constant shifts and return the appropriate shift mnemonic
13651    for the operation code.  The returned result should not be overwritten.
13652    OP is the rtx code of the shift.
13653    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
13654    shift.  */
13655 static const char *
13656 shift_op (rtx op, HOST_WIDE_INT *amountp)
13657 {
13658   const char * mnem;
13659   enum rtx_code code = GET_CODE (op);
13660
13661   switch (GET_CODE (XEXP (op, 1)))
13662     {
13663     case REG:
13664     case SUBREG:
13665       *amountp = -1;
13666       break;
13667
13668     case CONST_INT:
13669       *amountp = INTVAL (XEXP (op, 1));
13670       break;
13671
13672     default:
13673       gcc_unreachable ();
13674     }
13675
13676   switch (code)
13677     {
13678     case ROTATE:
13679       gcc_assert (*amountp != -1);
13680       *amountp = 32 - *amountp;
13681       code = ROTATERT;
13682
13683       /* Fall through.  */
13684
13685     case ASHIFT:
13686     case ASHIFTRT:
13687     case LSHIFTRT:
13688     case ROTATERT:
13689       mnem = arm_shift_nmem(code);
13690       break;
13691
13692     case MULT:
13693       /* We never have to worry about the amount being other than a
13694          power of 2, since this case can never be reloaded from a reg.  */
13695       gcc_assert (*amountp != -1);
13696       *amountp = int_log2 (*amountp);
13697       return ARM_LSL_NAME;
13698
13699     default:
13700       gcc_unreachable ();
13701     }
13702
13703   if (*amountp != -1)
13704     {
13705       /* This is not 100% correct, but follows from the desire to merge
13706          multiplication by a power of 2 with the recognizer for a
13707          shift.  >=32 is not a valid shift for "lsl", so we must try and
13708          output a shift that produces the correct arithmetical result.
13709          Using lsr #32 is identical except for the fact that the carry bit
13710          is not set correctly if we set the flags; but we never use the
13711          carry bit from such an operation, so we can ignore that.  */
13712       if (code == ROTATERT)
13713         /* Rotate is just modulo 32.  */
13714         *amountp &= 31;
13715       else if (*amountp != (*amountp & 31))
13716         {
13717           if (code == ASHIFT)
13718             mnem = "lsr";
13719           *amountp = 32;
13720         }
13721
13722       /* Shifts of 0 are no-ops.  */
13723       if (*amountp == 0)
13724         return NULL;
13725     }
13726
13727   return mnem;
13728 }
13729
13730 /* Obtain the shift from the POWER of two.  */
13731
13732 static HOST_WIDE_INT
13733 int_log2 (HOST_WIDE_INT power)
13734 {
13735   HOST_WIDE_INT shift = 0;
13736
13737   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
13738     {
13739       gcc_assert (shift <= 31);
13740       shift++;
13741     }
13742
13743   return shift;
13744 }
13745
13746 /* Output a .ascii pseudo-op, keeping track of lengths.  This is
13747    because /bin/as is horribly restrictive.  The judgement about
13748    whether or not each character is 'printable' (and can be output as
13749    is) or not (and must be printed with an octal escape) must be made
13750    with reference to the *host* character set -- the situation is
13751    similar to that discussed in the comments above pp_c_char in
13752    c-pretty-print.c.  */
13753
13754 #define MAX_ASCII_LEN 51
13755
13756 void
13757 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
13758 {
13759   int i;
13760   int len_so_far = 0;
13761
13762   fputs ("\t.ascii\t\"", stream);
13763
13764   for (i = 0; i < len; i++)
13765     {
13766       int c = p[i];
13767
13768       if (len_so_far >= MAX_ASCII_LEN)
13769         {
13770           fputs ("\"\n\t.ascii\t\"", stream);
13771           len_so_far = 0;
13772         }
13773
13774       if (ISPRINT (c))
13775         {
13776           if (c == '\\' || c == '\"')
13777             {
13778               putc ('\\', stream);
13779               len_so_far++;
13780             }
13781           putc (c, stream);
13782           len_so_far++;
13783         }
13784       else
13785         {
13786           fprintf (stream, "\\%03o", c);
13787           len_so_far += 4;
13788         }
13789     }
13790
13791   fputs ("\"\n", stream);
13792 }
13793 \f
13794 /* Compute the register save mask for registers 0 through 12
13795    inclusive.  This code is used by arm_compute_save_reg_mask.  */
13796
13797 static unsigned long
13798 arm_compute_save_reg0_reg12_mask (void)
13799 {
13800   unsigned long func_type = arm_current_func_type ();
13801   unsigned long save_reg_mask = 0;
13802   unsigned int reg;
13803
13804   if (IS_INTERRUPT (func_type))
13805     {
13806       unsigned int max_reg;
13807       /* Interrupt functions must not corrupt any registers,
13808          even call clobbered ones.  If this is a leaf function
13809          we can just examine the registers used by the RTL, but
13810          otherwise we have to assume that whatever function is
13811          called might clobber anything, and so we have to save
13812          all the call-clobbered registers as well.  */
13813       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
13814         /* FIQ handlers have registers r8 - r12 banked, so
13815            we only need to check r0 - r7, Normal ISRs only
13816            bank r14 and r15, so we must check up to r12.
13817            r13 is the stack pointer which is always preserved,
13818            so we do not need to consider it here.  */
13819         max_reg = 7;
13820       else
13821         max_reg = 12;
13822
13823       for (reg = 0; reg <= max_reg; reg++)
13824         if (df_regs_ever_live_p (reg)
13825             || (! current_function_is_leaf && call_used_regs[reg]))
13826           save_reg_mask |= (1 << reg);
13827
13828       /* Also save the pic base register if necessary.  */
13829       if (flag_pic
13830           && !TARGET_SINGLE_PIC_BASE
13831           && arm_pic_register != INVALID_REGNUM
13832           && crtl->uses_pic_offset_table)
13833         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
13834     }
13835   else if (IS_VOLATILE(func_type))
13836     {
13837       /* For noreturn functions we historically omitted register saves
13838          altogether.  However this really messes up debugging.  As a
13839          compromise save just the frame pointers.  Combined with the link
13840          register saved elsewhere this should be sufficient to get
13841          a backtrace.  */
13842       if (frame_pointer_needed)
13843         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
13844       if (df_regs_ever_live_p (ARM_HARD_FRAME_POINTER_REGNUM))
13845         save_reg_mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
13846       if (df_regs_ever_live_p (THUMB_HARD_FRAME_POINTER_REGNUM))
13847         save_reg_mask |= 1 << THUMB_HARD_FRAME_POINTER_REGNUM;
13848     }
13849   else
13850     {
13851       /* In the normal case we only need to save those registers
13852          which are call saved and which are used by this function.  */
13853       for (reg = 0; reg <= 11; reg++)
13854         if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
13855           save_reg_mask |= (1 << reg);
13856
13857       /* Handle the frame pointer as a special case.  */
13858       if (frame_pointer_needed)
13859         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
13860
13861       /* If we aren't loading the PIC register,
13862          don't stack it even though it may be live.  */
13863       if (flag_pic
13864           && !TARGET_SINGLE_PIC_BASE
13865           && arm_pic_register != INVALID_REGNUM
13866           && (df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM)
13867               || crtl->uses_pic_offset_table))
13868         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
13869
13870       /* The prologue will copy SP into R0, so save it.  */
13871       if (IS_STACKALIGN (func_type))
13872         save_reg_mask |= 1;
13873     }
13874
13875   /* Save registers so the exception handler can modify them.  */
13876   if (crtl->calls_eh_return)
13877     {
13878       unsigned int i;
13879
13880       for (i = 0; ; i++)
13881         {
13882           reg = EH_RETURN_DATA_REGNO (i);
13883           if (reg == INVALID_REGNUM)
13884             break;
13885           save_reg_mask |= 1 << reg;
13886         }
13887     }
13888
13889   return save_reg_mask;
13890 }
13891
13892
13893 /* Compute the number of bytes used to store the static chain register on the 
13894    stack, above the stack frame. We need to know this accurately to get the
13895    alignment of the rest of the stack frame correct. */
13896
13897 static int arm_compute_static_chain_stack_bytes (void)
13898 {
13899   unsigned long func_type = arm_current_func_type ();
13900   int static_chain_stack_bytes = 0;
13901
13902   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM &&
13903       IS_NESTED (func_type) &&
13904       df_regs_ever_live_p (3) && crtl->args.pretend_args_size == 0)
13905     static_chain_stack_bytes = 4;
13906
13907   return static_chain_stack_bytes;
13908 }
13909
13910
13911 /* Compute a bit mask of which registers need to be
13912    saved on the stack for the current function.
13913    This is used by arm_get_frame_offsets, which may add extra registers.  */
13914
13915 static unsigned long
13916 arm_compute_save_reg_mask (void)
13917 {
13918   unsigned int save_reg_mask = 0;
13919   unsigned long func_type = arm_current_func_type ();
13920   unsigned int reg;
13921
13922   if (IS_NAKED (func_type))
13923     /* This should never really happen.  */
13924     return 0;
13925
13926   /* If we are creating a stack frame, then we must save the frame pointer,
13927      IP (which will hold the old stack pointer), LR and the PC.  */
13928   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
13929     save_reg_mask |=
13930       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
13931       | (1 << IP_REGNUM)
13932       | (1 << LR_REGNUM)
13933       | (1 << PC_REGNUM);
13934
13935   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
13936
13937   /* Decide if we need to save the link register.
13938      Interrupt routines have their own banked link register,
13939      so they never need to save it.
13940      Otherwise if we do not use the link register we do not need to save
13941      it.  If we are pushing other registers onto the stack however, we
13942      can save an instruction in the epilogue by pushing the link register
13943      now and then popping it back into the PC.  This incurs extra memory
13944      accesses though, so we only do it when optimizing for size, and only
13945      if we know that we will not need a fancy return sequence.  */
13946   if (df_regs_ever_live_p (LR_REGNUM)
13947       || (save_reg_mask
13948           && optimize_size
13949           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
13950           && !crtl->calls_eh_return))
13951     save_reg_mask |= 1 << LR_REGNUM;
13952
13953   if (cfun->machine->lr_save_eliminated)
13954     save_reg_mask &= ~ (1 << LR_REGNUM);
13955
13956   if (TARGET_REALLY_IWMMXT
13957       && ((bit_count (save_reg_mask)
13958            + ARM_NUM_INTS (crtl->args.pretend_args_size +
13959                            arm_compute_static_chain_stack_bytes())
13960            ) % 2) != 0)
13961     {
13962       /* The total number of registers that are going to be pushed
13963          onto the stack is odd.  We need to ensure that the stack
13964          is 64-bit aligned before we start to save iWMMXt registers,
13965          and also before we start to create locals.  (A local variable
13966          might be a double or long long which we will load/store using
13967          an iWMMXt instruction).  Therefore we need to push another
13968          ARM register, so that the stack will be 64-bit aligned.  We
13969          try to avoid using the arg registers (r0 -r3) as they might be
13970          used to pass values in a tail call.  */
13971       for (reg = 4; reg <= 12; reg++)
13972         if ((save_reg_mask & (1 << reg)) == 0)
13973           break;
13974
13975       if (reg <= 12)
13976         save_reg_mask |= (1 << reg);
13977       else
13978         {
13979           cfun->machine->sibcall_blocked = 1;
13980           save_reg_mask |= (1 << 3);
13981         }
13982     }
13983
13984   /* We may need to push an additional register for use initializing the
13985      PIC base register.  */
13986   if (TARGET_THUMB2 && IS_NESTED (func_type) && flag_pic
13987       && (save_reg_mask & THUMB2_WORK_REGS) == 0)
13988     {
13989       reg = thumb_find_work_register (1 << 4);
13990       if (!call_used_regs[reg])
13991         save_reg_mask |= (1 << reg);
13992     }
13993
13994   return save_reg_mask;
13995 }
13996
13997
13998 /* Compute a bit mask of which registers need to be
13999    saved on the stack for the current function.  */
14000 static unsigned long
14001 thumb1_compute_save_reg_mask (void)
14002 {
14003   unsigned long mask;
14004   unsigned reg;
14005
14006   mask = 0;
14007   for (reg = 0; reg < 12; reg ++)
14008     if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14009       mask |= 1 << reg;
14010
14011   if (flag_pic
14012       && !TARGET_SINGLE_PIC_BASE
14013       && arm_pic_register != INVALID_REGNUM
14014       && crtl->uses_pic_offset_table)
14015     mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
14016
14017   /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
14018   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
14019     mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
14020
14021   /* LR will also be pushed if any lo regs are pushed.  */
14022   if (mask & 0xff || thumb_force_lr_save ())
14023     mask |= (1 << LR_REGNUM);
14024
14025   /* Make sure we have a low work register if we need one.
14026      We will need one if we are going to push a high register,
14027      but we are not currently intending to push a low register.  */
14028   if ((mask & 0xff) == 0
14029       && ((mask & 0x0f00) || TARGET_BACKTRACE))
14030     {
14031       /* Use thumb_find_work_register to choose which register
14032          we will use.  If the register is live then we will
14033          have to push it.  Use LAST_LO_REGNUM as our fallback
14034          choice for the register to select.  */
14035       reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
14036       /* Make sure the register returned by thumb_find_work_register is
14037          not part of the return value.  */
14038       if (reg * UNITS_PER_WORD <= (unsigned) arm_size_return_regs ())
14039         reg = LAST_LO_REGNUM;
14040
14041       if (! call_used_regs[reg])
14042         mask |= 1 << reg;
14043     }
14044
14045   /* The 504 below is 8 bytes less than 512 because there are two possible
14046      alignment words.  We can't tell here if they will be present or not so we
14047      have to play it safe and assume that they are. */
14048   if ((CALLER_INTERWORKING_SLOT_SIZE +
14049        ROUND_UP_WORD (get_frame_size ()) +
14050        crtl->outgoing_args_size) >= 504)
14051     {
14052       /* This is the same as the code in thumb1_expand_prologue() which
14053          determines which register to use for stack decrement. */
14054       for (reg = LAST_ARG_REGNUM + 1; reg <= LAST_LO_REGNUM; reg++)
14055         if (mask & (1 << reg))
14056           break;
14057
14058       if (reg > LAST_LO_REGNUM)
14059         {
14060           /* Make sure we have a register available for stack decrement. */
14061           mask |= 1 << LAST_LO_REGNUM;
14062         }
14063     }
14064
14065   return mask;
14066 }
14067
14068
14069 /* Return the number of bytes required to save VFP registers.  */
14070 static int
14071 arm_get_vfp_saved_size (void)
14072 {
14073   unsigned int regno;
14074   int count;
14075   int saved;
14076
14077   saved = 0;
14078   /* Space for saved VFP registers.  */
14079   if (TARGET_HARD_FLOAT && TARGET_VFP)
14080     {
14081       count = 0;
14082       for (regno = FIRST_VFP_REGNUM;
14083            regno < LAST_VFP_REGNUM;
14084            regno += 2)
14085         {
14086           if ((!df_regs_ever_live_p (regno) || call_used_regs[regno])
14087               && (!df_regs_ever_live_p (regno + 1) || call_used_regs[regno + 1]))
14088             {
14089               if (count > 0)
14090                 {
14091                   /* Workaround ARM10 VFPr1 bug.  */
14092                   if (count == 2 && !arm_arch6)
14093                     count++;
14094                   saved += count * 8;
14095                 }
14096               count = 0;
14097             }
14098           else
14099             count++;
14100         }
14101       if (count > 0)
14102         {
14103           if (count == 2 && !arm_arch6)
14104             count++;
14105           saved += count * 8;
14106         }
14107     }
14108   return saved;
14109 }
14110
14111
14112 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
14113    everything bar the final return instruction.  */
14114 const char *
14115 output_return_instruction (rtx operand, int really_return, int reverse)
14116 {
14117   char conditional[10];
14118   char instr[100];
14119   unsigned reg;
14120   unsigned long live_regs_mask;
14121   unsigned long func_type;
14122   arm_stack_offsets *offsets;
14123
14124   func_type = arm_current_func_type ();
14125
14126   if (IS_NAKED (func_type))
14127     return "";
14128
14129   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
14130     {
14131       /* If this function was declared non-returning, and we have
14132          found a tail call, then we have to trust that the called
14133          function won't return.  */
14134       if (really_return)
14135         {
14136           rtx ops[2];
14137
14138           /* Otherwise, trap an attempted return by aborting.  */
14139           ops[0] = operand;
14140           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
14141                                        : "abort");
14142           assemble_external_libcall (ops[1]);
14143           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
14144         }
14145
14146       return "";
14147     }
14148
14149   gcc_assert (!cfun->calls_alloca || really_return);
14150
14151   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
14152
14153   cfun->machine->return_used_this_function = 1;
14154
14155   offsets = arm_get_frame_offsets ();
14156   live_regs_mask = offsets->saved_regs_mask;
14157
14158   if (live_regs_mask)
14159     {
14160       const char * return_reg;
14161
14162       /* If we do not have any special requirements for function exit
14163          (e.g. interworking) then we can load the return address
14164          directly into the PC.  Otherwise we must load it into LR.  */
14165       if (really_return
14166           && (IS_INTERRUPT (func_type) || !TARGET_INTERWORK))
14167         return_reg = reg_names[PC_REGNUM];
14168       else
14169         return_reg = reg_names[LR_REGNUM];
14170
14171       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
14172         {
14173           /* There are three possible reasons for the IP register
14174              being saved.  1) a stack frame was created, in which case
14175              IP contains the old stack pointer, or 2) an ISR routine
14176              corrupted it, or 3) it was saved to align the stack on
14177              iWMMXt.  In case 1, restore IP into SP, otherwise just
14178              restore IP.  */
14179           if (frame_pointer_needed)
14180             {
14181               live_regs_mask &= ~ (1 << IP_REGNUM);
14182               live_regs_mask |=   (1 << SP_REGNUM);
14183             }
14184           else
14185             gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
14186         }
14187
14188       /* On some ARM architectures it is faster to use LDR rather than
14189          LDM to load a single register.  On other architectures, the
14190          cost is the same.  In 26 bit mode, or for exception handlers,
14191          we have to use LDM to load the PC so that the CPSR is also
14192          restored.  */
14193       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
14194         if (live_regs_mask == (1U << reg))
14195           break;
14196
14197       if (reg <= LAST_ARM_REGNUM
14198           && (reg != LR_REGNUM
14199               || ! really_return
14200               || ! IS_INTERRUPT (func_type)))
14201         {
14202           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
14203                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
14204         }
14205       else
14206         {
14207           char *p;
14208           int first = 1;
14209
14210           /* Generate the load multiple instruction to restore the
14211              registers.  Note we can get here, even if
14212              frame_pointer_needed is true, but only if sp already
14213              points to the base of the saved core registers.  */
14214           if (live_regs_mask & (1 << SP_REGNUM))
14215             {
14216               unsigned HOST_WIDE_INT stack_adjust;
14217
14218               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
14219               gcc_assert (stack_adjust == 0 || stack_adjust == 4);
14220
14221               if (stack_adjust && arm_arch5 && TARGET_ARM)
14222                 if (TARGET_UNIFIED_ASM)
14223                   sprintf (instr, "ldmib%s\t%%|sp, {", conditional);
14224                 else
14225                   sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
14226               else
14227                 {
14228                   /* If we can't use ldmib (SA110 bug),
14229                      then try to pop r3 instead.  */
14230                   if (stack_adjust)
14231                     live_regs_mask |= 1 << 3;
14232                   
14233                   if (TARGET_UNIFIED_ASM)
14234                     sprintf (instr, "ldmfd%s\t%%|sp, {", conditional);
14235                   else
14236                     sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
14237                 }
14238             }
14239           else
14240             if (TARGET_UNIFIED_ASM)
14241               sprintf (instr, "pop%s\t{", conditional);
14242             else
14243               sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
14244
14245           p = instr + strlen (instr);
14246
14247           for (reg = 0; reg <= SP_REGNUM; reg++)
14248             if (live_regs_mask & (1 << reg))
14249               {
14250                 int l = strlen (reg_names[reg]);
14251
14252                 if (first)
14253                   first = 0;
14254                 else
14255                   {
14256                     memcpy (p, ", ", 2);
14257                     p += 2;
14258                   }
14259
14260                 memcpy (p, "%|", 2);
14261                 memcpy (p + 2, reg_names[reg], l);
14262                 p += l + 2;
14263               }
14264
14265           if (live_regs_mask & (1 << LR_REGNUM))
14266             {
14267               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
14268               /* If returning from an interrupt, restore the CPSR.  */
14269               if (IS_INTERRUPT (func_type))
14270                 strcat (p, "^");
14271             }
14272           else
14273             strcpy (p, "}");
14274         }
14275
14276       output_asm_insn (instr, & operand);
14277
14278       /* See if we need to generate an extra instruction to
14279          perform the actual function return.  */
14280       if (really_return
14281           && func_type != ARM_FT_INTERWORKED
14282           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
14283         {
14284           /* The return has already been handled
14285              by loading the LR into the PC.  */
14286           really_return = 0;
14287         }
14288     }
14289
14290   if (really_return)
14291     {
14292       switch ((int) ARM_FUNC_TYPE (func_type))
14293         {
14294         case ARM_FT_ISR:
14295         case ARM_FT_FIQ:
14296           /* ??? This is wrong for unified assembly syntax.  */
14297           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
14298           break;
14299
14300         case ARM_FT_INTERWORKED:
14301           sprintf (instr, "bx%s\t%%|lr", conditional);
14302           break;
14303
14304         case ARM_FT_EXCEPTION:
14305           /* ??? This is wrong for unified assembly syntax.  */
14306           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
14307           break;
14308
14309         default:
14310           /* Use bx if it's available.  */
14311           if (arm_arch5 || arm_arch4t)
14312             sprintf (instr, "bx%s\t%%|lr", conditional);
14313           else
14314             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
14315           break;
14316         }
14317
14318       output_asm_insn (instr, & operand);
14319     }
14320
14321   return "";
14322 }
14323
14324 /* Write the function name into the code section, directly preceding
14325    the function prologue.
14326
14327    Code will be output similar to this:
14328      t0
14329          .ascii "arm_poke_function_name", 0
14330          .align
14331      t1
14332          .word 0xff000000 + (t1 - t0)
14333      arm_poke_function_name
14334          mov     ip, sp
14335          stmfd   sp!, {fp, ip, lr, pc}
14336          sub     fp, ip, #4
14337
14338    When performing a stack backtrace, code can inspect the value
14339    of 'pc' stored at 'fp' + 0.  If the trace function then looks
14340    at location pc - 12 and the top 8 bits are set, then we know
14341    that there is a function name embedded immediately preceding this
14342    location and has length ((pc[-3]) & 0xff000000).
14343
14344    We assume that pc is declared as a pointer to an unsigned long.
14345
14346    It is of no benefit to output the function name if we are assembling
14347    a leaf function.  These function types will not contain a stack
14348    backtrace structure, therefore it is not possible to determine the
14349    function name.  */
14350 void
14351 arm_poke_function_name (FILE *stream, const char *name)
14352 {
14353   unsigned long alignlength;
14354   unsigned long length;
14355   rtx           x;
14356
14357   length      = strlen (name) + 1;
14358   alignlength = ROUND_UP_WORD (length);
14359
14360   ASM_OUTPUT_ASCII (stream, name, length);
14361   ASM_OUTPUT_ALIGN (stream, 2);
14362   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
14363   assemble_aligned_integer (UNITS_PER_WORD, x);
14364 }
14365
14366 /* Place some comments into the assembler stream
14367    describing the current function.  */
14368 static void
14369 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
14370 {
14371   unsigned long func_type;
14372
14373   if (TARGET_THUMB1)
14374     {
14375       thumb1_output_function_prologue (f, frame_size);
14376       return;
14377     }
14378
14379   /* Sanity check.  */
14380   gcc_assert (!arm_ccfsm_state && !arm_target_insn);
14381
14382   func_type = arm_current_func_type ();
14383
14384   switch ((int) ARM_FUNC_TYPE (func_type))
14385     {
14386     default:
14387     case ARM_FT_NORMAL:
14388       break;
14389     case ARM_FT_INTERWORKED:
14390       asm_fprintf (f, "\t%@ Function supports interworking.\n");
14391       break;
14392     case ARM_FT_ISR:
14393       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
14394       break;
14395     case ARM_FT_FIQ:
14396       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
14397       break;
14398     case ARM_FT_EXCEPTION:
14399       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
14400       break;
14401     }
14402
14403   if (IS_NAKED (func_type))
14404     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
14405
14406   if (IS_VOLATILE (func_type))
14407     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
14408
14409   if (IS_NESTED (func_type))
14410     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
14411   if (IS_STACKALIGN (func_type))
14412     asm_fprintf (f, "\t%@ Stack Align: May be called with mis-aligned SP.\n");
14413
14414   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
14415                crtl->args.size,
14416                crtl->args.pretend_args_size, frame_size);
14417
14418   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
14419                frame_pointer_needed,
14420                cfun->machine->uses_anonymous_args);
14421
14422   if (cfun->machine->lr_save_eliminated)
14423     asm_fprintf (f, "\t%@ link register save eliminated.\n");
14424
14425   if (crtl->calls_eh_return)
14426     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
14427
14428 }
14429
14430 const char *
14431 arm_output_epilogue (rtx sibling)
14432 {
14433   int reg;
14434   unsigned long saved_regs_mask;
14435   unsigned long func_type;
14436   /* Floats_offset is the offset from the "virtual" frame.  In an APCS
14437      frame that is $fp + 4 for a non-variadic function.  */
14438   int floats_offset = 0;
14439   rtx operands[3];
14440   FILE * f = asm_out_file;
14441   unsigned int lrm_count = 0;
14442   int really_return = (sibling == NULL);
14443   int start_reg;
14444   arm_stack_offsets *offsets;
14445
14446   /* If we have already generated the return instruction
14447      then it is futile to generate anything else.  */
14448   if (use_return_insn (FALSE, sibling) && 
14449       (cfun->machine->return_used_this_function != 0))
14450     return "";
14451
14452   func_type = arm_current_func_type ();
14453
14454   if (IS_NAKED (func_type))
14455     /* Naked functions don't have epilogues.  */
14456     return "";
14457
14458   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
14459     {
14460       rtx op;
14461
14462       /* A volatile function should never return.  Call abort.  */
14463       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
14464       assemble_external_libcall (op);
14465       output_asm_insn ("bl\t%a0", &op);
14466
14467       return "";
14468     }
14469
14470   /* If we are throwing an exception, then we really must be doing a
14471      return, so we can't tail-call.  */
14472   gcc_assert (!crtl->calls_eh_return || really_return);
14473
14474   offsets = arm_get_frame_offsets ();
14475   saved_regs_mask = offsets->saved_regs_mask;
14476
14477   if (TARGET_IWMMXT)
14478     lrm_count = bit_count (saved_regs_mask);
14479
14480   floats_offset = offsets->saved_args;
14481   /* Compute how far away the floats will be.  */
14482   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
14483     if (saved_regs_mask & (1 << reg))
14484       floats_offset += 4;
14485
14486   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
14487     {
14488       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
14489       int vfp_offset = offsets->frame;
14490
14491       if (TARGET_FPA_EMU2)
14492         {
14493           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
14494             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14495               {
14496                 floats_offset += 12;
14497                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
14498                              reg, FP_REGNUM, floats_offset - vfp_offset);
14499               }
14500         }
14501       else
14502         {
14503           start_reg = LAST_FPA_REGNUM;
14504
14505           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
14506             {
14507               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14508                 {
14509                   floats_offset += 12;
14510
14511                   /* We can't unstack more than four registers at once.  */
14512                   if (start_reg - reg == 3)
14513                     {
14514                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
14515                                    reg, FP_REGNUM, floats_offset - vfp_offset);
14516                       start_reg = reg - 1;
14517                     }
14518                 }
14519               else
14520                 {
14521                   if (reg != start_reg)
14522                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
14523                                  reg + 1, start_reg - reg,
14524                                  FP_REGNUM, floats_offset - vfp_offset);
14525                   start_reg = reg - 1;
14526                 }
14527             }
14528
14529           /* Just in case the last register checked also needs unstacking.  */
14530           if (reg != start_reg)
14531             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
14532                          reg + 1, start_reg - reg,
14533                          FP_REGNUM, floats_offset - vfp_offset);
14534         }
14535
14536       if (TARGET_HARD_FLOAT && TARGET_VFP)
14537         {
14538           int saved_size;
14539
14540           /* The fldmd insns do not have base+offset addressing
14541              modes, so we use IP to hold the address.  */
14542           saved_size = arm_get_vfp_saved_size ();
14543
14544           if (saved_size > 0)
14545             {
14546               floats_offset += saved_size;
14547               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
14548                            FP_REGNUM, floats_offset - vfp_offset);
14549             }
14550           start_reg = FIRST_VFP_REGNUM;
14551           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
14552             {
14553               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
14554                   && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
14555                 {
14556                   if (start_reg != reg)
14557                     vfp_output_fldmd (f, IP_REGNUM,
14558                                       (start_reg - FIRST_VFP_REGNUM) / 2,
14559                                       (reg - start_reg) / 2);
14560                   start_reg = reg + 2;
14561                 }
14562             }
14563           if (start_reg != reg)
14564             vfp_output_fldmd (f, IP_REGNUM,
14565                               (start_reg - FIRST_VFP_REGNUM) / 2,
14566                               (reg - start_reg) / 2);
14567         }
14568
14569       if (TARGET_IWMMXT)
14570         {
14571           /* The frame pointer is guaranteed to be non-double-word aligned.
14572              This is because it is set to (old_stack_pointer - 4) and the
14573              old_stack_pointer was double word aligned.  Thus the offset to
14574              the iWMMXt registers to be loaded must also be non-double-word
14575              sized, so that the resultant address *is* double-word aligned.
14576              We can ignore floats_offset since that was already included in
14577              the live_regs_mask.  */
14578           lrm_count += (lrm_count % 2 ? 2 : 1);
14579
14580           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
14581             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14582               {
14583                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
14584                              reg, FP_REGNUM, lrm_count * 4);
14585                 lrm_count += 2;
14586               }
14587         }
14588
14589       /* saved_regs_mask should contain the IP, which at the time of stack
14590          frame generation actually contains the old stack pointer.  So a
14591          quick way to unwind the stack is just pop the IP register directly
14592          into the stack pointer.  */
14593       gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
14594       saved_regs_mask &= ~ (1 << IP_REGNUM);
14595       saved_regs_mask |=   (1 << SP_REGNUM);
14596
14597       /* There are two registers left in saved_regs_mask - LR and PC.  We
14598          only need to restore the LR register (the return address), but to
14599          save time we can load it directly into the PC, unless we need a
14600          special function exit sequence, or we are not really returning.  */
14601       if (really_return
14602           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
14603           && !crtl->calls_eh_return)
14604         /* Delete the LR from the register mask, so that the LR on
14605            the stack is loaded into the PC in the register mask.  */
14606         saved_regs_mask &= ~ (1 << LR_REGNUM);
14607       else
14608         saved_regs_mask &= ~ (1 << PC_REGNUM);
14609
14610       /* We must use SP as the base register, because SP is one of the
14611          registers being restored.  If an interrupt or page fault
14612          happens in the ldm instruction, the SP might or might not
14613          have been restored.  That would be bad, as then SP will no
14614          longer indicate the safe area of stack, and we can get stack
14615          corruption.  Using SP as the base register means that it will
14616          be reset correctly to the original value, should an interrupt
14617          occur.  If the stack pointer already points at the right
14618          place, then omit the subtraction.  */
14619       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
14620           || cfun->calls_alloca)
14621         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
14622                      4 * bit_count (saved_regs_mask));
14623       print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask, 0);
14624
14625       if (IS_INTERRUPT (func_type))
14626         /* Interrupt handlers will have pushed the
14627            IP onto the stack, so restore it now.  */
14628         print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, 1 << IP_REGNUM, 0);
14629     }
14630   else
14631     {
14632       /* This branch is executed for ARM mode (non-apcs frames) and
14633          Thumb-2 mode. Frame layout is essentially the same for those
14634          cases, except that in ARM mode frame pointer points to the
14635          first saved register, while in Thumb-2 mode the frame pointer points
14636          to the last saved register.
14637
14638          It is possible to make frame pointer point to last saved
14639          register in both cases, and remove some conditionals below.
14640          That means that fp setup in prologue would be just "mov fp, sp"
14641          and sp restore in epilogue would be just "mov sp, fp", whereas
14642          now we have to use add/sub in those cases. However, the value
14643          of that would be marginal, as both mov and add/sub are 32-bit
14644          in ARM mode, and it would require extra conditionals
14645          in arm_expand_prologue to distingish ARM-apcs-frame case
14646          (where frame pointer is required to point at first register)
14647          and ARM-non-apcs-frame. Therefore, such change is postponed
14648          until real need arise.  */
14649       unsigned HOST_WIDE_INT amount;
14650       int rfe;
14651       /* Restore stack pointer if necessary.  */
14652       if (TARGET_ARM && frame_pointer_needed)
14653         {
14654           operands[0] = stack_pointer_rtx;
14655           operands[1] = hard_frame_pointer_rtx;
14656           
14657           operands[2] = GEN_INT (offsets->frame - offsets->saved_regs);
14658           output_add_immediate (operands);
14659         }
14660       else
14661         {
14662           if (frame_pointer_needed)
14663             {
14664               /* For Thumb-2 restore sp from the frame pointer.
14665                  Operand restrictions mean we have to incrememnt FP, then copy
14666                  to SP.  */
14667               amount = offsets->locals_base - offsets->saved_regs;
14668               operands[0] = hard_frame_pointer_rtx;
14669             }
14670           else
14671             {
14672               unsigned long count;
14673               operands[0] = stack_pointer_rtx;
14674               amount = offsets->outgoing_args - offsets->saved_regs;
14675               /* pop call clobbered registers if it avoids a
14676                  separate stack adjustment.  */
14677               count = offsets->saved_regs - offsets->saved_args;
14678               if (optimize_size
14679                   && count != 0
14680                   && !crtl->calls_eh_return
14681                   && bit_count(saved_regs_mask) * 4 == count
14682                   && !IS_INTERRUPT (func_type)
14683                   && !crtl->tail_call_emit)
14684                 {
14685                   unsigned long mask;
14686                   /* Preserve return values, of any size.  */
14687                   mask = (1 << ((arm_size_return_regs() + 3) / 4)) - 1;
14688                   mask ^= 0xf;
14689                   mask &= ~saved_regs_mask;
14690                   reg = 0;
14691                   while (bit_count (mask) * 4 > amount)
14692                     {
14693                       while ((mask & (1 << reg)) == 0)
14694                         reg++;
14695                       mask &= ~(1 << reg);
14696                     }
14697                   if (bit_count (mask) * 4 == amount) {
14698                       amount = 0;
14699                       saved_regs_mask |= mask;
14700                   }
14701                 }
14702             }
14703           
14704           if (amount)
14705             {
14706               operands[1] = operands[0];
14707               operands[2] = GEN_INT (amount);
14708               output_add_immediate (operands);
14709             }
14710           if (frame_pointer_needed)
14711             asm_fprintf (f, "\tmov\t%r, %r\n",
14712                          SP_REGNUM, HARD_FRAME_POINTER_REGNUM);
14713         }
14714
14715       if (TARGET_FPA_EMU2)
14716         {
14717           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
14718             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14719               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
14720                            reg, SP_REGNUM);
14721         }
14722       else
14723         {
14724           start_reg = FIRST_FPA_REGNUM;
14725
14726           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
14727             {
14728               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14729                 {
14730                   if (reg - start_reg == 3)
14731                     {
14732                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
14733                                    start_reg, SP_REGNUM);
14734                       start_reg = reg + 1;
14735                     }
14736                 }
14737               else
14738                 {
14739                   if (reg != start_reg)
14740                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
14741                                  start_reg, reg - start_reg,
14742                                  SP_REGNUM);
14743
14744                   start_reg = reg + 1;
14745                 }
14746             }
14747
14748           /* Just in case the last register checked also needs unstacking.  */
14749           if (reg != start_reg)
14750             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
14751                          start_reg, reg - start_reg, SP_REGNUM);
14752         }
14753
14754       if (TARGET_HARD_FLOAT && TARGET_VFP)
14755         {
14756           int end_reg = LAST_VFP_REGNUM + 1;
14757
14758           /* Scan the registers in reverse order.  We need to match
14759              any groupings made in the prologue and generate matching
14760              pop operations.  */
14761           for (reg = LAST_VFP_REGNUM - 1; reg >= FIRST_VFP_REGNUM; reg -= 2)
14762             {
14763               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
14764                   && (!df_regs_ever_live_p (reg + 1)
14765                       || call_used_regs[reg + 1]))
14766                 {
14767                   if (end_reg > reg + 2)
14768                     vfp_output_fldmd (f, SP_REGNUM,
14769                                       (reg + 2 - FIRST_VFP_REGNUM) / 2,
14770                                       (end_reg - (reg + 2)) / 2);
14771                   end_reg = reg;
14772                 }
14773             }
14774           if (end_reg > reg + 2)
14775             vfp_output_fldmd (f, SP_REGNUM, 0,
14776                               (end_reg - (reg + 2)) / 2);
14777         }
14778
14779       if (TARGET_IWMMXT)
14780         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
14781           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14782             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
14783
14784       /* If we can, restore the LR into the PC.  */
14785       if (ARM_FUNC_TYPE (func_type) != ARM_FT_INTERWORKED
14786           && (TARGET_ARM || ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
14787           && !IS_STACKALIGN (func_type)
14788           && really_return
14789           && crtl->args.pretend_args_size == 0
14790           && saved_regs_mask & (1 << LR_REGNUM)
14791           && !crtl->calls_eh_return)
14792         {
14793           saved_regs_mask &= ~ (1 << LR_REGNUM);
14794           saved_regs_mask |=   (1 << PC_REGNUM);
14795           rfe = IS_INTERRUPT (func_type);
14796         }
14797       else
14798         rfe = 0;
14799
14800       /* Load the registers off the stack.  If we only have one register
14801          to load use the LDR instruction - it is faster.  For Thumb-2
14802          always use pop and the assembler will pick the best instruction.*/
14803       if (TARGET_ARM && saved_regs_mask == (1 << LR_REGNUM)
14804           && !IS_INTERRUPT(func_type))
14805         {
14806           asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
14807         }
14808       else if (saved_regs_mask)
14809         {
14810           if (saved_regs_mask & (1 << SP_REGNUM))
14811             /* Note - write back to the stack register is not enabled
14812                (i.e. "ldmfd sp!...").  We know that the stack pointer is
14813                in the list of registers and if we add writeback the
14814                instruction becomes UNPREDICTABLE.  */
14815             print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask,
14816                              rfe);
14817           else if (TARGET_ARM)
14818             print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, saved_regs_mask,
14819                              rfe);
14820           else
14821             print_multi_reg (f, "pop\t", SP_REGNUM, saved_regs_mask, 0);
14822         }
14823
14824       if (crtl->args.pretend_args_size)
14825         {
14826           /* Unwind the pre-pushed regs.  */
14827           operands[0] = operands[1] = stack_pointer_rtx;
14828           operands[2] = GEN_INT (crtl->args.pretend_args_size);
14829           output_add_immediate (operands);
14830         }
14831     }
14832
14833   /* We may have already restored PC directly from the stack.  */
14834   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
14835     return "";
14836
14837   /* Stack adjustment for exception handler.  */
14838   if (crtl->calls_eh_return)
14839     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
14840                  ARM_EH_STACKADJ_REGNUM);
14841
14842   /* Generate the return instruction.  */
14843   switch ((int) ARM_FUNC_TYPE (func_type))
14844     {
14845     case ARM_FT_ISR:
14846     case ARM_FT_FIQ:
14847       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
14848       break;
14849
14850     case ARM_FT_EXCEPTION:
14851       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
14852       break;
14853
14854     case ARM_FT_INTERWORKED:
14855       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
14856       break;
14857
14858     default:
14859       if (IS_STACKALIGN (func_type))
14860         {
14861           /* See comment in arm_expand_prologue.  */
14862           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, 0);
14863         }
14864       if (arm_arch5 || arm_arch4t)
14865         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
14866       else
14867         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
14868       break;
14869     }
14870
14871   return "";
14872 }
14873
14874 static void
14875 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
14876                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
14877 {
14878   arm_stack_offsets *offsets;
14879
14880   if (TARGET_THUMB1)
14881     {
14882       int regno;
14883
14884       /* Emit any call-via-reg trampolines that are needed for v4t support
14885          of call_reg and call_value_reg type insns.  */
14886       for (regno = 0; regno < LR_REGNUM; regno++)
14887         {
14888           rtx label = cfun->machine->call_via[regno];
14889
14890           if (label != NULL)
14891             {
14892               switch_to_section (function_section (current_function_decl));
14893               targetm.asm_out.internal_label (asm_out_file, "L",
14894                                               CODE_LABEL_NUMBER (label));
14895               asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
14896             }
14897         }
14898
14899       /* ??? Probably not safe to set this here, since it assumes that a
14900          function will be emitted as assembly immediately after we generate
14901          RTL for it.  This does not happen for inline functions.  */
14902       cfun->machine->return_used_this_function = 0;
14903     }
14904   else /* TARGET_32BIT */
14905     {
14906       /* We need to take into account any stack-frame rounding.  */
14907       offsets = arm_get_frame_offsets ();
14908
14909       gcc_assert (!use_return_insn (FALSE, NULL)
14910                   || (cfun->machine->return_used_this_function != 0)
14911                   || offsets->saved_regs == offsets->outgoing_args
14912                   || frame_pointer_needed);
14913
14914       /* Reset the ARM-specific per-function variables.  */
14915       after_arm_reorg = 0;
14916     }
14917 }
14918
14919 /* Generate and emit an insn that we will recognize as a push_multi.
14920    Unfortunately, since this insn does not reflect very well the actual
14921    semantics of the operation, we need to annotate the insn for the benefit
14922    of DWARF2 frame unwind information.  */
14923 static rtx
14924 emit_multi_reg_push (unsigned long mask)
14925 {
14926   int num_regs = 0;
14927   int num_dwarf_regs;
14928   int i, j;
14929   rtx par;
14930   rtx dwarf;
14931   int dwarf_par_index;
14932   rtx tmp, reg;
14933
14934   for (i = 0; i <= LAST_ARM_REGNUM; i++)
14935     if (mask & (1 << i))
14936       num_regs++;
14937
14938   gcc_assert (num_regs && num_regs <= 16);
14939
14940   /* We don't record the PC in the dwarf frame information.  */
14941   num_dwarf_regs = num_regs;
14942   if (mask & (1 << PC_REGNUM))
14943     num_dwarf_regs--;
14944
14945   /* For the body of the insn we are going to generate an UNSPEC in
14946      parallel with several USEs.  This allows the insn to be recognized
14947      by the push_multi pattern in the arm.md file.
14948
14949      The body of the insn looks something like this:
14950
14951        (parallel [
14952            (set (mem:BLK (pre_modify:SI (reg:SI sp)
14953                                         (const_int:SI <num>)))
14954                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
14955            (use (reg:SI XX))
14956            (use (reg:SI YY))
14957            ...
14958         ])
14959
14960      For the frame note however, we try to be more explicit and actually
14961      show each register being stored into the stack frame, plus a (single)
14962      decrement of the stack pointer.  We do it this way in order to be
14963      friendly to the stack unwinding code, which only wants to see a single
14964      stack decrement per instruction.  The RTL we generate for the note looks
14965      something like this:
14966
14967       (sequence [
14968            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
14969            (set (mem:SI (reg:SI sp)) (reg:SI r4))
14970            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI XX))
14971            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI YY))
14972            ...
14973         ])
14974
14975      FIXME:: In an ideal world the PRE_MODIFY would not exist and
14976      instead we'd have a parallel expression detailing all
14977      the stores to the various memory addresses so that debug
14978      information is more up-to-date. Remember however while writing
14979      this to take care of the constraints with the push instruction.
14980
14981      Note also that this has to be taken care of for the VFP registers.
14982
14983      For more see PR43399.  */
14984
14985   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
14986   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
14987   dwarf_par_index = 1;
14988
14989   for (i = 0; i <= LAST_ARM_REGNUM; i++)
14990     {
14991       if (mask & (1 << i))
14992         {
14993           reg = gen_rtx_REG (SImode, i);
14994
14995           XVECEXP (par, 0, 0)
14996             = gen_rtx_SET (VOIDmode,
14997                            gen_frame_mem
14998                            (BLKmode,
14999                             gen_rtx_PRE_MODIFY (Pmode,
15000                                                 stack_pointer_rtx,
15001                                                 plus_constant
15002                                                 (stack_pointer_rtx,
15003                                                  -4 * num_regs))
15004                             ),
15005                            gen_rtx_UNSPEC (BLKmode,
15006                                            gen_rtvec (1, reg),
15007                                            UNSPEC_PUSH_MULT));
15008
15009           if (i != PC_REGNUM)
15010             {
15011               tmp = gen_rtx_SET (VOIDmode,
15012                                  gen_frame_mem (SImode, stack_pointer_rtx),
15013                                  reg);
15014               RTX_FRAME_RELATED_P (tmp) = 1;
15015               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
15016               dwarf_par_index++;
15017             }
15018
15019           break;
15020         }
15021     }
15022
15023   for (j = 1, i++; j < num_regs; i++)
15024     {
15025       if (mask & (1 << i))
15026         {
15027           reg = gen_rtx_REG (SImode, i);
15028
15029           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
15030
15031           if (i != PC_REGNUM)
15032             {
15033               tmp
15034                 = gen_rtx_SET (VOIDmode,
15035                                gen_frame_mem
15036                                (SImode,
15037                                 plus_constant (stack_pointer_rtx,
15038                                                4 * j)),
15039                                reg);
15040               RTX_FRAME_RELATED_P (tmp) = 1;
15041               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
15042             }
15043
15044           j++;
15045         }
15046     }
15047
15048   par = emit_insn (par);
15049
15050   tmp = gen_rtx_SET (VOIDmode,
15051                      stack_pointer_rtx,
15052                      plus_constant (stack_pointer_rtx, -4 * num_regs));
15053   RTX_FRAME_RELATED_P (tmp) = 1;
15054   XVECEXP (dwarf, 0, 0) = tmp;
15055
15056   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
15057
15058   return par;
15059 }
15060
15061 /* Calculate the size of the return value that is passed in registers.  */
15062 static unsigned
15063 arm_size_return_regs (void)
15064 {
15065   enum machine_mode mode;
15066
15067   if (crtl->return_rtx != 0)
15068     mode = GET_MODE (crtl->return_rtx);
15069   else
15070     mode = DECL_MODE (DECL_RESULT (current_function_decl));
15071
15072   return GET_MODE_SIZE (mode);
15073 }
15074
15075 static rtx
15076 emit_sfm (int base_reg, int count)
15077 {
15078   rtx par;
15079   rtx dwarf;
15080   rtx tmp, reg;
15081   int i;
15082
15083   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
15084   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
15085
15086   reg = gen_rtx_REG (XFmode, base_reg++);
15087
15088   XVECEXP (par, 0, 0)
15089     = gen_rtx_SET (VOIDmode,
15090                    gen_frame_mem
15091                    (BLKmode,
15092                     gen_rtx_PRE_MODIFY (Pmode,
15093                                         stack_pointer_rtx,
15094                                         plus_constant
15095                                         (stack_pointer_rtx,
15096                                          -12 * count))
15097                     ),
15098                    gen_rtx_UNSPEC (BLKmode,
15099                                    gen_rtvec (1, reg),
15100                                    UNSPEC_PUSH_MULT));
15101   tmp = gen_rtx_SET (VOIDmode,
15102                      gen_frame_mem (XFmode, stack_pointer_rtx), reg);
15103   RTX_FRAME_RELATED_P (tmp) = 1;
15104   XVECEXP (dwarf, 0, 1) = tmp;
15105
15106   for (i = 1; i < count; i++)
15107     {
15108       reg = gen_rtx_REG (XFmode, base_reg++);
15109       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
15110
15111       tmp = gen_rtx_SET (VOIDmode,
15112                          gen_frame_mem (XFmode,
15113                                         plus_constant (stack_pointer_rtx,
15114                                                        i * 12)),
15115                          reg);
15116       RTX_FRAME_RELATED_P (tmp) = 1;
15117       XVECEXP (dwarf, 0, i + 1) = tmp;
15118     }
15119
15120   tmp = gen_rtx_SET (VOIDmode,
15121                      stack_pointer_rtx,
15122                      plus_constant (stack_pointer_rtx, -12 * count));
15123
15124   RTX_FRAME_RELATED_P (tmp) = 1;
15125   XVECEXP (dwarf, 0, 0) = tmp;
15126
15127   par = emit_insn (par);
15128   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
15129
15130   return par;
15131 }
15132
15133
15134 /* Return true if the current function needs to save/restore LR.  */
15135
15136 static bool
15137 thumb_force_lr_save (void)
15138 {
15139   return !cfun->machine->lr_save_eliminated
15140          && (!leaf_function_p ()
15141              || thumb_far_jump_used_p ()
15142              || df_regs_ever_live_p (LR_REGNUM));
15143 }
15144
15145
15146 /* Compute the distance from register FROM to register TO.
15147    These can be the arg pointer (26), the soft frame pointer (25),
15148    the stack pointer (13) or the hard frame pointer (11).
15149    In thumb mode r7 is used as the soft frame pointer, if needed.
15150    Typical stack layout looks like this:
15151
15152        old stack pointer -> |    |
15153                              ----
15154                             |    | \
15155                             |    |   saved arguments for
15156                             |    |   vararg functions
15157                             |    | /
15158                               --
15159    hard FP & arg pointer -> |    | \
15160                             |    |   stack
15161                             |    |   frame
15162                             |    | /
15163                               --
15164                             |    | \
15165                             |    |   call saved
15166                             |    |   registers
15167       soft frame pointer -> |    | /
15168                               --
15169                             |    | \
15170                             |    |   local
15171                             |    |   variables
15172      locals base pointer -> |    | /
15173                               --
15174                             |    | \
15175                             |    |   outgoing
15176                             |    |   arguments
15177    current stack pointer -> |    | /
15178                               --
15179
15180   For a given function some or all of these stack components
15181   may not be needed, giving rise to the possibility of
15182   eliminating some of the registers.
15183
15184   The values returned by this function must reflect the behavior
15185   of arm_expand_prologue() and arm_compute_save_reg_mask().
15186
15187   The sign of the number returned reflects the direction of stack
15188   growth, so the values are positive for all eliminations except
15189   from the soft frame pointer to the hard frame pointer.
15190
15191   SFP may point just inside the local variables block to ensure correct
15192   alignment.  */
15193
15194
15195 /* Calculate stack offsets.  These are used to calculate register elimination
15196    offsets and in prologue/epilogue code.  Also calculates which registers
15197    should be saved.  */
15198
15199 static arm_stack_offsets *
15200 arm_get_frame_offsets (void)
15201 {
15202   struct arm_stack_offsets *offsets;
15203   unsigned long func_type;
15204   int leaf;
15205   int saved;
15206   int core_saved;
15207   HOST_WIDE_INT frame_size;
15208   int i;
15209
15210   offsets = &cfun->machine->stack_offsets;
15211
15212   /* We need to know if we are a leaf function.  Unfortunately, it
15213      is possible to be called after start_sequence has been called,
15214      which causes get_insns to return the insns for the sequence,
15215      not the function, which will cause leaf_function_p to return
15216      the incorrect result.
15217
15218      to know about leaf functions once reload has completed, and the
15219      frame size cannot be changed after that time, so we can safely
15220      use the cached value.  */
15221
15222   if (reload_completed)
15223     return offsets;
15224
15225   /* Initially this is the size of the local variables.  It will translated
15226      into an offset once we have determined the size of preceding data.  */
15227   frame_size = ROUND_UP_WORD (get_frame_size ());
15228
15229   leaf = leaf_function_p ();
15230
15231   /* Space for variadic functions.  */
15232   offsets->saved_args = crtl->args.pretend_args_size;
15233
15234   /* In Thumb mode this is incorrect, but never used.  */
15235   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0) +
15236                    arm_compute_static_chain_stack_bytes();
15237
15238   if (TARGET_32BIT)
15239     {
15240       unsigned int regno;
15241
15242       offsets->saved_regs_mask = arm_compute_save_reg_mask ();
15243       core_saved = bit_count (offsets->saved_regs_mask) * 4;
15244       saved = core_saved;
15245
15246       /* We know that SP will be doubleword aligned on entry, and we must
15247          preserve that condition at any subroutine call.  We also require the
15248          soft frame pointer to be doubleword aligned.  */
15249
15250       if (TARGET_REALLY_IWMMXT)
15251         {
15252           /* Check for the call-saved iWMMXt registers.  */
15253           for (regno = FIRST_IWMMXT_REGNUM;
15254                regno <= LAST_IWMMXT_REGNUM;
15255                regno++)
15256             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
15257               saved += 8;
15258         }
15259
15260       func_type = arm_current_func_type ();
15261       if (! IS_VOLATILE (func_type))
15262         {
15263           /* Space for saved FPA registers.  */
15264           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
15265             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
15266             saved += 12;
15267
15268           /* Space for saved VFP registers.  */
15269           if (TARGET_HARD_FLOAT && TARGET_VFP)
15270             saved += arm_get_vfp_saved_size ();
15271         }
15272     }
15273   else /* TARGET_THUMB1 */
15274     {
15275       offsets->saved_regs_mask = thumb1_compute_save_reg_mask ();
15276       core_saved = bit_count (offsets->saved_regs_mask) * 4;
15277       saved = core_saved;
15278       if (TARGET_BACKTRACE)
15279         saved += 16;
15280     }
15281
15282   /* Saved registers include the stack frame.  */
15283   offsets->saved_regs = offsets->saved_args + saved +
15284                         arm_compute_static_chain_stack_bytes();
15285   offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
15286   /* A leaf function does not need any stack alignment if it has nothing
15287      on the stack.  */
15288   if (leaf && frame_size == 0)
15289     {
15290       offsets->outgoing_args = offsets->soft_frame;
15291       offsets->locals_base = offsets->soft_frame;
15292       return offsets;
15293     }
15294
15295   /* Ensure SFP has the correct alignment.  */
15296   if (ARM_DOUBLEWORD_ALIGN
15297       && (offsets->soft_frame & 7))
15298     {
15299       offsets->soft_frame += 4;
15300       /* Try to align stack by pushing an extra reg.  Don't bother doing this
15301          when there is a stack frame as the alignment will be rolled into
15302          the normal stack adjustment.  */
15303       if (frame_size + crtl->outgoing_args_size == 0)
15304         {
15305           int reg = -1;
15306
15307           /* If it is safe to use r3, then do so.  This sometimes 
15308              generates better code on Thumb-2 by avoiding the need to
15309              use 32-bit push/pop instructions.  */
15310           if (!crtl->tail_call_emit
15311               && arm_size_return_regs () <= 12
15312               && (offsets->saved_regs_mask & (1 << 3)) == 0)
15313             {
15314               reg = 3;
15315             }
15316           else
15317             for (i = 4; i <= (TARGET_THUMB1 ? LAST_LO_REGNUM : 11); i++)
15318               {
15319                 if ((offsets->saved_regs_mask & (1 << i)) == 0)
15320                   {
15321                     reg = i;
15322                     break;
15323                   }
15324               }
15325
15326           if (reg != -1)
15327             {
15328               offsets->saved_regs += 4;
15329               offsets->saved_regs_mask |= (1 << reg);
15330             }
15331         }
15332     }
15333
15334   offsets->locals_base = offsets->soft_frame + frame_size;
15335   offsets->outgoing_args = (offsets->locals_base
15336                             + crtl->outgoing_args_size);
15337
15338   if (ARM_DOUBLEWORD_ALIGN)
15339     {
15340       /* Ensure SP remains doubleword aligned.  */
15341       if (offsets->outgoing_args & 7)
15342         offsets->outgoing_args += 4;
15343       gcc_assert (!(offsets->outgoing_args & 7));
15344     }
15345
15346   return offsets;
15347 }
15348
15349
15350 /* Calculate the relative offsets for the different stack pointers.  Positive
15351    offsets are in the direction of stack growth.  */
15352
15353 HOST_WIDE_INT
15354 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
15355 {
15356   arm_stack_offsets *offsets;
15357
15358   offsets = arm_get_frame_offsets ();
15359
15360   /* OK, now we have enough information to compute the distances.
15361      There must be an entry in these switch tables for each pair
15362      of registers in ELIMINABLE_REGS, even if some of the entries
15363      seem to be redundant or useless.  */
15364   switch (from)
15365     {
15366     case ARG_POINTER_REGNUM:
15367       switch (to)
15368         {
15369         case THUMB_HARD_FRAME_POINTER_REGNUM:
15370           return 0;
15371
15372         case FRAME_POINTER_REGNUM:
15373           /* This is the reverse of the soft frame pointer
15374              to hard frame pointer elimination below.  */
15375           return offsets->soft_frame - offsets->saved_args;
15376
15377         case ARM_HARD_FRAME_POINTER_REGNUM:
15378           /* This is only non-zero in the case where the static chain register
15379              is stored above the frame.  */
15380           return offsets->frame - offsets->saved_args - 4;
15381
15382         case STACK_POINTER_REGNUM:
15383           /* If nothing has been pushed on the stack at all
15384              then this will return -4.  This *is* correct!  */
15385           return offsets->outgoing_args - (offsets->saved_args + 4);
15386
15387         default:
15388           gcc_unreachable ();
15389         }
15390       gcc_unreachable ();
15391
15392     case FRAME_POINTER_REGNUM:
15393       switch (to)
15394         {
15395         case THUMB_HARD_FRAME_POINTER_REGNUM:
15396           return 0;
15397
15398         case ARM_HARD_FRAME_POINTER_REGNUM:
15399           /* The hard frame pointer points to the top entry in the
15400              stack frame.  The soft frame pointer to the bottom entry
15401              in the stack frame.  If there is no stack frame at all,
15402              then they are identical.  */
15403
15404           return offsets->frame - offsets->soft_frame;
15405
15406         case STACK_POINTER_REGNUM:
15407           return offsets->outgoing_args - offsets->soft_frame;
15408
15409         default:
15410           gcc_unreachable ();
15411         }
15412       gcc_unreachable ();
15413
15414     default:
15415       /* You cannot eliminate from the stack pointer.
15416          In theory you could eliminate from the hard frame
15417          pointer to the stack pointer, but this will never
15418          happen, since if a stack frame is not needed the
15419          hard frame pointer will never be used.  */
15420       gcc_unreachable ();
15421     }
15422 }
15423
15424 /* Given FROM and TO register numbers, say whether this elimination is
15425    allowed.  Frame pointer elimination is automatically handled.
15426
15427    All eliminations are permissible.  Note that ARG_POINTER_REGNUM and
15428    HARD_FRAME_POINTER_REGNUM are in fact the same thing.  If we need a frame
15429    pointer, we must eliminate FRAME_POINTER_REGNUM into
15430    HARD_FRAME_POINTER_REGNUM and not into STACK_POINTER_REGNUM or
15431    ARG_POINTER_REGNUM.  */
15432
15433 bool
15434 arm_can_eliminate (const int from, const int to)
15435 {
15436   return ((to == FRAME_POINTER_REGNUM && from == ARG_POINTER_REGNUM) ? false :
15437           (to == STACK_POINTER_REGNUM && frame_pointer_needed) ? false :
15438           (to == ARM_HARD_FRAME_POINTER_REGNUM && TARGET_THUMB) ? false :
15439           (to == THUMB_HARD_FRAME_POINTER_REGNUM && TARGET_ARM) ? false :
15440            true);
15441 }
15442
15443 /* Emit RTL to save coprocessor registers on function entry.  Returns the
15444    number of bytes pushed.  */
15445
15446 static int
15447 arm_save_coproc_regs(void)
15448 {
15449   int saved_size = 0;
15450   unsigned reg;
15451   unsigned start_reg;
15452   rtx insn;
15453
15454   for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
15455     if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
15456       {
15457         insn = gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx);
15458         insn = gen_rtx_MEM (V2SImode, insn);
15459         insn = emit_set_insn (insn, gen_rtx_REG (V2SImode, reg));
15460         RTX_FRAME_RELATED_P (insn) = 1;
15461         saved_size += 8;
15462       }
15463
15464   /* Save any floating point call-saved registers used by this
15465      function.  */
15466   if (TARGET_FPA_EMU2)
15467     {
15468       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
15469         if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
15470           {
15471             insn = gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx);
15472             insn = gen_rtx_MEM (XFmode, insn);
15473             insn = emit_set_insn (insn, gen_rtx_REG (XFmode, reg));
15474             RTX_FRAME_RELATED_P (insn) = 1;
15475             saved_size += 12;
15476           }
15477     }
15478   else
15479     {
15480       start_reg = LAST_FPA_REGNUM;
15481
15482       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
15483         {
15484           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
15485             {
15486               if (start_reg - reg == 3)
15487                 {
15488                   insn = emit_sfm (reg, 4);
15489                   RTX_FRAME_RELATED_P (insn) = 1;
15490                   saved_size += 48;
15491                   start_reg = reg - 1;
15492                 }
15493             }
15494           else
15495             {
15496               if (start_reg != reg)
15497                 {
15498                   insn = emit_sfm (reg + 1, start_reg - reg);
15499                   RTX_FRAME_RELATED_P (insn) = 1;
15500                   saved_size += (start_reg - reg) * 12;
15501                 }
15502               start_reg = reg - 1;
15503             }
15504         }
15505
15506       if (start_reg != reg)
15507         {
15508           insn = emit_sfm (reg + 1, start_reg - reg);
15509           saved_size += (start_reg - reg) * 12;
15510           RTX_FRAME_RELATED_P (insn) = 1;
15511         }
15512     }
15513   if (TARGET_HARD_FLOAT && TARGET_VFP)
15514     {
15515       start_reg = FIRST_VFP_REGNUM;
15516
15517       for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
15518         {
15519           if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
15520               && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
15521             {
15522               if (start_reg != reg)
15523                 saved_size += vfp_emit_fstmd (start_reg,
15524                                               (reg - start_reg) / 2);
15525               start_reg = reg + 2;
15526             }
15527         }
15528       if (start_reg != reg)
15529         saved_size += vfp_emit_fstmd (start_reg,
15530                                       (reg - start_reg) / 2);
15531     }
15532   return saved_size;
15533 }
15534
15535
15536 /* Set the Thumb frame pointer from the stack pointer.  */
15537
15538 static void
15539 thumb_set_frame_pointer (arm_stack_offsets *offsets)
15540 {
15541   HOST_WIDE_INT amount;
15542   rtx insn, dwarf;
15543
15544   amount = offsets->outgoing_args - offsets->locals_base;
15545   if (amount < 1024)
15546     insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15547                                   stack_pointer_rtx, GEN_INT (amount)));
15548   else
15549     {
15550       emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
15551       /* Thumb-2 RTL patterns expect sp as the first input.  Thumb-1
15552          expects the first two operands to be the same.  */
15553       if (TARGET_THUMB2)
15554         {
15555           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15556                                         stack_pointer_rtx,
15557                                         hard_frame_pointer_rtx));
15558         }
15559       else
15560         {
15561           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15562                                         hard_frame_pointer_rtx,
15563                                         stack_pointer_rtx));
15564         }
15565       dwarf = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
15566                            plus_constant (stack_pointer_rtx, amount));
15567       RTX_FRAME_RELATED_P (dwarf) = 1;
15568       add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
15569     }
15570
15571   RTX_FRAME_RELATED_P (insn) = 1;
15572 }
15573
15574 /* Generate the prologue instructions for entry into an ARM or Thumb-2
15575    function.  */
15576 void
15577 arm_expand_prologue (void)
15578 {
15579   rtx amount;
15580   rtx insn;
15581   rtx ip_rtx;
15582   unsigned long live_regs_mask;
15583   unsigned long func_type;
15584   int fp_offset = 0;
15585   int saved_pretend_args = 0;
15586   int saved_regs = 0;
15587   unsigned HOST_WIDE_INT args_to_push;
15588   arm_stack_offsets *offsets;
15589
15590   func_type = arm_current_func_type ();
15591
15592   /* Naked functions don't have prologues.  */
15593   if (IS_NAKED (func_type))
15594     return;
15595
15596   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
15597   args_to_push = crtl->args.pretend_args_size;
15598
15599   /* Compute which register we will have to save onto the stack.  */
15600   offsets = arm_get_frame_offsets ();
15601   live_regs_mask = offsets->saved_regs_mask;
15602
15603   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
15604
15605   if (IS_STACKALIGN (func_type))
15606     {
15607       rtx dwarf;
15608       rtx r0;
15609       rtx r1;
15610       /* Handle a word-aligned stack pointer.  We generate the following:
15611
15612           mov r0, sp
15613           bic r1, r0, #7
15614           mov sp, r1
15615           <save and restore r0 in normal prologue/epilogue>
15616           mov sp, r0
15617           bx lr
15618
15619          The unwinder doesn't need to know about the stack realignment.
15620          Just tell it we saved SP in r0.  */
15621       gcc_assert (TARGET_THUMB2 && !arm_arch_notm && args_to_push == 0);
15622
15623       r0 = gen_rtx_REG (SImode, 0);
15624       r1 = gen_rtx_REG (SImode, 1);
15625       /* Use a real rtvec rather than NULL_RTVEC so the rest of the
15626          compiler won't choke.  */
15627       dwarf = gen_rtx_UNSPEC (SImode, rtvec_alloc (0), UNSPEC_STACK_ALIGN);
15628       dwarf = gen_rtx_SET (VOIDmode, r0, dwarf);
15629       insn = gen_movsi (r0, stack_pointer_rtx);
15630       RTX_FRAME_RELATED_P (insn) = 1;
15631       add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
15632       emit_insn (insn);
15633       emit_insn (gen_andsi3 (r1, r0, GEN_INT (~(HOST_WIDE_INT)7)));
15634       emit_insn (gen_movsi (stack_pointer_rtx, r1));
15635     }
15636
15637   /* For APCS frames, if IP register is clobbered
15638      when creating frame, save that register in a special
15639      way.  */
15640   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
15641     {
15642       if (IS_INTERRUPT (func_type))
15643         {
15644           /* Interrupt functions must not corrupt any registers.
15645              Creating a frame pointer however, corrupts the IP
15646              register, so we must push it first.  */
15647           insn = emit_multi_reg_push (1 << IP_REGNUM);
15648
15649           /* Do not set RTX_FRAME_RELATED_P on this insn.
15650              The dwarf stack unwinding code only wants to see one
15651              stack decrement per function, and this is not it.  If
15652              this instruction is labeled as being part of the frame
15653              creation sequence then dwarf2out_frame_debug_expr will
15654              die when it encounters the assignment of IP to FP
15655              later on, since the use of SP here establishes SP as
15656              the CFA register and not IP.
15657
15658              Anyway this instruction is not really part of the stack
15659              frame creation although it is part of the prologue.  */
15660         }
15661       else if (IS_NESTED (func_type))
15662         {
15663           /* The Static chain register is the same as the IP register
15664              used as a scratch register during stack frame creation.
15665              To get around this need to find somewhere to store IP
15666              whilst the frame is being created.  We try the following
15667              places in order:
15668
15669                1. The last argument register.
15670                2. A slot on the stack above the frame.  (This only
15671                   works if the function is not a varargs function).
15672                3. Register r3, after pushing the argument registers
15673                   onto the stack.
15674
15675              Note - we only need to tell the dwarf2 backend about the SP
15676              adjustment in the second variant; the static chain register
15677              doesn't need to be unwound, as it doesn't contain a value
15678              inherited from the caller.  */
15679
15680           if (df_regs_ever_live_p (3) == false)
15681             insn = emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
15682           else if (args_to_push == 0)
15683             {
15684               rtx dwarf;
15685
15686               gcc_assert(arm_compute_static_chain_stack_bytes() == 4);
15687               saved_regs += 4;
15688
15689               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
15690               insn = emit_set_insn (gen_frame_mem (SImode, insn), ip_rtx);
15691               fp_offset = 4;
15692
15693               /* Just tell the dwarf backend that we adjusted SP.  */
15694               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
15695                                    plus_constant (stack_pointer_rtx,
15696                                                   -fp_offset));
15697               RTX_FRAME_RELATED_P (insn) = 1;
15698               add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
15699             }
15700           else
15701             {
15702               /* Store the args on the stack.  */
15703               if (cfun->machine->uses_anonymous_args)
15704                 insn = emit_multi_reg_push
15705                   ((0xf0 >> (args_to_push / 4)) & 0xf);
15706               else
15707                 insn = emit_insn
15708                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
15709                                GEN_INT (- args_to_push)));
15710
15711               RTX_FRAME_RELATED_P (insn) = 1;
15712
15713               saved_pretend_args = 1;
15714               fp_offset = args_to_push;
15715               args_to_push = 0;
15716
15717               /* Now reuse r3 to preserve IP.  */
15718               emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
15719             }
15720         }
15721
15722       insn = emit_set_insn (ip_rtx,
15723                             plus_constant (stack_pointer_rtx, fp_offset));
15724       RTX_FRAME_RELATED_P (insn) = 1;
15725     }
15726
15727   if (args_to_push)
15728     {
15729       /* Push the argument registers, or reserve space for them.  */
15730       if (cfun->machine->uses_anonymous_args)
15731         insn = emit_multi_reg_push
15732           ((0xf0 >> (args_to_push / 4)) & 0xf);
15733       else
15734         insn = emit_insn
15735           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
15736                        GEN_INT (- args_to_push)));
15737       RTX_FRAME_RELATED_P (insn) = 1;
15738     }
15739
15740   /* If this is an interrupt service routine, and the link register
15741      is going to be pushed, and we're not generating extra
15742      push of IP (needed when frame is needed and frame layout if apcs),
15743      subtracting four from LR now will mean that the function return
15744      can be done with a single instruction.  */
15745   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
15746       && (live_regs_mask & (1 << LR_REGNUM)) != 0
15747       && !(frame_pointer_needed && TARGET_APCS_FRAME)
15748       && TARGET_ARM)
15749     {
15750       rtx lr = gen_rtx_REG (SImode, LR_REGNUM);
15751       
15752       emit_set_insn (lr, plus_constant (lr, -4));
15753     }
15754
15755   if (live_regs_mask)
15756     {
15757       saved_regs += bit_count (live_regs_mask) * 4;
15758       if (optimize_size && !frame_pointer_needed
15759           && saved_regs == offsets->saved_regs - offsets->saved_args)
15760         {
15761           /* If no coprocessor registers are being pushed and we don't have
15762              to worry about a frame pointer then push extra registers to
15763              create the stack frame.  This is done is a way that does not
15764              alter the frame layout, so is independent of the epilogue.  */
15765           int n;
15766           int frame;
15767           n = 0;
15768           while (n < 8 && (live_regs_mask & (1 << n)) == 0)
15769             n++;
15770           frame = offsets->outgoing_args - (offsets->saved_args + saved_regs);
15771           if (frame && n * 4 >= frame)
15772             {
15773               n = frame / 4;
15774               live_regs_mask |= (1 << n) - 1;
15775               saved_regs += frame;
15776             }
15777         }
15778       insn = emit_multi_reg_push (live_regs_mask);
15779       RTX_FRAME_RELATED_P (insn) = 1;
15780     }
15781
15782   if (! IS_VOLATILE (func_type))
15783     saved_regs += arm_save_coproc_regs ();
15784
15785   if (frame_pointer_needed && TARGET_ARM)
15786     {
15787       /* Create the new frame pointer.  */
15788       if (TARGET_APCS_FRAME)
15789         {
15790           insn = GEN_INT (-(4 + args_to_push + fp_offset));
15791           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
15792           RTX_FRAME_RELATED_P (insn) = 1;
15793
15794           if (IS_NESTED (func_type))
15795             {
15796               /* Recover the static chain register.  */
15797               if (!df_regs_ever_live_p (3)
15798                   || saved_pretend_args)
15799                 insn = gen_rtx_REG (SImode, 3);
15800               else /* if (crtl->args.pretend_args_size == 0) */
15801                 {
15802                   insn = plus_constant (hard_frame_pointer_rtx, 4);
15803                   insn = gen_frame_mem (SImode, insn);
15804                 }
15805               emit_set_insn (ip_rtx, insn);
15806               /* Add a USE to stop propagate_one_insn() from barfing.  */
15807               emit_insn (gen_prologue_use (ip_rtx));
15808             }
15809         }
15810       else
15811         {
15812           insn = GEN_INT (saved_regs - 4);
15813           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
15814                                         stack_pointer_rtx, insn));
15815           RTX_FRAME_RELATED_P (insn) = 1;
15816         }
15817     }
15818
15819   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
15820     {
15821       /* This add can produce multiple insns for a large constant, so we
15822          need to get tricky.  */
15823       rtx last = get_last_insn ();
15824
15825       amount = GEN_INT (offsets->saved_args + saved_regs
15826                         - offsets->outgoing_args);
15827
15828       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
15829                                     amount));
15830       do
15831         {
15832           last = last ? NEXT_INSN (last) : get_insns ();
15833           RTX_FRAME_RELATED_P (last) = 1;
15834         }
15835       while (last != insn);
15836
15837       /* If the frame pointer is needed, emit a special barrier that
15838          will prevent the scheduler from moving stores to the frame
15839          before the stack adjustment.  */
15840       if (frame_pointer_needed)
15841         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
15842                                          hard_frame_pointer_rtx));
15843     }
15844
15845
15846   if (frame_pointer_needed && TARGET_THUMB2)
15847     thumb_set_frame_pointer (offsets);
15848
15849   if (flag_pic && arm_pic_register != INVALID_REGNUM)
15850     {
15851       unsigned long mask;
15852
15853       mask = live_regs_mask;
15854       mask &= THUMB2_WORK_REGS;
15855       if (!IS_NESTED (func_type))
15856         mask |= (1 << IP_REGNUM);
15857       arm_load_pic_register (mask);
15858     }
15859
15860   /* If we are profiling, make sure no instructions are scheduled before
15861      the call to mcount.  Similarly if the user has requested no
15862      scheduling in the prolog.  Similarly if we want non-call exceptions
15863      using the EABI unwinder, to prevent faulting instructions from being
15864      swapped with a stack adjustment.  */
15865   if (crtl->profile || !TARGET_SCHED_PROLOG
15866       || (arm_except_unwind_info (&global_options) == UI_TARGET
15867           && cfun->can_throw_non_call_exceptions))
15868     emit_insn (gen_blockage ());
15869
15870   /* If the link register is being kept alive, with the return address in it,
15871      then make sure that it does not get reused by the ce2 pass.  */
15872   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
15873     cfun->machine->lr_save_eliminated = 1;
15874 }
15875 \f
15876 /* Print condition code to STREAM.  Helper function for arm_print_operand.  */
15877 static void
15878 arm_print_condition (FILE *stream)
15879 {
15880   if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
15881     {
15882       /* Branch conversion is not implemented for Thumb-2.  */
15883       if (TARGET_THUMB)
15884         {
15885           output_operand_lossage ("predicated Thumb instruction");
15886           return;
15887         }
15888       if (current_insn_predicate != NULL)
15889         {
15890           output_operand_lossage
15891             ("predicated instruction in conditional sequence");
15892           return;
15893         }
15894
15895       fputs (arm_condition_codes[arm_current_cc], stream);
15896     }
15897   else if (current_insn_predicate)
15898     {
15899       enum arm_cond_code code;
15900
15901       if (TARGET_THUMB1)
15902         {
15903           output_operand_lossage ("predicated Thumb instruction");
15904           return;
15905         }
15906
15907       code = get_arm_condition_code (current_insn_predicate);
15908       fputs (arm_condition_codes[code], stream);
15909     }
15910 }
15911
15912
15913 /* If CODE is 'd', then the X is a condition operand and the instruction
15914    should only be executed if the condition is true.
15915    if CODE is 'D', then the X is a condition operand and the instruction
15916    should only be executed if the condition is false: however, if the mode
15917    of the comparison is CCFPEmode, then always execute the instruction -- we
15918    do this because in these circumstances !GE does not necessarily imply LT;
15919    in these cases the instruction pattern will take care to make sure that
15920    an instruction containing %d will follow, thereby undoing the effects of
15921    doing this instruction unconditionally.
15922    If CODE is 'N' then X is a floating point operand that must be negated
15923    before output.
15924    If CODE is 'B' then output a bitwise inverted value of X (a const int).
15925    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
15926 static void
15927 arm_print_operand (FILE *stream, rtx x, int code)
15928 {
15929   switch (code)
15930     {
15931     case '@':
15932       fputs (ASM_COMMENT_START, stream);
15933       return;
15934
15935     case '_':
15936       fputs (user_label_prefix, stream);
15937       return;
15938
15939     case '|':
15940       fputs (REGISTER_PREFIX, stream);
15941       return;
15942
15943     case '?':
15944       arm_print_condition (stream);
15945       return;
15946
15947     case '(':
15948       /* Nothing in unified syntax, otherwise the current condition code.  */
15949       if (!TARGET_UNIFIED_ASM)
15950         arm_print_condition (stream);
15951       break;
15952
15953     case ')':
15954       /* The current condition code in unified syntax, otherwise nothing.  */
15955       if (TARGET_UNIFIED_ASM)
15956         arm_print_condition (stream);
15957       break;
15958   
15959     case '.':
15960       /* The current condition code for a condition code setting instruction.
15961          Preceded by 's' in unified syntax, otherwise followed by 's'.  */
15962       if (TARGET_UNIFIED_ASM)
15963         {
15964           fputc('s', stream);
15965           arm_print_condition (stream);
15966         }
15967       else
15968         {
15969           arm_print_condition (stream);
15970           fputc('s', stream);
15971         }
15972       return;
15973
15974     case '!':
15975       /* If the instruction is conditionally executed then print
15976          the current condition code, otherwise print 's'.  */
15977       gcc_assert (TARGET_THUMB2 && TARGET_UNIFIED_ASM);
15978       if (current_insn_predicate)
15979         arm_print_condition (stream);
15980       else
15981         fputc('s', stream);
15982       break;
15983
15984     /* %# is a "break" sequence. It doesn't output anything, but is used to
15985        separate e.g. operand numbers from following text, if that text consists
15986        of further digits which we don't want to be part of the operand
15987        number.  */
15988     case '#':
15989       return;
15990
15991     case 'N':
15992       {
15993         REAL_VALUE_TYPE r;
15994         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
15995         r = real_value_negate (&r);
15996         fprintf (stream, "%s", fp_const_from_val (&r));
15997       }
15998       return;
15999
16000     /* An integer or symbol address without a preceding # sign.  */
16001     case 'c':
16002       switch (GET_CODE (x))
16003         {
16004         case CONST_INT:
16005           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
16006           break;
16007
16008         case SYMBOL_REF:
16009           output_addr_const (stream, x);
16010           break;
16011
16012         default:
16013           gcc_unreachable ();
16014         }
16015       return;
16016
16017     case 'B':
16018       if (GET_CODE (x) == CONST_INT)
16019         {
16020           HOST_WIDE_INT val;
16021           val = ARM_SIGN_EXTEND (~INTVAL (x));
16022           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
16023         }
16024       else
16025         {
16026           putc ('~', stream);
16027           output_addr_const (stream, x);
16028         }
16029       return;
16030
16031     case 'L':
16032       /* The low 16 bits of an immediate constant.  */
16033       fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL(x) & 0xffff);
16034       return;
16035
16036     case 'i':
16037       fprintf (stream, "%s", arithmetic_instr (x, 1));
16038       return;
16039
16040     /* Truncate Cirrus shift counts.  */
16041     case 's':
16042       if (GET_CODE (x) == CONST_INT)
16043         {
16044           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
16045           return;
16046         }
16047       arm_print_operand (stream, x, 0);
16048       return;
16049
16050     case 'I':
16051       fprintf (stream, "%s", arithmetic_instr (x, 0));
16052       return;
16053
16054     case 'S':
16055       {
16056         HOST_WIDE_INT val;
16057         const char *shift;
16058
16059         if (!shift_operator (x, SImode))
16060           {
16061             output_operand_lossage ("invalid shift operand");
16062             break;
16063           }
16064
16065         shift = shift_op (x, &val);
16066
16067         if (shift)
16068           {
16069             fprintf (stream, ", %s ", shift);
16070             if (val == -1)
16071               arm_print_operand (stream, XEXP (x, 1), 0);
16072             else
16073               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
16074           }
16075       }
16076       return;
16077
16078       /* An explanation of the 'Q', 'R' and 'H' register operands:
16079
16080          In a pair of registers containing a DI or DF value the 'Q'
16081          operand returns the register number of the register containing
16082          the least significant part of the value.  The 'R' operand returns
16083          the register number of the register containing the most
16084          significant part of the value.
16085
16086          The 'H' operand returns the higher of the two register numbers.
16087          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
16088          same as the 'Q' operand, since the most significant part of the
16089          value is held in the lower number register.  The reverse is true
16090          on systems where WORDS_BIG_ENDIAN is false.
16091
16092          The purpose of these operands is to distinguish between cases
16093          where the endian-ness of the values is important (for example
16094          when they are added together), and cases where the endian-ness
16095          is irrelevant, but the order of register operations is important.
16096          For example when loading a value from memory into a register
16097          pair, the endian-ness does not matter.  Provided that the value
16098          from the lower memory address is put into the lower numbered
16099          register, and the value from the higher address is put into the
16100          higher numbered register, the load will work regardless of whether
16101          the value being loaded is big-wordian or little-wordian.  The
16102          order of the two register loads can matter however, if the address
16103          of the memory location is actually held in one of the registers
16104          being overwritten by the load.
16105
16106          The 'Q' and 'R' constraints are also available for 64-bit
16107          constants.  */
16108     case 'Q':
16109       if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
16110         {
16111           rtx part = gen_lowpart (SImode, x);
16112           fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, INTVAL (part));
16113           return;
16114         }
16115
16116       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16117         {
16118           output_operand_lossage ("invalid operand for code '%c'", code);
16119           return;
16120         }
16121
16122       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
16123       return;
16124
16125     case 'R':
16126       if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
16127         {
16128           enum machine_mode mode = GET_MODE (x);
16129           rtx part;
16130
16131           if (mode == VOIDmode)
16132             mode = DImode;
16133           part = gen_highpart_mode (SImode, mode, x);
16134           fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, INTVAL (part));
16135           return;
16136         }
16137
16138       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16139         {
16140           output_operand_lossage ("invalid operand for code '%c'", code);
16141           return;
16142         }
16143
16144       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
16145       return;
16146
16147     case 'H':
16148       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16149         {
16150           output_operand_lossage ("invalid operand for code '%c'", code);
16151           return;
16152         }
16153
16154       asm_fprintf (stream, "%r", REGNO (x) + 1);
16155       return;
16156
16157     case 'J':
16158       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16159         {
16160           output_operand_lossage ("invalid operand for code '%c'", code);
16161           return;
16162         }
16163
16164       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 3 : 2));
16165       return;
16166
16167     case 'K':
16168       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
16169         {
16170           output_operand_lossage ("invalid operand for code '%c'", code);
16171           return;
16172         }
16173
16174       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 2 : 3));
16175       return;
16176
16177     case 'm':
16178       asm_fprintf (stream, "%r",
16179                    GET_CODE (XEXP (x, 0)) == REG
16180                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
16181       return;
16182
16183     case 'M':
16184       asm_fprintf (stream, "{%r-%r}",
16185                    REGNO (x),
16186                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
16187       return;
16188
16189     /* Like 'M', but writing doubleword vector registers, for use by Neon
16190        insns.  */
16191     case 'h':
16192       {
16193         int regno = (REGNO (x) - FIRST_VFP_REGNUM) / 2;
16194         int numregs = ARM_NUM_REGS (GET_MODE (x)) / 2;
16195         if (numregs == 1)
16196           asm_fprintf (stream, "{d%d}", regno);
16197         else
16198           asm_fprintf (stream, "{d%d-d%d}", regno, regno + numregs - 1);
16199       }
16200       return;
16201
16202     case 'd':
16203       /* CONST_TRUE_RTX means always -- that's the default.  */
16204       if (x == const_true_rtx)
16205         return;
16206
16207       if (!COMPARISON_P (x))
16208         {
16209           output_operand_lossage ("invalid operand for code '%c'", code);
16210           return;
16211         }
16212
16213       fputs (arm_condition_codes[get_arm_condition_code (x)],
16214              stream);
16215       return;
16216
16217     case 'D':
16218       /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
16219          want to do that.  */
16220       if (x == const_true_rtx)
16221         {
16222           output_operand_lossage ("instruction never executed");
16223           return;
16224         }
16225       if (!COMPARISON_P (x))
16226         {
16227           output_operand_lossage ("invalid operand for code '%c'", code);
16228           return;
16229         }
16230
16231       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
16232                                  (get_arm_condition_code (x))],
16233              stream);
16234       return;
16235
16236     /* Cirrus registers can be accessed in a variety of ways:
16237          single floating point (f)
16238          double floating point (d)
16239          32bit integer         (fx)
16240          64bit integer         (dx).  */
16241     case 'W':                   /* Cirrus register in F mode.  */
16242     case 'X':                   /* Cirrus register in D mode.  */
16243     case 'Y':                   /* Cirrus register in FX mode.  */
16244     case 'Z':                   /* Cirrus register in DX mode.  */
16245       gcc_assert (GET_CODE (x) == REG
16246                   && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
16247
16248       fprintf (stream, "mv%s%s",
16249                code == 'W' ? "f"
16250                : code == 'X' ? "d"
16251                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
16252
16253       return;
16254
16255     /* Print cirrus register in the mode specified by the register's mode.  */
16256     case 'V':
16257       {
16258         int mode = GET_MODE (x);
16259
16260         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
16261           {
16262             output_operand_lossage ("invalid operand for code '%c'", code);
16263             return;
16264           }
16265
16266         fprintf (stream, "mv%s%s",
16267                  mode == DFmode ? "d"
16268                  : mode == SImode ? "fx"
16269                  : mode == DImode ? "dx"
16270                  : "f", reg_names[REGNO (x)] + 2);
16271
16272         return;
16273       }
16274
16275     case 'U':
16276       if (GET_CODE (x) != REG
16277           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
16278           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
16279         /* Bad value for wCG register number.  */
16280         {
16281           output_operand_lossage ("invalid operand for code '%c'", code);
16282           return;
16283         }
16284
16285       else
16286         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
16287       return;
16288
16289       /* Print an iWMMXt control register name.  */
16290     case 'w':
16291       if (GET_CODE (x) != CONST_INT
16292           || INTVAL (x) < 0
16293           || INTVAL (x) >= 16)
16294         /* Bad value for wC register number.  */
16295         {
16296           output_operand_lossage ("invalid operand for code '%c'", code);
16297           return;
16298         }
16299
16300       else
16301         {
16302           static const char * wc_reg_names [16] =
16303             {
16304               "wCID",  "wCon",  "wCSSF", "wCASF",
16305               "wC4",   "wC5",   "wC6",   "wC7",
16306               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
16307               "wC12",  "wC13",  "wC14",  "wC15"
16308             };
16309
16310           fprintf (stream, wc_reg_names [INTVAL (x)]);
16311         }
16312       return;
16313
16314     /* Print the high single-precision register of a VFP double-precision
16315        register.  */
16316     case 'p':
16317       {
16318         int mode = GET_MODE (x);
16319         int regno;
16320
16321         if (GET_MODE_SIZE (mode) != 8 || GET_CODE (x) != REG)
16322           {
16323             output_operand_lossage ("invalid operand for code '%c'", code);
16324             return;
16325           }
16326
16327         regno = REGNO (x);
16328         if (!VFP_REGNO_OK_FOR_DOUBLE (regno))
16329           {
16330             output_operand_lossage ("invalid operand for code '%c'", code);
16331             return;
16332           }
16333
16334         fprintf (stream, "s%d", regno - FIRST_VFP_REGNUM + 1);
16335       }
16336       return;
16337
16338     /* Print a VFP/Neon double precision or quad precision register name.  */
16339     case 'P':
16340     case 'q':
16341       {
16342         int mode = GET_MODE (x);
16343         int is_quad = (code == 'q');
16344         int regno;
16345
16346         if (GET_MODE_SIZE (mode) != (is_quad ? 16 : 8))
16347           {
16348             output_operand_lossage ("invalid operand for code '%c'", code);
16349             return;
16350           }
16351
16352         if (GET_CODE (x) != REG
16353             || !IS_VFP_REGNUM (REGNO (x)))
16354           {
16355             output_operand_lossage ("invalid operand for code '%c'", code);
16356             return;
16357           }
16358
16359         regno = REGNO (x);
16360         if ((is_quad && !NEON_REGNO_OK_FOR_QUAD (regno))
16361             || (!is_quad && !VFP_REGNO_OK_FOR_DOUBLE (regno)))
16362           {
16363             output_operand_lossage ("invalid operand for code '%c'", code);
16364             return;
16365           }
16366
16367         fprintf (stream, "%c%d", is_quad ? 'q' : 'd',
16368           (regno - FIRST_VFP_REGNUM) >> (is_quad ? 2 : 1));
16369       }
16370       return;
16371
16372     /* These two codes print the low/high doubleword register of a Neon quad
16373        register, respectively.  For pair-structure types, can also print
16374        low/high quadword registers.  */
16375     case 'e':
16376     case 'f':
16377       {
16378         int mode = GET_MODE (x);
16379         int regno;
16380
16381         if ((GET_MODE_SIZE (mode) != 16
16382              && GET_MODE_SIZE (mode) != 32) || GET_CODE (x) != REG)
16383           {
16384             output_operand_lossage ("invalid operand for code '%c'", code);
16385             return;
16386           }
16387
16388         regno = REGNO (x);
16389         if (!NEON_REGNO_OK_FOR_QUAD (regno))
16390           {
16391             output_operand_lossage ("invalid operand for code '%c'", code);
16392             return;
16393           }
16394
16395         if (GET_MODE_SIZE (mode) == 16)
16396           fprintf (stream, "d%d", ((regno - FIRST_VFP_REGNUM) >> 1)
16397                                   + (code == 'f' ? 1 : 0));
16398         else
16399           fprintf (stream, "q%d", ((regno - FIRST_VFP_REGNUM) >> 2)
16400                                   + (code == 'f' ? 1 : 0));
16401       }
16402       return;
16403
16404     /* Print a VFPv3 floating-point constant, represented as an integer
16405        index.  */
16406     case 'G':
16407       {
16408         int index = vfp3_const_double_index (x);
16409         gcc_assert (index != -1);
16410         fprintf (stream, "%d", index);
16411       }
16412       return;
16413
16414     /* Print bits representing opcode features for Neon.
16415
16416        Bit 0 is 1 for signed, 0 for unsigned.  Floats count as signed
16417        and polynomials as unsigned.
16418
16419        Bit 1 is 1 for floats and polynomials, 0 for ordinary integers.
16420
16421        Bit 2 is 1 for rounding functions, 0 otherwise.  */
16422
16423     /* Identify the type as 's', 'u', 'p' or 'f'.  */
16424     case 'T':
16425       {
16426         HOST_WIDE_INT bits = INTVAL (x);
16427         fputc ("uspf"[bits & 3], stream);
16428       }
16429       return;
16430
16431     /* Likewise, but signed and unsigned integers are both 'i'.  */
16432     case 'F':
16433       {
16434         HOST_WIDE_INT bits = INTVAL (x);
16435         fputc ("iipf"[bits & 3], stream);
16436       }
16437       return;
16438
16439     /* As for 'T', but emit 'u' instead of 'p'.  */
16440     case 't':
16441       {
16442         HOST_WIDE_INT bits = INTVAL (x);
16443         fputc ("usuf"[bits & 3], stream);
16444       }
16445       return;
16446
16447     /* Bit 2: rounding (vs none).  */
16448     case 'O':
16449       {
16450         HOST_WIDE_INT bits = INTVAL (x);
16451         fputs ((bits & 4) != 0 ? "r" : "", stream);
16452       }
16453       return;
16454
16455     /* Memory operand for vld1/vst1 instruction.  */
16456     case 'A':
16457       {
16458         rtx addr;
16459         bool postinc = FALSE;
16460         unsigned align, modesize, align_bits;
16461
16462         gcc_assert (GET_CODE (x) == MEM);
16463         addr = XEXP (x, 0);
16464         if (GET_CODE (addr) == POST_INC)
16465           {
16466             postinc = 1;
16467             addr = XEXP (addr, 0);
16468           }
16469         asm_fprintf (stream, "[%r", REGNO (addr));
16470
16471         /* We know the alignment of this access, so we can emit a hint in the
16472            instruction (for some alignments) as an aid to the memory subsystem
16473            of the target.  */
16474         align = MEM_ALIGN (x) >> 3;
16475         modesize = GET_MODE_SIZE (GET_MODE (x));
16476         
16477         /* Only certain alignment specifiers are supported by the hardware.  */
16478         if (modesize == 16 && (align % 32) == 0)
16479           align_bits = 256;
16480         else if ((modesize == 8 || modesize == 16) && (align % 16) == 0)
16481           align_bits = 128;
16482         else if ((align % 8) == 0)
16483           align_bits = 64;
16484         else
16485           align_bits = 0;
16486         
16487         if (align_bits != 0)
16488           asm_fprintf (stream, ":%d", align_bits);
16489
16490         asm_fprintf (stream, "]");
16491
16492         if (postinc)
16493           fputs("!", stream);
16494       }
16495       return;
16496
16497     case 'C':
16498       {
16499         rtx addr;
16500
16501         gcc_assert (GET_CODE (x) == MEM);
16502         addr = XEXP (x, 0);
16503         gcc_assert (GET_CODE (addr) == REG);
16504         asm_fprintf (stream, "[%r]", REGNO (addr));
16505       }
16506       return;
16507
16508     /* Translate an S register number into a D register number and element index.  */
16509     case 'y':
16510       {
16511         int mode = GET_MODE (x);
16512         int regno;
16513
16514         if (GET_MODE_SIZE (mode) != 4 || GET_CODE (x) != REG)
16515           {
16516             output_operand_lossage ("invalid operand for code '%c'", code);
16517             return;
16518           }
16519
16520         regno = REGNO (x);
16521         if (!VFP_REGNO_OK_FOR_SINGLE (regno))
16522           {
16523             output_operand_lossage ("invalid operand for code '%c'", code);
16524             return;
16525           }
16526
16527         regno = regno - FIRST_VFP_REGNUM;
16528         fprintf (stream, "d%d[%d]", regno / 2, regno % 2);
16529       }
16530       return;
16531
16532     /* Register specifier for vld1.16/vst1.16.  Translate the S register
16533        number into a D register number and element index.  */
16534     case 'z':
16535       {
16536         int mode = GET_MODE (x);
16537         int regno;
16538
16539         if (GET_MODE_SIZE (mode) != 2 || GET_CODE (x) != REG)
16540           {
16541             output_operand_lossage ("invalid operand for code '%c'", code);
16542             return;
16543           }
16544
16545         regno = REGNO (x);
16546         if (!VFP_REGNO_OK_FOR_SINGLE (regno))
16547           {
16548             output_operand_lossage ("invalid operand for code '%c'", code);
16549             return;
16550           }
16551
16552         regno = regno - FIRST_VFP_REGNUM;
16553         fprintf (stream, "d%d[%d]", regno/2, ((regno % 2) ? 2 : 0));
16554       }
16555       return;
16556       
16557     default:
16558       if (x == 0)
16559         {
16560           output_operand_lossage ("missing operand");
16561           return;
16562         }
16563
16564       switch (GET_CODE (x))
16565         {
16566         case REG:
16567           asm_fprintf (stream, "%r", REGNO (x));
16568           break;
16569
16570         case MEM:
16571           output_memory_reference_mode = GET_MODE (x);
16572           output_address (XEXP (x, 0));
16573           break;
16574
16575         case CONST_DOUBLE:
16576           if (TARGET_NEON)
16577             {
16578               char fpstr[20];
16579               real_to_decimal (fpstr, CONST_DOUBLE_REAL_VALUE (x),
16580                                sizeof (fpstr), 0, 1);
16581               fprintf (stream, "#%s", fpstr);
16582             }
16583           else
16584             fprintf (stream, "#%s", fp_immediate_constant (x));
16585           break;
16586
16587         default:
16588           gcc_assert (GET_CODE (x) != NEG);
16589           fputc ('#', stream);
16590           if (GET_CODE (x) == HIGH)
16591             {
16592               fputs (":lower16:", stream);
16593               x = XEXP (x, 0);
16594             }
16595             
16596           output_addr_const (stream, x);
16597           break;
16598         }
16599     }
16600 }
16601 \f
16602 /* Target hook for printing a memory address.  */
16603 static void
16604 arm_print_operand_address (FILE *stream, rtx x)
16605 {
16606   if (TARGET_32BIT)
16607     {
16608       int is_minus = GET_CODE (x) == MINUS;
16609
16610       if (GET_CODE (x) == REG)
16611         asm_fprintf (stream, "[%r, #0]", REGNO (x));
16612       else if (GET_CODE (x) == PLUS || is_minus)
16613         {
16614           rtx base = XEXP (x, 0);
16615           rtx index = XEXP (x, 1);
16616           HOST_WIDE_INT offset = 0;
16617           if (GET_CODE (base) != REG
16618               || (GET_CODE (index) == REG && REGNO (index) == SP_REGNUM))
16619             {
16620               /* Ensure that BASE is a register.  */
16621               /* (one of them must be).  */
16622               /* Also ensure the SP is not used as in index register.  */
16623               rtx temp = base;
16624               base = index;
16625               index = temp;
16626             }
16627           switch (GET_CODE (index))
16628             {
16629             case CONST_INT:
16630               offset = INTVAL (index);
16631               if (is_minus)
16632                 offset = -offset;
16633               asm_fprintf (stream, "[%r, #%wd]",
16634                            REGNO (base), offset);
16635               break;
16636
16637             case REG:
16638               asm_fprintf (stream, "[%r, %s%r]",
16639                            REGNO (base), is_minus ? "-" : "",
16640                            REGNO (index));
16641               break;
16642
16643             case MULT:
16644             case ASHIFTRT:
16645             case LSHIFTRT:
16646             case ASHIFT:
16647             case ROTATERT:
16648               {
16649                 asm_fprintf (stream, "[%r, %s%r",
16650                              REGNO (base), is_minus ? "-" : "",
16651                              REGNO (XEXP (index, 0)));
16652                 arm_print_operand (stream, index, 'S');
16653                 fputs ("]", stream);
16654                 break;
16655               }
16656
16657             default:
16658               gcc_unreachable ();
16659             }
16660         }
16661       else if (GET_CODE (x) == PRE_INC || GET_CODE (x) == POST_INC
16662                || GET_CODE (x) == PRE_DEC || GET_CODE (x) == POST_DEC)
16663         {
16664           extern enum machine_mode output_memory_reference_mode;
16665
16666           gcc_assert (GET_CODE (XEXP (x, 0)) == REG);
16667
16668           if (GET_CODE (x) == PRE_DEC || GET_CODE (x) == PRE_INC)
16669             asm_fprintf (stream, "[%r, #%s%d]!",
16670                          REGNO (XEXP (x, 0)),
16671                          GET_CODE (x) == PRE_DEC ? "-" : "",
16672                          GET_MODE_SIZE (output_memory_reference_mode));
16673           else
16674             asm_fprintf (stream, "[%r], #%s%d",
16675                          REGNO (XEXP (x, 0)),
16676                          GET_CODE (x) == POST_DEC ? "-" : "",
16677                          GET_MODE_SIZE (output_memory_reference_mode));
16678         }
16679       else if (GET_CODE (x) == PRE_MODIFY)
16680         {
16681           asm_fprintf (stream, "[%r, ", REGNO (XEXP (x, 0)));
16682           if (GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
16683             asm_fprintf (stream, "#%wd]!",
16684                          INTVAL (XEXP (XEXP (x, 1), 1)));
16685           else
16686             asm_fprintf (stream, "%r]!",
16687                          REGNO (XEXP (XEXP (x, 1), 1)));
16688         }
16689       else if (GET_CODE (x) == POST_MODIFY)
16690         {
16691           asm_fprintf (stream, "[%r], ", REGNO (XEXP (x, 0)));
16692           if (GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
16693             asm_fprintf (stream, "#%wd",
16694                          INTVAL (XEXP (XEXP (x, 1), 1)));
16695           else
16696             asm_fprintf (stream, "%r",
16697                          REGNO (XEXP (XEXP (x, 1), 1)));
16698         }
16699       else output_addr_const (stream, x);
16700     }
16701   else
16702     {
16703       if (GET_CODE (x) == REG)
16704         asm_fprintf (stream, "[%r]", REGNO (x));
16705       else if (GET_CODE (x) == POST_INC)
16706         asm_fprintf (stream, "%r!", REGNO (XEXP (x, 0)));
16707       else if (GET_CODE (x) == PLUS)
16708         {
16709           gcc_assert (GET_CODE (XEXP (x, 0)) == REG);
16710           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
16711             asm_fprintf (stream, "[%r, #%wd]",
16712                          REGNO (XEXP (x, 0)),
16713                          INTVAL (XEXP (x, 1)));
16714           else
16715             asm_fprintf (stream, "[%r, %r]",
16716                          REGNO (XEXP (x, 0)),
16717                          REGNO (XEXP (x, 1)));
16718         }
16719       else
16720         output_addr_const (stream, x);
16721     }
16722 }
16723 \f
16724 /* Target hook for indicating whether a punctuation character for
16725    TARGET_PRINT_OPERAND is valid.  */
16726 static bool
16727 arm_print_operand_punct_valid_p (unsigned char code)
16728 {
16729   return (code == '@' || code == '|' || code == '.'
16730           || code == '(' || code == ')' || code == '#'
16731           || (TARGET_32BIT && (code == '?'))
16732           || (TARGET_THUMB2 && (code == '!'))
16733           || (TARGET_THUMB && (code == '_')));
16734 }
16735 \f
16736 /* Target hook for assembling integer objects.  The ARM version needs to
16737    handle word-sized values specially.  */
16738 static bool
16739 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
16740 {
16741   enum machine_mode mode;
16742
16743   if (size == UNITS_PER_WORD && aligned_p)
16744     {
16745       fputs ("\t.word\t", asm_out_file);
16746       output_addr_const (asm_out_file, x);
16747
16748       /* Mark symbols as position independent.  We only do this in the
16749          .text segment, not in the .data segment.  */
16750       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
16751           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
16752         {
16753           /* See legitimize_pic_address for an explanation of the
16754              TARGET_VXWORKS_RTP check.  */
16755           if (TARGET_VXWORKS_RTP
16756               || (GET_CODE (x) == SYMBOL_REF && !SYMBOL_REF_LOCAL_P (x)))
16757             fputs ("(GOT)", asm_out_file);
16758           else
16759             fputs ("(GOTOFF)", asm_out_file);
16760         }
16761       fputc ('\n', asm_out_file);
16762       return true;
16763     }
16764
16765   mode = GET_MODE (x);
16766
16767   if (arm_vector_mode_supported_p (mode))
16768     {
16769       int i, units;
16770
16771       gcc_assert (GET_CODE (x) == CONST_VECTOR);
16772
16773       units = CONST_VECTOR_NUNITS (x);
16774       size = GET_MODE_SIZE (GET_MODE_INNER (mode));
16775
16776       if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
16777         for (i = 0; i < units; i++)
16778           {
16779             rtx elt = CONST_VECTOR_ELT (x, i);
16780             assemble_integer
16781               (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
16782           }
16783       else
16784         for (i = 0; i < units; i++)
16785           {
16786             rtx elt = CONST_VECTOR_ELT (x, i);
16787             REAL_VALUE_TYPE rval;
16788
16789             REAL_VALUE_FROM_CONST_DOUBLE (rval, elt);
16790
16791             assemble_real
16792               (rval, GET_MODE_INNER (mode),
16793               i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT);
16794           }
16795
16796       return true;
16797     }
16798
16799   return default_assemble_integer (x, size, aligned_p);
16800 }
16801
16802 static void
16803 arm_elf_asm_cdtor (rtx symbol, int priority, bool is_ctor)
16804 {
16805   section *s;
16806
16807   if (!TARGET_AAPCS_BASED)
16808     {
16809       (is_ctor ? 
16810        default_named_section_asm_out_constructor 
16811        : default_named_section_asm_out_destructor) (symbol, priority);
16812       return;
16813     }
16814
16815   /* Put these in the .init_array section, using a special relocation.  */
16816   if (priority != DEFAULT_INIT_PRIORITY)
16817     {
16818       char buf[18];
16819       sprintf (buf, "%s.%.5u", 
16820                is_ctor ? ".init_array" : ".fini_array",
16821                priority);
16822       s = get_section (buf, SECTION_WRITE, NULL_TREE);
16823     }
16824   else if (is_ctor)
16825     s = ctors_section;
16826   else
16827     s = dtors_section;
16828
16829   switch_to_section (s);
16830   assemble_align (POINTER_SIZE);
16831   fputs ("\t.word\t", asm_out_file);
16832   output_addr_const (asm_out_file, symbol);
16833   fputs ("(target1)\n", asm_out_file);
16834 }
16835
16836 /* Add a function to the list of static constructors.  */
16837
16838 static void
16839 arm_elf_asm_constructor (rtx symbol, int priority)
16840 {
16841   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/true);
16842 }
16843
16844 /* Add a function to the list of static destructors.  */
16845
16846 static void
16847 arm_elf_asm_destructor (rtx symbol, int priority)
16848 {
16849   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/false);
16850 }
16851 \f
16852 /* A finite state machine takes care of noticing whether or not instructions
16853    can be conditionally executed, and thus decrease execution time and code
16854    size by deleting branch instructions.  The fsm is controlled by
16855    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
16856
16857 /* The state of the fsm controlling condition codes are:
16858    0: normal, do nothing special
16859    1: make ASM_OUTPUT_OPCODE not output this instruction
16860    2: make ASM_OUTPUT_OPCODE not output this instruction
16861    3: make instructions conditional
16862    4: make instructions conditional
16863
16864    State transitions (state->state by whom under condition):
16865    0 -> 1 final_prescan_insn if the `target' is a label
16866    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
16867    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
16868    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
16869    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
16870           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
16871    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
16872           (the target insn is arm_target_insn).
16873
16874    If the jump clobbers the conditions then we use states 2 and 4.
16875
16876    A similar thing can be done with conditional return insns.
16877
16878    XXX In case the `target' is an unconditional branch, this conditionalising
16879    of the instructions always reduces code size, but not always execution
16880    time.  But then, I want to reduce the code size to somewhere near what
16881    /bin/cc produces.  */
16882
16883 /* In addition to this, state is maintained for Thumb-2 COND_EXEC
16884    instructions.  When a COND_EXEC instruction is seen the subsequent
16885    instructions are scanned so that multiple conditional instructions can be
16886    combined into a single IT block.  arm_condexec_count and arm_condexec_mask
16887    specify the length and true/false mask for the IT block.  These will be
16888    decremented/zeroed by arm_asm_output_opcode as the insns are output.  */
16889
16890 /* Returns the index of the ARM condition code string in
16891    `arm_condition_codes'.  COMPARISON should be an rtx like
16892    `(eq (...) (...))'.  */
16893 static enum arm_cond_code
16894 get_arm_condition_code (rtx comparison)
16895 {
16896   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
16897   enum arm_cond_code code;
16898   enum rtx_code comp_code = GET_CODE (comparison);
16899
16900   if (GET_MODE_CLASS (mode) != MODE_CC)
16901     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
16902                            XEXP (comparison, 1));
16903
16904   switch (mode)
16905     {
16906     case CC_DNEmode: code = ARM_NE; goto dominance;
16907     case CC_DEQmode: code = ARM_EQ; goto dominance;
16908     case CC_DGEmode: code = ARM_GE; goto dominance;
16909     case CC_DGTmode: code = ARM_GT; goto dominance;
16910     case CC_DLEmode: code = ARM_LE; goto dominance;
16911     case CC_DLTmode: code = ARM_LT; goto dominance;
16912     case CC_DGEUmode: code = ARM_CS; goto dominance;
16913     case CC_DGTUmode: code = ARM_HI; goto dominance;
16914     case CC_DLEUmode: code = ARM_LS; goto dominance;
16915     case CC_DLTUmode: code = ARM_CC;
16916
16917     dominance:
16918       gcc_assert (comp_code == EQ || comp_code == NE);
16919
16920       if (comp_code == EQ)
16921         return ARM_INVERSE_CONDITION_CODE (code);
16922       return code;
16923
16924     case CC_NOOVmode:
16925       switch (comp_code)
16926         {
16927         case NE: return ARM_NE;
16928         case EQ: return ARM_EQ;
16929         case GE: return ARM_PL;
16930         case LT: return ARM_MI;
16931         default: gcc_unreachable ();
16932         }
16933
16934     case CC_Zmode:
16935       switch (comp_code)
16936         {
16937         case NE: return ARM_NE;
16938         case EQ: return ARM_EQ;
16939         default: gcc_unreachable ();
16940         }
16941
16942     case CC_Nmode:
16943       switch (comp_code)
16944         {
16945         case NE: return ARM_MI;
16946         case EQ: return ARM_PL;
16947         default: gcc_unreachable ();
16948         }
16949
16950     case CCFPEmode:
16951     case CCFPmode:
16952       /* These encodings assume that AC=1 in the FPA system control
16953          byte.  This allows us to handle all cases except UNEQ and
16954          LTGT.  */
16955       switch (comp_code)
16956         {
16957         case GE: return ARM_GE;
16958         case GT: return ARM_GT;
16959         case LE: return ARM_LS;
16960         case LT: return ARM_MI;
16961         case NE: return ARM_NE;
16962         case EQ: return ARM_EQ;
16963         case ORDERED: return ARM_VC;
16964         case UNORDERED: return ARM_VS;
16965         case UNLT: return ARM_LT;
16966         case UNLE: return ARM_LE;
16967         case UNGT: return ARM_HI;
16968         case UNGE: return ARM_PL;
16969           /* UNEQ and LTGT do not have a representation.  */
16970         case UNEQ: /* Fall through.  */
16971         case LTGT: /* Fall through.  */
16972         default: gcc_unreachable ();
16973         }
16974
16975     case CC_SWPmode:
16976       switch (comp_code)
16977         {
16978         case NE: return ARM_NE;
16979         case EQ: return ARM_EQ;
16980         case GE: return ARM_LE;
16981         case GT: return ARM_LT;
16982         case LE: return ARM_GE;
16983         case LT: return ARM_GT;
16984         case GEU: return ARM_LS;
16985         case GTU: return ARM_CC;
16986         case LEU: return ARM_CS;
16987         case LTU: return ARM_HI;
16988         default: gcc_unreachable ();
16989         }
16990
16991     case CC_Cmode:
16992       switch (comp_code)
16993         {
16994         case LTU: return ARM_CS;
16995         case GEU: return ARM_CC;
16996         default: gcc_unreachable ();
16997         }
16998
16999     case CC_CZmode:
17000       switch (comp_code)
17001         {
17002         case NE: return ARM_NE;
17003         case EQ: return ARM_EQ;
17004         case GEU: return ARM_CS;
17005         case GTU: return ARM_HI;
17006         case LEU: return ARM_LS;
17007         case LTU: return ARM_CC;
17008         default: gcc_unreachable ();
17009         }
17010
17011     case CC_NCVmode:
17012       switch (comp_code)
17013         {
17014         case GE: return ARM_GE;
17015         case LT: return ARM_LT;
17016         case GEU: return ARM_CS;
17017         case LTU: return ARM_CC;
17018         default: gcc_unreachable ();
17019         }
17020
17021     case CCmode:
17022       switch (comp_code)
17023         {
17024         case NE: return ARM_NE;
17025         case EQ: return ARM_EQ;
17026         case GE: return ARM_GE;
17027         case GT: return ARM_GT;
17028         case LE: return ARM_LE;
17029         case LT: return ARM_LT;
17030         case GEU: return ARM_CS;
17031         case GTU: return ARM_HI;
17032         case LEU: return ARM_LS;
17033         case LTU: return ARM_CC;
17034         default: gcc_unreachable ();
17035         }
17036
17037     default: gcc_unreachable ();
17038     }
17039 }
17040
17041 /* Tell arm_asm_output_opcode to output IT blocks for conditionally executed
17042    instructions.  */
17043 void
17044 thumb2_final_prescan_insn (rtx insn)
17045 {
17046   rtx first_insn = insn;
17047   rtx body = PATTERN (insn);
17048   rtx predicate;
17049   enum arm_cond_code code;
17050   int n;
17051   int mask;
17052
17053   /* Remove the previous insn from the count of insns to be output.  */
17054   if (arm_condexec_count)
17055       arm_condexec_count--;
17056
17057   /* Nothing to do if we are already inside a conditional block.  */
17058   if (arm_condexec_count)
17059     return;
17060
17061   if (GET_CODE (body) != COND_EXEC)
17062     return;
17063
17064   /* Conditional jumps are implemented directly.  */
17065   if (GET_CODE (insn) == JUMP_INSN)
17066     return;
17067
17068   predicate = COND_EXEC_TEST (body);
17069   arm_current_cc = get_arm_condition_code (predicate);
17070
17071   n = get_attr_ce_count (insn);
17072   arm_condexec_count = 1;
17073   arm_condexec_mask = (1 << n) - 1;
17074   arm_condexec_masklen = n;
17075   /* See if subsequent instructions can be combined into the same block.  */
17076   for (;;)
17077     {
17078       insn = next_nonnote_insn (insn);
17079
17080       /* Jumping into the middle of an IT block is illegal, so a label or
17081          barrier terminates the block.  */
17082       if (GET_CODE (insn) != INSN && GET_CODE(insn) != JUMP_INSN)
17083         break;
17084
17085       body = PATTERN (insn);
17086       /* USE and CLOBBER aren't really insns, so just skip them.  */
17087       if (GET_CODE (body) == USE
17088           || GET_CODE (body) == CLOBBER)
17089         continue;
17090
17091       /* ??? Recognize conditional jumps, and combine them with IT blocks.  */
17092       if (GET_CODE (body) != COND_EXEC)
17093         break;
17094       /* Allow up to 4 conditionally executed instructions in a block.  */
17095       n = get_attr_ce_count (insn);
17096       if (arm_condexec_masklen + n > 4)
17097         break;
17098
17099       predicate = COND_EXEC_TEST (body);
17100       code = get_arm_condition_code (predicate);
17101       mask = (1 << n) - 1;
17102       if (arm_current_cc == code)
17103         arm_condexec_mask |= (mask << arm_condexec_masklen);
17104       else if (arm_current_cc != ARM_INVERSE_CONDITION_CODE(code))
17105         break;
17106
17107       arm_condexec_count++;
17108       arm_condexec_masklen += n;
17109
17110       /* A jump must be the last instruction in a conditional block.  */
17111       if (GET_CODE(insn) == JUMP_INSN)
17112         break;
17113     }
17114   /* Restore recog_data (getting the attributes of other insns can
17115      destroy this array, but final.c assumes that it remains intact
17116      across this call).  */
17117   extract_constrain_insn_cached (first_insn);
17118 }
17119
17120 void
17121 arm_final_prescan_insn (rtx insn)
17122 {
17123   /* BODY will hold the body of INSN.  */
17124   rtx body = PATTERN (insn);
17125
17126   /* This will be 1 if trying to repeat the trick, and things need to be
17127      reversed if it appears to fail.  */
17128   int reverse = 0;
17129
17130   /* If we start with a return insn, we only succeed if we find another one.  */
17131   int seeking_return = 0;
17132
17133   /* START_INSN will hold the insn from where we start looking.  This is the
17134      first insn after the following code_label if REVERSE is true.  */
17135   rtx start_insn = insn;
17136
17137   /* If in state 4, check if the target branch is reached, in order to
17138      change back to state 0.  */
17139   if (arm_ccfsm_state == 4)
17140     {
17141       if (insn == arm_target_insn)
17142         {
17143           arm_target_insn = NULL;
17144           arm_ccfsm_state = 0;
17145         }
17146       return;
17147     }
17148
17149   /* If in state 3, it is possible to repeat the trick, if this insn is an
17150      unconditional branch to a label, and immediately following this branch
17151      is the previous target label which is only used once, and the label this
17152      branch jumps to is not too far off.  */
17153   if (arm_ccfsm_state == 3)
17154     {
17155       if (simplejump_p (insn))
17156         {
17157           start_insn = next_nonnote_insn (start_insn);
17158           if (GET_CODE (start_insn) == BARRIER)
17159             {
17160               /* XXX Isn't this always a barrier?  */
17161               start_insn = next_nonnote_insn (start_insn);
17162             }
17163           if (GET_CODE (start_insn) == CODE_LABEL
17164               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
17165               && LABEL_NUSES (start_insn) == 1)
17166             reverse = TRUE;
17167           else
17168             return;
17169         }
17170       else if (GET_CODE (body) == RETURN)
17171         {
17172           start_insn = next_nonnote_insn (start_insn);
17173           if (GET_CODE (start_insn) == BARRIER)
17174             start_insn = next_nonnote_insn (start_insn);
17175           if (GET_CODE (start_insn) == CODE_LABEL
17176               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
17177               && LABEL_NUSES (start_insn) == 1)
17178             {
17179               reverse = TRUE;
17180               seeking_return = 1;
17181             }
17182           else
17183             return;
17184         }
17185       else
17186         return;
17187     }
17188
17189   gcc_assert (!arm_ccfsm_state || reverse);
17190   if (GET_CODE (insn) != JUMP_INSN)
17191     return;
17192
17193   /* This jump might be paralleled with a clobber of the condition codes
17194      the jump should always come first */
17195   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
17196     body = XVECEXP (body, 0, 0);
17197
17198   if (reverse
17199       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
17200           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
17201     {
17202       int insns_skipped;
17203       int fail = FALSE, succeed = FALSE;
17204       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
17205       int then_not_else = TRUE;
17206       rtx this_insn = start_insn, label = 0;
17207
17208       /* Register the insn jumped to.  */
17209       if (reverse)
17210         {
17211           if (!seeking_return)
17212             label = XEXP (SET_SRC (body), 0);
17213         }
17214       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
17215         label = XEXP (XEXP (SET_SRC (body), 1), 0);
17216       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
17217         {
17218           label = XEXP (XEXP (SET_SRC (body), 2), 0);
17219           then_not_else = FALSE;
17220         }
17221       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
17222         seeking_return = 1;
17223       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
17224         {
17225           seeking_return = 1;
17226           then_not_else = FALSE;
17227         }
17228       else
17229         gcc_unreachable ();
17230
17231       /* See how many insns this branch skips, and what kind of insns.  If all
17232          insns are okay, and the label or unconditional branch to the same
17233          label is not too far away, succeed.  */
17234       for (insns_skipped = 0;
17235            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
17236         {
17237           rtx scanbody;
17238
17239           this_insn = next_nonnote_insn (this_insn);
17240           if (!this_insn)
17241             break;
17242
17243           switch (GET_CODE (this_insn))
17244             {
17245             case CODE_LABEL:
17246               /* Succeed if it is the target label, otherwise fail since
17247                  control falls in from somewhere else.  */
17248               if (this_insn == label)
17249                 {
17250                   arm_ccfsm_state = 1;
17251                   succeed = TRUE;
17252                 }
17253               else
17254                 fail = TRUE;
17255               break;
17256
17257             case BARRIER:
17258               /* Succeed if the following insn is the target label.
17259                  Otherwise fail.
17260                  If return insns are used then the last insn in a function
17261                  will be a barrier.  */
17262               this_insn = next_nonnote_insn (this_insn);
17263               if (this_insn && this_insn == label)
17264                 {
17265                   arm_ccfsm_state = 1;
17266                   succeed = TRUE;
17267                 }
17268               else
17269                 fail = TRUE;
17270               break;
17271
17272             case CALL_INSN:
17273               /* The AAPCS says that conditional calls should not be
17274                  used since they make interworking inefficient (the
17275                  linker can't transform BL<cond> into BLX).  That's
17276                  only a problem if the machine has BLX.  */
17277               if (arm_arch5)
17278                 {
17279                   fail = TRUE;
17280                   break;
17281                 }
17282
17283               /* Succeed if the following insn is the target label, or
17284                  if the following two insns are a barrier and the
17285                  target label.  */
17286               this_insn = next_nonnote_insn (this_insn);
17287               if (this_insn && GET_CODE (this_insn) == BARRIER)
17288                 this_insn = next_nonnote_insn (this_insn);
17289
17290               if (this_insn && this_insn == label
17291                   && insns_skipped < max_insns_skipped)
17292                 {
17293                   arm_ccfsm_state = 1;
17294                   succeed = TRUE;
17295                 }
17296               else
17297                 fail = TRUE;
17298               break;
17299
17300             case JUMP_INSN:
17301               /* If this is an unconditional branch to the same label, succeed.
17302                  If it is to another label, do nothing.  If it is conditional,
17303                  fail.  */
17304               /* XXX Probably, the tests for SET and the PC are
17305                  unnecessary.  */
17306
17307               scanbody = PATTERN (this_insn);
17308               if (GET_CODE (scanbody) == SET
17309                   && GET_CODE (SET_DEST (scanbody)) == PC)
17310                 {
17311                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
17312                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
17313                     {
17314                       arm_ccfsm_state = 2;
17315                       succeed = TRUE;
17316                     }
17317                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
17318                     fail = TRUE;
17319                 }
17320               /* Fail if a conditional return is undesirable (e.g. on a
17321                  StrongARM), but still allow this if optimizing for size.  */
17322               else if (GET_CODE (scanbody) == RETURN
17323                        && !use_return_insn (TRUE, NULL)
17324                        && !optimize_size)
17325                 fail = TRUE;
17326               else if (GET_CODE (scanbody) == RETURN
17327                        && seeking_return)
17328                 {
17329                   arm_ccfsm_state = 2;
17330                   succeed = TRUE;
17331                 }
17332               else if (GET_CODE (scanbody) == PARALLEL)
17333                 {
17334                   switch (get_attr_conds (this_insn))
17335                     {
17336                     case CONDS_NOCOND:
17337                       break;
17338                     default:
17339                       fail = TRUE;
17340                       break;
17341                     }
17342                 }
17343               else
17344                 fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
17345
17346               break;
17347
17348             case INSN:
17349               /* Instructions using or affecting the condition codes make it
17350                  fail.  */
17351               scanbody = PATTERN (this_insn);
17352               if (!(GET_CODE (scanbody) == SET
17353                     || GET_CODE (scanbody) == PARALLEL)
17354                   || get_attr_conds (this_insn) != CONDS_NOCOND)
17355                 fail = TRUE;
17356
17357               /* A conditional cirrus instruction must be followed by
17358                  a non Cirrus instruction.  However, since we
17359                  conditionalize instructions in this function and by
17360                  the time we get here we can't add instructions
17361                  (nops), because shorten_branches() has already been
17362                  called, we will disable conditionalizing Cirrus
17363                  instructions to be safe.  */
17364               if (GET_CODE (scanbody) != USE
17365                   && GET_CODE (scanbody) != CLOBBER
17366                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
17367                 fail = TRUE;
17368               break;
17369
17370             default:
17371               break;
17372             }
17373         }
17374       if (succeed)
17375         {
17376           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
17377             arm_target_label = CODE_LABEL_NUMBER (label);
17378           else
17379             {
17380               gcc_assert (seeking_return || arm_ccfsm_state == 2);
17381
17382               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
17383                 {
17384                   this_insn = next_nonnote_insn (this_insn);
17385                   gcc_assert (!this_insn
17386                               || (GET_CODE (this_insn) != BARRIER
17387                                   && GET_CODE (this_insn) != CODE_LABEL));
17388                 }
17389               if (!this_insn)
17390                 {
17391                   /* Oh, dear! we ran off the end.. give up.  */
17392                   extract_constrain_insn_cached (insn);
17393                   arm_ccfsm_state = 0;
17394                   arm_target_insn = NULL;
17395                   return;
17396                 }
17397               arm_target_insn = this_insn;
17398             }
17399
17400           /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
17401              what it was.  */
17402           if (!reverse)
17403             arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body), 0));
17404
17405           if (reverse || then_not_else)
17406             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
17407         }
17408
17409       /* Restore recog_data (getting the attributes of other insns can
17410          destroy this array, but final.c assumes that it remains intact
17411          across this call.  */
17412       extract_constrain_insn_cached (insn);
17413     }
17414 }
17415
17416 /* Output IT instructions.  */
17417 void
17418 thumb2_asm_output_opcode (FILE * stream)
17419 {
17420   char buff[5];
17421   int n;
17422
17423   if (arm_condexec_mask)
17424     {
17425       for (n = 0; n < arm_condexec_masklen; n++)
17426         buff[n] = (arm_condexec_mask & (1 << n)) ? 't' : 'e';
17427       buff[n] = 0;
17428       asm_fprintf(stream, "i%s\t%s\n\t", buff,
17429                   arm_condition_codes[arm_current_cc]);
17430       arm_condexec_mask = 0;
17431     }
17432 }
17433
17434 /* Returns true if REGNO is a valid register
17435    for holding a quantity of type MODE.  */
17436 int
17437 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
17438 {
17439   if (GET_MODE_CLASS (mode) == MODE_CC)
17440     return (regno == CC_REGNUM
17441             || (TARGET_HARD_FLOAT && TARGET_VFP
17442                 && regno == VFPCC_REGNUM));
17443
17444   if (TARGET_THUMB1)
17445     /* For the Thumb we only allow values bigger than SImode in
17446        registers 0 - 6, so that there is always a second low
17447        register available to hold the upper part of the value.
17448        We probably we ought to ensure that the register is the
17449        start of an even numbered register pair.  */
17450     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
17451
17452   if (TARGET_HARD_FLOAT && TARGET_MAVERICK
17453       && IS_CIRRUS_REGNUM (regno))
17454     /* We have outlawed SI values in Cirrus registers because they
17455        reside in the lower 32 bits, but SF values reside in the
17456        upper 32 bits.  This causes gcc all sorts of grief.  We can't
17457        even split the registers into pairs because Cirrus SI values
17458        get sign extended to 64bits-- aldyh.  */
17459     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
17460
17461   if (TARGET_HARD_FLOAT && TARGET_VFP
17462       && IS_VFP_REGNUM (regno))
17463     {
17464       if (mode == SFmode || mode == SImode)
17465         return VFP_REGNO_OK_FOR_SINGLE (regno);
17466
17467       if (mode == DFmode)
17468         return VFP_REGNO_OK_FOR_DOUBLE (regno);
17469
17470       /* VFP registers can hold HFmode values, but there is no point in
17471          putting them there unless we have hardware conversion insns. */
17472       if (mode == HFmode)
17473         return TARGET_FP16 && VFP_REGNO_OK_FOR_SINGLE (regno);
17474
17475       if (TARGET_NEON)
17476         return (VALID_NEON_DREG_MODE (mode) && VFP_REGNO_OK_FOR_DOUBLE (regno))
17477                || (VALID_NEON_QREG_MODE (mode)
17478                    && NEON_REGNO_OK_FOR_QUAD (regno))
17479                || (mode == TImode && NEON_REGNO_OK_FOR_NREGS (regno, 2))
17480                || (mode == EImode && NEON_REGNO_OK_FOR_NREGS (regno, 3))
17481                || (mode == OImode && NEON_REGNO_OK_FOR_NREGS (regno, 4))
17482                || (mode == CImode && NEON_REGNO_OK_FOR_NREGS (regno, 6))
17483                || (mode == XImode && NEON_REGNO_OK_FOR_NREGS (regno, 8));
17484
17485       return FALSE;
17486     }
17487
17488   if (TARGET_REALLY_IWMMXT)
17489     {
17490       if (IS_IWMMXT_GR_REGNUM (regno))
17491         return mode == SImode;
17492
17493       if (IS_IWMMXT_REGNUM (regno))
17494         return VALID_IWMMXT_REG_MODE (mode);
17495     }
17496   
17497   /* We allow almost any value to be stored in the general registers.
17498      Restrict doubleword quantities to even register pairs so that we can
17499      use ldrd.  Do not allow very large Neon structure opaque modes in
17500      general registers; they would use too many.  */
17501   if (regno <= LAST_ARM_REGNUM)
17502     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0)
17503       && ARM_NUM_REGS (mode) <= 4;
17504
17505   if (regno == FRAME_POINTER_REGNUM
17506       || regno == ARG_POINTER_REGNUM)
17507     /* We only allow integers in the fake hard registers.  */
17508     return GET_MODE_CLASS (mode) == MODE_INT;
17509
17510   /* The only registers left are the FPA registers
17511      which we only allow to hold FP values.  */
17512   return (TARGET_HARD_FLOAT && TARGET_FPA
17513           && GET_MODE_CLASS (mode) == MODE_FLOAT
17514           && regno >= FIRST_FPA_REGNUM
17515           && regno <= LAST_FPA_REGNUM);
17516 }
17517
17518 /* For efficiency and historical reasons LO_REGS, HI_REGS and CC_REGS are
17519    not used in arm mode.  */
17520
17521 enum reg_class
17522 arm_regno_class (int regno)
17523 {
17524   if (TARGET_THUMB1)
17525     {
17526       if (regno == STACK_POINTER_REGNUM)
17527         return STACK_REG;
17528       if (regno == CC_REGNUM)
17529         return CC_REG;
17530       if (regno < 8)
17531         return LO_REGS;
17532       return HI_REGS;
17533     }
17534
17535   if (TARGET_THUMB2 && regno < 8)
17536     return LO_REGS;
17537
17538   if (   regno <= LAST_ARM_REGNUM
17539       || regno == FRAME_POINTER_REGNUM
17540       || regno == ARG_POINTER_REGNUM)
17541     return TARGET_THUMB2 ? HI_REGS : GENERAL_REGS;
17542
17543   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
17544     return TARGET_THUMB2 ? CC_REG : NO_REGS;
17545
17546   if (IS_CIRRUS_REGNUM (regno))
17547     return CIRRUS_REGS;
17548
17549   if (IS_VFP_REGNUM (regno))
17550     {
17551       if (regno <= D7_VFP_REGNUM)
17552         return VFP_D0_D7_REGS;
17553       else if (regno <= LAST_LO_VFP_REGNUM)
17554         return VFP_LO_REGS;
17555       else
17556         return VFP_HI_REGS;
17557     }
17558
17559   if (IS_IWMMXT_REGNUM (regno))
17560     return IWMMXT_REGS;
17561
17562   if (IS_IWMMXT_GR_REGNUM (regno))
17563     return IWMMXT_GR_REGS;
17564
17565   return FPA_REGS;
17566 }
17567
17568 /* Handle a special case when computing the offset
17569    of an argument from the frame pointer.  */
17570 int
17571 arm_debugger_arg_offset (int value, rtx addr)
17572 {
17573   rtx insn;
17574
17575   /* We are only interested if dbxout_parms() failed to compute the offset.  */
17576   if (value != 0)
17577     return 0;
17578
17579   /* We can only cope with the case where the address is held in a register.  */
17580   if (GET_CODE (addr) != REG)
17581     return 0;
17582
17583   /* If we are using the frame pointer to point at the argument, then
17584      an offset of 0 is correct.  */
17585   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
17586     return 0;
17587
17588   /* If we are using the stack pointer to point at the
17589      argument, then an offset of 0 is correct.  */
17590   /* ??? Check this is consistent with thumb2 frame layout.  */
17591   if ((TARGET_THUMB || !frame_pointer_needed)
17592       && REGNO (addr) == SP_REGNUM)
17593     return 0;
17594
17595   /* Oh dear.  The argument is pointed to by a register rather
17596      than being held in a register, or being stored at a known
17597      offset from the frame pointer.  Since GDB only understands
17598      those two kinds of argument we must translate the address
17599      held in the register into an offset from the frame pointer.
17600      We do this by searching through the insns for the function
17601      looking to see where this register gets its value.  If the
17602      register is initialized from the frame pointer plus an offset
17603      then we are in luck and we can continue, otherwise we give up.
17604
17605      This code is exercised by producing debugging information
17606      for a function with arguments like this:
17607
17608            double func (double a, double b, int c, double d) {return d;}
17609
17610      Without this code the stab for parameter 'd' will be set to
17611      an offset of 0 from the frame pointer, rather than 8.  */
17612
17613   /* The if() statement says:
17614
17615      If the insn is a normal instruction
17616      and if the insn is setting the value in a register
17617      and if the register being set is the register holding the address of the argument
17618      and if the address is computing by an addition
17619      that involves adding to a register
17620      which is the frame pointer
17621      a constant integer
17622
17623      then...  */
17624
17625   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
17626     {
17627       if (   GET_CODE (insn) == INSN
17628           && GET_CODE (PATTERN (insn)) == SET
17629           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
17630           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
17631           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
17632           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
17633           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
17634              )
17635         {
17636           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
17637
17638           break;
17639         }
17640     }
17641
17642   if (value == 0)
17643     {
17644       debug_rtx (addr);
17645       warning (0, "unable to compute real location of stacked parameter");
17646       value = 8; /* XXX magic hack */
17647     }
17648
17649   return value;
17650 }
17651 \f
17652 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
17653   do                                                                    \
17654     {                                                                   \
17655       if ((MASK) & insn_flags)                                          \
17656         add_builtin_function ((NAME), (TYPE), (CODE),                   \
17657                              BUILT_IN_MD, NULL, NULL_TREE);             \
17658     }                                                                   \
17659   while (0)
17660
17661 struct builtin_description
17662 {
17663   const unsigned int       mask;
17664   const enum insn_code     icode;
17665   const char * const       name;
17666   const enum arm_builtins  code;
17667   const enum rtx_code      comparison;
17668   const unsigned int       flag;
17669 };
17670
17671 static const struct builtin_description bdesc_2arg[] =
17672 {
17673 #define IWMMXT_BUILTIN(code, string, builtin) \
17674   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
17675     ARM_BUILTIN_##builtin, UNKNOWN, 0 },
17676
17677   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
17678   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
17679   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
17680   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
17681   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
17682   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
17683   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
17684   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
17685   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
17686   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
17687   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
17688   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
17689   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
17690   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
17691   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
17692   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
17693   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
17694   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
17695   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
17696   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
17697   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
17698   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
17699   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
17700   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
17701   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
17702   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
17703   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
17704   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
17705   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
17706   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
17707   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
17708   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
17709   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
17710   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
17711   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
17712   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
17713   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
17714   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
17715   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
17716   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
17717   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
17718   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
17719   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
17720   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
17721   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
17722   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
17723   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
17724   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
17725   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
17726   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
17727   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
17728   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
17729   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
17730   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
17731   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
17732   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
17733   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
17734   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
17735
17736 #define IWMMXT_BUILTIN2(code, builtin) \
17737   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, UNKNOWN, 0 },
17738
17739   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
17740   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
17741   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
17742   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
17743   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
17744   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
17745   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
17746   IWMMXT_BUILTIN2 (ashlv4hi3_iwmmxt, WSLLHI)
17747   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
17748   IWMMXT_BUILTIN2 (ashlv2si3_iwmmxt, WSLLWI)
17749   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
17750   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
17751   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
17752   IWMMXT_BUILTIN2 (lshrv4hi3_iwmmxt, WSRLHI)
17753   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
17754   IWMMXT_BUILTIN2 (lshrv2si3_iwmmxt, WSRLWI)
17755   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
17756   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
17757   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
17758   IWMMXT_BUILTIN2 (ashrv4hi3_iwmmxt, WSRAHI)
17759   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
17760   IWMMXT_BUILTIN2 (ashrv2si3_iwmmxt, WSRAWI)
17761   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
17762   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
17763   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
17764   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
17765   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
17766   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
17767   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
17768   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
17769   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
17770   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
17771 };
17772
17773 static const struct builtin_description bdesc_1arg[] =
17774 {
17775   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
17776   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
17777   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
17778   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
17779   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
17780   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
17781   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
17782   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
17783   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
17784   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
17785   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
17786   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
17787   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
17788   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
17789   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
17790   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
17791   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
17792   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
17793 };
17794
17795 /* Set up all the iWMMXt builtins.  This is
17796    not called if TARGET_IWMMXT is zero.  */
17797
17798 static void
17799 arm_init_iwmmxt_builtins (void)
17800 {
17801   const struct builtin_description * d;
17802   size_t i;
17803   tree endlink = void_list_node;
17804
17805   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
17806   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
17807   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
17808
17809   tree int_ftype_int
17810     = build_function_type (integer_type_node,
17811                            tree_cons (NULL_TREE, integer_type_node, endlink));
17812   tree v8qi_ftype_v8qi_v8qi_int
17813     = build_function_type (V8QI_type_node,
17814                            tree_cons (NULL_TREE, V8QI_type_node,
17815                                       tree_cons (NULL_TREE, V8QI_type_node,
17816                                                  tree_cons (NULL_TREE,
17817                                                             integer_type_node,
17818                                                             endlink))));
17819   tree v4hi_ftype_v4hi_int
17820     = build_function_type (V4HI_type_node,
17821                            tree_cons (NULL_TREE, V4HI_type_node,
17822                                       tree_cons (NULL_TREE, integer_type_node,
17823                                                  endlink)));
17824   tree v2si_ftype_v2si_int
17825     = build_function_type (V2SI_type_node,
17826                            tree_cons (NULL_TREE, V2SI_type_node,
17827                                       tree_cons (NULL_TREE, integer_type_node,
17828                                                  endlink)));
17829   tree v2si_ftype_di_di
17830     = build_function_type (V2SI_type_node,
17831                            tree_cons (NULL_TREE, long_long_integer_type_node,
17832                                       tree_cons (NULL_TREE, long_long_integer_type_node,
17833                                                  endlink)));
17834   tree di_ftype_di_int
17835     = build_function_type (long_long_integer_type_node,
17836                            tree_cons (NULL_TREE, long_long_integer_type_node,
17837                                       tree_cons (NULL_TREE, integer_type_node,
17838                                                  endlink)));
17839   tree di_ftype_di_int_int
17840     = build_function_type (long_long_integer_type_node,
17841                            tree_cons (NULL_TREE, long_long_integer_type_node,
17842                                       tree_cons (NULL_TREE, integer_type_node,
17843                                                  tree_cons (NULL_TREE,
17844                                                             integer_type_node,
17845                                                             endlink))));
17846   tree int_ftype_v8qi
17847     = build_function_type (integer_type_node,
17848                            tree_cons (NULL_TREE, V8QI_type_node,
17849                                       endlink));
17850   tree int_ftype_v4hi
17851     = build_function_type (integer_type_node,
17852                            tree_cons (NULL_TREE, V4HI_type_node,
17853                                       endlink));
17854   tree int_ftype_v2si
17855     = build_function_type (integer_type_node,
17856                            tree_cons (NULL_TREE, V2SI_type_node,
17857                                       endlink));
17858   tree int_ftype_v8qi_int
17859     = build_function_type (integer_type_node,
17860                            tree_cons (NULL_TREE, V8QI_type_node,
17861                                       tree_cons (NULL_TREE, integer_type_node,
17862                                                  endlink)));
17863   tree int_ftype_v4hi_int
17864     = build_function_type (integer_type_node,
17865                            tree_cons (NULL_TREE, V4HI_type_node,
17866                                       tree_cons (NULL_TREE, integer_type_node,
17867                                                  endlink)));
17868   tree int_ftype_v2si_int
17869     = build_function_type (integer_type_node,
17870                            tree_cons (NULL_TREE, V2SI_type_node,
17871                                       tree_cons (NULL_TREE, integer_type_node,
17872                                                  endlink)));
17873   tree v8qi_ftype_v8qi_int_int
17874     = build_function_type (V8QI_type_node,
17875                            tree_cons (NULL_TREE, V8QI_type_node,
17876                                       tree_cons (NULL_TREE, integer_type_node,
17877                                                  tree_cons (NULL_TREE,
17878                                                             integer_type_node,
17879                                                             endlink))));
17880   tree v4hi_ftype_v4hi_int_int
17881     = build_function_type (V4HI_type_node,
17882                            tree_cons (NULL_TREE, V4HI_type_node,
17883                                       tree_cons (NULL_TREE, integer_type_node,
17884                                                  tree_cons (NULL_TREE,
17885                                                             integer_type_node,
17886                                                             endlink))));
17887   tree v2si_ftype_v2si_int_int
17888     = build_function_type (V2SI_type_node,
17889                            tree_cons (NULL_TREE, V2SI_type_node,
17890                                       tree_cons (NULL_TREE, integer_type_node,
17891                                                  tree_cons (NULL_TREE,
17892                                                             integer_type_node,
17893                                                             endlink))));
17894   /* Miscellaneous.  */
17895   tree v8qi_ftype_v4hi_v4hi
17896     = build_function_type (V8QI_type_node,
17897                            tree_cons (NULL_TREE, V4HI_type_node,
17898                                       tree_cons (NULL_TREE, V4HI_type_node,
17899                                                  endlink)));
17900   tree v4hi_ftype_v2si_v2si
17901     = build_function_type (V4HI_type_node,
17902                            tree_cons (NULL_TREE, V2SI_type_node,
17903                                       tree_cons (NULL_TREE, V2SI_type_node,
17904                                                  endlink)));
17905   tree v2si_ftype_v4hi_v4hi
17906     = build_function_type (V2SI_type_node,
17907                            tree_cons (NULL_TREE, V4HI_type_node,
17908                                       tree_cons (NULL_TREE, V4HI_type_node,
17909                                                  endlink)));
17910   tree v2si_ftype_v8qi_v8qi
17911     = build_function_type (V2SI_type_node,
17912                            tree_cons (NULL_TREE, V8QI_type_node,
17913                                       tree_cons (NULL_TREE, V8QI_type_node,
17914                                                  endlink)));
17915   tree v4hi_ftype_v4hi_di
17916     = build_function_type (V4HI_type_node,
17917                            tree_cons (NULL_TREE, V4HI_type_node,
17918                                       tree_cons (NULL_TREE,
17919                                                  long_long_integer_type_node,
17920                                                  endlink)));
17921   tree v2si_ftype_v2si_di
17922     = build_function_type (V2SI_type_node,
17923                            tree_cons (NULL_TREE, V2SI_type_node,
17924                                       tree_cons (NULL_TREE,
17925                                                  long_long_integer_type_node,
17926                                                  endlink)));
17927   tree void_ftype_int_int
17928     = build_function_type (void_type_node,
17929                            tree_cons (NULL_TREE, integer_type_node,
17930                                       tree_cons (NULL_TREE, integer_type_node,
17931                                                  endlink)));
17932   tree di_ftype_void
17933     = build_function_type (long_long_unsigned_type_node, endlink);
17934   tree di_ftype_v8qi
17935     = build_function_type (long_long_integer_type_node,
17936                            tree_cons (NULL_TREE, V8QI_type_node,
17937                                       endlink));
17938   tree di_ftype_v4hi
17939     = build_function_type (long_long_integer_type_node,
17940                            tree_cons (NULL_TREE, V4HI_type_node,
17941                                       endlink));
17942   tree di_ftype_v2si
17943     = build_function_type (long_long_integer_type_node,
17944                            tree_cons (NULL_TREE, V2SI_type_node,
17945                                       endlink));
17946   tree v2si_ftype_v4hi
17947     = build_function_type (V2SI_type_node,
17948                            tree_cons (NULL_TREE, V4HI_type_node,
17949                                       endlink));
17950   tree v4hi_ftype_v8qi
17951     = build_function_type (V4HI_type_node,
17952                            tree_cons (NULL_TREE, V8QI_type_node,
17953                                       endlink));
17954
17955   tree di_ftype_di_v4hi_v4hi
17956     = build_function_type (long_long_unsigned_type_node,
17957                            tree_cons (NULL_TREE,
17958                                       long_long_unsigned_type_node,
17959                                       tree_cons (NULL_TREE, V4HI_type_node,
17960                                                  tree_cons (NULL_TREE,
17961                                                             V4HI_type_node,
17962                                                             endlink))));
17963
17964   tree di_ftype_v4hi_v4hi
17965     = build_function_type (long_long_unsigned_type_node,
17966                            tree_cons (NULL_TREE, V4HI_type_node,
17967                                       tree_cons (NULL_TREE, V4HI_type_node,
17968                                                  endlink)));
17969
17970   /* Normal vector binops.  */
17971   tree v8qi_ftype_v8qi_v8qi
17972     = build_function_type (V8QI_type_node,
17973                            tree_cons (NULL_TREE, V8QI_type_node,
17974                                       tree_cons (NULL_TREE, V8QI_type_node,
17975                                                  endlink)));
17976   tree v4hi_ftype_v4hi_v4hi
17977     = build_function_type (V4HI_type_node,
17978                            tree_cons (NULL_TREE, V4HI_type_node,
17979                                       tree_cons (NULL_TREE, V4HI_type_node,
17980                                                  endlink)));
17981   tree v2si_ftype_v2si_v2si
17982     = build_function_type (V2SI_type_node,
17983                            tree_cons (NULL_TREE, V2SI_type_node,
17984                                       tree_cons (NULL_TREE, V2SI_type_node,
17985                                                  endlink)));
17986   tree di_ftype_di_di
17987     = build_function_type (long_long_unsigned_type_node,
17988                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
17989                                       tree_cons (NULL_TREE,
17990                                                  long_long_unsigned_type_node,
17991                                                  endlink)));
17992
17993   /* Add all builtins that are more or less simple operations on two
17994      operands.  */
17995   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
17996     {
17997       /* Use one of the operands; the target can have a different mode for
17998          mask-generating compares.  */
17999       enum machine_mode mode;
18000       tree type;
18001
18002       if (d->name == 0)
18003         continue;
18004
18005       mode = insn_data[d->icode].operand[1].mode;
18006
18007       switch (mode)
18008         {
18009         case V8QImode:
18010           type = v8qi_ftype_v8qi_v8qi;
18011           break;
18012         case V4HImode:
18013           type = v4hi_ftype_v4hi_v4hi;
18014           break;
18015         case V2SImode:
18016           type = v2si_ftype_v2si_v2si;
18017           break;
18018         case DImode:
18019           type = di_ftype_di_di;
18020           break;
18021
18022         default:
18023           gcc_unreachable ();
18024         }
18025
18026       def_mbuiltin (d->mask, d->name, type, d->code);
18027     }
18028
18029   /* Add the remaining MMX insns with somewhat more complicated types.  */
18030   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
18031   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
18032   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
18033
18034   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
18035   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
18036   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
18037   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
18038   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
18039   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
18040
18041   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
18042   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
18043   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
18044   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
18045   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
18046   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
18047
18048   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
18049   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
18050   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
18051   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
18052   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
18053   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
18054
18055   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
18056   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
18057   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
18058   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
18059   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
18060   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
18061
18062   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
18063
18064   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
18065   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
18066   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
18067   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
18068
18069   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
18070   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
18071   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
18072   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
18073   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
18074   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
18075   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
18076   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
18077   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
18078
18079   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
18080   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
18081   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
18082
18083   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
18084   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
18085   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
18086
18087   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
18088   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
18089   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
18090   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
18091   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
18092   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
18093
18094   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
18095   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
18096   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
18097   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
18098   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
18099   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
18100   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
18101   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
18102   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
18103   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
18104   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
18105   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
18106
18107   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
18108   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
18109   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
18110   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
18111
18112   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
18113   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
18114   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
18115   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
18116   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
18117   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
18118   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
18119 }
18120
18121 static void
18122 arm_init_tls_builtins (void)
18123 {
18124   tree ftype, decl;
18125
18126   ftype = build_function_type (ptr_type_node, void_list_node);
18127   decl = add_builtin_function ("__builtin_thread_pointer", ftype,
18128                                ARM_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
18129                                NULL, NULL_TREE);
18130   TREE_NOTHROW (decl) = 1;
18131   TREE_READONLY (decl) = 1;
18132 }
18133
18134 enum neon_builtin_type_bits {
18135   T_V8QI  = 0x0001,
18136   T_V4HI  = 0x0002,
18137   T_V2SI  = 0x0004,
18138   T_V2SF  = 0x0008,
18139   T_DI    = 0x0010,
18140   T_V16QI = 0x0020,
18141   T_V8HI  = 0x0040,
18142   T_V4SI  = 0x0080,
18143   T_V4SF  = 0x0100,
18144   T_V2DI  = 0x0200,
18145   T_TI    = 0x0400,
18146   T_EI    = 0x0800,
18147   T_OI    = 0x1000
18148 };
18149
18150 #define v8qi_UP  T_V8QI
18151 #define v4hi_UP  T_V4HI
18152 #define v2si_UP  T_V2SI
18153 #define v2sf_UP  T_V2SF
18154 #define di_UP    T_DI
18155 #define v16qi_UP T_V16QI
18156 #define v8hi_UP  T_V8HI
18157 #define v4si_UP  T_V4SI
18158 #define v4sf_UP  T_V4SF
18159 #define v2di_UP  T_V2DI
18160 #define ti_UP    T_TI
18161 #define ei_UP    T_EI
18162 #define oi_UP    T_OI
18163
18164 #define UP(X) X##_UP
18165
18166 #define T_MAX 13
18167
18168 typedef enum {
18169   NEON_BINOP,
18170   NEON_TERNOP,
18171   NEON_UNOP,
18172   NEON_GETLANE,
18173   NEON_SETLANE,
18174   NEON_CREATE,
18175   NEON_DUP,
18176   NEON_DUPLANE,
18177   NEON_COMBINE,
18178   NEON_SPLIT,
18179   NEON_LANEMUL,
18180   NEON_LANEMULL,
18181   NEON_LANEMULH,
18182   NEON_LANEMAC,
18183   NEON_SCALARMUL,
18184   NEON_SCALARMULL,
18185   NEON_SCALARMULH,
18186   NEON_SCALARMAC,
18187   NEON_CONVERT,
18188   NEON_FIXCONV,
18189   NEON_SELECT,
18190   NEON_RESULTPAIR,
18191   NEON_REINTERP,
18192   NEON_VTBL,
18193   NEON_VTBX,
18194   NEON_LOAD1,
18195   NEON_LOAD1LANE,
18196   NEON_STORE1,
18197   NEON_STORE1LANE,
18198   NEON_LOADSTRUCT,
18199   NEON_LOADSTRUCTLANE,
18200   NEON_STORESTRUCT,
18201   NEON_STORESTRUCTLANE,
18202   NEON_LOGICBINOP,
18203   NEON_SHIFTINSERT,
18204   NEON_SHIFTIMM,
18205   NEON_SHIFTACC
18206 } neon_itype;
18207
18208 typedef struct {
18209   const char *name;
18210   const neon_itype itype;
18211   const int bits;
18212   const enum insn_code codes[T_MAX];
18213   const unsigned int num_vars;
18214   unsigned int base_fcode;
18215 } neon_builtin_datum;
18216
18217 #define CF(N,X) CODE_FOR_neon_##N##X
18218
18219 #define VAR1(T, N, A) \
18220   #N, NEON_##T, UP (A), { CF (N, A) }, 1, 0
18221 #define VAR2(T, N, A, B) \
18222   #N, NEON_##T, UP (A) | UP (B), { CF (N, A), CF (N, B) }, 2, 0
18223 #define VAR3(T, N, A, B, C) \
18224   #N, NEON_##T, UP (A) | UP (B) | UP (C), \
18225   { CF (N, A), CF (N, B), CF (N, C) }, 3, 0
18226 #define VAR4(T, N, A, B, C, D) \
18227   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D), \
18228   { CF (N, A), CF (N, B), CF (N, C), CF (N, D) }, 4, 0
18229 #define VAR5(T, N, A, B, C, D, E) \
18230   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E), \
18231   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E) }, 5, 0
18232 #define VAR6(T, N, A, B, C, D, E, F) \
18233   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F), \
18234   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F) }, 6, 0
18235 #define VAR7(T, N, A, B, C, D, E, F, G) \
18236   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G), \
18237   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
18238     CF (N, G) }, 7, 0
18239 #define VAR8(T, N, A, B, C, D, E, F, G, H) \
18240   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
18241                 | UP (H), \
18242   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
18243     CF (N, G), CF (N, H) }, 8, 0
18244 #define VAR9(T, N, A, B, C, D, E, F, G, H, I) \
18245   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
18246                 | UP (H) | UP (I), \
18247   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
18248     CF (N, G), CF (N, H), CF (N, I) }, 9, 0
18249 #define VAR10(T, N, A, B, C, D, E, F, G, H, I, J) \
18250   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
18251                 | UP (H) | UP (I) | UP (J), \
18252   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
18253     CF (N, G), CF (N, H), CF (N, I), CF (N, J) }, 10, 0
18254
18255 /* The mode entries in the following table correspond to the "key" type of the
18256    instruction variant, i.e. equivalent to that which would be specified after
18257    the assembler mnemonic, which usually refers to the last vector operand.
18258    (Signed/unsigned/polynomial types are not differentiated between though, and
18259    are all mapped onto the same mode for a given element size.) The modes
18260    listed per instruction should be the same as those defined for that
18261    instruction's pattern in neon.md.
18262    WARNING: Variants should be listed in the same increasing order as
18263    neon_builtin_type_bits.  */
18264
18265 static neon_builtin_datum neon_builtin_data[] =
18266 {
18267   { VAR10 (BINOP, vadd,
18268            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18269   { VAR3 (BINOP, vaddl, v8qi, v4hi, v2si) },
18270   { VAR3 (BINOP, vaddw, v8qi, v4hi, v2si) },
18271   { VAR6 (BINOP, vhadd, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18272   { VAR8 (BINOP, vqadd, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18273   { VAR3 (BINOP, vaddhn, v8hi, v4si, v2di) },
18274   { VAR8 (BINOP, vmul, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18275   { VAR8 (TERNOP, vmla, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18276   { VAR3 (TERNOP, vmlal, v8qi, v4hi, v2si) },
18277   { VAR8 (TERNOP, vmls, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18278   { VAR3 (TERNOP, vmlsl, v8qi, v4hi, v2si) },
18279   { VAR4 (BINOP, vqdmulh, v4hi, v2si, v8hi, v4si) },
18280   { VAR2 (TERNOP, vqdmlal, v4hi, v2si) },
18281   { VAR2 (TERNOP, vqdmlsl, v4hi, v2si) },
18282   { VAR3 (BINOP, vmull, v8qi, v4hi, v2si) },
18283   { VAR2 (SCALARMULL, vmull_n, v4hi, v2si) },
18284   { VAR2 (LANEMULL, vmull_lane, v4hi, v2si) },
18285   { VAR2 (SCALARMULL, vqdmull_n, v4hi, v2si) },
18286   { VAR2 (LANEMULL, vqdmull_lane, v4hi, v2si) },
18287   { VAR4 (SCALARMULH, vqdmulh_n, v4hi, v2si, v8hi, v4si) },
18288   { VAR4 (LANEMULH, vqdmulh_lane, v4hi, v2si, v8hi, v4si) },
18289   { VAR2 (BINOP, vqdmull, v4hi, v2si) },
18290   { VAR8 (BINOP, vshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18291   { VAR8 (BINOP, vqshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18292   { VAR8 (SHIFTIMM, vshr_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18293   { VAR3 (SHIFTIMM, vshrn_n, v8hi, v4si, v2di) },
18294   { VAR3 (SHIFTIMM, vqshrn_n, v8hi, v4si, v2di) },
18295   { VAR3 (SHIFTIMM, vqshrun_n, v8hi, v4si, v2di) },
18296   { VAR8 (SHIFTIMM, vshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18297   { VAR8 (SHIFTIMM, vqshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18298   { VAR8 (SHIFTIMM, vqshlu_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18299   { VAR3 (SHIFTIMM, vshll_n, v8qi, v4hi, v2si) },
18300   { VAR8 (SHIFTACC, vsra_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18301   { VAR10 (BINOP, vsub,
18302            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18303   { VAR3 (BINOP, vsubl, v8qi, v4hi, v2si) },
18304   { VAR3 (BINOP, vsubw, v8qi, v4hi, v2si) },
18305   { VAR8 (BINOP, vqsub, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18306   { VAR6 (BINOP, vhsub, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18307   { VAR3 (BINOP, vsubhn, v8hi, v4si, v2di) },
18308   { VAR8 (BINOP, vceq, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18309   { VAR8 (BINOP, vcge, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18310   { VAR8 (BINOP, vcgt, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18311   { VAR2 (BINOP, vcage, v2sf, v4sf) },
18312   { VAR2 (BINOP, vcagt, v2sf, v4sf) },
18313   { VAR6 (BINOP, vtst, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18314   { VAR8 (BINOP, vabd, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18315   { VAR3 (BINOP, vabdl, v8qi, v4hi, v2si) },
18316   { VAR6 (TERNOP, vaba, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18317   { VAR3 (TERNOP, vabal, v8qi, v4hi, v2si) },
18318   { VAR8 (BINOP, vmax, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18319   { VAR8 (BINOP, vmin, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18320   { VAR4 (BINOP, vpadd, v8qi, v4hi, v2si, v2sf) },
18321   { VAR6 (UNOP, vpaddl, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18322   { VAR6 (BINOP, vpadal, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18323   { VAR4 (BINOP, vpmax, v8qi, v4hi, v2si, v2sf) },
18324   { VAR4 (BINOP, vpmin, v8qi, v4hi, v2si, v2sf) },
18325   { VAR2 (BINOP, vrecps, v2sf, v4sf) },
18326   { VAR2 (BINOP, vrsqrts, v2sf, v4sf) },
18327   { VAR8 (SHIFTINSERT, vsri_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18328   { VAR8 (SHIFTINSERT, vsli_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
18329   { VAR8 (UNOP, vabs, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18330   { VAR6 (UNOP, vqabs, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18331   { VAR8 (UNOP, vneg, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18332   { VAR6 (UNOP, vqneg, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18333   { VAR6 (UNOP, vcls, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18334   { VAR6 (UNOP, vclz, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18335   { VAR2 (UNOP, vcnt, v8qi, v16qi) },
18336   { VAR4 (UNOP, vrecpe, v2si, v2sf, v4si, v4sf) },
18337   { VAR4 (UNOP, vrsqrte, v2si, v2sf, v4si, v4sf) },
18338   { VAR6 (UNOP, vmvn, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
18339   /* FIXME: vget_lane supports more variants than this!  */
18340   { VAR10 (GETLANE, vget_lane,
18341            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18342   { VAR10 (SETLANE, vset_lane,
18343            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18344   { VAR5 (CREATE, vcreate, v8qi, v4hi, v2si, v2sf, di) },
18345   { VAR10 (DUP, vdup_n,
18346            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18347   { VAR10 (DUPLANE, vdup_lane,
18348            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18349   { VAR5 (COMBINE, vcombine, v8qi, v4hi, v2si, v2sf, di) },
18350   { VAR5 (SPLIT, vget_high, v16qi, v8hi, v4si, v4sf, v2di) },
18351   { VAR5 (SPLIT, vget_low, v16qi, v8hi, v4si, v4sf, v2di) },
18352   { VAR3 (UNOP, vmovn, v8hi, v4si, v2di) },
18353   { VAR3 (UNOP, vqmovn, v8hi, v4si, v2di) },
18354   { VAR3 (UNOP, vqmovun, v8hi, v4si, v2di) },
18355   { VAR3 (UNOP, vmovl, v8qi, v4hi, v2si) },
18356   { VAR6 (LANEMUL, vmul_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18357   { VAR6 (LANEMAC, vmla_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18358   { VAR2 (LANEMAC, vmlal_lane, v4hi, v2si) },
18359   { VAR2 (LANEMAC, vqdmlal_lane, v4hi, v2si) },
18360   { VAR6 (LANEMAC, vmls_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18361   { VAR2 (LANEMAC, vmlsl_lane, v4hi, v2si) },
18362   { VAR2 (LANEMAC, vqdmlsl_lane, v4hi, v2si) },
18363   { VAR6 (SCALARMUL, vmul_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18364   { VAR6 (SCALARMAC, vmla_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18365   { VAR2 (SCALARMAC, vmlal_n, v4hi, v2si) },
18366   { VAR2 (SCALARMAC, vqdmlal_n, v4hi, v2si) },
18367   { VAR6 (SCALARMAC, vmls_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18368   { VAR2 (SCALARMAC, vmlsl_n, v4hi, v2si) },
18369   { VAR2 (SCALARMAC, vqdmlsl_n, v4hi, v2si) },
18370   { VAR10 (BINOP, vext,
18371            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18372   { VAR8 (UNOP, vrev64, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18373   { VAR4 (UNOP, vrev32, v8qi, v4hi, v16qi, v8hi) },
18374   { VAR2 (UNOP, vrev16, v8qi, v16qi) },
18375   { VAR4 (CONVERT, vcvt, v2si, v2sf, v4si, v4sf) },
18376   { VAR4 (FIXCONV, vcvt_n, v2si, v2sf, v4si, v4sf) },
18377   { VAR10 (SELECT, vbsl,
18378            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18379   { VAR1 (VTBL, vtbl1, v8qi) },
18380   { VAR1 (VTBL, vtbl2, v8qi) },
18381   { VAR1 (VTBL, vtbl3, v8qi) },
18382   { VAR1 (VTBL, vtbl4, v8qi) },
18383   { VAR1 (VTBX, vtbx1, v8qi) },
18384   { VAR1 (VTBX, vtbx2, v8qi) },
18385   { VAR1 (VTBX, vtbx3, v8qi) },
18386   { VAR1 (VTBX, vtbx4, v8qi) },
18387   { VAR8 (RESULTPAIR, vtrn, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18388   { VAR8 (RESULTPAIR, vzip, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18389   { VAR8 (RESULTPAIR, vuzp, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
18390   { VAR5 (REINTERP, vreinterpretv8qi, v8qi, v4hi, v2si, v2sf, di) },
18391   { VAR5 (REINTERP, vreinterpretv4hi, v8qi, v4hi, v2si, v2sf, di) },
18392   { VAR5 (REINTERP, vreinterpretv2si, v8qi, v4hi, v2si, v2sf, di) },
18393   { VAR5 (REINTERP, vreinterpretv2sf, v8qi, v4hi, v2si, v2sf, di) },
18394   { VAR5 (REINTERP, vreinterpretdi, v8qi, v4hi, v2si, v2sf, di) },
18395   { VAR5 (REINTERP, vreinterpretv16qi, v16qi, v8hi, v4si, v4sf, v2di) },
18396   { VAR5 (REINTERP, vreinterpretv8hi, v16qi, v8hi, v4si, v4sf, v2di) },
18397   { VAR5 (REINTERP, vreinterpretv4si, v16qi, v8hi, v4si, v4sf, v2di) },
18398   { VAR5 (REINTERP, vreinterpretv4sf, v16qi, v8hi, v4si, v4sf, v2di) },
18399   { VAR5 (REINTERP, vreinterpretv2di, v16qi, v8hi, v4si, v4sf, v2di) },
18400   { VAR10 (LOAD1, vld1,
18401            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18402   { VAR10 (LOAD1LANE, vld1_lane,
18403            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18404   { VAR10 (LOAD1, vld1_dup,
18405            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18406   { VAR10 (STORE1, vst1,
18407            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18408   { VAR10 (STORE1LANE, vst1_lane,
18409            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18410   { VAR9 (LOADSTRUCT,
18411           vld2, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18412   { VAR7 (LOADSTRUCTLANE, vld2_lane,
18413           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18414   { VAR5 (LOADSTRUCT, vld2_dup, v8qi, v4hi, v2si, v2sf, di) },
18415   { VAR9 (STORESTRUCT, vst2,
18416           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18417   { VAR7 (STORESTRUCTLANE, vst2_lane,
18418           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18419   { VAR9 (LOADSTRUCT,
18420           vld3, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18421   { VAR7 (LOADSTRUCTLANE, vld3_lane,
18422           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18423   { VAR5 (LOADSTRUCT, vld3_dup, v8qi, v4hi, v2si, v2sf, di) },
18424   { VAR9 (STORESTRUCT, vst3,
18425           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18426   { VAR7 (STORESTRUCTLANE, vst3_lane,
18427           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18428   { VAR9 (LOADSTRUCT, vld4,
18429           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18430   { VAR7 (LOADSTRUCTLANE, vld4_lane,
18431           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18432   { VAR5 (LOADSTRUCT, vld4_dup, v8qi, v4hi, v2si, v2sf, di) },
18433   { VAR9 (STORESTRUCT, vst4,
18434           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
18435   { VAR7 (STORESTRUCTLANE, vst4_lane,
18436           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
18437   { VAR10 (LOGICBINOP, vand,
18438            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18439   { VAR10 (LOGICBINOP, vorr,
18440            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18441   { VAR10 (BINOP, veor,
18442            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18443   { VAR10 (LOGICBINOP, vbic,
18444            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
18445   { VAR10 (LOGICBINOP, vorn,
18446            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) }
18447 };
18448
18449 #undef CF
18450 #undef VAR1
18451 #undef VAR2
18452 #undef VAR3
18453 #undef VAR4
18454 #undef VAR5
18455 #undef VAR6
18456 #undef VAR7
18457 #undef VAR8
18458 #undef VAR9
18459 #undef VAR10
18460
18461 static void
18462 arm_init_neon_builtins (void)
18463 {
18464   unsigned int i, fcode = ARM_BUILTIN_NEON_BASE;
18465
18466   tree neon_intQI_type_node;
18467   tree neon_intHI_type_node;
18468   tree neon_polyQI_type_node;
18469   tree neon_polyHI_type_node;
18470   tree neon_intSI_type_node;
18471   tree neon_intDI_type_node;
18472   tree neon_float_type_node;
18473
18474   tree intQI_pointer_node;
18475   tree intHI_pointer_node;
18476   tree intSI_pointer_node;
18477   tree intDI_pointer_node;
18478   tree float_pointer_node;
18479
18480   tree const_intQI_node;
18481   tree const_intHI_node;
18482   tree const_intSI_node;
18483   tree const_intDI_node;
18484   tree const_float_node;
18485
18486   tree const_intQI_pointer_node;
18487   tree const_intHI_pointer_node;
18488   tree const_intSI_pointer_node;
18489   tree const_intDI_pointer_node;
18490   tree const_float_pointer_node;
18491
18492   tree V8QI_type_node;
18493   tree V4HI_type_node;
18494   tree V2SI_type_node;
18495   tree V2SF_type_node;
18496   tree V16QI_type_node;
18497   tree V8HI_type_node;
18498   tree V4SI_type_node;
18499   tree V4SF_type_node;
18500   tree V2DI_type_node;
18501
18502   tree intUQI_type_node;
18503   tree intUHI_type_node;
18504   tree intUSI_type_node;
18505   tree intUDI_type_node;
18506
18507   tree intEI_type_node;
18508   tree intOI_type_node;
18509   tree intCI_type_node;
18510   tree intXI_type_node;
18511
18512   tree V8QI_pointer_node;
18513   tree V4HI_pointer_node;
18514   tree V2SI_pointer_node;
18515   tree V2SF_pointer_node;
18516   tree V16QI_pointer_node;
18517   tree V8HI_pointer_node;
18518   tree V4SI_pointer_node;
18519   tree V4SF_pointer_node;
18520   tree V2DI_pointer_node;
18521
18522   tree void_ftype_pv8qi_v8qi_v8qi;
18523   tree void_ftype_pv4hi_v4hi_v4hi;
18524   tree void_ftype_pv2si_v2si_v2si;
18525   tree void_ftype_pv2sf_v2sf_v2sf;
18526   tree void_ftype_pdi_di_di;
18527   tree void_ftype_pv16qi_v16qi_v16qi;
18528   tree void_ftype_pv8hi_v8hi_v8hi;
18529   tree void_ftype_pv4si_v4si_v4si;
18530   tree void_ftype_pv4sf_v4sf_v4sf;
18531   tree void_ftype_pv2di_v2di_v2di;
18532
18533   tree reinterp_ftype_dreg[5][5];
18534   tree reinterp_ftype_qreg[5][5];
18535   tree dreg_types[5], qreg_types[5];
18536
18537   /* Create distinguished type nodes for NEON vector element types,
18538      and pointers to values of such types, so we can detect them later.  */
18539   neon_intQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
18540   neon_intHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
18541   neon_polyQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
18542   neon_polyHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
18543   neon_intSI_type_node = make_signed_type (GET_MODE_PRECISION (SImode));
18544   neon_intDI_type_node = make_signed_type (GET_MODE_PRECISION (DImode));
18545   neon_float_type_node = make_node (REAL_TYPE);
18546   TYPE_PRECISION (neon_float_type_node) = FLOAT_TYPE_SIZE;
18547   layout_type (neon_float_type_node);
18548
18549   /* Define typedefs which exactly correspond to the modes we are basing vector
18550      types on.  If you change these names you'll need to change
18551      the table used by arm_mangle_type too.  */
18552   (*lang_hooks.types.register_builtin_type) (neon_intQI_type_node,
18553                                              "__builtin_neon_qi");
18554   (*lang_hooks.types.register_builtin_type) (neon_intHI_type_node,
18555                                              "__builtin_neon_hi");
18556   (*lang_hooks.types.register_builtin_type) (neon_intSI_type_node,
18557                                              "__builtin_neon_si");
18558   (*lang_hooks.types.register_builtin_type) (neon_float_type_node,
18559                                              "__builtin_neon_sf");
18560   (*lang_hooks.types.register_builtin_type) (neon_intDI_type_node,
18561                                              "__builtin_neon_di");
18562   (*lang_hooks.types.register_builtin_type) (neon_polyQI_type_node,
18563                                              "__builtin_neon_poly8");
18564   (*lang_hooks.types.register_builtin_type) (neon_polyHI_type_node,
18565                                              "__builtin_neon_poly16");
18566
18567   intQI_pointer_node = build_pointer_type (neon_intQI_type_node);
18568   intHI_pointer_node = build_pointer_type (neon_intHI_type_node);
18569   intSI_pointer_node = build_pointer_type (neon_intSI_type_node);
18570   intDI_pointer_node = build_pointer_type (neon_intDI_type_node);
18571   float_pointer_node = build_pointer_type (neon_float_type_node);
18572
18573   /* Next create constant-qualified versions of the above types.  */
18574   const_intQI_node = build_qualified_type (neon_intQI_type_node,
18575                                            TYPE_QUAL_CONST);
18576   const_intHI_node = build_qualified_type (neon_intHI_type_node,
18577                                            TYPE_QUAL_CONST);
18578   const_intSI_node = build_qualified_type (neon_intSI_type_node,
18579                                            TYPE_QUAL_CONST);
18580   const_intDI_node = build_qualified_type (neon_intDI_type_node,
18581                                            TYPE_QUAL_CONST);
18582   const_float_node = build_qualified_type (neon_float_type_node,
18583                                            TYPE_QUAL_CONST);
18584
18585   const_intQI_pointer_node = build_pointer_type (const_intQI_node);
18586   const_intHI_pointer_node = build_pointer_type (const_intHI_node);
18587   const_intSI_pointer_node = build_pointer_type (const_intSI_node);
18588   const_intDI_pointer_node = build_pointer_type (const_intDI_node);
18589   const_float_pointer_node = build_pointer_type (const_float_node);
18590
18591   /* Now create vector types based on our NEON element types.  */
18592   /* 64-bit vectors.  */
18593   V8QI_type_node =
18594     build_vector_type_for_mode (neon_intQI_type_node, V8QImode);
18595   V4HI_type_node =
18596     build_vector_type_for_mode (neon_intHI_type_node, V4HImode);
18597   V2SI_type_node =
18598     build_vector_type_for_mode (neon_intSI_type_node, V2SImode);
18599   V2SF_type_node =
18600     build_vector_type_for_mode (neon_float_type_node, V2SFmode);
18601   /* 128-bit vectors.  */
18602   V16QI_type_node =
18603     build_vector_type_for_mode (neon_intQI_type_node, V16QImode);
18604   V8HI_type_node =
18605     build_vector_type_for_mode (neon_intHI_type_node, V8HImode);
18606   V4SI_type_node =
18607     build_vector_type_for_mode (neon_intSI_type_node, V4SImode);
18608   V4SF_type_node =
18609     build_vector_type_for_mode (neon_float_type_node, V4SFmode);
18610   V2DI_type_node =
18611     build_vector_type_for_mode (neon_intDI_type_node, V2DImode);
18612
18613   /* Unsigned integer types for various mode sizes.  */
18614   intUQI_type_node = make_unsigned_type (GET_MODE_PRECISION (QImode));
18615   intUHI_type_node = make_unsigned_type (GET_MODE_PRECISION (HImode));
18616   intUSI_type_node = make_unsigned_type (GET_MODE_PRECISION (SImode));
18617   intUDI_type_node = make_unsigned_type (GET_MODE_PRECISION (DImode));
18618
18619   (*lang_hooks.types.register_builtin_type) (intUQI_type_node,
18620                                              "__builtin_neon_uqi");
18621   (*lang_hooks.types.register_builtin_type) (intUHI_type_node,
18622                                              "__builtin_neon_uhi");
18623   (*lang_hooks.types.register_builtin_type) (intUSI_type_node,
18624                                              "__builtin_neon_usi");
18625   (*lang_hooks.types.register_builtin_type) (intUDI_type_node,
18626                                              "__builtin_neon_udi");
18627
18628   /* Opaque integer types for structures of vectors.  */
18629   intEI_type_node = make_signed_type (GET_MODE_PRECISION (EImode));
18630   intOI_type_node = make_signed_type (GET_MODE_PRECISION (OImode));
18631   intCI_type_node = make_signed_type (GET_MODE_PRECISION (CImode));
18632   intXI_type_node = make_signed_type (GET_MODE_PRECISION (XImode));
18633
18634   (*lang_hooks.types.register_builtin_type) (intTI_type_node,
18635                                              "__builtin_neon_ti");
18636   (*lang_hooks.types.register_builtin_type) (intEI_type_node,
18637                                              "__builtin_neon_ei");
18638   (*lang_hooks.types.register_builtin_type) (intOI_type_node,
18639                                              "__builtin_neon_oi");
18640   (*lang_hooks.types.register_builtin_type) (intCI_type_node,
18641                                              "__builtin_neon_ci");
18642   (*lang_hooks.types.register_builtin_type) (intXI_type_node,
18643                                              "__builtin_neon_xi");
18644
18645   /* Pointers to vector types.  */
18646   V8QI_pointer_node = build_pointer_type (V8QI_type_node);
18647   V4HI_pointer_node = build_pointer_type (V4HI_type_node);
18648   V2SI_pointer_node = build_pointer_type (V2SI_type_node);
18649   V2SF_pointer_node = build_pointer_type (V2SF_type_node);
18650   V16QI_pointer_node = build_pointer_type (V16QI_type_node);
18651   V8HI_pointer_node = build_pointer_type (V8HI_type_node);
18652   V4SI_pointer_node = build_pointer_type (V4SI_type_node);
18653   V4SF_pointer_node = build_pointer_type (V4SF_type_node);
18654   V2DI_pointer_node = build_pointer_type (V2DI_type_node);
18655
18656   /* Operations which return results as pairs.  */
18657   void_ftype_pv8qi_v8qi_v8qi =
18658     build_function_type_list (void_type_node, V8QI_pointer_node, V8QI_type_node,
18659                               V8QI_type_node, NULL);
18660   void_ftype_pv4hi_v4hi_v4hi =
18661     build_function_type_list (void_type_node, V4HI_pointer_node, V4HI_type_node,
18662                               V4HI_type_node, NULL);
18663   void_ftype_pv2si_v2si_v2si =
18664     build_function_type_list (void_type_node, V2SI_pointer_node, V2SI_type_node,
18665                               V2SI_type_node, NULL);
18666   void_ftype_pv2sf_v2sf_v2sf =
18667     build_function_type_list (void_type_node, V2SF_pointer_node, V2SF_type_node,
18668                               V2SF_type_node, NULL);
18669   void_ftype_pdi_di_di =
18670     build_function_type_list (void_type_node, intDI_pointer_node,
18671                               neon_intDI_type_node, neon_intDI_type_node, NULL);
18672   void_ftype_pv16qi_v16qi_v16qi =
18673     build_function_type_list (void_type_node, V16QI_pointer_node,
18674                               V16QI_type_node, V16QI_type_node, NULL);
18675   void_ftype_pv8hi_v8hi_v8hi =
18676     build_function_type_list (void_type_node, V8HI_pointer_node, V8HI_type_node,
18677                               V8HI_type_node, NULL);
18678   void_ftype_pv4si_v4si_v4si =
18679     build_function_type_list (void_type_node, V4SI_pointer_node, V4SI_type_node,
18680                               V4SI_type_node, NULL);
18681   void_ftype_pv4sf_v4sf_v4sf =
18682     build_function_type_list (void_type_node, V4SF_pointer_node, V4SF_type_node,
18683                               V4SF_type_node, NULL);
18684   void_ftype_pv2di_v2di_v2di =
18685     build_function_type_list (void_type_node, V2DI_pointer_node, V2DI_type_node,
18686                               V2DI_type_node, NULL);
18687
18688   dreg_types[0] = V8QI_type_node;
18689   dreg_types[1] = V4HI_type_node;
18690   dreg_types[2] = V2SI_type_node;
18691   dreg_types[3] = V2SF_type_node;
18692   dreg_types[4] = neon_intDI_type_node;
18693
18694   qreg_types[0] = V16QI_type_node;
18695   qreg_types[1] = V8HI_type_node;
18696   qreg_types[2] = V4SI_type_node;
18697   qreg_types[3] = V4SF_type_node;
18698   qreg_types[4] = V2DI_type_node;
18699
18700   for (i = 0; i < 5; i++)
18701     {
18702       int j;
18703       for (j = 0; j < 5; j++)
18704         {
18705           reinterp_ftype_dreg[i][j]
18706             = build_function_type_list (dreg_types[i], dreg_types[j], NULL);
18707           reinterp_ftype_qreg[i][j]
18708             = build_function_type_list (qreg_types[i], qreg_types[j], NULL);
18709         }
18710     }
18711
18712   for (i = 0; i < ARRAY_SIZE (neon_builtin_data); i++)
18713     {
18714       neon_builtin_datum *d = &neon_builtin_data[i];
18715       unsigned int j, codeidx = 0;
18716
18717       d->base_fcode = fcode;
18718
18719       for (j = 0; j < T_MAX; j++)
18720         {
18721           const char* const modenames[] = {
18722             "v8qi", "v4hi", "v2si", "v2sf", "di",
18723             "v16qi", "v8hi", "v4si", "v4sf", "v2di"
18724           };
18725           char namebuf[60];
18726           tree ftype = NULL;
18727           enum insn_code icode;
18728           int is_load = 0, is_store = 0;
18729
18730           if ((d->bits & (1 << j)) == 0)
18731             continue;
18732
18733           icode = d->codes[codeidx++];
18734
18735           switch (d->itype)
18736             {
18737             case NEON_LOAD1:
18738             case NEON_LOAD1LANE:
18739             case NEON_LOADSTRUCT:
18740             case NEON_LOADSTRUCTLANE:
18741               is_load = 1;
18742               /* Fall through.  */
18743             case NEON_STORE1:
18744             case NEON_STORE1LANE:
18745             case NEON_STORESTRUCT:
18746             case NEON_STORESTRUCTLANE:
18747               if (!is_load)
18748                 is_store = 1;
18749               /* Fall through.  */
18750             case NEON_UNOP:
18751             case NEON_BINOP:
18752             case NEON_LOGICBINOP:
18753             case NEON_SHIFTINSERT:
18754             case NEON_TERNOP:
18755             case NEON_GETLANE:
18756             case NEON_SETLANE:
18757             case NEON_CREATE:
18758             case NEON_DUP:
18759             case NEON_DUPLANE:
18760             case NEON_SHIFTIMM:
18761             case NEON_SHIFTACC:
18762             case NEON_COMBINE:
18763             case NEON_SPLIT:
18764             case NEON_CONVERT:
18765             case NEON_FIXCONV:
18766             case NEON_LANEMUL:
18767             case NEON_LANEMULL:
18768             case NEON_LANEMULH:
18769             case NEON_LANEMAC:
18770             case NEON_SCALARMUL:
18771             case NEON_SCALARMULL:
18772             case NEON_SCALARMULH:
18773             case NEON_SCALARMAC:
18774             case NEON_SELECT:
18775             case NEON_VTBL:
18776             case NEON_VTBX:
18777               {
18778                 int k;
18779                 tree return_type = void_type_node, args = void_list_node;
18780
18781                 /* Build a function type directly from the insn_data for this
18782                    builtin.  The build_function_type() function takes care of
18783                    removing duplicates for us.  */
18784                 for (k = insn_data[icode].n_operands - 1; k >= 0; k--)
18785                   {
18786                     tree eltype;
18787
18788                     if (is_load && k == 1)
18789                       {
18790                         /* Neon load patterns always have the memory operand
18791                            (a SImode pointer) in the operand 1 position.  We
18792                            want a const pointer to the element type in that
18793                            position.  */
18794                         gcc_assert (insn_data[icode].operand[k].mode == SImode);
18795
18796                         switch (1 << j)
18797                           {
18798                           case T_V8QI:
18799                           case T_V16QI:
18800                             eltype = const_intQI_pointer_node;
18801                             break;
18802
18803                           case T_V4HI:
18804                           case T_V8HI:
18805                             eltype = const_intHI_pointer_node;
18806                             break;
18807
18808                           case T_V2SI:
18809                           case T_V4SI:
18810                             eltype = const_intSI_pointer_node;
18811                             break;
18812
18813                           case T_V2SF:
18814                           case T_V4SF:
18815                             eltype = const_float_pointer_node;
18816                             break;
18817
18818                           case T_DI:
18819                           case T_V2DI:
18820                             eltype = const_intDI_pointer_node;
18821                             break;
18822
18823                           default: gcc_unreachable ();
18824                           }
18825                       }
18826                     else if (is_store && k == 0)
18827                       {
18828                         /* Similarly, Neon store patterns use operand 0 as
18829                            the memory location to store to (a SImode pointer).
18830                            Use a pointer to the element type of the store in
18831                            that position.  */
18832                         gcc_assert (insn_data[icode].operand[k].mode == SImode);
18833
18834                         switch (1 << j)
18835                           {
18836                           case T_V8QI:
18837                           case T_V16QI:
18838                             eltype = intQI_pointer_node;
18839                             break;
18840
18841                           case T_V4HI:
18842                           case T_V8HI:
18843                             eltype = intHI_pointer_node;
18844                             break;
18845
18846                           case T_V2SI:
18847                           case T_V4SI:
18848                             eltype = intSI_pointer_node;
18849                             break;
18850
18851                           case T_V2SF:
18852                           case T_V4SF:
18853                             eltype = float_pointer_node;
18854                             break;
18855
18856                           case T_DI:
18857                           case T_V2DI:
18858                             eltype = intDI_pointer_node;
18859                             break;
18860
18861                           default: gcc_unreachable ();
18862                           }
18863                       }
18864                     else
18865                       {
18866                         switch (insn_data[icode].operand[k].mode)
18867                           {
18868                           case VOIDmode: eltype = void_type_node; break;
18869                           /* Scalars.  */
18870                           case QImode: eltype = neon_intQI_type_node; break;
18871                           case HImode: eltype = neon_intHI_type_node; break;
18872                           case SImode: eltype = neon_intSI_type_node; break;
18873                           case SFmode: eltype = neon_float_type_node; break;
18874                           case DImode: eltype = neon_intDI_type_node; break;
18875                           case TImode: eltype = intTI_type_node; break;
18876                           case EImode: eltype = intEI_type_node; break;
18877                           case OImode: eltype = intOI_type_node; break;
18878                           case CImode: eltype = intCI_type_node; break;
18879                           case XImode: eltype = intXI_type_node; break;
18880                           /* 64-bit vectors.  */
18881                           case V8QImode: eltype = V8QI_type_node; break;
18882                           case V4HImode: eltype = V4HI_type_node; break;
18883                           case V2SImode: eltype = V2SI_type_node; break;
18884                           case V2SFmode: eltype = V2SF_type_node; break;
18885                           /* 128-bit vectors.  */
18886                           case V16QImode: eltype = V16QI_type_node; break;
18887                           case V8HImode: eltype = V8HI_type_node; break;
18888                           case V4SImode: eltype = V4SI_type_node; break;
18889                           case V4SFmode: eltype = V4SF_type_node; break;
18890                           case V2DImode: eltype = V2DI_type_node; break;
18891                           default: gcc_unreachable ();
18892                           }
18893                       }
18894
18895                     if (k == 0 && !is_store)
18896                       return_type = eltype;
18897                     else
18898                       args = tree_cons (NULL_TREE, eltype, args);
18899                   }
18900
18901                 ftype = build_function_type (return_type, args);
18902               }
18903               break;
18904
18905             case NEON_RESULTPAIR:
18906               {
18907                 switch (insn_data[icode].operand[1].mode)
18908                   {
18909                   case V8QImode: ftype = void_ftype_pv8qi_v8qi_v8qi; break;
18910                   case V4HImode: ftype = void_ftype_pv4hi_v4hi_v4hi; break;
18911                   case V2SImode: ftype = void_ftype_pv2si_v2si_v2si; break;
18912                   case V2SFmode: ftype = void_ftype_pv2sf_v2sf_v2sf; break;
18913                   case DImode: ftype = void_ftype_pdi_di_di; break;
18914                   case V16QImode: ftype = void_ftype_pv16qi_v16qi_v16qi; break;
18915                   case V8HImode: ftype = void_ftype_pv8hi_v8hi_v8hi; break;
18916                   case V4SImode: ftype = void_ftype_pv4si_v4si_v4si; break;
18917                   case V4SFmode: ftype = void_ftype_pv4sf_v4sf_v4sf; break;
18918                   case V2DImode: ftype = void_ftype_pv2di_v2di_v2di; break;
18919                   default: gcc_unreachable ();
18920                   }
18921               }
18922               break;
18923
18924             case NEON_REINTERP:
18925               {
18926                 /* We iterate over 5 doubleword types, then 5 quadword
18927                    types.  */
18928                 int rhs = j % 5;
18929                 switch (insn_data[icode].operand[0].mode)
18930                   {
18931                   case V8QImode: ftype = reinterp_ftype_dreg[0][rhs]; break;
18932                   case V4HImode: ftype = reinterp_ftype_dreg[1][rhs]; break;
18933                   case V2SImode: ftype = reinterp_ftype_dreg[2][rhs]; break;
18934                   case V2SFmode: ftype = reinterp_ftype_dreg[3][rhs]; break;
18935                   case DImode: ftype = reinterp_ftype_dreg[4][rhs]; break;
18936                   case V16QImode: ftype = reinterp_ftype_qreg[0][rhs]; break;
18937                   case V8HImode: ftype = reinterp_ftype_qreg[1][rhs]; break;
18938                   case V4SImode: ftype = reinterp_ftype_qreg[2][rhs]; break;
18939                   case V4SFmode: ftype = reinterp_ftype_qreg[3][rhs]; break;
18940                   case V2DImode: ftype = reinterp_ftype_qreg[4][rhs]; break;
18941                   default: gcc_unreachable ();
18942                   }
18943               }
18944               break;
18945
18946             default:
18947               gcc_unreachable ();
18948             }
18949
18950           gcc_assert (ftype != NULL);
18951
18952           sprintf (namebuf, "__builtin_neon_%s%s", d->name, modenames[j]);
18953
18954           add_builtin_function (namebuf, ftype, fcode++, BUILT_IN_MD, NULL,
18955                                 NULL_TREE);
18956         }
18957     }
18958 }
18959
18960 static void
18961 arm_init_fp16_builtins (void)
18962 {
18963   tree fp16_type = make_node (REAL_TYPE);
18964   TYPE_PRECISION (fp16_type) = 16;
18965   layout_type (fp16_type);
18966   (*lang_hooks.types.register_builtin_type) (fp16_type, "__fp16");
18967 }
18968
18969 static void
18970 arm_init_builtins (void)
18971 {
18972   arm_init_tls_builtins ();
18973
18974   if (TARGET_REALLY_IWMMXT)
18975     arm_init_iwmmxt_builtins ();
18976
18977   if (TARGET_NEON)
18978     arm_init_neon_builtins ();
18979
18980   if (arm_fp16_format)
18981     arm_init_fp16_builtins ();
18982 }
18983
18984 /* Implement TARGET_INVALID_PARAMETER_TYPE.  */
18985
18986 static const char *
18987 arm_invalid_parameter_type (const_tree t)
18988 {
18989   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
18990     return N_("function parameters cannot have __fp16 type");
18991   return NULL;
18992 }
18993
18994 /* Implement TARGET_INVALID_PARAMETER_TYPE.  */
18995
18996 static const char *
18997 arm_invalid_return_type (const_tree t)
18998 {
18999   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
19000     return N_("functions cannot return __fp16 type");
19001   return NULL;
19002 }
19003
19004 /* Implement TARGET_PROMOTED_TYPE.  */
19005
19006 static tree
19007 arm_promoted_type (const_tree t)
19008 {
19009   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
19010     return float_type_node;
19011   return NULL_TREE;
19012 }
19013
19014 /* Implement TARGET_CONVERT_TO_TYPE.
19015    Specifically, this hook implements the peculiarity of the ARM
19016    half-precision floating-point C semantics that requires conversions between
19017    __fp16 to or from double to do an intermediate conversion to float.  */
19018
19019 static tree
19020 arm_convert_to_type (tree type, tree expr)
19021 {
19022   tree fromtype = TREE_TYPE (expr);
19023   if (!SCALAR_FLOAT_TYPE_P (fromtype) || !SCALAR_FLOAT_TYPE_P (type))
19024     return NULL_TREE;
19025   if ((TYPE_PRECISION (fromtype) == 16 && TYPE_PRECISION (type) > 32)
19026       || (TYPE_PRECISION (type) == 16 && TYPE_PRECISION (fromtype) > 32))
19027     return convert (type, convert (float_type_node, expr));
19028   return NULL_TREE;
19029 }
19030
19031 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P.
19032    This simply adds HFmode as a supported mode; even though we don't
19033    implement arithmetic on this type directly, it's supported by
19034    optabs conversions, much the way the double-word arithmetic is
19035    special-cased in the default hook.  */
19036
19037 static bool
19038 arm_scalar_mode_supported_p (enum machine_mode mode)
19039 {
19040   if (mode == HFmode)
19041     return (arm_fp16_format != ARM_FP16_FORMAT_NONE);
19042   else
19043     return default_scalar_mode_supported_p (mode);
19044 }
19045
19046 /* Errors in the source file can cause expand_expr to return const0_rtx
19047    where we expect a vector.  To avoid crashing, use one of the vector
19048    clear instructions.  */
19049
19050 static rtx
19051 safe_vector_operand (rtx x, enum machine_mode mode)
19052 {
19053   if (x != const0_rtx)
19054     return x;
19055   x = gen_reg_rtx (mode);
19056
19057   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
19058                                : gen_rtx_SUBREG (DImode, x, 0)));
19059   return x;
19060 }
19061
19062 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
19063
19064 static rtx
19065 arm_expand_binop_builtin (enum insn_code icode,
19066                           tree exp, rtx target)
19067 {
19068   rtx pat;
19069   tree arg0 = CALL_EXPR_ARG (exp, 0);
19070   tree arg1 = CALL_EXPR_ARG (exp, 1);
19071   rtx op0 = expand_normal (arg0);
19072   rtx op1 = expand_normal (arg1);
19073   enum machine_mode tmode = insn_data[icode].operand[0].mode;
19074   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
19075   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
19076
19077   if (VECTOR_MODE_P (mode0))
19078     op0 = safe_vector_operand (op0, mode0);
19079   if (VECTOR_MODE_P (mode1))
19080     op1 = safe_vector_operand (op1, mode1);
19081
19082   if (! target
19083       || GET_MODE (target) != tmode
19084       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19085     target = gen_reg_rtx (tmode);
19086
19087   gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
19088
19089   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19090     op0 = copy_to_mode_reg (mode0, op0);
19091   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
19092     op1 = copy_to_mode_reg (mode1, op1);
19093
19094   pat = GEN_FCN (icode) (target, op0, op1);
19095   if (! pat)
19096     return 0;
19097   emit_insn (pat);
19098   return target;
19099 }
19100
19101 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
19102
19103 static rtx
19104 arm_expand_unop_builtin (enum insn_code icode,
19105                          tree exp, rtx target, int do_load)
19106 {
19107   rtx pat;
19108   tree arg0 = CALL_EXPR_ARG (exp, 0);
19109   rtx op0 = expand_normal (arg0);
19110   enum machine_mode tmode = insn_data[icode].operand[0].mode;
19111   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
19112
19113   if (! target
19114       || GET_MODE (target) != tmode
19115       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19116     target = gen_reg_rtx (tmode);
19117   if (do_load)
19118     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
19119   else
19120     {
19121       if (VECTOR_MODE_P (mode0))
19122         op0 = safe_vector_operand (op0, mode0);
19123
19124       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19125         op0 = copy_to_mode_reg (mode0, op0);
19126     }
19127
19128   pat = GEN_FCN (icode) (target, op0);
19129   if (! pat)
19130     return 0;
19131   emit_insn (pat);
19132   return target;
19133 }
19134
19135 static int
19136 neon_builtin_compare (const void *a, const void *b)
19137 {
19138   const neon_builtin_datum *const key = (const neon_builtin_datum *) a;
19139   const neon_builtin_datum *const memb = (const neon_builtin_datum *) b;
19140   unsigned int soughtcode = key->base_fcode;
19141
19142   if (soughtcode >= memb->base_fcode
19143       && soughtcode < memb->base_fcode + memb->num_vars)
19144     return 0;
19145   else if (soughtcode < memb->base_fcode)
19146     return -1;
19147   else
19148     return 1;
19149 }
19150
19151 static enum insn_code
19152 locate_neon_builtin_icode (int fcode, neon_itype *itype)
19153 {
19154   neon_builtin_datum key
19155     = { NULL, (neon_itype) 0, 0, { CODE_FOR_nothing }, 0, 0 };
19156   neon_builtin_datum *found;
19157   int idx;
19158
19159   key.base_fcode = fcode;
19160   found = (neon_builtin_datum *)
19161     bsearch (&key, &neon_builtin_data[0], ARRAY_SIZE (neon_builtin_data),
19162                    sizeof (neon_builtin_data[0]), neon_builtin_compare);
19163   gcc_assert (found);
19164   idx = fcode - (int) found->base_fcode;
19165   gcc_assert (idx >= 0 && idx < T_MAX && idx < (int)found->num_vars);
19166
19167   if (itype)
19168     *itype = found->itype;
19169
19170   return found->codes[idx];
19171 }
19172
19173 typedef enum {
19174   NEON_ARG_COPY_TO_REG,
19175   NEON_ARG_CONSTANT,
19176   NEON_ARG_STOP
19177 } builtin_arg;
19178
19179 #define NEON_MAX_BUILTIN_ARGS 5
19180
19181 /* Expand a Neon builtin.  */
19182 static rtx
19183 arm_expand_neon_args (rtx target, int icode, int have_retval,
19184                       tree exp, ...)
19185 {
19186   va_list ap;
19187   rtx pat;
19188   tree arg[NEON_MAX_BUILTIN_ARGS];
19189   rtx op[NEON_MAX_BUILTIN_ARGS];
19190   enum machine_mode tmode = insn_data[icode].operand[0].mode;
19191   enum machine_mode mode[NEON_MAX_BUILTIN_ARGS];
19192   int argc = 0;
19193
19194   if (have_retval
19195       && (!target
19196           || GET_MODE (target) != tmode
19197           || !(*insn_data[icode].operand[0].predicate) (target, tmode)))
19198     target = gen_reg_rtx (tmode);
19199
19200   va_start (ap, exp);
19201
19202   for (;;)
19203     {
19204       builtin_arg thisarg = (builtin_arg) va_arg (ap, int);
19205
19206       if (thisarg == NEON_ARG_STOP)
19207         break;
19208       else
19209         {
19210           arg[argc] = CALL_EXPR_ARG (exp, argc);
19211           op[argc] = expand_normal (arg[argc]);
19212           mode[argc] = insn_data[icode].operand[argc + have_retval].mode;
19213
19214           switch (thisarg)
19215             {
19216             case NEON_ARG_COPY_TO_REG:
19217               /*gcc_assert (GET_MODE (op[argc]) == mode[argc]);*/
19218               if (!(*insn_data[icode].operand[argc + have_retval].predicate)
19219                      (op[argc], mode[argc]))
19220                 op[argc] = copy_to_mode_reg (mode[argc], op[argc]);
19221               break;
19222
19223             case NEON_ARG_CONSTANT:
19224               /* FIXME: This error message is somewhat unhelpful.  */
19225               if (!(*insn_data[icode].operand[argc + have_retval].predicate)
19226                     (op[argc], mode[argc]))
19227                 error ("argument must be a constant");
19228               break;
19229
19230             case NEON_ARG_STOP:
19231               gcc_unreachable ();
19232             }
19233
19234           argc++;
19235         }
19236     }
19237
19238   va_end (ap);
19239
19240   if (have_retval)
19241     switch (argc)
19242       {
19243       case 1:
19244         pat = GEN_FCN (icode) (target, op[0]);
19245         break;
19246
19247       case 2:
19248         pat = GEN_FCN (icode) (target, op[0], op[1]);
19249         break;
19250
19251       case 3:
19252         pat = GEN_FCN (icode) (target, op[0], op[1], op[2]);
19253         break;
19254
19255       case 4:
19256         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3]);
19257         break;
19258
19259       case 5:
19260         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3], op[4]);
19261         break;
19262
19263       default:
19264         gcc_unreachable ();
19265       }
19266   else
19267     switch (argc)
19268       {
19269       case 1:
19270         pat = GEN_FCN (icode) (op[0]);
19271         break;
19272
19273       case 2:
19274         pat = GEN_FCN (icode) (op[0], op[1]);
19275         break;
19276
19277       case 3:
19278         pat = GEN_FCN (icode) (op[0], op[1], op[2]);
19279         break;
19280
19281       case 4:
19282         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
19283         break;
19284
19285       case 5:
19286         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4]);
19287         break;
19288
19289       default:
19290         gcc_unreachable ();
19291       }
19292
19293   if (!pat)
19294     return 0;
19295
19296   emit_insn (pat);
19297
19298   return target;
19299 }
19300
19301 /* Expand a Neon builtin. These are "special" because they don't have symbolic
19302    constants defined per-instruction or per instruction-variant. Instead, the
19303    required info is looked up in the table neon_builtin_data.  */
19304 static rtx
19305 arm_expand_neon_builtin (int fcode, tree exp, rtx target)
19306 {
19307   neon_itype itype;
19308   enum insn_code icode = locate_neon_builtin_icode (fcode, &itype);
19309
19310   switch (itype)
19311     {
19312     case NEON_UNOP:
19313     case NEON_CONVERT:
19314     case NEON_DUPLANE:
19315       return arm_expand_neon_args (target, icode, 1, exp,
19316         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_STOP);
19317
19318     case NEON_BINOP:
19319     case NEON_SETLANE:
19320     case NEON_SCALARMUL:
19321     case NEON_SCALARMULL:
19322     case NEON_SCALARMULH:
19323     case NEON_SHIFTINSERT:
19324     case NEON_LOGICBINOP:
19325       return arm_expand_neon_args (target, icode, 1, exp,
19326         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19327         NEON_ARG_STOP);
19328
19329     case NEON_TERNOP:
19330       return arm_expand_neon_args (target, icode, 1, exp,
19331         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19332         NEON_ARG_CONSTANT, NEON_ARG_STOP);
19333
19334     case NEON_GETLANE:
19335     case NEON_FIXCONV:
19336     case NEON_SHIFTIMM:
19337       return arm_expand_neon_args (target, icode, 1, exp,
19338         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_CONSTANT,
19339         NEON_ARG_STOP);
19340
19341     case NEON_CREATE:
19342       return arm_expand_neon_args (target, icode, 1, exp,
19343         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19344
19345     case NEON_DUP:
19346     case NEON_SPLIT:
19347     case NEON_REINTERP:
19348       return arm_expand_neon_args (target, icode, 1, exp,
19349         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19350
19351     case NEON_COMBINE:
19352     case NEON_VTBL:
19353       return arm_expand_neon_args (target, icode, 1, exp,
19354         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19355
19356     case NEON_RESULTPAIR:
19357       return arm_expand_neon_args (target, icode, 0, 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_LANEMUL:
19362     case NEON_LANEMULL:
19363     case NEON_LANEMULH:
19364       return arm_expand_neon_args (target, icode, 1, exp,
19365         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19366         NEON_ARG_CONSTANT, NEON_ARG_STOP);
19367
19368     case NEON_LANEMAC:
19369       return arm_expand_neon_args (target, icode, 1, exp,
19370         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19371         NEON_ARG_CONSTANT, NEON_ARG_CONSTANT, NEON_ARG_STOP);
19372
19373     case NEON_SHIFTACC:
19374       return arm_expand_neon_args (target, icode, 1, exp,
19375         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19376         NEON_ARG_CONSTANT, NEON_ARG_STOP);
19377
19378     case NEON_SCALARMAC:
19379       return arm_expand_neon_args (target, icode, 1, exp,
19380         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19381         NEON_ARG_CONSTANT, NEON_ARG_STOP);
19382
19383     case NEON_SELECT:
19384     case NEON_VTBX:
19385       return arm_expand_neon_args (target, icode, 1, exp,
19386         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
19387         NEON_ARG_STOP);
19388
19389     case NEON_LOAD1:
19390     case NEON_LOADSTRUCT:
19391       return arm_expand_neon_args (target, icode, 1, exp,
19392         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19393
19394     case NEON_LOAD1LANE:
19395     case NEON_LOADSTRUCTLANE:
19396       return arm_expand_neon_args (target, icode, 1, exp,
19397         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19398         NEON_ARG_STOP);
19399
19400     case NEON_STORE1:
19401     case NEON_STORESTRUCT:
19402       return arm_expand_neon_args (target, icode, 0, exp,
19403         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
19404
19405     case NEON_STORE1LANE:
19406     case NEON_STORESTRUCTLANE:
19407       return arm_expand_neon_args (target, icode, 0, exp,
19408         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
19409         NEON_ARG_STOP);
19410     }
19411
19412   gcc_unreachable ();
19413 }
19414
19415 /* Emit code to reinterpret one Neon type as another, without altering bits.  */
19416 void
19417 neon_reinterpret (rtx dest, rtx src)
19418 {
19419   emit_move_insn (dest, gen_lowpart (GET_MODE (dest), src));
19420 }
19421
19422 /* Emit code to place a Neon pair result in memory locations (with equal
19423    registers).  */
19424 void
19425 neon_emit_pair_result_insn (enum machine_mode mode,
19426                             rtx (*intfn) (rtx, rtx, rtx, rtx), rtx destaddr,
19427                             rtx op1, rtx op2)
19428 {
19429   rtx mem = gen_rtx_MEM (mode, destaddr);
19430   rtx tmp1 = gen_reg_rtx (mode);
19431   rtx tmp2 = gen_reg_rtx (mode);
19432
19433   emit_insn (intfn (tmp1, op1, tmp2, op2));
19434
19435   emit_move_insn (mem, tmp1);
19436   mem = adjust_address (mem, mode, GET_MODE_SIZE (mode));
19437   emit_move_insn (mem, tmp2);
19438 }
19439
19440 /* Set up operands for a register copy from src to dest, taking care not to
19441    clobber registers in the process.
19442    FIXME: This has rather high polynomial complexity (O(n^3)?) but shouldn't
19443    be called with a large N, so that should be OK.  */
19444
19445 void
19446 neon_disambiguate_copy (rtx *operands, rtx *dest, rtx *src, unsigned int count)
19447 {
19448   unsigned int copied = 0, opctr = 0;
19449   unsigned int done = (1 << count) - 1;
19450   unsigned int i, j;
19451
19452   while (copied != done)
19453     {
19454       for (i = 0; i < count; i++)
19455         {
19456           int good = 1;
19457
19458           for (j = 0; good && j < count; j++)
19459             if (i != j && (copied & (1 << j)) == 0
19460                 && reg_overlap_mentioned_p (src[j], dest[i]))
19461               good = 0;
19462
19463           if (good)
19464             {
19465               operands[opctr++] = dest[i];
19466               operands[opctr++] = src[i];
19467               copied |= 1 << i;
19468             }
19469         }
19470     }
19471
19472   gcc_assert (opctr == count * 2);
19473 }
19474
19475 /* Expand an expression EXP that calls a built-in function,
19476    with result going to TARGET if that's convenient
19477    (and in mode MODE if that's convenient).
19478    SUBTARGET may be used as the target for computing one of EXP's operands.
19479    IGNORE is nonzero if the value is to be ignored.  */
19480
19481 static rtx
19482 arm_expand_builtin (tree exp,
19483                     rtx target,
19484                     rtx subtarget ATTRIBUTE_UNUSED,
19485                     enum machine_mode mode ATTRIBUTE_UNUSED,
19486                     int ignore ATTRIBUTE_UNUSED)
19487 {
19488   const struct builtin_description * d;
19489   enum insn_code    icode;
19490   tree              fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
19491   tree              arg0;
19492   tree              arg1;
19493   tree              arg2;
19494   rtx               op0;
19495   rtx               op1;
19496   rtx               op2;
19497   rtx               pat;
19498   int               fcode = DECL_FUNCTION_CODE (fndecl);
19499   size_t            i;
19500   enum machine_mode tmode;
19501   enum machine_mode mode0;
19502   enum machine_mode mode1;
19503   enum machine_mode mode2;
19504
19505   if (fcode >= ARM_BUILTIN_NEON_BASE)
19506     return arm_expand_neon_builtin (fcode, exp, target);
19507
19508   switch (fcode)
19509     {
19510     case ARM_BUILTIN_TEXTRMSB:
19511     case ARM_BUILTIN_TEXTRMUB:
19512     case ARM_BUILTIN_TEXTRMSH:
19513     case ARM_BUILTIN_TEXTRMUH:
19514     case ARM_BUILTIN_TEXTRMSW:
19515     case ARM_BUILTIN_TEXTRMUW:
19516       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
19517                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
19518                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
19519                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
19520                : CODE_FOR_iwmmxt_textrmw);
19521
19522       arg0 = CALL_EXPR_ARG (exp, 0);
19523       arg1 = CALL_EXPR_ARG (exp, 1);
19524       op0 = expand_normal (arg0);
19525       op1 = expand_normal (arg1);
19526       tmode = insn_data[icode].operand[0].mode;
19527       mode0 = insn_data[icode].operand[1].mode;
19528       mode1 = insn_data[icode].operand[2].mode;
19529
19530       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19531         op0 = copy_to_mode_reg (mode0, op0);
19532       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
19533         {
19534           /* @@@ better error message */
19535           error ("selector must be an immediate");
19536           return gen_reg_rtx (tmode);
19537         }
19538       if (target == 0
19539           || GET_MODE (target) != tmode
19540           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19541         target = gen_reg_rtx (tmode);
19542       pat = GEN_FCN (icode) (target, op0, op1);
19543       if (! pat)
19544         return 0;
19545       emit_insn (pat);
19546       return target;
19547
19548     case ARM_BUILTIN_TINSRB:
19549     case ARM_BUILTIN_TINSRH:
19550     case ARM_BUILTIN_TINSRW:
19551       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
19552                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
19553                : CODE_FOR_iwmmxt_tinsrw);
19554       arg0 = CALL_EXPR_ARG (exp, 0);
19555       arg1 = CALL_EXPR_ARG (exp, 1);
19556       arg2 = CALL_EXPR_ARG (exp, 2);
19557       op0 = expand_normal (arg0);
19558       op1 = expand_normal (arg1);
19559       op2 = expand_normal (arg2);
19560       tmode = insn_data[icode].operand[0].mode;
19561       mode0 = insn_data[icode].operand[1].mode;
19562       mode1 = insn_data[icode].operand[2].mode;
19563       mode2 = insn_data[icode].operand[3].mode;
19564
19565       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19566         op0 = copy_to_mode_reg (mode0, op0);
19567       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
19568         op1 = copy_to_mode_reg (mode1, op1);
19569       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
19570         {
19571           /* @@@ better error message */
19572           error ("selector must be an immediate");
19573           return const0_rtx;
19574         }
19575       if (target == 0
19576           || GET_MODE (target) != tmode
19577           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19578         target = gen_reg_rtx (tmode);
19579       pat = GEN_FCN (icode) (target, op0, op1, op2);
19580       if (! pat)
19581         return 0;
19582       emit_insn (pat);
19583       return target;
19584
19585     case ARM_BUILTIN_SETWCX:
19586       arg0 = CALL_EXPR_ARG (exp, 0);
19587       arg1 = CALL_EXPR_ARG (exp, 1);
19588       op0 = force_reg (SImode, expand_normal (arg0));
19589       op1 = expand_normal (arg1);
19590       emit_insn (gen_iwmmxt_tmcr (op1, op0));
19591       return 0;
19592
19593     case ARM_BUILTIN_GETWCX:
19594       arg0 = CALL_EXPR_ARG (exp, 0);
19595       op0 = expand_normal (arg0);
19596       target = gen_reg_rtx (SImode);
19597       emit_insn (gen_iwmmxt_tmrc (target, op0));
19598       return target;
19599
19600     case ARM_BUILTIN_WSHUFH:
19601       icode = CODE_FOR_iwmmxt_wshufh;
19602       arg0 = CALL_EXPR_ARG (exp, 0);
19603       arg1 = CALL_EXPR_ARG (exp, 1);
19604       op0 = expand_normal (arg0);
19605       op1 = expand_normal (arg1);
19606       tmode = insn_data[icode].operand[0].mode;
19607       mode1 = insn_data[icode].operand[1].mode;
19608       mode2 = insn_data[icode].operand[2].mode;
19609
19610       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
19611         op0 = copy_to_mode_reg (mode1, op0);
19612       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
19613         {
19614           /* @@@ better error message */
19615           error ("mask must be an immediate");
19616           return const0_rtx;
19617         }
19618       if (target == 0
19619           || GET_MODE (target) != tmode
19620           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19621         target = gen_reg_rtx (tmode);
19622       pat = GEN_FCN (icode) (target, op0, op1);
19623       if (! pat)
19624         return 0;
19625       emit_insn (pat);
19626       return target;
19627
19628     case ARM_BUILTIN_WSADB:
19629       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, exp, target);
19630     case ARM_BUILTIN_WSADH:
19631       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, exp, target);
19632     case ARM_BUILTIN_WSADBZ:
19633       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, exp, target);
19634     case ARM_BUILTIN_WSADHZ:
19635       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, exp, target);
19636
19637       /* Several three-argument builtins.  */
19638     case ARM_BUILTIN_WMACS:
19639     case ARM_BUILTIN_WMACU:
19640     case ARM_BUILTIN_WALIGN:
19641     case ARM_BUILTIN_TMIA:
19642     case ARM_BUILTIN_TMIAPH:
19643     case ARM_BUILTIN_TMIATT:
19644     case ARM_BUILTIN_TMIATB:
19645     case ARM_BUILTIN_TMIABT:
19646     case ARM_BUILTIN_TMIABB:
19647       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
19648                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
19649                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
19650                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
19651                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
19652                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
19653                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
19654                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
19655                : CODE_FOR_iwmmxt_walign);
19656       arg0 = CALL_EXPR_ARG (exp, 0);
19657       arg1 = CALL_EXPR_ARG (exp, 1);
19658       arg2 = CALL_EXPR_ARG (exp, 2);
19659       op0 = expand_normal (arg0);
19660       op1 = expand_normal (arg1);
19661       op2 = expand_normal (arg2);
19662       tmode = insn_data[icode].operand[0].mode;
19663       mode0 = insn_data[icode].operand[1].mode;
19664       mode1 = insn_data[icode].operand[2].mode;
19665       mode2 = insn_data[icode].operand[3].mode;
19666
19667       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
19668         op0 = copy_to_mode_reg (mode0, op0);
19669       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
19670         op1 = copy_to_mode_reg (mode1, op1);
19671       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
19672         op2 = copy_to_mode_reg (mode2, op2);
19673       if (target == 0
19674           || GET_MODE (target) != tmode
19675           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19676         target = gen_reg_rtx (tmode);
19677       pat = GEN_FCN (icode) (target, op0, op1, op2);
19678       if (! pat)
19679         return 0;
19680       emit_insn (pat);
19681       return target;
19682
19683     case ARM_BUILTIN_WZERO:
19684       target = gen_reg_rtx (DImode);
19685       emit_insn (gen_iwmmxt_clrdi (target));
19686       return target;
19687
19688     case ARM_BUILTIN_THREAD_POINTER:
19689       return arm_load_tp (target);
19690
19691     default:
19692       break;
19693     }
19694
19695   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
19696     if (d->code == (const enum arm_builtins) fcode)
19697       return arm_expand_binop_builtin (d->icode, exp, target);
19698
19699   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
19700     if (d->code == (const enum arm_builtins) fcode)
19701       return arm_expand_unop_builtin (d->icode, exp, target, 0);
19702
19703   /* @@@ Should really do something sensible here.  */
19704   return NULL_RTX;
19705 }
19706 \f
19707 /* Return the number (counting from 0) of
19708    the least significant set bit in MASK.  */
19709
19710 inline static int
19711 number_of_first_bit_set (unsigned mask)
19712 {
19713   int bit;
19714
19715   for (bit = 0;
19716        (mask & (1 << bit)) == 0;
19717        ++bit)
19718     continue;
19719
19720   return bit;
19721 }
19722
19723 /* Emit code to push or pop registers to or from the stack.  F is the
19724    assembly file.  MASK is the registers to push or pop.  PUSH is
19725    nonzero if we should push, and zero if we should pop.  For debugging
19726    output, if pushing, adjust CFA_OFFSET by the amount of space added
19727    to the stack.  REAL_REGS should have the same number of bits set as
19728    MASK, and will be used instead (in the same order) to describe which
19729    registers were saved - this is used to mark the save slots when we
19730    push high registers after moving them to low registers.  */
19731 static void
19732 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
19733                unsigned long real_regs)
19734 {
19735   int regno;
19736   int lo_mask = mask & 0xFF;
19737   int pushed_words = 0;
19738
19739   gcc_assert (mask);
19740
19741   if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
19742     {
19743       /* Special case.  Do not generate a POP PC statement here, do it in
19744          thumb_exit() */
19745       thumb_exit (f, -1);
19746       return;
19747     }
19748
19749   if (push && arm_except_unwind_info (&global_options) == UI_TARGET)
19750     {
19751       fprintf (f, "\t.save\t{");
19752       for (regno = 0; regno < 15; regno++)
19753         {
19754           if (real_regs & (1 << regno))
19755             {
19756               if (real_regs & ((1 << regno) -1))
19757                 fprintf (f, ", ");
19758               asm_fprintf (f, "%r", regno);
19759             }
19760         }
19761       fprintf (f, "}\n");
19762     }
19763
19764   fprintf (f, "\t%s\t{", push ? "push" : "pop");
19765
19766   /* Look at the low registers first.  */
19767   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
19768     {
19769       if (lo_mask & 1)
19770         {
19771           asm_fprintf (f, "%r", regno);
19772
19773           if ((lo_mask & ~1) != 0)
19774             fprintf (f, ", ");
19775
19776           pushed_words++;
19777         }
19778     }
19779
19780   if (push && (mask & (1 << LR_REGNUM)))
19781     {
19782       /* Catch pushing the LR.  */
19783       if (mask & 0xFF)
19784         fprintf (f, ", ");
19785
19786       asm_fprintf (f, "%r", LR_REGNUM);
19787
19788       pushed_words++;
19789     }
19790   else if (!push && (mask & (1 << PC_REGNUM)))
19791     {
19792       /* Catch popping the PC.  */
19793       if (TARGET_INTERWORK || TARGET_BACKTRACE
19794           || crtl->calls_eh_return)
19795         {
19796           /* The PC is never poped directly, instead
19797              it is popped into r3 and then BX is used.  */
19798           fprintf (f, "}\n");
19799
19800           thumb_exit (f, -1);
19801
19802           return;
19803         }
19804       else
19805         {
19806           if (mask & 0xFF)
19807             fprintf (f, ", ");
19808
19809           asm_fprintf (f, "%r", PC_REGNUM);
19810         }
19811     }
19812
19813   fprintf (f, "}\n");
19814
19815   if (push && pushed_words && dwarf2out_do_frame ())
19816     {
19817       char *l = dwarf2out_cfi_label (false);
19818       int pushed_mask = real_regs;
19819
19820       *cfa_offset += pushed_words * 4;
19821       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
19822
19823       pushed_words = 0;
19824       pushed_mask = real_regs;
19825       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
19826         {
19827           if (pushed_mask & 1)
19828             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
19829         }
19830     }
19831 }
19832
19833 /* Generate code to return from a thumb function.
19834    If 'reg_containing_return_addr' is -1, then the return address is
19835    actually on the stack, at the stack pointer.  */
19836 static void
19837 thumb_exit (FILE *f, int reg_containing_return_addr)
19838 {
19839   unsigned regs_available_for_popping;
19840   unsigned regs_to_pop;
19841   int pops_needed;
19842   unsigned available;
19843   unsigned required;
19844   int mode;
19845   int size;
19846   int restore_a4 = FALSE;
19847
19848   /* Compute the registers we need to pop.  */
19849   regs_to_pop = 0;
19850   pops_needed = 0;
19851
19852   if (reg_containing_return_addr == -1)
19853     {
19854       regs_to_pop |= 1 << LR_REGNUM;
19855       ++pops_needed;
19856     }
19857
19858   if (TARGET_BACKTRACE)
19859     {
19860       /* Restore the (ARM) frame pointer and stack pointer.  */
19861       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
19862       pops_needed += 2;
19863     }
19864
19865   /* If there is nothing to pop then just emit the BX instruction and
19866      return.  */
19867   if (pops_needed == 0)
19868     {
19869       if (crtl->calls_eh_return)
19870         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
19871
19872       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
19873       return;
19874     }
19875   /* Otherwise if we are not supporting interworking and we have not created
19876      a backtrace structure and the function was not entered in ARM mode then
19877      just pop the return address straight into the PC.  */
19878   else if (!TARGET_INTERWORK
19879            && !TARGET_BACKTRACE
19880            && !is_called_in_ARM_mode (current_function_decl)
19881            && !crtl->calls_eh_return)
19882     {
19883       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
19884       return;
19885     }
19886
19887   /* Find out how many of the (return) argument registers we can corrupt.  */
19888   regs_available_for_popping = 0;
19889
19890   /* If returning via __builtin_eh_return, the bottom three registers
19891      all contain information needed for the return.  */
19892   if (crtl->calls_eh_return)
19893     size = 12;
19894   else
19895     {
19896       /* If we can deduce the registers used from the function's
19897          return value.  This is more reliable that examining
19898          df_regs_ever_live_p () because that will be set if the register is
19899          ever used in the function, not just if the register is used
19900          to hold a return value.  */
19901
19902       if (crtl->return_rtx != 0)
19903         mode = GET_MODE (crtl->return_rtx);
19904       else
19905         mode = DECL_MODE (DECL_RESULT (current_function_decl));
19906
19907       size = GET_MODE_SIZE (mode);
19908
19909       if (size == 0)
19910         {
19911           /* In a void function we can use any argument register.
19912              In a function that returns a structure on the stack
19913              we can use the second and third argument registers.  */
19914           if (mode == VOIDmode)
19915             regs_available_for_popping =
19916               (1 << ARG_REGISTER (1))
19917               | (1 << ARG_REGISTER (2))
19918               | (1 << ARG_REGISTER (3));
19919           else
19920             regs_available_for_popping =
19921               (1 << ARG_REGISTER (2))
19922               | (1 << ARG_REGISTER (3));
19923         }
19924       else if (size <= 4)
19925         regs_available_for_popping =
19926           (1 << ARG_REGISTER (2))
19927           | (1 << ARG_REGISTER (3));
19928       else if (size <= 8)
19929         regs_available_for_popping =
19930           (1 << ARG_REGISTER (3));
19931     }
19932
19933   /* Match registers to be popped with registers into which we pop them.  */
19934   for (available = regs_available_for_popping,
19935        required  = regs_to_pop;
19936        required != 0 && available != 0;
19937        available &= ~(available & - available),
19938        required  &= ~(required  & - required))
19939     -- pops_needed;
19940
19941   /* If we have any popping registers left over, remove them.  */
19942   if (available > 0)
19943     regs_available_for_popping &= ~available;
19944
19945   /* Otherwise if we need another popping register we can use
19946      the fourth argument register.  */
19947   else if (pops_needed)
19948     {
19949       /* If we have not found any free argument registers and
19950          reg a4 contains the return address, we must move it.  */
19951       if (regs_available_for_popping == 0
19952           && reg_containing_return_addr == LAST_ARG_REGNUM)
19953         {
19954           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
19955           reg_containing_return_addr = LR_REGNUM;
19956         }
19957       else if (size > 12)
19958         {
19959           /* Register a4 is being used to hold part of the return value,
19960              but we have dire need of a free, low register.  */
19961           restore_a4 = TRUE;
19962
19963           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
19964         }
19965
19966       if (reg_containing_return_addr != LAST_ARG_REGNUM)
19967         {
19968           /* The fourth argument register is available.  */
19969           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
19970
19971           --pops_needed;
19972         }
19973     }
19974
19975   /* Pop as many registers as we can.  */
19976   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
19977                  regs_available_for_popping);
19978
19979   /* Process the registers we popped.  */
19980   if (reg_containing_return_addr == -1)
19981     {
19982       /* The return address was popped into the lowest numbered register.  */
19983       regs_to_pop &= ~(1 << LR_REGNUM);
19984
19985       reg_containing_return_addr =
19986         number_of_first_bit_set (regs_available_for_popping);
19987
19988       /* Remove this register for the mask of available registers, so that
19989          the return address will not be corrupted by further pops.  */
19990       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
19991     }
19992
19993   /* If we popped other registers then handle them here.  */
19994   if (regs_available_for_popping)
19995     {
19996       int frame_pointer;
19997
19998       /* Work out which register currently contains the frame pointer.  */
19999       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
20000
20001       /* Move it into the correct place.  */
20002       asm_fprintf (f, "\tmov\t%r, %r\n",
20003                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
20004
20005       /* (Temporarily) remove it from the mask of popped registers.  */
20006       regs_available_for_popping &= ~(1 << frame_pointer);
20007       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
20008
20009       if (regs_available_for_popping)
20010         {
20011           int stack_pointer;
20012
20013           /* We popped the stack pointer as well,
20014              find the register that contains it.  */
20015           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
20016
20017           /* Move it into the stack register.  */
20018           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
20019
20020           /* At this point we have popped all necessary registers, so
20021              do not worry about restoring regs_available_for_popping
20022              to its correct value:
20023
20024              assert (pops_needed == 0)
20025              assert (regs_available_for_popping == (1 << frame_pointer))
20026              assert (regs_to_pop == (1 << STACK_POINTER))  */
20027         }
20028       else
20029         {
20030           /* Since we have just move the popped value into the frame
20031              pointer, the popping register is available for reuse, and
20032              we know that we still have the stack pointer left to pop.  */
20033           regs_available_for_popping |= (1 << frame_pointer);
20034         }
20035     }
20036
20037   /* If we still have registers left on the stack, but we no longer have
20038      any registers into which we can pop them, then we must move the return
20039      address into the link register and make available the register that
20040      contained it.  */
20041   if (regs_available_for_popping == 0 && pops_needed > 0)
20042     {
20043       regs_available_for_popping |= 1 << reg_containing_return_addr;
20044
20045       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
20046                    reg_containing_return_addr);
20047
20048       reg_containing_return_addr = LR_REGNUM;
20049     }
20050
20051   /* If we have registers left on the stack then pop some more.
20052      We know that at most we will want to pop FP and SP.  */
20053   if (pops_needed > 0)
20054     {
20055       int  popped_into;
20056       int  move_to;
20057
20058       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
20059                      regs_available_for_popping);
20060
20061       /* We have popped either FP or SP.
20062          Move whichever one it is into the correct register.  */
20063       popped_into = number_of_first_bit_set (regs_available_for_popping);
20064       move_to     = number_of_first_bit_set (regs_to_pop);
20065
20066       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
20067
20068       regs_to_pop &= ~(1 << move_to);
20069
20070       --pops_needed;
20071     }
20072
20073   /* If we still have not popped everything then we must have only
20074      had one register available to us and we are now popping the SP.  */
20075   if (pops_needed > 0)
20076     {
20077       int  popped_into;
20078
20079       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
20080                      regs_available_for_popping);
20081
20082       popped_into = number_of_first_bit_set (regs_available_for_popping);
20083
20084       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
20085       /*
20086         assert (regs_to_pop == (1 << STACK_POINTER))
20087         assert (pops_needed == 1)
20088       */
20089     }
20090
20091   /* If necessary restore the a4 register.  */
20092   if (restore_a4)
20093     {
20094       if (reg_containing_return_addr != LR_REGNUM)
20095         {
20096           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
20097           reg_containing_return_addr = LR_REGNUM;
20098         }
20099
20100       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
20101     }
20102
20103   if (crtl->calls_eh_return)
20104     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
20105
20106   /* Return to caller.  */
20107   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
20108 }
20109 \f
20110 /* Scan INSN just before assembler is output for it.
20111    For Thumb-1, we track the status of the condition codes; this
20112    information is used in the cbranchsi4_insn pattern.  */
20113 void
20114 thumb1_final_prescan_insn (rtx insn)
20115 {
20116   if (flag_print_asm_name)
20117     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
20118                  INSN_ADDRESSES (INSN_UID (insn)));
20119   /* Don't overwrite the previous setter when we get to a cbranch.  */
20120   if (INSN_CODE (insn) != CODE_FOR_cbranchsi4_insn)
20121     {
20122       enum attr_conds conds;
20123
20124       if (cfun->machine->thumb1_cc_insn)
20125         {
20126           if (modified_in_p (cfun->machine->thumb1_cc_op0, insn)
20127               || modified_in_p (cfun->machine->thumb1_cc_op1, insn))
20128             CC_STATUS_INIT;
20129         }
20130       conds = get_attr_conds (insn);
20131       if (conds == CONDS_SET)
20132         {
20133           rtx set = single_set (insn);
20134           cfun->machine->thumb1_cc_insn = insn;
20135           cfun->machine->thumb1_cc_op0 = SET_DEST (set);
20136           cfun->machine->thumb1_cc_op1 = const0_rtx;
20137           cfun->machine->thumb1_cc_mode = CC_NOOVmode;
20138           if (INSN_CODE (insn) == CODE_FOR_thumb1_subsi3_insn)
20139             {
20140               rtx src1 = XEXP (SET_SRC (set), 1);
20141               if (src1 == const0_rtx)
20142                 cfun->machine->thumb1_cc_mode = CCmode;
20143             }
20144         }
20145       else if (conds != CONDS_NOCOND)
20146         cfun->machine->thumb1_cc_insn = NULL_RTX;
20147     }
20148 }
20149
20150 int
20151 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
20152 {
20153   unsigned HOST_WIDE_INT mask = 0xff;
20154   int i;
20155
20156   val = val & (unsigned HOST_WIDE_INT)0xffffffffu;
20157   if (val == 0) /* XXX */
20158     return 0;
20159
20160   for (i = 0; i < 25; i++)
20161     if ((val & (mask << i)) == val)
20162       return 1;
20163
20164   return 0;
20165 }
20166
20167 /* Returns nonzero if the current function contains,
20168    or might contain a far jump.  */
20169 static int
20170 thumb_far_jump_used_p (void)
20171 {
20172   rtx insn;
20173
20174   /* This test is only important for leaf functions.  */
20175   /* assert (!leaf_function_p ()); */
20176
20177   /* If we have already decided that far jumps may be used,
20178      do not bother checking again, and always return true even if
20179      it turns out that they are not being used.  Once we have made
20180      the decision that far jumps are present (and that hence the link
20181      register will be pushed onto the stack) we cannot go back on it.  */
20182   if (cfun->machine->far_jump_used)
20183     return 1;
20184
20185   /* If this function is not being called from the prologue/epilogue
20186      generation code then it must be being called from the
20187      INITIAL_ELIMINATION_OFFSET macro.  */
20188   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
20189     {
20190       /* In this case we know that we are being asked about the elimination
20191          of the arg pointer register.  If that register is not being used,
20192          then there are no arguments on the stack, and we do not have to
20193          worry that a far jump might force the prologue to push the link
20194          register, changing the stack offsets.  In this case we can just
20195          return false, since the presence of far jumps in the function will
20196          not affect stack offsets.
20197
20198          If the arg pointer is live (or if it was live, but has now been
20199          eliminated and so set to dead) then we do have to test to see if
20200          the function might contain a far jump.  This test can lead to some
20201          false negatives, since before reload is completed, then length of
20202          branch instructions is not known, so gcc defaults to returning their
20203          longest length, which in turn sets the far jump attribute to true.
20204
20205          A false negative will not result in bad code being generated, but it
20206          will result in a needless push and pop of the link register.  We
20207          hope that this does not occur too often.
20208
20209          If we need doubleword stack alignment this could affect the other
20210          elimination offsets so we can't risk getting it wrong.  */
20211       if (df_regs_ever_live_p (ARG_POINTER_REGNUM))
20212         cfun->machine->arg_pointer_live = 1;
20213       else if (!cfun->machine->arg_pointer_live)
20214         return 0;
20215     }
20216
20217   /* Check to see if the function contains a branch
20218      insn with the far jump attribute set.  */
20219   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
20220     {
20221       if (GET_CODE (insn) == JUMP_INSN
20222           /* Ignore tablejump patterns.  */
20223           && GET_CODE (PATTERN (insn)) != ADDR_VEC
20224           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
20225           && get_attr_far_jump (insn) == FAR_JUMP_YES
20226           )
20227         {
20228           /* Record the fact that we have decided that
20229              the function does use far jumps.  */
20230           cfun->machine->far_jump_used = 1;
20231           return 1;
20232         }
20233     }
20234
20235   return 0;
20236 }
20237
20238 /* Return nonzero if FUNC must be entered in ARM mode.  */
20239 int
20240 is_called_in_ARM_mode (tree func)
20241 {
20242   gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
20243
20244   /* Ignore the problem about functions whose address is taken.  */
20245   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
20246     return TRUE;
20247
20248 #ifdef ARM_PE
20249   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
20250 #else
20251   return FALSE;
20252 #endif
20253 }
20254
20255 /* Given the stack offsets and register mask in OFFSETS, decide how
20256    many additional registers to push instead of subtracting a constant
20257    from SP.  For epilogues the principle is the same except we use pop.
20258    FOR_PROLOGUE indicates which we're generating.  */
20259 static int
20260 thumb1_extra_regs_pushed (arm_stack_offsets *offsets, bool for_prologue)
20261 {
20262   HOST_WIDE_INT amount;
20263   unsigned long live_regs_mask = offsets->saved_regs_mask;
20264   /* Extract a mask of the ones we can give to the Thumb's push/pop
20265      instruction.  */
20266   unsigned long l_mask = live_regs_mask & (for_prologue ? 0x40ff : 0xff);
20267   /* Then count how many other high registers will need to be pushed.  */
20268   unsigned long high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
20269   int n_free, reg_base;
20270
20271   if (!for_prologue && frame_pointer_needed)
20272     amount = offsets->locals_base - offsets->saved_regs;
20273   else
20274     amount = offsets->outgoing_args - offsets->saved_regs;
20275
20276   /* If the stack frame size is 512 exactly, we can save one load
20277      instruction, which should make this a win even when optimizing
20278      for speed.  */
20279   if (!optimize_size && amount != 512)
20280     return 0;
20281
20282   /* Can't do this if there are high registers to push.  */
20283   if (high_regs_pushed != 0)
20284     return 0;
20285
20286   /* Shouldn't do it in the prologue if no registers would normally
20287      be pushed at all.  In the epilogue, also allow it if we'll have
20288      a pop insn for the PC.  */
20289   if  (l_mask == 0
20290        && (for_prologue
20291            || TARGET_BACKTRACE
20292            || (live_regs_mask & 1 << LR_REGNUM) == 0
20293            || TARGET_INTERWORK
20294            || crtl->args.pretend_args_size != 0))
20295     return 0;
20296
20297   /* Don't do this if thumb_expand_prologue wants to emit instructions
20298      between the push and the stack frame allocation.  */
20299   if (for_prologue
20300       && ((flag_pic && arm_pic_register != INVALID_REGNUM)
20301           || (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)))
20302     return 0;
20303
20304   reg_base = 0;
20305   n_free = 0;
20306   if (!for_prologue)
20307     {
20308       reg_base = arm_size_return_regs () / UNITS_PER_WORD;
20309       live_regs_mask >>= reg_base;
20310     }
20311
20312   while (reg_base + n_free < 8 && !(live_regs_mask & 1)
20313          && (for_prologue || call_used_regs[reg_base + n_free]))
20314     {
20315       live_regs_mask >>= 1;
20316       n_free++;
20317     }
20318
20319   if (n_free == 0)
20320     return 0;
20321   gcc_assert (amount / 4 * 4 == amount);
20322
20323   if (amount >= 512 && (amount - n_free * 4) < 512)
20324     return (amount - 508) / 4;
20325   if (amount <= n_free * 4)
20326     return amount / 4;
20327   return 0;
20328 }
20329
20330 /* The bits which aren't usefully expanded as rtl.  */
20331 const char *
20332 thumb_unexpanded_epilogue (void)
20333 {
20334   arm_stack_offsets *offsets;
20335   int regno;
20336   unsigned long live_regs_mask = 0;
20337   int high_regs_pushed = 0;
20338   int extra_pop;
20339   int had_to_push_lr;
20340   int size;
20341
20342   if (cfun->machine->return_used_this_function != 0)
20343     return "";
20344
20345   if (IS_NAKED (arm_current_func_type ()))
20346     return "";
20347
20348   offsets = arm_get_frame_offsets ();
20349   live_regs_mask = offsets->saved_regs_mask;
20350   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
20351
20352   /* If we can deduce the registers used from the function's return value.
20353      This is more reliable that examining df_regs_ever_live_p () because that
20354      will be set if the register is ever used in the function, not just if
20355      the register is used to hold a return value.  */
20356   size = arm_size_return_regs ();
20357
20358   extra_pop = thumb1_extra_regs_pushed (offsets, false);
20359   if (extra_pop > 0)
20360     {
20361       unsigned long extra_mask = (1 << extra_pop) - 1;
20362       live_regs_mask |= extra_mask << (size / UNITS_PER_WORD);
20363     }
20364
20365   /* The prolog may have pushed some high registers to use as
20366      work registers.  e.g. the testsuite file:
20367      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
20368      compiles to produce:
20369         push    {r4, r5, r6, r7, lr}
20370         mov     r7, r9
20371         mov     r6, r8
20372         push    {r6, r7}
20373      as part of the prolog.  We have to undo that pushing here.  */
20374
20375   if (high_regs_pushed)
20376     {
20377       unsigned long mask = live_regs_mask & 0xff;
20378       int next_hi_reg;
20379
20380       /* The available low registers depend on the size of the value we are
20381          returning.  */
20382       if (size <= 12)
20383         mask |=  1 << 3;
20384       if (size <= 8)
20385         mask |= 1 << 2;
20386
20387       if (mask == 0)
20388         /* Oh dear!  We have no low registers into which we can pop
20389            high registers!  */
20390         internal_error
20391           ("no low registers available for popping high registers");
20392
20393       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
20394         if (live_regs_mask & (1 << next_hi_reg))
20395           break;
20396
20397       while (high_regs_pushed)
20398         {
20399           /* Find lo register(s) into which the high register(s) can
20400              be popped.  */
20401           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
20402             {
20403               if (mask & (1 << regno))
20404                 high_regs_pushed--;
20405               if (high_regs_pushed == 0)
20406                 break;
20407             }
20408
20409           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
20410
20411           /* Pop the values into the low register(s).  */
20412           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
20413
20414           /* Move the value(s) into the high registers.  */
20415           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
20416             {
20417               if (mask & (1 << regno))
20418                 {
20419                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
20420                                regno);
20421
20422                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
20423                     if (live_regs_mask & (1 << next_hi_reg))
20424                       break;
20425                 }
20426             }
20427         }
20428       live_regs_mask &= ~0x0f00;
20429     }
20430
20431   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
20432   live_regs_mask &= 0xff;
20433
20434   if (crtl->args.pretend_args_size == 0 || TARGET_BACKTRACE)
20435     {
20436       /* Pop the return address into the PC.  */
20437       if (had_to_push_lr)
20438         live_regs_mask |= 1 << PC_REGNUM;
20439
20440       /* Either no argument registers were pushed or a backtrace
20441          structure was created which includes an adjusted stack
20442          pointer, so just pop everything.  */
20443       if (live_regs_mask)
20444         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
20445                        live_regs_mask);
20446
20447       /* We have either just popped the return address into the
20448          PC or it is was kept in LR for the entire function.
20449          Note that thumb_pushpop has already called thumb_exit if the
20450          PC was in the list.  */
20451       if (!had_to_push_lr)
20452         thumb_exit (asm_out_file, LR_REGNUM);
20453     }
20454   else
20455     {
20456       /* Pop everything but the return address.  */
20457       if (live_regs_mask)
20458         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
20459                        live_regs_mask);
20460
20461       if (had_to_push_lr)
20462         {
20463           if (size > 12)
20464             {
20465               /* We have no free low regs, so save one.  */
20466               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
20467                            LAST_ARG_REGNUM);
20468             }
20469
20470           /* Get the return address into a temporary register.  */
20471           thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
20472                          1 << LAST_ARG_REGNUM);
20473
20474           if (size > 12)
20475             {
20476               /* Move the return address to lr.  */
20477               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
20478                            LAST_ARG_REGNUM);
20479               /* Restore the low register.  */
20480               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
20481                            IP_REGNUM);
20482               regno = LR_REGNUM;
20483             }
20484           else
20485             regno = LAST_ARG_REGNUM;
20486         }
20487       else
20488         regno = LR_REGNUM;
20489
20490       /* Remove the argument registers that were pushed onto the stack.  */
20491       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
20492                    SP_REGNUM, SP_REGNUM,
20493                    crtl->args.pretend_args_size);
20494
20495       thumb_exit (asm_out_file, regno);
20496     }
20497
20498   return "";
20499 }
20500
20501 /* Functions to save and restore machine-specific function data.  */
20502 static struct machine_function *
20503 arm_init_machine_status (void)
20504 {
20505   struct machine_function *machine;
20506   machine = ggc_alloc_cleared_machine_function ();
20507
20508 #if ARM_FT_UNKNOWN != 0
20509   machine->func_type = ARM_FT_UNKNOWN;
20510 #endif
20511   return machine;
20512 }
20513
20514 /* Return an RTX indicating where the return address to the
20515    calling function can be found.  */
20516 rtx
20517 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
20518 {
20519   if (count != 0)
20520     return NULL_RTX;
20521
20522   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
20523 }
20524
20525 /* Do anything needed before RTL is emitted for each function.  */
20526 void
20527 arm_init_expanders (void)
20528 {
20529   /* Arrange to initialize and mark the machine per-function status.  */
20530   init_machine_status = arm_init_machine_status;
20531
20532   /* This is to stop the combine pass optimizing away the alignment
20533      adjustment of va_arg.  */
20534   /* ??? It is claimed that this should not be necessary.  */
20535   if (cfun)
20536     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
20537 }
20538
20539
20540 /* Like arm_compute_initial_elimination offset.  Simpler because there
20541    isn't an ABI specified frame pointer for Thumb.  Instead, we set it
20542    to point at the base of the local variables after static stack
20543    space for a function has been allocated.  */
20544
20545 HOST_WIDE_INT
20546 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
20547 {
20548   arm_stack_offsets *offsets;
20549
20550   offsets = arm_get_frame_offsets ();
20551
20552   switch (from)
20553     {
20554     case ARG_POINTER_REGNUM:
20555       switch (to)
20556         {
20557         case STACK_POINTER_REGNUM:
20558           return offsets->outgoing_args - offsets->saved_args;
20559
20560         case FRAME_POINTER_REGNUM:
20561           return offsets->soft_frame - offsets->saved_args;
20562
20563         case ARM_HARD_FRAME_POINTER_REGNUM:
20564           return offsets->saved_regs - offsets->saved_args;
20565
20566         case THUMB_HARD_FRAME_POINTER_REGNUM:
20567           return offsets->locals_base - offsets->saved_args;
20568
20569         default:
20570           gcc_unreachable ();
20571         }
20572       break;
20573
20574     case FRAME_POINTER_REGNUM:
20575       switch (to)
20576         {
20577         case STACK_POINTER_REGNUM:
20578           return offsets->outgoing_args - offsets->soft_frame;
20579
20580         case ARM_HARD_FRAME_POINTER_REGNUM:
20581           return offsets->saved_regs - offsets->soft_frame;
20582
20583         case THUMB_HARD_FRAME_POINTER_REGNUM:
20584           return offsets->locals_base - offsets->soft_frame;
20585
20586         default:
20587           gcc_unreachable ();
20588         }
20589       break;
20590
20591     default:
20592       gcc_unreachable ();
20593     }
20594 }
20595
20596 /* Generate the rest of a function's prologue.  */
20597 void
20598 thumb1_expand_prologue (void)
20599 {
20600   rtx insn, dwarf;
20601
20602   HOST_WIDE_INT amount;
20603   arm_stack_offsets *offsets;
20604   unsigned long func_type;
20605   int regno;
20606   unsigned long live_regs_mask;
20607
20608   func_type = arm_current_func_type ();
20609
20610   /* Naked functions don't have prologues.  */
20611   if (IS_NAKED (func_type))
20612     return;
20613
20614   if (IS_INTERRUPT (func_type))
20615     {
20616       error ("interrupt Service Routines cannot be coded in Thumb mode");
20617       return;
20618     }
20619
20620   offsets = arm_get_frame_offsets ();
20621   live_regs_mask = offsets->saved_regs_mask;
20622   /* Load the pic register before setting the frame pointer,
20623      so we can use r7 as a temporary work register.  */
20624   if (flag_pic && arm_pic_register != INVALID_REGNUM)
20625     arm_load_pic_register (live_regs_mask);
20626
20627   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
20628     emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
20629                     stack_pointer_rtx);
20630
20631   amount = offsets->outgoing_args - offsets->saved_regs;
20632   amount -= 4 * thumb1_extra_regs_pushed (offsets, true);
20633   if (amount)
20634     {
20635       if (amount < 512)
20636         {
20637           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
20638                                         GEN_INT (- amount)));
20639           RTX_FRAME_RELATED_P (insn) = 1;
20640         }
20641       else
20642         {
20643           rtx reg;
20644
20645           /* The stack decrement is too big for an immediate value in a single
20646              insn.  In theory we could issue multiple subtracts, but after
20647              three of them it becomes more space efficient to place the full
20648              value in the constant pool and load into a register.  (Also the
20649              ARM debugger really likes to see only one stack decrement per
20650              function).  So instead we look for a scratch register into which
20651              we can load the decrement, and then we subtract this from the
20652              stack pointer.  Unfortunately on the thumb the only available
20653              scratch registers are the argument registers, and we cannot use
20654              these as they may hold arguments to the function.  Instead we
20655              attempt to locate a call preserved register which is used by this
20656              function.  If we can find one, then we know that it will have
20657              been pushed at the start of the prologue and so we can corrupt
20658              it now.  */
20659           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
20660             if (live_regs_mask & (1 << regno))
20661               break;
20662
20663           gcc_assert(regno <= LAST_LO_REGNUM);
20664
20665           reg = gen_rtx_REG (SImode, regno);
20666
20667           emit_insn (gen_movsi (reg, GEN_INT (- amount)));
20668
20669           insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
20670                                         stack_pointer_rtx, reg));
20671           RTX_FRAME_RELATED_P (insn) = 1;
20672           dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
20673                                plus_constant (stack_pointer_rtx,
20674                                               -amount));
20675           RTX_FRAME_RELATED_P (dwarf) = 1;
20676           add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
20677         }
20678     }
20679
20680   if (frame_pointer_needed)
20681     thumb_set_frame_pointer (offsets);
20682
20683   /* If we are profiling, make sure no instructions are scheduled before
20684      the call to mcount.  Similarly if the user has requested no
20685      scheduling in the prolog.  Similarly if we want non-call exceptions
20686      using the EABI unwinder, to prevent faulting instructions from being
20687      swapped with a stack adjustment.  */
20688   if (crtl->profile || !TARGET_SCHED_PROLOG
20689       || (arm_except_unwind_info (&global_options) == UI_TARGET
20690           && cfun->can_throw_non_call_exceptions))
20691     emit_insn (gen_blockage ());
20692
20693   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
20694   if (live_regs_mask & 0xff)
20695     cfun->machine->lr_save_eliminated = 0;
20696 }
20697
20698
20699 void
20700 thumb1_expand_epilogue (void)
20701 {
20702   HOST_WIDE_INT amount;
20703   arm_stack_offsets *offsets;
20704   int regno;
20705
20706   /* Naked functions don't have prologues.  */
20707   if (IS_NAKED (arm_current_func_type ()))
20708     return;
20709
20710   offsets = arm_get_frame_offsets ();
20711   amount = offsets->outgoing_args - offsets->saved_regs;
20712
20713   if (frame_pointer_needed)
20714     {
20715       emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
20716       amount = offsets->locals_base - offsets->saved_regs;
20717     }
20718   amount -= 4 * thumb1_extra_regs_pushed (offsets, false);
20719
20720   gcc_assert (amount >= 0);
20721   if (amount)
20722     {
20723       if (amount < 512)
20724         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
20725                                GEN_INT (amount)));
20726       else
20727         {
20728           /* r3 is always free in the epilogue.  */
20729           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
20730
20731           emit_insn (gen_movsi (reg, GEN_INT (amount)));
20732           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
20733         }
20734     }
20735
20736   /* Emit a USE (stack_pointer_rtx), so that
20737      the stack adjustment will not be deleted.  */
20738   emit_insn (gen_prologue_use (stack_pointer_rtx));
20739
20740   if (crtl->profile || !TARGET_SCHED_PROLOG)
20741     emit_insn (gen_blockage ());
20742
20743   /* Emit a clobber for each insn that will be restored in the epilogue,
20744      so that flow2 will get register lifetimes correct.  */
20745   for (regno = 0; regno < 13; regno++)
20746     if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
20747       emit_clobber (gen_rtx_REG (SImode, regno));
20748
20749   if (! df_regs_ever_live_p (LR_REGNUM))
20750     emit_use (gen_rtx_REG (SImode, LR_REGNUM));
20751 }
20752
20753 static void
20754 thumb1_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
20755 {
20756   arm_stack_offsets *offsets;
20757   unsigned long live_regs_mask = 0;
20758   unsigned long l_mask;
20759   unsigned high_regs_pushed = 0;
20760   int cfa_offset = 0;
20761   int regno;
20762
20763   if (IS_NAKED (arm_current_func_type ()))
20764     return;
20765
20766   if (is_called_in_ARM_mode (current_function_decl))
20767     {
20768       const char * name;
20769
20770       gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
20771       gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
20772                   == SYMBOL_REF);
20773       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
20774
20775       /* Generate code sequence to switch us into Thumb mode.  */
20776       /* The .code 32 directive has already been emitted by
20777          ASM_DECLARE_FUNCTION_NAME.  */
20778       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
20779       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
20780
20781       /* Generate a label, so that the debugger will notice the
20782          change in instruction sets.  This label is also used by
20783          the assembler to bypass the ARM code when this function
20784          is called from a Thumb encoded function elsewhere in the
20785          same file.  Hence the definition of STUB_NAME here must
20786          agree with the definition in gas/config/tc-arm.c.  */
20787
20788 #define STUB_NAME ".real_start_of"
20789
20790       fprintf (f, "\t.code\t16\n");
20791 #ifdef ARM_PE
20792       if (arm_dllexport_name_p (name))
20793         name = arm_strip_name_encoding (name);
20794 #endif
20795       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
20796       fprintf (f, "\t.thumb_func\n");
20797       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
20798     }
20799
20800   if (crtl->args.pretend_args_size)
20801     {
20802       /* Output unwind directive for the stack adjustment.  */
20803       if (arm_except_unwind_info (&global_options) == UI_TARGET)
20804         fprintf (f, "\t.pad #%d\n",
20805                  crtl->args.pretend_args_size);
20806
20807       if (cfun->machine->uses_anonymous_args)
20808         {
20809           int num_pushes;
20810
20811           fprintf (f, "\tpush\t{");
20812
20813           num_pushes = ARM_NUM_INTS (crtl->args.pretend_args_size);
20814
20815           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
20816                regno <= LAST_ARG_REGNUM;
20817                regno++)
20818             asm_fprintf (f, "%r%s", regno,
20819                          regno == LAST_ARG_REGNUM ? "" : ", ");
20820
20821           fprintf (f, "}\n");
20822         }
20823       else
20824         asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
20825                      SP_REGNUM, SP_REGNUM,
20826                      crtl->args.pretend_args_size);
20827
20828       /* We don't need to record the stores for unwinding (would it
20829          help the debugger any if we did?), but record the change in
20830          the stack pointer.  */
20831       if (dwarf2out_do_frame ())
20832         {
20833           char *l = dwarf2out_cfi_label (false);
20834
20835           cfa_offset = cfa_offset + crtl->args.pretend_args_size;
20836           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
20837         }
20838     }
20839
20840   /* Get the registers we are going to push.  */
20841   offsets = arm_get_frame_offsets ();
20842   live_regs_mask = offsets->saved_regs_mask;
20843   /* Extract a mask of the ones we can give to the Thumb's push instruction.  */
20844   l_mask = live_regs_mask & 0x40ff;
20845   /* Then count how many other high registers will need to be pushed.  */
20846   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
20847
20848   if (TARGET_BACKTRACE)
20849     {
20850       unsigned offset;
20851       unsigned work_register;
20852
20853       /* We have been asked to create a stack backtrace structure.
20854          The code looks like this:
20855
20856          0   .align 2
20857          0   func:
20858          0     sub   SP, #16         Reserve space for 4 registers.
20859          2     push  {R7}            Push low registers.
20860          4     add   R7, SP, #20     Get the stack pointer before the push.
20861          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
20862          8     mov   R7, PC          Get hold of the start of this code plus 12.
20863         10     str   R7, [SP, #16]   Store it.
20864         12     mov   R7, FP          Get hold of the current frame pointer.
20865         14     str   R7, [SP, #4]    Store it.
20866         16     mov   R7, LR          Get hold of the current return address.
20867         18     str   R7, [SP, #12]   Store it.
20868         20     add   R7, SP, #16     Point at the start of the backtrace structure.
20869         22     mov   FP, R7          Put this value into the frame pointer.  */
20870
20871       work_register = thumb_find_work_register (live_regs_mask);
20872
20873       if (arm_except_unwind_info (&global_options) == UI_TARGET)
20874         asm_fprintf (f, "\t.pad #16\n");
20875
20876       asm_fprintf
20877         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
20878          SP_REGNUM, SP_REGNUM);
20879
20880       if (dwarf2out_do_frame ())
20881         {
20882           char *l = dwarf2out_cfi_label (false);
20883
20884           cfa_offset = cfa_offset + 16;
20885           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
20886         }
20887
20888       if (l_mask)
20889         {
20890           thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
20891           offset = bit_count (l_mask) * UNITS_PER_WORD;
20892         }
20893       else
20894         offset = 0;
20895
20896       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
20897                    offset + 16 + crtl->args.pretend_args_size);
20898
20899       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20900                    offset + 4);
20901
20902       /* Make sure that the instruction fetching the PC is in the right place
20903          to calculate "start of backtrace creation code + 12".  */
20904       if (l_mask)
20905         {
20906           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
20907           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20908                        offset + 12);
20909           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
20910                        ARM_HARD_FRAME_POINTER_REGNUM);
20911           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20912                        offset);
20913         }
20914       else
20915         {
20916           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
20917                        ARM_HARD_FRAME_POINTER_REGNUM);
20918           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20919                        offset);
20920           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
20921           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20922                        offset + 12);
20923         }
20924
20925       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
20926       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
20927                    offset + 8);
20928       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
20929                    offset + 12);
20930       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
20931                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
20932     }
20933   /* Optimization:  If we are not pushing any low registers but we are going
20934      to push some high registers then delay our first push.  This will just
20935      be a push of LR and we can combine it with the push of the first high
20936      register.  */
20937   else if ((l_mask & 0xff) != 0
20938            || (high_regs_pushed == 0 && l_mask))
20939     {
20940       unsigned long mask = l_mask;
20941       mask |= (1 << thumb1_extra_regs_pushed (offsets, true)) - 1;
20942       thumb_pushpop (f, mask, 1, &cfa_offset, mask);
20943     }
20944
20945   if (high_regs_pushed)
20946     {
20947       unsigned pushable_regs;
20948       unsigned next_hi_reg;
20949
20950       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
20951         if (live_regs_mask & (1 << next_hi_reg))
20952           break;
20953
20954       pushable_regs = l_mask & 0xff;
20955
20956       if (pushable_regs == 0)
20957         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
20958
20959       while (high_regs_pushed > 0)
20960         {
20961           unsigned long real_regs_mask = 0;
20962
20963           for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
20964             {
20965               if (pushable_regs & (1 << regno))
20966                 {
20967                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
20968
20969                   high_regs_pushed --;
20970                   real_regs_mask |= (1 << next_hi_reg);
20971
20972                   if (high_regs_pushed)
20973                     {
20974                       for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
20975                            next_hi_reg --)
20976                         if (live_regs_mask & (1 << next_hi_reg))
20977                           break;
20978                     }
20979                   else
20980                     {
20981                       pushable_regs &= ~((1 << regno) - 1);
20982                       break;
20983                     }
20984                 }
20985             }
20986
20987           /* If we had to find a work register and we have not yet
20988              saved the LR then add it to the list of regs to push.  */
20989           if (l_mask == (1 << LR_REGNUM))
20990             {
20991               thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
20992                              1, &cfa_offset,
20993                              real_regs_mask | (1 << LR_REGNUM));
20994               l_mask = 0;
20995             }
20996           else
20997             thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
20998         }
20999     }
21000 }
21001
21002 /* Handle the case of a double word load into a low register from
21003    a computed memory address.  The computed address may involve a
21004    register which is overwritten by the load.  */
21005 const char *
21006 thumb_load_double_from_address (rtx *operands)
21007 {
21008   rtx addr;
21009   rtx base;
21010   rtx offset;
21011   rtx arg1;
21012   rtx arg2;
21013
21014   gcc_assert (GET_CODE (operands[0]) == REG);
21015   gcc_assert (GET_CODE (operands[1]) == MEM);
21016
21017   /* Get the memory address.  */
21018   addr = XEXP (operands[1], 0);
21019
21020   /* Work out how the memory address is computed.  */
21021   switch (GET_CODE (addr))
21022     {
21023     case REG:
21024       operands[2] = adjust_address (operands[1], SImode, 4);
21025
21026       if (REGNO (operands[0]) == REGNO (addr))
21027         {
21028           output_asm_insn ("ldr\t%H0, %2", operands);
21029           output_asm_insn ("ldr\t%0, %1", operands);
21030         }
21031       else
21032         {
21033           output_asm_insn ("ldr\t%0, %1", operands);
21034           output_asm_insn ("ldr\t%H0, %2", operands);
21035         }
21036       break;
21037
21038     case CONST:
21039       /* Compute <address> + 4 for the high order load.  */
21040       operands[2] = adjust_address (operands[1], SImode, 4);
21041
21042       output_asm_insn ("ldr\t%0, %1", operands);
21043       output_asm_insn ("ldr\t%H0, %2", operands);
21044       break;
21045
21046     case PLUS:
21047       arg1   = XEXP (addr, 0);
21048       arg2   = XEXP (addr, 1);
21049
21050       if (CONSTANT_P (arg1))
21051         base = arg2, offset = arg1;
21052       else
21053         base = arg1, offset = arg2;
21054
21055       gcc_assert (GET_CODE (base) == REG);
21056
21057       /* Catch the case of <address> = <reg> + <reg> */
21058       if (GET_CODE (offset) == REG)
21059         {
21060           int reg_offset = REGNO (offset);
21061           int reg_base   = REGNO (base);
21062           int reg_dest   = REGNO (operands[0]);
21063
21064           /* Add the base and offset registers together into the
21065              higher destination register.  */
21066           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
21067                        reg_dest + 1, reg_base, reg_offset);
21068
21069           /* Load the lower destination register from the address in
21070              the higher destination register.  */
21071           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
21072                        reg_dest, reg_dest + 1);
21073
21074           /* Load the higher destination register from its own address
21075              plus 4.  */
21076           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
21077                        reg_dest + 1, reg_dest + 1);
21078         }
21079       else
21080         {
21081           /* Compute <address> + 4 for the high order load.  */
21082           operands[2] = adjust_address (operands[1], SImode, 4);
21083
21084           /* If the computed address is held in the low order register
21085              then load the high order register first, otherwise always
21086              load the low order register first.  */
21087           if (REGNO (operands[0]) == REGNO (base))
21088             {
21089               output_asm_insn ("ldr\t%H0, %2", operands);
21090               output_asm_insn ("ldr\t%0, %1", operands);
21091             }
21092           else
21093             {
21094               output_asm_insn ("ldr\t%0, %1", operands);
21095               output_asm_insn ("ldr\t%H0, %2", operands);
21096             }
21097         }
21098       break;
21099
21100     case LABEL_REF:
21101       /* With no registers to worry about we can just load the value
21102          directly.  */
21103       operands[2] = adjust_address (operands[1], SImode, 4);
21104
21105       output_asm_insn ("ldr\t%H0, %2", operands);
21106       output_asm_insn ("ldr\t%0, %1", operands);
21107       break;
21108
21109     default:
21110       gcc_unreachable ();
21111     }
21112
21113   return "";
21114 }
21115
21116 const char *
21117 thumb_output_move_mem_multiple (int n, rtx *operands)
21118 {
21119   rtx tmp;
21120
21121   switch (n)
21122     {
21123     case 2:
21124       if (REGNO (operands[4]) > REGNO (operands[5]))
21125         {
21126           tmp = operands[4];
21127           operands[4] = operands[5];
21128           operands[5] = tmp;
21129         }
21130       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
21131       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
21132       break;
21133
21134     case 3:
21135       if (REGNO (operands[4]) > REGNO (operands[5]))
21136         {
21137           tmp = operands[4];
21138           operands[4] = operands[5];
21139           operands[5] = tmp;
21140         }
21141       if (REGNO (operands[5]) > REGNO (operands[6]))
21142         {
21143           tmp = operands[5];
21144           operands[5] = operands[6];
21145           operands[6] = tmp;
21146         }
21147       if (REGNO (operands[4]) > REGNO (operands[5]))
21148         {
21149           tmp = operands[4];
21150           operands[4] = operands[5];
21151           operands[5] = tmp;
21152         }
21153
21154       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
21155       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
21156       break;
21157
21158     default:
21159       gcc_unreachable ();
21160     }
21161
21162   return "";
21163 }
21164
21165 /* Output a call-via instruction for thumb state.  */
21166 const char *
21167 thumb_call_via_reg (rtx reg)
21168 {
21169   int regno = REGNO (reg);
21170   rtx *labelp;
21171
21172   gcc_assert (regno < LR_REGNUM);
21173
21174   /* If we are in the normal text section we can use a single instance
21175      per compilation unit.  If we are doing function sections, then we need
21176      an entry per section, since we can't rely on reachability.  */
21177   if (in_section == text_section)
21178     {
21179       thumb_call_reg_needed = 1;
21180
21181       if (thumb_call_via_label[regno] == NULL)
21182         thumb_call_via_label[regno] = gen_label_rtx ();
21183       labelp = thumb_call_via_label + regno;
21184     }
21185   else
21186     {
21187       if (cfun->machine->call_via[regno] == NULL)
21188         cfun->machine->call_via[regno] = gen_label_rtx ();
21189       labelp = cfun->machine->call_via + regno;
21190     }
21191
21192   output_asm_insn ("bl\t%a0", labelp);
21193   return "";
21194 }
21195
21196 /* Routines for generating rtl.  */
21197 void
21198 thumb_expand_movmemqi (rtx *operands)
21199 {
21200   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
21201   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
21202   HOST_WIDE_INT len = INTVAL (operands[2]);
21203   HOST_WIDE_INT offset = 0;
21204
21205   while (len >= 12)
21206     {
21207       emit_insn (gen_movmem12b (out, in, out, in));
21208       len -= 12;
21209     }
21210
21211   if (len >= 8)
21212     {
21213       emit_insn (gen_movmem8b (out, in, out, in));
21214       len -= 8;
21215     }
21216
21217   if (len >= 4)
21218     {
21219       rtx reg = gen_reg_rtx (SImode);
21220       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
21221       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
21222       len -= 4;
21223       offset += 4;
21224     }
21225
21226   if (len >= 2)
21227     {
21228       rtx reg = gen_reg_rtx (HImode);
21229       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
21230                                               plus_constant (in, offset))));
21231       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
21232                             reg));
21233       len -= 2;
21234       offset += 2;
21235     }
21236
21237   if (len)
21238     {
21239       rtx reg = gen_reg_rtx (QImode);
21240       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
21241                                               plus_constant (in, offset))));
21242       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
21243                             reg));
21244     }
21245 }
21246
21247 void
21248 thumb_reload_out_hi (rtx *operands)
21249 {
21250   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
21251 }
21252
21253 /* Handle reading a half-word from memory during reload.  */
21254 void
21255 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
21256 {
21257   gcc_unreachable ();
21258 }
21259
21260 /* Return the length of a function name prefix
21261     that starts with the character 'c'.  */
21262 static int
21263 arm_get_strip_length (int c)
21264 {
21265   switch (c)
21266     {
21267     ARM_NAME_ENCODING_LENGTHS
21268       default: return 0;
21269     }
21270 }
21271
21272 /* Return a pointer to a function's name with any
21273    and all prefix encodings stripped from it.  */
21274 const char *
21275 arm_strip_name_encoding (const char *name)
21276 {
21277   int skip;
21278
21279   while ((skip = arm_get_strip_length (* name)))
21280     name += skip;
21281
21282   return name;
21283 }
21284
21285 /* If there is a '*' anywhere in the name's prefix, then
21286    emit the stripped name verbatim, otherwise prepend an
21287    underscore if leading underscores are being used.  */
21288 void
21289 arm_asm_output_labelref (FILE *stream, const char *name)
21290 {
21291   int skip;
21292   int verbatim = 0;
21293
21294   while ((skip = arm_get_strip_length (* name)))
21295     {
21296       verbatim |= (*name == '*');
21297       name += skip;
21298     }
21299
21300   if (verbatim)
21301     fputs (name, stream);
21302   else
21303     asm_fprintf (stream, "%U%s", name);
21304 }
21305
21306 static void
21307 arm_file_start (void)
21308 {
21309   int val;
21310
21311   if (TARGET_UNIFIED_ASM)
21312     asm_fprintf (asm_out_file, "\t.syntax unified\n");
21313
21314   if (TARGET_BPABI)
21315     {
21316       const char *fpu_name;
21317       if (arm_selected_arch)
21318         asm_fprintf (asm_out_file, "\t.arch %s\n", arm_selected_arch->name);
21319       else
21320         asm_fprintf (asm_out_file, "\t.cpu %s\n", arm_selected_cpu->name);
21321
21322       if (TARGET_SOFT_FLOAT)
21323         {
21324           if (TARGET_VFP)
21325             fpu_name = "softvfp";
21326           else
21327             fpu_name = "softfpa";
21328         }
21329       else
21330         {
21331           fpu_name = arm_fpu_desc->name;
21332           if (arm_fpu_desc->model == ARM_FP_MODEL_VFP)
21333             {
21334               if (TARGET_HARD_FLOAT)
21335                 asm_fprintf (asm_out_file, "\t.eabi_attribute 27, 3\n");
21336               if (TARGET_HARD_FLOAT_ABI)
21337                 asm_fprintf (asm_out_file, "\t.eabi_attribute 28, 1\n");
21338             }
21339         }
21340       asm_fprintf (asm_out_file, "\t.fpu %s\n", fpu_name);
21341
21342       /* Some of these attributes only apply when the corresponding features
21343          are used.  However we don't have any easy way of figuring this out.
21344          Conservatively record the setting that would have been used.  */
21345
21346       /* Tag_ABI_FP_rounding.  */
21347       if (flag_rounding_math)
21348         asm_fprintf (asm_out_file, "\t.eabi_attribute 19, 1\n");
21349       if (!flag_unsafe_math_optimizations)
21350         {
21351           /* Tag_ABI_FP_denomal.  */
21352           asm_fprintf (asm_out_file, "\t.eabi_attribute 20, 1\n");
21353           /* Tag_ABI_FP_exceptions.  */
21354           asm_fprintf (asm_out_file, "\t.eabi_attribute 21, 1\n");
21355         }
21356       /* Tag_ABI_FP_user_exceptions.  */
21357       if (flag_signaling_nans)
21358         asm_fprintf (asm_out_file, "\t.eabi_attribute 22, 1\n");
21359       /* Tag_ABI_FP_number_model.  */
21360       asm_fprintf (asm_out_file, "\t.eabi_attribute 23, %d\n", 
21361                    flag_finite_math_only ? 1 : 3);
21362
21363       /* Tag_ABI_align8_needed.  */
21364       asm_fprintf (asm_out_file, "\t.eabi_attribute 24, 1\n");
21365       /* Tag_ABI_align8_preserved.  */
21366       asm_fprintf (asm_out_file, "\t.eabi_attribute 25, 1\n");
21367       /* Tag_ABI_enum_size.  */
21368       asm_fprintf (asm_out_file, "\t.eabi_attribute 26, %d\n",
21369                    flag_short_enums ? 1 : 2);
21370
21371       /* Tag_ABI_optimization_goals.  */
21372       if (optimize_size)
21373         val = 4;
21374       else if (optimize >= 2)
21375         val = 2;
21376       else if (optimize)
21377         val = 1;
21378       else
21379         val = 6;
21380       asm_fprintf (asm_out_file, "\t.eabi_attribute 30, %d\n", val);
21381
21382       /* Tag_ABI_FP_16bit_format.  */
21383       if (arm_fp16_format)
21384         asm_fprintf (asm_out_file, "\t.eabi_attribute 38, %d\n",
21385                      (int)arm_fp16_format);
21386
21387       if (arm_lang_output_object_attributes_hook)
21388         arm_lang_output_object_attributes_hook();
21389     }
21390   default_file_start();
21391 }
21392
21393 static void
21394 arm_file_end (void)
21395 {
21396   int regno;
21397
21398   if (NEED_INDICATE_EXEC_STACK)
21399     /* Add .note.GNU-stack.  */
21400     file_end_indicate_exec_stack ();
21401
21402   if (! thumb_call_reg_needed)
21403     return;
21404
21405   switch_to_section (text_section);
21406   asm_fprintf (asm_out_file, "\t.code 16\n");
21407   ASM_OUTPUT_ALIGN (asm_out_file, 1);
21408
21409   for (regno = 0; regno < LR_REGNUM; regno++)
21410     {
21411       rtx label = thumb_call_via_label[regno];
21412
21413       if (label != 0)
21414         {
21415           targetm.asm_out.internal_label (asm_out_file, "L",
21416                                           CODE_LABEL_NUMBER (label));
21417           asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
21418         }
21419     }
21420 }
21421
21422 #ifndef ARM_PE
21423 /* Symbols in the text segment can be accessed without indirecting via the
21424    constant pool; it may take an extra binary operation, but this is still
21425    faster than indirecting via memory.  Don't do this when not optimizing,
21426    since we won't be calculating al of the offsets necessary to do this
21427    simplification.  */
21428
21429 static void
21430 arm_encode_section_info (tree decl, rtx rtl, int first)
21431 {
21432   if (optimize > 0 && TREE_CONSTANT (decl))
21433     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
21434
21435   default_encode_section_info (decl, rtl, first);
21436 }
21437 #endif /* !ARM_PE */
21438
21439 static void
21440 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
21441 {
21442   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
21443       && !strcmp (prefix, "L"))
21444     {
21445       arm_ccfsm_state = 0;
21446       arm_target_insn = NULL;
21447     }
21448   default_internal_label (stream, prefix, labelno);
21449 }
21450
21451 /* Output code to add DELTA to the first argument, and then jump
21452    to FUNCTION.  Used for C++ multiple inheritance.  */
21453 static void
21454 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
21455                      HOST_WIDE_INT delta,
21456                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
21457                      tree function)
21458 {
21459   static int thunk_label = 0;
21460   char label[256];
21461   char labelpc[256];
21462   int mi_delta = delta;
21463   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
21464   int shift = 0;
21465   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
21466                     ? 1 : 0);
21467   if (mi_delta < 0)
21468     mi_delta = - mi_delta;
21469
21470   if (TARGET_THUMB1)
21471     {
21472       int labelno = thunk_label++;
21473       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
21474       /* Thunks are entered in arm mode when avaiable.  */
21475       if (TARGET_THUMB1_ONLY)
21476         {
21477           /* push r3 so we can use it as a temporary.  */
21478           /* TODO: Omit this save if r3 is not used.  */
21479           fputs ("\tpush {r3}\n", file);
21480           fputs ("\tldr\tr3, ", file);
21481         }
21482       else
21483         {
21484           fputs ("\tldr\tr12, ", file);
21485         }
21486       assemble_name (file, label);
21487       fputc ('\n', file);
21488       if (flag_pic)
21489         {
21490           /* If we are generating PIC, the ldr instruction below loads
21491              "(target - 7) - .LTHUNKPCn" into r12.  The pc reads as
21492              the address of the add + 8, so we have:
21493
21494              r12 = (target - 7) - .LTHUNKPCn + (.LTHUNKPCn + 8)
21495                  = target + 1.
21496
21497              Note that we have "+ 1" because some versions of GNU ld
21498              don't set the low bit of the result for R_ARM_REL32
21499              relocations against thumb function symbols.
21500              On ARMv6M this is +4, not +8.  */
21501           ASM_GENERATE_INTERNAL_LABEL (labelpc, "LTHUNKPC", labelno);
21502           assemble_name (file, labelpc);
21503           fputs (":\n", file);
21504           if (TARGET_THUMB1_ONLY)
21505             {
21506               /* This is 2 insns after the start of the thunk, so we know it
21507                  is 4-byte aligned.  */
21508               fputs ("\tadd\tr3, pc, r3\n", file);
21509               fputs ("\tmov r12, r3\n", file);
21510             }
21511           else
21512             fputs ("\tadd\tr12, pc, r12\n", file);
21513         }
21514       else if (TARGET_THUMB1_ONLY)
21515         fputs ("\tmov r12, r3\n", file);
21516     }
21517   if (TARGET_THUMB1_ONLY)
21518     {
21519       if (mi_delta > 255)
21520         {
21521           fputs ("\tldr\tr3, ", file);
21522           assemble_name (file, label);
21523           fputs ("+4\n", file);
21524           asm_fprintf (file, "\t%s\t%r, %r, r3\n",
21525                        mi_op, this_regno, this_regno);
21526         }
21527       else if (mi_delta != 0)
21528         {
21529           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
21530                        mi_op, this_regno, this_regno,
21531                        mi_delta);
21532         }
21533     }
21534   else
21535     {
21536       /* TODO: Use movw/movt for large constants when available.  */
21537       while (mi_delta != 0)
21538         {
21539           if ((mi_delta & (3 << shift)) == 0)
21540             shift += 2;
21541           else
21542             {
21543               asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
21544                            mi_op, this_regno, this_regno,
21545                            mi_delta & (0xff << shift));
21546               mi_delta &= ~(0xff << shift);
21547               shift += 8;
21548             }
21549         }
21550     }
21551   if (TARGET_THUMB1)
21552     {
21553       if (TARGET_THUMB1_ONLY)
21554         fputs ("\tpop\t{r3}\n", file);
21555
21556       fprintf (file, "\tbx\tr12\n");
21557       ASM_OUTPUT_ALIGN (file, 2);
21558       assemble_name (file, label);
21559       fputs (":\n", file);
21560       if (flag_pic)
21561         {
21562           /* Output ".word .LTHUNKn-7-.LTHUNKPCn".  */
21563           rtx tem = XEXP (DECL_RTL (function), 0);
21564           tem = gen_rtx_PLUS (GET_MODE (tem), tem, GEN_INT (-7));
21565           tem = gen_rtx_MINUS (GET_MODE (tem),
21566                                tem,
21567                                gen_rtx_SYMBOL_REF (Pmode,
21568                                                    ggc_strdup (labelpc)));
21569           assemble_integer (tem, 4, BITS_PER_WORD, 1);
21570         }
21571       else
21572         /* Output ".word .LTHUNKn".  */
21573         assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
21574
21575       if (TARGET_THUMB1_ONLY && mi_delta > 255)
21576         assemble_integer (GEN_INT(mi_delta), 4, BITS_PER_WORD, 1);
21577     }
21578   else
21579     {
21580       fputs ("\tb\t", file);
21581       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
21582       if (NEED_PLT_RELOC)
21583         fputs ("(PLT)", file);
21584       fputc ('\n', file);
21585     }
21586 }
21587
21588 int
21589 arm_emit_vector_const (FILE *file, rtx x)
21590 {
21591   int i;
21592   const char * pattern;
21593
21594   gcc_assert (GET_CODE (x) == CONST_VECTOR);
21595
21596   switch (GET_MODE (x))
21597     {
21598     case V2SImode: pattern = "%08x"; break;
21599     case V4HImode: pattern = "%04x"; break;
21600     case V8QImode: pattern = "%02x"; break;
21601     default:       gcc_unreachable ();
21602     }
21603
21604   fprintf (file, "0x");
21605   for (i = CONST_VECTOR_NUNITS (x); i--;)
21606     {
21607       rtx element;
21608
21609       element = CONST_VECTOR_ELT (x, i);
21610       fprintf (file, pattern, INTVAL (element));
21611     }
21612
21613   return 1;
21614 }
21615
21616 /* Emit a fp16 constant appropriately padded to occupy a 4-byte word.
21617    HFmode constant pool entries are actually loaded with ldr.  */
21618 void
21619 arm_emit_fp16_const (rtx c)
21620 {
21621   REAL_VALUE_TYPE r;
21622   long bits;
21623
21624   REAL_VALUE_FROM_CONST_DOUBLE (r, c);
21625   bits = real_to_target (NULL, &r, HFmode);
21626   if (WORDS_BIG_ENDIAN)
21627     assemble_zeros (2);
21628   assemble_integer (GEN_INT (bits), 2, BITS_PER_WORD, 1);
21629   if (!WORDS_BIG_ENDIAN)
21630     assemble_zeros (2);
21631 }
21632
21633 const char *
21634 arm_output_load_gr (rtx *operands)
21635 {
21636   rtx reg;
21637   rtx offset;
21638   rtx wcgr;
21639   rtx sum;
21640
21641   if (GET_CODE (operands [1]) != MEM
21642       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
21643       || GET_CODE (reg = XEXP (sum, 0)) != REG
21644       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
21645       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
21646     return "wldrw%?\t%0, %1";
21647
21648   /* Fix up an out-of-range load of a GR register.  */
21649   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
21650   wcgr = operands[0];
21651   operands[0] = reg;
21652   output_asm_insn ("ldr%?\t%0, %1", operands);
21653
21654   operands[0] = wcgr;
21655   operands[1] = reg;
21656   output_asm_insn ("tmcr%?\t%0, %1", operands);
21657   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
21658
21659   return "";
21660 }
21661
21662 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
21663
21664    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
21665    named arg and all anonymous args onto the stack.
21666    XXX I know the prologue shouldn't be pushing registers, but it is faster
21667    that way.  */
21668
21669 static void
21670 arm_setup_incoming_varargs (CUMULATIVE_ARGS *pcum,
21671                             enum machine_mode mode,
21672                             tree type,
21673                             int *pretend_size,
21674                             int second_time ATTRIBUTE_UNUSED)
21675 {
21676   int nregs;
21677   
21678   cfun->machine->uses_anonymous_args = 1;
21679   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
21680     {
21681       nregs = pcum->aapcs_ncrn;
21682       if ((nregs & 1) && arm_needs_doubleword_align (mode, type))
21683         nregs++;
21684     }
21685   else
21686     nregs = pcum->nregs;
21687   
21688   if (nregs < NUM_ARG_REGS)
21689     *pretend_size = (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
21690 }
21691
21692 /* Return nonzero if the CONSUMER instruction (a store) does not need
21693    PRODUCER's value to calculate the address.  */
21694
21695 int
21696 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
21697 {
21698   rtx value = PATTERN (producer);
21699   rtx addr = PATTERN (consumer);
21700
21701   if (GET_CODE (value) == COND_EXEC)
21702     value = COND_EXEC_CODE (value);
21703   if (GET_CODE (value) == PARALLEL)
21704     value = XVECEXP (value, 0, 0);
21705   value = XEXP (value, 0);
21706   if (GET_CODE (addr) == COND_EXEC)
21707     addr = COND_EXEC_CODE (addr);
21708   if (GET_CODE (addr) == PARALLEL)
21709     addr = XVECEXP (addr, 0, 0);
21710   addr = XEXP (addr, 0);
21711
21712   return !reg_overlap_mentioned_p (value, addr);
21713 }
21714
21715 /* Return nonzero if the CONSUMER instruction (a store) does need
21716    PRODUCER's value to calculate the address.  */
21717
21718 int
21719 arm_early_store_addr_dep (rtx producer, rtx consumer)
21720 {
21721   return !arm_no_early_store_addr_dep (producer, consumer);
21722 }
21723
21724 /* Return nonzero if the CONSUMER instruction (a load) does need
21725    PRODUCER's value to calculate the address.  */
21726
21727 int
21728 arm_early_load_addr_dep (rtx producer, rtx consumer)
21729 {
21730   rtx value = PATTERN (producer);
21731   rtx addr = PATTERN (consumer);
21732
21733   if (GET_CODE (value) == COND_EXEC)
21734     value = COND_EXEC_CODE (value);
21735   if (GET_CODE (value) == PARALLEL)
21736     value = XVECEXP (value, 0, 0);
21737   value = XEXP (value, 0);
21738   if (GET_CODE (addr) == COND_EXEC)
21739     addr = COND_EXEC_CODE (addr);
21740   if (GET_CODE (addr) == PARALLEL)
21741     addr = XVECEXP (addr, 0, 0);
21742   addr = XEXP (addr, 1);
21743
21744   return reg_overlap_mentioned_p (value, addr);
21745 }
21746
21747 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
21748    have an early register shift value or amount dependency on the
21749    result of PRODUCER.  */
21750
21751 int
21752 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
21753 {
21754   rtx value = PATTERN (producer);
21755   rtx op = PATTERN (consumer);
21756   rtx early_op;
21757
21758   if (GET_CODE (value) == COND_EXEC)
21759     value = COND_EXEC_CODE (value);
21760   if (GET_CODE (value) == PARALLEL)
21761     value = XVECEXP (value, 0, 0);
21762   value = XEXP (value, 0);
21763   if (GET_CODE (op) == COND_EXEC)
21764     op = COND_EXEC_CODE (op);
21765   if (GET_CODE (op) == PARALLEL)
21766     op = XVECEXP (op, 0, 0);
21767   op = XEXP (op, 1);
21768
21769   early_op = XEXP (op, 0);
21770   /* This is either an actual independent shift, or a shift applied to
21771      the first operand of another operation.  We want the whole shift
21772      operation.  */
21773   if (GET_CODE (early_op) == REG)
21774     early_op = op;
21775
21776   return !reg_overlap_mentioned_p (value, early_op);
21777 }
21778
21779 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
21780    have an early register shift value dependency on the result of
21781    PRODUCER.  */
21782
21783 int
21784 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
21785 {
21786   rtx value = PATTERN (producer);
21787   rtx op = PATTERN (consumer);
21788   rtx early_op;
21789
21790   if (GET_CODE (value) == COND_EXEC)
21791     value = COND_EXEC_CODE (value);
21792   if (GET_CODE (value) == PARALLEL)
21793     value = XVECEXP (value, 0, 0);
21794   value = XEXP (value, 0);
21795   if (GET_CODE (op) == COND_EXEC)
21796     op = COND_EXEC_CODE (op);
21797   if (GET_CODE (op) == PARALLEL)
21798     op = XVECEXP (op, 0, 0);
21799   op = XEXP (op, 1);
21800
21801   early_op = XEXP (op, 0);
21802
21803   /* This is either an actual independent shift, or a shift applied to
21804      the first operand of another operation.  We want the value being
21805      shifted, in either case.  */
21806   if (GET_CODE (early_op) != REG)
21807     early_op = XEXP (early_op, 0);
21808
21809   return !reg_overlap_mentioned_p (value, early_op);
21810 }
21811
21812 /* Return nonzero if the CONSUMER (a mul or mac op) does not
21813    have an early register mult dependency on the result of
21814    PRODUCER.  */
21815
21816 int
21817 arm_no_early_mul_dep (rtx producer, rtx consumer)
21818 {
21819   rtx value = PATTERN (producer);
21820   rtx op = PATTERN (consumer);
21821
21822   if (GET_CODE (value) == COND_EXEC)
21823     value = COND_EXEC_CODE (value);
21824   if (GET_CODE (value) == PARALLEL)
21825     value = XVECEXP (value, 0, 0);
21826   value = XEXP (value, 0);
21827   if (GET_CODE (op) == COND_EXEC)
21828     op = COND_EXEC_CODE (op);
21829   if (GET_CODE (op) == PARALLEL)
21830     op = XVECEXP (op, 0, 0);
21831   op = XEXP (op, 1);
21832
21833   if (GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
21834     {
21835       if (GET_CODE (XEXP (op, 0)) == MULT)
21836         return !reg_overlap_mentioned_p (value, XEXP (op, 0));
21837       else
21838         return !reg_overlap_mentioned_p (value, XEXP (op, 1));
21839     }
21840
21841   return 0;
21842 }
21843
21844 /* We can't rely on the caller doing the proper promotion when
21845    using APCS or ATPCS.  */
21846
21847 static bool
21848 arm_promote_prototypes (const_tree t ATTRIBUTE_UNUSED)
21849 {
21850     return !TARGET_AAPCS_BASED;
21851 }
21852
21853 static enum machine_mode
21854 arm_promote_function_mode (const_tree type ATTRIBUTE_UNUSED,
21855                            enum machine_mode mode,
21856                            int *punsignedp ATTRIBUTE_UNUSED,
21857                            const_tree fntype ATTRIBUTE_UNUSED,
21858                            int for_return ATTRIBUTE_UNUSED)
21859 {
21860   if (GET_MODE_CLASS (mode) == MODE_INT
21861       && GET_MODE_SIZE (mode) < 4)
21862     return SImode;
21863
21864   return mode;
21865 }
21866
21867 /* AAPCS based ABIs use short enums by default.  */
21868
21869 static bool
21870 arm_default_short_enums (void)
21871 {
21872   return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
21873 }
21874
21875
21876 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
21877
21878 static bool
21879 arm_align_anon_bitfield (void)
21880 {
21881   return TARGET_AAPCS_BASED;
21882 }
21883
21884
21885 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
21886
21887 static tree
21888 arm_cxx_guard_type (void)
21889 {
21890   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
21891 }
21892
21893 /* Return non-zero if the consumer (a multiply-accumulate instruction)
21894    has an accumulator dependency on the result of the producer (a
21895    multiplication instruction) and no other dependency on that result.  */
21896 int
21897 arm_mac_accumulator_is_mul_result (rtx producer, rtx consumer)
21898 {
21899   rtx mul = PATTERN (producer);
21900   rtx mac = PATTERN (consumer);
21901   rtx mul_result;
21902   rtx mac_op0, mac_op1, mac_acc;
21903
21904   if (GET_CODE (mul) == COND_EXEC)
21905     mul = COND_EXEC_CODE (mul);
21906   if (GET_CODE (mac) == COND_EXEC)
21907     mac = COND_EXEC_CODE (mac);
21908
21909   /* Check that mul is of the form (set (...) (mult ...))
21910      and mla is of the form (set (...) (plus (mult ...) (...))).  */
21911   if ((GET_CODE (mul) != SET || GET_CODE (XEXP (mul, 1)) != MULT)
21912       || (GET_CODE (mac) != SET || GET_CODE (XEXP (mac, 1)) != PLUS
21913           || GET_CODE (XEXP (XEXP (mac, 1), 0)) != MULT))
21914     return 0;
21915
21916   mul_result = XEXP (mul, 0);
21917   mac_op0 = XEXP (XEXP (XEXP (mac, 1), 0), 0);
21918   mac_op1 = XEXP (XEXP (XEXP (mac, 1), 0), 1);
21919   mac_acc = XEXP (XEXP (mac, 1), 1);
21920
21921   return (reg_overlap_mentioned_p (mul_result, mac_acc)
21922           && !reg_overlap_mentioned_p (mul_result, mac_op0)
21923           && !reg_overlap_mentioned_p (mul_result, mac_op1));
21924 }
21925
21926
21927 /* The EABI says test the least significant bit of a guard variable.  */
21928
21929 static bool
21930 arm_cxx_guard_mask_bit (void)
21931 {
21932   return TARGET_AAPCS_BASED;
21933 }
21934
21935
21936 /* The EABI specifies that all array cookies are 8 bytes long.  */
21937
21938 static tree
21939 arm_get_cookie_size (tree type)
21940 {
21941   tree size;
21942
21943   if (!TARGET_AAPCS_BASED)
21944     return default_cxx_get_cookie_size (type);
21945
21946   size = build_int_cst (sizetype, 8);
21947   return size;
21948 }
21949
21950
21951 /* The EABI says that array cookies should also contain the element size.  */
21952
21953 static bool
21954 arm_cookie_has_size (void)
21955 {
21956   return TARGET_AAPCS_BASED;
21957 }
21958
21959
21960 /* The EABI says constructors and destructors should return a pointer to
21961    the object constructed/destroyed.  */
21962
21963 static bool
21964 arm_cxx_cdtor_returns_this (void)
21965 {
21966   return TARGET_AAPCS_BASED;
21967 }
21968
21969 /* The EABI says that an inline function may never be the key
21970    method.  */
21971
21972 static bool
21973 arm_cxx_key_method_may_be_inline (void)
21974 {
21975   return !TARGET_AAPCS_BASED;
21976 }
21977
21978 static void
21979 arm_cxx_determine_class_data_visibility (tree decl)
21980 {
21981   if (!TARGET_AAPCS_BASED
21982       || !TARGET_DLLIMPORT_DECL_ATTRIBUTES)
21983     return;
21984
21985   /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
21986      is exported.  However, on systems without dynamic vague linkage,
21987      \S 3.2.5.6 says that COMDAT class data has hidden linkage.  */
21988   if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
21989     DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
21990   else
21991     DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
21992   DECL_VISIBILITY_SPECIFIED (decl) = 1;
21993 }
21994
21995 static bool
21996 arm_cxx_class_data_always_comdat (void)
21997 {
21998   /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
21999      vague linkage if the class has no key function.  */
22000   return !TARGET_AAPCS_BASED;
22001 }
22002
22003
22004 /* The EABI says __aeabi_atexit should be used to register static
22005    destructors.  */
22006
22007 static bool
22008 arm_cxx_use_aeabi_atexit (void)
22009 {
22010   return TARGET_AAPCS_BASED;
22011 }
22012
22013
22014 void
22015 arm_set_return_address (rtx source, rtx scratch)
22016 {
22017   arm_stack_offsets *offsets;
22018   HOST_WIDE_INT delta;
22019   rtx addr;
22020   unsigned long saved_regs;
22021
22022   offsets = arm_get_frame_offsets ();
22023   saved_regs = offsets->saved_regs_mask;
22024
22025   if ((saved_regs & (1 << LR_REGNUM)) == 0)
22026     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
22027   else
22028     {
22029       if (frame_pointer_needed)
22030         addr = plus_constant(hard_frame_pointer_rtx, -4);
22031       else
22032         {
22033           /* LR will be the first saved register.  */
22034           delta = offsets->outgoing_args - (offsets->frame + 4);
22035
22036
22037           if (delta >= 4096)
22038             {
22039               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
22040                                      GEN_INT (delta & ~4095)));
22041               addr = scratch;
22042               delta &= 4095;
22043             }
22044           else
22045             addr = stack_pointer_rtx;
22046
22047           addr = plus_constant (addr, delta);
22048         }
22049       emit_move_insn (gen_frame_mem (Pmode, addr), source);
22050     }
22051 }
22052
22053
22054 void
22055 thumb_set_return_address (rtx source, rtx scratch)
22056 {
22057   arm_stack_offsets *offsets;
22058   HOST_WIDE_INT delta;
22059   HOST_WIDE_INT limit;
22060   int reg;
22061   rtx addr;
22062   unsigned long mask;
22063
22064   emit_use (source);
22065
22066   offsets = arm_get_frame_offsets ();
22067   mask = offsets->saved_regs_mask;
22068   if (mask & (1 << LR_REGNUM))
22069     {
22070       limit = 1024;
22071       /* Find the saved regs.  */
22072       if (frame_pointer_needed)
22073         {
22074           delta = offsets->soft_frame - offsets->saved_args;
22075           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
22076           if (TARGET_THUMB1)
22077             limit = 128;
22078         }
22079       else
22080         {
22081           delta = offsets->outgoing_args - offsets->saved_args;
22082           reg = SP_REGNUM;
22083         }
22084       /* Allow for the stack frame.  */
22085       if (TARGET_THUMB1 && TARGET_BACKTRACE)
22086         delta -= 16;
22087       /* The link register is always the first saved register.  */
22088       delta -= 4;
22089
22090       /* Construct the address.  */
22091       addr = gen_rtx_REG (SImode, reg);
22092       if (delta > limit)
22093         {
22094           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
22095           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
22096           addr = scratch;
22097         }
22098       else
22099         addr = plus_constant (addr, delta);
22100
22101       emit_move_insn (gen_frame_mem (Pmode, addr), source);
22102     }
22103   else
22104     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
22105 }
22106
22107 /* Implements target hook vector_mode_supported_p.  */
22108 bool
22109 arm_vector_mode_supported_p (enum machine_mode mode)
22110 {
22111   /* Neon also supports V2SImode, etc. listed in the clause below.  */
22112   if (TARGET_NEON && (mode == V2SFmode || mode == V4SImode || mode == V8HImode
22113       || mode == V16QImode || mode == V4SFmode || mode == V2DImode))
22114     return true;
22115
22116   if ((TARGET_NEON || TARGET_IWMMXT)
22117       && ((mode == V2SImode)
22118           || (mode == V4HImode)
22119           || (mode == V8QImode)))
22120     return true;
22121
22122   return false;
22123 }
22124
22125 /* Use the option -mvectorize-with-neon-quad to override the use of doubleword
22126    registers when autovectorizing for Neon, at least until multiple vector
22127    widths are supported properly by the middle-end.  */
22128
22129 static enum machine_mode
22130 arm_preferred_simd_mode (enum machine_mode mode)
22131 {
22132   if (TARGET_NEON)
22133     switch (mode)
22134       {
22135       case SFmode:
22136         return TARGET_NEON_VECTORIZE_QUAD ? V4SFmode : V2SFmode;
22137       case SImode:
22138         return TARGET_NEON_VECTORIZE_QUAD ? V4SImode : V2SImode;
22139       case HImode:
22140         return TARGET_NEON_VECTORIZE_QUAD ? V8HImode : V4HImode;
22141       case QImode:
22142         return TARGET_NEON_VECTORIZE_QUAD ? V16QImode : V8QImode;
22143       case DImode:
22144         if (TARGET_NEON_VECTORIZE_QUAD)
22145           return V2DImode;
22146         break;
22147
22148       default:;
22149       }
22150
22151   if (TARGET_REALLY_IWMMXT)
22152     switch (mode)
22153       {
22154       case SImode:
22155         return V2SImode;
22156       case HImode:
22157         return V4HImode;
22158       case QImode:
22159         return V8QImode;
22160
22161       default:;
22162       }
22163
22164   return word_mode;
22165 }
22166
22167 /* Implement TARGET_CLASS_LIKELY_SPILLED_P.
22168  
22169    We need to define this for LO_REGS on thumb.  Otherwise we can end up
22170    using r0-r4 for function arguments, r7 for the stack frame and don't
22171    have enough left over to do doubleword arithmetic.  */
22172
22173 static bool
22174 arm_class_likely_spilled_p (reg_class_t rclass)
22175 {
22176   if ((TARGET_THUMB && rclass == LO_REGS)
22177       || rclass  == CC_REG)
22178     return true;
22179
22180   return false;
22181 }
22182
22183 /* Implements target hook small_register_classes_for_mode_p.  */
22184 bool
22185 arm_small_register_classes_for_mode_p (enum machine_mode mode ATTRIBUTE_UNUSED)
22186 {
22187   return TARGET_THUMB1;
22188 }
22189
22190 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
22191    ARM insns and therefore guarantee that the shift count is modulo 256.
22192    DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
22193    guarantee no particular behavior for out-of-range counts.  */
22194
22195 static unsigned HOST_WIDE_INT
22196 arm_shift_truncation_mask (enum machine_mode mode)
22197 {
22198   return mode == SImode ? 255 : 0;
22199 }
22200
22201
22202 /* Map internal gcc register numbers to DWARF2 register numbers.  */
22203
22204 unsigned int
22205 arm_dbx_register_number (unsigned int regno)
22206 {
22207   if (regno < 16)
22208     return regno;
22209
22210   /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
22211      compatibility.  The EABI defines them as registers 96-103.  */
22212   if (IS_FPA_REGNUM (regno))
22213     return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
22214
22215   if (IS_VFP_REGNUM (regno))
22216     {
22217       /* See comment in arm_dwarf_register_span.  */
22218       if (VFP_REGNO_OK_FOR_SINGLE (regno))
22219         return 64 + regno - FIRST_VFP_REGNUM;
22220       else
22221         return 256 + (regno - FIRST_VFP_REGNUM) / 2;
22222     }
22223
22224   if (IS_IWMMXT_GR_REGNUM (regno))
22225     return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
22226
22227   if (IS_IWMMXT_REGNUM (regno))
22228     return 112 + regno - FIRST_IWMMXT_REGNUM;
22229
22230   gcc_unreachable ();
22231 }
22232
22233 /* Dwarf models VFPv3 registers as 32 64-bit registers.
22234    GCC models tham as 64 32-bit registers, so we need to describe this to
22235    the DWARF generation code.  Other registers can use the default.  */
22236 static rtx
22237 arm_dwarf_register_span (rtx rtl)
22238 {
22239   unsigned regno;
22240   int nregs;
22241   int i;
22242   rtx p;
22243
22244   regno = REGNO (rtl);
22245   if (!IS_VFP_REGNUM (regno))
22246     return NULL_RTX;
22247
22248   /* XXX FIXME: The EABI defines two VFP register ranges:
22249         64-95: Legacy VFPv2 numbering for S0-S31 (obsolescent)
22250         256-287: D0-D31
22251      The recommended encoding for S0-S31 is a DW_OP_bit_piece of the
22252      corresponding D register.  Until GDB supports this, we shall use the
22253      legacy encodings.  We also use these encodings for D0-D15 for
22254      compatibility with older debuggers.  */
22255   if (VFP_REGNO_OK_FOR_SINGLE (regno))
22256     return NULL_RTX;
22257
22258   nregs = GET_MODE_SIZE (GET_MODE (rtl)) / 8;
22259   p = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nregs));
22260   regno = (regno - FIRST_VFP_REGNUM) / 2;
22261   for (i = 0; i < nregs; i++)
22262     XVECEXP (p, 0, i) = gen_rtx_REG (DImode, 256 + regno + i);
22263
22264   return p;
22265 }
22266
22267 #if ARM_UNWIND_INFO
22268 /* Emit unwind directives for a store-multiple instruction or stack pointer
22269    push during alignment.
22270    These should only ever be generated by the function prologue code, so
22271    expect them to have a particular form.  */
22272
22273 static void
22274 arm_unwind_emit_sequence (FILE * asm_out_file, rtx p)
22275 {
22276   int i;
22277   HOST_WIDE_INT offset;
22278   HOST_WIDE_INT nregs;
22279   int reg_size;
22280   unsigned reg;
22281   unsigned lastreg;
22282   rtx e;
22283
22284   e = XVECEXP (p, 0, 0);
22285   if (GET_CODE (e) != SET)
22286     abort ();
22287
22288   /* First insn will adjust the stack pointer.  */
22289   if (GET_CODE (e) != SET
22290       || GET_CODE (XEXP (e, 0)) != REG
22291       || REGNO (XEXP (e, 0)) != SP_REGNUM
22292       || GET_CODE (XEXP (e, 1)) != PLUS)
22293     abort ();
22294
22295   offset = -INTVAL (XEXP (XEXP (e, 1), 1));
22296   nregs = XVECLEN (p, 0) - 1;
22297
22298   reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
22299   if (reg < 16)
22300     {
22301       /* The function prologue may also push pc, but not annotate it as it is
22302          never restored.  We turn this into a stack pointer adjustment.  */
22303       if (nregs * 4 == offset - 4)
22304         {
22305           fprintf (asm_out_file, "\t.pad #4\n");
22306           offset -= 4;
22307         }
22308       reg_size = 4;
22309       fprintf (asm_out_file, "\t.save {");
22310     }
22311   else if (IS_VFP_REGNUM (reg))
22312     {
22313       reg_size = 8;
22314       fprintf (asm_out_file, "\t.vsave {");
22315     }
22316   else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
22317     {
22318       /* FPA registers are done differently.  */
22319       asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
22320       return;
22321     }
22322   else
22323     /* Unknown register type.  */
22324     abort ();
22325
22326   /* If the stack increment doesn't match the size of the saved registers,
22327      something has gone horribly wrong.  */
22328   if (offset != nregs * reg_size)
22329     abort ();
22330
22331   offset = 0;
22332   lastreg = 0;
22333   /* The remaining insns will describe the stores.  */
22334   for (i = 1; i <= nregs; i++)
22335     {
22336       /* Expect (set (mem <addr>) (reg)).
22337          Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)).  */
22338       e = XVECEXP (p, 0, i);
22339       if (GET_CODE (e) != SET
22340           || GET_CODE (XEXP (e, 0)) != MEM
22341           || GET_CODE (XEXP (e, 1)) != REG)
22342         abort ();
22343
22344       reg = REGNO (XEXP (e, 1));
22345       if (reg < lastreg)
22346         abort ();
22347
22348       if (i != 1)
22349         fprintf (asm_out_file, ", ");
22350       /* We can't use %r for vfp because we need to use the
22351          double precision register names.  */
22352       if (IS_VFP_REGNUM (reg))
22353         asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
22354       else
22355         asm_fprintf (asm_out_file, "%r", reg);
22356
22357 #ifdef ENABLE_CHECKING
22358       /* Check that the addresses are consecutive.  */
22359       e = XEXP (XEXP (e, 0), 0);
22360       if (GET_CODE (e) == PLUS)
22361         {
22362           offset += reg_size;
22363           if (GET_CODE (XEXP (e, 0)) != REG
22364               || REGNO (XEXP (e, 0)) != SP_REGNUM
22365               || GET_CODE (XEXP (e, 1)) != CONST_INT
22366               || offset != INTVAL (XEXP (e, 1)))
22367             abort ();
22368         }
22369       else if (i != 1
22370                || GET_CODE (e) != REG
22371                || REGNO (e) != SP_REGNUM)
22372         abort ();
22373 #endif
22374     }
22375   fprintf (asm_out_file, "}\n");
22376 }
22377
22378 /*  Emit unwind directives for a SET.  */
22379
22380 static void
22381 arm_unwind_emit_set (FILE * asm_out_file, rtx p)
22382 {
22383   rtx e0;
22384   rtx e1;
22385   unsigned reg;
22386
22387   e0 = XEXP (p, 0);
22388   e1 = XEXP (p, 1);
22389   switch (GET_CODE (e0))
22390     {
22391     case MEM:
22392       /* Pushing a single register.  */
22393       if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
22394           || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
22395           || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
22396         abort ();
22397
22398       asm_fprintf (asm_out_file, "\t.save ");
22399       if (IS_VFP_REGNUM (REGNO (e1)))
22400         asm_fprintf(asm_out_file, "{d%d}\n",
22401                     (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
22402       else
22403         asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
22404       break;
22405
22406     case REG:
22407       if (REGNO (e0) == SP_REGNUM)
22408         {
22409           /* A stack increment.  */
22410           if (GET_CODE (e1) != PLUS
22411               || GET_CODE (XEXP (e1, 0)) != REG
22412               || REGNO (XEXP (e1, 0)) != SP_REGNUM
22413               || GET_CODE (XEXP (e1, 1)) != CONST_INT)
22414             abort ();
22415
22416           asm_fprintf (asm_out_file, "\t.pad #%wd\n",
22417                        -INTVAL (XEXP (e1, 1)));
22418         }
22419       else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
22420         {
22421           HOST_WIDE_INT offset;
22422
22423           if (GET_CODE (e1) == PLUS)
22424             {
22425               if (GET_CODE (XEXP (e1, 0)) != REG
22426                   || GET_CODE (XEXP (e1, 1)) != CONST_INT)
22427                 abort ();
22428               reg = REGNO (XEXP (e1, 0));
22429               offset = INTVAL (XEXP (e1, 1));
22430               asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
22431                            HARD_FRAME_POINTER_REGNUM, reg,
22432                            offset);
22433             }
22434           else if (GET_CODE (e1) == REG)
22435             {
22436               reg = REGNO (e1);
22437               asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
22438                            HARD_FRAME_POINTER_REGNUM, reg);
22439             }
22440           else
22441             abort ();
22442         }
22443       else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
22444         {
22445           /* Move from sp to reg.  */
22446           asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
22447         }
22448      else if (GET_CODE (e1) == PLUS
22449               && GET_CODE (XEXP (e1, 0)) == REG
22450               && REGNO (XEXP (e1, 0)) == SP_REGNUM
22451               && GET_CODE (XEXP (e1, 1)) == CONST_INT)
22452         {
22453           /* Set reg to offset from sp.  */
22454           asm_fprintf (asm_out_file, "\t.movsp %r, #%d\n",
22455                        REGNO (e0), (int)INTVAL(XEXP (e1, 1)));
22456         }
22457       else if (GET_CODE (e1) == UNSPEC && XINT (e1, 1) == UNSPEC_STACK_ALIGN)
22458         {
22459           /* Stack pointer save before alignment.  */
22460           reg = REGNO (e0);
22461           asm_fprintf (asm_out_file, "\t.unwind_raw 0, 0x%x @ vsp = r%d\n",
22462                        reg + 0x90, reg);
22463         }
22464       else
22465         abort ();
22466       break;
22467
22468     default:
22469       abort ();
22470     }
22471 }
22472
22473
22474 /* Emit unwind directives for the given insn.  */
22475
22476 static void
22477 arm_unwind_emit (FILE * asm_out_file, rtx insn)
22478 {
22479   rtx pat;
22480
22481   if (arm_except_unwind_info (&global_options) != UI_TARGET)
22482     return;
22483
22484   if (!(flag_unwind_tables || crtl->uses_eh_lsda)
22485       && (TREE_NOTHROW (current_function_decl)
22486           || crtl->all_throwers_are_sibcalls))
22487     return;
22488
22489   if (GET_CODE (insn) == NOTE || !RTX_FRAME_RELATED_P (insn))
22490     return;
22491
22492   pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
22493   if (pat)
22494     pat = XEXP (pat, 0);
22495   else
22496     pat = PATTERN (insn);
22497
22498   switch (GET_CODE (pat))
22499     {
22500     case SET:
22501       arm_unwind_emit_set (asm_out_file, pat);
22502       break;
22503
22504     case SEQUENCE:
22505       /* Store multiple.  */
22506       arm_unwind_emit_sequence (asm_out_file, pat);
22507       break;
22508
22509     default:
22510       abort();
22511     }
22512 }
22513
22514
22515 /* Output a reference from a function exception table to the type_info
22516    object X.  The EABI specifies that the symbol should be relocated by
22517    an R_ARM_TARGET2 relocation.  */
22518
22519 static bool
22520 arm_output_ttype (rtx x)
22521 {
22522   fputs ("\t.word\t", asm_out_file);
22523   output_addr_const (asm_out_file, x);
22524   /* Use special relocations for symbol references.  */
22525   if (GET_CODE (x) != CONST_INT)
22526     fputs ("(TARGET2)", asm_out_file);
22527   fputc ('\n', asm_out_file);
22528
22529   return TRUE;
22530 }
22531
22532 /* Implement TARGET_ASM_EMIT_EXCEPT_PERSONALITY.  */
22533
22534 static void
22535 arm_asm_emit_except_personality (rtx personality)
22536 {
22537   fputs ("\t.personality\t", asm_out_file);
22538   output_addr_const (asm_out_file, personality);
22539   fputc ('\n', asm_out_file);
22540 }
22541
22542 /* Implement TARGET_ASM_INITIALIZE_SECTIONS.  */
22543
22544 static void
22545 arm_asm_init_sections (void)
22546 {
22547   exception_section = get_unnamed_section (0, output_section_asm_op,
22548                                            "\t.handlerdata");
22549 }
22550 #endif /* ARM_UNWIND_INFO */
22551
22552 /* Implement TARGET_EXCEPT_UNWIND_INFO.  */
22553
22554 static enum unwind_info_type
22555 arm_except_unwind_info (struct gcc_options *opts)
22556 {
22557   /* Honor the --enable-sjlj-exceptions configure switch.  */
22558 #ifdef CONFIG_SJLJ_EXCEPTIONS
22559   if (CONFIG_SJLJ_EXCEPTIONS)
22560     return UI_SJLJ;
22561 #endif
22562
22563   /* If not using ARM EABI unwind tables... */
22564   if (ARM_UNWIND_INFO)
22565     {
22566       /* For simplicity elsewhere in this file, indicate that all unwind
22567          info is disabled if we're not emitting unwind tables.  */
22568       if (!opts->x_flag_exceptions && !opts->x_flag_unwind_tables)
22569         return UI_NONE;
22570       else
22571         return UI_TARGET;
22572     }
22573
22574   /* ... we use sjlj exceptions for backwards compatibility.  */
22575   return UI_SJLJ;
22576 }
22577
22578
22579 /* Handle UNSPEC DWARF call frame instructions.  These are needed for dynamic
22580    stack alignment.  */
22581
22582 static void
22583 arm_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
22584 {
22585   rtx unspec = SET_SRC (pattern);
22586   gcc_assert (GET_CODE (unspec) == UNSPEC);
22587
22588   switch (index)
22589     {
22590     case UNSPEC_STACK_ALIGN:
22591       /* ??? We should set the CFA = (SP & ~7).  At this point we haven't
22592          put anything on the stack, so hopefully it won't matter.
22593          CFA = SP will be correct after alignment.  */
22594       dwarf2out_reg_save_reg (label, stack_pointer_rtx,
22595                               SET_DEST (pattern));
22596       break;
22597     default:
22598       gcc_unreachable ();
22599     }
22600 }
22601
22602
22603 /* Output unwind directives for the start/end of a function.  */
22604
22605 void
22606 arm_output_fn_unwind (FILE * f, bool prologue)
22607 {
22608   if (arm_except_unwind_info (&global_options) != UI_TARGET)
22609     return;
22610
22611   if (prologue)
22612     fputs ("\t.fnstart\n", f);
22613   else
22614     {
22615       /* If this function will never be unwound, then mark it as such.
22616          The came condition is used in arm_unwind_emit to suppress
22617          the frame annotations.  */
22618       if (!(flag_unwind_tables || crtl->uses_eh_lsda)
22619           && (TREE_NOTHROW (current_function_decl)
22620               || crtl->all_throwers_are_sibcalls))
22621         fputs("\t.cantunwind\n", f);
22622
22623       fputs ("\t.fnend\n", f);
22624     }
22625 }
22626
22627 static bool
22628 arm_emit_tls_decoration (FILE *fp, rtx x)
22629 {
22630   enum tls_reloc reloc;
22631   rtx val;
22632
22633   val = XVECEXP (x, 0, 0);
22634   reloc = (enum tls_reloc) INTVAL (XVECEXP (x, 0, 1));
22635
22636   output_addr_const (fp, val);
22637
22638   switch (reloc)
22639     {
22640     case TLS_GD32:
22641       fputs ("(tlsgd)", fp);
22642       break;
22643     case TLS_LDM32:
22644       fputs ("(tlsldm)", fp);
22645       break;
22646     case TLS_LDO32:
22647       fputs ("(tlsldo)", fp);
22648       break;
22649     case TLS_IE32:
22650       fputs ("(gottpoff)", fp);
22651       break;
22652     case TLS_LE32:
22653       fputs ("(tpoff)", fp);
22654       break;
22655     default:
22656       gcc_unreachable ();
22657     }
22658
22659   switch (reloc)
22660     {
22661     case TLS_GD32:
22662     case TLS_LDM32:
22663     case TLS_IE32:
22664       fputs (" + (. - ", fp);
22665       output_addr_const (fp, XVECEXP (x, 0, 2));
22666       fputs (" - ", fp);
22667       output_addr_const (fp, XVECEXP (x, 0, 3));
22668       fputc (')', fp);
22669       break;
22670     default:
22671       break;
22672     }
22673
22674   return TRUE;
22675 }
22676
22677 /* ARM implementation of TARGET_ASM_OUTPUT_DWARF_DTPREL.  */
22678
22679 static void
22680 arm_output_dwarf_dtprel (FILE *file, int size, rtx x)
22681 {
22682   gcc_assert (size == 4);
22683   fputs ("\t.word\t", file);
22684   output_addr_const (file, x);
22685   fputs ("(tlsldo)", file);
22686 }
22687
22688 /* Implement TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA.  */
22689
22690 static bool
22691 arm_output_addr_const_extra (FILE *fp, rtx x)
22692 {
22693   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
22694     return arm_emit_tls_decoration (fp, x);
22695   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
22696     {
22697       char label[256];
22698       int labelno = INTVAL (XVECEXP (x, 0, 0));
22699
22700       ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
22701       assemble_name_raw (fp, label);
22702
22703       return TRUE;
22704     }
22705   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_GOTSYM_OFF)
22706     {
22707       assemble_name (fp, "_GLOBAL_OFFSET_TABLE_");
22708       if (GOT_PCREL)
22709         fputs ("+.", fp);
22710       fputs ("-(", fp);
22711       output_addr_const (fp, XVECEXP (x, 0, 0));
22712       fputc (')', fp);
22713       return TRUE;
22714     }
22715   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_SYMBOL_OFFSET)
22716     {
22717       output_addr_const (fp, XVECEXP (x, 0, 0));
22718       if (GOT_PCREL)
22719         fputs ("+.", fp);
22720       fputs ("-(", fp);
22721       output_addr_const (fp, XVECEXP (x, 0, 1));
22722       fputc (')', fp);
22723       return TRUE;
22724     }
22725   else if (GET_CODE (x) == CONST_VECTOR)
22726     return arm_emit_vector_const (fp, x);
22727
22728   return FALSE;
22729 }
22730
22731 /* Output assembly for a shift instruction.
22732    SET_FLAGS determines how the instruction modifies the condition codes.
22733    0 - Do not set condition codes.
22734    1 - Set condition codes.
22735    2 - Use smallest instruction.  */
22736 const char *
22737 arm_output_shift(rtx * operands, int set_flags)
22738 {
22739   char pattern[100];
22740   static const char flag_chars[3] = {'?', '.', '!'};
22741   const char *shift;
22742   HOST_WIDE_INT val;
22743   char c;
22744   
22745   c = flag_chars[set_flags];
22746   if (TARGET_UNIFIED_ASM)
22747     {
22748       shift = shift_op(operands[3], &val);
22749       if (shift)
22750         {
22751           if (val != -1)
22752             operands[2] = GEN_INT(val);
22753           sprintf (pattern, "%s%%%c\t%%0, %%1, %%2", shift, c);
22754         }
22755       else
22756         sprintf (pattern, "mov%%%c\t%%0, %%1", c);
22757     }
22758   else
22759     sprintf (pattern, "mov%%%c\t%%0, %%1%%S3", c);
22760   output_asm_insn (pattern, operands);
22761   return "";
22762 }
22763
22764 /* Output a Thumb-1 casesi dispatch sequence.  */
22765 const char *
22766 thumb1_output_casesi (rtx *operands)
22767 {
22768   rtx diff_vec = PATTERN (next_real_insn (operands[0]));
22769
22770   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
22771
22772   switch (GET_MODE(diff_vec))
22773     {
22774     case QImode:
22775       return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned ? 
22776               "bl\t%___gnu_thumb1_case_uqi" : "bl\t%___gnu_thumb1_case_sqi");
22777     case HImode:
22778       return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned ? 
22779               "bl\t%___gnu_thumb1_case_uhi" : "bl\t%___gnu_thumb1_case_shi");
22780     case SImode:
22781       return "bl\t%___gnu_thumb1_case_si";
22782     default:
22783       gcc_unreachable ();
22784     }
22785 }
22786
22787 /* Output a Thumb-2 casesi instruction.  */
22788 const char *
22789 thumb2_output_casesi (rtx *operands)
22790 {
22791   rtx diff_vec = PATTERN (next_real_insn (operands[2]));
22792
22793   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
22794
22795   output_asm_insn ("cmp\t%0, %1", operands);
22796   output_asm_insn ("bhi\t%l3", operands);
22797   switch (GET_MODE(diff_vec))
22798     {
22799     case QImode:
22800       return "tbb\t[%|pc, %0]";
22801     case HImode:
22802       return "tbh\t[%|pc, %0, lsl #1]";
22803     case SImode:
22804       if (flag_pic)
22805         {
22806           output_asm_insn ("adr\t%4, %l2", operands);
22807           output_asm_insn ("ldr\t%5, [%4, %0, lsl #2]", operands);
22808           output_asm_insn ("add\t%4, %4, %5", operands);
22809           return "bx\t%4";
22810         }
22811       else
22812         {
22813           output_asm_insn ("adr\t%4, %l2", operands);
22814           return "ldr\t%|pc, [%4, %0, lsl #2]";
22815         }
22816     default:
22817       gcc_unreachable ();
22818     }
22819 }
22820
22821 /* Most ARM cores are single issue, but some newer ones can dual issue.
22822    The scheduler descriptions rely on this being correct.  */
22823 static int
22824 arm_issue_rate (void)
22825 {
22826   switch (arm_tune)
22827     {
22828     case cortexr4:
22829     case cortexr4f:
22830     case cortexa5:
22831     case cortexa8:
22832     case cortexa9:
22833       return 2;
22834
22835     default:
22836       return 1;
22837     }
22838 }
22839
22840 /* A table and a function to perform ARM-specific name mangling for
22841    NEON vector types in order to conform to the AAPCS (see "Procedure
22842    Call Standard for the ARM Architecture", Appendix A).  To qualify
22843    for emission with the mangled names defined in that document, a
22844    vector type must not only be of the correct mode but also be
22845    composed of NEON vector element types (e.g. __builtin_neon_qi).  */
22846 typedef struct
22847 {
22848   enum machine_mode mode;
22849   const char *element_type_name;
22850   const char *aapcs_name;
22851 } arm_mangle_map_entry;
22852
22853 static arm_mangle_map_entry arm_mangle_map[] = {
22854   /* 64-bit containerized types.  */
22855   { V8QImode,  "__builtin_neon_qi",     "15__simd64_int8_t" },
22856   { V8QImode,  "__builtin_neon_uqi",    "16__simd64_uint8_t" },
22857   { V4HImode,  "__builtin_neon_hi",     "16__simd64_int16_t" },
22858   { V4HImode,  "__builtin_neon_uhi",    "17__simd64_uint16_t" },
22859   { V2SImode,  "__builtin_neon_si",     "16__simd64_int32_t" },
22860   { V2SImode,  "__builtin_neon_usi",    "17__simd64_uint32_t" },
22861   { V2SFmode,  "__builtin_neon_sf",     "18__simd64_float32_t" },
22862   { V8QImode,  "__builtin_neon_poly8",  "16__simd64_poly8_t" },
22863   { V4HImode,  "__builtin_neon_poly16", "17__simd64_poly16_t" },
22864   /* 128-bit containerized types.  */
22865   { V16QImode, "__builtin_neon_qi",     "16__simd128_int8_t" },
22866   { V16QImode, "__builtin_neon_uqi",    "17__simd128_uint8_t" },
22867   { V8HImode,  "__builtin_neon_hi",     "17__simd128_int16_t" },
22868   { V8HImode,  "__builtin_neon_uhi",    "18__simd128_uint16_t" },
22869   { V4SImode,  "__builtin_neon_si",     "17__simd128_int32_t" },
22870   { V4SImode,  "__builtin_neon_usi",    "18__simd128_uint32_t" },
22871   { V4SFmode,  "__builtin_neon_sf",     "19__simd128_float32_t" },
22872   { V16QImode, "__builtin_neon_poly8",  "17__simd128_poly8_t" },
22873   { V8HImode,  "__builtin_neon_poly16", "18__simd128_poly16_t" },
22874   { VOIDmode, NULL, NULL }
22875 };
22876
22877 const char *
22878 arm_mangle_type (const_tree type)
22879 {
22880   arm_mangle_map_entry *pos = arm_mangle_map;
22881
22882   /* The ARM ABI documents (10th October 2008) say that "__va_list"
22883      has to be managled as if it is in the "std" namespace.  */
22884   if (TARGET_AAPCS_BASED 
22885       && lang_hooks.types_compatible_p (CONST_CAST_TREE (type), va_list_type))
22886     {
22887       static bool warned;
22888       if (!warned && warn_psabi && !in_system_header)
22889         {
22890           warned = true;
22891           inform (input_location,
22892                   "the mangling of %<va_list%> has changed in GCC 4.4");
22893         }
22894       return "St9__va_list";
22895     }
22896
22897   /* Half-precision float.  */
22898   if (TREE_CODE (type) == REAL_TYPE && TYPE_PRECISION (type) == 16)
22899     return "Dh";
22900
22901   if (TREE_CODE (type) != VECTOR_TYPE)
22902     return NULL;
22903
22904   /* Check the mode of the vector type, and the name of the vector
22905      element type, against the table.  */
22906   while (pos->mode != VOIDmode)
22907     {
22908       tree elt_type = TREE_TYPE (type);
22909
22910       if (pos->mode == TYPE_MODE (type)
22911           && TREE_CODE (TYPE_NAME (elt_type)) == TYPE_DECL
22912           && !strcmp (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (elt_type))),
22913                       pos->element_type_name))
22914         return pos->aapcs_name;
22915
22916       pos++;
22917     }
22918
22919   /* Use the default mangling for unrecognized (possibly user-defined)
22920      vector types.  */
22921   return NULL;
22922 }
22923
22924 /* Order of allocation of core registers for Thumb: this allocation is
22925    written over the corresponding initial entries of the array
22926    initialized with REG_ALLOC_ORDER.  We allocate all low registers
22927    first.  Saving and restoring a low register is usually cheaper than
22928    using a call-clobbered high register.  */
22929
22930 static const int thumb_core_reg_alloc_order[] =
22931 {
22932    3,  2,  1,  0,  4,  5,  6,  7,
22933   14, 12,  8,  9, 10, 11, 13, 15
22934 };
22935
22936 /* Adjust register allocation order when compiling for Thumb.  */
22937
22938 void
22939 arm_order_regs_for_local_alloc (void)
22940 {
22941   const int arm_reg_alloc_order[] = REG_ALLOC_ORDER;
22942   memcpy(reg_alloc_order, arm_reg_alloc_order, sizeof (reg_alloc_order));
22943   if (TARGET_THUMB)
22944     memcpy (reg_alloc_order, thumb_core_reg_alloc_order,
22945             sizeof (thumb_core_reg_alloc_order));
22946 }
22947
22948 /* Implement TARGET_FRAME_POINTER_REQUIRED.  */
22949
22950 bool
22951 arm_frame_pointer_required (void)
22952 {
22953   return (cfun->has_nonlocal_label
22954           || SUBTARGET_FRAME_POINTER_REQUIRED
22955           || (TARGET_ARM && TARGET_APCS_FRAME && ! leaf_function_p ()));
22956 }
22957
22958 /* Only thumb1 can't support conditional execution, so return true if
22959    the target is not thumb1.  */
22960 static bool
22961 arm_have_conditional_execution (void)
22962 {
22963   return !TARGET_THUMB1;
22964 }
22965
22966 /* Legitimize a memory reference for sync primitive implemented using
22967    ldrex / strex.  We currently force the form of the reference to be
22968    indirect without offset.  We do not yet support the indirect offset
22969    addressing supported by some ARM targets for these
22970    instructions.  */
22971 static rtx
22972 arm_legitimize_sync_memory (rtx memory)
22973 {
22974   rtx addr = force_reg (Pmode, XEXP (memory, 0));
22975   rtx legitimate_memory = gen_rtx_MEM (GET_MODE (memory), addr);
22976
22977   set_mem_alias_set (legitimate_memory, ALIAS_SET_MEMORY_BARRIER);
22978   MEM_VOLATILE_P (legitimate_memory) = MEM_VOLATILE_P (memory);
22979   return legitimate_memory;
22980 }
22981
22982 /* An instruction emitter. */
22983 typedef void (* emit_f) (int label, const char *, rtx *);
22984
22985 /* An instruction emitter that emits via the conventional
22986    output_asm_insn.  */
22987 static void
22988 arm_emit (int label ATTRIBUTE_UNUSED, const char *pattern, rtx *operands)
22989 {
22990   output_asm_insn (pattern, operands);
22991 }
22992
22993 /* Count the number of emitted synchronization instructions.  */
22994 static unsigned arm_insn_count;
22995
22996 /* An emitter that counts emitted instructions but does not actually
22997    emit instruction into the the instruction stream.  */
22998 static void
22999 arm_count (int label,
23000            const char *pattern ATTRIBUTE_UNUSED,
23001            rtx *operands ATTRIBUTE_UNUSED)
23002 {
23003   if (! label)
23004     ++ arm_insn_count;
23005 }
23006
23007 /* Construct a pattern using conventional output formatting and feed
23008    it to output_asm_insn.  Provides a mechanism to construct the
23009    output pattern on the fly.  Note the hard limit on the pattern
23010    buffer size.  */
23011 static void ATTRIBUTE_PRINTF_4
23012 arm_output_asm_insn (emit_f emit, int label, rtx *operands,
23013                      const char *pattern, ...)
23014 {
23015   va_list ap;
23016   char buffer[256];
23017
23018   va_start (ap, pattern);
23019   vsprintf (buffer, pattern, ap);
23020   va_end (ap);
23021   emit (label, buffer, operands);
23022 }
23023
23024 /* Emit the memory barrier instruction, if any, provided by this
23025    target to a specified emitter.  */
23026 static void
23027 arm_process_output_memory_barrier (emit_f emit, rtx *operands)
23028 {
23029   if (TARGET_HAVE_DMB)
23030     {
23031       /* Note we issue a system level barrier. We should consider
23032          issuing a inner shareabilty zone barrier here instead, ie.
23033          "DMB ISH".  */
23034       emit (0, "dmb\tsy", operands);
23035       return;
23036     }
23037
23038   if (TARGET_HAVE_DMB_MCR)
23039     {
23040       emit (0, "mcr\tp15, 0, r0, c7, c10, 5", operands);
23041       return;
23042     }
23043
23044   gcc_unreachable ();
23045 }
23046
23047 /* Emit the memory barrier instruction, if any, provided by this
23048    target.  */
23049 const char *
23050 arm_output_memory_barrier (rtx *operands)
23051 {
23052   arm_process_output_memory_barrier (arm_emit, operands);
23053   return "";
23054 }
23055
23056 /* Helper to figure out the instruction suffix required on ldrex/strex
23057    for operations on an object of the specified mode.  */
23058 static const char *
23059 arm_ldrex_suffix (enum machine_mode mode)
23060 {
23061   switch (mode)
23062     {
23063     case QImode: return "b";
23064     case HImode: return "h";
23065     case SImode: return "";
23066     case DImode: return "d";
23067     default:
23068       gcc_unreachable ();
23069     }
23070   return "";
23071 }
23072
23073 /* Emit an ldrex{b,h,d, } instruction appropriate for the specified
23074    mode.  */
23075 static void
23076 arm_output_ldrex (emit_f emit,
23077                   enum machine_mode mode,
23078                   rtx target,
23079                   rtx memory)
23080 {
23081   const char *suffix = arm_ldrex_suffix (mode);
23082   rtx operands[2];
23083
23084   operands[0] = target;
23085   operands[1] = memory;
23086   arm_output_asm_insn (emit, 0, operands, "ldrex%s\t%%0, %%C1", suffix);
23087 }
23088
23089 /* Emit a strex{b,h,d, } instruction appropriate for the specified
23090    mode.  */
23091 static void
23092 arm_output_strex (emit_f emit,
23093                   enum machine_mode mode,
23094                   const char *cc,
23095                   rtx result,
23096                   rtx value,
23097                   rtx memory)
23098 {
23099   const char *suffix = arm_ldrex_suffix (mode);
23100   rtx operands[3];
23101
23102   operands[0] = result;
23103   operands[1] = value;
23104   operands[2] = memory;
23105   arm_output_asm_insn (emit, 0, operands, "strex%s%s\t%%0, %%1, %%C2", suffix,
23106                        cc);
23107 }
23108
23109 /* Helper to emit a two operand instruction.  */
23110 static void
23111 arm_output_op2 (emit_f emit, const char *mnemonic, rtx d, rtx s)
23112 {
23113   rtx operands[2];
23114
23115   operands[0] = d;
23116   operands[1] = s;
23117   arm_output_asm_insn (emit, 0, operands, "%s\t%%0, %%1", mnemonic);
23118 }
23119
23120 /* Helper to emit a three operand instruction.  */
23121 static void
23122 arm_output_op3 (emit_f emit, const char *mnemonic, rtx d, rtx a, rtx b)
23123 {
23124   rtx operands[3];
23125
23126   operands[0] = d;
23127   operands[1] = a;
23128   operands[2] = b;
23129   arm_output_asm_insn (emit, 0, operands, "%s\t%%0, %%1, %%2", mnemonic);
23130 }
23131
23132 /* Emit a load store exclusive synchronization loop.
23133
23134    do
23135      old_value = [mem]
23136      if old_value != required_value
23137        break;
23138      t1 = sync_op (old_value, new_value)
23139      [mem] = t1, t2 = [0|1]
23140    while ! t2
23141
23142    Note:
23143      t1 == t2 is not permitted
23144      t1 == old_value is permitted
23145
23146    required_value:
23147
23148    RTX register or const_int representing the required old_value for
23149    the modify to continue, if NULL no comparsion is performed.  */
23150 static void
23151 arm_output_sync_loop (emit_f emit,
23152                       enum machine_mode mode,
23153                       rtx old_value,
23154                       rtx memory,
23155                       rtx required_value,
23156                       rtx new_value,
23157                       rtx t1,
23158                       rtx t2,
23159                       enum attr_sync_op sync_op,
23160                       int early_barrier_required)
23161 {
23162   rtx operands[1];
23163
23164   gcc_assert (t1 != t2);
23165
23166   if (early_barrier_required)
23167     arm_process_output_memory_barrier (emit, NULL);
23168
23169   arm_output_asm_insn (emit, 1, operands, "%sLSYT%%=:", LOCAL_LABEL_PREFIX);
23170
23171   arm_output_ldrex (emit, mode, old_value, memory);
23172
23173   if (required_value)
23174     {
23175       rtx operands[2];
23176
23177       operands[0] = old_value;
23178       operands[1] = required_value;
23179       arm_output_asm_insn (emit, 0, operands, "cmp\t%%0, %%1");
23180       arm_output_asm_insn (emit, 0, operands, "bne\t%sLSYB%%=", LOCAL_LABEL_PREFIX);
23181     }
23182
23183   switch (sync_op)
23184     {
23185     case SYNC_OP_ADD:
23186       arm_output_op3 (emit, "add", t1, old_value, new_value);
23187       break;
23188
23189     case SYNC_OP_SUB:
23190       arm_output_op3 (emit, "sub", t1, old_value, new_value);
23191       break;
23192
23193     case SYNC_OP_IOR:
23194       arm_output_op3 (emit, "orr", t1, old_value, new_value);
23195       break;
23196
23197     case SYNC_OP_XOR:
23198       arm_output_op3 (emit, "eor", t1, old_value, new_value);
23199       break;
23200
23201     case SYNC_OP_AND:
23202       arm_output_op3 (emit,"and", t1, old_value, new_value);
23203       break;
23204
23205     case SYNC_OP_NAND:
23206       arm_output_op3 (emit, "and", t1, old_value, new_value);
23207       arm_output_op2 (emit, "mvn", t1, t1);
23208       break;
23209
23210     case SYNC_OP_NONE:
23211       t1 = new_value;
23212       break;
23213     }
23214
23215   arm_output_strex (emit, mode, "", t2, t1, memory);
23216   operands[0] = t2;
23217   arm_output_asm_insn (emit, 0, operands, "teq\t%%0, #0");
23218   arm_output_asm_insn (emit, 0, operands, "bne\t%sLSYT%%=", LOCAL_LABEL_PREFIX);
23219
23220   arm_process_output_memory_barrier (emit, NULL);
23221   arm_output_asm_insn (emit, 1, operands, "%sLSYB%%=:", LOCAL_LABEL_PREFIX);
23222 }
23223
23224 static rtx
23225 arm_get_sync_operand (rtx *operands, int index, rtx default_value)
23226 {
23227   if (index > 0)
23228     default_value = operands[index - 1];
23229
23230   return default_value;
23231 }
23232
23233 #define FETCH_SYNC_OPERAND(NAME, DEFAULT) \
23234   arm_get_sync_operand (operands, (int) get_attr_sync_##NAME (insn), DEFAULT);
23235
23236 /* Extract the operands for a synchroniztion instruction from the
23237    instructions attributes and emit the instruction.  */
23238 static void
23239 arm_process_output_sync_insn (emit_f emit, rtx insn, rtx *operands)
23240 {
23241   rtx result, memory, required_value, new_value, t1, t2;
23242   int early_barrier;
23243   enum machine_mode mode;
23244   enum attr_sync_op sync_op;
23245
23246   result = FETCH_SYNC_OPERAND(result, 0);
23247   memory = FETCH_SYNC_OPERAND(memory, 0);
23248   required_value = FETCH_SYNC_OPERAND(required_value, 0);
23249   new_value = FETCH_SYNC_OPERAND(new_value, 0);
23250   t1 = FETCH_SYNC_OPERAND(t1, 0);
23251   t2 = FETCH_SYNC_OPERAND(t2, 0);
23252   early_barrier =
23253     get_attr_sync_release_barrier (insn) == SYNC_RELEASE_BARRIER_YES;
23254   sync_op = get_attr_sync_op (insn);
23255   mode = GET_MODE (memory);
23256
23257   arm_output_sync_loop (emit, mode, result, memory, required_value,
23258                         new_value, t1, t2, sync_op, early_barrier);
23259 }
23260
23261 /* Emit a synchronization instruction loop.  */
23262 const char *
23263 arm_output_sync_insn (rtx insn, rtx *operands)
23264 {
23265   arm_process_output_sync_insn (arm_emit, insn, operands);
23266   return "";
23267 }
23268
23269 /* Count the number of machine instruction that will be emitted for a
23270    synchronization instruction.  Note that the emitter used does not
23271    emit instructions, it just counts instructions being carefull not
23272    to count labels.  */
23273 unsigned int
23274 arm_sync_loop_insns (rtx insn, rtx *operands)
23275 {
23276   arm_insn_count = 0;
23277   arm_process_output_sync_insn (arm_count, insn, operands);
23278   return arm_insn_count;
23279 }
23280
23281 /* Helper to call a target sync instruction generator, dealing with
23282    the variation in operands required by the different generators.  */
23283 static rtx
23284 arm_call_generator (struct arm_sync_generator *generator, rtx old_value,
23285                     rtx memory, rtx required_value, rtx new_value)
23286 {
23287   switch (generator->op)
23288     {
23289     case arm_sync_generator_omn:
23290       gcc_assert (! required_value);
23291       return generator->u.omn (old_value, memory, new_value);
23292
23293     case arm_sync_generator_omrn:
23294       gcc_assert (required_value);
23295       return generator->u.omrn (old_value, memory, required_value, new_value);
23296     }
23297
23298   return NULL;
23299 }
23300
23301 /* Expand a synchronization loop. The synchronization loop is expanded
23302    as an opaque block of instructions in order to ensure that we do
23303    not subsequently get extraneous memory accesses inserted within the
23304    critical region. The exclusive access property of ldrex/strex is
23305    only guaranteed in there are no intervening memory accesses. */
23306 void
23307 arm_expand_sync (enum machine_mode mode,
23308                  struct arm_sync_generator *generator,
23309                  rtx target, rtx memory, rtx required_value, rtx new_value)
23310 {
23311   if (target == NULL)
23312     target = gen_reg_rtx (mode);
23313
23314   memory = arm_legitimize_sync_memory (memory);
23315   if (mode != SImode)
23316     {
23317       rtx load_temp = gen_reg_rtx (SImode);
23318
23319       if (required_value)
23320         required_value = convert_modes (SImode, mode, required_value, true);
23321
23322       new_value = convert_modes (SImode, mode, new_value, true);
23323       emit_insn (arm_call_generator (generator, load_temp, memory,
23324                                      required_value, new_value));
23325       emit_move_insn (target, gen_lowpart (mode, load_temp));
23326     }
23327   else
23328     {
23329       emit_insn (arm_call_generator (generator, target, memory, required_value,
23330                                      new_value));
23331     }
23332 }
23333
23334 static bool
23335 arm_vector_alignment_reachable (const_tree type, bool is_packed)
23336 {
23337   /* Vectors which aren't in packed structures will not be less aligned than
23338      the natural alignment of their element type, so this is safe.  */
23339   if (TARGET_NEON && !BYTES_BIG_ENDIAN)
23340     return !is_packed;
23341
23342   return default_builtin_vector_alignment_reachable (type, is_packed);
23343 }
23344
23345 static bool
23346 arm_builtin_support_vector_misalignment (enum machine_mode mode,
23347                                          const_tree type, int misalignment,
23348                                          bool is_packed)
23349 {
23350   if (TARGET_NEON && !BYTES_BIG_ENDIAN)
23351     {
23352       HOST_WIDE_INT align = TYPE_ALIGN_UNIT (type);
23353
23354       if (is_packed)
23355         return align == 1;
23356
23357       /* If the misalignment is unknown, we should be able to handle the access
23358          so long as it is not to a member of a packed data structure.  */
23359       if (misalignment == -1)
23360         return true;
23361
23362       /* Return true if the misalignment is a multiple of the natural alignment
23363          of the vector's element type.  This is probably always going to be
23364          true in practice, since we've already established that this isn't a
23365          packed access.  */
23366       return ((misalignment % align) == 0);
23367     }
23368   
23369   return default_builtin_support_vector_misalignment (mode, type, misalignment,
23370                                                       is_packed);
23371 }
23372
23373 static void
23374 arm_conditional_register_usage (void)
23375 {
23376   int regno;
23377
23378   if (TARGET_SOFT_FLOAT || TARGET_THUMB1 || !TARGET_FPA)
23379     {
23380       for (regno = FIRST_FPA_REGNUM;
23381            regno <= LAST_FPA_REGNUM; ++regno)
23382         fixed_regs[regno] = call_used_regs[regno] = 1;
23383     }
23384
23385   if (TARGET_THUMB1 && optimize_size)
23386     {
23387       /* When optimizing for size on Thumb-1, it's better not
23388         to use the HI regs, because of the overhead of
23389         stacking them.  */
23390       for (regno = FIRST_HI_REGNUM;
23391            regno <= LAST_HI_REGNUM; ++regno)
23392         fixed_regs[regno] = call_used_regs[regno] = 1;
23393     }
23394
23395   /* The link register can be clobbered by any branch insn,
23396      but we have no way to track that at present, so mark
23397      it as unavailable.  */
23398   if (TARGET_THUMB1)
23399     fixed_regs[LR_REGNUM] = call_used_regs[LR_REGNUM] = 1;
23400
23401   if (TARGET_32BIT && TARGET_HARD_FLOAT)
23402     {
23403       if (TARGET_MAVERICK)
23404         {
23405           for (regno = FIRST_FPA_REGNUM;
23406                regno <= LAST_FPA_REGNUM; ++ regno)
23407             fixed_regs[regno] = call_used_regs[regno] = 1;
23408           for (regno = FIRST_CIRRUS_FP_REGNUM;
23409                regno <= LAST_CIRRUS_FP_REGNUM; ++ regno)
23410             {
23411               fixed_regs[regno] = 0;
23412               call_used_regs[regno] = regno < FIRST_CIRRUS_FP_REGNUM + 4;
23413             }
23414         }
23415       if (TARGET_VFP)
23416         {
23417           /* VFPv3 registers are disabled when earlier VFP
23418              versions are selected due to the definition of
23419              LAST_VFP_REGNUM.  */
23420           for (regno = FIRST_VFP_REGNUM;
23421                regno <= LAST_VFP_REGNUM; ++ regno)
23422             {
23423               fixed_regs[regno] = 0;
23424               call_used_regs[regno] = regno < FIRST_VFP_REGNUM + 16
23425                 || regno >= FIRST_VFP_REGNUM + 32;
23426             }
23427         }
23428     }
23429
23430   if (TARGET_REALLY_IWMMXT)
23431     {
23432       regno = FIRST_IWMMXT_GR_REGNUM;
23433       /* The 2002/10/09 revision of the XScale ABI has wCG0
23434          and wCG1 as call-preserved registers.  The 2002/11/21
23435          revision changed this so that all wCG registers are
23436          scratch registers.  */
23437       for (regno = FIRST_IWMMXT_GR_REGNUM;
23438            regno <= LAST_IWMMXT_GR_REGNUM; ++ regno)
23439         fixed_regs[regno] = 0;
23440       /* The XScale ABI has wR0 - wR9 as scratch registers,
23441          the rest as call-preserved registers.  */
23442       for (regno = FIRST_IWMMXT_REGNUM;
23443            regno <= LAST_IWMMXT_REGNUM; ++ regno)
23444         {
23445           fixed_regs[regno] = 0;
23446           call_used_regs[regno] = regno < FIRST_IWMMXT_REGNUM + 10;
23447         }
23448     }
23449
23450   if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
23451     {
23452       fixed_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
23453       call_used_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
23454     }
23455   else if (TARGET_APCS_STACK)
23456     {
23457       fixed_regs[10]     = 1;
23458       call_used_regs[10] = 1;
23459     }
23460   /* -mcaller-super-interworking reserves r11 for calls to
23461      _interwork_r11_call_via_rN().  Making the register global
23462      is an easy way of ensuring that it remains valid for all
23463      calls.  */
23464   if (TARGET_APCS_FRAME || TARGET_CALLER_INTERWORKING
23465       || TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME)
23466     {
23467       fixed_regs[ARM_HARD_FRAME_POINTER_REGNUM] = 1;
23468       call_used_regs[ARM_HARD_FRAME_POINTER_REGNUM] = 1;
23469       if (TARGET_CALLER_INTERWORKING)
23470         global_regs[ARM_HARD_FRAME_POINTER_REGNUM] = 1;
23471     }
23472   SUBTARGET_CONDITIONAL_REGISTER_USAGE
23473 }
23474
23475 static reg_class_t
23476 arm_preferred_rename_class (reg_class_t rclass)
23477 {
23478   /* Thumb-2 instructions using LO_REGS may be smaller than instructions
23479      using GENERIC_REGS.  During register rename pass, we prefer LO_REGS,
23480      and code size can be reduced.  */
23481   if (TARGET_THUMB2 && rclass == GENERAL_REGS)
23482     return LO_REGS;
23483   else
23484     return NO_REGS;
23485 }
23486
23487 #include "gt-arm.h"