OSDN Git Service

993d1219bde32ff4b8fb74d3acde834d7ac0d72d
[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
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 "real.h"
35 #include "insn-config.h"
36 #include "conditions.h"
37 #include "output.h"
38 #include "insn-attr.h"
39 #include "flags.h"
40 #include "reload.h"
41 #include "function.h"
42 #include "expr.h"
43 #include "optabs.h"
44 #include "toplev.h"
45 #include "recog.h"
46 #include "cgraph.h"
47 #include "ggc.h"
48 #include "except.h"
49 #include "c-pragma.h"
50 #include "integrate.h"
51 #include "tm_p.h"
52 #include "target.h"
53 #include "target-def.h"
54 #include "debug.h"
55 #include "langhooks.h"
56 #include "df.h"
57 #include "intl.h"
58 #include "libfuncs.h"
59
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 int arm_compute_static_chain_stack_bytes (void);
68 static arm_stack_offsets *arm_get_frame_offsets (void);
69 static void arm_add_gc_roots (void);
70 static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
71                              HOST_WIDE_INT, rtx, rtx, int, int);
72 static unsigned bit_count (unsigned long);
73 static int arm_address_register_rtx_p (rtx, int);
74 static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
75 static int thumb2_legitimate_index_p (enum machine_mode, rtx, int);
76 static int thumb1_base_register_rtx_p (rtx, enum machine_mode, int);
77 static rtx arm_legitimize_address (rtx, rtx, enum machine_mode);
78 static rtx thumb_legitimize_address (rtx, rtx, enum machine_mode);
79 inline static int thumb1_index_register_rtx_p (rtx, int);
80 static bool arm_legitimate_address_p (enum machine_mode, rtx, bool);
81 static int thumb_far_jump_used_p (void);
82 static bool thumb_force_lr_save (void);
83 static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
84 static rtx emit_sfm (int, int);
85 static unsigned arm_size_return_regs (void);
86 static bool arm_assemble_integer (rtx, unsigned int, int);
87 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
88 static arm_cc get_arm_condition_code (rtx);
89 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
90 static rtx is_jump_table (rtx);
91 static const char *output_multi_immediate (rtx *, const char *, const char *,
92                                            int, HOST_WIDE_INT);
93 static const char *shift_op (rtx, HOST_WIDE_INT *);
94 static struct machine_function *arm_init_machine_status (void);
95 static void thumb_exit (FILE *, int);
96 static rtx is_jump_table (rtx);
97 static HOST_WIDE_INT get_jump_table_size (rtx);
98 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
99 static Mnode *add_minipool_forward_ref (Mfix *);
100 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
101 static Mnode *add_minipool_backward_ref (Mfix *);
102 static void assign_minipool_offsets (Mfix *);
103 static void arm_print_value (FILE *, rtx);
104 static void dump_minipool (rtx);
105 static int arm_barrier_cost (rtx);
106 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
107 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
108 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
109                                rtx);
110 static void arm_reorg (void);
111 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
112 static unsigned long arm_compute_save_reg0_reg12_mask (void);
113 static unsigned long arm_compute_save_reg_mask (void);
114 static unsigned long arm_isr_value (tree);
115 static unsigned long arm_compute_func_type (void);
116 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
117 static tree arm_handle_pcs_attribute (tree *, tree, tree, int, bool *);
118 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
119 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
120 static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
121 #endif
122 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
123 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
124 static void thumb1_output_function_prologue (FILE *, HOST_WIDE_INT);
125 static int arm_comp_type_attributes (const_tree, const_tree);
126 static void arm_set_default_type_attributes (tree);
127 static int arm_adjust_cost (rtx, rtx, rtx, int);
128 static int count_insns_for_constant (HOST_WIDE_INT, int);
129 static int arm_get_strip_length (int);
130 static bool arm_function_ok_for_sibcall (tree, tree);
131 static enum machine_mode arm_promote_function_mode (const_tree,
132                                                     enum machine_mode, int *,
133                                                     const_tree, int);
134 static bool arm_return_in_memory (const_tree, const_tree);
135 static rtx arm_function_value (const_tree, const_tree, bool);
136 static rtx arm_libcall_value (enum machine_mode, rtx);
137
138 static void arm_internal_label (FILE *, const char *, unsigned long);
139 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
140                                  tree);
141 static bool arm_rtx_costs_1 (rtx, enum rtx_code, int*, bool);
142 static bool arm_size_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *);
143 static bool arm_slowmul_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
144 static bool arm_fastmul_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
145 static bool arm_xscale_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
146 static bool arm_9e_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
147 static bool arm_rtx_costs (rtx, int, int, int *, bool);
148 static int arm_address_cost (rtx, bool);
149 static bool arm_memory_load_p (rtx);
150 static bool arm_cirrus_insn_p (rtx);
151 static void cirrus_reorg (rtx);
152 static void arm_init_builtins (void);
153 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
154 static void arm_init_iwmmxt_builtins (void);
155 static rtx safe_vector_operand (rtx, enum machine_mode);
156 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
157 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
158 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
159 static void emit_constant_insn (rtx cond, rtx pattern);
160 static rtx emit_set_insn (rtx, rtx);
161 static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
162                                   tree, bool);
163 static rtx aapcs_allocate_return_reg (enum machine_mode, const_tree,
164                                       const_tree);
165 static int aapcs_select_return_coproc (const_tree, const_tree);
166
167 #ifdef OBJECT_FORMAT_ELF
168 static void arm_elf_asm_constructor (rtx, int) ATTRIBUTE_UNUSED;
169 static void arm_elf_asm_destructor (rtx, int) ATTRIBUTE_UNUSED;
170 #endif
171 #ifndef ARM_PE
172 static void arm_encode_section_info (tree, rtx, int);
173 #endif
174
175 static void arm_file_end (void);
176 static void arm_file_start (void);
177
178 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
179                                         tree, int *, int);
180 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
181                                    enum machine_mode, const_tree, bool);
182 static bool arm_promote_prototypes (const_tree);
183 static bool arm_default_short_enums (void);
184 static bool arm_align_anon_bitfield (void);
185 static bool arm_return_in_msb (const_tree);
186 static bool arm_must_pass_in_stack (enum machine_mode, const_tree);
187 static bool arm_return_in_memory (const_tree, const_tree);
188 #ifdef TARGET_UNWIND_INFO
189 static void arm_unwind_emit (FILE *, rtx);
190 static bool arm_output_ttype (rtx);
191 #endif
192 static void arm_dwarf_handle_frame_unspec (const char *, rtx, int);
193
194 static tree arm_cxx_guard_type (void);
195 static bool arm_cxx_guard_mask_bit (void);
196 static tree arm_get_cookie_size (tree);
197 static bool arm_cookie_has_size (void);
198 static bool arm_cxx_cdtor_returns_this (void);
199 static bool arm_cxx_key_method_may_be_inline (void);
200 static void arm_cxx_determine_class_data_visibility (tree);
201 static bool arm_cxx_class_data_always_comdat (void);
202 static bool arm_cxx_use_aeabi_atexit (void);
203 static void arm_init_libfuncs (void);
204 static tree arm_build_builtin_va_list (void);
205 static void arm_expand_builtin_va_start (tree, rtx);
206 static tree arm_gimplify_va_arg_expr (tree, tree, gimple_seq *, gimple_seq *);
207 static bool arm_handle_option (size_t, const char *, int);
208 static void arm_target_help (void);
209 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
210 static bool arm_cannot_copy_insn_p (rtx);
211 static bool arm_tls_symbol_p (rtx x);
212 static int arm_issue_rate (void);
213 static void arm_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
214 static bool arm_allocate_stack_slots_for_args (void);
215 static const char *arm_invalid_parameter_type (const_tree t);
216 static const char *arm_invalid_return_type (const_tree t);
217 static tree arm_promoted_type (const_tree t);
218 static tree arm_convert_to_type (tree type, tree expr);
219 static bool arm_scalar_mode_supported_p (enum machine_mode);
220 static bool arm_frame_pointer_required (void);
221
222 \f
223 /* Table of machine attributes.  */
224 static const struct attribute_spec arm_attribute_table[] =
225 {
226   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
227   /* Function calls made to this symbol must be done indirectly, because
228      it may lie outside of the 26 bit addressing range of a normal function
229      call.  */
230   { "long_call",    0, 0, false, true,  true,  NULL },
231   /* Whereas these functions are always known to reside within the 26 bit
232      addressing range.  */
233   { "short_call",   0, 0, false, true,  true,  NULL },
234   /* Specify the procedure call conventions for a function.  */
235   { "pcs",          1, 1, false, true,  true,  arm_handle_pcs_attribute },
236   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
237   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
238   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
239   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
240 #ifdef ARM_PE
241   /* ARM/PE has three new attributes:
242      interfacearm - ?
243      dllexport - for exporting a function/variable that will live in a dll
244      dllimport - for importing a function/variable from a dll
245
246      Microsoft allows multiple declspecs in one __declspec, separating
247      them with spaces.  We do NOT support this.  Instead, use __declspec
248      multiple times.
249   */
250   { "dllimport",    0, 0, true,  false, false, NULL },
251   { "dllexport",    0, 0, true,  false, false, NULL },
252   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
253 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
254   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
255   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
256   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
257 #endif
258   { NULL,           0, 0, false, false, false, NULL }
259 };
260 \f
261 /* Initialize the GCC target structure.  */
262 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
263 #undef  TARGET_MERGE_DECL_ATTRIBUTES
264 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
265 #endif
266
267 #undef TARGET_LEGITIMIZE_ADDRESS
268 #define TARGET_LEGITIMIZE_ADDRESS arm_legitimize_address
269
270 #undef  TARGET_ATTRIBUTE_TABLE
271 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
272
273 #undef TARGET_ASM_FILE_START
274 #define TARGET_ASM_FILE_START arm_file_start
275 #undef TARGET_ASM_FILE_END
276 #define TARGET_ASM_FILE_END arm_file_end
277
278 #undef  TARGET_ASM_ALIGNED_SI_OP
279 #define TARGET_ASM_ALIGNED_SI_OP NULL
280 #undef  TARGET_ASM_INTEGER
281 #define TARGET_ASM_INTEGER arm_assemble_integer
282
283 #undef  TARGET_ASM_FUNCTION_PROLOGUE
284 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
285
286 #undef  TARGET_ASM_FUNCTION_EPILOGUE
287 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
288
289 #undef  TARGET_DEFAULT_TARGET_FLAGS
290 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
291 #undef  TARGET_HANDLE_OPTION
292 #define TARGET_HANDLE_OPTION arm_handle_option
293 #undef  TARGET_HELP
294 #define TARGET_HELP arm_target_help
295
296 #undef  TARGET_COMP_TYPE_ATTRIBUTES
297 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
298
299 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
300 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
301
302 #undef  TARGET_SCHED_ADJUST_COST
303 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
304
305 #undef TARGET_ENCODE_SECTION_INFO
306 #ifdef ARM_PE
307 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
308 #else
309 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
310 #endif
311
312 #undef  TARGET_STRIP_NAME_ENCODING
313 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
314
315 #undef  TARGET_ASM_INTERNAL_LABEL
316 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
317
318 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
319 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
320
321 #undef  TARGET_FUNCTION_VALUE
322 #define TARGET_FUNCTION_VALUE arm_function_value
323
324 #undef  TARGET_LIBCALL_VALUE
325 #define TARGET_LIBCALL_VALUE arm_libcall_value
326
327 #undef  TARGET_ASM_OUTPUT_MI_THUNK
328 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
329 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
330 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
331
332 #undef  TARGET_RTX_COSTS
333 #define TARGET_RTX_COSTS arm_rtx_costs
334 #undef  TARGET_ADDRESS_COST
335 #define TARGET_ADDRESS_COST arm_address_cost
336
337 #undef TARGET_SHIFT_TRUNCATION_MASK
338 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
339 #undef TARGET_VECTOR_MODE_SUPPORTED_P
340 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
341
342 #undef  TARGET_MACHINE_DEPENDENT_REORG
343 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
344
345 #undef  TARGET_INIT_BUILTINS
346 #define TARGET_INIT_BUILTINS  arm_init_builtins
347 #undef  TARGET_EXPAND_BUILTIN
348 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
349
350 #undef TARGET_INIT_LIBFUNCS
351 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
352
353 #undef TARGET_PROMOTE_FUNCTION_MODE
354 #define TARGET_PROMOTE_FUNCTION_MODE arm_promote_function_mode
355 #undef TARGET_PROMOTE_PROTOTYPES
356 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
357 #undef TARGET_PASS_BY_REFERENCE
358 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
359 #undef TARGET_ARG_PARTIAL_BYTES
360 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
361
362 #undef  TARGET_SETUP_INCOMING_VARARGS
363 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
364
365 #undef TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS
366 #define TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS arm_allocate_stack_slots_for_args
367
368 #undef TARGET_DEFAULT_SHORT_ENUMS
369 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
370
371 #undef TARGET_ALIGN_ANON_BITFIELD
372 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
373
374 #undef TARGET_NARROW_VOLATILE_BITFIELD
375 #define TARGET_NARROW_VOLATILE_BITFIELD hook_bool_void_false
376
377 #undef TARGET_CXX_GUARD_TYPE
378 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
379
380 #undef TARGET_CXX_GUARD_MASK_BIT
381 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
382
383 #undef TARGET_CXX_GET_COOKIE_SIZE
384 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
385
386 #undef TARGET_CXX_COOKIE_HAS_SIZE
387 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
388
389 #undef TARGET_CXX_CDTOR_RETURNS_THIS
390 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
391
392 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
393 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
394
395 #undef TARGET_CXX_USE_AEABI_ATEXIT
396 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
397
398 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
399 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
400   arm_cxx_determine_class_data_visibility
401
402 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
403 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
404
405 #undef TARGET_RETURN_IN_MSB
406 #define TARGET_RETURN_IN_MSB arm_return_in_msb
407
408 #undef TARGET_RETURN_IN_MEMORY
409 #define TARGET_RETURN_IN_MEMORY arm_return_in_memory
410
411 #undef TARGET_MUST_PASS_IN_STACK
412 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
413
414 #ifdef TARGET_UNWIND_INFO
415 #undef TARGET_UNWIND_EMIT
416 #define TARGET_UNWIND_EMIT arm_unwind_emit
417
418 /* EABI unwinding tables use a different format for the typeinfo tables.  */
419 #undef TARGET_ASM_TTYPE
420 #define TARGET_ASM_TTYPE arm_output_ttype
421
422 #undef TARGET_ARM_EABI_UNWINDER
423 #define TARGET_ARM_EABI_UNWINDER true
424 #endif /* TARGET_UNWIND_INFO */
425
426 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
427 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC arm_dwarf_handle_frame_unspec
428
429 #undef  TARGET_CANNOT_COPY_INSN_P
430 #define TARGET_CANNOT_COPY_INSN_P arm_cannot_copy_insn_p
431
432 #ifdef HAVE_AS_TLS
433 #undef TARGET_HAVE_TLS
434 #define TARGET_HAVE_TLS true
435 #endif
436
437 #undef TARGET_CANNOT_FORCE_CONST_MEM
438 #define TARGET_CANNOT_FORCE_CONST_MEM arm_cannot_force_const_mem
439
440 #undef TARGET_MAX_ANCHOR_OFFSET
441 #define TARGET_MAX_ANCHOR_OFFSET 4095
442
443 /* The minimum is set such that the total size of the block
444    for a particular anchor is -4088 + 1 + 4095 bytes, which is
445    divisible by eight, ensuring natural spacing of anchors.  */
446 #undef TARGET_MIN_ANCHOR_OFFSET
447 #define TARGET_MIN_ANCHOR_OFFSET -4088
448
449 #undef TARGET_SCHED_ISSUE_RATE
450 #define TARGET_SCHED_ISSUE_RATE arm_issue_rate
451
452 #undef TARGET_MANGLE_TYPE
453 #define TARGET_MANGLE_TYPE arm_mangle_type
454
455 #undef TARGET_BUILD_BUILTIN_VA_LIST
456 #define TARGET_BUILD_BUILTIN_VA_LIST arm_build_builtin_va_list
457 #undef TARGET_EXPAND_BUILTIN_VA_START
458 #define TARGET_EXPAND_BUILTIN_VA_START arm_expand_builtin_va_start
459 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
460 #define TARGET_GIMPLIFY_VA_ARG_EXPR arm_gimplify_va_arg_expr
461
462 #ifdef HAVE_AS_TLS
463 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
464 #define TARGET_ASM_OUTPUT_DWARF_DTPREL arm_output_dwarf_dtprel
465 #endif
466
467 #undef TARGET_LEGITIMATE_ADDRESS_P
468 #define TARGET_LEGITIMATE_ADDRESS_P     arm_legitimate_address_p
469
470 #undef TARGET_INVALID_PARAMETER_TYPE
471 #define TARGET_INVALID_PARAMETER_TYPE arm_invalid_parameter_type
472
473 #undef TARGET_INVALID_RETURN_TYPE
474 #define TARGET_INVALID_RETURN_TYPE arm_invalid_return_type
475
476 #undef TARGET_PROMOTED_TYPE
477 #define TARGET_PROMOTED_TYPE arm_promoted_type
478
479 #undef TARGET_CONVERT_TO_TYPE
480 #define TARGET_CONVERT_TO_TYPE arm_convert_to_type
481
482 #undef TARGET_SCALAR_MODE_SUPPORTED_P
483 #define TARGET_SCALAR_MODE_SUPPORTED_P arm_scalar_mode_supported_p
484
485 #undef TARGET_FRAME_POINTER_REQUIRED
486 #define TARGET_FRAME_POINTER_REQUIRED arm_frame_pointer_required
487
488 struct gcc_target targetm = TARGET_INITIALIZER;
489 \f
490 /* Obstack for minipool constant handling.  */
491 static struct obstack minipool_obstack;
492 static char *         minipool_startobj;
493
494 /* The maximum number of insns skipped which
495    will be conditionalised if possible.  */
496 static int max_insns_skipped = 5;
497
498 extern FILE * asm_out_file;
499
500 /* True if we are currently building a constant table.  */
501 int making_const_table;
502
503 /* The processor for which instructions should be scheduled.  */
504 enum processor_type arm_tune = arm_none;
505
506 /* The default processor used if not overridden by commandline.  */
507 static enum processor_type arm_default_cpu = arm_none;
508
509 /* Which floating point model to use.  */
510 enum arm_fp_model arm_fp_model;
511
512 /* Which floating point hardware is available.  */
513 enum fputype arm_fpu_arch;
514
515 /* Which floating point hardware to schedule for.  */
516 enum fputype arm_fpu_tune;
517
518 /* Whether to use floating point hardware.  */
519 enum float_abi_type arm_float_abi;
520
521 /* Which __fp16 format to use.  */
522 enum arm_fp16_format_type arm_fp16_format;
523
524 /* Which ABI to use.  */
525 enum arm_abi_type arm_abi;
526
527 /* Which thread pointer model to use.  */
528 enum arm_tp_type target_thread_pointer = TP_AUTO;
529
530 /* Used to parse -mstructure_size_boundary command line option.  */
531 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
532
533 /* Used for Thumb call_via trampolines.  */
534 rtx thumb_call_via_label[14];
535 static int thumb_call_reg_needed;
536
537 /* Bit values used to identify processor capabilities.  */
538 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
539 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
540 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
541 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
542 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
543 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
544 #define FL_THUMB      (1 << 6)        /* Thumb aware */
545 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
546 #define FL_STRONG     (1 << 8)        /* StrongARM */
547 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
548 #define FL_XSCALE     (1 << 10)       /* XScale */
549 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
550 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
551                                          media instructions.  */
552 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
553 #define FL_WBUF       (1 << 14)       /* Schedule for write buffer ops.
554                                          Note: ARM6 & 7 derivatives only.  */
555 #define FL_ARCH6K     (1 << 15)       /* Architecture rel 6 K extensions.  */
556 #define FL_THUMB2     (1 << 16)       /* Thumb-2.  */
557 #define FL_NOTM       (1 << 17)       /* Instructions not present in the 'M'
558                                          profile.  */
559 #define FL_DIV        (1 << 18)       /* Hardware divide.  */
560 #define FL_VFPV3      (1 << 19)       /* Vector Floating Point V3.  */
561 #define FL_NEON       (1 << 20)       /* Neon instructions.  */
562
563 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
564
565 #define FL_FOR_ARCH2    FL_NOTM
566 #define FL_FOR_ARCH3    (FL_FOR_ARCH2 | FL_MODE32)
567 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
568 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
569 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
570 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
571 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
572 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
573 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
574 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
575 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
576 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
577 #define FL_FOR_ARCH6K   (FL_FOR_ARCH6 | FL_ARCH6K)
578 #define FL_FOR_ARCH6Z   FL_FOR_ARCH6
579 #define FL_FOR_ARCH6ZK  FL_FOR_ARCH6K
580 #define FL_FOR_ARCH6T2  (FL_FOR_ARCH6 | FL_THUMB2)
581 #define FL_FOR_ARCH6M   (FL_FOR_ARCH6 & ~FL_NOTM)
582 #define FL_FOR_ARCH7    (FL_FOR_ARCH6T2 &~ FL_NOTM)
583 #define FL_FOR_ARCH7A   (FL_FOR_ARCH7 | FL_NOTM)
584 #define FL_FOR_ARCH7R   (FL_FOR_ARCH7A | FL_DIV)
585 #define FL_FOR_ARCH7M   (FL_FOR_ARCH7 | FL_DIV)
586
587 /* The bits in this mask specify which
588    instructions we are allowed to generate.  */
589 static unsigned long insn_flags = 0;
590
591 /* The bits in this mask specify which instruction scheduling options should
592    be used.  */
593 static unsigned long tune_flags = 0;
594
595 /* The following are used in the arm.md file as equivalents to bits
596    in the above two flag variables.  */
597
598 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
599 int arm_arch3m = 0;
600
601 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
602 int arm_arch4 = 0;
603
604 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
605 int arm_arch4t = 0;
606
607 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
608 int arm_arch5 = 0;
609
610 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
611 int arm_arch5e = 0;
612
613 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
614 int arm_arch6 = 0;
615
616 /* Nonzero if this chip supports the ARM 6K extensions.  */
617 int arm_arch6k = 0;
618
619 /* Nonzero if instructions not present in the 'M' profile can be used.  */
620 int arm_arch_notm = 0;
621
622 /* Nonzero if this chip can benefit from load scheduling.  */
623 int arm_ld_sched = 0;
624
625 /* Nonzero if this chip is a StrongARM.  */
626 int arm_tune_strongarm = 0;
627
628 /* Nonzero if this chip is a Cirrus variant.  */
629 int arm_arch_cirrus = 0;
630
631 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
632 int arm_arch_iwmmxt = 0;
633
634 /* Nonzero if this chip is an XScale.  */
635 int arm_arch_xscale = 0;
636
637 /* Nonzero if tuning for XScale  */
638 int arm_tune_xscale = 0;
639
640 /* Nonzero if we want to tune for stores that access the write-buffer.
641    This typically means an ARM6 or ARM7 with MMU or MPU.  */
642 int arm_tune_wbuf = 0;
643
644 /* Nonzero if tuning for Cortex-A9.  */
645 int arm_tune_cortex_a9 = 0;
646
647 /* Nonzero if generating Thumb instructions.  */
648 int thumb_code = 0;
649
650 /* Nonzero if we should define __THUMB_INTERWORK__ in the
651    preprocessor.
652    XXX This is a bit of a hack, it's intended to help work around
653    problems in GLD which doesn't understand that armv5t code is
654    interworking clean.  */
655 int arm_cpp_interwork = 0;
656
657 /* Nonzero if chip supports Thumb 2.  */
658 int arm_arch_thumb2;
659
660 /* Nonzero if chip supports integer division instruction.  */
661 int arm_arch_hwdiv;
662
663 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
664    must report the mode of the memory reference from PRINT_OPERAND to
665    PRINT_OPERAND_ADDRESS.  */
666 enum machine_mode output_memory_reference_mode;
667
668 /* The register number to be used for the PIC offset register.  */
669 unsigned arm_pic_register = INVALID_REGNUM;
670
671 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
672    the next function.  */
673 static int after_arm_reorg = 0;
674
675 /* The maximum number of insns to be used when loading a constant.  */
676 static int arm_constant_limit = 3;
677
678 static enum arm_pcs arm_pcs_default;
679
680 /* For an explanation of these variables, see final_prescan_insn below.  */
681 int arm_ccfsm_state;
682 /* arm_current_cc is also used for Thumb-2 cond_exec blocks.  */
683 enum arm_cond_code arm_current_cc;
684 rtx arm_target_insn;
685 int arm_target_label;
686 /* The number of conditionally executed insns, including the current insn.  */
687 int arm_condexec_count = 0;
688 /* A bitmask specifying the patterns for the IT block.
689    Zero means do not output an IT block before this insn. */
690 int arm_condexec_mask = 0;
691 /* The number of bits used in arm_condexec_mask.  */
692 int arm_condexec_masklen = 0;
693
694 /* The condition codes of the ARM, and the inverse function.  */
695 static const char * const arm_condition_codes[] =
696 {
697   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
698   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
699 };
700
701 #define ARM_LSL_NAME (TARGET_UNIFIED_ASM ? "lsl" : "asl")
702 #define streq(string1, string2) (strcmp (string1, string2) == 0)
703
704 #define THUMB2_WORK_REGS (0xff & ~(  (1 << THUMB_HARD_FRAME_POINTER_REGNUM) \
705                                    | (1 << SP_REGNUM) | (1 << PC_REGNUM) \
706                                    | (1 << PIC_OFFSET_TABLE_REGNUM)))
707 \f
708 /* Initialization code.  */
709
710 struct processors
711 {
712   const char *const name;
713   enum processor_type core;
714   const char *arch;
715   const unsigned long flags;
716   bool (* rtx_costs) (rtx, enum rtx_code, enum rtx_code, int *, bool);
717 };
718
719 /* Not all of these give usefully different compilation alternatives,
720    but there is no simple way of generalizing them.  */
721 static const struct processors all_cores[] =
722 {
723   /* ARM Cores */
724 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
725   {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
726 #include "arm-cores.def"
727 #undef ARM_CORE
728   {NULL, arm_none, NULL, 0, NULL}
729 };
730
731 static const struct processors all_architectures[] =
732 {
733   /* ARM Architectures */
734   /* We don't specify rtx_costs here as it will be figured out
735      from the core.  */
736
737   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
738   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
739   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
740   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
741   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
742   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
743      implementations that support it, so we will leave it out for now.  */
744   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
745   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
746   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
747   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
748   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
749   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
750   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
751   {"armv6k",  mpcore,     "6K",  FL_CO_PROC |             FL_FOR_ARCH6K, NULL},
752   {"armv6z",  arm1176jzs, "6Z",  FL_CO_PROC |             FL_FOR_ARCH6Z, NULL},
753   {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC |             FL_FOR_ARCH6ZK, NULL},
754   {"armv6t2", arm1156t2s, "6T2", FL_CO_PROC |             FL_FOR_ARCH6T2, NULL},
755   {"armv6-m", cortexm1,   "6M",                           FL_FOR_ARCH6M, NULL},
756   {"armv7",   cortexa8,   "7",   FL_CO_PROC |             FL_FOR_ARCH7, NULL},
757   {"armv7-a", cortexa8,   "7A",  FL_CO_PROC |             FL_FOR_ARCH7A, NULL},
758   {"armv7-r", cortexr4,   "7R",  FL_CO_PROC |             FL_FOR_ARCH7R, NULL},
759   {"armv7-m", cortexm3,   "7M",  FL_CO_PROC |             FL_FOR_ARCH7M, NULL},
760   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
761   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
762   {"iwmmxt2", iwmmxt2,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
763   {NULL, arm_none, NULL, 0 , NULL}
764 };
765
766 struct arm_cpu_select
767 {
768   const char *              string;
769   const char *              name;
770   const struct processors * processors;
771 };
772
773 /* This is a magic structure.  The 'string' field is magically filled in
774    with a pointer to the value specified by the user on the command line
775    assuming that the user has specified such a value.  */
776
777 static struct arm_cpu_select arm_select[] =
778 {
779   /* string       name            processors  */
780   { NULL,       "-mcpu=",       all_cores  },
781   { NULL,       "-march=",      all_architectures },
782   { NULL,       "-mtune=",      all_cores }
783 };
784
785 /* Defines representing the indexes into the above table.  */
786 #define ARM_OPT_SET_CPU 0
787 #define ARM_OPT_SET_ARCH 1
788 #define ARM_OPT_SET_TUNE 2
789
790 /* The name of the preprocessor macro to define for this architecture.  */
791
792 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
793
794 struct fpu_desc
795 {
796   const char * name;
797   enum fputype fpu;
798 };
799
800
801 /* Available values for -mfpu=.  */
802
803 static const struct fpu_desc all_fpus[] =
804 {
805   {"fpa",               FPUTYPE_FPA},
806   {"fpe2",              FPUTYPE_FPA_EMU2},
807   {"fpe3",              FPUTYPE_FPA_EMU2},
808   {"maverick",          FPUTYPE_MAVERICK},
809   {"vfp",               FPUTYPE_VFP},
810   {"vfp3",              FPUTYPE_VFP3},
811   {"vfpv3",             FPUTYPE_VFP3},
812   {"vfpv3-d16",         FPUTYPE_VFP3D16},
813   {"neon",              FPUTYPE_NEON},
814   {"neon-fp16",         FPUTYPE_NEON_FP16}
815 };
816
817
818 /* Floating point models used by the different hardware.
819    See fputype in arm.h.  */
820
821 static const enum arm_fp_model fp_model_for_fpu[] =
822 {
823   /* No FP hardware.  */
824   ARM_FP_MODEL_UNKNOWN,         /* FPUTYPE_NONE  */
825   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA  */
826   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU2  */
827   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU3  */
828   ARM_FP_MODEL_MAVERICK,        /* FPUTYPE_MAVERICK  */
829   ARM_FP_MODEL_VFP,             /* FPUTYPE_VFP  */
830   ARM_FP_MODEL_VFP,             /* FPUTYPE_VFP3D16  */
831   ARM_FP_MODEL_VFP,             /* FPUTYPE_VFP3  */
832   ARM_FP_MODEL_VFP,             /* FPUTYPE_NEON  */
833   ARM_FP_MODEL_VFP              /* FPUTYPE_NEON_FP16  */
834 };
835
836
837 struct float_abi
838 {
839   const char * name;
840   enum float_abi_type abi_type;
841 };
842
843
844 /* Available values for -mfloat-abi=.  */
845
846 static const struct float_abi all_float_abis[] =
847 {
848   {"soft",      ARM_FLOAT_ABI_SOFT},
849   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
850   {"hard",      ARM_FLOAT_ABI_HARD}
851 };
852
853
854 struct fp16_format
855 {
856   const char *name;
857   enum arm_fp16_format_type fp16_format_type;
858 };
859
860
861 /* Available values for -mfp16-format=.  */
862
863 static const struct fp16_format all_fp16_formats[] =
864 {
865   {"none",              ARM_FP16_FORMAT_NONE},
866   {"ieee",              ARM_FP16_FORMAT_IEEE},
867   {"alternative",       ARM_FP16_FORMAT_ALTERNATIVE}
868 };
869
870
871 struct abi_name
872 {
873   const char *name;
874   enum arm_abi_type abi_type;
875 };
876
877
878 /* Available values for -mabi=.  */
879
880 static const struct abi_name arm_all_abis[] =
881 {
882   {"apcs-gnu",    ARM_ABI_APCS},
883   {"atpcs",   ARM_ABI_ATPCS},
884   {"aapcs",   ARM_ABI_AAPCS},
885   {"iwmmxt",  ARM_ABI_IWMMXT},
886   {"aapcs-linux",   ARM_ABI_AAPCS_LINUX}
887 };
888
889 /* Supported TLS relocations.  */
890
891 enum tls_reloc {
892   TLS_GD32,
893   TLS_LDM32,
894   TLS_LDO32,
895   TLS_IE32,
896   TLS_LE32
897 };
898
899 /* Emit an insn that's a simple single-set.  Both the operands must be known
900    to be valid.  */
901 inline static rtx
902 emit_set_insn (rtx x, rtx y)
903 {
904   return emit_insn (gen_rtx_SET (VOIDmode, x, y));
905 }
906
907 /* Return the number of bits set in VALUE.  */
908 static unsigned
909 bit_count (unsigned long value)
910 {
911   unsigned long count = 0;
912
913   while (value)
914     {
915       count++;
916       value &= value - 1;  /* Clear the least-significant set bit.  */
917     }
918
919   return count;
920 }
921
922 /* Set up library functions unique to ARM.  */
923
924 static void
925 arm_init_libfuncs (void)
926 {
927   /* There are no special library functions unless we are using the
928      ARM BPABI.  */
929   if (!TARGET_BPABI)
930     return;
931
932   /* The functions below are described in Section 4 of the "Run-Time
933      ABI for the ARM architecture", Version 1.0.  */
934
935   /* Double-precision floating-point arithmetic.  Table 2.  */
936   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
937   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
938   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
939   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
940   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
941
942   /* Double-precision comparisons.  Table 3.  */
943   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
944   set_optab_libfunc (ne_optab, DFmode, NULL);
945   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
946   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
947   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
948   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
949   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
950
951   /* Single-precision floating-point arithmetic.  Table 4.  */
952   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
953   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
954   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
955   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
956   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
957
958   /* Single-precision comparisons.  Table 5.  */
959   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
960   set_optab_libfunc (ne_optab, SFmode, NULL);
961   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
962   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
963   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
964   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
965   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
966
967   /* Floating-point to integer conversions.  Table 6.  */
968   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
969   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
970   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
971   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
972   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
973   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
974   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
975   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
976
977   /* Conversions between floating types.  Table 7.  */
978   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
979   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
980
981   /* Integer to floating-point conversions.  Table 8.  */
982   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
983   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
984   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
985   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
986   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
987   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
988   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
989   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
990
991   /* Long long.  Table 9.  */
992   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
993   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
994   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
995   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
996   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
997   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
998   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
999   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
1000
1001   /* Integer (32/32->32) division.  \S 4.3.1.  */
1002   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
1003   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
1004
1005   /* The divmod functions are designed so that they can be used for
1006      plain division, even though they return both the quotient and the
1007      remainder.  The quotient is returned in the usual location (i.e.,
1008      r0 for SImode, {r0, r1} for DImode), just as would be expected
1009      for an ordinary division routine.  Because the AAPCS calling
1010      conventions specify that all of { r0, r1, r2, r3 } are
1011      callee-saved registers, there is no need to tell the compiler
1012      explicitly that those registers are clobbered by these
1013      routines.  */
1014   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
1015   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
1016
1017   /* For SImode division the ABI provides div-without-mod routines,
1018      which are faster.  */
1019   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
1020   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
1021
1022   /* We don't have mod libcalls.  Fortunately gcc knows how to use the
1023      divmod libcalls instead.  */
1024   set_optab_libfunc (smod_optab, DImode, NULL);
1025   set_optab_libfunc (umod_optab, DImode, NULL);
1026   set_optab_libfunc (smod_optab, SImode, NULL);
1027   set_optab_libfunc (umod_optab, SImode, NULL);
1028
1029   /* Half-precision float operations.  The compiler handles all operations
1030      with NULL libfuncs by converting the SFmode.  */
1031   switch (arm_fp16_format)
1032     {
1033     case ARM_FP16_FORMAT_IEEE:
1034     case ARM_FP16_FORMAT_ALTERNATIVE:
1035
1036       /* Conversions.  */
1037       set_conv_libfunc (trunc_optab, HFmode, SFmode,
1038                         (arm_fp16_format == ARM_FP16_FORMAT_IEEE
1039                          ? "__gnu_f2h_ieee"
1040                          : "__gnu_f2h_alternative"));
1041       set_conv_libfunc (sext_optab, SFmode, HFmode, 
1042                         (arm_fp16_format == ARM_FP16_FORMAT_IEEE
1043                          ? "__gnu_h2f_ieee"
1044                          : "__gnu_h2f_alternative"));
1045       
1046       /* Arithmetic.  */
1047       set_optab_libfunc (add_optab, HFmode, NULL);
1048       set_optab_libfunc (sdiv_optab, HFmode, NULL);
1049       set_optab_libfunc (smul_optab, HFmode, NULL);
1050       set_optab_libfunc (neg_optab, HFmode, NULL);
1051       set_optab_libfunc (sub_optab, HFmode, NULL);
1052
1053       /* Comparisons.  */
1054       set_optab_libfunc (eq_optab, HFmode, NULL);
1055       set_optab_libfunc (ne_optab, HFmode, NULL);
1056       set_optab_libfunc (lt_optab, HFmode, NULL);
1057       set_optab_libfunc (le_optab, HFmode, NULL);
1058       set_optab_libfunc (ge_optab, HFmode, NULL);
1059       set_optab_libfunc (gt_optab, HFmode, NULL);
1060       set_optab_libfunc (unord_optab, HFmode, NULL);
1061       break;
1062
1063     default:
1064       break;
1065     }
1066
1067   if (TARGET_AAPCS_BASED)
1068     synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
1069 }
1070
1071 /* On AAPCS systems, this is the "struct __va_list".  */
1072 static GTY(()) tree va_list_type;
1073
1074 /* Return the type to use as __builtin_va_list.  */
1075 static tree
1076 arm_build_builtin_va_list (void)
1077 {
1078   tree va_list_name;
1079   tree ap_field;
1080   
1081   if (!TARGET_AAPCS_BASED)
1082     return std_build_builtin_va_list ();
1083
1084   /* AAPCS \S 7.1.4 requires that va_list be a typedef for a type
1085      defined as:
1086
1087        struct __va_list 
1088        {
1089          void *__ap;
1090        };
1091
1092      The C Library ABI further reinforces this definition in \S
1093      4.1.
1094
1095      We must follow this definition exactly.  The structure tag
1096      name is visible in C++ mangled names, and thus forms a part
1097      of the ABI.  The field name may be used by people who
1098      #include <stdarg.h>.  */
1099   /* Create the type.  */
1100   va_list_type = lang_hooks.types.make_type (RECORD_TYPE);
1101   /* Give it the required name.  */
1102   va_list_name = build_decl (BUILTINS_LOCATION,
1103                              TYPE_DECL,
1104                              get_identifier ("__va_list"),
1105                              va_list_type);
1106   DECL_ARTIFICIAL (va_list_name) = 1;
1107   TYPE_NAME (va_list_type) = va_list_name;
1108   /* Create the __ap field.  */
1109   ap_field = build_decl (BUILTINS_LOCATION,
1110                          FIELD_DECL, 
1111                          get_identifier ("__ap"),
1112                          ptr_type_node);
1113   DECL_ARTIFICIAL (ap_field) = 1;
1114   DECL_FIELD_CONTEXT (ap_field) = va_list_type;
1115   TYPE_FIELDS (va_list_type) = ap_field;
1116   /* Compute its layout.  */
1117   layout_type (va_list_type);
1118
1119   return va_list_type;
1120 }
1121
1122 /* Return an expression of type "void *" pointing to the next
1123    available argument in a variable-argument list.  VALIST is the
1124    user-level va_list object, of type __builtin_va_list.  */
1125 static tree
1126 arm_extract_valist_ptr (tree valist)
1127 {
1128   if (TREE_TYPE (valist) == error_mark_node)
1129     return error_mark_node;
1130
1131   /* On an AAPCS target, the pointer is stored within "struct
1132      va_list".  */
1133   if (TARGET_AAPCS_BASED)
1134     {
1135       tree ap_field = TYPE_FIELDS (TREE_TYPE (valist));
1136       valist = build3 (COMPONENT_REF, TREE_TYPE (ap_field), 
1137                        valist, ap_field, NULL_TREE);
1138     }
1139
1140   return valist;
1141 }
1142
1143 /* Implement TARGET_EXPAND_BUILTIN_VA_START.  */
1144 static void
1145 arm_expand_builtin_va_start (tree valist, rtx nextarg)
1146 {
1147   valist = arm_extract_valist_ptr (valist);
1148   std_expand_builtin_va_start (valist, nextarg);
1149 }
1150
1151 /* Implement TARGET_GIMPLIFY_VA_ARG_EXPR.  */
1152 static tree
1153 arm_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p, 
1154                           gimple_seq *post_p)
1155 {
1156   valist = arm_extract_valist_ptr (valist);
1157   return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
1158 }
1159
1160 /* Implement TARGET_HANDLE_OPTION.  */
1161
1162 static bool
1163 arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
1164 {
1165   switch (code)
1166     {
1167     case OPT_march_:
1168       arm_select[1].string = arg;
1169       return true;
1170
1171     case OPT_mcpu_:
1172       arm_select[0].string = arg;
1173       return true;
1174
1175     case OPT_mhard_float:
1176       target_float_abi_name = "hard";
1177       return true;
1178
1179     case OPT_msoft_float:
1180       target_float_abi_name = "soft";
1181       return true;
1182
1183     case OPT_mtune_:
1184       arm_select[2].string = arg;
1185       return true;
1186
1187     default:
1188       return true;
1189     }
1190 }
1191
1192 static void
1193 arm_target_help (void)
1194 {
1195   int i;
1196   static int columns = 0;
1197   int remaining;
1198
1199   /* If we have not done so already, obtain the desired maximum width of
1200      the output.  Note - this is a duplication of the code at the start of
1201      gcc/opts.c:print_specific_help() - the two copies should probably be
1202      replaced by a single function.  */
1203   if (columns == 0)
1204     {
1205       const char *p;
1206
1207       GET_ENVIRONMENT (p, "COLUMNS");
1208       if (p != NULL)
1209         {
1210           int value = atoi (p);
1211
1212           if (value > 0)
1213             columns = value;
1214         }
1215
1216       if (columns == 0)
1217         /* Use a reasonable default.  */
1218         columns = 80;
1219     }
1220
1221   printf ("  Known ARM CPUs (for use with the -mcpu= and -mtune= options):\n");
1222
1223   /* The - 2 is because we know that the last entry in the array is NULL.  */
1224   i = ARRAY_SIZE (all_cores) - 2;
1225   gcc_assert (i > 0);
1226   printf ("    %s", all_cores[i].name);
1227   remaining = columns - (strlen (all_cores[i].name) + 4);
1228   gcc_assert (remaining >= 0);
1229
1230   while (i--)
1231     {
1232       int len = strlen (all_cores[i].name);
1233
1234       if (remaining > len + 2)
1235         {
1236           printf (", %s", all_cores[i].name);
1237           remaining -= len + 2;
1238         }
1239       else
1240         {
1241           if (remaining > 0)
1242             printf (",");
1243           printf ("\n    %s", all_cores[i].name);
1244           remaining = columns - (len + 4);
1245         }
1246     }
1247
1248   printf ("\n\n  Known ARM architectures (for use with the -march= option):\n");
1249
1250   i = ARRAY_SIZE (all_architectures) - 2;
1251   gcc_assert (i > 0);
1252   
1253   printf ("    %s", all_architectures[i].name);
1254   remaining = columns - (strlen (all_architectures[i].name) + 4);
1255   gcc_assert (remaining >= 0);
1256
1257   while (i--)
1258     {
1259       int len = strlen (all_architectures[i].name);
1260
1261       if (remaining > len + 2)
1262         {
1263           printf (", %s", all_architectures[i].name);
1264           remaining -= len + 2;
1265         }
1266       else
1267         {
1268           if (remaining > 0)
1269             printf (",");
1270           printf ("\n    %s", all_architectures[i].name);
1271           remaining = columns - (len + 4);
1272         }
1273     }
1274   printf ("\n");
1275
1276 }
1277
1278 /* Fix up any incompatible options that the user has specified.
1279    This has now turned into a maze.  */
1280 void
1281 arm_override_options (void)
1282 {
1283   unsigned i;
1284   enum processor_type target_arch_cpu = arm_none;
1285   enum processor_type selected_cpu = arm_none;
1286
1287   /* Set up the flags based on the cpu/architecture selected by the user.  */
1288   for (i = ARRAY_SIZE (arm_select); i--;)
1289     {
1290       struct arm_cpu_select * ptr = arm_select + i;
1291
1292       if (ptr->string != NULL && ptr->string[0] != '\0')
1293         {
1294           const struct processors * sel;
1295
1296           for (sel = ptr->processors; sel->name != NULL; sel++)
1297             if (streq (ptr->string, sel->name))
1298               {
1299                 /* Set the architecture define.  */
1300                 if (i != ARM_OPT_SET_TUNE)
1301                   sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
1302
1303                 /* Determine the processor core for which we should
1304                    tune code-generation.  */
1305                 if (/* -mcpu= is a sensible default.  */
1306                     i == ARM_OPT_SET_CPU
1307                     /* -mtune= overrides -mcpu= and -march=.  */
1308                     || i == ARM_OPT_SET_TUNE)
1309                   arm_tune = (enum processor_type) (sel - ptr->processors);
1310
1311                 /* Remember the CPU associated with this architecture.
1312                    If no other option is used to set the CPU type,
1313                    we'll use this to guess the most suitable tuning
1314                    options.  */
1315                 if (i == ARM_OPT_SET_ARCH)
1316                   target_arch_cpu = sel->core;
1317
1318                 if (i == ARM_OPT_SET_CPU)
1319                   selected_cpu = (enum processor_type) (sel - ptr->processors);
1320                   
1321                 if (i != ARM_OPT_SET_TUNE)
1322                   {
1323                     /* If we have been given an architecture and a processor
1324                        make sure that they are compatible.  We only generate
1325                        a warning though, and we prefer the CPU over the
1326                        architecture.  */
1327                     if (insn_flags != 0 && (insn_flags ^ sel->flags))
1328                       warning (0, "switch -mcpu=%s conflicts with -march= switch",
1329                                ptr->string);
1330
1331                     insn_flags = sel->flags;
1332                   }
1333
1334                 break;
1335               }
1336
1337           if (sel->name == NULL)
1338             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
1339         }
1340     }
1341
1342   /* Guess the tuning options from the architecture if necessary.  */
1343   if (arm_tune == arm_none)
1344     arm_tune = target_arch_cpu;
1345
1346   /* If the user did not specify a processor, choose one for them.  */
1347   if (insn_flags == 0)
1348     {
1349       const struct processors * sel;
1350       unsigned int        sought;
1351
1352       selected_cpu = (enum processor_type) TARGET_CPU_DEFAULT;
1353       if (selected_cpu == arm_none)
1354         {
1355 #ifdef SUBTARGET_CPU_DEFAULT
1356           /* Use the subtarget default CPU if none was specified by
1357              configure.  */
1358           selected_cpu = (enum processor_type) SUBTARGET_CPU_DEFAULT;
1359 #endif
1360           /* Default to ARM6.  */
1361           if (selected_cpu == arm_none)
1362             selected_cpu = arm6;
1363         }
1364       sel = &all_cores[selected_cpu];
1365
1366       insn_flags = sel->flags;
1367
1368       /* Now check to see if the user has specified some command line
1369          switch that require certain abilities from the cpu.  */
1370       sought = 0;
1371
1372       if (TARGET_INTERWORK || TARGET_THUMB)
1373         {
1374           sought |= (FL_THUMB | FL_MODE32);
1375
1376           /* There are no ARM processors that support both APCS-26 and
1377              interworking.  Therefore we force FL_MODE26 to be removed
1378              from insn_flags here (if it was set), so that the search
1379              below will always be able to find a compatible processor.  */
1380           insn_flags &= ~FL_MODE26;
1381         }
1382
1383       if (sought != 0 && ((sought & insn_flags) != sought))
1384         {
1385           /* Try to locate a CPU type that supports all of the abilities
1386              of the default CPU, plus the extra abilities requested by
1387              the user.  */
1388           for (sel = all_cores; sel->name != NULL; sel++)
1389             if ((sel->flags & sought) == (sought | insn_flags))
1390               break;
1391
1392           if (sel->name == NULL)
1393             {
1394               unsigned current_bit_count = 0;
1395               const struct processors * best_fit = NULL;
1396
1397               /* Ideally we would like to issue an error message here
1398                  saying that it was not possible to find a CPU compatible
1399                  with the default CPU, but which also supports the command
1400                  line options specified by the programmer, and so they
1401                  ought to use the -mcpu=<name> command line option to
1402                  override the default CPU type.
1403
1404                  If we cannot find a cpu that has both the
1405                  characteristics of the default cpu and the given
1406                  command line options we scan the array again looking
1407                  for a best match.  */
1408               for (sel = all_cores; sel->name != NULL; sel++)
1409                 if ((sel->flags & sought) == sought)
1410                   {
1411                     unsigned count;
1412
1413                     count = bit_count (sel->flags & insn_flags);
1414
1415                     if (count >= current_bit_count)
1416                       {
1417                         best_fit = sel;
1418                         current_bit_count = count;
1419                       }
1420                   }
1421
1422               gcc_assert (best_fit);
1423               sel = best_fit;
1424             }
1425
1426           insn_flags = sel->flags;
1427         }
1428       sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
1429       arm_default_cpu = (enum processor_type) (sel - all_cores);
1430       if (arm_tune == arm_none)
1431         arm_tune = arm_default_cpu;
1432     }
1433
1434   /* The processor for which we should tune should now have been
1435      chosen.  */
1436   gcc_assert (arm_tune != arm_none);
1437
1438   tune_flags = all_cores[(int)arm_tune].flags;
1439
1440   if (target_fp16_format_name)
1441     {
1442       for (i = 0; i < ARRAY_SIZE (all_fp16_formats); i++)
1443         {
1444           if (streq (all_fp16_formats[i].name, target_fp16_format_name))
1445             {
1446               arm_fp16_format = all_fp16_formats[i].fp16_format_type;
1447               break;
1448             }
1449         }
1450       if (i == ARRAY_SIZE (all_fp16_formats))
1451         error ("invalid __fp16 format option: -mfp16-format=%s",
1452                target_fp16_format_name);
1453     }
1454   else
1455     arm_fp16_format = ARM_FP16_FORMAT_NONE;
1456
1457   if (target_abi_name)
1458     {
1459       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1460         {
1461           if (streq (arm_all_abis[i].name, target_abi_name))
1462             {
1463               arm_abi = arm_all_abis[i].abi_type;
1464               break;
1465             }
1466         }
1467       if (i == ARRAY_SIZE (arm_all_abis))
1468         error ("invalid ABI option: -mabi=%s", target_abi_name);
1469     }
1470   else
1471     arm_abi = ARM_DEFAULT_ABI;
1472
1473   /* Make sure that the processor choice does not conflict with any of the
1474      other command line choices.  */
1475   if (TARGET_ARM && !(insn_flags & FL_NOTM))
1476     error ("target CPU does not support ARM mode");
1477
1478   /* BPABI targets use linker tricks to allow interworking on cores
1479      without thumb support.  */
1480   if (TARGET_INTERWORK && !((insn_flags & FL_THUMB) || TARGET_BPABI))
1481     {
1482       warning (0, "target CPU does not support interworking" );
1483       target_flags &= ~MASK_INTERWORK;
1484     }
1485
1486   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1487     {
1488       warning (0, "target CPU does not support THUMB instructions");
1489       target_flags &= ~MASK_THUMB;
1490     }
1491
1492   if (TARGET_APCS_FRAME && TARGET_THUMB)
1493     {
1494       /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1495       target_flags &= ~MASK_APCS_FRAME;
1496     }
1497
1498   /* Callee super interworking implies thumb interworking.  Adding
1499      this to the flags here simplifies the logic elsewhere.  */
1500   if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
1501       target_flags |= MASK_INTERWORK;
1502
1503   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1504      from here where no function is being compiled currently.  */
1505   if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1506     warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1507
1508   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1509     warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1510
1511   if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
1512     warning (0, "enabling caller interworking support is only meaningful when compiling for the Thumb");
1513
1514   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1515     {
1516       warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1517       target_flags |= MASK_APCS_FRAME;
1518     }
1519
1520   if (TARGET_POKE_FUNCTION_NAME)
1521     target_flags |= MASK_APCS_FRAME;
1522
1523   if (TARGET_APCS_REENT && flag_pic)
1524     error ("-fpic and -mapcs-reent are incompatible");
1525
1526   if (TARGET_APCS_REENT)
1527     warning (0, "APCS reentrant code not supported.  Ignored");
1528
1529   /* If this target is normally configured to use APCS frames, warn if they
1530      are turned off and debugging is turned on.  */
1531   if (TARGET_ARM
1532       && write_symbols != NO_DEBUG
1533       && !TARGET_APCS_FRAME
1534       && (TARGET_DEFAULT & MASK_APCS_FRAME))
1535     warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1536
1537   if (TARGET_APCS_FLOAT)
1538     warning (0, "passing floating point arguments in fp regs not yet supported");
1539
1540   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
1541   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1542   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1543   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1544   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1545   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1546   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1547   arm_arch6k = (insn_flags & FL_ARCH6K) != 0;
1548   arm_arch_notm = (insn_flags & FL_NOTM) != 0;
1549   arm_arch_thumb2 = (insn_flags & FL_THUMB2) != 0;
1550   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1551   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1552
1553   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1554   arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1555   thumb_code = (TARGET_ARM == 0);
1556   arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1557   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1558   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1559   arm_arch_hwdiv = (insn_flags & FL_DIV) != 0;
1560   arm_tune_cortex_a9 = (arm_tune == cortexa9) != 0;
1561
1562   /* If we are not using the default (ARM mode) section anchor offset
1563      ranges, then set the correct ranges now.  */
1564   if (TARGET_THUMB1)
1565     {
1566       /* Thumb-1 LDR instructions cannot have negative offsets.
1567          Permissible positive offset ranges are 5-bit (for byte loads),
1568          6-bit (for halfword loads), or 7-bit (for word loads).
1569          Empirical results suggest a 7-bit anchor range gives the best
1570          overall code size.  */
1571       targetm.min_anchor_offset = 0;
1572       targetm.max_anchor_offset = 127;
1573     }
1574   else if (TARGET_THUMB2)
1575     {
1576       /* The minimum is set such that the total size of the block
1577          for a particular anchor is 248 + 1 + 4095 bytes, which is
1578          divisible by eight, ensuring natural spacing of anchors.  */
1579       targetm.min_anchor_offset = -248;
1580       targetm.max_anchor_offset = 4095;
1581     }
1582
1583   /* V5 code we generate is completely interworking capable, so we turn off
1584      TARGET_INTERWORK here to avoid many tests later on.  */
1585
1586   /* XXX However, we must pass the right pre-processor defines to CPP
1587      or GLD can get confused.  This is a hack.  */
1588   if (TARGET_INTERWORK)
1589     arm_cpp_interwork = 1;
1590
1591   if (arm_arch5)
1592     target_flags &= ~MASK_INTERWORK;
1593
1594   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1595     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1596
1597   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1598     error ("iwmmxt abi requires an iwmmxt capable cpu");
1599
1600   arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1601   if (target_fpu_name == NULL && target_fpe_name != NULL)
1602     {
1603       if (streq (target_fpe_name, "2"))
1604         target_fpu_name = "fpe2";
1605       else if (streq (target_fpe_name, "3"))
1606         target_fpu_name = "fpe3";
1607       else
1608         error ("invalid floating point emulation option: -mfpe=%s",
1609                target_fpe_name);
1610     }
1611   if (target_fpu_name != NULL)
1612     {
1613       /* The user specified a FPU.  */
1614       for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1615         {
1616           if (streq (all_fpus[i].name, target_fpu_name))
1617             {
1618               arm_fpu_arch = all_fpus[i].fpu;
1619               arm_fpu_tune = arm_fpu_arch;
1620               arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1621               break;
1622             }
1623         }
1624       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1625         error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1626     }
1627   else
1628     {
1629 #ifdef FPUTYPE_DEFAULT
1630       /* Use the default if it is specified for this platform.  */
1631       arm_fpu_arch = FPUTYPE_DEFAULT;
1632       arm_fpu_tune = FPUTYPE_DEFAULT;
1633 #else
1634       /* Pick one based on CPU type.  */
1635       /* ??? Some targets assume FPA is the default.
1636       if ((insn_flags & FL_VFP) != 0)
1637         arm_fpu_arch = FPUTYPE_VFP;
1638       else
1639       */
1640       if (arm_arch_cirrus)
1641         arm_fpu_arch = FPUTYPE_MAVERICK;
1642       else
1643         arm_fpu_arch = FPUTYPE_FPA_EMU2;
1644 #endif
1645       if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1646         arm_fpu_tune = FPUTYPE_FPA;
1647       else
1648         arm_fpu_tune = arm_fpu_arch;
1649       arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1650       gcc_assert (arm_fp_model != ARM_FP_MODEL_UNKNOWN);
1651     }
1652
1653   if (target_float_abi_name != NULL)
1654     {
1655       /* The user specified a FP ABI.  */
1656       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1657         {
1658           if (streq (all_float_abis[i].name, target_float_abi_name))
1659             {
1660               arm_float_abi = all_float_abis[i].abi_type;
1661               break;
1662             }
1663         }
1664       if (i == ARRAY_SIZE (all_float_abis))
1665         error ("invalid floating point abi: -mfloat-abi=%s",
1666                target_float_abi_name);
1667     }
1668   else
1669     arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1670
1671   if (TARGET_AAPCS_BASED
1672       && (arm_fp_model == ARM_FP_MODEL_FPA))
1673     error ("FPA is unsupported in the AAPCS");
1674
1675   if (TARGET_AAPCS_BASED)
1676     {
1677       if (TARGET_CALLER_INTERWORKING)
1678         error ("AAPCS does not support -mcaller-super-interworking");
1679       else
1680         if (TARGET_CALLEE_INTERWORKING)
1681           error ("AAPCS does not support -mcallee-super-interworking");
1682     }
1683
1684   /* FPA and iWMMXt are incompatible because the insn encodings overlap.
1685      VFP and iWMMXt can theoretically coexist, but it's unlikely such silicon
1686      will ever exist.  GCC makes no attempt to support this combination.  */
1687   if (TARGET_IWMMXT && !TARGET_SOFT_FLOAT)
1688     sorry ("iWMMXt and hardware floating point");
1689
1690   /* ??? iWMMXt insn patterns need auditing for Thumb-2.  */
1691   if (TARGET_THUMB2 && TARGET_IWMMXT)
1692     sorry ("Thumb-2 iWMMXt");
1693
1694   /* __fp16 support currently assumes the core has ldrh.  */
1695   if (!arm_arch4 && arm_fp16_format != ARM_FP16_FORMAT_NONE)
1696     sorry ("__fp16 and no ldrh");
1697
1698   /* If soft-float is specified then don't use FPU.  */
1699   if (TARGET_SOFT_FLOAT)
1700     arm_fpu_arch = FPUTYPE_NONE;
1701
1702   if (TARGET_AAPCS_BASED)
1703     {
1704       if (arm_abi == ARM_ABI_IWMMXT)
1705         arm_pcs_default = ARM_PCS_AAPCS_IWMMXT;
1706       else if (arm_float_abi == ARM_FLOAT_ABI_HARD
1707                && TARGET_HARD_FLOAT
1708                && TARGET_VFP)
1709         arm_pcs_default = ARM_PCS_AAPCS_VFP;
1710       else
1711         arm_pcs_default = ARM_PCS_AAPCS;
1712     }
1713   else
1714     {
1715       if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1716         sorry ("-mfloat-abi=hard and VFP");
1717
1718       if (arm_abi == ARM_ABI_APCS)
1719         arm_pcs_default = ARM_PCS_APCS;
1720       else
1721         arm_pcs_default = ARM_PCS_ATPCS;
1722     }
1723
1724   /* For arm2/3 there is no need to do any scheduling if there is only
1725      a floating point emulator, or we are doing software floating-point.  */
1726   if ((TARGET_SOFT_FLOAT
1727        || arm_fpu_tune == FPUTYPE_FPA_EMU2
1728        || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1729       && (tune_flags & FL_MODE32) == 0)
1730     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1731
1732   if (target_thread_switch)
1733     {
1734       if (strcmp (target_thread_switch, "soft") == 0)
1735         target_thread_pointer = TP_SOFT;
1736       else if (strcmp (target_thread_switch, "auto") == 0)
1737         target_thread_pointer = TP_AUTO;
1738       else if (strcmp (target_thread_switch, "cp15") == 0)
1739         target_thread_pointer = TP_CP15;
1740       else
1741         error ("invalid thread pointer option: -mtp=%s", target_thread_switch);
1742     }
1743
1744   /* Use the cp15 method if it is available.  */
1745   if (target_thread_pointer == TP_AUTO)
1746     {
1747       if (arm_arch6k && !TARGET_THUMB)
1748         target_thread_pointer = TP_CP15;
1749       else
1750         target_thread_pointer = TP_SOFT;
1751     }
1752
1753   if (TARGET_HARD_TP && TARGET_THUMB1)
1754     error ("can not use -mtp=cp15 with 16-bit Thumb");
1755
1756   /* Override the default structure alignment for AAPCS ABI.  */
1757   if (TARGET_AAPCS_BASED)
1758     arm_structure_size_boundary = 8;
1759
1760   if (structure_size_string != NULL)
1761     {
1762       int size = strtol (structure_size_string, NULL, 0);
1763
1764       if (size == 8 || size == 32
1765           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1766         arm_structure_size_boundary = size;
1767       else
1768         warning (0, "structure size boundary can only be set to %s",
1769                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1770     }
1771
1772   if (!TARGET_ARM && TARGET_VXWORKS_RTP && flag_pic)
1773     {
1774       error ("RTP PIC is incompatible with Thumb");
1775       flag_pic = 0;
1776     }
1777
1778   /* If stack checking is disabled, we can use r10 as the PIC register,
1779      which keeps r9 available.  The EABI specifies r9 as the PIC register.  */
1780   if (flag_pic && TARGET_SINGLE_PIC_BASE)
1781     {
1782       if (TARGET_VXWORKS_RTP)
1783         warning (0, "RTP PIC is incompatible with -msingle-pic-base");
1784       arm_pic_register = (TARGET_APCS_STACK || TARGET_AAPCS_BASED) ? 9 : 10;
1785     }
1786
1787   if (flag_pic && TARGET_VXWORKS_RTP)
1788     arm_pic_register = 9;
1789
1790   if (arm_pic_register_string != NULL)
1791     {
1792       int pic_register = decode_reg_name (arm_pic_register_string);
1793
1794       if (!flag_pic)
1795         warning (0, "-mpic-register= is useless without -fpic");
1796
1797       /* Prevent the user from choosing an obviously stupid PIC register.  */
1798       else if (pic_register < 0 || call_used_regs[pic_register]
1799                || pic_register == HARD_FRAME_POINTER_REGNUM
1800                || pic_register == STACK_POINTER_REGNUM
1801                || pic_register >= PC_REGNUM
1802                || (TARGET_VXWORKS_RTP
1803                    && (unsigned int) pic_register != arm_pic_register))
1804         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1805       else
1806         arm_pic_register = pic_register;
1807     }
1808
1809   /* Enable -mfix-cortex-m3-ldrd by default for Cortex-M3 cores.  */
1810   if (fix_cm3_ldrd == 2)
1811     {
1812       if (selected_cpu == cortexm3)
1813         fix_cm3_ldrd = 1;
1814       else
1815         fix_cm3_ldrd = 0;
1816     }
1817
1818   /* ??? We might want scheduling for thumb2.  */
1819   if (TARGET_THUMB && flag_schedule_insns)
1820     {
1821       /* Don't warn since it's on by default in -O2.  */
1822       flag_schedule_insns = 0;
1823     }
1824
1825   if (optimize_size)
1826     {
1827       arm_constant_limit = 1;
1828
1829       /* If optimizing for size, bump the number of instructions that we
1830          are prepared to conditionally execute (even on a StrongARM).  */
1831       max_insns_skipped = 6;
1832     }
1833   else
1834     {
1835       /* For processors with load scheduling, it never costs more than
1836          2 cycles to load a constant, and the load scheduler may well
1837          reduce that to 1.  */
1838       if (arm_ld_sched)
1839         arm_constant_limit = 1;
1840
1841       /* On XScale the longer latency of a load makes it more difficult
1842          to achieve a good schedule, so it's faster to synthesize
1843          constants that can be done in two insns.  */
1844       if (arm_tune_xscale)
1845         arm_constant_limit = 2;
1846
1847       /* StrongARM has early execution of branches, so a sequence
1848          that is worth skipping is shorter.  */
1849       if (arm_tune_strongarm)
1850         max_insns_skipped = 3;
1851     }
1852
1853   /* Register global variables with the garbage collector.  */
1854   arm_add_gc_roots ();
1855 }
1856
1857 static void
1858 arm_add_gc_roots (void)
1859 {
1860   gcc_obstack_init(&minipool_obstack);
1861   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1862 }
1863 \f
1864 /* A table of known ARM exception types.
1865    For use with the interrupt function attribute.  */
1866
1867 typedef struct
1868 {
1869   const char *const arg;
1870   const unsigned long return_value;
1871 }
1872 isr_attribute_arg;
1873
1874 static const isr_attribute_arg isr_attribute_args [] =
1875 {
1876   { "IRQ",   ARM_FT_ISR },
1877   { "irq",   ARM_FT_ISR },
1878   { "FIQ",   ARM_FT_FIQ },
1879   { "fiq",   ARM_FT_FIQ },
1880   { "ABORT", ARM_FT_ISR },
1881   { "abort", ARM_FT_ISR },
1882   { "ABORT", ARM_FT_ISR },
1883   { "abort", ARM_FT_ISR },
1884   { "UNDEF", ARM_FT_EXCEPTION },
1885   { "undef", ARM_FT_EXCEPTION },
1886   { "SWI",   ARM_FT_EXCEPTION },
1887   { "swi",   ARM_FT_EXCEPTION },
1888   { NULL,    ARM_FT_NORMAL }
1889 };
1890
1891 /* Returns the (interrupt) function type of the current
1892    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1893
1894 static unsigned long
1895 arm_isr_value (tree argument)
1896 {
1897   const isr_attribute_arg * ptr;
1898   const char *              arg;
1899
1900   if (!arm_arch_notm)
1901     return ARM_FT_NORMAL | ARM_FT_STACKALIGN;
1902
1903   /* No argument - default to IRQ.  */
1904   if (argument == NULL_TREE)
1905     return ARM_FT_ISR;
1906
1907   /* Get the value of the argument.  */
1908   if (TREE_VALUE (argument) == NULL_TREE
1909       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1910     return ARM_FT_UNKNOWN;
1911
1912   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1913
1914   /* Check it against the list of known arguments.  */
1915   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1916     if (streq (arg, ptr->arg))
1917       return ptr->return_value;
1918
1919   /* An unrecognized interrupt type.  */
1920   return ARM_FT_UNKNOWN;
1921 }
1922
1923 /* Computes the type of the current function.  */
1924
1925 static unsigned long
1926 arm_compute_func_type (void)
1927 {
1928   unsigned long type = ARM_FT_UNKNOWN;
1929   tree a;
1930   tree attr;
1931
1932   gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
1933
1934   /* Decide if the current function is volatile.  Such functions
1935      never return, and many memory cycles can be saved by not storing
1936      register values that will never be needed again.  This optimization
1937      was added to speed up context switching in a kernel application.  */
1938   if (optimize > 0
1939       && (TREE_NOTHROW (current_function_decl)
1940           || !(flag_unwind_tables
1941                || (flag_exceptions && !USING_SJLJ_EXCEPTIONS)))
1942       && TREE_THIS_VOLATILE (current_function_decl))
1943     type |= ARM_FT_VOLATILE;
1944
1945   if (cfun->static_chain_decl != NULL)
1946     type |= ARM_FT_NESTED;
1947
1948   attr = DECL_ATTRIBUTES (current_function_decl);
1949
1950   a = lookup_attribute ("naked", attr);
1951   if (a != NULL_TREE)
1952     type |= ARM_FT_NAKED;
1953
1954   a = lookup_attribute ("isr", attr);
1955   if (a == NULL_TREE)
1956     a = lookup_attribute ("interrupt", attr);
1957
1958   if (a == NULL_TREE)
1959     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1960   else
1961     type |= arm_isr_value (TREE_VALUE (a));
1962
1963   return type;
1964 }
1965
1966 /* Returns the type of the current function.  */
1967
1968 unsigned long
1969 arm_current_func_type (void)
1970 {
1971   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1972     cfun->machine->func_type = arm_compute_func_type ();
1973
1974   return cfun->machine->func_type;
1975 }
1976
1977 bool
1978 arm_allocate_stack_slots_for_args (void)
1979 {
1980   /* Naked functions should not allocate stack slots for arguments.  */
1981   return !IS_NAKED (arm_current_func_type ());
1982 }
1983
1984 \f
1985 /* Return 1 if it is possible to return using a single instruction.
1986    If SIBLING is non-null, this is a test for a return before a sibling
1987    call.  SIBLING is the call insn, so we can examine its register usage.  */
1988
1989 int
1990 use_return_insn (int iscond, rtx sibling)
1991 {
1992   int regno;
1993   unsigned int func_type;
1994   unsigned long saved_int_regs;
1995   unsigned HOST_WIDE_INT stack_adjust;
1996   arm_stack_offsets *offsets;
1997
1998   /* Never use a return instruction before reload has run.  */
1999   if (!reload_completed)
2000     return 0;
2001
2002   func_type = arm_current_func_type ();
2003
2004   /* Naked, volatile and stack alignment functions need special
2005      consideration.  */
2006   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED | ARM_FT_STACKALIGN))
2007     return 0;
2008
2009   /* So do interrupt functions that use the frame pointer and Thumb
2010      interrupt functions.  */
2011   if (IS_INTERRUPT (func_type) && (frame_pointer_needed || TARGET_THUMB))
2012     return 0;
2013
2014   offsets = arm_get_frame_offsets ();
2015   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
2016
2017   /* As do variadic functions.  */
2018   if (crtl->args.pretend_args_size
2019       || cfun->machine->uses_anonymous_args
2020       /* Or if the function calls __builtin_eh_return () */
2021       || crtl->calls_eh_return
2022       /* Or if the function calls alloca */
2023       || cfun->calls_alloca
2024       /* Or if there is a stack adjustment.  However, if the stack pointer
2025          is saved on the stack, we can use a pre-incrementing stack load.  */
2026       || !(stack_adjust == 0 || (TARGET_APCS_FRAME && frame_pointer_needed
2027                                  && stack_adjust == 4)))
2028     return 0;
2029
2030   saved_int_regs = offsets->saved_regs_mask;
2031
2032   /* Unfortunately, the insn
2033
2034        ldmib sp, {..., sp, ...}
2035
2036      triggers a bug on most SA-110 based devices, such that the stack
2037      pointer won't be correctly restored if the instruction takes a
2038      page fault.  We work around this problem by popping r3 along with
2039      the other registers, since that is never slower than executing
2040      another instruction.
2041
2042      We test for !arm_arch5 here, because code for any architecture
2043      less than this could potentially be run on one of the buggy
2044      chips.  */
2045   if (stack_adjust == 4 && !arm_arch5 && TARGET_ARM)
2046     {
2047       /* Validate that r3 is a call-clobbered register (always true in
2048          the default abi) ...  */
2049       if (!call_used_regs[3])
2050         return 0;
2051
2052       /* ... that it isn't being used for a return value ... */
2053       if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
2054         return 0;
2055
2056       /* ... or for a tail-call argument ...  */
2057       if (sibling)
2058         {
2059           gcc_assert (GET_CODE (sibling) == CALL_INSN);
2060
2061           if (find_regno_fusage (sibling, USE, 3))
2062             return 0;
2063         }
2064
2065       /* ... and that there are no call-saved registers in r0-r2
2066          (always true in the default ABI).  */
2067       if (saved_int_regs & 0x7)
2068         return 0;
2069     }
2070
2071   /* Can't be done if interworking with Thumb, and any registers have been
2072      stacked.  */
2073   if (TARGET_INTERWORK && saved_int_regs != 0 && !IS_INTERRUPT(func_type))
2074     return 0;
2075
2076   /* On StrongARM, conditional returns are expensive if they aren't
2077      taken and multiple registers have been stacked.  */
2078   if (iscond && arm_tune_strongarm)
2079     {
2080       /* Conditional return when just the LR is stored is a simple
2081          conditional-load instruction, that's not expensive.  */
2082       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
2083         return 0;
2084
2085       if (flag_pic 
2086           && arm_pic_register != INVALID_REGNUM
2087           && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
2088         return 0;
2089     }
2090
2091   /* If there are saved registers but the LR isn't saved, then we need
2092      two instructions for the return.  */
2093   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
2094     return 0;
2095
2096   /* Can't be done if any of the FPA regs are pushed,
2097      since this also requires an insn.  */
2098   if (TARGET_HARD_FLOAT && TARGET_FPA)
2099     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
2100       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
2101         return 0;
2102
2103   /* Likewise VFP regs.  */
2104   if (TARGET_HARD_FLOAT && TARGET_VFP)
2105     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
2106       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
2107         return 0;
2108
2109   if (TARGET_REALLY_IWMMXT)
2110     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
2111       if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
2112         return 0;
2113
2114   return 1;
2115 }
2116
2117 /* Return TRUE if int I is a valid immediate ARM constant.  */
2118
2119 int
2120 const_ok_for_arm (HOST_WIDE_INT i)
2121 {
2122   int lowbit;
2123
2124   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
2125      be all zero, or all one.  */
2126   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
2127       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
2128           != ((~(unsigned HOST_WIDE_INT) 0)
2129               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
2130     return FALSE;
2131
2132   i &= (unsigned HOST_WIDE_INT) 0xffffffff;
2133
2134   /* Fast return for 0 and small values.  We must do this for zero, since
2135      the code below can't handle that one case.  */
2136   if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
2137     return TRUE;
2138
2139   /* Get the number of trailing zeros.  */
2140   lowbit = ffs((int) i) - 1;
2141   
2142   /* Only even shifts are allowed in ARM mode so round down to the
2143      nearest even number.  */
2144   if (TARGET_ARM)
2145     lowbit &= ~1;
2146
2147   if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
2148     return TRUE;
2149
2150   if (TARGET_ARM)
2151     {
2152       /* Allow rotated constants in ARM mode.  */
2153       if (lowbit <= 4
2154            && ((i & ~0xc000003f) == 0
2155                || (i & ~0xf000000f) == 0
2156                || (i & ~0xfc000003) == 0))
2157         return TRUE;
2158     }
2159   else
2160     {
2161       HOST_WIDE_INT v;
2162
2163       /* Allow repeated pattern.  */
2164       v = i & 0xff;
2165       v |= v << 16;
2166       if (i == v || i == (v | (v << 8)))
2167         return TRUE;
2168     }
2169
2170   return FALSE;
2171 }
2172
2173 /* Return true if I is a valid constant for the operation CODE.  */
2174 static int
2175 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
2176 {
2177   if (const_ok_for_arm (i))
2178     return 1;
2179
2180   switch (code)
2181     {
2182     case PLUS:
2183     case COMPARE:
2184     case EQ:
2185     case NE:
2186     case GT:
2187     case LE:
2188     case LT:
2189     case GE:
2190     case GEU:
2191     case LTU:
2192     case GTU:
2193     case LEU:
2194     case UNORDERED:
2195     case ORDERED:
2196     case UNEQ:
2197     case UNGE:
2198     case UNLT:
2199     case UNGT:
2200     case UNLE:
2201       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
2202
2203     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
2204     case XOR:
2205       return 0;
2206
2207     case IOR:
2208       if (TARGET_THUMB2)
2209         return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2210       return 0;
2211
2212     case AND:
2213       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2214
2215     default:
2216       gcc_unreachable ();
2217     }
2218 }
2219
2220 /* Emit a sequence of insns to handle a large constant.
2221    CODE is the code of the operation required, it can be any of SET, PLUS,
2222    IOR, AND, XOR, MINUS;
2223    MODE is the mode in which the operation is being performed;
2224    VAL is the integer to operate on;
2225    SOURCE is the other operand (a register, or a null-pointer for SET);
2226    SUBTARGETS means it is safe to create scratch registers if that will
2227    either produce a simpler sequence, or we will want to cse the values.
2228    Return value is the number of insns emitted.  */
2229
2230 /* ??? Tweak this for thumb2.  */
2231 int
2232 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
2233                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
2234 {
2235   rtx cond;
2236
2237   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
2238     cond = COND_EXEC_TEST (PATTERN (insn));
2239   else
2240     cond = NULL_RTX;
2241
2242   if (subtargets || code == SET
2243       || (GET_CODE (target) == REG && GET_CODE (source) == REG
2244           && REGNO (target) != REGNO (source)))
2245     {
2246       /* After arm_reorg has been called, we can't fix up expensive
2247          constants by pushing them into memory so we must synthesize
2248          them in-line, regardless of the cost.  This is only likely to
2249          be more costly on chips that have load delay slots and we are
2250          compiling without running the scheduler (so no splitting
2251          occurred before the final instruction emission).
2252
2253          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
2254       */
2255       if (!after_arm_reorg
2256           && !cond
2257           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
2258                                 1, 0)
2259               > arm_constant_limit + (code != SET)))
2260         {
2261           if (code == SET)
2262             {
2263               /* Currently SET is the only monadic value for CODE, all
2264                  the rest are diadic.  */
2265               if (TARGET_USE_MOVT)
2266                 arm_emit_movpair (target, GEN_INT (val));
2267               else
2268                 emit_set_insn (target, GEN_INT (val));
2269
2270               return 1;
2271             }
2272           else
2273             {
2274               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
2275
2276               if (TARGET_USE_MOVT)
2277                 arm_emit_movpair (temp, GEN_INT (val));
2278               else
2279                 emit_set_insn (temp, GEN_INT (val));
2280
2281               /* For MINUS, the value is subtracted from, since we never
2282                  have subtraction of a constant.  */
2283               if (code == MINUS)
2284                 emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
2285               else
2286                 emit_set_insn (target,
2287                                gen_rtx_fmt_ee (code, mode, source, temp));
2288               return 2;
2289             }
2290         }
2291     }
2292
2293   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
2294                            1);
2295 }
2296
2297 /* Return the number of ARM instructions required to synthesize the given
2298    constant.  */
2299 static int
2300 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
2301 {
2302   HOST_WIDE_INT temp1;
2303   int num_insns = 0;
2304   do
2305     {
2306       int end;
2307
2308       if (i <= 0)
2309         i += 32;
2310       if (remainder & (3 << (i - 2)))
2311         {
2312           end = i - 8;
2313           if (end < 0)
2314             end += 32;
2315           temp1 = remainder & ((0x0ff << end)
2316                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
2317           remainder &= ~temp1;
2318           num_insns++;
2319           i -= 6;
2320         }
2321       i -= 2;
2322     } while (remainder);
2323   return num_insns;
2324 }
2325
2326 /* Emit an instruction with the indicated PATTERN.  If COND is
2327    non-NULL, conditionalize the execution of the instruction on COND
2328    being true.  */
2329
2330 static void
2331 emit_constant_insn (rtx cond, rtx pattern)
2332 {
2333   if (cond)
2334     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
2335   emit_insn (pattern);
2336 }
2337
2338 /* As above, but extra parameter GENERATE which, if clear, suppresses
2339    RTL generation.  */
2340 /* ??? This needs more work for thumb2.  */
2341
2342 static int
2343 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
2344                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
2345                   int generate)
2346 {
2347   int can_invert = 0;
2348   int can_negate = 0;
2349   int can_negate_initial = 0;
2350   int can_shift = 0;
2351   int i;
2352   int num_bits_set = 0;
2353   int set_sign_bit_copies = 0;
2354   int clear_sign_bit_copies = 0;
2355   int clear_zero_bit_copies = 0;
2356   int set_zero_bit_copies = 0;
2357   int insns = 0;
2358   unsigned HOST_WIDE_INT temp1, temp2;
2359   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
2360
2361   /* Find out which operations are safe for a given CODE.  Also do a quick
2362      check for degenerate cases; these can occur when DImode operations
2363      are split.  */
2364   switch (code)
2365     {
2366     case SET:
2367       can_invert = 1;
2368       can_shift = 1;
2369       can_negate = 1;
2370       break;
2371
2372     case PLUS:
2373       can_negate = 1;
2374       can_negate_initial = 1;
2375       break;
2376
2377     case IOR:
2378       if (remainder == 0xffffffff)
2379         {
2380           if (generate)
2381             emit_constant_insn (cond,
2382                                 gen_rtx_SET (VOIDmode, target,
2383                                              GEN_INT (ARM_SIGN_EXTEND (val))));
2384           return 1;
2385         }
2386
2387       if (remainder == 0)
2388         {
2389           if (reload_completed && rtx_equal_p (target, source))
2390             return 0;
2391
2392           if (generate)
2393             emit_constant_insn (cond,
2394                                 gen_rtx_SET (VOIDmode, target, source));
2395           return 1;
2396         }
2397
2398       if (TARGET_THUMB2)
2399         can_invert = 1;
2400       break;
2401
2402     case AND:
2403       if (remainder == 0)
2404         {
2405           if (generate)
2406             emit_constant_insn (cond,
2407                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
2408           return 1;
2409         }
2410       if (remainder == 0xffffffff)
2411         {
2412           if (reload_completed && rtx_equal_p (target, source))
2413             return 0;
2414           if (generate)
2415             emit_constant_insn (cond,
2416                                 gen_rtx_SET (VOIDmode, target, source));
2417           return 1;
2418         }
2419       can_invert = 1;
2420       break;
2421
2422     case XOR:
2423       if (remainder == 0)
2424         {
2425           if (reload_completed && rtx_equal_p (target, source))
2426             return 0;
2427           if (generate)
2428             emit_constant_insn (cond,
2429                                 gen_rtx_SET (VOIDmode, target, source));
2430           return 1;
2431         }
2432
2433       /* We don't know how to handle other cases yet.  */
2434       gcc_assert (remainder == 0xffffffff);
2435
2436       if (generate)
2437         emit_constant_insn (cond,
2438                             gen_rtx_SET (VOIDmode, target,
2439                                          gen_rtx_NOT (mode, source)));
2440       return 1;
2441
2442     case MINUS:
2443       /* We treat MINUS as (val - source), since (source - val) is always
2444          passed as (source + (-val)).  */
2445       if (remainder == 0)
2446         {
2447           if (generate)
2448             emit_constant_insn (cond,
2449                                 gen_rtx_SET (VOIDmode, target,
2450                                              gen_rtx_NEG (mode, source)));
2451           return 1;
2452         }
2453       if (const_ok_for_arm (val))
2454         {
2455           if (generate)
2456             emit_constant_insn (cond,
2457                                 gen_rtx_SET (VOIDmode, target,
2458                                              gen_rtx_MINUS (mode, GEN_INT (val),
2459                                                             source)));
2460           return 1;
2461         }
2462       can_negate = 1;
2463
2464       break;
2465
2466     default:
2467       gcc_unreachable ();
2468     }
2469
2470   /* If we can do it in one insn get out quickly.  */
2471   if (const_ok_for_arm (val)
2472       || (can_negate_initial && const_ok_for_arm (-val))
2473       || (can_invert && const_ok_for_arm (~val)))
2474     {
2475       if (generate)
2476         emit_constant_insn (cond,
2477                             gen_rtx_SET (VOIDmode, target,
2478                                          (source
2479                                           ? gen_rtx_fmt_ee (code, mode, source,
2480                                                             GEN_INT (val))
2481                                           : GEN_INT (val))));
2482       return 1;
2483     }
2484
2485   /* Calculate a few attributes that may be useful for specific
2486      optimizations.  */
2487   /* Count number of leading zeros.  */
2488   for (i = 31; i >= 0; i--)
2489     {
2490       if ((remainder & (1 << i)) == 0)
2491         clear_sign_bit_copies++;
2492       else
2493         break;
2494     }
2495
2496   /* Count number of leading 1's.  */
2497   for (i = 31; i >= 0; i--)
2498     {
2499       if ((remainder & (1 << i)) != 0)
2500         set_sign_bit_copies++;
2501       else
2502         break;
2503     }
2504
2505   /* Count number of trailing zero's.  */
2506   for (i = 0; i <= 31; i++)
2507     {
2508       if ((remainder & (1 << i)) == 0)
2509         clear_zero_bit_copies++;
2510       else
2511         break;
2512     }
2513
2514   /* Count number of trailing 1's.  */
2515   for (i = 0; i <= 31; i++)
2516     {
2517       if ((remainder & (1 << i)) != 0)
2518         set_zero_bit_copies++;
2519       else
2520         break;
2521     }
2522
2523   switch (code)
2524     {
2525     case SET:
2526       /* See if we can use movw.  */
2527       if (arm_arch_thumb2 && (remainder & 0xffff0000) == 0)
2528         {
2529           if (generate)
2530             emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
2531                                                    GEN_INT (val)));
2532           return 1;
2533         }
2534
2535       /* See if we can do this by sign_extending a constant that is known
2536          to be negative.  This is a good, way of doing it, since the shift
2537          may well merge into a subsequent insn.  */
2538       if (set_sign_bit_copies > 1)
2539         {
2540           if (const_ok_for_arm
2541               (temp1 = ARM_SIGN_EXTEND (remainder
2542                                         << (set_sign_bit_copies - 1))))
2543             {
2544               if (generate)
2545                 {
2546                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2547                   emit_constant_insn (cond,
2548                                       gen_rtx_SET (VOIDmode, new_src,
2549                                                    GEN_INT (temp1)));
2550                   emit_constant_insn (cond,
2551                                       gen_ashrsi3 (target, new_src,
2552                                                    GEN_INT (set_sign_bit_copies - 1)));
2553                 }
2554               return 2;
2555             }
2556           /* For an inverted constant, we will need to set the low bits,
2557              these will be shifted out of harm's way.  */
2558           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
2559           if (const_ok_for_arm (~temp1))
2560             {
2561               if (generate)
2562                 {
2563                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2564                   emit_constant_insn (cond,
2565                                       gen_rtx_SET (VOIDmode, new_src,
2566                                                    GEN_INT (temp1)));
2567                   emit_constant_insn (cond,
2568                                       gen_ashrsi3 (target, new_src,
2569                                                    GEN_INT (set_sign_bit_copies - 1)));
2570                 }
2571               return 2;
2572             }
2573         }
2574
2575       /* See if we can calculate the value as the difference between two
2576          valid immediates.  */
2577       if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
2578         {
2579           int topshift = clear_sign_bit_copies & ~1;
2580
2581           temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
2582                                    & (0xff000000 >> topshift));
2583
2584           /* If temp1 is zero, then that means the 9 most significant
2585              bits of remainder were 1 and we've caused it to overflow.
2586              When topshift is 0 we don't need to do anything since we
2587              can borrow from 'bit 32'.  */
2588           if (temp1 == 0 && topshift != 0)
2589             temp1 = 0x80000000 >> (topshift - 1);
2590
2591           temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
2592
2593           if (const_ok_for_arm (temp2))
2594             {
2595               if (generate)
2596                 {
2597                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2598                   emit_constant_insn (cond,
2599                                       gen_rtx_SET (VOIDmode, new_src,
2600                                                    GEN_INT (temp1)));
2601                   emit_constant_insn (cond,
2602                                       gen_addsi3 (target, new_src,
2603                                                   GEN_INT (-temp2)));
2604                 }
2605
2606               return 2;
2607             }
2608         }
2609
2610       /* See if we can generate this by setting the bottom (or the top)
2611          16 bits, and then shifting these into the other half of the
2612          word.  We only look for the simplest cases, to do more would cost
2613          too much.  Be careful, however, not to generate this when the
2614          alternative would take fewer insns.  */
2615       if (val & 0xffff0000)
2616         {
2617           temp1 = remainder & 0xffff0000;
2618           temp2 = remainder & 0x0000ffff;
2619
2620           /* Overlaps outside this range are best done using other methods.  */
2621           for (i = 9; i < 24; i++)
2622             {
2623               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
2624                   && !const_ok_for_arm (temp2))
2625                 {
2626                   rtx new_src = (subtargets
2627                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2628                                  : target);
2629                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2630                                             source, subtargets, generate);
2631                   source = new_src;
2632                   if (generate)
2633                     emit_constant_insn
2634                       (cond,
2635                        gen_rtx_SET
2636                        (VOIDmode, target,
2637                         gen_rtx_IOR (mode,
2638                                      gen_rtx_ASHIFT (mode, source,
2639                                                      GEN_INT (i)),
2640                                      source)));
2641                   return insns + 1;
2642                 }
2643             }
2644
2645           /* Don't duplicate cases already considered.  */
2646           for (i = 17; i < 24; i++)
2647             {
2648               if (((temp1 | (temp1 >> i)) == remainder)
2649                   && !const_ok_for_arm (temp1))
2650                 {
2651                   rtx new_src = (subtargets
2652                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2653                                  : target);
2654                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2655                                             source, subtargets, generate);
2656                   source = new_src;
2657                   if (generate)
2658                     emit_constant_insn
2659                       (cond,
2660                        gen_rtx_SET (VOIDmode, target,
2661                                     gen_rtx_IOR
2662                                     (mode,
2663                                      gen_rtx_LSHIFTRT (mode, source,
2664                                                        GEN_INT (i)),
2665                                      source)));
2666                   return insns + 1;
2667                 }
2668             }
2669         }
2670       break;
2671
2672     case IOR:
2673     case XOR:
2674       /* If we have IOR or XOR, and the constant can be loaded in a
2675          single instruction, and we can find a temporary to put it in,
2676          then this can be done in two instructions instead of 3-4.  */
2677       if (subtargets
2678           /* TARGET can't be NULL if SUBTARGETS is 0 */
2679           || (reload_completed && !reg_mentioned_p (target, source)))
2680         {
2681           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
2682             {
2683               if (generate)
2684                 {
2685                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2686
2687                   emit_constant_insn (cond,
2688                                       gen_rtx_SET (VOIDmode, sub,
2689                                                    GEN_INT (val)));
2690                   emit_constant_insn (cond,
2691                                       gen_rtx_SET (VOIDmode, target,
2692                                                    gen_rtx_fmt_ee (code, mode,
2693                                                                    source, sub)));
2694                 }
2695               return 2;
2696             }
2697         }
2698
2699       if (code == XOR)
2700         break;
2701
2702       /*  Convert.
2703           x = y | constant ( which is composed of set_sign_bit_copies of leading 1s
2704                              and the remainder 0s for e.g. 0xfff00000)
2705           x = ~(~(y ashift set_sign_bit_copies) lshiftrt set_sign_bit_copies)
2706
2707           This can be done in 2 instructions by using shifts with mov or mvn.
2708           e.g. for
2709           x = x | 0xfff00000;
2710           we generate.
2711           mvn   r0, r0, asl #12
2712           mvn   r0, r0, lsr #12  */
2713       if (set_sign_bit_copies > 8
2714           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
2715         {
2716           if (generate)
2717             {
2718               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2719               rtx shift = GEN_INT (set_sign_bit_copies);
2720
2721               emit_constant_insn
2722                 (cond,
2723                  gen_rtx_SET (VOIDmode, sub,
2724                               gen_rtx_NOT (mode,
2725                                            gen_rtx_ASHIFT (mode,
2726                                                            source,
2727                                                            shift))));
2728               emit_constant_insn
2729                 (cond,
2730                  gen_rtx_SET (VOIDmode, target,
2731                               gen_rtx_NOT (mode,
2732                                            gen_rtx_LSHIFTRT (mode, sub,
2733                                                              shift))));
2734             }
2735           return 2;
2736         }
2737
2738       /* Convert
2739           x = y | constant (which has set_zero_bit_copies number of trailing ones).
2740            to
2741           x = ~((~y lshiftrt set_zero_bit_copies) ashift set_zero_bit_copies).
2742
2743           For eg. r0 = r0 | 0xfff
2744                mvn      r0, r0, lsr #12
2745                mvn      r0, r0, asl #12
2746
2747       */
2748       if (set_zero_bit_copies > 8
2749           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2750         {
2751           if (generate)
2752             {
2753               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2754               rtx shift = GEN_INT (set_zero_bit_copies);
2755
2756               emit_constant_insn
2757                 (cond,
2758                  gen_rtx_SET (VOIDmode, sub,
2759                               gen_rtx_NOT (mode,
2760                                            gen_rtx_LSHIFTRT (mode,
2761                                                              source,
2762                                                              shift))));
2763               emit_constant_insn
2764                 (cond,
2765                  gen_rtx_SET (VOIDmode, target,
2766                               gen_rtx_NOT (mode,
2767                                            gen_rtx_ASHIFT (mode, sub,
2768                                                            shift))));
2769             }
2770           return 2;
2771         }
2772
2773       /* This will never be reached for Thumb2 because orn is a valid
2774          instruction. This is for Thumb1 and the ARM 32 bit cases.
2775
2776          x = y | constant (such that ~constant is a valid constant)
2777          Transform this to
2778          x = ~(~y & ~constant).
2779       */
2780       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
2781         {
2782           if (generate)
2783             {
2784               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2785               emit_constant_insn (cond,
2786                                   gen_rtx_SET (VOIDmode, sub,
2787                                                gen_rtx_NOT (mode, source)));
2788               source = sub;
2789               if (subtargets)
2790                 sub = gen_reg_rtx (mode);
2791               emit_constant_insn (cond,
2792                                   gen_rtx_SET (VOIDmode, sub,
2793                                                gen_rtx_AND (mode, source,
2794                                                             GEN_INT (temp1))));
2795               emit_constant_insn (cond,
2796                                   gen_rtx_SET (VOIDmode, target,
2797                                                gen_rtx_NOT (mode, sub)));
2798             }
2799           return 3;
2800         }
2801       break;
2802
2803     case AND:
2804       /* See if two shifts will do 2 or more insn's worth of work.  */
2805       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
2806         {
2807           HOST_WIDE_INT shift_mask = ((0xffffffff
2808                                        << (32 - clear_sign_bit_copies))
2809                                       & 0xffffffff);
2810
2811           if ((remainder | shift_mask) != 0xffffffff)
2812             {
2813               if (generate)
2814                 {
2815                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2816                   insns = arm_gen_constant (AND, mode, cond,
2817                                             remainder | shift_mask,
2818                                             new_src, source, subtargets, 1);
2819                   source = new_src;
2820                 }
2821               else
2822                 {
2823                   rtx targ = subtargets ? NULL_RTX : target;
2824                   insns = arm_gen_constant (AND, mode, cond,
2825                                             remainder | shift_mask,
2826                                             targ, source, subtargets, 0);
2827                 }
2828             }
2829
2830           if (generate)
2831             {
2832               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2833               rtx shift = GEN_INT (clear_sign_bit_copies);
2834
2835               emit_insn (gen_ashlsi3 (new_src, source, shift));
2836               emit_insn (gen_lshrsi3 (target, new_src, shift));
2837             }
2838
2839           return insns + 2;
2840         }
2841
2842       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2843         {
2844           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2845
2846           if ((remainder | shift_mask) != 0xffffffff)
2847             {
2848               if (generate)
2849                 {
2850                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2851
2852                   insns = arm_gen_constant (AND, mode, cond,
2853                                             remainder | shift_mask,
2854                                             new_src, source, subtargets, 1);
2855                   source = new_src;
2856                 }
2857               else
2858                 {
2859                   rtx targ = subtargets ? NULL_RTX : target;
2860
2861                   insns = arm_gen_constant (AND, mode, cond,
2862                                             remainder | shift_mask,
2863                                             targ, source, subtargets, 0);
2864                 }
2865             }
2866
2867           if (generate)
2868             {
2869               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2870               rtx shift = GEN_INT (clear_zero_bit_copies);
2871
2872               emit_insn (gen_lshrsi3 (new_src, source, shift));
2873               emit_insn (gen_ashlsi3 (target, new_src, shift));
2874             }
2875
2876           return insns + 2;
2877         }
2878
2879       break;
2880
2881     default:
2882       break;
2883     }
2884
2885   for (i = 0; i < 32; i++)
2886     if (remainder & (1 << i))
2887       num_bits_set++;
2888
2889   if ((code == AND)
2890       || (code != IOR && can_invert && num_bits_set > 16))
2891     remainder = (~remainder) & 0xffffffff;
2892   else if (code == PLUS && num_bits_set > 16)
2893     remainder = (-remainder) & 0xffffffff;
2894   else
2895     {
2896       can_invert = 0;
2897       can_negate = 0;
2898     }
2899
2900   /* Now try and find a way of doing the job in either two or three
2901      instructions.
2902      We start by looking for the largest block of zeros that are aligned on
2903      a 2-bit boundary, we then fill up the temps, wrapping around to the
2904      top of the word when we drop off the bottom.
2905      In the worst case this code should produce no more than four insns.
2906      Thumb-2 constants are shifted, not rotated, so the MSB is always the
2907      best place to start.  */
2908
2909   /* ??? Use thumb2 replicated constants when the high and low halfwords are
2910      the same.  */
2911   {
2912     int best_start = 0;
2913     if (!TARGET_THUMB2)
2914       {
2915         int best_consecutive_zeros = 0;
2916
2917         for (i = 0; i < 32; i += 2)
2918           {
2919             int consecutive_zeros = 0;
2920
2921             if (!(remainder & (3 << i)))
2922               {
2923                 while ((i < 32) && !(remainder & (3 << i)))
2924                   {
2925                     consecutive_zeros += 2;
2926                     i += 2;
2927                   }
2928                 if (consecutive_zeros > best_consecutive_zeros)
2929                   {
2930                     best_consecutive_zeros = consecutive_zeros;
2931                     best_start = i - consecutive_zeros;
2932                   }
2933                 i -= 2;
2934               }
2935           }
2936
2937         /* So long as it won't require any more insns to do so, it's
2938            desirable to emit a small constant (in bits 0...9) in the last
2939            insn.  This way there is more chance that it can be combined with
2940            a later addressing insn to form a pre-indexed load or store
2941            operation.  Consider:
2942
2943                    *((volatile int *)0xe0000100) = 1;
2944                    *((volatile int *)0xe0000110) = 2;
2945
2946            We want this to wind up as:
2947
2948                     mov rA, #0xe0000000
2949                     mov rB, #1
2950                     str rB, [rA, #0x100]
2951                     mov rB, #2
2952                     str rB, [rA, #0x110]
2953
2954            rather than having to synthesize both large constants from scratch.
2955
2956            Therefore, we calculate how many insns would be required to emit
2957            the constant starting from `best_start', and also starting from
2958            zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2959            yield a shorter sequence, we may as well use zero.  */
2960         if (best_start != 0
2961             && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2962             && (count_insns_for_constant (remainder, 0) <=
2963                 count_insns_for_constant (remainder, best_start)))
2964           best_start = 0;
2965       }
2966
2967     /* Now start emitting the insns.  */
2968     i = best_start;
2969     do
2970       {
2971         int end;
2972
2973         if (i <= 0)
2974           i += 32;
2975         if (remainder & (3 << (i - 2)))
2976           {
2977             end = i - 8;
2978             if (end < 0)
2979               end += 32;
2980             temp1 = remainder & ((0x0ff << end)
2981                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
2982             remainder &= ~temp1;
2983
2984             if (generate)
2985               {
2986                 rtx new_src, temp1_rtx;
2987
2988                 if (code == SET || code == MINUS)
2989                   {
2990                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
2991                     if (can_invert && code != MINUS)
2992                       temp1 = ~temp1;
2993                   }
2994                 else
2995                   {
2996                     if (remainder && subtargets)
2997                       new_src = gen_reg_rtx (mode);
2998                     else
2999                       new_src = target;
3000                     if (can_invert)
3001                       temp1 = ~temp1;
3002                     else if (can_negate)
3003                       temp1 = -temp1;
3004                   }
3005
3006                 temp1 = trunc_int_for_mode (temp1, mode);
3007                 temp1_rtx = GEN_INT (temp1);
3008
3009                 if (code == SET)
3010                   ;
3011                 else if (code == MINUS)
3012                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
3013                 else
3014                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
3015
3016                 emit_constant_insn (cond,
3017                                     gen_rtx_SET (VOIDmode, new_src,
3018                                                  temp1_rtx));
3019                 source = new_src;
3020               }
3021
3022             if (code == SET)
3023               {
3024                 can_invert = 0;
3025                 code = PLUS;
3026               }
3027             else if (code == MINUS)
3028               code = PLUS;
3029
3030             insns++;
3031             if (TARGET_ARM)
3032               i -= 6;
3033             else
3034               i -= 7;
3035           }
3036         /* Arm allows rotates by a multiple of two. Thumb-2 allows arbitrary
3037            shifts.  */
3038         if (TARGET_ARM)
3039           i -= 2;
3040         else
3041           i--;
3042       }
3043     while (remainder);
3044   }
3045
3046   return insns;
3047 }
3048
3049 /* Canonicalize a comparison so that we are more likely to recognize it.
3050    This can be done for a few constant compares, where we can make the
3051    immediate value easier to load.  */
3052
3053 enum rtx_code
3054 arm_canonicalize_comparison (enum rtx_code code, enum machine_mode mode,
3055                              rtx * op1)
3056 {
3057   unsigned HOST_WIDE_INT i = INTVAL (*op1);
3058   unsigned HOST_WIDE_INT maxval;
3059   maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
3060
3061   switch (code)
3062     {
3063     case EQ:
3064     case NE:
3065       return code;
3066
3067     case GT:
3068     case LE:
3069       if (i != maxval
3070           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
3071         {
3072           *op1 = GEN_INT (i + 1);
3073           return code == GT ? GE : LT;
3074         }
3075       break;
3076
3077     case GE:
3078     case LT:
3079       if (i != ~maxval
3080           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
3081         {
3082           *op1 = GEN_INT (i - 1);
3083           return code == GE ? GT : LE;
3084         }
3085       break;
3086
3087     case GTU:
3088     case LEU:
3089       if (i != ~((unsigned HOST_WIDE_INT) 0)
3090           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
3091         {
3092           *op1 = GEN_INT (i + 1);
3093           return code == GTU ? GEU : LTU;
3094         }
3095       break;
3096
3097     case GEU:
3098     case LTU:
3099       if (i != 0
3100           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
3101         {
3102           *op1 = GEN_INT (i - 1);
3103           return code == GEU ? GTU : LEU;
3104         }
3105       break;
3106
3107     default:
3108       gcc_unreachable ();
3109     }
3110
3111   return code;
3112 }
3113
3114
3115 /* Define how to find the value returned by a function.  */
3116
3117 static rtx
3118 arm_function_value(const_tree type, const_tree func,
3119                    bool outgoing ATTRIBUTE_UNUSED)
3120 {
3121   enum machine_mode mode;
3122   int unsignedp ATTRIBUTE_UNUSED;
3123   rtx r ATTRIBUTE_UNUSED;
3124
3125   mode = TYPE_MODE (type);
3126
3127   if (TARGET_AAPCS_BASED)
3128     return aapcs_allocate_return_reg (mode, type, func);
3129
3130   /* Promote integer types.  */
3131   if (INTEGRAL_TYPE_P (type))
3132     mode = arm_promote_function_mode (type, mode, &unsignedp, func, 1);
3133
3134   /* Promotes small structs returned in a register to full-word size
3135      for big-endian AAPCS.  */
3136   if (arm_return_in_msb (type))
3137     {
3138       HOST_WIDE_INT size = int_size_in_bytes (type);
3139       if (size % UNITS_PER_WORD != 0)
3140         {
3141           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
3142           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
3143         }
3144     }
3145
3146   return LIBCALL_VALUE (mode);
3147 }
3148
3149 static int
3150 libcall_eq (const void *p1, const void *p2)
3151 {
3152   return rtx_equal_p ((const_rtx) p1, (const_rtx) p2);
3153 }
3154
3155 static hashval_t
3156 libcall_hash (const void *p1)
3157 {
3158   return hash_rtx ((const_rtx) p1, VOIDmode, NULL, NULL, FALSE);
3159 }
3160
3161 static void
3162 add_libcall (htab_t htab, rtx libcall)
3163 {
3164   *htab_find_slot (htab, libcall, INSERT) = libcall;
3165 }
3166
3167 static bool
3168 arm_libcall_uses_aapcs_base (rtx libcall)
3169 {
3170   static bool init_done = false;
3171   static htab_t libcall_htab;
3172
3173   if (!init_done)
3174     {
3175       init_done = true;
3176
3177       libcall_htab = htab_create (31, libcall_hash, libcall_eq,
3178                                   NULL);
3179       add_libcall (libcall_htab,
3180                    convert_optab_libfunc (sfloat_optab, SFmode, SImode));
3181       add_libcall (libcall_htab,
3182                    convert_optab_libfunc (sfloat_optab, DFmode, SImode));
3183       add_libcall (libcall_htab,
3184                    convert_optab_libfunc (sfloat_optab, SFmode, DImode));
3185       add_libcall (libcall_htab,
3186                    convert_optab_libfunc (sfloat_optab, DFmode, DImode));
3187       
3188       add_libcall (libcall_htab,
3189                    convert_optab_libfunc (ufloat_optab, SFmode, SImode));
3190       add_libcall (libcall_htab,
3191                    convert_optab_libfunc (ufloat_optab, DFmode, SImode));
3192       add_libcall (libcall_htab,
3193                    convert_optab_libfunc (ufloat_optab, SFmode, DImode));
3194       add_libcall (libcall_htab,
3195                    convert_optab_libfunc (ufloat_optab, DFmode, DImode));
3196
3197       add_libcall (libcall_htab,
3198                    convert_optab_libfunc (sext_optab, SFmode, HFmode));
3199       add_libcall (libcall_htab,
3200                    convert_optab_libfunc (trunc_optab, HFmode, SFmode));
3201       add_libcall (libcall_htab,
3202                    convert_optab_libfunc (sfix_optab, DImode, DFmode));
3203       add_libcall (libcall_htab,
3204                    convert_optab_libfunc (ufix_optab, DImode, DFmode));
3205       add_libcall (libcall_htab,
3206                    convert_optab_libfunc (sfix_optab, DImode, SFmode));
3207       add_libcall (libcall_htab,
3208                    convert_optab_libfunc (ufix_optab, DImode, SFmode));
3209     }
3210
3211   return libcall && htab_find (libcall_htab, libcall) != NULL;
3212 }
3213
3214 rtx
3215 arm_libcall_value (enum machine_mode mode, rtx libcall)
3216 {
3217   if (TARGET_AAPCS_BASED && arm_pcs_default != ARM_PCS_AAPCS
3218       && GET_MODE_CLASS (mode) == MODE_FLOAT)
3219     {
3220       /* The following libcalls return their result in integer registers,
3221          even though they return a floating point value.  */
3222       if (arm_libcall_uses_aapcs_base (libcall))
3223         return gen_rtx_REG (mode, ARG_REGISTER(1));
3224
3225     }
3226
3227   return LIBCALL_VALUE (mode);
3228 }
3229
3230 /* Determine the amount of memory needed to store the possible return
3231    registers of an untyped call.  */
3232 int
3233 arm_apply_result_size (void)
3234 {
3235   int size = 16;
3236
3237   if (TARGET_32BIT)
3238     {
3239       if (TARGET_HARD_FLOAT_ABI)
3240         {
3241           if (TARGET_VFP)
3242             size += 32;
3243           if (TARGET_FPA)
3244             size += 12;
3245           if (TARGET_MAVERICK)
3246             size += 8;
3247         }
3248       if (TARGET_IWMMXT_ABI)
3249         size += 8;
3250     }
3251
3252   return size;
3253 }
3254
3255 /* Decide whether TYPE should be returned in memory (true)
3256    or in a register (false).  FNTYPE is the type of the function making
3257    the call.  */
3258 static bool
3259 arm_return_in_memory (const_tree type, const_tree fntype)
3260 {
3261   HOST_WIDE_INT size;
3262
3263   size = int_size_in_bytes (type);  /* Negative if not fixed size.  */
3264
3265   if (TARGET_AAPCS_BASED)
3266     {
3267       /* Simple, non-aggregate types (ie not including vectors and
3268          complex) are always returned in a register (or registers).
3269          We don't care about which register here, so we can short-cut
3270          some of the detail.  */
3271       if (!AGGREGATE_TYPE_P (type)
3272           && TREE_CODE (type) != VECTOR_TYPE
3273           && TREE_CODE (type) != COMPLEX_TYPE)
3274         return false;
3275
3276       /* Any return value that is no larger than one word can be
3277          returned in r0.  */
3278       if (((unsigned HOST_WIDE_INT) size) <= UNITS_PER_WORD)
3279         return false;
3280
3281       /* Check any available co-processors to see if they accept the
3282          type as a register candidate (VFP, for example, can return
3283          some aggregates in consecutive registers).  These aren't
3284          available if the call is variadic.  */
3285       if (aapcs_select_return_coproc (type, fntype) >= 0)
3286         return false;
3287
3288       /* Vector values should be returned using ARM registers, not
3289          memory (unless they're over 16 bytes, which will break since
3290          we only have four call-clobbered registers to play with).  */
3291       if (TREE_CODE (type) == VECTOR_TYPE)
3292         return (size < 0 || size > (4 * UNITS_PER_WORD));
3293
3294       /* The rest go in memory.  */
3295       return true;
3296     }
3297
3298   if (TREE_CODE (type) == VECTOR_TYPE)
3299     return (size < 0 || size > (4 * UNITS_PER_WORD));
3300
3301   if (!AGGREGATE_TYPE_P (type) &&
3302       (TREE_CODE (type) != VECTOR_TYPE))
3303     /* All simple types are returned in registers.  */
3304     return false;
3305
3306   if (arm_abi != ARM_ABI_APCS)
3307     {
3308       /* ATPCS and later return aggregate types in memory only if they are
3309          larger than a word (or are variable size).  */
3310       return (size < 0 || size > UNITS_PER_WORD);
3311     }
3312
3313   /* For the arm-wince targets we choose to be compatible with Microsoft's
3314      ARM and Thumb compilers, which always return aggregates in memory.  */
3315 #ifndef ARM_WINCE
3316   /* All structures/unions bigger than one word are returned in memory.
3317      Also catch the case where int_size_in_bytes returns -1.  In this case
3318      the aggregate is either huge or of variable size, and in either case
3319      we will want to return it via memory and not in a register.  */
3320   if (size < 0 || size > UNITS_PER_WORD)
3321     return true;
3322
3323   if (TREE_CODE (type) == RECORD_TYPE)
3324     {
3325       tree field;
3326
3327       /* For a struct the APCS says that we only return in a register
3328          if the type is 'integer like' and every addressable element
3329          has an offset of zero.  For practical purposes this means
3330          that the structure can have at most one non bit-field element
3331          and that this element must be the first one in the structure.  */
3332
3333       /* Find the first field, ignoring non FIELD_DECL things which will
3334          have been created by C++.  */
3335       for (field = TYPE_FIELDS (type);
3336            field && TREE_CODE (field) != FIELD_DECL;
3337            field = TREE_CHAIN (field))
3338         continue;
3339
3340       if (field == NULL)
3341         return false; /* An empty structure.  Allowed by an extension to ANSI C.  */
3342
3343       /* Check that the first field is valid for returning in a register.  */
3344
3345       /* ... Floats are not allowed */
3346       if (FLOAT_TYPE_P (TREE_TYPE (field)))
3347         return true;
3348
3349       /* ... Aggregates that are not themselves valid for returning in
3350          a register are not allowed.  */
3351       if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
3352         return true;
3353
3354       /* Now check the remaining fields, if any.  Only bitfields are allowed,
3355          since they are not addressable.  */
3356       for (field = TREE_CHAIN (field);
3357            field;
3358            field = TREE_CHAIN (field))
3359         {
3360           if (TREE_CODE (field) != FIELD_DECL)
3361             continue;
3362
3363           if (!DECL_BIT_FIELD_TYPE (field))
3364             return true;
3365         }
3366
3367       return false;
3368     }
3369
3370   if (TREE_CODE (type) == UNION_TYPE)
3371     {
3372       tree field;
3373
3374       /* Unions can be returned in registers if every element is
3375          integral, or can be returned in an integer register.  */
3376       for (field = TYPE_FIELDS (type);
3377            field;
3378            field = TREE_CHAIN (field))
3379         {
3380           if (TREE_CODE (field) != FIELD_DECL)
3381             continue;
3382
3383           if (FLOAT_TYPE_P (TREE_TYPE (field)))
3384             return true;
3385
3386           if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
3387             return true;
3388         }
3389
3390       return false;
3391     }
3392 #endif /* not ARM_WINCE */
3393
3394   /* Return all other types in memory.  */
3395   return true;
3396 }
3397
3398 /* Indicate whether or not words of a double are in big-endian order.  */
3399
3400 int
3401 arm_float_words_big_endian (void)
3402 {
3403   if (TARGET_MAVERICK)
3404     return 0;
3405
3406   /* For FPA, float words are always big-endian.  For VFP, floats words
3407      follow the memory system mode.  */
3408
3409   if (TARGET_FPA)
3410     {
3411       return 1;
3412     }
3413
3414   if (TARGET_VFP)
3415     return (TARGET_BIG_END ? 1 : 0);
3416
3417   return 1;
3418 }
3419
3420 const struct pcs_attribute_arg
3421 {
3422   const char *arg;
3423   enum arm_pcs value;
3424 } pcs_attribute_args[] =
3425   {
3426     {"aapcs", ARM_PCS_AAPCS},
3427     {"aapcs-vfp", ARM_PCS_AAPCS_VFP},
3428 #if 0
3429     /* We could recognize these, but changes would be needed elsewhere
3430      * to implement them.  */
3431     {"aapcs-iwmmxt", ARM_PCS_AAPCS_IWMMXT},
3432     {"atpcs", ARM_PCS_ATPCS},
3433     {"apcs", ARM_PCS_APCS},
3434 #endif
3435     {NULL, ARM_PCS_UNKNOWN}
3436   };
3437
3438 static enum arm_pcs
3439 arm_pcs_from_attribute (tree attr)
3440 {
3441   const struct pcs_attribute_arg *ptr;
3442   const char *arg;
3443
3444   /* Get the value of the argument.  */
3445   if (TREE_VALUE (attr) == NULL_TREE
3446       || TREE_CODE (TREE_VALUE (attr)) != STRING_CST)
3447     return ARM_PCS_UNKNOWN;
3448
3449   arg = TREE_STRING_POINTER (TREE_VALUE (attr));
3450
3451   /* Check it against the list of known arguments.  */
3452   for (ptr = pcs_attribute_args; ptr->arg != NULL; ptr++)
3453     if (streq (arg, ptr->arg))
3454       return ptr->value;
3455
3456   /* An unrecognized interrupt type.  */
3457   return ARM_PCS_UNKNOWN;
3458 }
3459
3460 /* Get the PCS variant to use for this call.  TYPE is the function's type
3461    specification, DECL is the specific declartion.  DECL may be null if
3462    the call could be indirect or if this is a library call.  */
3463 static enum arm_pcs
3464 arm_get_pcs_model (const_tree type, const_tree decl)
3465 {
3466   bool user_convention = false;
3467   enum arm_pcs user_pcs = arm_pcs_default;
3468   tree attr;
3469
3470   gcc_assert (type);
3471
3472   attr = lookup_attribute ("pcs", TYPE_ATTRIBUTES (type));
3473   if (attr)
3474     {
3475       user_pcs = arm_pcs_from_attribute (TREE_VALUE (attr));
3476       user_convention = true;
3477     }
3478
3479   if (TARGET_AAPCS_BASED)
3480     {
3481       /* Detect varargs functions.  These always use the base rules
3482          (no argument is ever a candidate for a co-processor
3483          register).  */
3484       bool base_rules = (TYPE_ARG_TYPES (type) != 0
3485                          && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (type)))
3486                              != void_type_node));
3487       
3488       if (user_convention)
3489         {
3490           if (user_pcs > ARM_PCS_AAPCS_LOCAL)
3491             sorry ("Non-AAPCS derived PCS variant");
3492           else if (base_rules && user_pcs != ARM_PCS_AAPCS)
3493             error ("Variadic functions must use the base AAPCS variant");
3494         }
3495
3496       if (base_rules)
3497         return ARM_PCS_AAPCS;
3498       else if (user_convention)
3499         return user_pcs;
3500       else if (decl && flag_unit_at_a_time)
3501         {
3502           /* Local functions never leak outside this compilation unit,
3503              so we are free to use whatever conventions are
3504              appropriate.  */
3505           /* FIXME: remove CONST_CAST_TREE when cgraph is constified.  */
3506           struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE(decl));
3507           if (i && i->local)
3508             return ARM_PCS_AAPCS_LOCAL;
3509         }
3510     }
3511   else if (user_convention && user_pcs != arm_pcs_default)
3512     sorry ("PCS variant");
3513
3514   /* For everything else we use the target's default.  */
3515   return arm_pcs_default;
3516 }
3517
3518
3519 static void
3520 aapcs_vfp_cum_init (CUMULATIVE_ARGS *pcum  ATTRIBUTE_UNUSED,
3521                     const_tree fntype ATTRIBUTE_UNUSED,
3522                     rtx libcall ATTRIBUTE_UNUSED, 
3523                     const_tree fndecl ATTRIBUTE_UNUSED)
3524 {
3525   /* Record the unallocated VFP registers.  */
3526   pcum->aapcs_vfp_regs_free = (1 << NUM_VFP_ARG_REGS) - 1;
3527   pcum->aapcs_vfp_reg_alloc = 0;
3528 }
3529
3530 /* Walk down the type tree of TYPE counting consecutive base elements.
3531    If *MODEP is VOIDmode, then set it to the first valid floating point
3532    type.  If a non-floating point type is found, or if a floating point
3533    type that doesn't match a non-VOIDmode *MODEP is found, then return -1,
3534    otherwise return the count in the sub-tree.  */
3535 static int
3536 aapcs_vfp_sub_candidate (const_tree type, enum machine_mode *modep)
3537 {
3538   enum machine_mode mode;
3539   HOST_WIDE_INT size;
3540
3541   switch (TREE_CODE (type))
3542     {
3543     case REAL_TYPE:
3544       mode = TYPE_MODE (type);
3545       if (mode != DFmode && mode != SFmode)
3546         return -1;
3547
3548       if (*modep == VOIDmode)
3549         *modep = mode;
3550
3551       if (*modep == mode)
3552         return 1;
3553
3554       break;
3555
3556     case COMPLEX_TYPE:
3557       mode = TYPE_MODE (TREE_TYPE (type));
3558       if (mode != DFmode && mode != SFmode)
3559         return -1;
3560
3561       if (*modep == VOIDmode)
3562         *modep = mode;
3563
3564       if (*modep == mode)
3565         return 2;
3566
3567       break;
3568
3569     case VECTOR_TYPE:
3570       /* Use V2SImode and V4SImode as representatives of all 64-bit
3571          and 128-bit vector types, whether or not those modes are
3572          supported with the present options.  */
3573       size = int_size_in_bytes (type);
3574       switch (size)
3575         {
3576         case 8:
3577           mode = V2SImode;
3578           break;
3579         case 16:
3580           mode = V4SImode;
3581           break;
3582         default:
3583           return -1;
3584         }
3585
3586       if (*modep == VOIDmode)
3587         *modep = mode;
3588
3589       /* Vector modes are considered to be opaque: two vectors are
3590          equivalent for the purposes of being homogeneous aggregates
3591          if they are the same size.  */
3592       if (*modep == mode)
3593         return 1;
3594
3595       break;
3596
3597     case ARRAY_TYPE:
3598       {
3599         int count;
3600         tree index = TYPE_DOMAIN (type);
3601
3602         /* Can't handle incomplete types.  */
3603         if (!COMPLETE_TYPE_P(type))
3604           return -1;
3605
3606         count = aapcs_vfp_sub_candidate (TREE_TYPE (type), modep);
3607         if (count == -1
3608             || !index
3609             || !TYPE_MAX_VALUE (index)
3610             || !host_integerp (TYPE_MAX_VALUE (index), 1)
3611             || !TYPE_MIN_VALUE (index)
3612             || !host_integerp (TYPE_MIN_VALUE (index), 1)
3613             || count < 0)
3614           return -1;
3615
3616         count *= (1 + tree_low_cst (TYPE_MAX_VALUE (index), 1)
3617                       - tree_low_cst (TYPE_MIN_VALUE (index), 1));
3618
3619         /* There must be no padding.  */
3620         if (!host_integerp (TYPE_SIZE (type), 1)
3621             || (tree_low_cst (TYPE_SIZE (type), 1)
3622                 != count * GET_MODE_BITSIZE (*modep)))
3623           return -1;
3624
3625         return count;
3626       }
3627       
3628     case RECORD_TYPE:
3629       {
3630         int count = 0;
3631         int sub_count;
3632         tree field;
3633
3634         /* Can't handle incomplete types.  */
3635         if (!COMPLETE_TYPE_P(type))
3636           return -1;
3637
3638         for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3639           {
3640             if (TREE_CODE (field) != FIELD_DECL)
3641               continue;
3642
3643             sub_count = aapcs_vfp_sub_candidate (TREE_TYPE (field), modep);
3644             if (sub_count < 0)
3645               return -1;
3646             count += sub_count;
3647           }
3648
3649         /* There must be no padding.  */
3650         if (!host_integerp (TYPE_SIZE (type), 1)
3651             || (tree_low_cst (TYPE_SIZE (type), 1)
3652                 != count * GET_MODE_BITSIZE (*modep)))
3653           return -1;
3654
3655         return count;
3656       }
3657
3658     case UNION_TYPE:
3659     case QUAL_UNION_TYPE:
3660       {
3661         /* These aren't very interesting except in a degenerate case.  */
3662         int count = 0;
3663         int sub_count;
3664         tree field;
3665
3666         /* Can't handle incomplete types.  */
3667         if (!COMPLETE_TYPE_P(type))
3668           return -1;
3669
3670         for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3671           {
3672             if (TREE_CODE (field) != FIELD_DECL)
3673               continue;
3674
3675             sub_count = aapcs_vfp_sub_candidate (TREE_TYPE (field), modep);
3676             if (sub_count < 0)
3677               return -1;
3678             count = count > sub_count ? count : sub_count;
3679           }
3680
3681         /* There must be no padding.  */
3682         if (!host_integerp (TYPE_SIZE (type), 1)
3683             || (tree_low_cst (TYPE_SIZE (type), 1)
3684                 != count * GET_MODE_BITSIZE (*modep)))
3685           return -1;
3686
3687         return count;
3688       }
3689
3690     default:
3691       break;
3692     }
3693
3694   return -1;
3695 }
3696
3697 static bool
3698 aapcs_vfp_is_call_or_return_candidate (enum machine_mode mode, const_tree type,
3699                                        int *base_mode,
3700                                        int *count)
3701 {
3702   if (GET_MODE_CLASS (mode) == MODE_FLOAT
3703       || GET_MODE_CLASS (mode) == MODE_VECTOR_INT
3704       || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
3705     {
3706       *count = 1;
3707       *base_mode = mode;
3708       return true;
3709     }
3710   else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3711     {
3712       *count = 2;
3713       *base_mode = (mode == DCmode ? DFmode : SFmode);
3714       return true;
3715     }
3716   else if (type && (mode == BLKmode || TREE_CODE (type) == VECTOR_TYPE))
3717     {
3718       enum machine_mode aggregate_mode = VOIDmode;
3719       int ag_count = aapcs_vfp_sub_candidate (type, &aggregate_mode);
3720
3721       if (ag_count > 0 && ag_count <= 4)
3722         {
3723           *count = ag_count;
3724           *base_mode = aggregate_mode;
3725           return true;
3726         }
3727     }
3728   return false;
3729 }
3730
3731 static bool
3732 aapcs_vfp_is_return_candidate (enum arm_pcs pcs_variant,
3733                                enum machine_mode mode, const_tree type)
3734 {
3735   int count ATTRIBUTE_UNUSED;
3736   int ag_mode ATTRIBUTE_UNUSED;
3737
3738   if (!(pcs_variant == ARM_PCS_AAPCS_VFP
3739         || (pcs_variant == ARM_PCS_AAPCS_LOCAL
3740             && TARGET_32BIT && TARGET_VFP && TARGET_HARD_FLOAT)))
3741     return false;
3742   return aapcs_vfp_is_call_or_return_candidate (mode, type, &ag_mode, &count);
3743 }
3744
3745 static bool
3746 aapcs_vfp_is_call_candidate (CUMULATIVE_ARGS *pcum, enum machine_mode mode, 
3747                              const_tree type)
3748 {
3749   if (!(pcum->pcs_variant == ARM_PCS_AAPCS_VFP
3750         || (pcum->pcs_variant == ARM_PCS_AAPCS_LOCAL
3751             && TARGET_32BIT && TARGET_VFP && TARGET_HARD_FLOAT)))
3752     return false;
3753   return aapcs_vfp_is_call_or_return_candidate (mode, type,
3754                                                 &pcum->aapcs_vfp_rmode,
3755                                                 &pcum->aapcs_vfp_rcount);
3756 }
3757
3758 static bool
3759 aapcs_vfp_allocate (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
3760                     const_tree type  ATTRIBUTE_UNUSED)
3761 {
3762   int shift = GET_MODE_SIZE (pcum->aapcs_vfp_rmode) / GET_MODE_SIZE (SFmode);
3763   unsigned mask = (1 << (shift * pcum->aapcs_vfp_rcount)) - 1;
3764   int regno;
3765   
3766   for (regno = 0; regno < NUM_VFP_ARG_REGS; regno += shift)
3767     if (((pcum->aapcs_vfp_regs_free >> regno) & mask) == mask)
3768       {
3769         pcum->aapcs_vfp_reg_alloc = mask << regno;
3770         if (mode == BLKmode || (mode == TImode && !TARGET_NEON))
3771           {
3772             int i;
3773             int rcount = pcum->aapcs_vfp_rcount;
3774             int rshift = shift;
3775             enum machine_mode rmode = pcum->aapcs_vfp_rmode;
3776             rtx par;
3777             if (!TARGET_NEON)
3778               {
3779                 /* Avoid using unsupported vector modes.  */
3780                 if (rmode == V2SImode)
3781                   rmode = DImode;
3782                 else if (rmode == V4SImode)
3783                   {
3784                     rmode = DImode;
3785                     rcount *= 2;
3786                     rshift /= 2;
3787                   }
3788               }
3789             par = gen_rtx_PARALLEL (mode, rtvec_alloc (rcount));
3790             for (i = 0; i < rcount; i++)
3791               {
3792                 rtx tmp = gen_rtx_REG (rmode, 
3793                                        FIRST_VFP_REGNUM + regno + i * rshift);
3794                 tmp = gen_rtx_EXPR_LIST
3795                   (VOIDmode, tmp, 
3796                    GEN_INT (i * GET_MODE_SIZE (rmode)));
3797                 XVECEXP (par, 0, i) = tmp;
3798               }
3799
3800             pcum->aapcs_reg = par;
3801           }
3802         else
3803           pcum->aapcs_reg = gen_rtx_REG (mode, FIRST_VFP_REGNUM + regno);
3804         return true;
3805       }
3806   return false;
3807 }
3808
3809 static rtx
3810 aapcs_vfp_allocate_return_reg (enum arm_pcs pcs_variant ATTRIBUTE_UNUSED,
3811                                enum machine_mode mode,
3812                                const_tree type ATTRIBUTE_UNUSED)
3813 {
3814   if (!(pcs_variant == ARM_PCS_AAPCS_VFP
3815         || (pcs_variant == ARM_PCS_AAPCS_LOCAL
3816             && TARGET_32BIT && TARGET_VFP && TARGET_HARD_FLOAT)))
3817     return false;
3818   if (mode == BLKmode || (mode == TImode && !TARGET_NEON))
3819     {
3820       int count;
3821       int ag_mode;
3822       int i;
3823       rtx par;
3824       int shift;
3825       
3826       aapcs_vfp_is_call_or_return_candidate (mode, type, &ag_mode, &count);
3827
3828       if (!TARGET_NEON)
3829         {
3830           if (ag_mode == V2SImode)
3831             ag_mode = DImode;
3832           else if (ag_mode == V4SImode)
3833             {
3834               ag_mode = DImode;
3835               count *= 2;
3836             }
3837         }
3838       shift = GET_MODE_SIZE(ag_mode) / GET_MODE_SIZE(SFmode);
3839       par = gen_rtx_PARALLEL (mode, rtvec_alloc (count));
3840       for (i = 0; i < count; i++)
3841         {
3842           rtx tmp = gen_rtx_REG (ag_mode, FIRST_VFP_REGNUM + i * shift);
3843           tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, 
3844                                    GEN_INT (i * GET_MODE_SIZE (ag_mode)));
3845           XVECEXP (par, 0, i) = tmp;
3846         }
3847
3848       return par;
3849     }
3850
3851   return gen_rtx_REG (mode, FIRST_VFP_REGNUM);
3852 }
3853
3854 static void
3855 aapcs_vfp_advance (CUMULATIVE_ARGS *pcum  ATTRIBUTE_UNUSED,
3856                    enum machine_mode mode  ATTRIBUTE_UNUSED,
3857                    const_tree type  ATTRIBUTE_UNUSED)
3858 {
3859   pcum->aapcs_vfp_regs_free &= ~pcum->aapcs_vfp_reg_alloc;
3860   pcum->aapcs_vfp_reg_alloc = 0;
3861   return;
3862 }
3863
3864 #define AAPCS_CP(X)                             \
3865   {                                             \
3866     aapcs_ ## X ## _cum_init,                   \
3867     aapcs_ ## X ## _is_call_candidate,          \
3868     aapcs_ ## X ## _allocate,                   \
3869     aapcs_ ## X ## _is_return_candidate,        \
3870     aapcs_ ## X ## _allocate_return_reg,        \
3871     aapcs_ ## X ## _advance                     \
3872   }
3873
3874 /* Table of co-processors that can be used to pass arguments in
3875    registers.  Idealy no arugment should be a candidate for more than
3876    one co-processor table entry, but the table is processed in order
3877    and stops after the first match.  If that entry then fails to put
3878    the argument into a co-processor register, the argument will go on
3879    the stack.  */
3880 static struct 
3881 {
3882   /* Initialize co-processor related state in CUMULATIVE_ARGS structure.  */
3883   void (*cum_init) (CUMULATIVE_ARGS *, const_tree, rtx, const_tree);
3884
3885   /* Return true if an argument of mode MODE (or type TYPE if MODE is
3886      BLKmode) is a candidate for this co-processor's registers; this
3887      function should ignore any position-dependent state in
3888      CUMULATIVE_ARGS and only use call-type dependent information.  */
3889   bool (*is_call_candidate) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
3890
3891   /* Return true if the argument does get a co-processor register; it
3892      should set aapcs_reg to an RTX of the register allocated as is
3893      required for a return from FUNCTION_ARG.  */
3894   bool (*allocate) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
3895
3896   /* Return true if a result of mode MODE (or type TYPE if MODE is
3897      BLKmode) is can be returned in this co-processor's registers.  */
3898   bool (*is_return_candidate) (enum arm_pcs, enum machine_mode, const_tree);
3899
3900   /* Allocate and return an RTX element to hold the return type of a
3901      call, this routine must not fail and will only be called if
3902      is_return_candidate returned true with the same parameters.  */
3903   rtx (*allocate_return_reg) (enum arm_pcs, enum machine_mode, const_tree);
3904
3905   /* Finish processing this argument and prepare to start processing
3906      the next one.  */
3907   void (*advance) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
3908 } aapcs_cp_arg_layout[ARM_NUM_COPROC_SLOTS] =
3909   {
3910     AAPCS_CP(vfp)
3911   };
3912
3913 #undef AAPCS_CP
3914
3915 static int
3916 aapcs_select_call_coproc (CUMULATIVE_ARGS *pcum, enum machine_mode mode, 
3917                           tree type)
3918 {
3919   int i;
3920
3921   for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
3922     if (aapcs_cp_arg_layout[i].is_call_candidate (pcum, mode, type))
3923       return i;
3924
3925   return -1;
3926 }
3927
3928 static int
3929 aapcs_select_return_coproc (const_tree type, const_tree fntype)
3930 {
3931   /* We aren't passed a decl, so we can't check that a call is local.
3932      However, it isn't clear that that would be a win anyway, since it
3933      might limit some tail-calling opportunities.  */
3934   enum arm_pcs pcs_variant;
3935
3936   if (fntype)
3937     {
3938       const_tree fndecl = NULL_TREE;
3939
3940       if (TREE_CODE (fntype) == FUNCTION_DECL)
3941         {
3942           fndecl = fntype;
3943           fntype = TREE_TYPE (fntype);
3944         }
3945
3946       pcs_variant = arm_get_pcs_model (fntype, fndecl);
3947     }
3948   else
3949     pcs_variant = arm_pcs_default;
3950
3951   if (pcs_variant != ARM_PCS_AAPCS)
3952     {
3953       int i;
3954
3955       for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
3956         if (aapcs_cp_arg_layout[i].is_return_candidate (pcs_variant, 
3957                                                         TYPE_MODE (type),
3958                                                         type))
3959           return i;
3960     }
3961   return -1;
3962 }
3963
3964 static rtx
3965 aapcs_allocate_return_reg (enum machine_mode mode, const_tree type,
3966                            const_tree fntype)
3967 {
3968   /* We aren't passed a decl, so we can't check that a call is local.
3969      However, it isn't clear that that would be a win anyway, since it
3970      might limit some tail-calling opportunities.  */
3971   enum arm_pcs pcs_variant;
3972   int unsignedp ATTRIBUTE_UNUSED;
3973
3974   if (fntype)
3975     {
3976       const_tree fndecl = NULL_TREE;
3977
3978       if (TREE_CODE (fntype) == FUNCTION_DECL)
3979         {
3980           fndecl = fntype;
3981           fntype = TREE_TYPE (fntype);
3982         }
3983
3984       pcs_variant = arm_get_pcs_model (fntype, fndecl);
3985     }
3986   else
3987     pcs_variant = arm_pcs_default;
3988
3989   /* Promote integer types.  */
3990   if (type && INTEGRAL_TYPE_P (type))
3991     mode = arm_promote_function_mode (type, mode, &unsignedp, fntype, 1);
3992
3993   if (pcs_variant != ARM_PCS_AAPCS)
3994     {
3995       int i;
3996
3997       for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
3998         if (aapcs_cp_arg_layout[i].is_return_candidate (pcs_variant, mode,
3999                                                         type))
4000           return aapcs_cp_arg_layout[i].allocate_return_reg (pcs_variant,
4001                                                              mode, type);
4002     }
4003
4004   /* Promotes small structs returned in a register to full-word size
4005      for big-endian AAPCS.  */
4006   if (type && arm_return_in_msb (type))
4007     {
4008       HOST_WIDE_INT size = int_size_in_bytes (type);
4009       if (size % UNITS_PER_WORD != 0)
4010         {
4011           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
4012           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
4013         }
4014     }
4015
4016   return gen_rtx_REG (mode, R0_REGNUM);
4017 }
4018
4019 rtx
4020 aapcs_libcall_value (enum machine_mode mode)
4021 {
4022   return aapcs_allocate_return_reg (mode, NULL_TREE, NULL_TREE);
4023 }
4024
4025 /* Lay out a function argument using the AAPCS rules.  The rule
4026    numbers referred to here are those in the AAPCS.  */
4027 static void
4028 aapcs_layout_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4029                   tree type, int named)
4030 {
4031   int nregs, nregs2;
4032   int ncrn;
4033
4034   /* We only need to do this once per argument.  */
4035   if (pcum->aapcs_arg_processed)
4036     return;
4037
4038   pcum->aapcs_arg_processed = true;
4039
4040   /* Special case: if named is false then we are handling an incoming
4041      anonymous argument which is on the stack.  */
4042   if (!named)
4043     return;
4044   
4045   /* Is this a potential co-processor register candidate?  */
4046   if (pcum->pcs_variant != ARM_PCS_AAPCS)
4047     {
4048       int slot = aapcs_select_call_coproc (pcum, mode, type);
4049       pcum->aapcs_cprc_slot = slot;
4050
4051       /* We don't have to apply any of the rules from part B of the
4052          preparation phase, these are handled elsewhere in the
4053          compiler.  */
4054
4055       if (slot >= 0)
4056         {
4057           /* A Co-processor register candidate goes either in its own
4058              class of registers or on the stack.  */
4059           if (!pcum->aapcs_cprc_failed[slot])
4060             {
4061               /* C1.cp - Try to allocate the argument to co-processor
4062                  registers.  */
4063               if (aapcs_cp_arg_layout[slot].allocate (pcum, mode, type))
4064                 return;
4065
4066               /* C2.cp - Put the argument on the stack and note that we
4067                  can't assign any more candidates in this slot.  We also
4068                  need to note that we have allocated stack space, so that
4069                  we won't later try to split a non-cprc candidate between
4070                  core registers and the stack.  */
4071               pcum->aapcs_cprc_failed[slot] = true;
4072               pcum->can_split = false;
4073             }
4074
4075           /* We didn't get a register, so this argument goes on the
4076              stack.  */
4077           gcc_assert (pcum->can_split == false);
4078           return;
4079         }
4080     }
4081
4082   /* C3 - For double-word aligned arguments, round the NCRN up to the
4083      next even number.  */
4084   ncrn = pcum->aapcs_ncrn;
4085   if ((ncrn & 1) && arm_needs_doubleword_align (mode, type))
4086     ncrn++;
4087
4088   nregs = ARM_NUM_REGS2(mode, type);
4089
4090   /* Sigh, this test should really assert that nregs > 0, but a GCC
4091      extension allows empty structs and then gives them empty size; it
4092      then allows such a structure to be passed by value.  For some of
4093      the code below we have to pretend that such an argument has
4094      non-zero size so that we 'locate' it correctly either in
4095      registers or on the stack.  */
4096   gcc_assert (nregs >= 0);
4097
4098   nregs2 = nregs ? nregs : 1;
4099
4100   /* C4 - Argument fits entirely in core registers.  */
4101   if (ncrn + nregs2 <= NUM_ARG_REGS)
4102     {
4103       pcum->aapcs_reg = gen_rtx_REG (mode, ncrn);
4104       pcum->aapcs_next_ncrn = ncrn + nregs;
4105       return;
4106     }
4107
4108   /* C5 - Some core registers left and there are no arguments already
4109      on the stack: split this argument between the remaining core
4110      registers and the stack.  */
4111   if (ncrn < NUM_ARG_REGS && pcum->can_split)
4112     {
4113       pcum->aapcs_reg = gen_rtx_REG (mode, ncrn);
4114       pcum->aapcs_next_ncrn = NUM_ARG_REGS;
4115       pcum->aapcs_partial = (NUM_ARG_REGS - ncrn) * UNITS_PER_WORD;
4116       return;
4117     }
4118
4119   /* C6 - NCRN is set to 4.  */
4120   pcum->aapcs_next_ncrn = NUM_ARG_REGS;
4121
4122   /* C7,C8 - arugment goes on the stack.  We have nothing to do here.  */
4123   return;
4124 }
4125
4126 /* Initialize a variable CUM of type CUMULATIVE_ARGS
4127    for a call to a function whose data type is FNTYPE.
4128    For a library call, FNTYPE is NULL.  */
4129 void
4130 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
4131                           rtx libname,
4132                           tree fndecl ATTRIBUTE_UNUSED)
4133 {
4134   /* Long call handling.  */
4135   if (fntype)
4136     pcum->pcs_variant = arm_get_pcs_model (fntype, fndecl);
4137   else
4138     pcum->pcs_variant = arm_pcs_default;
4139
4140   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4141     {
4142       if (arm_libcall_uses_aapcs_base (libname))
4143         pcum->pcs_variant = ARM_PCS_AAPCS;
4144  
4145       pcum->aapcs_ncrn = pcum->aapcs_next_ncrn = 0;
4146       pcum->aapcs_reg = NULL_RTX;
4147       pcum->aapcs_partial = 0;
4148       pcum->aapcs_arg_processed = false;
4149       pcum->aapcs_cprc_slot = -1;
4150       pcum->can_split = true;
4151
4152       if (pcum->pcs_variant != ARM_PCS_AAPCS)
4153         {
4154           int i;
4155
4156           for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4157             {
4158               pcum->aapcs_cprc_failed[i] = false;
4159               aapcs_cp_arg_layout[i].cum_init (pcum, fntype, libname, fndecl);
4160             }
4161         }
4162       return;
4163     }
4164
4165   /* Legacy ABIs */
4166
4167   /* On the ARM, the offset starts at 0.  */
4168   pcum->nregs = 0;
4169   pcum->iwmmxt_nregs = 0;
4170   pcum->can_split = true;
4171
4172   /* Varargs vectors are treated the same as long long.
4173      named_count avoids having to change the way arm handles 'named' */
4174   pcum->named_count = 0;
4175   pcum->nargs = 0;
4176
4177   if (TARGET_REALLY_IWMMXT && fntype)
4178     {
4179       tree fn_arg;
4180
4181       for (fn_arg = TYPE_ARG_TYPES (fntype);
4182            fn_arg;
4183            fn_arg = TREE_CHAIN (fn_arg))
4184         pcum->named_count += 1;
4185
4186       if (! pcum->named_count)
4187         pcum->named_count = INT_MAX;
4188     }
4189 }
4190
4191
4192 /* Return true if mode/type need doubleword alignment.  */
4193 bool
4194 arm_needs_doubleword_align (enum machine_mode mode, tree type)
4195 {
4196   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
4197           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
4198 }
4199
4200
4201 /* Determine where to put an argument to a function.
4202    Value is zero to push the argument on the stack,
4203    or a hard register in which to store the argument.
4204
4205    MODE is the argument's machine mode.
4206    TYPE is the data type of the argument (as a tree).
4207     This is null for libcalls where that information may
4208     not be available.
4209    CUM is a variable of type CUMULATIVE_ARGS which gives info about
4210     the preceding args and about the function being called.
4211    NAMED is nonzero if this argument is a named parameter
4212     (otherwise it is an extra parameter matching an ellipsis).  */
4213
4214 rtx
4215 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4216                   tree type, int named)
4217 {
4218   int nregs;
4219
4220   /* Handle the special case quickly.  Pick an arbitrary value for op2 of
4221      a call insn (op3 of a call_value insn).  */
4222   if (mode == VOIDmode)
4223     return const0_rtx;
4224
4225   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4226     {
4227       aapcs_layout_arg (pcum, mode, type, named);
4228       return pcum->aapcs_reg;
4229     }
4230
4231   /* Varargs vectors are treated the same as long long.
4232      named_count avoids having to change the way arm handles 'named' */
4233   if (TARGET_IWMMXT_ABI
4234       && arm_vector_mode_supported_p (mode)
4235       && pcum->named_count > pcum->nargs + 1)
4236     {
4237       if (pcum->iwmmxt_nregs <= 9)
4238         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
4239       else
4240         {
4241           pcum->can_split = false;
4242           return NULL_RTX;
4243         }
4244     }
4245
4246   /* Put doubleword aligned quantities in even register pairs.  */
4247   if (pcum->nregs & 1
4248       && ARM_DOUBLEWORD_ALIGN
4249       && arm_needs_doubleword_align (mode, type))
4250     pcum->nregs++;
4251
4252   if (mode == VOIDmode)
4253     /* Pick an arbitrary value for operand 2 of the call insn.  */
4254     return const0_rtx;
4255
4256   /* Only allow splitting an arg between regs and memory if all preceding
4257      args were allocated to regs.  For args passed by reference we only count
4258      the reference pointer.  */
4259   if (pcum->can_split)
4260     nregs = 1;
4261   else
4262     nregs = ARM_NUM_REGS2 (mode, type);
4263
4264   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
4265     return NULL_RTX;
4266
4267   return gen_rtx_REG (mode, pcum->nregs);
4268 }
4269
4270 static int
4271 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4272                        tree type, bool named)
4273 {
4274   int nregs = pcum->nregs;
4275
4276   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4277     {
4278       aapcs_layout_arg (pcum, mode, type, named);
4279       return pcum->aapcs_partial;
4280     }
4281
4282   if (TARGET_IWMMXT_ABI && arm_vector_mode_supported_p (mode))
4283     return 0;
4284
4285   if (NUM_ARG_REGS > nregs
4286       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
4287       && pcum->can_split)
4288     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
4289
4290   return 0;
4291 }
4292
4293 void
4294 arm_function_arg_advance (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4295                           tree type, bool named)
4296 {
4297   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4298     {
4299       aapcs_layout_arg (pcum, mode, type, named);
4300
4301       if (pcum->aapcs_cprc_slot >= 0)
4302         {
4303           aapcs_cp_arg_layout[pcum->aapcs_cprc_slot].advance (pcum, mode,
4304                                                               type);
4305           pcum->aapcs_cprc_slot = -1;
4306         }
4307
4308       /* Generic stuff.  */
4309       pcum->aapcs_arg_processed = false;
4310       pcum->aapcs_ncrn = pcum->aapcs_next_ncrn;
4311       pcum->aapcs_reg = NULL_RTX;
4312       pcum->aapcs_partial = 0;
4313     }
4314   else
4315     {
4316       pcum->nargs += 1;
4317       if (arm_vector_mode_supported_p (mode)
4318           && pcum->named_count > pcum->nargs
4319           && TARGET_IWMMXT_ABI)
4320         pcum->iwmmxt_nregs += 1;
4321       else
4322         pcum->nregs += ARM_NUM_REGS2 (mode, type);
4323     }
4324 }
4325
4326 /* Variable sized types are passed by reference.  This is a GCC
4327    extension to the ARM ABI.  */
4328
4329 static bool
4330 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
4331                        enum machine_mode mode ATTRIBUTE_UNUSED,
4332                        const_tree type, bool named ATTRIBUTE_UNUSED)
4333 {
4334   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
4335 }
4336 \f
4337 /* Encode the current state of the #pragma [no_]long_calls.  */
4338 typedef enum
4339 {
4340   OFF,          /* No #pragma [no_]long_calls is in effect.  */
4341   LONG,         /* #pragma long_calls is in effect.  */
4342   SHORT         /* #pragma no_long_calls is in effect.  */
4343 } arm_pragma_enum;
4344
4345 static arm_pragma_enum arm_pragma_long_calls = OFF;
4346
4347 void
4348 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
4349 {
4350   arm_pragma_long_calls = LONG;
4351 }
4352
4353 void
4354 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
4355 {
4356   arm_pragma_long_calls = SHORT;
4357 }
4358
4359 void
4360 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
4361 {
4362   arm_pragma_long_calls = OFF;
4363 }
4364 \f
4365 /* Handle an attribute requiring a FUNCTION_DECL;
4366    arguments as in struct attribute_spec.handler.  */
4367 static tree
4368 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
4369                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4370 {
4371   if (TREE_CODE (*node) != FUNCTION_DECL)
4372     {
4373       warning (OPT_Wattributes, "%qE attribute only applies to functions",
4374                name);
4375       *no_add_attrs = true;
4376     }
4377
4378   return NULL_TREE;
4379 }
4380
4381 /* Handle an "interrupt" or "isr" attribute;
4382    arguments as in struct attribute_spec.handler.  */
4383 static tree
4384 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
4385                           bool *no_add_attrs)
4386 {
4387   if (DECL_P (*node))
4388     {
4389       if (TREE_CODE (*node) != FUNCTION_DECL)
4390         {
4391           warning (OPT_Wattributes, "%qE attribute only applies to functions",
4392                    name);
4393           *no_add_attrs = true;
4394         }
4395       /* FIXME: the argument if any is checked for type attributes;
4396          should it be checked for decl ones?  */
4397     }
4398   else
4399     {
4400       if (TREE_CODE (*node) == FUNCTION_TYPE
4401           || TREE_CODE (*node) == METHOD_TYPE)
4402         {
4403           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
4404             {
4405               warning (OPT_Wattributes, "%qE attribute ignored",
4406                        name);
4407               *no_add_attrs = true;
4408             }
4409         }
4410       else if (TREE_CODE (*node) == POINTER_TYPE
4411                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
4412                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
4413                && arm_isr_value (args) != ARM_FT_UNKNOWN)
4414         {
4415           *node = build_variant_type_copy (*node);
4416           TREE_TYPE (*node) = build_type_attribute_variant
4417             (TREE_TYPE (*node),
4418              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
4419           *no_add_attrs = true;
4420         }
4421       else
4422         {
4423           /* Possibly pass this attribute on from the type to a decl.  */
4424           if (flags & ((int) ATTR_FLAG_DECL_NEXT
4425                        | (int) ATTR_FLAG_FUNCTION_NEXT
4426                        | (int) ATTR_FLAG_ARRAY_NEXT))
4427             {
4428               *no_add_attrs = true;
4429               return tree_cons (name, args, NULL_TREE);
4430             }
4431           else
4432             {
4433               warning (OPT_Wattributes, "%qE attribute ignored",
4434                        name);
4435             }
4436         }
4437     }
4438
4439   return NULL_TREE;
4440 }
4441
4442 /* Handle a "pcs" attribute; arguments as in struct
4443    attribute_spec.handler.  */
4444 static tree
4445 arm_handle_pcs_attribute (tree *node ATTRIBUTE_UNUSED, tree name, tree args,
4446                           int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4447 {
4448   if (arm_pcs_from_attribute (args) == ARM_PCS_UNKNOWN)
4449     {
4450       warning (OPT_Wattributes, "%qE attribute ignored", name);
4451       *no_add_attrs = true;
4452     }
4453   return NULL_TREE;
4454 }
4455
4456 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
4457 /* Handle the "notshared" attribute.  This attribute is another way of
4458    requesting hidden visibility.  ARM's compiler supports
4459    "__declspec(notshared)"; we support the same thing via an
4460    attribute.  */
4461
4462 static tree
4463 arm_handle_notshared_attribute (tree *node,
4464                                 tree name ATTRIBUTE_UNUSED,
4465                                 tree args ATTRIBUTE_UNUSED,
4466                                 int flags ATTRIBUTE_UNUSED,
4467                                 bool *no_add_attrs)
4468 {
4469   tree decl = TYPE_NAME (*node);
4470
4471   if (decl)
4472     {
4473       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
4474       DECL_VISIBILITY_SPECIFIED (decl) = 1;
4475       *no_add_attrs = false;
4476     }
4477   return NULL_TREE;
4478 }
4479 #endif
4480
4481 /* Return 0 if the attributes for two types are incompatible, 1 if they
4482    are compatible, and 2 if they are nearly compatible (which causes a
4483    warning to be generated).  */
4484 static int
4485 arm_comp_type_attributes (const_tree type1, const_tree type2)
4486 {
4487   int l1, l2, s1, s2;
4488
4489   /* Check for mismatch of non-default calling convention.  */
4490   if (TREE_CODE (type1) != FUNCTION_TYPE)
4491     return 1;
4492
4493   /* Check for mismatched call attributes.  */
4494   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
4495   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
4496   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
4497   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
4498
4499   /* Only bother to check if an attribute is defined.  */
4500   if (l1 | l2 | s1 | s2)
4501     {
4502       /* If one type has an attribute, the other must have the same attribute.  */
4503       if ((l1 != l2) || (s1 != s2))
4504         return 0;
4505
4506       /* Disallow mixed attributes.  */
4507       if ((l1 & s2) || (l2 & s1))
4508         return 0;
4509     }
4510
4511   /* Check for mismatched ISR attribute.  */
4512   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
4513   if (! l1)
4514     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
4515   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
4516   if (! l2)
4517     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
4518   if (l1 != l2)
4519     return 0;
4520
4521   return 1;
4522 }
4523
4524 /*  Assigns default attributes to newly defined type.  This is used to
4525     set short_call/long_call attributes for function types of
4526     functions defined inside corresponding #pragma scopes.  */
4527 static void
4528 arm_set_default_type_attributes (tree type)
4529 {
4530   /* Add __attribute__ ((long_call)) to all functions, when
4531      inside #pragma long_calls or __attribute__ ((short_call)),
4532      when inside #pragma no_long_calls.  */
4533   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
4534     {
4535       tree type_attr_list, attr_name;
4536       type_attr_list = TYPE_ATTRIBUTES (type);
4537
4538       if (arm_pragma_long_calls == LONG)
4539         attr_name = get_identifier ("long_call");
4540       else if (arm_pragma_long_calls == SHORT)
4541         attr_name = get_identifier ("short_call");
4542       else
4543         return;
4544
4545       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
4546       TYPE_ATTRIBUTES (type) = type_attr_list;
4547     }
4548 }
4549 \f
4550 /* Return true if DECL is known to be linked into section SECTION.  */
4551
4552 static bool
4553 arm_function_in_section_p (tree decl, section *section)
4554 {
4555   /* We can only be certain about functions defined in the same
4556      compilation unit.  */
4557   if (!TREE_STATIC (decl))
4558     return false;
4559
4560   /* Make sure that SYMBOL always binds to the definition in this
4561      compilation unit.  */
4562   if (!targetm.binds_local_p (decl))
4563     return false;
4564
4565   /* If DECL_SECTION_NAME is set, assume it is trustworthy.  */
4566   if (!DECL_SECTION_NAME (decl))
4567     {
4568       /* Make sure that we will not create a unique section for DECL.  */
4569       if (flag_function_sections || DECL_ONE_ONLY (decl))
4570         return false;
4571     }
4572
4573   return function_section (decl) == section;
4574 }
4575
4576 /* Return nonzero if a 32-bit "long_call" should be generated for
4577    a call from the current function to DECL.  We generate a long_call
4578    if the function:
4579
4580         a.  has an __attribute__((long call))
4581      or b.  is within the scope of a #pragma long_calls
4582      or c.  the -mlong-calls command line switch has been specified
4583
4584    However we do not generate a long call if the function:
4585
4586         d.  has an __attribute__ ((short_call))
4587      or e.  is inside the scope of a #pragma no_long_calls
4588      or f.  is defined in the same section as the current function.  */
4589
4590 bool
4591 arm_is_long_call_p (tree decl)
4592 {
4593   tree attrs;
4594
4595   if (!decl)
4596     return TARGET_LONG_CALLS;
4597
4598   attrs = TYPE_ATTRIBUTES (TREE_TYPE (decl));
4599   if (lookup_attribute ("short_call", attrs))
4600     return false;
4601
4602   /* For "f", be conservative, and only cater for cases in which the
4603      whole of the current function is placed in the same section.  */
4604   if (!flag_reorder_blocks_and_partition
4605       && TREE_CODE (decl) == FUNCTION_DECL
4606       && arm_function_in_section_p (decl, current_function_section ()))
4607     return false;
4608
4609   if (lookup_attribute ("long_call", attrs))
4610     return true;
4611
4612   return TARGET_LONG_CALLS;
4613 }
4614
4615 /* Return nonzero if it is ok to make a tail-call to DECL.  */
4616 static bool
4617 arm_function_ok_for_sibcall (tree decl, tree exp)
4618 {
4619   unsigned long func_type;
4620
4621   if (cfun->machine->sibcall_blocked)
4622     return false;
4623
4624   /* Never tailcall something for which we have no decl, or if we
4625      are in Thumb mode.  */
4626   if (decl == NULL || TARGET_THUMB)
4627     return false;
4628
4629   /* The PIC register is live on entry to VxWorks PLT entries, so we
4630      must make the call before restoring the PIC register.  */
4631   if (TARGET_VXWORKS_RTP && flag_pic && !targetm.binds_local_p (decl))
4632     return false;
4633
4634   /* Cannot tail-call to long calls, since these are out of range of
4635      a branch instruction.  */
4636   if (arm_is_long_call_p (decl))
4637     return false;
4638
4639   /* If we are interworking and the function is not declared static
4640      then we can't tail-call it unless we know that it exists in this
4641      compilation unit (since it might be a Thumb routine).  */
4642   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
4643     return false;
4644
4645   func_type = arm_current_func_type ();
4646   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
4647   if (IS_INTERRUPT (func_type))
4648     return false;
4649
4650   if (!VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
4651     {
4652       /* Check that the return value locations are the same.  For
4653          example that we aren't returning a value from the sibling in
4654          a VFP register but then need to transfer it to a core
4655          register.  */
4656       rtx a, b;
4657
4658       a = arm_function_value (TREE_TYPE (exp), decl, false);
4659       b = arm_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
4660                               cfun->decl, false);
4661       if (!rtx_equal_p (a, b))
4662         return false;
4663     }
4664
4665   /* Never tailcall if function may be called with a misaligned SP.  */
4666   if (IS_STACKALIGN (func_type))
4667     return false;
4668
4669   /* Everything else is ok.  */
4670   return true;
4671 }
4672
4673 \f
4674 /* Addressing mode support functions.  */
4675
4676 /* Return nonzero if X is a legitimate immediate operand when compiling
4677    for PIC.  We know that X satisfies CONSTANT_P and flag_pic is true.  */
4678 int
4679 legitimate_pic_operand_p (rtx x)
4680 {
4681   if (GET_CODE (x) == SYMBOL_REF
4682       || (GET_CODE (x) == CONST
4683           && GET_CODE (XEXP (x, 0)) == PLUS
4684           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
4685     return 0;
4686
4687   return 1;
4688 }
4689
4690 /* Record that the current function needs a PIC register.  Initialize
4691    cfun->machine->pic_reg if we have not already done so.  */
4692
4693 static void
4694 require_pic_register (void)
4695 {
4696   /* A lot of the logic here is made obscure by the fact that this
4697      routine gets called as part of the rtx cost estimation process.
4698      We don't want those calls to affect any assumptions about the real
4699      function; and further, we can't call entry_of_function() until we
4700      start the real expansion process.  */
4701   if (!crtl->uses_pic_offset_table)
4702     {
4703       gcc_assert (can_create_pseudo_p ());
4704       if (arm_pic_register != INVALID_REGNUM)
4705         {
4706           if (!cfun->machine->pic_reg)
4707             cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
4708
4709           /* Play games to avoid marking the function as needing pic
4710              if we are being called as part of the cost-estimation
4711              process.  */
4712           if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
4713             crtl->uses_pic_offset_table = 1;
4714         }
4715       else
4716         {
4717           rtx seq;
4718
4719           if (!cfun->machine->pic_reg)
4720             cfun->machine->pic_reg = gen_reg_rtx (Pmode);
4721
4722           /* Play games to avoid marking the function as needing pic
4723              if we are being called as part of the cost-estimation
4724              process.  */
4725           if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
4726             {
4727               crtl->uses_pic_offset_table = 1;
4728               start_sequence ();
4729
4730               arm_load_pic_register (0UL);
4731
4732               seq = get_insns ();
4733               end_sequence ();
4734               /* We can be called during expansion of PHI nodes, where
4735                  we can't yet emit instructions directly in the final
4736                  insn stream.  Queue the insns on the entry edge, they will
4737                  be committed after everything else is expanded.  */
4738               insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
4739             }
4740         }
4741     }
4742 }
4743
4744 rtx
4745 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
4746 {
4747   if (GET_CODE (orig) == SYMBOL_REF
4748       || GET_CODE (orig) == LABEL_REF)
4749     {
4750       rtx pic_ref, address;
4751       rtx insn;
4752       int subregs = 0;
4753
4754       /* If this function doesn't have a pic register, create one now.  */
4755       require_pic_register ();
4756
4757       if (reg == 0)
4758         {
4759           gcc_assert (can_create_pseudo_p ());
4760           reg = gen_reg_rtx (Pmode);
4761
4762           subregs = 1;
4763         }
4764
4765       if (subregs)
4766         address = gen_reg_rtx (Pmode);
4767       else
4768         address = reg;
4769
4770       if (TARGET_ARM)
4771         emit_insn (gen_pic_load_addr_arm (address, orig));
4772       else if (TARGET_THUMB2)
4773         emit_insn (gen_pic_load_addr_thumb2 (address, orig));
4774       else /* TARGET_THUMB1 */
4775         emit_insn (gen_pic_load_addr_thumb1 (address, orig));
4776
4777       /* VxWorks does not impose a fixed gap between segments; the run-time
4778          gap can be different from the object-file gap.  We therefore can't
4779          use GOTOFF unless we are absolutely sure that the symbol is in the
4780          same segment as the GOT.  Unfortunately, the flexibility of linker
4781          scripts means that we can't be sure of that in general, so assume
4782          that GOTOFF is never valid on VxWorks.  */
4783       if ((GET_CODE (orig) == LABEL_REF
4784            || (GET_CODE (orig) == SYMBOL_REF &&
4785                SYMBOL_REF_LOCAL_P (orig)))
4786           && NEED_GOT_RELOC
4787           && !TARGET_VXWORKS_RTP)
4788         pic_ref = gen_rtx_PLUS (Pmode, cfun->machine->pic_reg, address);
4789       else
4790         {
4791           pic_ref = gen_const_mem (Pmode,
4792                                    gen_rtx_PLUS (Pmode, cfun->machine->pic_reg,
4793                                                  address));
4794         }
4795
4796       insn = emit_move_insn (reg, pic_ref);
4797
4798       /* Put a REG_EQUAL note on this insn, so that it can be optimized
4799          by loop.  */
4800       set_unique_reg_note (insn, REG_EQUAL, orig);
4801
4802       return reg;
4803     }
4804   else if (GET_CODE (orig) == CONST)
4805     {
4806       rtx base, offset;
4807
4808       if (GET_CODE (XEXP (orig, 0)) == PLUS
4809           && XEXP (XEXP (orig, 0), 0) == cfun->machine->pic_reg)
4810         return orig;
4811
4812       /* Handle the case where we have: const (UNSPEC_TLS).  */
4813       if (GET_CODE (XEXP (orig, 0)) == UNSPEC
4814           && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
4815         return orig;
4816
4817       /* Handle the case where we have:
4818          const (plus (UNSPEC_TLS) (ADDEND)).  The ADDEND must be a
4819          CONST_INT.  */
4820       if (GET_CODE (XEXP (orig, 0)) == PLUS
4821           && GET_CODE (XEXP (XEXP (orig, 0), 0)) == UNSPEC
4822           && XINT (XEXP (XEXP (orig, 0), 0), 1) == UNSPEC_TLS)
4823         {
4824           gcc_assert (GET_CODE (XEXP (XEXP (orig, 0), 1)) == CONST_INT);
4825           return orig;
4826         }
4827
4828       if (reg == 0)
4829         {
4830           gcc_assert (can_create_pseudo_p ());
4831           reg = gen_reg_rtx (Pmode);
4832         }
4833
4834       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
4835
4836       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
4837       offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
4838                                        base == reg ? 0 : reg);
4839
4840       if (GET_CODE (offset) == CONST_INT)
4841         {
4842           /* The base register doesn't really matter, we only want to
4843              test the index for the appropriate mode.  */
4844           if (!arm_legitimate_index_p (mode, offset, SET, 0))
4845             {
4846               gcc_assert (can_create_pseudo_p ());
4847               offset = force_reg (Pmode, offset);
4848             }
4849
4850           if (GET_CODE (offset) == CONST_INT)
4851             return plus_constant (base, INTVAL (offset));
4852         }
4853
4854       if (GET_MODE_SIZE (mode) > 4
4855           && (GET_MODE_CLASS (mode) == MODE_INT
4856               || TARGET_SOFT_FLOAT))
4857         {
4858           emit_insn (gen_addsi3 (reg, base, offset));
4859           return reg;
4860         }
4861
4862       return gen_rtx_PLUS (Pmode, base, offset);
4863     }
4864
4865   return orig;
4866 }
4867
4868
4869 /* Find a spare register to use during the prolog of a function.  */
4870
4871 static int
4872 thumb_find_work_register (unsigned long pushed_regs_mask)
4873 {
4874   int reg;
4875
4876   /* Check the argument registers first as these are call-used.  The
4877      register allocation order means that sometimes r3 might be used
4878      but earlier argument registers might not, so check them all.  */
4879   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
4880     if (!df_regs_ever_live_p (reg))
4881       return reg;
4882
4883   /* Before going on to check the call-saved registers we can try a couple
4884      more ways of deducing that r3 is available.  The first is when we are
4885      pushing anonymous arguments onto the stack and we have less than 4
4886      registers worth of fixed arguments(*).  In this case r3 will be part of
4887      the variable argument list and so we can be sure that it will be
4888      pushed right at the start of the function.  Hence it will be available
4889      for the rest of the prologue.
4890      (*): ie crtl->args.pretend_args_size is greater than 0.  */
4891   if (cfun->machine->uses_anonymous_args
4892       && crtl->args.pretend_args_size > 0)
4893     return LAST_ARG_REGNUM;
4894
4895   /* The other case is when we have fixed arguments but less than 4 registers
4896      worth.  In this case r3 might be used in the body of the function, but
4897      it is not being used to convey an argument into the function.  In theory
4898      we could just check crtl->args.size to see how many bytes are
4899      being passed in argument registers, but it seems that it is unreliable.
4900      Sometimes it will have the value 0 when in fact arguments are being
4901      passed.  (See testcase execute/20021111-1.c for an example).  So we also
4902      check the args_info.nregs field as well.  The problem with this field is
4903      that it makes no allowances for arguments that are passed to the
4904      function but which are not used.  Hence we could miss an opportunity
4905      when a function has an unused argument in r3.  But it is better to be
4906      safe than to be sorry.  */
4907   if (! cfun->machine->uses_anonymous_args
4908       && crtl->args.size >= 0
4909       && crtl->args.size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
4910       && crtl->args.info.nregs < 4)
4911     return LAST_ARG_REGNUM;
4912
4913   /* Otherwise look for a call-saved register that is going to be pushed.  */
4914   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
4915     if (pushed_regs_mask & (1 << reg))
4916       return reg;
4917
4918   if (TARGET_THUMB2)
4919     {
4920       /* Thumb-2 can use high regs.  */
4921       for (reg = FIRST_HI_REGNUM; reg < 15; reg ++)
4922         if (pushed_regs_mask & (1 << reg))
4923           return reg;
4924     }
4925   /* Something went wrong - thumb_compute_save_reg_mask()
4926      should have arranged for a suitable register to be pushed.  */
4927   gcc_unreachable ();
4928 }
4929
4930 static GTY(()) int pic_labelno;
4931
4932 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
4933    low register.  */
4934
4935 void
4936 arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
4937 {
4938   rtx l1, labelno, pic_tmp, pic_rtx, pic_reg;
4939
4940   if (crtl->uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
4941     return;
4942
4943   gcc_assert (flag_pic);
4944
4945   pic_reg = cfun->machine->pic_reg;
4946   if (TARGET_VXWORKS_RTP)
4947     {
4948       pic_rtx = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE);
4949       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
4950       emit_insn (gen_pic_load_addr_arm (pic_reg, pic_rtx));
4951
4952       emit_insn (gen_rtx_SET (Pmode, pic_reg, gen_rtx_MEM (Pmode, pic_reg)));
4953
4954       pic_tmp = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
4955       emit_insn (gen_pic_offset_arm (pic_reg, pic_reg, pic_tmp));
4956     }
4957   else
4958     {
4959       /* We use an UNSPEC rather than a LABEL_REF because this label
4960          never appears in the code stream.  */
4961
4962       labelno = GEN_INT (pic_labelno++);
4963       l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
4964       l1 = gen_rtx_CONST (VOIDmode, l1);
4965
4966       /* On the ARM the PC register contains 'dot + 8' at the time of the
4967          addition, on the Thumb it is 'dot + 4'.  */
4968       pic_rtx = plus_constant (l1, TARGET_ARM ? 8 : 4);
4969       pic_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, pic_rtx),
4970                                 UNSPEC_GOTSYM_OFF);
4971       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
4972
4973       if (TARGET_ARM)
4974         {
4975           emit_insn (gen_pic_load_addr_arm (pic_reg, pic_rtx));
4976           emit_insn (gen_pic_add_dot_plus_eight (pic_reg, pic_reg, labelno));
4977         }
4978       else if (TARGET_THUMB2)
4979         {
4980           /* Thumb-2 only allows very limited access to the PC.  Calculate the
4981              address in a temporary register.  */
4982           if (arm_pic_register != INVALID_REGNUM)
4983             {
4984               pic_tmp = gen_rtx_REG (SImode,
4985                                      thumb_find_work_register (saved_regs));
4986             }
4987           else
4988             {
4989               gcc_assert (can_create_pseudo_p ());
4990               pic_tmp = gen_reg_rtx (Pmode);
4991             }
4992
4993           emit_insn (gen_pic_load_addr_thumb2 (pic_reg, pic_rtx));
4994           emit_insn (gen_pic_load_dot_plus_four (pic_tmp, labelno));
4995           emit_insn (gen_addsi3 (pic_reg, pic_reg, pic_tmp));
4996         }
4997       else /* TARGET_THUMB1 */
4998         {
4999           if (arm_pic_register != INVALID_REGNUM
5000               && REGNO (pic_reg) > LAST_LO_REGNUM)
5001             {
5002               /* We will have pushed the pic register, so we should always be
5003                  able to find a work register.  */
5004               pic_tmp = gen_rtx_REG (SImode,
5005                                      thumb_find_work_register (saved_regs));
5006               emit_insn (gen_pic_load_addr_thumb1 (pic_tmp, pic_rtx));
5007               emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
5008             }
5009           else
5010             emit_insn (gen_pic_load_addr_thumb1 (pic_reg, pic_rtx));
5011           emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
5012         }
5013     }
5014
5015   /* Need to emit this whether or not we obey regdecls,
5016      since setjmp/longjmp can cause life info to screw up.  */
5017   emit_use (pic_reg);
5018 }
5019
5020
5021 /* Return nonzero if X is valid as an ARM state addressing register.  */
5022 static int
5023 arm_address_register_rtx_p (rtx x, int strict_p)
5024 {
5025   int regno;
5026
5027   if (GET_CODE (x) != REG)
5028     return 0;
5029
5030   regno = REGNO (x);
5031
5032   if (strict_p)
5033     return ARM_REGNO_OK_FOR_BASE_P (regno);
5034
5035   return (regno <= LAST_ARM_REGNUM
5036           || regno >= FIRST_PSEUDO_REGISTER
5037           || regno == FRAME_POINTER_REGNUM
5038           || regno == ARG_POINTER_REGNUM);
5039 }
5040
5041 /* Return TRUE if this rtx is the difference of a symbol and a label,
5042    and will reduce to a PC-relative relocation in the object file.
5043    Expressions like this can be left alone when generating PIC, rather
5044    than forced through the GOT.  */
5045 static int
5046 pcrel_constant_p (rtx x)
5047 {
5048   if (GET_CODE (x) == MINUS)
5049     return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
5050
5051   return FALSE;
5052 }
5053
5054 /* Return nonzero if X is a valid ARM state address operand.  */
5055 int
5056 arm_legitimate_address_outer_p (enum machine_mode mode, rtx x, RTX_CODE outer,
5057                                 int strict_p)
5058 {
5059   bool use_ldrd;
5060   enum rtx_code code = GET_CODE (x);
5061
5062   if (arm_address_register_rtx_p (x, strict_p))
5063     return 1;
5064
5065   use_ldrd = (TARGET_LDRD
5066               && (mode == DImode
5067                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
5068
5069   if (code == POST_INC || code == PRE_DEC
5070       || ((code == PRE_INC || code == POST_DEC)
5071           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
5072     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
5073
5074   else if ((code == POST_MODIFY || code == PRE_MODIFY)
5075            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
5076            && GET_CODE (XEXP (x, 1)) == PLUS
5077            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5078     {
5079       rtx addend = XEXP (XEXP (x, 1), 1);
5080
5081       /* Don't allow ldrd post increment by register because it's hard
5082          to fixup invalid register choices.  */
5083       if (use_ldrd
5084           && GET_CODE (x) == POST_MODIFY
5085           && GET_CODE (addend) == REG)
5086         return 0;
5087
5088       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
5089               && arm_legitimate_index_p (mode, addend, outer, strict_p));
5090     }
5091
5092   /* After reload constants split into minipools will have addresses
5093      from a LABEL_REF.  */
5094   else if (reload_completed
5095            && (code == LABEL_REF
5096                || (code == CONST
5097                    && GET_CODE (XEXP (x, 0)) == PLUS
5098                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5099                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5100     return 1;
5101
5102   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
5103     return 0;
5104
5105   else if (code == PLUS)
5106     {
5107       rtx xop0 = XEXP (x, 0);
5108       rtx xop1 = XEXP (x, 1);
5109
5110       return ((arm_address_register_rtx_p (xop0, strict_p)
5111                && GET_CODE(xop1) == CONST_INT
5112                && arm_legitimate_index_p (mode, xop1, outer, strict_p))
5113               || (arm_address_register_rtx_p (xop1, strict_p)
5114                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
5115     }
5116
5117 #if 0
5118   /* Reload currently can't handle MINUS, so disable this for now */
5119   else if (GET_CODE (x) == MINUS)
5120     {
5121       rtx xop0 = XEXP (x, 0);
5122       rtx xop1 = XEXP (x, 1);
5123
5124       return (arm_address_register_rtx_p (xop0, strict_p)
5125               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
5126     }
5127 #endif
5128
5129   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
5130            && code == SYMBOL_REF
5131            && CONSTANT_POOL_ADDRESS_P (x)
5132            && ! (flag_pic
5133                  && symbol_mentioned_p (get_pool_constant (x))
5134                  && ! pcrel_constant_p (get_pool_constant (x))))
5135     return 1;
5136
5137   return 0;
5138 }
5139
5140 /* Return nonzero if X is a valid Thumb-2 address operand.  */
5141 static int
5142 thumb2_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
5143 {
5144   bool use_ldrd;
5145   enum rtx_code code = GET_CODE (x);
5146   
5147   if (arm_address_register_rtx_p (x, strict_p))
5148     return 1;
5149
5150   use_ldrd = (TARGET_LDRD
5151               && (mode == DImode
5152                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
5153
5154   if (code == POST_INC || code == PRE_DEC
5155       || ((code == PRE_INC || code == POST_DEC)
5156           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
5157     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
5158
5159   else if ((code == POST_MODIFY || code == PRE_MODIFY)
5160            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
5161            && GET_CODE (XEXP (x, 1)) == PLUS
5162            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5163     {
5164       /* Thumb-2 only has autoincrement by constant.  */
5165       rtx addend = XEXP (XEXP (x, 1), 1);
5166       HOST_WIDE_INT offset;
5167
5168       if (GET_CODE (addend) != CONST_INT)
5169         return 0;
5170
5171       offset = INTVAL(addend);
5172       if (GET_MODE_SIZE (mode) <= 4)
5173         return (offset > -256 && offset < 256);
5174       
5175       return (use_ldrd && offset > -1024 && offset < 1024
5176               && (offset & 3) == 0);
5177     }
5178
5179   /* After reload constants split into minipools will have addresses
5180      from a LABEL_REF.  */
5181   else if (reload_completed
5182            && (code == LABEL_REF
5183                || (code == CONST
5184                    && GET_CODE (XEXP (x, 0)) == PLUS
5185                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5186                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5187     return 1;
5188
5189   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
5190     return 0;
5191
5192   else if (code == PLUS)
5193     {
5194       rtx xop0 = XEXP (x, 0);
5195       rtx xop1 = XEXP (x, 1);
5196
5197       return ((arm_address_register_rtx_p (xop0, strict_p)
5198                && thumb2_legitimate_index_p (mode, xop1, strict_p))
5199               || (arm_address_register_rtx_p (xop1, strict_p)
5200                   && thumb2_legitimate_index_p (mode, xop0, strict_p)));
5201     }
5202
5203   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
5204            && code == SYMBOL_REF
5205            && CONSTANT_POOL_ADDRESS_P (x)
5206            && ! (flag_pic
5207                  && symbol_mentioned_p (get_pool_constant (x))
5208                  && ! pcrel_constant_p (get_pool_constant (x))))
5209     return 1;
5210
5211   return 0;
5212 }
5213
5214 /* Return nonzero if INDEX is valid for an address index operand in
5215    ARM state.  */
5216 static int
5217 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
5218                         int strict_p)
5219 {
5220   HOST_WIDE_INT range;
5221   enum rtx_code code = GET_CODE (index);
5222
5223   /* Standard coprocessor addressing modes.  */
5224   if (TARGET_HARD_FLOAT
5225       && (TARGET_FPA || TARGET_MAVERICK)
5226       && (GET_MODE_CLASS (mode) == MODE_FLOAT
5227           || (TARGET_MAVERICK && mode == DImode)))
5228     return (code == CONST_INT && INTVAL (index) < 1024
5229             && INTVAL (index) > -1024
5230             && (INTVAL (index) & 3) == 0);
5231
5232   if (TARGET_NEON
5233       && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode)))
5234     return (code == CONST_INT
5235             && INTVAL (index) < 1016
5236             && INTVAL (index) > -1024
5237             && (INTVAL (index) & 3) == 0);
5238
5239   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
5240     return (code == CONST_INT
5241             && INTVAL (index) < 1024
5242             && INTVAL (index) > -1024
5243             && (INTVAL (index) & 3) == 0);
5244
5245   if (arm_address_register_rtx_p (index, strict_p)
5246       && (GET_MODE_SIZE (mode) <= 4))
5247     return 1;
5248
5249   if (mode == DImode || mode == DFmode)
5250     {
5251       if (code == CONST_INT)
5252         {
5253           HOST_WIDE_INT val = INTVAL (index);
5254
5255           if (TARGET_LDRD)
5256             return val > -256 && val < 256;
5257           else
5258             return val > -4096 && val < 4092;
5259         }
5260
5261       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
5262     }
5263
5264   if (GET_MODE_SIZE (mode) <= 4
5265       && ! (arm_arch4
5266             && (mode == HImode
5267                 || mode == HFmode
5268                 || (mode == QImode && outer == SIGN_EXTEND))))
5269     {
5270       if (code == MULT)
5271         {
5272           rtx xiop0 = XEXP (index, 0);
5273           rtx xiop1 = XEXP (index, 1);
5274
5275           return ((arm_address_register_rtx_p (xiop0, strict_p)
5276                    && power_of_two_operand (xiop1, SImode))
5277                   || (arm_address_register_rtx_p (xiop1, strict_p)
5278                       && power_of_two_operand (xiop0, SImode)));
5279         }
5280       else if (code == LSHIFTRT || code == ASHIFTRT
5281                || code == ASHIFT || code == ROTATERT)
5282         {
5283           rtx op = XEXP (index, 1);
5284
5285           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
5286                   && GET_CODE (op) == CONST_INT
5287                   && INTVAL (op) > 0
5288                   && INTVAL (op) <= 31);
5289         }
5290     }
5291
5292   /* For ARM v4 we may be doing a sign-extend operation during the
5293      load.  */
5294   if (arm_arch4)
5295     {
5296       if (mode == HImode
5297           || mode == HFmode
5298           || (outer == SIGN_EXTEND && mode == QImode))
5299         range = 256;
5300       else
5301         range = 4096;
5302     }
5303   else
5304     range = (mode == HImode || mode == HFmode) ? 4095 : 4096;
5305
5306   return (code == CONST_INT
5307           && INTVAL (index) < range
5308           && INTVAL (index) > -range);
5309 }
5310
5311 /* Return true if OP is a valid index scaling factor for Thumb-2 address
5312    index operand.  i.e. 1, 2, 4 or 8.  */
5313 static bool
5314 thumb2_index_mul_operand (rtx op)
5315 {
5316   HOST_WIDE_INT val;
5317   
5318   if (GET_CODE(op) != CONST_INT)
5319     return false;
5320
5321   val = INTVAL(op);
5322   return (val == 1 || val == 2 || val == 4 || val == 8);
5323 }
5324   
5325 /* Return nonzero if INDEX is a valid Thumb-2 address index operand.  */
5326 static int
5327 thumb2_legitimate_index_p (enum machine_mode mode, rtx index, int strict_p)
5328 {
5329   enum rtx_code code = GET_CODE (index);
5330
5331   /* ??? Combine arm and thumb2 coprocessor addressing modes.  */
5332   /* Standard coprocessor addressing modes.  */
5333   if (TARGET_HARD_FLOAT
5334       && (TARGET_FPA || TARGET_MAVERICK)
5335       && (GET_MODE_CLASS (mode) == MODE_FLOAT
5336           || (TARGET_MAVERICK && mode == DImode)))
5337     return (code == CONST_INT && INTVAL (index) < 1024
5338             && INTVAL (index) > -1024
5339             && (INTVAL (index) & 3) == 0);
5340
5341   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
5342     {
5343       /* For DImode assume values will usually live in core regs
5344          and only allow LDRD addressing modes.  */
5345       if (!TARGET_LDRD || mode != DImode)
5346         return (code == CONST_INT
5347                 && INTVAL (index) < 1024
5348                 && INTVAL (index) > -1024
5349                 && (INTVAL (index) & 3) == 0);
5350     }
5351
5352   if (TARGET_NEON
5353       && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode)))
5354     return (code == CONST_INT
5355             && INTVAL (index) < 1016
5356             && INTVAL (index) > -1024
5357             && (INTVAL (index) & 3) == 0);
5358
5359   if (arm_address_register_rtx_p (index, strict_p)
5360       && (GET_MODE_SIZE (mode) <= 4))
5361     return 1;
5362
5363   if (mode == DImode || mode == DFmode)
5364     {
5365       if (code == CONST_INT)
5366         {
5367           HOST_WIDE_INT val = INTVAL (index);
5368           /* ??? Can we assume ldrd for thumb2?  */
5369           /* Thumb-2 ldrd only has reg+const addressing modes.  */
5370           /* ldrd supports offsets of +-1020.
5371              However the ldr fallback does not.  */
5372           return val > -256 && val < 256 && (val & 3) == 0;
5373         }
5374       else
5375         return 0;
5376     }
5377
5378   if (code == MULT)
5379     {
5380       rtx xiop0 = XEXP (index, 0);
5381       rtx xiop1 = XEXP (index, 1);
5382
5383       return ((arm_address_register_rtx_p (xiop0, strict_p)
5384                && thumb2_index_mul_operand (xiop1))
5385               || (arm_address_register_rtx_p (xiop1, strict_p)
5386                   && thumb2_index_mul_operand (xiop0)));
5387     }
5388   else if (code == ASHIFT)
5389     {
5390       rtx op = XEXP (index, 1);
5391
5392       return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
5393               && GET_CODE (op) == CONST_INT
5394               && INTVAL (op) > 0
5395               && INTVAL (op) <= 3);
5396     }
5397
5398   return (code == CONST_INT
5399           && INTVAL (index) < 4096
5400           && INTVAL (index) > -256);
5401 }
5402
5403 /* Return nonzero if X is valid as a 16-bit Thumb state base register.  */
5404 static int
5405 thumb1_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
5406 {
5407   int regno;
5408
5409   if (GET_CODE (x) != REG)
5410     return 0;
5411
5412   regno = REGNO (x);
5413
5414   if (strict_p)
5415     return THUMB1_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
5416
5417   return (regno <= LAST_LO_REGNUM
5418           || regno > LAST_VIRTUAL_REGISTER
5419           || regno == FRAME_POINTER_REGNUM
5420           || (GET_MODE_SIZE (mode) >= 4
5421               && (regno == STACK_POINTER_REGNUM
5422                   || regno >= FIRST_PSEUDO_REGISTER
5423                   || x == hard_frame_pointer_rtx
5424                   || x == arg_pointer_rtx)));
5425 }
5426
5427 /* Return nonzero if x is a legitimate index register.  This is the case
5428    for any base register that can access a QImode object.  */
5429 inline static int
5430 thumb1_index_register_rtx_p (rtx x, int strict_p)
5431 {
5432   return thumb1_base_register_rtx_p (x, QImode, strict_p);
5433 }
5434
5435 /* Return nonzero if x is a legitimate 16-bit Thumb-state address.
5436
5437    The AP may be eliminated to either the SP or the FP, so we use the
5438    least common denominator, e.g. SImode, and offsets from 0 to 64.
5439
5440    ??? Verify whether the above is the right approach.
5441
5442    ??? Also, the FP may be eliminated to the SP, so perhaps that
5443    needs special handling also.
5444
5445    ??? Look at how the mips16 port solves this problem.  It probably uses
5446    better ways to solve some of these problems.
5447
5448    Although it is not incorrect, we don't accept QImode and HImode
5449    addresses based on the frame pointer or arg pointer until the
5450    reload pass starts.  This is so that eliminating such addresses
5451    into stack based ones won't produce impossible code.  */
5452 static int
5453 thumb1_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
5454 {
5455   /* ??? Not clear if this is right.  Experiment.  */
5456   if (GET_MODE_SIZE (mode) < 4
5457       && !(reload_in_progress || reload_completed)
5458       && (reg_mentioned_p (frame_pointer_rtx, x)
5459           || reg_mentioned_p (arg_pointer_rtx, x)
5460           || reg_mentioned_p (virtual_incoming_args_rtx, x)
5461           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
5462           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
5463           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
5464     return 0;
5465
5466   /* Accept any base register.  SP only in SImode or larger.  */
5467   else if (thumb1_base_register_rtx_p (x, mode, strict_p))
5468     return 1;
5469
5470   /* This is PC relative data before arm_reorg runs.  */
5471   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
5472            && GET_CODE (x) == SYMBOL_REF
5473            && CONSTANT_POOL_ADDRESS_P (x) && !flag_pic)
5474     return 1;
5475
5476   /* This is PC relative data after arm_reorg runs.  */
5477   else if ((GET_MODE_SIZE (mode) >= 4 || mode == HFmode)
5478            && reload_completed
5479            && (GET_CODE (x) == LABEL_REF
5480                || (GET_CODE (x) == CONST
5481                    && GET_CODE (XEXP (x, 0)) == PLUS
5482                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5483                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5484     return 1;
5485
5486   /* Post-inc indexing only supported for SImode and larger.  */
5487   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
5488            && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p))
5489     return 1;
5490
5491   else if (GET_CODE (x) == PLUS)
5492     {
5493       /* REG+REG address can be any two index registers.  */
5494       /* We disallow FRAME+REG addressing since we know that FRAME
5495          will be replaced with STACK, and SP relative addressing only
5496          permits SP+OFFSET.  */
5497       if (GET_MODE_SIZE (mode) <= 4
5498           && XEXP (x, 0) != frame_pointer_rtx
5499           && XEXP (x, 1) != frame_pointer_rtx
5500           && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
5501           && thumb1_index_register_rtx_p (XEXP (x, 1), strict_p))
5502         return 1;
5503
5504       /* REG+const has 5-7 bit offset for non-SP registers.  */
5505       else if ((thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
5506                 || XEXP (x, 0) == arg_pointer_rtx)
5507                && GET_CODE (XEXP (x, 1)) == CONST_INT
5508                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
5509         return 1;
5510
5511       /* REG+const has 10-bit offset for SP, but only SImode and
5512          larger is supported.  */
5513       /* ??? Should probably check for DI/DFmode overflow here
5514          just like GO_IF_LEGITIMATE_OFFSET does.  */
5515       else if (GET_CODE (XEXP (x, 0)) == REG
5516                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
5517                && GET_MODE_SIZE (mode) >= 4
5518                && GET_CODE (XEXP (x, 1)) == CONST_INT
5519                && INTVAL (XEXP (x, 1)) >= 0
5520                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
5521                && (INTVAL (XEXP (x, 1)) & 3) == 0)
5522         return 1;
5523
5524       else if (GET_CODE (XEXP (x, 0)) == REG
5525                && (REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
5526                    || REGNO (XEXP (x, 0)) == ARG_POINTER_REGNUM
5527                    || (REGNO (XEXP (x, 0)) >= FIRST_VIRTUAL_REGISTER
5528                        && REGNO (XEXP (x, 0)) <= LAST_VIRTUAL_REGISTER))
5529                && GET_MODE_SIZE (mode) >= 4
5530                && GET_CODE (XEXP (x, 1)) == CONST_INT
5531                && (INTVAL (XEXP (x, 1)) & 3) == 0)
5532         return 1;
5533     }
5534
5535   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
5536            && GET_MODE_SIZE (mode) == 4
5537            && GET_CODE (x) == SYMBOL_REF
5538            && CONSTANT_POOL_ADDRESS_P (x)
5539            && ! (flag_pic
5540                  && symbol_mentioned_p (get_pool_constant (x))
5541                  && ! pcrel_constant_p (get_pool_constant (x))))
5542     return 1;
5543
5544   return 0;
5545 }
5546
5547 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
5548    instruction of mode MODE.  */
5549 int
5550 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
5551 {
5552   switch (GET_MODE_SIZE (mode))
5553     {
5554     case 1:
5555       return val >= 0 && val < 32;
5556
5557     case 2:
5558       return val >= 0 && val < 64 && (val & 1) == 0;
5559
5560     default:
5561       return (val >= 0
5562               && (val + GET_MODE_SIZE (mode)) <= 128
5563               && (val & 3) == 0);
5564     }
5565 }
5566
5567 bool
5568 arm_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
5569 {
5570   if (TARGET_ARM)
5571     return arm_legitimate_address_outer_p (mode, x, SET, strict_p);
5572   else if (TARGET_THUMB2)
5573     return thumb2_legitimate_address_p (mode, x, strict_p);
5574   else /* if (TARGET_THUMB1) */
5575     return thumb1_legitimate_address_p (mode, x, strict_p);
5576 }
5577
5578 /* Build the SYMBOL_REF for __tls_get_addr.  */
5579
5580 static GTY(()) rtx tls_get_addr_libfunc;
5581
5582 static rtx
5583 get_tls_get_addr (void)
5584 {
5585   if (!tls_get_addr_libfunc)
5586     tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
5587   return tls_get_addr_libfunc;
5588 }
5589
5590 static rtx
5591 arm_load_tp (rtx target)
5592 {
5593   if (!target)
5594     target = gen_reg_rtx (SImode);
5595
5596   if (TARGET_HARD_TP)
5597     {
5598       /* Can return in any reg.  */
5599       emit_insn (gen_load_tp_hard (target));
5600     }
5601   else
5602     {
5603       /* Always returned in r0.  Immediately copy the result into a pseudo,
5604          otherwise other uses of r0 (e.g. setting up function arguments) may
5605          clobber the value.  */
5606
5607       rtx tmp;
5608
5609       emit_insn (gen_load_tp_soft ());
5610
5611       tmp = gen_rtx_REG (SImode, 0);
5612       emit_move_insn (target, tmp);
5613     }
5614   return target;
5615 }
5616
5617 static rtx
5618 load_tls_operand (rtx x, rtx reg)
5619 {
5620   rtx tmp;
5621
5622   if (reg == NULL_RTX)
5623     reg = gen_reg_rtx (SImode);
5624
5625   tmp = gen_rtx_CONST (SImode, x);
5626
5627   emit_move_insn (reg, tmp);
5628
5629   return reg;
5630 }
5631
5632 static rtx
5633 arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
5634 {
5635   rtx insns, label, labelno, sum;
5636
5637   start_sequence ();
5638
5639   labelno = GEN_INT (pic_labelno++);
5640   label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
5641   label = gen_rtx_CONST (VOIDmode, label);
5642
5643   sum = gen_rtx_UNSPEC (Pmode,
5644                         gen_rtvec (4, x, GEN_INT (reloc), label,
5645                                    GEN_INT (TARGET_ARM ? 8 : 4)),
5646                         UNSPEC_TLS);
5647   reg = load_tls_operand (sum, reg);
5648
5649   if (TARGET_ARM)
5650     emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
5651   else if (TARGET_THUMB2)
5652     {
5653       rtx tmp;
5654       /* Thumb-2 only allows very limited access to the PC.  Calculate
5655          the address in a temporary register.  */
5656       tmp = gen_reg_rtx (SImode);
5657       emit_insn (gen_pic_load_dot_plus_four (tmp, labelno));
5658       emit_insn (gen_addsi3(reg, reg, tmp));
5659     }
5660   else /* TARGET_THUMB1 */
5661     emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5662
5663   *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX, LCT_PURE, /* LCT_CONST?  */
5664                                      Pmode, 1, reg, Pmode);
5665
5666   insns = get_insns ();
5667   end_sequence ();
5668
5669   return insns;
5670 }
5671
5672 rtx
5673 legitimize_tls_address (rtx x, rtx reg)
5674 {
5675   rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
5676   unsigned int model = SYMBOL_REF_TLS_MODEL (x);
5677
5678   switch (model)
5679     {
5680     case TLS_MODEL_GLOBAL_DYNAMIC:
5681       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
5682       dest = gen_reg_rtx (Pmode);
5683       emit_libcall_block (insns, dest, ret, x);
5684       return dest;
5685
5686     case TLS_MODEL_LOCAL_DYNAMIC:
5687       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
5688
5689       /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
5690          share the LDM result with other LD model accesses.  */
5691       eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
5692                             UNSPEC_TLS);
5693       dest = gen_reg_rtx (Pmode);
5694       emit_libcall_block (insns, dest, ret, eqv);
5695
5696       /* Load the addend.  */
5697       addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (TLS_LDO32)),
5698                                UNSPEC_TLS);
5699       addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
5700       return gen_rtx_PLUS (Pmode, dest, addend);
5701
5702     case TLS_MODEL_INITIAL_EXEC:
5703       labelno = GEN_INT (pic_labelno++);
5704       label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
5705       label = gen_rtx_CONST (VOIDmode, label);
5706       sum = gen_rtx_UNSPEC (Pmode,
5707                             gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
5708                                        GEN_INT (TARGET_ARM ? 8 : 4)),
5709                             UNSPEC_TLS);
5710       reg = load_tls_operand (sum, reg);
5711
5712       if (TARGET_ARM)
5713         emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
5714       else if (TARGET_THUMB2)
5715         {
5716           rtx tmp;
5717           /* Thumb-2 only allows very limited access to the PC.  Calculate
5718              the address in a temporary register.  */
5719           tmp = gen_reg_rtx (SImode);
5720           emit_insn (gen_pic_load_dot_plus_four (tmp, labelno));
5721           emit_insn (gen_addsi3(reg, reg, tmp));
5722           emit_move_insn (reg, gen_const_mem (SImode, reg));
5723         }
5724       else
5725         {
5726           emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
5727           emit_move_insn (reg, gen_const_mem (SImode, reg));
5728         }
5729
5730       tp = arm_load_tp (NULL_RTX);
5731
5732       return gen_rtx_PLUS (Pmode, tp, reg);
5733
5734     case TLS_MODEL_LOCAL_EXEC:
5735       tp = arm_load_tp (NULL_RTX);
5736
5737       reg = gen_rtx_UNSPEC (Pmode,
5738                             gen_rtvec (2, x, GEN_INT (TLS_LE32)),
5739                             UNSPEC_TLS);
5740       reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
5741
5742       return gen_rtx_PLUS (Pmode, tp, reg);
5743
5744     default:
5745       abort ();
5746     }
5747 }
5748
5749 /* Try machine-dependent ways of modifying an illegitimate address
5750    to be legitimate.  If we find one, return the new, valid address.  */
5751 rtx
5752 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
5753 {
5754   if (!TARGET_ARM)
5755     {
5756       /* TODO: legitimize_address for Thumb2.  */
5757       if (TARGET_THUMB2)
5758         return x;
5759       return thumb_legitimize_address (x, orig_x, mode);
5760     }
5761
5762   if (arm_tls_symbol_p (x))
5763     return legitimize_tls_address (x, NULL_RTX);
5764
5765   if (GET_CODE (x) == PLUS)
5766     {
5767       rtx xop0 = XEXP (x, 0);
5768       rtx xop1 = XEXP (x, 1);
5769
5770       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
5771         xop0 = force_reg (SImode, xop0);
5772
5773       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
5774         xop1 = force_reg (SImode, xop1);
5775
5776       if (ARM_BASE_REGISTER_RTX_P (xop0)
5777           && GET_CODE (xop1) == CONST_INT)
5778         {
5779           HOST_WIDE_INT n, low_n;
5780           rtx base_reg, val;
5781           n = INTVAL (xop1);
5782
5783           /* VFP addressing modes actually allow greater offsets, but for
5784              now we just stick with the lowest common denominator.  */
5785           if (mode == DImode
5786               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
5787             {
5788               low_n = n & 0x0f;
5789               n &= ~0x0f;
5790               if (low_n > 4)
5791                 {
5792                   n += 16;
5793                   low_n -= 16;
5794                 }
5795             }
5796           else
5797             {
5798               low_n = ((mode) == TImode ? 0
5799                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
5800               n -= low_n;
5801             }
5802
5803           base_reg = gen_reg_rtx (SImode);
5804           val = force_operand (plus_constant (xop0, n), NULL_RTX);
5805           emit_move_insn (base_reg, val);
5806           x = plus_constant (base_reg, low_n);
5807         }
5808       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
5809         x = gen_rtx_PLUS (SImode, xop0, xop1);
5810     }
5811
5812   /* XXX We don't allow MINUS any more -- see comment in
5813      arm_legitimate_address_outer_p ().  */
5814   else if (GET_CODE (x) == MINUS)
5815     {
5816       rtx xop0 = XEXP (x, 0);
5817       rtx xop1 = XEXP (x, 1);
5818
5819       if (CONSTANT_P (xop0))
5820         xop0 = force_reg (SImode, xop0);
5821
5822       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
5823         xop1 = force_reg (SImode, xop1);
5824
5825       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
5826         x = gen_rtx_MINUS (SImode, xop0, xop1);
5827     }
5828
5829   /* Make sure to take full advantage of the pre-indexed addressing mode
5830      with absolute addresses which often allows for the base register to
5831      be factorized for multiple adjacent memory references, and it might
5832      even allows for the mini pool to be avoided entirely. */
5833   else if (GET_CODE (x) == CONST_INT && optimize > 0)
5834     {
5835       unsigned int bits;
5836       HOST_WIDE_INT mask, base, index;
5837       rtx base_reg;
5838
5839       /* ldr and ldrb can use a 12-bit index, ldrsb and the rest can only
5840          use a 8-bit index. So let's use a 12-bit index for SImode only and
5841          hope that arm_gen_constant will enable ldrb to use more bits. */
5842       bits = (mode == SImode) ? 12 : 8;
5843       mask = (1 << bits) - 1;
5844       base = INTVAL (x) & ~mask;
5845       index = INTVAL (x) & mask;
5846       if (bit_count (base & 0xffffffff) > (32 - bits)/2)
5847         {
5848           /* It'll most probably be more efficient to generate the base
5849              with more bits set and use a negative index instead. */
5850           base |= mask;
5851           index -= mask;
5852         }
5853       base_reg = force_reg (SImode, GEN_INT (base));
5854       x = plus_constant (base_reg, index);
5855     }
5856
5857   if (flag_pic)
5858     {
5859       /* We need to find and carefully transform any SYMBOL and LABEL
5860          references; so go back to the original address expression.  */
5861       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
5862
5863       if (new_x != orig_x)
5864         x = new_x;
5865     }
5866
5867   return x;
5868 }
5869
5870
5871 /* Try machine-dependent ways of modifying an illegitimate Thumb address
5872    to be legitimate.  If we find one, return the new, valid address.  */
5873 rtx
5874 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
5875 {
5876   if (arm_tls_symbol_p (x))
5877     return legitimize_tls_address (x, NULL_RTX);
5878
5879   if (GET_CODE (x) == PLUS
5880       && GET_CODE (XEXP (x, 1)) == CONST_INT
5881       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
5882           || INTVAL (XEXP (x, 1)) < 0))
5883     {
5884       rtx xop0 = XEXP (x, 0);
5885       rtx xop1 = XEXP (x, 1);
5886       HOST_WIDE_INT offset = INTVAL (xop1);
5887
5888       /* Try and fold the offset into a biasing of the base register and
5889          then offsetting that.  Don't do this when optimizing for space
5890          since it can cause too many CSEs.  */
5891       if (optimize_size && offset >= 0
5892           && offset < 256 + 31 * GET_MODE_SIZE (mode))
5893         {
5894           HOST_WIDE_INT delta;
5895
5896           if (offset >= 256)
5897             delta = offset - (256 - GET_MODE_SIZE (mode));
5898           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
5899             delta = 31 * GET_MODE_SIZE (mode);
5900           else
5901             delta = offset & (~31 * GET_MODE_SIZE (mode));
5902
5903           xop0 = force_operand (plus_constant (xop0, offset - delta),
5904                                 NULL_RTX);
5905           x = plus_constant (xop0, delta);
5906         }
5907       else if (offset < 0 && offset > -256)
5908         /* Small negative offsets are best done with a subtract before the
5909            dereference, forcing these into a register normally takes two
5910            instructions.  */
5911         x = force_operand (x, NULL_RTX);
5912       else
5913         {
5914           /* For the remaining cases, force the constant into a register.  */
5915           xop1 = force_reg (SImode, xop1);
5916           x = gen_rtx_PLUS (SImode, xop0, xop1);
5917         }
5918     }
5919   else if (GET_CODE (x) == PLUS
5920            && s_register_operand (XEXP (x, 1), SImode)
5921            && !s_register_operand (XEXP (x, 0), SImode))
5922     {
5923       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
5924
5925       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
5926     }
5927
5928   if (flag_pic)
5929     {
5930       /* We need to find and carefully transform any SYMBOL and LABEL
5931          references; so go back to the original address expression.  */
5932       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
5933
5934       if (new_x != orig_x)
5935         x = new_x;
5936     }
5937
5938   return x;
5939 }
5940
5941 rtx
5942 thumb_legitimize_reload_address (rtx *x_p,
5943                                  enum machine_mode mode,
5944                                  int opnum, int type,
5945                                  int ind_levels ATTRIBUTE_UNUSED)
5946 {
5947   rtx x = *x_p;
5948
5949   if (GET_CODE (x) == PLUS
5950       && GET_MODE_SIZE (mode) < 4
5951       && REG_P (XEXP (x, 0))
5952       && XEXP (x, 0) == stack_pointer_rtx
5953       && GET_CODE (XEXP (x, 1)) == CONST_INT
5954       && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
5955     {
5956       rtx orig_x = x;
5957
5958       x = copy_rtx (x);
5959       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
5960                    Pmode, VOIDmode, 0, 0, opnum, (enum reload_type) type);
5961       return x;
5962     }
5963
5964   /* If both registers are hi-regs, then it's better to reload the
5965      entire expression rather than each register individually.  That
5966      only requires one reload register rather than two.  */
5967   if (GET_CODE (x) == PLUS
5968       && REG_P (XEXP (x, 0))
5969       && REG_P (XEXP (x, 1))
5970       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
5971       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
5972     {
5973       rtx orig_x = x;
5974
5975       x = copy_rtx (x);
5976       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
5977                    Pmode, VOIDmode, 0, 0, opnum, (enum reload_type) type);
5978       return x;
5979     }
5980
5981   return NULL;
5982 }
5983
5984 /* Test for various thread-local symbols.  */
5985
5986 /* Return TRUE if X is a thread-local symbol.  */
5987
5988 static bool
5989 arm_tls_symbol_p (rtx x)
5990 {
5991   if (! TARGET_HAVE_TLS)
5992     return false;
5993
5994   if (GET_CODE (x) != SYMBOL_REF)
5995     return false;
5996
5997   return SYMBOL_REF_TLS_MODEL (x) != 0;
5998 }
5999
6000 /* Helper for arm_tls_referenced_p.  */
6001
6002 static int
6003 arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
6004 {
6005   if (GET_CODE (*x) == SYMBOL_REF)
6006     return SYMBOL_REF_TLS_MODEL (*x) != 0;
6007
6008   /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
6009      TLS offsets, not real symbol references.  */
6010   if (GET_CODE (*x) == UNSPEC
6011       && XINT (*x, 1) == UNSPEC_TLS)
6012     return -1;
6013
6014   return 0;
6015 }
6016
6017 /* Return TRUE if X contains any TLS symbol references.  */
6018
6019 bool
6020 arm_tls_referenced_p (rtx x)
6021 {
6022   if (! TARGET_HAVE_TLS)
6023     return false;
6024
6025   return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
6026 }
6027
6028 /* Implement TARGET_CANNOT_FORCE_CONST_MEM.  */
6029
6030 bool
6031 arm_cannot_force_const_mem (rtx x)
6032 {
6033   rtx base, offset;
6034
6035   if (ARM_OFFSETS_MUST_BE_WITHIN_SECTIONS_P)
6036     {
6037       split_const (x, &base, &offset);
6038       if (GET_CODE (base) == SYMBOL_REF
6039           && !offset_within_block_p (base, INTVAL (offset)))
6040         return true;
6041     }
6042   return arm_tls_referenced_p (x);
6043 }
6044 \f
6045 #define REG_OR_SUBREG_REG(X)                                            \
6046   (GET_CODE (X) == REG                                                  \
6047    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
6048
6049 #define REG_OR_SUBREG_RTX(X)                    \
6050    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
6051
6052 #ifndef COSTS_N_INSNS
6053 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
6054 #endif
6055 static inline int
6056 thumb1_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
6057 {
6058   enum machine_mode mode = GET_MODE (x);
6059
6060   switch (code)
6061     {
6062     case ASHIFT:
6063     case ASHIFTRT:
6064     case LSHIFTRT:
6065     case ROTATERT:
6066     case PLUS:
6067     case MINUS:
6068     case COMPARE:
6069     case NEG:
6070     case NOT:
6071       return COSTS_N_INSNS (1);
6072
6073     case MULT:
6074       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6075         {
6076           int cycles = 0;
6077           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
6078
6079           while (i)
6080             {
6081               i >>= 2;
6082               cycles++;
6083             }
6084           return COSTS_N_INSNS (2) + cycles;
6085         }
6086       return COSTS_N_INSNS (1) + 16;
6087
6088     case SET:
6089       return (COSTS_N_INSNS (1)
6090               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
6091                      + GET_CODE (SET_DEST (x)) == MEM));
6092
6093     case CONST_INT:
6094       if (outer == SET)
6095         {
6096           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
6097             return 0;
6098           if (thumb_shiftable_const (INTVAL (x)))
6099             return COSTS_N_INSNS (2);
6100           return COSTS_N_INSNS (3);
6101         }
6102       else if ((outer == PLUS || outer == COMPARE)
6103                && INTVAL (x) < 256 && INTVAL (x) > -256)
6104         return 0;
6105       else if (outer == AND
6106                && INTVAL (x) < 256 && INTVAL (x) >= -256)
6107         return COSTS_N_INSNS (1);
6108       else if (outer == ASHIFT || outer == ASHIFTRT
6109                || outer == LSHIFTRT)
6110         return 0;
6111       return COSTS_N_INSNS (2);
6112
6113     case CONST:
6114     case CONST_DOUBLE:
6115     case LABEL_REF:
6116     case SYMBOL_REF:
6117       return COSTS_N_INSNS (3);
6118
6119     case UDIV:
6120     case UMOD:
6121     case DIV:
6122     case MOD:
6123       return 100;
6124
6125     case TRUNCATE:
6126       return 99;
6127
6128     case AND:
6129     case XOR:
6130     case IOR:
6131       /* XXX guess.  */
6132       return 8;
6133
6134     case MEM:
6135       /* XXX another guess.  */
6136       /* Memory costs quite a lot for the first word, but subsequent words
6137          load at the equivalent of a single insn each.  */
6138       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
6139               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
6140                  ? 4 : 0));
6141
6142     case IF_THEN_ELSE:
6143       /* XXX a guess.  */
6144       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
6145         return 14;
6146       return 2;
6147
6148     case ZERO_EXTEND:
6149       /* XXX still guessing.  */
6150       switch (GET_MODE (XEXP (x, 0)))
6151         {
6152         case QImode:
6153           return (1 + (mode == DImode ? 4 : 0)
6154                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
6155
6156         case HImode:
6157           return (4 + (mode == DImode ? 4 : 0)
6158                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
6159
6160         case SImode:
6161           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
6162
6163         default:
6164           return 99;
6165         }
6166
6167     default:
6168       return 99;
6169     }
6170 }
6171
6172 static inline bool
6173 arm_rtx_costs_1 (rtx x, enum rtx_code outer, int* total, bool speed)
6174 {
6175   enum machine_mode mode = GET_MODE (x);
6176   enum rtx_code subcode;
6177   rtx operand;
6178   enum rtx_code code = GET_CODE (x);
6179   int extra_cost;
6180   *total = 0;
6181
6182   switch (code)
6183     {
6184     case MEM:
6185       /* Memory costs quite a lot for the first word, but subsequent words
6186          load at the equivalent of a single insn each.  */
6187       *total = COSTS_N_INSNS (2 + ARM_NUM_REGS (mode));
6188       return true;
6189
6190     case DIV:
6191     case MOD:
6192     case UDIV:
6193     case UMOD:
6194       if (TARGET_HARD_FLOAT && mode == SFmode)
6195         *total = COSTS_N_INSNS (2);
6196       else if (TARGET_HARD_FLOAT && mode == DFmode)
6197         *total = COSTS_N_INSNS (4);
6198       else
6199         *total = COSTS_N_INSNS (20);
6200       return false;
6201
6202     case ROTATE:
6203       if (GET_CODE (XEXP (x, 1)) == REG)
6204         *total = COSTS_N_INSNS (1); /* Need to subtract from 32 */
6205       else if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6206         *total = rtx_cost (XEXP (x, 1), code, speed);
6207
6208       /* Fall through */
6209     case ROTATERT:
6210       if (mode != SImode)
6211         {
6212           *total += COSTS_N_INSNS (4);
6213           return true;
6214         }
6215
6216       /* Fall through */
6217     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
6218       *total += rtx_cost (XEXP (x, 0), code, speed);
6219       if (mode == DImode)
6220         {
6221           *total += COSTS_N_INSNS (3);
6222           return true;
6223         }
6224
6225       *total += COSTS_N_INSNS (1);
6226       /* Increase the cost of complex shifts because they aren't any faster,
6227          and reduce dual issue opportunities.  */
6228       if (arm_tune_cortex_a9
6229           && outer != SET && GET_CODE (XEXP (x, 1)) != CONST_INT)
6230         ++*total;
6231
6232       return true;
6233
6234     case MINUS:
6235       if (TARGET_THUMB2)
6236         {
6237           if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6238             {
6239               if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
6240                 *total = COSTS_N_INSNS (1);
6241               else
6242                 *total = COSTS_N_INSNS (20);
6243             }
6244           else
6245             *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
6246           /* Thumb2 does not have RSB, so all arguments must be
6247              registers (subtracting a constant is canonicalized as
6248              addition of the negated constant).  */
6249           return false;
6250         }
6251
6252       if (mode == DImode)
6253         {
6254           *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
6255           if (GET_CODE (XEXP (x, 0)) == CONST_INT
6256               && const_ok_for_arm (INTVAL (XEXP (x, 0))))
6257             {
6258               *total += rtx_cost (XEXP (x, 1), code, speed);
6259               return true;
6260             }
6261
6262           if (GET_CODE (XEXP (x, 1)) == CONST_INT
6263               && const_ok_for_arm (INTVAL (XEXP (x, 1))))
6264             {
6265               *total += rtx_cost (XEXP (x, 0), code, speed);
6266               return true;
6267             }
6268
6269           return false;
6270         }
6271
6272       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6273         {
6274           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
6275             {
6276               *total = COSTS_N_INSNS (1);
6277               if (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
6278                   && arm_const_double_rtx (XEXP (x, 0)))
6279                 {
6280                   *total += rtx_cost (XEXP (x, 1), code, speed);
6281                   return true;
6282                 }
6283
6284               if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
6285                   && arm_const_double_rtx (XEXP (x, 1)))
6286                 {
6287                   *total += rtx_cost (XEXP (x, 0), code, speed);
6288                   return true;
6289                 }
6290
6291               return false;
6292             }
6293           *total = COSTS_N_INSNS (20);
6294           return false;
6295         }
6296
6297       *total = COSTS_N_INSNS (1);
6298       if (GET_CODE (XEXP (x, 0)) == CONST_INT
6299           && const_ok_for_arm (INTVAL (XEXP (x, 0))))
6300         {
6301           *total += rtx_cost (XEXP (x, 1), code, speed);
6302           return true;
6303         }
6304
6305       subcode = GET_CODE (XEXP (x, 1));
6306       if (subcode == ASHIFT || subcode == ASHIFTRT
6307           || subcode == LSHIFTRT
6308           || subcode == ROTATE || subcode == ROTATERT)
6309         {
6310           *total += rtx_cost (XEXP (x, 0), code, speed);
6311           *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, speed);
6312           return true;
6313         }
6314
6315       /* A shift as a part of RSB costs no more than RSB itself.  */
6316       if (GET_CODE (XEXP (x, 0)) == MULT
6317           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
6318         {
6319           *total += rtx_cost (XEXP (XEXP (x, 0), 0), code, speed);
6320           *total += rtx_cost (XEXP (x, 1), code, speed);
6321           return true;
6322         }
6323
6324       if (subcode == MULT
6325           && power_of_two_operand (XEXP (XEXP (x, 1), 1), SImode))
6326         {
6327           *total += rtx_cost (XEXP (x, 0), code, speed);
6328           *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, speed);
6329           return true;
6330         }
6331
6332       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMPARE
6333           || GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMM_COMPARE)
6334         {
6335           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
6336           if (GET_CODE (XEXP (XEXP (x, 1), 0)) == REG
6337               && REGNO (XEXP (XEXP (x, 1), 0)) != CC_REGNUM)
6338             *total += COSTS_N_INSNS (1);
6339
6340           return true;
6341         }
6342
6343       /* Fall through */
6344
6345     case PLUS:
6346       if (code == PLUS && arm_arch6 && mode == SImode
6347           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
6348               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
6349         {
6350           *total = COSTS_N_INSNS (1);
6351           *total += rtx_cost (XEXP (XEXP (x, 0), 0), GET_CODE (XEXP (x, 0)),
6352                               speed);
6353           *total += rtx_cost (XEXP (x, 1), code, speed);
6354           return true;
6355         }
6356
6357       /* MLA: All arguments must be registers.  We filter out
6358          multiplication by a power of two, so that we fall down into
6359          the code below.  */
6360       if (GET_CODE (XEXP (x, 0)) == MULT
6361           && !power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
6362         {
6363           /* The cost comes from the cost of the multiply.  */
6364           return false;
6365         }
6366
6367       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6368         {
6369           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
6370             {
6371               *total = COSTS_N_INSNS (1);
6372               if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
6373                   && arm_const_double_rtx (XEXP (x, 1)))
6374                 {
6375                   *total += rtx_cost (XEXP (x, 0), code, speed);
6376                   return true;
6377                 }
6378
6379               return false;
6380             }
6381
6382           *total = COSTS_N_INSNS (20);
6383           return false;
6384         }
6385
6386       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMPARE
6387           || GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMM_COMPARE)
6388         {
6389           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 1), code, speed);
6390           if (GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
6391               && REGNO (XEXP (XEXP (x, 0), 0)) != CC_REGNUM)
6392             *total += COSTS_N_INSNS (1);
6393           return true;
6394         }
6395
6396       /* Fall through */
6397
6398     case AND: case XOR: case IOR:
6399       extra_cost = 0;
6400
6401       /* Normally the frame registers will be spilt into reg+const during
6402          reload, so it is a bad idea to combine them with other instructions,
6403          since then they might not be moved outside of loops.  As a compromise
6404          we allow integration with ops that have a constant as their second
6405          operand.  */
6406       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
6407            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
6408            && GET_CODE (XEXP (x, 1)) != CONST_INT)
6409           || (REG_OR_SUBREG_REG (XEXP (x, 0))
6410               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
6411         *total = 4;
6412
6413       if (mode == DImode)
6414         {
6415           *total += COSTS_N_INSNS (2);
6416           if (GET_CODE (XEXP (x, 1)) == CONST_INT
6417               && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
6418             {
6419               *total += rtx_cost (XEXP (x, 0), code, speed);
6420               return true;
6421             }
6422
6423           return false;
6424         }
6425
6426       *total += COSTS_N_INSNS (1);
6427       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6428           && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
6429         {
6430           *total += rtx_cost (XEXP (x, 0), code, speed);
6431           return true;
6432         }
6433       subcode = GET_CODE (XEXP (x, 0));
6434       if (subcode == ASHIFT || subcode == ASHIFTRT
6435           || subcode == LSHIFTRT
6436           || subcode == ROTATE || subcode == ROTATERT)
6437         {
6438           *total += rtx_cost (XEXP (x, 1), code, speed);
6439           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6440           return true;
6441         }
6442
6443       if (subcode == MULT
6444           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
6445         {
6446           *total += rtx_cost (XEXP (x, 1), code, speed);
6447           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6448           return true;
6449         }
6450
6451       if (subcode == UMIN || subcode == UMAX
6452           || subcode == SMIN || subcode == SMAX)
6453         {
6454           *total = COSTS_N_INSNS (3);
6455           return true;
6456         }
6457
6458       return false;
6459
6460     case MULT:
6461       /* This should have been handled by the CPU specific routines.  */
6462       gcc_unreachable ();
6463
6464     case TRUNCATE:
6465       if (arm_arch3m && mode == SImode
6466           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
6467           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
6468           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
6469               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
6470           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
6471               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
6472         {
6473           *total = rtx_cost (XEXP (XEXP (x, 0), 0), LSHIFTRT, speed);
6474           return true;
6475         }
6476       *total = COSTS_N_INSNS (2); /* Plus the cost of the MULT */
6477       return false;
6478
6479     case NEG:
6480       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6481         {
6482           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
6483             {
6484               *total = COSTS_N_INSNS (1);
6485               return false;
6486             }
6487           *total = COSTS_N_INSNS (2);
6488           return false;
6489         }
6490
6491       /* Fall through */
6492     case NOT:
6493       *total = COSTS_N_INSNS (ARM_NUM_REGS(mode));
6494       if (mode == SImode && code == NOT)
6495         {
6496           subcode = GET_CODE (XEXP (x, 0));
6497           if (subcode == ASHIFT || subcode == ASHIFTRT
6498               || subcode == LSHIFTRT
6499               || subcode == ROTATE || subcode == ROTATERT
6500               || (subcode == MULT
6501                   && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode)))
6502             {
6503               *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6504               /* Register shifts cost an extra cycle.  */
6505               if (GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
6506                 *total += COSTS_N_INSNS (1) + rtx_cost (XEXP (XEXP (x, 0), 1),
6507                                                         subcode, speed);
6508               return true;
6509             }
6510         }
6511
6512       return false;
6513
6514     case IF_THEN_ELSE:
6515       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
6516         {
6517           *total = COSTS_N_INSNS (4);
6518           return true;
6519         }
6520
6521       operand = XEXP (x, 0);
6522
6523       if (!((GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMPARE
6524              || GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMM_COMPARE)
6525             && GET_CODE (XEXP (operand, 0)) == REG
6526             && REGNO (XEXP (operand, 0)) == CC_REGNUM))
6527         *total += COSTS_N_INSNS (1);
6528       *total += (rtx_cost (XEXP (x, 1), code, speed)
6529                  + rtx_cost (XEXP (x, 2), code, speed));
6530       return true;
6531
6532     case NE:
6533       if (mode == SImode && XEXP (x, 1) == const0_rtx)
6534         {
6535           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
6536           return true;
6537         }
6538       goto scc_insn;
6539
6540     case GE:
6541       if ((GET_CODE (XEXP (x, 0)) != REG || REGNO (XEXP (x, 0)) != CC_REGNUM)
6542           && mode == SImode && XEXP (x, 1) == const0_rtx)
6543         {
6544           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
6545           return true;
6546         }
6547       goto scc_insn;
6548
6549     case LT:
6550       if ((GET_CODE (XEXP (x, 0)) != REG || REGNO (XEXP (x, 0)) != CC_REGNUM)
6551           && mode == SImode && XEXP (x, 1) == const0_rtx)
6552         {
6553           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
6554           return true;
6555         }
6556       goto scc_insn;
6557
6558     case EQ:
6559     case GT:
6560     case LE:
6561     case GEU:
6562     case LTU:
6563     case GTU:
6564     case LEU:
6565     case UNORDERED:
6566     case ORDERED:
6567     case UNEQ:
6568     case UNGE:
6569     case UNLT:
6570     case UNGT:
6571     case UNLE:
6572     scc_insn:
6573       /* SCC insns.  In the case where the comparison has already been
6574          performed, then they cost 2 instructions.  Otherwise they need
6575          an additional comparison before them.  */
6576       *total = COSTS_N_INSNS (2);
6577       if (GET_CODE (XEXP (x, 0)) == REG && REGNO (XEXP (x, 0)) == CC_REGNUM)
6578         {
6579           return true;
6580         }
6581
6582       /* Fall through */
6583     case COMPARE:
6584       if (GET_CODE (XEXP (x, 0)) == REG && REGNO (XEXP (x, 0)) == CC_REGNUM)
6585         {
6586           *total = 0;
6587           return true;
6588         }
6589
6590       *total += COSTS_N_INSNS (1);
6591       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6592           && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
6593         {
6594           *total += rtx_cost (XEXP (x, 0), code, speed);
6595           return true;
6596         }
6597
6598       subcode = GET_CODE (XEXP (x, 0));
6599       if (subcode == ASHIFT || subcode == ASHIFTRT
6600           || subcode == LSHIFTRT
6601           || subcode == ROTATE || subcode == ROTATERT)
6602         {
6603           *total += rtx_cost (XEXP (x, 1), code, speed);
6604           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6605           return true;
6606         }
6607
6608       if (subcode == MULT
6609           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
6610         {
6611           *total += rtx_cost (XEXP (x, 1), code, speed);
6612           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
6613           return true;
6614         }
6615       
6616       return false;
6617
6618     case UMIN:
6619     case UMAX:
6620     case SMIN:
6621     case SMAX:
6622       *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
6623       if (GET_CODE (XEXP (x, 1)) != CONST_INT
6624           || !const_ok_for_arm (INTVAL (XEXP (x, 1))))
6625         *total += rtx_cost (XEXP (x, 1), code, speed);
6626       return true;
6627
6628     case ABS:
6629       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6630         {
6631           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
6632             {
6633               *total = COSTS_N_INSNS (1);
6634               return false;
6635             }
6636           *total = COSTS_N_INSNS (20);
6637           return false;
6638         }
6639       *total = COSTS_N_INSNS (1);
6640       if (mode == DImode)
6641         *total += COSTS_N_INSNS (3);
6642       return false;
6643
6644     case SIGN_EXTEND:
6645       if (GET_MODE_CLASS (mode) == MODE_INT)
6646         {
6647           *total = 0;
6648           if (mode == DImode)
6649             *total += COSTS_N_INSNS (1);
6650
6651           if (GET_MODE (XEXP (x, 0)) != SImode)
6652             {
6653               if (arm_arch6)
6654                 {
6655                   if (GET_CODE (XEXP (x, 0)) != MEM)
6656                     *total += COSTS_N_INSNS (1);
6657                 }
6658               else if (!arm_arch4 || GET_CODE (XEXP (x, 0)) != MEM)
6659                 *total += COSTS_N_INSNS (2);
6660             }
6661
6662           return false;
6663         }
6664
6665       /* Fall through */
6666     case ZERO_EXTEND:
6667       *total = 0;
6668       if (GET_MODE_CLASS (mode) == MODE_INT)
6669         {
6670           if (mode == DImode)
6671             *total += COSTS_N_INSNS (1);
6672
6673           if (GET_MODE (XEXP (x, 0)) != SImode)
6674             {
6675               if (arm_arch6)
6676                 {
6677                   if (GET_CODE (XEXP (x, 0)) != MEM)
6678                     *total += COSTS_N_INSNS (1);
6679                 }
6680               else if (!arm_arch4 || GET_CODE (XEXP (x, 0)) != MEM)
6681                 *total += COSTS_N_INSNS (GET_MODE (XEXP (x, 0)) == QImode ?
6682                                          1 : 2);
6683             }
6684
6685           return false;
6686         }
6687
6688       switch (GET_MODE (XEXP (x, 0)))
6689         {
6690         case V8QImode:
6691         case V4HImode:
6692         case V2SImode:
6693         case V4QImode:
6694         case V2HImode:
6695           *total = COSTS_N_INSNS (1);
6696           return false;
6697
6698         default:
6699           gcc_unreachable ();
6700         }
6701       gcc_unreachable ();
6702
6703     case ZERO_EXTRACT:
6704     case SIGN_EXTRACT:
6705       *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
6706       return true;
6707
6708     case CONST_INT:
6709       if (const_ok_for_arm (INTVAL (x))
6710           || const_ok_for_arm (~INTVAL (x)))
6711         *total = COSTS_N_INSNS (1);
6712       else
6713         *total = COSTS_N_INSNS (arm_gen_constant (SET, mode, NULL_RTX,
6714                                                   INTVAL (x), NULL_RTX,
6715                                                   NULL_RTX, 0, 0));
6716       return true;
6717
6718     case CONST:
6719     case LABEL_REF:
6720     case SYMBOL_REF:
6721       *total = COSTS_N_INSNS (3);
6722       return true;
6723
6724     case HIGH:
6725       *total = COSTS_N_INSNS (1);
6726       return true;
6727
6728     case LO_SUM:
6729       *total = COSTS_N_INSNS (1);
6730       *total += rtx_cost (XEXP (x, 0), code, speed);
6731       return true;
6732
6733     case CONST_DOUBLE:
6734       if (TARGET_HARD_FLOAT && vfp3_const_double_rtx (x))
6735         *total = COSTS_N_INSNS (1);
6736       else
6737         *total = COSTS_N_INSNS (4);
6738       return true;
6739
6740     default:
6741       *total = COSTS_N_INSNS (4);
6742       return false;
6743     }
6744 }
6745
6746 /* RTX costs when optimizing for size.  */
6747 static bool
6748 arm_size_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
6749                     int *total)
6750 {
6751   enum machine_mode mode = GET_MODE (x);
6752   if (TARGET_THUMB1)
6753     {
6754       /* XXX TBD.  For now, use the standard costs.  */
6755       *total = thumb1_rtx_costs (x, code, outer_code);
6756       return true;
6757     }
6758
6759   /* FIXME: This makes no attempt to prefer narrow Thumb-2 instructions.  */
6760   switch (code)
6761     {
6762     case MEM:
6763       /* A memory access costs 1 insn if the mode is small, or the address is
6764          a single register, otherwise it costs one insn per word.  */
6765       if (REG_P (XEXP (x, 0)))
6766         *total = COSTS_N_INSNS (1);
6767       else
6768         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
6769       return true;
6770
6771     case DIV:
6772     case MOD:
6773     case UDIV:
6774     case UMOD:
6775       /* Needs a libcall, so it costs about this.  */
6776       *total = COSTS_N_INSNS (2);
6777       return false;
6778
6779     case ROTATE:
6780       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
6781         {
6782           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, false);
6783           return true;
6784         }
6785       /* Fall through */
6786     case ROTATERT:
6787     case ASHIFT:
6788     case LSHIFTRT:
6789     case ASHIFTRT:
6790       if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
6791         {
6792           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code, false);
6793           return true;
6794         }
6795       else if (mode == SImode)
6796         {
6797           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, false);
6798           /* Slightly disparage register shifts, but not by much.  */
6799           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6800             *total += 1 + rtx_cost (XEXP (x, 1), code, false);
6801           return true;
6802         }
6803
6804       /* Needs a libcall.  */
6805       *total = COSTS_N_INSNS (2);
6806       return false;
6807
6808     case MINUS:
6809       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
6810         {
6811           *total = COSTS_N_INSNS (1);
6812           return false;
6813         }
6814
6815       if (mode == SImode)
6816         {
6817           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
6818           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
6819
6820           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
6821               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
6822               || subcode1 == ROTATE || subcode1 == ROTATERT
6823               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
6824               || subcode1 == ASHIFTRT)
6825             {
6826               /* It's just the cost of the two operands.  */
6827               *total = 0;
6828               return false;
6829             }
6830
6831           *total = COSTS_N_INSNS (1);
6832           return false;
6833         }
6834
6835       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
6836       return false;
6837
6838     case PLUS:
6839       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
6840         {
6841           *total = COSTS_N_INSNS (1);
6842           return false;
6843         }
6844
6845       /* A shift as a part of ADD costs nothing.  */
6846       if (GET_CODE (XEXP (x, 0)) == MULT
6847           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
6848         {
6849           *total = COSTS_N_INSNS (TARGET_THUMB2 ? 2 : 1);
6850           *total += rtx_cost (XEXP (XEXP (x, 0), 0), code, false);
6851           *total += rtx_cost (XEXP (x, 1), code, false);
6852           return true;
6853         }
6854
6855       /* Fall through */
6856     case AND: case XOR: case IOR:
6857       if (mode == SImode)
6858         {
6859           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
6860
6861           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
6862               || subcode == LSHIFTRT || subcode == ASHIFTRT
6863               || (code == AND && subcode == NOT))
6864             {
6865               /* It's just the cost of the two operands.  */
6866               *total = 0;
6867               return false;
6868             }
6869         }
6870
6871       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
6872       return false;
6873
6874     case MULT:
6875       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
6876       return false;
6877
6878     case NEG:
6879       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
6880         {
6881           *total = COSTS_N_INSNS (1);
6882           return false;
6883         }
6884
6885       /* Fall through */
6886     case NOT:
6887       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
6888
6889       return false;
6890
6891     case IF_THEN_ELSE:
6892       *total = 0;
6893       return false;
6894
6895     case COMPARE:
6896       if (cc_register (XEXP (x, 0), VOIDmode))
6897         * total = 0;
6898       else
6899         *total = COSTS_N_INSNS (1);
6900       return false;
6901
6902     case ABS:
6903       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
6904         *total = COSTS_N_INSNS (1);
6905       else
6906         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
6907       return false;
6908
6909     case SIGN_EXTEND:
6910       *total = 0;
6911       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
6912         {
6913           if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
6914             *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
6915         }
6916       if (mode == DImode)
6917         *total += COSTS_N_INSNS (1);
6918       return false;
6919
6920     case ZERO_EXTEND:
6921       *total = 0;
6922       if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
6923         {
6924           switch (GET_MODE (XEXP (x, 0)))
6925             {
6926             case QImode:
6927               *total += COSTS_N_INSNS (1);
6928               break;
6929
6930             case HImode:
6931               *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
6932
6933             case SImode:
6934               break;
6935
6936             default:
6937               *total += COSTS_N_INSNS (2);
6938             }
6939         }
6940
6941       if (mode == DImode)
6942         *total += COSTS_N_INSNS (1);
6943
6944       return false;
6945
6946     case CONST_INT:
6947       if (const_ok_for_arm (INTVAL (x)))
6948         /* A multiplication by a constant requires another instruction
6949            to load the constant to a register.  */
6950         *total = COSTS_N_INSNS ((outer_code == SET || outer_code == MULT)
6951                                 ? 1 : 0);
6952       else if (const_ok_for_arm (~INTVAL (x)))
6953         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
6954       else if (const_ok_for_arm (-INTVAL (x)))
6955         {
6956           if (outer_code == COMPARE || outer_code == PLUS
6957               || outer_code == MINUS)
6958             *total = 0;
6959           else
6960             *total = COSTS_N_INSNS (1);
6961         }
6962       else
6963         *total = COSTS_N_INSNS (2);
6964       return true;
6965
6966     case CONST:
6967     case LABEL_REF:
6968     case SYMBOL_REF:
6969       *total = COSTS_N_INSNS (2);
6970       return true;
6971
6972     case CONST_DOUBLE:
6973       *total = COSTS_N_INSNS (4);
6974       return true;
6975
6976     case HIGH:
6977     case LO_SUM:
6978       /* We prefer constant pool entries to MOVW/MOVT pairs, so bump the
6979          cost of these slightly.  */
6980       *total = COSTS_N_INSNS (1) + 1;
6981       return true;
6982
6983     default:
6984       if (mode != VOIDmode)
6985         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
6986       else
6987         *total = COSTS_N_INSNS (4); /* How knows?  */
6988       return false;
6989     }
6990 }
6991
6992 /* RTX costs when optimizing for size.  */
6993 static bool
6994 arm_rtx_costs (rtx x, int code, int outer_code, int *total,
6995                bool speed)
6996 {
6997   if (!speed)
6998     return arm_size_rtx_costs (x, (enum rtx_code) code,
6999                                (enum rtx_code) outer_code, total);
7000   else
7001     return all_cores[(int)arm_tune].rtx_costs (x, (enum rtx_code) code,
7002                                                (enum rtx_code) outer_code,
7003                                                total, speed);
7004 }
7005
7006 /* RTX costs for cores with a slow MUL implementation.  Thumb-2 is not
7007    supported on any "slowmul" cores, so it can be ignored.  */
7008
7009 static bool
7010 arm_slowmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7011                        int *total, bool speed)
7012 {
7013   enum machine_mode mode = GET_MODE (x);
7014
7015   if (TARGET_THUMB)
7016     {
7017       *total = thumb1_rtx_costs (x, code, outer_code);
7018       return true;
7019     }
7020
7021   switch (code)
7022     {
7023     case MULT:
7024       if (GET_MODE_CLASS (mode) == MODE_FLOAT
7025           || mode == DImode)
7026         {
7027           *total = COSTS_N_INSNS (20);
7028           return false;
7029         }
7030
7031       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7032         {
7033           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
7034                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
7035           int cost, const_ok = const_ok_for_arm (i);
7036           int j, booth_unit_size;
7037
7038           /* Tune as appropriate.  */
7039           cost = const_ok ? 4 : 8;
7040           booth_unit_size = 2;
7041           for (j = 0; i && j < 32; j += booth_unit_size)
7042             {
7043               i >>= booth_unit_size;
7044               cost++;
7045             }
7046
7047           *total = COSTS_N_INSNS (cost);
7048           *total += rtx_cost (XEXP (x, 0), code, speed);
7049           return true;
7050         }
7051
7052       *total = COSTS_N_INSNS (20);
7053       return false;
7054
7055     default:
7056       return arm_rtx_costs_1 (x, outer_code, total, speed);;
7057     }
7058 }
7059
7060
7061 /* RTX cost for cores with a fast multiply unit (M variants).  */
7062
7063 static bool
7064 arm_fastmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7065                        int *total, bool speed)
7066 {
7067   enum machine_mode mode = GET_MODE (x);
7068
7069   if (TARGET_THUMB1)
7070     {
7071       *total = thumb1_rtx_costs (x, code, outer_code);
7072       return true;
7073     }
7074
7075   /* ??? should thumb2 use different costs?  */
7076   switch (code)
7077     {
7078     case MULT:
7079       /* There is no point basing this on the tuning, since it is always the
7080          fast variant if it exists at all.  */
7081       if (mode == DImode
7082           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
7083           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7084               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7085         {
7086           *total = COSTS_N_INSNS(2);
7087           return false;
7088         }
7089
7090
7091       if (mode == DImode)
7092         {
7093           *total = COSTS_N_INSNS (5);
7094           return false;
7095         }
7096
7097       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7098         {
7099           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
7100                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
7101           int cost, const_ok = const_ok_for_arm (i);
7102           int j, booth_unit_size;
7103
7104           /* Tune as appropriate.  */
7105           cost = const_ok ? 4 : 8;
7106           booth_unit_size = 8;
7107           for (j = 0; i && j < 32; j += booth_unit_size)
7108             {
7109               i >>= booth_unit_size;
7110               cost++;
7111             }
7112
7113           *total = COSTS_N_INSNS(cost);
7114           return false;
7115         }
7116
7117       if (mode == SImode)
7118         {
7119           *total = COSTS_N_INSNS (4);
7120           return false;
7121         }
7122
7123       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7124         {
7125           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
7126             {
7127               *total = COSTS_N_INSNS (1);
7128               return false;
7129             }
7130         }
7131
7132       /* Requires a lib call */
7133       *total = COSTS_N_INSNS (20);
7134       return false;
7135
7136     default:
7137       return arm_rtx_costs_1 (x, outer_code, total, speed);
7138     }
7139 }
7140
7141
7142 /* RTX cost for XScale CPUs.  Thumb-2 is not supported on any xscale cores,
7143    so it can be ignored.  */
7144
7145 static bool
7146 arm_xscale_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code, int *total, bool speed)
7147 {
7148   enum machine_mode mode = GET_MODE (x);
7149
7150   if (TARGET_THUMB)
7151     {
7152       *total = thumb1_rtx_costs (x, code, outer_code);
7153       return true;
7154     }
7155
7156   switch (code)
7157     {
7158     case COMPARE:
7159       if (GET_CODE (XEXP (x, 0)) != MULT)
7160         return arm_rtx_costs_1 (x, outer_code, total, speed);
7161
7162       /* A COMPARE of a MULT is slow on XScale; the muls instruction
7163          will stall until the multiplication is complete.  */
7164       *total = COSTS_N_INSNS (3);
7165       return false;
7166
7167     case MULT:
7168       /* There is no point basing this on the tuning, since it is always the
7169          fast variant if it exists at all.  */
7170       if (mode == DImode
7171           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
7172           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7173               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7174         {
7175           *total = COSTS_N_INSNS (2);
7176           return false;
7177         }
7178
7179
7180       if (mode == DImode)
7181         {
7182           *total = COSTS_N_INSNS (5);
7183           return false;
7184         }
7185
7186       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7187         {
7188           /* If operand 1 is a constant we can more accurately
7189              calculate the cost of the multiply.  The multiplier can
7190              retire 15 bits on the first cycle and a further 12 on the
7191              second.  We do, of course, have to load the constant into
7192              a register first.  */
7193           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
7194           /* There's a general overhead of one cycle.  */
7195           int cost = 1;
7196           unsigned HOST_WIDE_INT masked_const;
7197
7198           if (i & 0x80000000)
7199             i = ~i;
7200
7201           i &= (unsigned HOST_WIDE_INT) 0xffffffff;
7202
7203           masked_const = i & 0xffff8000;
7204           if (masked_const != 0)
7205             {
7206               cost++;
7207               masked_const = i & 0xf8000000;
7208               if (masked_const != 0)
7209                 cost++;
7210             }
7211           *total = COSTS_N_INSNS (cost);
7212           return false;
7213         }
7214
7215       if (mode == SImode)
7216         {
7217           *total = COSTS_N_INSNS (3);
7218           return false;
7219         }
7220
7221       /* Requires a lib call */
7222       *total = COSTS_N_INSNS (20);
7223       return false;
7224
7225     default:
7226       return arm_rtx_costs_1 (x, outer_code, total, speed);
7227     }
7228 }
7229
7230
7231 /* RTX costs for 9e (and later) cores.  */
7232
7233 static bool
7234 arm_9e_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7235                   int *total, bool speed)
7236 {
7237   enum machine_mode mode = GET_MODE (x);
7238
7239   if (TARGET_THUMB1)
7240     {
7241       switch (code)
7242         {
7243         case MULT:
7244           *total = COSTS_N_INSNS (3);
7245           return true;
7246
7247         default:
7248           *total = thumb1_rtx_costs (x, code, outer_code);
7249           return true;
7250         }
7251     }
7252
7253   switch (code)
7254     {
7255     case MULT:
7256       /* There is no point basing this on the tuning, since it is always the
7257          fast variant if it exists at all.  */
7258       if (mode == DImode
7259           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
7260           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7261               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7262         {
7263           *total = COSTS_N_INSNS (2);
7264           return false;
7265         }
7266
7267
7268       if (mode == DImode)
7269         {
7270           *total = COSTS_N_INSNS (5);
7271           return false;
7272         }
7273
7274       if (mode == SImode)
7275         {
7276           *total = COSTS_N_INSNS (2);
7277           return false;
7278         }
7279
7280       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7281         {
7282           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
7283             {
7284               *total = COSTS_N_INSNS (1);
7285               return false;
7286             }
7287         }
7288
7289       *total = COSTS_N_INSNS (20);
7290       return false;
7291
7292     default:
7293       return arm_rtx_costs_1 (x, outer_code, total, speed);
7294     }
7295 }
7296 /* All address computations that can be done are free, but rtx cost returns
7297    the same for practically all of them.  So we weight the different types
7298    of address here in the order (most pref first):
7299    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
7300 static inline int
7301 arm_arm_address_cost (rtx x)
7302 {
7303   enum rtx_code c  = GET_CODE (x);
7304
7305   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
7306     return 0;
7307   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
7308     return 10;
7309
7310   if (c == PLUS)
7311     {
7312       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7313         return 2;
7314
7315       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
7316         return 3;
7317
7318       return 4;
7319     }
7320
7321   return 6;
7322 }
7323
7324 static inline int
7325 arm_thumb_address_cost (rtx x)
7326 {
7327   enum rtx_code c  = GET_CODE (x);
7328
7329   if (c == REG)
7330     return 1;
7331   if (c == PLUS
7332       && GET_CODE (XEXP (x, 0)) == REG
7333       && GET_CODE (XEXP (x, 1)) == CONST_INT)
7334     return 1;
7335
7336   return 2;
7337 }
7338
7339 static int
7340 arm_address_cost (rtx x, bool speed ATTRIBUTE_UNUSED)
7341 {
7342   return TARGET_32BIT ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
7343 }
7344
7345 static int
7346 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
7347 {
7348   rtx i_pat, d_pat;
7349
7350   /* Some true dependencies can have a higher cost depending
7351      on precisely how certain input operands are used.  */
7352   if (arm_tune_xscale
7353       && REG_NOTE_KIND (link) == 0
7354       && recog_memoized (insn) >= 0
7355       && recog_memoized (dep) >= 0)
7356     {
7357       int shift_opnum = get_attr_shift (insn);
7358       enum attr_type attr_type = get_attr_type (dep);
7359
7360       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
7361          operand for INSN.  If we have a shifted input operand and the
7362          instruction we depend on is another ALU instruction, then we may
7363          have to account for an additional stall.  */
7364       if (shift_opnum != 0
7365           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
7366         {
7367           rtx shifted_operand;
7368           int opno;
7369
7370           /* Get the shifted operand.  */
7371           extract_insn (insn);
7372           shifted_operand = recog_data.operand[shift_opnum];
7373
7374           /* Iterate over all the operands in DEP.  If we write an operand
7375              that overlaps with SHIFTED_OPERAND, then we have increase the
7376              cost of this dependency.  */
7377           extract_insn (dep);
7378           preprocess_constraints ();
7379           for (opno = 0; opno < recog_data.n_operands; opno++)
7380             {
7381               /* We can ignore strict inputs.  */
7382               if (recog_data.operand_type[opno] == OP_IN)
7383                 continue;
7384
7385               if (reg_overlap_mentioned_p (recog_data.operand[opno],
7386                                            shifted_operand))
7387                 return 2;
7388             }
7389         }
7390     }
7391
7392   /* XXX This is not strictly true for the FPA.  */
7393   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
7394       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
7395     return 0;
7396
7397   /* Call insns don't incur a stall, even if they follow a load.  */
7398   if (REG_NOTE_KIND (link) == 0
7399       && GET_CODE (insn) == CALL_INSN)
7400     return 1;
7401
7402   if ((i_pat = single_set (insn)) != NULL
7403       && GET_CODE (SET_SRC (i_pat)) == MEM
7404       && (d_pat = single_set (dep)) != NULL
7405       && GET_CODE (SET_DEST (d_pat)) == MEM)
7406     {
7407       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
7408       /* This is a load after a store, there is no conflict if the load reads
7409          from a cached area.  Assume that loads from the stack, and from the
7410          constant pool are cached, and that others will miss.  This is a
7411          hack.  */
7412
7413       if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
7414           || reg_mentioned_p (stack_pointer_rtx, src_mem)
7415           || reg_mentioned_p (frame_pointer_rtx, src_mem)
7416           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
7417         return 1;
7418     }
7419
7420   return cost;
7421 }
7422
7423 static int fp_consts_inited = 0;
7424
7425 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
7426 static const char * const strings_fp[8] =
7427 {
7428   "0",   "1",   "2",   "3",
7429   "4",   "5",   "0.5", "10"
7430 };
7431
7432 static REAL_VALUE_TYPE values_fp[8];
7433
7434 static void
7435 init_fp_table (void)
7436 {
7437   int i;
7438   REAL_VALUE_TYPE r;
7439
7440   if (TARGET_VFP)
7441     fp_consts_inited = 1;
7442   else
7443     fp_consts_inited = 8;
7444
7445   for (i = 0; i < fp_consts_inited; i++)
7446     {
7447       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
7448       values_fp[i] = r;
7449     }
7450 }
7451
7452 /* Return TRUE if rtx X is a valid immediate FP constant.  */
7453 int
7454 arm_const_double_rtx (rtx x)
7455 {
7456   REAL_VALUE_TYPE r;
7457   int i;
7458
7459   if (!fp_consts_inited)
7460     init_fp_table ();
7461
7462   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7463   if (REAL_VALUE_MINUS_ZERO (r))
7464     return 0;
7465
7466   for (i = 0; i < fp_consts_inited; i++)
7467     if (REAL_VALUES_EQUAL (r, values_fp[i]))
7468       return 1;
7469
7470   return 0;
7471 }
7472
7473 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
7474 int
7475 neg_const_double_rtx_ok_for_fpa (rtx x)
7476 {
7477   REAL_VALUE_TYPE r;
7478   int i;
7479
7480   if (!fp_consts_inited)
7481     init_fp_table ();
7482
7483   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7484   r = REAL_VALUE_NEGATE (r);
7485   if (REAL_VALUE_MINUS_ZERO (r))
7486     return 0;
7487
7488   for (i = 0; i < 8; i++)
7489     if (REAL_VALUES_EQUAL (r, values_fp[i]))
7490       return 1;
7491
7492   return 0;
7493 }
7494
7495
7496 /* VFPv3 has a fairly wide range of representable immediates, formed from
7497    "quarter-precision" floating-point values. These can be evaluated using this
7498    formula (with ^ for exponentiation):
7499
7500      -1^s * n * 2^-r
7501
7502    Where 's' is a sign bit (0/1), 'n' and 'r' are integers such that
7503    16 <= n <= 31 and 0 <= r <= 7.
7504
7505    These values are mapped onto an 8-bit integer ABCDEFGH s.t.
7506
7507      - A (most-significant) is the sign bit.
7508      - BCD are the exponent (encoded as r XOR 3).
7509      - EFGH are the mantissa (encoded as n - 16).
7510 */
7511
7512 /* Return an integer index for a VFPv3 immediate operand X suitable for the
7513    fconst[sd] instruction, or -1 if X isn't suitable.  */
7514 static int
7515 vfp3_const_double_index (rtx x)
7516 {
7517   REAL_VALUE_TYPE r, m;
7518   int sign, exponent;
7519   unsigned HOST_WIDE_INT mantissa, mant_hi;
7520   unsigned HOST_WIDE_INT mask;
7521   HOST_WIDE_INT m1, m2;
7522   int point_pos = 2 * HOST_BITS_PER_WIDE_INT - 1;
7523
7524   if (!TARGET_VFP3 || GET_CODE (x) != CONST_DOUBLE)
7525     return -1;
7526
7527   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7528
7529   /* We can't represent these things, so detect them first.  */
7530   if (REAL_VALUE_ISINF (r) || REAL_VALUE_ISNAN (r) || REAL_VALUE_MINUS_ZERO (r))
7531     return -1;
7532
7533   /* Extract sign, exponent and mantissa.  */
7534   sign = REAL_VALUE_NEGATIVE (r) ? 1 : 0;
7535   r = REAL_VALUE_ABS (r);
7536   exponent = REAL_EXP (&r);
7537   /* For the mantissa, we expand into two HOST_WIDE_INTS, apart from the
7538      highest (sign) bit, with a fixed binary point at bit point_pos.
7539      WARNING: If there's ever a VFP version which uses more than 2 * H_W_I - 1
7540      bits for the mantissa, this may fail (low bits would be lost).  */
7541   real_ldexp (&m, &r, point_pos - exponent);
7542   REAL_VALUE_TO_INT (&m1, &m2, m);
7543   mantissa = m1;
7544   mant_hi = m2;
7545
7546   /* If there are bits set in the low part of the mantissa, we can't
7547      represent this value.  */
7548   if (mantissa != 0)
7549     return -1;
7550
7551   /* Now make it so that mantissa contains the most-significant bits, and move
7552      the point_pos to indicate that the least-significant bits have been
7553      discarded.  */
7554   point_pos -= HOST_BITS_PER_WIDE_INT;
7555   mantissa = mant_hi;
7556
7557   /* We can permit four significant bits of mantissa only, plus a high bit
7558      which is always 1.  */
7559   mask = ((unsigned HOST_WIDE_INT)1 << (point_pos - 5)) - 1;
7560   if ((mantissa & mask) != 0)
7561     return -1;
7562
7563   /* Now we know the mantissa is in range, chop off the unneeded bits.  */
7564   mantissa >>= point_pos - 5;
7565
7566   /* The mantissa may be zero. Disallow that case. (It's possible to load the
7567      floating-point immediate zero with Neon using an integer-zero load, but
7568      that case is handled elsewhere.)  */
7569   if (mantissa == 0)
7570     return -1;
7571
7572   gcc_assert (mantissa >= 16 && mantissa <= 31);
7573
7574   /* The value of 5 here would be 4 if GCC used IEEE754-like encoding (where
7575      normalized significands are in the range [1, 2). (Our mantissa is shifted
7576      left 4 places at this point relative to normalized IEEE754 values).  GCC
7577      internally uses [0.5, 1) (see real.c), so the exponent returned from
7578      REAL_EXP must be altered.  */
7579   exponent = 5 - exponent;
7580
7581   if (exponent < 0 || exponent > 7)
7582     return -1;
7583
7584   /* Sign, mantissa and exponent are now in the correct form to plug into the
7585      formula described in the comment above.  */
7586   return (sign << 7) | ((exponent ^ 3) << 4) | (mantissa - 16);
7587 }
7588
7589 /* Return TRUE if rtx X is a valid immediate VFPv3 constant.  */
7590 int
7591 vfp3_const_double_rtx (rtx x)
7592 {
7593   if (!TARGET_VFP3)
7594     return 0;
7595
7596   return vfp3_const_double_index (x) != -1;
7597 }
7598
7599 /* Recognize immediates which can be used in various Neon instructions. Legal
7600    immediates are described by the following table (for VMVN variants, the
7601    bitwise inverse of the constant shown is recognized. In either case, VMOV
7602    is output and the correct instruction to use for a given constant is chosen
7603    by the assembler). The constant shown is replicated across all elements of
7604    the destination vector.
7605
7606    insn elems variant constant (binary)
7607    ---- ----- ------- -----------------
7608    vmov  i32     0    00000000 00000000 00000000 abcdefgh
7609    vmov  i32     1    00000000 00000000 abcdefgh 00000000
7610    vmov  i32     2    00000000 abcdefgh 00000000 00000000
7611    vmov  i32     3    abcdefgh 00000000 00000000 00000000
7612    vmov  i16     4    00000000 abcdefgh
7613    vmov  i16     5    abcdefgh 00000000
7614    vmvn  i32     6    00000000 00000000 00000000 abcdefgh
7615    vmvn  i32     7    00000000 00000000 abcdefgh 00000000
7616    vmvn  i32     8    00000000 abcdefgh 00000000 00000000
7617    vmvn  i32     9    abcdefgh 00000000 00000000 00000000
7618    vmvn  i16    10    00000000 abcdefgh
7619    vmvn  i16    11    abcdefgh 00000000
7620    vmov  i32    12    00000000 00000000 abcdefgh 11111111
7621    vmvn  i32    13    00000000 00000000 abcdefgh 11111111
7622    vmov  i32    14    00000000 abcdefgh 11111111 11111111
7623    vmvn  i32    15    00000000 abcdefgh 11111111 11111111
7624    vmov   i8    16    abcdefgh
7625    vmov  i64    17    aaaaaaaa bbbbbbbb cccccccc dddddddd
7626                       eeeeeeee ffffffff gggggggg hhhhhhhh
7627    vmov  f32    18    aBbbbbbc defgh000 00000000 00000000
7628
7629    For case 18, B = !b. Representable values are exactly those accepted by
7630    vfp3_const_double_index, but are output as floating-point numbers rather
7631    than indices.
7632
7633    Variants 0-5 (inclusive) may also be used as immediates for the second
7634    operand of VORR/VBIC instructions.
7635
7636    The INVERSE argument causes the bitwise inverse of the given operand to be
7637    recognized instead (used for recognizing legal immediates for the VAND/VORN
7638    pseudo-instructions). If INVERSE is true, the value placed in *MODCONST is
7639    *not* inverted (i.e. the pseudo-instruction forms vand/vorn should still be
7640    output, rather than the real insns vbic/vorr).
7641
7642    INVERSE makes no difference to the recognition of float vectors.
7643
7644    The return value is the variant of immediate as shown in the above table, or
7645    -1 if the given value doesn't match any of the listed patterns.
7646 */
7647 static int
7648 neon_valid_immediate (rtx op, enum machine_mode mode, int inverse,
7649                       rtx *modconst, int *elementwidth)
7650 {
7651 #define CHECK(STRIDE, ELSIZE, CLASS, TEST)      \
7652   matches = 1;                                  \
7653   for (i = 0; i < idx; i += (STRIDE))           \
7654     if (!(TEST))                                \
7655       matches = 0;                              \
7656   if (matches)                                  \
7657     {                                           \
7658       immtype = (CLASS);                        \
7659       elsize = (ELSIZE);                        \
7660       break;                                    \
7661     }
7662
7663   unsigned int i, elsize = 0, idx = 0, n_elts = CONST_VECTOR_NUNITS (op);
7664   unsigned int innersize = GET_MODE_SIZE (GET_MODE_INNER (mode));
7665   unsigned char bytes[16];
7666   int immtype = -1, matches;
7667   unsigned int invmask = inverse ? 0xff : 0;
7668
7669   /* Vectors of float constants.  */
7670   if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
7671     {
7672       rtx el0 = CONST_VECTOR_ELT (op, 0);
7673       REAL_VALUE_TYPE r0;
7674
7675       if (!vfp3_const_double_rtx (el0))
7676         return -1;
7677
7678       REAL_VALUE_FROM_CONST_DOUBLE (r0, el0);
7679
7680       for (i = 1; i < n_elts; i++)
7681         {
7682           rtx elt = CONST_VECTOR_ELT (op, i);
7683           REAL_VALUE_TYPE re;
7684
7685           REAL_VALUE_FROM_CONST_DOUBLE (re, elt);
7686
7687           if (!REAL_VALUES_EQUAL (r0, re))
7688             return -1;
7689         }
7690
7691       if (modconst)
7692         *modconst = CONST_VECTOR_ELT (op, 0);
7693
7694       if (elementwidth)
7695         *elementwidth = 0;
7696
7697       return 18;
7698     }
7699
7700   /* Splat vector constant out into a byte vector.  */
7701   for (i = 0; i < n_elts; i++)
7702     {
7703       rtx el = CONST_VECTOR_ELT (op, i);
7704       unsigned HOST_WIDE_INT elpart;
7705       unsigned int part, parts;
7706
7707       if (GET_CODE (el) == CONST_INT)
7708         {
7709           elpart = INTVAL (el);
7710           parts = 1;
7711         }
7712       else if (GET_CODE (el) == CONST_DOUBLE)
7713         {
7714           elpart = CONST_DOUBLE_LOW (el);
7715           parts = 2;
7716         }
7717       else
7718         gcc_unreachable ();
7719
7720       for (part = 0; part < parts; part++)
7721         {
7722           unsigned int byte;
7723           for (byte = 0; byte < innersize; byte++)
7724             {
7725               bytes[idx++] = (elpart & 0xff) ^ invmask;
7726               elpart >>= BITS_PER_UNIT;
7727             }
7728           if (GET_CODE (el) == CONST_DOUBLE)
7729             elpart = CONST_DOUBLE_HIGH (el);
7730         }
7731     }
7732
7733   /* Sanity check.  */
7734   gcc_assert (idx == GET_MODE_SIZE (mode));
7735
7736   do
7737     {
7738       CHECK (4, 32, 0, bytes[i] == bytes[0] && bytes[i + 1] == 0
7739                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
7740
7741       CHECK (4, 32, 1, bytes[i] == 0 && bytes[i + 1] == bytes[1]
7742                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
7743
7744       CHECK (4, 32, 2, bytes[i] == 0 && bytes[i + 1] == 0
7745                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
7746
7747       CHECK (4, 32, 3, bytes[i] == 0 && bytes[i + 1] == 0
7748                        && bytes[i + 2] == 0 && bytes[i + 3] == bytes[3]);
7749
7750       CHECK (2, 16, 4, bytes[i] == bytes[0] && bytes[i + 1] == 0);
7751
7752       CHECK (2, 16, 5, bytes[i] == 0 && bytes[i + 1] == bytes[1]);
7753
7754       CHECK (4, 32, 6, bytes[i] == bytes[0] && bytes[i + 1] == 0xff
7755                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
7756
7757       CHECK (4, 32, 7, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
7758                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
7759
7760       CHECK (4, 32, 8, bytes[i] == 0xff && bytes[i + 1] == 0xff
7761                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
7762
7763       CHECK (4, 32, 9, bytes[i] == 0xff && bytes[i + 1] == 0xff
7764                        && bytes[i + 2] == 0xff && bytes[i + 3] == bytes[3]);
7765
7766       CHECK (2, 16, 10, bytes[i] == bytes[0] && bytes[i + 1] == 0xff);
7767
7768       CHECK (2, 16, 11, bytes[i] == 0xff && bytes[i + 1] == bytes[1]);
7769
7770       CHECK (4, 32, 12, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
7771                         && bytes[i + 2] == 0 && bytes[i + 3] == 0);
7772
7773       CHECK (4, 32, 13, bytes[i] == 0 && bytes[i + 1] == bytes[1]
7774                         && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
7775
7776       CHECK (4, 32, 14, bytes[i] == 0xff && bytes[i + 1] == 0xff
7777                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
7778
7779       CHECK (4, 32, 15, bytes[i] == 0 && bytes[i + 1] == 0
7780                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
7781
7782       CHECK (1, 8, 16, bytes[i] == bytes[0]);
7783
7784       CHECK (1, 64, 17, (bytes[i] == 0 || bytes[i] == 0xff)
7785                         && bytes[i] == bytes[(i + 8) % idx]);
7786     }
7787   while (0);
7788
7789   if (immtype == -1)
7790     return -1;
7791
7792   if (elementwidth)
7793     *elementwidth = elsize;
7794
7795   if (modconst)
7796     {
7797       unsigned HOST_WIDE_INT imm = 0;
7798
7799       /* Un-invert bytes of recognized vector, if necessary.  */
7800       if (invmask != 0)
7801         for (i = 0; i < idx; i++)
7802           bytes[i] ^= invmask;
7803
7804       if (immtype == 17)
7805         {
7806           /* FIXME: Broken on 32-bit H_W_I hosts.  */
7807           gcc_assert (sizeof (HOST_WIDE_INT) == 8);
7808
7809           for (i = 0; i < 8; i++)
7810             imm |= (unsigned HOST_WIDE_INT) (bytes[i] ? 0xff : 0)
7811                    << (i * BITS_PER_UNIT);
7812
7813           *modconst = GEN_INT (imm);
7814         }
7815       else
7816         {
7817           unsigned HOST_WIDE_INT imm = 0;
7818
7819           for (i = 0; i < elsize / BITS_PER_UNIT; i++)
7820             imm |= (unsigned HOST_WIDE_INT) bytes[i] << (i * BITS_PER_UNIT);
7821
7822           *modconst = GEN_INT (imm);
7823         }
7824     }
7825
7826   return immtype;
7827 #undef CHECK
7828 }
7829
7830 /* Return TRUE if rtx X is legal for use as either a Neon VMOV (or, implicitly,
7831    VMVN) immediate. Write back width per element to *ELEMENTWIDTH (or zero for
7832    float elements), and a modified constant (whatever should be output for a
7833    VMOV) in *MODCONST.  */
7834
7835 int
7836 neon_immediate_valid_for_move (rtx op, enum machine_mode mode,
7837                                rtx *modconst, int *elementwidth)
7838 {
7839   rtx tmpconst;
7840   int tmpwidth;
7841   int retval = neon_valid_immediate (op, mode, 0, &tmpconst, &tmpwidth);
7842
7843   if (retval == -1)
7844     return 0;
7845
7846   if (modconst)
7847     *modconst = tmpconst;
7848
7849   if (elementwidth)
7850     *elementwidth = tmpwidth;
7851
7852   return 1;
7853 }
7854
7855 /* Return TRUE if rtx X is legal for use in a VORR or VBIC instruction.  If
7856    the immediate is valid, write a constant suitable for using as an operand
7857    to VORR/VBIC/VAND/VORN to *MODCONST and the corresponding element width to
7858    *ELEMENTWIDTH. See neon_valid_immediate for description of INVERSE.  */
7859
7860 int
7861 neon_immediate_valid_for_logic (rtx op, enum machine_mode mode, int inverse,
7862                                 rtx *modconst, int *elementwidth)
7863 {
7864   rtx tmpconst;
7865   int tmpwidth;
7866   int retval = neon_valid_immediate (op, mode, inverse, &tmpconst, &tmpwidth);
7867
7868   if (retval < 0 || retval > 5)
7869     return 0;
7870
7871   if (modconst)
7872     *modconst = tmpconst;
7873
7874   if (elementwidth)
7875     *elementwidth = tmpwidth;
7876
7877   return 1;
7878 }
7879
7880 /* Return a string suitable for output of Neon immediate logic operation
7881    MNEM.  */
7882
7883 char *
7884 neon_output_logic_immediate (const char *mnem, rtx *op2, enum machine_mode mode,
7885                              int inverse, int quad)
7886 {
7887   int width, is_valid;
7888   static char templ[40];
7889
7890   is_valid = neon_immediate_valid_for_logic (*op2, mode, inverse, op2, &width);
7891
7892   gcc_assert (is_valid != 0);
7893
7894   if (quad)
7895     sprintf (templ, "%s.i%d\t%%q0, %%2", mnem, width);
7896   else
7897     sprintf (templ, "%s.i%d\t%%P0, %%2", mnem, width);
7898
7899   return templ;
7900 }
7901
7902 /* Output a sequence of pairwise operations to implement a reduction.
7903    NOTE: We do "too much work" here, because pairwise operations work on two
7904    registers-worth of operands in one go. Unfortunately we can't exploit those
7905    extra calculations to do the full operation in fewer steps, I don't think.
7906    Although all vector elements of the result but the first are ignored, we
7907    actually calculate the same result in each of the elements. An alternative
7908    such as initially loading a vector with zero to use as each of the second
7909    operands would use up an additional register and take an extra instruction,
7910    for no particular gain.  */
7911
7912 void
7913 neon_pairwise_reduce (rtx op0, rtx op1, enum machine_mode mode,
7914                       rtx (*reduc) (rtx, rtx, rtx))
7915 {
7916   enum machine_mode inner = GET_MODE_INNER (mode);
7917   unsigned int i, parts = GET_MODE_SIZE (mode) / GET_MODE_SIZE (inner);
7918   rtx tmpsum = op1;
7919
7920   for (i = parts / 2; i >= 1; i /= 2)
7921     {
7922       rtx dest = (i == 1) ? op0 : gen_reg_rtx (mode);
7923       emit_insn (reduc (dest, tmpsum, tmpsum));
7924       tmpsum = dest;
7925     }
7926 }
7927
7928 /* Initialize a vector with non-constant elements.  FIXME: We can do better
7929    than the current implementation (building a vector on the stack and then
7930    loading it) in many cases.  See rs6000.c.  */
7931
7932 void
7933 neon_expand_vector_init (rtx target, rtx vals)
7934 {
7935   enum machine_mode mode = GET_MODE (target);
7936   enum machine_mode inner = GET_MODE_INNER (mode);
7937   unsigned int i, n_elts = GET_MODE_NUNITS (mode);
7938   rtx mem;
7939
7940   gcc_assert (VECTOR_MODE_P (mode));
7941
7942   mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), 0);
7943   for (i = 0; i < n_elts; i++)
7944     emit_move_insn (adjust_address_nv (mem, inner, i * GET_MODE_SIZE (inner)),
7945                    XVECEXP (vals, 0, i));
7946
7947   emit_move_insn (target, mem);
7948 }
7949
7950 /* Ensure OPERAND lies between LOW (inclusive) and HIGH (exclusive).  Raise
7951    ERR if it doesn't.  FIXME: NEON bounds checks occur late in compilation, so
7952    reported source locations are bogus.  */
7953
7954 static void
7955 bounds_check (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high,
7956               const char *err)
7957 {
7958   HOST_WIDE_INT lane;
7959
7960   gcc_assert (GET_CODE (operand) == CONST_INT);
7961
7962   lane = INTVAL (operand);
7963
7964   if (lane < low || lane >= high)
7965     error (err);
7966 }
7967
7968 /* Bounds-check lanes.  */
7969
7970 void
7971 neon_lane_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
7972 {
7973   bounds_check (operand, low, high, "lane out of range");
7974 }
7975
7976 /* Bounds-check constants.  */
7977
7978 void
7979 neon_const_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
7980 {
7981   bounds_check (operand, low, high, "constant out of range");
7982 }
7983
7984 HOST_WIDE_INT
7985 neon_element_bits (enum machine_mode mode)
7986 {
7987   if (mode == DImode)
7988     return GET_MODE_BITSIZE (mode);
7989   else
7990     return GET_MODE_BITSIZE (GET_MODE_INNER (mode));
7991 }
7992
7993 \f
7994 /* Predicates for `match_operand' and `match_operator'.  */
7995
7996 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
7997 int
7998 cirrus_memory_offset (rtx op)
7999 {
8000   /* Reject eliminable registers.  */
8001   if (! (reload_in_progress || reload_completed)
8002       && (   reg_mentioned_p (frame_pointer_rtx, op)
8003           || reg_mentioned_p (arg_pointer_rtx, op)
8004           || reg_mentioned_p (virtual_incoming_args_rtx, op)
8005           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8006           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8007           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8008     return 0;
8009
8010   if (GET_CODE (op) == MEM)
8011     {
8012       rtx ind;
8013
8014       ind = XEXP (op, 0);
8015
8016       /* Match: (mem (reg)).  */
8017       if (GET_CODE (ind) == REG)
8018         return 1;
8019
8020       /* Match:
8021          (mem (plus (reg)
8022                     (const))).  */
8023       if (GET_CODE (ind) == PLUS
8024           && GET_CODE (XEXP (ind, 0)) == REG
8025           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
8026           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
8027         return 1;
8028     }
8029
8030   return 0;
8031 }
8032
8033 /* Return TRUE if OP is a valid coprocessor memory address pattern.
8034    WB is true if full writeback address modes are allowed and is false
8035    if limited writeback address modes (POST_INC and PRE_DEC) are
8036    allowed.  */
8037
8038 int
8039 arm_coproc_mem_operand (rtx op, bool wb)
8040 {
8041   rtx ind;
8042
8043   /* Reject eliminable registers.  */
8044   if (! (reload_in_progress || reload_completed)
8045       && (   reg_mentioned_p (frame_pointer_rtx, op)
8046           || reg_mentioned_p (arg_pointer_rtx, op)
8047           || reg_mentioned_p (virtual_incoming_args_rtx, op)
8048           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8049           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8050           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8051     return FALSE;
8052
8053   /* Constants are converted into offsets from labels.  */
8054   if (GET_CODE (op) != MEM)
8055     return FALSE;
8056
8057   ind = XEXP (op, 0);
8058
8059   if (reload_completed
8060       && (GET_CODE (ind) == LABEL_REF
8061           || (GET_CODE (ind) == CONST
8062               && GET_CODE (XEXP (ind, 0)) == PLUS
8063               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
8064               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
8065     return TRUE;
8066
8067   /* Match: (mem (reg)).  */
8068   if (GET_CODE (ind) == REG)
8069     return arm_address_register_rtx_p (ind, 0);
8070
8071   /* Autoincremment addressing modes.  POST_INC and PRE_DEC are
8072      acceptable in any case (subject to verification by
8073      arm_address_register_rtx_p).  We need WB to be true to accept
8074      PRE_INC and POST_DEC.  */
8075   if (GET_CODE (ind) == POST_INC
8076       || GET_CODE (ind) == PRE_DEC
8077       || (wb
8078           && (GET_CODE (ind) == PRE_INC
8079               || GET_CODE (ind) == POST_DEC)))
8080     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
8081
8082   if (wb
8083       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
8084       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
8085       && GET_CODE (XEXP (ind, 1)) == PLUS
8086       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
8087     ind = XEXP (ind, 1);
8088
8089   /* Match:
8090      (plus (reg)
8091            (const)).  */
8092   if (GET_CODE (ind) == PLUS
8093       && GET_CODE (XEXP (ind, 0)) == REG
8094       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
8095       && GET_CODE (XEXP (ind, 1)) == CONST_INT
8096       && INTVAL (XEXP (ind, 1)) > -1024
8097       && INTVAL (XEXP (ind, 1)) <  1024
8098       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
8099     return TRUE;
8100
8101   return FALSE;
8102 }
8103
8104 /* Return TRUE if OP is a memory operand which we can load or store a vector
8105    to/from. TYPE is one of the following values:
8106     0 - Vector load/stor (vldr)
8107     1 - Core registers (ldm)
8108     2 - Element/structure loads (vld1)
8109  */
8110 int
8111 neon_vector_mem_operand (rtx op, int type)
8112 {
8113   rtx ind;
8114
8115   /* Reject eliminable registers.  */
8116   if (! (reload_in_progress || reload_completed)
8117       && (   reg_mentioned_p (frame_pointer_rtx, op)
8118           || reg_mentioned_p (arg_pointer_rtx, op)
8119           || reg_mentioned_p (virtual_incoming_args_rtx, op)
8120           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8121           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8122           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8123     return FALSE;
8124
8125   /* Constants are converted into offsets from labels.  */
8126   if (GET_CODE (op) != MEM)
8127     return FALSE;
8128
8129   ind = XEXP (op, 0);
8130
8131   if (reload_completed
8132       && (GET_CODE (ind) == LABEL_REF
8133           || (GET_CODE (ind) == CONST
8134               && GET_CODE (XEXP (ind, 0)) == PLUS
8135               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
8136               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
8137     return TRUE;
8138
8139   /* Match: (mem (reg)).  */
8140   if (GET_CODE (ind) == REG)
8141     return arm_address_register_rtx_p (ind, 0);
8142
8143   /* Allow post-increment with Neon registers.  */
8144   if (type != 1 && (GET_CODE (ind) == POST_INC || GET_CODE (ind) == PRE_DEC))
8145     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
8146
8147   /* FIXME: vld1 allows register post-modify.  */
8148
8149   /* Match:
8150      (plus (reg)
8151           (const)).  */
8152   if (type == 0
8153       && GET_CODE (ind) == PLUS
8154       && GET_CODE (XEXP (ind, 0)) == REG
8155       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
8156       && GET_CODE (XEXP (ind, 1)) == CONST_INT
8157       && INTVAL (XEXP (ind, 1)) > -1024
8158       && INTVAL (XEXP (ind, 1)) < 1016
8159       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
8160     return TRUE;
8161
8162   return FALSE;
8163 }
8164
8165 /* Return TRUE if OP is a mem suitable for loading/storing a Neon struct
8166    type.  */
8167 int
8168 neon_struct_mem_operand (rtx op)
8169 {
8170   rtx ind;
8171
8172   /* Reject eliminable registers.  */
8173   if (! (reload_in_progress || reload_completed)
8174       && (   reg_mentioned_p (frame_pointer_rtx, op)
8175           || reg_mentioned_p (arg_pointer_rtx, op)
8176           || reg_mentioned_p (virtual_incoming_args_rtx, op)
8177           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
8178           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
8179           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
8180     return FALSE;
8181
8182   /* Constants are converted into offsets from labels.  */
8183   if (GET_CODE (op) != MEM)
8184     return FALSE;
8185
8186   ind = XEXP (op, 0);
8187
8188   if (reload_completed
8189       && (GET_CODE (ind) == LABEL_REF
8190           || (GET_CODE (ind) == CONST
8191               && GET_CODE (XEXP (ind, 0)) == PLUS
8192               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
8193               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
8194     return TRUE;
8195
8196   /* Match: (mem (reg)).  */
8197   if (GET_CODE (ind) == REG)
8198     return arm_address_register_rtx_p (ind, 0);
8199
8200   return FALSE;
8201 }
8202
8203 /* Return true if X is a register that will be eliminated later on.  */
8204 int
8205 arm_eliminable_register (rtx x)
8206 {
8207   return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
8208                        || REGNO (x) == ARG_POINTER_REGNUM
8209                        || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
8210                            && REGNO (x) <= LAST_VIRTUAL_REGISTER));
8211 }
8212
8213 /* Return GENERAL_REGS if a scratch register required to reload x to/from
8214    coprocessor registers.  Otherwise return NO_REGS.  */
8215
8216 enum reg_class
8217 coproc_secondary_reload_class (enum machine_mode mode, rtx x, bool wb)
8218 {
8219   if (mode == HFmode)
8220     {
8221       if (s_register_operand (x, mode) || neon_vector_mem_operand (x, 2))
8222         return NO_REGS;
8223       return GENERAL_REGS;
8224     }
8225
8226   if (TARGET_NEON
8227       && (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
8228           || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
8229       && neon_vector_mem_operand (x, 0))
8230      return NO_REGS;
8231
8232   if (arm_coproc_mem_operand (x, wb) || s_register_operand (x, mode))
8233     return NO_REGS;
8234
8235   return GENERAL_REGS;
8236 }
8237
8238 /* Values which must be returned in the most-significant end of the return
8239    register.  */
8240
8241 static bool
8242 arm_return_in_msb (const_tree valtype)
8243 {
8244   return (TARGET_AAPCS_BASED
8245           && BYTES_BIG_ENDIAN
8246           && (AGGREGATE_TYPE_P (valtype)
8247               || TREE_CODE (valtype) == COMPLEX_TYPE));
8248 }
8249
8250 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
8251    Use by the Cirrus Maverick code which has to workaround
8252    a hardware bug triggered by such instructions.  */
8253 static bool
8254 arm_memory_load_p (rtx insn)
8255 {
8256   rtx body, lhs, rhs;;
8257
8258   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
8259     return false;
8260
8261   body = PATTERN (insn);
8262
8263   if (GET_CODE (body) != SET)
8264     return false;
8265
8266   lhs = XEXP (body, 0);
8267   rhs = XEXP (body, 1);
8268
8269   lhs = REG_OR_SUBREG_RTX (lhs);
8270
8271   /* If the destination is not a general purpose
8272      register we do not have to worry.  */
8273   if (GET_CODE (lhs) != REG
8274       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
8275     return false;
8276
8277   /* As well as loads from memory we also have to react
8278      to loads of invalid constants which will be turned
8279      into loads from the minipool.  */
8280   return (GET_CODE (rhs) == MEM
8281           || GET_CODE (rhs) == SYMBOL_REF
8282           || note_invalid_constants (insn, -1, false));
8283 }
8284
8285 /* Return TRUE if INSN is a Cirrus instruction.  */
8286 static bool
8287 arm_cirrus_insn_p (rtx insn)
8288 {
8289   enum attr_cirrus attr;
8290
8291   /* get_attr cannot accept USE or CLOBBER.  */
8292   if (!insn
8293       || GET_CODE (insn) != INSN
8294       || GET_CODE (PATTERN (insn)) == USE
8295       || GET_CODE (PATTERN (insn)) == CLOBBER)
8296     return 0;
8297
8298   attr = get_attr_cirrus (insn);
8299
8300   return attr != CIRRUS_NOT;
8301 }
8302
8303 /* Cirrus reorg for invalid instruction combinations.  */
8304 static void
8305 cirrus_reorg (rtx first)
8306 {
8307   enum attr_cirrus attr;
8308   rtx body = PATTERN (first);
8309   rtx t;
8310   int nops;
8311
8312   /* Any branch must be followed by 2 non Cirrus instructions.  */
8313   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
8314     {
8315       nops = 0;
8316       t = next_nonnote_insn (first);
8317
8318       if (arm_cirrus_insn_p (t))
8319         ++ nops;
8320
8321       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
8322         ++ nops;
8323
8324       while (nops --)
8325         emit_insn_after (gen_nop (), first);
8326
8327       return;
8328     }
8329
8330   /* (float (blah)) is in parallel with a clobber.  */
8331   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
8332     body = XVECEXP (body, 0, 0);
8333
8334   if (GET_CODE (body) == SET)
8335     {
8336       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
8337
8338       /* cfldrd, cfldr64, cfstrd, cfstr64 must
8339          be followed by a non Cirrus insn.  */
8340       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
8341         {
8342           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
8343             emit_insn_after (gen_nop (), first);
8344
8345           return;
8346         }
8347       else if (arm_memory_load_p (first))
8348         {
8349           unsigned int arm_regno;
8350
8351           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
8352              ldr/cfmv64hr combination where the Rd field is the same
8353              in both instructions must be split with a non Cirrus
8354              insn.  Example:
8355
8356              ldr r0, blah
8357              nop
8358              cfmvsr mvf0, r0.  */
8359
8360           /* Get Arm register number for ldr insn.  */
8361           if (GET_CODE (lhs) == REG)
8362             arm_regno = REGNO (lhs);
8363           else
8364             {
8365               gcc_assert (GET_CODE (rhs) == REG);
8366               arm_regno = REGNO (rhs);
8367             }
8368
8369           /* Next insn.  */
8370           first = next_nonnote_insn (first);
8371
8372           if (! arm_cirrus_insn_p (first))
8373             return;
8374
8375           body = PATTERN (first);
8376
8377           /* (float (blah)) is in parallel with a clobber.  */
8378           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
8379             body = XVECEXP (body, 0, 0);
8380
8381           if (GET_CODE (body) == FLOAT)
8382             body = XEXP (body, 0);
8383
8384           if (get_attr_cirrus (first) == CIRRUS_MOVE
8385               && GET_CODE (XEXP (body, 1)) == REG
8386               && arm_regno == REGNO (XEXP (body, 1)))
8387             emit_insn_after (gen_nop (), first);
8388
8389           return;
8390         }
8391     }
8392
8393   /* get_attr cannot accept USE or CLOBBER.  */
8394   if (!first
8395       || GET_CODE (first) != INSN
8396       || GET_CODE (PATTERN (first)) == USE
8397       || GET_CODE (PATTERN (first)) == CLOBBER)
8398     return;
8399
8400   attr = get_attr_cirrus (first);
8401
8402   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
8403      must be followed by a non-coprocessor instruction.  */
8404   if (attr == CIRRUS_COMPARE)
8405     {
8406       nops = 0;
8407
8408       t = next_nonnote_insn (first);
8409
8410       if (arm_cirrus_insn_p (t))
8411         ++ nops;
8412
8413       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
8414         ++ nops;
8415
8416       while (nops --)
8417         emit_insn_after (gen_nop (), first);
8418
8419       return;
8420     }
8421 }
8422
8423 /* Return TRUE if X references a SYMBOL_REF.  */
8424 int
8425 symbol_mentioned_p (rtx x)
8426 {
8427   const char * fmt;
8428   int i;
8429
8430   if (GET_CODE (x) == SYMBOL_REF)
8431     return 1;
8432
8433   /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
8434      are constant offsets, not symbols.  */
8435   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
8436     return 0;
8437
8438   fmt = GET_RTX_FORMAT (GET_CODE (x));
8439
8440   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
8441     {
8442       if (fmt[i] == 'E')
8443         {
8444           int j;
8445
8446           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8447             if (symbol_mentioned_p (XVECEXP (x, i, j)))
8448               return 1;
8449         }
8450       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
8451         return 1;
8452     }
8453
8454   return 0;
8455 }
8456
8457 /* Return TRUE if X references a LABEL_REF.  */
8458 int
8459 label_mentioned_p (rtx x)
8460 {
8461   const char * fmt;
8462   int i;
8463
8464   if (GET_CODE (x) == LABEL_REF)
8465     return 1;
8466
8467   /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
8468      instruction, but they are constant offsets, not symbols.  */
8469   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
8470     return 0;
8471
8472   fmt = GET_RTX_FORMAT (GET_CODE (x));
8473   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
8474     {
8475       if (fmt[i] == 'E')
8476         {
8477           int j;
8478
8479           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8480             if (label_mentioned_p (XVECEXP (x, i, j)))
8481               return 1;
8482         }
8483       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
8484         return 1;
8485     }
8486
8487   return 0;
8488 }
8489
8490 int
8491 tls_mentioned_p (rtx x)
8492 {
8493   switch (GET_CODE (x))
8494     {
8495     case CONST:
8496       return tls_mentioned_p (XEXP (x, 0));
8497
8498     case UNSPEC:
8499       if (XINT (x, 1) == UNSPEC_TLS)
8500         return 1;
8501
8502     default:
8503       return 0;
8504     }
8505 }
8506
8507 /* Must not copy a SET whose source operand is PC-relative.  */
8508
8509 static bool
8510 arm_cannot_copy_insn_p (rtx insn)
8511 {
8512   rtx pat = PATTERN (insn);
8513
8514   if (GET_CODE (pat) == SET)
8515     {
8516       rtx rhs = SET_SRC (pat);
8517
8518       if (GET_CODE (rhs) == UNSPEC
8519           && XINT (rhs, 1) == UNSPEC_PIC_BASE)
8520         return TRUE;
8521
8522       if (GET_CODE (rhs) == MEM
8523           && GET_CODE (XEXP (rhs, 0)) == UNSPEC
8524           && XINT (XEXP (rhs, 0), 1) == UNSPEC_PIC_BASE)
8525         return TRUE;
8526     }
8527
8528   return FALSE;
8529 }
8530
8531 enum rtx_code
8532 minmax_code (rtx x)
8533 {
8534   enum rtx_code code = GET_CODE (x);
8535
8536   switch (code)
8537     {
8538     case SMAX:
8539       return GE;
8540     case SMIN:
8541       return LE;
8542     case UMIN:
8543       return LEU;
8544     case UMAX:
8545       return GEU;
8546     default:
8547       gcc_unreachable ();
8548     }
8549 }
8550
8551 /* Return 1 if memory locations are adjacent.  */
8552 int
8553 adjacent_mem_locations (rtx a, rtx b)
8554 {
8555   /* We don't guarantee to preserve the order of these memory refs.  */
8556   if (volatile_refs_p (a) || volatile_refs_p (b))
8557     return 0;
8558
8559   if ((GET_CODE (XEXP (a, 0)) == REG
8560        || (GET_CODE (XEXP (a, 0)) == PLUS
8561            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
8562       && (GET_CODE (XEXP (b, 0)) == REG
8563           || (GET_CODE (XEXP (b, 0)) == PLUS
8564               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
8565     {
8566       HOST_WIDE_INT val0 = 0, val1 = 0;
8567       rtx reg0, reg1;
8568       int val_diff;
8569
8570       if (GET_CODE (XEXP (a, 0)) == PLUS)
8571         {
8572           reg0 = XEXP (XEXP (a, 0), 0);
8573           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
8574         }
8575       else
8576         reg0 = XEXP (a, 0);
8577
8578       if (GET_CODE (XEXP (b, 0)) == PLUS)
8579         {
8580           reg1 = XEXP (XEXP (b, 0), 0);
8581           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
8582         }
8583       else
8584         reg1 = XEXP (b, 0);
8585
8586       /* Don't accept any offset that will require multiple
8587          instructions to handle, since this would cause the
8588          arith_adjacentmem pattern to output an overlong sequence.  */
8589       if (!const_ok_for_op (val0, PLUS) || !const_ok_for_op (val1, PLUS))
8590         return 0;
8591
8592       /* Don't allow an eliminable register: register elimination can make
8593          the offset too large.  */
8594       if (arm_eliminable_register (reg0))
8595         return 0;
8596
8597       val_diff = val1 - val0;
8598
8599       if (arm_ld_sched)
8600         {
8601           /* If the target has load delay slots, then there's no benefit
8602              to using an ldm instruction unless the offset is zero and
8603              we are optimizing for size.  */
8604           return (optimize_size && (REGNO (reg0) == REGNO (reg1))
8605                   && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
8606                   && (val_diff == 4 || val_diff == -4));
8607         }
8608
8609       return ((REGNO (reg0) == REGNO (reg1))
8610               && (val_diff == 4 || val_diff == -4));
8611     }
8612
8613   return 0;
8614 }
8615
8616 int
8617 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
8618                         HOST_WIDE_INT *load_offset)
8619 {
8620   int unsorted_regs[4];
8621   HOST_WIDE_INT unsorted_offsets[4];
8622   int order[4];
8623   int base_reg = -1;
8624   int i;
8625
8626   /* Can only handle 2, 3, or 4 insns at present,
8627      though could be easily extended if required.  */
8628   gcc_assert (nops >= 2 && nops <= 4);
8629
8630   memset (order, 0, 4 * sizeof (int));
8631
8632   /* Loop over the operands and check that the memory references are
8633      suitable (i.e. immediate offsets from the same base register).  At
8634      the same time, extract the target register, and the memory
8635      offsets.  */
8636   for (i = 0; i < nops; i++)
8637     {
8638       rtx reg;
8639       rtx offset;
8640
8641       /* Convert a subreg of a mem into the mem itself.  */
8642       if (GET_CODE (operands[nops + i]) == SUBREG)
8643         operands[nops + i] = alter_subreg (operands + (nops + i));
8644
8645       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
8646
8647       /* Don't reorder volatile memory references; it doesn't seem worth
8648          looking for the case where the order is ok anyway.  */
8649       if (MEM_VOLATILE_P (operands[nops + i]))
8650         return 0;
8651
8652       offset = const0_rtx;
8653
8654       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
8655            || (GET_CODE (reg) == SUBREG
8656                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
8657           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
8658               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
8659                    == REG)
8660                   || (GET_CODE (reg) == SUBREG
8661                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
8662               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
8663                   == CONST_INT)))
8664         {
8665           if (i == 0)
8666             {
8667               base_reg = REGNO (reg);
8668               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
8669                                   ? REGNO (operands[i])
8670                                   : REGNO (SUBREG_REG (operands[i])));
8671               order[0] = 0;
8672             }
8673           else
8674             {
8675               if (base_reg != (int) REGNO (reg))
8676                 /* Not addressed from the same base register.  */
8677                 return 0;
8678
8679               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
8680                                   ? REGNO (operands[i])
8681                                   : REGNO (SUBREG_REG (operands[i])));
8682               if (unsorted_regs[i] < unsorted_regs[order[0]])
8683                 order[0] = i;
8684             }
8685
8686           /* If it isn't an integer register, or if it overwrites the
8687              base register but isn't the last insn in the list, then
8688              we can't do this.  */
8689           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
8690               || (i != nops - 1 && unsorted_regs[i] == base_reg))
8691             return 0;
8692
8693           unsorted_offsets[i] = INTVAL (offset);
8694         }
8695       else
8696         /* Not a suitable memory address.  */
8697         return 0;
8698     }
8699
8700   /* All the useful information has now been extracted from the
8701      operands into unsorted_regs and unsorted_offsets; additionally,
8702      order[0] has been set to the lowest numbered register in the
8703      list.  Sort the registers into order, and check that the memory
8704      offsets are ascending and adjacent.  */
8705
8706   for (i = 1; i < nops; i++)
8707     {
8708       int j;
8709
8710       order[i] = order[i - 1];
8711       for (j = 0; j < nops; j++)
8712         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
8713             && (order[i] == order[i - 1]
8714                 || unsorted_regs[j] < unsorted_regs[order[i]]))
8715           order[i] = j;
8716
8717       /* Have we found a suitable register? if not, one must be used more
8718          than once.  */
8719       if (order[i] == order[i - 1])
8720         return 0;
8721
8722       /* Is the memory address adjacent and ascending? */
8723       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
8724         return 0;
8725     }
8726
8727   if (base)
8728     {
8729       *base = base_reg;
8730
8731       for (i = 0; i < nops; i++)
8732         regs[i] = unsorted_regs[order[i]];
8733
8734       *load_offset = unsorted_offsets[order[0]];
8735     }
8736
8737   if (unsorted_offsets[order[0]] == 0)
8738     return 1; /* ldmia */
8739
8740   if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
8741     return 2; /* ldmib */
8742
8743   if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
8744     return 3; /* ldmda */
8745
8746   if (unsorted_offsets[order[nops - 1]] == -4)
8747     return 4; /* ldmdb */
8748
8749   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
8750      if the offset isn't small enough.  The reason 2 ldrs are faster
8751      is because these ARMs are able to do more than one cache access
8752      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
8753      whilst the ARM8 has a double bandwidth cache.  This means that
8754      these cores can do both an instruction fetch and a data fetch in
8755      a single cycle, so the trick of calculating the address into a
8756      scratch register (one of the result regs) and then doing a load
8757      multiple actually becomes slower (and no smaller in code size).
8758      That is the transformation
8759
8760         ldr     rd1, [rbase + offset]
8761         ldr     rd2, [rbase + offset + 4]
8762
8763      to
8764
8765         add     rd1, rbase, offset
8766         ldmia   rd1, {rd1, rd2}
8767
8768      produces worse code -- '3 cycles + any stalls on rd2' instead of
8769      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
8770      access per cycle, the first sequence could never complete in less
8771      than 6 cycles, whereas the ldm sequence would only take 5 and
8772      would make better use of sequential accesses if not hitting the
8773      cache.
8774
8775      We cheat here and test 'arm_ld_sched' which we currently know to
8776      only be true for the ARM8, ARM9 and StrongARM.  If this ever
8777      changes, then the test below needs to be reworked.  */
8778   if (nops == 2 && arm_ld_sched)
8779     return 0;
8780
8781   /* Can't do it without setting up the offset, only do this if it takes
8782      no more than one insn.  */
8783   return (const_ok_for_arm (unsorted_offsets[order[0]])
8784           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
8785 }
8786
8787 const char *
8788 emit_ldm_seq (rtx *operands, int nops)
8789 {
8790   int regs[4];
8791   int base_reg;
8792   HOST_WIDE_INT offset;
8793   char buf[100];
8794   int i;
8795
8796   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
8797     {
8798     case 1:
8799       strcpy (buf, "ldm%(ia%)\t");
8800       break;
8801
8802     case 2:
8803       strcpy (buf, "ldm%(ib%)\t");
8804       break;
8805
8806     case 3:
8807       strcpy (buf, "ldm%(da%)\t");
8808       break;
8809
8810     case 4:
8811       strcpy (buf, "ldm%(db%)\t");
8812       break;
8813
8814     case 5:
8815       if (offset >= 0)
8816         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
8817                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
8818                  (long) offset);
8819       else
8820         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
8821                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
8822                  (long) -offset);
8823       output_asm_insn (buf, operands);
8824       base_reg = regs[0];
8825       strcpy (buf, "ldm%(ia%)\t");
8826       break;
8827
8828     default:
8829       gcc_unreachable ();
8830     }
8831
8832   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
8833            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
8834
8835   for (i = 1; i < nops; i++)
8836     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
8837              reg_names[regs[i]]);
8838
8839   strcat (buf, "}\t%@ phole ldm");
8840
8841   output_asm_insn (buf, operands);
8842   return "";
8843 }
8844
8845 int
8846 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
8847                          HOST_WIDE_INT * load_offset)
8848 {
8849   int unsorted_regs[4];
8850   HOST_WIDE_INT unsorted_offsets[4];
8851   int order[4];
8852   int base_reg = -1;
8853   int i;
8854
8855   /* Can only handle 2, 3, or 4 insns at present, though could be easily
8856      extended if required.  */
8857   gcc_assert (nops >= 2 && nops <= 4);
8858
8859   memset (order, 0, 4 * sizeof (int));
8860
8861   /* Loop over the operands and check that the memory references are
8862      suitable (i.e. immediate offsets from the same base register).  At
8863      the same time, extract the target register, and the memory
8864      offsets.  */
8865   for (i = 0; i < nops; i++)
8866     {
8867       rtx reg;
8868       rtx offset;
8869
8870       /* Convert a subreg of a mem into the mem itself.  */
8871       if (GET_CODE (operands[nops + i]) == SUBREG)
8872         operands[nops + i] = alter_subreg (operands + (nops + i));
8873
8874       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
8875
8876       /* Don't reorder volatile memory references; it doesn't seem worth
8877          looking for the case where the order is ok anyway.  */
8878       if (MEM_VOLATILE_P (operands[nops + i]))
8879         return 0;
8880
8881       offset = const0_rtx;
8882
8883       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
8884            || (GET_CODE (reg) == SUBREG
8885                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
8886           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
8887               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
8888                    == REG)
8889                   || (GET_CODE (reg) == SUBREG
8890                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
8891               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
8892                   == CONST_INT)))
8893         {
8894           if (i == 0)
8895             {
8896               base_reg = REGNO (reg);
8897               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
8898                                   ? REGNO (operands[i])
8899                                   : REGNO (SUBREG_REG (operands[i])));
8900               order[0] = 0;
8901             }
8902           else
8903             {
8904               if (base_reg != (int) REGNO (reg))
8905                 /* Not addressed from the same base register.  */
8906                 return 0;
8907
8908               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
8909                                   ? REGNO (operands[i])
8910                                   : REGNO (SUBREG_REG (operands[i])));
8911               if (unsorted_regs[i] < unsorted_regs[order[0]])
8912                 order[0] = i;
8913             }
8914
8915           /* If it isn't an integer register, then we can't do this.  */
8916           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
8917             return 0;
8918
8919           unsorted_offsets[i] = INTVAL (offset);
8920         }
8921       else
8922         /* Not a suitable memory address.  */
8923         return 0;
8924     }
8925
8926   /* All the useful information has now been extracted from the
8927      operands into unsorted_regs and unsorted_offsets; additionally,
8928      order[0] has been set to the lowest numbered register in the
8929      list.  Sort the registers into order, and check that the memory
8930      offsets are ascending and adjacent.  */
8931
8932   for (i = 1; i < nops; i++)
8933     {
8934       int j;
8935
8936       order[i] = order[i - 1];
8937       for (j = 0; j < nops; j++)
8938         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
8939             && (order[i] == order[i - 1]
8940                 || unsorted_regs[j] < unsorted_regs[order[i]]))
8941           order[i] = j;
8942
8943       /* Have we found a suitable register? if not, one must be used more
8944          than once.  */
8945       if (order[i] == order[i - 1])
8946         return 0;
8947
8948       /* Is the memory address adjacent and ascending? */
8949       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
8950         return 0;
8951     }
8952
8953   if (base)
8954     {
8955       *base = base_reg;
8956
8957       for (i = 0; i < nops; i++)
8958         regs[i] = unsorted_regs[order[i]];
8959
8960       *load_offset = unsorted_offsets[order[0]];
8961     }
8962
8963   if (unsorted_offsets[order[0]] == 0)
8964     return 1; /* stmia */
8965
8966   if (unsorted_offsets[order[0]] == 4)
8967     return 2; /* stmib */
8968
8969   if (unsorted_offsets[order[nops - 1]] == 0)
8970     return 3; /* stmda */
8971
8972   if (unsorted_offsets[order[nops - 1]] == -4)
8973     return 4; /* stmdb */
8974
8975   return 0;
8976 }
8977
8978 const char *
8979 emit_stm_seq (rtx *operands, int nops)
8980 {
8981   int regs[4];
8982   int base_reg;
8983   HOST_WIDE_INT offset;
8984   char buf[100];
8985   int i;
8986
8987   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
8988     {
8989     case 1:
8990       strcpy (buf, "stm%(ia%)\t");
8991       break;
8992
8993     case 2:
8994       strcpy (buf, "stm%(ib%)\t");
8995       break;
8996
8997     case 3:
8998       strcpy (buf, "stm%(da%)\t");
8999       break;
9000
9001     case 4:
9002       strcpy (buf, "stm%(db%)\t");
9003       break;
9004
9005     default:
9006       gcc_unreachable ();
9007     }
9008
9009   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
9010            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
9011
9012   for (i = 1; i < nops; i++)
9013     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
9014              reg_names[regs[i]]);
9015
9016   strcat (buf, "}\t%@ phole stm");
9017
9018   output_asm_insn (buf, operands);
9019   return "";
9020 }
9021 \f
9022 /* Routines for use in generating RTL.  */
9023
9024 rtx
9025 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
9026                        int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
9027 {
9028   HOST_WIDE_INT offset = *offsetp;
9029   int i = 0, j;
9030   rtx result;
9031   int sign = up ? 1 : -1;
9032   rtx mem, addr;
9033
9034   /* XScale has load-store double instructions, but they have stricter
9035      alignment requirements than load-store multiple, so we cannot
9036      use them.
9037
9038      For XScale ldm requires 2 + NREGS cycles to complete and blocks
9039      the pipeline until completion.
9040
9041         NREGS           CYCLES
9042           1               3
9043           2               4
9044           3               5
9045           4               6
9046
9047      An ldr instruction takes 1-3 cycles, but does not block the
9048      pipeline.
9049
9050         NREGS           CYCLES
9051           1              1-3
9052           2              2-6
9053           3              3-9
9054           4              4-12
9055
9056      Best case ldr will always win.  However, the more ldr instructions
9057      we issue, the less likely we are to be able to schedule them well.
9058      Using ldr instructions also increases code size.
9059
9060      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
9061      for counts of 3 or 4 regs.  */
9062   if (arm_tune_xscale && count <= 2 && ! optimize_size)
9063     {
9064       rtx seq;
9065
9066       start_sequence ();
9067
9068       for (i = 0; i < count; i++)
9069         {
9070           addr = plus_constant (from, i * 4 * sign);
9071           mem = adjust_automodify_address (basemem, SImode, addr, offset);
9072           emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
9073           offset += 4 * sign;
9074         }
9075
9076       if (write_back)
9077         {
9078           emit_move_insn (from, plus_constant (from, count * 4 * sign));
9079           *offsetp = offset;
9080         }
9081
9082       seq = get_insns ();
9083       end_sequence ();
9084
9085       return seq;
9086     }
9087
9088   result = gen_rtx_PARALLEL (VOIDmode,
9089                              rtvec_alloc (count + (write_back ? 1 : 0)));
9090   if (write_back)
9091     {
9092       XVECEXP (result, 0, 0)
9093         = gen_rtx_SET (VOIDmode, from, plus_constant (from, count * 4 * sign));
9094       i = 1;
9095       count++;
9096     }
9097
9098   for (j = 0; i < count; i++, j++)
9099     {
9100       addr = plus_constant (from, j * 4 * sign);
9101       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
9102       XVECEXP (result, 0, i)
9103         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
9104       offset += 4 * sign;
9105     }
9106
9107   if (write_back)
9108     *offsetp = offset;
9109
9110   return result;
9111 }
9112
9113 rtx
9114 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
9115                         int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
9116 {
9117   HOST_WIDE_INT offset = *offsetp;
9118   int i = 0, j;
9119   rtx result;
9120   int sign = up ? 1 : -1;
9121   rtx mem, addr;
9122
9123   /* See arm_gen_load_multiple for discussion of
9124      the pros/cons of ldm/stm usage for XScale.  */
9125   if (arm_tune_xscale && count <= 2 && ! optimize_size)
9126     {
9127       rtx seq;
9128
9129       start_sequence ();
9130
9131       for (i = 0; i < count; i++)
9132         {
9133           addr = plus_constant (to, i * 4 * sign);
9134           mem = adjust_automodify_address (basemem, SImode, addr, offset);
9135           emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
9136           offset += 4 * sign;
9137         }
9138
9139       if (write_back)
9140         {
9141           emit_move_insn (to, plus_constant (to, count * 4 * sign));
9142           *offsetp = offset;
9143         }
9144
9145       seq = get_insns ();
9146       end_sequence ();
9147
9148       return seq;
9149     }
9150
9151   result = gen_rtx_PARALLEL (VOIDmode,
9152                              rtvec_alloc (count + (write_back ? 1 : 0)));
9153   if (write_back)
9154     {
9155       XVECEXP (result, 0, 0)
9156         = gen_rtx_SET (VOIDmode, to,
9157                        plus_constant (to, count * 4 * sign));
9158       i = 1;
9159       count++;
9160     }
9161
9162   for (j = 0; i < count; i++, j++)
9163     {
9164       addr = plus_constant (to, j * 4 * sign);
9165       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
9166       XVECEXP (result, 0, i)
9167         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
9168       offset += 4 * sign;
9169     }
9170
9171   if (write_back)
9172     *offsetp = offset;
9173
9174   return result;
9175 }
9176
9177 int
9178 arm_gen_movmemqi (rtx *operands)
9179 {
9180   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
9181   HOST_WIDE_INT srcoffset, dstoffset;
9182   int i;
9183   rtx src, dst, srcbase, dstbase;
9184   rtx part_bytes_reg = NULL;
9185   rtx mem;
9186
9187   if (GET_CODE (operands[2]) != CONST_INT
9188       || GET_CODE (operands[3]) != CONST_INT
9189       || INTVAL (operands[2]) > 64
9190       || INTVAL (operands[3]) & 3)
9191     return 0;
9192
9193   dstbase = operands[0];
9194   srcbase = operands[1];
9195
9196   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
9197   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
9198
9199   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
9200   out_words_to_go = INTVAL (operands[2]) / 4;
9201   last_bytes = INTVAL (operands[2]) & 3;
9202   dstoffset = srcoffset = 0;
9203
9204   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
9205     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
9206
9207   for (i = 0; in_words_to_go >= 2; i+=4)
9208     {
9209       if (in_words_to_go > 4)
9210         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
9211                                           srcbase, &srcoffset));
9212       else
9213         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
9214                                           FALSE, srcbase, &srcoffset));
9215
9216       if (out_words_to_go)
9217         {
9218           if (out_words_to_go > 4)
9219             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
9220                                                dstbase, &dstoffset));
9221           else if (out_words_to_go != 1)
9222             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
9223                                                dst, TRUE,
9224                                                (last_bytes == 0
9225                                                 ? FALSE : TRUE),
9226                                                dstbase, &dstoffset));
9227           else
9228             {
9229               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
9230               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
9231               if (last_bytes != 0)
9232                 {
9233                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
9234                   dstoffset += 4;
9235                 }
9236             }
9237         }
9238
9239       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
9240       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
9241     }
9242
9243   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
9244   if (out_words_to_go)
9245     {
9246       rtx sreg;
9247
9248       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
9249       sreg = copy_to_reg (mem);
9250
9251       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
9252       emit_move_insn (mem, sreg);
9253       in_words_to_go--;
9254
9255       gcc_assert (!in_words_to_go);     /* Sanity check */
9256     }
9257
9258   if (in_words_to_go)
9259     {
9260       gcc_assert (in_words_to_go > 0);
9261
9262       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
9263       part_bytes_reg = copy_to_mode_reg (SImode, mem);
9264     }
9265
9266   gcc_assert (!last_bytes || part_bytes_reg);
9267
9268   if (BYTES_BIG_ENDIAN && last_bytes)
9269     {
9270       rtx tmp = gen_reg_rtx (SImode);
9271
9272       /* The bytes we want are in the top end of the word.  */
9273       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
9274                               GEN_INT (8 * (4 - last_bytes))));
9275       part_bytes_reg = tmp;
9276
9277       while (last_bytes)
9278         {
9279           mem = adjust_automodify_address (dstbase, QImode,
9280                                            plus_constant (dst, last_bytes - 1),
9281                                            dstoffset + last_bytes - 1);
9282           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
9283
9284           if (--last_bytes)
9285             {
9286               tmp = gen_reg_rtx (SImode);
9287               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
9288               part_bytes_reg = tmp;
9289             }
9290         }
9291
9292     }
9293   else
9294     {
9295       if (last_bytes > 1)
9296         {
9297           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
9298           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
9299           last_bytes -= 2;
9300           if (last_bytes)
9301             {
9302               rtx tmp = gen_reg_rtx (SImode);
9303               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
9304               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
9305               part_bytes_reg = tmp;
9306               dstoffset += 2;
9307             }
9308         }
9309
9310       if (last_bytes)
9311         {
9312           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
9313           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
9314         }
9315     }
9316
9317   return 1;
9318 }
9319
9320 /* Select a dominance comparison mode if possible for a test of the general
9321    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
9322    COND_OR == DOM_CC_X_AND_Y => (X && Y)
9323    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
9324    COND_OR == DOM_CC_X_OR_Y => (X || Y)
9325    In all cases OP will be either EQ or NE, but we don't need to know which
9326    here.  If we are unable to support a dominance comparison we return
9327    CC mode.  This will then fail to match for the RTL expressions that
9328    generate this call.  */
9329 enum machine_mode
9330 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
9331 {
9332   enum rtx_code cond1, cond2;
9333   int swapped = 0;
9334
9335   /* Currently we will probably get the wrong result if the individual
9336      comparisons are not simple.  This also ensures that it is safe to
9337      reverse a comparison if necessary.  */
9338   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
9339        != CCmode)
9340       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
9341           != CCmode))
9342     return CCmode;
9343
9344   /* The if_then_else variant of this tests the second condition if the
9345      first passes, but is true if the first fails.  Reverse the first
9346      condition to get a true "inclusive-or" expression.  */
9347   if (cond_or == DOM_CC_NX_OR_Y)
9348     cond1 = reverse_condition (cond1);
9349
9350   /* If the comparisons are not equal, and one doesn't dominate the other,
9351      then we can't do this.  */
9352   if (cond1 != cond2
9353       && !comparison_dominates_p (cond1, cond2)
9354       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
9355     return CCmode;
9356
9357   if (swapped)
9358     {
9359       enum rtx_code temp = cond1;
9360       cond1 = cond2;
9361       cond2 = temp;
9362     }
9363
9364   switch (cond1)
9365     {
9366     case EQ:
9367       if (cond_or == DOM_CC_X_AND_Y)
9368         return CC_DEQmode;
9369
9370       switch (cond2)
9371         {
9372         case EQ: return CC_DEQmode;
9373         case LE: return CC_DLEmode;
9374         case LEU: return CC_DLEUmode;
9375         case GE: return CC_DGEmode;
9376         case GEU: return CC_DGEUmode;
9377         default: gcc_unreachable ();
9378         }
9379
9380     case LT:
9381       if (cond_or == DOM_CC_X_AND_Y)
9382         return CC_DLTmode;
9383
9384       switch (cond2)
9385         {
9386         case  LT:
9387             return CC_DLTmode;
9388         case LE:
9389           return CC_DLEmode;
9390         case NE:
9391           return CC_DNEmode;
9392         default:
9393           gcc_unreachable ();
9394         }
9395
9396     case GT:
9397       if (cond_or == DOM_CC_X_AND_Y)
9398         return CC_DGTmode;
9399
9400       switch (cond2)
9401         {
9402         case GT:
9403           return CC_DGTmode;
9404         case GE:
9405           return CC_DGEmode;
9406         case NE:
9407           return CC_DNEmode;
9408         default:
9409           gcc_unreachable ();
9410         }
9411
9412     case LTU:
9413       if (cond_or == DOM_CC_X_AND_Y)
9414         return CC_DLTUmode;
9415
9416       switch (cond2)
9417         {
9418         case LTU:
9419           return CC_DLTUmode;
9420         case LEU:
9421           return CC_DLEUmode;
9422         case NE:
9423           return CC_DNEmode;
9424         default:
9425           gcc_unreachable ();
9426         }
9427
9428     case GTU:
9429       if (cond_or == DOM_CC_X_AND_Y)
9430         return CC_DGTUmode;
9431
9432       switch (cond2)
9433         {
9434         case GTU:
9435           return CC_DGTUmode;
9436         case GEU:
9437           return CC_DGEUmode;
9438         case NE:
9439           return CC_DNEmode;
9440         default:
9441           gcc_unreachable ();
9442         }
9443
9444     /* The remaining cases only occur when both comparisons are the
9445        same.  */
9446     case NE:
9447       gcc_assert (cond1 == cond2);
9448       return CC_DNEmode;
9449
9450     case LE:
9451       gcc_assert (cond1 == cond2);
9452       return CC_DLEmode;
9453
9454     case GE:
9455       gcc_assert (cond1 == cond2);
9456       return CC_DGEmode;
9457
9458     case LEU:
9459       gcc_assert (cond1 == cond2);
9460       return CC_DLEUmode;
9461
9462     case GEU:
9463       gcc_assert (cond1 == cond2);
9464       return CC_DGEUmode;
9465
9466     default:
9467       gcc_unreachable ();
9468     }
9469 }
9470
9471 enum machine_mode
9472 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
9473 {
9474   /* All floating point compares return CCFP if it is an equality
9475      comparison, and CCFPE otherwise.  */
9476   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
9477     {
9478       switch (op)
9479         {
9480         case EQ:
9481         case NE:
9482         case UNORDERED:
9483         case ORDERED:
9484         case UNLT:
9485         case UNLE:
9486         case UNGT:
9487         case UNGE:
9488         case UNEQ:
9489         case LTGT:
9490           return CCFPmode;
9491
9492         case LT:
9493         case LE:
9494         case GT:
9495         case GE:
9496           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
9497             return CCFPmode;
9498           return CCFPEmode;
9499
9500         default:
9501           gcc_unreachable ();
9502         }
9503     }
9504
9505   /* A compare with a shifted operand.  Because of canonicalization, the
9506      comparison will have to be swapped when we emit the assembler.  */
9507   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
9508       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
9509           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
9510           || GET_CODE (x) == ROTATERT))
9511     return CC_SWPmode;
9512
9513   /* This operation is performed swapped, but since we only rely on the Z
9514      flag we don't need an additional mode.  */
9515   if (GET_MODE (y) == SImode && REG_P (y)
9516       && GET_CODE (x) == NEG
9517       && (op == EQ || op == NE))
9518     return CC_Zmode;
9519
9520   /* This is a special case that is used by combine to allow a
9521      comparison of a shifted byte load to be split into a zero-extend
9522      followed by a comparison of the shifted integer (only valid for
9523      equalities and unsigned inequalities).  */
9524   if (GET_MODE (x) == SImode
9525       && GET_CODE (x) == ASHIFT
9526       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
9527       && GET_CODE (XEXP (x, 0)) == SUBREG
9528       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
9529       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
9530       && (op == EQ || op == NE
9531           || op == GEU || op == GTU || op == LTU || op == LEU)
9532       && GET_CODE (y) == CONST_INT)
9533     return CC_Zmode;
9534
9535   /* A construct for a conditional compare, if the false arm contains
9536      0, then both conditions must be true, otherwise either condition
9537      must be true.  Not all conditions are possible, so CCmode is
9538      returned if it can't be done.  */
9539   if (GET_CODE (x) == IF_THEN_ELSE
9540       && (XEXP (x, 2) == const0_rtx
9541           || XEXP (x, 2) == const1_rtx)
9542       && COMPARISON_P (XEXP (x, 0))
9543       && COMPARISON_P (XEXP (x, 1)))
9544     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
9545                                          INTVAL (XEXP (x, 2)));
9546
9547   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
9548   if (GET_CODE (x) == AND
9549       && COMPARISON_P (XEXP (x, 0))
9550       && COMPARISON_P (XEXP (x, 1)))
9551     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
9552                                          DOM_CC_X_AND_Y);
9553
9554   if (GET_CODE (x) == IOR
9555       && COMPARISON_P (XEXP (x, 0))
9556       && COMPARISON_P (XEXP (x, 1)))
9557     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
9558                                          DOM_CC_X_OR_Y);
9559
9560   /* An operation (on Thumb) where we want to test for a single bit.
9561      This is done by shifting that bit up into the top bit of a
9562      scratch register; we can then branch on the sign bit.  */
9563   if (TARGET_THUMB1
9564       && GET_MODE (x) == SImode
9565       && (op == EQ || op == NE)
9566       && GET_CODE (x) == ZERO_EXTRACT
9567       && XEXP (x, 1) == const1_rtx)
9568     return CC_Nmode;
9569
9570   /* An operation that sets the condition codes as a side-effect, the
9571      V flag is not set correctly, so we can only use comparisons where
9572      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
9573      instead.)  */
9574   /* ??? Does the ZERO_EXTRACT case really apply to thumb2?  */
9575   if (GET_MODE (x) == SImode
9576       && y == const0_rtx
9577       && (op == EQ || op == NE || op == LT || op == GE)
9578       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
9579           || GET_CODE (x) == AND || GET_CODE (x) == IOR
9580           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
9581           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
9582           || GET_CODE (x) == LSHIFTRT
9583           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
9584           || GET_CODE (x) == ROTATERT
9585           || (TARGET_32BIT && GET_CODE (x) == ZERO_EXTRACT)))
9586     return CC_NOOVmode;
9587
9588   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
9589     return CC_Zmode;
9590
9591   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
9592       && GET_CODE (x) == PLUS
9593       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
9594     return CC_Cmode;
9595
9596   return CCmode;
9597 }
9598
9599 /* X and Y are two things to compare using CODE.  Emit the compare insn and
9600    return the rtx for register 0 in the proper mode.  FP means this is a
9601    floating point compare: I don't think that it is needed on the arm.  */
9602 rtx
9603 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
9604 {
9605   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
9606   rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
9607
9608   emit_set_insn (cc_reg, gen_rtx_COMPARE (mode, x, y));
9609
9610   return cc_reg;
9611 }
9612
9613 /* Generate a sequence of insns that will generate the correct return
9614    address mask depending on the physical architecture that the program
9615    is running on.  */
9616 rtx
9617 arm_gen_return_addr_mask (void)
9618 {
9619   rtx reg = gen_reg_rtx (Pmode);
9620
9621   emit_insn (gen_return_addr_mask (reg));
9622   return reg;
9623 }
9624
9625 void
9626 arm_reload_in_hi (rtx *operands)
9627 {
9628   rtx ref = operands[1];
9629   rtx base, scratch;
9630   HOST_WIDE_INT offset = 0;
9631
9632   if (GET_CODE (ref) == SUBREG)
9633     {
9634       offset = SUBREG_BYTE (ref);
9635       ref = SUBREG_REG (ref);
9636     }
9637
9638   if (GET_CODE (ref) == REG)
9639     {
9640       /* We have a pseudo which has been spilt onto the stack; there
9641          are two cases here: the first where there is a simple
9642          stack-slot replacement and a second where the stack-slot is
9643          out of range, or is used as a subreg.  */
9644       if (reg_equiv_mem[REGNO (ref)])
9645         {
9646           ref = reg_equiv_mem[REGNO (ref)];
9647           base = find_replacement (&XEXP (ref, 0));
9648         }
9649       else
9650         /* The slot is out of range, or was dressed up in a SUBREG.  */
9651         base = reg_equiv_address[REGNO (ref)];
9652     }
9653   else
9654     base = find_replacement (&XEXP (ref, 0));
9655
9656   /* Handle the case where the address is too complex to be offset by 1.  */
9657   if (GET_CODE (base) == MINUS
9658       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
9659     {
9660       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
9661
9662       emit_set_insn (base_plus, base);
9663       base = base_plus;
9664     }
9665   else if (GET_CODE (base) == PLUS)
9666     {
9667       /* The addend must be CONST_INT, or we would have dealt with it above.  */
9668       HOST_WIDE_INT hi, lo;
9669
9670       offset += INTVAL (XEXP (base, 1));
9671       base = XEXP (base, 0);
9672
9673       /* Rework the address into a legal sequence of insns.  */
9674       /* Valid range for lo is -4095 -> 4095 */
9675       lo = (offset >= 0
9676             ? (offset & 0xfff)
9677             : -((-offset) & 0xfff));
9678
9679       /* Corner case, if lo is the max offset then we would be out of range
9680          once we have added the additional 1 below, so bump the msb into the
9681          pre-loading insn(s).  */
9682       if (lo == 4095)
9683         lo &= 0x7ff;
9684
9685       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
9686              ^ (HOST_WIDE_INT) 0x80000000)
9687             - (HOST_WIDE_INT) 0x80000000);
9688
9689       gcc_assert (hi + lo == offset);
9690
9691       if (hi != 0)
9692         {
9693           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
9694
9695           /* Get the base address; addsi3 knows how to handle constants
9696              that require more than one insn.  */
9697           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
9698           base = base_plus;
9699           offset = lo;
9700         }
9701     }
9702
9703   /* Operands[2] may overlap operands[0] (though it won't overlap
9704      operands[1]), that's why we asked for a DImode reg -- so we can
9705      use the bit that does not overlap.  */
9706   if (REGNO (operands[2]) == REGNO (operands[0]))
9707     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
9708   else
9709     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
9710
9711   emit_insn (gen_zero_extendqisi2 (scratch,
9712                                    gen_rtx_MEM (QImode,
9713                                                 plus_constant (base,
9714                                                                offset))));
9715   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
9716                                    gen_rtx_MEM (QImode,
9717                                                 plus_constant (base,
9718                                                                offset + 1))));
9719   if (!BYTES_BIG_ENDIAN)
9720     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
9721                    gen_rtx_IOR (SImode,
9722                                 gen_rtx_ASHIFT
9723                                 (SImode,
9724                                  gen_rtx_SUBREG (SImode, operands[0], 0),
9725                                  GEN_INT (8)),
9726                                 scratch));
9727   else
9728     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
9729                    gen_rtx_IOR (SImode,
9730                                 gen_rtx_ASHIFT (SImode, scratch,
9731                                                 GEN_INT (8)),
9732                                 gen_rtx_SUBREG (SImode, operands[0], 0)));
9733 }
9734
9735 /* Handle storing a half-word to memory during reload by synthesizing as two
9736    byte stores.  Take care not to clobber the input values until after we
9737    have moved them somewhere safe.  This code assumes that if the DImode
9738    scratch in operands[2] overlaps either the input value or output address
9739    in some way, then that value must die in this insn (we absolutely need
9740    two scratch registers for some corner cases).  */
9741 void
9742 arm_reload_out_hi (rtx *operands)
9743 {
9744   rtx ref = operands[0];
9745   rtx outval = operands[1];
9746   rtx base, scratch;
9747   HOST_WIDE_INT offset = 0;
9748
9749   if (GET_CODE (ref) == SUBREG)
9750     {
9751       offset = SUBREG_BYTE (ref);
9752       ref = SUBREG_REG (ref);
9753     }
9754
9755   if (GET_CODE (ref) == REG)
9756     {
9757       /* We have a pseudo which has been spilt onto the stack; there
9758          are two cases here: the first where there is a simple
9759          stack-slot replacement and a second where the stack-slot is
9760          out of range, or is used as a subreg.  */
9761       if (reg_equiv_mem[REGNO (ref)])
9762         {
9763           ref = reg_equiv_mem[REGNO (ref)];
9764           base = find_replacement (&XEXP (ref, 0));
9765         }
9766       else
9767         /* The slot is out of range, or was dressed up in a SUBREG.  */
9768         base = reg_equiv_address[REGNO (ref)];
9769     }
9770   else
9771     base = find_replacement (&XEXP (ref, 0));
9772
9773   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
9774
9775   /* Handle the case where the address is too complex to be offset by 1.  */
9776   if (GET_CODE (base) == MINUS
9777       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
9778     {
9779       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
9780
9781       /* Be careful not to destroy OUTVAL.  */
9782       if (reg_overlap_mentioned_p (base_plus, outval))
9783         {
9784           /* Updating base_plus might destroy outval, see if we can
9785              swap the scratch and base_plus.  */
9786           if (!reg_overlap_mentioned_p (scratch, outval))
9787             {
9788               rtx tmp = scratch;
9789               scratch = base_plus;
9790               base_plus = tmp;
9791             }
9792           else
9793             {
9794               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
9795
9796               /* Be conservative and copy OUTVAL into the scratch now,
9797                  this should only be necessary if outval is a subreg
9798                  of something larger than a word.  */
9799               /* XXX Might this clobber base?  I can't see how it can,
9800                  since scratch is known to overlap with OUTVAL, and
9801                  must be wider than a word.  */
9802               emit_insn (gen_movhi (scratch_hi, outval));
9803               outval = scratch_hi;
9804             }
9805         }
9806
9807       emit_set_insn (base_plus, base);
9808       base = base_plus;
9809     }
9810   else if (GET_CODE (base) == PLUS)
9811     {
9812       /* The addend must be CONST_INT, or we would have dealt with it above.  */
9813       HOST_WIDE_INT hi, lo;
9814
9815       offset += INTVAL (XEXP (base, 1));
9816       base = XEXP (base, 0);
9817
9818       /* Rework the address into a legal sequence of insns.  */
9819       /* Valid range for lo is -4095 -> 4095 */
9820       lo = (offset >= 0
9821             ? (offset & 0xfff)
9822             : -((-offset) & 0xfff));
9823
9824       /* Corner case, if lo is the max offset then we would be out of range
9825          once we have added the additional 1 below, so bump the msb into the
9826          pre-loading insn(s).  */
9827       if (lo == 4095)
9828         lo &= 0x7ff;
9829
9830       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
9831              ^ (HOST_WIDE_INT) 0x80000000)
9832             - (HOST_WIDE_INT) 0x80000000);
9833
9834       gcc_assert (hi + lo == offset);
9835
9836       if (hi != 0)
9837         {
9838           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
9839
9840           /* Be careful not to destroy OUTVAL.  */
9841           if (reg_overlap_mentioned_p (base_plus, outval))
9842             {
9843               /* Updating base_plus might destroy outval, see if we
9844                  can swap the scratch and base_plus.  */
9845               if (!reg_overlap_mentioned_p (scratch, outval))
9846                 {
9847                   rtx tmp = scratch;
9848                   scratch = base_plus;
9849                   base_plus = tmp;
9850                 }
9851               else
9852                 {
9853                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
9854
9855                   /* Be conservative and copy outval into scratch now,
9856                      this should only be necessary if outval is a
9857                      subreg of something larger than a word.  */
9858                   /* XXX Might this clobber base?  I can't see how it
9859                      can, since scratch is known to overlap with
9860                      outval.  */
9861                   emit_insn (gen_movhi (scratch_hi, outval));
9862                   outval = scratch_hi;
9863                 }
9864             }
9865
9866           /* Get the base address; addsi3 knows how to handle constants
9867              that require more than one insn.  */
9868           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
9869           base = base_plus;
9870           offset = lo;
9871         }
9872     }
9873
9874   if (BYTES_BIG_ENDIAN)
9875     {
9876       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
9877                                          plus_constant (base, offset + 1)),
9878                             gen_lowpart (QImode, outval)));
9879       emit_insn (gen_lshrsi3 (scratch,
9880                               gen_rtx_SUBREG (SImode, outval, 0),
9881                               GEN_INT (8)));
9882       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
9883                             gen_lowpart (QImode, scratch)));
9884     }
9885   else
9886     {
9887       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
9888                             gen_lowpart (QImode, outval)));
9889       emit_insn (gen_lshrsi3 (scratch,
9890                               gen_rtx_SUBREG (SImode, outval, 0),
9891                               GEN_INT (8)));
9892       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
9893                                          plus_constant (base, offset + 1)),
9894                             gen_lowpart (QImode, scratch)));
9895     }
9896 }
9897
9898 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
9899    (padded to the size of a word) should be passed in a register.  */
9900
9901 static bool
9902 arm_must_pass_in_stack (enum machine_mode mode, const_tree type)
9903 {
9904   if (TARGET_AAPCS_BASED)
9905     return must_pass_in_stack_var_size (mode, type);
9906   else
9907     return must_pass_in_stack_var_size_or_pad (mode, type);
9908 }
9909
9910
9911 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
9912    Return true if an argument passed on the stack should be padded upwards,
9913    i.e. if the least-significant byte has useful data.
9914    For legacy APCS ABIs we use the default.  For AAPCS based ABIs small
9915    aggregate types are placed in the lowest memory address.  */
9916
9917 bool
9918 arm_pad_arg_upward (enum machine_mode mode, const_tree type)
9919 {
9920   if (!TARGET_AAPCS_BASED)
9921     return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
9922
9923   if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
9924     return false;
9925
9926   return true;
9927 }
9928
9929
9930 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
9931    For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
9932    byte of the register has useful data, and return the opposite if the
9933    most significant byte does.
9934    For AAPCS, small aggregates and small complex types are always padded
9935    upwards.  */
9936
9937 bool
9938 arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
9939                     tree type, int first ATTRIBUTE_UNUSED)
9940 {
9941   if (TARGET_AAPCS_BASED
9942       && BYTES_BIG_ENDIAN
9943       && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
9944       && int_size_in_bytes (type) <= 4)
9945     return true;
9946
9947   /* Otherwise, use default padding.  */
9948   return !BYTES_BIG_ENDIAN;
9949 }
9950
9951 \f
9952 /* Print a symbolic form of X to the debug file, F.  */
9953 static void
9954 arm_print_value (FILE *f, rtx x)
9955 {
9956   switch (GET_CODE (x))
9957     {
9958     case CONST_INT:
9959       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
9960       return;
9961
9962     case CONST_DOUBLE:
9963       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
9964       return;
9965
9966     case CONST_VECTOR:
9967       {
9968         int i;
9969
9970         fprintf (f, "<");
9971         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
9972           {
9973             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
9974             if (i < (CONST_VECTOR_NUNITS (x) - 1))
9975               fputc (',', f);
9976           }
9977         fprintf (f, ">");
9978       }
9979       return;
9980
9981     case CONST_STRING:
9982       fprintf (f, "\"%s\"", XSTR (x, 0));
9983       return;
9984
9985     case SYMBOL_REF:
9986       fprintf (f, "`%s'", XSTR (x, 0));
9987       return;
9988
9989     case LABEL_REF:
9990       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
9991       return;
9992
9993     case CONST:
9994       arm_print_value (f, XEXP (x, 0));
9995       return;
9996
9997     case PLUS:
9998       arm_print_value (f, XEXP (x, 0));
9999       fprintf (f, "+");
10000       arm_print_value (f, XEXP (x, 1));
10001       return;
10002
10003     case PC:
10004       fprintf (f, "pc");
10005       return;
10006
10007     default:
10008       fprintf (f, "????");
10009       return;
10010     }
10011 }
10012 \f
10013 /* Routines for manipulation of the constant pool.  */
10014
10015 /* Arm instructions cannot load a large constant directly into a
10016    register; they have to come from a pc relative load.  The constant
10017    must therefore be placed in the addressable range of the pc
10018    relative load.  Depending on the precise pc relative load
10019    instruction the range is somewhere between 256 bytes and 4k.  This
10020    means that we often have to dump a constant inside a function, and
10021    generate code to branch around it.
10022
10023    It is important to minimize this, since the branches will slow
10024    things down and make the code larger.
10025
10026    Normally we can hide the table after an existing unconditional
10027    branch so that there is no interruption of the flow, but in the
10028    worst case the code looks like this:
10029
10030         ldr     rn, L1
10031         ...
10032         b       L2
10033         align
10034         L1:     .long value
10035         L2:
10036         ...
10037
10038         ldr     rn, L3
10039         ...
10040         b       L4
10041         align
10042         L3:     .long value
10043         L4:
10044         ...
10045
10046    We fix this by performing a scan after scheduling, which notices
10047    which instructions need to have their operands fetched from the
10048    constant table and builds the table.
10049
10050    The algorithm starts by building a table of all the constants that
10051    need fixing up and all the natural barriers in the function (places
10052    where a constant table can be dropped without breaking the flow).
10053    For each fixup we note how far the pc-relative replacement will be
10054    able to reach and the offset of the instruction into the function.
10055
10056    Having built the table we then group the fixes together to form
10057    tables that are as large as possible (subject to addressing
10058    constraints) and emit each table of constants after the last
10059    barrier that is within range of all the instructions in the group.
10060    If a group does not contain a barrier, then we forcibly create one
10061    by inserting a jump instruction into the flow.  Once the table has
10062    been inserted, the insns are then modified to reference the
10063    relevant entry in the pool.
10064
10065    Possible enhancements to the algorithm (not implemented) are:
10066
10067    1) For some processors and object formats, there may be benefit in
10068    aligning the pools to the start of cache lines; this alignment
10069    would need to be taken into account when calculating addressability
10070    of a pool.  */
10071
10072 /* These typedefs are located at the start of this file, so that
10073    they can be used in the prototypes there.  This comment is to
10074    remind readers of that fact so that the following structures
10075    can be understood more easily.
10076
10077      typedef struct minipool_node    Mnode;
10078      typedef struct minipool_fixup   Mfix;  */
10079
10080 struct minipool_node
10081 {
10082   /* Doubly linked chain of entries.  */
10083   Mnode * next;
10084   Mnode * prev;
10085   /* The maximum offset into the code that this entry can be placed.  While
10086      pushing fixes for forward references, all entries are sorted in order
10087      of increasing max_address.  */
10088   HOST_WIDE_INT max_address;
10089   /* Similarly for an entry inserted for a backwards ref.  */
10090   HOST_WIDE_INT min_address;
10091   /* The number of fixes referencing this entry.  This can become zero
10092      if we "unpush" an entry.  In this case we ignore the entry when we
10093      come to emit the code.  */
10094   int refcount;
10095   /* The offset from the start of the minipool.  */
10096   HOST_WIDE_INT offset;
10097   /* The value in table.  */
10098   rtx value;
10099   /* The mode of value.  */
10100   enum machine_mode mode;
10101   /* The size of the value.  With iWMMXt enabled
10102      sizes > 4 also imply an alignment of 8-bytes.  */
10103   int fix_size;
10104 };
10105
10106 struct minipool_fixup
10107 {
10108   Mfix *            next;
10109   rtx               insn;
10110   HOST_WIDE_INT     address;
10111   rtx *             loc;
10112   enum machine_mode mode;
10113   int               fix_size;
10114   rtx               value;
10115   Mnode *           minipool;
10116   HOST_WIDE_INT     forwards;
10117   HOST_WIDE_INT     backwards;
10118 };
10119
10120 /* Fixes less than a word need padding out to a word boundary.  */
10121 #define MINIPOOL_FIX_SIZE(mode) \
10122   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
10123
10124 static Mnode *  minipool_vector_head;
10125 static Mnode *  minipool_vector_tail;
10126 static rtx      minipool_vector_label;
10127 static int      minipool_pad;
10128
10129 /* The linked list of all minipool fixes required for this function.  */
10130 Mfix *          minipool_fix_head;
10131 Mfix *          minipool_fix_tail;
10132 /* The fix entry for the current minipool, once it has been placed.  */
10133 Mfix *          minipool_barrier;
10134
10135 /* Determines if INSN is the start of a jump table.  Returns the end
10136    of the TABLE or NULL_RTX.  */
10137 static rtx
10138 is_jump_table (rtx insn)
10139 {
10140   rtx table;
10141
10142   if (GET_CODE (insn) == JUMP_INSN
10143       && JUMP_LABEL (insn) != NULL
10144       && ((table = next_real_insn (JUMP_LABEL (insn)))
10145           == next_real_insn (insn))
10146       && table != NULL
10147       && GET_CODE (table) == JUMP_INSN
10148       && (GET_CODE (PATTERN (table)) == ADDR_VEC
10149           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
10150     return table;
10151
10152   return NULL_RTX;
10153 }
10154
10155 #ifndef JUMP_TABLES_IN_TEXT_SECTION
10156 #define JUMP_TABLES_IN_TEXT_SECTION 0
10157 #endif
10158
10159 static HOST_WIDE_INT
10160 get_jump_table_size (rtx insn)
10161 {
10162   /* ADDR_VECs only take room if read-only data does into the text
10163      section.  */
10164   if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
10165     {
10166       rtx body = PATTERN (insn);
10167       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
10168       HOST_WIDE_INT size;
10169       HOST_WIDE_INT modesize;
10170
10171       modesize = GET_MODE_SIZE (GET_MODE (body));
10172       size = modesize * XVECLEN (body, elt);
10173       switch (modesize)
10174         {
10175         case 1:
10176           /* Round up size  of TBB table to a halfword boundary.  */
10177           size = (size + 1) & ~(HOST_WIDE_INT)1;
10178           break;
10179         case 2:
10180           /* No padding necessary for TBH.  */
10181           break;
10182         case 4:
10183           /* Add two bytes for alignment on Thumb.  */
10184           if (TARGET_THUMB)
10185             size += 2;
10186           break;
10187         default:
10188           gcc_unreachable ();
10189         }
10190       return size;
10191     }
10192
10193   return 0;
10194 }
10195
10196 /* Move a minipool fix MP from its current location to before MAX_MP.
10197    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
10198    constraints may need updating.  */
10199 static Mnode *
10200 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
10201                                HOST_WIDE_INT max_address)
10202 {
10203   /* The code below assumes these are different.  */
10204   gcc_assert (mp != max_mp);
10205
10206   if (max_mp == NULL)
10207     {
10208       if (max_address < mp->max_address)
10209         mp->max_address = max_address;
10210     }
10211   else
10212     {
10213       if (max_address > max_mp->max_address - mp->fix_size)
10214         mp->max_address = max_mp->max_address - mp->fix_size;
10215       else
10216         mp->max_address = max_address;
10217
10218       /* Unlink MP from its current position.  Since max_mp is non-null,
10219        mp->prev must be non-null.  */
10220       mp->prev->next = mp->next;
10221       if (mp->next != NULL)
10222         mp->next->prev = mp->prev;
10223       else
10224         minipool_vector_tail = mp->prev;
10225
10226       /* Re-insert it before MAX_MP.  */
10227       mp->next = max_mp;
10228       mp->prev = max_mp->prev;
10229       max_mp->prev = mp;
10230
10231       if (mp->prev != NULL)
10232         mp->prev->next = mp;
10233       else
10234         minipool_vector_head = mp;
10235     }
10236
10237   /* Save the new entry.  */
10238   max_mp = mp;
10239
10240   /* Scan over the preceding entries and adjust their addresses as
10241      required.  */
10242   while (mp->prev != NULL
10243          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
10244     {
10245       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
10246       mp = mp->prev;
10247     }
10248
10249   return max_mp;
10250 }
10251
10252 /* Add a constant to the minipool for a forward reference.  Returns the
10253    node added or NULL if the constant will not fit in this pool.  */
10254 static Mnode *
10255 add_minipool_forward_ref (Mfix *fix)
10256 {
10257   /* If set, max_mp is the first pool_entry that has a lower
10258      constraint than the one we are trying to add.  */
10259   Mnode *       max_mp = NULL;
10260   HOST_WIDE_INT max_address = fix->address + fix->forwards - minipool_pad;
10261   Mnode *       mp;
10262
10263   /* If the minipool starts before the end of FIX->INSN then this FIX
10264      can not be placed into the current pool.  Furthermore, adding the
10265      new constant pool entry may cause the pool to start FIX_SIZE bytes
10266      earlier.  */
10267   if (minipool_vector_head &&
10268       (fix->address + get_attr_length (fix->insn)
10269        >= minipool_vector_head->max_address - fix->fix_size))
10270     return NULL;
10271
10272   /* Scan the pool to see if a constant with the same value has
10273      already been added.  While we are doing this, also note the
10274      location where we must insert the constant if it doesn't already
10275      exist.  */
10276   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
10277     {
10278       if (GET_CODE (fix->value) == GET_CODE (mp->value)
10279           && fix->mode == mp->mode
10280           && (GET_CODE (fix->value) != CODE_LABEL
10281               || (CODE_LABEL_NUMBER (fix->value)
10282                   == CODE_LABEL_NUMBER (mp->value)))
10283           && rtx_equal_p (fix->value, mp->value))
10284         {
10285           /* More than one fix references this entry.  */
10286           mp->refcount++;
10287           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
10288         }
10289
10290       /* Note the insertion point if necessary.  */
10291       if (max_mp == NULL
10292           && mp->max_address > max_address)
10293         max_mp = mp;
10294
10295       /* If we are inserting an 8-bytes aligned quantity and
10296          we have not already found an insertion point, then
10297          make sure that all such 8-byte aligned quantities are
10298          placed at the start of the pool.  */
10299       if (ARM_DOUBLEWORD_ALIGN
10300           && max_mp == NULL
10301           && fix->fix_size >= 8
10302           && mp->fix_size < 8)
10303         {
10304           max_mp = mp;
10305           max_address = mp->max_address;
10306         }
10307     }
10308
10309   /* The value is not currently in the minipool, so we need to create
10310      a new entry for it.  If MAX_MP is NULL, the entry will be put on
10311      the end of the list since the placement is less constrained than
10312      any existing entry.  Otherwise, we insert the new fix before
10313      MAX_MP and, if necessary, adjust the constraints on the other
10314      entries.  */
10315   mp = XNEW (Mnode);
10316   mp->fix_size = fix->fix_size;
10317   mp->mode = fix->mode;
10318   mp->value = fix->value;
10319   mp->refcount = 1;
10320   /* Not yet required for a backwards ref.  */
10321   mp->min_address = -65536;
10322
10323   if (max_mp == NULL)
10324     {
10325       mp->max_address = max_address;
10326       mp->next = NULL;
10327       mp->prev = minipool_vector_tail;
10328
10329       if (mp->prev == NULL)
10330         {
10331           minipool_vector_head = mp;
10332           minipool_vector_label = gen_label_rtx ();
10333         }
10334       else
10335         mp->prev->next = mp;
10336
10337       minipool_vector_tail = mp;
10338     }
10339   else
10340     {
10341       if (max_address > max_mp->max_address - mp->fix_size)
10342         mp->max_address = max_mp->max_address - mp->fix_size;
10343       else
10344         mp->max_address = max_address;
10345
10346       mp->next = max_mp;
10347       mp->prev = max_mp->prev;
10348       max_mp->prev = mp;
10349       if (mp->prev != NULL)
10350         mp->prev->next = mp;
10351       else
10352         minipool_vector_head = mp;
10353     }
10354
10355   /* Save the new entry.  */
10356   max_mp = mp;
10357
10358   /* Scan over the preceding entries and adjust their addresses as
10359      required.  */
10360   while (mp->prev != NULL
10361          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
10362     {
10363       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
10364       mp = mp->prev;
10365     }
10366
10367   return max_mp;
10368 }
10369
10370 static Mnode *
10371 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
10372                                 HOST_WIDE_INT  min_address)
10373 {
10374   HOST_WIDE_INT offset;
10375
10376   /* The code below assumes these are different.  */
10377   gcc_assert (mp != min_mp);
10378
10379   if (min_mp == NULL)
10380     {
10381       if (min_address > mp->min_address)
10382         mp->min_address = min_address;
10383     }
10384   else
10385     {
10386       /* We will adjust this below if it is too loose.  */
10387       mp->min_address = min_address;
10388
10389       /* Unlink MP from its current position.  Since min_mp is non-null,
10390          mp->next must be non-null.  */
10391       mp->next->prev = mp->prev;
10392       if (mp->prev != NULL)
10393         mp->prev->next = mp->next;
10394       else
10395         minipool_vector_head = mp->next;
10396
10397       /* Reinsert it after MIN_MP.  */
10398       mp->prev = min_mp;
10399       mp->next = min_mp->next;
10400       min_mp->next = mp;
10401       if (mp->next != NULL)
10402         mp->next->prev = mp;
10403       else
10404         minipool_vector_tail = mp;
10405     }
10406
10407   min_mp = mp;
10408
10409   offset = 0;
10410   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
10411     {
10412       mp->offset = offset;
10413       if (mp->refcount > 0)
10414         offset += mp->fix_size;
10415
10416       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
10417         mp->next->min_address = mp->min_address + mp->fix_size;
10418     }
10419
10420   return min_mp;
10421 }
10422
10423 /* Add a constant to the minipool for a backward reference.  Returns the
10424    node added or NULL if the constant will not fit in this pool.
10425
10426    Note that the code for insertion for a backwards reference can be
10427    somewhat confusing because the calculated offsets for each fix do
10428    not take into account the size of the pool (which is still under
10429    construction.  */
10430 static Mnode *
10431 add_minipool_backward_ref (Mfix *fix)
10432 {
10433   /* If set, min_mp is the last pool_entry that has a lower constraint
10434      than the one we are trying to add.  */
10435   Mnode *min_mp = NULL;
10436   /* This can be negative, since it is only a constraint.  */
10437   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
10438   Mnode *mp;
10439
10440   /* If we can't reach the current pool from this insn, or if we can't
10441      insert this entry at the end of the pool without pushing other
10442      fixes out of range, then we don't try.  This ensures that we
10443      can't fail later on.  */
10444   if (min_address >= minipool_barrier->address
10445       || (minipool_vector_tail->min_address + fix->fix_size
10446           >= minipool_barrier->address))
10447     return NULL;
10448
10449   /* Scan the pool to see if a constant with the same value has
10450      already been added.  While we are doing this, also note the
10451      location where we must insert the constant if it doesn't already
10452      exist.  */
10453   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
10454     {
10455       if (GET_CODE (fix->value) == GET_CODE (mp->value)
10456           && fix->mode == mp->mode
10457           && (GET_CODE (fix->value) != CODE_LABEL
10458               || (CODE_LABEL_NUMBER (fix->value)
10459                   == CODE_LABEL_NUMBER (mp->value)))
10460           && rtx_equal_p (fix->value, mp->value)
10461           /* Check that there is enough slack to move this entry to the
10462              end of the table (this is conservative).  */
10463           && (mp->max_address
10464               > (minipool_barrier->address
10465                  + minipool_vector_tail->offset
10466                  + minipool_vector_tail->fix_size)))
10467         {
10468           mp->refcount++;
10469           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
10470         }
10471
10472       if (min_mp != NULL)
10473         mp->min_address += fix->fix_size;
10474       else
10475         {
10476           /* Note the insertion point if necessary.  */
10477           if (mp->min_address < min_address)
10478             {
10479               /* For now, we do not allow the insertion of 8-byte alignment
10480                  requiring nodes anywhere but at the start of the pool.  */
10481               if (ARM_DOUBLEWORD_ALIGN
10482                   && fix->fix_size >= 8 && mp->fix_size < 8)
10483                 return NULL;
10484               else
10485                 min_mp = mp;
10486             }
10487           else if (mp->max_address
10488                    < minipool_barrier->address + mp->offset + fix->fix_size)
10489             {
10490               /* Inserting before this entry would push the fix beyond
10491                  its maximum address (which can happen if we have
10492                  re-located a forwards fix); force the new fix to come
10493                  after it.  */
10494               if (ARM_DOUBLEWORD_ALIGN
10495                   && fix->fix_size >= 8 && mp->fix_size < 8)
10496                 return NULL;
10497               else
10498                 {
10499                   min_mp = mp;
10500                   min_address = mp->min_address + fix->fix_size;
10501                 }
10502             }
10503           /* Do not insert a non-8-byte aligned quantity before 8-byte
10504              aligned quantities.  */
10505           else if (ARM_DOUBLEWORD_ALIGN
10506                    && fix->fix_size < 8
10507                    && mp->fix_size >= 8)
10508             {
10509               min_mp = mp;
10510               min_address = mp->min_address + fix->fix_size;
10511             }
10512         }
10513     }
10514
10515   /* We need to create a new entry.  */
10516   mp = XNEW (Mnode);
10517   mp->fix_size = fix->fix_size;
10518   mp->mode = fix->mode;
10519   mp->value = fix->value;
10520   mp->refcount = 1;
10521   mp->max_address = minipool_barrier->address + 65536;
10522
10523   mp->min_address = min_address;
10524
10525   if (min_mp == NULL)
10526     {
10527       mp->prev = NULL;
10528       mp->next = minipool_vector_head;
10529
10530       if (mp->next == NULL)
10531         {
10532           minipool_vector_tail = mp;
10533           minipool_vector_label = gen_label_rtx ();
10534         }
10535       else
10536         mp->next->prev = mp;
10537
10538       minipool_vector_head = mp;
10539     }
10540   else
10541     {
10542       mp->next = min_mp->next;
10543       mp->prev = min_mp;
10544       min_mp->next = mp;
10545
10546       if (mp->next != NULL)
10547         mp->next->prev = mp;
10548       else
10549         minipool_vector_tail = mp;
10550     }
10551
10552   /* Save the new entry.  */
10553   min_mp = mp;
10554
10555   if (mp->prev)
10556     mp = mp->prev;
10557   else
10558     mp->offset = 0;
10559
10560   /* Scan over the following entries and adjust their offsets.  */
10561   while (mp->next != NULL)
10562     {
10563       if (mp->next->min_address < mp->min_address + mp->fix_size)
10564         mp->next->min_address = mp->min_address + mp->fix_size;
10565
10566       if (mp->refcount)
10567         mp->next->offset = mp->offset + mp->fix_size;
10568       else
10569         mp->next->offset = mp->offset;
10570
10571       mp = mp->next;
10572     }
10573
10574   return min_mp;
10575 }
10576
10577 static void
10578 assign_minipool_offsets (Mfix *barrier)
10579 {
10580   HOST_WIDE_INT offset = 0;
10581   Mnode *mp;
10582
10583   minipool_barrier = barrier;
10584
10585   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
10586     {
10587       mp->offset = offset;
10588
10589       if (mp->refcount > 0)
10590         offset += mp->fix_size;
10591     }
10592 }
10593
10594 /* Output the literal table */
10595 static void
10596 dump_minipool (rtx scan)
10597 {
10598   Mnode * mp;
10599   Mnode * nmp;
10600   int align64 = 0;
10601
10602   if (ARM_DOUBLEWORD_ALIGN)
10603     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
10604       if (mp->refcount > 0 && mp->fix_size >= 8)
10605         {
10606           align64 = 1;
10607           break;
10608         }
10609
10610   if (dump_file)
10611     fprintf (dump_file,
10612              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
10613              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
10614
10615   scan = emit_label_after (gen_label_rtx (), scan);
10616   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
10617   scan = emit_label_after (minipool_vector_label, scan);
10618
10619   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
10620     {
10621       if (mp->refcount > 0)
10622         {
10623           if (dump_file)
10624             {
10625               fprintf (dump_file,
10626                        ";;  Offset %u, min %ld, max %ld ",
10627                        (unsigned) mp->offset, (unsigned long) mp->min_address,
10628                        (unsigned long) mp->max_address);
10629               arm_print_value (dump_file, mp->value);
10630               fputc ('\n', dump_file);
10631             }
10632
10633           switch (mp->fix_size)
10634             {
10635 #ifdef HAVE_consttable_1
10636             case 1:
10637               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
10638               break;
10639
10640 #endif
10641 #ifdef HAVE_consttable_2
10642             case 2:
10643               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
10644               break;
10645
10646 #endif
10647 #ifdef HAVE_consttable_4
10648             case 4:
10649               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
10650               break;
10651
10652 #endif
10653 #ifdef HAVE_consttable_8
10654             case 8:
10655               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
10656               break;
10657
10658 #endif
10659 #ifdef HAVE_consttable_16
10660             case 16:
10661               scan = emit_insn_after (gen_consttable_16 (mp->value), scan);
10662               break;
10663
10664 #endif
10665             default:
10666               gcc_unreachable ();
10667             }
10668         }
10669
10670       nmp = mp->next;
10671       free (mp);
10672     }
10673
10674   minipool_vector_head = minipool_vector_tail = NULL;
10675   scan = emit_insn_after (gen_consttable_end (), scan);
10676   scan = emit_barrier_after (scan);
10677 }
10678
10679 /* Return the cost of forcibly inserting a barrier after INSN.  */
10680 static int
10681 arm_barrier_cost (rtx insn)
10682 {
10683   /* Basing the location of the pool on the loop depth is preferable,
10684      but at the moment, the basic block information seems to be
10685      corrupt by this stage of the compilation.  */
10686   int base_cost = 50;
10687   rtx next = next_nonnote_insn (insn);
10688
10689   if (next != NULL && GET_CODE (next) == CODE_LABEL)
10690     base_cost -= 20;
10691
10692   switch (GET_CODE (insn))
10693     {
10694     case CODE_LABEL:
10695       /* It will always be better to place the table before the label, rather
10696          than after it.  */
10697       return 50;
10698
10699     case INSN:
10700     case CALL_INSN:
10701       return base_cost;
10702
10703     case JUMP_INSN:
10704       return base_cost - 10;
10705
10706     default:
10707       return base_cost + 10;
10708     }
10709 }
10710
10711 /* Find the best place in the insn stream in the range
10712    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
10713    Create the barrier by inserting a jump and add a new fix entry for
10714    it.  */
10715 static Mfix *
10716 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
10717 {
10718   HOST_WIDE_INT count = 0;
10719   rtx barrier;
10720   rtx from = fix->insn;
10721   /* The instruction after which we will insert the jump.  */
10722   rtx selected = NULL;
10723   int selected_cost;
10724   /* The address at which the jump instruction will be placed.  */
10725   HOST_WIDE_INT selected_address;
10726   Mfix * new_fix;
10727   HOST_WIDE_INT max_count = max_address - fix->address;
10728   rtx label = gen_label_rtx ();
10729
10730   selected_cost = arm_barrier_cost (from);
10731   selected_address = fix->address;
10732
10733   while (from && count < max_count)
10734     {
10735       rtx tmp;
10736       int new_cost;
10737
10738       /* This code shouldn't have been called if there was a natural barrier
10739          within range.  */
10740       gcc_assert (GET_CODE (from) != BARRIER);
10741
10742       /* Count the length of this insn.  */
10743       count += get_attr_length (from);
10744
10745       /* If there is a jump table, add its length.  */
10746       tmp = is_jump_table (from);
10747       if (tmp != NULL)
10748         {
10749           count += get_jump_table_size (tmp);
10750
10751           /* Jump tables aren't in a basic block, so base the cost on
10752              the dispatch insn.  If we select this location, we will
10753              still put the pool after the table.  */
10754           new_cost = arm_barrier_cost (from);
10755
10756           if (count < max_count 
10757               && (!selected || new_cost <= selected_cost))
10758             {
10759               selected = tmp;
10760               selected_cost = new_cost;
10761               selected_address = fix->address + count;
10762             }
10763
10764           /* Continue after the dispatch table.  */
10765           from = NEXT_INSN (tmp);
10766           continue;
10767         }
10768
10769       new_cost = arm_barrier_cost (from);
10770
10771       if (count < max_count
10772           && (!selected || new_cost <= selected_cost))
10773         {
10774           selected = from;
10775           selected_cost = new_cost;
10776           selected_address = fix->address + count;
10777         }
10778
10779       from = NEXT_INSN (from);
10780     }
10781
10782   /* Make sure that we found a place to insert the jump.  */
10783   gcc_assert (selected);
10784
10785   /* Create a new JUMP_INSN that branches around a barrier.  */
10786   from = emit_jump_insn_after (gen_jump (label), selected);
10787   JUMP_LABEL (from) = label;
10788   barrier = emit_barrier_after (from);
10789   emit_label_after (label, barrier);
10790
10791   /* Create a minipool barrier entry for the new barrier.  */
10792   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
10793   new_fix->insn = barrier;
10794   new_fix->address = selected_address;
10795   new_fix->next = fix->next;
10796   fix->next = new_fix;
10797
10798   return new_fix;
10799 }
10800
10801 /* Record that there is a natural barrier in the insn stream at
10802    ADDRESS.  */
10803 static void
10804 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
10805 {
10806   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
10807
10808   fix->insn = insn;
10809   fix->address = address;
10810
10811   fix->next = NULL;
10812   if (minipool_fix_head != NULL)
10813     minipool_fix_tail->next = fix;
10814   else
10815     minipool_fix_head = fix;
10816
10817   minipool_fix_tail = fix;
10818 }
10819
10820 /* Record INSN, which will need fixing up to load a value from the
10821    minipool.  ADDRESS is the offset of the insn since the start of the
10822    function; LOC is a pointer to the part of the insn which requires
10823    fixing; VALUE is the constant that must be loaded, which is of type
10824    MODE.  */
10825 static void
10826 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
10827                    enum machine_mode mode, rtx value)
10828 {
10829   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
10830
10831   fix->insn = insn;
10832   fix->address = address;
10833   fix->loc = loc;
10834   fix->mode = mode;
10835   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
10836   fix->value = value;
10837   fix->forwards = get_attr_pool_range (insn);
10838   fix->backwards = get_attr_neg_pool_range (insn);
10839   fix->minipool = NULL;
10840
10841   /* If an insn doesn't have a range defined for it, then it isn't
10842      expecting to be reworked by this code.  Better to stop now than
10843      to generate duff assembly code.  */
10844   gcc_assert (fix->forwards || fix->backwards);
10845
10846   /* If an entry requires 8-byte alignment then assume all constant pools
10847      require 4 bytes of padding.  Trying to do this later on a per-pool
10848      basis is awkward because existing pool entries have to be modified.  */
10849   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size >= 8)
10850     minipool_pad = 4;
10851
10852   if (dump_file)
10853     {
10854       fprintf (dump_file,
10855                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
10856                GET_MODE_NAME (mode),
10857                INSN_UID (insn), (unsigned long) address,
10858                -1 * (long)fix->backwards, (long)fix->forwards);
10859       arm_print_value (dump_file, fix->value);
10860       fprintf (dump_file, "\n");
10861     }
10862
10863   /* Add it to the chain of fixes.  */
10864   fix->next = NULL;
10865
10866   if (minipool_fix_head != NULL)
10867     minipool_fix_tail->next = fix;
10868   else
10869     minipool_fix_head = fix;
10870
10871   minipool_fix_tail = fix;
10872 }
10873
10874 /* Return the cost of synthesizing a 64-bit constant VAL inline.
10875    Returns the number of insns needed, or 99 if we don't know how to
10876    do it.  */
10877 int
10878 arm_const_double_inline_cost (rtx val)
10879 {
10880   rtx lowpart, highpart;
10881   enum machine_mode mode;
10882
10883   mode = GET_MODE (val);
10884
10885   if (mode == VOIDmode)
10886     mode = DImode;
10887
10888   gcc_assert (GET_MODE_SIZE (mode) == 8);
10889
10890   lowpart = gen_lowpart (SImode, val);
10891   highpart = gen_highpart_mode (SImode, mode, val);
10892
10893   gcc_assert (GET_CODE (lowpart) == CONST_INT);
10894   gcc_assert (GET_CODE (highpart) == CONST_INT);
10895
10896   return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
10897                             NULL_RTX, NULL_RTX, 0, 0)
10898           + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
10899                               NULL_RTX, NULL_RTX, 0, 0));
10900 }
10901
10902 /* Return true if it is worthwhile to split a 64-bit constant into two
10903    32-bit operations.  This is the case if optimizing for size, or
10904    if we have load delay slots, or if one 32-bit part can be done with
10905    a single data operation.  */
10906 bool
10907 arm_const_double_by_parts (rtx val)
10908 {
10909   enum machine_mode mode = GET_MODE (val);
10910   rtx part;
10911
10912   if (optimize_size || arm_ld_sched)
10913     return true;
10914
10915   if (mode == VOIDmode)
10916     mode = DImode;
10917
10918   part = gen_highpart_mode (SImode, mode, val);
10919
10920   gcc_assert (GET_CODE (part) == CONST_INT);
10921
10922   if (const_ok_for_arm (INTVAL (part))
10923       || const_ok_for_arm (~INTVAL (part)))
10924     return true;
10925
10926   part = gen_lowpart (SImode, val);
10927
10928   gcc_assert (GET_CODE (part) == CONST_INT);
10929
10930   if (const_ok_for_arm (INTVAL (part))
10931       || const_ok_for_arm (~INTVAL (part)))
10932     return true;
10933
10934   return false;
10935 }
10936
10937 /* Scan INSN and note any of its operands that need fixing.
10938    If DO_PUSHES is false we do not actually push any of the fixups
10939    needed.  The function returns TRUE if any fixups were needed/pushed.
10940    This is used by arm_memory_load_p() which needs to know about loads
10941    of constants that will be converted into minipool loads.  */
10942 static bool
10943 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
10944 {
10945   bool result = false;
10946   int opno;
10947
10948   extract_insn (insn);
10949
10950   if (!constrain_operands (1))
10951     fatal_insn_not_found (insn);
10952
10953   if (recog_data.n_alternatives == 0)
10954     return false;
10955
10956   /* Fill in recog_op_alt with information about the constraints of
10957      this insn.  */
10958   preprocess_constraints ();
10959
10960   for (opno = 0; opno < recog_data.n_operands; opno++)
10961     {
10962       /* Things we need to fix can only occur in inputs.  */
10963       if (recog_data.operand_type[opno] != OP_IN)
10964         continue;
10965
10966       /* If this alternative is a memory reference, then any mention
10967          of constants in this alternative is really to fool reload
10968          into allowing us to accept one there.  We need to fix them up
10969          now so that we output the right code.  */
10970       if (recog_op_alt[opno][which_alternative].memory_ok)
10971         {
10972           rtx op = recog_data.operand[opno];
10973
10974           if (CONSTANT_P (op))
10975             {
10976               if (do_pushes)
10977                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
10978                                    recog_data.operand_mode[opno], op);
10979               result = true;
10980             }
10981           else if (GET_CODE (op) == MEM
10982                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
10983                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
10984             {
10985               if (do_pushes)
10986                 {
10987                   rtx cop = avoid_constant_pool_reference (op);
10988
10989                   /* Casting the address of something to a mode narrower
10990                      than a word can cause avoid_constant_pool_reference()
10991                      to return the pool reference itself.  That's no good to
10992                      us here.  Lets just hope that we can use the
10993                      constant pool value directly.  */
10994                   if (op == cop)
10995                     cop = get_pool_constant (XEXP (op, 0));
10996
10997                   push_minipool_fix (insn, address,
10998                                      recog_data.operand_loc[opno],
10999                                      recog_data.operand_mode[opno], cop);
11000                 }
11001
11002               result = true;
11003             }
11004         }
11005     }
11006
11007   return result;
11008 }
11009
11010 /* Gcc puts the pool in the wrong place for ARM, since we can only
11011    load addresses a limited distance around the pc.  We do some
11012    special munging to move the constant pool values to the correct
11013    point in the code.  */
11014 static void
11015 arm_reorg (void)
11016 {
11017   rtx insn;
11018   HOST_WIDE_INT address = 0;
11019   Mfix * fix;
11020
11021   minipool_fix_head = minipool_fix_tail = NULL;
11022
11023   /* The first insn must always be a note, or the code below won't
11024      scan it properly.  */
11025   insn = get_insns ();
11026   gcc_assert (GET_CODE (insn) == NOTE);
11027   minipool_pad = 0;
11028
11029   /* Scan all the insns and record the operands that will need fixing.  */
11030   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
11031     {
11032       if (TARGET_CIRRUS_FIX_INVALID_INSNS
11033           && (arm_cirrus_insn_p (insn)
11034               || GET_CODE (insn) == JUMP_INSN
11035               || arm_memory_load_p (insn)))
11036         cirrus_reorg (insn);
11037
11038       if (GET_CODE (insn) == BARRIER)
11039         push_minipool_barrier (insn, address);
11040       else if (INSN_P (insn))
11041         {
11042           rtx table;
11043
11044           note_invalid_constants (insn, address, true);
11045           address += get_attr_length (insn);
11046
11047           /* If the insn is a vector jump, add the size of the table
11048              and skip the table.  */
11049           if ((table = is_jump_table (insn)) != NULL)
11050             {
11051               address += get_jump_table_size (table);
11052               insn = table;
11053             }
11054         }
11055     }
11056
11057   fix = minipool_fix_head;
11058
11059   /* Now scan the fixups and perform the required changes.  */
11060   while (fix)
11061     {
11062       Mfix * ftmp;
11063       Mfix * fdel;
11064       Mfix *  last_added_fix;
11065       Mfix * last_barrier = NULL;
11066       Mfix * this_fix;
11067
11068       /* Skip any further barriers before the next fix.  */
11069       while (fix && GET_CODE (fix->insn) == BARRIER)
11070         fix = fix->next;
11071
11072       /* No more fixes.  */
11073       if (fix == NULL)
11074         break;
11075
11076       last_added_fix = NULL;
11077
11078       for (ftmp = fix; ftmp; ftmp = ftmp->next)
11079         {
11080           if (GET_CODE (ftmp->insn) == BARRIER)
11081             {
11082               if (ftmp->address >= minipool_vector_head->max_address)
11083                 break;
11084
11085               last_barrier = ftmp;
11086             }
11087           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
11088             break;
11089
11090           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
11091         }
11092
11093       /* If we found a barrier, drop back to that; any fixes that we
11094          could have reached but come after the barrier will now go in
11095          the next mini-pool.  */
11096       if (last_barrier != NULL)
11097         {
11098           /* Reduce the refcount for those fixes that won't go into this
11099              pool after all.  */
11100           for (fdel = last_barrier->next;
11101                fdel && fdel != ftmp;
11102                fdel = fdel->next)
11103             {
11104               fdel->minipool->refcount--;
11105               fdel->minipool = NULL;
11106             }
11107
11108           ftmp = last_barrier;
11109         }
11110       else
11111         {
11112           /* ftmp is first fix that we can't fit into this pool and
11113              there no natural barriers that we could use.  Insert a
11114              new barrier in the code somewhere between the previous
11115              fix and this one, and arrange to jump around it.  */
11116           HOST_WIDE_INT max_address;
11117
11118           /* The last item on the list of fixes must be a barrier, so
11119              we can never run off the end of the list of fixes without
11120              last_barrier being set.  */
11121           gcc_assert (ftmp);
11122
11123           max_address = minipool_vector_head->max_address;
11124           /* Check that there isn't another fix that is in range that
11125              we couldn't fit into this pool because the pool was
11126              already too large: we need to put the pool before such an
11127              instruction.  The pool itself may come just after the
11128              fix because create_fix_barrier also allows space for a
11129              jump instruction.  */
11130           if (ftmp->address < max_address)
11131             max_address = ftmp->address + 1;
11132
11133           last_barrier = create_fix_barrier (last_added_fix, max_address);
11134         }
11135
11136       assign_minipool_offsets (last_barrier);
11137
11138       while (ftmp)
11139         {
11140           if (GET_CODE (ftmp->insn) != BARRIER
11141               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
11142                   == NULL))
11143             break;
11144
11145           ftmp = ftmp->next;
11146         }
11147
11148       /* Scan over the fixes we have identified for this pool, fixing them
11149          up and adding the constants to the pool itself.  */
11150       for (this_fix = fix; this_fix && ftmp != this_fix;
11151            this_fix = this_fix->next)
11152         if (GET_CODE (this_fix->insn) != BARRIER)
11153           {
11154             rtx addr
11155               = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
11156                                                   minipool_vector_label),
11157                                this_fix->minipool->offset);
11158             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
11159           }
11160
11161       dump_minipool (last_barrier->insn);
11162       fix = ftmp;
11163     }
11164
11165   /* From now on we must synthesize any constants that we can't handle
11166      directly.  This can happen if the RTL gets split during final
11167      instruction generation.  */
11168   after_arm_reorg = 1;
11169
11170   /* Free the minipool memory.  */
11171   obstack_free (&minipool_obstack, minipool_startobj);
11172 }
11173 \f
11174 /* Routines to output assembly language.  */
11175
11176 /* If the rtx is the correct value then return the string of the number.
11177    In this way we can ensure that valid double constants are generated even
11178    when cross compiling.  */
11179 const char *
11180 fp_immediate_constant (rtx x)
11181 {
11182   REAL_VALUE_TYPE r;
11183   int i;
11184
11185   if (!fp_consts_inited)
11186     init_fp_table ();
11187
11188   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
11189   for (i = 0; i < 8; i++)
11190     if (REAL_VALUES_EQUAL (r, values_fp[i]))
11191       return strings_fp[i];
11192
11193   gcc_unreachable ();
11194 }
11195
11196 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
11197 static const char *
11198 fp_const_from_val (REAL_VALUE_TYPE *r)
11199 {
11200   int i;
11201
11202   if (!fp_consts_inited)
11203     init_fp_table ();
11204
11205   for (i = 0; i < 8; i++)
11206     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
11207       return strings_fp[i];
11208
11209   gcc_unreachable ();
11210 }
11211
11212 /* Output the operands of a LDM/STM instruction to STREAM.
11213    MASK is the ARM register set mask of which only bits 0-15 are important.
11214    REG is the base register, either the frame pointer or the stack pointer,
11215    INSTR is the possibly suffixed load or store instruction.
11216    RFE is nonzero if the instruction should also copy spsr to cpsr.  */
11217
11218 static void
11219 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
11220                  unsigned long mask, int rfe)
11221 {
11222   unsigned i;
11223   bool not_first = FALSE;
11224
11225   gcc_assert (!rfe || (mask & (1 << PC_REGNUM)));
11226   fputc ('\t', stream);
11227   asm_fprintf (stream, instr, reg);
11228   fputc ('{', stream);
11229
11230   for (i = 0; i <= LAST_ARM_REGNUM; i++)
11231     if (mask & (1 << i))
11232       {
11233         if (not_first)
11234           fprintf (stream, ", ");
11235
11236         asm_fprintf (stream, "%r", i);
11237         not_first = TRUE;
11238       }
11239
11240   if (rfe)
11241     fprintf (stream, "}^\n");
11242   else
11243     fprintf (stream, "}\n");
11244 }
11245
11246
11247 /* Output a FLDMD instruction to STREAM.
11248    BASE if the register containing the address.
11249    REG and COUNT specify the register range.
11250    Extra registers may be added to avoid hardware bugs.
11251
11252    We output FLDMD even for ARMv5 VFP implementations.  Although
11253    FLDMD is technically not supported until ARMv6, it is believed
11254    that all VFP implementations support its use in this context.  */
11255
11256 static void
11257 vfp_output_fldmd (FILE * stream, unsigned int base, int reg, int count)
11258 {
11259   int i;
11260
11261   /* Workaround ARM10 VFPr1 bug.  */
11262   if (count == 2 && !arm_arch6)
11263     {
11264       if (reg == 15)
11265         reg--;
11266       count++;
11267     }
11268
11269   /* FLDMD may not load more than 16 doubleword registers at a time. Split the
11270      load into multiple parts if we have to handle more than 16 registers.  */
11271   if (count > 16)
11272     {
11273       vfp_output_fldmd (stream, base, reg, 16);
11274       vfp_output_fldmd (stream, base, reg + 16, count - 16);
11275       return;
11276     }
11277
11278   fputc ('\t', stream);
11279   asm_fprintf (stream, "fldmfdd\t%r!, {", base);
11280
11281   for (i = reg; i < reg + count; i++)
11282     {
11283       if (i > reg)
11284         fputs (", ", stream);
11285       asm_fprintf (stream, "d%d", i);
11286     }
11287   fputs ("}\n", stream);
11288
11289 }
11290
11291
11292 /* Output the assembly for a store multiple.  */
11293
11294 const char *
11295 vfp_output_fstmd (rtx * operands)
11296 {
11297   char pattern[100];
11298   int p;
11299   int base;
11300   int i;
11301
11302   strcpy (pattern, "fstmfdd\t%m0!, {%P1");
11303   p = strlen (pattern);
11304
11305   gcc_assert (GET_CODE (operands[1]) == REG);
11306
11307   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
11308   for (i = 1; i < XVECLEN (operands[2], 0); i++)
11309     {
11310       p += sprintf (&pattern[p], ", d%d", base + i);
11311     }
11312   strcpy (&pattern[p], "}");
11313
11314   output_asm_insn (pattern, operands);
11315   return "";
11316 }
11317
11318
11319 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
11320    number of bytes pushed.  */
11321
11322 static int
11323 vfp_emit_fstmd (int base_reg, int count)
11324 {
11325   rtx par;
11326   rtx dwarf;
11327   rtx tmp, reg;
11328   int i;
11329
11330   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
11331      register pairs are stored by a store multiple insn.  We avoid this
11332      by pushing an extra pair.  */
11333   if (count == 2 && !arm_arch6)
11334     {
11335       if (base_reg == LAST_VFP_REGNUM - 3)
11336         base_reg -= 2;
11337       count++;
11338     }
11339
11340   /* FSTMD may not store more than 16 doubleword registers at once.  Split
11341      larger stores into multiple parts (up to a maximum of two, in
11342      practice).  */
11343   if (count > 16)
11344     {
11345       int saved;
11346       /* NOTE: base_reg is an internal register number, so each D register
11347          counts as 2.  */
11348       saved = vfp_emit_fstmd (base_reg + 32, count - 16);
11349       saved += vfp_emit_fstmd (base_reg, 16);
11350       return saved;
11351     }
11352
11353   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
11354   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
11355
11356   reg = gen_rtx_REG (DFmode, base_reg);
11357   base_reg += 2;
11358
11359   XVECEXP (par, 0, 0)
11360     = gen_rtx_SET (VOIDmode,
11361                    gen_frame_mem (BLKmode,
11362                                   gen_rtx_PRE_DEC (BLKmode,
11363                                                    stack_pointer_rtx)),
11364                    gen_rtx_UNSPEC (BLKmode,
11365                                    gen_rtvec (1, reg),
11366                                    UNSPEC_PUSH_MULT));
11367
11368   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
11369                      plus_constant (stack_pointer_rtx, -(count * 8)));
11370   RTX_FRAME_RELATED_P (tmp) = 1;
11371   XVECEXP (dwarf, 0, 0) = tmp;
11372
11373   tmp = gen_rtx_SET (VOIDmode,
11374                      gen_frame_mem (DFmode, stack_pointer_rtx),
11375                      reg);
11376   RTX_FRAME_RELATED_P (tmp) = 1;
11377   XVECEXP (dwarf, 0, 1) = tmp;
11378
11379   for (i = 1; i < count; i++)
11380     {
11381       reg = gen_rtx_REG (DFmode, base_reg);
11382       base_reg += 2;
11383       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
11384
11385       tmp = gen_rtx_SET (VOIDmode,
11386                          gen_frame_mem (DFmode,
11387                                         plus_constant (stack_pointer_rtx,
11388                                                        i * 8)),
11389                          reg);
11390       RTX_FRAME_RELATED_P (tmp) = 1;
11391       XVECEXP (dwarf, 0, i + 1) = tmp;
11392     }
11393
11394   par = emit_insn (par);
11395   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
11396   RTX_FRAME_RELATED_P (par) = 1;
11397
11398   return count * 8;
11399 }
11400
11401 /* Emit a call instruction with pattern PAT.  ADDR is the address of
11402    the call target.  */
11403
11404 void
11405 arm_emit_call_insn (rtx pat, rtx addr)
11406 {
11407   rtx insn;
11408
11409   insn = emit_call_insn (pat);
11410
11411   /* The PIC register is live on entry to VxWorks PIC PLT entries.
11412      If the call might use such an entry, add a use of the PIC register
11413      to the instruction's CALL_INSN_FUNCTION_USAGE.  */
11414   if (TARGET_VXWORKS_RTP
11415       && flag_pic
11416       && GET_CODE (addr) == SYMBOL_REF
11417       && (SYMBOL_REF_DECL (addr)
11418           ? !targetm.binds_local_p (SYMBOL_REF_DECL (addr))
11419           : !SYMBOL_REF_LOCAL_P (addr)))
11420     {
11421       require_pic_register ();
11422       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), cfun->machine->pic_reg);
11423     }
11424 }
11425
11426 /* Output a 'call' insn.  */
11427 const char *
11428 output_call (rtx *operands)
11429 {
11430   gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly.  */
11431
11432   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
11433   if (REGNO (operands[0]) == LR_REGNUM)
11434     {
11435       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
11436       output_asm_insn ("mov%?\t%0, %|lr", operands);
11437     }
11438
11439   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
11440
11441   if (TARGET_INTERWORK || arm_arch4t)
11442     output_asm_insn ("bx%?\t%0", operands);
11443   else
11444     output_asm_insn ("mov%?\t%|pc, %0", operands);
11445
11446   return "";
11447 }
11448
11449 /* Output a 'call' insn that is a reference in memory.  */
11450 const char *
11451 output_call_mem (rtx *operands)
11452 {
11453   if (TARGET_INTERWORK && !arm_arch5)
11454     {
11455       output_asm_insn ("ldr%?\t%|ip, %0", operands);
11456       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
11457       output_asm_insn ("bx%?\t%|ip", operands);
11458     }
11459   else if (regno_use_in (LR_REGNUM, operands[0]))
11460     {
11461       /* LR is used in the memory address.  We load the address in the
11462          first instruction.  It's safe to use IP as the target of the
11463          load since the call will kill it anyway.  */
11464       output_asm_insn ("ldr%?\t%|ip, %0", operands);
11465       if (arm_arch5)
11466         output_asm_insn ("blx%?\t%|ip", operands);
11467       else
11468         {
11469           output_asm_insn ("mov%?\t%|lr, %|pc", operands);
11470           if (arm_arch4t)
11471             output_asm_insn ("bx%?\t%|ip", operands);
11472           else
11473             output_asm_insn ("mov%?\t%|pc, %|ip", operands);
11474         }
11475     }
11476   else
11477     {
11478       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
11479       output_asm_insn ("ldr%?\t%|pc, %0", operands);
11480     }
11481
11482   return "";
11483 }
11484
11485
11486 /* Output a move from arm registers to an fpa registers.
11487    OPERANDS[0] is an fpa register.
11488    OPERANDS[1] is the first registers of an arm register pair.  */
11489 const char *
11490 output_mov_long_double_fpa_from_arm (rtx *operands)
11491 {
11492   int arm_reg0 = REGNO (operands[1]);
11493   rtx ops[3];
11494
11495   gcc_assert (arm_reg0 != IP_REGNUM);
11496
11497   ops[0] = gen_rtx_REG (SImode, arm_reg0);
11498   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
11499   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
11500
11501   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
11502   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
11503
11504   return "";
11505 }
11506
11507 /* Output a move from an fpa register to arm registers.
11508    OPERANDS[0] is the first registers of an arm register pair.
11509    OPERANDS[1] is an fpa register.  */
11510 const char *
11511 output_mov_long_double_arm_from_fpa (rtx *operands)
11512 {
11513   int arm_reg0 = REGNO (operands[0]);
11514   rtx ops[3];
11515
11516   gcc_assert (arm_reg0 != IP_REGNUM);
11517
11518   ops[0] = gen_rtx_REG (SImode, arm_reg0);
11519   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
11520   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
11521
11522   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
11523   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
11524   return "";
11525 }
11526
11527 /* Output a move from arm registers to arm registers of a long double
11528    OPERANDS[0] is the destination.
11529    OPERANDS[1] is the source.  */
11530 const char *
11531 output_mov_long_double_arm_from_arm (rtx *operands)
11532 {
11533   /* We have to be careful here because the two might overlap.  */
11534   int dest_start = REGNO (operands[0]);
11535   int src_start = REGNO (operands[1]);
11536   rtx ops[2];
11537   int i;
11538
11539   if (dest_start < src_start)
11540     {
11541       for (i = 0; i < 3; i++)
11542         {
11543           ops[0] = gen_rtx_REG (SImode, dest_start + i);
11544           ops[1] = gen_rtx_REG (SImode, src_start + i);
11545           output_asm_insn ("mov%?\t%0, %1", ops);
11546         }
11547     }
11548   else
11549     {
11550       for (i = 2; i >= 0; i--)
11551         {
11552           ops[0] = gen_rtx_REG (SImode, dest_start + i);
11553           ops[1] = gen_rtx_REG (SImode, src_start + i);
11554           output_asm_insn ("mov%?\t%0, %1", ops);
11555         }
11556     }
11557
11558   return "";
11559 }
11560
11561
11562 /* Emit a MOVW/MOVT pair.  */
11563 void arm_emit_movpair (rtx dest, rtx src)
11564 {
11565   emit_set_insn (dest, gen_rtx_HIGH (SImode, src));
11566   emit_set_insn (dest, gen_rtx_LO_SUM (SImode, dest, src));
11567 }
11568
11569
11570 /* Output a move from arm registers to an fpa registers.
11571    OPERANDS[0] is an fpa register.
11572    OPERANDS[1] is the first registers of an arm register pair.  */
11573 const char *
11574 output_mov_double_fpa_from_arm (rtx *operands)
11575 {
11576   int arm_reg0 = REGNO (operands[1]);
11577   rtx ops[2];
11578
11579   gcc_assert (arm_reg0 != IP_REGNUM);
11580
11581   ops[0] = gen_rtx_REG (SImode, arm_reg0);
11582   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
11583   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1}", ops);
11584   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
11585   return "";
11586 }
11587
11588 /* Output a move from an fpa register to arm registers.
11589    OPERANDS[0] is the first registers of an arm register pair.
11590    OPERANDS[1] is an fpa register.  */
11591 const char *
11592 output_mov_double_arm_from_fpa (rtx *operands)
11593 {
11594   int arm_reg0 = REGNO (operands[0]);
11595   rtx ops[2];
11596
11597   gcc_assert (arm_reg0 != IP_REGNUM);
11598
11599   ops[0] = gen_rtx_REG (SImode, arm_reg0);
11600   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
11601   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
11602   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1}", ops);
11603   return "";
11604 }
11605
11606 /* Output a move between double words.
11607    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
11608    or MEM<-REG and all MEMs must be offsettable addresses.  */
11609 const char *
11610 output_move_double (rtx *operands)
11611 {
11612   enum rtx_code code0 = GET_CODE (operands[0]);
11613   enum rtx_code code1 = GET_CODE (operands[1]);
11614   rtx otherops[3];
11615
11616   if (code0 == REG)
11617     {
11618       unsigned int reg0 = REGNO (operands[0]);
11619
11620       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
11621
11622       gcc_assert (code1 == MEM);  /* Constraints should ensure this.  */
11623
11624       switch (GET_CODE (XEXP (operands[1], 0)))
11625         {
11626         case REG:
11627           if (TARGET_LDRD
11628               && !(fix_cm3_ldrd && reg0 == REGNO(XEXP (operands[1], 0))))
11629             output_asm_insn ("ldr%(d%)\t%0, [%m1]", operands);
11630           else
11631             output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
11632           break;
11633
11634         case PRE_INC:
11635           gcc_assert (TARGET_LDRD);
11636           output_asm_insn ("ldr%(d%)\t%0, [%m1, #8]!", operands);
11637           break;
11638
11639         case PRE_DEC:
11640           if (TARGET_LDRD)
11641             output_asm_insn ("ldr%(d%)\t%0, [%m1, #-8]!", operands);
11642           else
11643             output_asm_insn ("ldm%(db%)\t%m1!, %M0", operands);
11644           break;
11645
11646         case POST_INC:
11647           if (TARGET_LDRD)
11648             output_asm_insn ("ldr%(d%)\t%0, [%m1], #8", operands);
11649           else
11650             output_asm_insn ("ldm%(ia%)\t%m1!, %M0", operands);
11651           break;
11652
11653         case POST_DEC:
11654           gcc_assert (TARGET_LDRD);
11655           output_asm_insn ("ldr%(d%)\t%0, [%m1], #-8", operands);
11656           break;
11657
11658         case PRE_MODIFY:
11659         case POST_MODIFY:
11660           /* Autoicrement addressing modes should never have overlapping
11661              base and destination registers, and overlapping index registers
11662              are already prohibited, so this doesn't need to worry about
11663              fix_cm3_ldrd.  */
11664           otherops[0] = operands[0];
11665           otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
11666           otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
11667
11668           if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
11669             {
11670               if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
11671                 {
11672                   /* Registers overlap so split out the increment.  */
11673                   output_asm_insn ("add%?\t%1, %1, %2", otherops);
11674                   output_asm_insn ("ldr%(d%)\t%0, [%1] @split", otherops);
11675                 }
11676               else
11677                 {
11678                   /* Use a single insn if we can.
11679                      FIXME: IWMMXT allows offsets larger than ldrd can
11680                      handle, fix these up with a pair of ldr.  */
11681                   if (TARGET_THUMB2
11682                       || GET_CODE (otherops[2]) != CONST_INT
11683                       || (INTVAL (otherops[2]) > -256
11684                           && INTVAL (otherops[2]) < 256))
11685                     output_asm_insn ("ldr%(d%)\t%0, [%1, %2]!", otherops);
11686                   else
11687                     {
11688                       output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
11689                       output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
11690                     }
11691                 }
11692             }
11693           else
11694             {
11695               /* Use a single insn if we can.
11696                  FIXME: IWMMXT allows offsets larger than ldrd can handle,
11697                  fix these up with a pair of ldr.  */
11698               if (TARGET_THUMB2
11699                   || GET_CODE (otherops[2]) != CONST_INT
11700                   || (INTVAL (otherops[2]) > -256
11701                       && INTVAL (otherops[2]) < 256))
11702                 output_asm_insn ("ldr%(d%)\t%0, [%1], %2", otherops);
11703               else
11704                 {
11705                   output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
11706                   output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
11707                 }
11708             }
11709           break;
11710
11711         case LABEL_REF:
11712         case CONST:
11713           /* We might be able to use ldrd %0, %1 here.  However the range is
11714              different to ldr/adr, and it is broken on some ARMv7-M
11715              implementations.  */
11716           /* Use the second register of the pair to avoid problematic
11717              overlap.  */
11718           otherops[1] = operands[1];
11719           output_asm_insn ("adr%?\t%0, %1", otherops);
11720           operands[1] = otherops[0];
11721           if (TARGET_LDRD)
11722             output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
11723           else
11724             output_asm_insn ("ldm%(ia%)\t%1, %M0", operands);
11725           break;
11726
11727           /* ??? This needs checking for thumb2.  */
11728         default:
11729           if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
11730                                GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
11731             {
11732               otherops[0] = operands[0];
11733               otherops[1] = XEXP (XEXP (operands[1], 0), 0);
11734               otherops[2] = XEXP (XEXP (operands[1], 0), 1);
11735
11736               if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
11737                 {
11738                   if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
11739                     {
11740                       switch ((int) INTVAL (otherops[2]))
11741                         {
11742                         case -8:
11743                           output_asm_insn ("ldm%(db%)\t%1, %M0", otherops);
11744                           return "";
11745                         case -4:
11746                           if (TARGET_THUMB2)
11747                             break;
11748                           output_asm_insn ("ldm%(da%)\t%1, %M0", otherops);
11749                           return "";
11750                         case 4:
11751                           if (TARGET_THUMB2)
11752                             break;
11753                           output_asm_insn ("ldm%(ib%)\t%1, %M0", otherops);
11754                           return "";
11755                         }
11756                     }
11757                   otherops[0] = gen_rtx_REG(SImode, REGNO(operands[0]) + 1);
11758                   operands[1] = otherops[0];
11759                   if (TARGET_LDRD
11760                       && (GET_CODE (otherops[2]) == REG
11761                           || TARGET_THUMB2
11762                           || (GET_CODE (otherops[2]) == CONST_INT
11763                               && INTVAL (otherops[2]) > -256
11764                               && INTVAL (otherops[2]) < 256)))
11765                     {
11766                       if (reg_overlap_mentioned_p (operands[0],
11767                                                    otherops[2]))
11768                         {
11769                           rtx tmp;
11770                           /* Swap base and index registers over to
11771                              avoid a conflict.  */
11772                           tmp = otherops[1];
11773                           otherops[1] = otherops[2];
11774                           otherops[2] = tmp;
11775                         }
11776                       /* If both registers conflict, it will usually
11777                          have been fixed by a splitter.  */
11778                       if (reg_overlap_mentioned_p (operands[0], otherops[2])
11779                           || (fix_cm3_ldrd && reg0 == REGNO (otherops[1])))
11780                         {
11781                           output_asm_insn ("add%?\t%0, %1, %2", otherops);
11782                           output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
11783                         }
11784                       else
11785                         {
11786                           otherops[0] = operands[0];
11787                           output_asm_insn ("ldr%(d%)\t%0, [%1, %2]", otherops);
11788                         }
11789                       return "";
11790                     }
11791
11792                   if (GET_CODE (otherops[2]) == CONST_INT)
11793                     {
11794                       if (!(const_ok_for_arm (INTVAL (otherops[2]))))
11795                         output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
11796                       else
11797                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
11798                     }
11799                   else
11800                     output_asm_insn ("add%?\t%0, %1, %2", otherops);
11801                 }
11802               else
11803                 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
11804
11805               if (TARGET_LDRD)
11806                 return "ldr%(d%)\t%0, [%1]";
11807
11808               return "ldm%(ia%)\t%1, %M0";
11809             }
11810           else
11811             {
11812               otherops[1] = adjust_address (operands[1], SImode, 4);
11813               /* Take care of overlapping base/data reg.  */
11814               if (reg_mentioned_p (operands[0], operands[1]))
11815                 {
11816                   output_asm_insn ("ldr%?\t%0, %1", otherops);
11817                   output_asm_insn ("ldr%?\t%0, %1", operands);
11818                 }
11819               else
11820                 {
11821                   output_asm_insn ("ldr%?\t%0, %1", operands);
11822                   output_asm_insn ("ldr%?\t%0, %1", otherops);
11823                 }
11824             }
11825         }
11826     }
11827   else
11828     {
11829       /* Constraints should ensure this.  */
11830       gcc_assert (code0 == MEM && code1 == REG);
11831       gcc_assert (REGNO (operands[1]) != IP_REGNUM);
11832
11833       switch (GET_CODE (XEXP (operands[0], 0)))
11834         {
11835         case REG:
11836           if (TARGET_LDRD)
11837             output_asm_insn ("str%(d%)\t%1, [%m0]", operands);
11838           else
11839             output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
11840           break;
11841
11842         case PRE_INC:
11843           gcc_assert (TARGET_LDRD);
11844           output_asm_insn ("str%(d%)\t%1, [%m0, #8]!", operands);
11845           break;
11846
11847         case PRE_DEC:
11848           if (TARGET_LDRD)
11849             output_asm_insn ("str%(d%)\t%1, [%m0, #-8]!", operands);
11850           else
11851             output_asm_insn ("stm%(db%)\t%m0!, %M1", operands);
11852           break;
11853
11854         case POST_INC:
11855           if (TARGET_LDRD)
11856             output_asm_insn ("str%(d%)\t%1, [%m0], #8", operands);
11857           else
11858             output_asm_insn ("stm%(ia%)\t%m0!, %M1", operands);
11859           break;
11860
11861         case POST_DEC:
11862           gcc_assert (TARGET_LDRD);
11863           output_asm_insn ("str%(d%)\t%1, [%m0], #-8", operands);
11864           break;
11865
11866         case PRE_MODIFY:
11867         case POST_MODIFY:
11868           otherops[0] = operands[1];
11869           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
11870           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
11871
11872           /* IWMMXT allows offsets larger than ldrd can handle,
11873              fix these up with a pair of ldr.  */
11874           if (!TARGET_THUMB2
11875               && GET_CODE (otherops[2]) == CONST_INT
11876               && (INTVAL(otherops[2]) <= -256
11877                   || INTVAL(otherops[2]) >= 256))
11878             {
11879               if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
11880                 {
11881                   output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
11882                   output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
11883                 }
11884               else
11885                 {
11886                   output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
11887                   output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
11888                 }
11889             }
11890           else if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
11891             output_asm_insn ("str%(d%)\t%0, [%1, %2]!", otherops);
11892           else
11893             output_asm_insn ("str%(d%)\t%0, [%1], %2", otherops);
11894           break;
11895
11896         case PLUS:
11897           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
11898           if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
11899             {
11900               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
11901                 {
11902                 case -8:
11903                   output_asm_insn ("stm%(db%)\t%m0, %M1", operands);
11904                   return "";
11905
11906                 case -4:
11907                   if (TARGET_THUMB2)
11908                     break;
11909                   output_asm_insn ("stm%(da%)\t%m0, %M1", operands);
11910                   return "";
11911
11912                 case 4:
11913                   if (TARGET_THUMB2)
11914                     break;
11915                   output_asm_insn ("stm%(ib%)\t%m0, %M1", operands);
11916                   return "";
11917                 }
11918             }
11919           if (TARGET_LDRD
11920               && (GET_CODE (otherops[2]) == REG
11921                   || TARGET_THUMB2
11922                   || (GET_CODE (otherops[2]) == CONST_INT
11923                       && INTVAL (otherops[2]) > -256
11924                       && INTVAL (otherops[2]) < 256)))
11925             {
11926               otherops[0] = operands[1];
11927               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
11928               output_asm_insn ("str%(d%)\t%0, [%1, %2]", otherops);
11929               return "";
11930             }
11931           /* Fall through */
11932
11933         default:
11934           otherops[0] = adjust_address (operands[0], SImode, 4);
11935           otherops[1] = operands[1];
11936           output_asm_insn ("str%?\t%1, %0", operands);
11937           output_asm_insn ("str%?\t%H1, %0", otherops);
11938         }
11939     }
11940
11941   return "";
11942 }
11943
11944 /* Output a move, load or store for quad-word vectors in ARM registers.  Only
11945    handles MEMs accepted by neon_vector_mem_operand with TYPE=1.  */
11946
11947 const char *
11948 output_move_quad (rtx *operands)
11949 {
11950   if (REG_P (operands[0]))
11951     {
11952       /* Load, or reg->reg move.  */
11953
11954       if (MEM_P (operands[1]))
11955         {
11956           switch (GET_CODE (XEXP (operands[1], 0)))
11957             {
11958             case REG:
11959               output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
11960               break;
11961
11962             case LABEL_REF:
11963             case CONST:
11964               output_asm_insn ("adr%?\t%0, %1", operands);
11965               output_asm_insn ("ldm%(ia%)\t%0, %M0", operands);
11966               break;
11967
11968             default:
11969               gcc_unreachable ();
11970             }
11971         }
11972       else
11973         {
11974           rtx ops[2];
11975           int dest, src, i;
11976
11977           gcc_assert (REG_P (operands[1]));
11978
11979           dest = REGNO (operands[0]);
11980           src = REGNO (operands[1]);
11981
11982           /* This seems pretty dumb, but hopefully GCC won't try to do it
11983              very often.  */
11984           if (dest < src)
11985             for (i = 0; i < 4; i++)
11986               {
11987                 ops[0] = gen_rtx_REG (SImode, dest + i);
11988                 ops[1] = gen_rtx_REG (SImode, src + i);
11989                 output_asm_insn ("mov%?\t%0, %1", ops);
11990               }
11991           else
11992             for (i = 3; i >= 0; i--)
11993               {
11994                 ops[0] = gen_rtx_REG (SImode, dest + i);
11995                 ops[1] = gen_rtx_REG (SImode, src + i);
11996                 output_asm_insn ("mov%?\t%0, %1", ops);
11997               }
11998         }
11999     }
12000   else
12001     {
12002       gcc_assert (MEM_P (operands[0]));
12003       gcc_assert (REG_P (operands[1]));
12004       gcc_assert (!reg_overlap_mentioned_p (operands[1], operands[0]));
12005
12006       switch (GET_CODE (XEXP (operands[0], 0)))
12007         {
12008         case REG:
12009           output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
12010           break;
12011
12012         default:
12013           gcc_unreachable ();
12014         }
12015     }
12016
12017   return "";
12018 }
12019
12020 /* Output a VFP load or store instruction.  */
12021
12022 const char *
12023 output_move_vfp (rtx *operands)
12024 {
12025   rtx reg, mem, addr, ops[2];
12026   int load = REG_P (operands[0]);
12027   int dp = GET_MODE_SIZE (GET_MODE (operands[0])) == 8;
12028   int integer_p = GET_MODE_CLASS (GET_MODE (operands[0])) == MODE_INT;
12029   const char *templ;
12030   char buff[50];
12031   enum machine_mode mode;
12032
12033   reg = operands[!load];
12034   mem = operands[load];
12035
12036   mode = GET_MODE (reg);
12037
12038   gcc_assert (REG_P (reg));
12039   gcc_assert (IS_VFP_REGNUM (REGNO (reg)));
12040   gcc_assert (mode == SFmode
12041               || mode == DFmode
12042               || mode == SImode
12043               || mode == DImode
12044               || (TARGET_NEON && VALID_NEON_DREG_MODE (mode)));
12045   gcc_assert (MEM_P (mem));
12046
12047   addr = XEXP (mem, 0);
12048
12049   switch (GET_CODE (addr))
12050     {
12051     case PRE_DEC:
12052       templ = "f%smdb%c%%?\t%%0!, {%%%s1}%s";
12053       ops[0] = XEXP (addr, 0);
12054       ops[1] = reg;
12055       break;
12056
12057     case POST_INC:
12058       templ = "f%smia%c%%?\t%%0!, {%%%s1}%s";
12059       ops[0] = XEXP (addr, 0);
12060       ops[1] = reg;
12061       break;
12062
12063     default:
12064       templ = "f%s%c%%?\t%%%s0, %%1%s";
12065       ops[0] = reg;
12066       ops[1] = mem;
12067       break;
12068     }
12069
12070   sprintf (buff, templ,
12071            load ? "ld" : "st",
12072            dp ? 'd' : 's',
12073            dp ? "P" : "",
12074            integer_p ? "\t%@ int" : "");
12075   output_asm_insn (buff, ops);
12076
12077   return "";
12078 }
12079
12080 /* Output a Neon quad-word load or store, or a load or store for
12081    larger structure modes.
12082
12083    WARNING: The ordering of elements is weird in big-endian mode,
12084    because we use VSTM, as required by the EABI.  GCC RTL defines
12085    element ordering based on in-memory order.  This can be differ
12086    from the architectural ordering of elements within a NEON register.
12087    The intrinsics defined in arm_neon.h use the NEON register element
12088    ordering, not the GCC RTL element ordering.
12089
12090    For example, the in-memory ordering of a big-endian a quadword
12091    vector with 16-bit elements when stored from register pair {d0,d1}
12092    will be (lowest address first, d0[N] is NEON register element N):
12093
12094      [d0[3], d0[2], d0[1], d0[0], d1[7], d1[6], d1[5], d1[4]]
12095
12096    When necessary, quadword registers (dN, dN+1) are moved to ARM
12097    registers from rN in the order:
12098
12099      dN -> (rN+1, rN), dN+1 -> (rN+3, rN+2)
12100
12101    So that STM/LDM can be used on vectors in ARM registers, and the
12102    same memory layout will result as if VSTM/VLDM were used.  */
12103
12104 const char *
12105 output_move_neon (rtx *operands)
12106 {
12107   rtx reg, mem, addr, ops[2];
12108   int regno, load = REG_P (operands[0]);
12109   const char *templ;
12110   char buff[50];
12111   enum machine_mode mode;
12112
12113   reg = operands[!load];
12114   mem = operands[load];
12115
12116   mode = GET_MODE (reg);
12117
12118   gcc_assert (REG_P (reg));
12119   regno = REGNO (reg);
12120   gcc_assert (VFP_REGNO_OK_FOR_DOUBLE (regno)
12121               || NEON_REGNO_OK_FOR_QUAD (regno));
12122   gcc_assert (VALID_NEON_DREG_MODE (mode)
12123               || VALID_NEON_QREG_MODE (mode)
12124               || VALID_NEON_STRUCT_MODE (mode));
12125   gcc_assert (MEM_P (mem));
12126
12127   addr = XEXP (mem, 0);
12128
12129   /* Strip off const from addresses like (const (plus (...))).  */
12130   if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
12131     addr = XEXP (addr, 0);
12132
12133   switch (GET_CODE (addr))
12134     {
12135     case POST_INC:
12136       templ = "v%smia%%?\t%%0!, %%h1";
12137       ops[0] = XEXP (addr, 0);
12138       ops[1] = reg;
12139       break;
12140
12141     case PRE_DEC:
12142       /* FIXME: We should be using vld1/vst1 here in BE mode?  */
12143       templ = "v%smdb%%?\t%%0!, %%h1";
12144       ops[0] = XEXP (addr, 0);
12145       ops[1] = reg;
12146       break;
12147     
12148     case POST_MODIFY:
12149       /* FIXME: Not currently enabled in neon_vector_mem_operand.  */
12150       gcc_unreachable ();
12151
12152     case LABEL_REF:
12153     case PLUS:
12154       {
12155         int nregs = HARD_REGNO_NREGS (REGNO (reg), mode) / 2;
12156         int i;
12157         int overlap = -1;
12158         for (i = 0; i < nregs; i++)
12159           {
12160             /* We're only using DImode here because it's a convenient size.  */
12161             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * i);
12162             ops[1] = adjust_address (mem, SImode, 8 * i);
12163             if (reg_overlap_mentioned_p (ops[0], mem))
12164               {
12165                 gcc_assert (overlap == -1);
12166                 overlap = i;
12167               }
12168             else
12169               {
12170                 sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
12171                 output_asm_insn (buff, ops);
12172               }
12173           }
12174         if (overlap != -1)
12175           {
12176             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * overlap);
12177             ops[1] = adjust_address (mem, SImode, 8 * overlap);
12178             sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
12179             output_asm_insn (buff, ops);
12180           }
12181
12182         return "";
12183       }
12184
12185     default:
12186       templ = "v%smia%%?\t%%m0, %%h1";
12187       ops[0] = mem;
12188       ops[1] = reg;
12189     }
12190
12191   sprintf (buff, templ, load ? "ld" : "st");
12192   output_asm_insn (buff, ops);
12193
12194   return "";
12195 }
12196
12197 /* Output an ADD r, s, #n where n may be too big for one instruction.
12198    If adding zero to one register, output nothing.  */
12199 const char *
12200 output_add_immediate (rtx *operands)
12201 {
12202   HOST_WIDE_INT n = INTVAL (operands[2]);
12203
12204   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
12205     {
12206       if (n < 0)
12207         output_multi_immediate (operands,
12208                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
12209                                 -n);
12210       else
12211         output_multi_immediate (operands,
12212                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
12213                                 n);
12214     }
12215
12216   return "";
12217 }
12218
12219 /* Output a multiple immediate operation.
12220    OPERANDS is the vector of operands referred to in the output patterns.
12221    INSTR1 is the output pattern to use for the first constant.
12222    INSTR2 is the output pattern to use for subsequent constants.
12223    IMMED_OP is the index of the constant slot in OPERANDS.
12224    N is the constant value.  */
12225 static const char *
12226 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
12227                         int immed_op, HOST_WIDE_INT n)
12228 {
12229 #if HOST_BITS_PER_WIDE_INT > 32
12230   n &= 0xffffffff;
12231 #endif
12232
12233   if (n == 0)
12234     {
12235       /* Quick and easy output.  */
12236       operands[immed_op] = const0_rtx;
12237       output_asm_insn (instr1, operands);
12238     }
12239   else
12240     {
12241       int i;
12242       const char * instr = instr1;
12243
12244       /* Note that n is never zero here (which would give no output).  */
12245       for (i = 0; i < 32; i += 2)
12246         {
12247           if (n & (3 << i))
12248             {
12249               operands[immed_op] = GEN_INT (n & (255 << i));
12250               output_asm_insn (instr, operands);
12251               instr = instr2;
12252               i += 6;
12253             }
12254         }
12255     }
12256
12257   return "";
12258 }
12259
12260 /* Return the name of a shifter operation.  */
12261 static const char *
12262 arm_shift_nmem(enum rtx_code code)
12263 {
12264   switch (code)
12265     {
12266     case ASHIFT:
12267       return ARM_LSL_NAME;
12268
12269     case ASHIFTRT:
12270       return "asr";
12271
12272     case LSHIFTRT:
12273       return "lsr";
12274
12275     case ROTATERT:
12276       return "ror";
12277
12278     default:
12279       abort();
12280     }
12281 }
12282
12283 /* Return the appropriate ARM instruction for the operation code.
12284    The returned result should not be overwritten.  OP is the rtx of the
12285    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
12286    was shifted.  */
12287 const char *
12288 arithmetic_instr (rtx op, int shift_first_arg)
12289 {
12290   switch (GET_CODE (op))
12291     {
12292     case PLUS:
12293       return "add";
12294
12295     case MINUS:
12296       return shift_first_arg ? "rsb" : "sub";
12297
12298     case IOR:
12299       return "orr";
12300
12301     case XOR:
12302       return "eor";
12303
12304     case AND:
12305       return "and";
12306
12307     case ASHIFT:
12308     case ASHIFTRT:
12309     case LSHIFTRT:
12310     case ROTATERT:
12311       return arm_shift_nmem(GET_CODE(op));
12312
12313     default:
12314       gcc_unreachable ();
12315     }
12316 }
12317
12318 /* Ensure valid constant shifts and return the appropriate shift mnemonic
12319    for the operation code.  The returned result should not be overwritten.
12320    OP is the rtx code of the shift.
12321    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
12322    shift.  */
12323 static const char *
12324 shift_op (rtx op, HOST_WIDE_INT *amountp)
12325 {
12326   const char * mnem;
12327   enum rtx_code code = GET_CODE (op);
12328
12329   switch (GET_CODE (XEXP (op, 1)))
12330     {
12331     case REG:
12332     case SUBREG:
12333       *amountp = -1;
12334       break;
12335
12336     case CONST_INT:
12337       *amountp = INTVAL (XEXP (op, 1));
12338       break;
12339
12340     default:
12341       gcc_unreachable ();
12342     }
12343
12344   switch (code)
12345     {
12346     case ROTATE:
12347       gcc_assert (*amountp != -1);
12348       *amountp = 32 - *amountp;
12349       code = ROTATERT;
12350
12351       /* Fall through.  */
12352
12353     case ASHIFT:
12354     case ASHIFTRT:
12355     case LSHIFTRT:
12356     case ROTATERT:
12357       mnem = arm_shift_nmem(code);
12358       break;
12359
12360     case MULT:
12361       /* We never have to worry about the amount being other than a
12362          power of 2, since this case can never be reloaded from a reg.  */
12363       gcc_assert (*amountp != -1);
12364       *amountp = int_log2 (*amountp);
12365       return ARM_LSL_NAME;
12366
12367     default:
12368       gcc_unreachable ();
12369     }
12370
12371   if (*amountp != -1)
12372     {
12373       /* This is not 100% correct, but follows from the desire to merge
12374          multiplication by a power of 2 with the recognizer for a
12375          shift.  >=32 is not a valid shift for "lsl", so we must try and
12376          output a shift that produces the correct arithmetical result.
12377          Using lsr #32 is identical except for the fact that the carry bit
12378          is not set correctly if we set the flags; but we never use the
12379          carry bit from such an operation, so we can ignore that.  */
12380       if (code == ROTATERT)
12381         /* Rotate is just modulo 32.  */
12382         *amountp &= 31;
12383       else if (*amountp != (*amountp & 31))
12384         {
12385           if (code == ASHIFT)
12386             mnem = "lsr";
12387           *amountp = 32;
12388         }
12389
12390       /* Shifts of 0 are no-ops.  */
12391       if (*amountp == 0)
12392         return NULL;
12393     }
12394
12395   return mnem;
12396 }
12397
12398 /* Obtain the shift from the POWER of two.  */
12399
12400 static HOST_WIDE_INT
12401 int_log2 (HOST_WIDE_INT power)
12402 {
12403   HOST_WIDE_INT shift = 0;
12404
12405   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
12406     {
12407       gcc_assert (shift <= 31);
12408       shift++;
12409     }
12410
12411   return shift;
12412 }
12413
12414 /* Output a .ascii pseudo-op, keeping track of lengths.  This is
12415    because /bin/as is horribly restrictive.  The judgement about
12416    whether or not each character is 'printable' (and can be output as
12417    is) or not (and must be printed with an octal escape) must be made
12418    with reference to the *host* character set -- the situation is
12419    similar to that discussed in the comments above pp_c_char in
12420    c-pretty-print.c.  */
12421
12422 #define MAX_ASCII_LEN 51
12423
12424 void
12425 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
12426 {
12427   int i;
12428   int len_so_far = 0;
12429
12430   fputs ("\t.ascii\t\"", stream);
12431
12432   for (i = 0; i < len; i++)
12433     {
12434       int c = p[i];
12435
12436       if (len_so_far >= MAX_ASCII_LEN)
12437         {
12438           fputs ("\"\n\t.ascii\t\"", stream);
12439           len_so_far = 0;
12440         }
12441
12442       if (ISPRINT (c))
12443         {
12444           if (c == '\\' || c == '\"')
12445             {
12446               putc ('\\', stream);
12447               len_so_far++;
12448             }
12449           putc (c, stream);
12450           len_so_far++;
12451         }
12452       else
12453         {
12454           fprintf (stream, "\\%03o", c);
12455           len_so_far += 4;
12456         }
12457     }
12458
12459   fputs ("\"\n", stream);
12460 }
12461 \f
12462 /* Compute the register save mask for registers 0 through 12
12463    inclusive.  This code is used by arm_compute_save_reg_mask.  */
12464
12465 static unsigned long
12466 arm_compute_save_reg0_reg12_mask (void)
12467 {
12468   unsigned long func_type = arm_current_func_type ();
12469   unsigned long save_reg_mask = 0;
12470   unsigned int reg;
12471
12472   if (IS_INTERRUPT (func_type))
12473     {
12474       unsigned int max_reg;
12475       /* Interrupt functions must not corrupt any registers,
12476          even call clobbered ones.  If this is a leaf function
12477          we can just examine the registers used by the RTL, but
12478          otherwise we have to assume that whatever function is
12479          called might clobber anything, and so we have to save
12480          all the call-clobbered registers as well.  */
12481       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
12482         /* FIQ handlers have registers r8 - r12 banked, so
12483            we only need to check r0 - r7, Normal ISRs only
12484            bank r14 and r15, so we must check up to r12.
12485            r13 is the stack pointer which is always preserved,
12486            so we do not need to consider it here.  */
12487         max_reg = 7;
12488       else
12489         max_reg = 12;
12490
12491       for (reg = 0; reg <= max_reg; reg++)
12492         if (df_regs_ever_live_p (reg)
12493             || (! current_function_is_leaf && call_used_regs[reg]))
12494           save_reg_mask |= (1 << reg);
12495
12496       /* Also save the pic base register if necessary.  */
12497       if (flag_pic
12498           && !TARGET_SINGLE_PIC_BASE
12499           && arm_pic_register != INVALID_REGNUM
12500           && crtl->uses_pic_offset_table)
12501         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
12502     }
12503   else
12504     {
12505       /* In the normal case we only need to save those registers
12506          which are call saved and which are used by this function.  */
12507       for (reg = 0; reg <= 11; reg++)
12508         if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
12509           save_reg_mask |= (1 << reg);
12510
12511       /* Handle the frame pointer as a special case.  */
12512       if (frame_pointer_needed)
12513         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
12514
12515       /* If we aren't loading the PIC register,
12516          don't stack it even though it may be live.  */
12517       if (flag_pic
12518           && !TARGET_SINGLE_PIC_BASE
12519           && arm_pic_register != INVALID_REGNUM
12520           && (df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM)
12521               || crtl->uses_pic_offset_table))
12522         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
12523
12524       /* The prologue will copy SP into R0, so save it.  */
12525       if (IS_STACKALIGN (func_type))
12526         save_reg_mask |= 1;
12527     }
12528
12529   /* Save registers so the exception handler can modify them.  */
12530   if (crtl->calls_eh_return)
12531     {
12532       unsigned int i;
12533
12534       for (i = 0; ; i++)
12535         {
12536           reg = EH_RETURN_DATA_REGNO (i);
12537           if (reg == INVALID_REGNUM)
12538             break;
12539           save_reg_mask |= 1 << reg;
12540         }
12541     }
12542
12543   return save_reg_mask;
12544 }
12545
12546
12547 /* Compute the number of bytes used to store the static chain register on the 
12548    stack, above the stack frame. We need to know this accurately to get the
12549    alignment of the rest of the stack frame correct. */
12550
12551 static int arm_compute_static_chain_stack_bytes (void)
12552 {
12553   unsigned long func_type = arm_current_func_type ();
12554   int static_chain_stack_bytes = 0;
12555
12556   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM &&
12557       IS_NESTED (func_type) &&
12558       df_regs_ever_live_p (3) && crtl->args.pretend_args_size == 0)
12559     static_chain_stack_bytes = 4;
12560
12561   return static_chain_stack_bytes;
12562 }
12563
12564
12565 /* Compute a bit mask of which registers need to be
12566    saved on the stack for the current function.
12567    This is used by arm_get_frame_offsets, which may add extra registers.  */
12568
12569 static unsigned long
12570 arm_compute_save_reg_mask (void)
12571 {
12572   unsigned int save_reg_mask = 0;
12573   unsigned long func_type = arm_current_func_type ();
12574   unsigned int reg;
12575
12576   if (IS_NAKED (func_type))
12577     /* This should never really happen.  */
12578     return 0;
12579
12580   /* If we are creating a stack frame, then we must save the frame pointer,
12581      IP (which will hold the old stack pointer), LR and the PC.  */
12582   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
12583     save_reg_mask |=
12584       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
12585       | (1 << IP_REGNUM)
12586       | (1 << LR_REGNUM)
12587       | (1 << PC_REGNUM);
12588
12589   /* Volatile functions do not return, so there
12590      is no need to save any other registers.  */
12591   if (IS_VOLATILE (func_type))
12592     return save_reg_mask;
12593
12594   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
12595
12596   /* Decide if we need to save the link register.
12597      Interrupt routines have their own banked link register,
12598      so they never need to save it.
12599      Otherwise if we do not use the link register we do not need to save
12600      it.  If we are pushing other registers onto the stack however, we
12601      can save an instruction in the epilogue by pushing the link register
12602      now and then popping it back into the PC.  This incurs extra memory
12603      accesses though, so we only do it when optimizing for size, and only
12604      if we know that we will not need a fancy return sequence.  */
12605   if (df_regs_ever_live_p (LR_REGNUM)
12606       || (save_reg_mask
12607           && optimize_size
12608           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
12609           && !crtl->calls_eh_return))
12610     save_reg_mask |= 1 << LR_REGNUM;
12611
12612   if (cfun->machine->lr_save_eliminated)
12613     save_reg_mask &= ~ (1 << LR_REGNUM);
12614
12615   if (TARGET_REALLY_IWMMXT
12616       && ((bit_count (save_reg_mask)
12617            + ARM_NUM_INTS (crtl->args.pretend_args_size +
12618                            arm_compute_static_chain_stack_bytes())
12619            ) % 2) != 0)
12620     {
12621       /* The total number of registers that are going to be pushed
12622          onto the stack is odd.  We need to ensure that the stack
12623          is 64-bit aligned before we start to save iWMMXt registers,
12624          and also before we start to create locals.  (A local variable
12625          might be a double or long long which we will load/store using
12626          an iWMMXt instruction).  Therefore we need to push another
12627          ARM register, so that the stack will be 64-bit aligned.  We
12628          try to avoid using the arg registers (r0 -r3) as they might be
12629          used to pass values in a tail call.  */
12630       for (reg = 4; reg <= 12; reg++)
12631         if ((save_reg_mask & (1 << reg)) == 0)
12632           break;
12633
12634       if (reg <= 12)
12635         save_reg_mask |= (1 << reg);
12636       else
12637         {
12638           cfun->machine->sibcall_blocked = 1;
12639           save_reg_mask |= (1 << 3);
12640         }
12641     }
12642
12643   /* We may need to push an additional register for use initializing the
12644      PIC base register.  */
12645   if (TARGET_THUMB2 && IS_NESTED (func_type) && flag_pic
12646       && (save_reg_mask & THUMB2_WORK_REGS) == 0)
12647     {
12648       reg = thumb_find_work_register (1 << 4);
12649       if (!call_used_regs[reg])
12650         save_reg_mask |= (1 << reg);
12651     }
12652
12653   return save_reg_mask;
12654 }
12655
12656
12657 /* Compute a bit mask of which registers need to be
12658    saved on the stack for the current function.  */
12659 static unsigned long
12660 thumb1_compute_save_reg_mask (void)
12661 {
12662   unsigned long mask;
12663   unsigned reg;
12664
12665   mask = 0;
12666   for (reg = 0; reg < 12; reg ++)
12667     if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
12668       mask |= 1 << reg;
12669
12670   if (flag_pic
12671       && !TARGET_SINGLE_PIC_BASE
12672       && arm_pic_register != INVALID_REGNUM
12673       && crtl->uses_pic_offset_table)
12674     mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
12675
12676   /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
12677   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
12678     mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
12679
12680   /* LR will also be pushed if any lo regs are pushed.  */
12681   if (mask & 0xff || thumb_force_lr_save ())
12682     mask |= (1 << LR_REGNUM);
12683
12684   /* Make sure we have a low work register if we need one.
12685      We will need one if we are going to push a high register,
12686      but we are not currently intending to push a low register.  */
12687   if ((mask & 0xff) == 0
12688       && ((mask & 0x0f00) || TARGET_BACKTRACE))
12689     {
12690       /* Use thumb_find_work_register to choose which register
12691          we will use.  If the register is live then we will
12692          have to push it.  Use LAST_LO_REGNUM as our fallback
12693          choice for the register to select.  */
12694       reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
12695       /* Make sure the register returned by thumb_find_work_register is
12696          not part of the return value.  */
12697       if (reg * UNITS_PER_WORD <= (unsigned) arm_size_return_regs ())
12698         reg = LAST_LO_REGNUM;
12699
12700       if (! call_used_regs[reg])
12701         mask |= 1 << reg;
12702     }
12703
12704   /* The 504 below is 8 bytes less than 512 because there are two possible
12705      alignment words.  We can't tell here if they will be present or not so we
12706      have to play it safe and assume that they are. */
12707   if ((CALLER_INTERWORKING_SLOT_SIZE +
12708        ROUND_UP_WORD (get_frame_size ()) +
12709        crtl->outgoing_args_size) >= 504)
12710     {
12711       /* This is the same as the code in thumb1_expand_prologue() which
12712          determines which register to use for stack decrement. */
12713       for (reg = LAST_ARG_REGNUM + 1; reg <= LAST_LO_REGNUM; reg++)
12714         if (mask & (1 << reg))
12715           break;
12716
12717       if (reg > LAST_LO_REGNUM)
12718         {
12719           /* Make sure we have a register available for stack decrement. */
12720           mask |= 1 << LAST_LO_REGNUM;
12721         }
12722     }
12723
12724   return mask;
12725 }
12726
12727
12728 /* Return the number of bytes required to save VFP registers.  */
12729 static int
12730 arm_get_vfp_saved_size (void)
12731 {
12732   unsigned int regno;
12733   int count;
12734   int saved;
12735
12736   saved = 0;
12737   /* Space for saved VFP registers.  */
12738   if (TARGET_HARD_FLOAT && TARGET_VFP)
12739     {
12740       count = 0;
12741       for (regno = FIRST_VFP_REGNUM;
12742            regno < LAST_VFP_REGNUM;
12743            regno += 2)
12744         {
12745           if ((!df_regs_ever_live_p (regno) || call_used_regs[regno])
12746               && (!df_regs_ever_live_p (regno + 1) || call_used_regs[regno + 1]))
12747             {
12748               if (count > 0)
12749                 {
12750                   /* Workaround ARM10 VFPr1 bug.  */
12751                   if (count == 2 && !arm_arch6)
12752                     count++;
12753                   saved += count * 8;
12754                 }
12755               count = 0;
12756             }
12757           else
12758             count++;
12759         }
12760       if (count > 0)
12761         {
12762           if (count == 2 && !arm_arch6)
12763             count++;
12764           saved += count * 8;
12765         }
12766     }
12767   return saved;
12768 }
12769
12770
12771 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
12772    everything bar the final return instruction.  */
12773 const char *
12774 output_return_instruction (rtx operand, int really_return, int reverse)
12775 {
12776   char conditional[10];
12777   char instr[100];
12778   unsigned reg;
12779   unsigned long live_regs_mask;
12780   unsigned long func_type;
12781   arm_stack_offsets *offsets;
12782
12783   func_type = arm_current_func_type ();
12784
12785   if (IS_NAKED (func_type))
12786     return "";
12787
12788   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
12789     {
12790       /* If this function was declared non-returning, and we have
12791          found a tail call, then we have to trust that the called
12792          function won't return.  */
12793       if (really_return)
12794         {
12795           rtx ops[2];
12796
12797           /* Otherwise, trap an attempted return by aborting.  */
12798           ops[0] = operand;
12799           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
12800                                        : "abort");
12801           assemble_external_libcall (ops[1]);
12802           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
12803         }
12804
12805       return "";
12806     }
12807
12808   gcc_assert (!cfun->calls_alloca || really_return);
12809
12810   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
12811
12812   cfun->machine->return_used_this_function = 1;
12813
12814   offsets = arm_get_frame_offsets ();
12815   live_regs_mask = offsets->saved_regs_mask;
12816
12817   if (live_regs_mask)
12818     {
12819       const char * return_reg;
12820
12821       /* If we do not have any special requirements for function exit
12822          (e.g. interworking) then we can load the return address
12823          directly into the PC.  Otherwise we must load it into LR.  */
12824       if (really_return
12825           && (IS_INTERRUPT (func_type) || !TARGET_INTERWORK))
12826         return_reg = reg_names[PC_REGNUM];
12827       else
12828         return_reg = reg_names[LR_REGNUM];
12829
12830       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
12831         {
12832           /* There are three possible reasons for the IP register
12833              being saved.  1) a stack frame was created, in which case
12834              IP contains the old stack pointer, or 2) an ISR routine
12835              corrupted it, or 3) it was saved to align the stack on
12836              iWMMXt.  In case 1, restore IP into SP, otherwise just
12837              restore IP.  */
12838           if (frame_pointer_needed)
12839             {
12840               live_regs_mask &= ~ (1 << IP_REGNUM);
12841               live_regs_mask |=   (1 << SP_REGNUM);
12842             }
12843           else
12844             gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
12845         }
12846
12847       /* On some ARM architectures it is faster to use LDR rather than
12848          LDM to load a single register.  On other architectures, the
12849          cost is the same.  In 26 bit mode, or for exception handlers,
12850          we have to use LDM to load the PC so that the CPSR is also
12851          restored.  */
12852       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
12853         if (live_regs_mask == (1U << reg))
12854           break;
12855
12856       if (reg <= LAST_ARM_REGNUM
12857           && (reg != LR_REGNUM
12858               || ! really_return
12859               || ! IS_INTERRUPT (func_type)))
12860         {
12861           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
12862                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
12863         }
12864       else
12865         {
12866           char *p;
12867           int first = 1;
12868
12869           /* Generate the load multiple instruction to restore the
12870              registers.  Note we can get here, even if
12871              frame_pointer_needed is true, but only if sp already
12872              points to the base of the saved core registers.  */
12873           if (live_regs_mask & (1 << SP_REGNUM))
12874             {
12875               unsigned HOST_WIDE_INT stack_adjust;
12876
12877               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
12878               gcc_assert (stack_adjust == 0 || stack_adjust == 4);
12879
12880               if (stack_adjust && arm_arch5 && TARGET_ARM)
12881                 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
12882               else
12883                 {
12884                   /* If we can't use ldmib (SA110 bug),
12885                      then try to pop r3 instead.  */
12886                   if (stack_adjust)
12887                     live_regs_mask |= 1 << 3;
12888                   sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
12889                 }
12890             }
12891           else
12892             sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
12893
12894           p = instr + strlen (instr);
12895
12896           for (reg = 0; reg <= SP_REGNUM; reg++)
12897             if (live_regs_mask & (1 << reg))
12898               {
12899                 int l = strlen (reg_names[reg]);
12900
12901                 if (first)
12902                   first = 0;
12903                 else
12904                   {
12905                     memcpy (p, ", ", 2);
12906                     p += 2;
12907                   }
12908
12909                 memcpy (p, "%|", 2);
12910                 memcpy (p + 2, reg_names[reg], l);
12911                 p += l + 2;
12912               }
12913
12914           if (live_regs_mask & (1 << LR_REGNUM))
12915             {
12916               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
12917               /* If returning from an interrupt, restore the CPSR.  */
12918               if (IS_INTERRUPT (func_type))
12919                 strcat (p, "^");
12920             }
12921           else
12922             strcpy (p, "}");
12923         }
12924
12925       output_asm_insn (instr, & operand);
12926
12927       /* See if we need to generate an extra instruction to
12928          perform the actual function return.  */
12929       if (really_return
12930           && func_type != ARM_FT_INTERWORKED
12931           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
12932         {
12933           /* The return has already been handled
12934              by loading the LR into the PC.  */
12935           really_return = 0;
12936         }
12937     }
12938
12939   if (really_return)
12940     {
12941       switch ((int) ARM_FUNC_TYPE (func_type))
12942         {
12943         case ARM_FT_ISR:
12944         case ARM_FT_FIQ:
12945           /* ??? This is wrong for unified assembly syntax.  */
12946           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
12947           break;
12948
12949         case ARM_FT_INTERWORKED:
12950           sprintf (instr, "bx%s\t%%|lr", conditional);
12951           break;
12952
12953         case ARM_FT_EXCEPTION:
12954           /* ??? This is wrong for unified assembly syntax.  */
12955           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
12956           break;
12957
12958         default:
12959           /* Use bx if it's available.  */
12960           if (arm_arch5 || arm_arch4t)
12961             sprintf (instr, "bx%s\t%%|lr", conditional);
12962           else
12963             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
12964           break;
12965         }
12966
12967       output_asm_insn (instr, & operand);
12968     }
12969
12970   return "";
12971 }
12972
12973 /* Write the function name into the code section, directly preceding
12974    the function prologue.
12975
12976    Code will be output similar to this:
12977      t0
12978          .ascii "arm_poke_function_name", 0
12979          .align
12980      t1
12981          .word 0xff000000 + (t1 - t0)
12982      arm_poke_function_name
12983          mov     ip, sp
12984          stmfd   sp!, {fp, ip, lr, pc}
12985          sub     fp, ip, #4
12986
12987    When performing a stack backtrace, code can inspect the value
12988    of 'pc' stored at 'fp' + 0.  If the trace function then looks
12989    at location pc - 12 and the top 8 bits are set, then we know
12990    that there is a function name embedded immediately preceding this
12991    location and has length ((pc[-3]) & 0xff000000).
12992
12993    We assume that pc is declared as a pointer to an unsigned long.
12994
12995    It is of no benefit to output the function name if we are assembling
12996    a leaf function.  These function types will not contain a stack
12997    backtrace structure, therefore it is not possible to determine the
12998    function name.  */
12999 void
13000 arm_poke_function_name (FILE *stream, const char *name)
13001 {
13002   unsigned long alignlength;
13003   unsigned long length;
13004   rtx           x;
13005
13006   length      = strlen (name) + 1;
13007   alignlength = ROUND_UP_WORD (length);
13008
13009   ASM_OUTPUT_ASCII (stream, name, length);
13010   ASM_OUTPUT_ALIGN (stream, 2);
13011   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
13012   assemble_aligned_integer (UNITS_PER_WORD, x);
13013 }
13014
13015 /* Place some comments into the assembler stream
13016    describing the current function.  */
13017 static void
13018 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
13019 {
13020   unsigned long func_type;
13021
13022   if (TARGET_THUMB1)
13023     {
13024       thumb1_output_function_prologue (f, frame_size);
13025       return;
13026     }
13027
13028   /* Sanity check.  */
13029   gcc_assert (!arm_ccfsm_state && !arm_target_insn);
13030
13031   func_type = arm_current_func_type ();
13032
13033   switch ((int) ARM_FUNC_TYPE (func_type))
13034     {
13035     default:
13036     case ARM_FT_NORMAL:
13037       break;
13038     case ARM_FT_INTERWORKED:
13039       asm_fprintf (f, "\t%@ Function supports interworking.\n");
13040       break;
13041     case ARM_FT_ISR:
13042       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
13043       break;
13044     case ARM_FT_FIQ:
13045       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
13046       break;
13047     case ARM_FT_EXCEPTION:
13048       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
13049       break;
13050     }
13051
13052   if (IS_NAKED (func_type))
13053     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
13054
13055   if (IS_VOLATILE (func_type))
13056     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
13057
13058   if (IS_NESTED (func_type))
13059     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
13060   if (IS_STACKALIGN (func_type))
13061     asm_fprintf (f, "\t%@ Stack Align: May be called with mis-aligned SP.\n");
13062
13063   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
13064                crtl->args.size,
13065                crtl->args.pretend_args_size, frame_size);
13066
13067   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
13068                frame_pointer_needed,
13069                cfun->machine->uses_anonymous_args);
13070
13071   if (cfun->machine->lr_save_eliminated)
13072     asm_fprintf (f, "\t%@ link register save eliminated.\n");
13073
13074   if (crtl->calls_eh_return)
13075     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
13076
13077 }
13078
13079 const char *
13080 arm_output_epilogue (rtx sibling)
13081 {
13082   int reg;
13083   unsigned long saved_regs_mask;
13084   unsigned long func_type;
13085   /* Floats_offset is the offset from the "virtual" frame.  In an APCS
13086      frame that is $fp + 4 for a non-variadic function.  */
13087   int floats_offset = 0;
13088   rtx operands[3];
13089   FILE * f = asm_out_file;
13090   unsigned int lrm_count = 0;
13091   int really_return = (sibling == NULL);
13092   int start_reg;
13093   arm_stack_offsets *offsets;
13094
13095   /* If we have already generated the return instruction
13096      then it is futile to generate anything else.  */
13097   if (use_return_insn (FALSE, sibling) && 
13098       (cfun->machine->return_used_this_function != 0))
13099     return "";
13100
13101   func_type = arm_current_func_type ();
13102
13103   if (IS_NAKED (func_type))
13104     /* Naked functions don't have epilogues.  */
13105     return "";
13106
13107   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
13108     {
13109       rtx op;
13110
13111       /* A volatile function should never return.  Call abort.  */
13112       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
13113       assemble_external_libcall (op);
13114       output_asm_insn ("bl\t%a0", &op);
13115
13116       return "";
13117     }
13118
13119   /* If we are throwing an exception, then we really must be doing a
13120      return, so we can't tail-call.  */
13121   gcc_assert (!crtl->calls_eh_return || really_return);
13122
13123   offsets = arm_get_frame_offsets ();
13124   saved_regs_mask = offsets->saved_regs_mask;
13125
13126   if (TARGET_IWMMXT)
13127     lrm_count = bit_count (saved_regs_mask);
13128
13129   floats_offset = offsets->saved_args;
13130   /* Compute how far away the floats will be.  */
13131   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
13132     if (saved_regs_mask & (1 << reg))
13133       floats_offset += 4;
13134
13135   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
13136     {
13137       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
13138       int vfp_offset = offsets->frame;
13139
13140       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
13141         {
13142           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
13143             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
13144               {
13145                 floats_offset += 12;
13146                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
13147                              reg, FP_REGNUM, floats_offset - vfp_offset);
13148               }
13149         }
13150       else
13151         {
13152           start_reg = LAST_FPA_REGNUM;
13153
13154           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
13155             {
13156               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
13157                 {
13158                   floats_offset += 12;
13159
13160                   /* We can't unstack more than four registers at once.  */
13161                   if (start_reg - reg == 3)
13162                     {
13163                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
13164                                    reg, FP_REGNUM, floats_offset - vfp_offset);
13165                       start_reg = reg - 1;
13166                     }
13167                 }
13168               else
13169                 {
13170                   if (reg != start_reg)
13171                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
13172                                  reg + 1, start_reg - reg,
13173                                  FP_REGNUM, floats_offset - vfp_offset);
13174                   start_reg = reg - 1;
13175                 }
13176             }
13177
13178           /* Just in case the last register checked also needs unstacking.  */
13179           if (reg != start_reg)
13180             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
13181                          reg + 1, start_reg - reg,
13182                          FP_REGNUM, floats_offset - vfp_offset);
13183         }
13184
13185       if (TARGET_HARD_FLOAT && TARGET_VFP)
13186         {
13187           int saved_size;
13188
13189           /* The fldmd insns do not have base+offset addressing
13190              modes, so we use IP to hold the address.  */
13191           saved_size = arm_get_vfp_saved_size ();
13192
13193           if (saved_size > 0)
13194             {
13195               floats_offset += saved_size;
13196               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
13197                            FP_REGNUM, floats_offset - vfp_offset);
13198             }
13199           start_reg = FIRST_VFP_REGNUM;
13200           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
13201             {
13202               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
13203                   && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
13204                 {
13205                   if (start_reg != reg)
13206                     vfp_output_fldmd (f, IP_REGNUM,
13207                                       (start_reg - FIRST_VFP_REGNUM) / 2,
13208                                       (reg - start_reg) / 2);
13209                   start_reg = reg + 2;
13210                 }
13211             }
13212           if (start_reg != reg)
13213             vfp_output_fldmd (f, IP_REGNUM,
13214                               (start_reg - FIRST_VFP_REGNUM) / 2,
13215                               (reg - start_reg) / 2);
13216         }
13217
13218       if (TARGET_IWMMXT)
13219         {
13220           /* The frame pointer is guaranteed to be non-double-word aligned.
13221              This is because it is set to (old_stack_pointer - 4) and the
13222              old_stack_pointer was double word aligned.  Thus the offset to
13223              the iWMMXt registers to be loaded must also be non-double-word
13224              sized, so that the resultant address *is* double-word aligned.
13225              We can ignore floats_offset since that was already included in
13226              the live_regs_mask.  */
13227           lrm_count += (lrm_count % 2 ? 2 : 1);
13228
13229           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
13230             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
13231               {
13232                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
13233                              reg, FP_REGNUM, lrm_count * 4);
13234                 lrm_count += 2;
13235               }
13236         }
13237
13238       /* saved_regs_mask should contain the IP, which at the time of stack
13239          frame generation actually contains the old stack pointer.  So a
13240          quick way to unwind the stack is just pop the IP register directly
13241          into the stack pointer.  */
13242       gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
13243       saved_regs_mask &= ~ (1 << IP_REGNUM);
13244       saved_regs_mask |=   (1 << SP_REGNUM);
13245
13246       /* There are two registers left in saved_regs_mask - LR and PC.  We
13247          only need to restore the LR register (the return address), but to
13248          save time we can load it directly into the PC, unless we need a
13249          special function exit sequence, or we are not really returning.  */
13250       if (really_return
13251           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
13252           && !crtl->calls_eh_return)
13253         /* Delete the LR from the register mask, so that the LR on
13254            the stack is loaded into the PC in the register mask.  */
13255         saved_regs_mask &= ~ (1 << LR_REGNUM);
13256       else
13257         saved_regs_mask &= ~ (1 << PC_REGNUM);
13258
13259       /* We must use SP as the base register, because SP is one of the
13260          registers being restored.  If an interrupt or page fault
13261          happens in the ldm instruction, the SP might or might not
13262          have been restored.  That would be bad, as then SP will no
13263          longer indicate the safe area of stack, and we can get stack
13264          corruption.  Using SP as the base register means that it will
13265          be reset correctly to the original value, should an interrupt
13266          occur.  If the stack pointer already points at the right
13267          place, then omit the subtraction.  */
13268       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
13269           || cfun->calls_alloca)
13270         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
13271                      4 * bit_count (saved_regs_mask));
13272       print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask, 0);
13273
13274       if (IS_INTERRUPT (func_type))
13275         /* Interrupt handlers will have pushed the
13276            IP onto the stack, so restore it now.  */
13277         print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, 1 << IP_REGNUM, 0);
13278     }
13279   else
13280     {
13281       /* This branch is executed for ARM mode (non-apcs frames) and
13282          Thumb-2 mode. Frame layout is essentially the same for those
13283          cases, except that in ARM mode frame pointer points to the
13284          first saved register, while in Thumb-2 mode the frame pointer points
13285          to the last saved register.
13286
13287          It is possible to make frame pointer point to last saved
13288          register in both cases, and remove some conditionals below.
13289          That means that fp setup in prologue would be just "mov fp, sp"
13290          and sp restore in epilogue would be just "mov sp, fp", whereas
13291          now we have to use add/sub in those cases. However, the value
13292          of that would be marginal, as both mov and add/sub are 32-bit
13293          in ARM mode, and it would require extra conditionals
13294          in arm_expand_prologue to distingish ARM-apcs-frame case
13295          (where frame pointer is required to point at first register)
13296          and ARM-non-apcs-frame. Therefore, such change is postponed
13297          until real need arise.  */
13298       unsigned HOST_WIDE_INT amount;
13299       int rfe;
13300       /* Restore stack pointer if necessary.  */
13301       if (TARGET_ARM && frame_pointer_needed)
13302         {
13303           operands[0] = stack_pointer_rtx;
13304           operands[1] = hard_frame_pointer_rtx;
13305           
13306           operands[2] = GEN_INT (offsets->frame - offsets->saved_regs);
13307           output_add_immediate (operands);
13308         }
13309       else
13310         {
13311           if (frame_pointer_needed)
13312             {
13313               /* For Thumb-2 restore sp from the frame pointer.
13314                  Operand restrictions mean we have to incrememnt FP, then copy
13315                  to SP.  */
13316               amount = offsets->locals_base - offsets->saved_regs;
13317               operands[0] = hard_frame_pointer_rtx;
13318             }
13319           else
13320             {
13321               unsigned long count;
13322               operands[0] = stack_pointer_rtx;
13323               amount = offsets->outgoing_args - offsets->saved_regs;
13324               /* pop call clobbered registers if it avoids a
13325                  separate stack adjustment.  */
13326               count = offsets->saved_regs - offsets->saved_args;
13327               if (optimize_size
13328                   && count != 0
13329                   && !crtl->calls_eh_return
13330                   && bit_count(saved_regs_mask) * 4 == count
13331                   && !IS_INTERRUPT (func_type)
13332                   && !crtl->tail_call_emit)
13333                 {
13334                   unsigned long mask;
13335                   mask = (1 << (arm_size_return_regs() / 4)) - 1;
13336                   mask ^= 0xf;
13337                   mask &= ~saved_regs_mask;
13338                   reg = 0;
13339                   while (bit_count (mask) * 4 > amount)
13340                     {
13341                       while ((mask & (1 << reg)) == 0)
13342                         reg++;
13343                       mask &= ~(1 << reg);
13344                     }
13345                   if (bit_count (mask) * 4 == amount) {
13346                       amount = 0;
13347                       saved_regs_mask |= mask;
13348                   }
13349                 }
13350             }
13351           
13352           if (amount)
13353             {
13354               operands[1] = operands[0];
13355               operands[2] = GEN_INT (amount);
13356               output_add_immediate (operands);
13357             }
13358           if (frame_pointer_needed)
13359             asm_fprintf (f, "\tmov\t%r, %r\n",
13360                          SP_REGNUM, HARD_FRAME_POINTER_REGNUM);
13361         }
13362
13363       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
13364         {
13365           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
13366             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
13367               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
13368                            reg, SP_REGNUM);
13369         }
13370       else
13371         {
13372           start_reg = FIRST_FPA_REGNUM;
13373
13374           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
13375             {
13376               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
13377                 {
13378                   if (reg - start_reg == 3)
13379                     {
13380                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
13381                                    start_reg, SP_REGNUM);
13382                       start_reg = reg + 1;
13383                     }
13384                 }
13385               else
13386                 {
13387                   if (reg != start_reg)
13388                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
13389                                  start_reg, reg - start_reg,
13390                                  SP_REGNUM);
13391
13392                   start_reg = reg + 1;
13393                 }
13394             }
13395
13396           /* Just in case the last register checked also needs unstacking.  */
13397           if (reg != start_reg)
13398             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
13399                          start_reg, reg - start_reg, SP_REGNUM);
13400         }
13401
13402       if (TARGET_HARD_FLOAT && TARGET_VFP)
13403         {
13404           start_reg = FIRST_VFP_REGNUM;
13405           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
13406             {
13407               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
13408                   && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
13409                 {
13410                   if (start_reg != reg)
13411                     vfp_output_fldmd (f, SP_REGNUM,
13412                                       (start_reg - FIRST_VFP_REGNUM) / 2,
13413                                       (reg - start_reg) / 2);
13414                   start_reg = reg + 2;
13415                 }
13416             }
13417           if (start_reg != reg)
13418             vfp_output_fldmd (f, SP_REGNUM,
13419                               (start_reg - FIRST_VFP_REGNUM) / 2,
13420                               (reg - start_reg) / 2);
13421         }
13422       if (TARGET_IWMMXT)
13423         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
13424           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
13425             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
13426
13427       /* If we can, restore the LR into the PC.  */
13428       if (ARM_FUNC_TYPE (func_type) != ARM_FT_INTERWORKED
13429           && (TARGET_ARM || ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
13430           && !IS_STACKALIGN (func_type)
13431           && really_return
13432           && crtl->args.pretend_args_size == 0
13433           && saved_regs_mask & (1 << LR_REGNUM)
13434           && !crtl->calls_eh_return)
13435         {
13436           saved_regs_mask &= ~ (1 << LR_REGNUM);
13437           saved_regs_mask |=   (1 << PC_REGNUM);
13438           rfe = IS_INTERRUPT (func_type);
13439         }
13440       else
13441         rfe = 0;
13442
13443       /* Load the registers off the stack.  If we only have one register
13444          to load use the LDR instruction - it is faster.  For Thumb-2
13445          always use pop and the assembler will pick the best instruction.*/
13446       if (TARGET_ARM && saved_regs_mask == (1 << LR_REGNUM)
13447           && !IS_INTERRUPT(func_type))
13448         {
13449           asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
13450         }
13451       else if (saved_regs_mask)
13452         {
13453           if (saved_regs_mask & (1 << SP_REGNUM))
13454             /* Note - write back to the stack register is not enabled
13455                (i.e. "ldmfd sp!...").  We know that the stack pointer is
13456                in the list of registers and if we add writeback the
13457                instruction becomes UNPREDICTABLE.  */
13458             print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask,
13459                              rfe);
13460           else if (TARGET_ARM)
13461             print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, saved_regs_mask,
13462                              rfe);
13463           else
13464             print_multi_reg (f, "pop\t", SP_REGNUM, saved_regs_mask, 0);
13465         }
13466
13467       if (crtl->args.pretend_args_size)
13468         {
13469           /* Unwind the pre-pushed regs.  */
13470           operands[0] = operands[1] = stack_pointer_rtx;
13471           operands[2] = GEN_INT (crtl->args.pretend_args_size);
13472           output_add_immediate (operands);
13473         }
13474     }
13475
13476   /* We may have already restored PC directly from the stack.  */
13477   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
13478     return "";
13479
13480   /* Stack adjustment for exception handler.  */
13481   if (crtl->calls_eh_return)
13482     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
13483                  ARM_EH_STACKADJ_REGNUM);
13484
13485   /* Generate the return instruction.  */
13486   switch ((int) ARM_FUNC_TYPE (func_type))
13487     {
13488     case ARM_FT_ISR:
13489     case ARM_FT_FIQ:
13490       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
13491       break;
13492
13493     case ARM_FT_EXCEPTION:
13494       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
13495       break;
13496
13497     case ARM_FT_INTERWORKED:
13498       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
13499       break;
13500
13501     default:
13502       if (IS_STACKALIGN (func_type))
13503         {
13504           /* See comment in arm_expand_prologue.  */
13505           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, 0);
13506         }
13507       if (arm_arch5 || arm_arch4t)
13508         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
13509       else
13510         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
13511       break;
13512     }
13513
13514   return "";
13515 }
13516
13517 static void
13518 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
13519                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
13520 {
13521   arm_stack_offsets *offsets;
13522
13523   if (TARGET_THUMB1)
13524     {
13525       int regno;
13526
13527       /* Emit any call-via-reg trampolines that are needed for v4t support
13528          of call_reg and call_value_reg type insns.  */
13529       for (regno = 0; regno < LR_REGNUM; regno++)
13530         {
13531           rtx label = cfun->machine->call_via[regno];
13532
13533           if (label != NULL)
13534             {
13535               switch_to_section (function_section (current_function_decl));
13536               targetm.asm_out.internal_label (asm_out_file, "L",
13537                                               CODE_LABEL_NUMBER (label));
13538               asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
13539             }
13540         }
13541
13542       /* ??? Probably not safe to set this here, since it assumes that a
13543          function will be emitted as assembly immediately after we generate
13544          RTL for it.  This does not happen for inline functions.  */
13545       cfun->machine->return_used_this_function = 0;
13546     }
13547   else /* TARGET_32BIT */
13548     {
13549       /* We need to take into account any stack-frame rounding.  */
13550       offsets = arm_get_frame_offsets ();
13551
13552       gcc_assert (!use_return_insn (FALSE, NULL)
13553                   || (cfun->machine->return_used_this_function != 0)
13554                   || offsets->saved_regs == offsets->outgoing_args
13555                   || frame_pointer_needed);
13556
13557       /* Reset the ARM-specific per-function variables.  */
13558       after_arm_reorg = 0;
13559     }
13560 }
13561
13562 /* Generate and emit an insn that we will recognize as a push_multi.
13563    Unfortunately, since this insn does not reflect very well the actual
13564    semantics of the operation, we need to annotate the insn for the benefit
13565    of DWARF2 frame unwind information.  */
13566 static rtx
13567 emit_multi_reg_push (unsigned long mask)
13568 {
13569   int num_regs = 0;
13570   int num_dwarf_regs;
13571   int i, j;
13572   rtx par;
13573   rtx dwarf;
13574   int dwarf_par_index;
13575   rtx tmp, reg;
13576
13577   for (i = 0; i <= LAST_ARM_REGNUM; i++)
13578     if (mask & (1 << i))
13579       num_regs++;
13580
13581   gcc_assert (num_regs && num_regs <= 16);
13582
13583   /* We don't record the PC in the dwarf frame information.  */
13584   num_dwarf_regs = num_regs;
13585   if (mask & (1 << PC_REGNUM))
13586     num_dwarf_regs--;
13587
13588   /* For the body of the insn we are going to generate an UNSPEC in
13589      parallel with several USEs.  This allows the insn to be recognized
13590      by the push_multi pattern in the arm.md file.  The insn looks
13591      something like this:
13592
13593        (parallel [
13594            (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
13595                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
13596            (use (reg:SI 11 fp))
13597            (use (reg:SI 12 ip))
13598            (use (reg:SI 14 lr))
13599            (use (reg:SI 15 pc))
13600         ])
13601
13602      For the frame note however, we try to be more explicit and actually
13603      show each register being stored into the stack frame, plus a (single)
13604      decrement of the stack pointer.  We do it this way in order to be
13605      friendly to the stack unwinding code, which only wants to see a single
13606      stack decrement per instruction.  The RTL we generate for the note looks
13607      something like this:
13608
13609       (sequence [
13610            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
13611            (set (mem:SI (reg:SI sp)) (reg:SI r4))
13612            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
13613            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
13614            (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
13615         ])
13616
13617       This sequence is used both by the code to support stack unwinding for
13618       exceptions handlers and the code to generate dwarf2 frame debugging.  */
13619
13620   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
13621   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
13622   dwarf_par_index = 1;
13623
13624   for (i = 0; i <= LAST_ARM_REGNUM; i++)
13625     {
13626       if (mask & (1 << i))
13627         {
13628           reg = gen_rtx_REG (SImode, i);
13629
13630           XVECEXP (par, 0, 0)
13631             = gen_rtx_SET (VOIDmode,
13632                            gen_frame_mem (BLKmode,
13633                                           gen_rtx_PRE_DEC (BLKmode,
13634                                                            stack_pointer_rtx)),
13635                            gen_rtx_UNSPEC (BLKmode,
13636                                            gen_rtvec (1, reg),
13637                                            UNSPEC_PUSH_MULT));
13638
13639           if (i != PC_REGNUM)
13640             {
13641               tmp = gen_rtx_SET (VOIDmode,
13642                                  gen_frame_mem (SImode, stack_pointer_rtx),
13643                                  reg);
13644               RTX_FRAME_RELATED_P (tmp) = 1;
13645               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
13646               dwarf_par_index++;
13647             }
13648
13649           break;
13650         }
13651     }
13652
13653   for (j = 1, i++; j < num_regs; i++)
13654     {
13655       if (mask & (1 << i))
13656         {
13657           reg = gen_rtx_REG (SImode, i);
13658
13659           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
13660
13661           if (i != PC_REGNUM)
13662             {
13663               tmp
13664                 = gen_rtx_SET (VOIDmode,
13665                                gen_frame_mem (SImode,
13666                                               plus_constant (stack_pointer_rtx,
13667                                                              4 * j)),
13668                                reg);
13669               RTX_FRAME_RELATED_P (tmp) = 1;
13670               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
13671             }
13672
13673           j++;
13674         }
13675     }
13676
13677   par = emit_insn (par);
13678
13679   tmp = gen_rtx_SET (VOIDmode,
13680                      stack_pointer_rtx,
13681                      plus_constant (stack_pointer_rtx, -4 * num_regs));
13682   RTX_FRAME_RELATED_P (tmp) = 1;
13683   XVECEXP (dwarf, 0, 0) = tmp;
13684
13685   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
13686
13687   return par;
13688 }
13689
13690 /* Calculate the size of the return value that is passed in registers.  */
13691 static unsigned
13692 arm_size_return_regs (void)
13693 {
13694   enum machine_mode mode;
13695
13696   if (crtl->return_rtx != 0)
13697     mode = GET_MODE (crtl->return_rtx);
13698   else
13699     mode = DECL_MODE (DECL_RESULT (current_function_decl));
13700
13701   return GET_MODE_SIZE (mode);
13702 }
13703
13704 static rtx
13705 emit_sfm (int base_reg, int count)
13706 {
13707   rtx par;
13708   rtx dwarf;
13709   rtx tmp, reg;
13710   int i;
13711
13712   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
13713   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
13714
13715   reg = gen_rtx_REG (XFmode, base_reg++);
13716
13717   XVECEXP (par, 0, 0)
13718     = gen_rtx_SET (VOIDmode,
13719                    gen_frame_mem (BLKmode,
13720                                   gen_rtx_PRE_DEC (BLKmode,
13721                                                    stack_pointer_rtx)),
13722                    gen_rtx_UNSPEC (BLKmode,
13723                                    gen_rtvec (1, reg),
13724                                    UNSPEC_PUSH_MULT));
13725   tmp = gen_rtx_SET (VOIDmode,
13726                      gen_frame_mem (XFmode, stack_pointer_rtx), reg);
13727   RTX_FRAME_RELATED_P (tmp) = 1;
13728   XVECEXP (dwarf, 0, 1) = tmp;
13729
13730   for (i = 1; i < count; i++)
13731     {
13732       reg = gen_rtx_REG (XFmode, base_reg++);
13733       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
13734
13735       tmp = gen_rtx_SET (VOIDmode,
13736                          gen_frame_mem (XFmode,
13737                                         plus_constant (stack_pointer_rtx,
13738                                                        i * 12)),
13739                          reg);
13740       RTX_FRAME_RELATED_P (tmp) = 1;
13741       XVECEXP (dwarf, 0, i + 1) = tmp;
13742     }
13743
13744   tmp = gen_rtx_SET (VOIDmode,
13745                      stack_pointer_rtx,
13746                      plus_constant (stack_pointer_rtx, -12 * count));
13747
13748   RTX_FRAME_RELATED_P (tmp) = 1;
13749   XVECEXP (dwarf, 0, 0) = tmp;
13750
13751   par = emit_insn (par);
13752   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
13753
13754   return par;
13755 }
13756
13757
13758 /* Return true if the current function needs to save/restore LR.  */
13759
13760 static bool
13761 thumb_force_lr_save (void)
13762 {
13763   return !cfun->machine->lr_save_eliminated
13764          && (!leaf_function_p ()
13765              || thumb_far_jump_used_p ()
13766              || df_regs_ever_live_p (LR_REGNUM));
13767 }
13768
13769
13770 /* Compute the distance from register FROM to register TO.
13771    These can be the arg pointer (26), the soft frame pointer (25),
13772    the stack pointer (13) or the hard frame pointer (11).
13773    In thumb mode r7 is used as the soft frame pointer, if needed.
13774    Typical stack layout looks like this:
13775
13776        old stack pointer -> |    |
13777                              ----
13778                             |    | \
13779                             |    |   saved arguments for
13780                             |    |   vararg functions
13781                             |    | /
13782                               --
13783    hard FP & arg pointer -> |    | \
13784                             |    |   stack
13785                             |    |   frame
13786                             |    | /
13787                               --
13788                             |    | \
13789                             |    |   call saved
13790                             |    |   registers
13791       soft frame pointer -> |    | /
13792                               --
13793                             |    | \
13794                             |    |   local
13795                             |    |   variables
13796      locals base pointer -> |    | /
13797                               --
13798                             |    | \
13799                             |    |   outgoing
13800                             |    |   arguments
13801    current stack pointer -> |    | /
13802                               --
13803
13804   For a given function some or all of these stack components
13805   may not be needed, giving rise to the possibility of
13806   eliminating some of the registers.
13807
13808   The values returned by this function must reflect the behavior
13809   of arm_expand_prologue() and arm_compute_save_reg_mask().
13810
13811   The sign of the number returned reflects the direction of stack
13812   growth, so the values are positive for all eliminations except
13813   from the soft frame pointer to the hard frame pointer.
13814
13815   SFP may point just inside the local variables block to ensure correct
13816   alignment.  */
13817
13818
13819 /* Calculate stack offsets.  These are used to calculate register elimination
13820    offsets and in prologue/epilogue code.  Also calculates which registers
13821    should be saved.  */
13822
13823 static arm_stack_offsets *
13824 arm_get_frame_offsets (void)
13825 {
13826   struct arm_stack_offsets *offsets;
13827   unsigned long func_type;
13828   int leaf;
13829   int saved;
13830   int core_saved;
13831   HOST_WIDE_INT frame_size;
13832   int i;
13833
13834   offsets = &cfun->machine->stack_offsets;
13835
13836   /* We need to know if we are a leaf function.  Unfortunately, it
13837      is possible to be called after start_sequence has been called,
13838      which causes get_insns to return the insns for the sequence,
13839      not the function, which will cause leaf_function_p to return
13840      the incorrect result.
13841
13842      to know about leaf functions once reload has completed, and the
13843      frame size cannot be changed after that time, so we can safely
13844      use the cached value.  */
13845
13846   if (reload_completed)
13847     return offsets;
13848
13849   /* Initially this is the size of the local variables.  It will translated
13850      into an offset once we have determined the size of preceding data.  */
13851   frame_size = ROUND_UP_WORD (get_frame_size ());
13852
13853   leaf = leaf_function_p ();
13854
13855   /* Space for variadic functions.  */
13856   offsets->saved_args = crtl->args.pretend_args_size;
13857
13858   /* In Thumb mode this is incorrect, but never used.  */
13859   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0) +
13860                    arm_compute_static_chain_stack_bytes();
13861
13862   if (TARGET_32BIT)
13863     {
13864       unsigned int regno;
13865
13866       offsets->saved_regs_mask = arm_compute_save_reg_mask ();
13867       core_saved = bit_count (offsets->saved_regs_mask) * 4;
13868       saved = core_saved;
13869
13870       /* We know that SP will be doubleword aligned on entry, and we must
13871          preserve that condition at any subroutine call.  We also require the
13872          soft frame pointer to be doubleword aligned.  */
13873
13874       if (TARGET_REALLY_IWMMXT)
13875         {
13876           /* Check for the call-saved iWMMXt registers.  */
13877           for (regno = FIRST_IWMMXT_REGNUM;
13878                regno <= LAST_IWMMXT_REGNUM;
13879                regno++)
13880             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
13881               saved += 8;
13882         }
13883
13884       func_type = arm_current_func_type ();
13885       if (! IS_VOLATILE (func_type))
13886         {
13887           /* Space for saved FPA registers.  */
13888           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
13889             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
13890             saved += 12;
13891
13892           /* Space for saved VFP registers.  */
13893           if (TARGET_HARD_FLOAT && TARGET_VFP)
13894             saved += arm_get_vfp_saved_size ();
13895         }
13896     }
13897   else /* TARGET_THUMB1 */
13898     {
13899       offsets->saved_regs_mask = thumb1_compute_save_reg_mask ();
13900       core_saved = bit_count (offsets->saved_regs_mask) * 4;
13901       saved = core_saved;
13902       if (TARGET_BACKTRACE)
13903         saved += 16;
13904     }
13905
13906   /* Saved registers include the stack frame.  */
13907   offsets->saved_regs = offsets->saved_args + saved +
13908                         arm_compute_static_chain_stack_bytes();
13909   offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
13910   /* A leaf function does not need any stack alignment if it has nothing
13911      on the stack.  */
13912   if (leaf && frame_size == 0)
13913     {
13914       offsets->outgoing_args = offsets->soft_frame;
13915       offsets->locals_base = offsets->soft_frame;
13916       return offsets;
13917     }
13918
13919   /* Ensure SFP has the correct alignment.  */
13920   if (ARM_DOUBLEWORD_ALIGN
13921       && (offsets->soft_frame & 7))
13922     {
13923       offsets->soft_frame += 4;
13924       /* Try to align stack by pushing an extra reg.  Don't bother doing this
13925          when there is a stack frame as the alignment will be rolled into
13926          the normal stack adjustment.  */
13927       if (frame_size + crtl->outgoing_args_size == 0)
13928         {
13929           int reg = -1;
13930
13931           /* If it is safe to use r3, then do so.  This sometimes 
13932              generates better code on Thumb-2 by avoiding the need to
13933              use 32-bit push/pop instructions.  */
13934           if (!crtl->tail_call_emit
13935               && arm_size_return_regs () <= 12)
13936             {
13937               reg = 3;
13938             }
13939           else
13940             for (i = 4; i <= (TARGET_THUMB1 ? LAST_LO_REGNUM : 11); i++)
13941               {
13942                 if ((offsets->saved_regs_mask & (1 << i)) == 0)
13943                   {
13944                     reg = i;
13945                     break;
13946                   }
13947               }
13948
13949           if (reg != -1)
13950             {
13951               offsets->saved_regs += 4;
13952               offsets->saved_regs_mask |= (1 << reg);
13953             }
13954         }
13955     }
13956
13957   offsets->locals_base = offsets->soft_frame + frame_size;
13958   offsets->outgoing_args = (offsets->locals_base
13959                             + crtl->outgoing_args_size);
13960
13961   if (ARM_DOUBLEWORD_ALIGN)
13962     {
13963       /* Ensure SP remains doubleword aligned.  */
13964       if (offsets->outgoing_args & 7)
13965         offsets->outgoing_args += 4;
13966       gcc_assert (!(offsets->outgoing_args & 7));
13967     }
13968
13969   return offsets;
13970 }
13971
13972
13973 /* Calculate the relative offsets for the different stack pointers.  Positive
13974    offsets are in the direction of stack growth.  */
13975
13976 HOST_WIDE_INT
13977 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
13978 {
13979   arm_stack_offsets *offsets;
13980
13981   offsets = arm_get_frame_offsets ();
13982
13983   /* OK, now we have enough information to compute the distances.
13984      There must be an entry in these switch tables for each pair
13985      of registers in ELIMINABLE_REGS, even if some of the entries
13986      seem to be redundant or useless.  */
13987   switch (from)
13988     {
13989     case ARG_POINTER_REGNUM:
13990       switch (to)
13991         {
13992         case THUMB_HARD_FRAME_POINTER_REGNUM:
13993           return 0;
13994
13995         case FRAME_POINTER_REGNUM:
13996           /* This is the reverse of the soft frame pointer
13997              to hard frame pointer elimination below.  */
13998           return offsets->soft_frame - offsets->saved_args;
13999
14000         case ARM_HARD_FRAME_POINTER_REGNUM:
14001           /* This is only non-zero in the case where the static chain register
14002              is stored above the frame.  */
14003           return offsets->frame - offsets->saved_args - 4;
14004
14005         case STACK_POINTER_REGNUM:
14006           /* If nothing has been pushed on the stack at all
14007              then this will return -4.  This *is* correct!  */
14008           return offsets->outgoing_args - (offsets->saved_args + 4);
14009
14010         default:
14011           gcc_unreachable ();
14012         }
14013       gcc_unreachable ();
14014
14015     case FRAME_POINTER_REGNUM:
14016       switch (to)
14017         {
14018         case THUMB_HARD_FRAME_POINTER_REGNUM:
14019           return 0;
14020
14021         case ARM_HARD_FRAME_POINTER_REGNUM:
14022           /* The hard frame pointer points to the top entry in the
14023              stack frame.  The soft frame pointer to the bottom entry
14024              in the stack frame.  If there is no stack frame at all,
14025              then they are identical.  */
14026
14027           return offsets->frame - offsets->soft_frame;
14028
14029         case STACK_POINTER_REGNUM:
14030           return offsets->outgoing_args - offsets->soft_frame;
14031
14032         default:
14033           gcc_unreachable ();
14034         }
14035       gcc_unreachable ();
14036
14037     default:
14038       /* You cannot eliminate from the stack pointer.
14039          In theory you could eliminate from the hard frame
14040          pointer to the stack pointer, but this will never
14041          happen, since if a stack frame is not needed the
14042          hard frame pointer will never be used.  */
14043       gcc_unreachable ();
14044     }
14045 }
14046
14047
14048 /* Emit RTL to save coprocessor registers on function entry.  Returns the
14049    number of bytes pushed.  */
14050
14051 static int
14052 arm_save_coproc_regs(void)
14053 {
14054   int saved_size = 0;
14055   unsigned reg;
14056   unsigned start_reg;
14057   rtx insn;
14058
14059   for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
14060     if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
14061       {
14062         insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
14063         insn = gen_rtx_MEM (V2SImode, insn);
14064         insn = emit_set_insn (insn, gen_rtx_REG (V2SImode, reg));
14065         RTX_FRAME_RELATED_P (insn) = 1;
14066         saved_size += 8;
14067       }
14068
14069   /* Save any floating point call-saved registers used by this
14070      function.  */
14071   if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
14072     {
14073       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
14074         if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14075           {
14076             insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
14077             insn = gen_rtx_MEM (XFmode, insn);
14078             insn = emit_set_insn (insn, gen_rtx_REG (XFmode, reg));
14079             RTX_FRAME_RELATED_P (insn) = 1;
14080             saved_size += 12;
14081           }
14082     }
14083   else
14084     {
14085       start_reg = LAST_FPA_REGNUM;
14086
14087       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
14088         {
14089           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
14090             {
14091               if (start_reg - reg == 3)
14092                 {
14093                   insn = emit_sfm (reg, 4);
14094                   RTX_FRAME_RELATED_P (insn) = 1;
14095                   saved_size += 48;
14096                   start_reg = reg - 1;
14097                 }
14098             }
14099           else
14100             {
14101               if (start_reg != reg)
14102                 {
14103                   insn = emit_sfm (reg + 1, start_reg - reg);
14104                   RTX_FRAME_RELATED_P (insn) = 1;
14105                   saved_size += (start_reg - reg) * 12;
14106                 }
14107               start_reg = reg - 1;
14108             }
14109         }
14110
14111       if (start_reg != reg)
14112         {
14113           insn = emit_sfm (reg + 1, start_reg - reg);
14114           saved_size += (start_reg - reg) * 12;
14115           RTX_FRAME_RELATED_P (insn) = 1;
14116         }
14117     }
14118   if (TARGET_HARD_FLOAT && TARGET_VFP)
14119     {
14120       start_reg = FIRST_VFP_REGNUM;
14121
14122       for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
14123         {
14124           if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
14125               && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
14126             {
14127               if (start_reg != reg)
14128                 saved_size += vfp_emit_fstmd (start_reg,
14129                                               (reg - start_reg) / 2);
14130               start_reg = reg + 2;
14131             }
14132         }
14133       if (start_reg != reg)
14134         saved_size += vfp_emit_fstmd (start_reg,
14135                                       (reg - start_reg) / 2);
14136     }
14137   return saved_size;
14138 }
14139
14140
14141 /* Set the Thumb frame pointer from the stack pointer.  */
14142
14143 static void
14144 thumb_set_frame_pointer (arm_stack_offsets *offsets)
14145 {
14146   HOST_WIDE_INT amount;
14147   rtx insn, dwarf;
14148
14149   amount = offsets->outgoing_args - offsets->locals_base;
14150   if (amount < 1024)
14151     insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
14152                                   stack_pointer_rtx, GEN_INT (amount)));
14153   else
14154     {
14155       emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
14156       /* Thumb-2 RTL patterns expect sp as the first input.  Thumb-1
14157          expects the first two operands to be the same.  */
14158       if (TARGET_THUMB2)
14159         {
14160           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
14161                                         stack_pointer_rtx,
14162                                         hard_frame_pointer_rtx));
14163         }
14164       else
14165         {
14166           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
14167                                         hard_frame_pointer_rtx,
14168                                         stack_pointer_rtx));
14169         }
14170       dwarf = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
14171                            plus_constant (stack_pointer_rtx, amount));
14172       RTX_FRAME_RELATED_P (dwarf) = 1;
14173       add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
14174     }
14175
14176   RTX_FRAME_RELATED_P (insn) = 1;
14177 }
14178
14179 /* Generate the prologue instructions for entry into an ARM or Thumb-2
14180    function.  */
14181 void
14182 arm_expand_prologue (void)
14183 {
14184   rtx amount;
14185   rtx insn;
14186   rtx ip_rtx;
14187   unsigned long live_regs_mask;
14188   unsigned long func_type;
14189   int fp_offset = 0;
14190   int saved_pretend_args = 0;
14191   int saved_regs = 0;
14192   unsigned HOST_WIDE_INT args_to_push;
14193   arm_stack_offsets *offsets;
14194
14195   func_type = arm_current_func_type ();
14196
14197   /* Naked functions don't have prologues.  */
14198   if (IS_NAKED (func_type))
14199     return;
14200
14201   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
14202   args_to_push = crtl->args.pretend_args_size;
14203
14204   /* Compute which register we will have to save onto the stack.  */
14205   offsets = arm_get_frame_offsets ();
14206   live_regs_mask = offsets->saved_regs_mask;
14207
14208   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
14209
14210   if (IS_STACKALIGN (func_type))
14211     {
14212       rtx dwarf;
14213       rtx r0;
14214       rtx r1;
14215       /* Handle a word-aligned stack pointer.  We generate the following:
14216
14217           mov r0, sp
14218           bic r1, r0, #7
14219           mov sp, r1
14220           <save and restore r0 in normal prologue/epilogue>
14221           mov sp, r0
14222           bx lr
14223
14224          The unwinder doesn't need to know about the stack realignment.
14225          Just tell it we saved SP in r0.  */
14226       gcc_assert (TARGET_THUMB2 && !arm_arch_notm && args_to_push == 0);
14227
14228       r0 = gen_rtx_REG (SImode, 0);
14229       r1 = gen_rtx_REG (SImode, 1);
14230       /* Use a real rtvec rather than NULL_RTVEC so the rest of the
14231          compiler won't choke.  */
14232       dwarf = gen_rtx_UNSPEC (SImode, rtvec_alloc (0), UNSPEC_STACK_ALIGN);
14233       dwarf = gen_rtx_SET (VOIDmode, r0, dwarf);
14234       insn = gen_movsi (r0, stack_pointer_rtx);
14235       RTX_FRAME_RELATED_P (insn) = 1;
14236       add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
14237       emit_insn (insn);
14238       emit_insn (gen_andsi3 (r1, r0, GEN_INT (~(HOST_WIDE_INT)7)));
14239       emit_insn (gen_movsi (stack_pointer_rtx, r1));
14240     }
14241
14242   /* For APCS frames, if IP register is clobbered
14243      when creating frame, save that register in a special
14244      way.  */
14245   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
14246     {
14247       if (IS_INTERRUPT (func_type))
14248         {
14249           /* Interrupt functions must not corrupt any registers.
14250              Creating a frame pointer however, corrupts the IP
14251              register, so we must push it first.  */
14252           insn = emit_multi_reg_push (1 << IP_REGNUM);
14253
14254           /* Do not set RTX_FRAME_RELATED_P on this insn.
14255              The dwarf stack unwinding code only wants to see one
14256              stack decrement per function, and this is not it.  If
14257              this instruction is labeled as being part of the frame
14258              creation sequence then dwarf2out_frame_debug_expr will
14259              die when it encounters the assignment of IP to FP
14260              later on, since the use of SP here establishes SP as
14261              the CFA register and not IP.
14262
14263              Anyway this instruction is not really part of the stack
14264              frame creation although it is part of the prologue.  */
14265         }
14266       else if (IS_NESTED (func_type))
14267         {
14268           /* The Static chain register is the same as the IP register
14269              used as a scratch register during stack frame creation.
14270              To get around this need to find somewhere to store IP
14271              whilst the frame is being created.  We try the following
14272              places in order:
14273
14274                1. The last argument register.
14275                2. A slot on the stack above the frame.  (This only
14276                   works if the function is not a varargs function).
14277                3. Register r3, after pushing the argument registers
14278                   onto the stack.
14279
14280              Note - we only need to tell the dwarf2 backend about the SP
14281              adjustment in the second variant; the static chain register
14282              doesn't need to be unwound, as it doesn't contain a value
14283              inherited from the caller.  */
14284
14285           if (df_regs_ever_live_p (3) == false)
14286             insn = emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
14287           else if (args_to_push == 0)
14288             {
14289               rtx dwarf;
14290
14291               gcc_assert(arm_compute_static_chain_stack_bytes() == 4);
14292               saved_regs += 4;
14293
14294               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
14295               insn = emit_set_insn (gen_frame_mem (SImode, insn), ip_rtx);
14296               fp_offset = 4;
14297
14298               /* Just tell the dwarf backend that we adjusted SP.  */
14299               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
14300                                    plus_constant (stack_pointer_rtx,
14301                                                   -fp_offset));
14302               RTX_FRAME_RELATED_P (insn) = 1;
14303               add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
14304             }
14305           else
14306             {
14307               /* Store the args on the stack.  */
14308               if (cfun->machine->uses_anonymous_args)
14309                 insn = emit_multi_reg_push
14310                   ((0xf0 >> (args_to_push / 4)) & 0xf);
14311               else
14312                 insn = emit_insn
14313                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
14314                                GEN_INT (- args_to_push)));
14315
14316               RTX_FRAME_RELATED_P (insn) = 1;
14317
14318               saved_pretend_args = 1;
14319               fp_offset = args_to_push;
14320               args_to_push = 0;
14321
14322               /* Now reuse r3 to preserve IP.  */
14323               emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
14324             }
14325         }
14326
14327       insn = emit_set_insn (ip_rtx,
14328                             plus_constant (stack_pointer_rtx, fp_offset));
14329       RTX_FRAME_RELATED_P (insn) = 1;
14330     }
14331
14332   if (args_to_push)
14333     {
14334       /* Push the argument registers, or reserve space for them.  */
14335       if (cfun->machine->uses_anonymous_args)
14336         insn = emit_multi_reg_push
14337           ((0xf0 >> (args_to_push / 4)) & 0xf);
14338       else
14339         insn = emit_insn
14340           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
14341                        GEN_INT (- args_to_push)));
14342       RTX_FRAME_RELATED_P (insn) = 1;
14343     }
14344
14345   /* If this is an interrupt service routine, and the link register
14346      is going to be pushed, and we're not generating extra
14347      push of IP (needed when frame is needed and frame layout if apcs),
14348      subtracting four from LR now will mean that the function return
14349      can be done with a single instruction.  */
14350   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
14351       && (live_regs_mask & (1 << LR_REGNUM)) != 0
14352       && !(frame_pointer_needed && TARGET_APCS_FRAME)
14353       && TARGET_ARM)
14354     {
14355       rtx lr = gen_rtx_REG (SImode, LR_REGNUM);
14356       
14357       emit_set_insn (lr, plus_constant (lr, -4));
14358     }
14359
14360   if (live_regs_mask)
14361     {
14362       saved_regs += bit_count (live_regs_mask) * 4;
14363       if (optimize_size && !frame_pointer_needed
14364           && saved_regs == offsets->saved_regs - offsets->saved_args)
14365         {
14366           /* If no coprocessor registers are being pushed and we don't have
14367              to worry about a frame pointer then push extra registers to
14368              create the stack frame.  This is done is a way that does not
14369              alter the frame layout, so is independent of the epilogue.  */
14370           int n;
14371           int frame;
14372           n = 0;
14373           while (n < 8 && (live_regs_mask & (1 << n)) == 0)
14374             n++;
14375           frame = offsets->outgoing_args - (offsets->saved_args + saved_regs);
14376           if (frame && n * 4 >= frame)
14377             {
14378               n = frame / 4;
14379               live_regs_mask |= (1 << n) - 1;
14380               saved_regs += frame;
14381             }
14382         }
14383       insn = emit_multi_reg_push (live_regs_mask);
14384       RTX_FRAME_RELATED_P (insn) = 1;
14385     }
14386
14387   if (! IS_VOLATILE (func_type))
14388     saved_regs += arm_save_coproc_regs ();
14389
14390   if (frame_pointer_needed && TARGET_ARM)
14391     {
14392       /* Create the new frame pointer.  */
14393       if (TARGET_APCS_FRAME)
14394         {
14395           insn = GEN_INT (-(4 + args_to_push + fp_offset));
14396           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
14397           RTX_FRAME_RELATED_P (insn) = 1;
14398
14399           if (IS_NESTED (func_type))
14400             {
14401               /* Recover the static chain register.  */
14402               if (!df_regs_ever_live_p (3)
14403                   || saved_pretend_args)
14404                 insn = gen_rtx_REG (SImode, 3);
14405               else /* if (crtl->args.pretend_args_size == 0) */
14406                 {
14407                   insn = plus_constant (hard_frame_pointer_rtx, 4);
14408                   insn = gen_frame_mem (SImode, insn);
14409                 }
14410               emit_set_insn (ip_rtx, insn);
14411               /* Add a USE to stop propagate_one_insn() from barfing.  */
14412               emit_insn (gen_prologue_use (ip_rtx));
14413             }
14414         }
14415       else
14416         {
14417           insn = GEN_INT (saved_regs - 4);
14418           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
14419                                         stack_pointer_rtx, insn));
14420           RTX_FRAME_RELATED_P (insn) = 1;
14421         }
14422     }
14423
14424   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
14425     {
14426       /* This add can produce multiple insns for a large constant, so we
14427          need to get tricky.  */
14428       rtx last = get_last_insn ();
14429
14430       amount = GEN_INT (offsets->saved_args + saved_regs
14431                         - offsets->outgoing_args);
14432
14433       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
14434                                     amount));
14435       do
14436         {
14437           last = last ? NEXT_INSN (last) : get_insns ();
14438           RTX_FRAME_RELATED_P (last) = 1;
14439         }
14440       while (last != insn);
14441
14442       /* If the frame pointer is needed, emit a special barrier that
14443          will prevent the scheduler from moving stores to the frame
14444          before the stack adjustment.  */
14445       if (frame_pointer_needed)
14446         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
14447                                          hard_frame_pointer_rtx));
14448     }
14449
14450
14451   if (frame_pointer_needed && TARGET_THUMB2)
14452     thumb_set_frame_pointer (offsets);
14453
14454   if (flag_pic && arm_pic_register != INVALID_REGNUM)
14455     {
14456       unsigned long mask;
14457
14458       mask = live_regs_mask;
14459       mask &= THUMB2_WORK_REGS;
14460       if (!IS_NESTED (func_type))
14461         mask |= (1 << IP_REGNUM);
14462       arm_load_pic_register (mask);
14463     }
14464
14465   /* If we are profiling, make sure no instructions are scheduled before
14466      the call to mcount.  Similarly if the user has requested no
14467      scheduling in the prolog.  Similarly if we want non-call exceptions
14468      using the EABI unwinder, to prevent faulting instructions from being
14469      swapped with a stack adjustment.  */
14470   if (crtl->profile || !TARGET_SCHED_PROLOG
14471       || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
14472     emit_insn (gen_blockage ());
14473
14474   /* If the link register is being kept alive, with the return address in it,
14475      then make sure that it does not get reused by the ce2 pass.  */
14476   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
14477     cfun->machine->lr_save_eliminated = 1;
14478 }
14479 \f
14480 /* Print condition code to STREAM.  Helper function for arm_print_operand.  */
14481 static void
14482 arm_print_condition (FILE *stream)
14483 {
14484   if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
14485     {
14486       /* Branch conversion is not implemented for Thumb-2.  */
14487       if (TARGET_THUMB)
14488         {
14489           output_operand_lossage ("predicated Thumb instruction");
14490           return;
14491         }
14492       if (current_insn_predicate != NULL)
14493         {
14494           output_operand_lossage
14495             ("predicated instruction in conditional sequence");
14496           return;
14497         }
14498
14499       fputs (arm_condition_codes[arm_current_cc], stream);
14500     }
14501   else if (current_insn_predicate)
14502     {
14503       enum arm_cond_code code;
14504
14505       if (TARGET_THUMB1)
14506         {
14507           output_operand_lossage ("predicated Thumb instruction");
14508           return;
14509         }
14510
14511       code = get_arm_condition_code (current_insn_predicate);
14512       fputs (arm_condition_codes[code], stream);
14513     }
14514 }
14515
14516
14517 /* If CODE is 'd', then the X is a condition operand and the instruction
14518    should only be executed if the condition is true.
14519    if CODE is 'D', then the X is a condition operand and the instruction
14520    should only be executed if the condition is false: however, if the mode
14521    of the comparison is CCFPEmode, then always execute the instruction -- we
14522    do this because in these circumstances !GE does not necessarily imply LT;
14523    in these cases the instruction pattern will take care to make sure that
14524    an instruction containing %d will follow, thereby undoing the effects of
14525    doing this instruction unconditionally.
14526    If CODE is 'N' then X is a floating point operand that must be negated
14527    before output.
14528    If CODE is 'B' then output a bitwise inverted value of X (a const int).
14529    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
14530 void
14531 arm_print_operand (FILE *stream, rtx x, int code)
14532 {
14533   switch (code)
14534     {
14535     case '@':
14536       fputs (ASM_COMMENT_START, stream);
14537       return;
14538
14539     case '_':
14540       fputs (user_label_prefix, stream);
14541       return;
14542
14543     case '|':
14544       fputs (REGISTER_PREFIX, stream);
14545       return;
14546
14547     case '?':
14548       arm_print_condition (stream);
14549       return;
14550
14551     case '(':
14552       /* Nothing in unified syntax, otherwise the current condition code.  */
14553       if (!TARGET_UNIFIED_ASM)
14554         arm_print_condition (stream);
14555       break;
14556
14557     case ')':
14558       /* The current condition code in unified syntax, otherwise nothing.  */
14559       if (TARGET_UNIFIED_ASM)
14560         arm_print_condition (stream);
14561       break;
14562   
14563     case '.':
14564       /* The current condition code for a condition code setting instruction.
14565          Preceded by 's' in unified syntax, otherwise followed by 's'.  */
14566       if (TARGET_UNIFIED_ASM)
14567         {
14568           fputc('s', stream);
14569           arm_print_condition (stream);
14570         }
14571       else
14572         {
14573           arm_print_condition (stream);
14574           fputc('s', stream);
14575         }
14576       return;
14577
14578     case '!':
14579       /* If the instruction is conditionally executed then print
14580          the current condition code, otherwise print 's'.  */
14581       gcc_assert (TARGET_THUMB2 && TARGET_UNIFIED_ASM);
14582       if (current_insn_predicate)
14583         arm_print_condition (stream);
14584       else
14585         fputc('s', stream);
14586       break;
14587
14588     /* %# is a "break" sequence. It doesn't output anything, but is used to
14589        separate e.g. operand numbers from following text, if that text consists
14590        of further digits which we don't want to be part of the operand
14591        number.  */
14592     case '#':
14593       return;
14594
14595     case 'N':
14596       {
14597         REAL_VALUE_TYPE r;
14598         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
14599         r = REAL_VALUE_NEGATE (r);
14600         fprintf (stream, "%s", fp_const_from_val (&r));
14601       }
14602       return;
14603
14604     /* An integer or symbol address without a preceding # sign.  */
14605     case 'c':
14606       switch (GET_CODE (x))
14607         {
14608         case CONST_INT:
14609           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
14610           break;
14611
14612         case SYMBOL_REF:
14613           output_addr_const (stream, x);
14614           break;
14615
14616         default:
14617           gcc_unreachable ();
14618         }
14619       return;
14620
14621     case 'B':
14622       if (GET_CODE (x) == CONST_INT)
14623         {
14624           HOST_WIDE_INT val;
14625           val = ARM_SIGN_EXTEND (~INTVAL (x));
14626           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
14627         }
14628       else
14629         {
14630           putc ('~', stream);
14631           output_addr_const (stream, x);
14632         }
14633       return;
14634
14635     case 'L':
14636       /* The low 16 bits of an immediate constant.  */
14637       fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL(x) & 0xffff);
14638       return;
14639
14640     case 'i':
14641       fprintf (stream, "%s", arithmetic_instr (x, 1));
14642       return;
14643
14644     /* Truncate Cirrus shift counts.  */
14645     case 's':
14646       if (GET_CODE (x) == CONST_INT)
14647         {
14648           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
14649           return;
14650         }
14651       arm_print_operand (stream, x, 0);
14652       return;
14653
14654     case 'I':
14655       fprintf (stream, "%s", arithmetic_instr (x, 0));
14656       return;
14657
14658     case 'S':
14659       {
14660         HOST_WIDE_INT val;
14661         const char *shift;
14662
14663         if (!shift_operator (x, SImode))
14664           {
14665             output_operand_lossage ("invalid shift operand");
14666             break;
14667           }
14668
14669         shift = shift_op (x, &val);
14670
14671         if (shift)
14672           {
14673             fprintf (stream, ", %s ", shift);
14674             if (val == -1)
14675               arm_print_operand (stream, XEXP (x, 1), 0);
14676             else
14677               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
14678           }
14679       }
14680       return;
14681
14682       /* An explanation of the 'Q', 'R' and 'H' register operands:
14683
14684          In a pair of registers containing a DI or DF value the 'Q'
14685          operand returns the register number of the register containing
14686          the least significant part of the value.  The 'R' operand returns
14687          the register number of the register containing the most
14688          significant part of the value.
14689
14690          The 'H' operand returns the higher of the two register numbers.
14691          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
14692          same as the 'Q' operand, since the most significant part of the
14693          value is held in the lower number register.  The reverse is true
14694          on systems where WORDS_BIG_ENDIAN is false.
14695
14696          The purpose of these operands is to distinguish between cases
14697          where the endian-ness of the values is important (for example
14698          when they are added together), and cases where the endian-ness
14699          is irrelevant, but the order of register operations is important.
14700          For example when loading a value from memory into a register
14701          pair, the endian-ness does not matter.  Provided that the value
14702          from the lower memory address is put into the lower numbered
14703          register, and the value from the higher address is put into the
14704          higher numbered register, the load will work regardless of whether
14705          the value being loaded is big-wordian or little-wordian.  The
14706          order of the two register loads can matter however, if the address
14707          of the memory location is actually held in one of the registers
14708          being overwritten by the load.  */
14709     case 'Q':
14710       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
14711         {
14712           output_operand_lossage ("invalid operand for code '%c'", code);
14713           return;
14714         }
14715
14716       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
14717       return;
14718
14719     case 'R':
14720       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
14721         {
14722           output_operand_lossage ("invalid operand for code '%c'", code);
14723           return;
14724         }
14725
14726       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
14727       return;
14728
14729     case 'H':
14730       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
14731         {
14732           output_operand_lossage ("invalid operand for code '%c'", code);
14733           return;
14734         }
14735
14736       asm_fprintf (stream, "%r", REGNO (x) + 1);
14737       return;
14738
14739     case 'J':
14740       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
14741         {
14742           output_operand_lossage ("invalid operand for code '%c'", code);
14743           return;
14744         }
14745
14746       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 3 : 2));
14747       return;
14748
14749     case 'K':
14750       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
14751         {
14752           output_operand_lossage ("invalid operand for code '%c'", code);
14753           return;
14754         }
14755
14756       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 2 : 3));
14757       return;
14758
14759     case 'm':
14760       asm_fprintf (stream, "%r",
14761                    GET_CODE (XEXP (x, 0)) == REG
14762                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
14763       return;
14764
14765     case 'M':
14766       asm_fprintf (stream, "{%r-%r}",
14767                    REGNO (x),
14768                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
14769       return;
14770
14771     /* Like 'M', but writing doubleword vector registers, for use by Neon
14772        insns.  */
14773     case 'h':
14774       {
14775         int regno = (REGNO (x) - FIRST_VFP_REGNUM) / 2;
14776         int numregs = ARM_NUM_REGS (GET_MODE (x)) / 2;
14777         if (numregs == 1)
14778           asm_fprintf (stream, "{d%d}", regno);
14779         else
14780           asm_fprintf (stream, "{d%d-d%d}", regno, regno + numregs - 1);
14781       }
14782       return;
14783
14784     case 'd':
14785       /* CONST_TRUE_RTX means always -- that's the default.  */
14786       if (x == const_true_rtx)
14787         return;
14788
14789       if (!COMPARISON_P (x))
14790         {
14791           output_operand_lossage ("invalid operand for code '%c'", code);
14792           return;
14793         }
14794
14795       fputs (arm_condition_codes[get_arm_condition_code (x)],
14796              stream);
14797       return;
14798
14799     case 'D':
14800       /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
14801          want to do that.  */
14802       if (x == const_true_rtx)
14803         {
14804           output_operand_lossage ("instruction never executed");
14805           return;
14806         }
14807       if (!COMPARISON_P (x))
14808         {
14809           output_operand_lossage ("invalid operand for code '%c'", code);
14810           return;
14811         }
14812
14813       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
14814                                  (get_arm_condition_code (x))],
14815              stream);
14816       return;
14817
14818     /* Cirrus registers can be accessed in a variety of ways:
14819          single floating point (f)
14820          double floating point (d)
14821          32bit integer         (fx)
14822          64bit integer         (dx).  */
14823     case 'W':                   /* Cirrus register in F mode.  */
14824     case 'X':                   /* Cirrus register in D mode.  */
14825     case 'Y':                   /* Cirrus register in FX mode.  */
14826     case 'Z':                   /* Cirrus register in DX mode.  */
14827       gcc_assert (GET_CODE (x) == REG
14828                   && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
14829
14830       fprintf (stream, "mv%s%s",
14831                code == 'W' ? "f"
14832                : code == 'X' ? "d"
14833                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
14834
14835       return;
14836
14837     /* Print cirrus register in the mode specified by the register's mode.  */
14838     case 'V':
14839       {
14840         int mode = GET_MODE (x);
14841
14842         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
14843           {
14844             output_operand_lossage ("invalid operand for code '%c'", code);
14845             return;
14846           }
14847
14848         fprintf (stream, "mv%s%s",
14849                  mode == DFmode ? "d"
14850                  : mode == SImode ? "fx"
14851                  : mode == DImode ? "dx"
14852                  : "f", reg_names[REGNO (x)] + 2);
14853
14854         return;
14855       }
14856
14857     case 'U':
14858       if (GET_CODE (x) != REG
14859           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
14860           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
14861         /* Bad value for wCG register number.  */
14862         {
14863           output_operand_lossage ("invalid operand for code '%c'", code);
14864           return;
14865         }
14866
14867       else
14868         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
14869       return;
14870
14871       /* Print an iWMMXt control register name.  */
14872     case 'w':
14873       if (GET_CODE (x) != CONST_INT
14874           || INTVAL (x) < 0
14875           || INTVAL (x) >= 16)
14876         /* Bad value for wC register number.  */
14877         {
14878           output_operand_lossage ("invalid operand for code '%c'", code);
14879           return;
14880         }
14881
14882       else
14883         {
14884           static const char * wc_reg_names [16] =
14885             {
14886               "wCID",  "wCon",  "wCSSF", "wCASF",
14887               "wC4",   "wC5",   "wC6",   "wC7",
14888               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
14889               "wC12",  "wC13",  "wC14",  "wC15"
14890             };
14891
14892           fprintf (stream, wc_reg_names [INTVAL (x)]);
14893         }
14894       return;
14895
14896     /* Print a VFP/Neon double precision or quad precision register name.  */
14897     case 'P':
14898     case 'q':
14899       {
14900         int mode = GET_MODE (x);
14901         int is_quad = (code == 'q');
14902         int regno;
14903
14904         if (GET_MODE_SIZE (mode) != (is_quad ? 16 : 8))
14905           {
14906             output_operand_lossage ("invalid operand for code '%c'", code);
14907             return;
14908           }
14909
14910         if (GET_CODE (x) != REG
14911             || !IS_VFP_REGNUM (REGNO (x)))
14912           {
14913             output_operand_lossage ("invalid operand for code '%c'", code);
14914             return;
14915           }
14916
14917         regno = REGNO (x);
14918         if ((is_quad && !NEON_REGNO_OK_FOR_QUAD (regno))
14919             || (!is_quad && !VFP_REGNO_OK_FOR_DOUBLE (regno)))
14920           {
14921             output_operand_lossage ("invalid operand for code '%c'", code);
14922             return;
14923           }
14924
14925         fprintf (stream, "%c%d", is_quad ? 'q' : 'd',
14926           (regno - FIRST_VFP_REGNUM) >> (is_quad ? 2 : 1));
14927       }
14928       return;
14929
14930     /* These two codes print the low/high doubleword register of a Neon quad
14931        register, respectively.  For pair-structure types, can also print
14932        low/high quadword registers.  */
14933     case 'e':
14934     case 'f':
14935       {
14936         int mode = GET_MODE (x);
14937         int regno;
14938
14939         if ((GET_MODE_SIZE (mode) != 16
14940              && GET_MODE_SIZE (mode) != 32) || GET_CODE (x) != REG)
14941           {
14942             output_operand_lossage ("invalid operand for code '%c'", code);
14943             return;
14944           }
14945
14946         regno = REGNO (x);
14947         if (!NEON_REGNO_OK_FOR_QUAD (regno))
14948           {
14949             output_operand_lossage ("invalid operand for code '%c'", code);
14950             return;
14951           }
14952
14953         if (GET_MODE_SIZE (mode) == 16)
14954           fprintf (stream, "d%d", ((regno - FIRST_VFP_REGNUM) >> 1)
14955                                   + (code == 'f' ? 1 : 0));
14956         else
14957           fprintf (stream, "q%d", ((regno - FIRST_VFP_REGNUM) >> 2)
14958                                   + (code == 'f' ? 1 : 0));
14959       }
14960       return;
14961
14962     /* Print a VFPv3 floating-point constant, represented as an integer
14963        index.  */
14964     case 'G':
14965       {
14966         int index = vfp3_const_double_index (x);
14967         gcc_assert (index != -1);
14968         fprintf (stream, "%d", index);
14969       }
14970       return;
14971
14972     /* Print bits representing opcode features for Neon.
14973
14974        Bit 0 is 1 for signed, 0 for unsigned.  Floats count as signed
14975        and polynomials as unsigned.
14976
14977        Bit 1 is 1 for floats and polynomials, 0 for ordinary integers.
14978
14979        Bit 2 is 1 for rounding functions, 0 otherwise.  */
14980
14981     /* Identify the type as 's', 'u', 'p' or 'f'.  */
14982     case 'T':
14983       {
14984         HOST_WIDE_INT bits = INTVAL (x);
14985         fputc ("uspf"[bits & 3], stream);
14986       }
14987       return;
14988
14989     /* Likewise, but signed and unsigned integers are both 'i'.  */
14990     case 'F':
14991       {
14992         HOST_WIDE_INT bits = INTVAL (x);
14993         fputc ("iipf"[bits & 3], stream);
14994       }
14995       return;
14996
14997     /* As for 'T', but emit 'u' instead of 'p'.  */
14998     case 't':
14999       {
15000         HOST_WIDE_INT bits = INTVAL (x);
15001         fputc ("usuf"[bits & 3], stream);
15002       }
15003       return;
15004
15005     /* Bit 2: rounding (vs none).  */
15006     case 'O':
15007       {
15008         HOST_WIDE_INT bits = INTVAL (x);
15009         fputs ((bits & 4) != 0 ? "r" : "", stream);
15010       }
15011       return;
15012
15013     /* Memory operand for vld1/vst1 instruction.  */
15014     case 'A':
15015       {
15016         rtx addr;
15017         bool postinc = FALSE;
15018         gcc_assert (GET_CODE (x) == MEM);
15019         addr = XEXP (x, 0);
15020         if (GET_CODE (addr) == POST_INC)
15021           {
15022             postinc = 1;
15023             addr = XEXP (addr, 0);
15024           }
15025         asm_fprintf (stream, "[%r]", REGNO (addr));
15026         if (postinc)
15027           fputs("!", stream);
15028       }
15029       return;
15030
15031     /* Register specifier for vld1.16/vst1.16.  Translate the S register
15032        number into a D register number and element index.  */
15033     case 'z':
15034       {
15035         int mode = GET_MODE (x);
15036         int regno;
15037
15038         if (GET_MODE_SIZE (mode) != 2 || GET_CODE (x) != REG)
15039           {
15040             output_operand_lossage ("invalid operand for code '%c'", code);
15041             return;
15042           }
15043
15044         regno = REGNO (x);
15045         if (!VFP_REGNO_OK_FOR_SINGLE (regno))
15046           {
15047             output_operand_lossage ("invalid operand for code '%c'", code);
15048             return;
15049           }
15050
15051         regno = regno - FIRST_VFP_REGNUM;
15052         fprintf (stream, "d%d[%d]", regno/2, ((regno % 2) ? 2 : 0));
15053       }
15054       return;
15055       
15056     default:
15057       if (x == 0)
15058         {
15059           output_operand_lossage ("missing operand");
15060           return;
15061         }
15062
15063       switch (GET_CODE (x))
15064         {
15065         case REG:
15066           asm_fprintf (stream, "%r", REGNO (x));
15067           break;
15068
15069         case MEM:
15070           output_memory_reference_mode = GET_MODE (x);
15071           output_address (XEXP (x, 0));
15072           break;
15073
15074         case CONST_DOUBLE:
15075           if (TARGET_NEON)
15076             {
15077               char fpstr[20];
15078               real_to_decimal (fpstr, CONST_DOUBLE_REAL_VALUE (x),
15079                                sizeof (fpstr), 0, 1);
15080               fprintf (stream, "#%s", fpstr);
15081             }
15082           else
15083             fprintf (stream, "#%s", fp_immediate_constant (x));
15084           break;
15085
15086         default:
15087           gcc_assert (GET_CODE (x) != NEG);
15088           fputc ('#', stream);
15089           if (GET_CODE (x) == HIGH)
15090             {
15091               fputs (":lower16:", stream);
15092               x = XEXP (x, 0);
15093             }
15094             
15095           output_addr_const (stream, x);
15096           break;
15097         }
15098     }
15099 }
15100 \f
15101 /* Target hook for assembling integer objects.  The ARM version needs to
15102    handle word-sized values specially.  */
15103 static bool
15104 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
15105 {
15106   enum machine_mode mode;
15107
15108   if (size == UNITS_PER_WORD && aligned_p)
15109     {
15110       fputs ("\t.word\t", asm_out_file);
15111       output_addr_const (asm_out_file, x);
15112
15113       /* Mark symbols as position independent.  We only do this in the
15114          .text segment, not in the .data segment.  */
15115       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
15116           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
15117         {
15118           /* See legitimize_pic_address for an explanation of the
15119              TARGET_VXWORKS_RTP check.  */
15120           if (TARGET_VXWORKS_RTP
15121               || (GET_CODE (x) == SYMBOL_REF && !SYMBOL_REF_LOCAL_P (x)))
15122             fputs ("(GOT)", asm_out_file);
15123           else
15124             fputs ("(GOTOFF)", asm_out_file);
15125         }
15126       fputc ('\n', asm_out_file);
15127       return true;
15128     }
15129
15130   mode = GET_MODE (x);
15131
15132   if (arm_vector_mode_supported_p (mode))
15133     {
15134       int i, units;
15135
15136       gcc_assert (GET_CODE (x) == CONST_VECTOR);
15137
15138       units = CONST_VECTOR_NUNITS (x);
15139       size = GET_MODE_SIZE (GET_MODE_INNER (mode));
15140
15141       if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
15142         for (i = 0; i < units; i++)
15143           {
15144             rtx elt = CONST_VECTOR_ELT (x, i);
15145             assemble_integer
15146               (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
15147           }
15148       else
15149         for (i = 0; i < units; i++)
15150           {
15151             rtx elt = CONST_VECTOR_ELT (x, i);
15152             REAL_VALUE_TYPE rval;
15153
15154             REAL_VALUE_FROM_CONST_DOUBLE (rval, elt);
15155
15156             assemble_real
15157               (rval, GET_MODE_INNER (mode),
15158               i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT);
15159           }
15160
15161       return true;
15162     }
15163
15164   return default_assemble_integer (x, size, aligned_p);
15165 }
15166
15167 static void
15168 arm_elf_asm_cdtor (rtx symbol, int priority, bool is_ctor)
15169 {
15170   section *s;
15171
15172   if (!TARGET_AAPCS_BASED)
15173     {
15174       (is_ctor ? 
15175        default_named_section_asm_out_constructor 
15176        : default_named_section_asm_out_destructor) (symbol, priority);
15177       return;
15178     }
15179
15180   /* Put these in the .init_array section, using a special relocation.  */
15181   if (priority != DEFAULT_INIT_PRIORITY)
15182     {
15183       char buf[18];
15184       sprintf (buf, "%s.%.5u", 
15185                is_ctor ? ".init_array" : ".fini_array",
15186                priority);
15187       s = get_section (buf, SECTION_WRITE, NULL_TREE);
15188     }
15189   else if (is_ctor)
15190     s = ctors_section;
15191   else
15192     s = dtors_section;
15193
15194   switch_to_section (s);
15195   assemble_align (POINTER_SIZE);
15196   fputs ("\t.word\t", asm_out_file);
15197   output_addr_const (asm_out_file, symbol);
15198   fputs ("(target1)\n", asm_out_file);
15199 }
15200
15201 /* Add a function to the list of static constructors.  */
15202
15203 static void
15204 arm_elf_asm_constructor (rtx symbol, int priority)
15205 {
15206   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/true);
15207 }
15208
15209 /* Add a function to the list of static destructors.  */
15210
15211 static void
15212 arm_elf_asm_destructor (rtx symbol, int priority)
15213 {
15214   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/false);
15215 }
15216 \f
15217 /* A finite state machine takes care of noticing whether or not instructions
15218    can be conditionally executed, and thus decrease execution time and code
15219    size by deleting branch instructions.  The fsm is controlled by
15220    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
15221
15222 /* The state of the fsm controlling condition codes are:
15223    0: normal, do nothing special
15224    1: make ASM_OUTPUT_OPCODE not output this instruction
15225    2: make ASM_OUTPUT_OPCODE not output this instruction
15226    3: make instructions conditional
15227    4: make instructions conditional
15228
15229    State transitions (state->state by whom under condition):
15230    0 -> 1 final_prescan_insn if the `target' is a label
15231    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
15232    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
15233    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
15234    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
15235           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
15236    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
15237           (the target insn is arm_target_insn).
15238
15239    If the jump clobbers the conditions then we use states 2 and 4.
15240
15241    A similar thing can be done with conditional return insns.
15242
15243    XXX In case the `target' is an unconditional branch, this conditionalising
15244    of the instructions always reduces code size, but not always execution
15245    time.  But then, I want to reduce the code size to somewhere near what
15246    /bin/cc produces.  */
15247
15248 /* In addition to this, state is maintained for Thumb-2 COND_EXEC
15249    instructions.  When a COND_EXEC instruction is seen the subsequent
15250    instructions are scanned so that multiple conditional instructions can be
15251    combined into a single IT block.  arm_condexec_count and arm_condexec_mask
15252    specify the length and true/false mask for the IT block.  These will be
15253    decremented/zeroed by arm_asm_output_opcode as the insns are output.  */
15254
15255 /* Returns the index of the ARM condition code string in
15256    `arm_condition_codes'.  COMPARISON should be an rtx like
15257    `(eq (...) (...))'.  */
15258 static enum arm_cond_code
15259 get_arm_condition_code (rtx comparison)
15260 {
15261   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
15262   enum arm_cond_code code;
15263   enum rtx_code comp_code = GET_CODE (comparison);
15264
15265   if (GET_MODE_CLASS (mode) != MODE_CC)
15266     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
15267                            XEXP (comparison, 1));
15268
15269   switch (mode)
15270     {
15271     case CC_DNEmode: code = ARM_NE; goto dominance;
15272     case CC_DEQmode: code = ARM_EQ; goto dominance;
15273     case CC_DGEmode: code = ARM_GE; goto dominance;
15274     case CC_DGTmode: code = ARM_GT; goto dominance;
15275     case CC_DLEmode: code = ARM_LE; goto dominance;
15276     case CC_DLTmode: code = ARM_LT; goto dominance;
15277     case CC_DGEUmode: code = ARM_CS; goto dominance;
15278     case CC_DGTUmode: code = ARM_HI; goto dominance;
15279     case CC_DLEUmode: code = ARM_LS; goto dominance;
15280     case CC_DLTUmode: code = ARM_CC;
15281
15282     dominance:
15283       gcc_assert (comp_code == EQ || comp_code == NE);
15284
15285       if (comp_code == EQ)
15286         return ARM_INVERSE_CONDITION_CODE (code);
15287       return code;
15288
15289     case CC_NOOVmode:
15290       switch (comp_code)
15291         {
15292         case NE: return ARM_NE;
15293         case EQ: return ARM_EQ;
15294         case GE: return ARM_PL;
15295         case LT: return ARM_MI;
15296         default: gcc_unreachable ();
15297         }
15298
15299     case CC_Zmode:
15300       switch (comp_code)
15301         {
15302         case NE: return ARM_NE;
15303         case EQ: return ARM_EQ;
15304         default: gcc_unreachable ();
15305         }
15306
15307     case CC_Nmode:
15308       switch (comp_code)
15309         {
15310         case NE: return ARM_MI;
15311         case EQ: return ARM_PL;
15312         default: gcc_unreachable ();
15313         }
15314
15315     case CCFPEmode:
15316     case CCFPmode:
15317       /* These encodings assume that AC=1 in the FPA system control
15318          byte.  This allows us to handle all cases except UNEQ and
15319          LTGT.  */
15320       switch (comp_code)
15321         {
15322         case GE: return ARM_GE;
15323         case GT: return ARM_GT;
15324         case LE: return ARM_LS;
15325         case LT: return ARM_MI;
15326         case NE: return ARM_NE;
15327         case EQ: return ARM_EQ;
15328         case ORDERED: return ARM_VC;
15329         case UNORDERED: return ARM_VS;
15330         case UNLT: return ARM_LT;
15331         case UNLE: return ARM_LE;
15332         case UNGT: return ARM_HI;
15333         case UNGE: return ARM_PL;
15334           /* UNEQ and LTGT do not have a representation.  */
15335         case UNEQ: /* Fall through.  */
15336         case LTGT: /* Fall through.  */
15337         default: gcc_unreachable ();
15338         }
15339
15340     case CC_SWPmode:
15341       switch (comp_code)
15342         {
15343         case NE: return ARM_NE;
15344         case EQ: return ARM_EQ;
15345         case GE: return ARM_LE;
15346         case GT: return ARM_LT;
15347         case LE: return ARM_GE;
15348         case LT: return ARM_GT;
15349         case GEU: return ARM_LS;
15350         case GTU: return ARM_CC;
15351         case LEU: return ARM_CS;
15352         case LTU: return ARM_HI;
15353         default: gcc_unreachable ();
15354         }
15355
15356     case CC_Cmode:
15357       switch (comp_code)
15358       {
15359       case LTU: return ARM_CS;
15360       case GEU: return ARM_CC;
15361       default: gcc_unreachable ();
15362       }
15363
15364     case CCmode:
15365       switch (comp_code)
15366         {
15367         case NE: return ARM_NE;
15368         case EQ: return ARM_EQ;
15369         case GE: return ARM_GE;
15370         case GT: return ARM_GT;
15371         case LE: return ARM_LE;
15372         case LT: return ARM_LT;
15373         case GEU: return ARM_CS;
15374         case GTU: return ARM_HI;
15375         case LEU: return ARM_LS;
15376         case LTU: return ARM_CC;
15377         default: gcc_unreachable ();
15378         }
15379
15380     default: gcc_unreachable ();
15381     }
15382 }
15383
15384 /* Tell arm_asm_output_opcode to output IT blocks for conditionally executed
15385    instructions.  */
15386 void
15387 thumb2_final_prescan_insn (rtx insn)
15388 {
15389   rtx first_insn = insn;
15390   rtx body = PATTERN (insn);
15391   rtx predicate;
15392   enum arm_cond_code code;
15393   int n;
15394   int mask;
15395
15396   /* Remove the previous insn from the count of insns to be output.  */
15397   if (arm_condexec_count)
15398       arm_condexec_count--;
15399
15400   /* Nothing to do if we are already inside a conditional block.  */
15401   if (arm_condexec_count)
15402     return;
15403
15404   if (GET_CODE (body) != COND_EXEC)
15405     return;
15406
15407   /* Conditional jumps are implemented directly.  */
15408   if (GET_CODE (insn) == JUMP_INSN)
15409     return;
15410
15411   predicate = COND_EXEC_TEST (body);
15412   arm_current_cc = get_arm_condition_code (predicate);
15413
15414   n = get_attr_ce_count (insn);
15415   arm_condexec_count = 1;
15416   arm_condexec_mask = (1 << n) - 1;
15417   arm_condexec_masklen = n;
15418   /* See if subsequent instructions can be combined into the same block.  */
15419   for (;;)
15420     {
15421       insn = next_nonnote_insn (insn);
15422
15423       /* Jumping into the middle of an IT block is illegal, so a label or
15424          barrier terminates the block.  */
15425       if (GET_CODE (insn) != INSN && GET_CODE(insn) != JUMP_INSN)
15426         break;
15427
15428       body = PATTERN (insn);
15429       /* USE and CLOBBER aren't really insns, so just skip them.  */
15430       if (GET_CODE (body) == USE
15431           || GET_CODE (body) == CLOBBER)
15432         continue;
15433
15434       /* ??? Recognize conditional jumps, and combine them with IT blocks.  */
15435       if (GET_CODE (body) != COND_EXEC)
15436         break;
15437       /* Allow up to 4 conditionally executed instructions in a block.  */
15438       n = get_attr_ce_count (insn);
15439       if (arm_condexec_masklen + n > 4)
15440         break;
15441
15442       predicate = COND_EXEC_TEST (body);
15443       code = get_arm_condition_code (predicate);
15444       mask = (1 << n) - 1;
15445       if (arm_current_cc == code)
15446         arm_condexec_mask |= (mask << arm_condexec_masklen);
15447       else if (arm_current_cc != ARM_INVERSE_CONDITION_CODE(code))
15448         break;
15449
15450       arm_condexec_count++;
15451       arm_condexec_masklen += n;
15452
15453       /* A jump must be the last instruction in a conditional block.  */
15454       if (GET_CODE(insn) == JUMP_INSN)
15455         break;
15456     }
15457   /* Restore recog_data (getting the attributes of other insns can
15458      destroy this array, but final.c assumes that it remains intact
15459      across this call).  */
15460   extract_constrain_insn_cached (first_insn);
15461 }
15462
15463 void
15464 arm_final_prescan_insn (rtx insn)
15465 {
15466   /* BODY will hold the body of INSN.  */
15467   rtx body = PATTERN (insn);
15468
15469   /* This will be 1 if trying to repeat the trick, and things need to be
15470      reversed if it appears to fail.  */
15471   int reverse = 0;
15472
15473   /* If we start with a return insn, we only succeed if we find another one.  */
15474   int seeking_return = 0;
15475
15476   /* START_INSN will hold the insn from where we start looking.  This is the
15477      first insn after the following code_label if REVERSE is true.  */
15478   rtx start_insn = insn;
15479
15480   /* If in state 4, check if the target branch is reached, in order to
15481      change back to state 0.  */
15482   if (arm_ccfsm_state == 4)
15483     {
15484       if (insn == arm_target_insn)
15485         {
15486           arm_target_insn = NULL;
15487           arm_ccfsm_state = 0;
15488         }
15489       return;
15490     }
15491
15492   /* If in state 3, it is possible to repeat the trick, if this insn is an
15493      unconditional branch to a label, and immediately following this branch
15494      is the previous target label which is only used once, and the label this
15495      branch jumps to is not too far off.  */
15496   if (arm_ccfsm_state == 3)
15497     {
15498       if (simplejump_p (insn))
15499         {
15500           start_insn = next_nonnote_insn (start_insn);
15501           if (GET_CODE (start_insn) == BARRIER)
15502             {
15503               /* XXX Isn't this always a barrier?  */
15504               start_insn = next_nonnote_insn (start_insn);
15505             }
15506           if (GET_CODE (start_insn) == CODE_LABEL
15507               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
15508               && LABEL_NUSES (start_insn) == 1)
15509             reverse = TRUE;
15510           else
15511             return;
15512         }
15513       else if (GET_CODE (body) == RETURN)
15514         {
15515           start_insn = next_nonnote_insn (start_insn);
15516           if (GET_CODE (start_insn) == BARRIER)
15517             start_insn = next_nonnote_insn (start_insn);
15518           if (GET_CODE (start_insn) == CODE_LABEL
15519               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
15520               && LABEL_NUSES (start_insn) == 1)
15521             {
15522               reverse = TRUE;
15523               seeking_return = 1;
15524             }
15525           else
15526             return;
15527         }
15528       else
15529         return;
15530     }
15531
15532   gcc_assert (!arm_ccfsm_state || reverse);
15533   if (GET_CODE (insn) != JUMP_INSN)
15534     return;
15535
15536   /* This jump might be paralleled with a clobber of the condition codes
15537      the jump should always come first */
15538   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
15539     body = XVECEXP (body, 0, 0);
15540
15541   if (reverse
15542       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
15543           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
15544     {
15545       int insns_skipped;
15546       int fail = FALSE, succeed = FALSE;
15547       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
15548       int then_not_else = TRUE;
15549       rtx this_insn = start_insn, label = 0;
15550
15551       /* Register the insn jumped to.  */
15552       if (reverse)
15553         {
15554           if (!seeking_return)
15555             label = XEXP (SET_SRC (body), 0);
15556         }
15557       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
15558         label = XEXP (XEXP (SET_SRC (body), 1), 0);
15559       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
15560         {
15561           label = XEXP (XEXP (SET_SRC (body), 2), 0);
15562           then_not_else = FALSE;
15563         }
15564       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
15565         seeking_return = 1;
15566       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
15567         {
15568           seeking_return = 1;
15569           then_not_else = FALSE;
15570         }
15571       else
15572         gcc_unreachable ();
15573
15574       /* See how many insns this branch skips, and what kind of insns.  If all
15575          insns are okay, and the label or unconditional branch to the same
15576          label is not too far away, succeed.  */
15577       for (insns_skipped = 0;
15578            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
15579         {
15580           rtx scanbody;
15581
15582           this_insn = next_nonnote_insn (this_insn);
15583           if (!this_insn)
15584             break;
15585
15586           switch (GET_CODE (this_insn))
15587             {
15588             case CODE_LABEL:
15589               /* Succeed if it is the target label, otherwise fail since
15590                  control falls in from somewhere else.  */
15591               if (this_insn == label)
15592                 {
15593                   arm_ccfsm_state = 1;
15594                   succeed = TRUE;
15595                 }
15596               else
15597                 fail = TRUE;
15598               break;
15599
15600             case BARRIER:
15601               /* Succeed if the following insn is the target label.
15602                  Otherwise fail.
15603                  If return insns are used then the last insn in a function
15604                  will be a barrier.  */
15605               this_insn = next_nonnote_insn (this_insn);
15606               if (this_insn && this_insn == label)
15607                 {
15608                   arm_ccfsm_state = 1;
15609                   succeed = TRUE;
15610                 }
15611               else
15612                 fail = TRUE;
15613               break;
15614
15615             case CALL_INSN:
15616               /* The AAPCS says that conditional calls should not be
15617                  used since they make interworking inefficient (the
15618                  linker can't transform BL<cond> into BLX).  That's
15619                  only a problem if the machine has BLX.  */
15620               if (arm_arch5)
15621                 {
15622                   fail = TRUE;
15623                   break;
15624                 }
15625
15626               /* Succeed if the following insn is the target label, or
15627                  if the following two insns are a barrier and the
15628                  target label.  */
15629               this_insn = next_nonnote_insn (this_insn);
15630               if (this_insn && GET_CODE (this_insn) == BARRIER)
15631                 this_insn = next_nonnote_insn (this_insn);
15632
15633               if (this_insn && this_insn == label
15634                   && insns_skipped < max_insns_skipped)
15635                 {
15636                   arm_ccfsm_state = 1;
15637                   succeed = TRUE;
15638                 }
15639               else
15640                 fail = TRUE;
15641               break;
15642
15643             case JUMP_INSN:
15644               /* If this is an unconditional branch to the same label, succeed.
15645                  If it is to another label, do nothing.  If it is conditional,
15646                  fail.  */
15647               /* XXX Probably, the tests for SET and the PC are
15648                  unnecessary.  */
15649
15650               scanbody = PATTERN (this_insn);
15651               if (GET_CODE (scanbody) == SET
15652                   && GET_CODE (SET_DEST (scanbody)) == PC)
15653                 {
15654                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
15655                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
15656                     {
15657                       arm_ccfsm_state = 2;
15658                       succeed = TRUE;
15659                     }
15660                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
15661                     fail = TRUE;
15662                 }
15663               /* Fail if a conditional return is undesirable (e.g. on a
15664                  StrongARM), but still allow this if optimizing for size.  */
15665               else if (GET_CODE (scanbody) == RETURN
15666                        && !use_return_insn (TRUE, NULL)
15667                        && !optimize_size)
15668                 fail = TRUE;
15669               else if (GET_CODE (scanbody) == RETURN
15670                        && seeking_return)
15671                 {
15672                   arm_ccfsm_state = 2;
15673                   succeed = TRUE;
15674                 }
15675               else if (GET_CODE (scanbody) == PARALLEL)
15676                 {
15677                   switch (get_attr_conds (this_insn))
15678                     {
15679                     case CONDS_NOCOND:
15680                       break;
15681                     default:
15682                       fail = TRUE;
15683                       break;
15684                     }
15685                 }
15686               else
15687                 fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
15688
15689               break;
15690
15691             case INSN:
15692               /* Instructions using or affecting the condition codes make it
15693                  fail.  */
15694               scanbody = PATTERN (this_insn);
15695               if (!(GET_CODE (scanbody) == SET
15696                     || GET_CODE (scanbody) == PARALLEL)
15697                   || get_attr_conds (this_insn) != CONDS_NOCOND)
15698                 fail = TRUE;
15699
15700               /* A conditional cirrus instruction must be followed by
15701                  a non Cirrus instruction.  However, since we
15702                  conditionalize instructions in this function and by
15703                  the time we get here we can't add instructions
15704                  (nops), because shorten_branches() has already been
15705                  called, we will disable conditionalizing Cirrus
15706                  instructions to be safe.  */
15707               if (GET_CODE (scanbody) != USE
15708                   && GET_CODE (scanbody) != CLOBBER
15709                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
15710                 fail = TRUE;
15711               break;
15712
15713             default:
15714               break;
15715             }
15716         }
15717       if (succeed)
15718         {
15719           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
15720             arm_target_label = CODE_LABEL_NUMBER (label);
15721           else
15722             {
15723               gcc_assert (seeking_return || arm_ccfsm_state == 2);
15724
15725               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
15726                 {
15727                   this_insn = next_nonnote_insn (this_insn);
15728                   gcc_assert (!this_insn
15729                               || (GET_CODE (this_insn) != BARRIER
15730                                   && GET_CODE (this_insn) != CODE_LABEL));
15731                 }
15732               if (!this_insn)
15733                 {
15734                   /* Oh, dear! we ran off the end.. give up.  */
15735                   extract_constrain_insn_cached (insn);
15736                   arm_ccfsm_state = 0;
15737                   arm_target_insn = NULL;
15738                   return;
15739                 }
15740               arm_target_insn = this_insn;
15741             }
15742
15743           /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
15744              what it was.  */
15745           if (!reverse)
15746             arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body), 0));
15747
15748           if (reverse || then_not_else)
15749             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
15750         }
15751
15752       /* Restore recog_data (getting the attributes of other insns can
15753          destroy this array, but final.c assumes that it remains intact
15754          across this call.  */
15755       extract_constrain_insn_cached (insn);
15756     }
15757 }
15758
15759 /* Output IT instructions.  */
15760 void
15761 thumb2_asm_output_opcode (FILE * stream)
15762 {
15763   char buff[5];
15764   int n;
15765
15766   if (arm_condexec_mask)
15767     {
15768       for (n = 0; n < arm_condexec_masklen; n++)
15769         buff[n] = (arm_condexec_mask & (1 << n)) ? 't' : 'e';
15770       buff[n] = 0;
15771       asm_fprintf(stream, "i%s\t%s\n\t", buff,
15772                   arm_condition_codes[arm_current_cc]);
15773       arm_condexec_mask = 0;
15774     }
15775 }
15776
15777 /* Returns true if REGNO is a valid register
15778    for holding a quantity of type MODE.  */
15779 int
15780 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
15781 {
15782   if (GET_MODE_CLASS (mode) == MODE_CC)
15783     return (regno == CC_REGNUM
15784             || (TARGET_HARD_FLOAT && TARGET_VFP
15785                 && regno == VFPCC_REGNUM));
15786
15787   if (TARGET_THUMB1)
15788     /* For the Thumb we only allow values bigger than SImode in
15789        registers 0 - 6, so that there is always a second low
15790        register available to hold the upper part of the value.
15791        We probably we ought to ensure that the register is the
15792        start of an even numbered register pair.  */
15793     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
15794
15795   if (TARGET_HARD_FLOAT && TARGET_MAVERICK
15796       && IS_CIRRUS_REGNUM (regno))
15797     /* We have outlawed SI values in Cirrus registers because they
15798        reside in the lower 32 bits, but SF values reside in the
15799        upper 32 bits.  This causes gcc all sorts of grief.  We can't
15800        even split the registers into pairs because Cirrus SI values
15801        get sign extended to 64bits-- aldyh.  */
15802     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
15803
15804   if (TARGET_HARD_FLOAT && TARGET_VFP
15805       && IS_VFP_REGNUM (regno))
15806     {
15807       if (mode == SFmode || mode == SImode)
15808         return VFP_REGNO_OK_FOR_SINGLE (regno);
15809
15810       if (mode == DFmode)
15811         return VFP_REGNO_OK_FOR_DOUBLE (regno);
15812
15813       /* VFP registers can hold HFmode values, but there is no point in
15814          putting them there unless we have the NEON extensions for
15815          loading/storing them, too.  */
15816       if (mode == HFmode)
15817         return TARGET_NEON_FP16 && VFP_REGNO_OK_FOR_SINGLE (regno);
15818
15819       if (TARGET_NEON)
15820         return (VALID_NEON_DREG_MODE (mode) && VFP_REGNO_OK_FOR_DOUBLE (regno))
15821                || (VALID_NEON_QREG_MODE (mode)
15822                    && NEON_REGNO_OK_FOR_QUAD (regno))
15823                || (mode == TImode && NEON_REGNO_OK_FOR_NREGS (regno, 2))
15824                || (mode == EImode && NEON_REGNO_OK_FOR_NREGS (regno, 3))
15825                || (mode == OImode && NEON_REGNO_OK_FOR_NREGS (regno, 4))
15826                || (mode == CImode && NEON_REGNO_OK_FOR_NREGS (regno, 6))
15827                || (mode == XImode && NEON_REGNO_OK_FOR_NREGS (regno, 8));
15828
15829       return FALSE;
15830     }
15831
15832   if (TARGET_REALLY_IWMMXT)
15833     {
15834       if (IS_IWMMXT_GR_REGNUM (regno))
15835         return mode == SImode;
15836
15837       if (IS_IWMMXT_REGNUM (regno))
15838         return VALID_IWMMXT_REG_MODE (mode);
15839     }
15840   
15841   /* We allow almost any value to be stored in the general registers.
15842      Restrict doubleword quantities to even register pairs so that we can
15843      use ldrd.  Do not allow very large Neon structure opaque modes in
15844      general registers; they would use too many.  */
15845   if (regno <= LAST_ARM_REGNUM)
15846     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0)
15847       && ARM_NUM_REGS (mode) <= 4;
15848
15849   if (regno == FRAME_POINTER_REGNUM
15850       || regno == ARG_POINTER_REGNUM)
15851     /* We only allow integers in the fake hard registers.  */
15852     return GET_MODE_CLASS (mode) == MODE_INT;
15853
15854   /* The only registers left are the FPA registers
15855      which we only allow to hold FP values.  */
15856   return (TARGET_HARD_FLOAT && TARGET_FPA
15857           && GET_MODE_CLASS (mode) == MODE_FLOAT
15858           && regno >= FIRST_FPA_REGNUM
15859           && regno <= LAST_FPA_REGNUM);
15860 }
15861
15862 /* For efficiency and historical reasons LO_REGS, HI_REGS and CC_REGS are
15863    not used in arm mode.  */
15864
15865 enum reg_class
15866 arm_regno_class (int regno)
15867 {
15868   if (TARGET_THUMB1)
15869     {
15870       if (regno == STACK_POINTER_REGNUM)
15871         return STACK_REG;
15872       if (regno == CC_REGNUM)
15873         return CC_REG;
15874       if (regno < 8)
15875         return LO_REGS;
15876       return HI_REGS;
15877     }
15878
15879   if (TARGET_THUMB2 && regno < 8)
15880     return LO_REGS;
15881
15882   if (   regno <= LAST_ARM_REGNUM
15883       || regno == FRAME_POINTER_REGNUM
15884       || regno == ARG_POINTER_REGNUM)
15885     return TARGET_THUMB2 ? HI_REGS : GENERAL_REGS;
15886
15887   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
15888     return TARGET_THUMB2 ? CC_REG : NO_REGS;
15889
15890   if (IS_CIRRUS_REGNUM (regno))
15891     return CIRRUS_REGS;
15892
15893   if (IS_VFP_REGNUM (regno))
15894     {
15895       if (regno <= D7_VFP_REGNUM)
15896         return VFP_D0_D7_REGS;
15897       else if (regno <= LAST_LO_VFP_REGNUM)
15898         return VFP_LO_REGS;
15899       else
15900         return VFP_HI_REGS;
15901     }
15902
15903   if (IS_IWMMXT_REGNUM (regno))
15904     return IWMMXT_REGS;
15905
15906   if (IS_IWMMXT_GR_REGNUM (regno))
15907     return IWMMXT_GR_REGS;
15908
15909   return FPA_REGS;
15910 }
15911
15912 /* Handle a special case when computing the offset
15913    of an argument from the frame pointer.  */
15914 int
15915 arm_debugger_arg_offset (int value, rtx addr)
15916 {
15917   rtx insn;
15918
15919   /* We are only interested if dbxout_parms() failed to compute the offset.  */
15920   if (value != 0)
15921     return 0;
15922
15923   /* We can only cope with the case where the address is held in a register.  */
15924   if (GET_CODE (addr) != REG)
15925     return 0;
15926
15927   /* If we are using the frame pointer to point at the argument, then
15928      an offset of 0 is correct.  */
15929   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
15930     return 0;
15931
15932   /* If we are using the stack pointer to point at the
15933      argument, then an offset of 0 is correct.  */
15934   /* ??? Check this is consistent with thumb2 frame layout.  */
15935   if ((TARGET_THUMB || !frame_pointer_needed)
15936       && REGNO (addr) == SP_REGNUM)
15937     return 0;
15938
15939   /* Oh dear.  The argument is pointed to by a register rather
15940      than being held in a register, or being stored at a known
15941      offset from the frame pointer.  Since GDB only understands
15942      those two kinds of argument we must translate the address
15943      held in the register into an offset from the frame pointer.
15944      We do this by searching through the insns for the function
15945      looking to see where this register gets its value.  If the
15946      register is initialized from the frame pointer plus an offset
15947      then we are in luck and we can continue, otherwise we give up.
15948
15949      This code is exercised by producing debugging information
15950      for a function with arguments like this:
15951
15952            double func (double a, double b, int c, double d) {return d;}
15953
15954      Without this code the stab for parameter 'd' will be set to
15955      an offset of 0 from the frame pointer, rather than 8.  */
15956
15957   /* The if() statement says:
15958
15959      If the insn is a normal instruction
15960      and if the insn is setting the value in a register
15961      and if the register being set is the register holding the address of the argument
15962      and if the address is computing by an addition
15963      that involves adding to a register
15964      which is the frame pointer
15965      a constant integer
15966
15967      then...  */
15968
15969   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
15970     {
15971       if (   GET_CODE (insn) == INSN
15972           && GET_CODE (PATTERN (insn)) == SET
15973           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
15974           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
15975           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
15976           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
15977           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
15978              )
15979         {
15980           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
15981
15982           break;
15983         }
15984     }
15985
15986   if (value == 0)
15987     {
15988       debug_rtx (addr);
15989       warning (0, "unable to compute real location of stacked parameter");
15990       value = 8; /* XXX magic hack */
15991     }
15992
15993   return value;
15994 }
15995 \f
15996 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
15997   do                                                                    \
15998     {                                                                   \
15999       if ((MASK) & insn_flags)                                          \
16000         add_builtin_function ((NAME), (TYPE), (CODE),                   \
16001                              BUILT_IN_MD, NULL, NULL_TREE);             \
16002     }                                                                   \
16003   while (0)
16004
16005 struct builtin_description
16006 {
16007   const unsigned int       mask;
16008   const enum insn_code     icode;
16009   const char * const       name;
16010   const enum arm_builtins  code;
16011   const enum rtx_code      comparison;
16012   const unsigned int       flag;
16013 };
16014
16015 static const struct builtin_description bdesc_2arg[] =
16016 {
16017 #define IWMMXT_BUILTIN(code, string, builtin) \
16018   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
16019     ARM_BUILTIN_##builtin, UNKNOWN, 0 },
16020
16021   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
16022   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
16023   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
16024   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
16025   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
16026   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
16027   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
16028   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
16029   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
16030   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
16031   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
16032   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
16033   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
16034   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
16035   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
16036   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
16037   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
16038   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
16039   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
16040   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
16041   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
16042   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
16043   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
16044   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
16045   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
16046   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
16047   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
16048   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
16049   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
16050   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
16051   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
16052   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
16053   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
16054   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
16055   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
16056   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
16057   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
16058   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
16059   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
16060   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
16061   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
16062   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
16063   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
16064   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
16065   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
16066   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
16067   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
16068   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
16069   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
16070   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
16071   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
16072   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
16073   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
16074   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
16075   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
16076   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
16077   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
16078   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
16079
16080 #define IWMMXT_BUILTIN2(code, builtin) \
16081   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, UNKNOWN, 0 },
16082
16083   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
16084   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
16085   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
16086   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
16087   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
16088   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
16089   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
16090   IWMMXT_BUILTIN2 (ashlv4hi3_iwmmxt, WSLLHI)
16091   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
16092   IWMMXT_BUILTIN2 (ashlv2si3_iwmmxt, WSLLWI)
16093   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
16094   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
16095   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
16096   IWMMXT_BUILTIN2 (lshrv4hi3_iwmmxt, WSRLHI)
16097   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
16098   IWMMXT_BUILTIN2 (lshrv2si3_iwmmxt, WSRLWI)
16099   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
16100   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
16101   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
16102   IWMMXT_BUILTIN2 (ashrv4hi3_iwmmxt, WSRAHI)
16103   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
16104   IWMMXT_BUILTIN2 (ashrv2si3_iwmmxt, WSRAWI)
16105   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
16106   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
16107   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
16108   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
16109   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
16110   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
16111   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
16112   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
16113   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
16114   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
16115 };
16116
16117 static const struct builtin_description bdesc_1arg[] =
16118 {
16119   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
16120   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
16121   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
16122   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
16123   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
16124   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
16125   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
16126   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
16127   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
16128   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
16129   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
16130   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
16131   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
16132   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
16133   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
16134   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
16135   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
16136   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
16137 };
16138
16139 /* Set up all the iWMMXt builtins.  This is
16140    not called if TARGET_IWMMXT is zero.  */
16141
16142 static void
16143 arm_init_iwmmxt_builtins (void)
16144 {
16145   const struct builtin_description * d;
16146   size_t i;
16147   tree endlink = void_list_node;
16148
16149   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
16150   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
16151   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
16152
16153   tree int_ftype_int
16154     = build_function_type (integer_type_node,
16155                            tree_cons (NULL_TREE, integer_type_node, endlink));
16156   tree v8qi_ftype_v8qi_v8qi_int
16157     = build_function_type (V8QI_type_node,
16158                            tree_cons (NULL_TREE, V8QI_type_node,
16159                                       tree_cons (NULL_TREE, V8QI_type_node,
16160                                                  tree_cons (NULL_TREE,
16161                                                             integer_type_node,
16162                                                             endlink))));
16163   tree v4hi_ftype_v4hi_int
16164     = build_function_type (V4HI_type_node,
16165                            tree_cons (NULL_TREE, V4HI_type_node,
16166                                       tree_cons (NULL_TREE, integer_type_node,
16167                                                  endlink)));
16168   tree v2si_ftype_v2si_int
16169     = build_function_type (V2SI_type_node,
16170                            tree_cons (NULL_TREE, V2SI_type_node,
16171                                       tree_cons (NULL_TREE, integer_type_node,
16172                                                  endlink)));
16173   tree v2si_ftype_di_di
16174     = build_function_type (V2SI_type_node,
16175                            tree_cons (NULL_TREE, long_long_integer_type_node,
16176                                       tree_cons (NULL_TREE, long_long_integer_type_node,
16177                                                  endlink)));
16178   tree di_ftype_di_int
16179     = build_function_type (long_long_integer_type_node,
16180                            tree_cons (NULL_TREE, long_long_integer_type_node,
16181                                       tree_cons (NULL_TREE, integer_type_node,
16182                                                  endlink)));
16183   tree di_ftype_di_int_int
16184     = build_function_type (long_long_integer_type_node,
16185                            tree_cons (NULL_TREE, long_long_integer_type_node,
16186                                       tree_cons (NULL_TREE, integer_type_node,
16187                                                  tree_cons (NULL_TREE,
16188                                                             integer_type_node,
16189                                                             endlink))));
16190   tree int_ftype_v8qi
16191     = build_function_type (integer_type_node,
16192                            tree_cons (NULL_TREE, V8QI_type_node,
16193                                       endlink));
16194   tree int_ftype_v4hi
16195     = build_function_type (integer_type_node,
16196                            tree_cons (NULL_TREE, V4HI_type_node,
16197                                       endlink));
16198   tree int_ftype_v2si
16199     = build_function_type (integer_type_node,
16200                            tree_cons (NULL_TREE, V2SI_type_node,
16201                                       endlink));
16202   tree int_ftype_v8qi_int
16203     = build_function_type (integer_type_node,
16204                            tree_cons (NULL_TREE, V8QI_type_node,
16205                                       tree_cons (NULL_TREE, integer_type_node,
16206                                                  endlink)));
16207   tree int_ftype_v4hi_int
16208     = build_function_type (integer_type_node,
16209                            tree_cons (NULL_TREE, V4HI_type_node,
16210                                       tree_cons (NULL_TREE, integer_type_node,
16211                                                  endlink)));
16212   tree int_ftype_v2si_int
16213     = build_function_type (integer_type_node,
16214                            tree_cons (NULL_TREE, V2SI_type_node,
16215                                       tree_cons (NULL_TREE, integer_type_node,
16216                                                  endlink)));
16217   tree v8qi_ftype_v8qi_int_int
16218     = build_function_type (V8QI_type_node,
16219                            tree_cons (NULL_TREE, V8QI_type_node,
16220                                       tree_cons (NULL_TREE, integer_type_node,
16221                                                  tree_cons (NULL_TREE,
16222                                                             integer_type_node,
16223                                                             endlink))));
16224   tree v4hi_ftype_v4hi_int_int
16225     = build_function_type (V4HI_type_node,
16226                            tree_cons (NULL_TREE, V4HI_type_node,
16227                                       tree_cons (NULL_TREE, integer_type_node,
16228                                                  tree_cons (NULL_TREE,
16229                                                             integer_type_node,
16230                                                             endlink))));
16231   tree v2si_ftype_v2si_int_int
16232     = build_function_type (V2SI_type_node,
16233                            tree_cons (NULL_TREE, V2SI_type_node,
16234                                       tree_cons (NULL_TREE, integer_type_node,
16235                                                  tree_cons (NULL_TREE,
16236                                                             integer_type_node,
16237                                                             endlink))));
16238   /* Miscellaneous.  */
16239   tree v8qi_ftype_v4hi_v4hi
16240     = build_function_type (V8QI_type_node,
16241                            tree_cons (NULL_TREE, V4HI_type_node,
16242                                       tree_cons (NULL_TREE, V4HI_type_node,
16243                                                  endlink)));
16244   tree v4hi_ftype_v2si_v2si
16245     = build_function_type (V4HI_type_node,
16246                            tree_cons (NULL_TREE, V2SI_type_node,
16247                                       tree_cons (NULL_TREE, V2SI_type_node,
16248                                                  endlink)));
16249   tree v2si_ftype_v4hi_v4hi
16250     = build_function_type (V2SI_type_node,
16251                            tree_cons (NULL_TREE, V4HI_type_node,
16252                                       tree_cons (NULL_TREE, V4HI_type_node,
16253                                                  endlink)));
16254   tree v2si_ftype_v8qi_v8qi
16255     = build_function_type (V2SI_type_node,
16256                            tree_cons (NULL_TREE, V8QI_type_node,
16257                                       tree_cons (NULL_TREE, V8QI_type_node,
16258                                                  endlink)));
16259   tree v4hi_ftype_v4hi_di
16260     = build_function_type (V4HI_type_node,
16261                            tree_cons (NULL_TREE, V4HI_type_node,
16262                                       tree_cons (NULL_TREE,
16263                                                  long_long_integer_type_node,
16264                                                  endlink)));
16265   tree v2si_ftype_v2si_di
16266     = build_function_type (V2SI_type_node,
16267                            tree_cons (NULL_TREE, V2SI_type_node,
16268                                       tree_cons (NULL_TREE,
16269                                                  long_long_integer_type_node,
16270                                                  endlink)));
16271   tree void_ftype_int_int
16272     = build_function_type (void_type_node,
16273                            tree_cons (NULL_TREE, integer_type_node,
16274                                       tree_cons (NULL_TREE, integer_type_node,
16275                                                  endlink)));
16276   tree di_ftype_void
16277     = build_function_type (long_long_unsigned_type_node, endlink);
16278   tree di_ftype_v8qi
16279     = build_function_type (long_long_integer_type_node,
16280                            tree_cons (NULL_TREE, V8QI_type_node,
16281                                       endlink));
16282   tree di_ftype_v4hi
16283     = build_function_type (long_long_integer_type_node,
16284                            tree_cons (NULL_TREE, V4HI_type_node,
16285                                       endlink));
16286   tree di_ftype_v2si
16287     = build_function_type (long_long_integer_type_node,
16288                            tree_cons (NULL_TREE, V2SI_type_node,
16289                                       endlink));
16290   tree v2si_ftype_v4hi
16291     = build_function_type (V2SI_type_node,
16292                            tree_cons (NULL_TREE, V4HI_type_node,
16293                                       endlink));
16294   tree v4hi_ftype_v8qi
16295     = build_function_type (V4HI_type_node,
16296                            tree_cons (NULL_TREE, V8QI_type_node,
16297                                       endlink));
16298
16299   tree di_ftype_di_v4hi_v4hi
16300     = build_function_type (long_long_unsigned_type_node,
16301                            tree_cons (NULL_TREE,
16302                                       long_long_unsigned_type_node,
16303                                       tree_cons (NULL_TREE, V4HI_type_node,
16304                                                  tree_cons (NULL_TREE,
16305                                                             V4HI_type_node,
16306                                                             endlink))));
16307
16308   tree di_ftype_v4hi_v4hi
16309     = build_function_type (long_long_unsigned_type_node,
16310                            tree_cons (NULL_TREE, V4HI_type_node,
16311                                       tree_cons (NULL_TREE, V4HI_type_node,
16312                                                  endlink)));
16313
16314   /* Normal vector binops.  */
16315   tree v8qi_ftype_v8qi_v8qi
16316     = build_function_type (V8QI_type_node,
16317                            tree_cons (NULL_TREE, V8QI_type_node,
16318                                       tree_cons (NULL_TREE, V8QI_type_node,
16319                                                  endlink)));
16320   tree v4hi_ftype_v4hi_v4hi
16321     = build_function_type (V4HI_type_node,
16322                            tree_cons (NULL_TREE, V4HI_type_node,
16323                                       tree_cons (NULL_TREE, V4HI_type_node,
16324                                                  endlink)));
16325   tree v2si_ftype_v2si_v2si
16326     = build_function_type (V2SI_type_node,
16327                            tree_cons (NULL_TREE, V2SI_type_node,
16328                                       tree_cons (NULL_TREE, V2SI_type_node,
16329                                                  endlink)));
16330   tree di_ftype_di_di
16331     = build_function_type (long_long_unsigned_type_node,
16332                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
16333                                       tree_cons (NULL_TREE,
16334                                                  long_long_unsigned_type_node,
16335                                                  endlink)));
16336
16337   /* Add all builtins that are more or less simple operations on two
16338      operands.  */
16339   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
16340     {
16341       /* Use one of the operands; the target can have a different mode for
16342          mask-generating compares.  */
16343       enum machine_mode mode;
16344       tree type;
16345
16346       if (d->name == 0)
16347         continue;
16348
16349       mode = insn_data[d->icode].operand[1].mode;
16350
16351       switch (mode)
16352         {
16353         case V8QImode:
16354           type = v8qi_ftype_v8qi_v8qi;
16355           break;
16356         case V4HImode:
16357           type = v4hi_ftype_v4hi_v4hi;
16358           break;
16359         case V2SImode:
16360           type = v2si_ftype_v2si_v2si;
16361           break;
16362         case DImode:
16363           type = di_ftype_di_di;
16364           break;
16365
16366         default:
16367           gcc_unreachable ();
16368         }
16369
16370       def_mbuiltin (d->mask, d->name, type, d->code);
16371     }
16372
16373   /* Add the remaining MMX insns with somewhat more complicated types.  */
16374   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
16375   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
16376   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
16377
16378   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
16379   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
16380   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
16381   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
16382   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
16383   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
16384
16385   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
16386   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
16387   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
16388   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
16389   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
16390   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
16391
16392   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
16393   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
16394   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
16395   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
16396   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
16397   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
16398
16399   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
16400   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
16401   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
16402   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
16403   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
16404   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
16405
16406   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
16407
16408   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
16409   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
16410   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
16411   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
16412
16413   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
16414   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
16415   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
16416   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
16417   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
16418   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
16419   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
16420   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
16421   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
16422
16423   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
16424   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
16425   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
16426
16427   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
16428   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
16429   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
16430
16431   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
16432   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
16433   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
16434   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
16435   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
16436   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
16437
16438   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
16439   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
16440   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
16441   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
16442   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
16443   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
16444   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
16445   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
16446   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
16447   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
16448   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
16449   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
16450
16451   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
16452   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
16453   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
16454   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
16455
16456   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
16457   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
16458   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
16459   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
16460   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
16461   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
16462   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
16463 }
16464
16465 static void
16466 arm_init_tls_builtins (void)
16467 {
16468   tree ftype, decl;
16469
16470   ftype = build_function_type (ptr_type_node, void_list_node);
16471   decl = add_builtin_function ("__builtin_thread_pointer", ftype,
16472                                ARM_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
16473                                NULL, NULL_TREE);
16474   TREE_NOTHROW (decl) = 1;
16475   TREE_READONLY (decl) = 1;
16476 }
16477
16478 enum neon_builtin_type_bits {
16479   T_V8QI  = 0x0001,
16480   T_V4HI  = 0x0002,
16481   T_V2SI  = 0x0004,
16482   T_V2SF  = 0x0008,
16483   T_DI    = 0x0010,
16484   T_V16QI = 0x0020,
16485   T_V8HI  = 0x0040,
16486   T_V4SI  = 0x0080,
16487   T_V4SF  = 0x0100,
16488   T_V2DI  = 0x0200,
16489   T_TI    = 0x0400,
16490   T_EI    = 0x0800,
16491   T_OI    = 0x1000
16492 };
16493
16494 #define v8qi_UP  T_V8QI
16495 #define v4hi_UP  T_V4HI
16496 #define v2si_UP  T_V2SI
16497 #define v2sf_UP  T_V2SF
16498 #define di_UP    T_DI
16499 #define v16qi_UP T_V16QI
16500 #define v8hi_UP  T_V8HI
16501 #define v4si_UP  T_V4SI
16502 #define v4sf_UP  T_V4SF
16503 #define v2di_UP  T_V2DI
16504 #define ti_UP    T_TI
16505 #define ei_UP    T_EI
16506 #define oi_UP    T_OI
16507
16508 #define UP(X) X##_UP
16509
16510 #define T_MAX 13
16511
16512 typedef enum {
16513   NEON_BINOP,
16514   NEON_TERNOP,
16515   NEON_UNOP,
16516   NEON_GETLANE,
16517   NEON_SETLANE,
16518   NEON_CREATE,
16519   NEON_DUP,
16520   NEON_DUPLANE,
16521   NEON_COMBINE,
16522   NEON_SPLIT,
16523   NEON_LANEMUL,
16524   NEON_LANEMULL,
16525   NEON_LANEMULH,
16526   NEON_LANEMAC,
16527   NEON_SCALARMUL,
16528   NEON_SCALARMULL,
16529   NEON_SCALARMULH,
16530   NEON_SCALARMAC,
16531   NEON_CONVERT,
16532   NEON_FIXCONV,
16533   NEON_SELECT,
16534   NEON_RESULTPAIR,
16535   NEON_REINTERP,
16536   NEON_VTBL,
16537   NEON_VTBX,
16538   NEON_LOAD1,
16539   NEON_LOAD1LANE,
16540   NEON_STORE1,
16541   NEON_STORE1LANE,
16542   NEON_LOADSTRUCT,
16543   NEON_LOADSTRUCTLANE,
16544   NEON_STORESTRUCT,
16545   NEON_STORESTRUCTLANE,
16546   NEON_LOGICBINOP,
16547   NEON_SHIFTINSERT,
16548   NEON_SHIFTIMM,
16549   NEON_SHIFTACC
16550 } neon_itype;
16551
16552 typedef struct {
16553   const char *name;
16554   const neon_itype itype;
16555   const int bits;
16556   const enum insn_code codes[T_MAX];
16557   const unsigned int num_vars;
16558   unsigned int base_fcode;
16559 } neon_builtin_datum;
16560
16561 #define CF(N,X) CODE_FOR_neon_##N##X
16562
16563 #define VAR1(T, N, A) \
16564   #N, NEON_##T, UP (A), { CF (N, A) }, 1, 0
16565 #define VAR2(T, N, A, B) \
16566   #N, NEON_##T, UP (A) | UP (B), { CF (N, A), CF (N, B) }, 2, 0
16567 #define VAR3(T, N, A, B, C) \
16568   #N, NEON_##T, UP (A) | UP (B) | UP (C), \
16569   { CF (N, A), CF (N, B), CF (N, C) }, 3, 0
16570 #define VAR4(T, N, A, B, C, D) \
16571   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D), \
16572   { CF (N, A), CF (N, B), CF (N, C), CF (N, D) }, 4, 0
16573 #define VAR5(T, N, A, B, C, D, E) \
16574   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E), \
16575   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E) }, 5, 0
16576 #define VAR6(T, N, A, B, C, D, E, F) \
16577   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F), \
16578   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F) }, 6, 0
16579 #define VAR7(T, N, A, B, C, D, E, F, G) \
16580   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G), \
16581   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
16582     CF (N, G) }, 7, 0
16583 #define VAR8(T, N, A, B, C, D, E, F, G, H) \
16584   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
16585                 | UP (H), \
16586   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
16587     CF (N, G), CF (N, H) }, 8, 0
16588 #define VAR9(T, N, A, B, C, D, E, F, G, H, I) \
16589   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
16590                 | UP (H) | UP (I), \
16591   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
16592     CF (N, G), CF (N, H), CF (N, I) }, 9, 0
16593 #define VAR10(T, N, A, B, C, D, E, F, G, H, I, J) \
16594   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
16595                 | UP (H) | UP (I) | UP (J), \
16596   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
16597     CF (N, G), CF (N, H), CF (N, I), CF (N, J) }, 10, 0
16598
16599 /* The mode entries in the following table correspond to the "key" type of the
16600    instruction variant, i.e. equivalent to that which would be specified after
16601    the assembler mnemonic, which usually refers to the last vector operand.
16602    (Signed/unsigned/polynomial types are not differentiated between though, and
16603    are all mapped onto the same mode for a given element size.) The modes
16604    listed per instruction should be the same as those defined for that
16605    instruction's pattern in neon.md.
16606    WARNING: Variants should be listed in the same increasing order as
16607    neon_builtin_type_bits.  */
16608
16609 static neon_builtin_datum neon_builtin_data[] =
16610 {
16611   { VAR10 (BINOP, vadd,
16612            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
16613   { VAR3 (BINOP, vaddl, v8qi, v4hi, v2si) },
16614   { VAR3 (BINOP, vaddw, v8qi, v4hi, v2si) },
16615   { VAR6 (BINOP, vhadd, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
16616   { VAR8 (BINOP, vqadd, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
16617   { VAR3 (BINOP, vaddhn, v8hi, v4si, v2di) },
16618   { VAR8 (BINOP, vmul, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
16619   { VAR8 (TERNOP, vmla, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
16620   { VAR3 (TERNOP, vmlal, v8qi, v4hi, v2si) },
16621   { VAR8 (TERNOP, vmls, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
16622   { VAR3 (TERNOP, vmlsl, v8qi, v4hi, v2si) },
16623   { VAR4 (BINOP, vqdmulh, v4hi, v2si, v8hi, v4si) },
16624   { VAR2 (TERNOP, vqdmlal, v4hi, v2si) },
16625   { VAR2 (TERNOP, vqdmlsl, v4hi, v2si) },
16626   { VAR3 (BINOP, vmull, v8qi, v4hi, v2si) },
16627   { VAR2 (SCALARMULL, vmull_n, v4hi, v2si) },
16628   { VAR2 (LANEMULL, vmull_lane, v4hi, v2si) },
16629   { VAR2 (SCALARMULL, vqdmull_n, v4hi, v2si) },
16630   { VAR2 (LANEMULL, vqdmull_lane, v4hi, v2si) },
16631   { VAR4 (SCALARMULH, vqdmulh_n, v4hi, v2si, v8hi, v4si) },
16632   { VAR4 (LANEMULH, vqdmulh_lane, v4hi, v2si, v8hi, v4si) },
16633   { VAR2 (BINOP, vqdmull, v4hi, v2si) },
16634   { VAR8 (BINOP, vshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
16635   { VAR8 (BINOP, vqshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
16636   { VAR8 (SHIFTIMM, vshr_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
16637   { VAR3 (SHIFTIMM, vshrn_n, v8hi, v4si, v2di) },
16638   { VAR3 (SHIFTIMM, vqshrn_n, v8hi, v4si, v2di) },
16639   { VAR3 (SHIFTIMM, vqshrun_n, v8hi, v4si, v2di) },
16640   { VAR8 (SHIFTIMM, vshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
16641   { VAR8 (SHIFTIMM, vqshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
16642   { VAR8 (SHIFTIMM, vqshlu_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
16643   { VAR3 (SHIFTIMM, vshll_n, v8qi, v4hi, v2si) },
16644   { VAR8 (SHIFTACC, vsra_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
16645   { VAR10 (BINOP, vsub,
16646            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
16647   { VAR3 (BINOP, vsubl, v8qi, v4hi, v2si) },
16648   { VAR3 (BINOP, vsubw, v8qi, v4hi, v2si) },
16649   { VAR8 (BINOP, vqsub, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
16650   { VAR6 (BINOP, vhsub, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
16651   { VAR3 (BINOP, vsubhn, v8hi, v4si, v2di) },
16652   { VAR8 (BINOP, vceq, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
16653   { VAR8 (BINOP, vcge, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
16654   { VAR8 (BINOP, vcgt, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
16655   { VAR2 (BINOP, vcage, v2sf, v4sf) },
16656   { VAR2 (BINOP, vcagt, v2sf, v4sf) },
16657   { VAR6 (BINOP, vtst, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
16658   { VAR8 (BINOP, vabd, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
16659   { VAR3 (BINOP, vabdl, v8qi, v4hi, v2si) },
16660   { VAR6 (TERNOP, vaba, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
16661   { VAR3 (TERNOP, vabal, v8qi, v4hi, v2si) },
16662   { VAR8 (BINOP, vmax, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
16663   { VAR8 (BINOP, vmin, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
16664   { VAR4 (BINOP, vpadd, v8qi, v4hi, v2si, v2sf) },
16665   { VAR6 (UNOP, vpaddl, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
16666   { VAR6 (BINOP, vpadal, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
16667   { VAR4 (BINOP, vpmax, v8qi, v4hi, v2si, v2sf) },
16668   { VAR4 (BINOP, vpmin, v8qi, v4hi, v2si, v2sf) },
16669   { VAR2 (BINOP, vrecps, v2sf, v4sf) },
16670   { VAR2 (BINOP, vrsqrts, v2sf, v4sf) },
16671   { VAR8 (SHIFTINSERT, vsri_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
16672   { VAR8 (SHIFTINSERT, vsli_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
16673   { VAR8 (UNOP, vabs, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
16674   { VAR6 (UNOP, vqabs, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
16675   { VAR8 (UNOP, vneg, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
16676   { VAR6 (UNOP, vqneg, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
16677   { VAR6 (UNOP, vcls, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
16678   { VAR6 (UNOP, vclz, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
16679   { VAR2 (UNOP, vcnt, v8qi, v16qi) },
16680   { VAR4 (UNOP, vrecpe, v2si, v2sf, v4si, v4sf) },
16681   { VAR4 (UNOP, vrsqrte, v2si, v2sf, v4si, v4sf) },
16682   { VAR6 (UNOP, vmvn, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
16683   /* FIXME: vget_lane supports more variants than this!  */
16684   { VAR10 (GETLANE, vget_lane,
16685            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
16686   { VAR10 (SETLANE, vset_lane,
16687            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
16688   { VAR5 (CREATE, vcreate, v8qi, v4hi, v2si, v2sf, di) },
16689   { VAR10 (DUP, vdup_n,
16690            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
16691   { VAR10 (DUPLANE, vdup_lane,
16692            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
16693   { VAR5 (COMBINE, vcombine, v8qi, v4hi, v2si, v2sf, di) },
16694   { VAR5 (SPLIT, vget_high, v16qi, v8hi, v4si, v4sf, v2di) },
16695   { VAR5 (SPLIT, vget_low, v16qi, v8hi, v4si, v4sf, v2di) },
16696   { VAR3 (UNOP, vmovn, v8hi, v4si, v2di) },
16697   { VAR3 (UNOP, vqmovn, v8hi, v4si, v2di) },
16698   { VAR3 (UNOP, vqmovun, v8hi, v4si, v2di) },
16699   { VAR3 (UNOP, vmovl, v8qi, v4hi, v2si) },
16700   { VAR6 (LANEMUL, vmul_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
16701   { VAR6 (LANEMAC, vmla_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
16702   { VAR2 (LANEMAC, vmlal_lane, v4hi, v2si) },
16703   { VAR2 (LANEMAC, vqdmlal_lane, v4hi, v2si) },
16704   { VAR6 (LANEMAC, vmls_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
16705   { VAR2 (LANEMAC, vmlsl_lane, v4hi, v2si) },
16706   { VAR2 (LANEMAC, vqdmlsl_lane, v4hi, v2si) },
16707   { VAR6 (SCALARMUL, vmul_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
16708   { VAR6 (SCALARMAC, vmla_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
16709   { VAR2 (SCALARMAC, vmlal_n, v4hi, v2si) },
16710   { VAR2 (SCALARMAC, vqdmlal_n, v4hi, v2si) },
16711   { VAR6 (SCALARMAC, vmls_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
16712   { VAR2 (SCALARMAC, vmlsl_n, v4hi, v2si) },
16713   { VAR2 (SCALARMAC, vqdmlsl_n, v4hi, v2si) },
16714   { VAR10 (BINOP, vext,
16715            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
16716   { VAR8 (UNOP, vrev64, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
16717   { VAR4 (UNOP, vrev32, v8qi, v4hi, v16qi, v8hi) },
16718   { VAR2 (UNOP, vrev16, v8qi, v16qi) },
16719   { VAR4 (CONVERT, vcvt, v2si, v2sf, v4si, v4sf) },
16720   { VAR4 (FIXCONV, vcvt_n, v2si, v2sf, v4si, v4sf) },
16721   { VAR10 (SELECT, vbsl,
16722            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
16723   { VAR1 (VTBL, vtbl1, v8qi) },
16724   { VAR1 (VTBL, vtbl2, v8qi) },
16725   { VAR1 (VTBL, vtbl3, v8qi) },
16726   { VAR1 (VTBL, vtbl4, v8qi) },
16727   { VAR1 (VTBX, vtbx1, v8qi) },
16728   { VAR1 (VTBX, vtbx2, v8qi) },
16729   { VAR1 (VTBX, vtbx3, v8qi) },
16730   { VAR1 (VTBX, vtbx4, v8qi) },
16731   { VAR8 (RESULTPAIR, vtrn, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
16732   { VAR8 (RESULTPAIR, vzip, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
16733   { VAR8 (RESULTPAIR, vuzp, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
16734   { VAR5 (REINTERP, vreinterpretv8qi, v8qi, v4hi, v2si, v2sf, di) },
16735   { VAR5 (REINTERP, vreinterpretv4hi, v8qi, v4hi, v2si, v2sf, di) },
16736   { VAR5 (REINTERP, vreinterpretv2si, v8qi, v4hi, v2si, v2sf, di) },
16737   { VAR5 (REINTERP, vreinterpretv2sf, v8qi, v4hi, v2si, v2sf, di) },
16738   { VAR5 (REINTERP, vreinterpretdi, v8qi, v4hi, v2si, v2sf, di) },
16739   { VAR5 (REINTERP, vreinterpretv16qi, v16qi, v8hi, v4si, v4sf, v2di) },
16740   { VAR5 (REINTERP, vreinterpretv8hi, v16qi, v8hi, v4si, v4sf, v2di) },
16741   { VAR5 (REINTERP, vreinterpretv4si, v16qi, v8hi, v4si, v4sf, v2di) },
16742   { VAR5 (REINTERP, vreinterpretv4sf, v16qi, v8hi, v4si, v4sf, v2di) },
16743   { VAR5 (REINTERP, vreinterpretv2di, v16qi, v8hi, v4si, v4sf, v2di) },
16744   { VAR10 (LOAD1, vld1,
16745            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
16746   { VAR10 (LOAD1LANE, vld1_lane,
16747            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
16748   { VAR10 (LOAD1, vld1_dup,
16749            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
16750   { VAR10 (STORE1, vst1,
16751            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
16752   { VAR10 (STORE1LANE, vst1_lane,
16753            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
16754   { VAR9 (LOADSTRUCT,
16755           vld2, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
16756   { VAR7 (LOADSTRUCTLANE, vld2_lane,
16757           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
16758   { VAR5 (LOADSTRUCT, vld2_dup, v8qi, v4hi, v2si, v2sf, di) },
16759   { VAR9 (STORESTRUCT, vst2,
16760           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
16761   { VAR7 (STORESTRUCTLANE, vst2_lane,
16762           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
16763   { VAR9 (LOADSTRUCT,
16764           vld3, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
16765   { VAR7 (LOADSTRUCTLANE, vld3_lane,
16766           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
16767   { VAR5 (LOADSTRUCT, vld3_dup, v8qi, v4hi, v2si, v2sf, di) },
16768   { VAR9 (STORESTRUCT, vst3,
16769           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
16770   { VAR7 (STORESTRUCTLANE, vst3_lane,
16771           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
16772   { VAR9 (LOADSTRUCT, vld4,
16773           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
16774   { VAR7 (LOADSTRUCTLANE, vld4_lane,
16775           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
16776   { VAR5 (LOADSTRUCT, vld4_dup, v8qi, v4hi, v2si, v2sf, di) },
16777   { VAR9 (STORESTRUCT, vst4,
16778           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
16779   { VAR7 (STORESTRUCTLANE, vst4_lane,
16780           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
16781   { VAR10 (LOGICBINOP, vand,
16782            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
16783   { VAR10 (LOGICBINOP, vorr,
16784            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
16785   { VAR10 (BINOP, veor,
16786            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
16787   { VAR10 (LOGICBINOP, vbic,
16788            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
16789   { VAR10 (LOGICBINOP, vorn,
16790            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) }
16791 };
16792
16793 #undef CF
16794 #undef VAR1
16795 #undef VAR2
16796 #undef VAR3
16797 #undef VAR4
16798 #undef VAR5
16799 #undef VAR6
16800 #undef VAR7
16801 #undef VAR8
16802 #undef VAR9
16803 #undef VAR10
16804
16805 static void
16806 arm_init_neon_builtins (void)
16807 {
16808   unsigned int i, fcode = ARM_BUILTIN_NEON_BASE;
16809
16810   tree neon_intQI_type_node;
16811   tree neon_intHI_type_node;
16812   tree neon_polyQI_type_node;
16813   tree neon_polyHI_type_node;
16814   tree neon_intSI_type_node;
16815   tree neon_intDI_type_node;
16816   tree neon_float_type_node;
16817
16818   tree intQI_pointer_node;
16819   tree intHI_pointer_node;
16820   tree intSI_pointer_node;
16821   tree intDI_pointer_node;
16822   tree float_pointer_node;
16823
16824   tree const_intQI_node;
16825   tree const_intHI_node;
16826   tree const_intSI_node;
16827   tree const_intDI_node;
16828   tree const_float_node;
16829
16830   tree const_intQI_pointer_node;
16831   tree const_intHI_pointer_node;
16832   tree const_intSI_pointer_node;
16833   tree const_intDI_pointer_node;
16834   tree const_float_pointer_node;
16835
16836   tree V8QI_type_node;
16837   tree V4HI_type_node;
16838   tree V2SI_type_node;
16839   tree V2SF_type_node;
16840   tree V16QI_type_node;
16841   tree V8HI_type_node;
16842   tree V4SI_type_node;
16843   tree V4SF_type_node;
16844   tree V2DI_type_node;
16845
16846   tree intUQI_type_node;
16847   tree intUHI_type_node;
16848   tree intUSI_type_node;
16849   tree intUDI_type_node;
16850
16851   tree intEI_type_node;
16852   tree intOI_type_node;
16853   tree intCI_type_node;
16854   tree intXI_type_node;
16855
16856   tree V8QI_pointer_node;
16857   tree V4HI_pointer_node;
16858   tree V2SI_pointer_node;
16859   tree V2SF_pointer_node;
16860   tree V16QI_pointer_node;
16861   tree V8HI_pointer_node;
16862   tree V4SI_pointer_node;
16863   tree V4SF_pointer_node;
16864   tree V2DI_pointer_node;
16865
16866   tree void_ftype_pv8qi_v8qi_v8qi;
16867   tree void_ftype_pv4hi_v4hi_v4hi;
16868   tree void_ftype_pv2si_v2si_v2si;
16869   tree void_ftype_pv2sf_v2sf_v2sf;
16870   tree void_ftype_pdi_di_di;
16871   tree void_ftype_pv16qi_v16qi_v16qi;
16872   tree void_ftype_pv8hi_v8hi_v8hi;
16873   tree void_ftype_pv4si_v4si_v4si;
16874   tree void_ftype_pv4sf_v4sf_v4sf;
16875   tree void_ftype_pv2di_v2di_v2di;
16876
16877   tree reinterp_ftype_dreg[5][5];
16878   tree reinterp_ftype_qreg[5][5];
16879   tree dreg_types[5], qreg_types[5];
16880
16881   /* Create distinguished type nodes for NEON vector element types,
16882      and pointers to values of such types, so we can detect them later.  */
16883   neon_intQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
16884   neon_intHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
16885   neon_polyQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
16886   neon_polyHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
16887   neon_intSI_type_node = make_signed_type (GET_MODE_PRECISION (SImode));
16888   neon_intDI_type_node = make_signed_type (GET_MODE_PRECISION (DImode));
16889   neon_float_type_node = make_node (REAL_TYPE);
16890   TYPE_PRECISION (neon_float_type_node) = FLOAT_TYPE_SIZE;
16891   layout_type (neon_float_type_node);
16892
16893   /* Define typedefs which exactly correspond to the modes we are basing vector
16894      types on.  If you change these names you'll need to change
16895      the table used by arm_mangle_type too.  */
16896   (*lang_hooks.types.register_builtin_type) (neon_intQI_type_node,
16897                                              "__builtin_neon_qi");
16898   (*lang_hooks.types.register_builtin_type) (neon_intHI_type_node,
16899                                              "__builtin_neon_hi");
16900   (*lang_hooks.types.register_builtin_type) (neon_intSI_type_node,
16901                                              "__builtin_neon_si");
16902   (*lang_hooks.types.register_builtin_type) (neon_float_type_node,
16903                                              "__builtin_neon_sf");
16904   (*lang_hooks.types.register_builtin_type) (neon_intDI_type_node,
16905                                              "__builtin_neon_di");
16906   (*lang_hooks.types.register_builtin_type) (neon_polyQI_type_node,
16907                                              "__builtin_neon_poly8");
16908   (*lang_hooks.types.register_builtin_type) (neon_polyHI_type_node,
16909                                              "__builtin_neon_poly16");
16910
16911   intQI_pointer_node = build_pointer_type (neon_intQI_type_node);
16912   intHI_pointer_node = build_pointer_type (neon_intHI_type_node);
16913   intSI_pointer_node = build_pointer_type (neon_intSI_type_node);
16914   intDI_pointer_node = build_pointer_type (neon_intDI_type_node);
16915   float_pointer_node = build_pointer_type (neon_float_type_node);
16916
16917   /* Next create constant-qualified versions of the above types.  */
16918   const_intQI_node = build_qualified_type (neon_intQI_type_node,
16919                                            TYPE_QUAL_CONST);
16920   const_intHI_node = build_qualified_type (neon_intHI_type_node,
16921                                            TYPE_QUAL_CONST);
16922   const_intSI_node = build_qualified_type (neon_intSI_type_node,
16923                                            TYPE_QUAL_CONST);
16924   const_intDI_node = build_qualified_type (neon_intDI_type_node,
16925                                            TYPE_QUAL_CONST);
16926   const_float_node = build_qualified_type (neon_float_type_node,
16927                                            TYPE_QUAL_CONST);
16928
16929   const_intQI_pointer_node = build_pointer_type (const_intQI_node);
16930   const_intHI_pointer_node = build_pointer_type (const_intHI_node);
16931   const_intSI_pointer_node = build_pointer_type (const_intSI_node);
16932   const_intDI_pointer_node = build_pointer_type (const_intDI_node);
16933   const_float_pointer_node = build_pointer_type (const_float_node);
16934
16935   /* Now create vector types based on our NEON element types.  */
16936   /* 64-bit vectors.  */
16937   V8QI_type_node =
16938     build_vector_type_for_mode (neon_intQI_type_node, V8QImode);
16939   V4HI_type_node =
16940     build_vector_type_for_mode (neon_intHI_type_node, V4HImode);
16941   V2SI_type_node =
16942     build_vector_type_for_mode (neon_intSI_type_node, V2SImode);
16943   V2SF_type_node =
16944     build_vector_type_for_mode (neon_float_type_node, V2SFmode);
16945   /* 128-bit vectors.  */
16946   V16QI_type_node =
16947     build_vector_type_for_mode (neon_intQI_type_node, V16QImode);
16948   V8HI_type_node =
16949     build_vector_type_for_mode (neon_intHI_type_node, V8HImode);
16950   V4SI_type_node =
16951     build_vector_type_for_mode (neon_intSI_type_node, V4SImode);
16952   V4SF_type_node =
16953     build_vector_type_for_mode (neon_float_type_node, V4SFmode);
16954   V2DI_type_node =
16955     build_vector_type_for_mode (neon_intDI_type_node, V2DImode);
16956
16957   /* Unsigned integer types for various mode sizes.  */
16958   intUQI_type_node = make_unsigned_type (GET_MODE_PRECISION (QImode));
16959   intUHI_type_node = make_unsigned_type (GET_MODE_PRECISION (HImode));
16960   intUSI_type_node = make_unsigned_type (GET_MODE_PRECISION (SImode));
16961   intUDI_type_node = make_unsigned_type (GET_MODE_PRECISION (DImode));
16962
16963   (*lang_hooks.types.register_builtin_type) (intUQI_type_node,
16964                                              "__builtin_neon_uqi");
16965   (*lang_hooks.types.register_builtin_type) (intUHI_type_node,
16966                                              "__builtin_neon_uhi");
16967   (*lang_hooks.types.register_builtin_type) (intUSI_type_node,
16968                                              "__builtin_neon_usi");
16969   (*lang_hooks.types.register_builtin_type) (intUDI_type_node,
16970                                              "__builtin_neon_udi");
16971
16972   /* Opaque integer types for structures of vectors.  */
16973   intEI_type_node = make_signed_type (GET_MODE_PRECISION (EImode));
16974   intOI_type_node = make_signed_type (GET_MODE_PRECISION (OImode));
16975   intCI_type_node = make_signed_type (GET_MODE_PRECISION (CImode));
16976   intXI_type_node = make_signed_type (GET_MODE_PRECISION (XImode));
16977
16978   (*lang_hooks.types.register_builtin_type) (intTI_type_node,
16979                                              "__builtin_neon_ti");
16980   (*lang_hooks.types.register_builtin_type) (intEI_type_node,
16981                                              "__builtin_neon_ei");
16982   (*lang_hooks.types.register_builtin_type) (intOI_type_node,
16983                                              "__builtin_neon_oi");
16984   (*lang_hooks.types.register_builtin_type) (intCI_type_node,
16985                                              "__builtin_neon_ci");
16986   (*lang_hooks.types.register_builtin_type) (intXI_type_node,
16987                                              "__builtin_neon_xi");
16988
16989   /* Pointers to vector types.  */
16990   V8QI_pointer_node = build_pointer_type (V8QI_type_node);
16991   V4HI_pointer_node = build_pointer_type (V4HI_type_node);
16992   V2SI_pointer_node = build_pointer_type (V2SI_type_node);
16993   V2SF_pointer_node = build_pointer_type (V2SF_type_node);
16994   V16QI_pointer_node = build_pointer_type (V16QI_type_node);
16995   V8HI_pointer_node = build_pointer_type (V8HI_type_node);
16996   V4SI_pointer_node = build_pointer_type (V4SI_type_node);
16997   V4SF_pointer_node = build_pointer_type (V4SF_type_node);
16998   V2DI_pointer_node = build_pointer_type (V2DI_type_node);
16999
17000   /* Operations which return results as pairs.  */
17001   void_ftype_pv8qi_v8qi_v8qi =
17002     build_function_type_list (void_type_node, V8QI_pointer_node, V8QI_type_node,
17003                               V8QI_type_node, NULL);
17004   void_ftype_pv4hi_v4hi_v4hi =
17005     build_function_type_list (void_type_node, V4HI_pointer_node, V4HI_type_node,
17006                               V4HI_type_node, NULL);
17007   void_ftype_pv2si_v2si_v2si =
17008     build_function_type_list (void_type_node, V2SI_pointer_node, V2SI_type_node,
17009                               V2SI_type_node, NULL);
17010   void_ftype_pv2sf_v2sf_v2sf =
17011     build_function_type_list (void_type_node, V2SF_pointer_node, V2SF_type_node,
17012                               V2SF_type_node, NULL);
17013   void_ftype_pdi_di_di =
17014     build_function_type_list (void_type_node, intDI_pointer_node,
17015                               neon_intDI_type_node, neon_intDI_type_node, NULL);
17016   void_ftype_pv16qi_v16qi_v16qi =
17017     build_function_type_list (void_type_node, V16QI_pointer_node,
17018                               V16QI_type_node, V16QI_type_node, NULL);
17019   void_ftype_pv8hi_v8hi_v8hi =
17020     build_function_type_list (void_type_node, V8HI_pointer_node, V8HI_type_node,
17021                               V8HI_type_node, NULL);
17022   void_ftype_pv4si_v4si_v4si =
17023     build_function_type_list (void_type_node, V4SI_pointer_node, V4SI_type_node,
17024                               V4SI_type_node, NULL);
17025   void_ftype_pv4sf_v4sf_v4sf =
17026     build_function_type_list (void_type_node, V4SF_pointer_node, V4SF_type_node,
17027                               V4SF_type_node, NULL);
17028   void_ftype_pv2di_v2di_v2di =
17029     build_function_type_list (void_type_node, V2DI_pointer_node, V2DI_type_node,
17030                               V2DI_type_node, NULL);
17031
17032   dreg_types[0] = V8QI_type_node;
17033   dreg_types[1] = V4HI_type_node;
17034   dreg_types[2] = V2SI_type_node;
17035   dreg_types[3] = V2SF_type_node;
17036   dreg_types[4] = neon_intDI_type_node;
17037
17038   qreg_types[0] = V16QI_type_node;
17039   qreg_types[1] = V8HI_type_node;
17040   qreg_types[2] = V4SI_type_node;
17041   qreg_types[3] = V4SF_type_node;
17042   qreg_types[4] = V2DI_type_node;
17043
17044   for (i = 0; i < 5; i++)
17045     {
17046       int j;
17047       for (j = 0; j < 5; j++)
17048         {
17049           reinterp_ftype_dreg[i][j]
17050             = build_function_type_list (dreg_types[i], dreg_types[j], NULL);
17051           reinterp_ftype_qreg[i][j]
17052             = build_function_type_list (qreg_types[i], qreg_types[j], NULL);
17053         }
17054     }
17055
17056   for (i = 0; i < ARRAY_SIZE (neon_builtin_data); i++)
17057     {
17058       neon_builtin_datum *d = &neon_builtin_data[i];
17059       unsigned int j, codeidx = 0;
17060
17061       d->base_fcode = fcode;
17062
17063       for (j = 0; j < T_MAX; j++)
17064         {
17065           const char* const modenames[] = {
17066             "v8qi", "v4hi", "v2si", "v2sf", "di",
17067             "v16qi", "v8hi", "v4si", "v4sf", "v2di"
17068           };
17069           char namebuf[60];
17070           tree ftype = NULL;
17071           enum insn_code icode;
17072           int is_load = 0, is_store = 0;
17073
17074           if ((d->bits & (1 << j)) == 0)
17075             continue;
17076
17077           icode = d->codes[codeidx++];
17078
17079           switch (d->itype)
17080             {
17081             case NEON_LOAD1:
17082             case NEON_LOAD1LANE:
17083             case NEON_LOADSTRUCT:
17084             case NEON_LOADSTRUCTLANE:
17085               is_load = 1;
17086               /* Fall through.  */
17087             case NEON_STORE1:
17088             case NEON_STORE1LANE:
17089             case NEON_STORESTRUCT:
17090             case NEON_STORESTRUCTLANE:
17091               if (!is_load)
17092                 is_store = 1;
17093               /* Fall through.  */
17094             case NEON_UNOP:
17095             case NEON_BINOP:
17096             case NEON_LOGICBINOP:
17097             case NEON_SHIFTINSERT:
17098             case NEON_TERNOP:
17099             case NEON_GETLANE:
17100             case NEON_SETLANE:
17101             case NEON_CREATE:
17102             case NEON_DUP:
17103             case NEON_DUPLANE:
17104             case NEON_SHIFTIMM:
17105             case NEON_SHIFTACC:
17106             case NEON_COMBINE:
17107             case NEON_SPLIT:
17108             case NEON_CONVERT:
17109             case NEON_FIXCONV:
17110             case NEON_LANEMUL:
17111             case NEON_LANEMULL:
17112             case NEON_LANEMULH:
17113             case NEON_LANEMAC:
17114             case NEON_SCALARMUL:
17115             case NEON_SCALARMULL:
17116             case NEON_SCALARMULH:
17117             case NEON_SCALARMAC:
17118             case NEON_SELECT:
17119             case NEON_VTBL:
17120             case NEON_VTBX:
17121               {
17122                 int k;
17123                 tree return_type = void_type_node, args = void_list_node;
17124
17125                 /* Build a function type directly from the insn_data for this
17126                    builtin.  The build_function_type() function takes care of
17127                    removing duplicates for us.  */
17128                 for (k = insn_data[icode].n_operands - 1; k >= 0; k--)
17129                   {
17130                     tree eltype;
17131
17132                     if (is_load && k == 1)
17133                       {
17134                         /* Neon load patterns always have the memory operand
17135                            (a SImode pointer) in the operand 1 position.  We
17136                            want a const pointer to the element type in that
17137                            position.  */
17138                         gcc_assert (insn_data[icode].operand[k].mode == SImode);
17139
17140                         switch (1 << j)
17141                           {
17142                           case T_V8QI:
17143                           case T_V16QI:
17144                             eltype = const_intQI_pointer_node;
17145                             break;
17146
17147                           case T_V4HI:
17148                           case T_V8HI:
17149                             eltype = const_intHI_pointer_node;
17150                             break;
17151
17152                           case T_V2SI:
17153                           case T_V4SI:
17154                             eltype = const_intSI_pointer_node;
17155                             break;
17156
17157                           case T_V2SF:
17158                           case T_V4SF:
17159                             eltype = const_float_pointer_node;
17160                             break;
17161
17162                           case T_DI:
17163                           case T_V2DI:
17164                             eltype = const_intDI_pointer_node;
17165                             break;
17166
17167                           default: gcc_unreachable ();
17168                           }
17169                       }
17170                     else if (is_store && k == 0)
17171                       {
17172                         /* Similarly, Neon store patterns use operand 0 as
17173                            the memory location to store to (a SImode pointer).
17174                            Use a pointer to the element type of the store in
17175                            that position.  */
17176                         gcc_assert (insn_data[icode].operand[k].mode == SImode);
17177
17178                         switch (1 << j)
17179                           {
17180                           case T_V8QI:
17181                           case T_V16QI:
17182                             eltype = intQI_pointer_node;
17183                             break;
17184
17185                           case T_V4HI:
17186                           case T_V8HI:
17187                             eltype = intHI_pointer_node;
17188                             break;
17189
17190                           case T_V2SI:
17191                           case T_V4SI:
17192                             eltype = intSI_pointer_node;
17193                             break;
17194
17195                           case T_V2SF:
17196                           case T_V4SF:
17197                             eltype = float_pointer_node;
17198                             break;
17199
17200                           case T_DI:
17201                           case T_V2DI:
17202                             eltype = intDI_pointer_node;
17203                             break;
17204
17205                           default: gcc_unreachable ();
17206                           }
17207                       }
17208                     else
17209                       {
17210                         switch (insn_data[icode].operand[k].mode)
17211                           {
17212                           case VOIDmode: eltype = void_type_node; break;
17213                           /* Scalars.  */
17214                           case QImode: eltype = neon_intQI_type_node; break;
17215                           case HImode: eltype = neon_intHI_type_node; break;
17216                           case SImode: eltype = neon_intSI_type_node; break;
17217                           case SFmode: eltype = neon_float_type_node; break;
17218                           case DImode: eltype = neon_intDI_type_node; break;
17219                           case TImode: eltype = intTI_type_node; break;
17220                           case EImode: eltype = intEI_type_node; break;
17221                           case OImode: eltype = intOI_type_node; break;
17222                           case CImode: eltype = intCI_type_node; break;
17223                           case XImode: eltype = intXI_type_node; break;
17224                           /* 64-bit vectors.  */
17225                           case V8QImode: eltype = V8QI_type_node; break;
17226                           case V4HImode: eltype = V4HI_type_node; break;
17227                           case V2SImode: eltype = V2SI_type_node; break;
17228                           case V2SFmode: eltype = V2SF_type_node; break;
17229                           /* 128-bit vectors.  */
17230                           case V16QImode: eltype = V16QI_type_node; break;
17231                           case V8HImode: eltype = V8HI_type_node; break;
17232                           case V4SImode: eltype = V4SI_type_node; break;
17233                           case V4SFmode: eltype = V4SF_type_node; break;
17234                           case V2DImode: eltype = V2DI_type_node; break;
17235                           default: gcc_unreachable ();
17236                           }
17237                       }
17238
17239                     if (k == 0 && !is_store)
17240                       return_type = eltype;
17241                     else
17242                       args = tree_cons (NULL_TREE, eltype, args);
17243                   }
17244
17245                 ftype = build_function_type (return_type, args);
17246               }
17247               break;
17248
17249             case NEON_RESULTPAIR:
17250               {
17251                 switch (insn_data[icode].operand[1].mode)
17252                   {
17253                   case V8QImode: ftype = void_ftype_pv8qi_v8qi_v8qi; break;
17254                   case V4HImode: ftype = void_ftype_pv4hi_v4hi_v4hi; break;
17255                   case V2SImode: ftype = void_ftype_pv2si_v2si_v2si; break;
17256                   case V2SFmode: ftype = void_ftype_pv2sf_v2sf_v2sf; break;
17257                   case DImode: ftype = void_ftype_pdi_di_di; break;
17258                   case V16QImode: ftype = void_ftype_pv16qi_v16qi_v16qi; break;
17259                   case V8HImode: ftype = void_ftype_pv8hi_v8hi_v8hi; break;
17260                   case V4SImode: ftype = void_ftype_pv4si_v4si_v4si; break;
17261                   case V4SFmode: ftype = void_ftype_pv4sf_v4sf_v4sf; break;
17262                   case V2DImode: ftype = void_ftype_pv2di_v2di_v2di; break;
17263                   default: gcc_unreachable ();
17264                   }
17265               }
17266               break;
17267
17268             case NEON_REINTERP:
17269               {
17270                 /* We iterate over 5 doubleword types, then 5 quadword
17271                    types.  */
17272                 int rhs = j % 5;
17273                 switch (insn_data[icode].operand[0].mode)
17274                   {
17275                   case V8QImode: ftype = reinterp_ftype_dreg[0][rhs]; break;
17276                   case V4HImode: ftype = reinterp_ftype_dreg[1][rhs]; break;
17277                   case V2SImode: ftype = reinterp_ftype_dreg[2][rhs]; break;
17278                   case V2SFmode: ftype = reinterp_ftype_dreg[3][rhs]; break;
17279                   case DImode: ftype = reinterp_ftype_dreg[4][rhs]; break;
17280                   case V16QImode: ftype = reinterp_ftype_qreg[0][rhs]; break;
17281                   case V8HImode: ftype = reinterp_ftype_qreg[1][rhs]; break;
17282                   case V4SImode: ftype = reinterp_ftype_qreg[2][rhs]; break;
17283                   case V4SFmode: ftype = reinterp_ftype_qreg[3][rhs]; break;
17284                   case V2DImode: ftype = reinterp_ftype_qreg[4][rhs]; break;
17285                   default: gcc_unreachable ();
17286                   }
17287               }
17288               break;
17289
17290             default:
17291               gcc_unreachable ();
17292             }
17293
17294           gcc_assert (ftype != NULL);
17295
17296           sprintf (namebuf, "__builtin_neon_%s%s", d->name, modenames[j]);
17297
17298           add_builtin_function (namebuf, ftype, fcode++, BUILT_IN_MD, NULL,
17299                                 NULL_TREE);
17300         }
17301     }
17302 }
17303
17304 static void
17305 arm_init_fp16_builtins (void)
17306 {
17307   tree fp16_type = make_node (REAL_TYPE);
17308   TYPE_PRECISION (fp16_type) = 16;
17309   layout_type (fp16_type);
17310   (*lang_hooks.types.register_builtin_type) (fp16_type, "__fp16");
17311 }
17312
17313 static void
17314 arm_init_builtins (void)
17315 {
17316   arm_init_tls_builtins ();
17317
17318   if (TARGET_REALLY_IWMMXT)
17319     arm_init_iwmmxt_builtins ();
17320
17321   if (TARGET_NEON)
17322     arm_init_neon_builtins ();
17323
17324   if (arm_fp16_format)
17325     arm_init_fp16_builtins ();
17326 }
17327
17328 /* Implement TARGET_INVALID_PARAMETER_TYPE.  */
17329
17330 static const char *
17331 arm_invalid_parameter_type (const_tree t)
17332 {
17333   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
17334     return N_("function parameters cannot have __fp16 type");
17335   return NULL;
17336 }
17337
17338 /* Implement TARGET_INVALID_PARAMETER_TYPE.  */
17339
17340 static const char *
17341 arm_invalid_return_type (const_tree t)
17342 {
17343   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
17344     return N_("functions cannot return __fp16 type");
17345   return NULL;
17346 }
17347
17348 /* Implement TARGET_PROMOTED_TYPE.  */
17349
17350 static tree
17351 arm_promoted_type (const_tree t)
17352 {
17353   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
17354     return float_type_node;
17355   return NULL_TREE;
17356 }
17357
17358 /* Implement TARGET_CONVERT_TO_TYPE.
17359    Specifically, this hook implements the peculiarity of the ARM
17360    half-precision floating-point C semantics that requires conversions between
17361    __fp16 to or from double to do an intermediate conversion to float.  */
17362
17363 static tree
17364 arm_convert_to_type (tree type, tree expr)
17365 {
17366   tree fromtype = TREE_TYPE (expr);
17367   if (!SCALAR_FLOAT_TYPE_P (fromtype) || !SCALAR_FLOAT_TYPE_P (type))
17368     return NULL_TREE;
17369   if ((TYPE_PRECISION (fromtype) == 16 && TYPE_PRECISION (type) > 32)
17370       || (TYPE_PRECISION (type) == 16 && TYPE_PRECISION (fromtype) > 32))
17371     return convert (type, convert (float_type_node, expr));
17372   return NULL_TREE;
17373 }
17374
17375 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P.
17376    This simply adds HFmode as a supported mode; even though we don't
17377    implement arithmetic on this type directly, it's supported by
17378    optabs conversions, much the way the double-word arithmetic is
17379    special-cased in the default hook.  */
17380
17381 static bool
17382 arm_scalar_mode_supported_p (enum machine_mode mode)
17383 {
17384   if (mode == HFmode)
17385     return (arm_fp16_format != ARM_FP16_FORMAT_NONE);
17386   else
17387     return default_scalar_mode_supported_p (mode);
17388 }
17389
17390 /* Errors in the source file can cause expand_expr to return const0_rtx
17391    where we expect a vector.  To avoid crashing, use one of the vector
17392    clear instructions.  */
17393
17394 static rtx
17395 safe_vector_operand (rtx x, enum machine_mode mode)
17396 {
17397   if (x != const0_rtx)
17398     return x;
17399   x = gen_reg_rtx (mode);
17400
17401   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
17402                                : gen_rtx_SUBREG (DImode, x, 0)));
17403   return x;
17404 }
17405
17406 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
17407
17408 static rtx
17409 arm_expand_binop_builtin (enum insn_code icode,
17410                           tree exp, rtx target)
17411 {
17412   rtx pat;
17413   tree arg0 = CALL_EXPR_ARG (exp, 0);
17414   tree arg1 = CALL_EXPR_ARG (exp, 1);
17415   rtx op0 = expand_normal (arg0);
17416   rtx op1 = expand_normal (arg1);
17417   enum machine_mode tmode = insn_data[icode].operand[0].mode;
17418   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
17419   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
17420
17421   if (VECTOR_MODE_P (mode0))
17422     op0 = safe_vector_operand (op0, mode0);
17423   if (VECTOR_MODE_P (mode1))
17424     op1 = safe_vector_operand (op1, mode1);
17425
17426   if (! target
17427       || GET_MODE (target) != tmode
17428       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17429     target = gen_reg_rtx (tmode);
17430
17431   gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
17432
17433   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
17434     op0 = copy_to_mode_reg (mode0, op0);
17435   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
17436     op1 = copy_to_mode_reg (mode1, op1);
17437
17438   pat = GEN_FCN (icode) (target, op0, op1);
17439   if (! pat)
17440     return 0;
17441   emit_insn (pat);
17442   return target;
17443 }
17444
17445 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
17446
17447 static rtx
17448 arm_expand_unop_builtin (enum insn_code icode,
17449                          tree exp, rtx target, int do_load)
17450 {
17451   rtx pat;
17452   tree arg0 = CALL_EXPR_ARG (exp, 0);
17453   rtx op0 = expand_normal (arg0);
17454   enum machine_mode tmode = insn_data[icode].operand[0].mode;
17455   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
17456
17457   if (! target
17458       || GET_MODE (target) != tmode
17459       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17460     target = gen_reg_rtx (tmode);
17461   if (do_load)
17462     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
17463   else
17464     {
17465       if (VECTOR_MODE_P (mode0))
17466         op0 = safe_vector_operand (op0, mode0);
17467
17468       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
17469         op0 = copy_to_mode_reg (mode0, op0);
17470     }
17471
17472   pat = GEN_FCN (icode) (target, op0);
17473   if (! pat)
17474     return 0;
17475   emit_insn (pat);
17476   return target;
17477 }
17478
17479 static int
17480 neon_builtin_compare (const void *a, const void *b)
17481 {
17482   const neon_builtin_datum *const key = (const neon_builtin_datum *) a;
17483   const neon_builtin_datum *const memb = (const neon_builtin_datum *) b;
17484   unsigned int soughtcode = key->base_fcode;
17485
17486   if (soughtcode >= memb->base_fcode
17487       && soughtcode < memb->base_fcode + memb->num_vars)
17488     return 0;
17489   else if (soughtcode < memb->base_fcode)
17490     return -1;
17491   else
17492     return 1;
17493 }
17494
17495 static enum insn_code
17496 locate_neon_builtin_icode (int fcode, neon_itype *itype)
17497 {
17498   neon_builtin_datum key, *found;
17499   int idx;
17500
17501   key.base_fcode = fcode;
17502   found = (neon_builtin_datum *)
17503     bsearch (&key, &neon_builtin_data[0], ARRAY_SIZE (neon_builtin_data),
17504                    sizeof (neon_builtin_data[0]), neon_builtin_compare);
17505   gcc_assert (found);
17506   idx = fcode - (int) found->base_fcode;
17507   gcc_assert (idx >= 0 && idx < T_MAX && idx < (int)found->num_vars);
17508
17509   if (itype)
17510     *itype = found->itype;
17511
17512   return found->codes[idx];
17513 }
17514
17515 typedef enum {
17516   NEON_ARG_COPY_TO_REG,
17517   NEON_ARG_CONSTANT,
17518   NEON_ARG_STOP
17519 } builtin_arg;
17520
17521 #define NEON_MAX_BUILTIN_ARGS 5
17522
17523 /* Expand a Neon builtin.  */
17524 static rtx
17525 arm_expand_neon_args (rtx target, int icode, int have_retval,
17526                       tree exp, ...)
17527 {
17528   va_list ap;
17529   rtx pat;
17530   tree arg[NEON_MAX_BUILTIN_ARGS];
17531   rtx op[NEON_MAX_BUILTIN_ARGS];
17532   enum machine_mode tmode = insn_data[icode].operand[0].mode;
17533   enum machine_mode mode[NEON_MAX_BUILTIN_ARGS];
17534   int argc = 0;
17535
17536   if (have_retval
17537       && (!target
17538           || GET_MODE (target) != tmode
17539           || !(*insn_data[icode].operand[0].predicate) (target, tmode)))
17540     target = gen_reg_rtx (tmode);
17541
17542   va_start (ap, exp);
17543
17544   for (;;)
17545     {
17546       builtin_arg thisarg = (builtin_arg) va_arg (ap, int);
17547
17548       if (thisarg == NEON_ARG_STOP)
17549         break;
17550       else
17551         {
17552           arg[argc] = CALL_EXPR_ARG (exp, argc);
17553           op[argc] = expand_normal (arg[argc]);
17554           mode[argc] = insn_data[icode].operand[argc + have_retval].mode;
17555
17556           switch (thisarg)
17557             {
17558             case NEON_ARG_COPY_TO_REG:
17559               /*gcc_assert (GET_MODE (op[argc]) == mode[argc]);*/
17560               if (!(*insn_data[icode].operand[argc + have_retval].predicate)
17561                      (op[argc], mode[argc]))
17562                 op[argc] = copy_to_mode_reg (mode[argc], op[argc]);
17563               break;
17564
17565             case NEON_ARG_CONSTANT:
17566               /* FIXME: This error message is somewhat unhelpful.  */
17567               if (!(*insn_data[icode].operand[argc + have_retval].predicate)
17568                     (op[argc], mode[argc]))
17569                 error ("argument must be a constant");
17570               break;
17571
17572             case NEON_ARG_STOP:
17573               gcc_unreachable ();
17574             }
17575
17576           argc++;
17577         }
17578     }
17579
17580   va_end (ap);
17581
17582   if (have_retval)
17583     switch (argc)
17584       {
17585       case 1:
17586         pat = GEN_FCN (icode) (target, op[0]);
17587         break;
17588
17589       case 2:
17590         pat = GEN_FCN (icode) (target, op[0], op[1]);
17591         break;
17592
17593       case 3:
17594         pat = GEN_FCN (icode) (target, op[0], op[1], op[2]);
17595         break;
17596
17597       case 4:
17598         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3]);
17599         break;
17600
17601       case 5:
17602         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3], op[4]);
17603         break;
17604
17605       default:
17606         gcc_unreachable ();
17607       }
17608   else
17609     switch (argc)
17610       {
17611       case 1:
17612         pat = GEN_FCN (icode) (op[0]);
17613         break;
17614
17615       case 2:
17616         pat = GEN_FCN (icode) (op[0], op[1]);
17617         break;
17618
17619       case 3:
17620         pat = GEN_FCN (icode) (op[0], op[1], op[2]);
17621         break;
17622
17623       case 4:
17624         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
17625         break;
17626
17627       case 5:
17628         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4]);
17629         break;
17630
17631       default:
17632         gcc_unreachable ();
17633       }
17634
17635   if (!pat)
17636     return 0;
17637
17638   emit_insn (pat);
17639
17640   return target;
17641 }
17642
17643 /* Expand a Neon builtin. These are "special" because they don't have symbolic
17644    constants defined per-instruction or per instruction-variant. Instead, the
17645    required info is looked up in the table neon_builtin_data.  */
17646 static rtx
17647 arm_expand_neon_builtin (int fcode, tree exp, rtx target)
17648 {
17649   neon_itype itype;
17650   enum insn_code icode = locate_neon_builtin_icode (fcode, &itype);
17651
17652   switch (itype)
17653     {
17654     case NEON_UNOP:
17655     case NEON_CONVERT:
17656     case NEON_DUPLANE:
17657       return arm_expand_neon_args (target, icode, 1, exp,
17658         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_STOP);
17659
17660     case NEON_BINOP:
17661     case NEON_SETLANE:
17662     case NEON_SCALARMUL:
17663     case NEON_SCALARMULL:
17664     case NEON_SCALARMULH:
17665     case NEON_SHIFTINSERT:
17666     case NEON_LOGICBINOP:
17667       return arm_expand_neon_args (target, icode, 1, exp,
17668         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
17669         NEON_ARG_STOP);
17670
17671     case NEON_TERNOP:
17672       return arm_expand_neon_args (target, icode, 1, exp,
17673         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
17674         NEON_ARG_CONSTANT, NEON_ARG_STOP);
17675
17676     case NEON_GETLANE:
17677     case NEON_FIXCONV:
17678     case NEON_SHIFTIMM:
17679       return arm_expand_neon_args (target, icode, 1, exp,
17680         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_CONSTANT,
17681         NEON_ARG_STOP);
17682
17683     case NEON_CREATE:
17684       return arm_expand_neon_args (target, icode, 1, exp,
17685         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
17686
17687     case NEON_DUP:
17688     case NEON_SPLIT:
17689     case NEON_REINTERP:
17690       return arm_expand_neon_args (target, icode, 1, exp,
17691         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
17692
17693     case NEON_COMBINE:
17694     case NEON_VTBL:
17695       return arm_expand_neon_args (target, icode, 1, exp,
17696         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
17697
17698     case NEON_RESULTPAIR:
17699       return arm_expand_neon_args (target, icode, 0, exp,
17700         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
17701         NEON_ARG_STOP);
17702
17703     case NEON_LANEMUL:
17704     case NEON_LANEMULL:
17705     case NEON_LANEMULH:
17706       return arm_expand_neon_args (target, icode, 1, exp,
17707         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
17708         NEON_ARG_CONSTANT, NEON_ARG_STOP);
17709
17710     case NEON_LANEMAC:
17711       return arm_expand_neon_args (target, icode, 1, exp,
17712         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
17713         NEON_ARG_CONSTANT, NEON_ARG_CONSTANT, NEON_ARG_STOP);
17714
17715     case NEON_SHIFTACC:
17716       return arm_expand_neon_args (target, icode, 1, exp,
17717         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
17718         NEON_ARG_CONSTANT, NEON_ARG_STOP);
17719
17720     case NEON_SCALARMAC:
17721       return arm_expand_neon_args (target, icode, 1, exp,
17722         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
17723         NEON_ARG_CONSTANT, NEON_ARG_STOP);
17724
17725     case NEON_SELECT:
17726     case NEON_VTBX:
17727       return arm_expand_neon_args (target, icode, 1, exp,
17728         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
17729         NEON_ARG_STOP);
17730
17731     case NEON_LOAD1:
17732     case NEON_LOADSTRUCT:
17733       return arm_expand_neon_args (target, icode, 1, exp,
17734         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
17735
17736     case NEON_LOAD1LANE:
17737     case NEON_LOADSTRUCTLANE:
17738       return arm_expand_neon_args (target, icode, 1, exp,
17739         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
17740         NEON_ARG_STOP);
17741
17742     case NEON_STORE1:
17743     case NEON_STORESTRUCT:
17744       return arm_expand_neon_args (target, icode, 0, exp,
17745         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
17746
17747     case NEON_STORE1LANE:
17748     case NEON_STORESTRUCTLANE:
17749       return arm_expand_neon_args (target, icode, 0, exp,
17750         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
17751         NEON_ARG_STOP);
17752     }
17753
17754   gcc_unreachable ();
17755 }
17756
17757 /* Emit code to reinterpret one Neon type as another, without altering bits.  */
17758 void
17759 neon_reinterpret (rtx dest, rtx src)
17760 {
17761   emit_move_insn (dest, gen_lowpart (GET_MODE (dest), src));
17762 }
17763
17764 /* Emit code to place a Neon pair result in memory locations (with equal
17765    registers).  */
17766 void
17767 neon_emit_pair_result_insn (enum machine_mode mode,
17768                             rtx (*intfn) (rtx, rtx, rtx, rtx), rtx destaddr,
17769                             rtx op1, rtx op2)
17770 {
17771   rtx mem = gen_rtx_MEM (mode, destaddr);
17772   rtx tmp1 = gen_reg_rtx (mode);
17773   rtx tmp2 = gen_reg_rtx (mode);
17774
17775   emit_insn (intfn (tmp1, op1, tmp2, op2));
17776
17777   emit_move_insn (mem, tmp1);
17778   mem = adjust_address (mem, mode, GET_MODE_SIZE (mode));
17779   emit_move_insn (mem, tmp2);
17780 }
17781
17782 /* Set up operands for a register copy from src to dest, taking care not to
17783    clobber registers in the process.
17784    FIXME: This has rather high polynomial complexity (O(n^3)?) but shouldn't
17785    be called with a large N, so that should be OK.  */
17786
17787 void
17788 neon_disambiguate_copy (rtx *operands, rtx *dest, rtx *src, unsigned int count)
17789 {
17790   unsigned int copied = 0, opctr = 0;
17791   unsigned int done = (1 << count) - 1;
17792   unsigned int i, j;
17793
17794   while (copied != done)
17795     {
17796       for (i = 0; i < count; i++)
17797         {
17798           int good = 1;
17799
17800           for (j = 0; good && j < count; j++)
17801             if (i != j && (copied & (1 << j)) == 0
17802                 && reg_overlap_mentioned_p (src[j], dest[i]))
17803               good = 0;
17804
17805           if (good)
17806             {
17807               operands[opctr++] = dest[i];
17808               operands[opctr++] = src[i];
17809               copied |= 1 << i;
17810             }
17811         }
17812     }
17813
17814   gcc_assert (opctr == count * 2);
17815 }
17816
17817 /* Expand an expression EXP that calls a built-in function,
17818    with result going to TARGET if that's convenient
17819    (and in mode MODE if that's convenient).
17820    SUBTARGET may be used as the target for computing one of EXP's operands.
17821    IGNORE is nonzero if the value is to be ignored.  */
17822
17823 static rtx
17824 arm_expand_builtin (tree exp,
17825                     rtx target,
17826                     rtx subtarget ATTRIBUTE_UNUSED,
17827                     enum machine_mode mode ATTRIBUTE_UNUSED,
17828                     int ignore ATTRIBUTE_UNUSED)
17829 {
17830   const struct builtin_description * d;
17831   enum insn_code    icode;
17832   tree              fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
17833   tree              arg0;
17834   tree              arg1;
17835   tree              arg2;
17836   rtx               op0;
17837   rtx               op1;
17838   rtx               op2;
17839   rtx               pat;
17840   int               fcode = DECL_FUNCTION_CODE (fndecl);
17841   size_t            i;
17842   enum machine_mode tmode;
17843   enum machine_mode mode0;
17844   enum machine_mode mode1;
17845   enum machine_mode mode2;
17846
17847   if (fcode >= ARM_BUILTIN_NEON_BASE)
17848     return arm_expand_neon_builtin (fcode, exp, target);
17849
17850   switch (fcode)
17851     {
17852     case ARM_BUILTIN_TEXTRMSB:
17853     case ARM_BUILTIN_TEXTRMUB:
17854     case ARM_BUILTIN_TEXTRMSH:
17855     case ARM_BUILTIN_TEXTRMUH:
17856     case ARM_BUILTIN_TEXTRMSW:
17857     case ARM_BUILTIN_TEXTRMUW:
17858       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
17859                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
17860                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
17861                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
17862                : CODE_FOR_iwmmxt_textrmw);
17863
17864       arg0 = CALL_EXPR_ARG (exp, 0);
17865       arg1 = CALL_EXPR_ARG (exp, 1);
17866       op0 = expand_normal (arg0);
17867       op1 = expand_normal (arg1);
17868       tmode = insn_data[icode].operand[0].mode;
17869       mode0 = insn_data[icode].operand[1].mode;
17870       mode1 = insn_data[icode].operand[2].mode;
17871
17872       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
17873         op0 = copy_to_mode_reg (mode0, op0);
17874       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
17875         {
17876           /* @@@ better error message */
17877           error ("selector must be an immediate");
17878           return gen_reg_rtx (tmode);
17879         }
17880       if (target == 0
17881           || GET_MODE (target) != tmode
17882           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17883         target = gen_reg_rtx (tmode);
17884       pat = GEN_FCN (icode) (target, op0, op1);
17885       if (! pat)
17886         return 0;
17887       emit_insn (pat);
17888       return target;
17889
17890     case ARM_BUILTIN_TINSRB:
17891     case ARM_BUILTIN_TINSRH:
17892     case ARM_BUILTIN_TINSRW:
17893       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
17894                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
17895                : CODE_FOR_iwmmxt_tinsrw);
17896       arg0 = CALL_EXPR_ARG (exp, 0);
17897       arg1 = CALL_EXPR_ARG (exp, 1);
17898       arg2 = CALL_EXPR_ARG (exp, 2);
17899       op0 = expand_normal (arg0);
17900       op1 = expand_normal (arg1);
17901       op2 = expand_normal (arg2);
17902       tmode = insn_data[icode].operand[0].mode;
17903       mode0 = insn_data[icode].operand[1].mode;
17904       mode1 = insn_data[icode].operand[2].mode;
17905       mode2 = insn_data[icode].operand[3].mode;
17906
17907       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
17908         op0 = copy_to_mode_reg (mode0, op0);
17909       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
17910         op1 = copy_to_mode_reg (mode1, op1);
17911       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
17912         {
17913           /* @@@ better error message */
17914           error ("selector must be an immediate");
17915           return const0_rtx;
17916         }
17917       if (target == 0
17918           || GET_MODE (target) != tmode
17919           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17920         target = gen_reg_rtx (tmode);
17921       pat = GEN_FCN (icode) (target, op0, op1, op2);
17922       if (! pat)
17923         return 0;
17924       emit_insn (pat);
17925       return target;
17926
17927     case ARM_BUILTIN_SETWCX:
17928       arg0 = CALL_EXPR_ARG (exp, 0);
17929       arg1 = CALL_EXPR_ARG (exp, 1);
17930       op0 = force_reg (SImode, expand_normal (arg0));
17931       op1 = expand_normal (arg1);
17932       emit_insn (gen_iwmmxt_tmcr (op1, op0));
17933       return 0;
17934
17935     case ARM_BUILTIN_GETWCX:
17936       arg0 = CALL_EXPR_ARG (exp, 0);
17937       op0 = expand_normal (arg0);
17938       target = gen_reg_rtx (SImode);
17939       emit_insn (gen_iwmmxt_tmrc (target, op0));
17940       return target;
17941
17942     case ARM_BUILTIN_WSHUFH:
17943       icode = CODE_FOR_iwmmxt_wshufh;
17944       arg0 = CALL_EXPR_ARG (exp, 0);
17945       arg1 = CALL_EXPR_ARG (exp, 1);
17946       op0 = expand_normal (arg0);
17947       op1 = expand_normal (arg1);
17948       tmode = insn_data[icode].operand[0].mode;
17949       mode1 = insn_data[icode].operand[1].mode;
17950       mode2 = insn_data[icode].operand[2].mode;
17951
17952       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
17953         op0 = copy_to_mode_reg (mode1, op0);
17954       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
17955         {
17956           /* @@@ better error message */
17957           error ("mask must be an immediate");
17958           return const0_rtx;
17959         }
17960       if (target == 0
17961           || GET_MODE (target) != tmode
17962           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17963         target = gen_reg_rtx (tmode);
17964       pat = GEN_FCN (icode) (target, op0, op1);
17965       if (! pat)
17966         return 0;
17967       emit_insn (pat);
17968       return target;
17969
17970     case ARM_BUILTIN_WSADB:
17971       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, exp, target);
17972     case ARM_BUILTIN_WSADH:
17973       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, exp, target);
17974     case ARM_BUILTIN_WSADBZ:
17975       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, exp, target);
17976     case ARM_BUILTIN_WSADHZ:
17977       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, exp, target);
17978
17979       /* Several three-argument builtins.  */
17980     case ARM_BUILTIN_WMACS:
17981     case ARM_BUILTIN_WMACU:
17982     case ARM_BUILTIN_WALIGN:
17983     case ARM_BUILTIN_TMIA:
17984     case ARM_BUILTIN_TMIAPH:
17985     case ARM_BUILTIN_TMIATT:
17986     case ARM_BUILTIN_TMIATB:
17987     case ARM_BUILTIN_TMIABT:
17988     case ARM_BUILTIN_TMIABB:
17989       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
17990                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
17991                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
17992                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
17993                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
17994                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
17995                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
17996                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
17997                : CODE_FOR_iwmmxt_walign);
17998       arg0 = CALL_EXPR_ARG (exp, 0);
17999       arg1 = CALL_EXPR_ARG (exp, 1);
18000       arg2 = CALL_EXPR_ARG (exp, 2);
18001       op0 = expand_normal (arg0);
18002       op1 = expand_normal (arg1);
18003       op2 = expand_normal (arg2);
18004       tmode = insn_data[icode].operand[0].mode;
18005       mode0 = insn_data[icode].operand[1].mode;
18006       mode1 = insn_data[icode].operand[2].mode;
18007       mode2 = insn_data[icode].operand[3].mode;
18008
18009       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
18010         op0 = copy_to_mode_reg (mode0, op0);
18011       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
18012         op1 = copy_to_mode_reg (mode1, op1);
18013       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
18014         op2 = copy_to_mode_reg (mode2, op2);
18015       if (target == 0
18016           || GET_MODE (target) != tmode
18017           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
18018         target = gen_reg_rtx (tmode);
18019       pat = GEN_FCN (icode) (target, op0, op1, op2);
18020       if (! pat)
18021         return 0;
18022       emit_insn (pat);
18023       return target;
18024
18025     case ARM_BUILTIN_WZERO:
18026       target = gen_reg_rtx (DImode);
18027       emit_insn (gen_iwmmxt_clrdi (target));
18028       return target;
18029
18030     case ARM_BUILTIN_THREAD_POINTER:
18031       return arm_load_tp (target);
18032
18033     default:
18034       break;
18035     }
18036
18037   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
18038     if (d->code == (const enum arm_builtins) fcode)
18039       return arm_expand_binop_builtin (d->icode, exp, target);
18040
18041   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
18042     if (d->code == (const enum arm_builtins) fcode)
18043       return arm_expand_unop_builtin (d->icode, exp, target, 0);
18044
18045   /* @@@ Should really do something sensible here.  */
18046   return NULL_RTX;
18047 }
18048 \f
18049 /* Return the number (counting from 0) of
18050    the least significant set bit in MASK.  */
18051
18052 inline static int
18053 number_of_first_bit_set (unsigned mask)
18054 {
18055   int bit;
18056
18057   for (bit = 0;
18058        (mask & (1 << bit)) == 0;
18059        ++bit)
18060     continue;
18061
18062   return bit;
18063 }
18064
18065 /* Emit code to push or pop registers to or from the stack.  F is the
18066    assembly file.  MASK is the registers to push or pop.  PUSH is
18067    nonzero if we should push, and zero if we should pop.  For debugging
18068    output, if pushing, adjust CFA_OFFSET by the amount of space added
18069    to the stack.  REAL_REGS should have the same number of bits set as
18070    MASK, and will be used instead (in the same order) to describe which
18071    registers were saved - this is used to mark the save slots when we
18072    push high registers after moving them to low registers.  */
18073 static void
18074 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
18075                unsigned long real_regs)
18076 {
18077   int regno;
18078   int lo_mask = mask & 0xFF;
18079   int pushed_words = 0;
18080
18081   gcc_assert (mask);
18082
18083   if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
18084     {
18085       /* Special case.  Do not generate a POP PC statement here, do it in
18086          thumb_exit() */
18087       thumb_exit (f, -1);
18088       return;
18089     }
18090
18091   if (ARM_EABI_UNWIND_TABLES && push)
18092     {
18093       fprintf (f, "\t.save\t{");
18094       for (regno = 0; regno < 15; regno++)
18095         {
18096           if (real_regs & (1 << regno))
18097             {
18098               if (real_regs & ((1 << regno) -1))
18099                 fprintf (f, ", ");
18100               asm_fprintf (f, "%r", regno);
18101             }
18102         }
18103       fprintf (f, "}\n");
18104     }
18105
18106   fprintf (f, "\t%s\t{", push ? "push" : "pop");
18107
18108   /* Look at the low registers first.  */
18109   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
18110     {
18111       if (lo_mask & 1)
18112         {
18113           asm_fprintf (f, "%r", regno);
18114
18115           if ((lo_mask & ~1) != 0)
18116             fprintf (f, ", ");
18117
18118           pushed_words++;
18119         }
18120     }
18121
18122   if (push && (mask & (1 << LR_REGNUM)))
18123     {
18124       /* Catch pushing the LR.  */
18125       if (mask & 0xFF)
18126         fprintf (f, ", ");
18127
18128       asm_fprintf (f, "%r", LR_REGNUM);
18129
18130       pushed_words++;
18131     }
18132   else if (!push && (mask & (1 << PC_REGNUM)))
18133     {
18134       /* Catch popping the PC.  */
18135       if (TARGET_INTERWORK || TARGET_BACKTRACE
18136           || crtl->calls_eh_return)
18137         {
18138           /* The PC is never poped directly, instead
18139              it is popped into r3 and then BX is used.  */
18140           fprintf (f, "}\n");
18141
18142           thumb_exit (f, -1);
18143
18144           return;
18145         }
18146       else
18147         {
18148           if (mask & 0xFF)
18149             fprintf (f, ", ");
18150
18151           asm_fprintf (f, "%r", PC_REGNUM);
18152         }
18153     }
18154
18155   fprintf (f, "}\n");
18156
18157   if (push && pushed_words && dwarf2out_do_frame ())
18158     {
18159       char *l = dwarf2out_cfi_label (false);
18160       int pushed_mask = real_regs;
18161
18162       *cfa_offset += pushed_words * 4;
18163       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
18164
18165       pushed_words = 0;
18166       pushed_mask = real_regs;
18167       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
18168         {
18169           if (pushed_mask & 1)
18170             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
18171         }
18172     }
18173 }
18174
18175 /* Generate code to return from a thumb function.
18176    If 'reg_containing_return_addr' is -1, then the return address is
18177    actually on the stack, at the stack pointer.  */
18178 static void
18179 thumb_exit (FILE *f, int reg_containing_return_addr)
18180 {
18181   unsigned regs_available_for_popping;
18182   unsigned regs_to_pop;
18183   int pops_needed;
18184   unsigned available;
18185   unsigned required;
18186   int mode;
18187   int size;
18188   int restore_a4 = FALSE;
18189
18190   /* Compute the registers we need to pop.  */
18191   regs_to_pop = 0;
18192   pops_needed = 0;
18193
18194   if (reg_containing_return_addr == -1)
18195     {
18196       regs_to_pop |= 1 << LR_REGNUM;
18197       ++pops_needed;
18198     }
18199
18200   if (TARGET_BACKTRACE)
18201     {
18202       /* Restore the (ARM) frame pointer and stack pointer.  */
18203       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
18204       pops_needed += 2;
18205     }
18206
18207   /* If there is nothing to pop then just emit the BX instruction and
18208      return.  */
18209   if (pops_needed == 0)
18210     {
18211       if (crtl->calls_eh_return)
18212         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
18213
18214       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
18215       return;
18216     }
18217   /* Otherwise if we are not supporting interworking and we have not created
18218      a backtrace structure and the function was not entered in ARM mode then
18219      just pop the return address straight into the PC.  */
18220   else if (!TARGET_INTERWORK
18221            && !TARGET_BACKTRACE
18222            && !is_called_in_ARM_mode (current_function_decl)
18223            && !crtl->calls_eh_return)
18224     {
18225       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
18226       return;
18227     }
18228
18229   /* Find out how many of the (return) argument registers we can corrupt.  */
18230   regs_available_for_popping = 0;
18231
18232   /* If returning via __builtin_eh_return, the bottom three registers
18233      all contain information needed for the return.  */
18234   if (crtl->calls_eh_return)
18235     size = 12;
18236   else
18237     {
18238       /* If we can deduce the registers used from the function's
18239          return value.  This is more reliable that examining
18240          df_regs_ever_live_p () because that will be set if the register is
18241          ever used in the function, not just if the register is used
18242          to hold a return value.  */
18243
18244       if (crtl->return_rtx != 0)
18245         mode = GET_MODE (crtl->return_rtx);
18246       else
18247         mode = DECL_MODE (DECL_RESULT (current_function_decl));
18248
18249       size = GET_MODE_SIZE (mode);
18250
18251       if (size == 0)
18252         {
18253           /* In a void function we can use any argument register.
18254              In a function that returns a structure on the stack
18255              we can use the second and third argument registers.  */
18256           if (mode == VOIDmode)
18257             regs_available_for_popping =
18258               (1 << ARG_REGISTER (1))
18259               | (1 << ARG_REGISTER (2))
18260               | (1 << ARG_REGISTER (3));
18261           else
18262             regs_available_for_popping =
18263               (1 << ARG_REGISTER (2))
18264               | (1 << ARG_REGISTER (3));
18265         }
18266       else if (size <= 4)
18267         regs_available_for_popping =
18268           (1 << ARG_REGISTER (2))
18269           | (1 << ARG_REGISTER (3));
18270       else if (size <= 8)
18271         regs_available_for_popping =
18272           (1 << ARG_REGISTER (3));
18273     }
18274
18275   /* Match registers to be popped with registers into which we pop them.  */
18276   for (available = regs_available_for_popping,
18277        required  = regs_to_pop;
18278        required != 0 && available != 0;
18279        available &= ~(available & - available),
18280        required  &= ~(required  & - required))
18281     -- pops_needed;
18282
18283   /* If we have any popping registers left over, remove them.  */
18284   if (available > 0)
18285     regs_available_for_popping &= ~available;
18286
18287   /* Otherwise if we need another popping register we can use
18288      the fourth argument register.  */
18289   else if (pops_needed)
18290     {
18291       /* If we have not found any free argument registers and
18292          reg a4 contains the return address, we must move it.  */
18293       if (regs_available_for_popping == 0
18294           && reg_containing_return_addr == LAST_ARG_REGNUM)
18295         {
18296           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
18297           reg_containing_return_addr = LR_REGNUM;
18298         }
18299       else if (size > 12)
18300         {
18301           /* Register a4 is being used to hold part of the return value,
18302              but we have dire need of a free, low register.  */
18303           restore_a4 = TRUE;
18304
18305           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
18306         }
18307
18308       if (reg_containing_return_addr != LAST_ARG_REGNUM)
18309         {
18310           /* The fourth argument register is available.  */
18311           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
18312
18313           --pops_needed;
18314         }
18315     }
18316
18317   /* Pop as many registers as we can.  */
18318   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
18319                  regs_available_for_popping);
18320
18321   /* Process the registers we popped.  */
18322   if (reg_containing_return_addr == -1)
18323     {
18324       /* The return address was popped into the lowest numbered register.  */
18325       regs_to_pop &= ~(1 << LR_REGNUM);
18326
18327       reg_containing_return_addr =
18328         number_of_first_bit_set (regs_available_for_popping);
18329
18330       /* Remove this register for the mask of available registers, so that
18331          the return address will not be corrupted by further pops.  */
18332       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
18333     }
18334
18335   /* If we popped other registers then handle them here.  */
18336   if (regs_available_for_popping)
18337     {
18338       int frame_pointer;
18339
18340       /* Work out which register currently contains the frame pointer.  */
18341       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
18342
18343       /* Move it into the correct place.  */
18344       asm_fprintf (f, "\tmov\t%r, %r\n",
18345                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
18346
18347       /* (Temporarily) remove it from the mask of popped registers.  */
18348       regs_available_for_popping &= ~(1 << frame_pointer);
18349       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
18350
18351       if (regs_available_for_popping)
18352         {
18353           int stack_pointer;
18354
18355           /* We popped the stack pointer as well,
18356              find the register that contains it.  */
18357           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
18358
18359           /* Move it into the stack register.  */
18360           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
18361
18362           /* At this point we have popped all necessary registers, so
18363              do not worry about restoring regs_available_for_popping
18364              to its correct value:
18365
18366              assert (pops_needed == 0)
18367              assert (regs_available_for_popping == (1 << frame_pointer))
18368              assert (regs_to_pop == (1 << STACK_POINTER))  */
18369         }
18370       else
18371         {
18372           /* Since we have just move the popped value into the frame
18373              pointer, the popping register is available for reuse, and
18374              we know that we still have the stack pointer left to pop.  */
18375           regs_available_for_popping |= (1 << frame_pointer);
18376         }
18377     }
18378
18379   /* If we still have registers left on the stack, but we no longer have
18380      any registers into which we can pop them, then we must move the return
18381      address into the link register and make available the register that
18382      contained it.  */
18383   if (regs_available_for_popping == 0 && pops_needed > 0)
18384     {
18385       regs_available_for_popping |= 1 << reg_containing_return_addr;
18386
18387       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
18388                    reg_containing_return_addr);
18389
18390       reg_containing_return_addr = LR_REGNUM;
18391     }
18392
18393   /* If we have registers left on the stack then pop some more.
18394      We know that at most we will want to pop FP and SP.  */
18395   if (pops_needed > 0)
18396     {
18397       int  popped_into;
18398       int  move_to;
18399
18400       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
18401                      regs_available_for_popping);
18402
18403       /* We have popped either FP or SP.
18404          Move whichever one it is into the correct register.  */
18405       popped_into = number_of_first_bit_set (regs_available_for_popping);
18406       move_to     = number_of_first_bit_set (regs_to_pop);
18407
18408       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
18409
18410       regs_to_pop &= ~(1 << move_to);
18411
18412       --pops_needed;
18413     }
18414
18415   /* If we still have not popped everything then we must have only
18416      had one register available to us and we are now popping the SP.  */
18417   if (pops_needed > 0)
18418     {
18419       int  popped_into;
18420
18421       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
18422                      regs_available_for_popping);
18423
18424       popped_into = number_of_first_bit_set (regs_available_for_popping);
18425
18426       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
18427       /*
18428         assert (regs_to_pop == (1 << STACK_POINTER))
18429         assert (pops_needed == 1)
18430       */
18431     }
18432
18433   /* If necessary restore the a4 register.  */
18434   if (restore_a4)
18435     {
18436       if (reg_containing_return_addr != LR_REGNUM)
18437         {
18438           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
18439           reg_containing_return_addr = LR_REGNUM;
18440         }
18441
18442       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
18443     }
18444
18445   if (crtl->calls_eh_return)
18446     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
18447
18448   /* Return to caller.  */
18449   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
18450 }
18451
18452 \f
18453 void
18454 thumb1_final_prescan_insn (rtx insn)
18455 {
18456   if (flag_print_asm_name)
18457     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
18458                  INSN_ADDRESSES (INSN_UID (insn)));
18459 }
18460
18461 int
18462 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
18463 {
18464   unsigned HOST_WIDE_INT mask = 0xff;
18465   int i;
18466
18467   val = val & (unsigned HOST_WIDE_INT)0xffffffffu;
18468   if (val == 0) /* XXX */
18469     return 0;
18470
18471   for (i = 0; i < 25; i++)
18472     if ((val & (mask << i)) == val)
18473       return 1;
18474
18475   return 0;
18476 }
18477
18478 /* Returns nonzero if the current function contains,
18479    or might contain a far jump.  */
18480 static int
18481 thumb_far_jump_used_p (void)
18482 {
18483   rtx insn;
18484
18485   /* This test is only important for leaf functions.  */
18486   /* assert (!leaf_function_p ()); */
18487
18488   /* If we have already decided that far jumps may be used,
18489      do not bother checking again, and always return true even if
18490      it turns out that they are not being used.  Once we have made
18491      the decision that far jumps are present (and that hence the link
18492      register will be pushed onto the stack) we cannot go back on it.  */
18493   if (cfun->machine->far_jump_used)
18494     return 1;
18495
18496   /* If this function is not being called from the prologue/epilogue
18497      generation code then it must be being called from the
18498      INITIAL_ELIMINATION_OFFSET macro.  */
18499   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
18500     {
18501       /* In this case we know that we are being asked about the elimination
18502          of the arg pointer register.  If that register is not being used,
18503          then there are no arguments on the stack, and we do not have to
18504          worry that a far jump might force the prologue to push the link
18505          register, changing the stack offsets.  In this case we can just
18506          return false, since the presence of far jumps in the function will
18507          not affect stack offsets.
18508
18509          If the arg pointer is live (or if it was live, but has now been
18510          eliminated and so set to dead) then we do have to test to see if
18511          the function might contain a far jump.  This test can lead to some
18512          false negatives, since before reload is completed, then length of
18513          branch instructions is not known, so gcc defaults to returning their
18514          longest length, which in turn sets the far jump attribute to true.
18515
18516          A false negative will not result in bad code being generated, but it
18517          will result in a needless push and pop of the link register.  We
18518          hope that this does not occur too often.
18519
18520          If we need doubleword stack alignment this could affect the other
18521          elimination offsets so we can't risk getting it wrong.  */
18522       if (df_regs_ever_live_p (ARG_POINTER_REGNUM))
18523         cfun->machine->arg_pointer_live = 1;
18524       else if (!cfun->machine->arg_pointer_live)
18525         return 0;
18526     }
18527
18528   /* Check to see if the function contains a branch
18529      insn with the far jump attribute set.  */
18530   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
18531     {
18532       if (GET_CODE (insn) == JUMP_INSN
18533           /* Ignore tablejump patterns.  */
18534           && GET_CODE (PATTERN (insn)) != ADDR_VEC
18535           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
18536           && get_attr_far_jump (insn) == FAR_JUMP_YES
18537           )
18538         {
18539           /* Record the fact that we have decided that
18540              the function does use far jumps.  */
18541           cfun->machine->far_jump_used = 1;
18542           return 1;
18543         }
18544     }
18545
18546   return 0;
18547 }
18548
18549 /* Return nonzero if FUNC must be entered in ARM mode.  */
18550 int
18551 is_called_in_ARM_mode (tree func)
18552 {
18553   gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
18554
18555   /* Ignore the problem about functions whose address is taken.  */
18556   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
18557     return TRUE;
18558
18559 #ifdef ARM_PE
18560   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
18561 #else
18562   return FALSE;
18563 #endif
18564 }
18565
18566 /* The bits which aren't usefully expanded as rtl.  */
18567 const char *
18568 thumb_unexpanded_epilogue (void)
18569 {
18570   arm_stack_offsets *offsets;
18571   int regno;
18572   unsigned long live_regs_mask = 0;
18573   int high_regs_pushed = 0;
18574   int had_to_push_lr;
18575   int size;
18576
18577   if (cfun->machine->return_used_this_function != 0)
18578     return "";
18579
18580   if (IS_NAKED (arm_current_func_type ()))
18581     return "";
18582
18583   offsets = arm_get_frame_offsets ();
18584   live_regs_mask = offsets->saved_regs_mask;
18585   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
18586
18587   /* If we can deduce the registers used from the function's return value.
18588      This is more reliable that examining df_regs_ever_live_p () because that
18589      will be set if the register is ever used in the function, not just if
18590      the register is used to hold a return value.  */
18591   size = arm_size_return_regs ();
18592
18593   /* The prolog may have pushed some high registers to use as
18594      work registers.  e.g. the testsuite file:
18595      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
18596      compiles to produce:
18597         push    {r4, r5, r6, r7, lr}
18598         mov     r7, r9
18599         mov     r6, r8
18600         push    {r6, r7}
18601      as part of the prolog.  We have to undo that pushing here.  */
18602
18603   if (high_regs_pushed)
18604     {
18605       unsigned long mask = live_regs_mask & 0xff;
18606       int next_hi_reg;
18607
18608       /* The available low registers depend on the size of the value we are
18609          returning.  */
18610       if (size <= 12)
18611         mask |=  1 << 3;
18612       if (size <= 8)
18613         mask |= 1 << 2;
18614
18615       if (mask == 0)
18616         /* Oh dear!  We have no low registers into which we can pop
18617            high registers!  */
18618         internal_error
18619           ("no low registers available for popping high registers");
18620
18621       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
18622         if (live_regs_mask & (1 << next_hi_reg))
18623           break;
18624
18625       while (high_regs_pushed)
18626         {
18627           /* Find lo register(s) into which the high register(s) can
18628              be popped.  */
18629           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
18630             {
18631               if (mask & (1 << regno))
18632                 high_regs_pushed--;
18633               if (high_regs_pushed == 0)
18634                 break;
18635             }
18636
18637           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
18638
18639           /* Pop the values into the low register(s).  */
18640           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
18641
18642           /* Move the value(s) into the high registers.  */
18643           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
18644             {
18645               if (mask & (1 << regno))
18646                 {
18647                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
18648                                regno);
18649
18650                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
18651                     if (live_regs_mask & (1 << next_hi_reg))
18652                       break;
18653                 }
18654             }
18655         }
18656       live_regs_mask &= ~0x0f00;
18657     }
18658
18659   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
18660   live_regs_mask &= 0xff;
18661
18662   if (crtl->args.pretend_args_size == 0 || TARGET_BACKTRACE)
18663     {
18664       /* Pop the return address into the PC.  */
18665       if (had_to_push_lr)
18666         live_regs_mask |= 1 << PC_REGNUM;
18667
18668       /* Either no argument registers were pushed or a backtrace
18669          structure was created which includes an adjusted stack
18670          pointer, so just pop everything.  */
18671       if (live_regs_mask)
18672         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
18673                        live_regs_mask);
18674
18675       /* We have either just popped the return address into the
18676          PC or it is was kept in LR for the entire function.  */
18677       if (!had_to_push_lr)
18678         thumb_exit (asm_out_file, LR_REGNUM);
18679     }
18680   else
18681     {
18682       /* Pop everything but the return address.  */
18683       if (live_regs_mask)
18684         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
18685                        live_regs_mask);
18686
18687       if (had_to_push_lr)
18688         {
18689           if (size > 12)
18690             {
18691               /* We have no free low regs, so save one.  */
18692               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
18693                            LAST_ARG_REGNUM);
18694             }
18695
18696           /* Get the return address into a temporary register.  */
18697           thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
18698                          1 << LAST_ARG_REGNUM);
18699
18700           if (size > 12)
18701             {
18702               /* Move the return address to lr.  */
18703               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
18704                            LAST_ARG_REGNUM);
18705               /* Restore the low register.  */
18706               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
18707                            IP_REGNUM);
18708               regno = LR_REGNUM;
18709             }
18710           else
18711             regno = LAST_ARG_REGNUM;
18712         }
18713       else
18714         regno = LR_REGNUM;
18715
18716       /* Remove the argument registers that were pushed onto the stack.  */
18717       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
18718                    SP_REGNUM, SP_REGNUM,
18719                    crtl->args.pretend_args_size);
18720
18721       thumb_exit (asm_out_file, regno);
18722     }
18723
18724   return "";
18725 }
18726
18727 /* Functions to save and restore machine-specific function data.  */
18728 static struct machine_function *
18729 arm_init_machine_status (void)
18730 {
18731   struct machine_function *machine;
18732   machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
18733
18734 #if ARM_FT_UNKNOWN != 0
18735   machine->func_type = ARM_FT_UNKNOWN;
18736 #endif
18737   return machine;
18738 }
18739
18740 /* Return an RTX indicating where the return address to the
18741    calling function can be found.  */
18742 rtx
18743 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
18744 {
18745   if (count != 0)
18746     return NULL_RTX;
18747
18748   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
18749 }
18750
18751 /* Do anything needed before RTL is emitted for each function.  */
18752 void
18753 arm_init_expanders (void)
18754 {
18755   /* Arrange to initialize and mark the machine per-function status.  */
18756   init_machine_status = arm_init_machine_status;
18757
18758   /* This is to stop the combine pass optimizing away the alignment
18759      adjustment of va_arg.  */
18760   /* ??? It is claimed that this should not be necessary.  */
18761   if (cfun)
18762     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
18763 }
18764
18765
18766 /* Like arm_compute_initial_elimination offset.  Simpler because there
18767    isn't an ABI specified frame pointer for Thumb.  Instead, we set it
18768    to point at the base of the local variables after static stack
18769    space for a function has been allocated.  */
18770
18771 HOST_WIDE_INT
18772 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
18773 {
18774   arm_stack_offsets *offsets;
18775
18776   offsets = arm_get_frame_offsets ();
18777
18778   switch (from)
18779     {
18780     case ARG_POINTER_REGNUM:
18781       switch (to)
18782         {
18783         case STACK_POINTER_REGNUM:
18784           return offsets->outgoing_args - offsets->saved_args;
18785
18786         case FRAME_POINTER_REGNUM:
18787           return offsets->soft_frame - offsets->saved_args;
18788
18789         case ARM_HARD_FRAME_POINTER_REGNUM:
18790           return offsets->saved_regs - offsets->saved_args;
18791
18792         case THUMB_HARD_FRAME_POINTER_REGNUM:
18793           return offsets->locals_base - offsets->saved_args;
18794
18795         default:
18796           gcc_unreachable ();
18797         }
18798       break;
18799
18800     case FRAME_POINTER_REGNUM:
18801       switch (to)
18802         {
18803         case STACK_POINTER_REGNUM:
18804           return offsets->outgoing_args - offsets->soft_frame;
18805
18806         case ARM_HARD_FRAME_POINTER_REGNUM:
18807           return offsets->saved_regs - offsets->soft_frame;
18808
18809         case THUMB_HARD_FRAME_POINTER_REGNUM:
18810           return offsets->locals_base - offsets->soft_frame;
18811
18812         default:
18813           gcc_unreachable ();
18814         }
18815       break;
18816
18817     default:
18818       gcc_unreachable ();
18819     }
18820 }
18821
18822 /* Generate the rest of a function's prologue.  */
18823 void
18824 thumb1_expand_prologue (void)
18825 {
18826   rtx insn, dwarf;
18827
18828   HOST_WIDE_INT amount;
18829   arm_stack_offsets *offsets;
18830   unsigned long func_type;
18831   int regno;
18832   unsigned long live_regs_mask;
18833
18834   func_type = arm_current_func_type ();
18835
18836   /* Naked functions don't have prologues.  */
18837   if (IS_NAKED (func_type))
18838     return;
18839
18840   if (IS_INTERRUPT (func_type))
18841     {
18842       error ("interrupt Service Routines cannot be coded in Thumb mode");
18843       return;
18844     }
18845
18846   offsets = arm_get_frame_offsets ();
18847   live_regs_mask = offsets->saved_regs_mask;
18848   /* Load the pic register before setting the frame pointer,
18849      so we can use r7 as a temporary work register.  */
18850   if (flag_pic && arm_pic_register != INVALID_REGNUM)
18851     arm_load_pic_register (live_regs_mask);
18852
18853   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
18854     emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
18855                     stack_pointer_rtx);
18856
18857   amount = offsets->outgoing_args - offsets->saved_regs;
18858   if (amount)
18859     {
18860       if (amount < 512)
18861         {
18862           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
18863                                         GEN_INT (- amount)));
18864           RTX_FRAME_RELATED_P (insn) = 1;
18865         }
18866       else
18867         {
18868           rtx reg;
18869
18870           /* The stack decrement is too big for an immediate value in a single
18871              insn.  In theory we could issue multiple subtracts, but after
18872              three of them it becomes more space efficient to place the full
18873              value in the constant pool and load into a register.  (Also the
18874              ARM debugger really likes to see only one stack decrement per
18875              function).  So instead we look for a scratch register into which
18876              we can load the decrement, and then we subtract this from the
18877              stack pointer.  Unfortunately on the thumb the only available
18878              scratch registers are the argument registers, and we cannot use
18879              these as they may hold arguments to the function.  Instead we
18880              attempt to locate a call preserved register which is used by this
18881              function.  If we can find one, then we know that it will have
18882              been pushed at the start of the prologue and so we can corrupt
18883              it now.  */
18884           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
18885             if (live_regs_mask & (1 << regno))
18886               break;
18887
18888           gcc_assert(regno <= LAST_LO_REGNUM);
18889
18890           reg = gen_rtx_REG (SImode, regno);
18891
18892           emit_insn (gen_movsi (reg, GEN_INT (- amount)));
18893
18894           insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
18895                                         stack_pointer_rtx, reg));
18896           RTX_FRAME_RELATED_P (insn) = 1;
18897           dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
18898                                plus_constant (stack_pointer_rtx,
18899                                               -amount));
18900           RTX_FRAME_RELATED_P (dwarf) = 1;
18901           add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
18902         }
18903     }
18904
18905   if (frame_pointer_needed)
18906     thumb_set_frame_pointer (offsets);
18907
18908   /* If we are profiling, make sure no instructions are scheduled before
18909      the call to mcount.  Similarly if the user has requested no
18910      scheduling in the prolog.  Similarly if we want non-call exceptions
18911      using the EABI unwinder, to prevent faulting instructions from being
18912      swapped with a stack adjustment.  */
18913   if (crtl->profile || !TARGET_SCHED_PROLOG
18914       || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
18915     emit_insn (gen_blockage ());
18916
18917   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
18918   if (live_regs_mask & 0xff)
18919     cfun->machine->lr_save_eliminated = 0;
18920 }
18921
18922
18923 void
18924 thumb1_expand_epilogue (void)
18925 {
18926   HOST_WIDE_INT amount;
18927   arm_stack_offsets *offsets;
18928   int regno;
18929
18930   /* Naked functions don't have prologues.  */
18931   if (IS_NAKED (arm_current_func_type ()))
18932     return;
18933
18934   offsets = arm_get_frame_offsets ();
18935   amount = offsets->outgoing_args - offsets->saved_regs;
18936
18937   if (frame_pointer_needed)
18938     {
18939       emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
18940       amount = offsets->locals_base - offsets->saved_regs;
18941     }
18942
18943   gcc_assert (amount >= 0);
18944   if (amount)
18945     {
18946       if (amount < 512)
18947         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
18948                                GEN_INT (amount)));
18949       else
18950         {
18951           /* r3 is always free in the epilogue.  */
18952           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
18953
18954           emit_insn (gen_movsi (reg, GEN_INT (amount)));
18955           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
18956         }
18957     }
18958
18959   /* Emit a USE (stack_pointer_rtx), so that
18960      the stack adjustment will not be deleted.  */
18961   emit_insn (gen_prologue_use (stack_pointer_rtx));
18962
18963   if (crtl->profile || !TARGET_SCHED_PROLOG)
18964     emit_insn (gen_blockage ());
18965
18966   /* Emit a clobber for each insn that will be restored in the epilogue,
18967      so that flow2 will get register lifetimes correct.  */
18968   for (regno = 0; regno < 13; regno++)
18969     if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
18970       emit_clobber (gen_rtx_REG (SImode, regno));
18971
18972   if (! df_regs_ever_live_p (LR_REGNUM))
18973     emit_use (gen_rtx_REG (SImode, LR_REGNUM));
18974 }
18975
18976 static void
18977 thumb1_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
18978 {
18979   arm_stack_offsets *offsets;
18980   unsigned long live_regs_mask = 0;
18981   unsigned long l_mask;
18982   unsigned high_regs_pushed = 0;
18983   int cfa_offset = 0;
18984   int regno;
18985
18986   if (IS_NAKED (arm_current_func_type ()))
18987     return;
18988
18989   if (is_called_in_ARM_mode (current_function_decl))
18990     {
18991       const char * name;
18992
18993       gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
18994       gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
18995                   == SYMBOL_REF);
18996       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
18997
18998       /* Generate code sequence to switch us into Thumb mode.  */
18999       /* The .code 32 directive has already been emitted by
19000          ASM_DECLARE_FUNCTION_NAME.  */
19001       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
19002       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
19003
19004       /* Generate a label, so that the debugger will notice the
19005          change in instruction sets.  This label is also used by
19006          the assembler to bypass the ARM code when this function
19007          is called from a Thumb encoded function elsewhere in the
19008          same file.  Hence the definition of STUB_NAME here must
19009          agree with the definition in gas/config/tc-arm.c.  */
19010
19011 #define STUB_NAME ".real_start_of"
19012
19013       fprintf (f, "\t.code\t16\n");
19014 #ifdef ARM_PE
19015       if (arm_dllexport_name_p (name))
19016         name = arm_strip_name_encoding (name);
19017 #endif
19018       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
19019       fprintf (f, "\t.thumb_func\n");
19020       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
19021     }
19022
19023   if (crtl->args.pretend_args_size)
19024     {
19025       /* Output unwind directive for the stack adjustment.  */
19026       if (ARM_EABI_UNWIND_TABLES)
19027         fprintf (f, "\t.pad #%d\n",
19028                  crtl->args.pretend_args_size);
19029
19030       if (cfun->machine->uses_anonymous_args)
19031         {
19032           int num_pushes;
19033
19034           fprintf (f, "\tpush\t{");
19035
19036           num_pushes = ARM_NUM_INTS (crtl->args.pretend_args_size);
19037
19038           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
19039                regno <= LAST_ARG_REGNUM;
19040                regno++)
19041             asm_fprintf (f, "%r%s", regno,
19042                          regno == LAST_ARG_REGNUM ? "" : ", ");
19043
19044           fprintf (f, "}\n");
19045         }
19046       else
19047         asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
19048                      SP_REGNUM, SP_REGNUM,
19049                      crtl->args.pretend_args_size);
19050
19051       /* We don't need to record the stores for unwinding (would it
19052          help the debugger any if we did?), but record the change in
19053          the stack pointer.  */
19054       if (dwarf2out_do_frame ())
19055         {
19056           char *l = dwarf2out_cfi_label (false);
19057
19058           cfa_offset = cfa_offset + crtl->args.pretend_args_size;
19059           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
19060         }
19061     }
19062
19063   /* Get the registers we are going to push.  */
19064   offsets = arm_get_frame_offsets ();
19065   live_regs_mask = offsets->saved_regs_mask;
19066   /* Extract a mask of the ones we can give to the Thumb's push instruction.  */
19067   l_mask = live_regs_mask & 0x40ff;
19068   /* Then count how many other high registers will need to be pushed.  */
19069   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
19070
19071   if (TARGET_BACKTRACE)
19072     {
19073       unsigned offset;
19074       unsigned work_register;
19075
19076       /* We have been asked to create a stack backtrace structure.
19077          The code looks like this:
19078
19079          0   .align 2
19080          0   func:
19081          0     sub   SP, #16         Reserve space for 4 registers.
19082          2     push  {R7}            Push low registers.
19083          4     add   R7, SP, #20     Get the stack pointer before the push.
19084          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
19085          8     mov   R7, PC          Get hold of the start of this code plus 12.
19086         10     str   R7, [SP, #16]   Store it.
19087         12     mov   R7, FP          Get hold of the current frame pointer.
19088         14     str   R7, [SP, #4]    Store it.
19089         16     mov   R7, LR          Get hold of the current return address.
19090         18     str   R7, [SP, #12]   Store it.
19091         20     add   R7, SP, #16     Point at the start of the backtrace structure.
19092         22     mov   FP, R7          Put this value into the frame pointer.  */
19093
19094       work_register = thumb_find_work_register (live_regs_mask);
19095
19096       if (ARM_EABI_UNWIND_TABLES)
19097         asm_fprintf (f, "\t.pad #16\n");
19098
19099       asm_fprintf
19100         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
19101          SP_REGNUM, SP_REGNUM);
19102
19103       if (dwarf2out_do_frame ())
19104         {
19105           char *l = dwarf2out_cfi_label (false);
19106
19107           cfa_offset = cfa_offset + 16;
19108           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
19109         }
19110
19111       if (l_mask)
19112         {
19113           thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
19114           offset = bit_count (l_mask) * UNITS_PER_WORD;
19115         }
19116       else
19117         offset = 0;
19118
19119       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
19120                    offset + 16 + crtl->args.pretend_args_size);
19121
19122       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
19123                    offset + 4);
19124
19125       /* Make sure that the instruction fetching the PC is in the right place
19126          to calculate "start of backtrace creation code + 12".  */
19127       if (l_mask)
19128         {
19129           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
19130           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
19131                        offset + 12);
19132           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
19133                        ARM_HARD_FRAME_POINTER_REGNUM);
19134           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
19135                        offset);
19136         }
19137       else
19138         {
19139           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
19140                        ARM_HARD_FRAME_POINTER_REGNUM);
19141           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
19142                        offset);
19143           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
19144           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
19145                        offset + 12);
19146         }
19147
19148       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
19149       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
19150                    offset + 8);
19151       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
19152                    offset + 12);
19153       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
19154                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
19155     }
19156   /* Optimization:  If we are not pushing any low registers but we are going
19157      to push some high registers then delay our first push.  This will just
19158      be a push of LR and we can combine it with the push of the first high
19159      register.  */
19160   else if ((l_mask & 0xff) != 0
19161            || (high_regs_pushed == 0 && l_mask))
19162     thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
19163
19164   if (high_regs_pushed)
19165     {
19166       unsigned pushable_regs;
19167       unsigned next_hi_reg;
19168
19169       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
19170         if (live_regs_mask & (1 << next_hi_reg))
19171           break;
19172
19173       pushable_regs = l_mask & 0xff;
19174
19175       if (pushable_regs == 0)
19176         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
19177
19178       while (high_regs_pushed > 0)
19179         {
19180           unsigned long real_regs_mask = 0;
19181
19182           for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
19183             {
19184               if (pushable_regs & (1 << regno))
19185                 {
19186                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
19187
19188                   high_regs_pushed --;
19189                   real_regs_mask |= (1 << next_hi_reg);
19190
19191                   if (high_regs_pushed)
19192                     {
19193                       for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
19194                            next_hi_reg --)
19195                         if (live_regs_mask & (1 << next_hi_reg))
19196                           break;
19197                     }
19198                   else
19199                     {
19200                       pushable_regs &= ~((1 << regno) - 1);
19201                       break;
19202                     }
19203                 }
19204             }
19205
19206           /* If we had to find a work register and we have not yet
19207              saved the LR then add it to the list of regs to push.  */
19208           if (l_mask == (1 << LR_REGNUM))
19209             {
19210               thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
19211                              1, &cfa_offset,
19212                              real_regs_mask | (1 << LR_REGNUM));
19213               l_mask = 0;
19214             }
19215           else
19216             thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
19217         }
19218     }
19219 }
19220
19221 /* Handle the case of a double word load into a low register from
19222    a computed memory address.  The computed address may involve a
19223    register which is overwritten by the load.  */
19224 const char *
19225 thumb_load_double_from_address (rtx *operands)
19226 {
19227   rtx addr;
19228   rtx base;
19229   rtx offset;
19230   rtx arg1;
19231   rtx arg2;
19232
19233   gcc_assert (GET_CODE (operands[0]) == REG);
19234   gcc_assert (GET_CODE (operands[1]) == MEM);
19235
19236   /* Get the memory address.  */
19237   addr = XEXP (operands[1], 0);
19238
19239   /* Work out how the memory address is computed.  */
19240   switch (GET_CODE (addr))
19241     {
19242     case REG:
19243       operands[2] = adjust_address (operands[1], SImode, 4);
19244
19245       if (REGNO (operands[0]) == REGNO (addr))
19246         {
19247           output_asm_insn ("ldr\t%H0, %2", operands);
19248           output_asm_insn ("ldr\t%0, %1", operands);
19249         }
19250       else
19251         {
19252           output_asm_insn ("ldr\t%0, %1", operands);
19253           output_asm_insn ("ldr\t%H0, %2", operands);
19254         }
19255       break;
19256
19257     case CONST:
19258       /* Compute <address> + 4 for the high order load.  */
19259       operands[2] = adjust_address (operands[1], SImode, 4);
19260
19261       output_asm_insn ("ldr\t%0, %1", operands);
19262       output_asm_insn ("ldr\t%H0, %2", operands);
19263       break;
19264
19265     case PLUS:
19266       arg1   = XEXP (addr, 0);
19267       arg2   = XEXP (addr, 1);
19268
19269       if (CONSTANT_P (arg1))
19270         base = arg2, offset = arg1;
19271       else
19272         base = arg1, offset = arg2;
19273
19274       gcc_assert (GET_CODE (base) == REG);
19275
19276       /* Catch the case of <address> = <reg> + <reg> */
19277       if (GET_CODE (offset) == REG)
19278         {
19279           int reg_offset = REGNO (offset);
19280           int reg_base   = REGNO (base);
19281           int reg_dest   = REGNO (operands[0]);
19282
19283           /* Add the base and offset registers together into the
19284              higher destination register.  */
19285           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
19286                        reg_dest + 1, reg_base, reg_offset);
19287
19288           /* Load the lower destination register from the address in
19289              the higher destination register.  */
19290           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
19291                        reg_dest, reg_dest + 1);
19292
19293           /* Load the higher destination register from its own address
19294              plus 4.  */
19295           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
19296                        reg_dest + 1, reg_dest + 1);
19297         }
19298       else
19299         {
19300           /* Compute <address> + 4 for the high order load.  */
19301           operands[2] = adjust_address (operands[1], SImode, 4);
19302
19303           /* If the computed address is held in the low order register
19304              then load the high order register first, otherwise always
19305              load the low order register first.  */
19306           if (REGNO (operands[0]) == REGNO (base))
19307             {
19308               output_asm_insn ("ldr\t%H0, %2", operands);
19309               output_asm_insn ("ldr\t%0, %1", operands);
19310             }
19311           else
19312             {
19313               output_asm_insn ("ldr\t%0, %1", operands);
19314               output_asm_insn ("ldr\t%H0, %2", operands);
19315             }
19316         }
19317       break;
19318
19319     case LABEL_REF:
19320       /* With no registers to worry about we can just load the value
19321          directly.  */
19322       operands[2] = adjust_address (operands[1], SImode, 4);
19323
19324       output_asm_insn ("ldr\t%H0, %2", operands);
19325       output_asm_insn ("ldr\t%0, %1", operands);
19326       break;
19327
19328     default:
19329       gcc_unreachable ();
19330     }
19331
19332   return "";
19333 }
19334
19335 const char *
19336 thumb_output_move_mem_multiple (int n, rtx *operands)
19337 {
19338   rtx tmp;
19339
19340   switch (n)
19341     {
19342     case 2:
19343       if (REGNO (operands[4]) > REGNO (operands[5]))
19344         {
19345           tmp = operands[4];
19346           operands[4] = operands[5];
19347           operands[5] = tmp;
19348         }
19349       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
19350       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
19351       break;
19352
19353     case 3:
19354       if (REGNO (operands[4]) > REGNO (operands[5]))
19355         {
19356           tmp = operands[4];
19357           operands[4] = operands[5];
19358           operands[5] = tmp;
19359         }
19360       if (REGNO (operands[5]) > REGNO (operands[6]))
19361         {
19362           tmp = operands[5];
19363           operands[5] = operands[6];
19364           operands[6] = tmp;
19365         }
19366       if (REGNO (operands[4]) > REGNO (operands[5]))
19367         {
19368           tmp = operands[4];
19369           operands[4] = operands[5];
19370           operands[5] = tmp;
19371         }
19372
19373       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
19374       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
19375       break;
19376
19377     default:
19378       gcc_unreachable ();
19379     }
19380
19381   return "";
19382 }
19383
19384 /* Output a call-via instruction for thumb state.  */
19385 const char *
19386 thumb_call_via_reg (rtx reg)
19387 {
19388   int regno = REGNO (reg);
19389   rtx *labelp;
19390
19391   gcc_assert (regno < LR_REGNUM);
19392
19393   /* If we are in the normal text section we can use a single instance
19394      per compilation unit.  If we are doing function sections, then we need
19395      an entry per section, since we can't rely on reachability.  */
19396   if (in_section == text_section)
19397     {
19398       thumb_call_reg_needed = 1;
19399
19400       if (thumb_call_via_label[regno] == NULL)
19401         thumb_call_via_label[regno] = gen_label_rtx ();
19402       labelp = thumb_call_via_label + regno;
19403     }
19404   else
19405     {
19406       if (cfun->machine->call_via[regno] == NULL)
19407         cfun->machine->call_via[regno] = gen_label_rtx ();
19408       labelp = cfun->machine->call_via + regno;
19409     }
19410
19411   output_asm_insn ("bl\t%a0", labelp);
19412   return "";
19413 }
19414
19415 /* Routines for generating rtl.  */
19416 void
19417 thumb_expand_movmemqi (rtx *operands)
19418 {
19419   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
19420   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
19421   HOST_WIDE_INT len = INTVAL (operands[2]);
19422   HOST_WIDE_INT offset = 0;
19423
19424   while (len >= 12)
19425     {
19426       emit_insn (gen_movmem12b (out, in, out, in));
19427       len -= 12;
19428     }
19429
19430   if (len >= 8)
19431     {
19432       emit_insn (gen_movmem8b (out, in, out, in));
19433       len -= 8;
19434     }
19435
19436   if (len >= 4)
19437     {
19438       rtx reg = gen_reg_rtx (SImode);
19439       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
19440       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
19441       len -= 4;
19442       offset += 4;
19443     }
19444
19445   if (len >= 2)
19446     {
19447       rtx reg = gen_reg_rtx (HImode);
19448       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
19449                                               plus_constant (in, offset))));
19450       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
19451                             reg));
19452       len -= 2;
19453       offset += 2;
19454     }
19455
19456   if (len)
19457     {
19458       rtx reg = gen_reg_rtx (QImode);
19459       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
19460                                               plus_constant (in, offset))));
19461       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
19462                             reg));
19463     }
19464 }
19465
19466 void
19467 thumb_reload_out_hi (rtx *operands)
19468 {
19469   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
19470 }
19471
19472 /* Handle reading a half-word from memory during reload.  */
19473 void
19474 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
19475 {
19476   gcc_unreachable ();
19477 }
19478
19479 /* Return the length of a function name prefix
19480     that starts with the character 'c'.  */
19481 static int
19482 arm_get_strip_length (int c)
19483 {
19484   switch (c)
19485     {
19486     ARM_NAME_ENCODING_LENGTHS
19487       default: return 0;
19488     }
19489 }
19490
19491 /* Return a pointer to a function's name with any
19492    and all prefix encodings stripped from it.  */
19493 const char *
19494 arm_strip_name_encoding (const char *name)
19495 {
19496   int skip;
19497
19498   while ((skip = arm_get_strip_length (* name)))
19499     name += skip;
19500
19501   return name;
19502 }
19503
19504 /* If there is a '*' anywhere in the name's prefix, then
19505    emit the stripped name verbatim, otherwise prepend an
19506    underscore if leading underscores are being used.  */
19507 void
19508 arm_asm_output_labelref (FILE *stream, const char *name)
19509 {
19510   int skip;
19511   int verbatim = 0;
19512
19513   while ((skip = arm_get_strip_length (* name)))
19514     {
19515       verbatim |= (*name == '*');
19516       name += skip;
19517     }
19518
19519   if (verbatim)
19520     fputs (name, stream);
19521   else
19522     asm_fprintf (stream, "%U%s", name);
19523 }
19524
19525 static void
19526 arm_file_start (void)
19527 {
19528   int val;
19529
19530   if (TARGET_UNIFIED_ASM)
19531     asm_fprintf (asm_out_file, "\t.syntax unified\n");
19532
19533   if (TARGET_BPABI)
19534     {
19535       const char *fpu_name;
19536       if (arm_select[0].string)
19537         asm_fprintf (asm_out_file, "\t.cpu %s\n", arm_select[0].string);
19538       else if (arm_select[1].string)
19539         asm_fprintf (asm_out_file, "\t.arch %s\n", arm_select[1].string);
19540       else
19541         asm_fprintf (asm_out_file, "\t.cpu %s\n",
19542                      all_cores[arm_default_cpu].name);
19543
19544       if (TARGET_SOFT_FLOAT)
19545         {
19546           if (TARGET_VFP)
19547             fpu_name = "softvfp";
19548           else
19549             fpu_name = "softfpa";
19550         }
19551       else
19552         {
19553           int set_float_abi_attributes = 0;
19554           switch (arm_fpu_arch)
19555             {
19556             case FPUTYPE_FPA:
19557               fpu_name = "fpa";
19558               break;
19559             case FPUTYPE_FPA_EMU2:
19560               fpu_name = "fpe2";
19561               break;
19562             case FPUTYPE_FPA_EMU3:
19563               fpu_name = "fpe3";
19564               break;
19565             case FPUTYPE_MAVERICK:
19566               fpu_name = "maverick";
19567               break;
19568             case FPUTYPE_VFP:
19569               fpu_name = "vfp";
19570               set_float_abi_attributes = 1;
19571               break;
19572             case FPUTYPE_VFP3D16:
19573               fpu_name = "vfpv3-d16";
19574               set_float_abi_attributes = 1;
19575               break;
19576             case FPUTYPE_VFP3:
19577               fpu_name = "vfpv3";
19578               set_float_abi_attributes = 1;
19579               break;
19580             case FPUTYPE_NEON:
19581               fpu_name = "neon";
19582               set_float_abi_attributes = 1;
19583               break;
19584             case FPUTYPE_NEON_FP16:
19585               fpu_name = "neon-fp16";
19586               set_float_abi_attributes = 1;
19587               break;
19588             default:
19589               abort();
19590             }
19591           if (set_float_abi_attributes)
19592             {
19593               if (TARGET_HARD_FLOAT)
19594                 asm_fprintf (asm_out_file, "\t.eabi_attribute 27, 3\n");
19595               if (TARGET_HARD_FLOAT_ABI)
19596                 asm_fprintf (asm_out_file, "\t.eabi_attribute 28, 1\n");
19597             }
19598         }
19599       asm_fprintf (asm_out_file, "\t.fpu %s\n", fpu_name);
19600
19601       /* Some of these attributes only apply when the corresponding features
19602          are used.  However we don't have any easy way of figuring this out.
19603          Conservatively record the setting that would have been used.  */
19604
19605       /* Tag_ABI_FP_rounding.  */
19606       if (flag_rounding_math)
19607         asm_fprintf (asm_out_file, "\t.eabi_attribute 19, 1\n");
19608       if (!flag_unsafe_math_optimizations)
19609         {
19610           /* Tag_ABI_FP_denomal.  */
19611           asm_fprintf (asm_out_file, "\t.eabi_attribute 20, 1\n");
19612           /* Tag_ABI_FP_exceptions.  */
19613           asm_fprintf (asm_out_file, "\t.eabi_attribute 21, 1\n");
19614         }
19615       /* Tag_ABI_FP_user_exceptions.  */
19616       if (flag_signaling_nans)
19617         asm_fprintf (asm_out_file, "\t.eabi_attribute 22, 1\n");
19618       /* Tag_ABI_FP_number_model.  */
19619       asm_fprintf (asm_out_file, "\t.eabi_attribute 23, %d\n", 
19620                    flag_finite_math_only ? 1 : 3);
19621
19622       /* Tag_ABI_align8_needed.  */
19623       asm_fprintf (asm_out_file, "\t.eabi_attribute 24, 1\n");
19624       /* Tag_ABI_align8_preserved.  */
19625       asm_fprintf (asm_out_file, "\t.eabi_attribute 25, 1\n");
19626       /* Tag_ABI_enum_size.  */
19627       asm_fprintf (asm_out_file, "\t.eabi_attribute 26, %d\n",
19628                    flag_short_enums ? 1 : 2);
19629
19630       /* Tag_ABI_optimization_goals.  */
19631       if (optimize_size)
19632         val = 4;
19633       else if (optimize >= 2)
19634         val = 2;
19635       else if (optimize)
19636         val = 1;
19637       else
19638         val = 6;
19639       asm_fprintf (asm_out_file, "\t.eabi_attribute 30, %d\n", val);
19640
19641       /* Tag_ABI_FP_16bit_format.  */
19642       if (arm_fp16_format)
19643         asm_fprintf (asm_out_file, "\t.eabi_attribute 38, %d\n",
19644                      (int)arm_fp16_format);
19645
19646       if (arm_lang_output_object_attributes_hook)
19647         arm_lang_output_object_attributes_hook();
19648     }
19649   default_file_start();
19650 }
19651
19652 static void
19653 arm_file_end (void)
19654 {
19655   int regno;
19656
19657   if (NEED_INDICATE_EXEC_STACK)
19658     /* Add .note.GNU-stack.  */
19659     file_end_indicate_exec_stack ();
19660
19661   if (! thumb_call_reg_needed)
19662     return;
19663
19664   switch_to_section (text_section);
19665   asm_fprintf (asm_out_file, "\t.code 16\n");
19666   ASM_OUTPUT_ALIGN (asm_out_file, 1);
19667
19668   for (regno = 0; regno < LR_REGNUM; regno++)
19669     {
19670       rtx label = thumb_call_via_label[regno];
19671
19672       if (label != 0)
19673         {
19674           targetm.asm_out.internal_label (asm_out_file, "L",
19675                                           CODE_LABEL_NUMBER (label));
19676           asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
19677         }
19678     }
19679 }
19680
19681 #ifndef ARM_PE
19682 /* Symbols in the text segment can be accessed without indirecting via the
19683    constant pool; it may take an extra binary operation, but this is still
19684    faster than indirecting via memory.  Don't do this when not optimizing,
19685    since we won't be calculating al of the offsets necessary to do this
19686    simplification.  */
19687
19688 static void
19689 arm_encode_section_info (tree decl, rtx rtl, int first)
19690 {
19691   if (optimize > 0 && TREE_CONSTANT (decl))
19692     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
19693
19694   default_encode_section_info (decl, rtl, first);
19695 }
19696 #endif /* !ARM_PE */
19697
19698 static void
19699 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
19700 {
19701   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
19702       && !strcmp (prefix, "L"))
19703     {
19704       arm_ccfsm_state = 0;
19705       arm_target_insn = NULL;
19706     }
19707   default_internal_label (stream, prefix, labelno);
19708 }
19709
19710 /* Output code to add DELTA to the first argument, and then jump
19711    to FUNCTION.  Used for C++ multiple inheritance.  */
19712 static void
19713 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
19714                      HOST_WIDE_INT delta,
19715                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
19716                      tree function)
19717 {
19718   static int thunk_label = 0;
19719   char label[256];
19720   char labelpc[256];
19721   int mi_delta = delta;
19722   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
19723   int shift = 0;
19724   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
19725                     ? 1 : 0);
19726   if (mi_delta < 0)
19727     mi_delta = - mi_delta;
19728
19729   if (TARGET_THUMB1)
19730     {
19731       int labelno = thunk_label++;
19732       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
19733       /* Thunks are entered in arm mode when avaiable.  */
19734       if (TARGET_THUMB1_ONLY)
19735         {
19736           /* push r3 so we can use it as a temporary.  */
19737           /* TODO: Omit this save if r3 is not used.  */
19738           fputs ("\tpush {r3}\n", file);
19739           fputs ("\tldr\tr3, ", file);
19740         }
19741       else
19742         {
19743           fputs ("\tldr\tr12, ", file);
19744         }
19745       assemble_name (file, label);
19746       fputc ('\n', file);
19747       if (flag_pic)
19748         {
19749           /* If we are generating PIC, the ldr instruction below loads
19750              "(target - 7) - .LTHUNKPCn" into r12.  The pc reads as
19751              the address of the add + 8, so we have:
19752
19753              r12 = (target - 7) - .LTHUNKPCn + (.LTHUNKPCn + 8)
19754                  = target + 1.
19755
19756              Note that we have "+ 1" because some versions of GNU ld
19757              don't set the low bit of the result for R_ARM_REL32
19758              relocations against thumb function symbols.
19759              On ARMv6M this is +4, not +8.  */
19760           ASM_GENERATE_INTERNAL_LABEL (labelpc, "LTHUNKPC", labelno);
19761           assemble_name (file, labelpc);
19762           fputs (":\n", file);
19763           if (TARGET_THUMB1_ONLY)
19764             {
19765               /* This is 2 insns after the start of the thunk, so we know it
19766                  is 4-byte aligned.  */
19767               fputs ("\tadd\tr3, pc, r3\n", file);
19768               fputs ("\tmov r12, r3\n", file);
19769             }
19770           else
19771             fputs ("\tadd\tr12, pc, r12\n", file);
19772         }
19773       else if (TARGET_THUMB1_ONLY)
19774         fputs ("\tmov r12, r3\n", file);
19775     }
19776   if (TARGET_THUMB1_ONLY)
19777     {
19778       if (mi_delta > 255)
19779         {
19780           fputs ("\tldr\tr3, ", file);
19781           assemble_name (file, label);
19782           fputs ("+4\n", file);
19783           asm_fprintf (file, "\t%s\t%r, %r, r3\n",
19784                        mi_op, this_regno, this_regno);
19785         }
19786       else if (mi_delta != 0)
19787         {
19788           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
19789                        mi_op, this_regno, this_regno,
19790                        mi_delta);
19791         }
19792     }
19793   else
19794     {
19795       /* TODO: Use movw/movt for large constants when available.  */
19796       while (mi_delta != 0)
19797         {
19798           if ((mi_delta & (3 << shift)) == 0)
19799             shift += 2;
19800           else
19801             {
19802               asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
19803                            mi_op, this_regno, this_regno,
19804                            mi_delta & (0xff << shift));
19805               mi_delta &= ~(0xff << shift);
19806               shift += 8;
19807             }
19808         }
19809     }
19810   if (TARGET_THUMB1)
19811     {
19812       if (TARGET_THUMB1_ONLY)
19813         fputs ("\tpop\t{r3}\n", file);
19814
19815       fprintf (file, "\tbx\tr12\n");
19816       ASM_OUTPUT_ALIGN (file, 2);
19817       assemble_name (file, label);
19818       fputs (":\n", file);
19819       if (flag_pic)
19820         {
19821           /* Output ".word .LTHUNKn-7-.LTHUNKPCn".  */
19822           rtx tem = XEXP (DECL_RTL (function), 0);
19823           tem = gen_rtx_PLUS (GET_MODE (tem), tem, GEN_INT (-7));
19824           tem = gen_rtx_MINUS (GET_MODE (tem),
19825                                tem,
19826                                gen_rtx_SYMBOL_REF (Pmode,
19827                                                    ggc_strdup (labelpc)));
19828           assemble_integer (tem, 4, BITS_PER_WORD, 1);
19829         }
19830       else
19831         /* Output ".word .LTHUNKn".  */
19832         assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
19833
19834       if (TARGET_THUMB1_ONLY && mi_delta > 255)
19835         assemble_integer (GEN_INT(mi_delta), 4, BITS_PER_WORD, 1);
19836     }
19837   else
19838     {
19839       fputs ("\tb\t", file);
19840       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
19841       if (NEED_PLT_RELOC)
19842         fputs ("(PLT)", file);
19843       fputc ('\n', file);
19844     }
19845 }
19846
19847 int
19848 arm_emit_vector_const (FILE *file, rtx x)
19849 {
19850   int i;
19851   const char * pattern;
19852
19853   gcc_assert (GET_CODE (x) == CONST_VECTOR);
19854
19855   switch (GET_MODE (x))
19856     {
19857     case V2SImode: pattern = "%08x"; break;
19858     case V4HImode: pattern = "%04x"; break;
19859     case V8QImode: pattern = "%02x"; break;
19860     default:       gcc_unreachable ();
19861     }
19862
19863   fprintf (file, "0x");
19864   for (i = CONST_VECTOR_NUNITS (x); i--;)
19865     {
19866       rtx element;
19867
19868       element = CONST_VECTOR_ELT (x, i);
19869       fprintf (file, pattern, INTVAL (element));
19870     }
19871
19872   return 1;
19873 }
19874
19875 /* Emit a fp16 constant appropriately padded to occupy a 4-byte word.
19876    HFmode constant pool entries are actually loaded with ldr.  */
19877 void
19878 arm_emit_fp16_const (rtx c)
19879 {
19880   REAL_VALUE_TYPE r;
19881   long bits;
19882
19883   REAL_VALUE_FROM_CONST_DOUBLE (r, c);
19884   bits = real_to_target (NULL, &r, HFmode);
19885   if (WORDS_BIG_ENDIAN)
19886     assemble_zeros (2);
19887   assemble_integer (GEN_INT (bits), 2, BITS_PER_WORD, 1);
19888   if (!WORDS_BIG_ENDIAN)
19889     assemble_zeros (2);
19890 }
19891
19892 const char *
19893 arm_output_load_gr (rtx *operands)
19894 {
19895   rtx reg;
19896   rtx offset;
19897   rtx wcgr;
19898   rtx sum;
19899
19900   if (GET_CODE (operands [1]) != MEM
19901       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
19902       || GET_CODE (reg = XEXP (sum, 0)) != REG
19903       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
19904       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
19905     return "wldrw%?\t%0, %1";
19906
19907   /* Fix up an out-of-range load of a GR register.  */
19908   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
19909   wcgr = operands[0];
19910   operands[0] = reg;
19911   output_asm_insn ("ldr%?\t%0, %1", operands);
19912
19913   operands[0] = wcgr;
19914   operands[1] = reg;
19915   output_asm_insn ("tmcr%?\t%0, %1", operands);
19916   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
19917
19918   return "";
19919 }
19920
19921 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
19922
19923    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
19924    named arg and all anonymous args onto the stack.
19925    XXX I know the prologue shouldn't be pushing registers, but it is faster
19926    that way.  */
19927
19928 static void
19929 arm_setup_incoming_varargs (CUMULATIVE_ARGS *pcum,
19930                             enum machine_mode mode,
19931                             tree type,
19932                             int *pretend_size,
19933                             int second_time ATTRIBUTE_UNUSED)
19934 {
19935   int nregs;
19936   
19937   cfun->machine->uses_anonymous_args = 1;
19938   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
19939     {
19940       nregs = pcum->aapcs_ncrn;
19941       if ((nregs & 1) && arm_needs_doubleword_align (mode, type))
19942         nregs++;
19943     }
19944   else
19945     nregs = pcum->nregs;
19946   
19947   if (nregs < NUM_ARG_REGS)
19948     *pretend_size = (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
19949 }
19950
19951 /* Return nonzero if the CONSUMER instruction (a store) does not need
19952    PRODUCER's value to calculate the address.  */
19953
19954 int
19955 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
19956 {
19957   rtx value = PATTERN (producer);
19958   rtx addr = PATTERN (consumer);
19959
19960   if (GET_CODE (value) == COND_EXEC)
19961     value = COND_EXEC_CODE (value);
19962   if (GET_CODE (value) == PARALLEL)
19963     value = XVECEXP (value, 0, 0);
19964   value = XEXP (value, 0);
19965   if (GET_CODE (addr) == COND_EXEC)
19966     addr = COND_EXEC_CODE (addr);
19967   if (GET_CODE (addr) == PARALLEL)
19968     addr = XVECEXP (addr, 0, 0);
19969   addr = XEXP (addr, 0);
19970
19971   return !reg_overlap_mentioned_p (value, addr);
19972 }
19973
19974 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
19975    have an early register shift value or amount dependency on the
19976    result of PRODUCER.  */
19977
19978 int
19979 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
19980 {
19981   rtx value = PATTERN (producer);
19982   rtx op = PATTERN (consumer);
19983   rtx early_op;
19984
19985   if (GET_CODE (value) == COND_EXEC)
19986     value = COND_EXEC_CODE (value);
19987   if (GET_CODE (value) == PARALLEL)
19988     value = XVECEXP (value, 0, 0);
19989   value = XEXP (value, 0);
19990   if (GET_CODE (op) == COND_EXEC)
19991     op = COND_EXEC_CODE (op);
19992   if (GET_CODE (op) == PARALLEL)
19993     op = XVECEXP (op, 0, 0);
19994   op = XEXP (op, 1);
19995
19996   early_op = XEXP (op, 0);
19997   /* This is either an actual independent shift, or a shift applied to
19998      the first operand of another operation.  We want the whole shift
19999      operation.  */
20000   if (GET_CODE (early_op) == REG)
20001     early_op = op;
20002
20003   return !reg_overlap_mentioned_p (value, early_op);
20004 }
20005
20006 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
20007    have an early register shift value dependency on the result of
20008    PRODUCER.  */
20009
20010 int
20011 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
20012 {
20013   rtx value = PATTERN (producer);
20014   rtx op = PATTERN (consumer);
20015   rtx early_op;
20016
20017   if (GET_CODE (value) == COND_EXEC)
20018     value = COND_EXEC_CODE (value);
20019   if (GET_CODE (value) == PARALLEL)
20020     value = XVECEXP (value, 0, 0);
20021   value = XEXP (value, 0);
20022   if (GET_CODE (op) == COND_EXEC)
20023     op = COND_EXEC_CODE (op);
20024   if (GET_CODE (op) == PARALLEL)
20025     op = XVECEXP (op, 0, 0);
20026   op = XEXP (op, 1);
20027
20028   early_op = XEXP (op, 0);
20029
20030   /* This is either an actual independent shift, or a shift applied to
20031      the first operand of another operation.  We want the value being
20032      shifted, in either case.  */
20033   if (GET_CODE (early_op) != REG)
20034     early_op = XEXP (early_op, 0);
20035
20036   return !reg_overlap_mentioned_p (value, early_op);
20037 }
20038
20039 /* Return nonzero if the CONSUMER (a mul or mac op) does not
20040    have an early register mult dependency on the result of
20041    PRODUCER.  */
20042
20043 int
20044 arm_no_early_mul_dep (rtx producer, rtx consumer)
20045 {
20046   rtx value = PATTERN (producer);
20047   rtx op = PATTERN (consumer);
20048
20049   if (GET_CODE (value) == COND_EXEC)
20050     value = COND_EXEC_CODE (value);
20051   if (GET_CODE (value) == PARALLEL)
20052     value = XVECEXP (value, 0, 0);
20053   value = XEXP (value, 0);
20054   if (GET_CODE (op) == COND_EXEC)
20055     op = COND_EXEC_CODE (op);
20056   if (GET_CODE (op) == PARALLEL)
20057     op = XVECEXP (op, 0, 0);
20058   op = XEXP (op, 1);
20059
20060   if (GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
20061     {
20062       if (GET_CODE (XEXP (op, 0)) == MULT)
20063         return !reg_overlap_mentioned_p (value, XEXP (op, 0));
20064       else
20065         return !reg_overlap_mentioned_p (value, XEXP (op, 1));
20066     }
20067
20068   return 0;
20069 }
20070
20071 /* We can't rely on the caller doing the proper promotion when
20072    using APCS or ATPCS.  */
20073
20074 static bool
20075 arm_promote_prototypes (const_tree t ATTRIBUTE_UNUSED)
20076 {
20077     return !TARGET_AAPCS_BASED;
20078 }
20079
20080 static enum machine_mode
20081 arm_promote_function_mode (const_tree type ATTRIBUTE_UNUSED,
20082                            enum machine_mode mode,
20083                            int *punsignedp ATTRIBUTE_UNUSED,
20084                            const_tree fntype ATTRIBUTE_UNUSED,
20085                            int for_return ATTRIBUTE_UNUSED)
20086 {
20087   if (GET_MODE_CLASS (mode) == MODE_INT
20088       && GET_MODE_SIZE (mode) < 4)
20089     return SImode;
20090
20091   return mode;
20092 }
20093
20094 /* AAPCS based ABIs use short enums by default.  */
20095
20096 static bool
20097 arm_default_short_enums (void)
20098 {
20099   return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
20100 }
20101
20102
20103 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
20104
20105 static bool
20106 arm_align_anon_bitfield (void)
20107 {
20108   return TARGET_AAPCS_BASED;
20109 }
20110
20111
20112 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
20113
20114 static tree
20115 arm_cxx_guard_type (void)
20116 {
20117   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
20118 }
20119
20120 /* Return non-zero if the consumer (a multiply-accumulate instruction)
20121    has an accumulator dependency on the result of the producer (a
20122    multiplication instruction) and no other dependency on that result.  */
20123 int
20124 arm_mac_accumulator_is_mul_result (rtx producer, rtx consumer)
20125 {
20126   rtx mul = PATTERN (producer);
20127   rtx mac = PATTERN (consumer);
20128   rtx mul_result;
20129   rtx mac_op0, mac_op1, mac_acc;
20130
20131   if (GET_CODE (mul) == COND_EXEC)
20132     mul = COND_EXEC_CODE (mul);
20133   if (GET_CODE (mac) == COND_EXEC)
20134     mac = COND_EXEC_CODE (mac);
20135
20136   /* Check that mul is of the form (set (...) (mult ...))
20137      and mla is of the form (set (...) (plus (mult ...) (...))).  */
20138   if ((GET_CODE (mul) != SET || GET_CODE (XEXP (mul, 1)) != MULT)
20139       || (GET_CODE (mac) != SET || GET_CODE (XEXP (mac, 1)) != PLUS
20140           || GET_CODE (XEXP (XEXP (mac, 1), 0)) != MULT))
20141     return 0;
20142
20143   mul_result = XEXP (mul, 0);
20144   mac_op0 = XEXP (XEXP (XEXP (mac, 1), 0), 0);
20145   mac_op1 = XEXP (XEXP (XEXP (mac, 1), 0), 1);
20146   mac_acc = XEXP (XEXP (mac, 1), 1);
20147
20148   return (reg_overlap_mentioned_p (mul_result, mac_acc)
20149           && !reg_overlap_mentioned_p (mul_result, mac_op0)
20150           && !reg_overlap_mentioned_p (mul_result, mac_op1));
20151 }
20152
20153
20154 /* The EABI says test the least significant bit of a guard variable.  */
20155
20156 static bool
20157 arm_cxx_guard_mask_bit (void)
20158 {
20159   return TARGET_AAPCS_BASED;
20160 }
20161
20162
20163 /* The EABI specifies that all array cookies are 8 bytes long.  */
20164
20165 static tree
20166 arm_get_cookie_size (tree type)
20167 {
20168   tree size;
20169
20170   if (!TARGET_AAPCS_BASED)
20171     return default_cxx_get_cookie_size (type);
20172
20173   size = build_int_cst (sizetype, 8);
20174   return size;
20175 }
20176
20177
20178 /* The EABI says that array cookies should also contain the element size.  */
20179
20180 static bool
20181 arm_cookie_has_size (void)
20182 {
20183   return TARGET_AAPCS_BASED;
20184 }
20185
20186
20187 /* The EABI says constructors and destructors should return a pointer to
20188    the object constructed/destroyed.  */
20189
20190 static bool
20191 arm_cxx_cdtor_returns_this (void)
20192 {
20193   return TARGET_AAPCS_BASED;
20194 }
20195
20196 /* The EABI says that an inline function may never be the key
20197    method.  */
20198
20199 static bool
20200 arm_cxx_key_method_may_be_inline (void)
20201 {
20202   return !TARGET_AAPCS_BASED;
20203 }
20204
20205 static void
20206 arm_cxx_determine_class_data_visibility (tree decl)
20207 {
20208   if (!TARGET_AAPCS_BASED
20209       || !TARGET_DLLIMPORT_DECL_ATTRIBUTES)
20210     return;
20211
20212   /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
20213      is exported.  However, on systems without dynamic vague linkage,
20214      \S 3.2.5.6 says that COMDAT class data has hidden linkage.  */
20215   if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
20216     DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
20217   else
20218     DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
20219   DECL_VISIBILITY_SPECIFIED (decl) = 1;
20220 }
20221
20222 static bool
20223 arm_cxx_class_data_always_comdat (void)
20224 {
20225   /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
20226      vague linkage if the class has no key function.  */
20227   return !TARGET_AAPCS_BASED;
20228 }
20229
20230
20231 /* The EABI says __aeabi_atexit should be used to register static
20232    destructors.  */
20233
20234 static bool
20235 arm_cxx_use_aeabi_atexit (void)
20236 {
20237   return TARGET_AAPCS_BASED;
20238 }
20239
20240
20241 void
20242 arm_set_return_address (rtx source, rtx scratch)
20243 {
20244   arm_stack_offsets *offsets;
20245   HOST_WIDE_INT delta;
20246   rtx addr;
20247   unsigned long saved_regs;
20248
20249   offsets = arm_get_frame_offsets ();
20250   saved_regs = offsets->saved_regs_mask;
20251
20252   if ((saved_regs & (1 << LR_REGNUM)) == 0)
20253     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
20254   else
20255     {
20256       if (frame_pointer_needed)
20257         addr = plus_constant(hard_frame_pointer_rtx, -4);
20258       else
20259         {
20260           /* LR will be the first saved register.  */
20261           delta = offsets->outgoing_args - (offsets->frame + 4);
20262
20263
20264           if (delta >= 4096)
20265             {
20266               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
20267                                      GEN_INT (delta & ~4095)));
20268               addr = scratch;
20269               delta &= 4095;
20270             }
20271           else
20272             addr = stack_pointer_rtx;
20273
20274           addr = plus_constant (addr, delta);
20275         }
20276       emit_move_insn (gen_frame_mem (Pmode, addr), source);
20277     }
20278 }
20279
20280
20281 void
20282 thumb_set_return_address (rtx source, rtx scratch)
20283 {
20284   arm_stack_offsets *offsets;
20285   HOST_WIDE_INT delta;
20286   HOST_WIDE_INT limit;
20287   int reg;
20288   rtx addr;
20289   unsigned long mask;
20290
20291   emit_use (source);
20292
20293   offsets = arm_get_frame_offsets ();
20294   mask = offsets->saved_regs_mask;
20295   if (mask & (1 << LR_REGNUM))
20296     {
20297       limit = 1024;
20298       /* Find the saved regs.  */
20299       if (frame_pointer_needed)
20300         {
20301           delta = offsets->soft_frame - offsets->saved_args;
20302           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
20303           if (TARGET_THUMB1)
20304             limit = 128;
20305         }
20306       else
20307         {
20308           delta = offsets->outgoing_args - offsets->saved_args;
20309           reg = SP_REGNUM;
20310         }
20311       /* Allow for the stack frame.  */
20312       if (TARGET_THUMB1 && TARGET_BACKTRACE)
20313         delta -= 16;
20314       /* The link register is always the first saved register.  */
20315       delta -= 4;
20316
20317       /* Construct the address.  */
20318       addr = gen_rtx_REG (SImode, reg);
20319       if (delta > limit)
20320         {
20321           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
20322           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
20323           addr = scratch;
20324         }
20325       else
20326         addr = plus_constant (addr, delta);
20327
20328       emit_move_insn (gen_frame_mem (Pmode, addr), source);
20329     }
20330   else
20331     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
20332 }
20333
20334 /* Implements target hook vector_mode_supported_p.  */
20335 bool
20336 arm_vector_mode_supported_p (enum machine_mode mode)
20337 {
20338   /* Neon also supports V2SImode, etc. listed in the clause below.  */
20339   if (TARGET_NEON && (mode == V2SFmode || mode == V4SImode || mode == V8HImode
20340       || mode == V16QImode || mode == V4SFmode || mode == V2DImode))
20341     return true;
20342
20343   if ((TARGET_NEON || TARGET_IWMMXT)
20344       && ((mode == V2SImode)
20345           || (mode == V4HImode)
20346           || (mode == V8QImode)))
20347     return true;
20348
20349   return false;
20350 }
20351
20352 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
20353    ARM insns and therefore guarantee that the shift count is modulo 256.
20354    DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
20355    guarantee no particular behavior for out-of-range counts.  */
20356
20357 static unsigned HOST_WIDE_INT
20358 arm_shift_truncation_mask (enum machine_mode mode)
20359 {
20360   return mode == SImode ? 255 : 0;
20361 }
20362
20363
20364 /* Map internal gcc register numbers to DWARF2 register numbers.  */
20365
20366 unsigned int
20367 arm_dbx_register_number (unsigned int regno)
20368 {
20369   if (regno < 16)
20370     return regno;
20371
20372   /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
20373      compatibility.  The EABI defines them as registers 96-103.  */
20374   if (IS_FPA_REGNUM (regno))
20375     return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
20376
20377   /* FIXME: VFPv3 register numbering.  */
20378   if (IS_VFP_REGNUM (regno))
20379     return 64 + regno - FIRST_VFP_REGNUM;
20380
20381   if (IS_IWMMXT_GR_REGNUM (regno))
20382     return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
20383
20384   if (IS_IWMMXT_REGNUM (regno))
20385     return 112 + regno - FIRST_IWMMXT_REGNUM;
20386
20387   gcc_unreachable ();
20388 }
20389
20390
20391 #ifdef TARGET_UNWIND_INFO
20392 /* Emit unwind directives for a store-multiple instruction or stack pointer
20393    push during alignment.
20394    These should only ever be generated by the function prologue code, so
20395    expect them to have a particular form.  */
20396
20397 static void
20398 arm_unwind_emit_sequence (FILE * asm_out_file, rtx p)
20399 {
20400   int i;
20401   HOST_WIDE_INT offset;
20402   HOST_WIDE_INT nregs;
20403   int reg_size;
20404   unsigned reg;
20405   unsigned lastreg;
20406   rtx e;
20407
20408   e = XVECEXP (p, 0, 0);
20409   if (GET_CODE (e) != SET)
20410     abort ();
20411
20412   /* First insn will adjust the stack pointer.  */
20413   if (GET_CODE (e) != SET
20414       || GET_CODE (XEXP (e, 0)) != REG
20415       || REGNO (XEXP (e, 0)) != SP_REGNUM
20416       || GET_CODE (XEXP (e, 1)) != PLUS)
20417     abort ();
20418
20419   offset = -INTVAL (XEXP (XEXP (e, 1), 1));
20420   nregs = XVECLEN (p, 0) - 1;
20421
20422   reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
20423   if (reg < 16)
20424     {
20425       /* The function prologue may also push pc, but not annotate it as it is
20426          never restored.  We turn this into a stack pointer adjustment.  */
20427       if (nregs * 4 == offset - 4)
20428         {
20429           fprintf (asm_out_file, "\t.pad #4\n");
20430           offset -= 4;
20431         }
20432       reg_size = 4;
20433       fprintf (asm_out_file, "\t.save {");
20434     }
20435   else if (IS_VFP_REGNUM (reg))
20436     {
20437       reg_size = 8;
20438       fprintf (asm_out_file, "\t.vsave {");
20439     }
20440   else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
20441     {
20442       /* FPA registers are done differently.  */
20443       asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
20444       return;
20445     }
20446   else
20447     /* Unknown register type.  */
20448     abort ();
20449
20450   /* If the stack increment doesn't match the size of the saved registers,
20451      something has gone horribly wrong.  */
20452   if (offset != nregs * reg_size)
20453     abort ();
20454
20455   offset = 0;
20456   lastreg = 0;
20457   /* The remaining insns will describe the stores.  */
20458   for (i = 1; i <= nregs; i++)
20459     {
20460       /* Expect (set (mem <addr>) (reg)).
20461          Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)).  */
20462       e = XVECEXP (p, 0, i);
20463       if (GET_CODE (e) != SET
20464           || GET_CODE (XEXP (e, 0)) != MEM
20465           || GET_CODE (XEXP (e, 1)) != REG)
20466         abort ();
20467
20468       reg = REGNO (XEXP (e, 1));
20469       if (reg < lastreg)
20470         abort ();
20471
20472       if (i != 1)
20473         fprintf (asm_out_file, ", ");
20474       /* We can't use %r for vfp because we need to use the
20475          double precision register names.  */
20476       if (IS_VFP_REGNUM (reg))
20477         asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
20478       else
20479         asm_fprintf (asm_out_file, "%r", reg);
20480
20481 #ifdef ENABLE_CHECKING
20482       /* Check that the addresses are consecutive.  */
20483       e = XEXP (XEXP (e, 0), 0);
20484       if (GET_CODE (e) == PLUS)
20485         {
20486           offset += reg_size;
20487           if (GET_CODE (XEXP (e, 0)) != REG
20488               || REGNO (XEXP (e, 0)) != SP_REGNUM
20489               || GET_CODE (XEXP (e, 1)) != CONST_INT
20490               || offset != INTVAL (XEXP (e, 1)))
20491             abort ();
20492         }
20493       else if (i != 1
20494                || GET_CODE (e) != REG
20495                || REGNO (e) != SP_REGNUM)
20496         abort ();
20497 #endif
20498     }
20499   fprintf (asm_out_file, "}\n");
20500 }
20501
20502 /*  Emit unwind directives for a SET.  */
20503
20504 static void
20505 arm_unwind_emit_set (FILE * asm_out_file, rtx p)
20506 {
20507   rtx e0;
20508   rtx e1;
20509   unsigned reg;
20510
20511   e0 = XEXP (p, 0);
20512   e1 = XEXP (p, 1);
20513   switch (GET_CODE (e0))
20514     {
20515     case MEM:
20516       /* Pushing a single register.  */
20517       if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
20518           || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
20519           || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
20520         abort ();
20521
20522       asm_fprintf (asm_out_file, "\t.save ");
20523       if (IS_VFP_REGNUM (REGNO (e1)))
20524         asm_fprintf(asm_out_file, "{d%d}\n",
20525                     (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
20526       else
20527         asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
20528       break;
20529
20530     case REG:
20531       if (REGNO (e0) == SP_REGNUM)
20532         {
20533           /* A stack increment.  */
20534           if (GET_CODE (e1) != PLUS
20535               || GET_CODE (XEXP (e1, 0)) != REG
20536               || REGNO (XEXP (e1, 0)) != SP_REGNUM
20537               || GET_CODE (XEXP (e1, 1)) != CONST_INT)
20538             abort ();
20539
20540           asm_fprintf (asm_out_file, "\t.pad #%wd\n",
20541                        -INTVAL (XEXP (e1, 1)));
20542         }
20543       else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
20544         {
20545           HOST_WIDE_INT offset;
20546
20547           if (GET_CODE (e1) == PLUS)
20548             {
20549               if (GET_CODE (XEXP (e1, 0)) != REG
20550                   || GET_CODE (XEXP (e1, 1)) != CONST_INT)
20551                 abort ();
20552               reg = REGNO (XEXP (e1, 0));
20553               offset = INTVAL (XEXP (e1, 1));
20554               asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
20555                            HARD_FRAME_POINTER_REGNUM, reg,
20556                            INTVAL (XEXP (e1, 1)));
20557             }
20558           else if (GET_CODE (e1) == REG)
20559             {
20560               reg = REGNO (e1);
20561               asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
20562                            HARD_FRAME_POINTER_REGNUM, reg);
20563             }
20564           else
20565             abort ();
20566         }
20567       else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
20568         {
20569           /* Move from sp to reg.  */
20570           asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
20571         }
20572      else if (GET_CODE (e1) == PLUS
20573               && GET_CODE (XEXP (e1, 0)) == REG
20574               && REGNO (XEXP (e1, 0)) == SP_REGNUM
20575               && GET_CODE (XEXP (e1, 1)) == CONST_INT)
20576         {
20577           /* Set reg to offset from sp.  */
20578           asm_fprintf (asm_out_file, "\t.movsp %r, #%d\n",
20579                        REGNO (e0), (int)INTVAL(XEXP (e1, 1)));
20580         }
20581       else if (GET_CODE (e1) == UNSPEC && XINT (e1, 1) == UNSPEC_STACK_ALIGN)
20582         {
20583           /* Stack pointer save before alignment.  */
20584           reg = REGNO (e0);
20585           asm_fprintf (asm_out_file, "\t.unwind_raw 0, 0x%x @ vsp = r%d\n",
20586                        reg + 0x90, reg);
20587         }
20588       else
20589         abort ();
20590       break;
20591
20592     default:
20593       abort ();
20594     }
20595 }
20596
20597
20598 /* Emit unwind directives for the given insn.  */
20599
20600 static void
20601 arm_unwind_emit (FILE * asm_out_file, rtx insn)
20602 {
20603   rtx pat;
20604
20605   if (!ARM_EABI_UNWIND_TABLES)
20606     return;
20607
20608   if (!(flag_unwind_tables || crtl->uses_eh_lsda)
20609       && (TREE_NOTHROW (current_function_decl)
20610           || crtl->all_throwers_are_sibcalls))
20611     return;
20612
20613   if (GET_CODE (insn) == NOTE || !RTX_FRAME_RELATED_P (insn))
20614     return;
20615
20616   pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
20617   if (pat)
20618     pat = XEXP (pat, 0);
20619   else
20620     pat = PATTERN (insn);
20621
20622   switch (GET_CODE (pat))
20623     {
20624     case SET:
20625       arm_unwind_emit_set (asm_out_file, pat);
20626       break;
20627
20628     case SEQUENCE:
20629       /* Store multiple.  */
20630       arm_unwind_emit_sequence (asm_out_file, pat);
20631       break;
20632
20633     default:
20634       abort();
20635     }
20636 }
20637
20638
20639 /* Output a reference from a function exception table to the type_info
20640    object X.  The EABI specifies that the symbol should be relocated by
20641    an R_ARM_TARGET2 relocation.  */
20642
20643 static bool
20644 arm_output_ttype (rtx x)
20645 {
20646   fputs ("\t.word\t", asm_out_file);
20647   output_addr_const (asm_out_file, x);
20648   /* Use special relocations for symbol references.  */
20649   if (GET_CODE (x) != CONST_INT)
20650     fputs ("(TARGET2)", asm_out_file);
20651   fputc ('\n', asm_out_file);
20652
20653   return TRUE;
20654 }
20655 #endif /* TARGET_UNWIND_INFO */
20656
20657
20658 /* Handle UNSPEC DWARF call frame instructions.  These are needed for dynamic
20659    stack alignment.  */
20660
20661 static void
20662 arm_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
20663 {
20664   rtx unspec = SET_SRC (pattern);
20665   gcc_assert (GET_CODE (unspec) == UNSPEC);
20666
20667   switch (index)
20668     {
20669     case UNSPEC_STACK_ALIGN:
20670       /* ??? We should set the CFA = (SP & ~7).  At this point we haven't
20671          put anything on the stack, so hopefully it won't matter.
20672          CFA = SP will be correct after alignment.  */
20673       dwarf2out_reg_save_reg (label, stack_pointer_rtx,
20674                               SET_DEST (pattern));
20675       break;
20676     default:
20677       gcc_unreachable ();
20678     }
20679 }
20680
20681
20682 /* Output unwind directives for the start/end of a function.  */
20683
20684 void
20685 arm_output_fn_unwind (FILE * f, bool prologue)
20686 {
20687   if (!ARM_EABI_UNWIND_TABLES)
20688     return;
20689
20690   if (prologue)
20691     fputs ("\t.fnstart\n", f);
20692   else
20693     {
20694       /* If this function will never be unwound, then mark it as such.
20695          The came condition is used in arm_unwind_emit to suppress
20696          the frame annotations.  */
20697       if (!(flag_unwind_tables || crtl->uses_eh_lsda)
20698           && (TREE_NOTHROW (current_function_decl)
20699               || crtl->all_throwers_are_sibcalls))
20700         fputs("\t.cantunwind\n", f);
20701
20702       fputs ("\t.fnend\n", f);
20703     }
20704 }
20705
20706 static bool
20707 arm_emit_tls_decoration (FILE *fp, rtx x)
20708 {
20709   enum tls_reloc reloc;
20710   rtx val;
20711
20712   val = XVECEXP (x, 0, 0);
20713   reloc = (enum tls_reloc) INTVAL (XVECEXP (x, 0, 1));
20714
20715   output_addr_const (fp, val);
20716
20717   switch (reloc)
20718     {
20719     case TLS_GD32:
20720       fputs ("(tlsgd)", fp);
20721       break;
20722     case TLS_LDM32:
20723       fputs ("(tlsldm)", fp);
20724       break;
20725     case TLS_LDO32:
20726       fputs ("(tlsldo)", fp);
20727       break;
20728     case TLS_IE32:
20729       fputs ("(gottpoff)", fp);
20730       break;
20731     case TLS_LE32:
20732       fputs ("(tpoff)", fp);
20733       break;
20734     default:
20735       gcc_unreachable ();
20736     }
20737
20738   switch (reloc)
20739     {
20740     case TLS_GD32:
20741     case TLS_LDM32:
20742     case TLS_IE32:
20743       fputs (" + (. - ", fp);
20744       output_addr_const (fp, XVECEXP (x, 0, 2));
20745       fputs (" - ", fp);
20746       output_addr_const (fp, XVECEXP (x, 0, 3));
20747       fputc (')', fp);
20748       break;
20749     default:
20750       break;
20751     }
20752
20753   return TRUE;
20754 }
20755
20756 /* ARM implementation of TARGET_ASM_OUTPUT_DWARF_DTPREL.  */
20757
20758 static void
20759 arm_output_dwarf_dtprel (FILE *file, int size, rtx x)
20760 {
20761   gcc_assert (size == 4);
20762   fputs ("\t.word\t", file);
20763   output_addr_const (file, x);
20764   fputs ("(tlsldo)", file);
20765 }
20766
20767 bool
20768 arm_output_addr_const_extra (FILE *fp, rtx x)
20769 {
20770   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
20771     return arm_emit_tls_decoration (fp, x);
20772   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
20773     {
20774       char label[256];
20775       int labelno = INTVAL (XVECEXP (x, 0, 0));
20776
20777       ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
20778       assemble_name_raw (fp, label);
20779
20780       return TRUE;
20781     }
20782   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_GOTSYM_OFF)
20783     {
20784       assemble_name (fp, "_GLOBAL_OFFSET_TABLE_");
20785       if (GOT_PCREL)
20786         fputs ("+.", fp);
20787       fputs ("-(", fp);
20788       output_addr_const (fp, XVECEXP (x, 0, 0));
20789       fputc (')', fp);
20790       return TRUE;
20791     }
20792   else if (GET_CODE (x) == CONST_VECTOR)
20793     return arm_emit_vector_const (fp, x);
20794
20795   return FALSE;
20796 }
20797
20798 /* Output assembly for a shift instruction.
20799    SET_FLAGS determines how the instruction modifies the condition codes.
20800    0 - Do not set condition codes.
20801    1 - Set condition codes.
20802    2 - Use smallest instruction.  */
20803 const char *
20804 arm_output_shift(rtx * operands, int set_flags)
20805 {
20806   char pattern[100];
20807   static const char flag_chars[3] = {'?', '.', '!'};
20808   const char *shift;
20809   HOST_WIDE_INT val;
20810   char c;
20811   
20812   c = flag_chars[set_flags];
20813   if (TARGET_UNIFIED_ASM)
20814     {
20815       shift = shift_op(operands[3], &val);
20816       if (shift)
20817         {
20818           if (val != -1)
20819             operands[2] = GEN_INT(val);
20820           sprintf (pattern, "%s%%%c\t%%0, %%1, %%2", shift, c);
20821         }
20822       else
20823         sprintf (pattern, "mov%%%c\t%%0, %%1", c);
20824     }
20825   else
20826     sprintf (pattern, "mov%%%c\t%%0, %%1%%S3", c);
20827   output_asm_insn (pattern, operands);
20828   return "";
20829 }
20830
20831 /* Output a Thumb-1 casesi dispatch sequence.  */
20832 const char *
20833 thumb1_output_casesi (rtx *operands)
20834 {
20835   rtx diff_vec = PATTERN (next_real_insn (operands[0]));
20836   addr_diff_vec_flags flags;
20837
20838   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
20839
20840   flags = ADDR_DIFF_VEC_FLAGS (diff_vec);
20841
20842   switch (GET_MODE(diff_vec))
20843     {
20844     case QImode:
20845       return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned ? 
20846               "bl\t%___gnu_thumb1_case_uqi" : "bl\t%___gnu_thumb1_case_sqi");
20847     case HImode:
20848       return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned ? 
20849               "bl\t%___gnu_thumb1_case_uhi" : "bl\t%___gnu_thumb1_case_shi");
20850     case SImode:
20851       return "bl\t%___gnu_thumb1_case_si";
20852     default:
20853       gcc_unreachable ();
20854     }
20855 }
20856
20857 /* Output a Thumb-2 casesi instruction.  */
20858 const char *
20859 thumb2_output_casesi (rtx *operands)
20860 {
20861   rtx diff_vec = PATTERN (next_real_insn (operands[2]));
20862
20863   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
20864
20865   output_asm_insn ("cmp\t%0, %1", operands);
20866   output_asm_insn ("bhi\t%l3", operands);
20867   switch (GET_MODE(diff_vec))
20868     {
20869     case QImode:
20870       return "tbb\t[%|pc, %0]";
20871     case HImode:
20872       return "tbh\t[%|pc, %0, lsl #1]";
20873     case SImode:
20874       if (flag_pic)
20875         {
20876           output_asm_insn ("adr\t%4, %l2", operands);
20877           output_asm_insn ("ldr\t%5, [%4, %0, lsl #2]", operands);
20878           output_asm_insn ("add\t%4, %4, %5", operands);
20879           return "bx\t%4";
20880         }
20881       else
20882         {
20883           output_asm_insn ("adr\t%4, %l2", operands);
20884           return "ldr\t%|pc, [%4, %0, lsl #2]";
20885         }
20886     default:
20887       gcc_unreachable ();
20888     }
20889 }
20890
20891 /* Most ARM cores are single issue, but some newer ones can dual issue.
20892    The scheduler descriptions rely on this being correct.  */
20893 static int
20894 arm_issue_rate (void)
20895 {
20896   switch (arm_tune)
20897     {
20898     case cortexr4:
20899     case cortexr4f:
20900     case cortexa8:
20901     case cortexa9:
20902       return 2;
20903
20904     default:
20905       return 1;
20906     }
20907 }
20908
20909 /* A table and a function to perform ARM-specific name mangling for
20910    NEON vector types in order to conform to the AAPCS (see "Procedure
20911    Call Standard for the ARM Architecture", Appendix A).  To qualify
20912    for emission with the mangled names defined in that document, a
20913    vector type must not only be of the correct mode but also be
20914    composed of NEON vector element types (e.g. __builtin_neon_qi).  */
20915 typedef struct
20916 {
20917   enum machine_mode mode;
20918   const char *element_type_name;
20919   const char *aapcs_name;
20920 } arm_mangle_map_entry;
20921
20922 static arm_mangle_map_entry arm_mangle_map[] = {
20923   /* 64-bit containerized types.  */
20924   { V8QImode,  "__builtin_neon_qi",     "15__simd64_int8_t" },
20925   { V8QImode,  "__builtin_neon_uqi",    "16__simd64_uint8_t" },
20926   { V4HImode,  "__builtin_neon_hi",     "16__simd64_int16_t" },
20927   { V4HImode,  "__builtin_neon_uhi",    "17__simd64_uint16_t" },
20928   { V2SImode,  "__builtin_neon_si",     "16__simd64_int32_t" },
20929   { V2SImode,  "__builtin_neon_usi",    "17__simd64_uint32_t" },
20930   { V2SFmode,  "__builtin_neon_sf",     "18__simd64_float32_t" },
20931   { V8QImode,  "__builtin_neon_poly8",  "16__simd64_poly8_t" },
20932   { V4HImode,  "__builtin_neon_poly16", "17__simd64_poly16_t" },
20933   /* 128-bit containerized types.  */
20934   { V16QImode, "__builtin_neon_qi",     "16__simd128_int8_t" },
20935   { V16QImode, "__builtin_neon_uqi",    "17__simd128_uint8_t" },
20936   { V8HImode,  "__builtin_neon_hi",     "17__simd128_int16_t" },
20937   { V8HImode,  "__builtin_neon_uhi",    "18__simd128_uint16_t" },
20938   { V4SImode,  "__builtin_neon_si",     "17__simd128_int32_t" },
20939   { V4SImode,  "__builtin_neon_usi",    "18__simd128_uint32_t" },
20940   { V4SFmode,  "__builtin_neon_sf",     "19__simd128_float32_t" },
20941   { V16QImode, "__builtin_neon_poly8",  "17__simd128_poly8_t" },
20942   { V8HImode,  "__builtin_neon_poly16", "18__simd128_poly16_t" },
20943   { VOIDmode, NULL, NULL }
20944 };
20945
20946 const char *
20947 arm_mangle_type (const_tree type)
20948 {
20949   arm_mangle_map_entry *pos = arm_mangle_map;
20950
20951   /* The ARM ABI documents (10th October 2008) say that "__va_list"
20952      has to be managled as if it is in the "std" namespace.  */
20953   if (TARGET_AAPCS_BASED 
20954       && lang_hooks.types_compatible_p (CONST_CAST_TREE (type), va_list_type))
20955     {
20956       static bool warned;
20957       if (!warned && warn_psabi)
20958         {
20959           warned = true;
20960           inform (input_location,
20961                   "the mangling of %<va_list%> has changed in GCC 4.4");
20962         }
20963       return "St9__va_list";
20964     }
20965
20966   /* Half-precision float.  */
20967   if (TREE_CODE (type) == REAL_TYPE && TYPE_PRECISION (type) == 16)
20968     return "Dh";
20969
20970   if (TREE_CODE (type) != VECTOR_TYPE)
20971     return NULL;
20972
20973   /* Check the mode of the vector type, and the name of the vector
20974      element type, against the table.  */
20975   while (pos->mode != VOIDmode)
20976     {
20977       tree elt_type = TREE_TYPE (type);
20978
20979       if (pos->mode == TYPE_MODE (type)
20980           && TREE_CODE (TYPE_NAME (elt_type)) == TYPE_DECL
20981           && !strcmp (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (elt_type))),
20982                       pos->element_type_name))
20983         return pos->aapcs_name;
20984
20985       pos++;
20986     }
20987
20988   /* Use the default mangling for unrecognized (possibly user-defined)
20989      vector types.  */
20990   return NULL;
20991 }
20992
20993 /* Order of allocation of core registers for Thumb: this allocation is
20994    written over the corresponding initial entries of the array
20995    initialized with REG_ALLOC_ORDER.  We allocate all low registers
20996    first.  Saving and restoring a low register is usually cheaper than
20997    using a call-clobbered high register.  */
20998
20999 static const int thumb_core_reg_alloc_order[] =
21000 {
21001    3,  2,  1,  0,  4,  5,  6,  7,
21002   14, 12,  8,  9, 10, 11, 13, 15
21003 };
21004
21005 /* Adjust register allocation order when compiling for Thumb.  */
21006
21007 void
21008 arm_order_regs_for_local_alloc (void)
21009 {
21010   const int arm_reg_alloc_order[] = REG_ALLOC_ORDER;
21011   memcpy(reg_alloc_order, arm_reg_alloc_order, sizeof (reg_alloc_order));
21012   if (TARGET_THUMB)
21013     memcpy (reg_alloc_order, thumb_core_reg_alloc_order,
21014             sizeof (thumb_core_reg_alloc_order));
21015 }
21016
21017 /* Set default optimization options.  */
21018 void
21019 arm_optimization_options (int level, int size ATTRIBUTE_UNUSED)
21020 {
21021   /* Enable section anchors by default at -O1 or higher.
21022      Use 2 to distinguish from an explicit -fsection-anchors
21023      given on the command line.  */
21024   if (level > 0)
21025     flag_section_anchors = 2;
21026 }
21027
21028 /* Implement TARGET_FRAME_POINTER_REQUIRED.  */
21029
21030 bool
21031 arm_frame_pointer_required (void)
21032 {
21033   return (cfun->has_nonlocal_label
21034           || SUBTARGET_FRAME_POINTER_REQUIRED
21035           || (TARGET_ARM && TARGET_APCS_FRAME && ! leaf_function_p ()));
21036 }
21037
21038 #include "gt-arm.h"