OSDN Git Service

a7e225f22071e957f098bcf30bb8db6adaefdbda
[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 "ggc.h"
47 #include "except.h"
48 #include "c-pragma.h"
49 #include "integrate.h"
50 #include "tm_p.h"
51 #include "target.h"
52 #include "target-def.h"
53 #include "debug.h"
54 #include "langhooks.h"
55 #include "df.h"
56 #include "intl.h"
57
58 /* Forward definitions of types.  */
59 typedef struct minipool_node    Mnode;
60 typedef struct minipool_fixup   Mfix;
61
62 EXPORTED_CONST struct attribute_spec arm_attribute_table[];
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_isr_attribute (tree *, tree, tree, int, bool *);
118 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
119 static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
120 #endif
121 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
122 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
123 static void thumb1_output_function_prologue (FILE *, HOST_WIDE_INT);
124 static int arm_comp_type_attributes (const_tree, const_tree);
125 static void arm_set_default_type_attributes (tree);
126 static int arm_adjust_cost (rtx, rtx, rtx, int);
127 static int count_insns_for_constant (HOST_WIDE_INT, int);
128 static int arm_get_strip_length (int);
129 static bool arm_function_ok_for_sibcall (tree, tree);
130 static void arm_internal_label (FILE *, const char *, unsigned long);
131 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
132                                  tree);
133 static bool arm_rtx_costs_1 (rtx, enum rtx_code, int*, bool);
134 static bool arm_size_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *);
135 static bool arm_slowmul_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
136 static bool arm_fastmul_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
137 static bool arm_xscale_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
138 static bool arm_9e_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
139 static bool arm_rtx_costs (rtx, int, int, int *, bool);
140 static int arm_address_cost (rtx, bool);
141 static bool arm_memory_load_p (rtx);
142 static bool arm_cirrus_insn_p (rtx);
143 static void cirrus_reorg (rtx);
144 static void arm_init_builtins (void);
145 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
146 static void arm_init_iwmmxt_builtins (void);
147 static rtx safe_vector_operand (rtx, enum machine_mode);
148 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
149 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
150 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
151 static void emit_constant_insn (rtx cond, rtx pattern);
152 static rtx emit_set_insn (rtx, rtx);
153 static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
154                                   tree, bool);
155
156 #ifdef OBJECT_FORMAT_ELF
157 static void arm_elf_asm_constructor (rtx, int) ATTRIBUTE_UNUSED;
158 static void arm_elf_asm_destructor (rtx, int) ATTRIBUTE_UNUSED;
159 #endif
160 #ifndef ARM_PE
161 static void arm_encode_section_info (tree, rtx, int);
162 #endif
163
164 static void arm_file_end (void);
165 static void arm_file_start (void);
166
167 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
168                                         tree, int *, int);
169 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
170                                    enum machine_mode, const_tree, bool);
171 static bool arm_promote_prototypes (const_tree);
172 static bool arm_default_short_enums (void);
173 static bool arm_align_anon_bitfield (void);
174 static bool arm_return_in_msb (const_tree);
175 static bool arm_must_pass_in_stack (enum machine_mode, const_tree);
176 static bool arm_return_in_memory (const_tree, const_tree);
177 #ifdef TARGET_UNWIND_INFO
178 static void arm_unwind_emit (FILE *, rtx);
179 static bool arm_output_ttype (rtx);
180 #endif
181 static void arm_dwarf_handle_frame_unspec (const char *, rtx, int);
182
183 static tree arm_cxx_guard_type (void);
184 static bool arm_cxx_guard_mask_bit (void);
185 static tree arm_get_cookie_size (tree);
186 static bool arm_cookie_has_size (void);
187 static bool arm_cxx_cdtor_returns_this (void);
188 static bool arm_cxx_key_method_may_be_inline (void);
189 static void arm_cxx_determine_class_data_visibility (tree);
190 static bool arm_cxx_class_data_always_comdat (void);
191 static bool arm_cxx_use_aeabi_atexit (void);
192 static void arm_init_libfuncs (void);
193 static tree arm_build_builtin_va_list (void);
194 static void arm_expand_builtin_va_start (tree, rtx);
195 static tree arm_gimplify_va_arg_expr (tree, tree, gimple_seq *, gimple_seq *);
196 static bool arm_handle_option (size_t, const char *, int);
197 static void arm_target_help (void);
198 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
199 static bool arm_cannot_copy_insn_p (rtx);
200 static bool arm_tls_symbol_p (rtx x);
201 static int arm_issue_rate (void);
202 static void arm_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
203 static bool arm_allocate_stack_slots_for_args (void);
204 static const char *arm_invalid_parameter_type (const_tree t);
205 static const char *arm_invalid_return_type (const_tree t);
206 static tree arm_promoted_type (const_tree t);
207 static tree arm_convert_to_type (tree type, tree expr);
208 static bool arm_scalar_mode_supported_p (enum machine_mode);
209
210 \f
211 /* Initialize the GCC target structure.  */
212 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
213 #undef  TARGET_MERGE_DECL_ATTRIBUTES
214 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
215 #endif
216
217 #undef TARGET_LEGITIMIZE_ADDRESS
218 #define TARGET_LEGITIMIZE_ADDRESS arm_legitimize_address
219
220 #undef  TARGET_ATTRIBUTE_TABLE
221 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
222
223 #undef TARGET_ASM_FILE_START
224 #define TARGET_ASM_FILE_START arm_file_start
225 #undef TARGET_ASM_FILE_END
226 #define TARGET_ASM_FILE_END arm_file_end
227
228 #undef  TARGET_ASM_ALIGNED_SI_OP
229 #define TARGET_ASM_ALIGNED_SI_OP NULL
230 #undef  TARGET_ASM_INTEGER
231 #define TARGET_ASM_INTEGER arm_assemble_integer
232
233 #undef  TARGET_ASM_FUNCTION_PROLOGUE
234 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
235
236 #undef  TARGET_ASM_FUNCTION_EPILOGUE
237 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
238
239 #undef  TARGET_DEFAULT_TARGET_FLAGS
240 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
241 #undef  TARGET_HANDLE_OPTION
242 #define TARGET_HANDLE_OPTION arm_handle_option
243 #undef  TARGET_HELP
244 #define TARGET_HELP arm_target_help
245
246 #undef  TARGET_COMP_TYPE_ATTRIBUTES
247 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
248
249 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
250 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
251
252 #undef  TARGET_SCHED_ADJUST_COST
253 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
254
255 #undef TARGET_ENCODE_SECTION_INFO
256 #ifdef ARM_PE
257 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
258 #else
259 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
260 #endif
261
262 #undef  TARGET_STRIP_NAME_ENCODING
263 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
264
265 #undef  TARGET_ASM_INTERNAL_LABEL
266 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
267
268 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
269 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
270
271 #undef  TARGET_ASM_OUTPUT_MI_THUNK
272 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
273 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
274 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
275
276 #undef  TARGET_RTX_COSTS
277 #define TARGET_RTX_COSTS arm_rtx_costs
278 #undef  TARGET_ADDRESS_COST
279 #define TARGET_ADDRESS_COST arm_address_cost
280
281 #undef TARGET_SHIFT_TRUNCATION_MASK
282 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
283 #undef TARGET_VECTOR_MODE_SUPPORTED_P
284 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
285
286 #undef  TARGET_MACHINE_DEPENDENT_REORG
287 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
288
289 #undef  TARGET_INIT_BUILTINS
290 #define TARGET_INIT_BUILTINS  arm_init_builtins
291 #undef  TARGET_EXPAND_BUILTIN
292 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
293
294 #undef TARGET_INIT_LIBFUNCS
295 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
296
297 #undef TARGET_PROMOTE_FUNCTION_ARGS
298 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_const_tree_true
299 #undef TARGET_PROMOTE_FUNCTION_RETURN
300 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_const_tree_true
301 #undef TARGET_PROMOTE_PROTOTYPES
302 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
303 #undef TARGET_PASS_BY_REFERENCE
304 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
305 #undef TARGET_ARG_PARTIAL_BYTES
306 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
307
308 #undef  TARGET_SETUP_INCOMING_VARARGS
309 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
310
311 #undef TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS
312 #define TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS arm_allocate_stack_slots_for_args
313
314 #undef TARGET_DEFAULT_SHORT_ENUMS
315 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
316
317 #undef TARGET_ALIGN_ANON_BITFIELD
318 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
319
320 #undef TARGET_NARROW_VOLATILE_BITFIELD
321 #define TARGET_NARROW_VOLATILE_BITFIELD hook_bool_void_false
322
323 #undef TARGET_CXX_GUARD_TYPE
324 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
325
326 #undef TARGET_CXX_GUARD_MASK_BIT
327 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
328
329 #undef TARGET_CXX_GET_COOKIE_SIZE
330 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
331
332 #undef TARGET_CXX_COOKIE_HAS_SIZE
333 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
334
335 #undef TARGET_CXX_CDTOR_RETURNS_THIS
336 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
337
338 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
339 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
340
341 #undef TARGET_CXX_USE_AEABI_ATEXIT
342 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
343
344 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
345 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
346   arm_cxx_determine_class_data_visibility
347
348 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
349 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
350
351 #undef TARGET_RETURN_IN_MSB
352 #define TARGET_RETURN_IN_MSB arm_return_in_msb
353
354 #undef TARGET_RETURN_IN_MEMORY
355 #define TARGET_RETURN_IN_MEMORY arm_return_in_memory
356
357 #undef TARGET_MUST_PASS_IN_STACK
358 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
359
360 #ifdef TARGET_UNWIND_INFO
361 #undef TARGET_UNWIND_EMIT
362 #define TARGET_UNWIND_EMIT arm_unwind_emit
363
364 /* EABI unwinding tables use a different format for the typeinfo tables.  */
365 #undef TARGET_ASM_TTYPE
366 #define TARGET_ASM_TTYPE arm_output_ttype
367
368 #undef TARGET_ARM_EABI_UNWINDER
369 #define TARGET_ARM_EABI_UNWINDER true
370 #endif /* TARGET_UNWIND_INFO */
371
372 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
373 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC arm_dwarf_handle_frame_unspec
374
375 #undef  TARGET_CANNOT_COPY_INSN_P
376 #define TARGET_CANNOT_COPY_INSN_P arm_cannot_copy_insn_p
377
378 #ifdef HAVE_AS_TLS
379 #undef TARGET_HAVE_TLS
380 #define TARGET_HAVE_TLS true
381 #endif
382
383 #undef TARGET_CANNOT_FORCE_CONST_MEM
384 #define TARGET_CANNOT_FORCE_CONST_MEM arm_cannot_force_const_mem
385
386 #undef TARGET_MAX_ANCHOR_OFFSET
387 #define TARGET_MAX_ANCHOR_OFFSET 4095
388
389 /* The minimum is set such that the total size of the block
390    for a particular anchor is -4088 + 1 + 4095 bytes, which is
391    divisible by eight, ensuring natural spacing of anchors.  */
392 #undef TARGET_MIN_ANCHOR_OFFSET
393 #define TARGET_MIN_ANCHOR_OFFSET -4088
394
395 #undef TARGET_SCHED_ISSUE_RATE
396 #define TARGET_SCHED_ISSUE_RATE arm_issue_rate
397
398 #undef TARGET_MANGLE_TYPE
399 #define TARGET_MANGLE_TYPE arm_mangle_type
400
401 #undef TARGET_BUILD_BUILTIN_VA_LIST
402 #define TARGET_BUILD_BUILTIN_VA_LIST arm_build_builtin_va_list
403 #undef TARGET_EXPAND_BUILTIN_VA_START
404 #define TARGET_EXPAND_BUILTIN_VA_START arm_expand_builtin_va_start
405 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
406 #define TARGET_GIMPLIFY_VA_ARG_EXPR arm_gimplify_va_arg_expr
407
408 #ifdef HAVE_AS_TLS
409 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
410 #define TARGET_ASM_OUTPUT_DWARF_DTPREL arm_output_dwarf_dtprel
411 #endif
412
413 #undef TARGET_LEGITIMATE_ADDRESS_P
414 #define TARGET_LEGITIMATE_ADDRESS_P     arm_legitimate_address_p
415
416 #undef TARGET_INVALID_PARAMETER_TYPE
417 #define TARGET_INVALID_PARAMETER_TYPE arm_invalid_parameter_type
418
419 #undef TARGET_INVALID_RETURN_TYPE
420 #define TARGET_INVALID_RETURN_TYPE arm_invalid_return_type
421
422 #undef TARGET_PROMOTED_TYPE
423 #define TARGET_PROMOTED_TYPE arm_promoted_type
424
425 #undef TARGET_CONVERT_TO_TYPE
426 #define TARGET_CONVERT_TO_TYPE arm_convert_to_type
427
428 #undef TARGET_SCALAR_MODE_SUPPORTED_P
429 #define TARGET_SCALAR_MODE_SUPPORTED_P arm_scalar_mode_supported_p
430
431 struct gcc_target targetm = TARGET_INITIALIZER;
432 \f
433 /* Obstack for minipool constant handling.  */
434 static struct obstack minipool_obstack;
435 static char *         minipool_startobj;
436
437 /* The maximum number of insns skipped which
438    will be conditionalised if possible.  */
439 static int max_insns_skipped = 5;
440
441 extern FILE * asm_out_file;
442
443 /* True if we are currently building a constant table.  */
444 int making_const_table;
445
446 /* The processor for which instructions should be scheduled.  */
447 enum processor_type arm_tune = arm_none;
448
449 /* The default processor used if not overridden by commandline.  */
450 static enum processor_type arm_default_cpu = arm_none;
451
452 /* Which floating point model to use.  */
453 enum arm_fp_model arm_fp_model;
454
455 /* Which floating point hardware is available.  */
456 enum fputype arm_fpu_arch;
457
458 /* Which floating point hardware to schedule for.  */
459 enum fputype arm_fpu_tune;
460
461 /* Whether to use floating point hardware.  */
462 enum float_abi_type arm_float_abi;
463
464 /* Which __fp16 format to use.  */
465 enum arm_fp16_format_type arm_fp16_format;
466
467 /* Which ABI to use.  */
468 enum arm_abi_type arm_abi;
469
470 /* Which thread pointer model to use.  */
471 enum arm_tp_type target_thread_pointer = TP_AUTO;
472
473 /* Used to parse -mstructure_size_boundary command line option.  */
474 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
475
476 /* Used for Thumb call_via trampolines.  */
477 rtx thumb_call_via_label[14];
478 static int thumb_call_reg_needed;
479
480 /* Bit values used to identify processor capabilities.  */
481 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
482 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
483 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
484 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
485 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
486 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
487 #define FL_THUMB      (1 << 6)        /* Thumb aware */
488 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
489 #define FL_STRONG     (1 << 8)        /* StrongARM */
490 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
491 #define FL_XSCALE     (1 << 10)       /* XScale */
492 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
493 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
494                                          media instructions.  */
495 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
496 #define FL_WBUF       (1 << 14)       /* Schedule for write buffer ops.
497                                          Note: ARM6 & 7 derivatives only.  */
498 #define FL_ARCH6K     (1 << 15)       /* Architecture rel 6 K extensions.  */
499 #define FL_THUMB2     (1 << 16)       /* Thumb-2.  */
500 #define FL_NOTM       (1 << 17)       /* Instructions not present in the 'M'
501                                          profile.  */
502 #define FL_DIV        (1 << 18)       /* Hardware divide.  */
503 #define FL_VFPV3      (1 << 19)       /* Vector Floating Point V3.  */
504 #define FL_NEON       (1 << 20)       /* Neon instructions.  */
505
506 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
507
508 #define FL_FOR_ARCH2    FL_NOTM
509 #define FL_FOR_ARCH3    (FL_FOR_ARCH2 | FL_MODE32)
510 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
511 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
512 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
513 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
514 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
515 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
516 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
517 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
518 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
519 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
520 #define FL_FOR_ARCH6K   (FL_FOR_ARCH6 | FL_ARCH6K)
521 #define FL_FOR_ARCH6Z   FL_FOR_ARCH6
522 #define FL_FOR_ARCH6ZK  FL_FOR_ARCH6K
523 #define FL_FOR_ARCH6T2  (FL_FOR_ARCH6 | FL_THUMB2)
524 #define FL_FOR_ARCH6M   (FL_FOR_ARCH6 & ~FL_NOTM)
525 #define FL_FOR_ARCH7    (FL_FOR_ARCH6T2 &~ FL_NOTM)
526 #define FL_FOR_ARCH7A   (FL_FOR_ARCH7 | FL_NOTM)
527 #define FL_FOR_ARCH7R   (FL_FOR_ARCH7A | FL_DIV)
528 #define FL_FOR_ARCH7M   (FL_FOR_ARCH7 | FL_DIV)
529
530 /* The bits in this mask specify which
531    instructions we are allowed to generate.  */
532 static unsigned long insn_flags = 0;
533
534 /* The bits in this mask specify which instruction scheduling options should
535    be used.  */
536 static unsigned long tune_flags = 0;
537
538 /* The following are used in the arm.md file as equivalents to bits
539    in the above two flag variables.  */
540
541 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
542 int arm_arch3m = 0;
543
544 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
545 int arm_arch4 = 0;
546
547 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
548 int arm_arch4t = 0;
549
550 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
551 int arm_arch5 = 0;
552
553 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
554 int arm_arch5e = 0;
555
556 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
557 int arm_arch6 = 0;
558
559 /* Nonzero if this chip supports the ARM 6K extensions.  */
560 int arm_arch6k = 0;
561
562 /* Nonzero if instructions not present in the 'M' profile can be used.  */
563 int arm_arch_notm = 0;
564
565 /* Nonzero if this chip can benefit from load scheduling.  */
566 int arm_ld_sched = 0;
567
568 /* Nonzero if this chip is a StrongARM.  */
569 int arm_tune_strongarm = 0;
570
571 /* Nonzero if this chip is a Cirrus variant.  */
572 int arm_arch_cirrus = 0;
573
574 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
575 int arm_arch_iwmmxt = 0;
576
577 /* Nonzero if this chip is an XScale.  */
578 int arm_arch_xscale = 0;
579
580 /* Nonzero if tuning for XScale  */
581 int arm_tune_xscale = 0;
582
583 /* Nonzero if we want to tune for stores that access the write-buffer.
584    This typically means an ARM6 or ARM7 with MMU or MPU.  */
585 int arm_tune_wbuf = 0;
586
587 /* Nonzero if tuning for Cortex-A9.  */
588 int arm_tune_cortex_a9 = 0;
589
590 /* Nonzero if generating Thumb instructions.  */
591 int thumb_code = 0;
592
593 /* Nonzero if we should define __THUMB_INTERWORK__ in the
594    preprocessor.
595    XXX This is a bit of a hack, it's intended to help work around
596    problems in GLD which doesn't understand that armv5t code is
597    interworking clean.  */
598 int arm_cpp_interwork = 0;
599
600 /* Nonzero if chip supports Thumb 2.  */
601 int arm_arch_thumb2;
602
603 /* Nonzero if chip supports integer division instruction.  */
604 int arm_arch_hwdiv;
605
606 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
607    must report the mode of the memory reference from PRINT_OPERAND to
608    PRINT_OPERAND_ADDRESS.  */
609 enum machine_mode output_memory_reference_mode;
610
611 /* The register number to be used for the PIC offset register.  */
612 unsigned arm_pic_register = INVALID_REGNUM;
613
614 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
615    the next function.  */
616 static int after_arm_reorg = 0;
617
618 /* The maximum number of insns to be used when loading a constant.  */
619 static int arm_constant_limit = 3;
620
621 /* For an explanation of these variables, see final_prescan_insn below.  */
622 int arm_ccfsm_state;
623 /* arm_current_cc is also used for Thumb-2 cond_exec blocks.  */
624 enum arm_cond_code arm_current_cc;
625 rtx arm_target_insn;
626 int arm_target_label;
627 /* The number of conditionally executed insns, including the current insn.  */
628 int arm_condexec_count = 0;
629 /* A bitmask specifying the patterns for the IT block.
630    Zero means do not output an IT block before this insn. */
631 int arm_condexec_mask = 0;
632 /* The number of bits used in arm_condexec_mask.  */
633 int arm_condexec_masklen = 0;
634
635 /* The condition codes of the ARM, and the inverse function.  */
636 static const char * const arm_condition_codes[] =
637 {
638   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
639   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
640 };
641
642 #define ARM_LSL_NAME (TARGET_UNIFIED_ASM ? "lsl" : "asl")
643 #define streq(string1, string2) (strcmp (string1, string2) == 0)
644
645 #define THUMB2_WORK_REGS (0xff & ~(  (1 << THUMB_HARD_FRAME_POINTER_REGNUM) \
646                                    | (1 << SP_REGNUM) | (1 << PC_REGNUM) \
647                                    | (1 << PIC_OFFSET_TABLE_REGNUM)))
648 \f
649 /* Initialization code.  */
650
651 struct processors
652 {
653   const char *const name;
654   enum processor_type core;
655   const char *arch;
656   const unsigned long flags;
657   bool (* rtx_costs) (rtx, enum rtx_code, enum rtx_code, int *, bool);
658 };
659
660 /* Not all of these give usefully different compilation alternatives,
661    but there is no simple way of generalizing them.  */
662 static const struct processors all_cores[] =
663 {
664   /* ARM Cores */
665 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
666   {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
667 #include "arm-cores.def"
668 #undef ARM_CORE
669   {NULL, arm_none, NULL, 0, NULL}
670 };
671
672 static const struct processors all_architectures[] =
673 {
674   /* ARM Architectures */
675   /* We don't specify rtx_costs here as it will be figured out
676      from the core.  */
677
678   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
679   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
680   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
681   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
682   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
683   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
684      implementations that support it, so we will leave it out for now.  */
685   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
686   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
687   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
688   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
689   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
690   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
691   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
692   {"armv6k",  mpcore,     "6K",  FL_CO_PROC |             FL_FOR_ARCH6K, NULL},
693   {"armv6z",  arm1176jzs, "6Z",  FL_CO_PROC |             FL_FOR_ARCH6Z, NULL},
694   {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC |             FL_FOR_ARCH6ZK, NULL},
695   {"armv6t2", arm1156t2s, "6T2", FL_CO_PROC |             FL_FOR_ARCH6T2, NULL},
696   {"armv6-m", cortexm1,   "6M",                           FL_FOR_ARCH6M, NULL},
697   {"armv7",   cortexa8,   "7",   FL_CO_PROC |             FL_FOR_ARCH7, NULL},
698   {"armv7-a", cortexa8,   "7A",  FL_CO_PROC |             FL_FOR_ARCH7A, NULL},
699   {"armv7-r", cortexr4,   "7R",  FL_CO_PROC |             FL_FOR_ARCH7R, NULL},
700   {"armv7-m", cortexm3,   "7M",  FL_CO_PROC |             FL_FOR_ARCH7M, NULL},
701   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
702   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
703   {"iwmmxt2", iwmmxt2,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
704   {NULL, arm_none, NULL, 0 , NULL}
705 };
706
707 struct arm_cpu_select
708 {
709   const char *              string;
710   const char *              name;
711   const struct processors * processors;
712 };
713
714 /* This is a magic structure.  The 'string' field is magically filled in
715    with a pointer to the value specified by the user on the command line
716    assuming that the user has specified such a value.  */
717
718 static struct arm_cpu_select arm_select[] =
719 {
720   /* string       name            processors  */
721   { NULL,       "-mcpu=",       all_cores  },
722   { NULL,       "-march=",      all_architectures },
723   { NULL,       "-mtune=",      all_cores }
724 };
725
726 /* Defines representing the indexes into the above table.  */
727 #define ARM_OPT_SET_CPU 0
728 #define ARM_OPT_SET_ARCH 1
729 #define ARM_OPT_SET_TUNE 2
730
731 /* The name of the preprocessor macro to define for this architecture.  */
732
733 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
734
735 struct fpu_desc
736 {
737   const char * name;
738   enum fputype fpu;
739 };
740
741
742 /* Available values for -mfpu=.  */
743
744 static const struct fpu_desc all_fpus[] =
745 {
746   {"fpa",               FPUTYPE_FPA},
747   {"fpe2",              FPUTYPE_FPA_EMU2},
748   {"fpe3",              FPUTYPE_FPA_EMU2},
749   {"maverick",          FPUTYPE_MAVERICK},
750   {"vfp",               FPUTYPE_VFP},
751   {"vfp3",              FPUTYPE_VFP3},
752   {"vfpv3",             FPUTYPE_VFP3},
753   {"vfpv3-d16",         FPUTYPE_VFP3D16},
754   {"neon",              FPUTYPE_NEON},
755   {"neon-fp16",         FPUTYPE_NEON_FP16}
756 };
757
758
759 /* Floating point models used by the different hardware.
760    See fputype in arm.h.  */
761
762 static const enum arm_fp_model fp_model_for_fpu[] =
763 {
764   /* No FP hardware.  */
765   ARM_FP_MODEL_UNKNOWN,         /* FPUTYPE_NONE  */
766   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA  */
767   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU2  */
768   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU3  */
769   ARM_FP_MODEL_MAVERICK,        /* FPUTYPE_MAVERICK  */
770   ARM_FP_MODEL_VFP,             /* FPUTYPE_VFP  */
771   ARM_FP_MODEL_VFP,             /* FPUTYPE_VFP3D16  */
772   ARM_FP_MODEL_VFP,             /* FPUTYPE_VFP3  */
773   ARM_FP_MODEL_VFP,             /* FPUTYPE_NEON  */
774   ARM_FP_MODEL_VFP              /* FPUTYPE_NEON_FP16  */
775 };
776
777
778 struct float_abi
779 {
780   const char * name;
781   enum float_abi_type abi_type;
782 };
783
784
785 /* Available values for -mfloat-abi=.  */
786
787 static const struct float_abi all_float_abis[] =
788 {
789   {"soft",      ARM_FLOAT_ABI_SOFT},
790   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
791   {"hard",      ARM_FLOAT_ABI_HARD}
792 };
793
794
795 struct fp16_format
796 {
797   const char *name;
798   enum arm_fp16_format_type fp16_format_type;
799 };
800
801
802 /* Available values for -mfp16-format=.  */
803
804 static const struct fp16_format all_fp16_formats[] =
805 {
806   {"none",              ARM_FP16_FORMAT_NONE},
807   {"ieee",              ARM_FP16_FORMAT_IEEE},
808   {"alternative",       ARM_FP16_FORMAT_ALTERNATIVE}
809 };
810
811
812 struct abi_name
813 {
814   const char *name;
815   enum arm_abi_type abi_type;
816 };
817
818
819 /* Available values for -mabi=.  */
820
821 static const struct abi_name arm_all_abis[] =
822 {
823   {"apcs-gnu",    ARM_ABI_APCS},
824   {"atpcs",   ARM_ABI_ATPCS},
825   {"aapcs",   ARM_ABI_AAPCS},
826   {"iwmmxt",  ARM_ABI_IWMMXT},
827   {"aapcs-linux",   ARM_ABI_AAPCS_LINUX}
828 };
829
830 /* Supported TLS relocations.  */
831
832 enum tls_reloc {
833   TLS_GD32,
834   TLS_LDM32,
835   TLS_LDO32,
836   TLS_IE32,
837   TLS_LE32
838 };
839
840 /* Emit an insn that's a simple single-set.  Both the operands must be known
841    to be valid.  */
842 inline static rtx
843 emit_set_insn (rtx x, rtx y)
844 {
845   return emit_insn (gen_rtx_SET (VOIDmode, x, y));
846 }
847
848 /* Return the number of bits set in VALUE.  */
849 static unsigned
850 bit_count (unsigned long value)
851 {
852   unsigned long count = 0;
853
854   while (value)
855     {
856       count++;
857       value &= value - 1;  /* Clear the least-significant set bit.  */
858     }
859
860   return count;
861 }
862
863 /* Set up library functions unique to ARM.  */
864
865 static void
866 arm_init_libfuncs (void)
867 {
868   /* There are no special library functions unless we are using the
869      ARM BPABI.  */
870   if (!TARGET_BPABI)
871     return;
872
873   /* The functions below are described in Section 4 of the "Run-Time
874      ABI for the ARM architecture", Version 1.0.  */
875
876   /* Double-precision floating-point arithmetic.  Table 2.  */
877   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
878   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
879   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
880   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
881   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
882
883   /* Double-precision comparisons.  Table 3.  */
884   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
885   set_optab_libfunc (ne_optab, DFmode, NULL);
886   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
887   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
888   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
889   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
890   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
891
892   /* Single-precision floating-point arithmetic.  Table 4.  */
893   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
894   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
895   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
896   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
897   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
898
899   /* Single-precision comparisons.  Table 5.  */
900   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
901   set_optab_libfunc (ne_optab, SFmode, NULL);
902   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
903   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
904   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
905   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
906   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
907
908   /* Floating-point to integer conversions.  Table 6.  */
909   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
910   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
911   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
912   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
913   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
914   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
915   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
916   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
917
918   /* Conversions between floating types.  Table 7.  */
919   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
920   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
921
922   /* Integer to floating-point conversions.  Table 8.  */
923   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
924   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
925   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
926   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
927   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
928   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
929   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
930   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
931
932   /* Long long.  Table 9.  */
933   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
934   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
935   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
936   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
937   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
938   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
939   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
940   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
941
942   /* Integer (32/32->32) division.  \S 4.3.1.  */
943   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
944   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
945
946   /* The divmod functions are designed so that they can be used for
947      plain division, even though they return both the quotient and the
948      remainder.  The quotient is returned in the usual location (i.e.,
949      r0 for SImode, {r0, r1} for DImode), just as would be expected
950      for an ordinary division routine.  Because the AAPCS calling
951      conventions specify that all of { r0, r1, r2, r3 } are
952      callee-saved registers, there is no need to tell the compiler
953      explicitly that those registers are clobbered by these
954      routines.  */
955   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
956   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
957
958   /* For SImode division the ABI provides div-without-mod routines,
959      which are faster.  */
960   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
961   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
962
963   /* We don't have mod libcalls.  Fortunately gcc knows how to use the
964      divmod libcalls instead.  */
965   set_optab_libfunc (smod_optab, DImode, NULL);
966   set_optab_libfunc (umod_optab, DImode, NULL);
967   set_optab_libfunc (smod_optab, SImode, NULL);
968   set_optab_libfunc (umod_optab, SImode, NULL);
969
970   /* Half-precision float operations.  The compiler handles all operations
971      with NULL libfuncs by converting the SFmode.  */
972   switch (arm_fp16_format)
973     {
974     case ARM_FP16_FORMAT_IEEE:
975     case ARM_FP16_FORMAT_ALTERNATIVE:
976
977       /* Conversions.  */
978       set_conv_libfunc (trunc_optab, HFmode, SFmode,
979                         (arm_fp16_format == ARM_FP16_FORMAT_IEEE
980                          ? "__gnu_f2h_ieee"
981                          : "__gnu_f2h_alternative"));
982       set_conv_libfunc (sext_optab, SFmode, HFmode, 
983                         (arm_fp16_format == ARM_FP16_FORMAT_IEEE
984                          ? "__gnu_h2f_ieee"
985                          : "__gnu_h2f_alternative"));
986       
987       /* Arithmetic.  */
988       set_optab_libfunc (add_optab, HFmode, NULL);
989       set_optab_libfunc (sdiv_optab, HFmode, NULL);
990       set_optab_libfunc (smul_optab, HFmode, NULL);
991       set_optab_libfunc (neg_optab, HFmode, NULL);
992       set_optab_libfunc (sub_optab, HFmode, NULL);
993
994       /* Comparisons.  */
995       set_optab_libfunc (eq_optab, HFmode, NULL);
996       set_optab_libfunc (ne_optab, HFmode, NULL);
997       set_optab_libfunc (lt_optab, HFmode, NULL);
998       set_optab_libfunc (le_optab, HFmode, NULL);
999       set_optab_libfunc (ge_optab, HFmode, NULL);
1000       set_optab_libfunc (gt_optab, HFmode, NULL);
1001       set_optab_libfunc (unord_optab, HFmode, NULL);
1002       break;
1003
1004     default:
1005       break;
1006     }
1007 }
1008
1009 /* On AAPCS systems, this is the "struct __va_list".  */
1010 static GTY(()) tree va_list_type;
1011
1012 /* Return the type to use as __builtin_va_list.  */
1013 static tree
1014 arm_build_builtin_va_list (void)
1015 {
1016   tree va_list_name;
1017   tree ap_field;
1018   
1019   if (!TARGET_AAPCS_BASED)
1020     return std_build_builtin_va_list ();
1021
1022   /* AAPCS \S 7.1.4 requires that va_list be a typedef for a type
1023      defined as:
1024
1025        struct __va_list 
1026        {
1027          void *__ap;
1028        };
1029
1030      The C Library ABI further reinforces this definition in \S
1031      4.1.
1032
1033      We must follow this definition exactly.  The structure tag
1034      name is visible in C++ mangled names, and thus forms a part
1035      of the ABI.  The field name may be used by people who
1036      #include <stdarg.h>.  */
1037   /* Create the type.  */
1038   va_list_type = lang_hooks.types.make_type (RECORD_TYPE);
1039   /* Give it the required name.  */
1040   va_list_name = build_decl (BUILTINS_LOCATION,
1041                              TYPE_DECL,
1042                              get_identifier ("__va_list"),
1043                              va_list_type);
1044   DECL_ARTIFICIAL (va_list_name) = 1;
1045   TYPE_NAME (va_list_type) = va_list_name;
1046   /* Create the __ap field.  */
1047   ap_field = build_decl (BUILTINS_LOCATION,
1048                          FIELD_DECL, 
1049                          get_identifier ("__ap"),
1050                          ptr_type_node);
1051   DECL_ARTIFICIAL (ap_field) = 1;
1052   DECL_FIELD_CONTEXT (ap_field) = va_list_type;
1053   TYPE_FIELDS (va_list_type) = ap_field;
1054   /* Compute its layout.  */
1055   layout_type (va_list_type);
1056
1057   return va_list_type;
1058 }
1059
1060 /* Return an expression of type "void *" pointing to the next
1061    available argument in a variable-argument list.  VALIST is the
1062    user-level va_list object, of type __builtin_va_list.  */
1063 static tree
1064 arm_extract_valist_ptr (tree valist)
1065 {
1066   if (TREE_TYPE (valist) == error_mark_node)
1067     return error_mark_node;
1068
1069   /* On an AAPCS target, the pointer is stored within "struct
1070      va_list".  */
1071   if (TARGET_AAPCS_BASED)
1072     {
1073       tree ap_field = TYPE_FIELDS (TREE_TYPE (valist));
1074       valist = build3 (COMPONENT_REF, TREE_TYPE (ap_field), 
1075                        valist, ap_field, NULL_TREE);
1076     }
1077
1078   return valist;
1079 }
1080
1081 /* Implement TARGET_EXPAND_BUILTIN_VA_START.  */
1082 static void
1083 arm_expand_builtin_va_start (tree valist, rtx nextarg)
1084 {
1085   valist = arm_extract_valist_ptr (valist);
1086   std_expand_builtin_va_start (valist, nextarg);
1087 }
1088
1089 /* Implement TARGET_GIMPLIFY_VA_ARG_EXPR.  */
1090 static tree
1091 arm_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p, 
1092                           gimple_seq *post_p)
1093 {
1094   valist = arm_extract_valist_ptr (valist);
1095   return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
1096 }
1097
1098 /* Implement TARGET_HANDLE_OPTION.  */
1099
1100 static bool
1101 arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
1102 {
1103   switch (code)
1104     {
1105     case OPT_march_:
1106       arm_select[1].string = arg;
1107       return true;
1108
1109     case OPT_mcpu_:
1110       arm_select[0].string = arg;
1111       return true;
1112
1113     case OPT_mhard_float:
1114       target_float_abi_name = "hard";
1115       return true;
1116
1117     case OPT_msoft_float:
1118       target_float_abi_name = "soft";
1119       return true;
1120
1121     case OPT_mtune_:
1122       arm_select[2].string = arg;
1123       return true;
1124
1125     default:
1126       return true;
1127     }
1128 }
1129
1130 static void
1131 arm_target_help (void)
1132 {
1133   int i;
1134   static int columns = 0;
1135   int remaining;
1136
1137   /* If we have not done so already, obtain the desired maximum width of
1138      the output.  Note - this is a duplication of the code at the start of
1139      gcc/opts.c:print_specific_help() - the two copies should probably be
1140      replaced by a single function.  */
1141   if (columns == 0)
1142     {
1143       const char *p;
1144
1145       GET_ENVIRONMENT (p, "COLUMNS");
1146       if (p != NULL)
1147         {
1148           int value = atoi (p);
1149
1150           if (value > 0)
1151             columns = value;
1152         }
1153
1154       if (columns == 0)
1155         /* Use a reasonable default.  */
1156         columns = 80;
1157     }
1158
1159   printf ("  Known ARM CPUs (for use with the -mcpu= and -mtune= options):\n");
1160
1161   /* The - 2 is because we know that the last entry in the array is NULL.  */
1162   i = ARRAY_SIZE (all_cores) - 2;
1163   gcc_assert (i > 0);
1164   printf ("    %s", all_cores[i].name);
1165   remaining = columns - (strlen (all_cores[i].name) + 4);
1166   gcc_assert (remaining >= 0);
1167
1168   while (i--)
1169     {
1170       int len = strlen (all_cores[i].name);
1171
1172       if (remaining > len + 2)
1173         {
1174           printf (", %s", all_cores[i].name);
1175           remaining -= len + 2;
1176         }
1177       else
1178         {
1179           if (remaining > 0)
1180             printf (",");
1181           printf ("\n    %s", all_cores[i].name);
1182           remaining = columns - (len + 4);
1183         }
1184     }
1185
1186   printf ("\n\n  Known ARM architectures (for use with the -march= option):\n");
1187
1188   i = ARRAY_SIZE (all_architectures) - 2;
1189   gcc_assert (i > 0);
1190   
1191   printf ("    %s", all_architectures[i].name);
1192   remaining = columns - (strlen (all_architectures[i].name) + 4);
1193   gcc_assert (remaining >= 0);
1194
1195   while (i--)
1196     {
1197       int len = strlen (all_architectures[i].name);
1198
1199       if (remaining > len + 2)
1200         {
1201           printf (", %s", all_architectures[i].name);
1202           remaining -= len + 2;
1203         }
1204       else
1205         {
1206           if (remaining > 0)
1207             printf (",");
1208           printf ("\n    %s", all_architectures[i].name);
1209           remaining = columns - (len + 4);
1210         }
1211     }
1212   printf ("\n");
1213
1214 }
1215
1216 /* Fix up any incompatible options that the user has specified.
1217    This has now turned into a maze.  */
1218 void
1219 arm_override_options (void)
1220 {
1221   unsigned i;
1222   enum processor_type target_arch_cpu = arm_none;
1223   enum processor_type selected_cpu = arm_none;
1224
1225   /* Set up the flags based on the cpu/architecture selected by the user.  */
1226   for (i = ARRAY_SIZE (arm_select); i--;)
1227     {
1228       struct arm_cpu_select * ptr = arm_select + i;
1229
1230       if (ptr->string != NULL && ptr->string[0] != '\0')
1231         {
1232           const struct processors * sel;
1233
1234           for (sel = ptr->processors; sel->name != NULL; sel++)
1235             if (streq (ptr->string, sel->name))
1236               {
1237                 /* Set the architecture define.  */
1238                 if (i != ARM_OPT_SET_TUNE)
1239                   sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
1240
1241                 /* Determine the processor core for which we should
1242                    tune code-generation.  */
1243                 if (/* -mcpu= is a sensible default.  */
1244                     i == ARM_OPT_SET_CPU
1245                     /* -mtune= overrides -mcpu= and -march=.  */
1246                     || i == ARM_OPT_SET_TUNE)
1247                   arm_tune = (enum processor_type) (sel - ptr->processors);
1248
1249                 /* Remember the CPU associated with this architecture.
1250                    If no other option is used to set the CPU type,
1251                    we'll use this to guess the most suitable tuning
1252                    options.  */
1253                 if (i == ARM_OPT_SET_ARCH)
1254                   target_arch_cpu = sel->core;
1255
1256                 if (i == ARM_OPT_SET_CPU)
1257                   selected_cpu = (enum processor_type) (sel - ptr->processors);
1258                   
1259                 if (i != ARM_OPT_SET_TUNE)
1260                   {
1261                     /* If we have been given an architecture and a processor
1262                        make sure that they are compatible.  We only generate
1263                        a warning though, and we prefer the CPU over the
1264                        architecture.  */
1265                     if (insn_flags != 0 && (insn_flags ^ sel->flags))
1266                       warning (0, "switch -mcpu=%s conflicts with -march= switch",
1267                                ptr->string);
1268
1269                     insn_flags = sel->flags;
1270                   }
1271
1272                 break;
1273               }
1274
1275           if (sel->name == NULL)
1276             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
1277         }
1278     }
1279
1280   /* Guess the tuning options from the architecture if necessary.  */
1281   if (arm_tune == arm_none)
1282     arm_tune = target_arch_cpu;
1283
1284   /* If the user did not specify a processor, choose one for them.  */
1285   if (insn_flags == 0)
1286     {
1287       const struct processors * sel;
1288       unsigned int        sought;
1289
1290       selected_cpu = (enum processor_type) TARGET_CPU_DEFAULT;
1291       if (selected_cpu == arm_none)
1292         {
1293 #ifdef SUBTARGET_CPU_DEFAULT
1294           /* Use the subtarget default CPU if none was specified by
1295              configure.  */
1296           selected_cpu = (enum processor_type) SUBTARGET_CPU_DEFAULT;
1297 #endif
1298           /* Default to ARM6.  */
1299           if (selected_cpu == arm_none)
1300             selected_cpu = arm6;
1301         }
1302       sel = &all_cores[selected_cpu];
1303
1304       insn_flags = sel->flags;
1305
1306       /* Now check to see if the user has specified some command line
1307          switch that require certain abilities from the cpu.  */
1308       sought = 0;
1309
1310       if (TARGET_INTERWORK || TARGET_THUMB)
1311         {
1312           sought |= (FL_THUMB | FL_MODE32);
1313
1314           /* There are no ARM processors that support both APCS-26 and
1315              interworking.  Therefore we force FL_MODE26 to be removed
1316              from insn_flags here (if it was set), so that the search
1317              below will always be able to find a compatible processor.  */
1318           insn_flags &= ~FL_MODE26;
1319         }
1320
1321       if (sought != 0 && ((sought & insn_flags) != sought))
1322         {
1323           /* Try to locate a CPU type that supports all of the abilities
1324              of the default CPU, plus the extra abilities requested by
1325              the user.  */
1326           for (sel = all_cores; sel->name != NULL; sel++)
1327             if ((sel->flags & sought) == (sought | insn_flags))
1328               break;
1329
1330           if (sel->name == NULL)
1331             {
1332               unsigned current_bit_count = 0;
1333               const struct processors * best_fit = NULL;
1334
1335               /* Ideally we would like to issue an error message here
1336                  saying that it was not possible to find a CPU compatible
1337                  with the default CPU, but which also supports the command
1338                  line options specified by the programmer, and so they
1339                  ought to use the -mcpu=<name> command line option to
1340                  override the default CPU type.
1341
1342                  If we cannot find a cpu that has both the
1343                  characteristics of the default cpu and the given
1344                  command line options we scan the array again looking
1345                  for a best match.  */
1346               for (sel = all_cores; sel->name != NULL; sel++)
1347                 if ((sel->flags & sought) == sought)
1348                   {
1349                     unsigned count;
1350
1351                     count = bit_count (sel->flags & insn_flags);
1352
1353                     if (count >= current_bit_count)
1354                       {
1355                         best_fit = sel;
1356                         current_bit_count = count;
1357                       }
1358                   }
1359
1360               gcc_assert (best_fit);
1361               sel = best_fit;
1362             }
1363
1364           insn_flags = sel->flags;
1365         }
1366       sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
1367       arm_default_cpu = (enum processor_type) (sel - all_cores);
1368       if (arm_tune == arm_none)
1369         arm_tune = arm_default_cpu;
1370     }
1371
1372   /* The processor for which we should tune should now have been
1373      chosen.  */
1374   gcc_assert (arm_tune != arm_none);
1375
1376   tune_flags = all_cores[(int)arm_tune].flags;
1377
1378   if (target_fp16_format_name)
1379     {
1380       for (i = 0; i < ARRAY_SIZE (all_fp16_formats); i++)
1381         {
1382           if (streq (all_fp16_formats[i].name, target_fp16_format_name))
1383             {
1384               arm_fp16_format = all_fp16_formats[i].fp16_format_type;
1385               break;
1386             }
1387         }
1388       if (i == ARRAY_SIZE (all_fp16_formats))
1389         error ("invalid __fp16 format option: -mfp16-format=%s",
1390                target_fp16_format_name);
1391     }
1392   else
1393     arm_fp16_format = ARM_FP16_FORMAT_NONE;
1394
1395   if (target_abi_name)
1396     {
1397       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1398         {
1399           if (streq (arm_all_abis[i].name, target_abi_name))
1400             {
1401               arm_abi = arm_all_abis[i].abi_type;
1402               break;
1403             }
1404         }
1405       if (i == ARRAY_SIZE (arm_all_abis))
1406         error ("invalid ABI option: -mabi=%s", target_abi_name);
1407     }
1408   else
1409     arm_abi = ARM_DEFAULT_ABI;
1410
1411   /* Make sure that the processor choice does not conflict with any of the
1412      other command line choices.  */
1413   if (TARGET_ARM && !(insn_flags & FL_NOTM))
1414     error ("target CPU does not support ARM mode");
1415
1416   /* BPABI targets use linker tricks to allow interworking on cores
1417      without thumb support.  */
1418   if (TARGET_INTERWORK && !((insn_flags & FL_THUMB) || TARGET_BPABI))
1419     {
1420       warning (0, "target CPU does not support interworking" );
1421       target_flags &= ~MASK_INTERWORK;
1422     }
1423
1424   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1425     {
1426       warning (0, "target CPU does not support THUMB instructions");
1427       target_flags &= ~MASK_THUMB;
1428     }
1429
1430   if (TARGET_APCS_FRAME && TARGET_THUMB)
1431     {
1432       /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1433       target_flags &= ~MASK_APCS_FRAME;
1434     }
1435
1436   /* Callee super interworking implies thumb interworking.  Adding
1437      this to the flags here simplifies the logic elsewhere.  */
1438   if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
1439       target_flags |= MASK_INTERWORK;
1440
1441   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1442      from here where no function is being compiled currently.  */
1443   if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1444     warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1445
1446   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1447     warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1448
1449   if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
1450     warning (0, "enabling caller interworking support is only meaningful when compiling for the Thumb");
1451
1452   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1453     {
1454       warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1455       target_flags |= MASK_APCS_FRAME;
1456     }
1457
1458   if (TARGET_POKE_FUNCTION_NAME)
1459     target_flags |= MASK_APCS_FRAME;
1460
1461   if (TARGET_APCS_REENT && flag_pic)
1462     error ("-fpic and -mapcs-reent are incompatible");
1463
1464   if (TARGET_APCS_REENT)
1465     warning (0, "APCS reentrant code not supported.  Ignored");
1466
1467   /* If this target is normally configured to use APCS frames, warn if they
1468      are turned off and debugging is turned on.  */
1469   if (TARGET_ARM
1470       && write_symbols != NO_DEBUG
1471       && !TARGET_APCS_FRAME
1472       && (TARGET_DEFAULT & MASK_APCS_FRAME))
1473     warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1474
1475   if (TARGET_APCS_FLOAT)
1476     warning (0, "passing floating point arguments in fp regs not yet supported");
1477
1478   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
1479   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1480   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1481   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1482   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1483   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1484   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1485   arm_arch6k = (insn_flags & FL_ARCH6K) != 0;
1486   arm_arch_notm = (insn_flags & FL_NOTM) != 0;
1487   arm_arch_thumb2 = (insn_flags & FL_THUMB2) != 0;
1488   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1489   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1490
1491   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1492   arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1493   thumb_code = (TARGET_ARM == 0);
1494   arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1495   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1496   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1497   arm_arch_hwdiv = (insn_flags & FL_DIV) != 0;
1498   arm_tune_cortex_a9 = (arm_tune == cortexa9) != 0;
1499
1500   /* If we are not using the default (ARM mode) section anchor offset
1501      ranges, then set the correct ranges now.  */
1502   if (TARGET_THUMB1)
1503     {
1504       /* Thumb-1 LDR instructions cannot have negative offsets.
1505          Permissible positive offset ranges are 5-bit (for byte loads),
1506          6-bit (for halfword loads), or 7-bit (for word loads).
1507          Empirical results suggest a 7-bit anchor range gives the best
1508          overall code size.  */
1509       targetm.min_anchor_offset = 0;
1510       targetm.max_anchor_offset = 127;
1511     }
1512   else if (TARGET_THUMB2)
1513     {
1514       /* The minimum is set such that the total size of the block
1515          for a particular anchor is 248 + 1 + 4095 bytes, which is
1516          divisible by eight, ensuring natural spacing of anchors.  */
1517       targetm.min_anchor_offset = -248;
1518       targetm.max_anchor_offset = 4095;
1519     }
1520
1521   /* V5 code we generate is completely interworking capable, so we turn off
1522      TARGET_INTERWORK here to avoid many tests later on.  */
1523
1524   /* XXX However, we must pass the right pre-processor defines to CPP
1525      or GLD can get confused.  This is a hack.  */
1526   if (TARGET_INTERWORK)
1527     arm_cpp_interwork = 1;
1528
1529   if (arm_arch5)
1530     target_flags &= ~MASK_INTERWORK;
1531
1532   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1533     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1534
1535   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1536     error ("iwmmxt abi requires an iwmmxt capable cpu");
1537
1538   arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1539   if (target_fpu_name == NULL && target_fpe_name != NULL)
1540     {
1541       if (streq (target_fpe_name, "2"))
1542         target_fpu_name = "fpe2";
1543       else if (streq (target_fpe_name, "3"))
1544         target_fpu_name = "fpe3";
1545       else
1546         error ("invalid floating point emulation option: -mfpe=%s",
1547                target_fpe_name);
1548     }
1549   if (target_fpu_name != NULL)
1550     {
1551       /* The user specified a FPU.  */
1552       for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1553         {
1554           if (streq (all_fpus[i].name, target_fpu_name))
1555             {
1556               arm_fpu_arch = all_fpus[i].fpu;
1557               arm_fpu_tune = arm_fpu_arch;
1558               arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1559               break;
1560             }
1561         }
1562       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1563         error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1564     }
1565   else
1566     {
1567 #ifdef FPUTYPE_DEFAULT
1568       /* Use the default if it is specified for this platform.  */
1569       arm_fpu_arch = FPUTYPE_DEFAULT;
1570       arm_fpu_tune = FPUTYPE_DEFAULT;
1571 #else
1572       /* Pick one based on CPU type.  */
1573       /* ??? Some targets assume FPA is the default.
1574       if ((insn_flags & FL_VFP) != 0)
1575         arm_fpu_arch = FPUTYPE_VFP;
1576       else
1577       */
1578       if (arm_arch_cirrus)
1579         arm_fpu_arch = FPUTYPE_MAVERICK;
1580       else
1581         arm_fpu_arch = FPUTYPE_FPA_EMU2;
1582 #endif
1583       if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1584         arm_fpu_tune = FPUTYPE_FPA;
1585       else
1586         arm_fpu_tune = arm_fpu_arch;
1587       arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1588       gcc_assert (arm_fp_model != ARM_FP_MODEL_UNKNOWN);
1589     }
1590
1591   if (target_float_abi_name != NULL)
1592     {
1593       /* The user specified a FP ABI.  */
1594       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1595         {
1596           if (streq (all_float_abis[i].name, target_float_abi_name))
1597             {
1598               arm_float_abi = all_float_abis[i].abi_type;
1599               break;
1600             }
1601         }
1602       if (i == ARRAY_SIZE (all_float_abis))
1603         error ("invalid floating point abi: -mfloat-abi=%s",
1604                target_float_abi_name);
1605     }
1606   else
1607     arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1608
1609   if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1610     sorry ("-mfloat-abi=hard and VFP");
1611
1612   if (TARGET_AAPCS_BASED
1613       && (arm_fp_model == ARM_FP_MODEL_FPA))
1614     error ("FPA is unsupported in the AAPCS");
1615
1616   if (TARGET_AAPCS_BASED)
1617     {
1618       if (TARGET_CALLER_INTERWORKING)
1619         error ("AAPCS does not support -mcaller-super-interworking");
1620       else
1621         if (TARGET_CALLEE_INTERWORKING)
1622           error ("AAPCS does not support -mcallee-super-interworking");
1623     }
1624
1625   /* FPA and iWMMXt are incompatible because the insn encodings overlap.
1626      VFP and iWMMXt can theoretically coexist, but it's unlikely such silicon
1627      will ever exist.  GCC makes no attempt to support this combination.  */
1628   if (TARGET_IWMMXT && !TARGET_SOFT_FLOAT)
1629     sorry ("iWMMXt and hardware floating point");
1630
1631   /* ??? iWMMXt insn patterns need auditing for Thumb-2.  */
1632   if (TARGET_THUMB2 && TARGET_IWMMXT)
1633     sorry ("Thumb-2 iWMMXt");
1634
1635   /* __fp16 support currently assumes the core has ldrh.  */
1636   if (!arm_arch4 && arm_fp16_format != ARM_FP16_FORMAT_NONE)
1637     sorry ("__fp16 and no ldrh");
1638
1639   /* If soft-float is specified then don't use FPU.  */
1640   if (TARGET_SOFT_FLOAT)
1641     arm_fpu_arch = FPUTYPE_NONE;
1642
1643   /* For arm2/3 there is no need to do any scheduling if there is only
1644      a floating point emulator, or we are doing software floating-point.  */
1645   if ((TARGET_SOFT_FLOAT
1646        || arm_fpu_tune == FPUTYPE_FPA_EMU2
1647        || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1648       && (tune_flags & FL_MODE32) == 0)
1649     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1650
1651   if (target_thread_switch)
1652     {
1653       if (strcmp (target_thread_switch, "soft") == 0)
1654         target_thread_pointer = TP_SOFT;
1655       else if (strcmp (target_thread_switch, "auto") == 0)
1656         target_thread_pointer = TP_AUTO;
1657       else if (strcmp (target_thread_switch, "cp15") == 0)
1658         target_thread_pointer = TP_CP15;
1659       else
1660         error ("invalid thread pointer option: -mtp=%s", target_thread_switch);
1661     }
1662
1663   /* Use the cp15 method if it is available.  */
1664   if (target_thread_pointer == TP_AUTO)
1665     {
1666       if (arm_arch6k && !TARGET_THUMB)
1667         target_thread_pointer = TP_CP15;
1668       else
1669         target_thread_pointer = TP_SOFT;
1670     }
1671
1672   if (TARGET_HARD_TP && TARGET_THUMB1)
1673     error ("can not use -mtp=cp15 with 16-bit Thumb");
1674
1675   /* Override the default structure alignment for AAPCS ABI.  */
1676   if (TARGET_AAPCS_BASED)
1677     arm_structure_size_boundary = 8;
1678
1679   if (structure_size_string != NULL)
1680     {
1681       int size = strtol (structure_size_string, NULL, 0);
1682
1683       if (size == 8 || size == 32
1684           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1685         arm_structure_size_boundary = size;
1686       else
1687         warning (0, "structure size boundary can only be set to %s",
1688                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1689     }
1690
1691   if (!TARGET_ARM && TARGET_VXWORKS_RTP && flag_pic)
1692     {
1693       error ("RTP PIC is incompatible with Thumb");
1694       flag_pic = 0;
1695     }
1696
1697   /* If stack checking is disabled, we can use r10 as the PIC register,
1698      which keeps r9 available.  The EABI specifies r9 as the PIC register.  */
1699   if (flag_pic && TARGET_SINGLE_PIC_BASE)
1700     {
1701       if (TARGET_VXWORKS_RTP)
1702         warning (0, "RTP PIC is incompatible with -msingle-pic-base");
1703       arm_pic_register = (TARGET_APCS_STACK || TARGET_AAPCS_BASED) ? 9 : 10;
1704     }
1705
1706   if (flag_pic && TARGET_VXWORKS_RTP)
1707     arm_pic_register = 9;
1708
1709   if (arm_pic_register_string != NULL)
1710     {
1711       int pic_register = decode_reg_name (arm_pic_register_string);
1712
1713       if (!flag_pic)
1714         warning (0, "-mpic-register= is useless without -fpic");
1715
1716       /* Prevent the user from choosing an obviously stupid PIC register.  */
1717       else if (pic_register < 0 || call_used_regs[pic_register]
1718                || pic_register == HARD_FRAME_POINTER_REGNUM
1719                || pic_register == STACK_POINTER_REGNUM
1720                || pic_register >= PC_REGNUM
1721                || (TARGET_VXWORKS_RTP
1722                    && (unsigned int) pic_register != arm_pic_register))
1723         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1724       else
1725         arm_pic_register = pic_register;
1726     }
1727
1728   /* Enable -mfix-cortex-m3-ldrd by default for Cortex-M3 cores.  */
1729   if (fix_cm3_ldrd == 2)
1730     {
1731       if (selected_cpu == cortexm3)
1732         fix_cm3_ldrd = 1;
1733       else
1734         fix_cm3_ldrd = 0;
1735     }
1736
1737   /* ??? We might want scheduling for thumb2.  */
1738   if (TARGET_THUMB && flag_schedule_insns)
1739     {
1740       /* Don't warn since it's on by default in -O2.  */
1741       flag_schedule_insns = 0;
1742     }
1743
1744   if (optimize_size)
1745     {
1746       arm_constant_limit = 1;
1747
1748       /* If optimizing for size, bump the number of instructions that we
1749          are prepared to conditionally execute (even on a StrongARM).  */
1750       max_insns_skipped = 6;
1751     }
1752   else
1753     {
1754       /* For processors with load scheduling, it never costs more than
1755          2 cycles to load a constant, and the load scheduler may well
1756          reduce that to 1.  */
1757       if (arm_ld_sched)
1758         arm_constant_limit = 1;
1759
1760       /* On XScale the longer latency of a load makes it more difficult
1761          to achieve a good schedule, so it's faster to synthesize
1762          constants that can be done in two insns.  */
1763       if (arm_tune_xscale)
1764         arm_constant_limit = 2;
1765
1766       /* StrongARM has early execution of branches, so a sequence
1767          that is worth skipping is shorter.  */
1768       if (arm_tune_strongarm)
1769         max_insns_skipped = 3;
1770     }
1771
1772   /* Register global variables with the garbage collector.  */
1773   arm_add_gc_roots ();
1774 }
1775
1776 static void
1777 arm_add_gc_roots (void)
1778 {
1779   gcc_obstack_init(&minipool_obstack);
1780   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1781 }
1782 \f
1783 /* A table of known ARM exception types.
1784    For use with the interrupt function attribute.  */
1785
1786 typedef struct
1787 {
1788   const char *const arg;
1789   const unsigned long return_value;
1790 }
1791 isr_attribute_arg;
1792
1793 static const isr_attribute_arg isr_attribute_args [] =
1794 {
1795   { "IRQ",   ARM_FT_ISR },
1796   { "irq",   ARM_FT_ISR },
1797   { "FIQ",   ARM_FT_FIQ },
1798   { "fiq",   ARM_FT_FIQ },
1799   { "ABORT", ARM_FT_ISR },
1800   { "abort", ARM_FT_ISR },
1801   { "ABORT", ARM_FT_ISR },
1802   { "abort", ARM_FT_ISR },
1803   { "UNDEF", ARM_FT_EXCEPTION },
1804   { "undef", ARM_FT_EXCEPTION },
1805   { "SWI",   ARM_FT_EXCEPTION },
1806   { "swi",   ARM_FT_EXCEPTION },
1807   { NULL,    ARM_FT_NORMAL }
1808 };
1809
1810 /* Returns the (interrupt) function type of the current
1811    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1812
1813 static unsigned long
1814 arm_isr_value (tree argument)
1815 {
1816   const isr_attribute_arg * ptr;
1817   const char *              arg;
1818
1819   if (!arm_arch_notm)
1820     return ARM_FT_NORMAL | ARM_FT_STACKALIGN;
1821
1822   /* No argument - default to IRQ.  */
1823   if (argument == NULL_TREE)
1824     return ARM_FT_ISR;
1825
1826   /* Get the value of the argument.  */
1827   if (TREE_VALUE (argument) == NULL_TREE
1828       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1829     return ARM_FT_UNKNOWN;
1830
1831   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1832
1833   /* Check it against the list of known arguments.  */
1834   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1835     if (streq (arg, ptr->arg))
1836       return ptr->return_value;
1837
1838   /* An unrecognized interrupt type.  */
1839   return ARM_FT_UNKNOWN;
1840 }
1841
1842 /* Computes the type of the current function.  */
1843
1844 static unsigned long
1845 arm_compute_func_type (void)
1846 {
1847   unsigned long type = ARM_FT_UNKNOWN;
1848   tree a;
1849   tree attr;
1850
1851   gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
1852
1853   /* Decide if the current function is volatile.  Such functions
1854      never return, and many memory cycles can be saved by not storing
1855      register values that will never be needed again.  This optimization
1856      was added to speed up context switching in a kernel application.  */
1857   if (optimize > 0
1858       && (TREE_NOTHROW (current_function_decl)
1859           || !(flag_unwind_tables
1860                || (flag_exceptions && !USING_SJLJ_EXCEPTIONS)))
1861       && TREE_THIS_VOLATILE (current_function_decl))
1862     type |= ARM_FT_VOLATILE;
1863
1864   if (cfun->static_chain_decl != NULL)
1865     type |= ARM_FT_NESTED;
1866
1867   attr = DECL_ATTRIBUTES (current_function_decl);
1868
1869   a = lookup_attribute ("naked", attr);
1870   if (a != NULL_TREE)
1871     type |= ARM_FT_NAKED;
1872
1873   a = lookup_attribute ("isr", attr);
1874   if (a == NULL_TREE)
1875     a = lookup_attribute ("interrupt", attr);
1876
1877   if (a == NULL_TREE)
1878     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1879   else
1880     type |= arm_isr_value (TREE_VALUE (a));
1881
1882   return type;
1883 }
1884
1885 /* Returns the type of the current function.  */
1886
1887 unsigned long
1888 arm_current_func_type (void)
1889 {
1890   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1891     cfun->machine->func_type = arm_compute_func_type ();
1892
1893   return cfun->machine->func_type;
1894 }
1895
1896 bool
1897 arm_allocate_stack_slots_for_args (void)
1898 {
1899   /* Naked functions should not allocate stack slots for arguments.  */
1900   return !IS_NAKED (arm_current_func_type ());
1901 }
1902
1903 \f
1904 /* Return 1 if it is possible to return using a single instruction.
1905    If SIBLING is non-null, this is a test for a return before a sibling
1906    call.  SIBLING is the call insn, so we can examine its register usage.  */
1907
1908 int
1909 use_return_insn (int iscond, rtx sibling)
1910 {
1911   int regno;
1912   unsigned int func_type;
1913   unsigned long saved_int_regs;
1914   unsigned HOST_WIDE_INT stack_adjust;
1915   arm_stack_offsets *offsets;
1916
1917   /* Never use a return instruction before reload has run.  */
1918   if (!reload_completed)
1919     return 0;
1920
1921   func_type = arm_current_func_type ();
1922
1923   /* Naked, volatile and stack alignment functions need special
1924      consideration.  */
1925   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED | ARM_FT_STACKALIGN))
1926     return 0;
1927
1928   /* So do interrupt functions that use the frame pointer and Thumb
1929      interrupt functions.  */
1930   if (IS_INTERRUPT (func_type) && (frame_pointer_needed || TARGET_THUMB))
1931     return 0;
1932
1933   offsets = arm_get_frame_offsets ();
1934   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1935
1936   /* As do variadic functions.  */
1937   if (crtl->args.pretend_args_size
1938       || cfun->machine->uses_anonymous_args
1939       /* Or if the function calls __builtin_eh_return () */
1940       || crtl->calls_eh_return
1941       /* Or if the function calls alloca */
1942       || cfun->calls_alloca
1943       /* Or if there is a stack adjustment.  However, if the stack pointer
1944          is saved on the stack, we can use a pre-incrementing stack load.  */
1945       || !(stack_adjust == 0 || (TARGET_APCS_FRAME && frame_pointer_needed
1946                                  && stack_adjust == 4)))
1947     return 0;
1948
1949   saved_int_regs = offsets->saved_regs_mask;
1950
1951   /* Unfortunately, the insn
1952
1953        ldmib sp, {..., sp, ...}
1954
1955      triggers a bug on most SA-110 based devices, such that the stack
1956      pointer won't be correctly restored if the instruction takes a
1957      page fault.  We work around this problem by popping r3 along with
1958      the other registers, since that is never slower than executing
1959      another instruction.
1960
1961      We test for !arm_arch5 here, because code for any architecture
1962      less than this could potentially be run on one of the buggy
1963      chips.  */
1964   if (stack_adjust == 4 && !arm_arch5 && TARGET_ARM)
1965     {
1966       /* Validate that r3 is a call-clobbered register (always true in
1967          the default abi) ...  */
1968       if (!call_used_regs[3])
1969         return 0;
1970
1971       /* ... that it isn't being used for a return value ... */
1972       if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
1973         return 0;
1974
1975       /* ... or for a tail-call argument ...  */
1976       if (sibling)
1977         {
1978           gcc_assert (GET_CODE (sibling) == CALL_INSN);
1979
1980           if (find_regno_fusage (sibling, USE, 3))
1981             return 0;
1982         }
1983
1984       /* ... and that there are no call-saved registers in r0-r2
1985          (always true in the default ABI).  */
1986       if (saved_int_regs & 0x7)
1987         return 0;
1988     }
1989
1990   /* Can't be done if interworking with Thumb, and any registers have been
1991      stacked.  */
1992   if (TARGET_INTERWORK && saved_int_regs != 0 && !IS_INTERRUPT(func_type))
1993     return 0;
1994
1995   /* On StrongARM, conditional returns are expensive if they aren't
1996      taken and multiple registers have been stacked.  */
1997   if (iscond && arm_tune_strongarm)
1998     {
1999       /* Conditional return when just the LR is stored is a simple
2000          conditional-load instruction, that's not expensive.  */
2001       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
2002         return 0;
2003
2004       if (flag_pic 
2005           && arm_pic_register != INVALID_REGNUM
2006           && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
2007         return 0;
2008     }
2009
2010   /* If there are saved registers but the LR isn't saved, then we need
2011      two instructions for the return.  */
2012   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
2013     return 0;
2014
2015   /* Can't be done if any of the FPA regs are pushed,
2016      since this also requires an insn.  */
2017   if (TARGET_HARD_FLOAT && TARGET_FPA)
2018     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
2019       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
2020         return 0;
2021
2022   /* Likewise VFP regs.  */
2023   if (TARGET_HARD_FLOAT && TARGET_VFP)
2024     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
2025       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
2026         return 0;
2027
2028   if (TARGET_REALLY_IWMMXT)
2029     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
2030       if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
2031         return 0;
2032
2033   return 1;
2034 }
2035
2036 /* Return TRUE if int I is a valid immediate ARM constant.  */
2037
2038 int
2039 const_ok_for_arm (HOST_WIDE_INT i)
2040 {
2041   int lowbit;
2042
2043   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
2044      be all zero, or all one.  */
2045   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
2046       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
2047           != ((~(unsigned HOST_WIDE_INT) 0)
2048               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
2049     return FALSE;
2050
2051   i &= (unsigned HOST_WIDE_INT) 0xffffffff;
2052
2053   /* Fast return for 0 and small values.  We must do this for zero, since
2054      the code below can't handle that one case.  */
2055   if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
2056     return TRUE;
2057
2058   /* Get the number of trailing zeros.  */
2059   lowbit = ffs((int) i) - 1;
2060   
2061   /* Only even shifts are allowed in ARM mode so round down to the
2062      nearest even number.  */
2063   if (TARGET_ARM)
2064     lowbit &= ~1;
2065
2066   if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
2067     return TRUE;
2068
2069   if (TARGET_ARM)
2070     {
2071       /* Allow rotated constants in ARM mode.  */
2072       if (lowbit <= 4
2073            && ((i & ~0xc000003f) == 0
2074                || (i & ~0xf000000f) == 0
2075                || (i & ~0xfc000003) == 0))
2076         return TRUE;
2077     }
2078   else
2079     {
2080       HOST_WIDE_INT v;
2081
2082       /* Allow repeated pattern.  */
2083       v = i & 0xff;
2084       v |= v << 16;
2085       if (i == v || i == (v | (v << 8)))
2086         return TRUE;
2087     }
2088
2089   return FALSE;
2090 }
2091
2092 /* Return true if I is a valid constant for the operation CODE.  */
2093 static int
2094 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
2095 {
2096   if (const_ok_for_arm (i))
2097     return 1;
2098
2099   switch (code)
2100     {
2101     case PLUS:
2102     case COMPARE:
2103     case EQ:
2104     case NE:
2105     case GT:
2106     case LE:
2107     case LT:
2108     case GE:
2109     case GEU:
2110     case LTU:
2111     case GTU:
2112     case LEU:
2113     case UNORDERED:
2114     case ORDERED:
2115     case UNEQ:
2116     case UNGE:
2117     case UNLT:
2118     case UNGT:
2119     case UNLE:
2120       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
2121
2122     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
2123     case XOR:
2124       return 0;
2125
2126     case IOR:
2127       if (TARGET_THUMB2)
2128         return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2129       return 0;
2130
2131     case AND:
2132       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2133
2134     default:
2135       gcc_unreachable ();
2136     }
2137 }
2138
2139 /* Emit a sequence of insns to handle a large constant.
2140    CODE is the code of the operation required, it can be any of SET, PLUS,
2141    IOR, AND, XOR, MINUS;
2142    MODE is the mode in which the operation is being performed;
2143    VAL is the integer to operate on;
2144    SOURCE is the other operand (a register, or a null-pointer for SET);
2145    SUBTARGETS means it is safe to create scratch registers if that will
2146    either produce a simpler sequence, or we will want to cse the values.
2147    Return value is the number of insns emitted.  */
2148
2149 /* ??? Tweak this for thumb2.  */
2150 int
2151 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
2152                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
2153 {
2154   rtx cond;
2155
2156   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
2157     cond = COND_EXEC_TEST (PATTERN (insn));
2158   else
2159     cond = NULL_RTX;
2160
2161   if (subtargets || code == SET
2162       || (GET_CODE (target) == REG && GET_CODE (source) == REG
2163           && REGNO (target) != REGNO (source)))
2164     {
2165       /* After arm_reorg has been called, we can't fix up expensive
2166          constants by pushing them into memory so we must synthesize
2167          them in-line, regardless of the cost.  This is only likely to
2168          be more costly on chips that have load delay slots and we are
2169          compiling without running the scheduler (so no splitting
2170          occurred before the final instruction emission).
2171
2172          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
2173       */
2174       if (!after_arm_reorg
2175           && !cond
2176           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
2177                                 1, 0)
2178               > arm_constant_limit + (code != SET)))
2179         {
2180           if (code == SET)
2181             {
2182               /* Currently SET is the only monadic value for CODE, all
2183                  the rest are diadic.  */
2184               if (TARGET_USE_MOVT)
2185                 arm_emit_movpair (target, GEN_INT (val));
2186               else
2187                 emit_set_insn (target, GEN_INT (val));
2188
2189               return 1;
2190             }
2191           else
2192             {
2193               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
2194
2195               if (TARGET_USE_MOVT)
2196                 arm_emit_movpair (temp, GEN_INT (val));
2197               else
2198                 emit_set_insn (temp, GEN_INT (val));
2199
2200               /* For MINUS, the value is subtracted from, since we never
2201                  have subtraction of a constant.  */
2202               if (code == MINUS)
2203                 emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
2204               else
2205                 emit_set_insn (target,
2206                                gen_rtx_fmt_ee (code, mode, source, temp));
2207               return 2;
2208             }
2209         }
2210     }
2211
2212   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
2213                            1);
2214 }
2215
2216 /* Return the number of ARM instructions required to synthesize the given
2217    constant.  */
2218 static int
2219 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
2220 {
2221   HOST_WIDE_INT temp1;
2222   int num_insns = 0;
2223   do
2224     {
2225       int end;
2226
2227       if (i <= 0)
2228         i += 32;
2229       if (remainder & (3 << (i - 2)))
2230         {
2231           end = i - 8;
2232           if (end < 0)
2233             end += 32;
2234           temp1 = remainder & ((0x0ff << end)
2235                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
2236           remainder &= ~temp1;
2237           num_insns++;
2238           i -= 6;
2239         }
2240       i -= 2;
2241     } while (remainder);
2242   return num_insns;
2243 }
2244
2245 /* Emit an instruction with the indicated PATTERN.  If COND is
2246    non-NULL, conditionalize the execution of the instruction on COND
2247    being true.  */
2248
2249 static void
2250 emit_constant_insn (rtx cond, rtx pattern)
2251 {
2252   if (cond)
2253     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
2254   emit_insn (pattern);
2255 }
2256
2257 /* As above, but extra parameter GENERATE which, if clear, suppresses
2258    RTL generation.  */
2259 /* ??? This needs more work for thumb2.  */
2260
2261 static int
2262 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
2263                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
2264                   int generate)
2265 {
2266   int can_invert = 0;
2267   int can_negate = 0;
2268   int can_negate_initial = 0;
2269   int can_shift = 0;
2270   int i;
2271   int num_bits_set = 0;
2272   int set_sign_bit_copies = 0;
2273   int clear_sign_bit_copies = 0;
2274   int clear_zero_bit_copies = 0;
2275   int set_zero_bit_copies = 0;
2276   int insns = 0;
2277   unsigned HOST_WIDE_INT temp1, temp2;
2278   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
2279
2280   /* Find out which operations are safe for a given CODE.  Also do a quick
2281      check for degenerate cases; these can occur when DImode operations
2282      are split.  */
2283   switch (code)
2284     {
2285     case SET:
2286       can_invert = 1;
2287       can_shift = 1;
2288       can_negate = 1;
2289       break;
2290
2291     case PLUS:
2292       can_negate = 1;
2293       can_negate_initial = 1;
2294       break;
2295
2296     case IOR:
2297       if (remainder == 0xffffffff)
2298         {
2299           if (generate)
2300             emit_constant_insn (cond,
2301                                 gen_rtx_SET (VOIDmode, target,
2302                                              GEN_INT (ARM_SIGN_EXTEND (val))));
2303           return 1;
2304         }
2305
2306       if (remainder == 0)
2307         {
2308           if (reload_completed && rtx_equal_p (target, source))
2309             return 0;
2310
2311           if (generate)
2312             emit_constant_insn (cond,
2313                                 gen_rtx_SET (VOIDmode, target, source));
2314           return 1;
2315         }
2316
2317       if (TARGET_THUMB2)
2318         can_invert = 1;
2319       break;
2320
2321     case AND:
2322       if (remainder == 0)
2323         {
2324           if (generate)
2325             emit_constant_insn (cond,
2326                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
2327           return 1;
2328         }
2329       if (remainder == 0xffffffff)
2330         {
2331           if (reload_completed && rtx_equal_p (target, source))
2332             return 0;
2333           if (generate)
2334             emit_constant_insn (cond,
2335                                 gen_rtx_SET (VOIDmode, target, source));
2336           return 1;
2337         }
2338       can_invert = 1;
2339       break;
2340
2341     case XOR:
2342       if (remainder == 0)
2343         {
2344           if (reload_completed && rtx_equal_p (target, source))
2345             return 0;
2346           if (generate)
2347             emit_constant_insn (cond,
2348                                 gen_rtx_SET (VOIDmode, target, source));
2349           return 1;
2350         }
2351
2352       /* We don't know how to handle other cases yet.  */
2353       gcc_assert (remainder == 0xffffffff);
2354
2355       if (generate)
2356         emit_constant_insn (cond,
2357                             gen_rtx_SET (VOIDmode, target,
2358                                          gen_rtx_NOT (mode, source)));
2359       return 1;
2360
2361     case MINUS:
2362       /* We treat MINUS as (val - source), since (source - val) is always
2363          passed as (source + (-val)).  */
2364       if (remainder == 0)
2365         {
2366           if (generate)
2367             emit_constant_insn (cond,
2368                                 gen_rtx_SET (VOIDmode, target,
2369                                              gen_rtx_NEG (mode, source)));
2370           return 1;
2371         }
2372       if (const_ok_for_arm (val))
2373         {
2374           if (generate)
2375             emit_constant_insn (cond,
2376                                 gen_rtx_SET (VOIDmode, target,
2377                                              gen_rtx_MINUS (mode, GEN_INT (val),
2378                                                             source)));
2379           return 1;
2380         }
2381       can_negate = 1;
2382
2383       break;
2384
2385     default:
2386       gcc_unreachable ();
2387     }
2388
2389   /* If we can do it in one insn get out quickly.  */
2390   if (const_ok_for_arm (val)
2391       || (can_negate_initial && const_ok_for_arm (-val))
2392       || (can_invert && const_ok_for_arm (~val)))
2393     {
2394       if (generate)
2395         emit_constant_insn (cond,
2396                             gen_rtx_SET (VOIDmode, target,
2397                                          (source
2398                                           ? gen_rtx_fmt_ee (code, mode, source,
2399                                                             GEN_INT (val))
2400                                           : GEN_INT (val))));
2401       return 1;
2402     }
2403
2404   /* Calculate a few attributes that may be useful for specific
2405      optimizations.  */
2406   /* Count number of leading zeros.  */
2407   for (i = 31; i >= 0; i--)
2408     {
2409       if ((remainder & (1 << i)) == 0)
2410         clear_sign_bit_copies++;
2411       else
2412         break;
2413     }
2414
2415   /* Count number of leading 1's.  */
2416   for (i = 31; i >= 0; i--)
2417     {
2418       if ((remainder & (1 << i)) != 0)
2419         set_sign_bit_copies++;
2420       else
2421         break;
2422     }
2423
2424   /* Count number of trailing zero's.  */
2425   for (i = 0; i <= 31; i++)
2426     {
2427       if ((remainder & (1 << i)) == 0)
2428         clear_zero_bit_copies++;
2429       else
2430         break;
2431     }
2432
2433   /* Count number of trailing 1's.  */
2434   for (i = 0; i <= 31; i++)
2435     {
2436       if ((remainder & (1 << i)) != 0)
2437         set_zero_bit_copies++;
2438       else
2439         break;
2440     }
2441
2442   switch (code)
2443     {
2444     case SET:
2445       /* See if we can use movw.  */
2446       if (arm_arch_thumb2 && (remainder & 0xffff0000) == 0)
2447         {
2448           if (generate)
2449             emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
2450                                                    GEN_INT (val)));
2451           return 1;
2452         }
2453
2454       /* See if we can do this by sign_extending a constant that is known
2455          to be negative.  This is a good, way of doing it, since the shift
2456          may well merge into a subsequent insn.  */
2457       if (set_sign_bit_copies > 1)
2458         {
2459           if (const_ok_for_arm
2460               (temp1 = ARM_SIGN_EXTEND (remainder
2461                                         << (set_sign_bit_copies - 1))))
2462             {
2463               if (generate)
2464                 {
2465                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2466                   emit_constant_insn (cond,
2467                                       gen_rtx_SET (VOIDmode, new_src,
2468                                                    GEN_INT (temp1)));
2469                   emit_constant_insn (cond,
2470                                       gen_ashrsi3 (target, new_src,
2471                                                    GEN_INT (set_sign_bit_copies - 1)));
2472                 }
2473               return 2;
2474             }
2475           /* For an inverted constant, we will need to set the low bits,
2476              these will be shifted out of harm's way.  */
2477           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
2478           if (const_ok_for_arm (~temp1))
2479             {
2480               if (generate)
2481                 {
2482                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2483                   emit_constant_insn (cond,
2484                                       gen_rtx_SET (VOIDmode, new_src,
2485                                                    GEN_INT (temp1)));
2486                   emit_constant_insn (cond,
2487                                       gen_ashrsi3 (target, new_src,
2488                                                    GEN_INT (set_sign_bit_copies - 1)));
2489                 }
2490               return 2;
2491             }
2492         }
2493
2494       /* See if we can calculate the value as the difference between two
2495          valid immediates.  */
2496       if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
2497         {
2498           int topshift = clear_sign_bit_copies & ~1;
2499
2500           temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
2501                                    & (0xff000000 >> topshift));
2502
2503           /* If temp1 is zero, then that means the 9 most significant
2504              bits of remainder were 1 and we've caused it to overflow.
2505              When topshift is 0 we don't need to do anything since we
2506              can borrow from 'bit 32'.  */
2507           if (temp1 == 0 && topshift != 0)
2508             temp1 = 0x80000000 >> (topshift - 1);
2509
2510           temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
2511
2512           if (const_ok_for_arm (temp2))
2513             {
2514               if (generate)
2515                 {
2516                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2517                   emit_constant_insn (cond,
2518                                       gen_rtx_SET (VOIDmode, new_src,
2519                                                    GEN_INT (temp1)));
2520                   emit_constant_insn (cond,
2521                                       gen_addsi3 (target, new_src,
2522                                                   GEN_INT (-temp2)));
2523                 }
2524
2525               return 2;
2526             }
2527         }
2528
2529       /* See if we can generate this by setting the bottom (or the top)
2530          16 bits, and then shifting these into the other half of the
2531          word.  We only look for the simplest cases, to do more would cost
2532          too much.  Be careful, however, not to generate this when the
2533          alternative would take fewer insns.  */
2534       if (val & 0xffff0000)
2535         {
2536           temp1 = remainder & 0xffff0000;
2537           temp2 = remainder & 0x0000ffff;
2538
2539           /* Overlaps outside this range are best done using other methods.  */
2540           for (i = 9; i < 24; i++)
2541             {
2542               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
2543                   && !const_ok_for_arm (temp2))
2544                 {
2545                   rtx new_src = (subtargets
2546                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2547                                  : target);
2548                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2549                                             source, subtargets, generate);
2550                   source = new_src;
2551                   if (generate)
2552                     emit_constant_insn
2553                       (cond,
2554                        gen_rtx_SET
2555                        (VOIDmode, target,
2556                         gen_rtx_IOR (mode,
2557                                      gen_rtx_ASHIFT (mode, source,
2558                                                      GEN_INT (i)),
2559                                      source)));
2560                   return insns + 1;
2561                 }
2562             }
2563
2564           /* Don't duplicate cases already considered.  */
2565           for (i = 17; i < 24; i++)
2566             {
2567               if (((temp1 | (temp1 >> i)) == remainder)
2568                   && !const_ok_for_arm (temp1))
2569                 {
2570                   rtx new_src = (subtargets
2571                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2572                                  : target);
2573                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2574                                             source, subtargets, generate);
2575                   source = new_src;
2576                   if (generate)
2577                     emit_constant_insn
2578                       (cond,
2579                        gen_rtx_SET (VOIDmode, target,
2580                                     gen_rtx_IOR
2581                                     (mode,
2582                                      gen_rtx_LSHIFTRT (mode, source,
2583                                                        GEN_INT (i)),
2584                                      source)));
2585                   return insns + 1;
2586                 }
2587             }
2588         }
2589       break;
2590
2591     case IOR:
2592     case XOR:
2593       /* If we have IOR or XOR, and the constant can be loaded in a
2594          single instruction, and we can find a temporary to put it in,
2595          then this can be done in two instructions instead of 3-4.  */
2596       if (subtargets
2597           /* TARGET can't be NULL if SUBTARGETS is 0 */
2598           || (reload_completed && !reg_mentioned_p (target, source)))
2599         {
2600           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
2601             {
2602               if (generate)
2603                 {
2604                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2605
2606                   emit_constant_insn (cond,
2607                                       gen_rtx_SET (VOIDmode, sub,
2608                                                    GEN_INT (val)));
2609                   emit_constant_insn (cond,
2610                                       gen_rtx_SET (VOIDmode, target,
2611                                                    gen_rtx_fmt_ee (code, mode,
2612                                                                    source, sub)));
2613                 }
2614               return 2;
2615             }
2616         }
2617
2618       if (code == XOR)
2619         break;
2620
2621       /*  Convert.
2622           x = y | constant ( which is composed of set_sign_bit_copies of leading 1s
2623                              and the remainder 0s for e.g. 0xfff00000)
2624           x = ~(~(y ashift set_sign_bit_copies) lshiftrt set_sign_bit_copies)
2625
2626           This can be done in 2 instructions by using shifts with mov or mvn.
2627           e.g. for
2628           x = x | 0xfff00000;
2629           we generate.
2630           mvn   r0, r0, asl #12
2631           mvn   r0, r0, lsr #12  */
2632       if (set_sign_bit_copies > 8
2633           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
2634         {
2635           if (generate)
2636             {
2637               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2638               rtx shift = GEN_INT (set_sign_bit_copies);
2639
2640               emit_constant_insn
2641                 (cond,
2642                  gen_rtx_SET (VOIDmode, sub,
2643                               gen_rtx_NOT (mode,
2644                                            gen_rtx_ASHIFT (mode,
2645                                                            source,
2646                                                            shift))));
2647               emit_constant_insn
2648                 (cond,
2649                  gen_rtx_SET (VOIDmode, target,
2650                               gen_rtx_NOT (mode,
2651                                            gen_rtx_LSHIFTRT (mode, sub,
2652                                                              shift))));
2653             }
2654           return 2;
2655         }
2656
2657       /* Convert
2658           x = y | constant (which has set_zero_bit_copies number of trailing ones).
2659            to
2660           x = ~((~y lshiftrt set_zero_bit_copies) ashift set_zero_bit_copies).
2661
2662           For eg. r0 = r0 | 0xfff
2663                mvn      r0, r0, lsr #12
2664                mvn      r0, r0, asl #12
2665
2666       */
2667       if (set_zero_bit_copies > 8
2668           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2669         {
2670           if (generate)
2671             {
2672               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2673               rtx shift = GEN_INT (set_zero_bit_copies);
2674
2675               emit_constant_insn
2676                 (cond,
2677                  gen_rtx_SET (VOIDmode, sub,
2678                               gen_rtx_NOT (mode,
2679                                            gen_rtx_LSHIFTRT (mode,
2680                                                              source,
2681                                                              shift))));
2682               emit_constant_insn
2683                 (cond,
2684                  gen_rtx_SET (VOIDmode, target,
2685                               gen_rtx_NOT (mode,
2686                                            gen_rtx_ASHIFT (mode, sub,
2687                                                            shift))));
2688             }
2689           return 2;
2690         }
2691
2692       /* This will never be reached for Thumb2 because orn is a valid
2693          instruction. This is for Thumb1 and the ARM 32 bit cases.
2694
2695          x = y | constant (such that ~constant is a valid constant)
2696          Transform this to
2697          x = ~(~y & ~constant).
2698       */
2699       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
2700         {
2701           if (generate)
2702             {
2703               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2704               emit_constant_insn (cond,
2705                                   gen_rtx_SET (VOIDmode, sub,
2706                                                gen_rtx_NOT (mode, source)));
2707               source = sub;
2708               if (subtargets)
2709                 sub = gen_reg_rtx (mode);
2710               emit_constant_insn (cond,
2711                                   gen_rtx_SET (VOIDmode, sub,
2712                                                gen_rtx_AND (mode, source,
2713                                                             GEN_INT (temp1))));
2714               emit_constant_insn (cond,
2715                                   gen_rtx_SET (VOIDmode, target,
2716                                                gen_rtx_NOT (mode, sub)));
2717             }
2718           return 3;
2719         }
2720       break;
2721
2722     case AND:
2723       /* See if two shifts will do 2 or more insn's worth of work.  */
2724       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
2725         {
2726           HOST_WIDE_INT shift_mask = ((0xffffffff
2727                                        << (32 - clear_sign_bit_copies))
2728                                       & 0xffffffff);
2729
2730           if ((remainder | shift_mask) != 0xffffffff)
2731             {
2732               if (generate)
2733                 {
2734                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2735                   insns = arm_gen_constant (AND, mode, cond,
2736                                             remainder | shift_mask,
2737                                             new_src, source, subtargets, 1);
2738                   source = new_src;
2739                 }
2740               else
2741                 {
2742                   rtx targ = subtargets ? NULL_RTX : target;
2743                   insns = arm_gen_constant (AND, mode, cond,
2744                                             remainder | shift_mask,
2745                                             targ, source, subtargets, 0);
2746                 }
2747             }
2748
2749           if (generate)
2750             {
2751               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2752               rtx shift = GEN_INT (clear_sign_bit_copies);
2753
2754               emit_insn (gen_ashlsi3 (new_src, source, shift));
2755               emit_insn (gen_lshrsi3 (target, new_src, shift));
2756             }
2757
2758           return insns + 2;
2759         }
2760
2761       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2762         {
2763           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2764
2765           if ((remainder | shift_mask) != 0xffffffff)
2766             {
2767               if (generate)
2768                 {
2769                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2770
2771                   insns = arm_gen_constant (AND, mode, cond,
2772                                             remainder | shift_mask,
2773                                             new_src, source, subtargets, 1);
2774                   source = new_src;
2775                 }
2776               else
2777                 {
2778                   rtx targ = subtargets ? NULL_RTX : target;
2779
2780                   insns = arm_gen_constant (AND, mode, cond,
2781                                             remainder | shift_mask,
2782                                             targ, source, subtargets, 0);
2783                 }
2784             }
2785
2786           if (generate)
2787             {
2788               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2789               rtx shift = GEN_INT (clear_zero_bit_copies);
2790
2791               emit_insn (gen_lshrsi3 (new_src, source, shift));
2792               emit_insn (gen_ashlsi3 (target, new_src, shift));
2793             }
2794
2795           return insns + 2;
2796         }
2797
2798       break;
2799
2800     default:
2801       break;
2802     }
2803
2804   for (i = 0; i < 32; i++)
2805     if (remainder & (1 << i))
2806       num_bits_set++;
2807
2808   if ((code == AND)
2809       || (code != IOR && can_invert && num_bits_set > 16))
2810     remainder = (~remainder) & 0xffffffff;
2811   else if (code == PLUS && num_bits_set > 16)
2812     remainder = (-remainder) & 0xffffffff;
2813   else
2814     {
2815       can_invert = 0;
2816       can_negate = 0;
2817     }
2818
2819   /* Now try and find a way of doing the job in either two or three
2820      instructions.
2821      We start by looking for the largest block of zeros that are aligned on
2822      a 2-bit boundary, we then fill up the temps, wrapping around to the
2823      top of the word when we drop off the bottom.
2824      In the worst case this code should produce no more than four insns.
2825      Thumb-2 constants are shifted, not rotated, so the MSB is always the
2826      best place to start.  */
2827
2828   /* ??? Use thumb2 replicated constants when the high and low halfwords are
2829      the same.  */
2830   {
2831     int best_start = 0;
2832     if (!TARGET_THUMB2)
2833       {
2834         int best_consecutive_zeros = 0;
2835
2836         for (i = 0; i < 32; i += 2)
2837           {
2838             int consecutive_zeros = 0;
2839
2840             if (!(remainder & (3 << i)))
2841               {
2842                 while ((i < 32) && !(remainder & (3 << i)))
2843                   {
2844                     consecutive_zeros += 2;
2845                     i += 2;
2846                   }
2847                 if (consecutive_zeros > best_consecutive_zeros)
2848                   {
2849                     best_consecutive_zeros = consecutive_zeros;
2850                     best_start = i - consecutive_zeros;
2851                   }
2852                 i -= 2;
2853               }
2854           }
2855
2856         /* So long as it won't require any more insns to do so, it's
2857            desirable to emit a small constant (in bits 0...9) in the last
2858            insn.  This way there is more chance that it can be combined with
2859            a later addressing insn to form a pre-indexed load or store
2860            operation.  Consider:
2861
2862                    *((volatile int *)0xe0000100) = 1;
2863                    *((volatile int *)0xe0000110) = 2;
2864
2865            We want this to wind up as:
2866
2867                     mov rA, #0xe0000000
2868                     mov rB, #1
2869                     str rB, [rA, #0x100]
2870                     mov rB, #2
2871                     str rB, [rA, #0x110]
2872
2873            rather than having to synthesize both large constants from scratch.
2874
2875            Therefore, we calculate how many insns would be required to emit
2876            the constant starting from `best_start', and also starting from
2877            zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2878            yield a shorter sequence, we may as well use zero.  */
2879         if (best_start != 0
2880             && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2881             && (count_insns_for_constant (remainder, 0) <=
2882                 count_insns_for_constant (remainder, best_start)))
2883           best_start = 0;
2884       }
2885
2886     /* Now start emitting the insns.  */
2887     i = best_start;
2888     do
2889       {
2890         int end;
2891
2892         if (i <= 0)
2893           i += 32;
2894         if (remainder & (3 << (i - 2)))
2895           {
2896             end = i - 8;
2897             if (end < 0)
2898               end += 32;
2899             temp1 = remainder & ((0x0ff << end)
2900                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
2901             remainder &= ~temp1;
2902
2903             if (generate)
2904               {
2905                 rtx new_src, temp1_rtx;
2906
2907                 if (code == SET || code == MINUS)
2908                   {
2909                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
2910                     if (can_invert && code != MINUS)
2911                       temp1 = ~temp1;
2912                   }
2913                 else
2914                   {
2915                     if (remainder && subtargets)
2916                       new_src = gen_reg_rtx (mode);
2917                     else
2918                       new_src = target;
2919                     if (can_invert)
2920                       temp1 = ~temp1;
2921                     else if (can_negate)
2922                       temp1 = -temp1;
2923                   }
2924
2925                 temp1 = trunc_int_for_mode (temp1, mode);
2926                 temp1_rtx = GEN_INT (temp1);
2927
2928                 if (code == SET)
2929                   ;
2930                 else if (code == MINUS)
2931                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2932                 else
2933                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2934
2935                 emit_constant_insn (cond,
2936                                     gen_rtx_SET (VOIDmode, new_src,
2937                                                  temp1_rtx));
2938                 source = new_src;
2939               }
2940
2941             if (code == SET)
2942               {
2943                 can_invert = 0;
2944                 code = PLUS;
2945               }
2946             else if (code == MINUS)
2947               code = PLUS;
2948
2949             insns++;
2950             if (TARGET_ARM)
2951               i -= 6;
2952             else
2953               i -= 7;
2954           }
2955         /* Arm allows rotates by a multiple of two. Thumb-2 allows arbitrary
2956            shifts.  */
2957         if (TARGET_ARM)
2958           i -= 2;
2959         else
2960           i--;
2961       }
2962     while (remainder);
2963   }
2964
2965   return insns;
2966 }
2967
2968 /* Canonicalize a comparison so that we are more likely to recognize it.
2969    This can be done for a few constant compares, where we can make the
2970    immediate value easier to load.  */
2971
2972 enum rtx_code
2973 arm_canonicalize_comparison (enum rtx_code code, enum machine_mode mode,
2974                              rtx * op1)
2975 {
2976   unsigned HOST_WIDE_INT i = INTVAL (*op1);
2977   unsigned HOST_WIDE_INT maxval;
2978   maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
2979
2980   switch (code)
2981     {
2982     case EQ:
2983     case NE:
2984       return code;
2985
2986     case GT:
2987     case LE:
2988       if (i != maxval
2989           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2990         {
2991           *op1 = GEN_INT (i + 1);
2992           return code == GT ? GE : LT;
2993         }
2994       break;
2995
2996     case GE:
2997     case LT:
2998       if (i != ~maxval
2999           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
3000         {
3001           *op1 = GEN_INT (i - 1);
3002           return code == GE ? GT : LE;
3003         }
3004       break;
3005
3006     case GTU:
3007     case LEU:
3008       if (i != ~((unsigned HOST_WIDE_INT) 0)
3009           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
3010         {
3011           *op1 = GEN_INT (i + 1);
3012           return code == GTU ? GEU : LTU;
3013         }
3014       break;
3015
3016     case GEU:
3017     case LTU:
3018       if (i != 0
3019           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
3020         {
3021           *op1 = GEN_INT (i - 1);
3022           return code == GEU ? GTU : LEU;
3023         }
3024       break;
3025
3026     default:
3027       gcc_unreachable ();
3028     }
3029
3030   return code;
3031 }
3032
3033
3034 /* Define how to find the value returned by a function.  */
3035
3036 rtx
3037 arm_function_value(const_tree type, const_tree func ATTRIBUTE_UNUSED)
3038 {
3039   enum machine_mode mode;
3040   int unsignedp ATTRIBUTE_UNUSED;
3041   rtx r ATTRIBUTE_UNUSED;
3042
3043   mode = TYPE_MODE (type);
3044   /* Promote integer types.  */
3045   if (INTEGRAL_TYPE_P (type))
3046     PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
3047
3048   /* Promotes small structs returned in a register to full-word size
3049      for big-endian AAPCS.  */
3050   if (arm_return_in_msb (type))
3051     {
3052       HOST_WIDE_INT size = int_size_in_bytes (type);
3053       if (size % UNITS_PER_WORD != 0)
3054         {
3055           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
3056           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
3057         }
3058     }
3059
3060   return LIBCALL_VALUE(mode);
3061 }
3062
3063 /* Determine the amount of memory needed to store the possible return
3064    registers of an untyped call.  */
3065 int
3066 arm_apply_result_size (void)
3067 {
3068   int size = 16;
3069
3070   if (TARGET_ARM)
3071     {
3072       if (TARGET_HARD_FLOAT_ABI)
3073         {
3074           if (TARGET_FPA)
3075             size += 12;
3076           if (TARGET_MAVERICK)
3077             size += 8;
3078         }
3079       if (TARGET_IWMMXT_ABI)
3080         size += 8;
3081     }
3082
3083   return size;
3084 }
3085
3086 /* Decide whether a type should be returned in memory (true)
3087    or in a register (false).  This is called as the target hook
3088    TARGET_RETURN_IN_MEMORY.  */
3089 static bool
3090 arm_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
3091 {
3092   HOST_WIDE_INT size;
3093
3094   size = int_size_in_bytes (type);
3095
3096   /* Vector values should be returned using ARM registers, not memory (unless
3097      they're over 16 bytes, which will break since we only have four
3098      call-clobbered registers to play with).  */
3099   if (TREE_CODE (type) == VECTOR_TYPE)
3100     return (size < 0 || size > (4 * UNITS_PER_WORD));
3101
3102   if (!AGGREGATE_TYPE_P (type) &&
3103       !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
3104     /* All simple types are returned in registers.
3105        For AAPCS, complex types are treated the same as aggregates.  */
3106     return 0;
3107
3108   if (arm_abi != ARM_ABI_APCS)
3109     {
3110       /* ATPCS and later return aggregate types in memory only if they are
3111          larger than a word (or are variable size).  */
3112       return (size < 0 || size > UNITS_PER_WORD);
3113     }
3114
3115   /* For the arm-wince targets we choose to be compatible with Microsoft's
3116      ARM and Thumb compilers, which always return aggregates in memory.  */
3117 #ifndef ARM_WINCE
3118   /* All structures/unions bigger than one word are returned in memory.
3119      Also catch the case where int_size_in_bytes returns -1.  In this case
3120      the aggregate is either huge or of variable size, and in either case
3121      we will want to return it via memory and not in a register.  */
3122   if (size < 0 || size > UNITS_PER_WORD)
3123     return 1;
3124
3125   if (TREE_CODE (type) == RECORD_TYPE)
3126     {
3127       tree field;
3128
3129       /* For a struct the APCS says that we only return in a register
3130          if the type is 'integer like' and every addressable element
3131          has an offset of zero.  For practical purposes this means
3132          that the structure can have at most one non bit-field element
3133          and that this element must be the first one in the structure.  */
3134
3135       /* Find the first field, ignoring non FIELD_DECL things which will
3136          have been created by C++.  */
3137       for (field = TYPE_FIELDS (type);
3138            field && TREE_CODE (field) != FIELD_DECL;
3139            field = TREE_CHAIN (field))
3140         continue;
3141
3142       if (field == NULL)
3143         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
3144
3145       /* Check that the first field is valid for returning in a register.  */
3146
3147       /* ... Floats are not allowed */
3148       if (FLOAT_TYPE_P (TREE_TYPE (field)))
3149         return 1;
3150
3151       /* ... Aggregates that are not themselves valid for returning in
3152          a register are not allowed.  */
3153       if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
3154         return 1;
3155
3156       /* Now check the remaining fields, if any.  Only bitfields are allowed,
3157          since they are not addressable.  */
3158       for (field = TREE_CHAIN (field);
3159            field;
3160            field = TREE_CHAIN (field))
3161         {
3162           if (TREE_CODE (field) != FIELD_DECL)
3163             continue;
3164
3165           if (!DECL_BIT_FIELD_TYPE (field))
3166             return 1;
3167         }
3168
3169       return 0;
3170     }
3171
3172   if (TREE_CODE (type) == UNION_TYPE)
3173     {
3174       tree field;
3175
3176       /* Unions can be returned in registers if every element is
3177          integral, or can be returned in an integer register.  */
3178       for (field = TYPE_FIELDS (type);
3179            field;
3180            field = TREE_CHAIN (field))
3181         {
3182           if (TREE_CODE (field) != FIELD_DECL)
3183             continue;
3184
3185           if (FLOAT_TYPE_P (TREE_TYPE (field)))
3186             return 1;
3187
3188           if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
3189             return 1;
3190         }
3191
3192       return 0;
3193     }
3194 #endif /* not ARM_WINCE */
3195
3196   /* Return all other types in memory.  */
3197   return 1;
3198 }
3199
3200 /* Indicate whether or not words of a double are in big-endian order.  */
3201
3202 int
3203 arm_float_words_big_endian (void)
3204 {
3205   if (TARGET_MAVERICK)
3206     return 0;
3207
3208   /* For FPA, float words are always big-endian.  For VFP, floats words
3209      follow the memory system mode.  */
3210
3211   if (TARGET_FPA)
3212     {
3213       return 1;
3214     }
3215
3216   if (TARGET_VFP)
3217     return (TARGET_BIG_END ? 1 : 0);
3218
3219   return 1;
3220 }
3221
3222 /* Initialize a variable CUM of type CUMULATIVE_ARGS
3223    for a call to a function whose data type is FNTYPE.
3224    For a library call, FNTYPE is NULL.  */
3225 void
3226 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
3227                           rtx libname  ATTRIBUTE_UNUSED,
3228                           tree fndecl ATTRIBUTE_UNUSED)
3229 {
3230   /* On the ARM, the offset starts at 0.  */
3231   pcum->nregs = 0;
3232   pcum->iwmmxt_nregs = 0;
3233   pcum->can_split = true;
3234
3235   /* Varargs vectors are treated the same as long long.
3236      named_count avoids having to change the way arm handles 'named' */
3237   pcum->named_count = 0;
3238   pcum->nargs = 0;
3239
3240   if (TARGET_REALLY_IWMMXT && fntype)
3241     {
3242       tree fn_arg;
3243
3244       for (fn_arg = TYPE_ARG_TYPES (fntype);
3245            fn_arg;
3246            fn_arg = TREE_CHAIN (fn_arg))
3247         pcum->named_count += 1;
3248
3249       if (! pcum->named_count)
3250         pcum->named_count = INT_MAX;
3251     }
3252 }
3253
3254
3255 /* Return true if mode/type need doubleword alignment.  */
3256 bool
3257 arm_needs_doubleword_align (enum machine_mode mode, tree type)
3258 {
3259   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
3260           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
3261 }
3262
3263
3264 /* Determine where to put an argument to a function.
3265    Value is zero to push the argument on the stack,
3266    or a hard register in which to store the argument.
3267
3268    MODE is the argument's machine mode.
3269    TYPE is the data type of the argument (as a tree).
3270     This is null for libcalls where that information may
3271     not be available.
3272    CUM is a variable of type CUMULATIVE_ARGS which gives info about
3273     the preceding args and about the function being called.
3274    NAMED is nonzero if this argument is a named parameter
3275     (otherwise it is an extra parameter matching an ellipsis).  */
3276
3277 rtx
3278 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
3279                   tree type, int named)
3280 {
3281   int nregs;
3282
3283   /* Varargs vectors are treated the same as long long.
3284      named_count avoids having to change the way arm handles 'named' */
3285   if (TARGET_IWMMXT_ABI
3286       && arm_vector_mode_supported_p (mode)
3287       && pcum->named_count > pcum->nargs + 1)
3288     {
3289       if (pcum->iwmmxt_nregs <= 9)
3290         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
3291       else
3292         {
3293           pcum->can_split = false;
3294           return NULL_RTX;
3295         }
3296     }
3297
3298   /* Put doubleword aligned quantities in even register pairs.  */
3299   if (pcum->nregs & 1
3300       && ARM_DOUBLEWORD_ALIGN
3301       && arm_needs_doubleword_align (mode, type))
3302     pcum->nregs++;
3303
3304   if (mode == VOIDmode)
3305     /* Pick an arbitrary value for operand 2 of the call insn.  */
3306     return const0_rtx;
3307
3308   /* Only allow splitting an arg between regs and memory if all preceding
3309      args were allocated to regs.  For args passed by reference we only count
3310      the reference pointer.  */
3311   if (pcum->can_split)
3312     nregs = 1;
3313   else
3314     nregs = ARM_NUM_REGS2 (mode, type);
3315
3316   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
3317     return NULL_RTX;
3318
3319   return gen_rtx_REG (mode, pcum->nregs);
3320 }
3321
3322 static int
3323 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
3324                        tree type, bool named ATTRIBUTE_UNUSED)
3325 {
3326   int nregs = pcum->nregs;
3327
3328   if (TARGET_IWMMXT_ABI && arm_vector_mode_supported_p (mode))
3329     return 0;
3330
3331   if (NUM_ARG_REGS > nregs
3332       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
3333       && pcum->can_split)
3334     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
3335
3336   return 0;
3337 }
3338
3339 /* Variable sized types are passed by reference.  This is a GCC
3340    extension to the ARM ABI.  */
3341
3342 static bool
3343 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
3344                        enum machine_mode mode ATTRIBUTE_UNUSED,
3345                        const_tree type, bool named ATTRIBUTE_UNUSED)
3346 {
3347   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
3348 }
3349 \f
3350 /* Encode the current state of the #pragma [no_]long_calls.  */
3351 typedef enum
3352 {
3353   OFF,          /* No #pragma [no_]long_calls is in effect.  */
3354   LONG,         /* #pragma long_calls is in effect.  */
3355   SHORT         /* #pragma no_long_calls is in effect.  */
3356 } arm_pragma_enum;
3357
3358 static arm_pragma_enum arm_pragma_long_calls = OFF;
3359
3360 void
3361 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
3362 {
3363   arm_pragma_long_calls = LONG;
3364 }
3365
3366 void
3367 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
3368 {
3369   arm_pragma_long_calls = SHORT;
3370 }
3371
3372 void
3373 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
3374 {
3375   arm_pragma_long_calls = OFF;
3376 }
3377 \f
3378 /* Table of machine attributes.  */
3379 const struct attribute_spec arm_attribute_table[] =
3380 {
3381   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
3382   /* Function calls made to this symbol must be done indirectly, because
3383      it may lie outside of the 26 bit addressing range of a normal function
3384      call.  */
3385   { "long_call",    0, 0, false, true,  true,  NULL },
3386   /* Whereas these functions are always known to reside within the 26 bit
3387      addressing range.  */
3388   { "short_call",   0, 0, false, true,  true,  NULL },
3389   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
3390   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
3391   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
3392   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
3393 #ifdef ARM_PE
3394   /* ARM/PE has three new attributes:
3395      interfacearm - ?
3396      dllexport - for exporting a function/variable that will live in a dll
3397      dllimport - for importing a function/variable from a dll
3398
3399      Microsoft allows multiple declspecs in one __declspec, separating
3400      them with spaces.  We do NOT support this.  Instead, use __declspec
3401      multiple times.
3402   */
3403   { "dllimport",    0, 0, true,  false, false, NULL },
3404   { "dllexport",    0, 0, true,  false, false, NULL },
3405   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
3406 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
3407   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
3408   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
3409   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
3410 #endif
3411   { NULL,           0, 0, false, false, false, NULL }
3412 };
3413
3414 /* Handle an attribute requiring a FUNCTION_DECL;
3415    arguments as in struct attribute_spec.handler.  */
3416 static tree
3417 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
3418                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
3419 {
3420   if (TREE_CODE (*node) != FUNCTION_DECL)
3421     {
3422       warning (OPT_Wattributes, "%qE attribute only applies to functions",
3423                name);
3424       *no_add_attrs = true;
3425     }
3426
3427   return NULL_TREE;
3428 }
3429
3430 /* Handle an "interrupt" or "isr" attribute;
3431    arguments as in struct attribute_spec.handler.  */
3432 static tree
3433 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
3434                           bool *no_add_attrs)
3435 {
3436   if (DECL_P (*node))
3437     {
3438       if (TREE_CODE (*node) != FUNCTION_DECL)
3439         {
3440           warning (OPT_Wattributes, "%qE attribute only applies to functions",
3441                    name);
3442           *no_add_attrs = true;
3443         }
3444       /* FIXME: the argument if any is checked for type attributes;
3445          should it be checked for decl ones?  */
3446     }
3447   else
3448     {
3449       if (TREE_CODE (*node) == FUNCTION_TYPE
3450           || TREE_CODE (*node) == METHOD_TYPE)
3451         {
3452           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
3453             {
3454               warning (OPT_Wattributes, "%qE attribute ignored",
3455                        name);
3456               *no_add_attrs = true;
3457             }
3458         }
3459       else if (TREE_CODE (*node) == POINTER_TYPE
3460                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
3461                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
3462                && arm_isr_value (args) != ARM_FT_UNKNOWN)
3463         {
3464           *node = build_variant_type_copy (*node);
3465           TREE_TYPE (*node) = build_type_attribute_variant
3466             (TREE_TYPE (*node),
3467              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
3468           *no_add_attrs = true;
3469         }
3470       else
3471         {
3472           /* Possibly pass this attribute on from the type to a decl.  */
3473           if (flags & ((int) ATTR_FLAG_DECL_NEXT
3474                        | (int) ATTR_FLAG_FUNCTION_NEXT
3475                        | (int) ATTR_FLAG_ARRAY_NEXT))
3476             {
3477               *no_add_attrs = true;
3478               return tree_cons (name, args, NULL_TREE);
3479             }
3480           else
3481             {
3482               warning (OPT_Wattributes, "%qE attribute ignored",
3483                        name);
3484             }
3485         }
3486     }
3487
3488   return NULL_TREE;
3489 }
3490
3491 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
3492 /* Handle the "notshared" attribute.  This attribute is another way of
3493    requesting hidden visibility.  ARM's compiler supports
3494    "__declspec(notshared)"; we support the same thing via an
3495    attribute.  */
3496
3497 static tree
3498 arm_handle_notshared_attribute (tree *node,
3499                                 tree name ATTRIBUTE_UNUSED,
3500                                 tree args ATTRIBUTE_UNUSED,
3501                                 int flags ATTRIBUTE_UNUSED,
3502                                 bool *no_add_attrs)
3503 {
3504   tree decl = TYPE_NAME (*node);
3505
3506   if (decl)
3507     {
3508       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
3509       DECL_VISIBILITY_SPECIFIED (decl) = 1;
3510       *no_add_attrs = false;
3511     }
3512   return NULL_TREE;
3513 }
3514 #endif
3515
3516 /* Return 0 if the attributes for two types are incompatible, 1 if they
3517    are compatible, and 2 if they are nearly compatible (which causes a
3518    warning to be generated).  */
3519 static int
3520 arm_comp_type_attributes (const_tree type1, const_tree type2)
3521 {
3522   int l1, l2, s1, s2;
3523
3524   /* Check for mismatch of non-default calling convention.  */
3525   if (TREE_CODE (type1) != FUNCTION_TYPE)
3526     return 1;
3527
3528   /* Check for mismatched call attributes.  */
3529   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
3530   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
3531   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
3532   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
3533
3534   /* Only bother to check if an attribute is defined.  */
3535   if (l1 | l2 | s1 | s2)
3536     {
3537       /* If one type has an attribute, the other must have the same attribute.  */
3538       if ((l1 != l2) || (s1 != s2))
3539         return 0;
3540
3541       /* Disallow mixed attributes.  */
3542       if ((l1 & s2) || (l2 & s1))
3543         return 0;
3544     }
3545
3546   /* Check for mismatched ISR attribute.  */
3547   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
3548   if (! l1)
3549     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
3550   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
3551   if (! l2)
3552     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
3553   if (l1 != l2)
3554     return 0;
3555
3556   return 1;
3557 }
3558
3559 /*  Assigns default attributes to newly defined type.  This is used to
3560     set short_call/long_call attributes for function types of
3561     functions defined inside corresponding #pragma scopes.  */
3562 static void
3563 arm_set_default_type_attributes (tree type)
3564 {
3565   /* Add __attribute__ ((long_call)) to all functions, when
3566      inside #pragma long_calls or __attribute__ ((short_call)),
3567      when inside #pragma no_long_calls.  */
3568   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
3569     {
3570       tree type_attr_list, attr_name;
3571       type_attr_list = TYPE_ATTRIBUTES (type);
3572
3573       if (arm_pragma_long_calls == LONG)
3574         attr_name = get_identifier ("long_call");
3575       else if (arm_pragma_long_calls == SHORT)
3576         attr_name = get_identifier ("short_call");
3577       else
3578         return;
3579
3580       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
3581       TYPE_ATTRIBUTES (type) = type_attr_list;
3582     }
3583 }
3584 \f
3585 /* Return true if DECL is known to be linked into section SECTION.  */
3586
3587 static bool
3588 arm_function_in_section_p (tree decl, section *section)
3589 {
3590   /* We can only be certain about functions defined in the same
3591      compilation unit.  */
3592   if (!TREE_STATIC (decl))
3593     return false;
3594
3595   /* Make sure that SYMBOL always binds to the definition in this
3596      compilation unit.  */
3597   if (!targetm.binds_local_p (decl))
3598     return false;
3599
3600   /* If DECL_SECTION_NAME is set, assume it is trustworthy.  */
3601   if (!DECL_SECTION_NAME (decl))
3602     {
3603       /* Make sure that we will not create a unique section for DECL.  */
3604       if (flag_function_sections || DECL_ONE_ONLY (decl))
3605         return false;
3606     }
3607
3608   return function_section (decl) == section;
3609 }
3610
3611 /* Return nonzero if a 32-bit "long_call" should be generated for
3612    a call from the current function to DECL.  We generate a long_call
3613    if the function:
3614
3615         a.  has an __attribute__((long call))
3616      or b.  is within the scope of a #pragma long_calls
3617      or c.  the -mlong-calls command line switch has been specified
3618
3619    However we do not generate a long call if the function:
3620
3621         d.  has an __attribute__ ((short_call))
3622      or e.  is inside the scope of a #pragma no_long_calls
3623      or f.  is defined in the same section as the current function.  */
3624
3625 bool
3626 arm_is_long_call_p (tree decl)
3627 {
3628   tree attrs;
3629
3630   if (!decl)
3631     return TARGET_LONG_CALLS;
3632
3633   attrs = TYPE_ATTRIBUTES (TREE_TYPE (decl));
3634   if (lookup_attribute ("short_call", attrs))
3635     return false;
3636
3637   /* For "f", be conservative, and only cater for cases in which the
3638      whole of the current function is placed in the same section.  */
3639   if (!flag_reorder_blocks_and_partition
3640       && TREE_CODE (decl) == FUNCTION_DECL
3641       && arm_function_in_section_p (decl, current_function_section ()))
3642     return false;
3643
3644   if (lookup_attribute ("long_call", attrs))
3645     return true;
3646
3647   return TARGET_LONG_CALLS;
3648 }
3649
3650 /* Return nonzero if it is ok to make a tail-call to DECL.  */
3651 static bool
3652 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
3653 {
3654   unsigned long func_type;
3655
3656   if (cfun->machine->sibcall_blocked)
3657     return false;
3658
3659   /* Never tailcall something for which we have no decl, or if we
3660      are in Thumb mode.  */
3661   if (decl == NULL || TARGET_THUMB)
3662     return false;
3663
3664   /* The PIC register is live on entry to VxWorks PLT entries, so we
3665      must make the call before restoring the PIC register.  */
3666   if (TARGET_VXWORKS_RTP && flag_pic && !targetm.binds_local_p (decl))
3667     return false;
3668
3669   /* Cannot tail-call to long calls, since these are out of range of
3670      a branch instruction.  */
3671   if (arm_is_long_call_p (decl))
3672     return false;
3673
3674   /* If we are interworking and the function is not declared static
3675      then we can't tail-call it unless we know that it exists in this
3676      compilation unit (since it might be a Thumb routine).  */
3677   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
3678     return false;
3679
3680   func_type = arm_current_func_type ();
3681   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
3682   if (IS_INTERRUPT (func_type))
3683     return false;
3684
3685   /* Never tailcall if function may be called with a misaligned SP.  */
3686   if (IS_STACKALIGN (func_type))
3687     return false;
3688
3689   /* Everything else is ok.  */
3690   return true;
3691 }
3692
3693 \f
3694 /* Addressing mode support functions.  */
3695
3696 /* Return nonzero if X is a legitimate immediate operand when compiling
3697    for PIC.  We know that X satisfies CONSTANT_P and flag_pic is true.  */
3698 int
3699 legitimate_pic_operand_p (rtx x)
3700 {
3701   if (GET_CODE (x) == SYMBOL_REF
3702       || (GET_CODE (x) == CONST
3703           && GET_CODE (XEXP (x, 0)) == PLUS
3704           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
3705     return 0;
3706
3707   return 1;
3708 }
3709
3710 /* Record that the current function needs a PIC register.  Initialize
3711    cfun->machine->pic_reg if we have not already done so.  */
3712
3713 static void
3714 require_pic_register (void)
3715 {
3716   /* A lot of the logic here is made obscure by the fact that this
3717      routine gets called as part of the rtx cost estimation process.
3718      We don't want those calls to affect any assumptions about the real
3719      function; and further, we can't call entry_of_function() until we
3720      start the real expansion process.  */
3721   if (!crtl->uses_pic_offset_table)
3722     {
3723       gcc_assert (can_create_pseudo_p ());
3724       if (arm_pic_register != INVALID_REGNUM)
3725         {
3726           if (!cfun->machine->pic_reg)
3727             cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
3728
3729           /* Play games to avoid marking the function as needing pic
3730              if we are being called as part of the cost-estimation
3731              process.  */
3732           if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
3733             crtl->uses_pic_offset_table = 1;
3734         }
3735       else
3736         {
3737           rtx seq;
3738
3739           if (!cfun->machine->pic_reg)
3740             cfun->machine->pic_reg = gen_reg_rtx (Pmode);
3741
3742           /* Play games to avoid marking the function as needing pic
3743              if we are being called as part of the cost-estimation
3744              process.  */
3745           if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
3746             {
3747               crtl->uses_pic_offset_table = 1;
3748               start_sequence ();
3749
3750               arm_load_pic_register (0UL);
3751
3752               seq = get_insns ();
3753               end_sequence ();
3754               /* We can be called during expansion of PHI nodes, where
3755                  we can't yet emit instructions directly in the final
3756                  insn stream.  Queue the insns on the entry edge, they will
3757                  be committed after everything else is expanded.  */
3758               insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
3759             }
3760         }
3761     }
3762 }
3763
3764 rtx
3765 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
3766 {
3767   if (GET_CODE (orig) == SYMBOL_REF
3768       || GET_CODE (orig) == LABEL_REF)
3769     {
3770       rtx pic_ref, address;
3771       rtx insn;
3772       int subregs = 0;
3773
3774       /* If this function doesn't have a pic register, create one now.  */
3775       require_pic_register ();
3776
3777       if (reg == 0)
3778         {
3779           gcc_assert (can_create_pseudo_p ());
3780           reg = gen_reg_rtx (Pmode);
3781
3782           subregs = 1;
3783         }
3784
3785       if (subregs)
3786         address = gen_reg_rtx (Pmode);
3787       else
3788         address = reg;
3789
3790       if (TARGET_ARM)
3791         emit_insn (gen_pic_load_addr_arm (address, orig));
3792       else if (TARGET_THUMB2)
3793         emit_insn (gen_pic_load_addr_thumb2 (address, orig));
3794       else /* TARGET_THUMB1 */
3795         emit_insn (gen_pic_load_addr_thumb1 (address, orig));
3796
3797       /* VxWorks does not impose a fixed gap between segments; the run-time
3798          gap can be different from the object-file gap.  We therefore can't
3799          use GOTOFF unless we are absolutely sure that the symbol is in the
3800          same segment as the GOT.  Unfortunately, the flexibility of linker
3801          scripts means that we can't be sure of that in general, so assume
3802          that GOTOFF is never valid on VxWorks.  */
3803       if ((GET_CODE (orig) == LABEL_REF
3804            || (GET_CODE (orig) == SYMBOL_REF &&
3805                SYMBOL_REF_LOCAL_P (orig)))
3806           && NEED_GOT_RELOC
3807           && !TARGET_VXWORKS_RTP)
3808         pic_ref = gen_rtx_PLUS (Pmode, cfun->machine->pic_reg, address);
3809       else
3810         {
3811           pic_ref = gen_const_mem (Pmode,
3812                                    gen_rtx_PLUS (Pmode, cfun->machine->pic_reg,
3813                                                  address));
3814         }
3815
3816       insn = emit_move_insn (reg, pic_ref);
3817
3818       /* Put a REG_EQUAL note on this insn, so that it can be optimized
3819          by loop.  */
3820       set_unique_reg_note (insn, REG_EQUAL, orig);
3821
3822       return reg;
3823     }
3824   else if (GET_CODE (orig) == CONST)
3825     {
3826       rtx base, offset;
3827
3828       if (GET_CODE (XEXP (orig, 0)) == PLUS
3829           && XEXP (XEXP (orig, 0), 0) == cfun->machine->pic_reg)
3830         return orig;
3831
3832       /* Handle the case where we have: const (UNSPEC_TLS).  */
3833       if (GET_CODE (XEXP (orig, 0)) == UNSPEC
3834           && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
3835         return orig;
3836
3837       /* Handle the case where we have:
3838          const (plus (UNSPEC_TLS) (ADDEND)).  The ADDEND must be a
3839          CONST_INT.  */
3840       if (GET_CODE (XEXP (orig, 0)) == PLUS
3841           && GET_CODE (XEXP (XEXP (orig, 0), 0)) == UNSPEC
3842           && XINT (XEXP (XEXP (orig, 0), 0), 1) == UNSPEC_TLS)
3843         {
3844           gcc_assert (GET_CODE (XEXP (XEXP (orig, 0), 1)) == CONST_INT);
3845           return orig;
3846         }
3847
3848       if (reg == 0)
3849         {
3850           gcc_assert (can_create_pseudo_p ());
3851           reg = gen_reg_rtx (Pmode);
3852         }
3853
3854       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
3855
3856       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3857       offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3858                                        base == reg ? 0 : reg);
3859
3860       if (GET_CODE (offset) == CONST_INT)
3861         {
3862           /* The base register doesn't really matter, we only want to
3863              test the index for the appropriate mode.  */
3864           if (!arm_legitimate_index_p (mode, offset, SET, 0))
3865             {
3866               gcc_assert (can_create_pseudo_p ());
3867               offset = force_reg (Pmode, offset);
3868             }
3869
3870           if (GET_CODE (offset) == CONST_INT)
3871             return plus_constant (base, INTVAL (offset));
3872         }
3873
3874       if (GET_MODE_SIZE (mode) > 4
3875           && (GET_MODE_CLASS (mode) == MODE_INT
3876               || TARGET_SOFT_FLOAT))
3877         {
3878           emit_insn (gen_addsi3 (reg, base, offset));
3879           return reg;
3880         }
3881
3882       return gen_rtx_PLUS (Pmode, base, offset);
3883     }
3884
3885   return orig;
3886 }
3887
3888
3889 /* Find a spare register to use during the prolog of a function.  */
3890
3891 static int
3892 thumb_find_work_register (unsigned long pushed_regs_mask)
3893 {
3894   int reg;
3895
3896   /* Check the argument registers first as these are call-used.  The
3897      register allocation order means that sometimes r3 might be used
3898      but earlier argument registers might not, so check them all.  */
3899   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3900     if (!df_regs_ever_live_p (reg))
3901       return reg;
3902
3903   /* Before going on to check the call-saved registers we can try a couple
3904      more ways of deducing that r3 is available.  The first is when we are
3905      pushing anonymous arguments onto the stack and we have less than 4
3906      registers worth of fixed arguments(*).  In this case r3 will be part of
3907      the variable argument list and so we can be sure that it will be
3908      pushed right at the start of the function.  Hence it will be available
3909      for the rest of the prologue.
3910      (*): ie crtl->args.pretend_args_size is greater than 0.  */
3911   if (cfun->machine->uses_anonymous_args
3912       && crtl->args.pretend_args_size > 0)
3913     return LAST_ARG_REGNUM;
3914
3915   /* The other case is when we have fixed arguments but less than 4 registers
3916      worth.  In this case r3 might be used in the body of the function, but
3917      it is not being used to convey an argument into the function.  In theory
3918      we could just check crtl->args.size to see how many bytes are
3919      being passed in argument registers, but it seems that it is unreliable.
3920      Sometimes it will have the value 0 when in fact arguments are being
3921      passed.  (See testcase execute/20021111-1.c for an example).  So we also
3922      check the args_info.nregs field as well.  The problem with this field is
3923      that it makes no allowances for arguments that are passed to the
3924      function but which are not used.  Hence we could miss an opportunity
3925      when a function has an unused argument in r3.  But it is better to be
3926      safe than to be sorry.  */
3927   if (! cfun->machine->uses_anonymous_args
3928       && crtl->args.size >= 0
3929       && crtl->args.size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3930       && crtl->args.info.nregs < 4)
3931     return LAST_ARG_REGNUM;
3932
3933   /* Otherwise look for a call-saved register that is going to be pushed.  */
3934   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3935     if (pushed_regs_mask & (1 << reg))
3936       return reg;
3937
3938   if (TARGET_THUMB2)
3939     {
3940       /* Thumb-2 can use high regs.  */
3941       for (reg = FIRST_HI_REGNUM; reg < 15; reg ++)
3942         if (pushed_regs_mask & (1 << reg))
3943           return reg;
3944     }
3945   /* Something went wrong - thumb_compute_save_reg_mask()
3946      should have arranged for a suitable register to be pushed.  */
3947   gcc_unreachable ();
3948 }
3949
3950 static GTY(()) int pic_labelno;
3951
3952 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
3953    low register.  */
3954
3955 void
3956 arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
3957 {
3958   rtx l1, labelno, pic_tmp, pic_rtx, pic_reg;
3959
3960   if (crtl->uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3961     return;
3962
3963   gcc_assert (flag_pic);
3964
3965   pic_reg = cfun->machine->pic_reg;
3966   if (TARGET_VXWORKS_RTP)
3967     {
3968       pic_rtx = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE);
3969       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
3970       emit_insn (gen_pic_load_addr_arm (pic_reg, pic_rtx));
3971
3972       emit_insn (gen_rtx_SET (Pmode, pic_reg, gen_rtx_MEM (Pmode, pic_reg)));
3973
3974       pic_tmp = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
3975       emit_insn (gen_pic_offset_arm (pic_reg, pic_reg, pic_tmp));
3976     }
3977   else
3978     {
3979       /* We use an UNSPEC rather than a LABEL_REF because this label
3980          never appears in the code stream.  */
3981
3982       labelno = GEN_INT (pic_labelno++);
3983       l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3984       l1 = gen_rtx_CONST (VOIDmode, l1);
3985
3986       /* On the ARM the PC register contains 'dot + 8' at the time of the
3987          addition, on the Thumb it is 'dot + 4'.  */
3988       pic_rtx = plus_constant (l1, TARGET_ARM ? 8 : 4);
3989       pic_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, pic_rtx),
3990                                 UNSPEC_GOTSYM_OFF);
3991       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
3992
3993       if (TARGET_ARM)
3994         {
3995           emit_insn (gen_pic_load_addr_arm (pic_reg, pic_rtx));
3996           emit_insn (gen_pic_add_dot_plus_eight (pic_reg, pic_reg, labelno));
3997         }
3998       else if (TARGET_THUMB2)
3999         {
4000           /* Thumb-2 only allows very limited access to the PC.  Calculate the
4001              address in a temporary register.  */
4002           if (arm_pic_register != INVALID_REGNUM)
4003             {
4004               pic_tmp = gen_rtx_REG (SImode,
4005                                      thumb_find_work_register (saved_regs));
4006             }
4007           else
4008             {
4009               gcc_assert (can_create_pseudo_p ());
4010               pic_tmp = gen_reg_rtx (Pmode);
4011             }
4012
4013           emit_insn (gen_pic_load_addr_thumb2 (pic_reg, pic_rtx));
4014           emit_insn (gen_pic_load_dot_plus_four (pic_tmp, labelno));
4015           emit_insn (gen_addsi3 (pic_reg, pic_reg, pic_tmp));
4016         }
4017       else /* TARGET_THUMB1 */
4018         {
4019           if (arm_pic_register != INVALID_REGNUM
4020               && REGNO (pic_reg) > LAST_LO_REGNUM)
4021             {
4022               /* We will have pushed the pic register, so we should always be
4023                  able to find a work register.  */
4024               pic_tmp = gen_rtx_REG (SImode,
4025                                      thumb_find_work_register (saved_regs));
4026               emit_insn (gen_pic_load_addr_thumb1 (pic_tmp, pic_rtx));
4027               emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
4028             }
4029           else
4030             emit_insn (gen_pic_load_addr_thumb1 (pic_reg, pic_rtx));
4031           emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
4032         }
4033     }
4034
4035   /* Need to emit this whether or not we obey regdecls,
4036      since setjmp/longjmp can cause life info to screw up.  */
4037   emit_use (pic_reg);
4038 }
4039
4040
4041 /* Return nonzero if X is valid as an ARM state addressing register.  */
4042 static int
4043 arm_address_register_rtx_p (rtx x, int strict_p)
4044 {
4045   int regno;
4046
4047   if (GET_CODE (x) != REG)
4048     return 0;
4049
4050   regno = REGNO (x);
4051
4052   if (strict_p)
4053     return ARM_REGNO_OK_FOR_BASE_P (regno);
4054
4055   return (regno <= LAST_ARM_REGNUM
4056           || regno >= FIRST_PSEUDO_REGISTER
4057           || regno == FRAME_POINTER_REGNUM
4058           || regno == ARG_POINTER_REGNUM);
4059 }
4060
4061 /* Return TRUE if this rtx is the difference of a symbol and a label,
4062    and will reduce to a PC-relative relocation in the object file.
4063    Expressions like this can be left alone when generating PIC, rather
4064    than forced through the GOT.  */
4065 static int
4066 pcrel_constant_p (rtx x)
4067 {
4068   if (GET_CODE (x) == MINUS)
4069     return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
4070
4071   return FALSE;
4072 }
4073
4074 /* Return nonzero if X is a valid ARM state address operand.  */
4075 int
4076 arm_legitimate_address_outer_p (enum machine_mode mode, rtx x, RTX_CODE outer,
4077                                 int strict_p)
4078 {
4079   bool use_ldrd;
4080   enum rtx_code code = GET_CODE (x);
4081
4082   if (arm_address_register_rtx_p (x, strict_p))
4083     return 1;
4084
4085   use_ldrd = (TARGET_LDRD
4086               && (mode == DImode
4087                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
4088
4089   if (code == POST_INC || code == PRE_DEC
4090       || ((code == PRE_INC || code == POST_DEC)
4091           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
4092     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
4093
4094   else if ((code == POST_MODIFY || code == PRE_MODIFY)
4095            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
4096            && GET_CODE (XEXP (x, 1)) == PLUS
4097            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4098     {
4099       rtx addend = XEXP (XEXP (x, 1), 1);
4100
4101       /* Don't allow ldrd post increment by register because it's hard
4102          to fixup invalid register choices.  */
4103       if (use_ldrd
4104           && GET_CODE (x) == POST_MODIFY
4105           && GET_CODE (addend) == REG)
4106         return 0;
4107
4108       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
4109               && arm_legitimate_index_p (mode, addend, outer, strict_p));
4110     }
4111
4112   /* After reload constants split into minipools will have addresses
4113      from a LABEL_REF.  */
4114   else if (reload_completed
4115            && (code == LABEL_REF
4116                || (code == CONST
4117                    && GET_CODE (XEXP (x, 0)) == PLUS
4118                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
4119                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
4120     return 1;
4121
4122   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
4123     return 0;
4124
4125   else if (code == PLUS)
4126     {
4127       rtx xop0 = XEXP (x, 0);
4128       rtx xop1 = XEXP (x, 1);
4129
4130       return ((arm_address_register_rtx_p (xop0, strict_p)
4131                && GET_CODE(xop1) == CONST_INT
4132                && arm_legitimate_index_p (mode, xop1, outer, strict_p))
4133               || (arm_address_register_rtx_p (xop1, strict_p)
4134                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
4135     }
4136
4137 #if 0
4138   /* Reload currently can't handle MINUS, so disable this for now */
4139   else if (GET_CODE (x) == MINUS)
4140     {
4141       rtx xop0 = XEXP (x, 0);
4142       rtx xop1 = XEXP (x, 1);
4143
4144       return (arm_address_register_rtx_p (xop0, strict_p)
4145               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
4146     }
4147 #endif
4148
4149   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
4150            && code == SYMBOL_REF
4151            && CONSTANT_POOL_ADDRESS_P (x)
4152            && ! (flag_pic
4153                  && symbol_mentioned_p (get_pool_constant (x))
4154                  && ! pcrel_constant_p (get_pool_constant (x))))
4155     return 1;
4156
4157   return 0;
4158 }
4159
4160 /* Return nonzero if X is a valid Thumb-2 address operand.  */
4161 static int
4162 thumb2_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
4163 {
4164   bool use_ldrd;
4165   enum rtx_code code = GET_CODE (x);
4166   
4167   if (arm_address_register_rtx_p (x, strict_p))
4168     return 1;
4169
4170   use_ldrd = (TARGET_LDRD
4171               && (mode == DImode
4172                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
4173
4174   if (code == POST_INC || code == PRE_DEC
4175       || ((code == PRE_INC || code == POST_DEC)
4176           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
4177     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
4178
4179   else if ((code == POST_MODIFY || code == PRE_MODIFY)
4180            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
4181            && GET_CODE (XEXP (x, 1)) == PLUS
4182            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4183     {
4184       /* Thumb-2 only has autoincrement by constant.  */
4185       rtx addend = XEXP (XEXP (x, 1), 1);
4186       HOST_WIDE_INT offset;
4187
4188       if (GET_CODE (addend) != CONST_INT)
4189         return 0;
4190
4191       offset = INTVAL(addend);
4192       if (GET_MODE_SIZE (mode) <= 4)
4193         return (offset > -256 && offset < 256);
4194       
4195       return (use_ldrd && offset > -1024 && offset < 1024
4196               && (offset & 3) == 0);
4197     }
4198
4199   /* After reload constants split into minipools will have addresses
4200      from a LABEL_REF.  */
4201   else if (reload_completed
4202            && (code == LABEL_REF
4203                || (code == CONST
4204                    && GET_CODE (XEXP (x, 0)) == PLUS
4205                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
4206                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
4207     return 1;
4208
4209   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
4210     return 0;
4211
4212   else if (code == PLUS)
4213     {
4214       rtx xop0 = XEXP (x, 0);
4215       rtx xop1 = XEXP (x, 1);
4216
4217       return ((arm_address_register_rtx_p (xop0, strict_p)
4218                && thumb2_legitimate_index_p (mode, xop1, strict_p))
4219               || (arm_address_register_rtx_p (xop1, strict_p)
4220                   && thumb2_legitimate_index_p (mode, xop0, strict_p)));
4221     }
4222
4223   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
4224            && code == SYMBOL_REF
4225            && CONSTANT_POOL_ADDRESS_P (x)
4226            && ! (flag_pic
4227                  && symbol_mentioned_p (get_pool_constant (x))
4228                  && ! pcrel_constant_p (get_pool_constant (x))))
4229     return 1;
4230
4231   return 0;
4232 }
4233
4234 /* Return nonzero if INDEX is valid for an address index operand in
4235    ARM state.  */
4236 static int
4237 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
4238                         int strict_p)
4239 {
4240   HOST_WIDE_INT range;
4241   enum rtx_code code = GET_CODE (index);
4242
4243   /* Standard coprocessor addressing modes.  */
4244   if (TARGET_HARD_FLOAT
4245       && (TARGET_FPA || TARGET_MAVERICK)
4246       && (GET_MODE_CLASS (mode) == MODE_FLOAT
4247           || (TARGET_MAVERICK && mode == DImode)))
4248     return (code == CONST_INT && INTVAL (index) < 1024
4249             && INTVAL (index) > -1024
4250             && (INTVAL (index) & 3) == 0);
4251
4252   if (TARGET_NEON
4253       && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode)))
4254     return (code == CONST_INT
4255             && INTVAL (index) < 1016
4256             && INTVAL (index) > -1024
4257             && (INTVAL (index) & 3) == 0);
4258
4259   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
4260     return (code == CONST_INT
4261             && INTVAL (index) < 1024
4262             && INTVAL (index) > -1024
4263             && (INTVAL (index) & 3) == 0);
4264
4265   if (arm_address_register_rtx_p (index, strict_p)
4266       && (GET_MODE_SIZE (mode) <= 4))
4267     return 1;
4268
4269   if (mode == DImode || mode == DFmode)
4270     {
4271       if (code == CONST_INT)
4272         {
4273           HOST_WIDE_INT val = INTVAL (index);
4274
4275           if (TARGET_LDRD)
4276             return val > -256 && val < 256;
4277           else
4278             return val > -4096 && val < 4092;
4279         }
4280
4281       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
4282     }
4283
4284   if (GET_MODE_SIZE (mode) <= 4
4285       && ! (arm_arch4
4286             && (mode == HImode
4287                 || mode == HFmode
4288                 || (mode == QImode && outer == SIGN_EXTEND))))
4289     {
4290       if (code == MULT)
4291         {
4292           rtx xiop0 = XEXP (index, 0);
4293           rtx xiop1 = XEXP (index, 1);
4294
4295           return ((arm_address_register_rtx_p (xiop0, strict_p)
4296                    && power_of_two_operand (xiop1, SImode))
4297                   || (arm_address_register_rtx_p (xiop1, strict_p)
4298                       && power_of_two_operand (xiop0, SImode)));
4299         }
4300       else if (code == LSHIFTRT || code == ASHIFTRT
4301                || code == ASHIFT || code == ROTATERT)
4302         {
4303           rtx op = XEXP (index, 1);
4304
4305           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
4306                   && GET_CODE (op) == CONST_INT
4307                   && INTVAL (op) > 0
4308                   && INTVAL (op) <= 31);
4309         }
4310     }
4311
4312   /* For ARM v4 we may be doing a sign-extend operation during the
4313      load.  */
4314   if (arm_arch4)
4315     {
4316       if (mode == HImode
4317           || mode == HFmode
4318           || (outer == SIGN_EXTEND && mode == QImode))
4319         range = 256;
4320       else
4321         range = 4096;
4322     }
4323   else
4324     range = (mode == HImode || mode == HFmode) ? 4095 : 4096;
4325
4326   return (code == CONST_INT
4327           && INTVAL (index) < range
4328           && INTVAL (index) > -range);
4329 }
4330
4331 /* Return true if OP is a valid index scaling factor for Thumb-2 address
4332    index operand.  i.e. 1, 2, 4 or 8.  */
4333 static bool
4334 thumb2_index_mul_operand (rtx op)
4335 {
4336   HOST_WIDE_INT val;
4337   
4338   if (GET_CODE(op) != CONST_INT)
4339     return false;
4340
4341   val = INTVAL(op);
4342   return (val == 1 || val == 2 || val == 4 || val == 8);
4343 }
4344   
4345 /* Return nonzero if INDEX is a valid Thumb-2 address index operand.  */
4346 static int
4347 thumb2_legitimate_index_p (enum machine_mode mode, rtx index, int strict_p)
4348 {
4349   enum rtx_code code = GET_CODE (index);
4350
4351   /* ??? Combine arm and thumb2 coprocessor addressing modes.  */
4352   /* Standard coprocessor addressing modes.  */
4353   if (TARGET_HARD_FLOAT
4354       && (TARGET_FPA || TARGET_MAVERICK)
4355       && (GET_MODE_CLASS (mode) == MODE_FLOAT
4356           || (TARGET_MAVERICK && mode == DImode)))
4357     return (code == CONST_INT && INTVAL (index) < 1024
4358             && INTVAL (index) > -1024
4359             && (INTVAL (index) & 3) == 0);
4360
4361   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
4362     {
4363       /* For DImode assume values will usually live in core regs
4364          and only allow LDRD addressing modes.  */
4365       if (!TARGET_LDRD || mode != DImode)
4366         return (code == CONST_INT
4367                 && INTVAL (index) < 1024
4368                 && INTVAL (index) > -1024
4369                 && (INTVAL (index) & 3) == 0);
4370     }
4371
4372   if (TARGET_NEON
4373       && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode)))
4374     return (code == CONST_INT
4375             && INTVAL (index) < 1016
4376             && INTVAL (index) > -1024
4377             && (INTVAL (index) & 3) == 0);
4378
4379   if (arm_address_register_rtx_p (index, strict_p)
4380       && (GET_MODE_SIZE (mode) <= 4))
4381     return 1;
4382
4383   if (mode == DImode || mode == DFmode)
4384     {
4385       if (code == CONST_INT)
4386         {
4387           HOST_WIDE_INT val = INTVAL (index);
4388           /* ??? Can we assume ldrd for thumb2?  */
4389           /* Thumb-2 ldrd only has reg+const addressing modes.  */
4390           /* ldrd supports offsets of +-1020.
4391              However the ldr fallback does not.  */
4392           return val > -256 && val < 256 && (val & 3) == 0;
4393         }
4394       else
4395         return 0;
4396     }
4397
4398   if (code == MULT)
4399     {
4400       rtx xiop0 = XEXP (index, 0);
4401       rtx xiop1 = XEXP (index, 1);
4402
4403       return ((arm_address_register_rtx_p (xiop0, strict_p)
4404                && thumb2_index_mul_operand (xiop1))
4405               || (arm_address_register_rtx_p (xiop1, strict_p)
4406                   && thumb2_index_mul_operand (xiop0)));
4407     }
4408   else if (code == ASHIFT)
4409     {
4410       rtx op = XEXP (index, 1);
4411
4412       return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
4413               && GET_CODE (op) == CONST_INT
4414               && INTVAL (op) > 0
4415               && INTVAL (op) <= 3);
4416     }
4417
4418   return (code == CONST_INT
4419           && INTVAL (index) < 4096
4420           && INTVAL (index) > -256);
4421 }
4422
4423 /* Return nonzero if X is valid as a 16-bit Thumb state base register.  */
4424 static int
4425 thumb1_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
4426 {
4427   int regno;
4428
4429   if (GET_CODE (x) != REG)
4430     return 0;
4431
4432   regno = REGNO (x);
4433
4434   if (strict_p)
4435     return THUMB1_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
4436
4437   return (regno <= LAST_LO_REGNUM
4438           || regno > LAST_VIRTUAL_REGISTER
4439           || regno == FRAME_POINTER_REGNUM
4440           || (GET_MODE_SIZE (mode) >= 4
4441               && (regno == STACK_POINTER_REGNUM
4442                   || regno >= FIRST_PSEUDO_REGISTER
4443                   || x == hard_frame_pointer_rtx
4444                   || x == arg_pointer_rtx)));
4445 }
4446
4447 /* Return nonzero if x is a legitimate index register.  This is the case
4448    for any base register that can access a QImode object.  */
4449 inline static int
4450 thumb1_index_register_rtx_p (rtx x, int strict_p)
4451 {
4452   return thumb1_base_register_rtx_p (x, QImode, strict_p);
4453 }
4454
4455 /* Return nonzero if x is a legitimate 16-bit Thumb-state address.
4456
4457    The AP may be eliminated to either the SP or the FP, so we use the
4458    least common denominator, e.g. SImode, and offsets from 0 to 64.
4459
4460    ??? Verify whether the above is the right approach.
4461
4462    ??? Also, the FP may be eliminated to the SP, so perhaps that
4463    needs special handling also.
4464
4465    ??? Look at how the mips16 port solves this problem.  It probably uses
4466    better ways to solve some of these problems.
4467
4468    Although it is not incorrect, we don't accept QImode and HImode
4469    addresses based on the frame pointer or arg pointer until the
4470    reload pass starts.  This is so that eliminating such addresses
4471    into stack based ones won't produce impossible code.  */
4472 static int
4473 thumb1_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
4474 {
4475   /* ??? Not clear if this is right.  Experiment.  */
4476   if (GET_MODE_SIZE (mode) < 4
4477       && !(reload_in_progress || reload_completed)
4478       && (reg_mentioned_p (frame_pointer_rtx, x)
4479           || reg_mentioned_p (arg_pointer_rtx, x)
4480           || reg_mentioned_p (virtual_incoming_args_rtx, x)
4481           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
4482           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
4483           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
4484     return 0;
4485
4486   /* Accept any base register.  SP only in SImode or larger.  */
4487   else if (thumb1_base_register_rtx_p (x, mode, strict_p))
4488     return 1;
4489
4490   /* This is PC relative data before arm_reorg runs.  */
4491   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
4492            && GET_CODE (x) == SYMBOL_REF
4493            && CONSTANT_POOL_ADDRESS_P (x) && !flag_pic)
4494     return 1;
4495
4496   /* This is PC relative data after arm_reorg runs.  */
4497   else if ((GET_MODE_SIZE (mode) >= 4 || mode == HFmode)
4498            && reload_completed
4499            && (GET_CODE (x) == LABEL_REF
4500                || (GET_CODE (x) == CONST
4501                    && GET_CODE (XEXP (x, 0)) == PLUS
4502                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
4503                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
4504     return 1;
4505
4506   /* Post-inc indexing only supported for SImode and larger.  */
4507   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
4508            && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p))
4509     return 1;
4510
4511   else if (GET_CODE (x) == PLUS)
4512     {
4513       /* REG+REG address can be any two index registers.  */
4514       /* We disallow FRAME+REG addressing since we know that FRAME
4515          will be replaced with STACK, and SP relative addressing only
4516          permits SP+OFFSET.  */
4517       if (GET_MODE_SIZE (mode) <= 4
4518           && XEXP (x, 0) != frame_pointer_rtx
4519           && XEXP (x, 1) != frame_pointer_rtx
4520           && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
4521           && thumb1_index_register_rtx_p (XEXP (x, 1), strict_p))
4522         return 1;
4523
4524       /* REG+const has 5-7 bit offset for non-SP registers.  */
4525       else if ((thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
4526                 || XEXP (x, 0) == arg_pointer_rtx)
4527                && GET_CODE (XEXP (x, 1)) == CONST_INT
4528                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4529         return 1;
4530
4531       /* REG+const has 10-bit offset for SP, but only SImode and
4532          larger is supported.  */
4533       /* ??? Should probably check for DI/DFmode overflow here
4534          just like GO_IF_LEGITIMATE_OFFSET does.  */
4535       else if (GET_CODE (XEXP (x, 0)) == REG
4536                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
4537                && GET_MODE_SIZE (mode) >= 4
4538                && GET_CODE (XEXP (x, 1)) == CONST_INT
4539                && INTVAL (XEXP (x, 1)) >= 0
4540                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
4541                && (INTVAL (XEXP (x, 1)) & 3) == 0)
4542         return 1;
4543
4544       else if (GET_CODE (XEXP (x, 0)) == REG
4545                && (REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
4546                    || REGNO (XEXP (x, 0)) == ARG_POINTER_REGNUM
4547                    || (REGNO (XEXP (x, 0)) >= FIRST_VIRTUAL_REGISTER
4548                        && REGNO (XEXP (x, 0)) <= LAST_VIRTUAL_REGISTER))
4549                && GET_MODE_SIZE (mode) >= 4
4550                && GET_CODE (XEXP (x, 1)) == CONST_INT
4551                && (INTVAL (XEXP (x, 1)) & 3) == 0)
4552         return 1;
4553     }
4554
4555   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
4556            && GET_MODE_SIZE (mode) == 4
4557            && GET_CODE (x) == SYMBOL_REF
4558            && CONSTANT_POOL_ADDRESS_P (x)
4559            && ! (flag_pic
4560                  && symbol_mentioned_p (get_pool_constant (x))
4561                  && ! pcrel_constant_p (get_pool_constant (x))))
4562     return 1;
4563
4564   return 0;
4565 }
4566
4567 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
4568    instruction of mode MODE.  */
4569 int
4570 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
4571 {
4572   switch (GET_MODE_SIZE (mode))
4573     {
4574     case 1:
4575       return val >= 0 && val < 32;
4576
4577     case 2:
4578       return val >= 0 && val < 64 && (val & 1) == 0;
4579
4580     default:
4581       return (val >= 0
4582               && (val + GET_MODE_SIZE (mode)) <= 128
4583               && (val & 3) == 0);
4584     }
4585 }
4586
4587 bool
4588 arm_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
4589 {
4590   if (TARGET_ARM)
4591     return arm_legitimate_address_outer_p (mode, x, SET, strict_p);
4592   else if (TARGET_THUMB2)
4593     return thumb2_legitimate_address_p (mode, x, strict_p);
4594   else /* if (TARGET_THUMB1) */
4595     return thumb1_legitimate_address_p (mode, x, strict_p);
4596 }
4597
4598 /* Build the SYMBOL_REF for __tls_get_addr.  */
4599
4600 static GTY(()) rtx tls_get_addr_libfunc;
4601
4602 static rtx
4603 get_tls_get_addr (void)
4604 {
4605   if (!tls_get_addr_libfunc)
4606     tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
4607   return tls_get_addr_libfunc;
4608 }
4609
4610 static rtx
4611 arm_load_tp (rtx target)
4612 {
4613   if (!target)
4614     target = gen_reg_rtx (SImode);
4615
4616   if (TARGET_HARD_TP)
4617     {
4618       /* Can return in any reg.  */
4619       emit_insn (gen_load_tp_hard (target));
4620     }
4621   else
4622     {
4623       /* Always returned in r0.  Immediately copy the result into a pseudo,
4624          otherwise other uses of r0 (e.g. setting up function arguments) may
4625          clobber the value.  */
4626
4627       rtx tmp;
4628
4629       emit_insn (gen_load_tp_soft ());
4630
4631       tmp = gen_rtx_REG (SImode, 0);
4632       emit_move_insn (target, tmp);
4633     }
4634   return target;
4635 }
4636
4637 static rtx
4638 load_tls_operand (rtx x, rtx reg)
4639 {
4640   rtx tmp;
4641
4642   if (reg == NULL_RTX)
4643     reg = gen_reg_rtx (SImode);
4644
4645   tmp = gen_rtx_CONST (SImode, x);
4646
4647   emit_move_insn (reg, tmp);
4648
4649   return reg;
4650 }
4651
4652 static rtx
4653 arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
4654 {
4655   rtx insns, label, labelno, sum;
4656
4657   start_sequence ();
4658
4659   labelno = GEN_INT (pic_labelno++);
4660   label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
4661   label = gen_rtx_CONST (VOIDmode, label);
4662
4663   sum = gen_rtx_UNSPEC (Pmode,
4664                         gen_rtvec (4, x, GEN_INT (reloc), label,
4665                                    GEN_INT (TARGET_ARM ? 8 : 4)),
4666                         UNSPEC_TLS);
4667   reg = load_tls_operand (sum, reg);
4668
4669   if (TARGET_ARM)
4670     emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
4671   else if (TARGET_THUMB2)
4672     {
4673       rtx tmp;
4674       /* Thumb-2 only allows very limited access to the PC.  Calculate
4675          the address in a temporary register.  */
4676       tmp = gen_reg_rtx (SImode);
4677       emit_insn (gen_pic_load_dot_plus_four (tmp, labelno));
4678       emit_insn (gen_addsi3(reg, reg, tmp));
4679     }
4680   else /* TARGET_THUMB1 */
4681     emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
4682
4683   *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX, LCT_PURE, /* LCT_CONST?  */
4684                                      Pmode, 1, reg, Pmode);
4685
4686   insns = get_insns ();
4687   end_sequence ();
4688
4689   return insns;
4690 }
4691
4692 rtx
4693 legitimize_tls_address (rtx x, rtx reg)
4694 {
4695   rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
4696   unsigned int model = SYMBOL_REF_TLS_MODEL (x);
4697
4698   switch (model)
4699     {
4700     case TLS_MODEL_GLOBAL_DYNAMIC:
4701       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
4702       dest = gen_reg_rtx (Pmode);
4703       emit_libcall_block (insns, dest, ret, x);
4704       return dest;
4705
4706     case TLS_MODEL_LOCAL_DYNAMIC:
4707       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
4708
4709       /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
4710          share the LDM result with other LD model accesses.  */
4711       eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
4712                             UNSPEC_TLS);
4713       dest = gen_reg_rtx (Pmode);
4714       emit_libcall_block (insns, dest, ret, eqv);
4715
4716       /* Load the addend.  */
4717       addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (TLS_LDO32)),
4718                                UNSPEC_TLS);
4719       addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
4720       return gen_rtx_PLUS (Pmode, dest, addend);
4721
4722     case TLS_MODEL_INITIAL_EXEC:
4723       labelno = GEN_INT (pic_labelno++);
4724       label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
4725       label = gen_rtx_CONST (VOIDmode, label);
4726       sum = gen_rtx_UNSPEC (Pmode,
4727                             gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
4728                                        GEN_INT (TARGET_ARM ? 8 : 4)),
4729                             UNSPEC_TLS);
4730       reg = load_tls_operand (sum, reg);
4731
4732       if (TARGET_ARM)
4733         emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
4734       else if (TARGET_THUMB2)
4735         {
4736           rtx tmp;
4737           /* Thumb-2 only allows very limited access to the PC.  Calculate
4738              the address in a temporary register.  */
4739           tmp = gen_reg_rtx (SImode);
4740           emit_insn (gen_pic_load_dot_plus_four (tmp, labelno));
4741           emit_insn (gen_addsi3(reg, reg, tmp));
4742           emit_move_insn (reg, gen_const_mem (SImode, reg));
4743         }
4744       else
4745         {
4746           emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
4747           emit_move_insn (reg, gen_const_mem (SImode, reg));
4748         }
4749
4750       tp = arm_load_tp (NULL_RTX);
4751
4752       return gen_rtx_PLUS (Pmode, tp, reg);
4753
4754     case TLS_MODEL_LOCAL_EXEC:
4755       tp = arm_load_tp (NULL_RTX);
4756
4757       reg = gen_rtx_UNSPEC (Pmode,
4758                             gen_rtvec (2, x, GEN_INT (TLS_LE32)),
4759                             UNSPEC_TLS);
4760       reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
4761
4762       return gen_rtx_PLUS (Pmode, tp, reg);
4763
4764     default:
4765       abort ();
4766     }
4767 }
4768
4769 /* Try machine-dependent ways of modifying an illegitimate address
4770    to be legitimate.  If we find one, return the new, valid address.  */
4771 rtx
4772 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
4773 {
4774   if (!TARGET_ARM)
4775     {
4776       /* TODO: legitimize_address for Thumb2.  */
4777       if (TARGET_THUMB2)
4778         return x;
4779       return thumb_legitimize_address (x, orig_x, mode);
4780     }
4781
4782   if (arm_tls_symbol_p (x))
4783     return legitimize_tls_address (x, NULL_RTX);
4784
4785   if (GET_CODE (x) == PLUS)
4786     {
4787       rtx xop0 = XEXP (x, 0);
4788       rtx xop1 = XEXP (x, 1);
4789
4790       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
4791         xop0 = force_reg (SImode, xop0);
4792
4793       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
4794         xop1 = force_reg (SImode, xop1);
4795
4796       if (ARM_BASE_REGISTER_RTX_P (xop0)
4797           && GET_CODE (xop1) == CONST_INT)
4798         {
4799           HOST_WIDE_INT n, low_n;
4800           rtx base_reg, val;
4801           n = INTVAL (xop1);
4802
4803           /* VFP addressing modes actually allow greater offsets, but for
4804              now we just stick with the lowest common denominator.  */
4805           if (mode == DImode
4806               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
4807             {
4808               low_n = n & 0x0f;
4809               n &= ~0x0f;
4810               if (low_n > 4)
4811                 {
4812                   n += 16;
4813                   low_n -= 16;
4814                 }
4815             }
4816           else
4817             {
4818               low_n = ((mode) == TImode ? 0
4819                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
4820               n -= low_n;
4821             }
4822
4823           base_reg = gen_reg_rtx (SImode);
4824           val = force_operand (plus_constant (xop0, n), NULL_RTX);
4825           emit_move_insn (base_reg, val);
4826           x = plus_constant (base_reg, low_n);
4827         }
4828       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4829         x = gen_rtx_PLUS (SImode, xop0, xop1);
4830     }
4831
4832   /* XXX We don't allow MINUS any more -- see comment in
4833      arm_legitimate_address_outer_p ().  */
4834   else if (GET_CODE (x) == MINUS)
4835     {
4836       rtx xop0 = XEXP (x, 0);
4837       rtx xop1 = XEXP (x, 1);
4838
4839       if (CONSTANT_P (xop0))
4840         xop0 = force_reg (SImode, xop0);
4841
4842       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
4843         xop1 = force_reg (SImode, xop1);
4844
4845       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4846         x = gen_rtx_MINUS (SImode, xop0, xop1);
4847     }
4848
4849   /* Make sure to take full advantage of the pre-indexed addressing mode
4850      with absolute addresses which often allows for the base register to
4851      be factorized for multiple adjacent memory references, and it might
4852      even allows for the mini pool to be avoided entirely. */
4853   else if (GET_CODE (x) == CONST_INT && optimize > 0)
4854     {
4855       unsigned int bits;
4856       HOST_WIDE_INT mask, base, index;
4857       rtx base_reg;
4858
4859       /* ldr and ldrb can use a 12-bit index, ldrsb and the rest can only
4860          use a 8-bit index. So let's use a 12-bit index for SImode only and
4861          hope that arm_gen_constant will enable ldrb to use more bits. */
4862       bits = (mode == SImode) ? 12 : 8;
4863       mask = (1 << bits) - 1;
4864       base = INTVAL (x) & ~mask;
4865       index = INTVAL (x) & mask;
4866       if (bit_count (base & 0xffffffff) > (32 - bits)/2)
4867         {
4868           /* It'll most probably be more efficient to generate the base
4869              with more bits set and use a negative index instead. */
4870           base |= mask;
4871           index -= mask;
4872         }
4873       base_reg = force_reg (SImode, GEN_INT (base));
4874       x = plus_constant (base_reg, index);
4875     }
4876
4877   if (flag_pic)
4878     {
4879       /* We need to find and carefully transform any SYMBOL and LABEL
4880          references; so go back to the original address expression.  */
4881       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4882
4883       if (new_x != orig_x)
4884         x = new_x;
4885     }
4886
4887   return x;
4888 }
4889
4890
4891 /* Try machine-dependent ways of modifying an illegitimate Thumb address
4892    to be legitimate.  If we find one, return the new, valid address.  */
4893 rtx
4894 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
4895 {
4896   if (arm_tls_symbol_p (x))
4897     return legitimize_tls_address (x, NULL_RTX);
4898
4899   if (GET_CODE (x) == PLUS
4900       && GET_CODE (XEXP (x, 1)) == CONST_INT
4901       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
4902           || INTVAL (XEXP (x, 1)) < 0))
4903     {
4904       rtx xop0 = XEXP (x, 0);
4905       rtx xop1 = XEXP (x, 1);
4906       HOST_WIDE_INT offset = INTVAL (xop1);
4907
4908       /* Try and fold the offset into a biasing of the base register and
4909          then offsetting that.  Don't do this when optimizing for space
4910          since it can cause too many CSEs.  */
4911       if (optimize_size && offset >= 0
4912           && offset < 256 + 31 * GET_MODE_SIZE (mode))
4913         {
4914           HOST_WIDE_INT delta;
4915
4916           if (offset >= 256)
4917             delta = offset - (256 - GET_MODE_SIZE (mode));
4918           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
4919             delta = 31 * GET_MODE_SIZE (mode);
4920           else
4921             delta = offset & (~31 * GET_MODE_SIZE (mode));
4922
4923           xop0 = force_operand (plus_constant (xop0, offset - delta),
4924                                 NULL_RTX);
4925           x = plus_constant (xop0, delta);
4926         }
4927       else if (offset < 0 && offset > -256)
4928         /* Small negative offsets are best done with a subtract before the
4929            dereference, forcing these into a register normally takes two
4930            instructions.  */
4931         x = force_operand (x, NULL_RTX);
4932       else
4933         {
4934           /* For the remaining cases, force the constant into a register.  */
4935           xop1 = force_reg (SImode, xop1);
4936           x = gen_rtx_PLUS (SImode, xop0, xop1);
4937         }
4938     }
4939   else if (GET_CODE (x) == PLUS
4940            && s_register_operand (XEXP (x, 1), SImode)
4941            && !s_register_operand (XEXP (x, 0), SImode))
4942     {
4943       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
4944
4945       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
4946     }
4947
4948   if (flag_pic)
4949     {
4950       /* We need to find and carefully transform any SYMBOL and LABEL
4951          references; so go back to the original address expression.  */
4952       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4953
4954       if (new_x != orig_x)
4955         x = new_x;
4956     }
4957
4958   return x;
4959 }
4960
4961 rtx
4962 thumb_legitimize_reload_address (rtx *x_p,
4963                                  enum machine_mode mode,
4964                                  int opnum, int type,
4965                                  int ind_levels ATTRIBUTE_UNUSED)
4966 {
4967   rtx x = *x_p;
4968
4969   if (GET_CODE (x) == PLUS
4970       && GET_MODE_SIZE (mode) < 4
4971       && REG_P (XEXP (x, 0))
4972       && XEXP (x, 0) == stack_pointer_rtx
4973       && GET_CODE (XEXP (x, 1)) == CONST_INT
4974       && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4975     {
4976       rtx orig_x = x;
4977
4978       x = copy_rtx (x);
4979       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4980                    Pmode, VOIDmode, 0, 0, opnum, (enum reload_type) type);
4981       return x;
4982     }
4983
4984   /* If both registers are hi-regs, then it's better to reload the
4985      entire expression rather than each register individually.  That
4986      only requires one reload register rather than two.  */
4987   if (GET_CODE (x) == PLUS
4988       && REG_P (XEXP (x, 0))
4989       && REG_P (XEXP (x, 1))
4990       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
4991       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
4992     {
4993       rtx orig_x = x;
4994
4995       x = copy_rtx (x);
4996       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4997                    Pmode, VOIDmode, 0, 0, opnum, (enum reload_type) type);
4998       return x;
4999     }
5000
5001   return NULL;
5002 }
5003
5004 /* Test for various thread-local symbols.  */
5005
5006 /* Return TRUE if X is a thread-local symbol.  */
5007
5008 static bool
5009 arm_tls_symbol_p (rtx x)
5010 {
5011   if (! TARGET_HAVE_TLS)
5012     return false;
5013
5014   if (GET_CODE (x) != SYMBOL_REF)
5015     return false;
5016
5017   return SYMBOL_REF_TLS_MODEL (x) != 0;
5018 }
5019
5020 /* Helper for arm_tls_referenced_p.  */
5021
5022 static int
5023 arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
5024 {
5025   if (GET_CODE (*x) == SYMBOL_REF)
5026     return SYMBOL_REF_TLS_MODEL (*x) != 0;
5027
5028   /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
5029      TLS offsets, not real symbol references.  */
5030   if (GET_CODE (*x) == UNSPEC
5031       && XINT (*x, 1) == UNSPEC_TLS)
5032     return -1;
5033
5034   return 0;
5035 }
5036
5037 /* Return TRUE if X contains any TLS symbol references.  */
5038
5039 bool
5040 arm_tls_referenced_p (rtx x)
5041 {
5042   if (! TARGET_HAVE_TLS)
5043     return false;
5044
5045   return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
5046 }
5047
5048 /* Implement TARGET_CANNOT_FORCE_CONST_MEM.  */
5049
5050 bool
5051 arm_cannot_force_const_mem (rtx x)
5052 {
5053   rtx base, offset;
5054
5055   if (ARM_OFFSETS_MUST_BE_WITHIN_SECTIONS_P)
5056     {
5057       split_const (x, &base, &offset);
5058       if (GET_CODE (base) == SYMBOL_REF
5059           && !offset_within_block_p (base, INTVAL (offset)))
5060         return true;
5061     }
5062   return arm_tls_referenced_p (x);
5063 }
5064 \f
5065 #define REG_OR_SUBREG_REG(X)                                            \
5066   (GET_CODE (X) == REG                                                  \
5067    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
5068
5069 #define REG_OR_SUBREG_RTX(X)                    \
5070    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
5071
5072 #ifndef COSTS_N_INSNS
5073 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
5074 #endif
5075 static inline int
5076 thumb1_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
5077 {
5078   enum machine_mode mode = GET_MODE (x);
5079
5080   switch (code)
5081     {
5082     case ASHIFT:
5083     case ASHIFTRT:
5084     case LSHIFTRT:
5085     case ROTATERT:
5086     case PLUS:
5087     case MINUS:
5088     case COMPARE:
5089     case NEG:
5090     case NOT:
5091       return COSTS_N_INSNS (1);
5092
5093     case MULT:
5094       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5095         {
5096           int cycles = 0;
5097           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
5098
5099           while (i)
5100             {
5101               i >>= 2;
5102               cycles++;
5103             }
5104           return COSTS_N_INSNS (2) + cycles;
5105         }
5106       return COSTS_N_INSNS (1) + 16;
5107
5108     case SET:
5109       return (COSTS_N_INSNS (1)
5110               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
5111                      + GET_CODE (SET_DEST (x)) == MEM));
5112
5113     case CONST_INT:
5114       if (outer == SET)
5115         {
5116           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
5117             return 0;
5118           if (thumb_shiftable_const (INTVAL (x)))
5119             return COSTS_N_INSNS (2);
5120           return COSTS_N_INSNS (3);
5121         }
5122       else if ((outer == PLUS || outer == COMPARE)
5123                && INTVAL (x) < 256 && INTVAL (x) > -256)
5124         return 0;
5125       else if (outer == AND
5126                && INTVAL (x) < 256 && INTVAL (x) >= -256)
5127         return COSTS_N_INSNS (1);
5128       else if (outer == ASHIFT || outer == ASHIFTRT
5129                || outer == LSHIFTRT)
5130         return 0;
5131       return COSTS_N_INSNS (2);
5132
5133     case CONST:
5134     case CONST_DOUBLE:
5135     case LABEL_REF:
5136     case SYMBOL_REF:
5137       return COSTS_N_INSNS (3);
5138
5139     case UDIV:
5140     case UMOD:
5141     case DIV:
5142     case MOD:
5143       return 100;
5144
5145     case TRUNCATE:
5146       return 99;
5147
5148     case AND:
5149     case XOR:
5150     case IOR:
5151       /* XXX guess.  */
5152       return 8;
5153
5154     case MEM:
5155       /* XXX another guess.  */
5156       /* Memory costs quite a lot for the first word, but subsequent words
5157          load at the equivalent of a single insn each.  */
5158       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
5159               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
5160                  ? 4 : 0));
5161
5162     case IF_THEN_ELSE:
5163       /* XXX a guess.  */
5164       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
5165         return 14;
5166       return 2;
5167
5168     case ZERO_EXTEND:
5169       /* XXX still guessing.  */
5170       switch (GET_MODE (XEXP (x, 0)))
5171         {
5172         case QImode:
5173           return (1 + (mode == DImode ? 4 : 0)
5174                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
5175
5176         case HImode:
5177           return (4 + (mode == DImode ? 4 : 0)
5178                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
5179
5180         case SImode:
5181           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
5182
5183         default:
5184           return 99;
5185         }
5186
5187     default:
5188       return 99;
5189     }
5190 }
5191
5192 static inline bool
5193 arm_rtx_costs_1 (rtx x, enum rtx_code outer, int* total, bool speed)
5194 {
5195   enum machine_mode mode = GET_MODE (x);
5196   enum rtx_code subcode;
5197   rtx operand;
5198   enum rtx_code code = GET_CODE (x);
5199   int extra_cost;
5200   *total = 0;
5201
5202   switch (code)
5203     {
5204     case MEM:
5205       /* Memory costs quite a lot for the first word, but subsequent words
5206          load at the equivalent of a single insn each.  */
5207       *total = COSTS_N_INSNS (2 + ARM_NUM_REGS (mode));
5208       return true;
5209
5210     case DIV:
5211     case MOD:
5212     case UDIV:
5213     case UMOD:
5214       if (TARGET_HARD_FLOAT && mode == SFmode)
5215         *total = COSTS_N_INSNS (2);
5216       else if (TARGET_HARD_FLOAT && mode == DFmode)
5217         *total = COSTS_N_INSNS (4);
5218       else
5219         *total = COSTS_N_INSNS (20);
5220       return false;
5221
5222     case ROTATE:
5223       if (GET_CODE (XEXP (x, 1)) == REG)
5224         *total = COSTS_N_INSNS (1); /* Need to subtract from 32 */
5225       else if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5226         *total = rtx_cost (XEXP (x, 1), code, speed);
5227
5228       /* Fall through */
5229     case ROTATERT:
5230       if (mode != SImode)
5231         {
5232           *total += COSTS_N_INSNS (4);
5233           return true;
5234         }
5235
5236       /* Fall through */
5237     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
5238       *total += rtx_cost (XEXP (x, 0), code, speed);
5239       if (mode == DImode)
5240         {
5241           *total += COSTS_N_INSNS (3);
5242           return true;
5243         }
5244
5245       *total += COSTS_N_INSNS (1);
5246       /* Increase the cost of complex shifts because they aren't any faster,
5247          and reduce dual issue opportunities.  */
5248       if (arm_tune_cortex_a9
5249           && outer != SET && GET_CODE (XEXP (x, 1)) != CONST_INT)
5250         ++*total;
5251
5252       return true;
5253
5254     case MINUS:
5255       if (TARGET_THUMB2)
5256         {
5257           if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5258             {
5259               if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
5260                 *total = COSTS_N_INSNS (1);
5261               else
5262                 *total = COSTS_N_INSNS (20);
5263             }
5264           else
5265             *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5266           /* Thumb2 does not have RSB, so all arguments must be
5267              registers (subtracting a constant is canonicalized as
5268              addition of the negated constant).  */
5269           return false;
5270         }
5271
5272       if (mode == DImode)
5273         {
5274           *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5275           if (GET_CODE (XEXP (x, 0)) == CONST_INT
5276               && const_ok_for_arm (INTVAL (XEXP (x, 0))))
5277             {
5278               *total += rtx_cost (XEXP (x, 1), code, speed);
5279               return true;
5280             }
5281
5282           if (GET_CODE (XEXP (x, 1)) == CONST_INT
5283               && const_ok_for_arm (INTVAL (XEXP (x, 1))))
5284             {
5285               *total += rtx_cost (XEXP (x, 0), code, speed);
5286               return true;
5287             }
5288
5289           return false;
5290         }
5291
5292       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5293         {
5294           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
5295             {
5296               *total = COSTS_N_INSNS (1);
5297               if (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
5298                   && arm_const_double_rtx (XEXP (x, 0)))
5299                 {
5300                   *total += rtx_cost (XEXP (x, 1), code, speed);
5301                   return true;
5302                 }
5303
5304               if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
5305                   && arm_const_double_rtx (XEXP (x, 1)))
5306                 {
5307                   *total += rtx_cost (XEXP (x, 0), code, speed);
5308                   return true;
5309                 }
5310
5311               return false;
5312             }
5313           *total = COSTS_N_INSNS (20);
5314           return false;
5315         }
5316
5317       *total = COSTS_N_INSNS (1);
5318       if (GET_CODE (XEXP (x, 0)) == CONST_INT
5319           && const_ok_for_arm (INTVAL (XEXP (x, 0))))
5320         {
5321           *total += rtx_cost (XEXP (x, 1), code, speed);
5322           return true;
5323         }
5324
5325       subcode = GET_CODE (XEXP (x, 1));
5326       if (subcode == ASHIFT || subcode == ASHIFTRT
5327           || subcode == LSHIFTRT
5328           || subcode == ROTATE || subcode == ROTATERT)
5329         {
5330           *total += rtx_cost (XEXP (x, 0), code, speed);
5331           *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, speed);
5332           return true;
5333         }
5334
5335       /* A shift as a part of RSB costs no more than RSB itself.  */
5336       if (GET_CODE (XEXP (x, 0)) == MULT
5337           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
5338         {
5339           *total += rtx_cost (XEXP (XEXP (x, 0), 0), code, speed);
5340           *total += rtx_cost (XEXP (x, 1), code, speed);
5341           return true;
5342         }
5343
5344       if (subcode == MULT
5345           && power_of_two_operand (XEXP (XEXP (x, 1), 1), SImode))
5346         {
5347           *total += rtx_cost (XEXP (x, 0), code, speed);
5348           *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, speed);
5349           return true;
5350         }
5351
5352       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMPARE
5353           || GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMM_COMPARE)
5354         {
5355           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
5356           if (GET_CODE (XEXP (XEXP (x, 1), 0)) == REG
5357               && REGNO (XEXP (XEXP (x, 1), 0)) != CC_REGNUM)
5358             *total += COSTS_N_INSNS (1);
5359
5360           return true;
5361         }
5362
5363       /* Fall through */
5364
5365     case PLUS:
5366       if (code == PLUS && arm_arch6 && mode == SImode
5367           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5368               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5369         {
5370           *total = COSTS_N_INSNS (1);
5371           *total += rtx_cost (XEXP (XEXP (x, 0), 0), GET_CODE (XEXP (x, 0)),
5372                               speed);
5373           *total += rtx_cost (XEXP (x, 1), code, speed);
5374           return true;
5375         }
5376
5377       /* MLA: All arguments must be registers.  We filter out
5378          multiplication by a power of two, so that we fall down into
5379          the code below.  */
5380       if (GET_CODE (XEXP (x, 0)) == MULT
5381           && !power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
5382         {
5383           /* The cost comes from the cost of the multiply.  */
5384           return false;
5385         }
5386
5387       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5388         {
5389           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
5390             {
5391               *total = COSTS_N_INSNS (1);
5392               if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
5393                   && arm_const_double_rtx (XEXP (x, 1)))
5394                 {
5395                   *total += rtx_cost (XEXP (x, 0), code, speed);
5396                   return true;
5397                 }
5398
5399               return false;
5400             }
5401
5402           *total = COSTS_N_INSNS (20);
5403           return false;
5404         }
5405
5406       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMPARE
5407           || GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMM_COMPARE)
5408         {
5409           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 1), code, speed);
5410           if (GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
5411               && REGNO (XEXP (XEXP (x, 0), 0)) != CC_REGNUM)
5412             *total += COSTS_N_INSNS (1);
5413           return true;
5414         }
5415
5416       /* Fall through */
5417
5418     case AND: case XOR: case IOR:
5419       extra_cost = 0;
5420
5421       /* Normally the frame registers will be spilt into reg+const during
5422          reload, so it is a bad idea to combine them with other instructions,
5423          since then they might not be moved outside of loops.  As a compromise
5424          we allow integration with ops that have a constant as their second
5425          operand.  */
5426       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
5427            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
5428            && GET_CODE (XEXP (x, 1)) != CONST_INT)
5429           || (REG_OR_SUBREG_REG (XEXP (x, 0))
5430               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
5431         *total = 4;
5432
5433       if (mode == DImode)
5434         {
5435           *total += COSTS_N_INSNS (2);
5436           if (GET_CODE (XEXP (x, 1)) == CONST_INT
5437               && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
5438             {
5439               *total += rtx_cost (XEXP (x, 0), code, speed);
5440               return true;
5441             }
5442
5443           return false;
5444         }
5445
5446       *total += COSTS_N_INSNS (1);
5447       if (GET_CODE (XEXP (x, 1)) == CONST_INT
5448           && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
5449         {
5450           *total += rtx_cost (XEXP (x, 0), code, speed);
5451           return true;
5452         }
5453       subcode = GET_CODE (XEXP (x, 0));
5454       if (subcode == ASHIFT || subcode == ASHIFTRT
5455           || subcode == LSHIFTRT
5456           || subcode == ROTATE || subcode == ROTATERT)
5457         {
5458           *total += rtx_cost (XEXP (x, 1), code, speed);
5459           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
5460           return true;
5461         }
5462
5463       if (subcode == MULT
5464           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
5465         {
5466           *total += rtx_cost (XEXP (x, 1), code, speed);
5467           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
5468           return true;
5469         }
5470
5471       if (subcode == UMIN || subcode == UMAX
5472           || subcode == SMIN || subcode == SMAX)
5473         {
5474           *total = COSTS_N_INSNS (3);
5475           return true;
5476         }
5477
5478       return false;
5479
5480     case MULT:
5481       /* This should have been handled by the CPU specific routines.  */
5482       gcc_unreachable ();
5483
5484     case TRUNCATE:
5485       if (arm_arch3m && mode == SImode
5486           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
5487           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
5488           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
5489               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
5490           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
5491               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
5492         {
5493           *total = rtx_cost (XEXP (XEXP (x, 0), 0), LSHIFTRT, speed);
5494           return true;
5495         }
5496       *total = COSTS_N_INSNS (2); /* Plus the cost of the MULT */
5497       return false;
5498
5499     case NEG:
5500       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5501         {
5502           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
5503             {
5504               *total = COSTS_N_INSNS (1);
5505               return false;
5506             }
5507           *total = COSTS_N_INSNS (2);
5508           return false;
5509         }
5510
5511       /* Fall through */
5512     case NOT:
5513       *total = COSTS_N_INSNS (ARM_NUM_REGS(mode));
5514       if (mode == SImode && code == NOT)
5515         {
5516           subcode = GET_CODE (XEXP (x, 0));
5517           if (subcode == ASHIFT || subcode == ASHIFTRT
5518               || subcode == LSHIFTRT
5519               || subcode == ROTATE || subcode == ROTATERT
5520               || (subcode == MULT
5521                   && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode)))
5522             {
5523               *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
5524               /* Register shifts cost an extra cycle.  */
5525               if (GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
5526                 *total += COSTS_N_INSNS (1) + rtx_cost (XEXP (XEXP (x, 0), 1),
5527                                                         subcode, speed);
5528               return true;
5529             }
5530         }
5531
5532       return false;
5533
5534     case IF_THEN_ELSE:
5535       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
5536         {
5537           *total = COSTS_N_INSNS (4);
5538           return true;
5539         }
5540
5541       operand = XEXP (x, 0);
5542
5543       if (!((GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMPARE
5544              || GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMM_COMPARE)
5545             && GET_CODE (XEXP (operand, 0)) == REG
5546             && REGNO (XEXP (operand, 0)) == CC_REGNUM))
5547         *total += COSTS_N_INSNS (1);
5548       *total += (rtx_cost (XEXP (x, 1), code, speed)
5549                  + rtx_cost (XEXP (x, 2), code, speed));
5550       return true;
5551
5552     case NE:
5553       if (mode == SImode && XEXP (x, 1) == const0_rtx)
5554         {
5555           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
5556           return true;
5557         }
5558       goto scc_insn;
5559
5560     case GE:
5561       if ((GET_CODE (XEXP (x, 0)) != REG || REGNO (XEXP (x, 0)) != CC_REGNUM)
5562           && mode == SImode && XEXP (x, 1) == const0_rtx)
5563         {
5564           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
5565           return true;
5566         }
5567       goto scc_insn;
5568
5569     case LT:
5570       if ((GET_CODE (XEXP (x, 0)) != REG || REGNO (XEXP (x, 0)) != CC_REGNUM)
5571           && mode == SImode && XEXP (x, 1) == const0_rtx)
5572         {
5573           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
5574           return true;
5575         }
5576       goto scc_insn;
5577
5578     case EQ:
5579     case GT:
5580     case LE:
5581     case GEU:
5582     case LTU:
5583     case GTU:
5584     case LEU:
5585     case UNORDERED:
5586     case ORDERED:
5587     case UNEQ:
5588     case UNGE:
5589     case UNLT:
5590     case UNGT:
5591     case UNLE:
5592     scc_insn:
5593       /* SCC insns.  In the case where the comparison has already been
5594          performed, then they cost 2 instructions.  Otherwise they need
5595          an additional comparison before them.  */
5596       *total = COSTS_N_INSNS (2);
5597       if (GET_CODE (XEXP (x, 0)) == REG && REGNO (XEXP (x, 0)) == CC_REGNUM)
5598         {
5599           return true;
5600         }
5601
5602       /* Fall through */
5603     case COMPARE:
5604       if (GET_CODE (XEXP (x, 0)) == REG && REGNO (XEXP (x, 0)) == CC_REGNUM)
5605         {
5606           *total = 0;
5607           return true;
5608         }
5609
5610       *total += COSTS_N_INSNS (1);
5611       if (GET_CODE (XEXP (x, 1)) == CONST_INT
5612           && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
5613         {
5614           *total += rtx_cost (XEXP (x, 0), code, speed);
5615           return true;
5616         }
5617
5618       subcode = GET_CODE (XEXP (x, 0));
5619       if (subcode == ASHIFT || subcode == ASHIFTRT
5620           || subcode == LSHIFTRT
5621           || subcode == ROTATE || subcode == ROTATERT)
5622         {
5623           *total += rtx_cost (XEXP (x, 1), code, speed);
5624           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
5625           return true;
5626         }
5627
5628       if (subcode == MULT
5629           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
5630         {
5631           *total += rtx_cost (XEXP (x, 1), code, speed);
5632           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
5633           return true;
5634         }
5635       
5636       return false;
5637
5638     case UMIN:
5639     case UMAX:
5640     case SMIN:
5641     case SMAX:
5642       *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
5643       if (GET_CODE (XEXP (x, 1)) != CONST_INT
5644           || !const_ok_for_arm (INTVAL (XEXP (x, 1))))
5645         *total += rtx_cost (XEXP (x, 1), code, speed);
5646       return true;
5647
5648     case ABS:
5649       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5650         {
5651           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
5652             {
5653               *total = COSTS_N_INSNS (1);
5654               return false;
5655             }
5656           *total = COSTS_N_INSNS (20);
5657           return false;
5658         }
5659       *total = COSTS_N_INSNS (1);
5660       if (mode == DImode)
5661         *total += COSTS_N_INSNS (3);
5662       return false;
5663
5664     case SIGN_EXTEND:
5665       if (GET_MODE_CLASS (mode) == MODE_INT)
5666         {
5667           *total = 0;
5668           if (mode == DImode)
5669             *total += COSTS_N_INSNS (1);
5670
5671           if (GET_MODE (XEXP (x, 0)) != SImode)
5672             {
5673               if (arm_arch6)
5674                 {
5675                   if (GET_CODE (XEXP (x, 0)) != MEM)
5676                     *total += COSTS_N_INSNS (1);
5677                 }
5678               else if (!arm_arch4 || GET_CODE (XEXP (x, 0)) != MEM)
5679                 *total += COSTS_N_INSNS (2);
5680             }
5681
5682           return false;
5683         }
5684
5685       /* Fall through */
5686     case ZERO_EXTEND:
5687       *total = 0;
5688       if (GET_MODE_CLASS (mode) == MODE_INT)
5689         {
5690           if (mode == DImode)
5691             *total += COSTS_N_INSNS (1);
5692
5693           if (GET_MODE (XEXP (x, 0)) != SImode)
5694             {
5695               if (arm_arch6)
5696                 {
5697                   if (GET_CODE (XEXP (x, 0)) != MEM)
5698                     *total += COSTS_N_INSNS (1);
5699                 }
5700               else if (!arm_arch4 || GET_CODE (XEXP (x, 0)) != MEM)
5701                 *total += COSTS_N_INSNS (GET_MODE (XEXP (x, 0)) == QImode ?
5702                                          1 : 2);
5703             }
5704
5705           return false;
5706         }
5707
5708       switch (GET_MODE (XEXP (x, 0)))
5709         {
5710         case V8QImode:
5711         case V4HImode:
5712         case V2SImode:
5713         case V4QImode:
5714         case V2HImode:
5715           *total = COSTS_N_INSNS (1);
5716           return false;
5717
5718         default:
5719           gcc_unreachable ();
5720         }
5721       gcc_unreachable ();
5722
5723     case ZERO_EXTRACT:
5724     case SIGN_EXTRACT:
5725       *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
5726       return true;
5727
5728     case CONST_INT:
5729       if (const_ok_for_arm (INTVAL (x))
5730           || const_ok_for_arm (~INTVAL (x)))
5731         *total = COSTS_N_INSNS (1);
5732       else
5733         *total = COSTS_N_INSNS (arm_gen_constant (SET, mode, NULL_RTX,
5734                                                   INTVAL (x), NULL_RTX,
5735                                                   NULL_RTX, 0, 0));
5736       return true;
5737
5738     case CONST:
5739     case LABEL_REF:
5740     case SYMBOL_REF:
5741       *total = COSTS_N_INSNS (3);
5742       return true;
5743
5744     case HIGH:
5745       *total = COSTS_N_INSNS (1);
5746       return true;
5747
5748     case LO_SUM:
5749       *total = COSTS_N_INSNS (1);
5750       *total += rtx_cost (XEXP (x, 0), code, speed);
5751       return true;
5752
5753     case CONST_DOUBLE:
5754       if (TARGET_HARD_FLOAT && vfp3_const_double_rtx (x))
5755         *total = COSTS_N_INSNS (1);
5756       else
5757         *total = COSTS_N_INSNS (4);
5758       return true;
5759
5760     default:
5761       *total = COSTS_N_INSNS (4);
5762       return false;
5763     }
5764 }
5765
5766 /* RTX costs when optimizing for size.  */
5767 static bool
5768 arm_size_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
5769                     int *total)
5770 {
5771   enum machine_mode mode = GET_MODE (x);
5772   if (TARGET_THUMB1)
5773     {
5774       /* XXX TBD.  For now, use the standard costs.  */
5775       *total = thumb1_rtx_costs (x, code, outer_code);
5776       return true;
5777     }
5778
5779   /* FIXME: This makes no attempt to prefer narrow Thumb-2 instructions.  */
5780   switch (code)
5781     {
5782     case MEM:
5783       /* A memory access costs 1 insn if the mode is small, or the address is
5784          a single register, otherwise it costs one insn per word.  */
5785       if (REG_P (XEXP (x, 0)))
5786         *total = COSTS_N_INSNS (1);
5787       else
5788         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5789       return true;
5790
5791     case DIV:
5792     case MOD:
5793     case UDIV:
5794     case UMOD:
5795       /* Needs a libcall, so it costs about this.  */
5796       *total = COSTS_N_INSNS (2);
5797       return false;
5798
5799     case ROTATE:
5800       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
5801         {
5802           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, false);
5803           return true;
5804         }
5805       /* Fall through */
5806     case ROTATERT:
5807     case ASHIFT:
5808     case LSHIFTRT:
5809     case ASHIFTRT:
5810       if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
5811         {
5812           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code, false);
5813           return true;
5814         }
5815       else if (mode == SImode)
5816         {
5817           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, false);
5818           /* Slightly disparage register shifts, but not by much.  */
5819           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5820             *total += 1 + rtx_cost (XEXP (x, 1), code, false);
5821           return true;
5822         }
5823
5824       /* Needs a libcall.  */
5825       *total = COSTS_N_INSNS (2);
5826       return false;
5827
5828     case MINUS:
5829       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5830         {
5831           *total = COSTS_N_INSNS (1);
5832           return false;
5833         }
5834
5835       if (mode == SImode)
5836         {
5837           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
5838           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
5839
5840           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
5841               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
5842               || subcode1 == ROTATE || subcode1 == ROTATERT
5843               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
5844               || subcode1 == ASHIFTRT)
5845             {
5846               /* It's just the cost of the two operands.  */
5847               *total = 0;
5848               return false;
5849             }
5850
5851           *total = COSTS_N_INSNS (1);
5852           return false;
5853         }
5854
5855       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5856       return false;
5857
5858     case PLUS:
5859       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5860         {
5861           *total = COSTS_N_INSNS (1);
5862           return false;
5863         }
5864
5865       /* A shift as a part of ADD costs nothing.  */
5866       if (GET_CODE (XEXP (x, 0)) == MULT
5867           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
5868         {
5869           *total = COSTS_N_INSNS (TARGET_THUMB2 ? 2 : 1);
5870           *total += rtx_cost (XEXP (XEXP (x, 0), 0), code, false);
5871           *total += rtx_cost (XEXP (x, 1), code, false);
5872           return true;
5873         }
5874
5875       /* Fall through */
5876     case AND: case XOR: case IOR:
5877       if (mode == SImode)
5878         {
5879           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
5880
5881           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
5882               || subcode == LSHIFTRT || subcode == ASHIFTRT
5883               || (code == AND && subcode == NOT))
5884             {
5885               /* It's just the cost of the two operands.  */
5886               *total = 0;
5887               return false;
5888             }
5889         }
5890
5891       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5892       return false;
5893
5894     case MULT:
5895       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5896       return false;
5897
5898     case NEG:
5899       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5900         {
5901           *total = COSTS_N_INSNS (1);
5902           return false;
5903         }
5904
5905       /* Fall through */
5906     case NOT:
5907       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5908
5909       return false;
5910
5911     case IF_THEN_ELSE:
5912       *total = 0;
5913       return false;
5914
5915     case COMPARE:
5916       if (cc_register (XEXP (x, 0), VOIDmode))
5917         * total = 0;
5918       else
5919         *total = COSTS_N_INSNS (1);
5920       return false;
5921
5922     case ABS:
5923       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5924         *total = COSTS_N_INSNS (1);
5925       else
5926         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
5927       return false;
5928
5929     case SIGN_EXTEND:
5930       *total = 0;
5931       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
5932         {
5933           if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
5934             *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
5935         }
5936       if (mode == DImode)
5937         *total += COSTS_N_INSNS (1);
5938       return false;
5939
5940     case ZERO_EXTEND:
5941       *total = 0;
5942       if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
5943         {
5944           switch (GET_MODE (XEXP (x, 0)))
5945             {
5946             case QImode:
5947               *total += COSTS_N_INSNS (1);
5948               break;
5949
5950             case HImode:
5951               *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
5952
5953             case SImode:
5954               break;
5955
5956             default:
5957               *total += COSTS_N_INSNS (2);
5958             }
5959         }
5960
5961       if (mode == DImode)
5962         *total += COSTS_N_INSNS (1);
5963
5964       return false;
5965
5966     case CONST_INT:
5967       if (const_ok_for_arm (INTVAL (x)))
5968         /* A multiplication by a constant requires another instruction
5969            to load the constant to a register.  */
5970         *total = COSTS_N_INSNS ((outer_code == SET || outer_code == MULT)
5971                                 ? 1 : 0);
5972       else if (const_ok_for_arm (~INTVAL (x)))
5973         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
5974       else if (const_ok_for_arm (-INTVAL (x)))
5975         {
5976           if (outer_code == COMPARE || outer_code == PLUS
5977               || outer_code == MINUS)
5978             *total = 0;
5979           else
5980             *total = COSTS_N_INSNS (1);
5981         }
5982       else
5983         *total = COSTS_N_INSNS (2);
5984       return true;
5985
5986     case CONST:
5987     case LABEL_REF:
5988     case SYMBOL_REF:
5989       *total = COSTS_N_INSNS (2);
5990       return true;
5991
5992     case CONST_DOUBLE:
5993       *total = COSTS_N_INSNS (4);
5994       return true;
5995
5996     case HIGH:
5997     case LO_SUM:
5998       /* We prefer constant pool entries to MOVW/MOVT pairs, so bump the
5999          cost of these slightly.  */
6000       *total = COSTS_N_INSNS (1) + 1;
6001       return true;
6002
6003     default:
6004       if (mode != VOIDmode)
6005         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
6006       else
6007         *total = COSTS_N_INSNS (4); /* How knows?  */
6008       return false;
6009     }
6010 }
6011
6012 /* RTX costs when optimizing for size.  */
6013 static bool
6014 arm_rtx_costs (rtx x, int code, int outer_code, int *total,
6015                bool speed)
6016 {
6017   if (!speed)
6018     return arm_size_rtx_costs (x, (enum rtx_code) code,
6019                                (enum rtx_code) outer_code, total);
6020   else
6021     return all_cores[(int)arm_tune].rtx_costs (x, (enum rtx_code) code,
6022                                                (enum rtx_code) outer_code,
6023                                                total, speed);
6024 }
6025
6026 /* RTX costs for cores with a slow MUL implementation.  Thumb-2 is not
6027    supported on any "slowmul" cores, so it can be ignored.  */
6028
6029 static bool
6030 arm_slowmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
6031                        int *total, bool speed)
6032 {
6033   enum machine_mode mode = GET_MODE (x);
6034
6035   if (TARGET_THUMB)
6036     {
6037       *total = thumb1_rtx_costs (x, code, outer_code);
6038       return true;
6039     }
6040
6041   switch (code)
6042     {
6043     case MULT:
6044       if (GET_MODE_CLASS (mode) == MODE_FLOAT
6045           || mode == DImode)
6046         {
6047           *total = COSTS_N_INSNS (20);
6048           return false;
6049         }
6050
6051       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6052         {
6053           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
6054                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
6055           int cost, const_ok = const_ok_for_arm (i);
6056           int j, booth_unit_size;
6057
6058           /* Tune as appropriate.  */
6059           cost = const_ok ? 4 : 8;
6060           booth_unit_size = 2;
6061           for (j = 0; i && j < 32; j += booth_unit_size)
6062             {
6063               i >>= booth_unit_size;
6064               cost++;
6065             }
6066
6067           *total = COSTS_N_INSNS (cost);
6068           *total += rtx_cost (XEXP (x, 0), code, speed);
6069           return true;
6070         }
6071
6072       *total = COSTS_N_INSNS (20);
6073       return false;
6074
6075     default:
6076       return arm_rtx_costs_1 (x, outer_code, total, speed);;
6077     }
6078 }
6079
6080
6081 /* RTX cost for cores with a fast multiply unit (M variants).  */
6082
6083 static bool
6084 arm_fastmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
6085                        int *total, bool speed)
6086 {
6087   enum machine_mode mode = GET_MODE (x);
6088
6089   if (TARGET_THUMB1)
6090     {
6091       *total = thumb1_rtx_costs (x, code, outer_code);
6092       return true;
6093     }
6094
6095   /* ??? should thumb2 use different costs?  */
6096   switch (code)
6097     {
6098     case MULT:
6099       /* There is no point basing this on the tuning, since it is always the
6100          fast variant if it exists at all.  */
6101       if (mode == DImode
6102           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
6103           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
6104               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
6105         {
6106           *total = COSTS_N_INSNS(2);
6107           return false;
6108         }
6109
6110
6111       if (mode == DImode)
6112         {
6113           *total = COSTS_N_INSNS (5);
6114           return false;
6115         }
6116
6117       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6118         {
6119           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
6120                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
6121           int cost, const_ok = const_ok_for_arm (i);
6122           int j, booth_unit_size;
6123
6124           /* Tune as appropriate.  */
6125           cost = const_ok ? 4 : 8;
6126           booth_unit_size = 8;
6127           for (j = 0; i && j < 32; j += booth_unit_size)
6128             {
6129               i >>= booth_unit_size;
6130               cost++;
6131             }
6132
6133           *total = COSTS_N_INSNS(cost);
6134           return false;
6135         }
6136
6137       if (mode == SImode)
6138         {
6139           *total = COSTS_N_INSNS (4);
6140           return false;
6141         }
6142
6143       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6144         {
6145           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
6146             {
6147               *total = COSTS_N_INSNS (1);
6148               return false;
6149             }
6150         }
6151
6152       /* Requires a lib call */
6153       *total = COSTS_N_INSNS (20);
6154       return false;
6155
6156     default:
6157       return arm_rtx_costs_1 (x, outer_code, total, speed);
6158     }
6159 }
6160
6161
6162 /* RTX cost for XScale CPUs.  Thumb-2 is not supported on any xscale cores,
6163    so it can be ignored.  */
6164
6165 static bool
6166 arm_xscale_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code, int *total, bool speed)
6167 {
6168   enum machine_mode mode = GET_MODE (x);
6169
6170   if (TARGET_THUMB)
6171     {
6172       *total = thumb1_rtx_costs (x, code, outer_code);
6173       return true;
6174     }
6175
6176   switch (code)
6177     {
6178     case COMPARE:
6179       if (GET_CODE (XEXP (x, 0)) != MULT)
6180         return arm_rtx_costs_1 (x, outer_code, total, speed);
6181
6182       /* A COMPARE of a MULT is slow on XScale; the muls instruction
6183          will stall until the multiplication is complete.  */
6184       *total = COSTS_N_INSNS (3);
6185       return false;
6186
6187     case MULT:
6188       /* There is no point basing this on the tuning, since it is always the
6189          fast variant if it exists at all.  */
6190       if (mode == DImode
6191           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
6192           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
6193               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
6194         {
6195           *total = COSTS_N_INSNS (2);
6196           return false;
6197         }
6198
6199
6200       if (mode == DImode)
6201         {
6202           *total = COSTS_N_INSNS (5);
6203           return false;
6204         }
6205
6206       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6207         {
6208           /* If operand 1 is a constant we can more accurately
6209              calculate the cost of the multiply.  The multiplier can
6210              retire 15 bits on the first cycle and a further 12 on the
6211              second.  We do, of course, have to load the constant into
6212              a register first.  */
6213           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
6214           /* There's a general overhead of one cycle.  */
6215           int cost = 1;
6216           unsigned HOST_WIDE_INT masked_const;
6217
6218           if (i & 0x80000000)
6219             i = ~i;
6220
6221           i &= (unsigned HOST_WIDE_INT) 0xffffffff;
6222
6223           masked_const = i & 0xffff8000;
6224           if (masked_const != 0)
6225             {
6226               cost++;
6227               masked_const = i & 0xf8000000;
6228               if (masked_const != 0)
6229                 cost++;
6230             }
6231           *total = COSTS_N_INSNS (cost);
6232           return false;
6233         }
6234
6235       if (mode == SImode)
6236         {
6237           *total = COSTS_N_INSNS (3);
6238           return false;
6239         }
6240
6241       /* Requires a lib call */
6242       *total = COSTS_N_INSNS (20);
6243       return false;
6244
6245     default:
6246       return arm_rtx_costs_1 (x, outer_code, total, speed);
6247     }
6248 }
6249
6250
6251 /* RTX costs for 9e (and later) cores.  */
6252
6253 static bool
6254 arm_9e_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
6255                   int *total, bool speed)
6256 {
6257   enum machine_mode mode = GET_MODE (x);
6258
6259   if (TARGET_THUMB1)
6260     {
6261       switch (code)
6262         {
6263         case MULT:
6264           *total = COSTS_N_INSNS (3);
6265           return true;
6266
6267         default:
6268           *total = thumb1_rtx_costs (x, code, outer_code);
6269           return true;
6270         }
6271     }
6272
6273   switch (code)
6274     {
6275     case MULT:
6276       /* There is no point basing this on the tuning, since it is always the
6277          fast variant if it exists at all.  */
6278       if (mode == DImode
6279           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
6280           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
6281               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
6282         {
6283           *total = COSTS_N_INSNS (2);
6284           return false;
6285         }
6286
6287
6288       if (mode == DImode)
6289         {
6290           *total = COSTS_N_INSNS (5);
6291           return false;
6292         }
6293
6294       if (mode == SImode)
6295         {
6296           *total = COSTS_N_INSNS (2);
6297           return false;
6298         }
6299
6300       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6301         {
6302           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
6303             {
6304               *total = COSTS_N_INSNS (1);
6305               return false;
6306             }
6307         }
6308
6309       *total = COSTS_N_INSNS (20);
6310       return false;
6311
6312     default:
6313       return arm_rtx_costs_1 (x, outer_code, total, speed);
6314     }
6315 }
6316 /* All address computations that can be done are free, but rtx cost returns
6317    the same for practically all of them.  So we weight the different types
6318    of address here in the order (most pref first):
6319    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
6320 static inline int
6321 arm_arm_address_cost (rtx x)
6322 {
6323   enum rtx_code c  = GET_CODE (x);
6324
6325   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
6326     return 0;
6327   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
6328     return 10;
6329
6330   if (c == PLUS || c == MINUS)
6331     {
6332       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
6333         return 2;
6334
6335       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
6336         return 3;
6337
6338       return 4;
6339     }
6340
6341   return 6;
6342 }
6343
6344 static inline int
6345 arm_thumb_address_cost (rtx x)
6346 {
6347   enum rtx_code c  = GET_CODE (x);
6348
6349   if (c == REG)
6350     return 1;
6351   if (c == PLUS
6352       && GET_CODE (XEXP (x, 0)) == REG
6353       && GET_CODE (XEXP (x, 1)) == CONST_INT)
6354     return 1;
6355
6356   return 2;
6357 }
6358
6359 static int
6360 arm_address_cost (rtx x, bool speed ATTRIBUTE_UNUSED)
6361 {
6362   return TARGET_32BIT ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
6363 }
6364
6365 static int
6366 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
6367 {
6368   rtx i_pat, d_pat;
6369
6370   /* Some true dependencies can have a higher cost depending
6371      on precisely how certain input operands are used.  */
6372   if (arm_tune_xscale
6373       && REG_NOTE_KIND (link) == 0
6374       && recog_memoized (insn) >= 0
6375       && recog_memoized (dep) >= 0)
6376     {
6377       int shift_opnum = get_attr_shift (insn);
6378       enum attr_type attr_type = get_attr_type (dep);
6379
6380       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
6381          operand for INSN.  If we have a shifted input operand and the
6382          instruction we depend on is another ALU instruction, then we may
6383          have to account for an additional stall.  */
6384       if (shift_opnum != 0
6385           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
6386         {
6387           rtx shifted_operand;
6388           int opno;
6389
6390           /* Get the shifted operand.  */
6391           extract_insn (insn);
6392           shifted_operand = recog_data.operand[shift_opnum];
6393
6394           /* Iterate over all the operands in DEP.  If we write an operand
6395              that overlaps with SHIFTED_OPERAND, then we have increase the
6396              cost of this dependency.  */
6397           extract_insn (dep);
6398           preprocess_constraints ();
6399           for (opno = 0; opno < recog_data.n_operands; opno++)
6400             {
6401               /* We can ignore strict inputs.  */
6402               if (recog_data.operand_type[opno] == OP_IN)
6403                 continue;
6404
6405               if (reg_overlap_mentioned_p (recog_data.operand[opno],
6406                                            shifted_operand))
6407                 return 2;
6408             }
6409         }
6410     }
6411
6412   /* XXX This is not strictly true for the FPA.  */
6413   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
6414       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
6415     return 0;
6416
6417   /* Call insns don't incur a stall, even if they follow a load.  */
6418   if (REG_NOTE_KIND (link) == 0
6419       && GET_CODE (insn) == CALL_INSN)
6420     return 1;
6421
6422   if ((i_pat = single_set (insn)) != NULL
6423       && GET_CODE (SET_SRC (i_pat)) == MEM
6424       && (d_pat = single_set (dep)) != NULL
6425       && GET_CODE (SET_DEST (d_pat)) == MEM)
6426     {
6427       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
6428       /* This is a load after a store, there is no conflict if the load reads
6429          from a cached area.  Assume that loads from the stack, and from the
6430          constant pool are cached, and that others will miss.  This is a
6431          hack.  */
6432
6433       if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
6434           || reg_mentioned_p (stack_pointer_rtx, src_mem)
6435           || reg_mentioned_p (frame_pointer_rtx, src_mem)
6436           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
6437         return 1;
6438     }
6439
6440   return cost;
6441 }
6442
6443 static int fp_consts_inited = 0;
6444
6445 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
6446 static const char * const strings_fp[8] =
6447 {
6448   "0",   "1",   "2",   "3",
6449   "4",   "5",   "0.5", "10"
6450 };
6451
6452 static REAL_VALUE_TYPE values_fp[8];
6453
6454 static void
6455 init_fp_table (void)
6456 {
6457   int i;
6458   REAL_VALUE_TYPE r;
6459
6460   if (TARGET_VFP)
6461     fp_consts_inited = 1;
6462   else
6463     fp_consts_inited = 8;
6464
6465   for (i = 0; i < fp_consts_inited; i++)
6466     {
6467       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
6468       values_fp[i] = r;
6469     }
6470 }
6471
6472 /* Return TRUE if rtx X is a valid immediate FP constant.  */
6473 int
6474 arm_const_double_rtx (rtx x)
6475 {
6476   REAL_VALUE_TYPE r;
6477   int i;
6478
6479   if (!fp_consts_inited)
6480     init_fp_table ();
6481
6482   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
6483   if (REAL_VALUE_MINUS_ZERO (r))
6484     return 0;
6485
6486   for (i = 0; i < fp_consts_inited; i++)
6487     if (REAL_VALUES_EQUAL (r, values_fp[i]))
6488       return 1;
6489
6490   return 0;
6491 }
6492
6493 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
6494 int
6495 neg_const_double_rtx_ok_for_fpa (rtx x)
6496 {
6497   REAL_VALUE_TYPE r;
6498   int i;
6499
6500   if (!fp_consts_inited)
6501     init_fp_table ();
6502
6503   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
6504   r = REAL_VALUE_NEGATE (r);
6505   if (REAL_VALUE_MINUS_ZERO (r))
6506     return 0;
6507
6508   for (i = 0; i < 8; i++)
6509     if (REAL_VALUES_EQUAL (r, values_fp[i]))
6510       return 1;
6511
6512   return 0;
6513 }
6514
6515
6516 /* VFPv3 has a fairly wide range of representable immediates, formed from
6517    "quarter-precision" floating-point values. These can be evaluated using this
6518    formula (with ^ for exponentiation):
6519
6520      -1^s * n * 2^-r
6521
6522    Where 's' is a sign bit (0/1), 'n' and 'r' are integers such that
6523    16 <= n <= 31 and 0 <= r <= 7.
6524
6525    These values are mapped onto an 8-bit integer ABCDEFGH s.t.
6526
6527      - A (most-significant) is the sign bit.
6528      - BCD are the exponent (encoded as r XOR 3).
6529      - EFGH are the mantissa (encoded as n - 16).
6530 */
6531
6532 /* Return an integer index for a VFPv3 immediate operand X suitable for the
6533    fconst[sd] instruction, or -1 if X isn't suitable.  */
6534 static int
6535 vfp3_const_double_index (rtx x)
6536 {
6537   REAL_VALUE_TYPE r, m;
6538   int sign, exponent;
6539   unsigned HOST_WIDE_INT mantissa, mant_hi;
6540   unsigned HOST_WIDE_INT mask;
6541   HOST_WIDE_INT m1, m2;
6542   int point_pos = 2 * HOST_BITS_PER_WIDE_INT - 1;
6543
6544   if (!TARGET_VFP3 || GET_CODE (x) != CONST_DOUBLE)
6545     return -1;
6546
6547   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
6548
6549   /* We can't represent these things, so detect them first.  */
6550   if (REAL_VALUE_ISINF (r) || REAL_VALUE_ISNAN (r) || REAL_VALUE_MINUS_ZERO (r))
6551     return -1;
6552
6553   /* Extract sign, exponent and mantissa.  */
6554   sign = REAL_VALUE_NEGATIVE (r) ? 1 : 0;
6555   r = REAL_VALUE_ABS (r);
6556   exponent = REAL_EXP (&r);
6557   /* For the mantissa, we expand into two HOST_WIDE_INTS, apart from the
6558      highest (sign) bit, with a fixed binary point at bit point_pos.
6559      WARNING: If there's ever a VFP version which uses more than 2 * H_W_I - 1
6560      bits for the mantissa, this may fail (low bits would be lost).  */
6561   real_ldexp (&m, &r, point_pos - exponent);
6562   REAL_VALUE_TO_INT (&m1, &m2, m);
6563   mantissa = m1;
6564   mant_hi = m2;
6565
6566   /* If there are bits set in the low part of the mantissa, we can't
6567      represent this value.  */
6568   if (mantissa != 0)
6569     return -1;
6570
6571   /* Now make it so that mantissa contains the most-significant bits, and move
6572      the point_pos to indicate that the least-significant bits have been
6573      discarded.  */
6574   point_pos -= HOST_BITS_PER_WIDE_INT;
6575   mantissa = mant_hi;
6576
6577   /* We can permit four significant bits of mantissa only, plus a high bit
6578      which is always 1.  */
6579   mask = ((unsigned HOST_WIDE_INT)1 << (point_pos - 5)) - 1;
6580   if ((mantissa & mask) != 0)
6581     return -1;
6582
6583   /* Now we know the mantissa is in range, chop off the unneeded bits.  */
6584   mantissa >>= point_pos - 5;
6585
6586   /* The mantissa may be zero. Disallow that case. (It's possible to load the
6587      floating-point immediate zero with Neon using an integer-zero load, but
6588      that case is handled elsewhere.)  */
6589   if (mantissa == 0)
6590     return -1;
6591
6592   gcc_assert (mantissa >= 16 && mantissa <= 31);
6593
6594   /* The value of 5 here would be 4 if GCC used IEEE754-like encoding (where
6595      normalized significands are in the range [1, 2). (Our mantissa is shifted
6596      left 4 places at this point relative to normalized IEEE754 values).  GCC
6597      internally uses [0.5, 1) (see real.c), so the exponent returned from
6598      REAL_EXP must be altered.  */
6599   exponent = 5 - exponent;
6600
6601   if (exponent < 0 || exponent > 7)
6602     return -1;
6603
6604   /* Sign, mantissa and exponent are now in the correct form to plug into the
6605      formula described in the comment above.  */
6606   return (sign << 7) | ((exponent ^ 3) << 4) | (mantissa - 16);
6607 }
6608
6609 /* Return TRUE if rtx X is a valid immediate VFPv3 constant.  */
6610 int
6611 vfp3_const_double_rtx (rtx x)
6612 {
6613   if (!TARGET_VFP3)
6614     return 0;
6615
6616   return vfp3_const_double_index (x) != -1;
6617 }
6618
6619 /* Recognize immediates which can be used in various Neon instructions. Legal
6620    immediates are described by the following table (for VMVN variants, the
6621    bitwise inverse of the constant shown is recognized. In either case, VMOV
6622    is output and the correct instruction to use for a given constant is chosen
6623    by the assembler). The constant shown is replicated across all elements of
6624    the destination vector.
6625
6626    insn elems variant constant (binary)
6627    ---- ----- ------- -----------------
6628    vmov  i32     0    00000000 00000000 00000000 abcdefgh
6629    vmov  i32     1    00000000 00000000 abcdefgh 00000000
6630    vmov  i32     2    00000000 abcdefgh 00000000 00000000
6631    vmov  i32     3    abcdefgh 00000000 00000000 00000000
6632    vmov  i16     4    00000000 abcdefgh
6633    vmov  i16     5    abcdefgh 00000000
6634    vmvn  i32     6    00000000 00000000 00000000 abcdefgh
6635    vmvn  i32     7    00000000 00000000 abcdefgh 00000000
6636    vmvn  i32     8    00000000 abcdefgh 00000000 00000000
6637    vmvn  i32     9    abcdefgh 00000000 00000000 00000000
6638    vmvn  i16    10    00000000 abcdefgh
6639    vmvn  i16    11    abcdefgh 00000000
6640    vmov  i32    12    00000000 00000000 abcdefgh 11111111
6641    vmvn  i32    13    00000000 00000000 abcdefgh 11111111
6642    vmov  i32    14    00000000 abcdefgh 11111111 11111111
6643    vmvn  i32    15    00000000 abcdefgh 11111111 11111111
6644    vmov   i8    16    abcdefgh
6645    vmov  i64    17    aaaaaaaa bbbbbbbb cccccccc dddddddd
6646                       eeeeeeee ffffffff gggggggg hhhhhhhh
6647    vmov  f32    18    aBbbbbbc defgh000 00000000 00000000
6648
6649    For case 18, B = !b. Representable values are exactly those accepted by
6650    vfp3_const_double_index, but are output as floating-point numbers rather
6651    than indices.
6652
6653    Variants 0-5 (inclusive) may also be used as immediates for the second
6654    operand of VORR/VBIC instructions.
6655
6656    The INVERSE argument causes the bitwise inverse of the given operand to be
6657    recognized instead (used for recognizing legal immediates for the VAND/VORN
6658    pseudo-instructions). If INVERSE is true, the value placed in *MODCONST is
6659    *not* inverted (i.e. the pseudo-instruction forms vand/vorn should still be
6660    output, rather than the real insns vbic/vorr).
6661
6662    INVERSE makes no difference to the recognition of float vectors.
6663
6664    The return value is the variant of immediate as shown in the above table, or
6665    -1 if the given value doesn't match any of the listed patterns.
6666 */
6667 static int
6668 neon_valid_immediate (rtx op, enum machine_mode mode, int inverse,
6669                       rtx *modconst, int *elementwidth)
6670 {
6671 #define CHECK(STRIDE, ELSIZE, CLASS, TEST)      \
6672   matches = 1;                                  \
6673   for (i = 0; i < idx; i += (STRIDE))           \
6674     if (!(TEST))                                \
6675       matches = 0;                              \
6676   if (matches)                                  \
6677     {                                           \
6678       immtype = (CLASS);                        \
6679       elsize = (ELSIZE);                        \
6680       break;                                    \
6681     }
6682
6683   unsigned int i, elsize = 0, idx = 0, n_elts = CONST_VECTOR_NUNITS (op);
6684   unsigned int innersize = GET_MODE_SIZE (GET_MODE_INNER (mode));
6685   unsigned char bytes[16];
6686   int immtype = -1, matches;
6687   unsigned int invmask = inverse ? 0xff : 0;
6688
6689   /* Vectors of float constants.  */
6690   if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
6691     {
6692       rtx el0 = CONST_VECTOR_ELT (op, 0);
6693       REAL_VALUE_TYPE r0;
6694
6695       if (!vfp3_const_double_rtx (el0))
6696         return -1;
6697
6698       REAL_VALUE_FROM_CONST_DOUBLE (r0, el0);
6699
6700       for (i = 1; i < n_elts; i++)
6701         {
6702           rtx elt = CONST_VECTOR_ELT (op, i);
6703           REAL_VALUE_TYPE re;
6704
6705           REAL_VALUE_FROM_CONST_DOUBLE (re, elt);
6706
6707           if (!REAL_VALUES_EQUAL (r0, re))
6708             return -1;
6709         }
6710
6711       if (modconst)
6712         *modconst = CONST_VECTOR_ELT (op, 0);
6713
6714       if (elementwidth)
6715         *elementwidth = 0;
6716
6717       return 18;
6718     }
6719
6720   /* Splat vector constant out into a byte vector.  */
6721   for (i = 0; i < n_elts; i++)
6722     {
6723       rtx el = CONST_VECTOR_ELT (op, i);
6724       unsigned HOST_WIDE_INT elpart;
6725       unsigned int part, parts;
6726
6727       if (GET_CODE (el) == CONST_INT)
6728         {
6729           elpart = INTVAL (el);
6730           parts = 1;
6731         }
6732       else if (GET_CODE (el) == CONST_DOUBLE)
6733         {
6734           elpart = CONST_DOUBLE_LOW (el);
6735           parts = 2;
6736         }
6737       else
6738         gcc_unreachable ();
6739
6740       for (part = 0; part < parts; part++)
6741         {
6742           unsigned int byte;
6743           for (byte = 0; byte < innersize; byte++)
6744             {
6745               bytes[idx++] = (elpart & 0xff) ^ invmask;
6746               elpart >>= BITS_PER_UNIT;
6747             }
6748           if (GET_CODE (el) == CONST_DOUBLE)
6749             elpart = CONST_DOUBLE_HIGH (el);
6750         }
6751     }
6752
6753   /* Sanity check.  */
6754   gcc_assert (idx == GET_MODE_SIZE (mode));
6755
6756   do
6757     {
6758       CHECK (4, 32, 0, bytes[i] == bytes[0] && bytes[i + 1] == 0
6759                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
6760
6761       CHECK (4, 32, 1, bytes[i] == 0 && bytes[i + 1] == bytes[1]
6762                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
6763
6764       CHECK (4, 32, 2, bytes[i] == 0 && bytes[i + 1] == 0
6765                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
6766
6767       CHECK (4, 32, 3, bytes[i] == 0 && bytes[i + 1] == 0
6768                        && bytes[i + 2] == 0 && bytes[i + 3] == bytes[3]);
6769
6770       CHECK (2, 16, 4, bytes[i] == bytes[0] && bytes[i + 1] == 0);
6771
6772       CHECK (2, 16, 5, bytes[i] == 0 && bytes[i + 1] == bytes[1]);
6773
6774       CHECK (4, 32, 6, bytes[i] == bytes[0] && bytes[i + 1] == 0xff
6775                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
6776
6777       CHECK (4, 32, 7, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
6778                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
6779
6780       CHECK (4, 32, 8, bytes[i] == 0xff && bytes[i + 1] == 0xff
6781                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
6782
6783       CHECK (4, 32, 9, bytes[i] == 0xff && bytes[i + 1] == 0xff
6784                        && bytes[i + 2] == 0xff && bytes[i + 3] == bytes[3]);
6785
6786       CHECK (2, 16, 10, bytes[i] == bytes[0] && bytes[i + 1] == 0xff);
6787
6788       CHECK (2, 16, 11, bytes[i] == 0xff && bytes[i + 1] == bytes[1]);
6789
6790       CHECK (4, 32, 12, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
6791                         && bytes[i + 2] == 0 && bytes[i + 3] == 0);
6792
6793       CHECK (4, 32, 13, bytes[i] == 0 && bytes[i + 1] == bytes[1]
6794                         && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
6795
6796       CHECK (4, 32, 14, bytes[i] == 0xff && bytes[i + 1] == 0xff
6797                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
6798
6799       CHECK (4, 32, 15, bytes[i] == 0 && bytes[i + 1] == 0
6800                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
6801
6802       CHECK (1, 8, 16, bytes[i] == bytes[0]);
6803
6804       CHECK (1, 64, 17, (bytes[i] == 0 || bytes[i] == 0xff)
6805                         && bytes[i] == bytes[(i + 8) % idx]);
6806     }
6807   while (0);
6808
6809   if (immtype == -1)
6810     return -1;
6811
6812   if (elementwidth)
6813     *elementwidth = elsize;
6814
6815   if (modconst)
6816     {
6817       unsigned HOST_WIDE_INT imm = 0;
6818
6819       /* Un-invert bytes of recognized vector, if necessary.  */
6820       if (invmask != 0)
6821         for (i = 0; i < idx; i++)
6822           bytes[i] ^= invmask;
6823
6824       if (immtype == 17)
6825         {
6826           /* FIXME: Broken on 32-bit H_W_I hosts.  */
6827           gcc_assert (sizeof (HOST_WIDE_INT) == 8);
6828
6829           for (i = 0; i < 8; i++)
6830             imm |= (unsigned HOST_WIDE_INT) (bytes[i] ? 0xff : 0)
6831                    << (i * BITS_PER_UNIT);
6832
6833           *modconst = GEN_INT (imm);
6834         }
6835       else
6836         {
6837           unsigned HOST_WIDE_INT imm = 0;
6838
6839           for (i = 0; i < elsize / BITS_PER_UNIT; i++)
6840             imm |= (unsigned HOST_WIDE_INT) bytes[i] << (i * BITS_PER_UNIT);
6841
6842           *modconst = GEN_INT (imm);
6843         }
6844     }
6845
6846   return immtype;
6847 #undef CHECK
6848 }
6849
6850 /* Return TRUE if rtx X is legal for use as either a Neon VMOV (or, implicitly,
6851    VMVN) immediate. Write back width per element to *ELEMENTWIDTH (or zero for
6852    float elements), and a modified constant (whatever should be output for a
6853    VMOV) in *MODCONST.  */
6854
6855 int
6856 neon_immediate_valid_for_move (rtx op, enum machine_mode mode,
6857                                rtx *modconst, int *elementwidth)
6858 {
6859   rtx tmpconst;
6860   int tmpwidth;
6861   int retval = neon_valid_immediate (op, mode, 0, &tmpconst, &tmpwidth);
6862
6863   if (retval == -1)
6864     return 0;
6865
6866   if (modconst)
6867     *modconst = tmpconst;
6868
6869   if (elementwidth)
6870     *elementwidth = tmpwidth;
6871
6872   return 1;
6873 }
6874
6875 /* Return TRUE if rtx X is legal for use in a VORR or VBIC instruction.  If
6876    the immediate is valid, write a constant suitable for using as an operand
6877    to VORR/VBIC/VAND/VORN to *MODCONST and the corresponding element width to
6878    *ELEMENTWIDTH. See neon_valid_immediate for description of INVERSE.  */
6879
6880 int
6881 neon_immediate_valid_for_logic (rtx op, enum machine_mode mode, int inverse,
6882                                 rtx *modconst, int *elementwidth)
6883 {
6884   rtx tmpconst;
6885   int tmpwidth;
6886   int retval = neon_valid_immediate (op, mode, inverse, &tmpconst, &tmpwidth);
6887
6888   if (retval < 0 || retval > 5)
6889     return 0;
6890
6891   if (modconst)
6892     *modconst = tmpconst;
6893
6894   if (elementwidth)
6895     *elementwidth = tmpwidth;
6896
6897   return 1;
6898 }
6899
6900 /* Return a string suitable for output of Neon immediate logic operation
6901    MNEM.  */
6902
6903 char *
6904 neon_output_logic_immediate (const char *mnem, rtx *op2, enum machine_mode mode,
6905                              int inverse, int quad)
6906 {
6907   int width, is_valid;
6908   static char templ[40];
6909
6910   is_valid = neon_immediate_valid_for_logic (*op2, mode, inverse, op2, &width);
6911
6912   gcc_assert (is_valid != 0);
6913
6914   if (quad)
6915     sprintf (templ, "%s.i%d\t%%q0, %%2", mnem, width);
6916   else
6917     sprintf (templ, "%s.i%d\t%%P0, %%2", mnem, width);
6918
6919   return templ;
6920 }
6921
6922 /* Output a sequence of pairwise operations to implement a reduction.
6923    NOTE: We do "too much work" here, because pairwise operations work on two
6924    registers-worth of operands in one go. Unfortunately we can't exploit those
6925    extra calculations to do the full operation in fewer steps, I don't think.
6926    Although all vector elements of the result but the first are ignored, we
6927    actually calculate the same result in each of the elements. An alternative
6928    such as initially loading a vector with zero to use as each of the second
6929    operands would use up an additional register and take an extra instruction,
6930    for no particular gain.  */
6931
6932 void
6933 neon_pairwise_reduce (rtx op0, rtx op1, enum machine_mode mode,
6934                       rtx (*reduc) (rtx, rtx, rtx))
6935 {
6936   enum machine_mode inner = GET_MODE_INNER (mode);
6937   unsigned int i, parts = GET_MODE_SIZE (mode) / GET_MODE_SIZE (inner);
6938   rtx tmpsum = op1;
6939
6940   for (i = parts / 2; i >= 1; i /= 2)
6941     {
6942       rtx dest = (i == 1) ? op0 : gen_reg_rtx (mode);
6943       emit_insn (reduc (dest, tmpsum, tmpsum));
6944       tmpsum = dest;
6945     }
6946 }
6947
6948 /* Initialize a vector with non-constant elements.  FIXME: We can do better
6949    than the current implementation (building a vector on the stack and then
6950    loading it) in many cases.  See rs6000.c.  */
6951
6952 void
6953 neon_expand_vector_init (rtx target, rtx vals)
6954 {
6955   enum machine_mode mode = GET_MODE (target);
6956   enum machine_mode inner = GET_MODE_INNER (mode);
6957   unsigned int i, n_elts = GET_MODE_NUNITS (mode);
6958   rtx mem;
6959
6960   gcc_assert (VECTOR_MODE_P (mode));
6961
6962   mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), 0);
6963   for (i = 0; i < n_elts; i++)
6964     emit_move_insn (adjust_address_nv (mem, inner, i * GET_MODE_SIZE (inner)),
6965                    XVECEXP (vals, 0, i));
6966
6967   emit_move_insn (target, mem);
6968 }
6969
6970 /* Ensure OPERAND lies between LOW (inclusive) and HIGH (exclusive).  Raise
6971    ERR if it doesn't.  FIXME: NEON bounds checks occur late in compilation, so
6972    reported source locations are bogus.  */
6973
6974 static void
6975 bounds_check (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high,
6976               const char *err)
6977 {
6978   HOST_WIDE_INT lane;
6979
6980   gcc_assert (GET_CODE (operand) == CONST_INT);
6981
6982   lane = INTVAL (operand);
6983
6984   if (lane < low || lane >= high)
6985     error (err);
6986 }
6987
6988 /* Bounds-check lanes.  */
6989
6990 void
6991 neon_lane_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
6992 {
6993   bounds_check (operand, low, high, "lane out of range");
6994 }
6995
6996 /* Bounds-check constants.  */
6997
6998 void
6999 neon_const_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
7000 {
7001   bounds_check (operand, low, high, "constant out of range");
7002 }
7003
7004 HOST_WIDE_INT
7005 neon_element_bits (enum machine_mode mode)
7006 {
7007   if (mode == DImode)
7008     return GET_MODE_BITSIZE (mode);
7009   else
7010     return GET_MODE_BITSIZE (GET_MODE_INNER (mode));
7011 }
7012
7013 \f
7014 /* Predicates for `match_operand' and `match_operator'.  */
7015
7016 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
7017 int
7018 cirrus_memory_offset (rtx op)
7019 {
7020   /* Reject eliminable registers.  */
7021   if (! (reload_in_progress || reload_completed)
7022       && (   reg_mentioned_p (frame_pointer_rtx, op)
7023           || reg_mentioned_p (arg_pointer_rtx, op)
7024           || reg_mentioned_p (virtual_incoming_args_rtx, op)
7025           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
7026           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
7027           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
7028     return 0;
7029
7030   if (GET_CODE (op) == MEM)
7031     {
7032       rtx ind;
7033
7034       ind = XEXP (op, 0);
7035
7036       /* Match: (mem (reg)).  */
7037       if (GET_CODE (ind) == REG)
7038         return 1;
7039
7040       /* Match:
7041          (mem (plus (reg)
7042                     (const))).  */
7043       if (GET_CODE (ind) == PLUS
7044           && GET_CODE (XEXP (ind, 0)) == REG
7045           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
7046           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
7047         return 1;
7048     }
7049
7050   return 0;
7051 }
7052
7053 /* Return TRUE if OP is a valid coprocessor memory address pattern.
7054    WB is true if full writeback address modes are allowed and is false
7055    if limited writeback address modes (POST_INC and PRE_DEC) are
7056    allowed.  */
7057
7058 int
7059 arm_coproc_mem_operand (rtx op, bool wb)
7060 {
7061   rtx ind;
7062
7063   /* Reject eliminable registers.  */
7064   if (! (reload_in_progress || reload_completed)
7065       && (   reg_mentioned_p (frame_pointer_rtx, op)
7066           || reg_mentioned_p (arg_pointer_rtx, op)
7067           || reg_mentioned_p (virtual_incoming_args_rtx, op)
7068           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
7069           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
7070           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
7071     return FALSE;
7072
7073   /* Constants are converted into offsets from labels.  */
7074   if (GET_CODE (op) != MEM)
7075     return FALSE;
7076
7077   ind = XEXP (op, 0);
7078
7079   if (reload_completed
7080       && (GET_CODE (ind) == LABEL_REF
7081           || (GET_CODE (ind) == CONST
7082               && GET_CODE (XEXP (ind, 0)) == PLUS
7083               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
7084               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
7085     return TRUE;
7086
7087   /* Match: (mem (reg)).  */
7088   if (GET_CODE (ind) == REG)
7089     return arm_address_register_rtx_p (ind, 0);
7090
7091   /* Autoincremment addressing modes.  POST_INC and PRE_DEC are
7092      acceptable in any case (subject to verification by
7093      arm_address_register_rtx_p).  We need WB to be true to accept
7094      PRE_INC and POST_DEC.  */
7095   if (GET_CODE (ind) == POST_INC
7096       || GET_CODE (ind) == PRE_DEC
7097       || (wb
7098           && (GET_CODE (ind) == PRE_INC
7099               || GET_CODE (ind) == POST_DEC)))
7100     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
7101
7102   if (wb
7103       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
7104       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
7105       && GET_CODE (XEXP (ind, 1)) == PLUS
7106       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
7107     ind = XEXP (ind, 1);
7108
7109   /* Match:
7110      (plus (reg)
7111            (const)).  */
7112   if (GET_CODE (ind) == PLUS
7113       && GET_CODE (XEXP (ind, 0)) == REG
7114       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
7115       && GET_CODE (XEXP (ind, 1)) == CONST_INT
7116       && INTVAL (XEXP (ind, 1)) > -1024
7117       && INTVAL (XEXP (ind, 1)) <  1024
7118       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
7119     return TRUE;
7120
7121   return FALSE;
7122 }
7123
7124 /* Return TRUE if OP is a memory operand which we can load or store a vector
7125    to/from. TYPE is one of the following values:
7126     0 - Vector load/stor (vldr)
7127     1 - Core registers (ldm)
7128     2 - Element/structure loads (vld1)
7129  */
7130 int
7131 neon_vector_mem_operand (rtx op, int type)
7132 {
7133   rtx ind;
7134
7135   /* Reject eliminable registers.  */
7136   if (! (reload_in_progress || reload_completed)
7137       && (   reg_mentioned_p (frame_pointer_rtx, op)
7138           || reg_mentioned_p (arg_pointer_rtx, op)
7139           || reg_mentioned_p (virtual_incoming_args_rtx, op)
7140           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
7141           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
7142           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
7143     return FALSE;
7144
7145   /* Constants are converted into offsets from labels.  */
7146   if (GET_CODE (op) != MEM)
7147     return FALSE;
7148
7149   ind = XEXP (op, 0);
7150
7151   if (reload_completed
7152       && (GET_CODE (ind) == LABEL_REF
7153           || (GET_CODE (ind) == CONST
7154               && GET_CODE (XEXP (ind, 0)) == PLUS
7155               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
7156               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
7157     return TRUE;
7158
7159   /* Match: (mem (reg)).  */
7160   if (GET_CODE (ind) == REG)
7161     return arm_address_register_rtx_p (ind, 0);
7162
7163   /* Allow post-increment with Neon registers.  */
7164   if (type != 1 && (GET_CODE (ind) == POST_INC || GET_CODE (ind) == PRE_DEC))
7165     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
7166
7167   /* FIXME: vld1 allows register post-modify.  */
7168
7169   /* Match:
7170      (plus (reg)
7171           (const)).  */
7172   if (type == 0
7173       && GET_CODE (ind) == PLUS
7174       && GET_CODE (XEXP (ind, 0)) == REG
7175       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
7176       && GET_CODE (XEXP (ind, 1)) == CONST_INT
7177       && INTVAL (XEXP (ind, 1)) > -1024
7178       && INTVAL (XEXP (ind, 1)) < 1016
7179       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
7180     return TRUE;
7181
7182   return FALSE;
7183 }
7184
7185 /* Return TRUE if OP is a mem suitable for loading/storing a Neon struct
7186    type.  */
7187 int
7188 neon_struct_mem_operand (rtx op)
7189 {
7190   rtx ind;
7191
7192   /* Reject eliminable registers.  */
7193   if (! (reload_in_progress || reload_completed)
7194       && (   reg_mentioned_p (frame_pointer_rtx, op)
7195           || reg_mentioned_p (arg_pointer_rtx, op)
7196           || reg_mentioned_p (virtual_incoming_args_rtx, op)
7197           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
7198           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
7199           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
7200     return FALSE;
7201
7202   /* Constants are converted into offsets from labels.  */
7203   if (GET_CODE (op) != MEM)
7204     return FALSE;
7205
7206   ind = XEXP (op, 0);
7207
7208   if (reload_completed
7209       && (GET_CODE (ind) == LABEL_REF
7210           || (GET_CODE (ind) == CONST
7211               && GET_CODE (XEXP (ind, 0)) == PLUS
7212               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
7213               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
7214     return TRUE;
7215
7216   /* Match: (mem (reg)).  */
7217   if (GET_CODE (ind) == REG)
7218     return arm_address_register_rtx_p (ind, 0);
7219
7220   return FALSE;
7221 }
7222
7223 /* Return true if X is a register that will be eliminated later on.  */
7224 int
7225 arm_eliminable_register (rtx x)
7226 {
7227   return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
7228                        || REGNO (x) == ARG_POINTER_REGNUM
7229                        || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
7230                            && REGNO (x) <= LAST_VIRTUAL_REGISTER));
7231 }
7232
7233 /* Return GENERAL_REGS if a scratch register required to reload x to/from
7234    coprocessor registers.  Otherwise return NO_REGS.  */
7235
7236 enum reg_class
7237 coproc_secondary_reload_class (enum machine_mode mode, rtx x, bool wb)
7238 {
7239   if (mode == HFmode)
7240     {
7241       if (s_register_operand (x, mode) || neon_vector_mem_operand (x, 2))
7242         return NO_REGS;
7243       return GENERAL_REGS;
7244     }
7245
7246   if (TARGET_NEON
7247       && (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
7248           || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
7249       && neon_vector_mem_operand (x, 0))
7250      return NO_REGS;
7251
7252   if (arm_coproc_mem_operand (x, wb) || s_register_operand (x, mode))
7253     return NO_REGS;
7254
7255   return GENERAL_REGS;
7256 }
7257
7258 /* Values which must be returned in the most-significant end of the return
7259    register.  */
7260
7261 static bool
7262 arm_return_in_msb (const_tree valtype)
7263 {
7264   return (TARGET_AAPCS_BASED
7265           && BYTES_BIG_ENDIAN
7266           && (AGGREGATE_TYPE_P (valtype)
7267               || TREE_CODE (valtype) == COMPLEX_TYPE));
7268 }
7269
7270 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
7271    Use by the Cirrus Maverick code which has to workaround
7272    a hardware bug triggered by such instructions.  */
7273 static bool
7274 arm_memory_load_p (rtx insn)
7275 {
7276   rtx body, lhs, rhs;;
7277
7278   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
7279     return false;
7280
7281   body = PATTERN (insn);
7282
7283   if (GET_CODE (body) != SET)
7284     return false;
7285
7286   lhs = XEXP (body, 0);
7287   rhs = XEXP (body, 1);
7288
7289   lhs = REG_OR_SUBREG_RTX (lhs);
7290
7291   /* If the destination is not a general purpose
7292      register we do not have to worry.  */
7293   if (GET_CODE (lhs) != REG
7294       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
7295     return false;
7296
7297   /* As well as loads from memory we also have to react
7298      to loads of invalid constants which will be turned
7299      into loads from the minipool.  */
7300   return (GET_CODE (rhs) == MEM
7301           || GET_CODE (rhs) == SYMBOL_REF
7302           || note_invalid_constants (insn, -1, false));
7303 }
7304
7305 /* Return TRUE if INSN is a Cirrus instruction.  */
7306 static bool
7307 arm_cirrus_insn_p (rtx insn)
7308 {
7309   enum attr_cirrus attr;
7310
7311   /* get_attr cannot accept USE or CLOBBER.  */
7312   if (!insn
7313       || GET_CODE (insn) != INSN
7314       || GET_CODE (PATTERN (insn)) == USE
7315       || GET_CODE (PATTERN (insn)) == CLOBBER)
7316     return 0;
7317
7318   attr = get_attr_cirrus (insn);
7319
7320   return attr != CIRRUS_NOT;
7321 }
7322
7323 /* Cirrus reorg for invalid instruction combinations.  */
7324 static void
7325 cirrus_reorg (rtx first)
7326 {
7327   enum attr_cirrus attr;
7328   rtx body = PATTERN (first);
7329   rtx t;
7330   int nops;
7331
7332   /* Any branch must be followed by 2 non Cirrus instructions.  */
7333   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
7334     {
7335       nops = 0;
7336       t = next_nonnote_insn (first);
7337
7338       if (arm_cirrus_insn_p (t))
7339         ++ nops;
7340
7341       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
7342         ++ nops;
7343
7344       while (nops --)
7345         emit_insn_after (gen_nop (), first);
7346
7347       return;
7348     }
7349
7350   /* (float (blah)) is in parallel with a clobber.  */
7351   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
7352     body = XVECEXP (body, 0, 0);
7353
7354   if (GET_CODE (body) == SET)
7355     {
7356       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
7357
7358       /* cfldrd, cfldr64, cfstrd, cfstr64 must
7359          be followed by a non Cirrus insn.  */
7360       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
7361         {
7362           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
7363             emit_insn_after (gen_nop (), first);
7364
7365           return;
7366         }
7367       else if (arm_memory_load_p (first))
7368         {
7369           unsigned int arm_regno;
7370
7371           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
7372              ldr/cfmv64hr combination where the Rd field is the same
7373              in both instructions must be split with a non Cirrus
7374              insn.  Example:
7375
7376              ldr r0, blah
7377              nop
7378              cfmvsr mvf0, r0.  */
7379
7380           /* Get Arm register number for ldr insn.  */
7381           if (GET_CODE (lhs) == REG)
7382             arm_regno = REGNO (lhs);
7383           else
7384             {
7385               gcc_assert (GET_CODE (rhs) == REG);
7386               arm_regno = REGNO (rhs);
7387             }
7388
7389           /* Next insn.  */
7390           first = next_nonnote_insn (first);
7391
7392           if (! arm_cirrus_insn_p (first))
7393             return;
7394
7395           body = PATTERN (first);
7396
7397           /* (float (blah)) is in parallel with a clobber.  */
7398           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
7399             body = XVECEXP (body, 0, 0);
7400
7401           if (GET_CODE (body) == FLOAT)
7402             body = XEXP (body, 0);
7403
7404           if (get_attr_cirrus (first) == CIRRUS_MOVE
7405               && GET_CODE (XEXP (body, 1)) == REG
7406               && arm_regno == REGNO (XEXP (body, 1)))
7407             emit_insn_after (gen_nop (), first);
7408
7409           return;
7410         }
7411     }
7412
7413   /* get_attr cannot accept USE or CLOBBER.  */
7414   if (!first
7415       || GET_CODE (first) != INSN
7416       || GET_CODE (PATTERN (first)) == USE
7417       || GET_CODE (PATTERN (first)) == CLOBBER)
7418     return;
7419
7420   attr = get_attr_cirrus (first);
7421
7422   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
7423      must be followed by a non-coprocessor instruction.  */
7424   if (attr == CIRRUS_COMPARE)
7425     {
7426       nops = 0;
7427
7428       t = next_nonnote_insn (first);
7429
7430       if (arm_cirrus_insn_p (t))
7431         ++ nops;
7432
7433       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
7434         ++ nops;
7435
7436       while (nops --)
7437         emit_insn_after (gen_nop (), first);
7438
7439       return;
7440     }
7441 }
7442
7443 /* Return TRUE if X references a SYMBOL_REF.  */
7444 int
7445 symbol_mentioned_p (rtx x)
7446 {
7447   const char * fmt;
7448   int i;
7449
7450   if (GET_CODE (x) == SYMBOL_REF)
7451     return 1;
7452
7453   /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
7454      are constant offsets, not symbols.  */
7455   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
7456     return 0;
7457
7458   fmt = GET_RTX_FORMAT (GET_CODE (x));
7459
7460   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
7461     {
7462       if (fmt[i] == 'E')
7463         {
7464           int j;
7465
7466           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7467             if (symbol_mentioned_p (XVECEXP (x, i, j)))
7468               return 1;
7469         }
7470       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
7471         return 1;
7472     }
7473
7474   return 0;
7475 }
7476
7477 /* Return TRUE if X references a LABEL_REF.  */
7478 int
7479 label_mentioned_p (rtx x)
7480 {
7481   const char * fmt;
7482   int i;
7483
7484   if (GET_CODE (x) == LABEL_REF)
7485     return 1;
7486
7487   /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
7488      instruction, but they are constant offsets, not symbols.  */
7489   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
7490     return 0;
7491
7492   fmt = GET_RTX_FORMAT (GET_CODE (x));
7493   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
7494     {
7495       if (fmt[i] == 'E')
7496         {
7497           int j;
7498
7499           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7500             if (label_mentioned_p (XVECEXP (x, i, j)))
7501               return 1;
7502         }
7503       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
7504         return 1;
7505     }
7506
7507   return 0;
7508 }
7509
7510 int
7511 tls_mentioned_p (rtx x)
7512 {
7513   switch (GET_CODE (x))
7514     {
7515     case CONST:
7516       return tls_mentioned_p (XEXP (x, 0));
7517
7518     case UNSPEC:
7519       if (XINT (x, 1) == UNSPEC_TLS)
7520         return 1;
7521
7522     default:
7523       return 0;
7524     }
7525 }
7526
7527 /* Must not copy a SET whose source operand is PC-relative.  */
7528
7529 static bool
7530 arm_cannot_copy_insn_p (rtx insn)
7531 {
7532   rtx pat = PATTERN (insn);
7533
7534   if (GET_CODE (pat) == SET)
7535     {
7536       rtx rhs = SET_SRC (pat);
7537
7538       if (GET_CODE (rhs) == UNSPEC
7539           && XINT (rhs, 1) == UNSPEC_PIC_BASE)
7540         return TRUE;
7541
7542       if (GET_CODE (rhs) == MEM
7543           && GET_CODE (XEXP (rhs, 0)) == UNSPEC
7544           && XINT (XEXP (rhs, 0), 1) == UNSPEC_PIC_BASE)
7545         return TRUE;
7546     }
7547
7548   return FALSE;
7549 }
7550
7551 enum rtx_code
7552 minmax_code (rtx x)
7553 {
7554   enum rtx_code code = GET_CODE (x);
7555
7556   switch (code)
7557     {
7558     case SMAX:
7559       return GE;
7560     case SMIN:
7561       return LE;
7562     case UMIN:
7563       return LEU;
7564     case UMAX:
7565       return GEU;
7566     default:
7567       gcc_unreachable ();
7568     }
7569 }
7570
7571 /* Return 1 if memory locations are adjacent.  */
7572 int
7573 adjacent_mem_locations (rtx a, rtx b)
7574 {
7575   /* We don't guarantee to preserve the order of these memory refs.  */
7576   if (volatile_refs_p (a) || volatile_refs_p (b))
7577     return 0;
7578
7579   if ((GET_CODE (XEXP (a, 0)) == REG
7580        || (GET_CODE (XEXP (a, 0)) == PLUS
7581            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
7582       && (GET_CODE (XEXP (b, 0)) == REG
7583           || (GET_CODE (XEXP (b, 0)) == PLUS
7584               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
7585     {
7586       HOST_WIDE_INT val0 = 0, val1 = 0;
7587       rtx reg0, reg1;
7588       int val_diff;
7589
7590       if (GET_CODE (XEXP (a, 0)) == PLUS)
7591         {
7592           reg0 = XEXP (XEXP (a, 0), 0);
7593           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
7594         }
7595       else
7596         reg0 = XEXP (a, 0);
7597
7598       if (GET_CODE (XEXP (b, 0)) == PLUS)
7599         {
7600           reg1 = XEXP (XEXP (b, 0), 0);
7601           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
7602         }
7603       else
7604         reg1 = XEXP (b, 0);
7605
7606       /* Don't accept any offset that will require multiple
7607          instructions to handle, since this would cause the
7608          arith_adjacentmem pattern to output an overlong sequence.  */
7609       if (!const_ok_for_op (val0, PLUS) || !const_ok_for_op (val1, PLUS))
7610         return 0;
7611
7612       /* Don't allow an eliminable register: register elimination can make
7613          the offset too large.  */
7614       if (arm_eliminable_register (reg0))
7615         return 0;
7616
7617       val_diff = val1 - val0;
7618
7619       if (arm_ld_sched)
7620         {
7621           /* If the target has load delay slots, then there's no benefit
7622              to using an ldm instruction unless the offset is zero and
7623              we are optimizing for size.  */
7624           return (optimize_size && (REGNO (reg0) == REGNO (reg1))
7625                   && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
7626                   && (val_diff == 4 || val_diff == -4));
7627         }
7628
7629       return ((REGNO (reg0) == REGNO (reg1))
7630               && (val_diff == 4 || val_diff == -4));
7631     }
7632
7633   return 0;
7634 }
7635
7636 int
7637 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
7638                         HOST_WIDE_INT *load_offset)
7639 {
7640   int unsorted_regs[4];
7641   HOST_WIDE_INT unsorted_offsets[4];
7642   int order[4];
7643   int base_reg = -1;
7644   int i;
7645
7646   /* Can only handle 2, 3, or 4 insns at present,
7647      though could be easily extended if required.  */
7648   gcc_assert (nops >= 2 && nops <= 4);
7649
7650   memset (order, 0, 4 * sizeof (int));
7651
7652   /* Loop over the operands and check that the memory references are
7653      suitable (i.e. immediate offsets from the same base register).  At
7654      the same time, extract the target register, and the memory
7655      offsets.  */
7656   for (i = 0; i < nops; i++)
7657     {
7658       rtx reg;
7659       rtx offset;
7660
7661       /* Convert a subreg of a mem into the mem itself.  */
7662       if (GET_CODE (operands[nops + i]) == SUBREG)
7663         operands[nops + i] = alter_subreg (operands + (nops + i));
7664
7665       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
7666
7667       /* Don't reorder volatile memory references; it doesn't seem worth
7668          looking for the case where the order is ok anyway.  */
7669       if (MEM_VOLATILE_P (operands[nops + i]))
7670         return 0;
7671
7672       offset = const0_rtx;
7673
7674       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
7675            || (GET_CODE (reg) == SUBREG
7676                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
7677           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
7678               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
7679                    == REG)
7680                   || (GET_CODE (reg) == SUBREG
7681                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
7682               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
7683                   == CONST_INT)))
7684         {
7685           if (i == 0)
7686             {
7687               base_reg = REGNO (reg);
7688               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
7689                                   ? REGNO (operands[i])
7690                                   : REGNO (SUBREG_REG (operands[i])));
7691               order[0] = 0;
7692             }
7693           else
7694             {
7695               if (base_reg != (int) REGNO (reg))
7696                 /* Not addressed from the same base register.  */
7697                 return 0;
7698
7699               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
7700                                   ? REGNO (operands[i])
7701                                   : REGNO (SUBREG_REG (operands[i])));
7702               if (unsorted_regs[i] < unsorted_regs[order[0]])
7703                 order[0] = i;
7704             }
7705
7706           /* If it isn't an integer register, or if it overwrites the
7707              base register but isn't the last insn in the list, then
7708              we can't do this.  */
7709           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
7710               || (i != nops - 1 && unsorted_regs[i] == base_reg))
7711             return 0;
7712
7713           unsorted_offsets[i] = INTVAL (offset);
7714         }
7715       else
7716         /* Not a suitable memory address.  */
7717         return 0;
7718     }
7719
7720   /* All the useful information has now been extracted from the
7721      operands into unsorted_regs and unsorted_offsets; additionally,
7722      order[0] has been set to the lowest numbered register in the
7723      list.  Sort the registers into order, and check that the memory
7724      offsets are ascending and adjacent.  */
7725
7726   for (i = 1; i < nops; i++)
7727     {
7728       int j;
7729
7730       order[i] = order[i - 1];
7731       for (j = 0; j < nops; j++)
7732         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
7733             && (order[i] == order[i - 1]
7734                 || unsorted_regs[j] < unsorted_regs[order[i]]))
7735           order[i] = j;
7736
7737       /* Have we found a suitable register? if not, one must be used more
7738          than once.  */
7739       if (order[i] == order[i - 1])
7740         return 0;
7741
7742       /* Is the memory address adjacent and ascending? */
7743       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
7744         return 0;
7745     }
7746
7747   if (base)
7748     {
7749       *base = base_reg;
7750
7751       for (i = 0; i < nops; i++)
7752         regs[i] = unsorted_regs[order[i]];
7753
7754       *load_offset = unsorted_offsets[order[0]];
7755     }
7756
7757   if (unsorted_offsets[order[0]] == 0)
7758     return 1; /* ldmia */
7759
7760   if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
7761     return 2; /* ldmib */
7762
7763   if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
7764     return 3; /* ldmda */
7765
7766   if (unsorted_offsets[order[nops - 1]] == -4)
7767     return 4; /* ldmdb */
7768
7769   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
7770      if the offset isn't small enough.  The reason 2 ldrs are faster
7771      is because these ARMs are able to do more than one cache access
7772      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
7773      whilst the ARM8 has a double bandwidth cache.  This means that
7774      these cores can do both an instruction fetch and a data fetch in
7775      a single cycle, so the trick of calculating the address into a
7776      scratch register (one of the result regs) and then doing a load
7777      multiple actually becomes slower (and no smaller in code size).
7778      That is the transformation
7779
7780         ldr     rd1, [rbase + offset]
7781         ldr     rd2, [rbase + offset + 4]
7782
7783      to
7784
7785         add     rd1, rbase, offset
7786         ldmia   rd1, {rd1, rd2}
7787
7788      produces worse code -- '3 cycles + any stalls on rd2' instead of
7789      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
7790      access per cycle, the first sequence could never complete in less
7791      than 6 cycles, whereas the ldm sequence would only take 5 and
7792      would make better use of sequential accesses if not hitting the
7793      cache.
7794
7795      We cheat here and test 'arm_ld_sched' which we currently know to
7796      only be true for the ARM8, ARM9 and StrongARM.  If this ever
7797      changes, then the test below needs to be reworked.  */
7798   if (nops == 2 && arm_ld_sched)
7799     return 0;
7800
7801   /* Can't do it without setting up the offset, only do this if it takes
7802      no more than one insn.  */
7803   return (const_ok_for_arm (unsorted_offsets[order[0]])
7804           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
7805 }
7806
7807 const char *
7808 emit_ldm_seq (rtx *operands, int nops)
7809 {
7810   int regs[4];
7811   int base_reg;
7812   HOST_WIDE_INT offset;
7813   char buf[100];
7814   int i;
7815
7816   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
7817     {
7818     case 1:
7819       strcpy (buf, "ldm%(ia%)\t");
7820       break;
7821
7822     case 2:
7823       strcpy (buf, "ldm%(ib%)\t");
7824       break;
7825
7826     case 3:
7827       strcpy (buf, "ldm%(da%)\t");
7828       break;
7829
7830     case 4:
7831       strcpy (buf, "ldm%(db%)\t");
7832       break;
7833
7834     case 5:
7835       if (offset >= 0)
7836         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
7837                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
7838                  (long) offset);
7839       else
7840         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
7841                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
7842                  (long) -offset);
7843       output_asm_insn (buf, operands);
7844       base_reg = regs[0];
7845       strcpy (buf, "ldm%(ia%)\t");
7846       break;
7847
7848     default:
7849       gcc_unreachable ();
7850     }
7851
7852   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
7853            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
7854
7855   for (i = 1; i < nops; i++)
7856     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
7857              reg_names[regs[i]]);
7858
7859   strcat (buf, "}\t%@ phole ldm");
7860
7861   output_asm_insn (buf, operands);
7862   return "";
7863 }
7864
7865 int
7866 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
7867                          HOST_WIDE_INT * load_offset)
7868 {
7869   int unsorted_regs[4];
7870   HOST_WIDE_INT unsorted_offsets[4];
7871   int order[4];
7872   int base_reg = -1;
7873   int i;
7874
7875   /* Can only handle 2, 3, or 4 insns at present, though could be easily
7876      extended if required.  */
7877   gcc_assert (nops >= 2 && nops <= 4);
7878
7879   memset (order, 0, 4 * sizeof (int));
7880
7881   /* Loop over the operands and check that the memory references are
7882      suitable (i.e. immediate offsets from the same base register).  At
7883      the same time, extract the target register, and the memory
7884      offsets.  */
7885   for (i = 0; i < nops; i++)
7886     {
7887       rtx reg;
7888       rtx offset;
7889
7890       /* Convert a subreg of a mem into the mem itself.  */
7891       if (GET_CODE (operands[nops + i]) == SUBREG)
7892         operands[nops + i] = alter_subreg (operands + (nops + i));
7893
7894       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
7895
7896       /* Don't reorder volatile memory references; it doesn't seem worth
7897          looking for the case where the order is ok anyway.  */
7898       if (MEM_VOLATILE_P (operands[nops + i]))
7899         return 0;
7900
7901       offset = const0_rtx;
7902
7903       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
7904            || (GET_CODE (reg) == SUBREG
7905                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
7906           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
7907               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
7908                    == REG)
7909                   || (GET_CODE (reg) == SUBREG
7910                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
7911               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
7912                   == CONST_INT)))
7913         {
7914           if (i == 0)
7915             {
7916               base_reg = REGNO (reg);
7917               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
7918                                   ? REGNO (operands[i])
7919                                   : REGNO (SUBREG_REG (operands[i])));
7920               order[0] = 0;
7921             }
7922           else
7923             {
7924               if (base_reg != (int) REGNO (reg))
7925                 /* Not addressed from the same base register.  */
7926                 return 0;
7927
7928               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
7929                                   ? REGNO (operands[i])
7930                                   : REGNO (SUBREG_REG (operands[i])));
7931               if (unsorted_regs[i] < unsorted_regs[order[0]])
7932                 order[0] = i;
7933             }
7934
7935           /* If it isn't an integer register, then we can't do this.  */
7936           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
7937             return 0;
7938
7939           unsorted_offsets[i] = INTVAL (offset);
7940         }
7941       else
7942         /* Not a suitable memory address.  */
7943         return 0;
7944     }
7945
7946   /* All the useful information has now been extracted from the
7947      operands into unsorted_regs and unsorted_offsets; additionally,
7948      order[0] has been set to the lowest numbered register in the
7949      list.  Sort the registers into order, and check that the memory
7950      offsets are ascending and adjacent.  */
7951
7952   for (i = 1; i < nops; i++)
7953     {
7954       int j;
7955
7956       order[i] = order[i - 1];
7957       for (j = 0; j < nops; j++)
7958         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
7959             && (order[i] == order[i - 1]
7960                 || unsorted_regs[j] < unsorted_regs[order[i]]))
7961           order[i] = j;
7962
7963       /* Have we found a suitable register? if not, one must be used more
7964          than once.  */
7965       if (order[i] == order[i - 1])
7966         return 0;
7967
7968       /* Is the memory address adjacent and ascending? */
7969       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
7970         return 0;
7971     }
7972
7973   if (base)
7974     {
7975       *base = base_reg;
7976
7977       for (i = 0; i < nops; i++)
7978         regs[i] = unsorted_regs[order[i]];
7979
7980       *load_offset = unsorted_offsets[order[0]];
7981     }
7982
7983   if (unsorted_offsets[order[0]] == 0)
7984     return 1; /* stmia */
7985
7986   if (unsorted_offsets[order[0]] == 4)
7987     return 2; /* stmib */
7988
7989   if (unsorted_offsets[order[nops - 1]] == 0)
7990     return 3; /* stmda */
7991
7992   if (unsorted_offsets[order[nops - 1]] == -4)
7993     return 4; /* stmdb */
7994
7995   return 0;
7996 }
7997
7998 const char *
7999 emit_stm_seq (rtx *operands, int nops)
8000 {
8001   int regs[4];
8002   int base_reg;
8003   HOST_WIDE_INT offset;
8004   char buf[100];
8005   int i;
8006
8007   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
8008     {
8009     case 1:
8010       strcpy (buf, "stm%(ia%)\t");
8011       break;
8012
8013     case 2:
8014       strcpy (buf, "stm%(ib%)\t");
8015       break;
8016
8017     case 3:
8018       strcpy (buf, "stm%(da%)\t");
8019       break;
8020
8021     case 4:
8022       strcpy (buf, "stm%(db%)\t");
8023       break;
8024
8025     default:
8026       gcc_unreachable ();
8027     }
8028
8029   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
8030            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
8031
8032   for (i = 1; i < nops; i++)
8033     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
8034              reg_names[regs[i]]);
8035
8036   strcat (buf, "}\t%@ phole stm");
8037
8038   output_asm_insn (buf, operands);
8039   return "";
8040 }
8041 \f
8042 /* Routines for use in generating RTL.  */
8043
8044 rtx
8045 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
8046                        int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
8047 {
8048   HOST_WIDE_INT offset = *offsetp;
8049   int i = 0, j;
8050   rtx result;
8051   int sign = up ? 1 : -1;
8052   rtx mem, addr;
8053
8054   /* XScale has load-store double instructions, but they have stricter
8055      alignment requirements than load-store multiple, so we cannot
8056      use them.
8057
8058      For XScale ldm requires 2 + NREGS cycles to complete and blocks
8059      the pipeline until completion.
8060
8061         NREGS           CYCLES
8062           1               3
8063           2               4
8064           3               5
8065           4               6
8066
8067      An ldr instruction takes 1-3 cycles, but does not block the
8068      pipeline.
8069
8070         NREGS           CYCLES
8071           1              1-3
8072           2              2-6
8073           3              3-9
8074           4              4-12
8075
8076      Best case ldr will always win.  However, the more ldr instructions
8077      we issue, the less likely we are to be able to schedule them well.
8078      Using ldr instructions also increases code size.
8079
8080      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
8081      for counts of 3 or 4 regs.  */
8082   if (arm_tune_xscale && count <= 2 && ! optimize_size)
8083     {
8084       rtx seq;
8085
8086       start_sequence ();
8087
8088       for (i = 0; i < count; i++)
8089         {
8090           addr = plus_constant (from, i * 4 * sign);
8091           mem = adjust_automodify_address (basemem, SImode, addr, offset);
8092           emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
8093           offset += 4 * sign;
8094         }
8095
8096       if (write_back)
8097         {
8098           emit_move_insn (from, plus_constant (from, count * 4 * sign));
8099           *offsetp = offset;
8100         }
8101
8102       seq = get_insns ();
8103       end_sequence ();
8104
8105       return seq;
8106     }
8107
8108   result = gen_rtx_PARALLEL (VOIDmode,
8109                              rtvec_alloc (count + (write_back ? 1 : 0)));
8110   if (write_back)
8111     {
8112       XVECEXP (result, 0, 0)
8113         = gen_rtx_SET (VOIDmode, from, plus_constant (from, count * 4 * sign));
8114       i = 1;
8115       count++;
8116     }
8117
8118   for (j = 0; i < count; i++, j++)
8119     {
8120       addr = plus_constant (from, j * 4 * sign);
8121       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
8122       XVECEXP (result, 0, i)
8123         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
8124       offset += 4 * sign;
8125     }
8126
8127   if (write_back)
8128     *offsetp = offset;
8129
8130   return result;
8131 }
8132
8133 rtx
8134 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
8135                         int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
8136 {
8137   HOST_WIDE_INT offset = *offsetp;
8138   int i = 0, j;
8139   rtx result;
8140   int sign = up ? 1 : -1;
8141   rtx mem, addr;
8142
8143   /* See arm_gen_load_multiple for discussion of
8144      the pros/cons of ldm/stm usage for XScale.  */
8145   if (arm_tune_xscale && count <= 2 && ! optimize_size)
8146     {
8147       rtx seq;
8148
8149       start_sequence ();
8150
8151       for (i = 0; i < count; i++)
8152         {
8153           addr = plus_constant (to, i * 4 * sign);
8154           mem = adjust_automodify_address (basemem, SImode, addr, offset);
8155           emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
8156           offset += 4 * sign;
8157         }
8158
8159       if (write_back)
8160         {
8161           emit_move_insn (to, plus_constant (to, count * 4 * sign));
8162           *offsetp = offset;
8163         }
8164
8165       seq = get_insns ();
8166       end_sequence ();
8167
8168       return seq;
8169     }
8170
8171   result = gen_rtx_PARALLEL (VOIDmode,
8172                              rtvec_alloc (count + (write_back ? 1 : 0)));
8173   if (write_back)
8174     {
8175       XVECEXP (result, 0, 0)
8176         = gen_rtx_SET (VOIDmode, to,
8177                        plus_constant (to, count * 4 * sign));
8178       i = 1;
8179       count++;
8180     }
8181
8182   for (j = 0; i < count; i++, j++)
8183     {
8184       addr = plus_constant (to, j * 4 * sign);
8185       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
8186       XVECEXP (result, 0, i)
8187         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
8188       offset += 4 * sign;
8189     }
8190
8191   if (write_back)
8192     *offsetp = offset;
8193
8194   return result;
8195 }
8196
8197 int
8198 arm_gen_movmemqi (rtx *operands)
8199 {
8200   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
8201   HOST_WIDE_INT srcoffset, dstoffset;
8202   int i;
8203   rtx src, dst, srcbase, dstbase;
8204   rtx part_bytes_reg = NULL;
8205   rtx mem;
8206
8207   if (GET_CODE (operands[2]) != CONST_INT
8208       || GET_CODE (operands[3]) != CONST_INT
8209       || INTVAL (operands[2]) > 64
8210       || INTVAL (operands[3]) & 3)
8211     return 0;
8212
8213   dstbase = operands[0];
8214   srcbase = operands[1];
8215
8216   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
8217   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
8218
8219   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
8220   out_words_to_go = INTVAL (operands[2]) / 4;
8221   last_bytes = INTVAL (operands[2]) & 3;
8222   dstoffset = srcoffset = 0;
8223
8224   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
8225     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
8226
8227   for (i = 0; in_words_to_go >= 2; i+=4)
8228     {
8229       if (in_words_to_go > 4)
8230         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
8231                                           srcbase, &srcoffset));
8232       else
8233         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
8234                                           FALSE, srcbase, &srcoffset));
8235
8236       if (out_words_to_go)
8237         {
8238           if (out_words_to_go > 4)
8239             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
8240                                                dstbase, &dstoffset));
8241           else if (out_words_to_go != 1)
8242             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
8243                                                dst, TRUE,
8244                                                (last_bytes == 0
8245                                                 ? FALSE : TRUE),
8246                                                dstbase, &dstoffset));
8247           else
8248             {
8249               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
8250               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
8251               if (last_bytes != 0)
8252                 {
8253                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
8254                   dstoffset += 4;
8255                 }
8256             }
8257         }
8258
8259       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
8260       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
8261     }
8262
8263   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
8264   if (out_words_to_go)
8265     {
8266       rtx sreg;
8267
8268       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
8269       sreg = copy_to_reg (mem);
8270
8271       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
8272       emit_move_insn (mem, sreg);
8273       in_words_to_go--;
8274
8275       gcc_assert (!in_words_to_go);     /* Sanity check */
8276     }
8277
8278   if (in_words_to_go)
8279     {
8280       gcc_assert (in_words_to_go > 0);
8281
8282       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
8283       part_bytes_reg = copy_to_mode_reg (SImode, mem);
8284     }
8285
8286   gcc_assert (!last_bytes || part_bytes_reg);
8287
8288   if (BYTES_BIG_ENDIAN && last_bytes)
8289     {
8290       rtx tmp = gen_reg_rtx (SImode);
8291
8292       /* The bytes we want are in the top end of the word.  */
8293       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
8294                               GEN_INT (8 * (4 - last_bytes))));
8295       part_bytes_reg = tmp;
8296
8297       while (last_bytes)
8298         {
8299           mem = adjust_automodify_address (dstbase, QImode,
8300                                            plus_constant (dst, last_bytes - 1),
8301                                            dstoffset + last_bytes - 1);
8302           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
8303
8304           if (--last_bytes)
8305             {
8306               tmp = gen_reg_rtx (SImode);
8307               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
8308               part_bytes_reg = tmp;
8309             }
8310         }
8311
8312     }
8313   else
8314     {
8315       if (last_bytes > 1)
8316         {
8317           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
8318           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
8319           last_bytes -= 2;
8320           if (last_bytes)
8321             {
8322               rtx tmp = gen_reg_rtx (SImode);
8323               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
8324               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
8325               part_bytes_reg = tmp;
8326               dstoffset += 2;
8327             }
8328         }
8329
8330       if (last_bytes)
8331         {
8332           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
8333           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
8334         }
8335     }
8336
8337   return 1;
8338 }
8339
8340 /* Select a dominance comparison mode if possible for a test of the general
8341    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
8342    COND_OR == DOM_CC_X_AND_Y => (X && Y)
8343    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
8344    COND_OR == DOM_CC_X_OR_Y => (X || Y)
8345    In all cases OP will be either EQ or NE, but we don't need to know which
8346    here.  If we are unable to support a dominance comparison we return
8347    CC mode.  This will then fail to match for the RTL expressions that
8348    generate this call.  */
8349 enum machine_mode
8350 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
8351 {
8352   enum rtx_code cond1, cond2;
8353   int swapped = 0;
8354
8355   /* Currently we will probably get the wrong result if the individual
8356      comparisons are not simple.  This also ensures that it is safe to
8357      reverse a comparison if necessary.  */
8358   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
8359        != CCmode)
8360       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
8361           != CCmode))
8362     return CCmode;
8363
8364   /* The if_then_else variant of this tests the second condition if the
8365      first passes, but is true if the first fails.  Reverse the first
8366      condition to get a true "inclusive-or" expression.  */
8367   if (cond_or == DOM_CC_NX_OR_Y)
8368     cond1 = reverse_condition (cond1);
8369
8370   /* If the comparisons are not equal, and one doesn't dominate the other,
8371      then we can't do this.  */
8372   if (cond1 != cond2
8373       && !comparison_dominates_p (cond1, cond2)
8374       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
8375     return CCmode;
8376
8377   if (swapped)
8378     {
8379       enum rtx_code temp = cond1;
8380       cond1 = cond2;
8381       cond2 = temp;
8382     }
8383
8384   switch (cond1)
8385     {
8386     case EQ:
8387       if (cond_or == DOM_CC_X_AND_Y)
8388         return CC_DEQmode;
8389
8390       switch (cond2)
8391         {
8392         case EQ: return CC_DEQmode;
8393         case LE: return CC_DLEmode;
8394         case LEU: return CC_DLEUmode;
8395         case GE: return CC_DGEmode;
8396         case GEU: return CC_DGEUmode;
8397         default: gcc_unreachable ();
8398         }
8399
8400     case LT:
8401       if (cond_or == DOM_CC_X_AND_Y)
8402         return CC_DLTmode;
8403
8404       switch (cond2)
8405         {
8406         case  LT:
8407             return CC_DLTmode;
8408         case LE:
8409           return CC_DLEmode;
8410         case NE:
8411           return CC_DNEmode;
8412         default:
8413           gcc_unreachable ();
8414         }
8415
8416     case GT:
8417       if (cond_or == DOM_CC_X_AND_Y)
8418         return CC_DGTmode;
8419
8420       switch (cond2)
8421         {
8422         case GT:
8423           return CC_DGTmode;
8424         case GE:
8425           return CC_DGEmode;
8426         case NE:
8427           return CC_DNEmode;
8428         default:
8429           gcc_unreachable ();
8430         }
8431
8432     case LTU:
8433       if (cond_or == DOM_CC_X_AND_Y)
8434         return CC_DLTUmode;
8435
8436       switch (cond2)
8437         {
8438         case LTU:
8439           return CC_DLTUmode;
8440         case LEU:
8441           return CC_DLEUmode;
8442         case NE:
8443           return CC_DNEmode;
8444         default:
8445           gcc_unreachable ();
8446         }
8447
8448     case GTU:
8449       if (cond_or == DOM_CC_X_AND_Y)
8450         return CC_DGTUmode;
8451
8452       switch (cond2)
8453         {
8454         case GTU:
8455           return CC_DGTUmode;
8456         case GEU:
8457           return CC_DGEUmode;
8458         case NE:
8459           return CC_DNEmode;
8460         default:
8461           gcc_unreachable ();
8462         }
8463
8464     /* The remaining cases only occur when both comparisons are the
8465        same.  */
8466     case NE:
8467       gcc_assert (cond1 == cond2);
8468       return CC_DNEmode;
8469
8470     case LE:
8471       gcc_assert (cond1 == cond2);
8472       return CC_DLEmode;
8473
8474     case GE:
8475       gcc_assert (cond1 == cond2);
8476       return CC_DGEmode;
8477
8478     case LEU:
8479       gcc_assert (cond1 == cond2);
8480       return CC_DLEUmode;
8481
8482     case GEU:
8483       gcc_assert (cond1 == cond2);
8484       return CC_DGEUmode;
8485
8486     default:
8487       gcc_unreachable ();
8488     }
8489 }
8490
8491 enum machine_mode
8492 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
8493 {
8494   /* All floating point compares return CCFP if it is an equality
8495      comparison, and CCFPE otherwise.  */
8496   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
8497     {
8498       switch (op)
8499         {
8500         case EQ:
8501         case NE:
8502         case UNORDERED:
8503         case ORDERED:
8504         case UNLT:
8505         case UNLE:
8506         case UNGT:
8507         case UNGE:
8508         case UNEQ:
8509         case LTGT:
8510           return CCFPmode;
8511
8512         case LT:
8513         case LE:
8514         case GT:
8515         case GE:
8516           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
8517             return CCFPmode;
8518           return CCFPEmode;
8519
8520         default:
8521           gcc_unreachable ();
8522         }
8523     }
8524
8525   /* A compare with a shifted operand.  Because of canonicalization, the
8526      comparison will have to be swapped when we emit the assembler.  */
8527   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
8528       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
8529           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
8530           || GET_CODE (x) == ROTATERT))
8531     return CC_SWPmode;
8532
8533   /* This operation is performed swapped, but since we only rely on the Z
8534      flag we don't need an additional mode.  */
8535   if (GET_MODE (y) == SImode && REG_P (y)
8536       && GET_CODE (x) == NEG
8537       && (op == EQ || op == NE))
8538     return CC_Zmode;
8539
8540   /* This is a special case that is used by combine to allow a
8541      comparison of a shifted byte load to be split into a zero-extend
8542      followed by a comparison of the shifted integer (only valid for
8543      equalities and unsigned inequalities).  */
8544   if (GET_MODE (x) == SImode
8545       && GET_CODE (x) == ASHIFT
8546       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
8547       && GET_CODE (XEXP (x, 0)) == SUBREG
8548       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
8549       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
8550       && (op == EQ || op == NE
8551           || op == GEU || op == GTU || op == LTU || op == LEU)
8552       && GET_CODE (y) == CONST_INT)
8553     return CC_Zmode;
8554
8555   /* A construct for a conditional compare, if the false arm contains
8556      0, then both conditions must be true, otherwise either condition
8557      must be true.  Not all conditions are possible, so CCmode is
8558      returned if it can't be done.  */
8559   if (GET_CODE (x) == IF_THEN_ELSE
8560       && (XEXP (x, 2) == const0_rtx
8561           || XEXP (x, 2) == const1_rtx)
8562       && COMPARISON_P (XEXP (x, 0))
8563       && COMPARISON_P (XEXP (x, 1)))
8564     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
8565                                          INTVAL (XEXP (x, 2)));
8566
8567   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
8568   if (GET_CODE (x) == AND
8569       && COMPARISON_P (XEXP (x, 0))
8570       && COMPARISON_P (XEXP (x, 1)))
8571     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
8572                                          DOM_CC_X_AND_Y);
8573
8574   if (GET_CODE (x) == IOR
8575       && COMPARISON_P (XEXP (x, 0))
8576       && COMPARISON_P (XEXP (x, 1)))
8577     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
8578                                          DOM_CC_X_OR_Y);
8579
8580   /* An operation (on Thumb) where we want to test for a single bit.
8581      This is done by shifting that bit up into the top bit of a
8582      scratch register; we can then branch on the sign bit.  */
8583   if (TARGET_THUMB1
8584       && GET_MODE (x) == SImode
8585       && (op == EQ || op == NE)
8586       && GET_CODE (x) == ZERO_EXTRACT
8587       && XEXP (x, 1) == const1_rtx)
8588     return CC_Nmode;
8589
8590   /* An operation that sets the condition codes as a side-effect, the
8591      V flag is not set correctly, so we can only use comparisons where
8592      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
8593      instead.)  */
8594   /* ??? Does the ZERO_EXTRACT case really apply to thumb2?  */
8595   if (GET_MODE (x) == SImode
8596       && y == const0_rtx
8597       && (op == EQ || op == NE || op == LT || op == GE)
8598       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
8599           || GET_CODE (x) == AND || GET_CODE (x) == IOR
8600           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
8601           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
8602           || GET_CODE (x) == LSHIFTRT
8603           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
8604           || GET_CODE (x) == ROTATERT
8605           || (TARGET_32BIT && GET_CODE (x) == ZERO_EXTRACT)))
8606     return CC_NOOVmode;
8607
8608   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
8609     return CC_Zmode;
8610
8611   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
8612       && GET_CODE (x) == PLUS
8613       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
8614     return CC_Cmode;
8615
8616   return CCmode;
8617 }
8618
8619 /* X and Y are two things to compare using CODE.  Emit the compare insn and
8620    return the rtx for register 0 in the proper mode.  FP means this is a
8621    floating point compare: I don't think that it is needed on the arm.  */
8622 rtx
8623 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
8624 {
8625   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
8626   rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
8627
8628   emit_set_insn (cc_reg, gen_rtx_COMPARE (mode, x, y));
8629
8630   return cc_reg;
8631 }
8632
8633 /* Generate a sequence of insns that will generate the correct return
8634    address mask depending on the physical architecture that the program
8635    is running on.  */
8636 rtx
8637 arm_gen_return_addr_mask (void)
8638 {
8639   rtx reg = gen_reg_rtx (Pmode);
8640
8641   emit_insn (gen_return_addr_mask (reg));
8642   return reg;
8643 }
8644
8645 void
8646 arm_reload_in_hi (rtx *operands)
8647 {
8648   rtx ref = operands[1];
8649   rtx base, scratch;
8650   HOST_WIDE_INT offset = 0;
8651
8652   if (GET_CODE (ref) == SUBREG)
8653     {
8654       offset = SUBREG_BYTE (ref);
8655       ref = SUBREG_REG (ref);
8656     }
8657
8658   if (GET_CODE (ref) == REG)
8659     {
8660       /* We have a pseudo which has been spilt onto the stack; there
8661          are two cases here: the first where there is a simple
8662          stack-slot replacement and a second where the stack-slot is
8663          out of range, or is used as a subreg.  */
8664       if (reg_equiv_mem[REGNO (ref)])
8665         {
8666           ref = reg_equiv_mem[REGNO (ref)];
8667           base = find_replacement (&XEXP (ref, 0));
8668         }
8669       else
8670         /* The slot is out of range, or was dressed up in a SUBREG.  */
8671         base = reg_equiv_address[REGNO (ref)];
8672     }
8673   else
8674     base = find_replacement (&XEXP (ref, 0));
8675
8676   /* Handle the case where the address is too complex to be offset by 1.  */
8677   if (GET_CODE (base) == MINUS
8678       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
8679     {
8680       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8681
8682       emit_set_insn (base_plus, base);
8683       base = base_plus;
8684     }
8685   else if (GET_CODE (base) == PLUS)
8686     {
8687       /* The addend must be CONST_INT, or we would have dealt with it above.  */
8688       HOST_WIDE_INT hi, lo;
8689
8690       offset += INTVAL (XEXP (base, 1));
8691       base = XEXP (base, 0);
8692
8693       /* Rework the address into a legal sequence of insns.  */
8694       /* Valid range for lo is -4095 -> 4095 */
8695       lo = (offset >= 0
8696             ? (offset & 0xfff)
8697             : -((-offset) & 0xfff));
8698
8699       /* Corner case, if lo is the max offset then we would be out of range
8700          once we have added the additional 1 below, so bump the msb into the
8701          pre-loading insn(s).  */
8702       if (lo == 4095)
8703         lo &= 0x7ff;
8704
8705       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
8706              ^ (HOST_WIDE_INT) 0x80000000)
8707             - (HOST_WIDE_INT) 0x80000000);
8708
8709       gcc_assert (hi + lo == offset);
8710
8711       if (hi != 0)
8712         {
8713           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8714
8715           /* Get the base address; addsi3 knows how to handle constants
8716              that require more than one insn.  */
8717           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
8718           base = base_plus;
8719           offset = lo;
8720         }
8721     }
8722
8723   /* Operands[2] may overlap operands[0] (though it won't overlap
8724      operands[1]), that's why we asked for a DImode reg -- so we can
8725      use the bit that does not overlap.  */
8726   if (REGNO (operands[2]) == REGNO (operands[0]))
8727     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8728   else
8729     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
8730
8731   emit_insn (gen_zero_extendqisi2 (scratch,
8732                                    gen_rtx_MEM (QImode,
8733                                                 plus_constant (base,
8734                                                                offset))));
8735   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
8736                                    gen_rtx_MEM (QImode,
8737                                                 plus_constant (base,
8738                                                                offset + 1))));
8739   if (!BYTES_BIG_ENDIAN)
8740     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
8741                    gen_rtx_IOR (SImode,
8742                                 gen_rtx_ASHIFT
8743                                 (SImode,
8744                                  gen_rtx_SUBREG (SImode, operands[0], 0),
8745                                  GEN_INT (8)),
8746                                 scratch));
8747   else
8748     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
8749                    gen_rtx_IOR (SImode,
8750                                 gen_rtx_ASHIFT (SImode, scratch,
8751                                                 GEN_INT (8)),
8752                                 gen_rtx_SUBREG (SImode, operands[0], 0)));
8753 }
8754
8755 /* Handle storing a half-word to memory during reload by synthesizing as two
8756    byte stores.  Take care not to clobber the input values until after we
8757    have moved them somewhere safe.  This code assumes that if the DImode
8758    scratch in operands[2] overlaps either the input value or output address
8759    in some way, then that value must die in this insn (we absolutely need
8760    two scratch registers for some corner cases).  */
8761 void
8762 arm_reload_out_hi (rtx *operands)
8763 {
8764   rtx ref = operands[0];
8765   rtx outval = operands[1];
8766   rtx base, scratch;
8767   HOST_WIDE_INT offset = 0;
8768
8769   if (GET_CODE (ref) == SUBREG)
8770     {
8771       offset = SUBREG_BYTE (ref);
8772       ref = SUBREG_REG (ref);
8773     }
8774
8775   if (GET_CODE (ref) == REG)
8776     {
8777       /* We have a pseudo which has been spilt onto the stack; there
8778          are two cases here: the first where there is a simple
8779          stack-slot replacement and a second where the stack-slot is
8780          out of range, or is used as a subreg.  */
8781       if (reg_equiv_mem[REGNO (ref)])
8782         {
8783           ref = reg_equiv_mem[REGNO (ref)];
8784           base = find_replacement (&XEXP (ref, 0));
8785         }
8786       else
8787         /* The slot is out of range, or was dressed up in a SUBREG.  */
8788         base = reg_equiv_address[REGNO (ref)];
8789     }
8790   else
8791     base = find_replacement (&XEXP (ref, 0));
8792
8793   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
8794
8795   /* Handle the case where the address is too complex to be offset by 1.  */
8796   if (GET_CODE (base) == MINUS
8797       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
8798     {
8799       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8800
8801       /* Be careful not to destroy OUTVAL.  */
8802       if (reg_overlap_mentioned_p (base_plus, outval))
8803         {
8804           /* Updating base_plus might destroy outval, see if we can
8805              swap the scratch and base_plus.  */
8806           if (!reg_overlap_mentioned_p (scratch, outval))
8807             {
8808               rtx tmp = scratch;
8809               scratch = base_plus;
8810               base_plus = tmp;
8811             }
8812           else
8813             {
8814               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
8815
8816               /* Be conservative and copy OUTVAL into the scratch now,
8817                  this should only be necessary if outval is a subreg
8818                  of something larger than a word.  */
8819               /* XXX Might this clobber base?  I can't see how it can,
8820                  since scratch is known to overlap with OUTVAL, and
8821                  must be wider than a word.  */
8822               emit_insn (gen_movhi (scratch_hi, outval));
8823               outval = scratch_hi;
8824             }
8825         }
8826
8827       emit_set_insn (base_plus, base);
8828       base = base_plus;
8829     }
8830   else if (GET_CODE (base) == PLUS)
8831     {
8832       /* The addend must be CONST_INT, or we would have dealt with it above.  */
8833       HOST_WIDE_INT hi, lo;
8834
8835       offset += INTVAL (XEXP (base, 1));
8836       base = XEXP (base, 0);
8837
8838       /* Rework the address into a legal sequence of insns.  */
8839       /* Valid range for lo is -4095 -> 4095 */
8840       lo = (offset >= 0
8841             ? (offset & 0xfff)
8842             : -((-offset) & 0xfff));
8843
8844       /* Corner case, if lo is the max offset then we would be out of range
8845          once we have added the additional 1 below, so bump the msb into the
8846          pre-loading insn(s).  */
8847       if (lo == 4095)
8848         lo &= 0x7ff;
8849
8850       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
8851              ^ (HOST_WIDE_INT) 0x80000000)
8852             - (HOST_WIDE_INT) 0x80000000);
8853
8854       gcc_assert (hi + lo == offset);
8855
8856       if (hi != 0)
8857         {
8858           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8859
8860           /* Be careful not to destroy OUTVAL.  */
8861           if (reg_overlap_mentioned_p (base_plus, outval))
8862             {
8863               /* Updating base_plus might destroy outval, see if we
8864                  can swap the scratch and base_plus.  */
8865               if (!reg_overlap_mentioned_p (scratch, outval))
8866                 {
8867                   rtx tmp = scratch;
8868                   scratch = base_plus;
8869                   base_plus = tmp;
8870                 }
8871               else
8872                 {
8873                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
8874
8875                   /* Be conservative and copy outval into scratch now,
8876                      this should only be necessary if outval is a
8877                      subreg of something larger than a word.  */
8878                   /* XXX Might this clobber base?  I can't see how it
8879                      can, since scratch is known to overlap with
8880                      outval.  */
8881                   emit_insn (gen_movhi (scratch_hi, outval));
8882                   outval = scratch_hi;
8883                 }
8884             }
8885
8886           /* Get the base address; addsi3 knows how to handle constants
8887              that require more than one insn.  */
8888           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
8889           base = base_plus;
8890           offset = lo;
8891         }
8892     }
8893
8894   if (BYTES_BIG_ENDIAN)
8895     {
8896       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
8897                                          plus_constant (base, offset + 1)),
8898                             gen_lowpart (QImode, outval)));
8899       emit_insn (gen_lshrsi3 (scratch,
8900                               gen_rtx_SUBREG (SImode, outval, 0),
8901                               GEN_INT (8)));
8902       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
8903                             gen_lowpart (QImode, scratch)));
8904     }
8905   else
8906     {
8907       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
8908                             gen_lowpart (QImode, outval)));
8909       emit_insn (gen_lshrsi3 (scratch,
8910                               gen_rtx_SUBREG (SImode, outval, 0),
8911                               GEN_INT (8)));
8912       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
8913                                          plus_constant (base, offset + 1)),
8914                             gen_lowpart (QImode, scratch)));
8915     }
8916 }
8917
8918 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
8919    (padded to the size of a word) should be passed in a register.  */
8920
8921 static bool
8922 arm_must_pass_in_stack (enum machine_mode mode, const_tree type)
8923 {
8924   if (TARGET_AAPCS_BASED)
8925     return must_pass_in_stack_var_size (mode, type);
8926   else
8927     return must_pass_in_stack_var_size_or_pad (mode, type);
8928 }
8929
8930
8931 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
8932    Return true if an argument passed on the stack should be padded upwards,
8933    i.e. if the least-significant byte has useful data.
8934    For legacy APCS ABIs we use the default.  For AAPCS based ABIs small
8935    aggregate types are placed in the lowest memory address.  */
8936
8937 bool
8938 arm_pad_arg_upward (enum machine_mode mode, const_tree type)
8939 {
8940   if (!TARGET_AAPCS_BASED)
8941     return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
8942
8943   if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
8944     return false;
8945
8946   return true;
8947 }
8948
8949
8950 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
8951    For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
8952    byte of the register has useful data, and return the opposite if the
8953    most significant byte does.
8954    For AAPCS, small aggregates and small complex types are always padded
8955    upwards.  */
8956
8957 bool
8958 arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
8959                     tree type, int first ATTRIBUTE_UNUSED)
8960 {
8961   if (TARGET_AAPCS_BASED
8962       && BYTES_BIG_ENDIAN
8963       && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
8964       && int_size_in_bytes (type) <= 4)
8965     return true;
8966
8967   /* Otherwise, use default padding.  */
8968   return !BYTES_BIG_ENDIAN;
8969 }
8970
8971 \f
8972 /* Print a symbolic form of X to the debug file, F.  */
8973 static void
8974 arm_print_value (FILE *f, rtx x)
8975 {
8976   switch (GET_CODE (x))
8977     {
8978     case CONST_INT:
8979       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
8980       return;
8981
8982     case CONST_DOUBLE:
8983       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
8984       return;
8985
8986     case CONST_VECTOR:
8987       {
8988         int i;
8989
8990         fprintf (f, "<");
8991         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
8992           {
8993             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
8994             if (i < (CONST_VECTOR_NUNITS (x) - 1))
8995               fputc (',', f);
8996           }
8997         fprintf (f, ">");
8998       }
8999       return;
9000
9001     case CONST_STRING:
9002       fprintf (f, "\"%s\"", XSTR (x, 0));
9003       return;
9004
9005     case SYMBOL_REF:
9006       fprintf (f, "`%s'", XSTR (x, 0));
9007       return;
9008
9009     case LABEL_REF:
9010       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
9011       return;
9012
9013     case CONST:
9014       arm_print_value (f, XEXP (x, 0));
9015       return;
9016
9017     case PLUS:
9018       arm_print_value (f, XEXP (x, 0));
9019       fprintf (f, "+");
9020       arm_print_value (f, XEXP (x, 1));
9021       return;
9022
9023     case PC:
9024       fprintf (f, "pc");
9025       return;
9026
9027     default:
9028       fprintf (f, "????");
9029       return;
9030     }
9031 }
9032 \f
9033 /* Routines for manipulation of the constant pool.  */
9034
9035 /* Arm instructions cannot load a large constant directly into a
9036    register; they have to come from a pc relative load.  The constant
9037    must therefore be placed in the addressable range of the pc
9038    relative load.  Depending on the precise pc relative load
9039    instruction the range is somewhere between 256 bytes and 4k.  This
9040    means that we often have to dump a constant inside a function, and
9041    generate code to branch around it.
9042
9043    It is important to minimize this, since the branches will slow
9044    things down and make the code larger.
9045
9046    Normally we can hide the table after an existing unconditional
9047    branch so that there is no interruption of the flow, but in the
9048    worst case the code looks like this:
9049
9050         ldr     rn, L1
9051         ...
9052         b       L2
9053         align
9054         L1:     .long value
9055         L2:
9056         ...
9057
9058         ldr     rn, L3
9059         ...
9060         b       L4
9061         align
9062         L3:     .long value
9063         L4:
9064         ...
9065
9066    We fix this by performing a scan after scheduling, which notices
9067    which instructions need to have their operands fetched from the
9068    constant table and builds the table.
9069
9070    The algorithm starts by building a table of all the constants that
9071    need fixing up and all the natural barriers in the function (places
9072    where a constant table can be dropped without breaking the flow).
9073    For each fixup we note how far the pc-relative replacement will be
9074    able to reach and the offset of the instruction into the function.
9075
9076    Having built the table we then group the fixes together to form
9077    tables that are as large as possible (subject to addressing
9078    constraints) and emit each table of constants after the last
9079    barrier that is within range of all the instructions in the group.
9080    If a group does not contain a barrier, then we forcibly create one
9081    by inserting a jump instruction into the flow.  Once the table has
9082    been inserted, the insns are then modified to reference the
9083    relevant entry in the pool.
9084
9085    Possible enhancements to the algorithm (not implemented) are:
9086
9087    1) For some processors and object formats, there may be benefit in
9088    aligning the pools to the start of cache lines; this alignment
9089    would need to be taken into account when calculating addressability
9090    of a pool.  */
9091
9092 /* These typedefs are located at the start of this file, so that
9093    they can be used in the prototypes there.  This comment is to
9094    remind readers of that fact so that the following structures
9095    can be understood more easily.
9096
9097      typedef struct minipool_node    Mnode;
9098      typedef struct minipool_fixup   Mfix;  */
9099
9100 struct minipool_node
9101 {
9102   /* Doubly linked chain of entries.  */
9103   Mnode * next;
9104   Mnode * prev;
9105   /* The maximum offset into the code that this entry can be placed.  While
9106      pushing fixes for forward references, all entries are sorted in order
9107      of increasing max_address.  */
9108   HOST_WIDE_INT max_address;
9109   /* Similarly for an entry inserted for a backwards ref.  */
9110   HOST_WIDE_INT min_address;
9111   /* The number of fixes referencing this entry.  This can become zero
9112      if we "unpush" an entry.  In this case we ignore the entry when we
9113      come to emit the code.  */
9114   int refcount;
9115   /* The offset from the start of the minipool.  */
9116   HOST_WIDE_INT offset;
9117   /* The value in table.  */
9118   rtx value;
9119   /* The mode of value.  */
9120   enum machine_mode mode;
9121   /* The size of the value.  With iWMMXt enabled
9122      sizes > 4 also imply an alignment of 8-bytes.  */
9123   int fix_size;
9124 };
9125
9126 struct minipool_fixup
9127 {
9128   Mfix *            next;
9129   rtx               insn;
9130   HOST_WIDE_INT     address;
9131   rtx *             loc;
9132   enum machine_mode mode;
9133   int               fix_size;
9134   rtx               value;
9135   Mnode *           minipool;
9136   HOST_WIDE_INT     forwards;
9137   HOST_WIDE_INT     backwards;
9138 };
9139
9140 /* Fixes less than a word need padding out to a word boundary.  */
9141 #define MINIPOOL_FIX_SIZE(mode) \
9142   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
9143
9144 static Mnode *  minipool_vector_head;
9145 static Mnode *  minipool_vector_tail;
9146 static rtx      minipool_vector_label;
9147 static int      minipool_pad;
9148
9149 /* The linked list of all minipool fixes required for this function.  */
9150 Mfix *          minipool_fix_head;
9151 Mfix *          minipool_fix_tail;
9152 /* The fix entry for the current minipool, once it has been placed.  */
9153 Mfix *          minipool_barrier;
9154
9155 /* Determines if INSN is the start of a jump table.  Returns the end
9156    of the TABLE or NULL_RTX.  */
9157 static rtx
9158 is_jump_table (rtx insn)
9159 {
9160   rtx table;
9161
9162   if (GET_CODE (insn) == JUMP_INSN
9163       && JUMP_LABEL (insn) != NULL
9164       && ((table = next_real_insn (JUMP_LABEL (insn)))
9165           == next_real_insn (insn))
9166       && table != NULL
9167       && GET_CODE (table) == JUMP_INSN
9168       && (GET_CODE (PATTERN (table)) == ADDR_VEC
9169           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
9170     return table;
9171
9172   return NULL_RTX;
9173 }
9174
9175 #ifndef JUMP_TABLES_IN_TEXT_SECTION
9176 #define JUMP_TABLES_IN_TEXT_SECTION 0
9177 #endif
9178
9179 static HOST_WIDE_INT
9180 get_jump_table_size (rtx insn)
9181 {
9182   /* ADDR_VECs only take room if read-only data does into the text
9183      section.  */
9184   if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
9185     {
9186       rtx body = PATTERN (insn);
9187       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
9188       HOST_WIDE_INT size;
9189       HOST_WIDE_INT modesize;
9190
9191       modesize = GET_MODE_SIZE (GET_MODE (body));
9192       size = modesize * XVECLEN (body, elt);
9193       switch (modesize)
9194         {
9195         case 1:
9196           /* Round up size  of TBB table to a halfword boundary.  */
9197           size = (size + 1) & ~(HOST_WIDE_INT)1;
9198           break;
9199         case 2:
9200           /* No padding necessary for TBH.  */
9201           break;
9202         case 4:
9203           /* Add two bytes for alignment on Thumb.  */
9204           if (TARGET_THUMB)
9205             size += 2;
9206           break;
9207         default:
9208           gcc_unreachable ();
9209         }
9210       return size;
9211     }
9212
9213   return 0;
9214 }
9215
9216 /* Move a minipool fix MP from its current location to before MAX_MP.
9217    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
9218    constraints may need updating.  */
9219 static Mnode *
9220 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
9221                                HOST_WIDE_INT max_address)
9222 {
9223   /* The code below assumes these are different.  */
9224   gcc_assert (mp != max_mp);
9225
9226   if (max_mp == NULL)
9227     {
9228       if (max_address < mp->max_address)
9229         mp->max_address = max_address;
9230     }
9231   else
9232     {
9233       if (max_address > max_mp->max_address - mp->fix_size)
9234         mp->max_address = max_mp->max_address - mp->fix_size;
9235       else
9236         mp->max_address = max_address;
9237
9238       /* Unlink MP from its current position.  Since max_mp is non-null,
9239        mp->prev must be non-null.  */
9240       mp->prev->next = mp->next;
9241       if (mp->next != NULL)
9242         mp->next->prev = mp->prev;
9243       else
9244         minipool_vector_tail = mp->prev;
9245
9246       /* Re-insert it before MAX_MP.  */
9247       mp->next = max_mp;
9248       mp->prev = max_mp->prev;
9249       max_mp->prev = mp;
9250
9251       if (mp->prev != NULL)
9252         mp->prev->next = mp;
9253       else
9254         minipool_vector_head = mp;
9255     }
9256
9257   /* Save the new entry.  */
9258   max_mp = mp;
9259
9260   /* Scan over the preceding entries and adjust their addresses as
9261      required.  */
9262   while (mp->prev != NULL
9263          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
9264     {
9265       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
9266       mp = mp->prev;
9267     }
9268
9269   return max_mp;
9270 }
9271
9272 /* Add a constant to the minipool for a forward reference.  Returns the
9273    node added or NULL if the constant will not fit in this pool.  */
9274 static Mnode *
9275 add_minipool_forward_ref (Mfix *fix)
9276 {
9277   /* If set, max_mp is the first pool_entry that has a lower
9278      constraint than the one we are trying to add.  */
9279   Mnode *       max_mp = NULL;
9280   HOST_WIDE_INT max_address = fix->address + fix->forwards - minipool_pad;
9281   Mnode *       mp;
9282
9283   /* If the minipool starts before the end of FIX->INSN then this FIX
9284      can not be placed into the current pool.  Furthermore, adding the
9285      new constant pool entry may cause the pool to start FIX_SIZE bytes
9286      earlier.  */
9287   if (minipool_vector_head &&
9288       (fix->address + get_attr_length (fix->insn)
9289        >= minipool_vector_head->max_address - fix->fix_size))
9290     return NULL;
9291
9292   /* Scan the pool to see if a constant with the same value has
9293      already been added.  While we are doing this, also note the
9294      location where we must insert the constant if it doesn't already
9295      exist.  */
9296   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
9297     {
9298       if (GET_CODE (fix->value) == GET_CODE (mp->value)
9299           && fix->mode == mp->mode
9300           && (GET_CODE (fix->value) != CODE_LABEL
9301               || (CODE_LABEL_NUMBER (fix->value)
9302                   == CODE_LABEL_NUMBER (mp->value)))
9303           && rtx_equal_p (fix->value, mp->value))
9304         {
9305           /* More than one fix references this entry.  */
9306           mp->refcount++;
9307           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
9308         }
9309
9310       /* Note the insertion point if necessary.  */
9311       if (max_mp == NULL
9312           && mp->max_address > max_address)
9313         max_mp = mp;
9314
9315       /* If we are inserting an 8-bytes aligned quantity and
9316          we have not already found an insertion point, then
9317          make sure that all such 8-byte aligned quantities are
9318          placed at the start of the pool.  */
9319       if (ARM_DOUBLEWORD_ALIGN
9320           && max_mp == NULL
9321           && fix->fix_size >= 8
9322           && mp->fix_size < 8)
9323         {
9324           max_mp = mp;
9325           max_address = mp->max_address;
9326         }
9327     }
9328
9329   /* The value is not currently in the minipool, so we need to create
9330      a new entry for it.  If MAX_MP is NULL, the entry will be put on
9331      the end of the list since the placement is less constrained than
9332      any existing entry.  Otherwise, we insert the new fix before
9333      MAX_MP and, if necessary, adjust the constraints on the other
9334      entries.  */
9335   mp = XNEW (Mnode);
9336   mp->fix_size = fix->fix_size;
9337   mp->mode = fix->mode;
9338   mp->value = fix->value;
9339   mp->refcount = 1;
9340   /* Not yet required for a backwards ref.  */
9341   mp->min_address = -65536;
9342
9343   if (max_mp == NULL)
9344     {
9345       mp->max_address = max_address;
9346       mp->next = NULL;
9347       mp->prev = minipool_vector_tail;
9348
9349       if (mp->prev == NULL)
9350         {
9351           minipool_vector_head = mp;
9352           minipool_vector_label = gen_label_rtx ();
9353         }
9354       else
9355         mp->prev->next = mp;
9356
9357       minipool_vector_tail = mp;
9358     }
9359   else
9360     {
9361       if (max_address > max_mp->max_address - mp->fix_size)
9362         mp->max_address = max_mp->max_address - mp->fix_size;
9363       else
9364         mp->max_address = max_address;
9365
9366       mp->next = max_mp;
9367       mp->prev = max_mp->prev;
9368       max_mp->prev = mp;
9369       if (mp->prev != NULL)
9370         mp->prev->next = mp;
9371       else
9372         minipool_vector_head = mp;
9373     }
9374
9375   /* Save the new entry.  */
9376   max_mp = mp;
9377
9378   /* Scan over the preceding entries and adjust their addresses as
9379      required.  */
9380   while (mp->prev != NULL
9381          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
9382     {
9383       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
9384       mp = mp->prev;
9385     }
9386
9387   return max_mp;
9388 }
9389
9390 static Mnode *
9391 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
9392                                 HOST_WIDE_INT  min_address)
9393 {
9394   HOST_WIDE_INT offset;
9395
9396   /* The code below assumes these are different.  */
9397   gcc_assert (mp != min_mp);
9398
9399   if (min_mp == NULL)
9400     {
9401       if (min_address > mp->min_address)
9402         mp->min_address = min_address;
9403     }
9404   else
9405     {
9406       /* We will adjust this below if it is too loose.  */
9407       mp->min_address = min_address;
9408
9409       /* Unlink MP from its current position.  Since min_mp is non-null,
9410          mp->next must be non-null.  */
9411       mp->next->prev = mp->prev;
9412       if (mp->prev != NULL)
9413         mp->prev->next = mp->next;
9414       else
9415         minipool_vector_head = mp->next;
9416
9417       /* Reinsert it after MIN_MP.  */
9418       mp->prev = min_mp;
9419       mp->next = min_mp->next;
9420       min_mp->next = mp;
9421       if (mp->next != NULL)
9422         mp->next->prev = mp;
9423       else
9424         minipool_vector_tail = mp;
9425     }
9426
9427   min_mp = mp;
9428
9429   offset = 0;
9430   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
9431     {
9432       mp->offset = offset;
9433       if (mp->refcount > 0)
9434         offset += mp->fix_size;
9435
9436       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
9437         mp->next->min_address = mp->min_address + mp->fix_size;
9438     }
9439
9440   return min_mp;
9441 }
9442
9443 /* Add a constant to the minipool for a backward reference.  Returns the
9444    node added or NULL if the constant will not fit in this pool.
9445
9446    Note that the code for insertion for a backwards reference can be
9447    somewhat confusing because the calculated offsets for each fix do
9448    not take into account the size of the pool (which is still under
9449    construction.  */
9450 static Mnode *
9451 add_minipool_backward_ref (Mfix *fix)
9452 {
9453   /* If set, min_mp is the last pool_entry that has a lower constraint
9454      than the one we are trying to add.  */
9455   Mnode *min_mp = NULL;
9456   /* This can be negative, since it is only a constraint.  */
9457   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
9458   Mnode *mp;
9459
9460   /* If we can't reach the current pool from this insn, or if we can't
9461      insert this entry at the end of the pool without pushing other
9462      fixes out of range, then we don't try.  This ensures that we
9463      can't fail later on.  */
9464   if (min_address >= minipool_barrier->address
9465       || (minipool_vector_tail->min_address + fix->fix_size
9466           >= minipool_barrier->address))
9467     return NULL;
9468
9469   /* Scan the pool to see if a constant with the same value has
9470      already been added.  While we are doing this, also note the
9471      location where we must insert the constant if it doesn't already
9472      exist.  */
9473   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
9474     {
9475       if (GET_CODE (fix->value) == GET_CODE (mp->value)
9476           && fix->mode == mp->mode
9477           && (GET_CODE (fix->value) != CODE_LABEL
9478               || (CODE_LABEL_NUMBER (fix->value)
9479                   == CODE_LABEL_NUMBER (mp->value)))
9480           && rtx_equal_p (fix->value, mp->value)
9481           /* Check that there is enough slack to move this entry to the
9482              end of the table (this is conservative).  */
9483           && (mp->max_address
9484               > (minipool_barrier->address
9485                  + minipool_vector_tail->offset
9486                  + minipool_vector_tail->fix_size)))
9487         {
9488           mp->refcount++;
9489           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
9490         }
9491
9492       if (min_mp != NULL)
9493         mp->min_address += fix->fix_size;
9494       else
9495         {
9496           /* Note the insertion point if necessary.  */
9497           if (mp->min_address < min_address)
9498             {
9499               /* For now, we do not allow the insertion of 8-byte alignment
9500                  requiring nodes anywhere but at the start of the pool.  */
9501               if (ARM_DOUBLEWORD_ALIGN
9502                   && fix->fix_size >= 8 && mp->fix_size < 8)
9503                 return NULL;
9504               else
9505                 min_mp = mp;
9506             }
9507           else if (mp->max_address
9508                    < minipool_barrier->address + mp->offset + fix->fix_size)
9509             {
9510               /* Inserting before this entry would push the fix beyond
9511                  its maximum address (which can happen if we have
9512                  re-located a forwards fix); force the new fix to come
9513                  after it.  */
9514               if (ARM_DOUBLEWORD_ALIGN
9515                   && fix->fix_size >= 8 && mp->fix_size < 8)
9516                 return NULL;
9517               else
9518                 {
9519                   min_mp = mp;
9520                   min_address = mp->min_address + fix->fix_size;
9521                 }
9522             }
9523           /* Do not insert a non-8-byte aligned quantity before 8-byte
9524              aligned quantities.  */
9525           else if (ARM_DOUBLEWORD_ALIGN
9526                    && fix->fix_size < 8
9527                    && mp->fix_size >= 8)
9528             {
9529               min_mp = mp;
9530               min_address = mp->min_address + fix->fix_size;
9531             }
9532         }
9533     }
9534
9535   /* We need to create a new entry.  */
9536   mp = XNEW (Mnode);
9537   mp->fix_size = fix->fix_size;
9538   mp->mode = fix->mode;
9539   mp->value = fix->value;
9540   mp->refcount = 1;
9541   mp->max_address = minipool_barrier->address + 65536;
9542
9543   mp->min_address = min_address;
9544
9545   if (min_mp == NULL)
9546     {
9547       mp->prev = NULL;
9548       mp->next = minipool_vector_head;
9549
9550       if (mp->next == NULL)
9551         {
9552           minipool_vector_tail = mp;
9553           minipool_vector_label = gen_label_rtx ();
9554         }
9555       else
9556         mp->next->prev = mp;
9557
9558       minipool_vector_head = mp;
9559     }
9560   else
9561     {
9562       mp->next = min_mp->next;
9563       mp->prev = min_mp;
9564       min_mp->next = mp;
9565
9566       if (mp->next != NULL)
9567         mp->next->prev = mp;
9568       else
9569         minipool_vector_tail = mp;
9570     }
9571
9572   /* Save the new entry.  */
9573   min_mp = mp;
9574
9575   if (mp->prev)
9576     mp = mp->prev;
9577   else
9578     mp->offset = 0;
9579
9580   /* Scan over the following entries and adjust their offsets.  */
9581   while (mp->next != NULL)
9582     {
9583       if (mp->next->min_address < mp->min_address + mp->fix_size)
9584         mp->next->min_address = mp->min_address + mp->fix_size;
9585
9586       if (mp->refcount)
9587         mp->next->offset = mp->offset + mp->fix_size;
9588       else
9589         mp->next->offset = mp->offset;
9590
9591       mp = mp->next;
9592     }
9593
9594   return min_mp;
9595 }
9596
9597 static void
9598 assign_minipool_offsets (Mfix *barrier)
9599 {
9600   HOST_WIDE_INT offset = 0;
9601   Mnode *mp;
9602
9603   minipool_barrier = barrier;
9604
9605   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
9606     {
9607       mp->offset = offset;
9608
9609       if (mp->refcount > 0)
9610         offset += mp->fix_size;
9611     }
9612 }
9613
9614 /* Output the literal table */
9615 static void
9616 dump_minipool (rtx scan)
9617 {
9618   Mnode * mp;
9619   Mnode * nmp;
9620   int align64 = 0;
9621
9622   if (ARM_DOUBLEWORD_ALIGN)
9623     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
9624       if (mp->refcount > 0 && mp->fix_size >= 8)
9625         {
9626           align64 = 1;
9627           break;
9628         }
9629
9630   if (dump_file)
9631     fprintf (dump_file,
9632              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
9633              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
9634
9635   scan = emit_label_after (gen_label_rtx (), scan);
9636   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
9637   scan = emit_label_after (minipool_vector_label, scan);
9638
9639   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
9640     {
9641       if (mp->refcount > 0)
9642         {
9643           if (dump_file)
9644             {
9645               fprintf (dump_file,
9646                        ";;  Offset %u, min %ld, max %ld ",
9647                        (unsigned) mp->offset, (unsigned long) mp->min_address,
9648                        (unsigned long) mp->max_address);
9649               arm_print_value (dump_file, mp->value);
9650               fputc ('\n', dump_file);
9651             }
9652
9653           switch (mp->fix_size)
9654             {
9655 #ifdef HAVE_consttable_1
9656             case 1:
9657               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
9658               break;
9659
9660 #endif
9661 #ifdef HAVE_consttable_2
9662             case 2:
9663               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
9664               break;
9665
9666 #endif
9667 #ifdef HAVE_consttable_4
9668             case 4:
9669               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
9670               break;
9671
9672 #endif
9673 #ifdef HAVE_consttable_8
9674             case 8:
9675               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
9676               break;
9677
9678 #endif
9679 #ifdef HAVE_consttable_16
9680             case 16:
9681               scan = emit_insn_after (gen_consttable_16 (mp->value), scan);
9682               break;
9683
9684 #endif
9685             default:
9686               gcc_unreachable ();
9687             }
9688         }
9689
9690       nmp = mp->next;
9691       free (mp);
9692     }
9693
9694   minipool_vector_head = minipool_vector_tail = NULL;
9695   scan = emit_insn_after (gen_consttable_end (), scan);
9696   scan = emit_barrier_after (scan);
9697 }
9698
9699 /* Return the cost of forcibly inserting a barrier after INSN.  */
9700 static int
9701 arm_barrier_cost (rtx insn)
9702 {
9703   /* Basing the location of the pool on the loop depth is preferable,
9704      but at the moment, the basic block information seems to be
9705      corrupt by this stage of the compilation.  */
9706   int base_cost = 50;
9707   rtx next = next_nonnote_insn (insn);
9708
9709   if (next != NULL && GET_CODE (next) == CODE_LABEL)
9710     base_cost -= 20;
9711
9712   switch (GET_CODE (insn))
9713     {
9714     case CODE_LABEL:
9715       /* It will always be better to place the table before the label, rather
9716          than after it.  */
9717       return 50;
9718
9719     case INSN:
9720     case CALL_INSN:
9721       return base_cost;
9722
9723     case JUMP_INSN:
9724       return base_cost - 10;
9725
9726     default:
9727       return base_cost + 10;
9728     }
9729 }
9730
9731 /* Find the best place in the insn stream in the range
9732    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
9733    Create the barrier by inserting a jump and add a new fix entry for
9734    it.  */
9735 static Mfix *
9736 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
9737 {
9738   HOST_WIDE_INT count = 0;
9739   rtx barrier;
9740   rtx from = fix->insn;
9741   /* The instruction after which we will insert the jump.  */
9742   rtx selected = NULL;
9743   int selected_cost;
9744   /* The address at which the jump instruction will be placed.  */
9745   HOST_WIDE_INT selected_address;
9746   Mfix * new_fix;
9747   HOST_WIDE_INT max_count = max_address - fix->address;
9748   rtx label = gen_label_rtx ();
9749
9750   selected_cost = arm_barrier_cost (from);
9751   selected_address = fix->address;
9752
9753   while (from && count < max_count)
9754     {
9755       rtx tmp;
9756       int new_cost;
9757
9758       /* This code shouldn't have been called if there was a natural barrier
9759          within range.  */
9760       gcc_assert (GET_CODE (from) != BARRIER);
9761
9762       /* Count the length of this insn.  */
9763       count += get_attr_length (from);
9764
9765       /* If there is a jump table, add its length.  */
9766       tmp = is_jump_table (from);
9767       if (tmp != NULL)
9768         {
9769           count += get_jump_table_size (tmp);
9770
9771           /* Jump tables aren't in a basic block, so base the cost on
9772              the dispatch insn.  If we select this location, we will
9773              still put the pool after the table.  */
9774           new_cost = arm_barrier_cost (from);
9775
9776           if (count < max_count 
9777               && (!selected || new_cost <= selected_cost))
9778             {
9779               selected = tmp;
9780               selected_cost = new_cost;
9781               selected_address = fix->address + count;
9782             }
9783
9784           /* Continue after the dispatch table.  */
9785           from = NEXT_INSN (tmp);
9786           continue;
9787         }
9788
9789       new_cost = arm_barrier_cost (from);
9790
9791       if (count < max_count
9792           && (!selected || new_cost <= selected_cost))
9793         {
9794           selected = from;
9795           selected_cost = new_cost;
9796           selected_address = fix->address + count;
9797         }
9798
9799       from = NEXT_INSN (from);
9800     }
9801
9802   /* Make sure that we found a place to insert the jump.  */
9803   gcc_assert (selected);
9804
9805   /* Create a new JUMP_INSN that branches around a barrier.  */
9806   from = emit_jump_insn_after (gen_jump (label), selected);
9807   JUMP_LABEL (from) = label;
9808   barrier = emit_barrier_after (from);
9809   emit_label_after (label, barrier);
9810
9811   /* Create a minipool barrier entry for the new barrier.  */
9812   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
9813   new_fix->insn = barrier;
9814   new_fix->address = selected_address;
9815   new_fix->next = fix->next;
9816   fix->next = new_fix;
9817
9818   return new_fix;
9819 }
9820
9821 /* Record that there is a natural barrier in the insn stream at
9822    ADDRESS.  */
9823 static void
9824 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
9825 {
9826   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
9827
9828   fix->insn = insn;
9829   fix->address = address;
9830
9831   fix->next = NULL;
9832   if (minipool_fix_head != NULL)
9833     minipool_fix_tail->next = fix;
9834   else
9835     minipool_fix_head = fix;
9836
9837   minipool_fix_tail = fix;
9838 }
9839
9840 /* Record INSN, which will need fixing up to load a value from the
9841    minipool.  ADDRESS is the offset of the insn since the start of the
9842    function; LOC is a pointer to the part of the insn which requires
9843    fixing; VALUE is the constant that must be loaded, which is of type
9844    MODE.  */
9845 static void
9846 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
9847                    enum machine_mode mode, rtx value)
9848 {
9849   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
9850
9851   fix->insn = insn;
9852   fix->address = address;
9853   fix->loc = loc;
9854   fix->mode = mode;
9855   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
9856   fix->value = value;
9857   fix->forwards = get_attr_pool_range (insn);
9858   fix->backwards = get_attr_neg_pool_range (insn);
9859   fix->minipool = NULL;
9860
9861   /* If an insn doesn't have a range defined for it, then it isn't
9862      expecting to be reworked by this code.  Better to stop now than
9863      to generate duff assembly code.  */
9864   gcc_assert (fix->forwards || fix->backwards);
9865
9866   /* If an entry requires 8-byte alignment then assume all constant pools
9867      require 4 bytes of padding.  Trying to do this later on a per-pool
9868      basis is awkward because existing pool entries have to be modified.  */
9869   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size >= 8)
9870     minipool_pad = 4;
9871
9872   if (dump_file)
9873     {
9874       fprintf (dump_file,
9875                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
9876                GET_MODE_NAME (mode),
9877                INSN_UID (insn), (unsigned long) address,
9878                -1 * (long)fix->backwards, (long)fix->forwards);
9879       arm_print_value (dump_file, fix->value);
9880       fprintf (dump_file, "\n");
9881     }
9882
9883   /* Add it to the chain of fixes.  */
9884   fix->next = NULL;
9885
9886   if (minipool_fix_head != NULL)
9887     minipool_fix_tail->next = fix;
9888   else
9889     minipool_fix_head = fix;
9890
9891   minipool_fix_tail = fix;
9892 }
9893
9894 /* Return the cost of synthesizing a 64-bit constant VAL inline.
9895    Returns the number of insns needed, or 99 if we don't know how to
9896    do it.  */
9897 int
9898 arm_const_double_inline_cost (rtx val)
9899 {
9900   rtx lowpart, highpart;
9901   enum machine_mode mode;
9902
9903   mode = GET_MODE (val);
9904
9905   if (mode == VOIDmode)
9906     mode = DImode;
9907
9908   gcc_assert (GET_MODE_SIZE (mode) == 8);
9909
9910   lowpart = gen_lowpart (SImode, val);
9911   highpart = gen_highpart_mode (SImode, mode, val);
9912
9913   gcc_assert (GET_CODE (lowpart) == CONST_INT);
9914   gcc_assert (GET_CODE (highpart) == CONST_INT);
9915
9916   return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
9917                             NULL_RTX, NULL_RTX, 0, 0)
9918           + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
9919                               NULL_RTX, NULL_RTX, 0, 0));
9920 }
9921
9922 /* Return true if it is worthwhile to split a 64-bit constant into two
9923    32-bit operations.  This is the case if optimizing for size, or
9924    if we have load delay slots, or if one 32-bit part can be done with
9925    a single data operation.  */
9926 bool
9927 arm_const_double_by_parts (rtx val)
9928 {
9929   enum machine_mode mode = GET_MODE (val);
9930   rtx part;
9931
9932   if (optimize_size || arm_ld_sched)
9933     return true;
9934
9935   if (mode == VOIDmode)
9936     mode = DImode;
9937
9938   part = gen_highpart_mode (SImode, mode, val);
9939
9940   gcc_assert (GET_CODE (part) == CONST_INT);
9941
9942   if (const_ok_for_arm (INTVAL (part))
9943       || const_ok_for_arm (~INTVAL (part)))
9944     return true;
9945
9946   part = gen_lowpart (SImode, val);
9947
9948   gcc_assert (GET_CODE (part) == CONST_INT);
9949
9950   if (const_ok_for_arm (INTVAL (part))
9951       || const_ok_for_arm (~INTVAL (part)))
9952     return true;
9953
9954   return false;
9955 }
9956
9957 /* Scan INSN and note any of its operands that need fixing.
9958    If DO_PUSHES is false we do not actually push any of the fixups
9959    needed.  The function returns TRUE if any fixups were needed/pushed.
9960    This is used by arm_memory_load_p() which needs to know about loads
9961    of constants that will be converted into minipool loads.  */
9962 static bool
9963 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
9964 {
9965   bool result = false;
9966   int opno;
9967
9968   extract_insn (insn);
9969
9970   if (!constrain_operands (1))
9971     fatal_insn_not_found (insn);
9972
9973   if (recog_data.n_alternatives == 0)
9974     return false;
9975
9976   /* Fill in recog_op_alt with information about the constraints of
9977      this insn.  */
9978   preprocess_constraints ();
9979
9980   for (opno = 0; opno < recog_data.n_operands; opno++)
9981     {
9982       /* Things we need to fix can only occur in inputs.  */
9983       if (recog_data.operand_type[opno] != OP_IN)
9984         continue;
9985
9986       /* If this alternative is a memory reference, then any mention
9987          of constants in this alternative is really to fool reload
9988          into allowing us to accept one there.  We need to fix them up
9989          now so that we output the right code.  */
9990       if (recog_op_alt[opno][which_alternative].memory_ok)
9991         {
9992           rtx op = recog_data.operand[opno];
9993
9994           if (CONSTANT_P (op))
9995             {
9996               if (do_pushes)
9997                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
9998                                    recog_data.operand_mode[opno], op);
9999               result = true;
10000             }
10001           else if (GET_CODE (op) == MEM
10002                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
10003                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
10004             {
10005               if (do_pushes)
10006                 {
10007                   rtx cop = avoid_constant_pool_reference (op);
10008
10009                   /* Casting the address of something to a mode narrower
10010                      than a word can cause avoid_constant_pool_reference()
10011                      to return the pool reference itself.  That's no good to
10012                      us here.  Lets just hope that we can use the
10013                      constant pool value directly.  */
10014                   if (op == cop)
10015                     cop = get_pool_constant (XEXP (op, 0));
10016
10017                   push_minipool_fix (insn, address,
10018                                      recog_data.operand_loc[opno],
10019                                      recog_data.operand_mode[opno], cop);
10020                 }
10021
10022               result = true;
10023             }
10024         }
10025     }
10026
10027   return result;
10028 }
10029
10030 /* Gcc puts the pool in the wrong place for ARM, since we can only
10031    load addresses a limited distance around the pc.  We do some
10032    special munging to move the constant pool values to the correct
10033    point in the code.  */
10034 static void
10035 arm_reorg (void)
10036 {
10037   rtx insn;
10038   HOST_WIDE_INT address = 0;
10039   Mfix * fix;
10040
10041   minipool_fix_head = minipool_fix_tail = NULL;
10042
10043   /* The first insn must always be a note, or the code below won't
10044      scan it properly.  */
10045   insn = get_insns ();
10046   gcc_assert (GET_CODE (insn) == NOTE);
10047   minipool_pad = 0;
10048
10049   /* Scan all the insns and record the operands that will need fixing.  */
10050   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
10051     {
10052       if (TARGET_CIRRUS_FIX_INVALID_INSNS
10053           && (arm_cirrus_insn_p (insn)
10054               || GET_CODE (insn) == JUMP_INSN
10055               || arm_memory_load_p (insn)))
10056         cirrus_reorg (insn);
10057
10058       if (GET_CODE (insn) == BARRIER)
10059         push_minipool_barrier (insn, address);
10060       else if (INSN_P (insn))
10061         {
10062           rtx table;
10063
10064           note_invalid_constants (insn, address, true);
10065           address += get_attr_length (insn);
10066
10067           /* If the insn is a vector jump, add the size of the table
10068              and skip the table.  */
10069           if ((table = is_jump_table (insn)) != NULL)
10070             {
10071               address += get_jump_table_size (table);
10072               insn = table;
10073             }
10074         }
10075     }
10076
10077   fix = minipool_fix_head;
10078
10079   /* Now scan the fixups and perform the required changes.  */
10080   while (fix)
10081     {
10082       Mfix * ftmp;
10083       Mfix * fdel;
10084       Mfix *  last_added_fix;
10085       Mfix * last_barrier = NULL;
10086       Mfix * this_fix;
10087
10088       /* Skip any further barriers before the next fix.  */
10089       while (fix && GET_CODE (fix->insn) == BARRIER)
10090         fix = fix->next;
10091
10092       /* No more fixes.  */
10093       if (fix == NULL)
10094         break;
10095
10096       last_added_fix = NULL;
10097
10098       for (ftmp = fix; ftmp; ftmp = ftmp->next)
10099         {
10100           if (GET_CODE (ftmp->insn) == BARRIER)
10101             {
10102               if (ftmp->address >= minipool_vector_head->max_address)
10103                 break;
10104
10105               last_barrier = ftmp;
10106             }
10107           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
10108             break;
10109
10110           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
10111         }
10112
10113       /* If we found a barrier, drop back to that; any fixes that we
10114          could have reached but come after the barrier will now go in
10115          the next mini-pool.  */
10116       if (last_barrier != NULL)
10117         {
10118           /* Reduce the refcount for those fixes that won't go into this
10119              pool after all.  */
10120           for (fdel = last_barrier->next;
10121                fdel && fdel != ftmp;
10122                fdel = fdel->next)
10123             {
10124               fdel->minipool->refcount--;
10125               fdel->minipool = NULL;
10126             }
10127
10128           ftmp = last_barrier;
10129         }
10130       else
10131         {
10132           /* ftmp is first fix that we can't fit into this pool and
10133              there no natural barriers that we could use.  Insert a
10134              new barrier in the code somewhere between the previous
10135              fix and this one, and arrange to jump around it.  */
10136           HOST_WIDE_INT max_address;
10137
10138           /* The last item on the list of fixes must be a barrier, so
10139              we can never run off the end of the list of fixes without
10140              last_barrier being set.  */
10141           gcc_assert (ftmp);
10142
10143           max_address = minipool_vector_head->max_address;
10144           /* Check that there isn't another fix that is in range that
10145              we couldn't fit into this pool because the pool was
10146              already too large: we need to put the pool before such an
10147              instruction.  The pool itself may come just after the
10148              fix because create_fix_barrier also allows space for a
10149              jump instruction.  */
10150           if (ftmp->address < max_address)
10151             max_address = ftmp->address + 1;
10152
10153           last_barrier = create_fix_barrier (last_added_fix, max_address);
10154         }
10155
10156       assign_minipool_offsets (last_barrier);
10157
10158       while (ftmp)
10159         {
10160           if (GET_CODE (ftmp->insn) != BARRIER
10161               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
10162                   == NULL))
10163             break;
10164
10165           ftmp = ftmp->next;
10166         }
10167
10168       /* Scan over the fixes we have identified for this pool, fixing them
10169          up and adding the constants to the pool itself.  */
10170       for (this_fix = fix; this_fix && ftmp != this_fix;
10171            this_fix = this_fix->next)
10172         if (GET_CODE (this_fix->insn) != BARRIER)
10173           {
10174             rtx addr
10175               = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
10176                                                   minipool_vector_label),
10177                                this_fix->minipool->offset);
10178             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
10179           }
10180
10181       dump_minipool (last_barrier->insn);
10182       fix = ftmp;
10183     }
10184
10185   /* From now on we must synthesize any constants that we can't handle
10186      directly.  This can happen if the RTL gets split during final
10187      instruction generation.  */
10188   after_arm_reorg = 1;
10189
10190   /* Free the minipool memory.  */
10191   obstack_free (&minipool_obstack, minipool_startobj);
10192 }
10193 \f
10194 /* Routines to output assembly language.  */
10195
10196 /* If the rtx is the correct value then return the string of the number.
10197    In this way we can ensure that valid double constants are generated even
10198    when cross compiling.  */
10199 const char *
10200 fp_immediate_constant (rtx x)
10201 {
10202   REAL_VALUE_TYPE r;
10203   int i;
10204
10205   if (!fp_consts_inited)
10206     init_fp_table ();
10207
10208   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10209   for (i = 0; i < 8; i++)
10210     if (REAL_VALUES_EQUAL (r, values_fp[i]))
10211       return strings_fp[i];
10212
10213   gcc_unreachable ();
10214 }
10215
10216 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
10217 static const char *
10218 fp_const_from_val (REAL_VALUE_TYPE *r)
10219 {
10220   int i;
10221
10222   if (!fp_consts_inited)
10223     init_fp_table ();
10224
10225   for (i = 0; i < 8; i++)
10226     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
10227       return strings_fp[i];
10228
10229   gcc_unreachable ();
10230 }
10231
10232 /* Output the operands of a LDM/STM instruction to STREAM.
10233    MASK is the ARM register set mask of which only bits 0-15 are important.
10234    REG is the base register, either the frame pointer or the stack pointer,
10235    INSTR is the possibly suffixed load or store instruction.
10236    RFE is nonzero if the instruction should also copy spsr to cpsr.  */
10237
10238 static void
10239 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
10240                  unsigned long mask, int rfe)
10241 {
10242   unsigned i;
10243   bool not_first = FALSE;
10244
10245   gcc_assert (!rfe || (mask & (1 << PC_REGNUM)));
10246   fputc ('\t', stream);
10247   asm_fprintf (stream, instr, reg);
10248   fputc ('{', stream);
10249
10250   for (i = 0; i <= LAST_ARM_REGNUM; i++)
10251     if (mask & (1 << i))
10252       {
10253         if (not_first)
10254           fprintf (stream, ", ");
10255
10256         asm_fprintf (stream, "%r", i);
10257         not_first = TRUE;
10258       }
10259
10260   if (rfe)
10261     fprintf (stream, "}^\n");
10262   else
10263     fprintf (stream, "}\n");
10264 }
10265
10266
10267 /* Output a FLDMD instruction to STREAM.
10268    BASE if the register containing the address.
10269    REG and COUNT specify the register range.
10270    Extra registers may be added to avoid hardware bugs.
10271
10272    We output FLDMD even for ARMv5 VFP implementations.  Although
10273    FLDMD is technically not supported until ARMv6, it is believed
10274    that all VFP implementations support its use in this context.  */
10275
10276 static void
10277 vfp_output_fldmd (FILE * stream, unsigned int base, int reg, int count)
10278 {
10279   int i;
10280
10281   /* Workaround ARM10 VFPr1 bug.  */
10282   if (count == 2 && !arm_arch6)
10283     {
10284       if (reg == 15)
10285         reg--;
10286       count++;
10287     }
10288
10289   /* FLDMD may not load more than 16 doubleword registers at a time. Split the
10290      load into multiple parts if we have to handle more than 16 registers.  */
10291   if (count > 16)
10292     {
10293       vfp_output_fldmd (stream, base, reg, 16);
10294       vfp_output_fldmd (stream, base, reg + 16, count - 16);
10295       return;
10296     }
10297
10298   fputc ('\t', stream);
10299   asm_fprintf (stream, "fldmfdd\t%r!, {", base);
10300
10301   for (i = reg; i < reg + count; i++)
10302     {
10303       if (i > reg)
10304         fputs (", ", stream);
10305       asm_fprintf (stream, "d%d", i);
10306     }
10307   fputs ("}\n", stream);
10308
10309 }
10310
10311
10312 /* Output the assembly for a store multiple.  */
10313
10314 const char *
10315 vfp_output_fstmd (rtx * operands)
10316 {
10317   char pattern[100];
10318   int p;
10319   int base;
10320   int i;
10321
10322   strcpy (pattern, "fstmfdd\t%m0!, {%P1");
10323   p = strlen (pattern);
10324
10325   gcc_assert (GET_CODE (operands[1]) == REG);
10326
10327   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
10328   for (i = 1; i < XVECLEN (operands[2], 0); i++)
10329     {
10330       p += sprintf (&pattern[p], ", d%d", base + i);
10331     }
10332   strcpy (&pattern[p], "}");
10333
10334   output_asm_insn (pattern, operands);
10335   return "";
10336 }
10337
10338
10339 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
10340    number of bytes pushed.  */
10341
10342 static int
10343 vfp_emit_fstmd (int base_reg, int count)
10344 {
10345   rtx par;
10346   rtx dwarf;
10347   rtx tmp, reg;
10348   int i;
10349
10350   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
10351      register pairs are stored by a store multiple insn.  We avoid this
10352      by pushing an extra pair.  */
10353   if (count == 2 && !arm_arch6)
10354     {
10355       if (base_reg == LAST_VFP_REGNUM - 3)
10356         base_reg -= 2;
10357       count++;
10358     }
10359
10360   /* FSTMD may not store more than 16 doubleword registers at once.  Split
10361      larger stores into multiple parts (up to a maximum of two, in
10362      practice).  */
10363   if (count > 16)
10364     {
10365       int saved;
10366       /* NOTE: base_reg is an internal register number, so each D register
10367          counts as 2.  */
10368       saved = vfp_emit_fstmd (base_reg + 32, count - 16);
10369       saved += vfp_emit_fstmd (base_reg, 16);
10370       return saved;
10371     }
10372
10373   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
10374   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
10375
10376   reg = gen_rtx_REG (DFmode, base_reg);
10377   base_reg += 2;
10378
10379   XVECEXP (par, 0, 0)
10380     = gen_rtx_SET (VOIDmode,
10381                    gen_frame_mem (BLKmode,
10382                                   gen_rtx_PRE_DEC (BLKmode,
10383                                                    stack_pointer_rtx)),
10384                    gen_rtx_UNSPEC (BLKmode,
10385                                    gen_rtvec (1, reg),
10386                                    UNSPEC_PUSH_MULT));
10387
10388   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10389                      plus_constant (stack_pointer_rtx, -(count * 8)));
10390   RTX_FRAME_RELATED_P (tmp) = 1;
10391   XVECEXP (dwarf, 0, 0) = tmp;
10392
10393   tmp = gen_rtx_SET (VOIDmode,
10394                      gen_frame_mem (DFmode, stack_pointer_rtx),
10395                      reg);
10396   RTX_FRAME_RELATED_P (tmp) = 1;
10397   XVECEXP (dwarf, 0, 1) = tmp;
10398
10399   for (i = 1; i < count; i++)
10400     {
10401       reg = gen_rtx_REG (DFmode, base_reg);
10402       base_reg += 2;
10403       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
10404
10405       tmp = gen_rtx_SET (VOIDmode,
10406                          gen_frame_mem (DFmode,
10407                                         plus_constant (stack_pointer_rtx,
10408                                                        i * 8)),
10409                          reg);
10410       RTX_FRAME_RELATED_P (tmp) = 1;
10411       XVECEXP (dwarf, 0, i + 1) = tmp;
10412     }
10413
10414   par = emit_insn (par);
10415   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
10416   RTX_FRAME_RELATED_P (par) = 1;
10417
10418   return count * 8;
10419 }
10420
10421 /* Emit a call instruction with pattern PAT.  ADDR is the address of
10422    the call target.  */
10423
10424 void
10425 arm_emit_call_insn (rtx pat, rtx addr)
10426 {
10427   rtx insn;
10428
10429   insn = emit_call_insn (pat);
10430
10431   /* The PIC register is live on entry to VxWorks PIC PLT entries.
10432      If the call might use such an entry, add a use of the PIC register
10433      to the instruction's CALL_INSN_FUNCTION_USAGE.  */
10434   if (TARGET_VXWORKS_RTP
10435       && flag_pic
10436       && GET_CODE (addr) == SYMBOL_REF
10437       && (SYMBOL_REF_DECL (addr)
10438           ? !targetm.binds_local_p (SYMBOL_REF_DECL (addr))
10439           : !SYMBOL_REF_LOCAL_P (addr)))
10440     {
10441       require_pic_register ();
10442       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), cfun->machine->pic_reg);
10443     }
10444 }
10445
10446 /* Output a 'call' insn.  */
10447 const char *
10448 output_call (rtx *operands)
10449 {
10450   gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly.  */
10451
10452   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
10453   if (REGNO (operands[0]) == LR_REGNUM)
10454     {
10455       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
10456       output_asm_insn ("mov%?\t%0, %|lr", operands);
10457     }
10458
10459   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
10460
10461   if (TARGET_INTERWORK || arm_arch4t)
10462     output_asm_insn ("bx%?\t%0", operands);
10463   else
10464     output_asm_insn ("mov%?\t%|pc, %0", operands);
10465
10466   return "";
10467 }
10468
10469 /* Output a 'call' insn that is a reference in memory.  */
10470 const char *
10471 output_call_mem (rtx *operands)
10472 {
10473   if (TARGET_INTERWORK && !arm_arch5)
10474     {
10475       output_asm_insn ("ldr%?\t%|ip, %0", operands);
10476       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
10477       output_asm_insn ("bx%?\t%|ip", operands);
10478     }
10479   else if (regno_use_in (LR_REGNUM, operands[0]))
10480     {
10481       /* LR is used in the memory address.  We load the address in the
10482          first instruction.  It's safe to use IP as the target of the
10483          load since the call will kill it anyway.  */
10484       output_asm_insn ("ldr%?\t%|ip, %0", operands);
10485       if (arm_arch5)
10486         output_asm_insn ("blx%?\t%|ip", operands);
10487       else
10488         {
10489           output_asm_insn ("mov%?\t%|lr, %|pc", operands);
10490           if (arm_arch4t)
10491             output_asm_insn ("bx%?\t%|ip", operands);
10492           else
10493             output_asm_insn ("mov%?\t%|pc, %|ip", operands);
10494         }
10495     }
10496   else
10497     {
10498       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
10499       output_asm_insn ("ldr%?\t%|pc, %0", operands);
10500     }
10501
10502   return "";
10503 }
10504
10505
10506 /* Output a move from arm registers to an fpa registers.
10507    OPERANDS[0] is an fpa register.
10508    OPERANDS[1] is the first registers of an arm register pair.  */
10509 const char *
10510 output_mov_long_double_fpa_from_arm (rtx *operands)
10511 {
10512   int arm_reg0 = REGNO (operands[1]);
10513   rtx ops[3];
10514
10515   gcc_assert (arm_reg0 != IP_REGNUM);
10516
10517   ops[0] = gen_rtx_REG (SImode, arm_reg0);
10518   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
10519   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
10520
10521   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
10522   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
10523
10524   return "";
10525 }
10526
10527 /* Output a move from an fpa register to arm registers.
10528    OPERANDS[0] is the first registers of an arm register pair.
10529    OPERANDS[1] is an fpa register.  */
10530 const char *
10531 output_mov_long_double_arm_from_fpa (rtx *operands)
10532 {
10533   int arm_reg0 = REGNO (operands[0]);
10534   rtx ops[3];
10535
10536   gcc_assert (arm_reg0 != IP_REGNUM);
10537
10538   ops[0] = gen_rtx_REG (SImode, arm_reg0);
10539   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
10540   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
10541
10542   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
10543   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
10544   return "";
10545 }
10546
10547 /* Output a move from arm registers to arm registers of a long double
10548    OPERANDS[0] is the destination.
10549    OPERANDS[1] is the source.  */
10550 const char *
10551 output_mov_long_double_arm_from_arm (rtx *operands)
10552 {
10553   /* We have to be careful here because the two might overlap.  */
10554   int dest_start = REGNO (operands[0]);
10555   int src_start = REGNO (operands[1]);
10556   rtx ops[2];
10557   int i;
10558
10559   if (dest_start < src_start)
10560     {
10561       for (i = 0; i < 3; i++)
10562         {
10563           ops[0] = gen_rtx_REG (SImode, dest_start + i);
10564           ops[1] = gen_rtx_REG (SImode, src_start + i);
10565           output_asm_insn ("mov%?\t%0, %1", ops);
10566         }
10567     }
10568   else
10569     {
10570       for (i = 2; i >= 0; i--)
10571         {
10572           ops[0] = gen_rtx_REG (SImode, dest_start + i);
10573           ops[1] = gen_rtx_REG (SImode, src_start + i);
10574           output_asm_insn ("mov%?\t%0, %1", ops);
10575         }
10576     }
10577
10578   return "";
10579 }
10580
10581
10582 /* Emit a MOVW/MOVT pair.  */
10583 void arm_emit_movpair (rtx dest, rtx src)
10584 {
10585   emit_set_insn (dest, gen_rtx_HIGH (SImode, src));
10586   emit_set_insn (dest, gen_rtx_LO_SUM (SImode, dest, src));
10587 }
10588
10589
10590 /* Output a move from arm registers to an fpa registers.
10591    OPERANDS[0] is an fpa register.
10592    OPERANDS[1] is the first registers of an arm register pair.  */
10593 const char *
10594 output_mov_double_fpa_from_arm (rtx *operands)
10595 {
10596   int arm_reg0 = REGNO (operands[1]);
10597   rtx ops[2];
10598
10599   gcc_assert (arm_reg0 != IP_REGNUM);
10600
10601   ops[0] = gen_rtx_REG (SImode, arm_reg0);
10602   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
10603   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1}", ops);
10604   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
10605   return "";
10606 }
10607
10608 /* Output a move from an fpa register to arm registers.
10609    OPERANDS[0] is the first registers of an arm register pair.
10610    OPERANDS[1] is an fpa register.  */
10611 const char *
10612 output_mov_double_arm_from_fpa (rtx *operands)
10613 {
10614   int arm_reg0 = REGNO (operands[0]);
10615   rtx ops[2];
10616
10617   gcc_assert (arm_reg0 != IP_REGNUM);
10618
10619   ops[0] = gen_rtx_REG (SImode, arm_reg0);
10620   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
10621   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
10622   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1}", ops);
10623   return "";
10624 }
10625
10626 /* Output a move between double words.
10627    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
10628    or MEM<-REG and all MEMs must be offsettable addresses.  */
10629 const char *
10630 output_move_double (rtx *operands)
10631 {
10632   enum rtx_code code0 = GET_CODE (operands[0]);
10633   enum rtx_code code1 = GET_CODE (operands[1]);
10634   rtx otherops[3];
10635
10636   if (code0 == REG)
10637     {
10638       unsigned int reg0 = REGNO (operands[0]);
10639
10640       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
10641
10642       gcc_assert (code1 == MEM);  /* Constraints should ensure this.  */
10643
10644       switch (GET_CODE (XEXP (operands[1], 0)))
10645         {
10646         case REG:
10647           if (TARGET_LDRD
10648               && !(fix_cm3_ldrd && reg0 == REGNO(XEXP (operands[1], 0))))
10649             output_asm_insn ("ldr%(d%)\t%0, [%m1]", operands);
10650           else
10651             output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
10652           break;
10653
10654         case PRE_INC:
10655           gcc_assert (TARGET_LDRD);
10656           output_asm_insn ("ldr%(d%)\t%0, [%m1, #8]!", operands);
10657           break;
10658
10659         case PRE_DEC:
10660           if (TARGET_LDRD)
10661             output_asm_insn ("ldr%(d%)\t%0, [%m1, #-8]!", operands);
10662           else
10663             output_asm_insn ("ldm%(db%)\t%m1!, %M0", operands);
10664           break;
10665
10666         case POST_INC:
10667           if (TARGET_LDRD)
10668             output_asm_insn ("ldr%(d%)\t%0, [%m1], #8", operands);
10669           else
10670             output_asm_insn ("ldm%(ia%)\t%m1!, %M0", operands);
10671           break;
10672
10673         case POST_DEC:
10674           gcc_assert (TARGET_LDRD);
10675           output_asm_insn ("ldr%(d%)\t%0, [%m1], #-8", operands);
10676           break;
10677
10678         case PRE_MODIFY:
10679         case POST_MODIFY:
10680           /* Autoicrement addressing modes should never have overlapping
10681              base and destination registers, and overlapping index registers
10682              are already prohibited, so this doesn't need to worry about
10683              fix_cm3_ldrd.  */
10684           otherops[0] = operands[0];
10685           otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
10686           otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
10687
10688           if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
10689             {
10690               if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
10691                 {
10692                   /* Registers overlap so split out the increment.  */
10693                   output_asm_insn ("add%?\t%1, %1, %2", otherops);
10694                   output_asm_insn ("ldr%(d%)\t%0, [%1] @split", otherops);
10695                 }
10696               else
10697                 {
10698                   /* Use a single insn if we can.
10699                      FIXME: IWMMXT allows offsets larger than ldrd can
10700                      handle, fix these up with a pair of ldr.  */
10701                   if (TARGET_THUMB2
10702                       || GET_CODE (otherops[2]) != CONST_INT
10703                       || (INTVAL (otherops[2]) > -256
10704                           && INTVAL (otherops[2]) < 256))
10705                     output_asm_insn ("ldr%(d%)\t%0, [%1, %2]!", otherops);
10706                   else
10707                     {
10708                       output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
10709                       output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
10710                     }
10711                 }
10712             }
10713           else
10714             {
10715               /* Use a single insn if we can.
10716                  FIXME: IWMMXT allows offsets larger than ldrd can handle,
10717                  fix these up with a pair of ldr.  */
10718               if (TARGET_THUMB2
10719                   || GET_CODE (otherops[2]) != CONST_INT
10720                   || (INTVAL (otherops[2]) > -256
10721                       && INTVAL (otherops[2]) < 256))
10722                 output_asm_insn ("ldr%(d%)\t%0, [%1], %2", otherops);
10723               else
10724                 {
10725                   output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
10726                   output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
10727                 }
10728             }
10729           break;
10730
10731         case LABEL_REF:
10732         case CONST:
10733           /* We might be able to use ldrd %0, %1 here.  However the range is
10734              different to ldr/adr, and it is broken on some ARMv7-M
10735              implementations.  */
10736           /* Use the second register of the pair to avoid problematic
10737              overlap.  */
10738           otherops[1] = operands[1];
10739           output_asm_insn ("adr%?\t%0, %1", otherops);
10740           operands[1] = otherops[0];
10741           if (TARGET_LDRD)
10742             output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
10743           else
10744             output_asm_insn ("ldm%(ia%)\t%1, %M0", operands);
10745           break;
10746
10747           /* ??? This needs checking for thumb2.  */
10748         default:
10749           if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
10750                                GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
10751             {
10752               otherops[0] = operands[0];
10753               otherops[1] = XEXP (XEXP (operands[1], 0), 0);
10754               otherops[2] = XEXP (XEXP (operands[1], 0), 1);
10755
10756               if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
10757                 {
10758                   if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
10759                     {
10760                       switch ((int) INTVAL (otherops[2]))
10761                         {
10762                         case -8:
10763                           output_asm_insn ("ldm%(db%)\t%1, %M0", otherops);
10764                           return "";
10765                         case -4:
10766                           if (TARGET_THUMB2)
10767                             break;
10768                           output_asm_insn ("ldm%(da%)\t%1, %M0", otherops);
10769                           return "";
10770                         case 4:
10771                           if (TARGET_THUMB2)
10772                             break;
10773                           output_asm_insn ("ldm%(ib%)\t%1, %M0", otherops);
10774                           return "";
10775                         }
10776                     }
10777                   otherops[0] = gen_rtx_REG(SImode, REGNO(operands[0]) + 1);
10778                   operands[1] = otherops[0];
10779                   if (TARGET_LDRD
10780                       && (GET_CODE (otherops[2]) == REG
10781                           || TARGET_THUMB2
10782                           || (GET_CODE (otherops[2]) == CONST_INT
10783                               && INTVAL (otherops[2]) > -256
10784                               && INTVAL (otherops[2]) < 256)))
10785                     {
10786                       if (reg_overlap_mentioned_p (operands[0],
10787                                                    otherops[2]))
10788                         {
10789                           rtx tmp;
10790                           /* Swap base and index registers over to
10791                              avoid a conflict.  */
10792                           tmp = otherops[1];
10793                           otherops[1] = otherops[2];
10794                           otherops[2] = tmp;
10795                         }
10796                       /* If both registers conflict, it will usually
10797                          have been fixed by a splitter.  */
10798                       if (reg_overlap_mentioned_p (operands[0], otherops[2])
10799                           || (fix_cm3_ldrd && reg0 == REGNO (otherops[1])))
10800                         {
10801                           output_asm_insn ("add%?\t%0, %1, %2", otherops);
10802                           output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
10803                         }
10804                       else
10805                         {
10806                           otherops[0] = operands[0];
10807                           output_asm_insn ("ldr%(d%)\t%0, [%1, %2]", otherops);
10808                         }
10809                       return "";
10810                     }
10811
10812                   if (GET_CODE (otherops[2]) == CONST_INT)
10813                     {
10814                       if (!(const_ok_for_arm (INTVAL (otherops[2]))))
10815                         output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
10816                       else
10817                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
10818                     }
10819                   else
10820                     output_asm_insn ("add%?\t%0, %1, %2", otherops);
10821                 }
10822               else
10823                 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
10824
10825               if (TARGET_LDRD)
10826                 return "ldr%(d%)\t%0, [%1]";
10827
10828               return "ldm%(ia%)\t%1, %M0";
10829             }
10830           else
10831             {
10832               otherops[1] = adjust_address (operands[1], SImode, 4);
10833               /* Take care of overlapping base/data reg.  */
10834               if (reg_mentioned_p (operands[0], operands[1]))
10835                 {
10836                   output_asm_insn ("ldr%?\t%0, %1", otherops);
10837                   output_asm_insn ("ldr%?\t%0, %1", operands);
10838                 }
10839               else
10840                 {
10841                   output_asm_insn ("ldr%?\t%0, %1", operands);
10842                   output_asm_insn ("ldr%?\t%0, %1", otherops);
10843                 }
10844             }
10845         }
10846     }
10847   else
10848     {
10849       /* Constraints should ensure this.  */
10850       gcc_assert (code0 == MEM && code1 == REG);
10851       gcc_assert (REGNO (operands[1]) != IP_REGNUM);
10852
10853       switch (GET_CODE (XEXP (operands[0], 0)))
10854         {
10855         case REG:
10856           if (TARGET_LDRD)
10857             output_asm_insn ("str%(d%)\t%1, [%m0]", operands);
10858           else
10859             output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
10860           break;
10861
10862         case PRE_INC:
10863           gcc_assert (TARGET_LDRD);
10864           output_asm_insn ("str%(d%)\t%1, [%m0, #8]!", operands);
10865           break;
10866
10867         case PRE_DEC:
10868           if (TARGET_LDRD)
10869             output_asm_insn ("str%(d%)\t%1, [%m0, #-8]!", operands);
10870           else
10871             output_asm_insn ("stm%(db%)\t%m0!, %M1", operands);
10872           break;
10873
10874         case POST_INC:
10875           if (TARGET_LDRD)
10876             output_asm_insn ("str%(d%)\t%1, [%m0], #8", operands);
10877           else
10878             output_asm_insn ("stm%(ia%)\t%m0!, %M1", operands);
10879           break;
10880
10881         case POST_DEC:
10882           gcc_assert (TARGET_LDRD);
10883           output_asm_insn ("str%(d%)\t%1, [%m0], #-8", operands);
10884           break;
10885
10886         case PRE_MODIFY:
10887         case POST_MODIFY:
10888           otherops[0] = operands[1];
10889           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
10890           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
10891
10892           /* IWMMXT allows offsets larger than ldrd can handle,
10893              fix these up with a pair of ldr.  */
10894           if (!TARGET_THUMB2
10895               && GET_CODE (otherops[2]) == CONST_INT
10896               && (INTVAL(otherops[2]) <= -256
10897                   || INTVAL(otherops[2]) >= 256))
10898             {
10899               if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
10900                 {
10901                   output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
10902                   output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
10903                 }
10904               else
10905                 {
10906                   output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
10907                   output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
10908                 }
10909             }
10910           else if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
10911             output_asm_insn ("str%(d%)\t%0, [%1, %2]!", otherops);
10912           else
10913             output_asm_insn ("str%(d%)\t%0, [%1], %2", otherops);
10914           break;
10915
10916         case PLUS:
10917           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
10918           if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
10919             {
10920               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
10921                 {
10922                 case -8:
10923                   output_asm_insn ("stm%(db%)\t%m0, %M1", operands);
10924                   return "";
10925
10926                 case -4:
10927                   if (TARGET_THUMB2)
10928                     break;
10929                   output_asm_insn ("stm%(da%)\t%m0, %M1", operands);
10930                   return "";
10931
10932                 case 4:
10933                   if (TARGET_THUMB2)
10934                     break;
10935                   output_asm_insn ("stm%(ib%)\t%m0, %M1", operands);
10936                   return "";
10937                 }
10938             }
10939           if (TARGET_LDRD
10940               && (GET_CODE (otherops[2]) == REG
10941                   || TARGET_THUMB2
10942                   || (GET_CODE (otherops[2]) == CONST_INT
10943                       && INTVAL (otherops[2]) > -256
10944                       && INTVAL (otherops[2]) < 256)))
10945             {
10946               otherops[0] = operands[1];
10947               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
10948               output_asm_insn ("str%(d%)\t%0, [%1, %2]", otherops);
10949               return "";
10950             }
10951           /* Fall through */
10952
10953         default:
10954           otherops[0] = adjust_address (operands[0], SImode, 4);
10955           otherops[1] = operands[1];
10956           output_asm_insn ("str%?\t%1, %0", operands);
10957           output_asm_insn ("str%?\t%H1, %0", otherops);
10958         }
10959     }
10960
10961   return "";
10962 }
10963
10964 /* Output a move, load or store for quad-word vectors in ARM registers.  Only
10965    handles MEMs accepted by neon_vector_mem_operand with TYPE=1.  */
10966
10967 const char *
10968 output_move_quad (rtx *operands)
10969 {
10970   if (REG_P (operands[0]))
10971     {
10972       /* Load, or reg->reg move.  */
10973
10974       if (MEM_P (operands[1]))
10975         {
10976           switch (GET_CODE (XEXP (operands[1], 0)))
10977             {
10978             case REG:
10979               output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
10980               break;
10981
10982             case LABEL_REF:
10983             case CONST:
10984               output_asm_insn ("adr%?\t%0, %1", operands);
10985               output_asm_insn ("ldm%(ia%)\t%0, %M0", operands);
10986               break;
10987
10988             default:
10989               gcc_unreachable ();
10990             }
10991         }
10992       else
10993         {
10994           rtx ops[2];
10995           int dest, src, i;
10996
10997           gcc_assert (REG_P (operands[1]));
10998
10999           dest = REGNO (operands[0]);
11000           src = REGNO (operands[1]);
11001
11002           /* This seems pretty dumb, but hopefully GCC won't try to do it
11003              very often.  */
11004           if (dest < src)
11005             for (i = 0; i < 4; i++)
11006               {
11007                 ops[0] = gen_rtx_REG (SImode, dest + i);
11008                 ops[1] = gen_rtx_REG (SImode, src + i);
11009                 output_asm_insn ("mov%?\t%0, %1", ops);
11010               }
11011           else
11012             for (i = 3; i >= 0; i--)
11013               {
11014                 ops[0] = gen_rtx_REG (SImode, dest + i);
11015                 ops[1] = gen_rtx_REG (SImode, src + i);
11016                 output_asm_insn ("mov%?\t%0, %1", ops);
11017               }
11018         }
11019     }
11020   else
11021     {
11022       gcc_assert (MEM_P (operands[0]));
11023       gcc_assert (REG_P (operands[1]));
11024       gcc_assert (!reg_overlap_mentioned_p (operands[1], operands[0]));
11025
11026       switch (GET_CODE (XEXP (operands[0], 0)))
11027         {
11028         case REG:
11029           output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
11030           break;
11031
11032         default:
11033           gcc_unreachable ();
11034         }
11035     }
11036
11037   return "";
11038 }
11039
11040 /* Output a VFP load or store instruction.  */
11041
11042 const char *
11043 output_move_vfp (rtx *operands)
11044 {
11045   rtx reg, mem, addr, ops[2];
11046   int load = REG_P (operands[0]);
11047   int dp = GET_MODE_SIZE (GET_MODE (operands[0])) == 8;
11048   int integer_p = GET_MODE_CLASS (GET_MODE (operands[0])) == MODE_INT;
11049   const char *templ;
11050   char buff[50];
11051   enum machine_mode mode;
11052
11053   reg = operands[!load];
11054   mem = operands[load];
11055
11056   mode = GET_MODE (reg);
11057
11058   gcc_assert (REG_P (reg));
11059   gcc_assert (IS_VFP_REGNUM (REGNO (reg)));
11060   gcc_assert (mode == SFmode
11061               || mode == DFmode
11062               || mode == SImode
11063               || mode == DImode
11064               || (TARGET_NEON && VALID_NEON_DREG_MODE (mode)));
11065   gcc_assert (MEM_P (mem));
11066
11067   addr = XEXP (mem, 0);
11068
11069   switch (GET_CODE (addr))
11070     {
11071     case PRE_DEC:
11072       templ = "f%smdb%c%%?\t%%0!, {%%%s1}%s";
11073       ops[0] = XEXP (addr, 0);
11074       ops[1] = reg;
11075       break;
11076
11077     case POST_INC:
11078       templ = "f%smia%c%%?\t%%0!, {%%%s1}%s";
11079       ops[0] = XEXP (addr, 0);
11080       ops[1] = reg;
11081       break;
11082
11083     default:
11084       templ = "f%s%c%%?\t%%%s0, %%1%s";
11085       ops[0] = reg;
11086       ops[1] = mem;
11087       break;
11088     }
11089
11090   sprintf (buff, templ,
11091            load ? "ld" : "st",
11092            dp ? 'd' : 's',
11093            dp ? "P" : "",
11094            integer_p ? "\t%@ int" : "");
11095   output_asm_insn (buff, ops);
11096
11097   return "";
11098 }
11099
11100 /* Output a Neon quad-word load or store, or a load or store for
11101    larger structure modes.
11102
11103    WARNING: The ordering of elements is weird in big-endian mode,
11104    because we use VSTM, as required by the EABI.  GCC RTL defines
11105    element ordering based on in-memory order.  This can be differ
11106    from the architectural ordering of elements within a NEON register.
11107    The intrinsics defined in arm_neon.h use the NEON register element
11108    ordering, not the GCC RTL element ordering.
11109
11110    For example, the in-memory ordering of a big-endian a quadword
11111    vector with 16-bit elements when stored from register pair {d0,d1}
11112    will be (lowest address first, d0[N] is NEON register element N):
11113
11114      [d0[3], d0[2], d0[1], d0[0], d1[7], d1[6], d1[5], d1[4]]
11115
11116    When necessary, quadword registers (dN, dN+1) are moved to ARM
11117    registers from rN in the order:
11118
11119      dN -> (rN+1, rN), dN+1 -> (rN+3, rN+2)
11120
11121    So that STM/LDM can be used on vectors in ARM registers, and the
11122    same memory layout will result as if VSTM/VLDM were used.  */
11123
11124 const char *
11125 output_move_neon (rtx *operands)
11126 {
11127   rtx reg, mem, addr, ops[2];
11128   int regno, load = REG_P (operands[0]);
11129   const char *templ;
11130   char buff[50];
11131   enum machine_mode mode;
11132
11133   reg = operands[!load];
11134   mem = operands[load];
11135
11136   mode = GET_MODE (reg);
11137
11138   gcc_assert (REG_P (reg));
11139   regno = REGNO (reg);
11140   gcc_assert (VFP_REGNO_OK_FOR_DOUBLE (regno)
11141               || NEON_REGNO_OK_FOR_QUAD (regno));
11142   gcc_assert (VALID_NEON_DREG_MODE (mode)
11143               || VALID_NEON_QREG_MODE (mode)
11144               || VALID_NEON_STRUCT_MODE (mode));
11145   gcc_assert (MEM_P (mem));
11146
11147   addr = XEXP (mem, 0);
11148
11149   /* Strip off const from addresses like (const (plus (...))).  */
11150   if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
11151     addr = XEXP (addr, 0);
11152
11153   switch (GET_CODE (addr))
11154     {
11155     case POST_INC:
11156       templ = "v%smia%%?\t%%0!, %%h1";
11157       ops[0] = XEXP (addr, 0);
11158       ops[1] = reg;
11159       break;
11160
11161     case PRE_DEC:
11162       /* FIXME: We should be using vld1/vst1 here in BE mode?  */
11163       templ = "v%smdb%%?\t%%0!, %%h1";
11164       ops[0] = XEXP (addr, 0);
11165       ops[1] = reg;
11166       break;
11167     
11168     case POST_MODIFY:
11169       /* FIXME: Not currently enabled in neon_vector_mem_operand.  */
11170       gcc_unreachable ();
11171
11172     case LABEL_REF:
11173     case PLUS:
11174       {
11175         int nregs = HARD_REGNO_NREGS (REGNO (reg), mode) / 2;
11176         int i;
11177         int overlap = -1;
11178         for (i = 0; i < nregs; i++)
11179           {
11180             /* We're only using DImode here because it's a convenient size.  */
11181             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * i);
11182             ops[1] = adjust_address (mem, SImode, 8 * i);
11183             if (reg_overlap_mentioned_p (ops[0], mem))
11184               {
11185                 gcc_assert (overlap == -1);
11186                 overlap = i;
11187               }
11188             else
11189               {
11190                 sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
11191                 output_asm_insn (buff, ops);
11192               }
11193           }
11194         if (overlap != -1)
11195           {
11196             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * overlap);
11197             ops[1] = adjust_address (mem, SImode, 8 * overlap);
11198             sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
11199             output_asm_insn (buff, ops);
11200           }
11201
11202         return "";
11203       }
11204
11205     default:
11206       templ = "v%smia%%?\t%%m0, %%h1";
11207       ops[0] = mem;
11208       ops[1] = reg;
11209     }
11210
11211   sprintf (buff, templ, load ? "ld" : "st");
11212   output_asm_insn (buff, ops);
11213
11214   return "";
11215 }
11216
11217 /* Output an ADD r, s, #n where n may be too big for one instruction.
11218    If adding zero to one register, output nothing.  */
11219 const char *
11220 output_add_immediate (rtx *operands)
11221 {
11222   HOST_WIDE_INT n = INTVAL (operands[2]);
11223
11224   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
11225     {
11226       if (n < 0)
11227         output_multi_immediate (operands,
11228                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
11229                                 -n);
11230       else
11231         output_multi_immediate (operands,
11232                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
11233                                 n);
11234     }
11235
11236   return "";
11237 }
11238
11239 /* Output a multiple immediate operation.
11240    OPERANDS is the vector of operands referred to in the output patterns.
11241    INSTR1 is the output pattern to use for the first constant.
11242    INSTR2 is the output pattern to use for subsequent constants.
11243    IMMED_OP is the index of the constant slot in OPERANDS.
11244    N is the constant value.  */
11245 static const char *
11246 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
11247                         int immed_op, HOST_WIDE_INT n)
11248 {
11249 #if HOST_BITS_PER_WIDE_INT > 32
11250   n &= 0xffffffff;
11251 #endif
11252
11253   if (n == 0)
11254     {
11255       /* Quick and easy output.  */
11256       operands[immed_op] = const0_rtx;
11257       output_asm_insn (instr1, operands);
11258     }
11259   else
11260     {
11261       int i;
11262       const char * instr = instr1;
11263
11264       /* Note that n is never zero here (which would give no output).  */
11265       for (i = 0; i < 32; i += 2)
11266         {
11267           if (n & (3 << i))
11268             {
11269               operands[immed_op] = GEN_INT (n & (255 << i));
11270               output_asm_insn (instr, operands);
11271               instr = instr2;
11272               i += 6;
11273             }
11274         }
11275     }
11276
11277   return "";
11278 }
11279
11280 /* Return the name of a shifter operation.  */
11281 static const char *
11282 arm_shift_nmem(enum rtx_code code)
11283 {
11284   switch (code)
11285     {
11286     case ASHIFT:
11287       return ARM_LSL_NAME;
11288
11289     case ASHIFTRT:
11290       return "asr";
11291
11292     case LSHIFTRT:
11293       return "lsr";
11294
11295     case ROTATERT:
11296       return "ror";
11297
11298     default:
11299       abort();
11300     }
11301 }
11302
11303 /* Return the appropriate ARM instruction for the operation code.
11304    The returned result should not be overwritten.  OP is the rtx of the
11305    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
11306    was shifted.  */
11307 const char *
11308 arithmetic_instr (rtx op, int shift_first_arg)
11309 {
11310   switch (GET_CODE (op))
11311     {
11312     case PLUS:
11313       return "add";
11314
11315     case MINUS:
11316       return shift_first_arg ? "rsb" : "sub";
11317
11318     case IOR:
11319       return "orr";
11320
11321     case XOR:
11322       return "eor";
11323
11324     case AND:
11325       return "and";
11326
11327     case ASHIFT:
11328     case ASHIFTRT:
11329     case LSHIFTRT:
11330     case ROTATERT:
11331       return arm_shift_nmem(GET_CODE(op));
11332
11333     default:
11334       gcc_unreachable ();
11335     }
11336 }
11337
11338 /* Ensure valid constant shifts and return the appropriate shift mnemonic
11339    for the operation code.  The returned result should not be overwritten.
11340    OP is the rtx code of the shift.
11341    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
11342    shift.  */
11343 static const char *
11344 shift_op (rtx op, HOST_WIDE_INT *amountp)
11345 {
11346   const char * mnem;
11347   enum rtx_code code = GET_CODE (op);
11348
11349   switch (GET_CODE (XEXP (op, 1)))
11350     {
11351     case REG:
11352     case SUBREG:
11353       *amountp = -1;
11354       break;
11355
11356     case CONST_INT:
11357       *amountp = INTVAL (XEXP (op, 1));
11358       break;
11359
11360     default:
11361       gcc_unreachable ();
11362     }
11363
11364   switch (code)
11365     {
11366     case ROTATE:
11367       gcc_assert (*amountp != -1);
11368       *amountp = 32 - *amountp;
11369       code = ROTATERT;
11370
11371       /* Fall through.  */
11372
11373     case ASHIFT:
11374     case ASHIFTRT:
11375     case LSHIFTRT:
11376     case ROTATERT:
11377       mnem = arm_shift_nmem(code);
11378       break;
11379
11380     case MULT:
11381       /* We never have to worry about the amount being other than a
11382          power of 2, since this case can never be reloaded from a reg.  */
11383       gcc_assert (*amountp != -1);
11384       *amountp = int_log2 (*amountp);
11385       return ARM_LSL_NAME;
11386
11387     default:
11388       gcc_unreachable ();
11389     }
11390
11391   if (*amountp != -1)
11392     {
11393       /* This is not 100% correct, but follows from the desire to merge
11394          multiplication by a power of 2 with the recognizer for a
11395          shift.  >=32 is not a valid shift for "lsl", so we must try and
11396          output a shift that produces the correct arithmetical result.
11397          Using lsr #32 is identical except for the fact that the carry bit
11398          is not set correctly if we set the flags; but we never use the
11399          carry bit from such an operation, so we can ignore that.  */
11400       if (code == ROTATERT)
11401         /* Rotate is just modulo 32.  */
11402         *amountp &= 31;
11403       else if (*amountp != (*amountp & 31))
11404         {
11405           if (code == ASHIFT)
11406             mnem = "lsr";
11407           *amountp = 32;
11408         }
11409
11410       /* Shifts of 0 are no-ops.  */
11411       if (*amountp == 0)
11412         return NULL;
11413     }
11414
11415   return mnem;
11416 }
11417
11418 /* Obtain the shift from the POWER of two.  */
11419
11420 static HOST_WIDE_INT
11421 int_log2 (HOST_WIDE_INT power)
11422 {
11423   HOST_WIDE_INT shift = 0;
11424
11425   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
11426     {
11427       gcc_assert (shift <= 31);
11428       shift++;
11429     }
11430
11431   return shift;
11432 }
11433
11434 /* Output a .ascii pseudo-op, keeping track of lengths.  This is
11435    because /bin/as is horribly restrictive.  The judgement about
11436    whether or not each character is 'printable' (and can be output as
11437    is) or not (and must be printed with an octal escape) must be made
11438    with reference to the *host* character set -- the situation is
11439    similar to that discussed in the comments above pp_c_char in
11440    c-pretty-print.c.  */
11441
11442 #define MAX_ASCII_LEN 51
11443
11444 void
11445 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
11446 {
11447   int i;
11448   int len_so_far = 0;
11449
11450   fputs ("\t.ascii\t\"", stream);
11451
11452   for (i = 0; i < len; i++)
11453     {
11454       int c = p[i];
11455
11456       if (len_so_far >= MAX_ASCII_LEN)
11457         {
11458           fputs ("\"\n\t.ascii\t\"", stream);
11459           len_so_far = 0;
11460         }
11461
11462       if (ISPRINT (c))
11463         {
11464           if (c == '\\' || c == '\"')
11465             {
11466               putc ('\\', stream);
11467               len_so_far++;
11468             }
11469           putc (c, stream);
11470           len_so_far++;
11471         }
11472       else
11473         {
11474           fprintf (stream, "\\%03o", c);
11475           len_so_far += 4;
11476         }
11477     }
11478
11479   fputs ("\"\n", stream);
11480 }
11481 \f
11482 /* Compute the register save mask for registers 0 through 12
11483    inclusive.  This code is used by arm_compute_save_reg_mask.  */
11484
11485 static unsigned long
11486 arm_compute_save_reg0_reg12_mask (void)
11487 {
11488   unsigned long func_type = arm_current_func_type ();
11489   unsigned long save_reg_mask = 0;
11490   unsigned int reg;
11491
11492   if (IS_INTERRUPT (func_type))
11493     {
11494       unsigned int max_reg;
11495       /* Interrupt functions must not corrupt any registers,
11496          even call clobbered ones.  If this is a leaf function
11497          we can just examine the registers used by the RTL, but
11498          otherwise we have to assume that whatever function is
11499          called might clobber anything, and so we have to save
11500          all the call-clobbered registers as well.  */
11501       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
11502         /* FIQ handlers have registers r8 - r12 banked, so
11503            we only need to check r0 - r7, Normal ISRs only
11504            bank r14 and r15, so we must check up to r12.
11505            r13 is the stack pointer which is always preserved,
11506            so we do not need to consider it here.  */
11507         max_reg = 7;
11508       else
11509         max_reg = 12;
11510
11511       for (reg = 0; reg <= max_reg; reg++)
11512         if (df_regs_ever_live_p (reg)
11513             || (! current_function_is_leaf && call_used_regs[reg]))
11514           save_reg_mask |= (1 << reg);
11515
11516       /* Also save the pic base register if necessary.  */
11517       if (flag_pic
11518           && !TARGET_SINGLE_PIC_BASE
11519           && arm_pic_register != INVALID_REGNUM
11520           && crtl->uses_pic_offset_table)
11521         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
11522     }
11523   else
11524     {
11525       /* In the normal case we only need to save those registers
11526          which are call saved and which are used by this function.  */
11527       for (reg = 0; reg <= 11; reg++)
11528         if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
11529           save_reg_mask |= (1 << reg);
11530
11531       /* Handle the frame pointer as a special case.  */
11532       if (frame_pointer_needed)
11533         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
11534
11535       /* If we aren't loading the PIC register,
11536          don't stack it even though it may be live.  */
11537       if (flag_pic
11538           && !TARGET_SINGLE_PIC_BASE
11539           && arm_pic_register != INVALID_REGNUM
11540           && (df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM)
11541               || crtl->uses_pic_offset_table))
11542         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
11543
11544       /* The prologue will copy SP into R0, so save it.  */
11545       if (IS_STACKALIGN (func_type))
11546         save_reg_mask |= 1;
11547     }
11548
11549   /* Save registers so the exception handler can modify them.  */
11550   if (crtl->calls_eh_return)
11551     {
11552       unsigned int i;
11553
11554       for (i = 0; ; i++)
11555         {
11556           reg = EH_RETURN_DATA_REGNO (i);
11557           if (reg == INVALID_REGNUM)
11558             break;
11559           save_reg_mask |= 1 << reg;
11560         }
11561     }
11562
11563   return save_reg_mask;
11564 }
11565
11566
11567 /* Compute the number of bytes used to store the static chain register on the 
11568    stack, above the stack frame. We need to know this accurately to get the
11569    alignment of the rest of the stack frame correct. */
11570
11571 static int arm_compute_static_chain_stack_bytes (void)
11572 {
11573   unsigned long func_type = arm_current_func_type ();
11574   int static_chain_stack_bytes = 0;
11575
11576   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM &&
11577       IS_NESTED (func_type) &&
11578       df_regs_ever_live_p (3) && crtl->args.pretend_args_size == 0)
11579     static_chain_stack_bytes = 4;
11580
11581   return static_chain_stack_bytes;
11582 }
11583
11584
11585 /* Compute a bit mask of which registers need to be
11586    saved on the stack for the current function.
11587    This is used by arm_get_frame_offsets, which may add extra registers.  */
11588
11589 static unsigned long
11590 arm_compute_save_reg_mask (void)
11591 {
11592   unsigned int save_reg_mask = 0;
11593   unsigned long func_type = arm_current_func_type ();
11594   unsigned int reg;
11595
11596   if (IS_NAKED (func_type))
11597     /* This should never really happen.  */
11598     return 0;
11599
11600   /* If we are creating a stack frame, then we must save the frame pointer,
11601      IP (which will hold the old stack pointer), LR and the PC.  */
11602   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
11603     save_reg_mask |=
11604       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
11605       | (1 << IP_REGNUM)
11606       | (1 << LR_REGNUM)
11607       | (1 << PC_REGNUM);
11608
11609   /* Volatile functions do not return, so there
11610      is no need to save any other registers.  */
11611   if (IS_VOLATILE (func_type))
11612     return save_reg_mask;
11613
11614   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
11615
11616   /* Decide if we need to save the link register.
11617      Interrupt routines have their own banked link register,
11618      so they never need to save it.
11619      Otherwise if we do not use the link register we do not need to save
11620      it.  If we are pushing other registers onto the stack however, we
11621      can save an instruction in the epilogue by pushing the link register
11622      now and then popping it back into the PC.  This incurs extra memory
11623      accesses though, so we only do it when optimizing for size, and only
11624      if we know that we will not need a fancy return sequence.  */
11625   if (df_regs_ever_live_p (LR_REGNUM)
11626       || (save_reg_mask
11627           && optimize_size
11628           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
11629           && !crtl->calls_eh_return))
11630     save_reg_mask |= 1 << LR_REGNUM;
11631
11632   if (cfun->machine->lr_save_eliminated)
11633     save_reg_mask &= ~ (1 << LR_REGNUM);
11634
11635   if (TARGET_REALLY_IWMMXT
11636       && ((bit_count (save_reg_mask)
11637            + ARM_NUM_INTS (crtl->args.pretend_args_size +
11638                            arm_compute_static_chain_stack_bytes())
11639            ) % 2) != 0)
11640     {
11641       /* The total number of registers that are going to be pushed
11642          onto the stack is odd.  We need to ensure that the stack
11643          is 64-bit aligned before we start to save iWMMXt registers,
11644          and also before we start to create locals.  (A local variable
11645          might be a double or long long which we will load/store using
11646          an iWMMXt instruction).  Therefore we need to push another
11647          ARM register, so that the stack will be 64-bit aligned.  We
11648          try to avoid using the arg registers (r0 -r3) as they might be
11649          used to pass values in a tail call.  */
11650       for (reg = 4; reg <= 12; reg++)
11651         if ((save_reg_mask & (1 << reg)) == 0)
11652           break;
11653
11654       if (reg <= 12)
11655         save_reg_mask |= (1 << reg);
11656       else
11657         {
11658           cfun->machine->sibcall_blocked = 1;
11659           save_reg_mask |= (1 << 3);
11660         }
11661     }
11662
11663   /* We may need to push an additional register for use initializing the
11664      PIC base register.  */
11665   if (TARGET_THUMB2 && IS_NESTED (func_type) && flag_pic
11666       && (save_reg_mask & THUMB2_WORK_REGS) == 0)
11667     {
11668       reg = thumb_find_work_register (1 << 4);
11669       if (!call_used_regs[reg])
11670         save_reg_mask |= (1 << reg);
11671     }
11672
11673   return save_reg_mask;
11674 }
11675
11676
11677 /* Compute a bit mask of which registers need to be
11678    saved on the stack for the current function.  */
11679 static unsigned long
11680 thumb1_compute_save_reg_mask (void)
11681 {
11682   unsigned long mask;
11683   unsigned reg;
11684
11685   mask = 0;
11686   for (reg = 0; reg < 12; reg ++)
11687     if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
11688       mask |= 1 << reg;
11689
11690   if (flag_pic
11691       && !TARGET_SINGLE_PIC_BASE
11692       && arm_pic_register != INVALID_REGNUM
11693       && crtl->uses_pic_offset_table)
11694     mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
11695
11696   /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
11697   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
11698     mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
11699
11700   /* LR will also be pushed if any lo regs are pushed.  */
11701   if (mask & 0xff || thumb_force_lr_save ())
11702     mask |= (1 << LR_REGNUM);
11703
11704   /* Make sure we have a low work register if we need one.
11705      We will need one if we are going to push a high register,
11706      but we are not currently intending to push a low register.  */
11707   if ((mask & 0xff) == 0
11708       && ((mask & 0x0f00) || TARGET_BACKTRACE))
11709     {
11710       /* Use thumb_find_work_register to choose which register
11711          we will use.  If the register is live then we will
11712          have to push it.  Use LAST_LO_REGNUM as our fallback
11713          choice for the register to select.  */
11714       reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
11715       /* Make sure the register returned by thumb_find_work_register is
11716          not part of the return value.  */
11717       if (reg * UNITS_PER_WORD <= (unsigned) arm_size_return_regs ())
11718         reg = LAST_LO_REGNUM;
11719
11720       if (! call_used_regs[reg])
11721         mask |= 1 << reg;
11722     }
11723
11724   /* The 504 below is 8 bytes less than 512 because there are two possible
11725      alignment words.  We can't tell here if they will be present or not so we
11726      have to play it safe and assume that they are. */
11727   if ((CALLER_INTERWORKING_SLOT_SIZE +
11728        ROUND_UP_WORD (get_frame_size ()) +
11729        crtl->outgoing_args_size) >= 504)
11730     {
11731       /* This is the same as the code in thumb1_expand_prologue() which
11732          determines which register to use for stack decrement. */
11733       for (reg = LAST_ARG_REGNUM + 1; reg <= LAST_LO_REGNUM; reg++)
11734         if (mask & (1 << reg))
11735           break;
11736
11737       if (reg > LAST_LO_REGNUM)
11738         {
11739           /* Make sure we have a register available for stack decrement. */
11740           mask |= 1 << LAST_LO_REGNUM;
11741         }
11742     }
11743
11744   return mask;
11745 }
11746
11747
11748 /* Return the number of bytes required to save VFP registers.  */
11749 static int
11750 arm_get_vfp_saved_size (void)
11751 {
11752   unsigned int regno;
11753   int count;
11754   int saved;
11755
11756   saved = 0;
11757   /* Space for saved VFP registers.  */
11758   if (TARGET_HARD_FLOAT && TARGET_VFP)
11759     {
11760       count = 0;
11761       for (regno = FIRST_VFP_REGNUM;
11762            regno < LAST_VFP_REGNUM;
11763            regno += 2)
11764         {
11765           if ((!df_regs_ever_live_p (regno) || call_used_regs[regno])
11766               && (!df_regs_ever_live_p (regno + 1) || call_used_regs[regno + 1]))
11767             {
11768               if (count > 0)
11769                 {
11770                   /* Workaround ARM10 VFPr1 bug.  */
11771                   if (count == 2 && !arm_arch6)
11772                     count++;
11773                   saved += count * 8;
11774                 }
11775               count = 0;
11776             }
11777           else
11778             count++;
11779         }
11780       if (count > 0)
11781         {
11782           if (count == 2 && !arm_arch6)
11783             count++;
11784           saved += count * 8;
11785         }
11786     }
11787   return saved;
11788 }
11789
11790
11791 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
11792    everything bar the final return instruction.  */
11793 const char *
11794 output_return_instruction (rtx operand, int really_return, int reverse)
11795 {
11796   char conditional[10];
11797   char instr[100];
11798   unsigned reg;
11799   unsigned long live_regs_mask;
11800   unsigned long func_type;
11801   arm_stack_offsets *offsets;
11802
11803   func_type = arm_current_func_type ();
11804
11805   if (IS_NAKED (func_type))
11806     return "";
11807
11808   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
11809     {
11810       /* If this function was declared non-returning, and we have
11811          found a tail call, then we have to trust that the called
11812          function won't return.  */
11813       if (really_return)
11814         {
11815           rtx ops[2];
11816
11817           /* Otherwise, trap an attempted return by aborting.  */
11818           ops[0] = operand;
11819           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
11820                                        : "abort");
11821           assemble_external_libcall (ops[1]);
11822           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
11823         }
11824
11825       return "";
11826     }
11827
11828   gcc_assert (!cfun->calls_alloca || really_return);
11829
11830   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
11831
11832   cfun->machine->return_used_this_function = 1;
11833
11834   offsets = arm_get_frame_offsets ();
11835   live_regs_mask = offsets->saved_regs_mask;
11836
11837   if (live_regs_mask)
11838     {
11839       const char * return_reg;
11840
11841       /* If we do not have any special requirements for function exit
11842          (e.g. interworking) then we can load the return address
11843          directly into the PC.  Otherwise we must load it into LR.  */
11844       if (really_return
11845           && (IS_INTERRUPT (func_type) || !TARGET_INTERWORK))
11846         return_reg = reg_names[PC_REGNUM];
11847       else
11848         return_reg = reg_names[LR_REGNUM];
11849
11850       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
11851         {
11852           /* There are three possible reasons for the IP register
11853              being saved.  1) a stack frame was created, in which case
11854              IP contains the old stack pointer, or 2) an ISR routine
11855              corrupted it, or 3) it was saved to align the stack on
11856              iWMMXt.  In case 1, restore IP into SP, otherwise just
11857              restore IP.  */
11858           if (frame_pointer_needed)
11859             {
11860               live_regs_mask &= ~ (1 << IP_REGNUM);
11861               live_regs_mask |=   (1 << SP_REGNUM);
11862             }
11863           else
11864             gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
11865         }
11866
11867       /* On some ARM architectures it is faster to use LDR rather than
11868          LDM to load a single register.  On other architectures, the
11869          cost is the same.  In 26 bit mode, or for exception handlers,
11870          we have to use LDM to load the PC so that the CPSR is also
11871          restored.  */
11872       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
11873         if (live_regs_mask == (1U << reg))
11874           break;
11875
11876       if (reg <= LAST_ARM_REGNUM
11877           && (reg != LR_REGNUM
11878               || ! really_return
11879               || ! IS_INTERRUPT (func_type)))
11880         {
11881           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
11882                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
11883         }
11884       else
11885         {
11886           char *p;
11887           int first = 1;
11888
11889           /* Generate the load multiple instruction to restore the
11890              registers.  Note we can get here, even if
11891              frame_pointer_needed is true, but only if sp already
11892              points to the base of the saved core registers.  */
11893           if (live_regs_mask & (1 << SP_REGNUM))
11894             {
11895               unsigned HOST_WIDE_INT stack_adjust;
11896
11897               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
11898               gcc_assert (stack_adjust == 0 || stack_adjust == 4);
11899
11900               if (stack_adjust && arm_arch5 && TARGET_ARM)
11901                 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
11902               else
11903                 {
11904                   /* If we can't use ldmib (SA110 bug),
11905                      then try to pop r3 instead.  */
11906                   if (stack_adjust)
11907                     live_regs_mask |= 1 << 3;
11908                   sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
11909                 }
11910             }
11911           else
11912             sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
11913
11914           p = instr + strlen (instr);
11915
11916           for (reg = 0; reg <= SP_REGNUM; reg++)
11917             if (live_regs_mask & (1 << reg))
11918               {
11919                 int l = strlen (reg_names[reg]);
11920
11921                 if (first)
11922                   first = 0;
11923                 else
11924                   {
11925                     memcpy (p, ", ", 2);
11926                     p += 2;
11927                   }
11928
11929                 memcpy (p, "%|", 2);
11930                 memcpy (p + 2, reg_names[reg], l);
11931                 p += l + 2;
11932               }
11933
11934           if (live_regs_mask & (1 << LR_REGNUM))
11935             {
11936               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
11937               /* If returning from an interrupt, restore the CPSR.  */
11938               if (IS_INTERRUPT (func_type))
11939                 strcat (p, "^");
11940             }
11941           else
11942             strcpy (p, "}");
11943         }
11944
11945       output_asm_insn (instr, & operand);
11946
11947       /* See if we need to generate an extra instruction to
11948          perform the actual function return.  */
11949       if (really_return
11950           && func_type != ARM_FT_INTERWORKED
11951           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
11952         {
11953           /* The return has already been handled
11954              by loading the LR into the PC.  */
11955           really_return = 0;
11956         }
11957     }
11958
11959   if (really_return)
11960     {
11961       switch ((int) ARM_FUNC_TYPE (func_type))
11962         {
11963         case ARM_FT_ISR:
11964         case ARM_FT_FIQ:
11965           /* ??? This is wrong for unified assembly syntax.  */
11966           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
11967           break;
11968
11969         case ARM_FT_INTERWORKED:
11970           sprintf (instr, "bx%s\t%%|lr", conditional);
11971           break;
11972
11973         case ARM_FT_EXCEPTION:
11974           /* ??? This is wrong for unified assembly syntax.  */
11975           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
11976           break;
11977
11978         default:
11979           /* Use bx if it's available.  */
11980           if (arm_arch5 || arm_arch4t)
11981             sprintf (instr, "bx%s\t%%|lr", conditional);
11982           else
11983             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
11984           break;
11985         }
11986
11987       output_asm_insn (instr, & operand);
11988     }
11989
11990   return "";
11991 }
11992
11993 /* Write the function name into the code section, directly preceding
11994    the function prologue.
11995
11996    Code will be output similar to this:
11997      t0
11998          .ascii "arm_poke_function_name", 0
11999          .align
12000      t1
12001          .word 0xff000000 + (t1 - t0)
12002      arm_poke_function_name
12003          mov     ip, sp
12004          stmfd   sp!, {fp, ip, lr, pc}
12005          sub     fp, ip, #4
12006
12007    When performing a stack backtrace, code can inspect the value
12008    of 'pc' stored at 'fp' + 0.  If the trace function then looks
12009    at location pc - 12 and the top 8 bits are set, then we know
12010    that there is a function name embedded immediately preceding this
12011    location and has length ((pc[-3]) & 0xff000000).
12012
12013    We assume that pc is declared as a pointer to an unsigned long.
12014
12015    It is of no benefit to output the function name if we are assembling
12016    a leaf function.  These function types will not contain a stack
12017    backtrace structure, therefore it is not possible to determine the
12018    function name.  */
12019 void
12020 arm_poke_function_name (FILE *stream, const char *name)
12021 {
12022   unsigned long alignlength;
12023   unsigned long length;
12024   rtx           x;
12025
12026   length      = strlen (name) + 1;
12027   alignlength = ROUND_UP_WORD (length);
12028
12029   ASM_OUTPUT_ASCII (stream, name, length);
12030   ASM_OUTPUT_ALIGN (stream, 2);
12031   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
12032   assemble_aligned_integer (UNITS_PER_WORD, x);
12033 }
12034
12035 /* Place some comments into the assembler stream
12036    describing the current function.  */
12037 static void
12038 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
12039 {
12040   unsigned long func_type;
12041
12042   if (TARGET_THUMB1)
12043     {
12044       thumb1_output_function_prologue (f, frame_size);
12045       return;
12046     }
12047
12048   /* Sanity check.  */
12049   gcc_assert (!arm_ccfsm_state && !arm_target_insn);
12050
12051   func_type = arm_current_func_type ();
12052
12053   switch ((int) ARM_FUNC_TYPE (func_type))
12054     {
12055     default:
12056     case ARM_FT_NORMAL:
12057       break;
12058     case ARM_FT_INTERWORKED:
12059       asm_fprintf (f, "\t%@ Function supports interworking.\n");
12060       break;
12061     case ARM_FT_ISR:
12062       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
12063       break;
12064     case ARM_FT_FIQ:
12065       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
12066       break;
12067     case ARM_FT_EXCEPTION:
12068       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
12069       break;
12070     }
12071
12072   if (IS_NAKED (func_type))
12073     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
12074
12075   if (IS_VOLATILE (func_type))
12076     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
12077
12078   if (IS_NESTED (func_type))
12079     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
12080   if (IS_STACKALIGN (func_type))
12081     asm_fprintf (f, "\t%@ Stack Align: May be called with mis-aligned SP.\n");
12082
12083   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
12084                crtl->args.size,
12085                crtl->args.pretend_args_size, frame_size);
12086
12087   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
12088                frame_pointer_needed,
12089                cfun->machine->uses_anonymous_args);
12090
12091   if (cfun->machine->lr_save_eliminated)
12092     asm_fprintf (f, "\t%@ link register save eliminated.\n");
12093
12094   if (crtl->calls_eh_return)
12095     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
12096
12097 }
12098
12099 const char *
12100 arm_output_epilogue (rtx sibling)
12101 {
12102   int reg;
12103   unsigned long saved_regs_mask;
12104   unsigned long func_type;
12105   /* Floats_offset is the offset from the "virtual" frame.  In an APCS
12106      frame that is $fp + 4 for a non-variadic function.  */
12107   int floats_offset = 0;
12108   rtx operands[3];
12109   FILE * f = asm_out_file;
12110   unsigned int lrm_count = 0;
12111   int really_return = (sibling == NULL);
12112   int start_reg;
12113   arm_stack_offsets *offsets;
12114
12115   /* If we have already generated the return instruction
12116      then it is futile to generate anything else.  */
12117   if (use_return_insn (FALSE, sibling) && 
12118       (cfun->machine->return_used_this_function != 0))
12119     return "";
12120
12121   func_type = arm_current_func_type ();
12122
12123   if (IS_NAKED (func_type))
12124     /* Naked functions don't have epilogues.  */
12125     return "";
12126
12127   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
12128     {
12129       rtx op;
12130
12131       /* A volatile function should never return.  Call abort.  */
12132       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
12133       assemble_external_libcall (op);
12134       output_asm_insn ("bl\t%a0", &op);
12135
12136       return "";
12137     }
12138
12139   /* If we are throwing an exception, then we really must be doing a
12140      return, so we can't tail-call.  */
12141   gcc_assert (!crtl->calls_eh_return || really_return);
12142
12143   offsets = arm_get_frame_offsets ();
12144   saved_regs_mask = offsets->saved_regs_mask;
12145
12146   if (TARGET_IWMMXT)
12147     lrm_count = bit_count (saved_regs_mask);
12148
12149   floats_offset = offsets->saved_args;
12150   /* Compute how far away the floats will be.  */
12151   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
12152     if (saved_regs_mask & (1 << reg))
12153       floats_offset += 4;
12154
12155   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
12156     {
12157       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
12158       int vfp_offset = offsets->frame;
12159
12160       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
12161         {
12162           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
12163             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
12164               {
12165                 floats_offset += 12;
12166                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
12167                              reg, FP_REGNUM, floats_offset - vfp_offset);
12168               }
12169         }
12170       else
12171         {
12172           start_reg = LAST_FPA_REGNUM;
12173
12174           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
12175             {
12176               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
12177                 {
12178                   floats_offset += 12;
12179
12180                   /* We can't unstack more than four registers at once.  */
12181                   if (start_reg - reg == 3)
12182                     {
12183                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
12184                                    reg, FP_REGNUM, floats_offset - vfp_offset);
12185                       start_reg = reg - 1;
12186                     }
12187                 }
12188               else
12189                 {
12190                   if (reg != start_reg)
12191                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
12192                                  reg + 1, start_reg - reg,
12193                                  FP_REGNUM, floats_offset - vfp_offset);
12194                   start_reg = reg - 1;
12195                 }
12196             }
12197
12198           /* Just in case the last register checked also needs unstacking.  */
12199           if (reg != start_reg)
12200             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
12201                          reg + 1, start_reg - reg,
12202                          FP_REGNUM, floats_offset - vfp_offset);
12203         }
12204
12205       if (TARGET_HARD_FLOAT && TARGET_VFP)
12206         {
12207           int saved_size;
12208
12209           /* The fldmd insns do not have base+offset addressing
12210              modes, so we use IP to hold the address.  */
12211           saved_size = arm_get_vfp_saved_size ();
12212
12213           if (saved_size > 0)
12214             {
12215               floats_offset += saved_size;
12216               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
12217                            FP_REGNUM, floats_offset - vfp_offset);
12218             }
12219           start_reg = FIRST_VFP_REGNUM;
12220           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
12221             {
12222               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
12223                   && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
12224                 {
12225                   if (start_reg != reg)
12226                     vfp_output_fldmd (f, IP_REGNUM,
12227                                       (start_reg - FIRST_VFP_REGNUM) / 2,
12228                                       (reg - start_reg) / 2);
12229                   start_reg = reg + 2;
12230                 }
12231             }
12232           if (start_reg != reg)
12233             vfp_output_fldmd (f, IP_REGNUM,
12234                               (start_reg - FIRST_VFP_REGNUM) / 2,
12235                               (reg - start_reg) / 2);
12236         }
12237
12238       if (TARGET_IWMMXT)
12239         {
12240           /* The frame pointer is guaranteed to be non-double-word aligned.
12241              This is because it is set to (old_stack_pointer - 4) and the
12242              old_stack_pointer was double word aligned.  Thus the offset to
12243              the iWMMXt registers to be loaded must also be non-double-word
12244              sized, so that the resultant address *is* double-word aligned.
12245              We can ignore floats_offset since that was already included in
12246              the live_regs_mask.  */
12247           lrm_count += (lrm_count % 2 ? 2 : 1);
12248
12249           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
12250             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
12251               {
12252                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
12253                              reg, FP_REGNUM, lrm_count * 4);
12254                 lrm_count += 2;
12255               }
12256         }
12257
12258       /* saved_regs_mask should contain the IP, which at the time of stack
12259          frame generation actually contains the old stack pointer.  So a
12260          quick way to unwind the stack is just pop the IP register directly
12261          into the stack pointer.  */
12262       gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
12263       saved_regs_mask &= ~ (1 << IP_REGNUM);
12264       saved_regs_mask |=   (1 << SP_REGNUM);
12265
12266       /* There are two registers left in saved_regs_mask - LR and PC.  We
12267          only need to restore the LR register (the return address), but to
12268          save time we can load it directly into the PC, unless we need a
12269          special function exit sequence, or we are not really returning.  */
12270       if (really_return
12271           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
12272           && !crtl->calls_eh_return)
12273         /* Delete the LR from the register mask, so that the LR on
12274            the stack is loaded into the PC in the register mask.  */
12275         saved_regs_mask &= ~ (1 << LR_REGNUM);
12276       else
12277         saved_regs_mask &= ~ (1 << PC_REGNUM);
12278
12279       /* We must use SP as the base register, because SP is one of the
12280          registers being restored.  If an interrupt or page fault
12281          happens in the ldm instruction, the SP might or might not
12282          have been restored.  That would be bad, as then SP will no
12283          longer indicate the safe area of stack, and we can get stack
12284          corruption.  Using SP as the base register means that it will
12285          be reset correctly to the original value, should an interrupt
12286          occur.  If the stack pointer already points at the right
12287          place, then omit the subtraction.  */
12288       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
12289           || cfun->calls_alloca)
12290         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
12291                      4 * bit_count (saved_regs_mask));
12292       print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask, 0);
12293
12294       if (IS_INTERRUPT (func_type))
12295         /* Interrupt handlers will have pushed the
12296            IP onto the stack, so restore it now.  */
12297         print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, 1 << IP_REGNUM, 0);
12298     }
12299   else
12300     {
12301       /* This branch is executed for ARM mode (non-apcs frames) and
12302          Thumb-2 mode. Frame layout is essentially the same for those
12303          cases, except that in ARM mode frame pointer points to the
12304          first saved register, while in Thumb-2 mode the frame pointer points
12305          to the last saved register.
12306
12307          It is possible to make frame pointer point to last saved
12308          register in both cases, and remove some conditionals below.
12309          That means that fp setup in prologue would be just "mov fp, sp"
12310          and sp restore in epilogue would be just "mov sp, fp", whereas
12311          now we have to use add/sub in those cases. However, the value
12312          of that would be marginal, as both mov and add/sub are 32-bit
12313          in ARM mode, and it would require extra conditionals
12314          in arm_expand_prologue to distingish ARM-apcs-frame case
12315          (where frame pointer is required to point at first register)
12316          and ARM-non-apcs-frame. Therefore, such change is postponed
12317          until real need arise.  */
12318       unsigned HOST_WIDE_INT amount;
12319       int rfe;
12320       /* Restore stack pointer if necessary.  */
12321       if (TARGET_ARM && frame_pointer_needed)
12322         {
12323           operands[0] = stack_pointer_rtx;
12324           operands[1] = hard_frame_pointer_rtx;
12325           
12326           operands[2] = GEN_INT (offsets->frame - offsets->saved_regs);
12327           output_add_immediate (operands);
12328         }
12329       else
12330         {
12331           if (frame_pointer_needed)
12332             {
12333               /* For Thumb-2 restore sp from the frame pointer.
12334                  Operand restrictions mean we have to incrememnt FP, then copy
12335                  to SP.  */
12336               amount = offsets->locals_base - offsets->saved_regs;
12337               operands[0] = hard_frame_pointer_rtx;
12338             }
12339           else
12340             {
12341               unsigned long count;
12342               operands[0] = stack_pointer_rtx;
12343               amount = offsets->outgoing_args - offsets->saved_regs;
12344               /* pop call clobbered registers if it avoids a
12345                  separate stack adjustment.  */
12346               count = offsets->saved_regs - offsets->saved_args;
12347               if (optimize_size
12348                   && count != 0
12349                   && !crtl->calls_eh_return
12350                   && bit_count(saved_regs_mask) * 4 == count
12351                   && !IS_INTERRUPT (func_type)
12352                   && !crtl->tail_call_emit)
12353                 {
12354                   unsigned long mask;
12355                   mask = (1 << (arm_size_return_regs() / 4)) - 1;
12356                   mask ^= 0xf;
12357                   mask &= ~saved_regs_mask;
12358                   reg = 0;
12359                   while (bit_count (mask) * 4 > amount)
12360                     {
12361                       while ((mask & (1 << reg)) == 0)
12362                         reg++;
12363                       mask &= ~(1 << reg);
12364                     }
12365                   if (bit_count (mask) * 4 == amount) {
12366                       amount = 0;
12367                       saved_regs_mask |= mask;
12368                   }
12369                 }
12370             }
12371           
12372           if (amount)
12373             {
12374               operands[1] = operands[0];
12375               operands[2] = GEN_INT (amount);
12376               output_add_immediate (operands);
12377             }
12378           if (frame_pointer_needed)
12379             asm_fprintf (f, "\tmov\t%r, %r\n",
12380                          SP_REGNUM, HARD_FRAME_POINTER_REGNUM);
12381         }
12382
12383       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
12384         {
12385           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
12386             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
12387               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
12388                            reg, SP_REGNUM);
12389         }
12390       else
12391         {
12392           start_reg = FIRST_FPA_REGNUM;
12393
12394           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
12395             {
12396               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
12397                 {
12398                   if (reg - start_reg == 3)
12399                     {
12400                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
12401                                    start_reg, SP_REGNUM);
12402                       start_reg = reg + 1;
12403                     }
12404                 }
12405               else
12406                 {
12407                   if (reg != start_reg)
12408                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
12409                                  start_reg, reg - start_reg,
12410                                  SP_REGNUM);
12411
12412                   start_reg = reg + 1;
12413                 }
12414             }
12415
12416           /* Just in case the last register checked also needs unstacking.  */
12417           if (reg != start_reg)
12418             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
12419                          start_reg, reg - start_reg, SP_REGNUM);
12420         }
12421
12422       if (TARGET_HARD_FLOAT && TARGET_VFP)
12423         {
12424           start_reg = FIRST_VFP_REGNUM;
12425           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
12426             {
12427               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
12428                   && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
12429                 {
12430                   if (start_reg != reg)
12431                     vfp_output_fldmd (f, SP_REGNUM,
12432                                       (start_reg - FIRST_VFP_REGNUM) / 2,
12433                                       (reg - start_reg) / 2);
12434                   start_reg = reg + 2;
12435                 }
12436             }
12437           if (start_reg != reg)
12438             vfp_output_fldmd (f, SP_REGNUM,
12439                               (start_reg - FIRST_VFP_REGNUM) / 2,
12440                               (reg - start_reg) / 2);
12441         }
12442       if (TARGET_IWMMXT)
12443         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
12444           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
12445             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
12446
12447       /* If we can, restore the LR into the PC.  */
12448       if (ARM_FUNC_TYPE (func_type) != ARM_FT_INTERWORKED
12449           && (TARGET_ARM || ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
12450           && !IS_STACKALIGN (func_type)
12451           && really_return
12452           && crtl->args.pretend_args_size == 0
12453           && saved_regs_mask & (1 << LR_REGNUM)
12454           && !crtl->calls_eh_return)
12455         {
12456           saved_regs_mask &= ~ (1 << LR_REGNUM);
12457           saved_regs_mask |=   (1 << PC_REGNUM);
12458           rfe = IS_INTERRUPT (func_type);
12459         }
12460       else
12461         rfe = 0;
12462
12463       /* Load the registers off the stack.  If we only have one register
12464          to load use the LDR instruction - it is faster.  For Thumb-2
12465          always use pop and the assembler will pick the best instruction.*/
12466       if (TARGET_ARM && saved_regs_mask == (1 << LR_REGNUM)
12467           && !IS_INTERRUPT(func_type))
12468         {
12469           asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
12470         }
12471       else if (saved_regs_mask)
12472         {
12473           if (saved_regs_mask & (1 << SP_REGNUM))
12474             /* Note - write back to the stack register is not enabled
12475                (i.e. "ldmfd sp!...").  We know that the stack pointer is
12476                in the list of registers and if we add writeback the
12477                instruction becomes UNPREDICTABLE.  */
12478             print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask,
12479                              rfe);
12480           else if (TARGET_ARM)
12481             print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, saved_regs_mask,
12482                              rfe);
12483           else
12484             print_multi_reg (f, "pop\t", SP_REGNUM, saved_regs_mask, 0);
12485         }
12486
12487       if (crtl->args.pretend_args_size)
12488         {
12489           /* Unwind the pre-pushed regs.  */
12490           operands[0] = operands[1] = stack_pointer_rtx;
12491           operands[2] = GEN_INT (crtl->args.pretend_args_size);
12492           output_add_immediate (operands);
12493         }
12494     }
12495
12496   /* We may have already restored PC directly from the stack.  */
12497   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
12498     return "";
12499
12500   /* Stack adjustment for exception handler.  */
12501   if (crtl->calls_eh_return)
12502     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
12503                  ARM_EH_STACKADJ_REGNUM);
12504
12505   /* Generate the return instruction.  */
12506   switch ((int) ARM_FUNC_TYPE (func_type))
12507     {
12508     case ARM_FT_ISR:
12509     case ARM_FT_FIQ:
12510       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
12511       break;
12512
12513     case ARM_FT_EXCEPTION:
12514       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
12515       break;
12516
12517     case ARM_FT_INTERWORKED:
12518       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
12519       break;
12520
12521     default:
12522       if (IS_STACKALIGN (func_type))
12523         {
12524           /* See comment in arm_expand_prologue.  */
12525           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, 0);
12526         }
12527       if (arm_arch5 || arm_arch4t)
12528         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
12529       else
12530         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
12531       break;
12532     }
12533
12534   return "";
12535 }
12536
12537 static void
12538 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
12539                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
12540 {
12541   arm_stack_offsets *offsets;
12542
12543   if (TARGET_THUMB1)
12544     {
12545       int regno;
12546
12547       /* Emit any call-via-reg trampolines that are needed for v4t support
12548          of call_reg and call_value_reg type insns.  */
12549       for (regno = 0; regno < LR_REGNUM; regno++)
12550         {
12551           rtx label = cfun->machine->call_via[regno];
12552
12553           if (label != NULL)
12554             {
12555               switch_to_section (function_section (current_function_decl));
12556               targetm.asm_out.internal_label (asm_out_file, "L",
12557                                               CODE_LABEL_NUMBER (label));
12558               asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
12559             }
12560         }
12561
12562       /* ??? Probably not safe to set this here, since it assumes that a
12563          function will be emitted as assembly immediately after we generate
12564          RTL for it.  This does not happen for inline functions.  */
12565       cfun->machine->return_used_this_function = 0;
12566     }
12567   else /* TARGET_32BIT */
12568     {
12569       /* We need to take into account any stack-frame rounding.  */
12570       offsets = arm_get_frame_offsets ();
12571
12572       gcc_assert (!use_return_insn (FALSE, NULL)
12573                   || (cfun->machine->return_used_this_function != 0)
12574                   || offsets->saved_regs == offsets->outgoing_args
12575                   || frame_pointer_needed);
12576
12577       /* Reset the ARM-specific per-function variables.  */
12578       after_arm_reorg = 0;
12579     }
12580 }
12581
12582 /* Generate and emit an insn that we will recognize as a push_multi.
12583    Unfortunately, since this insn does not reflect very well the actual
12584    semantics of the operation, we need to annotate the insn for the benefit
12585    of DWARF2 frame unwind information.  */
12586 static rtx
12587 emit_multi_reg_push (unsigned long mask)
12588 {
12589   int num_regs = 0;
12590   int num_dwarf_regs;
12591   int i, j;
12592   rtx par;
12593   rtx dwarf;
12594   int dwarf_par_index;
12595   rtx tmp, reg;
12596
12597   for (i = 0; i <= LAST_ARM_REGNUM; i++)
12598     if (mask & (1 << i))
12599       num_regs++;
12600
12601   gcc_assert (num_regs && num_regs <= 16);
12602
12603   /* We don't record the PC in the dwarf frame information.  */
12604   num_dwarf_regs = num_regs;
12605   if (mask & (1 << PC_REGNUM))
12606     num_dwarf_regs--;
12607
12608   /* For the body of the insn we are going to generate an UNSPEC in
12609      parallel with several USEs.  This allows the insn to be recognized
12610      by the push_multi pattern in the arm.md file.  The insn looks
12611      something like this:
12612
12613        (parallel [
12614            (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
12615                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
12616            (use (reg:SI 11 fp))
12617            (use (reg:SI 12 ip))
12618            (use (reg:SI 14 lr))
12619            (use (reg:SI 15 pc))
12620         ])
12621
12622      For the frame note however, we try to be more explicit and actually
12623      show each register being stored into the stack frame, plus a (single)
12624      decrement of the stack pointer.  We do it this way in order to be
12625      friendly to the stack unwinding code, which only wants to see a single
12626      stack decrement per instruction.  The RTL we generate for the note looks
12627      something like this:
12628
12629       (sequence [
12630            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
12631            (set (mem:SI (reg:SI sp)) (reg:SI r4))
12632            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
12633            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
12634            (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
12635         ])
12636
12637       This sequence is used both by the code to support stack unwinding for
12638       exceptions handlers and the code to generate dwarf2 frame debugging.  */
12639
12640   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
12641   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
12642   dwarf_par_index = 1;
12643
12644   for (i = 0; i <= LAST_ARM_REGNUM; i++)
12645     {
12646       if (mask & (1 << i))
12647         {
12648           reg = gen_rtx_REG (SImode, i);
12649
12650           XVECEXP (par, 0, 0)
12651             = gen_rtx_SET (VOIDmode,
12652                            gen_frame_mem (BLKmode,
12653                                           gen_rtx_PRE_DEC (BLKmode,
12654                                                            stack_pointer_rtx)),
12655                            gen_rtx_UNSPEC (BLKmode,
12656                                            gen_rtvec (1, reg),
12657                                            UNSPEC_PUSH_MULT));
12658
12659           if (i != PC_REGNUM)
12660             {
12661               tmp = gen_rtx_SET (VOIDmode,
12662                                  gen_frame_mem (SImode, stack_pointer_rtx),
12663                                  reg);
12664               RTX_FRAME_RELATED_P (tmp) = 1;
12665               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
12666               dwarf_par_index++;
12667             }
12668
12669           break;
12670         }
12671     }
12672
12673   for (j = 1, i++; j < num_regs; i++)
12674     {
12675       if (mask & (1 << i))
12676         {
12677           reg = gen_rtx_REG (SImode, i);
12678
12679           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
12680
12681           if (i != PC_REGNUM)
12682             {
12683               tmp
12684                 = gen_rtx_SET (VOIDmode,
12685                                gen_frame_mem (SImode,
12686                                               plus_constant (stack_pointer_rtx,
12687                                                              4 * j)),
12688                                reg);
12689               RTX_FRAME_RELATED_P (tmp) = 1;
12690               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
12691             }
12692
12693           j++;
12694         }
12695     }
12696
12697   par = emit_insn (par);
12698
12699   tmp = gen_rtx_SET (VOIDmode,
12700                      stack_pointer_rtx,
12701                      plus_constant (stack_pointer_rtx, -4 * num_regs));
12702   RTX_FRAME_RELATED_P (tmp) = 1;
12703   XVECEXP (dwarf, 0, 0) = tmp;
12704
12705   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
12706
12707   return par;
12708 }
12709
12710 /* Calculate the size of the return value that is passed in registers.  */
12711 static unsigned
12712 arm_size_return_regs (void)
12713 {
12714   enum machine_mode mode;
12715
12716   if (crtl->return_rtx != 0)
12717     mode = GET_MODE (crtl->return_rtx);
12718   else
12719     mode = DECL_MODE (DECL_RESULT (current_function_decl));
12720
12721   return GET_MODE_SIZE (mode);
12722 }
12723
12724 static rtx
12725 emit_sfm (int base_reg, int count)
12726 {
12727   rtx par;
12728   rtx dwarf;
12729   rtx tmp, reg;
12730   int i;
12731
12732   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
12733   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
12734
12735   reg = gen_rtx_REG (XFmode, base_reg++);
12736
12737   XVECEXP (par, 0, 0)
12738     = gen_rtx_SET (VOIDmode,
12739                    gen_frame_mem (BLKmode,
12740                                   gen_rtx_PRE_DEC (BLKmode,
12741                                                    stack_pointer_rtx)),
12742                    gen_rtx_UNSPEC (BLKmode,
12743                                    gen_rtvec (1, reg),
12744                                    UNSPEC_PUSH_MULT));
12745   tmp = gen_rtx_SET (VOIDmode,
12746                      gen_frame_mem (XFmode, stack_pointer_rtx), reg);
12747   RTX_FRAME_RELATED_P (tmp) = 1;
12748   XVECEXP (dwarf, 0, 1) = tmp;
12749
12750   for (i = 1; i < count; i++)
12751     {
12752       reg = gen_rtx_REG (XFmode, base_reg++);
12753       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
12754
12755       tmp = gen_rtx_SET (VOIDmode,
12756                          gen_frame_mem (XFmode,
12757                                         plus_constant (stack_pointer_rtx,
12758                                                        i * 12)),
12759                          reg);
12760       RTX_FRAME_RELATED_P (tmp) = 1;
12761       XVECEXP (dwarf, 0, i + 1) = tmp;
12762     }
12763
12764   tmp = gen_rtx_SET (VOIDmode,
12765                      stack_pointer_rtx,
12766                      plus_constant (stack_pointer_rtx, -12 * count));
12767
12768   RTX_FRAME_RELATED_P (tmp) = 1;
12769   XVECEXP (dwarf, 0, 0) = tmp;
12770
12771   par = emit_insn (par);
12772   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
12773
12774   return par;
12775 }
12776
12777
12778 /* Return true if the current function needs to save/restore LR.  */
12779
12780 static bool
12781 thumb_force_lr_save (void)
12782 {
12783   return !cfun->machine->lr_save_eliminated
12784          && (!leaf_function_p ()
12785              || thumb_far_jump_used_p ()
12786              || df_regs_ever_live_p (LR_REGNUM));
12787 }
12788
12789
12790 /* Compute the distance from register FROM to register TO.
12791    These can be the arg pointer (26), the soft frame pointer (25),
12792    the stack pointer (13) or the hard frame pointer (11).
12793    In thumb mode r7 is used as the soft frame pointer, if needed.
12794    Typical stack layout looks like this:
12795
12796        old stack pointer -> |    |
12797                              ----
12798                             |    | \
12799                             |    |   saved arguments for
12800                             |    |   vararg functions
12801                             |    | /
12802                               --
12803    hard FP & arg pointer -> |    | \
12804                             |    |   stack
12805                             |    |   frame
12806                             |    | /
12807                               --
12808                             |    | \
12809                             |    |   call saved
12810                             |    |   registers
12811       soft frame pointer -> |    | /
12812                               --
12813                             |    | \
12814                             |    |   local
12815                             |    |   variables
12816      locals base pointer -> |    | /
12817                               --
12818                             |    | \
12819                             |    |   outgoing
12820                             |    |   arguments
12821    current stack pointer -> |    | /
12822                               --
12823
12824   For a given function some or all of these stack components
12825   may not be needed, giving rise to the possibility of
12826   eliminating some of the registers.
12827
12828   The values returned by this function must reflect the behavior
12829   of arm_expand_prologue() and arm_compute_save_reg_mask().
12830
12831   The sign of the number returned reflects the direction of stack
12832   growth, so the values are positive for all eliminations except
12833   from the soft frame pointer to the hard frame pointer.
12834
12835   SFP may point just inside the local variables block to ensure correct
12836   alignment.  */
12837
12838
12839 /* Calculate stack offsets.  These are used to calculate register elimination
12840    offsets and in prologue/epilogue code.  Also calculates which registers
12841    should be saved.  */
12842
12843 static arm_stack_offsets *
12844 arm_get_frame_offsets (void)
12845 {
12846   struct arm_stack_offsets *offsets;
12847   unsigned long func_type;
12848   int leaf;
12849   int saved;
12850   int core_saved;
12851   HOST_WIDE_INT frame_size;
12852   int i;
12853
12854   offsets = &cfun->machine->stack_offsets;
12855
12856   /* We need to know if we are a leaf function.  Unfortunately, it
12857      is possible to be called after start_sequence has been called,
12858      which causes get_insns to return the insns for the sequence,
12859      not the function, which will cause leaf_function_p to return
12860      the incorrect result.
12861
12862      to know about leaf functions once reload has completed, and the
12863      frame size cannot be changed after that time, so we can safely
12864      use the cached value.  */
12865
12866   if (reload_completed)
12867     return offsets;
12868
12869   /* Initially this is the size of the local variables.  It will translated
12870      into an offset once we have determined the size of preceding data.  */
12871   frame_size = ROUND_UP_WORD (get_frame_size ());
12872
12873   leaf = leaf_function_p ();
12874
12875   /* Space for variadic functions.  */
12876   offsets->saved_args = crtl->args.pretend_args_size;
12877
12878   /* In Thumb mode this is incorrect, but never used.  */
12879   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0) +
12880                    arm_compute_static_chain_stack_bytes();
12881
12882   if (TARGET_32BIT)
12883     {
12884       unsigned int regno;
12885
12886       offsets->saved_regs_mask = arm_compute_save_reg_mask ();
12887       core_saved = bit_count (offsets->saved_regs_mask) * 4;
12888       saved = core_saved;
12889
12890       /* We know that SP will be doubleword aligned on entry, and we must
12891          preserve that condition at any subroutine call.  We also require the
12892          soft frame pointer to be doubleword aligned.  */
12893
12894       if (TARGET_REALLY_IWMMXT)
12895         {
12896           /* Check for the call-saved iWMMXt registers.  */
12897           for (regno = FIRST_IWMMXT_REGNUM;
12898                regno <= LAST_IWMMXT_REGNUM;
12899                regno++)
12900             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
12901               saved += 8;
12902         }
12903
12904       func_type = arm_current_func_type ();
12905       if (! IS_VOLATILE (func_type))
12906         {
12907           /* Space for saved FPA registers.  */
12908           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
12909             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
12910             saved += 12;
12911
12912           /* Space for saved VFP registers.  */
12913           if (TARGET_HARD_FLOAT && TARGET_VFP)
12914             saved += arm_get_vfp_saved_size ();
12915         }
12916     }
12917   else /* TARGET_THUMB1 */
12918     {
12919       offsets->saved_regs_mask = thumb1_compute_save_reg_mask ();
12920       core_saved = bit_count (offsets->saved_regs_mask) * 4;
12921       saved = core_saved;
12922       if (TARGET_BACKTRACE)
12923         saved += 16;
12924     }
12925
12926   /* Saved registers include the stack frame.  */
12927   offsets->saved_regs = offsets->saved_args + saved +
12928                         arm_compute_static_chain_stack_bytes();
12929   offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
12930   /* A leaf function does not need any stack alignment if it has nothing
12931      on the stack.  */
12932   if (leaf && frame_size == 0)
12933     {
12934       offsets->outgoing_args = offsets->soft_frame;
12935       offsets->locals_base = offsets->soft_frame;
12936       return offsets;
12937     }
12938
12939   /* Ensure SFP has the correct alignment.  */
12940   if (ARM_DOUBLEWORD_ALIGN
12941       && (offsets->soft_frame & 7))
12942     {
12943       offsets->soft_frame += 4;
12944       /* Try to align stack by pushing an extra reg.  Don't bother doing this
12945          when there is a stack frame as the alignment will be rolled into
12946          the normal stack adjustment.  */
12947       if (frame_size + crtl->outgoing_args_size == 0)
12948         {
12949           int reg = -1;
12950
12951           /* If it is safe to use r3, then do so.  This sometimes 
12952              generates better code on Thumb-2 by avoiding the need to
12953              use 32-bit push/pop instructions.  */
12954           if (!crtl->tail_call_emit
12955               && arm_size_return_regs () <= 12)
12956             {
12957               reg = 3;
12958             }
12959           else
12960             for (i = 4; i <= (TARGET_THUMB1 ? LAST_LO_REGNUM : 11); i++)
12961               {
12962                 if ((offsets->saved_regs_mask & (1 << i)) == 0)
12963                   {
12964                     reg = i;
12965                     break;
12966                   }
12967               }
12968
12969           if (reg != -1)
12970             {
12971               offsets->saved_regs += 4;
12972               offsets->saved_regs_mask |= (1 << reg);
12973             }
12974         }
12975     }
12976
12977   offsets->locals_base = offsets->soft_frame + frame_size;
12978   offsets->outgoing_args = (offsets->locals_base
12979                             + crtl->outgoing_args_size);
12980
12981   if (ARM_DOUBLEWORD_ALIGN)
12982     {
12983       /* Ensure SP remains doubleword aligned.  */
12984       if (offsets->outgoing_args & 7)
12985         offsets->outgoing_args += 4;
12986       gcc_assert (!(offsets->outgoing_args & 7));
12987     }
12988
12989   return offsets;
12990 }
12991
12992
12993 /* Calculate the relative offsets for the different stack pointers.  Positive
12994    offsets are in the direction of stack growth.  */
12995
12996 HOST_WIDE_INT
12997 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
12998 {
12999   arm_stack_offsets *offsets;
13000
13001   offsets = arm_get_frame_offsets ();
13002
13003   /* OK, now we have enough information to compute the distances.
13004      There must be an entry in these switch tables for each pair
13005      of registers in ELIMINABLE_REGS, even if some of the entries
13006      seem to be redundant or useless.  */
13007   switch (from)
13008     {
13009     case ARG_POINTER_REGNUM:
13010       switch (to)
13011         {
13012         case THUMB_HARD_FRAME_POINTER_REGNUM:
13013           return 0;
13014
13015         case FRAME_POINTER_REGNUM:
13016           /* This is the reverse of the soft frame pointer
13017              to hard frame pointer elimination below.  */
13018           return offsets->soft_frame - offsets->saved_args;
13019
13020         case ARM_HARD_FRAME_POINTER_REGNUM:
13021           /* This is only non-zero in the case where the static chain register
13022              is stored above the frame.  */
13023           return offsets->frame - offsets->saved_args - 4;
13024
13025         case STACK_POINTER_REGNUM:
13026           /* If nothing has been pushed on the stack at all
13027              then this will return -4.  This *is* correct!  */
13028           return offsets->outgoing_args - (offsets->saved_args + 4);
13029
13030         default:
13031           gcc_unreachable ();
13032         }
13033       gcc_unreachable ();
13034
13035     case FRAME_POINTER_REGNUM:
13036       switch (to)
13037         {
13038         case THUMB_HARD_FRAME_POINTER_REGNUM:
13039           return 0;
13040
13041         case ARM_HARD_FRAME_POINTER_REGNUM:
13042           /* The hard frame pointer points to the top entry in the
13043              stack frame.  The soft frame pointer to the bottom entry
13044              in the stack frame.  If there is no stack frame at all,
13045              then they are identical.  */
13046
13047           return offsets->frame - offsets->soft_frame;
13048
13049         case STACK_POINTER_REGNUM:
13050           return offsets->outgoing_args - offsets->soft_frame;
13051
13052         default:
13053           gcc_unreachable ();
13054         }
13055       gcc_unreachable ();
13056
13057     default:
13058       /* You cannot eliminate from the stack pointer.
13059          In theory you could eliminate from the hard frame
13060          pointer to the stack pointer, but this will never
13061          happen, since if a stack frame is not needed the
13062          hard frame pointer will never be used.  */
13063       gcc_unreachable ();
13064     }
13065 }
13066
13067
13068 /* Emit RTL to save coprocessor registers on function entry.  Returns the
13069    number of bytes pushed.  */
13070
13071 static int
13072 arm_save_coproc_regs(void)
13073 {
13074   int saved_size = 0;
13075   unsigned reg;
13076   unsigned start_reg;
13077   rtx insn;
13078
13079   for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
13080     if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
13081       {
13082         insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
13083         insn = gen_rtx_MEM (V2SImode, insn);
13084         insn = emit_set_insn (insn, gen_rtx_REG (V2SImode, reg));
13085         RTX_FRAME_RELATED_P (insn) = 1;
13086         saved_size += 8;
13087       }
13088
13089   /* Save any floating point call-saved registers used by this
13090      function.  */
13091   if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
13092     {
13093       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
13094         if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
13095           {
13096             insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
13097             insn = gen_rtx_MEM (XFmode, insn);
13098             insn = emit_set_insn (insn, gen_rtx_REG (XFmode, reg));
13099             RTX_FRAME_RELATED_P (insn) = 1;
13100             saved_size += 12;
13101           }
13102     }
13103   else
13104     {
13105       start_reg = LAST_FPA_REGNUM;
13106
13107       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
13108         {
13109           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
13110             {
13111               if (start_reg - reg == 3)
13112                 {
13113                   insn = emit_sfm (reg, 4);
13114                   RTX_FRAME_RELATED_P (insn) = 1;
13115                   saved_size += 48;
13116                   start_reg = reg - 1;
13117                 }
13118             }
13119           else
13120             {
13121               if (start_reg != reg)
13122                 {
13123                   insn = emit_sfm (reg + 1, start_reg - reg);
13124                   RTX_FRAME_RELATED_P (insn) = 1;
13125                   saved_size += (start_reg - reg) * 12;
13126                 }
13127               start_reg = reg - 1;
13128             }
13129         }
13130
13131       if (start_reg != reg)
13132         {
13133           insn = emit_sfm (reg + 1, start_reg - reg);
13134           saved_size += (start_reg - reg) * 12;
13135           RTX_FRAME_RELATED_P (insn) = 1;
13136         }
13137     }
13138   if (TARGET_HARD_FLOAT && TARGET_VFP)
13139     {
13140       start_reg = FIRST_VFP_REGNUM;
13141
13142       for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
13143         {
13144           if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
13145               && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
13146             {
13147               if (start_reg != reg)
13148                 saved_size += vfp_emit_fstmd (start_reg,
13149                                               (reg - start_reg) / 2);
13150               start_reg = reg + 2;
13151             }
13152         }
13153       if (start_reg != reg)
13154         saved_size += vfp_emit_fstmd (start_reg,
13155                                       (reg - start_reg) / 2);
13156     }
13157   return saved_size;
13158 }
13159
13160
13161 /* Set the Thumb frame pointer from the stack pointer.  */
13162
13163 static void
13164 thumb_set_frame_pointer (arm_stack_offsets *offsets)
13165 {
13166   HOST_WIDE_INT amount;
13167   rtx insn, dwarf;
13168
13169   amount = offsets->outgoing_args - offsets->locals_base;
13170   if (amount < 1024)
13171     insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
13172                                   stack_pointer_rtx, GEN_INT (amount)));
13173   else
13174     {
13175       emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
13176       /* Thumb-2 RTL patterns expect sp as the first input.  Thumb-1
13177          expects the first two operands to be the same.  */
13178       if (TARGET_THUMB2)
13179         {
13180           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
13181                                         stack_pointer_rtx,
13182                                         hard_frame_pointer_rtx));
13183         }
13184       else
13185         {
13186           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
13187                                         hard_frame_pointer_rtx,
13188                                         stack_pointer_rtx));
13189         }
13190       dwarf = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
13191                            plus_constant (stack_pointer_rtx, amount));
13192       RTX_FRAME_RELATED_P (dwarf) = 1;
13193       add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
13194     }
13195
13196   RTX_FRAME_RELATED_P (insn) = 1;
13197 }
13198
13199 /* Generate the prologue instructions for entry into an ARM or Thumb-2
13200    function.  */
13201 void
13202 arm_expand_prologue (void)
13203 {
13204   rtx amount;
13205   rtx insn;
13206   rtx ip_rtx;
13207   unsigned long live_regs_mask;
13208   unsigned long func_type;
13209   int fp_offset = 0;
13210   int saved_pretend_args = 0;
13211   int saved_regs = 0;
13212   unsigned HOST_WIDE_INT args_to_push;
13213   arm_stack_offsets *offsets;
13214
13215   func_type = arm_current_func_type ();
13216
13217   /* Naked functions don't have prologues.  */
13218   if (IS_NAKED (func_type))
13219     return;
13220
13221   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
13222   args_to_push = crtl->args.pretend_args_size;
13223
13224   /* Compute which register we will have to save onto the stack.  */
13225   offsets = arm_get_frame_offsets ();
13226   live_regs_mask = offsets->saved_regs_mask;
13227
13228   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
13229
13230   if (IS_STACKALIGN (func_type))
13231     {
13232       rtx dwarf;
13233       rtx r0;
13234       rtx r1;
13235       /* Handle a word-aligned stack pointer.  We generate the following:
13236
13237           mov r0, sp
13238           bic r1, r0, #7
13239           mov sp, r1
13240           <save and restore r0 in normal prologue/epilogue>
13241           mov sp, r0
13242           bx lr
13243
13244          The unwinder doesn't need to know about the stack realignment.
13245          Just tell it we saved SP in r0.  */
13246       gcc_assert (TARGET_THUMB2 && !arm_arch_notm && args_to_push == 0);
13247
13248       r0 = gen_rtx_REG (SImode, 0);
13249       r1 = gen_rtx_REG (SImode, 1);
13250       /* Use a real rtvec rather than NULL_RTVEC so the rest of the
13251          compiler won't choke.  */
13252       dwarf = gen_rtx_UNSPEC (SImode, rtvec_alloc (0), UNSPEC_STACK_ALIGN);
13253       dwarf = gen_rtx_SET (VOIDmode, r0, dwarf);
13254       insn = gen_movsi (r0, stack_pointer_rtx);
13255       RTX_FRAME_RELATED_P (insn) = 1;
13256       add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
13257       emit_insn (insn);
13258       emit_insn (gen_andsi3 (r1, r0, GEN_INT (~(HOST_WIDE_INT)7)));
13259       emit_insn (gen_movsi (stack_pointer_rtx, r1));
13260     }
13261
13262   /* For APCS frames, if IP register is clobbered
13263      when creating frame, save that register in a special
13264      way.  */
13265   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
13266     {
13267       if (IS_INTERRUPT (func_type))
13268         {
13269           /* Interrupt functions must not corrupt any registers.
13270              Creating a frame pointer however, corrupts the IP
13271              register, so we must push it first.  */
13272           insn = emit_multi_reg_push (1 << IP_REGNUM);
13273
13274           /* Do not set RTX_FRAME_RELATED_P on this insn.
13275              The dwarf stack unwinding code only wants to see one
13276              stack decrement per function, and this is not it.  If
13277              this instruction is labeled as being part of the frame
13278              creation sequence then dwarf2out_frame_debug_expr will
13279              die when it encounters the assignment of IP to FP
13280              later on, since the use of SP here establishes SP as
13281              the CFA register and not IP.
13282
13283              Anyway this instruction is not really part of the stack
13284              frame creation although it is part of the prologue.  */
13285         }
13286       else if (IS_NESTED (func_type))
13287         {
13288           /* The Static chain register is the same as the IP register
13289              used as a scratch register during stack frame creation.
13290              To get around this need to find somewhere to store IP
13291              whilst the frame is being created.  We try the following
13292              places in order:
13293
13294                1. The last argument register.
13295                2. A slot on the stack above the frame.  (This only
13296                   works if the function is not a varargs function).
13297                3. Register r3, after pushing the argument registers
13298                   onto the stack.
13299
13300              Note - we only need to tell the dwarf2 backend about the SP
13301              adjustment in the second variant; the static chain register
13302              doesn't need to be unwound, as it doesn't contain a value
13303              inherited from the caller.  */
13304
13305           if (df_regs_ever_live_p (3) == false)
13306             insn = emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
13307           else if (args_to_push == 0)
13308             {
13309               rtx dwarf;
13310
13311               gcc_assert(arm_compute_static_chain_stack_bytes() == 4);
13312               saved_regs += 4;
13313
13314               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
13315               insn = emit_set_insn (gen_frame_mem (SImode, insn), ip_rtx);
13316               fp_offset = 4;
13317
13318               /* Just tell the dwarf backend that we adjusted SP.  */
13319               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
13320                                    plus_constant (stack_pointer_rtx,
13321                                                   -fp_offset));
13322               RTX_FRAME_RELATED_P (insn) = 1;
13323               add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
13324             }
13325           else
13326             {
13327               /* Store the args on the stack.  */
13328               if (cfun->machine->uses_anonymous_args)
13329                 insn = emit_multi_reg_push
13330                   ((0xf0 >> (args_to_push / 4)) & 0xf);
13331               else
13332                 insn = emit_insn
13333                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13334                                GEN_INT (- args_to_push)));
13335
13336               RTX_FRAME_RELATED_P (insn) = 1;
13337
13338               saved_pretend_args = 1;
13339               fp_offset = args_to_push;
13340               args_to_push = 0;
13341
13342               /* Now reuse r3 to preserve IP.  */
13343               emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
13344             }
13345         }
13346
13347       insn = emit_set_insn (ip_rtx,
13348                             plus_constant (stack_pointer_rtx, fp_offset));
13349       RTX_FRAME_RELATED_P (insn) = 1;
13350     }
13351
13352   if (args_to_push)
13353     {
13354       /* Push the argument registers, or reserve space for them.  */
13355       if (cfun->machine->uses_anonymous_args)
13356         insn = emit_multi_reg_push
13357           ((0xf0 >> (args_to_push / 4)) & 0xf);
13358       else
13359         insn = emit_insn
13360           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13361                        GEN_INT (- args_to_push)));
13362       RTX_FRAME_RELATED_P (insn) = 1;
13363     }
13364
13365   /* If this is an interrupt service routine, and the link register
13366      is going to be pushed, and we're not generating extra
13367      push of IP (needed when frame is needed and frame layout if apcs),
13368      subtracting four from LR now will mean that the function return
13369      can be done with a single instruction.  */
13370   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
13371       && (live_regs_mask & (1 << LR_REGNUM)) != 0
13372       && !(frame_pointer_needed && TARGET_APCS_FRAME)
13373       && TARGET_ARM)
13374     {
13375       rtx lr = gen_rtx_REG (SImode, LR_REGNUM);
13376       
13377       emit_set_insn (lr, plus_constant (lr, -4));
13378     }
13379
13380   if (live_regs_mask)
13381     {
13382       saved_regs += bit_count (live_regs_mask) * 4;
13383       if (optimize_size && !frame_pointer_needed
13384           && saved_regs == offsets->saved_regs - offsets->saved_args)
13385         {
13386           /* If no coprocessor registers are being pushed and we don't have
13387              to worry about a frame pointer then push extra registers to
13388              create the stack frame.  This is done is a way that does not
13389              alter the frame layout, so is independent of the epilogue.  */
13390           int n;
13391           int frame;
13392           n = 0;
13393           while (n < 8 && (live_regs_mask & (1 << n)) == 0)
13394             n++;
13395           frame = offsets->outgoing_args - (offsets->saved_args + saved_regs);
13396           if (frame && n * 4 >= frame)
13397             {
13398               n = frame / 4;
13399               live_regs_mask |= (1 << n) - 1;
13400               saved_regs += frame;
13401             }
13402         }
13403       insn = emit_multi_reg_push (live_regs_mask);
13404       RTX_FRAME_RELATED_P (insn) = 1;
13405     }
13406
13407   if (! IS_VOLATILE (func_type))
13408     saved_regs += arm_save_coproc_regs ();
13409
13410   if (frame_pointer_needed && TARGET_ARM)
13411     {
13412       /* Create the new frame pointer.  */
13413       if (TARGET_APCS_FRAME)
13414         {
13415           insn = GEN_INT (-(4 + args_to_push + fp_offset));
13416           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
13417           RTX_FRAME_RELATED_P (insn) = 1;
13418
13419           if (IS_NESTED (func_type))
13420             {
13421               /* Recover the static chain register.  */
13422               if (!df_regs_ever_live_p (3)
13423                   || saved_pretend_args)
13424                 insn = gen_rtx_REG (SImode, 3);
13425               else /* if (crtl->args.pretend_args_size == 0) */
13426                 {
13427                   insn = plus_constant (hard_frame_pointer_rtx, 4);
13428                   insn = gen_frame_mem (SImode, insn);
13429                 }
13430               emit_set_insn (ip_rtx, insn);
13431               /* Add a USE to stop propagate_one_insn() from barfing.  */
13432               emit_insn (gen_prologue_use (ip_rtx));
13433             }
13434         }
13435       else
13436         {
13437           insn = GEN_INT (saved_regs - 4);
13438           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
13439                                         stack_pointer_rtx, insn));
13440           RTX_FRAME_RELATED_P (insn) = 1;
13441         }
13442     }
13443
13444   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
13445     {
13446       /* This add can produce multiple insns for a large constant, so we
13447          need to get tricky.  */
13448       rtx last = get_last_insn ();
13449
13450       amount = GEN_INT (offsets->saved_args + saved_regs
13451                         - offsets->outgoing_args);
13452
13453       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13454                                     amount));
13455       do
13456         {
13457           last = last ? NEXT_INSN (last) : get_insns ();
13458           RTX_FRAME_RELATED_P (last) = 1;
13459         }
13460       while (last != insn);
13461
13462       /* If the frame pointer is needed, emit a special barrier that
13463          will prevent the scheduler from moving stores to the frame
13464          before the stack adjustment.  */
13465       if (frame_pointer_needed)
13466         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
13467                                          hard_frame_pointer_rtx));
13468     }
13469
13470
13471   if (frame_pointer_needed && TARGET_THUMB2)
13472     thumb_set_frame_pointer (offsets);
13473
13474   if (flag_pic && arm_pic_register != INVALID_REGNUM)
13475     {
13476       unsigned long mask;
13477
13478       mask = live_regs_mask;
13479       mask &= THUMB2_WORK_REGS;
13480       if (!IS_NESTED (func_type))
13481         mask |= (1 << IP_REGNUM);
13482       arm_load_pic_register (mask);
13483     }
13484
13485   /* If we are profiling, make sure no instructions are scheduled before
13486      the call to mcount.  Similarly if the user has requested no
13487      scheduling in the prolog.  Similarly if we want non-call exceptions
13488      using the EABI unwinder, to prevent faulting instructions from being
13489      swapped with a stack adjustment.  */
13490   if (crtl->profile || !TARGET_SCHED_PROLOG
13491       || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
13492     emit_insn (gen_blockage ());
13493
13494   /* If the link register is being kept alive, with the return address in it,
13495      then make sure that it does not get reused by the ce2 pass.  */
13496   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
13497     cfun->machine->lr_save_eliminated = 1;
13498 }
13499 \f
13500 /* Print condition code to STREAM.  Helper function for arm_print_operand.  */
13501 static void
13502 arm_print_condition (FILE *stream)
13503 {
13504   if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
13505     {
13506       /* Branch conversion is not implemented for Thumb-2.  */
13507       if (TARGET_THUMB)
13508         {
13509           output_operand_lossage ("predicated Thumb instruction");
13510           return;
13511         }
13512       if (current_insn_predicate != NULL)
13513         {
13514           output_operand_lossage
13515             ("predicated instruction in conditional sequence");
13516           return;
13517         }
13518
13519       fputs (arm_condition_codes[arm_current_cc], stream);
13520     }
13521   else if (current_insn_predicate)
13522     {
13523       enum arm_cond_code code;
13524
13525       if (TARGET_THUMB1)
13526         {
13527           output_operand_lossage ("predicated Thumb instruction");
13528           return;
13529         }
13530
13531       code = get_arm_condition_code (current_insn_predicate);
13532       fputs (arm_condition_codes[code], stream);
13533     }
13534 }
13535
13536
13537 /* If CODE is 'd', then the X is a condition operand and the instruction
13538    should only be executed if the condition is true.
13539    if CODE is 'D', then the X is a condition operand and the instruction
13540    should only be executed if the condition is false: however, if the mode
13541    of the comparison is CCFPEmode, then always execute the instruction -- we
13542    do this because in these circumstances !GE does not necessarily imply LT;
13543    in these cases the instruction pattern will take care to make sure that
13544    an instruction containing %d will follow, thereby undoing the effects of
13545    doing this instruction unconditionally.
13546    If CODE is 'N' then X is a floating point operand that must be negated
13547    before output.
13548    If CODE is 'B' then output a bitwise inverted value of X (a const int).
13549    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
13550 void
13551 arm_print_operand (FILE *stream, rtx x, int code)
13552 {
13553   switch (code)
13554     {
13555     case '@':
13556       fputs (ASM_COMMENT_START, stream);
13557       return;
13558
13559     case '_':
13560       fputs (user_label_prefix, stream);
13561       return;
13562
13563     case '|':
13564       fputs (REGISTER_PREFIX, stream);
13565       return;
13566
13567     case '?':
13568       arm_print_condition (stream);
13569       return;
13570
13571     case '(':
13572       /* Nothing in unified syntax, otherwise the current condition code.  */
13573       if (!TARGET_UNIFIED_ASM)
13574         arm_print_condition (stream);
13575       break;
13576
13577     case ')':
13578       /* The current condition code in unified syntax, otherwise nothing.  */
13579       if (TARGET_UNIFIED_ASM)
13580         arm_print_condition (stream);
13581       break;
13582   
13583     case '.':
13584       /* The current condition code for a condition code setting instruction.
13585          Preceded by 's' in unified syntax, otherwise followed by 's'.  */
13586       if (TARGET_UNIFIED_ASM)
13587         {
13588           fputc('s', stream);
13589           arm_print_condition (stream);
13590         }
13591       else
13592         {
13593           arm_print_condition (stream);
13594           fputc('s', stream);
13595         }
13596       return;
13597
13598     case '!':
13599       /* If the instruction is conditionally executed then print
13600          the current condition code, otherwise print 's'.  */
13601       gcc_assert (TARGET_THUMB2 && TARGET_UNIFIED_ASM);
13602       if (current_insn_predicate)
13603         arm_print_condition (stream);
13604       else
13605         fputc('s', stream);
13606       break;
13607
13608     /* %# is a "break" sequence. It doesn't output anything, but is used to
13609        separate e.g. operand numbers from following text, if that text consists
13610        of further digits which we don't want to be part of the operand
13611        number.  */
13612     case '#':
13613       return;
13614
13615     case 'N':
13616       {
13617         REAL_VALUE_TYPE r;
13618         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
13619         r = REAL_VALUE_NEGATE (r);
13620         fprintf (stream, "%s", fp_const_from_val (&r));
13621       }
13622       return;
13623
13624     /* An integer or symbol address without a preceding # sign.  */
13625     case 'c':
13626       switch (GET_CODE (x))
13627         {
13628         case CONST_INT:
13629           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
13630           break;
13631
13632         case SYMBOL_REF:
13633           output_addr_const (stream, x);
13634           break;
13635
13636         default:
13637           gcc_unreachable ();
13638         }
13639       return;
13640
13641     case 'B':
13642       if (GET_CODE (x) == CONST_INT)
13643         {
13644           HOST_WIDE_INT val;
13645           val = ARM_SIGN_EXTEND (~INTVAL (x));
13646           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
13647         }
13648       else
13649         {
13650           putc ('~', stream);
13651           output_addr_const (stream, x);
13652         }
13653       return;
13654
13655     case 'L':
13656       /* The low 16 bits of an immediate constant.  */
13657       fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL(x) & 0xffff);
13658       return;
13659
13660     case 'i':
13661       fprintf (stream, "%s", arithmetic_instr (x, 1));
13662       return;
13663
13664     /* Truncate Cirrus shift counts.  */
13665     case 's':
13666       if (GET_CODE (x) == CONST_INT)
13667         {
13668           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
13669           return;
13670         }
13671       arm_print_operand (stream, x, 0);
13672       return;
13673
13674     case 'I':
13675       fprintf (stream, "%s", arithmetic_instr (x, 0));
13676       return;
13677
13678     case 'S':
13679       {
13680         HOST_WIDE_INT val;
13681         const char *shift;
13682
13683         if (!shift_operator (x, SImode))
13684           {
13685             output_operand_lossage ("invalid shift operand");
13686             break;
13687           }
13688
13689         shift = shift_op (x, &val);
13690
13691         if (shift)
13692           {
13693             fprintf (stream, ", %s ", shift);
13694             if (val == -1)
13695               arm_print_operand (stream, XEXP (x, 1), 0);
13696             else
13697               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
13698           }
13699       }
13700       return;
13701
13702       /* An explanation of the 'Q', 'R' and 'H' register operands:
13703
13704          In a pair of registers containing a DI or DF value the 'Q'
13705          operand returns the register number of the register containing
13706          the least significant part of the value.  The 'R' operand returns
13707          the register number of the register containing the most
13708          significant part of the value.
13709
13710          The 'H' operand returns the higher of the two register numbers.
13711          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
13712          same as the 'Q' operand, since the most significant part of the
13713          value is held in the lower number register.  The reverse is true
13714          on systems where WORDS_BIG_ENDIAN is false.
13715
13716          The purpose of these operands is to distinguish between cases
13717          where the endian-ness of the values is important (for example
13718          when they are added together), and cases where the endian-ness
13719          is irrelevant, but the order of register operations is important.
13720          For example when loading a value from memory into a register
13721          pair, the endian-ness does not matter.  Provided that the value
13722          from the lower memory address is put into the lower numbered
13723          register, and the value from the higher address is put into the
13724          higher numbered register, the load will work regardless of whether
13725          the value being loaded is big-wordian or little-wordian.  The
13726          order of the two register loads can matter however, if the address
13727          of the memory location is actually held in one of the registers
13728          being overwritten by the load.  */
13729     case 'Q':
13730       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13731         {
13732           output_operand_lossage ("invalid operand for code '%c'", code);
13733           return;
13734         }
13735
13736       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
13737       return;
13738
13739     case 'R':
13740       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13741         {
13742           output_operand_lossage ("invalid operand for code '%c'", code);
13743           return;
13744         }
13745
13746       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
13747       return;
13748
13749     case 'H':
13750       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13751         {
13752           output_operand_lossage ("invalid operand for code '%c'", code);
13753           return;
13754         }
13755
13756       asm_fprintf (stream, "%r", REGNO (x) + 1);
13757       return;
13758
13759     case 'J':
13760       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13761         {
13762           output_operand_lossage ("invalid operand for code '%c'", code);
13763           return;
13764         }
13765
13766       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 3 : 2));
13767       return;
13768
13769     case 'K':
13770       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13771         {
13772           output_operand_lossage ("invalid operand for code '%c'", code);
13773           return;
13774         }
13775
13776       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 2 : 3));
13777       return;
13778
13779     case 'm':
13780       asm_fprintf (stream, "%r",
13781                    GET_CODE (XEXP (x, 0)) == REG
13782                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
13783       return;
13784
13785     case 'M':
13786       asm_fprintf (stream, "{%r-%r}",
13787                    REGNO (x),
13788                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
13789       return;
13790
13791     /* Like 'M', but writing doubleword vector registers, for use by Neon
13792        insns.  */
13793     case 'h':
13794       {
13795         int regno = (REGNO (x) - FIRST_VFP_REGNUM) / 2;
13796         int numregs = ARM_NUM_REGS (GET_MODE (x)) / 2;
13797         if (numregs == 1)
13798           asm_fprintf (stream, "{d%d}", regno);
13799         else
13800           asm_fprintf (stream, "{d%d-d%d}", regno, regno + numregs - 1);
13801       }
13802       return;
13803
13804     case 'd':
13805       /* CONST_TRUE_RTX means always -- that's the default.  */
13806       if (x == const_true_rtx)
13807         return;
13808
13809       if (!COMPARISON_P (x))
13810         {
13811           output_operand_lossage ("invalid operand for code '%c'", code);
13812           return;
13813         }
13814
13815       fputs (arm_condition_codes[get_arm_condition_code (x)],
13816              stream);
13817       return;
13818
13819     case 'D':
13820       /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
13821          want to do that.  */
13822       if (x == const_true_rtx)
13823         {
13824           output_operand_lossage ("instruction never executed");
13825           return;
13826         }
13827       if (!COMPARISON_P (x))
13828         {
13829           output_operand_lossage ("invalid operand for code '%c'", code);
13830           return;
13831         }
13832
13833       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
13834                                  (get_arm_condition_code (x))],
13835              stream);
13836       return;
13837
13838     /* Cirrus registers can be accessed in a variety of ways:
13839          single floating point (f)
13840          double floating point (d)
13841          32bit integer         (fx)
13842          64bit integer         (dx).  */
13843     case 'W':                   /* Cirrus register in F mode.  */
13844     case 'X':                   /* Cirrus register in D mode.  */
13845     case 'Y':                   /* Cirrus register in FX mode.  */
13846     case 'Z':                   /* Cirrus register in DX mode.  */
13847       gcc_assert (GET_CODE (x) == REG
13848                   && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
13849
13850       fprintf (stream, "mv%s%s",
13851                code == 'W' ? "f"
13852                : code == 'X' ? "d"
13853                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
13854
13855       return;
13856
13857     /* Print cirrus register in the mode specified by the register's mode.  */
13858     case 'V':
13859       {
13860         int mode = GET_MODE (x);
13861
13862         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
13863           {
13864             output_operand_lossage ("invalid operand for code '%c'", code);
13865             return;
13866           }
13867
13868         fprintf (stream, "mv%s%s",
13869                  mode == DFmode ? "d"
13870                  : mode == SImode ? "fx"
13871                  : mode == DImode ? "dx"
13872                  : "f", reg_names[REGNO (x)] + 2);
13873
13874         return;
13875       }
13876
13877     case 'U':
13878       if (GET_CODE (x) != REG
13879           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
13880           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
13881         /* Bad value for wCG register number.  */
13882         {
13883           output_operand_lossage ("invalid operand for code '%c'", code);
13884           return;
13885         }
13886
13887       else
13888         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
13889       return;
13890
13891       /* Print an iWMMXt control register name.  */
13892     case 'w':
13893       if (GET_CODE (x) != CONST_INT
13894           || INTVAL (x) < 0
13895           || INTVAL (x) >= 16)
13896         /* Bad value for wC register number.  */
13897         {
13898           output_operand_lossage ("invalid operand for code '%c'", code);
13899           return;
13900         }
13901
13902       else
13903         {
13904           static const char * wc_reg_names [16] =
13905             {
13906               "wCID",  "wCon",  "wCSSF", "wCASF",
13907               "wC4",   "wC5",   "wC6",   "wC7",
13908               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
13909               "wC12",  "wC13",  "wC14",  "wC15"
13910             };
13911
13912           fprintf (stream, wc_reg_names [INTVAL (x)]);
13913         }
13914       return;
13915
13916     /* Print a VFP/Neon double precision or quad precision register name.  */
13917     case 'P':
13918     case 'q':
13919       {
13920         int mode = GET_MODE (x);
13921         int is_quad = (code == 'q');
13922         int regno;
13923
13924         if (GET_MODE_SIZE (mode) != (is_quad ? 16 : 8))
13925           {
13926             output_operand_lossage ("invalid operand for code '%c'", code);
13927             return;
13928           }
13929
13930         if (GET_CODE (x) != REG
13931             || !IS_VFP_REGNUM (REGNO (x)))
13932           {
13933             output_operand_lossage ("invalid operand for code '%c'", code);
13934             return;
13935           }
13936
13937         regno = REGNO (x);
13938         if ((is_quad && !NEON_REGNO_OK_FOR_QUAD (regno))
13939             || (!is_quad && !VFP_REGNO_OK_FOR_DOUBLE (regno)))
13940           {
13941             output_operand_lossage ("invalid operand for code '%c'", code);
13942             return;
13943           }
13944
13945         fprintf (stream, "%c%d", is_quad ? 'q' : 'd',
13946           (regno - FIRST_VFP_REGNUM) >> (is_quad ? 2 : 1));
13947       }
13948       return;
13949
13950     /* These two codes print the low/high doubleword register of a Neon quad
13951        register, respectively.  For pair-structure types, can also print
13952        low/high quadword registers.  */
13953     case 'e':
13954     case 'f':
13955       {
13956         int mode = GET_MODE (x);
13957         int regno;
13958
13959         if ((GET_MODE_SIZE (mode) != 16
13960              && GET_MODE_SIZE (mode) != 32) || GET_CODE (x) != REG)
13961           {
13962             output_operand_lossage ("invalid operand for code '%c'", code);
13963             return;
13964           }
13965
13966         regno = REGNO (x);
13967         if (!NEON_REGNO_OK_FOR_QUAD (regno))
13968           {
13969             output_operand_lossage ("invalid operand for code '%c'", code);
13970             return;
13971           }
13972
13973         if (GET_MODE_SIZE (mode) == 16)
13974           fprintf (stream, "d%d", ((regno - FIRST_VFP_REGNUM) >> 1)
13975                                   + (code == 'f' ? 1 : 0));
13976         else
13977           fprintf (stream, "q%d", ((regno - FIRST_VFP_REGNUM) >> 2)
13978                                   + (code == 'f' ? 1 : 0));
13979       }
13980       return;
13981
13982     /* Print a VFPv3 floating-point constant, represented as an integer
13983        index.  */
13984     case 'G':
13985       {
13986         int index = vfp3_const_double_index (x);
13987         gcc_assert (index != -1);
13988         fprintf (stream, "%d", index);
13989       }
13990       return;
13991
13992     /* Print bits representing opcode features for Neon.
13993
13994        Bit 0 is 1 for signed, 0 for unsigned.  Floats count as signed
13995        and polynomials as unsigned.
13996
13997        Bit 1 is 1 for floats and polynomials, 0 for ordinary integers.
13998
13999        Bit 2 is 1 for rounding functions, 0 otherwise.  */
14000
14001     /* Identify the type as 's', 'u', 'p' or 'f'.  */
14002     case 'T':
14003       {
14004         HOST_WIDE_INT bits = INTVAL (x);
14005         fputc ("uspf"[bits & 3], stream);
14006       }
14007       return;
14008
14009     /* Likewise, but signed and unsigned integers are both 'i'.  */
14010     case 'F':
14011       {
14012         HOST_WIDE_INT bits = INTVAL (x);
14013         fputc ("iipf"[bits & 3], stream);
14014       }
14015       return;
14016
14017     /* As for 'T', but emit 'u' instead of 'p'.  */
14018     case 't':
14019       {
14020         HOST_WIDE_INT bits = INTVAL (x);
14021         fputc ("usuf"[bits & 3], stream);
14022       }
14023       return;
14024
14025     /* Bit 2: rounding (vs none).  */
14026     case 'O':
14027       {
14028         HOST_WIDE_INT bits = INTVAL (x);
14029         fputs ((bits & 4) != 0 ? "r" : "", stream);
14030       }
14031       return;
14032
14033     /* Memory operand for vld1/vst1 instruction.  */
14034     case 'A':
14035       {
14036         rtx addr;
14037         bool postinc = FALSE;
14038         gcc_assert (GET_CODE (x) == MEM);
14039         addr = XEXP (x, 0);
14040         if (GET_CODE (addr) == POST_INC)
14041           {
14042             postinc = 1;
14043             addr = XEXP (addr, 0);
14044           }
14045         asm_fprintf (stream, "[%r]", REGNO (addr));
14046         if (postinc)
14047           fputs("!", stream);
14048       }
14049       return;
14050
14051     /* Register specifier for vld1.16/vst1.16.  Translate the S register
14052        number into a D register number and element index.  */
14053     case 'z':
14054       {
14055         int mode = GET_MODE (x);
14056         int regno;
14057
14058         if (GET_MODE_SIZE (mode) != 2 || GET_CODE (x) != REG)
14059           {
14060             output_operand_lossage ("invalid operand for code '%c'", code);
14061             return;
14062           }
14063
14064         regno = REGNO (x);
14065         if (!VFP_REGNO_OK_FOR_SINGLE (regno))
14066           {
14067             output_operand_lossage ("invalid operand for code '%c'", code);
14068             return;
14069           }
14070
14071         regno = regno - FIRST_VFP_REGNUM;
14072         fprintf (stream, "d%d[%d]", regno/2, ((regno % 2) ? 2 : 0));
14073       }
14074       return;
14075       
14076     default:
14077       if (x == 0)
14078         {
14079           output_operand_lossage ("missing operand");
14080           return;
14081         }
14082
14083       switch (GET_CODE (x))
14084         {
14085         case REG:
14086           asm_fprintf (stream, "%r", REGNO (x));
14087           break;
14088
14089         case MEM:
14090           output_memory_reference_mode = GET_MODE (x);
14091           output_address (XEXP (x, 0));
14092           break;
14093
14094         case CONST_DOUBLE:
14095           if (TARGET_NEON)
14096             {
14097               char fpstr[20];
14098               real_to_decimal (fpstr, CONST_DOUBLE_REAL_VALUE (x),
14099                                sizeof (fpstr), 0, 1);
14100               fprintf (stream, "#%s", fpstr);
14101             }
14102           else
14103             fprintf (stream, "#%s", fp_immediate_constant (x));
14104           break;
14105
14106         default:
14107           gcc_assert (GET_CODE (x) != NEG);
14108           fputc ('#', stream);
14109           if (GET_CODE (x) == HIGH)
14110             {
14111               fputs (":lower16:", stream);
14112               x = XEXP (x, 0);
14113             }
14114             
14115           output_addr_const (stream, x);
14116           break;
14117         }
14118     }
14119 }
14120 \f
14121 /* Target hook for assembling integer objects.  The ARM version needs to
14122    handle word-sized values specially.  */
14123 static bool
14124 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
14125 {
14126   enum machine_mode mode;
14127
14128   if (size == UNITS_PER_WORD && aligned_p)
14129     {
14130       fputs ("\t.word\t", asm_out_file);
14131       output_addr_const (asm_out_file, x);
14132
14133       /* Mark symbols as position independent.  We only do this in the
14134          .text segment, not in the .data segment.  */
14135       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
14136           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
14137         {
14138           /* See legitimize_pic_address for an explanation of the
14139              TARGET_VXWORKS_RTP check.  */
14140           if (TARGET_VXWORKS_RTP
14141               || (GET_CODE (x) == SYMBOL_REF && !SYMBOL_REF_LOCAL_P (x)))
14142             fputs ("(GOT)", asm_out_file);
14143           else
14144             fputs ("(GOTOFF)", asm_out_file);
14145         }
14146       fputc ('\n', asm_out_file);
14147       return true;
14148     }
14149
14150   mode = GET_MODE (x);
14151
14152   if (arm_vector_mode_supported_p (mode))
14153     {
14154       int i, units;
14155
14156       gcc_assert (GET_CODE (x) == CONST_VECTOR);
14157
14158       units = CONST_VECTOR_NUNITS (x);
14159       size = GET_MODE_SIZE (GET_MODE_INNER (mode));
14160
14161       if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
14162         for (i = 0; i < units; i++)
14163           {
14164             rtx elt = CONST_VECTOR_ELT (x, i);
14165             assemble_integer
14166               (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
14167           }
14168       else
14169         for (i = 0; i < units; i++)
14170           {
14171             rtx elt = CONST_VECTOR_ELT (x, i);
14172             REAL_VALUE_TYPE rval;
14173
14174             REAL_VALUE_FROM_CONST_DOUBLE (rval, elt);
14175
14176             assemble_real
14177               (rval, GET_MODE_INNER (mode),
14178               i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT);
14179           }
14180
14181       return true;
14182     }
14183
14184   return default_assemble_integer (x, size, aligned_p);
14185 }
14186
14187 static void
14188 arm_elf_asm_cdtor (rtx symbol, int priority, bool is_ctor)
14189 {
14190   section *s;
14191
14192   if (!TARGET_AAPCS_BASED)
14193     {
14194       (is_ctor ? 
14195        default_named_section_asm_out_constructor 
14196        : default_named_section_asm_out_destructor) (symbol, priority);
14197       return;
14198     }
14199
14200   /* Put these in the .init_array section, using a special relocation.  */
14201   if (priority != DEFAULT_INIT_PRIORITY)
14202     {
14203       char buf[18];
14204       sprintf (buf, "%s.%.5u", 
14205                is_ctor ? ".init_array" : ".fini_array",
14206                priority);
14207       s = get_section (buf, SECTION_WRITE, NULL_TREE);
14208     }
14209   else if (is_ctor)
14210     s = ctors_section;
14211   else
14212     s = dtors_section;
14213
14214   switch_to_section (s);
14215   assemble_align (POINTER_SIZE);
14216   fputs ("\t.word\t", asm_out_file);
14217   output_addr_const (asm_out_file, symbol);
14218   fputs ("(target1)\n", asm_out_file);
14219 }
14220
14221 /* Add a function to the list of static constructors.  */
14222
14223 static void
14224 arm_elf_asm_constructor (rtx symbol, int priority)
14225 {
14226   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/true);
14227 }
14228
14229 /* Add a function to the list of static destructors.  */
14230
14231 static void
14232 arm_elf_asm_destructor (rtx symbol, int priority)
14233 {
14234   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/false);
14235 }
14236 \f
14237 /* A finite state machine takes care of noticing whether or not instructions
14238    can be conditionally executed, and thus decrease execution time and code
14239    size by deleting branch instructions.  The fsm is controlled by
14240    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
14241
14242 /* The state of the fsm controlling condition codes are:
14243    0: normal, do nothing special
14244    1: make ASM_OUTPUT_OPCODE not output this instruction
14245    2: make ASM_OUTPUT_OPCODE not output this instruction
14246    3: make instructions conditional
14247    4: make instructions conditional
14248
14249    State transitions (state->state by whom under condition):
14250    0 -> 1 final_prescan_insn if the `target' is a label
14251    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
14252    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
14253    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
14254    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
14255           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
14256    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
14257           (the target insn is arm_target_insn).
14258
14259    If the jump clobbers the conditions then we use states 2 and 4.
14260
14261    A similar thing can be done with conditional return insns.
14262
14263    XXX In case the `target' is an unconditional branch, this conditionalising
14264    of the instructions always reduces code size, but not always execution
14265    time.  But then, I want to reduce the code size to somewhere near what
14266    /bin/cc produces.  */
14267
14268 /* In addition to this, state is maintained for Thumb-2 COND_EXEC
14269    instructions.  When a COND_EXEC instruction is seen the subsequent
14270    instructions are scanned so that multiple conditional instructions can be
14271    combined into a single IT block.  arm_condexec_count and arm_condexec_mask
14272    specify the length and true/false mask for the IT block.  These will be
14273    decremented/zeroed by arm_asm_output_opcode as the insns are output.  */
14274
14275 /* Returns the index of the ARM condition code string in
14276    `arm_condition_codes'.  COMPARISON should be an rtx like
14277    `(eq (...) (...))'.  */
14278 static enum arm_cond_code
14279 get_arm_condition_code (rtx comparison)
14280 {
14281   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
14282   enum arm_cond_code code;
14283   enum rtx_code comp_code = GET_CODE (comparison);
14284
14285   if (GET_MODE_CLASS (mode) != MODE_CC)
14286     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
14287                            XEXP (comparison, 1));
14288
14289   switch (mode)
14290     {
14291     case CC_DNEmode: code = ARM_NE; goto dominance;
14292     case CC_DEQmode: code = ARM_EQ; goto dominance;
14293     case CC_DGEmode: code = ARM_GE; goto dominance;
14294     case CC_DGTmode: code = ARM_GT; goto dominance;
14295     case CC_DLEmode: code = ARM_LE; goto dominance;
14296     case CC_DLTmode: code = ARM_LT; goto dominance;
14297     case CC_DGEUmode: code = ARM_CS; goto dominance;
14298     case CC_DGTUmode: code = ARM_HI; goto dominance;
14299     case CC_DLEUmode: code = ARM_LS; goto dominance;
14300     case CC_DLTUmode: code = ARM_CC;
14301
14302     dominance:
14303       gcc_assert (comp_code == EQ || comp_code == NE);
14304
14305       if (comp_code == EQ)
14306         return ARM_INVERSE_CONDITION_CODE (code);
14307       return code;
14308
14309     case CC_NOOVmode:
14310       switch (comp_code)
14311         {
14312         case NE: return ARM_NE;
14313         case EQ: return ARM_EQ;
14314         case GE: return ARM_PL;
14315         case LT: return ARM_MI;
14316         default: gcc_unreachable ();
14317         }
14318
14319     case CC_Zmode:
14320       switch (comp_code)
14321         {
14322         case NE: return ARM_NE;
14323         case EQ: return ARM_EQ;
14324         default: gcc_unreachable ();
14325         }
14326
14327     case CC_Nmode:
14328       switch (comp_code)
14329         {
14330         case NE: return ARM_MI;
14331         case EQ: return ARM_PL;
14332         default: gcc_unreachable ();
14333         }
14334
14335     case CCFPEmode:
14336     case CCFPmode:
14337       /* These encodings assume that AC=1 in the FPA system control
14338          byte.  This allows us to handle all cases except UNEQ and
14339          LTGT.  */
14340       switch (comp_code)
14341         {
14342         case GE: return ARM_GE;
14343         case GT: return ARM_GT;
14344         case LE: return ARM_LS;
14345         case LT: return ARM_MI;
14346         case NE: return ARM_NE;
14347         case EQ: return ARM_EQ;
14348         case ORDERED: return ARM_VC;
14349         case UNORDERED: return ARM_VS;
14350         case UNLT: return ARM_LT;
14351         case UNLE: return ARM_LE;
14352         case UNGT: return ARM_HI;
14353         case UNGE: return ARM_PL;
14354           /* UNEQ and LTGT do not have a representation.  */
14355         case UNEQ: /* Fall through.  */
14356         case LTGT: /* Fall through.  */
14357         default: gcc_unreachable ();
14358         }
14359
14360     case CC_SWPmode:
14361       switch (comp_code)
14362         {
14363         case NE: return ARM_NE;
14364         case EQ: return ARM_EQ;
14365         case GE: return ARM_LE;
14366         case GT: return ARM_LT;
14367         case LE: return ARM_GE;
14368         case LT: return ARM_GT;
14369         case GEU: return ARM_LS;
14370         case GTU: return ARM_CC;
14371         case LEU: return ARM_CS;
14372         case LTU: return ARM_HI;
14373         default: gcc_unreachable ();
14374         }
14375
14376     case CC_Cmode:
14377       switch (comp_code)
14378       {
14379       case LTU: return ARM_CS;
14380       case GEU: return ARM_CC;
14381       default: gcc_unreachable ();
14382       }
14383
14384     case CCmode:
14385       switch (comp_code)
14386         {
14387         case NE: return ARM_NE;
14388         case EQ: return ARM_EQ;
14389         case GE: return ARM_GE;
14390         case GT: return ARM_GT;
14391         case LE: return ARM_LE;
14392         case LT: return ARM_LT;
14393         case GEU: return ARM_CS;
14394         case GTU: return ARM_HI;
14395         case LEU: return ARM_LS;
14396         case LTU: return ARM_CC;
14397         default: gcc_unreachable ();
14398         }
14399
14400     default: gcc_unreachable ();
14401     }
14402 }
14403
14404 /* Tell arm_asm_output_opcode to output IT blocks for conditionally executed
14405    instructions.  */
14406 void
14407 thumb2_final_prescan_insn (rtx insn)
14408 {
14409   rtx first_insn = insn;
14410   rtx body = PATTERN (insn);
14411   rtx predicate;
14412   enum arm_cond_code code;
14413   int n;
14414   int mask;
14415
14416   /* Remove the previous insn from the count of insns to be output.  */
14417   if (arm_condexec_count)
14418       arm_condexec_count--;
14419
14420   /* Nothing to do if we are already inside a conditional block.  */
14421   if (arm_condexec_count)
14422     return;
14423
14424   if (GET_CODE (body) != COND_EXEC)
14425     return;
14426
14427   /* Conditional jumps are implemented directly.  */
14428   if (GET_CODE (insn) == JUMP_INSN)
14429     return;
14430
14431   predicate = COND_EXEC_TEST (body);
14432   arm_current_cc = get_arm_condition_code (predicate);
14433
14434   n = get_attr_ce_count (insn);
14435   arm_condexec_count = 1;
14436   arm_condexec_mask = (1 << n) - 1;
14437   arm_condexec_masklen = n;
14438   /* See if subsequent instructions can be combined into the same block.  */
14439   for (;;)
14440     {
14441       insn = next_nonnote_insn (insn);
14442
14443       /* Jumping into the middle of an IT block is illegal, so a label or
14444          barrier terminates the block.  */
14445       if (GET_CODE (insn) != INSN && GET_CODE(insn) != JUMP_INSN)
14446         break;
14447
14448       body = PATTERN (insn);
14449       /* USE and CLOBBER aren't really insns, so just skip them.  */
14450       if (GET_CODE (body) == USE
14451           || GET_CODE (body) == CLOBBER)
14452         continue;
14453
14454       /* ??? Recognize conditional jumps, and combine them with IT blocks.  */
14455       if (GET_CODE (body) != COND_EXEC)
14456         break;
14457       /* Allow up to 4 conditionally executed instructions in a block.  */
14458       n = get_attr_ce_count (insn);
14459       if (arm_condexec_masklen + n > 4)
14460         break;
14461
14462       predicate = COND_EXEC_TEST (body);
14463       code = get_arm_condition_code (predicate);
14464       mask = (1 << n) - 1;
14465       if (arm_current_cc == code)
14466         arm_condexec_mask |= (mask << arm_condexec_masklen);
14467       else if (arm_current_cc != ARM_INVERSE_CONDITION_CODE(code))
14468         break;
14469
14470       arm_condexec_count++;
14471       arm_condexec_masklen += n;
14472
14473       /* A jump must be the last instruction in a conditional block.  */
14474       if (GET_CODE(insn) == JUMP_INSN)
14475         break;
14476     }
14477   /* Restore recog_data (getting the attributes of other insns can
14478      destroy this array, but final.c assumes that it remains intact
14479      across this call).  */
14480   extract_constrain_insn_cached (first_insn);
14481 }
14482
14483 void
14484 arm_final_prescan_insn (rtx insn)
14485 {
14486   /* BODY will hold the body of INSN.  */
14487   rtx body = PATTERN (insn);
14488
14489   /* This will be 1 if trying to repeat the trick, and things need to be
14490      reversed if it appears to fail.  */
14491   int reverse = 0;
14492
14493   /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
14494      taken are clobbered, even if the rtl suggests otherwise.  It also
14495      means that we have to grub around within the jump expression to find
14496      out what the conditions are when the jump isn't taken.  */
14497   int jump_clobbers = 0;
14498
14499   /* If we start with a return insn, we only succeed if we find another one.  */
14500   int seeking_return = 0;
14501
14502   /* START_INSN will hold the insn from where we start looking.  This is the
14503      first insn after the following code_label if REVERSE is true.  */
14504   rtx start_insn = insn;
14505
14506   /* If in state 4, check if the target branch is reached, in order to
14507      change back to state 0.  */
14508   if (arm_ccfsm_state == 4)
14509     {
14510       if (insn == arm_target_insn)
14511         {
14512           arm_target_insn = NULL;
14513           arm_ccfsm_state = 0;
14514         }
14515       return;
14516     }
14517
14518   /* If in state 3, it is possible to repeat the trick, if this insn is an
14519      unconditional branch to a label, and immediately following this branch
14520      is the previous target label which is only used once, and the label this
14521      branch jumps to is not too far off.  */
14522   if (arm_ccfsm_state == 3)
14523     {
14524       if (simplejump_p (insn))
14525         {
14526           start_insn = next_nonnote_insn (start_insn);
14527           if (GET_CODE (start_insn) == BARRIER)
14528             {
14529               /* XXX Isn't this always a barrier?  */
14530               start_insn = next_nonnote_insn (start_insn);
14531             }
14532           if (GET_CODE (start_insn) == CODE_LABEL
14533               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
14534               && LABEL_NUSES (start_insn) == 1)
14535             reverse = TRUE;
14536           else
14537             return;
14538         }
14539       else if (GET_CODE (body) == RETURN)
14540         {
14541           start_insn = next_nonnote_insn (start_insn);
14542           if (GET_CODE (start_insn) == BARRIER)
14543             start_insn = next_nonnote_insn (start_insn);
14544           if (GET_CODE (start_insn) == CODE_LABEL
14545               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
14546               && LABEL_NUSES (start_insn) == 1)
14547             {
14548               reverse = TRUE;
14549               seeking_return = 1;
14550             }
14551           else
14552             return;
14553         }
14554       else
14555         return;
14556     }
14557
14558   gcc_assert (!arm_ccfsm_state || reverse);
14559   if (GET_CODE (insn) != JUMP_INSN)
14560     return;
14561
14562   /* This jump might be paralleled with a clobber of the condition codes
14563      the jump should always come first */
14564   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
14565     body = XVECEXP (body, 0, 0);
14566
14567   if (reverse
14568       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
14569           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
14570     {
14571       int insns_skipped;
14572       int fail = FALSE, succeed = FALSE;
14573       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
14574       int then_not_else = TRUE;
14575       rtx this_insn = start_insn, label = 0;
14576
14577       /* If the jump cannot be done with one instruction, we cannot
14578          conditionally execute the instruction in the inverse case.  */
14579       if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
14580         {
14581           jump_clobbers = 1;
14582           return;
14583         }
14584
14585       /* Register the insn jumped to.  */
14586       if (reverse)
14587         {
14588           if (!seeking_return)
14589             label = XEXP (SET_SRC (body), 0);
14590         }
14591       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
14592         label = XEXP (XEXP (SET_SRC (body), 1), 0);
14593       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
14594         {
14595           label = XEXP (XEXP (SET_SRC (body), 2), 0);
14596           then_not_else = FALSE;
14597         }
14598       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
14599         seeking_return = 1;
14600       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
14601         {
14602           seeking_return = 1;
14603           then_not_else = FALSE;
14604         }
14605       else
14606         gcc_unreachable ();
14607
14608       /* See how many insns this branch skips, and what kind of insns.  If all
14609          insns are okay, and the label or unconditional branch to the same
14610          label is not too far away, succeed.  */
14611       for (insns_skipped = 0;
14612            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
14613         {
14614           rtx scanbody;
14615
14616           this_insn = next_nonnote_insn (this_insn);
14617           if (!this_insn)
14618             break;
14619
14620           switch (GET_CODE (this_insn))
14621             {
14622             case CODE_LABEL:
14623               /* Succeed if it is the target label, otherwise fail since
14624                  control falls in from somewhere else.  */
14625               if (this_insn == label)
14626                 {
14627                   if (jump_clobbers)
14628                     {
14629                       arm_ccfsm_state = 2;
14630                       this_insn = next_nonnote_insn (this_insn);
14631                     }
14632                   else
14633                     arm_ccfsm_state = 1;
14634                   succeed = TRUE;
14635                 }
14636               else
14637                 fail = TRUE;
14638               break;
14639
14640             case BARRIER:
14641               /* Succeed if the following insn is the target label.
14642                  Otherwise fail.
14643                  If return insns are used then the last insn in a function
14644                  will be a barrier.  */
14645               this_insn = next_nonnote_insn (this_insn);
14646               if (this_insn && this_insn == label)
14647                 {
14648                   if (jump_clobbers)
14649                     {
14650                       arm_ccfsm_state = 2;
14651                       this_insn = next_nonnote_insn (this_insn);
14652                     }
14653                   else
14654                     arm_ccfsm_state = 1;
14655                   succeed = TRUE;
14656                 }
14657               else
14658                 fail = TRUE;
14659               break;
14660
14661             case CALL_INSN:
14662               /* The AAPCS says that conditional calls should not be
14663                  used since they make interworking inefficient (the
14664                  linker can't transform BL<cond> into BLX).  That's
14665                  only a problem if the machine has BLX.  */
14666               if (arm_arch5)
14667                 {
14668                   fail = TRUE;
14669                   break;
14670                 }
14671
14672               /* Succeed if the following insn is the target label, or
14673                  if the following two insns are a barrier and the
14674                  target label.  */
14675               this_insn = next_nonnote_insn (this_insn);
14676               if (this_insn && GET_CODE (this_insn) == BARRIER)
14677                 this_insn = next_nonnote_insn (this_insn);
14678
14679               if (this_insn && this_insn == label
14680                   && insns_skipped < max_insns_skipped)
14681                 {
14682                   if (jump_clobbers)
14683                     {
14684                       arm_ccfsm_state = 2;
14685                       this_insn = next_nonnote_insn (this_insn);
14686                     }
14687                   else
14688                     arm_ccfsm_state = 1;
14689                   succeed = TRUE;
14690                 }
14691               else
14692                 fail = TRUE;
14693               break;
14694
14695             case JUMP_INSN:
14696               /* If this is an unconditional branch to the same label, succeed.
14697                  If it is to another label, do nothing.  If it is conditional,
14698                  fail.  */
14699               /* XXX Probably, the tests for SET and the PC are
14700                  unnecessary.  */
14701
14702               scanbody = PATTERN (this_insn);
14703               if (GET_CODE (scanbody) == SET
14704                   && GET_CODE (SET_DEST (scanbody)) == PC)
14705                 {
14706                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
14707                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
14708                     {
14709                       arm_ccfsm_state = 2;
14710                       succeed = TRUE;
14711                     }
14712                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
14713                     fail = TRUE;
14714                 }
14715               /* Fail if a conditional return is undesirable (e.g. on a
14716                  StrongARM), but still allow this if optimizing for size.  */
14717               else if (GET_CODE (scanbody) == RETURN
14718                        && !use_return_insn (TRUE, NULL)
14719                        && !optimize_size)
14720                 fail = TRUE;
14721               else if (GET_CODE (scanbody) == RETURN
14722                        && seeking_return)
14723                 {
14724                   arm_ccfsm_state = 2;
14725                   succeed = TRUE;
14726                 }
14727               else if (GET_CODE (scanbody) == PARALLEL)
14728                 {
14729                   switch (get_attr_conds (this_insn))
14730                     {
14731                     case CONDS_NOCOND:
14732                       break;
14733                     default:
14734                       fail = TRUE;
14735                       break;
14736                     }
14737                 }
14738               else
14739                 fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
14740
14741               break;
14742
14743             case INSN:
14744               /* Instructions using or affecting the condition codes make it
14745                  fail.  */
14746               scanbody = PATTERN (this_insn);
14747               if (!(GET_CODE (scanbody) == SET
14748                     || GET_CODE (scanbody) == PARALLEL)
14749                   || get_attr_conds (this_insn) != CONDS_NOCOND)
14750                 fail = TRUE;
14751
14752               /* A conditional cirrus instruction must be followed by
14753                  a non Cirrus instruction.  However, since we
14754                  conditionalize instructions in this function and by
14755                  the time we get here we can't add instructions
14756                  (nops), because shorten_branches() has already been
14757                  called, we will disable conditionalizing Cirrus
14758                  instructions to be safe.  */
14759               if (GET_CODE (scanbody) != USE
14760                   && GET_CODE (scanbody) != CLOBBER
14761                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
14762                 fail = TRUE;
14763               break;
14764
14765             default:
14766               break;
14767             }
14768         }
14769       if (succeed)
14770         {
14771           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
14772             arm_target_label = CODE_LABEL_NUMBER (label);
14773           else
14774             {
14775               gcc_assert (seeking_return || arm_ccfsm_state == 2);
14776
14777               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
14778                 {
14779                   this_insn = next_nonnote_insn (this_insn);
14780                   gcc_assert (!this_insn
14781                               || (GET_CODE (this_insn) != BARRIER
14782                                   && GET_CODE (this_insn) != CODE_LABEL));
14783                 }
14784               if (!this_insn)
14785                 {
14786                   /* Oh, dear! we ran off the end.. give up.  */
14787                   extract_constrain_insn_cached (insn);
14788                   arm_ccfsm_state = 0;
14789                   arm_target_insn = NULL;
14790                   return;
14791                 }
14792               arm_target_insn = this_insn;
14793             }
14794           if (jump_clobbers)
14795             {
14796               gcc_assert (!reverse);
14797               arm_current_cc =
14798                   get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
14799                                                             0), 0), 1));
14800               if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
14801                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
14802               if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
14803                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
14804             }
14805           else
14806             {
14807               /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
14808                  what it was.  */
14809               if (!reverse)
14810                 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
14811                                                                0));
14812             }
14813
14814           if (reverse || then_not_else)
14815             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
14816         }
14817
14818       /* Restore recog_data (getting the attributes of other insns can
14819          destroy this array, but final.c assumes that it remains intact
14820          across this call.  */
14821       extract_constrain_insn_cached (insn);
14822     }
14823 }
14824
14825 /* Output IT instructions.  */
14826 void
14827 thumb2_asm_output_opcode (FILE * stream)
14828 {
14829   char buff[5];
14830   int n;
14831
14832   if (arm_condexec_mask)
14833     {
14834       for (n = 0; n < arm_condexec_masklen; n++)
14835         buff[n] = (arm_condexec_mask & (1 << n)) ? 't' : 'e';
14836       buff[n] = 0;
14837       asm_fprintf(stream, "i%s\t%s\n\t", buff,
14838                   arm_condition_codes[arm_current_cc]);
14839       arm_condexec_mask = 0;
14840     }
14841 }
14842
14843 /* Returns true if REGNO is a valid register
14844    for holding a quantity of type MODE.  */
14845 int
14846 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
14847 {
14848   if (GET_MODE_CLASS (mode) == MODE_CC)
14849     return (regno == CC_REGNUM
14850             || (TARGET_HARD_FLOAT && TARGET_VFP
14851                 && regno == VFPCC_REGNUM));
14852
14853   if (TARGET_THUMB1)
14854     /* For the Thumb we only allow values bigger than SImode in
14855        registers 0 - 6, so that there is always a second low
14856        register available to hold the upper part of the value.
14857        We probably we ought to ensure that the register is the
14858        start of an even numbered register pair.  */
14859     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
14860
14861   if (TARGET_HARD_FLOAT && TARGET_MAVERICK
14862       && IS_CIRRUS_REGNUM (regno))
14863     /* We have outlawed SI values in Cirrus registers because they
14864        reside in the lower 32 bits, but SF values reside in the
14865        upper 32 bits.  This causes gcc all sorts of grief.  We can't
14866        even split the registers into pairs because Cirrus SI values
14867        get sign extended to 64bits-- aldyh.  */
14868     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
14869
14870   if (TARGET_HARD_FLOAT && TARGET_VFP
14871       && IS_VFP_REGNUM (regno))
14872     {
14873       if (mode == SFmode || mode == SImode)
14874         return VFP_REGNO_OK_FOR_SINGLE (regno);
14875
14876       if (mode == DFmode)
14877         return VFP_REGNO_OK_FOR_DOUBLE (regno);
14878
14879       /* VFP registers can hold HFmode values, but there is no point in
14880          putting them there unless we have the NEON extensions for
14881          loading/storing them, too.  */
14882       if (mode == HFmode)
14883         return TARGET_NEON_FP16 && VFP_REGNO_OK_FOR_SINGLE (regno);
14884
14885       if (TARGET_NEON)
14886         return (VALID_NEON_DREG_MODE (mode) && VFP_REGNO_OK_FOR_DOUBLE (regno))
14887                || (VALID_NEON_QREG_MODE (mode)
14888                    && NEON_REGNO_OK_FOR_QUAD (regno))
14889                || (mode == TImode && NEON_REGNO_OK_FOR_NREGS (regno, 2))
14890                || (mode == EImode && NEON_REGNO_OK_FOR_NREGS (regno, 3))
14891                || (mode == OImode && NEON_REGNO_OK_FOR_NREGS (regno, 4))
14892                || (mode == CImode && NEON_REGNO_OK_FOR_NREGS (regno, 6))
14893                || (mode == XImode && NEON_REGNO_OK_FOR_NREGS (regno, 8));
14894
14895       return FALSE;
14896     }
14897
14898   if (TARGET_REALLY_IWMMXT)
14899     {
14900       if (IS_IWMMXT_GR_REGNUM (regno))
14901         return mode == SImode;
14902
14903       if (IS_IWMMXT_REGNUM (regno))
14904         return VALID_IWMMXT_REG_MODE (mode);
14905     }
14906   
14907   /* We allow almost any value to be stored in the general registers.
14908      Restrict doubleword quantities to even register pairs so that we can
14909      use ldrd.  Do not allow very large Neon structure opaque modes in
14910      general registers; they would use too many.  */
14911   if (regno <= LAST_ARM_REGNUM)
14912     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0)
14913       && ARM_NUM_REGS (mode) <= 4;
14914
14915   if (regno == FRAME_POINTER_REGNUM
14916       || regno == ARG_POINTER_REGNUM)
14917     /* We only allow integers in the fake hard registers.  */
14918     return GET_MODE_CLASS (mode) == MODE_INT;
14919
14920   /* The only registers left are the FPA registers
14921      which we only allow to hold FP values.  */
14922   return (TARGET_HARD_FLOAT && TARGET_FPA
14923           && GET_MODE_CLASS (mode) == MODE_FLOAT
14924           && regno >= FIRST_FPA_REGNUM
14925           && regno <= LAST_FPA_REGNUM);
14926 }
14927
14928 /* For efficiency and historical reasons LO_REGS, HI_REGS and CC_REGS are
14929    not used in arm mode.  */
14930
14931 enum reg_class
14932 arm_regno_class (int regno)
14933 {
14934   if (TARGET_THUMB1)
14935     {
14936       if (regno == STACK_POINTER_REGNUM)
14937         return STACK_REG;
14938       if (regno == CC_REGNUM)
14939         return CC_REG;
14940       if (regno < 8)
14941         return LO_REGS;
14942       return HI_REGS;
14943     }
14944
14945   if (TARGET_THUMB2 && regno < 8)
14946     return LO_REGS;
14947
14948   if (   regno <= LAST_ARM_REGNUM
14949       || regno == FRAME_POINTER_REGNUM
14950       || regno == ARG_POINTER_REGNUM)
14951     return TARGET_THUMB2 ? HI_REGS : GENERAL_REGS;
14952
14953   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
14954     return TARGET_THUMB2 ? CC_REG : NO_REGS;
14955
14956   if (IS_CIRRUS_REGNUM (regno))
14957     return CIRRUS_REGS;
14958
14959   if (IS_VFP_REGNUM (regno))
14960     {
14961       if (regno <= D7_VFP_REGNUM)
14962         return VFP_D0_D7_REGS;
14963       else if (regno <= LAST_LO_VFP_REGNUM)
14964         return VFP_LO_REGS;
14965       else
14966         return VFP_HI_REGS;
14967     }
14968
14969   if (IS_IWMMXT_REGNUM (regno))
14970     return IWMMXT_REGS;
14971
14972   if (IS_IWMMXT_GR_REGNUM (regno))
14973     return IWMMXT_GR_REGS;
14974
14975   return FPA_REGS;
14976 }
14977
14978 /* Handle a special case when computing the offset
14979    of an argument from the frame pointer.  */
14980 int
14981 arm_debugger_arg_offset (int value, rtx addr)
14982 {
14983   rtx insn;
14984
14985   /* We are only interested if dbxout_parms() failed to compute the offset.  */
14986   if (value != 0)
14987     return 0;
14988
14989   /* We can only cope with the case where the address is held in a register.  */
14990   if (GET_CODE (addr) != REG)
14991     return 0;
14992
14993   /* If we are using the frame pointer to point at the argument, then
14994      an offset of 0 is correct.  */
14995   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
14996     return 0;
14997
14998   /* If we are using the stack pointer to point at the
14999      argument, then an offset of 0 is correct.  */
15000   /* ??? Check this is consistent with thumb2 frame layout.  */
15001   if ((TARGET_THUMB || !frame_pointer_needed)
15002       && REGNO (addr) == SP_REGNUM)
15003     return 0;
15004
15005   /* Oh dear.  The argument is pointed to by a register rather
15006      than being held in a register, or being stored at a known
15007      offset from the frame pointer.  Since GDB only understands
15008      those two kinds of argument we must translate the address
15009      held in the register into an offset from the frame pointer.
15010      We do this by searching through the insns for the function
15011      looking to see where this register gets its value.  If the
15012      register is initialized from the frame pointer plus an offset
15013      then we are in luck and we can continue, otherwise we give up.
15014
15015      This code is exercised by producing debugging information
15016      for a function with arguments like this:
15017
15018            double func (double a, double b, int c, double d) {return d;}
15019
15020      Without this code the stab for parameter 'd' will be set to
15021      an offset of 0 from the frame pointer, rather than 8.  */
15022
15023   /* The if() statement says:
15024
15025      If the insn is a normal instruction
15026      and if the insn is setting the value in a register
15027      and if the register being set is the register holding the address of the argument
15028      and if the address is computing by an addition
15029      that involves adding to a register
15030      which is the frame pointer
15031      a constant integer
15032
15033      then...  */
15034
15035   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
15036     {
15037       if (   GET_CODE (insn) == INSN
15038           && GET_CODE (PATTERN (insn)) == SET
15039           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
15040           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
15041           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
15042           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
15043           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
15044              )
15045         {
15046           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
15047
15048           break;
15049         }
15050     }
15051
15052   if (value == 0)
15053     {
15054       debug_rtx (addr);
15055       warning (0, "unable to compute real location of stacked parameter");
15056       value = 8; /* XXX magic hack */
15057     }
15058
15059   return value;
15060 }
15061 \f
15062 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
15063   do                                                                    \
15064     {                                                                   \
15065       if ((MASK) & insn_flags)                                          \
15066         add_builtin_function ((NAME), (TYPE), (CODE),                   \
15067                              BUILT_IN_MD, NULL, NULL_TREE);             \
15068     }                                                                   \
15069   while (0)
15070
15071 struct builtin_description
15072 {
15073   const unsigned int       mask;
15074   const enum insn_code     icode;
15075   const char * const       name;
15076   const enum arm_builtins  code;
15077   const enum rtx_code      comparison;
15078   const unsigned int       flag;
15079 };
15080
15081 static const struct builtin_description bdesc_2arg[] =
15082 {
15083 #define IWMMXT_BUILTIN(code, string, builtin) \
15084   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
15085     ARM_BUILTIN_##builtin, UNKNOWN, 0 },
15086
15087   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
15088   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
15089   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
15090   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
15091   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
15092   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
15093   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
15094   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
15095   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
15096   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
15097   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
15098   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
15099   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
15100   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
15101   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
15102   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
15103   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
15104   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
15105   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
15106   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
15107   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
15108   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
15109   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
15110   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
15111   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
15112   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
15113   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
15114   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
15115   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
15116   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
15117   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
15118   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
15119   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
15120   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
15121   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
15122   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
15123   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
15124   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
15125   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
15126   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
15127   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
15128   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
15129   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
15130   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
15131   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
15132   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
15133   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
15134   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
15135   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
15136   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
15137   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
15138   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
15139   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
15140   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
15141   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
15142   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
15143   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
15144   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
15145
15146 #define IWMMXT_BUILTIN2(code, builtin) \
15147   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, UNKNOWN, 0 },
15148
15149   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
15150   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
15151   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
15152   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
15153   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
15154   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
15155   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
15156   IWMMXT_BUILTIN2 (ashlv4hi3_iwmmxt, WSLLHI)
15157   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
15158   IWMMXT_BUILTIN2 (ashlv2si3_iwmmxt, WSLLWI)
15159   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
15160   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
15161   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
15162   IWMMXT_BUILTIN2 (lshrv4hi3_iwmmxt, WSRLHI)
15163   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
15164   IWMMXT_BUILTIN2 (lshrv2si3_iwmmxt, WSRLWI)
15165   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
15166   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
15167   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
15168   IWMMXT_BUILTIN2 (ashrv4hi3_iwmmxt, WSRAHI)
15169   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
15170   IWMMXT_BUILTIN2 (ashrv2si3_iwmmxt, WSRAWI)
15171   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
15172   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
15173   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
15174   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
15175   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
15176   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
15177   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
15178   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
15179   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
15180   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
15181 };
15182
15183 static const struct builtin_description bdesc_1arg[] =
15184 {
15185   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
15186   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
15187   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
15188   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
15189   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
15190   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
15191   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
15192   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
15193   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
15194   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
15195   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
15196   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
15197   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
15198   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
15199   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
15200   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
15201   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
15202   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
15203 };
15204
15205 /* Set up all the iWMMXt builtins.  This is
15206    not called if TARGET_IWMMXT is zero.  */
15207
15208 static void
15209 arm_init_iwmmxt_builtins (void)
15210 {
15211   const struct builtin_description * d;
15212   size_t i;
15213   tree endlink = void_list_node;
15214
15215   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
15216   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
15217   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
15218
15219   tree int_ftype_int
15220     = build_function_type (integer_type_node,
15221                            tree_cons (NULL_TREE, integer_type_node, endlink));
15222   tree v8qi_ftype_v8qi_v8qi_int
15223     = build_function_type (V8QI_type_node,
15224                            tree_cons (NULL_TREE, V8QI_type_node,
15225                                       tree_cons (NULL_TREE, V8QI_type_node,
15226                                                  tree_cons (NULL_TREE,
15227                                                             integer_type_node,
15228                                                             endlink))));
15229   tree v4hi_ftype_v4hi_int
15230     = build_function_type (V4HI_type_node,
15231                            tree_cons (NULL_TREE, V4HI_type_node,
15232                                       tree_cons (NULL_TREE, integer_type_node,
15233                                                  endlink)));
15234   tree v2si_ftype_v2si_int
15235     = build_function_type (V2SI_type_node,
15236                            tree_cons (NULL_TREE, V2SI_type_node,
15237                                       tree_cons (NULL_TREE, integer_type_node,
15238                                                  endlink)));
15239   tree v2si_ftype_di_di
15240     = build_function_type (V2SI_type_node,
15241                            tree_cons (NULL_TREE, long_long_integer_type_node,
15242                                       tree_cons (NULL_TREE, long_long_integer_type_node,
15243                                                  endlink)));
15244   tree di_ftype_di_int
15245     = build_function_type (long_long_integer_type_node,
15246                            tree_cons (NULL_TREE, long_long_integer_type_node,
15247                                       tree_cons (NULL_TREE, integer_type_node,
15248                                                  endlink)));
15249   tree di_ftype_di_int_int
15250     = build_function_type (long_long_integer_type_node,
15251                            tree_cons (NULL_TREE, long_long_integer_type_node,
15252                                       tree_cons (NULL_TREE, integer_type_node,
15253                                                  tree_cons (NULL_TREE,
15254                                                             integer_type_node,
15255                                                             endlink))));
15256   tree int_ftype_v8qi
15257     = build_function_type (integer_type_node,
15258                            tree_cons (NULL_TREE, V8QI_type_node,
15259                                       endlink));
15260   tree int_ftype_v4hi
15261     = build_function_type (integer_type_node,
15262                            tree_cons (NULL_TREE, V4HI_type_node,
15263                                       endlink));
15264   tree int_ftype_v2si
15265     = build_function_type (integer_type_node,
15266                            tree_cons (NULL_TREE, V2SI_type_node,
15267                                       endlink));
15268   tree int_ftype_v8qi_int
15269     = build_function_type (integer_type_node,
15270                            tree_cons (NULL_TREE, V8QI_type_node,
15271                                       tree_cons (NULL_TREE, integer_type_node,
15272                                                  endlink)));
15273   tree int_ftype_v4hi_int
15274     = build_function_type (integer_type_node,
15275                            tree_cons (NULL_TREE, V4HI_type_node,
15276                                       tree_cons (NULL_TREE, integer_type_node,
15277                                                  endlink)));
15278   tree int_ftype_v2si_int
15279     = build_function_type (integer_type_node,
15280                            tree_cons (NULL_TREE, V2SI_type_node,
15281                                       tree_cons (NULL_TREE, integer_type_node,
15282                                                  endlink)));
15283   tree v8qi_ftype_v8qi_int_int
15284     = build_function_type (V8QI_type_node,
15285                            tree_cons (NULL_TREE, V8QI_type_node,
15286                                       tree_cons (NULL_TREE, integer_type_node,
15287                                                  tree_cons (NULL_TREE,
15288                                                             integer_type_node,
15289                                                             endlink))));
15290   tree v4hi_ftype_v4hi_int_int
15291     = build_function_type (V4HI_type_node,
15292                            tree_cons (NULL_TREE, V4HI_type_node,
15293                                       tree_cons (NULL_TREE, integer_type_node,
15294                                                  tree_cons (NULL_TREE,
15295                                                             integer_type_node,
15296                                                             endlink))));
15297   tree v2si_ftype_v2si_int_int
15298     = build_function_type (V2SI_type_node,
15299                            tree_cons (NULL_TREE, V2SI_type_node,
15300                                       tree_cons (NULL_TREE, integer_type_node,
15301                                                  tree_cons (NULL_TREE,
15302                                                             integer_type_node,
15303                                                             endlink))));
15304   /* Miscellaneous.  */
15305   tree v8qi_ftype_v4hi_v4hi
15306     = build_function_type (V8QI_type_node,
15307                            tree_cons (NULL_TREE, V4HI_type_node,
15308                                       tree_cons (NULL_TREE, V4HI_type_node,
15309                                                  endlink)));
15310   tree v4hi_ftype_v2si_v2si
15311     = build_function_type (V4HI_type_node,
15312                            tree_cons (NULL_TREE, V2SI_type_node,
15313                                       tree_cons (NULL_TREE, V2SI_type_node,
15314                                                  endlink)));
15315   tree v2si_ftype_v4hi_v4hi
15316     = build_function_type (V2SI_type_node,
15317                            tree_cons (NULL_TREE, V4HI_type_node,
15318                                       tree_cons (NULL_TREE, V4HI_type_node,
15319                                                  endlink)));
15320   tree v2si_ftype_v8qi_v8qi
15321     = build_function_type (V2SI_type_node,
15322                            tree_cons (NULL_TREE, V8QI_type_node,
15323                                       tree_cons (NULL_TREE, V8QI_type_node,
15324                                                  endlink)));
15325   tree v4hi_ftype_v4hi_di
15326     = build_function_type (V4HI_type_node,
15327                            tree_cons (NULL_TREE, V4HI_type_node,
15328                                       tree_cons (NULL_TREE,
15329                                                  long_long_integer_type_node,
15330                                                  endlink)));
15331   tree v2si_ftype_v2si_di
15332     = build_function_type (V2SI_type_node,
15333                            tree_cons (NULL_TREE, V2SI_type_node,
15334                                       tree_cons (NULL_TREE,
15335                                                  long_long_integer_type_node,
15336                                                  endlink)));
15337   tree void_ftype_int_int
15338     = build_function_type (void_type_node,
15339                            tree_cons (NULL_TREE, integer_type_node,
15340                                       tree_cons (NULL_TREE, integer_type_node,
15341                                                  endlink)));
15342   tree di_ftype_void
15343     = build_function_type (long_long_unsigned_type_node, endlink);
15344   tree di_ftype_v8qi
15345     = build_function_type (long_long_integer_type_node,
15346                            tree_cons (NULL_TREE, V8QI_type_node,
15347                                       endlink));
15348   tree di_ftype_v4hi
15349     = build_function_type (long_long_integer_type_node,
15350                            tree_cons (NULL_TREE, V4HI_type_node,
15351                                       endlink));
15352   tree di_ftype_v2si
15353     = build_function_type (long_long_integer_type_node,
15354                            tree_cons (NULL_TREE, V2SI_type_node,
15355                                       endlink));
15356   tree v2si_ftype_v4hi
15357     = build_function_type (V2SI_type_node,
15358                            tree_cons (NULL_TREE, V4HI_type_node,
15359                                       endlink));
15360   tree v4hi_ftype_v8qi
15361     = build_function_type (V4HI_type_node,
15362                            tree_cons (NULL_TREE, V8QI_type_node,
15363                                       endlink));
15364
15365   tree di_ftype_di_v4hi_v4hi
15366     = build_function_type (long_long_unsigned_type_node,
15367                            tree_cons (NULL_TREE,
15368                                       long_long_unsigned_type_node,
15369                                       tree_cons (NULL_TREE, V4HI_type_node,
15370                                                  tree_cons (NULL_TREE,
15371                                                             V4HI_type_node,
15372                                                             endlink))));
15373
15374   tree di_ftype_v4hi_v4hi
15375     = build_function_type (long_long_unsigned_type_node,
15376                            tree_cons (NULL_TREE, V4HI_type_node,
15377                                       tree_cons (NULL_TREE, V4HI_type_node,
15378                                                  endlink)));
15379
15380   /* Normal vector binops.  */
15381   tree v8qi_ftype_v8qi_v8qi
15382     = build_function_type (V8QI_type_node,
15383                            tree_cons (NULL_TREE, V8QI_type_node,
15384                                       tree_cons (NULL_TREE, V8QI_type_node,
15385                                                  endlink)));
15386   tree v4hi_ftype_v4hi_v4hi
15387     = build_function_type (V4HI_type_node,
15388                            tree_cons (NULL_TREE, V4HI_type_node,
15389                                       tree_cons (NULL_TREE, V4HI_type_node,
15390                                                  endlink)));
15391   tree v2si_ftype_v2si_v2si
15392     = build_function_type (V2SI_type_node,
15393                            tree_cons (NULL_TREE, V2SI_type_node,
15394                                       tree_cons (NULL_TREE, V2SI_type_node,
15395                                                  endlink)));
15396   tree di_ftype_di_di
15397     = build_function_type (long_long_unsigned_type_node,
15398                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
15399                                       tree_cons (NULL_TREE,
15400                                                  long_long_unsigned_type_node,
15401                                                  endlink)));
15402
15403   /* Add all builtins that are more or less simple operations on two
15404      operands.  */
15405   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
15406     {
15407       /* Use one of the operands; the target can have a different mode for
15408          mask-generating compares.  */
15409       enum machine_mode mode;
15410       tree type;
15411
15412       if (d->name == 0)
15413         continue;
15414
15415       mode = insn_data[d->icode].operand[1].mode;
15416
15417       switch (mode)
15418         {
15419         case V8QImode:
15420           type = v8qi_ftype_v8qi_v8qi;
15421           break;
15422         case V4HImode:
15423           type = v4hi_ftype_v4hi_v4hi;
15424           break;
15425         case V2SImode:
15426           type = v2si_ftype_v2si_v2si;
15427           break;
15428         case DImode:
15429           type = di_ftype_di_di;
15430           break;
15431
15432         default:
15433           gcc_unreachable ();
15434         }
15435
15436       def_mbuiltin (d->mask, d->name, type, d->code);
15437     }
15438
15439   /* Add the remaining MMX insns with somewhat more complicated types.  */
15440   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
15441   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
15442   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
15443
15444   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
15445   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
15446   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
15447   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
15448   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
15449   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
15450
15451   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
15452   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
15453   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
15454   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
15455   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
15456   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
15457
15458   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
15459   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
15460   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
15461   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
15462   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
15463   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
15464
15465   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
15466   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
15467   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
15468   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
15469   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
15470   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
15471
15472   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
15473
15474   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
15475   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
15476   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
15477   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
15478
15479   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
15480   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
15481   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
15482   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
15483   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
15484   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
15485   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
15486   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
15487   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
15488
15489   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
15490   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
15491   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
15492
15493   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
15494   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
15495   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
15496
15497   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
15498   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
15499   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
15500   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
15501   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
15502   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
15503
15504   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
15505   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
15506   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
15507   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
15508   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
15509   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
15510   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
15511   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
15512   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
15513   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
15514   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
15515   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
15516
15517   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
15518   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
15519   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
15520   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
15521
15522   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
15523   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
15524   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
15525   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
15526   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
15527   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
15528   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
15529 }
15530
15531 static void
15532 arm_init_tls_builtins (void)
15533 {
15534   tree ftype, decl;
15535
15536   ftype = build_function_type (ptr_type_node, void_list_node);
15537   decl = add_builtin_function ("__builtin_thread_pointer", ftype,
15538                                ARM_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
15539                                NULL, NULL_TREE);
15540   TREE_NOTHROW (decl) = 1;
15541   TREE_READONLY (decl) = 1;
15542 }
15543
15544 enum neon_builtin_type_bits {
15545   T_V8QI  = 0x0001,
15546   T_V4HI  = 0x0002,
15547   T_V2SI  = 0x0004,
15548   T_V2SF  = 0x0008,
15549   T_DI    = 0x0010,
15550   T_V16QI = 0x0020,
15551   T_V8HI  = 0x0040,
15552   T_V4SI  = 0x0080,
15553   T_V4SF  = 0x0100,
15554   T_V2DI  = 0x0200,
15555   T_TI    = 0x0400,
15556   T_EI    = 0x0800,
15557   T_OI    = 0x1000
15558 };
15559
15560 #define v8qi_UP  T_V8QI
15561 #define v4hi_UP  T_V4HI
15562 #define v2si_UP  T_V2SI
15563 #define v2sf_UP  T_V2SF
15564 #define di_UP    T_DI
15565 #define v16qi_UP T_V16QI
15566 #define v8hi_UP  T_V8HI
15567 #define v4si_UP  T_V4SI
15568 #define v4sf_UP  T_V4SF
15569 #define v2di_UP  T_V2DI
15570 #define ti_UP    T_TI
15571 #define ei_UP    T_EI
15572 #define oi_UP    T_OI
15573
15574 #define UP(X) X##_UP
15575
15576 #define T_MAX 13
15577
15578 typedef enum {
15579   NEON_BINOP,
15580   NEON_TERNOP,
15581   NEON_UNOP,
15582   NEON_GETLANE,
15583   NEON_SETLANE,
15584   NEON_CREATE,
15585   NEON_DUP,
15586   NEON_DUPLANE,
15587   NEON_COMBINE,
15588   NEON_SPLIT,
15589   NEON_LANEMUL,
15590   NEON_LANEMULL,
15591   NEON_LANEMULH,
15592   NEON_LANEMAC,
15593   NEON_SCALARMUL,
15594   NEON_SCALARMULL,
15595   NEON_SCALARMULH,
15596   NEON_SCALARMAC,
15597   NEON_CONVERT,
15598   NEON_FIXCONV,
15599   NEON_SELECT,
15600   NEON_RESULTPAIR,
15601   NEON_REINTERP,
15602   NEON_VTBL,
15603   NEON_VTBX,
15604   NEON_LOAD1,
15605   NEON_LOAD1LANE,
15606   NEON_STORE1,
15607   NEON_STORE1LANE,
15608   NEON_LOADSTRUCT,
15609   NEON_LOADSTRUCTLANE,
15610   NEON_STORESTRUCT,
15611   NEON_STORESTRUCTLANE,
15612   NEON_LOGICBINOP,
15613   NEON_SHIFTINSERT,
15614   NEON_SHIFTIMM,
15615   NEON_SHIFTACC
15616 } neon_itype;
15617
15618 typedef struct {
15619   const char *name;
15620   const neon_itype itype;
15621   const int bits;
15622   const enum insn_code codes[T_MAX];
15623   const unsigned int num_vars;
15624   unsigned int base_fcode;
15625 } neon_builtin_datum;
15626
15627 #define CF(N,X) CODE_FOR_neon_##N##X
15628
15629 #define VAR1(T, N, A) \
15630   #N, NEON_##T, UP (A), { CF (N, A) }, 1, 0
15631 #define VAR2(T, N, A, B) \
15632   #N, NEON_##T, UP (A) | UP (B), { CF (N, A), CF (N, B) }, 2, 0
15633 #define VAR3(T, N, A, B, C) \
15634   #N, NEON_##T, UP (A) | UP (B) | UP (C), \
15635   { CF (N, A), CF (N, B), CF (N, C) }, 3, 0
15636 #define VAR4(T, N, A, B, C, D) \
15637   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D), \
15638   { CF (N, A), CF (N, B), CF (N, C), CF (N, D) }, 4, 0
15639 #define VAR5(T, N, A, B, C, D, E) \
15640   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E), \
15641   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E) }, 5, 0
15642 #define VAR6(T, N, A, B, C, D, E, F) \
15643   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F), \
15644   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F) }, 6, 0
15645 #define VAR7(T, N, A, B, C, D, E, F, G) \
15646   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G), \
15647   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
15648     CF (N, G) }, 7, 0
15649 #define VAR8(T, N, A, B, C, D, E, F, G, H) \
15650   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
15651                 | UP (H), \
15652   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
15653     CF (N, G), CF (N, H) }, 8, 0
15654 #define VAR9(T, N, A, B, C, D, E, F, G, H, I) \
15655   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
15656                 | UP (H) | UP (I), \
15657   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
15658     CF (N, G), CF (N, H), CF (N, I) }, 9, 0
15659 #define VAR10(T, N, A, B, C, D, E, F, G, H, I, J) \
15660   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
15661                 | UP (H) | UP (I) | UP (J), \
15662   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
15663     CF (N, G), CF (N, H), CF (N, I), CF (N, J) }, 10, 0
15664
15665 /* The mode entries in the following table correspond to the "key" type of the
15666    instruction variant, i.e. equivalent to that which would be specified after
15667    the assembler mnemonic, which usually refers to the last vector operand.
15668    (Signed/unsigned/polynomial types are not differentiated between though, and
15669    are all mapped onto the same mode for a given element size.) The modes
15670    listed per instruction should be the same as those defined for that
15671    instruction's pattern in neon.md.
15672    WARNING: Variants should be listed in the same increasing order as
15673    neon_builtin_type_bits.  */
15674
15675 static neon_builtin_datum neon_builtin_data[] =
15676 {
15677   { VAR10 (BINOP, vadd,
15678            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15679   { VAR3 (BINOP, vaddl, v8qi, v4hi, v2si) },
15680   { VAR3 (BINOP, vaddw, v8qi, v4hi, v2si) },
15681   { VAR6 (BINOP, vhadd, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15682   { VAR8 (BINOP, vqadd, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15683   { VAR3 (BINOP, vaddhn, v8hi, v4si, v2di) },
15684   { VAR8 (BINOP, vmul, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15685   { VAR8 (TERNOP, vmla, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15686   { VAR3 (TERNOP, vmlal, v8qi, v4hi, v2si) },
15687   { VAR8 (TERNOP, vmls, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15688   { VAR3 (TERNOP, vmlsl, v8qi, v4hi, v2si) },
15689   { VAR4 (BINOP, vqdmulh, v4hi, v2si, v8hi, v4si) },
15690   { VAR2 (TERNOP, vqdmlal, v4hi, v2si) },
15691   { VAR2 (TERNOP, vqdmlsl, v4hi, v2si) },
15692   { VAR3 (BINOP, vmull, v8qi, v4hi, v2si) },
15693   { VAR2 (SCALARMULL, vmull_n, v4hi, v2si) },
15694   { VAR2 (LANEMULL, vmull_lane, v4hi, v2si) },
15695   { VAR2 (SCALARMULL, vqdmull_n, v4hi, v2si) },
15696   { VAR2 (LANEMULL, vqdmull_lane, v4hi, v2si) },
15697   { VAR4 (SCALARMULH, vqdmulh_n, v4hi, v2si, v8hi, v4si) },
15698   { VAR4 (LANEMULH, vqdmulh_lane, v4hi, v2si, v8hi, v4si) },
15699   { VAR2 (BINOP, vqdmull, v4hi, v2si) },
15700   { VAR8 (BINOP, vshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15701   { VAR8 (BINOP, vqshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15702   { VAR8 (SHIFTIMM, vshr_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15703   { VAR3 (SHIFTIMM, vshrn_n, v8hi, v4si, v2di) },
15704   { VAR3 (SHIFTIMM, vqshrn_n, v8hi, v4si, v2di) },
15705   { VAR3 (SHIFTIMM, vqshrun_n, v8hi, v4si, v2di) },
15706   { VAR8 (SHIFTIMM, vshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15707   { VAR8 (SHIFTIMM, vqshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15708   { VAR8 (SHIFTIMM, vqshlu_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15709   { VAR3 (SHIFTIMM, vshll_n, v8qi, v4hi, v2si) },
15710   { VAR8 (SHIFTACC, vsra_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15711   { VAR10 (BINOP, vsub,
15712            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15713   { VAR3 (BINOP, vsubl, v8qi, v4hi, v2si) },
15714   { VAR3 (BINOP, vsubw, v8qi, v4hi, v2si) },
15715   { VAR8 (BINOP, vqsub, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15716   { VAR6 (BINOP, vhsub, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15717   { VAR3 (BINOP, vsubhn, v8hi, v4si, v2di) },
15718   { VAR8 (BINOP, vceq, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15719   { VAR8 (BINOP, vcge, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15720   { VAR8 (BINOP, vcgt, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15721   { VAR2 (BINOP, vcage, v2sf, v4sf) },
15722   { VAR2 (BINOP, vcagt, v2sf, v4sf) },
15723   { VAR6 (BINOP, vtst, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15724   { VAR8 (BINOP, vabd, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15725   { VAR3 (BINOP, vabdl, v8qi, v4hi, v2si) },
15726   { VAR6 (TERNOP, vaba, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15727   { VAR3 (TERNOP, vabal, v8qi, v4hi, v2si) },
15728   { VAR8 (BINOP, vmax, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15729   { VAR8 (BINOP, vmin, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15730   { VAR4 (BINOP, vpadd, v8qi, v4hi, v2si, v2sf) },
15731   { VAR6 (UNOP, vpaddl, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15732   { VAR6 (BINOP, vpadal, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15733   { VAR4 (BINOP, vpmax, v8qi, v4hi, v2si, v2sf) },
15734   { VAR4 (BINOP, vpmin, v8qi, v4hi, v2si, v2sf) },
15735   { VAR2 (BINOP, vrecps, v2sf, v4sf) },
15736   { VAR2 (BINOP, vrsqrts, v2sf, v4sf) },
15737   { VAR8 (SHIFTINSERT, vsri_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15738   { VAR8 (SHIFTINSERT, vsli_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15739   { VAR8 (UNOP, vabs, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15740   { VAR6 (UNOP, vqabs, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15741   { VAR8 (UNOP, vneg, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15742   { VAR6 (UNOP, vqneg, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15743   { VAR6 (UNOP, vcls, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15744   { VAR6 (UNOP, vclz, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15745   { VAR2 (UNOP, vcnt, v8qi, v16qi) },
15746   { VAR4 (UNOP, vrecpe, v2si, v2sf, v4si, v4sf) },
15747   { VAR4 (UNOP, vrsqrte, v2si, v2sf, v4si, v4sf) },
15748   { VAR6 (UNOP, vmvn, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15749   /* FIXME: vget_lane supports more variants than this!  */
15750   { VAR10 (GETLANE, vget_lane,
15751            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15752   { VAR10 (SETLANE, vset_lane,
15753            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15754   { VAR5 (CREATE, vcreate, v8qi, v4hi, v2si, v2sf, di) },
15755   { VAR10 (DUP, vdup_n,
15756            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15757   { VAR10 (DUPLANE, vdup_lane,
15758            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15759   { VAR5 (COMBINE, vcombine, v8qi, v4hi, v2si, v2sf, di) },
15760   { VAR5 (SPLIT, vget_high, v16qi, v8hi, v4si, v4sf, v2di) },
15761   { VAR5 (SPLIT, vget_low, v16qi, v8hi, v4si, v4sf, v2di) },
15762   { VAR3 (UNOP, vmovn, v8hi, v4si, v2di) },
15763   { VAR3 (UNOP, vqmovn, v8hi, v4si, v2di) },
15764   { VAR3 (UNOP, vqmovun, v8hi, v4si, v2di) },
15765   { VAR3 (UNOP, vmovl, v8qi, v4hi, v2si) },
15766   { VAR6 (LANEMUL, vmul_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15767   { VAR6 (LANEMAC, vmla_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15768   { VAR2 (LANEMAC, vmlal_lane, v4hi, v2si) },
15769   { VAR2 (LANEMAC, vqdmlal_lane, v4hi, v2si) },
15770   { VAR6 (LANEMAC, vmls_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15771   { VAR2 (LANEMAC, vmlsl_lane, v4hi, v2si) },
15772   { VAR2 (LANEMAC, vqdmlsl_lane, v4hi, v2si) },
15773   { VAR6 (SCALARMUL, vmul_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15774   { VAR6 (SCALARMAC, vmla_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15775   { VAR2 (SCALARMAC, vmlal_n, v4hi, v2si) },
15776   { VAR2 (SCALARMAC, vqdmlal_n, v4hi, v2si) },
15777   { VAR6 (SCALARMAC, vmls_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15778   { VAR2 (SCALARMAC, vmlsl_n, v4hi, v2si) },
15779   { VAR2 (SCALARMAC, vqdmlsl_n, v4hi, v2si) },
15780   { VAR10 (BINOP, vext,
15781            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15782   { VAR8 (UNOP, vrev64, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15783   { VAR4 (UNOP, vrev32, v8qi, v4hi, v16qi, v8hi) },
15784   { VAR2 (UNOP, vrev16, v8qi, v16qi) },
15785   { VAR4 (CONVERT, vcvt, v2si, v2sf, v4si, v4sf) },
15786   { VAR4 (FIXCONV, vcvt_n, v2si, v2sf, v4si, v4sf) },
15787   { VAR10 (SELECT, vbsl,
15788            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15789   { VAR1 (VTBL, vtbl1, v8qi) },
15790   { VAR1 (VTBL, vtbl2, v8qi) },
15791   { VAR1 (VTBL, vtbl3, v8qi) },
15792   { VAR1 (VTBL, vtbl4, v8qi) },
15793   { VAR1 (VTBX, vtbx1, v8qi) },
15794   { VAR1 (VTBX, vtbx2, v8qi) },
15795   { VAR1 (VTBX, vtbx3, v8qi) },
15796   { VAR1 (VTBX, vtbx4, v8qi) },
15797   { VAR8 (RESULTPAIR, vtrn, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15798   { VAR8 (RESULTPAIR, vzip, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15799   { VAR8 (RESULTPAIR, vuzp, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15800   { VAR5 (REINTERP, vreinterpretv8qi, v8qi, v4hi, v2si, v2sf, di) },
15801   { VAR5 (REINTERP, vreinterpretv4hi, v8qi, v4hi, v2si, v2sf, di) },
15802   { VAR5 (REINTERP, vreinterpretv2si, v8qi, v4hi, v2si, v2sf, di) },
15803   { VAR5 (REINTERP, vreinterpretv2sf, v8qi, v4hi, v2si, v2sf, di) },
15804   { VAR5 (REINTERP, vreinterpretdi, v8qi, v4hi, v2si, v2sf, di) },
15805   { VAR5 (REINTERP, vreinterpretv16qi, v16qi, v8hi, v4si, v4sf, v2di) },
15806   { VAR5 (REINTERP, vreinterpretv8hi, v16qi, v8hi, v4si, v4sf, v2di) },
15807   { VAR5 (REINTERP, vreinterpretv4si, v16qi, v8hi, v4si, v4sf, v2di) },
15808   { VAR5 (REINTERP, vreinterpretv4sf, v16qi, v8hi, v4si, v4sf, v2di) },
15809   { VAR5 (REINTERP, vreinterpretv2di, v16qi, v8hi, v4si, v4sf, v2di) },
15810   { VAR10 (LOAD1, vld1,
15811            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15812   { VAR10 (LOAD1LANE, vld1_lane,
15813            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15814   { VAR10 (LOAD1, vld1_dup,
15815            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15816   { VAR10 (STORE1, vst1,
15817            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15818   { VAR10 (STORE1LANE, vst1_lane,
15819            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15820   { VAR9 (LOADSTRUCT,
15821           vld2, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15822   { VAR7 (LOADSTRUCTLANE, vld2_lane,
15823           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15824   { VAR5 (LOADSTRUCT, vld2_dup, v8qi, v4hi, v2si, v2sf, di) },
15825   { VAR9 (STORESTRUCT, vst2,
15826           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15827   { VAR7 (STORESTRUCTLANE, vst2_lane,
15828           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15829   { VAR9 (LOADSTRUCT,
15830           vld3, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15831   { VAR7 (LOADSTRUCTLANE, vld3_lane,
15832           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15833   { VAR5 (LOADSTRUCT, vld3_dup, v8qi, v4hi, v2si, v2sf, di) },
15834   { VAR9 (STORESTRUCT, vst3,
15835           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15836   { VAR7 (STORESTRUCTLANE, vst3_lane,
15837           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15838   { VAR9 (LOADSTRUCT, vld4,
15839           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15840   { VAR7 (LOADSTRUCTLANE, vld4_lane,
15841           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15842   { VAR5 (LOADSTRUCT, vld4_dup, v8qi, v4hi, v2si, v2sf, di) },
15843   { VAR9 (STORESTRUCT, vst4,
15844           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15845   { VAR7 (STORESTRUCTLANE, vst4_lane,
15846           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15847   { VAR10 (LOGICBINOP, vand,
15848            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15849   { VAR10 (LOGICBINOP, vorr,
15850            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15851   { VAR10 (BINOP, veor,
15852            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15853   { VAR10 (LOGICBINOP, vbic,
15854            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15855   { VAR10 (LOGICBINOP, vorn,
15856            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) }
15857 };
15858
15859 #undef CF
15860 #undef VAR1
15861 #undef VAR2
15862 #undef VAR3
15863 #undef VAR4
15864 #undef VAR5
15865 #undef VAR6
15866 #undef VAR7
15867 #undef VAR8
15868 #undef VAR9
15869 #undef VAR10
15870
15871 static void
15872 arm_init_neon_builtins (void)
15873 {
15874   unsigned int i, fcode = ARM_BUILTIN_NEON_BASE;
15875
15876   tree neon_intQI_type_node;
15877   tree neon_intHI_type_node;
15878   tree neon_polyQI_type_node;
15879   tree neon_polyHI_type_node;
15880   tree neon_intSI_type_node;
15881   tree neon_intDI_type_node;
15882   tree neon_float_type_node;
15883
15884   tree intQI_pointer_node;
15885   tree intHI_pointer_node;
15886   tree intSI_pointer_node;
15887   tree intDI_pointer_node;
15888   tree float_pointer_node;
15889
15890   tree const_intQI_node;
15891   tree const_intHI_node;
15892   tree const_intSI_node;
15893   tree const_intDI_node;
15894   tree const_float_node;
15895
15896   tree const_intQI_pointer_node;
15897   tree const_intHI_pointer_node;
15898   tree const_intSI_pointer_node;
15899   tree const_intDI_pointer_node;
15900   tree const_float_pointer_node;
15901
15902   tree V8QI_type_node;
15903   tree V4HI_type_node;
15904   tree V2SI_type_node;
15905   tree V2SF_type_node;
15906   tree V16QI_type_node;
15907   tree V8HI_type_node;
15908   tree V4SI_type_node;
15909   tree V4SF_type_node;
15910   tree V2DI_type_node;
15911
15912   tree intUQI_type_node;
15913   tree intUHI_type_node;
15914   tree intUSI_type_node;
15915   tree intUDI_type_node;
15916
15917   tree intEI_type_node;
15918   tree intOI_type_node;
15919   tree intCI_type_node;
15920   tree intXI_type_node;
15921
15922   tree V8QI_pointer_node;
15923   tree V4HI_pointer_node;
15924   tree V2SI_pointer_node;
15925   tree V2SF_pointer_node;
15926   tree V16QI_pointer_node;
15927   tree V8HI_pointer_node;
15928   tree V4SI_pointer_node;
15929   tree V4SF_pointer_node;
15930   tree V2DI_pointer_node;
15931
15932   tree void_ftype_pv8qi_v8qi_v8qi;
15933   tree void_ftype_pv4hi_v4hi_v4hi;
15934   tree void_ftype_pv2si_v2si_v2si;
15935   tree void_ftype_pv2sf_v2sf_v2sf;
15936   tree void_ftype_pdi_di_di;
15937   tree void_ftype_pv16qi_v16qi_v16qi;
15938   tree void_ftype_pv8hi_v8hi_v8hi;
15939   tree void_ftype_pv4si_v4si_v4si;
15940   tree void_ftype_pv4sf_v4sf_v4sf;
15941   tree void_ftype_pv2di_v2di_v2di;
15942
15943   tree reinterp_ftype_dreg[5][5];
15944   tree reinterp_ftype_qreg[5][5];
15945   tree dreg_types[5], qreg_types[5];
15946
15947   /* Create distinguished type nodes for NEON vector element types,
15948      and pointers to values of such types, so we can detect them later.  */
15949   neon_intQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
15950   neon_intHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
15951   neon_polyQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
15952   neon_polyHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
15953   neon_intSI_type_node = make_signed_type (GET_MODE_PRECISION (SImode));
15954   neon_intDI_type_node = make_signed_type (GET_MODE_PRECISION (DImode));
15955   neon_float_type_node = make_node (REAL_TYPE);
15956   TYPE_PRECISION (neon_float_type_node) = FLOAT_TYPE_SIZE;
15957   layout_type (neon_float_type_node);
15958
15959   /* Define typedefs which exactly correspond to the modes we are basing vector
15960      types on.  If you change these names you'll need to change
15961      the table used by arm_mangle_type too.  */
15962   (*lang_hooks.types.register_builtin_type) (neon_intQI_type_node,
15963                                              "__builtin_neon_qi");
15964   (*lang_hooks.types.register_builtin_type) (neon_intHI_type_node,
15965                                              "__builtin_neon_hi");
15966   (*lang_hooks.types.register_builtin_type) (neon_intSI_type_node,
15967                                              "__builtin_neon_si");
15968   (*lang_hooks.types.register_builtin_type) (neon_float_type_node,
15969                                              "__builtin_neon_sf");
15970   (*lang_hooks.types.register_builtin_type) (neon_intDI_type_node,
15971                                              "__builtin_neon_di");
15972   (*lang_hooks.types.register_builtin_type) (neon_polyQI_type_node,
15973                                              "__builtin_neon_poly8");
15974   (*lang_hooks.types.register_builtin_type) (neon_polyHI_type_node,
15975                                              "__builtin_neon_poly16");
15976
15977   intQI_pointer_node = build_pointer_type (neon_intQI_type_node);
15978   intHI_pointer_node = build_pointer_type (neon_intHI_type_node);
15979   intSI_pointer_node = build_pointer_type (neon_intSI_type_node);
15980   intDI_pointer_node = build_pointer_type (neon_intDI_type_node);
15981   float_pointer_node = build_pointer_type (neon_float_type_node);
15982
15983   /* Next create constant-qualified versions of the above types.  */
15984   const_intQI_node = build_qualified_type (neon_intQI_type_node,
15985                                            TYPE_QUAL_CONST);
15986   const_intHI_node = build_qualified_type (neon_intHI_type_node,
15987                                            TYPE_QUAL_CONST);
15988   const_intSI_node = build_qualified_type (neon_intSI_type_node,
15989                                            TYPE_QUAL_CONST);
15990   const_intDI_node = build_qualified_type (neon_intDI_type_node,
15991                                            TYPE_QUAL_CONST);
15992   const_float_node = build_qualified_type (neon_float_type_node,
15993                                            TYPE_QUAL_CONST);
15994
15995   const_intQI_pointer_node = build_pointer_type (const_intQI_node);
15996   const_intHI_pointer_node = build_pointer_type (const_intHI_node);
15997   const_intSI_pointer_node = build_pointer_type (const_intSI_node);
15998   const_intDI_pointer_node = build_pointer_type (const_intDI_node);
15999   const_float_pointer_node = build_pointer_type (const_float_node);
16000
16001   /* Now create vector types based on our NEON element types.  */
16002   /* 64-bit vectors.  */
16003   V8QI_type_node =
16004     build_vector_type_for_mode (neon_intQI_type_node, V8QImode);
16005   V4HI_type_node =
16006     build_vector_type_for_mode (neon_intHI_type_node, V4HImode);
16007   V2SI_type_node =
16008     build_vector_type_for_mode (neon_intSI_type_node, V2SImode);
16009   V2SF_type_node =
16010     build_vector_type_for_mode (neon_float_type_node, V2SFmode);
16011   /* 128-bit vectors.  */
16012   V16QI_type_node =
16013     build_vector_type_for_mode (neon_intQI_type_node, V16QImode);
16014   V8HI_type_node =
16015     build_vector_type_for_mode (neon_intHI_type_node, V8HImode);
16016   V4SI_type_node =
16017     build_vector_type_for_mode (neon_intSI_type_node, V4SImode);
16018   V4SF_type_node =
16019     build_vector_type_for_mode (neon_float_type_node, V4SFmode);
16020   V2DI_type_node =
16021     build_vector_type_for_mode (neon_intDI_type_node, V2DImode);
16022
16023   /* Unsigned integer types for various mode sizes.  */
16024   intUQI_type_node = make_unsigned_type (GET_MODE_PRECISION (QImode));
16025   intUHI_type_node = make_unsigned_type (GET_MODE_PRECISION (HImode));
16026   intUSI_type_node = make_unsigned_type (GET_MODE_PRECISION (SImode));
16027   intUDI_type_node = make_unsigned_type (GET_MODE_PRECISION (DImode));
16028
16029   (*lang_hooks.types.register_builtin_type) (intUQI_type_node,
16030                                              "__builtin_neon_uqi");
16031   (*lang_hooks.types.register_builtin_type) (intUHI_type_node,
16032                                              "__builtin_neon_uhi");
16033   (*lang_hooks.types.register_builtin_type) (intUSI_type_node,
16034                                              "__builtin_neon_usi");
16035   (*lang_hooks.types.register_builtin_type) (intUDI_type_node,
16036                                              "__builtin_neon_udi");
16037
16038   /* Opaque integer types for structures of vectors.  */
16039   intEI_type_node = make_signed_type (GET_MODE_PRECISION (EImode));
16040   intOI_type_node = make_signed_type (GET_MODE_PRECISION (OImode));
16041   intCI_type_node = make_signed_type (GET_MODE_PRECISION (CImode));
16042   intXI_type_node = make_signed_type (GET_MODE_PRECISION (XImode));
16043
16044   (*lang_hooks.types.register_builtin_type) (intTI_type_node,
16045                                              "__builtin_neon_ti");
16046   (*lang_hooks.types.register_builtin_type) (intEI_type_node,
16047                                              "__builtin_neon_ei");
16048   (*lang_hooks.types.register_builtin_type) (intOI_type_node,
16049                                              "__builtin_neon_oi");
16050   (*lang_hooks.types.register_builtin_type) (intCI_type_node,
16051                                              "__builtin_neon_ci");
16052   (*lang_hooks.types.register_builtin_type) (intXI_type_node,
16053                                              "__builtin_neon_xi");
16054
16055   /* Pointers to vector types.  */
16056   V8QI_pointer_node = build_pointer_type (V8QI_type_node);
16057   V4HI_pointer_node = build_pointer_type (V4HI_type_node);
16058   V2SI_pointer_node = build_pointer_type (V2SI_type_node);
16059   V2SF_pointer_node = build_pointer_type (V2SF_type_node);
16060   V16QI_pointer_node = build_pointer_type (V16QI_type_node);
16061   V8HI_pointer_node = build_pointer_type (V8HI_type_node);
16062   V4SI_pointer_node = build_pointer_type (V4SI_type_node);
16063   V4SF_pointer_node = build_pointer_type (V4SF_type_node);
16064   V2DI_pointer_node = build_pointer_type (V2DI_type_node);
16065
16066   /* Operations which return results as pairs.  */
16067   void_ftype_pv8qi_v8qi_v8qi =
16068     build_function_type_list (void_type_node, V8QI_pointer_node, V8QI_type_node,
16069                               V8QI_type_node, NULL);
16070   void_ftype_pv4hi_v4hi_v4hi =
16071     build_function_type_list (void_type_node, V4HI_pointer_node, V4HI_type_node,
16072                               V4HI_type_node, NULL);
16073   void_ftype_pv2si_v2si_v2si =
16074     build_function_type_list (void_type_node, V2SI_pointer_node, V2SI_type_node,
16075                               V2SI_type_node, NULL);
16076   void_ftype_pv2sf_v2sf_v2sf =
16077     build_function_type_list (void_type_node, V2SF_pointer_node, V2SF_type_node,
16078                               V2SF_type_node, NULL);
16079   void_ftype_pdi_di_di =
16080     build_function_type_list (void_type_node, intDI_pointer_node,
16081                               neon_intDI_type_node, neon_intDI_type_node, NULL);
16082   void_ftype_pv16qi_v16qi_v16qi =
16083     build_function_type_list (void_type_node, V16QI_pointer_node,
16084                               V16QI_type_node, V16QI_type_node, NULL);
16085   void_ftype_pv8hi_v8hi_v8hi =
16086     build_function_type_list (void_type_node, V8HI_pointer_node, V8HI_type_node,
16087                               V8HI_type_node, NULL);
16088   void_ftype_pv4si_v4si_v4si =
16089     build_function_type_list (void_type_node, V4SI_pointer_node, V4SI_type_node,
16090                               V4SI_type_node, NULL);
16091   void_ftype_pv4sf_v4sf_v4sf =
16092     build_function_type_list (void_type_node, V4SF_pointer_node, V4SF_type_node,
16093                               V4SF_type_node, NULL);
16094   void_ftype_pv2di_v2di_v2di =
16095     build_function_type_list (void_type_node, V2DI_pointer_node, V2DI_type_node,
16096                               V2DI_type_node, NULL);
16097
16098   dreg_types[0] = V8QI_type_node;
16099   dreg_types[1] = V4HI_type_node;
16100   dreg_types[2] = V2SI_type_node;
16101   dreg_types[3] = V2SF_type_node;
16102   dreg_types[4] = neon_intDI_type_node;
16103
16104   qreg_types[0] = V16QI_type_node;
16105   qreg_types[1] = V8HI_type_node;
16106   qreg_types[2] = V4SI_type_node;
16107   qreg_types[3] = V4SF_type_node;
16108   qreg_types[4] = V2DI_type_node;
16109
16110   for (i = 0; i < 5; i++)
16111     {
16112       int j;
16113       for (j = 0; j < 5; j++)
16114         {
16115           reinterp_ftype_dreg[i][j]
16116             = build_function_type_list (dreg_types[i], dreg_types[j], NULL);
16117           reinterp_ftype_qreg[i][j]
16118             = build_function_type_list (qreg_types[i], qreg_types[j], NULL);
16119         }
16120     }
16121
16122   for (i = 0; i < ARRAY_SIZE (neon_builtin_data); i++)
16123     {
16124       neon_builtin_datum *d = &neon_builtin_data[i];
16125       unsigned int j, codeidx = 0;
16126
16127       d->base_fcode = fcode;
16128
16129       for (j = 0; j < T_MAX; j++)
16130         {
16131           const char* const modenames[] = {
16132             "v8qi", "v4hi", "v2si", "v2sf", "di",
16133             "v16qi", "v8hi", "v4si", "v4sf", "v2di"
16134           };
16135           char namebuf[60];
16136           tree ftype = NULL;
16137           enum insn_code icode;
16138           int is_load = 0, is_store = 0;
16139
16140           if ((d->bits & (1 << j)) == 0)
16141             continue;
16142
16143           icode = d->codes[codeidx++];
16144
16145           switch (d->itype)
16146             {
16147             case NEON_LOAD1:
16148             case NEON_LOAD1LANE:
16149             case NEON_LOADSTRUCT:
16150             case NEON_LOADSTRUCTLANE:
16151               is_load = 1;
16152               /* Fall through.  */
16153             case NEON_STORE1:
16154             case NEON_STORE1LANE:
16155             case NEON_STORESTRUCT:
16156             case NEON_STORESTRUCTLANE:
16157               if (!is_load)
16158                 is_store = 1;
16159               /* Fall through.  */
16160             case NEON_UNOP:
16161             case NEON_BINOP:
16162             case NEON_LOGICBINOP:
16163             case NEON_SHIFTINSERT:
16164             case NEON_TERNOP:
16165             case NEON_GETLANE:
16166             case NEON_SETLANE:
16167             case NEON_CREATE:
16168             case NEON_DUP:
16169             case NEON_DUPLANE:
16170             case NEON_SHIFTIMM:
16171             case NEON_SHIFTACC:
16172             case NEON_COMBINE:
16173             case NEON_SPLIT:
16174             case NEON_CONVERT:
16175             case NEON_FIXCONV:
16176             case NEON_LANEMUL:
16177             case NEON_LANEMULL:
16178             case NEON_LANEMULH:
16179             case NEON_LANEMAC:
16180             case NEON_SCALARMUL:
16181             case NEON_SCALARMULL:
16182             case NEON_SCALARMULH:
16183             case NEON_SCALARMAC:
16184             case NEON_SELECT:
16185             case NEON_VTBL:
16186             case NEON_VTBX:
16187               {
16188                 int k;
16189                 tree return_type = void_type_node, args = void_list_node;
16190
16191                 /* Build a function type directly from the insn_data for this
16192                    builtin.  The build_function_type() function takes care of
16193                    removing duplicates for us.  */
16194                 for (k = insn_data[icode].n_operands - 1; k >= 0; k--)
16195                   {
16196                     tree eltype;
16197
16198                     if (is_load && k == 1)
16199                       {
16200                         /* Neon load patterns always have the memory operand
16201                            (a SImode pointer) in the operand 1 position.  We
16202                            want a const pointer to the element type in that
16203                            position.  */
16204                         gcc_assert (insn_data[icode].operand[k].mode == SImode);
16205
16206                         switch (1 << j)
16207                           {
16208                           case T_V8QI:
16209                           case T_V16QI:
16210                             eltype = const_intQI_pointer_node;
16211                             break;
16212
16213                           case T_V4HI:
16214                           case T_V8HI:
16215                             eltype = const_intHI_pointer_node;
16216                             break;
16217
16218                           case T_V2SI:
16219                           case T_V4SI:
16220                             eltype = const_intSI_pointer_node;
16221                             break;
16222
16223                           case T_V2SF:
16224                           case T_V4SF:
16225                             eltype = const_float_pointer_node;
16226                             break;
16227
16228                           case T_DI:
16229                           case T_V2DI:
16230                             eltype = const_intDI_pointer_node;
16231                             break;
16232
16233                           default: gcc_unreachable ();
16234                           }
16235                       }
16236                     else if (is_store && k == 0)
16237                       {
16238                         /* Similarly, Neon store patterns use operand 0 as
16239                            the memory location to store to (a SImode pointer).
16240                            Use a pointer to the element type of the store in
16241                            that position.  */
16242                         gcc_assert (insn_data[icode].operand[k].mode == SImode);
16243
16244                         switch (1 << j)
16245                           {
16246                           case T_V8QI:
16247                           case T_V16QI:
16248                             eltype = intQI_pointer_node;
16249                             break;
16250
16251                           case T_V4HI:
16252                           case T_V8HI:
16253                             eltype = intHI_pointer_node;
16254                             break;
16255
16256                           case T_V2SI:
16257                           case T_V4SI:
16258                             eltype = intSI_pointer_node;
16259                             break;
16260
16261                           case T_V2SF:
16262                           case T_V4SF:
16263                             eltype = float_pointer_node;
16264                             break;
16265
16266                           case T_DI:
16267                           case T_V2DI:
16268                             eltype = intDI_pointer_node;
16269                             break;
16270
16271                           default: gcc_unreachable ();
16272                           }
16273                       }
16274                     else
16275                       {
16276                         switch (insn_data[icode].operand[k].mode)
16277                           {
16278                           case VOIDmode: eltype = void_type_node; break;
16279                           /* Scalars.  */
16280                           case QImode: eltype = neon_intQI_type_node; break;
16281                           case HImode: eltype = neon_intHI_type_node; break;
16282                           case SImode: eltype = neon_intSI_type_node; break;
16283                           case SFmode: eltype = neon_float_type_node; break;
16284                           case DImode: eltype = neon_intDI_type_node; break;
16285                           case TImode: eltype = intTI_type_node; break;
16286                           case EImode: eltype = intEI_type_node; break;
16287                           case OImode: eltype = intOI_type_node; break;
16288                           case CImode: eltype = intCI_type_node; break;
16289                           case XImode: eltype = intXI_type_node; break;
16290                           /* 64-bit vectors.  */
16291                           case V8QImode: eltype = V8QI_type_node; break;
16292                           case V4HImode: eltype = V4HI_type_node; break;
16293                           case V2SImode: eltype = V2SI_type_node; break;
16294                           case V2SFmode: eltype = V2SF_type_node; break;
16295                           /* 128-bit vectors.  */
16296                           case V16QImode: eltype = V16QI_type_node; break;
16297                           case V8HImode: eltype = V8HI_type_node; break;
16298                           case V4SImode: eltype = V4SI_type_node; break;
16299                           case V4SFmode: eltype = V4SF_type_node; break;
16300                           case V2DImode: eltype = V2DI_type_node; break;
16301                           default: gcc_unreachable ();
16302                           }
16303                       }
16304
16305                     if (k == 0 && !is_store)
16306                       return_type = eltype;
16307                     else
16308                       args = tree_cons (NULL_TREE, eltype, args);
16309                   }
16310
16311                 ftype = build_function_type (return_type, args);
16312               }
16313               break;
16314
16315             case NEON_RESULTPAIR:
16316               {
16317                 switch (insn_data[icode].operand[1].mode)
16318                   {
16319                   case V8QImode: ftype = void_ftype_pv8qi_v8qi_v8qi; break;
16320                   case V4HImode: ftype = void_ftype_pv4hi_v4hi_v4hi; break;
16321                   case V2SImode: ftype = void_ftype_pv2si_v2si_v2si; break;
16322                   case V2SFmode: ftype = void_ftype_pv2sf_v2sf_v2sf; break;
16323                   case DImode: ftype = void_ftype_pdi_di_di; break;
16324                   case V16QImode: ftype = void_ftype_pv16qi_v16qi_v16qi; break;
16325                   case V8HImode: ftype = void_ftype_pv8hi_v8hi_v8hi; break;
16326                   case V4SImode: ftype = void_ftype_pv4si_v4si_v4si; break;
16327                   case V4SFmode: ftype = void_ftype_pv4sf_v4sf_v4sf; break;
16328                   case V2DImode: ftype = void_ftype_pv2di_v2di_v2di; break;
16329                   default: gcc_unreachable ();
16330                   }
16331               }
16332               break;
16333
16334             case NEON_REINTERP:
16335               {
16336                 /* We iterate over 5 doubleword types, then 5 quadword
16337                    types.  */
16338                 int rhs = j % 5;
16339                 switch (insn_data[icode].operand[0].mode)
16340                   {
16341                   case V8QImode: ftype = reinterp_ftype_dreg[0][rhs]; break;
16342                   case V4HImode: ftype = reinterp_ftype_dreg[1][rhs]; break;
16343                   case V2SImode: ftype = reinterp_ftype_dreg[2][rhs]; break;
16344                   case V2SFmode: ftype = reinterp_ftype_dreg[3][rhs]; break;
16345                   case DImode: ftype = reinterp_ftype_dreg[4][rhs]; break;
16346                   case V16QImode: ftype = reinterp_ftype_qreg[0][rhs]; break;
16347                   case V8HImode: ftype = reinterp_ftype_qreg[1][rhs]; break;
16348                   case V4SImode: ftype = reinterp_ftype_qreg[2][rhs]; break;
16349                   case V4SFmode: ftype = reinterp_ftype_qreg[3][rhs]; break;
16350                   case V2DImode: ftype = reinterp_ftype_qreg[4][rhs]; break;
16351                   default: gcc_unreachable ();
16352                   }
16353               }
16354               break;
16355
16356             default:
16357               gcc_unreachable ();
16358             }
16359
16360           gcc_assert (ftype != NULL);
16361
16362           sprintf (namebuf, "__builtin_neon_%s%s", d->name, modenames[j]);
16363
16364           add_builtin_function (namebuf, ftype, fcode++, BUILT_IN_MD, NULL,
16365                                 NULL_TREE);
16366         }
16367     }
16368 }
16369
16370 static void
16371 arm_init_fp16_builtins (void)
16372 {
16373   tree fp16_type = make_node (REAL_TYPE);
16374   TYPE_PRECISION (fp16_type) = 16;
16375   layout_type (fp16_type);
16376   (*lang_hooks.types.register_builtin_type) (fp16_type, "__fp16");
16377 }
16378
16379 static void
16380 arm_init_builtins (void)
16381 {
16382   arm_init_tls_builtins ();
16383
16384   if (TARGET_REALLY_IWMMXT)
16385     arm_init_iwmmxt_builtins ();
16386
16387   if (TARGET_NEON)
16388     arm_init_neon_builtins ();
16389
16390   if (arm_fp16_format)
16391     arm_init_fp16_builtins ();
16392 }
16393
16394 /* Implement TARGET_INVALID_PARAMETER_TYPE.  */
16395
16396 static const char *
16397 arm_invalid_parameter_type (const_tree t)
16398 {
16399   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
16400     return N_("function parameters cannot have __fp16 type");
16401   return NULL;
16402 }
16403
16404 /* Implement TARGET_INVALID_PARAMETER_TYPE.  */
16405
16406 static const char *
16407 arm_invalid_return_type (const_tree t)
16408 {
16409   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
16410     return N_("functions cannot return __fp16 type");
16411   return NULL;
16412 }
16413
16414 /* Implement TARGET_PROMOTED_TYPE.  */
16415
16416 static tree
16417 arm_promoted_type (const_tree t)
16418 {
16419   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
16420     return float_type_node;
16421   return NULL_TREE;
16422 }
16423
16424 /* Implement TARGET_CONVERT_TO_TYPE.
16425    Specifically, this hook implements the peculiarity of the ARM
16426    half-precision floating-point C semantics that requires conversions between
16427    __fp16 to or from double to do an intermediate conversion to float.  */
16428
16429 static tree
16430 arm_convert_to_type (tree type, tree expr)
16431 {
16432   tree fromtype = TREE_TYPE (expr);
16433   if (!SCALAR_FLOAT_TYPE_P (fromtype) || !SCALAR_FLOAT_TYPE_P (type))
16434     return NULL_TREE;
16435   if ((TYPE_PRECISION (fromtype) == 16 && TYPE_PRECISION (type) > 32)
16436       || (TYPE_PRECISION (type) == 16 && TYPE_PRECISION (fromtype) > 32))
16437     return convert (type, convert (float_type_node, expr));
16438   return NULL_TREE;
16439 }
16440
16441 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P.
16442    This simply adds HFmode as a supported mode; even though we don't
16443    implement arithmetic on this type directly, it's supported by
16444    optabs conversions, much the way the double-word arithmetic is
16445    special-cased in the default hook.  */
16446
16447 static bool
16448 arm_scalar_mode_supported_p (enum machine_mode mode)
16449 {
16450   if (mode == HFmode)
16451     return (arm_fp16_format != ARM_FP16_FORMAT_NONE);
16452   else
16453     return default_scalar_mode_supported_p (mode);
16454 }
16455
16456 /* Errors in the source file can cause expand_expr to return const0_rtx
16457    where we expect a vector.  To avoid crashing, use one of the vector
16458    clear instructions.  */
16459
16460 static rtx
16461 safe_vector_operand (rtx x, enum machine_mode mode)
16462 {
16463   if (x != const0_rtx)
16464     return x;
16465   x = gen_reg_rtx (mode);
16466
16467   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
16468                                : gen_rtx_SUBREG (DImode, x, 0)));
16469   return x;
16470 }
16471
16472 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
16473
16474 static rtx
16475 arm_expand_binop_builtin (enum insn_code icode,
16476                           tree exp, rtx target)
16477 {
16478   rtx pat;
16479   tree arg0 = CALL_EXPR_ARG (exp, 0);
16480   tree arg1 = CALL_EXPR_ARG (exp, 1);
16481   rtx op0 = expand_normal (arg0);
16482   rtx op1 = expand_normal (arg1);
16483   enum machine_mode tmode = insn_data[icode].operand[0].mode;
16484   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
16485   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
16486
16487   if (VECTOR_MODE_P (mode0))
16488     op0 = safe_vector_operand (op0, mode0);
16489   if (VECTOR_MODE_P (mode1))
16490     op1 = safe_vector_operand (op1, mode1);
16491
16492   if (! target
16493       || GET_MODE (target) != tmode
16494       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16495     target = gen_reg_rtx (tmode);
16496
16497   gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
16498
16499   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16500     op0 = copy_to_mode_reg (mode0, op0);
16501   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
16502     op1 = copy_to_mode_reg (mode1, op1);
16503
16504   pat = GEN_FCN (icode) (target, op0, op1);
16505   if (! pat)
16506     return 0;
16507   emit_insn (pat);
16508   return target;
16509 }
16510
16511 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
16512
16513 static rtx
16514 arm_expand_unop_builtin (enum insn_code icode,
16515                          tree exp, rtx target, int do_load)
16516 {
16517   rtx pat;
16518   tree arg0 = CALL_EXPR_ARG (exp, 0);
16519   rtx op0 = expand_normal (arg0);
16520   enum machine_mode tmode = insn_data[icode].operand[0].mode;
16521   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
16522
16523   if (! target
16524       || GET_MODE (target) != tmode
16525       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16526     target = gen_reg_rtx (tmode);
16527   if (do_load)
16528     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
16529   else
16530     {
16531       if (VECTOR_MODE_P (mode0))
16532         op0 = safe_vector_operand (op0, mode0);
16533
16534       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16535         op0 = copy_to_mode_reg (mode0, op0);
16536     }
16537
16538   pat = GEN_FCN (icode) (target, op0);
16539   if (! pat)
16540     return 0;
16541   emit_insn (pat);
16542   return target;
16543 }
16544
16545 static int
16546 neon_builtin_compare (const void *a, const void *b)
16547 {
16548   const neon_builtin_datum *const key = (const neon_builtin_datum *) a;
16549   const neon_builtin_datum *const memb = (const neon_builtin_datum *) b;
16550   unsigned int soughtcode = key->base_fcode;
16551
16552   if (soughtcode >= memb->base_fcode
16553       && soughtcode < memb->base_fcode + memb->num_vars)
16554     return 0;
16555   else if (soughtcode < memb->base_fcode)
16556     return -1;
16557   else
16558     return 1;
16559 }
16560
16561 static enum insn_code
16562 locate_neon_builtin_icode (int fcode, neon_itype *itype)
16563 {
16564   neon_builtin_datum key, *found;
16565   int idx;
16566
16567   key.base_fcode = fcode;
16568   found = (neon_builtin_datum *)
16569     bsearch (&key, &neon_builtin_data[0], ARRAY_SIZE (neon_builtin_data),
16570                    sizeof (neon_builtin_data[0]), neon_builtin_compare);
16571   gcc_assert (found);
16572   idx = fcode - (int) found->base_fcode;
16573   gcc_assert (idx >= 0 && idx < T_MAX && idx < (int)found->num_vars);
16574
16575   if (itype)
16576     *itype = found->itype;
16577
16578   return found->codes[idx];
16579 }
16580
16581 typedef enum {
16582   NEON_ARG_COPY_TO_REG,
16583   NEON_ARG_CONSTANT,
16584   NEON_ARG_STOP
16585 } builtin_arg;
16586
16587 #define NEON_MAX_BUILTIN_ARGS 5
16588
16589 /* Expand a Neon builtin.  */
16590 static rtx
16591 arm_expand_neon_args (rtx target, int icode, int have_retval,
16592                       tree exp, ...)
16593 {
16594   va_list ap;
16595   rtx pat;
16596   tree arg[NEON_MAX_BUILTIN_ARGS];
16597   rtx op[NEON_MAX_BUILTIN_ARGS];
16598   enum machine_mode tmode = insn_data[icode].operand[0].mode;
16599   enum machine_mode mode[NEON_MAX_BUILTIN_ARGS];
16600   int argc = 0;
16601
16602   if (have_retval
16603       && (!target
16604           || GET_MODE (target) != tmode
16605           || !(*insn_data[icode].operand[0].predicate) (target, tmode)))
16606     target = gen_reg_rtx (tmode);
16607
16608   va_start (ap, exp);
16609
16610   for (;;)
16611     {
16612       builtin_arg thisarg = (builtin_arg) va_arg (ap, int);
16613
16614       if (thisarg == NEON_ARG_STOP)
16615         break;
16616       else
16617         {
16618           arg[argc] = CALL_EXPR_ARG (exp, argc);
16619           op[argc] = expand_normal (arg[argc]);
16620           mode[argc] = insn_data[icode].operand[argc + have_retval].mode;
16621
16622           switch (thisarg)
16623             {
16624             case NEON_ARG_COPY_TO_REG:
16625               /*gcc_assert (GET_MODE (op[argc]) == mode[argc]);*/
16626               if (!(*insn_data[icode].operand[argc + have_retval].predicate)
16627                      (op[argc], mode[argc]))
16628                 op[argc] = copy_to_mode_reg (mode[argc], op[argc]);
16629               break;
16630
16631             case NEON_ARG_CONSTANT:
16632               /* FIXME: This error message is somewhat unhelpful.  */
16633               if (!(*insn_data[icode].operand[argc + have_retval].predicate)
16634                     (op[argc], mode[argc]))
16635                 error ("argument must be a constant");
16636               break;
16637
16638             case NEON_ARG_STOP:
16639               gcc_unreachable ();
16640             }
16641
16642           argc++;
16643         }
16644     }
16645
16646   va_end (ap);
16647
16648   if (have_retval)
16649     switch (argc)
16650       {
16651       case 1:
16652         pat = GEN_FCN (icode) (target, op[0]);
16653         break;
16654
16655       case 2:
16656         pat = GEN_FCN (icode) (target, op[0], op[1]);
16657         break;
16658
16659       case 3:
16660         pat = GEN_FCN (icode) (target, op[0], op[1], op[2]);
16661         break;
16662
16663       case 4:
16664         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3]);
16665         break;
16666
16667       case 5:
16668         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3], op[4]);
16669         break;
16670
16671       default:
16672         gcc_unreachable ();
16673       }
16674   else
16675     switch (argc)
16676       {
16677       case 1:
16678         pat = GEN_FCN (icode) (op[0]);
16679         break;
16680
16681       case 2:
16682         pat = GEN_FCN (icode) (op[0], op[1]);
16683         break;
16684
16685       case 3:
16686         pat = GEN_FCN (icode) (op[0], op[1], op[2]);
16687         break;
16688
16689       case 4:
16690         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
16691         break;
16692
16693       case 5:
16694         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4]);
16695         break;
16696
16697       default:
16698         gcc_unreachable ();
16699       }
16700
16701   if (!pat)
16702     return 0;
16703
16704   emit_insn (pat);
16705
16706   return target;
16707 }
16708
16709 /* Expand a Neon builtin. These are "special" because they don't have symbolic
16710    constants defined per-instruction or per instruction-variant. Instead, the
16711    required info is looked up in the table neon_builtin_data.  */
16712 static rtx
16713 arm_expand_neon_builtin (int fcode, tree exp, rtx target)
16714 {
16715   neon_itype itype;
16716   enum insn_code icode = locate_neon_builtin_icode (fcode, &itype);
16717
16718   switch (itype)
16719     {
16720     case NEON_UNOP:
16721     case NEON_CONVERT:
16722     case NEON_DUPLANE:
16723       return arm_expand_neon_args (target, icode, 1, exp,
16724         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_STOP);
16725
16726     case NEON_BINOP:
16727     case NEON_SETLANE:
16728     case NEON_SCALARMUL:
16729     case NEON_SCALARMULL:
16730     case NEON_SCALARMULH:
16731     case NEON_SHIFTINSERT:
16732     case NEON_LOGICBINOP:
16733       return arm_expand_neon_args (target, icode, 1, exp,
16734         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
16735         NEON_ARG_STOP);
16736
16737     case NEON_TERNOP:
16738       return arm_expand_neon_args (target, icode, 1, exp,
16739         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
16740         NEON_ARG_CONSTANT, NEON_ARG_STOP);
16741
16742     case NEON_GETLANE:
16743     case NEON_FIXCONV:
16744     case NEON_SHIFTIMM:
16745       return arm_expand_neon_args (target, icode, 1, exp,
16746         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_CONSTANT,
16747         NEON_ARG_STOP);
16748
16749     case NEON_CREATE:
16750       return arm_expand_neon_args (target, icode, 1, exp,
16751         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
16752
16753     case NEON_DUP:
16754     case NEON_SPLIT:
16755     case NEON_REINTERP:
16756       return arm_expand_neon_args (target, icode, 1, exp,
16757         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
16758
16759     case NEON_COMBINE:
16760     case NEON_VTBL:
16761       return arm_expand_neon_args (target, icode, 1, exp,
16762         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
16763
16764     case NEON_RESULTPAIR:
16765       return arm_expand_neon_args (target, icode, 0, exp,
16766         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
16767         NEON_ARG_STOP);
16768
16769     case NEON_LANEMUL:
16770     case NEON_LANEMULL:
16771     case NEON_LANEMULH:
16772       return arm_expand_neon_args (target, icode, 1, exp,
16773         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
16774         NEON_ARG_CONSTANT, NEON_ARG_STOP);
16775
16776     case NEON_LANEMAC:
16777       return arm_expand_neon_args (target, icode, 1, exp,
16778         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
16779         NEON_ARG_CONSTANT, NEON_ARG_CONSTANT, NEON_ARG_STOP);
16780
16781     case NEON_SHIFTACC:
16782       return arm_expand_neon_args (target, icode, 1, exp,
16783         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
16784         NEON_ARG_CONSTANT, NEON_ARG_STOP);
16785
16786     case NEON_SCALARMAC:
16787       return arm_expand_neon_args (target, icode, 1, exp,
16788         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
16789         NEON_ARG_CONSTANT, NEON_ARG_STOP);
16790
16791     case NEON_SELECT:
16792     case NEON_VTBX:
16793       return arm_expand_neon_args (target, icode, 1, exp,
16794         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
16795         NEON_ARG_STOP);
16796
16797     case NEON_LOAD1:
16798     case NEON_LOADSTRUCT:
16799       return arm_expand_neon_args (target, icode, 1, exp,
16800         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
16801
16802     case NEON_LOAD1LANE:
16803     case NEON_LOADSTRUCTLANE:
16804       return arm_expand_neon_args (target, icode, 1, exp,
16805         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
16806         NEON_ARG_STOP);
16807
16808     case NEON_STORE1:
16809     case NEON_STORESTRUCT:
16810       return arm_expand_neon_args (target, icode, 0, exp,
16811         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
16812
16813     case NEON_STORE1LANE:
16814     case NEON_STORESTRUCTLANE:
16815       return arm_expand_neon_args (target, icode, 0, exp,
16816         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
16817         NEON_ARG_STOP);
16818     }
16819
16820   gcc_unreachable ();
16821 }
16822
16823 /* Emit code to reinterpret one Neon type as another, without altering bits.  */
16824 void
16825 neon_reinterpret (rtx dest, rtx src)
16826 {
16827   emit_move_insn (dest, gen_lowpart (GET_MODE (dest), src));
16828 }
16829
16830 /* Emit code to place a Neon pair result in memory locations (with equal
16831    registers).  */
16832 void
16833 neon_emit_pair_result_insn (enum machine_mode mode,
16834                             rtx (*intfn) (rtx, rtx, rtx, rtx), rtx destaddr,
16835                             rtx op1, rtx op2)
16836 {
16837   rtx mem = gen_rtx_MEM (mode, destaddr);
16838   rtx tmp1 = gen_reg_rtx (mode);
16839   rtx tmp2 = gen_reg_rtx (mode);
16840
16841   emit_insn (intfn (tmp1, op1, tmp2, op2));
16842
16843   emit_move_insn (mem, tmp1);
16844   mem = adjust_address (mem, mode, GET_MODE_SIZE (mode));
16845   emit_move_insn (mem, tmp2);
16846 }
16847
16848 /* Set up operands for a register copy from src to dest, taking care not to
16849    clobber registers in the process.
16850    FIXME: This has rather high polynomial complexity (O(n^3)?) but shouldn't
16851    be called with a large N, so that should be OK.  */
16852
16853 void
16854 neon_disambiguate_copy (rtx *operands, rtx *dest, rtx *src, unsigned int count)
16855 {
16856   unsigned int copied = 0, opctr = 0;
16857   unsigned int done = (1 << count) - 1;
16858   unsigned int i, j;
16859
16860   while (copied != done)
16861     {
16862       for (i = 0; i < count; i++)
16863         {
16864           int good = 1;
16865
16866           for (j = 0; good && j < count; j++)
16867             if (i != j && (copied & (1 << j)) == 0
16868                 && reg_overlap_mentioned_p (src[j], dest[i]))
16869               good = 0;
16870
16871           if (good)
16872             {
16873               operands[opctr++] = dest[i];
16874               operands[opctr++] = src[i];
16875               copied |= 1 << i;
16876             }
16877         }
16878     }
16879
16880   gcc_assert (opctr == count * 2);
16881 }
16882
16883 /* Expand an expression EXP that calls a built-in function,
16884    with result going to TARGET if that's convenient
16885    (and in mode MODE if that's convenient).
16886    SUBTARGET may be used as the target for computing one of EXP's operands.
16887    IGNORE is nonzero if the value is to be ignored.  */
16888
16889 static rtx
16890 arm_expand_builtin (tree exp,
16891                     rtx target,
16892                     rtx subtarget ATTRIBUTE_UNUSED,
16893                     enum machine_mode mode ATTRIBUTE_UNUSED,
16894                     int ignore ATTRIBUTE_UNUSED)
16895 {
16896   const struct builtin_description * d;
16897   enum insn_code    icode;
16898   tree              fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
16899   tree              arg0;
16900   tree              arg1;
16901   tree              arg2;
16902   rtx               op0;
16903   rtx               op1;
16904   rtx               op2;
16905   rtx               pat;
16906   int               fcode = DECL_FUNCTION_CODE (fndecl);
16907   size_t            i;
16908   enum machine_mode tmode;
16909   enum machine_mode mode0;
16910   enum machine_mode mode1;
16911   enum machine_mode mode2;
16912
16913   if (fcode >= ARM_BUILTIN_NEON_BASE)
16914     return arm_expand_neon_builtin (fcode, exp, target);
16915
16916   switch (fcode)
16917     {
16918     case ARM_BUILTIN_TEXTRMSB:
16919     case ARM_BUILTIN_TEXTRMUB:
16920     case ARM_BUILTIN_TEXTRMSH:
16921     case ARM_BUILTIN_TEXTRMUH:
16922     case ARM_BUILTIN_TEXTRMSW:
16923     case ARM_BUILTIN_TEXTRMUW:
16924       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
16925                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
16926                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
16927                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
16928                : CODE_FOR_iwmmxt_textrmw);
16929
16930       arg0 = CALL_EXPR_ARG (exp, 0);
16931       arg1 = CALL_EXPR_ARG (exp, 1);
16932       op0 = expand_normal (arg0);
16933       op1 = expand_normal (arg1);
16934       tmode = insn_data[icode].operand[0].mode;
16935       mode0 = insn_data[icode].operand[1].mode;
16936       mode1 = insn_data[icode].operand[2].mode;
16937
16938       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16939         op0 = copy_to_mode_reg (mode0, op0);
16940       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
16941         {
16942           /* @@@ better error message */
16943           error ("selector must be an immediate");
16944           return gen_reg_rtx (tmode);
16945         }
16946       if (target == 0
16947           || GET_MODE (target) != tmode
16948           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16949         target = gen_reg_rtx (tmode);
16950       pat = GEN_FCN (icode) (target, op0, op1);
16951       if (! pat)
16952         return 0;
16953       emit_insn (pat);
16954       return target;
16955
16956     case ARM_BUILTIN_TINSRB:
16957     case ARM_BUILTIN_TINSRH:
16958     case ARM_BUILTIN_TINSRW:
16959       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
16960                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
16961                : CODE_FOR_iwmmxt_tinsrw);
16962       arg0 = CALL_EXPR_ARG (exp, 0);
16963       arg1 = CALL_EXPR_ARG (exp, 1);
16964       arg2 = CALL_EXPR_ARG (exp, 2);
16965       op0 = expand_normal (arg0);
16966       op1 = expand_normal (arg1);
16967       op2 = expand_normal (arg2);
16968       tmode = insn_data[icode].operand[0].mode;
16969       mode0 = insn_data[icode].operand[1].mode;
16970       mode1 = insn_data[icode].operand[2].mode;
16971       mode2 = insn_data[icode].operand[3].mode;
16972
16973       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16974         op0 = copy_to_mode_reg (mode0, op0);
16975       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
16976         op1 = copy_to_mode_reg (mode1, op1);
16977       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
16978         {
16979           /* @@@ better error message */
16980           error ("selector must be an immediate");
16981           return const0_rtx;
16982         }
16983       if (target == 0
16984           || GET_MODE (target) != tmode
16985           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16986         target = gen_reg_rtx (tmode);
16987       pat = GEN_FCN (icode) (target, op0, op1, op2);
16988       if (! pat)
16989         return 0;
16990       emit_insn (pat);
16991       return target;
16992
16993     case ARM_BUILTIN_SETWCX:
16994       arg0 = CALL_EXPR_ARG (exp, 0);
16995       arg1 = CALL_EXPR_ARG (exp, 1);
16996       op0 = force_reg (SImode, expand_normal (arg0));
16997       op1 = expand_normal (arg1);
16998       emit_insn (gen_iwmmxt_tmcr (op1, op0));
16999       return 0;
17000
17001     case ARM_BUILTIN_GETWCX:
17002       arg0 = CALL_EXPR_ARG (exp, 0);
17003       op0 = expand_normal (arg0);
17004       target = gen_reg_rtx (SImode);
17005       emit_insn (gen_iwmmxt_tmrc (target, op0));
17006       return target;
17007
17008     case ARM_BUILTIN_WSHUFH:
17009       icode = CODE_FOR_iwmmxt_wshufh;
17010       arg0 = CALL_EXPR_ARG (exp, 0);
17011       arg1 = CALL_EXPR_ARG (exp, 1);
17012       op0 = expand_normal (arg0);
17013       op1 = expand_normal (arg1);
17014       tmode = insn_data[icode].operand[0].mode;
17015       mode1 = insn_data[icode].operand[1].mode;
17016       mode2 = insn_data[icode].operand[2].mode;
17017
17018       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
17019         op0 = copy_to_mode_reg (mode1, op0);
17020       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
17021         {
17022           /* @@@ better error message */
17023           error ("mask must be an immediate");
17024           return const0_rtx;
17025         }
17026       if (target == 0
17027           || GET_MODE (target) != tmode
17028           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17029         target = gen_reg_rtx (tmode);
17030       pat = GEN_FCN (icode) (target, op0, op1);
17031       if (! pat)
17032         return 0;
17033       emit_insn (pat);
17034       return target;
17035
17036     case ARM_BUILTIN_WSADB:
17037       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, exp, target);
17038     case ARM_BUILTIN_WSADH:
17039       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, exp, target);
17040     case ARM_BUILTIN_WSADBZ:
17041       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, exp, target);
17042     case ARM_BUILTIN_WSADHZ:
17043       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, exp, target);
17044
17045       /* Several three-argument builtins.  */
17046     case ARM_BUILTIN_WMACS:
17047     case ARM_BUILTIN_WMACU:
17048     case ARM_BUILTIN_WALIGN:
17049     case ARM_BUILTIN_TMIA:
17050     case ARM_BUILTIN_TMIAPH:
17051     case ARM_BUILTIN_TMIATT:
17052     case ARM_BUILTIN_TMIATB:
17053     case ARM_BUILTIN_TMIABT:
17054     case ARM_BUILTIN_TMIABB:
17055       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
17056                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
17057                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
17058                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
17059                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
17060                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
17061                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
17062                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
17063                : CODE_FOR_iwmmxt_walign);
17064       arg0 = CALL_EXPR_ARG (exp, 0);
17065       arg1 = CALL_EXPR_ARG (exp, 1);
17066       arg2 = CALL_EXPR_ARG (exp, 2);
17067       op0 = expand_normal (arg0);
17068       op1 = expand_normal (arg1);
17069       op2 = expand_normal (arg2);
17070       tmode = insn_data[icode].operand[0].mode;
17071       mode0 = insn_data[icode].operand[1].mode;
17072       mode1 = insn_data[icode].operand[2].mode;
17073       mode2 = insn_data[icode].operand[3].mode;
17074
17075       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
17076         op0 = copy_to_mode_reg (mode0, op0);
17077       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
17078         op1 = copy_to_mode_reg (mode1, op1);
17079       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
17080         op2 = copy_to_mode_reg (mode2, op2);
17081       if (target == 0
17082           || GET_MODE (target) != tmode
17083           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17084         target = gen_reg_rtx (tmode);
17085       pat = GEN_FCN (icode) (target, op0, op1, op2);
17086       if (! pat)
17087         return 0;
17088       emit_insn (pat);
17089       return target;
17090
17091     case ARM_BUILTIN_WZERO:
17092       target = gen_reg_rtx (DImode);
17093       emit_insn (gen_iwmmxt_clrdi (target));
17094       return target;
17095
17096     case ARM_BUILTIN_THREAD_POINTER:
17097       return arm_load_tp (target);
17098
17099     default:
17100       break;
17101     }
17102
17103   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
17104     if (d->code == (const enum arm_builtins) fcode)
17105       return arm_expand_binop_builtin (d->icode, exp, target);
17106
17107   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
17108     if (d->code == (const enum arm_builtins) fcode)
17109       return arm_expand_unop_builtin (d->icode, exp, target, 0);
17110
17111   /* @@@ Should really do something sensible here.  */
17112   return NULL_RTX;
17113 }
17114 \f
17115 /* Return the number (counting from 0) of
17116    the least significant set bit in MASK.  */
17117
17118 inline static int
17119 number_of_first_bit_set (unsigned mask)
17120 {
17121   int bit;
17122
17123   for (bit = 0;
17124        (mask & (1 << bit)) == 0;
17125        ++bit)
17126     continue;
17127
17128   return bit;
17129 }
17130
17131 /* Emit code to push or pop registers to or from the stack.  F is the
17132    assembly file.  MASK is the registers to push or pop.  PUSH is
17133    nonzero if we should push, and zero if we should pop.  For debugging
17134    output, if pushing, adjust CFA_OFFSET by the amount of space added
17135    to the stack.  REAL_REGS should have the same number of bits set as
17136    MASK, and will be used instead (in the same order) to describe which
17137    registers were saved - this is used to mark the save slots when we
17138    push high registers after moving them to low registers.  */
17139 static void
17140 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
17141                unsigned long real_regs)
17142 {
17143   int regno;
17144   int lo_mask = mask & 0xFF;
17145   int pushed_words = 0;
17146
17147   gcc_assert (mask);
17148
17149   if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
17150     {
17151       /* Special case.  Do not generate a POP PC statement here, do it in
17152          thumb_exit() */
17153       thumb_exit (f, -1);
17154       return;
17155     }
17156
17157   if (ARM_EABI_UNWIND_TABLES && push)
17158     {
17159       fprintf (f, "\t.save\t{");
17160       for (regno = 0; regno < 15; regno++)
17161         {
17162           if (real_regs & (1 << regno))
17163             {
17164               if (real_regs & ((1 << regno) -1))
17165                 fprintf (f, ", ");
17166               asm_fprintf (f, "%r", regno);
17167             }
17168         }
17169       fprintf (f, "}\n");
17170     }
17171
17172   fprintf (f, "\t%s\t{", push ? "push" : "pop");
17173
17174   /* Look at the low registers first.  */
17175   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
17176     {
17177       if (lo_mask & 1)
17178         {
17179           asm_fprintf (f, "%r", regno);
17180
17181           if ((lo_mask & ~1) != 0)
17182             fprintf (f, ", ");
17183
17184           pushed_words++;
17185         }
17186     }
17187
17188   if (push && (mask & (1 << LR_REGNUM)))
17189     {
17190       /* Catch pushing the LR.  */
17191       if (mask & 0xFF)
17192         fprintf (f, ", ");
17193
17194       asm_fprintf (f, "%r", LR_REGNUM);
17195
17196       pushed_words++;
17197     }
17198   else if (!push && (mask & (1 << PC_REGNUM)))
17199     {
17200       /* Catch popping the PC.  */
17201       if (TARGET_INTERWORK || TARGET_BACKTRACE
17202           || crtl->calls_eh_return)
17203         {
17204           /* The PC is never poped directly, instead
17205              it is popped into r3 and then BX is used.  */
17206           fprintf (f, "}\n");
17207
17208           thumb_exit (f, -1);
17209
17210           return;
17211         }
17212       else
17213         {
17214           if (mask & 0xFF)
17215             fprintf (f, ", ");
17216
17217           asm_fprintf (f, "%r", PC_REGNUM);
17218         }
17219     }
17220
17221   fprintf (f, "}\n");
17222
17223   if (push && pushed_words && dwarf2out_do_frame ())
17224     {
17225       char *l = dwarf2out_cfi_label (false);
17226       int pushed_mask = real_regs;
17227
17228       *cfa_offset += pushed_words * 4;
17229       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
17230
17231       pushed_words = 0;
17232       pushed_mask = real_regs;
17233       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
17234         {
17235           if (pushed_mask & 1)
17236             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
17237         }
17238     }
17239 }
17240
17241 /* Generate code to return from a thumb function.
17242    If 'reg_containing_return_addr' is -1, then the return address is
17243    actually on the stack, at the stack pointer.  */
17244 static void
17245 thumb_exit (FILE *f, int reg_containing_return_addr)
17246 {
17247   unsigned regs_available_for_popping;
17248   unsigned regs_to_pop;
17249   int pops_needed;
17250   unsigned available;
17251   unsigned required;
17252   int mode;
17253   int size;
17254   int restore_a4 = FALSE;
17255
17256   /* Compute the registers we need to pop.  */
17257   regs_to_pop = 0;
17258   pops_needed = 0;
17259
17260   if (reg_containing_return_addr == -1)
17261     {
17262       regs_to_pop |= 1 << LR_REGNUM;
17263       ++pops_needed;
17264     }
17265
17266   if (TARGET_BACKTRACE)
17267     {
17268       /* Restore the (ARM) frame pointer and stack pointer.  */
17269       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
17270       pops_needed += 2;
17271     }
17272
17273   /* If there is nothing to pop then just emit the BX instruction and
17274      return.  */
17275   if (pops_needed == 0)
17276     {
17277       if (crtl->calls_eh_return)
17278         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
17279
17280       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
17281       return;
17282     }
17283   /* Otherwise if we are not supporting interworking and we have not created
17284      a backtrace structure and the function was not entered in ARM mode then
17285      just pop the return address straight into the PC.  */
17286   else if (!TARGET_INTERWORK
17287            && !TARGET_BACKTRACE
17288            && !is_called_in_ARM_mode (current_function_decl)
17289            && !crtl->calls_eh_return)
17290     {
17291       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
17292       return;
17293     }
17294
17295   /* Find out how many of the (return) argument registers we can corrupt.  */
17296   regs_available_for_popping = 0;
17297
17298   /* If returning via __builtin_eh_return, the bottom three registers
17299      all contain information needed for the return.  */
17300   if (crtl->calls_eh_return)
17301     size = 12;
17302   else
17303     {
17304       /* If we can deduce the registers used from the function's
17305          return value.  This is more reliable that examining
17306          df_regs_ever_live_p () because that will be set if the register is
17307          ever used in the function, not just if the register is used
17308          to hold a return value.  */
17309
17310       if (crtl->return_rtx != 0)
17311         mode = GET_MODE (crtl->return_rtx);
17312       else
17313         mode = DECL_MODE (DECL_RESULT (current_function_decl));
17314
17315       size = GET_MODE_SIZE (mode);
17316
17317       if (size == 0)
17318         {
17319           /* In a void function we can use any argument register.
17320              In a function that returns a structure on the stack
17321              we can use the second and third argument registers.  */
17322           if (mode == VOIDmode)
17323             regs_available_for_popping =
17324               (1 << ARG_REGISTER (1))
17325               | (1 << ARG_REGISTER (2))
17326               | (1 << ARG_REGISTER (3));
17327           else
17328             regs_available_for_popping =
17329               (1 << ARG_REGISTER (2))
17330               | (1 << ARG_REGISTER (3));
17331         }
17332       else if (size <= 4)
17333         regs_available_for_popping =
17334           (1 << ARG_REGISTER (2))
17335           | (1 << ARG_REGISTER (3));
17336       else if (size <= 8)
17337         regs_available_for_popping =
17338           (1 << ARG_REGISTER (3));
17339     }
17340
17341   /* Match registers to be popped with registers into which we pop them.  */
17342   for (available = regs_available_for_popping,
17343        required  = regs_to_pop;
17344        required != 0 && available != 0;
17345        available &= ~(available & - available),
17346        required  &= ~(required  & - required))
17347     -- pops_needed;
17348
17349   /* If we have any popping registers left over, remove them.  */
17350   if (available > 0)
17351     regs_available_for_popping &= ~available;
17352
17353   /* Otherwise if we need another popping register we can use
17354      the fourth argument register.  */
17355   else if (pops_needed)
17356     {
17357       /* If we have not found any free argument registers and
17358          reg a4 contains the return address, we must move it.  */
17359       if (regs_available_for_popping == 0
17360           && reg_containing_return_addr == LAST_ARG_REGNUM)
17361         {
17362           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
17363           reg_containing_return_addr = LR_REGNUM;
17364         }
17365       else if (size > 12)
17366         {
17367           /* Register a4 is being used to hold part of the return value,
17368              but we have dire need of a free, low register.  */
17369           restore_a4 = TRUE;
17370
17371           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
17372         }
17373
17374       if (reg_containing_return_addr != LAST_ARG_REGNUM)
17375         {
17376           /* The fourth argument register is available.  */
17377           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
17378
17379           --pops_needed;
17380         }
17381     }
17382
17383   /* Pop as many registers as we can.  */
17384   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
17385                  regs_available_for_popping);
17386
17387   /* Process the registers we popped.  */
17388   if (reg_containing_return_addr == -1)
17389     {
17390       /* The return address was popped into the lowest numbered register.  */
17391       regs_to_pop &= ~(1 << LR_REGNUM);
17392
17393       reg_containing_return_addr =
17394         number_of_first_bit_set (regs_available_for_popping);
17395
17396       /* Remove this register for the mask of available registers, so that
17397          the return address will not be corrupted by further pops.  */
17398       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
17399     }
17400
17401   /* If we popped other registers then handle them here.  */
17402   if (regs_available_for_popping)
17403     {
17404       int frame_pointer;
17405
17406       /* Work out which register currently contains the frame pointer.  */
17407       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
17408
17409       /* Move it into the correct place.  */
17410       asm_fprintf (f, "\tmov\t%r, %r\n",
17411                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
17412
17413       /* (Temporarily) remove it from the mask of popped registers.  */
17414       regs_available_for_popping &= ~(1 << frame_pointer);
17415       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
17416
17417       if (regs_available_for_popping)
17418         {
17419           int stack_pointer;
17420
17421           /* We popped the stack pointer as well,
17422              find the register that contains it.  */
17423           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
17424
17425           /* Move it into the stack register.  */
17426           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
17427
17428           /* At this point we have popped all necessary registers, so
17429              do not worry about restoring regs_available_for_popping
17430              to its correct value:
17431
17432              assert (pops_needed == 0)
17433              assert (regs_available_for_popping == (1 << frame_pointer))
17434              assert (regs_to_pop == (1 << STACK_POINTER))  */
17435         }
17436       else
17437         {
17438           /* Since we have just move the popped value into the frame
17439              pointer, the popping register is available for reuse, and
17440              we know that we still have the stack pointer left to pop.  */
17441           regs_available_for_popping |= (1 << frame_pointer);
17442         }
17443     }
17444
17445   /* If we still have registers left on the stack, but we no longer have
17446      any registers into which we can pop them, then we must move the return
17447      address into the link register and make available the register that
17448      contained it.  */
17449   if (regs_available_for_popping == 0 && pops_needed > 0)
17450     {
17451       regs_available_for_popping |= 1 << reg_containing_return_addr;
17452
17453       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
17454                    reg_containing_return_addr);
17455
17456       reg_containing_return_addr = LR_REGNUM;
17457     }
17458
17459   /* If we have registers left on the stack then pop some more.
17460      We know that at most we will want to pop FP and SP.  */
17461   if (pops_needed > 0)
17462     {
17463       int  popped_into;
17464       int  move_to;
17465
17466       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
17467                      regs_available_for_popping);
17468
17469       /* We have popped either FP or SP.
17470          Move whichever one it is into the correct register.  */
17471       popped_into = number_of_first_bit_set (regs_available_for_popping);
17472       move_to     = number_of_first_bit_set (regs_to_pop);
17473
17474       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
17475
17476       regs_to_pop &= ~(1 << move_to);
17477
17478       --pops_needed;
17479     }
17480
17481   /* If we still have not popped everything then we must have only
17482      had one register available to us and we are now popping the SP.  */
17483   if (pops_needed > 0)
17484     {
17485       int  popped_into;
17486
17487       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
17488                      regs_available_for_popping);
17489
17490       popped_into = number_of_first_bit_set (regs_available_for_popping);
17491
17492       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
17493       /*
17494         assert (regs_to_pop == (1 << STACK_POINTER))
17495         assert (pops_needed == 1)
17496       */
17497     }
17498
17499   /* If necessary restore the a4 register.  */
17500   if (restore_a4)
17501     {
17502       if (reg_containing_return_addr != LR_REGNUM)
17503         {
17504           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
17505           reg_containing_return_addr = LR_REGNUM;
17506         }
17507
17508       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
17509     }
17510
17511   if (crtl->calls_eh_return)
17512     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
17513
17514   /* Return to caller.  */
17515   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
17516 }
17517
17518 \f
17519 void
17520 thumb1_final_prescan_insn (rtx insn)
17521 {
17522   if (flag_print_asm_name)
17523     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
17524                  INSN_ADDRESSES (INSN_UID (insn)));
17525 }
17526
17527 int
17528 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
17529 {
17530   unsigned HOST_WIDE_INT mask = 0xff;
17531   int i;
17532
17533   val = val & (unsigned HOST_WIDE_INT)0xffffffffu;
17534   if (val == 0) /* XXX */
17535     return 0;
17536
17537   for (i = 0; i < 25; i++)
17538     if ((val & (mask << i)) == val)
17539       return 1;
17540
17541   return 0;
17542 }
17543
17544 /* Returns nonzero if the current function contains,
17545    or might contain a far jump.  */
17546 static int
17547 thumb_far_jump_used_p (void)
17548 {
17549   rtx insn;
17550
17551   /* This test is only important for leaf functions.  */
17552   /* assert (!leaf_function_p ()); */
17553
17554   /* If we have already decided that far jumps may be used,
17555      do not bother checking again, and always return true even if
17556      it turns out that they are not being used.  Once we have made
17557      the decision that far jumps are present (and that hence the link
17558      register will be pushed onto the stack) we cannot go back on it.  */
17559   if (cfun->machine->far_jump_used)
17560     return 1;
17561
17562   /* If this function is not being called from the prologue/epilogue
17563      generation code then it must be being called from the
17564      INITIAL_ELIMINATION_OFFSET macro.  */
17565   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
17566     {
17567       /* In this case we know that we are being asked about the elimination
17568          of the arg pointer register.  If that register is not being used,
17569          then there are no arguments on the stack, and we do not have to
17570          worry that a far jump might force the prologue to push the link
17571          register, changing the stack offsets.  In this case we can just
17572          return false, since the presence of far jumps in the function will
17573          not affect stack offsets.
17574
17575          If the arg pointer is live (or if it was live, but has now been
17576          eliminated and so set to dead) then we do have to test to see if
17577          the function might contain a far jump.  This test can lead to some
17578          false negatives, since before reload is completed, then length of
17579          branch instructions is not known, so gcc defaults to returning their
17580          longest length, which in turn sets the far jump attribute to true.
17581
17582          A false negative will not result in bad code being generated, but it
17583          will result in a needless push and pop of the link register.  We
17584          hope that this does not occur too often.
17585
17586          If we need doubleword stack alignment this could affect the other
17587          elimination offsets so we can't risk getting it wrong.  */
17588       if (df_regs_ever_live_p (ARG_POINTER_REGNUM))
17589         cfun->machine->arg_pointer_live = 1;
17590       else if (!cfun->machine->arg_pointer_live)
17591         return 0;
17592     }
17593
17594   /* Check to see if the function contains a branch
17595      insn with the far jump attribute set.  */
17596   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
17597     {
17598       if (GET_CODE (insn) == JUMP_INSN
17599           /* Ignore tablejump patterns.  */
17600           && GET_CODE (PATTERN (insn)) != ADDR_VEC
17601           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
17602           && get_attr_far_jump (insn) == FAR_JUMP_YES
17603           )
17604         {
17605           /* Record the fact that we have decided that
17606              the function does use far jumps.  */
17607           cfun->machine->far_jump_used = 1;
17608           return 1;
17609         }
17610     }
17611
17612   return 0;
17613 }
17614
17615 /* Return nonzero if FUNC must be entered in ARM mode.  */
17616 int
17617 is_called_in_ARM_mode (tree func)
17618 {
17619   gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
17620
17621   /* Ignore the problem about functions whose address is taken.  */
17622   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
17623     return TRUE;
17624
17625 #ifdef ARM_PE
17626   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
17627 #else
17628   return FALSE;
17629 #endif
17630 }
17631
17632 /* The bits which aren't usefully expanded as rtl.  */
17633 const char *
17634 thumb_unexpanded_epilogue (void)
17635 {
17636   arm_stack_offsets *offsets;
17637   int regno;
17638   unsigned long live_regs_mask = 0;
17639   int high_regs_pushed = 0;
17640   int had_to_push_lr;
17641   int size;
17642
17643   if (cfun->machine->return_used_this_function != 0)
17644     return "";
17645
17646   if (IS_NAKED (arm_current_func_type ()))
17647     return "";
17648
17649   offsets = arm_get_frame_offsets ();
17650   live_regs_mask = offsets->saved_regs_mask;
17651   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
17652
17653   /* If we can deduce the registers used from the function's return value.
17654      This is more reliable that examining df_regs_ever_live_p () because that
17655      will be set if the register is ever used in the function, not just if
17656      the register is used to hold a return value.  */
17657   size = arm_size_return_regs ();
17658
17659   /* The prolog may have pushed some high registers to use as
17660      work registers.  e.g. the testsuite file:
17661      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
17662      compiles to produce:
17663         push    {r4, r5, r6, r7, lr}
17664         mov     r7, r9
17665         mov     r6, r8
17666         push    {r6, r7}
17667      as part of the prolog.  We have to undo that pushing here.  */
17668
17669   if (high_regs_pushed)
17670     {
17671       unsigned long mask = live_regs_mask & 0xff;
17672       int next_hi_reg;
17673
17674       /* The available low registers depend on the size of the value we are
17675          returning.  */
17676       if (size <= 12)
17677         mask |=  1 << 3;
17678       if (size <= 8)
17679         mask |= 1 << 2;
17680
17681       if (mask == 0)
17682         /* Oh dear!  We have no low registers into which we can pop
17683            high registers!  */
17684         internal_error
17685           ("no low registers available for popping high registers");
17686
17687       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
17688         if (live_regs_mask & (1 << next_hi_reg))
17689           break;
17690
17691       while (high_regs_pushed)
17692         {
17693           /* Find lo register(s) into which the high register(s) can
17694              be popped.  */
17695           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
17696             {
17697               if (mask & (1 << regno))
17698                 high_regs_pushed--;
17699               if (high_regs_pushed == 0)
17700                 break;
17701             }
17702
17703           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
17704
17705           /* Pop the values into the low register(s).  */
17706           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
17707
17708           /* Move the value(s) into the high registers.  */
17709           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
17710             {
17711               if (mask & (1 << regno))
17712                 {
17713                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
17714                                regno);
17715
17716                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
17717                     if (live_regs_mask & (1 << next_hi_reg))
17718                       break;
17719                 }
17720             }
17721         }
17722       live_regs_mask &= ~0x0f00;
17723     }
17724
17725   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
17726   live_regs_mask &= 0xff;
17727
17728   if (crtl->args.pretend_args_size == 0 || TARGET_BACKTRACE)
17729     {
17730       /* Pop the return address into the PC.  */
17731       if (had_to_push_lr)
17732         live_regs_mask |= 1 << PC_REGNUM;
17733
17734       /* Either no argument registers were pushed or a backtrace
17735          structure was created which includes an adjusted stack
17736          pointer, so just pop everything.  */
17737       if (live_regs_mask)
17738         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
17739                        live_regs_mask);
17740
17741       /* We have either just popped the return address into the
17742          PC or it is was kept in LR for the entire function.  */
17743       if (!had_to_push_lr)
17744         thumb_exit (asm_out_file, LR_REGNUM);
17745     }
17746   else
17747     {
17748       /* Pop everything but the return address.  */
17749       if (live_regs_mask)
17750         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
17751                        live_regs_mask);
17752
17753       if (had_to_push_lr)
17754         {
17755           if (size > 12)
17756             {
17757               /* We have no free low regs, so save one.  */
17758               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
17759                            LAST_ARG_REGNUM);
17760             }
17761
17762           /* Get the return address into a temporary register.  */
17763           thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
17764                          1 << LAST_ARG_REGNUM);
17765
17766           if (size > 12)
17767             {
17768               /* Move the return address to lr.  */
17769               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
17770                            LAST_ARG_REGNUM);
17771               /* Restore the low register.  */
17772               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
17773                            IP_REGNUM);
17774               regno = LR_REGNUM;
17775             }
17776           else
17777             regno = LAST_ARG_REGNUM;
17778         }
17779       else
17780         regno = LR_REGNUM;
17781
17782       /* Remove the argument registers that were pushed onto the stack.  */
17783       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
17784                    SP_REGNUM, SP_REGNUM,
17785                    crtl->args.pretend_args_size);
17786
17787       thumb_exit (asm_out_file, regno);
17788     }
17789
17790   return "";
17791 }
17792
17793 /* Functions to save and restore machine-specific function data.  */
17794 static struct machine_function *
17795 arm_init_machine_status (void)
17796 {
17797   struct machine_function *machine;
17798   machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
17799
17800 #if ARM_FT_UNKNOWN != 0
17801   machine->func_type = ARM_FT_UNKNOWN;
17802 #endif
17803   return machine;
17804 }
17805
17806 /* Return an RTX indicating where the return address to the
17807    calling function can be found.  */
17808 rtx
17809 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
17810 {
17811   if (count != 0)
17812     return NULL_RTX;
17813
17814   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
17815 }
17816
17817 /* Do anything needed before RTL is emitted for each function.  */
17818 void
17819 arm_init_expanders (void)
17820 {
17821   /* Arrange to initialize and mark the machine per-function status.  */
17822   init_machine_status = arm_init_machine_status;
17823
17824   /* This is to stop the combine pass optimizing away the alignment
17825      adjustment of va_arg.  */
17826   /* ??? It is claimed that this should not be necessary.  */
17827   if (cfun)
17828     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
17829 }
17830
17831
17832 /* Like arm_compute_initial_elimination offset.  Simpler because there
17833    isn't an ABI specified frame pointer for Thumb.  Instead, we set it
17834    to point at the base of the local variables after static stack
17835    space for a function has been allocated.  */
17836
17837 HOST_WIDE_INT
17838 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
17839 {
17840   arm_stack_offsets *offsets;
17841
17842   offsets = arm_get_frame_offsets ();
17843
17844   switch (from)
17845     {
17846     case ARG_POINTER_REGNUM:
17847       switch (to)
17848         {
17849         case STACK_POINTER_REGNUM:
17850           return offsets->outgoing_args - offsets->saved_args;
17851
17852         case FRAME_POINTER_REGNUM:
17853           return offsets->soft_frame - offsets->saved_args;
17854
17855         case ARM_HARD_FRAME_POINTER_REGNUM:
17856           return offsets->saved_regs - offsets->saved_args;
17857
17858         case THUMB_HARD_FRAME_POINTER_REGNUM:
17859           return offsets->locals_base - offsets->saved_args;
17860
17861         default:
17862           gcc_unreachable ();
17863         }
17864       break;
17865
17866     case FRAME_POINTER_REGNUM:
17867       switch (to)
17868         {
17869         case STACK_POINTER_REGNUM:
17870           return offsets->outgoing_args - offsets->soft_frame;
17871
17872         case ARM_HARD_FRAME_POINTER_REGNUM:
17873           return offsets->saved_regs - offsets->soft_frame;
17874
17875         case THUMB_HARD_FRAME_POINTER_REGNUM:
17876           return offsets->locals_base - offsets->soft_frame;
17877
17878         default:
17879           gcc_unreachable ();
17880         }
17881       break;
17882
17883     default:
17884       gcc_unreachable ();
17885     }
17886 }
17887
17888 /* Generate the rest of a function's prologue.  */
17889 void
17890 thumb1_expand_prologue (void)
17891 {
17892   rtx insn, dwarf;
17893
17894   HOST_WIDE_INT amount;
17895   arm_stack_offsets *offsets;
17896   unsigned long func_type;
17897   int regno;
17898   unsigned long live_regs_mask;
17899
17900   func_type = arm_current_func_type ();
17901
17902   /* Naked functions don't have prologues.  */
17903   if (IS_NAKED (func_type))
17904     return;
17905
17906   if (IS_INTERRUPT (func_type))
17907     {
17908       error ("interrupt Service Routines cannot be coded in Thumb mode");
17909       return;
17910     }
17911
17912   offsets = arm_get_frame_offsets ();
17913   live_regs_mask = offsets->saved_regs_mask;
17914   /* Load the pic register before setting the frame pointer,
17915      so we can use r7 as a temporary work register.  */
17916   if (flag_pic && arm_pic_register != INVALID_REGNUM)
17917     arm_load_pic_register (live_regs_mask);
17918
17919   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
17920     emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
17921                     stack_pointer_rtx);
17922
17923   amount = offsets->outgoing_args - offsets->saved_regs;
17924   if (amount)
17925     {
17926       if (amount < 512)
17927         {
17928           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
17929                                         GEN_INT (- amount)));
17930           RTX_FRAME_RELATED_P (insn) = 1;
17931         }
17932       else
17933         {
17934           rtx reg;
17935
17936           /* The stack decrement is too big for an immediate value in a single
17937              insn.  In theory we could issue multiple subtracts, but after
17938              three of them it becomes more space efficient to place the full
17939              value in the constant pool and load into a register.  (Also the
17940              ARM debugger really likes to see only one stack decrement per
17941              function).  So instead we look for a scratch register into which
17942              we can load the decrement, and then we subtract this from the
17943              stack pointer.  Unfortunately on the thumb the only available
17944              scratch registers are the argument registers, and we cannot use
17945              these as they may hold arguments to the function.  Instead we
17946              attempt to locate a call preserved register which is used by this
17947              function.  If we can find one, then we know that it will have
17948              been pushed at the start of the prologue and so we can corrupt
17949              it now.  */
17950           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
17951             if (live_regs_mask & (1 << regno))
17952               break;
17953
17954           gcc_assert(regno <= LAST_LO_REGNUM);
17955
17956           reg = gen_rtx_REG (SImode, regno);
17957
17958           emit_insn (gen_movsi (reg, GEN_INT (- amount)));
17959
17960           insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
17961                                         stack_pointer_rtx, reg));
17962           RTX_FRAME_RELATED_P (insn) = 1;
17963           dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
17964                                plus_constant (stack_pointer_rtx,
17965                                               -amount));
17966           RTX_FRAME_RELATED_P (dwarf) = 1;
17967           add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
17968         }
17969     }
17970
17971   if (frame_pointer_needed)
17972     thumb_set_frame_pointer (offsets);
17973
17974   /* If we are profiling, make sure no instructions are scheduled before
17975      the call to mcount.  Similarly if the user has requested no
17976      scheduling in the prolog.  Similarly if we want non-call exceptions
17977      using the EABI unwinder, to prevent faulting instructions from being
17978      swapped with a stack adjustment.  */
17979   if (crtl->profile || !TARGET_SCHED_PROLOG
17980       || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
17981     emit_insn (gen_blockage ());
17982
17983   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
17984   if (live_regs_mask & 0xff)
17985     cfun->machine->lr_save_eliminated = 0;
17986 }
17987
17988
17989 void
17990 thumb1_expand_epilogue (void)
17991 {
17992   HOST_WIDE_INT amount;
17993   arm_stack_offsets *offsets;
17994   int regno;
17995
17996   /* Naked functions don't have prologues.  */
17997   if (IS_NAKED (arm_current_func_type ()))
17998     return;
17999
18000   offsets = arm_get_frame_offsets ();
18001   amount = offsets->outgoing_args - offsets->saved_regs;
18002
18003   if (frame_pointer_needed)
18004     {
18005       emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
18006       amount = offsets->locals_base - offsets->saved_regs;
18007     }
18008
18009   gcc_assert (amount >= 0);
18010   if (amount)
18011     {
18012       if (amount < 512)
18013         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
18014                                GEN_INT (amount)));
18015       else
18016         {
18017           /* r3 is always free in the epilogue.  */
18018           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
18019
18020           emit_insn (gen_movsi (reg, GEN_INT (amount)));
18021           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
18022         }
18023     }
18024
18025   /* Emit a USE (stack_pointer_rtx), so that
18026      the stack adjustment will not be deleted.  */
18027   emit_insn (gen_prologue_use (stack_pointer_rtx));
18028
18029   if (crtl->profile || !TARGET_SCHED_PROLOG)
18030     emit_insn (gen_blockage ());
18031
18032   /* Emit a clobber for each insn that will be restored in the epilogue,
18033      so that flow2 will get register lifetimes correct.  */
18034   for (regno = 0; regno < 13; regno++)
18035     if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
18036       emit_clobber (gen_rtx_REG (SImode, regno));
18037
18038   if (! df_regs_ever_live_p (LR_REGNUM))
18039     emit_use (gen_rtx_REG (SImode, LR_REGNUM));
18040 }
18041
18042 static void
18043 thumb1_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
18044 {
18045   arm_stack_offsets *offsets;
18046   unsigned long live_regs_mask = 0;
18047   unsigned long l_mask;
18048   unsigned high_regs_pushed = 0;
18049   int cfa_offset = 0;
18050   int regno;
18051
18052   if (IS_NAKED (arm_current_func_type ()))
18053     return;
18054
18055   if (is_called_in_ARM_mode (current_function_decl))
18056     {
18057       const char * name;
18058
18059       gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
18060       gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
18061                   == SYMBOL_REF);
18062       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
18063
18064       /* Generate code sequence to switch us into Thumb mode.  */
18065       /* The .code 32 directive has already been emitted by
18066          ASM_DECLARE_FUNCTION_NAME.  */
18067       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
18068       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
18069
18070       /* Generate a label, so that the debugger will notice the
18071          change in instruction sets.  This label is also used by
18072          the assembler to bypass the ARM code when this function
18073          is called from a Thumb encoded function elsewhere in the
18074          same file.  Hence the definition of STUB_NAME here must
18075          agree with the definition in gas/config/tc-arm.c.  */
18076
18077 #define STUB_NAME ".real_start_of"
18078
18079       fprintf (f, "\t.code\t16\n");
18080 #ifdef ARM_PE
18081       if (arm_dllexport_name_p (name))
18082         name = arm_strip_name_encoding (name);
18083 #endif
18084       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
18085       fprintf (f, "\t.thumb_func\n");
18086       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
18087     }
18088
18089   if (crtl->args.pretend_args_size)
18090     {
18091       /* Output unwind directive for the stack adjustment.  */
18092       if (ARM_EABI_UNWIND_TABLES)
18093         fprintf (f, "\t.pad #%d\n",
18094                  crtl->args.pretend_args_size);
18095
18096       if (cfun->machine->uses_anonymous_args)
18097         {
18098           int num_pushes;
18099
18100           fprintf (f, "\tpush\t{");
18101
18102           num_pushes = ARM_NUM_INTS (crtl->args.pretend_args_size);
18103
18104           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
18105                regno <= LAST_ARG_REGNUM;
18106                regno++)
18107             asm_fprintf (f, "%r%s", regno,
18108                          regno == LAST_ARG_REGNUM ? "" : ", ");
18109
18110           fprintf (f, "}\n");
18111         }
18112       else
18113         asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
18114                      SP_REGNUM, SP_REGNUM,
18115                      crtl->args.pretend_args_size);
18116
18117       /* We don't need to record the stores for unwinding (would it
18118          help the debugger any if we did?), but record the change in
18119          the stack pointer.  */
18120       if (dwarf2out_do_frame ())
18121         {
18122           char *l = dwarf2out_cfi_label (false);
18123
18124           cfa_offset = cfa_offset + crtl->args.pretend_args_size;
18125           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
18126         }
18127     }
18128
18129   /* Get the registers we are going to push.  */
18130   offsets = arm_get_frame_offsets ();
18131   live_regs_mask = offsets->saved_regs_mask;
18132   /* Extract a mask of the ones we can give to the Thumb's push instruction.  */
18133   l_mask = live_regs_mask & 0x40ff;
18134   /* Then count how many other high registers will need to be pushed.  */
18135   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
18136
18137   if (TARGET_BACKTRACE)
18138     {
18139       unsigned offset;
18140       unsigned work_register;
18141
18142       /* We have been asked to create a stack backtrace structure.
18143          The code looks like this:
18144
18145          0   .align 2
18146          0   func:
18147          0     sub   SP, #16         Reserve space for 4 registers.
18148          2     push  {R7}            Push low registers.
18149          4     add   R7, SP, #20     Get the stack pointer before the push.
18150          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
18151          8     mov   R7, PC          Get hold of the start of this code plus 12.
18152         10     str   R7, [SP, #16]   Store it.
18153         12     mov   R7, FP          Get hold of the current frame pointer.
18154         14     str   R7, [SP, #4]    Store it.
18155         16     mov   R7, LR          Get hold of the current return address.
18156         18     str   R7, [SP, #12]   Store it.
18157         20     add   R7, SP, #16     Point at the start of the backtrace structure.
18158         22     mov   FP, R7          Put this value into the frame pointer.  */
18159
18160       work_register = thumb_find_work_register (live_regs_mask);
18161
18162       if (ARM_EABI_UNWIND_TABLES)
18163         asm_fprintf (f, "\t.pad #16\n");
18164
18165       asm_fprintf
18166         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
18167          SP_REGNUM, SP_REGNUM);
18168
18169       if (dwarf2out_do_frame ())
18170         {
18171           char *l = dwarf2out_cfi_label (false);
18172
18173           cfa_offset = cfa_offset + 16;
18174           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
18175         }
18176
18177       if (l_mask)
18178         {
18179           thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
18180           offset = bit_count (l_mask) * UNITS_PER_WORD;
18181         }
18182       else
18183         offset = 0;
18184
18185       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
18186                    offset + 16 + crtl->args.pretend_args_size);
18187
18188       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
18189                    offset + 4);
18190
18191       /* Make sure that the instruction fetching the PC is in the right place
18192          to calculate "start of backtrace creation code + 12".  */
18193       if (l_mask)
18194         {
18195           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
18196           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
18197                        offset + 12);
18198           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
18199                        ARM_HARD_FRAME_POINTER_REGNUM);
18200           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
18201                        offset);
18202         }
18203       else
18204         {
18205           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
18206                        ARM_HARD_FRAME_POINTER_REGNUM);
18207           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
18208                        offset);
18209           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
18210           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
18211                        offset + 12);
18212         }
18213
18214       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
18215       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
18216                    offset + 8);
18217       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
18218                    offset + 12);
18219       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
18220                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
18221     }
18222   /* Optimization:  If we are not pushing any low registers but we are going
18223      to push some high registers then delay our first push.  This will just
18224      be a push of LR and we can combine it with the push of the first high
18225      register.  */
18226   else if ((l_mask & 0xff) != 0
18227            || (high_regs_pushed == 0 && l_mask))
18228     thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
18229
18230   if (high_regs_pushed)
18231     {
18232       unsigned pushable_regs;
18233       unsigned next_hi_reg;
18234
18235       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
18236         if (live_regs_mask & (1 << next_hi_reg))
18237           break;
18238
18239       pushable_regs = l_mask & 0xff;
18240
18241       if (pushable_regs == 0)
18242         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
18243
18244       while (high_regs_pushed > 0)
18245         {
18246           unsigned long real_regs_mask = 0;
18247
18248           for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
18249             {
18250               if (pushable_regs & (1 << regno))
18251                 {
18252                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
18253
18254                   high_regs_pushed --;
18255                   real_regs_mask |= (1 << next_hi_reg);
18256
18257                   if (high_regs_pushed)
18258                     {
18259                       for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
18260                            next_hi_reg --)
18261                         if (live_regs_mask & (1 << next_hi_reg))
18262                           break;
18263                     }
18264                   else
18265                     {
18266                       pushable_regs &= ~((1 << regno) - 1);
18267                       break;
18268                     }
18269                 }
18270             }
18271
18272           /* If we had to find a work register and we have not yet
18273              saved the LR then add it to the list of regs to push.  */
18274           if (l_mask == (1 << LR_REGNUM))
18275             {
18276               thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
18277                              1, &cfa_offset,
18278                              real_regs_mask | (1 << LR_REGNUM));
18279               l_mask = 0;
18280             }
18281           else
18282             thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
18283         }
18284     }
18285 }
18286
18287 /* Handle the case of a double word load into a low register from
18288    a computed memory address.  The computed address may involve a
18289    register which is overwritten by the load.  */
18290 const char *
18291 thumb_load_double_from_address (rtx *operands)
18292 {
18293   rtx addr;
18294   rtx base;
18295   rtx offset;
18296   rtx arg1;
18297   rtx arg2;
18298
18299   gcc_assert (GET_CODE (operands[0]) == REG);
18300   gcc_assert (GET_CODE (operands[1]) == MEM);
18301
18302   /* Get the memory address.  */
18303   addr = XEXP (operands[1], 0);
18304
18305   /* Work out how the memory address is computed.  */
18306   switch (GET_CODE (addr))
18307     {
18308     case REG:
18309       operands[2] = adjust_address (operands[1], SImode, 4);
18310
18311       if (REGNO (operands[0]) == REGNO (addr))
18312         {
18313           output_asm_insn ("ldr\t%H0, %2", operands);
18314           output_asm_insn ("ldr\t%0, %1", operands);
18315         }
18316       else
18317         {
18318           output_asm_insn ("ldr\t%0, %1", operands);
18319           output_asm_insn ("ldr\t%H0, %2", operands);
18320         }
18321       break;
18322
18323     case CONST:
18324       /* Compute <address> + 4 for the high order load.  */
18325       operands[2] = adjust_address (operands[1], SImode, 4);
18326
18327       output_asm_insn ("ldr\t%0, %1", operands);
18328       output_asm_insn ("ldr\t%H0, %2", operands);
18329       break;
18330
18331     case PLUS:
18332       arg1   = XEXP (addr, 0);
18333       arg2   = XEXP (addr, 1);
18334
18335       if (CONSTANT_P (arg1))
18336         base = arg2, offset = arg1;
18337       else
18338         base = arg1, offset = arg2;
18339
18340       gcc_assert (GET_CODE (base) == REG);
18341
18342       /* Catch the case of <address> = <reg> + <reg> */
18343       if (GET_CODE (offset) == REG)
18344         {
18345           int reg_offset = REGNO (offset);
18346           int reg_base   = REGNO (base);
18347           int reg_dest   = REGNO (operands[0]);
18348
18349           /* Add the base and offset registers together into the
18350              higher destination register.  */
18351           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
18352                        reg_dest + 1, reg_base, reg_offset);
18353
18354           /* Load the lower destination register from the address in
18355              the higher destination register.  */
18356           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
18357                        reg_dest, reg_dest + 1);
18358
18359           /* Load the higher destination register from its own address
18360              plus 4.  */
18361           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
18362                        reg_dest + 1, reg_dest + 1);
18363         }
18364       else
18365         {
18366           /* Compute <address> + 4 for the high order load.  */
18367           operands[2] = adjust_address (operands[1], SImode, 4);
18368
18369           /* If the computed address is held in the low order register
18370              then load the high order register first, otherwise always
18371              load the low order register first.  */
18372           if (REGNO (operands[0]) == REGNO (base))
18373             {
18374               output_asm_insn ("ldr\t%H0, %2", operands);
18375               output_asm_insn ("ldr\t%0, %1", operands);
18376             }
18377           else
18378             {
18379               output_asm_insn ("ldr\t%0, %1", operands);
18380               output_asm_insn ("ldr\t%H0, %2", operands);
18381             }
18382         }
18383       break;
18384
18385     case LABEL_REF:
18386       /* With no registers to worry about we can just load the value
18387          directly.  */
18388       operands[2] = adjust_address (operands[1], SImode, 4);
18389
18390       output_asm_insn ("ldr\t%H0, %2", operands);
18391       output_asm_insn ("ldr\t%0, %1", operands);
18392       break;
18393
18394     default:
18395       gcc_unreachable ();
18396     }
18397
18398   return "";
18399 }
18400
18401 const char *
18402 thumb_output_move_mem_multiple (int n, rtx *operands)
18403 {
18404   rtx tmp;
18405
18406   switch (n)
18407     {
18408     case 2:
18409       if (REGNO (operands[4]) > REGNO (operands[5]))
18410         {
18411           tmp = operands[4];
18412           operands[4] = operands[5];
18413           operands[5] = tmp;
18414         }
18415       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
18416       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
18417       break;
18418
18419     case 3:
18420       if (REGNO (operands[4]) > REGNO (operands[5]))
18421         {
18422           tmp = operands[4];
18423           operands[4] = operands[5];
18424           operands[5] = tmp;
18425         }
18426       if (REGNO (operands[5]) > REGNO (operands[6]))
18427         {
18428           tmp = operands[5];
18429           operands[5] = operands[6];
18430           operands[6] = tmp;
18431         }
18432       if (REGNO (operands[4]) > REGNO (operands[5]))
18433         {
18434           tmp = operands[4];
18435           operands[4] = operands[5];
18436           operands[5] = tmp;
18437         }
18438
18439       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
18440       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
18441       break;
18442
18443     default:
18444       gcc_unreachable ();
18445     }
18446
18447   return "";
18448 }
18449
18450 /* Output a call-via instruction for thumb state.  */
18451 const char *
18452 thumb_call_via_reg (rtx reg)
18453 {
18454   int regno = REGNO (reg);
18455   rtx *labelp;
18456
18457   gcc_assert (regno < LR_REGNUM);
18458
18459   /* If we are in the normal text section we can use a single instance
18460      per compilation unit.  If we are doing function sections, then we need
18461      an entry per section, since we can't rely on reachability.  */
18462   if (in_section == text_section)
18463     {
18464       thumb_call_reg_needed = 1;
18465
18466       if (thumb_call_via_label[regno] == NULL)
18467         thumb_call_via_label[regno] = gen_label_rtx ();
18468       labelp = thumb_call_via_label + regno;
18469     }
18470   else
18471     {
18472       if (cfun->machine->call_via[regno] == NULL)
18473         cfun->machine->call_via[regno] = gen_label_rtx ();
18474       labelp = cfun->machine->call_via + regno;
18475     }
18476
18477   output_asm_insn ("bl\t%a0", labelp);
18478   return "";
18479 }
18480
18481 /* Routines for generating rtl.  */
18482 void
18483 thumb_expand_movmemqi (rtx *operands)
18484 {
18485   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
18486   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
18487   HOST_WIDE_INT len = INTVAL (operands[2]);
18488   HOST_WIDE_INT offset = 0;
18489
18490   while (len >= 12)
18491     {
18492       emit_insn (gen_movmem12b (out, in, out, in));
18493       len -= 12;
18494     }
18495
18496   if (len >= 8)
18497     {
18498       emit_insn (gen_movmem8b (out, in, out, in));
18499       len -= 8;
18500     }
18501
18502   if (len >= 4)
18503     {
18504       rtx reg = gen_reg_rtx (SImode);
18505       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
18506       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
18507       len -= 4;
18508       offset += 4;
18509     }
18510
18511   if (len >= 2)
18512     {
18513       rtx reg = gen_reg_rtx (HImode);
18514       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
18515                                               plus_constant (in, offset))));
18516       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
18517                             reg));
18518       len -= 2;
18519       offset += 2;
18520     }
18521
18522   if (len)
18523     {
18524       rtx reg = gen_reg_rtx (QImode);
18525       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
18526                                               plus_constant (in, offset))));
18527       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
18528                             reg));
18529     }
18530 }
18531
18532 void
18533 thumb_reload_out_hi (rtx *operands)
18534 {
18535   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
18536 }
18537
18538 /* Handle reading a half-word from memory during reload.  */
18539 void
18540 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
18541 {
18542   gcc_unreachable ();
18543 }
18544
18545 /* Return the length of a function name prefix
18546     that starts with the character 'c'.  */
18547 static int
18548 arm_get_strip_length (int c)
18549 {
18550   switch (c)
18551     {
18552     ARM_NAME_ENCODING_LENGTHS
18553       default: return 0;
18554     }
18555 }
18556
18557 /* Return a pointer to a function's name with any
18558    and all prefix encodings stripped from it.  */
18559 const char *
18560 arm_strip_name_encoding (const char *name)
18561 {
18562   int skip;
18563
18564   while ((skip = arm_get_strip_length (* name)))
18565     name += skip;
18566
18567   return name;
18568 }
18569
18570 /* If there is a '*' anywhere in the name's prefix, then
18571    emit the stripped name verbatim, otherwise prepend an
18572    underscore if leading underscores are being used.  */
18573 void
18574 arm_asm_output_labelref (FILE *stream, const char *name)
18575 {
18576   int skip;
18577   int verbatim = 0;
18578
18579   while ((skip = arm_get_strip_length (* name)))
18580     {
18581       verbatim |= (*name == '*');
18582       name += skip;
18583     }
18584
18585   if (verbatim)
18586     fputs (name, stream);
18587   else
18588     asm_fprintf (stream, "%U%s", name);
18589 }
18590
18591 static void
18592 arm_file_start (void)
18593 {
18594   int val;
18595
18596   if (TARGET_UNIFIED_ASM)
18597     asm_fprintf (asm_out_file, "\t.syntax unified\n");
18598
18599   if (TARGET_BPABI)
18600     {
18601       const char *fpu_name;
18602       if (arm_select[0].string)
18603         asm_fprintf (asm_out_file, "\t.cpu %s\n", arm_select[0].string);
18604       else if (arm_select[1].string)
18605         asm_fprintf (asm_out_file, "\t.arch %s\n", arm_select[1].string);
18606       else
18607         asm_fprintf (asm_out_file, "\t.cpu %s\n",
18608                      all_cores[arm_default_cpu].name);
18609
18610       if (TARGET_SOFT_FLOAT)
18611         {
18612           if (TARGET_VFP)
18613             fpu_name = "softvfp";
18614           else
18615             fpu_name = "softfpa";
18616         }
18617       else
18618         {
18619           int set_float_abi_attributes = 0;
18620           switch (arm_fpu_arch)
18621             {
18622             case FPUTYPE_FPA:
18623               fpu_name = "fpa";
18624               break;
18625             case FPUTYPE_FPA_EMU2:
18626               fpu_name = "fpe2";
18627               break;
18628             case FPUTYPE_FPA_EMU3:
18629               fpu_name = "fpe3";
18630               break;
18631             case FPUTYPE_MAVERICK:
18632               fpu_name = "maverick";
18633               break;
18634             case FPUTYPE_VFP:
18635               fpu_name = "vfp";
18636               set_float_abi_attributes = 1;
18637               break;
18638             case FPUTYPE_VFP3D16:
18639               fpu_name = "vfpv3-d16";
18640               set_float_abi_attributes = 1;
18641               break;
18642             case FPUTYPE_VFP3:
18643               fpu_name = "vfpv3";
18644               set_float_abi_attributes = 1;
18645               break;
18646             case FPUTYPE_NEON:
18647               fpu_name = "neon";
18648               set_float_abi_attributes = 1;
18649               break;
18650             case FPUTYPE_NEON_FP16:
18651               fpu_name = "neon-fp16";
18652               set_float_abi_attributes = 1;
18653               break;
18654             default:
18655               abort();
18656             }
18657           if (set_float_abi_attributes)
18658             {
18659               if (TARGET_HARD_FLOAT)
18660                 asm_fprintf (asm_out_file, "\t.eabi_attribute 27, 3\n");
18661               if (TARGET_HARD_FLOAT_ABI)
18662                 asm_fprintf (asm_out_file, "\t.eabi_attribute 28, 1\n");
18663             }
18664         }
18665       asm_fprintf (asm_out_file, "\t.fpu %s\n", fpu_name);
18666
18667       /* Some of these attributes only apply when the corresponding features
18668          are used.  However we don't have any easy way of figuring this out.
18669          Conservatively record the setting that would have been used.  */
18670
18671       /* Tag_ABI_FP_rounding.  */
18672       if (flag_rounding_math)
18673         asm_fprintf (asm_out_file, "\t.eabi_attribute 19, 1\n");
18674       if (!flag_unsafe_math_optimizations)
18675         {
18676           /* Tag_ABI_FP_denomal.  */
18677           asm_fprintf (asm_out_file, "\t.eabi_attribute 20, 1\n");
18678           /* Tag_ABI_FP_exceptions.  */
18679           asm_fprintf (asm_out_file, "\t.eabi_attribute 21, 1\n");
18680         }
18681       /* Tag_ABI_FP_user_exceptions.  */
18682       if (flag_signaling_nans)
18683         asm_fprintf (asm_out_file, "\t.eabi_attribute 22, 1\n");
18684       /* Tag_ABI_FP_number_model.  */
18685       asm_fprintf (asm_out_file, "\t.eabi_attribute 23, %d\n", 
18686                    flag_finite_math_only ? 1 : 3);
18687
18688       /* Tag_ABI_align8_needed.  */
18689       asm_fprintf (asm_out_file, "\t.eabi_attribute 24, 1\n");
18690       /* Tag_ABI_align8_preserved.  */
18691       asm_fprintf (asm_out_file, "\t.eabi_attribute 25, 1\n");
18692       /* Tag_ABI_enum_size.  */
18693       asm_fprintf (asm_out_file, "\t.eabi_attribute 26, %d\n",
18694                    flag_short_enums ? 1 : 2);
18695
18696       /* Tag_ABI_optimization_goals.  */
18697       if (optimize_size)
18698         val = 4;
18699       else if (optimize >= 2)
18700         val = 2;
18701       else if (optimize)
18702         val = 1;
18703       else
18704         val = 6;
18705       asm_fprintf (asm_out_file, "\t.eabi_attribute 30, %d\n", val);
18706
18707       /* Tag_ABI_FP_16bit_format.  */
18708       if (arm_fp16_format)
18709         asm_fprintf (asm_out_file, "\t.eabi_attribute 38, %d\n",
18710                      (int)arm_fp16_format);
18711
18712       if (arm_lang_output_object_attributes_hook)
18713         arm_lang_output_object_attributes_hook();
18714     }
18715   default_file_start();
18716 }
18717
18718 static void
18719 arm_file_end (void)
18720 {
18721   int regno;
18722
18723   if (NEED_INDICATE_EXEC_STACK)
18724     /* Add .note.GNU-stack.  */
18725     file_end_indicate_exec_stack ();
18726
18727   if (! thumb_call_reg_needed)
18728     return;
18729
18730   switch_to_section (text_section);
18731   asm_fprintf (asm_out_file, "\t.code 16\n");
18732   ASM_OUTPUT_ALIGN (asm_out_file, 1);
18733
18734   for (regno = 0; regno < LR_REGNUM; regno++)
18735     {
18736       rtx label = thumb_call_via_label[regno];
18737
18738       if (label != 0)
18739         {
18740           targetm.asm_out.internal_label (asm_out_file, "L",
18741                                           CODE_LABEL_NUMBER (label));
18742           asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
18743         }
18744     }
18745 }
18746
18747 #ifndef ARM_PE
18748 /* Symbols in the text segment can be accessed without indirecting via the
18749    constant pool; it may take an extra binary operation, but this is still
18750    faster than indirecting via memory.  Don't do this when not optimizing,
18751    since we won't be calculating al of the offsets necessary to do this
18752    simplification.  */
18753
18754 static void
18755 arm_encode_section_info (tree decl, rtx rtl, int first)
18756 {
18757   if (optimize > 0 && TREE_CONSTANT (decl))
18758     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
18759
18760   default_encode_section_info (decl, rtl, first);
18761 }
18762 #endif /* !ARM_PE */
18763
18764 static void
18765 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
18766 {
18767   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
18768       && !strcmp (prefix, "L"))
18769     {
18770       arm_ccfsm_state = 0;
18771       arm_target_insn = NULL;
18772     }
18773   default_internal_label (stream, prefix, labelno);
18774 }
18775
18776 /* Output code to add DELTA to the first argument, and then jump
18777    to FUNCTION.  Used for C++ multiple inheritance.  */
18778 static void
18779 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
18780                      HOST_WIDE_INT delta,
18781                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
18782                      tree function)
18783 {
18784   static int thunk_label = 0;
18785   char label[256];
18786   char labelpc[256];
18787   int mi_delta = delta;
18788   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
18789   int shift = 0;
18790   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
18791                     ? 1 : 0);
18792   if (mi_delta < 0)
18793     mi_delta = - mi_delta;
18794
18795   if (TARGET_THUMB1)
18796     {
18797       int labelno = thunk_label++;
18798       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
18799       /* Thunks are entered in arm mode when avaiable.  */
18800       if (TARGET_THUMB1_ONLY)
18801         {
18802           /* push r3 so we can use it as a temporary.  */
18803           /* TODO: Omit this save if r3 is not used.  */
18804           fputs ("\tpush {r3}\n", file);
18805           fputs ("\tldr\tr3, ", file);
18806         }
18807       else
18808         {
18809           fputs ("\tldr\tr12, ", file);
18810         }
18811       assemble_name (file, label);
18812       fputc ('\n', file);
18813       if (flag_pic)
18814         {
18815           /* If we are generating PIC, the ldr instruction below loads
18816              "(target - 7) - .LTHUNKPCn" into r12.  The pc reads as
18817              the address of the add + 8, so we have:
18818
18819              r12 = (target - 7) - .LTHUNKPCn + (.LTHUNKPCn + 8)
18820                  = target + 1.
18821
18822              Note that we have "+ 1" because some versions of GNU ld
18823              don't set the low bit of the result for R_ARM_REL32
18824              relocations against thumb function symbols.
18825              On ARMv6M this is +4, not +8.  */
18826           ASM_GENERATE_INTERNAL_LABEL (labelpc, "LTHUNKPC", labelno);
18827           assemble_name (file, labelpc);
18828           fputs (":\n", file);
18829           if (TARGET_THUMB1_ONLY)
18830             {
18831               /* This is 2 insns after the start of the thunk, so we know it
18832                  is 4-byte aligned.  */
18833               fputs ("\tadd\tr3, pc, r3\n", file);
18834               fputs ("\tmov r12, r3\n", file);
18835             }
18836           else
18837             fputs ("\tadd\tr12, pc, r12\n", file);
18838         }
18839       else if (TARGET_THUMB1_ONLY)
18840         fputs ("\tmov r12, r3\n", file);
18841     }
18842   if (TARGET_THUMB1_ONLY)
18843     {
18844       if (mi_delta > 255)
18845         {
18846           fputs ("\tldr\tr3, ", file);
18847           assemble_name (file, label);
18848           fputs ("+4\n", file);
18849           asm_fprintf (file, "\t%s\t%r, %r, r3\n",
18850                        mi_op, this_regno, this_regno);
18851         }
18852       else if (mi_delta != 0)
18853         {
18854           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
18855                        mi_op, this_regno, this_regno,
18856                        mi_delta);
18857         }
18858     }
18859   else
18860     {
18861       /* TODO: Use movw/movt for large constants when available.  */
18862       while (mi_delta != 0)
18863         {
18864           if ((mi_delta & (3 << shift)) == 0)
18865             shift += 2;
18866           else
18867             {
18868               asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
18869                            mi_op, this_regno, this_regno,
18870                            mi_delta & (0xff << shift));
18871               mi_delta &= ~(0xff << shift);
18872               shift += 8;
18873             }
18874         }
18875     }
18876   if (TARGET_THUMB1)
18877     {
18878       if (TARGET_THUMB1_ONLY)
18879         fputs ("\tpop\t{r3}\n", file);
18880
18881       fprintf (file, "\tbx\tr12\n");
18882       ASM_OUTPUT_ALIGN (file, 2);
18883       assemble_name (file, label);
18884       fputs (":\n", file);
18885       if (flag_pic)
18886         {
18887           /* Output ".word .LTHUNKn-7-.LTHUNKPCn".  */
18888           rtx tem = XEXP (DECL_RTL (function), 0);
18889           tem = gen_rtx_PLUS (GET_MODE (tem), tem, GEN_INT (-7));
18890           tem = gen_rtx_MINUS (GET_MODE (tem),
18891                                tem,
18892                                gen_rtx_SYMBOL_REF (Pmode,
18893                                                    ggc_strdup (labelpc)));
18894           assemble_integer (tem, 4, BITS_PER_WORD, 1);
18895         }
18896       else
18897         /* Output ".word .LTHUNKn".  */
18898         assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
18899
18900       if (TARGET_THUMB1_ONLY && mi_delta > 255)
18901         assemble_integer (GEN_INT(mi_delta), 4, BITS_PER_WORD, 1);
18902     }
18903   else
18904     {
18905       fputs ("\tb\t", file);
18906       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
18907       if (NEED_PLT_RELOC)
18908         fputs ("(PLT)", file);
18909       fputc ('\n', file);
18910     }
18911 }
18912
18913 int
18914 arm_emit_vector_const (FILE *file, rtx x)
18915 {
18916   int i;
18917   const char * pattern;
18918
18919   gcc_assert (GET_CODE (x) == CONST_VECTOR);
18920
18921   switch (GET_MODE (x))
18922     {
18923     case V2SImode: pattern = "%08x"; break;
18924     case V4HImode: pattern = "%04x"; break;
18925     case V8QImode: pattern = "%02x"; break;
18926     default:       gcc_unreachable ();
18927     }
18928
18929   fprintf (file, "0x");
18930   for (i = CONST_VECTOR_NUNITS (x); i--;)
18931     {
18932       rtx element;
18933
18934       element = CONST_VECTOR_ELT (x, i);
18935       fprintf (file, pattern, INTVAL (element));
18936     }
18937
18938   return 1;
18939 }
18940
18941 /* Emit a fp16 constant appropriately padded to occupy a 4-byte word.
18942    HFmode constant pool entries are actually loaded with ldr.  */
18943 void
18944 arm_emit_fp16_const (rtx c)
18945 {
18946   REAL_VALUE_TYPE r;
18947   long bits;
18948
18949   REAL_VALUE_FROM_CONST_DOUBLE (r, c);
18950   bits = real_to_target (NULL, &r, HFmode);
18951   if (WORDS_BIG_ENDIAN)
18952     assemble_zeros (2);
18953   assemble_integer (GEN_INT (bits), 2, BITS_PER_WORD, 1);
18954   if (!WORDS_BIG_ENDIAN)
18955     assemble_zeros (2);
18956 }
18957
18958 const char *
18959 arm_output_load_gr (rtx *operands)
18960 {
18961   rtx reg;
18962   rtx offset;
18963   rtx wcgr;
18964   rtx sum;
18965
18966   if (GET_CODE (operands [1]) != MEM
18967       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
18968       || GET_CODE (reg = XEXP (sum, 0)) != REG
18969       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
18970       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
18971     return "wldrw%?\t%0, %1";
18972
18973   /* Fix up an out-of-range load of a GR register.  */
18974   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
18975   wcgr = operands[0];
18976   operands[0] = reg;
18977   output_asm_insn ("ldr%?\t%0, %1", operands);
18978
18979   operands[0] = wcgr;
18980   operands[1] = reg;
18981   output_asm_insn ("tmcr%?\t%0, %1", operands);
18982   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
18983
18984   return "";
18985 }
18986
18987 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
18988
18989    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
18990    named arg and all anonymous args onto the stack.
18991    XXX I know the prologue shouldn't be pushing registers, but it is faster
18992    that way.  */
18993
18994 static void
18995 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
18996                             enum machine_mode mode,
18997                             tree type,
18998                             int *pretend_size,
18999                             int second_time ATTRIBUTE_UNUSED)
19000 {
19001   int nregs = cum->nregs;
19002   if (nregs & 1
19003       && ARM_DOUBLEWORD_ALIGN
19004       && arm_needs_doubleword_align (mode, type))
19005     nregs++;
19006
19007   cfun->machine->uses_anonymous_args = 1;
19008   if (nregs < NUM_ARG_REGS)
19009     *pretend_size = (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
19010 }
19011
19012 /* Return nonzero if the CONSUMER instruction (a store) does not need
19013    PRODUCER's value to calculate the address.  */
19014
19015 int
19016 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
19017 {
19018   rtx value = PATTERN (producer);
19019   rtx addr = PATTERN (consumer);
19020
19021   if (GET_CODE (value) == COND_EXEC)
19022     value = COND_EXEC_CODE (value);
19023   if (GET_CODE (value) == PARALLEL)
19024     value = XVECEXP (value, 0, 0);
19025   value = XEXP (value, 0);
19026   if (GET_CODE (addr) == COND_EXEC)
19027     addr = COND_EXEC_CODE (addr);
19028   if (GET_CODE (addr) == PARALLEL)
19029     addr = XVECEXP (addr, 0, 0);
19030   addr = XEXP (addr, 0);
19031
19032   return !reg_overlap_mentioned_p (value, addr);
19033 }
19034
19035 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
19036    have an early register shift value or amount dependency on the
19037    result of PRODUCER.  */
19038
19039 int
19040 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
19041 {
19042   rtx value = PATTERN (producer);
19043   rtx op = PATTERN (consumer);
19044   rtx early_op;
19045
19046   if (GET_CODE (value) == COND_EXEC)
19047     value = COND_EXEC_CODE (value);
19048   if (GET_CODE (value) == PARALLEL)
19049     value = XVECEXP (value, 0, 0);
19050   value = XEXP (value, 0);
19051   if (GET_CODE (op) == COND_EXEC)
19052     op = COND_EXEC_CODE (op);
19053   if (GET_CODE (op) == PARALLEL)
19054     op = XVECEXP (op, 0, 0);
19055   op = XEXP (op, 1);
19056
19057   early_op = XEXP (op, 0);
19058   /* This is either an actual independent shift, or a shift applied to
19059      the first operand of another operation.  We want the whole shift
19060      operation.  */
19061   if (GET_CODE (early_op) == REG)
19062     early_op = op;
19063
19064   return !reg_overlap_mentioned_p (value, early_op);
19065 }
19066
19067 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
19068    have an early register shift value dependency on the result of
19069    PRODUCER.  */
19070
19071 int
19072 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
19073 {
19074   rtx value = PATTERN (producer);
19075   rtx op = PATTERN (consumer);
19076   rtx early_op;
19077
19078   if (GET_CODE (value) == COND_EXEC)
19079     value = COND_EXEC_CODE (value);
19080   if (GET_CODE (value) == PARALLEL)
19081     value = XVECEXP (value, 0, 0);
19082   value = XEXP (value, 0);
19083   if (GET_CODE (op) == COND_EXEC)
19084     op = COND_EXEC_CODE (op);
19085   if (GET_CODE (op) == PARALLEL)
19086     op = XVECEXP (op, 0, 0);
19087   op = XEXP (op, 1);
19088
19089   early_op = XEXP (op, 0);
19090
19091   /* This is either an actual independent shift, or a shift applied to
19092      the first operand of another operation.  We want the value being
19093      shifted, in either case.  */
19094   if (GET_CODE (early_op) != REG)
19095     early_op = XEXP (early_op, 0);
19096
19097   return !reg_overlap_mentioned_p (value, early_op);
19098 }
19099
19100 /* Return nonzero if the CONSUMER (a mul or mac op) does not
19101    have an early register mult dependency on the result of
19102    PRODUCER.  */
19103
19104 int
19105 arm_no_early_mul_dep (rtx producer, rtx consumer)
19106 {
19107   rtx value = PATTERN (producer);
19108   rtx op = PATTERN (consumer);
19109
19110   if (GET_CODE (value) == COND_EXEC)
19111     value = COND_EXEC_CODE (value);
19112   if (GET_CODE (value) == PARALLEL)
19113     value = XVECEXP (value, 0, 0);
19114   value = XEXP (value, 0);
19115   if (GET_CODE (op) == COND_EXEC)
19116     op = COND_EXEC_CODE (op);
19117   if (GET_CODE (op) == PARALLEL)
19118     op = XVECEXP (op, 0, 0);
19119   op = XEXP (op, 1);
19120
19121   if (GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
19122     {
19123       if (GET_CODE (XEXP (op, 0)) == MULT)
19124         return !reg_overlap_mentioned_p (value, XEXP (op, 0));
19125       else
19126         return !reg_overlap_mentioned_p (value, XEXP (op, 1));
19127     }
19128
19129   return 0;
19130 }
19131
19132 /* We can't rely on the caller doing the proper promotion when
19133    using APCS or ATPCS.  */
19134
19135 static bool
19136 arm_promote_prototypes (const_tree t ATTRIBUTE_UNUSED)
19137 {
19138     return !TARGET_AAPCS_BASED;
19139 }
19140
19141
19142 /* AAPCS based ABIs use short enums by default.  */
19143
19144 static bool
19145 arm_default_short_enums (void)
19146 {
19147   return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
19148 }
19149
19150
19151 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
19152
19153 static bool
19154 arm_align_anon_bitfield (void)
19155 {
19156   return TARGET_AAPCS_BASED;
19157 }
19158
19159
19160 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
19161
19162 static tree
19163 arm_cxx_guard_type (void)
19164 {
19165   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
19166 }
19167
19168 /* Return non-zero if the consumer (a multiply-accumulate instruction)
19169    has an accumulator dependency on the result of the producer (a
19170    multiplication instruction) and no other dependency on that result.  */
19171 int
19172 arm_mac_accumulator_is_mul_result (rtx producer, rtx consumer)
19173 {
19174   rtx mul = PATTERN (producer);
19175   rtx mac = PATTERN (consumer);
19176   rtx mul_result;
19177   rtx mac_op0, mac_op1, mac_acc;
19178
19179   if (GET_CODE (mul) == COND_EXEC)
19180     mul = COND_EXEC_CODE (mul);
19181   if (GET_CODE (mac) == COND_EXEC)
19182     mac = COND_EXEC_CODE (mac);
19183
19184   /* Check that mul is of the form (set (...) (mult ...))
19185      and mla is of the form (set (...) (plus (mult ...) (...))).  */
19186   if ((GET_CODE (mul) != SET || GET_CODE (XEXP (mul, 1)) != MULT)
19187       || (GET_CODE (mac) != SET || GET_CODE (XEXP (mac, 1)) != PLUS
19188           || GET_CODE (XEXP (XEXP (mac, 1), 0)) != MULT))
19189     return 0;
19190
19191   mul_result = XEXP (mul, 0);
19192   mac_op0 = XEXP (XEXP (XEXP (mac, 1), 0), 0);
19193   mac_op1 = XEXP (XEXP (XEXP (mac, 1), 0), 1);
19194   mac_acc = XEXP (XEXP (mac, 1), 1);
19195
19196   return (reg_overlap_mentioned_p (mul_result, mac_acc)
19197           && !reg_overlap_mentioned_p (mul_result, mac_op0)
19198           && !reg_overlap_mentioned_p (mul_result, mac_op1));
19199 }
19200
19201
19202 /* The EABI says test the least significant bit of a guard variable.  */
19203
19204 static bool
19205 arm_cxx_guard_mask_bit (void)
19206 {
19207   return TARGET_AAPCS_BASED;
19208 }
19209
19210
19211 /* The EABI specifies that all array cookies are 8 bytes long.  */
19212
19213 static tree
19214 arm_get_cookie_size (tree type)
19215 {
19216   tree size;
19217
19218   if (!TARGET_AAPCS_BASED)
19219     return default_cxx_get_cookie_size (type);
19220
19221   size = build_int_cst (sizetype, 8);
19222   return size;
19223 }
19224
19225
19226 /* The EABI says that array cookies should also contain the element size.  */
19227
19228 static bool
19229 arm_cookie_has_size (void)
19230 {
19231   return TARGET_AAPCS_BASED;
19232 }
19233
19234
19235 /* The EABI says constructors and destructors should return a pointer to
19236    the object constructed/destroyed.  */
19237
19238 static bool
19239 arm_cxx_cdtor_returns_this (void)
19240 {
19241   return TARGET_AAPCS_BASED;
19242 }
19243
19244 /* The EABI says that an inline function may never be the key
19245    method.  */
19246
19247 static bool
19248 arm_cxx_key_method_may_be_inline (void)
19249 {
19250   return !TARGET_AAPCS_BASED;
19251 }
19252
19253 static void
19254 arm_cxx_determine_class_data_visibility (tree decl)
19255 {
19256   if (!TARGET_AAPCS_BASED
19257       || !TARGET_DLLIMPORT_DECL_ATTRIBUTES)
19258     return;
19259
19260   /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
19261      is exported.  However, on systems without dynamic vague linkage,
19262      \S 3.2.5.6 says that COMDAT class data has hidden linkage.  */
19263   if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
19264     DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
19265   else
19266     DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
19267   DECL_VISIBILITY_SPECIFIED (decl) = 1;
19268 }
19269
19270 static bool
19271 arm_cxx_class_data_always_comdat (void)
19272 {
19273   /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
19274      vague linkage if the class has no key function.  */
19275   return !TARGET_AAPCS_BASED;
19276 }
19277
19278
19279 /* The EABI says __aeabi_atexit should be used to register static
19280    destructors.  */
19281
19282 static bool
19283 arm_cxx_use_aeabi_atexit (void)
19284 {
19285   return TARGET_AAPCS_BASED;
19286 }
19287
19288
19289 void
19290 arm_set_return_address (rtx source, rtx scratch)
19291 {
19292   arm_stack_offsets *offsets;
19293   HOST_WIDE_INT delta;
19294   rtx addr;
19295   unsigned long saved_regs;
19296
19297   offsets = arm_get_frame_offsets ();
19298   saved_regs = offsets->saved_regs_mask;
19299
19300   if ((saved_regs & (1 << LR_REGNUM)) == 0)
19301     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
19302   else
19303     {
19304       if (frame_pointer_needed)
19305         addr = plus_constant(hard_frame_pointer_rtx, -4);
19306       else
19307         {
19308           /* LR will be the first saved register.  */
19309           delta = offsets->outgoing_args - (offsets->frame + 4);
19310
19311
19312           if (delta >= 4096)
19313             {
19314               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
19315                                      GEN_INT (delta & ~4095)));
19316               addr = scratch;
19317               delta &= 4095;
19318             }
19319           else
19320             addr = stack_pointer_rtx;
19321
19322           addr = plus_constant (addr, delta);
19323         }
19324       emit_move_insn (gen_frame_mem (Pmode, addr), source);
19325     }
19326 }
19327
19328
19329 void
19330 thumb_set_return_address (rtx source, rtx scratch)
19331 {
19332   arm_stack_offsets *offsets;
19333   HOST_WIDE_INT delta;
19334   HOST_WIDE_INT limit;
19335   int reg;
19336   rtx addr;
19337   unsigned long mask;
19338
19339   emit_use (source);
19340
19341   offsets = arm_get_frame_offsets ();
19342   mask = offsets->saved_regs_mask;
19343   if (mask & (1 << LR_REGNUM))
19344     {
19345       limit = 1024;
19346       /* Find the saved regs.  */
19347       if (frame_pointer_needed)
19348         {
19349           delta = offsets->soft_frame - offsets->saved_args;
19350           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
19351           if (TARGET_THUMB1)
19352             limit = 128;
19353         }
19354       else
19355         {
19356           delta = offsets->outgoing_args - offsets->saved_args;
19357           reg = SP_REGNUM;
19358         }
19359       /* Allow for the stack frame.  */
19360       if (TARGET_THUMB1 && TARGET_BACKTRACE)
19361         delta -= 16;
19362       /* The link register is always the first saved register.  */
19363       delta -= 4;
19364
19365       /* Construct the address.  */
19366       addr = gen_rtx_REG (SImode, reg);
19367       if (delta > limit)
19368         {
19369           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
19370           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
19371           addr = scratch;
19372         }
19373       else
19374         addr = plus_constant (addr, delta);
19375
19376       emit_move_insn (gen_frame_mem (Pmode, addr), source);
19377     }
19378   else
19379     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
19380 }
19381
19382 /* Implements target hook vector_mode_supported_p.  */
19383 bool
19384 arm_vector_mode_supported_p (enum machine_mode mode)
19385 {
19386   /* Neon also supports V2SImode, etc. listed in the clause below.  */
19387   if (TARGET_NEON && (mode == V2SFmode || mode == V4SImode || mode == V8HImode
19388       || mode == V16QImode || mode == V4SFmode || mode == V2DImode))
19389     return true;
19390
19391   if ((mode == V2SImode)
19392       || (mode == V4HImode)
19393       || (mode == V8QImode))
19394     return true;
19395
19396   return false;
19397 }
19398
19399 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
19400    ARM insns and therefore guarantee that the shift count is modulo 256.
19401    DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
19402    guarantee no particular behavior for out-of-range counts.  */
19403
19404 static unsigned HOST_WIDE_INT
19405 arm_shift_truncation_mask (enum machine_mode mode)
19406 {
19407   return mode == SImode ? 255 : 0;
19408 }
19409
19410
19411 /* Map internal gcc register numbers to DWARF2 register numbers.  */
19412
19413 unsigned int
19414 arm_dbx_register_number (unsigned int regno)
19415 {
19416   if (regno < 16)
19417     return regno;
19418
19419   /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
19420      compatibility.  The EABI defines them as registers 96-103.  */
19421   if (IS_FPA_REGNUM (regno))
19422     return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
19423
19424   /* FIXME: VFPv3 register numbering.  */
19425   if (IS_VFP_REGNUM (regno))
19426     return 64 + regno - FIRST_VFP_REGNUM;
19427
19428   if (IS_IWMMXT_GR_REGNUM (regno))
19429     return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
19430
19431   if (IS_IWMMXT_REGNUM (regno))
19432     return 112 + regno - FIRST_IWMMXT_REGNUM;
19433
19434   gcc_unreachable ();
19435 }
19436
19437
19438 #ifdef TARGET_UNWIND_INFO
19439 /* Emit unwind directives for a store-multiple instruction or stack pointer
19440    push during alignment.
19441    These should only ever be generated by the function prologue code, so
19442    expect them to have a particular form.  */
19443
19444 static void
19445 arm_unwind_emit_sequence (FILE * asm_out_file, rtx p)
19446 {
19447   int i;
19448   HOST_WIDE_INT offset;
19449   HOST_WIDE_INT nregs;
19450   int reg_size;
19451   unsigned reg;
19452   unsigned lastreg;
19453   rtx e;
19454
19455   e = XVECEXP (p, 0, 0);
19456   if (GET_CODE (e) != SET)
19457     abort ();
19458
19459   /* First insn will adjust the stack pointer.  */
19460   if (GET_CODE (e) != SET
19461       || GET_CODE (XEXP (e, 0)) != REG
19462       || REGNO (XEXP (e, 0)) != SP_REGNUM
19463       || GET_CODE (XEXP (e, 1)) != PLUS)
19464     abort ();
19465
19466   offset = -INTVAL (XEXP (XEXP (e, 1), 1));
19467   nregs = XVECLEN (p, 0) - 1;
19468
19469   reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
19470   if (reg < 16)
19471     {
19472       /* The function prologue may also push pc, but not annotate it as it is
19473          never restored.  We turn this into a stack pointer adjustment.  */
19474       if (nregs * 4 == offset - 4)
19475         {
19476           fprintf (asm_out_file, "\t.pad #4\n");
19477           offset -= 4;
19478         }
19479       reg_size = 4;
19480       fprintf (asm_out_file, "\t.save {");
19481     }
19482   else if (IS_VFP_REGNUM (reg))
19483     {
19484       reg_size = 8;
19485       fprintf (asm_out_file, "\t.vsave {");
19486     }
19487   else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
19488     {
19489       /* FPA registers are done differently.  */
19490       asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
19491       return;
19492     }
19493   else
19494     /* Unknown register type.  */
19495     abort ();
19496
19497   /* If the stack increment doesn't match the size of the saved registers,
19498      something has gone horribly wrong.  */
19499   if (offset != nregs * reg_size)
19500     abort ();
19501
19502   offset = 0;
19503   lastreg = 0;
19504   /* The remaining insns will describe the stores.  */
19505   for (i = 1; i <= nregs; i++)
19506     {
19507       /* Expect (set (mem <addr>) (reg)).
19508          Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)).  */
19509       e = XVECEXP (p, 0, i);
19510       if (GET_CODE (e) != SET
19511           || GET_CODE (XEXP (e, 0)) != MEM
19512           || GET_CODE (XEXP (e, 1)) != REG)
19513         abort ();
19514
19515       reg = REGNO (XEXP (e, 1));
19516       if (reg < lastreg)
19517         abort ();
19518
19519       if (i != 1)
19520         fprintf (asm_out_file, ", ");
19521       /* We can't use %r for vfp because we need to use the
19522          double precision register names.  */
19523       if (IS_VFP_REGNUM (reg))
19524         asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
19525       else
19526         asm_fprintf (asm_out_file, "%r", reg);
19527
19528 #ifdef ENABLE_CHECKING
19529       /* Check that the addresses are consecutive.  */
19530       e = XEXP (XEXP (e, 0), 0);
19531       if (GET_CODE (e) == PLUS)
19532         {
19533           offset += reg_size;
19534           if (GET_CODE (XEXP (e, 0)) != REG
19535               || REGNO (XEXP (e, 0)) != SP_REGNUM
19536               || GET_CODE (XEXP (e, 1)) != CONST_INT
19537               || offset != INTVAL (XEXP (e, 1)))
19538             abort ();
19539         }
19540       else if (i != 1
19541                || GET_CODE (e) != REG
19542                || REGNO (e) != SP_REGNUM)
19543         abort ();
19544 #endif
19545     }
19546   fprintf (asm_out_file, "}\n");
19547 }
19548
19549 /*  Emit unwind directives for a SET.  */
19550
19551 static void
19552 arm_unwind_emit_set (FILE * asm_out_file, rtx p)
19553 {
19554   rtx e0;
19555   rtx e1;
19556   unsigned reg;
19557
19558   e0 = XEXP (p, 0);
19559   e1 = XEXP (p, 1);
19560   switch (GET_CODE (e0))
19561     {
19562     case MEM:
19563       /* Pushing a single register.  */
19564       if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
19565           || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
19566           || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
19567         abort ();
19568
19569       asm_fprintf (asm_out_file, "\t.save ");
19570       if (IS_VFP_REGNUM (REGNO (e1)))
19571         asm_fprintf(asm_out_file, "{d%d}\n",
19572                     (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
19573       else
19574         asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
19575       break;
19576
19577     case REG:
19578       if (REGNO (e0) == SP_REGNUM)
19579         {
19580           /* A stack increment.  */
19581           if (GET_CODE (e1) != PLUS
19582               || GET_CODE (XEXP (e1, 0)) != REG
19583               || REGNO (XEXP (e1, 0)) != SP_REGNUM
19584               || GET_CODE (XEXP (e1, 1)) != CONST_INT)
19585             abort ();
19586
19587           asm_fprintf (asm_out_file, "\t.pad #%wd\n",
19588                        -INTVAL (XEXP (e1, 1)));
19589         }
19590       else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
19591         {
19592           HOST_WIDE_INT offset;
19593
19594           if (GET_CODE (e1) == PLUS)
19595             {
19596               if (GET_CODE (XEXP (e1, 0)) != REG
19597                   || GET_CODE (XEXP (e1, 1)) != CONST_INT)
19598                 abort ();
19599               reg = REGNO (XEXP (e1, 0));
19600               offset = INTVAL (XEXP (e1, 1));
19601               asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
19602                            HARD_FRAME_POINTER_REGNUM, reg,
19603                            INTVAL (XEXP (e1, 1)));
19604             }
19605           else if (GET_CODE (e1) == REG)
19606             {
19607               reg = REGNO (e1);
19608               asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
19609                            HARD_FRAME_POINTER_REGNUM, reg);
19610             }
19611           else
19612             abort ();
19613         }
19614       else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
19615         {
19616           /* Move from sp to reg.  */
19617           asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
19618         }
19619      else if (GET_CODE (e1) == PLUS
19620               && GET_CODE (XEXP (e1, 0)) == REG
19621               && REGNO (XEXP (e1, 0)) == SP_REGNUM
19622               && GET_CODE (XEXP (e1, 1)) == CONST_INT)
19623         {
19624           /* Set reg to offset from sp.  */
19625           asm_fprintf (asm_out_file, "\t.movsp %r, #%d\n",
19626                        REGNO (e0), (int)INTVAL(XEXP (e1, 1)));
19627         }
19628       else if (GET_CODE (e1) == UNSPEC && XINT (e1, 1) == UNSPEC_STACK_ALIGN)
19629         {
19630           /* Stack pointer save before alignment.  */
19631           reg = REGNO (e0);
19632           asm_fprintf (asm_out_file, "\t.unwind_raw 0, 0x%x @ vsp = r%d\n",
19633                        reg + 0x90, reg);
19634         }
19635       else
19636         abort ();
19637       break;
19638
19639     default:
19640       abort ();
19641     }
19642 }
19643
19644
19645 /* Emit unwind directives for the given insn.  */
19646
19647 static void
19648 arm_unwind_emit (FILE * asm_out_file, rtx insn)
19649 {
19650   rtx pat;
19651
19652   if (!ARM_EABI_UNWIND_TABLES)
19653     return;
19654
19655   if (!(flag_unwind_tables || crtl->uses_eh_lsda)
19656       && (TREE_NOTHROW (current_function_decl)
19657           || crtl->all_throwers_are_sibcalls))
19658     return;
19659
19660   if (GET_CODE (insn) == NOTE || !RTX_FRAME_RELATED_P (insn))
19661     return;
19662
19663   pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
19664   if (pat)
19665     pat = XEXP (pat, 0);
19666   else
19667     pat = PATTERN (insn);
19668
19669   switch (GET_CODE (pat))
19670     {
19671     case SET:
19672       arm_unwind_emit_set (asm_out_file, pat);
19673       break;
19674
19675     case SEQUENCE:
19676       /* Store multiple.  */
19677       arm_unwind_emit_sequence (asm_out_file, pat);
19678       break;
19679
19680     default:
19681       abort();
19682     }
19683 }
19684
19685
19686 /* Output a reference from a function exception table to the type_info
19687    object X.  The EABI specifies that the symbol should be relocated by
19688    an R_ARM_TARGET2 relocation.  */
19689
19690 static bool
19691 arm_output_ttype (rtx x)
19692 {
19693   fputs ("\t.word\t", asm_out_file);
19694   output_addr_const (asm_out_file, x);
19695   /* Use special relocations for symbol references.  */
19696   if (GET_CODE (x) != CONST_INT)
19697     fputs ("(TARGET2)", asm_out_file);
19698   fputc ('\n', asm_out_file);
19699
19700   return TRUE;
19701 }
19702 #endif /* TARGET_UNWIND_INFO */
19703
19704
19705 /* Handle UNSPEC DWARF call frame instructions.  These are needed for dynamic
19706    stack alignment.  */
19707
19708 static void
19709 arm_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
19710 {
19711   rtx unspec = SET_SRC (pattern);
19712   gcc_assert (GET_CODE (unspec) == UNSPEC);
19713
19714   switch (index)
19715     {
19716     case UNSPEC_STACK_ALIGN:
19717       /* ??? We should set the CFA = (SP & ~7).  At this point we haven't
19718          put anything on the stack, so hopefully it won't matter.
19719          CFA = SP will be correct after alignment.  */
19720       dwarf2out_reg_save_reg (label, stack_pointer_rtx,
19721                               SET_DEST (pattern));
19722       break;
19723     default:
19724       gcc_unreachable ();
19725     }
19726 }
19727
19728
19729 /* Output unwind directives for the start/end of a function.  */
19730
19731 void
19732 arm_output_fn_unwind (FILE * f, bool prologue)
19733 {
19734   if (!ARM_EABI_UNWIND_TABLES)
19735     return;
19736
19737   if (prologue)
19738     fputs ("\t.fnstart\n", f);
19739   else
19740     {
19741       /* If this function will never be unwound, then mark it as such.
19742          The came condition is used in arm_unwind_emit to suppress
19743          the frame annotations.  */
19744       if (!(flag_unwind_tables || crtl->uses_eh_lsda)
19745           && (TREE_NOTHROW (current_function_decl)
19746               || crtl->all_throwers_are_sibcalls))
19747         fputs("\t.cantunwind\n", f);
19748
19749       fputs ("\t.fnend\n", f);
19750     }
19751 }
19752
19753 static bool
19754 arm_emit_tls_decoration (FILE *fp, rtx x)
19755 {
19756   enum tls_reloc reloc;
19757   rtx val;
19758
19759   val = XVECEXP (x, 0, 0);
19760   reloc = (enum tls_reloc) INTVAL (XVECEXP (x, 0, 1));
19761
19762   output_addr_const (fp, val);
19763
19764   switch (reloc)
19765     {
19766     case TLS_GD32:
19767       fputs ("(tlsgd)", fp);
19768       break;
19769     case TLS_LDM32:
19770       fputs ("(tlsldm)", fp);
19771       break;
19772     case TLS_LDO32:
19773       fputs ("(tlsldo)", fp);
19774       break;
19775     case TLS_IE32:
19776       fputs ("(gottpoff)", fp);
19777       break;
19778     case TLS_LE32:
19779       fputs ("(tpoff)", fp);
19780       break;
19781     default:
19782       gcc_unreachable ();
19783     }
19784
19785   switch (reloc)
19786     {
19787     case TLS_GD32:
19788     case TLS_LDM32:
19789     case TLS_IE32:
19790       fputs (" + (. - ", fp);
19791       output_addr_const (fp, XVECEXP (x, 0, 2));
19792       fputs (" - ", fp);
19793       output_addr_const (fp, XVECEXP (x, 0, 3));
19794       fputc (')', fp);
19795       break;
19796     default:
19797       break;
19798     }
19799
19800   return TRUE;
19801 }
19802
19803 /* ARM implementation of TARGET_ASM_OUTPUT_DWARF_DTPREL.  */
19804
19805 static void
19806 arm_output_dwarf_dtprel (FILE *file, int size, rtx x)
19807 {
19808   gcc_assert (size == 4);
19809   fputs ("\t.word\t", file);
19810   output_addr_const (file, x);
19811   fputs ("(tlsldo)", file);
19812 }
19813
19814 bool
19815 arm_output_addr_const_extra (FILE *fp, rtx x)
19816 {
19817   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
19818     return arm_emit_tls_decoration (fp, x);
19819   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
19820     {
19821       char label[256];
19822       int labelno = INTVAL (XVECEXP (x, 0, 0));
19823
19824       ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
19825       assemble_name_raw (fp, label);
19826
19827       return TRUE;
19828     }
19829   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_GOTSYM_OFF)
19830     {
19831       assemble_name (fp, "_GLOBAL_OFFSET_TABLE_");
19832       if (GOT_PCREL)
19833         fputs ("+.", fp);
19834       fputs ("-(", fp);
19835       output_addr_const (fp, XVECEXP (x, 0, 0));
19836       fputc (')', fp);
19837       return TRUE;
19838     }
19839   else if (GET_CODE (x) == CONST_VECTOR)
19840     return arm_emit_vector_const (fp, x);
19841
19842   return FALSE;
19843 }
19844
19845 /* Output assembly for a shift instruction.
19846    SET_FLAGS determines how the instruction modifies the condition codes.
19847    0 - Do not set condition codes.
19848    1 - Set condition codes.
19849    2 - Use smallest instruction.  */
19850 const char *
19851 arm_output_shift(rtx * operands, int set_flags)
19852 {
19853   char pattern[100];
19854   static const char flag_chars[3] = {'?', '.', '!'};
19855   const char *shift;
19856   HOST_WIDE_INT val;
19857   char c;
19858   
19859   c = flag_chars[set_flags];
19860   if (TARGET_UNIFIED_ASM)
19861     {
19862       shift = shift_op(operands[3], &val);
19863       if (shift)
19864         {
19865           if (val != -1)
19866             operands[2] = GEN_INT(val);
19867           sprintf (pattern, "%s%%%c\t%%0, %%1, %%2", shift, c);
19868         }
19869       else
19870         sprintf (pattern, "mov%%%c\t%%0, %%1", c);
19871     }
19872   else
19873     sprintf (pattern, "mov%%%c\t%%0, %%1%%S3", c);
19874   output_asm_insn (pattern, operands);
19875   return "";
19876 }
19877
19878 /* Output a Thumb-1 casesi dispatch sequence.  */
19879 const char *
19880 thumb1_output_casesi (rtx *operands)
19881 {
19882   rtx diff_vec = PATTERN (next_real_insn (operands[0]));
19883   addr_diff_vec_flags flags;
19884
19885   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
19886
19887   flags = ADDR_DIFF_VEC_FLAGS (diff_vec);
19888
19889   switch (GET_MODE(diff_vec))
19890     {
19891     case QImode:
19892       return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned ? 
19893               "bl\t%___gnu_thumb1_case_uqi" : "bl\t%___gnu_thumb1_case_sqi");
19894     case HImode:
19895       return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned ? 
19896               "bl\t%___gnu_thumb1_case_uhi" : "bl\t%___gnu_thumb1_case_shi");
19897     case SImode:
19898       return "bl\t%___gnu_thumb1_case_si";
19899     default:
19900       gcc_unreachable ();
19901     }
19902 }
19903
19904 /* Output a Thumb-2 casesi instruction.  */
19905 const char *
19906 thumb2_output_casesi (rtx *operands)
19907 {
19908   rtx diff_vec = PATTERN (next_real_insn (operands[2]));
19909
19910   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
19911
19912   output_asm_insn ("cmp\t%0, %1", operands);
19913   output_asm_insn ("bhi\t%l3", operands);
19914   switch (GET_MODE(diff_vec))
19915     {
19916     case QImode:
19917       return "tbb\t[%|pc, %0]";
19918     case HImode:
19919       return "tbh\t[%|pc, %0, lsl #1]";
19920     case SImode:
19921       if (flag_pic)
19922         {
19923           output_asm_insn ("adr\t%4, %l2", operands);
19924           output_asm_insn ("ldr\t%5, [%4, %0, lsl #2]", operands);
19925           output_asm_insn ("add\t%4, %4, %5", operands);
19926           return "bx\t%4";
19927         }
19928       else
19929         {
19930           output_asm_insn ("adr\t%4, %l2", operands);
19931           return "ldr\t%|pc, [%4, %0, lsl #2]";
19932         }
19933     default:
19934       gcc_unreachable ();
19935     }
19936 }
19937
19938 /* Most ARM cores are single issue, but some newer ones can dual issue.
19939    The scheduler descriptions rely on this being correct.  */
19940 static int
19941 arm_issue_rate (void)
19942 {
19943   switch (arm_tune)
19944     {
19945     case cortexr4:
19946     case cortexr4f:
19947     case cortexa8:
19948     case cortexa9:
19949       return 2;
19950
19951     default:
19952       return 1;
19953     }
19954 }
19955
19956 /* A table and a function to perform ARM-specific name mangling for
19957    NEON vector types in order to conform to the AAPCS (see "Procedure
19958    Call Standard for the ARM Architecture", Appendix A).  To qualify
19959    for emission with the mangled names defined in that document, a
19960    vector type must not only be of the correct mode but also be
19961    composed of NEON vector element types (e.g. __builtin_neon_qi).  */
19962 typedef struct
19963 {
19964   enum machine_mode mode;
19965   const char *element_type_name;
19966   const char *aapcs_name;
19967 } arm_mangle_map_entry;
19968
19969 static arm_mangle_map_entry arm_mangle_map[] = {
19970   /* 64-bit containerized types.  */
19971   { V8QImode,  "__builtin_neon_qi",     "15__simd64_int8_t" },
19972   { V8QImode,  "__builtin_neon_uqi",    "16__simd64_uint8_t" },
19973   { V4HImode,  "__builtin_neon_hi",     "16__simd64_int16_t" },
19974   { V4HImode,  "__builtin_neon_uhi",    "17__simd64_uint16_t" },
19975   { V2SImode,  "__builtin_neon_si",     "16__simd64_int32_t" },
19976   { V2SImode,  "__builtin_neon_usi",    "17__simd64_uint32_t" },
19977   { V2SFmode,  "__builtin_neon_sf",     "18__simd64_float32_t" },
19978   { V8QImode,  "__builtin_neon_poly8",  "16__simd64_poly8_t" },
19979   { V4HImode,  "__builtin_neon_poly16", "17__simd64_poly16_t" },
19980   /* 128-bit containerized types.  */
19981   { V16QImode, "__builtin_neon_qi",     "16__simd128_int8_t" },
19982   { V16QImode, "__builtin_neon_uqi",    "17__simd128_uint8_t" },
19983   { V8HImode,  "__builtin_neon_hi",     "17__simd128_int16_t" },
19984   { V8HImode,  "__builtin_neon_uhi",    "18__simd128_uint16_t" },
19985   { V4SImode,  "__builtin_neon_si",     "17__simd128_int32_t" },
19986   { V4SImode,  "__builtin_neon_usi",    "18__simd128_uint32_t" },
19987   { V4SFmode,  "__builtin_neon_sf",     "19__simd128_float32_t" },
19988   { V16QImode, "__builtin_neon_poly8",  "17__simd128_poly8_t" },
19989   { V8HImode,  "__builtin_neon_poly16", "18__simd128_poly16_t" },
19990   { VOIDmode, NULL, NULL }
19991 };
19992
19993 const char *
19994 arm_mangle_type (const_tree type)
19995 {
19996   arm_mangle_map_entry *pos = arm_mangle_map;
19997
19998   /* The ARM ABI documents (10th October 2008) say that "__va_list"
19999      has to be managled as if it is in the "std" namespace.  */
20000   if (TARGET_AAPCS_BASED 
20001       && lang_hooks.types_compatible_p (CONST_CAST_TREE (type), va_list_type))
20002     {
20003       static bool warned;
20004       if (!warned && warn_psabi)
20005         {
20006           warned = true;
20007           inform (input_location,
20008                   "the mangling of %<va_list%> has changed in GCC 4.4");
20009         }
20010       return "St9__va_list";
20011     }
20012
20013   /* Half-precision float.  */
20014   if (TREE_CODE (type) == REAL_TYPE && TYPE_PRECISION (type) == 16)
20015     return "Dh";
20016
20017   if (TREE_CODE (type) != VECTOR_TYPE)
20018     return NULL;
20019
20020   /* Check the mode of the vector type, and the name of the vector
20021      element type, against the table.  */
20022   while (pos->mode != VOIDmode)
20023     {
20024       tree elt_type = TREE_TYPE (type);
20025
20026       if (pos->mode == TYPE_MODE (type)
20027           && TREE_CODE (TYPE_NAME (elt_type)) == TYPE_DECL
20028           && !strcmp (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (elt_type))),
20029                       pos->element_type_name))
20030         return pos->aapcs_name;
20031
20032       pos++;
20033     }
20034
20035   /* Use the default mangling for unrecognized (possibly user-defined)
20036      vector types.  */
20037   return NULL;
20038 }
20039
20040 /* Order of allocation of core registers for Thumb: this allocation is
20041    written over the corresponding initial entries of the array
20042    initialized with REG_ALLOC_ORDER.  We allocate all low registers
20043    first.  Saving and restoring a low register is usually cheaper than
20044    using a call-clobbered high register.  */
20045
20046 static const int thumb_core_reg_alloc_order[] =
20047 {
20048    3,  2,  1,  0,  4,  5,  6,  7,
20049   14, 12,  8,  9, 10, 11, 13, 15
20050 };
20051
20052 /* Adjust register allocation order when compiling for Thumb.  */
20053
20054 void
20055 arm_order_regs_for_local_alloc (void)
20056 {
20057   const int arm_reg_alloc_order[] = REG_ALLOC_ORDER;
20058   memcpy(reg_alloc_order, arm_reg_alloc_order, sizeof (reg_alloc_order));
20059   if (TARGET_THUMB)
20060     memcpy (reg_alloc_order, thumb_core_reg_alloc_order,
20061             sizeof (thumb_core_reg_alloc_order));
20062 }
20063
20064 /* Set default optimization options.  */
20065 void
20066 arm_optimization_options (int level, int size ATTRIBUTE_UNUSED)
20067 {
20068   /* Enable section anchors by default at -O1 or higher.
20069      Use 2 to distinguish from an explicit -fsection-anchors
20070      given on the command line.  */
20071   if (level > 0)
20072     flag_section_anchors = 2;
20073 }
20074
20075 #include "gt-arm.h"